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