Added CSIDL_MYVIDEO|MYPICTURES|MYMUSIC to _SHRegisterUserShellFolders.
[wine] / dlls / kernel / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 = *(DWORD *)addr;
129         else SET_LOWORD(context->Eax,*(WORD *)addr);
130         break;
131     case 1:
132         if (long_op) context->Ecx = *(DWORD *)addr;
133         else SET_LOWORD(context->Ecx,*(WORD *)addr);
134         break;
135     case 2:
136         if (long_op) context->Edx = *(DWORD *)addr;
137         else SET_LOWORD(context->Edx,*(WORD *)addr);
138         break;
139     case 3:
140         if (long_op) context->Ebx = *(DWORD *)addr;
141         else SET_LOWORD(context->Ebx,*(WORD *)addr);
142         break;
143     case 4:
144         if (long_op) context->Esp = *(DWORD *)addr;
145         else SET_LOWORD(context->Esp,*(WORD *)addr);
146         break;
147     case 5:
148         if (long_op) context->Ebp = *(DWORD *)addr;
149         else SET_LOWORD(context->Ebp,*(WORD *)addr);
150         break;
151     case 6:
152         if (long_op) context->Esi = *(DWORD *)addr;
153         else SET_LOWORD(context->Esi,*(WORD *)addr);
154         break;
155     case 7:
156         if (long_op) context->Edi = *(DWORD *)addr;
157         else SET_LOWORD(context->Edi,*(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                     context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
572                     NtGetContextThread( GetCurrentThread(), context );
573                     TRACE("mov dr1,eax at 0x%08lx\n",context->Eip);
574                     context->Eax = context->Dr1;
575                     context->Eip += prefixlen+3;
576                     return ExceptionContinueExecution;
577                 case 0xf8: /* mov dr7, eax */
578                     TRACE("mov dr7,eax at 0x%08lx\n",context->Eip);
579                     context->Eax = 0x400;
580                     context->Eip += prefixlen+3;
581                     return ExceptionContinueExecution;
582                 }
583                 ERR("Unsupported DR register, eip+2 is %02x\n", instr[2]);
584                 /* fallthrough to illegal instruction */
585                 break;
586             case 0x23: /* mov eax drX */
587                 switch (instr[2])
588                 {
589                 case 0xc8: /* mov eax, dr1 */
590                     context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
591                     NtGetContextThread( GetCurrentThread(), context );
592                     context->Dr1 = context->Eax;
593                     context->Eip += prefixlen+3;
594                     context->ContextFlags = CONTEXT_DEBUG_REGISTERS;
595                     NtSetContextThread( GetCurrentThread(), context );
596                     return ExceptionContinueExecution;
597                 }
598                 ERR("Unsupported DR register, eip+2 is %02x\n", instr[2]);
599                 /* fallthrough to illegal instruction */
600                 break;
601             case 0xa1: /* pop fs */
602                 {
603                     WORD seg = *(WORD *)get_stack( context );
604                     if (INSTR_ReplaceSelector( context, &seg ))
605                     {
606                         context->SegFs = seg;
607                         add_stack(context, long_op ? 4 : 2);
608                         context->Eip += prefixlen + 2;
609                         return ExceptionContinueExecution;
610                     }
611                 }
612                 break;
613             case 0xa9: /* pop gs */
614                 {
615                     WORD seg = *(WORD *)get_stack( context );
616                     if (INSTR_ReplaceSelector( context, &seg ))
617                     {
618                         context->SegGs = seg;
619                         add_stack(context, long_op ? 4 : 2);
620                         context->Eip += prefixlen + 2;
621                         return ExceptionContinueExecution;
622                     }
623                 }
624                 break;
625             case 0xb2: /* lss addr,reg */
626             case 0xb4: /* lfs addr,reg */
627             case 0xb5: /* lgs addr,reg */
628                 if (INSTR_EmulateLDS( context, instr, long_op,
629                                       long_addr, segprefix, &len ))
630                 {
631                     context->Eip += prefixlen + len;
632                     return ExceptionContinueExecution;
633                 }
634                 break;
635             }
636             break;  /* Unable to emulate it */
637
638         case 0x6c: /* insb     */
639         case 0x6d: /* insw/d   */
640         case 0x6e: /* outsb    */
641         case 0x6f: /* outsw/d  */
642             {
643               int typ = *instr;  /* Just in case it's overwritten.  */
644               int outp = (typ >= 0x6e);
645               unsigned long count = repX ?
646                           (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
647               int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
648               int step = (context->EFlags & 0x400) ? -opsize : +opsize;
649               int seg = outp ? context->SegDs : context->SegEs;  /* FIXME: is this right? */
650
651               if (outp)
652                 /* FIXME: Check segment readable.  */
653                 (void)0;
654               else
655                 /* FIXME: Check segment writeable.  */
656                 (void)0;
657
658               if (repX)
659               {
660                 if (long_addr) context->Ecx = 0;
661                 else SET_LOWORD(context->Ecx,0);
662               }
663
664               while (count-- > 0)
665                 {
666                   void *data;
667                   WORD dx = LOWORD(context->Edx);
668                   if (outp)
669                   {
670                       data = make_ptr( context, seg, context->Esi, long_addr );
671                       if (long_addr) context->Esi += step;
672                       else ADD_LOWORD(context->Esi,step);
673                   }
674                   else
675                   {
676                       data = make_ptr( context, seg, context->Edi, long_addr );
677                       if (long_addr) context->Edi += step;
678                       else ADD_LOWORD(context->Edi,step);
679                   }
680
681                   switch (typ)
682                   {
683                     case 0x6c:
684                       *(BYTE *)data = INSTR_inport( dx, 1, context );
685                       break;
686                     case 0x6d:
687                       if (long_op)
688                           *(DWORD *)data = INSTR_inport( dx, 4, context );
689                       else
690                           *(WORD *)data = INSTR_inport( dx, 2, context );
691                       break;
692                     case 0x6e:
693                         INSTR_outport( dx, 1, *(BYTE *)data, context );
694                         break;
695                     case 0x6f:
696                         if (long_op)
697                             INSTR_outport( dx, 4, *(DWORD *)data, context );
698                         else
699                             INSTR_outport( dx, 2, *(WORD *)data, context );
700                         break;
701                     }
702                 }
703               context->Eip += prefixlen + 1;
704             }
705             return ExceptionContinueExecution;
706
707         case 0x8b: /* mov Ev, Gv */
708             {
709                 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1, long_addr,
710                                                   segprefix, &len);
711                 struct idtr idtr = get_idtr();
712                 unsigned int offset = addr - idtr.base;
713
714                 if (offset <= idtr.limit + 1 - (long_op ? 4 : 2))
715                 {
716                     idt[1].LimitLow = 0x100; /* FIXME */
717                     idt[2].LimitLow = 0x11E; /* FIXME */
718                     idt[3].LimitLow = 0x500; /* FIXME */
719                     store_reg( context, instr[1], (BYTE *)&idt + offset, long_op );
720                     context->Eip += prefixlen + len + 1;
721                     return ExceptionContinueExecution;
722                 }
723             }
724             break;  /* Unable to emulate it */
725
726         case 0x8e: /* mov XX,segment_reg */
727             {
728                 WORD seg;
729                 BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
730                                                   long_addr, segprefix, &len );
731                 if (!addr)
732                     break;  /* Unable to emulate it */
733                 seg = *(WORD *)addr;
734                 if (!INSTR_ReplaceSelector( context, &seg ))
735                     break;  /* Unable to emulate it */
736
737                 switch((instr[1] >> 3) & 7)
738                 {
739                 case 0:
740                     context->SegEs = seg;
741                     context->Eip += prefixlen + len + 1;
742                     return ExceptionContinueExecution;
743                 case 1:  /* cs */
744                     break;
745                 case 2:
746                     context->SegSs = seg;
747                     context->Eip += prefixlen + len + 1;
748                     return ExceptionContinueExecution;
749                 case 3:
750                     context->SegDs = seg;
751                     context->Eip += prefixlen + len + 1;
752                     return ExceptionContinueExecution;
753                 case 4:
754                     context->SegFs = seg;
755                     context->Eip += prefixlen + len + 1;
756                     return ExceptionContinueExecution;
757                 case 5:
758                     context->SegGs = seg;
759                     context->Eip += prefixlen + len + 1;
760                     return ExceptionContinueExecution;
761                 case 6:  /* unused */
762                 case 7:  /* unused */
763                     break;
764                 }
765             }
766             break;  /* Unable to emulate it */
767
768         case 0xc4: /* les addr,reg */
769         case 0xc5: /* lds addr,reg */
770             if (INSTR_EmulateLDS( context, instr, long_op,
771                                   long_addr, segprefix, &len ))
772             {
773                 context->Eip += prefixlen + len;
774                 return ExceptionContinueExecution;
775             }
776             break;  /* Unable to emulate it */
777
778         case 0xcd: /* int <XX> */
779             if (wine_ldt_is_system(context->SegCs)) break;  /* don't emulate it in 32-bit code */
780             if (!winedos.EmulateInterruptPM) load_winedos();
781             if (winedos.EmulateInterruptPM)
782             {
783                 context->Eip += prefixlen + 2;
784                 winedos.EmulateInterruptPM( context, instr[1] );
785                 return ExceptionContinueExecution;
786             }
787             break;  /* Unable to emulate it */
788
789         case 0xcf: /* iret */
790             if (wine_ldt_is_system(context->SegCs)) break;  /* don't emulate it in 32-bit code */
791             if (long_op)
792             {
793                 DWORD *stack = get_stack( context );
794                 context->Eip = *stack++;
795                 context->SegCs  = *stack++;
796                 context->EFlags = *stack;
797                 add_stack(context, 3*sizeof(DWORD));  /* Pop the return address and flags */
798             }
799             else
800             {
801                 WORD *stack = get_stack( context );
802                 context->Eip = *stack++;
803                 context->SegCs  = *stack++;
804                 SET_LOWORD(context->EFlags,*stack);
805                 add_stack(context, 3*sizeof(WORD));  /* Pop the return address and flags */
806             }
807             return ExceptionContinueExecution;
808
809         case 0xe4: /* inb al,XX */
810             SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
811             context->Eip += prefixlen + 2;
812             return ExceptionContinueExecution;
813
814         case 0xe5: /* in (e)ax,XX */
815             if (long_op)
816                 context->Eax = INSTR_inport( instr[1], 4, context );
817             else
818                 SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
819             context->Eip += prefixlen + 2;
820             return ExceptionContinueExecution;
821
822         case 0xe6: /* outb XX,al */
823             INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
824             context->Eip += prefixlen + 2;
825             return ExceptionContinueExecution;
826
827         case 0xe7: /* out XX,(e)ax */
828             if (long_op)
829                 INSTR_outport( instr[1], 4, context->Eax, context );
830             else
831                 INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
832             context->Eip += prefixlen + 2;
833             return ExceptionContinueExecution;
834
835         case 0xec: /* inb al,dx */
836             SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
837             context->Eip += prefixlen + 1;
838             return ExceptionContinueExecution;
839
840         case 0xed: /* in (e)ax,dx */
841             if (long_op)
842                 context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
843             else
844                 SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
845             context->Eip += prefixlen + 1;
846             return ExceptionContinueExecution;
847
848         case 0xee: /* outb dx,al */
849             INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
850             context->Eip += prefixlen + 1;
851             return ExceptionContinueExecution;
852
853         case 0xef: /* out dx,(e)ax */
854             if (long_op)
855                 INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
856             else
857                 INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
858             context->Eip += prefixlen + 1;
859             return ExceptionContinueExecution;
860
861         case 0xfa: /* cli */
862             NtCurrentTeb()->dpmi_vif = 0;
863             context->Eip += prefixlen + 1;
864             return ExceptionContinueExecution;
865
866         case 0xfb: /* sti */
867             NtCurrentTeb()->dpmi_vif = 1;
868             context->Eip += prefixlen + 1;
869             if (NtCurrentTeb()->vm86_pending)
870             {
871                 NtCurrentTeb()->vm86_pending = 0;
872                 rec->ExceptionCode = EXCEPTION_VM86_STI;
873                 break; /* Handle the pending event. */
874             }
875             return ExceptionContinueExecution;
876     }
877     return ExceptionContinueSearch;  /* Unable to emulate it */
878 }
879
880
881 /***********************************************************************
882  *           INSTR_vectored_handler
883  *
884  * Vectored exception handler used to emulate protected instructions
885  * from 32-bit code.
886  */
887 LONG CALLBACK INSTR_vectored_handler( EXCEPTION_POINTERS *ptrs )
888 {
889     EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
890     CONTEXT86 *context = ptrs->ContextRecord;
891
892     if (wine_ldt_is_system(context->SegCs) &&
893         (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
894          record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION))
895     {
896         if (INSTR_EmulateInstruction( record, context ) == ExceptionContinueExecution)
897             return EXCEPTION_CONTINUE_EXECUTION;
898     }
899     return EXCEPTION_CONTINUE_SEARCH;
900 }
901
902
903 /***********************************************************************
904  *           INSTR_CallBuiltinHandler
905  */
906 void INSTR_CallBuiltinHandler( CONTEXT86 *context, BYTE intnum )
907 {
908     if (!winedos.CallBuiltinHandler) load_winedos();
909     if (winedos.CallBuiltinHandler) winedos.CallBuiltinHandler( context, intnum );
910 }
911
912
913 /***********************************************************************
914  *           DOS3Call         (KERNEL.102)
915  */
916 void WINAPI DOS3Call( CONTEXT86 *context )
917 {
918     INSTR_CallBuiltinHandler( context, 0x21 );
919 }
920
921
922 /***********************************************************************
923  *           NetBIOSCall      (KERNEL.103)
924  */
925 void WINAPI NetBIOSCall16( CONTEXT86 *context )
926 {
927     INSTR_CallBuiltinHandler( context, 0x5c );
928 }
929
930
931 /***********************************************************************
932  *              GetSetKernelDOSProc (KERNEL.311)
933  */
934 FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
935 {
936     FIXME("(DosProc=%p): stub\n", DosProc);
937     return NULL;
938 }