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 0xa1: /* pop fs */
564 WORD seg = *(WORD *)get_stack( context );
565 if (INSTR_ReplaceSelector( context, &seg ))
567 context->SegFs = seg;
568 add_stack(context, long_op ? 4 : 2);
569 context->Eip += prefixlen + 2;
570 return ExceptionContinueExecution;
574 case 0xa9: /* pop gs */
576 WORD seg = *(WORD *)get_stack( context );
577 if (INSTR_ReplaceSelector( context, &seg ))
579 context->SegGs = seg;
580 add_stack(context, long_op ? 4 : 2);
581 context->Eip += prefixlen + 2;
582 return ExceptionContinueExecution;
586 case 0xb2: /* lss addr,reg */
587 case 0xb4: /* lfs addr,reg */
588 case 0xb5: /* lgs addr,reg */
589 if (INSTR_EmulateLDS( context, instr, long_op,
590 long_addr, segprefix, &len ))
592 context->Eip += prefixlen + len;
593 return ExceptionContinueExecution;
597 break; /* Unable to emulate it */
599 case 0x6c: /* insb */
600 case 0x6d: /* insw/d */
601 case 0x6e: /* outsb */
602 case 0x6f: /* outsw/d */
604 int typ = *instr; /* Just in case it's overwritten. */
605 int outp = (typ >= 0x6e);
606 unsigned long count = repX ?
607 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
608 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
609 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
610 int seg = outp ? context->SegDs : context->SegEs; /* FIXME: is this right? */
613 /* FIXME: Check segment readable. */
616 /* FIXME: Check segment writeable. */
621 if (long_addr) context->Ecx = 0;
622 else SET_LOWORD(context->Ecx,0);
628 WORD dx = LOWORD(context->Edx);
631 data = make_ptr( context, seg, context->Esi, long_addr );
632 if (long_addr) context->Esi += step;
633 else ADD_LOWORD(context->Esi,step);
637 data = make_ptr( context, seg, context->Edi, long_addr );
638 if (long_addr) context->Edi += step;
639 else ADD_LOWORD(context->Edi,step);
645 *(BYTE *)data = INSTR_inport( dx, 1, context );
649 *(DWORD *)data = INSTR_inport( dx, 4, context );
651 *(WORD *)data = INSTR_inport( dx, 2, context );
654 INSTR_outport( dx, 1, *(BYTE *)data, context );
658 INSTR_outport( dx, 4, *(DWORD *)data, context );
660 INSTR_outport( dx, 2, *(WORD *)data, context );
664 context->Eip += prefixlen + 1;
666 return ExceptionContinueExecution;
668 case 0x8e: /* mov XX,segment_reg */
671 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
672 long_addr, segprefix, &len );
674 break; /* Unable to emulate it */
676 if (!INSTR_ReplaceSelector( context, &seg ))
677 break; /* Unable to emulate it */
679 switch((instr[1] >> 3) & 7)
682 context->SegEs = seg;
683 context->Eip += prefixlen + len + 1;
684 return ExceptionContinueExecution;
688 context->SegSs = seg;
689 context->Eip += prefixlen + len + 1;
690 return ExceptionContinueExecution;
692 context->SegDs = seg;
693 context->Eip += prefixlen + len + 1;
694 return ExceptionContinueExecution;
696 context->SegFs = seg;
697 context->Eip += prefixlen + len + 1;
698 return ExceptionContinueExecution;
700 context->SegGs = seg;
701 context->Eip += prefixlen + len + 1;
702 return ExceptionContinueExecution;
708 break; /* Unable to emulate it */
710 case 0xc4: /* les addr,reg */
711 case 0xc5: /* lds addr,reg */
712 if (INSTR_EmulateLDS( context, instr, long_op,
713 long_addr, segprefix, &len ))
715 context->Eip += prefixlen + len;
716 return ExceptionContinueExecution;
718 break; /* Unable to emulate it */
720 case 0xcd: /* int <XX> */
721 if (wine_ldt_is_system(context->SegCs)) break; /* don't emulate it in 32-bit code */
722 if (!DOS_EmulateInterruptPM) init_winedos();
723 if (DOS_EmulateInterruptPM)
725 context->Eip += prefixlen + 2;
726 DOS_EmulateInterruptPM( context, instr[1] );
727 return ExceptionContinueExecution;
729 break; /* Unable to emulate it */
731 case 0xcf: /* iret */
732 if (wine_ldt_is_system(context->SegCs)) break; /* don't emulate it in 32-bit code */
735 DWORD *stack = get_stack( context );
736 context->Eip = *stack++;
737 context->SegCs = *stack++;
738 context->EFlags = *stack;
739 add_stack(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
743 WORD *stack = get_stack( context );
744 context->Eip = *stack++;
745 context->SegCs = *stack++;
746 SET_LOWORD(context->EFlags,*stack);
747 add_stack(context, 3*sizeof(WORD)); /* Pop the return address and flags */
749 return ExceptionContinueExecution;
751 case 0xe4: /* inb al,XX */
752 SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
753 context->Eip += prefixlen + 2;
754 return ExceptionContinueExecution;
756 case 0xe5: /* in (e)ax,XX */
758 context->Eax = INSTR_inport( instr[1], 4, context );
760 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
761 context->Eip += prefixlen + 2;
762 return ExceptionContinueExecution;
764 case 0xe6: /* outb XX,al */
765 INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
766 context->Eip += prefixlen + 2;
767 return ExceptionContinueExecution;
769 case 0xe7: /* out XX,(e)ax */
771 INSTR_outport( instr[1], 4, context->Eax, context );
773 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
774 context->Eip += prefixlen + 2;
775 return ExceptionContinueExecution;
777 case 0xec: /* inb al,dx */
778 SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
779 context->Eip += prefixlen + 1;
780 return ExceptionContinueExecution;
782 case 0xed: /* in (e)ax,dx */
784 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
786 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
787 context->Eip += prefixlen + 1;
788 return ExceptionContinueExecution;
790 case 0xee: /* outb dx,al */
791 INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
792 context->Eip += prefixlen + 1;
793 return ExceptionContinueExecution;
795 case 0xef: /* out dx,(e)ax */
797 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
799 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
800 context->Eip += prefixlen + 1;
801 return ExceptionContinueExecution;
804 NtCurrentTeb()->dpmi_vif = 0;
805 context->Eip += prefixlen + 1;
806 return ExceptionContinueExecution;
809 NtCurrentTeb()->dpmi_vif = 1;
810 context->Eip += prefixlen + 1;
811 return ExceptionContinueExecution;
813 return ExceptionContinueSearch; /* Unable to emulate it */
817 /***********************************************************************
818 * INSTR_CallBuiltinHandler
820 void INSTR_CallBuiltinHandler( CONTEXT86 *context, BYTE intnum )
822 if (!DOS_CallBuiltinHandler) init_winedos();
823 if (DOS_CallBuiltinHandler) DOS_CallBuiltinHandler( context, intnum );
827 /***********************************************************************
828 * DOS3Call (KERNEL.102)
830 void WINAPI DOS3Call( CONTEXT86 *context )
832 INSTR_CallBuiltinHandler( context, 0x21 );
836 /***********************************************************************
837 * NetBIOSCall (KERNEL.103)
839 void WINAPI NetBIOSCall16( CONTEXT86 *context )
841 INSTR_CallBuiltinHandler( context, 0x5c );
845 /***********************************************************************
846 * GetSetKernelDOSProc (KERNEL.311)
848 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
850 FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);