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 )
59 return PTR_REAL_TO_LIN( context->SegSs, context->Esp );
60 if (IS_SELECTOR_SYSTEM(context->SegSs))
61 return (void *)context->Esp;
62 if (IS_SELECTOR_32BIT(context->SegSs))
63 return (char *) MapSL( MAKESEGPTR( context->SegSs, 0 ) ) + context->Esp;
64 return MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Esp) ) );
67 /***********************************************************************
68 * INSTR_ReplaceSelector
70 * Try to replace an invalid selector by a valid one.
71 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
72 * is the so called 'bimodal' selector 0x40, which points to the BIOS
73 * data segment. Used by (at least) Borland products (and programs compiled
74 * using Borland products).
76 * See Undocumented Windows, Chapter 5, __0040.
78 static BOOL INSTR_ReplaceSelector( CONTEXT86 *context, WORD *sel )
80 extern char Call16_Start, Call16_End;
82 if (IS_SELECTOR_SYSTEM(context->SegCs))
83 if ( (char *)context->Eip >= &Call16_Start
84 && (char *)context->Eip < &Call16_End )
86 /* Saved selector may have become invalid when the relay code */
87 /* tries to restore it. We simply clear it. */
94 static WORD sys_timer = 0;
96 sys_timer = CreateSystemTimer( 55, DOSMEM_Tick );
97 *sel = DOSMEM_BiosDataSeg;
100 if (!IS_SELECTOR_SYSTEM(*sel) && !IS_SELECTOR_FREE(*sel))
101 ERR("Got protection fault on valid selector, maybe your kernel is too old?\n" );
102 return FALSE; /* Can't replace selector, crashdump */
106 /***********************************************************************
107 * INSTR_GetOperandAddr
109 * Return the address of an instruction operand (from the mod/rm byte).
111 static BYTE *INSTR_GetOperandAddr( CONTEXT86 *context, BYTE *instr,
112 int long_addr, int segprefix, int *len )
114 int mod, rm, base, index = 0, ss = 0, seg = 0, off;
116 #define GET_VAL(val,type) \
117 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
120 GET_VAL( &mod, BYTE );
128 case 0: return (BYTE *)&context->Eax;
129 case 1: return (BYTE *)&context->Ecx;
130 case 2: return (BYTE *)&context->Edx;
131 case 3: return (BYTE *)&context->Ebx;
132 case 4: return (BYTE *)&context->Esp;
133 case 5: return (BYTE *)&context->Ebp;
134 case 6: return (BYTE *)&context->Esi;
135 case 7: return (BYTE *)&context->Edi;
144 GET_VAL( &sib, BYTE );
149 case 0: index = context->Eax; break;
150 case 1: index = context->Ecx; break;
151 case 2: index = context->Edx; break;
152 case 3: index = context->Ebx; break;
153 case 4: index = 0; break;
154 case 5: index = context->Ebp; break;
155 case 6: index = context->Esi; break;
156 case 7: index = context->Edi; break;
162 case 0: base = context->Eax; seg = context->SegDs; break;
163 case 1: base = context->Ecx; seg = context->SegDs; break;
164 case 2: base = context->Edx; seg = context->SegDs; break;
165 case 3: base = context->Ebx; seg = context->SegDs; break;
166 case 4: base = context->Esp; seg = context->SegSs; break;
167 case 5: base = context->Ebp; seg = context->SegSs; break;
168 case 6: base = context->Esi; seg = context->SegDs; break;
169 case 7: base = context->Edi; seg = context->SegDs; break;
174 if (rm == 5) /* special case: ds:(disp32) */
176 GET_VAL( &base, DWORD );
177 seg = context->SegDs;
181 case 1: /* 8-bit disp */
182 GET_VAL( &off, BYTE );
183 base += (signed char)off;
186 case 2: /* 32-bit disp */
187 GET_VAL( &off, DWORD );
188 base += (signed long)off;
192 else /* short address */
196 case 0: /* ds:(bx,si) */
197 base = LOWORD(context->Ebx) + LOWORD(context->Esi);
198 seg = context->SegDs;
200 case 1: /* ds:(bx,di) */
201 base = LOWORD(context->Ebx) + LOWORD(context->Edi);
202 seg = context->SegDs;
204 case 2: /* ss:(bp,si) */
205 base = LOWORD(context->Ebp) + LOWORD(context->Esi);
206 seg = context->SegSs;
208 case 3: /* ss:(bp,di) */
209 base = LOWORD(context->Ebp) + LOWORD(context->Edi);
210 seg = context->SegSs;
212 case 4: /* ds:(si) */
213 base = LOWORD(context->Esi);
214 seg = context->SegDs;
216 case 5: /* ds:(di) */
217 base = LOWORD(context->Edi);
218 seg = context->SegDs;
220 case 6: /* ss:(bp) */
221 base = LOWORD(context->Ebp);
222 seg = context->SegSs;
224 case 7: /* ds:(bx) */
225 base = LOWORD(context->Ebx);
226 seg = context->SegDs;
233 if (rm == 6) /* special case: ds:(disp16) */
235 GET_VAL( &base, WORD );
236 seg = context->SegDs;
240 case 1: /* 8-bit disp */
241 GET_VAL( &off, BYTE );
242 base += (signed char)off;
245 case 2: /* 16-bit disp */
246 GET_VAL( &off, WORD );
247 base += (signed short)off;
252 if (segprefix != -1) seg = segprefix;
254 /* Make sure the segment and offset are valid */
255 if (IS_SELECTOR_SYSTEM(seg)) return (BYTE *)(base + (index << ss));
256 if (((seg & 7) != 7) || IS_SELECTOR_FREE(seg)) return NULL;
257 if (wine_ldt_copy.limit[seg >> 3] < (base + (index << ss))) return NULL;
258 return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + base + (index << ss);
263 /***********************************************************************
266 * Emulate the LDS (and LES,LFS,etc.) instruction.
268 static BOOL INSTR_EmulateLDS( CONTEXT86 *context, BYTE *instr, int long_op,
269 int long_addr, int segprefix, int *len )
272 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
273 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
274 long_addr, segprefix, len );
276 return FALSE; /* Unable to emulate it */
277 seg = *(WORD *)(addr + (long_op ? 4 : 2));
279 if (!INSTR_ReplaceSelector( context, &seg ))
280 return FALSE; /* Unable to emulate it */
282 /* Now store the offset in the correct register */
284 switch((*regmodrm >> 3) & 7)
287 if (long_op) context->Eax = *(DWORD *)addr;
288 else SET_LOWORD(context->Eax,*(WORD *)addr);
291 if (long_op) context->Ecx = *(DWORD *)addr;
292 else SET_LOWORD(context->Ecx,*(WORD *)addr);
295 if (long_op) context->Edx = *(DWORD *)addr;
296 else SET_LOWORD(context->Edx,*(WORD *)addr);
299 if (long_op) context->Ebx = *(DWORD *)addr;
300 else SET_LOWORD(context->Ebx,*(WORD *)addr);
303 if (long_op) context->Esp = *(DWORD *)addr;
304 else SET_LOWORD(context->Esp,*(WORD *)addr);
307 if (long_op) context->Ebp = *(DWORD *)addr;
308 else SET_LOWORD(context->Ebp,*(WORD *)addr);
311 if (long_op) context->Esi = *(DWORD *)addr;
312 else SET_LOWORD(context->Esi,*(WORD *)addr);
315 if (long_op) context->Edi = *(DWORD *)addr;
316 else SET_LOWORD(context->Edi,*(WORD *)addr);
320 /* Store the correct segment in the segment register */
324 case 0xc4: context->SegEs = seg; break; /* les */
325 case 0xc5: context->SegDs = seg; break; /* lds */
326 case 0x0f: switch(instr[1])
328 case 0xb2: context->SegSs = seg; break; /* lss */
329 case 0xb4: context->SegFs = seg; break; /* lfs */
330 case 0xb5: context->SegGs = seg; break; /* lgs */
335 /* Add the opcode size to the total length */
337 *len += 1 + (*instr == 0x0f);
341 /***********************************************************************
344 * input on a I/O port
346 static DWORD INSTR_inport( WORD port, int size, CONTEXT86 *context )
348 DWORD res = IO_inport( port, size );
354 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
355 (WORD)context->SegCs, LOWORD(context->Eip));
358 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
359 (WORD)context->SegCs, LOWORD(context->Eip));
362 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port, res,
363 (WORD)context->SegCs, LOWORD(context->Eip));
371 /***********************************************************************
374 * output on a I/O port
376 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT86 *context )
378 IO_outport( port, size, val );
384 DPRINTF("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
385 (WORD)context->SegCs, LOWORD(context->Eip));
388 DPRINTF("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
389 (WORD)context->SegCs, LOWORD(context->Eip));
392 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port, val,
393 (WORD)context->SegCs, LOWORD(context->Eip));
400 /***********************************************************************
401 * INSTR_EmulateInstruction
403 * Emulate a privileged instruction. Returns TRUE if emulation successful.
405 BOOL INSTR_EmulateInstruction( CONTEXT86 *context )
407 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
411 long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
412 instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
413 if (!instr) return FALSE;
415 /* First handle any possible prefix */
417 segprefix = -1; /* no prefix */
426 segprefix = context->SegCs;
429 segprefix = context->SegSs;
432 segprefix = context->SegDs;
435 segprefix = context->SegEs;
438 segprefix = context->SegFs;
441 segprefix = context->SegGs;
444 long_op = !long_op; /* opcode size prefix */
447 long_addr = !long_addr; /* addr size prefix */
449 case 0xf0: /* lock */
451 case 0xf2: /* repne */
454 case 0xf3: /* repe */
458 prefix = 0; /* no more prefixes */
468 /* Now look at the actual instruction */
472 case 0x07: /* pop es */
473 case 0x17: /* pop ss */
474 case 0x1f: /* pop ds */
476 WORD seg = *(WORD *)get_stack( context );
477 if (INSTR_ReplaceSelector( context, &seg ))
481 case 0x07: context->SegEs = seg; break;
482 case 0x17: context->SegSs = seg; break;
483 case 0x1f: context->SegDs = seg; break;
485 add_stack(context, long_op ? 4 : 2);
486 context->Eip += prefixlen + 1;
490 break; /* Unable to emulate it */
492 case 0x0f: /* extended instruction */
495 case 0x22: /* mov eax, crX */
498 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
499 context->Eip,context->Eax );
500 context->Eip += prefixlen+3;
503 break; /*fallthrough to bad instruction handling */
505 break; /*fallthrough to bad instruction handling */
506 case 0x20: /* mov crX, eax */
508 case 0xe0: /* mov cr4, eax */
509 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
510 * bit 0: VME Virtual Mode Exception ?
511 * bit 1: PVI Protected mode Virtual Interrupt
512 * bit 2: TSD Timestamp disable
513 * bit 3: DE Debugging extensions
514 * bit 4: PSE Page size extensions
515 * bit 5: PAE Physical address extension
516 * bit 6: MCE Machine check enable
517 * bit 7: PGE Enable global pages
518 * bit 8: PCE Enable performance counters at IPL3
520 ERR("mov cr4,eax at 0x%08lx\n",context->Eip);
522 context->Eip += prefixlen+3;
524 case 0xc0: /* mov cr0, eax */
525 ERR("mov cr0,eax at 0x%08lx\n",context->Eip);
526 context->Eax = 0x10; /* FIXME: set more bits ? */
527 context->Eip += prefixlen+3;
529 default: /* fallthrough to illegal instruction */
532 /* fallthrough to illegal instruction */
534 case 0xa1: /* pop fs */
536 WORD seg = *(WORD *)get_stack( context );
537 if (INSTR_ReplaceSelector( context, &seg ))
539 context->SegFs = seg;
540 add_stack(context, long_op ? 4 : 2);
541 context->Eip += prefixlen + 2;
546 case 0xa9: /* pop gs */
548 WORD seg = *(WORD *)get_stack( context );
549 if (INSTR_ReplaceSelector( context, &seg ))
551 context->SegGs = seg;
552 add_stack(context, long_op ? 4 : 2);
553 context->Eip += prefixlen + 2;
558 case 0xb2: /* lss addr,reg */
559 case 0xb4: /* lfs addr,reg */
560 case 0xb5: /* lgs addr,reg */
561 if (INSTR_EmulateLDS( context, instr, long_op,
562 long_addr, segprefix, &len ))
564 context->Eip += prefixlen + len;
569 break; /* Unable to emulate it */
571 case 0x6c: /* insb */
572 case 0x6d: /* insw/d */
573 case 0x6e: /* outsb */
574 case 0x6f: /* outsw/d */
576 int typ = *instr; /* Just in case it's overwritten. */
577 int outp = (typ >= 0x6e);
578 unsigned long count = repX ?
579 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
580 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
581 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
582 int seg = outp ? context->SegDs : context->SegEs; /* FIXME: is this right? */
585 /* FIXME: Check segment readable. */
588 /* FIXME: Check segment writeable. */
593 if (long_addr) context->Ecx = 0;
594 else SET_LOWORD(context->Ecx,0);
600 WORD dx = LOWORD(context->Edx);
603 data = make_ptr( context, seg, context->Esi, long_addr );
604 if (long_addr) context->Esi += step;
605 else ADD_LOWORD(context->Esi,step);
609 data = make_ptr( context, seg, context->Edi, long_addr );
610 if (long_addr) context->Edi += step;
611 else ADD_LOWORD(context->Edi,step);
617 *(BYTE *)data = INSTR_inport( dx, 1, context );
621 *(DWORD *)data = INSTR_inport( dx, 4, context );
623 *(WORD *)data = INSTR_inport( dx, 2, context );
626 INSTR_outport( dx, 1, *(BYTE *)data, context );
630 INSTR_outport( dx, 4, *(DWORD *)data, context );
632 INSTR_outport( dx, 2, *(WORD *)data, context );
636 context->Eip += prefixlen + 1;
640 case 0x8e: /* mov XX,segment_reg */
643 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
644 long_addr, segprefix, &len );
646 break; /* Unable to emulate it */
648 if (!INSTR_ReplaceSelector( context, &seg ))
649 break; /* Unable to emulate it */
651 switch((instr[1] >> 3) & 7)
654 context->SegEs = seg;
655 context->Eip += prefixlen + len + 1;
660 context->SegSs = seg;
661 context->Eip += prefixlen + len + 1;
664 context->SegDs = seg;
665 context->Eip += prefixlen + len + 1;
668 context->SegFs = seg;
669 context->Eip += prefixlen + len + 1;
672 context->SegGs = seg;
673 context->Eip += prefixlen + len + 1;
680 break; /* Unable to emulate it */
682 case 0xc4: /* les addr,reg */
683 case 0xc5: /* lds addr,reg */
684 if (INSTR_EmulateLDS( context, instr, long_op,
685 long_addr, segprefix, &len ))
687 context->Eip += prefixlen + len;
690 break; /* Unable to emulate it */
692 case 0xcd: /* int <XX> */
693 if(!Dosvm.EmulateInterruptPM && !DPMI_LoadDosSystem())
694 ERR("could not initialize interrupt handling\n");
696 context->Eip += prefixlen + 2;
697 Dosvm.EmulateInterruptPM( context, instr[1] );
700 break; /* Unable to emulate it */
702 case 0xcf: /* iret */
705 DWORD *stack = get_stack( context );
706 context->Eip = *stack++;
707 context->SegCs = *stack++;
708 context->EFlags = *stack;
709 add_stack(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
713 WORD *stack = get_stack( context );
714 context->Eip = *stack++;
715 context->SegCs = *stack++;
716 SET_LOWORD(context->EFlags,*stack);
717 add_stack(context, 3*sizeof(WORD)); /* Pop the return address and flags */
721 case 0xe4: /* inb al,XX */
722 SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
723 context->Eip += prefixlen + 2;
726 case 0xe5: /* in (e)ax,XX */
728 context->Eax = INSTR_inport( instr[1], 4, context );
730 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
731 context->Eip += prefixlen + 2;
734 case 0xe6: /* outb XX,al */
735 INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
736 context->Eip += prefixlen + 2;
739 case 0xe7: /* out XX,(e)ax */
741 INSTR_outport( instr[1], 4, context->Eax, context );
743 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
744 context->Eip += prefixlen + 2;
747 case 0xec: /* inb al,dx */
748 SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
749 context->Eip += prefixlen + 1;
752 case 0xed: /* in (e)ax,dx */
754 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
756 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
757 context->Eip += prefixlen + 1;
760 case 0xee: /* outb dx,al */
761 INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
762 context->Eip += prefixlen + 1;
765 case 0xef: /* out dx,(e)ax */
767 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
769 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
770 context->Eip += prefixlen + 1;
773 case 0xfa: /* cli, ignored */
774 context->Eip += prefixlen + 1;
777 case 0xfb: /* sti, ignored */
778 context->Eip += prefixlen + 1;
783 /* Check for Win16 __GP handler */
784 gpHandler = HasGPHandler16( MAKESEGPTR( context->SegCs, context->Eip ) );
787 WORD *stack = get_stack( context );
788 *--stack = context->SegCs;
789 *--stack = context->Eip;
790 add_stack(context, -2*sizeof(WORD));
792 context->SegCs = SELECTOROF( gpHandler );
793 context->Eip = OFFSETOF( gpHandler );
796 return FALSE; /* Unable to emulate it */
799 #endif /* __i386__ */