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
27 #include "wine/winuser16.h"
31 #include "wine/debug.h"
32 #include "kernel_private.h"
34 #include "wine/exception.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(int);
37 WINE_DECLARE_DEBUG_CHANNEL(io);
39 /* macros to set parts of a DWORD */
40 #define SET_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
41 #define SET_LOBYTE(dw,val) ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
42 #define ADD_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
43 #define ISV86(context) ((context)->EFlags & 0x00020000)
45 inline static void add_stack( CONTEXT86 *context, int offset )
47 if (ISV86(context) || !IS_SELECTOR_32BIT(context->SegSs))
48 ADD_LOWORD( context->Esp, offset );
50 context->Esp += offset;
53 inline static void *make_ptr( CONTEXT86 *context, DWORD seg, DWORD off, int long_addr )
55 if (ISV86(context)) return (void *)((seg << 4) + LOWORD(off));
56 if (wine_ldt_is_system(seg)) return (void *)off;
57 if (!long_addr) off = LOWORD(off);
58 return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
61 inline static void *get_stack( CONTEXT86 *context )
63 if (ISV86(context)) return (void *)((context->SegSs << 4) + LOWORD(context->Esp));
64 return wine_ldt_get_ptr( context->SegSs, context->Esp );
68 static void (WINAPI *DOS_EmulateInterruptPM)( CONTEXT86 *context, BYTE intnum );
69 static void (WINAPI *DOS_CallBuiltinHandler)( CONTEXT86 *context, BYTE intnum );
70 static DWORD (WINAPI *DOS_inport)( int port, int size );
71 static void (WINAPI *DOS_outport)( int port, int size, DWORD val );
74 static void init_winedos(void)
76 static HMODULE module;
79 module = LoadLibraryA( "winedos.dll" );
82 ERR("could not load winedos.dll, DOS subsystem unavailable\n");
83 module = (HMODULE)1; /* don't try again */
86 #define GET_ADDR(func) DOS_##func = (void *)GetProcAddress(module, #func);
89 GET_ADDR(EmulateInterruptPM);
90 GET_ADDR(CallBuiltinHandler);
95 /***********************************************************************
96 * INSTR_ReplaceSelector
98 * Try to replace an invalid selector by a valid one.
99 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
100 * is the so called 'bimodal' selector 0x40, which points to the BIOS
101 * data segment. Used by (at least) Borland products (and programs compiled
102 * using Borland products).
104 * See Undocumented Windows, Chapter 5, __0040.
106 static BOOL INSTR_ReplaceSelector( CONTEXT86 *context, WORD *sel )
110 static WORD sys_timer = 0;
112 sys_timer = CreateSystemTimer( 55, DOSMEM_Tick );
113 *sel = DOSMEM_BiosDataSeg;
116 return FALSE; /* Can't replace selector, crashdump */
120 /***********************************************************************
121 * INSTR_GetOperandAddr
123 * Return the address of an instruction operand (from the mod/rm byte).
125 static BYTE *INSTR_GetOperandAddr( CONTEXT86 *context, BYTE *instr,
126 int long_addr, int segprefix, int *len )
128 int mod, rm, base, index = 0, ss = 0, seg = 0, off;
131 #define GET_VAL(val,type) \
132 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
135 GET_VAL( &mod, BYTE );
143 case 0: return (BYTE *)&context->Eax;
144 case 1: return (BYTE *)&context->Ecx;
145 case 2: return (BYTE *)&context->Edx;
146 case 3: return (BYTE *)&context->Ebx;
147 case 4: return (BYTE *)&context->Esp;
148 case 5: return (BYTE *)&context->Ebp;
149 case 6: return (BYTE *)&context->Esi;
150 case 7: return (BYTE *)&context->Edi;
159 GET_VAL( &sib, BYTE );
164 case 0: index = context->Eax; break;
165 case 1: index = context->Ecx; break;
166 case 2: index = context->Edx; break;
167 case 3: index = context->Ebx; break;
168 case 4: index = 0; break;
169 case 5: index = context->Ebp; break;
170 case 6: index = context->Esi; break;
171 case 7: index = context->Edi; break;
177 case 0: base = context->Eax; seg = context->SegDs; break;
178 case 1: base = context->Ecx; seg = context->SegDs; break;
179 case 2: base = context->Edx; seg = context->SegDs; break;
180 case 3: base = context->Ebx; seg = context->SegDs; break;
181 case 4: base = context->Esp; seg = context->SegSs; break;
182 case 5: base = context->Ebp; seg = context->SegSs; break;
183 case 6: base = context->Esi; seg = context->SegDs; break;
184 case 7: base = context->Edi; seg = context->SegDs; break;
189 if (rm == 5) /* special case: ds:(disp32) */
191 GET_VAL( &base, DWORD );
192 seg = context->SegDs;
196 case 1: /* 8-bit disp */
197 GET_VAL( &off, BYTE );
198 base += (signed char)off;
201 case 2: /* 32-bit disp */
202 GET_VAL( &off, DWORD );
203 base += (signed long)off;
207 else /* short address */
211 case 0: /* ds:(bx,si) */
212 base = LOWORD(context->Ebx) + LOWORD(context->Esi);
213 seg = context->SegDs;
215 case 1: /* ds:(bx,di) */
216 base = LOWORD(context->Ebx) + LOWORD(context->Edi);
217 seg = context->SegDs;
219 case 2: /* ss:(bp,si) */
220 base = LOWORD(context->Ebp) + LOWORD(context->Esi);
221 seg = context->SegSs;
223 case 3: /* ss:(bp,di) */
224 base = LOWORD(context->Ebp) + LOWORD(context->Edi);
225 seg = context->SegSs;
227 case 4: /* ds:(si) */
228 base = LOWORD(context->Esi);
229 seg = context->SegDs;
231 case 5: /* ds:(di) */
232 base = LOWORD(context->Edi);
233 seg = context->SegDs;
235 case 6: /* ss:(bp) */
236 base = LOWORD(context->Ebp);
237 seg = context->SegSs;
239 case 7: /* ds:(bx) */
240 base = LOWORD(context->Ebx);
241 seg = context->SegDs;
248 if (rm == 6) /* special case: ds:(disp16) */
250 GET_VAL( &base, WORD );
251 seg = context->SegDs;
255 case 1: /* 8-bit disp */
256 GET_VAL( &off, BYTE );
257 base += (signed char)off;
260 case 2: /* 16-bit disp */
261 GET_VAL( &off, WORD );
262 base += (signed short)off;
267 if (segprefix != -1) seg = segprefix;
269 /* Make sure the segment and offset are valid */
270 if (wine_ldt_is_system(seg)) return (BYTE *)(base + (index << ss));
271 if ((seg & 7) != 7) return NULL;
272 wine_ldt_get_entry( seg, &entry );
273 if (wine_ldt_is_empty( &entry )) return NULL;
274 if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
275 return (char *)wine_ldt_get_base(&entry) + base + (index << ss);
280 /***********************************************************************
283 * Emulate the LDS (and LES,LFS,etc.) instruction.
285 static BOOL INSTR_EmulateLDS( CONTEXT86 *context, BYTE *instr, int long_op,
286 int long_addr, int segprefix, int *len )
289 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
290 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
291 long_addr, segprefix, len );
293 return FALSE; /* Unable to emulate it */
294 seg = *(WORD *)(addr + (long_op ? 4 : 2));
296 if (!INSTR_ReplaceSelector( context, &seg ))
297 return FALSE; /* Unable to emulate it */
299 /* Now store the offset in the correct register */
301 switch((*regmodrm >> 3) & 7)
304 if (long_op) context->Eax = *(DWORD *)addr;
305 else SET_LOWORD(context->Eax,*(WORD *)addr);
308 if (long_op) context->Ecx = *(DWORD *)addr;
309 else SET_LOWORD(context->Ecx,*(WORD *)addr);
312 if (long_op) context->Edx = *(DWORD *)addr;
313 else SET_LOWORD(context->Edx,*(WORD *)addr);
316 if (long_op) context->Ebx = *(DWORD *)addr;
317 else SET_LOWORD(context->Ebx,*(WORD *)addr);
320 if (long_op) context->Esp = *(DWORD *)addr;
321 else SET_LOWORD(context->Esp,*(WORD *)addr);
324 if (long_op) context->Ebp = *(DWORD *)addr;
325 else SET_LOWORD(context->Ebp,*(WORD *)addr);
328 if (long_op) context->Esi = *(DWORD *)addr;
329 else SET_LOWORD(context->Esi,*(WORD *)addr);
332 if (long_op) context->Edi = *(DWORD *)addr;
333 else SET_LOWORD(context->Edi,*(WORD *)addr);
337 /* Store the correct segment in the segment register */
341 case 0xc4: context->SegEs = seg; break; /* les */
342 case 0xc5: context->SegDs = seg; break; /* lds */
343 case 0x0f: switch(instr[1])
345 case 0xb2: context->SegSs = seg; break; /* lss */
346 case 0xb4: context->SegFs = seg; break; /* lfs */
347 case 0xb5: context->SegGs = seg; break; /* lgs */
352 /* Add the opcode size to the total length */
354 *len += 1 + (*instr == 0x0f);
358 /***********************************************************************
361 * input on a I/O port
363 static DWORD INSTR_inport( WORD port, int size, CONTEXT86 *context )
367 if (!DOS_inport) init_winedos();
368 if (DOS_inport) res = DOS_inport( port, size );
375 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
376 (WORD)context->SegCs, LOWORD(context->Eip));
379 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
380 (WORD)context->SegCs, LOWORD(context->Eip));
383 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port, res,
384 (WORD)context->SegCs, LOWORD(context->Eip));
392 /***********************************************************************
395 * output on a I/O port
397 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT86 *context )
399 if (!DOS_outport) init_winedos();
400 if (DOS_outport) DOS_outport( port, size, val );
407 DPRINTF("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
408 (WORD)context->SegCs, LOWORD(context->Eip));
411 DPRINTF("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
412 (WORD)context->SegCs, LOWORD(context->Eip));
415 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port, val,
416 (WORD)context->SegCs, LOWORD(context->Eip));
423 /***********************************************************************
424 * INSTR_EmulateInstruction
426 * Emulate a privileged instruction.
427 * Returns exception continuation status.
429 DWORD INSTR_EmulateInstruction( EXCEPTION_RECORD *rec, CONTEXT86 *context )
431 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
434 long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
435 instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
436 if (!instr) return ExceptionContinueSearch;
438 /* First handle any possible prefix */
440 segprefix = -1; /* no prefix */
449 segprefix = context->SegCs;
452 segprefix = context->SegSs;
455 segprefix = context->SegDs;
458 segprefix = context->SegEs;
461 segprefix = context->SegFs;
464 segprefix = context->SegGs;
467 long_op = !long_op; /* opcode size prefix */
470 long_addr = !long_addr; /* addr size prefix */
472 case 0xf0: /* lock */
474 case 0xf2: /* repne */
477 case 0xf3: /* repe */
481 prefix = 0; /* no more prefixes */
491 /* Now look at the actual instruction */
495 case 0x07: /* pop es */
496 case 0x17: /* pop ss */
497 case 0x1f: /* pop ds */
499 WORD seg = *(WORD *)get_stack( context );
500 if (INSTR_ReplaceSelector( context, &seg ))
504 case 0x07: context->SegEs = seg; break;
505 case 0x17: context->SegSs = seg; break;
506 case 0x1f: context->SegDs = seg; break;
508 add_stack(context, long_op ? 4 : 2);
509 context->Eip += prefixlen + 1;
510 return ExceptionContinueExecution;
513 break; /* Unable to emulate it */
515 case 0x0f: /* extended instruction */
518 case 0x22: /* mov eax, crX */
522 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
523 context->Eip,context->Eax );
524 context->Eip += prefixlen+3;
525 return ExceptionContinueExecution;
527 break; /*fallthrough to bad instruction handling */
529 break; /*fallthrough to bad instruction handling */
530 case 0x20: /* mov crX, eax */
533 case 0xe0: /* mov cr4, eax */
534 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
535 * bit 0: VME Virtual Mode Exception ?
536 * bit 1: PVI Protected mode Virtual Interrupt
537 * bit 2: TSD Timestamp disable
538 * bit 3: DE Debugging extensions
539 * bit 4: PSE Page size extensions
540 * bit 5: PAE Physical address extension
541 * bit 6: MCE Machine check enable
542 * bit 7: PGE Enable global pages
543 * bit 8: PCE Enable performance counters at IPL3
545 ERR("mov cr4,eax at 0x%08lx\n",context->Eip);
547 context->Eip += prefixlen+3;
548 return ExceptionContinueExecution;
549 case 0xc0: /* mov cr0, eax */
550 ERR("mov cr0,eax at 0x%08lx\n",context->Eip);
551 context->Eax = 0x10; /* FIXME: set more bits ? */
552 context->Eip += prefixlen+3;
553 return ExceptionContinueExecution;
554 default: /* fallthrough to illegal instruction */
557 /* fallthrough to illegal instruction */
559 case 0xa1: /* pop fs */
561 WORD seg = *(WORD *)get_stack( context );
562 if (INSTR_ReplaceSelector( context, &seg ))
564 context->SegFs = seg;
565 add_stack(context, long_op ? 4 : 2);
566 context->Eip += prefixlen + 2;
567 return ExceptionContinueExecution;
571 case 0xa9: /* pop gs */
573 WORD seg = *(WORD *)get_stack( context );
574 if (INSTR_ReplaceSelector( context, &seg ))
576 context->SegGs = seg;
577 add_stack(context, long_op ? 4 : 2);
578 context->Eip += prefixlen + 2;
579 return ExceptionContinueExecution;
583 case 0xb2: /* lss addr,reg */
584 case 0xb4: /* lfs addr,reg */
585 case 0xb5: /* lgs addr,reg */
586 if (INSTR_EmulateLDS( context, instr, long_op,
587 long_addr, segprefix, &len ))
589 context->Eip += prefixlen + len;
590 return ExceptionContinueExecution;
594 break; /* Unable to emulate it */
596 case 0x6c: /* insb */
597 case 0x6d: /* insw/d */
598 case 0x6e: /* outsb */
599 case 0x6f: /* outsw/d */
601 int typ = *instr; /* Just in case it's overwritten. */
602 int outp = (typ >= 0x6e);
603 unsigned long count = repX ?
604 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
605 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
606 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
607 int seg = outp ? context->SegDs : context->SegEs; /* FIXME: is this right? */
610 /* FIXME: Check segment readable. */
613 /* FIXME: Check segment writeable. */
618 if (long_addr) context->Ecx = 0;
619 else SET_LOWORD(context->Ecx,0);
625 WORD dx = LOWORD(context->Edx);
628 data = make_ptr( context, seg, context->Esi, long_addr );
629 if (long_addr) context->Esi += step;
630 else ADD_LOWORD(context->Esi,step);
634 data = make_ptr( context, seg, context->Edi, long_addr );
635 if (long_addr) context->Edi += step;
636 else ADD_LOWORD(context->Edi,step);
642 *(BYTE *)data = INSTR_inport( dx, 1, context );
646 *(DWORD *)data = INSTR_inport( dx, 4, context );
648 *(WORD *)data = INSTR_inport( dx, 2, context );
651 INSTR_outport( dx, 1, *(BYTE *)data, context );
655 INSTR_outport( dx, 4, *(DWORD *)data, context );
657 INSTR_outport( dx, 2, *(WORD *)data, context );
661 context->Eip += prefixlen + 1;
663 return ExceptionContinueExecution;
665 case 0x8e: /* mov XX,segment_reg */
668 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
669 long_addr, segprefix, &len );
671 break; /* Unable to emulate it */
673 if (!INSTR_ReplaceSelector( context, &seg ))
674 break; /* Unable to emulate it */
676 switch((instr[1] >> 3) & 7)
679 context->SegEs = seg;
680 context->Eip += prefixlen + len + 1;
681 return ExceptionContinueExecution;
685 context->SegSs = seg;
686 context->Eip += prefixlen + len + 1;
687 return ExceptionContinueExecution;
689 context->SegDs = seg;
690 context->Eip += prefixlen + len + 1;
691 return ExceptionContinueExecution;
693 context->SegFs = seg;
694 context->Eip += prefixlen + len + 1;
695 return ExceptionContinueExecution;
697 context->SegGs = seg;
698 context->Eip += prefixlen + len + 1;
699 return ExceptionContinueExecution;
705 break; /* Unable to emulate it */
707 case 0xc4: /* les addr,reg */
708 case 0xc5: /* lds addr,reg */
709 if (INSTR_EmulateLDS( context, instr, long_op,
710 long_addr, segprefix, &len ))
712 context->Eip += prefixlen + len;
713 return ExceptionContinueExecution;
715 break; /* Unable to emulate it */
717 case 0xcd: /* int <XX> */
718 if (wine_ldt_is_system(context->SegCs)) break; /* don't emulate it in 32-bit code */
719 if (!DOS_EmulateInterruptPM) init_winedos();
720 if (DOS_EmulateInterruptPM)
722 context->Eip += prefixlen + 2;
723 DOS_EmulateInterruptPM( context, instr[1] );
724 return ExceptionContinueExecution;
726 break; /* Unable to emulate it */
728 case 0xcf: /* iret */
729 if (wine_ldt_is_system(context->SegCs)) break; /* don't emulate it in 32-bit code */
732 DWORD *stack = get_stack( context );
733 context->Eip = *stack++;
734 context->SegCs = *stack++;
735 context->EFlags = *stack;
736 add_stack(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
740 WORD *stack = get_stack( context );
741 context->Eip = *stack++;
742 context->SegCs = *stack++;
743 SET_LOWORD(context->EFlags,*stack);
744 add_stack(context, 3*sizeof(WORD)); /* Pop the return address and flags */
746 return ExceptionContinueExecution;
748 case 0xe4: /* inb al,XX */
749 SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
750 context->Eip += prefixlen + 2;
751 return ExceptionContinueExecution;
753 case 0xe5: /* in (e)ax,XX */
755 context->Eax = INSTR_inport( instr[1], 4, context );
757 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
758 context->Eip += prefixlen + 2;
759 return ExceptionContinueExecution;
761 case 0xe6: /* outb XX,al */
762 INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
763 context->Eip += prefixlen + 2;
764 return ExceptionContinueExecution;
766 case 0xe7: /* out XX,(e)ax */
768 INSTR_outport( instr[1], 4, context->Eax, context );
770 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
771 context->Eip += prefixlen + 2;
772 return ExceptionContinueExecution;
774 case 0xec: /* inb al,dx */
775 SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
776 context->Eip += prefixlen + 1;
777 return ExceptionContinueExecution;
779 case 0xed: /* in (e)ax,dx */
781 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
783 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
784 context->Eip += prefixlen + 1;
785 return ExceptionContinueExecution;
787 case 0xee: /* outb dx,al */
788 INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
789 context->Eip += prefixlen + 1;
790 return ExceptionContinueExecution;
792 case 0xef: /* out dx,(e)ax */
794 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
796 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
797 context->Eip += prefixlen + 1;
798 return ExceptionContinueExecution;
801 NtCurrentTeb()->dpmi_vif = 0;
802 context->Eip += prefixlen + 1;
803 return ExceptionContinueExecution;
806 NtCurrentTeb()->dpmi_vif = 1;
807 context->Eip += prefixlen + 1;
808 return ExceptionContinueExecution;
810 return ExceptionContinueSearch; /* Unable to emulate it */
814 /***********************************************************************
815 * INSTR_CallBuiltinHandler
817 void INSTR_CallBuiltinHandler( CONTEXT86 *context, BYTE intnum )
819 if (!DOS_CallBuiltinHandler) init_winedos();
820 if (DOS_CallBuiltinHandler) DOS_CallBuiltinHandler( context, intnum );
824 /***********************************************************************
825 * DOS3Call (KERNEL.102)
827 void WINAPI DOS3Call( CONTEXT86 *context )
829 INSTR_CallBuiltinHandler( context, 0x21 );
833 /***********************************************************************
834 * NetBIOSCall (KERNEL.103)
836 void WINAPI NetBIOSCall16( CONTEXT86 *context )
838 INSTR_CallBuiltinHandler( context, 0x5c );
842 /***********************************************************************
843 * GetSetKernelDOSProc (KERNEL.311)
845 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
847 FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);