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