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