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