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