translate.c 272.3 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 19 20 21 22 23 24 25
 */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <signal.h>

26
#include "qemu/host-utils.h"
B
bellard 已提交
27
#include "cpu.h"
28
#include "disas/disas.h"
B
bellard 已提交
29
#include "tcg-op.h"
P
Paolo Bonzini 已提交
30
#include "exec/cpu_ldst.h"
B
bellard 已提交
31

32 33
#include "exec/helper-proto.h"
#include "exec/helper-gen.h"
P
pbrook 已提交
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

B
bellard 已提交
60 61 62
//#define MACRO_TEST   1

/* global register indexes */
P
pbrook 已提交
63
static TCGv_ptr cpu_env;
64
static TCGv cpu_A0;
65
static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT;
P
pbrook 已提交
66
static TCGv_i32 cpu_cc_op;
67
static TCGv cpu_regs[CPU_NB_REGS];
68
/* local temps */
P
Paolo Bonzini 已提交
69
static TCGv cpu_T[2];
B
bellard 已提交
70
/* local register indexes (only used inside old micro ops) */
P
pbrook 已提交
71 72 73 74
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 已提交
75

76 77
static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];

78
#include "exec/gen-icount.h"
P
pbrook 已提交
79

B
bellard 已提交
80 81
#ifdef TARGET_X86_64
static int x86_64_hregs;
B
bellard 已提交
82 83
#endif

B
bellard 已提交
84 85 86 87
typedef struct DisasContext {
    /* current insn context */
    int override; /* -1 if no override */
    int prefix;
88
    TCGMemOp aflag;
89
    TCGMemOp dflag;
B
bellard 已提交
90
    target_ulong pc; /* pc = eip + cs_base */
B
bellard 已提交
91 92 93
    int is_jmp; /* 1 = means jump (stop translation), 2 means CPU
                   static state change (stop translation) */
    /* current block context */
B
bellard 已提交
94
    target_ulong cs_base; /* base of CS segment */
B
bellard 已提交
95 96
    int pe;     /* protected mode */
    int code32; /* 32 bit code segment */
B
bellard 已提交
97 98 99 100 101
#ifdef TARGET_X86_64
    int lma;    /* long mode active */
    int code64; /* 64 bit code segment */
    int rex_x, rex_b;
#endif
102 103
    int vex_l;  /* vex vector length */
    int vex_v;  /* vex vvvv register, without 1's compliment.  */
B
bellard 已提交
104
    int ss32;   /* 32 bit stack segment */
105
    CCOp cc_op;  /* current CC operation */
106
    bool cc_op_dirty;
B
bellard 已提交
107 108 109 110 111 112
    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 */
113
    int singlestep_enabled; /* "hardware" single step enabled */
B
bellard 已提交
114 115
    int jmp_opt; /* use direct block chaining for direct jumps */
    int mem_index; /* select memory access functions */
116
    uint64_t flags; /* all execution flags */
B
bellard 已提交
117 118
    struct TranslationBlock *tb;
    int popl_esp_hack; /* for correct popl with esp base handling */
B
bellard 已提交
119 120
    int rip_offset; /* only used in x86_64, but left for simplicity */
    int cpuid_features;
B
bellard 已提交
121
    int cpuid_ext_features;
122
    int cpuid_ext2_features;
B
bellard 已提交
123
    int cpuid_ext3_features;
H
H. Peter Anvin 已提交
124
    int cpuid_7_0_ebx_features;
B
bellard 已提交
125 126 127
} DisasContext;

static void gen_eob(DisasContext *s);
B
bellard 已提交
128 129
static void gen_jmp(DisasContext *s, target_ulong eip);
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
130
static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d);
B
bellard 已提交
131 132 133

/* i386 arith/logic operations */
enum {
134 135 136
    OP_ADDL,
    OP_ORL,
    OP_ADCL,
B
bellard 已提交
137
    OP_SBBL,
138 139 140
    OP_ANDL,
    OP_SUBL,
    OP_XORL,
B
bellard 已提交
141 142 143 144 145
    OP_CMPL,
};

/* i386 shift ops */
enum {
146 147 148 149 150 151
    OP_ROL,
    OP_ROR,
    OP_RCL,
    OP_RCR,
    OP_SHL,
    OP_SHR,
B
bellard 已提交
152 153 154 155
    OP_SHL1, /* undocumented */
    OP_SAR = 7,
};

156 157 158 159 160 161 162 163 164 165 166
enum {
    JCC_O,
    JCC_B,
    JCC_Z,
    JCC_BE,
    JCC_S,
    JCC_P,
    JCC_L,
    JCC_LE,
};

B
bellard 已提交
167 168 169 170 171 172 173 174 175 176
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 已提交
177 178

    OR_TMP0 = 16,    /* temporary operand register */
B
bellard 已提交
179 180 181 182
    OR_TMP1,
    OR_A0, /* temporary register used when doing address evaluation */
};

183
enum {
184 185
    USES_CC_DST  = 1,
    USES_CC_SRC  = 2,
186 187
    USES_CC_SRC2 = 4,
    USES_CC_SRCT = 8,
188 189 190 191
};

/* Bit set if the global variable is live after setting CC_OP to X.  */
static const uint8_t cc_op_live[CC_OP_NB] = {
192
    [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
193 194 195
    [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,
196
    [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
197
    [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRCT,
198
    [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
199 200 201 202 203
    [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,
204
    [CC_OP_BMILGB ... CC_OP_BMILGQ] = USES_CC_DST | USES_CC_SRC,
205 206 207
    [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 已提交
208
    [CC_OP_CLR] = 0,
209 210
};

211
static void set_cc_op(DisasContext *s, CCOp op)
212
{
213 214 215 216 217 218 219 220 221 222
    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);
223
    }
224 225 226
    if (dead & USES_CC_SRC) {
        tcg_gen_discard_tl(cpu_cc_src);
    }
227 228 229
    if (dead & USES_CC_SRC2) {
        tcg_gen_discard_tl(cpu_cc_src2);
    }
230 231 232
    if (dead & USES_CC_SRCT) {
        tcg_gen_discard_tl(cpu_cc_srcT);
    }
233

234 235 236 237 238 239 240 241 242 243 244
    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;
    }
245
    s->cc_op = op;
246 247 248 249 250
}

static void gen_update_cc_op(DisasContext *s)
{
    if (s->cc_op_dirty) {
251
        tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
252 253
        s->cc_op_dirty = false;
    }
254 255
}

B
bellard 已提交
256 257 258 259 260 261 262 263 264 265
#ifdef TARGET_X86_64

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

266
#if defined(HOST_WORDS_BIGENDIAN)
B
bellard 已提交
267 268 269 270 271
#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 已提交
272
#else
B
bellard 已提交
273 274 275 276 277
#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 已提交
278
#endif
B
bellard 已提交
279

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
/* 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;
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/* 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;
    }
}

/* 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;
}

333
static void gen_op_mov_reg_v(TCGMemOp ot, int reg, TCGv t0)
B
bellard 已提交
334 335
{
    switch(ot) {
336
    case MO_8:
337
        if (!byte_reg_is_xH(reg)) {
338
            tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
B
bellard 已提交
339
        } else {
340
            tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
B
bellard 已提交
341 342
        }
        break;
343
    case MO_16:
344
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
B
bellard 已提交
345
        break;
346
    case MO_32:
347 348 349
        /* 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 已提交
350
        break;
351
#ifdef TARGET_X86_64
352
    case MO_64:
353
        tcg_gen_mov_tl(cpu_regs[reg], t0);
B
bellard 已提交
354
        break;
B
bellard 已提交
355
#endif
356 357
    default:
        tcg_abort();
B
bellard 已提交
358 359
    }
}
B
bellard 已提交
360

361
static inline void gen_op_mov_v_reg(TCGMemOp ot, TCGv t0, int reg)
B
bellard 已提交
362
{
363
    if (ot == MO_8 && byte_reg_is_xH(reg)) {
364 365 366
        tcg_gen_shri_tl(t0, cpu_regs[reg - 4], 8);
        tcg_gen_ext8u_tl(t0, t0);
    } else {
367
        tcg_gen_mov_tl(t0, cpu_regs[reg]);
B
bellard 已提交
368 369 370 371 372
    }
}

static inline void gen_op_movl_A0_reg(int reg)
{
373
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
374 375 376 377 378
}

static inline void gen_op_addl_A0_im(int32_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
B
bellard 已提交
379
#ifdef TARGET_X86_64
B
bellard 已提交
380
    tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
381
#endif
B
bellard 已提交
382
}
B
bellard 已提交
383

B
bellard 已提交
384
#ifdef TARGET_X86_64
B
bellard 已提交
385 386 387 388
static inline void gen_op_addq_A0_im(int64_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
}
B
bellard 已提交
389
#endif
B
bellard 已提交
390 391 392 393 394 395 396 397 398 399
    
static void gen_add_A0_im(DisasContext *s, int val)
{
#ifdef TARGET_X86_64
    if (CODE64(s))
        gen_op_addq_A0_im(val);
    else
#endif
        gen_op_addl_A0_im(val);
}
B
bellard 已提交
400

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

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

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

static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
{
420 421
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
422 423
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
424 425 426
    /* For x86_64, this sets the higher half of register to zero.
       For i386, this is equivalent to a nop. */
    tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
427
}
B
bellard 已提交
428

B
bellard 已提交
429 430
static inline void gen_op_movl_A0_seg(int reg)
{
431
    tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
B
bellard 已提交
432
}
B
bellard 已提交
433

434
static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
B
bellard 已提交
435
{
436
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
437
#ifdef TARGET_X86_64
438 439 440 441 442 443 444 445 446
    if (CODE64(s)) {
        tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
        tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
    } else {
        tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
        tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
    }
#else
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
B
bellard 已提交
447 448
#endif
}
B
bellard 已提交
449

B
bellard 已提交
450
#ifdef TARGET_X86_64
B
bellard 已提交
451 452
static inline void gen_op_movq_A0_seg(int reg)
{
453
    tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
454
}
B
bellard 已提交
455

B
bellard 已提交
456 457
static inline void gen_op_addq_A0_seg(int reg)
{
458
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
459 460 461 462 463
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}

static inline void gen_op_movq_A0_reg(int reg)
{
464
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
465 466 467 468
}

static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
{
469 470
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
471 472 473
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}
B
bellard 已提交
474 475
#endif

476
static inline void gen_op_ld_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
477
{
478
    tcg_gen_qemu_ld_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
479
}
B
bellard 已提交
480

481
static inline void gen_op_st_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
482
{
483
    tcg_gen_qemu_st_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
484
}
485

486 487 488
static inline void gen_op_st_rm_T0_A0(DisasContext *s, int idx, int d)
{
    if (d == OR_TMP0) {
489
        gen_op_st_v(s, idx, cpu_T[0], cpu_A0);
490
    } else {
491
        gen_op_mov_reg_v(idx, d, cpu_T[0]);
492 493 494
    }
}

B
bellard 已提交
495 496
static inline void gen_jmp_im(target_ulong pc)
{
B
bellard 已提交
497
    tcg_gen_movi_tl(cpu_tmp0, pc);
498
    gen_op_jmp_v(cpu_tmp0);
B
bellard 已提交
499 500
}

B
bellard 已提交
501 502 503 504 505
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
    int override;

    override = s->override;
506
    switch (s->aflag) {
B
bellard 已提交
507
#ifdef TARGET_X86_64
508
    case MO_64:
B
bellard 已提交
509
        if (override >= 0) {
B
bellard 已提交
510 511
            gen_op_movq_A0_seg(override);
            gen_op_addq_A0_reg_sN(0, R_ESI);
B
bellard 已提交
512
        } else {
B
bellard 已提交
513
            gen_op_movq_A0_reg(R_ESI);
B
bellard 已提交
514
        }
515
        break;
B
bellard 已提交
516
#endif
517
    case MO_32:
B
bellard 已提交
518 519 520 521
        /* 32 bit address */
        if (s->addseg && override < 0)
            override = R_DS;
        if (override >= 0) {
B
bellard 已提交
522 523
            gen_op_movl_A0_seg(override);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
524
        } else {
B
bellard 已提交
525
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
526
        }
527 528
        break;
    case MO_16:
B
bellard 已提交
529 530 531
        /* 16 address, always override */
        if (override < 0)
            override = R_DS;
532
        tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_ESI]);
533
        gen_op_addl_A0_seg(s, override);
534 535 536
        break;
    default:
        tcg_abort();
B
bellard 已提交
537 538 539 540 541
    }
}

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
542
    switch (s->aflag) {
B
bellard 已提交
543
#ifdef TARGET_X86_64
544
    case MO_64:
B
bellard 已提交
545
        gen_op_movq_A0_reg(R_EDI);
546
        break;
B
bellard 已提交
547
#endif
548
    case MO_32:
B
bellard 已提交
549
        if (s->addseg) {
B
bellard 已提交
550 551
            gen_op_movl_A0_seg(R_ES);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
552
        } else {
B
bellard 已提交
553
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
554
        }
555 556
        break;
    case MO_16:
557
        tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_EDI]);
558
        gen_op_addl_A0_seg(s, R_ES);
559 560 561
        break;
    default:
        tcg_abort();
B
bellard 已提交
562 563 564
    }
}

565
static inline void gen_op_movl_T0_Dshift(TCGMemOp ot)
566
{
567
    tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
568
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
B
bellard 已提交
569 570
};

571
static TCGv gen_ext_tl(TCGv dst, TCGv src, TCGMemOp size, bool sign)
572
{
573
    switch (size) {
574
    case MO_8:
575 576 577 578 579 580
        if (sign) {
            tcg_gen_ext8s_tl(dst, src);
        } else {
            tcg_gen_ext8u_tl(dst, src);
        }
        return dst;
581
    case MO_16:
582 583 584 585 586 587 588
        if (sign) {
            tcg_gen_ext16s_tl(dst, src);
        } else {
            tcg_gen_ext16u_tl(dst, src);
        }
        return dst;
#ifdef TARGET_X86_64
589
    case MO_32:
590 591 592 593 594 595 596
        if (sign) {
            tcg_gen_ext32s_tl(dst, src);
        } else {
            tcg_gen_ext32u_tl(dst, src);
        }
        return dst;
#endif
597
    default:
598
        return src;
599 600
    }
}
601

602
static void gen_extu(TCGMemOp ot, TCGv reg)
603 604 605 606
{
    gen_ext_tl(reg, reg, ot, false);
}

607
static void gen_exts(TCGMemOp ot, TCGv reg)
608
{
609
    gen_ext_tl(reg, reg, ot, true);
610
}
B
bellard 已提交
611

612
static inline void gen_op_jnz_ecx(TCGMemOp size, int label1)
613
{
614
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
615
    gen_extu(size, cpu_tmp0);
P
pbrook 已提交
616
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
617 618
}

619
static inline void gen_op_jz_ecx(TCGMemOp size, int label1)
620
{
621
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
622
    gen_extu(size, cpu_tmp0);
P
pbrook 已提交
623
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
624
}
B
bellard 已提交
625

626
static void gen_helper_in_func(TCGMemOp ot, TCGv v, TCGv_i32 n)
P
pbrook 已提交
627 628
{
    switch (ot) {
629
    case MO_8:
630 631
        gen_helper_inb(v, n);
        break;
632
    case MO_16:
633 634
        gen_helper_inw(v, n);
        break;
635
    case MO_32:
636 637
        gen_helper_inl(v, n);
        break;
638 639
    default:
        tcg_abort();
P
pbrook 已提交
640 641
    }
}
B
bellard 已提交
642

643
static void gen_helper_out_func(TCGMemOp ot, TCGv_i32 v, TCGv_i32 n)
P
pbrook 已提交
644 645
{
    switch (ot) {
646
    case MO_8:
647 648
        gen_helper_outb(v, n);
        break;
649
    case MO_16:
650 651
        gen_helper_outw(v, n);
        break;
652
    case MO_32:
653 654
        gen_helper_outl(v, n);
        break;
655 656
    default:
        tcg_abort();
P
pbrook 已提交
657 658
    }
}
659

660
static void gen_check_io(DisasContext *s, TCGMemOp ot, target_ulong cur_eip,
661
                         uint32_t svm_flags)
662
{
663 664 665 666
    int state_saved;
    target_ulong next_eip;

    state_saved = 0;
667
    if (s->pe && (s->cpl > s->iopl || s->vm86)) {
668
        gen_update_cc_op(s);
B
bellard 已提交
669
        gen_jmp_im(cur_eip);
670
        state_saved = 1;
671
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
672
        switch (ot) {
673
        case MO_8:
B
Blue Swirl 已提交
674 675
            gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
            break;
676
        case MO_16:
B
Blue Swirl 已提交
677 678
            gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
            break;
679
        case MO_32:
B
Blue Swirl 已提交
680 681
            gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
            break;
682 683
        default:
            tcg_abort();
P
pbrook 已提交
684
        }
685
    }
B
bellard 已提交
686
    if(s->flags & HF_SVMI_MASK) {
687
        if (!state_saved) {
688
            gen_update_cc_op(s);
689 690 691 692
            gen_jmp_im(cur_eip);
        }
        svm_flags |= (1 << (4 + ot));
        next_eip = s->pc - s->cs_base;
693
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
694 695
        gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
                                tcg_const_i32(svm_flags),
P
pbrook 已提交
696
                                tcg_const_i32(next_eip - cur_eip));
697 698 699
    }
}

700
static inline void gen_movs(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
701 702
{
    gen_string_movl_A0_ESI(s);
703
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
704
    gen_string_movl_A0_EDI(s);
705
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
706
    gen_op_movl_T0_Dshift(ot);
707 708
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
709 710
}

711 712 713 714 715 716 717 718 719 720 721
static void gen_op_update1_cc(void)
{
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
}

static void gen_op_update2_cc(void)
{
    tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
}

722 723 724 725 726 727 728
static void gen_op_update3_cc(TCGv reg)
{
    tcg_gen_mov_tl(cpu_cc_src2, reg);
    tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
}

729 730 731 732 733 734 735 736
static inline void gen_op_testl_T0_T1_cc(void)
{
    tcg_gen_and_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
}

static void gen_op_update_neg_cc(void)
{
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
737 738
    tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
    tcg_gen_movi_tl(cpu_cc_srcT, 0);
739 740
}

741 742
/* compute all eflags to cc_src */
static void gen_compute_eflags(DisasContext *s)
743
{
744
    TCGv zero, dst, src1, src2;
745 746
    int live, dead;

747 748 749
    if (s->cc_op == CC_OP_EFLAGS) {
        return;
    }
R
Richard Henderson 已提交
750
    if (s->cc_op == CC_OP_CLR) {
751
        tcg_gen_movi_tl(cpu_cc_src, CC_Z | CC_P);
R
Richard Henderson 已提交
752 753 754
        set_cc_op(s, CC_OP_EFLAGS);
        return;
    }
755 756 757 758

    TCGV_UNUSED(zero);
    dst = cpu_cc_dst;
    src1 = cpu_cc_src;
759
    src2 = cpu_cc_src2;
760 761 762

    /* Take care to not read values that are not live.  */
    live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
763
    dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
764 765 766 767 768 769 770 771
    if (dead) {
        zero = tcg_const_tl(0);
        if (dead & USES_CC_DST) {
            dst = zero;
        }
        if (dead & USES_CC_SRC) {
            src1 = zero;
        }
772 773 774
        if (dead & USES_CC_SRC2) {
            src2 = zero;
        }
775 776
    }

777
    gen_update_cc_op(s);
778
    gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
779
    set_cc_op(s, CC_OP_EFLAGS);
780 781 782 783

    if (dead) {
        tcg_temp_free(zero);
    }
784 785
}

786 787 788 789 790 791 792 793 794 795
typedef struct CCPrepare {
    TCGCond cond;
    TCGv reg;
    TCGv reg2;
    target_ulong imm;
    target_ulong mask;
    bool use_reg2;
    bool no_setcond;
} CCPrepare;

796
/* compute eflags.C to reg */
797
static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
798 799
{
    TCGv t0, t1;
800
    int size, shift;
801 802 803

    switch (s->cc_op) {
    case CC_OP_SUBB ... CC_OP_SUBQ:
804
        /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
805 806 807 808
        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.  */
        t0 = TCGV_EQUAL(t1, cpu_cc_src) ? cpu_tmp0 : reg;
809
        tcg_gen_mov_tl(t0, cpu_cc_srcT);
810 811 812 813 814 815 816 817 818
        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:
819 820
        return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
                             .reg2 = t1, .mask = -1, .use_reg2 = true };
821 822

    case CC_OP_LOGICB ... CC_OP_LOGICQ:
R
Richard Henderson 已提交
823
    case CC_OP_CLR:
824
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
825 826 827

    case CC_OP_INCB ... CC_OP_INCQ:
    case CC_OP_DECB ... CC_OP_DECQ:
828 829
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = -1, .no_setcond = true };
830 831 832 833

    case CC_OP_SHLB ... CC_OP_SHLQ:
        /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
        size = s->cc_op - CC_OP_SHLB;
834 835 836
        shift = (8 << size) - 1;
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = (target_ulong)1 << shift };
837 838

    case CC_OP_MULB ... CC_OP_MULQ:
839 840
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = -1 };
841

842 843 844 845 846
    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 };

847 848 849 850 851
    case CC_OP_ADCX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
                             .mask = -1, .no_setcond = true };

852 853 854
    case CC_OP_EFLAGS:
    case CC_OP_SARB ... CC_OP_SARQ:
        /* CC_SRC & 1 */
855 856
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = CC_C };
857 858 859 860 861

    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);
862 863
       gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
                               cpu_cc_src2, cpu_cc_op);
864 865
       return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
                            .mask = -1, .no_setcond = true };
866 867 868
    }
}

869
/* compute eflags.P to reg */
870
static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
871
{
872
    gen_compute_eflags(s);
873 874
    return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                         .mask = CC_P };
875 876 877
}

/* compute eflags.S to reg */
878
static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
879
{
880 881 882 883 884
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
885 886 887
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
888 889
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_S };
R
Richard Henderson 已提交
890 891
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
892 893
    default:
        {
894
            TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
895
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
896
            return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
897 898
        }
    }
899 900 901
}

/* compute eflags.O to reg */
902
static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
903
{
904 905 906 907 908
    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 已提交
909 910
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
911 912 913 914 915
    default:
        gen_compute_eflags(s);
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_O };
    }
916 917 918
}

/* compute eflags.Z to reg */
919
static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
920
{
921 922 923 924 925
    switch (s->cc_op) {
    case CC_OP_DYNAMIC:
        gen_compute_eflags(s);
        /* FALLTHRU */
    case CC_OP_EFLAGS:
926 927 928
    case CC_OP_ADCX:
    case CC_OP_ADOX:
    case CC_OP_ADCOX:
929 930
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = CC_Z };
R
Richard Henderson 已提交
931 932
    case CC_OP_CLR:
        return (CCPrepare) { .cond = TCG_COND_ALWAYS, .mask = -1 };
933 934
    default:
        {
935
            TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
936
            TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
937
            return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
938
        }
939 940 941
    }
}

942 943
/* perform a conditional store into register 'reg' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
944
static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
945
{
946 947
    int inv, jcc_op, cond;
    TCGMemOp size;
948
    CCPrepare cc;
949 950 951
    TCGv t0;

    inv = b & 1;
952
    jcc_op = (b >> 1) & 7;
953 954

    switch (s->cc_op) {
955 956
    case CC_OP_SUBB ... CC_OP_SUBQ:
        /* We optimize relational operators for the cmp/jcc case.  */
957 958 959
        size = s->cc_op - CC_OP_SUBB;
        switch (jcc_op) {
        case JCC_BE:
960
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
961 962
            gen_extu(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
963 964
            cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
965
            break;
966

967
        case JCC_L:
968
            cond = TCG_COND_LT;
969 970
            goto fast_jcc_l;
        case JCC_LE:
971
            cond = TCG_COND_LE;
972
        fast_jcc_l:
973
            tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
974 975
            gen_exts(size, cpu_tmp4);
            t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
976 977
            cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
                               .reg2 = t0, .mask = -1, .use_reg2 = true };
978
            break;
979

980
        default:
981
            goto slow_jcc;
982
        }
983
        break;
984

985 986
    default:
    slow_jcc:
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 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
        /* 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);
            if (TCGV_EQUAL(reg, cpu_cc_src)) {
                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);
            if (TCGV_EQUAL(reg, cpu_cc_src)) {
                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;
        }
1031
        break;
1032
    }
1033 1034 1035 1036 1037

    if (inv) {
        cc.cond = tcg_invert_cond(cc.cond);
    }
    return cc;
1038 1039
}

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
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);
}
1074

1075 1076
/* generate a conditional jump to label 'l1' according to jump opcode
   value 'b'. In the fast case, T0 is guaranted not to be used. */
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
static inline void gen_jcc1_noeob(DisasContext *s, int b, int l1)
{
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);

    if (cc.mask != -1) {
        tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
        cc.reg = cpu_T[0];
    }
    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.  */
1095
static inline void gen_jcc1(DisasContext *s, int b, int l1)
1096
{
1097
    CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
1098

1099
    gen_update_cc_op(s);
1100 1101 1102 1103
    if (cc.mask != -1) {
        tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
        cc.reg = cpu_T[0];
    }
1104
    set_cc_op(s, CC_OP_DYNAMIC);
1105 1106 1107 1108
    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);
1109 1110 1111
    }
}

B
bellard 已提交
1112 1113 1114
/* XXX: does not work with gdbstub "ice" single step - not a
   serious problem */
static int gen_jz_ecx_string(DisasContext *s, target_ulong next_eip)
B
bellard 已提交
1115
{
B
bellard 已提交
1116 1117 1118 1119
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
1120
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1121 1122 1123 1124
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1125 1126
}

1127
static inline void gen_stos(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1128
{
1129
    gen_op_mov_v_reg(MO_32, cpu_T[0], R_EAX);
B
bellard 已提交
1130
    gen_string_movl_A0_EDI(s);
1131
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1132
    gen_op_movl_T0_Dshift(ot);
1133
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1134 1135
}

1136
static inline void gen_lods(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1137 1138
{
    gen_string_movl_A0_ESI(s);
1139
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1140
    gen_op_mov_reg_v(ot, R_EAX, cpu_T[0]);
1141
    gen_op_movl_T0_Dshift(ot);
1142
    gen_op_add_reg_T0(s->aflag, R_ESI);
B
bellard 已提交
1143 1144
}

1145
static inline void gen_scas(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1146 1147
{
    gen_string_movl_A0_EDI(s);
1148
    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
1149
    gen_op(s, OP_CMPL, ot, R_EAX);
1150
    gen_op_movl_T0_Dshift(ot);
1151
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1152 1153
}

1154
static inline void gen_cmps(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1155 1156
{
    gen_string_movl_A0_EDI(s);
1157
    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
1158 1159
    gen_string_movl_A0_ESI(s);
    gen_op(s, OP_CMPL, ot, OR_TMP0);
1160
    gen_op_movl_T0_Dshift(ot);
1161 1162
    gen_op_add_reg_T0(s->aflag, R_ESI);
    gen_op_add_reg_T0(s->aflag, R_EDI);
B
bellard 已提交
1163 1164
}

1165
static inline void gen_ins(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1166
{
P
pbrook 已提交
1167 1168
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1169
    gen_string_movl_A0_EDI(s);
1170 1171
    /* Note: we must do this dummy write first to be restartable in
       case of page fault. */
1172
    tcg_gen_movi_tl(cpu_T[0], 0);
1173
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1174
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1175
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
P
pbrook 已提交
1176
    gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
1177
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1178
    gen_op_movl_T0_Dshift(ot);
1179
    gen_op_add_reg_T0(s->aflag, R_EDI);
P
pbrook 已提交
1180 1181
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1182 1183
}

1184
static inline void gen_outs(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1185
{
P
pbrook 已提交
1186 1187
    if (use_icount)
        gen_io_start();
B
bellard 已提交
1188
    gen_string_movl_A0_ESI(s);
1189
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1190

1191
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1192 1193
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
P
pbrook 已提交
1194
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1195

1196
    gen_op_movl_T0_Dshift(ot);
1197
    gen_op_add_reg_T0(s->aflag, R_ESI);
P
pbrook 已提交
1198 1199
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1200 1201 1202 1203 1204
}

/* same method as Valgrind : we generate jumps to current or next
   instruction */
#define GEN_REPZ(op)                                                          \
1205
static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot,              \
B
bellard 已提交
1206
                                 target_ulong cur_eip, target_ulong next_eip) \
B
bellard 已提交
1207
{                                                                             \
B
bellard 已提交
1208
    int l2;\
B
bellard 已提交
1209
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1210
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1211
    gen_ ## op(s, ot);                                                        \
1212
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
B
bellard 已提交
1213 1214 1215
    /* a loop would cause two single step exceptions if ECX = 1               \
       before rep string_insn */                                              \
    if (!s->jmp_opt)                                                          \
1216
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1217 1218 1219 1220
    gen_jmp(s, cur_eip);                                                      \
}

#define GEN_REPZ2(op)                                                         \
1221
static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot,              \
B
bellard 已提交
1222 1223
                                   target_ulong cur_eip,                      \
                                   target_ulong next_eip,                     \
B
bellard 已提交
1224 1225
                                   int nz)                                    \
{                                                                             \
B
bellard 已提交
1226
    int l2;\
B
bellard 已提交
1227
    gen_update_cc_op(s);                                                      \
B
bellard 已提交
1228
    l2 = gen_jz_ecx_string(s, next_eip);                                      \
B
bellard 已提交
1229
    gen_ ## op(s, ot);                                                        \
1230
    gen_op_add_reg_im(s->aflag, R_ECX, -1);                                   \
1231
    gen_update_cc_op(s);                                                      \
1232
    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);                                 \
B
bellard 已提交
1233
    if (!s->jmp_opt)                                                          \
1234
        gen_op_jz_ecx(s->aflag, l2);                                          \
B
bellard 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
    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 已提交
1246 1247 1248
static void gen_helper_fp_arith_ST0_FT0(int op)
{
    switch (op) {
B
Blue Swirl 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
    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 已提交
1273 1274
    }
}
B
bellard 已提交
1275 1276

/* NOTE the exception in "r" op ordering */
P
pbrook 已提交
1277 1278 1279 1280
static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
{
    TCGv_i32 tmp = tcg_const_i32(opreg);
    switch (op) {
B
Blue Swirl 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
    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 已提交
1299 1300
    }
}
B
bellard 已提交
1301 1302

/* if d == OR_TMP0, it means memory operand (address in A0) */
1303
static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d)
B
bellard 已提交
1304 1305
{
    if (d != OR_TMP0) {
1306
        gen_op_mov_v_reg(ot, cpu_T[0], d);
B
bellard 已提交
1307
    } else {
1308
        gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1309 1310 1311
    }
    switch(op) {
    case OP_ADCL:
1312
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1313 1314
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
1315
        gen_op_st_rm_T0_A0(s1, ot, d);
1316 1317
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_ADCB + ot);
B
bellard 已提交
1318
        break;
B
bellard 已提交
1319
    case OP_SBBL:
1320
        gen_compute_eflags_c(s1, cpu_tmp4);
B
bellard 已提交
1321 1322
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
1323
        gen_op_st_rm_T0_A0(s1, ot, d);
1324 1325
        gen_op_update3_cc(cpu_tmp4);
        set_cc_op(s1, CC_OP_SBBB + ot);
B
bellard 已提交
1326
        break;
B
bellard 已提交
1327
    case OP_ADDL:
1328
        tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1329
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1330
        gen_op_update2_cc();
1331
        set_cc_op(s1, CC_OP_ADDB + ot);
B
bellard 已提交
1332 1333
        break;
    case OP_SUBL:
1334
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
B
bellard 已提交
1335
        tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1336
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1337
        gen_op_update2_cc();
1338
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1339 1340 1341
        break;
    default:
    case OP_ANDL:
B
bellard 已提交
1342
        tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1343
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1344
        gen_op_update1_cc();
1345
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1346
        break;
B
bellard 已提交
1347
    case OP_ORL:
B
bellard 已提交
1348
        tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1349
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1350
        gen_op_update1_cc();
1351
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1352
        break;
B
bellard 已提交
1353
    case OP_XORL:
B
bellard 已提交
1354
        tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1355
        gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1356
        gen_op_update1_cc();
1357
        set_cc_op(s1, CC_OP_LOGICB + ot);
B
bellard 已提交
1358 1359
        break;
    case OP_CMPL:
1360
        tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
1361
        tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
1362
        tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
1363
        set_cc_op(s1, CC_OP_SUBB + ot);
B
bellard 已提交
1364 1365
        break;
    }
1366 1367
}

B
bellard 已提交
1368
/* if d == OR_TMP0, it means memory operand (address in A0) */
1369
static void gen_inc(DisasContext *s1, TCGMemOp ot, int d, int c)
B
bellard 已提交
1370
{
1371
    if (d != OR_TMP0) {
1372
        gen_op_mov_v_reg(ot, cpu_T[0], d);
1373 1374 1375
    } else {
        gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
    }
1376
    gen_compute_eflags_c(s1, cpu_cc_src);
B
bellard 已提交
1377
    if (c > 0) {
1378
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
1379
        set_cc_op(s1, CC_OP_INCB + ot);
B
bellard 已提交
1380
    } else {
1381
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
1382
        set_cc_op(s1, CC_OP_DECB + ot);
B
bellard 已提交
1383
    }
1384
    gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1385
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
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_T[0], cpu_A0);
1441
    } else {
1442
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1443
    }
1444

1445 1446
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
    tcg_gen_subi_tl(cpu_tmp0, cpu_T[1], 1);
1447 1448 1449

    if (is_right) {
        if (is_arith) {
B
bellard 已提交
1450
            gen_exts(ot, cpu_T[0]);
1451 1452
            tcg_gen_sar_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1453
        } else {
B
bellard 已提交
1454
            gen_extu(ot, cpu_T[0]);
1455 1456
            tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1457 1458
        }
    } else {
1459 1460
        tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
        tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1461 1462 1463
    }

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

1466
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, cpu_T[1], 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_T[0], cpu_A0);
B
bellard 已提交
1477
    else
1478
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
B
bellard 已提交
1479 1480 1481 1482 1483 1484

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

    /* 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);
B
bellard 已提交
1504
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
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_T[0], cpu_A0);
1517
    } else {
1518
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1519
    }
1520

1521
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
1522

1523
    switch (ot) {
1524
    case MO_8:
1525 1526 1527 1528
        /* Replicate the 8-bit input so that a 32-bit rotate works.  */
        tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
        tcg_gen_muli_tl(cpu_T[0], cpu_T[0], 0x01010101);
        goto do_long;
1529
    case MO_16:
1530 1531 1532 1533 1534
        /* Replicate the 16-bit input so that a 32-bit rotate works.  */
        tcg_gen_deposit_tl(cpu_T[0], cpu_T[0], cpu_T[0], 16, 16);
        goto do_long;
    do_long:
#ifdef TARGET_X86_64
1535
    case MO_32:
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
        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);
        }
        tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
        break;
#endif
    default:
        if (is_right) {
            tcg_gen_rotr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        } else {
            tcg_gen_rotl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
        }
        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_T[0], mask - 1);
        tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
P
Pavel Dovgaluk 已提交
1568
        tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1569 1570 1571
    } else {
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
        tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
1572
    }
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
    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();
    tcg_gen_trunc_tl_i32(t1, cpu_T[1]);
    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_T[0], cpu_A0);
M
malc 已提交
1603
    } else {
1604
        gen_op_mov_v_reg(ot, cpu_T[0], 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 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
            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);
            }
            tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
            break;
#endif
        default:
            if (is_right) {
                tcg_gen_rotri_tl(cpu_T[0], cpu_T[0], op2);
            } else {
                tcg_gen_rotli_tl(cpu_T[0], cpu_T[0], op2);
            }
            break;
1628
        case MO_8:
1629 1630
            mask = 7;
            goto do_shifts;
1631
        case MO_16:
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
            mask = 15;
        do_shifts:
            shift = op2 & mask;
            if (is_right) {
                shift = mask + 1 - shift;
            }
            gen_extu(ot, cpu_T[0]);
            tcg_gen_shli_tl(cpu_tmp0, cpu_T[0], shift);
            tcg_gen_shri_tl(cpu_T[0], cpu_T[0], mask + 1 - shift);
            tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            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_T[0], mask - 1);
            tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
1660
            tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1661 1662 1663
        } else {
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
            tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 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_T[0], cpu_A0);
1681
    else
1682
        gen_op_mov_v_reg(ot, cpu_T[0], op1);
1683
    
P
pbrook 已提交
1684 1685
    if (is_right) {
        switch (ot) {
1686
        case MO_8:
1687 1688
            gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1689
        case MO_16:
1690 1691
            gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1692
        case MO_32:
1693 1694
            gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1695
#ifdef TARGET_X86_64
1696
        case MO_64:
1697 1698
            gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1699
#endif
1700 1701
        default:
            tcg_abort();
P
pbrook 已提交
1702 1703 1704
        }
    } else {
        switch (ot) {
1705
        case MO_8:
1706 1707
            gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1708
        case MO_16:
1709 1710
            gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
1711
        case MO_32:
1712 1713
            gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            break;
P
pbrook 已提交
1714
#ifdef TARGET_X86_64
1715
        case MO_64:
1716 1717
            gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
            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_T[0], cpu_A0);
1737
    } else {
1738
        gen_op_mov_v_reg(ot, cpu_T[0], 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_T[0], cpu_T[1], 16, 16);
            tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
            tcg_gen_mov_tl(cpu_T[0], cpu_tmp0);
1753
        } else {
1754
            tcg_gen_deposit_tl(cpu_T[1], cpu_T[0], cpu_T[1], 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 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
            tcg_gen_concat_tl_i64(cpu_T[0], cpu_T[0], cpu_T[1]);
            tcg_gen_shr_i64(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shr_i64(cpu_T[0], cpu_T[0], count);
        } else {
            tcg_gen_concat_tl_i64(cpu_T[0], cpu_T[1], cpu_T[0]);
            tcg_gen_shl_i64(cpu_tmp0, cpu_T[0], cpu_tmp0);
            tcg_gen_shl_i64(cpu_T[0], cpu_T[0], count);
            tcg_gen_shri_i64(cpu_tmp0, cpu_tmp0, 32);
            tcg_gen_shri_i64(cpu_T[0], cpu_T[0], 32);
        }
        break;
#endif
    default:
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
        if (is_right) {
            tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1778

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

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

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

1805 1806
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, count, is_right);
    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_T[1], 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_T[1], c);
B
bellard 已提交
1861 1862 1863
        gen_shift(s1, op, ot, d, OR_TMP1);
        break;
    }
B
bellard 已提交
1864 1865
}

1866
static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
1867
{
B
bellard 已提交
1868
    target_long disp;
B
bellard 已提交
1869
    int havesib;
B
bellard 已提交
1870
    int base;
B
bellard 已提交
1871 1872 1873
    int index;
    int scale;
    int mod, rm, code, override, must_add_seg;
1874
    TCGv sum;
B
bellard 已提交
1875 1876 1877 1878 1879 1880 1881 1882

    override = s->override;
    must_add_seg = s->addseg;
    if (override >= 0)
        must_add_seg = 1;
    mod = (modrm >> 6) & 3;
    rm = modrm & 7;

1883 1884 1885
    switch (s->aflag) {
    case MO_64:
    case MO_32:
B
bellard 已提交
1886 1887
        havesib = 0;
        base = rm;
1888
        index = -1;
B
bellard 已提交
1889
        scale = 0;
1890

B
bellard 已提交
1891 1892
        if (base == 4) {
            havesib = 1;
1893
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
1894
            scale = (code >> 6) & 3;
B
bellard 已提交
1895
            index = ((code >> 3) & 7) | REX_X(s);
1896 1897 1898
            if (index == 4) {
                index = -1;  /* no index */
            }
B
bellard 已提交
1899
            base = (code & 7);
B
bellard 已提交
1900
        }
B
bellard 已提交
1901
        base |= REX_B(s);
B
bellard 已提交
1902 1903 1904

        switch (mod) {
        case 0:
B
bellard 已提交
1905
            if ((base & 7) == 5) {
B
bellard 已提交
1906
                base = -1;
1907
                disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
1908
                s->pc += 4;
B
bellard 已提交
1909 1910 1911
                if (CODE64(s) && !havesib) {
                    disp += s->pc + s->rip_offset;
                }
B
bellard 已提交
1912 1913 1914 1915 1916
            } else {
                disp = 0;
            }
            break;
        case 1:
1917
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
1918 1919 1920
            break;
        default:
        case 2:
1921
            disp = (int32_t)cpu_ldl_code(env, s->pc);
B
bellard 已提交
1922 1923 1924
            s->pc += 4;
            break;
        }
1925

1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
        /* For correct popl handling with esp.  */
        if (base == R_ESP && s->popl_esp_hack) {
            disp += s->popl_esp_hack;
        }

        /* Compute the address, with a minimum number of TCG ops.  */
        TCGV_UNUSED(sum);
        if (index >= 0) {
            if (scale == 0) {
                sum = cpu_regs[index];
            } else {
                tcg_gen_shli_tl(cpu_A0, cpu_regs[index], scale);
                sum = cpu_A0;
B
bellard 已提交
1939
            }
1940 1941 1942
            if (base >= 0) {
                tcg_gen_add_tl(cpu_A0, sum, cpu_regs[base]);
                sum = cpu_A0;
B
bellard 已提交
1943
            }
1944 1945
        } else if (base >= 0) {
            sum = cpu_regs[base];
B
bellard 已提交
1946
        }
1947 1948 1949 1950
        if (TCGV_IS_UNUSED(sum)) {
            tcg_gen_movi_tl(cpu_A0, disp);
        } else {
            tcg_gen_addi_tl(cpu_A0, sum, disp);
B
bellard 已提交
1951
        }
1952

B
bellard 已提交
1953 1954
        if (must_add_seg) {
            if (override < 0) {
1955
                if (base == R_EBP || base == R_ESP) {
B
bellard 已提交
1956
                    override = R_SS;
1957
                } else {
B
bellard 已提交
1958
                    override = R_DS;
1959
                }
B
bellard 已提交
1960
            }
1961 1962 1963 1964

            tcg_gen_ld_tl(cpu_tmp0, cpu_env,
                          offsetof(CPUX86State, segs[override].base));
            if (CODE64(s)) {
1965
                if (s->aflag == MO_32) {
1966 1967 1968
                    tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
                }
                tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
1969
                return;
B
bellard 已提交
1970
            }
1971 1972 1973 1974

            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
        }

1975
        if (s->aflag == MO_32) {
1976
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
1977
        }
1978 1979 1980
        break;

    case MO_16:
B
bellard 已提交
1981 1982 1983
        switch (mod) {
        case 0:
            if (rm == 6) {
1984
                disp = cpu_lduw_code(env, s->pc);
B
bellard 已提交
1985
                s->pc += 2;
1986
                tcg_gen_movi_tl(cpu_A0, disp);
B
bellard 已提交
1987 1988 1989 1990 1991 1992 1993
                rm = 0; /* avoid SS override */
                goto no_rm;
            } else {
                disp = 0;
            }
            break;
        case 1:
1994
            disp = (int8_t)cpu_ldub_code(env, s->pc++);
B
bellard 已提交
1995 1996 1997
            break;
        default:
        case 2:
1998
            disp = (int16_t)cpu_lduw_code(env, s->pc);
B
bellard 已提交
1999 2000 2001
            s->pc += 2;
            break;
        }
2002 2003 2004

        sum = cpu_A0;
        switch (rm) {
B
bellard 已提交
2005
        case 0:
2006
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBX], cpu_regs[R_ESI]);
B
bellard 已提交
2007 2008
            break;
        case 1:
2009
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBX], cpu_regs[R_EDI]);
B
bellard 已提交
2010 2011
            break;
        case 2:
2012
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBP], cpu_regs[R_ESI]);
B
bellard 已提交
2013 2014
            break;
        case 3:
2015
            tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBP], cpu_regs[R_EDI]);
B
bellard 已提交
2016 2017
            break;
        case 4:
2018
            sum = cpu_regs[R_ESI];
B
bellard 已提交
2019 2020
            break;
        case 5:
2021
            sum = cpu_regs[R_EDI];
B
bellard 已提交
2022 2023
            break;
        case 6:
2024
            sum = cpu_regs[R_EBP];
B
bellard 已提交
2025 2026 2027
            break;
        default:
        case 7:
2028
            sum = cpu_regs[R_EBX];
B
bellard 已提交
2029 2030
            break;
        }
2031
        tcg_gen_addi_tl(cpu_A0, sum, disp);
2032
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2033 2034 2035
    no_rm:
        if (must_add_seg) {
            if (override < 0) {
2036
                if (rm == 2 || rm == 3 || rm == 6) {
B
bellard 已提交
2037
                    override = R_SS;
2038
                } else {
B
bellard 已提交
2039
                    override = R_DS;
2040
                }
B
bellard 已提交
2041
            }
2042
            gen_op_addl_A0_seg(s, override);
B
bellard 已提交
2043
        }
2044 2045 2046 2047
        break;

    default:
        tcg_abort();
B
bellard 已提交
2048 2049 2050
    }
}

2051
static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
B
bellard 已提交
2052 2053 2054 2055 2056 2057 2058 2059
{
    int mod, rm, base, code;

    mod = (modrm >> 6) & 3;
    if (mod == 3)
        return;
    rm = modrm & 7;

2060 2061 2062
    switch (s->aflag) {
    case MO_64:
    case MO_32:
B
bellard 已提交
2063
        base = rm;
2064

B
bellard 已提交
2065
        if (base == 4) {
2066
            code = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
2067 2068
            base = (code & 7);
        }
2069

B
bellard 已提交
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
        switch (mod) {
        case 0:
            if (base == 5) {
                s->pc += 4;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 4;
            break;
        }
2084 2085 2086
        break;

    case MO_16:
B
bellard 已提交
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
        switch (mod) {
        case 0:
            if (rm == 6) {
                s->pc += 2;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 2;
            break;
        }
2101 2102 2103 2104
        break;

    default:
        tcg_abort();
B
bellard 已提交
2105 2106 2107
    }
}

B
bellard 已提交
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
/* used for LEA and MOV AX, mem */
static void gen_add_A0_ds_seg(DisasContext *s)
{
    int override, must_add_seg;
    must_add_seg = s->addseg;
    override = R_DS;
    if (s->override >= 0) {
        override = s->override;
        must_add_seg = 1;
    }
    if (must_add_seg) {
2119 2120
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2121
            gen_op_addq_A0_seg(override);
2122
        } else
2123 2124
#endif
        {
2125
            gen_op_addl_A0_seg(s, override);
2126
        }
B
bellard 已提交
2127 2128 2129
    }
}

B
balrog 已提交
2130
/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
B
bellard 已提交
2131
   OR_TMP0 */
2132
static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
2133
                           TCGMemOp ot, int reg, int is_store)
B
bellard 已提交
2134
{
2135
    int mod, rm;
B
bellard 已提交
2136 2137

    mod = (modrm >> 6) & 3;
B
bellard 已提交
2138
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2139 2140 2141
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
2142
                gen_op_mov_v_reg(ot, cpu_T[0], reg);
2143
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
2144
        } else {
2145
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
2146
            if (reg != OR_TMP0)
2147
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
2148 2149
        }
    } else {
2150
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
2151 2152
        if (is_store) {
            if (reg != OR_TMP0)
2153
                gen_op_mov_v_reg(ot, cpu_T[0], reg);
2154
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2155
        } else {
2156
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2157
            if (reg != OR_TMP0)
2158
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
2159 2160 2161 2162
        }
    }
}

2163
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2164 2165 2166
{
    uint32_t ret;

2167
    switch (ot) {
2168
    case MO_8:
2169
        ret = cpu_ldub_code(env, s->pc);
B
bellard 已提交
2170 2171
        s->pc++;
        break;
2172
    case MO_16:
2173
        ret = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2174 2175
        s->pc += 2;
        break;
2176
    case MO_32:
2177 2178 2179
#ifdef TARGET_X86_64
    case MO_64:
#endif
2180
        ret = cpu_ldl_code(env, s->pc);
B
bellard 已提交
2181 2182
        s->pc += 4;
        break;
2183 2184
    default:
        tcg_abort();
B
bellard 已提交
2185 2186 2187 2188
    }
    return ret;
}

2189
static inline int insn_const_size(TCGMemOp ot)
B
bellard 已提交
2190
{
2191
    if (ot <= MO_32) {
B
bellard 已提交
2192
        return 1 << ot;
2193
    } else {
B
bellard 已提交
2194
        return 4;
2195
    }
B
bellard 已提交
2196 2197
}

2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip)
{
    TranslationBlock *tb;
    target_ulong pc;

    pc = s->cs_base + eip;
    tb = s->tb;
    /* NOTE: we handle the case where the TB spans two pages here */
    if ((pc & TARGET_PAGE_MASK) == (tb->pc & TARGET_PAGE_MASK) ||
        (pc & TARGET_PAGE_MASK) == ((s->pc - 1) & TARGET_PAGE_MASK))  {
        /* jump to same page: we can use a direct jump */
B
bellard 已提交
2209
        tcg_gen_goto_tb(tb_num);
2210
        gen_jmp_im(eip);
2211
        tcg_gen_exit_tb((uintptr_t)tb + tb_num);
2212 2213 2214 2215 2216 2217 2218
    } else {
        /* jump to another page: currently not optimized */
        gen_jmp_im(eip);
        gen_eob(s);
    }
}

2219
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2220
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2221
{
2222
    int l1, l2;
2223

B
bellard 已提交
2224
    if (s->jmp_opt) {
B
bellard 已提交
2225
        l1 = gen_new_label();
2226
        gen_jcc1(s, b, l1);
2227

2228
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2229 2230

        gen_set_label(l1);
2231
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2232
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2233
    } else {
B
bellard 已提交
2234 2235
        l1 = gen_new_label();
        l2 = gen_new_label();
2236
        gen_jcc1(s, b, l1);
2237

B
bellard 已提交
2238
        gen_jmp_im(next_eip);
2239 2240
        tcg_gen_br(l2);

B
bellard 已提交
2241 2242 2243
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2244 2245 2246 2247
        gen_eob(s);
    }
}

2248
static void gen_cmovcc1(CPUX86State *env, DisasContext *s, TCGMemOp ot, int b,
2249 2250
                        int modrm, int reg)
{
2251
    CCPrepare cc;
2252

2253
    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2254

2255 2256 2257 2258 2259 2260 2261 2262
    cc = gen_prepare_cc(s, b, cpu_T[1]);
    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);
2263 2264
    }

2265 2266
    tcg_gen_movcond_tl(cc.cond, cpu_T[0], cc.reg, cc.reg2,
                       cpu_T[0], cpu_regs[reg]);
2267
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
2268 2269 2270 2271 2272 2273 2274

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

2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
static inline void gen_op_movl_T0_seg(int seg_reg)
{
    tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                     offsetof(CPUX86State,segs[seg_reg].selector));
}

static inline void gen_op_movl_seg_T0_vm(int seg_reg)
{
    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffff);
    tcg_gen_st32_tl(cpu_T[0], cpu_env, 
                    offsetof(CPUX86State,segs[seg_reg].selector));
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], 4);
    tcg_gen_st_tl(cpu_T[0], cpu_env, 
                  offsetof(CPUX86State,segs[seg_reg].base));
}

B
bellard 已提交
2293 2294
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
2295
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
2296
{
2297 2298
    if (s->pe && !s->vm86) {
        /* XXX: optimize by finding processor state dynamically */
2299
        gen_update_cc_op(s);
B
bellard 已提交
2300
        gen_jmp_im(cur_eip);
2301
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2302
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2303 2304 2305 2306 2307
        /* 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 */
        if (seg_reg == R_SS || (s->code32 && seg_reg < R_FS))
J
Jun Koi 已提交
2308
            s->is_jmp = DISAS_TB_JUMP;
2309
    } else {
2310
        gen_op_movl_seg_T0_vm(seg_reg);
B
bellard 已提交
2311
        if (seg_reg == R_SS)
J
Jun Koi 已提交
2312
            s->is_jmp = DISAS_TB_JUMP;
2313
    }
B
bellard 已提交
2314 2315
}

T
ths 已提交
2316 2317 2318 2319 2320
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

B
bellard 已提交
2321
static inline void
T
ths 已提交
2322
gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2323
                              uint32_t type, uint64_t param)
T
ths 已提交
2324
{
B
bellard 已提交
2325 2326 2327
    /* no SVM activated; fast case */
    if (likely(!(s->flags & HF_SVMI_MASK)))
        return;
2328
    gen_update_cc_op(s);
B
bellard 已提交
2329
    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
2330
    gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
P
pbrook 已提交
2331
                                         tcg_const_i64(param));
T
ths 已提交
2332 2333
}

B
bellard 已提交
2334
static inline void
T
ths 已提交
2335 2336
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2337
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2338 2339
}

2340 2341
static inline void gen_stack_update(DisasContext *s, int addend)
{
B
bellard 已提交
2342 2343
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2344
        gen_op_add_reg_im(MO_64, R_ESP, addend);
B
bellard 已提交
2345 2346
    } else
#endif
2347
    if (s->ss32) {
2348
        gen_op_add_reg_im(MO_32, R_ESP, addend);
2349
    } else {
2350
        gen_op_add_reg_im(MO_16, R_ESP, addend);
2351 2352 2353
    }
}

2354 2355
/* Generate a push. It depends on ss32, addseg and dflag.  */
static void gen_push_v(DisasContext *s, TCGv val)
B
bellard 已提交
2356
{
2357 2358 2359 2360 2361
    TCGMemOp a_ot, d_ot = mo_pushpop(s, s->dflag);
    int size = 1 << d_ot;
    TCGv new_esp = cpu_A0;

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

B
bellard 已提交
2363
    if (CODE64(s)) {
2364 2365 2366 2367 2368 2369
        a_ot = MO_64;
    } else if (s->ss32) {
        a_ot = MO_32;
        if (s->addseg) {
            new_esp = cpu_tmp4;
            tcg_gen_mov_tl(new_esp, cpu_A0);
2370
            gen_op_addl_A0_seg(s, R_SS);
2371 2372
        } else {
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2373
        }
2374 2375 2376 2377 2378 2379
    } else {
        a_ot = MO_16;
        new_esp = cpu_tmp4;
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
        tcg_gen_mov_tl(new_esp, cpu_A0);
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2380
    }
2381 2382 2383

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

2386
/* two step pop is necessary for precise exceptions */
2387
static TCGMemOp gen_pop_T0(DisasContext *s)
B
bellard 已提交
2388
{
2389 2390 2391
    TCGMemOp d_ot = mo_pushpop(s, s->dflag);
    TCGv addr = cpu_A0;

B
bellard 已提交
2392
    if (CODE64(s)) {
2393 2394 2395 2396 2397 2398 2399 2400 2401
        addr = cpu_regs[R_ESP];
    } else if (!s->ss32) {
        tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_ESP]);
        gen_op_addl_A0_seg(s, R_SS);
    } else if (s->addseg) {
        tcg_gen_mov_tl(cpu_A0, cpu_regs[R_ESP]);
        gen_op_addl_A0_seg(s, R_SS);
    } else {
        tcg_gen_ext32u_tl(cpu_A0, cpu_regs[R_ESP]);
B
bellard 已提交
2402
    }
2403 2404 2405

    gen_op_ld_v(s, d_ot, cpu_T[0], addr);
    return d_ot;
B
bellard 已提交
2406 2407
}

2408
static void gen_pop_update(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2409
{
2410
    gen_stack_update(s, 1 << ot);
B
bellard 已提交
2411 2412 2413 2414
}

static void gen_stack_A0(DisasContext *s)
{
B
bellard 已提交
2415
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2416
    if (!s->ss32)
2417
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2418
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2419
    if (s->addseg)
2420
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2421 2422 2423 2424 2425 2426
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_pusha(DisasContext *s)
{
    int i;
B
bellard 已提交
2427
    gen_op_movl_A0_reg(R_ESP);
2428
    gen_op_addl_A0_im(-8 << s->dflag);
B
bellard 已提交
2429
    if (!s->ss32)
2430
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2431
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2432
    if (s->addseg)
2433
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2434
    for(i = 0;i < 8; i++) {
2435
        gen_op_mov_v_reg(MO_32, cpu_T[0], 7 - i);
2436 2437
        gen_op_st_v(s, s->dflag, cpu_T[0], cpu_A0);
        gen_op_addl_A0_im(1 << s->dflag);
B
bellard 已提交
2438
    }
2439
    gen_op_mov_reg_v(MO_16 + s->ss32, R_ESP, cpu_T[1]);
B
bellard 已提交
2440 2441 2442 2443 2444 2445
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_popa(DisasContext *s)
{
    int i;
B
bellard 已提交
2446
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2447
    if (!s->ss32)
2448
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2449
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2450
    tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 8 << s->dflag);
B
bellard 已提交
2451
    if (s->addseg)
2452
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2453 2454 2455
    for(i = 0;i < 8; i++) {
        /* ESP is not reloaded */
        if (i != 3) {
2456
            gen_op_ld_v(s, s->dflag, cpu_T[0], cpu_A0);
2457
            gen_op_mov_reg_v(s->dflag, 7 - i, cpu_T[0]);
B
bellard 已提交
2458
        }
2459
        gen_op_addl_A0_im(1 << s->dflag);
B
bellard 已提交
2460
    }
2461
    gen_op_mov_reg_v(MO_16 + s->ss32, R_ESP, cpu_T[1]);
B
bellard 已提交
2462 2463 2464 2465
}

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
2466 2467
    TCGMemOp ot = mo_pushpop(s, s->dflag);
    int opsize = 1 << ot;
B
bellard 已提交
2468 2469

    level &= 0x1f;
2470 2471
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2472
        gen_op_movl_A0_reg(R_ESP);
2473
        gen_op_addq_A0_im(-opsize);
2474
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2475 2476

        /* push bp */
2477
        gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
2478
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2479
        if (level) {
B
bellard 已提交
2480
            /* XXX: must save state */
2481
            gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
2482
                                     tcg_const_i32((ot == MO_64)),
P
pbrook 已提交
2483
                                     cpu_T[1]);
2484
        }
2485
        gen_op_mov_reg_v(ot, R_EBP, cpu_T[1]);
2486
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2487
        gen_op_mov_reg_v(MO_64, R_ESP, cpu_T[1]);
2488
    } else
2489 2490
#endif
    {
B
bellard 已提交
2491
        gen_op_movl_A0_reg(R_ESP);
2492 2493
        gen_op_addl_A0_im(-opsize);
        if (!s->ss32)
2494
            tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2495
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2496
        if (s->addseg)
2497
            gen_op_addl_A0_seg(s, R_SS);
2498
        /* push bp */
2499
        gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
2500
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2501
        if (level) {
B
bellard 已提交
2502
            /* XXX: must save state */
2503
            gen_helper_enter_level(cpu_env, tcg_const_i32(level),
2504
                                   tcg_const_i32(s->dflag - 1),
P
pbrook 已提交
2505
                                   cpu_T[1]);
2506
        }
2507
        gen_op_mov_reg_v(ot, R_EBP, cpu_T[1]);
2508
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2509
        gen_op_mov_reg_v(MO_16 + s->ss32, R_ESP, cpu_T[1]);
B
bellard 已提交
2510 2511 2512
    }
}

B
bellard 已提交
2513
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2514
{
2515
    gen_update_cc_op(s);
B
bellard 已提交
2516
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2517
    gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
J
Jun Koi 已提交
2518
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2519 2520 2521
}

/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2522
   privilege checks */
2523
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2524
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2525
{
2526
    gen_update_cc_op(s);
B
bellard 已提交
2527
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2528
    gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
P
pbrook 已提交
2529
                               tcg_const_i32(next_eip - cur_eip));
J
Jun Koi 已提交
2530
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2531 2532
}

B
bellard 已提交
2533
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2534
{
2535
    gen_update_cc_op(s);
B
bellard 已提交
2536
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2537
    gen_helper_debug(cpu_env);
J
Jun Koi 已提交
2538
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2539 2540 2541 2542 2543 2544
}

/* generate a generic end of block. Trace exception is also generated
   if needed */
static void gen_eob(DisasContext *s)
{
2545
    gen_update_cc_op(s);
2546
    if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
2547
        gen_helper_reset_inhibit_irq(cpu_env);
2548
    }
J
Jan Kiszka 已提交
2549
    if (s->tb->flags & HF_RF_MASK) {
2550
        gen_helper_reset_rf(cpu_env);
J
Jan Kiszka 已提交
2551
    }
2552
    if (s->singlestep_enabled) {
B
Blue Swirl 已提交
2553
        gen_helper_debug(cpu_env);
2554
    } else if (s->tf) {
B
Blue Swirl 已提交
2555
        gen_helper_single_step(cpu_env);
B
bellard 已提交
2556
    } else {
B
bellard 已提交
2557
        tcg_gen_exit_tb(0);
B
bellard 已提交
2558
    }
J
Jun Koi 已提交
2559
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2560 2561 2562 2563
}

/* generate a jump to eip. No segment change must happen before as a
   direct call to the next block may occur */
B
bellard 已提交
2564
static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
B
bellard 已提交
2565
{
2566 2567
    gen_update_cc_op(s);
    set_cc_op(s, CC_OP_DYNAMIC);
B
bellard 已提交
2568
    if (s->jmp_opt) {
2569
        gen_goto_tb(s, tb_num, eip);
J
Jun Koi 已提交
2570
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2571
    } else {
B
bellard 已提交
2572
        gen_jmp_im(eip);
B
bellard 已提交
2573 2574 2575 2576
        gen_eob(s);
    }
}

B
bellard 已提交
2577 2578 2579 2580 2581
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

2582
static inline void gen_ldq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2583
{
2584
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
2585
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2586
}
B
bellard 已提交
2587

2588
static inline void gen_stq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2589
{
2590
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2591
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
bellard 已提交
2592
}
B
bellard 已提交
2593

2594
static inline void gen_ldo_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2595
{
2596
    int mem_index = s->mem_index;
2597
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
2598
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
B
bellard 已提交
2599
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2600
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
2601
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
B
bellard 已提交
2602
}
B
bellard 已提交
2603

2604
static inline void gen_sto_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2605
{
2606
    int mem_index = s->mem_index;
2607
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
2608
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
B
bellard 已提交
2609
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2610
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
2611
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
B
bellard 已提交
2612
}
B
bellard 已提交
2613

B
bellard 已提交
2614 2615
static inline void gen_op_movo(int d_offset, int s_offset)
{
2616 2617 2618 2619
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset + 8);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset + 8);
B
bellard 已提交
2620 2621 2622 2623
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2624 2625
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2626 2627 2628 2629
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2630 2631
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2632 2633 2634 2635
}

static inline void gen_op_movq_env_0(int d_offset)
{
2636 2637
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2638
}
B
bellard 已提交
2639

B
Blue Swirl 已提交
2640 2641 2642 2643 2644 2645 2646
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 已提交
2647
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2648 2649
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2650

B
bellard 已提交
2651 2652
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2653

P
pbrook 已提交
2654 2655 2656
#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 已提交
2657

B
Blue Swirl 已提交
2658
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2659 2660 2661
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2662 2663 2664
    /* 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 已提交
2665
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2666
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2667 2668
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2669 2670 2671 2672 2673 2674
    [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 */
2675
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2676 2677
    [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 已提交
2678 2679
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2680 2681
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2682 2683 2684 2685 2686 2687
    [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 已提交
2688 2689
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2690 2691 2692
    [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 已提交
2693 2694 2695 2696 2697 2698
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

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

R
Richard Henderson 已提交
2702 2703 2704
    /* 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 已提交
2705

B
bellard 已提交
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
    /* 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 已提交
2719 2720
    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2721 2722
    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
B
Blue Swirl 已提交
2723 2724 2725 2726
    [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 已提交
2727 2728 2729 2730 2731 2732
    [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 已提交
2733
    [0x77] = { SSE_DUMMY }, /* emms */
2734 2735
    [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
    [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
P
pbrook 已提交
2736 2737
    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
B
bellard 已提交
2738 2739 2740 2741
    [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 已提交
2742
    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
B
bellard 已提交
2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763
    [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 已提交
2764
    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
B
bellard 已提交
2765 2766 2767 2768 2769 2770 2771 2772 2773
    [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 已提交
2774
    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
B
bellard 已提交
2775 2776 2777 2778 2779 2780
    [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 已提交
2781 2782
    [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
               (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
B
bellard 已提交
2783 2784 2785 2786 2787 2788 2789 2790 2791
    [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 已提交
2792
static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
B
bellard 已提交
2793 2794 2795 2796 2797 2798 2799
    [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 已提交
2800
    [16 + 3] = { NULL, gen_helper_psrldq_xmm },
B
bellard 已提交
2801
    [16 + 6] = MMX_OP2(psllq),
P
pbrook 已提交
2802
    [16 + 7] = { NULL, gen_helper_pslldq_xmm },
B
bellard 已提交
2803 2804
};

B
Blue Swirl 已提交
2805
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
2806
    gen_helper_cvtsi2ss,
2807
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
2808
};
P
pbrook 已提交
2809

2810
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2811
static const SSEFunc_0_epl sse_op_table3aq[] = {
2812 2813 2814 2815 2816
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
2817
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
2818 2819
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
2820
    gen_helper_cvttsd2si,
2821
    gen_helper_cvtsd2si
B
bellard 已提交
2822
};
2823

2824
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2825
static const SSEFunc_l_ep sse_op_table3bq[] = {
2826 2827
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
2828
    gen_helper_cvttsd2sq,
2829 2830 2831 2832
    gen_helper_cvtsd2sq
};
#endif

B
Blue Swirl 已提交
2833
static const SSEFunc_0_epp sse_op_table4[8][4] = {
B
bellard 已提交
2834 2835 2836 2837 2838 2839 2840 2841 2842
    SSE_FOP(cmpeq),
    SSE_FOP(cmplt),
    SSE_FOP(cmple),
    SSE_FOP(cmpunord),
    SSE_FOP(cmpneq),
    SSE_FOP(cmpnlt),
    SSE_FOP(cmpnle),
    SSE_FOP(cmpord),
};
2843

B
Blue Swirl 已提交
2844
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868
    [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 已提交
2869 2870
};

B
Blue Swirl 已提交
2871 2872
struct SSEOpHelper_epp {
    SSEFunc_0_epp op[2];
B
Blue Swirl 已提交
2873 2874 2875
    uint32_t ext_mask;
};

B
Blue Swirl 已提交
2876 2877
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
2878
    uint32_t ext_mask;
B
balrog 已提交
2879
};
B
Blue Swirl 已提交
2880

B
balrog 已提交
2881
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
2882 2883
#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 已提交
2884
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2885 2886
#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
        CPUID_EXT_PCLMULQDQ }
2887
#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
B
Blue Swirl 已提交
2888

B
Blue Swirl 已提交
2889
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935
    [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),
2936 2937 2938 2939 2940
    [0xdb] = AESNI_OP(aesimc),
    [0xdc] = AESNI_OP(aesenc),
    [0xdd] = AESNI_OP(aesenclast),
    [0xde] = AESNI_OP(aesdec),
    [0xdf] = AESNI_OP(aesdeclast),
B
balrog 已提交
2941 2942
};

B
Blue Swirl 已提交
2943
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
    [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),
2962
    [0x44] = PCLMULQDQ_OP(pclmulqdq),
B
balrog 已提交
2963 2964 2965 2966
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
2967
    [0xdf] = AESNI_OP(aeskeygenassist),
B
balrog 已提交
2968 2969
};

2970 2971
static void gen_sse(CPUX86State *env, DisasContext *s, int b,
                    target_ulong pc_start, int rex_r)
B
bellard 已提交
2972
{
2973
    int b1, op1_offset, op2_offset, is_xmm, val;
2974
    int modrm, mod, rm, reg;
B
Blue Swirl 已提交
2975 2976
    SSEFunc_0_epp sse_fn_epp;
    SSEFunc_0_eppi sse_fn_eppi;
B
Blue Swirl 已提交
2977
    SSEFunc_0_ppi sse_fn_ppi;
B
Blue Swirl 已提交
2978
    SSEFunc_0_eppt sse_fn_eppt;
2979
    TCGMemOp ot;
B
bellard 已提交
2980 2981

    b &= 0xff;
2982
    if (s->prefix & PREFIX_DATA)
B
bellard 已提交
2983
        b1 = 1;
2984
    else if (s->prefix & PREFIX_REPZ)
B
bellard 已提交
2985
        b1 = 2;
2986
    else if (s->prefix & PREFIX_REPNZ)
B
bellard 已提交
2987 2988 2989
        b1 = 3;
    else
        b1 = 0;
B
Blue Swirl 已提交
2990 2991
    sse_fn_epp = sse_op_table1[b][b1];
    if (!sse_fn_epp) {
B
bellard 已提交
2992
        goto illegal_op;
B
Blue Swirl 已提交
2993
    }
A
aurel32 已提交
2994
    if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
B
bellard 已提交
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
        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:
        gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
        return;
    }
    if (is_xmm && !(s->flags & HF_OSFXSR_MASK))
B
balrog 已提交
3015 3016
        if ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))
            goto illegal_op;
3017 3018 3019 3020
    if (b == 0x0e) {
        if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
            goto illegal_op;
        /* femms */
B
Blue Swirl 已提交
3021
        gen_helper_emms(cpu_env);
3022 3023 3024 3025
        return;
    }
    if (b == 0x77) {
        /* emms */
B
Blue Swirl 已提交
3026
        gen_helper_emms(cpu_env);
B
bellard 已提交
3027 3028 3029 3030 3031
        return;
    }
    /* prepare MMX state (XXX: optimize by storing fptt and fptags in
       the static cpu state) */
    if (!is_xmm) {
B
Blue Swirl 已提交
3032
        gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3033 3034
    }

3035
    modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3036 3037 3038 3039
    reg = ((modrm >> 3) & 7);
    if (is_xmm)
        reg |= rex_r;
    mod = (modrm >> 6) & 3;
B
Blue Swirl 已提交
3040
    if (sse_fn_epp == SSE_SPECIAL) {
B
bellard 已提交
3041 3042 3043
        b |= (b1 << 8);
        switch(b) {
        case 0x0e7: /* movntq */
3044
            if (mod == 3)
B
bellard 已提交
3045
                goto illegal_op;
3046
            gen_lea_modrm(env, s, modrm);
3047
            gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3048 3049 3050 3051
            break;
        case 0x1e7: /* movntdq */
        case 0x02b: /* movntps */
        case 0x12b: /* movntps */
3052 3053
            if (mod == 3)
                goto illegal_op;
3054
            gen_lea_modrm(env, s, modrm);
3055
            gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3056
            break;
B
bellard 已提交
3057 3058
        case 0x3f0: /* lddqu */
            if (mod == 3)
B
bellard 已提交
3059
                goto illegal_op;
3060
            gen_lea_modrm(env, s, modrm);
3061
            gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3062
            break;
3063 3064 3065 3066
        case 0x22b: /* movntss */
        case 0x32b: /* movntsd */
            if (mod == 3)
                goto illegal_op;
3067
            gen_lea_modrm(env, s, modrm);
3068
            if (b1 & 1) {
3069
                gen_stq_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3070 3071 3072
            } else {
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                    xmm_regs[reg].XMM_L(0)));
3073
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
3074 3075
            }
            break;
B
bellard 已提交
3076
        case 0x6e: /* movd mm, ea */
B
bellard 已提交
3077
#ifdef TARGET_X86_64
3078
            if (s->dflag == MO_64) {
3079
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
B
bellard 已提交
3080
                tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
3081
            } else
B
bellard 已提交
3082 3083
#endif
            {
3084
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3085 3086
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx));
P
pbrook 已提交
3087 3088
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3089
            }
B
bellard 已提交
3090 3091
            break;
        case 0x16e: /* movd xmm, ea */
B
bellard 已提交
3092
#ifdef TARGET_X86_64
3093
            if (s->dflag == MO_64) {
3094
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
B
bellard 已提交
3095 3096
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
P
pbrook 已提交
3097
                gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T[0]);
3098
            } else
B
bellard 已提交
3099 3100
#endif
            {
3101
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
B
bellard 已提交
3102 3103
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg]));
3104
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
3105
                gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3106
            }
B
bellard 已提交
3107 3108 3109
            break;
        case 0x6f: /* movq mm, ea */
            if (mod != 3) {
3110
                gen_lea_modrm(env, s, modrm);
3111
                gen_ldq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3112 3113
            } else {
                rm = (modrm & 7);
3114
                tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3115
                               offsetof(CPUX86State,fpregs[rm].mmx));
3116
                tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
B
bellard 已提交
3117
                               offsetof(CPUX86State,fpregs[reg].mmx));
B
bellard 已提交
3118 3119 3120 3121 3122 3123 3124 3125 3126
            }
            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) {
3127
                gen_lea_modrm(env, s, modrm);
3128
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3129 3130 3131 3132 3133 3134 3135 3136
            } 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) {
3137
                gen_lea_modrm(env, s, modrm);
3138
                gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3139
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3140
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3141 3142 3143
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
B
bellard 已提交
3144 3145 3146 3147 3148 3149 3150 3151
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
            }
            break;
        case 0x310: /* movsd xmm, ea */
            if (mod != 3) {
3152
                gen_lea_modrm(env, s, modrm);
3153 3154
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
3155
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3156 3157
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
B
bellard 已提交
3158 3159 3160 3161 3162 3163 3164 3165 3166
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
            }
            break;
        case 0x012: /* movlps */
        case 0x112: /* movlpd */
            if (mod != 3) {
3167
                gen_lea_modrm(env, s, modrm);
3168 3169
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3170 3171 3172 3173 3174 3175 3176
            } else {
                /* movhlps */
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
            }
            break;
B
bellard 已提交
3177 3178
        case 0x212: /* movsldup */
            if (mod != 3) {
3179
                gen_lea_modrm(env, s, modrm);
3180
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(2)));
            }
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
                        offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
                        offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
            break;
        case 0x312: /* movddup */
            if (mod != 3) {
3195
                gen_lea_modrm(env, s, modrm);
3196 3197
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3198 3199 3200 3201 3202 3203
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
            }
            gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
B
bellard 已提交
3204
                        offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3205
            break;
B
bellard 已提交
3206 3207 3208
        case 0x016: /* movhps */
        case 0x116: /* movhpd */
            if (mod != 3) {
3209
                gen_lea_modrm(env, s, modrm);
3210 3211
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3212 3213 3214 3215 3216 3217 3218 3219 3220
            } else {
                /* movlhps */
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
            }
            break;
        case 0x216: /* movshdup */
            if (mod != 3) {
3221
                gen_lea_modrm(env, s, modrm);
3222
                gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(1)));
                gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_L(3)));
            }
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
                        offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
            gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
                        offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
            break;
3235 3236 3237 3238 3239 3240 3241
        case 0x178:
        case 0x378:
            {
                int bit_index, field_length;

                if (b1 == 1 && reg != 0)
                    goto illegal_op;
3242 3243
                field_length = cpu_ldub_code(env, s->pc++) & 0x3F;
                bit_index = cpu_ldub_code(env, s->pc++) & 0x3F;
3244 3245 3246
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
                    offsetof(CPUX86State,xmm_regs[reg]));
                if (b1 == 1)
B
Blue Swirl 已提交
3247 3248 3249
                    gen_helper_extrq_i(cpu_env, cpu_ptr0,
                                       tcg_const_i32(bit_index),
                                       tcg_const_i32(field_length));
3250
                else
B
Blue Swirl 已提交
3251 3252 3253
                    gen_helper_insertq_i(cpu_env, cpu_ptr0,
                                         tcg_const_i32(bit_index),
                                         tcg_const_i32(field_length));
3254 3255
            }
            break;
B
bellard 已提交
3256
        case 0x7e: /* movd ea, mm */
B
bellard 已提交
3257
#ifdef TARGET_X86_64
3258
            if (s->dflag == MO_64) {
B
bellard 已提交
3259 3260
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,fpregs[reg].mmx));
3261
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3262
            } else
B
bellard 已提交
3263 3264
#endif
            {
B
bellard 已提交
3265 3266
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
3267
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
B
bellard 已提交
3268
            }
B
bellard 已提交
3269 3270
            break;
        case 0x17e: /* movd ea, xmm */
B
bellard 已提交
3271
#ifdef TARGET_X86_64
3272
            if (s->dflag == MO_64) {
B
bellard 已提交
3273 3274
                tcg_gen_ld_i64(cpu_T[0], cpu_env, 
                               offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3275
                gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3276
            } else
B
bellard 已提交
3277 3278
#endif
            {
B
bellard 已提交
3279 3280
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, 
                                 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3281
                gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
B
bellard 已提交
3282
            }
B
bellard 已提交
3283 3284 3285
            break;
        case 0x27e: /* movq xmm, ea */
            if (mod != 3) {
3286
                gen_lea_modrm(env, s, modrm);
3287 3288
                gen_ldq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3289 3290 3291 3292 3293 3294 3295 3296 3297
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
            }
            gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
            break;
        case 0x7f: /* movq ea, mm */
            if (mod != 3) {
3298
                gen_lea_modrm(env, s, modrm);
3299
                gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
B
bellard 已提交
3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312
            } 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) {
3313
                gen_lea_modrm(env, s, modrm);
3314
                gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
B
bellard 已提交
3315 3316 3317 3318 3319 3320 3321 3322
            } 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) {
3323
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3324
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3325
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3326 3327 3328 3329 3330 3331 3332 3333
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movl(offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)),
                            offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
            }
            break;
        case 0x311: /* movsd ea, xmm */
            if (mod != 3) {
3334
                gen_lea_modrm(env, s, modrm);
3335 3336
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3337 3338 3339 3340 3341 3342 3343 3344 3345
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
            }
            break;
        case 0x013: /* movlps */
        case 0x113: /* movlpd */
            if (mod != 3) {
3346
                gen_lea_modrm(env, s, modrm);
3347 3348
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3349 3350 3351 3352 3353 3354 3355
            } else {
                goto illegal_op;
            }
            break;
        case 0x017: /* movhps */
        case 0x117: /* movhpd */
            if (mod != 3) {
3356
                gen_lea_modrm(env, s, modrm);
3357 3358
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368
            } else {
                goto illegal_op;
            }
            break;
        case 0x71: /* shift mm, im */
        case 0x72:
        case 0x73:
        case 0x171: /* shift xmm, im */
        case 0x172:
        case 0x173:
3369 3370 3371
            if (b1 >= 2) {
	        goto illegal_op;
            }
3372
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3373
            if (is_xmm) {
3374
                tcg_gen_movi_tl(cpu_T[0], val);
B
bellard 已提交
3375
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
3376
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3377
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(1)));
B
bellard 已提交
3378 3379
                op1_offset = offsetof(CPUX86State,xmm_t0);
            } else {
3380
                tcg_gen_movi_tl(cpu_T[0], val);
B
bellard 已提交
3381
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
3382
                tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
3383
                tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
B
bellard 已提交
3384 3385
                op1_offset = offsetof(CPUX86State,mmx_t0);
            }
B
Blue Swirl 已提交
3386 3387 3388
            sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
                                       (((modrm >> 3)) & 7)][b1];
            if (!sse_fn_epp) {
B
bellard 已提交
3389
                goto illegal_op;
B
Blue Swirl 已提交
3390
            }
B
bellard 已提交
3391 3392 3393 3394 3395 3396 3397
            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 已提交
3398 3399
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
B
Blue Swirl 已提交
3400
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3401 3402 3403
            break;
        case 0x050: /* movmskps */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3404 3405
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3406
            gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3407
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3408 3409 3410
            break;
        case 0x150: /* movmskpd */
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3411 3412
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, 
                             offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3413
            gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3414
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3415 3416 3417
            break;
        case 0x02a: /* cvtpi2ps */
        case 0x12a: /* cvtpi2pd */
B
Blue Swirl 已提交
3418
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3419
            if (mod != 3) {
3420
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3421
                op2_offset = offsetof(CPUX86State,mmx_t0);
3422
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
3423 3424 3425 3426 3427
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3428 3429
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3430 3431
            switch(b >> 8) {
            case 0x0:
B
Blue Swirl 已提交
3432
                gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3433 3434 3435
                break;
            default:
            case 0x1:
B
Blue Swirl 已提交
3436
                gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3437 3438 3439 3440 3441
                break;
            }
            break;
        case 0x22a: /* cvtsi2ss */
        case 0x32a: /* cvtsi2sd */
3442
            ot = mo_64_32(s->dflag);
3443
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
3444
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
B
bellard 已提交
3445
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3446
            if (ot == MO_32) {
B
Blue Swirl 已提交
3447
                SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
B
bellard 已提交
3448
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
B
Blue Swirl 已提交
3449
                sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
B
bellard 已提交
3450
            } else {
3451
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3452 3453
                SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
                sse_fn_epl(cpu_env, cpu_ptr0, cpu_T[0]);
3454 3455 3456
#else
                goto illegal_op;
#endif
B
bellard 已提交
3457
            }
B
bellard 已提交
3458 3459 3460 3461 3462
            break;
        case 0x02c: /* cvttps2pi */
        case 0x12c: /* cvttpd2pi */
        case 0x02d: /* cvtps2pi */
        case 0x12d: /* cvtpd2pi */
B
Blue Swirl 已提交
3463
            gen_helper_enter_mmx(cpu_env);
B
bellard 已提交
3464
            if (mod != 3) {
3465
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3466
                op2_offset = offsetof(CPUX86State,xmm_t0);
3467
                gen_ldo_env_A0(s, op2_offset);
B
bellard 已提交
3468 3469 3470 3471 3472
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
            op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
B
bellard 已提交
3473 3474
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
bellard 已提交
3475 3476
            switch(b) {
            case 0x02c:
B
Blue Swirl 已提交
3477
                gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3478 3479
                break;
            case 0x12c:
B
Blue Swirl 已提交
3480
                gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3481 3482
                break;
            case 0x02d:
B
Blue Swirl 已提交
3483
                gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3484 3485
                break;
            case 0x12d:
B
Blue Swirl 已提交
3486
                gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
3487 3488 3489 3490 3491 3492 3493
                break;
            }
            break;
        case 0x22c: /* cvttss2si */
        case 0x32c: /* cvttsd2si */
        case 0x22d: /* cvtss2si */
        case 0x32d: /* cvtsd2si */
3494
            ot = mo_64_32(s->dflag);
B
bellard 已提交
3495
            if (mod != 3) {
3496
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
3497
                if ((b >> 8) & 1) {
3498
                    gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.XMM_Q(0)));
B
bellard 已提交
3499
                } else {
3500
                    gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
3501
                    tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
3502 3503 3504 3505 3506 3507
                }
                op2_offset = offsetof(CPUX86State,xmm_t0);
            } else {
                rm = (modrm & 7) | REX_B(s);
                op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
            }
B
bellard 已提交
3508
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3509
            if (ot == MO_32) {
B
Blue Swirl 已提交
3510
                SSEFunc_i_ep sse_fn_i_ep =
3511
                    sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3512
                sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3513
                tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
B
bellard 已提交
3514
            } else {
3515
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
3516
                SSEFunc_l_ep sse_fn_l_ep =
3517
                    sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
B
Blue Swirl 已提交
3518
                sse_fn_l_ep(cpu_T[0], cpu_env, cpu_ptr0);
3519 3520 3521
#else
                goto illegal_op;
#endif
B
bellard 已提交
3522
            }
3523
            gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
3524 3525
            break;
        case 0xc4: /* pinsrw */
3526
        case 0x1c4:
B
bellard 已提交
3527
            s->rip_offset = 1;
3528
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
3529
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3530 3531
            if (b1) {
                val &= 7;
B
bellard 已提交
3532 3533
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,xmm_regs[reg].XMM_W(val)));
B
bellard 已提交
3534 3535
            } else {
                val &= 3;
B
bellard 已提交
3536 3537
                tcg_gen_st16_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
B
bellard 已提交
3538 3539 3540
            }
            break;
        case 0xc5: /* pextrw */
3541
        case 0x1c5:
B
bellard 已提交
3542 3543
            if (mod != 3)
                goto illegal_op;
3544
            ot = mo_64_32(s->dflag);
3545
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
3546 3547 3548
            if (b1) {
                val &= 7;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3549 3550
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                 offsetof(CPUX86State,xmm_regs[rm].XMM_W(val)));
B
bellard 已提交
3551 3552 3553
            } else {
                val &= 3;
                rm = (modrm & 7);
B
bellard 已提交
3554 3555
                tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
B
bellard 已提交
3556 3557
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3558
            gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
3559 3560 3561
            break;
        case 0x1d6: /* movq ea, xmm */
            if (mod != 3) {
3562
                gen_lea_modrm(env, s, modrm);
3563 3564
                gen_stq_env_A0(s, offsetof(CPUX86State,
                                           xmm_regs[reg].XMM_Q(0)));
B
bellard 已提交
3565 3566 3567 3568 3569 3570 3571 3572
            } else {
                rm = (modrm & 7) | REX_B(s);
                gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
                            offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
                gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
            }
            break;
        case 0x2d6: /* movq2dq */
B
Blue Swirl 已提交
3573
            gen_helper_enter_mmx(cpu_env);
3574 3575 3576 3577
            rm = (modrm & 7);
            gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
                        offsetof(CPUX86State,fpregs[rm].mmx));
            gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
B
bellard 已提交
3578 3579
            break;
        case 0x3d6: /* movdq2q */
B
Blue Swirl 已提交
3580
            gen_helper_enter_mmx(cpu_env);
3581 3582 3583
            rm = (modrm & 7) | REX_B(s);
            gen_op_movq(offsetof(CPUX86State,fpregs[reg & 7].mmx),
                        offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
B
bellard 已提交
3584 3585 3586 3587 3588 3589 3590
            break;
        case 0xd7: /* pmovmskb */
        case 0x1d7:
            if (mod != 3)
                goto illegal_op;
            if (b1) {
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
3591
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
B
Blue Swirl 已提交
3592
                gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3593 3594
            } else {
                rm = (modrm & 7);
B
bellard 已提交
3595
                tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
B
Blue Swirl 已提交
3596
                gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
B
bellard 已提交
3597 3598
            }
            reg = ((modrm >> 3) & 7) | rex_r;
3599
            tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
B
bellard 已提交
3600
            break;
R
Richard Henderson 已提交
3601

B
balrog 已提交
3602
        case 0x138:
3603
        case 0x038:
B
balrog 已提交
3604
            b = modrm;
R
Richard Henderson 已提交
3605 3606 3607
            if ((b & 0xf0) == 0xf0) {
                goto do_0f_38_fx;
            }
3608
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
3609 3610 3611
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
3612 3613 3614
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
3615

B
Blue Swirl 已提交
3616 3617
            sse_fn_epp = sse_op_table6[b].op[b1];
            if (!sse_fn_epp) {
B
balrog 已提交
3618
                goto illegal_op;
B
Blue Swirl 已提交
3619
            }
B
balrog 已提交
3620 3621
            if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
                goto illegal_op;
B
balrog 已提交
3622 3623 3624 3625 3626 3627 3628

            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);
3629
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
3630 3631 3632 3633
                    switch (b) {
                    case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
                    case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
                    case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3634
                        gen_ldq_env_A0(s, op2_offset +
B
balrog 已提交
3635 3636 3637 3638
                                        offsetof(XMMReg, XMM_Q(0)));
                        break;
                    case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
                    case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3639 3640
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
balrog 已提交
3641 3642 3643 3644
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_L(0)));
                        break;
                    case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3645 3646
                        tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
                                           s->mem_index, MO_LEUW);
B
balrog 已提交
3647 3648 3649 3650
                        tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_W(0)));
                        break;
                    case 0x2a:            /* movntqda */
3651
                        gen_ldo_env_A0(s, op1_offset);
B
balrog 已提交
3652 3653
                        return;
                    default:
3654
                        gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
3655
                    }
B
balrog 已提交
3656 3657 3658 3659 3660 3661 3662
                }
            } 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);
3663
                    gen_lea_modrm(env, s, modrm);
3664
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
3665 3666
                }
            }
B
Blue Swirl 已提交
3667
            if (sse_fn_epp == SSE_SPECIAL) {
B
balrog 已提交
3668
                goto illegal_op;
B
Blue Swirl 已提交
3669
            }
B
balrog 已提交
3670

B
balrog 已提交
3671 3672
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
3673
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
balrog 已提交
3674

3675 3676 3677
            if (b == 0x17) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
balrog 已提交
3678
            break;
R
Richard Henderson 已提交
3679 3680 3681 3682 3683 3684

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

R
Richard Henderson 已提交
3688 3689 3690 3691 3692 3693 3694 3695
            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) {
3696
                    ot = MO_8;
3697
                } else if (s->dflag != MO_64) {
3698
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3699
                } else {
3700
                    ot = MO_64;
R
Richard Henderson 已提交
3701
                }
B
balrog 已提交
3702

3703
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
R
Richard Henderson 已提交
3704 3705 3706
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                gen_helper_crc32(cpu_T[0], cpu_tmp2_i32,
                                 cpu_T[0], tcg_const_i32(8 << ot));
B
balrog 已提交
3707

3708
                ot = mo_64_32(s->dflag);
3709
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3710
                break;
B
balrog 已提交
3711

R
Richard Henderson 已提交
3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725
            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;
                }
3726
                if (s->dflag != MO_64) {
3727
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3728
                } else {
3729
                    ot = MO_64;
R
Richard Henderson 已提交
3730 3731
                }

3732
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
3733
                if ((b & 1) == 0) {
3734 3735
                    tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
                                       s->mem_index, ot | MO_BE);
3736
                    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3737
                } else {
3738 3739
                    tcg_gen_qemu_st_tl(cpu_regs[reg], cpu_A0,
                                       s->mem_index, ot | MO_BE);
R
Richard Henderson 已提交
3740 3741 3742
                }
                break;

R
Richard Henderson 已提交
3743 3744 3745 3746 3747 3748
            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;
                }
3749
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3750 3751
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_andc_tl(cpu_T[0], cpu_regs[s->vex_v], cpu_T[0]);
3752
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3753 3754 3755 3756
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_LOGICB + ot);
                break;

R
Richard Henderson 已提交
3757 3758 3759 3760 3761 3762
            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;
                }
3763
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3764 3765 3766 3767 3768 3769 3770 3771 3772
                {
                    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]);
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_A0);

3773
                    bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790
                    zero = tcg_const_tl(0);
                    tcg_gen_movcond_tl(TCG_COND_LEU, cpu_T[0], cpu_A0, bound,
                                       cpu_T[0], zero);
                    tcg_temp_free(zero);

                    /* Extract the LEN into a mask.  Lengths larger than
                       operand size get all ones.  */
                    tcg_gen_shri_tl(cpu_A0, cpu_regs[s->vex_v], 8);
                    tcg_gen_ext8u_tl(cpu_A0, cpu_A0);
                    tcg_gen_movcond_tl(TCG_COND_LEU, cpu_A0, cpu_A0, bound,
                                       cpu_A0, bound);
                    tcg_temp_free(bound);
                    tcg_gen_movi_tl(cpu_T[1], 1);
                    tcg_gen_shl_tl(cpu_T[1], cpu_T[1], cpu_A0);
                    tcg_gen_subi_tl(cpu_T[1], cpu_T[1], 1);
                    tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);

3791
                    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3792 3793 3794 3795 3796
                    gen_op_update1_cc();
                    set_cc_op(s, CC_OP_LOGICB + ot);
                }
                break;

R
Richard Henderson 已提交
3797 3798 3799 3800 3801 3802
            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;
                }
3803
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3804 3805 3806
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_ext8u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                {
3807
                    TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818
                    /* 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,
                                       cpu_T[1], bound);
                    tcg_gen_movcond_tl(TCG_COND_GT, cpu_T[1], cpu_T[1],
                                       bound, bound, cpu_T[1]);
                    tcg_temp_free(bound);
                }
                tcg_gen_movi_tl(cpu_A0, -1);
                tcg_gen_shl_tl(cpu_A0, cpu_A0, cpu_T[1]);
                tcg_gen_andc_tl(cpu_T[0], cpu_T[0], cpu_A0);
3819
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
3820 3821 3822 3823
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_BMILGB + ot);
                break;

R
Richard Henderson 已提交
3824 3825 3826 3827 3828 3829
            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;
                }
3830
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
3831 3832 3833
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                switch (ot) {
                default:
3834 3835 3836 3837 3838 3839
                    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                    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 已提交
3840 3841
                    break;
#ifdef TARGET_X86_64
3842
                case MO_64:
3843 3844
                    tcg_gen_mulu2_i64(cpu_regs[s->vex_v], cpu_regs[reg],
                                      cpu_T[0], cpu_regs[R_EDX]);
R
Richard Henderson 已提交
3845 3846 3847 3848 3849
                    break;
#endif
                }
                break;

3850 3851 3852 3853 3854 3855
            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;
                }
3856
                ot = mo_64_32(s->dflag);
3857 3858 3859
                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.  */
3860
                if (ot == MO_64) {
3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873
                    tcg_gen_mov_tl(cpu_T[1], cpu_regs[s->vex_v]);
                } else {
                    tcg_gen_ext32u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                }
                gen_helper_pdep(cpu_regs[reg], cpu_T[0], cpu_T[1]);
                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;
                }
3874
                ot = mo_64_32(s->dflag);
3875 3876 3877
                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.  */
3878
                if (ot == MO_64) {
3879 3880 3881 3882 3883 3884 3885
                    tcg_gen_mov_tl(cpu_T[1], cpu_regs[s->vex_v]);
                } else {
                    tcg_gen_ext32u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                }
                gen_helper_pext(cpu_regs[reg], cpu_T[0], cpu_T[1]);
                break;

3886 3887 3888 3889 3890
            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 {
3891
                    TCGv carry_in, carry_out, zero;
3892 3893
                    int end_op;

3894
                    ot = mo_64_32(s->dflag);
3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921
                    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);

                    /* Re-use the carry-out from a previous round.  */
                    TCGV_UNUSED(carry_in);
                    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:
3922
                        end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937
                        break;
                    }
                    /* If we can't reuse carry-out, get it out of EFLAGS.  */
                    if (TCGV_IS_UNUSED(carry_in)) {
                        if (s->cc_op != CC_OP_ADCX && s->cc_op != CC_OP_ADOX) {
                            gen_compute_eflags(s);
                        }
                        carry_in = cpu_tmp0;
                        tcg_gen_shri_tl(carry_in, cpu_cc_src,
                                        ctz32(b == 0x1f6 ? CC_C : CC_O));
                        tcg_gen_andi_tl(carry_in, carry_in, 1);
                    }

                    switch (ot) {
#ifdef TARGET_X86_64
3938
                    case MO_32:
3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950
                        /* 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]);
                        tcg_gen_ext32u_i64(cpu_T[0], cpu_T[0]);
                        tcg_gen_add_i64(cpu_T[0], cpu_T[0], cpu_regs[reg]);
                        tcg_gen_add_i64(cpu_T[0], cpu_T[0], carry_in);
                        tcg_gen_ext32u_i64(cpu_regs[reg], cpu_T[0]);
                        tcg_gen_shri_i64(carry_out, cpu_T[0], 32);
                        break;
#endif
                    default:
                        /* Otherwise compute the carry-out in two steps.  */
3951 3952 3953 3954 3955 3956 3957 3958
                        zero = tcg_const_tl(0);
                        tcg_gen_add2_tl(cpu_T[0], carry_out,
                                        cpu_T[0], zero,
                                        carry_in, zero);
                        tcg_gen_add2_tl(cpu_regs[reg], carry_out,
                                        cpu_regs[reg], carry_out,
                                        cpu_T[0], zero);
                        tcg_temp_free(zero);
3959 3960 3961 3962 3963 3964
                        break;
                    }
                    set_cc_op(s, end_op);
                }
                break;

3965 3966 3967 3968 3969 3970 3971 3972
            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;
                }
3973
                ot = mo_64_32(s->dflag);
3974
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3975
                if (ot == MO_64) {
3976 3977 3978 3979 3980 3981 3982
                    tcg_gen_andi_tl(cpu_T[1], cpu_regs[s->vex_v], 63);
                } else {
                    tcg_gen_andi_tl(cpu_T[1], cpu_regs[s->vex_v], 31);
                }
                if (b == 0x1f7) {
                    tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                } else if (b == 0x2f7) {
3983
                    if (ot != MO_64) {
3984 3985 3986 3987
                        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                } else {
3988
                    if (ot != MO_64) {
3989 3990 3991 3992
                        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                }
3993
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
3994 3995
                break;

3996 3997 3998 3999 4000 4001 4002 4003 4004
            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;
                }
4005
                ot = mo_64_32(s->dflag);
4006 4007 4008 4009 4010 4011
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);

                switch (reg & 7) {
                case 1: /* blsr By,Ey */
                    tcg_gen_neg_tl(cpu_T[1], cpu_T[0]);
                    tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4012
                    gen_op_mov_reg_v(ot, s->vex_v, cpu_T[0]);
4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037
                    gen_op_update2_cc();
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

                case 2: /* blsmsk By,Ey */
                    tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
                    tcg_gen_subi_tl(cpu_T[0], cpu_T[0], 1);
                    tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_cc_src);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

                case 3: /* blsi By, Ey */
                    tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
                    tcg_gen_subi_tl(cpu_T[0], cpu_T[0], 1);
                    tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_cc_src);
                    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                    set_cc_op(s, CC_OP_BMILGB + ot);
                    break;

                default:
                    goto illegal_op;
                }
                break;

R
Richard Henderson 已提交
4038 4039 4040
            default:
                goto illegal_op;
            }
B
balrog 已提交
4041
            break;
R
Richard Henderson 已提交
4042

B
balrog 已提交
4043 4044
        case 0x03a:
        case 0x13a:
B
balrog 已提交
4045
            b = modrm;
4046
            modrm = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4047 4048 4049
            rm = modrm & 7;
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
4050 4051 4052
            if (b1 >= 2) {
                goto illegal_op;
            }
B
balrog 已提交
4053

B
Blue Swirl 已提交
4054 4055
            sse_fn_eppi = sse_op_table7[b].op[b1];
            if (!sse_fn_eppi) {
B
balrog 已提交
4056
                goto illegal_op;
B
Blue Swirl 已提交
4057
            }
B
balrog 已提交
4058 4059 4060
            if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
                goto illegal_op;

B
Blue Swirl 已提交
4061
            if (sse_fn_eppi == SSE_SPECIAL) {
4062
                ot = mo_64_32(s->dflag);
B
balrog 已提交
4063 4064
                rm = (modrm & 7) | REX_B(s);
                if (mod != 3)
4065
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
4066
                reg = ((modrm >> 3) & 7) | rex_r;
4067
                val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4068 4069 4070 4071
                switch (b) {
                case 0x14: /* pextrb */
                    tcg_gen_ld8u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_B(val & 15)));
4072
                    if (mod == 3) {
4073
                        gen_op_mov_reg_v(ot, rm, cpu_T[0]);
4074 4075 4076 4077
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_UB);
                    }
B
balrog 已提交
4078 4079 4080 4081
                    break;
                case 0x15: /* pextrw */
                    tcg_gen_ld16u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_W(val & 7)));
4082
                    if (mod == 3) {
4083
                        gen_op_mov_reg_v(ot, rm, cpu_T[0]);
4084 4085 4086 4087
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_LEUW);
                    }
B
balrog 已提交
4088 4089
                    break;
                case 0x16:
4090
                    if (ot == MO_32) { /* pextrd */
B
balrog 已提交
4091 4092 4093
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
4094
                        if (mod == 3) {
4095
                            tcg_gen_extu_i32_tl(cpu_regs[rm], cpu_tmp2_i32);
4096
                        } else {
4097 4098
                            tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4099
                        }
B
balrog 已提交
4100
                    } else { /* pextrq */
P
pbrook 已提交
4101
#ifdef TARGET_X86_64
B
balrog 已提交
4102 4103 4104
                        tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
4105
                        if (mod == 3) {
4106
                            tcg_gen_mov_i64(cpu_regs[rm], cpu_tmp1_i64);
4107 4108 4109 4110
                        } else {
                            tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
P
pbrook 已提交
4111 4112 4113
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4114 4115 4116 4117 4118
                    }
                    break;
                case 0x17: /* extractps */
                    tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
                                            xmm_regs[reg].XMM_L(val & 3)));
4119
                    if (mod == 3) {
4120
                        gen_op_mov_reg_v(ot, rm, cpu_T[0]);
4121 4122 4123 4124
                    } else {
                        tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_LEUL);
                    }
B
balrog 已提交
4125 4126
                    break;
                case 0x20: /* pinsrb */
4127
                    if (mod == 3) {
4128
                        gen_op_mov_v_reg(MO_32, cpu_T[0], rm);
4129 4130 4131 4132
                    } else {
                        tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
                                           s->mem_index, MO_UB);
                    }
4133
                    tcg_gen_st8_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
B
balrog 已提交
4134 4135 4136
                                            xmm_regs[reg].XMM_B(val & 15)));
                    break;
                case 0x21: /* insertps */
P
pbrook 已提交
4137
                    if (mod == 3) {
B
balrog 已提交
4138 4139 4140
                        tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,xmm_regs[rm]
                                                .XMM_L((val >> 6) & 3)));
P
pbrook 已提交
4141
                    } else {
4142 4143
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
P
pbrook 已提交
4144
                    }
B
balrog 已提交
4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165
                    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                    offsetof(CPUX86State,xmm_regs[reg]
                                            .XMM_L((val >> 4) & 3)));
                    if ((val >> 0) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(0)));
                    if ((val >> 1) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(1)));
                    if ((val >> 2) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(2)));
                    if ((val >> 3) & 1)
                        tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
                                        cpu_env, offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(3)));
                    break;
                case 0x22:
4166
                    if (ot == MO_32) { /* pinsrd */
4167
                        if (mod == 3) {
4168
                            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[rm]);
4169
                        } else {
4170 4171
                            tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                                s->mem_index, MO_LEUL);
4172
                        }
B
balrog 已提交
4173 4174 4175 4176
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_L(val & 3)));
                    } else { /* pinsrq */
P
pbrook 已提交
4177
#ifdef TARGET_X86_64
4178
                        if (mod == 3) {
B
balrog 已提交
4179
                            gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
4180 4181 4182 4183
                        } else {
                            tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                                s->mem_index, MO_LEQ);
                        }
B
balrog 已提交
4184 4185 4186
                        tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
                                        offsetof(CPUX86State,
                                                xmm_regs[reg].XMM_Q(val & 1)));
P
pbrook 已提交
4187 4188 4189
#else
                        goto illegal_op;
#endif
B
balrog 已提交
4190 4191 4192 4193 4194
                    }
                    break;
                }
                return;
            }
B
balrog 已提交
4195 4196 4197 4198 4199 4200 4201

            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);
4202
                    gen_lea_modrm(env, s, modrm);
4203
                    gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
4204 4205 4206 4207 4208 4209 4210
                }
            } 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);
4211
                    gen_lea_modrm(env, s, modrm);
4212
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
4213 4214
                }
            }
4215
            val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4216

B
balrog 已提交
4217
            if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4218
                set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
4219

4220
                if (s->dflag == MO_64) {
B
balrog 已提交
4221 4222
                    /* The helper must use entire 64-bit gp registers */
                    val |= 1 << 8;
4223
                }
B
balrog 已提交
4224 4225
            }

B
balrog 已提交
4226 4227
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4228
            sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4229
            break;
R
Richard Henderson 已提交
4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243

        case 0x33a:
            /* Various integer extensions at 0f 3a f[0-f].  */
            b = modrm | (b1 << 8);
            modrm = cpu_ldub_code(env, s->pc++);
            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;
                }
4244
                ot = mo_64_32(s->dflag);
R
Richard Henderson 已提交
4245 4246
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                b = cpu_ldub_code(env, s->pc++);
4247
                if (ot == MO_64) {
R
Richard Henderson 已提交
4248 4249 4250 4251 4252 4253
                    tcg_gen_rotri_tl(cpu_T[0], cpu_T[0], b & 63);
                } else {
                    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                    tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, b & 31);
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
                }
4254
                gen_op_mov_reg_v(ot, reg, cpu_T[0]);
R
Richard Henderson 已提交
4255 4256 4257 4258 4259 4260 4261
                break;

            default:
                goto illegal_op;
            }
            break;

B
bellard 已提交
4262 4263 4264 4265 4266
        default:
            goto illegal_op;
        }
    } else {
        /* generic MMX or SSE operation */
B
bellard 已提交
4267 4268 4269 4270 4271 4272 4273 4274
        switch(b) {
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
        case 0xc2: /* compare insns */
            s->rip_offset = 1;
            break;
        default:
            break;
B
bellard 已提交
4275 4276 4277 4278
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
4279 4280
                int sz = 4;

4281
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4282
                op2_offset = offsetof(CPUX86State,xmm_t0);
4283 4284 4285 4286 4287 4288

                switch (b) {
                case 0x50 ... 0x5a:
                case 0x5c ... 0x5f:
                case 0xc2:
                    /* Most sse scalar operations.  */
B
bellard 已提交
4289
                    if (b1 == 2) {
4290 4291 4292 4293 4294 4295 4296 4297 4298 4299
                        sz = 2;
                    } else if (b1 == 3) {
                        sz = 3;
                    }
                    break;

                case 0x2e:  /* ucomis[sd] */
                case 0x2f:  /* comis[sd] */
                    if (b1 == 0) {
                        sz = 2;
B
bellard 已提交
4300
                    } else {
4301
                        sz = 3;
B
bellard 已提交
4302
                    }
4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318
                    break;
                }

                switch (sz) {
                case 2:
                    /* 32 bit access */
                    gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
                    tcg_gen_st32_tl(cpu_T[0], cpu_env,
                                    offsetof(CPUX86State,xmm_t0.XMM_L(0)));
                    break;
                case 3:
                    /* 64 bit access */
                    gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.XMM_D(0)));
                    break;
                default:
                    /* 128 bit access */
4319
                    gen_ldo_env_A0(s, op2_offset);
4320
                    break;
B
bellard 已提交
4321 4322 4323 4324 4325 4326 4327 4328
                }
            } 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) {
4329
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4330
                op2_offset = offsetof(CPUX86State,mmx_t0);
4331
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
4332 4333 4334 4335 4336 4337
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
        }
        switch(b) {
A
aurel32 已提交
4338
        case 0x0f: /* 3DNow! data insns */
4339 4340
            if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
                goto illegal_op;
4341
            val = cpu_ldub_code(env, s->pc++);
B
Blue Swirl 已提交
4342 4343
            sse_fn_epp = sse_op_table5[val];
            if (!sse_fn_epp) {
A
aurel32 已提交
4344
                goto illegal_op;
B
Blue Swirl 已提交
4345
            }
B
bellard 已提交
4346 4347
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4348
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
A
aurel32 已提交
4349
            break;
B
bellard 已提交
4350 4351
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
4352
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4353 4354
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4355
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4356
            sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
B
Blue Swirl 已提交
4357
            sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
bellard 已提交
4358 4359 4360
            break;
        case 0xc2:
            /* compare insns */
4361
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4362 4363
            if (val >= 8)
                goto illegal_op;
B
Blue Swirl 已提交
4364
            sse_fn_epp = sse_op_table4[val][b1];
B
Blue Swirl 已提交
4365

B
bellard 已提交
4366 4367
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4368
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4369
            break;
4370 4371 4372 4373
        case 0xf7:
            /* maskmov : we must prepare A0 */
            if (mod != 3)
                goto illegal_op;
4374 4375
            tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EDI]);
            gen_extu(s->aflag, cpu_A0);
4376 4377 4378 4379
            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 已提交
4380
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4381 4382
            sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
            sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4383
            break;
B
bellard 已提交
4384
        default:
B
bellard 已提交
4385 4386
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4387
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4388 4389 4390
            break;
        }
        if (b == 0x2e || b == 0x2f) {
4391
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
4392 4393 4394 4395
        }
    }
}

B
bellard 已提交
4396 4397
/* convert one instruction. s->is_jmp is set if the translation must
   be stopped. Return the next pc value */
4398 4399
static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
                               target_ulong pc_start)
B
bellard 已提交
4400
{
4401
    int b, prefixes;
4402
    int shift;
4403
    TCGMemOp ot, aflag, dflag;
4404
    int modrm, reg, rm, mod, op, opreg, val;
B
bellard 已提交
4405 4406
    target_ulong next_eip, tval;
    int rex_w, rex_r;
B
bellard 已提交
4407

4408
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
4409
        tcg_gen_debug_insn_start(pc_start);
4410
    }
B
bellard 已提交
4411 4412 4413
    s->pc = pc_start;
    prefixes = 0;
    s->override = -1;
B
bellard 已提交
4414 4415 4416 4417 4418
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4419
    x86_64_hregs = 0;
B
bellard 已提交
4420 4421
#endif
    s->rip_offset = 0; /* for relative ip address */
4422 4423
    s->vex_l = 0;
    s->vex_v = 0;
B
bellard 已提交
4424
 next_byte:
4425
    b = cpu_ldub_code(env, s->pc);
B
bellard 已提交
4426
    s->pc++;
4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461
    /* 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 已提交
4462
#ifdef TARGET_X86_64
4463 4464
    case 0x40 ... 0x4f:
        if (CODE64(s)) {
B
bellard 已提交
4465 4466 4467 4468 4469 4470 4471 4472
            /* 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;
        }
4473 4474
        break;
#endif
4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491
    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
            };
            int vex3, vex2 = cpu_ldub_code(env, s->pc);

            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.  */
                break;
            }
            s->pc++;

P
Peter Maydell 已提交
4492
            /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
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 4524 4525 4526 4527 4528 4529 4530 4531
            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) {
                vex3 = vex2;
                b = cpu_ldub_code(env, s->pc++);
            } else {
#ifdef TARGET_X86_64
                s->rex_x = (~vex2 >> 3) & 8;
                s->rex_b = (~vex2 >> 2) & 8;
#endif
                vex3 = cpu_ldub_code(env, s->pc++);
                rex_w = (vex3 >> 7) & 1;
                switch (vex2 & 0x1f) {
                case 0x01: /* Implied 0f leading opcode bytes.  */
                    b = cpu_ldub_code(env, s->pc++) | 0x100;
                    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.  */
                    goto illegal_op;
                }
            }
            s->vex_v = (~vex3 >> 3) & 0xf;
            s->vex_l = (vex3 >> 2) & 1;
            prefixes |= pp_prefix[vex3 & 3] | PREFIX_VEX;
        }
        break;
4532 4533 4534 4535
    }

    /* Post-process prefixes.  */
    if (CODE64(s)) {
4536 4537 4538
        /* 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.  */
4539
        dflag = (rex_w > 0 ? MO_64 : prefixes & PREFIX_DATA ? MO_16 : MO_32);
4540
        /* In 64-bit mode, 0x67 selects 32-bit addressing.  */
4541
        aflag = (prefixes & PREFIX_ADR ? MO_32 : MO_64);
4542 4543
    } else {
        /* In 16/32-bit mode, 0x66 selects the opposite data size.  */
4544 4545 4546 4547
        if (s->code32 ^ ((prefixes & PREFIX_DATA) != 0)) {
            dflag = MO_32;
        } else {
            dflag = MO_16;
B
bellard 已提交
4548
        }
4549
        /* In 16/32-bit mode, 0x67 selects the opposite addressing.  */
4550 4551 4552 4553
        if (s->code32 ^ ((prefixes & PREFIX_ADR) != 0)) {
            aflag = MO_32;
        }  else {
            aflag = MO_16;
B
bellard 已提交
4554
        }
B
bellard 已提交
4555 4556 4557 4558 4559 4560 4561 4562
    }

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

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
P
pbrook 已提交
4563
        gen_helper_lock();
B
bellard 已提交
4564 4565 4566 4567 4568 4569 4570

    /* now check op code */
 reswitch:
    switch(b) {
    case 0x0f:
        /**************************/
        /* extended op code */
4571
        b = cpu_ldub_code(env, s->pc++) | 0x100;
B
bellard 已提交
4572
        goto reswitch;
4573

B
bellard 已提交
4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588
        /**************************/
        /* 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;

4589
            ot = mo_b_d(b, dflag);
4590

B
bellard 已提交
4591 4592
            switch(f) {
            case 0: /* OP Ev, Gv */
4593
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4594
                reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4595
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4596
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4597
                if (mod != 3) {
4598
                    gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4599 4600 4601 4602
                    opreg = OR_TMP0;
                } else if (op == OP_XORL && rm == reg) {
                xor_zero:
                    /* xor reg, reg optimisation */
R
Richard Henderson 已提交
4603
                    set_cc_op(s, CC_OP_CLR);
4604
                    tcg_gen_movi_tl(cpu_T[0], 0);
4605
                    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
4606 4607 4608 4609
                    break;
                } else {
                    opreg = rm;
                }
4610
                gen_op_mov_v_reg(ot, cpu_T[1], reg);
B
bellard 已提交
4611 4612 4613
                gen_op(s, op, ot, opreg);
                break;
            case 1: /* OP Gv, Ev */
4614
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4615
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4616 4617
                reg = ((modrm >> 3) & 7) | rex_r;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4618
                if (mod != 3) {
4619
                    gen_lea_modrm(env, s, modrm);
4620
                    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
B
bellard 已提交
4621 4622 4623
                } else if (op == OP_XORL && rm == reg) {
                    goto xor_zero;
                } else {
4624
                    gen_op_mov_v_reg(ot, cpu_T[1], rm);
B
bellard 已提交
4625 4626 4627 4628
                }
                gen_op(s, op, ot, reg);
                break;
            case 2: /* OP A, Iv */
4629
                val = insn_get(env, s, ot);
4630
                tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
4631 4632 4633 4634 4635 4636
                gen_op(s, op, ot, OR_EAX);
                break;
            }
        }
        break;

4637 4638 4639
    case 0x82:
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4640 4641 4642 4643 4644 4645
    case 0x80: /* GRP1 */
    case 0x81:
    case 0x83:
        {
            int val;

4646
            ot = mo_b_d(b, dflag);
4647

4648
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4649
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4650
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4651
            op = (modrm >> 3) & 7;
4652

B
bellard 已提交
4653
            if (mod != 3) {
B
bellard 已提交
4654 4655 4656 4657
                if (b == 0x83)
                    s->rip_offset = 1;
                else
                    s->rip_offset = insn_const_size(ot);
4658
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4659 4660
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
4661
                opreg = rm;
B
bellard 已提交
4662 4663 4664 4665 4666 4667
            }

            switch(b) {
            default:
            case 0x80:
            case 0x81:
4668
            case 0x82:
4669
                val = insn_get(env, s, ot);
B
bellard 已提交
4670 4671
                break;
            case 0x83:
4672
                val = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
4673 4674
                break;
            }
4675
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
4676 4677 4678 4679 4680 4681 4682
            gen_op(s, op, ot, opreg);
        }
        break;

        /**************************/
        /* inc, dec, and other misc arith */
    case 0x40 ... 0x47: /* inc Gv */
4683
        ot = dflag;
B
bellard 已提交
4684 4685 4686
        gen_inc(s, ot, OR_EAX + (b & 7), 1);
        break;
    case 0x48 ... 0x4f: /* dec Gv */
4687
        ot = dflag;
B
bellard 已提交
4688 4689 4690 4691
        gen_inc(s, ot, OR_EAX + (b & 7), -1);
        break;
    case 0xf6: /* GRP3 */
    case 0xf7:
4692
        ot = mo_b_d(b, dflag);
B
bellard 已提交
4693

4694
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4695
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4696
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4697 4698
        op = (modrm >> 3) & 7;
        if (mod != 3) {
B
bellard 已提交
4699 4700
            if (op == 0)
                s->rip_offset = insn_const_size(ot);
4701
            gen_lea_modrm(env, s, modrm);
4702
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4703
        } else {
4704
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
4705 4706 4707 4708
        }

        switch(op) {
        case 0: /* test */
4709
            val = insn_get(env, s, ot);
4710
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
4711
            gen_op_testl_T0_T1_cc();
4712
            set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4713 4714
            break;
        case 2: /* not */
4715
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4716
            if (mod != 3) {
4717
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4718
            } else {
4719
                gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
4720 4721 4722
            }
            break;
        case 3: /* neg */
4723
            tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4724
            if (mod != 3) {
4725
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4726
            } else {
4727
                gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
4728 4729
            }
            gen_op_update_neg_cc();
4730
            set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
4731 4732 4733
            break;
        case 4: /* mul */
            switch(ot) {
4734
            case MO_8:
4735
                gen_op_mov_v_reg(MO_8, cpu_T[1], R_EAX);
B
bellard 已提交
4736 4737 4738 4739
                tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext8u_tl(cpu_T[1], cpu_T[1]);
                /* XXX: use 32 bit mul which could be faster */
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4740
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
4741 4742
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_andi_tl(cpu_cc_src, cpu_T[0], 0xff00);
4743
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4744
                break;
4745
            case MO_16:
4746
                gen_op_mov_v_reg(MO_16, cpu_T[1], R_EAX);
B
bellard 已提交
4747 4748 4749 4750
                tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext16u_tl(cpu_T[1], cpu_T[1]);
                /* XXX: use 32 bit mul which could be faster */
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4751
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
4752 4753
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4754
                gen_op_mov_reg_v(MO_16, R_EDX, cpu_T[0]);
B
bellard 已提交
4755
                tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4756
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4757 4758
                break;
            default:
4759
            case MO_32:
4760 4761 4762 4763 4764 4765 4766 4767
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                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]);
4768
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4769
                break;
B
bellard 已提交
4770
#ifdef TARGET_X86_64
4771
            case MO_64:
4772 4773 4774 4775
                tcg_gen_mulu2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
                                  cpu_T[0], cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
                tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
4776
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4777 4778
                break;
#endif
B
bellard 已提交
4779 4780 4781 4782
            }
            break;
        case 5: /* imul */
            switch(ot) {
4783
            case MO_8:
4784
                gen_op_mov_v_reg(MO_8, cpu_T[1], R_EAX);
B
bellard 已提交
4785 4786 4787 4788
                tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext8s_tl(cpu_T[1], cpu_T[1]);
                /* XXX: use 32 bit mul which could be faster */
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4789
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
4790 4791 4792
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_ext8s_tl(cpu_tmp0, cpu_T[0]);
                tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4793
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4794
                break;
4795
            case MO_16:
4796
                gen_op_mov_v_reg(MO_16, cpu_T[1], R_EAX);
B
bellard 已提交
4797 4798 4799 4800
                tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
                tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
                /* XXX: use 32 bit mul which could be faster */
                tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
4801
                gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
4802 4803 4804 4805
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
                tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4806
                gen_op_mov_reg_v(MO_16, R_EDX, cpu_T[0]);
4807
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4808 4809
                break;
            default:
4810
            case MO_32:
4811 4812 4813 4814 4815 4816 4817 4818 4819 4820
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
                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);
4821
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4822
                break;
B
bellard 已提交
4823
#ifdef TARGET_X86_64
4824
            case MO_64:
4825 4826 4827 4828 4829
                tcg_gen_muls2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
                                  cpu_T[0], cpu_regs[R_EAX]);
                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]);
4830
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4831 4832
                break;
#endif
B
bellard 已提交
4833 4834 4835 4836
            }
            break;
        case 6: /* div */
            switch(ot) {
4837
            case MO_8:
B
bellard 已提交
4838
                gen_jmp_im(pc_start - s->cs_base);
4839
                gen_helper_divb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4840
                break;
4841
            case MO_16:
B
bellard 已提交
4842
                gen_jmp_im(pc_start - s->cs_base);
4843
                gen_helper_divw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4844 4845
                break;
            default:
4846
            case MO_32:
B
bellard 已提交
4847
                gen_jmp_im(pc_start - s->cs_base);
4848
                gen_helper_divl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4849 4850
                break;
#ifdef TARGET_X86_64
4851
            case MO_64:
B
bellard 已提交
4852
                gen_jmp_im(pc_start - s->cs_base);
4853
                gen_helper_divq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4854
                break;
B
bellard 已提交
4855
#endif
B
bellard 已提交
4856 4857 4858 4859
            }
            break;
        case 7: /* idiv */
            switch(ot) {
4860
            case MO_8:
B
bellard 已提交
4861
                gen_jmp_im(pc_start - s->cs_base);
4862
                gen_helper_idivb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4863
                break;
4864
            case MO_16:
B
bellard 已提交
4865
                gen_jmp_im(pc_start - s->cs_base);
4866
                gen_helper_idivw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4867 4868
                break;
            default:
4869
            case MO_32:
B
bellard 已提交
4870
                gen_jmp_im(pc_start - s->cs_base);
4871
                gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4872 4873
                break;
#ifdef TARGET_X86_64
4874
            case MO_64:
B
bellard 已提交
4875
                gen_jmp_im(pc_start - s->cs_base);
4876
                gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4877
                break;
B
bellard 已提交
4878
#endif
B
bellard 已提交
4879 4880 4881 4882 4883 4884 4885 4886 4887
            }
            break;
        default:
            goto illegal_op;
        }
        break;

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

4890
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4891
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4892
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4893 4894 4895 4896
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
            goto illegal_op;
        }
B
bellard 已提交
4897
        if (CODE64(s)) {
4898
            if (op == 2 || op == 4) {
B
bellard 已提交
4899
                /* operand size for jumps is 64 bit */
4900
                ot = MO_64;
4901
            } else if (op == 3 || op == 5) {
4902
                ot = dflag != MO_16 ? MO_32 + (rex_w == 1) : MO_16;
B
bellard 已提交
4903 4904
            } else if (op == 6) {
                /* default push size is 64 bit */
4905
                ot = mo_pushpop(s, dflag);
B
bellard 已提交
4906 4907
            }
        }
B
bellard 已提交
4908
        if (mod != 3) {
4909
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4910
            if (op >= 2 && op != 3 && op != 5)
4911
                gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4912
        } else {
4913
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931
        }

        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 */
4932
            /* XXX: optimize if memory (no 'and' is necessary) */
4933
            if (dflag == MO_16) {
4934 4935
                tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
            }
B
bellard 已提交
4936
            next_eip = s->pc - s->cs_base;
4937
            tcg_gen_movi_tl(cpu_T[1], next_eip);
4938
            gen_push_v(s, cpu_T[1]);
4939
            gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
4940 4941
            gen_eob(s);
            break;
B
bellard 已提交
4942
        case 3: /* lcall Ev */
4943
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
4944
            gen_add_A0_im(s, 1 << ot);
4945
            gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
4946 4947
        do_lcall:
            if (s->pe && !s->vm86) {
4948
                gen_update_cc_op(s);
B
bellard 已提交
4949
                gen_jmp_im(pc_start - s->cs_base);
4950
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4951
                gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
4952
                                           tcg_const_i32(dflag - 1),
P
pbrook 已提交
4953
                                           tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
4954
            } else {
4955
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4956
                gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
4957
                                      tcg_const_i32(dflag - 1),
P
pbrook 已提交
4958
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
4959 4960 4961 4962
            }
            gen_eob(s);
            break;
        case 4: /* jmp Ev */
4963
            if (dflag == MO_16) {
4964 4965
                tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
            }
4966
            gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
4967 4968 4969
            gen_eob(s);
            break;
        case 5: /* ljmp Ev */
4970
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
4971
            gen_add_A0_im(s, 1 << ot);
4972
            gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
4973 4974
        do_ljmp:
            if (s->pe && !s->vm86) {
4975
                gen_update_cc_op(s);
B
bellard 已提交
4976
                gen_jmp_im(pc_start - s->cs_base);
4977
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4978
                gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
P
pbrook 已提交
4979
                                          tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
4980
            } else {
4981
                gen_op_movl_seg_T0_vm(R_CS);
R
Richard Henderson 已提交
4982
                gen_op_jmp_v(cpu_T[1]);
B
bellard 已提交
4983 4984 4985 4986
            }
            gen_eob(s);
            break;
        case 6: /* push Ev */
4987
            gen_push_v(s, cpu_T[0]);
B
bellard 已提交
4988 4989 4990 4991 4992 4993 4994
            break;
        default:
            goto illegal_op;
        }
        break;

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

4998
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4999
        reg = ((modrm >> 3) & 7) | rex_r;
5000

5001
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
5002
        gen_op_mov_v_reg(ot, cpu_T[1], reg);
B
bellard 已提交
5003
        gen_op_testl_T0_T1_cc();
5004
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5005
        break;
5006

B
bellard 已提交
5007 5008
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
5009
        ot = mo_b_d(b, dflag);
5010
        val = insn_get(env, s, ot);
B
bellard 已提交
5011

5012
        gen_op_mov_v_reg(ot, cpu_T[0], OR_EAX);
5013
        tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5014
        gen_op_testl_T0_T1_cc();
5015
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5016
        break;
5017

B
bellard 已提交
5018
    case 0x98: /* CWDE/CBW */
5019
        switch (dflag) {
B
bellard 已提交
5020
#ifdef TARGET_X86_64
5021
        case MO_64:
5022
            gen_op_mov_v_reg(MO_32, cpu_T[0], R_EAX);
B
bellard 已提交
5023
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
5024
            gen_op_mov_reg_v(MO_64, R_EAX, cpu_T[0]);
5025
            break;
B
bellard 已提交
5026
#endif
5027
        case MO_32:
5028
            gen_op_mov_v_reg(MO_16, cpu_T[0], R_EAX);
B
bellard 已提交
5029
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
5030
            gen_op_mov_reg_v(MO_32, R_EAX, cpu_T[0]);
5031 5032
            break;
        case MO_16:
5033
            gen_op_mov_v_reg(MO_8, cpu_T[0], R_EAX);
B
bellard 已提交
5034
            tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
5035
            gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
5036 5037 5038
            break;
        default:
            tcg_abort();
B
bellard 已提交
5039
        }
B
bellard 已提交
5040 5041
        break;
    case 0x99: /* CDQ/CWD */
5042
        switch (dflag) {
B
bellard 已提交
5043
#ifdef TARGET_X86_64
5044
        case MO_64:
5045
            gen_op_mov_v_reg(MO_64, cpu_T[0], R_EAX);
B
bellard 已提交
5046
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 63);
5047
            gen_op_mov_reg_v(MO_64, R_EDX, cpu_T[0]);
5048
            break;
B
bellard 已提交
5049
#endif
5050
        case MO_32:
5051
            gen_op_mov_v_reg(MO_32, cpu_T[0], R_EAX);
B
bellard 已提交
5052 5053
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 31);
5054
            gen_op_mov_reg_v(MO_32, R_EDX, cpu_T[0]);
5055 5056
            break;
        case MO_16:
5057
            gen_op_mov_v_reg(MO_16, cpu_T[0], R_EAX);
B
bellard 已提交
5058 5059
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 15);
5060
            gen_op_mov_reg_v(MO_16, R_EDX, cpu_T[0]);
5061 5062 5063
            break;
        default:
            tcg_abort();
B
bellard 已提交
5064
        }
B
bellard 已提交
5065 5066 5067 5068
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
5069
        ot = dflag;
5070
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5071 5072 5073 5074 5075
        reg = ((modrm >> 3) & 7) | rex_r;
        if (b == 0x69)
            s->rip_offset = insn_const_size(ot);
        else if (b == 0x6b)
            s->rip_offset = 1;
5076
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5077
        if (b == 0x69) {
5078
            val = insn_get(env, s, ot);
5079
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5080
        } else if (b == 0x6b) {
5081
            val = (int8_t)insn_get(env, s, MO_8);
5082
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5083
        } else {
5084
            gen_op_mov_v_reg(ot, cpu_T[1], reg);
B
bellard 已提交
5085
        }
5086
        switch (ot) {
B
bellard 已提交
5087
#ifdef TARGET_X86_64
5088
        case MO_64:
5089 5090 5091 5092 5093
            tcg_gen_muls2_i64(cpu_regs[reg], cpu_T[1], cpu_T[0], cpu_T[1]);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
            tcg_gen_sari_tl(cpu_cc_src, cpu_cc_dst, 63);
            tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_T[1]);
            break;
B
bellard 已提交
5094
#endif
5095
        case MO_32:
5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106
            tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
            tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
            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:
B
bellard 已提交
5107 5108 5109 5110 5111 5112 5113
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
            /* XXX: use 32 bit mul which could be faster */
            tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
            tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
            tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
            tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
5114
            gen_op_mov_reg_v(ot, reg, cpu_T[0]);
5115
            break;
B
bellard 已提交
5116
        }
5117
        set_cc_op(s, CC_OP_MULB + ot);
B
bellard 已提交
5118 5119 5120
        break;
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */
5121
        ot = mo_b_d(b, dflag);
5122
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5123
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5124 5125
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5126
            rm = (modrm & 7) | REX_B(s);
5127 5128
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
            gen_op_mov_v_reg(ot, cpu_T[1], rm);
5129
            tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5130
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
5131
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
5132
        } else {
5133
            gen_lea_modrm(env, s, modrm);
5134
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
5135
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5136
            tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5137
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
5138
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
5139 5140
        }
        gen_op_update2_cc();
5141
        set_cc_op(s, CC_OP_ADDB + ot);
B
bellard 已提交
5142 5143 5144
        break;
    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */
B
bellard 已提交
5145
        {
B
bellard 已提交
5146
            int label1, label2;
5147
            TCGv t0, t1, t2, a0;
B
bellard 已提交
5148

5149
            ot = mo_b_d(b, dflag);
5150
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5151 5152
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
P
pbrook 已提交
5153 5154 5155 5156
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
            a0 = tcg_temp_local_new();
5157
            gen_op_mov_v_reg(ot, t1, reg);
B
bellard 已提交
5158 5159
            if (mod == 3) {
                rm = (modrm & 7) | REX_B(s);
5160
                gen_op_mov_v_reg(ot, t0, rm);
B
bellard 已提交
5161
            } else {
5162
                gen_lea_modrm(env, s, modrm);
5163
                tcg_gen_mov_tl(a0, cpu_A0);
5164
                gen_op_ld_v(s, ot, t0, a0);
B
bellard 已提交
5165 5166 5167
                rm = 0; /* avoid warning */
            }
            label1 = gen_new_label();
5168 5169
            tcg_gen_mov_tl(t2, cpu_regs[R_EAX]);
            gen_extu(ot, t0);
5170
            gen_extu(ot, t2);
5171
            tcg_gen_brcond_tl(TCG_COND_EQ, t2, t0, label1);
5172
            label2 = gen_new_label();
B
bellard 已提交
5173
            if (mod == 3) {
5174
                gen_op_mov_reg_v(ot, R_EAX, t0);
B
bellard 已提交
5175 5176
                tcg_gen_br(label2);
                gen_set_label(label1);
5177
                gen_op_mov_reg_v(ot, rm, t1);
B
bellard 已提交
5178
            } else {
5179 5180 5181
                /* perform no-op store cycle like physical cpu; must be
                   before changing accumulator to ensure idempotency if
                   the store faults and the instruction is restarted */
5182
                gen_op_st_v(s, ot, t0, a0);
5183
                gen_op_mov_reg_v(ot, R_EAX, t0);
5184
                tcg_gen_br(label2);
B
bellard 已提交
5185
                gen_set_label(label1);
5186
                gen_op_st_v(s, ot, t1, a0);
B
bellard 已提交
5187
            }
5188
            gen_set_label(label2);
5189
            tcg_gen_mov_tl(cpu_cc_src, t0);
5190 5191
            tcg_gen_mov_tl(cpu_cc_srcT, t2);
            tcg_gen_sub_tl(cpu_cc_dst, t2, t0);
5192
            set_cc_op(s, CC_OP_SUBB + ot);
5193 5194 5195 5196
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
            tcg_temp_free(a0);
B
bellard 已提交
5197 5198 5199
        }
        break;
    case 0x1c7: /* cmpxchg8b */
5200
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5201
        mod = (modrm >> 6) & 3;
5202
        if ((mod == 3) || ((modrm & 0x38) != 0x8))
B
bellard 已提交
5203
            goto illegal_op;
B
bellard 已提交
5204
#ifdef TARGET_X86_64
5205
        if (dflag == MO_64) {
B
bellard 已提交
5206 5207 5208
            if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5209
            gen_update_cc_op(s);
5210
            gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
5211
            gen_helper_cmpxchg16b(cpu_env, cpu_A0);
B
bellard 已提交
5212 5213 5214 5215 5216 5217
        } else
#endif        
        {
            if (!(s->cpuid_features & CPUID_CX8))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5218
            gen_update_cc_op(s);
5219
            gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
5220
            gen_helper_cmpxchg8b(cpu_env, cpu_A0);
B
bellard 已提交
5221
        }
5222
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5223
        break;
5224

B
bellard 已提交
5225 5226 5227
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
5228
        gen_op_mov_v_reg(MO_32, cpu_T[0], (b & 7) | REX_B(s));
5229
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5230 5231
        break;
    case 0x58 ... 0x5f: /* pop */
5232
        ot = gen_pop_T0(s);
B
bellard 已提交
5233
        /* NOTE: order is important for pop %sp */
5234
        gen_pop_update(s, ot);
5235
        gen_op_mov_reg_v(ot, (b & 7) | REX_B(s), cpu_T[0]);
B
bellard 已提交
5236 5237
        break;
    case 0x60: /* pusha */
B
bellard 已提交
5238 5239
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5240 5241 5242
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
5243 5244
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5245 5246 5247 5248
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
5249
        ot = mo_pushpop(s, dflag);
B
bellard 已提交
5250
        if (b == 0x68)
5251
            val = insn_get(env, s, ot);
B
bellard 已提交
5252
        else
5253
            val = (int8_t)insn_get(env, s, MO_8);
5254
        tcg_gen_movi_tl(cpu_T[0], val);
5255
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5256 5257
        break;
    case 0x8f: /* pop Ev */
5258
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5259
        mod = (modrm >> 6) & 3;
5260
        ot = gen_pop_T0(s);
B
bellard 已提交
5261 5262
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
5263
            gen_pop_update(s, ot);
B
bellard 已提交
5264
            rm = (modrm & 7) | REX_B(s);
5265
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
5266 5267
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
5268
            s->popl_esp_hack = 1 << ot;
5269
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5270
            s->popl_esp_hack = 0;
5271
            gen_pop_update(s, ot);
B
bellard 已提交
5272
        }
B
bellard 已提交
5273 5274 5275 5276
        break;
    case 0xc8: /* enter */
        {
            int level;
5277
            val = cpu_lduw_code(env, s->pc);
B
bellard 已提交
5278
            s->pc += 2;
5279
            level = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5280 5281 5282 5283 5284
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
        /* XXX: exception not precise (ESP is updated before potential exception) */
B
bellard 已提交
5285
        if (CODE64(s)) {
5286
            gen_op_mov_v_reg(MO_64, cpu_T[0], R_EBP);
5287
            gen_op_mov_reg_v(MO_64, R_ESP, cpu_T[0]);
B
bellard 已提交
5288
        } else if (s->ss32) {
5289
            gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
5290
            gen_op_mov_reg_v(MO_32, R_ESP, cpu_T[0]);
B
bellard 已提交
5291
        } else {
5292
            gen_op_mov_v_reg(MO_16, cpu_T[0], R_EBP);
5293
            gen_op_mov_reg_v(MO_16, R_ESP, cpu_T[0]);
B
bellard 已提交
5294
        }
5295
        ot = gen_pop_T0(s);
5296
        gen_op_mov_reg_v(ot, R_EBP, cpu_T[0]);
5297
        gen_pop_update(s, ot);
B
bellard 已提交
5298 5299 5300 5301 5302
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
5303 5304
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5305
        gen_op_movl_T0_seg(b >> 3);
5306
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5307 5308 5309 5310
        break;
    case 0x1a0: /* push fs */
    case 0x1a8: /* push gs */
        gen_op_movl_T0_seg((b >> 3) & 7);
5311
        gen_push_v(s, cpu_T[0]);
B
bellard 已提交
5312 5313 5314 5315
        break;
    case 0x07: /* pop es */
    case 0x17: /* pop ss */
    case 0x1f: /* pop ds */
B
bellard 已提交
5316 5317
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5318
        reg = b >> 3;
5319
        ot = gen_pop_T0(s);
B
bellard 已提交
5320
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
5321
        gen_pop_update(s, ot);
B
bellard 已提交
5322
        if (reg == R_SS) {
5323 5324 5325 5326
            /* if reg == SS, inhibit interrupts/trace. */
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5327
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5328 5329 5330
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5331
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5332 5333 5334 5335 5336
            gen_eob(s);
        }
        break;
    case 0x1a1: /* pop fs */
    case 0x1a9: /* pop gs */
5337
        ot = gen_pop_T0(s);
B
bellard 已提交
5338
        gen_movl_seg_T0(s, (b >> 3) & 7, pc_start - s->cs_base);
5339
        gen_pop_update(s, ot);
B
bellard 已提交
5340
        if (s->is_jmp) {
B
bellard 已提交
5341
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5342 5343 5344 5345 5346 5347 5348 5349
            gen_eob(s);
        }
        break;

        /**************************/
        /* mov */
    case 0x88:
    case 0x89: /* mov Gv, Ev */
5350
        ot = mo_b_d(b, dflag);
5351
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5352
        reg = ((modrm >> 3) & 7) | rex_r;
5353

B
bellard 已提交
5354
        /* generate a generic store */
5355
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
5356 5357 5358
        break;
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
5359
        ot = mo_b_d(b, dflag);
5360
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5361
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5362 5363
        if (mod != 3) {
            s->rip_offset = insn_const_size(ot);
5364
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5365
        }
5366
        val = insn_get(env, s, ot);
5367
        tcg_gen_movi_tl(cpu_T[0], val);
5368 5369 5370
        if (mod != 3) {
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
        } else {
5371
            gen_op_mov_reg_v(ot, (modrm & 7) | REX_B(s), cpu_T[0]);
5372
        }
B
bellard 已提交
5373 5374 5375
        break;
    case 0x8a:
    case 0x8b: /* mov Ev, Gv */
5376
        ot = mo_b_d(b, dflag);
5377
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5378
        reg = ((modrm >> 3) & 7) | rex_r;
5379

5380
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
5381
        gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
5382 5383
        break;
    case 0x8e: /* mov seg, Gv */
5384
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5385 5386 5387
        reg = (modrm >> 3) & 7;
        if (reg >= 6 || reg == R_CS)
            goto illegal_op;
5388
        gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
5389 5390 5391
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
        if (reg == R_SS) {
            /* if reg == SS, inhibit interrupts/trace */
5392 5393 5394
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5395
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5396 5397 5398
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5399
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5400 5401 5402 5403
            gen_eob(s);
        }
        break;
    case 0x8c: /* mov Gv, seg */
5404
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5405 5406 5407 5408 5409
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (reg >= 6)
            goto illegal_op;
        gen_op_movl_T0_seg(reg);
5410
        ot = mod == 3 ? dflag : MO_16;
5411
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5412 5413 5414 5415 5416 5417 5418
        break;

    case 0x1b6: /* movzbS Gv, Eb */
    case 0x1b7: /* movzwS Gv, Eb */
    case 0x1be: /* movsbS Gv, Eb */
    case 0x1bf: /* movswS Gv, Eb */
        {
5419 5420 5421
            TCGMemOp d_ot;
            TCGMemOp s_ot;

B
bellard 已提交
5422
            /* d_ot is the size of destination */
5423
            d_ot = dflag;
B
bellard 已提交
5424
            /* ot is the size of source */
5425
            ot = (b & 1) + MO_8;
5426 5427 5428
            /* s_ot is the sign+size of source */
            s_ot = b & 8 ? MO_SIGN | ot : ot;

5429
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5430
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5431
            mod = (modrm >> 6) & 3;
B
bellard 已提交
5432
            rm = (modrm & 7) | REX_B(s);
5433

B
bellard 已提交
5434
            if (mod == 3) {
5435
                gen_op_mov_v_reg(ot, cpu_T[0], rm);
5436 5437
                switch (s_ot) {
                case MO_UB:
B
bellard 已提交
5438
                    tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5439
                    break;
5440
                case MO_SB:
B
bellard 已提交
5441
                    tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5442
                    break;
5443
                case MO_UW:
B
bellard 已提交
5444
                    tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5445 5446
                    break;
                default:
5447
                case MO_SW:
B
bellard 已提交
5448
                    tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5449 5450
                    break;
                }
5451
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
5452
            } else {
5453
                gen_lea_modrm(env, s, modrm);
5454
                gen_op_ld_v(s, s_ot, cpu_T[0], cpu_A0);
5455
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
5456 5457 5458 5459 5460
            }
        }
        break;

    case 0x8d: /* lea */
5461
        ot = dflag;
5462
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5463 5464 5465
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
5466
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5467 5468 5469 5470
        /* we must ensure that no segment is added */
        s->override = -1;
        val = s->addseg;
        s->addseg = 0;
5471
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5472
        s->addseg = val;
5473
        gen_op_mov_reg_v(ot, reg, cpu_A0);
B
bellard 已提交
5474
        break;
5475

B
bellard 已提交
5476 5477 5478 5479 5480
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
5481 5482
            target_ulong offset_addr;

5483
            ot = mo_b_d(b, dflag);
5484
            switch (s->aflag) {
B
bellard 已提交
5485
#ifdef TARGET_X86_64
5486
            case MO_64:
5487
                offset_addr = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5488
                s->pc += 8;
5489
                break;
B
bellard 已提交
5490
#endif
5491 5492 5493
            default:
                offset_addr = insn_get(env, s, s->aflag);
                break;
B
bellard 已提交
5494
            }
5495
            tcg_gen_movi_tl(cpu_A0, offset_addr);
B
bellard 已提交
5496
            gen_add_A0_ds_seg(s);
B
bellard 已提交
5497
            if ((b & 2) == 0) {
5498
                gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
5499
                gen_op_mov_reg_v(ot, R_EAX, cpu_T[0]);
B
bellard 已提交
5500
            } else {
5501
                gen_op_mov_v_reg(ot, cpu_T[0], R_EAX);
5502
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5503 5504 5505 5506
            }
        }
        break;
    case 0xd7: /* xlat */
5507 5508 5509 5510
        tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EBX]);
        tcg_gen_ext8u_tl(cpu_T[0], cpu_regs[R_EAX]);
        tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
        gen_extu(s->aflag, cpu_A0);
B
bellard 已提交
5511
        gen_add_A0_ds_seg(s);
5512
        gen_op_ld_v(s, MO_8, cpu_T[0], cpu_A0);
5513
        gen_op_mov_reg_v(MO_8, R_EAX, cpu_T[0]);
B
bellard 已提交
5514 5515
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
5516
        val = insn_get(env, s, MO_8);
5517
        tcg_gen_movi_tl(cpu_T[0], val);
5518
        gen_op_mov_reg_v(MO_8, (b & 7) | REX_B(s), cpu_T[0]);
B
bellard 已提交
5519 5520
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
B
bellard 已提交
5521
#ifdef TARGET_X86_64
5522
        if (dflag == MO_64) {
B
bellard 已提交
5523 5524
            uint64_t tmp;
            /* 64 bit case */
5525
            tmp = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5526 5527
            s->pc += 8;
            reg = (b & 7) | REX_B(s);
5528
            tcg_gen_movi_tl(cpu_T[0], tmp);
5529
            gen_op_mov_reg_v(MO_64, reg, cpu_T[0]);
5530
        } else
B
bellard 已提交
5531 5532
#endif
        {
5533
            ot = dflag;
5534
            val = insn_get(env, s, ot);
B
bellard 已提交
5535
            reg = (b & 7) | REX_B(s);
5536
            tcg_gen_movi_tl(cpu_T[0], val);
5537
            gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
5538
        }
B
bellard 已提交
5539 5540 5541
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
R
Richard Henderson 已提交
5542
    do_xchg_reg_eax:
5543
        ot = dflag;
B
bellard 已提交
5544
        reg = (b & 7) | REX_B(s);
B
bellard 已提交
5545 5546 5547 5548
        rm = R_EAX;
        goto do_xchg_reg;
    case 0x86:
    case 0x87: /* xchg Ev, Gv */
5549
        ot = mo_b_d(b, dflag);
5550
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5551
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5552 5553
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5554
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5555
        do_xchg_reg:
5556 5557
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
            gen_op_mov_v_reg(ot, cpu_T[1], rm);
5558
            gen_op_mov_reg_v(ot, rm, cpu_T[0]);
5559
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
5560
        } else {
5561
            gen_lea_modrm(env, s, modrm);
5562
            gen_op_mov_v_reg(ot, cpu_T[0], reg);
B
bellard 已提交
5563 5564
            /* for xchg, lock is implicit */
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5565
                gen_helper_lock();
5566
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5567
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5568
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5569
                gen_helper_unlock();
5570
            gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
5571 5572 5573
        }
        break;
    case 0xc4: /* les Gv */
5574
        /* In CODE64 this is VEX3; see above.  */
B
bellard 已提交
5575 5576 5577
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
5578
        /* In CODE64 this is VEX2; see above.  */
B
bellard 已提交
5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589
        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:
5590
        ot = dflag != MO_16 ? MO_32 : MO_16;
5591
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5592
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5593 5594 5595
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
5596
        gen_lea_modrm(env, s, modrm);
5597
        gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5598
        gen_add_A0_im(s, 1 << ot);
B
bellard 已提交
5599
        /* load the segment first to handle exceptions properly */
5600
        gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
5601 5602
        gen_movl_seg_T0(s, op, pc_start - s->cs_base);
        /* then put the data */
5603
        gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
5604
        if (s->is_jmp) {
B
bellard 已提交
5605
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5606 5607 5608
            gen_eob(s);
        }
        break;
5609

B
bellard 已提交
5610 5611 5612 5613 5614 5615 5616 5617
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
5618
            ot = mo_b_d(b, dflag);
5619
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5620 5621
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
5622

B
bellard 已提交
5623
            if (mod != 3) {
B
bellard 已提交
5624 5625 5626
                if (shift == 2) {
                    s->rip_offset = 1;
                }
5627
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5628 5629
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
5630
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
5631 5632 5633 5634 5635 5636 5637
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
5638
                    shift = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670
                }
                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:
5671
        ot = dflag;
5672
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5673
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5674 5675
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5676
        if (mod != 3) {
5677
            gen_lea_modrm(env, s, modrm);
5678
            opreg = OR_TMP0;
B
bellard 已提交
5679
        } else {
5680
            opreg = rm;
B
bellard 已提交
5681
        }
5682
        gen_op_mov_v_reg(ot, cpu_T[1], reg);
5683

B
bellard 已提交
5684
        if (shift) {
P
Paolo Bonzini 已提交
5685 5686 5687
            TCGv imm = tcg_const_tl(cpu_ldub_code(env, s->pc++));
            gen_shiftd_rm_T1(s, ot, opreg, op, imm);
            tcg_temp_free(imm);
B
bellard 已提交
5688
        } else {
P
Paolo Bonzini 已提交
5689
            gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
B
bellard 已提交
5690 5691 5692 5693 5694
        }
        break;

        /************************/
        /* floats */
5695
    case 0xd8 ... 0xdf:
B
bellard 已提交
5696 5697 5698 5699 5700 5701
        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;
        }
5702
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5703 5704 5705 5706 5707
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
5708
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719
            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:
5720 5721
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5722
                        gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5723 5724
                        break;
                    case 1:
5725 5726
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5727
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5728 5729
                        break;
                    case 2:
5730 5731
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5732
                        gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5733 5734 5735
                        break;
                    case 3:
                    default:
5736 5737
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5738
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5739 5740
                        break;
                    }
5741

P
pbrook 已提交
5742
                    gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
5743 5744
                    if (op1 == 3) {
                        /* fcomp needs pop */
B
Blue Swirl 已提交
5745
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
5746 5747 5748 5749 5750 5751
                    }
                }
                break;
            case 0x08: /* flds */
            case 0x0a: /* fsts */
            case 0x0b: /* fstps */
B
bellard 已提交
5752 5753 5754
            case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
            case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
            case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
B
bellard 已提交
5755 5756 5757 5758
                switch(op & 7) {
                case 0:
                    switch(op >> 4) {
                    case 0:
5759 5760
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5761
                        gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5762 5763
                        break;
                    case 1:
5764 5765
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5766
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5767 5768
                        break;
                    case 2:
5769 5770
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5771
                        gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5772 5773 5774
                        break;
                    case 3:
                    default:
5775 5776
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5777
                        gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5778 5779 5780
                        break;
                    }
                    break;
B
bellard 已提交
5781
                case 1:
B
bellard 已提交
5782
                    /* XXX: the corresponding CPUID bit must be tested ! */
B
bellard 已提交
5783 5784
                    switch(op >> 4) {
                    case 1:
B
Blue Swirl 已提交
5785
                        gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
5786 5787
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5788 5789
                        break;
                    case 2:
B
Blue Swirl 已提交
5790
                        gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
5791 5792
                        tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
bellard 已提交
5793 5794 5795
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
5796
                        gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
5797 5798
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUW);
B
bellard 已提交
5799
                        break;
B
bellard 已提交
5800
                    }
B
Blue Swirl 已提交
5801
                    gen_helper_fpop(cpu_env);
B
bellard 已提交
5802
                    break;
B
bellard 已提交
5803 5804 5805
                default:
                    switch(op >> 4) {
                    case 0:
B
Blue Swirl 已提交
5806
                        gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
5807 5808
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5809 5810
                        break;
                    case 1:
B
Blue Swirl 已提交
5811
                        gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
5812 5813
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
bellard 已提交
5814 5815
                        break;
                    case 2:
B
Blue Swirl 已提交
5816
                        gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
5817 5818
                        tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
bellard 已提交
5819 5820 5821
                        break;
                    case 3:
                    default:
B
Blue Swirl 已提交
5822
                        gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
5823 5824
                        tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUW);
B
bellard 已提交
5825 5826 5827
                        break;
                    }
                    if ((op & 7) == 3)
B
Blue Swirl 已提交
5828
                        gen_helper_fpop(cpu_env);
B
bellard 已提交
5829 5830 5831 5832
                    break;
                }
                break;
            case 0x0c: /* fldenv mem */
5833
                gen_update_cc_op(s);
B
bellard 已提交
5834
                gen_jmp_im(pc_start - s->cs_base);
5835
                gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5836 5837
                break;
            case 0x0d: /* fldcw mem */
5838 5839
                tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
Blue Swirl 已提交
5840
                gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5841 5842
                break;
            case 0x0e: /* fnstenv mem */
5843
                gen_update_cc_op(s);
B
bellard 已提交
5844
                gen_jmp_im(pc_start - s->cs_base);
5845
                gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5846 5847
                break;
            case 0x0f: /* fnstcw mem */
B
Blue Swirl 已提交
5848
                gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
5849 5850
                tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
bellard 已提交
5851 5852
                break;
            case 0x1d: /* fldt mem */
5853
                gen_update_cc_op(s);
B
bellard 已提交
5854
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5855
                gen_helper_fldt_ST0(cpu_env, cpu_A0);
B
bellard 已提交
5856 5857
                break;
            case 0x1f: /* fstpt mem */
5858
                gen_update_cc_op(s);
B
bellard 已提交
5859
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5860 5861
                gen_helper_fstt_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5862 5863
                break;
            case 0x2c: /* frstor mem */
5864
                gen_update_cc_op(s);
B
bellard 已提交
5865
                gen_jmp_im(pc_start - s->cs_base);
5866
                gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5867 5868
                break;
            case 0x2e: /* fnsave mem */
5869
                gen_update_cc_op(s);
B
bellard 已提交
5870
                gen_jmp_im(pc_start - s->cs_base);
5871
                gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
B
bellard 已提交
5872 5873
                break;
            case 0x2f: /* fnstsw mem */
B
Blue Swirl 已提交
5874
                gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
5875 5876
                tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUW);
B
bellard 已提交
5877 5878
                break;
            case 0x3c: /* fbld */
5879
                gen_update_cc_op(s);
B
bellard 已提交
5880
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5881
                gen_helper_fbld_ST0(cpu_env, cpu_A0);
B
bellard 已提交
5882 5883
                break;
            case 0x3e: /* fbstp */
5884
                gen_update_cc_op(s);
B
bellard 已提交
5885
                gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5886 5887
                gen_helper_fbst_ST0(cpu_env, cpu_A0);
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5888 5889
                break;
            case 0x3d: /* fildll */
5890
                tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5891
                gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5892 5893
                break;
            case 0x3f: /* fistpll */
B
Blue Swirl 已提交
5894
                gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
5895
                tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5896
                gen_helper_fpop(cpu_env);
B
bellard 已提交
5897 5898 5899 5900 5901 5902 5903 5904 5905 5906
                break;
            default:
                goto illegal_op;
            }
        } else {
            /* register float ops */
            opreg = rm;

            switch(op) {
            case 0x08: /* fld sti */
B
Blue Swirl 已提交
5907 5908 5909
                gen_helper_fpush(cpu_env);
                gen_helper_fmov_ST0_STN(cpu_env,
                                        tcg_const_i32((opreg + 1) & 7));
B
bellard 已提交
5910 5911
                break;
            case 0x09: /* fxchg sti */
B
bellard 已提交
5912 5913
            case 0x29: /* fxchg4 sti, undocumented op */
            case 0x39: /* fxchg7 sti, undocumented op */
B
Blue Swirl 已提交
5914
                gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
5915 5916 5917 5918
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
5919
                    /* check exceptions (FreeBSD FPU probe) */
5920
                    gen_update_cc_op(s);
B
bellard 已提交
5921
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
5922
                    gen_helper_fwait(cpu_env);
B
bellard 已提交
5923 5924 5925 5926 5927 5928 5929 5930
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
B
Blue Swirl 已提交
5931
                    gen_helper_fchs_ST0(cpu_env);
B
bellard 已提交
5932 5933
                    break;
                case 1: /* fabs */
B
Blue Swirl 已提交
5934
                    gen_helper_fabs_ST0(cpu_env);
B
bellard 已提交
5935 5936
                    break;
                case 4: /* ftst */
B
Blue Swirl 已提交
5937 5938
                    gen_helper_fldz_FT0(cpu_env);
                    gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
5939 5940
                    break;
                case 5: /* fxam */
B
Blue Swirl 已提交
5941
                    gen_helper_fxam_ST0(cpu_env);
B
bellard 已提交
5942 5943 5944 5945 5946 5947 5948 5949 5950
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0d: /* grp d9/5 */
                {
                    switch(rm) {
                    case 0:
B
Blue Swirl 已提交
5951 5952
                        gen_helper_fpush(cpu_env);
                        gen_helper_fld1_ST0(cpu_env);
B
bellard 已提交
5953 5954
                        break;
                    case 1:
B
Blue Swirl 已提交
5955 5956
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2t_ST0(cpu_env);
B
bellard 已提交
5957 5958
                        break;
                    case 2:
B
Blue Swirl 已提交
5959 5960
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2e_ST0(cpu_env);
B
bellard 已提交
5961 5962
                        break;
                    case 3:
B
Blue Swirl 已提交
5963 5964
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldpi_ST0(cpu_env);
B
bellard 已提交
5965 5966
                        break;
                    case 4:
B
Blue Swirl 已提交
5967 5968
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldlg2_ST0(cpu_env);
B
bellard 已提交
5969 5970
                        break;
                    case 5:
B
Blue Swirl 已提交
5971 5972
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldln2_ST0(cpu_env);
B
bellard 已提交
5973 5974
                        break;
                    case 6:
B
Blue Swirl 已提交
5975 5976
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldz_ST0(cpu_env);
B
bellard 已提交
5977 5978 5979 5980 5981 5982 5983 5984 5985
                        break;
                    default:
                        goto illegal_op;
                    }
                }
                break;
            case 0x0e: /* grp d9/6 */
                switch(rm) {
                case 0: /* f2xm1 */
B
Blue Swirl 已提交
5986
                    gen_helper_f2xm1(cpu_env);
B
bellard 已提交
5987 5988
                    break;
                case 1: /* fyl2x */
B
Blue Swirl 已提交
5989
                    gen_helper_fyl2x(cpu_env);
B
bellard 已提交
5990 5991
                    break;
                case 2: /* fptan */
B
Blue Swirl 已提交
5992
                    gen_helper_fptan(cpu_env);
B
bellard 已提交
5993 5994
                    break;
                case 3: /* fpatan */
B
Blue Swirl 已提交
5995
                    gen_helper_fpatan(cpu_env);
B
bellard 已提交
5996 5997
                    break;
                case 4: /* fxtract */
B
Blue Swirl 已提交
5998
                    gen_helper_fxtract(cpu_env);
B
bellard 已提交
5999 6000
                    break;
                case 5: /* fprem1 */
B
Blue Swirl 已提交
6001
                    gen_helper_fprem1(cpu_env);
B
bellard 已提交
6002 6003
                    break;
                case 6: /* fdecstp */
B
Blue Swirl 已提交
6004
                    gen_helper_fdecstp(cpu_env);
B
bellard 已提交
6005 6006 6007
                    break;
                default:
                case 7: /* fincstp */
B
Blue Swirl 已提交
6008
                    gen_helper_fincstp(cpu_env);
B
bellard 已提交
6009 6010 6011 6012 6013 6014
                    break;
                }
                break;
            case 0x0f: /* grp d9/7 */
                switch(rm) {
                case 0: /* fprem */
B
Blue Swirl 已提交
6015
                    gen_helper_fprem(cpu_env);
B
bellard 已提交
6016 6017
                    break;
                case 1: /* fyl2xp1 */
B
Blue Swirl 已提交
6018
                    gen_helper_fyl2xp1(cpu_env);
B
bellard 已提交
6019 6020
                    break;
                case 2: /* fsqrt */
B
Blue Swirl 已提交
6021
                    gen_helper_fsqrt(cpu_env);
B
bellard 已提交
6022 6023
                    break;
                case 3: /* fsincos */
B
Blue Swirl 已提交
6024
                    gen_helper_fsincos(cpu_env);
B
bellard 已提交
6025 6026
                    break;
                case 5: /* fscale */
B
Blue Swirl 已提交
6027
                    gen_helper_fscale(cpu_env);
B
bellard 已提交
6028 6029
                    break;
                case 4: /* frndint */
B
Blue Swirl 已提交
6030
                    gen_helper_frndint(cpu_env);
B
bellard 已提交
6031 6032
                    break;
                case 6: /* fsin */
B
Blue Swirl 已提交
6033
                    gen_helper_fsin(cpu_env);
B
bellard 已提交
6034 6035 6036
                    break;
                default:
                case 7: /* fcos */
B
Blue Swirl 已提交
6037
                    gen_helper_fcos(cpu_env);
B
bellard 已提交
6038 6039 6040 6041 6042 6043 6044 6045
                    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;
6046

B
bellard 已提交
6047 6048
                    op1 = op & 7;
                    if (op >= 0x20) {
P
pbrook 已提交
6049
                        gen_helper_fp_arith_STN_ST0(op1, opreg);
B
bellard 已提交
6050
                        if (op >= 0x30)
B
Blue Swirl 已提交
6051
                            gen_helper_fpop(cpu_env);
B
bellard 已提交
6052
                    } else {
B
Blue Swirl 已提交
6053
                        gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
P
pbrook 已提交
6054
                        gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
6055 6056 6057 6058
                    }
                }
                break;
            case 0x02: /* fcom */
B
bellard 已提交
6059
            case 0x22: /* fcom2, undocumented op */
B
Blue Swirl 已提交
6060 6061
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6062 6063
                break;
            case 0x03: /* fcomp */
B
bellard 已提交
6064 6065
            case 0x23: /* fcomp3, undocumented op */
            case 0x32: /* fcomp5, undocumented op */
B
Blue Swirl 已提交
6066 6067 6068
                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 已提交
6069 6070 6071 6072
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
B
Blue Swirl 已提交
6073 6074 6075 6076
                    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 已提交
6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088
                    break;
                default:
                    goto illegal_op;
                }
                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 已提交
6089
                    gen_helper_fclex(cpu_env);
B
bellard 已提交
6090 6091
                    break;
                case 3: /* fninit */
B
Blue Swirl 已提交
6092
                    gen_helper_fninit(cpu_env);
B
bellard 已提交
6093 6094 6095 6096 6097 6098 6099 6100
                    break;
                case 4: /* fsetpm (287 only, just do nop here) */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1d: /* fucomi */
6101 6102 6103
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6104
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6105 6106
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
6107
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6108 6109
                break;
            case 0x1e: /* fcomi */
6110 6111 6112
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6113
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6114 6115
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
6116
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6117
                break;
B
bellard 已提交
6118
            case 0x28: /* ffree sti */
B
Blue Swirl 已提交
6119
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6120
                break;
B
bellard 已提交
6121
            case 0x2a: /* fst sti */
B
Blue Swirl 已提交
6122
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6123 6124
                break;
            case 0x2b: /* fstp sti */
B
bellard 已提交
6125 6126 6127
            case 0x0b: /* fstp1 sti, undocumented op */
            case 0x3a: /* fstp8 sti, undocumented op */
            case 0x3b: /* fstp9 sti, undocumented op */
B
Blue Swirl 已提交
6128 6129
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6130 6131
                break;
            case 0x2c: /* fucom st(i) */
B
Blue Swirl 已提交
6132 6133
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
B
bellard 已提交
6134 6135
                break;
            case 0x2d: /* fucomp st(i) */
B
Blue Swirl 已提交
6136 6137 6138
                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 已提交
6139 6140 6141 6142
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
B
Blue Swirl 已提交
6143 6144 6145 6146
                    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 已提交
6147 6148 6149 6150 6151
                    break;
                default:
                    goto illegal_op;
                }
                break;
B
bellard 已提交
6152
            case 0x38: /* ffreep sti, undocumented op */
B
Blue Swirl 已提交
6153 6154
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6155
                break;
B
bellard 已提交
6156 6157 6158
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
B
Blue Swirl 已提交
6159
                    gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6160
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
6161
                    gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
B
bellard 已提交
6162 6163 6164 6165 6166 6167
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3d: /* fucomip */
6168 6169 6170
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6171
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6172 6173 6174
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6175
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6176 6177
                break;
            case 0x3e: /* fcomip */
6178 6179 6180
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6181
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6182 6183 6184
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6185
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6186
                break;
6187 6188 6189
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                {
B
bellard 已提交
6190
                    int op1, l1;
6191
                    static const uint8_t fcmov_cc[8] = {
6192 6193 6194 6195 6196
                        (JCC_B << 1),
                        (JCC_Z << 1),
                        (JCC_BE << 1),
                        (JCC_P << 1),
                    };
6197 6198 6199 6200

                    if (!(s->cpuid_features & CPUID_CMOV)) {
                        goto illegal_op;
                    }
6201
                    op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
B
bellard 已提交
6202
                    l1 = gen_new_label();
6203
                    gen_jcc1_noeob(s, op1, l1);
B
Blue Swirl 已提交
6204
                    gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6205
                    gen_set_label(l1);
6206 6207
                }
                break;
B
bellard 已提交
6208 6209 6210 6211 6212 6213 6214 6215 6216 6217
            default:
                goto illegal_op;
            }
        }
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
6218
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6219 6220 6221 6222 6223 6224
        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;
6225

B
bellard 已提交
6226 6227
    case 0xaa: /* stosS */
    case 0xab:
6228
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6229 6230 6231 6232 6233 6234 6235 6236
        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:
6237
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6238 6239 6240 6241 6242 6243 6244 6245
        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:
6246
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257
        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:
6258
        ot = mo_b_d(b, dflag);
B
bellard 已提交
6259 6260 6261 6262 6263 6264 6265 6266 6267 6268
        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:
6269
        ot = mo_b_d32(b, dflag);
6270
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6271 6272
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6273 6274
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6275
        } else {
6276
            gen_ins(s, ot);
P
pbrook 已提交
6277 6278 6279
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6280 6281 6282 6283
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6284
        ot = mo_b_d32(b, dflag);
6285
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6286 6287
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6288 6289
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6290
        } else {
6291
            gen_outs(s, ot);
P
pbrook 已提交
6292 6293 6294
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6295 6296 6297 6298 6299
        }
        break;

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

B
bellard 已提交
6301 6302
    case 0xe4:
    case 0xe5:
6303
        ot = mo_b_d32(b, dflag);
6304
        val = cpu_ldub_code(env, s->pc++);
6305
        tcg_gen_movi_tl(cpu_T[0], val);
6306 6307
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6308 6309
        if (use_icount)
            gen_io_start();
6310
        tcg_gen_movi_i32(cpu_tmp2_i32, val);
P
pbrook 已提交
6311
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
6312
        gen_op_mov_reg_v(ot, R_EAX, cpu_T[1]);
P
pbrook 已提交
6313 6314 6315 6316
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6317 6318 6319
        break;
    case 0xe6:
    case 0xe7:
6320
        ot = mo_b_d32(b, dflag);
6321
        val = cpu_ldub_code(env, s->pc++);
6322
        tcg_gen_movi_tl(cpu_T[0], val);
6323 6324
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
6325
        gen_op_mov_v_reg(ot, cpu_T[1], R_EAX);
6326

P
pbrook 已提交
6327 6328
        if (use_icount)
            gen_io_start();
6329
        tcg_gen_movi_i32(cpu_tmp2_i32, val);
6330
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6331
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6332 6333 6334 6335
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6336 6337 6338
        break;
    case 0xec:
    case 0xed:
6339
        ot = mo_b_d32(b, dflag);
6340
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6341 6342
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6343 6344
        if (use_icount)
            gen_io_start();
6345
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6346
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
6347
        gen_op_mov_reg_v(ot, R_EAX, cpu_T[1]);
P
pbrook 已提交
6348 6349 6350 6351
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6352 6353 6354
        break;
    case 0xee:
    case 0xef:
6355
        ot = mo_b_d32(b, dflag);
6356
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6357 6358
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
6359
        gen_op_mov_v_reg(ot, cpu_T[1], R_EAX);
6360

P
pbrook 已提交
6361 6362
        if (use_icount)
            gen_io_start();
6363 6364
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6365
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6366 6367 6368 6369
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6370 6371 6372 6373 6374
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
6375
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6376
        s->pc += 2;
6377 6378 6379
        ot = gen_pop_T0(s);
        gen_stack_update(s, val + (1 << ot));
        /* Note that gen_pop_T0 uses a zero-extending load.  */
6380
        gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
6381 6382 6383
        gen_eob(s);
        break;
    case 0xc3: /* ret */
6384 6385 6386
        ot = gen_pop_T0(s);
        gen_pop_update(s, ot);
        /* Note that gen_pop_T0 uses a zero-extending load.  */
6387
        gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
6388 6389 6390
        gen_eob(s);
        break;
    case 0xca: /* lret im */
6391
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6392 6393 6394
        s->pc += 2;
    do_lret:
        if (s->pe && !s->vm86) {
6395
            gen_update_cc_op(s);
B
bellard 已提交
6396
            gen_jmp_im(pc_start - s->cs_base);
6397
            gen_helper_lret_protected(cpu_env, tcg_const_i32(dflag - 1),
P
pbrook 已提交
6398
                                      tcg_const_i32(val));
B
bellard 已提交
6399 6400 6401
        } else {
            gen_stack_A0(s);
            /* pop offset */
6402
            gen_op_ld_v(s, dflag, cpu_T[0], cpu_A0);
B
bellard 已提交
6403 6404
            /* NOTE: keeping EIP updated is not a problem in case of
               exception */
6405
            gen_op_jmp_v(cpu_T[0]);
B
bellard 已提交
6406
            /* pop selector */
6407 6408
            gen_op_addl_A0_im(1 << dflag);
            gen_op_ld_v(s, dflag, cpu_T[0], cpu_A0);
6409
            gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
6410
            /* add stack offset */
6411
            gen_stack_update(s, val + (2 << dflag));
B
bellard 已提交
6412 6413 6414 6415 6416 6417 6418
        }
        gen_eob(s);
        break;
    case 0xcb: /* lret */
        val = 0;
        goto do_lret;
    case 0xcf: /* iret */
B
bellard 已提交
6419
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
B
bellard 已提交
6420 6421
        if (!s->pe) {
            /* real mode */
6422
            gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
6423
            set_cc_op(s, CC_OP_EFLAGS);
6424 6425 6426 6427
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
6428
                gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
6429
                set_cc_op(s, CC_OP_EFLAGS);
6430
            }
B
bellard 已提交
6431
        } else {
6432
            gen_update_cc_op(s);
B
bellard 已提交
6433
            gen_jmp_im(pc_start - s->cs_base);
6434
            gen_helper_iret_protected(cpu_env, tcg_const_i32(dflag - 1),
P
pbrook 已提交
6435
                                      tcg_const_i32(s->pc - s->cs_base));
6436
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6437 6438 6439 6440 6441
        }
        gen_eob(s);
        break;
    case 0xe8: /* call im */
        {
6442
            if (dflag != MO_16) {
6443
                tval = (int32_t)insn_get(env, s, MO_32);
6444
            } else {
6445
                tval = (int16_t)insn_get(env, s, MO_16);
6446
            }
B
bellard 已提交
6447
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
6448
            tval += next_eip;
6449
            if (dflag == MO_16) {
B
bellard 已提交
6450
                tval &= 0xffff;
6451
            } else if (!CODE64(s)) {
6452
                tval &= 0xffffffff;
6453
            }
6454
            tcg_gen_movi_tl(cpu_T[0], next_eip);
6455
            gen_push_v(s, cpu_T[0]);
B
bellard 已提交
6456
            gen_jmp(s, tval);
B
bellard 已提交
6457 6458 6459 6460 6461
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
6462

B
bellard 已提交
6463 6464
            if (CODE64(s))
                goto illegal_op;
6465
            ot = dflag;
6466
            offset = insn_get(env, s, ot);
6467
            selector = insn_get(env, s, MO_16);
6468

6469
            tcg_gen_movi_tl(cpu_T[0], selector);
6470
            tcg_gen_movi_tl(cpu_T[1], offset);
B
bellard 已提交
6471 6472
        }
        goto do_lcall;
B
bellard 已提交
6473
    case 0xe9: /* jmp im */
6474
        if (dflag != MO_16) {
6475
            tval = (int32_t)insn_get(env, s, MO_32);
6476
        } else {
6477
            tval = (int16_t)insn_get(env, s, MO_16);
6478
        }
B
bellard 已提交
6479
        tval += s->pc - s->cs_base;
6480
        if (dflag == MO_16) {
B
bellard 已提交
6481
            tval &= 0xffff;
6482
        } else if (!CODE64(s)) {
6483
            tval &= 0xffffffff;
6484
        }
B
bellard 已提交
6485
        gen_jmp(s, tval);
B
bellard 已提交
6486 6487 6488 6489 6490
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6491 6492
            if (CODE64(s))
                goto illegal_op;
6493
            ot = dflag;
6494
            offset = insn_get(env, s, ot);
6495
            selector = insn_get(env, s, MO_16);
6496

6497
            tcg_gen_movi_tl(cpu_T[0], selector);
6498
            tcg_gen_movi_tl(cpu_T[1], offset);
B
bellard 已提交
6499 6500 6501
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
6502
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6503
        tval += s->pc - s->cs_base;
6504
        if (dflag == MO_16) {
B
bellard 已提交
6505
            tval &= 0xffff;
6506
        }
B
bellard 已提交
6507
        gen_jmp(s, tval);
B
bellard 已提交
6508 6509
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
6510
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6511 6512
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
6513
        if (dflag != MO_16) {
6514
            tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6515
        } else {
6516
            tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6517 6518 6519
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6520
        tval += next_eip;
6521
        if (dflag == MO_16) {
B
bellard 已提交
6522
            tval &= 0xffff;
6523
        }
B
bellard 已提交
6524
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6525 6526 6527
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
6528
        modrm = cpu_ldub_code(env, s->pc++);
6529
        gen_setcc1(s, b, cpu_T[0]);
6530
        gen_ldst_modrm(env, s, modrm, MO_8, OR_TMP0, 1);
B
bellard 已提交
6531 6532
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6533 6534 6535
        if (!(s->cpuid_features & CPUID_CMOV)) {
            goto illegal_op;
        }
6536
        ot = dflag;
6537 6538 6539
        modrm = cpu_ldub_code(env, s->pc++);
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_cmovcc1(env, s, ot, b, modrm, reg);
B
bellard 已提交
6540
        break;
6541

B
bellard 已提交
6542 6543 6544
        /************************/
        /* flags */
    case 0x9c: /* pushf */
B
bellard 已提交
6545
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
B
bellard 已提交
6546 6547 6548
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6549
            gen_update_cc_op(s);
6550
            gen_helper_read_eflags(cpu_T[0], cpu_env);
6551
            gen_push_v(s, cpu_T[0]);
B
bellard 已提交
6552 6553 6554
        }
        break;
    case 0x9d: /* popf */
B
bellard 已提交
6555
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
B
bellard 已提交
6556 6557 6558
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6559
            ot = gen_pop_T0(s);
B
bellard 已提交
6560
            if (s->cpl == 0) {
6561
                if (dflag != MO_16) {
6562 6563 6564 6565 6566
                    gen_helper_write_eflags(cpu_env, cpu_T[0],
                                            tcg_const_i32((TF_MASK | AC_MASK |
                                                           ID_MASK | NT_MASK |
                                                           IF_MASK |
                                                           IOPL_MASK)));
B
bellard 已提交
6567
                } else {
6568 6569 6570 6571 6572
                    gen_helper_write_eflags(cpu_env, cpu_T[0],
                                            tcg_const_i32((TF_MASK | AC_MASK |
                                                           ID_MASK | NT_MASK |
                                                           IF_MASK | IOPL_MASK)
                                                          & 0xffff));
B
bellard 已提交
6573 6574
                }
            } else {
B
bellard 已提交
6575
                if (s->cpl <= s->iopl) {
6576
                    if (dflag != MO_16) {
6577 6578 6579 6580 6581 6582
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)));
B
bellard 已提交
6583
                    } else {
6584 6585 6586 6587 6588 6589 6590
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)
                                                              & 0xffff));
B
bellard 已提交
6591
                    }
B
bellard 已提交
6592
                } else {
6593
                    if (dflag != MO_16) {
6594 6595 6596
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)));
B
bellard 已提交
6597
                    } else {
6598 6599 6600 6601
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)
                                                         & 0xffff));
B
bellard 已提交
6602
                    }
B
bellard 已提交
6603 6604
                }
            }
6605
            gen_pop_update(s, ot);
6606
            set_cc_op(s, CC_OP_EFLAGS);
H
H. Peter Anvin 已提交
6607
            /* abort translation because TF/AC flag may change */
B
bellard 已提交
6608
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6609 6610 6611 6612
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
6613
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6614
            goto illegal_op;
6615
        gen_op_mov_v_reg(MO_8, cpu_T[0], R_AH);
6616
        gen_compute_eflags(s);
6617 6618 6619
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, CC_O);
        tcg_gen_andi_tl(cpu_T[0], cpu_T[0], CC_S | CC_Z | CC_A | CC_P | CC_C);
        tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_T[0]);
B
bellard 已提交
6620 6621
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
6622
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6623
            goto illegal_op;
6624
        gen_compute_eflags(s);
6625
        /* Note: gen_compute_eflags() only gives the condition codes */
6626
        tcg_gen_ori_tl(cpu_T[0], cpu_cc_src, 0x02);
6627
        gen_op_mov_reg_v(MO_8, R_AH, cpu_T[0]);
B
bellard 已提交
6628 6629
        break;
    case 0xf5: /* cmc */
6630
        gen_compute_eflags(s);
6631
        tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6632 6633
        break;
    case 0xf8: /* clc */
6634
        gen_compute_eflags(s);
6635
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
B
bellard 已提交
6636 6637
        break;
    case 0xf9: /* stc */
6638
        gen_compute_eflags(s);
6639
        tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6640 6641
        break;
    case 0xfc: /* cld */
6642
        tcg_gen_movi_i32(cpu_tmp2_i32, 1);
6643
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6644 6645
        break;
    case 0xfd: /* std */
6646
        tcg_gen_movi_i32(cpu_tmp2_i32, -1);
6647
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6648 6649 6650 6651 6652
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
6653
        ot = dflag;
6654
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6655
        op = (modrm >> 3) & 7;
B
bellard 已提交
6656
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6657
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6658
        if (mod != 3) {
B
bellard 已提交
6659
            s->rip_offset = 1;
6660
            gen_lea_modrm(env, s, modrm);
6661
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6662
        } else {
6663
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
6664 6665
        }
        /* load shift */
6666
        val = cpu_ldub_code(env, s->pc++);
6667
        tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
6668 6669 6670
        if (op < 4)
            goto illegal_op;
        op -= 4;
B
bellard 已提交
6671
        goto bt_op;
B
bellard 已提交
6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683
    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:
6684
        ot = dflag;
6685
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6686
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6687
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6688
        rm = (modrm & 7) | REX_B(s);
6689
        gen_op_mov_v_reg(MO_32, cpu_T[1], reg);
B
bellard 已提交
6690
        if (mod != 3) {
6691
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
6692
            /* specific case: we need to add a displacement */
B
bellard 已提交
6693 6694 6695 6696
            gen_exts(ot, cpu_T[1]);
            tcg_gen_sari_tl(cpu_tmp0, cpu_T[1], 3 + ot);
            tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, ot);
            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
6697
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6698
        } else {
6699
            gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
bellard 已提交
6700
        }
B
bellard 已提交
6701 6702
    bt_op:
        tcg_gen_andi_tl(cpu_T[1], cpu_T[1], (1 << (3 + ot)) - 1);
6703
        tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
B
bellard 已提交
6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714
        switch(op) {
        case 0:
            break;
        case 1:
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
            tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
        case 2:
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6715
            tcg_gen_andc_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
B
bellard 已提交
6716 6717 6718 6719 6720 6721 6722 6723
            break;
        default:
        case 3:
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
            tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
        }
B
bellard 已提交
6724
        if (op != 0) {
6725 6726 6727
            if (mod != 3) {
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
            } else {
6728
                gen_op_mov_reg_v(ot, rm, cpu_T[0]);
6729
            }
6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750
        }

        /* 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 已提交
6751
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
6752 6753 6754 6755 6756 6757 6758 6759
            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 已提交
6760 6761
        }
        break;
6762 6763
    case 0x1bc: /* bsf / tzcnt */
    case 0x1bd: /* bsr / lzcnt */
6764
        ot = dflag;
6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781
        modrm = cpu_ldub_code(env, s->pc++);
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
        gen_extu(ot, cpu_T[0]);

        /* 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;
            tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
            if (b & 1) {
                /* For lzcnt, reduce the target_ulong result by the
                   number of zeros that we expect to find at the top.  */
                gen_helper_clz(cpu_T[0], cpu_T[0]);
                tcg_gen_subi_tl(cpu_T[0], cpu_T[0], TARGET_LONG_BITS - size);
B
bellard 已提交
6782
            } else {
6783 6784 6785 6786 6787
                /* For tzcnt, a zero input must return the operand size:
                   force all bits outside the operand size to 1.  */
                target_ulong mask = (target_ulong)-2 << (size - 1);
                tcg_gen_ori_tl(cpu_T[0], cpu_T[0], mask);
                gen_helper_ctz(cpu_T[0], cpu_T[0]);
B
bellard 已提交
6788
            }
6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811
            /* For lzcnt/tzcnt, C and Z bits are defined and are
               related to the result.  */
            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.  */
            tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
            set_cc_op(s, CC_OP_LOGICB + ot);
            if (b & 1) {
                /* For bsr, return the bit index of the first 1 bit,
                   not the count of leading zeros.  */
                gen_helper_clz(cpu_T[0], cpu_T[0]);
                tcg_gen_xori_tl(cpu_T[0], cpu_T[0], TARGET_LONG_BITS - 1);
            } else {
                gen_helper_ctz(cpu_T[0], cpu_T[0]);
            }
            /* ??? 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.  */
            tcg_gen_movi_tl(cpu_tmp0, 0);
            tcg_gen_movcond_tl(TCG_COND_EQ, cpu_T[0], cpu_cc_dst, cpu_tmp0,
                               cpu_regs[reg], cpu_T[0]);
B
bellard 已提交
6812
        }
6813
        gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
6814 6815 6816 6817
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
6818 6819
        if (CODE64(s))
            goto illegal_op;
6820
        gen_update_cc_op(s);
6821
        gen_helper_daa(cpu_env);
6822
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6823 6824
        break;
    case 0x2f: /* das */
B
bellard 已提交
6825 6826
        if (CODE64(s))
            goto illegal_op;
6827
        gen_update_cc_op(s);
6828
        gen_helper_das(cpu_env);
6829
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6830 6831
        break;
    case 0x37: /* aaa */
B
bellard 已提交
6832 6833
        if (CODE64(s))
            goto illegal_op;
6834
        gen_update_cc_op(s);
6835
        gen_helper_aaa(cpu_env);
6836
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6837 6838
        break;
    case 0x3f: /* aas */
B
bellard 已提交
6839 6840
        if (CODE64(s))
            goto illegal_op;
6841
        gen_update_cc_op(s);
6842
        gen_helper_aas(cpu_env);
6843
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6844 6845
        break;
    case 0xd4: /* aam */
B
bellard 已提交
6846 6847
        if (CODE64(s))
            goto illegal_op;
6848
        val = cpu_ldub_code(env, s->pc++);
6849 6850 6851
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
6852
            gen_helper_aam(cpu_env, tcg_const_i32(val));
6853
            set_cc_op(s, CC_OP_LOGICB);
6854
        }
B
bellard 已提交
6855 6856
        break;
    case 0xd5: /* aad */
B
bellard 已提交
6857 6858
        if (CODE64(s))
            goto illegal_op;
6859
        val = cpu_ldub_code(env, s->pc++);
6860
        gen_helper_aad(cpu_env, tcg_const_i32(val));
6861
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
6862 6863 6864 6865
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
6866
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
6867
        if (prefixes & PREFIX_LOCK) {
6868
            goto illegal_op;
R
Richard Henderson 已提交
6869 6870 6871 6872 6873
        }
        /* 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 已提交
6874
        if (prefixes & PREFIX_REPZ) {
6875 6876 6877 6878
            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));
            s->is_jmp = DISAS_TB_JUMP;
T
ths 已提交
6879
        }
B
bellard 已提交
6880 6881
        break;
    case 0x9b: /* fwait */
6882
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
6883 6884
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
6885
        } else {
6886
            gen_update_cc_op(s);
B
bellard 已提交
6887
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6888
            gen_helper_fwait(cpu_env);
B
bellard 已提交
6889
        }
B
bellard 已提交
6890 6891 6892 6893 6894
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
6895
        val = cpu_ldub_code(env, s->pc++);
6896
        if (s->vm86 && s->iopl != 3) {
6897
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6898 6899 6900
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
6901 6902
        break;
    case 0xce: /* into */
B
bellard 已提交
6903 6904
        if (CODE64(s))
            goto illegal_op;
6905
        gen_update_cc_op(s);
6906
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6907
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
6908
        break;
A
aurel32 已提交
6909
#ifdef WANT_ICEBP
B
bellard 已提交
6910
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
6911
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
6912
#if 1
B
bellard 已提交
6913
        gen_debug(s, pc_start - s->cs_base);
6914 6915
#else
        /* start debug */
6916
        tb_flush(env);
6917
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
6918
#endif
B
bellard 已提交
6919
        break;
A
aurel32 已提交
6920
#endif
B
bellard 已提交
6921 6922 6923
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
6924
                gen_helper_cli(cpu_env);
B
bellard 已提交
6925 6926 6927 6928 6929
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
6930
                gen_helper_cli(cpu_env);
B
bellard 已提交
6931 6932 6933 6934 6935 6936 6937 6938 6939
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        }
        break;
    case 0xfb: /* sti */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
            gen_sti:
6940
                gen_helper_sti(cpu_env);
B
bellard 已提交
6941
                /* interruptions are enabled only the first insn after sti */
6942 6943 6944
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
6945
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
6946
                /* give a chance to handle pending irqs */
B
bellard 已提交
6947
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960
                gen_eob(s);
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
                goto gen_sti;
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        }
        break;
    case 0x62: /* bound */
B
bellard 已提交
6961 6962
        if (CODE64(s))
            goto illegal_op;
6963
        ot = dflag;
6964
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6965 6966 6967 6968
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
6969
        gen_op_mov_v_reg(ot, cpu_T[0], reg);
6970
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
6971
        gen_jmp_im(pc_start - s->cs_base);
6972
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
6973
        if (ot == MO_16) {
B
Blue Swirl 已提交
6974 6975 6976 6977
            gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
        } else {
            gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
        }
B
bellard 已提交
6978 6979
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
6980 6981
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
6982
        if (dflag == MO_64) {
6983
            gen_op_mov_v_reg(MO_64, cpu_T[0], reg);
A
aurel32 已提交
6984
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
6985
            gen_op_mov_reg_v(MO_64, reg, cpu_T[0]);
6986
        } else
6987
#endif
B
bellard 已提交
6988
        {
6989
            gen_op_mov_v_reg(MO_32, cpu_T[0], reg);
6990 6991
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
6992
            gen_op_mov_reg_v(MO_32, reg, cpu_T[0]);
B
bellard 已提交
6993
        }
B
bellard 已提交
6994 6995
        break;
    case 0xd6: /* salc */
B
bellard 已提交
6996 6997
        if (CODE64(s))
            goto illegal_op;
6998
        gen_compute_eflags_c(s, cpu_T[0]);
6999
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
7000
        gen_op_mov_reg_v(MO_8, R_EAX, cpu_T[0]);
B
bellard 已提交
7001 7002 7003 7004 7005
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
7006
        {
7007
            int l1, l2, l3;
B
bellard 已提交
7008

7009
            tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
7010 7011
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
7012
            if (dflag == MO_16) {
B
bellard 已提交
7013
                tval &= 0xffff;
7014
            }
7015

B
bellard 已提交
7016 7017
            l1 = gen_new_label();
            l2 = gen_new_label();
7018
            l3 = gen_new_label();
B
bellard 已提交
7019
            b &= 3;
7020 7021 7022
            switch(b) {
            case 0: /* loopnz */
            case 1: /* loopz */
7023 7024
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jz_ecx(s->aflag, l3);
7025
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7026 7027
                break;
            case 2: /* loop */
7028 7029
                gen_op_add_reg_im(s->aflag, R_ECX, -1);
                gen_op_jnz_ecx(s->aflag, l1);
7030 7031 7032
                break;
            default:
            case 3: /* jcxz */
7033
                gen_op_jz_ecx(s->aflag, l1);
7034
                break;
B
bellard 已提交
7035 7036
            }

7037
            gen_set_label(l3);
B
bellard 已提交
7038
            gen_jmp_im(next_eip);
7039
            tcg_gen_br(l2);
7040

B
bellard 已提交
7041 7042 7043 7044 7045
            gen_set_label(l1);
            gen_jmp_im(tval);
            gen_set_label(l2);
            gen_eob(s);
        }
B
bellard 已提交
7046 7047 7048 7049 7050 7051
        break;
    case 0x130: /* wrmsr */
    case 0x132: /* rdmsr */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7052
            gen_update_cc_op(s);
B
bellard 已提交
7053
            gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7054
            if (b & 2) {
B
Blue Swirl 已提交
7055
                gen_helper_rdmsr(cpu_env);
T
ths 已提交
7056
            } else {
B
Blue Swirl 已提交
7057
                gen_helper_wrmsr(cpu_env);
T
ths 已提交
7058
            }
B
bellard 已提交
7059 7060 7061
        }
        break;
    case 0x131: /* rdtsc */
7062
        gen_update_cc_op(s);
B
bellard 已提交
7063
        gen_jmp_im(pc_start - s->cs_base);
P
pbrook 已提交
7064 7065
        if (use_icount)
            gen_io_start();
B
Blue Swirl 已提交
7066
        gen_helper_rdtsc(cpu_env);
P
pbrook 已提交
7067 7068 7069 7070
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
7071
        break;
7072
    case 0x133: /* rdpmc */
7073
        gen_update_cc_op(s);
7074
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7075
        gen_helper_rdpmc(cpu_env);
7076
        break;
7077
    case 0x134: /* sysenter */
7078
        /* For Intel SYSENTER is valid on 64-bit */
7079
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7080
            goto illegal_op;
7081 7082 7083
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7084
            gen_update_cc_op(s);
B
bellard 已提交
7085
            gen_jmp_im(pc_start - s->cs_base);
7086
            gen_helper_sysenter(cpu_env);
7087 7088 7089 7090
            gen_eob(s);
        }
        break;
    case 0x135: /* sysexit */
7091
        /* For Intel SYSEXIT is valid on 64-bit */
7092
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7093
            goto illegal_op;
7094 7095 7096
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7097
            gen_update_cc_op(s);
B
bellard 已提交
7098
            gen_jmp_im(pc_start - s->cs_base);
7099
            gen_helper_sysexit(cpu_env, tcg_const_i32(dflag - 1));
7100 7101 7102
            gen_eob(s);
        }
        break;
B
bellard 已提交
7103 7104 7105
#ifdef TARGET_X86_64
    case 0x105: /* syscall */
        /* XXX: is it usable in real mode ? */
J
Jun Koi 已提交
7106
        gen_update_cc_op(s);
B
bellard 已提交
7107
        gen_jmp_im(pc_start - s->cs_base);
7108
        gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7109 7110 7111 7112 7113 7114
        gen_eob(s);
        break;
    case 0x107: /* sysret */
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7115
            gen_update_cc_op(s);
B
bellard 已提交
7116
            gen_jmp_im(pc_start - s->cs_base);
7117
            gen_helper_sysret(cpu_env, tcg_const_i32(dflag - 1));
7118
            /* condition codes are modified only in long mode */
7119 7120 7121
            if (s->lma) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
bellard 已提交
7122 7123 7124 7125
            gen_eob(s);
        }
        break;
#endif
B
bellard 已提交
7126
    case 0x1a2: /* cpuid */
7127
        gen_update_cc_op(s);
B
bellard 已提交
7128
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7129
        gen_helper_cpuid(cpu_env);
B
bellard 已提交
7130 7131 7132 7133 7134
        break;
    case 0xf4: /* hlt */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7135
            gen_update_cc_op(s);
7136
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7137
            gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
J
Jun Koi 已提交
7138
            s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7139 7140 7141
        }
        break;
    case 0x100:
7142
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7143 7144 7145 7146
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* sldt */
7147 7148
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7149
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
B
bellard 已提交
7150
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,ldt.selector));
7151
            ot = mod == 3 ? dflag : MO_16;
7152
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7153 7154
            break;
        case 2: /* lldt */
7155 7156
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7157 7158 7159
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7160
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
7161
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
7162
                gen_jmp_im(pc_start - s->cs_base);
7163
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7164
                gen_helper_lldt(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7165 7166 7167
            }
            break;
        case 1: /* str */
7168 7169
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7170
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
B
bellard 已提交
7171
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,tr.selector));
7172
            ot = mod == 3 ? dflag : MO_16;
7173
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7174 7175
            break;
        case 3: /* ltr */
7176 7177
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7178 7179 7180
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7181
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
7182
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
7183
                gen_jmp_im(pc_start - s->cs_base);
7184
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7185
                gen_helper_ltr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7186 7187 7188 7189
            }
            break;
        case 4: /* verr */
        case 5: /* verw */
7190 7191
            if (!s->pe || s->vm86)
                goto illegal_op;
7192
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7193
            gen_update_cc_op(s);
7194 7195 7196 7197 7198
            if (op == 4) {
                gen_helper_verr(cpu_env, cpu_T[0]);
            } else {
                gen_helper_verw(cpu_env, cpu_T[0]);
            }
7199
            set_cc_op(s, CC_OP_EFLAGS);
7200
            break;
B
bellard 已提交
7201 7202 7203 7204 7205
        default:
            goto illegal_op;
        }
        break;
    case 0x101:
7206
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7207 7208
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
B
bellard 已提交
7209
        rm = modrm & 7;
B
bellard 已提交
7210 7211 7212 7213
        switch(op) {
        case 0: /* sgdt */
            if (mod == 3)
                goto illegal_op;
B
bellard 已提交
7214
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
7215
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7216
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.limit));
7217
            gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
7218
            gen_add_A0_im(s, 2);
B
bellard 已提交
7219
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.base));
7220
            if (dflag == MO_16) {
7221 7222
                tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
            }
7223
            gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7224
            break;
B
bellard 已提交
7225 7226 7227 7228 7229 7230 7231
        case 1:
            if (mod == 3) {
                switch (rm) {
                case 0: /* monitor */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
7232
                    gen_update_cc_op(s);
B
bellard 已提交
7233
                    gen_jmp_im(pc_start - s->cs_base);
7234 7235
                    tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EAX]);
                    gen_extu(s->aflag, cpu_A0);
B
bellard 已提交
7236
                    gen_add_A0_ds_seg(s);
B
Blue Swirl 已提交
7237
                    gen_helper_monitor(cpu_env, cpu_A0);
B
bellard 已提交
7238 7239 7240 7241 7242
                    break;
                case 1: /* mwait */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
J
Jun Koi 已提交
7243
                    gen_update_cc_op(s);
7244
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7245
                    gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7246 7247
                    gen_eob(s);
                    break;
H
H. Peter Anvin 已提交
7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265
                case 2: /* 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 3: /* 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;
B
bellard 已提交
7266 7267 7268 7269
                default:
                    goto illegal_op;
                }
            } else { /* sidt */
B
bellard 已提交
7270
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
7271
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7272
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.limit));
7273
                gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
7274
                gen_add_A0_im(s, 2);
B
bellard 已提交
7275
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.base));
7276
                if (dflag == MO_16) {
7277 7278
                    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
                }
7279
                gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7280 7281
            }
            break;
B
bellard 已提交
7282 7283
        case 2: /* lgdt */
        case 3: /* lidt */
T
ths 已提交
7284
            if (mod == 3) {
7285
                gen_update_cc_op(s);
B
bellard 已提交
7286
                gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7287 7288
                switch(rm) {
                case 0: /* VMRUN */
B
bellard 已提交
7289 7290 7291 7292
                    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);
T
ths 已提交
7293
                        break;
B
bellard 已提交
7294
                    } else {
7295
                        gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag - 1),
P
pbrook 已提交
7296
                                         tcg_const_i32(s->pc - pc_start));
7297
                        tcg_gen_exit_tb(0);
J
Jun Koi 已提交
7298
                        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7299
                    }
T
ths 已提交
7300 7301
                    break;
                case 1: /* VMMCALL */
B
bellard 已提交
7302 7303
                    if (!(s->flags & HF_SVME_MASK))
                        goto illegal_op;
B
Blue Swirl 已提交
7304
                    gen_helper_vmmcall(cpu_env);
T
ths 已提交
7305 7306
                    break;
                case 2: /* VMLOAD */
B
bellard 已提交
7307 7308 7309 7310 7311 7312
                    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;
                    } else {
7313
                        gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag - 1));
B
bellard 已提交
7314
                    }
T
ths 已提交
7315 7316
                    break;
                case 3: /* VMSAVE */
B
bellard 已提交
7317 7318 7319 7320 7321 7322
                    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;
                    } else {
7323
                        gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag - 1));
B
bellard 已提交
7324
                    }
T
ths 已提交
7325 7326
                    break;
                case 4: /* STGI */
B
bellard 已提交
7327 7328 7329 7330 7331 7332 7333 7334
                    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;
                    } else {
B
Blue Swirl 已提交
7335
                        gen_helper_stgi(cpu_env);
B
bellard 已提交
7336
                    }
T
ths 已提交
7337 7338
                    break;
                case 5: /* CLGI */
B
bellard 已提交
7339 7340 7341 7342 7343 7344
                    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;
                    } else {
B
Blue Swirl 已提交
7345
                        gen_helper_clgi(cpu_env);
B
bellard 已提交
7346
                    }
T
ths 已提交
7347 7348
                    break;
                case 6: /* SKINIT */
B
bellard 已提交
7349 7350 7351 7352
                    if ((!(s->flags & HF_SVME_MASK) && 
                         !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) || 
                        !s->pe)
                        goto illegal_op;
B
Blue Swirl 已提交
7353
                    gen_helper_skinit(cpu_env);
T
ths 已提交
7354 7355
                    break;
                case 7: /* INVLPGA */
B
bellard 已提交
7356 7357 7358 7359 7360 7361
                    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;
                    } else {
7362 7363
                        gen_helper_invlpga(cpu_env,
                                           tcg_const_i32(s->aflag - 1));
B
bellard 已提交
7364
                    }
T
ths 已提交
7365 7366 7367 7368 7369
                    break;
                default:
                    goto illegal_op;
                }
            } else if (s->cpl != 0) {
B
bellard 已提交
7370 7371
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7372 7373
                gen_svm_check_intercept(s, pc_start,
                                        op==2 ? SVM_EXIT_GDTR_WRITE : SVM_EXIT_IDTR_WRITE);
7374
                gen_lea_modrm(env, s, modrm);
7375
                gen_op_ld_v(s, MO_16, cpu_T[1], cpu_A0);
7376
                gen_add_A0_im(s, 2);
7377
                gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
7378
                if (dflag == MO_16) {
7379 7380
                    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
                }
B
bellard 已提交
7381
                if (op == 2) {
B
bellard 已提交
7382 7383
                    tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,gdt.base));
                    tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,gdt.limit));
B
bellard 已提交
7384
                } else {
B
bellard 已提交
7385 7386
                    tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,idt.base));
                    tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,idt.limit));
B
bellard 已提交
7387 7388 7389 7390
                }
            }
            break;
        case 4: /* smsw */
B
bellard 已提交
7391
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
7392
#if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
7393 7394
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]) + 4);
#else
B
bellard 已提交
7395
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]));
7396
#endif
7397
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 1);
B
bellard 已提交
7398 7399 7400 7401 7402
            break;
        case 6: /* lmsw */
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7403
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7404
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
Blue Swirl 已提交
7405
                gen_helper_lmsw(cpu_env, cpu_T[0]);
B
bellard 已提交
7406
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7407
                gen_eob(s);
B
bellard 已提交
7408 7409
            }
            break;
A
Andre Przywara 已提交
7410 7411 7412 7413 7414
        case 7:
            if (mod != 3) { /* invlpg */
                if (s->cpl != 0) {
                    gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                } else {
7415
                    gen_update_cc_op(s);
A
Andre Przywara 已提交
7416
                    gen_jmp_im(pc_start - s->cs_base);
7417
                    gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
7418
                    gen_helper_invlpg(cpu_env, cpu_A0);
A
Andre Przywara 已提交
7419 7420 7421
                    gen_jmp_im(s->pc - s->cs_base);
                    gen_eob(s);
                }
B
bellard 已提交
7422
            } else {
A
Andre Przywara 已提交
7423 7424
                switch (rm) {
                case 0: /* swapgs */
B
bellard 已提交
7425
#ifdef TARGET_X86_64
A
Andre Przywara 已提交
7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438
                    if (CODE64(s)) {
                        if (s->cpl != 0) {
                            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                        } else {
                            tcg_gen_ld_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,segs[R_GS].base));
                            tcg_gen_ld_tl(cpu_T[1], cpu_env,
                                offsetof(CPUX86State,kernelgsbase));
                            tcg_gen_st_tl(cpu_T[1], cpu_env,
                                offsetof(CPUX86State,segs[R_GS].base));
                            tcg_gen_st_tl(cpu_T[0], cpu_env,
                                offsetof(CPUX86State,kernelgsbase));
                        }
7439
                    } else
B
bellard 已提交
7440 7441 7442 7443
#endif
                    {
                        goto illegal_op;
                    }
A
Andre Przywara 已提交
7444 7445 7446 7447
                    break;
                case 1: /* rdtscp */
                    if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP))
                        goto illegal_op;
7448
                    gen_update_cc_op(s);
B
bellard 已提交
7449
                    gen_jmp_im(pc_start - s->cs_base);
A
Andre Przywara 已提交
7450 7451
                    if (use_icount)
                        gen_io_start();
B
Blue Swirl 已提交
7452
                    gen_helper_rdtscp(cpu_env);
A
Andre Przywara 已提交
7453 7454 7455 7456 7457 7458 7459
                    if (use_icount) {
                        gen_io_end();
                        gen_jmp(s, s->pc - s->cs_base);
                    }
                    break;
                default:
                    goto illegal_op;
B
bellard 已提交
7460
                }
B
bellard 已提交
7461 7462 7463 7464 7465 7466
            }
            break;
        default:
            goto illegal_op;
        }
        break;
7467 7468 7469 7470 7471
    case 0x108: /* invd */
    case 0x109: /* wbinvd */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
B
bellard 已提交
7472
            gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
7473 7474 7475
            /* nothing to do */
        }
        break;
B
bellard 已提交
7476 7477 7478 7479 7480
    case 0x63: /* arpl or movslS (x86_64) */
#ifdef TARGET_X86_64
        if (CODE64(s)) {
            int d_ot;
            /* d_ot is the size of destination */
7481
            d_ot = dflag;
B
bellard 已提交
7482

7483
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7484 7485 7486
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7487

B
bellard 已提交
7488
            if (mod == 3) {
7489
                gen_op_mov_v_reg(MO_32, cpu_T[0], rm);
B
bellard 已提交
7490
                /* sign extend */
7491
                if (d_ot == MO_64) {
B
bellard 已提交
7492
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
7493
                }
7494
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
7495
            } else {
7496
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
7497
                gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T[0], cpu_A0);
7498
                gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
B
bellard 已提交
7499
            }
7500
        } else
B
bellard 已提交
7501 7502
#endif
        {
7503
            int label1;
L
Laurent Desnogues 已提交
7504
            TCGv t0, t1, t2, a0;
7505

B
bellard 已提交
7506 7507
            if (!s->pe || s->vm86)
                goto illegal_op;
P
pbrook 已提交
7508 7509 7510
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
7511
            ot = MO_16;
7512
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7513 7514 7515 7516
            reg = (modrm >> 3) & 7;
            mod = (modrm >> 6) & 3;
            rm = modrm & 7;
            if (mod != 3) {
7517
                gen_lea_modrm(env, s, modrm);
7518
                gen_op_ld_v(s, ot, t0, cpu_A0);
L
Laurent Desnogues 已提交
7519 7520
                a0 = tcg_temp_local_new();
                tcg_gen_mov_tl(a0, cpu_A0);
B
bellard 已提交
7521
            } else {
7522
                gen_op_mov_v_reg(ot, t0, rm);
L
Laurent Desnogues 已提交
7523
                TCGV_UNUSED(a0);
B
bellard 已提交
7524
            }
7525 7526 7527 7528
            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);
7529
            label1 = gen_new_label();
7530 7531 7532 7533
            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);
7534
            gen_set_label(label1);
B
bellard 已提交
7535
            if (mod != 3) {
7536
                gen_op_st_v(s, ot, t0, a0);
L
Laurent Desnogues 已提交
7537 7538
                tcg_temp_free(a0);
           } else {
7539
                gen_op_mov_reg_v(ot, rm, t0);
B
bellard 已提交
7540
            }
7541
            gen_compute_eflags(s);
7542
            tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
7543 7544 7545 7546
            tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
7547 7548
        }
        break;
B
bellard 已提交
7549 7550
    case 0x102: /* lar */
    case 0x103: /* lsl */
7551 7552
        {
            int label1;
7553
            TCGv t0;
7554 7555
            if (!s->pe || s->vm86)
                goto illegal_op;
7556
            ot = dflag != MO_16 ? MO_32 : MO_16;
7557
            modrm = cpu_ldub_code(env, s->pc++);
7558
            reg = ((modrm >> 3) & 7) | rex_r;
7559
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
P
pbrook 已提交
7560
            t0 = tcg_temp_local_new();
7561
            gen_update_cc_op(s);
7562 7563 7564 7565 7566
            if (b == 0x102) {
                gen_helper_lar(t0, cpu_env, cpu_T[0]);
            } else {
                gen_helper_lsl(t0, cpu_env, cpu_T[0]);
            }
7567 7568
            tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
            label1 = gen_new_label();
P
pbrook 已提交
7569
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
7570
            gen_op_mov_reg_v(ot, reg, t0);
7571
            gen_set_label(label1);
7572
            set_cc_op(s, CC_OP_EFLAGS);
7573
            tcg_temp_free(t0);
7574
        }
B
bellard 已提交
7575 7576
        break;
    case 0x118:
7577
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7578 7579 7580 7581 7582 7583 7584 7585 7586
        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;
7587
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7588 7589
            /* nothing more to do */
            break;
B
bellard 已提交
7590
        default: /* nop (multi byte) */
7591
            gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7592
            break;
B
bellard 已提交
7593 7594
        }
        break;
B
bellard 已提交
7595
    case 0x119 ... 0x11f: /* nop (multi byte) */
7596 7597
        modrm = cpu_ldub_code(env, s->pc++);
        gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7598
        break;
B
bellard 已提交
7599 7600 7601 7602 7603
    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 {
7604
            modrm = cpu_ldub_code(env, s->pc++);
7605 7606 7607 7608 7609
            /* 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 已提交
7610 7611 7612
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
7613
                ot = MO_64;
B
bellard 已提交
7614
            else
7615
                ot = MO_32;
7616 7617 7618 7619
            if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
                (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
                reg = 8;
            }
B
bellard 已提交
7620 7621 7622 7623 7624
            switch(reg) {
            case 0:
            case 2:
            case 3:
            case 4:
B
bellard 已提交
7625
            case 8:
7626
                gen_update_cc_op(s);
B
bellard 已提交
7627
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
7628
                if (b & 2) {
7629
                    gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
Blue Swirl 已提交
7630 7631
                    gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
                                         cpu_T[0]);
B
bellard 已提交
7632
                    gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7633 7634
                    gen_eob(s);
                } else {
B
Blue Swirl 已提交
7635
                    gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
7636
                    gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648
                }
                break;
            default:
                goto illegal_op;
            }
        }
        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 {
7649
            modrm = cpu_ldub_code(env, s->pc++);
7650 7651 7652 7653 7654
            /* 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 已提交
7655 7656 7657
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
7658
                ot = MO_64;
B
bellard 已提交
7659
            else
7660
                ot = MO_32;
B
bellard 已提交
7661
            /* XXX: do it dynamically with CR4.DE bit */
B
bellard 已提交
7662
            if (reg == 4 || reg == 5 || reg >= 8)
B
bellard 已提交
7663 7664
                goto illegal_op;
            if (b & 2) {
T
ths 已提交
7665
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
7666
                gen_op_mov_v_reg(ot, cpu_T[0], rm);
B
Blue Swirl 已提交
7667
                gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
B
bellard 已提交
7668
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7669 7670
                gen_eob(s);
            } else {
T
ths 已提交
7671
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
B
bellard 已提交
7672
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
7673
                gen_op_mov_reg_v(ot, rm, cpu_T[0]);
B
bellard 已提交
7674 7675 7676 7677 7678 7679 7680
            }
        }
        break;
    case 0x106: /* clts */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
T
ths 已提交
7681
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7682
            gen_helper_clts(cpu_env);
B
bellard 已提交
7683
            /* abort block because static cpu state changed */
B
bellard 已提交
7684
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7685
            gen_eob(s);
B
bellard 已提交
7686 7687
        }
        break;
B
balrog 已提交
7688
    /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
B
bellard 已提交
7689 7690
    case 0x1c3: /* MOVNTI reg, mem */
        if (!(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7691
            goto illegal_op;
7692
        ot = mo_64_32(dflag);
7693
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7694 7695 7696 7697 7698
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        reg = ((modrm >> 3) & 7) | rex_r;
        /* generate a generic store */
7699
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
7700
        break;
B
bellard 已提交
7701
    case 0x1ae:
7702
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7703 7704 7705 7706
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* fxsave */
7707
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7708
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7709
                goto illegal_op;
7710
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7711 7712 7713
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7714
            gen_lea_modrm(env, s, modrm);
7715
            gen_update_cc_op(s);
B
bellard 已提交
7716
            gen_jmp_im(pc_start - s->cs_base);
7717
            gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32(dflag == MO_64));
B
bellard 已提交
7718 7719
            break;
        case 1: /* fxrstor */
7720
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7721
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7722
                goto illegal_op;
7723
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7724 7725 7726
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7727
            gen_lea_modrm(env, s, modrm);
7728
            gen_update_cc_op(s);
B
bellard 已提交
7729
            gen_jmp_im(pc_start - s->cs_base);
7730
            gen_helper_fxrstor(cpu_env, cpu_A0, tcg_const_i32(dflag == MO_64));
B
bellard 已提交
7731 7732 7733 7734 7735 7736
            break;
        case 2: /* ldmxcsr */
        case 3: /* stmxcsr */
            if (s->flags & HF_TS_MASK) {
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
B
bellard 已提交
7737
            }
B
bellard 已提交
7738 7739
            if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
                mod == 3)
B
bellard 已提交
7740
                goto illegal_op;
7741
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7742
            if (op == 2) {
7743 7744
                tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
7745
                gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7746
            } else {
B
bellard 已提交
7747
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
7748
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7749
            }
B
bellard 已提交
7750 7751 7752
            break;
        case 5: /* lfence */
        case 6: /* mfence */
7753
            if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7754 7755
                goto illegal_op;
            break;
7756 7757 7758
        case 7: /* sfence / clflush */
            if ((modrm & 0xc7) == 0xc0) {
                /* sfence */
A
aurel32 已提交
7759
                /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
7760 7761 7762 7763 7764 7765
                if (!(s->cpuid_features & CPUID_SSE))
                    goto illegal_op;
            } else {
                /* clflush */
                if (!(s->cpuid_features & CPUID_CLFLUSH))
                    goto illegal_op;
7766
                gen_lea_modrm(env, s, modrm);
7767 7768
            }
            break;
B
bellard 已提交
7769
        default:
B
bellard 已提交
7770 7771 7772
            goto illegal_op;
        }
        break;
A
aurel32 已提交
7773
    case 0x10d: /* 3DNow! prefetch(w) */
7774
        modrm = cpu_ldub_code(env, s->pc++);
A
aurel32 已提交
7775 7776 7777
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
7778
        gen_lea_modrm(env, s, modrm);
7779 7780
        /* ignore for now */
        break;
B
bellard 已提交
7781
    case 0x1aa: /* rsm */
B
bellard 已提交
7782
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
B
bellard 已提交
7783 7784
        if (!(s->flags & HF_SMM_MASK))
            goto illegal_op;
J
Jun Koi 已提交
7785
        gen_update_cc_op(s);
B
bellard 已提交
7786
        gen_jmp_im(s->pc - s->cs_base);
B
Blue Swirl 已提交
7787
        gen_helper_rsm(cpu_env);
B
bellard 已提交
7788 7789
        gen_eob(s);
        break;
B
balrog 已提交
7790 7791 7792 7793 7794 7795 7796
    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;

7797
        modrm = cpu_ldub_code(env, s->pc++);
M
malc 已提交
7798
        reg = ((modrm >> 3) & 7) | rex_r;
B
balrog 已提交
7799

7800
        if (s->prefix & PREFIX_DATA) {
7801
            ot = MO_16;
7802 7803 7804
        } else {
            ot = mo_64_32(dflag);
        }
B
balrog 已提交
7805

7806
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
7807
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
7808
        gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
balrog 已提交
7809

7810
        set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
7811
        break;
A
aurel32 已提交
7812 7813 7814
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
7815 7816
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
7817
    case 0x138 ... 0x13a:
7818
    case 0x150 ... 0x179:
B
bellard 已提交
7819 7820 7821 7822
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
7823
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
7824
        break;
B
bellard 已提交
7825 7826 7827 7828 7829
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7830
        gen_helper_unlock();
B
bellard 已提交
7831 7832
    return s->pc;
 illegal_op:
7833
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7834
        gen_helper_unlock();
B
bellard 已提交
7835 7836 7837 7838 7839 7840 7841
    /* XXX: ensure that no lock was generated */
    gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
    return s->pc;
}

void optimize_flags_init(void)
{
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 7872
    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
    };
    int i;

P
pbrook 已提交
7873 7874
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
7875 7876
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
7877
                                    "cc_dst");
7878 7879
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
                                    "cc_src");
7880 7881
    cpu_cc_src2 = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src2),
                                     "cc_src2");
7882

7883 7884 7885 7886 7887
    for (i = 0; i < CPU_NB_REGS; ++i) {
        cpu_regs[i] = tcg_global_mem_new(TCG_AREG0,
                                         offsetof(CPUX86State, regs[i]),
                                         reg_names[i]);
    }
B
bellard 已提交
7888 7889 7890 7891 7892
}

/* generate intermediate code in gen_opc_buf and gen_opparam_buf for
   basic block 'tb'. If search_pc is TRUE, also generate PC
   information for each intermediate instruction. */
7893
static inline void gen_intermediate_code_internal(X86CPU *cpu,
7894
                                                  TranslationBlock *tb,
7895
                                                  bool search_pc)
B
bellard 已提交
7896
{
7897
    CPUState *cs = CPU(cpu);
7898
    CPUX86State *env = &cpu->env;
B
bellard 已提交
7899
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
7900
    target_ulong pc_ptr;
B
bellard 已提交
7901
    uint16_t *gen_opc_end;
7902
    CPUBreakpoint *bp;
7903
    int j, lj;
7904
    uint64_t flags;
B
bellard 已提交
7905 7906
    target_ulong pc_start;
    target_ulong cs_base;
P
pbrook 已提交
7907 7908
    int num_insns;
    int max_insns;
7909

B
bellard 已提交
7910
    /* generate intermediate code */
B
bellard 已提交
7911 7912
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
7913
    flags = tb->flags;
B
bellard 已提交
7914

7915
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
7916 7917 7918 7919 7920 7921 7922 7923
    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;
7924
    dc->singlestep_enabled = cs->singlestep_enabled;
B
bellard 已提交
7925
    dc->cc_op = CC_OP_DYNAMIC;
7926
    dc->cc_op_dirty = false;
B
bellard 已提交
7927 7928 7929 7930 7931 7932
    dc->cs_base = cs_base;
    dc->tb = tb;
    dc->popl_esp_hack = 0;
    /* select memory access functions */
    dc->mem_index = 0;
    if (flags & HF_SOFTMMU_MASK) {
7933
        dc->mem_index = cpu_mmu_index(env);
B
bellard 已提交
7934
    }
7935 7936 7937 7938 7939
    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];
B
bellard 已提交
7940 7941 7942 7943
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
7944
    dc->flags = flags;
7945
    dc->jmp_opt = !(dc->tf || cs->singlestep_enabled ||
7946
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
7947
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
7948 7949 7950
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
7951 7952
#if 0
    /* check addseg logic */
B
bellard 已提交
7953
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
7954 7955 7956
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967
    cpu_T[0] = tcg_temp_new();
    cpu_T[1] = tcg_temp_new();
    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();
7968
    cpu_cc_srcT = tcg_temp_local_new();
B
bellard 已提交
7969

7970
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
7971 7972 7973 7974

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
7975 7976 7977 7978
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
7979

7980
    gen_tb_start();
B
bellard 已提交
7981
    for(;;) {
7982 7983
        if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
            QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
J
Jan Kiszka 已提交
7984 7985
                if (bp->pc == pc_ptr &&
                    !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
B
bellard 已提交
7986 7987 7988 7989 7990 7991
                    gen_debug(dc, pc_ptr - dc->cs_base);
                    break;
                }
            }
        }
        if (search_pc) {
7992
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
7993 7994 7995
            if (lj < j) {
                lj++;
                while (lj < j)
7996
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
7997
            }
7998
            tcg_ctx.gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
7999
            gen_opc_cc_op[lj] = dc->cc_op;
8000
            tcg_ctx.gen_opc_instr_start[lj] = 1;
8001
            tcg_ctx.gen_opc_icount[lj] = num_insns;
B
bellard 已提交
8002
        }
P
pbrook 已提交
8003 8004 8005
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();

8006
        pc_ptr = disas_insn(env, dc, pc_ptr);
P
pbrook 已提交
8007
        num_insns++;
B
bellard 已提交
8008 8009 8010 8011 8012
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
8013 8014 8015
        /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
           the flag and abort the translation to give the irqs a
           change to be happen */
8016
        if (dc->tf || dc->singlestep_enabled ||
P
pbrook 已提交
8017
            (flags & HF_INHIBIT_IRQ_MASK)) {
B
bellard 已提交
8018
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8019 8020 8021 8022
            gen_eob(dc);
            break;
        }
        /* if too long translation, stop generation too */
8023
        if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
P
pbrook 已提交
8024 8025
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
            num_insns >= max_insns) {
B
bellard 已提交
8026
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8027 8028 8029
            gen_eob(dc);
            break;
        }
8030 8031 8032 8033 8034
        if (singlestep) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
8035
    }
P
pbrook 已提交
8036 8037
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
8038
    gen_tb_end(tb, num_insns);
8039
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
B
bellard 已提交
8040 8041
    /* we don't forget to fill the last values */
    if (search_pc) {
8042
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8043 8044
        lj++;
        while (lj <= j)
8045
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8046
    }
8047

B
bellard 已提交
8048
#ifdef DEBUG_DISAS
8049
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
B
bellard 已提交
8050
        int disas_flags;
8051 8052
        qemu_log("----------------\n");
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
8053 8054 8055 8056 8057 8058
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
B
Blue Swirl 已提交
8059
        log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
8060
        qemu_log("\n");
B
bellard 已提交
8061 8062 8063
    }
#endif

P
pbrook 已提交
8064
    if (!search_pc) {
B
bellard 已提交
8065
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
8066 8067
        tb->icount = num_insns;
    }
B
bellard 已提交
8068 8069
}

8070
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8071
{
8072
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, false);
B
bellard 已提交
8073 8074
}

8075
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8076
{
8077
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, true);
B
bellard 已提交
8078 8079
}

8080
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
8081 8082 8083
{
    int cc_op;
#ifdef DEBUG_DISAS
8084
    if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
A
aurel32 已提交
8085
        int i;
8086
        qemu_log("RESTORE:\n");
A
aurel32 已提交
8087
        for(i = 0;i <= pc_pos; i++) {
8088
            if (tcg_ctx.gen_opc_instr_start[i]) {
8089 8090
                qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
                        tcg_ctx.gen_opc_pc[i]);
A
aurel32 已提交
8091 8092
            }
        }
8093
        qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
8094
                pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
A
aurel32 已提交
8095 8096 8097
                (uint32_t)tb->cs_base);
    }
#endif
8098
    env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
A
aurel32 已提交
8099 8100 8101 8102
    cc_op = gen_opc_cc_op[pc_pos];
    if (cc_op != CC_OP_DYNAMIC)
        env->cc_op = cc_op;
}