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