In GetVolumeInformationW handle the case when there are no permissions
[wine] / dlls / kernel / instr.c
1 /*
2  * Emulation of privileged instructions
3  *
4  * Copyright 1995 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25
26 #include "ntstatus.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "wine/winuser16.h"
31 #include "excpt.h"
32 #include "module.h"
33 #include "miscemu.h"
34 #include "wine/debug.h"
35 #include "kernel_private.h"
36 #include "thread.h"
37 #include "wine/exception.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(int);
40 WINE_DECLARE_DEBUG_CHANNEL(io);
41
42 /* macros to set parts of a DWORD */
43 #define SET_LOWORD(dw,val)  ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
44 #define SET_LOBYTE(dw,val)  ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
45 #define ADD_LOWORD(dw,val)  ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
46 #define ISV86(context)      ((context)->EFlags & 0x00020000)
47
48 inline static void add_stack( CONTEXT86 *context, int offset )
49 {
50     if (ISV86(context) || !IS_SELECTOR_32BIT(context->SegSs))
51         ADD_LOWORD( context->Esp, offset );
52     else
53         context->Esp += offset;
54 }
55
56 inline static void *make_ptr( CONTEXT86 *context, DWORD seg, DWORD off, int long_addr )
57 {
58     if (ISV86(context)) return (void *)((seg << 4) + LOWORD(off));
59     if (wine_ldt_is_system(seg)) return (void *)off;
60     if (!long_addr) off = LOWORD(off);
61     return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
62 }
63
64 inline static void *get_stack( CONTEXT86 *context )
65 {
66     if (ISV86(context)) return (void *)((context->SegSs << 4) + LOWORD(context->Esp));
67     return wine_ldt_get_ptr( context->SegSs, context->Esp );
68 }
69
70
71 static void (WINAPI *DOS_EmulateInterruptPM)( CONTEXT86 *context, BYTE intnum );
72 static void (WINAPI *DOS_CallBuiltinHandler)( CONTEXT86 *context, BYTE intnum );
73 static DWORD (WINAPI *DOS_inport)( int port, int size );
74 static void (WINAPI *DOS_outport)( int port, int size, DWORD val );
75
76
77 static void init_winedos(void)
78 {
79     static HMODULE module;
80
81     if (module) return;
82     module = LoadLibraryA( "winedos.dll" );
83     if (!module)
84     {
85         ERR("could not load winedos.dll, DOS subsystem unavailable\n");
86         module = (HMODULE)1;  /* don't try again */
87         return;
88     }
89 #define GET_ADDR(func)  DOS_##func = (void *)GetProcAddress(module, #func);
90     GET_ADDR(inport);
91     GET_ADDR(outport);
92     GET_ADDR(EmulateInterruptPM);
93     GET_ADDR(CallBuiltinHandler);
94 #undef GET_ADDR
95 }
96
97
98 /***********************************************************************
99  *           INSTR_ReplaceSelector
100  *
101  * Try to replace an invalid selector by a valid one.
102  * The only selector where it is allowed to do "mov ax,40;mov es,ax"
103  * is the so called 'bimodal' selector 0x40, which points to the BIOS
104  * data segment. Used by (at least) Borland products (and programs compiled
105  * using Borland products).
106  *
107  * See Undocumented Windows, Chapter 5, __0040.
108  */
109 static BOOL INSTR_ReplaceSelector( CONTEXT86 *context, WORD *sel )
110 {
111     if (*sel == 0x40)
112     {
113         static WORD sys_timer = 0;
114         if (!sys_timer)
115             sys_timer = CreateSystemTimer( 55, DOSMEM_Tick );
116         *sel = DOSMEM_BiosDataSeg;
117         return TRUE;
118     }
119     return FALSE;  /* Can't replace selector, crashdump */
120 }
121
122
123 /***********************************************************************
124  *           INSTR_GetOperandAddr
125  *
126  * Return the address of an instruction operand (from the mod/rm byte).
127  */
128 static BYTE *INSTR_GetOperandAddr( CONTEXT86 *context, BYTE *instr,
129                                    int long_addr, int segprefix, int *len )
130 {
131     int mod, rm, base, index = 0, ss = 0, seg = 0, off;
132     LDT_ENTRY entry;
133
134 #define GET_VAL(val,type) \
135     { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
136
137     *len = 0;
138     GET_VAL( &mod, BYTE );
139     rm = mod & 7;
140     mod >>= 6;
141
142     if (mod == 3)
143     {
144         switch(rm)
145         {
146         case 0: return (BYTE *)&context->Eax;
147         case 1: return (BYTE *)&context->Ecx;
148         case 2: return (BYTE *)&context->Edx;
149         case 3: return (BYTE *)&context->Ebx;
150         case 4: return (BYTE *)&context->Esp;
151         case 5: return (BYTE *)&context->Ebp;
152         case 6: return (BYTE *)&context->Esi;
153         case 7: return (BYTE *)&context->Edi;
154         }
155     }
156
157     if (long_addr)
158     {
159         if (rm == 4)
160         {
161             BYTE sib;
162             GET_VAL( &sib, BYTE );
163             rm = sib & 7;
164             ss = sib >> 6;
165             switch(sib >> 3)
166             {
167             case 0: index = context->Eax; break;
168             case 1: index = context->Ecx; break;
169             case 2: index = context->Edx; break;
170             case 3: index = context->Ebx; break;
171             case 4: index = 0; break;
172             case 5: index = context->Ebp; break;
173             case 6: index = context->Esi; break;
174             case 7: index = context->Edi; break;
175             }
176         }
177
178         switch(rm)
179         {
180         case 0: base = context->Eax; seg = context->SegDs; break;
181         case 1: base = context->Ecx; seg = context->SegDs; break;
182         case 2: base = context->Edx; seg = context->SegDs; break;
183         case 3: base = context->Ebx; seg = context->SegDs; break;
184         case 4: base = context->Esp; seg = context->SegSs; break;
185         case 5: base = context->Ebp; seg = context->SegSs; break;
186         case 6: base = context->Esi; seg = context->SegDs; break;
187         case 7: base = context->Edi; seg = context->SegDs; break;
188         }
189         switch (mod)
190         {
191         case 0:
192             if (rm == 5)  /* special case: ds:(disp32) */
193             {
194                 GET_VAL( &base, DWORD );
195                 seg = context->SegDs;
196             }
197             break;
198
199         case 1:  /* 8-bit disp */
200             GET_VAL( &off, BYTE );
201             base += (signed char)off;
202             break;
203
204         case 2:  /* 32-bit disp */
205             GET_VAL( &off, DWORD );
206             base += (signed long)off;
207             break;
208         }
209     }
210     else  /* short address */
211     {
212         switch(rm)
213         {
214         case 0:  /* ds:(bx,si) */
215             base = LOWORD(context->Ebx) + LOWORD(context->Esi);
216             seg  = context->SegDs;
217             break;
218         case 1:  /* ds:(bx,di) */
219             base = LOWORD(context->Ebx) + LOWORD(context->Edi);
220             seg  = context->SegDs;
221             break;
222         case 2:  /* ss:(bp,si) */
223             base = LOWORD(context->Ebp) + LOWORD(context->Esi);
224             seg  = context->SegSs;
225             break;
226         case 3:  /* ss:(bp,di) */
227             base = LOWORD(context->Ebp) + LOWORD(context->Edi);
228             seg  = context->SegSs;
229             break;
230         case 4:  /* ds:(si) */
231             base = LOWORD(context->Esi);
232             seg  = context->SegDs;
233             break;
234         case 5:  /* ds:(di) */
235             base = LOWORD(context->Edi);
236             seg  = context->SegDs;
237             break;
238         case 6:  /* ss:(bp) */
239             base = LOWORD(context->Ebp);
240             seg  = context->SegSs;
241             break;
242         case 7:  /* ds:(bx) */
243             base = LOWORD(context->Ebx);
244             seg  = context->SegDs;
245             break;
246         }
247
248         switch(mod)
249         {
250         case 0:
251             if (rm == 6)  /* special case: ds:(disp16) */
252             {
253                 GET_VAL( &base, WORD );
254                 seg  = context->SegDs;
255             }
256             break;
257
258         case 1:  /* 8-bit disp */
259             GET_VAL( &off, BYTE );
260             base += (signed char)off;
261             break;
262
263         case 2:  /* 16-bit disp */
264             GET_VAL( &off, WORD );
265             base += (signed short)off;
266             break;
267         }
268         base &= 0xffff;
269     }
270     if (segprefix != -1) seg = segprefix;
271
272     /* Make sure the segment and offset are valid */
273     if (wine_ldt_is_system(seg)) return (BYTE *)(base + (index << ss));
274     if ((seg & 7) != 7) return NULL;
275     wine_ldt_get_entry( seg, &entry );
276     if (wine_ldt_is_empty( &entry )) return NULL;
277     if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
278     return (BYTE *)wine_ldt_get_base(&entry) + base + (index << ss);
279 #undef GET_VAL
280 }
281
282
283 /***********************************************************************
284  *           INSTR_EmulateLDS
285  *
286  * Emulate the LDS (and LES,LFS,etc.) instruction.
287  */
288 static BOOL INSTR_EmulateLDS( CONTEXT86 *context, BYTE *instr, int long_op,
289                               int long_addr, int segprefix, int *len )
290 {
291     WORD seg;
292     BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
293     BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
294                                        long_addr, segprefix, len );
295     if (!addr)
296         return FALSE;  /* Unable to emulate it */
297     seg = *(WORD *)(addr + (long_op ? 4 : 2));
298
299     if (!INSTR_ReplaceSelector( context, &seg ))
300         return FALSE;  /* Unable to emulate it */
301
302     /* Now store the offset in the correct register */
303
304     switch((*regmodrm >> 3) & 7)
305     {
306     case 0:
307         if (long_op) context->Eax = *(DWORD *)addr;
308         else SET_LOWORD(context->Eax,*(WORD *)addr);
309         break;
310     case 1:
311         if (long_op) context->Ecx = *(DWORD *)addr;
312         else SET_LOWORD(context->Ecx,*(WORD *)addr);
313         break;
314     case 2:
315         if (long_op) context->Edx = *(DWORD *)addr;
316         else SET_LOWORD(context->Edx,*(WORD *)addr);
317         break;
318     case 3:
319         if (long_op) context->Ebx = *(DWORD *)addr;
320         else SET_LOWORD(context->Ebx,*(WORD *)addr);
321         break;
322     case 4:
323         if (long_op) context->Esp = *(DWORD *)addr;
324         else SET_LOWORD(context->Esp,*(WORD *)addr);
325         break;
326     case 5:
327         if (long_op) context->Ebp = *(DWORD *)addr;
328         else SET_LOWORD(context->Ebp,*(WORD *)addr);
329         break;
330     case 6:
331         if (long_op) context->Esi = *(DWORD *)addr;
332         else SET_LOWORD(context->Esi,*(WORD *)addr);
333         break;
334     case 7:
335         if (long_op) context->Edi = *(DWORD *)addr;
336         else SET_LOWORD(context->Edi,*(WORD *)addr);
337         break;
338     }
339
340     /* Store the correct segment in the segment register */
341
342     switch(*instr)
343     {
344     case 0xc4: context->SegEs = seg; break;  /* les */
345     case 0xc5: context->SegDs = seg; break;  /* lds */
346     case 0x0f: switch(instr[1])
347                {
348                case 0xb2: context->SegSs = seg; break;  /* lss */
349                case 0xb4: context->SegFs = seg; break;  /* lfs */
350                case 0xb5: context->SegGs = seg; break;  /* lgs */
351                }
352                break;
353     }
354
355     /* Add the opcode size to the total length */
356
357     *len += 1 + (*instr == 0x0f);
358     return TRUE;
359 }
360
361 /***********************************************************************
362  *           INSTR_inport
363  *
364  * input on a I/O port
365  */
366 static DWORD INSTR_inport( WORD port, int size, CONTEXT86 *context )
367 {
368     DWORD res = ~0U;
369
370     if (!DOS_inport) init_winedos();
371     if (DOS_inport) res = DOS_inport( port, size );
372
373     if (TRACE_ON(io))
374     {
375         switch(size)
376         {
377         case 1:
378             DPRINTF( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
379                      (WORD)context->SegCs, LOWORD(context->Eip));
380             break;
381         case 2:
382             DPRINTF( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
383                      (WORD)context->SegCs, LOWORD(context->Eip));
384             break;
385         case 4:
386             DPRINTF( "0x%x < %08lx @ %04x:%04x\n", port, res,
387                      (WORD)context->SegCs, LOWORD(context->Eip));
388             break;
389         }
390     }
391     return res;
392 }
393
394
395 /***********************************************************************
396  *           INSTR_outport
397  *
398  * output on a I/O port
399  */
400 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT86 *context )
401 {
402     if (!DOS_outport) init_winedos();
403     if (DOS_outport) DOS_outport( port, size, val );
404
405     if (TRACE_ON(io))
406     {
407         switch(size)
408         {
409         case 1:
410             DPRINTF("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
411                     (WORD)context->SegCs, LOWORD(context->Eip));
412             break;
413         case 2:
414             DPRINTF("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
415                     (WORD)context->SegCs, LOWORD(context->Eip));
416             break;
417         case 4:
418             DPRINTF("0x%x > %08lx @ %04x:%04x\n", port, val,
419                     (WORD)context->SegCs, LOWORD(context->Eip));
420             break;
421         }
422     }
423 }
424
425
426 /***********************************************************************
427  *           INSTR_EmulateInstruction
428  *
429  * Emulate a privileged instruction.
430  * Returns exception continuation status.
431  */
432 DWORD INSTR_EmulateInstruction( EXCEPTION_RECORD *rec, CONTEXT86 *context )
433 {
434     int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
435     BYTE *instr;
436
437     long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
438     instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
439     if (!instr) return ExceptionContinueSearch;
440
441     /* First handle any possible prefix */
442
443     segprefix = -1;  /* no prefix */
444     prefix = 1;
445     repX = 0;
446     prefixlen = 0;
447     while(prefix)
448     {
449         switch(*instr)
450         {
451         case 0x2e:
452             segprefix = context->SegCs;
453             break;
454         case 0x36:
455             segprefix = context->SegSs;
456             break;
457         case 0x3e:
458             segprefix = context->SegDs;
459             break;
460         case 0x26:
461             segprefix = context->SegEs;
462             break;
463         case 0x64:
464             segprefix = context->SegFs;
465             break;
466         case 0x65:
467             segprefix = context->SegGs;
468             break;
469         case 0x66:
470             long_op = !long_op;  /* opcode size prefix */
471             break;
472         case 0x67:
473             long_addr = !long_addr;  /* addr size prefix */
474             break;
475         case 0xf0:  /* lock */
476             break;
477         case 0xf2:  /* repne */
478             repX = 1;
479             break;
480         case 0xf3:  /* repe */
481             repX = 2;
482             break;
483         default:
484             prefix = 0;  /* no more prefixes */
485             break;
486         }
487         if (prefix)
488         {
489             instr++;
490             prefixlen++;
491         }
492     }
493
494     /* Now look at the actual instruction */
495
496     switch(*instr)
497     {
498         case 0x07: /* pop es */
499         case 0x17: /* pop ss */
500         case 0x1f: /* pop ds */
501             {
502                 WORD seg = *(WORD *)get_stack( context );
503                 if (INSTR_ReplaceSelector( context, &seg ))
504                 {
505                     switch(*instr)
506                     {
507                     case 0x07: context->SegEs = seg; break;
508                     case 0x17: context->SegSs = seg; break;
509                     case 0x1f: context->SegDs = seg; break;
510                     }
511                     add_stack(context, long_op ? 4 : 2);
512                     context->Eip += prefixlen + 1;
513                     return ExceptionContinueExecution;
514                 }
515             }
516             break;  /* Unable to emulate it */
517
518         case 0x0f: /* extended instruction */
519             switch(instr[1])
520             {
521             case 0x22: /* mov eax, crX */
522                 switch (instr[2])
523                 {
524                 case 0xc0:
525                         ERR("mov eax,cr0 at 0x%08lx, EAX=0x%08lx\n",
526                             context->Eip,context->Eax );
527                         context->Eip += prefixlen+3;
528                         return ExceptionContinueExecution;
529                 default:
530                         break; /*fallthrough to bad instruction handling */
531                 }
532                 break; /*fallthrough to bad instruction handling */
533             case 0x20: /* mov crX, eax */
534                 switch (instr[2])
535                 {
536                 case 0xe0: /* mov cr4, eax */
537                     /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
538                      * bit 0: VME       Virtual Mode Exception ?
539                      * bit 1: PVI       Protected mode Virtual Interrupt
540                      * bit 2: TSD       Timestamp disable
541                      * bit 3: DE        Debugging extensions
542                      * bit 4: PSE       Page size extensions
543                      * bit 5: PAE   Physical address extension
544                      * bit 6: MCE       Machine check enable
545                      * bit 7: PGE   Enable global pages
546                      * bit 8: PCE       Enable performance counters at IPL3
547                      */
548                     ERR("mov cr4,eax at 0x%08lx\n",context->Eip);
549                     context->Eax = 0;
550                     context->Eip += prefixlen+3;
551                     return ExceptionContinueExecution;
552                 case 0xc0: /* mov cr0, eax */
553                     ERR("mov cr0,eax at 0x%08lx\n",context->Eip);
554                     context->Eax = 0x10; /* FIXME: set more bits ? */
555                     context->Eip += prefixlen+3;
556                     return ExceptionContinueExecution;
557                 default: /* fallthrough to illegal instruction */
558                     break;
559                 }
560                 /* fallthrough to illegal instruction */
561                 break;
562             case 0x21: /* mov drX, eax */
563                 switch (instr[2])
564                 {
565                 case 0xf8: /* mov dr7, eax */
566                     TRACE("mov dr7,eax at 0x%08lx\n",context->Eip);
567                     context->Eax = 0x400;
568                     context->Eip += prefixlen+3;
569                     return ExceptionContinueExecution;
570                 default: /* fallthrough to illegal instruction */
571                     ERR("Unknown DR register, eip+2 is %02x\n", instr[2]);
572                     break;
573                 }
574                 /* fallthrough to illegal instruction */
575                 break;
576             case 0xa1: /* pop fs */
577                 {
578                     WORD seg = *(WORD *)get_stack( context );
579                     if (INSTR_ReplaceSelector( context, &seg ))
580                     {
581                         context->SegFs = seg;
582                         add_stack(context, long_op ? 4 : 2);
583                         context->Eip += prefixlen + 2;
584                         return ExceptionContinueExecution;
585                     }
586                 }
587                 break;
588             case 0xa9: /* pop gs */
589                 {
590                     WORD seg = *(WORD *)get_stack( context );
591                     if (INSTR_ReplaceSelector( context, &seg ))
592                     {
593                         context->SegGs = seg;
594                         add_stack(context, long_op ? 4 : 2);
595                         context->Eip += prefixlen + 2;
596                         return ExceptionContinueExecution;
597                     }
598                 }
599                 break;
600             case 0xb2: /* lss addr,reg */
601             case 0xb4: /* lfs addr,reg */
602             case 0xb5: /* lgs addr,reg */
603                 if (INSTR_EmulateLDS( context, instr, long_op,
604                                       long_addr, segprefix, &len ))
605                 {
606                     context->Eip += prefixlen + len;
607                     return ExceptionContinueExecution;
608                 }
609                 break;
610             }
611             break;  /* Unable to emulate it */
612
613         case 0x6c: /* insb     */
614         case 0x6d: /* insw/d   */
615         case 0x6e: /* outsb    */
616         case 0x6f: /* outsw/d  */
617             {
618               int typ = *instr;  /* Just in case it's overwritten.  */
619               int outp = (typ >= 0x6e);
620               unsigned long count = repX ?
621                           (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
622               int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
623               int step = (context->EFlags & 0x400) ? -opsize : +opsize;
624               int seg = outp ? context->SegDs : context->SegEs;  /* FIXME: is this right? */
625
626               if (outp)
627                 /* FIXME: Check segment readable.  */
628                 (void)0;
629               else
630                 /* FIXME: Check segment writeable.  */
631                 (void)0;
632
633               if (repX)
634               {
635                 if (long_addr) context->Ecx = 0;
636                 else SET_LOWORD(context->Ecx,0);
637               }
638
639               while (count-- > 0)
640                 {
641                   void *data;
642                   WORD dx = LOWORD(context->Edx);
643                   if (outp)
644                   {
645                       data = make_ptr( context, seg, context->Esi, long_addr );
646                       if (long_addr) context->Esi += step;
647                       else ADD_LOWORD(context->Esi,step);
648                   }
649                   else
650                   {
651                       data = make_ptr( context, seg, context->Edi, long_addr );
652                       if (long_addr) context->Edi += step;
653                       else ADD_LOWORD(context->Edi,step);
654                   }
655
656                   switch (typ)
657                   {
658                     case 0x6c:
659                       *(BYTE *)data = INSTR_inport( dx, 1, context );
660                       break;
661                     case 0x6d:
662                       if (long_op)
663                           *(DWORD *)data = INSTR_inport( dx, 4, context );
664                       else
665                           *(WORD *)data = INSTR_inport( dx, 2, context );
666                       break;
667                     case 0x6e:
668                         INSTR_outport( dx, 1, *(BYTE *)data, context );
669                         break;
670                     case 0x6f:
671                         if (long_op)
672                             INSTR_outport( dx, 4, *(DWORD *)data, context );
673                         else
674                             INSTR_outport( dx, 2, *(WORD *)data, context );
675                         break;
676                     }
677                 }
678               context->Eip += prefixlen + 1;
679             }
680             return ExceptionContinueExecution;
681
682         case 0x8e: /* mov XX,segment_reg */
683             {
684                 WORD seg;
685                 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
686                                                   long_addr, segprefix, &len );
687                 if (!addr)
688                     break;  /* Unable to emulate it */
689                 seg = *(WORD *)addr;
690                 if (!INSTR_ReplaceSelector( context, &seg ))
691                     break;  /* Unable to emulate it */
692
693                 switch((instr[1] >> 3) & 7)
694                 {
695                 case 0:
696                     context->SegEs = seg;
697                     context->Eip += prefixlen + len + 1;
698                     return ExceptionContinueExecution;
699                 case 1:  /* cs */
700                     break;
701                 case 2:
702                     context->SegSs = seg;
703                     context->Eip += prefixlen + len + 1;
704                     return ExceptionContinueExecution;
705                 case 3:
706                     context->SegDs = seg;
707                     context->Eip += prefixlen + len + 1;
708                     return ExceptionContinueExecution;
709                 case 4:
710                     context->SegFs = seg;
711                     context->Eip += prefixlen + len + 1;
712                     return ExceptionContinueExecution;
713                 case 5:
714                     context->SegGs = seg;
715                     context->Eip += prefixlen + len + 1;
716                     return ExceptionContinueExecution;
717                 case 6:  /* unused */
718                 case 7:  /* unused */
719                     break;
720                 }
721             }
722             break;  /* Unable to emulate it */
723
724         case 0xc4: /* les addr,reg */
725         case 0xc5: /* lds addr,reg */
726             if (INSTR_EmulateLDS( context, instr, long_op,
727                                   long_addr, segprefix, &len ))
728             {
729                 context->Eip += prefixlen + len;
730                 return ExceptionContinueExecution;
731             }
732             break;  /* Unable to emulate it */
733
734         case 0xcd: /* int <XX> */
735             if (wine_ldt_is_system(context->SegCs)) break;  /* don't emulate it in 32-bit code */
736             if (!DOS_EmulateInterruptPM) init_winedos();
737             if (DOS_EmulateInterruptPM)
738             {
739                 context->Eip += prefixlen + 2;
740                 DOS_EmulateInterruptPM( context, instr[1] );
741                 return ExceptionContinueExecution;
742             }
743             break;  /* Unable to emulate it */
744
745         case 0xcf: /* iret */
746             if (wine_ldt_is_system(context->SegCs)) break;  /* don't emulate it in 32-bit code */
747             if (long_op)
748             {
749                 DWORD *stack = get_stack( context );
750                 context->Eip = *stack++;
751                 context->SegCs  = *stack++;
752                 context->EFlags = *stack;
753                 add_stack(context, 3*sizeof(DWORD));  /* Pop the return address and flags */
754             }
755             else
756             {
757                 WORD *stack = get_stack( context );
758                 context->Eip = *stack++;
759                 context->SegCs  = *stack++;
760                 SET_LOWORD(context->EFlags,*stack);
761                 add_stack(context, 3*sizeof(WORD));  /* Pop the return address and flags */
762             }
763             return ExceptionContinueExecution;
764
765         case 0xe4: /* inb al,XX */
766             SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
767             context->Eip += prefixlen + 2;
768             return ExceptionContinueExecution;
769
770         case 0xe5: /* in (e)ax,XX */
771             if (long_op)
772                 context->Eax = INSTR_inport( instr[1], 4, context );
773             else
774                 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
775             context->Eip += prefixlen + 2;
776             return ExceptionContinueExecution;
777
778         case 0xe6: /* outb XX,al */
779             INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
780             context->Eip += prefixlen + 2;
781             return ExceptionContinueExecution;
782
783         case 0xe7: /* out XX,(e)ax */
784             if (long_op)
785                 INSTR_outport( instr[1], 4, context->Eax, context );
786             else
787                 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
788             context->Eip += prefixlen + 2;
789             return ExceptionContinueExecution;
790
791         case 0xec: /* inb al,dx */
792             SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
793             context->Eip += prefixlen + 1;
794             return ExceptionContinueExecution;
795
796         case 0xed: /* in (e)ax,dx */
797             if (long_op)
798                 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
799             else
800                 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
801             context->Eip += prefixlen + 1;
802             return ExceptionContinueExecution;
803
804         case 0xee: /* outb dx,al */
805             INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
806             context->Eip += prefixlen + 1;
807             return ExceptionContinueExecution;
808
809         case 0xef: /* out dx,(e)ax */
810             if (long_op)
811                 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
812             else
813                 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
814             context->Eip += prefixlen + 1;
815             return ExceptionContinueExecution;
816
817         case 0xfa: /* cli */
818             NtCurrentTeb()->dpmi_vif = 0;
819             context->Eip += prefixlen + 1;
820             return ExceptionContinueExecution;
821
822         case 0xfb: /* sti */
823             NtCurrentTeb()->dpmi_vif = 1;
824             context->Eip += prefixlen + 1;
825             if (NtCurrentTeb()->vm86_pending)
826             {
827                 NtCurrentTeb()->vm86_pending = 0;
828                 rec->ExceptionCode = EXCEPTION_VM86_STI;
829                 break; /* Handle the pending event. */
830             }
831             return ExceptionContinueExecution;
832     }
833     return ExceptionContinueSearch;  /* Unable to emulate it */
834 }
835
836
837 /***********************************************************************
838  *           INSTR_CallBuiltinHandler
839  */
840 void INSTR_CallBuiltinHandler( CONTEXT86 *context, BYTE intnum )
841 {
842     if (!DOS_CallBuiltinHandler) init_winedos();
843     if (DOS_CallBuiltinHandler) DOS_CallBuiltinHandler( context, intnum );
844 }
845
846
847 /***********************************************************************
848  *           DOS3Call         (KERNEL.102)
849  */
850 void WINAPI DOS3Call( CONTEXT86 *context )
851 {
852     INSTR_CallBuiltinHandler( context, 0x21 );
853 }
854
855
856 /***********************************************************************
857  *           NetBIOSCall      (KERNEL.103)
858  */
859 void WINAPI NetBIOSCall16( CONTEXT86 *context )
860 {
861     INSTR_CallBuiltinHandler( context, 0x5c );
862 }
863
864
865 /***********************************************************************
866  *              GetSetKernelDOSProc (KERNEL.311)
867  */
868 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
869 {
870     FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);
871     return NULL;
872 }