configure: Always set LDPATH, rpath support is broken on many platforms.
[wine.git] / dlls / kernel32 / instr.c
blob7fdc4cd6cab82f30a653076f7b6b2cd0f38584b3
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 #ifdef __i386__
28 #include <stdarg.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winternl.h"
33 #include "wine/winuser16.h"
34 #include "excpt.h"
35 #include "wine/debug.h"
36 #include "kernel_private.h"
37 #include "kernel16_private.h"
38 #include "wine/exception.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(int);
41 WINE_DECLARE_DEBUG_CHANNEL(io);
43 /* macros to set parts of a DWORD */
44 #define SET_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
45 #define SET_LOBYTE(dw,val) ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
46 #define ADD_LOWORD(dw,val) ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
47 #define ISV86(context) ((context)->EFlags & 0x00020000)
49 static inline void add_stack( CONTEXT86 *context, int offset )
51 if (ISV86(context) || !IS_SELECTOR_32BIT(context->SegSs))
52 ADD_LOWORD( context->Esp, offset );
53 else
54 context->Esp += offset;
57 static inline void *make_ptr( CONTEXT86 *context, DWORD seg, DWORD off, int long_addr )
59 if (ISV86(context)) return (void *)((seg << 4) + LOWORD(off));
60 if (wine_ldt_is_system(seg)) return (void *)off;
61 if (!long_addr) off = LOWORD(off);
62 return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
65 static inline void *get_stack( CONTEXT86 *context )
67 if (ISV86(context)) return (void *)((context->SegSs << 4) + LOWORD(context->Esp));
68 return wine_ldt_get_ptr( context->SegSs, context->Esp );
71 #include "pshpack1.h"
72 struct idtr
74 WORD limit;
75 BYTE *base;
77 #include "poppack.h"
79 static LDT_ENTRY idt[256];
81 static inline struct idtr get_idtr(void)
83 struct idtr ret;
84 #if defined(__i386__) && defined(__GNUC__)
85 __asm__( "sidtl %0" : "=m" (ret) );
86 #else
87 ret.base = (BYTE *)idt;
88 ret.limit = sizeof(idt) - 1;
89 #endif
90 return ret;
94 /***********************************************************************
95 * INSTR_ReplaceSelector
97 * Try to replace an invalid selector by a valid one.
98 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
99 * is the so called 'bimodal' selector 0x40, which points to the BIOS
100 * data segment. Used by (at least) Borland products (and programs compiled
101 * using Borland products).
103 * See Undocumented Windows, Chapter 5, __0040.
105 static BOOL INSTR_ReplaceSelector( CONTEXT86 *context, WORD *sel )
107 if (*sel == 0x40)
109 static WORD sys_timer = 0;
110 if (!sys_timer)
112 if (!winedos.BiosTick) load_winedos();
113 if (winedos.BiosTick)
114 sys_timer = CreateSystemTimer( 55, winedos.BiosTick );
116 *sel = DOSMEM_BiosDataSeg;
117 return TRUE;
119 return FALSE; /* Can't replace selector, crashdump */
123 /* store an operand into a register */
124 static void store_reg( CONTEXT86 *context, BYTE regmodrm, const BYTE *addr, int long_op )
126 switch((regmodrm >> 3) & 7)
128 case 0:
129 if (long_op) context->Eax = *(const DWORD *)addr;
130 else SET_LOWORD(context->Eax, *(const WORD *)addr);
131 break;
132 case 1:
133 if (long_op) context->Ecx = *(const DWORD *)addr;
134 else SET_LOWORD(context->Ecx, *(const WORD *)addr);
135 break;
136 case 2:
137 if (long_op) context->Edx = *(const DWORD *)addr;
138 else SET_LOWORD(context->Edx, *(const WORD *)addr);
139 break;
140 case 3:
141 if (long_op) context->Ebx = *(const DWORD *)addr;
142 else SET_LOWORD(context->Ebx, *(const WORD *)addr);
143 break;
144 case 4:
145 if (long_op) context->Esp = *(const DWORD *)addr;
146 else SET_LOWORD(context->Esp, *(const WORD *)addr);
147 break;
148 case 5:
149 if (long_op) context->Ebp = *(const DWORD *)addr;
150 else SET_LOWORD(context->Ebp, *(const WORD *)addr);
151 break;
152 case 6:
153 if (long_op) context->Esi = *(const DWORD *)addr;
154 else SET_LOWORD(context->Esi, *(const WORD *)addr);
155 break;
156 case 7:
157 if (long_op) context->Edi = *(const DWORD *)addr;
158 else SET_LOWORD(context->Edi, *(const WORD *)addr);
159 break;
163 /***********************************************************************
164 * INSTR_GetOperandAddr
166 * Return the address of an instruction operand (from the mod/rm byte).
168 static BYTE *INSTR_GetOperandAddr( CONTEXT86 *context, BYTE *instr,
169 int long_addr, int segprefix, int *len )
171 int mod, rm, base = 0, index = 0, ss = 0, seg = 0, off;
172 LDT_ENTRY entry;
174 #define GET_VAL(val,type) \
175 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
177 *len = 0;
178 GET_VAL( &mod, BYTE );
179 rm = mod & 7;
180 mod >>= 6;
182 if (mod == 3)
184 switch(rm)
186 case 0: return (BYTE *)&context->Eax;
187 case 1: return (BYTE *)&context->Ecx;
188 case 2: return (BYTE *)&context->Edx;
189 case 3: return (BYTE *)&context->Ebx;
190 case 4: return (BYTE *)&context->Esp;
191 case 5: return (BYTE *)&context->Ebp;
192 case 6: return (BYTE *)&context->Esi;
193 case 7: return (BYTE *)&context->Edi;
197 if (long_addr)
199 if (rm == 4)
201 BYTE sib;
202 GET_VAL( &sib, BYTE );
203 rm = sib & 7;
204 ss = sib >> 6;
205 switch(sib >> 3)
207 case 0: index = context->Eax; break;
208 case 1: index = context->Ecx; break;
209 case 2: index = context->Edx; break;
210 case 3: index = context->Ebx; break;
211 case 4: index = 0; break;
212 case 5: index = context->Ebp; break;
213 case 6: index = context->Esi; break;
214 case 7: index = context->Edi; break;
218 switch(rm)
220 case 0: base = context->Eax; seg = context->SegDs; break;
221 case 1: base = context->Ecx; seg = context->SegDs; break;
222 case 2: base = context->Edx; seg = context->SegDs; break;
223 case 3: base = context->Ebx; seg = context->SegDs; break;
224 case 4: base = context->Esp; seg = context->SegSs; break;
225 case 5: base = context->Ebp; seg = context->SegSs; break;
226 case 6: base = context->Esi; seg = context->SegDs; break;
227 case 7: base = context->Edi; seg = context->SegDs; break;
229 switch (mod)
231 case 0:
232 if (rm == 5) /* special case: ds:(disp32) */
234 GET_VAL( &base, DWORD );
235 seg = context->SegDs;
237 break;
239 case 1: /* 8-bit disp */
240 GET_VAL( &off, BYTE );
241 base += (signed char)off;
242 break;
244 case 2: /* 32-bit disp */
245 GET_VAL( &off, DWORD );
246 base += (signed long)off;
247 break;
250 else /* short address */
252 switch(rm)
254 case 0: /* ds:(bx,si) */
255 base = LOWORD(context->Ebx) + LOWORD(context->Esi);
256 seg = context->SegDs;
257 break;
258 case 1: /* ds:(bx,di) */
259 base = LOWORD(context->Ebx) + LOWORD(context->Edi);
260 seg = context->SegDs;
261 break;
262 case 2: /* ss:(bp,si) */
263 base = LOWORD(context->Ebp) + LOWORD(context->Esi);
264 seg = context->SegSs;
265 break;
266 case 3: /* ss:(bp,di) */
267 base = LOWORD(context->Ebp) + LOWORD(context->Edi);
268 seg = context->SegSs;
269 break;
270 case 4: /* ds:(si) */
271 base = LOWORD(context->Esi);
272 seg = context->SegDs;
273 break;
274 case 5: /* ds:(di) */
275 base = LOWORD(context->Edi);
276 seg = context->SegDs;
277 break;
278 case 6: /* ss:(bp) */
279 base = LOWORD(context->Ebp);
280 seg = context->SegSs;
281 break;
282 case 7: /* ds:(bx) */
283 base = LOWORD(context->Ebx);
284 seg = context->SegDs;
285 break;
288 switch(mod)
290 case 0:
291 if (rm == 6) /* special case: ds:(disp16) */
293 GET_VAL( &base, WORD );
294 seg = context->SegDs;
296 break;
298 case 1: /* 8-bit disp */
299 GET_VAL( &off, BYTE );
300 base += (signed char)off;
301 break;
303 case 2: /* 16-bit disp */
304 GET_VAL( &off, WORD );
305 base += (signed short)off;
306 break;
308 base &= 0xffff;
310 if (segprefix != -1) seg = segprefix;
312 /* Make sure the segment and offset are valid */
313 if (wine_ldt_is_system(seg)) return (BYTE *)(base + (index << ss));
314 if ((seg & 7) != 7) return NULL;
315 wine_ldt_get_entry( seg, &entry );
316 if (wine_ldt_is_empty( &entry )) return NULL;
317 if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
318 return (BYTE *)wine_ldt_get_base(&entry) + base + (index << ss);
319 #undef GET_VAL
323 /***********************************************************************
324 * INSTR_EmulateLDS
326 * Emulate the LDS (and LES,LFS,etc.) instruction.
328 static BOOL INSTR_EmulateLDS( CONTEXT86 *context, BYTE *instr, int long_op,
329 int long_addr, int segprefix, int *len )
331 WORD seg;
332 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
333 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
334 long_addr, segprefix, len );
335 if (!addr)
336 return FALSE; /* Unable to emulate it */
337 seg = *(WORD *)(addr + (long_op ? 4 : 2));
339 if (!INSTR_ReplaceSelector( context, &seg ))
340 return FALSE; /* Unable to emulate it */
342 /* Now store the offset in the correct register */
344 store_reg( context, *regmodrm, addr, long_op );
346 /* Store the correct segment in the segment register */
348 switch(*instr)
350 case 0xc4: context->SegEs = seg; break; /* les */
351 case 0xc5: context->SegDs = seg; break; /* lds */
352 case 0x0f: switch(instr[1])
354 case 0xb2: context->SegSs = seg; break; /* lss */
355 case 0xb4: context->SegFs = seg; break; /* lfs */
356 case 0xb5: context->SegGs = seg; break; /* lgs */
358 break;
361 /* Add the opcode size to the total length */
363 *len += 1 + (*instr == 0x0f);
364 return TRUE;
367 /***********************************************************************
368 * INSTR_inport
370 * input on an I/O port
372 static DWORD INSTR_inport( WORD port, int size, CONTEXT86 *context )
374 DWORD res = ~0U;
376 if (!winedos.inport) load_winedos();
377 if (winedos.inport) res = winedos.inport( port, size );
379 if (TRACE_ON(io))
381 switch(size)
383 case 1:
384 TRACE_(io)( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
385 (WORD)context->SegCs, LOWORD(context->Eip));
386 break;
387 case 2:
388 TRACE_(io)( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
389 (WORD)context->SegCs, LOWORD(context->Eip));
390 break;
391 case 4:
392 TRACE_(io)( "0x%x < %08x @ %04x:%04x\n", port, res,
393 (WORD)context->SegCs, LOWORD(context->Eip));
394 break;
397 return res;
401 /***********************************************************************
402 * INSTR_outport
404 * output on an I/O port
406 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT86 *context )
408 if (!winedos.outport) load_winedos();
409 if (winedos.outport) winedos.outport( port, size, val );
411 if (TRACE_ON(io))
413 switch(size)
415 case 1:
416 TRACE_(io)("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
417 (WORD)context->SegCs, LOWORD(context->Eip));
418 break;
419 case 2:
420 TRACE_(io)("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
421 (WORD)context->SegCs, LOWORD(context->Eip));
422 break;
423 case 4:
424 TRACE_(io)("0x%x > %08x @ %04x:%04x\n", port, val,
425 (WORD)context->SegCs, LOWORD(context->Eip));
426 break;
432 /***********************************************************************
433 * __wine_emulate_instruction
435 * Emulate a privileged instruction.
436 * Returns exception continuation status.
438 DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT86 *context )
440 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
441 BYTE *instr;
443 long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
444 instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
445 if (!instr) return ExceptionContinueSearch;
447 /* First handle any possible prefix */
449 segprefix = -1; /* no prefix */
450 prefix = 1;
451 repX = 0;
452 prefixlen = 0;
453 while(prefix)
455 switch(*instr)
457 case 0x2e:
458 segprefix = context->SegCs;
459 break;
460 case 0x36:
461 segprefix = context->SegSs;
462 break;
463 case 0x3e:
464 segprefix = context->SegDs;
465 break;
466 case 0x26:
467 segprefix = context->SegEs;
468 break;
469 case 0x64:
470 segprefix = context->SegFs;
471 break;
472 case 0x65:
473 segprefix = context->SegGs;
474 break;
475 case 0x66:
476 long_op = !long_op; /* opcode size prefix */
477 break;
478 case 0x67:
479 long_addr = !long_addr; /* addr size prefix */
480 break;
481 case 0xf0: /* lock */
482 break;
483 case 0xf2: /* repne */
484 repX = 1;
485 break;
486 case 0xf3: /* repe */
487 repX = 2;
488 break;
489 default:
490 prefix = 0; /* no more prefixes */
491 break;
493 if (prefix)
495 instr++;
496 prefixlen++;
500 /* Now look at the actual instruction */
502 switch(*instr)
504 case 0x07: /* pop es */
505 case 0x17: /* pop ss */
506 case 0x1f: /* pop ds */
508 WORD seg = *(WORD *)get_stack( context );
509 if (INSTR_ReplaceSelector( context, &seg ))
511 switch(*instr)
513 case 0x07: context->SegEs = seg; break;
514 case 0x17: context->SegSs = seg; break;
515 case 0x1f: context->SegDs = seg; break;
517 add_stack(context, long_op ? 4 : 2);
518 context->Eip += prefixlen + 1;
519 return ExceptionContinueExecution;
522 break; /* Unable to emulate it */
524 case 0x0f: /* extended instruction */
525 switch(instr[1])
527 case 0x22: /* mov eax, crX */
528 switch (instr[2])
530 case 0xc0:
531 ERR("mov eax,cr0 at 0x%08x, EAX=0x%08x\n",
532 context->Eip,context->Eax );
533 context->Eip += prefixlen+3;
534 return ExceptionContinueExecution;
535 default:
536 break; /*fallthrough to bad instruction handling */
538 break; /*fallthrough to bad instruction handling */
539 case 0x20: /* mov crX, eax */
540 switch (instr[2])
542 case 0xe0: /* mov cr4, eax */
543 /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
544 * bit 0: VME Virtual Mode Exception ?
545 * bit 1: PVI Protected mode Virtual Interrupt
546 * bit 2: TSD Timestamp disable
547 * bit 3: DE Debugging extensions
548 * bit 4: PSE Page size extensions
549 * bit 5: PAE Physical address extension
550 * bit 6: MCE Machine check enable
551 * bit 7: PGE Enable global pages
552 * bit 8: PCE Enable performance counters at IPL3
554 ERR("mov cr4,eax at 0x%08x\n",context->Eip);
555 context->Eax = 0;
556 context->Eip += prefixlen+3;
557 return ExceptionContinueExecution;
558 case 0xc0: /* mov cr0, eax */
559 ERR("mov cr0,eax at 0x%08x\n",context->Eip);
560 context->Eax = 0x10; /* FIXME: set more bits ? */
561 context->Eip += prefixlen+3;
562 return ExceptionContinueExecution;
563 default: /* fallthrough to illegal instruction */
564 break;
566 /* fallthrough to illegal instruction */
567 break;
568 case 0x21: /* mov drX, eax */
569 switch (instr[2])
571 case 0xc8: /* mov dr1, eax */
572 TRACE("mov dr1,eax at 0x%08x\n",context->Eip);
573 context->Eax = context->Dr1;
574 context->Eip += prefixlen+3;
575 return ExceptionContinueExecution;
576 case 0xf8: /* mov dr7, eax */
577 TRACE("mov dr7,eax at 0x%08x\n",context->Eip);
578 context->Eax = 0x400;
579 context->Eip += prefixlen+3;
580 return ExceptionContinueExecution;
582 ERR("Unsupported DR register, eip+2 is %02x\n", instr[2]);
583 /* fallthrough to illegal instruction */
584 break;
585 case 0x23: /* mov eax drX */
586 switch (instr[2])
588 case 0xc8: /* mov eax, dr1 */
589 context->Dr1 = context->Eax;
590 context->Eip += prefixlen+3;
591 return ExceptionContinueExecution;
593 ERR("Unsupported DR register, eip+2 is %02x\n", instr[2]);
594 /* fallthrough to illegal instruction */
595 break;
596 case 0xa1: /* pop fs */
598 WORD seg = *(WORD *)get_stack( context );
599 if (INSTR_ReplaceSelector( context, &seg ))
601 context->SegFs = seg;
602 add_stack(context, long_op ? 4 : 2);
603 context->Eip += prefixlen + 2;
604 return ExceptionContinueExecution;
607 break;
608 case 0xa9: /* pop gs */
610 WORD seg = *(WORD *)get_stack( context );
611 if (INSTR_ReplaceSelector( context, &seg ))
613 context->SegGs = seg;
614 add_stack(context, long_op ? 4 : 2);
615 context->Eip += prefixlen + 2;
616 return ExceptionContinueExecution;
619 break;
620 case 0xb2: /* lss addr,reg */
621 case 0xb4: /* lfs addr,reg */
622 case 0xb5: /* lgs addr,reg */
623 if (INSTR_EmulateLDS( context, instr, long_op,
624 long_addr, segprefix, &len ))
626 context->Eip += prefixlen + len;
627 return ExceptionContinueExecution;
629 break;
631 break; /* Unable to emulate it */
633 case 0x6c: /* insb */
634 case 0x6d: /* insw/d */
635 case 0x6e: /* outsb */
636 case 0x6f: /* outsw/d */
638 int typ = *instr; /* Just in case it's overwritten. */
639 int outp = (typ >= 0x6e);
640 unsigned long count = repX ?
641 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
642 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
643 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
644 int seg = outp ? context->SegDs : context->SegEs; /* FIXME: is this right? */
646 if (outp)
647 /* FIXME: Check segment is readable. */
649 else
650 /* FIXME: Check segment is writable. */
653 if (repX)
655 if (long_addr) context->Ecx = 0;
656 else SET_LOWORD(context->Ecx,0);
659 while (count-- > 0)
661 void *data;
662 WORD dx = LOWORD(context->Edx);
663 if (outp)
665 data = make_ptr( context, seg, context->Esi, long_addr );
666 if (long_addr) context->Esi += step;
667 else ADD_LOWORD(context->Esi,step);
669 else
671 data = make_ptr( context, seg, context->Edi, long_addr );
672 if (long_addr) context->Edi += step;
673 else ADD_LOWORD(context->Edi,step);
676 switch (typ)
678 case 0x6c:
679 *(BYTE *)data = INSTR_inport( dx, 1, context );
680 break;
681 case 0x6d:
682 if (long_op)
683 *(DWORD *)data = INSTR_inport( dx, 4, context );
684 else
685 *(WORD *)data = INSTR_inport( dx, 2, context );
686 break;
687 case 0x6e:
688 INSTR_outport( dx, 1, *(BYTE *)data, context );
689 break;
690 case 0x6f:
691 if (long_op)
692 INSTR_outport( dx, 4, *(DWORD *)data, context );
693 else
694 INSTR_outport( dx, 2, *(WORD *)data, context );
695 break;
698 context->Eip += prefixlen + 1;
700 return ExceptionContinueExecution;
702 case 0x8b: /* mov Ev, Gv */
704 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1, long_addr,
705 segprefix, &len);
706 struct idtr idtr = get_idtr();
707 unsigned int offset = addr - idtr.base;
709 if (offset <= idtr.limit + 1 - (long_op ? 4 : 2))
711 idt[1].LimitLow = 0x100; /* FIXME */
712 idt[2].LimitLow = 0x11E; /* FIXME */
713 idt[3].LimitLow = 0x500; /* FIXME */
714 store_reg( context, instr[1], (BYTE *)idt + offset, long_op );
715 context->Eip += prefixlen + len + 1;
716 return ExceptionContinueExecution;
719 break; /* Unable to emulate it */
721 case 0x8e: /* mov XX,segment_reg */
723 WORD seg;
724 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
725 long_addr, segprefix, &len );
726 if (!addr)
727 break; /* Unable to emulate it */
728 seg = *(WORD *)addr;
729 if (!INSTR_ReplaceSelector( context, &seg ))
730 break; /* Unable to emulate it */
732 switch((instr[1] >> 3) & 7)
734 case 0:
735 context->SegEs = seg;
736 context->Eip += prefixlen + len + 1;
737 return ExceptionContinueExecution;
738 case 1: /* cs */
739 break;
740 case 2:
741 context->SegSs = seg;
742 context->Eip += prefixlen + len + 1;
743 return ExceptionContinueExecution;
744 case 3:
745 context->SegDs = seg;
746 context->Eip += prefixlen + len + 1;
747 return ExceptionContinueExecution;
748 case 4:
749 context->SegFs = seg;
750 context->Eip += prefixlen + len + 1;
751 return ExceptionContinueExecution;
752 case 5:
753 context->SegGs = seg;
754 context->Eip += prefixlen + len + 1;
755 return ExceptionContinueExecution;
756 case 6: /* unused */
757 case 7: /* unused */
758 break;
761 break; /* Unable to emulate it */
763 case 0xc4: /* les addr,reg */
764 case 0xc5: /* lds addr,reg */
765 if (INSTR_EmulateLDS( context, instr, long_op,
766 long_addr, segprefix, &len ))
768 context->Eip += prefixlen + len;
769 return ExceptionContinueExecution;
771 break; /* Unable to emulate it */
773 case 0xcd: /* int <XX> */
774 if (!winedos.EmulateInterruptPM) load_winedos();
775 if (winedos.EmulateInterruptPM)
777 context->Eip += prefixlen + 2;
778 if (winedos.EmulateInterruptPM( context, instr[1] )) return ExceptionContinueExecution;
779 context->Eip -= prefixlen + 2; /* restore eip */
781 break; /* Unable to emulate it */
783 case 0xcf: /* iret */
784 if (wine_ldt_is_system(context->SegCs)) break; /* don't emulate it in 32-bit code */
785 if (long_op)
787 DWORD *stack = get_stack( context );
788 context->Eip = *stack++;
789 context->SegCs = *stack++;
790 context->EFlags = *stack;
791 add_stack(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
793 else
795 WORD *stack = get_stack( context );
796 context->Eip = *stack++;
797 context->SegCs = *stack++;
798 SET_LOWORD(context->EFlags,*stack);
799 add_stack(context, 3*sizeof(WORD)); /* Pop the return address and flags */
801 return ExceptionContinueExecution;
803 case 0xe4: /* inb al,XX */
804 SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
805 context->Eip += prefixlen + 2;
806 return ExceptionContinueExecution;
808 case 0xe5: /* in (e)ax,XX */
809 if (long_op)
810 context->Eax = INSTR_inport( instr[1], 4, context );
811 else
812 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
813 context->Eip += prefixlen + 2;
814 return ExceptionContinueExecution;
816 case 0xe6: /* outb XX,al */
817 INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
818 context->Eip += prefixlen + 2;
819 return ExceptionContinueExecution;
821 case 0xe7: /* out XX,(e)ax */
822 if (long_op)
823 INSTR_outport( instr[1], 4, context->Eax, context );
824 else
825 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
826 context->Eip += prefixlen + 2;
827 return ExceptionContinueExecution;
829 case 0xec: /* inb al,dx */
830 SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
831 context->Eip += prefixlen + 1;
832 return ExceptionContinueExecution;
834 case 0xed: /* in (e)ax,dx */
835 if (long_op)
836 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
837 else
838 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
839 context->Eip += prefixlen + 1;
840 return ExceptionContinueExecution;
842 case 0xee: /* outb dx,al */
843 INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
844 context->Eip += prefixlen + 1;
845 return ExceptionContinueExecution;
847 case 0xef: /* out dx,(e)ax */
848 if (long_op)
849 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
850 else
851 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
852 context->Eip += prefixlen + 1;
853 return ExceptionContinueExecution;
855 case 0xfa: /* cli */
856 get_vm86_teb_info()->dpmi_vif = 0;
857 context->Eip += prefixlen + 1;
858 return ExceptionContinueExecution;
860 case 0xfb: /* sti */
861 get_vm86_teb_info()->dpmi_vif = 1;
862 context->Eip += prefixlen + 1;
863 if (get_vm86_teb_info()->vm86_pending)
865 get_vm86_teb_info()->vm86_pending = 0;
866 rec->ExceptionCode = EXCEPTION_VM86_STI;
867 break; /* Handle the pending event. */
869 return ExceptionContinueExecution;
871 return ExceptionContinueSearch; /* Unable to emulate it */
875 /***********************************************************************
876 * INSTR_vectored_handler
878 * Vectored exception handler used to emulate protected instructions
879 * from 32-bit code.
881 LONG CALLBACK INSTR_vectored_handler( EXCEPTION_POINTERS *ptrs )
883 EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
884 CONTEXT86 *context = ptrs->ContextRecord;
886 if (wine_ldt_is_system(context->SegCs) &&
887 (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
888 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION))
890 if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
891 return EXCEPTION_CONTINUE_EXECUTION;
893 return EXCEPTION_CONTINUE_SEARCH;
897 /***********************************************************************
898 * INSTR_CallBuiltinHandler
900 static void INSTR_CallBuiltinHandler( CONTEXT86 *context, BYTE intnum )
902 if (!winedos.CallBuiltinHandler) load_winedos();
903 if (winedos.CallBuiltinHandler) winedos.CallBuiltinHandler( context, intnum );
907 /***********************************************************************
908 * DOS3Call (KERNEL.102)
910 void WINAPI DOS3Call( CONTEXT86 *context )
912 INSTR_CallBuiltinHandler( context, 0x21 );
916 /***********************************************************************
917 * NetBIOSCall (KERNEL.103)
919 void WINAPI NetBIOSCall16( CONTEXT86 *context )
921 INSTR_CallBuiltinHandler( context, 0x5c );
925 /***********************************************************************
926 * GetSetKernelDOSProc (KERNEL.311)
928 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
930 FIXME("(DosProc=%p): stub\n", DosProc);
931 return NULL;
934 #endif /* __i386__ */