translate.c 290.6 KB
Newer Older
B
bellard 已提交
1 2
/*
 *  i386 translation
3
 *
B
bellard 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 *  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
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
B
bellard 已提交
18
 */
P
Peter Maydell 已提交
19
#include "qemu/osdep.h"
B
bellard 已提交
20

21
#include "qemu/host-utils.h"
B
bellard 已提交
22
#include "cpu.h"
23
#include "disas/disas.h"
24
#include "exec/exec-all.h"
B
bellard 已提交
25
#include "tcg-op.h"
P
Paolo Bonzini 已提交
26
#include "exec/cpu_ldst.h"
27
#include "exec/translator.h"
B
bellard 已提交
28

29 30
#include "exec/helper-proto.h"
#include "exec/helper-gen.h"
P
pbrook 已提交
31

32
#include "trace-tcg.h"
33
#include "exec/log.h"
34

B
bellard 已提交
35 36 37 38 39
#define PREFIX_REPZ   0x01
#define PREFIX_REPNZ  0x02
#define PREFIX_LOCK   0x04
#define PREFIX_DATA   0x08
#define PREFIX_ADR    0x10
40
#define PREFIX_VEX    0x20
B
bellard 已提交
41

B
bellard 已提交
42 43 44 45 46 47 48 49 50 51
#ifdef TARGET_X86_64
#define CODE64(s) ((s)->code64)
#define REX_X(s) ((s)->rex_x)
#define REX_B(s) ((s)->rex_b)
#else
#define CODE64(s) 0
#define REX_X(s) 0
#define REX_B(s) 0
#endif

52 53 54 55 56 57 58 59
#ifdef TARGET_X86_64
# define ctztl  ctz64
# define clztl  clz64
#else
# define ctztl  ctz32
# define clztl  clz32
#endif

60
/* For a switch indexed by MODRM, match all memory operands for a given OP.  */
61
#define CASE_MODRM_MEM_OP(OP) \
62 63 64 65
    case (0 << 6) | (OP << 3) | 0 ... (0 << 6) | (OP << 3) | 7: \
    case (1 << 6) | (OP << 3) | 0 ... (1 << 6) | (OP << 3) | 7: \
    case (2 << 6) | (OP << 3) | 0 ... (2 << 6) | (OP << 3) | 7

66 67 68 69 70 71
#define CASE_MODRM_OP(OP) \
    case (0 << 6) | (OP << 3) | 0 ... (0 << 6) | (OP << 3) | 7: \
    case (1 << 6) | (OP << 3) | 0 ... (1 << 6) | (OP << 3) | 7: \
    case (2 << 6) | (OP << 3) | 0 ... (2 << 6) | (OP << 3) | 7: \
    case (3 << 6) | (OP << 3) | 0 ... (3 << 6) | (OP << 3) | 7

B
bellard 已提交
72 73 74
//#define MACRO_TEST   1

/* global register indexes */
75
static TCGv cpu_A0;
76
static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT;
P
pbrook 已提交
77
static TCGv_i32 cpu_cc_op;
78
static TCGv cpu_regs[CPU_NB_REGS];
79
static TCGv cpu_seg_base[6];
R
Richard Henderson 已提交
80 81
static TCGv_i64 cpu_bndl[4];
static TCGv_i64 cpu_bndu[4];
82
/* local temps */
83
static TCGv cpu_T0, cpu_T1;
B
bellard 已提交
84
/* local register indexes (only used inside old micro ops) */
P
pbrook 已提交
85 86 87 88
static TCGv cpu_tmp0, cpu_tmp4;
static TCGv_ptr cpu_ptr0, cpu_ptr1;
static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
static TCGv_i64 cpu_tmp1_i64;
B
bellard 已提交
89

90
#include "exec/gen-icount.h"
P
pbrook 已提交
91

B
bellard 已提交
92 93
#ifdef TARGET_X86_64
static int x86_64_hregs;
B
bellard 已提交
94 95
#endif

B
bellard 已提交
96
typedef struct DisasContext {
97 98
    DisasContextBase base;

B
bellard 已提交
99 100 101
    /* current insn context */
    int override; /* -1 if no override */
    int prefix;
102
    TCGMemOp aflag;
103
    TCGMemOp dflag;
104
    target_ulong pc_start;
B
bellard 已提交
105
    target_ulong pc; /* pc = eip + cs_base */
B
bellard 已提交
106
    /* current block context */
B
bellard 已提交
107
    target_ulong cs_base; /* base of CS segment */
B
bellard 已提交
108 109
    int pe;     /* protected mode */
    int code32; /* 32 bit code segment */
B
bellard 已提交
110 111 112 113 114
#ifdef TARGET_X86_64
    int lma;    /* long mode active */
    int code64; /* 64 bit code segment */
    int rex_x, rex_b;
#endif
115 116
    int vex_l;  /* vex vector length */
    int vex_v;  /* vex vvvv register, without 1's compliment.  */
B
bellard 已提交
117
    int ss32;   /* 32 bit stack segment */
118
    CCOp cc_op;  /* current CC operation */
119
    bool cc_op_dirty;
B
bellard 已提交
120 121 122 123 124 125 126
    int addseg; /* non zero if either DS/ES/SS have a non zero base */
    int f_st;   /* currently unused */
    int vm86;   /* vm86 mode */
    int cpl;
    int iopl;
    int tf;     /* TF cpu flag */
    int jmp_opt; /* use direct block chaining for direct jumps */
127
    int repz_opt; /* optimize jumps within repz instructions */
B
bellard 已提交
128
    int mem_index; /* select memory access functions */
129
    uint64_t flags; /* all execution flags */
B
bellard 已提交
130
    int popl_esp_hack; /* for correct popl with esp base handling */
B
bellard 已提交
131 132
    int rip_offset; /* only used in x86_64, but left for simplicity */
    int cpuid_features;
B
bellard 已提交
133
    int cpuid_ext_features;
134
    int cpuid_ext2_features;
B
bellard 已提交
135
    int cpuid_ext3_features;
H
H. Peter Anvin 已提交
136
    int cpuid_7_0_ebx_features;
137
    int cpuid_xsave_features;
138
    sigjmp_buf jmpbuf;
B
bellard 已提交
139 140 141
} DisasContext;

static void gen_eob(DisasContext *s);
142
static void gen_jr(DisasContext *s, TCGv dest);
B
bellard 已提交
143 144
static void gen_jmp(DisasContext *s, target_ulong eip);
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
145
static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d);
B
bellard 已提交
146 147 148

/* i386 arith/logic operations */
enum {
149 150 151
    OP_ADDL,
    OP_ORL,
    OP_ADCL,
B
bellard 已提交
152
    OP_SBBL,
153 154 155
    OP_ANDL,
    OP_SUBL,
    OP_XORL,
B
bellard 已提交
156 157 158 159 160
    OP_CMPL,
};

/* i386 shift ops */
enum {
161 162 163 164 165 166
    OP_ROL,
    OP_ROR,
    OP_RCL,
    OP_RCR,
    OP_SHL,
    OP_SHR,
B
bellard 已提交
167 168 169 170
    OP_SHL1, /* undocumented */
    OP_SAR = 7,
};

171 172 173 174 175 176 177 178 179 180 181
enum {
    JCC_O,
    JCC_B,
    JCC_Z,
    JCC_BE,
    JCC_S,
    JCC_P,
    JCC_L,
    JCC_LE,
};

B
bellard 已提交
182 183 184 185 186 187 188 189 190 191
enum {
    /* I386 int registers */
    OR_EAX,   /* MUST be even numbered */
    OR_ECX,
    OR_EDX,
    OR_EBX,
    OR_ESP,
    OR_EBP,
    OR_ESI,
    OR_EDI,
B
bellard 已提交
192 193

    OR_TMP0 = 16,    /* temporary operand register */
B
bellard 已提交
194 195 196 197
    OR_TMP1,
    OR_A0, /* temporary register used when doing address evaluation */
};

198
enum {
199 200
    USES_CC_DST  = 1,
    USES_CC_SRC  = 2,
201 202
    USES_CC_SRC2 = 4,
    USES_CC_SRCT = 8,
203 204 205 206
};

/* Bit set if the global variable is live after setting CC_OP to X.  */
static const uint8_t cc_op_live[CC_OP_NB] = {
207
    [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
208 209 210
    [CC_OP_EFLAGS] = USES_CC_SRC,
    [CC_OP_MULB ... CC_OP_MULQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_ADDB ... CC_OP_ADDQ] = USES_CC_DST | USES_CC_SRC,
211
    [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
212
    [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRCT,
213
    [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
214 215 216 217 218
    [CC_OP_LOGICB ... CC_OP_LOGICQ] = USES_CC_DST,
    [CC_OP_INCB ... CC_OP_INCQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_DECB ... CC_OP_DECQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_SHLB ... CC_OP_SHLQ] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_SARB ... CC_OP_SARQ] = USES_CC_DST | USES_CC_SRC,
219
    [CC_OP_BMILGB ... CC_OP_BMILGQ] = USES_CC_DST | USES_CC_SRC,
220 221 222
    [CC_OP_ADCX] = USES_CC_DST | USES_CC_SRC,
    [CC_OP_ADOX] = USES_CC_SRC | USES_CC_SRC2,
    [CC_OP_ADCOX] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
R
Richard Henderson 已提交
223
    [CC_OP_CLR] = 0,
224
    [CC_OP_POPCNT] = USES_CC_SRC,
225 226
};

227
static void set_cc_op(DisasContext *s, CCOp op)
228
{
229 230 231 232 233 234 235 236 237 238
    int dead;

    if (s->cc_op == op) {
        return;
    }

    /* Discard CC computation that will no longer be used.  */
    dead = cc_op_live[s->cc_op] & ~cc_op_live[op];
    if (dead & USES_CC_DST) {
        tcg_gen_discard_tl(cpu_cc_dst);
239
    }
240 241 242
    if (dead & USES_CC_SRC) {
        tcg_gen_discard_tl(cpu_cc_src);
    }
243 244 245
    if (dead & USES_CC_SRC2) {
        tcg_gen_discard_tl(cpu_cc_src2);
    }
246 247 248
    if (dead & USES_CC_SRCT) {
        tcg_gen_discard_tl(cpu_cc_srcT);
    }
249

250 251 252 253 254 255 256 257 258 259 260
    if (op == CC_OP_DYNAMIC) {
        /* The DYNAMIC setting is translator only, and should never be
           stored.  Thus we always consider it clean.  */
        s->cc_op_dirty = false;
    } else {
        /* Discard any computed CC_OP value (see shifts).  */
        if (s->cc_op == CC_OP_DYNAMIC) {
            tcg_gen_discard_i32(cpu_cc_op);
        }
        s->cc_op_dirty = true;
    }
261
    s->cc_op = op;
262 263 264 265 266
}

static void gen_update_cc_op(DisasContext *s)
{
    if (s->cc_op_dirty) {
267
        tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
268 269
        s->cc_op_dirty = false;
    }
270 271
}

B
bellard 已提交
272 273 274 275 276 277 278 279 280 281
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

282
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
283 284 285 286 287
#define REG_B_OFFSET (sizeof(target_ulong) - 1)
#define REG_H_OFFSET (sizeof(target_ulong) - 2)
#define REG_W_OFFSET (sizeof(target_ulong) - 2)
#define REG_L_OFFSET (sizeof(target_ulong) - 4)
#define REG_LH_OFFSET (sizeof(target_ulong) - 8)
B
bellard 已提交
288
#else
B
bellard 已提交
289 290 291 292 293
#define REG_B_OFFSET 0
#define REG_H_OFFSET 1
#define REG_W_OFFSET 0
#define REG_L_OFFSET 0
#define REG_LH_OFFSET 4
B
bellard 已提交
294
#endif
B
bellard 已提交
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
/* In instruction encodings for byte register accesses the
 * register number usually indicates "low 8 bits of register N";
 * however there are some special cases where N 4..7 indicates
 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
 * true for this special case, false otherwise.
 */
static inline bool byte_reg_is_xH(int reg)
{
    if (reg < 4) {
        return false;
    }
#ifdef TARGET_X86_64
    if (reg >= 8 || x86_64_hregs) {
        return false;
    }
#endif
    return true;
}

315 316 317 318 319 320 321 322 323 324
/* Select the size of a push/pop operation.  */
static inline TCGMemOp mo_pushpop(DisasContext *s, TCGMemOp ot)
{
    if (CODE64(s)) {
        return ot == MO_16 ? MO_16 : MO_64;
    } else {
        return ot;
    }
}

325 326 327 328 329 330
/* Select the size of the stack pointer.  */
static inline TCGMemOp mo_stacksize(DisasContext *s)
{
    return CODE64(s) ? MO_64 : s->ss32 ? MO_32 : MO_16;
}

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
/* Select only size 64 else 32.  Used for SSE operand sizes.  */
static inline TCGMemOp mo_64_32(TCGMemOp ot)
{
#ifdef TARGET_X86_64
    return ot == MO_64 ? MO_64 : MO_32;
#else
    return MO_32;
#endif
}

/* Select size 8 if lsb of B is clear, else OT.  Used for decoding
   byte vs word opcodes.  */
static inline TCGMemOp mo_b_d(int b, TCGMemOp ot)
{
    return b & 1 ? ot : MO_8;
}

/* Select size 8 if lsb of B is clear, else OT capped at 32.
   Used for decoding operand size of port opcodes.  */
static inline TCGMemOp mo_b_d32(int b, TCGMemOp ot)
{
    return b & 1 ? (ot == MO_16 ? MO_16 : MO_32) : MO_8;
}

355
static void gen_op_mov_reg_v(TCGMemOp ot, int reg, TCGv t0)
B
bellard 已提交
356 357
{
    switch(ot) {
358
    case MO_8:
359
        if (!byte_reg_is_xH(reg)) {
360
            tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
B
bellard 已提交
361
        } else {
362
            tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
B
bellard 已提交
363 364
        }
        break;
365
    case MO_16:
366
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
B
bellard 已提交
367
        break;
368
    case MO_32:
369 370 371
        /* For x86_64, this sets the higher half of register to zero.
           For i386, this is equivalent to a mov. */
        tcg_gen_ext32u_tl(cpu_regs[reg], t0);
B
bellard 已提交
372
        break;
373
#ifdef TARGET_X86_64
374
    case MO_64:
375
        tcg_gen_mov_tl(cpu_regs[reg], t0);
B
bellard 已提交
376
        break;
B
bellard 已提交
377
#endif
378 379
    default:
        tcg_abort();
B
bellard 已提交
380 381
    }
}
B
bellard 已提交
382

383
static inline void gen_op_mov_v_reg(TCGMemOp ot, TCGv t0, int reg)
B
bellard 已提交
384
{
385
    if (ot == MO_8 && byte_reg_is_xH(reg)) {
386
        tcg_gen_extract_tl(t0, cpu_regs[reg - 4], 8, 8);
387
    } else {
388
        tcg_gen_mov_tl(t0, cpu_regs[reg]);
B
bellard 已提交
389 390 391 392 393
    }
}

static void gen_add_A0_im(DisasContext *s, int val)
{
394 395 396 397
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
    if (!CODE64(s)) {
        tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
    }
B
bellard 已提交
398
}
B
bellard 已提交
399

400
static inline void gen_op_jmp_v(TCGv dest)
B
bellard 已提交
401
{
402
    tcg_gen_st_tl(dest, cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
403 404
}

405
static inline void gen_op_add_reg_im(TCGMemOp size, int reg, int32_t val)
B
bellard 已提交
406
{
407 408
    tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
    gen_op_mov_reg_v(size, reg, cpu_tmp0);
B
bellard 已提交
409 410
}

411
static inline void gen_op_add_reg_T0(TCGMemOp size, int reg)
B
bellard 已提交
412
{
413
    tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T0);
414
    gen_op_mov_reg_v(size, reg, cpu_tmp0);
415
}
B
bellard 已提交
416

417
static inline void gen_op_ld_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
418
{
419
    tcg_gen_qemu_ld_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
420
}
B
bellard 已提交
421

422
static inline void gen_op_st_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
423
{
424
    tcg_gen_qemu_st_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
425
}
426

427 428 429
static inline void gen_op_st_rm_T0_A0(DisasContext *s, int idx, int d)
{
    if (d == OR_TMP0) {
430
        gen_op_st_v(s, idx, cpu_T0, cpu_A0);
431
    } else {
432
        gen_op_mov_reg_v(idx, d, cpu_T0);
433 434 435
    }
}

B
bellard 已提交
436 437
static inline void gen_jmp_im(target_ulong pc)
{
B
bellard 已提交
438
    tcg_gen_movi_tl(cpu_tmp0, pc);
439
    gen_op_jmp_v(cpu_tmp0);
B
bellard 已提交
440 441
}

442 443 444
/* Compute SEG:REG into A0.  SEG is selected from the override segment
   (OVR_SEG) and the default segment (DEF_SEG).  OVR_SEG may be -1 to
   indicate no override.  */
445 446
static void gen_lea_v_seg(DisasContext *s, TCGMemOp aflag, TCGv a0,
                          int def_seg, int ovr_seg)
B
bellard 已提交
447
{
448
    switch (aflag) {
B
bellard 已提交
449
#ifdef TARGET_X86_64
450
    case MO_64:
451 452 453
        if (ovr_seg < 0) {
            tcg_gen_mov_tl(cpu_A0, a0);
            return;
B
bellard 已提交
454
        }
455
        break;
B
bellard 已提交
456
#endif
457
    case MO_32:
B
bellard 已提交
458
        /* 32 bit address */
459 460 461
        if (ovr_seg < 0 && s->addseg) {
            ovr_seg = def_seg;
        }
462
        if (ovr_seg < 0) {
463 464
            tcg_gen_ext32u_tl(cpu_A0, a0);
            return;
B
bellard 已提交
465
        }
466 467
        break;
    case MO_16:
468 469 470
        /* 16 bit address */
        tcg_gen_ext16u_tl(cpu_A0, a0);
        a0 = cpu_A0;
P
Paolo Bonzini 已提交
471 472 473 474 475 476 477
        if (ovr_seg < 0) {
            if (s->addseg) {
                ovr_seg = def_seg;
            } else {
                return;
            }
        }
478 479 480
        break;
    default:
        tcg_abort();
B
bellard 已提交
481 482
    }

483
    if (ovr_seg >= 0) {
484
        TCGv seg = cpu_seg_base[ovr_seg];
485 486 487 488 489 490

        if (aflag == MO_64) {
            tcg_gen_add_tl(cpu_A0, a0, seg);
        } else if (CODE64(s)) {
            tcg_gen_ext32u_tl(cpu_A0, a0);
            tcg_gen_add_tl(cpu_A0, cpu_A0, seg);
B
bellard 已提交
491
        } else {
492 493
            tcg_gen_add_tl(cpu_A0, a0, seg);
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
494 495 496 497
        }
    }
}

498 499
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
500
    gen_lea_v_seg(s, s->aflag, cpu_regs[R_ESI], R_DS, s->override);
501 502 503 504
}

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
505
    gen_lea_v_seg(s, s->aflag, cpu_regs[R_EDI], R_ES, -1);
506 507
}

508
static inline void gen_op_movl_T0_Dshift(TCGMemOp ot)
509
{
510 511
    tcg_gen_ld32s_tl(cpu_T0, cpu_env, offsetof(CPUX86State, df));
    tcg_gen_shli_tl(cpu_T0, cpu_T0, ot);
B
bellard 已提交
512 513
};

514
static TCGv gen_ext_tl(TCGv dst, TCGv src, TCGMemOp size, bool sign)
515
{
516
    switch (size) {
517
    case MO_8:
518 519 520 521 522 523
        if (sign) {
            tcg_gen_ext8s_tl(dst, src);
        } else {
            tcg_gen_ext8u_tl(dst, src);
        }
        return dst;
524
    case MO_16:
525 526 527 528 529 530 531
        if (sign) {
            tcg_gen_ext16s_tl(dst, src);
        } else {
            tcg_gen_ext16u_tl(dst, src);
        }
        return dst;
#ifdef TARGET_X86_64
532
    case MO_32:
533 534 535 536 537 538 539
        if (sign) {
            tcg_gen_ext32s_tl(dst, src);
        } else {
            tcg_gen_ext32u_tl(dst, src);
        }
        return dst;
#endif
540
    default:
541
        return src;
542 543
    }
}
544

545
static void gen_extu(TCGMemOp ot, TCGv reg)
546 547 548 549
{
    gen_ext_tl(reg, reg, ot, false);
}

550
static void gen_exts(TCGMemOp ot, TCGv reg)
551
{
552
    gen_ext_tl(reg, reg, ot, true);
553
}
B
bellard 已提交
554

555
static inline void gen_op_jnz_ecx(TCGMemOp size, TCGLabel *label1)
556
{
557
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
558
    gen_extu(size, cpu_tmp0);
P
pbrook 已提交
559
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
560 561
}

562
static inline void gen_op_jz_ecx(TCGMemOp size, TCGLabel *label1)
563
{
564
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
565
    gen_extu(size, cpu_tmp0);
P
pbrook 已提交
566
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
567
}
B
bellard 已提交
568

569
static void gen_helper_in_func(TCGMemOp ot, TCGv v, TCGv_i32 n)
P
pbrook 已提交
570 571
{
    switch (ot) {
572
    case MO_8:
573
        gen_helper_inb(v, cpu_env, n);
574
        break;
575
    case MO_16:
576
        gen_helper_inw(v, cpu_env, n);
577
        break;
578
    case MO_32:
579
        gen_helper_inl(v, cpu_env, n);
580
        break;
581 582
    default:
        tcg_abort();
P
pbrook 已提交
583 584
    }
}
B
bellard 已提交
585

586
static void gen_helper_out_func(TCGMemOp ot, TCGv_i32 v, TCGv_i32 n)
P
pbrook 已提交
587 588
{
    switch (ot) {
589
    case MO_8:
590
        gen_helper_outb(cpu_env, v, n);
591
        break;
592
    case MO_16:
593
        gen_helper_outw(cpu_env, v, n);
594
        break;
595
    case MO_32:
596
        gen_helper_outl(cpu_env, v, n);
597
        break;
598 599
    default:
        tcg_abort();
P
pbrook 已提交
600 601
    }
}
602

603
static void gen_check_io(DisasContext *s, TCGMemOp ot, target_ulong cur_eip,
604
                         uint32_t svm_flags)
605
{
606 607
    target_ulong next_eip;

608
    if (s->pe && (s->cpl > s->iopl || s->vm86)) {
609
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
P
pbrook 已提交
610
        switch (ot) {
611
        case MO_8:
B
Blue Swirl 已提交
612 613
            gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
            break;
614
        case MO_16:
B
Blue Swirl 已提交
615 616
            gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
            break;
617
        case MO_32:
B
Blue Swirl 已提交
618 619
            gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
            break;
620 621
        default:
            tcg_abort();
P
pbrook 已提交
622
        }
623
    }
B
bellard 已提交
624
    if(s->flags & HF_SVMI_MASK) {
625 626
        gen_update_cc_op(s);
        gen_jmp_im(cur_eip);
627 628
        svm_flags |= (1 << (4 + ot));
        next_eip = s->pc - s->cs_base;
629
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
B
Blue Swirl 已提交
630 631
        gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
                                tcg_const_i32(svm_flags),
P
pbrook 已提交
632
                                tcg_const_i32(next_eip - cur_eip));
633 634 635
    }
}

636
static inline void gen_movs(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
637 638
{
    gen_string_movl_A0_ESI(s);
639
    gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
B
bellard 已提交
640
    gen_string_movl_A0_EDI(s);
641
    gen_op_st_v(s, ot, cpu_T0, cpu_A0);
642
    gen_op_movl_T0_Dshift(ot);
643 644
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
645 646
}

647 648
static void gen_op_update1_cc(void)
{
649
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
650 651 652 653
}

static void gen_op_update2_cc(void)
{
654 655
    tcg_gen_mov_tl(cpu_cc_src, cpu_T1);
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
656 657
}

658 659 660
static void gen_op_update3_cc(TCGv reg)
{
    tcg_gen_mov_tl(cpu_cc_src2, reg);
661 662
    tcg_gen_mov_tl(cpu_cc_src, cpu_T1);
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
663 664
}

665 666
static inline void gen_op_testl_T0_T1_cc(void)
{
667
    tcg_gen_and_tl(cpu_cc_dst, cpu_T0, cpu_T1);
668 669 670 671
}

static void gen_op_update_neg_cc(void)
{
672 673
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
    tcg_gen_neg_tl(cpu_cc_src, cpu_T0);
674
    tcg_gen_movi_tl(cpu_cc_srcT, 0);
675 676
}

677 678
/* compute all eflags to cc_src */
static void gen_compute_eflags(DisasContext *s)
679
{
680
    TCGv zero, dst, src1, src2;
681 682
    int live, dead;

683 684 685
    if (s->cc_op == CC_OP_EFLAGS) {
        return;
    }
R
Richard Henderson 已提交
686
    if (s->cc_op == CC_OP_CLR) {
687
        tcg_gen_movi_tl(cpu_cc_src, CC_Z | CC_P);
R
Richard Henderson 已提交
688 689 690
        set_cc_op(s, CC_OP_EFLAGS);
        return;
    }
691

692
    zero = NULL;
693 694
    dst = cpu_cc_dst;
    src1 = cpu_cc_src;
695
    src2 = cpu_cc_src2;
696 697 698

    /* Take care to not read values that are not live.  */
    live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
699
    dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
700 701 702 703 704 705 706 707
    if (dead) {
        zero = tcg_const_tl(0);
        if (dead & USES_CC_DST) {
            dst = zero;
        }
        if (dead & USES_CC_SRC) {
            src1 = zero;
        }
708 709 710
        if (dead & USES_CC_SRC2) {
            src2 = zero;
        }
711 712
    }

713
    gen_update_cc_op(s);
714
    gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
715
    set_cc_op(s, CC_OP_EFLAGS);
716 717 718 719

    if (dead) {
        tcg_temp_free(zero);
    }
720 721
}

722 723 724 725 726 727 728 729 730 731
typedef struct CCPrepare {
    TCGCond cond;
    TCGv reg;
    TCGv reg2;
    target_ulong imm;
    target_ulong mask;
    bool use_reg2;
    bool no_setcond;
} CCPrepare;

732
/* compute eflags.C to reg */
733
static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
734 735
{
    TCGv t0, t1;
736
    int size, shift;
737 738 739

    switch (s->cc_op) {
    case CC_OP_SUBB ... CC_OP_SUBQ:
740
        /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
741 742 743
        size = s->cc_op - CC_OP_SUBB;
        t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
        /* If no temporary was used, be careful not to alias t1 and t0.  */
R
Richard Henderson 已提交
744
        t0 = t1 == cpu_cc_src ? cpu_tmp0 : reg;
745
        tcg_gen_mov_tl(t0, cpu_cc_srcT);
746 747 748 749 750 751 752 753 754
        gen_extu(size, t0);
        goto add_sub;

    case CC_OP_ADDB ... CC_OP_ADDQ:
        /* (DATA_TYPE)CC_DST < (DATA_TYPE)CC_SRC */
        size = s->cc_op - CC_OP_ADDB;
        t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
        t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
    add_sub:
755 756
        return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
                             .reg2 = t1, .mask = -1, .use_reg2 = true };
757 758

    case CC_OP_LOGICB ... CC_OP_LOGICQ:
R
Richard Henderson 已提交
759
    case CC_OP_CLR:
760
    case CC_OP_POPCNT:
761
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
762 763 764

    case CC_OP_INCB ... CC_OP_INCQ:
    case CC_OP_DECB ... CC_OP_DECQ:
765 766
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = -1, .no_setcond = true };
767 768 769 770

    case CC_OP_SHLB ... CC_OP_SHLQ:
        /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
        size = s->cc_op - CC_OP_SHLB;
771 772 773
        shift = (8 << size) - 1;
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = (target_ulong)1 << shift };
774 775

    case CC_OP_MULB ... CC_OP_MULQ:
776 777
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = -1 };
778

779 780 781 782 783
    case CC_OP_BMILGB ... CC_OP_BMILGQ:
        size = s->cc_op - CC_OP_BMILGB;
        t0 = gen_ext_tl(reg, cpu_cc_src, size, false);
        return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };

784 785 786 787 788
    case CC_OP_ADCX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
                             .mask = -1, .no_setcond = true };

789 790 791
    case CC_OP_EFLAGS:
    case CC_OP_SARB ... CC_OP_SARQ:
        /* CC_SRC & 1 */
792 793
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = CC_C };
794 795 796 797 798

    default:
       /* The need to compute only C from CC_OP_DYNAMIC is important
          in efficiently implementing e.g. INC at the start of a TB.  */
       gen_update_cc_op(s);
799 800
       gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
                               cpu_cc_src2, cpu_cc_op);
801 802
       return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                            .mask = -1, .no_setcond = true };
803 804 805
    }
}

806
/* compute eflags.P to reg */
807
static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
808
{
809
    gen_compute_eflags(s);
810 811
    return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                         .mask = CC_P };
812 813 814
}

/* compute eflags.S to reg */
815
static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
816
{
817 818 819 820 821
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
822 823 824
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
825 826
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_S };
R
Richard Henderson 已提交
827
    case CC_OP_CLR:
828
    case CC_OP_POPCNT:
R
Richard Henderson 已提交
829
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
830 831
    default:
        {
832
            TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
833
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
834
            return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
835 836
        }
    }
837 838 839
}

/* compute eflags.O to reg */
840
static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
841
{
842 843 844 845 846
    switch (s->cc_op) {
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src2,
                             .mask = -1, .no_setcond = true };
R
Richard Henderson 已提交
847
    case CC_OP_CLR:
848
    case CC_OP_POPCNT:
R
Richard Henderson 已提交
849
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
850 851 852 853 854
    default:
        gen_compute_eflags(s);
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_O };
    }
855 856 857
}

/* compute eflags.Z to reg */
858
static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
859
{
860 861 862 863 864
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
865 866 867
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
868 869
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_Z };
R
Richard Henderson 已提交
870 871
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_ALWAYS, .mask = -1 };
872 873 874
    case CC_OP_POPCNT:
        return (CCPrepare) { .cond = TCG_COND_EQ, .reg = cpu_cc_src,
                             .mask = -1 };
875 876
    default:
        {
877
            TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
878
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
879
            return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
880
        }
881 882 883
    }
}

884 885
/* perform a conditional store into register 'reg' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
886
static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
887
{
888 889
    int inv, jcc_op, cond;
    TCGMemOp size;
890
    CCPrepare cc;
891 892 893
    TCGv t0;

    inv = b & 1;
894
    jcc_op = (b >> 1) & 7;
895 896

    switch (s->cc_op) {
897 898
    case CC_OP_SUBB ... CC_OP_SUBQ:
        /* We optimize relational operators for the cmp/jcc case.  */
899 900 901
        size = s->cc_op - CC_OP_SUBB;
        switch (jcc_op) {
        case JCC_BE:
902
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
903 904
            gen_extu(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
905 906
            cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
907
            break;
908

909
        case JCC_L:
910
            cond = TCG_COND_LT;
911 912
            goto fast_jcc_l;
        case JCC_LE:
913
            cond = TCG_COND_LE;
914
        fast_jcc_l:
915
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
916 917
            gen_exts(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
918 919
            cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
920
            break;
921

922
        default:
923
            goto slow_jcc;
924
        }
925
        break;
926

927 928
    default:
    slow_jcc:
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
        /* This actually generates good code for JC, JZ and JS.  */
        switch (jcc_op) {
        case JCC_O:
            cc = gen_prepare_eflags_o(s, reg);
            break;
        case JCC_B:
            cc = gen_prepare_eflags_c(s, reg);
            break;
        case JCC_Z:
            cc = gen_prepare_eflags_z(s, reg);
            break;
        case JCC_BE:
            gen_compute_eflags(s);
            cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                               .mask = CC_Z | CC_C };
            break;
        case JCC_S:
            cc = gen_prepare_eflags_s(s, reg);
            break;
        case JCC_P:
            cc = gen_prepare_eflags_p(s, reg);
            break;
        case JCC_L:
            gen_compute_eflags(s);
R
Richard Henderson 已提交
953
            if (reg == cpu_cc_src) {
954 955 956 957 958 959 960 961 962 963
                reg = cpu_tmp0;
            }
            tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
            tcg_gen_xor_tl(reg, reg, cpu_cc_src);
            cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                               .mask = CC_S };
            break;
        default:
        case JCC_LE:
            gen_compute_eflags(s);
R
Richard Henderson 已提交
964
            if (reg == cpu_cc_src) {
965 966 967 968 969 970 971 972
                reg = cpu_tmp0;
            }
            tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
            tcg_gen_xor_tl(reg, reg, cpu_cc_src);
            cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                               .mask = CC_S | CC_Z };
            break;
        }
973
        break;
974
    }
975 976 977 978 979

    if (inv) {
        cc.cond = tcg_invert_cond(cc.cond);
    }
    return cc;
980 981
}

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
static void gen_setcc1(DisasContext *s, int b, TCGv reg)
{
    CCPrepare cc = gen_prepare_cc(s, b, reg);

    if (cc.no_setcond) {
        if (cc.cond == TCG_COND_EQ) {
            tcg_gen_xori_tl(reg, cc.reg, 1);
        } else {
            tcg_gen_mov_tl(reg, cc.reg);
        }
        return;
    }

    if (cc.cond == TCG_COND_NE && !cc.use_reg2 && cc.imm == 0 &&
        cc.mask != 0 && (cc.mask & (cc.mask - 1)) == 0) {
        tcg_gen_shri_tl(reg, cc.reg, ctztl(cc.mask));
        tcg_gen_andi_tl(reg, reg, 1);
        return;
    }
    if (cc.mask != -1) {
        tcg_gen_andi_tl(reg, cc.reg, cc.mask);
        cc.reg = reg;
    }
    if (cc.use_reg2) {
        tcg_gen_setcond_tl(cc.cond, reg, cc.reg, cc.reg2);
    } else {
        tcg_gen_setcondi_tl(cc.cond, reg, cc.reg, cc.imm);
    }
}

static inline void gen_compute_eflags_c(DisasContext *s, TCGv reg)
{
    gen_setcc1(s, JCC_B << 1, reg);
}
1016

1017 1018
/* generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1019
static inline void gen_jcc1_noeob(DisasContext *s, int b, TCGLabel *l1)
1020
{
1021
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T0);
1022 1023

    if (cc.mask != -1) {
1024 1025
        tcg_gen_andi_tl(cpu_T0, cc.reg, cc.mask);
        cc.reg = cpu_T0;
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    }
    if (cc.use_reg2) {
        tcg_gen_brcond_tl(cc.cond, cc.reg, cc.reg2, l1);
    } else {
        tcg_gen_brcondi_tl(cc.cond, cc.reg, cc.imm, l1);
    }
}

/* Generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used.
   A translation block must end soon.  */
1037
static inline void gen_jcc1(DisasContext *s, int b, TCGLabel *l1)
1038
{
1039
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T0);
1040

1041
    gen_update_cc_op(s);
1042
    if (cc.mask != -1) {
1043 1044
        tcg_gen_andi_tl(cpu_T0, cc.reg, cc.mask);
        cc.reg = cpu_T0;
1045
    }
1046
    set_cc_op(s, CC_OP_DYNAMIC);
1047 1048 1049 1050
    if (cc.use_reg2) {
        tcg_gen_brcond_tl(cc.cond, cc.reg, cc.reg2, l1);
    } else {
        tcg_gen_brcondi_tl(cc.cond, cc.reg, cc.imm, l1);
1051 1052 1053
    }
}

B
bellard 已提交
1054 1055
/* XXX: does not work with gdbstub "ice" single step - not a
   serious problem */
1056
static TCGLabel *gen_jz_ecx_string(DisasContext *s, target_ulong next_eip)
B
bellard 已提交
1057
{
1058 1059
    TCGLabel *l1 = gen_new_label();
    TCGLabel *l2 = gen_new_label();
1060
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1061 1062 1063 1064
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1065 1066
}

1067
static inline void gen_stos(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1068
{
1069
    gen_op_mov_v_reg(MO_32, cpu_T0, R_EAX);
B
bellard 已提交
1070
    gen_string_movl_A0_EDI(s);
1071
    gen_op_st_v(s, ot, cpu_T0, cpu_A0);
1072
    gen_op_movl_T0_Dshift(ot);
1073
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1074 1075
}

1076
static inline void gen_lods(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1077 1078
{
    gen_string_movl_A0_ESI(s);
1079 1080
    gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
    gen_op_mov_reg_v(ot, R_EAX, cpu_T0);
1081
    gen_op_movl_T0_Dshift(ot);
1082
    gen_op_add_reg_T0(s->aflag, R_ESI);
B
bellard 已提交
1083 1084
}

1085
static inline void gen_scas(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1086 1087
{
    gen_string_movl_A0_EDI(s);
1088
    gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
1089
    gen_op(s, OP_CMPL, ot, R_EAX);
1090
    gen_op_movl_T0_Dshift(ot);
1091
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1092 1093
}

1094
static inline void gen_cmps(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1095 1096
{
    gen_string_movl_A0_EDI(s);
1097
    gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
1098 1099
    gen_string_movl_A0_ESI(s);
    gen_op(s, OP_CMPL, ot, OR_TMP0);
1100
    gen_op_movl_T0_Dshift(ot);
1101 1102
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1103 1104
}

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
static void gen_bpt_io(DisasContext *s, TCGv_i32 t_port, int ot)
{
    if (s->flags & HF_IOBPT_MASK) {
        TCGv_i32 t_size = tcg_const_i32(1 << ot);
        TCGv t_next = tcg_const_tl(s->pc - s->cs_base);

        gen_helper_bpt_io(cpu_env, t_port, t_size, t_next);
        tcg_temp_free_i32(t_size);
        tcg_temp_free(t_next);
    }
}


1118
static inline void gen_ins(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1119
{
1120
    if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
1121
        gen_io_start();
1122
    }
B
bellard 已提交
1123
    gen_string_movl_A0_EDI(s);
1124 1125
    /* Note: we must do this dummy write first to be restartable in
       case of page fault. */
1126 1127
    tcg_gen_movi_tl(cpu_T0, 0);
    gen_op_st_v(s, ot, cpu_T0, cpu_A0);
1128
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1129
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
1130 1131
    gen_helper_in_func(ot, cpu_T0, cpu_tmp2_i32);
    gen_op_st_v(s, ot, cpu_T0, cpu_A0);
1132
    gen_op_movl_T0_Dshift(ot);
1133
    gen_op_add_reg_T0(s->aflag, R_EDI);
1134
    gen_bpt_io(s, cpu_tmp2_i32, ot);
1135
    if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
1136
        gen_io_end();
1137
    }
B
bellard 已提交
1138 1139
}

1140
static inline void gen_outs(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1141
{
1142
    if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
1143
        gen_io_start();
1144
    }
B
bellard 已提交
1145
    gen_string_movl_A0_ESI(s);
1146
    gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1147

1148
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1149
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
1150
    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T0);
P
pbrook 已提交
1151
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1152
    gen_op_movl_T0_Dshift(ot);
1153
    gen_op_add_reg_T0(s->aflag, R_ESI);
1154
    gen_bpt_io(s, cpu_tmp2_i32, ot);
1155
    if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
1156
        gen_io_end();
1157
    }
B
bellard 已提交
1158 1159 1160 1161 1162
}

/* same method as Valgrind : we generate jumps to current or next
   instruction */
#define GEN_REPZ(op)                                                          \
1163
static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot,              \
B
bellard 已提交
1164
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1165
{                                                                             \
1166
    TCGLabel *l2;                                                             \
B
bellard 已提交
1167
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1168
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1169
    gen_ ## op(s, ot);                                                        \
1170
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1171 1172
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
1173
    if (s->repz_opt)                                                          \
1174
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1175 1176 1177 1178
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
1179
static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot,              \
B
bellard 已提交
1180 1181
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1182 1183
                                   int nz)                                    \
{                                                                             \
1184
    TCGLabel *l2;                                                             \
B
bellard 已提交
1185
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1186
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1187
    gen_ ## op(s, ot);                                                        \
1188
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
1189
    gen_update_cc_op(s);                                                      \
1190
    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);                                 \
1191
    if (s->repz_opt)                                                          \
1192
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    gen_jmp(s, cur_eip);                                                      \
}

GEN_REPZ(movs)
GEN_REPZ(stos)
GEN_REPZ(lods)
GEN_REPZ(ins)
GEN_REPZ(outs)
GEN_REPZ2(scas)
GEN_REPZ2(cmps)

P
pbrook 已提交
1204 1205 1206
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
    case 0:
        gen_helper_fadd_ST0_FT0(cpu_env);
        break;
    case 1:
        gen_helper_fmul_ST0_FT0(cpu_env);
        break;
    case 2:
        gen_helper_fcom_ST0_FT0(cpu_env);
        break;
    case 3:
        gen_helper_fcom_ST0_FT0(cpu_env);
        break;
    case 4:
        gen_helper_fsub_ST0_FT0(cpu_env);
        break;
    case 5:
        gen_helper_fsubr_ST0_FT0(cpu_env);
        break;
    case 6:
        gen_helper_fdiv_ST0_FT0(cpu_env);
        break;
    case 7:
        gen_helper_fdivr_ST0_FT0(cpu_env);
        break;
P
pbrook 已提交
1231 1232
    }
}
B
bellard 已提交
1233 1234

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1235 1236 1237 1238
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
    case 0:
        gen_helper_fadd_STN_ST0(cpu_env, tmp);
        break;
    case 1:
        gen_helper_fmul_STN_ST0(cpu_env, tmp);
        break;
    case 4:
        gen_helper_fsubr_STN_ST0(cpu_env, tmp);
        break;
    case 5:
        gen_helper_fsub_STN_ST0(cpu_env, tmp);
        break;
    case 6:
        gen_helper_fdivr_STN_ST0(cpu_env, tmp);
        break;
    case 7:
        gen_helper_fdiv_STN_ST0(cpu_env, tmp);
        break;
P
pbrook 已提交
1257 1258
    }
}
B
bellard 已提交
1259 1260

/* if d == OR_TMP0, it means memory operand (address in A0) */
1261
static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d)
B
bellard 已提交
1262 1263
{
    if (d != OR_TMP0) {
1264
        gen_op_mov_v_reg(ot, cpu_T0, d);
1265
    } else if (!(s1->prefix & PREFIX_LOCK)) {
1266
        gen_op_ld_v(s1, ot, cpu_T0, cpu_A0);
B
bellard 已提交
1267 1268 1269
    }
    switch(op) {
    case OP_ADCL:
1270
        gen_compute_eflags_c(s1, cpu_tmp4);
1271 1272 1273 1274 1275 1276 1277 1278 1279
        if (s1->prefix & PREFIX_LOCK) {
            tcg_gen_add_tl(cpu_T0, cpu_tmp4, cpu_T1);
            tcg_gen_atomic_add_fetch_tl(cpu_T0, cpu_A0, cpu_T0,
                                        s1->mem_index, ot | MO_LE);
        } else {
            tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
            tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_tmp4);
            gen_op_st_rm_T0_A0(s1, ot, d);
        }
1280 1281
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_ADCB + ot);
B
bellard 已提交
1282
        break;
B
bellard 已提交
1283
    case OP_SBBL:
1284
        gen_compute_eflags_c(s1, cpu_tmp4);
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
        if (s1->prefix & PREFIX_LOCK) {
            tcg_gen_add_tl(cpu_T0, cpu_T1, cpu_tmp4);
            tcg_gen_neg_tl(cpu_T0, cpu_T0);
            tcg_gen_atomic_add_fetch_tl(cpu_T0, cpu_A0, cpu_T0,
                                        s1->mem_index, ot | MO_LE);
        } else {
            tcg_gen_sub_tl(cpu_T0, cpu_T0, cpu_T1);
            tcg_gen_sub_tl(cpu_T0, cpu_T0, cpu_tmp4);
            gen_op_st_rm_T0_A0(s1, ot, d);
        }
1295 1296
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_SBBB + ot);
B
bellard 已提交
1297
        break;
B
bellard 已提交
1298
    case OP_ADDL:
1299 1300 1301 1302 1303 1304 1305
        if (s1->prefix & PREFIX_LOCK) {
            tcg_gen_atomic_add_fetch_tl(cpu_T0, cpu_A0, cpu_T1,
                                        s1->mem_index, ot | MO_LE);
        } else {
            tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
            gen_op_st_rm_T0_A0(s1, ot, d);
        }
B
bellard 已提交
1306
        gen_op_update2_cc();
1307
        set_cc_op(s1, CC_OP_ADDB + ot);
B
bellard 已提交
1308 1309
        break;
    case OP_SUBL:
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
        if (s1->prefix & PREFIX_LOCK) {
            tcg_gen_neg_tl(cpu_T0, cpu_T1);
            tcg_gen_atomic_fetch_add_tl(cpu_cc_srcT, cpu_A0, cpu_T0,
                                        s1->mem_index, ot | MO_LE);
            tcg_gen_sub_tl(cpu_T0, cpu_cc_srcT, cpu_T1);
        } else {
            tcg_gen_mov_tl(cpu_cc_srcT, cpu_T0);
            tcg_gen_sub_tl(cpu_T0, cpu_T0, cpu_T1);
            gen_op_st_rm_T0_A0(s1, ot, d);
        }
B
bellard 已提交
1320
        gen_op_update2_cc();
1321
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1322 1323 1324
        break;
    default:
    case OP_ANDL:
1325 1326 1327 1328 1329 1330 1331
        if (s1->prefix & PREFIX_LOCK) {
            tcg_gen_atomic_and_fetch_tl(cpu_T0, cpu_A0, cpu_T1,
                                        s1->mem_index, ot | MO_LE);
        } else {
            tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_T1);
            gen_op_st_rm_T0_A0(s1, ot, d);
        }
B
bellard 已提交
1332
        gen_op_update1_cc();
1333
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1334
        break;
B
bellard 已提交
1335
    case OP_ORL:
1336 1337 1338 1339 1340 1341 1342
        if (s1->prefix & PREFIX_LOCK) {
            tcg_gen_atomic_or_fetch_tl(cpu_T0, cpu_A0, cpu_T1,
                                       s1->mem_index, ot | MO_LE);
        } else {
            tcg_gen_or_tl(cpu_T0, cpu_T0, cpu_T1);
            gen_op_st_rm_T0_A0(s1, ot, d);
        }
B
bellard 已提交
1343
        gen_op_update1_cc();
1344
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1345
        break;
B
bellard 已提交
1346
    case OP_XORL:
1347 1348 1349 1350 1351 1352 1353
        if (s1->prefix & PREFIX_LOCK) {
            tcg_gen_atomic_xor_fetch_tl(cpu_T0, cpu_A0, cpu_T1,
                                        s1->mem_index, ot | MO_LE);
        } else {
            tcg_gen_xor_tl(cpu_T0, cpu_T0, cpu_T1);
            gen_op_st_rm_T0_A0(s1, ot, d);
        }
B
bellard 已提交
1354
        gen_op_update1_cc();
1355
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1356 1357
        break;
    case OP_CMPL:
1358 1359 1360
        tcg_gen_mov_tl(cpu_cc_src, cpu_T1);
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T0);
        tcg_gen_sub_tl(cpu_cc_dst, cpu_T0, cpu_T1);
1361
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1362 1363
        break;
    }
1364 1365
}

B
bellard 已提交
1366
/* if d == OR_TMP0, it means memory operand (address in A0) */
1367
static void gen_inc(DisasContext *s1, TCGMemOp ot, int d, int c)
B
bellard 已提交
1368
{
1369 1370 1371 1372
    if (s1->prefix & PREFIX_LOCK) {
        tcg_gen_movi_tl(cpu_T0, c > 0 ? 1 : -1);
        tcg_gen_atomic_add_fetch_tl(cpu_T0, cpu_A0, cpu_T0,
                                    s1->mem_index, ot | MO_LE);
1373
    } else {
1374 1375 1376 1377 1378 1379 1380
        if (d != OR_TMP0) {
            gen_op_mov_v_reg(ot, cpu_T0, d);
        } else {
            gen_op_ld_v(s1, ot, cpu_T0, cpu_A0);
        }
        tcg_gen_addi_tl(cpu_T0, cpu_T0, (c > 0 ? 1 : -1));
        gen_op_st_rm_T0_A0(s1, ot, d);
1381
    }
1382

1383
    gen_compute_eflags_c(s1, cpu_cc_src);
1384
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
1385
    set_cc_op(s1, (c > 0 ? CC_OP_INCB : CC_OP_DECB) + ot);
B
bellard 已提交
1386 1387
}

1388 1389
static void gen_shift_flags(DisasContext *s, TCGMemOp ot, TCGv result,
                            TCGv shm1, TCGv count, bool is_right)
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
{
    TCGv_i32 z32, s32, oldop;
    TCGv z_tl;

    /* Store the results into the CC variables.  If we know that the
       variable must be dead, store unconditionally.  Otherwise we'll
       need to not disrupt the current contents.  */
    z_tl = tcg_const_tl(0);
    if (cc_op_live[s->cc_op] & USES_CC_DST) {
        tcg_gen_movcond_tl(TCG_COND_NE, cpu_cc_dst, count, z_tl,
                           result, cpu_cc_dst);
    } else {
        tcg_gen_mov_tl(cpu_cc_dst, result);
    }
    if (cc_op_live[s->cc_op] & USES_CC_SRC) {
        tcg_gen_movcond_tl(TCG_COND_NE, cpu_cc_src, count, z_tl,
                           shm1, cpu_cc_src);
    } else {
        tcg_gen_mov_tl(cpu_cc_src, shm1);
    }
    tcg_temp_free(z_tl);

    /* Get the two potential CC_OP values into temporaries.  */
    tcg_gen_movi_i32(cpu_tmp2_i32, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
    if (s->cc_op == CC_OP_DYNAMIC) {
        oldop = cpu_cc_op;
    } else {
        tcg_gen_movi_i32(cpu_tmp3_i32, s->cc_op);
        oldop = cpu_tmp3_i32;
    }

    /* Conditionally store the CC_OP value.  */
    z32 = tcg_const_i32(0);
    s32 = tcg_temp_new_i32();
    tcg_gen_trunc_tl_i32(s32, count);
    tcg_gen_movcond_i32(TCG_COND_NE, cpu_cc_op, s32, z32, cpu_tmp2_i32, oldop);
    tcg_temp_free_i32(z32);
    tcg_temp_free_i32(s32);

    /* The CC_OP value is no longer predictable.  */
    set_cc_op(s, CC_OP_DYNAMIC);
}

1433
static void gen_shift_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1434
                            int is_right, int is_arith)
B
bellard 已提交
1435
{
1436
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1437

1438
    /* load */
1439
    if (op1 == OR_TMP0) {
1440
        gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1441
    } else {
1442
        gen_op_mov_v_reg(ot, cpu_T0, op1);
1443
    }
1444

1445 1446
    tcg_gen_andi_tl(cpu_T1, cpu_T1, mask);
    tcg_gen_subi_tl(cpu_tmp0, cpu_T1, 1);
1447 1448 1449

    if (is_right) {
        if (is_arith) {
1450 1451 1452
            gen_exts(ot, cpu_T0);
            tcg_gen_sar_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
            tcg_gen_sar_tl(cpu_T0, cpu_T0, cpu_T1);
1453
        } else {
1454 1455 1456
            gen_extu(ot, cpu_T0);
            tcg_gen_shr_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
            tcg_gen_shr_tl(cpu_T0, cpu_T0, cpu_T1);
1457 1458
        }
    } else {
1459 1460
        tcg_gen_shl_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
        tcg_gen_shl_tl(cpu_T0, cpu_T0, cpu_T1);
1461 1462 1463
    }

    /* store */
1464
    gen_op_st_rm_T0_A0(s, ot, op1);
1465

1466
    gen_shift_flags(s, ot, cpu_T0, cpu_tmp0, cpu_T1, is_right);
1467 1468
}

1469
static void gen_shift_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
B
bellard 已提交
1470 1471
                            int is_right, int is_arith)
{
1472
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
B
bellard 已提交
1473 1474 1475

    /* load */
    if (op1 == OR_TMP0)
1476
        gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
B
bellard 已提交
1477
    else
1478
        gen_op_mov_v_reg(ot, cpu_T0, op1);
B
bellard 已提交
1479 1480 1481 1482 1483

    op2 &= mask;
    if (op2 != 0) {
        if (is_right) {
            if (is_arith) {
1484 1485 1486
                gen_exts(ot, cpu_T0);
                tcg_gen_sari_tl(cpu_tmp4, cpu_T0, op2 - 1);
                tcg_gen_sari_tl(cpu_T0, cpu_T0, op2);
B
bellard 已提交
1487
            } else {
1488 1489 1490
                gen_extu(ot, cpu_T0);
                tcg_gen_shri_tl(cpu_tmp4, cpu_T0, op2 - 1);
                tcg_gen_shri_tl(cpu_T0, cpu_T0, op2);
B
bellard 已提交
1491 1492
            }
        } else {
1493 1494
            tcg_gen_shli_tl(cpu_tmp4, cpu_T0, op2 - 1);
            tcg_gen_shli_tl(cpu_T0, cpu_T0, op2);
B
bellard 已提交
1495 1496 1497 1498
        }
    }

    /* store */
1499 1500
    gen_op_st_rm_T0_A0(s, ot, op1);

B
bellard 已提交
1501 1502
    /* update eflags if non zero shift */
    if (op2 != 0) {
B
bellard 已提交
1503
        tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
1504
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
1505
        set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
B
bellard 已提交
1506 1507 1508
    }
}

1509
static void gen_rot_rm_T1(DisasContext *s, TCGMemOp ot, int op1, int is_right)
1510
{
1511
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1512
    TCGv_i32 t0, t1;
1513 1514

    /* load */
1515
    if (op1 == OR_TMP0) {
1516
        gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1517
    } else {
1518
        gen_op_mov_v_reg(ot, cpu_T0, op1);
1519
    }
1520

1521
    tcg_gen_andi_tl(cpu_T1, cpu_T1, mask);
1522

1523
    switch (ot) {
1524
    case MO_8:
1525
        /* Replicate the 8-bit input so that a 32-bit rotate works.  */
1526 1527
        tcg_gen_ext8u_tl(cpu_T0, cpu_T0);
        tcg_gen_muli_tl(cpu_T0, cpu_T0, 0x01010101);
1528
        goto do_long;
1529
    case MO_16:
1530
        /* Replicate the 16-bit input so that a 32-bit rotate works.  */
1531
        tcg_gen_deposit_tl(cpu_T0, cpu_T0, cpu_T0, 16, 16);
1532 1533 1534
        goto do_long;
    do_long:
#ifdef TARGET_X86_64
1535
    case MO_32:
1536 1537
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
1538 1539 1540 1541 1542
        if (is_right) {
            tcg_gen_rotr_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
        } else {
            tcg_gen_rotl_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
        }
1543
        tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
1544 1545 1546 1547
        break;
#endif
    default:
        if (is_right) {
1548
            tcg_gen_rotr_tl(cpu_T0, cpu_T0, cpu_T1);
1549
        } else {
1550
            tcg_gen_rotl_tl(cpu_T0, cpu_T0, cpu_T1);
1551 1552
        }
        break;
1553 1554 1555
    }

    /* store */
1556
    gen_op_st_rm_T0_A0(s, ot, op1);
1557

1558 1559
    /* We'll need the flags computed into CC_SRC.  */
    gen_compute_eflags(s);
1560

1561 1562 1563 1564
    /* The value that was "rotated out" is now present at the other end
       of the word.  Compute C into CC_DST and O into CC_SRC2.  Note that
       since we've computed the flags into CC_SRC, these variables are
       currently dead.  */
1565
    if (is_right) {
1566 1567
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask - 1);
        tcg_gen_shri_tl(cpu_cc_dst, cpu_T0, mask);
P
Pavel Dovgaluk 已提交
1568
        tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1569
    } else {
1570 1571
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask);
        tcg_gen_andi_tl(cpu_cc_dst, cpu_T0, 1);
1572
    }
1573 1574 1575 1576 1577 1578 1579 1580 1581
    tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
    tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);

    /* Now conditionally store the new CC_OP value.  If the shift count
       is 0 we keep the CC_OP_EFLAGS setting so that only CC_SRC is live.
       Otherwise reuse CC_OP_ADCOX which have the C and O flags split out
       exactly as we computed above.  */
    t0 = tcg_const_i32(0);
    t1 = tcg_temp_new_i32();
1582
    tcg_gen_trunc_tl_i32(t1, cpu_T1);
1583 1584 1585 1586 1587 1588 1589 1590 1591
    tcg_gen_movi_i32(cpu_tmp2_i32, CC_OP_ADCOX); 
    tcg_gen_movi_i32(cpu_tmp3_i32, CC_OP_EFLAGS);
    tcg_gen_movcond_i32(TCG_COND_NE, cpu_cc_op, t1, t0,
                        cpu_tmp2_i32, cpu_tmp3_i32);
    tcg_temp_free_i32(t0);
    tcg_temp_free_i32(t1);

    /* The CC_OP value is no longer predictable.  */ 
    set_cc_op(s, CC_OP_DYNAMIC);
1592 1593
}

1594
static void gen_rot_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
M
malc 已提交
1595 1596
                          int is_right)
{
1597
    int mask = (ot == MO_64 ? 0x3f : 0x1f);
1598
    int shift;
M
malc 已提交
1599 1600 1601

    /* load */
    if (op1 == OR_TMP0) {
1602
        gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
M
malc 已提交
1603
    } else {
1604
        gen_op_mov_v_reg(ot, cpu_T0, op1);
M
malc 已提交
1605 1606 1607 1608
    }

    op2 &= mask;
    if (op2 != 0) {
1609 1610
        switch (ot) {
#ifdef TARGET_X86_64
1611
        case MO_32:
1612
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
1613 1614 1615 1616 1617
            if (is_right) {
                tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
            } else {
                tcg_gen_rotli_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
            }
1618
            tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
1619 1620 1621 1622
            break;
#endif
        default:
            if (is_right) {
1623
                tcg_gen_rotri_tl(cpu_T0, cpu_T0, op2);
1624
            } else {
1625
                tcg_gen_rotli_tl(cpu_T0, cpu_T0, op2);
1626 1627
            }
            break;
1628
        case MO_8:
1629 1630
            mask = 7;
            goto do_shifts;
1631
        case MO_16:
1632 1633 1634 1635 1636 1637
            mask = 15;
        do_shifts:
            shift = op2 & mask;
            if (is_right) {
                shift = mask + 1 - shift;
            }
1638 1639 1640 1641
            gen_extu(ot, cpu_T0);
            tcg_gen_shli_tl(cpu_tmp0, cpu_T0, shift);
            tcg_gen_shri_tl(cpu_T0, cpu_T0, mask + 1 - shift);
            tcg_gen_or_tl(cpu_T0, cpu_T0, cpu_tmp0);
1642
            break;
M
malc 已提交
1643 1644 1645 1646
        }
    }

    /* store */
1647
    gen_op_st_rm_T0_A0(s, ot, op1);
M
malc 已提交
1648 1649

    if (op2 != 0) {
1650
        /* Compute the flags into CC_SRC.  */
1651
        gen_compute_eflags(s);
1652

1653 1654 1655 1656
        /* The value that was "rotated out" is now present at the other end
           of the word.  Compute C into CC_DST and O into CC_SRC2.  Note that
           since we've computed the flags into CC_SRC, these variables are
           currently dead.  */
M
malc 已提交
1657
        if (is_right) {
1658 1659
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask - 1);
            tcg_gen_shri_tl(cpu_cc_dst, cpu_T0, mask);
1660
            tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1661
        } else {
1662 1663
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask);
            tcg_gen_andi_tl(cpu_cc_dst, cpu_T0, 1);
M
malc 已提交
1664
        }
1665 1666 1667
        tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
        tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);
        set_cc_op(s, CC_OP_ADCOX);
M
malc 已提交
1668 1669 1670
    }
}

1671
/* XXX: add faster immediate = 1 case */
1672
static void gen_rotc_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1673 1674
                           int is_right)
{
1675
    gen_compute_eflags(s);
1676
    assert(s->cc_op == CC_OP_EFLAGS);
1677 1678 1679

    /* load */
    if (op1 == OR_TMP0)
1680
        gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1681
    else
1682
        gen_op_mov_v_reg(ot, cpu_T0, op1);
1683
    
P
pbrook 已提交
1684 1685
    if (is_right) {
        switch (ot) {
1686
        case MO_8:
1687
            gen_helper_rcrb(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1688
            break;
1689
        case MO_16:
1690
            gen_helper_rcrw(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1691
            break;
1692
        case MO_32:
1693
            gen_helper_rcrl(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1694
            break;
P
pbrook 已提交
1695
#ifdef TARGET_X86_64
1696
        case MO_64:
1697
            gen_helper_rcrq(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1698
            break;
P
pbrook 已提交
1699
#endif
1700 1701
        default:
            tcg_abort();
P
pbrook 已提交
1702 1703 1704
        }
    } else {
        switch (ot) {
1705
        case MO_8:
1706
            gen_helper_rclb(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1707
            break;
1708
        case MO_16:
1709
            gen_helper_rclw(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1710
            break;
1711
        case MO_32:
1712
            gen_helper_rcll(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1713
            break;
P
pbrook 已提交
1714
#ifdef TARGET_X86_64
1715
        case MO_64:
1716
            gen_helper_rclq(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1717
            break;
P
pbrook 已提交
1718
#endif
1719 1720
        default:
            tcg_abort();
P
pbrook 已提交
1721 1722
        }
    }
1723
    /* store */
1724
    gen_op_st_rm_T0_A0(s, ot, op1);
1725 1726 1727
}

/* XXX: add faster immediate case */
1728
static void gen_shiftd_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1729
                             bool is_right, TCGv count_in)
1730
{
1731
    target_ulong mask = (ot == MO_64 ? 63 : 31);
1732
    TCGv count;
1733 1734

    /* load */
1735
    if (op1 == OR_TMP0) {
1736
        gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1737
    } else {
1738
        gen_op_mov_v_reg(ot, cpu_T0, op1);
1739
    }
1740

1741 1742
    count = tcg_temp_new();
    tcg_gen_andi_tl(count, count_in, mask);
1743

1744
    switch (ot) {
1745
    case MO_16:
1746 1747 1748
        /* Note: we implement the Intel behaviour for shift count > 16.
           This means "shrdw C, B, A" shifts A:B:A >> C.  Build the B:A
           portion by constructing it as a 32-bit value.  */
1749
        if (is_right) {
1750 1751 1752
            tcg_gen_deposit_tl(cpu_tmp0, cpu_T0, cpu_T1, 16, 16);
            tcg_gen_mov_tl(cpu_T1, cpu_T0);
            tcg_gen_mov_tl(cpu_T0, cpu_tmp0);
1753
        } else {
1754
            tcg_gen_deposit_tl(cpu_T1, cpu_T0, cpu_T1, 16, 16);
1755
        }
1756 1757
        /* FALLTHRU */
#ifdef TARGET_X86_64
1758
    case MO_32:
1759 1760
        /* Concatenate the two 32-bit values and use a 64-bit shift.  */
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
1761
        if (is_right) {
1762 1763 1764
            tcg_gen_concat_tl_i64(cpu_T0, cpu_T0, cpu_T1);
            tcg_gen_shr_i64(cpu_tmp0, cpu_T0, cpu_tmp0);
            tcg_gen_shr_i64(cpu_T0, cpu_T0, count);
1765
        } else {
1766 1767 1768
            tcg_gen_concat_tl_i64(cpu_T0, cpu_T1, cpu_T0);
            tcg_gen_shl_i64(cpu_tmp0, cpu_T0, cpu_tmp0);
            tcg_gen_shl_i64(cpu_T0, cpu_T0, count);
1769
            tcg_gen_shri_i64(cpu_tmp0, cpu_tmp0, 32);
1770
            tcg_gen_shri_i64(cpu_T0, cpu_T0, 32);
1771 1772 1773 1774 1775 1776
        }
        break;
#endif
    default:
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
        if (is_right) {
1777
            tcg_gen_shr_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
1778

1779
            tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
1780 1781
            tcg_gen_shr_tl(cpu_T0, cpu_T0, count);
            tcg_gen_shl_tl(cpu_T1, cpu_T1, cpu_tmp4);
1782
        } else {
1783
            tcg_gen_shl_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
1784
            if (ot == MO_16) {
1785 1786
                /* Only needed if count > 16, for Intel behaviour.  */
                tcg_gen_subfi_tl(cpu_tmp4, 33, count);
1787
                tcg_gen_shr_tl(cpu_tmp4, cpu_T1, cpu_tmp4);
1788 1789 1790 1791
                tcg_gen_or_tl(cpu_tmp0, cpu_tmp0, cpu_tmp4);
            }

            tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
1792 1793
            tcg_gen_shl_tl(cpu_T0, cpu_T0, count);
            tcg_gen_shr_tl(cpu_T1, cpu_T1, cpu_tmp4);
1794
        }
1795
        tcg_gen_movi_tl(cpu_tmp4, 0);
1796 1797 1798
        tcg_gen_movcond_tl(TCG_COND_EQ, cpu_T1, count, cpu_tmp4,
                           cpu_tmp4, cpu_T1);
        tcg_gen_or_tl(cpu_T0, cpu_T0, cpu_T1);
1799
        break;
1800 1801 1802
    }

    /* store */
1803
    gen_op_st_rm_T0_A0(s, ot, op1);
1804

1805
    gen_shift_flags(s, ot, cpu_T0, cpu_tmp0, count, is_right);
1806
    tcg_temp_free(count);
1807 1808
}

1809
static void gen_shift(DisasContext *s1, int op, TCGMemOp ot, int d, int s)
1810 1811
{
    if (s != OR_TMP1)
1812
        gen_op_mov_v_reg(ot, cpu_T1, s);
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
    switch(op) {
    case OP_ROL:
        gen_rot_rm_T1(s1, ot, d, 0);
        break;
    case OP_ROR:
        gen_rot_rm_T1(s1, ot, d, 1);
        break;
    case OP_SHL:
    case OP_SHL1:
        gen_shift_rm_T1(s1, ot, d, 0, 0);
        break;
    case OP_SHR:
        gen_shift_rm_T1(s1, ot, d, 1, 0);
        break;
    case OP_SAR:
        gen_shift_rm_T1(s1, ot, d, 1, 1);
        break;
    case OP_RCL:
        gen_rotc_rm_T1(s1, ot, d, 0);
        break;
    case OP_RCR:
        gen_rotc_rm_T1(s1, ot, d, 1);
        break;
    }
B
bellard 已提交
1837 1838
}

1839
static void gen_shifti(DisasContext *s1, int op, TCGMemOp ot, int d, int c)
B
bellard 已提交
1840
{
B
bellard 已提交
1841
    switch(op) {
M
malc 已提交
1842 1843 1844 1845 1846 1847
    case OP_ROL:
        gen_rot_rm_im(s1, ot, d, c, 0);
        break;
    case OP_ROR:
        gen_rot_rm_im(s1, ot, d, c, 1);
        break;
B
bellard 已提交
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
    case OP_SHL:
    case OP_SHL1:
        gen_shift_rm_im(s1, ot, d, c, 0, 0);
        break;
    case OP_SHR:
        gen_shift_rm_im(s1, ot, d, c, 1, 0);
        break;
    case OP_SAR:
        gen_shift_rm_im(s1, ot, d, c, 1, 1);
        break;
    default:
        /* currently not optimized */
1860
        tcg_gen_movi_tl(cpu_T1, c);
B
bellard 已提交
1861 1862 1863
        gen_shift(s1, op, ot, d, OR_TMP1);
        break;
    }
B
bellard 已提交
1864 1865
}

1866 1867
#define X86_MAX_INSN_LENGTH 15

1868 1869 1870 1871 1872
static uint64_t advance_pc(CPUX86State *env, DisasContext *s, int num_bytes)
{
    uint64_t pc = s->pc;

    s->pc += num_bytes;
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
    if (unlikely(s->pc - s->pc_start > X86_MAX_INSN_LENGTH)) {
        /* If the instruction's 16th byte is on a different page than the 1st, a
         * page fault on the second page wins over the general protection fault
         * caused by the instruction being too long.
         * This can happen even if the operand is only one byte long!
         */
        if (((s->pc - 1) ^ (pc - 1)) & TARGET_PAGE_MASK) {
            volatile uint8_t unused =
                cpu_ldub_code(env, (s->pc - 1) & TARGET_PAGE_MASK);
            (void) unused;
        }
        siglongjmp(s->jmpbuf, 1);
    }

1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
    return pc;
}

static inline uint8_t x86_ldub_code(CPUX86State *env, DisasContext *s)
{
    return cpu_ldub_code(env, advance_pc(env, s, 1));
}

static inline int16_t x86_ldsw_code(CPUX86State *env, DisasContext *s)
{
    return cpu_ldsw_code(env, advance_pc(env, s, 2));
}

static inline uint16_t x86_lduw_code(CPUX86State *env, DisasContext *s)
{
    return cpu_lduw_code(env, advance_pc(env, s, 2));
}

static inline uint32_t x86_ldl_code(CPUX86State *env, DisasContext *s)
{
    return cpu_ldl_code(env, advance_pc(env, s, 4));
}

#ifdef TARGET_X86_64
static inline uint64_t x86_ldq_code(CPUX86State *env, DisasContext *s)
{
    return cpu_ldq_code(env, advance_pc(env, s, 8));
}
#endif

1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
/* Decompose an address.  */

typedef struct AddressParts {
    int def_seg;
    int base;
    int index;
    int scale;
    target_long disp;
} AddressParts;

static AddressParts gen_lea_modrm_0(CPUX86State *env, DisasContext *s,
                                    int modrm)
B
bellard 已提交
1929
{
1930
    int def_seg, base, index, scale, mod, rm;
B
bellard 已提交
1931
    target_long disp;
1932
    bool havesib;
B
bellard 已提交
1933

1934
    def_seg = R_DS;
1935 1936 1937 1938
    index = -1;
    scale = 0;
    disp = 0;

B
bellard 已提交
1939 1940
    mod = (modrm >> 6) & 3;
    rm = modrm & 7;
1941 1942 1943 1944 1945 1946 1947
    base = rm | REX_B(s);

    if (mod == 3) {
        /* Normally filtered out earlier, but including this path
           simplifies multi-byte nop, as well as bndcl, bndcu, bndcn.  */
        goto done;
    }
B
bellard 已提交
1948

1949 1950 1951
    switch (s->aflag) {
    case MO_64:
    case MO_32:
B
bellard 已提交
1952
        havesib = 0;
1953
        if (rm == 4) {
1954
            int code = x86_ldub_code(env, s);
B
bellard 已提交
1955
            scale = (code >> 6) & 3;
B
bellard 已提交
1956
            index = ((code >> 3) & 7) | REX_X(s);
1957 1958 1959
            if (index == 4) {
                index = -1;  /* no index */
            }
1960 1961
            base = (code & 7) | REX_B(s);
            havesib = 1;
B
bellard 已提交
1962 1963 1964 1965
        }

        switch (mod) {
        case 0:
B
bellard 已提交
1966
            if ((base & 7) == 5) {
B
bellard 已提交
1967
                base = -1;
1968
                disp = (int32_t)x86_ldl_code(env, s);
B
bellard 已提交
1969
                if (CODE64(s) && !havesib) {
1970
                    base = -2;
B
bellard 已提交
1971 1972
                    disp += s->pc + s->rip_offset;
                }
B
bellard 已提交
1973 1974 1975
            }
            break;
        case 1:
1976
            disp = (int8_t)x86_ldub_code(env, s);
B
bellard 已提交
1977 1978 1979
            break;
        default:
        case 2:
1980
            disp = (int32_t)x86_ldl_code(env, s);
B
bellard 已提交
1981 1982
            break;
        }
1983

1984 1985 1986 1987
        /* For correct popl handling with esp.  */
        if (base == R_ESP && s->popl_esp_hack) {
            disp += s->popl_esp_hack;
        }
1988 1989
        if (base == R_EBP || base == R_ESP) {
            def_seg = R_SS;
B
bellard 已提交
1990
        }
1991 1992 1993
        break;

    case MO_16:
1994
        if (mod == 0) {
B
bellard 已提交
1995
            if (rm == 6) {
1996
                base = -1;
1997
                disp = x86_lduw_code(env, s);
1998
                break;
B
bellard 已提交
1999
            }
2000
        } else if (mod == 1) {
2001
            disp = (int8_t)x86_ldub_code(env, s);
2002
        } else {
2003
            disp = (int16_t)x86_lduw_code(env, s);
B
bellard 已提交
2004
        }
2005 2006

        switch (rm) {
B
bellard 已提交
2007
        case 0:
2008 2009
            base = R_EBX;
            index = R_ESI;
B
bellard 已提交
2010 2011
            break;
        case 1:
2012 2013
            base = R_EBX;
            index = R_EDI;
B
bellard 已提交
2014 2015
            break;
        case 2:
2016 2017
            base = R_EBP;
            index = R_ESI;
2018
            def_seg = R_SS;
B
bellard 已提交
2019 2020
            break;
        case 3:
2021 2022
            base = R_EBP;
            index = R_EDI;
2023
            def_seg = R_SS;
B
bellard 已提交
2024 2025
            break;
        case 4:
2026
            base = R_ESI;
B
bellard 已提交
2027 2028
            break;
        case 5:
2029
            base = R_EDI;
B
bellard 已提交
2030 2031
            break;
        case 6:
2032
            base = R_EBP;
2033
            def_seg = R_SS;
B
bellard 已提交
2034 2035 2036
            break;
        default:
        case 7:
2037
            base = R_EBX;
B
bellard 已提交
2038 2039
            break;
        }
2040 2041 2042 2043
        break;

    default:
        tcg_abort();
B
bellard 已提交
2044
    }
2045

2046 2047
 done:
    return (AddressParts){ def_seg, base, index, scale, disp };
B
bellard 已提交
2048 2049
}

2050 2051
/* Compute the address, with a minimum number of TCG ops.  */
static TCGv gen_lea_modrm_1(AddressParts a)
B
bellard 已提交
2052
{
2053
    TCGv ea = NULL;
2054

2055 2056 2057 2058 2059 2060
    if (a.index >= 0) {
        if (a.scale == 0) {
            ea = cpu_regs[a.index];
        } else {
            tcg_gen_shli_tl(cpu_A0, cpu_regs[a.index], a.scale);
            ea = cpu_A0;
B
bellard 已提交
2061
        }
2062 2063 2064
        if (a.base >= 0) {
            tcg_gen_add_tl(cpu_A0, ea, cpu_regs[a.base]);
            ea = cpu_A0;
B
bellard 已提交
2065
        }
2066 2067 2068
    } else if (a.base >= 0) {
        ea = cpu_regs[a.base];
    }
2069
    if (!ea) {
2070 2071 2072 2073 2074 2075
        tcg_gen_movi_tl(cpu_A0, a.disp);
        ea = cpu_A0;
    } else if (a.disp != 0) {
        tcg_gen_addi_tl(cpu_A0, ea, a.disp);
        ea = cpu_A0;
    }
2076

2077 2078
    return ea;
}
2079

2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm)
{
    AddressParts a = gen_lea_modrm_0(env, s, modrm);
    TCGv ea = gen_lea_modrm_1(a);
    gen_lea_v_seg(s, s->aflag, ea, a.def_seg, s->override);
}

static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
{
    (void)gen_lea_modrm_0(env, s, modrm);
B
bellard 已提交
2090 2091
}

2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
/* Used for BNDCL, BNDCU, BNDCN.  */
static void gen_bndck(CPUX86State *env, DisasContext *s, int modrm,
                      TCGCond cond, TCGv_i64 bndv)
{
    TCGv ea = gen_lea_modrm_1(gen_lea_modrm_0(env, s, modrm));

    tcg_gen_extu_tl_i64(cpu_tmp1_i64, ea);
    if (!CODE64(s)) {
        tcg_gen_ext32u_i64(cpu_tmp1_i64, cpu_tmp1_i64);
    }
    tcg_gen_setcond_i64(cond, cpu_tmp1_i64, cpu_tmp1_i64, bndv);
    tcg_gen_extrl_i64_i32(cpu_tmp2_i32, cpu_tmp1_i64);
    gen_helper_bndck(cpu_env, cpu_tmp2_i32);
}

B
bellard 已提交
2107 2108 2109
/* used for LEA and MOV AX, mem */
static void gen_add_A0_ds_seg(DisasContext *s)
{
2110
    gen_lea_v_seg(s, s->aflag, cpu_A0, R_DS, s->override);
B
bellard 已提交
2111 2112
}

B
balrog 已提交
2113
/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
B
bellard 已提交
2114
   OR_TMP0 */
2115
static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
2116
                           TCGMemOp ot, int reg, int is_store)
B
bellard 已提交
2117
{
2118
    int mod, rm;
B
bellard 已提交
2119 2120

    mod = (modrm >> 6) & 3;
B
bellard 已提交
2121
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2122 2123 2124
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
2125 2126
                gen_op_mov_v_reg(ot, cpu_T0, reg);
            gen_op_mov_reg_v(ot, rm, cpu_T0);
B
bellard 已提交
2127
        } else {
2128
            gen_op_mov_v_reg(ot, cpu_T0, rm);
B
bellard 已提交
2129
            if (reg != OR_TMP0)
2130
                gen_op_mov_reg_v(ot, reg, cpu_T0);
B
bellard 已提交
2131 2132
        }
    } else {
2133
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
2134 2135
        if (is_store) {
            if (reg != OR_TMP0)
2136 2137
                gen_op_mov_v_reg(ot, cpu_T0, reg);
            gen_op_st_v(s, ot, cpu_T0, cpu_A0);
B
bellard 已提交
2138
        } else {
2139
            gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
B
bellard 已提交
2140
            if (reg != OR_TMP0)
2141
                gen_op_mov_reg_v(ot, reg, cpu_T0);
B
bellard 已提交
2142 2143 2144 2145
        }
    }
}

2146
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2147 2148 2149
{
    uint32_t ret;

2150
    switch (ot) {
2151
    case MO_8:
2152
        ret = x86_ldub_code(env, s);
B
bellard 已提交
2153
        break;
2154
    case MO_16:
2155
        ret = x86_lduw_code(env, s);
B
bellard 已提交
2156
        break;
2157
    case MO_32:
2158 2159 2160
#ifdef TARGET_X86_64
    case MO_64:
#endif
2161
        ret = x86_ldl_code(env, s);
B
bellard 已提交
2162
        break;
2163 2164
    default:
        tcg_abort();
B
bellard 已提交
2165 2166 2167 2168
    }
    return ret;
}

2169
static inline int insn_const_size(TCGMemOp ot)
B
bellard 已提交
2170
{
2171
    if (ot <= MO_32) {
B
bellard 已提交
2172
        return 1 << ot;
2173
    } else {
B
bellard 已提交
2174
        return 4;
2175
    }
B
bellard 已提交
2176 2177
}

2178 2179 2180
static inline bool use_goto_tb(DisasContext *s, target_ulong pc)
{
#ifndef CONFIG_USER_ONLY
2181
    return (pc & TARGET_PAGE_MASK) == (s->base.tb->pc & TARGET_PAGE_MASK) ||
2182 2183 2184 2185 2186 2187
           (pc & TARGET_PAGE_MASK) == (s->pc_start & TARGET_PAGE_MASK);
#else
    return true;
#endif
}

2188 2189
static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip)
{
2190
    target_ulong pc = s->cs_base + eip;
2191

2192
    if (use_goto_tb(s, pc))  {
2193
        /* jump to same page: we can use a direct jump */
B
bellard 已提交
2194
        tcg_gen_goto_tb(tb_num);
2195
        gen_jmp_im(eip);
2196 2197
        tcg_gen_exit_tb((uintptr_t)s->base.tb + tb_num);
        s->base.is_jmp = DISAS_NORETURN;
2198
    } else {
2199
        /* jump to another page */
2200
        gen_jmp_im(eip);
2201
        gen_jr(s, cpu_tmp0);
2202 2203 2204
    }
}

2205
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2206
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2207
{
2208
    TCGLabel *l1, *l2;
2209

B
bellard 已提交
2210
    if (s->jmp_opt) {
B
bellard 已提交
2211
        l1 = gen_new_label();
2212
        gen_jcc1(s, b, l1);
2213

2214
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2215 2216

        gen_set_label(l1);
2217
        gen_goto_tb(s, 1, val);
B
bellard 已提交
2218
    } else {
B
bellard 已提交
2219 2220
        l1 = gen_new_label();
        l2 = gen_new_label();
2221
        gen_jcc1(s, b, l1);
2222

B
bellard 已提交
2223
        gen_jmp_im(next_eip);
2224 2225
        tcg_gen_br(l2);

B
bellard 已提交
2226 2227 2228
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2229 2230 2231 2232
        gen_eob(s);
    }
}

2233
static void gen_cmovcc1(CPUX86State *env, DisasContext *s, TCGMemOp ot, int b,
2234 2235
                        int modrm, int reg)
{
2236
    CCPrepare cc;
2237

2238
    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2239

2240
    cc = gen_prepare_cc(s, b, cpu_T1);
2241 2242 2243 2244 2245 2246 2247
    if (cc.mask != -1) {
        TCGv t0 = tcg_temp_new();
        tcg_gen_andi_tl(t0, cc.reg, cc.mask);
        cc.reg = t0;
    }
    if (!cc.use_reg2) {
        cc.reg2 = tcg_const_tl(cc.imm);
2248 2249
    }

2250 2251 2252
    tcg_gen_movcond_tl(cc.cond, cpu_T0, cc.reg, cc.reg2,
                       cpu_T0, cpu_regs[reg]);
    gen_op_mov_reg_v(ot, reg, cpu_T0);
2253 2254 2255 2256 2257 2258 2259

    if (cc.mask != -1) {
        tcg_temp_free(cc.reg);
    }
    if (!cc.use_reg2) {
        tcg_temp_free(cc.reg2);
    }
2260 2261
}

2262 2263
static inline void gen_op_movl_T0_seg(int seg_reg)
{
2264
    tcg_gen_ld32u_tl(cpu_T0, cpu_env,
2265 2266 2267 2268 2269
                     offsetof(CPUX86State,segs[seg_reg].selector));
}

static inline void gen_op_movl_seg_T0_vm(int seg_reg)
{
2270 2271
    tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
    tcg_gen_st32_tl(cpu_T0, cpu_env,
2272
                    offsetof(CPUX86State,segs[seg_reg].selector));
2273
    tcg_gen_shli_tl(cpu_seg_base[seg_reg], cpu_T0, 4);
2274 2275
}

B
bellard 已提交
2276 2277
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
2278
static void gen_movl_seg_T0(DisasContext *s, int seg_reg)
B
bellard 已提交
2279
{
2280
    if (s->pe && !s->vm86) {
2281
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
2282
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2283 2284 2285 2286
        /* abort translation because the addseg value may change or
           because ss32 may change. For R_SS, translation must always
           stop as a special handling must be done to disable hardware
           interrupts for the next instruction */
2287
        if (seg_reg == R_SS || (s->code32 && seg_reg < R_FS)) {
2288
            s->base.is_jmp = DISAS_TOO_MANY;
2289
        }
2290
    } else {
2291
        gen_op_movl_seg_T0_vm(seg_reg);
2292
        if (seg_reg == R_SS) {
2293
            s->base.is_jmp = DISAS_TOO_MANY;
2294
        }
2295
    }
B
bellard 已提交
2296 2297
}

T
ths 已提交
2298 2299 2300 2301 2302
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

B
bellard 已提交
2303
static inline void
T
ths 已提交
2304
gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2305
                              uint32_t type, uint64_t param)
T
ths 已提交
2306
{
B
bellard 已提交
2307 2308 2309
    /* no SVM activated; fast case */
    if (likely(!(s->flags & HF_SVMI_MASK)))
        return;
2310
    gen_update_cc_op(s);
B
bellard 已提交
2311
    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
2312
    gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
P
pbrook 已提交
2313
                                         tcg_const_i64(param));
T
ths 已提交
2314 2315
}

B
bellard 已提交
2316
static inline void
T
ths 已提交
2317 2318
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2319
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2320 2321
}

2322 2323
static inline void gen_stack_update(DisasContext *s, int addend)
{
2324
    gen_op_add_reg_im(mo_stacksize(s), R_ESP, addend);
2325 2326
}

2327 2328
/* Generate a push. It depends on ss32, addseg and dflag.  */
static void gen_push_v(DisasContext *s, TCGv val)
B
bellard 已提交
2329
{
2330 2331
    TCGMemOp d_ot = mo_pushpop(s, s->dflag);
    TCGMemOp a_ot = mo_stacksize(s);
2332 2333 2334 2335
    int size = 1 << d_ot;
    TCGv new_esp = cpu_A0;

    tcg_gen_subi_tl(cpu_A0, cpu_regs[R_ESP], size);
B
bellard 已提交
2336

2337
    if (!CODE64(s)) {
2338 2339 2340
        if (s->addseg) {
            new_esp = cpu_tmp4;
            tcg_gen_mov_tl(new_esp, cpu_A0);
B
bellard 已提交
2341
        }
2342
        gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
B
bellard 已提交
2343
    }
2344 2345 2346

    gen_op_st_v(s, d_ot, val, cpu_A0);
    gen_op_mov_reg_v(a_ot, R_ESP, new_esp);
B
bellard 已提交
2347 2348
}

2349
/* two step pop is necessary for precise exceptions */
2350
static TCGMemOp gen_pop_T0(DisasContext *s)
B
bellard 已提交
2351
{
2352 2353
    TCGMemOp d_ot = mo_pushpop(s, s->dflag);

2354
    gen_lea_v_seg(s, mo_stacksize(s), cpu_regs[R_ESP], R_SS, -1);
2355
    gen_op_ld_v(s, d_ot, cpu_T0, cpu_A0);
2356 2357

    return d_ot;
B
bellard 已提交
2358 2359
}

2360
static inline void gen_pop_update(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2361
{
2362
    gen_stack_update(s, 1 << ot);
B
bellard 已提交
2363 2364
}

2365
static inline void gen_stack_A0(DisasContext *s)
B
bellard 已提交
2366
{
2367
    gen_lea_v_seg(s, s->ss32 ? MO_32 : MO_16, cpu_regs[R_ESP], R_SS, -1);
B
bellard 已提交
2368 2369 2370 2371
}

static void gen_pusha(DisasContext *s)
{
2372 2373 2374
    TCGMemOp s_ot = s->ss32 ? MO_32 : MO_16;
    TCGMemOp d_ot = s->dflag;
    int size = 1 << d_ot;
B
bellard 已提交
2375
    int i;
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385

    for (i = 0; i < 8; i++) {
        tcg_gen_addi_tl(cpu_A0, cpu_regs[R_ESP], (i - 8) * size);
        gen_lea_v_seg(s, s_ot, cpu_A0, R_SS, -1);
        gen_op_st_v(s, d_ot, cpu_regs[7 - i], cpu_A0);
    }

    gen_stack_update(s, -8 * size);
}

B
bellard 已提交
2386 2387
static void gen_popa(DisasContext *s)
{
2388 2389 2390
    TCGMemOp s_ot = s->ss32 ? MO_32 : MO_16;
    TCGMemOp d_ot = s->dflag;
    int size = 1 << d_ot;
B
bellard 已提交
2391
    int i;
2392 2393

    for (i = 0; i < 8; i++) {
B
bellard 已提交
2394
        /* ESP is not reloaded */
2395 2396
        if (7 - i == R_ESP) {
            continue;
B
bellard 已提交
2397
        }
2398 2399
        tcg_gen_addi_tl(cpu_A0, cpu_regs[R_ESP], i * size);
        gen_lea_v_seg(s, s_ot, cpu_A0, R_SS, -1);
2400 2401
        gen_op_ld_v(s, d_ot, cpu_T0, cpu_A0);
        gen_op_mov_reg_v(d_ot, 7 - i, cpu_T0);
B
bellard 已提交
2402
    }
2403 2404

    gen_stack_update(s, 8 * size);
B
bellard 已提交
2405 2406 2407 2408
}

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
2409 2410 2411
    TCGMemOp d_ot = mo_pushpop(s, s->dflag);
    TCGMemOp a_ot = CODE64(s) ? MO_64 : s->ss32 ? MO_32 : MO_16;
    int size = 1 << d_ot;
B
bellard 已提交
2412

2413
    /* Push BP; compute FrameTemp into T1.  */
2414 2415
    tcg_gen_subi_tl(cpu_T1, cpu_regs[R_ESP], size);
    gen_lea_v_seg(s, a_ot, cpu_T1, R_SS, -1);
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
    gen_op_st_v(s, d_ot, cpu_regs[R_EBP], cpu_A0);

    level &= 31;
    if (level != 0) {
        int i;

        /* Copy level-1 pointers from the previous frame.  */
        for (i = 1; i < level; ++i) {
            tcg_gen_subi_tl(cpu_A0, cpu_regs[R_EBP], size * i);
            gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
            gen_op_ld_v(s, d_ot, cpu_tmp0, cpu_A0);

2428
            tcg_gen_subi_tl(cpu_A0, cpu_T1, size * i);
2429 2430
            gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
            gen_op_st_v(s, d_ot, cpu_tmp0, cpu_A0);
2431
        }
2432 2433

        /* Push the current FrameTemp as the last level.  */
2434
        tcg_gen_subi_tl(cpu_A0, cpu_T1, size * level);
2435
        gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
2436
        gen_op_st_v(s, d_ot, cpu_T1, cpu_A0);
B
bellard 已提交
2437
    }
2438 2439

    /* Copy the FrameTemp value to EBP.  */
2440
    gen_op_mov_reg_v(a_ot, R_EBP, cpu_T1);
2441 2442

    /* Compute the final value of ESP.  */
2443 2444
    tcg_gen_subi_tl(cpu_T1, cpu_T1, esp_addend + size * level);
    gen_op_mov_reg_v(a_ot, R_ESP, cpu_T1);
B
bellard 已提交
2445 2446
}

R
Richard Henderson 已提交
2447 2448 2449 2450 2451 2452
static void gen_leave(DisasContext *s)
{
    TCGMemOp d_ot = mo_pushpop(s, s->dflag);
    TCGMemOp a_ot = mo_stacksize(s);

    gen_lea_v_seg(s, a_ot, cpu_regs[R_EBP], R_SS, -1);
2453
    gen_op_ld_v(s, d_ot, cpu_T0, cpu_A0);
R
Richard Henderson 已提交
2454

2455
    tcg_gen_addi_tl(cpu_T1, cpu_regs[R_EBP], 1 << d_ot);
R
Richard Henderson 已提交
2456

2457 2458
    gen_op_mov_reg_v(d_ot, R_EBP, cpu_T0);
    gen_op_mov_reg_v(a_ot, R_ESP, cpu_T1);
R
Richard Henderson 已提交
2459 2460
}

B
bellard 已提交
2461
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2462
{
2463
    gen_update_cc_op(s);
B
bellard 已提交
2464
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2465
    gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
2466
    s->base.is_jmp = DISAS_NORETURN;
B
bellard 已提交
2467 2468
}

2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
/* Generate #UD for the current instruction.  The assumption here is that
   the instruction is known, but it isn't allowed in the current cpu mode.  */
static void gen_illegal_opcode(DisasContext *s)
{
    gen_exception(s, EXCP06_ILLOP, s->pc_start - s->cs_base);
}

/* Similarly, except that the assumption here is that we don't decode
   the instruction at all -- either a missing opcode, an unimplemented
   feature, or just a bogus instruction stream.  */
static void gen_unknown_opcode(CPUX86State *env, DisasContext *s)
{
    gen_illegal_opcode(s);

    if (qemu_loglevel_mask(LOG_UNIMP)) {
        target_ulong pc = s->pc_start, end = s->pc;
2485
        qemu_log_lock();
2486 2487 2488 2489 2490
        qemu_log("ILLOPC: " TARGET_FMT_lx ":", pc);
        for (; pc < end; ++pc) {
            qemu_log(" %02x", cpu_ldub_code(env, pc));
        }
        qemu_log("\n");
2491
        qemu_log_unlock();
2492 2493 2494
    }
}

B
bellard 已提交
2495
/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2496
   privilege checks */
2497
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2498
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2499
{
2500
    gen_update_cc_op(s);
B
bellard 已提交
2501
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2502
    gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
P
pbrook 已提交
2503
                               tcg_const_i32(next_eip - cur_eip));
2504
    s->base.is_jmp = DISAS_NORETURN;
B
bellard 已提交
2505 2506
}

B
bellard 已提交
2507
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2508
{
2509
    gen_update_cc_op(s);
B
bellard 已提交
2510
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2511
    gen_helper_debug(cpu_env);
2512
    s->base.is_jmp = DISAS_NORETURN;
B
bellard 已提交
2513 2514
}

2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
static void gen_set_hflag(DisasContext *s, uint32_t mask)
{
    if ((s->flags & mask) == 0) {
        TCGv_i32 t = tcg_temp_new_i32();
        tcg_gen_ld_i32(t, cpu_env, offsetof(CPUX86State, hflags));
        tcg_gen_ori_i32(t, t, mask);
        tcg_gen_st_i32(t, cpu_env, offsetof(CPUX86State, hflags));
        tcg_temp_free_i32(t);
        s->flags |= mask;
    }
}

static void gen_reset_hflag(DisasContext *s, uint32_t mask)
{
    if (s->flags & mask) {
        TCGv_i32 t = tcg_temp_new_i32();
        tcg_gen_ld_i32(t, cpu_env, offsetof(CPUX86State, hflags));
        tcg_gen_andi_i32(t, t, ~mask);
        tcg_gen_st_i32(t, cpu_env, offsetof(CPUX86State, hflags));
        tcg_temp_free_i32(t);
        s->flags &= ~mask;
    }
}

2539 2540 2541
/* Clear BND registers during legacy branches.  */
static void gen_bnd_jmp(DisasContext *s)
{
2542 2543 2544
    /* Clear the registers only if BND prefix is missing, MPX is enabled,
       and if the BNDREGs are known to be in use (non-zero) already.
       The helper itself will check BNDPRESERVE at runtime.  */
2545
    if ((s->prefix & PREFIX_REPNZ) == 0
2546 2547
        && (s->flags & HF_MPX_EN_MASK) != 0
        && (s->flags & HF_MPX_IU_MASK) != 0) {
2548 2549 2550 2551
        gen_helper_bnd_jmp(cpu_env);
    }
}

2552
/* Generate an end of block. Trace exception is also generated if needed.
2553 2554 2555
   If INHIBIT, set HF_INHIBIT_IRQ_MASK if it isn't already set.
   If RECHECK_TF, emit a rechecking helper for #DB, ignoring the state of
   S->TF.  This is used by the syscall/sysret insns.  */
2556
static void
2557
do_gen_eob_worker(DisasContext *s, bool inhibit, bool recheck_tf, bool jr)
B
bellard 已提交
2558
{
2559
    gen_update_cc_op(s);
2560 2561 2562 2563 2564 2565 2566 2567

    /* If several instructions disable interrupts, only the first does it.  */
    if (inhibit && !(s->flags & HF_INHIBIT_IRQ_MASK)) {
        gen_set_hflag(s, HF_INHIBIT_IRQ_MASK);
    } else {
        gen_reset_hflag(s, HF_INHIBIT_IRQ_MASK);
    }

2568
    if (s->base.tb->flags & HF_RF_MASK) {
2569
        gen_helper_reset_rf(cpu_env);
J
Jan Kiszka 已提交
2570
    }
2571
    if (s->base.singlestep_enabled) {
B
Blue Swirl 已提交
2572
        gen_helper_debug(cpu_env);
2573 2574 2575
    } else if (recheck_tf) {
        gen_helper_rechecking_single_step(cpu_env);
        tcg_gen_exit_tb(0);
2576
    } else if (s->tf) {
B
Blue Swirl 已提交
2577
        gen_helper_single_step(cpu_env);
2578 2579
    } else if (jr) {
        tcg_gen_lookup_and_goto_ptr();
B
bellard 已提交
2580
    } else {
B
bellard 已提交
2581
        tcg_gen_exit_tb(0);
B
bellard 已提交
2582
    }
2583
    s->base.is_jmp = DISAS_NORETURN;
B
bellard 已提交
2584 2585
}

2586 2587 2588
static inline void
gen_eob_worker(DisasContext *s, bool inhibit, bool recheck_tf)
{
2589
    do_gen_eob_worker(s, inhibit, recheck_tf, false);
2590 2591
}

2592 2593 2594 2595 2596 2597 2598
/* End of block.
   If INHIBIT, set HF_INHIBIT_IRQ_MASK if it isn't already set.  */
static void gen_eob_inhibit_irq(DisasContext *s, bool inhibit)
{
    gen_eob_worker(s, inhibit, false);
}

2599 2600 2601
/* End of block, resetting the inhibit irq flag.  */
static void gen_eob(DisasContext *s)
{
2602
    gen_eob_worker(s, false, false);
2603 2604
}

2605 2606 2607
/* Jump to register */
static void gen_jr(DisasContext *s, TCGv dest)
{
2608
    do_gen_eob_worker(s, false, false, true);
2609 2610
}

B
bellard 已提交
2611 2612
/* generate a jump to eip. No segment change must happen before as a
   direct call to the next block may occur */
B
bellard 已提交
2613
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
B
bellard 已提交
2614
{
2615 2616
    gen_update_cc_op(s);
    set_cc_op(s, CC_OP_DYNAMIC);
B
bellard 已提交
2617
    if (s->jmp_opt) {
2618
        gen_goto_tb(s, tb_num, eip);
B
bellard 已提交
2619
    } else {
B
bellard 已提交
2620
        gen_jmp_im(eip);
B
bellard 已提交
2621 2622 2623 2624
        gen_eob(s);
    }
}

B
bellard 已提交
2625 2626 2627 2628 2629
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

2630
static inline void gen_ldq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2631
{
2632
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
2633
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2634
}
B
bellard 已提交
2635

2636
static inline void gen_stq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2637
{
2638
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2639
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
bellard 已提交
2640
}
B
bellard 已提交
2641

2642
static inline void gen_ldo_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2643
{
2644
    int mem_index = s->mem_index;
2645
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
2646
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(0)));
B
bellard 已提交
2647
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2648
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
2649
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(1)));
B
bellard 已提交
2650
}
B
bellard 已提交
2651

2652
static inline void gen_sto_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2653
{
2654
    int mem_index = s->mem_index;
2655
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(0)));
2656
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
B
bellard 已提交
2657
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2658
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(1)));
2659
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
B
bellard 已提交
2660
}
B
bellard 已提交
2661

B
bellard 已提交
2662 2663
static inline void gen_op_movo(int d_offset, int s_offset)
{
2664 2665 2666 2667
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset + offsetof(ZMMReg, ZMM_Q(0)));
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset + offsetof(ZMMReg, ZMM_Q(0)));
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset + offsetof(ZMMReg, ZMM_Q(1)));
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset + offsetof(ZMMReg, ZMM_Q(1)));
B
bellard 已提交
2668 2669 2670 2671
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2672 2673
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2674 2675 2676 2677
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2678 2679
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2680 2681 2682 2683
}

static inline void gen_op_movq_env_0(int d_offset)
{
2684 2685
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2686
}
B
bellard 已提交
2687

B
Blue Swirl 已提交
2688 2689 2690 2691 2692 2693 2694
typedef void (*SSEFunc_i_ep)(TCGv_i32 val, TCGv_ptr env, TCGv_ptr reg);
typedef void (*SSEFunc_l_ep)(TCGv_i64 val, TCGv_ptr env, TCGv_ptr reg);
typedef void (*SSEFunc_0_epi)(TCGv_ptr env, TCGv_ptr reg, TCGv_i32 val);
typedef void (*SSEFunc_0_epl)(TCGv_ptr env, TCGv_ptr reg, TCGv_i64 val);
typedef void (*SSEFunc_0_epp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b);
typedef void (*SSEFunc_0_eppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv_i32 val);
B
Blue Swirl 已提交
2695
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2696 2697
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2698

B
bellard 已提交
2699 2700
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2701

P
pbrook 已提交
2702 2703 2704
#define MMX_OP2(x) { gen_helper_ ## x ## _mmx, gen_helper_ ## x ## _xmm }
#define SSE_FOP(x) { gen_helper_ ## x ## ps, gen_helper_ ## x ## pd, \
                     gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, }
B
bellard 已提交
2705

B
Blue Swirl 已提交
2706
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2707 2708 2709
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2710 2711 2712
    /* pure SSE operations */
    [0x10] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
    [0x11] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
B
bellard 已提交
2713
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2714
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2715 2716
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2717 2718 2719 2720 2721 2722
    [0x16] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },  /* movhps, movhpd, movshdup */
    [0x17] = { SSE_SPECIAL, SSE_SPECIAL },  /* movhps, movhpd */

    [0x28] = { SSE_SPECIAL, SSE_SPECIAL },  /* movaps, movapd */
    [0x29] = { SSE_SPECIAL, SSE_SPECIAL },  /* movaps, movapd */
    [0x2a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */
2723
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2724 2725
    [0x2c] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */
    [0x2d] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */
P
pbrook 已提交
2726 2727
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2728 2729
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2730 2731 2732 2733 2734 2735
    [0x52] = { gen_helper_rsqrtps, NULL, gen_helper_rsqrtss, NULL },
    [0x53] = { gen_helper_rcpps, NULL, gen_helper_rcpss, NULL },
    [0x54] = { gen_helper_pand_xmm, gen_helper_pand_xmm }, /* andps, andpd */
    [0x55] = { gen_helper_pandn_xmm, gen_helper_pandn_xmm }, /* andnps, andnpd */
    [0x56] = { gen_helper_por_xmm, gen_helper_por_xmm }, /* orps, orpd */
    [0x57] = { gen_helper_pxor_xmm, gen_helper_pxor_xmm }, /* xorps, xorpd */
B
bellard 已提交
2736 2737
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2738 2739 2740
    [0x5a] = { gen_helper_cvtps2pd, gen_helper_cvtpd2ps,
               gen_helper_cvtss2sd, gen_helper_cvtsd2ss },
    [0x5b] = { gen_helper_cvtdq2ps, gen_helper_cvtps2dq, gen_helper_cvttps2dq },
B
bellard 已提交
2741 2742 2743 2744 2745 2746
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

    [0xc2] = SSE_FOP(cmpeq),
B
Blue Swirl 已提交
2747 2748
    [0xc6] = { (SSEFunc_0_epp)gen_helper_shufps,
               (SSEFunc_0_epp)gen_helper_shufpd }, /* XXX: casts */
B
bellard 已提交
2749

R
Richard Henderson 已提交
2750 2751 2752
    /* SSSE3, SSE4, MOVBE, CRC32, BMI1, BMI2, ADX.  */
    [0x38] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
    [0x3a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
B
balrog 已提交
2753

B
bellard 已提交
2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766
    /* MMX ops and their SSE extensions */
    [0x60] = MMX_OP2(punpcklbw),
    [0x61] = MMX_OP2(punpcklwd),
    [0x62] = MMX_OP2(punpckldq),
    [0x63] = MMX_OP2(packsswb),
    [0x64] = MMX_OP2(pcmpgtb),
    [0x65] = MMX_OP2(pcmpgtw),
    [0x66] = MMX_OP2(pcmpgtl),
    [0x67] = MMX_OP2(packuswb),
    [0x68] = MMX_OP2(punpckhbw),
    [0x69] = MMX_OP2(punpckhwd),
    [0x6a] = MMX_OP2(punpckhdq),
    [0x6b] = MMX_OP2(packssdw),
P
pbrook 已提交
2767 2768
    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2769 2770
    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
B
Blue Swirl 已提交
2771 2772 2773 2774
    [0x70] = { (SSEFunc_0_epp)gen_helper_pshufw_mmx,
               (SSEFunc_0_epp)gen_helper_pshufd_xmm,
               (SSEFunc_0_epp)gen_helper_pshufhw_xmm,
               (SSEFunc_0_epp)gen_helper_pshuflw_xmm }, /* XXX: casts */
B
bellard 已提交
2775 2776 2777 2778 2779 2780
    [0x71] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftw */
    [0x72] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftd */
    [0x73] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftq */
    [0x74] = MMX_OP2(pcmpeqb),
    [0x75] = MMX_OP2(pcmpeqw),
    [0x76] = MMX_OP2(pcmpeql),
A
aurel32 已提交
2781
    [0x77] = { SSE_DUMMY }, /* emms */
2782 2783
    [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
    [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
P
pbrook 已提交
2784 2785
    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
B
bellard 已提交
2786 2787 2788 2789
    [0x7e] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movd, movd, , movq */
    [0x7f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, movdqu */
    [0xc4] = { SSE_SPECIAL, SSE_SPECIAL }, /* pinsrw */
    [0xc5] = { SSE_SPECIAL, SSE_SPECIAL }, /* pextrw */
P
pbrook 已提交
2790
    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
B
bellard 已提交
2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
    [0xd1] = MMX_OP2(psrlw),
    [0xd2] = MMX_OP2(psrld),
    [0xd3] = MMX_OP2(psrlq),
    [0xd4] = MMX_OP2(paddq),
    [0xd5] = MMX_OP2(pmullw),
    [0xd6] = { NULL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
    [0xd7] = { SSE_SPECIAL, SSE_SPECIAL }, /* pmovmskb */
    [0xd8] = MMX_OP2(psubusb),
    [0xd9] = MMX_OP2(psubusw),
    [0xda] = MMX_OP2(pminub),
    [0xdb] = MMX_OP2(pand),
    [0xdc] = MMX_OP2(paddusb),
    [0xdd] = MMX_OP2(paddusw),
    [0xde] = MMX_OP2(pmaxub),
    [0xdf] = MMX_OP2(pandn),
    [0xe0] = MMX_OP2(pavgb),
    [0xe1] = MMX_OP2(psraw),
    [0xe2] = MMX_OP2(psrad),
    [0xe3] = MMX_OP2(pavgw),
    [0xe4] = MMX_OP2(pmulhuw),
    [0xe5] = MMX_OP2(pmulhw),
P
pbrook 已提交
2812
    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
B
bellard 已提交
2813 2814 2815 2816 2817 2818 2819 2820 2821
    [0xe7] = { SSE_SPECIAL , SSE_SPECIAL },  /* movntq, movntq */
    [0xe8] = MMX_OP2(psubsb),
    [0xe9] = MMX_OP2(psubsw),
    [0xea] = MMX_OP2(pminsw),
    [0xeb] = MMX_OP2(por),
    [0xec] = MMX_OP2(paddsb),
    [0xed] = MMX_OP2(paddsw),
    [0xee] = MMX_OP2(pmaxsw),
    [0xef] = MMX_OP2(pxor),
B
bellard 已提交
2822
    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
B
bellard 已提交
2823 2824 2825 2826 2827 2828
    [0xf1] = MMX_OP2(psllw),
    [0xf2] = MMX_OP2(pslld),
    [0xf3] = MMX_OP2(psllq),
    [0xf4] = MMX_OP2(pmuludq),
    [0xf5] = MMX_OP2(pmaddwd),
    [0xf6] = MMX_OP2(psadbw),
B
Blue Swirl 已提交
2829 2830
    [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
               (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
B
bellard 已提交
2831 2832 2833 2834 2835 2836 2837 2838 2839
    [0xf8] = MMX_OP2(psubb),
    [0xf9] = MMX_OP2(psubw),
    [0xfa] = MMX_OP2(psubl),
    [0xfb] = MMX_OP2(psubq),
    [0xfc] = MMX_OP2(paddb),
    [0xfd] = MMX_OP2(paddw),
    [0xfe] = MMX_OP2(paddl),
};

B
Blue Swirl 已提交
2840
static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
B
bellard 已提交
2841 2842 2843 2844 2845 2846 2847
    [0 + 2] = MMX_OP2(psrlw),
    [0 + 4] = MMX_OP2(psraw),
    [0 + 6] = MMX_OP2(psllw),
    [8 + 2] = MMX_OP2(psrld),
    [8 + 4] = MMX_OP2(psrad),
    [8 + 6] = MMX_OP2(pslld),
    [16 + 2] = MMX_OP2(psrlq),
P
pbrook 已提交
2848
    [16 + 3] = { NULL, gen_helper_psrldq_xmm },
B
bellard 已提交
2849
    [16 + 6] = MMX_OP2(psllq),
P
pbrook 已提交
2850
    [16 + 7] = { NULL, gen_helper_pslldq_xmm },
B
bellard 已提交
2851 2852
};

B
Blue Swirl 已提交
2853
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
2854
    gen_helper_cvtsi2ss,
2855
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
2856
};
P
pbrook 已提交
2857

2858
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2859
static const SSEFunc_0_epl sse_op_table3aq[] = {
2860 2861 2862 2863 2864
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
2865
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
2866 2867
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
2868
    gen_helper_cvttsd2si,
2869
    gen_helper_cvtsd2si
B
bellard 已提交
2870
};
2871

2872
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2873
static const SSEFunc_l_ep sse_op_table3bq[] = {
2874 2875
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
2876
    gen_helper_cvttsd2sq,
2877 2878 2879 2880
    gen_helper_cvtsd2sq
};
#endif

B
Blue Swirl 已提交
2881
static const SSEFunc_0_epp sse_op_table4[8][4] = {
B
bellard 已提交
2882 2883 2884 2885 2886 2887 2888 2889 2890
    SSE_FOP(cmpeq),
    SSE_FOP(cmplt),
    SSE_FOP(cmple),
    SSE_FOP(cmpunord),
    SSE_FOP(cmpneq),
    SSE_FOP(cmpnlt),
    SSE_FOP(cmpnle),
    SSE_FOP(cmpord),
};
2891

B
Blue Swirl 已提交
2892
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
    [0x0c] = gen_helper_pi2fw,
    [0x0d] = gen_helper_pi2fd,
    [0x1c] = gen_helper_pf2iw,
    [0x1d] = gen_helper_pf2id,
    [0x8a] = gen_helper_pfnacc,
    [0x8e] = gen_helper_pfpnacc,
    [0x90] = gen_helper_pfcmpge,
    [0x94] = gen_helper_pfmin,
    [0x96] = gen_helper_pfrcp,
    [0x97] = gen_helper_pfrsqrt,
    [0x9a] = gen_helper_pfsub,
    [0x9e] = gen_helper_pfadd,
    [0xa0] = gen_helper_pfcmpgt,
    [0xa4] = gen_helper_pfmax,
    [0xa6] = gen_helper_movq, /* pfrcpit1; no need to actually increase precision */
    [0xa7] = gen_helper_movq, /* pfrsqit1 */
    [0xaa] = gen_helper_pfsubr,
    [0xae] = gen_helper_pfacc,
    [0xb0] = gen_helper_pfcmpeq,
    [0xb4] = gen_helper_pfmul,
    [0xb6] = gen_helper_movq, /* pfrcpit2 */
    [0xb7] = gen_helper_pmulhrw_mmx,
    [0xbb] = gen_helper_pswapd,
    [0xbf] = gen_helper_pavgb_mmx /* pavgusb */
A
aurel32 已提交
2917 2918
};

B
Blue Swirl 已提交
2919 2920
struct SSEOpHelper_epp {
    SSEFunc_0_epp op[2];
B
Blue Swirl 已提交
2921 2922 2923
    uint32_t ext_mask;
};

B
Blue Swirl 已提交
2924 2925
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
2926
    uint32_t ext_mask;
B
balrog 已提交
2927
};
B
Blue Swirl 已提交
2928

B
balrog 已提交
2929
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
2930 2931
#define SSE41_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }
#define SSE42_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }
B
balrog 已提交
2932
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2933 2934
#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
        CPUID_EXT_PCLMULQDQ }
2935
#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
B
Blue Swirl 已提交
2936

B
Blue Swirl 已提交
2937
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
    [0x00] = SSSE3_OP(pshufb),
    [0x01] = SSSE3_OP(phaddw),
    [0x02] = SSSE3_OP(phaddd),
    [0x03] = SSSE3_OP(phaddsw),
    [0x04] = SSSE3_OP(pmaddubsw),
    [0x05] = SSSE3_OP(phsubw),
    [0x06] = SSSE3_OP(phsubd),
    [0x07] = SSSE3_OP(phsubsw),
    [0x08] = SSSE3_OP(psignb),
    [0x09] = SSSE3_OP(psignw),
    [0x0a] = SSSE3_OP(psignd),
    [0x0b] = SSSE3_OP(pmulhrsw),
    [0x10] = SSE41_OP(pblendvb),
    [0x14] = SSE41_OP(blendvps),
    [0x15] = SSE41_OP(blendvpd),
    [0x17] = SSE41_OP(ptest),
    [0x1c] = SSSE3_OP(pabsb),
    [0x1d] = SSSE3_OP(pabsw),
    [0x1e] = SSSE3_OP(pabsd),
    [0x20] = SSE41_OP(pmovsxbw),
    [0x21] = SSE41_OP(pmovsxbd),
    [0x22] = SSE41_OP(pmovsxbq),
    [0x23] = SSE41_OP(pmovsxwd),
    [0x24] = SSE41_OP(pmovsxwq),
    [0x25] = SSE41_OP(pmovsxdq),
    [0x28] = SSE41_OP(pmuldq),
    [0x29] = SSE41_OP(pcmpeqq),
    [0x2a] = SSE41_SPECIAL, /* movntqda */
    [0x2b] = SSE41_OP(packusdw),
    [0x30] = SSE41_OP(pmovzxbw),
    [0x31] = SSE41_OP(pmovzxbd),
    [0x32] = SSE41_OP(pmovzxbq),
    [0x33] = SSE41_OP(pmovzxwd),
    [0x34] = SSE41_OP(pmovzxwq),
    [0x35] = SSE41_OP(pmovzxdq),
    [0x37] = SSE42_OP(pcmpgtq),
    [0x38] = SSE41_OP(pminsb),
    [0x39] = SSE41_OP(pminsd),
    [0x3a] = SSE41_OP(pminuw),
    [0x3b] = SSE41_OP(pminud),
    [0x3c] = SSE41_OP(pmaxsb),
    [0x3d] = SSE41_OP(pmaxsd),
    [0x3e] = SSE41_OP(pmaxuw),
    [0x3f] = SSE41_OP(pmaxud),
    [0x40] = SSE41_OP(pmulld),
    [0x41] = SSE41_OP(phminposuw),
2984 2985 2986 2987 2988
    [0xdb] = AESNI_OP(aesimc),
    [0xdc] = AESNI_OP(aesenc),
    [0xdd] = AESNI_OP(aesenclast),
    [0xde] = AESNI_OP(aesdec),
    [0xdf] = AESNI_OP(aesdeclast),
B
balrog 已提交
2989 2990
};

B
Blue Swirl 已提交
2991
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
    [0x08] = SSE41_OP(roundps),
    [0x09] = SSE41_OP(roundpd),
    [0x0a] = SSE41_OP(roundss),
    [0x0b] = SSE41_OP(roundsd),
    [0x0c] = SSE41_OP(blendps),
    [0x0d] = SSE41_OP(blendpd),
    [0x0e] = SSE41_OP(pblendw),
    [0x0f] = SSSE3_OP(palignr),
    [0x14] = SSE41_SPECIAL, /* pextrb */
    [0x15] = SSE41_SPECIAL, /* pextrw */
    [0x16] = SSE41_SPECIAL, /* pextrd/pextrq */
    [0x17] = SSE41_SPECIAL, /* extractps */
    [0x20] = SSE41_SPECIAL, /* pinsrb */
    [0x21] = SSE41_SPECIAL, /* insertps */
    [0x22] = SSE41_SPECIAL, /* pinsrd/pinsrq */
    [0x40] = SSE41_OP(dpps),
    [0x41] = SSE41_OP(dppd),
    [0x42] = SSE41_OP(mpsadbw),
3010
    [0x44] = PCLMULQDQ_OP(pclmulqdq),
B
balrog 已提交
3011 3012 3013 3014
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
3015
    [0xdf] = AESNI_OP(aeskeygenassist),
B
balrog 已提交
3016 3017
};

3018 3019
static void gen_sse(CPUX86State *env, DisasContext *s, int b,
                    target_ulong pc_start, int rex_r)
B
bellard 已提交
3020
{
3021
    int b1, op1_offset, op2_offset, is_xmm, val;
3022
    int modrm, mod, rm, reg;
B
Blue Swirl 已提交
3023 3024
    SSEFunc_0_epp sse_fn_epp;
    SSEFunc_0_eppi sse_fn_eppi;
B
Blue Swirl 已提交
3025
    SSEFunc_0_ppi sse_fn_ppi;
B
Blue Swirl 已提交
3026
    SSEFunc_0_eppt sse_fn_eppt;
3027
    TCGMemOp ot;
B
bellard 已提交
3028 3029

    b &= 0xff;
3030
    if (s->prefix & PREFIX_DATA)
B
bellard 已提交
3031
        b1 = 1;
3032
    else if (s->prefix & PREFIX_REPZ)
B
bellard 已提交
3033
        b1 = 2;
3034
    else if (s->prefix & PREFIX_REPNZ)
B
bellard 已提交
3035 3036 3037
        b1 = 3;
    else
        b1 = 0;
B
Blue Swirl 已提交
3038 3039
    sse_fn_epp = sse_op_table1[b][b1];
    if (!sse_fn_epp) {
3040
        goto unknown_op;
B
Blue Swirl 已提交
3041
    }
A
aurel32 已提交
3042
    if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
B
bellard 已提交
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058
        is_xmm = 1;
    } else {
        if (b1 == 0) {
            /* MMX case */
            is_xmm = 0;
        } else {
            is_xmm = 1;
        }
    }
    /* simple MMX/SSE operation */
    if (s->flags & HF_TS_MASK) {
        gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
        return;
    }
    if (s->flags & HF_EM_MASK) {
    illegal_op:
3059
        gen_illegal_opcode(s);
B
bellard 已提交
3060 3061
        return;
    }
3062 3063 3064 3065 3066
    if (is_xmm
        && !(s->flags & HF_OSFXSR_MASK)
        && ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))) {
        goto unknown_op;
    }
3067
    if (b == 0x0e) {
3068 3069 3070 3071
        if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW)) {
            /* If we were fully decoding this we might use illegal_op.  */
            goto unknown_op;
        }
3072
        /* femms */
B
Blue Swirl 已提交
3073
        gen_helper_emms(cpu_env);
3074 3075 3076 3077
        return;
    }
    if (b == 0x77) {
        /* emms */
B
Blue Swirl 已提交
3078
        gen_helper_emms(cpu_env);
B
bellard 已提交
3079 3080 3081 3082 3083
        return;
    }
    /* prepare MMX state (XXX: optimize by storing fptt and fptags in
       the static cpu state) */
    if (!is_xmm) {
B
Blue Swirl 已提交
3084
        gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3085 3086
    }

3087
    modrm = x86_ldub_code(env, s);
B
bellard 已提交
3088 3089 3090 3091
    reg = ((modrm >> 3) & 7);
    if (is_xmm)
        reg |= rex_r;
    mod = (modrm >> 6) & 3;
B
Blue Swirl 已提交
3092
    if (sse_fn_epp == SSE_SPECIAL) {
B
bellard 已提交
3093 3094 3095
        b |= (b1 << 8);
        switch(b) {
        case 0x0e7: /* movntq */
3096
            if (mod == 3) {
B
bellard 已提交
3097
                goto illegal_op;
3098
            }
3099
            gen_lea_modrm(env, s, modrm);
3100
            gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3101 3102 3103 3104
            break;
        case 0x1e7: /* movntdq */
        case 0x02b: /* movntps */
        case 0x12b: /* movntps */
3105 3106
            if (mod == 3)
                goto illegal_op;
3107
            gen_lea_modrm(env, s, modrm);
3108
            gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3109
            break;
B
bellard 已提交
3110 3111
        case 0x3f0: /* lddqu */
            if (mod == 3)
B
bellard 已提交
3112
                goto illegal_op;
3113
            gen_lea_modrm(env, s, modrm);
3114
            gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3115
            break;
3116 3117 3118 3119
        case 0x22b: /* movntss */
        case 0x32b: /* movntsd */
            if (mod == 3)
                goto illegal_op;
3120
            gen_lea_modrm(env, s, modrm);
3121
            if (b1 & 1) {
3122
                gen_stq_env_A0(s, offsetof(CPUX86State,
3123
                                           xmm_regs[reg].ZMM_Q(0)));
3124
            } else {
3125
                tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
3126
                    xmm_regs[reg].ZMM_L(0)));
3127
                gen_op_st_v(s, MO_32, cpu_T0, cpu_A0);
3128 3129
            }
            break;
B
bellard 已提交
3130
        case 0x6e: /* movd mm, ea */
B
bellard 已提交
3131
#ifdef TARGET_X86_64
3132
            if (s->dflag == MO_64) {
3133
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
3134
                tcg_gen_st_tl(cpu_T0, cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
3135
            } else
B
bellard 已提交
3136 3137
#endif
            {
3138
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3139 3140
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx));
3141
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
P
pbrook 已提交
3142
                gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3143
            }
B
bellard 已提交
3144 3145
            break;
        case 0x16e: /* movd xmm, ea */
B
bellard 已提交
3146
#ifdef TARGET_X86_64
3147
            if (s->dflag == MO_64) {
3148
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
B
bellard 已提交
3149 3150
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
3151
                gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T0);
3152
            } else
B
bellard 已提交
3153 3154
#endif
            {
3155
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3156 3157
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
3158
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
P
pbrook 已提交
3159
                gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3160
            }
B
bellard 已提交
3161 3162 3163
            break;
        case 0x6f: /* movq mm, ea */
            if (mod != 3) {
3164
                gen_lea_modrm(env, s, modrm);
3165
                gen_ldq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3166 3167
            } else {
                rm = (modrm & 7);
3168
                tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3169
                               offsetof(CPUX86State,fpregs[rm].mmx));
3170
                tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3171
                               offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3172 3173 3174 3175 3176 3177 3178 3179 3180
            }
            break;
        case 0x010: /* movups */
        case 0x110: /* movupd */
        case 0x028: /* movaps */
        case 0x128: /* movapd */
        case 0x16f: /* movdqa xmm, ea */
        case 0x26f: /* movdqu xmm, ea */
            if (mod != 3) {
3181
                gen_lea_modrm(env, s, modrm);
3182
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3183 3184 3185 3186 3187 3188 3189 3190
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movo(offsetof(CPUX86State,xmm_regs[reg]),
                            offsetof(CPUX86State,xmm_regs[rm]));
            }
            break;
        case 0x210: /* movss xmm, ea */
            if (mod != 3) {
3191
                gen_lea_modrm(env, s, modrm);
3192 3193 3194 3195 3196 3197
                gen_op_ld_v(s, MO_32, cpu_T0, cpu_A0);
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
                tcg_gen_movi_tl(cpu_T0, 0);
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(1)));
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)));
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)));
B
bellard 已提交
3198 3199
            } else {
                rm = (modrm & 7) | REX_B(s);
3200 3201
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_L(0)));
B
bellard 已提交
3202 3203 3204 3205
            }
            break;
        case 0x310: /* movsd xmm, ea */
            if (mod != 3) {
3206
                gen_lea_modrm(env, s, modrm);
3207
                gen_ldq_env_A0(s, offsetof(CPUX86State,
3208
                                           xmm_regs[reg].ZMM_Q(0)));
3209 3210 3211
                tcg_gen_movi_tl(cpu_T0, 0);
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)));
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)));
B
bellard 已提交
3212 3213
            } else {
                rm = (modrm & 7) | REX_B(s);
3214 3215
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
B
bellard 已提交
3216 3217 3218 3219 3220
            }
            break;
        case 0x012: /* movlps */
        case 0x112: /* movlpd */
            if (mod != 3) {
3221
                gen_lea_modrm(env, s, modrm);
3222
                gen_ldq_env_A0(s, offsetof(CPUX86State,
3223
                                           xmm_regs[reg].ZMM_Q(0)));
B
bellard 已提交
3224 3225 3226
            } else {
                /* movhlps */
                rm = (modrm & 7) | REX_B(s);
3227 3228
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(1)));
B
bellard 已提交
3229 3230
            }
            break;
B
bellard 已提交
3231 3232
        case 0x212: /* movsldup */
            if (mod != 3) {
3233
                gen_lea_modrm(env, s, modrm);
3234
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3235 3236
            } else {
                rm = (modrm & 7) | REX_B(s);
3237 3238 3239 3240
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_L(0)));
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_L(2)));
B
bellard 已提交
3241
            }
3242 3243 3244 3245
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(1)),
                        offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)),
                        offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)));
B
bellard 已提交
3246 3247 3248
            break;
        case 0x312: /* movddup */
            if (mod != 3) {
3249
                gen_lea_modrm(env, s, modrm);
3250
                gen_ldq_env_A0(s, offsetof(CPUX86State,
3251
                                           xmm_regs[reg].ZMM_Q(0)));
B
bellard 已提交
3252 3253
            } else {
                rm = (modrm & 7) | REX_B(s);
3254 3255
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
B
bellard 已提交
3256
            }
3257 3258
            gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)),
                        offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
B
bellard 已提交
3259
            break;
B
bellard 已提交
3260 3261 3262
        case 0x016: /* movhps */
        case 0x116: /* movhpd */
            if (mod != 3) {
3263
                gen_lea_modrm(env, s, modrm);
3264
                gen_ldq_env_A0(s, offsetof(CPUX86State,
3265
                                           xmm_regs[reg].ZMM_Q(1)));
B
bellard 已提交
3266 3267 3268
            } else {
                /* movlhps */
                rm = (modrm & 7) | REX_B(s);
3269 3270
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
B
bellard 已提交
3271 3272 3273 3274
            }
            break;
        case 0x216: /* movshdup */
            if (mod != 3) {
3275
                gen_lea_modrm(env, s, modrm);
3276
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3277 3278
            } else {
                rm = (modrm & 7) | REX_B(s);
3279 3280 3281 3282
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(1)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_L(1)));
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_L(3)));
B
bellard 已提交
3283
            }
3284 3285 3286 3287
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)),
                        offsetof(CPUX86State,xmm_regs[reg].ZMM_L(1)));
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)),
                        offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)));
B
bellard 已提交
3288
            break;
3289 3290 3291 3292 3293 3294 3295
        case 0x178:
        case 0x378:
            {
                int bit_index, field_length;

                if (b1 == 1 && reg != 0)
                    goto illegal_op;
3296 3297
                field_length = x86_ldub_code(env, s) & 0x3F;
                bit_index = x86_ldub_code(env, s) & 0x3F;
3298 3299 3300
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
                    offsetof(CPUX86State,xmm_regs[reg]));
                if (b1 == 1)
B
Blue Swirl 已提交
3301 3302 3303
                    gen_helper_extrq_i(cpu_env, cpu_ptr0,
                                       tcg_const_i32(bit_index),
                                       tcg_const_i32(field_length));
3304
                else
B
Blue Swirl 已提交
3305 3306 3307
                    gen_helper_insertq_i(cpu_env, cpu_ptr0,
                                         tcg_const_i32(bit_index),
                                         tcg_const_i32(field_length));
3308 3309
            }
            break;
B
bellard 已提交
3310
        case 0x7e: /* movd ea, mm */
B
bellard 已提交
3311
#ifdef TARGET_X86_64
3312
            if (s->dflag == MO_64) {
3313
                tcg_gen_ld_i64(cpu_T0, cpu_env,
B
bellard 已提交
3314
                               offsetof(CPUX86State,fpregs[reg].mmx));
3315
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3316
            } else
B
bellard 已提交
3317 3318
#endif
            {
3319
                tcg_gen_ld32u_tl(cpu_T0, cpu_env,
B
bellard 已提交
3320
                                 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
3321
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
B
bellard 已提交
3322
            }
B
bellard 已提交
3323 3324
            break;
        case 0x17e: /* movd ea, xmm */
B
bellard 已提交
3325
#ifdef TARGET_X86_64
3326
            if (s->dflag == MO_64) {
3327
                tcg_gen_ld_i64(cpu_T0, cpu_env,
3328
                               offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
3329
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3330
            } else
B
bellard 已提交
3331 3332
#endif
            {
3333
                tcg_gen_ld32u_tl(cpu_T0, cpu_env,
3334
                                 offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
3335
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
B
bellard 已提交
3336
            }
B
bellard 已提交
3337 3338 3339
            break;
        case 0x27e: /* movq xmm, ea */
            if (mod != 3) {
3340
                gen_lea_modrm(env, s, modrm);
3341
                gen_ldq_env_A0(s, offsetof(CPUX86State,
3342
                                           xmm_regs[reg].ZMM_Q(0)));
B
bellard 已提交
3343 3344
            } else {
                rm = (modrm & 7) | REX_B(s);
3345 3346
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
B
bellard 已提交
3347
            }
3348
            gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)));
B
bellard 已提交
3349 3350 3351
            break;
        case 0x7f: /* movq ea, mm */
            if (mod != 3) {
3352
                gen_lea_modrm(env, s, modrm);
3353
                gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366
            } else {
                rm = (modrm & 7);
                gen_op_movq(offsetof(CPUX86State,fpregs[rm].mmx),
                            offsetof(CPUX86State,fpregs[reg].mmx));
            }
            break;
        case 0x011: /* movups */
        case 0x111: /* movupd */
        case 0x029: /* movaps */
        case 0x129: /* movapd */
        case 0x17f: /* movdqa ea, xmm */
        case 0x27f: /* movdqu ea, xmm */
            if (mod != 3) {
3367
                gen_lea_modrm(env, s, modrm);
3368
                gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3369 3370 3371 3372 3373 3374 3375 3376
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movo(offsetof(CPUX86State,xmm_regs[rm]),
                            offsetof(CPUX86State,xmm_regs[reg]));
            }
            break;
        case 0x211: /* movss ea, xmm */
            if (mod != 3) {
3377
                gen_lea_modrm(env, s, modrm);
3378 3379
                tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
                gen_op_st_v(s, MO_32, cpu_T0, cpu_A0);
B
bellard 已提交
3380 3381
            } else {
                rm = (modrm & 7) | REX_B(s);
3382 3383
                gen_op_movl(offsetof(CPUX86State,xmm_regs[rm].ZMM_L(0)),
                            offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
B
bellard 已提交
3384 3385 3386 3387
            }
            break;
        case 0x311: /* movsd ea, xmm */
            if (mod != 3) {
3388
                gen_lea_modrm(env, s, modrm);
3389
                gen_stq_env_A0(s, offsetof(CPUX86State,
3390
                                           xmm_regs[reg].ZMM_Q(0)));
B
bellard 已提交
3391 3392
            } else {
                rm = (modrm & 7) | REX_B(s);
3393 3394
                gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
B
bellard 已提交
3395 3396 3397 3398 3399
            }
            break;
        case 0x013: /* movlps */
        case 0x113: /* movlpd */
            if (mod != 3) {
3400
                gen_lea_modrm(env, s, modrm);
3401
                gen_stq_env_A0(s, offsetof(CPUX86State,
3402
                                           xmm_regs[reg].ZMM_Q(0)));
B
bellard 已提交
3403 3404 3405 3406 3407 3408 3409
            } else {
                goto illegal_op;
            }
            break;
        case 0x017: /* movhps */
        case 0x117: /* movhpd */
            if (mod != 3) {
3410
                gen_lea_modrm(env, s, modrm);
3411
                gen_stq_env_A0(s, offsetof(CPUX86State,
3412
                                           xmm_regs[reg].ZMM_Q(1)));
B
bellard 已提交
3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
            } else {
                goto illegal_op;
            }
            break;
        case 0x71: /* shift mm, im */
        case 0x72:
        case 0x73:
        case 0x171: /* shift xmm, im */
        case 0x172:
        case 0x173:
3423
            if (b1 >= 2) {
3424
	        goto unknown_op;
3425
            }
3426
            val = x86_ldub_code(env, s);
B
bellard 已提交
3427
            if (is_xmm) {
3428 3429 3430 3431
                tcg_gen_movi_tl(cpu_T0, val);
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_t0.ZMM_L(0)));
                tcg_gen_movi_tl(cpu_T0, 0);
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_t0.ZMM_L(1)));
B
bellard 已提交
3432 3433
                op1_offset = offsetof(CPUX86State,xmm_t0);
            } else {
3434 3435 3436 3437
                tcg_gen_movi_tl(cpu_T0, val);
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
                tcg_gen_movi_tl(cpu_T0, 0);
                tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
B
bellard 已提交
3438 3439
                op1_offset = offsetof(CPUX86State,mmx_t0);
            }
B
Blue Swirl 已提交
3440 3441 3442
            sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
                                       (((modrm >> 3)) & 7)][b1];
            if (!sse_fn_epp) {
3443
                goto unknown_op;
B
Blue Swirl 已提交
3444
            }
B
bellard 已提交
3445 3446 3447 3448 3449 3450 3451
            if (is_xmm) {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
B
bellard 已提交
3452 3453
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
B
Blue Swirl 已提交
3454
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3455 3456 3457
            break;
        case 0x050: /* movmskps */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3458 3459
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3460
            gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3461
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3462 3463 3464
            break;
        case 0x150: /* movmskpd */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3465 3466
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3467
            gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3468
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3469 3470 3471
            break;
        case 0x02a: /* cvtpi2ps */
        case 0x12a: /* cvtpi2pd */
B
Blue Swirl 已提交
3472
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3473
            if (mod != 3) {
3474
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3475
                op2_offset = offsetof(CPUX86State,mmx_t0);
3476
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
3477 3478 3479 3480 3481
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3482 3483
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3484 3485
            switch(b >> 8) {
            case 0x0:
B
Blue Swirl 已提交
3486
                gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3487 3488 3489
                break;
            default:
            case 0x1:
B
Blue Swirl 已提交
3490
                gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3491 3492 3493 3494 3495
                break;
            }
            break;
        case 0x22a: /* cvtsi2ss */
        case 0x32a: /* cvtsi2sd */
3496
            ot = mo_64_32(s->dflag);
3497
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
3498
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3499
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3500
            if (ot == MO_32) {
B
Blue Swirl 已提交
3501
                SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
3502
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
B
Blue Swirl 已提交
3503
                sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3504
            } else {
3505
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3506
                SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
3507
                sse_fn_epl(cpu_env, cpu_ptr0, cpu_T0);
3508 3509 3510
#else
                goto illegal_op;
#endif
B
bellard 已提交
3511
            }
B
bellard 已提交
3512 3513 3514 3515 3516
            break;
        case 0x02c: /* cvttps2pi */
        case 0x12c: /* cvttpd2pi */
        case 0x02d: /* cvtps2pi */
        case 0x12d: /* cvtpd2pi */
B
Blue Swirl 已提交
3517
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3518
            if (mod != 3) {
3519
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3520
                op2_offset = offsetof(CPUX86State,xmm_t0);
3521
                gen_ldo_env_A0(s, op2_offset);
B
bellard 已提交
3522 3523 3524 3525 3526
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
            op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
B
bellard 已提交
3527 3528
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3529 3530
            switch(b) {
            case 0x02c:
B
Blue Swirl 已提交
3531
                gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3532 3533
                break;
            case 0x12c:
B
Blue Swirl 已提交
3534
                gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3535 3536
                break;
            case 0x02d:
B
Blue Swirl 已提交
3537
                gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3538 3539
                break;
            case 0x12d:
B
Blue Swirl 已提交
3540
                gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3541 3542 3543 3544 3545 3546 3547
                break;
            }
            break;
        case 0x22c: /* cvttss2si */
        case 0x32c: /* cvttsd2si */
        case 0x22d: /* cvtss2si */
        case 0x32d: /* cvtsd2si */
3548
            ot = mo_64_32(s->dflag);
B
bellard 已提交
3549
            if (mod != 3) {
3550
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3551
                if ((b >> 8) & 1) {
3552
                    gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.ZMM_Q(0)));
B
bellard 已提交
3553
                } else {
3554 3555
                    gen_op_ld_v(s, MO_32, cpu_T0, cpu_A0);
                    tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_t0.ZMM_L(0)));
B
bellard 已提交
3556 3557 3558 3559 3560 3561
                }
                op2_offset = offsetof(CPUX86State,xmm_t0);
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
B
bellard 已提交
3562
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3563
            if (ot == MO_32) {
B
Blue Swirl 已提交
3564
                SSEFunc_i_ep sse_fn_i_ep =
3565
                    sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3566
                sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3567
                tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
B
bellard 已提交
3568
            } else {
3569
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3570
                SSEFunc_l_ep sse_fn_l_ep =
3571
                    sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
3572
                sse_fn_l_ep(cpu_T0, cpu_env, cpu_ptr0);
3573 3574 3575
#else
                goto illegal_op;
#endif
B
bellard 已提交
3576
            }
3577
            gen_op_mov_reg_v(ot, reg, cpu_T0);
B
bellard 已提交
3578 3579
            break;
        case 0xc4: /* pinsrw */
3580
        case 0x1c4:
B
bellard 已提交
3581
            s->rip_offset = 1;
3582
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
3583
            val = x86_ldub_code(env, s);
B
bellard 已提交
3584 3585
            if (b1) {
                val &= 7;
3586
                tcg_gen_st16_tl(cpu_T0, cpu_env,
3587
                                offsetof(CPUX86State,xmm_regs[reg].ZMM_W(val)));
B
bellard 已提交
3588 3589
            } else {
                val &= 3;
3590
                tcg_gen_st16_tl(cpu_T0, cpu_env,
B
bellard 已提交
3591
                                offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
B
bellard 已提交
3592 3593 3594
            }
            break;
        case 0xc5: /* pextrw */
3595
        case 0x1c5:
B
bellard 已提交
3596 3597
            if (mod != 3)
                goto illegal_op;
3598
            ot = mo_64_32(s->dflag);
3599
            val = x86_ldub_code(env, s);
B
bellard 已提交
3600 3601 3602
            if (b1) {
                val &= 7;
                rm = (modrm & 7) | REX_B(s);
3603
                tcg_gen_ld16u_tl(cpu_T0, cpu_env,
3604
                                 offsetof(CPUX86State,xmm_regs[rm].ZMM_W(val)));
B
bellard 已提交
3605 3606 3607
            } else {
                val &= 3;
                rm = (modrm & 7);
3608
                tcg_gen_ld16u_tl(cpu_T0, cpu_env,
B
bellard 已提交
3609
                                offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
B
bellard 已提交
3610 3611
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3612
            gen_op_mov_reg_v(ot, reg, cpu_T0);
B
bellard 已提交
3613 3614 3615
            break;
        case 0x1d6: /* movq ea, xmm */
            if (mod != 3) {
3616
                gen_lea_modrm(env, s, modrm);
3617
                gen_stq_env_A0(s, offsetof(CPUX86State,
3618
                                           xmm_regs[reg].ZMM_Q(0)));
B
bellard 已提交
3619 3620
            } else {
                rm = (modrm & 7) | REX_B(s);
3621 3622 3623
                gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
                gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(1)));
B
bellard 已提交
3624 3625 3626
            }
            break;
        case 0x2d6: /* movq2dq */
B
Blue Swirl 已提交
3627
            gen_helper_enter_mmx(cpu_env);
3628
            rm = (modrm & 7);
3629
            gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3630
                        offsetof(CPUX86State,fpregs[rm].mmx));
3631
            gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)));
B
bellard 已提交
3632 3633
            break;
        case 0x3d6: /* movdq2q */
B
Blue Swirl 已提交
3634
            gen_helper_enter_mmx(cpu_env);
3635 3636
            rm = (modrm & 7) | REX_B(s);
            gen_op_movq(offsetof(CPUX86State,fpregs[reg & 7].mmx),
3637
                        offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
B
bellard 已提交
3638 3639 3640 3641 3642 3643 3644
            break;
        case 0xd7: /* pmovmskb */
        case 0x1d7:
            if (mod != 3)
                goto illegal_op;
            if (b1) {
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3645
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3646
                gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3647 3648
            } else {
                rm = (modrm & 7);
B
bellard 已提交
3649
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
B
Blue Swirl 已提交
3650
                gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3651 3652
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3653
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3654
            break;
R
Richard Henderson 已提交
3655

B
balrog 已提交
3656
        case 0x138:
3657
        case 0x038:
B
balrog 已提交
3658
            b = modrm;
R
Richard Henderson 已提交
3659 3660 3661
            if ((b & 0xf0) == 0xf0) {
                goto do_0f_38_fx;
            }
3662
            modrm = x86_ldub_code(env, s);
B
balrog 已提交
3663 3664 3665
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
3666
            if (b1 >= 2) {
3667
                goto unknown_op;
3668
            }
B
balrog 已提交
3669

B
Blue Swirl 已提交
3670 3671
            sse_fn_epp = sse_op_table6[b].op[b1];
            if (!sse_fn_epp) {
3672
                goto unknown_op;
B
Blue Swirl 已提交
3673
            }
B
balrog 已提交
3674 3675
            if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
                goto illegal_op;
B
balrog 已提交
3676 3677 3678 3679 3680 3681 3682

            if (b1) {
                op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
                if (mod == 3) {
                    op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
                } else {
                    op2_offset = offsetof(CPUX86State,xmm_t0);
3683
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
3684 3685 3686 3687
                    switch (b) {
                    case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
                    case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
                    case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3688
                        gen_ldq_env_A0(s, op2_offset +
3689
                                        offsetof(ZMMReg, ZMM_Q(0)));
B
balrog 已提交
3690 3691 3692
                        break;
                    case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
                    case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3693 3694
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
balrog 已提交
3695
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
3696
                                        offsetof(ZMMReg, ZMM_L(0)));
B
balrog 已提交
3697 3698
                        break;
                    case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3699 3700
                        tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
                                           s->mem_index, MO_LEUW);
B
balrog 已提交
3701
                        tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
3702
                                        offsetof(ZMMReg, ZMM_W(0)));
B
balrog 已提交
3703 3704
                        break;
                    case 0x2a:            /* movntqda */
3705
                        gen_ldo_env_A0(s, op1_offset);
B
balrog 已提交
3706 3707
                        return;
                    default:
3708
                        gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
3709
                    }
B
balrog 已提交
3710 3711 3712 3713 3714 3715 3716
                }
            } else {
                op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
                if (mod == 3) {
                    op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
                } else {
                    op2_offset = offsetof(CPUX86State,mmx_t0);
3717
                    gen_lea_modrm(env, s, modrm);
3718
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
3719 3720
                }
            }
B
Blue Swirl 已提交
3721
            if (sse_fn_epp == SSE_SPECIAL) {
3722
                goto unknown_op;
B
Blue Swirl 已提交
3723
            }
B
balrog 已提交
3724

B
balrog 已提交
3725 3726
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
3727
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
balrog 已提交
3728

3729 3730 3731
            if (b == 0x17) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
balrog 已提交
3732
            break;
R
Richard Henderson 已提交
3733 3734 3735 3736 3737 3738

        case 0x238:
        case 0x338:
        do_0f_38_fx:
            /* Various integer extensions at 0f 38 f[0-f].  */
            b = modrm | (b1 << 8);
3739
            modrm = x86_ldub_code(env, s);
B
balrog 已提交
3740 3741
            reg = ((modrm >> 3) & 7) | rex_r;

R
Richard Henderson 已提交
3742 3743 3744 3745 3746 3747 3748 3749
            switch (b) {
            case 0x3f0: /* crc32 Gd,Eb */
            case 0x3f1: /* crc32 Gd,Ey */
            do_crc32:
                if (!(s->cpuid_ext_features & CPUID_EXT_SSE42)) {
                    goto illegal_op;
                }
                if ((b & 0xff) == 0xf0) {
3750
                    ot = MO_8;
3751
                } else if (s->dflag != MO_64) {
3752
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3753
                } else {
3754
                    ot = MO_64;
R
Richard Henderson 已提交
3755
                }
B
balrog 已提交
3756

3757
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
R
Richard Henderson 已提交
3758
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3759 3760
                gen_helper_crc32(cpu_T0, cpu_tmp2_i32,
                                 cpu_T0, tcg_const_i32(8 << ot));
B
balrog 已提交
3761

3762
                ot = mo_64_32(s->dflag);
3763
                gen_op_mov_reg_v(ot, reg, cpu_T0);
R
Richard Henderson 已提交
3764
                break;
B
balrog 已提交
3765

R
Richard Henderson 已提交
3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
            case 0x1f0: /* crc32 or movbe */
            case 0x1f1:
                /* For these insns, the f3 prefix is supposed to have priority
                   over the 66 prefix, but that's not what we implement above
                   setting b1.  */
                if (s->prefix & PREFIX_REPNZ) {
                    goto do_crc32;
                }
                /* FALLTHRU */
            case 0x0f0: /* movbe Gy,My */
            case 0x0f1: /* movbe My,Gy */
                if (!(s->cpuid_ext_features & CPUID_EXT_MOVBE)) {
                    goto illegal_op;
                }
3780
                if (s->dflag != MO_64) {
3781
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3782
                } else {
3783
                    ot = MO_64;
R
Richard Henderson 已提交
3784 3785
                }

3786
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
3787
                if ((b & 1) == 0) {
3788
                    tcg_gen_qemu_ld_tl(cpu_T0, cpu_A0,
3789
                                       s->mem_index, ot | MO_BE);
3790
                    gen_op_mov_reg_v(ot, reg, cpu_T0);
R
Richard Henderson 已提交
3791
                } else {
3792 3793
                    tcg_gen_qemu_st_tl(cpu_regs[reg], cpu_A0,
                                       s->mem_index, ot | MO_BE);
R
Richard Henderson 已提交
3794 3795 3796
                }
                break;

R
Richard Henderson 已提交
3797 3798 3799 3800 3801 3802
            case 0x0f2: /* andn Gy, By, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3803
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3804
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3805
                tcg_gen_andc_tl(cpu_T0, cpu_T0, cpu_regs[s->vex_v]);
3806
                gen_op_mov_reg_v(ot, reg, cpu_T0);
R
Richard Henderson 已提交
3807 3808 3809 3810
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_LOGICB + ot);
                break;

R
Richard Henderson 已提交
3811 3812 3813 3814 3815 3816
            case 0x0f7: /* bextr Gy, Ey, By */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3817
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3818 3819 3820 3821 3822 3823 3824
                {
                    TCGv bound, zero;

                    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                    /* Extract START, and shift the operand.
                       Shifts larger than operand size get zeros.  */
                    tcg_gen_ext8u_tl(cpu_A0, cpu_regs[s->vex_v]);
3825
                    tcg_gen_shr_tl(cpu_T0, cpu_T0, cpu_A0);
R
Richard Henderson 已提交
3826

3827
                    bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3828
                    zero = tcg_const_tl(0);
3829 3830
                    tcg_gen_movcond_tl(TCG_COND_LEU, cpu_T0, cpu_A0, bound,
                                       cpu_T0, zero);
R
Richard Henderson 已提交
3831 3832 3833 3834
                    tcg_temp_free(zero);

                    /* Extract the LEN into a mask.  Lengths larger than
                       operand size get all ones.  */
3835
                    tcg_gen_extract_tl(cpu_A0, cpu_regs[s->vex_v], 8, 8);
R
Richard Henderson 已提交
3836 3837 3838
                    tcg_gen_movcond_tl(TCG_COND_LEU, cpu_A0, cpu_A0, bound,
                                       cpu_A0, bound);
                    tcg_temp_free(bound);
3839 3840 3841 3842
                    tcg_gen_movi_tl(cpu_T1, 1);
                    tcg_gen_shl_tl(cpu_T1, cpu_T1, cpu_A0);
                    tcg_gen_subi_tl(cpu_T1, cpu_T1, 1);
                    tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_T1);
R
Richard Henderson 已提交
3843

3844
                    gen_op_mov_reg_v(ot, reg, cpu_T0);
R
Richard Henderson 已提交
3845 3846 3847 3848 3849
                    gen_op_update1_cc();
                    set_cc_op(s, CC_OP_LOGICB + ot);
                }
                break;

R
Richard Henderson 已提交
3850 3851 3852 3853 3854 3855
            case 0x0f5: /* bzhi Gy, Ey, By */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3856
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3857
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3858
                tcg_gen_ext8u_tl(cpu_T1, cpu_regs[s->vex_v]);
R
Richard Henderson 已提交
3859
                {
3860
                    TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3861 3862 3863
                    /* Note that since we're using BMILG (in order to get O
                       cleared) we need to store the inverse into C.  */
                    tcg_gen_setcond_tl(TCG_COND_LT, cpu_cc_src,
3864 3865 3866
                                       cpu_T1, bound);
                    tcg_gen_movcond_tl(TCG_COND_GT, cpu_T1, cpu_T1,
                                       bound, bound, cpu_T1);
R
Richard Henderson 已提交
3867 3868 3869
                    tcg_temp_free(bound);
                }
                tcg_gen_movi_tl(cpu_A0, -1);
3870 3871 3872
                tcg_gen_shl_tl(cpu_A0, cpu_A0, cpu_T1);
                tcg_gen_andc_tl(cpu_T0, cpu_T0, cpu_A0);
                gen_op_mov_reg_v(ot, reg, cpu_T0);
R
Richard Henderson 已提交
3873 3874 3875 3876
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_BMILGB + ot);
                break;

R
Richard Henderson 已提交
3877 3878 3879 3880 3881 3882
            case 0x3f6: /* mulx By, Gy, rdx, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3883
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3884 3885 3886
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                switch (ot) {
                default:
3887
                    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
3888 3889 3890 3891 3892
                    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EDX]);
                    tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                                      cpu_tmp2_i32, cpu_tmp3_i32);
                    tcg_gen_extu_i32_tl(cpu_regs[s->vex_v], cpu_tmp2_i32);
                    tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp3_i32);
R
Richard Henderson 已提交
3893 3894
                    break;
#ifdef TARGET_X86_64
3895
                case MO_64:
3896 3897 3898 3899
                    tcg_gen_mulu2_i64(cpu_T0, cpu_T1,
                                      cpu_T0, cpu_regs[R_EDX]);
                    tcg_gen_mov_i64(cpu_regs[s->vex_v], cpu_T0);
                    tcg_gen_mov_i64(cpu_regs[reg], cpu_T1);
R
Richard Henderson 已提交
3900 3901 3902 3903 3904
                    break;
#endif
                }
                break;

3905 3906 3907 3908 3909 3910
            case 0x3f5: /* pdep Gy, By, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3911
                ot = mo_64_32(s->dflag);
3912 3913 3914
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                /* Note that by zero-extending the mask operand, we
                   automatically handle zero-extending the result.  */
3915
                if (ot == MO_64) {
3916
                    tcg_gen_mov_tl(cpu_T1, cpu_regs[s->vex_v]);
3917
                } else {
3918
                    tcg_gen_ext32u_tl(cpu_T1, cpu_regs[s->vex_v]);
3919
                }
3920
                gen_helper_pdep(cpu_regs[reg], cpu_T0, cpu_T1);
3921 3922 3923 3924 3925 3926 3927 3928
                break;

            case 0x2f5: /* pext Gy, By, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
3929
                ot = mo_64_32(s->dflag);
3930 3931 3932
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                /* Note that by zero-extending the mask operand, we
                   automatically handle zero-extending the result.  */
3933
                if (ot == MO_64) {
3934
                    tcg_gen_mov_tl(cpu_T1, cpu_regs[s->vex_v]);
3935
                } else {
3936
                    tcg_gen_ext32u_tl(cpu_T1, cpu_regs[s->vex_v]);
3937
                }
3938
                gen_helper_pext(cpu_regs[reg], cpu_T0, cpu_T1);
3939 3940
                break;

3941 3942 3943 3944 3945
            case 0x1f6: /* adcx Gy, Ey */
            case 0x2f6: /* adox Gy, Ey */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_ADX)) {
                    goto illegal_op;
                } else {
3946
                    TCGv carry_in, carry_out, zero;
3947 3948
                    int end_op;

3949
                    ot = mo_64_32(s->dflag);
3950 3951 3952
                    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);

                    /* Re-use the carry-out from a previous round.  */
3953
                    carry_in = NULL;
3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976
                    carry_out = (b == 0x1f6 ? cpu_cc_dst : cpu_cc_src2);
                    switch (s->cc_op) {
                    case CC_OP_ADCX:
                        if (b == 0x1f6) {
                            carry_in = cpu_cc_dst;
                            end_op = CC_OP_ADCX;
                        } else {
                            end_op = CC_OP_ADCOX;
                        }
                        break;
                    case CC_OP_ADOX:
                        if (b == 0x1f6) {
                            end_op = CC_OP_ADCOX;
                        } else {
                            carry_in = cpu_cc_src2;
                            end_op = CC_OP_ADOX;
                        }
                        break;
                    case CC_OP_ADCOX:
                        end_op = CC_OP_ADCOX;
                        carry_in = carry_out;
                        break;
                    default:
3977
                        end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
3978 3979 3980
                        break;
                    }
                    /* If we can't reuse carry-out, get it out of EFLAGS.  */
3981
                    if (!carry_in) {
3982 3983 3984 3985
                        if (s->cc_op != CC_OP_ADCX && s->cc_op != CC_OP_ADOX) {
                            gen_compute_eflags(s);
                        }
                        carry_in = cpu_tmp0;
3986 3987
                        tcg_gen_extract_tl(carry_in, cpu_cc_src,
                                           ctz32(b == 0x1f6 ? CC_C : CC_O), 1);
3988 3989 3990 3991
                    }

                    switch (ot) {
#ifdef TARGET_X86_64
3992
                    case MO_32:
3993 3994 3995
                        /* If we know TL is 64-bit, and we want a 32-bit
                           result, just do everything in 64-bit arithmetic.  */
                        tcg_gen_ext32u_i64(cpu_regs[reg], cpu_regs[reg]);
3996 3997 3998 3999 4000
                        tcg_gen_ext32u_i64(cpu_T0, cpu_T0);
                        tcg_gen_add_i64(cpu_T0, cpu_T0, cpu_regs[reg]);
                        tcg_gen_add_i64(cpu_T0, cpu_T0, carry_in);
                        tcg_gen_ext32u_i64(cpu_regs[reg], cpu_T0);
                        tcg_gen_shri_i64(carry_out, cpu_T0, 32);
4001 4002 4003 4004
                        break;
#endif
                    default:
                        /* Otherwise compute the carry-out in two steps.  */
4005
                        zero = tcg_const_tl(0);
4006 4007
                        tcg_gen_add2_tl(cpu_T0, carry_out,
                                        cpu_T0, zero,
4008 4009 4010
                                        carry_in, zero);
                        tcg_gen_add2_tl(cpu_regs[reg], carry_out,
                                        cpu_regs[reg], carry_out,
4011
                                        cpu_T0, zero);
4012
                        tcg_temp_free(zero);
4013 4014 4015 4016 4017 4018
                        break;
                    }
                    set_cc_op(s, end_op);
                }
                break;

4019 4020 4021 4022 4023 4024 4025 4026
            case 0x1f7: /* shlx Gy, Ey, By */
            case 0x2f7: /* sarx Gy, Ey, By */
            case 0x3f7: /* shrx Gy, Ey, By */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
4027
                ot = mo_64_32(s->dflag);
4028
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4029
                if (ot == MO_64) {
4030
                    tcg_gen_andi_tl(cpu_T1, cpu_regs[s->vex_v], 63);
4031
                } else {
4032
                    tcg_gen_andi_tl(cpu_T1, cpu_regs[s->vex_v], 31);
4033 4034
                }
                if (b == 0x1f7) {
4035
                    tcg_gen_shl_tl(cpu_T0, cpu_T0, cpu_T1);
4036
                } else if (b == 0x2f7) {
4037
                    if (ot != MO_64) {
4038
                        tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
4039
                    }
4040
                    tcg_gen_sar_tl(cpu_T0, cpu_T0, cpu_T1);
4041
                } else {
4042
                    if (ot != MO_64) {
4043
                        tcg_gen_ext32u_tl(cpu_T0, cpu_T0);
4044
                    }
4045
                    tcg_gen_shr_tl(cpu_T0, cpu_T0, cpu_T1);
4046
                }
4047
                gen_op_mov_reg_v(ot, reg, cpu_T0);
4048 4049
                break;

4050 4051 4052 4053 4054 4055 4056 4057 4058
            case 0x0f3:
            case 0x1f3:
            case 0x2f3:
            case 0x3f3: /* Group 17 */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
4059
                ot = mo_64_32(s->dflag);
4060 4061 4062 4063
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);

                switch (reg & 7) {
                case 1: /* blsr By,Ey */
4064 4065 4066
                    tcg_gen_neg_tl(cpu_T1, cpu_T0);
                    tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_T1);
                    gen_op_mov_reg_v(ot, s->vex_v, cpu_T0);
4067 4068 4069 4070 4071
                    gen_op_update2_cc();
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

                case 2: /* blsmsk By,Ey */
4072 4073 4074 4075
                    tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
                    tcg_gen_subi_tl(cpu_T0, cpu_T0, 1);
                    tcg_gen_xor_tl(cpu_T0, cpu_T0, cpu_cc_src);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4076 4077 4078 4079
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

                case 3: /* blsi By, Ey */
4080 4081 4082 4083
                    tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
                    tcg_gen_subi_tl(cpu_T0, cpu_T0, 1);
                    tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_cc_src);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4084 4085 4086 4087
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

                default:
4088
                    goto unknown_op;
4089 4090 4091
                }
                break;

R
Richard Henderson 已提交
4092
            default:
4093
                goto unknown_op;
R
Richard Henderson 已提交
4094
            }
B
balrog 已提交
4095
            break;
R
Richard Henderson 已提交
4096

B
balrog 已提交
4097 4098
        case 0x03a:
        case 0x13a:
B
balrog 已提交
4099
            b = modrm;
4100
            modrm = x86_ldub_code(env, s);
B
balrog 已提交
4101 4102 4103
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
4104
            if (b1 >= 2) {
4105
                goto unknown_op;
4106
            }
B
balrog 已提交
4107

B
Blue Swirl 已提交
4108 4109
            sse_fn_eppi = sse_op_table7[b].op[b1];
            if (!sse_fn_eppi) {
4110
                goto unknown_op;
B
Blue Swirl 已提交
4111
            }
B
balrog 已提交
4112 4113 4114
            if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
                goto illegal_op;

4115 4116
            s->rip_offset = 1;

B
Blue Swirl 已提交
4117
            if (sse_fn_eppi == SSE_SPECIAL) {
4118
                ot = mo_64_32(s->dflag);
B
balrog 已提交
4119 4120
                rm = (modrm & 7) | REX_B(s);
                if (mod != 3)
4121
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
4122
                reg = ((modrm >> 3) & 7) | rex_r;
4123
                val = x86_ldub_code(env, s);
B
balrog 已提交
4124 4125
                switch (b) {
                case 0x14: /* pextrb */
4126
                    tcg_gen_ld8u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
4127
                                            xmm_regs[reg].ZMM_B(val & 15)));
4128
                    if (mod == 3) {
4129
                        gen_op_mov_reg_v(ot, rm, cpu_T0);
4130
                    } else {
4131
                        tcg_gen_qemu_st_tl(cpu_T0, cpu_A0,
4132 4133
                                           s->mem_index, MO_UB);
                    }
B
balrog 已提交
4134 4135
                    break;
                case 0x15: /* pextrw */
4136
                    tcg_gen_ld16u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
4137
                                            xmm_regs[reg].ZMM_W(val & 7)));
4138
                    if (mod == 3) {
4139
                        gen_op_mov_reg_v(ot, rm, cpu_T0);
4140
                    } else {
4141
                        tcg_gen_qemu_st_tl(cpu_T0, cpu_A0,
4142 4143
                                           s->mem_index, MO_LEUW);
                    }
B
balrog 已提交
4144 4145
                    break;
                case 0x16:
4146
                    if (ot == MO_32) { /* pextrd */
B
balrog 已提交
4147 4148
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
4149
                                                xmm_regs[reg].ZMM_L(val & 3)));
4150
                        if (mod == 3) {
4151
                            tcg_gen_extu_i32_tl(cpu_regs[rm], cpu_tmp2_i32);
4152
                        } else {
4153 4154
                            tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4155
                        }
B
balrog 已提交
4156
                    } else { /* pextrq */
P
pbrook 已提交
4157
#ifdef TARGET_X86_64
B
balrog 已提交
4158 4159
                        tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
4160
                                                xmm_regs[reg].ZMM_Q(val & 1)));
4161
                        if (mod == 3) {
4162
                            tcg_gen_mov_i64(cpu_regs[rm], cpu_tmp1_i64);
4163 4164 4165 4166
                        } else {
                            tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
P
pbrook 已提交
4167 4168 4169
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4170 4171 4172
                    }
                    break;
                case 0x17: /* extractps */
4173
                    tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
4174
                                            xmm_regs[reg].ZMM_L(val & 3)));
4175
                    if (mod == 3) {
4176
                        gen_op_mov_reg_v(ot, rm, cpu_T0);
4177
                    } else {
4178
                        tcg_gen_qemu_st_tl(cpu_T0, cpu_A0,
4179 4180
                                           s->mem_index, MO_LEUL);
                    }
B
balrog 已提交
4181 4182
                    break;
                case 0x20: /* pinsrb */
4183
                    if (mod == 3) {
4184
                        gen_op_mov_v_reg(MO_32, cpu_T0, rm);
4185
                    } else {
4186
                        tcg_gen_qemu_ld_tl(cpu_T0, cpu_A0,
4187 4188
                                           s->mem_index, MO_UB);
                    }
4189
                    tcg_gen_st8_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
4190
                                            xmm_regs[reg].ZMM_B(val & 15)));
B
balrog 已提交
4191 4192
                    break;
                case 0x21: /* insertps */
P
pbrook 已提交
4193
                    if (mod == 3) {
B
balrog 已提交
4194 4195
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,xmm_regs[rm]
4196
                                                .ZMM_L((val >> 6) & 3)));
P
pbrook 已提交
4197
                    } else {
4198 4199
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
P
pbrook 已提交
4200
                    }
B
balrog 已提交
4201 4202
                    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                    offsetof(CPUX86State,xmm_regs[reg]
4203
                                            .ZMM_L((val >> 4) & 3)));
B
balrog 已提交
4204 4205 4206
                    if ((val >> 0) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
4207
                                                xmm_regs[reg].ZMM_L(0)));
B
balrog 已提交
4208 4209 4210
                    if ((val >> 1) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
4211
                                                xmm_regs[reg].ZMM_L(1)));
B
balrog 已提交
4212 4213 4214
                    if ((val >> 2) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
4215
                                                xmm_regs[reg].ZMM_L(2)));
B
balrog 已提交
4216 4217 4218
                    if ((val >> 3) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
4219
                                                xmm_regs[reg].ZMM_L(3)));
B
balrog 已提交
4220 4221
                    break;
                case 0x22:
4222
                    if (ot == MO_32) { /* pinsrd */
4223
                        if (mod == 3) {
4224
                            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[rm]);
4225
                        } else {
4226 4227
                            tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4228
                        }
B
balrog 已提交
4229 4230
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
4231
                                                xmm_regs[reg].ZMM_L(val & 3)));
B
balrog 已提交
4232
                    } else { /* pinsrq */
P
pbrook 已提交
4233
#ifdef TARGET_X86_64
4234
                        if (mod == 3) {
B
balrog 已提交
4235
                            gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
4236 4237 4238 4239
                        } else {
                            tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
B
balrog 已提交
4240 4241
                        tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
4242
                                                xmm_regs[reg].ZMM_Q(val & 1)));
P
pbrook 已提交
4243 4244 4245
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4246 4247 4248 4249 4250
                    }
                    break;
                }
                return;
            }
B
balrog 已提交
4251 4252 4253 4254 4255 4256 4257

            if (b1) {
                op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
                if (mod == 3) {
                    op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
                } else {
                    op2_offset = offsetof(CPUX86State,xmm_t0);
4258
                    gen_lea_modrm(env, s, modrm);
4259
                    gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
4260 4261 4262 4263 4264 4265 4266
                }
            } else {
                op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
                if (mod == 3) {
                    op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
                } else {
                    op2_offset = offsetof(CPUX86State,mmx_t0);
4267
                    gen_lea_modrm(env, s, modrm);
4268
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
4269 4270
                }
            }
4271
            val = x86_ldub_code(env, s);
B
balrog 已提交
4272

B
balrog 已提交
4273
            if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4274
                set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
4275

4276
                if (s->dflag == MO_64) {
B
balrog 已提交
4277 4278
                    /* The helper must use entire 64-bit gp registers */
                    val |= 1 << 8;
4279
                }
B
balrog 已提交
4280 4281
            }

B
balrog 已提交
4282 4283
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4284
            sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4285
            break;
R
Richard Henderson 已提交
4286 4287 4288 4289

        case 0x33a:
            /* Various integer extensions at 0f 3a f[0-f].  */
            b = modrm | (b1 << 8);
4290
            modrm = x86_ldub_code(env, s);
R
Richard Henderson 已提交
4291 4292 4293 4294 4295 4296 4297 4298 4299
            reg = ((modrm >> 3) & 7) | rex_r;

            switch (b) {
            case 0x3f0: /* rorx Gy,Ey, Ib */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
                    || !(s->prefix & PREFIX_VEX)
                    || s->vex_l != 0) {
                    goto illegal_op;
                }
4300
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
4301
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4302
                b = x86_ldub_code(env, s);
4303
                if (ot == MO_64) {
4304
                    tcg_gen_rotri_tl(cpu_T0, cpu_T0, b & 63);
R
Richard Henderson 已提交
4305
                } else {
4306
                    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
R
Richard Henderson 已提交
4307
                    tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, b & 31);
4308
                    tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
R
Richard Henderson 已提交
4309
                }
4310
                gen_op_mov_reg_v(ot, reg, cpu_T0);
R
Richard Henderson 已提交
4311 4312 4313
                break;

            default:
4314
                goto unknown_op;
R
Richard Henderson 已提交
4315 4316 4317
            }
            break;

B
bellard 已提交
4318
        default:
4319 4320 4321
        unknown_op:
            gen_unknown_opcode(env, s);
            return;
B
bellard 已提交
4322 4323 4324
        }
    } else {
        /* generic MMX or SSE operation */
B
bellard 已提交
4325 4326 4327 4328 4329 4330 4331 4332
        switch(b) {
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
        case 0xc2: /* compare insns */
            s->rip_offset = 1;
            break;
        default:
            break;
B
bellard 已提交
4333 4334 4335 4336
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
4337 4338
                int sz = 4;

4339
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4340
                op2_offset = offsetof(CPUX86State,xmm_t0);
4341 4342 4343 4344 4345 4346

                switch (b) {
                case 0x50 ... 0x5a:
                case 0x5c ... 0x5f:
                case 0xc2:
                    /* Most sse scalar operations.  */
B
bellard 已提交
4347
                    if (b1 == 2) {
4348 4349 4350 4351 4352 4353 4354 4355 4356 4357
                        sz = 2;
                    } else if (b1 == 3) {
                        sz = 3;
                    }
                    break;

                case 0x2e:  /* ucomis[sd] */
                case 0x2f:  /* comis[sd] */
                    if (b1 == 0) {
                        sz = 2;
B
bellard 已提交
4358
                    } else {
4359
                        sz = 3;
B
bellard 已提交
4360
                    }
4361 4362 4363 4364 4365 4366
                    break;
                }

                switch (sz) {
                case 2:
                    /* 32 bit access */
4367 4368
                    gen_op_ld_v(s, MO_32, cpu_T0, cpu_A0);
                    tcg_gen_st32_tl(cpu_T0, cpu_env,
4369
                                    offsetof(CPUX86State,xmm_t0.ZMM_L(0)));
4370 4371 4372
                    break;
                case 3:
                    /* 64 bit access */
4373
                    gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.ZMM_D(0)));
4374 4375 4376
                    break;
                default:
                    /* 128 bit access */
4377
                    gen_ldo_env_A0(s, op2_offset);
4378
                    break;
B
bellard 已提交
4379 4380 4381 4382 4383 4384 4385 4386
                }
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
        } else {
            op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
            if (mod != 3) {
4387
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4388
                op2_offset = offsetof(CPUX86State,mmx_t0);
4389
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
4390 4391 4392 4393 4394 4395
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
        }
        switch(b) {
A
aurel32 已提交
4396
        case 0x0f: /* 3DNow! data insns */
4397
            val = x86_ldub_code(env, s);
B
Blue Swirl 已提交
4398 4399
            sse_fn_epp = sse_op_table5[val];
            if (!sse_fn_epp) {
4400 4401 4402
                goto unknown_op;
            }
            if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW)) {
A
aurel32 已提交
4403
                goto illegal_op;
B
Blue Swirl 已提交
4404
            }
B
bellard 已提交
4405 4406
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4407
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
A
aurel32 已提交
4408
            break;
B
bellard 已提交
4409 4410
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
4411
            val = x86_ldub_code(env, s);
B
bellard 已提交
4412 4413
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4414
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4415
            sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
B
Blue Swirl 已提交
4416
            sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
bellard 已提交
4417 4418 4419
            break;
        case 0xc2:
            /* compare insns */
4420
            val = x86_ldub_code(env, s);
B
bellard 已提交
4421
            if (val >= 8)
4422
                goto unknown_op;
B
Blue Swirl 已提交
4423
            sse_fn_epp = sse_op_table4[val][b1];
B
Blue Swirl 已提交
4424

B
bellard 已提交
4425 4426
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4427
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4428
            break;
4429 4430 4431 4432
        case 0xf7:
            /* maskmov : we must prepare A0 */
            if (mod != 3)
                goto illegal_op;
4433 4434
            tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EDI]);
            gen_extu(s->aflag, cpu_A0);
4435 4436 4437 4438
            gen_add_A0_ds_seg(s);

            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4439
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4440 4441
            sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
            sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4442
            break;
B
bellard 已提交
4443
        default:
B
bellard 已提交
4444 4445
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4446
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4447 4448 4449
            break;
        }
        if (b == 0x2e || b == 0x2f) {
4450
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
4451 4452 4453 4454
        }
    }
}

4455
/* convert one instruction. s->base.is_jmp is set if the translation must
B
bellard 已提交
4456
   be stopped. Return the next pc value */
4457
static target_ulong disas_insn(DisasContext *s, CPUState *cpu)
B
bellard 已提交
4458
{
4459
    CPUX86State *env = cpu->env_ptr;
4460
    int b, prefixes;
4461
    int shift;
4462
    TCGMemOp ot, aflag, dflag;
4463
    int modrm, reg, rm, mod, op, opreg, val;
B
bellard 已提交
4464 4465
    target_ulong next_eip, tval;
    int rex_w, rex_r;
4466
    target_ulong pc_start = s->base.pc_next;
B
bellard 已提交
4467

4468
    s->pc_start = s->pc = pc_start;
B
bellard 已提交
4469
    s->override = -1;
B
bellard 已提交
4470 4471 4472
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4473
    x86_64_hregs = 0;
B
bellard 已提交
4474 4475
#endif
    s->rip_offset = 0; /* for relative ip address */
4476 4477
    s->vex_l = 0;
    s->vex_v = 0;
4478 4479 4480
    if (sigsetjmp(s->jmpbuf, 0) != 0) {
        gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        return s->pc;
4481
    }
4482

S
Stefan Weil 已提交
4483 4484 4485 4486
    prefixes = 0;
    rex_w = -1;
    rex_r = 0;

4487
 next_byte:
4488
    b = x86_ldub_code(env, s);
4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523
    /* Collect prefixes.  */
    switch (b) {
    case 0xf3:
        prefixes |= PREFIX_REPZ;
        goto next_byte;
    case 0xf2:
        prefixes |= PREFIX_REPNZ;
        goto next_byte;
    case 0xf0:
        prefixes |= PREFIX_LOCK;
        goto next_byte;
    case 0x2e:
        s->override = R_CS;
        goto next_byte;
    case 0x36:
        s->override = R_SS;
        goto next_byte;
    case 0x3e:
        s->override = R_DS;
        goto next_byte;
    case 0x26:
        s->override = R_ES;
        goto next_byte;
    case 0x64:
        s->override = R_FS;
        goto next_byte;
    case 0x65:
        s->override = R_GS;
        goto next_byte;
    case 0x66:
        prefixes |= PREFIX_DATA;
        goto next_byte;
    case 0x67:
        prefixes |= PREFIX_ADR;
        goto next_byte;
B
bellard 已提交
4524
#ifdef TARGET_X86_64
4525 4526
    case 0x40 ... 0x4f:
        if (CODE64(s)) {
B
bellard 已提交
4527 4528 4529 4530 4531 4532 4533 4534
            /* REX prefix */
            rex_w = (b >> 3) & 1;
            rex_r = (b & 0x4) << 1;
            s->rex_x = (b & 0x2) << 2;
            REX_B(s) = (b & 0x1) << 3;
            x86_64_hregs = 1; /* select uniform byte register addressing */
            goto next_byte;
        }
4535 4536
        break;
#endif
4537 4538 4539 4540 4541 4542 4543 4544
    case 0xc5: /* 2-byte VEX */
    case 0xc4: /* 3-byte VEX */
        /* VEX prefixes cannot be used except in 32-bit mode.
           Otherwise the instruction is LES or LDS.  */
        if (s->code32 && !s->vm86) {
            static const int pp_prefix[4] = {
                0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ
            };
4545
            int vex3, vex2 = x86_ldub_code(env, s);
4546 4547 4548 4549

            if (!CODE64(s) && (vex2 & 0xc0) != 0xc0) {
                /* 4.1.4.6: In 32-bit mode, bits [7:6] must be 11b,
                   otherwise the instruction is LES or LDS.  */
4550
                s->pc--; /* rewind the advance_pc() x86_ldub_code() did */
4551 4552 4553
                break;
            }

P
Peter Maydell 已提交
4554
            /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565
            if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ
                            | PREFIX_LOCK | PREFIX_DATA)) {
                goto illegal_op;
            }
#ifdef TARGET_X86_64
            if (x86_64_hregs) {
                goto illegal_op;
            }
#endif
            rex_r = (~vex2 >> 4) & 8;
            if (b == 0xc5) {
4566
                /* 2-byte VEX prefix: RVVVVlpp, implied 0f leading opcode byte */
4567
                vex3 = vex2;
4568
                b = x86_ldub_code(env, s) | 0x100;
4569
            } else {
4570
                /* 3-byte VEX prefix: RXBmmmmm wVVVVlpp */
4571 4572 4573 4574
#ifdef TARGET_X86_64
                s->rex_x = (~vex2 >> 3) & 8;
                s->rex_b = (~vex2 >> 2) & 8;
#endif
4575
                vex3 = x86_ldub_code(env, s);
4576 4577 4578
                rex_w = (vex3 >> 7) & 1;
                switch (vex2 & 0x1f) {
                case 0x01: /* Implied 0f leading opcode bytes.  */
4579
                    b = x86_ldub_code(env, s) | 0x100;
4580 4581 4582 4583 4584 4585 4586 4587
                    break;
                case 0x02: /* Implied 0f 38 leading opcode bytes.  */
                    b = 0x138;
                    break;
                case 0x03: /* Implied 0f 3a leading opcode bytes.  */
                    b = 0x13a;
                    break;
                default:   /* Reserved for future use.  */
4588
                    goto unknown_op;
4589 4590 4591 4592 4593 4594 4595
                }
            }
            s->vex_v = (~vex3 >> 3) & 0xf;
            s->vex_l = (vex3 >> 2) & 1;
            prefixes |= pp_prefix[vex3 & 3] | PREFIX_VEX;
        }
        break;
4596 4597 4598 4599
    }

    /* Post-process prefixes.  */
    if (CODE64(s)) {
4600 4601 4602
        /* In 64-bit mode, the default data size is 32-bit.  Select 64-bit
           data with rex_w, and 16-bit data with 0x66; rex_w takes precedence
           over 0x66 if both are present.  */
4603
        dflag = (rex_w > 0 ? MO_64 : prefixes & PREFIX_DATA ? MO_16 : MO_32);
4604
        /* In 64-bit mode, 0x67 selects 32-bit addressing.  */
4605
        aflag = (prefixes & PREFIX_ADR ? MO_32 : MO_64);
4606 4607
    } else {
        /* In 16/32-bit mode, 0x66 selects the opposite data size.  */
4608 4609 4610 4611
        if (s->code32 ^ ((prefixes & PREFIX_DATA) != 0)) {
            dflag = MO_32;
        } else {
            dflag = MO_16;
B
bellard 已提交
4612
        }
4613
        /* In 16/32-bit mode, 0x67 selects the opposite addressing.  */
4614 4615 4616 4617
        if (s->code32 ^ ((prefixes & PREFIX_ADR) != 0)) {
            aflag = MO_32;
        }  else {
            aflag = MO_16;
B
bellard 已提交
4618
        }
B
bellard 已提交
4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630
    }

    s->prefix = prefixes;
    s->aflag = aflag;
    s->dflag = dflag;

    /* now check op code */
 reswitch:
    switch(b) {
    case 0x0f:
        /**************************/
        /* extended op code */
4631
        b = x86_ldub_code(env, s) | 0x100;
B
bellard 已提交
4632
        goto reswitch;
4633

B
bellard 已提交
4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648
        /**************************/
        /* arith & logic */
    case 0x00 ... 0x05:
    case 0x08 ... 0x0d:
    case 0x10 ... 0x15:
    case 0x18 ... 0x1d:
    case 0x20 ... 0x25:
    case 0x28 ... 0x2d:
    case 0x30 ... 0x35:
    case 0x38 ... 0x3d:
        {
            int op, f, val;
            op = (b >> 3) & 7;
            f = (b >> 1) & 3;

4649
            ot = mo_b_d(b, dflag);
4650

B
bellard 已提交
4651 4652
            switch(f) {
            case 0: /* OP Ev, Gv */
4653
                modrm = x86_ldub_code(env, s);
B
bellard 已提交
4654
                reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4655
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4656
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4657
                if (mod != 3) {
4658
                    gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4659 4660 4661 4662
                    opreg = OR_TMP0;
                } else if (op == OP_XORL && rm == reg) {
                xor_zero:
                    /* xor reg, reg optimisation */
R
Richard Henderson 已提交
4663
                    set_cc_op(s, CC_OP_CLR);
4664 4665
                    tcg_gen_movi_tl(cpu_T0, 0);
                    gen_op_mov_reg_v(ot, reg, cpu_T0);
B
bellard 已提交
4666 4667 4668 4669
                    break;
                } else {
                    opreg = rm;
                }
4670
                gen_op_mov_v_reg(ot, cpu_T1, reg);
B
bellard 已提交
4671 4672 4673
                gen_op(s, op, ot, opreg);
                break;
            case 1: /* OP Gv, Ev */
4674
                modrm = x86_ldub_code(env, s);
B
bellard 已提交
4675
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4676 4677
                reg = ((modrm >> 3) & 7) | rex_r;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4678
                if (mod != 3) {
4679
                    gen_lea_modrm(env, s, modrm);
4680
                    gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
B
bellard 已提交
4681 4682 4683
                } else if (op == OP_XORL && rm == reg) {
                    goto xor_zero;
                } else {
4684
                    gen_op_mov_v_reg(ot, cpu_T1, rm);
B
bellard 已提交
4685 4686 4687 4688
                }
                gen_op(s, op, ot, reg);
                break;
            case 2: /* OP A, Iv */
4689
                val = insn_get(env, s, ot);
4690
                tcg_gen_movi_tl(cpu_T1, val);
B
bellard 已提交
4691 4692 4693 4694 4695 4696
                gen_op(s, op, ot, OR_EAX);
                break;
            }
        }
        break;

4697 4698 4699
    case 0x82:
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4700 4701 4702 4703 4704 4705
    case 0x80: /* GRP1 */
    case 0x81:
    case 0x83:
        {
            int val;

4706
            ot = mo_b_d(b, dflag);
4707

4708
            modrm = x86_ldub_code(env, s);
B
bellard 已提交
4709
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4710
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4711
            op = (modrm >> 3) & 7;
4712

B
bellard 已提交
4713
            if (mod != 3) {
B
bellard 已提交
4714 4715 4716 4717
                if (b == 0x83)
                    s->rip_offset = 1;
                else
                    s->rip_offset = insn_const_size(ot);
4718
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4719 4720
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
4721
                opreg = rm;
B
bellard 已提交
4722 4723 4724 4725 4726 4727
            }

            switch(b) {
            default:
            case 0x80:
            case 0x81:
4728
            case 0x82:
4729
                val = insn_get(env, s, ot);
B
bellard 已提交
4730 4731
                break;
            case 0x83:
4732
                val = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
4733 4734
                break;
            }
4735
            tcg_gen_movi_tl(cpu_T1, val);
B
bellard 已提交
4736 4737 4738 4739 4740 4741 4742
            gen_op(s, op, ot, opreg);
        }
        break;

        /**************************/
        /* inc, dec, and other misc arith */
    case 0x40 ... 0x47: /* inc Gv */
4743
        ot = dflag;
B
bellard 已提交
4744 4745 4746
        gen_inc(s, ot, OR_EAX + (b & 7), 1);
        break;
    case 0x48 ... 0x4f: /* dec Gv */
4747
        ot = dflag;
B
bellard 已提交
4748 4749 4750 4751
        gen_inc(s, ot, OR_EAX + (b & 7), -1);
        break;
    case 0xf6: /* GRP3 */
    case 0xf7:
4752
        ot = mo_b_d(b, dflag);
B
bellard 已提交
4753

4754
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
4755
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4756
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4757 4758
        op = (modrm >> 3) & 7;
        if (mod != 3) {
4759
            if (op == 0) {
B
bellard 已提交
4760
                s->rip_offset = insn_const_size(ot);
4761
            }
4762
            gen_lea_modrm(env, s, modrm);
4763 4764 4765 4766 4767
            /* For those below that handle locked memory, don't load here.  */
            if (!(s->prefix & PREFIX_LOCK)
                || op != 2) {
                gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
            }
B
bellard 已提交
4768
        } else {
4769
            gen_op_mov_v_reg(ot, cpu_T0, rm);
B
bellard 已提交
4770 4771 4772 4773
        }

        switch(op) {
        case 0: /* test */
4774
            val = insn_get(env, s, ot);
4775
            tcg_gen_movi_tl(cpu_T1, val);
B
bellard 已提交
4776
            gen_op_testl_T0_T1_cc();
4777
            set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4778 4779
            break;
        case 2: /* not */
4780 4781 4782 4783 4784 4785 4786
            if (s->prefix & PREFIX_LOCK) {
                if (mod == 3) {
                    goto illegal_op;
                }
                tcg_gen_movi_tl(cpu_T0, ~0);
                tcg_gen_atomic_xor_fetch_tl(cpu_T0, cpu_A0, cpu_T0,
                                            s->mem_index, ot | MO_LE);
B
bellard 已提交
4787
            } else {
4788 4789 4790 4791 4792 4793
                tcg_gen_not_tl(cpu_T0, cpu_T0);
                if (mod != 3) {
                    gen_op_st_v(s, ot, cpu_T0, cpu_A0);
                } else {
                    gen_op_mov_reg_v(ot, rm, cpu_T0);
                }
B
bellard 已提交
4794 4795 4796
            }
            break;
        case 3: /* neg */
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
            if (s->prefix & PREFIX_LOCK) {
                TCGLabel *label1;
                TCGv a0, t0, t1, t2;

                if (mod == 3) {
                    goto illegal_op;
                }
                a0 = tcg_temp_local_new();
                t0 = tcg_temp_local_new();
                label1 = gen_new_label();

                tcg_gen_mov_tl(a0, cpu_A0);
                tcg_gen_mov_tl(t0, cpu_T0);

                gen_set_label(label1);
                t1 = tcg_temp_new();
                t2 = tcg_temp_new();
                tcg_gen_mov_tl(t2, t0);
                tcg_gen_neg_tl(t1, t0);
                tcg_gen_atomic_cmpxchg_tl(t0, a0, t0, t1,
                                          s->mem_index, ot | MO_LE);
                tcg_temp_free(t1);
                tcg_gen_brcond_tl(TCG_COND_NE, t0, t2, label1);

                tcg_temp_free(t2);
                tcg_temp_free(a0);
                tcg_gen_mov_tl(cpu_T0, t0);
                tcg_temp_free(t0);
B
bellard 已提交
4825
            } else {
4826 4827 4828 4829 4830 4831
                tcg_gen_neg_tl(cpu_T0, cpu_T0);
                if (mod != 3) {
                    gen_op_st_v(s, ot, cpu_T0, cpu_A0);
                } else {
                    gen_op_mov_reg_v(ot, rm, cpu_T0);
                }
B
bellard 已提交
4832 4833
            }
            gen_op_update_neg_cc();
4834
            set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
4835 4836 4837
            break;
        case 4: /* mul */
            switch(ot) {
4838
            case MO_8:
4839 4840 4841
                gen_op_mov_v_reg(MO_8, cpu_T1, R_EAX);
                tcg_gen_ext8u_tl(cpu_T0, cpu_T0);
                tcg_gen_ext8u_tl(cpu_T1, cpu_T1);
B
bellard 已提交
4842
                /* XXX: use 32 bit mul which could be faster */
4843 4844 4845 4846
                tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
                tcg_gen_andi_tl(cpu_cc_src, cpu_T0, 0xff00);
4847
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4848
                break;
4849
            case MO_16:
4850 4851 4852
                gen_op_mov_v_reg(MO_16, cpu_T1, R_EAX);
                tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
                tcg_gen_ext16u_tl(cpu_T1, cpu_T1);
B
bellard 已提交
4853
                /* XXX: use 32 bit mul which could be faster */
4854 4855 4856 4857 4858 4859
                tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
                tcg_gen_shri_tl(cpu_T0, cpu_T0, 16);
                gen_op_mov_reg_v(MO_16, R_EDX, cpu_T0);
                tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
4860
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4861 4862
                break;
            default:
4863
            case MO_32:
4864
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
4865 4866 4867 4868 4869 4870 4871
                tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
                tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                                  cpu_tmp2_i32, cpu_tmp3_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
4872
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4873
                break;
B
bellard 已提交
4874
#ifdef TARGET_X86_64
4875
            case MO_64:
4876
                tcg_gen_mulu2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
4877
                                  cpu_T0, cpu_regs[R_EAX]);
4878 4879
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
4880
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4881 4882
                break;
#endif
B
bellard 已提交
4883 4884 4885 4886
            }
            break;
        case 5: /* imul */
            switch(ot) {
4887
            case MO_8:
4888 4889 4890
                gen_op_mov_v_reg(MO_8, cpu_T1, R_EAX);
                tcg_gen_ext8s_tl(cpu_T0, cpu_T0);
                tcg_gen_ext8s_tl(cpu_T1, cpu_T1);
B
bellard 已提交
4891
                /* XXX: use 32 bit mul which could be faster */
4892 4893 4894 4895 4896
                tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
                tcg_gen_ext8s_tl(cpu_tmp0, cpu_T0);
                tcg_gen_sub_tl(cpu_cc_src, cpu_T0, cpu_tmp0);
4897
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4898
                break;
4899
            case MO_16:
4900 4901 4902
                gen_op_mov_v_reg(MO_16, cpu_T1, R_EAX);
                tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
                tcg_gen_ext16s_tl(cpu_T1, cpu_T1);
B
bellard 已提交
4903
                /* XXX: use 32 bit mul which could be faster */
4904 4905 4906 4907 4908 4909 4910
                tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
                tcg_gen_ext16s_tl(cpu_tmp0, cpu_T0);
                tcg_gen_sub_tl(cpu_cc_src, cpu_T0, cpu_tmp0);
                tcg_gen_shri_tl(cpu_T0, cpu_T0, 16);
                gen_op_mov_reg_v(MO_16, R_EDX, cpu_T0);
4911
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4912 4913
                break;
            default:
4914
            case MO_32:
4915
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
4916 4917 4918 4919 4920 4921 4922 4923 4924
                tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
                tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                                  cpu_tmp2_i32, cpu_tmp3_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
                tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
                tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
                tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
4925
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4926
                break;
B
bellard 已提交
4927
#ifdef TARGET_X86_64
4928
            case MO_64:
4929
                tcg_gen_muls2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
4930
                                  cpu_T0, cpu_regs[R_EAX]);
4931 4932 4933
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_sari_tl(cpu_cc_src, cpu_regs[R_EAX], 63);
                tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_regs[R_EDX]);
4934
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4935 4936
                break;
#endif
B
bellard 已提交
4937 4938 4939 4940
            }
            break;
        case 6: /* div */
            switch(ot) {
4941
            case MO_8:
4942
                gen_helper_divb_AL(cpu_env, cpu_T0);
B
bellard 已提交
4943
                break;
4944
            case MO_16:
4945
                gen_helper_divw_AX(cpu_env, cpu_T0);
B
bellard 已提交
4946 4947
                break;
            default:
4948
            case MO_32:
4949
                gen_helper_divl_EAX(cpu_env, cpu_T0);
B
bellard 已提交
4950 4951
                break;
#ifdef TARGET_X86_64
4952
            case MO_64:
4953
                gen_helper_divq_EAX(cpu_env, cpu_T0);
B
bellard 已提交
4954
                break;
B
bellard 已提交
4955
#endif
B
bellard 已提交
4956 4957 4958 4959
            }
            break;
        case 7: /* idiv */
            switch(ot) {
4960
            case MO_8:
4961
                gen_helper_idivb_AL(cpu_env, cpu_T0);
B
bellard 已提交
4962
                break;
4963
            case MO_16:
4964
                gen_helper_idivw_AX(cpu_env, cpu_T0);
B
bellard 已提交
4965 4966
                break;
            default:
4967
            case MO_32:
4968
                gen_helper_idivl_EAX(cpu_env, cpu_T0);
B
bellard 已提交
4969 4970
                break;
#ifdef TARGET_X86_64
4971
            case MO_64:
4972
                gen_helper_idivq_EAX(cpu_env, cpu_T0);
B
bellard 已提交
4973
                break;
B
bellard 已提交
4974
#endif
B
bellard 已提交
4975 4976 4977
            }
            break;
        default:
4978
            goto unknown_op;
B
bellard 已提交
4979 4980 4981 4982 4983
        }
        break;

    case 0xfe: /* GRP4 */
    case 0xff: /* GRP5 */
4984
        ot = mo_b_d(b, dflag);
B
bellard 已提交
4985

4986
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
4987
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4988
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4989 4990
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
4991
            goto unknown_op;
B
bellard 已提交
4992
        }
B
bellard 已提交
4993
        if (CODE64(s)) {
4994
            if (op == 2 || op == 4) {
B
bellard 已提交
4995
                /* operand size for jumps is 64 bit */
4996
                ot = MO_64;
4997
            } else if (op == 3 || op == 5) {
4998
                ot = dflag != MO_16 ? MO_32 + (rex_w == 1) : MO_16;
B
bellard 已提交
4999 5000
            } else if (op == 6) {
                /* default push size is 64 bit */
5001
                ot = mo_pushpop(s, dflag);
B
bellard 已提交
5002 5003
            }
        }
B
bellard 已提交
5004
        if (mod != 3) {
5005
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5006
            if (op >= 2 && op != 3 && op != 5)
5007
                gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
B
bellard 已提交
5008
        } else {
5009
            gen_op_mov_v_reg(ot, cpu_T0, rm);
B
bellard 已提交
5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027
        }

        switch(op) {
        case 0: /* inc Ev */
            if (mod != 3)
                opreg = OR_TMP0;
            else
                opreg = rm;
            gen_inc(s, ot, opreg, 1);
            break;
        case 1: /* dec Ev */
            if (mod != 3)
                opreg = OR_TMP0;
            else
                opreg = rm;
            gen_inc(s, ot, opreg, -1);
            break;
        case 2: /* call Ev */
5028
            /* XXX: optimize if memory (no 'and' is necessary) */
5029
            if (dflag == MO_16) {
5030
                tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
5031
            }
B
bellard 已提交
5032
            next_eip = s->pc - s->cs_base;
5033 5034 5035
            tcg_gen_movi_tl(cpu_T1, next_eip);
            gen_push_v(s, cpu_T1);
            gen_op_jmp_v(cpu_T0);
5036
            gen_bnd_jmp(s);
5037
            gen_jr(s, cpu_T0);
B
bellard 已提交
5038
            break;
B
bellard 已提交
5039
        case 3: /* lcall Ev */
5040
            gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
5041
            gen_add_A0_im(s, 1 << ot);
5042
            gen_op_ld_v(s, MO_16, cpu_T0, cpu_A0);
B
bellard 已提交
5043 5044
        do_lcall:
            if (s->pe && !s->vm86) {
5045 5046
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
                gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T1,
5047
                                           tcg_const_i32(dflag - 1),
5048
                                           tcg_const_tl(s->pc - s->cs_base));
B
bellard 已提交
5049
            } else {
5050 5051
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
                gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T1,
5052
                                      tcg_const_i32(dflag - 1),
P
pbrook 已提交
5053
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
5054
            }
5055 5056
            tcg_gen_ld_tl(cpu_tmp4, cpu_env, offsetof(CPUX86State, eip));
            gen_jr(s, cpu_tmp4);
B
bellard 已提交
5057 5058
            break;
        case 4: /* jmp Ev */
5059
            if (dflag == MO_16) {
5060
                tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
5061
            }
5062
            gen_op_jmp_v(cpu_T0);
5063
            gen_bnd_jmp(s);
5064
            gen_jr(s, cpu_T0);
B
bellard 已提交
5065 5066
            break;
        case 5: /* ljmp Ev */
5067
            gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
5068
            gen_add_A0_im(s, 1 << ot);
5069
            gen_op_ld_v(s, MO_16, cpu_T0, cpu_A0);
B
bellard 已提交
5070 5071
        do_ljmp:
            if (s->pe && !s->vm86) {
5072 5073
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
                gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T1,
5074
                                          tcg_const_tl(s->pc - s->cs_base));
B
bellard 已提交
5075
            } else {
5076
                gen_op_movl_seg_T0_vm(R_CS);
5077
                gen_op_jmp_v(cpu_T1);
B
bellard 已提交
5078
            }
5079 5080
            tcg_gen_ld_tl(cpu_tmp4, cpu_env, offsetof(CPUX86State, eip));
            gen_jr(s, cpu_tmp4);
B
bellard 已提交
5081 5082
            break;
        case 6: /* push Ev */
5083
            gen_push_v(s, cpu_T0);
B
bellard 已提交
5084 5085
            break;
        default:
5086
            goto unknown_op;
B
bellard 已提交
5087 5088 5089 5090
        }
        break;

    case 0x84: /* test Ev, Gv */
5091
    case 0x85:
5092
        ot = mo_b_d(b, dflag);
B
bellard 已提交
5093

5094
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5095
        reg = ((modrm >> 3) & 7) | rex_r;
5096

5097
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
5098
        gen_op_mov_v_reg(ot, cpu_T1, reg);
B
bellard 已提交
5099
        gen_op_testl_T0_T1_cc();
5100
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5101
        break;
5102

B
bellard 已提交
5103 5104
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
5105
        ot = mo_b_d(b, dflag);
5106
        val = insn_get(env, s, ot);
B
bellard 已提交
5107

5108 5109
        gen_op_mov_v_reg(ot, cpu_T0, OR_EAX);
        tcg_gen_movi_tl(cpu_T1, val);
B
bellard 已提交
5110
        gen_op_testl_T0_T1_cc();
5111
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5112
        break;
5113

B
bellard 已提交
5114
    case 0x98: /* CWDE/CBW */
5115
        switch (dflag) {
B
bellard 已提交
5116
#ifdef TARGET_X86_64
5117
        case MO_64:
5118 5119 5120
            gen_op_mov_v_reg(MO_32, cpu_T0, R_EAX);
            tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
            gen_op_mov_reg_v(MO_64, R_EAX, cpu_T0);
5121
            break;
B
bellard 已提交
5122
#endif
5123
        case MO_32:
5124 5125 5126
            gen_op_mov_v_reg(MO_16, cpu_T0, R_EAX);
            tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
            gen_op_mov_reg_v(MO_32, R_EAX, cpu_T0);
5127 5128
            break;
        case MO_16:
5129 5130 5131
            gen_op_mov_v_reg(MO_8, cpu_T0, R_EAX);
            tcg_gen_ext8s_tl(cpu_T0, cpu_T0);
            gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
5132 5133 5134
            break;
        default:
            tcg_abort();
B
bellard 已提交
5135
        }
B
bellard 已提交
5136 5137
        break;
    case 0x99: /* CDQ/CWD */
5138
        switch (dflag) {
B
bellard 已提交
5139
#ifdef TARGET_X86_64
5140
        case MO_64:
5141 5142 5143
            gen_op_mov_v_reg(MO_64, cpu_T0, R_EAX);
            tcg_gen_sari_tl(cpu_T0, cpu_T0, 63);
            gen_op_mov_reg_v(MO_64, R_EDX, cpu_T0);
5144
            break;
B
bellard 已提交
5145
#endif
5146
        case MO_32:
5147 5148 5149 5150
            gen_op_mov_v_reg(MO_32, cpu_T0, R_EAX);
            tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
            tcg_gen_sari_tl(cpu_T0, cpu_T0, 31);
            gen_op_mov_reg_v(MO_32, R_EDX, cpu_T0);
5151 5152
            break;
        case MO_16:
5153 5154 5155 5156
            gen_op_mov_v_reg(MO_16, cpu_T0, R_EAX);
            tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
            tcg_gen_sari_tl(cpu_T0, cpu_T0, 15);
            gen_op_mov_reg_v(MO_16, R_EDX, cpu_T0);
5157 5158 5159
            break;
        default:
            tcg_abort();
B
bellard 已提交
5160
        }
B
bellard 已提交
5161 5162 5163 5164
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
5165
        ot = dflag;
5166
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5167 5168 5169 5170 5171
        reg = ((modrm >> 3) & 7) | rex_r;
        if (b == 0x69)
            s->rip_offset = insn_const_size(ot);
        else if (b == 0x6b)
            s->rip_offset = 1;
5172
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5173
        if (b == 0x69) {
5174
            val = insn_get(env, s, ot);
5175
            tcg_gen_movi_tl(cpu_T1, val);
B
bellard 已提交
5176
        } else if (b == 0x6b) {
5177
            val = (int8_t)insn_get(env, s, MO_8);
5178
            tcg_gen_movi_tl(cpu_T1, val);
B
bellard 已提交
5179
        } else {
5180
            gen_op_mov_v_reg(ot, cpu_T1, reg);
B
bellard 已提交
5181
        }
5182
        switch (ot) {
B
bellard 已提交
5183
#ifdef TARGET_X86_64
5184
        case MO_64:
5185
            tcg_gen_muls2_i64(cpu_regs[reg], cpu_T1, cpu_T0, cpu_T1);
5186 5187
            tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
            tcg_gen_sari_tl(cpu_cc_src, cpu_cc_dst, 63);
5188
            tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_T1);
5189
            break;
B
bellard 已提交
5190
#endif
5191
        case MO_32:
5192 5193
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
            tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
5194 5195 5196 5197 5198 5199 5200 5201 5202
            tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
                              cpu_tmp2_i32, cpu_tmp3_i32);
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
            tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
            tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
            tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
            break;
        default:
5203 5204
            tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
            tcg_gen_ext16s_tl(cpu_T1, cpu_T1);
B
bellard 已提交
5205
            /* XXX: use 32 bit mul which could be faster */
5206 5207 5208 5209 5210
            tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
            tcg_gen_ext16s_tl(cpu_tmp0, cpu_T0);
            tcg_gen_sub_tl(cpu_cc_src, cpu_T0, cpu_tmp0);
            gen_op_mov_reg_v(ot, reg, cpu_T0);
5211
            break;
B
bellard 已提交
5212
        }
5213
        set_cc_op(s, CC_OP_MULB + ot);
B
bellard 已提交
5214 5215 5216
        break;
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */
5217
        ot = mo_b_d(b, dflag);
5218
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5219
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5220
        mod = (modrm >> 6) & 3;
5221
        gen_op_mov_v_reg(ot, cpu_T0, reg);
B
bellard 已提交
5222
        if (mod == 3) {
B
bellard 已提交
5223
            rm = (modrm & 7) | REX_B(s);
5224 5225 5226 5227
            gen_op_mov_v_reg(ot, cpu_T1, rm);
            tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
            gen_op_mov_reg_v(ot, reg, cpu_T1);
            gen_op_mov_reg_v(ot, rm, cpu_T0);
B
bellard 已提交
5228
        } else {
5229
            gen_lea_modrm(env, s, modrm);
5230 5231 5232 5233 5234 5235 5236 5237 5238
            if (s->prefix & PREFIX_LOCK) {
                tcg_gen_atomic_fetch_add_tl(cpu_T1, cpu_A0, cpu_T0,
                                            s->mem_index, ot | MO_LE);
                tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
            } else {
                gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
                tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
                gen_op_st_v(s, ot, cpu_T0, cpu_A0);
            }
5239
            gen_op_mov_reg_v(ot, reg, cpu_T1);
B
bellard 已提交
5240 5241
        }
        gen_op_update2_cc();
5242
        set_cc_op(s, CC_OP_ADDB + ot);
B
bellard 已提交
5243 5244 5245
        break;
    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */
B
bellard 已提交
5246
        {
5247
            TCGv oldv, newv, cmpv;
B
bellard 已提交
5248

5249
            ot = mo_b_d(b, dflag);
5250
            modrm = x86_ldub_code(env, s);
B
bellard 已提交
5251 5252
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
5253 5254 5255 5256 5257 5258 5259 5260 5261 5262
            oldv = tcg_temp_new();
            newv = tcg_temp_new();
            cmpv = tcg_temp_new();
            gen_op_mov_v_reg(ot, newv, reg);
            tcg_gen_mov_tl(cmpv, cpu_regs[R_EAX]);

            if (s->prefix & PREFIX_LOCK) {
                if (mod == 3) {
                    goto illegal_op;
                }
5263
                gen_lea_modrm(env, s, modrm);
5264 5265 5266
                tcg_gen_atomic_cmpxchg_tl(oldv, cpu_A0, cmpv, newv,
                                          s->mem_index, ot | MO_LE);
                gen_op_mov_reg_v(ot, R_EAX, oldv);
B
bellard 已提交
5267
            } else {
5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290
                if (mod == 3) {
                    rm = (modrm & 7) | REX_B(s);
                    gen_op_mov_v_reg(ot, oldv, rm);
                } else {
                    gen_lea_modrm(env, s, modrm);
                    gen_op_ld_v(s, ot, oldv, cpu_A0);
                    rm = 0; /* avoid warning */
                }
                gen_extu(ot, oldv);
                gen_extu(ot, cmpv);
                /* store value = (old == cmp ? new : old);  */
                tcg_gen_movcond_tl(TCG_COND_EQ, newv, oldv, cmpv, newv, oldv);
                if (mod == 3) {
                    gen_op_mov_reg_v(ot, R_EAX, oldv);
                    gen_op_mov_reg_v(ot, rm, newv);
                } else {
                    /* Perform an unconditional store cycle like physical cpu;
                       must be before changing accumulator to ensure
                       idempotency if the store faults and the instruction
                       is restarted */
                    gen_op_st_v(s, ot, newv, cpu_A0);
                    gen_op_mov_reg_v(ot, R_EAX, oldv);
                }
B
bellard 已提交
5291
            }
5292 5293 5294
            tcg_gen_mov_tl(cpu_cc_src, oldv);
            tcg_gen_mov_tl(cpu_cc_srcT, cmpv);
            tcg_gen_sub_tl(cpu_cc_dst, cmpv, oldv);
5295
            set_cc_op(s, CC_OP_SUBB + ot);
5296 5297 5298
            tcg_temp_free(oldv);
            tcg_temp_free(newv);
            tcg_temp_free(cmpv);
B
bellard 已提交
5299 5300 5301
        }
        break;
    case 0x1c7: /* cmpxchg8b */
5302
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5303
        mod = (modrm >> 6) & 3;
5304
        if ((mod == 3) || ((modrm & 0x38) != 0x8))
B
bellard 已提交
5305
            goto illegal_op;
B
bellard 已提交
5306
#ifdef TARGET_X86_64
5307
        if (dflag == MO_64) {
B
bellard 已提交
5308 5309
            if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
                goto illegal_op;
5310
            gen_lea_modrm(env, s, modrm);
5311
            if ((s->prefix & PREFIX_LOCK) && (tb_cflags(s->base.tb) & CF_PARALLEL)) {
5312 5313 5314 5315
                gen_helper_cmpxchg16b(cpu_env, cpu_A0);
            } else {
                gen_helper_cmpxchg16b_unlocked(cpu_env, cpu_A0);
            }
B
bellard 已提交
5316 5317 5318 5319 5320
        } else
#endif        
        {
            if (!(s->cpuid_features & CPUID_CX8))
                goto illegal_op;
5321
            gen_lea_modrm(env, s, modrm);
5322
            if ((s->prefix & PREFIX_LOCK) && (tb_cflags(s->base.tb) & CF_PARALLEL)) {
5323 5324 5325 5326
                gen_helper_cmpxchg8b(cpu_env, cpu_A0);
            } else {
                gen_helper_cmpxchg8b_unlocked(cpu_env, cpu_A0);
            }
B
bellard 已提交
5327
        }
5328
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5329
        break;
5330

B
bellard 已提交
5331 5332 5333
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
5334 5335
        gen_op_mov_v_reg(MO_32, cpu_T0, (b & 7) | REX_B(s));
        gen_push_v(s, cpu_T0);
B
bellard 已提交
5336 5337
        break;
    case 0x58 ... 0x5f: /* pop */
5338
        ot = gen_pop_T0(s);
B
bellard 已提交
5339
        /* NOTE: order is important for pop %sp */
5340
        gen_pop_update(s, ot);
5341
        gen_op_mov_reg_v(ot, (b & 7) | REX_B(s), cpu_T0);
B
bellard 已提交
5342 5343
        break;
    case 0x60: /* pusha */
B
bellard 已提交
5344 5345
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5346 5347 5348
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
5349 5350
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5351 5352 5353 5354
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
5355
        ot = mo_pushpop(s, dflag);
B
bellard 已提交
5356
        if (b == 0x68)
5357
            val = insn_get(env, s, ot);
B
bellard 已提交
5358
        else
5359
            val = (int8_t)insn_get(env, s, MO_8);
5360 5361
        tcg_gen_movi_tl(cpu_T0, val);
        gen_push_v(s, cpu_T0);
B
bellard 已提交
5362 5363
        break;
    case 0x8f: /* pop Ev */
5364
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5365
        mod = (modrm >> 6) & 3;
5366
        ot = gen_pop_T0(s);
B
bellard 已提交
5367 5368
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
5369
            gen_pop_update(s, ot);
B
bellard 已提交
5370
            rm = (modrm & 7) | REX_B(s);
5371
            gen_op_mov_reg_v(ot, rm, cpu_T0);
B
bellard 已提交
5372 5373
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
5374
            s->popl_esp_hack = 1 << ot;
5375
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5376
            s->popl_esp_hack = 0;
5377
            gen_pop_update(s, ot);
B
bellard 已提交
5378
        }
B
bellard 已提交
5379 5380 5381 5382
        break;
    case 0xc8: /* enter */
        {
            int level;
5383 5384
            val = x86_lduw_code(env, s);
            level = x86_ldub_code(env, s);
B
bellard 已提交
5385 5386 5387 5388
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
R
Richard Henderson 已提交
5389
        gen_leave(s);
B
bellard 已提交
5390 5391 5392 5393 5394
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
5395 5396
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5397
        gen_op_movl_T0_seg(b >> 3);
5398
        gen_push_v(s, cpu_T0);
B
bellard 已提交
5399 5400 5401 5402
        break;
    case 0x1a0: /* push fs */
    case 0x1a8: /* push gs */
        gen_op_movl_T0_seg((b >> 3) & 7);
5403
        gen_push_v(s, cpu_T0);
B
bellard 已提交
5404 5405 5406 5407
        break;
    case 0x07: /* pop es */
    case 0x17: /* pop ss */
    case 0x1f: /* pop ds */
B
bellard 已提交
5408 5409
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5410
        reg = b >> 3;
5411
        ot = gen_pop_T0(s);
5412
        gen_movl_seg_T0(s, reg);
5413
        gen_pop_update(s, ot);
5414
        /* Note that reg == R_SS in gen_movl_seg_T0 always sets is_jmp.  */
5415
        if (s->base.is_jmp) {
B
bellard 已提交
5416
            gen_jmp_im(s->pc - s->cs_base);
5417 5418 5419 5420 5421 5422
            if (reg == R_SS) {
                s->tf = 0;
                gen_eob_inhibit_irq(s, true);
            } else {
                gen_eob(s);
            }
B
bellard 已提交
5423 5424 5425 5426
        }
        break;
    case 0x1a1: /* pop fs */
    case 0x1a9: /* pop gs */
5427
        ot = gen_pop_T0(s);
5428
        gen_movl_seg_T0(s, (b >> 3) & 7);
5429
        gen_pop_update(s, ot);
5430
        if (s->base.is_jmp) {
B
bellard 已提交
5431
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5432 5433 5434 5435 5436 5437 5438 5439
            gen_eob(s);
        }
        break;

        /**************************/
        /* mov */
    case 0x88:
    case 0x89: /* mov Gv, Ev */
5440
        ot = mo_b_d(b, dflag);
5441
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5442
        reg = ((modrm >> 3) & 7) | rex_r;
5443

B
bellard 已提交
5444
        /* generate a generic store */
5445
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
5446 5447 5448
        break;
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
5449
        ot = mo_b_d(b, dflag);
5450
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5451
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5452 5453
        if (mod != 3) {
            s->rip_offset = insn_const_size(ot);
5454
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5455
        }
5456
        val = insn_get(env, s, ot);
5457
        tcg_gen_movi_tl(cpu_T0, val);
5458
        if (mod != 3) {
5459
            gen_op_st_v(s, ot, cpu_T0, cpu_A0);
5460
        } else {
5461
            gen_op_mov_reg_v(ot, (modrm & 7) | REX_B(s), cpu_T0);
5462
        }
B
bellard 已提交
5463 5464 5465
        break;
    case 0x8a:
    case 0x8b: /* mov Ev, Gv */
5466
        ot = mo_b_d(b, dflag);
5467
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5468
        reg = ((modrm >> 3) & 7) | rex_r;
5469

5470
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
5471
        gen_op_mov_reg_v(ot, reg, cpu_T0);
B
bellard 已提交
5472 5473
        break;
    case 0x8e: /* mov seg, Gv */
5474
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5475 5476 5477
        reg = (modrm >> 3) & 7;
        if (reg >= 6 || reg == R_CS)
            goto illegal_op;
5478
        gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
5479
        gen_movl_seg_T0(s, reg);
5480
        /* Note that reg == R_SS in gen_movl_seg_T0 always sets is_jmp.  */
5481
        if (s->base.is_jmp) {
B
bellard 已提交
5482
            gen_jmp_im(s->pc - s->cs_base);
5483 5484 5485 5486 5487 5488
            if (reg == R_SS) {
                s->tf = 0;
                gen_eob_inhibit_irq(s, true);
            } else {
                gen_eob(s);
            }
B
bellard 已提交
5489 5490 5491
        }
        break;
    case 0x8c: /* mov Gv, seg */
5492
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5493 5494 5495 5496 5497
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (reg >= 6)
            goto illegal_op;
        gen_op_movl_T0_seg(reg);
5498
        ot = mod == 3 ? dflag : MO_16;
5499
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5500 5501 5502 5503 5504 5505 5506
        break;

    case 0x1b6: /* movzbS Gv, Eb */
    case 0x1b7: /* movzwS Gv, Eb */
    case 0x1be: /* movsbS Gv, Eb */
    case 0x1bf: /* movswS Gv, Eb */
        {
5507 5508 5509
            TCGMemOp d_ot;
            TCGMemOp s_ot;

B
bellard 已提交
5510
            /* d_ot is the size of destination */
5511
            d_ot = dflag;
B
bellard 已提交
5512
            /* ot is the size of source */
5513
            ot = (b & 1) + MO_8;
5514 5515 5516
            /* s_ot is the sign+size of source */
            s_ot = b & 8 ? MO_SIGN | ot : ot;

5517
            modrm = x86_ldub_code(env, s);
B
bellard 已提交
5518
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5519
            mod = (modrm >> 6) & 3;
B
bellard 已提交
5520
            rm = (modrm & 7) | REX_B(s);
5521

B
bellard 已提交
5522
            if (mod == 3) {
5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541
                if (s_ot == MO_SB && byte_reg_is_xH(rm)) {
                    tcg_gen_sextract_tl(cpu_T0, cpu_regs[rm - 4], 8, 8);
                } else {
                    gen_op_mov_v_reg(ot, cpu_T0, rm);
                    switch (s_ot) {
                    case MO_UB:
                        tcg_gen_ext8u_tl(cpu_T0, cpu_T0);
                        break;
                    case MO_SB:
                        tcg_gen_ext8s_tl(cpu_T0, cpu_T0);
                        break;
                    case MO_UW:
                        tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
                        break;
                    default:
                    case MO_SW:
                        tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
                        break;
                    }
B
bellard 已提交
5542
                }
5543
                gen_op_mov_reg_v(d_ot, reg, cpu_T0);
B
bellard 已提交
5544
            } else {
5545
                gen_lea_modrm(env, s, modrm);
5546 5547
                gen_op_ld_v(s, s_ot, cpu_T0, cpu_A0);
                gen_op_mov_reg_v(d_ot, reg, cpu_T0);
B
bellard 已提交
5548 5549 5550 5551 5552
            }
        }
        break;

    case 0x8d: /* lea */
5553
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5554 5555 5556
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
5557
        reg = ((modrm >> 3) & 7) | rex_r;
5558 5559 5560
        {
            AddressParts a = gen_lea_modrm_0(env, s, modrm);
            TCGv ea = gen_lea_modrm_1(a);
5561 5562
            gen_lea_v_seg(s, s->aflag, ea, -1, -1);
            gen_op_mov_reg_v(dflag, reg, cpu_A0);
5563
        }
B
bellard 已提交
5564
        break;
5565

B
bellard 已提交
5566 5567 5568 5569 5570
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
5571 5572
            target_ulong offset_addr;

5573
            ot = mo_b_d(b, dflag);
5574
            switch (s->aflag) {
B
bellard 已提交
5575
#ifdef TARGET_X86_64
5576
            case MO_64:
5577
                offset_addr = x86_ldq_code(env, s);
5578
                break;
B
bellard 已提交
5579
#endif
5580 5581 5582
            default:
                offset_addr = insn_get(env, s, s->aflag);
                break;
B
bellard 已提交
5583
            }
5584
            tcg_gen_movi_tl(cpu_A0, offset_addr);
B
bellard 已提交
5585
            gen_add_A0_ds_seg(s);
B
bellard 已提交
5586
            if ((b & 2) == 0) {
5587 5588
                gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
                gen_op_mov_reg_v(ot, R_EAX, cpu_T0);
B
bellard 已提交
5589
            } else {
5590 5591
                gen_op_mov_v_reg(ot, cpu_T0, R_EAX);
                gen_op_st_v(s, ot, cpu_T0, cpu_A0);
B
bellard 已提交
5592 5593 5594 5595
            }
        }
        break;
    case 0xd7: /* xlat */
5596
        tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EBX]);
5597 5598
        tcg_gen_ext8u_tl(cpu_T0, cpu_regs[R_EAX]);
        tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T0);
5599
        gen_extu(s->aflag, cpu_A0);
B
bellard 已提交
5600
        gen_add_A0_ds_seg(s);
5601 5602
        gen_op_ld_v(s, MO_8, cpu_T0, cpu_A0);
        gen_op_mov_reg_v(MO_8, R_EAX, cpu_T0);
B
bellard 已提交
5603 5604
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
5605
        val = insn_get(env, s, MO_8);
5606 5607
        tcg_gen_movi_tl(cpu_T0, val);
        gen_op_mov_reg_v(MO_8, (b & 7) | REX_B(s), cpu_T0);
B
bellard 已提交
5608 5609
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
B
bellard 已提交
5610
#ifdef TARGET_X86_64
5611
        if (dflag == MO_64) {
B
bellard 已提交
5612 5613
            uint64_t tmp;
            /* 64 bit case */
5614
            tmp = x86_ldq_code(env, s);
B
bellard 已提交
5615
            reg = (b & 7) | REX_B(s);
5616 5617
            tcg_gen_movi_tl(cpu_T0, tmp);
            gen_op_mov_reg_v(MO_64, reg, cpu_T0);
5618
        } else
B
bellard 已提交
5619 5620
#endif
        {
5621
            ot = dflag;
5622
            val = insn_get(env, s, ot);
B
bellard 已提交
5623
            reg = (b & 7) | REX_B(s);
5624 5625
            tcg_gen_movi_tl(cpu_T0, val);
            gen_op_mov_reg_v(ot, reg, cpu_T0);
B
bellard 已提交
5626
        }
B
bellard 已提交
5627 5628 5629
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
R
Richard Henderson 已提交
5630
    do_xchg_reg_eax:
5631
        ot = dflag;
B
bellard 已提交
5632
        reg = (b & 7) | REX_B(s);
B
bellard 已提交
5633 5634 5635 5636
        rm = R_EAX;
        goto do_xchg_reg;
    case 0x86:
    case 0x87: /* xchg Ev, Gv */
5637
        ot = mo_b_d(b, dflag);
5638
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5639
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5640 5641
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5642
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5643
        do_xchg_reg:
5644 5645 5646 5647
            gen_op_mov_v_reg(ot, cpu_T0, reg);
            gen_op_mov_v_reg(ot, cpu_T1, rm);
            gen_op_mov_reg_v(ot, rm, cpu_T0);
            gen_op_mov_reg_v(ot, reg, cpu_T1);
B
bellard 已提交
5648
        } else {
5649
            gen_lea_modrm(env, s, modrm);
5650
            gen_op_mov_v_reg(ot, cpu_T0, reg);
B
bellard 已提交
5651
            /* for xchg, lock is implicit */
5652 5653
            tcg_gen_atomic_xchg_tl(cpu_T1, cpu_A0, cpu_T0,
                                   s->mem_index, ot | MO_LE);
5654
            gen_op_mov_reg_v(ot, reg, cpu_T1);
B
bellard 已提交
5655 5656 5657
        }
        break;
    case 0xc4: /* les Gv */
5658
        /* In CODE64 this is VEX3; see above.  */
B
bellard 已提交
5659 5660 5661
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
5662
        /* In CODE64 this is VEX2; see above.  */
B
bellard 已提交
5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673
        op = R_DS;
        goto do_lxx;
    case 0x1b2: /* lss Gv */
        op = R_SS;
        goto do_lxx;
    case 0x1b4: /* lfs Gv */
        op = R_FS;
        goto do_lxx;
    case 0x1b5: /* lgs Gv */
        op = R_GS;
    do_lxx:
5674
        ot = dflag != MO_16 ? MO_32 : MO_16;
5675
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5676
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5677 5678 5679
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
5680
        gen_lea_modrm(env, s, modrm);
5681
        gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
5682
        gen_add_A0_im(s, 1 << ot);
B
bellard 已提交
5683
        /* load the segment first to handle exceptions properly */
5684
        gen_op_ld_v(s, MO_16, cpu_T0, cpu_A0);
5685
        gen_movl_seg_T0(s, op);
B
bellard 已提交
5686
        /* then put the data */
5687
        gen_op_mov_reg_v(ot, reg, cpu_T1);
5688
        if (s->base.is_jmp) {
B
bellard 已提交
5689
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5690 5691 5692
            gen_eob(s);
        }
        break;
5693

B
bellard 已提交
5694 5695 5696 5697 5698 5699 5700 5701
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
5702
            ot = mo_b_d(b, dflag);
5703
            modrm = x86_ldub_code(env, s);
B
bellard 已提交
5704 5705
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
5706

B
bellard 已提交
5707
            if (mod != 3) {
B
bellard 已提交
5708 5709 5710
                if (shift == 2) {
                    s->rip_offset = 1;
                }
5711
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5712 5713
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
5714
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
5715 5716 5717 5718 5719 5720 5721
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
5722
                    shift = x86_ldub_code(env, s);
B
bellard 已提交
5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754
                }
                gen_shifti(s, op, ot, opreg, shift);
            }
        }
        break;
    case 0xd0:
    case 0xd1:
        /* shift Ev,1 */
        shift = 1;
        goto grp2;
    case 0xd2:
    case 0xd3:
        /* shift Ev,cl */
        shift = 0;
        goto grp2;

    case 0x1a4: /* shld imm */
        op = 0;
        shift = 1;
        goto do_shiftd;
    case 0x1a5: /* shld cl */
        op = 0;
        shift = 0;
        goto do_shiftd;
    case 0x1ac: /* shrd imm */
        op = 1;
        shift = 1;
        goto do_shiftd;
    case 0x1ad: /* shrd cl */
        op = 1;
        shift = 0;
    do_shiftd:
5755
        ot = dflag;
5756
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5757
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5758 5759
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5760
        if (mod != 3) {
5761
            gen_lea_modrm(env, s, modrm);
5762
            opreg = OR_TMP0;
B
bellard 已提交
5763
        } else {
5764
            opreg = rm;
B
bellard 已提交
5765
        }
5766
        gen_op_mov_v_reg(ot, cpu_T1, reg);
5767

B
bellard 已提交
5768
        if (shift) {
5769
            TCGv imm = tcg_const_tl(x86_ldub_code(env, s));
P
Paolo Bonzini 已提交
5770 5771
            gen_shiftd_rm_T1(s, ot, opreg, op, imm);
            tcg_temp_free(imm);
B
bellard 已提交
5772
        } else {
P
Paolo Bonzini 已提交
5773
            gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
B
bellard 已提交
5774 5775 5776 5777 5778
        }
        break;

        /************************/
        /* floats */
5779
    case 0xd8 ... 0xdf:
B
bellard 已提交
5780 5781 5782 5783 5784 5785
        if (s->flags & (HF_EM_MASK | HF_TS_MASK)) {
            /* if CR0.EM or CR0.TS are set, generate an FPU exception */
            /* XXX: what to do if illegal op ? */
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
            break;
        }
5786
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
5787 5788 5789 5790 5791
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
5792
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803
            switch(op) {
            case 0x00 ... 0x07: /* fxxxs */
            case 0x10 ... 0x17: /* fixxxl */
            case 0x20 ... 0x27: /* fxxxl */
            case 0x30 ... 0x37: /* fixxx */
                {
                    int op1;
                    op1 = op & 7;

                    switch(op >> 4) {
                    case 0:
5804 5805
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5806
                        gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5807 5808
                        break;
                    case 1:
5809 5810
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5811
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5812 5813
                        break;
                    case 2:
5814 5815
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5816
                        gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5817 5818 5819
                        break;
                    case 3:
                    default:
5820 5821
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5822
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5823 5824
                        break;
                    }
5825

P
pbrook 已提交
5826
                    gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
5827 5828
                    if (op1 == 3) {
                        /* fcomp needs pop */
B
Blue Swirl 已提交
5829
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
5830 5831 5832 5833 5834 5835
                    }
                }
                break;
            case 0x08: /* flds */
            case 0x0a: /* fsts */
            case 0x0b: /* fstps */
B
bellard 已提交
5836 5837 5838
            case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
            case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
            case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
B
bellard 已提交
5839 5840 5841 5842
                switch(op & 7) {
                case 0:
                    switch(op >> 4) {
                    case 0:
5843 5844
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5845
                        gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5846 5847
                        break;
                    case 1:
5848 5849
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5850
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5851 5852
                        break;
                    case 2:
5853 5854
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5855
                        gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5856 5857 5858
                        break;
                    case 3:
                    default:
5859 5860
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5861
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5862 5863 5864
                        break;
                    }
                    break;
B
bellard 已提交
5865
                case 1:
B
bellard 已提交
5866
                    /* XXX: the corresponding CPUID bit must be tested ! */
B
bellard 已提交
5867 5868
                    switch(op >> 4) {
                    case 1:
B
Blue Swirl 已提交
5869
                        gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
5870 5871
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5872 5873
                        break;
                    case 2:
B
Blue Swirl 已提交
5874
                        gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
5875 5876
                        tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
bellard 已提交
5877 5878 5879
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
5880
                        gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
5881 5882
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUW);
B
bellard 已提交
5883
                        break;
B
bellard 已提交
5884
                    }
B
Blue Swirl 已提交
5885
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
5886
                    break;
B
bellard 已提交
5887 5888 5889
                default:
                    switch(op >> 4) {
                    case 0:
B
Blue Swirl 已提交
5890
                        gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
5891 5892
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5893 5894
                        break;
                    case 1:
B
Blue Swirl 已提交
5895
                        gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
5896 5897
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5898 5899
                        break;
                    case 2:
B
Blue Swirl 已提交
5900
                        gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
5901 5902
                        tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
bellard 已提交
5903 5904 5905
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
5906
                        gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
5907 5908
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUW);
B
bellard 已提交
5909 5910 5911
                        break;
                    }
                    if ((op & 7) == 3)
B
Blue Swirl 已提交
5912
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
5913 5914 5915 5916
                    break;
                }
                break;
            case 0x0c: /* fldenv mem */
5917
                gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5918 5919
                break;
            case 0x0d: /* fldcw mem */
5920 5921
                tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
Blue Swirl 已提交
5922
                gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5923 5924
                break;
            case 0x0e: /* fnstenv mem */
5925
                gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5926 5927
                break;
            case 0x0f: /* fnstcw mem */
B
Blue Swirl 已提交
5928
                gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
5929 5930
                tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
bellard 已提交
5931 5932
                break;
            case 0x1d: /* fldt mem */
B
Blue Swirl 已提交
5933
                gen_helper_fldt_ST0(cpu_env, cpu_A0);
B
bellard 已提交
5934 5935
                break;
            case 0x1f: /* fstpt mem */
B
Blue Swirl 已提交
5936 5937
                gen_helper_fstt_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5938 5939
                break;
            case 0x2c: /* frstor mem */
5940
                gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5941 5942
                break;
            case 0x2e: /* fnsave mem */
5943
                gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5944 5945
                break;
            case 0x2f: /* fnstsw mem */
B
Blue Swirl 已提交
5946
                gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
5947 5948
                tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
bellard 已提交
5949 5950
                break;
            case 0x3c: /* fbld */
B
Blue Swirl 已提交
5951
                gen_helper_fbld_ST0(cpu_env, cpu_A0);
B
bellard 已提交
5952 5953
                break;
            case 0x3e: /* fbstp */
B
Blue Swirl 已提交
5954 5955
                gen_helper_fbst_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5956 5957
                break;
            case 0x3d: /* fildll */
5958
                tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5959
                gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5960 5961
                break;
            case 0x3f: /* fistpll */
B
Blue Swirl 已提交
5962
                gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
5963
                tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5964
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5965 5966
                break;
            default:
5967
                goto unknown_op;
B
bellard 已提交
5968 5969 5970 5971 5972 5973 5974
            }
        } else {
            /* register float ops */
            opreg = rm;

            switch(op) {
            case 0x08: /* fld sti */
B
Blue Swirl 已提交
5975 5976 5977
                gen_helper_fpush(cpu_env);
                gen_helper_fmov_ST0_STN(cpu_env,
                                        tcg_const_i32((opreg + 1) & 7));
B
bellard 已提交
5978 5979
                break;
            case 0x09: /* fxchg sti */
B
bellard 已提交
5980 5981
            case 0x29: /* fxchg4 sti, undocumented op */
            case 0x39: /* fxchg7 sti, undocumented op */
B
Blue Swirl 已提交
5982
                gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
5983 5984 5985 5986
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
5987
                    /* check exceptions (FreeBSD FPU probe) */
B
Blue Swirl 已提交
5988
                    gen_helper_fwait(cpu_env);
B
bellard 已提交
5989 5990
                    break;
                default:
5991
                    goto unknown_op;
B
bellard 已提交
5992 5993 5994 5995 5996
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
B
Blue Swirl 已提交
5997
                    gen_helper_fchs_ST0(cpu_env);
B
bellard 已提交
5998 5999
                    break;
                case 1: /* fabs */
B
Blue Swirl 已提交
6000
                    gen_helper_fabs_ST0(cpu_env);
B
bellard 已提交
6001 6002
                    break;
                case 4: /* ftst */
B
Blue Swirl 已提交
6003 6004
                    gen_helper_fldz_FT0(cpu_env);
                    gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6005 6006
                    break;
                case 5: /* fxam */
B
Blue Swirl 已提交
6007
                    gen_helper_fxam_ST0(cpu_env);
B
bellard 已提交
6008 6009
                    break;
                default:
6010
                    goto unknown_op;
B
bellard 已提交
6011 6012 6013 6014 6015 6016
                }
                break;
            case 0x0d: /* grp d9/5 */
                {
                    switch(rm) {
                    case 0:
B
Blue Swirl 已提交
6017 6018
                        gen_helper_fpush(cpu_env);
                        gen_helper_fld1_ST0(cpu_env);
B
bellard 已提交
6019 6020
                        break;
                    case 1:
B
Blue Swirl 已提交
6021 6022
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2t_ST0(cpu_env);
B
bellard 已提交
6023 6024
                        break;
                    case 2:
B
Blue Swirl 已提交
6025 6026
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2e_ST0(cpu_env);
B
bellard 已提交
6027 6028
                        break;
                    case 3:
B
Blue Swirl 已提交
6029 6030
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldpi_ST0(cpu_env);
B
bellard 已提交
6031 6032
                        break;
                    case 4:
B
Blue Swirl 已提交
6033 6034
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldlg2_ST0(cpu_env);
B
bellard 已提交
6035 6036
                        break;
                    case 5:
B
Blue Swirl 已提交
6037 6038
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldln2_ST0(cpu_env);
B
bellard 已提交
6039 6040
                        break;
                    case 6:
B
Blue Swirl 已提交
6041 6042
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldz_ST0(cpu_env);
B
bellard 已提交
6043 6044
                        break;
                    default:
6045
                        goto unknown_op;
B
bellard 已提交
6046 6047 6048 6049 6050 6051
                    }
                }
                break;
            case 0x0e: /* grp d9/6 */
                switch(rm) {
                case 0: /* f2xm1 */
B
Blue Swirl 已提交
6052
                    gen_helper_f2xm1(cpu_env);
B
bellard 已提交
6053 6054
                    break;
                case 1: /* fyl2x */
B
Blue Swirl 已提交
6055
                    gen_helper_fyl2x(cpu_env);
B
bellard 已提交
6056 6057
                    break;
                case 2: /* fptan */
B
Blue Swirl 已提交
6058
                    gen_helper_fptan(cpu_env);
B
bellard 已提交
6059 6060
                    break;
                case 3: /* fpatan */
B
Blue Swirl 已提交
6061
                    gen_helper_fpatan(cpu_env);
B
bellard 已提交
6062 6063
                    break;
                case 4: /* fxtract */
B
Blue Swirl 已提交
6064
                    gen_helper_fxtract(cpu_env);
B
bellard 已提交
6065 6066
                    break;
                case 5: /* fprem1 */
B
Blue Swirl 已提交
6067
                    gen_helper_fprem1(cpu_env);
B
bellard 已提交
6068 6069
                    break;
                case 6: /* fdecstp */
B
Blue Swirl 已提交
6070
                    gen_helper_fdecstp(cpu_env);
B
bellard 已提交
6071 6072 6073
                    break;
                default:
                case 7: /* fincstp */
B
Blue Swirl 已提交
6074
                    gen_helper_fincstp(cpu_env);
B
bellard 已提交
6075 6076 6077 6078 6079 6080
                    break;
                }
                break;
            case 0x0f: /* grp d9/7 */
                switch(rm) {
                case 0: /* fprem */
B
Blue Swirl 已提交
6081
                    gen_helper_fprem(cpu_env);
B
bellard 已提交
6082 6083
                    break;
                case 1: /* fyl2xp1 */
B
Blue Swirl 已提交
6084
                    gen_helper_fyl2xp1(cpu_env);
B
bellard 已提交
6085 6086
                    break;
                case 2: /* fsqrt */
B
Blue Swirl 已提交
6087
                    gen_helper_fsqrt(cpu_env);
B
bellard 已提交
6088 6089
                    break;
                case 3: /* fsincos */
B
Blue Swirl 已提交
6090
                    gen_helper_fsincos(cpu_env);
B
bellard 已提交
6091 6092
                    break;
                case 5: /* fscale */
B
Blue Swirl 已提交
6093
                    gen_helper_fscale(cpu_env);
B
bellard 已提交
6094 6095
                    break;
                case 4: /* frndint */
B
Blue Swirl 已提交
6096
                    gen_helper_frndint(cpu_env);
B
bellard 已提交
6097 6098
                    break;
                case 6: /* fsin */
B
Blue Swirl 已提交
6099
                    gen_helper_fsin(cpu_env);
B
bellard 已提交
6100 6101 6102
                    break;
                default:
                case 7: /* fcos */
B
Blue Swirl 已提交
6103
                    gen_helper_fcos(cpu_env);
B
bellard 已提交
6104 6105 6106 6107 6108 6109 6110 6111
                    break;
                }
                break;
            case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
            case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
            case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
                {
                    int op1;
6112

B
bellard 已提交
6113 6114
                    op1 = op & 7;
                    if (op >= 0x20) {
P
pbrook 已提交
6115
                        gen_helper_fp_arith_STN_ST0(op1, opreg);
B
bellard 已提交
6116
                        if (op >= 0x30)
B
Blue Swirl 已提交
6117
                            gen_helper_fpop(cpu_env);
B
bellard 已提交
6118
                    } else {
B
Blue Swirl 已提交
6119
                        gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
P
pbrook 已提交
6120
                        gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
6121 6122 6123 6124
                    }
                }
                break;
            case 0x02: /* fcom */
B
bellard 已提交
6125
            case 0x22: /* fcom2, undocumented op */
B
Blue Swirl 已提交
6126 6127
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6128 6129
                break;
            case 0x03: /* fcomp */
B
bellard 已提交
6130 6131
            case 0x23: /* fcomp3, undocumented op */
            case 0x32: /* fcomp5, undocumented op */
B
Blue Swirl 已提交
6132 6133 6134
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6135 6136 6137 6138
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
B
Blue Swirl 已提交
6139 6140 6141 6142
                    gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
                    gen_helper_fucom_ST0_FT0(cpu_env);
                    gen_helper_fpop(cpu_env);
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
6143 6144
                    break;
                default:
6145
                    goto unknown_op;
B
bellard 已提交
6146 6147 6148 6149 6150 6151 6152 6153 6154
                }
                break;
            case 0x1c:
                switch(rm) {
                case 0: /* feni (287 only, just do nop here) */
                    break;
                case 1: /* fdisi (287 only, just do nop here) */
                    break;
                case 2: /* fclex */
B
Blue Swirl 已提交
6155
                    gen_helper_fclex(cpu_env);
B
bellard 已提交
6156 6157
                    break;
                case 3: /* fninit */
B
Blue Swirl 已提交
6158
                    gen_helper_fninit(cpu_env);
B
bellard 已提交
6159 6160 6161 6162
                    break;
                case 4: /* fsetpm (287 only, just do nop here) */
                    break;
                default:
6163
                    goto unknown_op;
B
bellard 已提交
6164 6165 6166
                }
                break;
            case 0x1d: /* fucomi */
6167 6168 6169
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6170
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6171 6172
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
6173
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6174 6175
                break;
            case 0x1e: /* fcomi */
6176 6177 6178
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6179
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6180 6181
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
6182
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6183
                break;
B
bellard 已提交
6184
            case 0x28: /* ffree sti */
B
Blue Swirl 已提交
6185
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6186
                break;
B
bellard 已提交
6187
            case 0x2a: /* fst sti */
B
Blue Swirl 已提交
6188
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6189 6190
                break;
            case 0x2b: /* fstp sti */
B
bellard 已提交
6191 6192 6193
            case 0x0b: /* fstp1 sti, undocumented op */
            case 0x3a: /* fstp8 sti, undocumented op */
            case 0x3b: /* fstp9 sti, undocumented op */
B
Blue Swirl 已提交
6194 6195
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6196 6197
                break;
            case 0x2c: /* fucom st(i) */
B
Blue Swirl 已提交
6198 6199
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
B
bellard 已提交
6200 6201
                break;
            case 0x2d: /* fucomp st(i) */
B
Blue Swirl 已提交
6202 6203 6204
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6205 6206 6207 6208
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
B
Blue Swirl 已提交
6209 6210 6211 6212
                    gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
                    gen_helper_fcom_ST0_FT0(cpu_env);
                    gen_helper_fpop(cpu_env);
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
6213 6214
                    break;
                default:
6215
                    goto unknown_op;
B
bellard 已提交
6216 6217
                }
                break;
B
bellard 已提交
6218
            case 0x38: /* ffreep sti, undocumented op */
B
Blue Swirl 已提交
6219 6220
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6221
                break;
B
bellard 已提交
6222 6223 6224
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
B
Blue Swirl 已提交
6225
                    gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6226 6227
                    tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
                    gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
B
bellard 已提交
6228 6229
                    break;
                default:
6230
                    goto unknown_op;
B
bellard 已提交
6231 6232 6233
                }
                break;
            case 0x3d: /* fucomip */
6234 6235 6236
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6237
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6238 6239 6240
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6241
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6242 6243
                break;
            case 0x3e: /* fcomip */
6244 6245 6246
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6247
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6248 6249 6250
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6251
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6252
                break;
6253 6254 6255
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                {
6256 6257
                    int op1;
                    TCGLabel *l1;
6258
                    static const uint8_t fcmov_cc[8] = {
6259 6260 6261 6262 6263
                        (JCC_B << 1),
                        (JCC_Z << 1),
                        (JCC_BE << 1),
                        (JCC_P << 1),
                    };
6264 6265 6266 6267

                    if (!(s->cpuid_features & CPUID_CMOV)) {
                        goto illegal_op;
                    }
6268
                    op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
B
bellard 已提交
6269
                    l1 = gen_new_label();
6270
                    gen_jcc1_noeob(s, op1, l1);
B
Blue Swirl 已提交
6271
                    gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6272
                    gen_set_label(l1);
6273 6274
                }
                break;
B
bellard 已提交
6275
            default:
6276
                goto unknown_op;
B
bellard 已提交
6277 6278 6279 6280 6281 6282 6283 6284
            }
        }
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
6285
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6286 6287 6288 6289 6290 6291
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_movs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
        } else {
            gen_movs(s, ot);
        }
        break;
6292

B
bellard 已提交
6293 6294
    case 0xaa: /* stosS */
    case 0xab:
6295
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6296 6297 6298 6299 6300 6301 6302 6303
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_stos(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
        } else {
            gen_stos(s, ot);
        }
        break;
    case 0xac: /* lodsS */
    case 0xad:
6304
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6305 6306 6307 6308 6309 6310 6311 6312
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_lods(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
        } else {
            gen_lods(s, ot);
        }
        break;
    case 0xae: /* scasS */
    case 0xaf:
6313
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324
        if (prefixes & PREFIX_REPNZ) {
            gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
        } else if (prefixes & PREFIX_REPZ) {
            gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
        } else {
            gen_scas(s, ot);
        }
        break;

    case 0xa6: /* cmpsS */
    case 0xa7:
6325
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6326 6327 6328 6329 6330 6331 6332 6333 6334 6335
        if (prefixes & PREFIX_REPNZ) {
            gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
        } else if (prefixes & PREFIX_REPZ) {
            gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
        } else {
            gen_cmps(s, ot);
        }
        break;
    case 0x6c: /* insS */
    case 0x6d:
6336
        ot = mo_b_d32(b, dflag);
6337
        tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
6338 6339
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6340 6341
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6342
        } else {
6343
            gen_ins(s, ot);
6344
            if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6345 6346
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6347 6348 6349 6350
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6351
        ot = mo_b_d32(b, dflag);
6352
        tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
6353 6354
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6355 6356
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6357
        } else {
6358
            gen_outs(s, ot);
6359
            if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6360 6361
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6362 6363 6364 6365 6366
        }
        break;

        /************************/
        /* port I/O */
T
ths 已提交
6367

B
bellard 已提交
6368 6369
    case 0xe4:
    case 0xe5:
6370
        ot = mo_b_d32(b, dflag);
6371
        val = x86_ldub_code(env, s);
6372
        tcg_gen_movi_tl(cpu_T0, val);
6373 6374
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
6375
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6376
            gen_io_start();
6377
	}
6378
        tcg_gen_movi_i32(cpu_tmp2_i32, val);
6379 6380
        gen_helper_in_func(ot, cpu_T1, cpu_tmp2_i32);
        gen_op_mov_reg_v(ot, R_EAX, cpu_T1);
6381
        gen_bpt_io(s, cpu_tmp2_i32, ot);
6382
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6383 6384 6385
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6386 6387 6388
        break;
    case 0xe6:
    case 0xe7:
6389
        ot = mo_b_d32(b, dflag);
6390
        val = x86_ldub_code(env, s);
6391
        tcg_gen_movi_tl(cpu_T0, val);
6392 6393
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
6394
        gen_op_mov_v_reg(ot, cpu_T1, R_EAX);
6395

6396
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6397
            gen_io_start();
6398
	}
6399
        tcg_gen_movi_i32(cpu_tmp2_i32, val);
6400
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
P
pbrook 已提交
6401
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
6402
        gen_bpt_io(s, cpu_tmp2_i32, ot);
6403
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6404 6405 6406
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6407 6408 6409
        break;
    case 0xec:
    case 0xed:
6410
        ot = mo_b_d32(b, dflag);
6411
        tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
6412 6413
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
6414
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6415
            gen_io_start();
6416
	}
6417 6418 6419
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
        gen_helper_in_func(ot, cpu_T1, cpu_tmp2_i32);
        gen_op_mov_reg_v(ot, R_EAX, cpu_T1);
6420
        gen_bpt_io(s, cpu_tmp2_i32, ot);
6421
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6422 6423 6424
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6425 6426 6427
        break;
    case 0xee:
    case 0xef:
6428
        ot = mo_b_d32(b, dflag);
6429
        tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
6430 6431
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
6432
        gen_op_mov_v_reg(ot, cpu_T1, R_EAX);
6433

6434
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6435
            gen_io_start();
6436
	}
6437 6438
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
P
pbrook 已提交
6439
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
6440
        gen_bpt_io(s, cpu_tmp2_i32, ot);
6441
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
6442 6443 6444
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6445 6446 6447 6448 6449
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
6450
        val = x86_ldsw_code(env, s);
6451 6452 6453
        ot = gen_pop_T0(s);
        gen_stack_update(s, val + (1 << ot));
        /* Note that gen_pop_T0 uses a zero-extending load.  */
6454
        gen_op_jmp_v(cpu_T0);
6455
        gen_bnd_jmp(s);
6456
        gen_jr(s, cpu_T0);
B
bellard 已提交
6457 6458
        break;
    case 0xc3: /* ret */
6459 6460 6461
        ot = gen_pop_T0(s);
        gen_pop_update(s, ot);
        /* Note that gen_pop_T0 uses a zero-extending load.  */
6462
        gen_op_jmp_v(cpu_T0);
6463
        gen_bnd_jmp(s);
6464
        gen_jr(s, cpu_T0);
B
bellard 已提交
6465 6466
        break;
    case 0xca: /* lret im */
6467
        val = x86_ldsw_code(env, s);
B
bellard 已提交
6468 6469
    do_lret:
        if (s->pe && !s->vm86) {
6470
            gen_update_cc_op(s);
B
bellard 已提交
6471
            gen_jmp_im(pc_start - s->cs_base);
6472
            gen_helper_lret_protected(cpu_env, tcg_const_i32(dflag - 1),
P
pbrook 已提交
6473
                                      tcg_const_i32(val));
B
bellard 已提交
6474 6475 6476
        } else {
            gen_stack_A0(s);
            /* pop offset */
6477
            gen_op_ld_v(s, dflag, cpu_T0, cpu_A0);
B
bellard 已提交
6478 6479
            /* NOTE: keeping EIP updated is not a problem in case of
               exception */
6480
            gen_op_jmp_v(cpu_T0);
B
bellard 已提交
6481
            /* pop selector */
6482
            gen_add_A0_im(s, 1 << dflag);
6483
            gen_op_ld_v(s, dflag, cpu_T0, cpu_A0);
6484
            gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
6485
            /* add stack offset */
6486
            gen_stack_update(s, val + (2 << dflag));
B
bellard 已提交
6487 6488 6489 6490 6491 6492 6493
        }
        gen_eob(s);
        break;
    case 0xcb: /* lret */
        val = 0;
        goto do_lret;
    case 0xcf: /* iret */
B
bellard 已提交
6494
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
B
bellard 已提交
6495 6496
        if (!s->pe) {
            /* real mode */
6497
            gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
6498
            set_cc_op(s, CC_OP_EFLAGS);
6499 6500 6501 6502
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
6503
                gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
6504
                set_cc_op(s, CC_OP_EFLAGS);
6505
            }
B
bellard 已提交
6506
        } else {
6507
            gen_helper_iret_protected(cpu_env, tcg_const_i32(dflag - 1),
P
pbrook 已提交
6508
                                      tcg_const_i32(s->pc - s->cs_base));
6509
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6510
        }
6511
        gen_eob(s);
B
bellard 已提交
6512 6513 6514
        break;
    case 0xe8: /* call im */
        {
6515
            if (dflag != MO_16) {
6516
                tval = (int32_t)insn_get(env, s, MO_32);
6517
            } else {
6518
                tval = (int16_t)insn_get(env, s, MO_16);
6519
            }
B
bellard 已提交
6520
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
6521
            tval += next_eip;
6522
            if (dflag == MO_16) {
B
bellard 已提交
6523
                tval &= 0xffff;
6524
            } else if (!CODE64(s)) {
6525
                tval &= 0xffffffff;
6526
            }
6527 6528
            tcg_gen_movi_tl(cpu_T0, next_eip);
            gen_push_v(s, cpu_T0);
6529
            gen_bnd_jmp(s);
B
bellard 已提交
6530
            gen_jmp(s, tval);
B
bellard 已提交
6531 6532 6533 6534 6535
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
6536

B
bellard 已提交
6537 6538
            if (CODE64(s))
                goto illegal_op;
6539
            ot = dflag;
6540
            offset = insn_get(env, s, ot);
6541
            selector = insn_get(env, s, MO_16);
6542

6543 6544
            tcg_gen_movi_tl(cpu_T0, selector);
            tcg_gen_movi_tl(cpu_T1, offset);
B
bellard 已提交
6545 6546
        }
        goto do_lcall;
B
bellard 已提交
6547
    case 0xe9: /* jmp im */
6548
        if (dflag != MO_16) {
6549
            tval = (int32_t)insn_get(env, s, MO_32);
6550
        } else {
6551
            tval = (int16_t)insn_get(env, s, MO_16);
6552
        }
B
bellard 已提交
6553
        tval += s->pc - s->cs_base;
6554
        if (dflag == MO_16) {
B
bellard 已提交
6555
            tval &= 0xffff;
6556
        } else if (!CODE64(s)) {
6557
            tval &= 0xffffffff;
6558
        }
6559
        gen_bnd_jmp(s);
B
bellard 已提交
6560
        gen_jmp(s, tval);
B
bellard 已提交
6561 6562 6563 6564 6565
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6566 6567
            if (CODE64(s))
                goto illegal_op;
6568
            ot = dflag;
6569
            offset = insn_get(env, s, ot);
6570
            selector = insn_get(env, s, MO_16);
6571

6572 6573
            tcg_gen_movi_tl(cpu_T0, selector);
            tcg_gen_movi_tl(cpu_T1, offset);
B
bellard 已提交
6574 6575 6576
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
6577
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6578
        tval += s->pc - s->cs_base;
6579
        if (dflag == MO_16) {
B
bellard 已提交
6580
            tval &= 0xffff;
6581
        }
B
bellard 已提交
6582
        gen_jmp(s, tval);
B
bellard 已提交
6583 6584
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
6585
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6586 6587
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
6588
        if (dflag != MO_16) {
6589
            tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6590
        } else {
6591
            tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6592 6593 6594
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6595
        tval += next_eip;
6596
        if (dflag == MO_16) {
B
bellard 已提交
6597
            tval &= 0xffff;
6598
        }
6599
        gen_bnd_jmp(s);
B
bellard 已提交
6600
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6601 6602 6603
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
6604
        modrm = x86_ldub_code(env, s);
6605
        gen_setcc1(s, b, cpu_T0);
6606
        gen_ldst_modrm(env, s, modrm, MO_8, OR_TMP0, 1);
B
bellard 已提交
6607 6608
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6609 6610 6611
        if (!(s->cpuid_features & CPUID_CMOV)) {
            goto illegal_op;
        }
6612
        ot = dflag;
6613
        modrm = x86_ldub_code(env, s);
6614 6615
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_cmovcc1(env, s, ot, b, modrm, reg);
B
bellard 已提交
6616
        break;
6617

B
bellard 已提交
6618 6619 6620
        /************************/
        /* flags */
    case 0x9c: /* pushf */
B
bellard 已提交
6621
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
B
bellard 已提交
6622 6623 6624
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6625
            gen_update_cc_op(s);
6626 6627
            gen_helper_read_eflags(cpu_T0, cpu_env);
            gen_push_v(s, cpu_T0);
B
bellard 已提交
6628 6629 6630
        }
        break;
    case 0x9d: /* popf */
B
bellard 已提交
6631
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
B
bellard 已提交
6632 6633 6634
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6635
            ot = gen_pop_T0(s);
B
bellard 已提交
6636
            if (s->cpl == 0) {
6637
                if (dflag != MO_16) {
6638
                    gen_helper_write_eflags(cpu_env, cpu_T0,
6639 6640 6641 6642
                                            tcg_const_i32((TF_MASK | AC_MASK |
                                                           ID_MASK | NT_MASK |
                                                           IF_MASK |
                                                           IOPL_MASK)));
B
bellard 已提交
6643
                } else {
6644
                    gen_helper_write_eflags(cpu_env, cpu_T0,
6645 6646 6647 6648
                                            tcg_const_i32((TF_MASK | AC_MASK |
                                                           ID_MASK | NT_MASK |
                                                           IF_MASK | IOPL_MASK)
                                                          & 0xffff));
B
bellard 已提交
6649 6650
                }
            } else {
B
bellard 已提交
6651
                if (s->cpl <= s->iopl) {
6652
                    if (dflag != MO_16) {
6653
                        gen_helper_write_eflags(cpu_env, cpu_T0,
6654 6655 6656 6657 6658
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)));
B
bellard 已提交
6659
                    } else {
6660
                        gen_helper_write_eflags(cpu_env, cpu_T0,
6661 6662 6663 6664 6665 6666
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)
                                                              & 0xffff));
B
bellard 已提交
6667
                    }
B
bellard 已提交
6668
                } else {
6669
                    if (dflag != MO_16) {
6670
                        gen_helper_write_eflags(cpu_env, cpu_T0,
6671 6672
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)));
B
bellard 已提交
6673
                    } else {
6674
                        gen_helper_write_eflags(cpu_env, cpu_T0,
6675 6676 6677
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)
                                                         & 0xffff));
B
bellard 已提交
6678
                    }
B
bellard 已提交
6679 6680
                }
            }
6681
            gen_pop_update(s, ot);
6682
            set_cc_op(s, CC_OP_EFLAGS);
H
H. Peter Anvin 已提交
6683
            /* abort translation because TF/AC flag may change */
B
bellard 已提交
6684
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6685 6686 6687 6688
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
6689
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6690
            goto illegal_op;
6691
        gen_op_mov_v_reg(MO_8, cpu_T0, R_AH);
6692
        gen_compute_eflags(s);
6693
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, CC_O);
6694 6695
        tcg_gen_andi_tl(cpu_T0, cpu_T0, CC_S | CC_Z | CC_A | CC_P | CC_C);
        tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_T0);
B
bellard 已提交
6696 6697
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
6698
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6699
            goto illegal_op;
6700
        gen_compute_eflags(s);
6701
        /* Note: gen_compute_eflags() only gives the condition codes */
6702 6703
        tcg_gen_ori_tl(cpu_T0, cpu_cc_src, 0x02);
        gen_op_mov_reg_v(MO_8, R_AH, cpu_T0);
B
bellard 已提交
6704 6705
        break;
    case 0xf5: /* cmc */
6706
        gen_compute_eflags(s);
6707
        tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6708 6709
        break;
    case 0xf8: /* clc */
6710
        gen_compute_eflags(s);
6711
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
B
bellard 已提交
6712 6713
        break;
    case 0xf9: /* stc */
6714
        gen_compute_eflags(s);
6715
        tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6716 6717
        break;
    case 0xfc: /* cld */
6718
        tcg_gen_movi_i32(cpu_tmp2_i32, 1);
6719
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6720 6721
        break;
    case 0xfd: /* std */
6722
        tcg_gen_movi_i32(cpu_tmp2_i32, -1);
6723
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6724 6725 6726 6727 6728
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
6729
        ot = dflag;
6730
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
6731
        op = (modrm >> 3) & 7;
B
bellard 已提交
6732
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6733
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6734
        if (mod != 3) {
B
bellard 已提交
6735
            s->rip_offset = 1;
6736
            gen_lea_modrm(env, s, modrm);
6737 6738 6739
            if (!(s->prefix & PREFIX_LOCK)) {
                gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
            }
B
bellard 已提交
6740
        } else {
6741
            gen_op_mov_v_reg(ot, cpu_T0, rm);
B
bellard 已提交
6742 6743
        }
        /* load shift */
6744
        val = x86_ldub_code(env, s);
6745
        tcg_gen_movi_tl(cpu_T1, val);
B
bellard 已提交
6746
        if (op < 4)
6747
            goto unknown_op;
B
bellard 已提交
6748
        op -= 4;
B
bellard 已提交
6749
        goto bt_op;
B
bellard 已提交
6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761
    case 0x1a3: /* bt Gv, Ev */
        op = 0;
        goto do_btx;
    case 0x1ab: /* bts */
        op = 1;
        goto do_btx;
    case 0x1b3: /* btr */
        op = 2;
        goto do_btx;
    case 0x1bb: /* btc */
        op = 3;
    do_btx:
6762
        ot = dflag;
6763
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
6764
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6765
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6766
        rm = (modrm & 7) | REX_B(s);
6767
        gen_op_mov_v_reg(MO_32, cpu_T1, reg);
B
bellard 已提交
6768
        if (mod != 3) {
6769
            AddressParts a = gen_lea_modrm_0(env, s, modrm);
B
bellard 已提交
6770
            /* specific case: we need to add a displacement */
6771 6772
            gen_exts(ot, cpu_T1);
            tcg_gen_sari_tl(cpu_tmp0, cpu_T1, 3 + ot);
B
bellard 已提交
6773
            tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, ot);
6774 6775 6776 6777 6778
            tcg_gen_add_tl(cpu_A0, gen_lea_modrm_1(a), cpu_tmp0);
            gen_lea_v_seg(s, s->aflag, cpu_A0, a.def_seg, s->override);
            if (!(s->prefix & PREFIX_LOCK)) {
                gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
            }
B
bellard 已提交
6779
        } else {
6780
            gen_op_mov_v_reg(ot, cpu_T0, rm);
B
bellard 已提交
6781
        }
B
bellard 已提交
6782
    bt_op:
6783
        tcg_gen_andi_tl(cpu_T1, cpu_T1, (1 << (3 + ot)) - 1);
6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831
        tcg_gen_movi_tl(cpu_tmp0, 1);
        tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T1);
        if (s->prefix & PREFIX_LOCK) {
            switch (op) {
            case 0: /* bt */
                /* Needs no atomic ops; we surpressed the normal
                   memory load for LOCK above so do it now.  */
                gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
                break;
            case 1: /* bts */
                tcg_gen_atomic_fetch_or_tl(cpu_T0, cpu_A0, cpu_tmp0,
                                           s->mem_index, ot | MO_LE);
                break;
            case 2: /* btr */
                tcg_gen_not_tl(cpu_tmp0, cpu_tmp0);
                tcg_gen_atomic_fetch_and_tl(cpu_T0, cpu_A0, cpu_tmp0,
                                            s->mem_index, ot | MO_LE);
                break;
            default:
            case 3: /* btc */
                tcg_gen_atomic_fetch_xor_tl(cpu_T0, cpu_A0, cpu_tmp0,
                                            s->mem_index, ot | MO_LE);
                break;
            }
            tcg_gen_shr_tl(cpu_tmp4, cpu_T0, cpu_T1);
        } else {
            tcg_gen_shr_tl(cpu_tmp4, cpu_T0, cpu_T1);
            switch (op) {
            case 0: /* bt */
                /* Data already loaded; nothing to do.  */
                break;
            case 1: /* bts */
                tcg_gen_or_tl(cpu_T0, cpu_T0, cpu_tmp0);
                break;
            case 2: /* btr */
                tcg_gen_andc_tl(cpu_T0, cpu_T0, cpu_tmp0);
                break;
            default:
            case 3: /* btc */
                tcg_gen_xor_tl(cpu_T0, cpu_T0, cpu_tmp0);
                break;
            }
            if (op != 0) {
                if (mod != 3) {
                    gen_op_st_v(s, ot, cpu_T0, cpu_A0);
                } else {
                    gen_op_mov_reg_v(ot, rm, cpu_T0);
                }
6832
            }
6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853
        }

        /* Delay all CC updates until after the store above.  Note that
           C is the result of the test, Z is unchanged, and the others
           are all undefined.  */
        switch (s->cc_op) {
        case CC_OP_MULB ... CC_OP_MULQ:
        case CC_OP_ADDB ... CC_OP_ADDQ:
        case CC_OP_ADCB ... CC_OP_ADCQ:
        case CC_OP_SUBB ... CC_OP_SUBQ:
        case CC_OP_SBBB ... CC_OP_SBBQ:
        case CC_OP_LOGICB ... CC_OP_LOGICQ:
        case CC_OP_INCB ... CC_OP_INCQ:
        case CC_OP_DECB ... CC_OP_DECQ:
        case CC_OP_SHLB ... CC_OP_SHLQ:
        case CC_OP_SARB ... CC_OP_SARQ:
        case CC_OP_BMILGB ... CC_OP_BMILGQ:
            /* Z was going to be computed from the non-zero status of CC_DST.
               We can get that same Z value (and the new C value) by leaving
               CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
               same width.  */
B
bellard 已提交
6854
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
6855 6856 6857 6858 6859 6860 6861 6862
            set_cc_op(s, ((s->cc_op - CC_OP_MULB) & 3) + CC_OP_SARB);
            break;
        default:
            /* Otherwise, generate EFLAGS and replace the C bit.  */
            gen_compute_eflags(s);
            tcg_gen_deposit_tl(cpu_cc_src, cpu_cc_src, cpu_tmp4,
                               ctz32(CC_C), 1);
            break;
B
bellard 已提交
6863 6864
        }
        break;
6865 6866
    case 0x1bc: /* bsf / tzcnt */
    case 0x1bd: /* bsr / lzcnt */
6867
        ot = dflag;
6868
        modrm = x86_ldub_code(env, s);
6869 6870
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
6871
        gen_extu(ot, cpu_T0);
6872 6873 6874 6875 6876 6877 6878

        /* Note that lzcnt and tzcnt are in different extensions.  */
        if ((prefixes & PREFIX_REPZ)
            && (b & 1
                ? s->cpuid_ext3_features & CPUID_EXT3_ABM
                : s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)) {
            int size = 8 << ot;
6879
            /* For lzcnt/tzcnt, C bit is defined related to the input. */
6880
            tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
6881 6882 6883
            if (b & 1) {
                /* For lzcnt, reduce the target_ulong result by the
                   number of zeros that we expect to find at the top.  */
6884
                tcg_gen_clzi_tl(cpu_T0, cpu_T0, TARGET_LONG_BITS);
6885
                tcg_gen_subi_tl(cpu_T0, cpu_T0, TARGET_LONG_BITS - size);
B
bellard 已提交
6886
            } else {
6887 6888
                /* For tzcnt, a zero input must return the operand size.  */
                tcg_gen_ctzi_tl(cpu_T0, cpu_T0, size);
B
bellard 已提交
6889
            }
6890
            /* For lzcnt/tzcnt, Z bit is defined related to the result.  */
6891 6892 6893 6894 6895
            gen_op_update1_cc();
            set_cc_op(s, CC_OP_BMILGB + ot);
        } else {
            /* For bsr/bsf, only the Z bit is defined and it is related
               to the input and not the result.  */
6896
            tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
6897
            set_cc_op(s, CC_OP_LOGICB + ot);
6898 6899 6900 6901 6902

            /* ??? The manual says that the output is undefined when the
               input is zero, but real hardware leaves it unchanged, and
               real programs appear to depend on that.  Accomplish this
               by passing the output as the value to return upon zero.  */
6903 6904 6905
            if (b & 1) {
                /* For bsr, return the bit index of the first 1 bit,
                   not the count of leading zeros.  */
6906 6907
                tcg_gen_xori_tl(cpu_T1, cpu_regs[reg], TARGET_LONG_BITS - 1);
                tcg_gen_clz_tl(cpu_T0, cpu_T0, cpu_T1);
6908
                tcg_gen_xori_tl(cpu_T0, cpu_T0, TARGET_LONG_BITS - 1);
6909
            } else {
6910
                tcg_gen_ctz_tl(cpu_T0, cpu_T0, cpu_regs[reg]);
6911
            }
B
bellard 已提交
6912
        }
6913
        gen_op_mov_reg_v(ot, reg, cpu_T0);
B
bellard 已提交
6914 6915 6916 6917
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
6918 6919
        if (CODE64(s))
            goto illegal_op;
6920
        gen_update_cc_op(s);
6921
        gen_helper_daa(cpu_env);
6922
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6923 6924
        break;
    case 0x2f: /* das */
B
bellard 已提交
6925 6926
        if (CODE64(s))
            goto illegal_op;
6927
        gen_update_cc_op(s);
6928
        gen_helper_das(cpu_env);
6929
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6930 6931
        break;
    case 0x37: /* aaa */
B
bellard 已提交
6932 6933
        if (CODE64(s))
            goto illegal_op;
6934
        gen_update_cc_op(s);
6935
        gen_helper_aaa(cpu_env);
6936
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6937 6938
        break;
    case 0x3f: /* aas */
B
bellard 已提交
6939 6940
        if (CODE64(s))
            goto illegal_op;
6941
        gen_update_cc_op(s);
6942
        gen_helper_aas(cpu_env);
6943
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6944 6945
        break;
    case 0xd4: /* aam */
B
bellard 已提交
6946 6947
        if (CODE64(s))
            goto illegal_op;
6948
        val = x86_ldub_code(env, s);
6949 6950 6951
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
6952
            gen_helper_aam(cpu_env, tcg_const_i32(val));
6953
            set_cc_op(s, CC_OP_LOGICB);
6954
        }
B
bellard 已提交
6955 6956
        break;
    case 0xd5: /* aad */
B
bellard 已提交
6957 6958
        if (CODE64(s))
            goto illegal_op;
6959
        val = x86_ldub_code(env, s);
6960
        gen_helper_aad(cpu_env, tcg_const_i32(val));
6961
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
6962 6963 6964 6965
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
6966
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
6967
        if (prefixes & PREFIX_LOCK) {
6968
            goto illegal_op;
R
Richard Henderson 已提交
6969 6970 6971 6972 6973
        }
        /* If REX_B is set, then this is xchg eax, r8d, not a nop.  */
        if (REX_B(s)) {
            goto do_xchg_reg_eax;
        }
T
ths 已提交
6974
        if (prefixes & PREFIX_REPZ) {
6975 6976 6977
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_pause(cpu_env, tcg_const_i32(s->pc - pc_start));
6978
            s->base.is_jmp = DISAS_NORETURN;
T
ths 已提交
6979
        }
B
bellard 已提交
6980 6981
        break;
    case 0x9b: /* fwait */
6982
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
6983 6984
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
6985
        } else {
B
Blue Swirl 已提交
6986
            gen_helper_fwait(cpu_env);
B
bellard 已提交
6987
        }
B
bellard 已提交
6988 6989 6990 6991 6992
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
6993
        val = x86_ldub_code(env, s);
6994
        if (s->vm86 && s->iopl != 3) {
6995
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6996 6997 6998
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
6999 7000
        break;
    case 0xce: /* into */
B
bellard 已提交
7001 7002
        if (CODE64(s))
            goto illegal_op;
7003
        gen_update_cc_op(s);
7004
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7005
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7006
        break;
A
aurel32 已提交
7007
#ifdef WANT_ICEBP
B
bellard 已提交
7008
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
7009
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
7010
#if 1
B
bellard 已提交
7011
        gen_debug(s, pc_start - s->cs_base);
7012 7013
#else
        /* start debug */
7014
        tb_flush(CPU(x86_env_get_cpu(env)));
7015
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
7016
#endif
B
bellard 已提交
7017
        break;
A
aurel32 已提交
7018
#endif
B
bellard 已提交
7019 7020 7021
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
7022
                gen_helper_cli(cpu_env);
B
bellard 已提交
7023 7024 7025 7026 7027
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
7028
                gen_helper_cli(cpu_env);
B
bellard 已提交
7029 7030 7031 7032 7033 7034
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        }
        break;
    case 0xfb: /* sti */
7035 7036 7037 7038 7039
        if (s->vm86 ? s->iopl == 3 : s->cpl <= s->iopl) {
            gen_helper_sti(cpu_env);
            /* interruptions are enabled only the first insn after sti */
            gen_jmp_im(s->pc - s->cs_base);
            gen_eob_inhibit_irq(s, true);
B
bellard 已提交
7040
        } else {
7041
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
B
bellard 已提交
7042 7043 7044
        }
        break;
    case 0x62: /* bound */
B
bellard 已提交
7045 7046
        if (CODE64(s))
            goto illegal_op;
7047
        ot = dflag;
7048
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
7049 7050 7051 7052
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
7053
        gen_op_mov_v_reg(ot, cpu_T0, reg);
7054
        gen_lea_modrm(env, s, modrm);
7055
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
7056
        if (ot == MO_16) {
B
Blue Swirl 已提交
7057 7058 7059 7060
            gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
        } else {
            gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
        }
B
bellard 已提交
7061 7062
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
7063 7064
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
7065
        if (dflag == MO_64) {
7066 7067 7068
            gen_op_mov_v_reg(MO_64, cpu_T0, reg);
            tcg_gen_bswap64_i64(cpu_T0, cpu_T0);
            gen_op_mov_reg_v(MO_64, reg, cpu_T0);
7069
        } else
7070
#endif
B
bellard 已提交
7071
        {
7072 7073 7074 7075
            gen_op_mov_v_reg(MO_32, cpu_T0, reg);
            tcg_gen_ext32u_tl(cpu_T0, cpu_T0);
            tcg_gen_bswap32_tl(cpu_T0, cpu_T0);
            gen_op_mov_reg_v(MO_32, reg, cpu_T0);
B
bellard 已提交
7076
        }
B
bellard 已提交
7077 7078
        break;
    case 0xd6: /* salc */
B
bellard 已提交
7079 7080
        if (CODE64(s))
            goto illegal_op;
7081 7082 7083
        gen_compute_eflags_c(s, cpu_T0);
        tcg_gen_neg_tl(cpu_T0, cpu_T0);
        gen_op_mov_reg_v(MO_8, R_EAX, cpu_T0);
B
bellard 已提交
7084 7085 7086 7087 7088
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
7089
        {
7090
            TCGLabel *l1, *l2, *l3;
B
bellard 已提交
7091

7092
            tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
7093 7094
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
7095
            if (dflag == MO_16) {
B
bellard 已提交
7096
                tval &= 0xffff;
7097
            }
7098

B
bellard 已提交
7099 7100
            l1 = gen_new_label();
            l2 = gen_new_label();
7101
            l3 = gen_new_label();
B
bellard 已提交
7102
            b &= 3;
7103 7104 7105
            switch(b) {
            case 0: /* loopnz */
            case 1: /* loopz */
7106 7107
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jz_ecx(s->aflag, l3);
7108
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7109 7110
                break;
            case 2: /* loop */
7111 7112
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jnz_ecx(s->aflag, l1);
7113 7114 7115
                break;
            default:
            case 3: /* jcxz */
7116
                gen_op_jz_ecx(s->aflag, l1);
7117
                break;
B
bellard 已提交
7118 7119
            }

7120
            gen_set_label(l3);
B
bellard 已提交
7121
            gen_jmp_im(next_eip);
7122
            tcg_gen_br(l2);
7123

B
bellard 已提交
7124 7125 7126 7127 7128
            gen_set_label(l1);
            gen_jmp_im(tval);
            gen_set_label(l2);
            gen_eob(s);
        }
B
bellard 已提交
7129 7130 7131 7132 7133 7134
        break;
    case 0x130: /* wrmsr */
    case 0x132: /* rdmsr */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7135
            gen_update_cc_op(s);
B
bellard 已提交
7136
            gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7137
            if (b & 2) {
B
Blue Swirl 已提交
7138
                gen_helper_rdmsr(cpu_env);
T
ths 已提交
7139
            } else {
B
Blue Swirl 已提交
7140
                gen_helper_wrmsr(cpu_env);
T
ths 已提交
7141
            }
B
bellard 已提交
7142 7143 7144
        }
        break;
    case 0x131: /* rdtsc */
7145
        gen_update_cc_op(s);
B
bellard 已提交
7146
        gen_jmp_im(pc_start - s->cs_base);
7147
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
7148
            gen_io_start();
7149
	}
B
Blue Swirl 已提交
7150
        gen_helper_rdtsc(cpu_env);
7151
        if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
P
pbrook 已提交
7152 7153 7154
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
7155
        break;
7156
    case 0x133: /* rdpmc */
7157
        gen_update_cc_op(s);
7158
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7159
        gen_helper_rdpmc(cpu_env);
7160
        break;
7161
    case 0x134: /* sysenter */
7162
        /* For Intel SYSENTER is valid on 64-bit */
7163
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7164
            goto illegal_op;
7165 7166 7167
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7168
            gen_helper_sysenter(cpu_env);
7169 7170 7171 7172
            gen_eob(s);
        }
        break;
    case 0x135: /* sysexit */
7173
        /* For Intel SYSEXIT is valid on 64-bit */
7174
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7175
            goto illegal_op;
7176 7177 7178
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7179
            gen_helper_sysexit(cpu_env, tcg_const_i32(dflag - 1));
7180 7181 7182
            gen_eob(s);
        }
        break;
B
bellard 已提交
7183 7184 7185
#ifdef TARGET_X86_64
    case 0x105: /* syscall */
        /* XXX: is it usable in real mode ? */
J
Jun Koi 已提交
7186
        gen_update_cc_op(s);
B
bellard 已提交
7187
        gen_jmp_im(pc_start - s->cs_base);
7188
        gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
7189 7190 7191 7192
        /* TF handling for the syscall insn is different. The TF bit is  checked
           after the syscall insn completes. This allows #DB to not be
           generated after one has entered CPL0 if TF is set in FMASK.  */
        gen_eob_worker(s, false, true);
B
bellard 已提交
7193 7194 7195 7196 7197
        break;
    case 0x107: /* sysret */
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7198
            gen_helper_sysret(cpu_env, tcg_const_i32(dflag - 1));
7199
            /* condition codes are modified only in long mode */
7200 7201 7202
            if (s->lma) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
7203 7204 7205 7206 7207
            /* TF handling for the sysret insn is different. The TF bit is
               checked after the sysret insn completes. This allows #DB to be
               generated "as if" the syscall insn in userspace has just
               completed.  */
            gen_eob_worker(s, false, true);
B
bellard 已提交
7208 7209 7210
        }
        break;
#endif
B
bellard 已提交
7211
    case 0x1a2: /* cpuid */
7212
        gen_update_cc_op(s);
B
bellard 已提交
7213
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7214
        gen_helper_cpuid(cpu_env);
B
bellard 已提交
7215 7216 7217 7218 7219
        break;
    case 0xf4: /* hlt */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7220
            gen_update_cc_op(s);
7221
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7222
            gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
7223
            s->base.is_jmp = DISAS_NORETURN;
B
bellard 已提交
7224 7225 7226
        }
        break;
    case 0x100:
7227
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
7228 7229 7230 7231
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* sldt */
7232 7233
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7234
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
7235 7236
            tcg_gen_ld32u_tl(cpu_T0, cpu_env,
                             offsetof(CPUX86State, ldt.selector));
7237
            ot = mod == 3 ? dflag : MO_16;
7238
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7239 7240
            break;
        case 2: /* lldt */
7241 7242
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7243 7244 7245
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7246
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
7247
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7248
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
7249
                gen_helper_lldt(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7250 7251 7252
            }
            break;
        case 1: /* str */
7253 7254
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7255
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
7256 7257
            tcg_gen_ld32u_tl(cpu_T0, cpu_env,
                             offsetof(CPUX86State, tr.selector));
7258
            ot = mod == 3 ? dflag : MO_16;
7259
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7260 7261
            break;
        case 3: /* ltr */
7262 7263
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7264 7265 7266
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7267
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
7268
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7269
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
7270
                gen_helper_ltr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7271 7272 7273 7274
            }
            break;
        case 4: /* verr */
        case 5: /* verw */
7275 7276
            if (!s->pe || s->vm86)
                goto illegal_op;
7277
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7278
            gen_update_cc_op(s);
7279
            if (op == 4) {
7280
                gen_helper_verr(cpu_env, cpu_T0);
7281
            } else {
7282
                gen_helper_verw(cpu_env, cpu_T0);
7283
            }
7284
            set_cc_op(s, CC_OP_EFLAGS);
7285
            break;
B
bellard 已提交
7286
        default:
7287
            goto unknown_op;
B
bellard 已提交
7288 7289
        }
        break;
7290

B
bellard 已提交
7291
    case 0x101:
7292
        modrm = x86_ldub_code(env, s);
7293
        switch (modrm) {
7294
        CASE_MODRM_MEM_OP(0): /* sgdt */
B
bellard 已提交
7295
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
7296
            gen_lea_modrm(env, s, modrm);
7297 7298 7299
            tcg_gen_ld32u_tl(cpu_T0,
                             cpu_env, offsetof(CPUX86State, gdt.limit));
            gen_op_st_v(s, MO_16, cpu_T0, cpu_A0);
7300
            gen_add_A0_im(s, 2);
7301
            tcg_gen_ld_tl(cpu_T0, cpu_env, offsetof(CPUX86State, gdt.base));
7302
            if (dflag == MO_16) {
7303
                tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
7304
            }
7305
            gen_op_st_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
B
bellard 已提交
7306
            break;
7307 7308 7309 7310

        case 0xc8: /* monitor */
            if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) || s->cpl != 0) {
                goto illegal_op;
B
bellard 已提交
7311
            }
7312 7313 7314 7315 7316 7317
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EAX]);
            gen_extu(s->aflag, cpu_A0);
            gen_add_A0_ds_seg(s);
            gen_helper_monitor(cpu_env, cpu_A0);
B
bellard 已提交
7318
            break;
7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349

        case 0xc9: /* mwait */
            if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) || s->cpl != 0) {
                goto illegal_op;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
            gen_eob(s);
            break;

        case 0xca: /* clac */
            if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP)
                || s->cpl != 0) {
                goto illegal_op;
            }
            gen_helper_clac(cpu_env);
            gen_jmp_im(s->pc - s->cs_base);
            gen_eob(s);
            break;

        case 0xcb: /* stac */
            if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP)
                || s->cpl != 0) {
                goto illegal_op;
            }
            gen_helper_stac(cpu_env);
            gen_jmp_im(s->pc - s->cs_base);
            gen_eob(s);
            break;

7350
        CASE_MODRM_MEM_OP(1): /* sidt */
7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
            gen_lea_modrm(env, s, modrm);
            tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State, idt.limit));
            gen_op_st_v(s, MO_16, cpu_T0, cpu_A0);
            gen_add_A0_im(s, 2);
            tcg_gen_ld_tl(cpu_T0, cpu_env, offsetof(CPUX86State, idt.base));
            if (dflag == MO_16) {
                tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
            }
            gen_op_st_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
            break;

7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388
        case 0xd0: /* xgetbv */
            if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
                || (s->prefix & (PREFIX_LOCK | PREFIX_DATA
                                 | PREFIX_REPZ | PREFIX_REPNZ))) {
                goto illegal_op;
            }
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
            gen_helper_xgetbv(cpu_tmp1_i64, cpu_env, cpu_tmp2_i32);
            tcg_gen_extr_i64_tl(cpu_regs[R_EAX], cpu_regs[R_EDX], cpu_tmp1_i64);
            break;

        case 0xd1: /* xsetbv */
            if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
                || (s->prefix & (PREFIX_LOCK | PREFIX_DATA
                                 | PREFIX_REPZ | PREFIX_REPNZ))) {
                goto illegal_op;
            }
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                break;
            }
            tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
                                  cpu_regs[R_EDX]);
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
            gen_helper_xsetbv(cpu_env, cpu_tmp2_i32, cpu_tmp1_i64);
            /* End TB because translation flags may change.  */
7389
            gen_jmp_im(s->pc - s->cs_base);
7390 7391 7392
            gen_eob(s);
            break;

7393 7394 7395 7396 7397
        case 0xd8: /* VMRUN */
            if (!(s->flags & HF_SVME_MASK) || !s->pe) {
                goto illegal_op;
            }
            if (s->cpl != 0) {
B
bellard 已提交
7398
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7399
                break;
B
bellard 已提交
7400
            }
7401 7402 7403 7404 7405
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag - 1),
                             tcg_const_i32(s->pc - pc_start));
            tcg_gen_exit_tb(0);
7406
            s->base.is_jmp = DISAS_NORETURN;
B
bellard 已提交
7407
            break;
7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495

        case 0xd9: /* VMMCALL */
            if (!(s->flags & HF_SVME_MASK)) {
                goto illegal_op;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_vmmcall(cpu_env);
            break;

        case 0xda: /* VMLOAD */
            if (!(s->flags & HF_SVME_MASK) || !s->pe) {
                goto illegal_op;
            }
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                break;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag - 1));
            break;

        case 0xdb: /* VMSAVE */
            if (!(s->flags & HF_SVME_MASK) || !s->pe) {
                goto illegal_op;
            }
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                break;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag - 1));
            break;

        case 0xdc: /* STGI */
            if ((!(s->flags & HF_SVME_MASK)
                   && !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT))
                || !s->pe) {
                goto illegal_op;
            }
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                break;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_stgi(cpu_env);
            break;

        case 0xdd: /* CLGI */
            if (!(s->flags & HF_SVME_MASK) || !s->pe) {
                goto illegal_op;
            }
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                break;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_clgi(cpu_env);
            break;

        case 0xde: /* SKINIT */
            if ((!(s->flags & HF_SVME_MASK)
                 && !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT))
                || !s->pe) {
                goto illegal_op;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_skinit(cpu_env);
            break;

        case 0xdf: /* INVLPGA */
            if (!(s->flags & HF_SVME_MASK) || !s->pe) {
                goto illegal_op;
            }
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                break;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_helper_invlpga(cpu_env, tcg_const_i32(s->aflag - 1));
            break;

7496
        CASE_MODRM_MEM_OP(2): /* lgdt */
7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                break;
            }
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_WRITE);
            gen_lea_modrm(env, s, modrm);
            gen_op_ld_v(s, MO_16, cpu_T1, cpu_A0);
            gen_add_A0_im(s, 2);
            gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
            if (dflag == MO_16) {
                tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
            }
            tcg_gen_st_tl(cpu_T0, cpu_env, offsetof(CPUX86State, gdt.base));
            tcg_gen_st32_tl(cpu_T1, cpu_env, offsetof(CPUX86State, gdt.limit));
            break;

7513
        CASE_MODRM_MEM_OP(3): /* lidt */
7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                break;
            }
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_WRITE);
            gen_lea_modrm(env, s, modrm);
            gen_op_ld_v(s, MO_16, cpu_T1, cpu_A0);
            gen_add_A0_im(s, 2);
            gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
            if (dflag == MO_16) {
                tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
            }
            tcg_gen_st_tl(cpu_T0, cpu_env, offsetof(CPUX86State, idt.base));
            tcg_gen_st32_tl(cpu_T1, cpu_env, offsetof(CPUX86State, idt.limit));
            break;

7530
        CASE_MODRM_OP(4): /* smsw */
B
bellard 已提交
7531
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
7532 7533 7534 7535 7536 7537 7538 7539
            tcg_gen_ld_tl(cpu_T0, cpu_env, offsetof(CPUX86State, cr[0]));
            if (CODE64(s)) {
                mod = (modrm >> 6) & 3;
                ot = (mod != 3 ? MO_16 : s->dflag);
            } else {
                ot = MO_16;
            }
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7540
            break;
7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557
        case 0xee: /* rdpkru */
            if (prefixes & PREFIX_LOCK) {
                goto illegal_op;
            }
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
            gen_helper_rdpkru(cpu_tmp1_i64, cpu_env, cpu_tmp2_i32);
            tcg_gen_extr_i64_tl(cpu_regs[R_EAX], cpu_regs[R_EDX], cpu_tmp1_i64);
            break;
        case 0xef: /* wrpkru */
            if (prefixes & PREFIX_LOCK) {
                goto illegal_op;
            }
            tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
                                  cpu_regs[R_EDX]);
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
            gen_helper_wrpkru(cpu_env, cpu_tmp2_i32, cpu_tmp1_i64);
            break;
7558
        CASE_MODRM_OP(6): /* lmsw */
B
bellard 已提交
7559 7560
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7561
                break;
B
bellard 已提交
7562
            }
7563 7564 7565 7566 7567
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
            gen_helper_lmsw(cpu_env, cpu_T0);
            gen_jmp_im(s->pc - s->cs_base);
            gen_eob(s);
B
bellard 已提交
7568
            break;
7569

7570
        CASE_MODRM_MEM_OP(7): /* invlpg */
7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                break;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
            gen_lea_modrm(env, s, modrm);
            gen_helper_invlpg(cpu_env, cpu_A0);
            gen_jmp_im(s->pc - s->cs_base);
            gen_eob(s);
            break;

        case 0xf8: /* swapgs */
#ifdef TARGET_X86_64
            if (CODE64(s)) {
A
Andre Przywara 已提交
7586 7587 7588
                if (s->cpl != 0) {
                    gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                } else {
7589 7590 7591 7592 7593
                    tcg_gen_mov_tl(cpu_T0, cpu_seg_base[R_GS]);
                    tcg_gen_ld_tl(cpu_seg_base[R_GS], cpu_env,
                                  offsetof(CPUX86State, kernelgsbase));
                    tcg_gen_st_tl(cpu_T0, cpu_env,
                                  offsetof(CPUX86State, kernelgsbase));
A
Andre Przywara 已提交
7594
                }
7595 7596
                break;
            }
7597
#endif
7598 7599 7600 7601 7602 7603 7604 7605
            goto illegal_op;

        case 0xf9: /* rdtscp */
            if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP)) {
                goto illegal_op;
            }
            gen_update_cc_op(s);
            gen_jmp_im(pc_start - s->cs_base);
7606
            if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7607 7608 7609
                gen_io_start();
            }
            gen_helper_rdtscp(cpu_env);
7610
            if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7611 7612
                gen_io_end();
                gen_jmp(s, s->pc - s->cs_base);
B
bellard 已提交
7613 7614
            }
            break;
7615

B
bellard 已提交
7616
        default:
7617
            goto unknown_op;
B
bellard 已提交
7618 7619
        }
        break;
7620

7621 7622 7623 7624 7625
    case 0x108: /* invd */
    case 0x109: /* wbinvd */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
B
bellard 已提交
7626
            gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
7627 7628 7629
            /* nothing to do */
        }
        break;
B
bellard 已提交
7630 7631 7632 7633 7634
    case 0x63: /* arpl or movslS (x86_64) */
#ifdef TARGET_X86_64
        if (CODE64(s)) {
            int d_ot;
            /* d_ot is the size of destination */
7635
            d_ot = dflag;
B
bellard 已提交
7636

7637
            modrm = x86_ldub_code(env, s);
B
bellard 已提交
7638 7639 7640
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7641

B
bellard 已提交
7642
            if (mod == 3) {
7643
                gen_op_mov_v_reg(MO_32, cpu_T0, rm);
B
bellard 已提交
7644
                /* sign extend */
7645
                if (d_ot == MO_64) {
7646
                    tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
7647
                }
7648
                gen_op_mov_reg_v(d_ot, reg, cpu_T0);
B
bellard 已提交
7649
            } else {
7650
                gen_lea_modrm(env, s, modrm);
7651 7652
                gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T0, cpu_A0);
                gen_op_mov_reg_v(d_ot, reg, cpu_T0);
B
bellard 已提交
7653
            }
7654
        } else
B
bellard 已提交
7655 7656
#endif
        {
7657
            TCGLabel *label1;
L
Laurent Desnogues 已提交
7658
            TCGv t0, t1, t2, a0;
7659

B
bellard 已提交
7660 7661
            if (!s->pe || s->vm86)
                goto illegal_op;
P
pbrook 已提交
7662 7663 7664
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
7665
            ot = MO_16;
7666
            modrm = x86_ldub_code(env, s);
B
bellard 已提交
7667 7668 7669 7670
            reg = (modrm >> 3) & 7;
            mod = (modrm >> 6) & 3;
            rm = modrm & 7;
            if (mod != 3) {
7671
                gen_lea_modrm(env, s, modrm);
7672
                gen_op_ld_v(s, ot, t0, cpu_A0);
L
Laurent Desnogues 已提交
7673 7674
                a0 = tcg_temp_local_new();
                tcg_gen_mov_tl(a0, cpu_A0);
B
bellard 已提交
7675
            } else {
7676
                gen_op_mov_v_reg(ot, t0, rm);
7677
                a0 = NULL;
B
bellard 已提交
7678
            }
7679 7680 7681 7682
            gen_op_mov_v_reg(ot, t1, reg);
            tcg_gen_andi_tl(cpu_tmp0, t0, 3);
            tcg_gen_andi_tl(t1, t1, 3);
            tcg_gen_movi_tl(t2, 0);
7683
            label1 = gen_new_label();
7684 7685 7686 7687
            tcg_gen_brcond_tl(TCG_COND_GE, cpu_tmp0, t1, label1);
            tcg_gen_andi_tl(t0, t0, ~3);
            tcg_gen_or_tl(t0, t0, t1);
            tcg_gen_movi_tl(t2, CC_Z);
7688
            gen_set_label(label1);
B
bellard 已提交
7689
            if (mod != 3) {
7690
                gen_op_st_v(s, ot, t0, a0);
L
Laurent Desnogues 已提交
7691 7692
                tcg_temp_free(a0);
           } else {
7693
                gen_op_mov_reg_v(ot, rm, t0);
B
bellard 已提交
7694
            }
7695
            gen_compute_eflags(s);
7696
            tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
7697 7698 7699 7700
            tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
7701 7702
        }
        break;
B
bellard 已提交
7703 7704
    case 0x102: /* lar */
    case 0x103: /* lsl */
7705
        {
7706
            TCGLabel *label1;
7707
            TCGv t0;
7708 7709
            if (!s->pe || s->vm86)
                goto illegal_op;
7710
            ot = dflag != MO_16 ? MO_32 : MO_16;
7711
            modrm = x86_ldub_code(env, s);
7712
            reg = ((modrm >> 3) & 7) | rex_r;
7713
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
P
pbrook 已提交
7714
            t0 = tcg_temp_local_new();
7715
            gen_update_cc_op(s);
7716
            if (b == 0x102) {
7717
                gen_helper_lar(t0, cpu_env, cpu_T0);
7718
            } else {
7719
                gen_helper_lsl(t0, cpu_env, cpu_T0);
7720
            }
7721 7722
            tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
            label1 = gen_new_label();
P
pbrook 已提交
7723
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
7724
            gen_op_mov_reg_v(ot, reg, t0);
7725
            gen_set_label(label1);
7726
            set_cc_op(s, CC_OP_EFLAGS);
7727
            tcg_temp_free(t0);
7728
        }
B
bellard 已提交
7729 7730
        break;
    case 0x118:
7731
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
7732 7733 7734 7735 7736 7737 7738 7739 7740
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* prefetchnta */
        case 1: /* prefetchnt0 */
        case 2: /* prefetchnt0 */
        case 3: /* prefetchnt0 */
            if (mod == 3)
                goto illegal_op;
7741
            gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7742 7743
            /* nothing more to do */
            break;
B
bellard 已提交
7744
        default: /* nop (multi byte) */
7745
            gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7746
            break;
B
bellard 已提交
7747 7748
        }
        break;
7749
    case 0x11a:
7750
        modrm = x86_ldub_code(env, s);
7751 7752 7753
        if (s->flags & HF_MPX_EN_MASK) {
            mod = (modrm >> 6) & 3;
            reg = ((modrm >> 3) & 7) | rex_r;
7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773
            if (prefixes & PREFIX_REPZ) {
                /* bndcl */
                if (reg >= 4
                    || (prefixes & PREFIX_LOCK)
                    || s->aflag == MO_16) {
                    goto illegal_op;
                }
                gen_bndck(env, s, modrm, TCG_COND_LTU, cpu_bndl[reg]);
            } else if (prefixes & PREFIX_REPNZ) {
                /* bndcu */
                if (reg >= 4
                    || (prefixes & PREFIX_LOCK)
                    || s->aflag == MO_16) {
                    goto illegal_op;
                }
                TCGv_i64 notu = tcg_temp_new_i64();
                tcg_gen_not_i64(notu, cpu_bndu[reg]);
                gen_bndck(env, s, modrm, TCG_COND_GTU, notu);
                tcg_temp_free_i64(notu);
            } else if (prefixes & PREFIX_DATA) {
7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804
                /* bndmov -- from reg/mem */
                if (reg >= 4 || s->aflag == MO_16) {
                    goto illegal_op;
                }
                if (mod == 3) {
                    int reg2 = (modrm & 7) | REX_B(s);
                    if (reg2 >= 4 || (prefixes & PREFIX_LOCK)) {
                        goto illegal_op;
                    }
                    if (s->flags & HF_MPX_IU_MASK) {
                        tcg_gen_mov_i64(cpu_bndl[reg], cpu_bndl[reg2]);
                        tcg_gen_mov_i64(cpu_bndu[reg], cpu_bndu[reg2]);
                    }
                } else {
                    gen_lea_modrm(env, s, modrm);
                    if (CODE64(s)) {
                        tcg_gen_qemu_ld_i64(cpu_bndl[reg], cpu_A0,
                                            s->mem_index, MO_LEQ);
                        tcg_gen_addi_tl(cpu_A0, cpu_A0, 8);
                        tcg_gen_qemu_ld_i64(cpu_bndu[reg], cpu_A0,
                                            s->mem_index, MO_LEQ);
                    } else {
                        tcg_gen_qemu_ld_i64(cpu_bndl[reg], cpu_A0,
                                            s->mem_index, MO_LEUL);
                        tcg_gen_addi_tl(cpu_A0, cpu_A0, 4);
                        tcg_gen_qemu_ld_i64(cpu_bndu[reg], cpu_A0,
                                            s->mem_index, MO_LEUL);
                    }
                    /* bnd registers are now in-use */
                    gen_set_hflag(s, HF_MPX_IU_MASK);
                }
7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834
            } else if (mod != 3) {
                /* bndldx */
                AddressParts a = gen_lea_modrm_0(env, s, modrm);
                if (reg >= 4
                    || (prefixes & PREFIX_LOCK)
                    || s->aflag == MO_16
                    || a.base < -1) {
                    goto illegal_op;
                }
                if (a.base >= 0) {
                    tcg_gen_addi_tl(cpu_A0, cpu_regs[a.base], a.disp);
                } else {
                    tcg_gen_movi_tl(cpu_A0, 0);
                }
                gen_lea_v_seg(s, s->aflag, cpu_A0, a.def_seg, s->override);
                if (a.index >= 0) {
                    tcg_gen_mov_tl(cpu_T0, cpu_regs[a.index]);
                } else {
                    tcg_gen_movi_tl(cpu_T0, 0);
                }
                if (CODE64(s)) {
                    gen_helper_bndldx64(cpu_bndl[reg], cpu_env, cpu_A0, cpu_T0);
                    tcg_gen_ld_i64(cpu_bndu[reg], cpu_env,
                                   offsetof(CPUX86State, mmx_t0.MMX_Q(0)));
                } else {
                    gen_helper_bndldx32(cpu_bndu[reg], cpu_env, cpu_A0, cpu_T0);
                    tcg_gen_ext32u_i64(cpu_bndl[reg], cpu_bndu[reg]);
                    tcg_gen_shri_i64(cpu_bndu[reg], cpu_bndu[reg], 32);
                }
                gen_set_hflag(s, HF_MPX_IU_MASK);
7835 7836 7837 7838
            }
        }
        gen_nop_modrm(env, s, modrm);
        break;
R
Richard Henderson 已提交
7839
    case 0x11b:
7840
        modrm = x86_ldub_code(env, s);
R
Richard Henderson 已提交
7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871
        if (s->flags & HF_MPX_EN_MASK) {
            mod = (modrm >> 6) & 3;
            reg = ((modrm >> 3) & 7) | rex_r;
            if (mod != 3 && (prefixes & PREFIX_REPZ)) {
                /* bndmk */
                if (reg >= 4
                    || (prefixes & PREFIX_LOCK)
                    || s->aflag == MO_16) {
                    goto illegal_op;
                }
                AddressParts a = gen_lea_modrm_0(env, s, modrm);
                if (a.base >= 0) {
                    tcg_gen_extu_tl_i64(cpu_bndl[reg], cpu_regs[a.base]);
                    if (!CODE64(s)) {
                        tcg_gen_ext32u_i64(cpu_bndl[reg], cpu_bndl[reg]);
                    }
                } else if (a.base == -1) {
                    /* no base register has lower bound of 0 */
                    tcg_gen_movi_i64(cpu_bndl[reg], 0);
                } else {
                    /* rip-relative generates #ud */
                    goto illegal_op;
                }
                tcg_gen_not_tl(cpu_A0, gen_lea_modrm_1(a));
                if (!CODE64(s)) {
                    tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
                }
                tcg_gen_extu_tl_i64(cpu_bndu[reg], cpu_A0);
                /* bnd registers are now in-use */
                gen_set_hflag(s, HF_MPX_IU_MASK);
                break;
7872 7873 7874 7875 7876 7877 7878 7879
            } else if (prefixes & PREFIX_REPNZ) {
                /* bndcn */
                if (reg >= 4
                    || (prefixes & PREFIX_LOCK)
                    || s->aflag == MO_16) {
                    goto illegal_op;
                }
                gen_bndck(env, s, modrm, TCG_COND_GTU, cpu_bndu[reg]);
7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909
            } else if (prefixes & PREFIX_DATA) {
                /* bndmov -- to reg/mem */
                if (reg >= 4 || s->aflag == MO_16) {
                    goto illegal_op;
                }
                if (mod == 3) {
                    int reg2 = (modrm & 7) | REX_B(s);
                    if (reg2 >= 4 || (prefixes & PREFIX_LOCK)) {
                        goto illegal_op;
                    }
                    if (s->flags & HF_MPX_IU_MASK) {
                        tcg_gen_mov_i64(cpu_bndl[reg2], cpu_bndl[reg]);
                        tcg_gen_mov_i64(cpu_bndu[reg2], cpu_bndu[reg]);
                    }
                } else {
                    gen_lea_modrm(env, s, modrm);
                    if (CODE64(s)) {
                        tcg_gen_qemu_st_i64(cpu_bndl[reg], cpu_A0,
                                            s->mem_index, MO_LEQ);
                        tcg_gen_addi_tl(cpu_A0, cpu_A0, 8);
                        tcg_gen_qemu_st_i64(cpu_bndu[reg], cpu_A0,
                                            s->mem_index, MO_LEQ);
                    } else {
                        tcg_gen_qemu_st_i64(cpu_bndl[reg], cpu_A0,
                                            s->mem_index, MO_LEUL);
                        tcg_gen_addi_tl(cpu_A0, cpu_A0, 4);
                        tcg_gen_qemu_st_i64(cpu_bndu[reg], cpu_A0,
                                            s->mem_index, MO_LEUL);
                    }
                }
7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936
            } else if (mod != 3) {
                /* bndstx */
                AddressParts a = gen_lea_modrm_0(env, s, modrm);
                if (reg >= 4
                    || (prefixes & PREFIX_LOCK)
                    || s->aflag == MO_16
                    || a.base < -1) {
                    goto illegal_op;
                }
                if (a.base >= 0) {
                    tcg_gen_addi_tl(cpu_A0, cpu_regs[a.base], a.disp);
                } else {
                    tcg_gen_movi_tl(cpu_A0, 0);
                }
                gen_lea_v_seg(s, s->aflag, cpu_A0, a.def_seg, s->override);
                if (a.index >= 0) {
                    tcg_gen_mov_tl(cpu_T0, cpu_regs[a.index]);
                } else {
                    tcg_gen_movi_tl(cpu_T0, 0);
                }
                if (CODE64(s)) {
                    gen_helper_bndstx64(cpu_env, cpu_A0, cpu_T0,
                                        cpu_bndl[reg], cpu_bndu[reg]);
                } else {
                    gen_helper_bndstx32(cpu_env, cpu_A0, cpu_T0,
                                        cpu_bndl[reg], cpu_bndu[reg]);
                }
R
Richard Henderson 已提交
7937 7938 7939 7940
            }
        }
        gen_nop_modrm(env, s, modrm);
        break;
7941
    case 0x119: case 0x11c ... 0x11f: /* nop (multi byte) */
7942
        modrm = x86_ldub_code(env, s);
7943
        gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7944
        break;
B
bellard 已提交
7945 7946 7947 7948 7949
    case 0x120: /* mov reg, crN */
    case 0x122: /* mov crN, reg */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7950
            modrm = x86_ldub_code(env, s);
7951 7952 7953 7954 7955
            /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
             * AMD documentation (24594.pdf) and testing of
             * intel 386 and 486 processors all show that the mod bits
             * are assumed to be 1's, regardless of actual values.
             */
B
bellard 已提交
7956 7957 7958
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
7959
                ot = MO_64;
B
bellard 已提交
7960
            else
7961
                ot = MO_32;
7962 7963 7964 7965
            if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
                (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
                reg = 8;
            }
B
bellard 已提交
7966 7967 7968 7969 7970
            switch(reg) {
            case 0:
            case 2:
            case 3:
            case 4:
B
bellard 已提交
7971
            case 8:
7972
                gen_update_cc_op(s);
B
bellard 已提交
7973
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
7974
                if (b & 2) {
7975
                    if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7976 7977
                        gen_io_start();
                    }
7978
                    gen_op_mov_v_reg(ot, cpu_T0, rm);
B
Blue Swirl 已提交
7979
                    gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
7980
                                         cpu_T0);
7981
                    if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7982 7983
                        gen_io_end();
                    }
B
bellard 已提交
7984
                    gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7985 7986
                    gen_eob(s);
                } else {
7987
                    if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7988 7989
                        gen_io_start();
                    }
7990 7991
                    gen_helper_read_crN(cpu_T0, cpu_env, tcg_const_i32(reg));
                    gen_op_mov_reg_v(ot, rm, cpu_T0);
7992
                    if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7993 7994
                        gen_io_end();
                    }
B
bellard 已提交
7995 7996 7997
                }
                break;
            default:
7998
                goto unknown_op;
B
bellard 已提交
7999 8000 8001 8002 8003 8004 8005 8006
            }
        }
        break;
    case 0x121: /* mov reg, drN */
    case 0x123: /* mov drN, reg */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
8007
            modrm = x86_ldub_code(env, s);
8008 8009 8010 8011 8012
            /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
             * AMD documentation (24594.pdf) and testing of
             * intel 386 and 486 processors all show that the mod bits
             * are assumed to be 1's, regardless of actual values.
             */
B
bellard 已提交
8013 8014 8015
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
8016
                ot = MO_64;
B
bellard 已提交
8017
            else
8018
                ot = MO_32;
8019
            if (reg >= 8) {
B
bellard 已提交
8020
                goto illegal_op;
8021
            }
B
bellard 已提交
8022
            if (b & 2) {
T
ths 已提交
8023
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
8024
                gen_op_mov_v_reg(ot, cpu_T0, rm);
8025
                tcg_gen_movi_i32(cpu_tmp2_i32, reg);
8026
                gen_helper_set_dr(cpu_env, cpu_tmp2_i32, cpu_T0);
B
bellard 已提交
8027
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
8028 8029
                gen_eob(s);
            } else {
T
ths 已提交
8030
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
8031
                tcg_gen_movi_i32(cpu_tmp2_i32, reg);
8032 8033
                gen_helper_get_dr(cpu_T0, cpu_env, cpu_tmp2_i32);
                gen_op_mov_reg_v(ot, rm, cpu_T0);
B
bellard 已提交
8034 8035 8036 8037 8038 8039 8040
            }
        }
        break;
    case 0x106: /* clts */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
T
ths 已提交
8041
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
8042
            gen_helper_clts(cpu_env);
B
bellard 已提交
8043
            /* abort block because static cpu state changed */
B
bellard 已提交
8044
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
8045
            gen_eob(s);
B
bellard 已提交
8046 8047
        }
        break;
B
balrog 已提交
8048
    /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
B
bellard 已提交
8049 8050
    case 0x1c3: /* MOVNTI reg, mem */
        if (!(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
8051
            goto illegal_op;
8052
        ot = mo_64_32(dflag);
8053
        modrm = x86_ldub_code(env, s);
B
bellard 已提交
8054 8055 8056 8057 8058
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        reg = ((modrm >> 3) & 7) | rex_r;
        /* generate a generic store */
8059
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
8060
        break;
B
bellard 已提交
8061
    case 0x1ae:
8062
        modrm = x86_ldub_code(env, s);
8063
        switch (modrm) {
8064
        CASE_MODRM_MEM_OP(0): /* fxsave */
8065 8066
            if (!(s->cpuid_features & CPUID_FXSR)
                || (prefixes & PREFIX_LOCK)) {
B
bellard 已提交
8067
                goto illegal_op;
8068
            }
8069
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
8070 8071 8072
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
8073
            gen_lea_modrm(env, s, modrm);
8074
            gen_helper_fxsave(cpu_env, cpu_A0);
B
bellard 已提交
8075
            break;
8076

8077
        CASE_MODRM_MEM_OP(1): /* fxrstor */
8078 8079
            if (!(s->cpuid_features & CPUID_FXSR)
                || (prefixes & PREFIX_LOCK)) {
B
bellard 已提交
8080
                goto illegal_op;
8081
            }
8082
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
8083 8084 8085
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
8086
            gen_lea_modrm(env, s, modrm);
8087
            gen_helper_fxrstor(cpu_env, cpu_A0);
B
bellard 已提交
8088
            break;
8089

8090
        CASE_MODRM_MEM_OP(2): /* ldmxcsr */
8091 8092 8093
            if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK)) {
                goto illegal_op;
            }
B
bellard 已提交
8094 8095 8096
            if (s->flags & HF_TS_MASK) {
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
B
bellard 已提交
8097
            }
8098
            gen_lea_modrm(env, s, modrm);
8099 8100
            tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0, s->mem_index, MO_LEUL);
            gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
8101
            break;
8102

8103
        CASE_MODRM_MEM_OP(3): /* stmxcsr */
8104
            if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK)) {
B
bellard 已提交
8105
                goto illegal_op;
8106 8107 8108 8109 8110 8111 8112 8113
            }
            if (s->flags & HF_TS_MASK) {
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
            gen_lea_modrm(env, s, modrm);
            tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State, mxcsr));
            gen_op_st_v(s, MO_32, cpu_T0, cpu_A0);
B
bellard 已提交
8114
            break;
8115

8116
        CASE_MODRM_MEM_OP(4): /* xsave */
8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127
            if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
                || (prefixes & (PREFIX_LOCK | PREFIX_DATA
                                | PREFIX_REPZ | PREFIX_REPNZ))) {
                goto illegal_op;
            }
            gen_lea_modrm(env, s, modrm);
            tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
                                  cpu_regs[R_EDX]);
            gen_helper_xsave(cpu_env, cpu_A0, cpu_tmp1_i64);
            break;

8128
        CASE_MODRM_MEM_OP(5): /* xrstor */
8129 8130 8131 8132 8133 8134 8135 8136 8137
            if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
                || (prefixes & (PREFIX_LOCK | PREFIX_DATA
                                | PREFIX_REPZ | PREFIX_REPNZ))) {
                goto illegal_op;
            }
            gen_lea_modrm(env, s, modrm);
            tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
                                  cpu_regs[R_EDX]);
            gen_helper_xrstor(cpu_env, cpu_A0, cpu_tmp1_i64);
8138 8139 8140 8141 8142
            /* XRSTOR is how MPX is enabled, which changes how
               we translate.  Thus we need to end the TB.  */
            gen_update_cc_op(s);
            gen_jmp_im(s->pc - s->cs_base);
            gen_eob(s);
8143 8144
            break;

8145
        CASE_MODRM_MEM_OP(6): /* xsaveopt / clwb */
8146 8147 8148 8149
            if (prefixes & PREFIX_LOCK) {
                goto illegal_op;
            }
            if (prefixes & PREFIX_DATA) {
8150
                /* clwb */
8151
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_CLWB)) {
8152
                    goto illegal_op;
8153
                }
8154
                gen_nop_modrm(env, s, modrm);
8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165
            } else {
                /* xsaveopt */
                if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
                    || (s->cpuid_xsave_features & CPUID_XSAVE_XSAVEOPT) == 0
                    || (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))) {
                    goto illegal_op;
                }
                gen_lea_modrm(env, s, modrm);
                tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
                                      cpu_regs[R_EDX]);
                gen_helper_xsaveopt(cpu_env, cpu_A0, cpu_tmp1_i64);
8166
            }
8167
            break;
8168

8169
        CASE_MODRM_MEM_OP(7): /* clflush / clflushopt */
8170 8171 8172 8173 8174 8175 8176 8177
            if (prefixes & PREFIX_LOCK) {
                goto illegal_op;
            }
            if (prefixes & PREFIX_DATA) {
                /* clflushopt */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_CLFLUSHOPT)) {
                    goto illegal_op;
                }
8178
            } else {
8179 8180 8181
                /* clflush */
                if ((s->prefix & (PREFIX_REPZ | PREFIX_REPNZ))
                    || !(s->cpuid_features & CPUID_CLFLUSH)) {
8182
                    goto illegal_op;
8183
                }
8184
            }
8185
            gen_nop_modrm(env, s, modrm);
8186
            break;
8187

8188
        case 0xc0 ... 0xc7: /* rdfsbase (f3 0f ae /0) */
8189
        case 0xc8 ... 0xcf: /* rdgsbase (f3 0f ae /1) */
8190
        case 0xd0 ... 0xd7: /* wrfsbase (f3 0f ae /2) */
8191
        case 0xd8 ... 0xdf: /* wrgsbase (f3 0f ae /3) */
8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219
            if (CODE64(s)
                && (prefixes & PREFIX_REPZ)
                && !(prefixes & PREFIX_LOCK)
                && (s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_FSGSBASE)) {
                TCGv base, treg, src, dst;

                /* Preserve hflags bits by testing CR4 at runtime.  */
                tcg_gen_movi_i32(cpu_tmp2_i32, CR4_FSGSBASE_MASK);
                gen_helper_cr4_testbit(cpu_env, cpu_tmp2_i32);

                base = cpu_seg_base[modrm & 8 ? R_GS : R_FS];
                treg = cpu_regs[(modrm & 7) | REX_B(s)];

                if (modrm & 0x10) {
                    /* wr*base */
                    dst = base, src = treg;
                } else {
                    /* rd*base */
                    dst = treg, src = base;
                }

                if (s->dflag == MO_32) {
                    tcg_gen_ext32u_tl(dst, src);
                } else {
                    tcg_gen_mov_tl(dst, src);
                }
                break;
            }
8220
            goto unknown_op;
8221

8222 8223 8224 8225 8226 8227
        case 0xf8: /* sfence / pcommit */
            if (prefixes & PREFIX_DATA) {
                /* pcommit */
                if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_PCOMMIT)
                    || (prefixes & PREFIX_LOCK)) {
                    goto illegal_op;
8228
                }
8229 8230 8231 8232
                break;
            }
            /* fallthru */
        case 0xf9 ... 0xff: /* sfence */
8233 8234 8235 8236
            if (!(s->cpuid_features & CPUID_SSE)
                || (prefixes & PREFIX_LOCK)) {
                goto illegal_op;
            }
8237
            tcg_gen_mb(TCG_MO_ST_ST | TCG_BAR_SC);
8238
            break;
8239
        case 0xe8 ... 0xef: /* lfence */
8240 8241 8242 8243 8244 8245
            if (!(s->cpuid_features & CPUID_SSE)
                || (prefixes & PREFIX_LOCK)) {
                goto illegal_op;
            }
            tcg_gen_mb(TCG_MO_LD_LD | TCG_BAR_SC);
            break;
8246 8247 8248 8249
        case 0xf0 ... 0xf7: /* mfence */
            if (!(s->cpuid_features & CPUID_SSE2)
                || (prefixes & PREFIX_LOCK)) {
                goto illegal_op;
8250
            }
8251
            tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
8252
            break;
8253

B
bellard 已提交
8254
        default:
8255
            goto unknown_op;
B
bellard 已提交
8256 8257
        }
        break;
8258

A
aurel32 已提交
8259
    case 0x10d: /* 3DNow! prefetch(w) */
8260
        modrm = x86_ldub_code(env, s);
A
aurel32 已提交
8261 8262 8263
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
8264
        gen_nop_modrm(env, s, modrm);
8265
        break;
B
bellard 已提交
8266
    case 0x1aa: /* rsm */
B
bellard 已提交
8267
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
B
bellard 已提交
8268 8269
        if (!(s->flags & HF_SMM_MASK))
            goto illegal_op;
J
Jun Koi 已提交
8270
        gen_update_cc_op(s);
B
bellard 已提交
8271
        gen_jmp_im(s->pc - s->cs_base);
B
Blue Swirl 已提交
8272
        gen_helper_rsm(cpu_env);
B
bellard 已提交
8273 8274
        gen_eob(s);
        break;
B
balrog 已提交
8275 8276 8277 8278 8279 8280 8281
    case 0x1b8: /* SSE4.2 popcnt */
        if ((prefixes & (PREFIX_REPZ | PREFIX_LOCK | PREFIX_REPNZ)) !=
             PREFIX_REPZ)
            goto illegal_op;
        if (!(s->cpuid_ext_features & CPUID_EXT_POPCNT))
            goto illegal_op;

8282
        modrm = x86_ldub_code(env, s);
M
malc 已提交
8283
        reg = ((modrm >> 3) & 7) | rex_r;
B
balrog 已提交
8284

8285
        if (s->prefix & PREFIX_DATA) {
8286
            ot = MO_16;
8287 8288 8289
        } else {
            ot = mo_64_32(dflag);
        }
B
balrog 已提交
8290

8291
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
8292 8293 8294
        gen_extu(ot, cpu_T0);
        tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
        tcg_gen_ctpop_tl(cpu_T0, cpu_T0);
8295
        gen_op_mov_reg_v(ot, reg, cpu_T0);
B
balrog 已提交
8296

8297
        set_cc_op(s, CC_OP_POPCNT);
B
balrog 已提交
8298
        break;
A
aurel32 已提交
8299 8300 8301
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
8302 8303
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
8304
    case 0x138 ... 0x13a:
8305
    case 0x150 ... 0x179:
B
bellard 已提交
8306 8307 8308 8309
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
8310
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
8311
        break;
B
bellard 已提交
8312
    default:
8313
        goto unknown_op;
B
bellard 已提交
8314 8315 8316
    }
    return s->pc;
 illegal_op:
8317 8318 8319 8320
    gen_illegal_opcode(s);
    return s->pc;
 unknown_op:
    gen_unknown_opcode(env, s);
B
bellard 已提交
8321 8322 8323
    return s->pc;
}

8324
void tcg_x86_init(void)
B
bellard 已提交
8325
{
8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354
    static const char reg_names[CPU_NB_REGS][4] = {
#ifdef TARGET_X86_64
        [R_EAX] = "rax",
        [R_EBX] = "rbx",
        [R_ECX] = "rcx",
        [R_EDX] = "rdx",
        [R_ESI] = "rsi",
        [R_EDI] = "rdi",
        [R_EBP] = "rbp",
        [R_ESP] = "rsp",
        [8]  = "r8",
        [9]  = "r9",
        [10] = "r10",
        [11] = "r11",
        [12] = "r12",
        [13] = "r13",
        [14] = "r14",
        [15] = "r15",
#else
        [R_EAX] = "eax",
        [R_EBX] = "ebx",
        [R_ECX] = "ecx",
        [R_EDX] = "edx",
        [R_ESI] = "esi",
        [R_EDI] = "edi",
        [R_EBP] = "ebp",
        [R_ESP] = "esp",
#endif
    };
8355 8356 8357 8358 8359 8360 8361 8362
    static const char seg_base_names[6][8] = {
        [R_CS] = "cs_base",
        [R_DS] = "ds_base",
        [R_ES] = "es_base",
        [R_FS] = "fs_base",
        [R_GS] = "gs_base",
        [R_SS] = "ss_base",
    };
R
Richard Henderson 已提交
8363 8364 8365 8366 8367 8368
    static const char bnd_regl_names[4][8] = {
        "bnd0_lb", "bnd1_lb", "bnd2_lb", "bnd3_lb"
    };
    static const char bnd_regu_names[4][8] = {
        "bnd0_ub", "bnd1_ub", "bnd2_ub", "bnd3_ub"
    };
8369 8370
    int i;

8371
    cpu_cc_op = tcg_global_mem_new_i32(cpu_env,
8372
                                       offsetof(CPUX86State, cc_op), "cc_op");
8373
    cpu_cc_dst = tcg_global_mem_new(cpu_env, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
8374
                                    "cc_dst");
8375
    cpu_cc_src = tcg_global_mem_new(cpu_env, offsetof(CPUX86State, cc_src),
8376
                                    "cc_src");
8377
    cpu_cc_src2 = tcg_global_mem_new(cpu_env, offsetof(CPUX86State, cc_src2),
8378
                                     "cc_src2");
8379

8380
    for (i = 0; i < CPU_NB_REGS; ++i) {
8381
        cpu_regs[i] = tcg_global_mem_new(cpu_env,
8382 8383 8384
                                         offsetof(CPUX86State, regs[i]),
                                         reg_names[i]);
    }
K
KONRAD Frederic 已提交
8385

8386 8387 8388 8389 8390 8391 8392
    for (i = 0; i < 6; ++i) {
        cpu_seg_base[i]
            = tcg_global_mem_new(cpu_env,
                                 offsetof(CPUX86State, segs[i].base),
                                 seg_base_names[i]);
    }

R
Richard Henderson 已提交
8393 8394 8395 8396 8397 8398 8399 8400 8401 8402
    for (i = 0; i < 4; ++i) {
        cpu_bndl[i]
            = tcg_global_mem_new_i64(cpu_env,
                                     offsetof(CPUX86State, bnd_regs[i].lb),
                                     bnd_regl_names[i]);
        cpu_bndu[i]
            = tcg_global_mem_new_i64(cpu_env,
                                     offsetof(CPUX86State, bnd_regs[i].ub),
                                     bnd_regu_names[i]);
    }
B
bellard 已提交
8403 8404
}

8405 8406
static int i386_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cpu,
                                      int max_insns)
B
bellard 已提交
8407
{
8408 8409 8410 8411
    DisasContext *dc = container_of(dcbase, DisasContext, base);
    CPUX86State *env = cpu->env_ptr;
    uint32_t flags = dc->base.tb->flags;
    target_ulong cs_base = dc->base.tb->cs_base;
B
bellard 已提交
8412

8413
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
8414 8415 8416 8417 8418 8419 8420 8421 8422
    dc->code32 = (flags >> HF_CS32_SHIFT) & 1;
    dc->ss32 = (flags >> HF_SS32_SHIFT) & 1;
    dc->addseg = (flags >> HF_ADDSEG_SHIFT) & 1;
    dc->f_st = 0;
    dc->vm86 = (flags >> VM_SHIFT) & 1;
    dc->cpl = (flags >> HF_CPL_SHIFT) & 3;
    dc->iopl = (flags >> IOPL_SHIFT) & 3;
    dc->tf = (flags >> TF_SHIFT) & 1;
    dc->cc_op = CC_OP_DYNAMIC;
8423
    dc->cc_op_dirty = false;
B
bellard 已提交
8424 8425 8426 8427
    dc->cs_base = cs_base;
    dc->popl_esp_hack = 0;
    /* select memory access functions */
    dc->mem_index = 0;
8428 8429 8430
#ifdef CONFIG_SOFTMMU
    dc->mem_index = cpu_mmu_index(env, false);
#endif
8431 8432 8433 8434 8435
    dc->cpuid_features = env->features[FEAT_1_EDX];
    dc->cpuid_ext_features = env->features[FEAT_1_ECX];
    dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
    dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
    dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];
8436
    dc->cpuid_xsave_features = env->features[FEAT_XSAVE];
B
bellard 已提交
8437 8438 8439 8440
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
8441
    dc->flags = flags;
8442
    dc->jmp_opt = !(dc->tf || dc->base.singlestep_enabled ||
8443
                    (flags & HF_INHIBIT_IRQ_MASK));
8444 8445 8446 8447 8448 8449 8450 8451 8452 8453
    /* Do not optimize repz jumps at all in icount mode, because
       rep movsS instructions are execured with different paths
       in !repz_opt and repz_opt modes. The first one was used
       always except single step mode. And this setting
       disables jumps optimization and control paths become
       equivalent in run and single step modes.
       Now there will be no jump optimization for repz in
       record/replay modes and there will always be an
       additional step for ecx=0 when icount is enabled.
     */
8454
    dc->repz_opt = !dc->jmp_opt && !(tb_cflags(dc->base.tb) & CF_USE_ICOUNT);
8455 8456
#if 0
    /* check addseg logic */
B
bellard 已提交
8457
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
8458 8459 8460
        printf("ERROR addseg\n");
#endif

8461 8462
    cpu_T0 = tcg_temp_new();
    cpu_T1 = tcg_temp_new();
P
pbrook 已提交
8463 8464 8465 8466 8467 8468 8469 8470 8471
    cpu_A0 = tcg_temp_new();

    cpu_tmp0 = tcg_temp_new();
    cpu_tmp1_i64 = tcg_temp_new_i64();
    cpu_tmp2_i32 = tcg_temp_new_i32();
    cpu_tmp3_i32 = tcg_temp_new_i32();
    cpu_tmp4 = tcg_temp_new();
    cpu_ptr0 = tcg_temp_new_ptr();
    cpu_ptr1 = tcg_temp_new_ptr();
8472
    cpu_cc_srcT = tcg_temp_local_new();
B
bellard 已提交
8473

8474 8475 8476
    return max_insns;
}

8477 8478 8479 8480
static void i386_tr_tb_start(DisasContextBase *db, CPUState *cpu)
{
}

8481 8482 8483 8484 8485 8486 8487
static void i386_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
{
    DisasContext *dc = container_of(dcbase, DisasContext, base);

    tcg_gen_insn_start(dc->base.pc_next, dc->cc_op);
}

8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499
static bool i386_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
                                     const CPUBreakpoint *bp)
{
    DisasContext *dc = container_of(dcbase, DisasContext, base);
    /* If RF is set, suppress an internally generated breakpoint.  */
    int flags = dc->base.tb->flags & HF_RF_MASK ? BP_GDB : BP_ANY;
    if (bp->flags & flags) {
        gen_debug(dc, dc->base.pc_next - dc->cs_base);
        dc->base.is_jmp = DISAS_NORETURN;
        /* The address covered by the breakpoint must be included in
           [tb->pc, tb->pc + tb->size) in order to for it to be
           properly cleared -- thus we increment the PC here so that
8500
           the generic logic setting tb->size later does the right thing.  */
8501 8502 8503 8504 8505 8506 8507
        dc->base.pc_next += 1;
        return true;
    } else {
        return false;
    }
}

8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519
static void i386_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
{
    DisasContext *dc = container_of(dcbase, DisasContext, base);
    target_ulong pc_next = disas_insn(dc, cpu);

    if (dc->tf || (dc->base.tb->flags & HF_INHIBIT_IRQ_MASK)) {
        /* if single step mode, we generate only one instruction and
           generate an exception */
        /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
           the flag and abort the translation to give the irqs a
           chance to happen */
        dc->base.is_jmp = DISAS_TOO_MANY;
8520
    } else if ((tb_cflags(dc->base.tb) & CF_USE_ICOUNT)
8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538
               && ((dc->base.pc_next & TARGET_PAGE_MASK)
                   != ((dc->base.pc_next + TARGET_MAX_INSN_SIZE - 1)
                       & TARGET_PAGE_MASK)
                   || (dc->base.pc_next & ~TARGET_PAGE_MASK) == 0)) {
        /* Do not cross the boundary of the pages in icount mode,
           it can cause an exception. Do it only when boundary is
           crossed by the first instruction in the block.
           If current instruction already crossed the bound - it's ok,
           because an exception hasn't stopped this code.
         */
        dc->base.is_jmp = DISAS_TOO_MANY;
    } else if ((pc_next - dc->base.pc_first) >= (TARGET_PAGE_SIZE - 32)) {
        dc->base.is_jmp = DISAS_TOO_MANY;
    }

    dc->base.pc_next = pc_next;
}

8539 8540 8541 8542 8543 8544 8545 8546 8547 8548
static void i386_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
{
    DisasContext *dc = container_of(dcbase, DisasContext, base);

    if (dc->base.is_jmp == DISAS_TOO_MANY) {
        gen_jmp_im(dc->base.pc_next - dc->cs_base);
        gen_eob(dc);
    }
}

8549 8550 8551 8552 8553 8554
static void i386_tr_disas_log(const DisasContextBase *dcbase,
                              CPUState *cpu)
{
    DisasContext *dc = container_of(dcbase, DisasContext, base);

    qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
8555
    log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
8556 8557
}

8558 8559 8560 8561 8562 8563 8564 8565 8566
static const TranslatorOps i386_tr_ops = {
    .init_disas_context = i386_tr_init_disas_context,
    .tb_start           = i386_tr_tb_start,
    .insn_start         = i386_tr_insn_start,
    .breakpoint_check   = i386_tr_breakpoint_check,
    .translate_insn     = i386_tr_translate_insn,
    .tb_stop            = i386_tr_tb_stop,
    .disas_log          = i386_tr_disas_log,
};
8567

8568 8569 8570 8571
/* generate intermediate code for basic block 'tb'.  */
void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb)
{
    DisasContext dc;
8572

8573
    translator_loop(&i386_tr_ops, &dc.base, cpu, tb);
B
bellard 已提交
8574 8575
}

8576 8577
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb,
                          target_ulong *data)
A
aurel32 已提交
8578
{
8579 8580 8581
    int cc_op = data[1];
    env->eip = data[0] - tb->cs_base;
    if (cc_op != CC_OP_DYNAMIC) {
A
aurel32 已提交
8582
        env->cc_op = cc_op;
8583
    }
A
aurel32 已提交
8584
}