2 * Emulation of priviledged instructions
4 * Copyright 1995 Alexandre Julliard
7 #include "wine/winuser16.h"
13 #include "sig_context.h"
14 #include "selectors.h"
15 #include "debugtools.h"
17 DECLARE_DEBUG_CHANNEL(int)
18 DECLARE_DEBUG_CHANNEL(io)
21 #define IS_V86(context) (EFL_sig(context)&V86_FLAG)
22 #define IS_SEL_32(context,seg) \
23 (IS_V86(context) ? FALSE : IS_SELECTOR_32BIT(seg))
25 #define STACK_sig(context) \
26 (IS_SEL_32(context,SS_sig(context)) ? ESP_sig(context) : (DWORD)SP_sig(context))
28 #define ADD_STACK_sig(context,offset) \
29 do { if (IS_SEL_32(context,SS_sig(context))) ESP_sig(context) += (offset); \
30 else SP_sig(context) += (offset); } while(0)
32 #define MAKE_PTR(seg,off) \
33 (IS_SELECTOR_SYSTEM(seg) ? (void *)(off) : PTR_SEG_OFF_TO_LIN(seg,off))
35 #define MK_PTR(context,seg,off) \
36 (IS_V86(context) ? DOSMEM_MapRealToLinear(MAKELONG(off,seg)) \
39 #define STACK_PTR(context) \
40 (IS_V86(context) ? DOSMEM_MapRealToLinear(MAKELONG(SP_sig(context),SS_sig(context))) : \
41 (IS_SELECTOR_SYSTEM(SS_sig(context)) ? (void *)ESP_sig(context) : \
42 (PTR_SEG_OFF_TO_LIN(SS_sig(context),STACK_sig(context)))))
44 /* For invalid registers fixup */
45 extern DWORD CallFrom16_Start,CallFrom16_End;
46 extern DWORD CALLTO16_Start,CALLTO16_End;
49 /***********************************************************************
50 * INSTR_ReplaceSelector
52 * Try to replace an invalid selector by a valid one.
53 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
54 * is the so called 'bimodal' selector 0x40, which points to the BIOS
55 * data segment. Used by (at least) Borland products (and programs compiled
56 * using Borland products).
58 * See Undocumented Windows, Chapter 5, __0040.
60 static BOOL INSTR_ReplaceSelector( SIGCONTEXT *context, WORD *sel )
62 if ( IS_SELECTOR_SYSTEM(CS_sig(context)) )
63 if ( ( EIP_sig(context) >= (DWORD)&CallFrom16_Start &&
64 EIP_sig(context) < (DWORD)&CallFrom16_End )
65 || ( EIP_sig(context) >= (DWORD)&CALLTO16_Start &&
66 EIP_sig(context) < (DWORD)&CALLTO16_End ) )
68 /* Saved selector may have become invalid when the relay code */
69 /* tries to restore it. We simply clear it. */
76 static WORD sys_timer = 0;
78 sys_timer = CreateSystemTimer( 55, DOSMEM_Tick );
79 *sel = DOSMEM_BiosDataSeg;
82 if (!IS_SELECTOR_SYSTEM(*sel) && !IS_SELECTOR_FREE(*sel))
83 ERR_(int)("Got protection fault on valid selector, maybe your kernel is too old?\n" );
84 return FALSE; /* Can't replace selector, crashdump */
88 /***********************************************************************
89 * INSTR_GetOperandAddr
91 * Return the address of an instruction operand (from the mod/rm byte).
93 static BYTE *INSTR_GetOperandAddr( SIGCONTEXT *context, BYTE *instr,
94 int long_addr, int segprefix, int *len )
96 int mod, rm, base, index = 0, ss = 0, seg = 0, off;
98 #define GET_VAL(val,type) \
99 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
102 GET_VAL( &mod, BYTE );
110 case 0: return (BYTE *)&EAX_sig(context);
111 case 1: return (BYTE *)&ECX_sig(context);
112 case 2: return (BYTE *)&EDX_sig(context);
113 case 3: return (BYTE *)&EBX_sig(context);
114 case 4: return (BYTE *)&ESP_sig(context);
115 case 5: return (BYTE *)&EBP_sig(context);
116 case 6: return (BYTE *)&ESI_sig(context);
117 case 7: return (BYTE *)&EDI_sig(context);
126 GET_VAL( &sib, BYTE );
131 case 0: index = EAX_sig(context); break;
132 case 1: index = ECX_sig(context); break;
133 case 2: index = EDX_sig(context); break;
134 case 3: index = EBX_sig(context); break;
135 case 4: index = 0; break;
136 case 5: index = EBP_sig(context); break;
137 case 6: index = ESI_sig(context); break;
138 case 7: index = EDI_sig(context); break;
144 case 0: base = EAX_sig(context); seg = DS_sig(context); break;
145 case 1: base = ECX_sig(context); seg = DS_sig(context); break;
146 case 2: base = EDX_sig(context); seg = DS_sig(context); break;
147 case 3: base = EBX_sig(context); seg = DS_sig(context); break;
148 case 4: base = ESP_sig(context); seg = SS_sig(context); break;
149 case 5: base = EBP_sig(context); seg = SS_sig(context); break;
150 case 6: base = ESI_sig(context); seg = DS_sig(context); break;
151 case 7: base = EDI_sig(context); seg = DS_sig(context); break;
156 if (rm == 5) /* special case: ds:(disp32) */
158 GET_VAL( &base, DWORD );
159 seg = DS_sig(context);
163 case 1: /* 8-bit disp */
164 GET_VAL( &off, BYTE );
165 base += (signed char)off;
168 case 2: /* 32-bit disp */
169 GET_VAL( &off, DWORD );
170 base += (signed long)off;
174 else /* short address */
178 case 0: /* ds:(bx,si) */
179 base = BX_sig(context) + SI_sig(context);
180 seg = DS_sig(context);
182 case 1: /* ds:(bx,di) */
183 base = BX_sig(context) + DI_sig(context);
184 seg = DS_sig(context);
186 case 2: /* ss:(bp,si) */
187 base = BP_sig(context) + SI_sig(context);
188 seg = SS_sig(context);
190 case 3: /* ss:(bp,di) */
191 base = BP_sig(context) + DI_sig(context);
192 seg = SS_sig(context);
194 case 4: /* ds:(si) */
195 base = SI_sig(context);
196 seg = DS_sig(context);
198 case 5: /* ds:(di) */
199 base = DI_sig(context);
200 seg = DS_sig(context);
202 case 6: /* ss:(bp) */
203 base = BP_sig(context);
204 seg = SS_sig(context);
206 case 7: /* ds:(bx) */
207 base = BX_sig(context);
208 seg = DS_sig(context);
215 if (rm == 6) /* special case: ds:(disp16) */
217 GET_VAL( &base, WORD );
218 seg = DS_sig(context);
222 case 1: /* 8-bit disp */
223 GET_VAL( &off, BYTE );
224 base += (signed char)off;
227 case 2: /* 16-bit disp */
228 GET_VAL( &off, WORD );
229 base += (signed short)off;
234 if (segprefix != -1) seg = segprefix;
236 /* Make sure the segment and offset are valid */
237 if (IS_SELECTOR_SYSTEM(seg)) return (BYTE *)(base + (index << ss));
238 if (((seg & 7) != 7) || IS_SELECTOR_FREE(seg)) return NULL;
239 if (GET_SEL_LIMIT(seg) < (base + (index << ss))) return NULL;
240 return (BYTE *)PTR_SEG_OFF_TO_LIN( seg, (base + (index << ss)) );
245 /***********************************************************************
248 * Emulate the LDS (and LES,LFS,etc.) instruction.
250 static BOOL INSTR_EmulateLDS( SIGCONTEXT *context, BYTE *instr, int long_op,
251 int long_addr, int segprefix, int *len )
254 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
255 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
256 long_addr, segprefix, len );
258 return FALSE; /* Unable to emulate it */
259 seg = *(WORD *)(addr + (long_op ? 4 : 2));
261 if (!INSTR_ReplaceSelector( context, &seg ))
262 return FALSE; /* Unable to emulate it */
264 /* Now store the offset in the correct register */
266 switch((*regmodrm >> 3) & 7)
269 if (long_op) EAX_sig(context) = *(DWORD *)addr;
270 else AX_sig(context) = *(WORD *)addr;
273 if (long_op) ECX_sig(context) = *(DWORD *)addr;
274 else CX_sig(context) = *(WORD *)addr;
277 if (long_op) EDX_sig(context) = *(DWORD *)addr;
278 else DX_sig(context) = *(WORD *)addr;
281 if (long_op) EBX_sig(context) = *(DWORD *)addr;
282 else BX_sig(context) = *(WORD *)addr;
285 if (long_op) ESP_sig(context) = *(DWORD *)addr;
286 else SP_sig(context) = *(WORD *)addr;
289 if (long_op) EBP_sig(context) = *(DWORD *)addr;
290 else BP_sig(context) = *(WORD *)addr;
293 if (long_op) ESI_sig(context) = *(DWORD *)addr;
294 else SI_sig(context) = *(WORD *)addr;
297 if (long_op) EDI_sig(context) = *(DWORD *)addr;
298 else DI_sig(context) = *(WORD *)addr;
302 /* Store the correct segment in the segment register */
306 case 0xc4: ES_sig(context) = seg; break; /* les */
307 case 0xc5: DS_sig(context) = seg; break; /* lds */
308 case 0x0f: switch(instr[1])
310 case 0xb2: SS_sig(context) = seg; break; /* lss */
312 case 0xb4: FS_sig(context) = seg; break; /* lfs */
315 case 0xb5: GS_sig(context) = seg; break; /* lgs */
321 /* Add the opcode size to the total length */
323 *len += 1 + (*instr == 0x0f);
328 /***********************************************************************
329 * INSTR_EmulateInstruction
331 * Emulate a priviledged instruction. Returns TRUE if emulation successful.
333 BOOL INSTR_EmulateInstruction( SIGCONTEXT *context )
335 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
339 long_op = long_addr = IS_SEL_32(context,CS_sig(context));
340 instr = (BYTE *)MK_PTR(context,CS_sig(context),EIP_sig(context));
341 if (!instr) return FALSE;
343 /* First handle any possible prefix */
345 segprefix = -1; /* no prefix */
354 segprefix = CS_sig(context);
357 segprefix = SS_sig(context);
360 segprefix = DS_sig(context);
363 segprefix = ES_sig(context);
367 segprefix = FS_sig(context);
372 segprefix = GS_sig(context);
376 long_op = !long_op; /* opcode size prefix */
379 long_addr = !long_addr; /* addr size prefix */
381 case 0xf0: /* lock */
383 case 0xf2: /* repne */
386 case 0xf3: /* repe */
390 prefix = 0; /* no more prefixes */
400 /* Now look at the actual instruction */
404 case 0x07: /* pop es */
405 case 0x17: /* pop ss */
406 case 0x1f: /* pop ds */
408 WORD seg = *(WORD *)STACK_PTR( context );
409 if (INSTR_ReplaceSelector( context, &seg ))
413 case 0x07: ES_sig(context) = seg; break;
414 case 0x17: SS_sig(context) = seg; break;
415 case 0x1f: DS_sig(context) = seg; break;
417 ADD_STACK_sig(context, long_op ? 4 : 2);
418 EIP_sig(context) += prefixlen + 1;
422 break; /* Unable to emulate it */
424 case 0x0f: /* extended instruction */
427 case 0x22: /* mov eax, crX */
430 fprintf(stderr,"mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
431 EIP_sig(context),EAX_sig(context)
433 EIP_sig(context) += prefixlen+3;
436 break; /*fallthrough to bad instruction handling */
438 break; /*fallthrough to bad instruction handling */
439 case 0x20: /* mov crX, eax */
441 case 0xe0: /* mov cr4, eax */
442 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
443 * bit 0: VME Virtual Mode Exception ?
444 * bit 1: PVI Protected mode Virtual Interrupt
445 * bit 2: TSD Timestamp disable
446 * bit 3: DE Debugging extensions
447 * bit 4: PSE Page size extensions
448 * bit 5: PAE Physical address extension
449 * bit 6: MCE Machine check enable
450 * bit 7: PGE Enable global pages
451 * bit 8: PCE Enable performance counters at IPL3
453 fprintf(stderr,"mov cr4,eax at 0x%08lx\n",EIP_sig(context));
454 EAX_sig(context) = 0;
455 EIP_sig(context) += prefixlen+3;
457 case 0xc0: /* mov cr0, eax */
458 fprintf(stderr,"mov cr0,eax at 0x%08lx\n",EIP_sig(context));
459 EAX_sig(context) = 0x10; /* FIXME: set more bits ? */
460 EIP_sig(context) += prefixlen+3;
462 default: /* fallthrough to illegal instruction */
465 /* fallthrough to illegal instruction */
468 case 0xa1: /* pop fs */
470 WORD seg = *(WORD *)STACK_PTR( context );
471 if (INSTR_ReplaceSelector( context, &seg ))
473 FS_sig(context) = seg;
474 ADD_STACK_sig(context, long_op ? 4 : 2);
475 EIP_sig(context) += prefixlen + 2;
483 case 0xa9: /* pop gs */
485 WORD seg = *(WORD *)STACK_PTR( context );
486 if (INSTR_ReplaceSelector( context, &seg ))
488 GS_sig(context) = seg;
489 ADD_STACK_sig(context, long_op ? 4 : 2);
490 EIP_sig(context) += prefixlen + 2;
497 case 0xb2: /* lss addr,reg */
499 case 0xb4: /* lfs addr,reg */
502 case 0xb5: /* lgs addr,reg */
504 if (INSTR_EmulateLDS( context, instr, long_op,
505 long_addr, segprefix, &len ))
507 EIP_sig(context) += prefixlen + len;
512 break; /* Unable to emulate it */
514 case 0x6c: /* insb */
515 case 0x6d: /* insw/d */
516 case 0x6e: /* outsb */
517 case 0x6f: /* outsw/d */
519 int typ = *instr; /* Just in case it's overwritten. */
520 int outp = (typ >= 0x6e);
521 unsigned long count = repX ?
522 (long_addr ? ECX_sig(context) : CX_sig(context)) : 1;
523 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
524 int step = (EFL_sig(context) & 0x400) ? -opsize : +opsize;
525 int seg = outp ? DS_sig(context) : ES_sig(context); /* FIXME: is this right? */
528 /* FIXME: Check segment readable. */
531 /* FIXME: Check segment writeable. */
537 ECX_sig(context) = 0;
547 data = MK_PTR(context, seg,
548 long_addr ? ESI_sig(context) : SI_sig(context));
549 if (long_addr) ESI_sig(context) += step;
550 else SI_sig(context) += step;
554 data = MK_PTR(context, seg,
555 long_addr ? EDI_sig(context) : DI_sig(context));
556 if (long_addr) EDI_sig(context) += step;
557 else DI_sig(context) += step;
563 *((BYTE *)data) = IO_inport( DX_sig(context), 1);
564 TRACE_(io)("0x%x < %02x @ %04x:%04x\n", DX_sig(context),
565 *((BYTE *)data), CS_sig(context), IP_sig(context));
570 *((DWORD *)data) = IO_inport( DX_sig(context), 4);
571 TRACE_(io)("0x%x < %08lx @ %04x:%04x\n", DX_sig(context),
572 *((DWORD *)data), CS_sig(context), IP_sig(context));
576 *((WORD *)data) = IO_inport( DX_sig(context), 2);
577 TRACE_(io)("0x%x < %04x @ %04x:%04x\n", DX_sig(context),
578 *((WORD *)data), CS_sig(context), IP_sig(context));
582 IO_outport( DX_sig(context), 1, *((BYTE *)data));
583 TRACE_(io)("0x%x > %02x @ %04x:%04x\n", DX_sig(context),
584 *((BYTE *)data), CS_sig(context), IP_sig(context));
589 IO_outport( DX_sig(context), 4, *((DWORD *)data));
590 TRACE_(io)("0x%x > %08lx @ %04x:%04x\n", DX_sig(context),
591 *((DWORD *)data), CS_sig(context), IP_sig(context));
595 IO_outport( DX_sig(context), 2, *((WORD *)data));
596 TRACE_(io)("0x%x > %04x @ %04x:%04x\n", DX_sig(context),
597 *((WORD *)data), CS_sig(context), IP_sig(context));
602 EIP_sig(context) += prefixlen + 1;
606 case 0x8e: /* mov XX,segment_reg */
609 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
610 long_addr, segprefix, &len );
612 break; /* Unable to emulate it */
614 if (!INSTR_ReplaceSelector( context, &seg ))
615 break; /* Unable to emulate it */
617 switch((instr[1] >> 3) & 7)
620 ES_sig(context) = seg;
621 EIP_sig(context) += prefixlen + len + 1;
626 SS_sig(context) = seg;
627 EIP_sig(context) += prefixlen + len + 1;
630 DS_sig(context) = seg;
631 EIP_sig(context) += prefixlen + len + 1;
635 FS_sig(context) = seg;
636 EIP_sig(context) += prefixlen + len + 1;
641 GS_sig(context) = seg;
642 EIP_sig(context) += prefixlen + len + 1;
650 break; /* Unable to emulate it */
652 case 0xc4: /* les addr,reg */
653 case 0xc5: /* lds addr,reg */
654 if (INSTR_EmulateLDS( context, instr, long_op,
655 long_addr, segprefix, &len ))
657 EIP_sig(context) += prefixlen + len;
660 break; /* Unable to emulate it */
662 case 0xcd: /* int <XX> */
665 ERR_(int)("int xx from 32-bit code is not supported.\n");
666 break; /* Unable to emulate it */
670 FARPROC16 addr = INT_GetPMHandler( instr[1] );
671 WORD *stack = (WORD *)STACK_PTR( context );
672 /* Push the flags and return address on the stack */
673 *(--stack) = FL_sig(context);
674 *(--stack) = CS_sig(context);
675 *(--stack) = IP_sig(context) + prefixlen + 2;
676 ADD_STACK_sig(context, -3 * sizeof(WORD));
677 /* Jump to the interrupt handler */
678 CS_sig(context) = HIWORD(addr);
679 EIP_sig(context) = LOWORD(addr);
683 case 0xcf: /* iret */
686 DWORD *stack = (DWORD *)STACK_PTR( context );
687 EIP_sig(context) = *stack++;
688 CS_sig(context) = *stack++;
689 EFL_sig(context) = *stack;
690 ADD_STACK_sig(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
694 WORD *stack = (WORD *)STACK_PTR( context );
695 EIP_sig(context) = *stack++;
696 CS_sig(context) = *stack++;
697 FL_sig(context) = *stack;
698 ADD_STACK_sig(context, 3*sizeof(WORD)); /* Pop the return address and flags */
702 case 0xe4: /* inb al,XX */
703 AL_sig(context) = IO_inport( instr[1], 1 );
704 TRACE_(io)("0x%x < %02x @ %04x:%04x\n", instr[1],
705 AL_sig(context), CS_sig(context), IP_sig(context));
706 EIP_sig(context) += prefixlen + 2;
709 case 0xe5: /* in (e)ax,XX */
712 EAX_sig(context) = IO_inport( instr[1], 4 );
713 TRACE_(io)("0x%x < %08lx @ %04x:%04x\n", instr[1],
714 EAX_sig(context), CS_sig(context), IP_sig(context));
718 AX_sig(context) = IO_inport( instr[1], 2 );
719 TRACE_(io)("0x%x < %04x @ %04x:%04x\n", instr[1],
720 AX_sig(context), CS_sig(context), IP_sig(context));
722 EIP_sig(context) += prefixlen + 2;
725 case 0xe6: /* outb XX,al */
726 IO_outport( instr[1], 1, AL_sig(context) );
727 TRACE_(io)("0x%x > %02x @ %04x:%04x\n", instr[1],
728 AL_sig(context), CS_sig(context), IP_sig(context));
729 EIP_sig(context) += prefixlen + 2;
732 case 0xe7: /* out XX,(e)ax */
735 IO_outport( instr[1], 4, EAX_sig(context) );
736 TRACE_(io)("0x%x > %08lx @ %04x:%04x\n", instr[1],
737 EAX_sig(context), CS_sig(context), IP_sig(context));
741 IO_outport( instr[1], 2, AX_sig(context) );
742 TRACE_(io)("0x%x > %04x @ %04x:%04x\n", instr[1],
743 AX_sig(context), CS_sig(context), IP_sig(context));
745 EIP_sig(context) += prefixlen + 2;
748 case 0xec: /* inb al,dx */
749 AL_sig(context) = IO_inport( DX_sig(context), 1 );
750 TRACE_(io)("0x%x < %02x @ %04x:%04x\n", DX_sig(context),
751 AL_sig(context), CS_sig(context), IP_sig(context));
752 EIP_sig(context) += prefixlen + 1;
755 case 0xed: /* in (e)ax,dx */
758 EAX_sig(context) = IO_inport( DX_sig(context), 4 );
759 TRACE_(io)("0x%x < %08lx @ %04x:%04x\n", DX_sig(context),
760 EAX_sig(context), CS_sig(context), IP_sig(context));
764 AX_sig(context) = IO_inport( DX_sig(context), 2 );
765 TRACE_(io)("0x%x < %04x @ %04x:%04x\n", DX_sig(context),
766 AX_sig(context), CS_sig(context), IP_sig(context));
768 EIP_sig(context) += prefixlen + 1;
771 case 0xee: /* outb dx,al */
772 IO_outport( DX_sig(context), 1, AL_sig(context) );
773 TRACE_(io)("0x%x > %02x @ %04x:%04x\n", DX_sig(context),
774 AL_sig(context), CS_sig(context), IP_sig(context));
775 EIP_sig(context) += prefixlen + 1;
778 case 0xef: /* out dx,(e)ax */
781 IO_outport( DX_sig(context), 4, EAX_sig(context) );
782 TRACE_(io)("0x%x > %08lx @ %04x:%04x\n", DX_sig(context),
783 EAX_sig(context), CS_sig(context), IP_sig(context));
787 IO_outport( DX_sig(context), 2, AX_sig(context) );
788 TRACE_(io)("0x%x > %04x @ %04x:%04x\n", DX_sig(context),
789 AX_sig(context), CS_sig(context), IP_sig(context));
791 EIP_sig(context) += prefixlen + 1;
794 case 0xfa: /* cli, ignored */
795 EIP_sig(context) += prefixlen + 1;
798 case 0xfb: /* sti, ignored */
799 EIP_sig(context) += prefixlen + 1;
804 /* Check for Win16 __GP handler */
805 gpHandler = HasGPHandler16( PTR_SEG_OFF_TO_SEGPTR( CS_sig(context),
806 EIP_sig(context) ) );
809 WORD *stack = (WORD *)STACK_PTR( context );
810 *--stack = CS_sig(context);
811 *--stack = EIP_sig(context);
812 ADD_STACK_sig(context, -2*sizeof(WORD));
814 CS_sig(context) = SELECTOROF( gpHandler );
815 EIP_sig(context) = OFFSETOF( gpHandler );
819 MESSAGE("Unexpected Windows program segfault"
820 " - opcode = %x\n", *instr);
821 return FALSE; /* Unable to emulate it */