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 WINE_DEFAULT_DEBUG_CHANNEL(int);
31 WINE_DECLARE_DEBUG_CHANNEL(io
);
35 /* macros to set parts of a DWORD */
36 #define SET_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
37 #define SET_LOBYTE(dw,val) ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
38 #define ADD_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
40 inline static void add_stack( CONTEXT86
*context
, int offset
)
42 if (ISV86(context
) || !IS_SELECTOR_32BIT(context
->SegSs
))
43 ADD_LOWORD( context
->Esp
, offset
);
45 context
->Esp
+= offset
;
48 inline static void *make_ptr( CONTEXT86
*context
, DWORD seg
, DWORD off
, int long_addr
)
50 if (ISV86(context
)) return PTR_REAL_TO_LIN( seg
, off
);
51 if (IS_SELECTOR_SYSTEM(seg
)) return (void *)off
;
52 if (!long_addr
) off
= LOWORD(off
);
53 return (char *) MapSL( MAKESEGPTR( seg
, 0 ) ) + off
;
56 inline static void *get_stack( CONTEXT86
*context
)
58 if (ISV86(context
)) return PTR_REAL_TO_LIN( context
->SegSs
, context
->Esp
);
59 return wine_ldt_get_ptr( context
->SegSs
, context
->Esp
);
63 /***********************************************************************
66 static DWORD CALLBACK
timer_thread( void *dummy
)
75 /***********************************************************************
76 * INSTR_ReplaceSelector
78 * Try to replace an invalid selector by a valid one.
79 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
80 * is the so called 'bimodal' selector 0x40, which points to the BIOS
81 * data segment. Used by (at least) Borland products (and programs compiled
82 * using Borland products).
84 * See Undocumented Windows, Chapter 5, __0040.
86 static BOOL
INSTR_ReplaceSelector( CONTEXT86
*context
, WORD
*sel
)
88 extern char Call16_Start
, Call16_End
;
90 if (IS_SELECTOR_SYSTEM(context
->SegCs
))
91 if ( (char *)context
->Eip
>= &Call16_Start
92 && (char *)context
->Eip
< &Call16_End
)
94 /* Saved selector may have become invalid when the relay code */
95 /* tries to restore it. We simply clear it. */
102 #if 0 /* hack until this is moved to kernel */
103 static WORD sys_timer
= 0;
105 sys_timer
= CreateSystemTimer( 55, DOSMEM_Tick
);
107 static HANDLE sys_thread
;
108 if (!sys_thread
) sys_thread
= CreateThread( NULL
, 0, timer_thread
, NULL
, 0, NULL
);
109 *sel
= DOSMEM_BiosDataSeg
;
112 return FALSE
; /* Can't replace selector, crashdump */
116 /***********************************************************************
117 * INSTR_GetOperandAddr
119 * Return the address of an instruction operand (from the mod/rm byte).
121 static BYTE
*INSTR_GetOperandAddr( CONTEXT86
*context
, BYTE
*instr
,
122 int long_addr
, int segprefix
, int *len
)
124 int mod
, rm
, base
, index
= 0, ss
= 0, seg
= 0, off
;
127 #define GET_VAL(val,type) \
128 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
131 GET_VAL( &mod
, BYTE
);
139 case 0: return (BYTE
*)&context
->Eax
;
140 case 1: return (BYTE
*)&context
->Ecx
;
141 case 2: return (BYTE
*)&context
->Edx
;
142 case 3: return (BYTE
*)&context
->Ebx
;
143 case 4: return (BYTE
*)&context
->Esp
;
144 case 5: return (BYTE
*)&context
->Ebp
;
145 case 6: return (BYTE
*)&context
->Esi
;
146 case 7: return (BYTE
*)&context
->Edi
;
155 GET_VAL( &sib
, BYTE
);
160 case 0: index
= context
->Eax
; break;
161 case 1: index
= context
->Ecx
; break;
162 case 2: index
= context
->Edx
; break;
163 case 3: index
= context
->Ebx
; break;
164 case 4: index
= 0; break;
165 case 5: index
= context
->Ebp
; break;
166 case 6: index
= context
->Esi
; break;
167 case 7: index
= context
->Edi
; break;
173 case 0: base
= context
->Eax
; seg
= context
->SegDs
; break;
174 case 1: base
= context
->Ecx
; seg
= context
->SegDs
; break;
175 case 2: base
= context
->Edx
; seg
= context
->SegDs
; break;
176 case 3: base
= context
->Ebx
; seg
= context
->SegDs
; break;
177 case 4: base
= context
->Esp
; seg
= context
->SegSs
; break;
178 case 5: base
= context
->Ebp
; seg
= context
->SegSs
; break;
179 case 6: base
= context
->Esi
; seg
= context
->SegDs
; break;
180 case 7: base
= context
->Edi
; seg
= context
->SegDs
; break;
185 if (rm
== 5) /* special case: ds:(disp32) */
187 GET_VAL( &base
, DWORD
);
188 seg
= context
->SegDs
;
192 case 1: /* 8-bit disp */
193 GET_VAL( &off
, BYTE
);
194 base
+= (signed char)off
;
197 case 2: /* 32-bit disp */
198 GET_VAL( &off
, DWORD
);
199 base
+= (signed long)off
;
203 else /* short address */
207 case 0: /* ds:(bx,si) */
208 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Esi
);
209 seg
= context
->SegDs
;
211 case 1: /* ds:(bx,di) */
212 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Edi
);
213 seg
= context
->SegDs
;
215 case 2: /* ss:(bp,si) */
216 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Esi
);
217 seg
= context
->SegSs
;
219 case 3: /* ss:(bp,di) */
220 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Edi
);
221 seg
= context
->SegSs
;
223 case 4: /* ds:(si) */
224 base
= LOWORD(context
->Esi
);
225 seg
= context
->SegDs
;
227 case 5: /* ds:(di) */
228 base
= LOWORD(context
->Edi
);
229 seg
= context
->SegDs
;
231 case 6: /* ss:(bp) */
232 base
= LOWORD(context
->Ebp
);
233 seg
= context
->SegSs
;
235 case 7: /* ds:(bx) */
236 base
= LOWORD(context
->Ebx
);
237 seg
= context
->SegDs
;
244 if (rm
== 6) /* special case: ds:(disp16) */
246 GET_VAL( &base
, WORD
);
247 seg
= context
->SegDs
;
251 case 1: /* 8-bit disp */
252 GET_VAL( &off
, BYTE
);
253 base
+= (signed char)off
;
256 case 2: /* 16-bit disp */
257 GET_VAL( &off
, WORD
);
258 base
+= (signed short)off
;
263 if (segprefix
!= -1) seg
= segprefix
;
265 /* Make sure the segment and offset are valid */
266 if (IS_SELECTOR_SYSTEM(seg
)) return (BYTE
*)(base
+ (index
<< ss
));
267 if ((seg
& 7) != 7) return NULL
;
268 wine_ldt_get_entry( seg
, &entry
);
269 if (wine_ldt_is_empty( &entry
)) return NULL
;
270 if (wine_ldt_get_limit(&entry
) < (base
+ (index
<< ss
))) return NULL
;
271 return (char *)wine_ldt_get_base(&entry
) + base
+ (index
<< ss
);
276 /***********************************************************************
279 * Emulate the LDS (and LES,LFS,etc.) instruction.
281 static BOOL
INSTR_EmulateLDS( CONTEXT86
*context
, BYTE
*instr
, int long_op
,
282 int long_addr
, int segprefix
, int *len
)
285 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
286 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
287 long_addr
, segprefix
, len
);
289 return FALSE
; /* Unable to emulate it */
290 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
292 if (!INSTR_ReplaceSelector( context
, &seg
))
293 return FALSE
; /* Unable to emulate it */
295 /* Now store the offset in the correct register */
297 switch((*regmodrm
>> 3) & 7)
300 if (long_op
) context
->Eax
= *(DWORD
*)addr
;
301 else SET_LOWORD(context
->Eax
,*(WORD
*)addr
);
304 if (long_op
) context
->Ecx
= *(DWORD
*)addr
;
305 else SET_LOWORD(context
->Ecx
,*(WORD
*)addr
);
308 if (long_op
) context
->Edx
= *(DWORD
*)addr
;
309 else SET_LOWORD(context
->Edx
,*(WORD
*)addr
);
312 if (long_op
) context
->Ebx
= *(DWORD
*)addr
;
313 else SET_LOWORD(context
->Ebx
,*(WORD
*)addr
);
316 if (long_op
) context
->Esp
= *(DWORD
*)addr
;
317 else SET_LOWORD(context
->Esp
,*(WORD
*)addr
);
320 if (long_op
) context
->Ebp
= *(DWORD
*)addr
;
321 else SET_LOWORD(context
->Ebp
,*(WORD
*)addr
);
324 if (long_op
) context
->Esi
= *(DWORD
*)addr
;
325 else SET_LOWORD(context
->Esi
,*(WORD
*)addr
);
328 if (long_op
) context
->Edi
= *(DWORD
*)addr
;
329 else SET_LOWORD(context
->Edi
,*(WORD
*)addr
);
333 /* Store the correct segment in the segment register */
337 case 0xc4: context
->SegEs
= seg
; break; /* les */
338 case 0xc5: context
->SegDs
= seg
; break; /* lds */
339 case 0x0f: switch(instr
[1])
341 case 0xb2: context
->SegSs
= seg
; break; /* lss */
342 case 0xb4: context
->SegFs
= seg
; break; /* lfs */
343 case 0xb5: context
->SegGs
= seg
; break; /* lgs */
348 /* Add the opcode size to the total length */
350 *len
+= 1 + (*instr
== 0x0f);
354 /***********************************************************************
357 * input on a I/O port
359 static DWORD
INSTR_inport( WORD port
, int size
, CONTEXT86
*context
)
361 DWORD res
= IO_inport( port
, size
);
367 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port
, LOBYTE(res
),
368 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
371 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port
, LOWORD(res
),
372 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
375 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port
, res
,
376 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
384 /***********************************************************************
387 * output on a I/O port
389 static void INSTR_outport( WORD port
, int size
, DWORD val
, CONTEXT86
*context
)
391 IO_outport( port
, size
, val
);
397 DPRINTF("0x%x > %02x @ %04x:%04x\n", port
, LOBYTE(val
),
398 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
401 DPRINTF("0x%x > %04x @ %04x:%04x\n", port
, LOWORD(val
),
402 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
405 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port
, val
,
406 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
413 /***********************************************************************
414 * INSTR_EmulateInstruction
416 * Emulate a privileged instruction.
417 * Returns exception code, or 0 if emulation successful.
419 DWORD
INSTR_EmulateInstruction( CONTEXT86
*context
)
421 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
423 DWORD ret
= EXCEPTION_PRIV_INSTRUCTION
;
425 long_op
= long_addr
= (!ISV86(context
) && IS_SELECTOR_32BIT(context
->SegCs
));
426 instr
= make_ptr( context
, context
->SegCs
, context
->Eip
, TRUE
);
427 if (!instr
) return ret
;
429 /* First handle any possible prefix */
431 segprefix
= -1; /* no prefix */
440 segprefix
= context
->SegCs
;
443 segprefix
= context
->SegSs
;
446 segprefix
= context
->SegDs
;
449 segprefix
= context
->SegEs
;
452 segprefix
= context
->SegFs
;
455 segprefix
= context
->SegGs
;
458 long_op
= !long_op
; /* opcode size prefix */
461 long_addr
= !long_addr
; /* addr size prefix */
463 case 0xf0: /* lock */
465 case 0xf2: /* repne */
468 case 0xf3: /* repe */
472 prefix
= 0; /* no more prefixes */
482 /* Now look at the actual instruction */
486 case 0x07: /* pop es */
487 case 0x17: /* pop ss */
488 case 0x1f: /* pop ds */
490 WORD seg
= *(WORD
*)get_stack( context
);
491 if (INSTR_ReplaceSelector( context
, &seg
))
495 case 0x07: context
->SegEs
= seg
; break;
496 case 0x17: context
->SegSs
= seg
; break;
497 case 0x1f: context
->SegDs
= seg
; break;
499 add_stack(context
, long_op
? 4 : 2);
500 context
->Eip
+= prefixlen
+ 1;
504 break; /* Unable to emulate it */
506 case 0x0f: /* extended instruction */
509 case 0x22: /* mov eax, crX */
512 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
513 context
->Eip
,context
->Eax
);
514 context
->Eip
+= prefixlen
+3;
517 break; /*fallthrough to bad instruction handling */
519 break; /*fallthrough to bad instruction handling */
520 case 0x20: /* mov crX, eax */
522 case 0xe0: /* mov cr4, eax */
523 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
524 * bit 0: VME Virtual Mode Exception ?
525 * bit 1: PVI Protected mode Virtual Interrupt
526 * bit 2: TSD Timestamp disable
527 * bit 3: DE Debugging extensions
528 * bit 4: PSE Page size extensions
529 * bit 5: PAE Physical address extension
530 * bit 6: MCE Machine check enable
531 * bit 7: PGE Enable global pages
532 * bit 8: PCE Enable performance counters at IPL3
534 ERR("mov cr4,eax at 0x%08lx\n",context
->Eip
);
536 context
->Eip
+= prefixlen
+3;
538 case 0xc0: /* mov cr0, eax */
539 ERR("mov cr0,eax at 0x%08lx\n",context
->Eip
);
540 context
->Eax
= 0x10; /* FIXME: set more bits ? */
541 context
->Eip
+= prefixlen
+3;
543 default: /* fallthrough to illegal instruction */
546 /* fallthrough to illegal instruction */
548 case 0xa1: /* pop fs */
550 WORD seg
= *(WORD
*)get_stack( context
);
551 if (INSTR_ReplaceSelector( context
, &seg
))
553 context
->SegFs
= seg
;
554 add_stack(context
, long_op
? 4 : 2);
555 context
->Eip
+= prefixlen
+ 2;
560 case 0xa9: /* pop gs */
562 WORD seg
= *(WORD
*)get_stack( context
);
563 if (INSTR_ReplaceSelector( context
, &seg
))
565 context
->SegGs
= seg
;
566 add_stack(context
, long_op
? 4 : 2);
567 context
->Eip
+= prefixlen
+ 2;
572 case 0xb2: /* lss addr,reg */
573 case 0xb4: /* lfs addr,reg */
574 case 0xb5: /* lgs addr,reg */
575 if (INSTR_EmulateLDS( context
, instr
, long_op
,
576 long_addr
, segprefix
, &len
))
578 context
->Eip
+= prefixlen
+ len
;
583 break; /* Unable to emulate it */
585 case 0x6c: /* insb */
586 case 0x6d: /* insw/d */
587 case 0x6e: /* outsb */
588 case 0x6f: /* outsw/d */
590 int typ
= *instr
; /* Just in case it's overwritten. */
591 int outp
= (typ
>= 0x6e);
592 unsigned long count
= repX
?
593 (long_addr
? context
->Ecx
: LOWORD(context
->Ecx
)) : 1;
594 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
595 int step
= (context
->EFlags
& 0x400) ? -opsize
: +opsize
;
596 int seg
= outp
? context
->SegDs
: context
->SegEs
; /* FIXME: is this right? */
599 /* FIXME: Check segment readable. */
602 /* FIXME: Check segment writeable. */
607 if (long_addr
) context
->Ecx
= 0;
608 else SET_LOWORD(context
->Ecx
,0);
614 WORD dx
= LOWORD(context
->Edx
);
617 data
= make_ptr( context
, seg
, context
->Esi
, long_addr
);
618 if (long_addr
) context
->Esi
+= step
;
619 else ADD_LOWORD(context
->Esi
,step
);
623 data
= make_ptr( context
, seg
, context
->Edi
, long_addr
);
624 if (long_addr
) context
->Edi
+= step
;
625 else ADD_LOWORD(context
->Edi
,step
);
631 *(BYTE
*)data
= INSTR_inport( dx
, 1, context
);
635 *(DWORD
*)data
= INSTR_inport( dx
, 4, context
);
637 *(WORD
*)data
= INSTR_inport( dx
, 2, context
);
640 INSTR_outport( dx
, 1, *(BYTE
*)data
, context
);
644 INSTR_outport( dx
, 4, *(DWORD
*)data
, context
);
646 INSTR_outport( dx
, 2, *(WORD
*)data
, context
);
650 context
->Eip
+= prefixlen
+ 1;
654 case 0x8e: /* mov XX,segment_reg */
657 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
658 long_addr
, segprefix
, &len
);
660 break; /* Unable to emulate it */
662 if (!INSTR_ReplaceSelector( context
, &seg
))
663 break; /* Unable to emulate it */
665 switch((instr
[1] >> 3) & 7)
668 context
->SegEs
= seg
;
669 context
->Eip
+= prefixlen
+ len
+ 1;
674 context
->SegSs
= seg
;
675 context
->Eip
+= prefixlen
+ len
+ 1;
678 context
->SegDs
= seg
;
679 context
->Eip
+= prefixlen
+ len
+ 1;
682 context
->SegFs
= seg
;
683 context
->Eip
+= prefixlen
+ len
+ 1;
686 context
->SegGs
= seg
;
687 context
->Eip
+= prefixlen
+ len
+ 1;
694 break; /* Unable to emulate it */
696 case 0xc4: /* les addr,reg */
697 case 0xc5: /* lds addr,reg */
698 if (INSTR_EmulateLDS( context
, instr
, long_op
,
699 long_addr
, segprefix
, &len
))
701 context
->Eip
+= prefixlen
+ len
;
704 break; /* Unable to emulate it */
706 case 0xcd: /* int <XX> */
707 if (IS_SELECTOR_SYSTEM(context
->SegCs
))
709 /* Win32 applications cannot use interrupts */
710 ret
= EXCEPTION_ACCESS_VIOLATION
;
713 else if (!Dosvm
.EmulateInterruptPM
&& !DPMI_LoadDosSystem())
715 ERR("could not initialize interrupt handling\n");
719 context
->Eip
+= prefixlen
+ 2;
720 Dosvm
.EmulateInterruptPM( context
, instr
[1] );
723 break; /* Unable to emulate it */
725 case 0xcf: /* iret */
728 DWORD
*stack
= get_stack( context
);
729 context
->Eip
= *stack
++;
730 context
->SegCs
= *stack
++;
731 context
->EFlags
= *stack
;
732 add_stack(context
, 3*sizeof(DWORD
)); /* Pop the return address and flags */
736 WORD
*stack
= get_stack( context
);
737 context
->Eip
= *stack
++;
738 context
->SegCs
= *stack
++;
739 SET_LOWORD(context
->EFlags
,*stack
);
740 add_stack(context
, 3*sizeof(WORD
)); /* Pop the return address and flags */
744 case 0xe4: /* inb al,XX */
745 SET_LOBYTE(context
->Eax
,INSTR_inport( instr
[1], 1, context
));
746 context
->Eip
+= prefixlen
+ 2;
749 case 0xe5: /* in (e)ax,XX */
751 context
->Eax
= INSTR_inport( instr
[1], 4, context
);
753 SET_LOWORD(context
->Eax
, INSTR_inport( instr
[1], 2, context
));
754 context
->Eip
+= prefixlen
+ 2;
757 case 0xe6: /* outb XX,al */
758 INSTR_outport( instr
[1], 1, LOBYTE(context
->Eax
), context
);
759 context
->Eip
+= prefixlen
+ 2;
762 case 0xe7: /* out XX,(e)ax */
764 INSTR_outport( instr
[1], 4, context
->Eax
, context
);
766 INSTR_outport( instr
[1], 2, LOWORD(context
->Eax
), context
);
767 context
->Eip
+= prefixlen
+ 2;
770 case 0xec: /* inb al,dx */
771 SET_LOBYTE(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 1, context
) );
772 context
->Eip
+= prefixlen
+ 1;
775 case 0xed: /* in (e)ax,dx */
777 context
->Eax
= INSTR_inport( LOWORD(context
->Edx
), 4, context
);
779 SET_LOWORD(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 2, context
));
780 context
->Eip
+= prefixlen
+ 1;
783 case 0xee: /* outb dx,al */
784 INSTR_outport( LOWORD(context
->Edx
), 1, LOBYTE(context
->Eax
), context
);
785 context
->Eip
+= prefixlen
+ 1;
788 case 0xef: /* out dx,(e)ax */
790 INSTR_outport( LOWORD(context
->Edx
), 4, context
->Eax
, context
);
792 INSTR_outport( LOWORD(context
->Edx
), 2, LOWORD(context
->Eax
), context
);
793 context
->Eip
+= prefixlen
+ 1;
796 case 0xfa: /* cli, ignored */
797 context
->Eip
+= prefixlen
+ 1;
800 case 0xfb: /* sti, ignored */
801 context
->Eip
+= prefixlen
+ 1;
806 /* Check for Win16 __GP handler */
807 if (!IS_SELECTOR_SYSTEM(context
->SegCs
))
809 SEGPTR gpHandler
= HasGPHandler16( MAKESEGPTR( context
->SegCs
, context
->Eip
) );
812 WORD
*stack
= get_stack( context
);
813 *--stack
= context
->SegCs
;
814 *--stack
= context
->Eip
;
815 add_stack(context
, -2*sizeof(WORD
));
817 context
->SegCs
= SELECTOROF( gpHandler
);
818 context
->Eip
= OFFSETOF( gpHandler
);
822 return ret
; /* Unable to emulate it */
825 #endif /* __i386__ */