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