Release 980628
[wine/gsoc-2012-control.git] / miscemu / instr.c
blob41eaa5bf7e933fc6ec81360ee24684d28af281a6
1 /*
2 * Emulation of priviledged instructions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include "windows.h"
8 #include "ldt.h"
9 #include "global.h"
10 #include "miscemu.h"
11 #include "sig_context.h"
12 #include "debug.h"
15 #define STACK_sig(context) \
16 (IS_SELECTOR_32BIT(SS_sig(context)) ? ESP_sig(context) : SP_sig(context))
18 #define MAKE_PTR(seg,off) \
19 (IS_SELECTOR_SYSTEM(seg) ? (void *)(off) : PTR_SEG_OFF_TO_LIN(seg,off))
21 #define STACK_PTR(context) \
22 (IS_SELECTOR_SYSTEM(SS_sig(context)) ? (void *)ESP_sig(context) : \
23 (PTR_SEG_OFF_TO_LIN(SS_sig(context),STACK_sig(context))))
26 /***********************************************************************
27 * INSTR_ReplaceSelector
29 * Try to replace an invalid selector by a valid one.
30 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
31 * is the so called 'bimodal' selector 0x40, which points to the BIOS
32 * data segment. Used by (at least) Borland products (and programs compiled
33 * using Borland products).
35 * See Undocumented Windows, Chapter 5, __0040.
37 static WORD INSTR_ReplaceSelector( SIGCONTEXT *context, WORD sel)
39 if (sel == 0x40)
41 static WORD sys_timer = 0;
42 if (!sys_timer)
43 sys_timer = CreateSystemTimer( 55, (FARPROC16)DOSMEM_Tick );
44 return DOSMEM_BiosSeg;
46 return 0; /* Can't replace selector, crashdump */
50 /***********************************************************************
51 * INSTR_GetOperandAddr
53 * Return the address of an instruction operand (from the mod/rm byte).
55 static BYTE *INSTR_GetOperandAddr( SIGCONTEXT *context, BYTE *instr,
56 int long_addr, int segprefix, int *len )
58 int mod, rm, base, index = 0, ss = 0, seg = 0, off;
60 #define GET_VAL(val,type) \
61 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
63 *len = 0;
64 GET_VAL( &mod, BYTE );
65 rm = mod & 7;
66 mod >>= 6;
68 if (mod == 3)
70 switch(rm)
72 case 0: return (BYTE *)&EAX_sig(context);
73 case 1: return (BYTE *)&ECX_sig(context);
74 case 2: return (BYTE *)&EDX_sig(context);
75 case 3: return (BYTE *)&EBX_sig(context);
76 case 4: return (BYTE *)&ESP_sig(context);
77 case 5: return (BYTE *)&EBP_sig(context);
78 case 6: return (BYTE *)&ESI_sig(context);
79 case 7: return (BYTE *)&EDI_sig(context);
83 if (long_addr)
85 if (rm == 4)
87 BYTE sib;
88 GET_VAL( &sib, BYTE );
89 rm = sib & 7;
90 ss = sib >> 6;
91 switch(sib >> 3)
93 case 0: index = EAX_sig(context); break;
94 case 1: index = ECX_sig(context); break;
95 case 2: index = EDX_sig(context); break;
96 case 3: index = EBX_sig(context); break;
97 case 4: index = 0; break;
98 case 5: index = EBP_sig(context); break;
99 case 6: index = ESI_sig(context); break;
100 case 7: index = EDI_sig(context); break;
104 switch(rm)
106 case 0: base = EAX_sig(context); seg = DS_sig(context); break;
107 case 1: base = ECX_sig(context); seg = DS_sig(context); break;
108 case 2: base = EDX_sig(context); seg = DS_sig(context); break;
109 case 3: base = EBX_sig(context); seg = DS_sig(context); break;
110 case 4: base = ESP_sig(context); seg = SS_sig(context); break;
111 case 5: base = EBP_sig(context); seg = SS_sig(context); break;
112 case 6: base = ESI_sig(context); seg = DS_sig(context); break;
113 case 7: base = EDI_sig(context); seg = DS_sig(context); break;
115 switch (mod)
117 case 0:
118 if (rm == 5) /* special case: ds:(disp32) */
120 GET_VAL( &base, DWORD );
121 seg = DS_sig(context);
123 break;
125 case 1: /* 8-bit disp */
126 GET_VAL( &off, BYTE );
127 base += (signed char)off;
128 break;
130 case 2: /* 32-bit disp */
131 GET_VAL( &off, DWORD );
132 base += (signed long)off;
133 break;
136 else /* short address */
138 switch(rm)
140 case 0: /* ds:(bx,si) */
141 base = BX_sig(context) + SI_sig(context);
142 seg = DS_sig(context);
143 break;
144 case 1: /* ds:(bx,di) */
145 base = BX_sig(context) + DI_sig(context);
146 seg = DS_sig(context);
147 break;
148 case 2: /* ss:(bp,si) */
149 base = BP_sig(context) + SI_sig(context);
150 seg = SS_sig(context);
151 break;
152 case 3: /* ss:(bp,di) */
153 base = BP_sig(context) + DI_sig(context);
154 seg = SS_sig(context);
155 break;
156 case 4: /* ds:(si) */
157 base = SI_sig(context);
158 seg = DS_sig(context);
159 break;
160 case 5: /* ds:(di) */
161 base = DI_sig(context);
162 seg = DS_sig(context);
163 break;
164 case 6: /* ss:(bp) */
165 base = BP_sig(context);
166 seg = SS_sig(context);
167 break;
168 case 7: /* ds:(bx) */
169 base = BX_sig(context);
170 seg = DS_sig(context);
171 break;
174 switch(mod)
176 case 0:
177 if (rm == 6) /* special case: ds:(disp16) */
179 GET_VAL( &base, WORD );
180 seg = DS_sig(context);
182 break;
184 case 1: /* 8-bit disp */
185 GET_VAL( &off, BYTE );
186 base += (signed char)off;
187 break;
189 case 2: /* 16-bit disp */
190 GET_VAL( &off, WORD );
191 base += (signed short)off;
192 break;
194 base &= 0xffff;
196 if (segprefix != -1) seg = segprefix;
198 /* Make sure the segment and offset are valid */
199 if (IS_SELECTOR_SYSTEM(seg)) return (BYTE *)(base + (index << ss));
200 if (((seg & 7) != 7) || IS_SELECTOR_FREE(seg)) return NULL;
201 if (GET_SEL_LIMIT(seg) < (base + (index << ss))) return NULL;
202 return (BYTE *)PTR_SEG_OFF_TO_LIN( seg, (base + (index << ss)) );
203 #undef GET_VAL
207 /***********************************************************************
208 * INSTR_EmulateLDS
210 * Emulate the LDS (and LES,LFS,etc.) instruction.
212 static BOOL32 INSTR_EmulateLDS( SIGCONTEXT *context, BYTE *instr, int long_op,
213 int long_addr, int segprefix, int *len )
215 WORD seg;
216 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
217 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
218 long_addr, segprefix, len );
219 if (!addr)
220 return FALSE; /* Unable to emulate it */
221 seg = *(WORD *)(addr + (long_op ? 4 : 2));
223 if (!(seg = INSTR_ReplaceSelector( context, seg )))
224 return FALSE; /* Unable to emulate it */
226 /* Now store the offset in the correct register */
228 switch((*regmodrm >> 3) & 7)
230 case 0:
231 if (long_op) EAX_sig(context) = *(DWORD *)addr;
232 else AX_sig(context) = *(WORD *)addr;
233 break;
234 case 1:
235 if (long_op) ECX_sig(context) = *(DWORD *)addr;
236 else CX_sig(context) = *(WORD *)addr;
237 break;
238 case 2:
239 if (long_op) EDX_sig(context) = *(DWORD *)addr;
240 else DX_sig(context) = *(WORD *)addr;
241 break;
242 case 3:
243 if (long_op) EBX_sig(context) = *(DWORD *)addr;
244 else BX_sig(context) = *(WORD *)addr;
245 break;
246 case 4:
247 if (long_op) ESP_sig(context) = *(DWORD *)addr;
248 else SP_sig(context) = *(WORD *)addr;
249 break;
250 case 5:
251 if (long_op) EBP_sig(context) = *(DWORD *)addr;
252 else BP_sig(context) = *(WORD *)addr;
253 break;
254 case 6:
255 if (long_op) ESI_sig(context) = *(DWORD *)addr;
256 else SI_sig(context) = *(WORD *)addr;
257 break;
258 case 7:
259 if (long_op) EDI_sig(context) = *(DWORD *)addr;
260 else DI_sig(context) = *(WORD *)addr;
261 break;
264 /* Store the correct segment in the segment register */
266 switch(*instr)
268 case 0xc4: ES_sig(context) = seg; break; /* les */
269 case 0xc5: DS_sig(context) = seg; break; /* lds */
270 case 0x0f: switch(instr[1])
272 case 0xb2: SS_sig(context) = seg; break; /* lss */
273 #ifdef FS_sig
274 case 0xb4: FS_sig(context) = seg; break; /* lfs */
275 #endif
276 #ifdef GS_sig
277 case 0xb5: GS_sig(context) = seg; break; /* lgs */
278 #endif
280 break;
283 /* Add the opcode size to the total length */
285 *len += 1 + (*instr == 0x0f);
286 return TRUE;
290 /***********************************************************************
291 * INSTR_EmulateInstruction
293 * Emulate a priviledged instruction. Returns TRUE if emulation successful.
295 BOOL32 INSTR_EmulateInstruction( SIGCONTEXT *context )
297 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
298 BYTE *instr;
300 /* Check for page-fault */
302 #if defined(TRAP_sig) && defined(CR2_sig)
303 if (TRAP_sig(context) == 0x0e
304 && VIRTUAL_HandleFault( (LPVOID)CR2_sig(context) )) return TRUE;
305 #endif
307 long_op = long_addr = IS_SELECTOR_32BIT(CS_sig(context));
308 instr = (BYTE *)MAKE_PTR(CS_sig(context),EIP_sig(context));
309 if (!instr) return FALSE;
311 /* First handle any possible prefix */
313 segprefix = -1; /* no prefix */
314 prefix = 1;
315 repX = 0;
316 prefixlen = 0;
317 while(prefix)
319 switch(*instr)
321 case 0x2e:
322 segprefix = CS_sig(context);
323 break;
324 case 0x36:
325 segprefix = SS_sig(context);
326 break;
327 case 0x3e:
328 segprefix = DS_sig(context);
329 break;
330 case 0x26:
331 segprefix = ES_sig(context);
332 break;
333 #ifdef FS_sig
334 case 0x64:
335 segprefix = FS_sig(context);
336 break;
337 #endif
338 #ifdef GS_sig
339 case 0x65:
340 segprefix = GS_sig(context);
341 break;
342 #endif
343 case 0x66:
344 long_op = !long_op; /* opcode size prefix */
345 break;
346 case 0x67:
347 long_addr = !long_addr; /* addr size prefix */
348 break;
349 case 0xf0: /* lock */
350 break;
351 case 0xf2: /* repne */
352 repX = 1;
353 break;
354 case 0xf3: /* repe */
355 repX = 2;
356 break;
357 default:
358 prefix = 0; /* no more prefixes */
359 break;
361 if (prefix)
363 instr++;
364 prefixlen++;
368 /* Now look at the actual instruction */
370 switch(*instr)
372 case 0x07: /* pop es */
373 case 0x17: /* pop ss */
374 case 0x1f: /* pop ds */
376 WORD seg = *(WORD *)STACK_PTR( context );
377 if ((seg = INSTR_ReplaceSelector( context, seg )) != 0)
379 switch(*instr)
381 case 0x07: ES_sig(context) = seg; break;
382 case 0x17: SS_sig(context) = seg; break;
383 case 0x1f: DS_sig(context) = seg; break;
385 STACK_sig(context) += long_op ? 4 : 2;
386 EIP_sig(context) += prefixlen + 1;
387 return TRUE;
390 break; /* Unable to emulate it */
392 case 0x0f: /* extended instruction */
393 switch(instr[1])
395 #ifdef FS_sig
396 case 0xa1: /* pop fs */
398 WORD seg = *(WORD *)STACK_PTR( context );
399 if ((seg = INSTR_ReplaceSelector( context, seg )) != 0)
401 FS_sig(context) = seg;
402 STACK_sig(context) += long_op ? 4 : 2;
403 EIP_sig(context) += prefixlen + 2;
404 return TRUE;
407 break;
408 #endif /* FS_sig */
410 #ifdef GS_sig
411 case 0xa9: /* pop gs */
413 WORD seg = *(WORD *)STACK_PTR( context );
414 if ((seg = INSTR_ReplaceSelector( context, seg )) != 0)
416 GS_sig(context) = seg;
417 STACK_sig(context) += long_op ? 4 : 2;
418 EIP_sig(context) += prefixlen + 2;
419 return TRUE;
422 break;
423 #endif /* GS_sig */
425 case 0xb2: /* lss addr,reg */
426 #ifdef FS_sig
427 case 0xb4: /* lfs addr,reg */
428 #endif
429 #ifdef GS_sig
430 case 0xb5: /* lgs addr,reg */
431 #endif
432 if (INSTR_EmulateLDS( context, instr, long_op,
433 long_addr, segprefix, &len ))
435 EIP_sig(context) += prefixlen + len;
436 return TRUE;
438 break;
440 break; /* Unable to emulate it */
442 case 0x6c: /* insb */
443 case 0x6d: /* insw/d */
444 case 0x6e: /* outsb */
445 case 0x6f: /* outsw/d */
447 int typ = *instr; /* Just in case it's overwritten. */
448 int outp = (typ >= 0x6e);
449 unsigned long count = repX ?
450 (long_addr ? ECX_sig(context) : CX_sig(context)) : 1;
451 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
452 int step = (EFL_sig(context) & 0x400) ? -opsize : +opsize;
453 int seg = outp ? DS_sig(context) : ES_sig(context); /* FIXME: is this right? */
455 if (outp)
456 /* FIXME: Check segment readable. */
457 (void)0;
458 else
459 /* FIXME: Check segment writeable. */
460 (void)0;
462 if (repX)
463 if (long_addr)
464 ECX_sig(context) = 0;
465 else
466 CX_sig(context) = 0;
468 while (count-- > 0)
470 void *data;
471 if (outp)
473 data = MAKE_PTR(seg,
474 long_addr ? ESI_sig(context) : SI_sig(context));
475 if (long_addr) ESI_sig(context) += step;
476 else SI_sig(context) += step;
478 else
480 data = MAKE_PTR(seg,
481 long_addr ? EDI_sig(context) : DI_sig(context));
482 if (long_addr) EDI_sig(context) += step;
483 else DI_sig(context) += step;
486 switch (typ)
488 case 0x6c:
489 *((BYTE *)data) = IO_inport( DX_sig(context), 1);
490 break;
491 case 0x6d:
492 if (long_op)
493 *((DWORD *)data) = IO_inport( DX_sig(context), 4);
494 else
495 *((WORD *)data) = IO_inport( DX_sig(context), 2);
496 break;
497 case 0x6e:
498 IO_outport( DX_sig(context), 1, *((BYTE *)data));
499 break;
500 case 0x6f:
501 if (long_op)
502 IO_outport( DX_sig(context), 4, *((DWORD *)data));
503 else
504 IO_outport( DX_sig(context), 2, *((WORD *)data));
505 break;
508 EIP_sig(context) += prefixlen + 1;
510 return TRUE;
512 case 0x8e: /* mov XX,segment_reg */
514 WORD seg;
515 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
516 long_addr, segprefix, &len );
517 if (!addr)
518 break; /* Unable to emulate it */
519 seg = *(WORD *)addr;
520 if (!(seg = INSTR_ReplaceSelector( context, seg )))
521 break; /* Unable to emulate it */
523 switch((instr[1] >> 3) & 7)
525 case 0:
526 ES_sig(context) = seg;
527 EIP_sig(context) += prefixlen + len + 1;
528 return TRUE;
529 case 1: /* cs */
530 break;
531 case 2:
532 SS_sig(context) = seg;
533 EIP_sig(context) += prefixlen + len + 1;
534 return TRUE;
535 case 3:
536 DS_sig(context) = seg;
537 EIP_sig(context) += prefixlen + len + 1;
538 return TRUE;
539 case 4:
540 #ifdef FS_sig
541 FS_sig(context) = seg;
542 EIP_sig(context) += prefixlen + len + 1;
543 return TRUE;
544 #endif
545 case 5:
546 #ifdef GS_sig
547 GS_sig(context) = seg;
548 EIP_sig(context) += prefixlen + len + 1;
549 return TRUE;
550 #endif
551 case 6: /* unused */
552 case 7: /* unused */
553 break;
556 break; /* Unable to emulate it */
558 case 0xc4: /* les addr,reg */
559 case 0xc5: /* lds addr,reg */
560 if (INSTR_EmulateLDS( context, instr, long_op,
561 long_addr, segprefix, &len ))
563 EIP_sig(context) += prefixlen + len;
564 return TRUE;
566 break; /* Unable to emulate it */
568 case 0xcd: /* int <XX> */
569 if (long_op)
571 ERR(int, "int xx from 32-bit code is not supported.\n");
572 break; /* Unable to emulate it */
574 else
576 FARPROC16 addr = INT_GetHandler( instr[1] );
577 WORD *stack = (WORD *)STACK_PTR( context );
578 /* Push the flags and return address on the stack */
579 *(--stack) = FL_sig(context);
580 *(--stack) = CS_sig(context);
581 *(--stack) = IP_sig(context) + prefixlen + 2;
582 STACK_sig(context) -= 3 * sizeof(WORD);
583 /* Jump to the interrupt handler */
584 CS_sig(context) = HIWORD(addr);
585 EIP_sig(context) = LOWORD(addr);
587 return TRUE;
589 case 0xcf: /* iret */
590 if (long_op)
592 DWORD *stack = (DWORD *)STACK_PTR( context );
593 EIP_sig(context) = *stack++;
594 CS_sig(context) = *stack++;
595 EFL_sig(context) = *stack;
596 STACK_sig(context) += 3*sizeof(DWORD); /* Pop the return address and flags */
598 else
600 WORD *stack = (WORD *)STACK_PTR( context );
601 EIP_sig(context) = *stack++;
602 CS_sig(context) = *stack++;
603 FL_sig(context) = *stack;
604 STACK_sig(context) += 3*sizeof(WORD); /* Pop the return address and flags */
606 return TRUE;
608 case 0xe4: /* inb al,XX */
609 AL_sig(context) = IO_inport( instr[1], 1 );
610 EIP_sig(context) += prefixlen + 2;
611 return TRUE;
613 case 0xe5: /* in (e)ax,XX */
614 if (long_op) EAX_sig(context) = IO_inport( instr[1], 4 );
615 else AX_sig(context) = IO_inport( instr[1], 2 );
616 EIP_sig(context) += prefixlen + 2;
617 return TRUE;
619 case 0xe6: /* outb XX,al */
620 IO_outport( instr[1], 1, AL_sig(context) );
621 EIP_sig(context) += prefixlen + 2;
622 return TRUE;
624 case 0xe7: /* out XX,(e)ax */
625 if (long_op) IO_outport( instr[1], 4, EAX_sig(context) );
626 else IO_outport( instr[1], 2, AX_sig(context) );
627 EIP_sig(context) += prefixlen + 2;
628 return TRUE;
630 case 0xec: /* inb al,dx */
631 AL_sig(context) = IO_inport( DX_sig(context), 1 );
632 EIP_sig(context) += prefixlen + 1;
633 return TRUE;
635 case 0xed: /* in (e)ax,dx */
636 if (long_op) EAX_sig(context) = IO_inport( DX_sig(context), 4 );
637 else AX_sig(context) = IO_inport( DX_sig(context), 2 );
638 EIP_sig(context) += prefixlen + 1;
639 return TRUE;
641 case 0xee: /* outb dx,al */
642 IO_outport( DX_sig(context), 1, AL_sig(context) );
643 EIP_sig(context) += prefixlen + 1;
644 return TRUE;
646 case 0xef: /* out dx,(e)ax */
647 if (long_op) IO_outport( DX_sig(context), 4, EAX_sig(context) );
648 else IO_outport( DX_sig(context), 2, AX_sig(context) );
649 EIP_sig(context) += prefixlen + 1;
650 return TRUE;
652 case 0xfa: /* cli, ignored */
653 EIP_sig(context) += prefixlen + 1;
654 return TRUE;
656 case 0xfb: /* sti, ignored */
657 EIP_sig(context) += prefixlen + 1;
658 return TRUE;
660 MSG("Unexpected Windows program segfault"
661 " - opcode = %x\n", *instr);
662 return FALSE; /* Unable to emulate it */