kernel32: LOADPARAMS16->showCmd can be NULL, do not crash in that case.
[wine] / dlls / kernel32 / instr.c
1 /*
2  * Emulation of privileged instructions
3  *
4  * Copyright 1995 Alexandre Julliard
5  * Copyright 2005 Ivan Leo Puoti
6  * Copyright 2005 Laurent Pinchart
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/winuser16.h"
31 #include "excpt.h"
32 #include "thread.h"
33 #include "wine/debug.h"
34 #include "kernel_private.h"
35 #include "kernel16_private.h"
36 #include "wine/exception.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(int);
39 WINE_DECLARE_DEBUG_CHANNEL(io);
40
41 /* macros to set parts of a DWORD */
42 #define SET_LOWORD(dw,val)  ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
43 #define SET_LOBYTE(dw,val)  ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
44 #define ADD_LOWORD(dw,val)  ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
45 #define ISV86(context)      ((context)->EFlags & 0x00020000)
46
47 static inline 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 static inline void *make_ptr( CONTEXT86 *context, DWORD seg, DWORD off, int long_addr )
56 {
57     if (ISV86(context)) return (void *)((seg << 4) + LOWORD(off));
58     if (wine_ldt_is_system(seg)) return (void *)off;
59     if (!long_addr) off = LOWORD(off);
60     return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
61 }
62
63 static inline void *get_stack( CONTEXT86 *context )
64 {
65     if (ISV86(context)) return (void *)((context->SegSs << 4) + LOWORD(context->Esp));
66     return wine_ldt_get_ptr( context->SegSs, context->Esp );
67 }
68
69 #include "pshpack1.h"
70 struct idtr
71 {
72     WORD  limit;
73     BYTE *base;
74 };
75 #include "poppack.h"
76
77 static LDT_ENTRY idt[256];
78
79 static inline struct idtr get_idtr(void)
80 {
81     struct idtr ret;
82 #if defined(__i386__) && defined(__GNUC__)
83     __asm__( "sidtl %0" : "=m" (ret) );
84 #else
85     ret.base = (BYTE *)idt;
86     ret.limit = sizeof(idt) - 1;
87 #endif
88     return ret;
89 }
90
91
92 /***********************************************************************
93  *           INSTR_ReplaceSelector
94  *
95  * Try to replace an invalid selector by a valid one.
96  * The only selector where it is allowed to do "mov ax,40;mov es,ax"
97  * is the so called 'bimodal' selector 0x40, which points to the BIOS
98  * data segment. Used by (at least) Borland products (and programs compiled
99  * using Borland products).
100  *
101  * See Undocumented Windows, Chapter 5, __0040.
102  */
103 static BOOL INSTR_ReplaceSelector( CONTEXT86 *context, WORD *sel )
104 {
105     if (*sel == 0x40)
106     {
107         static WORD sys_timer = 0;
108         if (!sys_timer)
109         {
110             if (!winedos.BiosTick) load_winedos();
111             if (winedos.BiosTick)
112                 sys_timer = CreateSystemTimer( 55, winedos.BiosTick );
113         }
114         *sel = DOSMEM_BiosDataSeg;
115         return TRUE;
116     }
117     return FALSE;  /* Can't replace selector, crashdump */
118 }
119
120
121 /* store an operand into a register */
122 static void store_reg( CONTEXT86 *context, BYTE regmodrm, const BYTE *addr, int long_op )
123 {
124     switch((regmodrm >> 3) & 7)
125     {
126     case 0:
127         if (long_op) context->Eax = *(const DWORD *)addr;
128         else SET_LOWORD(context->Eax, *(const WORD *)addr);
129         break;
130     case 1:
131         if (long_op) context->Ecx = *(const DWORD *)addr;
132         else SET_LOWORD(context->Ecx, *(const WORD *)addr);
133         break;
134     case 2:
135         if (long_op) context->Edx = *(const DWORD *)addr;
136         else SET_LOWORD(context->Edx, *(const WORD *)addr);
137         break;
138     case 3:
139         if (long_op) context->Ebx = *(const DWORD *)addr;
140         else SET_LOWORD(context->Ebx, *(const WORD *)addr);
141         break;
142     case 4:
143         if (long_op) context->Esp = *(const DWORD *)addr;
144         else SET_LOWORD(context->Esp, *(const WORD *)addr);
145         break;
146     case 5:
147         if (long_op) context->Ebp = *(const DWORD *)addr;
148         else SET_LOWORD(context->Ebp, *(const WORD *)addr);
149         break;
150     case 6:
151         if (long_op) context->Esi = *(const DWORD *)addr;
152         else SET_LOWORD(context->Esi, *(const WORD *)addr);
153         break;
154     case 7:
155         if (long_op) context->Edi = *(const DWORD *)addr;
156         else SET_LOWORD(context->Edi, *(const WORD *)addr);
157         break;
158     }
159 }
160
161 /***********************************************************************
162  *           INSTR_GetOperandAddr
163  *
164  * Return the address of an instruction operand (from the mod/rm byte).
165  */
166 static BYTE *INSTR_GetOperandAddr( CONTEXT86 *context, BYTE *instr,
167                                    int long_addr, int segprefix, int *len )
168 {
169     int mod, rm, base = 0, index = 0, ss = 0, seg = 0, off;
170     LDT_ENTRY entry;
171
172 #define GET_VAL(val,type) \
173     { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }
174
175     *len = 0;
176     GET_VAL( &mod, BYTE );
177     rm = mod & 7;
178     mod >>= 6;
179
180     if (mod == 3)
181     {
182         switch(rm)
183         {
184         case 0: return (BYTE *)&context->Eax;
185         case 1: return (BYTE *)&context->Ecx;
186         case 2: return (BYTE *)&context->Edx;
187         case 3: return (BYTE *)&context->Ebx;
188         case 4: return (BYTE *)&context->Esp;
189         case 5: return (BYTE *)&context->Ebp;
190         case 6: return (BYTE *)&context->Esi;
191         case 7: return (BYTE *)&context->Edi;
192         }
193     }
194
195     if (long_addr)
196     {
197         if (rm == 4)
198         {
199             BYTE sib;
200             GET_VAL( &sib, BYTE );
201             rm = sib & 7;
202             ss = sib >> 6;
203             switch(sib >> 3)
204             {
205             case 0: index = context->Eax; break;
206             case 1: index = context->Ecx; break;
207             case 2: index = context->Edx; break;
208             case 3: index = context->Ebx; break;
209             case 4: index = 0; break;
210             case 5: index = context->Ebp; break;
211             case 6: index = context->Esi; break;
212             case 7: index = context->Edi; break;
213             }
214         }
215
216         switch(rm)
217         {
218         case 0: base = context->Eax; seg = context->SegDs; break;
219         case 1: base = context->Ecx; seg = context->SegDs; break;
220         case 2: base = context->Edx; seg = context->SegDs; break;
221         case 3: base = context->Ebx; seg = context->SegDs; break;
222         case 4: base = context->Esp; seg = context->SegSs; break;
223         case 5: base = context->Ebp; seg = context->SegSs; break;
224         case 6: base = context->Esi; seg = context->SegDs; break;
225         case 7: base = context->Edi; seg = context->SegDs; break;
226         }
227         switch (mod)
228         {
229         case 0:
230             if (rm == 5)  /* special case: ds:(disp32) */
231             {
232                 GET_VAL( &base, DWORD );
233                 seg = context->SegDs;
234             }
235             break;
236
237         case 1:  /* 8-bit disp */
238             GET_VAL( &off, BYTE );
239             base += (signed char)off;
240             break;
241
242         case 2:  /* 32-bit disp */
243             GET_VAL( &off, DWORD );
244             base += (signed long)off;
245             break;
246         }
247     }
248     else  /* short address */
249     {
250         switch(rm)
251         {
252         case 0:  /* ds:(bx,si) */
253             base = LOWORD(context->Ebx) + LOWORD(context->Esi);
254             seg  = context->SegDs;
255             break;
256         case 1:  /* ds:(bx,di) */
257             base = LOWORD(context->Ebx) + LOWORD(context->Edi);
258             seg  = context->SegDs;
259             break;
260         case 2:  /* ss:(bp,si) */
261             base = LOWORD(context->Ebp) + LOWORD(context->Esi);
262             seg  = context->SegSs;
263             break;
264         case 3:  /* ss:(bp,di) */
265             base = LOWORD(context->Ebp) + LOWORD(context->Edi);
266             seg  = context->SegSs;
267             break;
268         case 4:  /* ds:(si) */
269             base = LOWORD(context->Esi);
270             seg  = context->SegDs;
271             break;
272         case 5:  /* ds:(di) */
273             base = LOWORD(context->Edi);
274             seg  = context->SegDs;
275             break;
276         case 6:  /* ss:(bp) */
277             base = LOWORD(context->Ebp);
278             seg  = context->SegSs;
279             break;
280         case 7:  /* ds:(bx) */
281             base = LOWORD(context->Ebx);
282             seg  = context->SegDs;
283             break;
284         }
285
286         switch(mod)
287         {
288         case 0:
289             if (rm == 6)  /* special case: ds:(disp16) */
290             {
291                 GET_VAL( &base, WORD );
292                 seg  = context->SegDs;
293             }
294             break;
295
296         case 1:  /* 8-bit disp */
297             GET_VAL( &off, BYTE );
298             base += (signed char)off;
299             break;
300
301         case 2:  /* 16-bit disp */
302             GET_VAL( &off, WORD );
303             base += (signed short)off;
304             break;
305         }
306         base &= 0xffff;
307     }
308     if (segprefix != -1) seg = segprefix;
309
310     /* Make sure the segment and offset are valid */
311     if (wine_ldt_is_system(seg)) return (BYTE *)(base + (index << ss));
312     if ((seg & 7) != 7) return NULL;
313     wine_ldt_get_entry( seg, &entry );
314     if (wine_ldt_is_empty( &entry )) return NULL;
315     if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
316     return (BYTE *)wine_ldt_get_base(&entry) + base + (index << ss);
317 #undef GET_VAL
318 }
319
320
321 /***********************************************************************
322  *           INSTR_EmulateLDS
323  *
324  * Emulate the LDS (and LES,LFS,etc.) instruction.
325  */
326 static BOOL INSTR_EmulateLDS( CONTEXT86 *context, BYTE *instr, int long_op,
327                               int long_addr, int segprefix, int *len )
328 {
329     WORD seg;
330     BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
331     BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
332                                        long_addr, segprefix, len );
333     if (!addr)
334         return FALSE;  /* Unable to emulate it */
335     seg = *(WORD *)(addr + (long_op ? 4 : 2));
336
337     if (!INSTR_ReplaceSelector( context, &seg ))
338         return FALSE;  /* Unable to emulate it */
339
340     /* Now store the offset in the correct register */
341
342     store_reg( context, *regmodrm, addr, long_op );
343
344     /* Store the correct segment in the segment register */
345
346     switch(*instr)
347     {
348     case 0xc4: context->SegEs = seg; break;  /* les */
349     case 0xc5: context->SegDs = seg; break;  /* lds */
350     case 0x0f: switch(instr[1])
351                {
352                case 0xb2: context->SegSs = seg; break;  /* lss */
353                case 0xb4: context->SegFs = seg; break;  /* lfs */
354                case 0xb5: context->SegGs = seg; break;  /* lgs */
355                }
356                break;
357     }
358
359     /* Add the opcode size to the total length */
360
361     *len += 1 + (*instr == 0x0f);
362     return TRUE;
363 }
364
365 /***********************************************************************
366  *           INSTR_inport
367  *
368  * input on an I/O port
369  */
370 static DWORD INSTR_inport( WORD port, int size, CONTEXT86 *context )
371 {
372     DWORD res = ~0U;
373
374     if (!winedos.inport) load_winedos();
375     if (winedos.inport) res = winedos.inport( port, size );
376
377     if (TRACE_ON(io))
378     {
379         switch(size)
380         {
381         case 1:
382             TRACE_(io)( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
383                      (WORD)context->SegCs, LOWORD(context->Eip));
384             break;
385         case 2:
386             TRACE_(io)( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
387                      (WORD)context->SegCs, LOWORD(context->Eip));
388             break;
389         case 4:
390             TRACE_(io)( "0x%x < %08x @ %04x:%04x\n", port, res,
391                      (WORD)context->SegCs, LOWORD(context->Eip));
392             break;
393         }
394     }
395     return res;
396 }
397
398
399 /***********************************************************************
400  *           INSTR_outport
401  *
402  * output on an I/O port
403  */
404 static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT86 *context )
405 {
406     if (!winedos.outport) load_winedos();
407     if (winedos.outport) winedos.outport( port, size, val );
408
409     if (TRACE_ON(io))
410     {
411         switch(size)
412         {
413         case 1:
414             TRACE_(io)("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
415                     (WORD)context->SegCs, LOWORD(context->Eip));
416             break;
417         case 2:
418             TRACE_(io)("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
419                     (WORD)context->SegCs, LOWORD(context->Eip));
420             break;
421         case 4:
422             TRACE_(io)("0x%x > %08x @ %04x:%04x\n", port, val,
423                     (WORD)context->SegCs, LOWORD(context->Eip));
424             break;
425         }
426     }
427 }
428
429
430 /***********************************************************************
431  *           __wine_emulate_instruction
432  *
433  * Emulate a privileged instruction.
434  * Returns exception continuation status.
435  */
436 DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT86 *context )
437 {
438     int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
439     BYTE *instr;
440
441     long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
442     instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
443     if (!instr) return ExceptionContinueSearch;
444
445     /* First handle any possible prefix */
446
447     segprefix = -1;  /* no prefix */
448     prefix = 1;
449     repX = 0;
450     prefixlen = 0;
451     while(prefix)
452     {
453         switch(*instr)
454         {
455         case 0x2e:
456             segprefix = context->SegCs;
457             break;
458         case 0x36:
459             segprefix = context->SegSs;
460             break;
461         case 0x3e:
462             segprefix = context->SegDs;
463             break;
464         case 0x26:
465             segprefix = context->SegEs;
466             break;
467         case 0x64:
468             segprefix = context->SegFs;
469             break;
470         case 0x65:
471             segprefix = context->SegGs;
472             break;
473         case 0x66:
474             long_op = !long_op;  /* opcode size prefix */
475             break;
476         case 0x67:
477             long_addr = !long_addr;  /* addr size prefix */
478             break;
479         case 0xf0:  /* lock */
480             break;
481         case 0xf2:  /* repne */
482             repX = 1;
483             break;
484         case 0xf3:  /* repe */
485             repX = 2;
486             break;
487         default:
488             prefix = 0;  /* no more prefixes */
489             break;
490         }
491         if (prefix)
492         {
493             instr++;
494             prefixlen++;
495         }
496     }
497
498     /* Now look at the actual instruction */
499
500     switch(*instr)
501     {
502         case 0x07: /* pop es */
503         case 0x17: /* pop ss */
504         case 0x1f: /* pop ds */
505             {
506                 WORD seg = *(WORD *)get_stack( context );
507                 if (INSTR_ReplaceSelector( context, &seg ))
508                 {
509                     switch(*instr)
510                     {
511                     case 0x07: context->SegEs = seg; break;
512                     case 0x17: context->SegSs = seg; break;
513                     case 0x1f: context->SegDs = seg; break;
514                     }
515                     add_stack(context, long_op ? 4 : 2);
516                     context->Eip += prefixlen + 1;
517                     return ExceptionContinueExecution;
518                 }
519             }
520             break;  /* Unable to emulate it */
521
522         case 0x0f: /* extended instruction */
523             switch(instr[1])
524             {
525             case 0x22: /* mov eax, crX */
526                 switch (instr[2])
527                 {
528                 case 0xc0:
529                         ERR("mov eax,cr0 at 0x%08x, EAX=0x%08x\n",
530                             context->Eip,context->Eax );
531                         context->Eip += prefixlen+3;
532                         return ExceptionContinueExecution;
533                 default:
534                         break; /*fallthrough to bad instruction handling */
535                 }
536                 break; /*fallthrough to bad instruction handling */
537             case 0x20: /* mov crX, eax */
538                 switch (instr[2])
539                 {
540                 case 0xe0: /* mov cr4, eax */
541                     /* CR4 register . See linux/arch/i386/mm/init.c, X86_CR4_ defs
542                      * bit 0: VME       Virtual Mode Exception ?
543                      * bit 1: PVI       Protected mode Virtual Interrupt
544                      * bit 2: TSD       Timestamp disable
545                      * bit 3: DE        Debugging extensions
546                      * bit 4: PSE       Page size extensions
547                      * bit 5: PAE   Physical address extension
548                      * bit 6: MCE       Machine check enable
549                      * bit 7: PGE   Enable global pages
550                      * bit 8: PCE       Enable performance counters at IPL3
551                      */
552                     ERR("mov cr4,eax at 0x%08x\n",context->Eip);
553                     context->Eax = 0;
554                     context->Eip += prefixlen+3;
555                     return ExceptionContinueExecution;
556                 case 0xc0: /* mov cr0, eax */
557                     ERR("mov cr0,eax at 0x%08x\n",context->Eip);
558                     context->Eax = 0x10; /* FIXME: set more bits ? */
559                     context->Eip += prefixlen+3;
560                     return ExceptionContinueExecution;
561                 default: /* fallthrough to illegal instruction */
562                     break;
563                 }
564                 /* fallthrough to illegal instruction */
565                 break;
566             case 0x21: /* mov drX, eax */
567                 switch (instr[2])
568                 {
569                 case 0xc8: /* mov dr1, eax */
570                     TRACE("mov dr1,eax at 0x%08x\n",context->Eip);
571                     context->Eax = context->Dr1;
572                     context->Eip += prefixlen+3;
573                     return ExceptionContinueExecution;
574                 case 0xf8: /* mov dr7, eax */
575                     TRACE("mov dr7,eax at 0x%08x\n",context->Eip);
576                     context->Eax = 0x400;
577                     context->Eip += prefixlen+3;
578                     return ExceptionContinueExecution;
579                 }
580                 ERR("Unsupported DR register, eip+2 is %02x\n", instr[2]);
581                 /* fallthrough to illegal instruction */
582                 break;
583             case 0x23: /* mov eax drX */
584                 switch (instr[2])
585                 {
586                 case 0xc8: /* mov eax, dr1 */
587                     context->Dr1 = context->Eax;
588                     context->Eip += prefixlen+3;
589                     return ExceptionContinueExecution;
590                 }
591                 ERR("Unsupported DR register, eip+2 is %02x\n", instr[2]);
592                 /* fallthrough to illegal instruction */
593                 break;
594             case 0xa1: /* pop fs */
595                 {
596                     WORD seg = *(WORD *)get_stack( context );
597                     if (INSTR_ReplaceSelector( context, &seg ))
598                     {
599                         context->SegFs = seg;
600                         add_stack(context, long_op ? 4 : 2);
601                         context->Eip += prefixlen + 2;
602                         return ExceptionContinueExecution;
603                     }
604                 }
605                 break;
606             case 0xa9: /* pop gs */
607                 {
608                     WORD seg = *(WORD *)get_stack( context );
609                     if (INSTR_ReplaceSelector( context, &seg ))
610                     {
611                         context->SegGs = seg;
612                         add_stack(context, long_op ? 4 : 2);
613                         context->Eip += prefixlen + 2;
614                         return ExceptionContinueExecution;
615                     }
616                 }
617                 break;
618             case 0xb2: /* lss addr,reg */
619             case 0xb4: /* lfs addr,reg */
620             case 0xb5: /* lgs addr,reg */
621                 if (INSTR_EmulateLDS( context, instr, long_op,
622                                       long_addr, segprefix, &len ))
623                 {
624                     context->Eip += prefixlen + len;
625                     return ExceptionContinueExecution;
626                 }
627                 break;
628             }
629             break;  /* Unable to emulate it */
630
631         case 0x6c: /* insb     */
632         case 0x6d: /* insw/d   */
633         case 0x6e: /* outsb    */
634         case 0x6f: /* outsw/d  */
635             {
636               int typ = *instr;  /* Just in case it's overwritten.  */
637               int outp = (typ >= 0x6e);
638               unsigned long count = repX ?
639                           (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
640               int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
641               int step = (context->EFlags & 0x400) ? -opsize : +opsize;
642               int seg = outp ? context->SegDs : context->SegEs;  /* FIXME: is this right? */
643
644               if (outp)
645                 /* FIXME: Check segment is readable.  */
646                 (void)0;
647               else
648                 /* FIXME: Check segment is writable.  */
649                 (void)0;
650
651               if (repX)
652               {
653                 if (long_addr) context->Ecx = 0;
654                 else SET_LOWORD(context->Ecx,0);
655               }
656
657               while (count-- > 0)
658                 {
659                   void *data;
660                   WORD dx = LOWORD(context->Edx);
661                   if (outp)
662                   {
663                       data = make_ptr( context, seg, context->Esi, long_addr );
664                       if (long_addr) context->Esi += step;
665                       else ADD_LOWORD(context->Esi,step);
666                   }
667                   else
668                   {
669                       data = make_ptr( context, seg, context->Edi, long_addr );
670                       if (long_addr) context->Edi += step;
671                       else ADD_LOWORD(context->Edi,step);
672                   }
673
674                   switch (typ)
675                   {
676                     case 0x6c:
677                       *(BYTE *)data = INSTR_inport( dx, 1, context );
678                       break;
679                     case 0x6d:
680                       if (long_op)
681                           *(DWORD *)data = INSTR_inport( dx, 4, context );
682                       else
683                           *(WORD *)data = INSTR_inport( dx, 2, context );
684                       break;
685                     case 0x6e:
686                         INSTR_outport( dx, 1, *(BYTE *)data, context );
687                         break;
688                     case 0x6f:
689                         if (long_op)
690                             INSTR_outport( dx, 4, *(DWORD *)data, context );
691                         else
692                             INSTR_outport( dx, 2, *(WORD *)data, context );
693                         break;
694                     }
695                 }
696               context->Eip += prefixlen + 1;
697             }
698             return ExceptionContinueExecution;
699
700         case 0x8b: /* mov Ev, Gv */
701             {
702                 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1, long_addr,
703                                                   segprefix, &len);
704                 struct idtr idtr = get_idtr();
705                 unsigned int offset = addr - idtr.base;
706
707                 if (offset <= idtr.limit + 1 - (long_op ? 4 : 2))
708                 {
709                     idt[1].LimitLow = 0x100; /* FIXME */
710                     idt[2].LimitLow = 0x11E; /* FIXME */
711                     idt[3].LimitLow = 0x500; /* FIXME */
712                     store_reg( context, instr[1], (BYTE *)&idt + offset, long_op );
713                     context->Eip += prefixlen + len + 1;
714                     return ExceptionContinueExecution;
715                 }
716             }
717             break;  /* Unable to emulate it */
718
719         case 0x8e: /* mov XX,segment_reg */
720             {
721                 WORD seg;
722                 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
723                                                   long_addr, segprefix, &len );
724                 if (!addr)
725                     break;  /* Unable to emulate it */
726                 seg = *(WORD *)addr;
727                 if (!INSTR_ReplaceSelector( context, &seg ))
728                     break;  /* Unable to emulate it */
729
730                 switch((instr[1] >> 3) & 7)
731                 {
732                 case 0:
733                     context->SegEs = seg;
734                     context->Eip += prefixlen + len + 1;
735                     return ExceptionContinueExecution;
736                 case 1:  /* cs */
737                     break;
738                 case 2:
739                     context->SegSs = seg;
740                     context->Eip += prefixlen + len + 1;
741                     return ExceptionContinueExecution;
742                 case 3:
743                     context->SegDs = seg;
744                     context->Eip += prefixlen + len + 1;
745                     return ExceptionContinueExecution;
746                 case 4:
747                     context->SegFs = seg;
748                     context->Eip += prefixlen + len + 1;
749                     return ExceptionContinueExecution;
750                 case 5:
751                     context->SegGs = seg;
752                     context->Eip += prefixlen + len + 1;
753                     return ExceptionContinueExecution;
754                 case 6:  /* unused */
755                 case 7:  /* unused */
756                     break;
757                 }
758             }
759             break;  /* Unable to emulate it */
760
761         case 0xc4: /* les addr,reg */
762         case 0xc5: /* lds addr,reg */
763             if (INSTR_EmulateLDS( context, instr, long_op,
764                                   long_addr, segprefix, &len ))
765             {
766                 context->Eip += prefixlen + len;
767                 return ExceptionContinueExecution;
768             }
769             break;  /* Unable to emulate it */
770
771         case 0xcd: /* int <XX> */
772             if (!winedos.EmulateInterruptPM) load_winedos();
773             if (winedos.EmulateInterruptPM)
774             {
775                 context->Eip += prefixlen + 2;
776                 if (winedos.EmulateInterruptPM( context, instr[1] )) return ExceptionContinueExecution;
777                 context->Eip -= prefixlen + 2;  /* restore eip */
778             }
779             break;  /* Unable to emulate it */
780
781         case 0xcf: /* iret */
782             if (wine_ldt_is_system(context->SegCs)) break;  /* don't emulate it in 32-bit code */
783             if (long_op)
784             {
785                 DWORD *stack = get_stack( context );
786                 context->Eip = *stack++;
787                 context->SegCs  = *stack++;
788                 context->EFlags = *stack;
789                 add_stack(context, 3*sizeof(DWORD));  /* Pop the return address and flags */
790             }
791             else
792             {
793                 WORD *stack = get_stack( context );
794                 context->Eip = *stack++;
795                 context->SegCs  = *stack++;
796                 SET_LOWORD(context->EFlags,*stack);
797                 add_stack(context, 3*sizeof(WORD));  /* Pop the return address and flags */
798             }
799             return ExceptionContinueExecution;
800
801         case 0xe4: /* inb al,XX */
802             SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
803             context->Eip += prefixlen + 2;
804             return ExceptionContinueExecution;
805
806         case 0xe5: /* in (e)ax,XX */
807             if (long_op)
808                 context->Eax = INSTR_inport( instr[1], 4, context );
809             else
810                 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
811             context->Eip += prefixlen + 2;
812             return ExceptionContinueExecution;
813
814         case 0xe6: /* outb XX,al */
815             INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
816             context->Eip += prefixlen + 2;
817             return ExceptionContinueExecution;
818
819         case 0xe7: /* out XX,(e)ax */
820             if (long_op)
821                 INSTR_outport( instr[1], 4, context->Eax, context );
822             else
823                 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
824             context->Eip += prefixlen + 2;
825             return ExceptionContinueExecution;
826
827         case 0xec: /* inb al,dx */
828             SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
829             context->Eip += prefixlen + 1;
830             return ExceptionContinueExecution;
831
832         case 0xed: /* in (e)ax,dx */
833             if (long_op)
834                 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
835             else
836                 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
837             context->Eip += prefixlen + 1;
838             return ExceptionContinueExecution;
839
840         case 0xee: /* outb dx,al */
841             INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
842             context->Eip += prefixlen + 1;
843             return ExceptionContinueExecution;
844
845         case 0xef: /* out dx,(e)ax */
846             if (long_op)
847                 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
848             else
849                 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
850             context->Eip += prefixlen + 1;
851             return ExceptionContinueExecution;
852
853         case 0xfa: /* cli */
854             NtCurrentTeb()->dpmi_vif = 0;
855             context->Eip += prefixlen + 1;
856             return ExceptionContinueExecution;
857
858         case 0xfb: /* sti */
859             NtCurrentTeb()->dpmi_vif = 1;
860             context->Eip += prefixlen + 1;
861             if (NtCurrentTeb()->vm86_pending)
862             {
863                 NtCurrentTeb()->vm86_pending = 0;
864                 rec->ExceptionCode = EXCEPTION_VM86_STI;
865                 break; /* Handle the pending event. */
866             }
867             return ExceptionContinueExecution;
868     }
869     return ExceptionContinueSearch;  /* Unable to emulate it */
870 }
871
872
873 /***********************************************************************
874  *           INSTR_vectored_handler
875  *
876  * Vectored exception handler used to emulate protected instructions
877  * from 32-bit code.
878  */
879 LONG CALLBACK INSTR_vectored_handler( EXCEPTION_POINTERS *ptrs )
880 {
881     EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
882     CONTEXT86 *context = ptrs->ContextRecord;
883
884     if (wine_ldt_is_system(context->SegCs) &&
885         (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
886          record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION))
887     {
888         if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
889             return EXCEPTION_CONTINUE_EXECUTION;
890     }
891     return EXCEPTION_CONTINUE_SEARCH;
892 }
893
894
895 /***********************************************************************
896  *           INSTR_CallBuiltinHandler
897  */
898 void INSTR_CallBuiltinHandler( CONTEXT86 *context, BYTE intnum )
899 {
900     if (!winedos.CallBuiltinHandler) load_winedos();
901     if (winedos.CallBuiltinHandler) winedos.CallBuiltinHandler( context, intnum );
902 }
903
904
905 /***********************************************************************
906  *           DOS3Call         (KERNEL.102)
907  */
908 void WINAPI DOS3Call( CONTEXT86 *context )
909 {
910     INSTR_CallBuiltinHandler( context, 0x21 );
911 }
912
913
914 /***********************************************************************
915  *           NetBIOSCall      (KERNEL.103)
916  */
917 void WINAPI NetBIOSCall16( CONTEXT86 *context )
918 {
919     INSTR_CallBuiltinHandler( context, 0x5c );
920 }
921
922
923 /***********************************************************************
924  *              GetSetKernelDOSProc (KERNEL.311)
925  */
926 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
927 {
928     FIXME("(DosProc=%p): stub\n", DosProc);
929     return NULL;
930 }