2 * Emulation of privileged instructions
4 * Copyright 1995 Alexandre Julliard
9 #include "wine/winuser16.h"
12 #include "selectors.h"
13 #include "debugtools.h"
15 DEFAULT_DEBUG_CHANNEL(int);
16 DECLARE_DEBUG_CHANNEL(io
);
20 inline static void add_stack( CONTEXT86
*context
, int offset
)
22 if (ISV86(context
) || !IS_SELECTOR_32BIT(context
->SegSs
))
23 ADD_LOWORD( context
->Esp
, offset
);
25 context
->Esp
+= offset
;
28 inline static void *make_ptr( CONTEXT86
*context
, DWORD seg
, DWORD off
, int long_addr
)
30 if (ISV86(context
)) return PTR_REAL_TO_LIN( seg
, off
);
31 if (IS_SELECTOR_SYSTEM(seg
)) return (void *)off
;
32 if (!long_addr
) off
= LOWORD(off
);
33 return MapSL( MAKESEGPTR( seg
, off
) );
36 inline static void *get_stack( CONTEXT86
*context
)
39 return PTR_REAL_TO_LIN( context
->SegSs
, context
->Esp
);
40 if (IS_SELECTOR_SYSTEM(context
->SegSs
))
41 return (void *)context
->Esp
;
42 if (IS_SELECTOR_32BIT(context
->SegSs
))
43 return MapSL( MAKESEGPTR( context
->SegSs
, context
->Esp
) );
44 return MapSL( MAKESEGPTR( context
->SegSs
, LOWORD(context
->Esp
) ) );
47 /***********************************************************************
48 * INSTR_ReplaceSelector
50 * Try to replace an invalid selector by a valid one.
51 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
52 * is the so called 'bimodal' selector 0x40, which points to the BIOS
53 * data segment. Used by (at least) Borland products (and programs compiled
54 * using Borland products).
56 * See Undocumented Windows, Chapter 5, __0040.
58 static BOOL
INSTR_ReplaceSelector( CONTEXT86
*context
, WORD
*sel
)
60 extern char Call16_Start
, Call16_End
;
62 if (IS_SELECTOR_SYSTEM(context
->SegCs
))
63 if ( (char *)context
->Eip
>= &Call16_Start
64 && (char *)context
->Eip
< &Call16_End
)
66 /* Saved selector may have become invalid when the relay code */
67 /* tries to restore it. We simply clear it. */
74 static WORD sys_timer
= 0;
76 sys_timer
= CreateSystemTimer( 55, DOSMEM_Tick
);
77 *sel
= DOSMEM_BiosDataSeg
;
80 if (!IS_SELECTOR_SYSTEM(*sel
) && !IS_SELECTOR_FREE(*sel
))
81 ERR("Got protection fault on valid selector, maybe your kernel is too old?\n" );
82 return FALSE
; /* Can't replace selector, crashdump */
86 /***********************************************************************
87 * INSTR_GetOperandAddr
89 * Return the address of an instruction operand (from the mod/rm byte).
91 static BYTE
*INSTR_GetOperandAddr( CONTEXT86
*context
, BYTE
*instr
,
92 int long_addr
, int segprefix
, int *len
)
94 int mod
, rm
, base
, index
= 0, ss
= 0, seg
= 0, off
;
96 #define GET_VAL(val,type) \
97 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
100 GET_VAL( &mod
, BYTE
);
108 case 0: return (BYTE
*)&context
->Eax
;
109 case 1: return (BYTE
*)&context
->Ecx
;
110 case 2: return (BYTE
*)&context
->Edx
;
111 case 3: return (BYTE
*)&context
->Ebx
;
112 case 4: return (BYTE
*)&context
->Esp
;
113 case 5: return (BYTE
*)&context
->Ebp
;
114 case 6: return (BYTE
*)&context
->Esi
;
115 case 7: return (BYTE
*)&context
->Edi
;
124 GET_VAL( &sib
, BYTE
);
129 case 0: index
= context
->Eax
; break;
130 case 1: index
= context
->Ecx
; break;
131 case 2: index
= context
->Edx
; break;
132 case 3: index
= context
->Ebx
; break;
133 case 4: index
= 0; break;
134 case 5: index
= context
->Ebp
; break;
135 case 6: index
= context
->Esi
; break;
136 case 7: index
= context
->Edi
; break;
142 case 0: base
= context
->Eax
; seg
= context
->SegDs
; break;
143 case 1: base
= context
->Ecx
; seg
= context
->SegDs
; break;
144 case 2: base
= context
->Edx
; seg
= context
->SegDs
; break;
145 case 3: base
= context
->Ebx
; seg
= context
->SegDs
; break;
146 case 4: base
= context
->Esp
; seg
= context
->SegSs
; break;
147 case 5: base
= context
->Ebp
; seg
= context
->SegSs
; break;
148 case 6: base
= context
->Esi
; seg
= context
->SegDs
; break;
149 case 7: base
= context
->Edi
; seg
= context
->SegDs
; break;
154 if (rm
== 5) /* special case: ds:(disp32) */
156 GET_VAL( &base
, DWORD
);
157 seg
= context
->SegDs
;
161 case 1: /* 8-bit disp */
162 GET_VAL( &off
, BYTE
);
163 base
+= (signed char)off
;
166 case 2: /* 32-bit disp */
167 GET_VAL( &off
, DWORD
);
168 base
+= (signed long)off
;
172 else /* short address */
176 case 0: /* ds:(bx,si) */
177 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Esi
);
178 seg
= context
->SegDs
;
180 case 1: /* ds:(bx,di) */
181 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Edi
);
182 seg
= context
->SegDs
;
184 case 2: /* ss:(bp,si) */
185 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Esi
);
186 seg
= context
->SegSs
;
188 case 3: /* ss:(bp,di) */
189 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Edi
);
190 seg
= context
->SegSs
;
192 case 4: /* ds:(si) */
193 base
= LOWORD(context
->Esi
);
194 seg
= context
->SegDs
;
196 case 5: /* ds:(di) */
197 base
= LOWORD(context
->Edi
);
198 seg
= context
->SegDs
;
200 case 6: /* ss:(bp) */
201 base
= LOWORD(context
->Ebp
);
202 seg
= context
->SegSs
;
204 case 7: /* ds:(bx) */
205 base
= LOWORD(context
->Ebx
);
206 seg
= context
->SegDs
;
213 if (rm
== 6) /* special case: ds:(disp16) */
215 GET_VAL( &base
, WORD
);
216 seg
= context
->SegDs
;
220 case 1: /* 8-bit disp */
221 GET_VAL( &off
, BYTE
);
222 base
+= (signed char)off
;
225 case 2: /* 16-bit disp */
226 GET_VAL( &off
, WORD
);
227 base
+= (signed short)off
;
232 if (segprefix
!= -1) seg
= segprefix
;
234 /* Make sure the segment and offset are valid */
235 if (IS_SELECTOR_SYSTEM(seg
)) return (BYTE
*)(base
+ (index
<< ss
));
236 if (((seg
& 7) != 7) || IS_SELECTOR_FREE(seg
)) return NULL
;
237 if (wine_ldt_copy
.limit
[seg
>> 3] < (base
+ (index
<< ss
))) return NULL
;
238 return MapSL( MAKESEGPTR( seg
, (base
+ (index
<< ss
))) );
243 /***********************************************************************
246 * Emulate the LDS (and LES,LFS,etc.) instruction.
248 static BOOL
INSTR_EmulateLDS( CONTEXT86
*context
, BYTE
*instr
, int long_op
,
249 int long_addr
, int segprefix
, int *len
)
252 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
253 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
254 long_addr
, segprefix
, len
);
256 return FALSE
; /* Unable to emulate it */
257 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
259 if (!INSTR_ReplaceSelector( context
, &seg
))
260 return FALSE
; /* Unable to emulate it */
262 /* Now store the offset in the correct register */
264 switch((*regmodrm
>> 3) & 7)
267 if (long_op
) context
->Eax
= *(DWORD
*)addr
;
268 else SET_LOWORD(context
->Eax
,*(WORD
*)addr
);
271 if (long_op
) context
->Ecx
= *(DWORD
*)addr
;
272 else SET_LOWORD(context
->Ecx
,*(WORD
*)addr
);
275 if (long_op
) context
->Edx
= *(DWORD
*)addr
;
276 else SET_LOWORD(context
->Edx
,*(WORD
*)addr
);
279 if (long_op
) context
->Ebx
= *(DWORD
*)addr
;
280 else SET_LOWORD(context
->Ebx
,*(WORD
*)addr
);
283 if (long_op
) context
->Esp
= *(DWORD
*)addr
;
284 else SET_LOWORD(context
->Esp
,*(WORD
*)addr
);
287 if (long_op
) context
->Ebp
= *(DWORD
*)addr
;
288 else SET_LOWORD(context
->Ebp
,*(WORD
*)addr
);
291 if (long_op
) context
->Esi
= *(DWORD
*)addr
;
292 else SET_LOWORD(context
->Esi
,*(WORD
*)addr
);
295 if (long_op
) context
->Edi
= *(DWORD
*)addr
;
296 else SET_LOWORD(context
->Edi
,*(WORD
*)addr
);
300 /* Store the correct segment in the segment register */
304 case 0xc4: context
->SegEs
= seg
; break; /* les */
305 case 0xc5: context
->SegDs
= seg
; break; /* lds */
306 case 0x0f: switch(instr
[1])
308 case 0xb2: context
->SegSs
= seg
; break; /* lss */
309 case 0xb4: context
->SegFs
= seg
; break; /* lfs */
310 case 0xb5: context
->SegGs
= seg
; break; /* lgs */
315 /* Add the opcode size to the total length */
317 *len
+= 1 + (*instr
== 0x0f);
321 /***********************************************************************
324 * input on a I/O port
326 static DWORD
INSTR_inport( WORD port
, int size
, CONTEXT86
*context
)
328 DWORD res
= IO_inport( port
, size
);
334 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port
, LOBYTE(res
),
335 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
338 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port
, LOWORD(res
),
339 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
342 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port
, res
,
343 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
351 /***********************************************************************
354 * output on a I/O port
356 static void INSTR_outport( WORD port
, int size
, DWORD val
, CONTEXT86
*context
)
358 IO_outport( port
, size
, val
);
364 DPRINTF("0x%x > %02x @ %04x:%04x\n", port
, LOBYTE(val
),
365 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
368 DPRINTF("0x%x > %04x @ %04x:%04x\n", port
, LOWORD(val
),
369 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
372 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port
, val
,
373 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
380 /***********************************************************************
381 * INSTR_EmulateInstruction
383 * Emulate a privileged instruction. Returns TRUE if emulation successful.
385 BOOL
INSTR_EmulateInstruction( CONTEXT86
*context
)
387 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
391 long_op
= long_addr
= (!ISV86(context
) && IS_SELECTOR_32BIT(context
->SegCs
));
392 instr
= make_ptr( context
, context
->SegCs
, context
->Eip
, TRUE
);
393 if (!instr
) return FALSE
;
395 /* First handle any possible prefix */
397 segprefix
= -1; /* no prefix */
406 segprefix
= context
->SegCs
;
409 segprefix
= context
->SegSs
;
412 segprefix
= context
->SegDs
;
415 segprefix
= context
->SegEs
;
418 segprefix
= context
->SegFs
;
421 segprefix
= context
->SegGs
;
424 long_op
= !long_op
; /* opcode size prefix */
427 long_addr
= !long_addr
; /* addr size prefix */
429 case 0xf0: /* lock */
431 case 0xf2: /* repne */
434 case 0xf3: /* repe */
438 prefix
= 0; /* no more prefixes */
448 /* Now look at the actual instruction */
452 case 0x07: /* pop es */
453 case 0x17: /* pop ss */
454 case 0x1f: /* pop ds */
456 WORD seg
= *(WORD
*)get_stack( context
);
457 if (INSTR_ReplaceSelector( context
, &seg
))
461 case 0x07: context
->SegEs
= seg
; break;
462 case 0x17: context
->SegSs
= seg
; break;
463 case 0x1f: context
->SegDs
= seg
; break;
465 add_stack(context
, long_op
? 4 : 2);
466 context
->Eip
+= prefixlen
+ 1;
470 break; /* Unable to emulate it */
472 case 0x0f: /* extended instruction */
475 case 0x22: /* mov eax, crX */
478 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
479 context
->Eip
,context
->Eax
);
480 context
->Eip
+= prefixlen
+3;
483 break; /*fallthrough to bad instruction handling */
485 break; /*fallthrough to bad instruction handling */
486 case 0x20: /* mov crX, eax */
488 case 0xe0: /* mov cr4, eax */
489 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
490 * bit 0: VME Virtual Mode Exception ?
491 * bit 1: PVI Protected mode Virtual Interrupt
492 * bit 2: TSD Timestamp disable
493 * bit 3: DE Debugging extensions
494 * bit 4: PSE Page size extensions
495 * bit 5: PAE Physical address extension
496 * bit 6: MCE Machine check enable
497 * bit 7: PGE Enable global pages
498 * bit 8: PCE Enable performance counters at IPL3
500 ERR("mov cr4,eax at 0x%08lx\n",context
->Eip
);
502 context
->Eip
+= prefixlen
+3;
504 case 0xc0: /* mov cr0, eax */
505 ERR("mov cr0,eax at 0x%08lx\n",context
->Eip
);
506 context
->Eax
= 0x10; /* FIXME: set more bits ? */
507 context
->Eip
+= prefixlen
+3;
509 default: /* fallthrough to illegal instruction */
512 /* fallthrough to illegal instruction */
514 case 0xa1: /* pop fs */
516 WORD seg
= *(WORD
*)get_stack( context
);
517 if (INSTR_ReplaceSelector( context
, &seg
))
519 context
->SegFs
= seg
;
520 add_stack(context
, long_op
? 4 : 2);
521 context
->Eip
+= prefixlen
+ 2;
526 case 0xa9: /* pop gs */
528 WORD seg
= *(WORD
*)get_stack( context
);
529 if (INSTR_ReplaceSelector( context
, &seg
))
531 context
->SegGs
= seg
;
532 add_stack(context
, long_op
? 4 : 2);
533 context
->Eip
+= prefixlen
+ 2;
538 case 0xb2: /* lss addr,reg */
539 case 0xb4: /* lfs addr,reg */
540 case 0xb5: /* lgs addr,reg */
541 if (INSTR_EmulateLDS( context
, instr
, long_op
,
542 long_addr
, segprefix
, &len
))
544 context
->Eip
+= prefixlen
+ len
;
549 break; /* Unable to emulate it */
551 case 0x6c: /* insb */
552 case 0x6d: /* insw/d */
553 case 0x6e: /* outsb */
554 case 0x6f: /* outsw/d */
556 int typ
= *instr
; /* Just in case it's overwritten. */
557 int outp
= (typ
>= 0x6e);
558 unsigned long count
= repX
?
559 (long_addr
? context
->Ecx
: LOWORD(context
->Ecx
)) : 1;
560 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
561 int step
= (context
->EFlags
& 0x400) ? -opsize
: +opsize
;
562 int seg
= outp
? context
->SegDs
: context
->SegEs
; /* FIXME: is this right? */
565 /* FIXME: Check segment readable. */
568 /* FIXME: Check segment writeable. */
573 if (long_addr
) context
->Ecx
= 0;
574 else SET_LOWORD(context
->Ecx
,0);
580 WORD dx
= LOWORD(context
->Edx
);
583 data
= make_ptr( context
, seg
, context
->Esi
, long_addr
);
584 if (long_addr
) context
->Esi
+= step
;
585 else ADD_LOWORD(context
->Esi
,step
);
589 data
= make_ptr( context
, seg
, context
->Edi
, long_addr
);
590 if (long_addr
) context
->Edi
+= step
;
591 else ADD_LOWORD(context
->Edi
,step
);
597 *(BYTE
*)data
= INSTR_inport( dx
, 1, context
);
601 *(DWORD
*)data
= INSTR_inport( dx
, 4, context
);
603 *(WORD
*)data
= INSTR_inport( dx
, 2, context
);
606 INSTR_outport( dx
, 1, *(BYTE
*)data
, context
);
610 INSTR_outport( dx
, 4, *(DWORD
*)data
, context
);
612 INSTR_outport( dx
, 2, *(WORD
*)data
, context
);
616 context
->Eip
+= prefixlen
+ 1;
620 case 0x8e: /* mov XX,segment_reg */
623 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
624 long_addr
, segprefix
, &len
);
626 break; /* Unable to emulate it */
628 if (!INSTR_ReplaceSelector( context
, &seg
))
629 break; /* Unable to emulate it */
631 switch((instr
[1] >> 3) & 7)
634 context
->SegEs
= seg
;
635 context
->Eip
+= prefixlen
+ len
+ 1;
640 context
->SegSs
= seg
;
641 context
->Eip
+= prefixlen
+ len
+ 1;
644 context
->SegDs
= seg
;
645 context
->Eip
+= prefixlen
+ len
+ 1;
648 context
->SegFs
= seg
;
649 context
->Eip
+= prefixlen
+ len
+ 1;
652 context
->SegGs
= seg
;
653 context
->Eip
+= prefixlen
+ len
+ 1;
660 break; /* Unable to emulate it */
662 case 0xc4: /* les addr,reg */
663 case 0xc5: /* lds addr,reg */
664 if (INSTR_EmulateLDS( context
, instr
, long_op
,
665 long_addr
, segprefix
, &len
))
667 context
->Eip
+= prefixlen
+ len
;
670 break; /* Unable to emulate it */
672 case 0xcd: /* int <XX> */
675 ERR("int xx from 32-bit code is not supported.\n");
676 break; /* Unable to emulate it */
680 FARPROC16 addr
= INT_GetPMHandler( instr
[1] );
681 WORD
*stack
= get_stack( context
);
684 FIXME("no handler for interrupt %02x, ignoring it\n", instr
[1]);
685 context
->Eip
+= prefixlen
+ 2;
688 /* Push the flags and return address on the stack */
689 *(--stack
) = LOWORD(context
->EFlags
);
690 *(--stack
) = context
->SegCs
;
691 *(--stack
) = LOWORD(context
->Eip
) + prefixlen
+ 2;
692 add_stack(context
, -3 * sizeof(WORD
));
693 /* Jump to the interrupt handler */
694 context
->SegCs
= HIWORD(addr
);
695 context
->Eip
= LOWORD(addr
);
699 case 0xcf: /* iret */
702 DWORD
*stack
= get_stack( context
);
703 context
->Eip
= *stack
++;
704 context
->SegCs
= *stack
++;
705 context
->EFlags
= *stack
;
706 add_stack(context
, 3*sizeof(DWORD
)); /* Pop the return address and flags */
710 WORD
*stack
= get_stack( context
);
711 context
->Eip
= *stack
++;
712 context
->SegCs
= *stack
++;
713 SET_LOWORD(context
->EFlags
,*stack
);
714 add_stack(context
, 3*sizeof(WORD
)); /* Pop the return address and flags */
718 case 0xe4: /* inb al,XX */
719 SET_LOBYTE(context
->Eax
,INSTR_inport( instr
[1], 1, context
));
720 context
->Eip
+= prefixlen
+ 2;
723 case 0xe5: /* in (e)ax,XX */
725 context
->Eax
= INSTR_inport( instr
[1], 4, context
);
727 SET_LOWORD(context
->Eax
, INSTR_inport( instr
[1], 2, context
));
728 context
->Eip
+= prefixlen
+ 2;
731 case 0xe6: /* outb XX,al */
732 INSTR_outport( instr
[1], 1, LOBYTE(context
->Eax
), context
);
733 context
->Eip
+= prefixlen
+ 2;
736 case 0xe7: /* out XX,(e)ax */
738 INSTR_outport( instr
[1], 4, context
->Eax
, context
);
740 INSTR_outport( instr
[1], 2, LOWORD(context
->Eax
), context
);
741 context
->Eip
+= prefixlen
+ 2;
744 case 0xec: /* inb al,dx */
745 SET_LOBYTE(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 1, context
) );
746 context
->Eip
+= prefixlen
+ 1;
749 case 0xed: /* in (e)ax,dx */
751 context
->Eax
= INSTR_inport( LOWORD(context
->Edx
), 4, context
);
753 SET_LOWORD(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 2, context
));
754 context
->Eip
+= prefixlen
+ 1;
757 case 0xee: /* outb dx,al */
758 INSTR_outport( LOWORD(context
->Edx
), 1, LOBYTE(context
->Eax
), context
);
759 context
->Eip
+= prefixlen
+ 1;
762 case 0xef: /* out dx,(e)ax */
764 INSTR_outport( LOWORD(context
->Edx
), 4, context
->Eax
, context
);
766 INSTR_outport( LOWORD(context
->Edx
), 2, LOWORD(context
->Eax
), context
);
767 context
->Eip
+= prefixlen
+ 1;
770 case 0xfa: /* cli, ignored */
771 context
->Eip
+= prefixlen
+ 1;
774 case 0xfb: /* sti, ignored */
775 context
->Eip
+= prefixlen
+ 1;
780 /* Check for Win16 __GP handler */
781 gpHandler
= HasGPHandler16( MAKESEGPTR( context
->SegCs
, context
->Eip
) );
784 WORD
*stack
= get_stack( context
);
785 *--stack
= context
->SegCs
;
786 *--stack
= context
->Eip
;
787 add_stack(context
, -2*sizeof(WORD
));
789 context
->SegCs
= SELECTOROF( gpHandler
);
790 context
->Eip
= OFFSETOF( gpHandler
);
793 return FALSE
; /* Unable to emulate it */
796 #endif /* __i386__ */