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