Release 951226
[wine/gsoc-2012-control.git] / miscemu / instr.c
blob8b795a960cff02aa04c15134b3799dd9afb638aa
1 /*
2 * Emulation of priviledged instructions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdio.h>
8 #include "windows.h"
9 #include "ldt.h"
10 #include "miscemu.h"
11 #include "registers.h"
14 #define STACK_reg(context) \
15 ((GET_SEL_FLAGS(SS_reg(context)) & LDT_FLAGS_32BIT) ? \
16 ESP_reg(context) : SP_reg(context))
18 #define STACK_PTR(context) \
19 (PTR_SEG_OFF_TO_LIN(SS_reg(context),STACK_reg(context)))
21 /***********************************************************************
22 * INSTR_ReplaceSelector
24 * Try to replace an invalid selector by a valid one.
25 * For now, only selector 0x40 is handled here.
27 static WORD INSTR_ReplaceSelector( struct sigcontext_struct *context, WORD sel)
29 if (sel == 0x40)
31 fprintf( stderr, "Direct access to segment 0x40 (cs:ip=%04x:%04lx).\n",
32 CS_reg(context), EIP_reg(context) );
33 return DOSMEM_BiosSeg;
35 return 0; /* Can't replace selector */
39 /***********************************************************************
40 * INSTR_GetOperandAddr
42 * Return the address of an instruction operand (from the mod/rm byte).
44 static BYTE *INSTR_GetOperandAddr( struct sigcontext_struct *context,
45 BYTE *instr, int long_addr,
46 int segprefix, int *len )
48 int mod, rm, base, index = 0, ss = 0, seg = 0, off;
50 #define GET_VAL(val,type) \
51 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
53 *len = 0;
54 GET_VAL( &mod, BYTE );
55 rm = mod & 7;
56 mod >>= 6;
58 if (mod == 3)
60 switch(rm)
62 case 0: return (BYTE *)&EAX_reg(context);
63 case 1: return (BYTE *)&ECX_reg(context);
64 case 2: return (BYTE *)&EDX_reg(context);
65 case 3: return (BYTE *)&EBX_reg(context);
66 case 4: return (BYTE *)&ESP_reg(context);
67 case 5: return (BYTE *)&EBP_reg(context);
68 case 6: return (BYTE *)&ESI_reg(context);
69 case 7: return (BYTE *)&EDI_reg(context);
73 if (long_addr)
75 if (rm == 4)
77 BYTE sib;
78 GET_VAL( &sib, BYTE );
79 rm = sib & 7;
80 ss = sib >> 6;
81 switch(sib >> 3)
83 case 0: index = EAX_reg(context); break;
84 case 1: index = ECX_reg(context); break;
85 case 2: index = EDX_reg(context); break;
86 case 3: index = EBX_reg(context); break;
87 case 4: index = 0; break;
88 case 5: index = EBP_reg(context); break;
89 case 6: index = ESI_reg(context); break;
90 case 7: index = EDI_reg(context); break;
94 switch(rm)
96 case 0: base = EAX_reg(context); seg = DS_reg(context); break;
97 case 1: base = ECX_reg(context); seg = DS_reg(context); break;
98 case 2: base = EDX_reg(context); seg = DS_reg(context); break;
99 case 3: base = EBX_reg(context); seg = DS_reg(context); break;
100 case 4: base = ESP_reg(context); seg = SS_reg(context); break;
101 case 5: base = EBP_reg(context); seg = SS_reg(context); break;
102 case 6: base = ESI_reg(context); seg = DS_reg(context); break;
103 case 7: base = EDI_reg(context); seg = DS_reg(context); break;
105 switch (mod)
107 case 0:
108 if (rm == 5) /* special case: ds:(disp32) */
110 GET_VAL( &base, DWORD );
111 seg = DS_reg(context);
113 break;
115 case 1: /* 8-bit disp */
116 GET_VAL( &off, BYTE );
117 base += (signed char)off;
118 break;
120 case 2: /* 32-bit disp */
121 GET_VAL( &off, DWORD );
122 base += (signed long)off;
123 break;
126 else /* short address */
128 switch(rm)
130 case 0: /* ds:(bx,si) */
131 base = BX_reg(context) + SI_reg(context);
132 seg = DS_reg(context);
133 break;
134 case 1: /* ds:(bx,di) */
135 base = BX_reg(context) + DI_reg(context);
136 seg = DS_reg(context);
137 break;
138 case 2: /* ss:(bp,si) */
139 base = BP_reg(context) + SI_reg(context);
140 seg = SS_reg(context);
141 break;
142 case 3: /* ss:(bp,di) */
143 base = BP_reg(context) + DI_reg(context);
144 seg = SS_reg(context);
145 break;
146 case 4: /* ds:(si) */
147 base = SI_reg(context);
148 seg = DS_reg(context);
149 break;
150 case 5: /* ds:(di) */
151 base = DI_reg(context);
152 seg = DS_reg(context);
153 break;
154 case 6: /* ss:(bp) */
155 base = BP_reg(context);
156 seg = SS_reg(context);
157 break;
158 case 7: /* ds:(bx) */
159 base = BX_reg(context);
160 seg = DS_reg(context);
161 break;
164 switch(mod)
166 case 0:
167 if (rm == 6) /* special case: ds:(disp16) */
169 GET_VAL( &base, WORD );
170 seg = DS_reg(context);
172 break;
174 case 1: /* 8-bit disp */
175 GET_VAL( &off, BYTE );
176 base += (signed char)off;
177 break;
179 case 2: /* 16-bit disp */
180 GET_VAL( &off, WORD );
181 base += (signed short)off;
182 break;
184 base &= 0xffff;
186 if (segprefix != -1) seg = segprefix;
188 /* FIXME: should check limit of the segment here */
189 return (BYTE *)PTR_SEG_OFF_TO_LIN( seg, (base + (index << ss)) );
193 /***********************************************************************
194 * INSTR_EmulateLDS
196 * Emulate the LDS (and LES,LFS,etc.) instruction.
198 static BOOL INSTR_EmulateLDS( struct sigcontext_struct *context,
199 BYTE *instr, int long_op, int long_addr,
200 int segprefix, int *len )
202 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
203 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
204 long_addr, segprefix, len );
205 WORD seg = *(WORD *)(addr + (long_op ? 4 : 2));
207 if (!(seg = INSTR_ReplaceSelector( context, seg )))
208 return FALSE; /* Unable to emulate it */
210 /* Now store the offset in the correct register */
212 switch((*regmodrm >> 3) & 7)
214 case 0:
215 if (long_op) EAX_reg(context) = *(DWORD *)addr;
216 else AX_reg(context) = *(WORD *)addr;
217 break;
218 case 1:
219 if (long_op) ECX_reg(context) = *(DWORD *)addr;
220 else CX_reg(context) = *(WORD *)addr;
221 break;
222 case 2:
223 if (long_op) EDX_reg(context) = *(DWORD *)addr;
224 else DX_reg(context) = *(WORD *)addr;
225 break;
226 case 3:
227 if (long_op) EBX_reg(context) = *(DWORD *)addr;
228 else BX_reg(context) = *(WORD *)addr;
229 break;
230 case 4:
231 if (long_op) ESP_reg(context) = *(DWORD *)addr;
232 else SP_reg(context) = *(WORD *)addr;
233 break;
234 case 5:
235 if (long_op) EBP_reg(context) = *(DWORD *)addr;
236 else BP_reg(context) = *(WORD *)addr;
237 break;
238 case 6:
239 if (long_op) ESI_reg(context) = *(DWORD *)addr;
240 else SI_reg(context) = *(WORD *)addr;
241 break;
242 case 7:
243 if (long_op) EDI_reg(context) = *(DWORD *)addr;
244 else DI_reg(context) = *(WORD *)addr;
245 break;
248 /* Store the correct segment in the segment register */
250 switch(*instr)
252 case 0xc4: ES_reg(context) = seg; break; /* les */
253 case 0xc5: DS_reg(context) = seg; break; /* lds */
254 case 0x0f: switch(instr[1])
256 case 0xb2: SS_reg(context) = seg; break; /* lss */
257 #ifdef FS_reg
258 case 0xb4: FS_reg(context) = seg; break; /* lfs */
259 #endif
260 #ifdef GS_reg
261 case 0xb5: GS_reg(context) = seg; break; /* lgs */
262 #endif
264 break;
267 /* Add the opcode size to the total length */
269 *len += 1 + (*instr == 0x0f);
270 return TRUE;
274 /***********************************************************************
275 * INSTR_EmulateInstruction
277 * Emulate a priviledged instruction. Returns TRUE if emulation successful.
279 BOOL INSTR_EmulateInstruction( struct sigcontext_struct *context )
281 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
282 BYTE *instr;
284 long_op = long_addr = (GET_SEL_FLAGS(CS_reg(context)) & LDT_FLAGS_32BIT) != 0;
285 instr = (BYTE *) PTR_SEG_OFF_TO_LIN( CS_reg(context), EIP_reg(context) );
287 /* First handle any possible prefix */
289 segprefix = -1; /* no prefix */
290 prefix = 1;
291 repX = 0;
292 prefixlen = 0;
293 while(prefix)
295 switch(*instr)
297 case 0x2e:
298 segprefix = CS_reg(context);
299 break;
300 case 0x36:
301 segprefix = SS_reg(context);
302 break;
303 case 0x3e:
304 segprefix = DS_reg(context);
305 break;
306 case 0x26:
307 segprefix = ES_reg(context);
308 break;
309 #ifdef FS_reg
310 case 0x64:
311 segprefix = FS_reg(context);
312 break;
313 #endif
314 #ifdef GS_reg
315 case 0x65:
316 segprefix = GS_reg(context);
317 break;
318 #endif
319 case 0x66:
320 long_op = !long_op; /* opcode size prefix */
321 break;
322 case 0x67:
323 long_addr = !long_addr; /* addr size prefix */
324 break;
325 case 0xf0: /* lock */
326 break;
327 case 0xf2: /* repne */
328 repX = 1;
329 break;
330 case 0xf3: /* repe */
331 repX = 2;
332 break;
333 default:
334 prefix = 0; /* no more prefixes */
335 break;
337 if (prefix)
339 instr++;
340 prefixlen++;
344 /* Now look at the actual instruction */
346 switch(*instr)
348 case 0x07: /* pop es */
349 case 0x17: /* pop ss */
350 case 0x1f: /* pop ds */
352 WORD seg = *(WORD *)STACK_PTR( context );
353 if ((seg = INSTR_ReplaceSelector( context, seg )) != 0)
355 switch(*instr)
357 case 0x07: ES_reg(context) = seg; break;
358 case 0x17: SS_reg(context) = seg; break;
359 case 0x1f: DS_reg(context) = seg; break;
361 STACK_reg(context) += long_op ? 4 : 2;
362 EIP_reg(context) += prefixlen + 1;
363 return TRUE;
366 break; /* Unable to emulate it */
368 case 0x0f: /* extended instruction */
369 switch(instr[1])
371 #ifdef FS_reg
372 case 0xa1: /* pop fs */
374 WORD seg = *(WORD *)STACK_PTR( context );
375 if ((seg = INSTR_ReplaceSelector( context, seg )) != 0)
377 FS_reg(context) = seg;
378 STACK_reg(context) += long_op ? 4 : 2;
379 EIP_reg(context) += prefixlen + 2;
380 return TRUE;
383 break;
384 #endif /* FS_reg */
386 #ifdef GS_reg
387 case 0xa9: /* pop gs */
389 WORD seg = *(WORD *)STACK_PTR( context );
390 if ((seg = INSTR_ReplaceSelector( context, seg )) != 0)
392 GS_reg(context) = seg;
393 STACK_reg(context) += long_op ? 4 : 2;
394 EIP_reg(context) += prefixlen + 2;
395 return TRUE;
398 break;
399 #endif /* GS_reg */
401 case 0xb2: /* lss addr,reg */
402 #ifdef FS_reg
403 case 0xb4: /* lfs addr,reg */
404 #endif
405 #ifdef GS_reg
406 case 0xb5: /* lgs addr,reg */
407 #endif
408 if (INSTR_EmulateLDS( context, instr, long_op,
409 long_addr, segprefix, &len ))
411 EIP_reg(context) += prefixlen + len;
412 return TRUE;
414 break;
416 break; /* Unable to emulate it */
418 case 0x6c: /* insb */
419 case 0x6d: /* insw/d */
420 case 0x6e: /* outsb */
421 case 0x6f: /* outsw/d */
423 int typ = *instr; /* Just in case it's overwritten. */
424 int outp = (typ >= 0x6e);
425 unsigned long count = repX ?
426 (long_addr ? ECX_reg(context) : CX_reg(context)) : 1;
427 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
428 int step = (EFL_reg(context) & 0x400) ? -opsize : +opsize;
429 int seg = outp ? (segprefix >= 0 ? segprefix : DS_reg(context))
430 : ES_reg(context); /* FIXME: is this right? */
432 if (outp)
433 /* FIXME: Check segment readable. */
434 (void)0;
435 else
436 /* FIXME: Check segment writeable. */
437 (void)0;
439 if (repX)
440 if (long_addr)
441 ECX_reg(context) = 0;
442 else
443 CX_reg(context) = 0;
445 while (count-- > 0)
447 void *data;
448 if (outp)
450 data = PTR_SEG_OFF_TO_LIN (seg,
451 long_addr ? ESI_reg(context) : SI_reg(context));
452 if (long_addr) ESI_reg(context) += step;
453 else SI_reg(context) += step;
455 else
457 data = PTR_SEG_OFF_TO_LIN (seg,
458 long_addr ? EDI_reg(context) : DI_reg(context));
459 if (long_addr) EDI_reg(context) += step;
460 else DI_reg(context) += step;
463 switch (typ)
465 case 0x6c:
466 *((BYTE *)data) = inport( DX_reg(context), 1);
467 break;
468 case 0x6d:
469 if (long_op)
470 *((DWORD *)data) = inport( DX_reg(context), 4);
471 else
472 *((WORD *)data) = inport( DX_reg(context), 2);
473 break;
474 case 0x6e:
475 outport( DX_reg(context), 1, *((BYTE *)data));
476 break;
477 case 0x6f:
478 if (long_op)
479 outport( DX_reg(context), 4, *((DWORD *)data));
480 else
481 outport( DX_reg(context), 2, *((WORD *)data));
482 break;
485 EIP_reg(context) += prefixlen + 1;
487 return TRUE;
489 case 0x8e: /* mov XX,segment_reg */
491 WORD seg = *(WORD *)INSTR_GetOperandAddr( context, instr + 1,
492 long_addr, segprefix, &len );
493 if (!(seg = INSTR_ReplaceSelector( context, seg )))
494 break; /* Unable to emulate it */
496 switch((instr[1] >> 3) & 7)
498 case 0:
499 ES_reg(context) = seg;
500 EIP_reg(context) += prefixlen + len + 1;
501 return TRUE;
502 case 1: /* cs */
503 break;
504 case 2:
505 SS_reg(context) = seg;
506 EIP_reg(context) += prefixlen + len + 1;
507 return TRUE;
508 case 3:
509 DS_reg(context) = seg;
510 EIP_reg(context) += prefixlen + len + 1;
511 return TRUE;
512 case 4:
513 #ifdef FS_reg
514 FS_reg(context) = seg;
515 EIP_reg(context) += prefixlen + len + 1;
516 return TRUE;
517 #endif
518 case 5:
519 #ifdef GS_reg
520 GS_reg(context) = seg;
521 EIP_reg(context) += prefixlen + len + 1;
522 return TRUE;
523 #endif
524 case 6: /* unused */
525 case 7: /* unused */
526 break;
529 break; /* Unable to emulate it */
531 case 0xc4: /* les addr,reg */
532 case 0xc5: /* lds addr,reg */
533 if (INSTR_EmulateLDS( context, instr, long_op,
534 long_addr, segprefix, &len ))
536 EIP_reg(context) += prefixlen + len;
537 return TRUE;
539 break; /* Unable to emulate it */
541 case 0xcd: /* int <XX> */
542 if (long_op)
544 fprintf(stderr, "int xx from 32-bit code is not supported.\n");
545 break; /* Unable to emulate it */
547 else
549 SEGPTR addr = INT_GetHandler( instr[1] );
550 WORD *stack = (WORD *)STACK_PTR( context );
551 /* Push the flags and return address on the stack */
552 *(--stack) = FL_reg(context);
553 *(--stack) = CS_reg(context);
554 *(--stack) = IP_reg(context) + prefixlen + 2;
555 STACK_reg(context) -= 3 * sizeof(WORD);
556 /* Jump to the interrupt handler */
557 CS_reg(context) = HIWORD(addr);
558 EIP_reg(context) = LOWORD(addr);
560 return TRUE;
562 case 0xcf: /* iret */
563 if (long_op)
565 DWORD *stack = (DWORD *)STACK_PTR( context );
566 EIP_reg(context) = *stack++;
567 CS_reg(context) = *stack++;
568 EFL_reg(context) = *stack;
569 STACK_reg(context) += 3*sizeof(DWORD); /* Pop the return address and flags */
571 else
573 WORD *stack = (WORD *)STACK_PTR( context );
574 EIP_reg(context) = *stack++;
575 CS_reg(context) = *stack++;
576 FL_reg(context) = *stack;
577 STACK_reg(context) += 3*sizeof(WORD); /* Pop the return address and flags */
579 return TRUE;
581 case 0xe4: /* inb al,XX */
582 AL_reg(context) = inport( instr[1], 1 );
583 EIP_reg(context) += prefixlen + 2;
584 return TRUE;
586 case 0xe5: /* in (e)ax,XX */
587 if (long_op) EAX_reg(context) = inport( instr[1], 4 );
588 else AX_reg(context) = inport( instr[1], 2 );
589 EIP_reg(context) += prefixlen + 2;
590 return TRUE;
592 case 0xe6: /* outb XX,al */
593 outport( instr[1], 1, AL_reg(context) );
594 EIP_reg(context) += prefixlen + 2;
595 return TRUE;
597 case 0xe7: /* out XX,(e)ax */
598 if (long_op) outport( instr[1], 4, EAX_reg(context) );
599 else outport( instr[1], 2, AX_reg(context) );
600 EIP_reg(context) += prefixlen + 2;
601 return TRUE;
603 case 0xec: /* inb al,dx */
604 AL_reg(context) = inport( DX_reg(context), 1 );
605 EIP_reg(context) += prefixlen + 1;
606 return TRUE;
608 case 0xed: /* in (e)ax,dx */
609 if (long_op) EAX_reg(context) = inport( DX_reg(context), 4 );
610 else AX_reg(context) = inport( DX_reg(context), 2 );
611 EIP_reg(context) += prefixlen + 1;
612 return TRUE;
614 case 0xee: /* outb dx,al */
615 outport( DX_reg(context), 1, AL_reg(context) );
616 EIP_reg(context) += prefixlen + 1;
617 return TRUE;
619 case 0xef: /* out dx,(e)ax */
620 if (long_op) outport( DX_reg(context), 4, EAX_reg(context) );
621 else outport( DX_reg(context), 2, AX_reg(context) );
622 EIP_reg(context) += prefixlen + 1;
623 return TRUE;
625 case 0xfa: /* cli, ignored */
626 EIP_reg(context) += prefixlen + 1;
627 return TRUE;
629 case 0xfb: /* sti, ignored */
630 EIP_reg(context) += prefixlen + 1;
631 return TRUE;
633 fprintf(stderr, "Unexpected Windows program segfault"
634 " - opcode = %x\n", *instr);
635 return FALSE; /* Unable to emulate it */