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
);
62 /***********************************************************************
63 * INSTR_ReplaceSelector
65 * Try to replace an invalid selector by a valid one.
66 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
67 * is the so called 'bimodal' selector 0x40, which points to the BIOS
68 * data segment. Used by (at least) Borland products (and programs compiled
69 * using Borland products).
71 * See Undocumented Windows, Chapter 5, __0040.
73 static BOOL
INSTR_ReplaceSelector( CONTEXT86
*context
, WORD
*sel
)
75 extern char Call16_Start
, Call16_End
;
77 if (IS_SELECTOR_SYSTEM(context
->SegCs
))
78 if ( (char *)context
->Eip
>= &Call16_Start
79 && (char *)context
->Eip
< &Call16_End
)
81 /* Saved selector may have become invalid when the relay code */
82 /* tries to restore it. We simply clear it. */
89 static WORD sys_timer
= 0;
91 sys_timer
= CreateSystemTimer( 55, DOSMEM_Tick
);
92 *sel
= DOSMEM_BiosDataSeg
;
95 return FALSE
; /* Can't replace selector, crashdump */
99 /***********************************************************************
100 * INSTR_GetOperandAddr
102 * Return the address of an instruction operand (from the mod/rm byte).
104 static BYTE
*INSTR_GetOperandAddr( CONTEXT86
*context
, BYTE
*instr
,
105 int long_addr
, int segprefix
, int *len
)
107 int mod
, rm
, base
, index
= 0, ss
= 0, seg
= 0, off
;
110 #define GET_VAL(val,type) \
111 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
114 GET_VAL( &mod
, BYTE
);
122 case 0: return (BYTE
*)&context
->Eax
;
123 case 1: return (BYTE
*)&context
->Ecx
;
124 case 2: return (BYTE
*)&context
->Edx
;
125 case 3: return (BYTE
*)&context
->Ebx
;
126 case 4: return (BYTE
*)&context
->Esp
;
127 case 5: return (BYTE
*)&context
->Ebp
;
128 case 6: return (BYTE
*)&context
->Esi
;
129 case 7: return (BYTE
*)&context
->Edi
;
138 GET_VAL( &sib
, BYTE
);
143 case 0: index
= context
->Eax
; break;
144 case 1: index
= context
->Ecx
; break;
145 case 2: index
= context
->Edx
; break;
146 case 3: index
= context
->Ebx
; break;
147 case 4: index
= 0; break;
148 case 5: index
= context
->Ebp
; break;
149 case 6: index
= context
->Esi
; break;
150 case 7: index
= context
->Edi
; break;
156 case 0: base
= context
->Eax
; seg
= context
->SegDs
; break;
157 case 1: base
= context
->Ecx
; seg
= context
->SegDs
; break;
158 case 2: base
= context
->Edx
; seg
= context
->SegDs
; break;
159 case 3: base
= context
->Ebx
; seg
= context
->SegDs
; break;
160 case 4: base
= context
->Esp
; seg
= context
->SegSs
; break;
161 case 5: base
= context
->Ebp
; seg
= context
->SegSs
; break;
162 case 6: base
= context
->Esi
; seg
= context
->SegDs
; break;
163 case 7: base
= context
->Edi
; seg
= context
->SegDs
; break;
168 if (rm
== 5) /* special case: ds:(disp32) */
170 GET_VAL( &base
, DWORD
);
171 seg
= context
->SegDs
;
175 case 1: /* 8-bit disp */
176 GET_VAL( &off
, BYTE
);
177 base
+= (signed char)off
;
180 case 2: /* 32-bit disp */
181 GET_VAL( &off
, DWORD
);
182 base
+= (signed long)off
;
186 else /* short address */
190 case 0: /* ds:(bx,si) */
191 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Esi
);
192 seg
= context
->SegDs
;
194 case 1: /* ds:(bx,di) */
195 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Edi
);
196 seg
= context
->SegDs
;
198 case 2: /* ss:(bp,si) */
199 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Esi
);
200 seg
= context
->SegSs
;
202 case 3: /* ss:(bp,di) */
203 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Edi
);
204 seg
= context
->SegSs
;
206 case 4: /* ds:(si) */
207 base
= LOWORD(context
->Esi
);
208 seg
= context
->SegDs
;
210 case 5: /* ds:(di) */
211 base
= LOWORD(context
->Edi
);
212 seg
= context
->SegDs
;
214 case 6: /* ss:(bp) */
215 base
= LOWORD(context
->Ebp
);
216 seg
= context
->SegSs
;
218 case 7: /* ds:(bx) */
219 base
= LOWORD(context
->Ebx
);
220 seg
= context
->SegDs
;
227 if (rm
== 6) /* special case: ds:(disp16) */
229 GET_VAL( &base
, WORD
);
230 seg
= context
->SegDs
;
234 case 1: /* 8-bit disp */
235 GET_VAL( &off
, BYTE
);
236 base
+= (signed char)off
;
239 case 2: /* 16-bit disp */
240 GET_VAL( &off
, WORD
);
241 base
+= (signed short)off
;
246 if (segprefix
!= -1) seg
= segprefix
;
248 /* Make sure the segment and offset are valid */
249 if (IS_SELECTOR_SYSTEM(seg
)) return (BYTE
*)(base
+ (index
<< ss
));
250 if ((seg
& 7) != 7) return NULL
;
251 wine_ldt_get_entry( seg
, &entry
);
252 if (wine_ldt_is_empty( &entry
)) return NULL
;
253 if (wine_ldt_get_limit(&entry
) < (base
+ (index
<< ss
))) return NULL
;
254 return (char *)wine_ldt_get_base(&entry
) + base
+ (index
<< ss
);
259 /***********************************************************************
262 * Emulate the LDS (and LES,LFS,etc.) instruction.
264 static BOOL
INSTR_EmulateLDS( CONTEXT86
*context
, BYTE
*instr
, int long_op
,
265 int long_addr
, int segprefix
, int *len
)
268 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
269 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
270 long_addr
, segprefix
, len
);
272 return FALSE
; /* Unable to emulate it */
273 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
275 if (!INSTR_ReplaceSelector( context
, &seg
))
276 return FALSE
; /* Unable to emulate it */
278 /* Now store the offset in the correct register */
280 switch((*regmodrm
>> 3) & 7)
283 if (long_op
) context
->Eax
= *(DWORD
*)addr
;
284 else SET_LOWORD(context
->Eax
,*(WORD
*)addr
);
287 if (long_op
) context
->Ecx
= *(DWORD
*)addr
;
288 else SET_LOWORD(context
->Ecx
,*(WORD
*)addr
);
291 if (long_op
) context
->Edx
= *(DWORD
*)addr
;
292 else SET_LOWORD(context
->Edx
,*(WORD
*)addr
);
295 if (long_op
) context
->Ebx
= *(DWORD
*)addr
;
296 else SET_LOWORD(context
->Ebx
,*(WORD
*)addr
);
299 if (long_op
) context
->Esp
= *(DWORD
*)addr
;
300 else SET_LOWORD(context
->Esp
,*(WORD
*)addr
);
303 if (long_op
) context
->Ebp
= *(DWORD
*)addr
;
304 else SET_LOWORD(context
->Ebp
,*(WORD
*)addr
);
307 if (long_op
) context
->Esi
= *(DWORD
*)addr
;
308 else SET_LOWORD(context
->Esi
,*(WORD
*)addr
);
311 if (long_op
) context
->Edi
= *(DWORD
*)addr
;
312 else SET_LOWORD(context
->Edi
,*(WORD
*)addr
);
316 /* Store the correct segment in the segment register */
320 case 0xc4: context
->SegEs
= seg
; break; /* les */
321 case 0xc5: context
->SegDs
= seg
; break; /* lds */
322 case 0x0f: switch(instr
[1])
324 case 0xb2: context
->SegSs
= seg
; break; /* lss */
325 case 0xb4: context
->SegFs
= seg
; break; /* lfs */
326 case 0xb5: context
->SegGs
= seg
; break; /* lgs */
331 /* Add the opcode size to the total length */
333 *len
+= 1 + (*instr
== 0x0f);
337 /***********************************************************************
340 * input on a I/O port
342 static DWORD
INSTR_inport( WORD port
, int size
, CONTEXT86
*context
)
344 DWORD res
= IO_inport( port
, size
);
350 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port
, LOBYTE(res
),
351 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
354 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port
, LOWORD(res
),
355 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
358 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port
, res
,
359 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
367 /***********************************************************************
370 * output on a I/O port
372 static void INSTR_outport( WORD port
, int size
, DWORD val
, CONTEXT86
*context
)
374 IO_outport( port
, size
, val
);
380 DPRINTF("0x%x > %02x @ %04x:%04x\n", port
, LOBYTE(val
),
381 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
384 DPRINTF("0x%x > %04x @ %04x:%04x\n", port
, LOWORD(val
),
385 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
388 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port
, val
,
389 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
396 /***********************************************************************
397 * INSTR_EmulateInstruction
399 * Emulate a privileged instruction. Returns TRUE if emulation successful.
401 BOOL
INSTR_EmulateInstruction( CONTEXT86
*context
)
403 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
407 long_op
= long_addr
= (!ISV86(context
) && IS_SELECTOR_32BIT(context
->SegCs
));
408 instr
= make_ptr( context
, context
->SegCs
, context
->Eip
, TRUE
);
409 if (!instr
) return FALSE
;
411 /* First handle any possible prefix */
413 segprefix
= -1; /* no prefix */
422 segprefix
= context
->SegCs
;
425 segprefix
= context
->SegSs
;
428 segprefix
= context
->SegDs
;
431 segprefix
= context
->SegEs
;
434 segprefix
= context
->SegFs
;
437 segprefix
= context
->SegGs
;
440 long_op
= !long_op
; /* opcode size prefix */
443 long_addr
= !long_addr
; /* addr size prefix */
445 case 0xf0: /* lock */
447 case 0xf2: /* repne */
450 case 0xf3: /* repe */
454 prefix
= 0; /* no more prefixes */
464 /* Now look at the actual instruction */
468 case 0x07: /* pop es */
469 case 0x17: /* pop ss */
470 case 0x1f: /* pop ds */
472 WORD seg
= *(WORD
*)get_stack( context
);
473 if (INSTR_ReplaceSelector( context
, &seg
))
477 case 0x07: context
->SegEs
= seg
; break;
478 case 0x17: context
->SegSs
= seg
; break;
479 case 0x1f: context
->SegDs
= seg
; break;
481 add_stack(context
, long_op
? 4 : 2);
482 context
->Eip
+= prefixlen
+ 1;
486 break; /* Unable to emulate it */
488 case 0x0f: /* extended instruction */
491 case 0x22: /* mov eax, crX */
494 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
495 context
->Eip
,context
->Eax
);
496 context
->Eip
+= prefixlen
+3;
499 break; /*fallthrough to bad instruction handling */
501 break; /*fallthrough to bad instruction handling */
502 case 0x20: /* mov crX, eax */
504 case 0xe0: /* mov cr4, eax */
505 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
506 * bit 0: VME Virtual Mode Exception ?
507 * bit 1: PVI Protected mode Virtual Interrupt
508 * bit 2: TSD Timestamp disable
509 * bit 3: DE Debugging extensions
510 * bit 4: PSE Page size extensions
511 * bit 5: PAE Physical address extension
512 * bit 6: MCE Machine check enable
513 * bit 7: PGE Enable global pages
514 * bit 8: PCE Enable performance counters at IPL3
516 ERR("mov cr4,eax at 0x%08lx\n",context
->Eip
);
518 context
->Eip
+= prefixlen
+3;
520 case 0xc0: /* mov cr0, eax */
521 ERR("mov cr0,eax at 0x%08lx\n",context
->Eip
);
522 context
->Eax
= 0x10; /* FIXME: set more bits ? */
523 context
->Eip
+= prefixlen
+3;
525 default: /* fallthrough to illegal instruction */
528 /* fallthrough to illegal instruction */
530 case 0xa1: /* pop fs */
532 WORD seg
= *(WORD
*)get_stack( context
);
533 if (INSTR_ReplaceSelector( context
, &seg
))
535 context
->SegFs
= seg
;
536 add_stack(context
, long_op
? 4 : 2);
537 context
->Eip
+= prefixlen
+ 2;
542 case 0xa9: /* pop gs */
544 WORD seg
= *(WORD
*)get_stack( context
);
545 if (INSTR_ReplaceSelector( context
, &seg
))
547 context
->SegGs
= seg
;
548 add_stack(context
, long_op
? 4 : 2);
549 context
->Eip
+= prefixlen
+ 2;
554 case 0xb2: /* lss addr,reg */
555 case 0xb4: /* lfs addr,reg */
556 case 0xb5: /* lgs addr,reg */
557 if (INSTR_EmulateLDS( context
, instr
, long_op
,
558 long_addr
, segprefix
, &len
))
560 context
->Eip
+= prefixlen
+ len
;
565 break; /* Unable to emulate it */
567 case 0x6c: /* insb */
568 case 0x6d: /* insw/d */
569 case 0x6e: /* outsb */
570 case 0x6f: /* outsw/d */
572 int typ
= *instr
; /* Just in case it's overwritten. */
573 int outp
= (typ
>= 0x6e);
574 unsigned long count
= repX
?
575 (long_addr
? context
->Ecx
: LOWORD(context
->Ecx
)) : 1;
576 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
577 int step
= (context
->EFlags
& 0x400) ? -opsize
: +opsize
;
578 int seg
= outp
? context
->SegDs
: context
->SegEs
; /* FIXME: is this right? */
581 /* FIXME: Check segment readable. */
584 /* FIXME: Check segment writeable. */
589 if (long_addr
) context
->Ecx
= 0;
590 else SET_LOWORD(context
->Ecx
,0);
596 WORD dx
= LOWORD(context
->Edx
);
599 data
= make_ptr( context
, seg
, context
->Esi
, long_addr
);
600 if (long_addr
) context
->Esi
+= step
;
601 else ADD_LOWORD(context
->Esi
,step
);
605 data
= make_ptr( context
, seg
, context
->Edi
, long_addr
);
606 if (long_addr
) context
->Edi
+= step
;
607 else ADD_LOWORD(context
->Edi
,step
);
613 *(BYTE
*)data
= INSTR_inport( dx
, 1, context
);
617 *(DWORD
*)data
= INSTR_inport( dx
, 4, context
);
619 *(WORD
*)data
= INSTR_inport( dx
, 2, context
);
622 INSTR_outport( dx
, 1, *(BYTE
*)data
, context
);
626 INSTR_outport( dx
, 4, *(DWORD
*)data
, context
);
628 INSTR_outport( dx
, 2, *(WORD
*)data
, context
);
632 context
->Eip
+= prefixlen
+ 1;
636 case 0x8e: /* mov XX,segment_reg */
639 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
640 long_addr
, segprefix
, &len
);
642 break; /* Unable to emulate it */
644 if (!INSTR_ReplaceSelector( context
, &seg
))
645 break; /* Unable to emulate it */
647 switch((instr
[1] >> 3) & 7)
650 context
->SegEs
= seg
;
651 context
->Eip
+= prefixlen
+ len
+ 1;
656 context
->SegSs
= seg
;
657 context
->Eip
+= prefixlen
+ len
+ 1;
660 context
->SegDs
= seg
;
661 context
->Eip
+= prefixlen
+ len
+ 1;
664 context
->SegFs
= seg
;
665 context
->Eip
+= prefixlen
+ len
+ 1;
668 context
->SegGs
= seg
;
669 context
->Eip
+= prefixlen
+ len
+ 1;
676 break; /* Unable to emulate it */
678 case 0xc4: /* les addr,reg */
679 case 0xc5: /* lds addr,reg */
680 if (INSTR_EmulateLDS( context
, instr
, long_op
,
681 long_addr
, segprefix
, &len
))
683 context
->Eip
+= prefixlen
+ len
;
686 break; /* Unable to emulate it */
688 case 0xcd: /* int <XX> */
689 if(!Dosvm
.EmulateInterruptPM
&& !DPMI_LoadDosSystem())
690 ERR("could not initialize interrupt handling\n");
692 context
->Eip
+= prefixlen
+ 2;
693 Dosvm
.EmulateInterruptPM( context
, instr
[1] );
696 break; /* Unable to emulate it */
698 case 0xcf: /* iret */
701 DWORD
*stack
= get_stack( context
);
702 context
->Eip
= *stack
++;
703 context
->SegCs
= *stack
++;
704 context
->EFlags
= *stack
;
705 add_stack(context
, 3*sizeof(DWORD
)); /* Pop the return address and flags */
709 WORD
*stack
= get_stack( context
);
710 context
->Eip
= *stack
++;
711 context
->SegCs
= *stack
++;
712 SET_LOWORD(context
->EFlags
,*stack
);
713 add_stack(context
, 3*sizeof(WORD
)); /* Pop the return address and flags */
717 case 0xe4: /* inb al,XX */
718 SET_LOBYTE(context
->Eax
,INSTR_inport( instr
[1], 1, context
));
719 context
->Eip
+= prefixlen
+ 2;
722 case 0xe5: /* in (e)ax,XX */
724 context
->Eax
= INSTR_inport( instr
[1], 4, context
);
726 SET_LOWORD(context
->Eax
, INSTR_inport( instr
[1], 2, context
));
727 context
->Eip
+= prefixlen
+ 2;
730 case 0xe6: /* outb XX,al */
731 INSTR_outport( instr
[1], 1, LOBYTE(context
->Eax
), context
);
732 context
->Eip
+= prefixlen
+ 2;
735 case 0xe7: /* out XX,(e)ax */
737 INSTR_outport( instr
[1], 4, context
->Eax
, context
);
739 INSTR_outport( instr
[1], 2, LOWORD(context
->Eax
), context
);
740 context
->Eip
+= prefixlen
+ 2;
743 case 0xec: /* inb al,dx */
744 SET_LOBYTE(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 1, context
) );
745 context
->Eip
+= prefixlen
+ 1;
748 case 0xed: /* in (e)ax,dx */
750 context
->Eax
= INSTR_inport( LOWORD(context
->Edx
), 4, context
);
752 SET_LOWORD(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 2, context
));
753 context
->Eip
+= prefixlen
+ 1;
756 case 0xee: /* outb dx,al */
757 INSTR_outport( LOWORD(context
->Edx
), 1, LOBYTE(context
->Eax
), context
);
758 context
->Eip
+= prefixlen
+ 1;
761 case 0xef: /* out dx,(e)ax */
763 INSTR_outport( LOWORD(context
->Edx
), 4, context
->Eax
, context
);
765 INSTR_outport( LOWORD(context
->Edx
), 2, LOWORD(context
->Eax
), context
);
766 context
->Eip
+= prefixlen
+ 1;
769 case 0xfa: /* cli, ignored */
770 context
->Eip
+= prefixlen
+ 1;
773 case 0xfb: /* sti, ignored */
774 context
->Eip
+= prefixlen
+ 1;
779 /* Check for Win16 __GP handler */
780 gpHandler
= HasGPHandler16( MAKESEGPTR( context
->SegCs
, context
->Eip
) );
783 WORD
*stack
= get_stack( context
);
784 *--stack
= context
->SegCs
;
785 *--stack
= context
->Eip
;
786 add_stack(context
, -2*sizeof(WORD
));
788 context
->SegCs
= SELECTOROF( gpHandler
);
789 context
->Eip
= OFFSETOF( gpHandler
);
792 return FALSE
; /* Unable to emulate it */
795 #endif /* __i386__ */