4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * 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/>.
26 #include "qemu/host-utils.h"
28 #include "disas/disas.h"
30 #include "exec/cpu_ldst.h"
32 #include "exec/helper-proto.h"
33 #include "exec/helper-gen.h"
35 #define PREFIX_REPZ 0x01
36 #define PREFIX_REPNZ 0x02
37 #define PREFIX_LOCK 0x04
38 #define PREFIX_DATA 0x08
39 #define PREFIX_ADR 0x10
40 #define PREFIX_VEX 0x20
43 #define CODE64(s) ((s)->code64)
44 #define REX_X(s) ((s)->rex_x)
45 #define REX_B(s) ((s)->rex_b)
60 //#define MACRO_TEST 1
62 /* global register indexes */
63 static TCGv_ptr cpu_env
;
65 static TCGv cpu_cc_dst
, cpu_cc_src
, cpu_cc_src2
, cpu_cc_srcT
;
66 static TCGv_i32 cpu_cc_op
;
67 static TCGv cpu_regs
[CPU_NB_REGS
];
70 /* local register indexes (only used inside old micro ops) */
71 static TCGv cpu_tmp0
, cpu_tmp4
;
72 static TCGv_ptr cpu_ptr0
, cpu_ptr1
;
73 static TCGv_i32 cpu_tmp2_i32
, cpu_tmp3_i32
;
74 static TCGv_i64 cpu_tmp1_i64
;
76 static uint8_t gen_opc_cc_op
[OPC_BUF_SIZE
];
78 #include "exec/gen-icount.h"
81 static int x86_64_hregs
;
84 typedef struct DisasContext
{
85 /* current insn context */
86 int override
; /* -1 if no override */
90 target_ulong pc
; /* pc = eip + cs_base */
91 int is_jmp
; /* 1 = means jump (stop translation), 2 means CPU
92 static state change (stop translation) */
93 /* current block context */
94 target_ulong cs_base
; /* base of CS segment */
95 int pe
; /* protected mode */
96 int code32
; /* 32 bit code segment */
98 int lma
; /* long mode active */
99 int code64
; /* 64 bit code segment */
102 int vex_l
; /* vex vector length */
103 int vex_v
; /* vex vvvv register, without 1's compliment. */
104 int ss32
; /* 32 bit stack segment */
105 CCOp cc_op
; /* current CC operation */
107 int addseg
; /* non zero if either DS/ES/SS have a non zero base */
108 int f_st
; /* currently unused */
109 int vm86
; /* vm86 mode */
112 int tf
; /* TF cpu flag */
113 int singlestep_enabled
; /* "hardware" single step enabled */
114 int jmp_opt
; /* use direct block chaining for direct jumps */
115 int mem_index
; /* select memory access functions */
116 uint64_t flags
; /* all execution flags */
117 struct TranslationBlock
*tb
;
118 int popl_esp_hack
; /* for correct popl with esp base handling */
119 int rip_offset
; /* only used in x86_64, but left for simplicity */
121 int cpuid_ext_features
;
122 int cpuid_ext2_features
;
123 int cpuid_ext3_features
;
124 int cpuid_7_0_ebx_features
;
127 static void gen_eob(DisasContext
*s
);
128 static void gen_jmp(DisasContext
*s
, target_ulong eip
);
129 static void gen_jmp_tb(DisasContext
*s
, target_ulong eip
, int tb_num
);
130 static void gen_op(DisasContext
*s1
, int op
, TCGMemOp ot
, int d
);
132 /* i386 arith/logic operations */
152 OP_SHL1
, /* undocumented */
168 /* I386 int registers */
169 OR_EAX
, /* MUST be even numbered */
178 OR_TMP0
= 16, /* temporary operand register */
180 OR_A0
, /* temporary register used when doing address evaluation */
190 /* Bit set if the global variable is live after setting CC_OP to X. */
191 static const uint8_t cc_op_live
[CC_OP_NB
] = {
192 [CC_OP_DYNAMIC
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
193 [CC_OP_EFLAGS
] = USES_CC_SRC
,
194 [CC_OP_MULB
... CC_OP_MULQ
] = USES_CC_DST
| USES_CC_SRC
,
195 [CC_OP_ADDB
... CC_OP_ADDQ
] = USES_CC_DST
| USES_CC_SRC
,
196 [CC_OP_ADCB
... CC_OP_ADCQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
197 [CC_OP_SUBB
... CC_OP_SUBQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRCT
,
198 [CC_OP_SBBB
... CC_OP_SBBQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
199 [CC_OP_LOGICB
... CC_OP_LOGICQ
] = USES_CC_DST
,
200 [CC_OP_INCB
... CC_OP_INCQ
] = USES_CC_DST
| USES_CC_SRC
,
201 [CC_OP_DECB
... CC_OP_DECQ
] = USES_CC_DST
| USES_CC_SRC
,
202 [CC_OP_SHLB
... CC_OP_SHLQ
] = USES_CC_DST
| USES_CC_SRC
,
203 [CC_OP_SARB
... CC_OP_SARQ
] = USES_CC_DST
| USES_CC_SRC
,
204 [CC_OP_BMILGB
... CC_OP_BMILGQ
] = USES_CC_DST
| USES_CC_SRC
,
205 [CC_OP_ADCX
] = USES_CC_DST
| USES_CC_SRC
,
206 [CC_OP_ADOX
] = USES_CC_SRC
| USES_CC_SRC2
,
207 [CC_OP_ADCOX
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
211 static void set_cc_op(DisasContext
*s
, CCOp op
)
215 if (s
->cc_op
== op
) {
219 /* Discard CC computation that will no longer be used. */
220 dead
= cc_op_live
[s
->cc_op
] & ~cc_op_live
[op
];
221 if (dead
& USES_CC_DST
) {
222 tcg_gen_discard_tl(cpu_cc_dst
);
224 if (dead
& USES_CC_SRC
) {
225 tcg_gen_discard_tl(cpu_cc_src
);
227 if (dead
& USES_CC_SRC2
) {
228 tcg_gen_discard_tl(cpu_cc_src2
);
230 if (dead
& USES_CC_SRCT
) {
231 tcg_gen_discard_tl(cpu_cc_srcT
);
234 if (op
== CC_OP_DYNAMIC
) {
235 /* The DYNAMIC setting is translator only, and should never be
236 stored. Thus we always consider it clean. */
237 s
->cc_op_dirty
= false;
239 /* Discard any computed CC_OP value (see shifts). */
240 if (s
->cc_op
== CC_OP_DYNAMIC
) {
241 tcg_gen_discard_i32(cpu_cc_op
);
243 s
->cc_op_dirty
= true;
248 static void gen_update_cc_op(DisasContext
*s
)
250 if (s
->cc_op_dirty
) {
251 tcg_gen_movi_i32(cpu_cc_op
, s
->cc_op
);
252 s
->cc_op_dirty
= false;
258 #define NB_OP_SIZES 4
260 #else /* !TARGET_X86_64 */
262 #define NB_OP_SIZES 3
264 #endif /* !TARGET_X86_64 */
266 #if defined(HOST_WORDS_BIGENDIAN)
267 #define REG_B_OFFSET (sizeof(target_ulong) - 1)
268 #define REG_H_OFFSET (sizeof(target_ulong) - 2)
269 #define REG_W_OFFSET (sizeof(target_ulong) - 2)
270 #define REG_L_OFFSET (sizeof(target_ulong) - 4)
271 #define REG_LH_OFFSET (sizeof(target_ulong) - 8)
273 #define REG_B_OFFSET 0
274 #define REG_H_OFFSET 1
275 #define REG_W_OFFSET 0
276 #define REG_L_OFFSET 0
277 #define REG_LH_OFFSET 4
280 /* In instruction encodings for byte register accesses the
281 * register number usually indicates "low 8 bits of register N";
282 * however there are some special cases where N 4..7 indicates
283 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
284 * true for this special case, false otherwise.
286 static inline bool byte_reg_is_xH(int reg
)
292 if (reg
>= 8 || x86_64_hregs
) {
299 /* Select the size of a push/pop operation. */
300 static inline TCGMemOp
mo_pushpop(DisasContext
*s
, TCGMemOp ot
)
303 return ot
== MO_16
? MO_16
: MO_64
;
309 /* Select only size 64 else 32. Used for SSE operand sizes. */
310 static inline TCGMemOp
mo_64_32(TCGMemOp ot
)
313 return ot
== MO_64
? MO_64
: MO_32
;
319 /* Select size 8 if lsb of B is clear, else OT. Used for decoding
320 byte vs word opcodes. */
321 static inline TCGMemOp
mo_b_d(int b
, TCGMemOp ot
)
323 return b
& 1 ? ot
: MO_8
;
326 /* Select size 8 if lsb of B is clear, else OT capped at 32.
327 Used for decoding operand size of port opcodes. */
328 static inline TCGMemOp
mo_b_d32(int b
, TCGMemOp ot
)
330 return b
& 1 ? (ot
== MO_16
? MO_16
: MO_32
) : MO_8
;
333 static void gen_op_mov_reg_v(TCGMemOp ot
, int reg
, TCGv t0
)
337 if (!byte_reg_is_xH(reg
)) {
338 tcg_gen_deposit_tl(cpu_regs
[reg
], cpu_regs
[reg
], t0
, 0, 8);
340 tcg_gen_deposit_tl(cpu_regs
[reg
- 4], cpu_regs
[reg
- 4], t0
, 8, 8);
344 tcg_gen_deposit_tl(cpu_regs
[reg
], cpu_regs
[reg
], t0
, 0, 16);
347 /* For x86_64, this sets the higher half of register to zero.
348 For i386, this is equivalent to a mov. */
349 tcg_gen_ext32u_tl(cpu_regs
[reg
], t0
);
353 tcg_gen_mov_tl(cpu_regs
[reg
], t0
);
361 static inline void gen_op_mov_v_reg(TCGMemOp ot
, TCGv t0
, int reg
)
363 if (ot
== MO_8
&& byte_reg_is_xH(reg
)) {
364 tcg_gen_shri_tl(t0
, cpu_regs
[reg
- 4], 8);
365 tcg_gen_ext8u_tl(t0
, t0
);
367 tcg_gen_mov_tl(t0
, cpu_regs
[reg
]);
371 static inline void gen_op_movl_A0_reg(int reg
)
373 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[reg
]);
376 static inline void gen_op_addl_A0_im(int32_t val
)
378 tcg_gen_addi_tl(cpu_A0
, cpu_A0
, val
);
380 tcg_gen_andi_tl(cpu_A0
, cpu_A0
, 0xffffffff);
385 static inline void gen_op_addq_A0_im(int64_t val
)
387 tcg_gen_addi_tl(cpu_A0
, cpu_A0
, val
);
391 static void gen_add_A0_im(DisasContext
*s
, int val
)
395 gen_op_addq_A0_im(val
);
398 gen_op_addl_A0_im(val
);
401 static inline void gen_op_jmp_v(TCGv dest
)
403 tcg_gen_st_tl(dest
, cpu_env
, offsetof(CPUX86State
, eip
));
406 static inline void gen_op_add_reg_im(TCGMemOp size
, int reg
, int32_t val
)
408 tcg_gen_addi_tl(cpu_tmp0
, cpu_regs
[reg
], val
);
409 gen_op_mov_reg_v(size
, reg
, cpu_tmp0
);
412 static inline void gen_op_add_reg_T0(TCGMemOp size
, int reg
)
414 tcg_gen_add_tl(cpu_tmp0
, cpu_regs
[reg
], cpu_T
[0]);
415 gen_op_mov_reg_v(size
, reg
, cpu_tmp0
);
418 static inline void gen_op_addl_A0_reg_sN(int shift
, int reg
)
420 tcg_gen_mov_tl(cpu_tmp0
, cpu_regs
[reg
]);
422 tcg_gen_shli_tl(cpu_tmp0
, cpu_tmp0
, shift
);
423 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
424 /* For x86_64, this sets the higher half of register to zero.
425 For i386, this is equivalent to a nop. */
426 tcg_gen_ext32u_tl(cpu_A0
, cpu_A0
);
429 static inline void gen_op_movl_A0_seg(int reg
)
431 tcg_gen_ld32u_tl(cpu_A0
, cpu_env
, offsetof(CPUX86State
, segs
[reg
].base
) + REG_L_OFFSET
);
434 static inline void gen_op_addl_A0_seg(DisasContext
*s
, int reg
)
436 tcg_gen_ld_tl(cpu_tmp0
, cpu_env
, offsetof(CPUX86State
, segs
[reg
].base
));
439 tcg_gen_andi_tl(cpu_A0
, cpu_A0
, 0xffffffff);
440 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
442 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
443 tcg_gen_andi_tl(cpu_A0
, cpu_A0
, 0xffffffff);
446 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
451 static inline void gen_op_movq_A0_seg(int reg
)
453 tcg_gen_ld_tl(cpu_A0
, cpu_env
, offsetof(CPUX86State
, segs
[reg
].base
));
456 static inline void gen_op_addq_A0_seg(int reg
)
458 tcg_gen_ld_tl(cpu_tmp0
, cpu_env
, offsetof(CPUX86State
, segs
[reg
].base
));
459 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
462 static inline void gen_op_movq_A0_reg(int reg
)
464 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[reg
]);
467 static inline void gen_op_addq_A0_reg_sN(int shift
, int reg
)
469 tcg_gen_mov_tl(cpu_tmp0
, cpu_regs
[reg
]);
471 tcg_gen_shli_tl(cpu_tmp0
, cpu_tmp0
, shift
);
472 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
476 static inline void gen_op_ld_v(DisasContext
*s
, int idx
, TCGv t0
, TCGv a0
)
478 tcg_gen_qemu_ld_tl(t0
, a0
, s
->mem_index
, idx
| MO_LE
);
481 static inline void gen_op_st_v(DisasContext
*s
, int idx
, TCGv t0
, TCGv a0
)
483 tcg_gen_qemu_st_tl(t0
, a0
, s
->mem_index
, idx
| MO_LE
);
486 static inline void gen_op_st_rm_T0_A0(DisasContext
*s
, int idx
, int d
)
489 gen_op_st_v(s
, idx
, cpu_T
[0], cpu_A0
);
491 gen_op_mov_reg_v(idx
, d
, cpu_T
[0]);
495 static inline void gen_jmp_im(target_ulong pc
)
497 tcg_gen_movi_tl(cpu_tmp0
, pc
);
498 gen_op_jmp_v(cpu_tmp0
);
501 static inline void gen_string_movl_A0_ESI(DisasContext
*s
)
505 override
= s
->override
;
510 gen_op_movq_A0_seg(override
);
511 gen_op_addq_A0_reg_sN(0, R_ESI
);
513 gen_op_movq_A0_reg(R_ESI
);
519 if (s
->addseg
&& override
< 0)
522 gen_op_movl_A0_seg(override
);
523 gen_op_addl_A0_reg_sN(0, R_ESI
);
525 gen_op_movl_A0_reg(R_ESI
);
529 /* 16 address, always override */
532 tcg_gen_ext16u_tl(cpu_A0
, cpu_regs
[R_ESI
]);
533 gen_op_addl_A0_seg(s
, override
);
540 static inline void gen_string_movl_A0_EDI(DisasContext
*s
)
545 gen_op_movq_A0_reg(R_EDI
);
550 gen_op_movl_A0_seg(R_ES
);
551 gen_op_addl_A0_reg_sN(0, R_EDI
);
553 gen_op_movl_A0_reg(R_EDI
);
557 tcg_gen_ext16u_tl(cpu_A0
, cpu_regs
[R_EDI
]);
558 gen_op_addl_A0_seg(s
, R_ES
);
565 static inline void gen_op_movl_T0_Dshift(TCGMemOp ot
)
567 tcg_gen_ld32s_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, df
));
568 tcg_gen_shli_tl(cpu_T
[0], cpu_T
[0], ot
);
571 static TCGv
gen_ext_tl(TCGv dst
, TCGv src
, TCGMemOp size
, bool sign
)
576 tcg_gen_ext8s_tl(dst
, src
);
578 tcg_gen_ext8u_tl(dst
, src
);
583 tcg_gen_ext16s_tl(dst
, src
);
585 tcg_gen_ext16u_tl(dst
, src
);
591 tcg_gen_ext32s_tl(dst
, src
);
593 tcg_gen_ext32u_tl(dst
, src
);
602 static void gen_extu(TCGMemOp ot
, TCGv reg
)
604 gen_ext_tl(reg
, reg
, ot
, false);
607 static void gen_exts(TCGMemOp ot
, TCGv reg
)
609 gen_ext_tl(reg
, reg
, ot
, true);
612 static inline void gen_op_jnz_ecx(TCGMemOp size
, int label1
)
614 tcg_gen_mov_tl(cpu_tmp0
, cpu_regs
[R_ECX
]);
615 gen_extu(size
, cpu_tmp0
);
616 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_tmp0
, 0, label1
);
619 static inline void gen_op_jz_ecx(TCGMemOp size
, int label1
)
621 tcg_gen_mov_tl(cpu_tmp0
, cpu_regs
[R_ECX
]);
622 gen_extu(size
, cpu_tmp0
);
623 tcg_gen_brcondi_tl(TCG_COND_EQ
, cpu_tmp0
, 0, label1
);
626 static void gen_helper_in_func(TCGMemOp ot
, TCGv v
, TCGv_i32 n
)
630 gen_helper_inb(v
, n
);
633 gen_helper_inw(v
, n
);
636 gen_helper_inl(v
, n
);
643 static void gen_helper_out_func(TCGMemOp ot
, TCGv_i32 v
, TCGv_i32 n
)
647 gen_helper_outb(v
, n
);
650 gen_helper_outw(v
, n
);
653 gen_helper_outl(v
, n
);
660 static void gen_check_io(DisasContext
*s
, TCGMemOp ot
, target_ulong cur_eip
,
664 target_ulong next_eip
;
667 if (s
->pe
&& (s
->cpl
> s
->iopl
|| s
->vm86
)) {
671 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
674 gen_helper_check_iob(cpu_env
, cpu_tmp2_i32
);
677 gen_helper_check_iow(cpu_env
, cpu_tmp2_i32
);
680 gen_helper_check_iol(cpu_env
, cpu_tmp2_i32
);
686 if(s
->flags
& HF_SVMI_MASK
) {
691 svm_flags
|= (1 << (4 + ot
));
692 next_eip
= s
->pc
- s
->cs_base
;
693 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
694 gen_helper_svm_check_io(cpu_env
, cpu_tmp2_i32
,
695 tcg_const_i32(svm_flags
),
696 tcg_const_i32(next_eip
- cur_eip
));
700 static inline void gen_movs(DisasContext
*s
, TCGMemOp ot
)
702 gen_string_movl_A0_ESI(s
);
703 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
704 gen_string_movl_A0_EDI(s
);
705 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
706 gen_op_movl_T0_Dshift(ot
);
707 gen_op_add_reg_T0(s
->aflag
, R_ESI
);
708 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
711 static void gen_op_update1_cc(void)
713 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
716 static void gen_op_update2_cc(void)
718 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[1]);
719 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
722 static void gen_op_update3_cc(TCGv reg
)
724 tcg_gen_mov_tl(cpu_cc_src2
, reg
);
725 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[1]);
726 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
729 static inline void gen_op_testl_T0_T1_cc(void)
731 tcg_gen_and_tl(cpu_cc_dst
, cpu_T
[0], cpu_T
[1]);
734 static void gen_op_update_neg_cc(void)
736 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
737 tcg_gen_neg_tl(cpu_cc_src
, cpu_T
[0]);
738 tcg_gen_movi_tl(cpu_cc_srcT
, 0);
741 /* compute all eflags to cc_src */
742 static void gen_compute_eflags(DisasContext
*s
)
744 TCGv zero
, dst
, src1
, src2
;
747 if (s
->cc_op
== CC_OP_EFLAGS
) {
750 if (s
->cc_op
== CC_OP_CLR
) {
751 tcg_gen_movi_tl(cpu_cc_src
, CC_Z
| CC_P
);
752 set_cc_op(s
, CC_OP_EFLAGS
);
761 /* Take care to not read values that are not live. */
762 live
= cc_op_live
[s
->cc_op
] & ~USES_CC_SRCT
;
763 dead
= live
^ (USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
);
765 zero
= tcg_const_tl(0);
766 if (dead
& USES_CC_DST
) {
769 if (dead
& USES_CC_SRC
) {
772 if (dead
& USES_CC_SRC2
) {
778 gen_helper_cc_compute_all(cpu_cc_src
, dst
, src1
, src2
, cpu_cc_op
);
779 set_cc_op(s
, CC_OP_EFLAGS
);
786 typedef struct CCPrepare
{
796 /* compute eflags.C to reg */
797 static CCPrepare
gen_prepare_eflags_c(DisasContext
*s
, TCGv reg
)
803 case CC_OP_SUBB
... CC_OP_SUBQ
:
804 /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
805 size
= s
->cc_op
- CC_OP_SUBB
;
806 t1
= gen_ext_tl(cpu_tmp0
, cpu_cc_src
, size
, false);
807 /* If no temporary was used, be careful not to alias t1 and t0. */
808 t0
= TCGV_EQUAL(t1
, cpu_cc_src
) ? cpu_tmp0
: reg
;
809 tcg_gen_mov_tl(t0
, cpu_cc_srcT
);
813 case CC_OP_ADDB
... CC_OP_ADDQ
:
814 /* (DATA_TYPE)CC_DST < (DATA_TYPE)CC_SRC */
815 size
= s
->cc_op
- CC_OP_ADDB
;
816 t1
= gen_ext_tl(cpu_tmp0
, cpu_cc_src
, size
, false);
817 t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, false);
819 return (CCPrepare
) { .cond
= TCG_COND_LTU
, .reg
= t0
,
820 .reg2
= t1
, .mask
= -1, .use_reg2
= true };
822 case CC_OP_LOGICB
... CC_OP_LOGICQ
:
824 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
826 case CC_OP_INCB
... CC_OP_INCQ
:
827 case CC_OP_DECB
... CC_OP_DECQ
:
828 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
829 .mask
= -1, .no_setcond
= true };
831 case CC_OP_SHLB
... CC_OP_SHLQ
:
832 /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
833 size
= s
->cc_op
- CC_OP_SHLB
;
834 shift
= (8 << size
) - 1;
835 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
836 .mask
= (target_ulong
)1 << shift
};
838 case CC_OP_MULB
... CC_OP_MULQ
:
839 return (CCPrepare
) { .cond
= TCG_COND_NE
,
840 .reg
= cpu_cc_src
, .mask
= -1 };
842 case CC_OP_BMILGB
... CC_OP_BMILGQ
:
843 size
= s
->cc_op
- CC_OP_BMILGB
;
844 t0
= gen_ext_tl(reg
, cpu_cc_src
, size
, false);
845 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= t0
, .mask
= -1 };
849 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_dst
,
850 .mask
= -1, .no_setcond
= true };
853 case CC_OP_SARB
... CC_OP_SARQ
:
855 return (CCPrepare
) { .cond
= TCG_COND_NE
,
856 .reg
= cpu_cc_src
, .mask
= CC_C
};
859 /* The need to compute only C from CC_OP_DYNAMIC is important
860 in efficiently implementing e.g. INC at the start of a TB. */
862 gen_helper_cc_compute_c(reg
, cpu_cc_dst
, cpu_cc_src
,
863 cpu_cc_src2
, cpu_cc_op
);
864 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
865 .mask
= -1, .no_setcond
= true };
869 /* compute eflags.P to reg */
870 static CCPrepare
gen_prepare_eflags_p(DisasContext
*s
, TCGv reg
)
872 gen_compute_eflags(s
);
873 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
877 /* compute eflags.S to reg */
878 static CCPrepare
gen_prepare_eflags_s(DisasContext
*s
, TCGv reg
)
882 gen_compute_eflags(s
);
888 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
891 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
894 TCGMemOp size
= (s
->cc_op
- CC_OP_ADDB
) & 3;
895 TCGv t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, true);
896 return (CCPrepare
) { .cond
= TCG_COND_LT
, .reg
= t0
, .mask
= -1 };
901 /* compute eflags.O to reg */
902 static CCPrepare
gen_prepare_eflags_o(DisasContext
*s
, TCGv reg
)
907 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src2
,
908 .mask
= -1, .no_setcond
= true };
910 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
912 gen_compute_eflags(s
);
913 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
918 /* compute eflags.Z to reg */
919 static CCPrepare
gen_prepare_eflags_z(DisasContext
*s
, TCGv reg
)
923 gen_compute_eflags(s
);
929 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
932 return (CCPrepare
) { .cond
= TCG_COND_ALWAYS
, .mask
= -1 };
935 TCGMemOp size
= (s
->cc_op
- CC_OP_ADDB
) & 3;
936 TCGv t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, false);
937 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= t0
, .mask
= -1 };
942 /* perform a conditional store into register 'reg' according to jump opcode
943 value 'b'. In the fast case, T0 is guaranted not to be used. */
944 static CCPrepare
gen_prepare_cc(DisasContext
*s
, int b
, TCGv reg
)
946 int inv
, jcc_op
, cond
;
952 jcc_op
= (b
>> 1) & 7;
955 case CC_OP_SUBB
... CC_OP_SUBQ
:
956 /* We optimize relational operators for the cmp/jcc case. */
957 size
= s
->cc_op
- CC_OP_SUBB
;
960 tcg_gen_mov_tl(cpu_tmp4
, cpu_cc_srcT
);
961 gen_extu(size
, cpu_tmp4
);
962 t0
= gen_ext_tl(cpu_tmp0
, cpu_cc_src
, size
, false);
963 cc
= (CCPrepare
) { .cond
= TCG_COND_LEU
, .reg
= cpu_tmp4
,
964 .reg2
= t0
, .mask
= -1, .use_reg2
= true };
973 tcg_gen_mov_tl(cpu_tmp4
, cpu_cc_srcT
);
974 gen_exts(size
, cpu_tmp4
);
975 t0
= gen_ext_tl(cpu_tmp0
, cpu_cc_src
, size
, true);
976 cc
= (CCPrepare
) { .cond
= cond
, .reg
= cpu_tmp4
,
977 .reg2
= t0
, .mask
= -1, .use_reg2
= true };
987 /* This actually generates good code for JC, JZ and JS. */
990 cc
= gen_prepare_eflags_o(s
, reg
);
993 cc
= gen_prepare_eflags_c(s
, reg
);
996 cc
= gen_prepare_eflags_z(s
, reg
);
999 gen_compute_eflags(s
);
1000 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
1001 .mask
= CC_Z
| CC_C
};
1004 cc
= gen_prepare_eflags_s(s
, reg
);
1007 cc
= gen_prepare_eflags_p(s
, reg
);
1010 gen_compute_eflags(s
);
1011 if (TCGV_EQUAL(reg
, cpu_cc_src
)) {
1014 tcg_gen_shri_tl(reg
, cpu_cc_src
, 4); /* CC_O -> CC_S */
1015 tcg_gen_xor_tl(reg
, reg
, cpu_cc_src
);
1016 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
1021 gen_compute_eflags(s
);
1022 if (TCGV_EQUAL(reg
, cpu_cc_src
)) {
1025 tcg_gen_shri_tl(reg
, cpu_cc_src
, 4); /* CC_O -> CC_S */
1026 tcg_gen_xor_tl(reg
, reg
, cpu_cc_src
);
1027 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
1028 .mask
= CC_S
| CC_Z
};
1035 cc
.cond
= tcg_invert_cond(cc
.cond
);
1040 static void gen_setcc1(DisasContext
*s
, int b
, TCGv reg
)
1042 CCPrepare cc
= gen_prepare_cc(s
, b
, reg
);
1044 if (cc
.no_setcond
) {
1045 if (cc
.cond
== TCG_COND_EQ
) {
1046 tcg_gen_xori_tl(reg
, cc
.reg
, 1);
1048 tcg_gen_mov_tl(reg
, cc
.reg
);
1053 if (cc
.cond
== TCG_COND_NE
&& !cc
.use_reg2
&& cc
.imm
== 0 &&
1054 cc
.mask
!= 0 && (cc
.mask
& (cc
.mask
- 1)) == 0) {
1055 tcg_gen_shri_tl(reg
, cc
.reg
, ctztl(cc
.mask
));
1056 tcg_gen_andi_tl(reg
, reg
, 1);
1059 if (cc
.mask
!= -1) {
1060 tcg_gen_andi_tl(reg
, cc
.reg
, cc
.mask
);
1064 tcg_gen_setcond_tl(cc
.cond
, reg
, cc
.reg
, cc
.reg2
);
1066 tcg_gen_setcondi_tl(cc
.cond
, reg
, cc
.reg
, cc
.imm
);
1070 static inline void gen_compute_eflags_c(DisasContext
*s
, TCGv reg
)
1072 gen_setcc1(s
, JCC_B
<< 1, reg
);
1075 /* generate a conditional jump to label 'l1' according to jump opcode
1076 value 'b'. In the fast case, T0 is guaranted not to be used. */
1077 static inline void gen_jcc1_noeob(DisasContext
*s
, int b
, int l1
)
1079 CCPrepare cc
= gen_prepare_cc(s
, b
, cpu_T
[0]);
1081 if (cc
.mask
!= -1) {
1082 tcg_gen_andi_tl(cpu_T
[0], cc
.reg
, cc
.mask
);
1086 tcg_gen_brcond_tl(cc
.cond
, cc
.reg
, cc
.reg2
, l1
);
1088 tcg_gen_brcondi_tl(cc
.cond
, cc
.reg
, cc
.imm
, l1
);
1092 /* Generate a conditional jump to label 'l1' according to jump opcode
1093 value 'b'. In the fast case, T0 is guaranted not to be used.
1094 A translation block must end soon. */
1095 static inline void gen_jcc1(DisasContext
*s
, int b
, int l1
)
1097 CCPrepare cc
= gen_prepare_cc(s
, b
, cpu_T
[0]);
1099 gen_update_cc_op(s
);
1100 if (cc
.mask
!= -1) {
1101 tcg_gen_andi_tl(cpu_T
[0], cc
.reg
, cc
.mask
);
1104 set_cc_op(s
, CC_OP_DYNAMIC
);
1106 tcg_gen_brcond_tl(cc
.cond
, cc
.reg
, cc
.reg2
, l1
);
1108 tcg_gen_brcondi_tl(cc
.cond
, cc
.reg
, cc
.imm
, l1
);
1112 /* XXX: does not work with gdbstub "ice" single step - not a
1114 static int gen_jz_ecx_string(DisasContext
*s
, target_ulong next_eip
)
1118 l1
= gen_new_label();
1119 l2
= gen_new_label();
1120 gen_op_jnz_ecx(s
->aflag
, l1
);
1122 gen_jmp_tb(s
, next_eip
, 1);
1127 static inline void gen_stos(DisasContext
*s
, TCGMemOp ot
)
1129 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EAX
);
1130 gen_string_movl_A0_EDI(s
);
1131 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
1132 gen_op_movl_T0_Dshift(ot
);
1133 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
1136 static inline void gen_lods(DisasContext
*s
, TCGMemOp ot
)
1138 gen_string_movl_A0_ESI(s
);
1139 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1140 gen_op_mov_reg_v(ot
, R_EAX
, cpu_T
[0]);
1141 gen_op_movl_T0_Dshift(ot
);
1142 gen_op_add_reg_T0(s
->aflag
, R_ESI
);
1145 static inline void gen_scas(DisasContext
*s
, TCGMemOp ot
)
1147 gen_string_movl_A0_EDI(s
);
1148 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
1149 gen_op(s
, OP_CMPL
, ot
, R_EAX
);
1150 gen_op_movl_T0_Dshift(ot
);
1151 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
1154 static inline void gen_cmps(DisasContext
*s
, TCGMemOp ot
)
1156 gen_string_movl_A0_EDI(s
);
1157 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
1158 gen_string_movl_A0_ESI(s
);
1159 gen_op(s
, OP_CMPL
, ot
, OR_TMP0
);
1160 gen_op_movl_T0_Dshift(ot
);
1161 gen_op_add_reg_T0(s
->aflag
, R_ESI
);
1162 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
1165 static inline void gen_ins(DisasContext
*s
, TCGMemOp ot
)
1169 gen_string_movl_A0_EDI(s
);
1170 /* Note: we must do this dummy write first to be restartable in
1171 case of page fault. */
1172 tcg_gen_movi_tl(cpu_T
[0], 0);
1173 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
1174 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_regs
[R_EDX
]);
1175 tcg_gen_andi_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, 0xffff);
1176 gen_helper_in_func(ot
, cpu_T
[0], cpu_tmp2_i32
);
1177 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
1178 gen_op_movl_T0_Dshift(ot
);
1179 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
1184 static inline void gen_outs(DisasContext
*s
, TCGMemOp ot
)
1188 gen_string_movl_A0_ESI(s
);
1189 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1191 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_regs
[R_EDX
]);
1192 tcg_gen_andi_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, 0xffff);
1193 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[0]);
1194 gen_helper_out_func(ot
, cpu_tmp2_i32
, cpu_tmp3_i32
);
1196 gen_op_movl_T0_Dshift(ot
);
1197 gen_op_add_reg_T0(s
->aflag
, R_ESI
);
1202 /* same method as Valgrind : we generate jumps to current or next
1204 #define GEN_REPZ(op) \
1205 static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
1206 target_ulong cur_eip, target_ulong next_eip) \
1209 gen_update_cc_op(s); \
1210 l2 = gen_jz_ecx_string(s, next_eip); \
1211 gen_ ## op(s, ot); \
1212 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
1213 /* a loop would cause two single step exceptions if ECX = 1 \
1214 before rep string_insn */ \
1216 gen_op_jz_ecx(s->aflag, l2); \
1217 gen_jmp(s, cur_eip); \
1220 #define GEN_REPZ2(op) \
1221 static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
1222 target_ulong cur_eip, \
1223 target_ulong next_eip, \
1227 gen_update_cc_op(s); \
1228 l2 = gen_jz_ecx_string(s, next_eip); \
1229 gen_ ## op(s, ot); \
1230 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
1231 gen_update_cc_op(s); \
1232 gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2); \
1234 gen_op_jz_ecx(s->aflag, l2); \
1235 gen_jmp(s, cur_eip); \
1246 static void gen_helper_fp_arith_ST0_FT0(int op
)
1250 gen_helper_fadd_ST0_FT0(cpu_env
);
1253 gen_helper_fmul_ST0_FT0(cpu_env
);
1256 gen_helper_fcom_ST0_FT0(cpu_env
);
1259 gen_helper_fcom_ST0_FT0(cpu_env
);
1262 gen_helper_fsub_ST0_FT0(cpu_env
);
1265 gen_helper_fsubr_ST0_FT0(cpu_env
);
1268 gen_helper_fdiv_ST0_FT0(cpu_env
);
1271 gen_helper_fdivr_ST0_FT0(cpu_env
);
1276 /* NOTE the exception in "r" op ordering */
1277 static void gen_helper_fp_arith_STN_ST0(int op
, int opreg
)
1279 TCGv_i32 tmp
= tcg_const_i32(opreg
);
1282 gen_helper_fadd_STN_ST0(cpu_env
, tmp
);
1285 gen_helper_fmul_STN_ST0(cpu_env
, tmp
);
1288 gen_helper_fsubr_STN_ST0(cpu_env
, tmp
);
1291 gen_helper_fsub_STN_ST0(cpu_env
, tmp
);
1294 gen_helper_fdivr_STN_ST0(cpu_env
, tmp
);
1297 gen_helper_fdiv_STN_ST0(cpu_env
, tmp
);
1302 /* if d == OR_TMP0, it means memory operand (address in A0) */
1303 static void gen_op(DisasContext
*s1
, int op
, TCGMemOp ot
, int d
)
1306 gen_op_mov_v_reg(ot
, cpu_T
[0], d
);
1308 gen_op_ld_v(s1
, ot
, cpu_T
[0], cpu_A0
);
1312 gen_compute_eflags_c(s1
, cpu_tmp4
);
1313 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1314 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_tmp4
);
1315 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1316 gen_op_update3_cc(cpu_tmp4
);
1317 set_cc_op(s1
, CC_OP_ADCB
+ ot
);
1320 gen_compute_eflags_c(s1
, cpu_tmp4
);
1321 tcg_gen_sub_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1322 tcg_gen_sub_tl(cpu_T
[0], cpu_T
[0], cpu_tmp4
);
1323 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1324 gen_op_update3_cc(cpu_tmp4
);
1325 set_cc_op(s1
, CC_OP_SBBB
+ ot
);
1328 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1329 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1330 gen_op_update2_cc();
1331 set_cc_op(s1
, CC_OP_ADDB
+ ot
);
1334 tcg_gen_mov_tl(cpu_cc_srcT
, cpu_T
[0]);
1335 tcg_gen_sub_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1336 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1337 gen_op_update2_cc();
1338 set_cc_op(s1
, CC_OP_SUBB
+ ot
);
1342 tcg_gen_and_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1343 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1344 gen_op_update1_cc();
1345 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1348 tcg_gen_or_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1349 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1350 gen_op_update1_cc();
1351 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1354 tcg_gen_xor_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1355 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1356 gen_op_update1_cc();
1357 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1360 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[1]);
1361 tcg_gen_mov_tl(cpu_cc_srcT
, cpu_T
[0]);
1362 tcg_gen_sub_tl(cpu_cc_dst
, cpu_T
[0], cpu_T
[1]);
1363 set_cc_op(s1
, CC_OP_SUBB
+ ot
);
1368 /* if d == OR_TMP0, it means memory operand (address in A0) */
1369 static void gen_inc(DisasContext
*s1
, TCGMemOp ot
, int d
, int c
)
1372 gen_op_mov_v_reg(ot
, cpu_T
[0], d
);
1374 gen_op_ld_v(s1
, ot
, cpu_T
[0], cpu_A0
);
1376 gen_compute_eflags_c(s1
, cpu_cc_src
);
1378 tcg_gen_addi_tl(cpu_T
[0], cpu_T
[0], 1);
1379 set_cc_op(s1
, CC_OP_INCB
+ ot
);
1381 tcg_gen_addi_tl(cpu_T
[0], cpu_T
[0], -1);
1382 set_cc_op(s1
, CC_OP_DECB
+ ot
);
1384 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1385 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
1388 static void gen_shift_flags(DisasContext
*s
, TCGMemOp ot
, TCGv result
,
1389 TCGv shm1
, TCGv count
, bool is_right
)
1391 TCGv_i32 z32
, s32
, oldop
;
1394 /* Store the results into the CC variables. If we know that the
1395 variable must be dead, store unconditionally. Otherwise we'll
1396 need to not disrupt the current contents. */
1397 z_tl
= tcg_const_tl(0);
1398 if (cc_op_live
[s
->cc_op
] & USES_CC_DST
) {
1399 tcg_gen_movcond_tl(TCG_COND_NE
, cpu_cc_dst
, count
, z_tl
,
1400 result
, cpu_cc_dst
);
1402 tcg_gen_mov_tl(cpu_cc_dst
, result
);
1404 if (cc_op_live
[s
->cc_op
] & USES_CC_SRC
) {
1405 tcg_gen_movcond_tl(TCG_COND_NE
, cpu_cc_src
, count
, z_tl
,
1408 tcg_gen_mov_tl(cpu_cc_src
, shm1
);
1410 tcg_temp_free(z_tl
);
1412 /* Get the two potential CC_OP values into temporaries. */
1413 tcg_gen_movi_i32(cpu_tmp2_i32
, (is_right
? CC_OP_SARB
: CC_OP_SHLB
) + ot
);
1414 if (s
->cc_op
== CC_OP_DYNAMIC
) {
1417 tcg_gen_movi_i32(cpu_tmp3_i32
, s
->cc_op
);
1418 oldop
= cpu_tmp3_i32
;
1421 /* Conditionally store the CC_OP value. */
1422 z32
= tcg_const_i32(0);
1423 s32
= tcg_temp_new_i32();
1424 tcg_gen_trunc_tl_i32(s32
, count
);
1425 tcg_gen_movcond_i32(TCG_COND_NE
, cpu_cc_op
, s32
, z32
, cpu_tmp2_i32
, oldop
);
1426 tcg_temp_free_i32(z32
);
1427 tcg_temp_free_i32(s32
);
1429 /* The CC_OP value is no longer predictable. */
1430 set_cc_op(s
, CC_OP_DYNAMIC
);
1433 static void gen_shift_rm_T1(DisasContext
*s
, TCGMemOp ot
, int op1
,
1434 int is_right
, int is_arith
)
1436 target_ulong mask
= (ot
== MO_64
? 0x3f : 0x1f);
1439 if (op1
== OR_TMP0
) {
1440 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1442 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1445 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], mask
);
1446 tcg_gen_subi_tl(cpu_tmp0
, cpu_T
[1], 1);
1450 gen_exts(ot
, cpu_T
[0]);
1451 tcg_gen_sar_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1452 tcg_gen_sar_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1454 gen_extu(ot
, cpu_T
[0]);
1455 tcg_gen_shr_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1456 tcg_gen_shr_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1459 tcg_gen_shl_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1460 tcg_gen_shl_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1464 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1466 gen_shift_flags(s
, ot
, cpu_T
[0], cpu_tmp0
, cpu_T
[1], is_right
);
1469 static void gen_shift_rm_im(DisasContext
*s
, TCGMemOp ot
, int op1
, int op2
,
1470 int is_right
, int is_arith
)
1472 int mask
= (ot
== MO_64
? 0x3f : 0x1f);
1476 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1478 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1484 gen_exts(ot
, cpu_T
[0]);
1485 tcg_gen_sari_tl(cpu_tmp4
, cpu_T
[0], op2
- 1);
1486 tcg_gen_sari_tl(cpu_T
[0], cpu_T
[0], op2
);
1488 gen_extu(ot
, cpu_T
[0]);
1489 tcg_gen_shri_tl(cpu_tmp4
, cpu_T
[0], op2
- 1);
1490 tcg_gen_shri_tl(cpu_T
[0], cpu_T
[0], op2
);
1493 tcg_gen_shli_tl(cpu_tmp4
, cpu_T
[0], op2
- 1);
1494 tcg_gen_shli_tl(cpu_T
[0], cpu_T
[0], op2
);
1499 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1501 /* update eflags if non zero shift */
1503 tcg_gen_mov_tl(cpu_cc_src
, cpu_tmp4
);
1504 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
1505 set_cc_op(s
, (is_right
? CC_OP_SARB
: CC_OP_SHLB
) + ot
);
1509 static void gen_rot_rm_T1(DisasContext
*s
, TCGMemOp ot
, int op1
, int is_right
)
1511 target_ulong mask
= (ot
== MO_64
? 0x3f : 0x1f);
1515 if (op1
== OR_TMP0
) {
1516 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1518 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1521 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], mask
);
1525 /* Replicate the 8-bit input so that a 32-bit rotate works. */
1526 tcg_gen_ext8u_tl(cpu_T
[0], cpu_T
[0]);
1527 tcg_gen_muli_tl(cpu_T
[0], cpu_T
[0], 0x01010101);
1530 /* Replicate the 16-bit input so that a 32-bit rotate works. */
1531 tcg_gen_deposit_tl(cpu_T
[0], cpu_T
[0], cpu_T
[0], 16, 16);
1534 #ifdef TARGET_X86_64
1536 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
1537 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[1]);
1539 tcg_gen_rotr_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, cpu_tmp3_i32
);
1541 tcg_gen_rotl_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, cpu_tmp3_i32
);
1543 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
1548 tcg_gen_rotr_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1550 tcg_gen_rotl_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1556 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1558 /* We'll need the flags computed into CC_SRC. */
1559 gen_compute_eflags(s
);
1561 /* The value that was "rotated out" is now present at the other end
1562 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1563 since we've computed the flags into CC_SRC, these variables are
1566 tcg_gen_shri_tl(cpu_cc_src2
, cpu_T
[0], mask
- 1);
1567 tcg_gen_shri_tl(cpu_cc_dst
, cpu_T
[0], mask
);
1568 tcg_gen_andi_tl(cpu_cc_dst
, cpu_cc_dst
, 1);
1570 tcg_gen_shri_tl(cpu_cc_src2
, cpu_T
[0], mask
);
1571 tcg_gen_andi_tl(cpu_cc_dst
, cpu_T
[0], 1);
1573 tcg_gen_andi_tl(cpu_cc_src2
, cpu_cc_src2
, 1);
1574 tcg_gen_xor_tl(cpu_cc_src2
, cpu_cc_src2
, cpu_cc_dst
);
1576 /* Now conditionally store the new CC_OP value. If the shift count
1577 is 0 we keep the CC_OP_EFLAGS setting so that only CC_SRC is live.
1578 Otherwise reuse CC_OP_ADCOX which have the C and O flags split out
1579 exactly as we computed above. */
1580 t0
= tcg_const_i32(0);
1581 t1
= tcg_temp_new_i32();
1582 tcg_gen_trunc_tl_i32(t1
, cpu_T
[1]);
1583 tcg_gen_movi_i32(cpu_tmp2_i32
, CC_OP_ADCOX
);
1584 tcg_gen_movi_i32(cpu_tmp3_i32
, CC_OP_EFLAGS
);
1585 tcg_gen_movcond_i32(TCG_COND_NE
, cpu_cc_op
, t1
, t0
,
1586 cpu_tmp2_i32
, cpu_tmp3_i32
);
1587 tcg_temp_free_i32(t0
);
1588 tcg_temp_free_i32(t1
);
1590 /* The CC_OP value is no longer predictable. */
1591 set_cc_op(s
, CC_OP_DYNAMIC
);
1594 static void gen_rot_rm_im(DisasContext
*s
, TCGMemOp ot
, int op1
, int op2
,
1597 int mask
= (ot
== MO_64
? 0x3f : 0x1f);
1601 if (op1
== OR_TMP0
) {
1602 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1604 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1610 #ifdef TARGET_X86_64
1612 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
1614 tcg_gen_rotri_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, op2
);
1616 tcg_gen_rotli_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, op2
);
1618 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
1623 tcg_gen_rotri_tl(cpu_T
[0], cpu_T
[0], op2
);
1625 tcg_gen_rotli_tl(cpu_T
[0], cpu_T
[0], op2
);
1636 shift
= mask
+ 1 - shift
;
1638 gen_extu(ot
, cpu_T
[0]);
1639 tcg_gen_shli_tl(cpu_tmp0
, cpu_T
[0], shift
);
1640 tcg_gen_shri_tl(cpu_T
[0], cpu_T
[0], mask
+ 1 - shift
);
1641 tcg_gen_or_tl(cpu_T
[0], cpu_T
[0], cpu_tmp0
);
1647 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1650 /* Compute the flags into CC_SRC. */
1651 gen_compute_eflags(s
);
1653 /* The value that was "rotated out" is now present at the other end
1654 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1655 since we've computed the flags into CC_SRC, these variables are
1658 tcg_gen_shri_tl(cpu_cc_src2
, cpu_T
[0], mask
- 1);
1659 tcg_gen_shri_tl(cpu_cc_dst
, cpu_T
[0], mask
);
1660 tcg_gen_andi_tl(cpu_cc_dst
, cpu_cc_dst
, 1);
1662 tcg_gen_shri_tl(cpu_cc_src2
, cpu_T
[0], mask
);
1663 tcg_gen_andi_tl(cpu_cc_dst
, cpu_T
[0], 1);
1665 tcg_gen_andi_tl(cpu_cc_src2
, cpu_cc_src2
, 1);
1666 tcg_gen_xor_tl(cpu_cc_src2
, cpu_cc_src2
, cpu_cc_dst
);
1667 set_cc_op(s
, CC_OP_ADCOX
);
1671 /* XXX: add faster immediate = 1 case */
1672 static void gen_rotc_rm_T1(DisasContext
*s
, TCGMemOp ot
, int op1
,
1675 gen_compute_eflags(s
);
1676 assert(s
->cc_op
== CC_OP_EFLAGS
);
1680 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1682 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1687 gen_helper_rcrb(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1690 gen_helper_rcrw(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1693 gen_helper_rcrl(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1695 #ifdef TARGET_X86_64
1697 gen_helper_rcrq(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1706 gen_helper_rclb(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1709 gen_helper_rclw(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1712 gen_helper_rcll(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1714 #ifdef TARGET_X86_64
1716 gen_helper_rclq(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1724 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1727 /* XXX: add faster immediate case */
1728 static void gen_shiftd_rm_T1(DisasContext
*s
, TCGMemOp ot
, int op1
,
1729 bool is_right
, TCGv count_in
)
1731 target_ulong mask
= (ot
== MO_64
? 63 : 31);
1735 if (op1
== OR_TMP0
) {
1736 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1738 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1741 count
= tcg_temp_new();
1742 tcg_gen_andi_tl(count
, count_in
, mask
);
1746 /* Note: we implement the Intel behaviour for shift count > 16.
1747 This means "shrdw C, B, A" shifts A:B:A >> C. Build the B:A
1748 portion by constructing it as a 32-bit value. */
1750 tcg_gen_deposit_tl(cpu_tmp0
, cpu_T
[0], cpu_T
[1], 16, 16);
1751 tcg_gen_mov_tl(cpu_T
[1], cpu_T
[0]);
1752 tcg_gen_mov_tl(cpu_T
[0], cpu_tmp0
);
1754 tcg_gen_deposit_tl(cpu_T
[1], cpu_T
[0], cpu_T
[1], 16, 16);
1757 #ifdef TARGET_X86_64
1759 /* Concatenate the two 32-bit values and use a 64-bit shift. */
1760 tcg_gen_subi_tl(cpu_tmp0
, count
, 1);
1762 tcg_gen_concat_tl_i64(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1763 tcg_gen_shr_i64(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1764 tcg_gen_shr_i64(cpu_T
[0], cpu_T
[0], count
);
1766 tcg_gen_concat_tl_i64(cpu_T
[0], cpu_T
[1], cpu_T
[0]);
1767 tcg_gen_shl_i64(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1768 tcg_gen_shl_i64(cpu_T
[0], cpu_T
[0], count
);
1769 tcg_gen_shri_i64(cpu_tmp0
, cpu_tmp0
, 32);
1770 tcg_gen_shri_i64(cpu_T
[0], cpu_T
[0], 32);
1775 tcg_gen_subi_tl(cpu_tmp0
, count
, 1);
1777 tcg_gen_shr_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1779 tcg_gen_subfi_tl(cpu_tmp4
, mask
+ 1, count
);
1780 tcg_gen_shr_tl(cpu_T
[0], cpu_T
[0], count
);
1781 tcg_gen_shl_tl(cpu_T
[1], cpu_T
[1], cpu_tmp4
);
1783 tcg_gen_shl_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1785 /* Only needed if count > 16, for Intel behaviour. */
1786 tcg_gen_subfi_tl(cpu_tmp4
, 33, count
);
1787 tcg_gen_shr_tl(cpu_tmp4
, cpu_T
[1], cpu_tmp4
);
1788 tcg_gen_or_tl(cpu_tmp0
, cpu_tmp0
, cpu_tmp4
);
1791 tcg_gen_subfi_tl(cpu_tmp4
, mask
+ 1, count
);
1792 tcg_gen_shl_tl(cpu_T
[0], cpu_T
[0], count
);
1793 tcg_gen_shr_tl(cpu_T
[1], cpu_T
[1], cpu_tmp4
);
1795 tcg_gen_movi_tl(cpu_tmp4
, 0);
1796 tcg_gen_movcond_tl(TCG_COND_EQ
, cpu_T
[1], count
, cpu_tmp4
,
1797 cpu_tmp4
, cpu_T
[1]);
1798 tcg_gen_or_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1803 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1805 gen_shift_flags(s
, ot
, cpu_T
[0], cpu_tmp0
, count
, is_right
);
1806 tcg_temp_free(count
);
1809 static void gen_shift(DisasContext
*s1
, int op
, TCGMemOp ot
, int d
, int s
)
1812 gen_op_mov_v_reg(ot
, cpu_T
[1], s
);
1815 gen_rot_rm_T1(s1
, ot
, d
, 0);
1818 gen_rot_rm_T1(s1
, ot
, d
, 1);
1822 gen_shift_rm_T1(s1
, ot
, d
, 0, 0);
1825 gen_shift_rm_T1(s1
, ot
, d
, 1, 0);
1828 gen_shift_rm_T1(s1
, ot
, d
, 1, 1);
1831 gen_rotc_rm_T1(s1
, ot
, d
, 0);
1834 gen_rotc_rm_T1(s1
, ot
, d
, 1);
1839 static void gen_shifti(DisasContext
*s1
, int op
, TCGMemOp ot
, int d
, int c
)
1843 gen_rot_rm_im(s1
, ot
, d
, c
, 0);
1846 gen_rot_rm_im(s1
, ot
, d
, c
, 1);
1850 gen_shift_rm_im(s1
, ot
, d
, c
, 0, 0);
1853 gen_shift_rm_im(s1
, ot
, d
, c
, 1, 0);
1856 gen_shift_rm_im(s1
, ot
, d
, c
, 1, 1);
1859 /* currently not optimized */
1860 tcg_gen_movi_tl(cpu_T
[1], c
);
1861 gen_shift(s1
, op
, ot
, d
, OR_TMP1
);
1866 static void gen_lea_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
)
1873 int mod
, rm
, code
, override
, must_add_seg
;
1876 override
= s
->override
;
1877 must_add_seg
= s
->addseg
;
1880 mod
= (modrm
>> 6) & 3;
1893 code
= cpu_ldub_code(env
, s
->pc
++);
1894 scale
= (code
>> 6) & 3;
1895 index
= ((code
>> 3) & 7) | REX_X(s
);
1897 index
= -1; /* no index */
1905 if ((base
& 7) == 5) {
1907 disp
= (int32_t)cpu_ldl_code(env
, s
->pc
);
1909 if (CODE64(s
) && !havesib
) {
1910 disp
+= s
->pc
+ s
->rip_offset
;
1917 disp
= (int8_t)cpu_ldub_code(env
, s
->pc
++);
1921 disp
= (int32_t)cpu_ldl_code(env
, s
->pc
);
1926 /* For correct popl handling with esp. */
1927 if (base
== R_ESP
&& s
->popl_esp_hack
) {
1928 disp
+= s
->popl_esp_hack
;
1931 /* Compute the address, with a minimum number of TCG ops. */
1935 sum
= cpu_regs
[index
];
1937 tcg_gen_shli_tl(cpu_A0
, cpu_regs
[index
], scale
);
1941 tcg_gen_add_tl(cpu_A0
, sum
, cpu_regs
[base
]);
1944 } else if (base
>= 0) {
1945 sum
= cpu_regs
[base
];
1947 if (TCGV_IS_UNUSED(sum
)) {
1948 tcg_gen_movi_tl(cpu_A0
, disp
);
1950 tcg_gen_addi_tl(cpu_A0
, sum
, disp
);
1955 if (base
== R_EBP
|| base
== R_ESP
) {
1962 tcg_gen_ld_tl(cpu_tmp0
, cpu_env
,
1963 offsetof(CPUX86State
, segs
[override
].base
));
1965 if (s
->aflag
== MO_32
) {
1966 tcg_gen_ext32u_tl(cpu_A0
, cpu_A0
);
1968 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
1972 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
1975 if (s
->aflag
== MO_32
) {
1976 tcg_gen_ext32u_tl(cpu_A0
, cpu_A0
);
1984 disp
= cpu_lduw_code(env
, s
->pc
);
1986 tcg_gen_movi_tl(cpu_A0
, disp
);
1987 rm
= 0; /* avoid SS override */
1994 disp
= (int8_t)cpu_ldub_code(env
, s
->pc
++);
1998 disp
= (int16_t)cpu_lduw_code(env
, s
->pc
);
2006 tcg_gen_add_tl(cpu_A0
, cpu_regs
[R_EBX
], cpu_regs
[R_ESI
]);
2009 tcg_gen_add_tl(cpu_A0
, cpu_regs
[R_EBX
], cpu_regs
[R_EDI
]);
2012 tcg_gen_add_tl(cpu_A0
, cpu_regs
[R_EBP
], cpu_regs
[R_ESI
]);
2015 tcg_gen_add_tl(cpu_A0
, cpu_regs
[R_EBP
], cpu_regs
[R_EDI
]);
2018 sum
= cpu_regs
[R_ESI
];
2021 sum
= cpu_regs
[R_EDI
];
2024 sum
= cpu_regs
[R_EBP
];
2028 sum
= cpu_regs
[R_EBX
];
2031 tcg_gen_addi_tl(cpu_A0
, sum
, disp
);
2032 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2036 if (rm
== 2 || rm
== 3 || rm
== 6) {
2042 gen_op_addl_A0_seg(s
, override
);
2051 static void gen_nop_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
)
2053 int mod
, rm
, base
, code
;
2055 mod
= (modrm
>> 6) & 3;
2066 code
= cpu_ldub_code(env
, s
->pc
++);
2108 /* used for LEA and MOV AX, mem */
2109 static void gen_add_A0_ds_seg(DisasContext
*s
)
2111 int override
, must_add_seg
;
2112 must_add_seg
= s
->addseg
;
2114 if (s
->override
>= 0) {
2115 override
= s
->override
;
2119 #ifdef TARGET_X86_64
2121 gen_op_addq_A0_seg(override
);
2125 gen_op_addl_A0_seg(s
, override
);
2130 /* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
2132 static void gen_ldst_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
,
2133 TCGMemOp ot
, int reg
, int is_store
)
2137 mod
= (modrm
>> 6) & 3;
2138 rm
= (modrm
& 7) | REX_B(s
);
2142 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
2143 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
2145 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
2147 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
2150 gen_lea_modrm(env
, s
, modrm
);
2153 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
2154 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
2156 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
2158 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
2163 static inline uint32_t insn_get(CPUX86State
*env
, DisasContext
*s
, TCGMemOp ot
)
2169 ret
= cpu_ldub_code(env
, s
->pc
);
2173 ret
= cpu_lduw_code(env
, s
->pc
);
2177 #ifdef TARGET_X86_64
2180 ret
= cpu_ldl_code(env
, s
->pc
);
2189 static inline int insn_const_size(TCGMemOp ot
)
2198 static inline void gen_goto_tb(DisasContext
*s
, int tb_num
, target_ulong eip
)
2200 TranslationBlock
*tb
;
2203 pc
= s
->cs_base
+ eip
;
2205 /* NOTE: we handle the case where the TB spans two pages here */
2206 if ((pc
& TARGET_PAGE_MASK
) == (tb
->pc
& TARGET_PAGE_MASK
) ||
2207 (pc
& TARGET_PAGE_MASK
) == ((s
->pc
- 1) & TARGET_PAGE_MASK
)) {
2208 /* jump to same page: we can use a direct jump */
2209 tcg_gen_goto_tb(tb_num
);
2211 tcg_gen_exit_tb((uintptr_t)tb
+ tb_num
);
2213 /* jump to another page: currently not optimized */
2219 static inline void gen_jcc(DisasContext
*s
, int b
,
2220 target_ulong val
, target_ulong next_eip
)
2225 l1
= gen_new_label();
2228 gen_goto_tb(s
, 0, next_eip
);
2231 gen_goto_tb(s
, 1, val
);
2232 s
->is_jmp
= DISAS_TB_JUMP
;
2234 l1
= gen_new_label();
2235 l2
= gen_new_label();
2238 gen_jmp_im(next_eip
);
2248 static void gen_cmovcc1(CPUX86State
*env
, DisasContext
*s
, TCGMemOp ot
, int b
,
2253 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
2255 cc
= gen_prepare_cc(s
, b
, cpu_T
[1]);
2256 if (cc
.mask
!= -1) {
2257 TCGv t0
= tcg_temp_new();
2258 tcg_gen_andi_tl(t0
, cc
.reg
, cc
.mask
);
2262 cc
.reg2
= tcg_const_tl(cc
.imm
);
2265 tcg_gen_movcond_tl(cc
.cond
, cpu_T
[0], cc
.reg
, cc
.reg2
,
2266 cpu_T
[0], cpu_regs
[reg
]);
2267 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
2269 if (cc
.mask
!= -1) {
2270 tcg_temp_free(cc
.reg
);
2273 tcg_temp_free(cc
.reg2
);
2277 static inline void gen_op_movl_T0_seg(int seg_reg
)
2279 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
,
2280 offsetof(CPUX86State
,segs
[seg_reg
].selector
));
2283 static inline void gen_op_movl_seg_T0_vm(int seg_reg
)
2285 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 0xffff);
2286 tcg_gen_st32_tl(cpu_T
[0], cpu_env
,
2287 offsetof(CPUX86State
,segs
[seg_reg
].selector
));
2288 tcg_gen_shli_tl(cpu_T
[0], cpu_T
[0], 4);
2289 tcg_gen_st_tl(cpu_T
[0], cpu_env
,
2290 offsetof(CPUX86State
,segs
[seg_reg
].base
));
2293 /* move T0 to seg_reg and compute if the CPU state may change. Never
2294 call this function with seg_reg == R_CS */
2295 static void gen_movl_seg_T0(DisasContext
*s
, int seg_reg
, target_ulong cur_eip
)
2297 if (s
->pe
&& !s
->vm86
) {
2298 /* XXX: optimize by finding processor state dynamically */
2299 gen_update_cc_op(s
);
2300 gen_jmp_im(cur_eip
);
2301 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
2302 gen_helper_load_seg(cpu_env
, tcg_const_i32(seg_reg
), cpu_tmp2_i32
);
2303 /* abort translation because the addseg value may change or
2304 because ss32 may change. For R_SS, translation must always
2305 stop as a special handling must be done to disable hardware
2306 interrupts for the next instruction */
2307 if (seg_reg
== R_SS
|| (s
->code32
&& seg_reg
< R_FS
))
2308 s
->is_jmp
= DISAS_TB_JUMP
;
2310 gen_op_movl_seg_T0_vm(seg_reg
);
2311 if (seg_reg
== R_SS
)
2312 s
->is_jmp
= DISAS_TB_JUMP
;
2316 static inline int svm_is_rep(int prefixes
)
2318 return ((prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) ? 8 : 0);
2322 gen_svm_check_intercept_param(DisasContext
*s
, target_ulong pc_start
,
2323 uint32_t type
, uint64_t param
)
2325 /* no SVM activated; fast case */
2326 if (likely(!(s
->flags
& HF_SVMI_MASK
)))
2328 gen_update_cc_op(s
);
2329 gen_jmp_im(pc_start
- s
->cs_base
);
2330 gen_helper_svm_check_intercept_param(cpu_env
, tcg_const_i32(type
),
2331 tcg_const_i64(param
));
2335 gen_svm_check_intercept(DisasContext
*s
, target_ulong pc_start
, uint64_t type
)
2337 gen_svm_check_intercept_param(s
, pc_start
, type
, 0);
2340 static inline void gen_stack_update(DisasContext
*s
, int addend
)
2342 #ifdef TARGET_X86_64
2344 gen_op_add_reg_im(MO_64
, R_ESP
, addend
);
2348 gen_op_add_reg_im(MO_32
, R_ESP
, addend
);
2350 gen_op_add_reg_im(MO_16
, R_ESP
, addend
);
2354 /* Generate a push. It depends on ss32, addseg and dflag. */
2355 static void gen_push_v(DisasContext
*s
, TCGv val
)
2357 TCGMemOp a_ot
, d_ot
= mo_pushpop(s
, s
->dflag
);
2358 int size
= 1 << d_ot
;
2359 TCGv new_esp
= cpu_A0
;
2361 tcg_gen_subi_tl(cpu_A0
, cpu_regs
[R_ESP
], size
);
2365 } else if (s
->ss32
) {
2369 tcg_gen_mov_tl(new_esp
, cpu_A0
);
2370 gen_op_addl_A0_seg(s
, R_SS
);
2372 tcg_gen_ext32u_tl(cpu_A0
, cpu_A0
);
2377 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2378 tcg_gen_mov_tl(new_esp
, cpu_A0
);
2379 gen_op_addl_A0_seg(s
, R_SS
);
2382 gen_op_st_v(s
, d_ot
, val
, cpu_A0
);
2383 gen_op_mov_reg_v(a_ot
, R_ESP
, new_esp
);
2386 /* two step pop is necessary for precise exceptions */
2387 static TCGMemOp
gen_pop_T0(DisasContext
*s
)
2389 TCGMemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2393 addr
= cpu_regs
[R_ESP
];
2394 } else if (!s
->ss32
) {
2395 tcg_gen_ext16u_tl(cpu_A0
, cpu_regs
[R_ESP
]);
2396 gen_op_addl_A0_seg(s
, R_SS
);
2397 } else if (s
->addseg
) {
2398 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[R_ESP
]);
2399 gen_op_addl_A0_seg(s
, R_SS
);
2401 tcg_gen_ext32u_tl(cpu_A0
, cpu_regs
[R_ESP
]);
2404 gen_op_ld_v(s
, d_ot
, cpu_T
[0], addr
);
2408 static void gen_pop_update(DisasContext
*s
, TCGMemOp ot
)
2410 gen_stack_update(s
, 1 << ot
);
2413 static void gen_stack_A0(DisasContext
*s
)
2415 gen_op_movl_A0_reg(R_ESP
);
2417 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2418 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2420 gen_op_addl_A0_seg(s
, R_SS
);
2423 /* NOTE: wrap around in 16 bit not fully handled */
2424 static void gen_pusha(DisasContext
*s
)
2427 gen_op_movl_A0_reg(R_ESP
);
2428 gen_op_addl_A0_im(-8 << s
->dflag
);
2430 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2431 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2433 gen_op_addl_A0_seg(s
, R_SS
);
2434 for(i
= 0;i
< 8; i
++) {
2435 gen_op_mov_v_reg(MO_32
, cpu_T
[0], 7 - i
);
2436 gen_op_st_v(s
, s
->dflag
, cpu_T
[0], cpu_A0
);
2437 gen_op_addl_A0_im(1 << s
->dflag
);
2439 gen_op_mov_reg_v(MO_16
+ s
->ss32
, R_ESP
, cpu_T
[1]);
2442 /* NOTE: wrap around in 16 bit not fully handled */
2443 static void gen_popa(DisasContext
*s
)
2446 gen_op_movl_A0_reg(R_ESP
);
2448 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2449 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2450 tcg_gen_addi_tl(cpu_T
[1], cpu_T
[1], 8 << s
->dflag
);
2452 gen_op_addl_A0_seg(s
, R_SS
);
2453 for(i
= 0;i
< 8; i
++) {
2454 /* ESP is not reloaded */
2456 gen_op_ld_v(s
, s
->dflag
, cpu_T
[0], cpu_A0
);
2457 gen_op_mov_reg_v(s
->dflag
, 7 - i
, cpu_T
[0]);
2459 gen_op_addl_A0_im(1 << s
->dflag
);
2461 gen_op_mov_reg_v(MO_16
+ s
->ss32
, R_ESP
, cpu_T
[1]);
2464 static void gen_enter(DisasContext
*s
, int esp_addend
, int level
)
2466 TCGMemOp ot
= mo_pushpop(s
, s
->dflag
);
2467 int opsize
= 1 << ot
;
2470 #ifdef TARGET_X86_64
2472 gen_op_movl_A0_reg(R_ESP
);
2473 gen_op_addq_A0_im(-opsize
);
2474 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2477 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EBP
);
2478 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
2480 /* XXX: must save state */
2481 gen_helper_enter64_level(cpu_env
, tcg_const_i32(level
),
2482 tcg_const_i32((ot
== MO_64
)),
2485 gen_op_mov_reg_v(ot
, R_EBP
, cpu_T
[1]);
2486 tcg_gen_addi_tl(cpu_T
[1], cpu_T
[1], -esp_addend
+ (-opsize
* level
));
2487 gen_op_mov_reg_v(MO_64
, R_ESP
, cpu_T
[1]);
2491 gen_op_movl_A0_reg(R_ESP
);
2492 gen_op_addl_A0_im(-opsize
);
2494 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2495 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2497 gen_op_addl_A0_seg(s
, R_SS
);
2499 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EBP
);
2500 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
2502 /* XXX: must save state */
2503 gen_helper_enter_level(cpu_env
, tcg_const_i32(level
),
2504 tcg_const_i32(s
->dflag
- 1),
2507 gen_op_mov_reg_v(ot
, R_EBP
, cpu_T
[1]);
2508 tcg_gen_addi_tl(cpu_T
[1], cpu_T
[1], -esp_addend
+ (-opsize
* level
));
2509 gen_op_mov_reg_v(MO_16
+ s
->ss32
, R_ESP
, cpu_T
[1]);
2513 static void gen_exception(DisasContext
*s
, int trapno
, target_ulong cur_eip
)
2515 gen_update_cc_op(s
);
2516 gen_jmp_im(cur_eip
);
2517 gen_helper_raise_exception(cpu_env
, tcg_const_i32(trapno
));
2518 s
->is_jmp
= DISAS_TB_JUMP
;
2521 /* an interrupt is different from an exception because of the
2523 static void gen_interrupt(DisasContext
*s
, int intno
,
2524 target_ulong cur_eip
, target_ulong next_eip
)
2526 gen_update_cc_op(s
);
2527 gen_jmp_im(cur_eip
);
2528 gen_helper_raise_interrupt(cpu_env
, tcg_const_i32(intno
),
2529 tcg_const_i32(next_eip
- cur_eip
));
2530 s
->is_jmp
= DISAS_TB_JUMP
;
2533 static void gen_debug(DisasContext
*s
, target_ulong cur_eip
)
2535 gen_update_cc_op(s
);
2536 gen_jmp_im(cur_eip
);
2537 gen_helper_debug(cpu_env
);
2538 s
->is_jmp
= DISAS_TB_JUMP
;
2541 /* generate a generic end of block. Trace exception is also generated
2543 static void gen_eob(DisasContext
*s
)
2545 gen_update_cc_op(s
);
2546 if (s
->tb
->flags
& HF_INHIBIT_IRQ_MASK
) {
2547 gen_helper_reset_inhibit_irq(cpu_env
);
2549 if (s
->tb
->flags
& HF_RF_MASK
) {
2550 gen_helper_reset_rf(cpu_env
);
2552 if (s
->singlestep_enabled
) {
2553 gen_helper_debug(cpu_env
);
2555 gen_helper_single_step(cpu_env
);
2559 s
->is_jmp
= DISAS_TB_JUMP
;
2562 /* generate a jump to eip. No segment change must happen before as a
2563 direct call to the next block may occur */
2564 static void gen_jmp_tb(DisasContext
*s
, target_ulong eip
, int tb_num
)
2566 gen_update_cc_op(s
);
2567 set_cc_op(s
, CC_OP_DYNAMIC
);
2569 gen_goto_tb(s
, tb_num
, eip
);
2570 s
->is_jmp
= DISAS_TB_JUMP
;
2577 static void gen_jmp(DisasContext
*s
, target_ulong eip
)
2579 gen_jmp_tb(s
, eip
, 0);
2582 static inline void gen_ldq_env_A0(DisasContext
*s
, int offset
)
2584 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
, s
->mem_index
, MO_LEQ
);
2585 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, offset
);
2588 static inline void gen_stq_env_A0(DisasContext
*s
, int offset
)
2590 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, offset
);
2591 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
, s
->mem_index
, MO_LEQ
);
2594 static inline void gen_ldo_env_A0(DisasContext
*s
, int offset
)
2596 int mem_index
= s
->mem_index
;
2597 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
, mem_index
, MO_LEQ
);
2598 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(0)));
2599 tcg_gen_addi_tl(cpu_tmp0
, cpu_A0
, 8);
2600 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_tmp0
, mem_index
, MO_LEQ
);
2601 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(1)));
2604 static inline void gen_sto_env_A0(DisasContext
*s
, int offset
)
2606 int mem_index
= s
->mem_index
;
2607 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(0)));
2608 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
, mem_index
, MO_LEQ
);
2609 tcg_gen_addi_tl(cpu_tmp0
, cpu_A0
, 8);
2610 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(1)));
2611 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_tmp0
, mem_index
, MO_LEQ
);
2614 static inline void gen_op_movo(int d_offset
, int s_offset
)
2616 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, s_offset
);
2617 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, d_offset
);
2618 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, s_offset
+ 8);
2619 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, d_offset
+ 8);
2622 static inline void gen_op_movq(int d_offset
, int s_offset
)
2624 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, s_offset
);
2625 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, d_offset
);
2628 static inline void gen_op_movl(int d_offset
, int s_offset
)
2630 tcg_gen_ld_i32(cpu_tmp2_i32
, cpu_env
, s_offset
);
2631 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
, d_offset
);
2634 static inline void gen_op_movq_env_0(int d_offset
)
2636 tcg_gen_movi_i64(cpu_tmp1_i64
, 0);
2637 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, d_offset
);
2640 typedef void (*SSEFunc_i_ep
)(TCGv_i32 val
, TCGv_ptr env
, TCGv_ptr reg
);
2641 typedef void (*SSEFunc_l_ep
)(TCGv_i64 val
, TCGv_ptr env
, TCGv_ptr reg
);
2642 typedef void (*SSEFunc_0_epi
)(TCGv_ptr env
, TCGv_ptr reg
, TCGv_i32 val
);
2643 typedef void (*SSEFunc_0_epl
)(TCGv_ptr env
, TCGv_ptr reg
, TCGv_i64 val
);
2644 typedef void (*SSEFunc_0_epp
)(TCGv_ptr env
, TCGv_ptr reg_a
, TCGv_ptr reg_b
);
2645 typedef void (*SSEFunc_0_eppi
)(TCGv_ptr env
, TCGv_ptr reg_a
, TCGv_ptr reg_b
,
2647 typedef void (*SSEFunc_0_ppi
)(TCGv_ptr reg_a
, TCGv_ptr reg_b
, TCGv_i32 val
);
2648 typedef void (*SSEFunc_0_eppt
)(TCGv_ptr env
, TCGv_ptr reg_a
, TCGv_ptr reg_b
,
2651 #define SSE_SPECIAL ((void *)1)
2652 #define SSE_DUMMY ((void *)2)
2654 #define MMX_OP2(x) { gen_helper_ ## x ## _mmx, gen_helper_ ## x ## _xmm }
2655 #define SSE_FOP(x) { gen_helper_ ## x ## ps, gen_helper_ ## x ## pd, \
2656 gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, }
2658 static const SSEFunc_0_epp sse_op_table1
[256][4] = {
2659 /* 3DNow! extensions */
2660 [0x0e] = { SSE_DUMMY
}, /* femms */
2661 [0x0f] = { SSE_DUMMY
}, /* pf... */
2662 /* pure SSE operations */
2663 [0x10] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movups, movupd, movss, movsd */
2664 [0x11] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movups, movupd, movss, movsd */
2665 [0x12] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movlps, movlpd, movsldup, movddup */
2666 [0x13] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movlps, movlpd */
2667 [0x14] = { gen_helper_punpckldq_xmm
, gen_helper_punpcklqdq_xmm
},
2668 [0x15] = { gen_helper_punpckhdq_xmm
, gen_helper_punpckhqdq_xmm
},
2669 [0x16] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movhps, movhpd, movshdup */
2670 [0x17] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movhps, movhpd */
2672 [0x28] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movaps, movapd */
2673 [0x29] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movaps, movapd */
2674 [0x2a] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */
2675 [0x2b] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movntps, movntpd, movntss, movntsd */
2676 [0x2c] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */
2677 [0x2d] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */
2678 [0x2e] = { gen_helper_ucomiss
, gen_helper_ucomisd
},
2679 [0x2f] = { gen_helper_comiss
, gen_helper_comisd
},
2680 [0x50] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movmskps, movmskpd */
2681 [0x51] = SSE_FOP(sqrt
),
2682 [0x52] = { gen_helper_rsqrtps
, NULL
, gen_helper_rsqrtss
, NULL
},
2683 [0x53] = { gen_helper_rcpps
, NULL
, gen_helper_rcpss
, NULL
},
2684 [0x54] = { gen_helper_pand_xmm
, gen_helper_pand_xmm
}, /* andps, andpd */
2685 [0x55] = { gen_helper_pandn_xmm
, gen_helper_pandn_xmm
}, /* andnps, andnpd */
2686 [0x56] = { gen_helper_por_xmm
, gen_helper_por_xmm
}, /* orps, orpd */
2687 [0x57] = { gen_helper_pxor_xmm
, gen_helper_pxor_xmm
}, /* xorps, xorpd */
2688 [0x58] = SSE_FOP(add
),
2689 [0x59] = SSE_FOP(mul
),
2690 [0x5a] = { gen_helper_cvtps2pd
, gen_helper_cvtpd2ps
,
2691 gen_helper_cvtss2sd
, gen_helper_cvtsd2ss
},
2692 [0x5b] = { gen_helper_cvtdq2ps
, gen_helper_cvtps2dq
, gen_helper_cvttps2dq
},
2693 [0x5c] = SSE_FOP(sub
),
2694 [0x5d] = SSE_FOP(min
),
2695 [0x5e] = SSE_FOP(div
),
2696 [0x5f] = SSE_FOP(max
),
2698 [0xc2] = SSE_FOP(cmpeq
),
2699 [0xc6] = { (SSEFunc_0_epp
)gen_helper_shufps
,
2700 (SSEFunc_0_epp
)gen_helper_shufpd
}, /* XXX: casts */
2702 /* SSSE3, SSE4, MOVBE, CRC32, BMI1, BMI2, ADX. */
2703 [0x38] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
},
2704 [0x3a] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
},
2706 /* MMX ops and their SSE extensions */
2707 [0x60] = MMX_OP2(punpcklbw
),
2708 [0x61] = MMX_OP2(punpcklwd
),
2709 [0x62] = MMX_OP2(punpckldq
),
2710 [0x63] = MMX_OP2(packsswb
),
2711 [0x64] = MMX_OP2(pcmpgtb
),
2712 [0x65] = MMX_OP2(pcmpgtw
),
2713 [0x66] = MMX_OP2(pcmpgtl
),
2714 [0x67] = MMX_OP2(packuswb
),
2715 [0x68] = MMX_OP2(punpckhbw
),
2716 [0x69] = MMX_OP2(punpckhwd
),
2717 [0x6a] = MMX_OP2(punpckhdq
),
2718 [0x6b] = MMX_OP2(packssdw
),
2719 [0x6c] = { NULL
, gen_helper_punpcklqdq_xmm
},
2720 [0x6d] = { NULL
, gen_helper_punpckhqdq_xmm
},
2721 [0x6e] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movd mm, ea */
2722 [0x6f] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movq, movdqa, , movqdu */
2723 [0x70] = { (SSEFunc_0_epp
)gen_helper_pshufw_mmx
,
2724 (SSEFunc_0_epp
)gen_helper_pshufd_xmm
,
2725 (SSEFunc_0_epp
)gen_helper_pshufhw_xmm
,
2726 (SSEFunc_0_epp
)gen_helper_pshuflw_xmm
}, /* XXX: casts */
2727 [0x71] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* shiftw */
2728 [0x72] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* shiftd */
2729 [0x73] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* shiftq */
2730 [0x74] = MMX_OP2(pcmpeqb
),
2731 [0x75] = MMX_OP2(pcmpeqw
),
2732 [0x76] = MMX_OP2(pcmpeql
),
2733 [0x77] = { SSE_DUMMY
}, /* emms */
2734 [0x78] = { NULL
, SSE_SPECIAL
, NULL
, SSE_SPECIAL
}, /* extrq_i, insertq_i */
2735 [0x79] = { NULL
, gen_helper_extrq_r
, NULL
, gen_helper_insertq_r
},
2736 [0x7c] = { NULL
, gen_helper_haddpd
, NULL
, gen_helper_haddps
},
2737 [0x7d] = { NULL
, gen_helper_hsubpd
, NULL
, gen_helper_hsubps
},
2738 [0x7e] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movd, movd, , movq */
2739 [0x7f] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movq, movdqa, movdqu */
2740 [0xc4] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* pinsrw */
2741 [0xc5] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* pextrw */
2742 [0xd0] = { NULL
, gen_helper_addsubpd
, NULL
, gen_helper_addsubps
},
2743 [0xd1] = MMX_OP2(psrlw
),
2744 [0xd2] = MMX_OP2(psrld
),
2745 [0xd3] = MMX_OP2(psrlq
),
2746 [0xd4] = MMX_OP2(paddq
),
2747 [0xd5] = MMX_OP2(pmullw
),
2748 [0xd6] = { NULL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
},
2749 [0xd7] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* pmovmskb */
2750 [0xd8] = MMX_OP2(psubusb
),
2751 [0xd9] = MMX_OP2(psubusw
),
2752 [0xda] = MMX_OP2(pminub
),
2753 [0xdb] = MMX_OP2(pand
),
2754 [0xdc] = MMX_OP2(paddusb
),
2755 [0xdd] = MMX_OP2(paddusw
),
2756 [0xde] = MMX_OP2(pmaxub
),
2757 [0xdf] = MMX_OP2(pandn
),
2758 [0xe0] = MMX_OP2(pavgb
),
2759 [0xe1] = MMX_OP2(psraw
),
2760 [0xe2] = MMX_OP2(psrad
),
2761 [0xe3] = MMX_OP2(pavgw
),
2762 [0xe4] = MMX_OP2(pmulhuw
),
2763 [0xe5] = MMX_OP2(pmulhw
),
2764 [0xe6] = { NULL
, gen_helper_cvttpd2dq
, gen_helper_cvtdq2pd
, gen_helper_cvtpd2dq
},
2765 [0xe7] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movntq, movntq */
2766 [0xe8] = MMX_OP2(psubsb
),
2767 [0xe9] = MMX_OP2(psubsw
),
2768 [0xea] = MMX_OP2(pminsw
),
2769 [0xeb] = MMX_OP2(por
),
2770 [0xec] = MMX_OP2(paddsb
),
2771 [0xed] = MMX_OP2(paddsw
),
2772 [0xee] = MMX_OP2(pmaxsw
),
2773 [0xef] = MMX_OP2(pxor
),
2774 [0xf0] = { NULL
, NULL
, NULL
, SSE_SPECIAL
}, /* lddqu */
2775 [0xf1] = MMX_OP2(psllw
),
2776 [0xf2] = MMX_OP2(pslld
),
2777 [0xf3] = MMX_OP2(psllq
),
2778 [0xf4] = MMX_OP2(pmuludq
),
2779 [0xf5] = MMX_OP2(pmaddwd
),
2780 [0xf6] = MMX_OP2(psadbw
),
2781 [0xf7] = { (SSEFunc_0_epp
)gen_helper_maskmov_mmx
,
2782 (SSEFunc_0_epp
)gen_helper_maskmov_xmm
}, /* XXX: casts */
2783 [0xf8] = MMX_OP2(psubb
),
2784 [0xf9] = MMX_OP2(psubw
),
2785 [0xfa] = MMX_OP2(psubl
),
2786 [0xfb] = MMX_OP2(psubq
),
2787 [0xfc] = MMX_OP2(paddb
),
2788 [0xfd] = MMX_OP2(paddw
),
2789 [0xfe] = MMX_OP2(paddl
),
2792 static const SSEFunc_0_epp sse_op_table2
[3 * 8][2] = {
2793 [0 + 2] = MMX_OP2(psrlw
),
2794 [0 + 4] = MMX_OP2(psraw
),
2795 [0 + 6] = MMX_OP2(psllw
),
2796 [8 + 2] = MMX_OP2(psrld
),
2797 [8 + 4] = MMX_OP2(psrad
),
2798 [8 + 6] = MMX_OP2(pslld
),
2799 [16 + 2] = MMX_OP2(psrlq
),
2800 [16 + 3] = { NULL
, gen_helper_psrldq_xmm
},
2801 [16 + 6] = MMX_OP2(psllq
),
2802 [16 + 7] = { NULL
, gen_helper_pslldq_xmm
},
2805 static const SSEFunc_0_epi sse_op_table3ai
[] = {
2806 gen_helper_cvtsi2ss
,
2810 #ifdef TARGET_X86_64
2811 static const SSEFunc_0_epl sse_op_table3aq
[] = {
2812 gen_helper_cvtsq2ss
,
2817 static const SSEFunc_i_ep sse_op_table3bi
[] = {
2818 gen_helper_cvttss2si
,
2819 gen_helper_cvtss2si
,
2820 gen_helper_cvttsd2si
,
2824 #ifdef TARGET_X86_64
2825 static const SSEFunc_l_ep sse_op_table3bq
[] = {
2826 gen_helper_cvttss2sq
,
2827 gen_helper_cvtss2sq
,
2828 gen_helper_cvttsd2sq
,
2833 static const SSEFunc_0_epp sse_op_table4
[8][4] = {
2844 static const SSEFunc_0_epp sse_op_table5
[256] = {
2845 [0x0c] = gen_helper_pi2fw
,
2846 [0x0d] = gen_helper_pi2fd
,
2847 [0x1c] = gen_helper_pf2iw
,
2848 [0x1d] = gen_helper_pf2id
,
2849 [0x8a] = gen_helper_pfnacc
,
2850 [0x8e] = gen_helper_pfpnacc
,
2851 [0x90] = gen_helper_pfcmpge
,
2852 [0x94] = gen_helper_pfmin
,
2853 [0x96] = gen_helper_pfrcp
,
2854 [0x97] = gen_helper_pfrsqrt
,
2855 [0x9a] = gen_helper_pfsub
,
2856 [0x9e] = gen_helper_pfadd
,
2857 [0xa0] = gen_helper_pfcmpgt
,
2858 [0xa4] = gen_helper_pfmax
,
2859 [0xa6] = gen_helper_movq
, /* pfrcpit1; no need to actually increase precision */
2860 [0xa7] = gen_helper_movq
, /* pfrsqit1 */
2861 [0xaa] = gen_helper_pfsubr
,
2862 [0xae] = gen_helper_pfacc
,
2863 [0xb0] = gen_helper_pfcmpeq
,
2864 [0xb4] = gen_helper_pfmul
,
2865 [0xb6] = gen_helper_movq
, /* pfrcpit2 */
2866 [0xb7] = gen_helper_pmulhrw_mmx
,
2867 [0xbb] = gen_helper_pswapd
,
2868 [0xbf] = gen_helper_pavgb_mmx
/* pavgusb */
2871 struct SSEOpHelper_epp
{
2872 SSEFunc_0_epp op
[2];
2876 struct SSEOpHelper_eppi
{
2877 SSEFunc_0_eppi op
[2];
2881 #define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
2882 #define SSE41_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }
2883 #define SSE42_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }
2884 #define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2885 #define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
2886 CPUID_EXT_PCLMULQDQ }
2887 #define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
2889 static const struct SSEOpHelper_epp sse_op_table6
[256] = {
2890 [0x00] = SSSE3_OP(pshufb
),
2891 [0x01] = SSSE3_OP(phaddw
),
2892 [0x02] = SSSE3_OP(phaddd
),
2893 [0x03] = SSSE3_OP(phaddsw
),
2894 [0x04] = SSSE3_OP(pmaddubsw
),
2895 [0x05] = SSSE3_OP(phsubw
),
2896 [0x06] = SSSE3_OP(phsubd
),
2897 [0x07] = SSSE3_OP(phsubsw
),
2898 [0x08] = SSSE3_OP(psignb
),
2899 [0x09] = SSSE3_OP(psignw
),
2900 [0x0a] = SSSE3_OP(psignd
),
2901 [0x0b] = SSSE3_OP(pmulhrsw
),
2902 [0x10] = SSE41_OP(pblendvb
),
2903 [0x14] = SSE41_OP(blendvps
),
2904 [0x15] = SSE41_OP(blendvpd
),
2905 [0x17] = SSE41_OP(ptest
),
2906 [0x1c] = SSSE3_OP(pabsb
),
2907 [0x1d] = SSSE3_OP(pabsw
),
2908 [0x1e] = SSSE3_OP(pabsd
),
2909 [0x20] = SSE41_OP(pmovsxbw
),
2910 [0x21] = SSE41_OP(pmovsxbd
),
2911 [0x22] = SSE41_OP(pmovsxbq
),
2912 [0x23] = SSE41_OP(pmovsxwd
),
2913 [0x24] = SSE41_OP(pmovsxwq
),
2914 [0x25] = SSE41_OP(pmovsxdq
),
2915 [0x28] = SSE41_OP(pmuldq
),
2916 [0x29] = SSE41_OP(pcmpeqq
),
2917 [0x2a] = SSE41_SPECIAL
, /* movntqda */
2918 [0x2b] = SSE41_OP(packusdw
),
2919 [0x30] = SSE41_OP(pmovzxbw
),
2920 [0x31] = SSE41_OP(pmovzxbd
),
2921 [0x32] = SSE41_OP(pmovzxbq
),
2922 [0x33] = SSE41_OP(pmovzxwd
),
2923 [0x34] = SSE41_OP(pmovzxwq
),
2924 [0x35] = SSE41_OP(pmovzxdq
),
2925 [0x37] = SSE42_OP(pcmpgtq
),
2926 [0x38] = SSE41_OP(pminsb
),
2927 [0x39] = SSE41_OP(pminsd
),
2928 [0x3a] = SSE41_OP(pminuw
),
2929 [0x3b] = SSE41_OP(pminud
),
2930 [0x3c] = SSE41_OP(pmaxsb
),
2931 [0x3d] = SSE41_OP(pmaxsd
),
2932 [0x3e] = SSE41_OP(pmaxuw
),
2933 [0x3f] = SSE41_OP(pmaxud
),
2934 [0x40] = SSE41_OP(pmulld
),
2935 [0x41] = SSE41_OP(phminposuw
),
2936 [0xdb] = AESNI_OP(aesimc
),
2937 [0xdc] = AESNI_OP(aesenc
),
2938 [0xdd] = AESNI_OP(aesenclast
),
2939 [0xde] = AESNI_OP(aesdec
),
2940 [0xdf] = AESNI_OP(aesdeclast
),
2943 static const struct SSEOpHelper_eppi sse_op_table7
[256] = {
2944 [0x08] = SSE41_OP(roundps
),
2945 [0x09] = SSE41_OP(roundpd
),
2946 [0x0a] = SSE41_OP(roundss
),
2947 [0x0b] = SSE41_OP(roundsd
),
2948 [0x0c] = SSE41_OP(blendps
),
2949 [0x0d] = SSE41_OP(blendpd
),
2950 [0x0e] = SSE41_OP(pblendw
),
2951 [0x0f] = SSSE3_OP(palignr
),
2952 [0x14] = SSE41_SPECIAL
, /* pextrb */
2953 [0x15] = SSE41_SPECIAL
, /* pextrw */
2954 [0x16] = SSE41_SPECIAL
, /* pextrd/pextrq */
2955 [0x17] = SSE41_SPECIAL
, /* extractps */
2956 [0x20] = SSE41_SPECIAL
, /* pinsrb */
2957 [0x21] = SSE41_SPECIAL
, /* insertps */
2958 [0x22] = SSE41_SPECIAL
, /* pinsrd/pinsrq */
2959 [0x40] = SSE41_OP(dpps
),
2960 [0x41] = SSE41_OP(dppd
),
2961 [0x42] = SSE41_OP(mpsadbw
),
2962 [0x44] = PCLMULQDQ_OP(pclmulqdq
),
2963 [0x60] = SSE42_OP(pcmpestrm
),
2964 [0x61] = SSE42_OP(pcmpestri
),
2965 [0x62] = SSE42_OP(pcmpistrm
),
2966 [0x63] = SSE42_OP(pcmpistri
),
2967 [0xdf] = AESNI_OP(aeskeygenassist
),
2970 static void gen_sse(CPUX86State
*env
, DisasContext
*s
, int b
,
2971 target_ulong pc_start
, int rex_r
)
2973 int b1
, op1_offset
, op2_offset
, is_xmm
, val
;
2974 int modrm
, mod
, rm
, reg
;
2975 SSEFunc_0_epp sse_fn_epp
;
2976 SSEFunc_0_eppi sse_fn_eppi
;
2977 SSEFunc_0_ppi sse_fn_ppi
;
2978 SSEFunc_0_eppt sse_fn_eppt
;
2982 if (s
->prefix
& PREFIX_DATA
)
2984 else if (s
->prefix
& PREFIX_REPZ
)
2986 else if (s
->prefix
& PREFIX_REPNZ
)
2990 sse_fn_epp
= sse_op_table1
[b
][b1
];
2994 if ((b
<= 0x5f && b
>= 0x10) || b
== 0xc6 || b
== 0xc2) {
3004 /* simple MMX/SSE operation */
3005 if (s
->flags
& HF_TS_MASK
) {
3006 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
3009 if (s
->flags
& HF_EM_MASK
) {
3011 gen_exception(s
, EXCP06_ILLOP
, pc_start
- s
->cs_base
);
3014 if (is_xmm
&& !(s
->flags
& HF_OSFXSR_MASK
))
3015 if ((b
!= 0x38 && b
!= 0x3a) || (s
->prefix
& PREFIX_DATA
))
3018 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_3DNOW
))
3021 gen_helper_emms(cpu_env
);
3026 gen_helper_emms(cpu_env
);
3029 /* prepare MMX state (XXX: optimize by storing fptt and fptags in
3030 the static cpu state) */
3032 gen_helper_enter_mmx(cpu_env
);
3035 modrm
= cpu_ldub_code(env
, s
->pc
++);
3036 reg
= ((modrm
>> 3) & 7);
3039 mod
= (modrm
>> 6) & 3;
3040 if (sse_fn_epp
== SSE_SPECIAL
) {
3043 case 0x0e7: /* movntq */
3046 gen_lea_modrm(env
, s
, modrm
);
3047 gen_stq_env_A0(s
, offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3049 case 0x1e7: /* movntdq */
3050 case 0x02b: /* movntps */
3051 case 0x12b: /* movntps */
3054 gen_lea_modrm(env
, s
, modrm
);
3055 gen_sto_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3057 case 0x3f0: /* lddqu */
3060 gen_lea_modrm(env
, s
, modrm
);
3061 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3063 case 0x22b: /* movntss */
3064 case 0x32b: /* movntsd */
3067 gen_lea_modrm(env
, s
, modrm
);
3069 gen_stq_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3071 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
3072 xmm_regs
[reg
].XMM_L(0)));
3073 gen_op_st_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
3076 case 0x6e: /* movd mm, ea */
3077 #ifdef TARGET_X86_64
3078 if (s
->dflag
== MO_64
) {
3079 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 0);
3080 tcg_gen_st_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3084 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 0);
3085 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3086 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3087 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
3088 gen_helper_movl_mm_T0_mmx(cpu_ptr0
, cpu_tmp2_i32
);
3091 case 0x16e: /* movd xmm, ea */
3092 #ifdef TARGET_X86_64
3093 if (s
->dflag
== MO_64
) {
3094 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 0);
3095 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3096 offsetof(CPUX86State
,xmm_regs
[reg
]));
3097 gen_helper_movq_mm_T0_xmm(cpu_ptr0
, cpu_T
[0]);
3101 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 0);
3102 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3103 offsetof(CPUX86State
,xmm_regs
[reg
]));
3104 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
3105 gen_helper_movl_mm_T0_xmm(cpu_ptr0
, cpu_tmp2_i32
);
3108 case 0x6f: /* movq mm, ea */
3110 gen_lea_modrm(env
, s
, modrm
);
3111 gen_ldq_env_A0(s
, offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3114 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
,
3115 offsetof(CPUX86State
,fpregs
[rm
].mmx
));
3116 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
,
3117 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3120 case 0x010: /* movups */
3121 case 0x110: /* movupd */
3122 case 0x028: /* movaps */
3123 case 0x128: /* movapd */
3124 case 0x16f: /* movdqa xmm, ea */
3125 case 0x26f: /* movdqu xmm, ea */
3127 gen_lea_modrm(env
, s
, modrm
);
3128 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3130 rm
= (modrm
& 7) | REX_B(s
);
3131 gen_op_movo(offsetof(CPUX86State
,xmm_regs
[reg
]),
3132 offsetof(CPUX86State
,xmm_regs
[rm
]));
3135 case 0x210: /* movss xmm, ea */
3137 gen_lea_modrm(env
, s
, modrm
);
3138 gen_op_ld_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
3139 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3140 tcg_gen_movi_tl(cpu_T
[0], 0);
3141 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(1)));
3142 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)));
3143 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)));
3145 rm
= (modrm
& 7) | REX_B(s
);
3146 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)),
3147 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(0)));
3150 case 0x310: /* movsd xmm, ea */
3152 gen_lea_modrm(env
, s
, modrm
);
3153 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3154 xmm_regs
[reg
].XMM_Q(0)));
3155 tcg_gen_movi_tl(cpu_T
[0], 0);
3156 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)));
3157 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)));
3159 rm
= (modrm
& 7) | REX_B(s
);
3160 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3161 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3164 case 0x012: /* movlps */
3165 case 0x112: /* movlpd */
3167 gen_lea_modrm(env
, s
, modrm
);
3168 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3169 xmm_regs
[reg
].XMM_Q(0)));
3172 rm
= (modrm
& 7) | REX_B(s
);
3173 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3174 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(1)));
3177 case 0x212: /* movsldup */
3179 gen_lea_modrm(env
, s
, modrm
);
3180 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3182 rm
= (modrm
& 7) | REX_B(s
);
3183 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)),
3184 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(0)));
3185 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)),
3186 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(2)));
3188 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(1)),
3189 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3190 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)),
3191 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)));
3193 case 0x312: /* movddup */
3195 gen_lea_modrm(env
, s
, modrm
);
3196 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3197 xmm_regs
[reg
].XMM_Q(0)));
3199 rm
= (modrm
& 7) | REX_B(s
);
3200 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3201 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3203 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(1)),
3204 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)));
3206 case 0x016: /* movhps */
3207 case 0x116: /* movhpd */
3209 gen_lea_modrm(env
, s
, modrm
);
3210 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3211 xmm_regs
[reg
].XMM_Q(1)));
3214 rm
= (modrm
& 7) | REX_B(s
);
3215 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(1)),
3216 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3219 case 0x216: /* movshdup */
3221 gen_lea_modrm(env
, s
, modrm
);
3222 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3224 rm
= (modrm
& 7) | REX_B(s
);
3225 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(1)),
3226 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(1)));
3227 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)),
3228 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(3)));
3230 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)),
3231 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(1)));
3232 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)),
3233 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)));
3238 int bit_index
, field_length
;
3240 if (b1
== 1 && reg
!= 0)
3242 field_length
= cpu_ldub_code(env
, s
->pc
++) & 0x3F;
3243 bit_index
= cpu_ldub_code(env
, s
->pc
++) & 0x3F;
3244 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3245 offsetof(CPUX86State
,xmm_regs
[reg
]));
3247 gen_helper_extrq_i(cpu_env
, cpu_ptr0
,
3248 tcg_const_i32(bit_index
),
3249 tcg_const_i32(field_length
));
3251 gen_helper_insertq_i(cpu_env
, cpu_ptr0
,
3252 tcg_const_i32(bit_index
),
3253 tcg_const_i32(field_length
));
3256 case 0x7e: /* movd ea, mm */
3257 #ifdef TARGET_X86_64
3258 if (s
->dflag
== MO_64
) {
3259 tcg_gen_ld_i64(cpu_T
[0], cpu_env
,
3260 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3261 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 1);
3265 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
,
3266 offsetof(CPUX86State
,fpregs
[reg
].mmx
.MMX_L(0)));
3267 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 1);
3270 case 0x17e: /* movd ea, xmm */
3271 #ifdef TARGET_X86_64
3272 if (s
->dflag
== MO_64
) {
3273 tcg_gen_ld_i64(cpu_T
[0], cpu_env
,
3274 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)));
3275 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 1);
3279 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
,
3280 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3281 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 1);
3284 case 0x27e: /* movq xmm, ea */
3286 gen_lea_modrm(env
, s
, modrm
);
3287 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3288 xmm_regs
[reg
].XMM_Q(0)));
3290 rm
= (modrm
& 7) | REX_B(s
);
3291 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3292 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3294 gen_op_movq_env_0(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(1)));
3296 case 0x7f: /* movq ea, mm */
3298 gen_lea_modrm(env
, s
, modrm
);
3299 gen_stq_env_A0(s
, offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3302 gen_op_movq(offsetof(CPUX86State
,fpregs
[rm
].mmx
),
3303 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3306 case 0x011: /* movups */
3307 case 0x111: /* movupd */
3308 case 0x029: /* movaps */
3309 case 0x129: /* movapd */
3310 case 0x17f: /* movdqa ea, xmm */
3311 case 0x27f: /* movdqu ea, xmm */
3313 gen_lea_modrm(env
, s
, modrm
);
3314 gen_sto_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3316 rm
= (modrm
& 7) | REX_B(s
);
3317 gen_op_movo(offsetof(CPUX86State
,xmm_regs
[rm
]),
3318 offsetof(CPUX86State
,xmm_regs
[reg
]));
3321 case 0x211: /* movss ea, xmm */
3323 gen_lea_modrm(env
, s
, modrm
);
3324 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3325 gen_op_st_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
3327 rm
= (modrm
& 7) | REX_B(s
);
3328 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(0)),
3329 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3332 case 0x311: /* movsd ea, xmm */
3334 gen_lea_modrm(env
, s
, modrm
);
3335 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3336 xmm_regs
[reg
].XMM_Q(0)));
3338 rm
= (modrm
& 7) | REX_B(s
);
3339 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)),
3340 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)));
3343 case 0x013: /* movlps */
3344 case 0x113: /* movlpd */
3346 gen_lea_modrm(env
, s
, modrm
);
3347 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3348 xmm_regs
[reg
].XMM_Q(0)));
3353 case 0x017: /* movhps */
3354 case 0x117: /* movhpd */
3356 gen_lea_modrm(env
, s
, modrm
);
3357 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3358 xmm_regs
[reg
].XMM_Q(1)));
3363 case 0x71: /* shift mm, im */
3366 case 0x171: /* shift xmm, im */
3372 val
= cpu_ldub_code(env
, s
->pc
++);
3374 tcg_gen_movi_tl(cpu_T
[0], val
);
3375 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_t0
.XMM_L(0)));
3376 tcg_gen_movi_tl(cpu_T
[0], 0);
3377 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_t0
.XMM_L(1)));
3378 op1_offset
= offsetof(CPUX86State
,xmm_t0
);
3380 tcg_gen_movi_tl(cpu_T
[0], val
);
3381 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,mmx_t0
.MMX_L(0)));
3382 tcg_gen_movi_tl(cpu_T
[0], 0);
3383 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,mmx_t0
.MMX_L(1)));
3384 op1_offset
= offsetof(CPUX86State
,mmx_t0
);
3386 sse_fn_epp
= sse_op_table2
[((b
- 1) & 3) * 8 +
3387 (((modrm
>> 3)) & 7)][b1
];
3392 rm
= (modrm
& 7) | REX_B(s
);
3393 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
3396 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
3398 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op2_offset
);
3399 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op1_offset
);
3400 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3402 case 0x050: /* movmskps */
3403 rm
= (modrm
& 7) | REX_B(s
);
3404 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3405 offsetof(CPUX86State
,xmm_regs
[rm
]));
3406 gen_helper_movmskps(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3407 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp2_i32
);
3409 case 0x150: /* movmskpd */
3410 rm
= (modrm
& 7) | REX_B(s
);
3411 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3412 offsetof(CPUX86State
,xmm_regs
[rm
]));
3413 gen_helper_movmskpd(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3414 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp2_i32
);
3416 case 0x02a: /* cvtpi2ps */
3417 case 0x12a: /* cvtpi2pd */
3418 gen_helper_enter_mmx(cpu_env
);
3420 gen_lea_modrm(env
, s
, modrm
);
3421 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
3422 gen_ldq_env_A0(s
, op2_offset
);
3425 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
3427 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
3428 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
3429 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
3432 gen_helper_cvtpi2ps(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3436 gen_helper_cvtpi2pd(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3440 case 0x22a: /* cvtsi2ss */
3441 case 0x32a: /* cvtsi2sd */
3442 ot
= mo_64_32(s
->dflag
);
3443 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3444 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
3445 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
3447 SSEFunc_0_epi sse_fn_epi
= sse_op_table3ai
[(b
>> 8) & 1];
3448 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
3449 sse_fn_epi(cpu_env
, cpu_ptr0
, cpu_tmp2_i32
);
3451 #ifdef TARGET_X86_64
3452 SSEFunc_0_epl sse_fn_epl
= sse_op_table3aq
[(b
>> 8) & 1];
3453 sse_fn_epl(cpu_env
, cpu_ptr0
, cpu_T
[0]);
3459 case 0x02c: /* cvttps2pi */
3460 case 0x12c: /* cvttpd2pi */
3461 case 0x02d: /* cvtps2pi */
3462 case 0x12d: /* cvtpd2pi */
3463 gen_helper_enter_mmx(cpu_env
);
3465 gen_lea_modrm(env
, s
, modrm
);
3466 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
3467 gen_ldo_env_A0(s
, op2_offset
);
3469 rm
= (modrm
& 7) | REX_B(s
);
3470 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
3472 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
& 7].mmx
);
3473 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
3474 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
3477 gen_helper_cvttps2pi(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3480 gen_helper_cvttpd2pi(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3483 gen_helper_cvtps2pi(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3486 gen_helper_cvtpd2pi(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3490 case 0x22c: /* cvttss2si */
3491 case 0x32c: /* cvttsd2si */
3492 case 0x22d: /* cvtss2si */
3493 case 0x32d: /* cvtsd2si */
3494 ot
= mo_64_32(s
->dflag
);
3496 gen_lea_modrm(env
, s
, modrm
);
3498 gen_ldq_env_A0(s
, offsetof(CPUX86State
, xmm_t0
.XMM_Q(0)));
3500 gen_op_ld_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
3501 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_t0
.XMM_L(0)));
3503 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
3505 rm
= (modrm
& 7) | REX_B(s
);
3506 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
3508 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op2_offset
);
3510 SSEFunc_i_ep sse_fn_i_ep
=
3511 sse_op_table3bi
[((b
>> 7) & 2) | (b
& 1)];
3512 sse_fn_i_ep(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3513 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
3515 #ifdef TARGET_X86_64
3516 SSEFunc_l_ep sse_fn_l_ep
=
3517 sse_op_table3bq
[((b
>> 7) & 2) | (b
& 1)];
3518 sse_fn_l_ep(cpu_T
[0], cpu_env
, cpu_ptr0
);
3523 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3525 case 0xc4: /* pinsrw */
3528 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
3529 val
= cpu_ldub_code(env
, s
->pc
++);
3532 tcg_gen_st16_tl(cpu_T
[0], cpu_env
,
3533 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_W(val
)));
3536 tcg_gen_st16_tl(cpu_T
[0], cpu_env
,
3537 offsetof(CPUX86State
,fpregs
[reg
].mmx
.MMX_W(val
)));
3540 case 0xc5: /* pextrw */
3544 ot
= mo_64_32(s
->dflag
);
3545 val
= cpu_ldub_code(env
, s
->pc
++);
3548 rm
= (modrm
& 7) | REX_B(s
);
3549 tcg_gen_ld16u_tl(cpu_T
[0], cpu_env
,
3550 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_W(val
)));
3554 tcg_gen_ld16u_tl(cpu_T
[0], cpu_env
,
3555 offsetof(CPUX86State
,fpregs
[rm
].mmx
.MMX_W(val
)));
3557 reg
= ((modrm
>> 3) & 7) | rex_r
;
3558 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3560 case 0x1d6: /* movq ea, xmm */
3562 gen_lea_modrm(env
, s
, modrm
);
3563 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3564 xmm_regs
[reg
].XMM_Q(0)));
3566 rm
= (modrm
& 7) | REX_B(s
);
3567 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)),
3568 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)));
3569 gen_op_movq_env_0(offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(1)));
3572 case 0x2d6: /* movq2dq */
3573 gen_helper_enter_mmx(cpu_env
);
3575 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3576 offsetof(CPUX86State
,fpregs
[rm
].mmx
));
3577 gen_op_movq_env_0(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(1)));
3579 case 0x3d6: /* movdq2q */
3580 gen_helper_enter_mmx(cpu_env
);
3581 rm
= (modrm
& 7) | REX_B(s
);
3582 gen_op_movq(offsetof(CPUX86State
,fpregs
[reg
& 7].mmx
),
3583 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3585 case 0xd7: /* pmovmskb */
3590 rm
= (modrm
& 7) | REX_B(s
);
3591 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, offsetof(CPUX86State
,xmm_regs
[rm
]));
3592 gen_helper_pmovmskb_xmm(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3595 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, offsetof(CPUX86State
,fpregs
[rm
].mmx
));
3596 gen_helper_pmovmskb_mmx(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3598 reg
= ((modrm
>> 3) & 7) | rex_r
;
3599 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp2_i32
);
3605 if ((b
& 0xf0) == 0xf0) {
3608 modrm
= cpu_ldub_code(env
, s
->pc
++);
3610 reg
= ((modrm
>> 3) & 7) | rex_r
;
3611 mod
= (modrm
>> 6) & 3;
3616 sse_fn_epp
= sse_op_table6
[b
].op
[b1
];
3620 if (!(s
->cpuid_ext_features
& sse_op_table6
[b
].ext_mask
))
3624 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
3626 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
| REX_B(s
)]);
3628 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
3629 gen_lea_modrm(env
, s
, modrm
);
3631 case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
3632 case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
3633 case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3634 gen_ldq_env_A0(s
, op2_offset
+
3635 offsetof(XMMReg
, XMM_Q(0)));
3637 case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
3638 case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3639 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
3640 s
->mem_index
, MO_LEUL
);
3641 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
, op2_offset
+
3642 offsetof(XMMReg
, XMM_L(0)));
3644 case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3645 tcg_gen_qemu_ld_tl(cpu_tmp0
, cpu_A0
,
3646 s
->mem_index
, MO_LEUW
);
3647 tcg_gen_st16_tl(cpu_tmp0
, cpu_env
, op2_offset
+
3648 offsetof(XMMReg
, XMM_W(0)));
3650 case 0x2a: /* movntqda */
3651 gen_ldo_env_A0(s
, op1_offset
);
3654 gen_ldo_env_A0(s
, op2_offset
);
3658 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
].mmx
);
3660 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
3662 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
3663 gen_lea_modrm(env
, s
, modrm
);
3664 gen_ldq_env_A0(s
, op2_offset
);
3667 if (sse_fn_epp
== SSE_SPECIAL
) {
3671 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
3672 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
3673 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3676 set_cc_op(s
, CC_OP_EFLAGS
);
3683 /* Various integer extensions at 0f 38 f[0-f]. */
3684 b
= modrm
| (b1
<< 8);
3685 modrm
= cpu_ldub_code(env
, s
->pc
++);
3686 reg
= ((modrm
>> 3) & 7) | rex_r
;
3689 case 0x3f0: /* crc32 Gd,Eb */
3690 case 0x3f1: /* crc32 Gd,Ey */
3692 if (!(s
->cpuid_ext_features
& CPUID_EXT_SSE42
)) {
3695 if ((b
& 0xff) == 0xf0) {
3697 } else if (s
->dflag
!= MO_64
) {
3698 ot
= (s
->prefix
& PREFIX_DATA
? MO_16
: MO_32
);
3703 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_regs
[reg
]);
3704 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3705 gen_helper_crc32(cpu_T
[0], cpu_tmp2_i32
,
3706 cpu_T
[0], tcg_const_i32(8 << ot
));
3708 ot
= mo_64_32(s
->dflag
);
3709 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3712 case 0x1f0: /* crc32 or movbe */
3714 /* For these insns, the f3 prefix is supposed to have priority
3715 over the 66 prefix, but that's not what we implement above
3717 if (s
->prefix
& PREFIX_REPNZ
) {
3721 case 0x0f0: /* movbe Gy,My */
3722 case 0x0f1: /* movbe My,Gy */
3723 if (!(s
->cpuid_ext_features
& CPUID_EXT_MOVBE
)) {
3726 if (s
->dflag
!= MO_64
) {
3727 ot
= (s
->prefix
& PREFIX_DATA
? MO_16
: MO_32
);
3732 gen_lea_modrm(env
, s
, modrm
);
3734 tcg_gen_qemu_ld_tl(cpu_T
[0], cpu_A0
,
3735 s
->mem_index
, ot
| MO_BE
);
3736 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3738 tcg_gen_qemu_st_tl(cpu_regs
[reg
], cpu_A0
,
3739 s
->mem_index
, ot
| MO_BE
);
3743 case 0x0f2: /* andn Gy, By, Ey */
3744 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)
3745 || !(s
->prefix
& PREFIX_VEX
)
3749 ot
= mo_64_32(s
->dflag
);
3750 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3751 tcg_gen_andc_tl(cpu_T
[0], cpu_regs
[s
->vex_v
], cpu_T
[0]);
3752 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3753 gen_op_update1_cc();
3754 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
3757 case 0x0f7: /* bextr Gy, Ey, By */
3758 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)
3759 || !(s
->prefix
& PREFIX_VEX
)
3763 ot
= mo_64_32(s
->dflag
);
3767 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3768 /* Extract START, and shift the operand.
3769 Shifts larger than operand size get zeros. */
3770 tcg_gen_ext8u_tl(cpu_A0
, cpu_regs
[s
->vex_v
]);
3771 tcg_gen_shr_tl(cpu_T
[0], cpu_T
[0], cpu_A0
);
3773 bound
= tcg_const_tl(ot
== MO_64
? 63 : 31);
3774 zero
= tcg_const_tl(0);
3775 tcg_gen_movcond_tl(TCG_COND_LEU
, cpu_T
[0], cpu_A0
, bound
,
3777 tcg_temp_free(zero
);
3779 /* Extract the LEN into a mask. Lengths larger than
3780 operand size get all ones. */
3781 tcg_gen_shri_tl(cpu_A0
, cpu_regs
[s
->vex_v
], 8);
3782 tcg_gen_ext8u_tl(cpu_A0
, cpu_A0
);
3783 tcg_gen_movcond_tl(TCG_COND_LEU
, cpu_A0
, cpu_A0
, bound
,
3785 tcg_temp_free(bound
);
3786 tcg_gen_movi_tl(cpu_T
[1], 1);
3787 tcg_gen_shl_tl(cpu_T
[1], cpu_T
[1], cpu_A0
);
3788 tcg_gen_subi_tl(cpu_T
[1], cpu_T
[1], 1);
3789 tcg_gen_and_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
3791 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3792 gen_op_update1_cc();
3793 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
3797 case 0x0f5: /* bzhi Gy, Ey, By */
3798 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3799 || !(s
->prefix
& PREFIX_VEX
)
3803 ot
= mo_64_32(s
->dflag
);
3804 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3805 tcg_gen_ext8u_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3807 TCGv bound
= tcg_const_tl(ot
== MO_64
? 63 : 31);
3808 /* Note that since we're using BMILG (in order to get O
3809 cleared) we need to store the inverse into C. */
3810 tcg_gen_setcond_tl(TCG_COND_LT
, cpu_cc_src
,
3812 tcg_gen_movcond_tl(TCG_COND_GT
, cpu_T
[1], cpu_T
[1],
3813 bound
, bound
, cpu_T
[1]);
3814 tcg_temp_free(bound
);
3816 tcg_gen_movi_tl(cpu_A0
, -1);
3817 tcg_gen_shl_tl(cpu_A0
, cpu_A0
, cpu_T
[1]);
3818 tcg_gen_andc_tl(cpu_T
[0], cpu_T
[0], cpu_A0
);
3819 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3820 gen_op_update1_cc();
3821 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
3824 case 0x3f6: /* mulx By, Gy, rdx, Ey */
3825 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3826 || !(s
->prefix
& PREFIX_VEX
)
3830 ot
= mo_64_32(s
->dflag
);
3831 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3834 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
3835 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_regs
[R_EDX
]);
3836 tcg_gen_mulu2_i32(cpu_tmp2_i32
, cpu_tmp3_i32
,
3837 cpu_tmp2_i32
, cpu_tmp3_i32
);
3838 tcg_gen_extu_i32_tl(cpu_regs
[s
->vex_v
], cpu_tmp2_i32
);
3839 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp3_i32
);
3841 #ifdef TARGET_X86_64
3843 tcg_gen_mulu2_i64(cpu_regs
[s
->vex_v
], cpu_regs
[reg
],
3844 cpu_T
[0], cpu_regs
[R_EDX
]);
3850 case 0x3f5: /* pdep Gy, By, Ey */
3851 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3852 || !(s
->prefix
& PREFIX_VEX
)
3856 ot
= mo_64_32(s
->dflag
);
3857 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3858 /* Note that by zero-extending the mask operand, we
3859 automatically handle zero-extending the result. */
3861 tcg_gen_mov_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3863 tcg_gen_ext32u_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3865 gen_helper_pdep(cpu_regs
[reg
], cpu_T
[0], cpu_T
[1]);
3868 case 0x2f5: /* pext Gy, By, Ey */
3869 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3870 || !(s
->prefix
& PREFIX_VEX
)
3874 ot
= mo_64_32(s
->dflag
);
3875 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3876 /* Note that by zero-extending the mask operand, we
3877 automatically handle zero-extending the result. */
3879 tcg_gen_mov_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3881 tcg_gen_ext32u_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3883 gen_helper_pext(cpu_regs
[reg
], cpu_T
[0], cpu_T
[1]);
3886 case 0x1f6: /* adcx Gy, Ey */
3887 case 0x2f6: /* adox Gy, Ey */
3888 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_ADX
)) {
3891 TCGv carry_in
, carry_out
, zero
;
3894 ot
= mo_64_32(s
->dflag
);
3895 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3897 /* Re-use the carry-out from a previous round. */
3898 TCGV_UNUSED(carry_in
);
3899 carry_out
= (b
== 0x1f6 ? cpu_cc_dst
: cpu_cc_src2
);
3903 carry_in
= cpu_cc_dst
;
3904 end_op
= CC_OP_ADCX
;
3906 end_op
= CC_OP_ADCOX
;
3911 end_op
= CC_OP_ADCOX
;
3913 carry_in
= cpu_cc_src2
;
3914 end_op
= CC_OP_ADOX
;
3918 end_op
= CC_OP_ADCOX
;
3919 carry_in
= carry_out
;
3922 end_op
= (b
== 0x1f6 ? CC_OP_ADCX
: CC_OP_ADOX
);
3925 /* If we can't reuse carry-out, get it out of EFLAGS. */
3926 if (TCGV_IS_UNUSED(carry_in
)) {
3927 if (s
->cc_op
!= CC_OP_ADCX
&& s
->cc_op
!= CC_OP_ADOX
) {
3928 gen_compute_eflags(s
);
3930 carry_in
= cpu_tmp0
;
3931 tcg_gen_shri_tl(carry_in
, cpu_cc_src
,
3932 ctz32(b
== 0x1f6 ? CC_C
: CC_O
));
3933 tcg_gen_andi_tl(carry_in
, carry_in
, 1);
3937 #ifdef TARGET_X86_64
3939 /* If we know TL is 64-bit, and we want a 32-bit
3940 result, just do everything in 64-bit arithmetic. */
3941 tcg_gen_ext32u_i64(cpu_regs
[reg
], cpu_regs
[reg
]);
3942 tcg_gen_ext32u_i64(cpu_T
[0], cpu_T
[0]);
3943 tcg_gen_add_i64(cpu_T
[0], cpu_T
[0], cpu_regs
[reg
]);
3944 tcg_gen_add_i64(cpu_T
[0], cpu_T
[0], carry_in
);
3945 tcg_gen_ext32u_i64(cpu_regs
[reg
], cpu_T
[0]);
3946 tcg_gen_shri_i64(carry_out
, cpu_T
[0], 32);
3950 /* Otherwise compute the carry-out in two steps. */
3951 zero
= tcg_const_tl(0);
3952 tcg_gen_add2_tl(cpu_T
[0], carry_out
,
3955 tcg_gen_add2_tl(cpu_regs
[reg
], carry_out
,
3956 cpu_regs
[reg
], carry_out
,
3958 tcg_temp_free(zero
);
3961 set_cc_op(s
, end_op
);
3965 case 0x1f7: /* shlx Gy, Ey, By */
3966 case 0x2f7: /* sarx Gy, Ey, By */
3967 case 0x3f7: /* shrx Gy, Ey, By */
3968 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3969 || !(s
->prefix
& PREFIX_VEX
)
3973 ot
= mo_64_32(s
->dflag
);
3974 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3976 tcg_gen_andi_tl(cpu_T
[1], cpu_regs
[s
->vex_v
], 63);
3978 tcg_gen_andi_tl(cpu_T
[1], cpu_regs
[s
->vex_v
], 31);
3981 tcg_gen_shl_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
3982 } else if (b
== 0x2f7) {
3984 tcg_gen_ext32s_tl(cpu_T
[0], cpu_T
[0]);
3986 tcg_gen_sar_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
3989 tcg_gen_ext32u_tl(cpu_T
[0], cpu_T
[0]);
3991 tcg_gen_shr_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
3993 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3999 case 0x3f3: /* Group 17 */
4000 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)
4001 || !(s
->prefix
& PREFIX_VEX
)
4005 ot
= mo_64_32(s
->dflag
);
4006 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
4009 case 1: /* blsr By,Ey */
4010 tcg_gen_neg_tl(cpu_T
[1], cpu_T
[0]);
4011 tcg_gen_and_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4012 gen_op_mov_reg_v(ot
, s
->vex_v
, cpu_T
[0]);
4013 gen_op_update2_cc();
4014 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
4017 case 2: /* blsmsk By,Ey */
4018 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[0]);
4019 tcg_gen_subi_tl(cpu_T
[0], cpu_T
[0], 1);
4020 tcg_gen_xor_tl(cpu_T
[0], cpu_T
[0], cpu_cc_src
);
4021 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4022 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
4025 case 3: /* blsi By, Ey */
4026 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[0]);
4027 tcg_gen_subi_tl(cpu_T
[0], cpu_T
[0], 1);
4028 tcg_gen_and_tl(cpu_T
[0], cpu_T
[0], cpu_cc_src
);
4029 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4030 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
4046 modrm
= cpu_ldub_code(env
, s
->pc
++);
4048 reg
= ((modrm
>> 3) & 7) | rex_r
;
4049 mod
= (modrm
>> 6) & 3;
4054 sse_fn_eppi
= sse_op_table7
[b
].op
[b1
];
4058 if (!(s
->cpuid_ext_features
& sse_op_table7
[b
].ext_mask
))
4061 if (sse_fn_eppi
== SSE_SPECIAL
) {
4062 ot
= mo_64_32(s
->dflag
);
4063 rm
= (modrm
& 7) | REX_B(s
);
4065 gen_lea_modrm(env
, s
, modrm
);
4066 reg
= ((modrm
>> 3) & 7) | rex_r
;
4067 val
= cpu_ldub_code(env
, s
->pc
++);
4069 case 0x14: /* pextrb */
4070 tcg_gen_ld8u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
4071 xmm_regs
[reg
].XMM_B(val
& 15)));
4073 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4075 tcg_gen_qemu_st_tl(cpu_T
[0], cpu_A0
,
4076 s
->mem_index
, MO_UB
);
4079 case 0x15: /* pextrw */
4080 tcg_gen_ld16u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
4081 xmm_regs
[reg
].XMM_W(val
& 7)));
4083 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4085 tcg_gen_qemu_st_tl(cpu_T
[0], cpu_A0
,
4086 s
->mem_index
, MO_LEUW
);
4090 if (ot
== MO_32
) { /* pextrd */
4091 tcg_gen_ld_i32(cpu_tmp2_i32
, cpu_env
,
4092 offsetof(CPUX86State
,
4093 xmm_regs
[reg
].XMM_L(val
& 3)));
4095 tcg_gen_extu_i32_tl(cpu_regs
[rm
], cpu_tmp2_i32
);
4097 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
4098 s
->mem_index
, MO_LEUL
);
4100 } else { /* pextrq */
4101 #ifdef TARGET_X86_64
4102 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
,
4103 offsetof(CPUX86State
,
4104 xmm_regs
[reg
].XMM_Q(val
& 1)));
4106 tcg_gen_mov_i64(cpu_regs
[rm
], cpu_tmp1_i64
);
4108 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
,
4109 s
->mem_index
, MO_LEQ
);
4116 case 0x17: /* extractps */
4117 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
4118 xmm_regs
[reg
].XMM_L(val
& 3)));
4120 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4122 tcg_gen_qemu_st_tl(cpu_T
[0], cpu_A0
,
4123 s
->mem_index
, MO_LEUL
);
4126 case 0x20: /* pinsrb */
4128 gen_op_mov_v_reg(MO_32
, cpu_T
[0], rm
);
4130 tcg_gen_qemu_ld_tl(cpu_T
[0], cpu_A0
,
4131 s
->mem_index
, MO_UB
);
4133 tcg_gen_st8_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
4134 xmm_regs
[reg
].XMM_B(val
& 15)));
4136 case 0x21: /* insertps */
4138 tcg_gen_ld_i32(cpu_tmp2_i32
, cpu_env
,
4139 offsetof(CPUX86State
,xmm_regs
[rm
]
4140 .XMM_L((val
>> 6) & 3)));
4142 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
4143 s
->mem_index
, MO_LEUL
);
4145 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
,
4146 offsetof(CPUX86State
,xmm_regs
[reg
]
4147 .XMM_L((val
>> 4) & 3)));
4149 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4150 cpu_env
, offsetof(CPUX86State
,
4151 xmm_regs
[reg
].XMM_L(0)));
4153 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4154 cpu_env
, offsetof(CPUX86State
,
4155 xmm_regs
[reg
].XMM_L(1)));
4157 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4158 cpu_env
, offsetof(CPUX86State
,
4159 xmm_regs
[reg
].XMM_L(2)));
4161 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4162 cpu_env
, offsetof(CPUX86State
,
4163 xmm_regs
[reg
].XMM_L(3)));
4166 if (ot
== MO_32
) { /* pinsrd */
4168 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_regs
[rm
]);
4170 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
4171 s
->mem_index
, MO_LEUL
);
4173 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
,
4174 offsetof(CPUX86State
,
4175 xmm_regs
[reg
].XMM_L(val
& 3)));
4176 } else { /* pinsrq */
4177 #ifdef TARGET_X86_64
4179 gen_op_mov_v_reg(ot
, cpu_tmp1_i64
, rm
);
4181 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
,
4182 s
->mem_index
, MO_LEQ
);
4184 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
,
4185 offsetof(CPUX86State
,
4186 xmm_regs
[reg
].XMM_Q(val
& 1)));
4197 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
4199 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
| REX_B(s
)]);
4201 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
4202 gen_lea_modrm(env
, s
, modrm
);
4203 gen_ldo_env_A0(s
, op2_offset
);
4206 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
].mmx
);
4208 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
4210 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
4211 gen_lea_modrm(env
, s
, modrm
);
4212 gen_ldq_env_A0(s
, op2_offset
);
4215 val
= cpu_ldub_code(env
, s
->pc
++);
4217 if ((b
& 0xfc) == 0x60) { /* pcmpXstrX */
4218 set_cc_op(s
, CC_OP_EFLAGS
);
4220 if (s
->dflag
== MO_64
) {
4221 /* The helper must use entire 64-bit gp registers */
4226 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4227 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4228 sse_fn_eppi(cpu_env
, cpu_ptr0
, cpu_ptr1
, tcg_const_i32(val
));
4232 /* Various integer extensions at 0f 3a f[0-f]. */
4233 b
= modrm
| (b1
<< 8);
4234 modrm
= cpu_ldub_code(env
, s
->pc
++);
4235 reg
= ((modrm
>> 3) & 7) | rex_r
;
4238 case 0x3f0: /* rorx Gy,Ey, Ib */
4239 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
4240 || !(s
->prefix
& PREFIX_VEX
)
4244 ot
= mo_64_32(s
->dflag
);
4245 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
4246 b
= cpu_ldub_code(env
, s
->pc
++);
4248 tcg_gen_rotri_tl(cpu_T
[0], cpu_T
[0], b
& 63);
4250 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4251 tcg_gen_rotri_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, b
& 31);
4252 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
4254 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
4266 /* generic MMX or SSE operation */
4268 case 0x70: /* pshufx insn */
4269 case 0xc6: /* pshufx insn */
4270 case 0xc2: /* compare insns */
4277 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
4281 gen_lea_modrm(env
, s
, modrm
);
4282 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
4288 /* Most sse scalar operations. */
4291 } else if (b1
== 3) {
4296 case 0x2e: /* ucomis[sd] */
4297 case 0x2f: /* comis[sd] */
4309 gen_op_ld_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
4310 tcg_gen_st32_tl(cpu_T
[0], cpu_env
,
4311 offsetof(CPUX86State
,xmm_t0
.XMM_L(0)));
4315 gen_ldq_env_A0(s
, offsetof(CPUX86State
, xmm_t0
.XMM_D(0)));
4318 /* 128 bit access */
4319 gen_ldo_env_A0(s
, op2_offset
);
4323 rm
= (modrm
& 7) | REX_B(s
);
4324 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
4327 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
].mmx
);
4329 gen_lea_modrm(env
, s
, modrm
);
4330 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
4331 gen_ldq_env_A0(s
, op2_offset
);
4334 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
4338 case 0x0f: /* 3DNow! data insns */
4339 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_3DNOW
))
4341 val
= cpu_ldub_code(env
, s
->pc
++);
4342 sse_fn_epp
= sse_op_table5
[val
];
4346 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4347 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4348 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
4350 case 0x70: /* pshufx insn */
4351 case 0xc6: /* pshufx insn */
4352 val
= cpu_ldub_code(env
, s
->pc
++);
4353 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4354 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4355 /* XXX: introduce a new table? */
4356 sse_fn_ppi
= (SSEFunc_0_ppi
)sse_fn_epp
;
4357 sse_fn_ppi(cpu_ptr0
, cpu_ptr1
, tcg_const_i32(val
));
4361 val
= cpu_ldub_code(env
, s
->pc
++);
4364 sse_fn_epp
= sse_op_table4
[val
][b1
];
4366 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4367 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4368 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
4371 /* maskmov : we must prepare A0 */
4374 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[R_EDI
]);
4375 gen_extu(s
->aflag
, cpu_A0
);
4376 gen_add_A0_ds_seg(s
);
4378 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4379 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4380 /* XXX: introduce a new table? */
4381 sse_fn_eppt
= (SSEFunc_0_eppt
)sse_fn_epp
;
4382 sse_fn_eppt(cpu_env
, cpu_ptr0
, cpu_ptr1
, cpu_A0
);
4385 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4386 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4387 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
4390 if (b
== 0x2e || b
== 0x2f) {
4391 set_cc_op(s
, CC_OP_EFLAGS
);
4396 /* convert one instruction. s->is_jmp is set if the translation must
4397 be stopped. Return the next pc value */
4398 static target_ulong
disas_insn(CPUX86State
*env
, DisasContext
*s
,
4399 target_ulong pc_start
)
4403 TCGMemOp ot
, aflag
, dflag
;
4404 int modrm
, reg
, rm
, mod
, op
, opreg
, val
;
4405 target_ulong next_eip
, tval
;
4408 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
| CPU_LOG_TB_OP_OPT
))) {
4409 tcg_gen_debug_insn_start(pc_start
);
4416 #ifdef TARGET_X86_64
4421 s
->rip_offset
= 0; /* for relative ip address */
4425 b
= cpu_ldub_code(env
, s
->pc
);
4427 /* Collect prefixes. */
4430 prefixes
|= PREFIX_REPZ
;
4433 prefixes
|= PREFIX_REPNZ
;
4436 prefixes
|= PREFIX_LOCK
;
4457 prefixes
|= PREFIX_DATA
;
4460 prefixes
|= PREFIX_ADR
;
4462 #ifdef TARGET_X86_64
4466 rex_w
= (b
>> 3) & 1;
4467 rex_r
= (b
& 0x4) << 1;
4468 s
->rex_x
= (b
& 0x2) << 2;
4469 REX_B(s
) = (b
& 0x1) << 3;
4470 x86_64_hregs
= 1; /* select uniform byte register addressing */
4475 case 0xc5: /* 2-byte VEX */
4476 case 0xc4: /* 3-byte VEX */
4477 /* VEX prefixes cannot be used except in 32-bit mode.
4478 Otherwise the instruction is LES or LDS. */
4479 if (s
->code32
&& !s
->vm86
) {
4480 static const int pp_prefix
[4] = {
4481 0, PREFIX_DATA
, PREFIX_REPZ
, PREFIX_REPNZ
4483 int vex3
, vex2
= cpu_ldub_code(env
, s
->pc
);
4485 if (!CODE64(s
) && (vex2
& 0xc0) != 0xc0) {
4486 /* 4.1.4.6: In 32-bit mode, bits [7:6] must be 11b,
4487 otherwise the instruction is LES or LDS. */
4492 /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
4493 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
4494 | PREFIX_LOCK
| PREFIX_DATA
)) {
4497 #ifdef TARGET_X86_64
4502 rex_r
= (~vex2
>> 4) & 8;
4505 b
= cpu_ldub_code(env
, s
->pc
++);
4507 #ifdef TARGET_X86_64
4508 s
->rex_x
= (~vex2
>> 3) & 8;
4509 s
->rex_b
= (~vex2
>> 2) & 8;
4511 vex3
= cpu_ldub_code(env
, s
->pc
++);
4512 rex_w
= (vex3
>> 7) & 1;
4513 switch (vex2
& 0x1f) {
4514 case 0x01: /* Implied 0f leading opcode bytes. */
4515 b
= cpu_ldub_code(env
, s
->pc
++) | 0x100;
4517 case 0x02: /* Implied 0f 38 leading opcode bytes. */
4520 case 0x03: /* Implied 0f 3a leading opcode bytes. */
4523 default: /* Reserved for future use. */
4527 s
->vex_v
= (~vex3
>> 3) & 0xf;
4528 s
->vex_l
= (vex3
>> 2) & 1;
4529 prefixes
|= pp_prefix
[vex3
& 3] | PREFIX_VEX
;
4534 /* Post-process prefixes. */
4536 /* In 64-bit mode, the default data size is 32-bit. Select 64-bit
4537 data with rex_w, and 16-bit data with 0x66; rex_w takes precedence
4538 over 0x66 if both are present. */
4539 dflag
= (rex_w
> 0 ? MO_64
: prefixes
& PREFIX_DATA
? MO_16
: MO_32
);
4540 /* In 64-bit mode, 0x67 selects 32-bit addressing. */
4541 aflag
= (prefixes
& PREFIX_ADR
? MO_32
: MO_64
);
4543 /* In 16/32-bit mode, 0x66 selects the opposite data size. */
4544 if (s
->code32
^ ((prefixes
& PREFIX_DATA
) != 0)) {
4549 /* In 16/32-bit mode, 0x67 selects the opposite addressing. */
4550 if (s
->code32
^ ((prefixes
& PREFIX_ADR
) != 0)) {
4557 s
->prefix
= prefixes
;
4561 /* lock generation */
4562 if (prefixes
& PREFIX_LOCK
)
4565 /* now check op code */
4569 /**************************/
4570 /* extended op code */
4571 b
= cpu_ldub_code(env
, s
->pc
++) | 0x100;
4574 /**************************/
4589 ot
= mo_b_d(b
, dflag
);
4592 case 0: /* OP Ev, Gv */
4593 modrm
= cpu_ldub_code(env
, s
->pc
++);
4594 reg
= ((modrm
>> 3) & 7) | rex_r
;
4595 mod
= (modrm
>> 6) & 3;
4596 rm
= (modrm
& 7) | REX_B(s
);
4598 gen_lea_modrm(env
, s
, modrm
);
4600 } else if (op
== OP_XORL
&& rm
== reg
) {
4602 /* xor reg, reg optimisation */
4603 set_cc_op(s
, CC_OP_CLR
);
4604 tcg_gen_movi_tl(cpu_T
[0], 0);
4605 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
4610 gen_op_mov_v_reg(ot
, cpu_T
[1], reg
);
4611 gen_op(s
, op
, ot
, opreg
);
4613 case 1: /* OP Gv, Ev */
4614 modrm
= cpu_ldub_code(env
, s
->pc
++);
4615 mod
= (modrm
>> 6) & 3;
4616 reg
= ((modrm
>> 3) & 7) | rex_r
;
4617 rm
= (modrm
& 7) | REX_B(s
);
4619 gen_lea_modrm(env
, s
, modrm
);
4620 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
4621 } else if (op
== OP_XORL
&& rm
== reg
) {
4624 gen_op_mov_v_reg(ot
, cpu_T
[1], rm
);
4626 gen_op(s
, op
, ot
, reg
);
4628 case 2: /* OP A, Iv */
4629 val
= insn_get(env
, s
, ot
);
4630 tcg_gen_movi_tl(cpu_T
[1], val
);
4631 gen_op(s
, op
, ot
, OR_EAX
);
4640 case 0x80: /* GRP1 */
4646 ot
= mo_b_d(b
, dflag
);
4648 modrm
= cpu_ldub_code(env
, s
->pc
++);
4649 mod
= (modrm
>> 6) & 3;
4650 rm
= (modrm
& 7) | REX_B(s
);
4651 op
= (modrm
>> 3) & 7;
4657 s
->rip_offset
= insn_const_size(ot
);
4658 gen_lea_modrm(env
, s
, modrm
);
4669 val
= insn_get(env
, s
, ot
);
4672 val
= (int8_t)insn_get(env
, s
, MO_8
);
4675 tcg_gen_movi_tl(cpu_T
[1], val
);
4676 gen_op(s
, op
, ot
, opreg
);
4680 /**************************/
4681 /* inc, dec, and other misc arith */
4682 case 0x40 ... 0x47: /* inc Gv */
4684 gen_inc(s
, ot
, OR_EAX
+ (b
& 7), 1);
4686 case 0x48 ... 0x4f: /* dec Gv */
4688 gen_inc(s
, ot
, OR_EAX
+ (b
& 7), -1);
4690 case 0xf6: /* GRP3 */
4692 ot
= mo_b_d(b
, dflag
);
4694 modrm
= cpu_ldub_code(env
, s
->pc
++);
4695 mod
= (modrm
>> 6) & 3;
4696 rm
= (modrm
& 7) | REX_B(s
);
4697 op
= (modrm
>> 3) & 7;
4700 s
->rip_offset
= insn_const_size(ot
);
4701 gen_lea_modrm(env
, s
, modrm
);
4702 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
4704 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
4709 val
= insn_get(env
, s
, ot
);
4710 tcg_gen_movi_tl(cpu_T
[1], val
);
4711 gen_op_testl_T0_T1_cc();
4712 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
4715 tcg_gen_not_tl(cpu_T
[0], cpu_T
[0]);
4717 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
4719 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4723 tcg_gen_neg_tl(cpu_T
[0], cpu_T
[0]);
4725 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
4727 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4729 gen_op_update_neg_cc();
4730 set_cc_op(s
, CC_OP_SUBB
+ ot
);
4735 gen_op_mov_v_reg(MO_8
, cpu_T
[1], R_EAX
);
4736 tcg_gen_ext8u_tl(cpu_T
[0], cpu_T
[0]);
4737 tcg_gen_ext8u_tl(cpu_T
[1], cpu_T
[1]);
4738 /* XXX: use 32 bit mul which could be faster */
4739 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4740 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
4741 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4742 tcg_gen_andi_tl(cpu_cc_src
, cpu_T
[0], 0xff00);
4743 set_cc_op(s
, CC_OP_MULB
);
4746 gen_op_mov_v_reg(MO_16
, cpu_T
[1], R_EAX
);
4747 tcg_gen_ext16u_tl(cpu_T
[0], cpu_T
[0]);
4748 tcg_gen_ext16u_tl(cpu_T
[1], cpu_T
[1]);
4749 /* XXX: use 32 bit mul which could be faster */
4750 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4751 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
4752 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4753 tcg_gen_shri_tl(cpu_T
[0], cpu_T
[0], 16);
4754 gen_op_mov_reg_v(MO_16
, R_EDX
, cpu_T
[0]);
4755 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[0]);
4756 set_cc_op(s
, CC_OP_MULW
);
4760 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4761 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_regs
[R_EAX
]);
4762 tcg_gen_mulu2_i32(cpu_tmp2_i32
, cpu_tmp3_i32
,
4763 cpu_tmp2_i32
, cpu_tmp3_i32
);
4764 tcg_gen_extu_i32_tl(cpu_regs
[R_EAX
], cpu_tmp2_i32
);
4765 tcg_gen_extu_i32_tl(cpu_regs
[R_EDX
], cpu_tmp3_i32
);
4766 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4767 tcg_gen_mov_tl(cpu_cc_src
, cpu_regs
[R_EDX
]);
4768 set_cc_op(s
, CC_OP_MULL
);
4770 #ifdef TARGET_X86_64
4772 tcg_gen_mulu2_i64(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
],
4773 cpu_T
[0], cpu_regs
[R_EAX
]);
4774 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4775 tcg_gen_mov_tl(cpu_cc_src
, cpu_regs
[R_EDX
]);
4776 set_cc_op(s
, CC_OP_MULQ
);
4784 gen_op_mov_v_reg(MO_8
, cpu_T
[1], R_EAX
);
4785 tcg_gen_ext8s_tl(cpu_T
[0], cpu_T
[0]);
4786 tcg_gen_ext8s_tl(cpu_T
[1], cpu_T
[1]);
4787 /* XXX: use 32 bit mul which could be faster */
4788 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4789 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
4790 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4791 tcg_gen_ext8s_tl(cpu_tmp0
, cpu_T
[0]);
4792 tcg_gen_sub_tl(cpu_cc_src
, cpu_T
[0], cpu_tmp0
);
4793 set_cc_op(s
, CC_OP_MULB
);
4796 gen_op_mov_v_reg(MO_16
, cpu_T
[1], R_EAX
);
4797 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
4798 tcg_gen_ext16s_tl(cpu_T
[1], cpu_T
[1]);
4799 /* XXX: use 32 bit mul which could be faster */
4800 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4801 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
4802 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4803 tcg_gen_ext16s_tl(cpu_tmp0
, cpu_T
[0]);
4804 tcg_gen_sub_tl(cpu_cc_src
, cpu_T
[0], cpu_tmp0
);
4805 tcg_gen_shri_tl(cpu_T
[0], cpu_T
[0], 16);
4806 gen_op_mov_reg_v(MO_16
, R_EDX
, cpu_T
[0]);
4807 set_cc_op(s
, CC_OP_MULW
);
4811 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4812 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_regs
[R_EAX
]);
4813 tcg_gen_muls2_i32(cpu_tmp2_i32
, cpu_tmp3_i32
,
4814 cpu_tmp2_i32
, cpu_tmp3_i32
);
4815 tcg_gen_extu_i32_tl(cpu_regs
[R_EAX
], cpu_tmp2_i32
);
4816 tcg_gen_extu_i32_tl(cpu_regs
[R_EDX
], cpu_tmp3_i32
);
4817 tcg_gen_sari_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, 31);
4818 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4819 tcg_gen_sub_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, cpu_tmp3_i32
);
4820 tcg_gen_extu_i32_tl(cpu_cc_src
, cpu_tmp2_i32
);
4821 set_cc_op(s
, CC_OP_MULL
);
4823 #ifdef TARGET_X86_64
4825 tcg_gen_muls2_i64(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
],
4826 cpu_T
[0], cpu_regs
[R_EAX
]);
4827 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4828 tcg_gen_sari_tl(cpu_cc_src
, cpu_regs
[R_EAX
], 63);
4829 tcg_gen_sub_tl(cpu_cc_src
, cpu_cc_src
, cpu_regs
[R_EDX
]);
4830 set_cc_op(s
, CC_OP_MULQ
);
4838 gen_jmp_im(pc_start
- s
->cs_base
);
4839 gen_helper_divb_AL(cpu_env
, cpu_T
[0]);
4842 gen_jmp_im(pc_start
- s
->cs_base
);
4843 gen_helper_divw_AX(cpu_env
, cpu_T
[0]);
4847 gen_jmp_im(pc_start
- s
->cs_base
);
4848 gen_helper_divl_EAX(cpu_env
, cpu_T
[0]);
4850 #ifdef TARGET_X86_64
4852 gen_jmp_im(pc_start
- s
->cs_base
);
4853 gen_helper_divq_EAX(cpu_env
, cpu_T
[0]);
4861 gen_jmp_im(pc_start
- s
->cs_base
);
4862 gen_helper_idivb_AL(cpu_env
, cpu_T
[0]);
4865 gen_jmp_im(pc_start
- s
->cs_base
);
4866 gen_helper_idivw_AX(cpu_env
, cpu_T
[0]);
4870 gen_jmp_im(pc_start
- s
->cs_base
);
4871 gen_helper_idivl_EAX(cpu_env
, cpu_T
[0]);
4873 #ifdef TARGET_X86_64
4875 gen_jmp_im(pc_start
- s
->cs_base
);
4876 gen_helper_idivq_EAX(cpu_env
, cpu_T
[0]);
4886 case 0xfe: /* GRP4 */
4887 case 0xff: /* GRP5 */
4888 ot
= mo_b_d(b
, dflag
);
4890 modrm
= cpu_ldub_code(env
, s
->pc
++);
4891 mod
= (modrm
>> 6) & 3;
4892 rm
= (modrm
& 7) | REX_B(s
);
4893 op
= (modrm
>> 3) & 7;
4894 if (op
>= 2 && b
== 0xfe) {
4898 if (op
== 2 || op
== 4) {
4899 /* operand size for jumps is 64 bit */
4901 } else if (op
== 3 || op
== 5) {
4902 ot
= dflag
!= MO_16
? MO_32
+ (rex_w
== 1) : MO_16
;
4903 } else if (op
== 6) {
4904 /* default push size is 64 bit */
4905 ot
= mo_pushpop(s
, dflag
);
4909 gen_lea_modrm(env
, s
, modrm
);
4910 if (op
>= 2 && op
!= 3 && op
!= 5)
4911 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
4913 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
4917 case 0: /* inc Ev */
4922 gen_inc(s
, ot
, opreg
, 1);
4924 case 1: /* dec Ev */
4929 gen_inc(s
, ot
, opreg
, -1);
4931 case 2: /* call Ev */
4932 /* XXX: optimize if memory (no 'and' is necessary) */
4933 if (dflag
== MO_16
) {
4934 tcg_gen_ext16u_tl(cpu_T
[0], cpu_T
[0]);
4936 next_eip
= s
->pc
- s
->cs_base
;
4937 tcg_gen_movi_tl(cpu_T
[1], next_eip
);
4938 gen_push_v(s
, cpu_T
[1]);
4939 gen_op_jmp_v(cpu_T
[0]);
4942 case 3: /* lcall Ev */
4943 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
4944 gen_add_A0_im(s
, 1 << ot
);
4945 gen_op_ld_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
4947 if (s
->pe
&& !s
->vm86
) {
4948 gen_update_cc_op(s
);
4949 gen_jmp_im(pc_start
- s
->cs_base
);
4950 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4951 gen_helper_lcall_protected(cpu_env
, cpu_tmp2_i32
, cpu_T
[1],
4952 tcg_const_i32(dflag
- 1),
4953 tcg_const_i32(s
->pc
- pc_start
));
4955 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4956 gen_helper_lcall_real(cpu_env
, cpu_tmp2_i32
, cpu_T
[1],
4957 tcg_const_i32(dflag
- 1),
4958 tcg_const_i32(s
->pc
- s
->cs_base
));
4962 case 4: /* jmp Ev */
4963 if (dflag
== MO_16
) {
4964 tcg_gen_ext16u_tl(cpu_T
[0], cpu_T
[0]);
4966 gen_op_jmp_v(cpu_T
[0]);
4969 case 5: /* ljmp Ev */
4970 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
4971 gen_add_A0_im(s
, 1 << ot
);
4972 gen_op_ld_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
4974 if (s
->pe
&& !s
->vm86
) {
4975 gen_update_cc_op(s
);
4976 gen_jmp_im(pc_start
- s
->cs_base
);
4977 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4978 gen_helper_ljmp_protected(cpu_env
, cpu_tmp2_i32
, cpu_T
[1],
4979 tcg_const_i32(s
->pc
- pc_start
));
4981 gen_op_movl_seg_T0_vm(R_CS
);
4982 gen_op_jmp_v(cpu_T
[1]);
4986 case 6: /* push Ev */
4987 gen_push_v(s
, cpu_T
[0]);
4994 case 0x84: /* test Ev, Gv */
4996 ot
= mo_b_d(b
, dflag
);
4998 modrm
= cpu_ldub_code(env
, s
->pc
++);
4999 reg
= ((modrm
>> 3) & 7) | rex_r
;
5001 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5002 gen_op_mov_v_reg(ot
, cpu_T
[1], reg
);
5003 gen_op_testl_T0_T1_cc();
5004 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
5007 case 0xa8: /* test eAX, Iv */
5009 ot
= mo_b_d(b
, dflag
);
5010 val
= insn_get(env
, s
, ot
);
5012 gen_op_mov_v_reg(ot
, cpu_T
[0], OR_EAX
);
5013 tcg_gen_movi_tl(cpu_T
[1], val
);
5014 gen_op_testl_T0_T1_cc();
5015 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
5018 case 0x98: /* CWDE/CBW */
5020 #ifdef TARGET_X86_64
5022 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EAX
);
5023 tcg_gen_ext32s_tl(cpu_T
[0], cpu_T
[0]);
5024 gen_op_mov_reg_v(MO_64
, R_EAX
, cpu_T
[0]);
5028 gen_op_mov_v_reg(MO_16
, cpu_T
[0], R_EAX
);
5029 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
5030 gen_op_mov_reg_v(MO_32
, R_EAX
, cpu_T
[0]);
5033 gen_op_mov_v_reg(MO_8
, cpu_T
[0], R_EAX
);
5034 tcg_gen_ext8s_tl(cpu_T
[0], cpu_T
[0]);
5035 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
5041 case 0x99: /* CDQ/CWD */
5043 #ifdef TARGET_X86_64
5045 gen_op_mov_v_reg(MO_64
, cpu_T
[0], R_EAX
);
5046 tcg_gen_sari_tl(cpu_T
[0], cpu_T
[0], 63);
5047 gen_op_mov_reg_v(MO_64
, R_EDX
, cpu_T
[0]);
5051 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EAX
);
5052 tcg_gen_ext32s_tl(cpu_T
[0], cpu_T
[0]);
5053 tcg_gen_sari_tl(cpu_T
[0], cpu_T
[0], 31);
5054 gen_op_mov_reg_v(MO_32
, R_EDX
, cpu_T
[0]);
5057 gen_op_mov_v_reg(MO_16
, cpu_T
[0], R_EAX
);
5058 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
5059 tcg_gen_sari_tl(cpu_T
[0], cpu_T
[0], 15);
5060 gen_op_mov_reg_v(MO_16
, R_EDX
, cpu_T
[0]);
5066 case 0x1af: /* imul Gv, Ev */
5067 case 0x69: /* imul Gv, Ev, I */
5070 modrm
= cpu_ldub_code(env
, s
->pc
++);
5071 reg
= ((modrm
>> 3) & 7) | rex_r
;
5073 s
->rip_offset
= insn_const_size(ot
);
5076 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5078 val
= insn_get(env
, s
, ot
);
5079 tcg_gen_movi_tl(cpu_T
[1], val
);
5080 } else if (b
== 0x6b) {
5081 val
= (int8_t)insn_get(env
, s
, MO_8
);
5082 tcg_gen_movi_tl(cpu_T
[1], val
);
5084 gen_op_mov_v_reg(ot
, cpu_T
[1], reg
);
5087 #ifdef TARGET_X86_64
5089 tcg_gen_muls2_i64(cpu_regs
[reg
], cpu_T
[1], cpu_T
[0], cpu_T
[1]);
5090 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[reg
]);
5091 tcg_gen_sari_tl(cpu_cc_src
, cpu_cc_dst
, 63);
5092 tcg_gen_sub_tl(cpu_cc_src
, cpu_cc_src
, cpu_T
[1]);
5096 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
5097 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[1]);
5098 tcg_gen_muls2_i32(cpu_tmp2_i32
, cpu_tmp3_i32
,
5099 cpu_tmp2_i32
, cpu_tmp3_i32
);
5100 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp2_i32
);
5101 tcg_gen_sari_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, 31);
5102 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[reg
]);
5103 tcg_gen_sub_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, cpu_tmp3_i32
);
5104 tcg_gen_extu_i32_tl(cpu_cc_src
, cpu_tmp2_i32
);
5107 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
5108 tcg_gen_ext16s_tl(cpu_T
[1], cpu_T
[1]);
5109 /* XXX: use 32 bit mul which could be faster */
5110 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
5111 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
5112 tcg_gen_ext16s_tl(cpu_tmp0
, cpu_T
[0]);
5113 tcg_gen_sub_tl(cpu_cc_src
, cpu_T
[0], cpu_tmp0
);
5114 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
5117 set_cc_op(s
, CC_OP_MULB
+ ot
);
5120 case 0x1c1: /* xadd Ev, Gv */
5121 ot
= mo_b_d(b
, dflag
);
5122 modrm
= cpu_ldub_code(env
, s
->pc
++);
5123 reg
= ((modrm
>> 3) & 7) | rex_r
;
5124 mod
= (modrm
>> 6) & 3;
5126 rm
= (modrm
& 7) | REX_B(s
);
5127 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
5128 gen_op_mov_v_reg(ot
, cpu_T
[1], rm
);
5129 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
5130 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5131 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
5133 gen_lea_modrm(env
, s
, modrm
);
5134 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
5135 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
5136 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
5137 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
5138 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5140 gen_op_update2_cc();
5141 set_cc_op(s
, CC_OP_ADDB
+ ot
);
5144 case 0x1b1: /* cmpxchg Ev, Gv */
5147 TCGv t0
, t1
, t2
, a0
;
5149 ot
= mo_b_d(b
, dflag
);
5150 modrm
= cpu_ldub_code(env
, s
->pc
++);
5151 reg
= ((modrm
>> 3) & 7) | rex_r
;
5152 mod
= (modrm
>> 6) & 3;
5153 t0
= tcg_temp_local_new();
5154 t1
= tcg_temp_local_new();
5155 t2
= tcg_temp_local_new();
5156 a0
= tcg_temp_local_new();
5157 gen_op_mov_v_reg(ot
, t1
, reg
);
5159 rm
= (modrm
& 7) | REX_B(s
);
5160 gen_op_mov_v_reg(ot
, t0
, rm
);
5162 gen_lea_modrm(env
, s
, modrm
);
5163 tcg_gen_mov_tl(a0
, cpu_A0
);
5164 gen_op_ld_v(s
, ot
, t0
, a0
);
5165 rm
= 0; /* avoid warning */
5167 label1
= gen_new_label();
5168 tcg_gen_mov_tl(t2
, cpu_regs
[R_EAX
]);
5171 tcg_gen_brcond_tl(TCG_COND_EQ
, t2
, t0
, label1
);
5172 label2
= gen_new_label();
5174 gen_op_mov_reg_v(ot
, R_EAX
, t0
);
5176 gen_set_label(label1
);
5177 gen_op_mov_reg_v(ot
, rm
, t1
);
5179 /* perform no-op store cycle like physical cpu; must be
5180 before changing accumulator to ensure idempotency if
5181 the store faults and the instruction is restarted */
5182 gen_op_st_v(s
, ot
, t0
, a0
);
5183 gen_op_mov_reg_v(ot
, R_EAX
, t0
);
5185 gen_set_label(label1
);
5186 gen_op_st_v(s
, ot
, t1
, a0
);
5188 gen_set_label(label2
);
5189 tcg_gen_mov_tl(cpu_cc_src
, t0
);
5190 tcg_gen_mov_tl(cpu_cc_srcT
, t2
);
5191 tcg_gen_sub_tl(cpu_cc_dst
, t2
, t0
);
5192 set_cc_op(s
, CC_OP_SUBB
+ ot
);
5199 case 0x1c7: /* cmpxchg8b */
5200 modrm
= cpu_ldub_code(env
, s
->pc
++);
5201 mod
= (modrm
>> 6) & 3;
5202 if ((mod
== 3) || ((modrm
& 0x38) != 0x8))
5204 #ifdef TARGET_X86_64
5205 if (dflag
== MO_64
) {
5206 if (!(s
->cpuid_ext_features
& CPUID_EXT_CX16
))
5208 gen_jmp_im(pc_start
- s
->cs_base
);
5209 gen_update_cc_op(s
);
5210 gen_lea_modrm(env
, s
, modrm
);
5211 gen_helper_cmpxchg16b(cpu_env
, cpu_A0
);
5215 if (!(s
->cpuid_features
& CPUID_CX8
))
5217 gen_jmp_im(pc_start
- s
->cs_base
);
5218 gen_update_cc_op(s
);
5219 gen_lea_modrm(env
, s
, modrm
);
5220 gen_helper_cmpxchg8b(cpu_env
, cpu_A0
);
5222 set_cc_op(s
, CC_OP_EFLAGS
);
5225 /**************************/
5227 case 0x50 ... 0x57: /* push */
5228 gen_op_mov_v_reg(MO_32
, cpu_T
[0], (b
& 7) | REX_B(s
));
5229 gen_push_v(s
, cpu_T
[0]);
5231 case 0x58 ... 0x5f: /* pop */
5233 /* NOTE: order is important for pop %sp */
5234 gen_pop_update(s
, ot
);
5235 gen_op_mov_reg_v(ot
, (b
& 7) | REX_B(s
), cpu_T
[0]);
5237 case 0x60: /* pusha */
5242 case 0x61: /* popa */
5247 case 0x68: /* push Iv */
5249 ot
= mo_pushpop(s
, dflag
);
5251 val
= insn_get(env
, s
, ot
);
5253 val
= (int8_t)insn_get(env
, s
, MO_8
);
5254 tcg_gen_movi_tl(cpu_T
[0], val
);
5255 gen_push_v(s
, cpu_T
[0]);
5257 case 0x8f: /* pop Ev */
5258 modrm
= cpu_ldub_code(env
, s
->pc
++);
5259 mod
= (modrm
>> 6) & 3;
5262 /* NOTE: order is important for pop %sp */
5263 gen_pop_update(s
, ot
);
5264 rm
= (modrm
& 7) | REX_B(s
);
5265 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
5267 /* NOTE: order is important too for MMU exceptions */
5268 s
->popl_esp_hack
= 1 << ot
;
5269 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
5270 s
->popl_esp_hack
= 0;
5271 gen_pop_update(s
, ot
);
5274 case 0xc8: /* enter */
5277 val
= cpu_lduw_code(env
, s
->pc
);
5279 level
= cpu_ldub_code(env
, s
->pc
++);
5280 gen_enter(s
, val
, level
);
5283 case 0xc9: /* leave */
5284 /* XXX: exception not precise (ESP is updated before potential exception) */
5286 gen_op_mov_v_reg(MO_64
, cpu_T
[0], R_EBP
);
5287 gen_op_mov_reg_v(MO_64
, R_ESP
, cpu_T
[0]);
5288 } else if (s
->ss32
) {
5289 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EBP
);
5290 gen_op_mov_reg_v(MO_32
, R_ESP
, cpu_T
[0]);
5292 gen_op_mov_v_reg(MO_16
, cpu_T
[0], R_EBP
);
5293 gen_op_mov_reg_v(MO_16
, R_ESP
, cpu_T
[0]);
5296 gen_op_mov_reg_v(ot
, R_EBP
, cpu_T
[0]);
5297 gen_pop_update(s
, ot
);
5299 case 0x06: /* push es */
5300 case 0x0e: /* push cs */
5301 case 0x16: /* push ss */
5302 case 0x1e: /* push ds */
5305 gen_op_movl_T0_seg(b
>> 3);
5306 gen_push_v(s
, cpu_T
[0]);
5308 case 0x1a0: /* push fs */
5309 case 0x1a8: /* push gs */
5310 gen_op_movl_T0_seg((b
>> 3) & 7);
5311 gen_push_v(s
, cpu_T
[0]);
5313 case 0x07: /* pop es */
5314 case 0x17: /* pop ss */
5315 case 0x1f: /* pop ds */
5320 gen_movl_seg_T0(s
, reg
, pc_start
- s
->cs_base
);
5321 gen_pop_update(s
, ot
);
5323 /* if reg == SS, inhibit interrupts/trace. */
5324 /* If several instructions disable interrupts, only the
5326 if (!(s
->tb
->flags
& HF_INHIBIT_IRQ_MASK
))
5327 gen_helper_set_inhibit_irq(cpu_env
);
5331 gen_jmp_im(s
->pc
- s
->cs_base
);
5335 case 0x1a1: /* pop fs */
5336 case 0x1a9: /* pop gs */
5338 gen_movl_seg_T0(s
, (b
>> 3) & 7, pc_start
- s
->cs_base
);
5339 gen_pop_update(s
, ot
);
5341 gen_jmp_im(s
->pc
- s
->cs_base
);
5346 /**************************/
5349 case 0x89: /* mov Gv, Ev */
5350 ot
= mo_b_d(b
, dflag
);
5351 modrm
= cpu_ldub_code(env
, s
->pc
++);
5352 reg
= ((modrm
>> 3) & 7) | rex_r
;
5354 /* generate a generic store */
5355 gen_ldst_modrm(env
, s
, modrm
, ot
, reg
, 1);
5358 case 0xc7: /* mov Ev, Iv */
5359 ot
= mo_b_d(b
, dflag
);
5360 modrm
= cpu_ldub_code(env
, s
->pc
++);
5361 mod
= (modrm
>> 6) & 3;
5363 s
->rip_offset
= insn_const_size(ot
);
5364 gen_lea_modrm(env
, s
, modrm
);
5366 val
= insn_get(env
, s
, ot
);
5367 tcg_gen_movi_tl(cpu_T
[0], val
);
5369 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
5371 gen_op_mov_reg_v(ot
, (modrm
& 7) | REX_B(s
), cpu_T
[0]);
5375 case 0x8b: /* mov Ev, Gv */
5376 ot
= mo_b_d(b
, dflag
);
5377 modrm
= cpu_ldub_code(env
, s
->pc
++);
5378 reg
= ((modrm
>> 3) & 7) | rex_r
;
5380 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5381 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
5383 case 0x8e: /* mov seg, Gv */
5384 modrm
= cpu_ldub_code(env
, s
->pc
++);
5385 reg
= (modrm
>> 3) & 7;
5386 if (reg
>= 6 || reg
== R_CS
)
5388 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
5389 gen_movl_seg_T0(s
, reg
, pc_start
- s
->cs_base
);
5391 /* if reg == SS, inhibit interrupts/trace */
5392 /* If several instructions disable interrupts, only the
5394 if (!(s
->tb
->flags
& HF_INHIBIT_IRQ_MASK
))
5395 gen_helper_set_inhibit_irq(cpu_env
);
5399 gen_jmp_im(s
->pc
- s
->cs_base
);
5403 case 0x8c: /* mov Gv, seg */
5404 modrm
= cpu_ldub_code(env
, s
->pc
++);
5405 reg
= (modrm
>> 3) & 7;
5406 mod
= (modrm
>> 6) & 3;
5409 gen_op_movl_T0_seg(reg
);
5410 ot
= mod
== 3 ? dflag
: MO_16
;
5411 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
5414 case 0x1b6: /* movzbS Gv, Eb */
5415 case 0x1b7: /* movzwS Gv, Eb */
5416 case 0x1be: /* movsbS Gv, Eb */
5417 case 0x1bf: /* movswS Gv, Eb */
5422 /* d_ot is the size of destination */
5424 /* ot is the size of source */
5425 ot
= (b
& 1) + MO_8
;
5426 /* s_ot is the sign+size of source */
5427 s_ot
= b
& 8 ? MO_SIGN
| ot
: ot
;
5429 modrm
= cpu_ldub_code(env
, s
->pc
++);
5430 reg
= ((modrm
>> 3) & 7) | rex_r
;
5431 mod
= (modrm
>> 6) & 3;
5432 rm
= (modrm
& 7) | REX_B(s
);
5435 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
5438 tcg_gen_ext8u_tl(cpu_T
[0], cpu_T
[0]);
5441 tcg_gen_ext8s_tl(cpu_T
[0], cpu_T
[0]);
5444 tcg_gen_ext16u_tl(cpu_T
[0], cpu_T
[0]);
5448 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
5451 gen_op_mov_reg_v(d_ot
, reg
, cpu_T
[0]);
5453 gen_lea_modrm(env
, s
, modrm
);
5454 gen_op_ld_v(s
, s_ot
, cpu_T
[0], cpu_A0
);
5455 gen_op_mov_reg_v(d_ot
, reg
, cpu_T
[0]);
5460 case 0x8d: /* lea */
5462 modrm
= cpu_ldub_code(env
, s
->pc
++);
5463 mod
= (modrm
>> 6) & 3;
5466 reg
= ((modrm
>> 3) & 7) | rex_r
;
5467 /* we must ensure that no segment is added */
5471 gen_lea_modrm(env
, s
, modrm
);
5473 gen_op_mov_reg_v(ot
, reg
, cpu_A0
);
5476 case 0xa0: /* mov EAX, Ov */
5478 case 0xa2: /* mov Ov, EAX */
5481 target_ulong offset_addr
;
5483 ot
= mo_b_d(b
, dflag
);
5485 #ifdef TARGET_X86_64
5487 offset_addr
= cpu_ldq_code(env
, s
->pc
);
5492 offset_addr
= insn_get(env
, s
, s
->aflag
);
5495 tcg_gen_movi_tl(cpu_A0
, offset_addr
);
5496 gen_add_A0_ds_seg(s
);
5498 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
5499 gen_op_mov_reg_v(ot
, R_EAX
, cpu_T
[0]);
5501 gen_op_mov_v_reg(ot
, cpu_T
[0], R_EAX
);
5502 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
5506 case 0xd7: /* xlat */
5507 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[R_EBX
]);
5508 tcg_gen_ext8u_tl(cpu_T
[0], cpu_regs
[R_EAX
]);
5509 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_T
[0]);
5510 gen_extu(s
->aflag
, cpu_A0
);
5511 gen_add_A0_ds_seg(s
);
5512 gen_op_ld_v(s
, MO_8
, cpu_T
[0], cpu_A0
);
5513 gen_op_mov_reg_v(MO_8
, R_EAX
, cpu_T
[0]);
5515 case 0xb0 ... 0xb7: /* mov R, Ib */
5516 val
= insn_get(env
, s
, MO_8
);
5517 tcg_gen_movi_tl(cpu_T
[0], val
);
5518 gen_op_mov_reg_v(MO_8
, (b
& 7) | REX_B(s
), cpu_T
[0]);
5520 case 0xb8 ... 0xbf: /* mov R, Iv */
5521 #ifdef TARGET_X86_64
5522 if (dflag
== MO_64
) {
5525 tmp
= cpu_ldq_code(env
, s
->pc
);
5527 reg
= (b
& 7) | REX_B(s
);
5528 tcg_gen_movi_tl(cpu_T
[0], tmp
);
5529 gen_op_mov_reg_v(MO_64
, reg
, cpu_T
[0]);
5534 val
= insn_get(env
, s
, ot
);
5535 reg
= (b
& 7) | REX_B(s
);
5536 tcg_gen_movi_tl(cpu_T
[0], val
);
5537 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
5541 case 0x91 ... 0x97: /* xchg R, EAX */
5544 reg
= (b
& 7) | REX_B(s
);
5548 case 0x87: /* xchg Ev, Gv */
5549 ot
= mo_b_d(b
, dflag
);
5550 modrm
= cpu_ldub_code(env
, s
->pc
++);
5551 reg
= ((modrm
>> 3) & 7) | rex_r
;
5552 mod
= (modrm
>> 6) & 3;
5554 rm
= (modrm
& 7) | REX_B(s
);
5556 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
5557 gen_op_mov_v_reg(ot
, cpu_T
[1], rm
);
5558 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
5559 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5561 gen_lea_modrm(env
, s
, modrm
);
5562 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
5563 /* for xchg, lock is implicit */
5564 if (!(prefixes
& PREFIX_LOCK
))
5566 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
5567 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
5568 if (!(prefixes
& PREFIX_LOCK
))
5569 gen_helper_unlock();
5570 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5573 case 0xc4: /* les Gv */
5574 /* In CODE64 this is VEX3; see above. */
5577 case 0xc5: /* lds Gv */
5578 /* In CODE64 this is VEX2; see above. */
5581 case 0x1b2: /* lss Gv */
5584 case 0x1b4: /* lfs Gv */
5587 case 0x1b5: /* lgs Gv */
5590 ot
= dflag
!= MO_16
? MO_32
: MO_16
;
5591 modrm
= cpu_ldub_code(env
, s
->pc
++);
5592 reg
= ((modrm
>> 3) & 7) | rex_r
;
5593 mod
= (modrm
>> 6) & 3;
5596 gen_lea_modrm(env
, s
, modrm
);
5597 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
5598 gen_add_A0_im(s
, 1 << ot
);
5599 /* load the segment first to handle exceptions properly */
5600 gen_op_ld_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
5601 gen_movl_seg_T0(s
, op
, pc_start
- s
->cs_base
);
5602 /* then put the data */
5603 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5605 gen_jmp_im(s
->pc
- s
->cs_base
);
5610 /************************/
5618 ot
= mo_b_d(b
, dflag
);
5619 modrm
= cpu_ldub_code(env
, s
->pc
++);
5620 mod
= (modrm
>> 6) & 3;
5621 op
= (modrm
>> 3) & 7;
5627 gen_lea_modrm(env
, s
, modrm
);
5630 opreg
= (modrm
& 7) | REX_B(s
);
5635 gen_shift(s
, op
, ot
, opreg
, OR_ECX
);
5638 shift
= cpu_ldub_code(env
, s
->pc
++);
5640 gen_shifti(s
, op
, ot
, opreg
, shift
);
5655 case 0x1a4: /* shld imm */
5659 case 0x1a5: /* shld cl */
5663 case 0x1ac: /* shrd imm */
5667 case 0x1ad: /* shrd cl */
5672 modrm
= cpu_ldub_code(env
, s
->pc
++);
5673 mod
= (modrm
>> 6) & 3;
5674 rm
= (modrm
& 7) | REX_B(s
);
5675 reg
= ((modrm
>> 3) & 7) | rex_r
;
5677 gen_lea_modrm(env
, s
, modrm
);
5682 gen_op_mov_v_reg(ot
, cpu_T
[1], reg
);
5685 TCGv imm
= tcg_const_tl(cpu_ldub_code(env
, s
->pc
++));
5686 gen_shiftd_rm_T1(s
, ot
, opreg
, op
, imm
);
5689 gen_shiftd_rm_T1(s
, ot
, opreg
, op
, cpu_regs
[R_ECX
]);
5693 /************************/
5696 if (s
->flags
& (HF_EM_MASK
| HF_TS_MASK
)) {
5697 /* if CR0.EM or CR0.TS are set, generate an FPU exception */
5698 /* XXX: what to do if illegal op ? */
5699 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
5702 modrm
= cpu_ldub_code(env
, s
->pc
++);
5703 mod
= (modrm
>> 6) & 3;
5705 op
= ((b
& 7) << 3) | ((modrm
>> 3) & 7);
5708 gen_lea_modrm(env
, s
, modrm
);
5710 case 0x00 ... 0x07: /* fxxxs */
5711 case 0x10 ... 0x17: /* fixxxl */
5712 case 0x20 ... 0x27: /* fxxxl */
5713 case 0x30 ... 0x37: /* fixxx */
5720 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5721 s
->mem_index
, MO_LEUL
);
5722 gen_helper_flds_FT0(cpu_env
, cpu_tmp2_i32
);
5725 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5726 s
->mem_index
, MO_LEUL
);
5727 gen_helper_fildl_FT0(cpu_env
, cpu_tmp2_i32
);
5730 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
,
5731 s
->mem_index
, MO_LEQ
);
5732 gen_helper_fldl_FT0(cpu_env
, cpu_tmp1_i64
);
5736 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5737 s
->mem_index
, MO_LESW
);
5738 gen_helper_fildl_FT0(cpu_env
, cpu_tmp2_i32
);
5742 gen_helper_fp_arith_ST0_FT0(op1
);
5744 /* fcomp needs pop */
5745 gen_helper_fpop(cpu_env
);
5749 case 0x08: /* flds */
5750 case 0x0a: /* fsts */
5751 case 0x0b: /* fstps */
5752 case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
5753 case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
5754 case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
5759 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5760 s
->mem_index
, MO_LEUL
);
5761 gen_helper_flds_ST0(cpu_env
, cpu_tmp2_i32
);
5764 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5765 s
->mem_index
, MO_LEUL
);
5766 gen_helper_fildl_ST0(cpu_env
, cpu_tmp2_i32
);
5769 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
,
5770 s
->mem_index
, MO_LEQ
);
5771 gen_helper_fldl_ST0(cpu_env
, cpu_tmp1_i64
);
5775 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5776 s
->mem_index
, MO_LESW
);
5777 gen_helper_fildl_ST0(cpu_env
, cpu_tmp2_i32
);
5782 /* XXX: the corresponding CPUID bit must be tested ! */
5785 gen_helper_fisttl_ST0(cpu_tmp2_i32
, cpu_env
);
5786 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5787 s
->mem_index
, MO_LEUL
);
5790 gen_helper_fisttll_ST0(cpu_tmp1_i64
, cpu_env
);
5791 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
,
5792 s
->mem_index
, MO_LEQ
);
5796 gen_helper_fistt_ST0(cpu_tmp2_i32
, cpu_env
);
5797 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5798 s
->mem_index
, MO_LEUW
);
5801 gen_helper_fpop(cpu_env
);
5806 gen_helper_fsts_ST0(cpu_tmp2_i32
, cpu_env
);
5807 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5808 s
->mem_index
, MO_LEUL
);
5811 gen_helper_fistl_ST0(cpu_tmp2_i32
, cpu_env
);
5812 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5813 s
->mem_index
, MO_LEUL
);
5816 gen_helper_fstl_ST0(cpu_tmp1_i64
, cpu_env
);
5817 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
,
5818 s
->mem_index
, MO_LEQ
);
5822 gen_helper_fist_ST0(cpu_tmp2_i32
, cpu_env
);
5823 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5824 s
->mem_index
, MO_LEUW
);
5828 gen_helper_fpop(cpu_env
);
5832 case 0x0c: /* fldenv mem */
5833 gen_update_cc_op(s
);
5834 gen_jmp_im(pc_start
- s
->cs_base
);
5835 gen_helper_fldenv(cpu_env
, cpu_A0
, tcg_const_i32(dflag
- 1));
5837 case 0x0d: /* fldcw mem */
5838 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5839 s
->mem_index
, MO_LEUW
);
5840 gen_helper_fldcw(cpu_env
, cpu_tmp2_i32
);
5842 case 0x0e: /* fnstenv mem */
5843 gen_update_cc_op(s
);
5844 gen_jmp_im(pc_start
- s
->cs_base
);
5845 gen_helper_fstenv(cpu_env
, cpu_A0
, tcg_const_i32(dflag
- 1));
5847 case 0x0f: /* fnstcw mem */
5848 gen_helper_fnstcw(cpu_tmp2_i32
, cpu_env
);
5849 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5850 s
->mem_index
, MO_LEUW
);
5852 case 0x1d: /* fldt mem */
5853 gen_update_cc_op(s
);
5854 gen_jmp_im(pc_start
- s
->cs_base
);
5855 gen_helper_fldt_ST0(cpu_env
, cpu_A0
);
5857 case 0x1f: /* fstpt mem */
5858 gen_update_cc_op(s
);
5859 gen_jmp_im(pc_start
- s
->cs_base
);
5860 gen_helper_fstt_ST0(cpu_env
, cpu_A0
);
5861 gen_helper_fpop(cpu_env
);
5863 case 0x2c: /* frstor mem */
5864 gen_update_cc_op(s
);
5865 gen_jmp_im(pc_start
- s
->cs_base
);
5866 gen_helper_frstor(cpu_env
, cpu_A0
, tcg_const_i32(dflag
- 1));
5868 case 0x2e: /* fnsave mem */
5869 gen_update_cc_op(s
);
5870 gen_jmp_im(pc_start
- s
->cs_base
);
5871 gen_helper_fsave(cpu_env
, cpu_A0
, tcg_const_i32(dflag
- 1));
5873 case 0x2f: /* fnstsw mem */
5874 gen_helper_fnstsw(cpu_tmp2_i32
, cpu_env
);
5875 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5876 s
->mem_index
, MO_LEUW
);
5878 case 0x3c: /* fbld */
5879 gen_update_cc_op(s
);
5880 gen_jmp_im(pc_start
- s
->cs_base
);
5881 gen_helper_fbld_ST0(cpu_env
, cpu_A0
);
5883 case 0x3e: /* fbstp */
5884 gen_update_cc_op(s
);
5885 gen_jmp_im(pc_start
- s
->cs_base
);
5886 gen_helper_fbst_ST0(cpu_env
, cpu_A0
);
5887 gen_helper_fpop(cpu_env
);
5889 case 0x3d: /* fildll */
5890 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
, s
->mem_index
, MO_LEQ
);
5891 gen_helper_fildll_ST0(cpu_env
, cpu_tmp1_i64
);
5893 case 0x3f: /* fistpll */
5894 gen_helper_fistll_ST0(cpu_tmp1_i64
, cpu_env
);
5895 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
, s
->mem_index
, MO_LEQ
);
5896 gen_helper_fpop(cpu_env
);
5902 /* register float ops */
5906 case 0x08: /* fld sti */
5907 gen_helper_fpush(cpu_env
);
5908 gen_helper_fmov_ST0_STN(cpu_env
,
5909 tcg_const_i32((opreg
+ 1) & 7));
5911 case 0x09: /* fxchg sti */
5912 case 0x29: /* fxchg4 sti, undocumented op */
5913 case 0x39: /* fxchg7 sti, undocumented op */
5914 gen_helper_fxchg_ST0_STN(cpu_env
, tcg_const_i32(opreg
));
5916 case 0x0a: /* grp d9/2 */
5919 /* check exceptions (FreeBSD FPU probe) */
5920 gen_update_cc_op(s
);
5921 gen_jmp_im(pc_start
- s
->cs_base
);
5922 gen_helper_fwait(cpu_env
);
5928 case 0x0c: /* grp d9/4 */
5931 gen_helper_fchs_ST0(cpu_env
);
5934 gen_helper_fabs_ST0(cpu_env
);
5937 gen_helper_fldz_FT0(cpu_env
);
5938 gen_helper_fcom_ST0_FT0(cpu_env
);
5941 gen_helper_fxam_ST0(cpu_env
);
5947 case 0x0d: /* grp d9/5 */
5951 gen_helper_fpush(cpu_env
);
5952 gen_helper_fld1_ST0(cpu_env
);
5955 gen_helper_fpush(cpu_env
);
5956 gen_helper_fldl2t_ST0(cpu_env
);
5959 gen_helper_fpush(cpu_env
);
5960 gen_helper_fldl2e_ST0(cpu_env
);
5963 gen_helper_fpush(cpu_env
);
5964 gen_helper_fldpi_ST0(cpu_env
);
5967 gen_helper_fpush(cpu_env
);
5968 gen_helper_fldlg2_ST0(cpu_env
);
5971 gen_helper_fpush(cpu_env
);
5972 gen_helper_fldln2_ST0(cpu_env
);
5975 gen_helper_fpush(cpu_env
);
5976 gen_helper_fldz_ST0(cpu_env
);
5983 case 0x0e: /* grp d9/6 */
5986 gen_helper_f2xm1(cpu_env
);
5989 gen_helper_fyl2x(cpu_env
);
5992 gen_helper_fptan(cpu_env
);
5994 case 3: /* fpatan */
5995 gen_helper_fpatan(cpu_env
);
5997 case 4: /* fxtract */
5998 gen_helper_fxtract(cpu_env
);
6000 case 5: /* fprem1 */
6001 gen_helper_fprem1(cpu_env
);
6003 case 6: /* fdecstp */
6004 gen_helper_fdecstp(cpu_env
);
6007 case 7: /* fincstp */
6008 gen_helper_fincstp(cpu_env
);
6012 case 0x0f: /* grp d9/7 */
6015 gen_helper_fprem(cpu_env
);
6017 case 1: /* fyl2xp1 */
6018 gen_helper_fyl2xp1(cpu_env
);
6021 gen_helper_fsqrt(cpu_env
);
6023 case 3: /* fsincos */
6024 gen_helper_fsincos(cpu_env
);
6026 case 5: /* fscale */
6027 gen_helper_fscale(cpu_env
);
6029 case 4: /* frndint */
6030 gen_helper_frndint(cpu_env
);
6033 gen_helper_fsin(cpu_env
);
6037 gen_helper_fcos(cpu_env
);
6041 case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
6042 case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
6043 case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
6049 gen_helper_fp_arith_STN_ST0(op1
, opreg
);
6051 gen_helper_fpop(cpu_env
);
6053 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6054 gen_helper_fp_arith_ST0_FT0(op1
);
6058 case 0x02: /* fcom */
6059 case 0x22: /* fcom2, undocumented op */
6060 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6061 gen_helper_fcom_ST0_FT0(cpu_env
);
6063 case 0x03: /* fcomp */
6064 case 0x23: /* fcomp3, undocumented op */
6065 case 0x32: /* fcomp5, undocumented op */
6066 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6067 gen_helper_fcom_ST0_FT0(cpu_env
);
6068 gen_helper_fpop(cpu_env
);
6070 case 0x15: /* da/5 */
6072 case 1: /* fucompp */
6073 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(1));
6074 gen_helper_fucom_ST0_FT0(cpu_env
);
6075 gen_helper_fpop(cpu_env
);
6076 gen_helper_fpop(cpu_env
);
6084 case 0: /* feni (287 only, just do nop here) */
6086 case 1: /* fdisi (287 only, just do nop here) */
6089 gen_helper_fclex(cpu_env
);
6091 case 3: /* fninit */
6092 gen_helper_fninit(cpu_env
);
6094 case 4: /* fsetpm (287 only, just do nop here) */
6100 case 0x1d: /* fucomi */
6101 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6104 gen_update_cc_op(s
);
6105 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6106 gen_helper_fucomi_ST0_FT0(cpu_env
);
6107 set_cc_op(s
, CC_OP_EFLAGS
);
6109 case 0x1e: /* fcomi */
6110 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6113 gen_update_cc_op(s
);
6114 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6115 gen_helper_fcomi_ST0_FT0(cpu_env
);
6116 set_cc_op(s
, CC_OP_EFLAGS
);
6118 case 0x28: /* ffree sti */
6119 gen_helper_ffree_STN(cpu_env
, tcg_const_i32(opreg
));
6121 case 0x2a: /* fst sti */
6122 gen_helper_fmov_STN_ST0(cpu_env
, tcg_const_i32(opreg
));
6124 case 0x2b: /* fstp sti */
6125 case 0x0b: /* fstp1 sti, undocumented op */
6126 case 0x3a: /* fstp8 sti, undocumented op */
6127 case 0x3b: /* fstp9 sti, undocumented op */
6128 gen_helper_fmov_STN_ST0(cpu_env
, tcg_const_i32(opreg
));
6129 gen_helper_fpop(cpu_env
);
6131 case 0x2c: /* fucom st(i) */
6132 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6133 gen_helper_fucom_ST0_FT0(cpu_env
);
6135 case 0x2d: /* fucomp st(i) */
6136 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6137 gen_helper_fucom_ST0_FT0(cpu_env
);
6138 gen_helper_fpop(cpu_env
);
6140 case 0x33: /* de/3 */
6142 case 1: /* fcompp */
6143 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(1));
6144 gen_helper_fcom_ST0_FT0(cpu_env
);
6145 gen_helper_fpop(cpu_env
);
6146 gen_helper_fpop(cpu_env
);
6152 case 0x38: /* ffreep sti, undocumented op */
6153 gen_helper_ffree_STN(cpu_env
, tcg_const_i32(opreg
));
6154 gen_helper_fpop(cpu_env
);
6156 case 0x3c: /* df/4 */
6159 gen_helper_fnstsw(cpu_tmp2_i32
, cpu_env
);
6160 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
6161 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
6167 case 0x3d: /* fucomip */
6168 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6171 gen_update_cc_op(s
);
6172 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6173 gen_helper_fucomi_ST0_FT0(cpu_env
);
6174 gen_helper_fpop(cpu_env
);
6175 set_cc_op(s
, CC_OP_EFLAGS
);
6177 case 0x3e: /* fcomip */
6178 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6181 gen_update_cc_op(s
);
6182 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6183 gen_helper_fcomi_ST0_FT0(cpu_env
);
6184 gen_helper_fpop(cpu_env
);
6185 set_cc_op(s
, CC_OP_EFLAGS
);
6187 case 0x10 ... 0x13: /* fcmovxx */
6191 static const uint8_t fcmov_cc
[8] = {
6198 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6201 op1
= fcmov_cc
[op
& 3] | (((op
>> 3) & 1) ^ 1);
6202 l1
= gen_new_label();
6203 gen_jcc1_noeob(s
, op1
, l1
);
6204 gen_helper_fmov_ST0_STN(cpu_env
, tcg_const_i32(opreg
));
6213 /************************/
6216 case 0xa4: /* movsS */
6218 ot
= mo_b_d(b
, dflag
);
6219 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6220 gen_repz_movs(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6226 case 0xaa: /* stosS */
6228 ot
= mo_b_d(b
, dflag
);
6229 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6230 gen_repz_stos(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6235 case 0xac: /* lodsS */
6237 ot
= mo_b_d(b
, dflag
);
6238 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6239 gen_repz_lods(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6244 case 0xae: /* scasS */
6246 ot
= mo_b_d(b
, dflag
);
6247 if (prefixes
& PREFIX_REPNZ
) {
6248 gen_repz_scas(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 1);
6249 } else if (prefixes
& PREFIX_REPZ
) {
6250 gen_repz_scas(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 0);
6256 case 0xa6: /* cmpsS */
6258 ot
= mo_b_d(b
, dflag
);
6259 if (prefixes
& PREFIX_REPNZ
) {
6260 gen_repz_cmps(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 1);
6261 } else if (prefixes
& PREFIX_REPZ
) {
6262 gen_repz_cmps(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 0);
6267 case 0x6c: /* insS */
6269 ot
= mo_b_d32(b
, dflag
);
6270 tcg_gen_ext16u_tl(cpu_T
[0], cpu_regs
[R_EDX
]);
6271 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6272 SVM_IOIO_TYPE_MASK
| svm_is_rep(prefixes
) | 4);
6273 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6274 gen_repz_ins(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6278 gen_jmp(s
, s
->pc
- s
->cs_base
);
6282 case 0x6e: /* outsS */
6284 ot
= mo_b_d32(b
, dflag
);
6285 tcg_gen_ext16u_tl(cpu_T
[0], cpu_regs
[R_EDX
]);
6286 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6287 svm_is_rep(prefixes
) | 4);
6288 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6289 gen_repz_outs(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6293 gen_jmp(s
, s
->pc
- s
->cs_base
);
6298 /************************/
6303 ot
= mo_b_d32(b
, dflag
);
6304 val
= cpu_ldub_code(env
, s
->pc
++);
6305 tcg_gen_movi_tl(cpu_T
[0], val
);
6306 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6307 SVM_IOIO_TYPE_MASK
| svm_is_rep(prefixes
));
6310 tcg_gen_movi_i32(cpu_tmp2_i32
, val
);
6311 gen_helper_in_func(ot
, cpu_T
[1], cpu_tmp2_i32
);
6312 gen_op_mov_reg_v(ot
, R_EAX
, cpu_T
[1]);
6315 gen_jmp(s
, s
->pc
- s
->cs_base
);
6320 ot
= mo_b_d32(b
, dflag
);
6321 val
= cpu_ldub_code(env
, s
->pc
++);
6322 tcg_gen_movi_tl(cpu_T
[0], val
);
6323 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6324 svm_is_rep(prefixes
));
6325 gen_op_mov_v_reg(ot
, cpu_T
[1], R_EAX
);
6329 tcg_gen_movi_i32(cpu_tmp2_i32
, val
);
6330 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[1]);
6331 gen_helper_out_func(ot
, cpu_tmp2_i32
, cpu_tmp3_i32
);
6334 gen_jmp(s
, s
->pc
- s
->cs_base
);
6339 ot
= mo_b_d32(b
, dflag
);
6340 tcg_gen_ext16u_tl(cpu_T
[0], cpu_regs
[R_EDX
]);
6341 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6342 SVM_IOIO_TYPE_MASK
| svm_is_rep(prefixes
));
6345 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
6346 gen_helper_in_func(ot
, cpu_T
[1], cpu_tmp2_i32
);
6347 gen_op_mov_reg_v(ot
, R_EAX
, cpu_T
[1]);
6350 gen_jmp(s
, s
->pc
- s
->cs_base
);
6355 ot
= mo_b_d32(b
, dflag
);
6356 tcg_gen_ext16u_tl(cpu_T
[0], cpu_regs
[R_EDX
]);
6357 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6358 svm_is_rep(prefixes
));
6359 gen_op_mov_v_reg(ot
, cpu_T
[1], R_EAX
);
6363 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
6364 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[1]);
6365 gen_helper_out_func(ot
, cpu_tmp2_i32
, cpu_tmp3_i32
);
6368 gen_jmp(s
, s
->pc
- s
->cs_base
);
6372 /************************/
6374 case 0xc2: /* ret im */
6375 val
= cpu_ldsw_code(env
, s
->pc
);
6378 gen_stack_update(s
, val
+ (1 << ot
));
6379 /* Note that gen_pop_T0 uses a zero-extending load. */
6380 gen_op_jmp_v(cpu_T
[0]);
6383 case 0xc3: /* ret */
6385 gen_pop_update(s
, ot
);
6386 /* Note that gen_pop_T0 uses a zero-extending load. */
6387 gen_op_jmp_v(cpu_T
[0]);
6390 case 0xca: /* lret im */
6391 val
= cpu_ldsw_code(env
, s
->pc
);
6394 if (s
->pe
&& !s
->vm86
) {
6395 gen_update_cc_op(s
);
6396 gen_jmp_im(pc_start
- s
->cs_base
);
6397 gen_helper_lret_protected(cpu_env
, tcg_const_i32(dflag
- 1),
6398 tcg_const_i32(val
));
6402 gen_op_ld_v(s
, dflag
, cpu_T
[0], cpu_A0
);
6403 /* NOTE: keeping EIP updated is not a problem in case of
6405 gen_op_jmp_v(cpu_T
[0]);
6407 gen_op_addl_A0_im(1 << dflag
);
6408 gen_op_ld_v(s
, dflag
, cpu_T
[0], cpu_A0
);
6409 gen_op_movl_seg_T0_vm(R_CS
);
6410 /* add stack offset */
6411 gen_stack_update(s
, val
+ (2 << dflag
));
6415 case 0xcb: /* lret */
6418 case 0xcf: /* iret */
6419 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_IRET
);
6422 gen_helper_iret_real(cpu_env
, tcg_const_i32(dflag
- 1));
6423 set_cc_op(s
, CC_OP_EFLAGS
);
6424 } else if (s
->vm86
) {
6426 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6428 gen_helper_iret_real(cpu_env
, tcg_const_i32(dflag
- 1));
6429 set_cc_op(s
, CC_OP_EFLAGS
);
6432 gen_update_cc_op(s
);
6433 gen_jmp_im(pc_start
- s
->cs_base
);
6434 gen_helper_iret_protected(cpu_env
, tcg_const_i32(dflag
- 1),
6435 tcg_const_i32(s
->pc
- s
->cs_base
));
6436 set_cc_op(s
, CC_OP_EFLAGS
);
6440 case 0xe8: /* call im */
6442 if (dflag
!= MO_16
) {
6443 tval
= (int32_t)insn_get(env
, s
, MO_32
);
6445 tval
= (int16_t)insn_get(env
, s
, MO_16
);
6447 next_eip
= s
->pc
- s
->cs_base
;
6449 if (dflag
== MO_16
) {
6451 } else if (!CODE64(s
)) {
6454 tcg_gen_movi_tl(cpu_T
[0], next_eip
);
6455 gen_push_v(s
, cpu_T
[0]);
6459 case 0x9a: /* lcall im */
6461 unsigned int selector
, offset
;
6466 offset
= insn_get(env
, s
, ot
);
6467 selector
= insn_get(env
, s
, MO_16
);
6469 tcg_gen_movi_tl(cpu_T
[0], selector
);
6470 tcg_gen_movi_tl(cpu_T
[1], offset
);
6473 case 0xe9: /* jmp im */
6474 if (dflag
!= MO_16
) {
6475 tval
= (int32_t)insn_get(env
, s
, MO_32
);
6477 tval
= (int16_t)insn_get(env
, s
, MO_16
);
6479 tval
+= s
->pc
- s
->cs_base
;
6480 if (dflag
== MO_16
) {
6482 } else if (!CODE64(s
)) {
6487 case 0xea: /* ljmp im */
6489 unsigned int selector
, offset
;
6494 offset
= insn_get(env
, s
, ot
);
6495 selector
= insn_get(env
, s
, MO_16
);
6497 tcg_gen_movi_tl(cpu_T
[0], selector
);
6498 tcg_gen_movi_tl(cpu_T
[1], offset
);
6501 case 0xeb: /* jmp Jb */
6502 tval
= (int8_t)insn_get(env
, s
, MO_8
);
6503 tval
+= s
->pc
- s
->cs_base
;
6504 if (dflag
== MO_16
) {
6509 case 0x70 ... 0x7f: /* jcc Jb */
6510 tval
= (int8_t)insn_get(env
, s
, MO_8
);
6512 case 0x180 ... 0x18f: /* jcc Jv */
6513 if (dflag
!= MO_16
) {
6514 tval
= (int32_t)insn_get(env
, s
, MO_32
);
6516 tval
= (int16_t)insn_get(env
, s
, MO_16
);
6519 next_eip
= s
->pc
- s
->cs_base
;
6521 if (dflag
== MO_16
) {
6524 gen_jcc(s
, b
, tval
, next_eip
);
6527 case 0x190 ... 0x19f: /* setcc Gv */
6528 modrm
= cpu_ldub_code(env
, s
->pc
++);
6529 gen_setcc1(s
, b
, cpu_T
[0]);
6530 gen_ldst_modrm(env
, s
, modrm
, MO_8
, OR_TMP0
, 1);
6532 case 0x140 ... 0x14f: /* cmov Gv, Ev */
6533 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6537 modrm
= cpu_ldub_code(env
, s
->pc
++);
6538 reg
= ((modrm
>> 3) & 7) | rex_r
;
6539 gen_cmovcc1(env
, s
, ot
, b
, modrm
, reg
);
6542 /************************/
6544 case 0x9c: /* pushf */
6545 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_PUSHF
);
6546 if (s
->vm86
&& s
->iopl
!= 3) {
6547 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6549 gen_update_cc_op(s
);
6550 gen_helper_read_eflags(cpu_T
[0], cpu_env
);
6551 gen_push_v(s
, cpu_T
[0]);
6554 case 0x9d: /* popf */
6555 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_POPF
);
6556 if (s
->vm86
&& s
->iopl
!= 3) {
6557 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6561 if (dflag
!= MO_16
) {
6562 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6563 tcg_const_i32((TF_MASK
| AC_MASK
|
6568 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6569 tcg_const_i32((TF_MASK
| AC_MASK
|
6571 IF_MASK
| IOPL_MASK
)
6575 if (s
->cpl
<= s
->iopl
) {
6576 if (dflag
!= MO_16
) {
6577 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6578 tcg_const_i32((TF_MASK
|
6584 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6585 tcg_const_i32((TF_MASK
|
6593 if (dflag
!= MO_16
) {
6594 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6595 tcg_const_i32((TF_MASK
| AC_MASK
|
6596 ID_MASK
| NT_MASK
)));
6598 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6599 tcg_const_i32((TF_MASK
| AC_MASK
|
6605 gen_pop_update(s
, ot
);
6606 set_cc_op(s
, CC_OP_EFLAGS
);
6607 /* abort translation because TF/AC flag may change */
6608 gen_jmp_im(s
->pc
- s
->cs_base
);
6612 case 0x9e: /* sahf */
6613 if (CODE64(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_LAHF_LM
))
6615 gen_op_mov_v_reg(MO_8
, cpu_T
[0], R_AH
);
6616 gen_compute_eflags(s
);
6617 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, CC_O
);
6618 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
6619 tcg_gen_or_tl(cpu_cc_src
, cpu_cc_src
, cpu_T
[0]);
6621 case 0x9f: /* lahf */
6622 if (CODE64(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_LAHF_LM
))
6624 gen_compute_eflags(s
);
6625 /* Note: gen_compute_eflags() only gives the condition codes */
6626 tcg_gen_ori_tl(cpu_T
[0], cpu_cc_src
, 0x02);
6627 gen_op_mov_reg_v(MO_8
, R_AH
, cpu_T
[0]);
6629 case 0xf5: /* cmc */
6630 gen_compute_eflags(s
);
6631 tcg_gen_xori_tl(cpu_cc_src
, cpu_cc_src
, CC_C
);
6633 case 0xf8: /* clc */
6634 gen_compute_eflags(s
);
6635 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, ~CC_C
);
6637 case 0xf9: /* stc */
6638 gen_compute_eflags(s
);
6639 tcg_gen_ori_tl(cpu_cc_src
, cpu_cc_src
, CC_C
);
6641 case 0xfc: /* cld */
6642 tcg_gen_movi_i32(cpu_tmp2_i32
, 1);
6643 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
, offsetof(CPUX86State
, df
));
6645 case 0xfd: /* std */
6646 tcg_gen_movi_i32(cpu_tmp2_i32
, -1);
6647 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
, offsetof(CPUX86State
, df
));
6650 /************************/
6651 /* bit operations */
6652 case 0x1ba: /* bt/bts/btr/btc Gv, im */
6654 modrm
= cpu_ldub_code(env
, s
->pc
++);
6655 op
= (modrm
>> 3) & 7;
6656 mod
= (modrm
>> 6) & 3;
6657 rm
= (modrm
& 7) | REX_B(s
);
6660 gen_lea_modrm(env
, s
, modrm
);
6661 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
6663 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
6666 val
= cpu_ldub_code(env
, s
->pc
++);
6667 tcg_gen_movi_tl(cpu_T
[1], val
);
6672 case 0x1a3: /* bt Gv, Ev */
6675 case 0x1ab: /* bts */
6678 case 0x1b3: /* btr */
6681 case 0x1bb: /* btc */
6685 modrm
= cpu_ldub_code(env
, s
->pc
++);
6686 reg
= ((modrm
>> 3) & 7) | rex_r
;
6687 mod
= (modrm
>> 6) & 3;
6688 rm
= (modrm
& 7) | REX_B(s
);
6689 gen_op_mov_v_reg(MO_32
, cpu_T
[1], reg
);
6691 gen_lea_modrm(env
, s
, modrm
);
6692 /* specific case: we need to add a displacement */
6693 gen_exts(ot
, cpu_T
[1]);
6694 tcg_gen_sari_tl(cpu_tmp0
, cpu_T
[1], 3 + ot
);
6695 tcg_gen_shli_tl(cpu_tmp0
, cpu_tmp0
, ot
);
6696 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
6697 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
6699 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
6702 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], (1 << (3 + ot
)) - 1);
6703 tcg_gen_shr_tl(cpu_tmp4
, cpu_T
[0], cpu_T
[1]);
6708 tcg_gen_movi_tl(cpu_tmp0
, 1);
6709 tcg_gen_shl_tl(cpu_tmp0
, cpu_tmp0
, cpu_T
[1]);
6710 tcg_gen_or_tl(cpu_T
[0], cpu_T
[0], cpu_tmp0
);
6713 tcg_gen_movi_tl(cpu_tmp0
, 1);
6714 tcg_gen_shl_tl(cpu_tmp0
, cpu_tmp0
, cpu_T
[1]);
6715 tcg_gen_andc_tl(cpu_T
[0], cpu_T
[0], cpu_tmp0
);
6719 tcg_gen_movi_tl(cpu_tmp0
, 1);
6720 tcg_gen_shl_tl(cpu_tmp0
, cpu_tmp0
, cpu_T
[1]);
6721 tcg_gen_xor_tl(cpu_T
[0], cpu_T
[0], cpu_tmp0
);
6726 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
6728 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
6732 /* Delay all CC updates until after the store above. Note that
6733 C is the result of the test, Z is unchanged, and the others
6734 are all undefined. */
6736 case CC_OP_MULB
... CC_OP_MULQ
:
6737 case CC_OP_ADDB
... CC_OP_ADDQ
:
6738 case CC_OP_ADCB
... CC_OP_ADCQ
:
6739 case CC_OP_SUBB
... CC_OP_SUBQ
:
6740 case CC_OP_SBBB
... CC_OP_SBBQ
:
6741 case CC_OP_LOGICB
... CC_OP_LOGICQ
:
6742 case CC_OP_INCB
... CC_OP_INCQ
:
6743 case CC_OP_DECB
... CC_OP_DECQ
:
6744 case CC_OP_SHLB
... CC_OP_SHLQ
:
6745 case CC_OP_SARB
... CC_OP_SARQ
:
6746 case CC_OP_BMILGB
... CC_OP_BMILGQ
:
6747 /* Z was going to be computed from the non-zero status of CC_DST.
6748 We can get that same Z value (and the new C value) by leaving
6749 CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
6751 tcg_gen_mov_tl(cpu_cc_src
, cpu_tmp4
);
6752 set_cc_op(s
, ((s
->cc_op
- CC_OP_MULB
) & 3) + CC_OP_SARB
);
6755 /* Otherwise, generate EFLAGS and replace the C bit. */
6756 gen_compute_eflags(s
);
6757 tcg_gen_deposit_tl(cpu_cc_src
, cpu_cc_src
, cpu_tmp4
,
6762 case 0x1bc: /* bsf / tzcnt */
6763 case 0x1bd: /* bsr / lzcnt */
6765 modrm
= cpu_ldub_code(env
, s
->pc
++);
6766 reg
= ((modrm
>> 3) & 7) | rex_r
;
6767 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
6768 gen_extu(ot
, cpu_T
[0]);
6770 /* Note that lzcnt and tzcnt are in different extensions. */
6771 if ((prefixes
& PREFIX_REPZ
)
6773 ? s
->cpuid_ext3_features
& CPUID_EXT3_ABM
6774 : s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)) {
6776 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[0]);
6778 /* For lzcnt, reduce the target_ulong result by the
6779 number of zeros that we expect to find at the top. */
6780 gen_helper_clz(cpu_T
[0], cpu_T
[0]);
6781 tcg_gen_subi_tl(cpu_T
[0], cpu_T
[0], TARGET_LONG_BITS
- size
);
6783 /* For tzcnt, a zero input must return the operand size:
6784 force all bits outside the operand size to 1. */
6785 target_ulong mask
= (target_ulong
)-2 << (size
- 1);
6786 tcg_gen_ori_tl(cpu_T
[0], cpu_T
[0], mask
);
6787 gen_helper_ctz(cpu_T
[0], cpu_T
[0]);
6789 /* For lzcnt/tzcnt, C and Z bits are defined and are
6790 related to the result. */
6791 gen_op_update1_cc();
6792 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
6794 /* For bsr/bsf, only the Z bit is defined and it is related
6795 to the input and not the result. */
6796 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
6797 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
6799 /* For bsr, return the bit index of the first 1 bit,
6800 not the count of leading zeros. */
6801 gen_helper_clz(cpu_T
[0], cpu_T
[0]);
6802 tcg_gen_xori_tl(cpu_T
[0], cpu_T
[0], TARGET_LONG_BITS
- 1);
6804 gen_helper_ctz(cpu_T
[0], cpu_T
[0]);
6806 /* ??? The manual says that the output is undefined when the
6807 input is zero, but real hardware leaves it unchanged, and
6808 real programs appear to depend on that. */
6809 tcg_gen_movi_tl(cpu_tmp0
, 0);
6810 tcg_gen_movcond_tl(TCG_COND_EQ
, cpu_T
[0], cpu_cc_dst
, cpu_tmp0
,
6811 cpu_regs
[reg
], cpu_T
[0]);
6813 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
6815 /************************/
6817 case 0x27: /* daa */
6820 gen_update_cc_op(s
);
6821 gen_helper_daa(cpu_env
);
6822 set_cc_op(s
, CC_OP_EFLAGS
);
6824 case 0x2f: /* das */
6827 gen_update_cc_op(s
);
6828 gen_helper_das(cpu_env
);
6829 set_cc_op(s
, CC_OP_EFLAGS
);
6831 case 0x37: /* aaa */
6834 gen_update_cc_op(s
);
6835 gen_helper_aaa(cpu_env
);
6836 set_cc_op(s
, CC_OP_EFLAGS
);
6838 case 0x3f: /* aas */
6841 gen_update_cc_op(s
);
6842 gen_helper_aas(cpu_env
);
6843 set_cc_op(s
, CC_OP_EFLAGS
);
6845 case 0xd4: /* aam */
6848 val
= cpu_ldub_code(env
, s
->pc
++);
6850 gen_exception(s
, EXCP00_DIVZ
, pc_start
- s
->cs_base
);
6852 gen_helper_aam(cpu_env
, tcg_const_i32(val
));
6853 set_cc_op(s
, CC_OP_LOGICB
);
6856 case 0xd5: /* aad */
6859 val
= cpu_ldub_code(env
, s
->pc
++);
6860 gen_helper_aad(cpu_env
, tcg_const_i32(val
));
6861 set_cc_op(s
, CC_OP_LOGICB
);
6863 /************************/
6865 case 0x90: /* nop */
6866 /* XXX: correct lock test for all insn */
6867 if (prefixes
& PREFIX_LOCK
) {
6870 /* If REX_B is set, then this is xchg eax, r8d, not a nop. */
6872 goto do_xchg_reg_eax
;
6874 if (prefixes
& PREFIX_REPZ
) {
6875 gen_update_cc_op(s
);
6876 gen_jmp_im(pc_start
- s
->cs_base
);
6877 gen_helper_pause(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
6878 s
->is_jmp
= DISAS_TB_JUMP
;
6881 case 0x9b: /* fwait */
6882 if ((s
->flags
& (HF_MP_MASK
| HF_TS_MASK
)) ==
6883 (HF_MP_MASK
| HF_TS_MASK
)) {
6884 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
6886 gen_update_cc_op(s
);
6887 gen_jmp_im(pc_start
- s
->cs_base
);
6888 gen_helper_fwait(cpu_env
);
6891 case 0xcc: /* int3 */
6892 gen_interrupt(s
, EXCP03_INT3
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6894 case 0xcd: /* int N */
6895 val
= cpu_ldub_code(env
, s
->pc
++);
6896 if (s
->vm86
&& s
->iopl
!= 3) {
6897 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6899 gen_interrupt(s
, val
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6902 case 0xce: /* into */
6905 gen_update_cc_op(s
);
6906 gen_jmp_im(pc_start
- s
->cs_base
);
6907 gen_helper_into(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
6910 case 0xf1: /* icebp (undocumented, exits to external debugger) */
6911 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_ICEBP
);
6913 gen_debug(s
, pc_start
- s
->cs_base
);
6917 qemu_set_log(CPU_LOG_INT
| CPU_LOG_TB_IN_ASM
);
6921 case 0xfa: /* cli */
6923 if (s
->cpl
<= s
->iopl
) {
6924 gen_helper_cli(cpu_env
);
6926 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6930 gen_helper_cli(cpu_env
);
6932 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6936 case 0xfb: /* sti */
6938 if (s
->cpl
<= s
->iopl
) {
6940 gen_helper_sti(cpu_env
);
6941 /* interruptions are enabled only the first insn after sti */
6942 /* If several instructions disable interrupts, only the
6944 if (!(s
->tb
->flags
& HF_INHIBIT_IRQ_MASK
))
6945 gen_helper_set_inhibit_irq(cpu_env
);
6946 /* give a chance to handle pending irqs */
6947 gen_jmp_im(s
->pc
- s
->cs_base
);
6950 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6956 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6960 case 0x62: /* bound */
6964 modrm
= cpu_ldub_code(env
, s
->pc
++);
6965 reg
= (modrm
>> 3) & 7;
6966 mod
= (modrm
>> 6) & 3;
6969 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
6970 gen_lea_modrm(env
, s
, modrm
);
6971 gen_jmp_im(pc_start
- s
->cs_base
);
6972 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
6974 gen_helper_boundw(cpu_env
, cpu_A0
, cpu_tmp2_i32
);
6976 gen_helper_boundl(cpu_env
, cpu_A0
, cpu_tmp2_i32
);
6979 case 0x1c8 ... 0x1cf: /* bswap reg */
6980 reg
= (b
& 7) | REX_B(s
);
6981 #ifdef TARGET_X86_64
6982 if (dflag
== MO_64
) {
6983 gen_op_mov_v_reg(MO_64
, cpu_T
[0], reg
);
6984 tcg_gen_bswap64_i64(cpu_T
[0], cpu_T
[0]);
6985 gen_op_mov_reg_v(MO_64
, reg
, cpu_T
[0]);
6989 gen_op_mov_v_reg(MO_32
, cpu_T
[0], reg
);
6990 tcg_gen_ext32u_tl(cpu_T
[0], cpu_T
[0]);
6991 tcg_gen_bswap32_tl(cpu_T
[0], cpu_T
[0]);
6992 gen_op_mov_reg_v(MO_32
, reg
, cpu_T
[0]);
6995 case 0xd6: /* salc */
6998 gen_compute_eflags_c(s
, cpu_T
[0]);
6999 tcg_gen_neg_tl(cpu_T
[0], cpu_T
[0]);
7000 gen_op_mov_reg_v(MO_8
, R_EAX
, cpu_T
[0]);
7002 case 0xe0: /* loopnz */
7003 case 0xe1: /* loopz */
7004 case 0xe2: /* loop */
7005 case 0xe3: /* jecxz */
7009 tval
= (int8_t)insn_get(env
, s
, MO_8
);
7010 next_eip
= s
->pc
- s
->cs_base
;
7012 if (dflag
== MO_16
) {
7016 l1
= gen_new_label();
7017 l2
= gen_new_label();
7018 l3
= gen_new_label();
7021 case 0: /* loopnz */
7023 gen_op_add_reg_im(s
->aflag
, R_ECX
, -1);
7024 gen_op_jz_ecx(s
->aflag
, l3
);
7025 gen_jcc1(s
, (JCC_Z
<< 1) | (b
^ 1), l1
);
7028 gen_op_add_reg_im(s
->aflag
, R_ECX
, -1);
7029 gen_op_jnz_ecx(s
->aflag
, l1
);
7033 gen_op_jz_ecx(s
->aflag
, l1
);
7038 gen_jmp_im(next_eip
);
7047 case 0x130: /* wrmsr */
7048 case 0x132: /* rdmsr */
7050 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7052 gen_update_cc_op(s
);
7053 gen_jmp_im(pc_start
- s
->cs_base
);
7055 gen_helper_rdmsr(cpu_env
);
7057 gen_helper_wrmsr(cpu_env
);
7061 case 0x131: /* rdtsc */
7062 gen_update_cc_op(s
);
7063 gen_jmp_im(pc_start
- s
->cs_base
);
7066 gen_helper_rdtsc(cpu_env
);
7069 gen_jmp(s
, s
->pc
- s
->cs_base
);
7072 case 0x133: /* rdpmc */
7073 gen_update_cc_op(s
);
7074 gen_jmp_im(pc_start
- s
->cs_base
);
7075 gen_helper_rdpmc(cpu_env
);
7077 case 0x134: /* sysenter */
7078 /* For Intel SYSENTER is valid on 64-bit */
7079 if (CODE64(s
) && env
->cpuid_vendor1
!= CPUID_VENDOR_INTEL_1
)
7082 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7084 gen_update_cc_op(s
);
7085 gen_jmp_im(pc_start
- s
->cs_base
);
7086 gen_helper_sysenter(cpu_env
);
7090 case 0x135: /* sysexit */
7091 /* For Intel SYSEXIT is valid on 64-bit */
7092 if (CODE64(s
) && env
->cpuid_vendor1
!= CPUID_VENDOR_INTEL_1
)
7095 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7097 gen_update_cc_op(s
);
7098 gen_jmp_im(pc_start
- s
->cs_base
);
7099 gen_helper_sysexit(cpu_env
, tcg_const_i32(dflag
- 1));
7103 #ifdef TARGET_X86_64
7104 case 0x105: /* syscall */
7105 /* XXX: is it usable in real mode ? */
7106 gen_update_cc_op(s
);
7107 gen_jmp_im(pc_start
- s
->cs_base
);
7108 gen_helper_syscall(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7111 case 0x107: /* sysret */
7113 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7115 gen_update_cc_op(s
);
7116 gen_jmp_im(pc_start
- s
->cs_base
);
7117 gen_helper_sysret(cpu_env
, tcg_const_i32(dflag
- 1));
7118 /* condition codes are modified only in long mode */
7120 set_cc_op(s
, CC_OP_EFLAGS
);
7126 case 0x1a2: /* cpuid */
7127 gen_update_cc_op(s
);
7128 gen_jmp_im(pc_start
- s
->cs_base
);
7129 gen_helper_cpuid(cpu_env
);
7131 case 0xf4: /* hlt */
7133 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7135 gen_update_cc_op(s
);
7136 gen_jmp_im(pc_start
- s
->cs_base
);
7137 gen_helper_hlt(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7138 s
->is_jmp
= DISAS_TB_JUMP
;
7142 modrm
= cpu_ldub_code(env
, s
->pc
++);
7143 mod
= (modrm
>> 6) & 3;
7144 op
= (modrm
>> 3) & 7;
7147 if (!s
->pe
|| s
->vm86
)
7149 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_LDTR_READ
);
7150 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,ldt
.selector
));
7151 ot
= mod
== 3 ? dflag
: MO_16
;
7152 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
7155 if (!s
->pe
|| s
->vm86
)
7158 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7160 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_LDTR_WRITE
);
7161 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7162 gen_jmp_im(pc_start
- s
->cs_base
);
7163 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
7164 gen_helper_lldt(cpu_env
, cpu_tmp2_i32
);
7168 if (!s
->pe
|| s
->vm86
)
7170 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_TR_READ
);
7171 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,tr
.selector
));
7172 ot
= mod
== 3 ? dflag
: MO_16
;
7173 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
7176 if (!s
->pe
|| s
->vm86
)
7179 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7181 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_TR_WRITE
);
7182 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7183 gen_jmp_im(pc_start
- s
->cs_base
);
7184 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
7185 gen_helper_ltr(cpu_env
, cpu_tmp2_i32
);
7190 if (!s
->pe
|| s
->vm86
)
7192 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7193 gen_update_cc_op(s
);
7195 gen_helper_verr(cpu_env
, cpu_T
[0]);
7197 gen_helper_verw(cpu_env
, cpu_T
[0]);
7199 set_cc_op(s
, CC_OP_EFLAGS
);
7206 modrm
= cpu_ldub_code(env
, s
->pc
++);
7207 mod
= (modrm
>> 6) & 3;
7208 op
= (modrm
>> 3) & 7;
7214 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_GDTR_READ
);
7215 gen_lea_modrm(env
, s
, modrm
);
7216 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, gdt
.limit
));
7217 gen_op_st_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
7218 gen_add_A0_im(s
, 2);
7219 tcg_gen_ld_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, gdt
.base
));
7220 if (dflag
== MO_16
) {
7221 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 0xffffff);
7223 gen_op_st_v(s
, CODE64(s
) + MO_32
, cpu_T
[0], cpu_A0
);
7228 case 0: /* monitor */
7229 if (!(s
->cpuid_ext_features
& CPUID_EXT_MONITOR
) ||
7232 gen_update_cc_op(s
);
7233 gen_jmp_im(pc_start
- s
->cs_base
);
7234 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[R_EAX
]);
7235 gen_extu(s
->aflag
, cpu_A0
);
7236 gen_add_A0_ds_seg(s
);
7237 gen_helper_monitor(cpu_env
, cpu_A0
);
7240 if (!(s
->cpuid_ext_features
& CPUID_EXT_MONITOR
) ||
7243 gen_update_cc_op(s
);
7244 gen_jmp_im(pc_start
- s
->cs_base
);
7245 gen_helper_mwait(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7249 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_SMAP
) ||
7253 gen_helper_clac(cpu_env
);
7254 gen_jmp_im(s
->pc
- s
->cs_base
);
7258 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_SMAP
) ||
7262 gen_helper_stac(cpu_env
);
7263 gen_jmp_im(s
->pc
- s
->cs_base
);
7270 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_IDTR_READ
);
7271 gen_lea_modrm(env
, s
, modrm
);
7272 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, idt
.limit
));
7273 gen_op_st_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
7274 gen_add_A0_im(s
, 2);
7275 tcg_gen_ld_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, idt
.base
));
7276 if (dflag
== MO_16
) {
7277 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 0xffffff);
7279 gen_op_st_v(s
, CODE64(s
) + MO_32
, cpu_T
[0], cpu_A0
);
7285 gen_update_cc_op(s
);
7286 gen_jmp_im(pc_start
- s
->cs_base
);
7289 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7292 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7295 gen_helper_vmrun(cpu_env
, tcg_const_i32(s
->aflag
- 1),
7296 tcg_const_i32(s
->pc
- pc_start
));
7298 s
->is_jmp
= DISAS_TB_JUMP
;
7301 case 1: /* VMMCALL */
7302 if (!(s
->flags
& HF_SVME_MASK
))
7304 gen_helper_vmmcall(cpu_env
);
7306 case 2: /* VMLOAD */
7307 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7310 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7313 gen_helper_vmload(cpu_env
, tcg_const_i32(s
->aflag
- 1));
7316 case 3: /* VMSAVE */
7317 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7320 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7323 gen_helper_vmsave(cpu_env
, tcg_const_i32(s
->aflag
- 1));
7327 if ((!(s
->flags
& HF_SVME_MASK
) &&
7328 !(s
->cpuid_ext3_features
& CPUID_EXT3_SKINIT
)) ||
7332 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7335 gen_helper_stgi(cpu_env
);
7339 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7342 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7345 gen_helper_clgi(cpu_env
);
7348 case 6: /* SKINIT */
7349 if ((!(s
->flags
& HF_SVME_MASK
) &&
7350 !(s
->cpuid_ext3_features
& CPUID_EXT3_SKINIT
)) ||
7353 gen_helper_skinit(cpu_env
);
7355 case 7: /* INVLPGA */
7356 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7359 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7362 gen_helper_invlpga(cpu_env
,
7363 tcg_const_i32(s
->aflag
- 1));
7369 } else if (s
->cpl
!= 0) {
7370 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7372 gen_svm_check_intercept(s
, pc_start
,
7373 op
==2 ? SVM_EXIT_GDTR_WRITE
: SVM_EXIT_IDTR_WRITE
);
7374 gen_lea_modrm(env
, s
, modrm
);
7375 gen_op_ld_v(s
, MO_16
, cpu_T
[1], cpu_A0
);
7376 gen_add_A0_im(s
, 2);
7377 gen_op_ld_v(s
, CODE64(s
) + MO_32
, cpu_T
[0], cpu_A0
);
7378 if (dflag
== MO_16
) {
7379 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 0xffffff);
7382 tcg_gen_st_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,gdt
.base
));
7383 tcg_gen_st32_tl(cpu_T
[1], cpu_env
, offsetof(CPUX86State
,gdt
.limit
));
7385 tcg_gen_st_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,idt
.base
));
7386 tcg_gen_st32_tl(cpu_T
[1], cpu_env
, offsetof(CPUX86State
,idt
.limit
));
7391 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_READ_CR0
);
7392 #if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
7393 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,cr
[0]) + 4);
7395 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,cr
[0]));
7397 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 1);
7401 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7403 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_WRITE_CR0
);
7404 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7405 gen_helper_lmsw(cpu_env
, cpu_T
[0]);
7406 gen_jmp_im(s
->pc
- s
->cs_base
);
7411 if (mod
!= 3) { /* invlpg */
7413 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7415 gen_update_cc_op(s
);
7416 gen_jmp_im(pc_start
- s
->cs_base
);
7417 gen_lea_modrm(env
, s
, modrm
);
7418 gen_helper_invlpg(cpu_env
, cpu_A0
);
7419 gen_jmp_im(s
->pc
- s
->cs_base
);
7424 case 0: /* swapgs */
7425 #ifdef TARGET_X86_64
7428 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7430 tcg_gen_ld_tl(cpu_T
[0], cpu_env
,
7431 offsetof(CPUX86State
,segs
[R_GS
].base
));
7432 tcg_gen_ld_tl(cpu_T
[1], cpu_env
,
7433 offsetof(CPUX86State
,kernelgsbase
));
7434 tcg_gen_st_tl(cpu_T
[1], cpu_env
,
7435 offsetof(CPUX86State
,segs
[R_GS
].base
));
7436 tcg_gen_st_tl(cpu_T
[0], cpu_env
,
7437 offsetof(CPUX86State
,kernelgsbase
));
7445 case 1: /* rdtscp */
7446 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_RDTSCP
))
7448 gen_update_cc_op(s
);
7449 gen_jmp_im(pc_start
- s
->cs_base
);
7452 gen_helper_rdtscp(cpu_env
);
7455 gen_jmp(s
, s
->pc
- s
->cs_base
);
7467 case 0x108: /* invd */
7468 case 0x109: /* wbinvd */
7470 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7472 gen_svm_check_intercept(s
, pc_start
, (b
& 2) ? SVM_EXIT_INVD
: SVM_EXIT_WBINVD
);
7476 case 0x63: /* arpl or movslS (x86_64) */
7477 #ifdef TARGET_X86_64
7480 /* d_ot is the size of destination */
7483 modrm
= cpu_ldub_code(env
, s
->pc
++);
7484 reg
= ((modrm
>> 3) & 7) | rex_r
;
7485 mod
= (modrm
>> 6) & 3;
7486 rm
= (modrm
& 7) | REX_B(s
);
7489 gen_op_mov_v_reg(MO_32
, cpu_T
[0], rm
);
7491 if (d_ot
== MO_64
) {
7492 tcg_gen_ext32s_tl(cpu_T
[0], cpu_T
[0]);
7494 gen_op_mov_reg_v(d_ot
, reg
, cpu_T
[0]);
7496 gen_lea_modrm(env
, s
, modrm
);
7497 gen_op_ld_v(s
, MO_32
| MO_SIGN
, cpu_T
[0], cpu_A0
);
7498 gen_op_mov_reg_v(d_ot
, reg
, cpu_T
[0]);
7504 TCGv t0
, t1
, t2
, a0
;
7506 if (!s
->pe
|| s
->vm86
)
7508 t0
= tcg_temp_local_new();
7509 t1
= tcg_temp_local_new();
7510 t2
= tcg_temp_local_new();
7512 modrm
= cpu_ldub_code(env
, s
->pc
++);
7513 reg
= (modrm
>> 3) & 7;
7514 mod
= (modrm
>> 6) & 3;
7517 gen_lea_modrm(env
, s
, modrm
);
7518 gen_op_ld_v(s
, ot
, t0
, cpu_A0
);
7519 a0
= tcg_temp_local_new();
7520 tcg_gen_mov_tl(a0
, cpu_A0
);
7522 gen_op_mov_v_reg(ot
, t0
, rm
);
7525 gen_op_mov_v_reg(ot
, t1
, reg
);
7526 tcg_gen_andi_tl(cpu_tmp0
, t0
, 3);
7527 tcg_gen_andi_tl(t1
, t1
, 3);
7528 tcg_gen_movi_tl(t2
, 0);
7529 label1
= gen_new_label();
7530 tcg_gen_brcond_tl(TCG_COND_GE
, cpu_tmp0
, t1
, label1
);
7531 tcg_gen_andi_tl(t0
, t0
, ~3);
7532 tcg_gen_or_tl(t0
, t0
, t1
);
7533 tcg_gen_movi_tl(t2
, CC_Z
);
7534 gen_set_label(label1
);
7536 gen_op_st_v(s
, ot
, t0
, a0
);
7539 gen_op_mov_reg_v(ot
, rm
, t0
);
7541 gen_compute_eflags(s
);
7542 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, ~CC_Z
);
7543 tcg_gen_or_tl(cpu_cc_src
, cpu_cc_src
, t2
);
7549 case 0x102: /* lar */
7550 case 0x103: /* lsl */
7554 if (!s
->pe
|| s
->vm86
)
7556 ot
= dflag
!= MO_16
? MO_32
: MO_16
;
7557 modrm
= cpu_ldub_code(env
, s
->pc
++);
7558 reg
= ((modrm
>> 3) & 7) | rex_r
;
7559 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7560 t0
= tcg_temp_local_new();
7561 gen_update_cc_op(s
);
7563 gen_helper_lar(t0
, cpu_env
, cpu_T
[0]);
7565 gen_helper_lsl(t0
, cpu_env
, cpu_T
[0]);
7567 tcg_gen_andi_tl(cpu_tmp0
, cpu_cc_src
, CC_Z
);
7568 label1
= gen_new_label();
7569 tcg_gen_brcondi_tl(TCG_COND_EQ
, cpu_tmp0
, 0, label1
);
7570 gen_op_mov_reg_v(ot
, reg
, t0
);
7571 gen_set_label(label1
);
7572 set_cc_op(s
, CC_OP_EFLAGS
);
7577 modrm
= cpu_ldub_code(env
, s
->pc
++);
7578 mod
= (modrm
>> 6) & 3;
7579 op
= (modrm
>> 3) & 7;
7581 case 0: /* prefetchnta */
7582 case 1: /* prefetchnt0 */
7583 case 2: /* prefetchnt0 */
7584 case 3: /* prefetchnt0 */
7587 gen_lea_modrm(env
, s
, modrm
);
7588 /* nothing more to do */
7590 default: /* nop (multi byte) */
7591 gen_nop_modrm(env
, s
, modrm
);
7595 case 0x119 ... 0x11f: /* nop (multi byte) */
7596 modrm
= cpu_ldub_code(env
, s
->pc
++);
7597 gen_nop_modrm(env
, s
, modrm
);
7599 case 0x120: /* mov reg, crN */
7600 case 0x122: /* mov crN, reg */
7602 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7604 modrm
= cpu_ldub_code(env
, s
->pc
++);
7605 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7606 * AMD documentation (24594.pdf) and testing of
7607 * intel 386 and 486 processors all show that the mod bits
7608 * are assumed to be 1's, regardless of actual values.
7610 rm
= (modrm
& 7) | REX_B(s
);
7611 reg
= ((modrm
>> 3) & 7) | rex_r
;
7616 if ((prefixes
& PREFIX_LOCK
) && (reg
== 0) &&
7617 (s
->cpuid_ext3_features
& CPUID_EXT3_CR8LEG
)) {
7626 gen_update_cc_op(s
);
7627 gen_jmp_im(pc_start
- s
->cs_base
);
7629 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
7630 gen_helper_write_crN(cpu_env
, tcg_const_i32(reg
),
7632 gen_jmp_im(s
->pc
- s
->cs_base
);
7635 gen_helper_read_crN(cpu_T
[0], cpu_env
, tcg_const_i32(reg
));
7636 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
7644 case 0x121: /* mov reg, drN */
7645 case 0x123: /* mov drN, reg */
7647 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7649 modrm
= cpu_ldub_code(env
, s
->pc
++);
7650 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7651 * AMD documentation (24594.pdf) and testing of
7652 * intel 386 and 486 processors all show that the mod bits
7653 * are assumed to be 1's, regardless of actual values.
7655 rm
= (modrm
& 7) | REX_B(s
);
7656 reg
= ((modrm
>> 3) & 7) | rex_r
;
7661 /* XXX: do it dynamically with CR4.DE bit */
7662 if (reg
== 4 || reg
== 5 || reg
>= 8)
7665 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_WRITE_DR0
+ reg
);
7666 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
7667 gen_helper_movl_drN_T0(cpu_env
, tcg_const_i32(reg
), cpu_T
[0]);
7668 gen_jmp_im(s
->pc
- s
->cs_base
);
7671 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_READ_DR0
+ reg
);
7672 tcg_gen_ld_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,dr
[reg
]));
7673 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
7677 case 0x106: /* clts */
7679 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7681 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_WRITE_CR0
);
7682 gen_helper_clts(cpu_env
);
7683 /* abort block because static cpu state changed */
7684 gen_jmp_im(s
->pc
- s
->cs_base
);
7688 /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
7689 case 0x1c3: /* MOVNTI reg, mem */
7690 if (!(s
->cpuid_features
& CPUID_SSE2
))
7692 ot
= mo_64_32(dflag
);
7693 modrm
= cpu_ldub_code(env
, s
->pc
++);
7694 mod
= (modrm
>> 6) & 3;
7697 reg
= ((modrm
>> 3) & 7) | rex_r
;
7698 /* generate a generic store */
7699 gen_ldst_modrm(env
, s
, modrm
, ot
, reg
, 1);
7702 modrm
= cpu_ldub_code(env
, s
->pc
++);
7703 mod
= (modrm
>> 6) & 3;
7704 op
= (modrm
>> 3) & 7;
7706 case 0: /* fxsave */
7707 if (mod
== 3 || !(s
->cpuid_features
& CPUID_FXSR
) ||
7708 (s
->prefix
& PREFIX_LOCK
))
7710 if ((s
->flags
& HF_EM_MASK
) || (s
->flags
& HF_TS_MASK
)) {
7711 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
7714 gen_lea_modrm(env
, s
, modrm
);
7715 gen_update_cc_op(s
);
7716 gen_jmp_im(pc_start
- s
->cs_base
);
7717 gen_helper_fxsave(cpu_env
, cpu_A0
, tcg_const_i32(dflag
== MO_64
));
7719 case 1: /* fxrstor */
7720 if (mod
== 3 || !(s
->cpuid_features
& CPUID_FXSR
) ||
7721 (s
->prefix
& PREFIX_LOCK
))
7723 if ((s
->flags
& HF_EM_MASK
) || (s
->flags
& HF_TS_MASK
)) {
7724 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
7727 gen_lea_modrm(env
, s
, modrm
);
7728 gen_update_cc_op(s
);
7729 gen_jmp_im(pc_start
- s
->cs_base
);
7730 gen_helper_fxrstor(cpu_env
, cpu_A0
, tcg_const_i32(dflag
== MO_64
));
7732 case 2: /* ldmxcsr */
7733 case 3: /* stmxcsr */
7734 if (s
->flags
& HF_TS_MASK
) {
7735 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
7738 if ((s
->flags
& HF_EM_MASK
) || !(s
->flags
& HF_OSFXSR_MASK
) ||
7741 gen_lea_modrm(env
, s
, modrm
);
7743 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
7744 s
->mem_index
, MO_LEUL
);
7745 gen_helper_ldmxcsr(cpu_env
, cpu_tmp2_i32
);
7747 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, mxcsr
));
7748 gen_op_st_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
7751 case 5: /* lfence */
7752 case 6: /* mfence */
7753 if ((modrm
& 0xc7) != 0xc0 || !(s
->cpuid_features
& CPUID_SSE2
))
7756 case 7: /* sfence / clflush */
7757 if ((modrm
& 0xc7) == 0xc0) {
7759 /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
7760 if (!(s
->cpuid_features
& CPUID_SSE
))
7764 if (!(s
->cpuid_features
& CPUID_CLFLUSH
))
7766 gen_lea_modrm(env
, s
, modrm
);
7773 case 0x10d: /* 3DNow! prefetch(w) */
7774 modrm
= cpu_ldub_code(env
, s
->pc
++);
7775 mod
= (modrm
>> 6) & 3;
7778 gen_lea_modrm(env
, s
, modrm
);
7779 /* ignore for now */
7781 case 0x1aa: /* rsm */
7782 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_RSM
);
7783 if (!(s
->flags
& HF_SMM_MASK
))
7785 gen_update_cc_op(s
);
7786 gen_jmp_im(s
->pc
- s
->cs_base
);
7787 gen_helper_rsm(cpu_env
);
7790 case 0x1b8: /* SSE4.2 popcnt */
7791 if ((prefixes
& (PREFIX_REPZ
| PREFIX_LOCK
| PREFIX_REPNZ
)) !=
7794 if (!(s
->cpuid_ext_features
& CPUID_EXT_POPCNT
))
7797 modrm
= cpu_ldub_code(env
, s
->pc
++);
7798 reg
= ((modrm
>> 3) & 7) | rex_r
;
7800 if (s
->prefix
& PREFIX_DATA
) {
7803 ot
= mo_64_32(dflag
);
7806 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
7807 gen_helper_popcnt(cpu_T
[0], cpu_env
, cpu_T
[0], tcg_const_i32(ot
));
7808 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
7810 set_cc_op(s
, CC_OP_EFLAGS
);
7812 case 0x10e ... 0x10f:
7813 /* 3DNow! instructions, ignore prefixes */
7814 s
->prefix
&= ~(PREFIX_REPZ
| PREFIX_REPNZ
| PREFIX_DATA
);
7815 case 0x110 ... 0x117:
7816 case 0x128 ... 0x12f:
7817 case 0x138 ... 0x13a:
7818 case 0x150 ... 0x179:
7819 case 0x17c ... 0x17f:
7821 case 0x1c4 ... 0x1c6:
7822 case 0x1d0 ... 0x1fe:
7823 gen_sse(env
, s
, b
, pc_start
, rex_r
);
7828 /* lock generation */
7829 if (s
->prefix
& PREFIX_LOCK
)
7830 gen_helper_unlock();
7833 if (s
->prefix
& PREFIX_LOCK
)
7834 gen_helper_unlock();
7835 /* XXX: ensure that no lock was generated */
7836 gen_exception(s
, EXCP06_ILLOP
, pc_start
- s
->cs_base
);
7840 void optimize_flags_init(void)
7842 static const char reg_names
[CPU_NB_REGS
][4] = {
7843 #ifdef TARGET_X86_64
7873 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
7874 cpu_cc_op
= tcg_global_mem_new_i32(TCG_AREG0
,
7875 offsetof(CPUX86State
, cc_op
), "cc_op");
7876 cpu_cc_dst
= tcg_global_mem_new(TCG_AREG0
, offsetof(CPUX86State
, cc_dst
),
7878 cpu_cc_src
= tcg_global_mem_new(TCG_AREG0
, offsetof(CPUX86State
, cc_src
),
7880 cpu_cc_src2
= tcg_global_mem_new(TCG_AREG0
, offsetof(CPUX86State
, cc_src2
),
7883 for (i
= 0; i
< CPU_NB_REGS
; ++i
) {
7884 cpu_regs
[i
] = tcg_global_mem_new(TCG_AREG0
,
7885 offsetof(CPUX86State
, regs
[i
]),
7890 /* generate intermediate code in gen_opc_buf and gen_opparam_buf for
7891 basic block 'tb'. If search_pc is TRUE, also generate PC
7892 information for each intermediate instruction. */
7893 static inline void gen_intermediate_code_internal(X86CPU
*cpu
,
7894 TranslationBlock
*tb
,
7897 CPUState
*cs
= CPU(cpu
);
7898 CPUX86State
*env
= &cpu
->env
;
7899 DisasContext dc1
, *dc
= &dc1
;
7900 target_ulong pc_ptr
;
7901 uint16_t *gen_opc_end
;
7905 target_ulong pc_start
;
7906 target_ulong cs_base
;
7910 /* generate intermediate code */
7912 cs_base
= tb
->cs_base
;
7915 dc
->pe
= (flags
>> HF_PE_SHIFT
) & 1;
7916 dc
->code32
= (flags
>> HF_CS32_SHIFT
) & 1;
7917 dc
->ss32
= (flags
>> HF_SS32_SHIFT
) & 1;
7918 dc
->addseg
= (flags
>> HF_ADDSEG_SHIFT
) & 1;
7920 dc
->vm86
= (flags
>> VM_SHIFT
) & 1;
7921 dc
->cpl
= (flags
>> HF_CPL_SHIFT
) & 3;
7922 dc
->iopl
= (flags
>> IOPL_SHIFT
) & 3;
7923 dc
->tf
= (flags
>> TF_SHIFT
) & 1;
7924 dc
->singlestep_enabled
= cs
->singlestep_enabled
;
7925 dc
->cc_op
= CC_OP_DYNAMIC
;
7926 dc
->cc_op_dirty
= false;
7927 dc
->cs_base
= cs_base
;
7929 dc
->popl_esp_hack
= 0;
7930 /* select memory access functions */
7932 if (flags
& HF_SOFTMMU_MASK
) {
7933 dc
->mem_index
= cpu_mmu_index(env
);
7935 dc
->cpuid_features
= env
->features
[FEAT_1_EDX
];
7936 dc
->cpuid_ext_features
= env
->features
[FEAT_1_ECX
];
7937 dc
->cpuid_ext2_features
= env
->features
[FEAT_8000_0001_EDX
];
7938 dc
->cpuid_ext3_features
= env
->features
[FEAT_8000_0001_ECX
];
7939 dc
->cpuid_7_0_ebx_features
= env
->features
[FEAT_7_0_EBX
];
7940 #ifdef TARGET_X86_64
7941 dc
->lma
= (flags
>> HF_LMA_SHIFT
) & 1;
7942 dc
->code64
= (flags
>> HF_CS64_SHIFT
) & 1;
7945 dc
->jmp_opt
= !(dc
->tf
|| cs
->singlestep_enabled
||
7946 (flags
& HF_INHIBIT_IRQ_MASK
)
7947 #ifndef CONFIG_SOFTMMU
7948 || (flags
& HF_SOFTMMU_MASK
)
7952 /* check addseg logic */
7953 if (!dc
->addseg
&& (dc
->vm86
|| !dc
->pe
|| !dc
->code32
))
7954 printf("ERROR addseg\n");
7957 cpu_T
[0] = tcg_temp_new();
7958 cpu_T
[1] = tcg_temp_new();
7959 cpu_A0
= tcg_temp_new();
7961 cpu_tmp0
= tcg_temp_new();
7962 cpu_tmp1_i64
= tcg_temp_new_i64();
7963 cpu_tmp2_i32
= tcg_temp_new_i32();
7964 cpu_tmp3_i32
= tcg_temp_new_i32();
7965 cpu_tmp4
= tcg_temp_new();
7966 cpu_ptr0
= tcg_temp_new_ptr();
7967 cpu_ptr1
= tcg_temp_new_ptr();
7968 cpu_cc_srcT
= tcg_temp_local_new();
7970 gen_opc_end
= tcg_ctx
.gen_opc_buf
+ OPC_MAX_SIZE
;
7972 dc
->is_jmp
= DISAS_NEXT
;
7976 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
7978 max_insns
= CF_COUNT_MASK
;
7982 if (unlikely(!QTAILQ_EMPTY(&cs
->breakpoints
))) {
7983 QTAILQ_FOREACH(bp
, &cs
->breakpoints
, entry
) {
7984 if (bp
->pc
== pc_ptr
&&
7985 !((bp
->flags
& BP_CPU
) && (tb
->flags
& HF_RF_MASK
))) {
7986 gen_debug(dc
, pc_ptr
- dc
->cs_base
);
7992 j
= tcg_ctx
.gen_opc_ptr
- tcg_ctx
.gen_opc_buf
;
7996 tcg_ctx
.gen_opc_instr_start
[lj
++] = 0;
7998 tcg_ctx
.gen_opc_pc
[lj
] = pc_ptr
;
7999 gen_opc_cc_op
[lj
] = dc
->cc_op
;
8000 tcg_ctx
.gen_opc_instr_start
[lj
] = 1;
8001 tcg_ctx
.gen_opc_icount
[lj
] = num_insns
;
8003 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
))
8006 pc_ptr
= disas_insn(env
, dc
, pc_ptr
);
8008 /* stop translation if indicated */
8011 /* if single step mode, we generate only one instruction and
8012 generate an exception */
8013 /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
8014 the flag and abort the translation to give the irqs a
8015 change to be happen */
8016 if (dc
->tf
|| dc
->singlestep_enabled
||
8017 (flags
& HF_INHIBIT_IRQ_MASK
)) {
8018 gen_jmp_im(pc_ptr
- dc
->cs_base
);
8022 /* if too long translation, stop generation too */
8023 if (tcg_ctx
.gen_opc_ptr
>= gen_opc_end
||
8024 (pc_ptr
- pc_start
) >= (TARGET_PAGE_SIZE
- 32) ||
8025 num_insns
>= max_insns
) {
8026 gen_jmp_im(pc_ptr
- dc
->cs_base
);
8031 gen_jmp_im(pc_ptr
- dc
->cs_base
);
8036 if (tb
->cflags
& CF_LAST_IO
)
8038 gen_tb_end(tb
, num_insns
);
8039 *tcg_ctx
.gen_opc_ptr
= INDEX_op_end
;
8040 /* we don't forget to fill the last values */
8042 j
= tcg_ctx
.gen_opc_ptr
- tcg_ctx
.gen_opc_buf
;
8045 tcg_ctx
.gen_opc_instr_start
[lj
++] = 0;
8049 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
8051 qemu_log("----------------\n");
8052 qemu_log("IN: %s\n", lookup_symbol(pc_start
));
8053 #ifdef TARGET_X86_64
8058 disas_flags
= !dc
->code32
;
8059 log_target_disas(env
, pc_start
, pc_ptr
- pc_start
, disas_flags
);
8065 tb
->size
= pc_ptr
- pc_start
;
8066 tb
->icount
= num_insns
;
8070 void gen_intermediate_code(CPUX86State
*env
, TranslationBlock
*tb
)
8072 gen_intermediate_code_internal(x86_env_get_cpu(env
), tb
, false);
8075 void gen_intermediate_code_pc(CPUX86State
*env
, TranslationBlock
*tb
)
8077 gen_intermediate_code_internal(x86_env_get_cpu(env
), tb
, true);
8080 void restore_state_to_opc(CPUX86State
*env
, TranslationBlock
*tb
, int pc_pos
)
8084 if (qemu_loglevel_mask(CPU_LOG_TB_OP
)) {
8086 qemu_log("RESTORE:\n");
8087 for(i
= 0;i
<= pc_pos
; i
++) {
8088 if (tcg_ctx
.gen_opc_instr_start
[i
]) {
8089 qemu_log("0x%04x: " TARGET_FMT_lx
"\n", i
,
8090 tcg_ctx
.gen_opc_pc
[i
]);
8093 qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx
" cs_base=%x\n",
8094 pc_pos
, tcg_ctx
.gen_opc_pc
[pc_pos
] - tb
->cs_base
,
8095 (uint32_t)tb
->cs_base
);
8098 env
->eip
= tcg_ctx
.gen_opc_pc
[pc_pos
] - tb
->cs_base
;
8099 cc_op
= gen_opc_cc_op
[pc_pos
];
8100 if (cc_op
!= CC_OP_DYNAMIC
)