api-ms-win-core-comm-l1-1-0: Add dll.
[wine.git] / dlls / krnl386.exe16 / instr.c
blob1437b4c541bd622821a59bb1389c47a3b6c3164c
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)))
46 static inline void add_stack( CONTEXT *context, int offset )
48 if (!IS_SELECTOR_32BIT(context->SegSs))
49 ADD_LOWORD( context->Esp, offset );
50 else
51 context->Esp += offset;
54 static inline void *make_ptr( CONTEXT *context, DWORD seg, DWORD off, int long_addr )
56 if (wine_ldt_is_system(seg)) return (void *)off;
57 if (!long_addr) off = LOWORD(off);
58 return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
61 static inline void *get_stack( CONTEXT *context )
63 return wine_ldt_get_ptr( context->SegSs, context->Esp );
66 #include "pshpack1.h"
67 struct idtr
69 WORD limit;
70 BYTE *base;
72 #include "poppack.h"
74 static LDT_ENTRY idt[256];
76 static inline struct idtr get_idtr(void)
78 struct idtr ret;
79 #if defined(__i386__) && defined(__GNUC__)
80 __asm__( "sidtl %0" : "=m" (ret) );
81 #else
82 ret.base = (BYTE *)idt;
83 ret.limit = sizeof(idt) - 1;
84 #endif
85 return ret;
89 /***********************************************************************
90 * INSTR_ReplaceSelector
92 * Try to replace an invalid selector by a valid one.
93 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
94 * is the so called 'bimodal' selector 0x40, which points to the BIOS
95 * data segment. Used by (at least) Borland products (and programs compiled
96 * using Borland products).
98 * See Undocumented Windows, Chapter 5, __0040.
100 static BOOL INSTR_ReplaceSelector( CONTEXT *context, WORD *sel )
102 if (*sel == 0x40)
104 DOSVM_start_bios_timer();
105 *sel = DOSMEM_BiosDataSeg;
106 return TRUE;
108 return FALSE; /* Can't replace selector, crashdump */
112 /* store an operand into a register */
113 static void store_reg_word( CONTEXT *context, BYTE regmodrm, const BYTE *addr, int long_op )
115 switch((regmodrm >> 3) & 7)
117 case 0:
118 if (long_op) context->Eax = *(const DWORD *)addr;
119 else SET_LOWORD(context->Eax, *(const WORD *)addr);
120 break;
121 case 1:
122 if (long_op) context->Ecx = *(const DWORD *)addr;
123 else SET_LOWORD(context->Ecx, *(const WORD *)addr);
124 break;
125 case 2:
126 if (long_op) context->Edx = *(const DWORD *)addr;
127 else SET_LOWORD(context->Edx, *(const WORD *)addr);
128 break;
129 case 3:
130 if (long_op) context->Ebx = *(const DWORD *)addr;
131 else SET_LOWORD(context->Ebx, *(const WORD *)addr);
132 break;
133 case 4:
134 if (long_op) context->Esp = *(const DWORD *)addr;
135 else SET_LOWORD(context->Esp, *(const WORD *)addr);
136 break;
137 case 5:
138 if (long_op) context->Ebp = *(const DWORD *)addr;
139 else SET_LOWORD(context->Ebp, *(const WORD *)addr);
140 break;
141 case 6:
142 if (long_op) context->Esi = *(const DWORD *)addr;
143 else SET_LOWORD(context->Esi, *(const WORD *)addr);
144 break;
145 case 7:
146 if (long_op) context->Edi = *(const DWORD *)addr;
147 else SET_LOWORD(context->Edi, *(const WORD *)addr);
148 break;
152 /* store an operand into a byte register */
153 static void store_reg_byte( CONTEXT *context, BYTE regmodrm, const BYTE *addr )
155 switch((regmodrm >> 3) & 7)
157 case 0: context->Eax = (context->Eax & 0xffffff00) | *addr; break;
158 case 1: context->Ecx = (context->Ecx & 0xffffff00) | *addr; break;
159 case 2: context->Edx = (context->Edx & 0xffffff00) | *addr; break;
160 case 3: context->Ebx = (context->Ebx & 0xffffff00) | *addr; break;
161 case 4: context->Eax = (context->Eax & 0xffff00ff) | (*addr << 8); break;
162 case 5: context->Ecx = (context->Ecx & 0xffff00ff) | (*addr << 8); break;
163 case 6: context->Edx = (context->Edx & 0xffff00ff) | (*addr << 8); break;
164 case 7: context->Ebx = (context->Ebx & 0xffff00ff) | (*addr << 8); break;
168 /***********************************************************************
169 * INSTR_GetOperandAddr
171 * Return the address of an instruction operand (from the mod/rm byte).
173 static BYTE *INSTR_GetOperandAddr( CONTEXT *context, BYTE *instr,
174 int long_addr, int segprefix, int *len )
176 int mod, rm, base = 0, index = 0, ss = 0, seg = 0, off;
177 LDT_ENTRY entry;
179 #define GET_VAL(val,type) \
180 { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
182 *len = 0;
183 GET_VAL( &mod, BYTE );
184 rm = mod & 7;
185 mod >>= 6;
187 if (mod == 3)
189 switch(rm)
191 case 0: return (BYTE *)&context->Eax;
192 case 1: return (BYTE *)&context->Ecx;
193 case 2: return (BYTE *)&context->Edx;
194 case 3: return (BYTE *)&context->Ebx;
195 case 4: return (BYTE *)&context->Esp;
196 case 5: return (BYTE *)&context->Ebp;
197 case 6: return (BYTE *)&context->Esi;
198 case 7: return (BYTE *)&context->Edi;
202 if (long_addr)
204 if (rm == 4)
206 BYTE sib;
207 GET_VAL( &sib, BYTE );
208 rm = sib & 7;
209 ss = sib >> 6;
210 switch((sib >> 3) & 7)
212 case 0: index = context->Eax; break;
213 case 1: index = context->Ecx; break;
214 case 2: index = context->Edx; break;
215 case 3: index = context->Ebx; break;
216 case 4: index = 0; break;
217 case 5: index = context->Ebp; break;
218 case 6: index = context->Esi; break;
219 case 7: index = context->Edi; break;
223 switch(rm)
225 case 0: base = context->Eax; seg = context->SegDs; break;
226 case 1: base = context->Ecx; seg = context->SegDs; break;
227 case 2: base = context->Edx; seg = context->SegDs; break;
228 case 3: base = context->Ebx; seg = context->SegDs; break;
229 case 4: base = context->Esp; seg = context->SegSs; break;
230 case 5: base = context->Ebp; seg = context->SegSs; break;
231 case 6: base = context->Esi; seg = context->SegDs; break;
232 case 7: base = context->Edi; seg = context->SegDs; break;
234 switch (mod)
236 case 0:
237 if (rm == 5) /* special case: ds:(disp32) */
239 GET_VAL( &base, DWORD );
240 seg = context->SegDs;
242 break;
244 case 1: /* 8-bit disp */
245 GET_VAL( &off, BYTE );
246 base += (signed char)off;
247 break;
249 case 2: /* 32-bit disp */
250 GET_VAL( &off, DWORD );
251 base += (signed long)off;
252 break;
255 else /* short address */
257 switch(rm)
259 case 0: /* ds:(bx,si) */
260 base = LOWORD(context->Ebx) + LOWORD(context->Esi);
261 seg = context->SegDs;
262 break;
263 case 1: /* ds:(bx,di) */
264 base = LOWORD(context->Ebx) + LOWORD(context->Edi);
265 seg = context->SegDs;
266 break;
267 case 2: /* ss:(bp,si) */
268 base = LOWORD(context->Ebp) + LOWORD(context->Esi);
269 seg = context->SegSs;
270 break;
271 case 3: /* ss:(bp,di) */
272 base = LOWORD(context->Ebp) + LOWORD(context->Edi);
273 seg = context->SegSs;
274 break;
275 case 4: /* ds:(si) */
276 base = LOWORD(context->Esi);
277 seg = context->SegDs;
278 break;
279 case 5: /* ds:(di) */
280 base = LOWORD(context->Edi);
281 seg = context->SegDs;
282 break;
283 case 6: /* ss:(bp) */
284 base = LOWORD(context->Ebp);
285 seg = context->SegSs;
286 break;
287 case 7: /* ds:(bx) */
288 base = LOWORD(context->Ebx);
289 seg = context->SegDs;
290 break;
293 switch(mod)
295 case 0:
296 if (rm == 6) /* special case: ds:(disp16) */
298 GET_VAL( &base, WORD );
299 seg = context->SegDs;
301 break;
303 case 1: /* 8-bit disp */
304 GET_VAL( &off, BYTE );
305 base += (signed char)off;
306 break;
308 case 2: /* 16-bit disp */
309 GET_VAL( &off, WORD );
310 base += (signed short)off;
311 break;
313 base &= 0xffff;
315 if (segprefix != -1) seg = segprefix;
317 /* Make sure the segment and offset are valid */
318 if (wine_ldt_is_system(seg)) return (BYTE *)(base + (index << ss));
319 if ((seg & 7) != 7) return NULL;
320 wine_ldt_get_entry( seg, &entry );
321 if (wine_ldt_is_empty( &entry )) return NULL;
322 if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
323 return (BYTE *)wine_ldt_get_base(&entry) + base + (index << ss);
324 #undef GET_VAL
328 /***********************************************************************
329 * INSTR_EmulateLDS
331 * Emulate the LDS (and LES,LFS,etc.) instruction.
333 static BOOL INSTR_EmulateLDS( CONTEXT *context, BYTE *instr, int long_op,
334 int long_addr, int segprefix, int *len )
336 WORD seg;
337 BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
338 BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
339 long_addr, segprefix, len );
340 if (!addr)
341 return FALSE; /* Unable to emulate it */
342 seg = *(WORD *)(addr + (long_op ? 4 : 2));
344 if (!INSTR_ReplaceSelector( context, &seg ))
345 return FALSE; /* Unable to emulate it */
347 /* Now store the offset in the correct register */
349 store_reg_word( context, *regmodrm, addr, long_op );
351 /* Store the correct segment in the segment register */
353 switch(*instr)
355 case 0xc4: context->SegEs = seg; break; /* les */
356 case 0xc5: context->SegDs = seg; break; /* lds */
357 case 0x0f: switch(instr[1])
359 case 0xb2: context->SegSs = seg; break; /* lss */
360 case 0xb4: context->SegFs = seg; break; /* lfs */
361 case 0xb5: context->SegGs = seg; break; /* lgs */
363 break;
366 /* Add the opcode size to the total length */
368 *len += 1 + (*instr == 0x0f);
369 return TRUE;
372 /***********************************************************************
373 * INSTR_inport
375 * input on an I/O port
377 static DWORD INSTR_inport( WORD port, int size, CONTEXT *context )
379 DWORD res = DOSVM_inport( port, size );
381 if (TRACE_ON(io))
383 switch(size)
385 case 1:
386 TRACE_(io)( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
387 (WORD)context->SegCs, LOWORD(context->Eip));
388 break;
389 case 2:
390 TRACE_(io)( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
391 (WORD)context->SegCs, LOWORD(context->Eip));
392 break;
393 case 4:
394 TRACE_(io)( "0x%x < %08x @ %04x:%04x\n", port, res,
395 (WORD)context->SegCs, LOWORD(context->Eip));
396 break;
399 return res;
403 /***********************************************************************
404 * INSTR_outport
406 * output on an I/O port
408 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT *context )
410 DOSVM_outport( port, size, val );
412 if (TRACE_ON(io))
414 switch(size)
416 case 1:
417 TRACE_(io)("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
418 (WORD)context->SegCs, LOWORD(context->Eip));
419 break;
420 case 2:
421 TRACE_(io)("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
422 (WORD)context->SegCs, LOWORD(context->Eip));
423 break;
424 case 4:
425 TRACE_(io)("0x%x > %08x @ %04x:%04x\n", port, val,
426 (WORD)context->SegCs, LOWORD(context->Eip));
427 break;
433 /***********************************************************************
434 * __wine_emulate_instruction
436 * Emulate a privileged instruction.
437 * Returns exception continuation status.
439 DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT *context )
441 int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
442 BYTE *instr;
444 long_op = long_addr = IS_SELECTOR_32BIT(context->SegCs);
445 instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
446 if (!instr) return ExceptionContinueSearch;
448 /* First handle any possible prefix */
450 segprefix = -1; /* no prefix */
451 prefix = 1;
452 repX = 0;
453 prefixlen = 0;
454 while(prefix)
456 switch(*instr)
458 case 0x2e:
459 segprefix = context->SegCs;
460 break;
461 case 0x36:
462 segprefix = context->SegSs;
463 break;
464 case 0x3e:
465 segprefix = context->SegDs;
466 break;
467 case 0x26:
468 segprefix = context->SegEs;
469 break;
470 case 0x64:
471 segprefix = context->SegFs;
472 break;
473 case 0x65:
474 segprefix = context->SegGs;
475 break;
476 case 0x66:
477 long_op = !long_op; /* opcode size prefix */
478 break;
479 case 0x67:
480 long_addr = !long_addr; /* addr size prefix */
481 break;
482 case 0xf0: /* lock */
483 break;
484 case 0xf2: /* repne */
485 repX = 1;
486 break;
487 case 0xf3: /* repe */
488 repX = 2;
489 break;
490 default:
491 prefix = 0; /* no more prefixes */
492 break;
494 if (prefix)
496 instr++;
497 prefixlen++;
501 /* Now look at the actual instruction */
503 switch(*instr)
505 case 0x07: /* pop es */
506 case 0x17: /* pop ss */
507 case 0x1f: /* pop ds */
509 WORD seg = *(WORD *)get_stack( context );
510 if (INSTR_ReplaceSelector( context, &seg ))
512 switch(*instr)
514 case 0x07: context->SegEs = seg; break;
515 case 0x17: context->SegSs = seg; break;
516 case 0x1f: context->SegDs = seg; break;
518 add_stack(context, long_op ? 4 : 2);
519 context->Eip += prefixlen + 1;
520 return ExceptionContinueExecution;
523 break; /* Unable to emulate it */
525 case 0x0f: /* extended instruction */
526 switch(instr[1])
528 case 0x22: /* mov %eax, %crX */
529 switch (instr[2])
531 case 0xc0:
532 FIXME("mov %%eax, %%cr0 at 0x%08x, EAX=0x%08x\n",
533 context->Eip,context->Eax );
534 context->Eip += prefixlen+3;
535 return ExceptionContinueExecution;
536 default:
537 break; /* Fallthrough to bad instruction handling */
539 break; /* Fallthrough to bad instruction handling */
540 case 0x20: /* mov %crX, %eax */
541 switch (instr[2])
543 case 0xe0: /* mov %cr4, %eax */
544 /* CR4 register: See linux/arch/i386/mm/init.c, X86_CR4_ defs
545 * bit 0: VME Virtual Mode Exception ?
546 * bit 1: PVI Protected mode Virtual Interrupt
547 * bit 2: TSD Timestamp disable
548 * bit 3: DE Debugging extensions
549 * bit 4: PSE Page size extensions
550 * bit 5: PAE Physical address extension
551 * bit 6: MCE Machine check enable
552 * bit 7: PGE Enable global pages
553 * bit 8: PCE Enable performance counters at IPL3
555 FIXME("mov %%cr4, %%eax at 0x%08x\n",context->Eip);
556 context->Eax = 0;
557 context->Eip += prefixlen+3;
558 return ExceptionContinueExecution;
559 case 0xc0: /* mov %cr0, %eax */
560 FIXME("mov %%cr0, %%eax at 0x%08x\n",context->Eip);
561 context->Eax = 0x10; /* FIXME: set more bits ? */
562 context->Eip += prefixlen+3;
563 return ExceptionContinueExecution;
564 default: /* Fallthrough to illegal instruction */
565 break;
567 /* Fallthrough to illegal instruction */
568 break;
569 case 0x21: /* mov %drX, %eax */
570 switch (instr[2])
572 case 0xc8: /* mov %dr1, %eax */
573 TRACE("mov %%dr1, %%eax at 0x%08x\n",context->Eip);
574 context->Eax = context->Dr1;
575 context->Eip += prefixlen+3;
576 return ExceptionContinueExecution;
577 case 0xf8: /* mov %dr7, %eax */
578 TRACE("mov %%dr7, %%eax at 0x%08x\n",context->Eip);
579 context->Eax = 0x400;
580 context->Eip += prefixlen+3;
581 return ExceptionContinueExecution;
583 FIXME("Unsupported DR register, eip+2 is %02x\n", instr[2]);
584 /* fallthrough to illegal instruction */
585 break;
586 case 0x23: /* mov %eax, %drX */
587 switch (instr[2])
589 case 0xc8: /* mov %eax, %dr1 */
590 context->Dr1 = context->Eax;
591 context->Eip += prefixlen+3;
592 return ExceptionContinueExecution;
594 FIXME("Unsupported DR register, eip+2 is %02x\n", instr[2]);
595 /* fallthrough to illegal instruction */
596 break;
597 case 0xa1: /* pop fs */
599 WORD seg = *(WORD *)get_stack( context );
600 if (INSTR_ReplaceSelector( context, &seg ))
602 context->SegFs = seg;
603 add_stack(context, long_op ? 4 : 2);
604 context->Eip += prefixlen + 2;
605 return ExceptionContinueExecution;
608 break;
609 case 0xa9: /* pop gs */
611 WORD seg = *(WORD *)get_stack( context );
612 if (INSTR_ReplaceSelector( context, &seg ))
614 context->SegGs = seg;
615 add_stack(context, long_op ? 4 : 2);
616 context->Eip += prefixlen + 2;
617 return ExceptionContinueExecution;
620 break;
621 case 0xb2: /* lss addr,reg */
622 case 0xb4: /* lfs addr,reg */
623 case 0xb5: /* lgs addr,reg */
624 if (INSTR_EmulateLDS( context, instr, long_op,
625 long_addr, segprefix, &len ))
627 context->Eip += prefixlen + len;
628 return ExceptionContinueExecution;
630 break;
632 break; /* Unable to emulate it */
634 case 0x6c: /* insb */
635 case 0x6d: /* insw/d */
636 case 0x6e: /* outsb */
637 case 0x6f: /* outsw/d */
639 int typ = *instr; /* Just in case it's overwritten. */
640 int outp = (typ >= 0x6e);
641 unsigned long count = repX ?
642 (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
643 int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
644 int step = (context->EFlags & 0x400) ? -opsize : +opsize;
645 int seg;
647 if (outp)
649 /* Check if there is a segment prefix override and honour it */
650 seg = segprefix == -1 ? context->SegDs : segprefix;
651 /* FIXME: Check segment is readable. */
653 else
655 seg = context->SegEs;
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 context->Eip += prefixlen + 1;
866 return ExceptionContinueExecution;
868 case 0xfb: /* sti */
869 context->Eip += prefixlen + 1;
870 return ExceptionContinueExecution;
872 return ExceptionContinueSearch; /* Unable to emulate it */
876 /***********************************************************************
877 * INSTR_vectored_handler
879 * Vectored exception handler used to emulate protected instructions
880 * from 32-bit code.
882 LONG CALLBACK INSTR_vectored_handler( EXCEPTION_POINTERS *ptrs )
884 EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
885 CONTEXT *context = ptrs->ContextRecord;
887 if (wine_ldt_is_system(context->SegCs) &&
888 (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
889 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION))
891 if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
892 return EXCEPTION_CONTINUE_EXECUTION;
894 return EXCEPTION_CONTINUE_SEARCH;
898 /***********************************************************************
899 * DOS3Call (KERNEL.102)
901 void WINAPI DOS3Call( CONTEXT *context )
903 __wine_call_int_handler( context, 0x21 );
907 /***********************************************************************
908 * NetBIOSCall (KERNEL.103)
910 void WINAPI NetBIOSCall16( CONTEXT *context )
912 __wine_call_int_handler( context, 0x5c );
916 /***********************************************************************
917 * GetSetKernelDOSProc (KERNEL.311)
919 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
921 FIXME("(DosProc=%p): stub\n", DosProc);
922 return NULL;