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 "selectors.h"
32 #include "wine/debug.h"
35 #include "wine/exception.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(int);
38 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)))
47 inline static void add_stack( CONTEXT86 *context, int offset )
49 if (ISV86(context) || !IS_SELECTOR_32BIT(context->SegSs))
50 ADD_LOWORD( context->Esp, offset );
52 context->Esp += offset;
55 inline static void *make_ptr( CONTEXT86 *context, DWORD seg, DWORD off, int long_addr )
57 if (ISV86(context)) return PTR_REAL_TO_LIN( seg, off );
58 if (IS_SELECTOR_SYSTEM(seg)) return (void *)off;
59 if (!long_addr) off = LOWORD(off);
60 return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
63 inline static void *get_stack( CONTEXT86 *context )
65 if (ISV86(context)) return PTR_REAL_TO_LIN( context->SegSs, context->Esp );
66 return wine_ldt_get_ptr( context->SegSs, context->Esp );
70 /***********************************************************************
71 * INSTR_ReplaceSelector
73 * Try to replace an invalid selector by a valid one.
74 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
75 * is the so called 'bimodal' selector 0x40, which points to the BIOS
76 * data segment. Used by (at least) Borland products (and programs compiled
77 * using Borland products).
79 * See Undocumented Windows, Chapter 5, __0040.
81 static BOOL INSTR_ReplaceSelector( CONTEXT86 *context, WORD *sel )
85 static WORD sys_timer = 0;
87 sys_timer = CreateSystemTimer( 55, DOSMEM_Tick );
88 *sel = DOSMEM_BiosDataSeg;
91 return FALSE; /* Can't replace selector, crashdump */
95 /***********************************************************************
96 * INSTR_GetOperandAddr
98 * Return the address of an instruction operand (from the mod/rm byte).
100 static BYTE *INSTR_GetOperandAddr( CONTEXT86 *context, BYTE *instr,
101 int long_addr, int segprefix, int *len )
103 int mod, rm, base, index = 0, ss = 0, seg = 0, off;
106 #define GET_VAL(val,type) \
107 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
110 GET_VAL( &mod, BYTE );
118 case 0: return (BYTE *)&context->Eax;
119 case 1: return (BYTE *)&context->Ecx;
120 case 2: return (BYTE *)&context->Edx;
121 case 3: return (BYTE *)&context->Ebx;
122 case 4: return (BYTE *)&context->Esp;
123 case 5: return (BYTE *)&context->Ebp;
124 case 6: return (BYTE *)&context->Esi;
125 case 7: return (BYTE *)&context->Edi;
134 GET_VAL( &sib, BYTE );
139 case 0: index = context->Eax; break;
140 case 1: index = context->Ecx; break;
141 case 2: index = context->Edx; break;
142 case 3: index = context->Ebx; break;
143 case 4: index = 0; break;
144 case 5: index = context->Ebp; break;
145 case 6: index = context->Esi; break;
146 case 7: index = context->Edi; break;
152 case 0: base = context->Eax; seg = context->SegDs; break;
153 case 1: base = context->Ecx; seg = context->SegDs; break;
154 case 2: base = context->Edx; seg = context->SegDs; break;
155 case 3: base = context->Ebx; seg = context->SegDs; break;
156 case 4: base = context->Esp; seg = context->SegSs; break;
157 case 5: base = context->Ebp; seg = context->SegSs; break;
158 case 6: base = context->Esi; seg = context->SegDs; break;
159 case 7: base = context->Edi; seg = context->SegDs; break;
164 if (rm == 5) /* special case: ds:(disp32) */
166 GET_VAL( &base, DWORD );
167 seg = context->SegDs;
171 case 1: /* 8-bit disp */
172 GET_VAL( &off, BYTE );
173 base += (signed char)off;
176 case 2: /* 32-bit disp */
177 GET_VAL( &off, DWORD );
178 base += (signed long)off;
182 else /* short address */
186 case 0: /* ds:(bx,si) */
187 base = LOWORD(context->Ebx) + LOWORD(context->Esi);
188 seg = context->SegDs;
190 case 1: /* ds:(bx,di) */
191 base = LOWORD(context->Ebx) + LOWORD(context->Edi);
192 seg = context->SegDs;
194 case 2: /* ss:(bp,si) */
195 base = LOWORD(context->Ebp) + LOWORD(context->Esi);
196 seg = context->SegSs;
198 case 3: /* ss:(bp,di) */
199 base = LOWORD(context->Ebp) + LOWORD(context->Edi);
200 seg = context->SegSs;
202 case 4: /* ds:(si) */
203 base = LOWORD(context->Esi);
204 seg = context->SegDs;
206 case 5: /* ds:(di) */
207 base = LOWORD(context->Edi);
208 seg = context->SegDs;
210 case 6: /* ss:(bp) */
211 base = LOWORD(context->Ebp);
212 seg = context->SegSs;
214 case 7: /* ds:(bx) */
215 base = LOWORD(context->Ebx);
216 seg = context->SegDs;
223 if (rm == 6) /* special case: ds:(disp16) */
225 GET_VAL( &base, WORD );
226 seg = context->SegDs;
230 case 1: /* 8-bit disp */
231 GET_VAL( &off, BYTE );
232 base += (signed char)off;
235 case 2: /* 16-bit disp */
236 GET_VAL( &off, WORD );
237 base += (signed short)off;
242 if (segprefix != -1) seg = segprefix;
244 /* Make sure the segment and offset are valid */
245 if (IS_SELECTOR_SYSTEM(seg)) return (BYTE *)(base + (index << ss));
246 if ((seg & 7) != 7) return NULL;
247 wine_ldt_get_entry( seg, &entry );
248 if (wine_ldt_is_empty( &entry )) return NULL;
249 if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
250 return (char *)wine_ldt_get_base(&entry) + base + (index << ss);
255 /***********************************************************************
258 * Emulate the LDS (and LES,LFS,etc.) instruction.
260 static BOOL INSTR_EmulateLDS( CONTEXT86 *context, BYTE *instr, int long_op,
261 int long_addr, int segprefix, int *len )
264 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
265 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
266 long_addr, segprefix, len );
268 return FALSE; /* Unable to emulate it */
269 seg = *(WORD *)(addr + (long_op ? 4 : 2));
271 if (!INSTR_ReplaceSelector( context, &seg ))
272 return FALSE; /* Unable to emulate it */
274 /* Now store the offset in the correct register */
276 switch((*regmodrm >> 3) & 7)
279 if (long_op) context->Eax = *(DWORD *)addr;
280 else SET_LOWORD(context->Eax,*(WORD *)addr);
283 if (long_op) context->Ecx = *(DWORD *)addr;
284 else SET_LOWORD(context->Ecx,*(WORD *)addr);
287 if (long_op) context->Edx = *(DWORD *)addr;
288 else SET_LOWORD(context->Edx,*(WORD *)addr);
291 if (long_op) context->Ebx = *(DWORD *)addr;
292 else SET_LOWORD(context->Ebx,*(WORD *)addr);
295 if (long_op) context->Esp = *(DWORD *)addr;
296 else SET_LOWORD(context->Esp,*(WORD *)addr);
299 if (long_op) context->Ebp = *(DWORD *)addr;
300 else SET_LOWORD(context->Ebp,*(WORD *)addr);
303 if (long_op) context->Esi = *(DWORD *)addr;
304 else SET_LOWORD(context->Esi,*(WORD *)addr);
307 if (long_op) context->Edi = *(DWORD *)addr;
308 else SET_LOWORD(context->Edi,*(WORD *)addr);
312 /* Store the correct segment in the segment register */
316 case 0xc4: context->SegEs = seg; break; /* les */
317 case 0xc5: context->SegDs = seg; break; /* lds */
318 case 0x0f: switch(instr[1])
320 case 0xb2: context->SegSs = seg; break; /* lss */
321 case 0xb4: context->SegFs = seg; break; /* lfs */
322 case 0xb5: context->SegGs = seg; break; /* lgs */
327 /* Add the opcode size to the total length */
329 *len += 1 + (*instr == 0x0f);
333 /***********************************************************************
336 * input on a I/O port
338 static DWORD INSTR_inport( WORD port, int size, CONTEXT86 *context )
342 if (Dosvm.inport || DPMI_LoadDosSystem()) res = Dosvm.inport( port, size );
349 DPRINTF( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
350 (WORD)context->SegCs, LOWORD(context->Eip));
353 DPRINTF( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
354 (WORD)context->SegCs, LOWORD(context->Eip));
357 DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port, res,
358 (WORD)context->SegCs, LOWORD(context->Eip));
366 /***********************************************************************
369 * output on a I/O port
371 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT86 *context )
373 if (Dosvm.outport || DPMI_LoadDosSystem()) Dosvm.outport( port, size, val );
380 DPRINTF("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
381 (WORD)context->SegCs, LOWORD(context->Eip));
384 DPRINTF("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
385 (WORD)context->SegCs, LOWORD(context->Eip));
388 DPRINTF("0x%x > %08lx @ %04x:%04x\n", port, val,
389 (WORD)context->SegCs, LOWORD(context->Eip));
396 /***********************************************************************
397 * INSTR_EmulateInstruction
399 * Emulate a privileged instruction.
400 * Returns exception continuation status.
402 DWORD INSTR_EmulateInstruction( EXCEPTION_RECORD *rec, CONTEXT86 *context )
404 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
407 long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
408 instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
409 if (!instr) return ExceptionContinueSearch;
411 /* First handle any possible prefix */
413 segprefix = -1; /* no prefix */
422 segprefix = context->SegCs;
425 segprefix = context->SegSs;
428 segprefix = context->SegDs;
431 segprefix = context->SegEs;
434 segprefix = context->SegFs;
437 segprefix = context->SegGs;
440 long_op = !long_op; /* opcode size prefix */
443 long_addr = !long_addr; /* addr size prefix */
445 case 0xf0: /* lock */
447 case 0xf2: /* repne */
450 case 0xf3: /* repe */
454 prefix = 0; /* no more prefixes */
464 /* Now look at the actual instruction */
468 case 0x07: /* pop es */
469 case 0x17: /* pop ss */
470 case 0x1f: /* pop ds */
472 WORD seg = *(WORD *)get_stack( context );
473 if (INSTR_ReplaceSelector( context, &seg ))
477 case 0x07: context->SegEs = seg; break;
478 case 0x17: context->SegSs = seg; break;
479 case 0x1f: context->SegDs = seg; break;
481 add_stack(context, long_op ? 4 : 2);
482 context->Eip += prefixlen + 1;
483 return ExceptionContinueExecution;
486 break; /* Unable to emulate it */
488 case 0x0f: /* extended instruction */
491 case 0x22: /* mov eax, crX */
495 ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
496 context->Eip,context->Eax );
497 context->Eip += prefixlen+3;
498 return ExceptionContinueExecution;
500 break; /*fallthrough to bad instruction handling */
502 break; /*fallthrough to bad instruction handling */
503 case 0x20: /* mov crX, eax */
506 case 0xe0: /* mov cr4, eax */
507 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
508 * bit 0: VME Virtual Mode Exception ?
509 * bit 1: PVI Protected mode Virtual Interrupt
510 * bit 2: TSD Timestamp disable
511 * bit 3: DE Debugging extensions
512 * bit 4: PSE Page size extensions
513 * bit 5: PAE Physical address extension
514 * bit 6: MCE Machine check enable
515 * bit 7: PGE Enable global pages
516 * bit 8: PCE Enable performance counters at IPL3
518 ERR("mov cr4,eax at 0x%08lx\n",context->Eip);
520 context->Eip += prefixlen+3;
521 return ExceptionContinueExecution;
522 case 0xc0: /* mov cr0, eax */
523 ERR("mov cr0,eax at 0x%08lx\n",context->Eip);
524 context->Eax = 0x10; /* FIXME: set more bits ? */
525 context->Eip += prefixlen+3;
526 return ExceptionContinueExecution;
527 default: /* fallthrough to illegal instruction */
530 /* fallthrough to illegal instruction */
532 case 0xa1: /* pop fs */
534 WORD seg = *(WORD *)get_stack( context );
535 if (INSTR_ReplaceSelector( context, &seg ))
537 context->SegFs = seg;
538 add_stack(context, long_op ? 4 : 2);
539 context->Eip += prefixlen + 2;
540 return ExceptionContinueExecution;
544 case 0xa9: /* pop gs */
546 WORD seg = *(WORD *)get_stack( context );
547 if (INSTR_ReplaceSelector( context, &seg ))
549 context->SegGs = seg;
550 add_stack(context, long_op ? 4 : 2);
551 context->Eip += prefixlen + 2;
552 return ExceptionContinueExecution;
556 case 0xb2: /* lss addr,reg */
557 case 0xb4: /* lfs addr,reg */
558 case 0xb5: /* lgs addr,reg */
559 if (INSTR_EmulateLDS( context, instr, long_op,
560 long_addr, segprefix, &len ))
562 context->Eip += prefixlen + len;
563 return ExceptionContinueExecution;
567 break; /* Unable to emulate it */
569 case 0x6c: /* insb */
570 case 0x6d: /* insw/d */
571 case 0x6e: /* outsb */
572 case 0x6f: /* outsw/d */
574 int typ = *instr; /* Just in case it's overwritten. */
575 int outp = (typ >= 0x6e);
576 unsigned long count = repX ?
577 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
578 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
579 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
580 int seg = outp ? context->SegDs : context->SegEs; /* FIXME: is this right? */
583 /* FIXME: Check segment readable. */
586 /* FIXME: Check segment writeable. */
591 if (long_addr) context->Ecx = 0;
592 else SET_LOWORD(context->Ecx,0);
598 WORD dx = LOWORD(context->Edx);
601 data = make_ptr( context, seg, context->Esi, long_addr );
602 if (long_addr) context->Esi += step;
603 else ADD_LOWORD(context->Esi,step);
607 data = make_ptr( context, seg, context->Edi, long_addr );
608 if (long_addr) context->Edi += step;
609 else ADD_LOWORD(context->Edi,step);
615 *(BYTE *)data = INSTR_inport( dx, 1, context );
619 *(DWORD *)data = INSTR_inport( dx, 4, context );
621 *(WORD *)data = INSTR_inport( dx, 2, context );
624 INSTR_outport( dx, 1, *(BYTE *)data, context );
628 INSTR_outport( dx, 4, *(DWORD *)data, context );
630 INSTR_outport( dx, 2, *(WORD *)data, context );
634 context->Eip += prefixlen + 1;
636 return ExceptionContinueExecution;
638 case 0x8e: /* mov XX,segment_reg */
641 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
642 long_addr, segprefix, &len );
644 break; /* Unable to emulate it */
646 if (!INSTR_ReplaceSelector( context, &seg ))
647 break; /* Unable to emulate it */
649 switch((instr[1] >> 3) & 7)
652 context->SegEs = seg;
653 context->Eip += prefixlen + len + 1;
654 return ExceptionContinueExecution;
658 context->SegSs = seg;
659 context->Eip += prefixlen + len + 1;
660 return ExceptionContinueExecution;
662 context->SegDs = seg;
663 context->Eip += prefixlen + len + 1;
664 return ExceptionContinueExecution;
666 context->SegFs = seg;
667 context->Eip += prefixlen + len + 1;
668 return ExceptionContinueExecution;
670 context->SegGs = seg;
671 context->Eip += prefixlen + len + 1;
672 return ExceptionContinueExecution;
678 break; /* Unable to emulate it */
680 case 0xc4: /* les addr,reg */
681 case 0xc5: /* lds addr,reg */
682 if (INSTR_EmulateLDS( context, instr, long_op,
683 long_addr, segprefix, &len ))
685 context->Eip += prefixlen + len;
686 return ExceptionContinueExecution;
688 break; /* Unable to emulate it */
690 case 0xcd: /* int <XX> */
691 if (!Dosvm.EmulateInterruptPM && !DPMI_LoadDosSystem())
693 ERR("could not initialize interrupt handling\n");
697 context->Eip += prefixlen + 2;
698 Dosvm.EmulateInterruptPM( context, instr[1] );
699 return ExceptionContinueExecution;
701 break; /* Unable to emulate it */
703 case 0xcf: /* iret */
706 DWORD *stack = get_stack( context );
707 context->Eip = *stack++;
708 context->SegCs = *stack++;
709 context->EFlags = *stack;
710 add_stack(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
714 WORD *stack = get_stack( context );
715 context->Eip = *stack++;
716 context->SegCs = *stack++;
717 SET_LOWORD(context->EFlags,*stack);
718 add_stack(context, 3*sizeof(WORD)); /* Pop the return address and flags */
720 return ExceptionContinueExecution;
722 case 0xe4: /* inb al,XX */
723 SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
724 context->Eip += prefixlen + 2;
725 return ExceptionContinueExecution;
727 case 0xe5: /* in (e)ax,XX */
729 context->Eax = INSTR_inport( instr[1], 4, context );
731 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
732 context->Eip += prefixlen + 2;
733 return ExceptionContinueExecution;
735 case 0xe6: /* outb XX,al */
736 INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
737 context->Eip += prefixlen + 2;
738 return ExceptionContinueExecution;
740 case 0xe7: /* out XX,(e)ax */
742 INSTR_outport( instr[1], 4, context->Eax, context );
744 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
745 context->Eip += prefixlen + 2;
746 return ExceptionContinueExecution;
748 case 0xec: /* inb al,dx */
749 SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
750 context->Eip += prefixlen + 1;
751 return ExceptionContinueExecution;
753 case 0xed: /* in (e)ax,dx */
755 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
757 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
758 context->Eip += prefixlen + 1;
759 return ExceptionContinueExecution;
761 case 0xee: /* outb dx,al */
762 INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
763 context->Eip += prefixlen + 1;
764 return ExceptionContinueExecution;
766 case 0xef: /* out dx,(e)ax */
768 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
770 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
771 context->Eip += prefixlen + 1;
772 return ExceptionContinueExecution;
775 NtCurrentTeb()->dpmi_vif = 0;
776 context->Eip += prefixlen + 1;
777 return ExceptionContinueExecution;
780 NtCurrentTeb()->dpmi_vif = 1;
781 context->Eip += prefixlen + 1;
782 if (NtCurrentTeb()->vm86_pending)
784 NtCurrentTeb()->vm86_pending = 0;
785 rec->ExceptionCode = EXCEPTION_VM86_STI;
786 return ExceptionContinueSearch;
788 return ExceptionContinueExecution;
790 return ExceptionContinueSearch; /* Unable to emulate it */
793 #endif /* __i386__ */