2 * Emulation of priviledged instructions
4 * Copyright 1995 Alexandre Julliard
9 #include "wine/winuser16.h"
14 #include "selectors.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(int);
18 DECLARE_DEBUG_CHANNEL(io
);
22 inline static void add_stack( CONTEXT86
*context
, int offset
)
24 if (ISV86(context
) || !IS_SELECTOR_32BIT(context
->SegSs
))
25 ADD_LOWORD( context
->Esp
, offset
);
27 context
->Esp
+= offset
;
30 inline static void *make_ptr( CONTEXT86
*context
, DWORD seg
, DWORD off
, int long_addr
)
32 if (ISV86(context
)) return PTR_REAL_TO_LIN( seg
, off
);
33 if (IS_SELECTOR_SYSTEM(seg
)) return (void *)off
;
34 if (!long_addr
) off
= LOWORD(off
);
35 return PTR_SEG_OFF_TO_LIN( seg
, off
);
38 inline static void *get_stack( CONTEXT86
*context
)
41 return PTR_REAL_TO_LIN( context
->SegSs
, context
->Esp
);
42 if (IS_SELECTOR_SYSTEM(context
->SegSs
))
43 return (void *)context
->Esp
;
44 if (IS_SELECTOR_32BIT(context
->SegSs
))
45 return PTR_SEG_OFF_TO_LIN( context
->SegSs
, context
->Esp
);
46 return PTR_SEG_OFF_TO_LIN( context
->SegSs
, LOWORD(context
->Esp
) );
49 /***********************************************************************
50 * INSTR_ReplaceSelector
52 * Try to replace an invalid selector by a valid one.
53 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
54 * is the so called 'bimodal' selector 0x40, which points to the BIOS
55 * data segment. Used by (at least) Borland products (and programs compiled
56 * using Borland products).
58 * See Undocumented Windows, Chapter 5, __0040.
60 static BOOL
INSTR_ReplaceSelector( CONTEXT86
*context
, WORD
*sel
)
62 extern char Call16_Start
, Call16_End
;
64 if (IS_SELECTOR_SYSTEM(context
->SegCs
))
65 if ( (char *)context
->Eip
>= &Call16_Start
66 && (char *)context
->Eip
< &Call16_End
)
68 /* Saved selector may have become invalid when the relay code */
69 /* tries to restore it. We simply clear it. */
76 static WORD sys_timer
= 0;
78 sys_timer
= CreateSystemTimer( 55, DOSMEM_Tick
);
79 *sel
= DOSMEM_BiosDataSeg
;
82 if (!IS_SELECTOR_SYSTEM(*sel
) && !IS_SELECTOR_FREE(*sel
))
83 ERR("Got protection fault on valid selector, maybe your kernel is too old?\n" );
84 return FALSE
; /* Can't replace selector, crashdump */
88 /***********************************************************************
89 * INSTR_GetOperandAddr
91 * Return the address of an instruction operand (from the mod/rm byte).
93 static BYTE
*INSTR_GetOperandAddr( CONTEXT86
*context
, BYTE
*instr
,
94 int long_addr
, int segprefix
, int *len
)
96 int mod
, rm
, base
, index
= 0, ss
= 0, seg
= 0, off
;
98 #define GET_VAL(val,type) \
99 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
102 GET_VAL( &mod
, BYTE
);
110 case 0: return (BYTE
*)&context
->Eax
;
111 case 1: return (BYTE
*)&context
->Ecx
;
112 case 2: return (BYTE
*)&context
->Edx
;
113 case 3: return (BYTE
*)&context
->Ebx
;
114 case 4: return (BYTE
*)&context
->Esp
;
115 case 5: return (BYTE
*)&context
->Ebp
;
116 case 6: return (BYTE
*)&context
->Esi
;
117 case 7: return (BYTE
*)&context
->Edi
;
126 GET_VAL( &sib
, BYTE
);
131 case 0: index
= context
->Eax
; break;
132 case 1: index
= context
->Ecx
; break;
133 case 2: index
= context
->Edx
; break;
134 case 3: index
= context
->Ebx
; break;
135 case 4: index
= 0; break;
136 case 5: index
= context
->Ebp
; break;
137 case 6: index
= context
->Esi
; break;
138 case 7: index
= context
->Edi
; break;
144 case 0: base
= context
->Eax
; seg
= context
->SegDs
; break;
145 case 1: base
= context
->Ecx
; seg
= context
->SegDs
; break;
146 case 2: base
= context
->Edx
; seg
= context
->SegDs
; break;
147 case 3: base
= context
->Ebx
; seg
= context
->SegDs
; break;
148 case 4: base
= context
->Esp
; seg
= context
->SegSs
; break;
149 case 5: base
= context
->Ebp
; seg
= context
->SegSs
; break;
150 case 6: base
= context
->Esi
; seg
= context
->SegDs
; break;
151 case 7: base
= context
->Edi
; seg
= context
->SegDs
; break;
156 if (rm
== 5) /* special case: ds:(disp32) */
158 GET_VAL( &base
, DWORD
);
159 seg
= context
->SegDs
;
163 case 1: /* 8-bit disp */
164 GET_VAL( &off
, BYTE
);
165 base
+= (signed char)off
;
168 case 2: /* 32-bit disp */
169 GET_VAL( &off
, DWORD
);
170 base
+= (signed long)off
;
174 else /* short address */
178 case 0: /* ds:(bx,si) */
179 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Esi
);
180 seg
= context
->SegDs
;
182 case 1: /* ds:(bx,di) */
183 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Edi
);
184 seg
= context
->SegDs
;
186 case 2: /* ss:(bp,si) */
187 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Esi
);
188 seg
= context
->SegSs
;
190 case 3: /* ss:(bp,di) */
191 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Edi
);
192 seg
= context
->SegSs
;
194 case 4: /* ds:(si) */
195 base
= LOWORD(context
->Esi
);
196 seg
= context
->SegDs
;
198 case 5: /* ds:(di) */
199 base
= LOWORD(context
->Edi
);
200 seg
= context
->SegDs
;
202 case 6: /* ss:(bp) */
203 base
= LOWORD(context
->Ebp
);
204 seg
= context
->SegSs
;
206 case 7: /* ds:(bx) */
207 base
= LOWORD(context
->Ebx
);
208 seg
= context
->SegDs
;
215 if (rm
== 6) /* special case: ds:(disp16) */
217 GET_VAL( &base
, WORD
);
218 seg
= context
->SegDs
;
222 case 1: /* 8-bit disp */
223 GET_VAL( &off
, BYTE
);
224 base
+= (signed char)off
;
227 case 2: /* 16-bit disp */
228 GET_VAL( &off
, WORD
);
229 base
+= (signed short)off
;
234 if (segprefix
!= -1) seg
= segprefix
;
236 /* Make sure the segment and offset are valid */
237 if (IS_SELECTOR_SYSTEM(seg
)) return (BYTE
*)(base
+ (index
<< ss
));
238 if (((seg
& 7) != 7) || IS_SELECTOR_FREE(seg
)) return NULL
;
239 if (wine_ldt_copy
.limit
[seg
>> 3] < (base
+ (index
<< ss
))) return NULL
;
240 return (BYTE
*)PTR_SEG_OFF_TO_LIN( seg
, (base
+ (index
<< ss
)) );
245 /***********************************************************************
248 * Emulate the LDS (and LES,LFS,etc.) instruction.
250 static BOOL
INSTR_EmulateLDS( CONTEXT86
*context
, BYTE
*instr
, int long_op
,
251 int long_addr
, int segprefix
, int *len
)
254 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
255 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
256 long_addr
, segprefix
, len
);
258 return FALSE
; /* Unable to emulate it */
259 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
261 if (!INSTR_ReplaceSelector( context
, &seg
))
262 return FALSE
; /* Unable to emulate it */
264 /* Now store the offset in the correct register */
266 switch((*regmodrm
>> 3) & 7)
269 if (long_op
) context
->Eax
= *(DWORD
*)addr
;
270 else SET_LOWORD(context
->Eax
,*(WORD
*)addr
);
273 if (long_op
) context
->Ecx
= *(DWORD
*)addr
;
274 else SET_LOWORD(context
->Ecx
,*(WORD
*)addr
);
277 if (long_op
) context
->Edx
= *(DWORD
*)addr
;
278 else SET_LOWORD(context
->Edx
,*(WORD
*)addr
);
281 if (long_op
) context
->Ebx
= *(DWORD
*)addr
;
282 else SET_LOWORD(context
->Ebx
,*(WORD
*)addr
);
285 if (long_op
) context
->Esp
= *(DWORD
*)addr
;
286 else SET_LOWORD(context
->Esp
,*(WORD
*)addr
);
289 if (long_op
) context
->Ebp
= *(DWORD
*)addr
;
290 else SET_LOWORD(context
->Ebp
,*(WORD
*)addr
);
293 if (long_op
) context
->Esi
= *(DWORD
*)addr
;
294 else SET_LOWORD(context
->Esi
,*(WORD
*)addr
);
297 if (long_op
) context
->Edi
= *(DWORD
*)addr
;
298 else SET_LOWORD(context
->Edi
,*(WORD
*)addr
);
302 /* Store the correct segment in the segment register */
306 case 0xc4: context
->SegEs
= seg
; break; /* les */
307 case 0xc5: context
->SegDs
= seg
; break; /* lds */
308 case 0x0f: switch(instr
[1])
310 case 0xb2: context
->SegSs
= seg
; break; /* lss */
311 case 0xb4: context
->SegFs
= seg
; break; /* lfs */
312 case 0xb5: context
->SegGs
= seg
; break; /* lgs */
317 /* Add the opcode size to the total length */
319 *len
+= 1 + (*instr
== 0x0f);
323 /***********************************************************************
326 * input on a I/O port
328 static DWORD
INSTR_inport( WORD port
, int size
, CONTEXT86
*context
)
330 DWORD res
= IO_inport( port
, size
);
336 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port
, LOBYTE(res
),
337 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
340 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port
, LOWORD(res
),
341 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
344 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port
, res
,
345 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
353 /***********************************************************************
356 * output on a I/O port
358 static void INSTR_outport( WORD port
, int size
, DWORD val
, CONTEXT86
*context
)
360 IO_outport( port
, size
, val
);
366 DPRINTF("0x%x > %02x @ %04x:%04x\n", port
, LOBYTE(val
),
367 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
370 DPRINTF("0x%x > %04x @ %04x:%04x\n", port
, LOWORD(val
),
371 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
374 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port
, val
,
375 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
382 /***********************************************************************
383 * INSTR_EmulateInstruction
385 * Emulate a priviledged instruction. Returns TRUE if emulation successful.
387 BOOL
INSTR_EmulateInstruction( CONTEXT86
*context
)
389 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
393 long_op
= long_addr
= (!ISV86(context
) && IS_SELECTOR_32BIT(context
->SegCs
));
394 instr
= make_ptr( context
, context
->SegCs
, context
->Eip
, TRUE
);
395 if (!instr
) return FALSE
;
397 /* First handle any possible prefix */
399 segprefix
= -1; /* no prefix */
408 segprefix
= context
->SegCs
;
411 segprefix
= context
->SegSs
;
414 segprefix
= context
->SegDs
;
417 segprefix
= context
->SegEs
;
420 segprefix
= context
->SegFs
;
423 segprefix
= context
->SegGs
;
426 long_op
= !long_op
; /* opcode size prefix */
429 long_addr
= !long_addr
; /* addr size prefix */
431 case 0xf0: /* lock */
433 case 0xf2: /* repne */
436 case 0xf3: /* repe */
440 prefix
= 0; /* no more prefixes */
450 /* Now look at the actual instruction */
454 case 0x07: /* pop es */
455 case 0x17: /* pop ss */
456 case 0x1f: /* pop ds */
458 WORD seg
= *(WORD
*)get_stack( context
);
459 if (INSTR_ReplaceSelector( context
, &seg
))
463 case 0x07: context
->SegEs
= seg
; break;
464 case 0x17: context
->SegSs
= seg
; break;
465 case 0x1f: context
->SegDs
= seg
; break;
467 add_stack(context
, long_op
? 4 : 2);
468 context
->Eip
+= prefixlen
+ 1;
472 break; /* Unable to emulate it */
474 case 0x0f: /* extended instruction */
477 case 0x22: /* mov eax, crX */
480 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
481 context
->Eip
,context
->Eax
);
482 context
->Eip
+= prefixlen
+3;
485 break; /*fallthrough to bad instruction handling */
487 break; /*fallthrough to bad instruction handling */
488 case 0x20: /* mov crX, eax */
490 case 0xe0: /* mov cr4, eax */
491 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
492 * bit 0: VME Virtual Mode Exception ?
493 * bit 1: PVI Protected mode Virtual Interrupt
494 * bit 2: TSD Timestamp disable
495 * bit 3: DE Debugging extensions
496 * bit 4: PSE Page size extensions
497 * bit 5: PAE Physical address extension
498 * bit 6: MCE Machine check enable
499 * bit 7: PGE Enable global pages
500 * bit 8: PCE Enable performance counters at IPL3
502 ERR("mov cr4,eax at 0x%08lx\n",context
->Eip
);
504 context
->Eip
+= prefixlen
+3;
506 case 0xc0: /* mov cr0, eax */
507 ERR("mov cr0,eax at 0x%08lx\n",context
->Eip
);
508 context
->Eax
= 0x10; /* FIXME: set more bits ? */
509 context
->Eip
+= prefixlen
+3;
511 default: /* fallthrough to illegal instruction */
514 /* fallthrough to illegal instruction */
516 case 0xa1: /* pop fs */
518 WORD seg
= *(WORD
*)get_stack( context
);
519 if (INSTR_ReplaceSelector( context
, &seg
))
521 context
->SegFs
= seg
;
522 add_stack(context
, long_op
? 4 : 2);
523 context
->Eip
+= prefixlen
+ 2;
528 case 0xa9: /* pop gs */
530 WORD seg
= *(WORD
*)get_stack( context
);
531 if (INSTR_ReplaceSelector( context
, &seg
))
533 context
->SegGs
= seg
;
534 add_stack(context
, long_op
? 4 : 2);
535 context
->Eip
+= prefixlen
+ 2;
540 case 0xb2: /* lss addr,reg */
541 case 0xb4: /* lfs addr,reg */
542 case 0xb5: /* lgs addr,reg */
543 if (INSTR_EmulateLDS( context
, instr
, long_op
,
544 long_addr
, segprefix
, &len
))
546 context
->Eip
+= prefixlen
+ len
;
551 break; /* Unable to emulate it */
553 case 0x6c: /* insb */
554 case 0x6d: /* insw/d */
555 case 0x6e: /* outsb */
556 case 0x6f: /* outsw/d */
558 int typ
= *instr
; /* Just in case it's overwritten. */
559 int outp
= (typ
>= 0x6e);
560 unsigned long count
= repX
?
561 (long_addr
? context
->Ecx
: LOWORD(context
->Ecx
)) : 1;
562 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
563 int step
= (context
->EFlags
& 0x400) ? -opsize
: +opsize
;
564 int seg
= outp
? context
->SegDs
: context
->SegEs
; /* FIXME: is this right? */
567 /* FIXME: Check segment readable. */
570 /* FIXME: Check segment writeable. */
575 if (long_addr
) context
->Ecx
= 0;
576 else SET_LOWORD(context
->Ecx
,0);
582 WORD dx
= LOWORD(context
->Edx
);
585 data
= make_ptr( context
, seg
, context
->Esi
, long_addr
);
586 if (long_addr
) context
->Esi
+= step
;
587 else ADD_LOWORD(context
->Esi
,step
);
591 data
= make_ptr( context
, seg
, context
->Edi
, long_addr
);
592 if (long_addr
) context
->Edi
+= step
;
593 else ADD_LOWORD(context
->Edi
,step
);
599 *(BYTE
*)data
= INSTR_inport( dx
, 1, context
);
603 *(DWORD
*)data
= INSTR_inport( dx
, 4, context
);
605 *(WORD
*)data
= INSTR_inport( dx
, 2, context
);
608 INSTR_outport( dx
, 1, *(BYTE
*)data
, context
);
612 INSTR_outport( dx
, 4, *(DWORD
*)data
, context
);
614 INSTR_outport( dx
, 2, *(WORD
*)data
, context
);
618 context
->Eip
+= prefixlen
+ 1;
622 case 0x8e: /* mov XX,segment_reg */
625 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
626 long_addr
, segprefix
, &len
);
628 break; /* Unable to emulate it */
630 if (!INSTR_ReplaceSelector( context
, &seg
))
631 break; /* Unable to emulate it */
633 switch((instr
[1] >> 3) & 7)
636 context
->SegEs
= seg
;
637 context
->Eip
+= prefixlen
+ len
+ 1;
642 context
->SegSs
= seg
;
643 context
->Eip
+= prefixlen
+ len
+ 1;
646 context
->SegDs
= seg
;
647 context
->Eip
+= prefixlen
+ len
+ 1;
650 context
->SegFs
= seg
;
651 context
->Eip
+= prefixlen
+ len
+ 1;
654 context
->SegGs
= seg
;
655 context
->Eip
+= prefixlen
+ len
+ 1;
662 break; /* Unable to emulate it */
664 case 0xc4: /* les addr,reg */
665 case 0xc5: /* lds addr,reg */
666 if (INSTR_EmulateLDS( context
, instr
, long_op
,
667 long_addr
, segprefix
, &len
))
669 context
->Eip
+= prefixlen
+ len
;
672 break; /* Unable to emulate it */
674 case 0xcd: /* int <XX> */
677 ERR("int xx from 32-bit code is not supported.\n");
678 break; /* Unable to emulate it */
682 FARPROC16 addr
= INT_GetPMHandler( instr
[1] );
683 WORD
*stack
= get_stack( context
);
686 FIXME("no handler for interrupt %02x, ignoring it\n", instr
[1]);
687 context
->Eip
+= prefixlen
+ 2;
690 /* Push the flags and return address on the stack */
691 *(--stack
) = LOWORD(context
->EFlags
);
692 *(--stack
) = context
->SegCs
;
693 *(--stack
) = LOWORD(context
->Eip
) + prefixlen
+ 2;
694 add_stack(context
, -3 * sizeof(WORD
));
695 /* Jump to the interrupt handler */
696 context
->SegCs
= HIWORD(addr
);
697 context
->Eip
= LOWORD(addr
);
701 case 0xcf: /* iret */
704 DWORD
*stack
= get_stack( context
);
705 context
->Eip
= *stack
++;
706 context
->SegCs
= *stack
++;
707 context
->EFlags
= *stack
;
708 add_stack(context
, 3*sizeof(DWORD
)); /* Pop the return address and flags */
712 WORD
*stack
= get_stack( context
);
713 context
->Eip
= *stack
++;
714 context
->SegCs
= *stack
++;
715 SET_LOWORD(context
->EFlags
,*stack
);
716 add_stack(context
, 3*sizeof(WORD
)); /* Pop the return address and flags */
720 case 0xe4: /* inb al,XX */
721 SET_LOBYTE(context
->Eax
,INSTR_inport( instr
[1], 1, context
));
722 context
->Eip
+= prefixlen
+ 2;
725 case 0xe5: /* in (e)ax,XX */
727 context
->Eax
= INSTR_inport( instr
[1], 4, context
);
729 SET_LOWORD(context
->Eax
, INSTR_inport( instr
[1], 2, context
));
730 context
->Eip
+= prefixlen
+ 2;
733 case 0xe6: /* outb XX,al */
734 INSTR_outport( instr
[1], 1, LOBYTE(context
->Eax
), context
);
735 context
->Eip
+= prefixlen
+ 2;
738 case 0xe7: /* out XX,(e)ax */
740 INSTR_outport( instr
[1], 4, context
->Eax
, context
);
742 INSTR_outport( instr
[1], 2, LOWORD(context
->Eax
), context
);
743 context
->Eip
+= prefixlen
+ 2;
746 case 0xec: /* inb al,dx */
747 SET_LOBYTE(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 1, context
) );
748 context
->Eip
+= prefixlen
+ 1;
751 case 0xed: /* in (e)ax,dx */
753 context
->Eax
= INSTR_inport( LOWORD(context
->Edx
), 4, context
);
755 SET_LOWORD(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 2, context
));
756 context
->Eip
+= prefixlen
+ 1;
759 case 0xee: /* outb dx,al */
760 INSTR_outport( LOWORD(context
->Edx
), 1, LOBYTE(context
->Eax
), context
);
761 context
->Eip
+= prefixlen
+ 1;
764 case 0xef: /* out dx,(e)ax */
766 INSTR_outport( LOWORD(context
->Edx
), 4, context
->Eax
, context
);
768 INSTR_outport( LOWORD(context
->Edx
), 2, LOWORD(context
->Eax
), context
);
769 context
->Eip
+= prefixlen
+ 1;
772 case 0xfa: /* cli, ignored */
773 context
->Eip
+= prefixlen
+ 1;
776 case 0xfb: /* sti, ignored */
777 context
->Eip
+= prefixlen
+ 1;
782 /* Check for Win16 __GP handler */
783 gpHandler
= HasGPHandler16( PTR_SEG_OFF_TO_SEGPTR( context
->SegCs
, context
->Eip
) );
786 WORD
*stack
= get_stack( context
);
787 *--stack
= context
->SegCs
;
788 *--stack
= context
->Eip
;
789 add_stack(context
, -2*sizeof(WORD
));
791 context
->SegCs
= SELECTOROF( gpHandler
);
792 context
->Eip
= OFFSETOF( gpHandler
);
795 return FALSE
; /* Unable to emulate it */
798 #endif /* __i386__ */