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
23 #include "wine/winuser16.h"
26 #include "selectors.h"
27 #include "wine/debug.h"
30 #include "wine/exception.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(int);
33 WINE_DECLARE_DEBUG_CHANNEL(io
);
37 /* macros to set parts of a DWORD */
38 #define SET_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
39 #define SET_LOBYTE(dw,val) ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
40 #define ADD_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
42 inline static void add_stack( CONTEXT86
*context
, int offset
)
44 if (ISV86(context
) || !IS_SELECTOR_32BIT(context
->SegSs
))
45 ADD_LOWORD( context
->Esp
, offset
);
47 context
->Esp
+= offset
;
50 inline static void *make_ptr( CONTEXT86
*context
, DWORD seg
, DWORD off
, int long_addr
)
52 if (ISV86(context
)) return PTR_REAL_TO_LIN( seg
, off
);
53 if (IS_SELECTOR_SYSTEM(seg
)) return (void *)off
;
54 if (!long_addr
) off
= LOWORD(off
);
55 return (char *) MapSL( MAKESEGPTR( seg
, 0 ) ) + off
;
58 inline static void *get_stack( CONTEXT86
*context
)
60 if (ISV86(context
)) return PTR_REAL_TO_LIN( context
->SegSs
, context
->Esp
);
61 return wine_ldt_get_ptr( context
->SegSs
, context
->Esp
);
65 /***********************************************************************
68 static DWORD CALLBACK
timer_thread( void *dummy
)
77 /***********************************************************************
78 * INSTR_ReplaceSelector
80 * Try to replace an invalid selector by a valid one.
81 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
82 * is the so called 'bimodal' selector 0x40, which points to the BIOS
83 * data segment. Used by (at least) Borland products (and programs compiled
84 * using Borland products).
86 * See Undocumented Windows, Chapter 5, __0040.
88 static BOOL
INSTR_ReplaceSelector( CONTEXT86
*context
, WORD
*sel
)
90 extern char Call16_Start
, Call16_End
;
92 if (IS_SELECTOR_SYSTEM(context
->SegCs
))
93 if ( (char *)context
->Eip
>= &Call16_Start
94 && (char *)context
->Eip
< &Call16_End
)
96 /* Saved selector may have become invalid when the relay code */
97 /* tries to restore it. We simply clear it. */
104 #if 0 /* hack until this is moved to kernel */
105 static WORD sys_timer
= 0;
107 sys_timer
= CreateSystemTimer( 55, DOSMEM_Tick
);
109 static HANDLE sys_thread
;
110 if (!sys_thread
) sys_thread
= CreateThread( NULL
, 0, timer_thread
, NULL
, 0, NULL
);
111 *sel
= DOSMEM_BiosDataSeg
;
114 return FALSE
; /* Can't replace selector, crashdump */
118 /***********************************************************************
119 * INSTR_GetOperandAddr
121 * Return the address of an instruction operand (from the mod/rm byte).
123 static BYTE
*INSTR_GetOperandAddr( CONTEXT86
*context
, BYTE
*instr
,
124 int long_addr
, int segprefix
, int *len
)
126 int mod
, rm
, base
, index
= 0, ss
= 0, seg
= 0, off
;
129 #define GET_VAL(val,type) \
130 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
133 GET_VAL( &mod
, BYTE
);
141 case 0: return (BYTE
*)&context
->Eax
;
142 case 1: return (BYTE
*)&context
->Ecx
;
143 case 2: return (BYTE
*)&context
->Edx
;
144 case 3: return (BYTE
*)&context
->Ebx
;
145 case 4: return (BYTE
*)&context
->Esp
;
146 case 5: return (BYTE
*)&context
->Ebp
;
147 case 6: return (BYTE
*)&context
->Esi
;
148 case 7: return (BYTE
*)&context
->Edi
;
157 GET_VAL( &sib
, BYTE
);
162 case 0: index
= context
->Eax
; break;
163 case 1: index
= context
->Ecx
; break;
164 case 2: index
= context
->Edx
; break;
165 case 3: index
= context
->Ebx
; break;
166 case 4: index
= 0; break;
167 case 5: index
= context
->Ebp
; break;
168 case 6: index
= context
->Esi
; break;
169 case 7: index
= context
->Edi
; break;
175 case 0: base
= context
->Eax
; seg
= context
->SegDs
; break;
176 case 1: base
= context
->Ecx
; seg
= context
->SegDs
; break;
177 case 2: base
= context
->Edx
; seg
= context
->SegDs
; break;
178 case 3: base
= context
->Ebx
; seg
= context
->SegDs
; break;
179 case 4: base
= context
->Esp
; seg
= context
->SegSs
; break;
180 case 5: base
= context
->Ebp
; seg
= context
->SegSs
; break;
181 case 6: base
= context
->Esi
; seg
= context
->SegDs
; break;
182 case 7: base
= context
->Edi
; seg
= context
->SegDs
; break;
187 if (rm
== 5) /* special case: ds:(disp32) */
189 GET_VAL( &base
, DWORD
);
190 seg
= context
->SegDs
;
194 case 1: /* 8-bit disp */
195 GET_VAL( &off
, BYTE
);
196 base
+= (signed char)off
;
199 case 2: /* 32-bit disp */
200 GET_VAL( &off
, DWORD
);
201 base
+= (signed long)off
;
205 else /* short address */
209 case 0: /* ds:(bx,si) */
210 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Esi
);
211 seg
= context
->SegDs
;
213 case 1: /* ds:(bx,di) */
214 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Edi
);
215 seg
= context
->SegDs
;
217 case 2: /* ss:(bp,si) */
218 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Esi
);
219 seg
= context
->SegSs
;
221 case 3: /* ss:(bp,di) */
222 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Edi
);
223 seg
= context
->SegSs
;
225 case 4: /* ds:(si) */
226 base
= LOWORD(context
->Esi
);
227 seg
= context
->SegDs
;
229 case 5: /* ds:(di) */
230 base
= LOWORD(context
->Edi
);
231 seg
= context
->SegDs
;
233 case 6: /* ss:(bp) */
234 base
= LOWORD(context
->Ebp
);
235 seg
= context
->SegSs
;
237 case 7: /* ds:(bx) */
238 base
= LOWORD(context
->Ebx
);
239 seg
= context
->SegDs
;
246 if (rm
== 6) /* special case: ds:(disp16) */
248 GET_VAL( &base
, WORD
);
249 seg
= context
->SegDs
;
253 case 1: /* 8-bit disp */
254 GET_VAL( &off
, BYTE
);
255 base
+= (signed char)off
;
258 case 2: /* 16-bit disp */
259 GET_VAL( &off
, WORD
);
260 base
+= (signed short)off
;
265 if (segprefix
!= -1) seg
= segprefix
;
267 /* Make sure the segment and offset are valid */
268 if (IS_SELECTOR_SYSTEM(seg
)) return (BYTE
*)(base
+ (index
<< ss
));
269 if ((seg
& 7) != 7) return NULL
;
270 wine_ldt_get_entry( seg
, &entry
);
271 if (wine_ldt_is_empty( &entry
)) return NULL
;
272 if (wine_ldt_get_limit(&entry
) < (base
+ (index
<< ss
))) return NULL
;
273 return (char *)wine_ldt_get_base(&entry
) + base
+ (index
<< ss
);
278 /***********************************************************************
281 * Emulate the LDS (and LES,LFS,etc.) instruction.
283 static BOOL
INSTR_EmulateLDS( CONTEXT86
*context
, BYTE
*instr
, int long_op
,
284 int long_addr
, int segprefix
, int *len
)
287 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
288 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
289 long_addr
, segprefix
, len
);
291 return FALSE
; /* Unable to emulate it */
292 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
294 if (!INSTR_ReplaceSelector( context
, &seg
))
295 return FALSE
; /* Unable to emulate it */
297 /* Now store the offset in the correct register */
299 switch((*regmodrm
>> 3) & 7)
302 if (long_op
) context
->Eax
= *(DWORD
*)addr
;
303 else SET_LOWORD(context
->Eax
,*(WORD
*)addr
);
306 if (long_op
) context
->Ecx
= *(DWORD
*)addr
;
307 else SET_LOWORD(context
->Ecx
,*(WORD
*)addr
);
310 if (long_op
) context
->Edx
= *(DWORD
*)addr
;
311 else SET_LOWORD(context
->Edx
,*(WORD
*)addr
);
314 if (long_op
) context
->Ebx
= *(DWORD
*)addr
;
315 else SET_LOWORD(context
->Ebx
,*(WORD
*)addr
);
318 if (long_op
) context
->Esp
= *(DWORD
*)addr
;
319 else SET_LOWORD(context
->Esp
,*(WORD
*)addr
);
322 if (long_op
) context
->Ebp
= *(DWORD
*)addr
;
323 else SET_LOWORD(context
->Ebp
,*(WORD
*)addr
);
326 if (long_op
) context
->Esi
= *(DWORD
*)addr
;
327 else SET_LOWORD(context
->Esi
,*(WORD
*)addr
);
330 if (long_op
) context
->Edi
= *(DWORD
*)addr
;
331 else SET_LOWORD(context
->Edi
,*(WORD
*)addr
);
335 /* Store the correct segment in the segment register */
339 case 0xc4: context
->SegEs
= seg
; break; /* les */
340 case 0xc5: context
->SegDs
= seg
; break; /* lds */
341 case 0x0f: switch(instr
[1])
343 case 0xb2: context
->SegSs
= seg
; break; /* lss */
344 case 0xb4: context
->SegFs
= seg
; break; /* lfs */
345 case 0xb5: context
->SegGs
= seg
; break; /* lgs */
350 /* Add the opcode size to the total length */
352 *len
+= 1 + (*instr
== 0x0f);
356 /***********************************************************************
359 * input on a I/O port
361 static DWORD
INSTR_inport( WORD port
, int size
, CONTEXT86
*context
)
363 DWORD res
= IO_inport( port
, size
);
369 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port
, LOBYTE(res
),
370 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
373 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port
, LOWORD(res
),
374 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
377 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port
, res
,
378 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
386 /***********************************************************************
389 * output on a I/O port
391 static void INSTR_outport( WORD port
, int size
, DWORD val
, CONTEXT86
*context
)
393 IO_outport( port
, size
, val
);
399 DPRINTF("0x%x > %02x @ %04x:%04x\n", port
, LOBYTE(val
),
400 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
403 DPRINTF("0x%x > %04x @ %04x:%04x\n", port
, LOWORD(val
),
404 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
407 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port
, val
,
408 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
415 /***********************************************************************
416 * INSTR_EmulateInstruction
418 * Emulate a privileged instruction.
419 * Returns exception code, or 0 if emulation successful.
421 DWORD
INSTR_EmulateInstruction( CONTEXT86
*context
)
423 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
425 DWORD ret
= EXCEPTION_PRIV_INSTRUCTION
;
427 long_op
= long_addr
= (!ISV86(context
) && IS_SELECTOR_32BIT(context
->SegCs
));
428 instr
= make_ptr( context
, context
->SegCs
, context
->Eip
, TRUE
);
429 if (!instr
) return ret
;
431 /* First handle any possible prefix */
433 segprefix
= -1; /* no prefix */
442 segprefix
= context
->SegCs
;
445 segprefix
= context
->SegSs
;
448 segprefix
= context
->SegDs
;
451 segprefix
= context
->SegEs
;
454 segprefix
= context
->SegFs
;
457 segprefix
= context
->SegGs
;
460 long_op
= !long_op
; /* opcode size prefix */
463 long_addr
= !long_addr
; /* addr size prefix */
465 case 0xf0: /* lock */
467 case 0xf2: /* repne */
470 case 0xf3: /* repe */
474 prefix
= 0; /* no more prefixes */
484 /* Now look at the actual instruction */
488 case 0x07: /* pop es */
489 case 0x17: /* pop ss */
490 case 0x1f: /* pop ds */
492 WORD seg
= *(WORD
*)get_stack( context
);
493 if (INSTR_ReplaceSelector( context
, &seg
))
497 case 0x07: context
->SegEs
= seg
; break;
498 case 0x17: context
->SegSs
= seg
; break;
499 case 0x1f: context
->SegDs
= seg
; break;
501 add_stack(context
, long_op
? 4 : 2);
502 context
->Eip
+= prefixlen
+ 1;
506 break; /* Unable to emulate it */
508 case 0x0f: /* extended instruction */
511 case 0x22: /* mov eax, crX */
514 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
515 context
->Eip
,context
->Eax
);
516 context
->Eip
+= prefixlen
+3;
519 break; /*fallthrough to bad instruction handling */
521 break; /*fallthrough to bad instruction handling */
522 case 0x20: /* mov crX, eax */
524 case 0xe0: /* mov cr4, eax */
525 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
526 * bit 0: VME Virtual Mode Exception ?
527 * bit 1: PVI Protected mode Virtual Interrupt
528 * bit 2: TSD Timestamp disable
529 * bit 3: DE Debugging extensions
530 * bit 4: PSE Page size extensions
531 * bit 5: PAE Physical address extension
532 * bit 6: MCE Machine check enable
533 * bit 7: PGE Enable global pages
534 * bit 8: PCE Enable performance counters at IPL3
536 ERR("mov cr4,eax at 0x%08lx\n",context
->Eip
);
538 context
->Eip
+= prefixlen
+3;
540 case 0xc0: /* mov cr0, eax */
541 ERR("mov cr0,eax at 0x%08lx\n",context
->Eip
);
542 context
->Eax
= 0x10; /* FIXME: set more bits ? */
543 context
->Eip
+= prefixlen
+3;
545 default: /* fallthrough to illegal instruction */
548 /* fallthrough to illegal instruction */
550 case 0xa1: /* pop fs */
552 WORD seg
= *(WORD
*)get_stack( context
);
553 if (INSTR_ReplaceSelector( context
, &seg
))
555 context
->SegFs
= seg
;
556 add_stack(context
, long_op
? 4 : 2);
557 context
->Eip
+= prefixlen
+ 2;
562 case 0xa9: /* pop gs */
564 WORD seg
= *(WORD
*)get_stack( context
);
565 if (INSTR_ReplaceSelector( context
, &seg
))
567 context
->SegGs
= seg
;
568 add_stack(context
, long_op
? 4 : 2);
569 context
->Eip
+= prefixlen
+ 2;
574 case 0xb2: /* lss addr,reg */
575 case 0xb4: /* lfs addr,reg */
576 case 0xb5: /* lgs addr,reg */
577 if (INSTR_EmulateLDS( context
, instr
, long_op
,
578 long_addr
, segprefix
, &len
))
580 context
->Eip
+= prefixlen
+ len
;
585 break; /* Unable to emulate it */
587 case 0x6c: /* insb */
588 case 0x6d: /* insw/d */
589 case 0x6e: /* outsb */
590 case 0x6f: /* outsw/d */
592 int typ
= *instr
; /* Just in case it's overwritten. */
593 int outp
= (typ
>= 0x6e);
594 unsigned long count
= repX
?
595 (long_addr
? context
->Ecx
: LOWORD(context
->Ecx
)) : 1;
596 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
597 int step
= (context
->EFlags
& 0x400) ? -opsize
: +opsize
;
598 int seg
= outp
? context
->SegDs
: context
->SegEs
; /* FIXME: is this right? */
601 /* FIXME: Check segment readable. */
604 /* FIXME: Check segment writeable. */
609 if (long_addr
) context
->Ecx
= 0;
610 else SET_LOWORD(context
->Ecx
,0);
616 WORD dx
= LOWORD(context
->Edx
);
619 data
= make_ptr( context
, seg
, context
->Esi
, long_addr
);
620 if (long_addr
) context
->Esi
+= step
;
621 else ADD_LOWORD(context
->Esi
,step
);
625 data
= make_ptr( context
, seg
, context
->Edi
, long_addr
);
626 if (long_addr
) context
->Edi
+= step
;
627 else ADD_LOWORD(context
->Edi
,step
);
633 *(BYTE
*)data
= INSTR_inport( dx
, 1, context
);
637 *(DWORD
*)data
= INSTR_inport( dx
, 4, context
);
639 *(WORD
*)data
= INSTR_inport( dx
, 2, context
);
642 INSTR_outport( dx
, 1, *(BYTE
*)data
, context
);
646 INSTR_outport( dx
, 4, *(DWORD
*)data
, context
);
648 INSTR_outport( dx
, 2, *(WORD
*)data
, context
);
652 context
->Eip
+= prefixlen
+ 1;
656 case 0x8e: /* mov XX,segment_reg */
659 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
660 long_addr
, segprefix
, &len
);
662 break; /* Unable to emulate it */
664 if (!INSTR_ReplaceSelector( context
, &seg
))
665 break; /* Unable to emulate it */
667 switch((instr
[1] >> 3) & 7)
670 context
->SegEs
= seg
;
671 context
->Eip
+= prefixlen
+ len
+ 1;
676 context
->SegSs
= seg
;
677 context
->Eip
+= prefixlen
+ len
+ 1;
680 context
->SegDs
= seg
;
681 context
->Eip
+= prefixlen
+ len
+ 1;
684 context
->SegFs
= seg
;
685 context
->Eip
+= prefixlen
+ len
+ 1;
688 context
->SegGs
= seg
;
689 context
->Eip
+= prefixlen
+ len
+ 1;
696 break; /* Unable to emulate it */
698 case 0xc4: /* les addr,reg */
699 case 0xc5: /* lds addr,reg */
700 if (INSTR_EmulateLDS( context
, instr
, long_op
,
701 long_addr
, segprefix
, &len
))
703 context
->Eip
+= prefixlen
+ len
;
706 break; /* Unable to emulate it */
708 case 0xcd: /* int <XX> */
709 if (IS_SELECTOR_SYSTEM(context
->SegCs
))
711 /* Win32 applications cannot use interrupts */
712 ret
= EXCEPTION_ACCESS_VIOLATION
;
715 else if (!Dosvm
.EmulateInterruptPM
&& !DPMI_LoadDosSystem())
717 ERR("could not initialize interrupt handling\n");
721 context
->Eip
+= prefixlen
+ 2;
722 Dosvm
.EmulateInterruptPM( context
, instr
[1] );
725 break; /* Unable to emulate it */
727 case 0xcf: /* iret */
730 DWORD
*stack
= get_stack( context
);
731 context
->Eip
= *stack
++;
732 context
->SegCs
= *stack
++;
733 context
->EFlags
= *stack
;
734 add_stack(context
, 3*sizeof(DWORD
)); /* Pop the return address and flags */
738 WORD
*stack
= get_stack( context
);
739 context
->Eip
= *stack
++;
740 context
->SegCs
= *stack
++;
741 SET_LOWORD(context
->EFlags
,*stack
);
742 add_stack(context
, 3*sizeof(WORD
)); /* Pop the return address and flags */
746 case 0xe4: /* inb al,XX */
747 SET_LOBYTE(context
->Eax
,INSTR_inport( instr
[1], 1, context
));
748 context
->Eip
+= prefixlen
+ 2;
751 case 0xe5: /* in (e)ax,XX */
753 context
->Eax
= INSTR_inport( instr
[1], 4, context
);
755 SET_LOWORD(context
->Eax
, INSTR_inport( instr
[1], 2, context
));
756 context
->Eip
+= prefixlen
+ 2;
759 case 0xe6: /* outb XX,al */
760 INSTR_outport( instr
[1], 1, LOBYTE(context
->Eax
), context
);
761 context
->Eip
+= prefixlen
+ 2;
764 case 0xe7: /* out XX,(e)ax */
766 INSTR_outport( instr
[1], 4, context
->Eax
, context
);
768 INSTR_outport( instr
[1], 2, LOWORD(context
->Eax
), context
);
769 context
->Eip
+= prefixlen
+ 2;
772 case 0xec: /* inb al,dx */
773 SET_LOBYTE(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 1, context
) );
774 context
->Eip
+= prefixlen
+ 1;
777 case 0xed: /* in (e)ax,dx */
779 context
->Eax
= INSTR_inport( LOWORD(context
->Edx
), 4, context
);
781 SET_LOWORD(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 2, context
));
782 context
->Eip
+= prefixlen
+ 1;
785 case 0xee: /* outb dx,al */
786 INSTR_outport( LOWORD(context
->Edx
), 1, LOBYTE(context
->Eax
), context
);
787 context
->Eip
+= prefixlen
+ 1;
790 case 0xef: /* out dx,(e)ax */
792 INSTR_outport( LOWORD(context
->Edx
), 4, context
->Eax
, context
);
794 INSTR_outport( LOWORD(context
->Edx
), 2, LOWORD(context
->Eax
), context
);
795 context
->Eip
+= prefixlen
+ 1;
799 NtCurrentTeb()->dpmi_vif
= 0;
800 context
->Eip
+= prefixlen
+ 1;
804 NtCurrentTeb()->dpmi_vif
= 1;
805 context
->Eip
+= prefixlen
+ 1;
806 if (NtCurrentTeb()->vm86_pending
)
808 NtCurrentTeb()->vm86_pending
= 0;
809 return EXCEPTION_VM86_STI
;
813 return ret
; /* Unable to emulate it */
816 #endif /* __i386__ */