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
22 #include "wine/port.h"
30 #include "wine/winuser16.h"
34 #include "wine/debug.h"
35 #include "kernel_private.h"
37 #include "wine/exception.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(int);
40 WINE_DECLARE_DEBUG_CHANNEL(io);
42 /* macros to set parts of a DWORD */
43 #define SET_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
44 #define SET_LOBYTE(dw,val) ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
45 #define ADD_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
46 #define ISV86(context) ((context)->EFlags & 0x00020000)
48 inline static void add_stack( CONTEXT86 *context, int offset )
50 if (ISV86(context) || !IS_SELECTOR_32BIT(context->SegSs))
51 ADD_LOWORD( context->Esp, offset );
53 context->Esp += offset;
56 inline static void *make_ptr( CONTEXT86 *context, DWORD seg, DWORD off, int long_addr )
58 if (ISV86(context)) return (void *)((seg << 4) + LOWORD(off));
59 if (wine_ldt_is_system(seg)) return (void *)off;
60 if (!long_addr) off = LOWORD(off);
61 return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
64 inline static void *get_stack( CONTEXT86 *context )
66 if (ISV86(context)) return (void *)((context->SegSs << 4) + LOWORD(context->Esp));
67 return wine_ldt_get_ptr( context->SegSs, context->Esp );
71 static void (WINAPI *DOS_EmulateInterruptPM)( CONTEXT86 *context, BYTE intnum );
72 static void (WINAPI *DOS_CallBuiltinHandler)( CONTEXT86 *context, BYTE intnum );
73 static DWORD (WINAPI *DOS_inport)( int port, int size );
74 static void (WINAPI *DOS_outport)( int port, int size, DWORD val );
77 static void init_winedos(void)
79 static HMODULE module;
82 module = LoadLibraryA( "winedos.dll" );
85 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
86 module = (HMODULE)1; /* don't try again */
89 #define GET_ADDR(func) DOS_##func = (void *)GetProcAddress(module, #func);
92 GET_ADDR(EmulateInterruptPM);
93 GET_ADDR(CallBuiltinHandler);
98 /***********************************************************************
99 * INSTR_ReplaceSelector
101 * Try to replace an invalid selector by a valid one.
102 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
103 * is the so called 'bimodal' selector 0x40, which points to the BIOS
104 * data segment. Used by (at least) Borland products (and programs compiled
105 * using Borland products).
107 * See Undocumented Windows, Chapter 5, __0040.
109 static BOOL INSTR_ReplaceSelector( CONTEXT86 *context, WORD *sel )
113 static WORD sys_timer = 0;
115 sys_timer = CreateSystemTimer( 55, DOSMEM_Tick );
116 *sel = DOSMEM_BiosDataSeg;
119 return FALSE; /* Can't replace selector, crashdump */
123 /***********************************************************************
124 * INSTR_GetOperandAddr
126 * Return the address of an instruction operand (from the mod/rm byte).
128 static BYTE *INSTR_GetOperandAddr( CONTEXT86 *context, BYTE *instr,
129 int long_addr, int segprefix, int *len )
131 int mod, rm, base, index = 0, ss = 0, seg = 0, off;
134 #define GET_VAL(val,type) \
135 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
138 GET_VAL( &mod, BYTE );
146 case 0: return (BYTE *)&context->Eax;
147 case 1: return (BYTE *)&context->Ecx;
148 case 2: return (BYTE *)&context->Edx;
149 case 3: return (BYTE *)&context->Ebx;
150 case 4: return (BYTE *)&context->Esp;
151 case 5: return (BYTE *)&context->Ebp;
152 case 6: return (BYTE *)&context->Esi;
153 case 7: return (BYTE *)&context->Edi;
162 GET_VAL( &sib, BYTE );
167 case 0: index = context->Eax; break;
168 case 1: index = context->Ecx; break;
169 case 2: index = context->Edx; break;
170 case 3: index = context->Ebx; break;
171 case 4: index = 0; break;
172 case 5: index = context->Ebp; break;
173 case 6: index = context->Esi; break;
174 case 7: index = context->Edi; break;
180 case 0: base = context->Eax; seg = context->SegDs; break;
181 case 1: base = context->Ecx; seg = context->SegDs; break;
182 case 2: base = context->Edx; seg = context->SegDs; break;
183 case 3: base = context->Ebx; seg = context->SegDs; break;
184 case 4: base = context->Esp; seg = context->SegSs; break;
185 case 5: base = context->Ebp; seg = context->SegSs; break;
186 case 6: base = context->Esi; seg = context->SegDs; break;
187 case 7: base = context->Edi; seg = context->SegDs; break;
192 if (rm == 5) /* special case: ds:(disp32) */
194 GET_VAL( &base, DWORD );
195 seg = context->SegDs;
199 case 1: /* 8-bit disp */
200 GET_VAL( &off, BYTE );
201 base += (signed char)off;
204 case 2: /* 32-bit disp */
205 GET_VAL( &off, DWORD );
206 base += (signed long)off;
210 else /* short address */
214 case 0: /* ds:(bx,si) */
215 base = LOWORD(context->Ebx) + LOWORD(context->Esi);
216 seg = context->SegDs;
218 case 1: /* ds:(bx,di) */
219 base = LOWORD(context->Ebx) + LOWORD(context->Edi);
220 seg = context->SegDs;
222 case 2: /* ss:(bp,si) */
223 base = LOWORD(context->Ebp) + LOWORD(context->Esi);
224 seg = context->SegSs;
226 case 3: /* ss:(bp,di) */
227 base = LOWORD(context->Ebp) + LOWORD(context->Edi);
228 seg = context->SegSs;
230 case 4: /* ds:(si) */
231 base = LOWORD(context->Esi);
232 seg = context->SegDs;
234 case 5: /* ds:(di) */
235 base = LOWORD(context->Edi);
236 seg = context->SegDs;
238 case 6: /* ss:(bp) */
239 base = LOWORD(context->Ebp);
240 seg = context->SegSs;
242 case 7: /* ds:(bx) */
243 base = LOWORD(context->Ebx);
244 seg = context->SegDs;
251 if (rm == 6) /* special case: ds:(disp16) */
253 GET_VAL( &base, WORD );
254 seg = context->SegDs;
258 case 1: /* 8-bit disp */
259 GET_VAL( &off, BYTE );
260 base += (signed char)off;
263 case 2: /* 16-bit disp */
264 GET_VAL( &off, WORD );
265 base += (signed short)off;
270 if (segprefix != -1) seg = segprefix;
272 /* Make sure the segment and offset are valid */
273 if (wine_ldt_is_system(seg)) return (BYTE *)(base + (index << ss));
274 if ((seg & 7) != 7) return NULL;
275 wine_ldt_get_entry( seg, &entry );
276 if (wine_ldt_is_empty( &entry )) return NULL;
277 if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
278 return (char *)wine_ldt_get_base(&entry) + base + (index << ss);
283 /***********************************************************************
286 * Emulate the LDS (and LES,LFS,etc.) instruction.
288 static BOOL INSTR_EmulateLDS( CONTEXT86 *context, BYTE *instr, int long_op,
289 int long_addr, int segprefix, int *len )
292 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
293 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
294 long_addr, segprefix, len );
296 return FALSE; /* Unable to emulate it */
297 seg = *(WORD *)(addr + (long_op ? 4 : 2));
299 if (!INSTR_ReplaceSelector( context, &seg ))
300 return FALSE; /* Unable to emulate it */
302 /* Now store the offset in the correct register */
304 switch((*regmodrm >> 3) & 7)
307 if (long_op) context->Eax = *(DWORD *)addr;
308 else SET_LOWORD(context->Eax,*(WORD *)addr);
311 if (long_op) context->Ecx = *(DWORD *)addr;
312 else SET_LOWORD(context->Ecx,*(WORD *)addr);
315 if (long_op) context->Edx = *(DWORD *)addr;
316 else SET_LOWORD(context->Edx,*(WORD *)addr);
319 if (long_op) context->Ebx = *(DWORD *)addr;
320 else SET_LOWORD(context->Ebx,*(WORD *)addr);
323 if (long_op) context->Esp = *(DWORD *)addr;
324 else SET_LOWORD(context->Esp,*(WORD *)addr);
327 if (long_op) context->Ebp = *(DWORD *)addr;
328 else SET_LOWORD(context->Ebp,*(WORD *)addr);
331 if (long_op) context->Esi = *(DWORD *)addr;
332 else SET_LOWORD(context->Esi,*(WORD *)addr);
335 if (long_op) context->Edi = *(DWORD *)addr;
336 else SET_LOWORD(context->Edi,*(WORD *)addr);
340 /* Store the correct segment in the segment register */
344 case 0xc4: context->SegEs = seg; break; /* les */
345 case 0xc5: context->SegDs = seg; break; /* lds */
346 case 0x0f: switch(instr[1])
348 case 0xb2: context->SegSs = seg; break; /* lss */
349 case 0xb4: context->SegFs = seg; break; /* lfs */
350 case 0xb5: context->SegGs = seg; break; /* lgs */
355 /* Add the opcode size to the total length */
357 *len += 1 + (*instr == 0x0f);
361 /***********************************************************************
364 * input on a I/O port
366 static DWORD INSTR_inport( WORD port, int size, CONTEXT86 *context )
370 if (!DOS_inport) init_winedos();
371 if (DOS_inport) res = DOS_inport( port, size );
378 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
379 (WORD)context->SegCs, LOWORD(context->Eip));
382 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
383 (WORD)context->SegCs, LOWORD(context->Eip));
386 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port, res,
387 (WORD)context->SegCs, LOWORD(context->Eip));
395 /***********************************************************************
398 * output on a I/O port
400 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT86 *context )
402 if (!DOS_outport) init_winedos();
403 if (DOS_outport) DOS_outport( port, size, val );
410 DPRINTF("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
411 (WORD)context->SegCs, LOWORD(context->Eip));
414 DPRINTF("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
415 (WORD)context->SegCs, LOWORD(context->Eip));
418 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port, val,
419 (WORD)context->SegCs, LOWORD(context->Eip));
426 /***********************************************************************
427 * INSTR_EmulateInstruction
429 * Emulate a privileged instruction.
430 * Returns exception continuation status.
432 DWORD INSTR_EmulateInstruction( EXCEPTION_RECORD *rec, CONTEXT86 *context )
434 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
437 long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
438 instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
439 if (!instr) return ExceptionContinueSearch;
441 /* First handle any possible prefix */
443 segprefix = -1; /* no prefix */
452 segprefix = context->SegCs;
455 segprefix = context->SegSs;
458 segprefix = context->SegDs;
461 segprefix = context->SegEs;
464 segprefix = context->SegFs;
467 segprefix = context->SegGs;
470 long_op = !long_op; /* opcode size prefix */
473 long_addr = !long_addr; /* addr size prefix */
475 case 0xf0: /* lock */
477 case 0xf2: /* repne */
480 case 0xf3: /* repe */
484 prefix = 0; /* no more prefixes */
494 /* Now look at the actual instruction */
498 case 0x07: /* pop es */
499 case 0x17: /* pop ss */
500 case 0x1f: /* pop ds */
502 WORD seg = *(WORD *)get_stack( context );
503 if (INSTR_ReplaceSelector( context, &seg ))
507 case 0x07: context->SegEs = seg; break;
508 case 0x17: context->SegSs = seg; break;
509 case 0x1f: context->SegDs = seg; break;
511 add_stack(context, long_op ? 4 : 2);
512 context->Eip += prefixlen + 1;
513 return ExceptionContinueExecution;
516 break; /* Unable to emulate it */
518 case 0x0f: /* extended instruction */
521 case 0x22: /* mov eax, crX */
525 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
526 context->Eip,context->Eax );
527 context->Eip += prefixlen+3;
528 return ExceptionContinueExecution;
530 break; /*fallthrough to bad instruction handling */
532 break; /*fallthrough to bad instruction handling */
533 case 0x20: /* mov crX, eax */
536 case 0xe0: /* mov cr4, eax */
537 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
538 * bit 0: VME Virtual Mode Exception ?
539 * bit 1: PVI Protected mode Virtual Interrupt
540 * bit 2: TSD Timestamp disable
541 * bit 3: DE Debugging extensions
542 * bit 4: PSE Page size extensions
543 * bit 5: PAE Physical address extension
544 * bit 6: MCE Machine check enable
545 * bit 7: PGE Enable global pages
546 * bit 8: PCE Enable performance counters at IPL3
548 ERR("mov cr4,eax at 0x%08lx\n",context->Eip);
550 context->Eip += prefixlen+3;
551 return ExceptionContinueExecution;
552 case 0xc0: /* mov cr0, eax */
553 ERR("mov cr0,eax at 0x%08lx\n",context->Eip);
554 context->Eax = 0x10; /* FIXME: set more bits ? */
555 context->Eip += prefixlen+3;
556 return ExceptionContinueExecution;
557 default: /* fallthrough to illegal instruction */
560 /* fallthrough to illegal instruction */
562 case 0x21: /* mov drX, eax */
565 case 0xf8: /* mov dr7, eax */
566 TRACE("mov dr7,eax at 0x%08lx\n",context->Eip);
567 context->Eax = 0x400;
568 context->Eip += prefixlen+3;
569 return ExceptionContinueExecution;
570 default: /* fallthrough to illegal instruction */
571 ERR("Unknown DR register, eip+2 is %02x\n", instr[2]);
574 /* fallthrough to illegal instruction */
576 case 0xa1: /* pop fs */
578 WORD seg = *(WORD *)get_stack( context );
579 if (INSTR_ReplaceSelector( context, &seg ))
581 context->SegFs = seg;
582 add_stack(context, long_op ? 4 : 2);
583 context->Eip += prefixlen + 2;
584 return ExceptionContinueExecution;
588 case 0xa9: /* pop gs */
590 WORD seg = *(WORD *)get_stack( context );
591 if (INSTR_ReplaceSelector( context, &seg ))
593 context->SegGs = seg;
594 add_stack(context, long_op ? 4 : 2);
595 context->Eip += prefixlen + 2;
596 return ExceptionContinueExecution;
600 case 0xb2: /* lss addr,reg */
601 case 0xb4: /* lfs addr,reg */
602 case 0xb5: /* lgs addr,reg */
603 if (INSTR_EmulateLDS( context, instr, long_op,
604 long_addr, segprefix, &len ))
606 context->Eip += prefixlen + len;
607 return ExceptionContinueExecution;
611 break; /* Unable to emulate it */
613 case 0x6c: /* insb */
614 case 0x6d: /* insw/d */
615 case 0x6e: /* outsb */
616 case 0x6f: /* outsw/d */
618 int typ = *instr; /* Just in case it's overwritten. */
619 int outp = (typ >= 0x6e);
620 unsigned long count = repX ?
621 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
622 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
623 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
624 int seg = outp ? context->SegDs : context->SegEs; /* FIXME: is this right? */
627 /* FIXME: Check segment readable. */
630 /* FIXME: Check segment writeable. */
635 if (long_addr) context->Ecx = 0;
636 else SET_LOWORD(context->Ecx,0);
642 WORD dx = LOWORD(context->Edx);
645 data = make_ptr( context, seg, context->Esi, long_addr );
646 if (long_addr) context->Esi += step;
647 else ADD_LOWORD(context->Esi,step);
651 data = make_ptr( context, seg, context->Edi, long_addr );
652 if (long_addr) context->Edi += step;
653 else ADD_LOWORD(context->Edi,step);
659 *(BYTE *)data = INSTR_inport( dx, 1, context );
663 *(DWORD *)data = INSTR_inport( dx, 4, context );
665 *(WORD *)data = INSTR_inport( dx, 2, context );
668 INSTR_outport( dx, 1, *(BYTE *)data, context );
672 INSTR_outport( dx, 4, *(DWORD *)data, context );
674 INSTR_outport( dx, 2, *(WORD *)data, context );
678 context->Eip += prefixlen + 1;
680 return ExceptionContinueExecution;
682 case 0x8e: /* mov XX,segment_reg */
685 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
686 long_addr, segprefix, &len );
688 break; /* Unable to emulate it */
690 if (!INSTR_ReplaceSelector( context, &seg ))
691 break; /* Unable to emulate it */
693 switch((instr[1] >> 3) & 7)
696 context->SegEs = seg;
697 context->Eip += prefixlen + len + 1;
698 return ExceptionContinueExecution;
702 context->SegSs = seg;
703 context->Eip += prefixlen + len + 1;
704 return ExceptionContinueExecution;
706 context->SegDs = seg;
707 context->Eip += prefixlen + len + 1;
708 return ExceptionContinueExecution;
710 context->SegFs = seg;
711 context->Eip += prefixlen + len + 1;
712 return ExceptionContinueExecution;
714 context->SegGs = seg;
715 context->Eip += prefixlen + len + 1;
716 return ExceptionContinueExecution;
722 break; /* Unable to emulate it */
724 case 0xc4: /* les addr,reg */
725 case 0xc5: /* lds addr,reg */
726 if (INSTR_EmulateLDS( context, instr, long_op,
727 long_addr, segprefix, &len ))
729 context->Eip += prefixlen + len;
730 return ExceptionContinueExecution;
732 break; /* Unable to emulate it */
734 case 0xcd: /* int <XX> */
735 if (wine_ldt_is_system(context->SegCs)) break; /* don't emulate it in 32-bit code */
736 if (!DOS_EmulateInterruptPM) init_winedos();
737 if (DOS_EmulateInterruptPM)
739 context->Eip += prefixlen + 2;
740 DOS_EmulateInterruptPM( context, instr[1] );
741 return ExceptionContinueExecution;
743 break; /* Unable to emulate it */
745 case 0xcf: /* iret */
746 if (wine_ldt_is_system(context->SegCs)) break; /* don't emulate it in 32-bit code */
749 DWORD *stack = get_stack( context );
750 context->Eip = *stack++;
751 context->SegCs = *stack++;
752 context->EFlags = *stack;
753 add_stack(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
757 WORD *stack = get_stack( context );
758 context->Eip = *stack++;
759 context->SegCs = *stack++;
760 SET_LOWORD(context->EFlags,*stack);
761 add_stack(context, 3*sizeof(WORD)); /* Pop the return address and flags */
763 return ExceptionContinueExecution;
765 case 0xe4: /* inb al,XX */
766 SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
767 context->Eip += prefixlen + 2;
768 return ExceptionContinueExecution;
770 case 0xe5: /* in (e)ax,XX */
772 context->Eax = INSTR_inport( instr[1], 4, context );
774 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
775 context->Eip += prefixlen + 2;
776 return ExceptionContinueExecution;
778 case 0xe6: /* outb XX,al */
779 INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
780 context->Eip += prefixlen + 2;
781 return ExceptionContinueExecution;
783 case 0xe7: /* out XX,(e)ax */
785 INSTR_outport( instr[1], 4, context->Eax, context );
787 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
788 context->Eip += prefixlen + 2;
789 return ExceptionContinueExecution;
791 case 0xec: /* inb al,dx */
792 SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
793 context->Eip += prefixlen + 1;
794 return ExceptionContinueExecution;
796 case 0xed: /* in (e)ax,dx */
798 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
800 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
801 context->Eip += prefixlen + 1;
802 return ExceptionContinueExecution;
804 case 0xee: /* outb dx,al */
805 INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
806 context->Eip += prefixlen + 1;
807 return ExceptionContinueExecution;
809 case 0xef: /* out dx,(e)ax */
811 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
813 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
814 context->Eip += prefixlen + 1;
815 return ExceptionContinueExecution;
818 NtCurrentTeb()->dpmi_vif = 0;
819 context->Eip += prefixlen + 1;
820 return ExceptionContinueExecution;
823 NtCurrentTeb()->dpmi_vif = 1;
824 context->Eip += prefixlen + 1;
825 return ExceptionContinueExecution;
827 return ExceptionContinueSearch; /* Unable to emulate it */
831 /***********************************************************************
832 * INSTR_CallBuiltinHandler
834 void INSTR_CallBuiltinHandler( CONTEXT86 *context, BYTE intnum )
836 if (!DOS_CallBuiltinHandler) init_winedos();
837 if (DOS_CallBuiltinHandler) DOS_CallBuiltinHandler( context, intnum );
841 /***********************************************************************
842 * DOS3Call (KERNEL.102)
844 void WINAPI DOS3Call( CONTEXT86 *context )
846 INSTR_CallBuiltinHandler( context, 0x21 );
850 /***********************************************************************
851 * NetBIOSCall (KERNEL.103)
853 void WINAPI NetBIOSCall16( CONTEXT86 *context )
855 INSTR_CallBuiltinHandler( context, 0x5c );
859 /***********************************************************************
860 * GetSetKernelDOSProc (KERNEL.311)
862 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
864 FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);