Always try to load the 32-bit owner dll instead of directly loading
[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 (char *)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 0xa1: /* pop fs */
563                 {
564                     WORD seg = *(WORD *)get_stack( context );
565                     if (INSTR_ReplaceSelector( context, &seg ))
566                     {
567                         context->SegFs = seg;
568                         add_stack(context, long_op ? 4 : 2);
569                         context->Eip += prefixlen + 2;
570                         return ExceptionContinueExecution;
571                     }
572                 }
573                 break;
574             case 0xa9: /* pop gs */
575                 {
576                     WORD seg = *(WORD *)get_stack( context );
577                     if (INSTR_ReplaceSelector( context, &seg ))
578                     {
579                         context->SegGs = seg;
580                         add_stack(context, long_op ? 4 : 2);
581                         context->Eip += prefixlen + 2;
582                         return ExceptionContinueExecution;
583                     }
584                 }
585                 break;
586             case 0xb2: /* lss addr,reg */
587             case 0xb4: /* lfs addr,reg */
588             case 0xb5: /* lgs addr,reg */
589                 if (INSTR_EmulateLDS( context, instr, long_op,
590                                       long_addr, segprefix, &len ))
591                 {
592                     context->Eip += prefixlen + len;
593                     return ExceptionContinueExecution;
594                 }
595                 break;
596             }
597             break;  /* Unable to emulate it */
598
599         case 0x6c: /* insb     */
600         case 0x6d: /* insw/d   */
601         case 0x6e: /* outsb    */
602         case 0x6f: /* outsw/d  */
603             {
604               int typ = *instr;  /* Just in case it's overwritten.  */
605               int outp = (typ >= 0x6e);
606               unsigned long count = repX ?
607                           (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
608               int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
609               int step = (context->EFlags & 0x400) ? -opsize : +opsize;
610               int seg = outp ? context->SegDs : context->SegEs;  /* FIXME: is this right? */
611
612               if (outp)
613                 /* FIXME: Check segment readable.  */
614                 (void)0;
615               else
616                 /* FIXME: Check segment writeable.  */
617                 (void)0;
618
619               if (repX)
620               {
621                 if (long_addr) context->Ecx = 0;
622                 else SET_LOWORD(context->Ecx,0);
623               }
624
625               while (count-- > 0)
626                 {
627                   void *data;
628                   WORD dx = LOWORD(context->Edx);
629                   if (outp)
630                   {
631                       data = make_ptr( context, seg, context->Esi, long_addr );
632                       if (long_addr) context->Esi += step;
633                       else ADD_LOWORD(context->Esi,step);
634                   }
635                   else
636                   {
637                       data = make_ptr( context, seg, context->Edi, long_addr );
638                       if (long_addr) context->Edi += step;
639                       else ADD_LOWORD(context->Edi,step);
640                   }
641
642                   switch (typ)
643                   {
644                     case 0x6c:
645                       *(BYTE *)data = INSTR_inport( dx, 1, context );
646                       break;
647                     case 0x6d:
648                       if (long_op)
649                           *(DWORD *)data = INSTR_inport( dx, 4, context );
650                       else
651                           *(WORD *)data = INSTR_inport( dx, 2, context );
652                       break;
653                     case 0x6e:
654                         INSTR_outport( dx, 1, *(BYTE *)data, context );
655                         break;
656                     case 0x6f:
657                         if (long_op)
658                             INSTR_outport( dx, 4, *(DWORD *)data, context );
659                         else
660                             INSTR_outport( dx, 2, *(WORD *)data, context );
661                         break;
662                     }
663                 }
664               context->Eip += prefixlen + 1;
665             }
666             return ExceptionContinueExecution;
667
668         case 0x8e: /* mov XX,segment_reg */
669             {
670                 WORD seg;
671                 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
672                                                   long_addr, segprefix, &len );
673                 if (!addr)
674                     break;  /* Unable to emulate it */
675                 seg = *(WORD *)addr;
676                 if (!INSTR_ReplaceSelector( context, &seg ))
677                     break;  /* Unable to emulate it */
678
679                 switch((instr[1] >> 3) & 7)
680                 {
681                 case 0:
682                     context->SegEs = seg;
683                     context->Eip += prefixlen + len + 1;
684                     return ExceptionContinueExecution;
685                 case 1:  /* cs */
686                     break;
687                 case 2:
688                     context->SegSs = seg;
689                     context->Eip += prefixlen + len + 1;
690                     return ExceptionContinueExecution;
691                 case 3:
692                     context->SegDs = seg;
693                     context->Eip += prefixlen + len + 1;
694                     return ExceptionContinueExecution;
695                 case 4:
696                     context->SegFs = seg;
697                     context->Eip += prefixlen + len + 1;
698                     return ExceptionContinueExecution;
699                 case 5:
700                     context->SegGs = seg;
701                     context->Eip += prefixlen + len + 1;
702                     return ExceptionContinueExecution;
703                 case 6:  /* unused */
704                 case 7:  /* unused */
705                     break;
706                 }
707             }
708             break;  /* Unable to emulate it */
709
710         case 0xc4: /* les addr,reg */
711         case 0xc5: /* lds addr,reg */
712             if (INSTR_EmulateLDS( context, instr, long_op,
713                                   long_addr, segprefix, &len ))
714             {
715                 context->Eip += prefixlen + len;
716                 return ExceptionContinueExecution;
717             }
718             break;  /* Unable to emulate it */
719
720         case 0xcd: /* int <XX> */
721             if (wine_ldt_is_system(context->SegCs)) break;  /* don't emulate it in 32-bit code */
722             if (!DOS_EmulateInterruptPM) init_winedos();
723             if (DOS_EmulateInterruptPM)
724             {
725                 context->Eip += prefixlen + 2;
726                 DOS_EmulateInterruptPM( context, instr[1] );
727                 return ExceptionContinueExecution;
728             }
729             break;  /* Unable to emulate it */
730
731         case 0xcf: /* iret */
732             if (wine_ldt_is_system(context->SegCs)) break;  /* don't emulate it in 32-bit code */
733             if (long_op)
734             {
735                 DWORD *stack = get_stack( context );
736                 context->Eip = *stack++;
737                 context->SegCs  = *stack++;
738                 context->EFlags = *stack;
739                 add_stack(context, 3*sizeof(DWORD));  /* Pop the return address and flags */
740             }
741             else
742             {
743                 WORD *stack = get_stack( context );
744                 context->Eip = *stack++;
745                 context->SegCs  = *stack++;
746                 SET_LOWORD(context->EFlags,*stack);
747                 add_stack(context, 3*sizeof(WORD));  /* Pop the return address and flags */
748             }
749             return ExceptionContinueExecution;
750
751         case 0xe4: /* inb al,XX */
752             SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
753             context->Eip += prefixlen + 2;
754             return ExceptionContinueExecution;
755
756         case 0xe5: /* in (e)ax,XX */
757             if (long_op)
758                 context->Eax = INSTR_inport( instr[1], 4, context );
759             else
760                 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
761             context->Eip += prefixlen + 2;
762             return ExceptionContinueExecution;
763
764         case 0xe6: /* outb XX,al */
765             INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
766             context->Eip += prefixlen + 2;
767             return ExceptionContinueExecution;
768
769         case 0xe7: /* out XX,(e)ax */
770             if (long_op)
771                 INSTR_outport( instr[1], 4, context->Eax, context );
772             else
773                 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
774             context->Eip += prefixlen + 2;
775             return ExceptionContinueExecution;
776
777         case 0xec: /* inb al,dx */
778             SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
779             context->Eip += prefixlen + 1;
780             return ExceptionContinueExecution;
781
782         case 0xed: /* in (e)ax,dx */
783             if (long_op)
784                 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
785             else
786                 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
787             context->Eip += prefixlen + 1;
788             return ExceptionContinueExecution;
789
790         case 0xee: /* outb dx,al */
791             INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
792             context->Eip += prefixlen + 1;
793             return ExceptionContinueExecution;
794
795         case 0xef: /* out dx,(e)ax */
796             if (long_op)
797                 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
798             else
799                 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
800             context->Eip += prefixlen + 1;
801             return ExceptionContinueExecution;
802
803         case 0xfa: /* cli */
804             NtCurrentTeb()->dpmi_vif = 0;
805             context->Eip += prefixlen + 1;
806             return ExceptionContinueExecution;
807
808         case 0xfb: /* sti */
809             NtCurrentTeb()->dpmi_vif = 1;
810             context->Eip += prefixlen + 1;
811             return ExceptionContinueExecution;
812     }
813     return ExceptionContinueSearch;  /* Unable to emulate it */
814 }
815
816
817 /***********************************************************************
818  *           INSTR_CallBuiltinHandler
819  */
820 void INSTR_CallBuiltinHandler( CONTEXT86 *context, BYTE intnum )
821 {
822     if (!DOS_CallBuiltinHandler) init_winedos();
823     if (DOS_CallBuiltinHandler) DOS_CallBuiltinHandler( context, intnum );
824 }
825
826
827 /***********************************************************************
828  *           DOS3Call         (KERNEL.102)
829  */
830 void WINAPI DOS3Call( CONTEXT86 *context )
831 {
832     INSTR_CallBuiltinHandler( context, 0x21 );
833 }
834
835
836 /***********************************************************************
837  *           NetBIOSCall      (KERNEL.103)
838  */
839 void WINAPI NetBIOSCall16( CONTEXT86 *context )
840 {
841     INSTR_CallBuiltinHandler( context, 0x5c );
842 }
843
844
845 /***********************************************************************
846  *              GetSetKernelDOSProc (KERNEL.311)
847  */
848 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
849 {
850     FIXME("(DosProc=0x%08x): stub\n", (UINT)DosProc);
851     return NULL;
852 }