wined3d: Clear the renderbuffer IDs on unload.
[wine.git] / dlls / krnl386.exe16 / instr.c
blobb0de30fa9e12c3a5f65fc2018c4dcd413eb2fdc0
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( CONTEXT *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( CONTEXT *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( CONTEXT *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( CONTEXT *context, WORD *sel )
105 if (*sel == 0x40)
107 DOSVM_start_bios_timer();
108 *sel = DOSMEM_BiosDataSeg;
109 return TRUE;
111 return FALSE; /* Can't replace selector, crashdump */
115 /* store an operand into a register */
116 static void store_reg_word( CONTEXT *context, BYTE regmodrm, const BYTE *addr, int long_op )
118 switch((regmodrm >> 3) & 7)
120 case 0:
121 if (long_op) context->Eax = *(const DWORD *)addr;
122 else SET_LOWORD(context->Eax, *(const WORD *)addr);
123 break;
124 case 1:
125 if (long_op) context->Ecx = *(const DWORD *)addr;
126 else SET_LOWORD(context->Ecx, *(const WORD *)addr);
127 break;
128 case 2:
129 if (long_op) context->Edx = *(const DWORD *)addr;
130 else SET_LOWORD(context->Edx, *(const WORD *)addr);
131 break;
132 case 3:
133 if (long_op) context->Ebx = *(const DWORD *)addr;
134 else SET_LOWORD(context->Ebx, *(const WORD *)addr);
135 break;
136 case 4:
137 if (long_op) context->Esp = *(const DWORD *)addr;
138 else SET_LOWORD(context->Esp, *(const WORD *)addr);
139 break;
140 case 5:
141 if (long_op) context->Ebp = *(const DWORD *)addr;
142 else SET_LOWORD(context->Ebp, *(const WORD *)addr);
143 break;
144 case 6:
145 if (long_op) context->Esi = *(const DWORD *)addr;
146 else SET_LOWORD(context->Esi, *(const WORD *)addr);
147 break;
148 case 7:
149 if (long_op) context->Edi = *(const DWORD *)addr;
150 else SET_LOWORD(context->Edi, *(const WORD *)addr);
151 break;
155 /* store an operand into a byte register */
156 static void store_reg_byte( CONTEXT *context, BYTE regmodrm, const BYTE *addr )
158 switch((regmodrm >> 3) & 7)
160 case 0: context->Eax = (context->Eax & 0xffffff00) | *addr; break;
161 case 1: context->Ecx = (context->Ecx & 0xffffff00) | *addr; break;
162 case 2: context->Edx = (context->Edx & 0xffffff00) | *addr; break;
163 case 3: context->Ebx = (context->Ebx & 0xffffff00) | *addr; break;
164 case 4: context->Eax = (context->Eax & 0xffff00ff) | (*addr << 8); break;
165 case 5: context->Ecx = (context->Ecx & 0xffff00ff) | (*addr << 8); break;
166 case 6: context->Edx = (context->Edx & 0xffff00ff) | (*addr << 8); break;
167 case 7: context->Ebx = (context->Ebx & 0xffff00ff) | (*addr << 8); break;
171 /***********************************************************************
172 * INSTR_GetOperandAddr
174 * Return the address of an instruction operand (from the mod/rm byte).
176 static BYTE *INSTR_GetOperandAddr( CONTEXT *context, BYTE *instr,
177 int long_addr, int segprefix, int *len )
179 int mod, rm, base = 0, index = 0, ss = 0, seg = 0, off;
180 LDT_ENTRY entry;
182 #define GET_VAL(val,type) \
183 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
185 *len = 0;
186 GET_VAL( &mod, BYTE );
187 rm = mod & 7;
188 mod >>= 6;
190 if (mod == 3)
192 switch(rm)
194 case 0: return (BYTE *)&context->Eax;
195 case 1: return (BYTE *)&context->Ecx;
196 case 2: return (BYTE *)&context->Edx;
197 case 3: return (BYTE *)&context->Ebx;
198 case 4: return (BYTE *)&context->Esp;
199 case 5: return (BYTE *)&context->Ebp;
200 case 6: return (BYTE *)&context->Esi;
201 case 7: return (BYTE *)&context->Edi;
205 if (long_addr)
207 if (rm == 4)
209 BYTE sib;
210 GET_VAL( &sib, BYTE );
211 rm = sib & 7;
212 ss = sib >> 6;
213 switch((sib >> 3) & 7)
215 case 0: index = context->Eax; break;
216 case 1: index = context->Ecx; break;
217 case 2: index = context->Edx; break;
218 case 3: index = context->Ebx; break;
219 case 4: index = 0; break;
220 case 5: index = context->Ebp; break;
221 case 6: index = context->Esi; break;
222 case 7: index = context->Edi; break;
226 switch(rm)
228 case 0: base = context->Eax; seg = context->SegDs; break;
229 case 1: base = context->Ecx; seg = context->SegDs; break;
230 case 2: base = context->Edx; seg = context->SegDs; break;
231 case 3: base = context->Ebx; seg = context->SegDs; break;
232 case 4: base = context->Esp; seg = context->SegSs; break;
233 case 5: base = context->Ebp; seg = context->SegSs; break;
234 case 6: base = context->Esi; seg = context->SegDs; break;
235 case 7: base = context->Edi; seg = context->SegDs; break;
237 switch (mod)
239 case 0:
240 if (rm == 5) /* special case: ds:(disp32) */
242 GET_VAL( &base, DWORD );
243 seg = context->SegDs;
245 break;
247 case 1: /* 8-bit disp */
248 GET_VAL( &off, BYTE );
249 base += (signed char)off;
250 break;
252 case 2: /* 32-bit disp */
253 GET_VAL( &off, DWORD );
254 base += (signed long)off;
255 break;
258 else /* short address */
260 switch(rm)
262 case 0: /* ds:(bx,si) */
263 base = LOWORD(context->Ebx) + LOWORD(context->Esi);
264 seg = context->SegDs;
265 break;
266 case 1: /* ds:(bx,di) */
267 base = LOWORD(context->Ebx) + LOWORD(context->Edi);
268 seg = context->SegDs;
269 break;
270 case 2: /* ss:(bp,si) */
271 base = LOWORD(context->Ebp) + LOWORD(context->Esi);
272 seg = context->SegSs;
273 break;
274 case 3: /* ss:(bp,di) */
275 base = LOWORD(context->Ebp) + LOWORD(context->Edi);
276 seg = context->SegSs;
277 break;
278 case 4: /* ds:(si) */
279 base = LOWORD(context->Esi);
280 seg = context->SegDs;
281 break;
282 case 5: /* ds:(di) */
283 base = LOWORD(context->Edi);
284 seg = context->SegDs;
285 break;
286 case 6: /* ss:(bp) */
287 base = LOWORD(context->Ebp);
288 seg = context->SegSs;
289 break;
290 case 7: /* ds:(bx) */
291 base = LOWORD(context->Ebx);
292 seg = context->SegDs;
293 break;
296 switch(mod)
298 case 0:
299 if (rm == 6) /* special case: ds:(disp16) */
301 GET_VAL( &base, WORD );
302 seg = context->SegDs;
304 break;
306 case 1: /* 8-bit disp */
307 GET_VAL( &off, BYTE );
308 base += (signed char)off;
309 break;
311 case 2: /* 16-bit disp */
312 GET_VAL( &off, WORD );
313 base += (signed short)off;
314 break;
316 base &= 0xffff;
318 if (segprefix != -1) seg = segprefix;
320 /* Make sure the segment and offset are valid */
321 if (wine_ldt_is_system(seg)) return (BYTE *)(base + (index << ss));
322 if ((seg & 7) != 7) return NULL;
323 wine_ldt_get_entry( seg, &entry );
324 if (wine_ldt_is_empty( &entry )) return NULL;
325 if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
326 return (BYTE *)wine_ldt_get_base(&entry) + base + (index << ss);
327 #undef GET_VAL
331 /***********************************************************************
332 * INSTR_EmulateLDS
334 * Emulate the LDS (and LES,LFS,etc.) instruction.
336 static BOOL INSTR_EmulateLDS( CONTEXT *context, BYTE *instr, int long_op,
337 int long_addr, int segprefix, int *len )
339 WORD seg;
340 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
341 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
342 long_addr, segprefix, len );
343 if (!addr)
344 return FALSE; /* Unable to emulate it */
345 seg = *(WORD *)(addr + (long_op ? 4 : 2));
347 if (!INSTR_ReplaceSelector( context, &seg ))
348 return FALSE; /* Unable to emulate it */
350 /* Now store the offset in the correct register */
352 store_reg_word( context, *regmodrm, addr, long_op );
354 /* Store the correct segment in the segment register */
356 switch(*instr)
358 case 0xc4: context->SegEs = seg; break; /* les */
359 case 0xc5: context->SegDs = seg; break; /* lds */
360 case 0x0f: switch(instr[1])
362 case 0xb2: context->SegSs = seg; break; /* lss */
363 case 0xb4: context->SegFs = seg; break; /* lfs */
364 case 0xb5: context->SegGs = seg; break; /* lgs */
366 break;
369 /* Add the opcode size to the total length */
371 *len += 1 + (*instr == 0x0f);
372 return TRUE;
375 /***********************************************************************
376 * INSTR_inport
378 * input on an I/O port
380 static DWORD INSTR_inport( WORD port, int size, CONTEXT *context )
382 DWORD res = DOSVM_inport( port, size );
384 if (TRACE_ON(io))
386 switch(size)
388 case 1:
389 TRACE_(io)( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
390 (WORD)context->SegCs, LOWORD(context->Eip));
391 break;
392 case 2:
393 TRACE_(io)( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
394 (WORD)context->SegCs, LOWORD(context->Eip));
395 break;
396 case 4:
397 TRACE_(io)( "0x%x < %08x @ %04x:%04x\n", port, res,
398 (WORD)context->SegCs, LOWORD(context->Eip));
399 break;
402 return res;
406 /***********************************************************************
407 * INSTR_outport
409 * output on an I/O port
411 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT *context )
413 DOSVM_outport( port, size, val );
415 if (TRACE_ON(io))
417 switch(size)
419 case 1:
420 TRACE_(io)("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
421 (WORD)context->SegCs, LOWORD(context->Eip));
422 break;
423 case 2:
424 TRACE_(io)("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
425 (WORD)context->SegCs, LOWORD(context->Eip));
426 break;
427 case 4:
428 TRACE_(io)("0x%x > %08x @ %04x:%04x\n", port, val,
429 (WORD)context->SegCs, LOWORD(context->Eip));
430 break;
436 /***********************************************************************
437 * __wine_emulate_instruction
439 * Emulate a privileged instruction.
440 * Returns exception continuation status.
442 DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT *context )
444 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
445 BYTE *instr;
447 long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
448 instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
449 if (!instr) return ExceptionContinueSearch;
451 /* First handle any possible prefix */
453 segprefix = -1; /* no prefix */
454 prefix = 1;
455 repX = 0;
456 prefixlen = 0;
457 while(prefix)
459 switch(*instr)
461 case 0x2e:
462 segprefix = context->SegCs;
463 break;
464 case 0x36:
465 segprefix = context->SegSs;
466 break;
467 case 0x3e:
468 segprefix = context->SegDs;
469 break;
470 case 0x26:
471 segprefix = context->SegEs;
472 break;
473 case 0x64:
474 segprefix = context->SegFs;
475 break;
476 case 0x65:
477 segprefix = context->SegGs;
478 break;
479 case 0x66:
480 long_op = !long_op; /* opcode size prefix */
481 break;
482 case 0x67:
483 long_addr = !long_addr; /* addr size prefix */
484 break;
485 case 0xf0: /* lock */
486 break;
487 case 0xf2: /* repne */
488 repX = 1;
489 break;
490 case 0xf3: /* repe */
491 repX = 2;
492 break;
493 default:
494 prefix = 0; /* no more prefixes */
495 break;
497 if (prefix)
499 instr++;
500 prefixlen++;
504 /* Now look at the actual instruction */
506 switch(*instr)
508 case 0x07: /* pop es */
509 case 0x17: /* pop ss */
510 case 0x1f: /* pop ds */
512 WORD seg = *(WORD *)get_stack( context );
513 if (INSTR_ReplaceSelector( context, &seg ))
515 switch(*instr)
517 case 0x07: context->SegEs = seg; break;
518 case 0x17: context->SegSs = seg; break;
519 case 0x1f: context->SegDs = seg; break;
521 add_stack(context, long_op ? 4 : 2);
522 context->Eip += prefixlen + 1;
523 return ExceptionContinueExecution;
526 break; /* Unable to emulate it */
528 case 0x0f: /* extended instruction */
529 switch(instr[1])
531 case 0x22: /* mov %eax, %crX */
532 switch (instr[2])
534 case 0xc0:
535 FIXME("mov %%eax, %%cr0 at 0x%08x, EAX=0x%08x\n",
536 context->Eip,context->Eax );
537 context->Eip += prefixlen+3;
538 return ExceptionContinueExecution;
539 default:
540 break; /* Fallthrough to bad instruction handling */
542 break; /* Fallthrough to bad instruction handling */
543 case 0x20: /* mov %crX, %eax */
544 switch (instr[2])
546 case 0xe0: /* mov %cr4, %eax */
547 /* CR4 register: See linux/arch/i386/mm/init.c, X86_CR4_ defs
548 * bit 0: VME Virtual Mode Exception ?
549 * bit 1: PVI Protected mode Virtual Interrupt
550 * bit 2: TSD Timestamp disable
551 * bit 3: DE Debugging extensions
552 * bit 4: PSE Page size extensions
553 * bit 5: PAE Physical address extension
554 * bit 6: MCE Machine check enable
555 * bit 7: PGE Enable global pages
556 * bit 8: PCE Enable performance counters at IPL3
558 FIXME("mov %%cr4, %%eax at 0x%08x\n",context->Eip);
559 context->Eax = 0;
560 context->Eip += prefixlen+3;
561 return ExceptionContinueExecution;
562 case 0xc0: /* mov %cr0, %eax */
563 FIXME("mov %%cr0, %%eax at 0x%08x\n",context->Eip);
564 context->Eax = 0x10; /* FIXME: set more bits ? */
565 context->Eip += prefixlen+3;
566 return ExceptionContinueExecution;
567 default: /* Fallthrough to illegal instruction */
568 break;
570 /* Fallthrough to illegal instruction */
571 break;
572 case 0x21: /* mov %drX, %eax */
573 switch (instr[2])
575 case 0xc8: /* mov %dr1, %eax */
576 TRACE("mov %%dr1, %%eax at 0x%08x\n",context->Eip);
577 context->Eax = context->Dr1;
578 context->Eip += prefixlen+3;
579 return ExceptionContinueExecution;
580 case 0xf8: /* mov %dr7, %eax */
581 TRACE("mov %%dr7, %%eax at 0x%08x\n",context->Eip);
582 context->Eax = 0x400;
583 context->Eip += prefixlen+3;
584 return ExceptionContinueExecution;
586 FIXME("Unsupported DR register, eip+2 is %02x\n", instr[2]);
587 /* fallthrough to illegal instruction */
588 break;
589 case 0x23: /* mov %eax, %drX */
590 switch (instr[2])
592 case 0xc8: /* mov %eax, %dr1 */
593 context->Dr1 = context->Eax;
594 context->Eip += prefixlen+3;
595 return ExceptionContinueExecution;
597 FIXME("Unsupported DR register, eip+2 is %02x\n", instr[2]);
598 /* fallthrough to illegal instruction */
599 break;
600 case 0xa1: /* pop fs */
602 WORD seg = *(WORD *)get_stack( context );
603 if (INSTR_ReplaceSelector( context, &seg ))
605 context->SegFs = seg;
606 add_stack(context, long_op ? 4 : 2);
607 context->Eip += prefixlen + 2;
608 return ExceptionContinueExecution;
611 break;
612 case 0xa9: /* pop gs */
614 WORD seg = *(WORD *)get_stack( context );
615 if (INSTR_ReplaceSelector( context, &seg ))
617 context->SegGs = seg;
618 add_stack(context, long_op ? 4 : 2);
619 context->Eip += prefixlen + 2;
620 return ExceptionContinueExecution;
623 break;
624 case 0xb2: /* lss addr,reg */
625 case 0xb4: /* lfs addr,reg */
626 case 0xb5: /* lgs addr,reg */
627 if (INSTR_EmulateLDS( context, instr, long_op,
628 long_addr, segprefix, &len ))
630 context->Eip += prefixlen + len;
631 return ExceptionContinueExecution;
633 break;
635 break; /* Unable to emulate it */
637 case 0x6c: /* insb */
638 case 0x6d: /* insw/d */
639 case 0x6e: /* outsb */
640 case 0x6f: /* outsw/d */
642 int typ = *instr; /* Just in case it's overwritten. */
643 int outp = (typ >= 0x6e);
644 unsigned long count = repX ?
645 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
646 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
647 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
648 int seg = outp ? context->SegDs : context->SegEs; /* FIXME: is this right? */
650 if (outp)
652 /* FIXME: Check segment is readable. */
654 else
656 /* FIXME: Check segment is writable. */
659 if (repX)
661 if (long_addr) context->Ecx = 0;
662 else SET_LOWORD(context->Ecx,0);
665 while (count-- > 0)
667 void *data;
668 WORD dx = LOWORD(context->Edx);
669 if (outp)
671 data = make_ptr( context, seg, context->Esi, long_addr );
672 if (long_addr) context->Esi += step;
673 else ADD_LOWORD(context->Esi,step);
675 else
677 data = make_ptr( context, seg, context->Edi, long_addr );
678 if (long_addr) context->Edi += step;
679 else ADD_LOWORD(context->Edi,step);
682 switch (typ)
684 case 0x6c:
685 *(BYTE *)data = INSTR_inport( dx, 1, context );
686 break;
687 case 0x6d:
688 if (long_op)
689 *(DWORD *)data = INSTR_inport( dx, 4, context );
690 else
691 *(WORD *)data = INSTR_inport( dx, 2, context );
692 break;
693 case 0x6e:
694 INSTR_outport( dx, 1, *(BYTE *)data, context );
695 break;
696 case 0x6f:
697 if (long_op)
698 INSTR_outport( dx, 4, *(DWORD *)data, context );
699 else
700 INSTR_outport( dx, 2, *(WORD *)data, context );
701 break;
704 context->Eip += prefixlen + 1;
706 return ExceptionContinueExecution;
708 case 0x8a: /* mov Eb, Gb */
709 case 0x8b: /* mov Ev, Gv */
711 BYTE *data = INSTR_GetOperandAddr(context, instr + 1, long_addr,
712 segprefix, &len);
713 unsigned int data_size = (*instr == 0x8b) ? (long_op ? 4 : 2) : 1;
714 struct idtr idtr = get_idtr();
715 unsigned int offset = data - idtr.base;
717 if (offset <= idtr.limit + 1 - data_size)
719 idt[1].LimitLow = 0x100; /* FIXME */
720 idt[2].LimitLow = 0x11E; /* FIXME */
721 idt[3].LimitLow = 0x500; /* FIXME */
723 switch (*instr)
725 case 0x8a: store_reg_byte( context, instr[1], (BYTE *)idt + offset ); break;
726 case 0x8b: store_reg_word( context, instr[1], (BYTE *)idt + offset, long_op ); break;
728 context->Eip += prefixlen + len + 1;
729 return ExceptionContinueExecution;
732 break; /* Unable to emulate it */
734 case 0x8e: /* mov XX,segment_reg */
736 WORD seg;
737 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
738 long_addr, segprefix, &len );
739 if (!addr)
740 break; /* Unable to emulate it */
741 seg = *(WORD *)addr;
742 if (!INSTR_ReplaceSelector( context, &seg ))
743 break; /* Unable to emulate it */
745 switch((instr[1] >> 3) & 7)
747 case 0:
748 context->SegEs = seg;
749 context->Eip += prefixlen + len + 1;
750 return ExceptionContinueExecution;
751 case 1: /* cs */
752 break;
753 case 2:
754 context->SegSs = seg;
755 context->Eip += prefixlen + len + 1;
756 return ExceptionContinueExecution;
757 case 3:
758 context->SegDs = seg;
759 context->Eip += prefixlen + len + 1;
760 return ExceptionContinueExecution;
761 case 4:
762 context->SegFs = seg;
763 context->Eip += prefixlen + len + 1;
764 return ExceptionContinueExecution;
765 case 5:
766 context->SegGs = seg;
767 context->Eip += prefixlen + len + 1;
768 return ExceptionContinueExecution;
769 case 6: /* unused */
770 case 7: /* unused */
771 break;
774 break; /* Unable to emulate it */
776 case 0xc4: /* les addr,reg */
777 case 0xc5: /* lds addr,reg */
778 if (INSTR_EmulateLDS( context, instr, long_op,
779 long_addr, segprefix, &len ))
781 context->Eip += prefixlen + len;
782 return ExceptionContinueExecution;
784 break; /* Unable to emulate it */
786 case 0xcd: /* int <XX> */
787 context->Eip += prefixlen + 2;
788 if (DOSVM_EmulateInterruptPM( context, instr[1] )) return ExceptionContinueExecution;
789 context->Eip -= prefixlen + 2; /* restore eip */
790 break; /* Unable to emulate it */
792 case 0xcf: /* iret */
793 if (wine_ldt_is_system(context->SegCs)) break; /* don't emulate it in 32-bit code */
794 if (long_op)
796 DWORD *stack = get_stack( context );
797 context->Eip = *stack++;
798 context->SegCs = *stack++;
799 context->EFlags = *stack;
800 add_stack(context, 3*sizeof(DWORD)); /* Pop the return address and flags */
802 else
804 WORD *stack = get_stack( context );
805 context->Eip = *stack++;
806 context->SegCs = *stack++;
807 SET_LOWORD(context->EFlags,*stack);
808 add_stack(context, 3*sizeof(WORD)); /* Pop the return address and flags */
810 return ExceptionContinueExecution;
812 case 0xe4: /* inb al,XX */
813 SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
814 context->Eip += prefixlen + 2;
815 return ExceptionContinueExecution;
817 case 0xe5: /* in (e)ax,XX */
818 if (long_op)
819 context->Eax = INSTR_inport( instr[1], 4, context );
820 else
821 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
822 context->Eip += prefixlen + 2;
823 return ExceptionContinueExecution;
825 case 0xe6: /* outb XX,al */
826 INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
827 context->Eip += prefixlen + 2;
828 return ExceptionContinueExecution;
830 case 0xe7: /* out XX,(e)ax */
831 if (long_op)
832 INSTR_outport( instr[1], 4, context->Eax, context );
833 else
834 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
835 context->Eip += prefixlen + 2;
836 return ExceptionContinueExecution;
838 case 0xec: /* inb al,dx */
839 SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
840 context->Eip += prefixlen + 1;
841 return ExceptionContinueExecution;
843 case 0xed: /* in (e)ax,dx */
844 if (long_op)
845 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
846 else
847 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
848 context->Eip += prefixlen + 1;
849 return ExceptionContinueExecution;
851 case 0xee: /* outb dx,al */
852 INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
853 context->Eip += prefixlen + 1;
854 return ExceptionContinueExecution;
856 case 0xef: /* out dx,(e)ax */
857 if (long_op)
858 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
859 else
860 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
861 context->Eip += prefixlen + 1;
862 return ExceptionContinueExecution;
864 case 0xfa: /* cli */
865 get_vm86_teb_info()->dpmi_vif = 0;
866 context->Eip += prefixlen + 1;
867 return ExceptionContinueExecution;
869 case 0xfb: /* sti */
870 get_vm86_teb_info()->dpmi_vif = 1;
871 context->Eip += prefixlen + 1;
872 if (get_vm86_teb_info()->vm86_pending)
874 get_vm86_teb_info()->vm86_pending = 0;
875 rec->ExceptionCode = EXCEPTION_VM86_STI;
876 break; /* Handle the pending event. */
878 return ExceptionContinueExecution;
880 return ExceptionContinueSearch; /* Unable to emulate it */
884 /***********************************************************************
885 * INSTR_vectored_handler
887 * Vectored exception handler used to emulate protected instructions
888 * from 32-bit code.
890 LONG CALLBACK INSTR_vectored_handler( EXCEPTION_POINTERS *ptrs )
892 EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
893 CONTEXT *context = ptrs->ContextRecord;
895 if (wine_ldt_is_system(context->SegCs) &&
896 (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
897 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION))
899 if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
900 return EXCEPTION_CONTINUE_EXECUTION;
902 return EXCEPTION_CONTINUE_SEARCH;
906 /***********************************************************************
907 * DOS3Call (KERNEL.102)
909 void WINAPI DOS3Call( CONTEXT *context )
911 __wine_call_int_handler( context, 0x21 );
915 /***********************************************************************
916 * NetBIOSCall (KERNEL.103)
918 void WINAPI NetBIOSCall16( CONTEXT *context )
920 __wine_call_int_handler( context, 0x5c );
924 /***********************************************************************
925 * GetSetKernelDOSProc (KERNEL.311)
927 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
929 FIXME("(DosProc=%p): stub\n", DosProc);
930 return NULL;