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