2 * Emulation of priviledged instructions
4 * Copyright 1995 Alexandre Julliard
9 #include "wine/winuser16.h"
15 #include "selectors.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(int);
19 DECLARE_DEBUG_CHANNEL(io
);
23 #define IS_SEL_32(context,seg) \
24 (ISV86(context) ? FALSE : IS_SELECTOR_32BIT(seg))
26 #define STACK_reg(context) \
27 (IS_SEL_32(context,SS_reg(context)) ? ESP_reg(context) : (DWORD)LOWORD(ESP_reg(context)))
29 #define ADD_STACK_reg(context,offset) \
30 do { if (IS_SEL_32(context,SS_reg(context))) ESP_reg(context) += (offset); \
31 else ADD_LOWORD(ESP_reg(context),(offset)); } while(0)
33 #define MAKE_PTR(seg,off) \
34 (IS_SELECTOR_SYSTEM(seg) ? (void *)(off) : PTR_SEG_OFF_TO_LIN(seg,off))
36 #define MK_PTR(context,seg,off) \
37 (ISV86(context) ? DOSMEM_MapRealToLinear(MAKELONG(off,seg)) \
40 #define STACK_PTR(context) \
42 DOSMEM_MapRealToLinear(MAKELONG(LOWORD(ESP_reg(context)),SS_reg(context))) : \
43 (IS_SELECTOR_SYSTEM(SS_reg(context)) ? (void *)ESP_reg(context) : \
44 (PTR_SEG_OFF_TO_LIN(SS_reg(context),STACK_reg(context)))))
46 /***********************************************************************
47 * INSTR_ReplaceSelector
49 * Try to replace an invalid selector by a valid one.
50 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
51 * is the so called 'bimodal' selector 0x40, which points to the BIOS
52 * data segment. Used by (at least) Borland products (and programs compiled
53 * using Borland products).
55 * See Undocumented Windows, Chapter 5, __0040.
57 static BOOL
INSTR_ReplaceSelector( CONTEXT86
*context
, WORD
*sel
)
59 extern char Call16_Start
, Call16_End
;
61 if (IS_SELECTOR_SYSTEM(CS_reg(context
)))
62 if ( (char *)EIP_reg(context
) >= &Call16_Start
63 && (char *)EIP_reg(context
) < &Call16_End
)
65 /* Saved selector may have become invalid when the relay code */
66 /* tries to restore it. We simply clear it. */
73 static WORD sys_timer
= 0;
75 sys_timer
= CreateSystemTimer( 55, DOSMEM_Tick
);
76 *sel
= DOSMEM_BiosDataSeg
;
79 if (!IS_SELECTOR_SYSTEM(*sel
) && !IS_SELECTOR_FREE(*sel
))
80 ERR("Got protection fault on valid selector, maybe your kernel is too old?\n" );
81 return FALSE
; /* Can't replace selector, crashdump */
85 /***********************************************************************
86 * INSTR_GetOperandAddr
88 * Return the address of an instruction operand (from the mod/rm byte).
90 static BYTE
*INSTR_GetOperandAddr( CONTEXT86
*context
, BYTE
*instr
,
91 int long_addr
, int segprefix
, int *len
)
93 int mod
, rm
, base
, index
= 0, ss
= 0, seg
= 0, off
;
95 #define GET_VAL(val,type) \
96 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
99 GET_VAL( &mod
, BYTE
);
107 case 0: return (BYTE
*)&EAX_reg(context
);
108 case 1: return (BYTE
*)&ECX_reg(context
);
109 case 2: return (BYTE
*)&EDX_reg(context
);
110 case 3: return (BYTE
*)&EBX_reg(context
);
111 case 4: return (BYTE
*)&ESP_reg(context
);
112 case 5: return (BYTE
*)&EBP_reg(context
);
113 case 6: return (BYTE
*)&ESI_reg(context
);
114 case 7: return (BYTE
*)&EDI_reg(context
);
123 GET_VAL( &sib
, BYTE
);
128 case 0: index
= EAX_reg(context
); break;
129 case 1: index
= ECX_reg(context
); break;
130 case 2: index
= EDX_reg(context
); break;
131 case 3: index
= EBX_reg(context
); break;
132 case 4: index
= 0; break;
133 case 5: index
= EBP_reg(context
); break;
134 case 6: index
= ESI_reg(context
); break;
135 case 7: index
= EDI_reg(context
); break;
141 case 0: base
= EAX_reg(context
); seg
= DS_reg(context
); break;
142 case 1: base
= ECX_reg(context
); seg
= DS_reg(context
); break;
143 case 2: base
= EDX_reg(context
); seg
= DS_reg(context
); break;
144 case 3: base
= EBX_reg(context
); seg
= DS_reg(context
); break;
145 case 4: base
= ESP_reg(context
); seg
= SS_reg(context
); break;
146 case 5: base
= EBP_reg(context
); seg
= SS_reg(context
); break;
147 case 6: base
= ESI_reg(context
); seg
= DS_reg(context
); break;
148 case 7: base
= EDI_reg(context
); seg
= DS_reg(context
); break;
153 if (rm
== 5) /* special case: ds:(disp32) */
155 GET_VAL( &base
, DWORD
);
156 seg
= DS_reg(context
);
160 case 1: /* 8-bit disp */
161 GET_VAL( &off
, BYTE
);
162 base
+= (signed char)off
;
165 case 2: /* 32-bit disp */
166 GET_VAL( &off
, DWORD
);
167 base
+= (signed long)off
;
171 else /* short address */
175 case 0: /* ds:(bx,si) */
176 base
= LOWORD(EBX_reg(context
)) + LOWORD(ESI_reg(context
));
177 seg
= DS_reg(context
);
179 case 1: /* ds:(bx,di) */
180 base
= LOWORD(EBX_reg(context
)) + LOWORD(EDI_reg(context
));
181 seg
= DS_reg(context
);
183 case 2: /* ss:(bp,si) */
184 base
= LOWORD(EBP_reg(context
)) + LOWORD(ESI_reg(context
));
185 seg
= SS_reg(context
);
187 case 3: /* ss:(bp,di) */
188 base
= LOWORD(EBP_reg(context
)) + LOWORD(EDI_reg(context
));
189 seg
= SS_reg(context
);
191 case 4: /* ds:(si) */
192 base
= LOWORD(ESI_reg(context
));
193 seg
= DS_reg(context
);
195 case 5: /* ds:(di) */
196 base
= LOWORD(EDI_reg(context
));
197 seg
= DS_reg(context
);
199 case 6: /* ss:(bp) */
200 base
= LOWORD(EBP_reg(context
));
201 seg
= SS_reg(context
);
203 case 7: /* ds:(bx) */
204 base
= LOWORD(EBX_reg(context
));
205 seg
= DS_reg(context
);
212 if (rm
== 6) /* special case: ds:(disp16) */
214 GET_VAL( &base
, WORD
);
215 seg
= DS_reg(context
);
219 case 1: /* 8-bit disp */
220 GET_VAL( &off
, BYTE
);
221 base
+= (signed char)off
;
224 case 2: /* 16-bit disp */
225 GET_VAL( &off
, WORD
);
226 base
+= (signed short)off
;
231 if (segprefix
!= -1) seg
= segprefix
;
233 /* Make sure the segment and offset are valid */
234 if (IS_SELECTOR_SYSTEM(seg
)) return (BYTE
*)(base
+ (index
<< ss
));
235 if (((seg
& 7) != 7) || IS_SELECTOR_FREE(seg
)) return NULL
;
236 if (GET_SEL_LIMIT(seg
) < (base
+ (index
<< ss
))) return NULL
;
237 return (BYTE
*)PTR_SEG_OFF_TO_LIN( seg
, (base
+ (index
<< ss
)) );
242 /***********************************************************************
245 * Emulate the LDS (and LES,LFS,etc.) instruction.
247 static BOOL
INSTR_EmulateLDS( CONTEXT86
*context
, BYTE
*instr
, int long_op
,
248 int long_addr
, int segprefix
, int *len
)
251 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
252 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
253 long_addr
, segprefix
, len
);
255 return FALSE
; /* Unable to emulate it */
256 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
258 if (!INSTR_ReplaceSelector( context
, &seg
))
259 return FALSE
; /* Unable to emulate it */
261 /* Now store the offset in the correct register */
263 switch((*regmodrm
>> 3) & 7)
266 if (long_op
) EAX_reg(context
) = *(DWORD
*)addr
;
267 else SET_LOWORD(EAX_reg(context
),*(WORD
*)addr
);
270 if (long_op
) ECX_reg(context
) = *(DWORD
*)addr
;
271 else SET_LOWORD(ECX_reg(context
),*(WORD
*)addr
);
274 if (long_op
) EDX_reg(context
) = *(DWORD
*)addr
;
275 else SET_LOWORD(EDX_reg(context
),*(WORD
*)addr
);
278 if (long_op
) EBX_reg(context
) = *(DWORD
*)addr
;
279 else SET_LOWORD(EBX_reg(context
),*(WORD
*)addr
);
282 if (long_op
) ESP_reg(context
) = *(DWORD
*)addr
;
283 else SET_LOWORD(ESP_reg(context
),*(WORD
*)addr
);
286 if (long_op
) EBP_reg(context
) = *(DWORD
*)addr
;
287 else SET_LOWORD(EBP_reg(context
),*(WORD
*)addr
);
290 if (long_op
) ESI_reg(context
) = *(DWORD
*)addr
;
291 else SET_LOWORD(ESI_reg(context
),*(WORD
*)addr
);
294 if (long_op
) EDI_reg(context
) = *(DWORD
*)addr
;
295 else SET_LOWORD(EDI_reg(context
),*(WORD
*)addr
);
299 /* Store the correct segment in the segment register */
303 case 0xc4: ES_reg(context
) = seg
; break; /* les */
304 case 0xc5: DS_reg(context
) = seg
; break; /* lds */
305 case 0x0f: switch(instr
[1])
307 case 0xb2: SS_reg(context
) = seg
; break; /* lss */
308 case 0xb4: FS_reg(context
) = seg
; break; /* lfs */
309 case 0xb5: GS_reg(context
) = seg
; break; /* lgs */
314 /* Add the opcode size to the total length */
316 *len
+= 1 + (*instr
== 0x0f);
320 /***********************************************************************
323 * input on a I/O port
325 static DWORD
INSTR_inport( WORD port
, int size
, CONTEXT86
*context
)
327 DWORD res
= IO_inport( port
, size
);
333 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port
, LOBYTE(res
),
334 (WORD
)CS_reg(context
), LOWORD(EIP_reg(context
)));
337 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port
, LOWORD(res
),
338 (WORD
)CS_reg(context
), LOWORD(EIP_reg(context
)));
341 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port
, res
,
342 (WORD
)CS_reg(context
), LOWORD(EIP_reg(context
)));
350 /***********************************************************************
353 * output on a I/O port
355 static void INSTR_outport( WORD port
, int size
, DWORD val
, CONTEXT86
*context
)
357 IO_outport( port
, size
, val
);
363 DPRINTF("0x%x > %02x @ %04x:%04x\n", port
, LOBYTE(val
),
364 (WORD
)CS_reg(context
), LOWORD(EIP_reg(context
)));
367 DPRINTF("0x%x > %04x @ %04x:%04x\n", port
, LOWORD(val
),
368 (WORD
)CS_reg(context
), LOWORD(EIP_reg(context
)));
371 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port
, val
,
372 (WORD
)CS_reg(context
), LOWORD(EIP_reg(context
)));
379 /***********************************************************************
380 * INSTR_EmulateInstruction
382 * Emulate a priviledged instruction. Returns TRUE if emulation successful.
384 BOOL
INSTR_EmulateInstruction( CONTEXT86
*context
)
386 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
390 long_op
= long_addr
= IS_SEL_32(context
,CS_reg(context
));
391 instr
= (BYTE
*)MK_PTR(context
,CS_reg(context
),EIP_reg(context
));
392 if (!instr
) return FALSE
;
394 /* First handle any possible prefix */
396 segprefix
= -1; /* no prefix */
405 segprefix
= CS_reg(context
);
408 segprefix
= SS_reg(context
);
411 segprefix
= DS_reg(context
);
414 segprefix
= ES_reg(context
);
417 segprefix
= FS_reg(context
);
420 segprefix
= GS_reg(context
);
423 long_op
= !long_op
; /* opcode size prefix */
426 long_addr
= !long_addr
; /* addr size prefix */
428 case 0xf0: /* lock */
430 case 0xf2: /* repne */
433 case 0xf3: /* repe */
437 prefix
= 0; /* no more prefixes */
447 /* Now look at the actual instruction */
451 case 0x07: /* pop es */
452 case 0x17: /* pop ss */
453 case 0x1f: /* pop ds */
455 WORD seg
= *(WORD
*)STACK_PTR( context
);
456 if (INSTR_ReplaceSelector( context
, &seg
))
460 case 0x07: ES_reg(context
) = seg
; break;
461 case 0x17: SS_reg(context
) = seg
; break;
462 case 0x1f: DS_reg(context
) = seg
; break;
464 ADD_STACK_reg(context
, long_op
? 4 : 2);
465 EIP_reg(context
) += prefixlen
+ 1;
469 break; /* Unable to emulate it */
471 case 0x0f: /* extended instruction */
474 case 0x22: /* mov eax, crX */
477 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
478 EIP_reg(context
),EAX_reg(context
) );
479 EIP_reg(context
) += prefixlen
+3;
482 break; /*fallthrough to bad instruction handling */
484 break; /*fallthrough to bad instruction handling */
485 case 0x20: /* mov crX, eax */
487 case 0xe0: /* mov cr4, eax */
488 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
489 * bit 0: VME Virtual Mode Exception ?
490 * bit 1: PVI Protected mode Virtual Interrupt
491 * bit 2: TSD Timestamp disable
492 * bit 3: DE Debugging extensions
493 * bit 4: PSE Page size extensions
494 * bit 5: PAE Physical address extension
495 * bit 6: MCE Machine check enable
496 * bit 7: PGE Enable global pages
497 * bit 8: PCE Enable performance counters at IPL3
499 ERR("mov cr4,eax at 0x%08lx\n",EIP_reg(context
));
500 EAX_reg(context
) = 0;
501 EIP_reg(context
) += prefixlen
+3;
503 case 0xc0: /* mov cr0, eax */
504 ERR("mov cr0,eax at 0x%08lx\n",EIP_reg(context
));
505 EAX_reg(context
) = 0x10; /* FIXME: set more bits ? */
506 EIP_reg(context
) += prefixlen
+3;
508 default: /* fallthrough to illegal instruction */
511 /* fallthrough to illegal instruction */
513 case 0xa1: /* pop fs */
515 WORD seg
= *(WORD
*)STACK_PTR( context
);
516 if (INSTR_ReplaceSelector( context
, &seg
))
518 FS_reg(context
) = seg
;
519 ADD_STACK_reg(context
, long_op
? 4 : 2);
520 EIP_reg(context
) += prefixlen
+ 2;
525 case 0xa9: /* pop gs */
527 WORD seg
= *(WORD
*)STACK_PTR( context
);
528 if (INSTR_ReplaceSelector( context
, &seg
))
530 GS_reg(context
) = seg
;
531 ADD_STACK_reg(context
, long_op
? 4 : 2);
532 EIP_reg(context
) += prefixlen
+ 2;
537 case 0xb2: /* lss addr,reg */
538 case 0xb4: /* lfs addr,reg */
539 case 0xb5: /* lgs addr,reg */
540 if (INSTR_EmulateLDS( context
, instr
, long_op
,
541 long_addr
, segprefix
, &len
))
543 EIP_reg(context
) += prefixlen
+ len
;
548 break; /* Unable to emulate it */
550 case 0x6c: /* insb */
551 case 0x6d: /* insw/d */
552 case 0x6e: /* outsb */
553 case 0x6f: /* outsw/d */
555 int typ
= *instr
; /* Just in case it's overwritten. */
556 int outp
= (typ
>= 0x6e);
557 unsigned long count
= repX
?
558 (long_addr
? ECX_reg(context
) : LOWORD(ECX_reg(context
))) : 1;
559 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
560 int step
= (EFL_reg(context
) & 0x400) ? -opsize
: +opsize
;
561 int seg
= outp
? DS_reg(context
) : ES_reg(context
); /* FIXME: is this right? */
564 /* FIXME: Check segment readable. */
567 /* FIXME: Check segment writeable. */
572 if (long_addr
) ECX_reg(context
) = 0;
573 else SET_LOWORD(ECX_reg(context
),0);
579 WORD dx
= LOWORD(EDX_reg(context
));
582 data
= MK_PTR(context
, seg
,
583 long_addr
? ESI_reg(context
) : LOWORD(ESI_reg(context
)));
584 if (long_addr
) ESI_reg(context
) += step
;
585 else ADD_LOWORD(ESI_reg(context
),step
);
589 data
= MK_PTR(context
, seg
,
590 long_addr
? EDI_reg(context
) : LOWORD(EDI_reg(context
)));
591 if (long_addr
) EDI_reg(context
) += step
;
592 else ADD_LOWORD(EDI_reg(context
),step
);
598 *(BYTE
*)data
= INSTR_inport( dx
, 1, context
);
602 *(DWORD
*)data
= INSTR_inport( dx
, 4, context
);
604 *(WORD
*)data
= INSTR_inport( dx
, 2, context
);
607 INSTR_outport( dx
, 1, *(BYTE
*)data
, context
);
611 INSTR_outport( dx
, 4, *(DWORD
*)data
, context
);
613 INSTR_outport( dx
, 2, *(WORD
*)data
, context
);
617 EIP_reg(context
) += prefixlen
+ 1;
621 case 0x8e: /* mov XX,segment_reg */
624 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
625 long_addr
, segprefix
, &len
);
627 break; /* Unable to emulate it */
629 if (!INSTR_ReplaceSelector( context
, &seg
))
630 break; /* Unable to emulate it */
632 switch((instr
[1] >> 3) & 7)
635 ES_reg(context
) = seg
;
636 EIP_reg(context
) += prefixlen
+ len
+ 1;
641 SS_reg(context
) = seg
;
642 EIP_reg(context
) += prefixlen
+ len
+ 1;
645 DS_reg(context
) = seg
;
646 EIP_reg(context
) += prefixlen
+ len
+ 1;
649 FS_reg(context
) = seg
;
650 EIP_reg(context
) += prefixlen
+ len
+ 1;
653 GS_reg(context
) = seg
;
654 EIP_reg(context
) += prefixlen
+ len
+ 1;
661 break; /* Unable to emulate it */
663 case 0xc4: /* les addr,reg */
664 case 0xc5: /* lds addr,reg */
665 if (INSTR_EmulateLDS( context
, instr
, long_op
,
666 long_addr
, segprefix
, &len
))
668 EIP_reg(context
) += prefixlen
+ len
;
671 break; /* Unable to emulate it */
673 case 0xcd: /* int <XX> */
676 ERR("int xx from 32-bit code is not supported.\n");
677 break; /* Unable to emulate it */
681 FARPROC16 addr
= INT_GetPMHandler( instr
[1] );
682 WORD
*stack
= (WORD
*)STACK_PTR( context
);
685 FIXME("no handler for interrupt %02x, ignoring it\n", instr
[1]);
686 EIP_reg(context
) += prefixlen
+ 2;
689 /* Push the flags and return address on the stack */
690 *(--stack
) = LOWORD(EFL_reg(context
));
691 *(--stack
) = CS_reg(context
);
692 *(--stack
) = LOWORD(EIP_reg(context
)) + prefixlen
+ 2;
693 ADD_STACK_reg(context
, -3 * sizeof(WORD
));
694 /* Jump to the interrupt handler */
695 CS_reg(context
) = HIWORD(addr
);
696 EIP_reg(context
) = LOWORD(addr
);
700 case 0xcf: /* iret */
703 DWORD
*stack
= (DWORD
*)STACK_PTR( context
);
704 EIP_reg(context
) = *stack
++;
705 CS_reg(context
) = *stack
++;
706 EFL_reg(context
) = *stack
;
707 ADD_STACK_reg(context
, 3*sizeof(DWORD
)); /* Pop the return address and flags */
711 WORD
*stack
= (WORD
*)STACK_PTR( context
);
712 EIP_reg(context
) = *stack
++;
713 CS_reg(context
) = *stack
++;
714 SET_LOWORD(EFL_reg(context
),*stack
);
715 ADD_STACK_reg(context
, 3*sizeof(WORD
)); /* Pop the return address and flags */
719 case 0xe4: /* inb al,XX */
720 SET_LOBYTE(EAX_reg(context
),INSTR_inport( instr
[1], 1, context
));
721 EIP_reg(context
) += prefixlen
+ 2;
724 case 0xe5: /* in (e)ax,XX */
726 EAX_reg(context
) = INSTR_inport( instr
[1], 4, context
);
728 SET_LOWORD(EAX_reg(context
), INSTR_inport( instr
[1], 2, context
));
729 EIP_reg(context
) += prefixlen
+ 2;
732 case 0xe6: /* outb XX,al */
733 INSTR_outport( instr
[1], 1, LOBYTE(EAX_reg(context
)), context
);
734 EIP_reg(context
) += prefixlen
+ 2;
737 case 0xe7: /* out XX,(e)ax */
739 INSTR_outport( instr
[1], 4, EAX_reg(context
), context
);
741 INSTR_outport( instr
[1], 2, LOWORD(EAX_reg(context
)), context
);
742 EIP_reg(context
) += prefixlen
+ 2;
745 case 0xec: /* inb al,dx */
746 SET_LOBYTE(EAX_reg(context
), INSTR_inport( LOWORD(EDX_reg(context
)), 1, context
) );
747 EIP_reg(context
) += prefixlen
+ 1;
750 case 0xed: /* in (e)ax,dx */
752 EAX_reg(context
) = INSTR_inport( LOWORD(EDX_reg(context
)), 4, context
);
754 SET_LOWORD(EAX_reg(context
), INSTR_inport( LOWORD(EDX_reg(context
)), 2, context
));
755 EIP_reg(context
) += prefixlen
+ 1;
758 case 0xee: /* outb dx,al */
759 INSTR_outport( LOWORD(EDX_reg(context
)), 1, LOBYTE(EAX_reg(context
)), context
);
760 EIP_reg(context
) += prefixlen
+ 1;
763 case 0xef: /* out dx,(e)ax */
765 INSTR_outport( LOWORD(EDX_reg(context
)), 4, EAX_reg(context
), context
);
767 INSTR_outport( LOWORD(EDX_reg(context
)), 2, LOWORD(EAX_reg(context
)), context
);
768 EIP_reg(context
) += prefixlen
+ 1;
771 case 0xfa: /* cli, ignored */
772 EIP_reg(context
) += prefixlen
+ 1;
775 case 0xfb: /* sti, ignored */
776 EIP_reg(context
) += prefixlen
+ 1;
781 /* Check for Win16 __GP handler */
782 gpHandler
= HasGPHandler16( PTR_SEG_OFF_TO_SEGPTR( CS_reg(context
),
783 EIP_reg(context
) ) );
786 WORD
*stack
= (WORD
*)STACK_PTR( context
);
787 *--stack
= CS_reg(context
);
788 *--stack
= EIP_reg(context
);
789 ADD_STACK_reg(context
, -2*sizeof(WORD
));
791 CS_reg(context
) = SELECTOROF( gpHandler
);
792 EIP_reg(context
) = OFFSETOF( gpHandler
);
795 return FALSE
; /* Unable to emulate it */
798 #endif /* __i386__ */