2 * Emulation of priviledged instructions
4 * Copyright 1995 Alexandre Julliard
11 #include "sig_context.h"
15 #define STACK_sig(context) \
16 (IS_SELECTOR_32BIT(SS_sig(context)) ? ESP_sig(context) : SP_sig(context))
18 #define MAKE_PTR(seg,off) \
19 (IS_SELECTOR_SYSTEM(seg) ? (void *)(off) : PTR_SEG_OFF_TO_LIN(seg,off))
21 #define STACK_PTR(context) \
22 (IS_SELECTOR_SYSTEM(SS_sig(context)) ? (void *)ESP_sig(context) : \
23 (PTR_SEG_OFF_TO_LIN(SS_sig(context),STACK_sig(context))))
26 /***********************************************************************
27 * INSTR_ReplaceSelector
29 * Try to replace an invalid selector by a valid one.
30 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
31 * is the so called 'bimodal' selector 0x40, which points to the BIOS
32 * data segment. Used by (at least) Borland products (and programs compiled
33 * using Borland products).
35 * See Undocumented Windows, Chapter 5, __0040.
37 static WORD
INSTR_ReplaceSelector( SIGCONTEXT
*context
, WORD sel
)
41 static WORD sys_timer
= 0;
43 sys_timer
= CreateSystemTimer( 55, (FARPROC16
)DOSMEM_Tick
);
44 return DOSMEM_BiosSeg
;
46 return 0; /* Can't replace selector, crashdump */
50 /***********************************************************************
51 * INSTR_GetOperandAddr
53 * Return the address of an instruction operand (from the mod/rm byte).
55 static BYTE
*INSTR_GetOperandAddr( SIGCONTEXT
*context
, BYTE
*instr
,
56 int long_addr
, int segprefix
, int *len
)
58 int mod
, rm
, base
, index
= 0, ss
= 0, seg
= 0, off
;
60 #define GET_VAL(val,type) \
61 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
64 GET_VAL( &mod
, BYTE
);
72 case 0: return (BYTE
*)&EAX_sig(context
);
73 case 1: return (BYTE
*)&ECX_sig(context
);
74 case 2: return (BYTE
*)&EDX_sig(context
);
75 case 3: return (BYTE
*)&EBX_sig(context
);
76 case 4: return (BYTE
*)&ESP_sig(context
);
77 case 5: return (BYTE
*)&EBP_sig(context
);
78 case 6: return (BYTE
*)&ESI_sig(context
);
79 case 7: return (BYTE
*)&EDI_sig(context
);
88 GET_VAL( &sib
, BYTE
);
93 case 0: index
= EAX_sig(context
); break;
94 case 1: index
= ECX_sig(context
); break;
95 case 2: index
= EDX_sig(context
); break;
96 case 3: index
= EBX_sig(context
); break;
97 case 4: index
= 0; break;
98 case 5: index
= EBP_sig(context
); break;
99 case 6: index
= ESI_sig(context
); break;
100 case 7: index
= EDI_sig(context
); break;
106 case 0: base
= EAX_sig(context
); seg
= DS_sig(context
); break;
107 case 1: base
= ECX_sig(context
); seg
= DS_sig(context
); break;
108 case 2: base
= EDX_sig(context
); seg
= DS_sig(context
); break;
109 case 3: base
= EBX_sig(context
); seg
= DS_sig(context
); break;
110 case 4: base
= ESP_sig(context
); seg
= SS_sig(context
); break;
111 case 5: base
= EBP_sig(context
); seg
= SS_sig(context
); break;
112 case 6: base
= ESI_sig(context
); seg
= DS_sig(context
); break;
113 case 7: base
= EDI_sig(context
); seg
= DS_sig(context
); break;
118 if (rm
== 5) /* special case: ds:(disp32) */
120 GET_VAL( &base
, DWORD
);
121 seg
= DS_sig(context
);
125 case 1: /* 8-bit disp */
126 GET_VAL( &off
, BYTE
);
127 base
+= (signed char)off
;
130 case 2: /* 32-bit disp */
131 GET_VAL( &off
, DWORD
);
132 base
+= (signed long)off
;
136 else /* short address */
140 case 0: /* ds:(bx,si) */
141 base
= BX_sig(context
) + SI_sig(context
);
142 seg
= DS_sig(context
);
144 case 1: /* ds:(bx,di) */
145 base
= BX_sig(context
) + DI_sig(context
);
146 seg
= DS_sig(context
);
148 case 2: /* ss:(bp,si) */
149 base
= BP_sig(context
) + SI_sig(context
);
150 seg
= SS_sig(context
);
152 case 3: /* ss:(bp,di) */
153 base
= BP_sig(context
) + DI_sig(context
);
154 seg
= SS_sig(context
);
156 case 4: /* ds:(si) */
157 base
= SI_sig(context
);
158 seg
= DS_sig(context
);
160 case 5: /* ds:(di) */
161 base
= DI_sig(context
);
162 seg
= DS_sig(context
);
164 case 6: /* ss:(bp) */
165 base
= BP_sig(context
);
166 seg
= SS_sig(context
);
168 case 7: /* ds:(bx) */
169 base
= BX_sig(context
);
170 seg
= DS_sig(context
);
177 if (rm
== 6) /* special case: ds:(disp16) */
179 GET_VAL( &base
, WORD
);
180 seg
= DS_sig(context
);
184 case 1: /* 8-bit disp */
185 GET_VAL( &off
, BYTE
);
186 base
+= (signed char)off
;
189 case 2: /* 16-bit disp */
190 GET_VAL( &off
, WORD
);
191 base
+= (signed short)off
;
196 if (segprefix
!= -1) seg
= segprefix
;
198 /* Make sure the segment and offset are valid */
199 if (IS_SELECTOR_SYSTEM(seg
)) return (BYTE
*)(base
+ (index
<< ss
));
200 if (((seg
& 7) != 7) || IS_SELECTOR_FREE(seg
)) return NULL
;
201 if (GET_SEL_LIMIT(seg
) < (base
+ (index
<< ss
))) return NULL
;
202 return (BYTE
*)PTR_SEG_OFF_TO_LIN( seg
, (base
+ (index
<< ss
)) );
207 /***********************************************************************
210 * Emulate the LDS (and LES,LFS,etc.) instruction.
212 static BOOL32
INSTR_EmulateLDS( SIGCONTEXT
*context
, BYTE
*instr
, int long_op
,
213 int long_addr
, int segprefix
, int *len
)
216 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
217 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
218 long_addr
, segprefix
, len
);
220 return FALSE
; /* Unable to emulate it */
221 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
223 if (!(seg
= INSTR_ReplaceSelector( context
, seg
)))
224 return FALSE
; /* Unable to emulate it */
226 /* Now store the offset in the correct register */
228 switch((*regmodrm
>> 3) & 7)
231 if (long_op
) EAX_sig(context
) = *(DWORD
*)addr
;
232 else AX_sig(context
) = *(WORD
*)addr
;
235 if (long_op
) ECX_sig(context
) = *(DWORD
*)addr
;
236 else CX_sig(context
) = *(WORD
*)addr
;
239 if (long_op
) EDX_sig(context
) = *(DWORD
*)addr
;
240 else DX_sig(context
) = *(WORD
*)addr
;
243 if (long_op
) EBX_sig(context
) = *(DWORD
*)addr
;
244 else BX_sig(context
) = *(WORD
*)addr
;
247 if (long_op
) ESP_sig(context
) = *(DWORD
*)addr
;
248 else SP_sig(context
) = *(WORD
*)addr
;
251 if (long_op
) EBP_sig(context
) = *(DWORD
*)addr
;
252 else BP_sig(context
) = *(WORD
*)addr
;
255 if (long_op
) ESI_sig(context
) = *(DWORD
*)addr
;
256 else SI_sig(context
) = *(WORD
*)addr
;
259 if (long_op
) EDI_sig(context
) = *(DWORD
*)addr
;
260 else DI_sig(context
) = *(WORD
*)addr
;
264 /* Store the correct segment in the segment register */
268 case 0xc4: ES_sig(context
) = seg
; break; /* les */
269 case 0xc5: DS_sig(context
) = seg
; break; /* lds */
270 case 0x0f: switch(instr
[1])
272 case 0xb2: SS_sig(context
) = seg
; break; /* lss */
274 case 0xb4: FS_sig(context
) = seg
; break; /* lfs */
277 case 0xb5: GS_sig(context
) = seg
; break; /* lgs */
283 /* Add the opcode size to the total length */
285 *len
+= 1 + (*instr
== 0x0f);
290 /***********************************************************************
291 * INSTR_EmulateInstruction
293 * Emulate a priviledged instruction. Returns TRUE if emulation successful.
295 BOOL32
INSTR_EmulateInstruction( SIGCONTEXT
*context
)
297 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
300 /* Check for page-fault */
302 #if defined(TRAP_sig) && defined(CR2_sig)
303 if (TRAP_sig(context
) == 0x0e
304 && VIRTUAL_HandleFault( (LPVOID
)CR2_sig(context
) )) return TRUE
;
307 long_op
= long_addr
= IS_SELECTOR_32BIT(CS_sig(context
));
308 instr
= (BYTE
*)MAKE_PTR(CS_sig(context
),EIP_sig(context
));
309 if (!instr
) return FALSE
;
311 /* First handle any possible prefix */
313 segprefix
= -1; /* no prefix */
322 segprefix
= CS_sig(context
);
325 segprefix
= SS_sig(context
);
328 segprefix
= DS_sig(context
);
331 segprefix
= ES_sig(context
);
335 segprefix
= FS_sig(context
);
340 segprefix
= GS_sig(context
);
344 long_op
= !long_op
; /* opcode size prefix */
347 long_addr
= !long_addr
; /* addr size prefix */
349 case 0xf0: /* lock */
351 case 0xf2: /* repne */
354 case 0xf3: /* repe */
358 prefix
= 0; /* no more prefixes */
368 /* Now look at the actual instruction */
372 case 0x07: /* pop es */
373 case 0x17: /* pop ss */
374 case 0x1f: /* pop ds */
376 WORD seg
= *(WORD
*)STACK_PTR( context
);
377 if ((seg
= INSTR_ReplaceSelector( context
, seg
)) != 0)
381 case 0x07: ES_sig(context
) = seg
; break;
382 case 0x17: SS_sig(context
) = seg
; break;
383 case 0x1f: DS_sig(context
) = seg
; break;
385 STACK_sig(context
) += long_op
? 4 : 2;
386 EIP_sig(context
) += prefixlen
+ 1;
390 break; /* Unable to emulate it */
392 case 0x0f: /* extended instruction */
396 case 0xa1: /* pop fs */
398 WORD seg
= *(WORD
*)STACK_PTR( context
);
399 if ((seg
= INSTR_ReplaceSelector( context
, seg
)) != 0)
401 FS_sig(context
) = seg
;
402 STACK_sig(context
) += long_op
? 4 : 2;
403 EIP_sig(context
) += prefixlen
+ 2;
411 case 0xa9: /* pop gs */
413 WORD seg
= *(WORD
*)STACK_PTR( context
);
414 if ((seg
= INSTR_ReplaceSelector( context
, seg
)) != 0)
416 GS_sig(context
) = seg
;
417 STACK_sig(context
) += long_op
? 4 : 2;
418 EIP_sig(context
) += prefixlen
+ 2;
425 case 0xb2: /* lss addr,reg */
427 case 0xb4: /* lfs addr,reg */
430 case 0xb5: /* lgs addr,reg */
432 if (INSTR_EmulateLDS( context
, instr
, long_op
,
433 long_addr
, segprefix
, &len
))
435 EIP_sig(context
) += prefixlen
+ len
;
440 break; /* Unable to emulate it */
442 case 0x6c: /* insb */
443 case 0x6d: /* insw/d */
444 case 0x6e: /* outsb */
445 case 0x6f: /* outsw/d */
447 int typ
= *instr
; /* Just in case it's overwritten. */
448 int outp
= (typ
>= 0x6e);
449 unsigned long count
= repX
?
450 (long_addr
? ECX_sig(context
) : CX_sig(context
)) : 1;
451 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
452 int step
= (EFL_sig(context
) & 0x400) ? -opsize
: +opsize
;
453 int seg
= outp
? DS_sig(context
) : ES_sig(context
); /* FIXME: is this right? */
456 /* FIXME: Check segment readable. */
459 /* FIXME: Check segment writeable. */
464 ECX_sig(context
) = 0;
474 long_addr
? ESI_sig(context
) : SI_sig(context
));
475 if (long_addr
) ESI_sig(context
) += step
;
476 else SI_sig(context
) += step
;
481 long_addr
? EDI_sig(context
) : DI_sig(context
));
482 if (long_addr
) EDI_sig(context
) += step
;
483 else DI_sig(context
) += step
;
489 *((BYTE
*)data
) = IO_inport( DX_sig(context
), 1);
493 *((DWORD
*)data
) = IO_inport( DX_sig(context
), 4);
495 *((WORD
*)data
) = IO_inport( DX_sig(context
), 2);
498 IO_outport( DX_sig(context
), 1, *((BYTE
*)data
));
502 IO_outport( DX_sig(context
), 4, *((DWORD
*)data
));
504 IO_outport( DX_sig(context
), 2, *((WORD
*)data
));
508 EIP_sig(context
) += prefixlen
+ 1;
512 case 0x8e: /* mov XX,segment_reg */
515 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
516 long_addr
, segprefix
, &len
);
518 break; /* Unable to emulate it */
520 if (!(seg
= INSTR_ReplaceSelector( context
, seg
)))
521 break; /* Unable to emulate it */
523 switch((instr
[1] >> 3) & 7)
526 ES_sig(context
) = seg
;
527 EIP_sig(context
) += prefixlen
+ len
+ 1;
532 SS_sig(context
) = seg
;
533 EIP_sig(context
) += prefixlen
+ len
+ 1;
536 DS_sig(context
) = seg
;
537 EIP_sig(context
) += prefixlen
+ len
+ 1;
541 FS_sig(context
) = seg
;
542 EIP_sig(context
) += prefixlen
+ len
+ 1;
547 GS_sig(context
) = seg
;
548 EIP_sig(context
) += prefixlen
+ len
+ 1;
556 break; /* Unable to emulate it */
558 case 0xc4: /* les addr,reg */
559 case 0xc5: /* lds addr,reg */
560 if (INSTR_EmulateLDS( context
, instr
, long_op
,
561 long_addr
, segprefix
, &len
))
563 EIP_sig(context
) += prefixlen
+ len
;
566 break; /* Unable to emulate it */
568 case 0xcd: /* int <XX> */
571 ERR(int, "int xx from 32-bit code is not supported.\n");
572 break; /* Unable to emulate it */
576 FARPROC16 addr
= INT_GetHandler( instr
[1] );
577 WORD
*stack
= (WORD
*)STACK_PTR( context
);
578 /* Push the flags and return address on the stack */
579 *(--stack
) = FL_sig(context
);
580 *(--stack
) = CS_sig(context
);
581 *(--stack
) = IP_sig(context
) + prefixlen
+ 2;
582 STACK_sig(context
) -= 3 * sizeof(WORD
);
583 /* Jump to the interrupt handler */
584 CS_sig(context
) = HIWORD(addr
);
585 EIP_sig(context
) = LOWORD(addr
);
589 case 0xcf: /* iret */
592 DWORD
*stack
= (DWORD
*)STACK_PTR( context
);
593 EIP_sig(context
) = *stack
++;
594 CS_sig(context
) = *stack
++;
595 EFL_sig(context
) = *stack
;
596 STACK_sig(context
) += 3*sizeof(DWORD
); /* Pop the return address and flags */
600 WORD
*stack
= (WORD
*)STACK_PTR( context
);
601 EIP_sig(context
) = *stack
++;
602 CS_sig(context
) = *stack
++;
603 FL_sig(context
) = *stack
;
604 STACK_sig(context
) += 3*sizeof(WORD
); /* Pop the return address and flags */
608 case 0xe4: /* inb al,XX */
609 AL_sig(context
) = IO_inport( instr
[1], 1 );
610 EIP_sig(context
) += prefixlen
+ 2;
613 case 0xe5: /* in (e)ax,XX */
614 if (long_op
) EAX_sig(context
) = IO_inport( instr
[1], 4 );
615 else AX_sig(context
) = IO_inport( instr
[1], 2 );
616 EIP_sig(context
) += prefixlen
+ 2;
619 case 0xe6: /* outb XX,al */
620 IO_outport( instr
[1], 1, AL_sig(context
) );
621 EIP_sig(context
) += prefixlen
+ 2;
624 case 0xe7: /* out XX,(e)ax */
625 if (long_op
) IO_outport( instr
[1], 4, EAX_sig(context
) );
626 else IO_outport( instr
[1], 2, AX_sig(context
) );
627 EIP_sig(context
) += prefixlen
+ 2;
630 case 0xec: /* inb al,dx */
631 AL_sig(context
) = IO_inport( DX_sig(context
), 1 );
632 EIP_sig(context
) += prefixlen
+ 1;
635 case 0xed: /* in (e)ax,dx */
636 if (long_op
) EAX_sig(context
) = IO_inport( DX_sig(context
), 4 );
637 else AX_sig(context
) = IO_inport( DX_sig(context
), 2 );
638 EIP_sig(context
) += prefixlen
+ 1;
641 case 0xee: /* outb dx,al */
642 IO_outport( DX_sig(context
), 1, AL_sig(context
) );
643 EIP_sig(context
) += prefixlen
+ 1;
646 case 0xef: /* out dx,(e)ax */
647 if (long_op
) IO_outport( DX_sig(context
), 4, EAX_sig(context
) );
648 else IO_outport( DX_sig(context
), 2, AX_sig(context
) );
649 EIP_sig(context
) += prefixlen
+ 1;
652 case 0xfa: /* cli, ignored */
653 EIP_sig(context
) += prefixlen
+ 1;
656 case 0xfb: /* sti, ignored */
657 EIP_sig(context
) += prefixlen
+ 1;
660 MSG("Unexpected Windows program segfault"
661 " - opcode = %x\n", *instr
);
662 return FALSE
; /* Unable to emulate it */