2 * Emulation of priviledged instructions
4 * Copyright 1995 Alexandre Julliard
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
)
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); }
54 GET_VAL( &mod
, BYTE
);
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
);
78 GET_VAL( &sib
, BYTE
);
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;
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;
108 if (rm
== 5) /* special case: ds:(disp32) */
110 GET_VAL( &base
, DWORD
);
111 seg
= DS_reg(context
);
115 case 1: /* 8-bit disp */
116 GET_VAL( &off
, BYTE
);
117 base
+= (signed char)off
;
120 case 2: /* 32-bit disp */
121 GET_VAL( &off
, DWORD
);
122 base
+= (signed long)off
;
126 else /* short address */
130 case 0: /* ds:(bx,si) */
131 base
= BX_reg(context
) + SI_reg(context
);
132 seg
= DS_reg(context
);
134 case 1: /* ds:(bx,di) */
135 base
= BX_reg(context
) + DI_reg(context
);
136 seg
= DS_reg(context
);
138 case 2: /* ss:(bp,si) */
139 base
= BP_reg(context
) + SI_reg(context
);
140 seg
= SS_reg(context
);
142 case 3: /* ss:(bp,di) */
143 base
= BP_reg(context
) + DI_reg(context
);
144 seg
= SS_reg(context
);
146 case 4: /* ds:(si) */
147 base
= SI_reg(context
);
148 seg
= DS_reg(context
);
150 case 5: /* ds:(di) */
151 base
= DI_reg(context
);
152 seg
= DS_reg(context
);
154 case 6: /* ss:(bp) */
155 base
= BP_reg(context
);
156 seg
= SS_reg(context
);
158 case 7: /* ds:(bx) */
159 base
= BX_reg(context
);
160 seg
= DS_reg(context
);
167 if (rm
== 6) /* special case: ds:(disp16) */
169 GET_VAL( &base
, WORD
);
170 seg
= DS_reg(context
);
174 case 1: /* 8-bit disp */
175 GET_VAL( &off
, BYTE
);
176 base
+= (signed char)off
;
179 case 2: /* 16-bit disp */
180 GET_VAL( &off
, WORD
);
181 base
+= (signed short)off
;
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 /***********************************************************************
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)
215 if (long_op
) EAX_reg(context
) = *(DWORD
*)addr
;
216 else AX_reg(context
) = *(WORD
*)addr
;
219 if (long_op
) ECX_reg(context
) = *(DWORD
*)addr
;
220 else CX_reg(context
) = *(WORD
*)addr
;
223 if (long_op
) EDX_reg(context
) = *(DWORD
*)addr
;
224 else DX_reg(context
) = *(WORD
*)addr
;
227 if (long_op
) EBX_reg(context
) = *(DWORD
*)addr
;
228 else BX_reg(context
) = *(WORD
*)addr
;
231 if (long_op
) ESP_reg(context
) = *(DWORD
*)addr
;
232 else SP_reg(context
) = *(WORD
*)addr
;
235 if (long_op
) EBP_reg(context
) = *(DWORD
*)addr
;
236 else BP_reg(context
) = *(WORD
*)addr
;
239 if (long_op
) ESI_reg(context
) = *(DWORD
*)addr
;
240 else SI_reg(context
) = *(WORD
*)addr
;
243 if (long_op
) EDI_reg(context
) = *(DWORD
*)addr
;
244 else DI_reg(context
) = *(WORD
*)addr
;
248 /* Store the correct segment in the segment register */
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 */
258 case 0xb4: FS_reg(context
) = seg
; break; /* lfs */
261 case 0xb5: GS_reg(context
) = seg
; break; /* lgs */
267 /* Add the opcode size to the total length */
269 *len
+= 1 + (*instr
== 0x0f);
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
;
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 */
298 segprefix
= CS_reg(context
);
301 segprefix
= SS_reg(context
);
304 segprefix
= DS_reg(context
);
307 segprefix
= ES_reg(context
);
311 segprefix
= FS_reg(context
);
316 segprefix
= GS_reg(context
);
320 long_op
= !long_op
; /* opcode size prefix */
323 long_addr
= !long_addr
; /* addr size prefix */
325 case 0xf0: /* lock */
327 case 0xf2: /* repne */
330 case 0xf3: /* repe */
334 prefix
= 0; /* no more prefixes */
344 /* Now look at the actual instruction */
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)
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;
366 break; /* Unable to emulate it */
368 case 0x0f: /* extended instruction */
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;
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;
401 case 0xb2: /* lss addr,reg */
403 case 0xb4: /* lfs addr,reg */
406 case 0xb5: /* lgs addr,reg */
408 if (INSTR_EmulateLDS( context
, instr
, long_op
,
409 long_addr
, segprefix
, &len
))
411 EIP_reg(context
) += prefixlen
+ len
;
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
? DS_reg(context
) : ES_reg(context
); /* FIXME: is this right? */
432 /* FIXME: Check segment readable. */
435 /* FIXME: Check segment writeable. */
440 ECX_reg(context
) = 0;
449 data
= PTR_SEG_OFF_TO_LIN (seg
,
450 long_addr
? ESI_reg(context
) : SI_reg(context
));
451 if (long_addr
) ESI_reg(context
) += step
;
452 else SI_reg(context
) += step
;
456 data
= PTR_SEG_OFF_TO_LIN (seg
,
457 long_addr
? EDI_reg(context
) : DI_reg(context
));
458 if (long_addr
) EDI_reg(context
) += step
;
459 else DI_reg(context
) += step
;
465 *((BYTE
*)data
) = inport( DX_reg(context
), 1);
469 *((DWORD
*)data
) = inport( DX_reg(context
), 4);
471 *((WORD
*)data
) = inport( DX_reg(context
), 2);
474 outport( DX_reg(context
), 1, *((BYTE
*)data
));
478 outport( DX_reg(context
), 4, *((DWORD
*)data
));
480 outport( DX_reg(context
), 2, *((WORD
*)data
));
484 EIP_reg(context
) += prefixlen
+ 1;
488 case 0x8e: /* mov XX,segment_reg */
490 WORD seg
= *(WORD
*)INSTR_GetOperandAddr( context
, instr
+ 1,
491 long_addr
, segprefix
, &len
);
492 if (!(seg
= INSTR_ReplaceSelector( context
, seg
)))
493 break; /* Unable to emulate it */
495 switch((instr
[1] >> 3) & 7)
498 ES_reg(context
) = seg
;
499 EIP_reg(context
) += prefixlen
+ len
+ 1;
504 SS_reg(context
) = seg
;
505 EIP_reg(context
) += prefixlen
+ len
+ 1;
508 DS_reg(context
) = seg
;
509 EIP_reg(context
) += prefixlen
+ len
+ 1;
513 FS_reg(context
) = seg
;
514 EIP_reg(context
) += prefixlen
+ len
+ 1;
519 GS_reg(context
) = seg
;
520 EIP_reg(context
) += prefixlen
+ len
+ 1;
528 break; /* Unable to emulate it */
530 case 0xc4: /* les addr,reg */
531 case 0xc5: /* lds addr,reg */
532 if (INSTR_EmulateLDS( context
, instr
, long_op
,
533 long_addr
, segprefix
, &len
))
535 EIP_reg(context
) += prefixlen
+ len
;
538 break; /* Unable to emulate it */
540 case 0xcd: /* int <XX> */
543 fprintf(stderr
, "int xx from 32-bit code is not supported.\n");
544 break; /* Unable to emulate it */
548 SEGPTR addr
= INT_GetHandler( instr
[1] );
549 WORD
*stack
= (WORD
*)STACK_PTR( context
);
550 /* Push the flags and return address on the stack */
551 *(--stack
) = FL_reg(context
);
552 *(--stack
) = CS_reg(context
);
553 *(--stack
) = IP_reg(context
) + prefixlen
+ 2;
554 STACK_reg(context
) -= 3 * sizeof(WORD
);
555 /* Jump to the interrupt handler */
556 CS_reg(context
) = HIWORD(addr
);
557 EIP_reg(context
) = LOWORD(addr
);
561 case 0xcf: /* iret */
564 DWORD
*stack
= (DWORD
*)STACK_PTR( context
);
565 EIP_reg(context
) = *stack
++;
566 CS_reg(context
) = *stack
++;
567 EFL_reg(context
) = *stack
;
568 STACK_reg(context
) += 3*sizeof(DWORD
); /* Pop the return address and flags */
572 WORD
*stack
= (WORD
*)STACK_PTR( context
);
573 EIP_reg(context
) = *stack
++;
574 CS_reg(context
) = *stack
++;
575 FL_reg(context
) = *stack
;
576 STACK_reg(context
) += 3*sizeof(WORD
); /* Pop the return address and flags */
580 case 0xe4: /* inb al,XX */
581 AL_reg(context
) = inport( instr
[1], 1 );
582 EIP_reg(context
) += prefixlen
+ 2;
585 case 0xe5: /* in (e)ax,XX */
586 if (long_op
) EAX_reg(context
) = inport( instr
[1], 4 );
587 else AX_reg(context
) = inport( instr
[1], 2 );
588 EIP_reg(context
) += prefixlen
+ 2;
591 case 0xe6: /* outb XX,al */
592 outport( instr
[1], 1, AL_reg(context
) );
593 EIP_reg(context
) += prefixlen
+ 2;
596 case 0xe7: /* out XX,(e)ax */
597 if (long_op
) outport( instr
[1], 4, EAX_reg(context
) );
598 else outport( instr
[1], 2, AX_reg(context
) );
599 EIP_reg(context
) += prefixlen
+ 2;
602 case 0xec: /* inb al,dx */
603 AL_reg(context
) = inport( DX_reg(context
), 1 );
604 EIP_reg(context
) += prefixlen
+ 1;
607 case 0xed: /* in (e)ax,dx */
608 if (long_op
) EAX_reg(context
) = inport( DX_reg(context
), 4 );
609 else AX_reg(context
) = inport( DX_reg(context
), 2 );
610 EIP_reg(context
) += prefixlen
+ 1;
613 case 0xee: /* outb dx,al */
614 outport( DX_reg(context
), 1, AL_reg(context
) );
615 EIP_reg(context
) += prefixlen
+ 1;
618 case 0xef: /* out dx,(e)ax */
619 if (long_op
) outport( DX_reg(context
), 4, EAX_reg(context
) );
620 else outport( DX_reg(context
), 2, AX_reg(context
) );
621 EIP_reg(context
) += prefixlen
+ 1;
624 case 0xfa: /* cli, ignored */
625 EIP_reg(context
) += prefixlen
+ 1;
628 case 0xfb: /* sti, ignored */
629 EIP_reg(context
) += prefixlen
+ 1;
632 fprintf(stderr
, "Unexpected Windows program segfault"
633 " - opcode = %x\n", *instr
);
634 return FALSE
; /* Unable to emulate it */