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