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 inline static void add_stack( CONTEXT86
*context
, int offset
)
37 if (ISV86(context
) || !IS_SELECTOR_32BIT(context
->SegSs
))
38 ADD_LOWORD( context
->Esp
, offset
);
40 context
->Esp
+= offset
;
43 inline static void *make_ptr( CONTEXT86
*context
, DWORD seg
, DWORD off
, int long_addr
)
45 if (ISV86(context
)) return PTR_REAL_TO_LIN( seg
, off
);
46 if (IS_SELECTOR_SYSTEM(seg
)) return (void *)off
;
47 if (!long_addr
) off
= LOWORD(off
);
48 return (char *) MapSL( MAKESEGPTR( seg
, 0 ) ) + off
;
51 inline static void *get_stack( CONTEXT86
*context
)
54 return PTR_REAL_TO_LIN( context
->SegSs
, context
->Esp
);
55 if (IS_SELECTOR_SYSTEM(context
->SegSs
))
56 return (void *)context
->Esp
;
57 if (IS_SELECTOR_32BIT(context
->SegSs
))
58 return (char *) MapSL( MAKESEGPTR( context
->SegSs
, 0 ) ) + context
->Esp
;
59 return MapSL( MAKESEGPTR( context
->SegSs
, LOWORD(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 if (!IS_SELECTOR_SYSTEM(*sel
) && !IS_SELECTOR_FREE(*sel
))
96 ERR("Got protection fault on valid selector, maybe your kernel is too old?\n" );
97 return FALSE
; /* Can't replace selector, crashdump */
101 /***********************************************************************
102 * INSTR_GetOperandAddr
104 * Return the address of an instruction operand (from the mod/rm byte).
106 static BYTE
*INSTR_GetOperandAddr( CONTEXT86
*context
, BYTE
*instr
,
107 int long_addr
, int segprefix
, int *len
)
109 int mod
, rm
, base
, index
= 0, ss
= 0, seg
= 0, off
;
111 #define GET_VAL(val,type) \
112 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
115 GET_VAL( &mod
, BYTE
);
123 case 0: return (BYTE
*)&context
->Eax
;
124 case 1: return (BYTE
*)&context
->Ecx
;
125 case 2: return (BYTE
*)&context
->Edx
;
126 case 3: return (BYTE
*)&context
->Ebx
;
127 case 4: return (BYTE
*)&context
->Esp
;
128 case 5: return (BYTE
*)&context
->Ebp
;
129 case 6: return (BYTE
*)&context
->Esi
;
130 case 7: return (BYTE
*)&context
->Edi
;
139 GET_VAL( &sib
, BYTE
);
144 case 0: index
= context
->Eax
; break;
145 case 1: index
= context
->Ecx
; break;
146 case 2: index
= context
->Edx
; break;
147 case 3: index
= context
->Ebx
; break;
148 case 4: index
= 0; break;
149 case 5: index
= context
->Ebp
; break;
150 case 6: index
= context
->Esi
; break;
151 case 7: index
= context
->Edi
; break;
157 case 0: base
= context
->Eax
; seg
= context
->SegDs
; break;
158 case 1: base
= context
->Ecx
; seg
= context
->SegDs
; break;
159 case 2: base
= context
->Edx
; seg
= context
->SegDs
; break;
160 case 3: base
= context
->Ebx
; seg
= context
->SegDs
; break;
161 case 4: base
= context
->Esp
; seg
= context
->SegSs
; break;
162 case 5: base
= context
->Ebp
; seg
= context
->SegSs
; break;
163 case 6: base
= context
->Esi
; seg
= context
->SegDs
; break;
164 case 7: base
= context
->Edi
; seg
= context
->SegDs
; break;
169 if (rm
== 5) /* special case: ds:(disp32) */
171 GET_VAL( &base
, DWORD
);
172 seg
= context
->SegDs
;
176 case 1: /* 8-bit disp */
177 GET_VAL( &off
, BYTE
);
178 base
+= (signed char)off
;
181 case 2: /* 32-bit disp */
182 GET_VAL( &off
, DWORD
);
183 base
+= (signed long)off
;
187 else /* short address */
191 case 0: /* ds:(bx,si) */
192 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Esi
);
193 seg
= context
->SegDs
;
195 case 1: /* ds:(bx,di) */
196 base
= LOWORD(context
->Ebx
) + LOWORD(context
->Edi
);
197 seg
= context
->SegDs
;
199 case 2: /* ss:(bp,si) */
200 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Esi
);
201 seg
= context
->SegSs
;
203 case 3: /* ss:(bp,di) */
204 base
= LOWORD(context
->Ebp
) + LOWORD(context
->Edi
);
205 seg
= context
->SegSs
;
207 case 4: /* ds:(si) */
208 base
= LOWORD(context
->Esi
);
209 seg
= context
->SegDs
;
211 case 5: /* ds:(di) */
212 base
= LOWORD(context
->Edi
);
213 seg
= context
->SegDs
;
215 case 6: /* ss:(bp) */
216 base
= LOWORD(context
->Ebp
);
217 seg
= context
->SegSs
;
219 case 7: /* ds:(bx) */
220 base
= LOWORD(context
->Ebx
);
221 seg
= context
->SegDs
;
228 if (rm
== 6) /* special case: ds:(disp16) */
230 GET_VAL( &base
, WORD
);
231 seg
= context
->SegDs
;
235 case 1: /* 8-bit disp */
236 GET_VAL( &off
, BYTE
);
237 base
+= (signed char)off
;
240 case 2: /* 16-bit disp */
241 GET_VAL( &off
, WORD
);
242 base
+= (signed short)off
;
247 if (segprefix
!= -1) seg
= segprefix
;
249 /* Make sure the segment and offset are valid */
250 if (IS_SELECTOR_SYSTEM(seg
)) return (BYTE
*)(base
+ (index
<< ss
));
251 if (((seg
& 7) != 7) || IS_SELECTOR_FREE(seg
)) return NULL
;
252 if (wine_ldt_copy
.limit
[seg
>> 3] < (base
+ (index
<< ss
))) return NULL
;
253 return (char *) MapSL( MAKESEGPTR( seg
, 0 ) ) + base
+ (index
<< ss
);
258 /***********************************************************************
261 * Emulate the LDS (and LES,LFS,etc.) instruction.
263 static BOOL
INSTR_EmulateLDS( CONTEXT86
*context
, BYTE
*instr
, int long_op
,
264 int long_addr
, int segprefix
, int *len
)
267 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
268 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
269 long_addr
, segprefix
, len
);
271 return FALSE
; /* Unable to emulate it */
272 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
274 if (!INSTR_ReplaceSelector( context
, &seg
))
275 return FALSE
; /* Unable to emulate it */
277 /* Now store the offset in the correct register */
279 switch((*regmodrm
>> 3) & 7)
282 if (long_op
) context
->Eax
= *(DWORD
*)addr
;
283 else SET_LOWORD(context
->Eax
,*(WORD
*)addr
);
286 if (long_op
) context
->Ecx
= *(DWORD
*)addr
;
287 else SET_LOWORD(context
->Ecx
,*(WORD
*)addr
);
290 if (long_op
) context
->Edx
= *(DWORD
*)addr
;
291 else SET_LOWORD(context
->Edx
,*(WORD
*)addr
);
294 if (long_op
) context
->Ebx
= *(DWORD
*)addr
;
295 else SET_LOWORD(context
->Ebx
,*(WORD
*)addr
);
298 if (long_op
) context
->Esp
= *(DWORD
*)addr
;
299 else SET_LOWORD(context
->Esp
,*(WORD
*)addr
);
302 if (long_op
) context
->Ebp
= *(DWORD
*)addr
;
303 else SET_LOWORD(context
->Ebp
,*(WORD
*)addr
);
306 if (long_op
) context
->Esi
= *(DWORD
*)addr
;
307 else SET_LOWORD(context
->Esi
,*(WORD
*)addr
);
310 if (long_op
) context
->Edi
= *(DWORD
*)addr
;
311 else SET_LOWORD(context
->Edi
,*(WORD
*)addr
);
315 /* Store the correct segment in the segment register */
319 case 0xc4: context
->SegEs
= seg
; break; /* les */
320 case 0xc5: context
->SegDs
= seg
; break; /* lds */
321 case 0x0f: switch(instr
[1])
323 case 0xb2: context
->SegSs
= seg
; break; /* lss */
324 case 0xb4: context
->SegFs
= seg
; break; /* lfs */
325 case 0xb5: context
->SegGs
= seg
; break; /* lgs */
330 /* Add the opcode size to the total length */
332 *len
+= 1 + (*instr
== 0x0f);
336 /***********************************************************************
339 * input on a I/O port
341 static DWORD
INSTR_inport( WORD port
, int size
, CONTEXT86
*context
)
343 DWORD res
= IO_inport( port
, size
);
349 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port
, LOBYTE(res
),
350 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
353 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port
, LOWORD(res
),
354 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
357 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port
, res
,
358 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
366 /***********************************************************************
369 * output on a I/O port
371 static void INSTR_outport( WORD port
, int size
, DWORD val
, CONTEXT86
*context
)
373 IO_outport( port
, size
, val
);
379 DPRINTF("0x%x > %02x @ %04x:%04x\n", port
, LOBYTE(val
),
380 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
383 DPRINTF("0x%x > %04x @ %04x:%04x\n", port
, LOWORD(val
),
384 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
387 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port
, val
,
388 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
395 /***********************************************************************
396 * INSTR_EmulateInstruction
398 * Emulate a privileged instruction. Returns TRUE if emulation successful.
400 BOOL
INSTR_EmulateInstruction( CONTEXT86
*context
)
402 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
406 long_op
= long_addr
= (!ISV86(context
) && IS_SELECTOR_32BIT(context
->SegCs
));
407 instr
= make_ptr( context
, context
->SegCs
, context
->Eip
, TRUE
);
408 if (!instr
) return FALSE
;
410 /* First handle any possible prefix */
412 segprefix
= -1; /* no prefix */
421 segprefix
= context
->SegCs
;
424 segprefix
= context
->SegSs
;
427 segprefix
= context
->SegDs
;
430 segprefix
= context
->SegEs
;
433 segprefix
= context
->SegFs
;
436 segprefix
= context
->SegGs
;
439 long_op
= !long_op
; /* opcode size prefix */
442 long_addr
= !long_addr
; /* addr size prefix */
444 case 0xf0: /* lock */
446 case 0xf2: /* repne */
449 case 0xf3: /* repe */
453 prefix
= 0; /* no more prefixes */
463 /* Now look at the actual instruction */
467 case 0x07: /* pop es */
468 case 0x17: /* pop ss */
469 case 0x1f: /* pop ds */
471 WORD seg
= *(WORD
*)get_stack( context
);
472 if (INSTR_ReplaceSelector( context
, &seg
))
476 case 0x07: context
->SegEs
= seg
; break;
477 case 0x17: context
->SegSs
= seg
; break;
478 case 0x1f: context
->SegDs
= seg
; break;
480 add_stack(context
, long_op
? 4 : 2);
481 context
->Eip
+= prefixlen
+ 1;
485 break; /* Unable to emulate it */
487 case 0x0f: /* extended instruction */
490 case 0x22: /* mov eax, crX */
493 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
494 context
->Eip
,context
->Eax
);
495 context
->Eip
+= prefixlen
+3;
498 break; /*fallthrough to bad instruction handling */
500 break; /*fallthrough to bad instruction handling */
501 case 0x20: /* mov crX, eax */
503 case 0xe0: /* mov cr4, eax */
504 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
505 * bit 0: VME Virtual Mode Exception ?
506 * bit 1: PVI Protected mode Virtual Interrupt
507 * bit 2: TSD Timestamp disable
508 * bit 3: DE Debugging extensions
509 * bit 4: PSE Page size extensions
510 * bit 5: PAE Physical address extension
511 * bit 6: MCE Machine check enable
512 * bit 7: PGE Enable global pages
513 * bit 8: PCE Enable performance counters at IPL3
515 ERR("mov cr4,eax at 0x%08lx\n",context
->Eip
);
517 context
->Eip
+= prefixlen
+3;
519 case 0xc0: /* mov cr0, eax */
520 ERR("mov cr0,eax at 0x%08lx\n",context
->Eip
);
521 context
->Eax
= 0x10; /* FIXME: set more bits ? */
522 context
->Eip
+= prefixlen
+3;
524 default: /* fallthrough to illegal instruction */
527 /* fallthrough to illegal instruction */
529 case 0xa1: /* pop fs */
531 WORD seg
= *(WORD
*)get_stack( context
);
532 if (INSTR_ReplaceSelector( context
, &seg
))
534 context
->SegFs
= seg
;
535 add_stack(context
, long_op
? 4 : 2);
536 context
->Eip
+= prefixlen
+ 2;
541 case 0xa9: /* pop gs */
543 WORD seg
= *(WORD
*)get_stack( context
);
544 if (INSTR_ReplaceSelector( context
, &seg
))
546 context
->SegGs
= seg
;
547 add_stack(context
, long_op
? 4 : 2);
548 context
->Eip
+= prefixlen
+ 2;
553 case 0xb2: /* lss addr,reg */
554 case 0xb4: /* lfs addr,reg */
555 case 0xb5: /* lgs addr,reg */
556 if (INSTR_EmulateLDS( context
, instr
, long_op
,
557 long_addr
, segprefix
, &len
))
559 context
->Eip
+= prefixlen
+ len
;
564 break; /* Unable to emulate it */
566 case 0x6c: /* insb */
567 case 0x6d: /* insw/d */
568 case 0x6e: /* outsb */
569 case 0x6f: /* outsw/d */
571 int typ
= *instr
; /* Just in case it's overwritten. */
572 int outp
= (typ
>= 0x6e);
573 unsigned long count
= repX
?
574 (long_addr
? context
->Ecx
: LOWORD(context
->Ecx
)) : 1;
575 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
576 int step
= (context
->EFlags
& 0x400) ? -opsize
: +opsize
;
577 int seg
= outp
? context
->SegDs
: context
->SegEs
; /* FIXME: is this right? */
580 /* FIXME: Check segment readable. */
583 /* FIXME: Check segment writeable. */
588 if (long_addr
) context
->Ecx
= 0;
589 else SET_LOWORD(context
->Ecx
,0);
595 WORD dx
= LOWORD(context
->Edx
);
598 data
= make_ptr( context
, seg
, context
->Esi
, long_addr
);
599 if (long_addr
) context
->Esi
+= step
;
600 else ADD_LOWORD(context
->Esi
,step
);
604 data
= make_ptr( context
, seg
, context
->Edi
, long_addr
);
605 if (long_addr
) context
->Edi
+= step
;
606 else ADD_LOWORD(context
->Edi
,step
);
612 *(BYTE
*)data
= INSTR_inport( dx
, 1, context
);
616 *(DWORD
*)data
= INSTR_inport( dx
, 4, context
);
618 *(WORD
*)data
= INSTR_inport( dx
, 2, context
);
621 INSTR_outport( dx
, 1, *(BYTE
*)data
, context
);
625 INSTR_outport( dx
, 4, *(DWORD
*)data
, context
);
627 INSTR_outport( dx
, 2, *(WORD
*)data
, context
);
631 context
->Eip
+= prefixlen
+ 1;
635 case 0x8e: /* mov XX,segment_reg */
638 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
639 long_addr
, segprefix
, &len
);
641 break; /* Unable to emulate it */
643 if (!INSTR_ReplaceSelector( context
, &seg
))
644 break; /* Unable to emulate it */
646 switch((instr
[1] >> 3) & 7)
649 context
->SegEs
= seg
;
650 context
->Eip
+= prefixlen
+ len
+ 1;
655 context
->SegSs
= seg
;
656 context
->Eip
+= prefixlen
+ len
+ 1;
659 context
->SegDs
= seg
;
660 context
->Eip
+= prefixlen
+ len
+ 1;
663 context
->SegFs
= seg
;
664 context
->Eip
+= prefixlen
+ len
+ 1;
667 context
->SegGs
= seg
;
668 context
->Eip
+= prefixlen
+ len
+ 1;
675 break; /* Unable to emulate it */
677 case 0xc4: /* les addr,reg */
678 case 0xc5: /* lds addr,reg */
679 if (INSTR_EmulateLDS( context
, instr
, long_op
,
680 long_addr
, segprefix
, &len
))
682 context
->Eip
+= prefixlen
+ len
;
685 break; /* Unable to emulate it */
687 case 0xcd: /* int <XX> */
688 if(!Dosvm
.EmulateInterruptPM
&& !DPMI_LoadDosSystem())
689 ERR("could not initialize interrupt handling\n");
691 context
->Eip
+= prefixlen
+ 2;
692 Dosvm
.EmulateInterruptPM( context
, instr
[1] );
695 break; /* Unable to emulate it */
697 case 0xcf: /* iret */
700 DWORD
*stack
= get_stack( context
);
701 context
->Eip
= *stack
++;
702 context
->SegCs
= *stack
++;
703 context
->EFlags
= *stack
;
704 add_stack(context
, 3*sizeof(DWORD
)); /* Pop the return address and flags */
708 WORD
*stack
= get_stack( context
);
709 context
->Eip
= *stack
++;
710 context
->SegCs
= *stack
++;
711 SET_LOWORD(context
->EFlags
,*stack
);
712 add_stack(context
, 3*sizeof(WORD
)); /* Pop the return address and flags */
716 case 0xe4: /* inb al,XX */
717 SET_LOBYTE(context
->Eax
,INSTR_inport( instr
[1], 1, context
));
718 context
->Eip
+= prefixlen
+ 2;
721 case 0xe5: /* in (e)ax,XX */
723 context
->Eax
= INSTR_inport( instr
[1], 4, context
);
725 SET_LOWORD(context
->Eax
, INSTR_inport( instr
[1], 2, context
));
726 context
->Eip
+= prefixlen
+ 2;
729 case 0xe6: /* outb XX,al */
730 INSTR_outport( instr
[1], 1, LOBYTE(context
->Eax
), context
);
731 context
->Eip
+= prefixlen
+ 2;
734 case 0xe7: /* out XX,(e)ax */
736 INSTR_outport( instr
[1], 4, context
->Eax
, context
);
738 INSTR_outport( instr
[1], 2, LOWORD(context
->Eax
), context
);
739 context
->Eip
+= prefixlen
+ 2;
742 case 0xec: /* inb al,dx */
743 SET_LOBYTE(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 1, context
) );
744 context
->Eip
+= prefixlen
+ 1;
747 case 0xed: /* in (e)ax,dx */
749 context
->Eax
= INSTR_inport( LOWORD(context
->Edx
), 4, context
);
751 SET_LOWORD(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 2, context
));
752 context
->Eip
+= prefixlen
+ 1;
755 case 0xee: /* outb dx,al */
756 INSTR_outport( LOWORD(context
->Edx
), 1, LOBYTE(context
->Eax
), context
);
757 context
->Eip
+= prefixlen
+ 1;
760 case 0xef: /* out dx,(e)ax */
762 INSTR_outport( LOWORD(context
->Edx
), 4, context
->Eax
, context
);
764 INSTR_outport( LOWORD(context
->Edx
), 2, LOWORD(context
->Eax
), context
);
765 context
->Eip
+= prefixlen
+ 1;
768 case 0xfa: /* cli, ignored */
769 context
->Eip
+= prefixlen
+ 1;
772 case 0xfb: /* sti, ignored */
773 context
->Eip
+= prefixlen
+ 1;
778 /* Check for Win16 __GP handler */
779 gpHandler
= HasGPHandler16( MAKESEGPTR( context
->SegCs
, context
->Eip
) );
782 WORD
*stack
= get_stack( context
);
783 *--stack
= context
->SegCs
;
784 *--stack
= context
->Eip
;
785 add_stack(context
, -2*sizeof(WORD
));
787 context
->SegCs
= SELECTOROF( gpHandler
);
788 context
->Eip
= OFFSETOF( gpHandler
);
791 return FALSE
; /* Unable to emulate it */
794 #endif /* __i386__ */