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"
29 WINE_DEFAULT_DEBUG_CHANNEL(int);
30 WINE_DECLARE_DEBUG_CHANNEL(io
);
34 inline static void add_stack( CONTEXT86
*context
, int offset
)
36 if (ISV86(context
) || !IS_SELECTOR_32BIT(context
->SegSs
))
37 ADD_LOWORD( context
->Esp
, offset
);
39 context
->Esp
+= offset
;
42 inline static void *make_ptr( CONTEXT86
*context
, DWORD seg
, DWORD off
, int long_addr
)
44 if (ISV86(context
)) return PTR_REAL_TO_LIN( seg
, off
);
45 if (IS_SELECTOR_SYSTEM(seg
)) return (void *)off
;
46 if (!long_addr
) off
= LOWORD(off
);
47 return (char *) MapSL( MAKESEGPTR( seg
, 0 ) ) + off
;
50 inline static void *get_stack( CONTEXT86
*context
)
53 return PTR_REAL_TO_LIN( context
->SegSs
, context
->Esp
);
54 if (IS_SELECTOR_SYSTEM(context
->SegSs
))
55 return (void *)context
->Esp
;
56 if (IS_SELECTOR_32BIT(context
->SegSs
))
57 return (char *) MapSL( MAKESEGPTR( context
->SegSs
, 0 ) ) + context
->Esp
;
58 return MapSL( MAKESEGPTR( context
->SegSs
, LOWORD(context
->Esp
) ) );
61 /***********************************************************************
62 * INSTR_ReplaceSelector
64 * Try to replace an invalid selector by a valid one.
65 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
66 * is the so called 'bimodal' selector 0x40, which points to the BIOS
67 * data segment. Used by (at least) Borland products (and programs compiled
68 * using Borland products).
70 * See Undocumented Windows, Chapter 5, __0040.
72 static BOOL
INSTR_ReplaceSelector( CONTEXT86
*context
, WORD
*sel
)
74 extern char Call16_Start
, Call16_End
;
76 if (IS_SELECTOR_SYSTEM(context
->SegCs
))
77 if ( (char *)context
->Eip
>= &Call16_Start
78 && (char *)context
->Eip
< &Call16_End
)
80 /* Saved selector may have become invalid when the relay code */
81 /* tries to restore it. We simply clear it. */
88 static WORD sys_timer
= 0;
90 sys_timer
= CreateSystemTimer( 55, DOSMEM_Tick
);
91 *sel
= DOSMEM_BiosDataSeg
;
94 if (!IS_SELECTOR_SYSTEM(*sel
) && !IS_SELECTOR_FREE(*sel
))
95 ERR("Got protection fault on valid selector, maybe your kernel is too old?\n" );
96 return FALSE
; /* Can't replace selector, crashdump */
100 /***********************************************************************
101 * INSTR_GetOperandAddr
103 * Return the address of an instruction operand (from the mod/rm byte).
105 static BYTE
*INSTR_GetOperandAddr( CONTEXT86
*context
, BYTE
*instr
,
106 int long_addr
, int segprefix
, int *len
)
108 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) || IS_SELECTOR_FREE(seg
)) return NULL
;
251 if (wine_ldt_copy
.limit
[seg
>> 3] < (base
+ (index
<< ss
))) return NULL
;
252 return (char *) MapSL( MAKESEGPTR( seg
, 0 ) ) + base
+ (index
<< ss
);
257 /***********************************************************************
260 * Emulate the LDS (and LES,LFS,etc.) instruction.
262 static BOOL
INSTR_EmulateLDS( CONTEXT86
*context
, BYTE
*instr
, int long_op
,
263 int long_addr
, int segprefix
, int *len
)
266 BYTE
*regmodrm
= instr
+ 1 + (*instr
== 0x0f);
267 BYTE
*addr
= INSTR_GetOperandAddr( context
, regmodrm
,
268 long_addr
, segprefix
, len
);
270 return FALSE
; /* Unable to emulate it */
271 seg
= *(WORD
*)(addr
+ (long_op
? 4 : 2));
273 if (!INSTR_ReplaceSelector( context
, &seg
))
274 return FALSE
; /* Unable to emulate it */
276 /* Now store the offset in the correct register */
278 switch((*regmodrm
>> 3) & 7)
281 if (long_op
) context
->Eax
= *(DWORD
*)addr
;
282 else SET_LOWORD(context
->Eax
,*(WORD
*)addr
);
285 if (long_op
) context
->Ecx
= *(DWORD
*)addr
;
286 else SET_LOWORD(context
->Ecx
,*(WORD
*)addr
);
289 if (long_op
) context
->Edx
= *(DWORD
*)addr
;
290 else SET_LOWORD(context
->Edx
,*(WORD
*)addr
);
293 if (long_op
) context
->Ebx
= *(DWORD
*)addr
;
294 else SET_LOWORD(context
->Ebx
,*(WORD
*)addr
);
297 if (long_op
) context
->Esp
= *(DWORD
*)addr
;
298 else SET_LOWORD(context
->Esp
,*(WORD
*)addr
);
301 if (long_op
) context
->Ebp
= *(DWORD
*)addr
;
302 else SET_LOWORD(context
->Ebp
,*(WORD
*)addr
);
305 if (long_op
) context
->Esi
= *(DWORD
*)addr
;
306 else SET_LOWORD(context
->Esi
,*(WORD
*)addr
);
309 if (long_op
) context
->Edi
= *(DWORD
*)addr
;
310 else SET_LOWORD(context
->Edi
,*(WORD
*)addr
);
314 /* Store the correct segment in the segment register */
318 case 0xc4: context
->SegEs
= seg
; break; /* les */
319 case 0xc5: context
->SegDs
= seg
; break; /* lds */
320 case 0x0f: switch(instr
[1])
322 case 0xb2: context
->SegSs
= seg
; break; /* lss */
323 case 0xb4: context
->SegFs
= seg
; break; /* lfs */
324 case 0xb5: context
->SegGs
= seg
; break; /* lgs */
329 /* Add the opcode size to the total length */
331 *len
+= 1 + (*instr
== 0x0f);
335 /***********************************************************************
338 * input on a I/O port
340 static DWORD
INSTR_inport( WORD port
, int size
, CONTEXT86
*context
)
342 DWORD res
= IO_inport( port
, size
);
348 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port
, LOBYTE(res
),
349 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
352 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port
, LOWORD(res
),
353 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
356 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port
, res
,
357 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
365 /***********************************************************************
368 * output on a I/O port
370 static void INSTR_outport( WORD port
, int size
, DWORD val
, CONTEXT86
*context
)
372 IO_outport( port
, size
, val
);
378 DPRINTF("0x%x > %02x @ %04x:%04x\n", port
, LOBYTE(val
),
379 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
382 DPRINTF("0x%x > %04x @ %04x:%04x\n", port
, LOWORD(val
),
383 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
386 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port
, val
,
387 (WORD
)context
->SegCs
, LOWORD(context
->Eip
));
394 /***********************************************************************
395 * INSTR_EmulateInstruction
397 * Emulate a privileged instruction. Returns TRUE if emulation successful.
399 BOOL
INSTR_EmulateInstruction( CONTEXT86
*context
)
401 int prefix
, segprefix
, prefixlen
, len
, repX
, long_op
, long_addr
;
405 long_op
= long_addr
= (!ISV86(context
) && IS_SELECTOR_32BIT(context
->SegCs
));
406 instr
= make_ptr( context
, context
->SegCs
, context
->Eip
, TRUE
);
407 if (!instr
) return FALSE
;
409 /* First handle any possible prefix */
411 segprefix
= -1; /* no prefix */
420 segprefix
= context
->SegCs
;
423 segprefix
= context
->SegSs
;
426 segprefix
= context
->SegDs
;
429 segprefix
= context
->SegEs
;
432 segprefix
= context
->SegFs
;
435 segprefix
= context
->SegGs
;
438 long_op
= !long_op
; /* opcode size prefix */
441 long_addr
= !long_addr
; /* addr size prefix */
443 case 0xf0: /* lock */
445 case 0xf2: /* repne */
448 case 0xf3: /* repe */
452 prefix
= 0; /* no more prefixes */
462 /* Now look at the actual instruction */
466 case 0x07: /* pop es */
467 case 0x17: /* pop ss */
468 case 0x1f: /* pop ds */
470 WORD seg
= *(WORD
*)get_stack( context
);
471 if (INSTR_ReplaceSelector( context
, &seg
))
475 case 0x07: context
->SegEs
= seg
; break;
476 case 0x17: context
->SegSs
= seg
; break;
477 case 0x1f: context
->SegDs
= seg
; break;
479 add_stack(context
, long_op
? 4 : 2);
480 context
->Eip
+= prefixlen
+ 1;
484 break; /* Unable to emulate it */
486 case 0x0f: /* extended instruction */
489 case 0x22: /* mov eax, crX */
492 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
493 context
->Eip
,context
->Eax
);
494 context
->Eip
+= prefixlen
+3;
497 break; /*fallthrough to bad instruction handling */
499 break; /*fallthrough to bad instruction handling */
500 case 0x20: /* mov crX, eax */
502 case 0xe0: /* mov cr4, eax */
503 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
504 * bit 0: VME Virtual Mode Exception ?
505 * bit 1: PVI Protected mode Virtual Interrupt
506 * bit 2: TSD Timestamp disable
507 * bit 3: DE Debugging extensions
508 * bit 4: PSE Page size extensions
509 * bit 5: PAE Physical address extension
510 * bit 6: MCE Machine check enable
511 * bit 7: PGE Enable global pages
512 * bit 8: PCE Enable performance counters at IPL3
514 ERR("mov cr4,eax at 0x%08lx\n",context
->Eip
);
516 context
->Eip
+= prefixlen
+3;
518 case 0xc0: /* mov cr0, eax */
519 ERR("mov cr0,eax at 0x%08lx\n",context
->Eip
);
520 context
->Eax
= 0x10; /* FIXME: set more bits ? */
521 context
->Eip
+= prefixlen
+3;
523 default: /* fallthrough to illegal instruction */
526 /* fallthrough to illegal instruction */
528 case 0xa1: /* pop fs */
530 WORD seg
= *(WORD
*)get_stack( context
);
531 if (INSTR_ReplaceSelector( context
, &seg
))
533 context
->SegFs
= seg
;
534 add_stack(context
, long_op
? 4 : 2);
535 context
->Eip
+= prefixlen
+ 2;
540 case 0xa9: /* pop gs */
542 WORD seg
= *(WORD
*)get_stack( context
);
543 if (INSTR_ReplaceSelector( context
, &seg
))
545 context
->SegGs
= seg
;
546 add_stack(context
, long_op
? 4 : 2);
547 context
->Eip
+= prefixlen
+ 2;
552 case 0xb2: /* lss addr,reg */
553 case 0xb4: /* lfs addr,reg */
554 case 0xb5: /* lgs addr,reg */
555 if (INSTR_EmulateLDS( context
, instr
, long_op
,
556 long_addr
, segprefix
, &len
))
558 context
->Eip
+= prefixlen
+ len
;
563 break; /* Unable to emulate it */
565 case 0x6c: /* insb */
566 case 0x6d: /* insw/d */
567 case 0x6e: /* outsb */
568 case 0x6f: /* outsw/d */
570 int typ
= *instr
; /* Just in case it's overwritten. */
571 int outp
= (typ
>= 0x6e);
572 unsigned long count
= repX
?
573 (long_addr
? context
->Ecx
: LOWORD(context
->Ecx
)) : 1;
574 int opsize
= (typ
& 1) ? (long_op
? 4 : 2) : 1;
575 int step
= (context
->EFlags
& 0x400) ? -opsize
: +opsize
;
576 int seg
= outp
? context
->SegDs
: context
->SegEs
; /* FIXME: is this right? */
579 /* FIXME: Check segment readable. */
582 /* FIXME: Check segment writeable. */
587 if (long_addr
) context
->Ecx
= 0;
588 else SET_LOWORD(context
->Ecx
,0);
594 WORD dx
= LOWORD(context
->Edx
);
597 data
= make_ptr( context
, seg
, context
->Esi
, long_addr
);
598 if (long_addr
) context
->Esi
+= step
;
599 else ADD_LOWORD(context
->Esi
,step
);
603 data
= make_ptr( context
, seg
, context
->Edi
, long_addr
);
604 if (long_addr
) context
->Edi
+= step
;
605 else ADD_LOWORD(context
->Edi
,step
);
611 *(BYTE
*)data
= INSTR_inport( dx
, 1, context
);
615 *(DWORD
*)data
= INSTR_inport( dx
, 4, context
);
617 *(WORD
*)data
= INSTR_inport( dx
, 2, context
);
620 INSTR_outport( dx
, 1, *(BYTE
*)data
, context
);
624 INSTR_outport( dx
, 4, *(DWORD
*)data
, context
);
626 INSTR_outport( dx
, 2, *(WORD
*)data
, context
);
630 context
->Eip
+= prefixlen
+ 1;
634 case 0x8e: /* mov XX,segment_reg */
637 BYTE
*addr
= INSTR_GetOperandAddr(context
, instr
+ 1,
638 long_addr
, segprefix
, &len
);
640 break; /* Unable to emulate it */
642 if (!INSTR_ReplaceSelector( context
, &seg
))
643 break; /* Unable to emulate it */
645 switch((instr
[1] >> 3) & 7)
648 context
->SegEs
= seg
;
649 context
->Eip
+= prefixlen
+ len
+ 1;
654 context
->SegSs
= seg
;
655 context
->Eip
+= prefixlen
+ len
+ 1;
658 context
->SegDs
= seg
;
659 context
->Eip
+= prefixlen
+ len
+ 1;
662 context
->SegFs
= seg
;
663 context
->Eip
+= prefixlen
+ len
+ 1;
666 context
->SegGs
= seg
;
667 context
->Eip
+= prefixlen
+ len
+ 1;
674 break; /* Unable to emulate it */
676 case 0xc4: /* les addr,reg */
677 case 0xc5: /* lds addr,reg */
678 if (INSTR_EmulateLDS( context
, instr
, long_op
,
679 long_addr
, segprefix
, &len
))
681 context
->Eip
+= prefixlen
+ len
;
684 break; /* Unable to emulate it */
686 case 0xcd: /* int <XX> */
689 FARPROC48 addr
= INT_GetPMHandler48( instr
[1] );
690 DWORD
*stack
= get_stack( context
);
691 /* Push the flags and return address on the stack */
692 *(--stack
) = context
->EFlags
;
693 *(--stack
) = context
->SegCs
;
694 *(--stack
) = context
->Eip
+ prefixlen
+ 2;
695 add_stack(context
, -3 * sizeof(DWORD
));
696 /* Jump to the interrupt handler */
697 context
->SegCs
= addr
.selector
;
698 context
->Eip
= addr
.offset
;
703 FARPROC16 addr
= INT_GetPMHandler( instr
[1] );
704 WORD
*stack
= get_stack( context
);
705 /* Push the flags and return address on the stack */
706 *(--stack
) = LOWORD(context
->EFlags
);
707 *(--stack
) = context
->SegCs
;
708 *(--stack
) = LOWORD(context
->Eip
) + prefixlen
+ 2;
709 add_stack(context
, -3 * sizeof(WORD
));
710 /* Jump to the interrupt handler */
711 context
->SegCs
= HIWORD(addr
);
712 context
->Eip
= LOWORD(addr
);
716 case 0xcf: /* iret */
719 DWORD
*stack
= get_stack( context
);
720 context
->Eip
= *stack
++;
721 context
->SegCs
= *stack
++;
722 context
->EFlags
= *stack
;
723 add_stack(context
, 3*sizeof(DWORD
)); /* Pop the return address and flags */
727 WORD
*stack
= get_stack( context
);
728 context
->Eip
= *stack
++;
729 context
->SegCs
= *stack
++;
730 SET_LOWORD(context
->EFlags
,*stack
);
731 add_stack(context
, 3*sizeof(WORD
)); /* Pop the return address and flags */
735 case 0xe4: /* inb al,XX */
736 SET_LOBYTE(context
->Eax
,INSTR_inport( instr
[1], 1, context
));
737 context
->Eip
+= prefixlen
+ 2;
740 case 0xe5: /* in (e)ax,XX */
742 context
->Eax
= INSTR_inport( instr
[1], 4, context
);
744 SET_LOWORD(context
->Eax
, INSTR_inport( instr
[1], 2, context
));
745 context
->Eip
+= prefixlen
+ 2;
748 case 0xe6: /* outb XX,al */
749 INSTR_outport( instr
[1], 1, LOBYTE(context
->Eax
), context
);
750 context
->Eip
+= prefixlen
+ 2;
753 case 0xe7: /* out XX,(e)ax */
755 INSTR_outport( instr
[1], 4, context
->Eax
, context
);
757 INSTR_outport( instr
[1], 2, LOWORD(context
->Eax
), context
);
758 context
->Eip
+= prefixlen
+ 2;
761 case 0xec: /* inb al,dx */
762 SET_LOBYTE(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 1, context
) );
763 context
->Eip
+= prefixlen
+ 1;
766 case 0xed: /* in (e)ax,dx */
768 context
->Eax
= INSTR_inport( LOWORD(context
->Edx
), 4, context
);
770 SET_LOWORD(context
->Eax
, INSTR_inport( LOWORD(context
->Edx
), 2, context
));
771 context
->Eip
+= prefixlen
+ 1;
774 case 0xee: /* outb dx,al */
775 INSTR_outport( LOWORD(context
->Edx
), 1, LOBYTE(context
->Eax
), context
);
776 context
->Eip
+= prefixlen
+ 1;
779 case 0xef: /* out dx,(e)ax */
781 INSTR_outport( LOWORD(context
->Edx
), 4, context
->Eax
, context
);
783 INSTR_outport( LOWORD(context
->Edx
), 2, LOWORD(context
->Eax
), context
);
784 context
->Eip
+= prefixlen
+ 1;
787 case 0xfa: /* cli, ignored */
788 context
->Eip
+= prefixlen
+ 1;
791 case 0xfb: /* sti, ignored */
792 context
->Eip
+= prefixlen
+ 1;
797 /* Check for Win16 __GP handler */
798 gpHandler
= HasGPHandler16( MAKESEGPTR( context
->SegCs
, context
->Eip
) );
801 WORD
*stack
= get_stack( context
);
802 *--stack
= context
->SegCs
;
803 *--stack
= context
->Eip
;
804 add_stack(context
, -2*sizeof(WORD
));
806 context
->SegCs
= SELECTOROF( gpHandler
);
807 context
->Eip
= OFFSETOF( gpHandler
);
810 return FALSE
; /* Unable to emulate it */
813 #endif /* __i386__ */