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 #include "wine/exception.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(int);
33 WINE_DECLARE_DEBUG_CHANNEL(io);
37 /* macros to set parts of a DWORD */
38 #define SET_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
39 #define SET_LOBYTE(dw,val) ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
40 #define ADD_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
42 inline static void add_stack( CONTEXT86 *context, int offset )
44 if (ISV86(context) || !IS_SELECTOR_32BIT(context->SegSs))
45 ADD_LOWORD( context->Esp, offset );
47 context->Esp += offset;
50 inline static void *make_ptr( CONTEXT86 *context, DWORD seg, DWORD off, int long_addr )
52 if (ISV86(context)) return PTR_REAL_TO_LIN( seg, off );
53 if (IS_SELECTOR_SYSTEM(seg)) return (void *)off;
54 if (!long_addr) off = LOWORD(off);
55 return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
58 inline static void *get_stack( CONTEXT86 *context )
60 if (ISV86(context)) return PTR_REAL_TO_LIN( context->SegSs, context->Esp );
61 return wine_ldt_get_ptr( context->SegSs, context->Esp );
65 /***********************************************************************
68 static DWORD CALLBACK timer_thread( void *dummy )
77 /***********************************************************************
78 * INSTR_ReplaceSelector
80 * Try to replace an invalid selector by a valid one.
81 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
82 * is the so called 'bimodal' selector 0x40, which points to the BIOS
83 * data segment. Used by (at least) Borland products (and programs compiled
84 * using Borland products).
86 * See Undocumented Windows, Chapter 5, __0040.
88 static BOOL INSTR_ReplaceSelector( CONTEXT86 *context, WORD *sel )
92 #if 0 /* hack until this is moved to kernel */
93 static WORD sys_timer = 0;
95 sys_timer = CreateSystemTimer( 55, DOSMEM_Tick );
97 static HANDLE sys_thread;
98 if (!sys_thread) sys_thread = CreateThread( NULL, 0, timer_thread, NULL, 0, NULL );
99 *sel = DOSMEM_BiosDataSeg;
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;
117 #define GET_VAL(val,type) \
118 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
121 GET_VAL( &mod, BYTE );
129 case 0: return (BYTE *)&context->Eax;
130 case 1: return (BYTE *)&context->Ecx;
131 case 2: return (BYTE *)&context->Edx;
132 case 3: return (BYTE *)&context->Ebx;
133 case 4: return (BYTE *)&context->Esp;
134 case 5: return (BYTE *)&context->Ebp;
135 case 6: return (BYTE *)&context->Esi;
136 case 7: return (BYTE *)&context->Edi;
145 GET_VAL( &sib, BYTE );
150 case 0: index = context->Eax; break;
151 case 1: index = context->Ecx; break;
152 case 2: index = context->Edx; break;
153 case 3: index = context->Ebx; break;
154 case 4: index = 0; break;
155 case 5: index = context->Ebp; break;
156 case 6: index = context->Esi; break;
157 case 7: index = context->Edi; break;
163 case 0: base = context->Eax; seg = context->SegDs; break;
164 case 1: base = context->Ecx; seg = context->SegDs; break;
165 case 2: base = context->Edx; seg = context->SegDs; break;
166 case 3: base = context->Ebx; seg = context->SegDs; break;
167 case 4: base = context->Esp; seg = context->SegSs; break;
168 case 5: base = context->Ebp; seg = context->SegSs; break;
169 case 6: base = context->Esi; seg = context->SegDs; break;
170 case 7: base = context->Edi; seg = context->SegDs; break;
175 if (rm == 5) /* special case: ds:(disp32) */
177 GET_VAL( &base, DWORD );
178 seg = context->SegDs;
182 case 1: /* 8-bit disp */
183 GET_VAL( &off, BYTE );
184 base += (signed char)off;
187 case 2: /* 32-bit disp */
188 GET_VAL( &off, DWORD );
189 base += (signed long)off;
193 else /* short address */
197 case 0: /* ds:(bx,si) */
198 base = LOWORD(context->Ebx) + LOWORD(context->Esi);
199 seg = context->SegDs;
201 case 1: /* ds:(bx,di) */
202 base = LOWORD(context->Ebx) + LOWORD(context->Edi);
203 seg = context->SegDs;
205 case 2: /* ss:(bp,si) */
206 base = LOWORD(context->Ebp) + LOWORD(context->Esi);
207 seg = context->SegSs;
209 case 3: /* ss:(bp,di) */
210 base = LOWORD(context->Ebp) + LOWORD(context->Edi);
211 seg = context->SegSs;
213 case 4: /* ds:(si) */
214 base = LOWORD(context->Esi);
215 seg = context->SegDs;
217 case 5: /* ds:(di) */
218 base = LOWORD(context->Edi);
219 seg = context->SegDs;
221 case 6: /* ss:(bp) */
222 base = LOWORD(context->Ebp);
223 seg = context->SegSs;
225 case 7: /* ds:(bx) */
226 base = LOWORD(context->Ebx);
227 seg = context->SegDs;
234 if (rm == 6) /* special case: ds:(disp16) */
236 GET_VAL( &base, WORD );
237 seg = context->SegDs;
241 case 1: /* 8-bit disp */
242 GET_VAL( &off, BYTE );
243 base += (signed char)off;
246 case 2: /* 16-bit disp */
247 GET_VAL( &off, WORD );
248 base += (signed short)off;
253 if (segprefix != -1) seg = segprefix;
255 /* Make sure the segment and offset are valid */
256 if (IS_SELECTOR_SYSTEM(seg)) return (BYTE *)(base + (index << ss));
257 if ((seg & 7) != 7) return NULL;
258 wine_ldt_get_entry( seg, &entry );
259 if (wine_ldt_is_empty( &entry )) return NULL;
260 if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
261 return (char *)wine_ldt_get_base(&entry) + base + (index << ss);
266 /***********************************************************************
269 * Emulate the LDS (and LES,LFS,etc.) instruction.
271 static BOOL INSTR_EmulateLDS( CONTEXT86 *context, BYTE *instr, int long_op,
272 int long_addr, int segprefix, int *len )
275 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
276 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
277 long_addr, segprefix, len );
279 return FALSE; /* Unable to emulate it */
280 seg = *(WORD *)(addr + (long_op ? 4 : 2));
282 if (!INSTR_ReplaceSelector( context, &seg ))
283 return FALSE; /* Unable to emulate it */
285 /* Now store the offset in the correct register */
287 switch((*regmodrm >> 3) & 7)
290 if (long_op) context->Eax = *(DWORD *)addr;
291 else SET_LOWORD(context->Eax,*(WORD *)addr);
294 if (long_op) context->Ecx = *(DWORD *)addr;
295 else SET_LOWORD(context->Ecx,*(WORD *)addr);
298 if (long_op) context->Edx = *(DWORD *)addr;
299 else SET_LOWORD(context->Edx,*(WORD *)addr);
302 if (long_op) context->Ebx = *(DWORD *)addr;
303 else SET_LOWORD(context->Ebx,*(WORD *)addr);
306 if (long_op) context->Esp = *(DWORD *)addr;
307 else SET_LOWORD(context->Esp,*(WORD *)addr);
310 if (long_op) context->Ebp = *(DWORD *)addr;
311 else SET_LOWORD(context->Ebp,*(WORD *)addr);
314 if (long_op) context->Esi = *(DWORD *)addr;
315 else SET_LOWORD(context->Esi,*(WORD *)addr);
318 if (long_op) context->Edi = *(DWORD *)addr;
319 else SET_LOWORD(context->Edi,*(WORD *)addr);
323 /* Store the correct segment in the segment register */
327 case 0xc4: context->SegEs = seg; break; /* les */
328 case 0xc5: context->SegDs = seg; break; /* lds */
329 case 0x0f: switch(instr[1])
331 case 0xb2: context->SegSs = seg; break; /* lss */
332 case 0xb4: context->SegFs = seg; break; /* lfs */
333 case 0xb5: context->SegGs = seg; break; /* lgs */
338 /* Add the opcode size to the total length */
340 *len += 1 + (*instr == 0x0f);
344 /***********************************************************************
347 * input on a I/O port
349 static DWORD INSTR_inport( WORD port, int size, CONTEXT86 *context )
351 DWORD res = IO_inport( port, size );
357 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
358 (WORD)context->SegCs, LOWORD(context->Eip));
361 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
362 (WORD)context->SegCs, LOWORD(context->Eip));
365 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port, res,
366 (WORD)context->SegCs, LOWORD(context->Eip));
374 /***********************************************************************
377 * output on a I/O port
379 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT86 *context )
381 IO_outport( port, size, val );
387 DPRINTF("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
388 (WORD)context->SegCs, LOWORD(context->Eip));
391 DPRINTF("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
392 (WORD)context->SegCs, LOWORD(context->Eip));
395 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port, val,
396 (WORD)context->SegCs, LOWORD(context->Eip));
403 /***********************************************************************
404 * INSTR_EmulateInstruction
406 * Emulate a privileged instruction.
407 * Returns exception code, or 0 if emulation successful.
409 DWORD INSTR_EmulateInstruction( CONTEXT86 *context )
411 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
413 DWORD ret = EXCEPTION_PRIV_INSTRUCTION;
415 long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
416 instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
417 if (!instr) return ret;
419 /* First handle any possible prefix */
421 segprefix = -1; /* no prefix */
430 segprefix = context->SegCs;
433 segprefix = context->SegSs;
436 segprefix = context->SegDs;
439 segprefix = context->SegEs;
442 segprefix = context->SegFs;
445 segprefix = context->SegGs;
448 long_op = !long_op; /* opcode size prefix */
451 long_addr = !long_addr; /* addr size prefix */
453 case 0xf0: /* lock */
455 case 0xf2: /* repne */
458 case 0xf3: /* repe */
462 prefix = 0; /* no more prefixes */
472 /* Now look at the actual instruction */
476 case 0x07: /* pop es */
477 case 0x17: /* pop ss */
478 case 0x1f: /* pop ds */
480 WORD seg = *(WORD *)get_stack( context );
481 if (INSTR_ReplaceSelector( context, &seg ))
485 case 0x07: context->SegEs = seg; break;
486 case 0x17: context->SegSs = seg; break;
487 case 0x1f: context->SegDs = seg; break;
489 add_stack(context, long_op ? 4 : 2);
490 context->Eip += prefixlen + 1;
494 break; /* Unable to emulate it */
496 case 0x0f: /* extended instruction */
499 case 0x22: /* mov eax, crX */
502 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
503 context->Eip,context->Eax );
504 context->Eip += prefixlen+3;
507 break; /*fallthrough to bad instruction handling */
509 break; /*fallthrough to bad instruction handling */
510 case 0x20: /* mov crX, eax */
512 case 0xe0: /* mov cr4, eax */
513 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
514 * bit 0: VME Virtual Mode Exception ?
515 * bit 1: PVI Protected mode Virtual Interrupt
516 * bit 2: TSD Timestamp disable
517 * bit 3: DE Debugging extensions
518 * bit 4: PSE Page size extensions
519 * bit 5: PAE Physical address extension
520 * bit 6: MCE Machine check enable
521 * bit 7: PGE Enable global pages
522 * bit 8: PCE Enable performance counters at IPL3
524 ERR("mov cr4,eax at 0x%08lx\n",context->Eip);
526 context->Eip += prefixlen+3;
528 case 0xc0: /* mov cr0, eax */
529 ERR("mov cr0,eax at 0x%08lx\n",context->Eip);
530 context->Eax = 0x10; /* FIXME: set more bits ? */
531 context->Eip += prefixlen+3;
533 default: /* fallthrough to illegal instruction */
536 /* fallthrough to illegal instruction */
538 case 0xa1: /* pop fs */
540 WORD seg = *(WORD *)get_stack( context );
541 if (INSTR_ReplaceSelector( context, &seg ))
543 context->SegFs = seg;
544 add_stack(context, long_op ? 4 : 2);
545 context->Eip += prefixlen + 2;
550 case 0xa9: /* pop gs */
552 WORD seg = *(WORD *)get_stack( context );
553 if (INSTR_ReplaceSelector( context, &seg ))
555 context->SegGs = seg;
556 add_stack(context, long_op ? 4 : 2);
557 context->Eip += prefixlen + 2;
562 case 0xb2: /* lss addr,reg */
563 case 0xb4: /* lfs addr,reg */
564 case 0xb5: /* lgs addr,reg */
565 if (INSTR_EmulateLDS( context, instr, long_op,
566 long_addr, segprefix, &len ))
568 context->Eip += prefixlen + len;
573 break; /* Unable to emulate it */
575 case 0x6c: /* insb */
576 case 0x6d: /* insw/d */
577 case 0x6e: /* outsb */
578 case 0x6f: /* outsw/d */
580 int typ = *instr; /* Just in case it's overwritten. */
581 int outp = (typ >= 0x6e);
582 unsigned long count = repX ?
583 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
584 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
585 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
586 int seg = outp ? context->SegDs : context->SegEs; /* FIXME: is this right? */
589 /* FIXME: Check segment readable. */
592 /* FIXME: Check segment writeable. */
597 if (long_addr) context->Ecx = 0;
598 else SET_LOWORD(context->Ecx,0);
604 WORD dx = LOWORD(context->Edx);
607 data = make_ptr( context, seg, context->Esi, long_addr );
608 if (long_addr) context->Esi += step;
609 else ADD_LOWORD(context->Esi,step);
613 data = make_ptr( context, seg, context->Edi, long_addr );
614 if (long_addr) context->Edi += step;
615 else ADD_LOWORD(context->Edi,step);
621 *(BYTE *)data = INSTR_inport( dx, 1, context );
625 *(DWORD *)data = INSTR_inport( dx, 4, context );
627 *(WORD *)data = INSTR_inport( dx, 2, context );
630 INSTR_outport( dx, 1, *(BYTE *)data, context );
634 INSTR_outport( dx, 4, *(DWORD *)data, context );
636 INSTR_outport( dx, 2, *(WORD *)data, context );
640 context->Eip += prefixlen + 1;
644 case 0x8e: /* mov XX,segment_reg */
647 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
648 long_addr, segprefix, &len );
650 break; /* Unable to emulate it */
652 if (!INSTR_ReplaceSelector( context, &seg ))
653 break; /* Unable to emulate it */
655 switch((instr[1] >> 3) & 7)
658 context->SegEs = seg;
659 context->Eip += prefixlen + len + 1;
664 context->SegSs = seg;
665 context->Eip += prefixlen + len + 1;
668 context->SegDs = seg;
669 context->Eip += prefixlen + len + 1;
672 context->SegFs = seg;
673 context->Eip += prefixlen + len + 1;
676 context->SegGs = seg;
677 context->Eip += prefixlen + len + 1;
684 break; /* Unable to emulate it */
686 case 0xc4: /* les addr,reg */
687 case 0xc5: /* lds addr,reg */
688 if (INSTR_EmulateLDS( context, instr, long_op,
689 long_addr, segprefix, &len ))
691 context->Eip += prefixlen + len;
694 break; /* Unable to emulate it */
696 case 0xcd: /* int <XX> */
697 if (IS_SELECTOR_SYSTEM(context->SegCs))
699 /* Win32 applications cannot use interrupts */
700 ret = EXCEPTION_ACCESS_VIOLATION;
703 else if (!Dosvm.EmulateInterruptPM && !DPMI_LoadDosSystem())
705 ERR("could not initialize interrupt handling\n");
709 context->Eip += prefixlen + 2;
710 Dosvm.EmulateInterruptPM( context, instr[1] );
713 break; /* Unable to emulate it */
715 case 0xcf: /* iret */
718 DWORD *stack = get_stack( context );
719 context->Eip = *stack++;
720 context->SegCs = *stack++;
721 context->EFlags = *stack;
722 add_stack(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
726 WORD *stack = get_stack( context );
727 context->Eip = *stack++;
728 context->SegCs = *stack++;
729 SET_LOWORD(context->EFlags,*stack);
730 add_stack(context, 3*sizeof(WORD)); /* Pop the return address and flags */
734 case 0xe4: /* inb al,XX */
735 SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
736 context->Eip += prefixlen + 2;
739 case 0xe5: /* in (e)ax,XX */
741 context->Eax = INSTR_inport( instr[1], 4, context );
743 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
744 context->Eip += prefixlen + 2;
747 case 0xe6: /* outb XX,al */
748 INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
749 context->Eip += prefixlen + 2;
752 case 0xe7: /* out XX,(e)ax */
754 INSTR_outport( instr[1], 4, context->Eax, context );
756 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
757 context->Eip += prefixlen + 2;
760 case 0xec: /* inb al,dx */
761 SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
762 context->Eip += prefixlen + 1;
765 case 0xed: /* in (e)ax,dx */
767 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
769 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
770 context->Eip += prefixlen + 1;
773 case 0xee: /* outb dx,al */
774 INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
775 context->Eip += prefixlen + 1;
778 case 0xef: /* out dx,(e)ax */
780 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
782 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
783 context->Eip += prefixlen + 1;
787 NtCurrentTeb()->dpmi_vif = 0;
788 context->Eip += prefixlen + 1;
792 NtCurrentTeb()->dpmi_vif = 1;
793 context->Eip += prefixlen + 1;
794 if (NtCurrentTeb()->vm86_pending)
796 NtCurrentTeb()->vm86_pending = 0;
797 return EXCEPTION_VM86_STI;
801 return ret; /* Unable to emulate it */
804 #endif /* __i386__ */