instr.c 29.9 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
Andreas Mohr's avatar
Andreas Mohr committed
2
 * Emulation of privileged instructions
Alexandre Julliard's avatar
Alexandre Julliard committed
3 4
 *
 * Copyright 1995 Alexandre Julliard
5 6
 * Copyright 2005 Ivan Leo Puoti
 * Copyright 2005 Laurent Pinchart
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
21 22
 */

Alexandre Julliard's avatar
Alexandre Julliard committed
23 24 25
#include "config.h"
#include "wine/port.h"

26 27
#include <stdarg.h>

28
#include "windef.h"
29
#include "winbase.h"
30
#include "winternl.h"
31
#include "wine/winuser16.h"
32
#include "excpt.h"
33
#include "wine/debug.h"
34
#include "kernel16_private.h"
35
#include "dosexe.h"
36
#include "wine/exception.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
37

38 39
WINE_DEFAULT_DEBUG_CHANNEL(int);
WINE_DECLARE_DEBUG_CHANNEL(io);
40

41 42 43 44
/* macros to set parts of a DWORD */
#define SET_LOWORD(dw,val)  ((dw) = ((dw) & 0xffff0000) | LOWORD(val))
#define SET_LOBYTE(dw,val)  ((dw) = ((dw) & 0xffffff00) | LOBYTE(val))
#define ADD_LOWORD(dw,val)  ((dw) = ((dw) & 0xffff0000) | LOWORD((DWORD)(dw)+(val)))
45
#define ISV86(context)      ((context)->EFlags & 0x00020000)
46

47
static inline void add_stack( CONTEXT *context, int offset )
Alexandre Julliard's avatar
Alexandre Julliard committed
48 49 50 51 52 53
{
    if (ISV86(context) || !IS_SELECTOR_32BIT(context->SegSs))
        ADD_LOWORD( context->Esp, offset );
    else
        context->Esp += offset;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
54

55
static inline void *make_ptr( CONTEXT *context, DWORD seg, DWORD off, int long_addr )
Alexandre Julliard's avatar
Alexandre Julliard committed
56
{
57
    if (ISV86(context)) return (void *)((seg << 4) + LOWORD(off));
58
    if (wine_ldt_is_system(seg)) return (void *)off;
Alexandre Julliard's avatar
Alexandre Julliard committed
59
    if (!long_addr) off = LOWORD(off);
60
    return (char *) MapSL( MAKESEGPTR( seg, 0 ) ) + off;
Alexandre Julliard's avatar
Alexandre Julliard committed
61
}
62

63
static inline void *get_stack( CONTEXT *context )
Alexandre Julliard's avatar
Alexandre Julliard committed
64
{
65
    if (ISV86(context)) return (void *)((context->SegSs << 4) + LOWORD(context->Esp));
66
    return wine_ldt_get_ptr( context->SegSs, context->Esp );
Alexandre Julliard's avatar
Alexandre Julliard committed
67
}
Alexandre Julliard's avatar
Alexandre Julliard committed
68

Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
69 70 71 72 73 74 75 76 77 78
#include "pshpack1.h"
struct idtr
{
    WORD  limit;
    BYTE *base;
};
#include "poppack.h"

static LDT_ENTRY idt[256];

79
static inline struct idtr get_idtr(void)
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
80 81 82 83 84 85 86 87 88 89 90 91
{
    struct idtr ret;
#if defined(__i386__) && defined(__GNUC__)
    __asm__( "sidtl %0" : "=m" (ret) );
#else
    ret.base = (BYTE *)idt;
    ret.limit = sizeof(idt) - 1;
#endif
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
92 93 94 95
/***********************************************************************
 *           INSTR_ReplaceSelector
 *
 * Try to replace an invalid selector by a valid one.
Alexandre Julliard's avatar
Alexandre Julliard committed
96 97
 * The only selector where it is allowed to do "mov ax,40;mov es,ax"
 * is the so called 'bimodal' selector 0x40, which points to the BIOS
98
 * data segment. Used by (at least) Borland products (and programs compiled
Alexandre Julliard's avatar
Alexandre Julliard committed
99 100 101
 * using Borland products).
 *
 * See Undocumented Windows, Chapter 5, __0040.
Alexandre Julliard's avatar
Alexandre Julliard committed
102
 */
103
static BOOL INSTR_ReplaceSelector( CONTEXT *context, WORD *sel )
Alexandre Julliard's avatar
Alexandre Julliard committed
104
{
Alexandre Julliard's avatar
Alexandre Julliard committed
105
    if (*sel == 0x40)
Alexandre Julliard's avatar
Alexandre Julliard committed
106
    {
107
        DOSVM_start_bios_timer();
108
        *sel = DOSMEM_BiosDataSeg;
Alexandre Julliard's avatar
Alexandre Julliard committed
109
        return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
110
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
111
    return FALSE;  /* Can't replace selector, crashdump */
Alexandre Julliard's avatar
Alexandre Julliard committed
112 113 114
}


Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
115
/* store an operand into a register */
116
static void store_reg( CONTEXT *context, BYTE regmodrm, const BYTE *addr, int long_op )
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
117 118 119 120
{
    switch((regmodrm >> 3) & 7)
    {
    case 0:
121 122
        if (long_op) context->Eax = *(const DWORD *)addr;
        else SET_LOWORD(context->Eax, *(const WORD *)addr);
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
123 124
        break;
    case 1:
125 126
        if (long_op) context->Ecx = *(const DWORD *)addr;
        else SET_LOWORD(context->Ecx, *(const WORD *)addr);
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
127 128
        break;
    case 2:
129 130
        if (long_op) context->Edx = *(const DWORD *)addr;
        else SET_LOWORD(context->Edx, *(const WORD *)addr);
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
131 132
        break;
    case 3:
133 134
        if (long_op) context->Ebx = *(const DWORD *)addr;
        else SET_LOWORD(context->Ebx, *(const WORD *)addr);
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
135 136
        break;
    case 4:
137 138
        if (long_op) context->Esp = *(const DWORD *)addr;
        else SET_LOWORD(context->Esp, *(const WORD *)addr);
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
139 140
        break;
    case 5:
141 142
        if (long_op) context->Ebp = *(const DWORD *)addr;
        else SET_LOWORD(context->Ebp, *(const WORD *)addr);
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
143 144
        break;
    case 6:
145 146
        if (long_op) context->Esi = *(const DWORD *)addr;
        else SET_LOWORD(context->Esi, *(const WORD *)addr);
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
147 148
        break;
    case 7:
149 150
        if (long_op) context->Edi = *(const DWORD *)addr;
        else SET_LOWORD(context->Edi, *(const WORD *)addr);
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
151 152 153 154
        break;
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
155 156 157 158 159
/***********************************************************************
 *           INSTR_GetOperandAddr
 *
 * Return the address of an instruction operand (from the mod/rm byte).
 */
160
static BYTE *INSTR_GetOperandAddr( CONTEXT *context, BYTE *instr,
Alexandre Julliard's avatar
Alexandre Julliard committed
161
                                   int long_addr, int segprefix, int *len )
Alexandre Julliard's avatar
Alexandre Julliard committed
162
{
163
    int mod, rm, base = 0, index = 0, ss = 0, seg = 0, off;
164
    LDT_ENTRY entry;
Alexandre Julliard's avatar
Alexandre Julliard committed
165 166 167 168 169 170 171 172 173 174 175 176 177

#define GET_VAL(val,type) \
    { *val = *(type *)instr; instr += sizeof(type); *len += sizeof(type); }

    *len = 0;
    GET_VAL( &mod, BYTE );
    rm = mod & 7;
    mod >>= 6;

    if (mod == 3)
    {
        switch(rm)
        {
178 179 180 181 182 183 184 185
        case 0: return (BYTE *)&context->Eax;
        case 1: return (BYTE *)&context->Ecx;
        case 2: return (BYTE *)&context->Edx;
        case 3: return (BYTE *)&context->Ebx;
        case 4: return (BYTE *)&context->Esp;
        case 5: return (BYTE *)&context->Ebp;
        case 6: return (BYTE *)&context->Esi;
        case 7: return (BYTE *)&context->Edi;
Alexandre Julliard's avatar
Alexandre Julliard committed
186 187 188 189 190 191 192 193 194 195 196
        }
    }

    if (long_addr)
    {
        if (rm == 4)
        {
            BYTE sib;
            GET_VAL( &sib, BYTE );
            rm = sib & 7;
            ss = sib >> 6;
197
            switch((sib >> 3) & 7)
Alexandre Julliard's avatar
Alexandre Julliard committed
198
            {
199 200 201 202
            case 0: index = context->Eax; break;
            case 1: index = context->Ecx; break;
            case 2: index = context->Edx; break;
            case 3: index = context->Ebx; break;
Alexandre Julliard's avatar
Alexandre Julliard committed
203
            case 4: index = 0; break;
204 205 206
            case 5: index = context->Ebp; break;
            case 6: index = context->Esi; break;
            case 7: index = context->Edi; break;
Alexandre Julliard's avatar
Alexandre Julliard committed
207 208 209 210 211
            }
        }

        switch(rm)
        {
212 213 214 215 216 217 218 219
        case 0: base = context->Eax; seg = context->SegDs; break;
        case 1: base = context->Ecx; seg = context->SegDs; break;
        case 2: base = context->Edx; seg = context->SegDs; break;
        case 3: base = context->Ebx; seg = context->SegDs; break;
        case 4: base = context->Esp; seg = context->SegSs; break;
        case 5: base = context->Ebp; seg = context->SegSs; break;
        case 6: base = context->Esi; seg = context->SegDs; break;
        case 7: base = context->Edi; seg = context->SegDs; break;
Alexandre Julliard's avatar
Alexandre Julliard committed
220 221 222 223 224 225 226
        }
        switch (mod)
        {
        case 0:
            if (rm == 5)  /* special case: ds:(disp32) */
            {
                GET_VAL( &base, DWORD );
227
                seg = context->SegDs;
Alexandre Julliard's avatar
Alexandre Julliard committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
            }
            break;

        case 1:  /* 8-bit disp */
            GET_VAL( &off, BYTE );
            base += (signed char)off;
            break;

        case 2:  /* 32-bit disp */
            GET_VAL( &off, DWORD );
            base += (signed long)off;
            break;
        }
    }
    else  /* short address */
    {
        switch(rm)
        {
        case 0:  /* ds:(bx,si) */
247 248
            base = LOWORD(context->Ebx) + LOWORD(context->Esi);
            seg  = context->SegDs;
Alexandre Julliard's avatar
Alexandre Julliard committed
249 250
            break;
        case 1:  /* ds:(bx,di) */
251 252
            base = LOWORD(context->Ebx) + LOWORD(context->Edi);
            seg  = context->SegDs;
Alexandre Julliard's avatar
Alexandre Julliard committed
253 254
            break;
        case 2:  /* ss:(bp,si) */
255 256
            base = LOWORD(context->Ebp) + LOWORD(context->Esi);
            seg  = context->SegSs;
Alexandre Julliard's avatar
Alexandre Julliard committed
257 258
            break;
        case 3:  /* ss:(bp,di) */
259 260
            base = LOWORD(context->Ebp) + LOWORD(context->Edi);
            seg  = context->SegSs;
Alexandre Julliard's avatar
Alexandre Julliard committed
261 262
            break;
        case 4:  /* ds:(si) */
263 264
            base = LOWORD(context->Esi);
            seg  = context->SegDs;
Alexandre Julliard's avatar
Alexandre Julliard committed
265 266
            break;
        case 5:  /* ds:(di) */
267 268
            base = LOWORD(context->Edi);
            seg  = context->SegDs;
Alexandre Julliard's avatar
Alexandre Julliard committed
269 270
            break;
        case 6:  /* ss:(bp) */
271 272
            base = LOWORD(context->Ebp);
            seg  = context->SegSs;
Alexandre Julliard's avatar
Alexandre Julliard committed
273 274
            break;
        case 7:  /* ds:(bx) */
275 276
            base = LOWORD(context->Ebx);
            seg  = context->SegDs;
Alexandre Julliard's avatar
Alexandre Julliard committed
277 278 279 280 281 282 283 284 285
            break;
        }

        switch(mod)
        {
        case 0:
            if (rm == 6)  /* special case: ds:(disp16) */
            {
                GET_VAL( &base, WORD );
286
                seg  = context->SegDs;
Alexandre Julliard's avatar
Alexandre Julliard committed
287 288 289 290 291 292 293 294 295 296 297 298 299
            }
            break;

        case 1:  /* 8-bit disp */
            GET_VAL( &off, BYTE );
            base += (signed char)off;
            break;

        case 2:  /* 16-bit disp */
            GET_VAL( &off, WORD );
            base += (signed short)off;
            break;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
300
        base &= 0xffff;
Alexandre Julliard's avatar
Alexandre Julliard committed
301 302 303
    }
    if (segprefix != -1) seg = segprefix;

Alexandre Julliard's avatar
Alexandre Julliard committed
304
    /* Make sure the segment and offset are valid */
305
    if (wine_ldt_is_system(seg)) return (BYTE *)(base + (index << ss));
306 307 308 309
    if ((seg & 7) != 7) return NULL;
    wine_ldt_get_entry( seg, &entry );
    if (wine_ldt_is_empty( &entry )) return NULL;
    if (wine_ldt_get_limit(&entry) < (base + (index << ss))) return NULL;
310
    return (BYTE *)wine_ldt_get_base(&entry) + base + (index << ss);
Alexandre Julliard's avatar
Alexandre Julliard committed
311
#undef GET_VAL
Alexandre Julliard's avatar
Alexandre Julliard committed
312 313 314 315 316 317 318 319
}


/***********************************************************************
 *           INSTR_EmulateLDS
 *
 * Emulate the LDS (and LES,LFS,etc.) instruction.
 */
320
static BOOL INSTR_EmulateLDS( CONTEXT *context, BYTE *instr, int long_op,
321
                              int long_addr, int segprefix, int *len )
Alexandre Julliard's avatar
Alexandre Julliard committed
322
{
Alexandre Julliard's avatar
Alexandre Julliard committed
323
    WORD seg;
Alexandre Julliard's avatar
Alexandre Julliard committed
324 325 326
    BYTE *regmodrm = instr + 1 + (*instr == 0x0f);
    BYTE *addr = INSTR_GetOperandAddr( context, regmodrm,
                                       long_addr, segprefix, len );
Alexandre Julliard's avatar
Alexandre Julliard committed
327 328 329
    if (!addr)
        return FALSE;  /* Unable to emulate it */
    seg = *(WORD *)(addr + (long_op ? 4 : 2));
Alexandre Julliard's avatar
Alexandre Julliard committed
330

Alexandre Julliard's avatar
Alexandre Julliard committed
331
    if (!INSTR_ReplaceSelector( context, &seg ))
Alexandre Julliard's avatar
Alexandre Julliard committed
332 333 334 335
        return FALSE;  /* Unable to emulate it */

    /* Now store the offset in the correct register */

Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
336
    store_reg( context, *regmodrm, addr, long_op );
Alexandre Julliard's avatar
Alexandre Julliard committed
337 338 339 340 341

    /* Store the correct segment in the segment register */

    switch(*instr)
    {
342 343
    case 0xc4: context->SegEs = seg; break;  /* les */
    case 0xc5: context->SegDs = seg; break;  /* lds */
Alexandre Julliard's avatar
Alexandre Julliard committed
344 345
    case 0x0f: switch(instr[1])
               {
346 347 348
               case 0xb2: context->SegSs = seg; break;  /* lss */
               case 0xb4: context->SegFs = seg; break;  /* lfs */
               case 0xb5: context->SegGs = seg; break;  /* lgs */
Alexandre Julliard's avatar
Alexandre Julliard committed
349 350 351 352 353 354 355 356 357 358
               }
               break;
    }

    /* Add the opcode size to the total length */

    *len += 1 + (*instr == 0x0f);
    return TRUE;
}

359 360 361
/***********************************************************************
 *           INSTR_inport
 *
362
 * input on an I/O port
363
 */
364
static DWORD INSTR_inport( WORD port, int size, CONTEXT *context )
365
{
366
    DWORD res = DOSVM_inport( port, size );
367

368 369 370 371 372
    if (TRACE_ON(io))
    {
        switch(size)
        {
        case 1:
373
            TRACE_(io)( "0x%x < %02x @ %04x:%04x\n", port, LOBYTE(res),
374
                     (WORD)context->SegCs, LOWORD(context->Eip));
375 376
            break;
        case 2:
377
            TRACE_(io)( "0x%x < %04x @ %04x:%04x\n", port, LOWORD(res),
378
                     (WORD)context->SegCs, LOWORD(context->Eip));
379 380
            break;
        case 4:
381
            TRACE_(io)( "0x%x < %08x @ %04x:%04x\n", port, res,
382
                     (WORD)context->SegCs, LOWORD(context->Eip));
383 384 385 386 387 388 389 390 391 392
            break;
        }
    }
    return res;
}


/***********************************************************************
 *           INSTR_outport
 *
393
 * output on an I/O port
394
 */
395
static void INSTR_outport( WORD port, int size, DWORD val, CONTEXT *context )
396
{
397
    DOSVM_outport( port, size, val );
398

399 400 401 402 403
    if (TRACE_ON(io))
    {
        switch(size)
        {
        case 1:
404
            TRACE_(io)("0x%x > %02x @ %04x:%04x\n", port, LOBYTE(val),
405
                    (WORD)context->SegCs, LOWORD(context->Eip));
406 407
            break;
        case 2:
408
            TRACE_(io)("0x%x > %04x @ %04x:%04x\n", port, LOWORD(val),
409
                    (WORD)context->SegCs, LOWORD(context->Eip));
410 411
            break;
        case 4:
412
            TRACE_(io)("0x%x > %08x @ %04x:%04x\n", port, val,
413
                    (WORD)context->SegCs, LOWORD(context->Eip));
414 415 416 417 418
            break;
        }
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
419

Alexandre Julliard's avatar
Alexandre Julliard committed
420
/***********************************************************************
421
 *           __wine_emulate_instruction
Alexandre Julliard's avatar
Alexandre Julliard committed
422
 *
423
 * Emulate a privileged instruction.
424
 * Returns exception continuation status.
Alexandre Julliard's avatar
Alexandre Julliard committed
425
 */
426
DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT *context )
Alexandre Julliard's avatar
Alexandre Julliard committed
427
{
Alexandre Julliard's avatar
Alexandre Julliard committed
428
    int prefix, segprefix, prefixlen, len, repX, long_op, long_addr;
Alexandre Julliard's avatar
Alexandre Julliard committed
429 430
    BYTE *instr;

Alexandre Julliard's avatar
Alexandre Julliard committed
431 432
    long_op = long_addr = (!ISV86(context) && IS_SELECTOR_32BIT(context->SegCs));
    instr = make_ptr( context, context->SegCs, context->Eip, TRUE );
433
    if (!instr) return ExceptionContinueSearch;
Alexandre Julliard's avatar
Alexandre Julliard committed
434 435 436

    /* First handle any possible prefix */

Alexandre Julliard's avatar
Alexandre Julliard committed
437
    segprefix = -1;  /* no prefix */
Alexandre Julliard's avatar
Alexandre Julliard committed
438
    prefix = 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
439
    repX = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
440
    prefixlen = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
441 442 443 444 445
    while(prefix)
    {
        switch(*instr)
        {
        case 0x2e:
446
            segprefix = context->SegCs;
Alexandre Julliard's avatar
Alexandre Julliard committed
447 448
            break;
        case 0x36:
449
            segprefix = context->SegSs;
Alexandre Julliard's avatar
Alexandre Julliard committed
450 451
            break;
        case 0x3e:
452
            segprefix = context->SegDs;
Alexandre Julliard's avatar
Alexandre Julliard committed
453 454
            break;
        case 0x26:
455
            segprefix = context->SegEs;
Alexandre Julliard's avatar
Alexandre Julliard committed
456 457
            break;
        case 0x64:
458
            segprefix = context->SegFs;
Alexandre Julliard's avatar
Alexandre Julliard committed
459 460
            break;
        case 0x65:
461
            segprefix = context->SegGs;
Alexandre Julliard's avatar
Alexandre Julliard committed
462 463 464 465 466 467 468
            break;
        case 0x66:
            long_op = !long_op;  /* opcode size prefix */
            break;
        case 0x67:
            long_addr = !long_addr;  /* addr size prefix */
            break;
Alexandre Julliard's avatar
Alexandre Julliard committed
469
        case 0xf0:  /* lock */
Alexandre Julliard's avatar
Alexandre Julliard committed
470
	    break;
Alexandre Julliard's avatar
Alexandre Julliard committed
471
        case 0xf2:  /* repne */
Alexandre Julliard's avatar
Alexandre Julliard committed
472 473
	    repX = 1;
	    break;
Alexandre Julliard's avatar
Alexandre Julliard committed
474
        case 0xf3:  /* repe */
Alexandre Julliard's avatar
Alexandre Julliard committed
475
	    repX = 2;
Alexandre Julliard's avatar
Alexandre Julliard committed
476
            break;
Alexandre Julliard's avatar
Alexandre Julliard committed
477 478 479 480 481 482 483
        default:
            prefix = 0;  /* no more prefixes */
            break;
        }
        if (prefix)
        {
            instr++;
Alexandre Julliard's avatar
Alexandre Julliard committed
484
            prefixlen++;
Alexandre Julliard's avatar
Alexandre Julliard committed
485 486 487 488 489 490 491
        }
    }

    /* Now look at the actual instruction */

    switch(*instr)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
492 493 494
        case 0x07: /* pop es */
        case 0x17: /* pop ss */
        case 0x1f: /* pop ds */
Alexandre Julliard's avatar
Alexandre Julliard committed
495
            {
Alexandre Julliard's avatar
Alexandre Julliard committed
496
                WORD seg = *(WORD *)get_stack( context );
Alexandre Julliard's avatar
Alexandre Julliard committed
497 498 499
                if (INSTR_ReplaceSelector( context, &seg ))
                {
                    switch(*instr)
Alexandre Julliard's avatar
Alexandre Julliard committed
500
                    {
501 502 503
                    case 0x07: context->SegEs = seg; break;
                    case 0x17: context->SegSs = seg; break;
                    case 0x1f: context->SegDs = seg; break;
Alexandre Julliard's avatar
Alexandre Julliard committed
504
                    }
Alexandre Julliard's avatar
Alexandre Julliard committed
505
                    add_stack(context, long_op ? 4 : 2);
506
                    context->Eip += prefixlen + 1;
507
                    return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
508
                }
Alexandre Julliard's avatar
Alexandre Julliard committed
509
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
510
            break;  /* Unable to emulate it */
Alexandre Julliard's avatar
Alexandre Julliard committed
511

Alexandre Julliard's avatar
Alexandre Julliard committed
512 513
        case 0x0f: /* extended instruction */
            switch(instr[1])
Alexandre Julliard's avatar
Alexandre Julliard committed
514
            {
515
            case 0x22: /* mov %eax, %crX */
516 517
                switch (instr[2])
                {
518
                case 0xc0:
519 520 521
                    FIXME("mov %%eax, %%cr0 at 0x%08x, EAX=0x%08x\n",
                          context->Eip,context->Eax );
                          context->Eip += prefixlen+3;
522 523 524 525 526 527
                    return ExceptionContinueExecution;
                default:
                    break; /* Fallthrough to bad instruction handling */
                }
                break; /* Fallthrough to bad instruction handling */
            case 0x20: /* mov %crX, %eax */
528 529
                switch (instr[2])
                {
530 531 532 533 534 535 536 537 538 539 540 541
                case 0xe0: /* mov %cr4, %eax */
                    /* CR4 register: See linux/arch/i386/mm/init.c, X86_CR4_ defs
                     * bit 0: VME Virtual Mode Exception ?
                     * bit 1: PVI Protected mode Virtual Interrupt
                     * bit 2: TSD Timestamp disable
                     * bit 3: DE  Debugging extensions
                     * bit 4: PSE Page size extensions
                     * bit 5: PAE Physical address extension
                     * bit 6: MCE Machine check enable
                     * bit 7: PGE Enable global pages
                     * bit 8: PCE Enable performance counters at IPL3
                     */
542
                    FIXME("mov %%cr4, %%eax at 0x%08x\n",context->Eip);
543 544
                    context->Eax = 0;
                    context->Eip += prefixlen+3;
545 546
                    return ExceptionContinueExecution;
                case 0xc0: /* mov %cr0, %eax */
547
                    FIXME("mov %%cr0, %%eax at 0x%08x\n",context->Eip);
548 549
                    context->Eax = 0x10; /* FIXME: set more bits ? */
                    context->Eip += prefixlen+3;
550 551 552 553 554 555 556
                    return ExceptionContinueExecution;
                default: /* Fallthrough to illegal instruction */
                    break;
                }
                /* Fallthrough to illegal instruction */
                break;
            case 0x21: /* mov %drX, %eax */
557 558
                switch (instr[2])
                {
559 560
                case 0xc8: /* mov %dr1, %eax */
                    TRACE("mov %%dr1, %%eax at 0x%08x\n",context->Eip);
561 562 563
                    context->Eax = context->Dr1;
                    context->Eip += prefixlen+3;
                    return ExceptionContinueExecution;
564 565
                case 0xf8: /* mov %dr7, %eax */
                    TRACE("mov %%dr7, %%eax at 0x%08x\n",context->Eip);
566 567 568 569
                    context->Eax = 0x400;
                    context->Eip += prefixlen+3;
                    return ExceptionContinueExecution;
                }
570
                FIXME("Unsupported DR register, eip+2 is %02x\n", instr[2]);
571 572
                /* fallthrough to illegal instruction */
                break;
573
            case 0x23: /* mov %eax, %drX */
574 575
                switch (instr[2])
                {
576
                case 0xc8: /* mov %eax, %dr1 */
577 578 579 580
                    context->Dr1 = context->Eax;
                    context->Eip += prefixlen+3;
                    return ExceptionContinueExecution;
                }
581
                FIXME("Unsupported DR register, eip+2 is %02x\n", instr[2]);
582 583
                /* fallthrough to illegal instruction */
                break;
Alexandre Julliard's avatar
Alexandre Julliard committed
584
            case 0xa1: /* pop fs */
Alexandre Julliard's avatar
Alexandre Julliard committed
585
                {
Alexandre Julliard's avatar
Alexandre Julliard committed
586
                    WORD seg = *(WORD *)get_stack( context );
Alexandre Julliard's avatar
Alexandre Julliard committed
587
                    if (INSTR_ReplaceSelector( context, &seg ))
Alexandre Julliard's avatar
Alexandre Julliard committed
588
                    {
589
                        context->SegFs = seg;
Alexandre Julliard's avatar
Alexandre Julliard committed
590
                        add_stack(context, long_op ? 4 : 2);
591
                        context->Eip += prefixlen + 2;
592
                        return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
593 594 595
                    }
                }
                break;
Alexandre Julliard's avatar
Alexandre Julliard committed
596 597
            case 0xa9: /* pop gs */
                {
Alexandre Julliard's avatar
Alexandre Julliard committed
598
                    WORD seg = *(WORD *)get_stack( context );
Alexandre Julliard's avatar
Alexandre Julliard committed
599
                    if (INSTR_ReplaceSelector( context, &seg ))
Alexandre Julliard's avatar
Alexandre Julliard committed
600
                    {
601
                        context->SegGs = seg;
Alexandre Julliard's avatar
Alexandre Julliard committed
602
                        add_stack(context, long_op ? 4 : 2);
603
                        context->Eip += prefixlen + 2;
604
                        return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
605 606 607 608 609 610 611 612 613
                    }
                }
                break;
            case 0xb2: /* lss addr,reg */
            case 0xb4: /* lfs addr,reg */
            case 0xb5: /* lgs addr,reg */
                if (INSTR_EmulateLDS( context, instr, long_op,
                                      long_addr, segprefix, &len ))
                {
614
                    context->Eip += prefixlen + len;
615
                    return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
616 617 618 619
                }
                break;
            }
            break;  /* Unable to emulate it */
Alexandre Julliard's avatar
Alexandre Julliard committed
620

Alexandre Julliard's avatar
Alexandre Julliard committed
621 622 623 624 625 626 627
        case 0x6c: /* insb     */
        case 0x6d: /* insw/d   */
        case 0x6e: /* outsb    */
        case 0x6f: /* outsw/d  */
	    {
	      int typ = *instr;  /* Just in case it's overwritten.  */
	      int outp = (typ >= 0x6e);
Alexandre Julliard's avatar
Alexandre Julliard committed
628
	      unsigned long count = repX ?
629
                          (long_addr ? context->Ecx : LOWORD(context->Ecx)) : 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
630
	      int opsize = (typ & 1) ? (long_op ? 4 : 2) : 1;
631 632
	      int step = (context->EFlags & 0x400) ? -opsize : +opsize;
	      int seg = outp ? context->SegDs : context->SegEs;  /* FIXME: is this right? */
Alexandre Julliard's avatar
Alexandre Julliard committed
633 634

	      if (outp)
635
              {
636
		/* FIXME: Check segment is readable.  */
637
              }
Alexandre Julliard's avatar
Alexandre Julliard committed
638
	      else
639
              {
640
		/* FIXME: Check segment is writable.  */
641
              }
Alexandre Julliard's avatar
Alexandre Julliard committed
642 643

	      if (repX)
644
              {
645 646
		if (long_addr) context->Ecx = 0;
		else SET_LOWORD(context->Ecx,0);
647
              }
Alexandre Julliard's avatar
Alexandre Julliard committed
648 649 650 651

	      while (count-- > 0)
		{
		  void *data;
652
                  WORD dx = LOWORD(context->Edx);
Alexandre Julliard's avatar
Alexandre Julliard committed
653
		  if (outp)
Alexandre Julliard's avatar
Alexandre Julliard committed
654
                  {
655 656 657
                      data = make_ptr( context, seg, context->Esi, long_addr );
                      if (long_addr) context->Esi += step;
                      else ADD_LOWORD(context->Esi,step);
Alexandre Julliard's avatar
Alexandre Julliard committed
658
                  }
Alexandre Julliard's avatar
Alexandre Julliard committed
659
		  else
Alexandre Julliard's avatar
Alexandre Julliard committed
660
                  {
661 662 663
                      data = make_ptr( context, seg, context->Edi, long_addr );
                      if (long_addr) context->Edi += step;
                      else ADD_LOWORD(context->Edi,step);
Alexandre Julliard's avatar
Alexandre Julliard committed
664
                  }
665

Alexandre Julliard's avatar
Alexandre Julliard committed
666
		  switch (typ)
Alexandre Julliard's avatar
Alexandre Julliard committed
667
                  {
Alexandre Julliard's avatar
Alexandre Julliard committed
668
		    case 0x6c:
669
		      *(BYTE *)data = INSTR_inport( dx, 1, context );
Alexandre Julliard's avatar
Alexandre Julliard committed
670 671 672
		      break;
		    case 0x6d:
		      if (long_op)
673
                          *(DWORD *)data = INSTR_inport( dx, 4, context );
Alexandre Julliard's avatar
Alexandre Julliard committed
674
		      else
675
                          *(WORD *)data = INSTR_inport( dx, 2, context );
Alexandre Julliard's avatar
Alexandre Julliard committed
676 677
		      break;
		    case 0x6e:
678
                        INSTR_outport( dx, 1, *(BYTE *)data, context );
Alexandre Julliard's avatar
Alexandre Julliard committed
679
                        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
680
		    case 0x6f:
Alexandre Julliard's avatar
Alexandre Julliard committed
681
                        if (long_op)
682
                            INSTR_outport( dx, 4, *(DWORD *)data, context );
Alexandre Julliard's avatar
Alexandre Julliard committed
683
                        else
684
                            INSTR_outport( dx, 2, *(WORD *)data, context );
Alexandre Julliard's avatar
Alexandre Julliard committed
685
                        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
686 687
		    }
		}
688
              context->Eip += prefixlen + 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
689
	    }
690
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
691

Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
692 693 694 695 696 697 698 699 700 701
        case 0x8b: /* mov Ev, Gv */
            {
                BYTE *addr = INSTR_GetOperandAddr(context, instr + 1, long_addr,
                                                  segprefix, &len);
                struct idtr idtr = get_idtr();
                unsigned int offset = addr - idtr.base;

                if (offset <= idtr.limit + 1 - (long_op ? 4 : 2))
                {
                    idt[1].LimitLow = 0x100; /* FIXME */
702
                    idt[2].LimitLow = 0x11E; /* FIXME */
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
703
                    idt[3].LimitLow = 0x500; /* FIXME */
704
                    store_reg( context, instr[1], (BYTE *)idt + offset, long_op );
Ivan Leo Puoti's avatar
Ivan Leo Puoti committed
705 706 707 708 709 710
                    context->Eip += prefixlen + len + 1;
                    return ExceptionContinueExecution;
                }
            }
            break;  /* Unable to emulate it */

Alexandre Julliard's avatar
Alexandre Julliard committed
711
        case 0x8e: /* mov XX,segment_reg */
Alexandre Julliard's avatar
Alexandre Julliard committed
712
            {
Alexandre Julliard's avatar
Alexandre Julliard committed
713 714
                WORD seg;
                BYTE *addr = INSTR_GetOperandAddr(context, instr + 1,
Alexandre Julliard's avatar
Alexandre Julliard committed
715
                                                  long_addr, segprefix, &len );
Alexandre Julliard's avatar
Alexandre Julliard committed
716 717 718
                if (!addr)
                    break;  /* Unable to emulate it */
                seg = *(WORD *)addr;
Alexandre Julliard's avatar
Alexandre Julliard committed
719
                if (!INSTR_ReplaceSelector( context, &seg ))
Alexandre Julliard's avatar
Alexandre Julliard committed
720 721 722 723 724
                    break;  /* Unable to emulate it */

                switch((instr[1] >> 3) & 7)
                {
                case 0:
725 726
                    context->SegEs = seg;
                    context->Eip += prefixlen + len + 1;
727
                    return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
728 729 730
                case 1:  /* cs */
                    break;
                case 2:
731 732
                    context->SegSs = seg;
                    context->Eip += prefixlen + len + 1;
733
                    return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
734
                case 3:
735 736
                    context->SegDs = seg;
                    context->Eip += prefixlen + len + 1;
737
                    return ExceptionContinueExecution;
738
                case 4:
739 740
                    context->SegFs = seg;
                    context->Eip += prefixlen + len + 1;
741
                    return ExceptionContinueExecution;
742
                case 5:
743 744
                    context->SegGs = seg;
                    context->Eip += prefixlen + len + 1;
745
                    return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
746 747 748 749 750 751 752 753 754 755 756 757
                case 6:  /* unused */
                case 7:  /* unused */
                    break;
                }
            }
            break;  /* Unable to emulate it */

        case 0xc4: /* les addr,reg */
        case 0xc5: /* lds addr,reg */
            if (INSTR_EmulateLDS( context, instr, long_op,
                                  long_addr, segprefix, &len ))
            {
758
                context->Eip += prefixlen + len;
759
                return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
760 761
            }
            break;  /* Unable to emulate it */
762

Alexandre Julliard's avatar
Alexandre Julliard committed
763
        case 0xcd: /* int <XX> */
764 765 766
            context->Eip += prefixlen + 2;
            if (DOSVM_EmulateInterruptPM( context, instr[1] )) return ExceptionContinueExecution;
            context->Eip -= prefixlen + 2;  /* restore eip */
767
            break;  /* Unable to emulate it */
Alexandre Julliard's avatar
Alexandre Julliard committed
768

Alexandre Julliard's avatar
Alexandre Julliard committed
769
        case 0xcf: /* iret */
770
            if (wine_ldt_is_system(context->SegCs)) break;  /* don't emulate it in 32-bit code */
Alexandre Julliard's avatar
Alexandre Julliard committed
771 772
            if (long_op)
            {
Alexandre Julliard's avatar
Alexandre Julliard committed
773
                DWORD *stack = get_stack( context );
774 775 776
                context->Eip = *stack++;
                context->SegCs  = *stack++;
                context->EFlags = *stack;
Alexandre Julliard's avatar
Alexandre Julliard committed
777
                add_stack(context, 3*sizeof(DWORD));  /* Pop the return address and flags */
Alexandre Julliard's avatar
Alexandre Julliard committed
778 779 780
            }
            else
            {
Alexandre Julliard's avatar
Alexandre Julliard committed
781
                WORD *stack = get_stack( context );
782 783 784
                context->Eip = *stack++;
                context->SegCs  = *stack++;
                SET_LOWORD(context->EFlags,*stack);
Alexandre Julliard's avatar
Alexandre Julliard committed
785
                add_stack(context, 3*sizeof(WORD));  /* Pop the return address and flags */
Alexandre Julliard's avatar
Alexandre Julliard committed
786
            }
787
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
788 789

        case 0xe4: /* inb al,XX */
790 791
            SET_LOBYTE(context->Eax,INSTR_inport( instr[1], 1, context ));
            context->Eip += prefixlen + 2;
792
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
793 794

        case 0xe5: /* in (e)ax,XX */
795
            if (long_op)
796
                context->Eax = INSTR_inport( instr[1], 4, context );
797
            else
798 799
                SET_LOWORD(context->Eax, INSTR_inport( instr[1], 2, context ));
            context->Eip += prefixlen + 2;
800
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
801 802

        case 0xe6: /* outb XX,al */
803 804
            INSTR_outport( instr[1], 1, LOBYTE(context->Eax), context );
            context->Eip += prefixlen + 2;
805
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
806 807

        case 0xe7: /* out XX,(e)ax */
808
            if (long_op)
809
                INSTR_outport( instr[1], 4, context->Eax, context );
810
            else
811 812
                INSTR_outport( instr[1], 2, LOWORD(context->Eax), context );
            context->Eip += prefixlen + 2;
813
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
814 815

        case 0xec: /* inb al,dx */
816 817
            SET_LOBYTE(context->Eax, INSTR_inport( LOWORD(context->Edx), 1, context ) );
            context->Eip += prefixlen + 1;
818
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
819 820

        case 0xed: /* in (e)ax,dx */
821
            if (long_op)
822
                context->Eax = INSTR_inport( LOWORD(context->Edx), 4, context );
823
            else
824 825
                SET_LOWORD(context->Eax, INSTR_inport( LOWORD(context->Edx), 2, context ));
            context->Eip += prefixlen + 1;
826
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
827 828

        case 0xee: /* outb dx,al */
829 830
            INSTR_outport( LOWORD(context->Edx), 1, LOBYTE(context->Eax), context );
            context->Eip += prefixlen + 1;
831
            return ExceptionContinueExecution;
832

Alexandre Julliard's avatar
Alexandre Julliard committed
833
        case 0xef: /* out dx,(e)ax */
834
            if (long_op)
835
                INSTR_outport( LOWORD(context->Edx), 4, context->Eax, context );
836
            else
837 838
                INSTR_outport( LOWORD(context->Edx), 2, LOWORD(context->Eax), context );
            context->Eip += prefixlen + 1;
839
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
840

841
        case 0xfa: /* cli */
842
            get_vm86_teb_info()->dpmi_vif = 0;
843
            context->Eip += prefixlen + 1;
844
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
845

846
        case 0xfb: /* sti */
847
            get_vm86_teb_info()->dpmi_vif = 1;
848
            context->Eip += prefixlen + 1;
849
            if (get_vm86_teb_info()->vm86_pending)
850
            {
851
                get_vm86_teb_info()->vm86_pending = 0;
852 853 854
                rec->ExceptionCode = EXCEPTION_VM86_STI;
                break; /* Handle the pending event. */
            }
855
            return ExceptionContinueExecution;
Alexandre Julliard's avatar
Alexandre Julliard committed
856
    }
857
    return ExceptionContinueSearch;  /* Unable to emulate it */
Alexandre Julliard's avatar
Alexandre Julliard committed
858
}
859

860

861 862 863 864 865 866 867 868 869
/***********************************************************************
 *           INSTR_vectored_handler
 *
 * Vectored exception handler used to emulate protected instructions
 * from 32-bit code.
 */
LONG CALLBACK INSTR_vectored_handler( EXCEPTION_POINTERS *ptrs )
{
    EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
870
    CONTEXT *context = ptrs->ContextRecord;
871 872 873 874 875

    if (wine_ldt_is_system(context->SegCs) &&
        (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
         record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION))
    {
876
        if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
877 878 879 880 881 882
            return EXCEPTION_CONTINUE_EXECUTION;
    }
    return EXCEPTION_CONTINUE_SEARCH;
}


883 884 885
/***********************************************************************
 *           DOS3Call         (KERNEL.102)
 */
886
void WINAPI DOS3Call( CONTEXT *context )
887
{
888
    __wine_call_int_handler( context, 0x21 );
889 890 891 892 893 894
}


/***********************************************************************
 *           NetBIOSCall      (KERNEL.103)
 */
895
void WINAPI NetBIOSCall16( CONTEXT *context )
896
{
897
    __wine_call_int_handler( context, 0x5c );
898 899 900 901 902 903 904 905
}


/***********************************************************************
 *		GetSetKernelDOSProc (KERNEL.311)
 */
FARPROC16 WINAPI GetSetKernelDOSProc16( FARPROC16 DosProc )
{
906
    FIXME("(DosProc=%p): stub\n", DosProc);
907 908
    return NULL;
}