helper.c 149.3 KB
Newer Older
B
bellard 已提交
1 2
/*
 *  i386 helpers
3
 *
B
bellard 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *  Copyright (c) 2003 Fabrice Bellard
 *
 * 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 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
20
#define CPU_NO_GLOBAL_REGS
B
bellard 已提交
21
#include "exec.h"
22
#include "host-utils.h"
B
bellard 已提交
23

B
bellard 已提交
24 25
//#define DEBUG_PCALL

26 27 28
#if 0
#define raise_exception_err(a, b)\
do {\
29 30
    if (logfile)\
        fprintf(logfile, "raise_exception line=%d\n", __LINE__);\
31 32 33 34
    (raise_exception_err)(a, b);\
} while (0)
#endif

B
bellard 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
const uint8_t parity_table[256] = {
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
    0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
};

/* modulo 17 table */
const uint8_t rclw_table[32] = {
72
    0, 1, 2, 3, 4, 5, 6, 7,
B
bellard 已提交
73 74 75 76 77 78 79
    8, 9,10,11,12,13,14,15,
   16, 0, 1, 2, 3, 4, 5, 6,
    7, 8, 9,10,11,12,13,14,
};

/* modulo 9 table */
const uint8_t rclb_table[32] = {
80
    0, 1, 2, 3, 4, 5, 6, 7,
B
bellard 已提交
81
    8, 0, 1, 2, 3, 4, 5, 6,
82
    7, 8, 0, 1, 2, 3, 4, 5,
B
bellard 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
    6, 7, 8, 0, 1, 2, 3, 4,
};

const CPU86_LDouble f15rk[7] =
{
    0.00000000000000000000L,
    1.00000000000000000000L,
    3.14159265358979323851L,  /*pi*/
    0.30102999566398119523L,  /*lg2*/
    0.69314718055994530943L,  /*ln2*/
    1.44269504088896340739L,  /*l2e*/
    3.32192809488736234781L,  /*l2t*/
};
96

97
/* broken thread support */
B
bellard 已提交
98 99 100

spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;

101
void helper_lock(void)
B
bellard 已提交
102 103 104 105
{
    spin_lock(&global_cpu_lock);
}

106
void helper_unlock(void)
B
bellard 已提交
107 108 109 110
{
    spin_unlock(&global_cpu_lock);
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124
void helper_write_eflags(target_ulong t0, uint32_t update_mask)
{
    load_eflags(t0, update_mask);
}

target_ulong helper_read_eflags(void)
{
    uint32_t eflags;
    eflags = cc_table[CC_OP].compute_all();
    eflags |= (DF & DF_MASK);
    eflags |= env->eflags & ~(VM_MASK | RF_MASK);
    return eflags;
}

125 126 127 128 129 130
/* return non zero if error */
static inline int load_segment(uint32_t *e1_ptr, uint32_t *e2_ptr,
                               int selector)
{
    SegmentCache *dt;
    int index;
B
bellard 已提交
131
    target_ulong ptr;
132 133 134 135 136 137 138 139 140 141 142 143 144

    if (selector & 0x4)
        dt = &env->ldt;
    else
        dt = &env->gdt;
    index = selector & ~7;
    if ((index + 7) > dt->limit)
        return -1;
    ptr = dt->base + index;
    *e1_ptr = ldl_kernel(ptr);
    *e2_ptr = ldl_kernel(ptr + 4);
    return 0;
}
145

146 147 148 149 150 151 152 153 154
static inline unsigned int get_seg_limit(uint32_t e1, uint32_t e2)
{
    unsigned int limit;
    limit = (e1 & 0xffff) | (e2 & 0x000f0000);
    if (e2 & DESC_G_MASK)
        limit = (limit << 12) | 0xfff;
    return limit;
}

B
bellard 已提交
155
static inline uint32_t get_seg_base(uint32_t e1, uint32_t e2)
156
{
B
bellard 已提交
157
    return ((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
158 159 160 161 162 163 164 165 166 167 168 169 170
}

static inline void load_seg_cache_raw_dt(SegmentCache *sc, uint32_t e1, uint32_t e2)
{
    sc->base = get_seg_base(e1, e2);
    sc->limit = get_seg_limit(e1, e2);
    sc->flags = e2;
}

/* init the segment cache in vm86 mode. */
static inline void load_seg_vm(int seg, int selector)
{
    selector &= 0xffff;
171
    cpu_x86_load_seg_cache(env, seg, selector,
B
bellard 已提交
172
                           (selector << 4), 0xffff, 0);
173 174
}

175
static inline void get_ss_esp_from_tss(uint32_t *ss_ptr,
B
bellard 已提交
176 177 178
                                       uint32_t *esp_ptr, int dpl)
{
    int type, index, shift;
179

B
bellard 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
#if 0
    {
        int i;
        printf("TR: base=%p limit=%x\n", env->tr.base, env->tr.limit);
        for(i=0;i<env->tr.limit;i++) {
            printf("%02x ", env->tr.base[i]);
            if ((i & 7) == 7) printf("\n");
        }
        printf("\n");
    }
#endif

    if (!(env->tr.flags & DESC_P_MASK))
        cpu_abort(env, "invalid tss");
    type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
    if ((type & 7) != 1)
        cpu_abort(env, "invalid tss type");
    shift = type >> 3;
    index = (dpl * 4 + 2) << shift;
    if (index + (4 << shift) - 1 > env->tr.limit)
        raise_exception_err(EXCP0A_TSS, env->tr.selector & 0xfffc);
    if (shift == 0) {
B
bellard 已提交
202 203
        *esp_ptr = lduw_kernel(env->tr.base + index);
        *ss_ptr = lduw_kernel(env->tr.base + index + 2);
B
bellard 已提交
204
    } else {
B
bellard 已提交
205 206
        *esp_ptr = ldl_kernel(env->tr.base + index);
        *ss_ptr = lduw_kernel(env->tr.base + index + 4);
B
bellard 已提交
207 208 209
    }
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
/* XXX: merge with load_seg() */
static void tss_load_seg(int seg_reg, int selector)
{
    uint32_t e1, e2;
    int rpl, dpl, cpl;

    if ((selector & 0xfffc) != 0) {
        if (load_segment(&e1, &e2, selector) != 0)
            raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
        if (!(e2 & DESC_S_MASK))
            raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
        rpl = selector & 3;
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
        cpl = env->hflags & HF_CPL_MASK;
        if (seg_reg == R_CS) {
            if (!(e2 & DESC_CS_MASK))
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
227
            /* XXX: is it correct ? */
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
            if (dpl != rpl)
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
            if ((e2 & DESC_C_MASK) && dpl > rpl)
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
        } else if (seg_reg == R_SS) {
            /* SS must be writable data */
            if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
            if (dpl != cpl || dpl != rpl)
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
        } else {
            /* not readable code */
            if ((e2 & DESC_CS_MASK) && !(e2 & DESC_R_MASK))
                raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
            /* if data or non conforming code, checks the rights */
            if (((e2 >> DESC_TYPE_SHIFT) & 0xf) < 12) {
                if (dpl < cpl || dpl < rpl)
                    raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
            }
        }
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
250
        cpu_x86_load_seg_cache(env, seg_reg, selector,
251 252 253 254
                       get_seg_base(e1, e2),
                       get_seg_limit(e1, e2),
                       e2);
    } else {
255
        if (seg_reg == R_SS || seg_reg == R_CS)
256 257 258 259 260 261 262 263 264
            raise_exception_err(EXCP0A_TSS, selector & 0xfffc);
    }
}

#define SWITCH_TSS_JMP  0
#define SWITCH_TSS_IRET 1
#define SWITCH_TSS_CALL 2

/* XXX: restore CPU state in registers (PowerPC case) */
265
static void switch_tss(int tss_selector,
B
bellard 已提交
266 267
                       uint32_t e1, uint32_t e2, int source,
                       uint32_t next_eip)
B
bellard 已提交
268
{
269
    int tss_limit, tss_limit_max, type, old_tss_limit_max, old_type, v1, v2, i;
B
bellard 已提交
270
    target_ulong tss_base;
271 272 273
    uint32_t new_regs[8], new_segs[6];
    uint32_t new_eflags, new_eip, new_cr3, new_ldt, new_trap;
    uint32_t old_eflags, eflags_mask;
B
bellard 已提交
274 275
    SegmentCache *dt;
    int index;
B
bellard 已提交
276
    target_ulong ptr;
B
bellard 已提交
277

278
    type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
279
#ifdef DEBUG_PCALL
B
bellard 已提交
280
    if (loglevel & CPU_LOG_PCALL)
281 282
        fprintf(logfile, "switch_tss: sel=0x%04x type=%d src=%d\n", tss_selector, type, source);
#endif
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

    /* if task gate, we read the TSS segment and we load it */
    if (type == 5) {
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);
        tss_selector = e1 >> 16;
        if (tss_selector & 4)
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
        if (load_segment(&e1, &e2, tss_selector) != 0)
            raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
        if (e2 & DESC_S_MASK)
            raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
        if ((type & 7) != 1)
            raise_exception_err(EXCP0D_GPF, tss_selector & 0xfffc);
    }

    if (!(e2 & DESC_P_MASK))
        raise_exception_err(EXCP0B_NOSEG, tss_selector & 0xfffc);

    if (type & 8)
        tss_limit_max = 103;
B
bellard 已提交
305
    else
306 307 308
        tss_limit_max = 43;
    tss_limit = get_seg_limit(e1, e2);
    tss_base = get_seg_base(e1, e2);
309
    if ((tss_selector & 4) != 0 ||
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
        tss_limit < tss_limit_max)
        raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
    old_type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
    if (old_type & 8)
        old_tss_limit_max = 103;
    else
        old_tss_limit_max = 43;

    /* read all the registers from the new TSS */
    if (type & 8) {
        /* 32 bit */
        new_cr3 = ldl_kernel(tss_base + 0x1c);
        new_eip = ldl_kernel(tss_base + 0x20);
        new_eflags = ldl_kernel(tss_base + 0x24);
        for(i = 0; i < 8; i++)
            new_regs[i] = ldl_kernel(tss_base + (0x28 + i * 4));
        for(i = 0; i < 6; i++)
            new_segs[i] = lduw_kernel(tss_base + (0x48 + i * 4));
        new_ldt = lduw_kernel(tss_base + 0x60);
        new_trap = ldl_kernel(tss_base + 0x64);
    } else {
        /* 16 bit */
        new_cr3 = 0;
        new_eip = lduw_kernel(tss_base + 0x0e);
        new_eflags = lduw_kernel(tss_base + 0x10);
        for(i = 0; i < 8; i++)
            new_regs[i] = lduw_kernel(tss_base + (0x12 + i * 2)) | 0xffff0000;
        for(i = 0; i < 4; i++)
            new_segs[i] = lduw_kernel(tss_base + (0x22 + i * 4));
        new_ldt = lduw_kernel(tss_base + 0x2a);
        new_segs[R_FS] = 0;
        new_segs[R_GS] = 0;
        new_trap = 0;
    }
344

345 346 347 348 349 350
    /* NOTE: we must avoid memory exceptions during the task switch,
       so we make dummy accesses before */
    /* XXX: it can still fail in some cases, so a bigger hack is
       necessary to valid the TLB after having done the accesses */

    v1 = ldub_kernel(env->tr.base);
B
bellard 已提交
351
    v2 = ldub_kernel(env->tr.base + old_tss_limit_max);
352 353
    stb_kernel(env->tr.base, v1);
    stb_kernel(env->tr.base + old_tss_limit_max, v2);
354

355 356
    /* clear busy bit (it is restartable) */
    if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_IRET) {
B
bellard 已提交
357
        target_ulong ptr;
358
        uint32_t e2;
B
bellard 已提交
359
        ptr = env->gdt.base + (env->tr.selector & ~7);
360 361 362 363 364 365 366
        e2 = ldl_kernel(ptr + 4);
        e2 &= ~DESC_TSS_BUSY_MASK;
        stl_kernel(ptr + 4, e2);
    }
    old_eflags = compute_eflags();
    if (source == SWITCH_TSS_IRET)
        old_eflags &= ~NT_MASK;
367

368 369 370
    /* save the current state in the old TSS */
    if (type & 8) {
        /* 32 bit */
B
bellard 已提交
371
        stl_kernel(env->tr.base + 0x20, next_eip);
372
        stl_kernel(env->tr.base + 0x24, old_eflags);
B
bellard 已提交
373 374 375 376 377 378 379 380
        stl_kernel(env->tr.base + (0x28 + 0 * 4), EAX);
        stl_kernel(env->tr.base + (0x28 + 1 * 4), ECX);
        stl_kernel(env->tr.base + (0x28 + 2 * 4), EDX);
        stl_kernel(env->tr.base + (0x28 + 3 * 4), EBX);
        stl_kernel(env->tr.base + (0x28 + 4 * 4), ESP);
        stl_kernel(env->tr.base + (0x28 + 5 * 4), EBP);
        stl_kernel(env->tr.base + (0x28 + 6 * 4), ESI);
        stl_kernel(env->tr.base + (0x28 + 7 * 4), EDI);
381 382 383 384
        for(i = 0; i < 6; i++)
            stw_kernel(env->tr.base + (0x48 + i * 4), env->segs[i].selector);
    } else {
        /* 16 bit */
B
bellard 已提交
385
        stw_kernel(env->tr.base + 0x0e, next_eip);
386
        stw_kernel(env->tr.base + 0x10, old_eflags);
B
bellard 已提交
387 388 389 390 391 392 393 394
        stw_kernel(env->tr.base + (0x12 + 0 * 2), EAX);
        stw_kernel(env->tr.base + (0x12 + 1 * 2), ECX);
        stw_kernel(env->tr.base + (0x12 + 2 * 2), EDX);
        stw_kernel(env->tr.base + (0x12 + 3 * 2), EBX);
        stw_kernel(env->tr.base + (0x12 + 4 * 2), ESP);
        stw_kernel(env->tr.base + (0x12 + 5 * 2), EBP);
        stw_kernel(env->tr.base + (0x12 + 6 * 2), ESI);
        stw_kernel(env->tr.base + (0x12 + 7 * 2), EDI);
395 396 397
        for(i = 0; i < 4; i++)
            stw_kernel(env->tr.base + (0x22 + i * 4), env->segs[i].selector);
    }
398

399 400 401 402 403 404 405 406 407 408
    /* now if an exception occurs, it will occurs in the next task
       context */

    if (source == SWITCH_TSS_CALL) {
        stw_kernel(tss_base, env->tr.selector);
        new_eflags |= NT_MASK;
    }

    /* set busy bit */
    if (source == SWITCH_TSS_JMP || source == SWITCH_TSS_CALL) {
B
bellard 已提交
409
        target_ulong ptr;
410
        uint32_t e2;
B
bellard 已提交
411
        ptr = env->gdt.base + (tss_selector & ~7);
412 413 414 415 416 417 418 419
        e2 = ldl_kernel(ptr + 4);
        e2 |= DESC_TSS_BUSY_MASK;
        stl_kernel(ptr + 4, e2);
    }

    /* set the new CPU state */
    /* from this point, any exception which occurs can give problems */
    env->cr[0] |= CR0_TS_MASK;
B
bellard 已提交
420
    env->hflags |= HF_TS_MASK;
421 422 423 424
    env->tr.selector = tss_selector;
    env->tr.base = tss_base;
    env->tr.limit = tss_limit;
    env->tr.flags = e2 & ~DESC_TSS_BUSY_MASK;
425

426
    if ((type & 8) && (env->cr[0] & CR0_PG_MASK)) {
427
        cpu_x86_update_cr3(env, new_cr3);
428
    }
429

430 431 432
    /* load all registers without an exception, then reload them with
       possible exception */
    env->eip = new_eip;
433
    eflags_mask = TF_MASK | AC_MASK | ID_MASK |
434
        IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK;
435 436 437
    if (!(type & 8))
        eflags_mask &= 0xffff;
    load_eflags(new_eflags, eflags_mask);
B
bellard 已提交
438 439 440 441 442 443 444 445 446
    /* XXX: what to do in 16 bit case ? */
    EAX = new_regs[0];
    ECX = new_regs[1];
    EDX = new_regs[2];
    EBX = new_regs[3];
    ESP = new_regs[4];
    EBP = new_regs[5];
    ESI = new_regs[6];
    EDI = new_regs[7];
447
    if (new_eflags & VM_MASK) {
448
        for(i = 0; i < 6; i++)
449 450 451 452 453 454 455 456
            load_seg_vm(i, new_segs[i]);
        /* in vm86, CPL is always 3 */
        cpu_x86_set_cpl(env, 3);
    } else {
        /* CPL is set the RPL of CS */
        cpu_x86_set_cpl(env, new_segs[R_CS] & 3);
        /* first just selectors as the rest may trigger exceptions */
        for(i = 0; i < 6; i++)
B
bellard 已提交
457
            cpu_x86_load_seg_cache(env, i, new_segs[i], 0, 0, 0);
458
    }
459

460
    env->ldt.selector = new_ldt & ~4;
B
bellard 已提交
461
    env->ldt.base = 0;
462 463 464 465 466 467 468
    env->ldt.limit = 0;
    env->ldt.flags = 0;

    /* load the LDT */
    if (new_ldt & 4)
        raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);

469 470 471 472 473 474 475 476 477 478 479 480 481 482
    if ((new_ldt & 0xfffc) != 0) {
        dt = &env->gdt;
        index = new_ldt & ~7;
        if ((index + 7) > dt->limit)
            raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
        ptr = dt->base + index;
        e1 = ldl_kernel(ptr);
        e2 = ldl_kernel(ptr + 4);
        if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
            raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0A_TSS, new_ldt & 0xfffc);
        load_seg_cache_raw_dt(&env->ldt, e1, e2);
    }
483

484 485 486 487 488 489 490 491 492
    /* load the segments */
    if (!(new_eflags & VM_MASK)) {
        tss_load_seg(R_CS, new_segs[R_CS]);
        tss_load_seg(R_SS, new_segs[R_SS]);
        tss_load_seg(R_ES, new_segs[R_ES]);
        tss_load_seg(R_DS, new_segs[R_DS]);
        tss_load_seg(R_FS, new_segs[R_FS]);
        tss_load_seg(R_GS, new_segs[R_GS]);
    }
493

494 495
    /* check that EIP is in the CS segment limits */
    if (new_eip > env->segs[R_CS].limit) {
B
bellard 已提交
496
        /* XXX: different exception if CALL ? */
497 498
        raise_exception_err(EXCP0D_GPF, 0);
    }
B
bellard 已提交
499
}
500 501 502

/* check if Port I/O is allowed in TSS */
static inline void check_io(int addr, int size)
B
bellard 已提交
503
{
504
    int io_offset, val, mask;
505

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    /* TSS must be a valid 32 bit one */
    if (!(env->tr.flags & DESC_P_MASK) ||
        ((env->tr.flags >> DESC_TYPE_SHIFT) & 0xf) != 9 ||
        env->tr.limit < 103)
        goto fail;
    io_offset = lduw_kernel(env->tr.base + 0x66);
    io_offset += (addr >> 3);
    /* Note: the check needs two bytes */
    if ((io_offset + 1) > env->tr.limit)
        goto fail;
    val = lduw_kernel(env->tr.base + io_offset);
    val >>= (addr & 7);
    mask = (1 << size) - 1;
    /* all bits must be zero to allow the I/O */
    if ((val & mask) != 0) {
    fail:
        raise_exception_err(EXCP0D_GPF, 0);
    }
B
bellard 已提交
524 525
}

526
void helper_check_iob(uint32_t t0)
B
bellard 已提交
527
{
528
    check_io(t0, 1);
B
bellard 已提交
529 530
}

531
void helper_check_iow(uint32_t t0)
B
bellard 已提交
532
{
533
    check_io(t0, 2);
B
bellard 已提交
534 535
}

536
void helper_check_iol(uint32_t t0)
B
bellard 已提交
537
{
538
    check_io(t0, 4);
539 540
}

541
void helper_outb(uint32_t port, uint32_t data)
542
{
543
    cpu_outb(env, port, data & 0xff);
544 545
}

546
target_ulong helper_inb(uint32_t port)
547
{
548
    return cpu_inb(env, port);
549 550
}

551
void helper_outw(uint32_t port, uint32_t data)
552
{
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    cpu_outw(env, port, data & 0xffff);
}

target_ulong helper_inw(uint32_t port)
{
    return cpu_inw(env, port);
}

void helper_outl(uint32_t port, uint32_t data)
{
    cpu_outl(env, port, data);
}

target_ulong helper_inl(uint32_t port)
{
    return cpu_inl(env, port);
B
bellard 已提交
569 570
}

571 572 573 574 575 576 577 578
static inline unsigned int get_sp_mask(unsigned int e2)
{
    if (e2 & DESC_B_MASK)
        return 0xffffffff;
    else
        return 0xffff;
}

579 580 581 582 583 584 585 586 587 588 589 590 591 592
#ifdef TARGET_X86_64
#define SET_ESP(val, sp_mask)\
do {\
    if ((sp_mask) == 0xffff)\
        ESP = (ESP & ~0xffff) | ((val) & 0xffff);\
    else if ((sp_mask) == 0xffffffffLL)\
        ESP = (uint32_t)(val);\
    else\
        ESP = (val);\
} while (0)
#else
#define SET_ESP(val, sp_mask) ESP = (ESP & ~(sp_mask)) | ((val) & (sp_mask))
#endif

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
/* XXX: add a is_user flag to have proper security support */
#define PUSHW(ssp, sp, sp_mask, val)\
{\
    sp -= 2;\
    stw_kernel((ssp) + (sp & (sp_mask)), (val));\
}

#define PUSHL(ssp, sp, sp_mask, val)\
{\
    sp -= 4;\
    stl_kernel((ssp) + (sp & (sp_mask)), (val));\
}

#define POPW(ssp, sp, sp_mask, val)\
{\
    val = lduw_kernel((ssp) + (sp & (sp_mask)));\
    sp += 2;\
}

#define POPL(ssp, sp, sp_mask, val)\
{\
B
bellard 已提交
614
    val = (uint32_t)ldl_kernel((ssp) + (sp & (sp_mask)));\
615 616 617
    sp += 4;\
}

B
bellard 已提交
618 619 620 621 622
/* protected mode interrupt */
static void do_interrupt_protected(int intno, int is_int, int error_code,
                                   unsigned int next_eip, int is_hw)
{
    SegmentCache *dt;
B
bellard 已提交
623
    target_ulong ptr, ssp;
624
    int type, dpl, selector, ss_dpl, cpl;
B
bellard 已提交
625
    int has_error_code, new_stack, shift;
626
    uint32_t e1, e2, offset, ss, esp, ss_e1, ss_e2;
627
    uint32_t old_eip, sp_mask;
T
ths 已提交
628
    int svm_should_check = 1;
B
bellard 已提交
629

T
ths 已提交
630 631 632 633 634 635 636 637 638 639
    if ((env->intercept & INTERCEPT_SVM_MASK) && !is_int && next_eip==-1) {
        next_eip = EIP;
        svm_should_check = 0;
    }

    if (svm_should_check
        && (INTERCEPTEDl(_exceptions, 1 << intno)
        && !is_int)) {
        raise_interrupt(intno, is_int, error_code, 0);
    }
640 641 642 643 644 645 646 647 648 649 650 651 652 653
    has_error_code = 0;
    if (!is_int && !is_hw) {
        switch(intno) {
        case 8:
        case 10:
        case 11:
        case 12:
        case 13:
        case 14:
        case 17:
            has_error_code = 1;
            break;
        }
    }
B
bellard 已提交
654 655 656 657
    if (is_int)
        old_eip = next_eip;
    else
        old_eip = env->eip;
658

B
bellard 已提交
659 660 661 662
    dt = &env->idt;
    if (intno * 8 + 7 > dt->limit)
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
    ptr = dt->base + intno * 8;
B
bellard 已提交
663 664
    e1 = ldl_kernel(ptr);
    e2 = ldl_kernel(ptr + 4);
B
bellard 已提交
665 666 667 668
    /* check gate type */
    type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
    switch(type) {
    case 5: /* task gate */
669 670 671
        /* must do that check here to return the correct error code */
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
B
bellard 已提交
672
        switch_tss(intno * 8, e1, e2, SWITCH_TSS_CALL, old_eip);
673
        if (has_error_code) {
674 675
            int type;
            uint32_t mask;
676
            /* push the error code */
B
bellard 已提交
677 678
            type = (env->tr.flags >> DESC_TYPE_SHIFT) & 0xf;
            shift = type >> 3;
679 680 681 682
            if (env->segs[R_SS].flags & DESC_B_MASK)
                mask = 0xffffffff;
            else
                mask = 0xffff;
B
bellard 已提交
683
            esp = (ESP - (2 << shift)) & mask;
684 685 686 687 688
            ssp = env->segs[R_SS].base + esp;
            if (shift)
                stl_kernel(ssp, error_code);
            else
                stw_kernel(ssp, error_code);
689
            SET_ESP(esp, mask);
690 691
        }
        return;
B
bellard 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
    case 6: /* 286 interrupt gate */
    case 7: /* 286 trap gate */
    case 14: /* 386 interrupt gate */
    case 15: /* 386 trap gate */
        break;
    default:
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
        break;
    }
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    cpl = env->hflags & HF_CPL_MASK;
    /* check privledge if software int */
    if (is_int && dpl < cpl)
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
    /* check valid bit */
    if (!(e2 & DESC_P_MASK))
        raise_exception_err(EXCP0B_NOSEG, intno * 8 + 2);
    selector = e1 >> 16;
    offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
    if ((selector & 0xfffc) == 0)
        raise_exception_err(EXCP0D_GPF, 0);

    if (load_segment(&e1, &e2, selector) != 0)
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
    if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    if (dpl > cpl)
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
    if (!(e2 & DESC_P_MASK))
        raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
    if (!(e2 & DESC_C_MASK) && dpl < cpl) {
B
blueswir1 已提交
724
        /* to inner privilege */
B
bellard 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
        get_ss_esp_from_tss(&ss, &esp, dpl);
        if ((ss & 0xfffc) == 0)
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
        if ((ss & 3) != dpl)
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
        if (load_segment(&ss_e1, &ss_e2, ss) != 0)
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
        ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
        if (ss_dpl != dpl)
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
        if (!(ss_e2 & DESC_S_MASK) ||
            (ss_e2 & DESC_CS_MASK) ||
            !(ss_e2 & DESC_W_MASK))
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
        if (!(ss_e2 & DESC_P_MASK))
            raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
        new_stack = 1;
742 743
        sp_mask = get_sp_mask(ss_e2);
        ssp = get_seg_base(ss_e1, ss_e2);
B
bellard 已提交
744
    } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
B
blueswir1 已提交
745
        /* to same privilege */
746 747
        if (env->eflags & VM_MASK)
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
B
bellard 已提交
748
        new_stack = 0;
749 750 751
        sp_mask = get_sp_mask(env->segs[R_SS].flags);
        ssp = env->segs[R_SS].base;
        esp = ESP;
752
        dpl = cpl;
B
bellard 已提交
753 754 755
    } else {
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        new_stack = 0; /* avoid warning */
756
        sp_mask = 0; /* avoid warning */
B
bellard 已提交
757
        ssp = 0; /* avoid warning */
758
        esp = 0; /* avoid warning */
B
bellard 已提交
759 760 761
    }

    shift = type >> 3;
762 763 764

#if 0
    /* XXX: check that enough room is available */
B
bellard 已提交
765 766 767 768
    push_size = 6 + (new_stack << 2) + (has_error_code << 1);
    if (env->eflags & VM_MASK)
        push_size += 8;
    push_size <<= shift;
769
#endif
B
bellard 已提交
770 771
    if (shift == 1) {
        if (new_stack) {
772 773 774 775 776 777
            if (env->eflags & VM_MASK) {
                PUSHL(ssp, esp, sp_mask, env->segs[R_GS].selector);
                PUSHL(ssp, esp, sp_mask, env->segs[R_FS].selector);
                PUSHL(ssp, esp, sp_mask, env->segs[R_DS].selector);
                PUSHL(ssp, esp, sp_mask, env->segs[R_ES].selector);
            }
778 779
            PUSHL(ssp, esp, sp_mask, env->segs[R_SS].selector);
            PUSHL(ssp, esp, sp_mask, ESP);
B
bellard 已提交
780
        }
781 782 783
        PUSHL(ssp, esp, sp_mask, compute_eflags());
        PUSHL(ssp, esp, sp_mask, env->segs[R_CS].selector);
        PUSHL(ssp, esp, sp_mask, old_eip);
B
bellard 已提交
784
        if (has_error_code) {
785
            PUSHL(ssp, esp, sp_mask, error_code);
B
bellard 已提交
786 787 788
        }
    } else {
        if (new_stack) {
789 790 791 792 793 794
            if (env->eflags & VM_MASK) {
                PUSHW(ssp, esp, sp_mask, env->segs[R_GS].selector);
                PUSHW(ssp, esp, sp_mask, env->segs[R_FS].selector);
                PUSHW(ssp, esp, sp_mask, env->segs[R_DS].selector);
                PUSHW(ssp, esp, sp_mask, env->segs[R_ES].selector);
            }
795 796
            PUSHW(ssp, esp, sp_mask, env->segs[R_SS].selector);
            PUSHW(ssp, esp, sp_mask, ESP);
B
bellard 已提交
797
        }
798 799 800
        PUSHW(ssp, esp, sp_mask, compute_eflags());
        PUSHW(ssp, esp, sp_mask, env->segs[R_CS].selector);
        PUSHW(ssp, esp, sp_mask, old_eip);
B
bellard 已提交
801
        if (has_error_code) {
802
            PUSHW(ssp, esp, sp_mask, error_code);
B
bellard 已提交
803 804
        }
    }
805

806
    if (new_stack) {
807
        if (env->eflags & VM_MASK) {
B
bellard 已提交
808 809 810 811
            cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0, 0);
            cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0, 0);
            cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0, 0);
            cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0, 0);
812
        }
813
        ss = (ss & ~3) | dpl;
814
        cpu_x86_load_seg_cache(env, R_SS, ss,
815 816
                               ssp, get_seg_limit(ss_e1, ss_e2), ss_e2);
    }
817
    SET_ESP(esp, sp_mask);
818 819

    selector = (selector & ~3) | dpl;
820
    cpu_x86_load_seg_cache(env, R_CS, selector,
821 822 823 824 825 826
                   get_seg_base(e1, e2),
                   get_seg_limit(e1, e2),
                   e2);
    cpu_x86_set_cpl(env, dpl);
    env->eip = offset;

B
bellard 已提交
827 828 829 830 831 832 833
    /* interrupt gate clear IF mask */
    if ((type & 1) == 0) {
        env->eflags &= ~IF_MASK;
    }
    env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
}

B
bellard 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
#ifdef TARGET_X86_64

#define PUSHQ(sp, val)\
{\
    sp -= 8;\
    stq_kernel(sp, (val));\
}

#define POPQ(sp, val)\
{\
    val = ldq_kernel(sp);\
    sp += 8;\
}

static inline target_ulong get_rsp_from_tss(int level)
{
    int index;
851

B
bellard 已提交
852
#if 0
853
    printf("TR: base=" TARGET_FMT_lx " limit=%x\n",
B
bellard 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
           env->tr.base, env->tr.limit);
#endif

    if (!(env->tr.flags & DESC_P_MASK))
        cpu_abort(env, "invalid tss");
    index = 8 * level + 4;
    if ((index + 7) > env->tr.limit)
        raise_exception_err(EXCP0A_TSS, env->tr.selector & 0xfffc);
    return ldq_kernel(env->tr.base + index);
}

/* 64 bit interrupt */
static void do_interrupt64(int intno, int is_int, int error_code,
                           target_ulong next_eip, int is_hw)
{
    SegmentCache *dt;
    target_ulong ptr;
    int type, dpl, selector, cpl, ist;
    int has_error_code, new_stack;
    uint32_t e1, e2, e3, ss;
    target_ulong old_eip, esp, offset;
T
ths 已提交
875
    int svm_should_check = 1;
B
bellard 已提交
876

T
ths 已提交
877 878 879 880 881 882 883 884 885
    if ((env->intercept & INTERCEPT_SVM_MASK) && !is_int && next_eip==-1) {
        next_eip = EIP;
        svm_should_check = 0;
    }
    if (svm_should_check
        && INTERCEPTEDl(_exceptions, 1 << intno)
        && !is_int) {
        raise_interrupt(intno, is_int, error_code, 0);
    }
B
bellard 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
    has_error_code = 0;
    if (!is_int && !is_hw) {
        switch(intno) {
        case 8:
        case 10:
        case 11:
        case 12:
        case 13:
        case 14:
        case 17:
            has_error_code = 1;
            break;
        }
    }
    if (is_int)
        old_eip = next_eip;
    else
        old_eip = env->eip;

    dt = &env->idt;
    if (intno * 16 + 15 > dt->limit)
        raise_exception_err(EXCP0D_GPF, intno * 16 + 2);
    ptr = dt->base + intno * 16;
    e1 = ldl_kernel(ptr);
    e2 = ldl_kernel(ptr + 4);
    e3 = ldl_kernel(ptr + 8);
    /* check gate type */
    type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
    switch(type) {
    case 14: /* 386 interrupt gate */
    case 15: /* 386 trap gate */
        break;
    default:
        raise_exception_err(EXCP0D_GPF, intno * 16 + 2);
        break;
    }
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    cpl = env->hflags & HF_CPL_MASK;
    /* check privledge if software int */
    if (is_int && dpl < cpl)
        raise_exception_err(EXCP0D_GPF, intno * 16 + 2);
    /* check valid bit */
    if (!(e2 & DESC_P_MASK))
        raise_exception_err(EXCP0B_NOSEG, intno * 16 + 2);
    selector = e1 >> 16;
    offset = ((target_ulong)e3 << 32) | (e2 & 0xffff0000) | (e1 & 0x0000ffff);
    ist = e2 & 7;
    if ((selector & 0xfffc) == 0)
        raise_exception_err(EXCP0D_GPF, 0);

    if (load_segment(&e1, &e2, selector) != 0)
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
    if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    if (dpl > cpl)
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
    if (!(e2 & DESC_P_MASK))
        raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
    if (!(e2 & DESC_L_MASK) || (e2 & DESC_B_MASK))
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
    if ((!(e2 & DESC_C_MASK) && dpl < cpl) || ist != 0) {
B
blueswir1 已提交
948
        /* to inner privilege */
B
bellard 已提交
949 950 951 952
        if (ist != 0)
            esp = get_rsp_from_tss(ist + 3);
        else
            esp = get_rsp_from_tss(dpl);
953
        esp &= ~0xfLL; /* align stack */
B
bellard 已提交
954 955 956
        ss = 0;
        new_stack = 1;
    } else if ((e2 & DESC_C_MASK) || dpl == cpl) {
B
blueswir1 已提交
957
        /* to same privilege */
B
bellard 已提交
958 959 960
        if (env->eflags & VM_MASK)
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        new_stack = 0;
961 962 963 964 965
        if (ist != 0)
            esp = get_rsp_from_tss(ist + 3);
        else
            esp = ESP;
        esp &= ~0xfLL; /* align stack */
B
bellard 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
        dpl = cpl;
    } else {
        raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        new_stack = 0; /* avoid warning */
        esp = 0; /* avoid warning */
    }

    PUSHQ(esp, env->segs[R_SS].selector);
    PUSHQ(esp, ESP);
    PUSHQ(esp, compute_eflags());
    PUSHQ(esp, env->segs[R_CS].selector);
    PUSHQ(esp, old_eip);
    if (has_error_code) {
        PUSHQ(esp, error_code);
    }
981

B
bellard 已提交
982 983 984 985 986 987 988
    if (new_stack) {
        ss = 0 | dpl;
        cpu_x86_load_seg_cache(env, R_SS, ss, 0, 0, 0);
    }
    ESP = esp;

    selector = (selector & ~3) | dpl;
989
    cpu_x86_load_seg_cache(env, R_CS, selector,
B
bellard 已提交
990 991 992 993 994 995 996 997 998 999 1000 1001
                   get_seg_base(e1, e2),
                   get_seg_limit(e1, e2),
                   e2);
    cpu_x86_set_cpl(env, dpl);
    env->eip = offset;

    /* interrupt gate clear IF mask */
    if ((type & 1) == 0) {
        env->eflags &= ~IF_MASK;
    }
    env->eflags &= ~(TF_MASK | VM_MASK | RF_MASK | NT_MASK);
}
1002
#endif
B
bellard 已提交
1003

B
bellard 已提交
1004 1005 1006 1007 1008 1009 1010 1011
#if defined(CONFIG_USER_ONLY)
void helper_syscall(int next_eip_addend)
{
    env->exception_index = EXCP_SYSCALL;
    env->exception_next_eip = env->eip + next_eip_addend;
    cpu_loop_exit();
}
#else
B
bellard 已提交
1012
void helper_syscall(int next_eip_addend)
B
bellard 已提交
1013 1014 1015 1016 1017 1018 1019
{
    int selector;

    if (!(env->efer & MSR_EFER_SCE)) {
        raise_exception_err(EXCP06_ILLOP, 0);
    }
    selector = (env->star >> 32) & 0xffff;
1020
#ifdef TARGET_X86_64
B
bellard 已提交
1021
    if (env->hflags & HF_LMA_MASK) {
1022 1023
        int code64;

B
bellard 已提交
1024
        ECX = env->eip + next_eip_addend;
B
bellard 已提交
1025
        env->regs[11] = compute_eflags();
1026

1027
        code64 = env->hflags & HF_CS64_MASK;
B
bellard 已提交
1028 1029

        cpu_x86_set_cpl(env, 0);
1030 1031
        cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
                           0, 0xffffffff,
1032
                               DESC_G_MASK | DESC_P_MASK |
B
bellard 已提交
1033 1034
                               DESC_S_MASK |
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK | DESC_L_MASK);
1035
        cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
B
bellard 已提交
1036 1037 1038 1039 1040
                               0, 0xffffffff,
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                               DESC_S_MASK |
                               DESC_W_MASK | DESC_A_MASK);
        env->eflags &= ~env->fmask;
1041
        load_eflags(env->eflags, 0);
1042
        if (code64)
B
bellard 已提交
1043 1044 1045
            env->eip = env->lstar;
        else
            env->eip = env->cstar;
1046
    } else
1047 1048
#endif
    {
B
bellard 已提交
1049
        ECX = (uint32_t)(env->eip + next_eip_addend);
1050

B
bellard 已提交
1051
        cpu_x86_set_cpl(env, 0);
1052 1053
        cpu_x86_load_seg_cache(env, R_CS, selector & 0xfffc,
                           0, 0xffffffff,
B
bellard 已提交
1054 1055 1056
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                               DESC_S_MASK |
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
1057
        cpu_x86_load_seg_cache(env, R_SS, (selector + 8) & 0xfffc,
B
bellard 已提交
1058 1059 1060 1061 1062 1063 1064 1065
                               0, 0xffffffff,
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                               DESC_S_MASK |
                               DESC_W_MASK | DESC_A_MASK);
        env->eflags &= ~(IF_MASK | RF_MASK | VM_MASK);
        env->eip = (uint32_t)env->star;
    }
}
B
bellard 已提交
1066
#endif
B
bellard 已提交
1067 1068 1069 1070 1071

void helper_sysret(int dflag)
{
    int cpl, selector;

1072 1073 1074
    if (!(env->efer & MSR_EFER_SCE)) {
        raise_exception_err(EXCP06_ILLOP, 0);
    }
B
bellard 已提交
1075 1076 1077 1078 1079
    cpl = env->hflags & HF_CPL_MASK;
    if (!(env->cr[0] & CR0_PE_MASK) || cpl != 0) {
        raise_exception_err(EXCP0D_GPF, 0);
    }
    selector = (env->star >> 48) & 0xffff;
1080
#ifdef TARGET_X86_64
B
bellard 已提交
1081 1082
    if (env->hflags & HF_LMA_MASK) {
        if (dflag == 2) {
1083 1084
            cpu_x86_load_seg_cache(env, R_CS, (selector + 16) | 3,
                                   0, 0xffffffff,
1085
                                   DESC_G_MASK | DESC_P_MASK |
B
bellard 已提交
1086
                                   DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
1087
                                   DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK |
B
bellard 已提交
1088 1089 1090
                                   DESC_L_MASK);
            env->eip = ECX;
        } else {
1091 1092
            cpu_x86_load_seg_cache(env, R_CS, selector | 3,
                                   0, 0xffffffff,
B
bellard 已提交
1093 1094 1095 1096 1097
                                   DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                                   DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
                                   DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
            env->eip = (uint32_t)ECX;
        }
1098
        cpu_x86_load_seg_cache(env, R_SS, selector + 8,
B
bellard 已提交
1099 1100 1101 1102
                               0, 0xffffffff,
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
                               DESC_W_MASK | DESC_A_MASK);
1103
        load_eflags((uint32_t)(env->regs[11]), TF_MASK | AC_MASK | ID_MASK |
B
bellard 已提交
1104
                    IF_MASK | IOPL_MASK | VM_MASK | RF_MASK | NT_MASK);
B
bellard 已提交
1105
        cpu_x86_set_cpl(env, 3);
1106
    } else
1107 1108
#endif
    {
1109 1110
        cpu_x86_load_seg_cache(env, R_CS, selector | 3,
                               0, 0xffffffff,
B
bellard 已提交
1111 1112 1113 1114
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
                               DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
        env->eip = (uint32_t)ECX;
1115
        cpu_x86_load_seg_cache(env, R_SS, selector + 8,
B
bellard 已提交
1116 1117 1118 1119 1120 1121 1122
                               0, 0xffffffff,
                               DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                               DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
                               DESC_W_MASK | DESC_A_MASK);
        env->eflags |= IF_MASK;
        cpu_x86_set_cpl(env, 3);
    }
1123 1124 1125 1126 1127 1128 1129
#ifdef USE_KQEMU
    if (kqemu_is_ok(env)) {
        if (env->hflags & HF_LMA_MASK)
            CC_OP = CC_OP_EFLAGS;
        env->exception_index = -1;
        cpu_loop_exit();
    }
B
bellard 已提交
1130
#endif
1131
}
B
bellard 已提交
1132

B
bellard 已提交
1133 1134
/* real mode interrupt */
static void do_interrupt_real(int intno, int is_int, int error_code,
B
bellard 已提交
1135
                              unsigned int next_eip)
B
bellard 已提交
1136 1137
{
    SegmentCache *dt;
B
bellard 已提交
1138
    target_ulong ptr, ssp;
B
bellard 已提交
1139 1140 1141
    int selector;
    uint32_t offset, esp;
    uint32_t old_cs, old_eip;
T
ths 已提交
1142
    int svm_should_check = 1;
B
bellard 已提交
1143

T
ths 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152
    if ((env->intercept & INTERCEPT_SVM_MASK) && !is_int && next_eip==-1) {
        next_eip = EIP;
        svm_should_check = 0;
    }
    if (svm_should_check
        && INTERCEPTEDl(_exceptions, 1 << intno)
        && !is_int) {
        raise_interrupt(intno, is_int, error_code, 0);
    }
B
bellard 已提交
1153 1154 1155 1156 1157
    /* real mode (simpler !) */
    dt = &env->idt;
    if (intno * 4 + 3 > dt->limit)
        raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
    ptr = dt->base + intno * 4;
B
bellard 已提交
1158 1159
    offset = lduw_kernel(ptr);
    selector = lduw_kernel(ptr + 2);
B
bellard 已提交
1160 1161 1162 1163 1164 1165 1166
    esp = ESP;
    ssp = env->segs[R_SS].base;
    if (is_int)
        old_eip = next_eip;
    else
        old_eip = env->eip;
    old_cs = env->segs[R_CS].selector;
1167 1168 1169 1170
    /* XXX: use SS segment size ? */
    PUSHW(ssp, esp, 0xffff, compute_eflags());
    PUSHW(ssp, esp, 0xffff, old_cs);
    PUSHW(ssp, esp, 0xffff, old_eip);
1171

B
bellard 已提交
1172 1173 1174 1175
    /* update processor state */
    ESP = (ESP & ~0xffff) | (esp & 0xffff);
    env->eip = offset;
    env->segs[R_CS].selector = selector;
B
bellard 已提交
1176
    env->segs[R_CS].base = (selector << 4);
B
bellard 已提交
1177 1178 1179 1180
    env->eflags &= ~(IF_MASK | TF_MASK | AC_MASK | RF_MASK);
}

/* fake user mode interrupt */
1181
void do_interrupt_user(int intno, int is_int, int error_code,
B
bellard 已提交
1182
                       target_ulong next_eip)
B
bellard 已提交
1183 1184
{
    SegmentCache *dt;
B
bellard 已提交
1185
    target_ulong ptr;
B
bellard 已提交
1186
    int dpl, cpl, shift;
B
bellard 已提交
1187 1188 1189
    uint32_t e2;

    dt = &env->idt;
B
bellard 已提交
1190 1191 1192 1193 1194 1195
    if (env->hflags & HF_LMA_MASK) {
        shift = 4;
    } else {
        shift = 3;
    }
    ptr = dt->base + (intno << shift);
B
bellard 已提交
1196
    e2 = ldl_kernel(ptr + 4);
1197

B
bellard 已提交
1198 1199 1200 1201
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    cpl = env->hflags & HF_CPL_MASK;
    /* check privledge if software int */
    if (is_int && dpl < cpl)
B
bellard 已提交
1202
        raise_exception_err(EXCP0D_GPF, (intno << shift) + 2);
B
bellard 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211

    /* Since we emulate only user space, we cannot do more than
       exiting the emulation with the suitable exception and error
       code */
    if (is_int)
        EIP = next_eip;
}

/*
B
bellard 已提交
1212
 * Begin execution of an interruption. is_int is TRUE if coming from
B
bellard 已提交
1213
 * the int instruction. next_eip is the EIP value AFTER the interrupt
1214
 * instruction. It is only relevant if is_int is TRUE.
B
bellard 已提交
1215
 */
1216
void do_interrupt(int intno, int is_int, int error_code,
B
bellard 已提交
1217
                  target_ulong next_eip, int is_hw)
B
bellard 已提交
1218
{
B
bellard 已提交
1219
    if (loglevel & CPU_LOG_INT) {
B
bellard 已提交
1220 1221
        if ((env->cr[0] & CR0_PE_MASK)) {
            static int count;
B
bellard 已提交
1222
            fprintf(logfile, "%6d: v=%02x e=%04x i=%d cpl=%d IP=%04x:" TARGET_FMT_lx " pc=" TARGET_FMT_lx " SP=%04x:" TARGET_FMT_lx,
1223 1224 1225
                    count, intno, error_code, is_int,
                    env->hflags & HF_CPL_MASK,
                    env->segs[R_CS].selector, EIP,
B
bellard 已提交
1226
                    (int)env->segs[R_CS].base + EIP,
1227 1228
                    env->segs[R_SS].selector, ESP);
            if (intno == 0x0e) {
B
bellard 已提交
1229
                fprintf(logfile, " CR2=" TARGET_FMT_lx, env->cr[2]);
1230
            } else {
B
bellard 已提交
1231
                fprintf(logfile, " EAX=" TARGET_FMT_lx, EAX);
1232
            }
B
bellard 已提交
1233
            fprintf(logfile, "\n");
B
bellard 已提交
1234
            cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
B
bellard 已提交
1235
#if 0
B
bellard 已提交
1236 1237 1238 1239 1240 1241 1242
            {
                int i;
                uint8_t *ptr;
                fprintf(logfile, "       code=");
                ptr = env->segs[R_CS].base + env->eip;
                for(i = 0; i < 16; i++) {
                    fprintf(logfile, " %02x", ldub(ptr + i));
1243
                }
B
bellard 已提交
1244
                fprintf(logfile, "\n");
1245
            }
1246
#endif
B
bellard 已提交
1247
            count++;
B
bellard 已提交
1248 1249
        }
    }
B
bellard 已提交
1250
    if (env->cr[0] & CR0_PE_MASK) {
B
bellard 已提交
1251 1252 1253 1254 1255 1256 1257 1258
#if TARGET_X86_64
        if (env->hflags & HF_LMA_MASK) {
            do_interrupt64(intno, is_int, error_code, next_eip, is_hw);
        } else
#endif
        {
            do_interrupt_protected(intno, is_int, error_code, next_eip, is_hw);
        }
B
bellard 已提交
1259 1260 1261 1262 1263
    } else {
        do_interrupt_real(intno, is_int, error_code, next_eip);
    }
}

1264 1265 1266 1267 1268
/*
 * Check nested exceptions and change to double or triple fault if
 * needed. It should only be called, if this is not an interrupt.
 * Returns the new exception number.
 */
1269
static int check_exception(int intno, int *error_code)
1270
{
B
bellard 已提交
1271
    int first_contributory = env->old_exception == 0 ||
1272 1273
                              (env->old_exception >= 10 &&
                               env->old_exception <= 13);
B
bellard 已提交
1274
    int second_contributory = intno == 0 ||
1275 1276 1277
                               (intno >= 10 && intno <= 13);

    if (loglevel & CPU_LOG_INT)
B
bellard 已提交
1278
        fprintf(logfile, "check_exception old: 0x%x new 0x%x\n",
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
                env->old_exception, intno);

    if (env->old_exception == EXCP08_DBLE)
        cpu_abort(env, "triple fault");

    if ((first_contributory && second_contributory)
        || (env->old_exception == EXCP0E_PAGE &&
            (second_contributory || (intno == EXCP0E_PAGE)))) {
        intno = EXCP08_DBLE;
        *error_code = 0;
    }

    if (second_contributory || (intno == EXCP0E_PAGE) ||
        (intno == EXCP08_DBLE))
        env->old_exception = intno;

    return intno;
}

B
bellard 已提交
1298 1299 1300 1301
/*
 * Signal an interruption. It is executed in the main CPU loop.
 * is_int is TRUE if coming from the int instruction. next_eip is the
 * EIP value AFTER the interrupt instruction. It is only relevant if
1302
 * is_int is TRUE.
B
bellard 已提交
1303
 */
1304
void raise_interrupt(int intno, int is_int, int error_code,
1305
                     int next_eip_addend)
B
bellard 已提交
1306
{
T
ths 已提交
1307
    if (!is_int) {
1308
        helper_svm_check_intercept_param(SVM_EXIT_EXCP_BASE + intno, error_code);
1309
        intno = check_exception(intno, &error_code);
T
ths 已提交
1310
    }
1311

B
bellard 已提交
1312 1313 1314
    env->exception_index = intno;
    env->error_code = error_code;
    env->exception_is_int = is_int;
1315
    env->exception_next_eip = env->eip + next_eip_addend;
B
bellard 已提交
1316 1317 1318
    cpu_loop_exit();
}

B
bellard 已提交
1319 1320 1321
/* same as raise_exception_err, but do not restore global registers */
static void raise_exception_err_norestore(int exception_index, int error_code)
{
1322 1323
    exception_index = check_exception(exception_index, &error_code);

B
bellard 已提交
1324 1325 1326 1327 1328 1329 1330
    env->exception_index = exception_index;
    env->error_code = error_code;
    env->exception_is_int = 0;
    env->exception_next_eip = 0;
    longjmp(env->jmp_env, 1);
}

B
bellard 已提交
1331
/* shortcuts to generate exceptions */
1332 1333

void (raise_exception_err)(int exception_index, int error_code)
B
bellard 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342
{
    raise_interrupt(exception_index, 0, error_code, 0);
}

void raise_exception(int exception_index)
{
    raise_interrupt(exception_index, 0, 0, 0);
}

B
bellard 已提交
1343 1344
/* SMM support */

1345
#if defined(CONFIG_USER_ONLY)
B
bellard 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356

void do_smm_enter(void)
{
}

void helper_rsm(void)
{
}

#else

B
bellard 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
#ifdef TARGET_X86_64
#define SMM_REVISION_ID 0x00020064
#else
#define SMM_REVISION_ID 0x00020000
#endif

void do_smm_enter(void)
{
    target_ulong sm_state;
    SegmentCache *dt;
    int i, offset;

    if (loglevel & CPU_LOG_INT) {
        fprintf(logfile, "SMM: enter\n");
        cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
    }

    env->hflags |= HF_SMM_MASK;
    cpu_smm_update(env);

    sm_state = env->smbase + 0x8000;
1378

B
bellard 已提交
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
#ifdef TARGET_X86_64
    for(i = 0; i < 6; i++) {
        dt = &env->segs[i];
        offset = 0x7e00 + i * 16;
        stw_phys(sm_state + offset, dt->selector);
        stw_phys(sm_state + offset + 2, (dt->flags >> 8) & 0xf0ff);
        stl_phys(sm_state + offset + 4, dt->limit);
        stq_phys(sm_state + offset + 8, dt->base);
    }

    stq_phys(sm_state + 0x7e68, env->gdt.base);
    stl_phys(sm_state + 0x7e64, env->gdt.limit);

    stw_phys(sm_state + 0x7e70, env->ldt.selector);
    stq_phys(sm_state + 0x7e78, env->ldt.base);
    stl_phys(sm_state + 0x7e74, env->ldt.limit);
    stw_phys(sm_state + 0x7e72, (env->ldt.flags >> 8) & 0xf0ff);
1396

B
bellard 已提交
1397 1398 1399 1400 1401 1402 1403
    stq_phys(sm_state + 0x7e88, env->idt.base);
    stl_phys(sm_state + 0x7e84, env->idt.limit);

    stw_phys(sm_state + 0x7e90, env->tr.selector);
    stq_phys(sm_state + 0x7e98, env->tr.base);
    stl_phys(sm_state + 0x7e94, env->tr.limit);
    stw_phys(sm_state + 0x7e92, (env->tr.flags >> 8) & 0xf0ff);
1404

B
bellard 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
    stq_phys(sm_state + 0x7ed0, env->efer);

    stq_phys(sm_state + 0x7ff8, EAX);
    stq_phys(sm_state + 0x7ff0, ECX);
    stq_phys(sm_state + 0x7fe8, EDX);
    stq_phys(sm_state + 0x7fe0, EBX);
    stq_phys(sm_state + 0x7fd8, ESP);
    stq_phys(sm_state + 0x7fd0, EBP);
    stq_phys(sm_state + 0x7fc8, ESI);
    stq_phys(sm_state + 0x7fc0, EDI);
1415
    for(i = 8; i < 16; i++)
B
bellard 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
        stq_phys(sm_state + 0x7ff8 - i * 8, env->regs[i]);
    stq_phys(sm_state + 0x7f78, env->eip);
    stl_phys(sm_state + 0x7f70, compute_eflags());
    stl_phys(sm_state + 0x7f68, env->dr[6]);
    stl_phys(sm_state + 0x7f60, env->dr[7]);

    stl_phys(sm_state + 0x7f48, env->cr[4]);
    stl_phys(sm_state + 0x7f50, env->cr[3]);
    stl_phys(sm_state + 0x7f58, env->cr[0]);

    stl_phys(sm_state + 0x7efc, SMM_REVISION_ID);
    stl_phys(sm_state + 0x7f00, env->smbase);
#else
    stl_phys(sm_state + 0x7ffc, env->cr[0]);
    stl_phys(sm_state + 0x7ff8, env->cr[3]);
    stl_phys(sm_state + 0x7ff4, compute_eflags());
    stl_phys(sm_state + 0x7ff0, env->eip);
    stl_phys(sm_state + 0x7fec, EDI);
    stl_phys(sm_state + 0x7fe8, ESI);
    stl_phys(sm_state + 0x7fe4, EBP);
    stl_phys(sm_state + 0x7fe0, ESP);
    stl_phys(sm_state + 0x7fdc, EBX);
    stl_phys(sm_state + 0x7fd8, EDX);
    stl_phys(sm_state + 0x7fd4, ECX);
    stl_phys(sm_state + 0x7fd0, EAX);
    stl_phys(sm_state + 0x7fcc, env->dr[6]);
    stl_phys(sm_state + 0x7fc8, env->dr[7]);
1443

B
bellard 已提交
1444 1445 1446 1447
    stl_phys(sm_state + 0x7fc4, env->tr.selector);
    stl_phys(sm_state + 0x7f64, env->tr.base);
    stl_phys(sm_state + 0x7f60, env->tr.limit);
    stl_phys(sm_state + 0x7f5c, (env->tr.flags >> 8) & 0xf0ff);
1448

B
bellard 已提交
1449 1450 1451 1452
    stl_phys(sm_state + 0x7fc0, env->ldt.selector);
    stl_phys(sm_state + 0x7f80, env->ldt.base);
    stl_phys(sm_state + 0x7f7c, env->ldt.limit);
    stl_phys(sm_state + 0x7f78, (env->ldt.flags >> 8) & 0xf0ff);
1453

B
bellard 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
    stl_phys(sm_state + 0x7f74, env->gdt.base);
    stl_phys(sm_state + 0x7f70, env->gdt.limit);

    stl_phys(sm_state + 0x7f58, env->idt.base);
    stl_phys(sm_state + 0x7f54, env->idt.limit);

    for(i = 0; i < 6; i++) {
        dt = &env->segs[i];
        if (i < 3)
            offset = 0x7f84 + i * 12;
        else
            offset = 0x7f2c + (i - 3) * 12;
        stl_phys(sm_state + 0x7fa8 + i * 4, dt->selector);
        stl_phys(sm_state + offset + 8, dt->base);
        stl_phys(sm_state + offset + 4, dt->limit);
        stl_phys(sm_state + offset, (dt->flags >> 8) & 0xf0ff);
    }
    stl_phys(sm_state + 0x7f14, env->cr[4]);

    stl_phys(sm_state + 0x7efc, SMM_REVISION_ID);
    stl_phys(sm_state + 0x7ef8, env->smbase);
#endif
    /* init SMM cpu state */

B
bellard 已提交
1478 1479 1480 1481
#ifdef TARGET_X86_64
    env->efer = 0;
    env->hflags &= ~HF_LMA_MASK;
#endif
B
bellard 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490
    load_eflags(0, ~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK));
    env->eip = 0x00008000;
    cpu_x86_load_seg_cache(env, R_CS, (env->smbase >> 4) & 0xffff, env->smbase,
                           0xffffffff, 0);
    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffffffff, 0);
    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffffffff, 0);
    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffffffff, 0);
    cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffffffff, 0);
    cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffffffff, 0);
1491

1492
    cpu_x86_update_cr0(env,
B
bellard 已提交
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
                       env->cr[0] & ~(CR0_PE_MASK | CR0_EM_MASK | CR0_TS_MASK | CR0_PG_MASK));
    cpu_x86_update_cr4(env, 0);
    env->dr[7] = 0x00000400;
    CC_OP = CC_OP_EFLAGS;
}

void helper_rsm(void)
{
    target_ulong sm_state;
    int i, offset;
    uint32_t val;

    sm_state = env->smbase + 0x8000;
#ifdef TARGET_X86_64
B
bellard 已提交
1507 1508 1509 1510 1511 1512
    env->efer = ldq_phys(sm_state + 0x7ed0);
    if (env->efer & MSR_EFER_LMA)
        env->hflags |= HF_LMA_MASK;
    else
        env->hflags &= ~HF_LMA_MASK;

B
bellard 已提交
1513 1514
    for(i = 0; i < 6; i++) {
        offset = 0x7e00 + i * 16;
1515
        cpu_x86_load_seg_cache(env, i,
B
bellard 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
                               lduw_phys(sm_state + offset),
                               ldq_phys(sm_state + offset + 8),
                               ldl_phys(sm_state + offset + 4),
                               (lduw_phys(sm_state + offset + 2) & 0xf0ff) << 8);
    }

    env->gdt.base = ldq_phys(sm_state + 0x7e68);
    env->gdt.limit = ldl_phys(sm_state + 0x7e64);

    env->ldt.selector = lduw_phys(sm_state + 0x7e70);
    env->ldt.base = ldq_phys(sm_state + 0x7e78);
    env->ldt.limit = ldl_phys(sm_state + 0x7e74);
    env->ldt.flags = (lduw_phys(sm_state + 0x7e72) & 0xf0ff) << 8;
1529

B
bellard 已提交
1530 1531 1532 1533 1534 1535 1536
    env->idt.base = ldq_phys(sm_state + 0x7e88);
    env->idt.limit = ldl_phys(sm_state + 0x7e84);

    env->tr.selector = lduw_phys(sm_state + 0x7e90);
    env->tr.base = ldq_phys(sm_state + 0x7e98);
    env->tr.limit = ldl_phys(sm_state + 0x7e94);
    env->tr.flags = (lduw_phys(sm_state + 0x7e92) & 0xf0ff) << 8;
1537

B
bellard 已提交
1538 1539 1540 1541 1542 1543 1544 1545
    EAX = ldq_phys(sm_state + 0x7ff8);
    ECX = ldq_phys(sm_state + 0x7ff0);
    EDX = ldq_phys(sm_state + 0x7fe8);
    EBX = ldq_phys(sm_state + 0x7fe0);
    ESP = ldq_phys(sm_state + 0x7fd8);
    EBP = ldq_phys(sm_state + 0x7fd0);
    ESI = ldq_phys(sm_state + 0x7fc8);
    EDI = ldq_phys(sm_state + 0x7fc0);
1546
    for(i = 8; i < 16; i++)
B
bellard 已提交
1547 1548
        env->regs[i] = ldq_phys(sm_state + 0x7ff8 - i * 8);
    env->eip = ldq_phys(sm_state + 0x7f78);
1549
    load_eflags(ldl_phys(sm_state + 0x7f70),
B
bellard 已提交
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
                ~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK));
    env->dr[6] = ldl_phys(sm_state + 0x7f68);
    env->dr[7] = ldl_phys(sm_state + 0x7f60);

    cpu_x86_update_cr4(env, ldl_phys(sm_state + 0x7f48));
    cpu_x86_update_cr3(env, ldl_phys(sm_state + 0x7f50));
    cpu_x86_update_cr0(env, ldl_phys(sm_state + 0x7f58));

    val = ldl_phys(sm_state + 0x7efc); /* revision ID */
    if (val & 0x20000) {
        env->smbase = ldl_phys(sm_state + 0x7f00) & ~0x7fff;
    }
#else
    cpu_x86_update_cr0(env, ldl_phys(sm_state + 0x7ffc));
    cpu_x86_update_cr3(env, ldl_phys(sm_state + 0x7ff8));
1565
    load_eflags(ldl_phys(sm_state + 0x7ff4),
B
bellard 已提交
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
                ~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK));
    env->eip = ldl_phys(sm_state + 0x7ff0);
    EDI = ldl_phys(sm_state + 0x7fec);
    ESI = ldl_phys(sm_state + 0x7fe8);
    EBP = ldl_phys(sm_state + 0x7fe4);
    ESP = ldl_phys(sm_state + 0x7fe0);
    EBX = ldl_phys(sm_state + 0x7fdc);
    EDX = ldl_phys(sm_state + 0x7fd8);
    ECX = ldl_phys(sm_state + 0x7fd4);
    EAX = ldl_phys(sm_state + 0x7fd0);
    env->dr[6] = ldl_phys(sm_state + 0x7fcc);
    env->dr[7] = ldl_phys(sm_state + 0x7fc8);
1578

B
bellard 已提交
1579 1580 1581 1582
    env->tr.selector = ldl_phys(sm_state + 0x7fc4) & 0xffff;
    env->tr.base = ldl_phys(sm_state + 0x7f64);
    env->tr.limit = ldl_phys(sm_state + 0x7f60);
    env->tr.flags = (ldl_phys(sm_state + 0x7f5c) & 0xf0ff) << 8;
1583

B
bellard 已提交
1584 1585 1586 1587
    env->ldt.selector = ldl_phys(sm_state + 0x7fc0) & 0xffff;
    env->ldt.base = ldl_phys(sm_state + 0x7f80);
    env->ldt.limit = ldl_phys(sm_state + 0x7f7c);
    env->ldt.flags = (ldl_phys(sm_state + 0x7f78) & 0xf0ff) << 8;
1588

B
bellard 已提交
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
    env->gdt.base = ldl_phys(sm_state + 0x7f74);
    env->gdt.limit = ldl_phys(sm_state + 0x7f70);

    env->idt.base = ldl_phys(sm_state + 0x7f58);
    env->idt.limit = ldl_phys(sm_state + 0x7f54);

    for(i = 0; i < 6; i++) {
        if (i < 3)
            offset = 0x7f84 + i * 12;
        else
            offset = 0x7f2c + (i - 3) * 12;
1600
        cpu_x86_load_seg_cache(env, i,
B
bellard 已提交
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
                               ldl_phys(sm_state + 0x7fa8 + i * 4) & 0xffff,
                               ldl_phys(sm_state + offset + 8),
                               ldl_phys(sm_state + offset + 4),
                               (ldl_phys(sm_state + offset) & 0xf0ff) << 8);
    }
    cpu_x86_update_cr4(env, ldl_phys(sm_state + 0x7f14));

    val = ldl_phys(sm_state + 0x7efc); /* revision ID */
    if (val & 0x20000) {
        env->smbase = ldl_phys(sm_state + 0x7ef8) & ~0x7fff;
    }
#endif
    CC_OP = CC_OP_EFLAGS;
    env->hflags &= ~HF_SMM_MASK;
    cpu_smm_update(env);

    if (loglevel & CPU_LOG_INT) {
        fprintf(logfile, "SMM: after RSM\n");
        cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
    }
}

B
bellard 已提交
1623 1624 1625
#endif /* !CONFIG_USER_ONLY */


B
bellard 已提交
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
/* division, flags are undefined */

void helper_divb_AL(target_ulong t0)
{
    unsigned int num, den, q, r;

    num = (EAX & 0xffff);
    den = (t0 & 0xff);
    if (den == 0) {
        raise_exception(EXCP00_DIVZ);
    }
    q = (num / den);
    if (q > 0xff)
        raise_exception(EXCP00_DIVZ);
    q &= 0xff;
    r = (num % den) & 0xff;
    EAX = (EAX & ~0xffff) | (r << 8) | q;
}

void helper_idivb_AL(target_ulong t0)
{
    int num, den, q, r;

    num = (int16_t)EAX;
    den = (int8_t)t0;
    if (den == 0) {
        raise_exception(EXCP00_DIVZ);
    }
    q = (num / den);
    if (q != (int8_t)q)
        raise_exception(EXCP00_DIVZ);
    q &= 0xff;
    r = (num % den) & 0xff;
    EAX = (EAX & ~0xffff) | (r << 8) | q;
}

void helper_divw_AX(target_ulong t0)
{
    unsigned int num, den, q, r;

    num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
    den = (t0 & 0xffff);
    if (den == 0) {
        raise_exception(EXCP00_DIVZ);
    }
    q = (num / den);
    if (q > 0xffff)
        raise_exception(EXCP00_DIVZ);
    q &= 0xffff;
    r = (num % den) & 0xffff;
    EAX = (EAX & ~0xffff) | q;
    EDX = (EDX & ~0xffff) | r;
}

void helper_idivw_AX(target_ulong t0)
{
    int num, den, q, r;

    num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
    den = (int16_t)t0;
    if (den == 0) {
        raise_exception(EXCP00_DIVZ);
    }
    q = (num / den);
    if (q != (int16_t)q)
        raise_exception(EXCP00_DIVZ);
    q &= 0xffff;
    r = (num % den) & 0xffff;
    EAX = (EAX & ~0xffff) | q;
    EDX = (EDX & ~0xffff) | r;
}

void helper_divl_EAX(target_ulong t0)
B
bellard 已提交
1699
{
B
bellard 已提交
1700 1701
    unsigned int den, r;
    uint64_t num, q;
1702

B
bellard 已提交
1703
    num = ((uint32_t)EAX) | ((uint64_t)((uint32_t)EDX) << 32);
B
bellard 已提交
1704
    den = t0;
B
bellard 已提交
1705 1706 1707 1708 1709
    if (den == 0) {
        raise_exception(EXCP00_DIVZ);
    }
    q = (num / den);
    r = (num % den);
B
bellard 已提交
1710 1711
    if (q > 0xffffffff)
        raise_exception(EXCP00_DIVZ);
B
bellard 已提交
1712 1713
    EAX = (uint32_t)q;
    EDX = (uint32_t)r;
B
bellard 已提交
1714 1715
}

B
bellard 已提交
1716
void helper_idivl_EAX(target_ulong t0)
B
bellard 已提交
1717
{
B
bellard 已提交
1718 1719
    int den, r;
    int64_t num, q;
1720

B
bellard 已提交
1721
    num = ((uint32_t)EAX) | ((uint64_t)((uint32_t)EDX) << 32);
B
bellard 已提交
1722
    den = t0;
B
bellard 已提交
1723 1724 1725 1726 1727
    if (den == 0) {
        raise_exception(EXCP00_DIVZ);
    }
    q = (num / den);
    r = (num % den);
B
bellard 已提交
1728 1729
    if (q != (int32_t)q)
        raise_exception(EXCP00_DIVZ);
B
bellard 已提交
1730 1731
    EAX = (uint32_t)q;
    EDX = (uint32_t)r;
B
bellard 已提交
1732 1733
}

B
bellard 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
/* bcd */

/* XXX: exception */
void helper_aam(int base)
{
    int al, ah;
    al = EAX & 0xff;
    ah = al / base;
    al = al % base;
    EAX = (EAX & ~0xffff) | al | (ah << 8);
    CC_DST = al;
}

void helper_aad(int base)
{
    int al, ah;
    al = EAX & 0xff;
    ah = (EAX >> 8) & 0xff;
    al = ((ah * base) + al) & 0xff;
    EAX = (EAX & ~0xffff) | al;
    CC_DST = al;
}

void helper_aaa(void)
{
    int icarry;
    int al, ah, af;
    int eflags;

    eflags = cc_table[CC_OP].compute_all();
    af = eflags & CC_A;
    al = EAX & 0xff;
    ah = (EAX >> 8) & 0xff;

    icarry = (al > 0xf9);
    if (((al & 0x0f) > 9 ) || af) {
        al = (al + 6) & 0x0f;
        ah = (ah + 1 + icarry) & 0xff;
        eflags |= CC_C | CC_A;
    } else {
        eflags &= ~(CC_C | CC_A);
        al &= 0x0f;
    }
    EAX = (EAX & ~0xffff) | al | (ah << 8);
    CC_SRC = eflags;
    FORCE_RET();
}

void helper_aas(void)
{
    int icarry;
    int al, ah, af;
    int eflags;

    eflags = cc_table[CC_OP].compute_all();
    af = eflags & CC_A;
    al = EAX & 0xff;
    ah = (EAX >> 8) & 0xff;

    icarry = (al < 6);
    if (((al & 0x0f) > 9 ) || af) {
        al = (al - 6) & 0x0f;
        ah = (ah - 1 - icarry) & 0xff;
        eflags |= CC_C | CC_A;
    } else {
        eflags &= ~(CC_C | CC_A);
        al &= 0x0f;
    }
    EAX = (EAX & ~0xffff) | al | (ah << 8);
    CC_SRC = eflags;
    FORCE_RET();
}

void helper_daa(void)
{
    int al, af, cf;
    int eflags;

    eflags = cc_table[CC_OP].compute_all();
    cf = eflags & CC_C;
    af = eflags & CC_A;
    al = EAX & 0xff;

    eflags = 0;
    if (((al & 0x0f) > 9 ) || af) {
        al = (al + 6) & 0xff;
        eflags |= CC_A;
    }
    if ((al > 0x9f) || cf) {
        al = (al + 0x60) & 0xff;
        eflags |= CC_C;
    }
    EAX = (EAX & ~0xff) | al;
    /* well, speed is not an issue here, so we compute the flags by hand */
    eflags |= (al == 0) << 6; /* zf */
    eflags |= parity_table[al]; /* pf */
    eflags |= (al & 0x80); /* sf */
    CC_SRC = eflags;
    FORCE_RET();
}

void helper_das(void)
{
    int al, al1, af, cf;
    int eflags;

    eflags = cc_table[CC_OP].compute_all();
    cf = eflags & CC_C;
    af = eflags & CC_A;
    al = EAX & 0xff;

    eflags = 0;
    al1 = al;
    if (((al & 0x0f) > 9 ) || af) {
        eflags |= CC_A;
        if (al < 6 || cf)
            eflags |= CC_C;
        al = (al - 6) & 0xff;
    }
    if ((al1 > 0x99) || cf) {
        al = (al - 0x60) & 0xff;
        eflags |= CC_C;
    }
    EAX = (EAX & ~0xff) | al;
    /* well, speed is not an issue here, so we compute the flags by hand */
    eflags |= (al == 0) << 6; /* zf */
    eflags |= parity_table[al]; /* pf */
    eflags |= (al & 0x80); /* sf */
    CC_SRC = eflags;
    FORCE_RET();
}

B
bellard 已提交
1866 1867 1868 1869 1870 1871 1872 1873 1874
void helper_into(int next_eip_addend)
{
    int eflags;
    eflags = cc_table[CC_OP].compute_all();
    if (eflags & CC_O) {
        raise_interrupt(EXCP04_INTO, 1, 0, next_eip_addend);
    }
}

1875
void helper_cmpxchg8b(target_ulong a0)
B
bellard 已提交
1876 1877 1878 1879 1880
{
    uint64_t d;
    int eflags;

    eflags = cc_table[CC_OP].compute_all();
1881
    d = ldq(a0);
B
bellard 已提交
1882 1883
    if (d == (((uint64_t)EDX << 32) | (uint32_t)EAX)) {
        stq(a0, ((uint64_t)ECX << 32) | (uint32_t)EBX);
B
bellard 已提交
1884 1885
        eflags |= CC_Z;
    } else {
1886 1887
        EDX = (uint32_t)(d >> 32);
        EAX = (uint32_t)d;
B
bellard 已提交
1888 1889 1890 1891 1892
        eflags &= ~CC_Z;
    }
    CC_SRC = eflags;
}

B
bellard 已提交
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
#ifdef TARGET_X86_64
void helper_cmpxchg16b(target_ulong a0)
{
    uint64_t d0, d1;
    int eflags;

    eflags = cc_table[CC_OP].compute_all();
    d0 = ldq(a0);
    d1 = ldq(a0 + 8);
    if (d0 == EAX && d1 == EDX) {
        stq(a0, EBX);
        stq(a0 + 8, ECX);
        eflags |= CC_Z;
    } else {
        EDX = d1;
        EAX = d0;
        eflags &= ~CC_Z;
    }
    CC_SRC = eflags;
}
#endif

1915
void helper_single_step(void)
1916 1917 1918 1919 1920
{
    env->dr[6] |= 0x4000;
    raise_exception(EXCP01_SSTP);
}

B
bellard 已提交
1921 1922
void helper_cpuid(void)
{
1923 1924
    uint32_t index;
    index = (uint32_t)EAX;
1925

1926 1927
    /* test if maximum index reached */
    if (index & 0x80000000) {
1928
        if (index > env->cpuid_xlevel)
1929 1930
            index = env->cpuid_level;
    } else {
1931
        if (index > env->cpuid_level)
1932 1933
            index = env->cpuid_level;
    }
1934

1935
    switch(index) {
1936
    case 0:
1937
        EAX = env->cpuid_level;
B
bellard 已提交
1938 1939 1940
        EBX = env->cpuid_vendor1;
        EDX = env->cpuid_vendor2;
        ECX = env->cpuid_vendor3;
1941 1942
        break;
    case 1:
B
bellard 已提交
1943
        EAX = env->cpuid_version;
1944
        EBX = (env->cpuid_apic_id << 24) | 8 << 8; /* CLFLUSH size in quad words, Linux wants it. */
B
bellard 已提交
1945
        ECX = env->cpuid_ext_features;
B
bellard 已提交
1946
        EDX = env->cpuid_features;
1947
        break;
1948
    case 2:
1949
        /* cache info: needed for Pentium Pro compatibility */
T
ths 已提交
1950
        EAX = 1;
B
bellard 已提交
1951 1952
        EBX = 0;
        ECX = 0;
T
ths 已提交
1953
        EDX = 0x2c307d;
1954
        break;
B
bellard 已提交
1955
    case 0x80000000:
1956
        EAX = env->cpuid_xlevel;
B
bellard 已提交
1957 1958 1959 1960 1961 1962 1963
        EBX = env->cpuid_vendor1;
        EDX = env->cpuid_vendor2;
        ECX = env->cpuid_vendor3;
        break;
    case 0x80000001:
        EAX = env->cpuid_features;
        EBX = 0;
T
ths 已提交
1964
        ECX = env->cpuid_ext3_features;
1965 1966 1967 1968 1969 1970 1971 1972 1973
        EDX = env->cpuid_ext2_features;
        break;
    case 0x80000002:
    case 0x80000003:
    case 0x80000004:
        EAX = env->cpuid_model[(index - 0x80000002) * 4 + 0];
        EBX = env->cpuid_model[(index - 0x80000002) * 4 + 1];
        ECX = env->cpuid_model[(index - 0x80000002) * 4 + 2];
        EDX = env->cpuid_model[(index - 0x80000002) * 4 + 3];
B
bellard 已提交
1974
        break;
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
    case 0x80000005:
        /* cache info (L1 cache) */
        EAX = 0x01ff01ff;
        EBX = 0x01ff01ff;
        ECX = 0x40020140;
        EDX = 0x40020140;
        break;
    case 0x80000006:
        /* cache info (L2 cache) */
        EAX = 0;
        EBX = 0x42004200;
        ECX = 0x02008140;
        EDX = 0;
        break;
B
bellard 已提交
1989 1990
    case 0x80000008:
        /* virtual & phys address size in low 2 bytes. */
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
/* XXX: This value must match the one used in the MMU code. */ 
#if defined(TARGET_X86_64)
#  if defined(USE_KQEMU)
        EAX = 0x00003020;	/* 48 bits virtual, 32 bits physical */
#  else
/* XXX: The physical address space is limited to 42 bits in exec.c. */
        EAX = 0x00003028;	/* 48 bits virtual, 40 bits physical */
#  endif
#else
# if defined(USE_KQEMU)
        EAX = 0x00000020;	/* 32 bits physical */
#  else
        EAX = 0x00000024;	/* 36 bits physical */
#  endif
#endif
B
bellard 已提交
2006 2007 2008 2009
        EBX = 0;
        ECX = 0;
        EDX = 0;
        break;
2010 2011 2012 2013 2014 2015
    case 0x8000000A:
        EAX = 0x00000001;
        EBX = 0;
        ECX = 0;
        EDX = 0;
        break;
2016 2017 2018 2019 2020 2021 2022
    default:
        /* reserved values: zero */
        EAX = 0;
        EBX = 0;
        ECX = 0;
        EDX = 0;
        break;
B
bellard 已提交
2023 2024 2025
    }
}

2026
void helper_enter_level(int level, int data32, target_ulong t1)
B
bellard 已提交
2027
{
B
bellard 已提交
2028
    target_ulong ssp;
B
bellard 已提交
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
    uint32_t esp_mask, esp, ebp;

    esp_mask = get_sp_mask(env->segs[R_SS].flags);
    ssp = env->segs[R_SS].base;
    ebp = EBP;
    esp = ESP;
    if (data32) {
        /* 32 bit */
        esp -= 4;
        while (--level) {
            esp -= 4;
            ebp -= 4;
            stl(ssp + (esp & esp_mask), ldl(ssp + (ebp & esp_mask)));
        }
        esp -= 4;
2044
        stl(ssp + (esp & esp_mask), t1);
B
bellard 已提交
2045 2046 2047 2048 2049 2050 2051 2052 2053
    } else {
        /* 16 bit */
        esp -= 2;
        while (--level) {
            esp -= 2;
            ebp -= 2;
            stw(ssp + (esp & esp_mask), lduw(ssp + (ebp & esp_mask)));
        }
        esp -= 2;
2054
        stw(ssp + (esp & esp_mask), t1);
B
bellard 已提交
2055 2056 2057
    }
}

2058
#ifdef TARGET_X86_64
2059
void helper_enter64_level(int level, int data64, target_ulong t1)
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
{
    target_ulong esp, ebp;
    ebp = EBP;
    esp = ESP;

    if (data64) {
        /* 64 bit */
        esp -= 8;
        while (--level) {
            esp -= 8;
            ebp -= 8;
            stq(esp, ldq(ebp));
        }
        esp -= 8;
2074
        stq(esp, t1);
2075 2076 2077 2078 2079 2080 2081 2082 2083
    } else {
        /* 16 bit */
        esp -= 2;
        while (--level) {
            esp -= 2;
            ebp -= 2;
            stw(esp, lduw(ebp));
        }
        esp -= 2;
2084
        stw(esp, t1);
2085 2086 2087 2088
    }
}
#endif

B
bellard 已提交
2089
void helper_lldt(int selector)
B
bellard 已提交
2090 2091 2092
{
    SegmentCache *dt;
    uint32_t e1, e2;
B
bellard 已提交
2093 2094
    int index, entry_limit;
    target_ulong ptr;
2095

B
bellard 已提交
2096
    selector &= 0xffff;
B
bellard 已提交
2097 2098
    if ((selector & 0xfffc) == 0) {
        /* XXX: NULL selector case: invalid LDT */
B
bellard 已提交
2099
        env->ldt.base = 0;
B
bellard 已提交
2100 2101 2102 2103 2104 2105
        env->ldt.limit = 0;
    } else {
        if (selector & 0x4)
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        dt = &env->gdt;
        index = selector & ~7;
B
bellard 已提交
2106 2107 2108 2109
#ifdef TARGET_X86_64
        if (env->hflags & HF_LMA_MASK)
            entry_limit = 15;
        else
2110
#endif
B
bellard 已提交
2111 2112
            entry_limit = 7;
        if ((index + entry_limit) > dt->limit)
B
bellard 已提交
2113 2114
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        ptr = dt->base + index;
B
bellard 已提交
2115 2116
        e1 = ldl_kernel(ptr);
        e2 = ldl_kernel(ptr + 4);
B
bellard 已提交
2117 2118 2119 2120
        if ((e2 & DESC_S_MASK) || ((e2 >> DESC_TYPE_SHIFT) & 0xf) != 2)
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
B
bellard 已提交
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
#ifdef TARGET_X86_64
        if (env->hflags & HF_LMA_MASK) {
            uint32_t e3;
            e3 = ldl_kernel(ptr + 8);
            load_seg_cache_raw_dt(&env->ldt, e1, e2);
            env->ldt.base |= (target_ulong)e3 << 32;
        } else
#endif
        {
            load_seg_cache_raw_dt(&env->ldt, e1, e2);
        }
B
bellard 已提交
2132 2133 2134 2135
    }
    env->ldt.selector = selector;
}

B
bellard 已提交
2136
void helper_ltr(int selector)
B
bellard 已提交
2137 2138 2139
{
    SegmentCache *dt;
    uint32_t e1, e2;
B
bellard 已提交
2140 2141
    int index, type, entry_limit;
    target_ulong ptr;
2142

B
bellard 已提交
2143
    selector &= 0xffff;
B
bellard 已提交
2144
    if ((selector & 0xfffc) == 0) {
B
bellard 已提交
2145 2146
        /* NULL selector case: invalid TR */
        env->tr.base = 0;
B
bellard 已提交
2147 2148 2149 2150 2151 2152 2153
        env->tr.limit = 0;
        env->tr.flags = 0;
    } else {
        if (selector & 0x4)
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        dt = &env->gdt;
        index = selector & ~7;
B
bellard 已提交
2154 2155 2156 2157
#ifdef TARGET_X86_64
        if (env->hflags & HF_LMA_MASK)
            entry_limit = 15;
        else
2158
#endif
B
bellard 已提交
2159 2160
            entry_limit = 7;
        if ((index + entry_limit) > dt->limit)
B
bellard 已提交
2161 2162
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        ptr = dt->base + index;
B
bellard 已提交
2163 2164
        e1 = ldl_kernel(ptr);
        e2 = ldl_kernel(ptr + 4);
B
bellard 已提交
2165
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
2166
        if ((e2 & DESC_S_MASK) ||
2167
            (type != 1 && type != 9))
B
bellard 已提交
2168 2169 2170
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
B
bellard 已提交
2171 2172
#ifdef TARGET_X86_64
        if (env->hflags & HF_LMA_MASK) {
2173
            uint32_t e3, e4;
B
bellard 已提交
2174
            e3 = ldl_kernel(ptr + 8);
2175 2176 2177
            e4 = ldl_kernel(ptr + 12);
            if ((e4 >> DESC_TYPE_SHIFT) & 0xf)
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
B
bellard 已提交
2178 2179
            load_seg_cache_raw_dt(&env->tr, e1, e2);
            env->tr.base |= (target_ulong)e3 << 32;
2180
        } else
B
bellard 已提交
2181 2182 2183 2184
#endif
        {
            load_seg_cache_raw_dt(&env->tr, e1, e2);
        }
2185
        e2 |= DESC_TSS_BUSY_MASK;
B
bellard 已提交
2186
        stl_kernel(ptr + 4, e2);
B
bellard 已提交
2187 2188 2189 2190
    }
    env->tr.selector = selector;
}

2191
/* only works if protected mode and not VM86. seg_reg must be != R_CS */
B
bellard 已提交
2192
void helper_load_seg(int seg_reg, int selector)
B
bellard 已提交
2193 2194
{
    uint32_t e1, e2;
2195 2196 2197
    int cpl, dpl, rpl;
    SegmentCache *dt;
    int index;
B
bellard 已提交
2198
    target_ulong ptr;
2199

2200
    selector &= 0xffff;
2201
    cpl = env->hflags & HF_CPL_MASK;
B
bellard 已提交
2202 2203
    if ((selector & 0xfffc) == 0) {
        /* null selector case */
B
bellard 已提交
2204 2205
        if (seg_reg == R_SS
#ifdef TARGET_X86_64
2206
            && (!(env->hflags & HF_CS64_MASK) || cpl == 3)
B
bellard 已提交
2207 2208
#endif
            )
B
bellard 已提交
2209
            raise_exception_err(EXCP0D_GPF, 0);
B
bellard 已提交
2210
        cpu_x86_load_seg_cache(env, seg_reg, selector, 0, 0, 0);
B
bellard 已提交
2211
    } else {
2212

2213 2214 2215 2216 2217
        if (selector & 0x4)
            dt = &env->ldt;
        else
            dt = &env->gdt;
        index = selector & ~7;
2218
        if ((index + 7) > dt->limit)
B
bellard 已提交
2219
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
2220 2221 2222
        ptr = dt->base + index;
        e1 = ldl_kernel(ptr);
        e2 = ldl_kernel(ptr + 4);
2223

2224
        if (!(e2 & DESC_S_MASK))
B
bellard 已提交
2225
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
2226 2227
        rpl = selector & 3;
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
B
bellard 已提交
2228
        if (seg_reg == R_SS) {
2229
            /* must be writable segment */
2230
            if ((e2 & DESC_CS_MASK) || !(e2 & DESC_W_MASK))
B
bellard 已提交
2231
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
2232
            if (rpl != cpl || dpl != cpl)
2233
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
B
bellard 已提交
2234
        } else {
2235
            /* must be readable segment */
2236
            if ((e2 & (DESC_CS_MASK | DESC_R_MASK)) == DESC_CS_MASK)
B
bellard 已提交
2237
                raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
2238

2239 2240
            if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
                /* if not conforming code, test rights */
2241
                if (dpl < cpl || dpl < rpl)
2242 2243
                    raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
            }
B
bellard 已提交
2244 2245 2246 2247 2248 2249 2250 2251
        }

        if (!(e2 & DESC_P_MASK)) {
            if (seg_reg == R_SS)
                raise_exception_err(EXCP0C_STACK, selector & 0xfffc);
            else
                raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);
        }
2252 2253 2254 2255 2256 2257 2258

        /* set the access bit if not already set */
        if (!(e2 & DESC_A_MASK)) {
            e2 |= DESC_A_MASK;
            stl_kernel(ptr + 4, e2);
        }

2259
        cpu_x86_load_seg_cache(env, seg_reg, selector,
B
bellard 已提交
2260 2261 2262 2263
                       get_seg_base(e1, e2),
                       get_seg_limit(e1, e2),
                       e2);
#if 0
2264
        fprintf(logfile, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx flags=%08x\n",
B
bellard 已提交
2265 2266 2267 2268 2269 2270
                selector, (unsigned long)sc->base, sc->limit, sc->flags);
#endif
    }
}

/* protected mode jump */
2271 2272
void helper_ljmp_protected(int new_cs, target_ulong new_eip,
                           int next_eip_addend)
B
bellard 已提交
2273
{
2274
    int gate_cs, type;
B
bellard 已提交
2275
    uint32_t e1, e2, cpl, dpl, rpl, limit;
2276
    target_ulong next_eip;
2277

B
bellard 已提交
2278 2279 2280 2281 2282 2283 2284 2285 2286
    if ((new_cs & 0xfffc) == 0)
        raise_exception_err(EXCP0D_GPF, 0);
    if (load_segment(&e1, &e2, new_cs) != 0)
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
    cpl = env->hflags & HF_CPL_MASK;
    if (e2 & DESC_S_MASK) {
        if (!(e2 & DESC_CS_MASK))
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2287
        if (e2 & DESC_C_MASK) {
B
bellard 已提交
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
            /* conforming code segment */
            if (dpl > cpl)
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
        } else {
            /* non conforming code segment */
            rpl = new_cs & 3;
            if (rpl > cpl)
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
            if (dpl != cpl)
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
        }
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
        limit = get_seg_limit(e1, e2);
2302
        if (new_eip > limit &&
B
bellard 已提交
2303
            !(env->hflags & HF_LMA_MASK) && !(e2 & DESC_L_MASK))
B
bellard 已提交
2304 2305 2306 2307 2308
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
        cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
                       get_seg_base(e1, e2), limit, e2);
        EIP = new_eip;
    } else {
2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
        /* jump to call or task gate */
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
        rpl = new_cs & 3;
        cpl = env->hflags & HF_CPL_MASK;
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
        switch(type) {
        case 1: /* 286 TSS */
        case 9: /* 386 TSS */
        case 5: /* task gate */
            if (dpl < cpl || dpl < rpl)
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
2320
            next_eip = env->eip + next_eip_addend;
B
bellard 已提交
2321
            switch_tss(new_cs, e1, e2, SWITCH_TSS_JMP, next_eip);
B
bellard 已提交
2322
            CC_OP = CC_OP_EFLAGS;
2323 2324 2325 2326 2327 2328 2329 2330
            break;
        case 4: /* 286 call gate */
        case 12: /* 386 call gate */
            if ((dpl < cpl) || (dpl < rpl))
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
            if (!(e2 & DESC_P_MASK))
                raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
            gate_cs = e1 >> 16;
2331 2332 2333
            new_eip = (e1 & 0xffff);
            if (type == 12)
                new_eip |= (e2 & 0xffff0000);
2334 2335 2336 2337
            if (load_segment(&e1, &e2, gate_cs) != 0)
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
            dpl = (e2 >> DESC_DPL_SHIFT) & 3;
            /* must be code segment */
2338
            if (((e2 & (DESC_S_MASK | DESC_CS_MASK)) !=
2339 2340
                 (DESC_S_MASK | DESC_CS_MASK)))
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
2341
            if (((e2 & DESC_C_MASK) && (dpl > cpl)) ||
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
                (!(e2 & DESC_C_MASK) && (dpl != cpl)))
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
            if (!(e2 & DESC_P_MASK))
                raise_exception_err(EXCP0D_GPF, gate_cs & 0xfffc);
            limit = get_seg_limit(e1, e2);
            if (new_eip > limit)
                raise_exception_err(EXCP0D_GPF, 0);
            cpu_x86_load_seg_cache(env, R_CS, (gate_cs & 0xfffc) | cpl,
                                   get_seg_base(e1, e2), limit, e2);
            EIP = new_eip;
            break;
        default:
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
            break;
        }
B
bellard 已提交
2357 2358 2359 2360
    }
}

/* real mode call */
2361 2362
void helper_lcall_real(int new_cs, target_ulong new_eip1,
                       int shift, int next_eip)
B
bellard 已提交
2363
{
2364
    int new_eip;
B
bellard 已提交
2365
    uint32_t esp, esp_mask;
B
bellard 已提交
2366
    target_ulong ssp;
B
bellard 已提交
2367

2368
    new_eip = new_eip1;
B
bellard 已提交
2369
    esp = ESP;
2370
    esp_mask = get_sp_mask(env->segs[R_SS].flags);
B
bellard 已提交
2371 2372
    ssp = env->segs[R_SS].base;
    if (shift) {
2373 2374
        PUSHL(ssp, esp, esp_mask, env->segs[R_CS].selector);
        PUSHL(ssp, esp, esp_mask, next_eip);
B
bellard 已提交
2375
    } else {
2376 2377
        PUSHW(ssp, esp, esp_mask, env->segs[R_CS].selector);
        PUSHW(ssp, esp, esp_mask, next_eip);
B
bellard 已提交
2378 2379
    }

2380
    SET_ESP(esp, esp_mask);
B
bellard 已提交
2381 2382
    env->eip = new_eip;
    env->segs[R_CS].selector = new_cs;
B
bellard 已提交
2383
    env->segs[R_CS].base = (new_cs << 4);
B
bellard 已提交
2384 2385 2386
}

/* protected mode call */
2387 2388
void helper_lcall_protected(int new_cs, target_ulong new_eip, 
                            int shift, int next_eip_addend)
B
bellard 已提交
2389
{
2390
    int new_stack, i;
B
bellard 已提交
2391
    uint32_t e1, e2, cpl, dpl, rpl, selector, offset, param_count;
2392 2393
    uint32_t ss, ss_e1, ss_e2, sp, type, ss_dpl, sp_mask;
    uint32_t val, limit, old_sp_mask;
2394
    target_ulong ssp, old_ssp, next_eip;
2395

2396
    next_eip = env->eip + next_eip_addend;
B
bellard 已提交
2397
#ifdef DEBUG_PCALL
B
bellard 已提交
2398 2399
    if (loglevel & CPU_LOG_PCALL) {
        fprintf(logfile, "lcall %04x:%08x s=%d\n",
B
bellard 已提交
2400
                new_cs, (uint32_t)new_eip, shift);
B
bellard 已提交
2401
        cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
B
bellard 已提交
2402 2403
    }
#endif
B
bellard 已提交
2404 2405 2406 2407 2408
    if ((new_cs & 0xfffc) == 0)
        raise_exception_err(EXCP0D_GPF, 0);
    if (load_segment(&e1, &e2, new_cs) != 0)
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
    cpl = env->hflags & HF_CPL_MASK;
B
bellard 已提交
2409
#ifdef DEBUG_PCALL
B
bellard 已提交
2410
    if (loglevel & CPU_LOG_PCALL) {
B
bellard 已提交
2411 2412 2413
        fprintf(logfile, "desc=%08x:%08x\n", e1, e2);
    }
#endif
B
bellard 已提交
2414 2415 2416 2417
    if (e2 & DESC_S_MASK) {
        if (!(e2 & DESC_CS_MASK))
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2418
        if (e2 & DESC_C_MASK) {
B
bellard 已提交
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
            /* conforming code segment */
            if (dpl > cpl)
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
        } else {
            /* non conforming code segment */
            rpl = new_cs & 3;
            if (rpl > cpl)
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
            if (dpl != cpl)
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
        }
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);

2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443
#ifdef TARGET_X86_64
        /* XXX: check 16/32 bit cases in long mode */
        if (shift == 2) {
            target_ulong rsp;
            /* 64 bit case */
            rsp = ESP;
            PUSHQ(rsp, env->segs[R_CS].selector);
            PUSHQ(rsp, next_eip);
            /* from this point, not restartable */
            ESP = rsp;
            cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
2444
                                   get_seg_base(e1, e2),
2445 2446
                                   get_seg_limit(e1, e2), e2);
            EIP = new_eip;
2447
        } else
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
#endif
        {
            sp = ESP;
            sp_mask = get_sp_mask(env->segs[R_SS].flags);
            ssp = env->segs[R_SS].base;
            if (shift) {
                PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
                PUSHL(ssp, sp, sp_mask, next_eip);
            } else {
                PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
                PUSHW(ssp, sp, sp_mask, next_eip);
            }
2460

2461 2462 2463 2464
            limit = get_seg_limit(e1, e2);
            if (new_eip > limit)
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
            /* from this point, not restartable */
2465
            SET_ESP(sp, sp_mask);
2466 2467 2468
            cpu_x86_load_seg_cache(env, R_CS, (new_cs & 0xfffc) | cpl,
                                   get_seg_base(e1, e2), limit, e2);
            EIP = new_eip;
B
bellard 已提交
2469 2470 2471 2472
        }
    } else {
        /* check gate type */
        type = (e2 >> DESC_TYPE_SHIFT) & 0x1f;
2473 2474
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
        rpl = new_cs & 3;
B
bellard 已提交
2475 2476 2477 2478
        switch(type) {
        case 1: /* available 286 TSS */
        case 9: /* available 386 TSS */
        case 5: /* task gate */
2479 2480
            if (dpl < cpl || dpl < rpl)
                raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
B
bellard 已提交
2481
            switch_tss(new_cs, e1, e2, SWITCH_TSS_CALL, next_eip);
B
bellard 已提交
2482
            CC_OP = CC_OP_EFLAGS;
2483
            return;
B
bellard 已提交
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499
        case 4: /* 286 call gate */
        case 12: /* 386 call gate */
            break;
        default:
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
            break;
        }
        shift = type >> 3;

        if (dpl < cpl || dpl < rpl)
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
        /* check valid bit */
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0B_NOSEG,  new_cs & 0xfffc);
        selector = e1 >> 16;
        offset = (e2 & 0xffff0000) | (e1 & 0x0000ffff);
B
bellard 已提交
2500
        param_count = e2 & 0x1f;
B
bellard 已提交
2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
        if ((selector & 0xfffc) == 0)
            raise_exception_err(EXCP0D_GPF, 0);

        if (load_segment(&e1, &e2, selector) != 0)
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        if (!(e2 & DESC_S_MASK) || !(e2 & (DESC_CS_MASK)))
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        dpl = (e2 >> DESC_DPL_SHIFT) & 3;
        if (dpl > cpl)
            raise_exception_err(EXCP0D_GPF, selector & 0xfffc);
        if (!(e2 & DESC_P_MASK))
            raise_exception_err(EXCP0B_NOSEG, selector & 0xfffc);

        if (!(e2 & DESC_C_MASK) && dpl < cpl) {
B
blueswir1 已提交
2515
            /* to inner privilege */
B
bellard 已提交
2516
            get_ss_esp_from_tss(&ss, &sp, dpl);
B
bellard 已提交
2517
#ifdef DEBUG_PCALL
B
bellard 已提交
2518
            if (loglevel & CPU_LOG_PCALL)
2519
                fprintf(logfile, "new ss:esp=%04x:%08x param_count=%d ESP=" TARGET_FMT_lx "\n",
B
bellard 已提交
2520 2521
                        ss, sp, param_count, ESP);
#endif
B
bellard 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
            if ((ss & 0xfffc) == 0)
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
            if ((ss & 3) != dpl)
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
            if (load_segment(&ss_e1, &ss_e2, ss) != 0)
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
            ss_dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
            if (ss_dpl != dpl)
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
            if (!(ss_e2 & DESC_S_MASK) ||
                (ss_e2 & DESC_CS_MASK) ||
                !(ss_e2 & DESC_W_MASK))
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
            if (!(ss_e2 & DESC_P_MASK))
                raise_exception_err(EXCP0A_TSS, ss & 0xfffc);
2537

2538
            //            push_size = ((param_count * 2) + 8) << shift;
B
bellard 已提交
2539

2540 2541
            old_sp_mask = get_sp_mask(env->segs[R_SS].flags);
            old_ssp = env->segs[R_SS].base;
2542

2543 2544
            sp_mask = get_sp_mask(ss_e2);
            ssp = get_seg_base(ss_e1, ss_e2);
B
bellard 已提交
2545
            if (shift) {
2546 2547 2548 2549 2550
                PUSHL(ssp, sp, sp_mask, env->segs[R_SS].selector);
                PUSHL(ssp, sp, sp_mask, ESP);
                for(i = param_count - 1; i >= 0; i--) {
                    val = ldl_kernel(old_ssp + ((ESP + i * 4) & old_sp_mask));
                    PUSHL(ssp, sp, sp_mask, val);
B
bellard 已提交
2551 2552
                }
            } else {
2553 2554 2555 2556 2557
                PUSHW(ssp, sp, sp_mask, env->segs[R_SS].selector);
                PUSHW(ssp, sp, sp_mask, ESP);
                for(i = param_count - 1; i >= 0; i--) {
                    val = lduw_kernel(old_ssp + ((ESP + i * 2) & old_sp_mask));
                    PUSHW(ssp, sp, sp_mask, val);
B
bellard 已提交
2558 2559
                }
            }
2560
            new_stack = 1;
B
bellard 已提交
2561
        } else {
B
blueswir1 已提交
2562
            /* to same privilege */
2563 2564 2565 2566 2567
            sp = ESP;
            sp_mask = get_sp_mask(env->segs[R_SS].flags);
            ssp = env->segs[R_SS].base;
            //            push_size = (4 << shift);
            new_stack = 0;
B
bellard 已提交
2568 2569 2570
        }

        if (shift) {
2571 2572
            PUSHL(ssp, sp, sp_mask, env->segs[R_CS].selector);
            PUSHL(ssp, sp, sp_mask, next_eip);
B
bellard 已提交
2573
        } else {
2574 2575 2576 2577 2578 2579 2580 2581
            PUSHW(ssp, sp, sp_mask, env->segs[R_CS].selector);
            PUSHW(ssp, sp, sp_mask, next_eip);
        }

        /* from this point, not restartable */

        if (new_stack) {
            ss = (ss & ~3) | dpl;
2582
            cpu_x86_load_seg_cache(env, R_SS, ss,
2583 2584 2585
                                   ssp,
                                   get_seg_limit(ss_e1, ss_e2),
                                   ss_e2);
B
bellard 已提交
2586 2587 2588
        }

        selector = (selector & ~3) | dpl;
2589
        cpu_x86_load_seg_cache(env, R_CS, selector,
B
bellard 已提交
2590 2591 2592 2593
                       get_seg_base(e1, e2),
                       get_seg_limit(e1, e2),
                       e2);
        cpu_x86_set_cpl(env, dpl);
2594
        SET_ESP(sp, sp_mask);
B
bellard 已提交
2595 2596
        EIP = offset;
    }
B
bellard 已提交
2597 2598 2599 2600 2601 2602
#ifdef USE_KQEMU
    if (kqemu_is_ok(env)) {
        env->exception_index = -1;
        cpu_loop_exit();
    }
#endif
B
bellard 已提交
2603 2604
}

2605
/* real and vm86 mode iret */
B
bellard 已提交
2606 2607
void helper_iret_real(int shift)
{
2608
    uint32_t sp, new_cs, new_eip, new_eflags, sp_mask;
B
bellard 已提交
2609
    target_ulong ssp;
B
bellard 已提交
2610
    int eflags_mask;
2611

2612 2613 2614
    sp_mask = 0xffff; /* XXXX: use SS segment size ? */
    sp = ESP;
    ssp = env->segs[R_SS].base;
B
bellard 已提交
2615 2616
    if (shift == 1) {
        /* 32 bits */
2617 2618 2619 2620
        POPL(ssp, sp, sp_mask, new_eip);
        POPL(ssp, sp, sp_mask, new_cs);
        new_cs &= 0xffff;
        POPL(ssp, sp, sp_mask, new_eflags);
B
bellard 已提交
2621 2622
    } else {
        /* 16 bits */
2623 2624 2625
        POPW(ssp, sp, sp_mask, new_eip);
        POPW(ssp, sp, sp_mask, new_cs);
        POPW(ssp, sp, sp_mask, new_eflags);
B
bellard 已提交
2626
    }
B
bellard 已提交
2627
    ESP = (ESP & ~sp_mask) | (sp & sp_mask);
B
bellard 已提交
2628 2629
    load_seg_vm(R_CS, new_cs);
    env->eip = new_eip;
2630
    if (env->eflags & VM_MASK)
2631
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | RF_MASK | NT_MASK;
2632
    else
2633
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | IF_MASK | IOPL_MASK | RF_MASK | NT_MASK;
B
bellard 已提交
2634 2635 2636
    if (shift == 0)
        eflags_mask &= 0xffff;
    load_eflags(new_eflags, eflags_mask);
A
aurel32 已提交
2637
    env->hflags &= ~HF_NMI_MASK;
B
bellard 已提交
2638 2639
}

2640 2641 2642 2643
static inline void validate_seg(int seg_reg, int cpl)
{
    int dpl;
    uint32_t e2;
2644 2645 2646 2647

    /* XXX: on x86_64, we do not want to nullify FS and GS because
       they may still contain a valid base. I would be interested to
       know how a real x86_64 CPU behaves */
2648
    if ((seg_reg == R_FS || seg_reg == R_GS) &&
2649 2650 2651
        (env->segs[seg_reg].selector & 0xfffc) == 0)
        return;

2652 2653 2654 2655 2656
    e2 = env->segs[seg_reg].flags;
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    if (!(e2 & DESC_CS_MASK) || !(e2 & DESC_C_MASK)) {
        /* data or non conforming code segment */
        if (dpl < cpl) {
B
bellard 已提交
2657
            cpu_x86_load_seg_cache(env, seg_reg, 0, 0, 0, 0);
2658 2659 2660 2661
        }
    }
}

B
bellard 已提交
2662 2663 2664
/* protected mode iret */
static inline void helper_ret_protected(int shift, int is_iret, int addend)
{
B
bellard 已提交
2665
    uint32_t new_cs, new_eflags, new_ss;
B
bellard 已提交
2666 2667
    uint32_t new_es, new_ds, new_fs, new_gs;
    uint32_t e1, e2, ss_e1, ss_e2;
B
bellard 已提交
2668
    int cpl, dpl, rpl, eflags_mask, iopl;
B
bellard 已提交
2669
    target_ulong ssp, sp, new_eip, new_esp, sp_mask;
2670

B
bellard 已提交
2671 2672 2673 2674 2675 2676
#ifdef TARGET_X86_64
    if (shift == 2)
        sp_mask = -1;
    else
#endif
        sp_mask = get_sp_mask(env->segs[R_SS].flags);
B
bellard 已提交
2677
    sp = ESP;
2678
    ssp = env->segs[R_SS].base;
B
bellard 已提交
2679
    new_eflags = 0; /* avoid warning */
B
bellard 已提交
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
#ifdef TARGET_X86_64
    if (shift == 2) {
        POPQ(sp, new_eip);
        POPQ(sp, new_cs);
        new_cs &= 0xffff;
        if (is_iret) {
            POPQ(sp, new_eflags);
        }
    } else
#endif
B
bellard 已提交
2690 2691
    if (shift == 1) {
        /* 32 bits */
2692 2693 2694 2695 2696 2697 2698 2699
        POPL(ssp, sp, sp_mask, new_eip);
        POPL(ssp, sp, sp_mask, new_cs);
        new_cs &= 0xffff;
        if (is_iret) {
            POPL(ssp, sp, sp_mask, new_eflags);
            if (new_eflags & VM_MASK)
                goto return_to_vm86;
        }
B
bellard 已提交
2700 2701
    } else {
        /* 16 bits */
2702 2703
        POPW(ssp, sp, sp_mask, new_eip);
        POPW(ssp, sp, sp_mask, new_cs);
B
bellard 已提交
2704
        if (is_iret)
2705
            POPW(ssp, sp, sp_mask, new_eflags);
B
bellard 已提交
2706
    }
2707
#ifdef DEBUG_PCALL
B
bellard 已提交
2708
    if (loglevel & CPU_LOG_PCALL) {
B
bellard 已提交
2709
        fprintf(logfile, "lret new %04x:" TARGET_FMT_lx " s=%d addend=0x%x\n",
B
bellard 已提交
2710
                new_cs, new_eip, shift, addend);
B
bellard 已提交
2711
        cpu_dump_state(env, logfile, fprintf, X86_DUMP_CCOP);
2712 2713
    }
#endif
B
bellard 已提交
2714 2715 2716 2717 2718 2719 2720 2721
    if ((new_cs & 0xfffc) == 0)
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
    if (load_segment(&e1, &e2, new_cs) != 0)
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
    if (!(e2 & DESC_S_MASK) ||
        !(e2 & DESC_CS_MASK))
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
    cpl = env->hflags & HF_CPL_MASK;
2722
    rpl = new_cs & 3;
B
bellard 已提交
2723 2724 2725
    if (rpl < cpl)
        raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
2726
    if (e2 & DESC_C_MASK) {
B
bellard 已提交
2727 2728 2729 2730 2731 2732 2733 2734
        if (dpl > rpl)
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
    } else {
        if (dpl != rpl)
            raise_exception_err(EXCP0D_GPF, new_cs & 0xfffc);
    }
    if (!(e2 & DESC_P_MASK))
        raise_exception_err(EXCP0B_NOSEG, new_cs & 0xfffc);
2735

2736
    sp += addend;
2737
    if (rpl == cpl && (!(env->hflags & HF_CS64_MASK) ||
B
bellard 已提交
2738
                       ((env->hflags & HF_CS64_MASK) && !is_iret))) {
B
bellard 已提交
2739
        /* return to same priledge level */
2740
        cpu_x86_load_seg_cache(env, R_CS, new_cs,
B
bellard 已提交
2741 2742 2743 2744
                       get_seg_base(e1, e2),
                       get_seg_limit(e1, e2),
                       e2);
    } else {
B
blueswir1 已提交
2745
        /* return to different privilege level */
B
bellard 已提交
2746 2747 2748 2749 2750 2751 2752
#ifdef TARGET_X86_64
        if (shift == 2) {
            POPQ(sp, new_esp);
            POPQ(sp, new_ss);
            new_ss &= 0xffff;
        } else
#endif
B
bellard 已提交
2753 2754
        if (shift == 1) {
            /* 32 bits */
2755 2756 2757
            POPL(ssp, sp, sp_mask, new_esp);
            POPL(ssp, sp, sp_mask, new_ss);
            new_ss &= 0xffff;
B
bellard 已提交
2758 2759
        } else {
            /* 16 bits */
2760 2761
            POPW(ssp, sp, sp_mask, new_esp);
            POPW(ssp, sp, sp_mask, new_ss);
B
bellard 已提交
2762
        }
B
bellard 已提交
2763 2764
#ifdef DEBUG_PCALL
        if (loglevel & CPU_LOG_PCALL) {
B
bellard 已提交
2765
            fprintf(logfile, "new ss:esp=%04x:" TARGET_FMT_lx "\n",
B
bellard 已提交
2766 2767 2768
                    new_ss, new_esp);
        }
#endif
2769 2770 2771
        if ((new_ss & 0xfffc) == 0) {
#ifdef TARGET_X86_64
            /* NULL ss is allowed in long mode if cpl != 3*/
2772
            /* XXX: test CS64 ? */
2773
            if ((env->hflags & HF_LMA_MASK) && rpl != 3) {
2774
                cpu_x86_load_seg_cache(env, R_SS, new_ss,
2775 2776 2777 2778
                                       0, 0xffffffff,
                                       DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                                       DESC_S_MASK | (rpl << DESC_DPL_SHIFT) |
                                       DESC_W_MASK | DESC_A_MASK);
2779
                ss_e2 = DESC_B_MASK; /* XXX: should not be needed ? */
2780
            } else
2781 2782 2783 2784
#endif
            {
                raise_exception_err(EXCP0D_GPF, 0);
            }
B
bellard 已提交
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
        } else {
            if ((new_ss & 3) != rpl)
                raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
            if (load_segment(&ss_e1, &ss_e2, new_ss) != 0)
                raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
            if (!(ss_e2 & DESC_S_MASK) ||
                (ss_e2 & DESC_CS_MASK) ||
                !(ss_e2 & DESC_W_MASK))
                raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
            dpl = (ss_e2 >> DESC_DPL_SHIFT) & 3;
            if (dpl != rpl)
                raise_exception_err(EXCP0D_GPF, new_ss & 0xfffc);
            if (!(ss_e2 & DESC_P_MASK))
                raise_exception_err(EXCP0B_NOSEG, new_ss & 0xfffc);
2799
            cpu_x86_load_seg_cache(env, R_SS, new_ss,
B
bellard 已提交
2800 2801 2802 2803
                                   get_seg_base(ss_e1, ss_e2),
                                   get_seg_limit(ss_e1, ss_e2),
                                   ss_e2);
        }
B
bellard 已提交
2804

2805
        cpu_x86_load_seg_cache(env, R_CS, new_cs,
B
bellard 已提交
2806 2807 2808 2809
                       get_seg_base(e1, e2),
                       get_seg_limit(e1, e2),
                       e2);
        cpu_x86_set_cpl(env, rpl);
2810
        sp = new_esp;
B
bellard 已提交
2811
#ifdef TARGET_X86_64
B
bellard 已提交
2812
        if (env->hflags & HF_CS64_MASK)
B
bellard 已提交
2813 2814 2815 2816
            sp_mask = -1;
        else
#endif
            sp_mask = get_sp_mask(ss_e2);
2817 2818

        /* validate data segments */
B
bellard 已提交
2819 2820 2821 2822
        validate_seg(R_ES, rpl);
        validate_seg(R_DS, rpl);
        validate_seg(R_FS, rpl);
        validate_seg(R_GS, rpl);
2823 2824

        sp += addend;
B
bellard 已提交
2825
    }
2826
    SET_ESP(sp, sp_mask);
B
bellard 已提交
2827 2828
    env->eip = new_eip;
    if (is_iret) {
B
bellard 已提交
2829
        /* NOTE: 'cpl' is the _old_ CPL */
2830
        eflags_mask = TF_MASK | AC_MASK | ID_MASK | RF_MASK | NT_MASK;
B
bellard 已提交
2831
        if (cpl == 0)
B
bellard 已提交
2832 2833 2834 2835
            eflags_mask |= IOPL_MASK;
        iopl = (env->eflags >> IOPL_SHIFT) & 3;
        if (cpl <= iopl)
            eflags_mask |= IF_MASK;
B
bellard 已提交
2836 2837 2838 2839 2840 2841 2842
        if (shift == 0)
            eflags_mask &= 0xffff;
        load_eflags(new_eflags, eflags_mask);
    }
    return;

 return_to_vm86:
2843 2844 2845 2846 2847 2848
    POPL(ssp, sp, sp_mask, new_esp);
    POPL(ssp, sp, sp_mask, new_ss);
    POPL(ssp, sp, sp_mask, new_es);
    POPL(ssp, sp, sp_mask, new_ds);
    POPL(ssp, sp, sp_mask, new_fs);
    POPL(ssp, sp, sp_mask, new_gs);
2849

B
bellard 已提交
2850
    /* modify processor state */
2851
    load_eflags(new_eflags, TF_MASK | AC_MASK | ID_MASK |
2852
                IF_MASK | IOPL_MASK | VM_MASK | NT_MASK | VIF_MASK | VIP_MASK);
2853
    load_seg_vm(R_CS, new_cs & 0xffff);
B
bellard 已提交
2854
    cpu_x86_set_cpl(env, 3);
2855 2856 2857 2858 2859
    load_seg_vm(R_SS, new_ss & 0xffff);
    load_seg_vm(R_ES, new_es & 0xffff);
    load_seg_vm(R_DS, new_ds & 0xffff);
    load_seg_vm(R_FS, new_fs & 0xffff);
    load_seg_vm(R_GS, new_gs & 0xffff);
B
bellard 已提交
2860

2861
    env->eip = new_eip & 0xffff;
B
bellard 已提交
2862 2863 2864
    ESP = new_esp;
}

B
bellard 已提交
2865
void helper_iret_protected(int shift, int next_eip)
B
bellard 已提交
2866
{
2867 2868
    int tss_selector, type;
    uint32_t e1, e2;
2869

2870 2871
    /* specific case for TSS */
    if (env->eflags & NT_MASK) {
B
bellard 已提交
2872 2873 2874 2875
#ifdef TARGET_X86_64
        if (env->hflags & HF_LMA_MASK)
            raise_exception_err(EXCP0D_GPF, 0);
#endif
2876 2877 2878 2879 2880 2881 2882 2883 2884
        tss_selector = lduw_kernel(env->tr.base + 0);
        if (tss_selector & 4)
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
        if (load_segment(&e1, &e2, tss_selector) != 0)
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
        type = (e2 >> DESC_TYPE_SHIFT) & 0x17;
        /* NOTE: we check both segment and busy TSS */
        if (type != 3)
            raise_exception_err(EXCP0A_TSS, tss_selector & 0xfffc);
B
bellard 已提交
2885
        switch_tss(tss_selector, e1, e2, SWITCH_TSS_IRET, next_eip);
2886 2887 2888
    } else {
        helper_ret_protected(shift, 1, 0);
    }
A
aurel32 已提交
2889
    env->hflags &= ~HF_NMI_MASK;
B
bellard 已提交
2890 2891 2892 2893 2894 2895 2896
#ifdef USE_KQEMU
    if (kqemu_is_ok(env)) {
        CC_OP = CC_OP_EFLAGS;
        env->exception_index = -1;
        cpu_loop_exit();
    }
#endif
B
bellard 已提交
2897 2898 2899 2900 2901
}

void helper_lret_protected(int shift, int addend)
{
    helper_ret_protected(shift, 0, addend);
B
bellard 已提交
2902 2903 2904 2905 2906 2907
#ifdef USE_KQEMU
    if (kqemu_is_ok(env)) {
        env->exception_index = -1;
        cpu_loop_exit();
    }
#endif
B
bellard 已提交
2908 2909
}

2910 2911 2912 2913 2914 2915 2916
void helper_sysenter(void)
{
    if (env->sysenter_cs == 0) {
        raise_exception_err(EXCP0D_GPF, 0);
    }
    env->eflags &= ~(VM_MASK | IF_MASK | RF_MASK);
    cpu_x86_set_cpl(env, 0);
2917 2918
    cpu_x86_load_seg_cache(env, R_CS, env->sysenter_cs & 0xfffc,
                           0, 0xffffffff,
2919 2920 2921
                           DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                           DESC_S_MASK |
                           DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2922
    cpu_x86_load_seg_cache(env, R_SS, (env->sysenter_cs + 8) & 0xfffc,
B
bellard 已提交
2923
                           0, 0xffffffff,
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939
                           DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                           DESC_S_MASK |
                           DESC_W_MASK | DESC_A_MASK);
    ESP = env->sysenter_esp;
    EIP = env->sysenter_eip;
}

void helper_sysexit(void)
{
    int cpl;

    cpl = env->hflags & HF_CPL_MASK;
    if (env->sysenter_cs == 0 || cpl != 0) {
        raise_exception_err(EXCP0D_GPF, 0);
    }
    cpu_x86_set_cpl(env, 3);
2940 2941
    cpu_x86_load_seg_cache(env, R_CS, ((env->sysenter_cs + 16) & 0xfffc) | 3,
                           0, 0xffffffff,
2942 2943 2944
                           DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                           DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
                           DESC_CS_MASK | DESC_R_MASK | DESC_A_MASK);
2945
    cpu_x86_load_seg_cache(env, R_SS, ((env->sysenter_cs + 24) & 0xfffc) | 3,
B
bellard 已提交
2946
                           0, 0xffffffff,
2947 2948 2949 2950 2951
                           DESC_G_MASK | DESC_B_MASK | DESC_P_MASK |
                           DESC_S_MASK | (3 << DESC_DPL_SHIFT) |
                           DESC_W_MASK | DESC_A_MASK);
    ESP = ECX;
    EIP = EDX;
B
bellard 已提交
2952 2953 2954 2955 2956 2957
#ifdef USE_KQEMU
    if (kqemu_is_ok(env)) {
        env->exception_index = -1;
        cpu_loop_exit();
    }
#endif
2958 2959
}

2960
void helper_movl_crN_T0(int reg, target_ulong t0)
B
bellard 已提交
2961
{
2962
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
2963 2964
    switch(reg) {
    case 0:
2965
        cpu_x86_update_cr0(env, t0);
B
bellard 已提交
2966 2967
        break;
    case 3:
2968
        cpu_x86_update_cr3(env, t0);
2969 2970
        break;
    case 4:
2971
        cpu_x86_update_cr4(env, t0);
2972
        break;
B
bellard 已提交
2973
    case 8:
2974 2975
        cpu_set_apic_tpr(env, t0);
        env->cr[8] = t0;
B
bellard 已提交
2976
        break;
2977
    default:
2978
        env->cr[reg] = t0;
B
bellard 已提交
2979 2980
        break;
    }
B
bellard 已提交
2981
#endif
B
bellard 已提交
2982 2983
}

2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004
void helper_lmsw(target_ulong t0)
{
    /* only 4 lower bits of CR0 are modified. PE cannot be set to zero
       if already set to one. */
    t0 = (env->cr[0] & ~0xe) | (t0 & 0xf);
    helper_movl_crN_T0(0, t0);
}

void helper_clts(void)
{
    env->cr[0] &= ~CR0_TS_MASK;
    env->hflags &= ~HF_TS_MASK;
}

#if !defined(CONFIG_USER_ONLY)
target_ulong helper_movtl_T0_cr8(void)
{
    return cpu_get_apic_tpr(env);
}
#endif

B
bellard 已提交
3005
/* XXX: do more */
3006
void helper_movl_drN_T0(int reg, target_ulong t0)
B
bellard 已提交
3007
{
3008
    env->dr[reg] = t0;
B
bellard 已提交
3009 3010
}

3011
void helper_invlpg(target_ulong addr)
B
bellard 已提交
3012 3013 3014 3015 3016 3017 3018
{
    cpu_x86_flush_tlb(env, addr);
}

void helper_rdtsc(void)
{
    uint64_t val;
B
bellard 已提交
3019 3020 3021 3022

    if ((env->cr[4] & CR4_TSD_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) {
        raise_exception(EXCP0D_GPF);
    }
B
bellard 已提交
3023
    val = cpu_get_tsc(env);
B
bellard 已提交
3024 3025 3026 3027
    EAX = (uint32_t)(val);
    EDX = (uint32_t)(val >> 32);
}

3028 3029 3030 3031 3032 3033
void helper_rdpmc(void)
{
    if ((env->cr[4] & CR4_PCE_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) {
        raise_exception(EXCP0D_GPF);
    }

3034 3035 3036 3037
    helper_svm_check_intercept_param(SVM_EXIT_RDPMC, 0);
    
    /* currently unimplemented */
    raise_exception_err(EXCP06_ILLOP, 0);
3038 3039
}

3040
#if defined(CONFIG_USER_ONLY)
B
bellard 已提交
3041 3042
void helper_wrmsr(void)
{
B
bellard 已提交
3043 3044
}

B
bellard 已提交
3045 3046 3047 3048
void helper_rdmsr(void)
{
}
#else
B
bellard 已提交
3049 3050
void helper_wrmsr(void)
{
B
bellard 已提交
3051 3052 3053 3054 3055
    uint64_t val;

    val = ((uint32_t)EAX) | ((uint64_t)((uint32_t)EDX) << 32);

    switch((uint32_t)ECX) {
B
bellard 已提交
3056
    case MSR_IA32_SYSENTER_CS:
B
bellard 已提交
3057
        env->sysenter_cs = val & 0xffff;
B
bellard 已提交
3058 3059
        break;
    case MSR_IA32_SYSENTER_ESP:
B
bellard 已提交
3060
        env->sysenter_esp = val;
B
bellard 已提交
3061 3062
        break;
    case MSR_IA32_SYSENTER_EIP:
B
bellard 已提交
3063 3064 3065 3066 3067 3068
        env->sysenter_eip = val;
        break;
    case MSR_IA32_APICBASE:
        cpu_set_apic_base(env, val);
        break;
    case MSR_EFER:
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079
        {
            uint64_t update_mask;
            update_mask = 0;
            if (env->cpuid_ext2_features & CPUID_EXT2_SYSCALL)
                update_mask |= MSR_EFER_SCE;
            if (env->cpuid_ext2_features & CPUID_EXT2_LM)
                update_mask |= MSR_EFER_LME;
            if (env->cpuid_ext2_features & CPUID_EXT2_FFXSR)
                update_mask |= MSR_EFER_FFXSR;
            if (env->cpuid_ext2_features & CPUID_EXT2_NX)
                update_mask |= MSR_EFER_NXE;
3080
            env->efer = (env->efer & ~update_mask) |
3081 3082
            (val & update_mask);
        }
B
bellard 已提交
3083
        break;
B
bellard 已提交
3084 3085 3086
    case MSR_STAR:
        env->star = val;
        break;
3087 3088 3089
    case MSR_PAT:
        env->pat = val;
        break;
T
ths 已提交
3090 3091 3092
    case MSR_VM_HSAVE_PA:
        env->vm_hsave = val;
        break;
3093
#ifdef TARGET_X86_64
B
bellard 已提交
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112
    case MSR_LSTAR:
        env->lstar = val;
        break;
    case MSR_CSTAR:
        env->cstar = val;
        break;
    case MSR_FMASK:
        env->fmask = val;
        break;
    case MSR_FSBASE:
        env->segs[R_FS].base = val;
        break;
    case MSR_GSBASE:
        env->segs[R_GS].base = val;
        break;
    case MSR_KERNELGSBASE:
        env->kernelgsbase = val;
        break;
#endif
B
bellard 已提交
3113 3114
    default:
        /* XXX: exception ? */
3115
        break;
B
bellard 已提交
3116 3117 3118 3119 3120
    }
}

void helper_rdmsr(void)
{
B
bellard 已提交
3121 3122
    uint64_t val;
    switch((uint32_t)ECX) {
B
bellard 已提交
3123
    case MSR_IA32_SYSENTER_CS:
B
bellard 已提交
3124
        val = env->sysenter_cs;
B
bellard 已提交
3125 3126
        break;
    case MSR_IA32_SYSENTER_ESP:
B
bellard 已提交
3127
        val = env->sysenter_esp;
B
bellard 已提交
3128 3129
        break;
    case MSR_IA32_SYSENTER_EIP:
B
bellard 已提交
3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
        val = env->sysenter_eip;
        break;
    case MSR_IA32_APICBASE:
        val = cpu_get_apic_base(env);
        break;
    case MSR_EFER:
        val = env->efer;
        break;
    case MSR_STAR:
        val = env->star;
        break;
3141 3142 3143
    case MSR_PAT:
        val = env->pat;
        break;
T
ths 已提交
3144 3145 3146
    case MSR_VM_HSAVE_PA:
        val = env->vm_hsave;
        break;
3147
#ifdef TARGET_X86_64
B
bellard 已提交
3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161
    case MSR_LSTAR:
        val = env->lstar;
        break;
    case MSR_CSTAR:
        val = env->cstar;
        break;
    case MSR_FMASK:
        val = env->fmask;
        break;
    case MSR_FSBASE:
        val = env->segs[R_FS].base;
        break;
    case MSR_GSBASE:
        val = env->segs[R_GS].base;
B
bellard 已提交
3162
        break;
B
bellard 已提交
3163 3164 3165 3166
    case MSR_KERNELGSBASE:
        val = env->kernelgsbase;
        break;
#endif
B
bellard 已提交
3167 3168
    default:
        /* XXX: exception ? */
B
bellard 已提交
3169
        val = 0;
3170
        break;
B
bellard 已提交
3171
    }
B
bellard 已提交
3172 3173
    EAX = (uint32_t)(val);
    EDX = (uint32_t)(val >> 32);
B
bellard 已提交
3174
}
B
bellard 已提交
3175
#endif
B
bellard 已提交
3176

3177
target_ulong helper_lsl(target_ulong selector1)
B
bellard 已提交
3178
{
B
bellard 已提交
3179
    unsigned int limit;
3180
    uint32_t e1, e2, eflags, selector;
3181
    int rpl, dpl, cpl, type;
B
bellard 已提交
3182

3183
    selector = selector1 & 0xffff;
3184
    eflags = cc_table[CC_OP].compute_all();
B
bellard 已提交
3185
    if (load_segment(&e1, &e2, selector) != 0)
3186
        goto fail;
3187 3188 3189 3190 3191 3192 3193 3194
    rpl = selector & 3;
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    cpl = env->hflags & HF_CPL_MASK;
    if (e2 & DESC_S_MASK) {
        if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
            /* conforming */
        } else {
            if (dpl < cpl || dpl < rpl)
3195
                goto fail;
3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206
        }
    } else {
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
        switch(type) {
        case 1:
        case 2:
        case 3:
        case 9:
        case 11:
            break;
        default:
3207
            goto fail;
3208
        }
3209 3210 3211
        if (dpl < cpl || dpl < rpl) {
        fail:
            CC_SRC = eflags & ~CC_Z;
3212
            return 0;
3213
        }
3214 3215
    }
    limit = get_seg_limit(e1, e2);
3216
    CC_SRC = eflags | CC_Z;
3217
    return limit;
B
bellard 已提交
3218 3219
}

3220
target_ulong helper_lar(target_ulong selector1)
B
bellard 已提交
3221
{
3222
    uint32_t e1, e2, eflags, selector;
3223
    int rpl, dpl, cpl, type;
B
bellard 已提交
3224

3225
    selector = selector1 & 0xffff;
3226
    eflags = cc_table[CC_OP].compute_all();
3227
    if ((selector & 0xfffc) == 0)
3228
        goto fail;
B
bellard 已提交
3229
    if (load_segment(&e1, &e2, selector) != 0)
3230
        goto fail;
3231 3232 3233 3234 3235 3236 3237 3238
    rpl = selector & 3;
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    cpl = env->hflags & HF_CPL_MASK;
    if (e2 & DESC_S_MASK) {
        if ((e2 & DESC_CS_MASK) && (e2 & DESC_C_MASK)) {
            /* conforming */
        } else {
            if (dpl < cpl || dpl < rpl)
3239
                goto fail;
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253
        }
    } else {
        type = (e2 >> DESC_TYPE_SHIFT) & 0xf;
        switch(type) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 9:
        case 11:
        case 12:
            break;
        default:
3254
            goto fail;
3255
        }
3256 3257 3258
        if (dpl < cpl || dpl < rpl) {
        fail:
            CC_SRC = eflags & ~CC_Z;
3259
            return 0;
3260
        }
3261
    }
3262
    CC_SRC = eflags | CC_Z;
3263
    return e2 & 0x00f0ff00;
B
bellard 已提交
3264 3265
}

3266
void helper_verr(target_ulong selector1)
3267
{
3268
    uint32_t e1, e2, eflags, selector;
3269 3270
    int rpl, dpl, cpl;

3271
    selector = selector1 & 0xffff;
3272
    eflags = cc_table[CC_OP].compute_all();
3273
    if ((selector & 0xfffc) == 0)
3274
        goto fail;
3275
    if (load_segment(&e1, &e2, selector) != 0)
3276
        goto fail;
3277
    if (!(e2 & DESC_S_MASK))
3278
        goto fail;
3279 3280 3281 3282 3283
    rpl = selector & 3;
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    cpl = env->hflags & HF_CPL_MASK;
    if (e2 & DESC_CS_MASK) {
        if (!(e2 & DESC_R_MASK))
3284
            goto fail;
3285 3286
        if (!(e2 & DESC_C_MASK)) {
            if (dpl < cpl || dpl < rpl)
3287
                goto fail;
3288 3289
        }
    } else {
3290 3291 3292
        if (dpl < cpl || dpl < rpl) {
        fail:
            CC_SRC = eflags & ~CC_Z;
3293
            return;
3294
        }
3295
    }
3296
    CC_SRC = eflags | CC_Z;
3297 3298
}

3299
void helper_verw(target_ulong selector1)
3300
{
3301
    uint32_t e1, e2, eflags, selector;
3302 3303
    int rpl, dpl, cpl;

3304
    selector = selector1 & 0xffff;
3305
    eflags = cc_table[CC_OP].compute_all();
3306
    if ((selector & 0xfffc) == 0)
3307
        goto fail;
3308
    if (load_segment(&e1, &e2, selector) != 0)
3309
        goto fail;
3310
    if (!(e2 & DESC_S_MASK))
3311
        goto fail;
3312 3313 3314 3315
    rpl = selector & 3;
    dpl = (e2 >> DESC_DPL_SHIFT) & 3;
    cpl = env->hflags & HF_CPL_MASK;
    if (e2 & DESC_CS_MASK) {
3316
        goto fail;
3317 3318
    } else {
        if (dpl < cpl || dpl < rpl)
3319 3320 3321 3322
            goto fail;
        if (!(e2 & DESC_W_MASK)) {
        fail:
            CC_SRC = eflags & ~CC_Z;
3323
            return;
3324
        }
3325
    }
3326
    CC_SRC = eflags | CC_Z;
3327 3328
}

B
bellard 已提交
3329
/* x87 FPU helpers */
B
bellard 已提交
3330

3331
static void fpu_set_exception(int mask)
B
bellard 已提交
3332 3333 3334 3335 3336 3337
{
    env->fpus |= mask;
    if (env->fpus & (~env->fpuc & FPUC_EM))
        env->fpus |= FPUS_SE | FPUS_B;
}

B
bellard 已提交
3338
static inline CPU86_LDouble helper_fdiv(CPU86_LDouble a, CPU86_LDouble b)
B
bellard 已提交
3339
{
3340
    if (b == 0.0)
B
bellard 已提交
3341 3342 3343 3344 3345 3346 3347 3348
        fpu_set_exception(FPUS_ZE);
    return a / b;
}

void fpu_raise_exception(void)
{
    if (env->cr[0] & CR0_NE_MASK) {
        raise_exception(EXCP10_COPR);
3349 3350
    }
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
3351 3352 3353 3354 3355 3356
    else {
        cpu_set_ferr(env);
    }
#endif
}

B
bellard 已提交
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741
void helper_flds_FT0(uint32_t val)
{
    union {
        float32 f;
        uint32_t i;
    } u;
    u.i = val;
    FT0 = float32_to_floatx(u.f, &env->fp_status);
}

void helper_fldl_FT0(uint64_t val)
{
    union {
        float64 f;
        uint64_t i;
    } u;
    u.i = val;
    FT0 = float64_to_floatx(u.f, &env->fp_status);
}

void helper_fildl_FT0(int32_t val)
{
    FT0 = int32_to_floatx(val, &env->fp_status);
}

void helper_flds_ST0(uint32_t val)
{
    int new_fpstt;
    union {
        float32 f;
        uint32_t i;
    } u;
    new_fpstt = (env->fpstt - 1) & 7;
    u.i = val;
    env->fpregs[new_fpstt].d = float32_to_floatx(u.f, &env->fp_status);
    env->fpstt = new_fpstt;
    env->fptags[new_fpstt] = 0; /* validate stack entry */
}

void helper_fldl_ST0(uint64_t val)
{
    int new_fpstt;
    union {
        float64 f;
        uint64_t i;
    } u;
    new_fpstt = (env->fpstt - 1) & 7;
    u.i = val;
    env->fpregs[new_fpstt].d = float64_to_floatx(u.f, &env->fp_status);
    env->fpstt = new_fpstt;
    env->fptags[new_fpstt] = 0; /* validate stack entry */
}

void helper_fildl_ST0(int32_t val)
{
    int new_fpstt;
    new_fpstt = (env->fpstt - 1) & 7;
    env->fpregs[new_fpstt].d = int32_to_floatx(val, &env->fp_status);
    env->fpstt = new_fpstt;
    env->fptags[new_fpstt] = 0; /* validate stack entry */
}

void helper_fildll_ST0(int64_t val)
{
    int new_fpstt;
    new_fpstt = (env->fpstt - 1) & 7;
    env->fpregs[new_fpstt].d = int64_to_floatx(val, &env->fp_status);
    env->fpstt = new_fpstt;
    env->fptags[new_fpstt] = 0; /* validate stack entry */
}

uint32_t helper_fsts_ST0(void)
{
    union {
        float32 f;
        uint32_t i;
    } u;
    u.f = floatx_to_float32(ST0, &env->fp_status);
    return u.i;
}

uint64_t helper_fstl_ST0(void)
{
    union {
        float64 f;
        uint64_t i;
    } u;
    u.f = floatx_to_float64(ST0, &env->fp_status);
    return u.i;
}

int32_t helper_fist_ST0(void)
{
    int32_t val;
    val = floatx_to_int32(ST0, &env->fp_status);
    if (val != (int16_t)val)
        val = -32768;
    return val;
}

int32_t helper_fistl_ST0(void)
{
    int32_t val;
    val = floatx_to_int32(ST0, &env->fp_status);
    return val;
}

int64_t helper_fistll_ST0(void)
{
    int64_t val;
    val = floatx_to_int64(ST0, &env->fp_status);
    return val;
}

int32_t helper_fistt_ST0(void)
{
    int32_t val;
    val = floatx_to_int32_round_to_zero(ST0, &env->fp_status);
    if (val != (int16_t)val)
        val = -32768;
    return val;
}

int32_t helper_fisttl_ST0(void)
{
    int32_t val;
    val = floatx_to_int32_round_to_zero(ST0, &env->fp_status);
    return val;
}

int64_t helper_fisttll_ST0(void)
{
    int64_t val;
    val = floatx_to_int64_round_to_zero(ST0, &env->fp_status);
    return val;
}

void helper_fldt_ST0(target_ulong ptr)
{
    int new_fpstt;
    new_fpstt = (env->fpstt - 1) & 7;
    env->fpregs[new_fpstt].d = helper_fldt(ptr);
    env->fpstt = new_fpstt;
    env->fptags[new_fpstt] = 0; /* validate stack entry */
}

void helper_fstt_ST0(target_ulong ptr)
{
    helper_fstt(ST0, ptr);
}

void helper_fpush(void)
{
    fpush();
}

void helper_fpop(void)
{
    fpop();
}

void helper_fdecstp(void)
{
    env->fpstt = (env->fpstt - 1) & 7;
    env->fpus &= (~0x4700);
}

void helper_fincstp(void)
{
    env->fpstt = (env->fpstt + 1) & 7;
    env->fpus &= (~0x4700);
}

/* FPU move */

void helper_ffree_STN(int st_index)
{
    env->fptags[(env->fpstt + st_index) & 7] = 1;
}

void helper_fmov_ST0_FT0(void)
{
    ST0 = FT0;
}

void helper_fmov_FT0_STN(int st_index)
{
    FT0 = ST(st_index);
}

void helper_fmov_ST0_STN(int st_index)
{
    ST0 = ST(st_index);
}

void helper_fmov_STN_ST0(int st_index)
{
    ST(st_index) = ST0;
}

void helper_fxchg_ST0_STN(int st_index)
{
    CPU86_LDouble tmp;
    tmp = ST(st_index);
    ST(st_index) = ST0;
    ST0 = tmp;
}

/* FPU operations */

static const int fcom_ccval[4] = {0x0100, 0x4000, 0x0000, 0x4500};

void helper_fcom_ST0_FT0(void)
{
    int ret;

    ret = floatx_compare(ST0, FT0, &env->fp_status);
    env->fpus = (env->fpus & ~0x4500) | fcom_ccval[ret + 1];
    FORCE_RET();
}

void helper_fucom_ST0_FT0(void)
{
    int ret;

    ret = floatx_compare_quiet(ST0, FT0, &env->fp_status);
    env->fpus = (env->fpus & ~0x4500) | fcom_ccval[ret+ 1];
    FORCE_RET();
}

static const int fcomi_ccval[4] = {CC_C, CC_Z, 0, CC_Z | CC_P | CC_C};

void helper_fcomi_ST0_FT0(void)
{
    int eflags;
    int ret;

    ret = floatx_compare(ST0, FT0, &env->fp_status);
    eflags = cc_table[CC_OP].compute_all();
    eflags = (eflags & ~(CC_Z | CC_P | CC_C)) | fcomi_ccval[ret + 1];
    CC_SRC = eflags;
    FORCE_RET();
}

void helper_fucomi_ST0_FT0(void)
{
    int eflags;
    int ret;

    ret = floatx_compare_quiet(ST0, FT0, &env->fp_status);
    eflags = cc_table[CC_OP].compute_all();
    eflags = (eflags & ~(CC_Z | CC_P | CC_C)) | fcomi_ccval[ret + 1];
    CC_SRC = eflags;
    FORCE_RET();
}

void helper_fadd_ST0_FT0(void)
{
    ST0 += FT0;
}

void helper_fmul_ST0_FT0(void)
{
    ST0 *= FT0;
}

void helper_fsub_ST0_FT0(void)
{
    ST0 -= FT0;
}

void helper_fsubr_ST0_FT0(void)
{
    ST0 = FT0 - ST0;
}

void helper_fdiv_ST0_FT0(void)
{
    ST0 = helper_fdiv(ST0, FT0);
}

void helper_fdivr_ST0_FT0(void)
{
    ST0 = helper_fdiv(FT0, ST0);
}

/* fp operations between STN and ST0 */

void helper_fadd_STN_ST0(int st_index)
{
    ST(st_index) += ST0;
}

void helper_fmul_STN_ST0(int st_index)
{
    ST(st_index) *= ST0;
}

void helper_fsub_STN_ST0(int st_index)
{
    ST(st_index) -= ST0;
}

void helper_fsubr_STN_ST0(int st_index)
{
    CPU86_LDouble *p;
    p = &ST(st_index);
    *p = ST0 - *p;
}

void helper_fdiv_STN_ST0(int st_index)
{
    CPU86_LDouble *p;
    p = &ST(st_index);
    *p = helper_fdiv(*p, ST0);
}

void helper_fdivr_STN_ST0(int st_index)
{
    CPU86_LDouble *p;
    p = &ST(st_index);
    *p = helper_fdiv(ST0, *p);
}

/* misc FPU operations */
void helper_fchs_ST0(void)
{
    ST0 = floatx_chs(ST0);
}

void helper_fabs_ST0(void)
{
    ST0 = floatx_abs(ST0);
}

void helper_fld1_ST0(void)
{
    ST0 = f15rk[1];
}

void helper_fldl2t_ST0(void)
{
    ST0 = f15rk[6];
}

void helper_fldl2e_ST0(void)
{
    ST0 = f15rk[5];
}

void helper_fldpi_ST0(void)
{
    ST0 = f15rk[2];
}

void helper_fldlg2_ST0(void)
{
    ST0 = f15rk[3];
}

void helper_fldln2_ST0(void)
{
    ST0 = f15rk[4];
}

void helper_fldz_ST0(void)
{
    ST0 = f15rk[0];
}

void helper_fldz_FT0(void)
{
    FT0 = f15rk[0];
}

uint32_t helper_fnstsw(void)
{
    return (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
}

uint32_t helper_fnstcw(void)
{
    return env->fpuc;
}

B
bellard 已提交
3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
static void update_fp_status(void)
{
    int rnd_type;

    /* set rounding mode */
    switch(env->fpuc & RC_MASK) {
    default:
    case RC_NEAR:
        rnd_type = float_round_nearest_even;
        break;
    case RC_DOWN:
        rnd_type = float_round_down;
        break;
    case RC_UP:
        rnd_type = float_round_up;
        break;
    case RC_CHOP:
        rnd_type = float_round_to_zero;
        break;
    }
    set_float_rounding_mode(rnd_type, &env->fp_status);
#ifdef FLOATX80
    switch((env->fpuc >> 8) & 3) {
    case 0:
        rnd_type = 32;
        break;
    case 2:
        rnd_type = 64;
        break;
    case 3:
    default:
        rnd_type = 80;
        break;
    }
    set_floatx80_rounding_precision(rnd_type, &env->fp_status);
#endif
}

B
bellard 已提交
3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812
void helper_fldcw(uint32_t val)
{
    env->fpuc = val;
    update_fp_status();
}

void helper_fclex(void)
{
    env->fpus &= 0x7f00;
}

void helper_fwait(void)
{
    if (env->fpus & FPUS_SE)
        fpu_raise_exception();
    FORCE_RET();
}

void helper_fninit(void)
{
    env->fpus = 0;
    env->fpstt = 0;
    env->fpuc = 0x37f;
    env->fptags[0] = 1;
    env->fptags[1] = 1;
    env->fptags[2] = 1;
    env->fptags[3] = 1;
    env->fptags[4] = 1;
    env->fptags[5] = 1;
    env->fptags[6] = 1;
    env->fptags[7] = 1;
}

B
bellard 已提交
3813 3814
/* BCD ops */

B
bellard 已提交
3815
void helper_fbld_ST0(target_ulong ptr)
B
bellard 已提交
3816 3817 3818 3819 3820 3821 3822 3823
{
    CPU86_LDouble tmp;
    uint64_t val;
    unsigned int v;
    int i;

    val = 0;
    for(i = 8; i >= 0; i--) {
B
bellard 已提交
3824
        v = ldub(ptr + i);
B
bellard 已提交
3825 3826 3827
        val = (val * 100) + ((v >> 4) * 10) + (v & 0xf);
    }
    tmp = val;
B
bellard 已提交
3828
    if (ldub(ptr + 9) & 0x80)
B
bellard 已提交
3829 3830 3831 3832 3833
        tmp = -tmp;
    fpush();
    ST0 = tmp;
}

B
bellard 已提交
3834
void helper_fbst_ST0(target_ulong ptr)
B
bellard 已提交
3835 3836
{
    int v;
B
bellard 已提交
3837
    target_ulong mem_ref, mem_end;
B
bellard 已提交
3838 3839
    int64_t val;

B
bellard 已提交
3840
    val = floatx_to_int64(ST0, &env->fp_status);
B
bellard 已提交
3841
    mem_ref = ptr;
B
bellard 已提交
3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869
    mem_end = mem_ref + 9;
    if (val < 0) {
        stb(mem_end, 0x80);
        val = -val;
    } else {
        stb(mem_end, 0x00);
    }
    while (mem_ref < mem_end) {
        if (val == 0)
            break;
        v = val % 100;
        val = val / 100;
        v = ((v / 10) << 4) | (v % 10);
        stb(mem_ref++, v);
    }
    while (mem_ref < mem_end) {
        stb(mem_ref++, 0);
    }
}

void helper_f2xm1(void)
{
    ST0 = pow(2.0,ST0) - 1.0;
}

void helper_fyl2x(void)
{
    CPU86_LDouble fptemp;
3870

B
bellard 已提交
3871 3872 3873 3874 3875
    fptemp = ST0;
    if (fptemp>0.0){
        fptemp = log(fptemp)/log(2.0);	 /* log2(ST) */
        ST1 *= fptemp;
        fpop();
3876
    } else {
B
bellard 已提交
3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926
        env->fpus &= (~0x4700);
        env->fpus |= 0x400;
    }
}

void helper_fptan(void)
{
    CPU86_LDouble fptemp;

    fptemp = ST0;
    if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
        env->fpus |= 0x400;
    } else {
        ST0 = tan(fptemp);
        fpush();
        ST0 = 1.0;
        env->fpus &= (~0x400);  /* C2 <-- 0 */
        /* the above code is for  |arg| < 2**52 only */
    }
}

void helper_fpatan(void)
{
    CPU86_LDouble fptemp, fpsrcop;

    fpsrcop = ST1;
    fptemp = ST0;
    ST1 = atan2(fpsrcop,fptemp);
    fpop();
}

void helper_fxtract(void)
{
    CPU86_LDoubleU temp;
    unsigned int expdif;

    temp.d = ST0;
    expdif = EXPD(temp) - EXPBIAS;
    /*DP exponent bias*/
    ST0 = expdif;
    fpush();
    BIASEXPONENT(temp);
    ST0 = temp.d;
}

void helper_fprem1(void)
{
    CPU86_LDouble dblq, fpsrcop, fptemp;
    CPU86_LDoubleU fpsrcop1, fptemp1;
    int expdif;
3927 3928 3929 3930 3931 3932 3933
    signed long long int q;

    if (isinf(ST0) || isnan(ST0) || isnan(ST1) || (ST1 == 0.0)) {
        ST0 = 0.0 / 0.0; /* NaN */
        env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
        return;
    }
B
bellard 已提交
3934 3935 3936 3937 3938 3939

    fpsrcop = ST0;
    fptemp = ST1;
    fpsrcop1.d = fpsrcop;
    fptemp1.d = fptemp;
    expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
3940 3941 3942 3943 3944 3945 3946 3947

    if (expdif < 0) {
        /* optimisation? taken from the AMD docs */
        env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
        /* ST0 is unchanged */
        return;
    }

B
bellard 已提交
3948 3949
    if (expdif < 53) {
        dblq = fpsrcop / fptemp;
3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
        /* round dblq towards nearest integer */
        dblq = rint(dblq);
        ST0 = fpsrcop - fptemp * dblq;

        /* convert dblq to q by truncating towards zero */
        if (dblq < 0.0)
           q = (signed long long int)(-dblq);
        else
           q = (signed long long int)dblq;

B
bellard 已提交
3960
        env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
3961 3962 3963 3964
                                /* (C0,C3,C1) <-- (q2,q1,q0) */
        env->fpus |= (q & 0x4) << (8 - 2);  /* (C0) <-- q2 */
        env->fpus |= (q & 0x2) << (14 - 1); /* (C3) <-- q1 */
        env->fpus |= (q & 0x1) << (9 - 0);  /* (C1) <-- q0 */
B
bellard 已提交
3965 3966
    } else {
        env->fpus |= 0x400;  /* C2 <-- 1 */
3967
        fptemp = pow(2.0, expdif - 50);
B
bellard 已提交
3968
        fpsrcop = (ST0 / ST1) / fptemp;
3969 3970 3971
        /* fpsrcop = integer obtained by chopping */
        fpsrcop = (fpsrcop < 0.0) ?
                  -(floor(fabs(fpsrcop))) : floor(fpsrcop);
B
bellard 已提交
3972 3973 3974 3975 3976 3977 3978 3979 3980
        ST0 -= (ST1 * fpsrcop * fptemp);
    }
}

void helper_fprem(void)
{
    CPU86_LDouble dblq, fpsrcop, fptemp;
    CPU86_LDoubleU fpsrcop1, fptemp1;
    int expdif;
3981 3982 3983 3984 3985 3986 3987 3988 3989 3990
    signed long long int q;

    if (isinf(ST0) || isnan(ST0) || isnan(ST1) || (ST1 == 0.0)) {
       ST0 = 0.0 / 0.0; /* NaN */
       env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
       return;
    }

    fpsrcop = (CPU86_LDouble)ST0;
    fptemp = (CPU86_LDouble)ST1;
B
bellard 已提交
3991 3992 3993
    fpsrcop1.d = fpsrcop;
    fptemp1.d = fptemp;
    expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
3994 3995 3996 3997 3998 3999 4000 4001

    if (expdif < 0) {
        /* optimisation? taken from the AMD docs */
        env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
        /* ST0 is unchanged */
        return;
    }

B
bellard 已提交
4002
    if ( expdif < 53 ) {
4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013
        dblq = fpsrcop/*ST0*/ / fptemp/*ST1*/;
        /* round dblq towards zero */
        dblq = (dblq < 0.0) ? ceil(dblq) : floor(dblq);
        ST0 = fpsrcop/*ST0*/ - fptemp * dblq;

        /* convert dblq to q by truncating towards zero */
        if (dblq < 0.0)
           q = (signed long long int)(-dblq);
        else
           q = (signed long long int)dblq;

B
bellard 已提交
4014
        env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
4015 4016 4017 4018
                                /* (C0,C3,C1) <-- (q2,q1,q0) */
        env->fpus |= (q & 0x4) << (8 - 2);  /* (C0) <-- q2 */
        env->fpus |= (q & 0x2) << (14 - 1); /* (C3) <-- q1 */
        env->fpus |= (q & 0x1) << (9 - 0);  /* (C1) <-- q0 */
B
bellard 已提交
4019
    } else {
4020
        int N = 32 + (expdif % 32); /* as per AMD docs */
B
bellard 已提交
4021
        env->fpus |= 0x400;  /* C2 <-- 1 */
4022
        fptemp = pow(2.0, (double)(expdif - N));
B
bellard 已提交
4023 4024
        fpsrcop = (ST0 / ST1) / fptemp;
        /* fpsrcop = integer obtained by chopping */
4025 4026
        fpsrcop = (fpsrcop < 0.0) ?
                  -(floor(fabs(fpsrcop))) : floor(fpsrcop);
B
bellard 已提交
4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039
        ST0 -= (ST1 * fpsrcop * fptemp);
    }
}

void helper_fyl2xp1(void)
{
    CPU86_LDouble fptemp;

    fptemp = ST0;
    if ((fptemp+1.0)>0.0) {
        fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
        ST1 *= fptemp;
        fpop();
4040
    } else {
B
bellard 已提交
4041 4042 4043 4044 4045 4046 4047 4048 4049 4050
        env->fpus &= (~0x4700);
        env->fpus |= 0x400;
    }
}

void helper_fsqrt(void)
{
    CPU86_LDouble fptemp;

    fptemp = ST0;
4051
    if (fptemp<0.0) {
B
bellard 已提交
4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075
        env->fpus &= (~0x4700);  /* (C3,C2,C1,C0) <-- 0000 */
        env->fpus |= 0x400;
    }
    ST0 = sqrt(fptemp);
}

void helper_fsincos(void)
{
    CPU86_LDouble fptemp;

    fptemp = ST0;
    if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
        env->fpus |= 0x400;
    } else {
        ST0 = sin(fptemp);
        fpush();
        ST0 = cos(fptemp);
        env->fpus &= (~0x400);  /* C2 <-- 0 */
        /* the above code is for  |arg| < 2**63 only */
    }
}

void helper_frndint(void)
{
B
bellard 已提交
4076
    ST0 = floatx_round_to_int(ST0, &env->fp_status);
B
bellard 已提交
4077 4078 4079 4080
}

void helper_fscale(void)
{
4081
    ST0 = ldexp (ST0, (int)(ST1));
B
bellard 已提交
4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122
}

void helper_fsin(void)
{
    CPU86_LDouble fptemp;

    fptemp = ST0;
    if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
        env->fpus |= 0x400;
    } else {
        ST0 = sin(fptemp);
        env->fpus &= (~0x400);  /* C2 <-- 0 */
        /* the above code is for  |arg| < 2**53 only */
    }
}

void helper_fcos(void)
{
    CPU86_LDouble fptemp;

    fptemp = ST0;
    if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
        env->fpus |= 0x400;
    } else {
        ST0 = cos(fptemp);
        env->fpus &= (~0x400);  /* C2 <-- 0 */
        /* the above code is for  |arg5 < 2**63 only */
    }
}

void helper_fxam_ST0(void)
{
    CPU86_LDoubleU temp;
    int expdif;

    temp.d = ST0;

    env->fpus &= (~0x4700);  /* (C3,C2,C1,C0) <-- 0000 */
    if (SIGND(temp))
        env->fpus |= 0x200; /* C1 <-- 1 */

B
bellard 已提交
4123
    /* XXX: test fptags too */
B
bellard 已提交
4124 4125
    expdif = EXPD(temp);
    if (expdif == MAXEXPD) {
B
bellard 已提交
4126 4127 4128
#ifdef USE_X86LDOUBLE
        if (MANTD(temp) == 0x8000000000000000ULL)
#else
B
bellard 已提交
4129
        if (MANTD(temp) == 0)
B
bellard 已提交
4130
#endif
B
bellard 已提交
4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143
            env->fpus |=  0x500 /*Infinity*/;
        else
            env->fpus |=  0x100 /*NaN*/;
    } else if (expdif == 0) {
        if (MANTD(temp) == 0)
            env->fpus |=  0x4000 /*Zero*/;
        else
            env->fpus |= 0x4400 /*Denormal*/;
    } else {
        env->fpus |= 0x400;
    }
}

B
bellard 已提交
4144
void helper_fstenv(target_ulong ptr, int data32)
B
bellard 已提交
4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156
{
    int fpus, fptag, exp, i;
    uint64_t mant;
    CPU86_LDoubleU tmp;

    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
    fptag = 0;
    for (i=7; i>=0; i--) {
	fptag <<= 2;
	if (env->fptags[i]) {
            fptag |= 3;
	} else {
B
bellard 已提交
4157
            tmp.d = env->fpregs[i].d;
B
bellard 已提交
4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177
            exp = EXPD(tmp);
            mant = MANTD(tmp);
            if (exp == 0 && mant == 0) {
                /* zero */
	        fptag |= 1;
	    } else if (exp == 0 || exp == MAXEXPD
#ifdef USE_X86LDOUBLE
                       || (mant & (1LL << 63)) == 0
#endif
                       ) {
                /* NaNs, infinity, denormal */
                fptag |= 2;
            }
        }
    }
    if (data32) {
        /* 32 bit */
        stl(ptr, env->fpuc);
        stl(ptr + 4, fpus);
        stl(ptr + 8, fptag);
B
fpu fix  
bellard 已提交
4178 4179 4180 4181
        stl(ptr + 12, 0); /* fpip */
        stl(ptr + 16, 0); /* fpcs */
        stl(ptr + 20, 0); /* fpoo */
        stl(ptr + 24, 0); /* fpos */
B
bellard 已提交
4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193
    } else {
        /* 16 bit */
        stw(ptr, env->fpuc);
        stw(ptr + 2, fpus);
        stw(ptr + 4, fptag);
        stw(ptr + 6, 0);
        stw(ptr + 8, 0);
        stw(ptr + 10, 0);
        stw(ptr + 12, 0);
    }
}

B
bellard 已提交
4194
void helper_fldenv(target_ulong ptr, int data32)
B
bellard 已提交
4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209
{
    int i, fpus, fptag;

    if (data32) {
	env->fpuc = lduw(ptr);
        fpus = lduw(ptr + 4);
        fptag = lduw(ptr + 8);
    }
    else {
	env->fpuc = lduw(ptr);
        fpus = lduw(ptr + 2);
        fptag = lduw(ptr + 4);
    }
    env->fpstt = (fpus >> 11) & 7;
    env->fpus = fpus & ~0x3800;
B
fpu fix  
bellard 已提交
4210
    for(i = 0;i < 8; i++) {
B
bellard 已提交
4211 4212 4213 4214 4215
        env->fptags[i] = ((fptag & 3) == 3);
        fptag >>= 2;
    }
}

B
bellard 已提交
4216
void helper_fsave(target_ulong ptr, int data32)
B
bellard 已提交
4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243
{
    CPU86_LDouble tmp;
    int i;

    helper_fstenv(ptr, data32);

    ptr += (14 << data32);
    for(i = 0;i < 8; i++) {
        tmp = ST(i);
        helper_fstt(tmp, ptr);
        ptr += 10;
    }

    /* fninit */
    env->fpus = 0;
    env->fpstt = 0;
    env->fpuc = 0x37f;
    env->fptags[0] = 1;
    env->fptags[1] = 1;
    env->fptags[2] = 1;
    env->fptags[3] = 1;
    env->fptags[4] = 1;
    env->fptags[5] = 1;
    env->fptags[6] = 1;
    env->fptags[7] = 1;
}

B
bellard 已提交
4244
void helper_frstor(target_ulong ptr, int data32)
B
bellard 已提交
4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258
{
    CPU86_LDouble tmp;
    int i;

    helper_fldenv(ptr, data32);
    ptr += (14 << data32);

    for(i = 0;i < 8; i++) {
        tmp = helper_fldt(ptr);
        ST(i) = tmp;
        ptr += 10;
    }
}

B
bellard 已提交
4259 4260 4261 4262 4263 4264 4265 4266 4267
void helper_fxsave(target_ulong ptr, int data64)
{
    int fpus, fptag, i, nb_xmm_regs;
    CPU86_LDouble tmp;
    target_ulong addr;

    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
    fptag = 0;
    for(i = 0; i < 8; i++) {
B
bellard 已提交
4268
        fptag |= (env->fptags[i] << i);
B
bellard 已提交
4269 4270 4271
    }
    stw(ptr, env->fpuc);
    stw(ptr + 2, fpus);
B
bellard 已提交
4272
    stw(ptr + 4, fptag ^ 0xff);
B
bellard 已提交
4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284
#ifdef TARGET_X86_64
    if (data64) {
        stq(ptr + 0x08, 0); /* rip */
        stq(ptr + 0x10, 0); /* rdp */
    } else 
#endif
    {
        stl(ptr + 0x08, 0); /* eip */
        stl(ptr + 0x0c, 0); /* sel  */
        stl(ptr + 0x10, 0); /* dp */
        stl(ptr + 0x14, 0); /* sel  */
    }
B
bellard 已提交
4285 4286 4287 4288 4289 4290 4291

    addr = ptr + 0x20;
    for(i = 0;i < 8; i++) {
        tmp = ST(i);
        helper_fstt(tmp, addr);
        addr += 16;
    }
4292

B
bellard 已提交
4293
    if (env->cr[4] & CR4_OSFXSR_MASK) {
4294
        /* XXX: finish it */
B
bellard 已提交
4295
        stl(ptr + 0x18, env->mxcsr); /* mxcsr */
B
bellard 已提交
4296
        stl(ptr + 0x1c, 0x0000ffff); /* mxcsr_mask */
B
bellard 已提交
4297 4298 4299 4300
        if (env->hflags & HF_CS64_MASK)
            nb_xmm_regs = 16;
        else
            nb_xmm_regs = 8;
B
bellard 已提交
4301 4302
        addr = ptr + 0xa0;
        for(i = 0; i < nb_xmm_regs; i++) {
4303 4304
            stq(addr, env->xmm_regs[i].XMM_Q(0));
            stq(addr + 8, env->xmm_regs[i].XMM_Q(1));
B
bellard 已提交
4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317
            addr += 16;
        }
    }
}

void helper_fxrstor(target_ulong ptr, int data64)
{
    int i, fpus, fptag, nb_xmm_regs;
    CPU86_LDouble tmp;
    target_ulong addr;

    env->fpuc = lduw(ptr);
    fpus = lduw(ptr + 2);
B
bellard 已提交
4318
    fptag = lduw(ptr + 4);
B
bellard 已提交
4319 4320 4321 4322
    env->fpstt = (fpus >> 11) & 7;
    env->fpus = fpus & ~0x3800;
    fptag ^= 0xff;
    for(i = 0;i < 8; i++) {
B
bellard 已提交
4323
        env->fptags[i] = ((fptag >> i) & 1);
B
bellard 已提交
4324 4325 4326 4327 4328 4329 4330 4331 4332 4333
    }

    addr = ptr + 0x20;
    for(i = 0;i < 8; i++) {
        tmp = helper_fldt(addr);
        ST(i) = tmp;
        addr += 16;
    }

    if (env->cr[4] & CR4_OSFXSR_MASK) {
B
bellard 已提交
4334
        /* XXX: finish it */
B
bellard 已提交
4335
        env->mxcsr = ldl(ptr + 0x18);
B
bellard 已提交
4336
        //ldl(ptr + 0x1c);
B
bellard 已提交
4337 4338 4339 4340
        if (env->hflags & HF_CS64_MASK)
            nb_xmm_regs = 16;
        else
            nb_xmm_regs = 8;
B
bellard 已提交
4341 4342
        addr = ptr + 0xa0;
        for(i = 0; i < nb_xmm_regs; i++) {
4343 4344
            env->xmm_regs[i].XMM_Q(0) = ldq(addr);
            env->xmm_regs[i].XMM_Q(1) = ldq(addr + 8);
B
bellard 已提交
4345 4346 4347 4348
            addr += 16;
        }
    }
}
4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405

#ifndef USE_X86LDOUBLE

void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
{
    CPU86_LDoubleU temp;
    int e;

    temp.d = f;
    /* mantissa */
    *pmant = (MANTD(temp) << 11) | (1LL << 63);
    /* exponent + sign */
    e = EXPD(temp) - EXPBIAS + 16383;
    e |= SIGND(temp) >> 16;
    *pexp = e;
}

CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
{
    CPU86_LDoubleU temp;
    int e;
    uint64_t ll;

    /* XXX: handle overflow ? */
    e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
    e |= (upper >> 4) & 0x800; /* sign */
    ll = (mant >> 11) & ((1LL << 52) - 1);
#ifdef __arm__
    temp.l.upper = (e << 20) | (ll >> 32);
    temp.l.lower = ll;
#else
    temp.ll = ll | ((uint64_t)e << 52);
#endif
    return temp.d;
}

#else

void cpu_get_fp80(uint64_t *pmant, uint16_t *pexp, CPU86_LDouble f)
{
    CPU86_LDoubleU temp;

    temp.d = f;
    *pmant = temp.l.lower;
    *pexp = temp.l.upper;
}

CPU86_LDouble cpu_set_fp80(uint64_t mant, uint16_t upper)
{
    CPU86_LDoubleU temp;

    temp.l.upper = upper;
    temp.l.lower = mant;
    return temp.d;
}
#endif

B
bellard 已提交
4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425
#ifdef TARGET_X86_64

//#define DEBUG_MULDIV

static void add128(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
{
    *plow += a;
    /* carry test */
    if (*plow < a)
        (*phigh)++;
    *phigh += b;
}

static void neg128(uint64_t *plow, uint64_t *phigh)
{
    *plow = ~ *plow;
    *phigh = ~ *phigh;
    add128(plow, phigh, 1, 0);
}

B
bellard 已提交
4426 4427
/* return TRUE if overflow */
static int div64(uint64_t *plow, uint64_t *phigh, uint64_t b)
B
bellard 已提交
4428 4429
{
    uint64_t q, r, a1, a0;
B
bellard 已提交
4430
    int i, qb, ab;
B
bellard 已提交
4431 4432 4433 4434 4435 4436 4437 4438 4439

    a0 = *plow;
    a1 = *phigh;
    if (a1 == 0) {
        q = a0 / b;
        r = a0 % b;
        *plow = q;
        *phigh = r;
    } else {
B
bellard 已提交
4440 4441
        if (a1 >= b)
            return 1;
B
bellard 已提交
4442 4443
        /* XXX: use a better algorithm */
        for(i = 0; i < 64; i++) {
B
bellard 已提交
4444
            ab = a1 >> 63;
4445
            a1 = (a1 << 1) | (a0 >> 63);
B
bellard 已提交
4446
            if (ab || a1 >= b) {
B
bellard 已提交
4447 4448 4449 4450 4451 4452 4453
                a1 -= b;
                qb = 1;
            } else {
                qb = 0;
            }
            a0 = (a0 << 1) | qb;
        }
4454
#if defined(DEBUG_MULDIV)
B
bellard 已提交
4455
        printf("div: 0x%016" PRIx64 "%016" PRIx64 " / 0x%016" PRIx64 ": q=0x%016" PRIx64 " r=0x%016" PRIx64 "\n",
B
bellard 已提交
4456 4457 4458 4459 4460
               *phigh, *plow, b, a0, a1);
#endif
        *plow = a0;
        *phigh = a1;
    }
B
bellard 已提交
4461
    return 0;
B
bellard 已提交
4462 4463
}

B
bellard 已提交
4464 4465
/* return TRUE if overflow */
static int idiv64(uint64_t *plow, uint64_t *phigh, int64_t b)
B
bellard 已提交
4466 4467 4468 4469 4470 4471 4472 4473
{
    int sa, sb;
    sa = ((int64_t)*phigh < 0);
    if (sa)
        neg128(plow, phigh);
    sb = (b < 0);
    if (sb)
        b = -b;
B
bellard 已提交
4474 4475 4476 4477 4478
    if (div64(plow, phigh, b) != 0)
        return 1;
    if (sa ^ sb) {
        if (*plow > (1ULL << 63))
            return 1;
B
bellard 已提交
4479
        *plow = - *plow;
B
bellard 已提交
4480 4481 4482 4483
    } else {
        if (*plow >= (1ULL << 63))
            return 1;
    }
B
bellard 已提交
4484
    if (sa)
B
bellard 已提交
4485
        *phigh = - *phigh;
B
bellard 已提交
4486
    return 0;
B
bellard 已提交
4487 4488
}

4489
void helper_mulq_EAX_T0(target_ulong t0)
B
bellard 已提交
4490 4491 4492
{
    uint64_t r0, r1;

4493
    mulu64(&r0, &r1, EAX, t0);
B
bellard 已提交
4494 4495 4496 4497 4498 4499
    EAX = r0;
    EDX = r1;
    CC_DST = r0;
    CC_SRC = r1;
}

4500
void helper_imulq_EAX_T0(target_ulong t0)
B
bellard 已提交
4501 4502 4503
{
    uint64_t r0, r1;

4504
    muls64(&r0, &r1, EAX, t0);
B
bellard 已提交
4505 4506 4507
    EAX = r0;
    EDX = r1;
    CC_DST = r0;
4508
    CC_SRC = ((int64_t)r1 != ((int64_t)r0 >> 63));
B
bellard 已提交
4509 4510
}

4511
target_ulong helper_imulq_T0_T1(target_ulong t0, target_ulong t1)
B
bellard 已提交
4512 4513 4514
{
    uint64_t r0, r1;

4515
    muls64(&r0, &r1, t0, t1);
B
bellard 已提交
4516 4517
    CC_DST = r0;
    CC_SRC = ((int64_t)r1 != ((int64_t)r0 >> 63));
4518
    return r0;
B
bellard 已提交
4519 4520
}

B
bellard 已提交
4521
void helper_divq_EAX(target_ulong t0)
B
bellard 已提交
4522 4523
{
    uint64_t r0, r1;
B
bellard 已提交
4524
    if (t0 == 0) {
B
bellard 已提交
4525 4526 4527 4528
        raise_exception(EXCP00_DIVZ);
    }
    r0 = EAX;
    r1 = EDX;
B
bellard 已提交
4529
    if (div64(&r0, &r1, t0))
B
bellard 已提交
4530
        raise_exception(EXCP00_DIVZ);
B
bellard 已提交
4531 4532 4533 4534
    EAX = r0;
    EDX = r1;
}

B
bellard 已提交
4535
void helper_idivq_EAX(target_ulong t0)
B
bellard 已提交
4536 4537
{
    uint64_t r0, r1;
B
bellard 已提交
4538
    if (t0 == 0) {
B
bellard 已提交
4539 4540 4541 4542
        raise_exception(EXCP00_DIVZ);
    }
    r0 = EAX;
    r1 = EDX;
B
bellard 已提交
4543
    if (idiv64(&r0, &r1, t0))
B
bellard 已提交
4544
        raise_exception(EXCP00_DIVZ);
B
bellard 已提交
4545 4546 4547 4548 4549
    EAX = r0;
    EDX = r1;
}
#endif

B
bellard 已提交
4550 4551 4552 4553 4554 4555 4556 4557
void helper_hlt(void)
{
    env->hflags &= ~HF_INHIBIT_IRQ_MASK; /* needed if sti is just before */
    env->hflags |= HF_HALTED_MASK;
    env->exception_index = EXCP_HLT;
    cpu_loop_exit();
}

B
bellard 已提交
4558
void helper_monitor(target_ulong ptr)
B
bellard 已提交
4559
{
4560
    if ((uint32_t)ECX != 0)
B
bellard 已提交
4561 4562 4563 4564 4565 4566
        raise_exception(EXCP0D_GPF);
    /* XXX: store address ? */
}

void helper_mwait(void)
{
4567
    if ((uint32_t)ECX != 0)
B
bellard 已提交
4568 4569 4570 4571 4572 4573 4574 4575 4576 4577
        raise_exception(EXCP0D_GPF);
    /* XXX: not complete but not completely erroneous */
    if (env->cpu_index != 0 || env->next_cpu != NULL) {
        /* more than one CPU: do not sleep because another CPU may
           wake this one */
    } else {
        helper_hlt();
    }
}

B
bellard 已提交
4578
void helper_debug(void)
B
bellard 已提交
4579
{
B
bellard 已提交
4580 4581
    env->exception_index = EXCP_DEBUG;
    cpu_loop_exit();
B
bellard 已提交
4582 4583
}

B
bellard 已提交
4584
void helper_raise_interrupt(int intno, int next_eip_addend)
B
bellard 已提交
4585
{
B
bellard 已提交
4586
    raise_interrupt(intno, 1, 0, next_eip_addend);
B
bellard 已提交
4587 4588
}

B
bellard 已提交
4589
void helper_raise_exception(int exception_index)
B
bellard 已提交
4590
{
B
bellard 已提交
4591 4592
    raise_exception(exception_index);
}
B
bellard 已提交
4593

B
bellard 已提交
4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615
void helper_cli(void)
{
    env->eflags &= ~IF_MASK;
}

void helper_sti(void)
{
    env->eflags |= IF_MASK;
}

#if 0
/* vm86plus instructions */
void helper_cli_vm(void)
{
    env->eflags &= ~VIF_MASK;
}

void helper_sti_vm(void)
{
    env->eflags |= VIF_MASK;
    if (env->eflags & VIP_MASK) {
        raise_exception(EXCP0D_GPF);
B
bellard 已提交
4616
    }
B
bellard 已提交
4617
}
B
bellard 已提交
4618
#endif
B
bellard 已提交
4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629

void helper_set_inhibit_irq(void)
{
    env->hflags |= HF_INHIBIT_IRQ_MASK;
}

void helper_reset_inhibit_irq(void)
{
    env->hflags &= ~HF_INHIBIT_IRQ_MASK;
}

4630
void helper_boundw(target_ulong a0, int v)
B
bellard 已提交
4631
{
4632 4633 4634 4635
    int low, high;
    low = ldsw(a0);
    high = ldsw(a0 + 2);
    v = (int16_t)v;
B
bellard 已提交
4636 4637 4638 4639 4640 4641
    if (v < low || v > high) {
        raise_exception(EXCP05_BOUND);
    }
    FORCE_RET();
}

4642
void helper_boundl(target_ulong a0, int v)
B
bellard 已提交
4643
{
4644 4645 4646
    int low, high;
    low = ldl(a0);
    high = ldl(a0 + 4);
B
bellard 已提交
4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660
    if (v < low || v > high) {
        raise_exception(EXCP05_BOUND);
    }
    FORCE_RET();
}

static float approx_rsqrt(float a)
{
    return 1.0 / sqrt(a);
}

static float approx_rcp(float a)
{
    return 1.0 / a;
B
bellard 已提交
4661
}
B
bellard 已提交
4662

4663
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
4664 4665 4666

#define MMUSUFFIX _mmu

B
bellard 已提交
4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678
#define SHIFT 0
#include "softmmu_template.h"

#define SHIFT 1
#include "softmmu_template.h"

#define SHIFT 2
#include "softmmu_template.h"

#define SHIFT 3
#include "softmmu_template.h"

B
bellard 已提交
4679 4680 4681 4682 4683 4684
#endif

/* try to fill the TLB and return an exception if error. If retaddr is
   NULL, it means that the function was called in C code (i.e. not
   from generated code or from helper.c) */
/* XXX: fix it to restore all registers */
4685
void tlb_fill(target_ulong addr, int is_write, int mmu_idx, void *retaddr)
B
bellard 已提交
4686 4687 4688 4689
{
    TranslationBlock *tb;
    int ret;
    unsigned long pc;
B
bellard 已提交
4690 4691 4692 4693 4694 4695 4696
    CPUX86State *saved_env;

    /* XXX: hack to restore env in all cases, even if not called from
       generated code */
    saved_env = env;
    env = cpu_single_env;

4697
    ret = cpu_x86_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
B
bellard 已提交
4698
    if (ret) {
B
bellard 已提交
4699 4700 4701 4702 4703 4704 4705
        if (retaddr) {
            /* now we have a real cpu fault */
            pc = (unsigned long)retaddr;
            tb = tb_find_pc(pc);
            if (tb) {
                /* the PC is inside the translated code. It means that we have
                   a virtual CPU fault */
B
bellard 已提交
4706
                cpu_restore_state(tb, env, pc, NULL);
B
bellard 已提交
4707
            }
B
bellard 已提交
4708
        }
B
bellard 已提交
4709
        if (retaddr)
B
bellard 已提交
4710
            raise_exception_err(env->exception_index, env->error_code);
B
bellard 已提交
4711
        else
B
bellard 已提交
4712
            raise_exception_err_norestore(env->exception_index, env->error_code);
B
bellard 已提交
4713
    }
B
bellard 已提交
4714
    env = saved_env;
B
bellard 已提交
4715
}
T
ths 已提交
4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731


/* Secure Virtual Machine helpers */

void helper_stgi(void)
{
    env->hflags |= HF_GIF_MASK;
}

void helper_clgi(void)
{
    env->hflags &= ~HF_GIF_MASK;
}

#if defined(CONFIG_USER_ONLY)

4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753
void helper_vmrun(void) 
{ 
}
void helper_vmmcall(void) 
{ 
}
void helper_vmload(void) 
{ 
}
void helper_vmsave(void) 
{ 
}
void helper_skinit(void) 
{ 
}
void helper_invlpga(void) 
{ 
}
void helper_vmexit(uint32_t exit_code, uint64_t exit_info_1) 
{ 
}
void helper_svm_check_intercept_param(uint32_t type, uint64_t param)
T
ths 已提交
4754 4755 4756
{
}

4757 4758 4759 4760
void helper_svm_check_io(uint32_t port, uint32_t param, 
                         uint32_t next_eip_addend)
{
}
T
ths 已提交
4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778
#else

static inline uint32_t
vmcb2cpu_attrib(uint16_t vmcb_attrib, uint32_t vmcb_base, uint32_t vmcb_limit)
{
    return    ((vmcb_attrib & 0x00ff) << 8)          /* Type, S, DPL, P */
	    | ((vmcb_attrib & 0x0f00) << 12)         /* AVL, L, DB, G */
	    | ((vmcb_base >> 16) & 0xff)             /* Base 23-16 */
	    | (vmcb_base & 0xff000000)               /* Base 31-24 */
	    | (vmcb_limit & 0xf0000);                /* Limit 19-16 */
}

static inline uint16_t cpu2vmcb_attrib(uint32_t cpu_attrib)
{
    return    ((cpu_attrib >> 8) & 0xff)             /* Type, S, DPL, P */
	    | ((cpu_attrib & 0xf00000) >> 12);       /* AVL, L, DB, G */
}

B
bellard 已提交
4779
void helper_vmrun(void)
T
ths 已提交
4780
{
B
bellard 已提交
4781
    target_ulong addr;
T
ths 已提交
4782 4783 4784
    uint32_t event_inj;
    uint32_t int_ctl;

B
bellard 已提交
4785
    addr = EAX;
T
ths 已提交
4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844
    if (loglevel & CPU_LOG_TB_IN_ASM)
        fprintf(logfile,"vmrun! " TARGET_FMT_lx "\n", addr);

    env->vm_vmcb = addr;

    /* save the current CPU state in the hsave page */
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.gdtr.base), env->gdt.base);
    stl_phys(env->vm_hsave + offsetof(struct vmcb, save.gdtr.limit), env->gdt.limit);

    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.idtr.base), env->idt.base);
    stl_phys(env->vm_hsave + offsetof(struct vmcb, save.idtr.limit), env->idt.limit);

    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr0), env->cr[0]);
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr2), env->cr[2]);
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr3), env->cr[3]);
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr4), env->cr[4]);
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr8), env->cr[8]);
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.dr6), env->dr[6]);
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.dr7), env->dr[7]);

    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.efer), env->efer);
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.rflags), compute_eflags());

    SVM_SAVE_SEG(env->vm_hsave, segs[R_ES], es);
    SVM_SAVE_SEG(env->vm_hsave, segs[R_CS], cs);
    SVM_SAVE_SEG(env->vm_hsave, segs[R_SS], ss);
    SVM_SAVE_SEG(env->vm_hsave, segs[R_DS], ds);

    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.rip), EIP);
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.rsp), ESP);
    stq_phys(env->vm_hsave + offsetof(struct vmcb, save.rax), EAX);

    /* load the interception bitmaps so we do not need to access the
       vmcb in svm mode */
    /* We shift all the intercept bits so we can OR them with the TB
       flags later on */
    env->intercept            = (ldq_phys(env->vm_vmcb + offsetof(struct vmcb, control.intercept)) << INTERCEPT_INTR) | INTERCEPT_SVM_MASK;
    env->intercept_cr_read    = lduw_phys(env->vm_vmcb + offsetof(struct vmcb, control.intercept_cr_read));
    env->intercept_cr_write   = lduw_phys(env->vm_vmcb + offsetof(struct vmcb, control.intercept_cr_write));
    env->intercept_dr_read    = lduw_phys(env->vm_vmcb + offsetof(struct vmcb, control.intercept_dr_read));
    env->intercept_dr_write   = lduw_phys(env->vm_vmcb + offsetof(struct vmcb, control.intercept_dr_write));
    env->intercept_exceptions = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.intercept_exceptions));

    env->gdt.base  = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.gdtr.base));
    env->gdt.limit = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, save.gdtr.limit));

    env->idt.base  = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.idtr.base));
    env->idt.limit = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, save.idtr.limit));

    /* clear exit_info_2 so we behave like the real hardware */
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2), 0);

    cpu_x86_update_cr0(env, ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr0)));
    cpu_x86_update_cr4(env, ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr4)));
    cpu_x86_update_cr3(env, ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr3)));
    env->cr[2] = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr2));
    int_ctl = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_ctl));
    if (int_ctl & V_INTR_MASKING_MASK) {
        env->cr[8] = int_ctl & V_TPR_MASK;
4845
        cpu_set_apic_tpr(env, env->cr[8]);
T
ths 已提交
4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902
        if (env->eflags & IF_MASK)
            env->hflags |= HF_HIF_MASK;
    }

#ifdef TARGET_X86_64
    env->efer = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.efer));
    env->hflags &= ~HF_LMA_MASK;
    if (env->efer & MSR_EFER_LMA)
       env->hflags |= HF_LMA_MASK;
#endif
    env->eflags = 0;
    load_eflags(ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rflags)),
                ~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK));
    CC_OP = CC_OP_EFLAGS;
    CC_DST = 0xffffffff;

    SVM_LOAD_SEG(env->vm_vmcb, ES, es);
    SVM_LOAD_SEG(env->vm_vmcb, CS, cs);
    SVM_LOAD_SEG(env->vm_vmcb, SS, ss);
    SVM_LOAD_SEG(env->vm_vmcb, DS, ds);

    EIP = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rip));
    env->eip = EIP;
    ESP = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rsp));
    EAX = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rax));
    env->dr[7] = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.dr7));
    env->dr[6] = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, save.dr6));
    cpu_x86_set_cpl(env, ldub_phys(env->vm_vmcb + offsetof(struct vmcb, save.cpl)));

    /* FIXME: guest state consistency checks */

    switch(ldub_phys(env->vm_vmcb + offsetof(struct vmcb, control.tlb_ctl))) {
        case TLB_CONTROL_DO_NOTHING:
            break;
        case TLB_CONTROL_FLUSH_ALL_ASID:
            /* FIXME: this is not 100% correct but should work for now */
            tlb_flush(env, 1);
        break;
    }

    helper_stgi();

    /* maybe we need to inject an event */
    event_inj = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj));
    if (event_inj & SVM_EVTINJ_VALID) {
        uint8_t vector = event_inj & SVM_EVTINJ_VEC_MASK;
        uint16_t valid_err = event_inj & SVM_EVTINJ_VALID_ERR;
        uint32_t event_inj_err = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj_err));
        stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.event_inj), event_inj & ~SVM_EVTINJ_VALID);

        if (loglevel & CPU_LOG_TB_IN_ASM)
            fprintf(logfile, "Injecting(%#hx): ", valid_err);
        /* FIXME: need to implement valid_err */
        switch (event_inj & SVM_EVTINJ_TYPE_MASK) {
        case SVM_EVTINJ_TYPE_INTR:
                env->exception_index = vector;
                env->error_code = event_inj_err;
4903
                env->exception_is_int = 0;
T
ths 已提交
4904 4905 4906 4907 4908 4909 4910
                env->exception_next_eip = -1;
                if (loglevel & CPU_LOG_TB_IN_ASM)
                    fprintf(logfile, "INTR");
                break;
        case SVM_EVTINJ_TYPE_NMI:
                env->exception_index = vector;
                env->error_code = event_inj_err;
4911
                env->exception_is_int = 0;
T
ths 已提交
4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935
                env->exception_next_eip = EIP;
                if (loglevel & CPU_LOG_TB_IN_ASM)
                    fprintf(logfile, "NMI");
                break;
        case SVM_EVTINJ_TYPE_EXEPT:
                env->exception_index = vector;
                env->error_code = event_inj_err;
                env->exception_is_int = 0;
                env->exception_next_eip = -1;
                if (loglevel & CPU_LOG_TB_IN_ASM)
                    fprintf(logfile, "EXEPT");
                break;
        case SVM_EVTINJ_TYPE_SOFT:
                env->exception_index = vector;
                env->error_code = event_inj_err;
                env->exception_is_int = 1;
                env->exception_next_eip = EIP;
                if (loglevel & CPU_LOG_TB_IN_ASM)
                    fprintf(logfile, "SOFT");
                break;
        }
        if (loglevel & CPU_LOG_TB_IN_ASM)
            fprintf(logfile, " %#x %#x\n", env->exception_index, env->error_code);
    }
T
ths 已提交
4936
    if ((int_ctl & V_IRQ_MASK) || (env->intercept & INTERCEPT_VINTR)) {
T
ths 已提交
4937
        env->interrupt_request |= CPU_INTERRUPT_VIRQ;
T
ths 已提交
4938
    }
T
ths 已提交
4939 4940 4941 4942 4943 4944 4945 4946 4947 4948

    cpu_loop_exit();
}

void helper_vmmcall(void)
{
    if (loglevel & CPU_LOG_TB_IN_ASM)
        fprintf(logfile,"vmmcall!\n");
}

B
bellard 已提交
4949
void helper_vmload(void)
T
ths 已提交
4950
{
B
bellard 已提交
4951 4952
    target_ulong addr;
    addr = EAX;
T
ths 已提交
4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974
    if (loglevel & CPU_LOG_TB_IN_ASM)
        fprintf(logfile,"vmload! " TARGET_FMT_lx "\nFS: %016" PRIx64 " | " TARGET_FMT_lx "\n",
                addr, ldq_phys(addr + offsetof(struct vmcb, save.fs.base)),
                env->segs[R_FS].base);

    SVM_LOAD_SEG2(addr, segs[R_FS], fs);
    SVM_LOAD_SEG2(addr, segs[R_GS], gs);
    SVM_LOAD_SEG2(addr, tr, tr);
    SVM_LOAD_SEG2(addr, ldt, ldtr);

#ifdef TARGET_X86_64
    env->kernelgsbase = ldq_phys(addr + offsetof(struct vmcb, save.kernel_gs_base));
    env->lstar = ldq_phys(addr + offsetof(struct vmcb, save.lstar));
    env->cstar = ldq_phys(addr + offsetof(struct vmcb, save.cstar));
    env->fmask = ldq_phys(addr + offsetof(struct vmcb, save.sfmask));
#endif
    env->star = ldq_phys(addr + offsetof(struct vmcb, save.star));
    env->sysenter_cs = ldq_phys(addr + offsetof(struct vmcb, save.sysenter_cs));
    env->sysenter_esp = ldq_phys(addr + offsetof(struct vmcb, save.sysenter_esp));
    env->sysenter_eip = ldq_phys(addr + offsetof(struct vmcb, save.sysenter_eip));
}

B
bellard 已提交
4975
void helper_vmsave(void)
T
ths 已提交
4976
{
B
bellard 已提交
4977 4978
    target_ulong addr;
    addr = EAX;
T
ths 已提交
4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011
    if (loglevel & CPU_LOG_TB_IN_ASM)
        fprintf(logfile,"vmsave! " TARGET_FMT_lx "\nFS: %016" PRIx64 " | " TARGET_FMT_lx "\n",
                addr, ldq_phys(addr + offsetof(struct vmcb, save.fs.base)),
                env->segs[R_FS].base);

    SVM_SAVE_SEG(addr, segs[R_FS], fs);
    SVM_SAVE_SEG(addr, segs[R_GS], gs);
    SVM_SAVE_SEG(addr, tr, tr);
    SVM_SAVE_SEG(addr, ldt, ldtr);

#ifdef TARGET_X86_64
    stq_phys(addr + offsetof(struct vmcb, save.kernel_gs_base), env->kernelgsbase);
    stq_phys(addr + offsetof(struct vmcb, save.lstar), env->lstar);
    stq_phys(addr + offsetof(struct vmcb, save.cstar), env->cstar);
    stq_phys(addr + offsetof(struct vmcb, save.sfmask), env->fmask);
#endif
    stq_phys(addr + offsetof(struct vmcb, save.star), env->star);
    stq_phys(addr + offsetof(struct vmcb, save.sysenter_cs), env->sysenter_cs);
    stq_phys(addr + offsetof(struct vmcb, save.sysenter_esp), env->sysenter_esp);
    stq_phys(addr + offsetof(struct vmcb, save.sysenter_eip), env->sysenter_eip);
}

void helper_skinit(void)
{
    if (loglevel & CPU_LOG_TB_IN_ASM)
        fprintf(logfile,"skinit!\n");
}

void helper_invlpga(void)
{
    tlb_flush(env, 0);
}

5012
void helper_svm_check_intercept_param(uint32_t type, uint64_t param)
T
ths 已提交
5013 5014 5015 5016
{
    switch(type) {
    case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR0 + 8:
        if (INTERCEPTEDw(_cr_read, (1 << (type - SVM_EXIT_READ_CR0)))) {
5017
            helper_vmexit(type, param);
T
ths 已提交
5018 5019 5020 5021
        }
        break;
    case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR0 + 8:
        if (INTERCEPTEDw(_dr_read, (1 << (type - SVM_EXIT_READ_DR0)))) {
5022
            helper_vmexit(type, param);
T
ths 已提交
5023 5024 5025 5026
        }
        break;
    case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR0 + 8:
        if (INTERCEPTEDw(_cr_write, (1 << (type - SVM_EXIT_WRITE_CR0)))) {
5027
            helper_vmexit(type, param);
T
ths 已提交
5028 5029 5030 5031
        }
        break;
    case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR0 + 8:
        if (INTERCEPTEDw(_dr_write, (1 << (type - SVM_EXIT_WRITE_DR0)))) {
5032
            helper_vmexit(type, param);
T
ths 已提交
5033 5034 5035 5036
        }
        break;
    case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 16:
        if (INTERCEPTEDl(_exceptions, (1 << (type - SVM_EXIT_EXCP_BASE)))) {
5037
            helper_vmexit(type, param);
T
ths 已提交
5038 5039 5040 5041 5042 5043 5044 5045 5046
        }
        break;
    case SVM_EXIT_IOIO:
        break;

    case SVM_EXIT_MSR:
        if (INTERCEPTED(1ULL << INTERCEPT_MSR_PROT)) {
            /* FIXME: this should be read in at vmrun (faster this way?) */
            uint64_t addr = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, control.msrpm_base_pa));
5047
            uint32_t t0, t1;
T
ths 已提交
5048 5049
            switch((uint32_t)ECX) {
            case 0 ... 0x1fff:
5050 5051
                t0 = (ECX * 2) % 8;
                t1 = ECX / 8;
T
ths 已提交
5052 5053
                break;
            case 0xc0000000 ... 0xc0001fff:
5054 5055 5056
                t0 = (8192 + ECX - 0xc0000000) * 2;
                t1 = (t0 / 8);
                t0 %= 8;
T
ths 已提交
5057 5058
                break;
            case 0xc0010000 ... 0xc0011fff:
5059 5060 5061
                t0 = (16384 + ECX - 0xc0010000) * 2;
                t1 = (t0 / 8);
                t0 %= 8;
T
ths 已提交
5062 5063
                break;
            default:
5064 5065 5066 5067
                helper_vmexit(type, param);
                t0 = 0;
                t1 = 0;
                break;
T
ths 已提交
5068
            }
5069 5070
            if (ldub_phys(addr + t1) & ((1 << param) << t0))
                helper_vmexit(type, param);
T
ths 已提交
5071 5072 5073 5074
        }
        break;
    default:
        if (INTERCEPTED((1ULL << ((type - SVM_EXIT_INTR) + INTERCEPT_INTR)))) {
5075
            helper_vmexit(type, param);
T
ths 已提交
5076 5077 5078 5079 5080
        }
        break;
    }
}

5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098
void helper_svm_check_io(uint32_t port, uint32_t param, 
                         uint32_t next_eip_addend)
{
    if (INTERCEPTED(1ULL << INTERCEPT_IOIO_PROT)) {
        /* FIXME: this should be read in at vmrun (faster this way?) */
        uint64_t addr = ldq_phys(env->vm_vmcb + offsetof(struct vmcb, control.iopm_base_pa));
        uint16_t mask = (1 << ((param >> 4) & 7)) - 1;
        if(lduw_phys(addr + port / 8) & (mask << (port & 7))) {
            /* next EIP */
            stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2), 
                     env->eip + next_eip_addend);
            helper_vmexit(SVM_EXIT_IOIO, param | (port << 16));
        }
    }
}

/* Note: currently only 32 bits of exit_code are used */
void helper_vmexit(uint32_t exit_code, uint64_t exit_info_1)
T
ths 已提交
5099 5100 5101 5102
{
    uint32_t int_ctl;

    if (loglevel & CPU_LOG_TB_IN_ASM)
5103
        fprintf(logfile,"vmexit(%08x, %016" PRIx64 ", %016" PRIx64 ", " TARGET_FMT_lx ")!\n",
T
ths 已提交
5104 5105 5106 5107
                exit_code, exit_info_1,
                ldq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2)),
                EIP);

T
ths 已提交
5108 5109 5110 5111 5112 5113 5114
    if(env->hflags & HF_INHIBIT_IRQ_MASK) {
        stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_state), SVM_INTERRUPT_SHADOW_MASK);
        env->hflags &= ~HF_INHIBIT_IRQ_MASK;
    } else {
        stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_state), 0);
    }

T
ths 已提交
5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161
    /* Save the VM state in the vmcb */
    SVM_SAVE_SEG(env->vm_vmcb, segs[R_ES], es);
    SVM_SAVE_SEG(env->vm_vmcb, segs[R_CS], cs);
    SVM_SAVE_SEG(env->vm_vmcb, segs[R_SS], ss);
    SVM_SAVE_SEG(env->vm_vmcb, segs[R_DS], ds);

    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.gdtr.base), env->gdt.base);
    stl_phys(env->vm_vmcb + offsetof(struct vmcb, save.gdtr.limit), env->gdt.limit);

    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.idtr.base), env->idt.base);
    stl_phys(env->vm_vmcb + offsetof(struct vmcb, save.idtr.limit), env->idt.limit);

    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.efer), env->efer);
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr0), env->cr[0]);
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr2), env->cr[2]);
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr3), env->cr[3]);
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.cr4), env->cr[4]);

    if ((int_ctl = ldl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_ctl))) & V_INTR_MASKING_MASK) {
        int_ctl &= ~V_TPR_MASK;
        int_ctl |= env->cr[8] & V_TPR_MASK;
        stl_phys(env->vm_vmcb + offsetof(struct vmcb, control.int_ctl), int_ctl);
    }

    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rflags), compute_eflags());
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rip), env->eip);
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rsp), ESP);
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.rax), EAX);
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.dr7), env->dr[7]);
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, save.dr6), env->dr[6]);
    stb_phys(env->vm_vmcb + offsetof(struct vmcb, save.cpl), env->hflags & HF_CPL_MASK);

    /* Reload the host state from vm_hsave */
    env->hflags &= ~HF_HIF_MASK;
    env->intercept = 0;
    env->intercept_exceptions = 0;
    env->interrupt_request &= ~CPU_INTERRUPT_VIRQ;

    env->gdt.base  = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.gdtr.base));
    env->gdt.limit = ldl_phys(env->vm_hsave + offsetof(struct vmcb, save.gdtr.limit));

    env->idt.base  = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.idtr.base));
    env->idt.limit = ldl_phys(env->vm_hsave + offsetof(struct vmcb, save.idtr.limit));

    cpu_x86_update_cr0(env, ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr0)) | CR0_PE_MASK);
    cpu_x86_update_cr4(env, ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr4)));
    cpu_x86_update_cr3(env, ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr3)));
5162
    if (int_ctl & V_INTR_MASKING_MASK) {
T
ths 已提交
5163
        env->cr[8] = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.cr8));
5164 5165
        cpu_set_apic_tpr(env, env->cr[8]);
    }
T
ths 已提交
5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192
    /* we need to set the efer after the crs so the hidden flags get set properly */
#ifdef TARGET_X86_64
    env->efer  = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.efer));
    env->hflags &= ~HF_LMA_MASK;
    if (env->efer & MSR_EFER_LMA)
       env->hflags |= HF_LMA_MASK;
#endif

    env->eflags = 0;
    load_eflags(ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.rflags)),
                ~(CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C | DF_MASK));
    CC_OP = CC_OP_EFLAGS;

    SVM_LOAD_SEG(env->vm_hsave, ES, es);
    SVM_LOAD_SEG(env->vm_hsave, CS, cs);
    SVM_LOAD_SEG(env->vm_hsave, SS, ss);
    SVM_LOAD_SEG(env->vm_hsave, DS, ds);

    EIP = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.rip));
    ESP = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.rsp));
    EAX = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.rax));

    env->dr[6] = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.dr6));
    env->dr[7] = ldq_phys(env->vm_hsave + offsetof(struct vmcb, save.dr7));

    /* other setups */
    cpu_x86_set_cpl(env, 0);
5193
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_code), exit_code);
T
ths 已提交
5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227
    stq_phys(env->vm_vmcb + offsetof(struct vmcb, control.exit_info_1), exit_info_1);

    helper_clgi();
    /* FIXME: Resets the current ASID register to zero (host ASID). */

    /* Clears the V_IRQ and V_INTR_MASKING bits inside the processor. */

    /* Clears the TSC_OFFSET inside the processor. */

    /* If the host is in PAE mode, the processor reloads the host's PDPEs
       from the page table indicated the host's CR3. If the PDPEs contain
       illegal state, the processor causes a shutdown. */

    /* Forces CR0.PE = 1, RFLAGS.VM = 0. */
    env->cr[0] |= CR0_PE_MASK;
    env->eflags &= ~VM_MASK;

    /* Disables all breakpoints in the host DR7 register. */

    /* Checks the reloaded host state for consistency. */

    /* If the host's rIP reloaded by #VMEXIT is outside the limit of the
       host's code segment or non-canonical (in the case of long mode), a
       #GP fault is delivered inside the host.) */

    /* remove any pending exception */
    env->exception_index = -1;
    env->error_code = 0;
    env->old_exception = -1;

    cpu_loop_exit();
}

#endif
B
bellard 已提交
5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256

/* MMX/SSE */
/* XXX: optimize by storing fptt and fptags in the static cpu state */
void helper_enter_mmx(void)
{
    env->fpstt = 0;
    *(uint32_t *)(env->fptags) = 0;
    *(uint32_t *)(env->fptags + 4) = 0;
}

void helper_emms(void)
{
    /* set to empty state */
    *(uint32_t *)(env->fptags) = 0x01010101;
    *(uint32_t *)(env->fptags + 4) = 0x01010101;
}

/* XXX: suppress */
void helper_movq(uint64_t *d, uint64_t *s)
{
    *d = *s;
}

#define SHIFT 0
#include "ops_sse.h"

#define SHIFT 1
#include "ops_sse.h"

5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275
#define SHIFT 0
#include "helper_template.h"
#undef SHIFT

#define SHIFT 1
#include "helper_template.h"
#undef SHIFT

#define SHIFT 2
#include "helper_template.h"
#undef SHIFT

#ifdef TARGET_X86_64

#define SHIFT 3
#include "helper_template.h"
#undef SHIFT

#endif
B
bellard 已提交
5276

B
bellard 已提交
5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307
/* bit operations */
target_ulong helper_bsf(target_ulong t0)
{
    int count;
    target_ulong res;

    res = t0;
    count = 0;
    while ((res & 1) == 0) {
        count++;
        res >>= 1;
    }
    return count;
}

target_ulong helper_bsr(target_ulong t0)
{
    int count;
    target_ulong res, mask;
    
    res = t0;
    count = TARGET_LONG_BITS - 1;
    mask = (target_ulong)1 << (TARGET_LONG_BITS - 1);
    while ((res & mask) == 0) {
        count--;
        res <<= 1;
    }
    return count;
}


B
bellard 已提交
5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385
static int compute_all_eflags(void)
{
    return CC_SRC;
}

static int compute_c_eflags(void)
{
    return CC_SRC & CC_C;
}

CCTable cc_table[CC_OP_NB] = {
    [CC_OP_DYNAMIC] = { /* should never happen */ },

    [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },

    [CC_OP_MULB] = { compute_all_mulb, compute_c_mull },
    [CC_OP_MULW] = { compute_all_mulw, compute_c_mull },
    [CC_OP_MULL] = { compute_all_mull, compute_c_mull },

    [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
    [CC_OP_ADDW] = { compute_all_addw, compute_c_addw  },
    [CC_OP_ADDL] = { compute_all_addl, compute_c_addl  },

    [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
    [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw  },
    [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl  },

    [CC_OP_SUBB] = { compute_all_subb, compute_c_subb  },
    [CC_OP_SUBW] = { compute_all_subw, compute_c_subw  },
    [CC_OP_SUBL] = { compute_all_subl, compute_c_subl  },

    [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb  },
    [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw  },
    [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl  },

    [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
    [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
    [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },

    [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
    [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
    [CC_OP_INCL] = { compute_all_incl, compute_c_incl },

    [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
    [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
    [CC_OP_DECL] = { compute_all_decl, compute_c_incl },

    [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
    [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
    [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },

    [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
    [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
    [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },

#ifdef TARGET_X86_64
    [CC_OP_MULQ] = { compute_all_mulq, compute_c_mull },

    [CC_OP_ADDQ] = { compute_all_addq, compute_c_addq  },

    [CC_OP_ADCQ] = { compute_all_adcq, compute_c_adcq  },

    [CC_OP_SUBQ] = { compute_all_subq, compute_c_subq  },

    [CC_OP_SBBQ] = { compute_all_sbbq, compute_c_sbbq  },

    [CC_OP_LOGICQ] = { compute_all_logicq, compute_c_logicq },

    [CC_OP_INCQ] = { compute_all_incq, compute_c_incl },

    [CC_OP_DECQ] = { compute_all_decq, compute_c_incl },

    [CC_OP_SHLQ] = { compute_all_shlq, compute_c_shlq },

    [CC_OP_SARQ] = { compute_all_sarq, compute_c_sarl },
#endif
};