Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / krnl386.exe16 / instr.c
blob6ba985cf31d0dae53770fa98ad6c5ee3c04967f3
1 /*
2 * Emulation of privileged instructions
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 2005 Ivan Leo Puoti
6 * Copyright 2005 Laurent Pinchart
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winternl.h"
31 #include "wine/winuser16.h"
32 #include "excpt.h"
33 #include "wine/debug.h"
34 #include "kernel16_private.h"
35 #include "dosexe.h"
36 #include "wine/exception.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(int);
39 WINE_DECLARE_DEBUG_CHANNEL(io);
41 /* macros to set parts of a DWORD */
42 #define SET_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
43 #define SET_LOBYTE(dw,val) ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
44 #define ADD_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
45 #define ISV86(context) ((context)->EFlags & 0x00020000)
47 static inline void add_stack( CONTEXT86 *context, int offset )
49 if (ISV86(context) || !IS_SELECTOR_32BIT(context->SegSs))
50 ADD_LOWORD( context->Esp, offset );
51 else
52 context->Esp += offset;
55 static inline void *make_ptr( CONTEXT86 *context, DWORD seg, DWORD off, int long_addr )
57 if (ISV86(context)) return (void *)((seg << 4) + LOWORD(off));
58 if (wine_ldt_is_system(seg)) return (void *)off;
59 if (!long_addr) off = LOWORD(off);
60 return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
63 static inline void *get_stack( CONTEXT86 *context )
65 if (ISV86(context)) return (void *)((context->SegSs << 4) + LOWORD(context->Esp));
66 return wine_ldt_get_ptr( context->SegSs, context->Esp );
69 #include "pshpack1.h"
70 struct idtr
72 WORD limit;
73 BYTE *base;
75 #include "poppack.h"
77 static LDT_ENTRY idt[256];
79 static inline struct idtr get_idtr(void)
81 struct idtr ret;
82 #if defined(__i386__) && defined(__GNUC__)
83 __asm__( "sidtl %0" : "=m" (ret) );
84 #else
85 ret.base = (BYTE *)idt;
86 ret.limit = sizeof(idt) - 1;
87 #endif
88 return ret;
92 /***********************************************************************
93 * INSTR_ReplaceSelector
95 * Try to replace an invalid selector by a valid one.
96 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
97 * is the so called 'bimodal' selector 0x40, which points to the BIOS
98 * data segment. Used by (at least) Borland products (and programs compiled
99 * using Borland products).
101 * See Undocumented Windows, Chapter 5, __0040.
103 static BOOL INSTR_ReplaceSelector( CONTEXT86 *context, WORD *sel )
105 if (*sel == 0x40)
107 *sel = DOSMEM_BiosDataSeg;
108 return TRUE;
110 return FALSE; /* Can't replace selector, crashdump */
114 /* store an operand into a register */
115 static void store_reg( CONTEXT86 *context, BYTE regmodrm, const BYTE *addr, int long_op )
117 switch((regmodrm >> 3) & 7)
119 case 0:
120 if (long_op) context->Eax = *(const DWORD *)addr;
121 else SET_LOWORD(context->Eax, *(const WORD *)addr);
122 break;
123 case 1:
124 if (long_op) context->Ecx = *(const DWORD *)addr;
125 else SET_LOWORD(context->Ecx, *(const WORD *)addr);
126 break;
127 case 2:
128 if (long_op) context->Edx = *(const DWORD *)addr;
129 else SET_LOWORD(context->Edx, *(const WORD *)addr);
130 break;
131 case 3:
132 if (long_op) context->Ebx = *(const DWORD *)addr;
133 else SET_LOWORD(context->Ebx, *(const WORD *)addr);
134 break;
135 case 4:
136 if (long_op) context->Esp = *(const DWORD *)addr;
137 else SET_LOWORD(context->Esp, *(const WORD *)addr);
138 break;
139 case 5:
140 if (long_op) context->Ebp = *(const DWORD *)addr;
141 else SET_LOWORD(context->Ebp, *(const WORD *)addr);
142 break;
143 case 6:
144 if (long_op) context->Esi = *(const DWORD *)addr;
145 else SET_LOWORD(context->Esi, *(const WORD *)addr);
146 break;
147 case 7:
148 if (long_op) context->Edi = *(const DWORD *)addr;
149 else SET_LOWORD(context->Edi, *(const WORD *)addr);
150 break;
154 /***********************************************************************
155 * INSTR_GetOperandAddr
157 * Return the address of an instruction operand (from the mod/rm byte).
159 static BYTE *INSTR_GetOperandAddr( CONTEXT86 *context, BYTE *instr,
160 int long_addr, int segprefix, int *len )
162 int mod, rm, base = 0, index = 0, ss = 0, seg = 0, off;
163 LDT_ENTRY entry;
165 #define GET_VAL(val,type) \
166 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
168 *len = 0;
169 GET_VAL( &mod, BYTE );
170 rm = mod & 7;
171 mod >>= 6;
173 if (mod == 3)
175 switch(rm)
177 case 0: return (BYTE *)&context->Eax;
178 case 1: return (BYTE *)&context->Ecx;
179 case 2: return (BYTE *)&context->Edx;
180 case 3: return (BYTE *)&context->Ebx;
181 case 4: return (BYTE *)&context->Esp;
182 case 5: return (BYTE *)&context->Ebp;
183 case 6: return (BYTE *)&context->Esi;
184 case 7: return (BYTE *)&context->Edi;
188 if (long_addr)
190 if (rm == 4)
192 BYTE sib;
193 GET_VAL( &sib, BYTE );
194 rm = sib & 7;
195 ss = sib >> 6;
196 switch(sib >> 3)
198 case 0: index = context->Eax; break;
199 case 1: index = context->Ecx; break;
200 case 2: index = context->Edx; break;
201 case 3: index = context->Ebx; break;
202 case 4: index = 0; break;
203 case 5: index = context->Ebp; break;
204 case 6: index = context->Esi; break;
205 case 7: index = context->Edi; break;
209 switch(rm)
211 case 0: base = context->Eax; seg = context->SegDs; break;
212 case 1: base = context->Ecx; seg = context->SegDs; break;
213 case 2: base = context->Edx; seg = context->SegDs; break;
214 case 3: base = context->Ebx; seg = context->SegDs; break;
215 case 4: base = context->Esp; seg = context->SegSs; break;
216 case 5: base = context->Ebp; seg = context->SegSs; break;
217 case 6: base = context->Esi; seg = context->SegDs; break;
218 case 7: base = context->Edi; seg = context->SegDs; break;
220 switch (mod)
222 case 0:
223 if (rm == 5) /* special case: ds:(disp32) */
225 GET_VAL( &base, DWORD );
226 seg = context->SegDs;
228 break;
230 case 1: /* 8-bit disp */
231 GET_VAL( &off, BYTE );
232 base += (signed char)off;
233 break;
235 case 2: /* 32-bit disp */
236 GET_VAL( &off, DWORD );
237 base += (signed long)off;
238 break;
241 else /* short address */
243 switch(rm)
245 case 0: /* ds:(bx,si) */
246 base = LOWORD(context->Ebx) + LOWORD(context->Esi);
247 seg = context->SegDs;
248 break;
249 case 1: /* ds:(bx,di) */
250 base = LOWORD(context->Ebx) + LOWORD(context->Edi);
251 seg = context->SegDs;
252 break;
253 case 2: /* ss:(bp,si) */
254 base = LOWORD(context->Ebp) + LOWORD(context->Esi);
255 seg = context->SegSs;
256 break;
257 case 3: /* ss:(bp,di) */
258 base = LOWORD(context->Ebp) + LOWORD(context->Edi);
259 seg = context->SegSs;
260 break;
261 case 4: /* ds:(si) */
262 base = LOWORD(context->Esi);
263 seg = context->SegDs;
264 break;
265 case 5: /* ds:(di) */
266 base = LOWORD(context->Edi);
267 seg = context->SegDs;
268 break;
269 case 6: /* ss:(bp) */
270 base = LOWORD(context->Ebp);
271 seg = context->SegSs;
272 break;
273 case 7: /* ds:(bx) */
274 base = LOWORD(context->Ebx);
275 seg = context->SegDs;
276 break;
279 switch(mod)
281 case 0:
282 if (rm == 6) /* special case: ds:(disp16) */
284 GET_VAL( &base, WORD );
285 seg = context->SegDs;
287 break;
289 case 1: /* 8-bit disp */
290 GET_VAL( &off, BYTE );
291 base += (signed char)off;
292 break;
294 case 2: /* 16-bit disp */
295 GET_VAL( &off, WORD );
296 base += (signed short)off;
297 break;
299 base &= 0xffff;
301 if (segprefix != -1) seg = segprefix;
303 /* Make sure the segment and offset are valid */
304 if (wine_ldt_is_system(seg)) return (BYTE *)(base + (index << ss));
305 if ((seg & 7) != 7) return NULL;
306 wine_ldt_get_entry( seg, &entry );
307 if (wine_ldt_is_empty( &entry )) return NULL;
308 if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
309 return (BYTE *)wine_ldt_get_base(&entry) + base + (index << ss);
310 #undef GET_VAL
314 /***********************************************************************
315 * INSTR_EmulateLDS
317 * Emulate the LDS (and LES,LFS,etc.) instruction.
319 static BOOL INSTR_EmulateLDS( CONTEXT86 *context, BYTE *instr, int long_op,
320 int long_addr, int segprefix, int *len )
322 WORD seg;
323 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
324 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
325 long_addr, segprefix, len );
326 if (!addr)
327 return FALSE; /* Unable to emulate it */
328 seg = *(WORD *)(addr + (long_op ? 4 : 2));
330 if (!INSTR_ReplaceSelector( context, &seg ))
331 return FALSE; /* Unable to emulate it */
333 /* Now store the offset in the correct register */
335 store_reg( context, *regmodrm, addr, long_op );
337 /* Store the correct segment in the segment register */
339 switch(*instr)
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 */
349 break;
352 /* Add the opcode size to the total length */
354 *len += 1 + (*instr == 0x0f);
355 return TRUE;
358 /***********************************************************************
359 * INSTR_inport
361 * input on an I/O port
363 static DWORD INSTR_inport( WORD port, int size, CONTEXT86 *context )
365 DWORD res = DOSVM_inport( port, size );
367 if (TRACE_ON(io))
369 switch(size)
371 case 1:
372 TRACE_(io)( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
373 (WORD)context->SegCs, LOWORD(context->Eip));
374 break;
375 case 2:
376 TRACE_(io)( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
377 (WORD)context->SegCs, LOWORD(context->Eip));
378 break;
379 case 4:
380 TRACE_(io)( "0x%x < %08x @ %04x:%04x\n", port, res,
381 (WORD)context->SegCs, LOWORD(context->Eip));
382 break;
385 return res;
389 /***********************************************************************
390 * INSTR_outport
392 * output on an I/O port
394 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT86 *context )
396 DOSVM_outport( port, size, val );
398 if (TRACE_ON(io))
400 switch(size)
402 case 1:
403 TRACE_(io)("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
404 (WORD)context->SegCs, LOWORD(context->Eip));
405 break;
406 case 2:
407 TRACE_(io)("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
408 (WORD)context->SegCs, LOWORD(context->Eip));
409 break;
410 case 4:
411 TRACE_(io)("0x%x > %08x @ %04x:%04x\n", port, val,
412 (WORD)context->SegCs, LOWORD(context->Eip));
413 break;
419 /***********************************************************************
420 * __wine_emulate_instruction
422 * Emulate a privileged instruction.
423 * Returns exception continuation status.
425 DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT86 *context )
427 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
428 BYTE *instr;
430 long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
431 instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
432 if (!instr) return ExceptionContinueSearch;
434 /* First handle any possible prefix */
436 segprefix = -1; /* no prefix */
437 prefix = 1;
438 repX = 0;
439 prefixlen = 0;
440 while(prefix)
442 switch(*instr)
444 case 0x2e:
445 segprefix = context->SegCs;
446 break;
447 case 0x36:
448 segprefix = context->SegSs;
449 break;
450 case 0x3e:
451 segprefix = context->SegDs;
452 break;
453 case 0x26:
454 segprefix = context->SegEs;
455 break;
456 case 0x64:
457 segprefix = context->SegFs;
458 break;
459 case 0x65:
460 segprefix = context->SegGs;
461 break;
462 case 0x66:
463 long_op = !long_op; /* opcode size prefix */
464 break;
465 case 0x67:
466 long_addr = !long_addr; /* addr size prefix */
467 break;
468 case 0xf0: /* lock */
469 break;
470 case 0xf2: /* repne */
471 repX = 1;
472 break;
473 case 0xf3: /* repe */
474 repX = 2;
475 break;
476 default:
477 prefix = 0; /* no more prefixes */
478 break;
480 if (prefix)
482 instr++;
483 prefixlen++;
487 /* Now look at the actual instruction */
489 switch(*instr)
491 case 0x07: /* pop es */
492 case 0x17: /* pop ss */
493 case 0x1f: /* pop ds */
495 WORD seg = *(WORD *)get_stack( context );
496 if (INSTR_ReplaceSelector( context, &seg ))
498 switch(*instr)
500 case 0x07: context->SegEs = seg; break;
501 case 0x17: context->SegSs = seg; break;
502 case 0x1f: context->SegDs = seg; break;
504 add_stack(context, long_op ? 4 : 2);
505 context->Eip += prefixlen + 1;
506 return ExceptionContinueExecution;
509 break; /* Unable to emulate it */
511 case 0x0f: /* extended instruction */
512 switch(instr[1])
514 case 0x22: /* mov eax, crX */
515 switch (instr[2])
517 case 0xc0:
518 ERR("mov eax,cr0 at 0x%08x, EAX=0x%08x\n",
519 context->Eip,context->Eax );
520 context->Eip += prefixlen+3;
521 return ExceptionContinueExecution;
522 default:
523 break; /*fallthrough to bad instruction handling */
525 break; /*fallthrough to bad instruction handling */
526 case 0x20: /* mov crX, eax */
527 switch (instr[2])
529 case 0xe0: /* mov cr4, eax */
530 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
531 * bit 0: VME Virtual Mode Exception ?
532 * bit 1: PVI Protected mode Virtual Interrupt
533 * bit 2: TSD Timestamp disable
534 * bit 3: DE Debugging extensions
535 * bit 4: PSE Page size extensions
536 * bit 5: PAE Physical address extension
537 * bit 6: MCE Machine check enable
538 * bit 7: PGE Enable global pages
539 * bit 8: PCE Enable performance counters at IPL3
541 ERR("mov cr4,eax at 0x%08x\n",context->Eip);
542 context->Eax = 0;
543 context->Eip += prefixlen+3;
544 return ExceptionContinueExecution;
545 case 0xc0: /* mov cr0, eax */
546 ERR("mov cr0,eax at 0x%08x\n",context->Eip);
547 context->Eax = 0x10; /* FIXME: set more bits ? */
548 context->Eip += prefixlen+3;
549 return ExceptionContinueExecution;
550 default: /* fallthrough to illegal instruction */
551 break;
553 /* fallthrough to illegal instruction */
554 break;
555 case 0x21: /* mov drX, eax */
556 switch (instr[2])
558 case 0xc8: /* mov dr1, eax */
559 TRACE("mov dr1,eax at 0x%08x\n",context->Eip);
560 context->Eax = context->Dr1;
561 context->Eip += prefixlen+3;
562 return ExceptionContinueExecution;
563 case 0xf8: /* mov dr7, eax */
564 TRACE("mov dr7,eax at 0x%08x\n",context->Eip);
565 context->Eax = 0x400;
566 context->Eip += prefixlen+3;
567 return ExceptionContinueExecution;
569 ERR("Unsupported DR register, eip+2 is %02x\n", instr[2]);
570 /* fallthrough to illegal instruction */
571 break;
572 case 0x23: /* mov eax drX */
573 switch (instr[2])
575 case 0xc8: /* mov eax, dr1 */
576 context->Dr1 = context->Eax;
577 context->Eip += prefixlen+3;
578 return ExceptionContinueExecution;
580 ERR("Unsupported DR register, eip+2 is %02x\n", instr[2]);
581 /* fallthrough to illegal instruction */
582 break;
583 case 0xa1: /* pop fs */
585 WORD seg = *(WORD *)get_stack( context );
586 if (INSTR_ReplaceSelector( context, &seg ))
588 context->SegFs = seg;
589 add_stack(context, long_op ? 4 : 2);
590 context->Eip += prefixlen + 2;
591 return ExceptionContinueExecution;
594 break;
595 case 0xa9: /* pop gs */
597 WORD seg = *(WORD *)get_stack( context );
598 if (INSTR_ReplaceSelector( context, &seg ))
600 context->SegGs = seg;
601 add_stack(context, long_op ? 4 : 2);
602 context->Eip += prefixlen + 2;
603 return ExceptionContinueExecution;
606 break;
607 case 0xb2: /* lss addr,reg */
608 case 0xb4: /* lfs addr,reg */
609 case 0xb5: /* lgs addr,reg */
610 if (INSTR_EmulateLDS( context, instr, long_op,
611 long_addr, segprefix, &len ))
613 context->Eip += prefixlen + len;
614 return ExceptionContinueExecution;
616 break;
618 break; /* Unable to emulate it */
620 case 0x6c: /* insb */
621 case 0x6d: /* insw/d */
622 case 0x6e: /* outsb */
623 case 0x6f: /* outsw/d */
625 int typ = *instr; /* Just in case it's overwritten. */
626 int outp = (typ >= 0x6e);
627 unsigned long count = repX ?
628 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
629 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
630 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
631 int seg = outp ? context->SegDs : context->SegEs; /* FIXME: is this right? */
633 if (outp)
634 /* FIXME: Check segment is readable. */
636 else
637 /* FIXME: Check segment is writable. */
640 if (repX)
642 if (long_addr) context->Ecx = 0;
643 else SET_LOWORD(context->Ecx,0);
646 while (count-- > 0)
648 void *data;
649 WORD dx = LOWORD(context->Edx);
650 if (outp)
652 data = make_ptr( context, seg, context->Esi, long_addr );
653 if (long_addr) context->Esi += step;
654 else ADD_LOWORD(context->Esi,step);
656 else
658 data = make_ptr( context, seg, context->Edi, long_addr );
659 if (long_addr) context->Edi += step;
660 else ADD_LOWORD(context->Edi,step);
663 switch (typ)
665 case 0x6c:
666 *(BYTE *)data = INSTR_inport( dx, 1, context );
667 break;
668 case 0x6d:
669 if (long_op)
670 *(DWORD *)data = INSTR_inport( dx, 4, context );
671 else
672 *(WORD *)data = INSTR_inport( dx, 2, context );
673 break;
674 case 0x6e:
675 INSTR_outport( dx, 1, *(BYTE *)data, context );
676 break;
677 case 0x6f:
678 if (long_op)
679 INSTR_outport( dx, 4, *(DWORD *)data, context );
680 else
681 INSTR_outport( dx, 2, *(WORD *)data, context );
682 break;
685 context->Eip += prefixlen + 1;
687 return ExceptionContinueExecution;
689 case 0x8b: /* mov Ev, Gv */
691 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1, long_addr,
692 segprefix, &len);
693 struct idtr idtr = get_idtr();
694 unsigned int offset = addr - idtr.base;
696 if (offset <= idtr.limit + 1 - (long_op ? 4 : 2))
698 idt[1].LimitLow = 0x100; /* FIXME */
699 idt[2].LimitLow = 0x11E; /* FIXME */
700 idt[3].LimitLow = 0x500; /* FIXME */
701 store_reg( context, instr[1], (BYTE *)idt + offset, long_op );
702 context->Eip += prefixlen + len + 1;
703 return ExceptionContinueExecution;
706 break; /* Unable to emulate it */
708 case 0x8e: /* mov XX,segment_reg */
710 WORD seg;
711 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
712 long_addr, segprefix, &len );
713 if (!addr)
714 break; /* Unable to emulate it */
715 seg = *(WORD *)addr;
716 if (!INSTR_ReplaceSelector( context, &seg ))
717 break; /* Unable to emulate it */
719 switch((instr[1] >> 3) & 7)
721 case 0:
722 context->SegEs = seg;
723 context->Eip += prefixlen + len + 1;
724 return ExceptionContinueExecution;
725 case 1: /* cs */
726 break;
727 case 2:
728 context->SegSs = seg;
729 context->Eip += prefixlen + len + 1;
730 return ExceptionContinueExecution;
731 case 3:
732 context->SegDs = seg;
733 context->Eip += prefixlen + len + 1;
734 return ExceptionContinueExecution;
735 case 4:
736 context->SegFs = seg;
737 context->Eip += prefixlen + len + 1;
738 return ExceptionContinueExecution;
739 case 5:
740 context->SegGs = seg;
741 context->Eip += prefixlen + len + 1;
742 return ExceptionContinueExecution;
743 case 6: /* unused */
744 case 7: /* unused */
745 break;
748 break; /* Unable to emulate it */
750 case 0xc4: /* les addr,reg */
751 case 0xc5: /* lds addr,reg */
752 if (INSTR_EmulateLDS( context, instr, long_op,
753 long_addr, segprefix, &len ))
755 context->Eip += prefixlen + len;
756 return ExceptionContinueExecution;
758 break; /* Unable to emulate it */
760 case 0xcd: /* int <XX> */
761 context->Eip += prefixlen + 2;
762 if (DOSVM_EmulateInterruptPM( context, instr[1] )) return ExceptionContinueExecution;
763 context->Eip -= prefixlen + 2; /* restore eip */
764 break; /* Unable to emulate it */
766 case 0xcf: /* iret */
767 if (wine_ldt_is_system(context->SegCs)) break; /* don't emulate it in 32-bit code */
768 if (long_op)
770 DWORD *stack = get_stack( context );
771 context->Eip = *stack++;
772 context->SegCs = *stack++;
773 context->EFlags = *stack;
774 add_stack(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
776 else
778 WORD *stack = get_stack( context );
779 context->Eip = *stack++;
780 context->SegCs = *stack++;
781 SET_LOWORD(context->EFlags,*stack);
782 add_stack(context, 3*sizeof(WORD)); /* Pop the return address and flags */
784 return ExceptionContinueExecution;
786 case 0xe4: /* inb al,XX */
787 SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
788 context->Eip += prefixlen + 2;
789 return ExceptionContinueExecution;
791 case 0xe5: /* in (e)ax,XX */
792 if (long_op)
793 context->Eax = INSTR_inport( instr[1], 4, context );
794 else
795 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
796 context->Eip += prefixlen + 2;
797 return ExceptionContinueExecution;
799 case 0xe6: /* outb XX,al */
800 INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
801 context->Eip += prefixlen + 2;
802 return ExceptionContinueExecution;
804 case 0xe7: /* out XX,(e)ax */
805 if (long_op)
806 INSTR_outport( instr[1], 4, context->Eax, context );
807 else
808 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
809 context->Eip += prefixlen + 2;
810 return ExceptionContinueExecution;
812 case 0xec: /* inb al,dx */
813 SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
814 context->Eip += prefixlen + 1;
815 return ExceptionContinueExecution;
817 case 0xed: /* in (e)ax,dx */
818 if (long_op)
819 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
820 else
821 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
822 context->Eip += prefixlen + 1;
823 return ExceptionContinueExecution;
825 case 0xee: /* outb dx,al */
826 INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
827 context->Eip += prefixlen + 1;
828 return ExceptionContinueExecution;
830 case 0xef: /* out dx,(e)ax */
831 if (long_op)
832 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
833 else
834 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
835 context->Eip += prefixlen + 1;
836 return ExceptionContinueExecution;
838 case 0xfa: /* cli */
839 get_vm86_teb_info()->dpmi_vif = 0;
840 context->Eip += prefixlen + 1;
841 return ExceptionContinueExecution;
843 case 0xfb: /* sti */
844 get_vm86_teb_info()->dpmi_vif = 1;
845 context->Eip += prefixlen + 1;
846 if (get_vm86_teb_info()->vm86_pending)
848 get_vm86_teb_info()->vm86_pending = 0;
849 rec->ExceptionCode = EXCEPTION_VM86_STI;
850 break; /* Handle the pending event. */
852 return ExceptionContinueExecution;
854 return ExceptionContinueSearch; /* Unable to emulate it */
858 /***********************************************************************
859 * INSTR_vectored_handler
861 * Vectored exception handler used to emulate protected instructions
862 * from 32-bit code.
864 LONG CALLBACK INSTR_vectored_handler( EXCEPTION_POINTERS *ptrs )
866 EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
867 CONTEXT86 *context = ptrs->ContextRecord;
869 if (wine_ldt_is_system(context->SegCs) &&
870 (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
871 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION))
873 if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
874 return EXCEPTION_CONTINUE_EXECUTION;
876 return EXCEPTION_CONTINUE_SEARCH;
880 /***********************************************************************
881 * DOS3Call (KERNEL.102)
883 void WINAPI DOS3Call( CONTEXT86 *context )
885 __wine_call_int_handler( context, 0x21 );
889 /***********************************************************************
890 * NetBIOSCall (KERNEL.103)
892 void WINAPI NetBIOSCall16( CONTEXT86 *context )
894 __wine_call_int_handler( context, 0x5c );
898 /***********************************************************************
899 * GetSetKernelDOSProc (KERNEL.311)
901 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
903 FIXME("(DosProc=%p): stub\n", DosProc);
904 return NULL;