2 * Emulation of privileged instructions
4 * Copyright 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/winuser16.h"
30 #include "selectors.h"
31 #include "wine/debug.h"
34 #include "wine/exception.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(int);
37 WINE_DECLARE_DEBUG_CHANNEL(io
);
41 /* macros to set parts of a DWORD */
42 #define SET_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
43 #define SET_LOBYTE(dw,val) ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
44 #define ADD_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
46 inline static void add_stack( CONTEXT86
*context
, int offset
)
48 if (ISV86(context
) || !IS_SELECTOR_32BIT(context
->SegSs
))
49 ADD_LOWORD( context
->Esp
, offset
);
51 context
->Esp
+= offset
;
54 inline static void *make_ptr( CONTEXT86
*context
, DWORD seg
, DWORD off
, int long_addr
)
56 if (ISV86(context
)) return PTR_REAL_TO_LIN( seg
, off
);
57 if (IS_SELECTOR_SYSTEM(seg
)) return (void *)off
;
58 if (!long_addr
) off
= LOWORD(off
);
59 return (char *) MapSL( MAKESEGPTR( seg
, 0 ) ) + off
;
62 inline static void *get_stack( CONTEXT86
*context
)
64 if (ISV86(context
)) return PTR_REAL_TO_LIN( context
->SegSs
, context
->Esp
);
65 return wine_ldt_get_ptr( context
->SegSs
, context
->Esp
);
69 /***********************************************************************
72 static DWORD CALLBACK
timer_thread( void *dummy
)
81 /***********************************************************************
82 * INSTR_ReplaceSelector
84 * Try to replace an invalid selector by a valid one.
85 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
86 * is the so called 'bimodal' selector 0x40, which points to the BIOS
87 * data segment. Used by (at least) Borland products (and programs compiled
88 * using Borland products).
90 * See Undocumented Windows, Chapter 5, __0040.
92 static BOOL
INSTR_ReplaceSelector( CONTEXT86
*context
, WORD
*sel
)
96 #if 0 /* hack until this is moved to kernel */
97 static WORD sys_timer
= 0;
99 sys_timer
= CreateSystemTimer( 55, DOSMEM_Tick
);
101 static HANDLE sys_thread
;
102 if (!sys_thread
) sys_thread
= CreateThread( NULL
, 0, timer_thread
, NULL
, 0, NULL
);
103 *sel
= DOSMEM_BiosDataSeg
;
106 return FALSE
; /* Can't replace selector, crashdump */
110 /***********************************************************************
111 * INSTR_GetOperandAddr
113 * Return the address of an instruction operand (from the mod/rm byte).
115 static BYTE
*INSTR_GetOperandAddr( CONTEXT86
*context
, BYTE
*instr
,
116 int long_addr
, int segprefix
, int *len
)
118 int mod
, rm
, base
, index
= 0, ss
= 0, seg
= 0, off
;
121 #define GET_VAL(val,type) \
122 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
125 GET_VAL( &mod
, BYTE
);
133 case 0: return (BYTE
*)&context
->Eax
;
134 case 1: return (BYTE
*)&context
->Ecx
;
135 case 2: return (BYTE
*)&context
->Edx
;
136 case 3: return (BYTE
*)&context
->Ebx
;
137 case 4: return (BYTE
*)&context
->Esp
;
138 case 5: return (BYTE
*)&context
->Ebp
;
139 case 6: return (BYTE
*)&context
->Esi
;
140 case 7: return (BYTE
*)&context
->Edi
;
149 GET_VAL( &sib
, BYTE
);
154 case 0: index
= context
->Eax
; break;
155 case 1: index
= context
->Ecx
; break;
156 case 2: index
= context
->Edx
; break;
157 case 3: index
= context
->Ebx
; break;
158 case 4: index
= 0; break;
159 case 5: index
= context
->Ebp
; break;
160 case 6: index
= context
->Esi
; break;
161 case 7: index
= context
->Edi
; break;
167 case 0: base
= context
->Eax
; seg
= context
->SegDs
; break;
168 case 1: base
= context
->Ecx
; seg
= context
->SegDs
; break;
169 case 2: base
= context
->Edx
; seg
= context
->SegDs
; break;
170 case 3: base
= context
->Ebx
; seg
= context
->SegDs
; break;
171 case 4: base
= context
->Esp
; seg
= context
->SegSs
; break;
172 case 5: base
= context
->Ebp
; seg
= context
->SegSs
; break;
173 case 6: base
= context
->Esi
; seg
= context
->SegDs
; break;
174 case 7: base
= context
->Edi
; seg
= context
->SegDs
; break;
179 if (rm
== 5) /* special case: ds:(disp32) */
181 GET_VAL( &base
, DWORD
);
182 seg
= context
->SegDs
;
186 case 1: /* 8-bit disp */
187 GET_VAL( &off
, BYTE
);
188 base
+= (signed char)off
;
191 case 2: /* 32-bit disp */
192 GET_VAL( &off
, DWORD
);
193 base
+= (signed long)off
;
197 else /* short address */
201 case 0: /* ds:(bx,si) */
202 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Esi
);
203 seg
= context
->SegDs
;
205 case 1: /* ds:(bx,di) */
206 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Edi
);
207 seg
= context
->SegDs
;
209 case 2: /* ss:(bp,si) */
210 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Esi
);
211 seg
= context
->SegSs
;
213 case 3: /* ss:(bp,di) */
214 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Edi
);
215 seg
= context
->SegSs
;
217 case 4: /* ds:(si) */
218 base
= LOWORD(context
->Esi
);
219 seg
= context
->SegDs
;
221 case 5: /* ds:(di) */
222 base
= LOWORD(context
->Edi
);
223 seg
= context
->SegDs
;
225 case 6: /* ss:(bp) */
226 base
= LOWORD(context
->Ebp
);
227 seg
= context
->SegSs
;
229 case 7: /* ds:(bx) */
230 base
= LOWORD(context
->Ebx
);
231 seg
= context
->SegDs
;
238 if (rm
== 6) /* special case: ds:(disp16) */
240 GET_VAL( &base
, WORD
);
241 seg
= context
->SegDs
;
245 case 1: /* 8-bit disp */
246 GET_VAL( &off
, BYTE
);
247 base
+= (signed char)off
;
250 case 2: /* 16-bit disp */
251 GET_VAL( &off
, WORD
);
252 base
+= (signed short)off
;
257 if (segprefix
!= -1) seg
= segprefix
;
259 /* Make sure the segment and offset are valid */
260 if (IS_SELECTOR_SYSTEM(seg
)) return (BYTE
*)(base
+ (index
<< ss
));
261 if ((seg
& 7) != 7) return NULL
;
262 wine_ldt_get_entry( seg
, &entry
);
263 if (wine_ldt_is_empty( &entry
)) return NULL
;
264 if (wine_ldt_get_limit(&entry
) < (base
+ (index
<< ss
))) return NULL
;
265 return (char *)wine_ldt_get_base(&entry
) + base
+ (index
<< ss
);
270 /***********************************************************************
273 * Emulate the LDS (and LES,LFS,etc.) instruction.
275 static BOOL
INSTR_EmulateLDS( CONTEXT86
*context
, BYTE
*instr
, int long_op
,
276 int long_addr
, int segprefix
, int *len
)
279 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
280 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
281 long_addr
, segprefix
, len
);
283 return FALSE
; /* Unable to emulate it */
284 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
286 if (!INSTR_ReplaceSelector( context
, &seg
))
287 return FALSE
; /* Unable to emulate it */
289 /* Now store the offset in the correct register */
291 switch((*regmodrm
>> 3) & 7)
294 if (long_op
) context
->Eax
= *(DWORD
*)addr
;
295 else SET_LOWORD(context
->Eax
,*(WORD
*)addr
);
298 if (long_op
) context
->Ecx
= *(DWORD
*)addr
;
299 else SET_LOWORD(context
->Ecx
,*(WORD
*)addr
);
302 if (long_op
) context
->Edx
= *(DWORD
*)addr
;
303 else SET_LOWORD(context
->Edx
,*(WORD
*)addr
);
306 if (long_op
) context
->Ebx
= *(DWORD
*)addr
;
307 else SET_LOWORD(context
->Ebx
,*(WORD
*)addr
);
310 if (long_op
) context
->Esp
= *(DWORD
*)addr
;
311 else SET_LOWORD(context
->Esp
,*(WORD
*)addr
);
314 if (long_op
) context
->Ebp
= *(DWORD
*)addr
;
315 else SET_LOWORD(context
->Ebp
,*(WORD
*)addr
);
318 if (long_op
) context
->Esi
= *(DWORD
*)addr
;
319 else SET_LOWORD(context
->Esi
,*(WORD
*)addr
);
322 if (long_op
) context
->Edi
= *(DWORD
*)addr
;
323 else SET_LOWORD(context
->Edi
,*(WORD
*)addr
);
327 /* Store the correct segment in the segment register */
331 case 0xc4: context
->SegEs
= seg
; break; /* les */
332 case 0xc5: context
->SegDs
= seg
; break; /* lds */
333 case 0x0f: switch(instr
[1])
335 case 0xb2: context
->SegSs
= seg
; break; /* lss */
336 case 0xb4: context
->SegFs
= seg
; break; /* lfs */
337 case 0xb5: context
->SegGs
= seg
; break; /* lgs */
342 /* Add the opcode size to the total length */
344 *len
+= 1 + (*instr
== 0x0f);
348 /***********************************************************************
351 * input on a I/O port
353 static DWORD
INSTR_inport( WORD port
, int size
, CONTEXT86
*context
)
355 DWORD res
= IO_inport( port
, size
);
361 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port
, LOBYTE(res
),
362 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
365 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port
, LOWORD(res
),
366 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
369 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port
, res
,
370 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
378 /***********************************************************************
381 * output on a I/O port
383 static void INSTR_outport( WORD port
, int size
, DWORD val
, CONTEXT86
*context
)
385 IO_outport( port
, size
, val
);
391 DPRINTF("0x%x > %02x @ %04x:%04x\n", port
, LOBYTE(val
),
392 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
395 DPRINTF("0x%x > %04x @ %04x:%04x\n", port
, LOWORD(val
),
396 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
399 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port
, val
,
400 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
407 /***********************************************************************
408 * INSTR_EmulateInstruction
410 * Emulate a privileged instruction.
411 * Returns exception code, or 0 if emulation successful.
413 DWORD
INSTR_EmulateInstruction( CONTEXT86
*context
)
415 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
417 DWORD ret
= EXCEPTION_PRIV_INSTRUCTION
;
419 long_op
= long_addr
= (!ISV86(context
) && IS_SELECTOR_32BIT(context
->SegCs
));
420 instr
= make_ptr( context
, context
->SegCs
, context
->Eip
, TRUE
);
421 if (!instr
) return ret
;
423 /* First handle any possible prefix */
425 segprefix
= -1; /* no prefix */
434 segprefix
= context
->SegCs
;
437 segprefix
= context
->SegSs
;
440 segprefix
= context
->SegDs
;
443 segprefix
= context
->SegEs
;
446 segprefix
= context
->SegFs
;
449 segprefix
= context
->SegGs
;
452 long_op
= !long_op
; /* opcode size prefix */
455 long_addr
= !long_addr
; /* addr size prefix */
457 case 0xf0: /* lock */
459 case 0xf2: /* repne */
462 case 0xf3: /* repe */
466 prefix
= 0; /* no more prefixes */
476 /* Now look at the actual instruction */
480 case 0x07: /* pop es */
481 case 0x17: /* pop ss */
482 case 0x1f: /* pop ds */
484 WORD seg
= *(WORD
*)get_stack( context
);
485 if (INSTR_ReplaceSelector( context
, &seg
))
489 case 0x07: context
->SegEs
= seg
; break;
490 case 0x17: context
->SegSs
= seg
; break;
491 case 0x1f: context
->SegDs
= seg
; break;
493 add_stack(context
, long_op
? 4 : 2);
494 context
->Eip
+= prefixlen
+ 1;
498 break; /* Unable to emulate it */
500 case 0x0f: /* extended instruction */
503 case 0x22: /* mov eax, crX */
506 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
507 context
->Eip
,context
->Eax
);
508 context
->Eip
+= prefixlen
+3;
511 break; /*fallthrough to bad instruction handling */
513 break; /*fallthrough to bad instruction handling */
514 case 0x20: /* mov crX, eax */
516 case 0xe0: /* mov cr4, eax */
517 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
518 * bit 0: VME Virtual Mode Exception ?
519 * bit 1: PVI Protected mode Virtual Interrupt
520 * bit 2: TSD Timestamp disable
521 * bit 3: DE Debugging extensions
522 * bit 4: PSE Page size extensions
523 * bit 5: PAE Physical address extension
524 * bit 6: MCE Machine check enable
525 * bit 7: PGE Enable global pages
526 * bit 8: PCE Enable performance counters at IPL3
528 ERR("mov cr4,eax at 0x%08lx\n",context
->Eip
);
530 context
->Eip
+= prefixlen
+3;
532 case 0xc0: /* mov cr0, eax */
533 ERR("mov cr0,eax at 0x%08lx\n",context
->Eip
);
534 context
->Eax
= 0x10; /* FIXME: set more bits ? */
535 context
->Eip
+= prefixlen
+3;
537 default: /* fallthrough to illegal instruction */
540 /* fallthrough to illegal instruction */
542 case 0xa1: /* pop fs */
544 WORD seg
= *(WORD
*)get_stack( context
);
545 if (INSTR_ReplaceSelector( context
, &seg
))
547 context
->SegFs
= seg
;
548 add_stack(context
, long_op
? 4 : 2);
549 context
->Eip
+= prefixlen
+ 2;
554 case 0xa9: /* pop gs */
556 WORD seg
= *(WORD
*)get_stack( context
);
557 if (INSTR_ReplaceSelector( context
, &seg
))
559 context
->SegGs
= seg
;
560 add_stack(context
, long_op
? 4 : 2);
561 context
->Eip
+= prefixlen
+ 2;
566 case 0xb2: /* lss addr,reg */
567 case 0xb4: /* lfs addr,reg */
568 case 0xb5: /* lgs addr,reg */
569 if (INSTR_EmulateLDS( context
, instr
, long_op
,
570 long_addr
, segprefix
, &len
))
572 context
->Eip
+= prefixlen
+ len
;
577 break; /* Unable to emulate it */
579 case 0x6c: /* insb */
580 case 0x6d: /* insw/d */
581 case 0x6e: /* outsb */
582 case 0x6f: /* outsw/d */
584 int typ
= *instr
; /* Just in case it's overwritten. */
585 int outp
= (typ
>= 0x6e);
586 unsigned long count
= repX
?
587 (long_addr
? context
->Ecx
: LOWORD(context
->Ecx
)) : 1;
588 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
589 int step
= (context
->EFlags
& 0x400) ? -opsize
: +opsize
;
590 int seg
= outp
? context
->SegDs
: context
->SegEs
; /* FIXME: is this right? */
593 /* FIXME: Check segment readable. */
596 /* FIXME: Check segment writeable. */
601 if (long_addr
) context
->Ecx
= 0;
602 else SET_LOWORD(context
->Ecx
,0);
608 WORD dx
= LOWORD(context
->Edx
);
611 data
= make_ptr( context
, seg
, context
->Esi
, long_addr
);
612 if (long_addr
) context
->Esi
+= step
;
613 else ADD_LOWORD(context
->Esi
,step
);
617 data
= make_ptr( context
, seg
, context
->Edi
, long_addr
);
618 if (long_addr
) context
->Edi
+= step
;
619 else ADD_LOWORD(context
->Edi
,step
);
625 *(BYTE
*)data
= INSTR_inport( dx
, 1, context
);
629 *(DWORD
*)data
= INSTR_inport( dx
, 4, context
);
631 *(WORD
*)data
= INSTR_inport( dx
, 2, context
);
634 INSTR_outport( dx
, 1, *(BYTE
*)data
, context
);
638 INSTR_outport( dx
, 4, *(DWORD
*)data
, context
);
640 INSTR_outport( dx
, 2, *(WORD
*)data
, context
);
644 context
->Eip
+= prefixlen
+ 1;
648 case 0x8e: /* mov XX,segment_reg */
651 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
652 long_addr
, segprefix
, &len
);
654 break; /* Unable to emulate it */
656 if (!INSTR_ReplaceSelector( context
, &seg
))
657 break; /* Unable to emulate it */
659 switch((instr
[1] >> 3) & 7)
662 context
->SegEs
= seg
;
663 context
->Eip
+= prefixlen
+ len
+ 1;
668 context
->SegSs
= seg
;
669 context
->Eip
+= prefixlen
+ len
+ 1;
672 context
->SegDs
= seg
;
673 context
->Eip
+= prefixlen
+ len
+ 1;
676 context
->SegFs
= seg
;
677 context
->Eip
+= prefixlen
+ len
+ 1;
680 context
->SegGs
= seg
;
681 context
->Eip
+= prefixlen
+ len
+ 1;
688 break; /* Unable to emulate it */
690 case 0xc4: /* les addr,reg */
691 case 0xc5: /* lds addr,reg */
692 if (INSTR_EmulateLDS( context
, instr
, long_op
,
693 long_addr
, segprefix
, &len
))
695 context
->Eip
+= prefixlen
+ len
;
698 break; /* Unable to emulate it */
700 case 0xcd: /* int <XX> */
701 if (IS_SELECTOR_SYSTEM(context
->SegCs
))
703 /* Win32 applications cannot use interrupts */
704 ret
= EXCEPTION_ACCESS_VIOLATION
;
707 else if (!Dosvm
.EmulateInterruptPM
&& !DPMI_LoadDosSystem())
709 ERR("could not initialize interrupt handling\n");
713 context
->Eip
+= prefixlen
+ 2;
714 Dosvm
.EmulateInterruptPM( context
, instr
[1] );
717 break; /* Unable to emulate it */
719 case 0xcf: /* iret */
722 DWORD
*stack
= get_stack( context
);
723 context
->Eip
= *stack
++;
724 context
->SegCs
= *stack
++;
725 context
->EFlags
= *stack
;
726 add_stack(context
, 3*sizeof(DWORD
)); /* Pop the return address and flags */
730 WORD
*stack
= get_stack( context
);
731 context
->Eip
= *stack
++;
732 context
->SegCs
= *stack
++;
733 SET_LOWORD(context
->EFlags
,*stack
);
734 add_stack(context
, 3*sizeof(WORD
)); /* Pop the return address and flags */
738 case 0xe4: /* inb al,XX */
739 SET_LOBYTE(context
->Eax
,INSTR_inport( instr
[1], 1, context
));
740 context
->Eip
+= prefixlen
+ 2;
743 case 0xe5: /* in (e)ax,XX */
745 context
->Eax
= INSTR_inport( instr
[1], 4, context
);
747 SET_LOWORD(context
->Eax
, INSTR_inport( instr
[1], 2, context
));
748 context
->Eip
+= prefixlen
+ 2;
751 case 0xe6: /* outb XX,al */
752 INSTR_outport( instr
[1], 1, LOBYTE(context
->Eax
), context
);
753 context
->Eip
+= prefixlen
+ 2;
756 case 0xe7: /* out XX,(e)ax */
758 INSTR_outport( instr
[1], 4, context
->Eax
, context
);
760 INSTR_outport( instr
[1], 2, LOWORD(context
->Eax
), context
);
761 context
->Eip
+= prefixlen
+ 2;
764 case 0xec: /* inb al,dx */
765 SET_LOBYTE(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 1, context
) );
766 context
->Eip
+= prefixlen
+ 1;
769 case 0xed: /* in (e)ax,dx */
771 context
->Eax
= INSTR_inport( LOWORD(context
->Edx
), 4, context
);
773 SET_LOWORD(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 2, context
));
774 context
->Eip
+= prefixlen
+ 1;
777 case 0xee: /* outb dx,al */
778 INSTR_outport( LOWORD(context
->Edx
), 1, LOBYTE(context
->Eax
), context
);
779 context
->Eip
+= prefixlen
+ 1;
782 case 0xef: /* out dx,(e)ax */
784 INSTR_outport( LOWORD(context
->Edx
), 4, context
->Eax
, context
);
786 INSTR_outport( LOWORD(context
->Edx
), 2, LOWORD(context
->Eax
), context
);
787 context
->Eip
+= prefixlen
+ 1;
791 NtCurrentTeb()->dpmi_vif
= 0;
792 context
->Eip
+= prefixlen
+ 1;
796 NtCurrentTeb()->dpmi_vif
= 1;
797 context
->Eip
+= prefixlen
+ 1;
798 if (NtCurrentTeb()->vm86_pending
)
800 NtCurrentTeb()->vm86_pending
= 0;
801 return EXCEPTION_VM86_STI
;
805 return ret
; /* Unable to emulate it */
808 #endif /* __i386__ */