translate.c 276.7 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"
B
bellard 已提交
30

P
pbrook 已提交
31 32 33 34
#include "helper.h"
#define GEN_HELPER 1
#include "helper.h"

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

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

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

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

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

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

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

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

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

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

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

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

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

#define NB_OP_SIZES 4

#else /* !TARGET_X86_64 */

#define NB_OP_SIZES 3

#endif /* !TARGET_X86_64 */

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

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

298
static void gen_op_mov_reg_v(TCGMemOp ot, int reg, TCGv t0)
B
bellard 已提交
299 300
{
    switch(ot) {
301
    case MO_8:
302
        if (!byte_reg_is_xH(reg)) {
303
            tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
B
bellard 已提交
304
        } else {
305
            tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
B
bellard 已提交
306 307
        }
        break;
308
    case MO_16:
309
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
B
bellard 已提交
310
        break;
311
    case MO_32:
312 313 314
        /* 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 已提交
315
        break;
316
#ifdef TARGET_X86_64
317
    case MO_64:
318
        tcg_gen_mov_tl(cpu_regs[reg], t0);
B
bellard 已提交
319
        break;
B
bellard 已提交
320
#endif
321 322
    default:
        tcg_abort();
B
bellard 已提交
323 324
    }
}
B
bellard 已提交
325

326
static inline void gen_op_mov_reg_T0(TCGMemOp ot, int reg)
B
bellard 已提交
327
{
328
    gen_op_mov_reg_v(ot, reg, cpu_T[0]);
B
bellard 已提交
329 330
}

331
static inline void gen_op_mov_reg_T1(TCGMemOp ot, int reg)
B
bellard 已提交
332
{
333
    gen_op_mov_reg_v(ot, reg, cpu_T[1]);
B
bellard 已提交
334 335
}

336
static void gen_op_mov_reg_A0(TCGMemOp size, int reg)
B
bellard 已提交
337
{
338
    switch (size) {
339
    case MO_8:
340
        tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], cpu_A0, 0, 16);
B
bellard 已提交
341
        break;
342
    case MO_16:
343 344 345
        /* 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], cpu_A0);
B
bellard 已提交
346
        break;
347
#ifdef TARGET_X86_64
348
    case MO_32:
349
        tcg_gen_mov_tl(cpu_regs[reg], cpu_A0);
B
bellard 已提交
350
        break;
B
bellard 已提交
351
#endif
352 353
    default:
        tcg_abort();
B
bellard 已提交
354 355 356
    }
}

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

367
static inline void gen_op_mov_TN_reg(TCGMemOp ot, int t_index, int reg)
368 369 370 371
{
    gen_op_mov_v_reg(ot, cpu_T[t_index], reg);
}

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

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

B
bellard 已提交
385
#ifdef TARGET_X86_64
B
bellard 已提交
386 387 388 389
static inline void gen_op_addq_A0_im(int64_t val)
{
    tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
}
B
bellard 已提交
390
#endif
B
bellard 已提交
391 392 393 394 395 396 397 398 399 400
    
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 已提交
401

B
bellard 已提交
402
static inline void gen_op_addl_T0_T1(void)
B
bellard 已提交
403
{
B
bellard 已提交
404 405 406 407 408
    tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
}

static inline void gen_op_jmp_T0(void)
{
409
    tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
410 411
}

412
static inline void gen_op_add_reg_im(TCGMemOp size, int reg, int32_t val)
B
bellard 已提交
413
{
414 415
    tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
    gen_op_mov_reg_v(size, reg, cpu_tmp0);
B
bellard 已提交
416 417
}

418
static inline void gen_op_add_reg_T0(TCGMemOp size, int reg)
B
bellard 已提交
419
{
420 421
    tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
    gen_op_mov_reg_v(size, reg, cpu_tmp0);
422
}
B
bellard 已提交
423 424 425

static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
{
426 427
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
428 429
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
430 431 432
    /* 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 已提交
433
}
B
bellard 已提交
434

B
bellard 已提交
435 436
static inline void gen_op_movl_A0_seg(int reg)
{
437
    tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
B
bellard 已提交
438
}
B
bellard 已提交
439

440
static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
B
bellard 已提交
441
{
442
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
443
#ifdef TARGET_X86_64
444 445 446 447 448 449 450 451 452
    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 已提交
453 454
#endif
}
B
bellard 已提交
455

B
bellard 已提交
456
#ifdef TARGET_X86_64
B
bellard 已提交
457 458
static inline void gen_op_movq_A0_seg(int reg)
{
459
    tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
460
}
B
bellard 已提交
461

B
bellard 已提交
462 463
static inline void gen_op_addq_A0_seg(int reg)
{
464
    tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
B
bellard 已提交
465 466 467 468 469
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}

static inline void gen_op_movq_A0_reg(int reg)
{
470
    tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
B
bellard 已提交
471 472 473 474
}

static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
{
475 476
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
    if (shift != 0)
B
bellard 已提交
477 478 479
        tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
    tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
}
B
bellard 已提交
480 481
#endif

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

487
static inline void gen_op_st_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
B
bellard 已提交
488
{
489
    tcg_gen_qemu_st_tl(t0, a0, s->mem_index, idx | MO_LE);
B
bellard 已提交
490
}
491

492 493 494
static inline void gen_op_st_rm_T0_A0(DisasContext *s, int idx, int d)
{
    if (d == OR_TMP0) {
495
        gen_op_st_v(s, idx, cpu_T[0], cpu_A0);
496 497 498 499 500
    } else {
        gen_op_mov_reg_T0(idx, d);
    }
}

B
bellard 已提交
501 502
static inline void gen_jmp_im(target_ulong pc)
{
B
bellard 已提交
503
    tcg_gen_movi_tl(cpu_tmp0, pc);
504
    tcg_gen_st_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, eip));
B
bellard 已提交
505 506
}

B
bellard 已提交
507 508 509 510 511
static inline void gen_string_movl_A0_ESI(DisasContext *s)
{
    int override;

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

static inline void gen_string_movl_A0_EDI(DisasContext *s)
{
B
bellard 已提交
543 544
#ifdef TARGET_X86_64
    if (s->aflag == 2) {
B
bellard 已提交
545
        gen_op_movq_A0_reg(R_EDI);
B
bellard 已提交
546 547
    } else
#endif
B
bellard 已提交
548 549
    if (s->aflag) {
        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
        }
    } else {
556
        tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_EDI]);
557
        gen_op_addl_A0_seg(s, R_ES);
B
bellard 已提交
558 559 560
    }
}

561
static inline void gen_op_movl_T0_Dshift(TCGMemOp ot)
562
{
563
    tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
564
    tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
B
bellard 已提交
565 566
};

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

598
static void gen_extu(TCGMemOp ot, TCGv reg)
599 600 601 602
{
    gen_ext_tl(reg, reg, ot, false);
}

603
static void gen_exts(TCGMemOp ot, TCGv reg)
604
{
605
    gen_ext_tl(reg, reg, ot, true);
606
}
B
bellard 已提交
607

608 609
static inline void gen_op_jnz_ecx(int size, int label1)
{
610
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
611
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
612
    tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
613 614 615 616
}

static inline void gen_op_jz_ecx(int size, int label1)
{
617
    tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
618
    gen_extu(size + 1, cpu_tmp0);
P
pbrook 已提交
619
    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
620
}
B
bellard 已提交
621

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

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

656
static void gen_check_io(DisasContext *s, TCGMemOp ot, target_ulong cur_eip,
657
                         uint32_t svm_flags)
658
{
659 660 661 662
    int state_saved;
    target_ulong next_eip;

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

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

707 708 709 710 711 712 713 714 715 716 717
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]);
}

718 719 720 721 722 723 724
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]);
}

725 726 727 728 729 730 731 732
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]);
733 734
    tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
    tcg_gen_movi_tl(cpu_cc_srcT, 0);
735 736
}

737 738
/* compute all eflags to cc_src */
static void gen_compute_eflags(DisasContext *s)
739
{
740
    TCGv zero, dst, src1, src2;
741 742
    int live, dead;

743 744 745
    if (s->cc_op == CC_OP_EFLAGS) {
        return;
    }
R
Richard Henderson 已提交
746 747 748 749 750
    if (s->cc_op == CC_OP_CLR) {
        tcg_gen_movi_tl(cpu_cc_src, CC_Z);
        set_cc_op(s, CC_OP_EFLAGS);
        return;
    }
751 752 753 754

    TCGV_UNUSED(zero);
    dst = cpu_cc_dst;
    src1 = cpu_cc_src;
755
    src2 = cpu_cc_src2;
756 757 758

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

773
    gen_update_cc_op(s);
774
    gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
775
    set_cc_op(s, CC_OP_EFLAGS);
776 777 778 779

    if (dead) {
        tcg_temp_free(zero);
    }
780 781
}

782 783 784 785 786 787 788 789 790 791
typedef struct CCPrepare {
    TCGCond cond;
    TCGv reg;
    TCGv reg2;
    target_ulong imm;
    target_ulong mask;
    bool use_reg2;
    bool no_setcond;
} CCPrepare;

792
/* compute eflags.C to reg */
793
static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
794 795
{
    TCGv t0, t1;
796
    int size, shift;
797 798 799

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

    case CC_OP_LOGICB ... CC_OP_LOGICQ:
R
Richard Henderson 已提交
819
    case CC_OP_CLR:
820
        return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
821 822 823

    case CC_OP_INCB ... CC_OP_INCQ:
    case CC_OP_DECB ... CC_OP_DECQ:
824 825
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
                             .mask = -1, .no_setcond = true };
826 827 828 829

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

    case CC_OP_MULB ... CC_OP_MULQ:
835 836
        return (CCPrepare) { .cond = TCG_COND_NE,
                             .reg = cpu_cc_src, .mask = -1 };
837

838 839 840 841 842
    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 };

843 844 845 846 847
    case CC_OP_ADCX:
    case CC_OP_ADCOX:
        return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
                             .mask = -1, .no_setcond = true };

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

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

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

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

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

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

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

    inv = b & 1;
948
    jcc_op = (b >> 1) & 7;
949 950

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

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

976
        default:
977
            goto slow_jcc;
978
        }
979
        break;
980

981 982
    default:
    slow_jcc:
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
        /* 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;
        }
1027
        break;
1028
    }
1029 1030 1031 1032 1033

    if (inv) {
        cc.cond = tcg_invert_cond(cc.cond);
    }
    return cc;
1034 1035
}

1036 1037 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
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);
}
1070

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

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

B
bellard 已提交
1108 1109 1110
/* 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 已提交
1111
{
B
bellard 已提交
1112 1113 1114 1115
    int l1, l2;

    l1 = gen_new_label();
    l2 = gen_new_label();
1116
    gen_op_jnz_ecx(s->aflag, l1);
B
bellard 已提交
1117 1118 1119 1120
    gen_set_label(l2);
    gen_jmp_tb(s, next_eip, 1);
    gen_set_label(l1);
    return l2;
B
bellard 已提交
1121 1122
}

1123
static inline void gen_stos(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1124
{
1125
    gen_op_mov_TN_reg(MO_32, 0, R_EAX);
B
bellard 已提交
1126
    gen_string_movl_A0_EDI(s);
1127
    gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
1128
    gen_op_movl_T0_Dshift(ot);
1129
    gen_op_add_reg_T0(s->aflag + 1, R_EDI);
B
bellard 已提交
1130 1131
}

1132
static inline void gen_lods(DisasContext *s, TCGMemOp ot)
B
bellard 已提交
1133 1134
{
    gen_string_movl_A0_ESI(s);
1135
    gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1136
    gen_op_mov_reg_T0(ot, R_EAX);
1137
    gen_op_movl_T0_Dshift(ot);
1138
    gen_op_add_reg_T0(s->aflag + 1, R_ESI);
B
bellard 已提交
1139 1140
}

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

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

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

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

1187
    tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1188 1189
    tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
    tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
P
pbrook 已提交
1190
    gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1191

1192
    gen_op_movl_T0_Dshift(ot);
1193
    gen_op_add_reg_T0(s->aflag + 1, R_ESI);
P
pbrook 已提交
1194 1195
    if (use_icount)
        gen_io_end();
B
bellard 已提交
1196 1197 1198 1199 1200
}

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

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

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

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

B
bellard 已提交
1364
/* if d == OR_TMP0, it means memory operand (address in A0) */
1365
static void gen_inc(DisasContext *s1, TCGMemOp ot, int d, int c)
B
bellard 已提交
1366
{
1367
    if (d != OR_TMP0) {
B
bellard 已提交
1368
        gen_op_mov_TN_reg(ot, 0, d);
1369 1370 1371
    } else {
        gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
    }
1372
    gen_compute_eflags_c(s1, cpu_cc_src);
B
bellard 已提交
1373
    if (c > 0) {
1374
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
1375
        set_cc_op(s1, CC_OP_INCB + ot);
B
bellard 已提交
1376
    } else {
1377
        tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
1378
        set_cc_op(s1, CC_OP_DECB + ot);
B
bellard 已提交
1379
    }
1380
    gen_op_st_rm_T0_A0(s1, ot, d);
B
bellard 已提交
1381
    tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
B
bellard 已提交
1382 1383
}

1384 1385
static void gen_shift_flags(DisasContext *s, TCGMemOp ot, TCGv result,
                            TCGv shm1, TCGv count, bool is_right)
1386 1387 1388 1389 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
{
    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);
}

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

1434
    /* load */
1435
    if (op1 == OR_TMP0) {
1436
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1437
    } else {
1438
        gen_op_mov_TN_reg(ot, 0, op1);
1439
    }
1440

1441 1442
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
    tcg_gen_subi_tl(cpu_tmp0, cpu_T[1], 1);
1443 1444 1445

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

    /* store */
1460
    gen_op_st_rm_T0_A0(s, ot, op1);
1461

1462
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, cpu_T[1], is_right);
1463 1464
}

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

    /* load */
    if (op1 == OR_TMP0)
1472
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
1473 1474 1475 1476 1477 1478 1479 1480
    else
        gen_op_mov_TN_reg(ot, 0, op1);

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

    /* store */
1495 1496
    gen_op_st_rm_T0_A0(s, ot, op1);

B
bellard 已提交
1497 1498
    /* update eflags if non zero shift */
    if (op2 != 0) {
B
bellard 已提交
1499
        tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
B
bellard 已提交
1500
        tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
1501
        set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
B
bellard 已提交
1502 1503 1504
    }
}

1505 1506 1507 1508 1509 1510 1511 1512
static inline void tcg_gen_lshift(TCGv ret, TCGv arg1, target_long arg2)
{
    if (arg2 >= 0)
        tcg_gen_shli_tl(ret, arg1, arg2);
    else
        tcg_gen_shri_tl(ret, arg1, -arg2);
}

1513
static void gen_rot_rm_T1(DisasContext *s, TCGMemOp ot, int op1, int is_right)
1514
{
1515
    target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1516
    TCGv_i32 t0, t1;
1517 1518

    /* load */
1519
    if (op1 == OR_TMP0) {
1520
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1521
    } else {
1522
        gen_op_mov_TN_reg(ot, 0, op1);
1523
    }
1524

1525
    tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
1526

1527
    switch (ot) {
1528
    case MO_8:
1529 1530 1531 1532
        /* 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;
1533
    case MO_16:
1534 1535 1536 1537 1538
        /* 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
1539
    case MO_32:
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
        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;
1557 1558 1559
    }

    /* store */
1560
    gen_op_st_rm_T0_A0(s, ot, op1);
1561

1562 1563
    /* We'll need the flags computed into CC_SRC.  */
    gen_compute_eflags(s);
1564

1565 1566 1567 1568
    /* 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.  */
1569
    if (is_right) {
1570 1571
        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 已提交
1572
        tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1573 1574 1575
    } else {
        tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
        tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
1576
    }
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
    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);
1596 1597
}

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

    /* load */
    if (op1 == OR_TMP0) {
1606
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
M
malc 已提交
1607
    } else {
1608
        gen_op_mov_TN_reg(ot, 0, op1);
M
malc 已提交
1609 1610 1611 1612
    }

    op2 &= mask;
    if (op2 != 0) {
1613 1614
        switch (ot) {
#ifdef TARGET_X86_64
1615
        case MO_32:
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
            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;
1632
        case MO_8:
1633 1634
            mask = 7;
            goto do_shifts;
1635
        case MO_16:
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
            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 已提交
1647 1648 1649 1650
        }
    }

    /* store */
1651
    gen_op_st_rm_T0_A0(s, ot, op1);
M
malc 已提交
1652 1653

    if (op2 != 0) {
1654
        /* Compute the flags into CC_SRC.  */
1655
        gen_compute_eflags(s);
1656

1657 1658 1659 1660
        /* 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 已提交
1661
        if (is_right) {
1662 1663
            tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
            tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
1664
            tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1665 1666 1667
        } 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 已提交
1668
        }
1669 1670 1671
        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 已提交
1672 1673 1674
    }
}

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

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

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

    /* load */
1739
    if (op1 == OR_TMP0) {
1740
        gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1741
    } else {
1742
        gen_op_mov_TN_reg(ot, 0, op1);
1743
    }
1744

1745 1746
    count = tcg_temp_new();
    tcg_gen_andi_tl(count, count_in, mask);
1747

1748
    switch (ot) {
1749
    case MO_16:
1750 1751 1752
        /* 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.  */
1753
        if (is_right) {
1754 1755 1756
            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);
1757
        } else {
1758
            tcg_gen_deposit_tl(cpu_T[1], cpu_T[0], cpu_T[1], 16, 16);
1759
        }
1760 1761
        /* FALLTHRU */
#ifdef TARGET_X86_64
1762
    case MO_32:
1763 1764
        /* Concatenate the two 32-bit values and use a 64-bit shift.  */
        tcg_gen_subi_tl(cpu_tmp0, count, 1);
1765
        if (is_right) {
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
            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);
1782

1783 1784 1785
            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);
1786
        } else {
1787
            tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1788
            if (ot == MO_16) {
1789 1790 1791 1792 1793 1794 1795 1796 1797
                /* 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);
1798
        }
1799 1800 1801 1802 1803
        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;
1804 1805 1806
    }

    /* store */
1807
    gen_op_st_rm_T0_A0(s, ot, op1);
1808

1809 1810
    gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, count, is_right);
    tcg_temp_free(count);
1811 1812
}

1813
static void gen_shift(DisasContext *s1, int op, TCGMemOp ot, int d, int s)
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840
{
    if (s != OR_TMP1)
        gen_op_mov_TN_reg(ot, 1, s);
    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 已提交
1841 1842
}

1843
static void gen_shifti(DisasContext *s1, int op, TCGMemOp ot, int d, int c)
B
bellard 已提交
1844
{
B
bellard 已提交
1845
    switch(op) {
M
malc 已提交
1846 1847 1848 1849 1850 1851
    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 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
    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 */
1864
        tcg_gen_movi_tl(cpu_T[1], c);
B
bellard 已提交
1865 1866 1867
        gen_shift(s1, op, ot, d, OR_TMP1);
        break;
    }
B
bellard 已提交
1868 1869
}

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

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

    if (s->aflag) {
        havesib = 0;
        base = rm;
1890
        index = -1;
B
bellard 已提交
1891
        scale = 0;
1892

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

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

1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
        /* 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 已提交
1941
            }
1942 1943 1944
            if (base >= 0) {
                tcg_gen_add_tl(cpu_A0, sum, cpu_regs[base]);
                sum = cpu_A0;
B
bellard 已提交
1945
            }
1946 1947
        } else if (base >= 0) {
            sum = cpu_regs[base];
B
bellard 已提交
1948
        }
1949 1950 1951 1952
        if (TCGV_IS_UNUSED(sum)) {
            tcg_gen_movi_tl(cpu_A0, disp);
        } else {
            tcg_gen_addi_tl(cpu_A0, sum, disp);
B
bellard 已提交
1953
        }
1954

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

            tcg_gen_ld_tl(cpu_tmp0, cpu_env,
                          offsetof(CPUX86State, segs[override].base));
            if (CODE64(s)) {
                if (s->aflag != 2) {
                    tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
                }
                tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
1971
                return;
B
bellard 已提交
1972
            }
1973 1974 1975 1976 1977 1978

            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
        }

        if (s->aflag != 2) {
            tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
1979 1980 1981 1982 1983
        }
    } else {
        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 = cpu_lduw_code(env, s->pc);
B
bellard 已提交
1999 2000 2001 2002 2003
            s->pc += 2;
            break;
        }
        switch(rm) {
        case 0:
B
bellard 已提交
2004 2005
            gen_op_movl_A0_reg(R_EBX);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
2006 2007
            break;
        case 1:
B
bellard 已提交
2008 2009
            gen_op_movl_A0_reg(R_EBX);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
2010 2011
            break;
        case 2:
B
bellard 已提交
2012 2013
            gen_op_movl_A0_reg(R_EBP);
            gen_op_addl_A0_reg_sN(0, R_ESI);
B
bellard 已提交
2014 2015
            break;
        case 3:
B
bellard 已提交
2016 2017
            gen_op_movl_A0_reg(R_EBP);
            gen_op_addl_A0_reg_sN(0, R_EDI);
B
bellard 已提交
2018 2019
            break;
        case 4:
B
bellard 已提交
2020
            gen_op_movl_A0_reg(R_ESI);
B
bellard 已提交
2021 2022
            break;
        case 5:
B
bellard 已提交
2023
            gen_op_movl_A0_reg(R_EDI);
B
bellard 已提交
2024 2025
            break;
        case 6:
B
bellard 已提交
2026
            gen_op_movl_A0_reg(R_EBP);
B
bellard 已提交
2027 2028 2029
            break;
        default:
        case 7:
B
bellard 已提交
2030
            gen_op_movl_A0_reg(R_EBX);
B
bellard 已提交
2031 2032 2033 2034
            break;
        }
        if (disp != 0)
            gen_op_addl_A0_im(disp);
2035
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
2036 2037 2038 2039 2040 2041 2042 2043
    no_rm:
        if (must_add_seg) {
            if (override < 0) {
                if (rm == 2 || rm == 3 || rm == 6)
                    override = R_SS;
                else
                    override = R_DS;
            }
2044
            gen_op_addl_A0_seg(s, override);
B
bellard 已提交
2045 2046 2047 2048
        }
    }
}

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

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

    if (s->aflag) {

        base = rm;
2061

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

B
bellard 已提交
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
        switch (mod) {
        case 0:
            if (base == 5) {
                s->pc += 4;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 4;
            break;
        }
    } else {
        switch (mod) {
        case 0:
            if (rm == 6) {
                s->pc += 2;
            }
            break;
        case 1:
            s->pc++;
            break;
        default:
        case 2:
            s->pc += 2;
            break;
        }
    }
}

B
bellard 已提交
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
/* 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) {
2110 2111
#ifdef TARGET_X86_64
        if (CODE64(s)) {
B
bellard 已提交
2112
            gen_op_addq_A0_seg(override);
2113
        } else
2114 2115
#endif
        {
2116
            gen_op_addl_A0_seg(s, override);
2117
        }
B
bellard 已提交
2118 2119 2120
    }
}

B
balrog 已提交
2121
/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
B
bellard 已提交
2122
   OR_TMP0 */
2123
static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
2124
                           TCGMemOp ot, int reg, int is_store)
B
bellard 已提交
2125
{
2126
    int mod, rm;
B
bellard 已提交
2127 2128

    mod = (modrm >> 6) & 3;
B
bellard 已提交
2129
    rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
2130 2131 2132
    if (mod == 3) {
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2133 2134
                gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
2135
        } else {
B
bellard 已提交
2136
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
2137
            if (reg != OR_TMP0)
B
bellard 已提交
2138
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2139 2140
        }
    } else {
2141
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
2142 2143
        if (is_store) {
            if (reg != OR_TMP0)
B
bellard 已提交
2144
                gen_op_mov_TN_reg(ot, 0, reg);
2145
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2146
        } else {
2147
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
2148
            if (reg != OR_TMP0)
B
bellard 已提交
2149
                gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
2150 2151 2152 2153
        }
    }
}

2154
static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, TCGMemOp ot)
B
bellard 已提交
2155 2156 2157
{
    uint32_t ret;

2158
    switch (ot) {
2159
    case MO_8:
2160
        ret = cpu_ldub_code(env, s->pc);
B
bellard 已提交
2161 2162
        s->pc++;
        break;
2163
    case MO_16:
2164
        ret = cpu_lduw_code(env, s->pc);
B
bellard 已提交
2165 2166
        s->pc += 2;
        break;
2167
    case MO_32:
2168 2169 2170
#ifdef TARGET_X86_64
    case MO_64:
#endif
2171
        ret = cpu_ldl_code(env, s->pc);
B
bellard 已提交
2172 2173
        s->pc += 4;
        break;
2174 2175
    default:
        tcg_abort();
B
bellard 已提交
2176 2177 2178 2179
    }
    return ret;
}

2180
static inline int insn_const_size(TCGMemOp ot)
B
bellard 已提交
2181
{
2182
    if (ot <= MO_32) {
B
bellard 已提交
2183
        return 1 << ot;
2184
    } else {
B
bellard 已提交
2185
        return 4;
2186
    }
B
bellard 已提交
2187 2188
}

2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
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 已提交
2200
        tcg_gen_goto_tb(tb_num);
2201
        gen_jmp_im(eip);
2202
        tcg_gen_exit_tb((uintptr_t)tb + tb_num);
2203 2204 2205 2206 2207 2208 2209
    } else {
        /* jump to another page: currently not optimized */
        gen_jmp_im(eip);
        gen_eob(s);
    }
}

2210
static inline void gen_jcc(DisasContext *s, int b,
B
bellard 已提交
2211
                           target_ulong val, target_ulong next_eip)
B
bellard 已提交
2212
{
2213
    int l1, l2;
2214

B
bellard 已提交
2215
    if (s->jmp_opt) {
B
bellard 已提交
2216
        l1 = gen_new_label();
2217
        gen_jcc1(s, b, l1);
2218

2219
        gen_goto_tb(s, 0, next_eip);
B
bellard 已提交
2220 2221

        gen_set_label(l1);
2222
        gen_goto_tb(s, 1, val);
J
Jun Koi 已提交
2223
        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2224
    } else {
B
bellard 已提交
2225 2226
        l1 = gen_new_label();
        l2 = gen_new_label();
2227
        gen_jcc1(s, b, l1);
2228

B
bellard 已提交
2229
        gen_jmp_im(next_eip);
2230 2231
        tcg_gen_br(l2);

B
bellard 已提交
2232 2233 2234
        gen_set_label(l1);
        gen_jmp_im(val);
        gen_set_label(l2);
B
bellard 已提交
2235 2236 2237 2238
        gen_eob(s);
    }
}

2239
static void gen_cmovcc1(CPUX86State *env, DisasContext *s, TCGMemOp ot, int b,
2240 2241
                        int modrm, int reg)
{
2242
    CCPrepare cc;
2243

2244
    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2245

2246 2247 2248 2249 2250 2251 2252 2253
    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);
2254 2255
    }

2256 2257 2258 2259 2260 2261 2262 2263 2264 2265
    tcg_gen_movcond_tl(cc.cond, cpu_T[0], cc.reg, cc.reg2,
                       cpu_T[0], cpu_regs[reg]);
    gen_op_mov_reg_T0(ot, reg);

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

2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
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 已提交
2284 2285
/* move T0 to seg_reg and compute if the CPU state may change. Never
   call this function with seg_reg == R_CS */
B
bellard 已提交
2286
static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
B
bellard 已提交
2287
{
2288 2289
    if (s->pe && !s->vm86) {
        /* XXX: optimize by finding processor state dynamically */
2290
        gen_update_cc_op(s);
B
bellard 已提交
2291
        gen_jmp_im(cur_eip);
2292
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2293
        gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
B
bellard 已提交
2294 2295 2296 2297 2298
        /* 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 已提交
2299
            s->is_jmp = DISAS_TB_JUMP;
2300
    } else {
2301
        gen_op_movl_seg_T0_vm(seg_reg);
B
bellard 已提交
2302
        if (seg_reg == R_SS)
J
Jun Koi 已提交
2303
            s->is_jmp = DISAS_TB_JUMP;
2304
    }
B
bellard 已提交
2305 2306
}

T
ths 已提交
2307 2308 2309 2310 2311
static inline int svm_is_rep(int prefixes)
{
    return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
}

B
bellard 已提交
2312
static inline void
T
ths 已提交
2313
gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2314
                              uint32_t type, uint64_t param)
T
ths 已提交
2315
{
B
bellard 已提交
2316 2317 2318
    /* no SVM activated; fast case */
    if (likely(!(s->flags & HF_SVMI_MASK)))
        return;
2319
    gen_update_cc_op(s);
B
bellard 已提交
2320
    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
2321
    gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
P
pbrook 已提交
2322
                                         tcg_const_i64(param));
T
ths 已提交
2323 2324
}

B
bellard 已提交
2325
static inline void
T
ths 已提交
2326 2327
gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
{
B
bellard 已提交
2328
    gen_svm_check_intercept_param(s, pc_start, type, 0);
T
ths 已提交
2329 2330
}

2331 2332
static inline void gen_stack_update(DisasContext *s, int addend)
{
B
bellard 已提交
2333 2334
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2335
        gen_op_add_reg_im(MO_64, R_ESP, addend);
B
bellard 已提交
2336 2337
    } else
#endif
2338
    if (s->ss32) {
2339
        gen_op_add_reg_im(MO_32, R_ESP, addend);
2340
    } else {
2341
        gen_op_add_reg_im(MO_16, R_ESP, addend);
2342 2343 2344
    }
}

B
bellard 已提交
2345 2346 2347
/* generate a push. It depends on ss32, addseg and dflag */
static void gen_push_T0(DisasContext *s)
{
B
bellard 已提交
2348 2349
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2350
        gen_op_movq_A0_reg(R_ESP);
2351
        if (s->dflag) {
B
bellard 已提交
2352
            gen_op_addq_A0_im(-8);
2353
            gen_op_st_v(s, MO_64, cpu_T[0], cpu_A0);
2354
        } else {
B
bellard 已提交
2355
            gen_op_addq_A0_im(-2);
2356
            gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
2357
        }
B
bellard 已提交
2358
        gen_op_mov_reg_A0(2, R_ESP);
2359
    } else
B
bellard 已提交
2360 2361
#endif
    {
B
bellard 已提交
2362
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2363
        if (!s->dflag)
B
bellard 已提交
2364
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2365
        else
B
bellard 已提交
2366
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2367 2368
        if (s->ss32) {
            if (s->addseg) {
2369
                tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2370
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2371 2372
            }
        } else {
2373
            tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2374
            tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2375
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2376
        }
2377
        gen_op_st_v(s, s->dflag + 1, cpu_T[0], cpu_A0);
B
bellard 已提交
2378
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2379
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2380
        else
B
bellard 已提交
2381
            gen_op_mov_reg_T1(s->ss32 + 1, R_ESP);
B
bellard 已提交
2382 2383 2384
    }
}

2385 2386 2387
/* generate a push. It depends on ss32, addseg and dflag */
/* slower version for T1, only used for call Ev */
static void gen_push_T1(DisasContext *s)
B
bellard 已提交
2388
{
B
bellard 已提交
2389 2390
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2391
        gen_op_movq_A0_reg(R_ESP);
2392
        if (s->dflag) {
B
bellard 已提交
2393
            gen_op_addq_A0_im(-8);
2394
            gen_op_st_v(s, MO_64, cpu_T[1], cpu_A0);
2395
        } else {
B
bellard 已提交
2396
            gen_op_addq_A0_im(-2);
2397
            gen_op_st_v(s, MO_16, cpu_T[1], cpu_A0);
2398
        }
B
bellard 已提交
2399
        gen_op_mov_reg_A0(2, R_ESP);
2400
    } else
B
bellard 已提交
2401 2402
#endif
    {
B
bellard 已提交
2403
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2404
        if (!s->dflag)
B
bellard 已提交
2405
            gen_op_addl_A0_im(-2);
B
bellard 已提交
2406
        else
B
bellard 已提交
2407
            gen_op_addl_A0_im(-4);
B
bellard 已提交
2408 2409
        if (s->ss32) {
            if (s->addseg) {
2410
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2411 2412
            }
        } else {
2413
            tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2414
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2415
        }
2416
        gen_op_st_v(s, s->dflag + 1, cpu_T[1], cpu_A0);
2417

B
bellard 已提交
2418
        if (s->ss32 && !s->addseg)
B
bellard 已提交
2419
            gen_op_mov_reg_A0(1, R_ESP);
B
bellard 已提交
2420 2421
        else
            gen_stack_update(s, (-2) << s->dflag);
B
bellard 已提交
2422 2423 2424
    }
}

2425 2426
/* two step pop is necessary for precise exceptions */
static void gen_pop_T0(DisasContext *s)
B
bellard 已提交
2427
{
B
bellard 已提交
2428 2429
#ifdef TARGET_X86_64
    if (CODE64(s)) {
B
bellard 已提交
2430
        gen_op_movq_A0_reg(R_ESP);
2431
        gen_op_ld_v(s, s->dflag ? MO_64 : MO_16, cpu_T[0], cpu_A0);
2432
    } else
B
bellard 已提交
2433 2434
#endif
    {
B
bellard 已提交
2435
        gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2436 2437
        if (s->ss32) {
            if (s->addseg)
2438
                gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2439
        } else {
2440
            tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2441
            gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2442
        }
2443
        gen_op_ld_v(s, s->dflag + 1, cpu_T[0], cpu_A0);
B
bellard 已提交
2444 2445 2446 2447 2448
    }
}

static void gen_pop_update(DisasContext *s)
{
B
bellard 已提交
2449
#ifdef TARGET_X86_64
2450
    if (CODE64(s) && s->dflag) {
B
bellard 已提交
2451 2452 2453 2454 2455 2456
        gen_stack_update(s, 8);
    } else
#endif
    {
        gen_stack_update(s, 2 << s->dflag);
    }
B
bellard 已提交
2457 2458 2459 2460
}

static void gen_stack_A0(DisasContext *s)
{
B
bellard 已提交
2461
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2462
    if (!s->ss32)
2463
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2464
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2465
    if (s->addseg)
2466
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2467 2468 2469 2470 2471 2472
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_pusha(DisasContext *s)
{
    int i;
B
bellard 已提交
2473
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2474 2475
    gen_op_addl_A0_im(-16 <<  s->dflag);
    if (!s->ss32)
2476
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2477
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
B
bellard 已提交
2478
    if (s->addseg)
2479
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2480
    for(i = 0;i < 8; i++) {
2481
        gen_op_mov_TN_reg(MO_32, 0, 7 - i);
2482
        gen_op_st_v(s, MO_16 + s->dflag, cpu_T[0], cpu_A0);
B
bellard 已提交
2483 2484
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
2485
    gen_op_mov_reg_T1(MO_16 + s->ss32, R_ESP);
B
bellard 已提交
2486 2487 2488 2489 2490 2491
}

/* NOTE: wrap around in 16 bit not fully handled */
static void gen_popa(DisasContext *s)
{
    int i;
B
bellard 已提交
2492
    gen_op_movl_A0_reg(R_ESP);
B
bellard 已提交
2493
    if (!s->ss32)
2494
        tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2495 2496
    tcg_gen_mov_tl(cpu_T[1], cpu_A0);
    tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 16 <<  s->dflag);
B
bellard 已提交
2497
    if (s->addseg)
2498
        gen_op_addl_A0_seg(s, R_SS);
B
bellard 已提交
2499 2500 2501
    for(i = 0;i < 8; i++) {
        /* ESP is not reloaded */
        if (i != 3) {
2502
            gen_op_ld_v(s, MO_16 + s->dflag, cpu_T[0], cpu_A0);
2503
            gen_op_mov_reg_T0(MO_16 + s->dflag, 7 - i);
B
bellard 已提交
2504 2505 2506
        }
        gen_op_addl_A0_im(2 <<  s->dflag);
    }
2507
    gen_op_mov_reg_T1(MO_16 + s->ss32, R_ESP);
B
bellard 已提交
2508 2509 2510 2511
}

static void gen_enter(DisasContext *s, int esp_addend, int level)
{
2512 2513
    TCGMemOp ot;
    int opsize;
B
bellard 已提交
2514 2515

    level &= 0x1f;
2516 2517
#ifdef TARGET_X86_64
    if (CODE64(s)) {
2518
        ot = s->dflag ? MO_64 : MO_16;
2519
        opsize = 1 << ot;
2520

B
bellard 已提交
2521
        gen_op_movl_A0_reg(R_ESP);
2522
        gen_op_addq_A0_im(-opsize);
2523
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2524 2525

        /* push bp */
2526
        gen_op_mov_TN_reg(MO_32, 0, R_EBP);
2527
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2528
        if (level) {
B
bellard 已提交
2529
            /* XXX: must save state */
2530
            gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
2531
                                     tcg_const_i32((ot == MO_64)),
P
pbrook 已提交
2532
                                     cpu_T[1]);
2533
        }
B
bellard 已提交
2534
        gen_op_mov_reg_T1(ot, R_EBP);
2535
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2536
        gen_op_mov_reg_T1(MO_64, R_ESP);
2537
    } else
2538 2539
#endif
    {
2540
        ot = s->dflag + MO_16;
2541
        opsize = 2 << s->dflag;
2542

B
bellard 已提交
2543
        gen_op_movl_A0_reg(R_ESP);
2544 2545
        gen_op_addl_A0_im(-opsize);
        if (!s->ss32)
2546
            tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2547
        tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2548
        if (s->addseg)
2549
            gen_op_addl_A0_seg(s, R_SS);
2550
        /* push bp */
2551
        gen_op_mov_TN_reg(MO_32, 0, R_EBP);
2552
        gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2553
        if (level) {
B
bellard 已提交
2554
            /* XXX: must save state */
2555
            gen_helper_enter_level(cpu_env, tcg_const_i32(level),
P
pbrook 已提交
2556 2557
                                   tcg_const_i32(s->dflag),
                                   cpu_T[1]);
2558
        }
B
bellard 已提交
2559
        gen_op_mov_reg_T1(ot, R_EBP);
2560
        tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
2561
        gen_op_mov_reg_T1(MO_16 + s->ss32, R_ESP);
B
bellard 已提交
2562 2563 2564
    }
}

B
bellard 已提交
2565
static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
B
bellard 已提交
2566
{
2567
    gen_update_cc_op(s);
B
bellard 已提交
2568
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2569
    gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
J
Jun Koi 已提交
2570
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2571 2572 2573
}

/* an interrupt is different from an exception because of the
B
blueswir1 已提交
2574
   privilege checks */
2575
static void gen_interrupt(DisasContext *s, int intno,
B
bellard 已提交
2576
                          target_ulong cur_eip, target_ulong next_eip)
B
bellard 已提交
2577
{
2578
    gen_update_cc_op(s);
B
bellard 已提交
2579
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2580
    gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
P
pbrook 已提交
2581
                               tcg_const_i32(next_eip - cur_eip));
J
Jun Koi 已提交
2582
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2583 2584
}

B
bellard 已提交
2585
static void gen_debug(DisasContext *s, target_ulong cur_eip)
B
bellard 已提交
2586
{
2587
    gen_update_cc_op(s);
B
bellard 已提交
2588
    gen_jmp_im(cur_eip);
B
Blue Swirl 已提交
2589
    gen_helper_debug(cpu_env);
J
Jun Koi 已提交
2590
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2591 2592 2593 2594 2595 2596
}

/* generate a generic end of block. Trace exception is also generated
   if needed */
static void gen_eob(DisasContext *s)
{
2597
    gen_update_cc_op(s);
2598
    if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
2599
        gen_helper_reset_inhibit_irq(cpu_env);
2600
    }
J
Jan Kiszka 已提交
2601
    if (s->tb->flags & HF_RF_MASK) {
2602
        gen_helper_reset_rf(cpu_env);
J
Jan Kiszka 已提交
2603
    }
2604
    if (s->singlestep_enabled) {
B
Blue Swirl 已提交
2605
        gen_helper_debug(cpu_env);
2606
    } else if (s->tf) {
B
Blue Swirl 已提交
2607
        gen_helper_single_step(cpu_env);
B
bellard 已提交
2608
    } else {
B
bellard 已提交
2609
        tcg_gen_exit_tb(0);
B
bellard 已提交
2610
    }
J
Jun Koi 已提交
2611
    s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
2612 2613 2614 2615
}

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

B
bellard 已提交
2629 2630 2631 2632 2633
static void gen_jmp(DisasContext *s, target_ulong eip)
{
    gen_jmp_tb(s, eip, 0);
}

2634
static inline void gen_ldq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2635
{
2636
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
2637
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
B
bellard 已提交
2638
}
B
bellard 已提交
2639

2640
static inline void gen_stq_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2641
{
2642
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2643
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
B
bellard 已提交
2644
}
B
bellard 已提交
2645

2646
static inline void gen_ldo_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2647
{
2648
    int mem_index = s->mem_index;
2649
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
2650
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
B
bellard 已提交
2651
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2652
    tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
2653
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
B
bellard 已提交
2654
}
B
bellard 已提交
2655

2656
static inline void gen_sto_env_A0(DisasContext *s, int offset)
B
bellard 已提交
2657
{
2658
    int mem_index = s->mem_index;
2659
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
2660
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
B
bellard 已提交
2661
    tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2662
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
2663
    tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
B
bellard 已提交
2664
}
B
bellard 已提交
2665

B
bellard 已提交
2666 2667
static inline void gen_op_movo(int d_offset, int s_offset)
{
2668 2669 2670 2671
    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 已提交
2672 2673 2674 2675
}

static inline void gen_op_movq(int d_offset, int s_offset)
{
2676 2677
    tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2678 2679 2680 2681
}

static inline void gen_op_movl(int d_offset, int s_offset)
{
2682 2683
    tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
    tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
B
bellard 已提交
2684 2685 2686 2687
}

static inline void gen_op_movq_env_0(int d_offset)
{
2688 2689
    tcg_gen_movi_i64(cpu_tmp1_i64, 0);
    tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
B
bellard 已提交
2690
}
B
bellard 已提交
2691

B
Blue Swirl 已提交
2692 2693 2694 2695 2696 2697 2698
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 已提交
2699
typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
B
Blue Swirl 已提交
2700 2701
typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
                               TCGv val);
B
Blue Swirl 已提交
2702

B
bellard 已提交
2703 2704
#define SSE_SPECIAL ((void *)1)
#define SSE_DUMMY ((void *)2)
B
bellard 已提交
2705

P
pbrook 已提交
2706 2707 2708
#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 已提交
2709

B
Blue Swirl 已提交
2710
static const SSEFunc_0_epp sse_op_table1[256][4] = {
A
aurel32 已提交
2711 2712 2713
    /* 3DNow! extensions */
    [0x0e] = { SSE_DUMMY }, /* femms */
    [0x0f] = { SSE_DUMMY }, /* pf... */
B
bellard 已提交
2714 2715 2716
    /* 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 已提交
2717
    [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
B
bellard 已提交
2718
    [0x13] = { SSE_SPECIAL, SSE_SPECIAL },  /* movlps, movlpd */
P
pbrook 已提交
2719 2720
    [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
    [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2721 2722 2723 2724 2725 2726
    [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 */
2727
    [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
B
bellard 已提交
2728 2729
    [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 已提交
2730 2731
    [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
    [0x2f] = { gen_helper_comiss, gen_helper_comisd },
B
bellard 已提交
2732 2733
    [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
    [0x51] = SSE_FOP(sqrt),
P
pbrook 已提交
2734 2735 2736 2737 2738 2739
    [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 已提交
2740 2741
    [0x58] = SSE_FOP(add),
    [0x59] = SSE_FOP(mul),
P
pbrook 已提交
2742 2743 2744
    [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 已提交
2745 2746 2747 2748 2749 2750
    [0x5c] = SSE_FOP(sub),
    [0x5d] = SSE_FOP(min),
    [0x5e] = SSE_FOP(div),
    [0x5f] = SSE_FOP(max),

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

R
Richard Henderson 已提交
2754 2755 2756
    /* 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 已提交
2757

B
bellard 已提交
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
    /* 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 已提交
2771 2772
    [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
    [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
B
bellard 已提交
2773 2774
    [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
    [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
B
Blue Swirl 已提交
2775 2776 2777 2778
    [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 已提交
2779 2780 2781 2782 2783 2784
    [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 已提交
2785
    [0x77] = { SSE_DUMMY }, /* emms */
2786 2787
    [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
    [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
P
pbrook 已提交
2788 2789
    [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
    [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
B
bellard 已提交
2790 2791 2792 2793
    [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 已提交
2794
    [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
B
bellard 已提交
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
    [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 已提交
2816
    [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
B
bellard 已提交
2817 2818 2819 2820 2821 2822 2823 2824 2825
    [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 已提交
2826
    [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
B
bellard 已提交
2827 2828 2829 2830 2831 2832
    [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 已提交
2833 2834
    [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
               (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
B
bellard 已提交
2835 2836 2837 2838 2839 2840 2841 2842 2843
    [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 已提交
2844
static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
B
bellard 已提交
2845 2846 2847 2848 2849 2850 2851
    [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 已提交
2852
    [16 + 3] = { NULL, gen_helper_psrldq_xmm },
B
bellard 已提交
2853
    [16 + 6] = MMX_OP2(psllq),
P
pbrook 已提交
2854
    [16 + 7] = { NULL, gen_helper_pslldq_xmm },
B
bellard 已提交
2855 2856
};

B
Blue Swirl 已提交
2857
static const SSEFunc_0_epi sse_op_table3ai[] = {
P
pbrook 已提交
2858
    gen_helper_cvtsi2ss,
2859
    gen_helper_cvtsi2sd
B
Blue Swirl 已提交
2860
};
P
pbrook 已提交
2861

2862
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2863
static const SSEFunc_0_epl sse_op_table3aq[] = {
2864 2865 2866 2867 2868
    gen_helper_cvtsq2ss,
    gen_helper_cvtsq2sd
};
#endif

B
Blue Swirl 已提交
2869
static const SSEFunc_i_ep sse_op_table3bi[] = {
P
pbrook 已提交
2870 2871
    gen_helper_cvttss2si,
    gen_helper_cvtss2si,
2872
    gen_helper_cvttsd2si,
2873
    gen_helper_cvtsd2si
B
bellard 已提交
2874
};
2875

2876
#ifdef TARGET_X86_64
B
Blue Swirl 已提交
2877
static const SSEFunc_l_ep sse_op_table3bq[] = {
2878 2879
    gen_helper_cvttss2sq,
    gen_helper_cvtss2sq,
2880
    gen_helper_cvttsd2sq,
2881 2882 2883 2884
    gen_helper_cvtsd2sq
};
#endif

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

B
Blue Swirl 已提交
2896
static const SSEFunc_0_epp sse_op_table5[256] = {
P
pbrook 已提交
2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
    [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 已提交
2921 2922
};

B
Blue Swirl 已提交
2923 2924
struct SSEOpHelper_epp {
    SSEFunc_0_epp op[2];
B
Blue Swirl 已提交
2925 2926 2927
    uint32_t ext_mask;
};

B
Blue Swirl 已提交
2928 2929
struct SSEOpHelper_eppi {
    SSEFunc_0_eppi op[2];
B
Blue Swirl 已提交
2930
    uint32_t ext_mask;
B
balrog 已提交
2931
};
B
Blue Swirl 已提交
2932

B
balrog 已提交
2933
#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
P
pbrook 已提交
2934 2935
#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 已提交
2936
#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2937 2938
#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
        CPUID_EXT_PCLMULQDQ }
2939
#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
B
Blue Swirl 已提交
2940

B
Blue Swirl 已提交
2941
static const struct SSEOpHelper_epp sse_op_table6[256] = {
B
balrog 已提交
2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
    [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),
2988 2989 2990 2991 2992
    [0xdb] = AESNI_OP(aesimc),
    [0xdc] = AESNI_OP(aesenc),
    [0xdd] = AESNI_OP(aesenclast),
    [0xde] = AESNI_OP(aesdec),
    [0xdf] = AESNI_OP(aesdeclast),
B
balrog 已提交
2993 2994
};

B
Blue Swirl 已提交
2995
static const struct SSEOpHelper_eppi sse_op_table7[256] = {
B
balrog 已提交
2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
    [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),
3014
    [0x44] = PCLMULQDQ_OP(pclmulqdq),
B
balrog 已提交
3015 3016 3017 3018
    [0x60] = SSE42_OP(pcmpestrm),
    [0x61] = SSE42_OP(pcmpestri),
    [0x62] = SSE42_OP(pcmpistrm),
    [0x63] = SSE42_OP(pcmpistri),
3019
    [0xdf] = AESNI_OP(aeskeygenassist),
B
balrog 已提交
3020 3021
};

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

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

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

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

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

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

            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);
3681
                    gen_lea_modrm(env, s, modrm);
B
balrog 已提交
3682 3683 3684 3685
                    switch (b) {
                    case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
                    case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
                    case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3686
                        gen_ldq_env_A0(s, op2_offset +
B
balrog 已提交
3687 3688 3689 3690
                                        offsetof(XMMReg, XMM_Q(0)));
                        break;
                    case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
                    case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3691 3692
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
balrog 已提交
3693 3694 3695 3696
                        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_L(0)));
                        break;
                    case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3697 3698
                        tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
                                           s->mem_index, MO_LEUW);
B
balrog 已提交
3699 3700 3701 3702
                        tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
                                        offsetof(XMMReg, XMM_W(0)));
                        break;
                    case 0x2a:            /* movntqda */
3703
                        gen_ldo_env_A0(s, op1_offset);
B
balrog 已提交
3704 3705
                        return;
                    default:
3706
                        gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
3707
                    }
B
balrog 已提交
3708 3709 3710 3711 3712 3713 3714
                }
            } 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);
3715
                    gen_lea_modrm(env, s, modrm);
3716
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
3717 3718
                }
            }
B
Blue Swirl 已提交
3719
            if (sse_fn_epp == SSE_SPECIAL) {
B
balrog 已提交
3720
                goto illegal_op;
B
Blue Swirl 已提交
3721
            }
B
balrog 已提交
3722

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

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

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

R
Richard Henderson 已提交
3740 3741 3742 3743 3744 3745 3746 3747
            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) {
3748
                    ot = MO_8;
R
Richard Henderson 已提交
3749
                } else if (s->dflag != 2) {
3750
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3751
                } else {
3752
                    ot = MO_64;
R
Richard Henderson 已提交
3753
                }
B
balrog 已提交
3754

3755
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
R
Richard Henderson 已提交
3756 3757 3758
                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 已提交
3759

3760
                ot = (s->dflag == 2) ? MO_64 : MO_32;
R
Richard Henderson 已提交
3761 3762
                gen_op_mov_reg_T0(ot, reg);
                break;
B
balrog 已提交
3763

R
Richard Henderson 已提交
3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778
            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;
                }
                if (s->dflag != 2) {
3779
                    ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
R
Richard Henderson 已提交
3780
                } else {
3781
                    ot = MO_64;
R
Richard Henderson 已提交
3782 3783
                }

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

R
Richard Henderson 已提交
3795 3796 3797 3798 3799 3800
            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;
                }
3801
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
3802 3803 3804 3805 3806 3807 3808
                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]);
                gen_op_mov_reg_T0(ot, reg);
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_LOGICB + ot);
                break;

R
Richard Henderson 已提交
3809 3810 3811 3812 3813 3814
            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;
                }
3815
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
3816 3817 3818 3819 3820 3821 3822 3823 3824
                {
                    TCGv bound, zero;

                    gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                    /* Extract START, and shift the operand.
                       Shifts larger than operand size get zeros.  */
                    tcg_gen_ext8u_tl(cpu_A0, cpu_regs[s->vex_v]);
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_A0);

3825
                    bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848
                    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]);

                    gen_op_mov_reg_T0(ot, reg);
                    gen_op_update1_cc();
                    set_cc_op(s, CC_OP_LOGICB + ot);
                }
                break;

R
Richard Henderson 已提交
3849 3850 3851 3852 3853 3854
            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;
                }
3855
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
3856 3857 3858
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                tcg_gen_ext8u_tl(cpu_T[1], cpu_regs[s->vex_v]);
                {
3859
                    TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
R
Richard Henderson 已提交
3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
                    /* 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);
                gen_op_mov_reg_T0(ot, reg);
                gen_op_update1_cc();
                set_cc_op(s, CC_OP_BMILGB + ot);
                break;

R
Richard Henderson 已提交
3876 3877 3878 3879 3880 3881
            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;
                }
3882
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
3883 3884 3885
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                switch (ot) {
                default:
3886 3887 3888 3889 3890 3891
                    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 已提交
3892 3893
                    break;
#ifdef TARGET_X86_64
3894
                case MO_64:
3895 3896
                    tcg_gen_mulu2_i64(cpu_regs[s->vex_v], cpu_regs[reg],
                                      cpu_T[0], cpu_regs[R_EDX]);
R
Richard Henderson 已提交
3897 3898 3899 3900 3901
                    break;
#endif
                }
                break;

3902 3903 3904 3905 3906 3907
            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;
                }
3908
                ot = s->dflag == 2 ? MO_64 : MO_32;
3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925
                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.  */
                if (s->dflag == 2) {
                    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;
                }
3926
                ot = s->dflag == 2 ? MO_64 : MO_32;
3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937
                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.  */
                if (s->dflag == 2) {
                    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;

3938 3939 3940 3941 3942
            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 {
3943
                    TCGv carry_in, carry_out, zero;
3944 3945
                    int end_op;

3946
                    ot = (s->dflag == 2 ? MO_64 : MO_32);
3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973
                    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:
3974
                        end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989
                        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
3990
                    case MO_32:
3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
                        /* 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.  */
4003 4004 4005 4006 4007 4008 4009 4010
                        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);
4011 4012 4013 4014 4015 4016
                        break;
                    }
                    set_cc_op(s, end_op);
                }
                break;

4017 4018 4019 4020 4021 4022 4023 4024
            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;
                }
4025
                ot = (s->dflag == 2 ? MO_64 : MO_32);
4026
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4027
                if (ot == MO_64) {
4028 4029 4030 4031 4032 4033 4034
                    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) {
4035
                    if (ot != MO_64) {
4036 4037 4038 4039
                        tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                } else {
4040
                    if (ot != MO_64) {
4041 4042 4043 4044 4045 4046 4047
                        tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
                    }
                    tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
                }
                gen_op_mov_reg_T0(ot, reg);
                break;

4048 4049 4050 4051 4052 4053 4054 4055 4056
            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;
                }
4057
                ot = s->dflag == 2 ? MO_64 : MO_32;
4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089
                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]);
                    gen_op_mov_reg_T0(ot, s->vex_v);
                    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 已提交
4090 4091 4092
            default:
                goto illegal_op;
            }
B
balrog 已提交
4093
            break;
R
Richard Henderson 已提交
4094

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

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

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

            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);
4254
                    gen_lea_modrm(env, s, modrm);
4255
                    gen_ldo_env_A0(s, op2_offset);
B
balrog 已提交
4256 4257 4258 4259 4260 4261 4262
                }
            } 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);
4263
                    gen_lea_modrm(env, s, modrm);
4264
                    gen_ldq_env_A0(s, op2_offset);
B
balrog 已提交
4265 4266
                }
            }
4267
            val = cpu_ldub_code(env, s->pc++);
B
balrog 已提交
4268

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

                if (s->dflag == 2)
                    /* The helper must use entire 64-bit gp registers */
                    val |= 1 << 8;
            }

B
balrog 已提交
4277 4278
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4279
            sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
balrog 已提交
4280
            break;
R
Richard Henderson 已提交
4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294

        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;
                }
4295
                ot = s->dflag == 2 ? MO_64 : MO_32;
R
Richard Henderson 已提交
4296 4297
                gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
                b = cpu_ldub_code(env, s->pc++);
4298
                if (ot == MO_64) {
R
Richard Henderson 已提交
4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312
                    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);
                }
                gen_op_mov_reg_T0(ot, reg);
                break;

            default:
                goto illegal_op;
            }
            break;

B
bellard 已提交
4313 4314 4315 4316 4317
        default:
            goto illegal_op;
        }
    } else {
        /* generic MMX or SSE operation */
B
bellard 已提交
4318 4319 4320 4321 4322 4323 4324 4325
        switch(b) {
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
        case 0xc2: /* compare insns */
            s->rip_offset = 1;
            break;
        default:
            break;
B
bellard 已提交
4326 4327 4328 4329
        }
        if (is_xmm) {
            op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
            if (mod != 3) {
4330
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4331
                op2_offset = offsetof(CPUX86State,xmm_t0);
4332
                if (b1 >= 2 && ((b >= 0x50 && b <= 0x5f && b != 0x5b) ||
B
bellard 已提交
4333 4334 4335 4336
                                b == 0xc2)) {
                    /* specific case for SSE single instructions */
                    if (b1 == 2) {
                        /* 32 bit access */
4337
                        gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
4338
                        tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
B
bellard 已提交
4339 4340
                    } else {
                        /* 64 bit access */
4341 4342
                        gen_ldq_env_A0(s, offsetof(CPUX86State,
                                                   xmm_t0.XMM_D(0)));
B
bellard 已提交
4343 4344
                    }
                } else {
4345
                    gen_ldo_env_A0(s, op2_offset);
B
bellard 已提交
4346 4347 4348 4349 4350 4351 4352 4353
                }
            } 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) {
4354
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4355
                op2_offset = offsetof(CPUX86State,mmx_t0);
4356
                gen_ldq_env_A0(s, op2_offset);
B
bellard 已提交
4357 4358 4359 4360 4361 4362
            } else {
                rm = (modrm & 7);
                op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
            }
        }
        switch(b) {
A
aurel32 已提交
4363
        case 0x0f: /* 3DNow! data insns */
4364 4365
            if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
                goto illegal_op;
4366
            val = cpu_ldub_code(env, s->pc++);
B
Blue Swirl 已提交
4367 4368
            sse_fn_epp = sse_op_table5[val];
            if (!sse_fn_epp) {
A
aurel32 已提交
4369
                goto illegal_op;
B
Blue Swirl 已提交
4370
            }
B
bellard 已提交
4371 4372
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4373
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
A
aurel32 已提交
4374
            break;
B
bellard 已提交
4375 4376
        case 0x70: /* pshufx insn */
        case 0xc6: /* pshufx insn */
4377
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4378 4379
            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
            sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
B
Blue Swirl 已提交
4382
            sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
B
bellard 已提交
4383 4384 4385
            break;
        case 0xc2:
            /* compare insns */
4386
            val = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4387 4388
            if (val >= 8)
                goto illegal_op;
B
Blue Swirl 已提交
4389
            sse_fn_epp = sse_op_table4[val][b1];
B
Blue Swirl 已提交
4390

B
bellard 已提交
4391 4392
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4393
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4394
            break;
4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406
        case 0xf7:
            /* maskmov : we must prepare A0 */
            if (mod != 3)
                goto illegal_op;
#ifdef TARGET_X86_64
            if (s->aflag == 2) {
                gen_op_movq_A0_reg(R_EDI);
            } else
#endif
            {
                gen_op_movl_A0_reg(R_EDI);
                if (s->aflag == 0)
4407
                    tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
4408 4409 4410 4411 4412
            }
            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 已提交
4413
            /* XXX: introduce a new table? */
B
Blue Swirl 已提交
4414 4415
            sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
            sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4416
            break;
B
bellard 已提交
4417
        default:
B
bellard 已提交
4418 4419
            tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
            tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
B
Blue Swirl 已提交
4420
            sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
B
bellard 已提交
4421 4422 4423
            break;
        }
        if (b == 0x2e || b == 0x2f) {
4424
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
4425 4426 4427 4428
        }
    }
}

B
bellard 已提交
4429 4430
/* convert one instruction. s->is_jmp is set if the translation must
   be stopped. Return the next pc value */
4431 4432
static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
                               target_ulong pc_start)
B
bellard 已提交
4433 4434
{
    int b, prefixes, aflag, dflag;
4435 4436
    int shift;
    TCGMemOp ot;
4437
    int modrm, reg, rm, mod, op, opreg, val;
B
bellard 已提交
4438 4439
    target_ulong next_eip, tval;
    int rex_w, rex_r;
B
bellard 已提交
4440

4441
    if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
4442
        tcg_gen_debug_insn_start(pc_start);
4443
    }
B
bellard 已提交
4444 4445 4446
    s->pc = pc_start;
    prefixes = 0;
    s->override = -1;
B
bellard 已提交
4447 4448 4449 4450 4451
    rex_w = -1;
    rex_r = 0;
#ifdef TARGET_X86_64
    s->rex_x = 0;
    s->rex_b = 0;
4452
    x86_64_hregs = 0;
B
bellard 已提交
4453 4454
#endif
    s->rip_offset = 0; /* for relative ip address */
4455 4456
    s->vex_l = 0;
    s->vex_v = 0;
B
bellard 已提交
4457
 next_byte:
4458
    b = cpu_ldub_code(env, s->pc);
B
bellard 已提交
4459
    s->pc++;
4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494
    /* 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 已提交
4495
#ifdef TARGET_X86_64
4496 4497
    case 0x40 ... 0x4f:
        if (CODE64(s)) {
B
bellard 已提交
4498 4499 4500 4501 4502 4503 4504 4505
            /* 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;
        }
4506 4507
        break;
#endif
4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524
    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 已提交
4525
            /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564
            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;
4565 4566 4567 4568
    }

    /* Post-process prefixes.  */
    if (CODE64(s)) {
4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579
        /* 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.  */
        dflag = (rex_w > 0 ? 2 : prefixes & PREFIX_DATA ? 0 : 1);
        /* In 64-bit mode, 0x67 selects 32-bit addressing.  */
        aflag = (prefixes & PREFIX_ADR ? 1 : 2);
    } else {
        /* In 16/32-bit mode, 0x66 selects the opposite data size.  */
        dflag = s->code32;
        if (prefixes & PREFIX_DATA) {
            dflag ^= 1;
B
bellard 已提交
4580
        }
4581 4582 4583 4584
        /* In 16/32-bit mode, 0x67 selects the opposite addressing.  */
        aflag = s->code32;
        if (prefixes & PREFIX_ADR) {
            aflag ^= 1;
B
bellard 已提交
4585
        }
B
bellard 已提交
4586 4587 4588 4589 4590 4591 4592 4593
    }

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

    /* lock generation */
    if (prefixes & PREFIX_LOCK)
P
pbrook 已提交
4594
        gen_helper_lock();
B
bellard 已提交
4595 4596 4597 4598 4599 4600 4601

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

B
bellard 已提交
4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620
        /**************************/
        /* 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;

            if ((b & 1) == 0)
4621
                ot = MO_8;
B
bellard 已提交
4622
            else
4623
                ot = dflag + MO_16;
4624

B
bellard 已提交
4625 4626
            switch(f) {
            case 0: /* OP Ev, Gv */
4627
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4628
                reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
4629
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4630
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4631
                if (mod != 3) {
4632
                    gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4633 4634 4635 4636
                    opreg = OR_TMP0;
                } else if (op == OP_XORL && rm == reg) {
                xor_zero:
                    /* xor reg, reg optimisation */
R
Richard Henderson 已提交
4637
                    set_cc_op(s, CC_OP_CLR);
4638
                    tcg_gen_movi_tl(cpu_T[0], 0);
B
bellard 已提交
4639
                    gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
4640 4641 4642 4643
                    break;
                } else {
                    opreg = rm;
                }
B
bellard 已提交
4644
                gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
4645 4646 4647
                gen_op(s, op, ot, opreg);
                break;
            case 1: /* OP Gv, Ev */
4648
                modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4649
                mod = (modrm >> 6) & 3;
B
bellard 已提交
4650 4651
                reg = ((modrm >> 3) & 7) | rex_r;
                rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4652
                if (mod != 3) {
4653
                    gen_lea_modrm(env, s, modrm);
4654
                    gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
B
bellard 已提交
4655 4656 4657
                } else if (op == OP_XORL && rm == reg) {
                    goto xor_zero;
                } else {
B
bellard 已提交
4658
                    gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
4659 4660 4661 4662
                }
                gen_op(s, op, ot, reg);
                break;
            case 2: /* OP A, Iv */
4663
                val = insn_get(env, s, ot);
4664
                tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
4665 4666 4667 4668 4669 4670
                gen_op(s, op, ot, OR_EAX);
                break;
            }
        }
        break;

4671 4672 4673
    case 0x82:
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
4674 4675 4676 4677 4678 4679 4680
    case 0x80: /* GRP1 */
    case 0x81:
    case 0x83:
        {
            int val;

            if ((b & 1) == 0)
4681
                ot = MO_8;
B
bellard 已提交
4682
            else
4683
                ot = dflag + MO_16;
4684

4685
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4686
            mod = (modrm >> 6) & 3;
B
bellard 已提交
4687
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4688
            op = (modrm >> 3) & 7;
4689

B
bellard 已提交
4690
            if (mod != 3) {
B
bellard 已提交
4691 4692 4693 4694
                if (b == 0x83)
                    s->rip_offset = 1;
                else
                    s->rip_offset = insn_const_size(ot);
4695
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4696 4697
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
4698
                opreg = rm;
B
bellard 已提交
4699 4700 4701 4702 4703 4704
            }

            switch(b) {
            default:
            case 0x80:
            case 0x81:
4705
            case 0x82:
4706
                val = insn_get(env, s, ot);
B
bellard 已提交
4707 4708
                break;
            case 0x83:
4709
                val = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
4710 4711
                break;
            }
4712
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
4713 4714 4715 4716 4717 4718 4719
            gen_op(s, op, ot, opreg);
        }
        break;

        /**************************/
        /* inc, dec, and other misc arith */
    case 0x40 ... 0x47: /* inc Gv */
4720
        ot = dflag ? MO_32 : MO_16;
B
bellard 已提交
4721 4722 4723
        gen_inc(s, ot, OR_EAX + (b & 7), 1);
        break;
    case 0x48 ... 0x4f: /* dec Gv */
4724
        ot = dflag ? MO_32 : MO_16;
B
bellard 已提交
4725 4726 4727 4728 4729
        gen_inc(s, ot, OR_EAX + (b & 7), -1);
        break;
    case 0xf6: /* GRP3 */
    case 0xf7:
        if ((b & 1) == 0)
4730
            ot = MO_8;
B
bellard 已提交
4731
        else
4732
            ot = dflag + MO_16;
B
bellard 已提交
4733

4734
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4735
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4736
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4737 4738
        op = (modrm >> 3) & 7;
        if (mod != 3) {
B
bellard 已提交
4739 4740
            if (op == 0)
                s->rip_offset = insn_const_size(ot);
4741
            gen_lea_modrm(env, s, modrm);
4742
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4743
        } else {
B
bellard 已提交
4744
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
4745 4746 4747 4748
        }

        switch(op) {
        case 0: /* test */
4749
            val = insn_get(env, s, ot);
4750
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
4751
            gen_op_testl_T0_T1_cc();
4752
            set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
4753 4754
            break;
        case 2: /* not */
4755
            tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4756
            if (mod != 3) {
4757
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4758
            } else {
B
bellard 已提交
4759
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
4760 4761 4762
            }
            break;
        case 3: /* neg */
4763
            tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
4764
            if (mod != 3) {
4765
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4766
            } else {
B
bellard 已提交
4767
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
4768 4769
            }
            gen_op_update_neg_cc();
4770
            set_cc_op(s, CC_OP_SUBB + ot);
B
bellard 已提交
4771 4772 4773
            break;
        case 4: /* mul */
            switch(ot) {
4774 4775
            case MO_8:
                gen_op_mov_TN_reg(MO_8, 1, R_EAX);
B
bellard 已提交
4776 4777 4778 4779
                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]);
4780
                gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
4781 4782
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_andi_tl(cpu_cc_src, cpu_T[0], 0xff00);
4783
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4784
                break;
4785 4786
            case MO_16:
                gen_op_mov_TN_reg(MO_16, 1, R_EAX);
B
bellard 已提交
4787 4788 4789 4790
                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]);
4791
                gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
4792 4793
                tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
                tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
4794
                gen_op_mov_reg_T0(MO_16, R_EDX);
B
bellard 已提交
4795
                tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4796
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4797 4798
                break;
            default:
4799
            case MO_32:
4800 4801 4802 4803 4804 4805 4806 4807
                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]);
4808
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4809
                break;
B
bellard 已提交
4810
#ifdef TARGET_X86_64
4811
            case MO_64:
4812 4813 4814 4815
                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]);
4816
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4817 4818
                break;
#endif
B
bellard 已提交
4819 4820 4821 4822
            }
            break;
        case 5: /* imul */
            switch(ot) {
4823 4824
            case MO_8:
                gen_op_mov_TN_reg(MO_8, 1, R_EAX);
B
bellard 已提交
4825 4826 4827 4828
                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]);
4829
                gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
4830 4831 4832
                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);
4833
                set_cc_op(s, CC_OP_MULB);
B
bellard 已提交
4834
                break;
4835 4836
            case MO_16:
                gen_op_mov_TN_reg(MO_16, 1, R_EAX);
B
bellard 已提交
4837 4838 4839 4840
                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]);
4841
                gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
4842 4843 4844 4845
                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);
4846
                gen_op_mov_reg_T0(MO_16, R_EDX);
4847
                set_cc_op(s, CC_OP_MULW);
B
bellard 已提交
4848 4849
                break;
            default:
4850
            case MO_32:
4851 4852 4853 4854 4855 4856 4857 4858 4859 4860
                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);
4861
                set_cc_op(s, CC_OP_MULL);
B
bellard 已提交
4862
                break;
B
bellard 已提交
4863
#ifdef TARGET_X86_64
4864
            case MO_64:
4865 4866 4867 4868 4869
                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]);
4870
                set_cc_op(s, CC_OP_MULQ);
B
bellard 已提交
4871 4872
                break;
#endif
B
bellard 已提交
4873 4874 4875 4876
            }
            break;
        case 6: /* div */
            switch(ot) {
4877
            case MO_8:
B
bellard 已提交
4878
                gen_jmp_im(pc_start - s->cs_base);
4879
                gen_helper_divb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4880
                break;
4881
            case MO_16:
B
bellard 已提交
4882
                gen_jmp_im(pc_start - s->cs_base);
4883
                gen_helper_divw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4884 4885
                break;
            default:
4886
            case MO_32:
B
bellard 已提交
4887
                gen_jmp_im(pc_start - s->cs_base);
4888
                gen_helper_divl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4889 4890
                break;
#ifdef TARGET_X86_64
4891
            case MO_64:
B
bellard 已提交
4892
                gen_jmp_im(pc_start - s->cs_base);
4893
                gen_helper_divq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4894
                break;
B
bellard 已提交
4895
#endif
B
bellard 已提交
4896 4897 4898 4899
            }
            break;
        case 7: /* idiv */
            switch(ot) {
4900
            case MO_8:
B
bellard 已提交
4901
                gen_jmp_im(pc_start - s->cs_base);
4902
                gen_helper_idivb_AL(cpu_env, cpu_T[0]);
B
bellard 已提交
4903
                break;
4904
            case MO_16:
B
bellard 已提交
4905
                gen_jmp_im(pc_start - s->cs_base);
4906
                gen_helper_idivw_AX(cpu_env, cpu_T[0]);
B
bellard 已提交
4907 4908
                break;
            default:
4909
            case MO_32:
B
bellard 已提交
4910
                gen_jmp_im(pc_start - s->cs_base);
4911
                gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4912 4913
                break;
#ifdef TARGET_X86_64
4914
            case MO_64:
B
bellard 已提交
4915
                gen_jmp_im(pc_start - s->cs_base);
4916
                gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
B
bellard 已提交
4917
                break;
B
bellard 已提交
4918
#endif
B
bellard 已提交
4919 4920 4921 4922 4923 4924 4925 4926 4927 4928
            }
            break;
        default:
            goto illegal_op;
        }
        break;

    case 0xfe: /* GRP4 */
    case 0xff: /* GRP5 */
        if ((b & 1) == 0)
4929
            ot = MO_8;
B
bellard 已提交
4930
        else
4931
            ot = dflag + MO_16;
B
bellard 已提交
4932

4933
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
4934
        mod = (modrm >> 6) & 3;
B
bellard 已提交
4935
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
4936 4937 4938 4939
        op = (modrm >> 3) & 7;
        if (op >= 2 && b == 0xfe) {
            goto illegal_op;
        }
B
bellard 已提交
4940
        if (CODE64(s)) {
4941
            if (op == 2 || op == 4) {
B
bellard 已提交
4942
                /* operand size for jumps is 64 bit */
4943
                ot = MO_64;
4944
            } else if (op == 3 || op == 5) {
4945
                ot = dflag ? MO_32 + (rex_w == 1) : MO_16;
B
bellard 已提交
4946 4947
            } else if (op == 6) {
                /* default push size is 64 bit */
4948
                ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
4949 4950
            }
        }
B
bellard 已提交
4951
        if (mod != 3) {
4952
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
4953
            if (op >= 2 && op != 3 && op != 5)
4954
                gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
4955
        } else {
B
bellard 已提交
4956
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974
        }

        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 */
4975
            /* XXX: optimize if memory (no 'and' is necessary) */
4976 4977 4978
            if (s->dflag == 0) {
                tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
            }
B
bellard 已提交
4979
            next_eip = s->pc - s->cs_base;
4980
            tcg_gen_movi_tl(cpu_T[1], next_eip);
4981 4982
            gen_push_T1(s);
            gen_op_jmp_T0();
B
bellard 已提交
4983 4984
            gen_eob(s);
            break;
B
bellard 已提交
4985
        case 3: /* lcall Ev */
4986
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
4987
            gen_add_A0_im(s, 1 << (ot - MO_16 + 1));
4988
            gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
4989 4990
        do_lcall:
            if (s->pe && !s->vm86) {
4991
                gen_update_cc_op(s);
B
bellard 已提交
4992
                gen_jmp_im(pc_start - s->cs_base);
4993
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4994 4995
                gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                           tcg_const_i32(dflag),
P
pbrook 已提交
4996
                                           tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
4997
            } else {
4998
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4999 5000
                gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
                                      tcg_const_i32(dflag),
P
pbrook 已提交
5001
                                      tcg_const_i32(s->pc - s->cs_base));
B
bellard 已提交
5002 5003 5004 5005
            }
            gen_eob(s);
            break;
        case 4: /* jmp Ev */
5006 5007 5008
            if (s->dflag == 0) {
                tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
            }
B
bellard 已提交
5009 5010 5011 5012
            gen_op_jmp_T0();
            gen_eob(s);
            break;
        case 5: /* ljmp Ev */
5013
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5014
            gen_add_A0_im(s, 1 << (ot - MO_16 + 1));
5015
            gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
5016 5017
        do_ljmp:
            if (s->pe && !s->vm86) {
5018
                gen_update_cc_op(s);
B
bellard 已提交
5019
                gen_jmp_im(pc_start - s->cs_base);
5020
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5021
                gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
P
pbrook 已提交
5022
                                          tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
5023
            } else {
5024
                gen_op_movl_seg_T0_vm(R_CS);
5025
                tcg_gen_mov_tl(cpu_T[0], cpu_T[1]);
B
bellard 已提交
5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038
                gen_op_jmp_T0();
            }
            gen_eob(s);
            break;
        case 6: /* push Ev */
            gen_push_T0(s);
            break;
        default:
            goto illegal_op;
        }
        break;

    case 0x84: /* test Ev, Gv */
5039
    case 0x85:
B
bellard 已提交
5040
        if ((b & 1) == 0)
5041
            ot = MO_8;
B
bellard 已提交
5042
        else
5043
            ot = dflag + MO_16;
B
bellard 已提交
5044

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

5048
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5049
        gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
5050
        gen_op_testl_T0_T1_cc();
5051
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5052
        break;
5053

B
bellard 已提交
5054 5055 5056
    case 0xa8: /* test eAX, Iv */
    case 0xa9:
        if ((b & 1) == 0)
5057
            ot = MO_8;
B
bellard 已提交
5058
        else
5059
            ot = dflag + MO_16;
5060
        val = insn_get(env, s, ot);
B
bellard 已提交
5061

B
bellard 已提交
5062
        gen_op_mov_TN_reg(ot, 0, OR_EAX);
5063
        tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5064
        gen_op_testl_T0_T1_cc();
5065
        set_cc_op(s, CC_OP_LOGICB + ot);
B
bellard 已提交
5066
        break;
5067

B
bellard 已提交
5068
    case 0x98: /* CWDE/CBW */
B
bellard 已提交
5069 5070
#ifdef TARGET_X86_64
        if (dflag == 2) {
5071
            gen_op_mov_TN_reg(MO_32, 0, R_EAX);
B
bellard 已提交
5072
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
5073
            gen_op_mov_reg_T0(MO_64, R_EAX);
B
bellard 已提交
5074 5075
        } else
#endif
B
bellard 已提交
5076
        if (dflag == 1) {
5077
            gen_op_mov_TN_reg(MO_16, 0, R_EAX);
B
bellard 已提交
5078
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
5079
            gen_op_mov_reg_T0(MO_32, R_EAX);
B
bellard 已提交
5080
        } else {
5081
            gen_op_mov_TN_reg(MO_8, 0, R_EAX);
B
bellard 已提交
5082
            tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
5083
            gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
5084
        }
B
bellard 已提交
5085 5086
        break;
    case 0x99: /* CDQ/CWD */
B
bellard 已提交
5087 5088
#ifdef TARGET_X86_64
        if (dflag == 2) {
5089
            gen_op_mov_TN_reg(MO_64, 0, R_EAX);
B
bellard 已提交
5090
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 63);
5091
            gen_op_mov_reg_T0(MO_64, R_EDX);
B
bellard 已提交
5092 5093
        } else
#endif
B
bellard 已提交
5094
        if (dflag == 1) {
5095
            gen_op_mov_TN_reg(MO_32, 0, R_EAX);
B
bellard 已提交
5096 5097
            tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 31);
5098
            gen_op_mov_reg_T0(MO_32, R_EDX);
B
bellard 已提交
5099
        } else {
5100
            gen_op_mov_TN_reg(MO_16, 0, R_EAX);
B
bellard 已提交
5101 5102
            tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 15);
5103
            gen_op_mov_reg_T0(MO_16, R_EDX);
B
bellard 已提交
5104
        }
B
bellard 已提交
5105 5106 5107 5108
        break;
    case 0x1af: /* imul Gv, Ev */
    case 0x69: /* imul Gv, Ev, I */
    case 0x6b:
5109
        ot = dflag + MO_16;
5110
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5111 5112 5113 5114 5115
        reg = ((modrm >> 3) & 7) | rex_r;
        if (b == 0x69)
            s->rip_offset = insn_const_size(ot);
        else if (b == 0x6b)
            s->rip_offset = 1;
5116
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5117
        if (b == 0x69) {
5118
            val = insn_get(env, s, ot);
5119
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5120
        } else if (b == 0x6b) {
5121
            val = (int8_t)insn_get(env, s, MO_8);
5122
            tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
5123
        } else {
B
bellard 已提交
5124
            gen_op_mov_TN_reg(ot, 1, reg);
B
bellard 已提交
5125
        }
5126
        switch (ot) {
B
bellard 已提交
5127
#ifdef TARGET_X86_64
5128
        case MO_64:
5129 5130 5131 5132 5133
            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 已提交
5134
#endif
5135
        case MO_32:
5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146
            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 已提交
5147 5148 5149 5150 5151 5152 5153
            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);
5154 5155
            gen_op_mov_reg_T0(ot, reg);
            break;
B
bellard 已提交
5156
        }
5157
        set_cc_op(s, CC_OP_MULB + ot);
B
bellard 已提交
5158 5159 5160 5161
        break;
    case 0x1c0:
    case 0x1c1: /* xadd Ev, Gv */
        if ((b & 1) == 0)
5162
            ot = MO_8;
B
bellard 已提交
5163
        else
5164
            ot = dflag + MO_16;
5165
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5166
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5167 5168
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5169
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5170 5171
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_TN_reg(ot, 1, rm);
B
bellard 已提交
5172
            gen_op_addl_T0_T1();
B
bellard 已提交
5173 5174
            gen_op_mov_reg_T1(ot, reg);
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5175
        } else {
5176
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5177
            gen_op_mov_TN_reg(ot, 0, reg);
5178
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
B
bellard 已提交
5179
            gen_op_addl_T0_T1();
5180
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5181
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5182 5183
        }
        gen_op_update2_cc();
5184
        set_cc_op(s, CC_OP_ADDB + ot);
B
bellard 已提交
5185 5186 5187
        break;
    case 0x1b0:
    case 0x1b1: /* cmpxchg Ev, Gv */
B
bellard 已提交
5188
        {
B
bellard 已提交
5189
            int label1, label2;
5190
            TCGv t0, t1, t2, a0;
B
bellard 已提交
5191 5192

            if ((b & 1) == 0)
5193
                ot = MO_8;
B
bellard 已提交
5194
            else
5195
                ot = dflag + MO_16;
5196
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5197 5198
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
P
pbrook 已提交
5199 5200 5201 5202
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
            a0 = tcg_temp_local_new();
5203
            gen_op_mov_v_reg(ot, t1, reg);
B
bellard 已提交
5204 5205
            if (mod == 3) {
                rm = (modrm & 7) | REX_B(s);
5206
                gen_op_mov_v_reg(ot, t0, rm);
B
bellard 已提交
5207
            } else {
5208
                gen_lea_modrm(env, s, modrm);
5209
                tcg_gen_mov_tl(a0, cpu_A0);
5210
                gen_op_ld_v(s, ot, t0, a0);
B
bellard 已提交
5211 5212 5213
                rm = 0; /* avoid warning */
            }
            label1 = gen_new_label();
5214 5215
            tcg_gen_mov_tl(t2, cpu_regs[R_EAX]);
            gen_extu(ot, t0);
5216
            gen_extu(ot, t2);
5217
            tcg_gen_brcond_tl(TCG_COND_EQ, t2, t0, label1);
5218
            label2 = gen_new_label();
B
bellard 已提交
5219
            if (mod == 3) {
5220
                gen_op_mov_reg_v(ot, R_EAX, t0);
B
bellard 已提交
5221 5222
                tcg_gen_br(label2);
                gen_set_label(label1);
5223
                gen_op_mov_reg_v(ot, rm, t1);
B
bellard 已提交
5224
            } else {
5225 5226 5227
                /* 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 */
5228
                gen_op_st_v(s, ot, t0, a0);
5229
                gen_op_mov_reg_v(ot, R_EAX, t0);
5230
                tcg_gen_br(label2);
B
bellard 已提交
5231
                gen_set_label(label1);
5232
                gen_op_st_v(s, ot, t1, a0);
B
bellard 已提交
5233
            }
5234
            gen_set_label(label2);
5235
            tcg_gen_mov_tl(cpu_cc_src, t0);
5236 5237
            tcg_gen_mov_tl(cpu_cc_srcT, t2);
            tcg_gen_sub_tl(cpu_cc_dst, t2, t0);
5238
            set_cc_op(s, CC_OP_SUBB + ot);
5239 5240 5241 5242
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
            tcg_temp_free(a0);
B
bellard 已提交
5243 5244 5245
        }
        break;
    case 0x1c7: /* cmpxchg8b */
5246
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5247
        mod = (modrm >> 6) & 3;
5248
        if ((mod == 3) || ((modrm & 0x38) != 0x8))
B
bellard 已提交
5249
            goto illegal_op;
B
bellard 已提交
5250 5251 5252 5253 5254
#ifdef TARGET_X86_64
        if (dflag == 2) {
            if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5255
            gen_update_cc_op(s);
5256
            gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
5257
            gen_helper_cmpxchg16b(cpu_env, cpu_A0);
B
bellard 已提交
5258 5259 5260 5261 5262 5263
        } else
#endif        
        {
            if (!(s->cpuid_features & CPUID_CX8))
                goto illegal_op;
            gen_jmp_im(pc_start - s->cs_base);
5264
            gen_update_cc_op(s);
5265
            gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
5266
            gen_helper_cmpxchg8b(cpu_env, cpu_A0);
B
bellard 已提交
5267
        }
5268
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
5269
        break;
5270

B
bellard 已提交
5271 5272 5273
        /**************************/
        /* push/pop */
    case 0x50 ... 0x57: /* push */
5274
        gen_op_mov_TN_reg(MO_32, 0, (b & 7) | REX_B(s));
B
bellard 已提交
5275 5276 5277
        gen_push_T0(s);
        break;
    case 0x58 ... 0x5f: /* pop */
B
bellard 已提交
5278
        if (CODE64(s)) {
5279
            ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
5280
        } else {
5281
            ot = dflag + MO_16;
B
bellard 已提交
5282
        }
B
bellard 已提交
5283
        gen_pop_T0(s);
B
bellard 已提交
5284
        /* NOTE: order is important for pop %sp */
B
bellard 已提交
5285
        gen_pop_update(s);
B
bellard 已提交
5286
        gen_op_mov_reg_T0(ot, (b & 7) | REX_B(s));
B
bellard 已提交
5287 5288
        break;
    case 0x60: /* pusha */
B
bellard 已提交
5289 5290
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5291 5292 5293
        gen_pusha(s);
        break;
    case 0x61: /* popa */
B
bellard 已提交
5294 5295
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5296 5297 5298 5299
        gen_popa(s);
        break;
    case 0x68: /* push Iv */
    case 0x6a:
B
bellard 已提交
5300
        if (CODE64(s)) {
5301
            ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
5302
        } else {
5303
            ot = dflag + MO_16;
B
bellard 已提交
5304
        }
B
bellard 已提交
5305
        if (b == 0x68)
5306
            val = insn_get(env, s, ot);
B
bellard 已提交
5307
        else
5308
            val = (int8_t)insn_get(env, s, MO_8);
5309
        tcg_gen_movi_tl(cpu_T[0], val);
B
bellard 已提交
5310 5311 5312
        gen_push_T0(s);
        break;
    case 0x8f: /* pop Ev */
B
bellard 已提交
5313
        if (CODE64(s)) {
5314
            ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
5315
        } else {
5316
            ot = dflag + MO_16;
B
bellard 已提交
5317
        }
5318
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5319
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5320
        gen_pop_T0(s);
B
bellard 已提交
5321 5322 5323
        if (mod == 3) {
            /* NOTE: order is important for pop %sp */
            gen_pop_update(s);
B
bellard 已提交
5324
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5325
            gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
5326 5327
        } else {
            /* NOTE: order is important too for MMU exceptions */
B
bellard 已提交
5328
            s->popl_esp_hack = 1 << ot;
5329
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5330 5331 5332
            s->popl_esp_hack = 0;
            gen_pop_update(s);
        }
B
bellard 已提交
5333 5334 5335 5336
        break;
    case 0xc8: /* enter */
        {
            int level;
5337
            val = cpu_lduw_code(env, s->pc);
B
bellard 已提交
5338
            s->pc += 2;
5339
            level = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5340 5341 5342 5343 5344
            gen_enter(s, val, level);
        }
        break;
    case 0xc9: /* leave */
        /* XXX: exception not precise (ESP is updated before potential exception) */
B
bellard 已提交
5345
        if (CODE64(s)) {
5346 5347
            gen_op_mov_TN_reg(MO_64, 0, R_EBP);
            gen_op_mov_reg_T0(MO_64, R_ESP);
B
bellard 已提交
5348
        } else if (s->ss32) {
5349 5350
            gen_op_mov_TN_reg(MO_32, 0, R_EBP);
            gen_op_mov_reg_T0(MO_32, R_ESP);
B
bellard 已提交
5351
        } else {
5352 5353
            gen_op_mov_TN_reg(MO_16, 0, R_EBP);
            gen_op_mov_reg_T0(MO_16, R_ESP);
B
bellard 已提交
5354 5355
        }
        gen_pop_T0(s);
B
bellard 已提交
5356
        if (CODE64(s)) {
5357
            ot = dflag ? MO_64 : MO_16;
B
bellard 已提交
5358
        } else {
5359
            ot = dflag + MO_16;
B
bellard 已提交
5360
        }
B
bellard 已提交
5361
        gen_op_mov_reg_T0(ot, R_EBP);
B
bellard 已提交
5362 5363 5364 5365 5366 5367
        gen_pop_update(s);
        break;
    case 0x06: /* push es */
    case 0x0e: /* push cs */
    case 0x16: /* push ss */
    case 0x1e: /* push ds */
B
bellard 已提交
5368 5369
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380
        gen_op_movl_T0_seg(b >> 3);
        gen_push_T0(s);
        break;
    case 0x1a0: /* push fs */
    case 0x1a8: /* push gs */
        gen_op_movl_T0_seg((b >> 3) & 7);
        gen_push_T0(s);
        break;
    case 0x07: /* pop es */
    case 0x17: /* pop ss */
    case 0x1f: /* pop ds */
B
bellard 已提交
5381 5382
        if (CODE64(s))
            goto illegal_op;
B
bellard 已提交
5383 5384 5385 5386 5387
        reg = b >> 3;
        gen_pop_T0(s);
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
        gen_pop_update(s);
        if (reg == R_SS) {
5388 5389 5390 5391
            /* if reg == SS, inhibit interrupts/trace. */
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5392
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5393 5394 5395
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5396
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5397 5398 5399 5400 5401 5402 5403 5404 5405
            gen_eob(s);
        }
        break;
    case 0x1a1: /* pop fs */
    case 0x1a9: /* pop gs */
        gen_pop_T0(s);
        gen_movl_seg_T0(s, (b >> 3) & 7, pc_start - s->cs_base);
        gen_pop_update(s);
        if (s->is_jmp) {
B
bellard 已提交
5406
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5407 5408 5409 5410 5411 5412 5413 5414 5415
            gen_eob(s);
        }
        break;

        /**************************/
        /* mov */
    case 0x88:
    case 0x89: /* mov Gv, Ev */
        if ((b & 1) == 0)
5416
            ot = MO_8;
B
bellard 已提交
5417
        else
5418
            ot = dflag + MO_16;
5419
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5420
        reg = ((modrm >> 3) & 7) | rex_r;
5421

B
bellard 已提交
5422
        /* generate a generic store */
5423
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
5424 5425 5426 5427
        break;
    case 0xc6:
    case 0xc7: /* mov Ev, Iv */
        if ((b & 1) == 0)
5428
            ot = MO_8;
B
bellard 已提交
5429
        else
5430
            ot = dflag + MO_16;
5431
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5432
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5433 5434
        if (mod != 3) {
            s->rip_offset = insn_const_size(ot);
5435
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5436
        }
5437
        val = insn_get(env, s, ot);
5438
        tcg_gen_movi_tl(cpu_T[0], val);
5439 5440 5441
        if (mod != 3) {
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
        } else {
B
bellard 已提交
5442
            gen_op_mov_reg_T0(ot, (modrm & 7) | REX_B(s));
5443
        }
B
bellard 已提交
5444 5445 5446 5447
        break;
    case 0x8a:
    case 0x8b: /* mov Ev, Gv */
        if ((b & 1) == 0)
5448
            ot = MO_8;
B
bellard 已提交
5449
        else
5450
            ot = MO_16 + dflag;
5451
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5452
        reg = ((modrm >> 3) & 7) | rex_r;
5453

5454
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
bellard 已提交
5455
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5456 5457
        break;
    case 0x8e: /* mov seg, Gv */
5458
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5459 5460 5461
        reg = (modrm >> 3) & 7;
        if (reg >= 6 || reg == R_CS)
            goto illegal_op;
5462
        gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
5463 5464 5465
        gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
        if (reg == R_SS) {
            /* if reg == SS, inhibit interrupts/trace */
5466 5467 5468
            /* If several instructions disable interrupts, only the
               _first_ does it */
            if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
5469
                gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
5470 5471 5472
            s->tf = 0;
        }
        if (s->is_jmp) {
B
bellard 已提交
5473
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5474 5475 5476 5477
            gen_eob(s);
        }
        break;
    case 0x8c: /* mov Gv, seg */
5478
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5479 5480 5481 5482 5483
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (reg >= 6)
            goto illegal_op;
        gen_op_movl_T0_seg(reg);
B
bellard 已提交
5484
        if (mod == 3)
5485
            ot = MO_16 + dflag;
B
bellard 已提交
5486
        else
5487
            ot = MO_16;
5488
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
5489 5490 5491 5492 5493 5494 5495
        break;

    case 0x1b6: /* movzbS Gv, Eb */
    case 0x1b7: /* movzwS Gv, Eb */
    case 0x1be: /* movsbS Gv, Eb */
    case 0x1bf: /* movswS Gv, Eb */
        {
5496 5497 5498
            TCGMemOp d_ot;
            TCGMemOp s_ot;

B
bellard 已提交
5499
            /* d_ot is the size of destination */
5500
            d_ot = dflag + MO_16;
B
bellard 已提交
5501
            /* ot is the size of source */
5502
            ot = (b & 1) + MO_8;
5503 5504 5505
            /* s_ot is the sign+size of source */
            s_ot = b & 8 ? MO_SIGN | ot : ot;

5506
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5507
            reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5508
            mod = (modrm >> 6) & 3;
B
bellard 已提交
5509
            rm = (modrm & 7) | REX_B(s);
5510

B
bellard 已提交
5511
            if (mod == 3) {
B
bellard 已提交
5512
                gen_op_mov_TN_reg(ot, 0, rm);
5513 5514
                switch (s_ot) {
                case MO_UB:
B
bellard 已提交
5515
                    tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5516
                    break;
5517
                case MO_SB:
B
bellard 已提交
5518
                    tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5519
                    break;
5520
                case MO_UW:
B
bellard 已提交
5521
                    tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5522 5523
                    break;
                default:
5524
                case MO_SW:
B
bellard 已提交
5525
                    tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
B
bellard 已提交
5526 5527
                    break;
                }
B
bellard 已提交
5528
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5529
            } else {
5530
                gen_lea_modrm(env, s, modrm);
5531
                gen_op_ld_v(s, s_ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5532
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
5533 5534 5535 5536 5537
            }
        }
        break;

    case 0x8d: /* lea */
5538
        ot = dflag + MO_16;
5539
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5540 5541 5542
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
5543
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5544 5545 5546 5547
        /* we must ensure that no segment is added */
        s->override = -1;
        val = s->addseg;
        s->addseg = 0;
5548
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5549
        s->addseg = val;
5550
        gen_op_mov_reg_A0(ot - MO_16, reg);
B
bellard 已提交
5551
        break;
5552

B
bellard 已提交
5553 5554 5555 5556 5557
    case 0xa0: /* mov EAX, Ov */
    case 0xa1:
    case 0xa2: /* mov Ov, EAX */
    case 0xa3:
        {
B
bellard 已提交
5558 5559 5560
            target_ulong offset_addr;

            if ((b & 1) == 0)
5561
                ot = MO_8;
B
bellard 已提交
5562
            else
5563
                ot = dflag + MO_16;
B
bellard 已提交
5564
#ifdef TARGET_X86_64
5565
            if (s->aflag == 2) {
5566
                offset_addr = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5567
                s->pc += 8;
5568
            } else
B
bellard 已提交
5569 5570 5571
#endif
            {
                if (s->aflag) {
5572
                    offset_addr = insn_get(env, s, MO_32);
B
bellard 已提交
5573
                } else {
5574
                    offset_addr = insn_get(env, s, MO_16);
B
bellard 已提交
5575 5576
                }
            }
5577
            tcg_gen_movi_tl(cpu_A0, offset_addr);
B
bellard 已提交
5578
            gen_add_A0_ds_seg(s);
B
bellard 已提交
5579
            if ((b & 2) == 0) {
5580
                gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5581
                gen_op_mov_reg_T0(ot, R_EAX);
B
bellard 已提交
5582
            } else {
B
bellard 已提交
5583
                gen_op_mov_TN_reg(ot, 0, R_EAX);
5584
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5585 5586 5587 5588
            }
        }
        break;
    case 0xd7: /* xlat */
B
bellard 已提交
5589
#ifdef TARGET_X86_64
5590
        if (s->aflag == 2) {
B
bellard 已提交
5591
            gen_op_movq_A0_reg(R_EBX);
5592
            gen_op_mov_TN_reg(MO_64, 0, R_EAX);
5593 5594
            tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
5595
        } else
B
bellard 已提交
5596 5597
#endif
        {
B
bellard 已提交
5598
            gen_op_movl_A0_reg(R_EBX);
5599
            gen_op_mov_TN_reg(MO_32, 0, R_EAX);
5600 5601
            tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xff);
            tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
B
bellard 已提交
5602
            if (s->aflag == 0)
5603
                tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
5604 5605
            else
                tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
B
bellard 已提交
5606
        }
B
bellard 已提交
5607
        gen_add_A0_ds_seg(s);
5608
        gen_op_ld_v(s, MO_8, cpu_T[0], cpu_A0);
5609
        gen_op_mov_reg_T0(MO_8, R_EAX);
B
bellard 已提交
5610 5611
        break;
    case 0xb0 ... 0xb7: /* mov R, Ib */
5612
        val = insn_get(env, s, MO_8);
5613
        tcg_gen_movi_tl(cpu_T[0], val);
5614
        gen_op_mov_reg_T0(MO_8, (b & 7) | REX_B(s));
B
bellard 已提交
5615 5616
        break;
    case 0xb8 ... 0xbf: /* mov R, Iv */
B
bellard 已提交
5617 5618 5619 5620
#ifdef TARGET_X86_64
        if (dflag == 2) {
            uint64_t tmp;
            /* 64 bit case */
5621
            tmp = cpu_ldq_code(env, s->pc);
B
bellard 已提交
5622 5623
            s->pc += 8;
            reg = (b & 7) | REX_B(s);
5624
            tcg_gen_movi_tl(cpu_T[0], tmp);
5625
            gen_op_mov_reg_T0(MO_64, reg);
5626
        } else
B
bellard 已提交
5627 5628
#endif
        {
5629
            ot = dflag ? MO_32 : MO_16;
5630
            val = insn_get(env, s, ot);
B
bellard 已提交
5631
            reg = (b & 7) | REX_B(s);
5632
            tcg_gen_movi_tl(cpu_T[0], val);
B
bellard 已提交
5633
            gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
5634
        }
B
bellard 已提交
5635 5636 5637
        break;

    case 0x91 ... 0x97: /* xchg R, EAX */
R
Richard Henderson 已提交
5638
    do_xchg_reg_eax:
5639
        ot = dflag + MO_16;
B
bellard 已提交
5640
        reg = (b & 7) | REX_B(s);
B
bellard 已提交
5641 5642 5643 5644 5645
        rm = R_EAX;
        goto do_xchg_reg;
    case 0x86:
    case 0x87: /* xchg Ev, Gv */
        if ((b & 1) == 0)
5646
            ot = MO_8;
B
bellard 已提交
5647
        else
5648
            ot = dflag + MO_16;
5649
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5650
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5651 5652
        mod = (modrm >> 6) & 3;
        if (mod == 3) {
B
bellard 已提交
5653
            rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
5654
        do_xchg_reg:
B
bellard 已提交
5655 5656 5657 5658
            gen_op_mov_TN_reg(ot, 0, reg);
            gen_op_mov_TN_reg(ot, 1, rm);
            gen_op_mov_reg_T0(ot, rm);
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5659
        } else {
5660
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5661
            gen_op_mov_TN_reg(ot, 0, reg);
B
bellard 已提交
5662 5663
            /* for xchg, lock is implicit */
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5664
                gen_helper_lock();
5665
            gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5666
            gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
5667
            if (!(prefixes & PREFIX_LOCK))
P
pbrook 已提交
5668
                gen_helper_unlock();
B
bellard 已提交
5669
            gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5670 5671 5672
        }
        break;
    case 0xc4: /* les Gv */
5673
        /* In CODE64 this is VEX3; see above.  */
B
bellard 已提交
5674 5675 5676
        op = R_ES;
        goto do_lxx;
    case 0xc5: /* lds Gv */
5677
        /* In CODE64 this is VEX2; see above.  */
B
bellard 已提交
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688
        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:
5689
        ot = dflag ? MO_32 : MO_16;
5690
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5691
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5692 5693 5694
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
5695
        gen_lea_modrm(env, s, modrm);
5696
        gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
5697
        gen_add_A0_im(s, 1 << (ot - MO_16 + 1));
B
bellard 已提交
5698
        /* load the segment first to handle exceptions properly */
5699
        gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
5700 5701
        gen_movl_seg_T0(s, op, pc_start - s->cs_base);
        /* then put the data */
B
bellard 已提交
5702
        gen_op_mov_reg_T1(ot, reg);
B
bellard 已提交
5703
        if (s->is_jmp) {
B
bellard 已提交
5704
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
5705 5706 5707
            gen_eob(s);
        }
        break;
5708

B
bellard 已提交
5709 5710 5711 5712 5713 5714 5715 5716 5717
        /************************/
        /* shifts */
    case 0xc0:
    case 0xc1:
        /* shift Ev,Ib */
        shift = 2;
    grp2:
        {
            if ((b & 1) == 0)
5718
                ot = MO_8;
B
bellard 已提交
5719
            else
5720
                ot = dflag + MO_16;
5721

5722
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5723 5724
            mod = (modrm >> 6) & 3;
            op = (modrm >> 3) & 7;
5725

B
bellard 已提交
5726
            if (mod != 3) {
B
bellard 已提交
5727 5728 5729
                if (shift == 2) {
                    s->rip_offset = 1;
                }
5730
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5731 5732
                opreg = OR_TMP0;
            } else {
B
bellard 已提交
5733
                opreg = (modrm & 7) | REX_B(s);
B
bellard 已提交
5734 5735 5736 5737 5738 5739 5740
            }

            /* simpler op */
            if (shift == 0) {
                gen_shift(s, op, ot, opreg, OR_ECX);
            } else {
                if (shift == 2) {
5741
                    shift = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773
                }
                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:
5774
        ot = dflag + MO_16;
5775
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5776
        mod = (modrm >> 6) & 3;
B
bellard 已提交
5777 5778
        rm = (modrm & 7) | REX_B(s);
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
5779
        if (mod != 3) {
5780
            gen_lea_modrm(env, s, modrm);
5781
            opreg = OR_TMP0;
B
bellard 已提交
5782
        } else {
5783
            opreg = rm;
B
bellard 已提交
5784
        }
B
bellard 已提交
5785
        gen_op_mov_TN_reg(ot, 1, reg);
5786

B
bellard 已提交
5787
        if (shift) {
P
Paolo Bonzini 已提交
5788 5789 5790
            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 已提交
5791
        } else {
P
Paolo Bonzini 已提交
5792
            gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
B
bellard 已提交
5793 5794 5795 5796 5797
        }
        break;

        /************************/
        /* floats */
5798
    case 0xd8 ... 0xdf:
B
bellard 已提交
5799 5800 5801 5802 5803 5804
        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;
        }
5805
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
5806 5807 5808 5809 5810
        mod = (modrm >> 6) & 3;
        rm = modrm & 7;
        op = ((b & 7) << 3) | ((modrm >> 3) & 7);
        if (mod != 3) {
            /* memory op */
5811
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822
            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:
5823 5824
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5825
                        gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5826 5827
                        break;
                    case 1:
5828 5829
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
5830
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5831 5832
                        break;
                    case 2:
5833 5834
                        tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
                                            s->mem_index, MO_LEQ);
B
Blue Swirl 已提交
5835
                        gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
B
bellard 已提交
5836 5837 5838
                        break;
                    case 3:
                    default:
5839 5840
                        tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                            s->mem_index, MO_LESW);
B
Blue Swirl 已提交
5841
                        gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
5842 5843
                        break;
                    }
5844

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

            switch(op) {
            case 0x08: /* fld sti */
B
Blue Swirl 已提交
6010 6011 6012
                gen_helper_fpush(cpu_env);
                gen_helper_fmov_ST0_STN(cpu_env,
                                        tcg_const_i32((opreg + 1) & 7));
B
bellard 已提交
6013 6014
                break;
            case 0x09: /* fxchg sti */
B
bellard 已提交
6015 6016
            case 0x29: /* fxchg4 sti, undocumented op */
            case 0x39: /* fxchg7 sti, undocumented op */
B
Blue Swirl 已提交
6017
                gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6018 6019 6020 6021
                break;
            case 0x0a: /* grp d9/2 */
                switch(rm) {
                case 0: /* fnop */
6022
                    /* check exceptions (FreeBSD FPU probe) */
6023
                    gen_update_cc_op(s);
B
bellard 已提交
6024
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
6025
                    gen_helper_fwait(cpu_env);
B
bellard 已提交
6026 6027 6028 6029 6030 6031 6032 6033
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0c: /* grp d9/4 */
                switch(rm) {
                case 0: /* fchs */
B
Blue Swirl 已提交
6034
                    gen_helper_fchs_ST0(cpu_env);
B
bellard 已提交
6035 6036
                    break;
                case 1: /* fabs */
B
Blue Swirl 已提交
6037
                    gen_helper_fabs_ST0(cpu_env);
B
bellard 已提交
6038 6039
                    break;
                case 4: /* ftst */
B
Blue Swirl 已提交
6040 6041
                    gen_helper_fldz_FT0(cpu_env);
                    gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6042 6043
                    break;
                case 5: /* fxam */
B
Blue Swirl 已提交
6044
                    gen_helper_fxam_ST0(cpu_env);
B
bellard 已提交
6045 6046 6047 6048 6049 6050 6051 6052 6053
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x0d: /* grp d9/5 */
                {
                    switch(rm) {
                    case 0:
B
Blue Swirl 已提交
6054 6055
                        gen_helper_fpush(cpu_env);
                        gen_helper_fld1_ST0(cpu_env);
B
bellard 已提交
6056 6057
                        break;
                    case 1:
B
Blue Swirl 已提交
6058 6059
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2t_ST0(cpu_env);
B
bellard 已提交
6060 6061
                        break;
                    case 2:
B
Blue Swirl 已提交
6062 6063
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldl2e_ST0(cpu_env);
B
bellard 已提交
6064 6065
                        break;
                    case 3:
B
Blue Swirl 已提交
6066 6067
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldpi_ST0(cpu_env);
B
bellard 已提交
6068 6069
                        break;
                    case 4:
B
Blue Swirl 已提交
6070 6071
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldlg2_ST0(cpu_env);
B
bellard 已提交
6072 6073
                        break;
                    case 5:
B
Blue Swirl 已提交
6074 6075
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldln2_ST0(cpu_env);
B
bellard 已提交
6076 6077
                        break;
                    case 6:
B
Blue Swirl 已提交
6078 6079
                        gen_helper_fpush(cpu_env);
                        gen_helper_fldz_ST0(cpu_env);
B
bellard 已提交
6080 6081 6082 6083 6084 6085 6086 6087 6088
                        break;
                    default:
                        goto illegal_op;
                    }
                }
                break;
            case 0x0e: /* grp d9/6 */
                switch(rm) {
                case 0: /* f2xm1 */
B
Blue Swirl 已提交
6089
                    gen_helper_f2xm1(cpu_env);
B
bellard 已提交
6090 6091
                    break;
                case 1: /* fyl2x */
B
Blue Swirl 已提交
6092
                    gen_helper_fyl2x(cpu_env);
B
bellard 已提交
6093 6094
                    break;
                case 2: /* fptan */
B
Blue Swirl 已提交
6095
                    gen_helper_fptan(cpu_env);
B
bellard 已提交
6096 6097
                    break;
                case 3: /* fpatan */
B
Blue Swirl 已提交
6098
                    gen_helper_fpatan(cpu_env);
B
bellard 已提交
6099 6100
                    break;
                case 4: /* fxtract */
B
Blue Swirl 已提交
6101
                    gen_helper_fxtract(cpu_env);
B
bellard 已提交
6102 6103
                    break;
                case 5: /* fprem1 */
B
Blue Swirl 已提交
6104
                    gen_helper_fprem1(cpu_env);
B
bellard 已提交
6105 6106
                    break;
                case 6: /* fdecstp */
B
Blue Swirl 已提交
6107
                    gen_helper_fdecstp(cpu_env);
B
bellard 已提交
6108 6109 6110
                    break;
                default:
                case 7: /* fincstp */
B
Blue Swirl 已提交
6111
                    gen_helper_fincstp(cpu_env);
B
bellard 已提交
6112 6113 6114 6115 6116 6117
                    break;
                }
                break;
            case 0x0f: /* grp d9/7 */
                switch(rm) {
                case 0: /* fprem */
B
Blue Swirl 已提交
6118
                    gen_helper_fprem(cpu_env);
B
bellard 已提交
6119 6120
                    break;
                case 1: /* fyl2xp1 */
B
Blue Swirl 已提交
6121
                    gen_helper_fyl2xp1(cpu_env);
B
bellard 已提交
6122 6123
                    break;
                case 2: /* fsqrt */
B
Blue Swirl 已提交
6124
                    gen_helper_fsqrt(cpu_env);
B
bellard 已提交
6125 6126
                    break;
                case 3: /* fsincos */
B
Blue Swirl 已提交
6127
                    gen_helper_fsincos(cpu_env);
B
bellard 已提交
6128 6129
                    break;
                case 5: /* fscale */
B
Blue Swirl 已提交
6130
                    gen_helper_fscale(cpu_env);
B
bellard 已提交
6131 6132
                    break;
                case 4: /* frndint */
B
Blue Swirl 已提交
6133
                    gen_helper_frndint(cpu_env);
B
bellard 已提交
6134 6135
                    break;
                case 6: /* fsin */
B
Blue Swirl 已提交
6136
                    gen_helper_fsin(cpu_env);
B
bellard 已提交
6137 6138 6139
                    break;
                default:
                case 7: /* fcos */
B
Blue Swirl 已提交
6140
                    gen_helper_fcos(cpu_env);
B
bellard 已提交
6141 6142 6143 6144 6145 6146 6147 6148
                    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;
6149

B
bellard 已提交
6150 6151
                    op1 = op & 7;
                    if (op >= 0x20) {
P
pbrook 已提交
6152
                        gen_helper_fp_arith_STN_ST0(op1, opreg);
B
bellard 已提交
6153
                        if (op >= 0x30)
B
Blue Swirl 已提交
6154
                            gen_helper_fpop(cpu_env);
B
bellard 已提交
6155
                    } else {
B
Blue Swirl 已提交
6156
                        gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
P
pbrook 已提交
6157
                        gen_helper_fp_arith_ST0_FT0(op1);
B
bellard 已提交
6158 6159 6160 6161
                    }
                }
                break;
            case 0x02: /* fcom */
B
bellard 已提交
6162
            case 0x22: /* fcom2, undocumented op */
B
Blue Swirl 已提交
6163 6164
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcom_ST0_FT0(cpu_env);
B
bellard 已提交
6165 6166
                break;
            case 0x03: /* fcomp */
B
bellard 已提交
6167 6168
            case 0x23: /* fcomp3, undocumented op */
            case 0x32: /* fcomp5, undocumented op */
B
Blue Swirl 已提交
6169 6170 6171
                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 已提交
6172 6173 6174 6175
                break;
            case 0x15: /* da/5 */
                switch(rm) {
                case 1: /* fucompp */
B
Blue Swirl 已提交
6176 6177 6178 6179
                    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 已提交
6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191
                    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 已提交
6192
                    gen_helper_fclex(cpu_env);
B
bellard 已提交
6193 6194
                    break;
                case 3: /* fninit */
B
Blue Swirl 已提交
6195
                    gen_helper_fninit(cpu_env);
B
bellard 已提交
6196 6197 6198 6199 6200 6201 6202 6203
                    break;
                case 4: /* fsetpm (287 only, just do nop here) */
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x1d: /* fucomi */
6204 6205 6206
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6207
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6208 6209
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
6210
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6211 6212
                break;
            case 0x1e: /* fcomi */
6213 6214 6215
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6216
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6217 6218
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
6219
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6220
                break;
B
bellard 已提交
6221
            case 0x28: /* ffree sti */
B
Blue Swirl 已提交
6222
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6223
                break;
B
bellard 已提交
6224
            case 0x2a: /* fst sti */
B
Blue Swirl 已提交
6225
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6226 6227
                break;
            case 0x2b: /* fstp sti */
B
bellard 已提交
6228 6229 6230
            case 0x0b: /* fstp1 sti, undocumented op */
            case 0x3a: /* fstp8 sti, undocumented op */
            case 0x3b: /* fstp9 sti, undocumented op */
B
Blue Swirl 已提交
6231 6232
                gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6233 6234
                break;
            case 0x2c: /* fucom st(i) */
B
Blue Swirl 已提交
6235 6236
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucom_ST0_FT0(cpu_env);
B
bellard 已提交
6237 6238
                break;
            case 0x2d: /* fucomp st(i) */
B
Blue Swirl 已提交
6239 6240 6241
                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 已提交
6242 6243 6244 6245
                break;
            case 0x33: /* de/3 */
                switch(rm) {
                case 1: /* fcompp */
B
Blue Swirl 已提交
6246 6247 6248 6249
                    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 已提交
6250 6251 6252 6253 6254
                    break;
                default:
                    goto illegal_op;
                }
                break;
B
bellard 已提交
6255
            case 0x38: /* ffreep sti, undocumented op */
B
Blue Swirl 已提交
6256 6257
                gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fpop(cpu_env);
B
bellard 已提交
6258
                break;
B
bellard 已提交
6259 6260 6261
            case 0x3c: /* df/4 */
                switch(rm) {
                case 0:
B
Blue Swirl 已提交
6262
                    gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6263
                    tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
6264
                    gen_op_mov_reg_T0(MO_16, R_EAX);
B
bellard 已提交
6265 6266 6267 6268 6269 6270
                    break;
                default:
                    goto illegal_op;
                }
                break;
            case 0x3d: /* fucomip */
6271 6272 6273
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6274
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6275 6276 6277
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fucomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6278
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6279 6280
                break;
            case 0x3e: /* fcomip */
6281 6282 6283
                if (!(s->cpuid_features & CPUID_CMOV)) {
                    goto illegal_op;
                }
6284
                gen_update_cc_op(s);
B
Blue Swirl 已提交
6285 6286 6287
                gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
                gen_helper_fcomi_ST0_FT0(cpu_env);
                gen_helper_fpop(cpu_env);
6288
                set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6289
                break;
6290 6291 6292
            case 0x10 ... 0x13: /* fcmovxx */
            case 0x18 ... 0x1b:
                {
B
bellard 已提交
6293
                    int op1, l1;
6294
                    static const uint8_t fcmov_cc[8] = {
6295 6296 6297 6298 6299
                        (JCC_B << 1),
                        (JCC_Z << 1),
                        (JCC_BE << 1),
                        (JCC_P << 1),
                    };
6300 6301 6302 6303

                    if (!(s->cpuid_features & CPUID_CMOV)) {
                        goto illegal_op;
                    }
6304
                    op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
B
bellard 已提交
6305
                    l1 = gen_new_label();
6306
                    gen_jcc1_noeob(s, op1, l1);
B
Blue Swirl 已提交
6307
                    gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
B
bellard 已提交
6308
                    gen_set_label(l1);
6309 6310
                }
                break;
B
bellard 已提交
6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321
            default:
                goto illegal_op;
            }
        }
        break;
        /************************/
        /* string ops */

    case 0xa4: /* movsS */
    case 0xa5:
        if ((b & 1) == 0)
6322
            ot = MO_8;
B
bellard 已提交
6323
        else
6324
            ot = dflag + MO_16;
B
bellard 已提交
6325 6326 6327 6328 6329 6330 6331

        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;
6332

B
bellard 已提交
6333 6334 6335
    case 0xaa: /* stosS */
    case 0xab:
        if ((b & 1) == 0)
6336
            ot = MO_8;
B
bellard 已提交
6337
        else
6338
            ot = dflag + MO_16;
B
bellard 已提交
6339 6340 6341 6342 6343 6344 6345 6346 6347 6348

        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:
        if ((b & 1) == 0)
6349
            ot = MO_8;
B
bellard 已提交
6350
        else
6351
            ot = dflag + MO_16;
B
bellard 已提交
6352 6353 6354 6355 6356 6357 6358 6359 6360
        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:
        if ((b & 1) == 0)
6361
            ot = MO_8;
B
bellard 已提交
6362
        else
6363
            ot = dflag + MO_16;
B
bellard 已提交
6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375
        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:
        if ((b & 1) == 0)
6376
            ot = MO_8;
B
bellard 已提交
6377
        else
6378
            ot = dflag + MO_16;
B
bellard 已提交
6379 6380 6381 6382 6383 6384 6385 6386 6387 6388
        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:
6389
        if ((b & 1) == 0)
6390
            ot = MO_8;
6391
        else
6392
            ot = dflag ? MO_32 : MO_16;
6393
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6394 6395
        gen_check_io(s, ot, pc_start - s->cs_base, 
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6396 6397
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6398
        } else {
6399
            gen_ins(s, ot);
P
pbrook 已提交
6400 6401 6402
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6403 6404 6405 6406
        }
        break;
    case 0x6e: /* outsS */
    case 0x6f:
6407
        if ((b & 1) == 0)
6408
            ot = MO_8;
6409
        else
6410
            ot = dflag ? MO_32 : MO_16;
6411
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6412 6413
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes) | 4);
6414 6415
        if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
            gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
B
bellard 已提交
6416
        } else {
6417
            gen_outs(s, ot);
P
pbrook 已提交
6418 6419 6420
            if (use_icount) {
                gen_jmp(s, s->pc - s->cs_base);
            }
B
bellard 已提交
6421 6422 6423 6424 6425
        }
        break;

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

B
bellard 已提交
6427 6428
    case 0xe4:
    case 0xe5:
6429
        if ((b & 1) == 0)
6430
            ot = MO_8;
6431
        else
6432
            ot = dflag ? MO_32 : MO_16;
6433
        val = cpu_ldub_code(env, s->pc++);
6434 6435
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6436 6437
        if (use_icount)
            gen_io_start();
6438
        tcg_gen_movi_i32(cpu_tmp2_i32, val);
P
pbrook 已提交
6439
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6440
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6441 6442 6443 6444
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6445 6446 6447
        break;
    case 0xe6:
    case 0xe7:
6448
        if ((b & 1) == 0)
6449
            ot = MO_8;
6450
        else
6451
            ot = dflag ? MO_32 : MO_16;
6452
        val = cpu_ldub_code(env, s->pc++);
6453 6454
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6455
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6456

P
pbrook 已提交
6457 6458
        if (use_icount)
            gen_io_start();
6459
        tcg_gen_movi_i32(cpu_tmp2_i32, val);
6460
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6461
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6462 6463 6464 6465
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6466 6467 6468
        break;
    case 0xec:
    case 0xed:
6469
        if ((b & 1) == 0)
6470
            ot = MO_8;
6471
        else
6472
            ot = dflag ? MO_32 : MO_16;
6473
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6474 6475
        gen_check_io(s, ot, pc_start - s->cs_base,
                     SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
P
pbrook 已提交
6476 6477
        if (use_icount)
            gen_io_start();
6478
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
P
pbrook 已提交
6479
        gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
B
bellard 已提交
6480
        gen_op_mov_reg_T1(ot, R_EAX);
P
pbrook 已提交
6481 6482 6483 6484
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6485 6486 6487
        break;
    case 0xee:
    case 0xef:
6488
        if ((b & 1) == 0)
6489
            ot = MO_8;
6490
        else
6491
            ot = dflag ? MO_32 : MO_16;
6492
        tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
6493 6494
        gen_check_io(s, ot, pc_start - s->cs_base,
                     svm_is_rep(prefixes));
B
bellard 已提交
6495
        gen_op_mov_TN_reg(ot, 1, R_EAX);
6496

P
pbrook 已提交
6497 6498
        if (use_icount)
            gen_io_start();
6499 6500
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
        tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
P
pbrook 已提交
6501
        gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
P
pbrook 已提交
6502 6503 6504 6505
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
6506 6507 6508 6509 6510
        break;

        /************************/
        /* control */
    case 0xc2: /* ret im */
6511
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6512 6513
        s->pc += 2;
        gen_pop_T0(s);
6514 6515
        if (CODE64(s) && s->dflag)
            s->dflag = 2;
B
bellard 已提交
6516
        gen_stack_update(s, val + (2 << s->dflag));
6517 6518 6519
        if (s->dflag == 0) {
            tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
        }
B
bellard 已提交
6520 6521 6522 6523 6524 6525
        gen_op_jmp_T0();
        gen_eob(s);
        break;
    case 0xc3: /* ret */
        gen_pop_T0(s);
        gen_pop_update(s);
6526 6527 6528
        if (s->dflag == 0) {
            tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
        }
B
bellard 已提交
6529 6530 6531 6532
        gen_op_jmp_T0();
        gen_eob(s);
        break;
    case 0xca: /* lret im */
6533
        val = cpu_ldsw_code(env, s->pc);
B
bellard 已提交
6534 6535 6536
        s->pc += 2;
    do_lret:
        if (s->pe && !s->vm86) {
6537
            gen_update_cc_op(s);
B
bellard 已提交
6538
            gen_jmp_im(pc_start - s->cs_base);
6539
            gen_helper_lret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6540
                                      tcg_const_i32(val));
B
bellard 已提交
6541 6542 6543
        } else {
            gen_stack_A0(s);
            /* pop offset */
6544
            gen_op_ld_v(s, MO_16 + s->dflag, cpu_T[0], cpu_A0);
B
bellard 已提交
6545 6546 6547 6548 6549
            /* NOTE: keeping EIP updated is not a problem in case of
               exception */
            gen_op_jmp_T0();
            /* pop selector */
            gen_op_addl_A0_im(2 << s->dflag);
6550
            gen_op_ld_v(s, MO_16 + s->dflag, cpu_T[0], cpu_A0);
6551
            gen_op_movl_seg_T0_vm(R_CS);
B
bellard 已提交
6552 6553 6554 6555 6556 6557 6558 6559 6560
            /* add stack offset */
            gen_stack_update(s, val + (4 << s->dflag));
        }
        gen_eob(s);
        break;
    case 0xcb: /* lret */
        val = 0;
        goto do_lret;
    case 0xcf: /* iret */
B
bellard 已提交
6561
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
B
bellard 已提交
6562 6563
        if (!s->pe) {
            /* real mode */
6564
            gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6565
            set_cc_op(s, CC_OP_EFLAGS);
6566 6567 6568 6569
        } else if (s->vm86) {
            if (s->iopl != 3) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
6570
                gen_helper_iret_real(cpu_env, tcg_const_i32(s->dflag));
6571
                set_cc_op(s, CC_OP_EFLAGS);
6572
            }
B
bellard 已提交
6573
        } else {
6574
            gen_update_cc_op(s);
B
bellard 已提交
6575
            gen_jmp_im(pc_start - s->cs_base);
6576
            gen_helper_iret_protected(cpu_env, tcg_const_i32(s->dflag),
P
pbrook 已提交
6577
                                      tcg_const_i32(s->pc - s->cs_base));
6578
            set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6579 6580 6581 6582 6583
        }
        gen_eob(s);
        break;
    case 0xe8: /* call im */
        {
B
bellard 已提交
6584
            if (dflag)
6585
                tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6586
            else
6587
                tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6588
            next_eip = s->pc - s->cs_base;
B
bellard 已提交
6589
            tval += next_eip;
B
bellard 已提交
6590
            if (s->dflag == 0)
B
bellard 已提交
6591
                tval &= 0xffff;
6592 6593
            else if(!CODE64(s))
                tval &= 0xffffffff;
6594
            tcg_gen_movi_tl(cpu_T[0], next_eip);
B
bellard 已提交
6595
            gen_push_T0(s);
B
bellard 已提交
6596
            gen_jmp(s, tval);
B
bellard 已提交
6597 6598 6599 6600 6601
        }
        break;
    case 0x9a: /* lcall im */
        {
            unsigned int selector, offset;
6602

B
bellard 已提交
6603 6604
            if (CODE64(s))
                goto illegal_op;
6605
            ot = dflag ? MO_32 : MO_16;
6606
            offset = insn_get(env, s, ot);
6607
            selector = insn_get(env, s, MO_16);
6608

6609
            tcg_gen_movi_tl(cpu_T[0], selector);
6610
            tcg_gen_movi_tl(cpu_T[1], offset);
B
bellard 已提交
6611 6612
        }
        goto do_lcall;
B
bellard 已提交
6613
    case 0xe9: /* jmp im */
B
bellard 已提交
6614
        if (dflag)
6615
            tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6616
        else
6617
            tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6618
        tval += s->pc - s->cs_base;
B
bellard 已提交
6619
        if (s->dflag == 0)
B
bellard 已提交
6620
            tval &= 0xffff;
6621 6622
        else if(!CODE64(s))
            tval &= 0xffffffff;
B
bellard 已提交
6623
        gen_jmp(s, tval);
B
bellard 已提交
6624 6625 6626 6627 6628
        break;
    case 0xea: /* ljmp im */
        {
            unsigned int selector, offset;

B
bellard 已提交
6629 6630
            if (CODE64(s))
                goto illegal_op;
6631
            ot = dflag ? MO_32 : MO_16;
6632
            offset = insn_get(env, s, ot);
6633
            selector = insn_get(env, s, MO_16);
6634

6635
            tcg_gen_movi_tl(cpu_T[0], selector);
6636
            tcg_gen_movi_tl(cpu_T[1], offset);
B
bellard 已提交
6637 6638 6639
        }
        goto do_ljmp;
    case 0xeb: /* jmp Jb */
6640
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6641
        tval += s->pc - s->cs_base;
B
bellard 已提交
6642
        if (s->dflag == 0)
B
bellard 已提交
6643 6644
            tval &= 0xffff;
        gen_jmp(s, tval);
B
bellard 已提交
6645 6646
        break;
    case 0x70 ... 0x7f: /* jcc Jb */
6647
        tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
6648 6649 6650
        goto do_jcc;
    case 0x180 ... 0x18f: /* jcc Jv */
        if (dflag) {
6651
            tval = (int32_t)insn_get(env, s, MO_32);
B
bellard 已提交
6652
        } else {
6653
            tval = (int16_t)insn_get(env, s, MO_16);
B
bellard 已提交
6654 6655 6656
        }
    do_jcc:
        next_eip = s->pc - s->cs_base;
B
bellard 已提交
6657
        tval += next_eip;
B
bellard 已提交
6658
        if (s->dflag == 0)
B
bellard 已提交
6659 6660
            tval &= 0xffff;
        gen_jcc(s, b, tval, next_eip);
B
bellard 已提交
6661 6662 6663
        break;

    case 0x190 ... 0x19f: /* setcc Gv */
6664
        modrm = cpu_ldub_code(env, s->pc++);
6665
        gen_setcc1(s, b, cpu_T[0]);
6666
        gen_ldst_modrm(env, s, modrm, MO_8, OR_TMP0, 1);
B
bellard 已提交
6667 6668
        break;
    case 0x140 ... 0x14f: /* cmov Gv, Ev */
6669 6670 6671
        if (!(s->cpuid_features & CPUID_CMOV)) {
            goto illegal_op;
        }
6672
        ot = dflag + MO_16;
6673 6674 6675
        modrm = cpu_ldub_code(env, s->pc++);
        reg = ((modrm >> 3) & 7) | rex_r;
        gen_cmovcc1(env, s, ot, b, modrm, reg);
B
bellard 已提交
6676
        break;
6677

B
bellard 已提交
6678 6679 6680
        /************************/
        /* flags */
    case 0x9c: /* pushf */
B
bellard 已提交
6681
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
B
bellard 已提交
6682 6683 6684
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
6685
            gen_update_cc_op(s);
6686
            gen_helper_read_eflags(cpu_T[0], cpu_env);
B
bellard 已提交
6687 6688 6689 6690
            gen_push_T0(s);
        }
        break;
    case 0x9d: /* popf */
B
bellard 已提交
6691
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
B
bellard 已提交
6692 6693 6694 6695 6696 6697
        if (s->vm86 && s->iopl != 3) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
            gen_pop_T0(s);
            if (s->cpl == 0) {
                if (s->dflag) {
6698 6699 6700 6701 6702
                    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 已提交
6703
                } else {
6704 6705 6706 6707 6708
                    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 已提交
6709 6710
                }
            } else {
B
bellard 已提交
6711 6712
                if (s->cpl <= s->iopl) {
                    if (s->dflag) {
6713 6714 6715 6716 6717 6718
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                                tcg_const_i32((TF_MASK |
                                                               AC_MASK |
                                                               ID_MASK |
                                                               NT_MASK |
                                                               IF_MASK)));
B
bellard 已提交
6719
                    } else {
6720 6721 6722 6723 6724 6725 6726
                        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 已提交
6727
                    }
B
bellard 已提交
6728
                } else {
B
bellard 已提交
6729
                    if (s->dflag) {
6730 6731 6732
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)));
B
bellard 已提交
6733
                    } else {
6734 6735 6736 6737
                        gen_helper_write_eflags(cpu_env, cpu_T[0],
                                           tcg_const_i32((TF_MASK | AC_MASK |
                                                          ID_MASK | NT_MASK)
                                                         & 0xffff));
B
bellard 已提交
6738
                    }
B
bellard 已提交
6739 6740 6741
                }
            }
            gen_pop_update(s);
6742
            set_cc_op(s, CC_OP_EFLAGS);
H
H. Peter Anvin 已提交
6743
            /* abort translation because TF/AC flag may change */
B
bellard 已提交
6744
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
6745 6746 6747 6748
            gen_eob(s);
        }
        break;
    case 0x9e: /* sahf */
B
bellard 已提交
6749
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6750
            goto illegal_op;
6751
        gen_op_mov_TN_reg(MO_8, 0, R_AH);
6752
        gen_compute_eflags(s);
6753 6754 6755
        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 已提交
6756 6757
        break;
    case 0x9f: /* lahf */
B
bellard 已提交
6758
        if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
B
bellard 已提交
6759
            goto illegal_op;
6760
        gen_compute_eflags(s);
6761
        /* Note: gen_compute_eflags() only gives the condition codes */
6762
        tcg_gen_ori_tl(cpu_T[0], cpu_cc_src, 0x02);
6763
        gen_op_mov_reg_T0(MO_8, R_AH);
B
bellard 已提交
6764 6765
        break;
    case 0xf5: /* cmc */
6766
        gen_compute_eflags(s);
6767
        tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6768 6769
        break;
    case 0xf8: /* clc */
6770
        gen_compute_eflags(s);
6771
        tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
B
bellard 已提交
6772 6773
        break;
    case 0xf9: /* stc */
6774
        gen_compute_eflags(s);
6775
        tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
B
bellard 已提交
6776 6777
        break;
    case 0xfc: /* cld */
6778
        tcg_gen_movi_i32(cpu_tmp2_i32, 1);
6779
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6780 6781
        break;
    case 0xfd: /* std */
6782
        tcg_gen_movi_i32(cpu_tmp2_i32, -1);
6783
        tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
B
bellard 已提交
6784 6785 6786 6787 6788
        break;

        /************************/
        /* bit operations */
    case 0x1ba: /* bt/bts/btr/btc Gv, im */
6789
        ot = dflag + MO_16;
6790
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6791
        op = (modrm >> 3) & 7;
B
bellard 已提交
6792
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6793
        rm = (modrm & 7) | REX_B(s);
B
bellard 已提交
6794
        if (mod != 3) {
B
bellard 已提交
6795
            s->rip_offset = 1;
6796
            gen_lea_modrm(env, s, modrm);
6797
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6798
        } else {
B
bellard 已提交
6799
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6800 6801
        }
        /* load shift */
6802
        val = cpu_ldub_code(env, s->pc++);
6803
        tcg_gen_movi_tl(cpu_T[1], val);
B
bellard 已提交
6804 6805 6806
        if (op < 4)
            goto illegal_op;
        op -= 4;
B
bellard 已提交
6807
        goto bt_op;
B
bellard 已提交
6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819
    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:
6820
        ot = dflag + MO_16;
6821
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
6822
        reg = ((modrm >> 3) & 7) | rex_r;
B
bellard 已提交
6823
        mod = (modrm >> 6) & 3;
B
bellard 已提交
6824
        rm = (modrm & 7) | REX_B(s);
6825
        gen_op_mov_TN_reg(MO_32, 1, reg);
B
bellard 已提交
6826
        if (mod != 3) {
6827
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
6828
            /* specific case: we need to add a displacement */
B
bellard 已提交
6829 6830 6831 6832
            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);
6833
            gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
B
bellard 已提交
6834
        } else {
B
bellard 已提交
6835
            gen_op_mov_TN_reg(ot, 0, rm);
B
bellard 已提交
6836
        }
B
bellard 已提交
6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864
    bt_op:
        tcg_gen_andi_tl(cpu_T[1], cpu_T[1], (1 << (3 + ot)) - 1);
        switch(op) {
        case 0:
            tcg_gen_shr_tl(cpu_cc_src, cpu_T[0], cpu_T[1]);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
            break;
        case 1:
            tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[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_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
            tcg_gen_movi_tl(cpu_tmp0, 1);
            tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
            tcg_gen_not_tl(cpu_tmp0, cpu_tmp0);
            tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
            break;
        default:
        case 3:
            tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
            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;
        }
6865
        set_cc_op(s, CC_OP_SARB + ot);
B
bellard 已提交
6866
        if (op != 0) {
6867 6868 6869
            if (mod != 3) {
                gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
            } else {
B
bellard 已提交
6870
                gen_op_mov_reg_T0(ot, rm);
6871
            }
B
bellard 已提交
6872 6873
            tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
            tcg_gen_movi_tl(cpu_cc_dst, 0);
B
bellard 已提交
6874 6875
        }
        break;
6876 6877
    case 0x1bc: /* bsf / tzcnt */
    case 0x1bd: /* bsr / lzcnt */
6878
        ot = dflag + MO_16;
6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895
        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 已提交
6896
            } else {
6897 6898 6899 6900 6901
                /* 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 已提交
6902
            }
6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925
            /* 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 已提交
6926
        }
6927
        gen_op_mov_reg_T0(ot, reg);
B
bellard 已提交
6928 6929 6930 6931
        break;
        /************************/
        /* bcd */
    case 0x27: /* daa */
B
bellard 已提交
6932 6933
        if (CODE64(s))
            goto illegal_op;
6934
        gen_update_cc_op(s);
6935
        gen_helper_daa(cpu_env);
6936
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6937 6938
        break;
    case 0x2f: /* das */
B
bellard 已提交
6939 6940
        if (CODE64(s))
            goto illegal_op;
6941
        gen_update_cc_op(s);
6942
        gen_helper_das(cpu_env);
6943
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6944 6945
        break;
    case 0x37: /* aaa */
B
bellard 已提交
6946 6947
        if (CODE64(s))
            goto illegal_op;
6948
        gen_update_cc_op(s);
6949
        gen_helper_aaa(cpu_env);
6950
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6951 6952
        break;
    case 0x3f: /* aas */
B
bellard 已提交
6953 6954
        if (CODE64(s))
            goto illegal_op;
6955
        gen_update_cc_op(s);
6956
        gen_helper_aas(cpu_env);
6957
        set_cc_op(s, CC_OP_EFLAGS);
B
bellard 已提交
6958 6959
        break;
    case 0xd4: /* aam */
B
bellard 已提交
6960 6961
        if (CODE64(s))
            goto illegal_op;
6962
        val = cpu_ldub_code(env, s->pc++);
6963 6964 6965
        if (val == 0) {
            gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
        } else {
6966
            gen_helper_aam(cpu_env, tcg_const_i32(val));
6967
            set_cc_op(s, CC_OP_LOGICB);
6968
        }
B
bellard 已提交
6969 6970
        break;
    case 0xd5: /* aad */
B
bellard 已提交
6971 6972
        if (CODE64(s))
            goto illegal_op;
6973
        val = cpu_ldub_code(env, s->pc++);
6974
        gen_helper_aad(cpu_env, tcg_const_i32(val));
6975
        set_cc_op(s, CC_OP_LOGICB);
B
bellard 已提交
6976 6977 6978 6979
        break;
        /************************/
        /* misc */
    case 0x90: /* nop */
6980
        /* XXX: correct lock test for all insn */
R
Richard Henderson 已提交
6981
        if (prefixes & PREFIX_LOCK) {
6982
            goto illegal_op;
R
Richard Henderson 已提交
6983 6984 6985 6986 6987
        }
        /* 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 已提交
6988
        if (prefixes & PREFIX_REPZ) {
6989 6990 6991 6992
            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 已提交
6993
        }
B
bellard 已提交
6994 6995
        break;
    case 0x9b: /* fwait */
6996
        if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
B
bellard 已提交
6997 6998
            (HF_MP_MASK | HF_TS_MASK)) {
            gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
B
bellard 已提交
6999
        } else {
7000
            gen_update_cc_op(s);
B
bellard 已提交
7001
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7002
            gen_helper_fwait(cpu_env);
B
bellard 已提交
7003
        }
B
bellard 已提交
7004 7005 7006 7007 7008
        break;
    case 0xcc: /* int3 */
        gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
        break;
    case 0xcd: /* int N */
7009
        val = cpu_ldub_code(env, s->pc++);
7010
        if (s->vm86 && s->iopl != 3) {
7011
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7012 7013 7014
        } else {
            gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
        }
B
bellard 已提交
7015 7016
        break;
    case 0xce: /* into */
B
bellard 已提交
7017 7018
        if (CODE64(s))
            goto illegal_op;
7019
        gen_update_cc_op(s);
7020
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7021
        gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7022
        break;
A
aurel32 已提交
7023
#ifdef WANT_ICEBP
B
bellard 已提交
7024
    case 0xf1: /* icebp (undocumented, exits to external debugger) */
B
bellard 已提交
7025
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
7026
#if 1
B
bellard 已提交
7027
        gen_debug(s, pc_start - s->cs_base);
7028 7029
#else
        /* start debug */
7030
        tb_flush(env);
7031
        qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
7032
#endif
B
bellard 已提交
7033
        break;
A
aurel32 已提交
7034
#endif
B
bellard 已提交
7035 7036 7037
    case 0xfa: /* cli */
        if (!s->vm86) {
            if (s->cpl <= s->iopl) {
7038
                gen_helper_cli(cpu_env);
B
bellard 已提交
7039 7040 7041 7042 7043
            } else {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            }
        } else {
            if (s->iopl == 3) {
7044
                gen_helper_cli(cpu_env);
B
bellard 已提交
7045 7046 7047 7048 7049 7050 7051 7052 7053
            } 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:
7054
                gen_helper_sti(cpu_env);
B
bellard 已提交
7055
                /* interruptions are enabled only the first insn after sti */
7056 7057 7058
                /* If several instructions disable interrupts, only the
                   _first_ does it */
                if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
7059
                    gen_helper_set_inhibit_irq(cpu_env);
B
bellard 已提交
7060
                /* give a chance to handle pending irqs */
B
bellard 已提交
7061
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074
                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 已提交
7075 7076
        if (CODE64(s))
            goto illegal_op;
7077
        ot = dflag ? MO_32 : MO_16;
7078
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7079 7080 7081 7082
        reg = (modrm >> 3) & 7;
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
B
bellard 已提交
7083
        gen_op_mov_TN_reg(ot, 0, reg);
7084
        gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7085
        gen_jmp_im(pc_start - s->cs_base);
7086
        tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7087
        if (ot == MO_16) {
B
Blue Swirl 已提交
7088 7089 7090 7091
            gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
        } else {
            gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
        }
B
bellard 已提交
7092 7093
        break;
    case 0x1c8 ... 0x1cf: /* bswap reg */
B
bellard 已提交
7094 7095 7096
        reg = (b & 7) | REX_B(s);
#ifdef TARGET_X86_64
        if (dflag == 2) {
7097
            gen_op_mov_TN_reg(MO_64, 0, reg);
A
aurel32 已提交
7098
            tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
7099
            gen_op_mov_reg_T0(MO_64, reg);
7100
        } else
7101
#endif
B
bellard 已提交
7102
        {
7103
            gen_op_mov_TN_reg(MO_32, 0, reg);
7104 7105
            tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
            tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
7106
            gen_op_mov_reg_T0(MO_32, reg);
B
bellard 已提交
7107
        }
B
bellard 已提交
7108 7109
        break;
    case 0xd6: /* salc */
B
bellard 已提交
7110 7111
        if (CODE64(s))
            goto illegal_op;
7112
        gen_compute_eflags_c(s, cpu_T[0]);
7113
        tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
7114
        gen_op_mov_reg_T0(MO_8, R_EAX);
B
bellard 已提交
7115 7116 7117 7118 7119
        break;
    case 0xe0: /* loopnz */
    case 0xe1: /* loopz */
    case 0xe2: /* loop */
    case 0xe3: /* jecxz */
B
bellard 已提交
7120
        {
7121
            int l1, l2, l3;
B
bellard 已提交
7122

7123
            tval = (int8_t)insn_get(env, s, MO_8);
B
bellard 已提交
7124 7125 7126 7127
            next_eip = s->pc - s->cs_base;
            tval += next_eip;
            if (s->dflag == 0)
                tval &= 0xffff;
7128

B
bellard 已提交
7129 7130
            l1 = gen_new_label();
            l2 = gen_new_label();
7131
            l3 = gen_new_label();
B
bellard 已提交
7132
            b &= 3;
7133 7134 7135
            switch(b) {
            case 0: /* loopnz */
            case 1: /* loopz */
7136
                gen_op_add_reg_im(s->aflag + 1, R_ECX, -1);
7137
                gen_op_jz_ecx(s->aflag, l3);
7138
                gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7139 7140
                break;
            case 2: /* loop */
7141
                gen_op_add_reg_im(s->aflag + 1, R_ECX, -1);
7142 7143 7144 7145 7146 7147
                gen_op_jnz_ecx(s->aflag, l1);
                break;
            default:
            case 3: /* jcxz */
                gen_op_jz_ecx(s->aflag, l1);
                break;
B
bellard 已提交
7148 7149
            }

7150
            gen_set_label(l3);
B
bellard 已提交
7151
            gen_jmp_im(next_eip);
7152
            tcg_gen_br(l2);
7153

B
bellard 已提交
7154 7155 7156 7157 7158
            gen_set_label(l1);
            gen_jmp_im(tval);
            gen_set_label(l2);
            gen_eob(s);
        }
B
bellard 已提交
7159 7160 7161 7162 7163 7164
        break;
    case 0x130: /* wrmsr */
    case 0x132: /* rdmsr */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7165
            gen_update_cc_op(s);
B
bellard 已提交
7166
            gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7167
            if (b & 2) {
B
Blue Swirl 已提交
7168
                gen_helper_rdmsr(cpu_env);
T
ths 已提交
7169
            } else {
B
Blue Swirl 已提交
7170
                gen_helper_wrmsr(cpu_env);
T
ths 已提交
7171
            }
B
bellard 已提交
7172 7173 7174
        }
        break;
    case 0x131: /* rdtsc */
7175
        gen_update_cc_op(s);
B
bellard 已提交
7176
        gen_jmp_im(pc_start - s->cs_base);
P
pbrook 已提交
7177 7178
        if (use_icount)
            gen_io_start();
B
Blue Swirl 已提交
7179
        gen_helper_rdtsc(cpu_env);
P
pbrook 已提交
7180 7181 7182 7183
        if (use_icount) {
            gen_io_end();
            gen_jmp(s, s->pc - s->cs_base);
        }
B
bellard 已提交
7184
        break;
7185
    case 0x133: /* rdpmc */
7186
        gen_update_cc_op(s);
7187
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7188
        gen_helper_rdpmc(cpu_env);
7189
        break;
7190
    case 0x134: /* sysenter */
7191
        /* For Intel SYSENTER is valid on 64-bit */
7192
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7193
            goto illegal_op;
7194 7195 7196
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7197
            gen_update_cc_op(s);
B
bellard 已提交
7198
            gen_jmp_im(pc_start - s->cs_base);
7199
            gen_helper_sysenter(cpu_env);
7200 7201 7202 7203
            gen_eob(s);
        }
        break;
    case 0x135: /* sysexit */
7204
        /* For Intel SYSEXIT is valid on 64-bit */
7205
        if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
B
bellard 已提交
7206
            goto illegal_op;
7207 7208 7209
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7210
            gen_update_cc_op(s);
B
bellard 已提交
7211
            gen_jmp_im(pc_start - s->cs_base);
7212
            gen_helper_sysexit(cpu_env, tcg_const_i32(dflag));
7213 7214 7215
            gen_eob(s);
        }
        break;
B
bellard 已提交
7216 7217 7218
#ifdef TARGET_X86_64
    case 0x105: /* syscall */
        /* XXX: is it usable in real mode ? */
J
Jun Koi 已提交
7219
        gen_update_cc_op(s);
B
bellard 已提交
7220
        gen_jmp_im(pc_start - s->cs_base);
7221
        gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7222 7223 7224 7225 7226 7227
        gen_eob(s);
        break;
    case 0x107: /* sysret */
        if (!s->pe) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
J
Jun Koi 已提交
7228
            gen_update_cc_op(s);
B
bellard 已提交
7229
            gen_jmp_im(pc_start - s->cs_base);
7230
            gen_helper_sysret(cpu_env, tcg_const_i32(s->dflag));
7231
            /* condition codes are modified only in long mode */
7232 7233 7234
            if (s->lma) {
                set_cc_op(s, CC_OP_EFLAGS);
            }
B
bellard 已提交
7235 7236 7237 7238
            gen_eob(s);
        }
        break;
#endif
B
bellard 已提交
7239
    case 0x1a2: /* cpuid */
7240
        gen_update_cc_op(s);
B
bellard 已提交
7241
        gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7242
        gen_helper_cpuid(cpu_env);
B
bellard 已提交
7243 7244 7245 7246 7247
        break;
    case 0xf4: /* hlt */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
7248
            gen_update_cc_op(s);
7249
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7250
            gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
J
Jun Koi 已提交
7251
            s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7252 7253 7254
        }
        break;
    case 0x100:
7255
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7256 7257 7258 7259
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* sldt */
7260 7261
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7262
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
B
bellard 已提交
7263
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,ldt.selector));
7264
            ot = MO_16;
B
bellard 已提交
7265 7266
            if (mod == 3)
                ot += s->dflag;
7267
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7268 7269
            break;
        case 2: /* lldt */
7270 7271
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7272 7273 7274
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7275
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
7276
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
7277
                gen_jmp_im(pc_start - s->cs_base);
7278
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7279
                gen_helper_lldt(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7280 7281 7282
            }
            break;
        case 1: /* str */
7283 7284
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7285
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
B
bellard 已提交
7286
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,tr.selector));
7287
            ot = MO_16;
B
bellard 已提交
7288 7289
            if (mod == 3)
                ot += s->dflag;
7290
            gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
B
bellard 已提交
7291 7292
            break;
        case 3: /* ltr */
7293 7294
            if (!s->pe || s->vm86)
                goto illegal_op;
B
bellard 已提交
7295 7296 7297
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7298
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
7299
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
bellard 已提交
7300
                gen_jmp_im(pc_start - s->cs_base);
7301
                tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
7302
                gen_helper_ltr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7303 7304 7305 7306
            }
            break;
        case 4: /* verr */
        case 5: /* verw */
7307 7308
            if (!s->pe || s->vm86)
                goto illegal_op;
7309
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7310
            gen_update_cc_op(s);
7311 7312 7313 7314 7315
            if (op == 4) {
                gen_helper_verr(cpu_env, cpu_T[0]);
            } else {
                gen_helper_verw(cpu_env, cpu_T[0]);
            }
7316
            set_cc_op(s, CC_OP_EFLAGS);
7317
            break;
B
bellard 已提交
7318 7319 7320 7321 7322
        default:
            goto illegal_op;
        }
        break;
    case 0x101:
7323
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7324 7325
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
B
bellard 已提交
7326
        rm = modrm & 7;
B
bellard 已提交
7327 7328 7329 7330
        switch(op) {
        case 0: /* sgdt */
            if (mod == 3)
                goto illegal_op;
B
bellard 已提交
7331
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
7332
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7333
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.limit));
7334
            gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
7335
            gen_add_A0_im(s, 2);
B
bellard 已提交
7336
            tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.base));
7337 7338 7339
            if (s->dflag == 0) {
                tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
            }
7340
            gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7341
            break;
B
bellard 已提交
7342 7343 7344 7345 7346 7347 7348
        case 1:
            if (mod == 3) {
                switch (rm) {
                case 0: /* monitor */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
7349
                    gen_update_cc_op(s);
B
bellard 已提交
7350 7351 7352
                    gen_jmp_im(pc_start - s->cs_base);
#ifdef TARGET_X86_64
                    if (s->aflag == 2) {
7353
                        gen_op_movq_A0_reg(R_EAX);
7354
                    } else
B
bellard 已提交
7355 7356
#endif
                    {
7357
                        gen_op_movl_A0_reg(R_EAX);
B
bellard 已提交
7358
                        if (s->aflag == 0)
7359
                            tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
B
bellard 已提交
7360 7361
                    }
                    gen_add_A0_ds_seg(s);
B
Blue Swirl 已提交
7362
                    gen_helper_monitor(cpu_env, cpu_A0);
B
bellard 已提交
7363 7364 7365 7366 7367
                    break;
                case 1: /* mwait */
                    if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
                        s->cpl != 0)
                        goto illegal_op;
J
Jun Koi 已提交
7368
                    gen_update_cc_op(s);
7369
                    gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7370
                    gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
B
bellard 已提交
7371 7372
                    gen_eob(s);
                    break;
H
H. Peter Anvin 已提交
7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390
                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 已提交
7391 7392 7393 7394
                default:
                    goto illegal_op;
                }
            } else { /* sidt */
B
bellard 已提交
7395
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
7396
                gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7397
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.limit));
7398
                gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
B
bellard 已提交
7399
                gen_add_A0_im(s, 2);
B
bellard 已提交
7400
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.base));
7401 7402 7403
                if (s->dflag == 0) {
                    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
                }
7404
                gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7405 7406
            }
            break;
B
bellard 已提交
7407 7408
        case 2: /* lgdt */
        case 3: /* lidt */
T
ths 已提交
7409
            if (mod == 3) {
7410
                gen_update_cc_op(s);
B
bellard 已提交
7411
                gen_jmp_im(pc_start - s->cs_base);
T
ths 已提交
7412 7413
                switch(rm) {
                case 0: /* VMRUN */
B
bellard 已提交
7414 7415 7416 7417
                    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 已提交
7418
                        break;
B
bellard 已提交
7419
                    } else {
B
Blue Swirl 已提交
7420
                        gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag),
P
pbrook 已提交
7421
                                         tcg_const_i32(s->pc - pc_start));
7422
                        tcg_gen_exit_tb(0);
J
Jun Koi 已提交
7423
                        s->is_jmp = DISAS_TB_JUMP;
B
bellard 已提交
7424
                    }
T
ths 已提交
7425 7426
                    break;
                case 1: /* VMMCALL */
B
bellard 已提交
7427 7428
                    if (!(s->flags & HF_SVME_MASK))
                        goto illegal_op;
B
Blue Swirl 已提交
7429
                    gen_helper_vmmcall(cpu_env);
T
ths 已提交
7430 7431
                    break;
                case 2: /* VMLOAD */
B
bellard 已提交
7432 7433 7434 7435 7436 7437
                    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 已提交
7438
                        gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7439
                    }
T
ths 已提交
7440 7441
                    break;
                case 3: /* VMSAVE */
B
bellard 已提交
7442 7443 7444 7445 7446 7447
                    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 已提交
7448
                        gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7449
                    }
T
ths 已提交
7450 7451
                    break;
                case 4: /* STGI */
B
bellard 已提交
7452 7453 7454 7455 7456 7457 7458 7459
                    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 已提交
7460
                        gen_helper_stgi(cpu_env);
B
bellard 已提交
7461
                    }
T
ths 已提交
7462 7463
                    break;
                case 5: /* CLGI */
B
bellard 已提交
7464 7465 7466 7467 7468 7469
                    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 已提交
7470
                        gen_helper_clgi(cpu_env);
B
bellard 已提交
7471
                    }
T
ths 已提交
7472 7473
                    break;
                case 6: /* SKINIT */
B
bellard 已提交
7474 7475 7476 7477
                    if ((!(s->flags & HF_SVME_MASK) && 
                         !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) || 
                        !s->pe)
                        goto illegal_op;
B
Blue Swirl 已提交
7478
                    gen_helper_skinit(cpu_env);
T
ths 已提交
7479 7480
                    break;
                case 7: /* INVLPGA */
B
bellard 已提交
7481 7482 7483 7484 7485 7486
                    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 已提交
7487
                        gen_helper_invlpga(cpu_env, tcg_const_i32(s->aflag));
B
bellard 已提交
7488
                    }
T
ths 已提交
7489 7490 7491 7492 7493
                    break;
                default:
                    goto illegal_op;
                }
            } else if (s->cpl != 0) {
B
bellard 已提交
7494 7495
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7496 7497
                gen_svm_check_intercept(s, pc_start,
                                        op==2 ? SVM_EXIT_GDTR_WRITE : SVM_EXIT_IDTR_WRITE);
7498
                gen_lea_modrm(env, s, modrm);
7499
                gen_op_ld_v(s, MO_16, cpu_T[1], cpu_A0);
7500
                gen_add_A0_im(s, 2);
7501
                gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
7502 7503 7504
                if (s->dflag == 0) {
                    tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
                }
B
bellard 已提交
7505
                if (op == 2) {
B
bellard 已提交
7506 7507
                    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 已提交
7508
                } else {
B
bellard 已提交
7509 7510
                    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 已提交
7511 7512 7513 7514
                }
            }
            break;
        case 4: /* smsw */
B
bellard 已提交
7515
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
7516
#if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
7517 7518
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]) + 4);
#else
B
bellard 已提交
7519
            tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]));
7520
#endif
7521
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 1);
B
bellard 已提交
7522 7523 7524 7525 7526
            break;
        case 6: /* lmsw */
            if (s->cpl != 0) {
                gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
            } else {
B
bellard 已提交
7527
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7528
                gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
B
Blue Swirl 已提交
7529
                gen_helper_lmsw(cpu_env, cpu_T[0]);
B
bellard 已提交
7530
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7531
                gen_eob(s);
B
bellard 已提交
7532 7533
            }
            break;
A
Andre Przywara 已提交
7534 7535 7536 7537 7538
        case 7:
            if (mod != 3) { /* invlpg */
                if (s->cpl != 0) {
                    gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
                } else {
7539
                    gen_update_cc_op(s);
A
Andre Przywara 已提交
7540
                    gen_jmp_im(pc_start - s->cs_base);
7541
                    gen_lea_modrm(env, s, modrm);
B
Blue Swirl 已提交
7542
                    gen_helper_invlpg(cpu_env, cpu_A0);
A
Andre Przywara 已提交
7543 7544 7545
                    gen_jmp_im(s->pc - s->cs_base);
                    gen_eob(s);
                }
B
bellard 已提交
7546
            } else {
A
Andre Przywara 已提交
7547 7548
                switch (rm) {
                case 0: /* swapgs */
B
bellard 已提交
7549
#ifdef TARGET_X86_64
A
Andre Przywara 已提交
7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562
                    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));
                        }
7563
                    } else
B
bellard 已提交
7564 7565 7566 7567
#endif
                    {
                        goto illegal_op;
                    }
A
Andre Przywara 已提交
7568 7569 7570 7571
                    break;
                case 1: /* rdtscp */
                    if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP))
                        goto illegal_op;
7572
                    gen_update_cc_op(s);
B
bellard 已提交
7573
                    gen_jmp_im(pc_start - s->cs_base);
A
Andre Przywara 已提交
7574 7575
                    if (use_icount)
                        gen_io_start();
B
Blue Swirl 已提交
7576
                    gen_helper_rdtscp(cpu_env);
A
Andre Przywara 已提交
7577 7578 7579 7580 7581 7582 7583
                    if (use_icount) {
                        gen_io_end();
                        gen_jmp(s, s->pc - s->cs_base);
                    }
                    break;
                default:
                    goto illegal_op;
B
bellard 已提交
7584
                }
B
bellard 已提交
7585 7586 7587 7588 7589 7590
            }
            break;
        default:
            goto illegal_op;
        }
        break;
7591 7592 7593 7594 7595
    case 0x108: /* invd */
    case 0x109: /* wbinvd */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
B
bellard 已提交
7596
            gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
7597 7598 7599
            /* nothing to do */
        }
        break;
B
bellard 已提交
7600 7601 7602 7603 7604
    case 0x63: /* arpl or movslS (x86_64) */
#ifdef TARGET_X86_64
        if (CODE64(s)) {
            int d_ot;
            /* d_ot is the size of destination */
7605
            d_ot = dflag + MO_16;
B
bellard 已提交
7606

7607
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7608 7609 7610
            reg = ((modrm >> 3) & 7) | rex_r;
            mod = (modrm >> 6) & 3;
            rm = (modrm & 7) | REX_B(s);
7611

B
bellard 已提交
7612
            if (mod == 3) {
7613
                gen_op_mov_TN_reg(MO_32, 0, rm);
B
bellard 已提交
7614
                /* sign extend */
7615
                if (d_ot == MO_64) {
B
bellard 已提交
7616
                    tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
7617
                }
B
bellard 已提交
7618
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7619
            } else {
7620
                gen_lea_modrm(env, s, modrm);
R
Richard Henderson 已提交
7621
                gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T[0], cpu_A0);
B
bellard 已提交
7622
                gen_op_mov_reg_T0(d_ot, reg);
B
bellard 已提交
7623
            }
7624
        } else
B
bellard 已提交
7625 7626
#endif
        {
7627
            int label1;
L
Laurent Desnogues 已提交
7628
            TCGv t0, t1, t2, a0;
7629

B
bellard 已提交
7630 7631
            if (!s->pe || s->vm86)
                goto illegal_op;
P
pbrook 已提交
7632 7633 7634
            t0 = tcg_temp_local_new();
            t1 = tcg_temp_local_new();
            t2 = tcg_temp_local_new();
7635
            ot = MO_16;
7636
            modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7637 7638 7639 7640
            reg = (modrm >> 3) & 7;
            mod = (modrm >> 6) & 3;
            rm = modrm & 7;
            if (mod != 3) {
7641
                gen_lea_modrm(env, s, modrm);
7642
                gen_op_ld_v(s, ot, t0, cpu_A0);
L
Laurent Desnogues 已提交
7643 7644
                a0 = tcg_temp_local_new();
                tcg_gen_mov_tl(a0, cpu_A0);
B
bellard 已提交
7645
            } else {
7646
                gen_op_mov_v_reg(ot, t0, rm);
L
Laurent Desnogues 已提交
7647
                TCGV_UNUSED(a0);
B
bellard 已提交
7648
            }
7649 7650 7651 7652
            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);
7653
            label1 = gen_new_label();
7654 7655 7656 7657
            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);
7658
            gen_set_label(label1);
B
bellard 已提交
7659
            if (mod != 3) {
7660
                gen_op_st_v(s, ot, t0, a0);
L
Laurent Desnogues 已提交
7661 7662
                tcg_temp_free(a0);
           } else {
7663
                gen_op_mov_reg_v(ot, rm, t0);
B
bellard 已提交
7664
            }
7665
            gen_compute_eflags(s);
7666
            tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
7667 7668 7669 7670
            tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
            tcg_temp_free(t0);
            tcg_temp_free(t1);
            tcg_temp_free(t2);
7671 7672
        }
        break;
B
bellard 已提交
7673 7674
    case 0x102: /* lar */
    case 0x103: /* lsl */
7675 7676
        {
            int label1;
7677
            TCGv t0;
7678 7679
            if (!s->pe || s->vm86)
                goto illegal_op;
7680
            ot = dflag ? MO_32 : MO_16;
7681
            modrm = cpu_ldub_code(env, s->pc++);
7682
            reg = ((modrm >> 3) & 7) | rex_r;
7683
            gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
P
pbrook 已提交
7684
            t0 = tcg_temp_local_new();
7685
            gen_update_cc_op(s);
7686 7687 7688 7689 7690
            if (b == 0x102) {
                gen_helper_lar(t0, cpu_env, cpu_T[0]);
            } else {
                gen_helper_lsl(t0, cpu_env, cpu_T[0]);
            }
7691 7692
            tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
            label1 = gen_new_label();
P
pbrook 已提交
7693
            tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
7694
            gen_op_mov_reg_v(ot, reg, t0);
7695
            gen_set_label(label1);
7696
            set_cc_op(s, CC_OP_EFLAGS);
7697
            tcg_temp_free(t0);
7698
        }
B
bellard 已提交
7699 7700
        break;
    case 0x118:
7701
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7702 7703 7704 7705 7706 7707 7708 7709 7710
        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;
7711
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7712 7713
            /* nothing more to do */
            break;
B
bellard 已提交
7714
        default: /* nop (multi byte) */
7715
            gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7716
            break;
B
bellard 已提交
7717 7718
        }
        break;
B
bellard 已提交
7719
    case 0x119 ... 0x11f: /* nop (multi byte) */
7720 7721
        modrm = cpu_ldub_code(env, s->pc++);
        gen_nop_modrm(env, s, modrm);
B
bellard 已提交
7722
        break;
B
bellard 已提交
7723 7724 7725 7726 7727
    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 {
7728
            modrm = cpu_ldub_code(env, s->pc++);
7729 7730 7731 7732 7733
            /* 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 已提交
7734 7735 7736
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
7737
                ot = MO_64;
B
bellard 已提交
7738
            else
7739
                ot = MO_32;
7740 7741 7742 7743
            if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
                (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
                reg = 8;
            }
B
bellard 已提交
7744 7745 7746 7747 7748
            switch(reg) {
            case 0:
            case 2:
            case 3:
            case 4:
B
bellard 已提交
7749
            case 8:
7750
                gen_update_cc_op(s);
B
bellard 已提交
7751
                gen_jmp_im(pc_start - s->cs_base);
B
bellard 已提交
7752
                if (b & 2) {
B
bellard 已提交
7753
                    gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
7754 7755
                    gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
                                         cpu_T[0]);
B
bellard 已提交
7756
                    gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7757 7758
                    gen_eob(s);
                } else {
B
Blue Swirl 已提交
7759
                    gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
B
bellard 已提交
7760
                    gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772
                }
                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 {
7773
            modrm = cpu_ldub_code(env, s->pc++);
7774 7775 7776 7777 7778
            /* 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 已提交
7779 7780 7781
            rm = (modrm & 7) | REX_B(s);
            reg = ((modrm >> 3) & 7) | rex_r;
            if (CODE64(s))
7782
                ot = MO_64;
B
bellard 已提交
7783
            else
7784
                ot = MO_32;
B
bellard 已提交
7785
            /* XXX: do it dynamically with CR4.DE bit */
B
bellard 已提交
7786
            if (reg == 4 || reg == 5 || reg >= 8)
B
bellard 已提交
7787 7788
                goto illegal_op;
            if (b & 2) {
T
ths 已提交
7789
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
B
bellard 已提交
7790
                gen_op_mov_TN_reg(ot, 0, rm);
B
Blue Swirl 已提交
7791
                gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
B
bellard 已提交
7792
                gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7793 7794
                gen_eob(s);
            } else {
T
ths 已提交
7795
                gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
B
bellard 已提交
7796
                tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
B
bellard 已提交
7797
                gen_op_mov_reg_T0(ot, rm);
B
bellard 已提交
7798 7799 7800 7801 7802 7803 7804
            }
        }
        break;
    case 0x106: /* clts */
        if (s->cpl != 0) {
            gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
        } else {
T
ths 已提交
7805
            gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7806
            gen_helper_clts(cpu_env);
B
bellard 已提交
7807
            /* abort block because static cpu state changed */
B
bellard 已提交
7808
            gen_jmp_im(s->pc - s->cs_base);
B
bellard 已提交
7809
            gen_eob(s);
B
bellard 已提交
7810 7811
        }
        break;
B
balrog 已提交
7812
    /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
B
bellard 已提交
7813 7814
    case 0x1c3: /* MOVNTI reg, mem */
        if (!(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7815
            goto illegal_op;
7816
        ot = s->dflag == 2 ? MO_64 : MO_32;
7817
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7818 7819 7820 7821 7822
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
        reg = ((modrm >> 3) & 7) | rex_r;
        /* generate a generic store */
7823
        gen_ldst_modrm(env, s, modrm, ot, reg, 1);
B
bellard 已提交
7824
        break;
B
bellard 已提交
7825
    case 0x1ae:
7826
        modrm = cpu_ldub_code(env, s->pc++);
B
bellard 已提交
7827 7828 7829 7830
        mod = (modrm >> 6) & 3;
        op = (modrm >> 3) & 7;
        switch(op) {
        case 0: /* fxsave */
7831
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7832
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7833
                goto illegal_op;
7834
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7835 7836 7837
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7838
            gen_lea_modrm(env, s, modrm);
7839
            gen_update_cc_op(s);
B
bellard 已提交
7840
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7841
            gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
7842 7843
            break;
        case 1: /* fxrstor */
7844
            if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
7845
                (s->prefix & PREFIX_LOCK))
B
bellard 已提交
7846
                goto illegal_op;
7847
            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
B
bellard 已提交
7848 7849 7850
                gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                break;
            }
7851
            gen_lea_modrm(env, s, modrm);
7852
            gen_update_cc_op(s);
B
bellard 已提交
7853
            gen_jmp_im(pc_start - s->cs_base);
B
Blue Swirl 已提交
7854 7855
            gen_helper_fxrstor(cpu_env, cpu_A0,
                               tcg_const_i32((s->dflag == 2)));
B
bellard 已提交
7856 7857 7858 7859 7860 7861
            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 已提交
7862
            }
B
bellard 已提交
7863 7864
            if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
                mod == 3)
B
bellard 已提交
7865
                goto illegal_op;
7866
            gen_lea_modrm(env, s, modrm);
B
bellard 已提交
7867
            if (op == 2) {
7868 7869
                tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
                                    s->mem_index, MO_LEUL);
B
Blue Swirl 已提交
7870
                gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
B
bellard 已提交
7871
            } else {
B
bellard 已提交
7872
                tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
7873
                gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
B
bellard 已提交
7874
            }
B
bellard 已提交
7875 7876 7877
            break;
        case 5: /* lfence */
        case 6: /* mfence */
7878
            if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
B
bellard 已提交
7879 7880
                goto illegal_op;
            break;
7881 7882 7883
        case 7: /* sfence / clflush */
            if ((modrm & 0xc7) == 0xc0) {
                /* sfence */
A
aurel32 已提交
7884
                /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
7885 7886 7887 7888 7889 7890
                if (!(s->cpuid_features & CPUID_SSE))
                    goto illegal_op;
            } else {
                /* clflush */
                if (!(s->cpuid_features & CPUID_CLFLUSH))
                    goto illegal_op;
7891
                gen_lea_modrm(env, s, modrm);
7892 7893
            }
            break;
B
bellard 已提交
7894
        default:
B
bellard 已提交
7895 7896 7897
            goto illegal_op;
        }
        break;
A
aurel32 已提交
7898
    case 0x10d: /* 3DNow! prefetch(w) */
7899
        modrm = cpu_ldub_code(env, s->pc++);
A
aurel32 已提交
7900 7901 7902
        mod = (modrm >> 6) & 3;
        if (mod == 3)
            goto illegal_op;
7903
        gen_lea_modrm(env, s, modrm);
7904 7905
        /* ignore for now */
        break;
B
bellard 已提交
7906
    case 0x1aa: /* rsm */
B
bellard 已提交
7907
        gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
B
bellard 已提交
7908 7909
        if (!(s->flags & HF_SMM_MASK))
            goto illegal_op;
J
Jun Koi 已提交
7910
        gen_update_cc_op(s);
B
bellard 已提交
7911
        gen_jmp_im(s->pc - s->cs_base);
B
Blue Swirl 已提交
7912
        gen_helper_rsm(cpu_env);
B
bellard 已提交
7913 7914
        gen_eob(s);
        break;
B
balrog 已提交
7915 7916 7917 7918 7919 7920 7921
    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;

7922
        modrm = cpu_ldub_code(env, s->pc++);
M
malc 已提交
7923
        reg = ((modrm >> 3) & 7) | rex_r;
B
balrog 已提交
7924 7925

        if (s->prefix & PREFIX_DATA)
7926
            ot = MO_16;
B
balrog 已提交
7927
        else if (s->dflag != 2)
7928
            ot = MO_32;
B
balrog 已提交
7929
        else
7930
            ot = MO_64;
B
balrog 已提交
7931

7932
        gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
B
Blue Swirl 已提交
7933
        gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
B
balrog 已提交
7934
        gen_op_mov_reg_T0(ot, reg);
B
balrog 已提交
7935

7936
        set_cc_op(s, CC_OP_EFLAGS);
B
balrog 已提交
7937
        break;
A
aurel32 已提交
7938 7939 7940
    case 0x10e ... 0x10f:
        /* 3DNow! instructions, ignore prefixes */
        s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
B
bellard 已提交
7941 7942
    case 0x110 ... 0x117:
    case 0x128 ... 0x12f:
B
balrog 已提交
7943
    case 0x138 ... 0x13a:
7944
    case 0x150 ... 0x179:
B
bellard 已提交
7945 7946 7947 7948
    case 0x17c ... 0x17f:
    case 0x1c2:
    case 0x1c4 ... 0x1c6:
    case 0x1d0 ... 0x1fe:
7949
        gen_sse(env, s, b, pc_start, rex_r);
B
bellard 已提交
7950
        break;
B
bellard 已提交
7951 7952 7953 7954 7955
    default:
        goto illegal_op;
    }
    /* lock generation */
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7956
        gen_helper_unlock();
B
bellard 已提交
7957 7958
    return s->pc;
 illegal_op:
7959
    if (s->prefix & PREFIX_LOCK)
P
pbrook 已提交
7960
        gen_helper_unlock();
B
bellard 已提交
7961 7962 7963 7964 7965 7966 7967
    /* 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)
{
P
pbrook 已提交
7968 7969
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
    cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
7970 7971
                                       offsetof(CPUX86State, cc_op), "cc_op");
    cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
P
pbrook 已提交
7972
                                    "cc_dst");
7973 7974
    cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
                                    "cc_src");
7975 7976
    cpu_cc_src2 = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src2),
                                     "cc_src2");
7977

7978 7979
#ifdef TARGET_X86_64
    cpu_regs[R_EAX] = tcg_global_mem_new_i64(TCG_AREG0,
7980
                                             offsetof(CPUX86State, regs[R_EAX]), "rax");
7981
    cpu_regs[R_ECX] = tcg_global_mem_new_i64(TCG_AREG0,
7982
                                             offsetof(CPUX86State, regs[R_ECX]), "rcx");
7983
    cpu_regs[R_EDX] = tcg_global_mem_new_i64(TCG_AREG0,
7984
                                             offsetof(CPUX86State, regs[R_EDX]), "rdx");
7985
    cpu_regs[R_EBX] = tcg_global_mem_new_i64(TCG_AREG0,
7986
                                             offsetof(CPUX86State, regs[R_EBX]), "rbx");
7987
    cpu_regs[R_ESP] = tcg_global_mem_new_i64(TCG_AREG0,
7988
                                             offsetof(CPUX86State, regs[R_ESP]), "rsp");
7989
    cpu_regs[R_EBP] = tcg_global_mem_new_i64(TCG_AREG0,
7990
                                             offsetof(CPUX86State, regs[R_EBP]), "rbp");
7991
    cpu_regs[R_ESI] = tcg_global_mem_new_i64(TCG_AREG0,
7992
                                             offsetof(CPUX86State, regs[R_ESI]), "rsi");
7993
    cpu_regs[R_EDI] = tcg_global_mem_new_i64(TCG_AREG0,
7994
                                             offsetof(CPUX86State, regs[R_EDI]), "rdi");
7995
    cpu_regs[8] = tcg_global_mem_new_i64(TCG_AREG0,
7996
                                         offsetof(CPUX86State, regs[8]), "r8");
7997
    cpu_regs[9] = tcg_global_mem_new_i64(TCG_AREG0,
7998
                                          offsetof(CPUX86State, regs[9]), "r9");
7999
    cpu_regs[10] = tcg_global_mem_new_i64(TCG_AREG0,
8000
                                          offsetof(CPUX86State, regs[10]), "r10");
8001
    cpu_regs[11] = tcg_global_mem_new_i64(TCG_AREG0,
8002
                                          offsetof(CPUX86State, regs[11]), "r11");
8003
    cpu_regs[12] = tcg_global_mem_new_i64(TCG_AREG0,
8004
                                          offsetof(CPUX86State, regs[12]), "r12");
8005
    cpu_regs[13] = tcg_global_mem_new_i64(TCG_AREG0,
8006
                                          offsetof(CPUX86State, regs[13]), "r13");
8007
    cpu_regs[14] = tcg_global_mem_new_i64(TCG_AREG0,
8008
                                          offsetof(CPUX86State, regs[14]), "r14");
8009
    cpu_regs[15] = tcg_global_mem_new_i64(TCG_AREG0,
8010
                                          offsetof(CPUX86State, regs[15]), "r15");
8011 8012
#else
    cpu_regs[R_EAX] = tcg_global_mem_new_i32(TCG_AREG0,
8013
                                             offsetof(CPUX86State, regs[R_EAX]), "eax");
8014
    cpu_regs[R_ECX] = tcg_global_mem_new_i32(TCG_AREG0,
8015
                                             offsetof(CPUX86State, regs[R_ECX]), "ecx");
8016
    cpu_regs[R_EDX] = tcg_global_mem_new_i32(TCG_AREG0,
8017
                                             offsetof(CPUX86State, regs[R_EDX]), "edx");
8018
    cpu_regs[R_EBX] = tcg_global_mem_new_i32(TCG_AREG0,
8019
                                             offsetof(CPUX86State, regs[R_EBX]), "ebx");
8020
    cpu_regs[R_ESP] = tcg_global_mem_new_i32(TCG_AREG0,
8021
                                             offsetof(CPUX86State, regs[R_ESP]), "esp");
8022
    cpu_regs[R_EBP] = tcg_global_mem_new_i32(TCG_AREG0,
8023
                                             offsetof(CPUX86State, regs[R_EBP]), "ebp");
8024
    cpu_regs[R_ESI] = tcg_global_mem_new_i32(TCG_AREG0,
8025
                                             offsetof(CPUX86State, regs[R_ESI]), "esi");
8026
    cpu_regs[R_EDI] = tcg_global_mem_new_i32(TCG_AREG0,
8027
                                             offsetof(CPUX86State, regs[R_EDI]), "edi");
8028
#endif
B
bellard 已提交
8029 8030 8031 8032 8033
}

/* 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. */
8034
static inline void gen_intermediate_code_internal(X86CPU *cpu,
8035
                                                  TranslationBlock *tb,
8036
                                                  bool search_pc)
B
bellard 已提交
8037
{
8038
    CPUState *cs = CPU(cpu);
8039
    CPUX86State *env = &cpu->env;
B
bellard 已提交
8040
    DisasContext dc1, *dc = &dc1;
B
bellard 已提交
8041
    target_ulong pc_ptr;
B
bellard 已提交
8042
    uint16_t *gen_opc_end;
8043
    CPUBreakpoint *bp;
8044
    int j, lj;
8045
    uint64_t flags;
B
bellard 已提交
8046 8047
    target_ulong pc_start;
    target_ulong cs_base;
P
pbrook 已提交
8048 8049
    int num_insns;
    int max_insns;
8050

B
bellard 已提交
8051
    /* generate intermediate code */
B
bellard 已提交
8052 8053
    pc_start = tb->pc;
    cs_base = tb->cs_base;
B
bellard 已提交
8054
    flags = tb->flags;
B
bellard 已提交
8055

8056
    dc->pe = (flags >> HF_PE_SHIFT) & 1;
B
bellard 已提交
8057 8058 8059 8060 8061 8062 8063 8064
    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;
8065
    dc->singlestep_enabled = cs->singlestep_enabled;
B
bellard 已提交
8066
    dc->cc_op = CC_OP_DYNAMIC;
8067
    dc->cc_op_dirty = false;
B
bellard 已提交
8068 8069 8070 8071 8072 8073
    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) {
8074
        dc->mem_index = cpu_mmu_index(env);
B
bellard 已提交
8075
    }
8076 8077 8078 8079 8080
    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 已提交
8081 8082 8083 8084
#ifdef TARGET_X86_64
    dc->lma = (flags >> HF_LMA_SHIFT) & 1;
    dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
#endif
B
bellard 已提交
8085
    dc->flags = flags;
8086
    dc->jmp_opt = !(dc->tf || cs->singlestep_enabled ||
8087
                    (flags & HF_INHIBIT_IRQ_MASK)
B
bellard 已提交
8088
#ifndef CONFIG_SOFTMMU
B
bellard 已提交
8089 8090 8091
                    || (flags & HF_SOFTMMU_MASK)
#endif
                    );
8092 8093
#if 0
    /* check addseg logic */
B
bellard 已提交
8094
    if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
8095 8096 8097
        printf("ERROR addseg\n");
#endif

P
pbrook 已提交
8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108
    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();
8109
    cpu_cc_srcT = tcg_temp_local_new();
B
bellard 已提交
8110

8111
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
B
bellard 已提交
8112 8113 8114 8115

    dc->is_jmp = DISAS_NEXT;
    pc_ptr = pc_start;
    lj = -1;
P
pbrook 已提交
8116 8117 8118 8119
    num_insns = 0;
    max_insns = tb->cflags & CF_COUNT_MASK;
    if (max_insns == 0)
        max_insns = CF_COUNT_MASK;
B
bellard 已提交
8120

8121
    gen_tb_start();
B
bellard 已提交
8122
    for(;;) {
B
Blue Swirl 已提交
8123 8124
        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
J
Jan Kiszka 已提交
8125 8126
                if (bp->pc == pc_ptr &&
                    !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
B
bellard 已提交
8127 8128 8129 8130 8131 8132
                    gen_debug(dc, pc_ptr - dc->cs_base);
                    break;
                }
            }
        }
        if (search_pc) {
8133
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8134 8135 8136
            if (lj < j) {
                lj++;
                while (lj < j)
8137
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8138
            }
8139
            tcg_ctx.gen_opc_pc[lj] = pc_ptr;
B
bellard 已提交
8140
            gen_opc_cc_op[lj] = dc->cc_op;
8141
            tcg_ctx.gen_opc_instr_start[lj] = 1;
8142
            tcg_ctx.gen_opc_icount[lj] = num_insns;
B
bellard 已提交
8143
        }
P
pbrook 已提交
8144 8145 8146
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
            gen_io_start();

8147
        pc_ptr = disas_insn(env, dc, pc_ptr);
P
pbrook 已提交
8148
        num_insns++;
B
bellard 已提交
8149 8150 8151 8152 8153
        /* stop translation if indicated */
        if (dc->is_jmp)
            break;
        /* if single step mode, we generate only one instruction and
           generate an exception */
8154 8155 8156
        /* 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 */
8157
        if (dc->tf || dc->singlestep_enabled ||
P
pbrook 已提交
8158
            (flags & HF_INHIBIT_IRQ_MASK)) {
B
bellard 已提交
8159
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8160 8161 8162 8163
            gen_eob(dc);
            break;
        }
        /* if too long translation, stop generation too */
8164
        if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
P
pbrook 已提交
8165 8166
            (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
            num_insns >= max_insns) {
B
bellard 已提交
8167
            gen_jmp_im(pc_ptr - dc->cs_base);
B
bellard 已提交
8168 8169 8170
            gen_eob(dc);
            break;
        }
8171 8172 8173 8174 8175
        if (singlestep) {
            gen_jmp_im(pc_ptr - dc->cs_base);
            gen_eob(dc);
            break;
        }
B
bellard 已提交
8176
    }
P
pbrook 已提交
8177 8178
    if (tb->cflags & CF_LAST_IO)
        gen_io_end();
8179
    gen_tb_end(tb, num_insns);
8180
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
B
bellard 已提交
8181 8182
    /* we don't forget to fill the last values */
    if (search_pc) {
8183
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
B
bellard 已提交
8184 8185
        lj++;
        while (lj <= j)
8186
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
8187
    }
8188

B
bellard 已提交
8189
#ifdef DEBUG_DISAS
8190
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
B
bellard 已提交
8191
        int disas_flags;
8192 8193
        qemu_log("----------------\n");
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
B
bellard 已提交
8194 8195 8196 8197 8198 8199
#ifdef TARGET_X86_64
        if (dc->code64)
            disas_flags = 2;
        else
#endif
            disas_flags = !dc->code32;
B
Blue Swirl 已提交
8200
        log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
8201
        qemu_log("\n");
B
bellard 已提交
8202 8203 8204
    }
#endif

P
pbrook 已提交
8205
    if (!search_pc) {
B
bellard 已提交
8206
        tb->size = pc_ptr - pc_start;
P
pbrook 已提交
8207 8208
        tb->icount = num_insns;
    }
B
bellard 已提交
8209 8210
}

8211
void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8212
{
8213
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, false);
B
bellard 已提交
8214 8215
}

8216
void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
B
bellard 已提交
8217
{
8218
    gen_intermediate_code_internal(x86_env_get_cpu(env), tb, true);
B
bellard 已提交
8219 8220
}

8221
void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
A
aurel32 已提交
8222 8223 8224
{
    int cc_op;
#ifdef DEBUG_DISAS
8225
    if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
A
aurel32 已提交
8226
        int i;
8227
        qemu_log("RESTORE:\n");
A
aurel32 已提交
8228
        for(i = 0;i <= pc_pos; i++) {
8229
            if (tcg_ctx.gen_opc_instr_start[i]) {
8230 8231
                qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
                        tcg_ctx.gen_opc_pc[i]);
A
aurel32 已提交
8232 8233
            }
        }
8234
        qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
8235
                pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
A
aurel32 已提交
8236 8237 8238
                (uint32_t)tb->cs_base);
    }
#endif
8239
    env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
A
aurel32 已提交
8240 8241 8242 8243
    cc_op = gen_opc_cc_op[pc_pos];
    if (cc_op != CC_OP_DYNAMIC)
        env->cc_op = cc_op;
}