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