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/>.
19 #include "qemu/osdep.h"
21 #include "qemu/host-utils.h"
23 #include "disas/disas.h"
24 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
27 #include "exec/translator.h"
29 #include "exec/helper-proto.h"
30 #include "exec/helper-gen.h"
32 #include "trace-tcg.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 /* For a switch indexed by MODRM, match all memory operands for a given OP. */
61 #define CASE_MODRM_MEM_OP(OP) \
62 case (0 << 6) | (OP << 3) | 0 ... (0 << 6) | (OP << 3) | 7: \
63 case (1 << 6) | (OP << 3) | 0 ... (1 << 6) | (OP << 3) | 7: \
64 case (2 << 6) | (OP << 3) | 0 ... (2 << 6) | (OP << 3) | 7
66 #define CASE_MODRM_OP(OP) \
67 case (0 << 6) | (OP << 3) | 0 ... (0 << 6) | (OP << 3) | 7: \
68 case (1 << 6) | (OP << 3) | 0 ... (1 << 6) | (OP << 3) | 7: \
69 case (2 << 6) | (OP << 3) | 0 ... (2 << 6) | (OP << 3) | 7: \
70 case (3 << 6) | (OP << 3) | 0 ... (3 << 6) | (OP << 3) | 7
72 //#define MACRO_TEST 1
74 /* global register indexes */
75 static TCGv cpu_cc_dst
, cpu_cc_src
, cpu_cc_src2
;
76 static TCGv_i32 cpu_cc_op
;
77 static TCGv cpu_regs
[CPU_NB_REGS
];
78 static TCGv cpu_seg_base
[6];
79 static TCGv_i64 cpu_bndl
[4];
80 static TCGv_i64 cpu_bndu
[4];
82 #include "exec/gen-icount.h"
84 typedef struct DisasContext
{
85 DisasContextBase base
;
87 /* current insn context */
88 int override
; /* -1 if no override */
92 target_ulong pc_start
;
93 target_ulong pc
; /* pc = eip + cs_base */
94 /* current block context */
95 target_ulong cs_base
; /* base of CS segment */
96 int pe
; /* protected mode */
97 int code32
; /* 32 bit code segment */
99 int lma
; /* long mode active */
100 int code64
; /* 64 bit code segment */
103 int vex_l
; /* vex vector length */
104 int vex_v
; /* vex vvvv register, without 1's complement. */
105 int ss32
; /* 32 bit stack segment */
106 CCOp cc_op
; /* current CC operation */
111 int addseg
; /* non zero if either DS/ES/SS have a non zero base */
112 int f_st
; /* currently unused */
113 int vm86
; /* vm86 mode */
116 int tf
; /* TF cpu flag */
117 int jmp_opt
; /* use direct block chaining for direct jumps */
118 int repz_opt
; /* optimize jumps within repz instructions */
119 int mem_index
; /* select memory access functions */
120 uint64_t flags
; /* all execution flags */
121 int popl_esp_hack
; /* for correct popl with esp base handling */
122 int rip_offset
; /* only used in x86_64, but left for simplicity */
124 int cpuid_ext_features
;
125 int cpuid_ext2_features
;
126 int cpuid_ext3_features
;
127 int cpuid_7_0_ebx_features
;
128 int cpuid_xsave_features
;
130 /* TCG local temps */
136 /* TCG local register indexes (only used inside old micro ops) */
148 static void gen_eob(DisasContext
*s
);
149 static void gen_jr(DisasContext
*s
, TCGv dest
);
150 static void gen_jmp(DisasContext
*s
, target_ulong eip
);
151 static void gen_jmp_tb(DisasContext
*s
, target_ulong eip
, int tb_num
);
152 static void gen_op(DisasContext
*s1
, int op
, MemOp ot
, int d
);
154 /* i386 arith/logic operations */
174 OP_SHL1
, /* undocumented */
190 /* I386 int registers */
191 OR_EAX
, /* MUST be even numbered */
200 OR_TMP0
= 16, /* temporary operand register */
202 OR_A0
, /* temporary register used when doing address evaluation */
212 /* Bit set if the global variable is live after setting CC_OP to X. */
213 static const uint8_t cc_op_live
[CC_OP_NB
] = {
214 [CC_OP_DYNAMIC
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
215 [CC_OP_EFLAGS
] = USES_CC_SRC
,
216 [CC_OP_MULB
... CC_OP_MULQ
] = USES_CC_DST
| USES_CC_SRC
,
217 [CC_OP_ADDB
... CC_OP_ADDQ
] = USES_CC_DST
| USES_CC_SRC
,
218 [CC_OP_ADCB
... CC_OP_ADCQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
219 [CC_OP_SUBB
... CC_OP_SUBQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRCT
,
220 [CC_OP_SBBB
... CC_OP_SBBQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
221 [CC_OP_LOGICB
... CC_OP_LOGICQ
] = USES_CC_DST
,
222 [CC_OP_INCB
... CC_OP_INCQ
] = USES_CC_DST
| USES_CC_SRC
,
223 [CC_OP_DECB
... CC_OP_DECQ
] = USES_CC_DST
| USES_CC_SRC
,
224 [CC_OP_SHLB
... CC_OP_SHLQ
] = USES_CC_DST
| USES_CC_SRC
,
225 [CC_OP_SARB
... CC_OP_SARQ
] = USES_CC_DST
| USES_CC_SRC
,
226 [CC_OP_BMILGB
... CC_OP_BMILGQ
] = USES_CC_DST
| USES_CC_SRC
,
227 [CC_OP_ADCX
] = USES_CC_DST
| USES_CC_SRC
,
228 [CC_OP_ADOX
] = USES_CC_SRC
| USES_CC_SRC2
,
229 [CC_OP_ADCOX
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
231 [CC_OP_POPCNT
] = USES_CC_SRC
,
234 static void set_cc_op(DisasContext
*s
, CCOp op
)
238 if (s
->cc_op
== op
) {
242 /* Discard CC computation that will no longer be used. */
243 dead
= cc_op_live
[s
->cc_op
] & ~cc_op_live
[op
];
244 if (dead
& USES_CC_DST
) {
245 tcg_gen_discard_tl(cpu_cc_dst
);
247 if (dead
& USES_CC_SRC
) {
248 tcg_gen_discard_tl(cpu_cc_src
);
250 if (dead
& USES_CC_SRC2
) {
251 tcg_gen_discard_tl(cpu_cc_src2
);
253 if (dead
& USES_CC_SRCT
) {
254 tcg_gen_discard_tl(s
->cc_srcT
);
257 if (op
== CC_OP_DYNAMIC
) {
258 /* The DYNAMIC setting is translator only, and should never be
259 stored. Thus we always consider it clean. */
260 s
->cc_op_dirty
= false;
262 /* Discard any computed CC_OP value (see shifts). */
263 if (s
->cc_op
== CC_OP_DYNAMIC
) {
264 tcg_gen_discard_i32(cpu_cc_op
);
266 s
->cc_op_dirty
= true;
271 static void gen_update_cc_op(DisasContext
*s
)
273 if (s
->cc_op_dirty
) {
274 tcg_gen_movi_i32(cpu_cc_op
, s
->cc_op
);
275 s
->cc_op_dirty
= false;
281 #define NB_OP_SIZES 4
283 #else /* !TARGET_X86_64 */
285 #define NB_OP_SIZES 3
287 #endif /* !TARGET_X86_64 */
289 #if defined(HOST_WORDS_BIGENDIAN)
290 #define REG_B_OFFSET (sizeof(target_ulong) - 1)
291 #define REG_H_OFFSET (sizeof(target_ulong) - 2)
292 #define REG_W_OFFSET (sizeof(target_ulong) - 2)
293 #define REG_L_OFFSET (sizeof(target_ulong) - 4)
294 #define REG_LH_OFFSET (sizeof(target_ulong) - 8)
296 #define REG_B_OFFSET 0
297 #define REG_H_OFFSET 1
298 #define REG_W_OFFSET 0
299 #define REG_L_OFFSET 0
300 #define REG_LH_OFFSET 4
303 /* In instruction encodings for byte register accesses the
304 * register number usually indicates "low 8 bits of register N";
305 * however there are some special cases where N 4..7 indicates
306 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
307 * true for this special case, false otherwise.
309 static inline bool byte_reg_is_xH(DisasContext
*s
, int reg
)
315 if (reg
>= 8 || s
->x86_64_hregs
) {
322 /* Select the size of a push/pop operation. */
323 static inline MemOp
mo_pushpop(DisasContext
*s
, MemOp ot
)
326 return ot
== MO_16
? MO_16
: MO_64
;
332 /* Select the size of the stack pointer. */
333 static inline MemOp
mo_stacksize(DisasContext
*s
)
335 return CODE64(s
) ? MO_64
: s
->ss32
? MO_32
: MO_16
;
338 /* Select only size 64 else 32. Used for SSE operand sizes. */
339 static inline MemOp
mo_64_32(MemOp ot
)
342 return ot
== MO_64
? MO_64
: MO_32
;
348 /* Select size 8 if lsb of B is clear, else OT. Used for decoding
349 byte vs word opcodes. */
350 static inline MemOp
mo_b_d(int b
, MemOp ot
)
352 return b
& 1 ? ot
: MO_8
;
355 /* Select size 8 if lsb of B is clear, else OT capped at 32.
356 Used for decoding operand size of port opcodes. */
357 static inline MemOp
mo_b_d32(int b
, MemOp ot
)
359 return b
& 1 ? (ot
== MO_16
? MO_16
: MO_32
) : MO_8
;
362 static void gen_op_mov_reg_v(DisasContext
*s
, MemOp ot
, int reg
, TCGv t0
)
366 if (!byte_reg_is_xH(s
, reg
)) {
367 tcg_gen_deposit_tl(cpu_regs
[reg
], cpu_regs
[reg
], t0
, 0, 8);
369 tcg_gen_deposit_tl(cpu_regs
[reg
- 4], cpu_regs
[reg
- 4], t0
, 8, 8);
373 tcg_gen_deposit_tl(cpu_regs
[reg
], cpu_regs
[reg
], t0
, 0, 16);
376 /* For x86_64, this sets the higher half of register to zero.
377 For i386, this is equivalent to a mov. */
378 tcg_gen_ext32u_tl(cpu_regs
[reg
], t0
);
382 tcg_gen_mov_tl(cpu_regs
[reg
], t0
);
391 void gen_op_mov_v_reg(DisasContext
*s
, MemOp ot
, TCGv t0
, int reg
)
393 if (ot
== MO_8
&& byte_reg_is_xH(s
, reg
)) {
394 tcg_gen_extract_tl(t0
, cpu_regs
[reg
- 4], 8, 8);
396 tcg_gen_mov_tl(t0
, cpu_regs
[reg
]);
400 static void gen_add_A0_im(DisasContext
*s
, int val
)
402 tcg_gen_addi_tl(s
->A0
, s
->A0
, val
);
404 tcg_gen_ext32u_tl(s
->A0
, s
->A0
);
408 static inline void gen_op_jmp_v(TCGv dest
)
410 tcg_gen_st_tl(dest
, cpu_env
, offsetof(CPUX86State
, eip
));
414 void gen_op_add_reg_im(DisasContext
*s
, MemOp size
, int reg
, int32_t val
)
416 tcg_gen_addi_tl(s
->tmp0
, cpu_regs
[reg
], val
);
417 gen_op_mov_reg_v(s
, size
, reg
, s
->tmp0
);
420 static inline void gen_op_add_reg_T0(DisasContext
*s
, MemOp size
, int reg
)
422 tcg_gen_add_tl(s
->tmp0
, cpu_regs
[reg
], s
->T0
);
423 gen_op_mov_reg_v(s
, size
, reg
, s
->tmp0
);
426 static inline void gen_op_ld_v(DisasContext
*s
, int idx
, TCGv t0
, TCGv a0
)
428 tcg_gen_qemu_ld_tl(t0
, a0
, s
->mem_index
, idx
| MO_LE
);
431 static inline void gen_op_st_v(DisasContext
*s
, int idx
, TCGv t0
, TCGv a0
)
433 tcg_gen_qemu_st_tl(t0
, a0
, s
->mem_index
, idx
| MO_LE
);
436 static inline void gen_op_st_rm_T0_A0(DisasContext
*s
, int idx
, int d
)
439 gen_op_st_v(s
, idx
, s
->T0
, s
->A0
);
441 gen_op_mov_reg_v(s
, idx
, d
, s
->T0
);
445 static inline void gen_jmp_im(DisasContext
*s
, target_ulong pc
)
447 tcg_gen_movi_tl(s
->tmp0
, pc
);
448 gen_op_jmp_v(s
->tmp0
);
451 /* Compute SEG:REG into A0. SEG is selected from the override segment
452 (OVR_SEG) and the default segment (DEF_SEG). OVR_SEG may be -1 to
453 indicate no override. */
454 static void gen_lea_v_seg(DisasContext
*s
, MemOp aflag
, TCGv a0
,
455 int def_seg
, int ovr_seg
)
461 tcg_gen_mov_tl(s
->A0
, a0
);
468 if (ovr_seg
< 0 && s
->addseg
) {
472 tcg_gen_ext32u_tl(s
->A0
, a0
);
478 tcg_gen_ext16u_tl(s
->A0
, a0
);
493 TCGv seg
= cpu_seg_base
[ovr_seg
];
495 if (aflag
== MO_64
) {
496 tcg_gen_add_tl(s
->A0
, a0
, seg
);
497 } else if (CODE64(s
)) {
498 tcg_gen_ext32u_tl(s
->A0
, a0
);
499 tcg_gen_add_tl(s
->A0
, s
->A0
, seg
);
501 tcg_gen_add_tl(s
->A0
, a0
, seg
);
502 tcg_gen_ext32u_tl(s
->A0
, s
->A0
);
507 static inline void gen_string_movl_A0_ESI(DisasContext
*s
)
509 gen_lea_v_seg(s
, s
->aflag
, cpu_regs
[R_ESI
], R_DS
, s
->override
);
512 static inline void gen_string_movl_A0_EDI(DisasContext
*s
)
514 gen_lea_v_seg(s
, s
->aflag
, cpu_regs
[R_EDI
], R_ES
, -1);
517 static inline void gen_op_movl_T0_Dshift(DisasContext
*s
, MemOp ot
)
519 tcg_gen_ld32s_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, df
));
520 tcg_gen_shli_tl(s
->T0
, s
->T0
, ot
);
523 static TCGv
gen_ext_tl(TCGv dst
, TCGv src
, MemOp size
, bool sign
)
528 tcg_gen_ext8s_tl(dst
, src
);
530 tcg_gen_ext8u_tl(dst
, src
);
535 tcg_gen_ext16s_tl(dst
, src
);
537 tcg_gen_ext16u_tl(dst
, src
);
543 tcg_gen_ext32s_tl(dst
, src
);
545 tcg_gen_ext32u_tl(dst
, src
);
554 static void gen_extu(MemOp ot
, TCGv reg
)
556 gen_ext_tl(reg
, reg
, ot
, false);
559 static void gen_exts(MemOp ot
, TCGv reg
)
561 gen_ext_tl(reg
, reg
, ot
, true);
565 void gen_op_jnz_ecx(DisasContext
*s
, MemOp size
, TCGLabel
*label1
)
567 tcg_gen_mov_tl(s
->tmp0
, cpu_regs
[R_ECX
]);
568 gen_extu(size
, s
->tmp0
);
569 tcg_gen_brcondi_tl(TCG_COND_NE
, s
->tmp0
, 0, label1
);
573 void gen_op_jz_ecx(DisasContext
*s
, MemOp size
, TCGLabel
*label1
)
575 tcg_gen_mov_tl(s
->tmp0
, cpu_regs
[R_ECX
]);
576 gen_extu(size
, s
->tmp0
);
577 tcg_gen_brcondi_tl(TCG_COND_EQ
, s
->tmp0
, 0, label1
);
580 static void gen_helper_in_func(MemOp ot
, TCGv v
, TCGv_i32 n
)
584 gen_helper_inb(v
, cpu_env
, n
);
587 gen_helper_inw(v
, cpu_env
, n
);
590 gen_helper_inl(v
, cpu_env
, n
);
597 static void gen_helper_out_func(MemOp ot
, TCGv_i32 v
, TCGv_i32 n
)
601 gen_helper_outb(cpu_env
, v
, n
);
604 gen_helper_outw(cpu_env
, v
, n
);
607 gen_helper_outl(cpu_env
, v
, n
);
614 static void gen_check_io(DisasContext
*s
, MemOp ot
, target_ulong cur_eip
,
617 target_ulong next_eip
;
619 if (s
->pe
&& (s
->cpl
> s
->iopl
|| s
->vm86
)) {
620 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
623 gen_helper_check_iob(cpu_env
, s
->tmp2_i32
);
626 gen_helper_check_iow(cpu_env
, s
->tmp2_i32
);
629 gen_helper_check_iol(cpu_env
, s
->tmp2_i32
);
635 if(s
->flags
& HF_GUEST_MASK
) {
637 gen_jmp_im(s
, cur_eip
);
638 svm_flags
|= (1 << (4 + ot
));
639 next_eip
= s
->pc
- s
->cs_base
;
640 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
641 gen_helper_svm_check_io(cpu_env
, s
->tmp2_i32
,
642 tcg_const_i32(svm_flags
),
643 tcg_const_i32(next_eip
- cur_eip
));
647 static inline void gen_movs(DisasContext
*s
, MemOp ot
)
649 gen_string_movl_A0_ESI(s
);
650 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
651 gen_string_movl_A0_EDI(s
);
652 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
653 gen_op_movl_T0_Dshift(s
, ot
);
654 gen_op_add_reg_T0(s
, s
->aflag
, R_ESI
);
655 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
658 static void gen_op_update1_cc(DisasContext
*s
)
660 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
663 static void gen_op_update2_cc(DisasContext
*s
)
665 tcg_gen_mov_tl(cpu_cc_src
, s
->T1
);
666 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
669 static void gen_op_update3_cc(DisasContext
*s
, TCGv reg
)
671 tcg_gen_mov_tl(cpu_cc_src2
, reg
);
672 tcg_gen_mov_tl(cpu_cc_src
, s
->T1
);
673 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
676 static inline void gen_op_testl_T0_T1_cc(DisasContext
*s
)
678 tcg_gen_and_tl(cpu_cc_dst
, s
->T0
, s
->T1
);
681 static void gen_op_update_neg_cc(DisasContext
*s
)
683 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
684 tcg_gen_neg_tl(cpu_cc_src
, s
->T0
);
685 tcg_gen_movi_tl(s
->cc_srcT
, 0);
688 /* compute all eflags to cc_src */
689 static void gen_compute_eflags(DisasContext
*s
)
691 TCGv zero
, dst
, src1
, src2
;
694 if (s
->cc_op
== CC_OP_EFLAGS
) {
697 if (s
->cc_op
== CC_OP_CLR
) {
698 tcg_gen_movi_tl(cpu_cc_src
, CC_Z
| CC_P
);
699 set_cc_op(s
, CC_OP_EFLAGS
);
708 /* Take care to not read values that are not live. */
709 live
= cc_op_live
[s
->cc_op
] & ~USES_CC_SRCT
;
710 dead
= live
^ (USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
);
712 zero
= tcg_const_tl(0);
713 if (dead
& USES_CC_DST
) {
716 if (dead
& USES_CC_SRC
) {
719 if (dead
& USES_CC_SRC2
) {
725 gen_helper_cc_compute_all(cpu_cc_src
, dst
, src1
, src2
, cpu_cc_op
);
726 set_cc_op(s
, CC_OP_EFLAGS
);
733 typedef struct CCPrepare
{
743 /* compute eflags.C to reg */
744 static CCPrepare
gen_prepare_eflags_c(DisasContext
*s
, TCGv reg
)
750 case CC_OP_SUBB
... CC_OP_SUBQ
:
751 /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
752 size
= s
->cc_op
- CC_OP_SUBB
;
753 t1
= gen_ext_tl(s
->tmp0
, cpu_cc_src
, size
, false);
754 /* If no temporary was used, be careful not to alias t1 and t0. */
755 t0
= t1
== cpu_cc_src
? s
->tmp0
: reg
;
756 tcg_gen_mov_tl(t0
, s
->cc_srcT
);
760 case CC_OP_ADDB
... CC_OP_ADDQ
:
761 /* (DATA_TYPE)CC_DST < (DATA_TYPE)CC_SRC */
762 size
= s
->cc_op
- CC_OP_ADDB
;
763 t1
= gen_ext_tl(s
->tmp0
, cpu_cc_src
, size
, false);
764 t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, false);
766 return (CCPrepare
) { .cond
= TCG_COND_LTU
, .reg
= t0
,
767 .reg2
= t1
, .mask
= -1, .use_reg2
= true };
769 case CC_OP_LOGICB
... CC_OP_LOGICQ
:
772 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
774 case CC_OP_INCB
... CC_OP_INCQ
:
775 case CC_OP_DECB
... CC_OP_DECQ
:
776 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
777 .mask
= -1, .no_setcond
= true };
779 case CC_OP_SHLB
... CC_OP_SHLQ
:
780 /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
781 size
= s
->cc_op
- CC_OP_SHLB
;
782 shift
= (8 << size
) - 1;
783 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
784 .mask
= (target_ulong
)1 << shift
};
786 case CC_OP_MULB
... CC_OP_MULQ
:
787 return (CCPrepare
) { .cond
= TCG_COND_NE
,
788 .reg
= cpu_cc_src
, .mask
= -1 };
790 case CC_OP_BMILGB
... CC_OP_BMILGQ
:
791 size
= s
->cc_op
- CC_OP_BMILGB
;
792 t0
= gen_ext_tl(reg
, cpu_cc_src
, size
, false);
793 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= t0
, .mask
= -1 };
797 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_dst
,
798 .mask
= -1, .no_setcond
= true };
801 case CC_OP_SARB
... CC_OP_SARQ
:
803 return (CCPrepare
) { .cond
= TCG_COND_NE
,
804 .reg
= cpu_cc_src
, .mask
= CC_C
};
807 /* The need to compute only C from CC_OP_DYNAMIC is important
808 in efficiently implementing e.g. INC at the start of a TB. */
810 gen_helper_cc_compute_c(reg
, cpu_cc_dst
, cpu_cc_src
,
811 cpu_cc_src2
, cpu_cc_op
);
812 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
813 .mask
= -1, .no_setcond
= true };
817 /* compute eflags.P to reg */
818 static CCPrepare
gen_prepare_eflags_p(DisasContext
*s
, TCGv reg
)
820 gen_compute_eflags(s
);
821 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
825 /* compute eflags.S to reg */
826 static CCPrepare
gen_prepare_eflags_s(DisasContext
*s
, TCGv reg
)
830 gen_compute_eflags(s
);
836 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
840 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
843 MemOp size
= (s
->cc_op
- CC_OP_ADDB
) & 3;
844 TCGv t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, true);
845 return (CCPrepare
) { .cond
= TCG_COND_LT
, .reg
= t0
, .mask
= -1 };
850 /* compute eflags.O to reg */
851 static CCPrepare
gen_prepare_eflags_o(DisasContext
*s
, TCGv reg
)
856 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src2
,
857 .mask
= -1, .no_setcond
= true };
860 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
862 gen_compute_eflags(s
);
863 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
868 /* compute eflags.Z to reg */
869 static CCPrepare
gen_prepare_eflags_z(DisasContext
*s
, TCGv reg
)
873 gen_compute_eflags(s
);
879 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
882 return (CCPrepare
) { .cond
= TCG_COND_ALWAYS
, .mask
= -1 };
884 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= cpu_cc_src
,
888 MemOp size
= (s
->cc_op
- CC_OP_ADDB
) & 3;
889 TCGv t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, false);
890 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= t0
, .mask
= -1 };
895 /* perform a conditional store into register 'reg' according to jump opcode
896 value 'b'. In the fast case, T0 is guaranted not to be used. */
897 static CCPrepare
gen_prepare_cc(DisasContext
*s
, int b
, TCGv reg
)
899 int inv
, jcc_op
, cond
;
905 jcc_op
= (b
>> 1) & 7;
908 case CC_OP_SUBB
... CC_OP_SUBQ
:
909 /* We optimize relational operators for the cmp/jcc case. */
910 size
= s
->cc_op
- CC_OP_SUBB
;
913 tcg_gen_mov_tl(s
->tmp4
, s
->cc_srcT
);
914 gen_extu(size
, s
->tmp4
);
915 t0
= gen_ext_tl(s
->tmp0
, cpu_cc_src
, size
, false);
916 cc
= (CCPrepare
) { .cond
= TCG_COND_LEU
, .reg
= s
->tmp4
,
917 .reg2
= t0
, .mask
= -1, .use_reg2
= true };
926 tcg_gen_mov_tl(s
->tmp4
, s
->cc_srcT
);
927 gen_exts(size
, s
->tmp4
);
928 t0
= gen_ext_tl(s
->tmp0
, cpu_cc_src
, size
, true);
929 cc
= (CCPrepare
) { .cond
= cond
, .reg
= s
->tmp4
,
930 .reg2
= t0
, .mask
= -1, .use_reg2
= true };
940 /* This actually generates good code for JC, JZ and JS. */
943 cc
= gen_prepare_eflags_o(s
, reg
);
946 cc
= gen_prepare_eflags_c(s
, reg
);
949 cc
= gen_prepare_eflags_z(s
, reg
);
952 gen_compute_eflags(s
);
953 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
954 .mask
= CC_Z
| CC_C
};
957 cc
= gen_prepare_eflags_s(s
, reg
);
960 cc
= gen_prepare_eflags_p(s
, reg
);
963 gen_compute_eflags(s
);
964 if (reg
== cpu_cc_src
) {
967 tcg_gen_shri_tl(reg
, cpu_cc_src
, 4); /* CC_O -> CC_S */
968 tcg_gen_xor_tl(reg
, reg
, cpu_cc_src
);
969 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
974 gen_compute_eflags(s
);
975 if (reg
== cpu_cc_src
) {
978 tcg_gen_shri_tl(reg
, cpu_cc_src
, 4); /* CC_O -> CC_S */
979 tcg_gen_xor_tl(reg
, reg
, cpu_cc_src
);
980 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
981 .mask
= CC_S
| CC_Z
};
988 cc
.cond
= tcg_invert_cond(cc
.cond
);
993 static void gen_setcc1(DisasContext
*s
, int b
, TCGv reg
)
995 CCPrepare cc
= gen_prepare_cc(s
, b
, reg
);
998 if (cc
.cond
== TCG_COND_EQ
) {
999 tcg_gen_xori_tl(reg
, cc
.reg
, 1);
1001 tcg_gen_mov_tl(reg
, cc
.reg
);
1006 if (cc
.cond
== TCG_COND_NE
&& !cc
.use_reg2
&& cc
.imm
== 0 &&
1007 cc
.mask
!= 0 && (cc
.mask
& (cc
.mask
- 1)) == 0) {
1008 tcg_gen_shri_tl(reg
, cc
.reg
, ctztl(cc
.mask
));
1009 tcg_gen_andi_tl(reg
, reg
, 1);
1012 if (cc
.mask
!= -1) {
1013 tcg_gen_andi_tl(reg
, cc
.reg
, cc
.mask
);
1017 tcg_gen_setcond_tl(cc
.cond
, reg
, cc
.reg
, cc
.reg2
);
1019 tcg_gen_setcondi_tl(cc
.cond
, reg
, cc
.reg
, cc
.imm
);
1023 static inline void gen_compute_eflags_c(DisasContext
*s
, TCGv reg
)
1025 gen_setcc1(s
, JCC_B
<< 1, reg
);
1028 /* generate a conditional jump to label 'l1' according to jump opcode
1029 value 'b'. In the fast case, T0 is guaranted not to be used. */
1030 static inline void gen_jcc1_noeob(DisasContext
*s
, int b
, TCGLabel
*l1
)
1032 CCPrepare cc
= gen_prepare_cc(s
, b
, s
->T0
);
1034 if (cc
.mask
!= -1) {
1035 tcg_gen_andi_tl(s
->T0
, cc
.reg
, cc
.mask
);
1039 tcg_gen_brcond_tl(cc
.cond
, cc
.reg
, cc
.reg2
, l1
);
1041 tcg_gen_brcondi_tl(cc
.cond
, cc
.reg
, cc
.imm
, l1
);
1045 /* Generate a conditional jump to label 'l1' according to jump opcode
1046 value 'b'. In the fast case, T0 is guaranted not to be used.
1047 A translation block must end soon. */
1048 static inline void gen_jcc1(DisasContext
*s
, int b
, TCGLabel
*l1
)
1050 CCPrepare cc
= gen_prepare_cc(s
, b
, s
->T0
);
1052 gen_update_cc_op(s
);
1053 if (cc
.mask
!= -1) {
1054 tcg_gen_andi_tl(s
->T0
, cc
.reg
, cc
.mask
);
1057 set_cc_op(s
, CC_OP_DYNAMIC
);
1059 tcg_gen_brcond_tl(cc
.cond
, cc
.reg
, cc
.reg2
, l1
);
1061 tcg_gen_brcondi_tl(cc
.cond
, cc
.reg
, cc
.imm
, l1
);
1065 /* XXX: does not work with gdbstub "ice" single step - not a
1067 static TCGLabel
*gen_jz_ecx_string(DisasContext
*s
, target_ulong next_eip
)
1069 TCGLabel
*l1
= gen_new_label();
1070 TCGLabel
*l2
= gen_new_label();
1071 gen_op_jnz_ecx(s
, s
->aflag
, l1
);
1073 gen_jmp_tb(s
, next_eip
, 1);
1078 static inline void gen_stos(DisasContext
*s
, MemOp ot
)
1080 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, R_EAX
);
1081 gen_string_movl_A0_EDI(s
);
1082 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
1083 gen_op_movl_T0_Dshift(s
, ot
);
1084 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
1087 static inline void gen_lods(DisasContext
*s
, MemOp ot
)
1089 gen_string_movl_A0_ESI(s
);
1090 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1091 gen_op_mov_reg_v(s
, ot
, R_EAX
, s
->T0
);
1092 gen_op_movl_T0_Dshift(s
, ot
);
1093 gen_op_add_reg_T0(s
, s
->aflag
, R_ESI
);
1096 static inline void gen_scas(DisasContext
*s
, MemOp ot
)
1098 gen_string_movl_A0_EDI(s
);
1099 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
1100 gen_op(s
, OP_CMPL
, ot
, R_EAX
);
1101 gen_op_movl_T0_Dshift(s
, ot
);
1102 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
1105 static inline void gen_cmps(DisasContext
*s
, MemOp ot
)
1107 gen_string_movl_A0_EDI(s
);
1108 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
1109 gen_string_movl_A0_ESI(s
);
1110 gen_op(s
, OP_CMPL
, ot
, OR_TMP0
);
1111 gen_op_movl_T0_Dshift(s
, ot
);
1112 gen_op_add_reg_T0(s
, s
->aflag
, R_ESI
);
1113 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
1116 static void gen_bpt_io(DisasContext
*s
, TCGv_i32 t_port
, int ot
)
1118 if (s
->flags
& HF_IOBPT_MASK
) {
1119 TCGv_i32 t_size
= tcg_const_i32(1 << ot
);
1120 TCGv t_next
= tcg_const_tl(s
->pc
- s
->cs_base
);
1122 gen_helper_bpt_io(cpu_env
, t_port
, t_size
, t_next
);
1123 tcg_temp_free_i32(t_size
);
1124 tcg_temp_free(t_next
);
1129 static inline void gen_ins(DisasContext
*s
, MemOp ot
)
1131 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
1134 gen_string_movl_A0_EDI(s
);
1135 /* Note: we must do this dummy write first to be restartable in
1136 case of page fault. */
1137 tcg_gen_movi_tl(s
->T0
, 0);
1138 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
1139 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_EDX
]);
1140 tcg_gen_andi_i32(s
->tmp2_i32
, s
->tmp2_i32
, 0xffff);
1141 gen_helper_in_func(ot
, s
->T0
, s
->tmp2_i32
);
1142 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
1143 gen_op_movl_T0_Dshift(s
, ot
);
1144 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
1145 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
1146 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
1151 static inline void gen_outs(DisasContext
*s
, MemOp ot
)
1153 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
1156 gen_string_movl_A0_ESI(s
);
1157 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1159 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_EDX
]);
1160 tcg_gen_andi_i32(s
->tmp2_i32
, s
->tmp2_i32
, 0xffff);
1161 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T0
);
1162 gen_helper_out_func(ot
, s
->tmp2_i32
, s
->tmp3_i32
);
1163 gen_op_movl_T0_Dshift(s
, ot
);
1164 gen_op_add_reg_T0(s
, s
->aflag
, R_ESI
);
1165 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
1166 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
1171 /* same method as Valgrind : we generate jumps to current or next
1173 #define GEN_REPZ(op) \
1174 static inline void gen_repz_ ## op(DisasContext *s, MemOp ot, \
1175 target_ulong cur_eip, target_ulong next_eip) \
1178 gen_update_cc_op(s); \
1179 l2 = gen_jz_ecx_string(s, next_eip); \
1180 gen_ ## op(s, ot); \
1181 gen_op_add_reg_im(s, s->aflag, R_ECX, -1); \
1182 /* a loop would cause two single step exceptions if ECX = 1 \
1183 before rep string_insn */ \
1185 gen_op_jz_ecx(s, s->aflag, l2); \
1186 gen_jmp(s, cur_eip); \
1189 #define GEN_REPZ2(op) \
1190 static inline void gen_repz_ ## op(DisasContext *s, MemOp ot, \
1191 target_ulong cur_eip, \
1192 target_ulong next_eip, \
1196 gen_update_cc_op(s); \
1197 l2 = gen_jz_ecx_string(s, next_eip); \
1198 gen_ ## op(s, ot); \
1199 gen_op_add_reg_im(s, s->aflag, R_ECX, -1); \
1200 gen_update_cc_op(s); \
1201 gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2); \
1203 gen_op_jz_ecx(s, s->aflag, l2); \
1204 gen_jmp(s, cur_eip); \
1215 static void gen_helper_fp_arith_ST0_FT0(int op
)
1219 gen_helper_fadd_ST0_FT0(cpu_env
);
1222 gen_helper_fmul_ST0_FT0(cpu_env
);
1225 gen_helper_fcom_ST0_FT0(cpu_env
);
1228 gen_helper_fcom_ST0_FT0(cpu_env
);
1231 gen_helper_fsub_ST0_FT0(cpu_env
);
1234 gen_helper_fsubr_ST0_FT0(cpu_env
);
1237 gen_helper_fdiv_ST0_FT0(cpu_env
);
1240 gen_helper_fdivr_ST0_FT0(cpu_env
);
1245 /* NOTE the exception in "r" op ordering */
1246 static void gen_helper_fp_arith_STN_ST0(int op
, int opreg
)
1248 TCGv_i32 tmp
= tcg_const_i32(opreg
);
1251 gen_helper_fadd_STN_ST0(cpu_env
, tmp
);
1254 gen_helper_fmul_STN_ST0(cpu_env
, tmp
);
1257 gen_helper_fsubr_STN_ST0(cpu_env
, tmp
);
1260 gen_helper_fsub_STN_ST0(cpu_env
, tmp
);
1263 gen_helper_fdivr_STN_ST0(cpu_env
, tmp
);
1266 gen_helper_fdiv_STN_ST0(cpu_env
, tmp
);
1271 static void gen_exception(DisasContext
*s
, int trapno
, target_ulong cur_eip
)
1273 gen_update_cc_op(s
);
1274 gen_jmp_im(s
, cur_eip
);
1275 gen_helper_raise_exception(cpu_env
, tcg_const_i32(trapno
));
1276 s
->base
.is_jmp
= DISAS_NORETURN
;
1279 /* Generate #UD for the current instruction. The assumption here is that
1280 the instruction is known, but it isn't allowed in the current cpu mode. */
1281 static void gen_illegal_opcode(DisasContext
*s
)
1283 gen_exception(s
, EXCP06_ILLOP
, s
->pc_start
- s
->cs_base
);
1286 /* if d == OR_TMP0, it means memory operand (address in A0) */
1287 static void gen_op(DisasContext
*s1
, int op
, MemOp ot
, int d
)
1290 if (s1
->prefix
& PREFIX_LOCK
) {
1291 /* Lock prefix when destination is not memory. */
1292 gen_illegal_opcode(s1
);
1295 gen_op_mov_v_reg(s1
, ot
, s1
->T0
, d
);
1296 } else if (!(s1
->prefix
& PREFIX_LOCK
)) {
1297 gen_op_ld_v(s1
, ot
, s1
->T0
, s1
->A0
);
1301 gen_compute_eflags_c(s1
, s1
->tmp4
);
1302 if (s1
->prefix
& PREFIX_LOCK
) {
1303 tcg_gen_add_tl(s1
->T0
, s1
->tmp4
, s1
->T1
);
1304 tcg_gen_atomic_add_fetch_tl(s1
->T0
, s1
->A0
, s1
->T0
,
1305 s1
->mem_index
, ot
| MO_LE
);
1307 tcg_gen_add_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1308 tcg_gen_add_tl(s1
->T0
, s1
->T0
, s1
->tmp4
);
1309 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1311 gen_op_update3_cc(s1
, s1
->tmp4
);
1312 set_cc_op(s1
, CC_OP_ADCB
+ ot
);
1315 gen_compute_eflags_c(s1
, s1
->tmp4
);
1316 if (s1
->prefix
& PREFIX_LOCK
) {
1317 tcg_gen_add_tl(s1
->T0
, s1
->T1
, s1
->tmp4
);
1318 tcg_gen_neg_tl(s1
->T0
, s1
->T0
);
1319 tcg_gen_atomic_add_fetch_tl(s1
->T0
, s1
->A0
, s1
->T0
,
1320 s1
->mem_index
, ot
| MO_LE
);
1322 tcg_gen_sub_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1323 tcg_gen_sub_tl(s1
->T0
, s1
->T0
, s1
->tmp4
);
1324 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1326 gen_op_update3_cc(s1
, s1
->tmp4
);
1327 set_cc_op(s1
, CC_OP_SBBB
+ ot
);
1330 if (s1
->prefix
& PREFIX_LOCK
) {
1331 tcg_gen_atomic_add_fetch_tl(s1
->T0
, s1
->A0
, s1
->T1
,
1332 s1
->mem_index
, ot
| MO_LE
);
1334 tcg_gen_add_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1335 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1337 gen_op_update2_cc(s1
);
1338 set_cc_op(s1
, CC_OP_ADDB
+ ot
);
1341 if (s1
->prefix
& PREFIX_LOCK
) {
1342 tcg_gen_neg_tl(s1
->T0
, s1
->T1
);
1343 tcg_gen_atomic_fetch_add_tl(s1
->cc_srcT
, s1
->A0
, s1
->T0
,
1344 s1
->mem_index
, ot
| MO_LE
);
1345 tcg_gen_sub_tl(s1
->T0
, s1
->cc_srcT
, s1
->T1
);
1347 tcg_gen_mov_tl(s1
->cc_srcT
, s1
->T0
);
1348 tcg_gen_sub_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1349 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1351 gen_op_update2_cc(s1
);
1352 set_cc_op(s1
, CC_OP_SUBB
+ ot
);
1356 if (s1
->prefix
& PREFIX_LOCK
) {
1357 tcg_gen_atomic_and_fetch_tl(s1
->T0
, s1
->A0
, s1
->T1
,
1358 s1
->mem_index
, ot
| MO_LE
);
1360 tcg_gen_and_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1361 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1363 gen_op_update1_cc(s1
);
1364 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1367 if (s1
->prefix
& PREFIX_LOCK
) {
1368 tcg_gen_atomic_or_fetch_tl(s1
->T0
, s1
->A0
, s1
->T1
,
1369 s1
->mem_index
, ot
| MO_LE
);
1371 tcg_gen_or_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1372 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1374 gen_op_update1_cc(s1
);
1375 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1378 if (s1
->prefix
& PREFIX_LOCK
) {
1379 tcg_gen_atomic_xor_fetch_tl(s1
->T0
, s1
->A0
, s1
->T1
,
1380 s1
->mem_index
, ot
| MO_LE
);
1382 tcg_gen_xor_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1383 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1385 gen_op_update1_cc(s1
);
1386 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1389 tcg_gen_mov_tl(cpu_cc_src
, s1
->T1
);
1390 tcg_gen_mov_tl(s1
->cc_srcT
, s1
->T0
);
1391 tcg_gen_sub_tl(cpu_cc_dst
, s1
->T0
, s1
->T1
);
1392 set_cc_op(s1
, CC_OP_SUBB
+ ot
);
1397 /* if d == OR_TMP0, it means memory operand (address in A0) */
1398 static void gen_inc(DisasContext
*s1
, MemOp ot
, int d
, int c
)
1400 if (s1
->prefix
& PREFIX_LOCK
) {
1402 /* Lock prefix when destination is not memory */
1403 gen_illegal_opcode(s1
);
1406 tcg_gen_movi_tl(s1
->T0
, c
> 0 ? 1 : -1);
1407 tcg_gen_atomic_add_fetch_tl(s1
->T0
, s1
->A0
, s1
->T0
,
1408 s1
->mem_index
, ot
| MO_LE
);
1411 gen_op_mov_v_reg(s1
, ot
, s1
->T0
, d
);
1413 gen_op_ld_v(s1
, ot
, s1
->T0
, s1
->A0
);
1415 tcg_gen_addi_tl(s1
->T0
, s1
->T0
, (c
> 0 ? 1 : -1));
1416 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1419 gen_compute_eflags_c(s1
, cpu_cc_src
);
1420 tcg_gen_mov_tl(cpu_cc_dst
, s1
->T0
);
1421 set_cc_op(s1
, (c
> 0 ? CC_OP_INCB
: CC_OP_DECB
) + ot
);
1424 static void gen_shift_flags(DisasContext
*s
, MemOp ot
, TCGv result
,
1425 TCGv shm1
, TCGv count
, bool is_right
)
1427 TCGv_i32 z32
, s32
, oldop
;
1430 /* Store the results into the CC variables. If we know that the
1431 variable must be dead, store unconditionally. Otherwise we'll
1432 need to not disrupt the current contents. */
1433 z_tl
= tcg_const_tl(0);
1434 if (cc_op_live
[s
->cc_op
] & USES_CC_DST
) {
1435 tcg_gen_movcond_tl(TCG_COND_NE
, cpu_cc_dst
, count
, z_tl
,
1436 result
, cpu_cc_dst
);
1438 tcg_gen_mov_tl(cpu_cc_dst
, result
);
1440 if (cc_op_live
[s
->cc_op
] & USES_CC_SRC
) {
1441 tcg_gen_movcond_tl(TCG_COND_NE
, cpu_cc_src
, count
, z_tl
,
1444 tcg_gen_mov_tl(cpu_cc_src
, shm1
);
1446 tcg_temp_free(z_tl
);
1448 /* Get the two potential CC_OP values into temporaries. */
1449 tcg_gen_movi_i32(s
->tmp2_i32
, (is_right
? CC_OP_SARB
: CC_OP_SHLB
) + ot
);
1450 if (s
->cc_op
== CC_OP_DYNAMIC
) {
1453 tcg_gen_movi_i32(s
->tmp3_i32
, s
->cc_op
);
1454 oldop
= s
->tmp3_i32
;
1457 /* Conditionally store the CC_OP value. */
1458 z32
= tcg_const_i32(0);
1459 s32
= tcg_temp_new_i32();
1460 tcg_gen_trunc_tl_i32(s32
, count
);
1461 tcg_gen_movcond_i32(TCG_COND_NE
, cpu_cc_op
, s32
, z32
, s
->tmp2_i32
, oldop
);
1462 tcg_temp_free_i32(z32
);
1463 tcg_temp_free_i32(s32
);
1465 /* The CC_OP value is no longer predictable. */
1466 set_cc_op(s
, CC_OP_DYNAMIC
);
1469 static void gen_shift_rm_T1(DisasContext
*s
, MemOp ot
, int op1
,
1470 int is_right
, int is_arith
)
1472 target_ulong mask
= (ot
== MO_64
? 0x3f : 0x1f);
1475 if (op1
== OR_TMP0
) {
1476 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1478 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1481 tcg_gen_andi_tl(s
->T1
, s
->T1
, mask
);
1482 tcg_gen_subi_tl(s
->tmp0
, s
->T1
, 1);
1486 gen_exts(ot
, s
->T0
);
1487 tcg_gen_sar_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
1488 tcg_gen_sar_tl(s
->T0
, s
->T0
, s
->T1
);
1490 gen_extu(ot
, s
->T0
);
1491 tcg_gen_shr_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
1492 tcg_gen_shr_tl(s
->T0
, s
->T0
, s
->T1
);
1495 tcg_gen_shl_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
1496 tcg_gen_shl_tl(s
->T0
, s
->T0
, s
->T1
);
1500 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1502 gen_shift_flags(s
, ot
, s
->T0
, s
->tmp0
, s
->T1
, is_right
);
1505 static void gen_shift_rm_im(DisasContext
*s
, MemOp ot
, int op1
, int op2
,
1506 int is_right
, int is_arith
)
1508 int mask
= (ot
== MO_64
? 0x3f : 0x1f);
1512 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1514 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1520 gen_exts(ot
, s
->T0
);
1521 tcg_gen_sari_tl(s
->tmp4
, s
->T0
, op2
- 1);
1522 tcg_gen_sari_tl(s
->T0
, s
->T0
, op2
);
1524 gen_extu(ot
, s
->T0
);
1525 tcg_gen_shri_tl(s
->tmp4
, s
->T0
, op2
- 1);
1526 tcg_gen_shri_tl(s
->T0
, s
->T0
, op2
);
1529 tcg_gen_shli_tl(s
->tmp4
, s
->T0
, op2
- 1);
1530 tcg_gen_shli_tl(s
->T0
, s
->T0
, op2
);
1535 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1537 /* update eflags if non zero shift */
1539 tcg_gen_mov_tl(cpu_cc_src
, s
->tmp4
);
1540 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
1541 set_cc_op(s
, (is_right
? CC_OP_SARB
: CC_OP_SHLB
) + ot
);
1545 static void gen_rot_rm_T1(DisasContext
*s
, MemOp ot
, int op1
, int is_right
)
1547 target_ulong mask
= (ot
== MO_64
? 0x3f : 0x1f);
1551 if (op1
== OR_TMP0
) {
1552 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1554 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1557 tcg_gen_andi_tl(s
->T1
, s
->T1
, mask
);
1561 /* Replicate the 8-bit input so that a 32-bit rotate works. */
1562 tcg_gen_ext8u_tl(s
->T0
, s
->T0
);
1563 tcg_gen_muli_tl(s
->T0
, s
->T0
, 0x01010101);
1566 /* Replicate the 16-bit input so that a 32-bit rotate works. */
1567 tcg_gen_deposit_tl(s
->T0
, s
->T0
, s
->T0
, 16, 16);
1570 #ifdef TARGET_X86_64
1572 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
1573 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T1
);
1575 tcg_gen_rotr_i32(s
->tmp2_i32
, s
->tmp2_i32
, s
->tmp3_i32
);
1577 tcg_gen_rotl_i32(s
->tmp2_i32
, s
->tmp2_i32
, s
->tmp3_i32
);
1579 tcg_gen_extu_i32_tl(s
->T0
, s
->tmp2_i32
);
1584 tcg_gen_rotr_tl(s
->T0
, s
->T0
, s
->T1
);
1586 tcg_gen_rotl_tl(s
->T0
, s
->T0
, s
->T1
);
1592 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1594 /* We'll need the flags computed into CC_SRC. */
1595 gen_compute_eflags(s
);
1597 /* The value that was "rotated out" is now present at the other end
1598 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1599 since we've computed the flags into CC_SRC, these variables are
1602 tcg_gen_shri_tl(cpu_cc_src2
, s
->T0
, mask
- 1);
1603 tcg_gen_shri_tl(cpu_cc_dst
, s
->T0
, mask
);
1604 tcg_gen_andi_tl(cpu_cc_dst
, cpu_cc_dst
, 1);
1606 tcg_gen_shri_tl(cpu_cc_src2
, s
->T0
, mask
);
1607 tcg_gen_andi_tl(cpu_cc_dst
, s
->T0
, 1);
1609 tcg_gen_andi_tl(cpu_cc_src2
, cpu_cc_src2
, 1);
1610 tcg_gen_xor_tl(cpu_cc_src2
, cpu_cc_src2
, cpu_cc_dst
);
1612 /* Now conditionally store the new CC_OP value. If the shift count
1613 is 0 we keep the CC_OP_EFLAGS setting so that only CC_SRC is live.
1614 Otherwise reuse CC_OP_ADCOX which have the C and O flags split out
1615 exactly as we computed above. */
1616 t0
= tcg_const_i32(0);
1617 t1
= tcg_temp_new_i32();
1618 tcg_gen_trunc_tl_i32(t1
, s
->T1
);
1619 tcg_gen_movi_i32(s
->tmp2_i32
, CC_OP_ADCOX
);
1620 tcg_gen_movi_i32(s
->tmp3_i32
, CC_OP_EFLAGS
);
1621 tcg_gen_movcond_i32(TCG_COND_NE
, cpu_cc_op
, t1
, t0
,
1622 s
->tmp2_i32
, s
->tmp3_i32
);
1623 tcg_temp_free_i32(t0
);
1624 tcg_temp_free_i32(t1
);
1626 /* The CC_OP value is no longer predictable. */
1627 set_cc_op(s
, CC_OP_DYNAMIC
);
1630 static void gen_rot_rm_im(DisasContext
*s
, MemOp ot
, int op1
, int op2
,
1633 int mask
= (ot
== MO_64
? 0x3f : 0x1f);
1637 if (op1
== OR_TMP0
) {
1638 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1640 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1646 #ifdef TARGET_X86_64
1648 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
1650 tcg_gen_rotri_i32(s
->tmp2_i32
, s
->tmp2_i32
, op2
);
1652 tcg_gen_rotli_i32(s
->tmp2_i32
, s
->tmp2_i32
, op2
);
1654 tcg_gen_extu_i32_tl(s
->T0
, s
->tmp2_i32
);
1659 tcg_gen_rotri_tl(s
->T0
, s
->T0
, op2
);
1661 tcg_gen_rotli_tl(s
->T0
, s
->T0
, op2
);
1672 shift
= mask
+ 1 - shift
;
1674 gen_extu(ot
, s
->T0
);
1675 tcg_gen_shli_tl(s
->tmp0
, s
->T0
, shift
);
1676 tcg_gen_shri_tl(s
->T0
, s
->T0
, mask
+ 1 - shift
);
1677 tcg_gen_or_tl(s
->T0
, s
->T0
, s
->tmp0
);
1683 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1686 /* Compute the flags into CC_SRC. */
1687 gen_compute_eflags(s
);
1689 /* The value that was "rotated out" is now present at the other end
1690 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1691 since we've computed the flags into CC_SRC, these variables are
1694 tcg_gen_shri_tl(cpu_cc_src2
, s
->T0
, mask
- 1);
1695 tcg_gen_shri_tl(cpu_cc_dst
, s
->T0
, mask
);
1696 tcg_gen_andi_tl(cpu_cc_dst
, cpu_cc_dst
, 1);
1698 tcg_gen_shri_tl(cpu_cc_src2
, s
->T0
, mask
);
1699 tcg_gen_andi_tl(cpu_cc_dst
, s
->T0
, 1);
1701 tcg_gen_andi_tl(cpu_cc_src2
, cpu_cc_src2
, 1);
1702 tcg_gen_xor_tl(cpu_cc_src2
, cpu_cc_src2
, cpu_cc_dst
);
1703 set_cc_op(s
, CC_OP_ADCOX
);
1707 /* XXX: add faster immediate = 1 case */
1708 static void gen_rotc_rm_T1(DisasContext
*s
, MemOp ot
, int op1
,
1711 gen_compute_eflags(s
);
1712 assert(s
->cc_op
== CC_OP_EFLAGS
);
1716 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1718 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1723 gen_helper_rcrb(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1726 gen_helper_rcrw(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1729 gen_helper_rcrl(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1731 #ifdef TARGET_X86_64
1733 gen_helper_rcrq(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1742 gen_helper_rclb(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1745 gen_helper_rclw(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1748 gen_helper_rcll(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1750 #ifdef TARGET_X86_64
1752 gen_helper_rclq(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1760 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1763 /* XXX: add faster immediate case */
1764 static void gen_shiftd_rm_T1(DisasContext
*s
, MemOp ot
, int op1
,
1765 bool is_right
, TCGv count_in
)
1767 target_ulong mask
= (ot
== MO_64
? 63 : 31);
1771 if (op1
== OR_TMP0
) {
1772 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1774 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1777 count
= tcg_temp_new();
1778 tcg_gen_andi_tl(count
, count_in
, mask
);
1782 /* Note: we implement the Intel behaviour for shift count > 16.
1783 This means "shrdw C, B, A" shifts A:B:A >> C. Build the B:A
1784 portion by constructing it as a 32-bit value. */
1786 tcg_gen_deposit_tl(s
->tmp0
, s
->T0
, s
->T1
, 16, 16);
1787 tcg_gen_mov_tl(s
->T1
, s
->T0
);
1788 tcg_gen_mov_tl(s
->T0
, s
->tmp0
);
1790 tcg_gen_deposit_tl(s
->T1
, s
->T0
, s
->T1
, 16, 16);
1793 #ifdef TARGET_X86_64
1795 /* Concatenate the two 32-bit values and use a 64-bit shift. */
1796 tcg_gen_subi_tl(s
->tmp0
, count
, 1);
1798 tcg_gen_concat_tl_i64(s
->T0
, s
->T0
, s
->T1
);
1799 tcg_gen_shr_i64(s
->tmp0
, s
->T0
, s
->tmp0
);
1800 tcg_gen_shr_i64(s
->T0
, s
->T0
, count
);
1802 tcg_gen_concat_tl_i64(s
->T0
, s
->T1
, s
->T0
);
1803 tcg_gen_shl_i64(s
->tmp0
, s
->T0
, s
->tmp0
);
1804 tcg_gen_shl_i64(s
->T0
, s
->T0
, count
);
1805 tcg_gen_shri_i64(s
->tmp0
, s
->tmp0
, 32);
1806 tcg_gen_shri_i64(s
->T0
, s
->T0
, 32);
1811 tcg_gen_subi_tl(s
->tmp0
, count
, 1);
1813 tcg_gen_shr_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
1815 tcg_gen_subfi_tl(s
->tmp4
, mask
+ 1, count
);
1816 tcg_gen_shr_tl(s
->T0
, s
->T0
, count
);
1817 tcg_gen_shl_tl(s
->T1
, s
->T1
, s
->tmp4
);
1819 tcg_gen_shl_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
1821 /* Only needed if count > 16, for Intel behaviour. */
1822 tcg_gen_subfi_tl(s
->tmp4
, 33, count
);
1823 tcg_gen_shr_tl(s
->tmp4
, s
->T1
, s
->tmp4
);
1824 tcg_gen_or_tl(s
->tmp0
, s
->tmp0
, s
->tmp4
);
1827 tcg_gen_subfi_tl(s
->tmp4
, mask
+ 1, count
);
1828 tcg_gen_shl_tl(s
->T0
, s
->T0
, count
);
1829 tcg_gen_shr_tl(s
->T1
, s
->T1
, s
->tmp4
);
1831 tcg_gen_movi_tl(s
->tmp4
, 0);
1832 tcg_gen_movcond_tl(TCG_COND_EQ
, s
->T1
, count
, s
->tmp4
,
1834 tcg_gen_or_tl(s
->T0
, s
->T0
, s
->T1
);
1839 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1841 gen_shift_flags(s
, ot
, s
->T0
, s
->tmp0
, count
, is_right
);
1842 tcg_temp_free(count
);
1845 static void gen_shift(DisasContext
*s1
, int op
, MemOp ot
, int d
, int s
)
1848 gen_op_mov_v_reg(s1
, ot
, s1
->T1
, s
);
1851 gen_rot_rm_T1(s1
, ot
, d
, 0);
1854 gen_rot_rm_T1(s1
, ot
, d
, 1);
1858 gen_shift_rm_T1(s1
, ot
, d
, 0, 0);
1861 gen_shift_rm_T1(s1
, ot
, d
, 1, 0);
1864 gen_shift_rm_T1(s1
, ot
, d
, 1, 1);
1867 gen_rotc_rm_T1(s1
, ot
, d
, 0);
1870 gen_rotc_rm_T1(s1
, ot
, d
, 1);
1875 static void gen_shifti(DisasContext
*s1
, int op
, MemOp ot
, int d
, int c
)
1879 gen_rot_rm_im(s1
, ot
, d
, c
, 0);
1882 gen_rot_rm_im(s1
, ot
, d
, c
, 1);
1886 gen_shift_rm_im(s1
, ot
, d
, c
, 0, 0);
1889 gen_shift_rm_im(s1
, ot
, d
, c
, 1, 0);
1892 gen_shift_rm_im(s1
, ot
, d
, c
, 1, 1);
1895 /* currently not optimized */
1896 tcg_gen_movi_tl(s1
->T1
, c
);
1897 gen_shift(s1
, op
, ot
, d
, OR_TMP1
);
1902 #define X86_MAX_INSN_LENGTH 15
1904 static uint64_t advance_pc(CPUX86State
*env
, DisasContext
*s
, int num_bytes
)
1906 uint64_t pc
= s
->pc
;
1909 if (unlikely(s
->pc
- s
->pc_start
> X86_MAX_INSN_LENGTH
)) {
1910 /* If the instruction's 16th byte is on a different page than the 1st, a
1911 * page fault on the second page wins over the general protection fault
1912 * caused by the instruction being too long.
1913 * This can happen even if the operand is only one byte long!
1915 if (((s
->pc
- 1) ^ (pc
- 1)) & TARGET_PAGE_MASK
) {
1916 volatile uint8_t unused
=
1917 cpu_ldub_code(env
, (s
->pc
- 1) & TARGET_PAGE_MASK
);
1920 siglongjmp(s
->jmpbuf
, 1);
1926 static inline uint8_t x86_ldub_code(CPUX86State
*env
, DisasContext
*s
)
1928 return translator_ldub(env
, advance_pc(env
, s
, 1));
1931 static inline int16_t x86_ldsw_code(CPUX86State
*env
, DisasContext
*s
)
1933 return translator_ldsw(env
, advance_pc(env
, s
, 2));
1936 static inline uint16_t x86_lduw_code(CPUX86State
*env
, DisasContext
*s
)
1938 return translator_lduw(env
, advance_pc(env
, s
, 2));
1941 static inline uint32_t x86_ldl_code(CPUX86State
*env
, DisasContext
*s
)
1943 return translator_ldl(env
, advance_pc(env
, s
, 4));
1946 #ifdef TARGET_X86_64
1947 static inline uint64_t x86_ldq_code(CPUX86State
*env
, DisasContext
*s
)
1949 return translator_ldq(env
, advance_pc(env
, s
, 8));
1953 /* Decompose an address. */
1955 typedef struct AddressParts
{
1963 static AddressParts
gen_lea_modrm_0(CPUX86State
*env
, DisasContext
*s
,
1966 int def_seg
, base
, index
, scale
, mod
, rm
;
1975 mod
= (modrm
>> 6) & 3;
1977 base
= rm
| REX_B(s
);
1980 /* Normally filtered out earlier, but including this path
1981 simplifies multi-byte nop, as well as bndcl, bndcu, bndcn. */
1990 int code
= x86_ldub_code(env
, s
);
1991 scale
= (code
>> 6) & 3;
1992 index
= ((code
>> 3) & 7) | REX_X(s
);
1994 index
= -1; /* no index */
1996 base
= (code
& 7) | REX_B(s
);
2002 if ((base
& 7) == 5) {
2004 disp
= (int32_t)x86_ldl_code(env
, s
);
2005 if (CODE64(s
) && !havesib
) {
2007 disp
+= s
->pc
+ s
->rip_offset
;
2012 disp
= (int8_t)x86_ldub_code(env
, s
);
2016 disp
= (int32_t)x86_ldl_code(env
, s
);
2020 /* For correct popl handling with esp. */
2021 if (base
== R_ESP
&& s
->popl_esp_hack
) {
2022 disp
+= s
->popl_esp_hack
;
2024 if (base
== R_EBP
|| base
== R_ESP
) {
2033 disp
= x86_lduw_code(env
, s
);
2036 } else if (mod
== 1) {
2037 disp
= (int8_t)x86_ldub_code(env
, s
);
2039 disp
= (int16_t)x86_lduw_code(env
, s
);
2083 return (AddressParts
){ def_seg
, base
, index
, scale
, disp
};
2086 /* Compute the address, with a minimum number of TCG ops. */
2087 static TCGv
gen_lea_modrm_1(DisasContext
*s
, AddressParts a
)
2093 ea
= cpu_regs
[a
.index
];
2095 tcg_gen_shli_tl(s
->A0
, cpu_regs
[a
.index
], a
.scale
);
2099 tcg_gen_add_tl(s
->A0
, ea
, cpu_regs
[a
.base
]);
2102 } else if (a
.base
>= 0) {
2103 ea
= cpu_regs
[a
.base
];
2106 tcg_gen_movi_tl(s
->A0
, a
.disp
);
2108 } else if (a
.disp
!= 0) {
2109 tcg_gen_addi_tl(s
->A0
, ea
, a
.disp
);
2116 static void gen_lea_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
)
2118 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
2119 TCGv ea
= gen_lea_modrm_1(s
, a
);
2120 gen_lea_v_seg(s
, s
->aflag
, ea
, a
.def_seg
, s
->override
);
2123 static void gen_nop_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
)
2125 (void)gen_lea_modrm_0(env
, s
, modrm
);
2128 /* Used for BNDCL, BNDCU, BNDCN. */
2129 static void gen_bndck(CPUX86State
*env
, DisasContext
*s
, int modrm
,
2130 TCGCond cond
, TCGv_i64 bndv
)
2132 TCGv ea
= gen_lea_modrm_1(s
, gen_lea_modrm_0(env
, s
, modrm
));
2134 tcg_gen_extu_tl_i64(s
->tmp1_i64
, ea
);
2136 tcg_gen_ext32u_i64(s
->tmp1_i64
, s
->tmp1_i64
);
2138 tcg_gen_setcond_i64(cond
, s
->tmp1_i64
, s
->tmp1_i64
, bndv
);
2139 tcg_gen_extrl_i64_i32(s
->tmp2_i32
, s
->tmp1_i64
);
2140 gen_helper_bndck(cpu_env
, s
->tmp2_i32
);
2143 /* used for LEA and MOV AX, mem */
2144 static void gen_add_A0_ds_seg(DisasContext
*s
)
2146 gen_lea_v_seg(s
, s
->aflag
, s
->A0
, R_DS
, s
->override
);
2149 /* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
2151 static void gen_ldst_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
,
2152 MemOp ot
, int reg
, int is_store
)
2156 mod
= (modrm
>> 6) & 3;
2157 rm
= (modrm
& 7) | REX_B(s
);
2161 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
2162 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
2164 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
2166 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
2169 gen_lea_modrm(env
, s
, modrm
);
2172 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
2173 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
2175 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
2177 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
2182 static inline uint32_t insn_get(CPUX86State
*env
, DisasContext
*s
, MemOp ot
)
2188 ret
= x86_ldub_code(env
, s
);
2191 ret
= x86_lduw_code(env
, s
);
2194 #ifdef TARGET_X86_64
2197 ret
= x86_ldl_code(env
, s
);
2205 static inline int insn_const_size(MemOp ot
)
2214 static inline bool use_goto_tb(DisasContext
*s
, target_ulong pc
)
2216 #ifndef CONFIG_USER_ONLY
2217 return (pc
& TARGET_PAGE_MASK
) == (s
->base
.tb
->pc
& TARGET_PAGE_MASK
) ||
2218 (pc
& TARGET_PAGE_MASK
) == (s
->pc_start
& TARGET_PAGE_MASK
);
2224 static inline void gen_goto_tb(DisasContext
*s
, int tb_num
, target_ulong eip
)
2226 target_ulong pc
= s
->cs_base
+ eip
;
2228 if (use_goto_tb(s
, pc
)) {
2229 /* jump to same page: we can use a direct jump */
2230 tcg_gen_goto_tb(tb_num
);
2232 tcg_gen_exit_tb(s
->base
.tb
, tb_num
);
2233 s
->base
.is_jmp
= DISAS_NORETURN
;
2235 /* jump to another page */
2241 static inline void gen_jcc(DisasContext
*s
, int b
,
2242 target_ulong val
, target_ulong next_eip
)
2247 l1
= gen_new_label();
2250 gen_goto_tb(s
, 0, next_eip
);
2253 gen_goto_tb(s
, 1, val
);
2255 l1
= gen_new_label();
2256 l2
= gen_new_label();
2259 gen_jmp_im(s
, next_eip
);
2269 static void gen_cmovcc1(CPUX86State
*env
, DisasContext
*s
, MemOp ot
, int b
,
2274 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
2276 cc
= gen_prepare_cc(s
, b
, s
->T1
);
2277 if (cc
.mask
!= -1) {
2278 TCGv t0
= tcg_temp_new();
2279 tcg_gen_andi_tl(t0
, cc
.reg
, cc
.mask
);
2283 cc
.reg2
= tcg_const_tl(cc
.imm
);
2286 tcg_gen_movcond_tl(cc
.cond
, s
->T0
, cc
.reg
, cc
.reg2
,
2287 s
->T0
, cpu_regs
[reg
]);
2288 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
2290 if (cc
.mask
!= -1) {
2291 tcg_temp_free(cc
.reg
);
2294 tcg_temp_free(cc
.reg2
);
2298 static inline void gen_op_movl_T0_seg(DisasContext
*s
, int seg_reg
)
2300 tcg_gen_ld32u_tl(s
->T0
, cpu_env
,
2301 offsetof(CPUX86State
,segs
[seg_reg
].selector
));
2304 static inline void gen_op_movl_seg_T0_vm(DisasContext
*s
, int seg_reg
)
2306 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
2307 tcg_gen_st32_tl(s
->T0
, cpu_env
,
2308 offsetof(CPUX86State
,segs
[seg_reg
].selector
));
2309 tcg_gen_shli_tl(cpu_seg_base
[seg_reg
], s
->T0
, 4);
2312 /* move T0 to seg_reg and compute if the CPU state may change. Never
2313 call this function with seg_reg == R_CS */
2314 static void gen_movl_seg_T0(DisasContext
*s
, int seg_reg
)
2316 if (s
->pe
&& !s
->vm86
) {
2317 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
2318 gen_helper_load_seg(cpu_env
, tcg_const_i32(seg_reg
), s
->tmp2_i32
);
2319 /* abort translation because the addseg value may change or
2320 because ss32 may change. For R_SS, translation must always
2321 stop as a special handling must be done to disable hardware
2322 interrupts for the next instruction */
2323 if (seg_reg
== R_SS
|| (s
->code32
&& seg_reg
< R_FS
)) {
2324 s
->base
.is_jmp
= DISAS_TOO_MANY
;
2327 gen_op_movl_seg_T0_vm(s
, seg_reg
);
2328 if (seg_reg
== R_SS
) {
2329 s
->base
.is_jmp
= DISAS_TOO_MANY
;
2334 static inline int svm_is_rep(int prefixes
)
2336 return ((prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) ? 8 : 0);
2340 gen_svm_check_intercept_param(DisasContext
*s
, target_ulong pc_start
,
2341 uint32_t type
, uint64_t param
)
2343 /* no SVM activated; fast case */
2344 if (likely(!(s
->flags
& HF_GUEST_MASK
)))
2346 gen_update_cc_op(s
);
2347 gen_jmp_im(s
, pc_start
- s
->cs_base
);
2348 gen_helper_svm_check_intercept_param(cpu_env
, tcg_const_i32(type
),
2349 tcg_const_i64(param
));
2353 gen_svm_check_intercept(DisasContext
*s
, target_ulong pc_start
, uint64_t type
)
2355 gen_svm_check_intercept_param(s
, pc_start
, type
, 0);
2358 static inline void gen_stack_update(DisasContext
*s
, int addend
)
2360 gen_op_add_reg_im(s
, mo_stacksize(s
), R_ESP
, addend
);
2363 /* Generate a push. It depends on ss32, addseg and dflag. */
2364 static void gen_push_v(DisasContext
*s
, TCGv val
)
2366 MemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2367 MemOp a_ot
= mo_stacksize(s
);
2368 int size
= 1 << d_ot
;
2369 TCGv new_esp
= s
->A0
;
2371 tcg_gen_subi_tl(s
->A0
, cpu_regs
[R_ESP
], size
);
2376 tcg_gen_mov_tl(new_esp
, s
->A0
);
2378 gen_lea_v_seg(s
, a_ot
, s
->A0
, R_SS
, -1);
2381 gen_op_st_v(s
, d_ot
, val
, s
->A0
);
2382 gen_op_mov_reg_v(s
, a_ot
, R_ESP
, new_esp
);
2385 /* two step pop is necessary for precise exceptions */
2386 static MemOp
gen_pop_T0(DisasContext
*s
)
2388 MemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2390 gen_lea_v_seg(s
, mo_stacksize(s
), cpu_regs
[R_ESP
], R_SS
, -1);
2391 gen_op_ld_v(s
, d_ot
, s
->T0
, s
->A0
);
2396 static inline void gen_pop_update(DisasContext
*s
, MemOp ot
)
2398 gen_stack_update(s
, 1 << ot
);
2401 static inline void gen_stack_A0(DisasContext
*s
)
2403 gen_lea_v_seg(s
, s
->ss32
? MO_32
: MO_16
, cpu_regs
[R_ESP
], R_SS
, -1);
2406 static void gen_pusha(DisasContext
*s
)
2408 MemOp s_ot
= s
->ss32
? MO_32
: MO_16
;
2409 MemOp d_ot
= s
->dflag
;
2410 int size
= 1 << d_ot
;
2413 for (i
= 0; i
< 8; i
++) {
2414 tcg_gen_addi_tl(s
->A0
, cpu_regs
[R_ESP
], (i
- 8) * size
);
2415 gen_lea_v_seg(s
, s_ot
, s
->A0
, R_SS
, -1);
2416 gen_op_st_v(s
, d_ot
, cpu_regs
[7 - i
], s
->A0
);
2419 gen_stack_update(s
, -8 * size
);
2422 static void gen_popa(DisasContext
*s
)
2424 MemOp s_ot
= s
->ss32
? MO_32
: MO_16
;
2425 MemOp d_ot
= s
->dflag
;
2426 int size
= 1 << d_ot
;
2429 for (i
= 0; i
< 8; i
++) {
2430 /* ESP is not reloaded */
2431 if (7 - i
== R_ESP
) {
2434 tcg_gen_addi_tl(s
->A0
, cpu_regs
[R_ESP
], i
* size
);
2435 gen_lea_v_seg(s
, s_ot
, s
->A0
, R_SS
, -1);
2436 gen_op_ld_v(s
, d_ot
, s
->T0
, s
->A0
);
2437 gen_op_mov_reg_v(s
, d_ot
, 7 - i
, s
->T0
);
2440 gen_stack_update(s
, 8 * size
);
2443 static void gen_enter(DisasContext
*s
, int esp_addend
, int level
)
2445 MemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2446 MemOp a_ot
= CODE64(s
) ? MO_64
: s
->ss32
? MO_32
: MO_16
;
2447 int size
= 1 << d_ot
;
2449 /* Push BP; compute FrameTemp into T1. */
2450 tcg_gen_subi_tl(s
->T1
, cpu_regs
[R_ESP
], size
);
2451 gen_lea_v_seg(s
, a_ot
, s
->T1
, R_SS
, -1);
2452 gen_op_st_v(s
, d_ot
, cpu_regs
[R_EBP
], s
->A0
);
2458 /* Copy level-1 pointers from the previous frame. */
2459 for (i
= 1; i
< level
; ++i
) {
2460 tcg_gen_subi_tl(s
->A0
, cpu_regs
[R_EBP
], size
* i
);
2461 gen_lea_v_seg(s
, a_ot
, s
->A0
, R_SS
, -1);
2462 gen_op_ld_v(s
, d_ot
, s
->tmp0
, s
->A0
);
2464 tcg_gen_subi_tl(s
->A0
, s
->T1
, size
* i
);
2465 gen_lea_v_seg(s
, a_ot
, s
->A0
, R_SS
, -1);
2466 gen_op_st_v(s
, d_ot
, s
->tmp0
, s
->A0
);
2469 /* Push the current FrameTemp as the last level. */
2470 tcg_gen_subi_tl(s
->A0
, s
->T1
, size
* level
);
2471 gen_lea_v_seg(s
, a_ot
, s
->A0
, R_SS
, -1);
2472 gen_op_st_v(s
, d_ot
, s
->T1
, s
->A0
);
2475 /* Copy the FrameTemp value to EBP. */
2476 gen_op_mov_reg_v(s
, a_ot
, R_EBP
, s
->T1
);
2478 /* Compute the final value of ESP. */
2479 tcg_gen_subi_tl(s
->T1
, s
->T1
, esp_addend
+ size
* level
);
2480 gen_op_mov_reg_v(s
, a_ot
, R_ESP
, s
->T1
);
2483 static void gen_leave(DisasContext
*s
)
2485 MemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2486 MemOp a_ot
= mo_stacksize(s
);
2488 gen_lea_v_seg(s
, a_ot
, cpu_regs
[R_EBP
], R_SS
, -1);
2489 gen_op_ld_v(s
, d_ot
, s
->T0
, s
->A0
);
2491 tcg_gen_addi_tl(s
->T1
, cpu_regs
[R_EBP
], 1 << d_ot
);
2493 gen_op_mov_reg_v(s
, d_ot
, R_EBP
, s
->T0
);
2494 gen_op_mov_reg_v(s
, a_ot
, R_ESP
, s
->T1
);
2497 /* Similarly, except that the assumption here is that we don't decode
2498 the instruction at all -- either a missing opcode, an unimplemented
2499 feature, or just a bogus instruction stream. */
2500 static void gen_unknown_opcode(CPUX86State
*env
, DisasContext
*s
)
2502 gen_illegal_opcode(s
);
2504 if (qemu_loglevel_mask(LOG_UNIMP
)) {
2505 FILE *logfile
= qemu_log_lock();
2506 target_ulong pc
= s
->pc_start
, end
= s
->pc
;
2508 qemu_log("ILLOPC: " TARGET_FMT_lx
":", pc
);
2509 for (; pc
< end
; ++pc
) {
2510 qemu_log(" %02x", cpu_ldub_code(env
, pc
));
2513 qemu_log_unlock(logfile
);
2517 /* an interrupt is different from an exception because of the
2519 static void gen_interrupt(DisasContext
*s
, int intno
,
2520 target_ulong cur_eip
, target_ulong next_eip
)
2522 gen_update_cc_op(s
);
2523 gen_jmp_im(s
, cur_eip
);
2524 gen_helper_raise_interrupt(cpu_env
, tcg_const_i32(intno
),
2525 tcg_const_i32(next_eip
- cur_eip
));
2526 s
->base
.is_jmp
= DISAS_NORETURN
;
2529 static void gen_debug(DisasContext
*s
, target_ulong cur_eip
)
2531 gen_update_cc_op(s
);
2532 gen_jmp_im(s
, cur_eip
);
2533 gen_helper_debug(cpu_env
);
2534 s
->base
.is_jmp
= DISAS_NORETURN
;
2537 static void gen_set_hflag(DisasContext
*s
, uint32_t mask
)
2539 if ((s
->flags
& mask
) == 0) {
2540 TCGv_i32 t
= tcg_temp_new_i32();
2541 tcg_gen_ld_i32(t
, cpu_env
, offsetof(CPUX86State
, hflags
));
2542 tcg_gen_ori_i32(t
, t
, mask
);
2543 tcg_gen_st_i32(t
, cpu_env
, offsetof(CPUX86State
, hflags
));
2544 tcg_temp_free_i32(t
);
2549 static void gen_reset_hflag(DisasContext
*s
, uint32_t mask
)
2551 if (s
->flags
& mask
) {
2552 TCGv_i32 t
= tcg_temp_new_i32();
2553 tcg_gen_ld_i32(t
, cpu_env
, offsetof(CPUX86State
, hflags
));
2554 tcg_gen_andi_i32(t
, t
, ~mask
);
2555 tcg_gen_st_i32(t
, cpu_env
, offsetof(CPUX86State
, hflags
));
2556 tcg_temp_free_i32(t
);
2561 /* Clear BND registers during legacy branches. */
2562 static void gen_bnd_jmp(DisasContext
*s
)
2564 /* Clear the registers only if BND prefix is missing, MPX is enabled,
2565 and if the BNDREGs are known to be in use (non-zero) already.
2566 The helper itself will check BNDPRESERVE at runtime. */
2567 if ((s
->prefix
& PREFIX_REPNZ
) == 0
2568 && (s
->flags
& HF_MPX_EN_MASK
) != 0
2569 && (s
->flags
& HF_MPX_IU_MASK
) != 0) {
2570 gen_helper_bnd_jmp(cpu_env
);
2574 /* Generate an end of block. Trace exception is also generated if needed.
2575 If INHIBIT, set HF_INHIBIT_IRQ_MASK if it isn't already set.
2576 If RECHECK_TF, emit a rechecking helper for #DB, ignoring the state of
2577 S->TF. This is used by the syscall/sysret insns. */
2579 do_gen_eob_worker(DisasContext
*s
, bool inhibit
, bool recheck_tf
, bool jr
)
2581 gen_update_cc_op(s
);
2583 /* If several instructions disable interrupts, only the first does it. */
2584 if (inhibit
&& !(s
->flags
& HF_INHIBIT_IRQ_MASK
)) {
2585 gen_set_hflag(s
, HF_INHIBIT_IRQ_MASK
);
2587 gen_reset_hflag(s
, HF_INHIBIT_IRQ_MASK
);
2590 if (s
->base
.tb
->flags
& HF_RF_MASK
) {
2591 gen_helper_reset_rf(cpu_env
);
2593 if (s
->base
.singlestep_enabled
) {
2594 gen_helper_debug(cpu_env
);
2595 } else if (recheck_tf
) {
2596 gen_helper_rechecking_single_step(cpu_env
);
2597 tcg_gen_exit_tb(NULL
, 0);
2599 gen_helper_single_step(cpu_env
);
2601 tcg_gen_lookup_and_goto_ptr();
2603 tcg_gen_exit_tb(NULL
, 0);
2605 s
->base
.is_jmp
= DISAS_NORETURN
;
2609 gen_eob_worker(DisasContext
*s
, bool inhibit
, bool recheck_tf
)
2611 do_gen_eob_worker(s
, inhibit
, recheck_tf
, false);
2615 If INHIBIT, set HF_INHIBIT_IRQ_MASK if it isn't already set. */
2616 static void gen_eob_inhibit_irq(DisasContext
*s
, bool inhibit
)
2618 gen_eob_worker(s
, inhibit
, false);
2621 /* End of block, resetting the inhibit irq flag. */
2622 static void gen_eob(DisasContext
*s
)
2624 gen_eob_worker(s
, false, false);
2627 /* Jump to register */
2628 static void gen_jr(DisasContext
*s
, TCGv dest
)
2630 do_gen_eob_worker(s
, false, false, true);
2633 /* generate a jump to eip. No segment change must happen before as a
2634 direct call to the next block may occur */
2635 static void gen_jmp_tb(DisasContext
*s
, target_ulong eip
, int tb_num
)
2637 gen_update_cc_op(s
);
2638 set_cc_op(s
, CC_OP_DYNAMIC
);
2640 gen_goto_tb(s
, tb_num
, eip
);
2647 static void gen_jmp(DisasContext
*s
, target_ulong eip
)
2649 gen_jmp_tb(s
, eip
, 0);
2652 static inline void gen_ldq_env_A0(DisasContext
*s
, int offset
)
2654 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
, s
->mem_index
, MO_LEQ
);
2655 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
);
2658 static inline void gen_stq_env_A0(DisasContext
*s
, int offset
)
2660 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
);
2661 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
, s
->mem_index
, MO_LEQ
);
2664 static inline void gen_ldo_env_A0(DisasContext
*s
, int offset
)
2666 int mem_index
= s
->mem_index
;
2667 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
, mem_index
, MO_LEQ
);
2668 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(ZMMReg
, ZMM_Q(0)));
2669 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 8);
2670 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEQ
);
2671 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(ZMMReg
, ZMM_Q(1)));
2674 static inline void gen_sto_env_A0(DisasContext
*s
, int offset
)
2676 int mem_index
= s
->mem_index
;
2677 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(ZMMReg
, ZMM_Q(0)));
2678 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
, mem_index
, MO_LEQ
);
2679 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 8);
2680 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(ZMMReg
, ZMM_Q(1)));
2681 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEQ
);
2684 static inline void gen_op_movo(DisasContext
*s
, int d_offset
, int s_offset
)
2686 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, s_offset
+ offsetof(ZMMReg
, ZMM_Q(0)));
2687 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, d_offset
+ offsetof(ZMMReg
, ZMM_Q(0)));
2688 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, s_offset
+ offsetof(ZMMReg
, ZMM_Q(1)));
2689 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, d_offset
+ offsetof(ZMMReg
, ZMM_Q(1)));
2692 static inline void gen_op_movq(DisasContext
*s
, int d_offset
, int s_offset
)
2694 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, s_offset
);
2695 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, d_offset
);
2698 static inline void gen_op_movl(DisasContext
*s
, int d_offset
, int s_offset
)
2700 tcg_gen_ld_i32(s
->tmp2_i32
, cpu_env
, s_offset
);
2701 tcg_gen_st_i32(s
->tmp2_i32
, cpu_env
, d_offset
);
2704 static inline void gen_op_movq_env_0(DisasContext
*s
, int d_offset
)
2706 tcg_gen_movi_i64(s
->tmp1_i64
, 0);
2707 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, d_offset
);
2710 typedef void (*SSEFunc_i_ep
)(TCGv_i32 val
, TCGv_ptr env
, TCGv_ptr reg
);
2711 typedef void (*SSEFunc_l_ep
)(TCGv_i64 val
, TCGv_ptr env
, TCGv_ptr reg
);
2712 typedef void (*SSEFunc_0_epi
)(TCGv_ptr env
, TCGv_ptr reg
, TCGv_i32 val
);
2713 typedef void (*SSEFunc_0_epl
)(TCGv_ptr env
, TCGv_ptr reg
, TCGv_i64 val
);
2714 typedef void (*SSEFunc_0_epp
)(TCGv_ptr env
, TCGv_ptr reg_a
, TCGv_ptr reg_b
);
2715 typedef void (*SSEFunc_0_eppi
)(TCGv_ptr env
, TCGv_ptr reg_a
, TCGv_ptr reg_b
,
2717 typedef void (*SSEFunc_0_ppi
)(TCGv_ptr reg_a
, TCGv_ptr reg_b
, TCGv_i32 val
);
2718 typedef void (*SSEFunc_0_eppt
)(TCGv_ptr env
, TCGv_ptr reg_a
, TCGv_ptr reg_b
,
2721 #define SSE_SPECIAL ((void *)1)
2722 #define SSE_DUMMY ((void *)2)
2724 #define MMX_OP2(x) { gen_helper_ ## x ## _mmx, gen_helper_ ## x ## _xmm }
2725 #define SSE_FOP(x) { gen_helper_ ## x ## ps, gen_helper_ ## x ## pd, \
2726 gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, }
2728 static const SSEFunc_0_epp sse_op_table1
[256][4] = {
2729 /* 3DNow! extensions */
2730 [0x0e] = { SSE_DUMMY
}, /* femms */
2731 [0x0f] = { SSE_DUMMY
}, /* pf... */
2732 /* pure SSE operations */
2733 [0x10] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movups, movupd, movss, movsd */
2734 [0x11] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movups, movupd, movss, movsd */
2735 [0x12] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movlps, movlpd, movsldup, movddup */
2736 [0x13] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movlps, movlpd */
2737 [0x14] = { gen_helper_punpckldq_xmm
, gen_helper_punpcklqdq_xmm
},
2738 [0x15] = { gen_helper_punpckhdq_xmm
, gen_helper_punpckhqdq_xmm
},
2739 [0x16] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movhps, movhpd, movshdup */
2740 [0x17] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movhps, movhpd */
2742 [0x28] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movaps, movapd */
2743 [0x29] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movaps, movapd */
2744 [0x2a] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */
2745 [0x2b] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movntps, movntpd, movntss, movntsd */
2746 [0x2c] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */
2747 [0x2d] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */
2748 [0x2e] = { gen_helper_ucomiss
, gen_helper_ucomisd
},
2749 [0x2f] = { gen_helper_comiss
, gen_helper_comisd
},
2750 [0x50] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movmskps, movmskpd */
2751 [0x51] = SSE_FOP(sqrt
),
2752 [0x52] = { gen_helper_rsqrtps
, NULL
, gen_helper_rsqrtss
, NULL
},
2753 [0x53] = { gen_helper_rcpps
, NULL
, gen_helper_rcpss
, NULL
},
2754 [0x54] = { gen_helper_pand_xmm
, gen_helper_pand_xmm
}, /* andps, andpd */
2755 [0x55] = { gen_helper_pandn_xmm
, gen_helper_pandn_xmm
}, /* andnps, andnpd */
2756 [0x56] = { gen_helper_por_xmm
, gen_helper_por_xmm
}, /* orps, orpd */
2757 [0x57] = { gen_helper_pxor_xmm
, gen_helper_pxor_xmm
}, /* xorps, xorpd */
2758 [0x58] = SSE_FOP(add
),
2759 [0x59] = SSE_FOP(mul
),
2760 [0x5a] = { gen_helper_cvtps2pd
, gen_helper_cvtpd2ps
,
2761 gen_helper_cvtss2sd
, gen_helper_cvtsd2ss
},
2762 [0x5b] = { gen_helper_cvtdq2ps
, gen_helper_cvtps2dq
, gen_helper_cvttps2dq
},
2763 [0x5c] = SSE_FOP(sub
),
2764 [0x5d] = SSE_FOP(min
),
2765 [0x5e] = SSE_FOP(div
),
2766 [0x5f] = SSE_FOP(max
),
2768 [0xc2] = SSE_FOP(cmpeq
),
2769 [0xc6] = { (SSEFunc_0_epp
)gen_helper_shufps
,
2770 (SSEFunc_0_epp
)gen_helper_shufpd
}, /* XXX: casts */
2772 /* SSSE3, SSE4, MOVBE, CRC32, BMI1, BMI2, ADX. */
2773 [0x38] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
},
2774 [0x3a] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
},
2776 /* MMX ops and their SSE extensions */
2777 [0x60] = MMX_OP2(punpcklbw
),
2778 [0x61] = MMX_OP2(punpcklwd
),
2779 [0x62] = MMX_OP2(punpckldq
),
2780 [0x63] = MMX_OP2(packsswb
),
2781 [0x64] = MMX_OP2(pcmpgtb
),
2782 [0x65] = MMX_OP2(pcmpgtw
),
2783 [0x66] = MMX_OP2(pcmpgtl
),
2784 [0x67] = MMX_OP2(packuswb
),
2785 [0x68] = MMX_OP2(punpckhbw
),
2786 [0x69] = MMX_OP2(punpckhwd
),
2787 [0x6a] = MMX_OP2(punpckhdq
),
2788 [0x6b] = MMX_OP2(packssdw
),
2789 [0x6c] = { NULL
, gen_helper_punpcklqdq_xmm
},
2790 [0x6d] = { NULL
, gen_helper_punpckhqdq_xmm
},
2791 [0x6e] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movd mm, ea */
2792 [0x6f] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movq, movdqa, , movqdu */
2793 [0x70] = { (SSEFunc_0_epp
)gen_helper_pshufw_mmx
,
2794 (SSEFunc_0_epp
)gen_helper_pshufd_xmm
,
2795 (SSEFunc_0_epp
)gen_helper_pshufhw_xmm
,
2796 (SSEFunc_0_epp
)gen_helper_pshuflw_xmm
}, /* XXX: casts */
2797 [0x71] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* shiftw */
2798 [0x72] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* shiftd */
2799 [0x73] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* shiftq */
2800 [0x74] = MMX_OP2(pcmpeqb
),
2801 [0x75] = MMX_OP2(pcmpeqw
),
2802 [0x76] = MMX_OP2(pcmpeql
),
2803 [0x77] = { SSE_DUMMY
}, /* emms */
2804 [0x78] = { NULL
, SSE_SPECIAL
, NULL
, SSE_SPECIAL
}, /* extrq_i, insertq_i */
2805 [0x79] = { NULL
, gen_helper_extrq_r
, NULL
, gen_helper_insertq_r
},
2806 [0x7c] = { NULL
, gen_helper_haddpd
, NULL
, gen_helper_haddps
},
2807 [0x7d] = { NULL
, gen_helper_hsubpd
, NULL
, gen_helper_hsubps
},
2808 [0x7e] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movd, movd, , movq */
2809 [0x7f] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movq, movdqa, movdqu */
2810 [0xc4] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* pinsrw */
2811 [0xc5] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* pextrw */
2812 [0xd0] = { NULL
, gen_helper_addsubpd
, NULL
, gen_helper_addsubps
},
2813 [0xd1] = MMX_OP2(psrlw
),
2814 [0xd2] = MMX_OP2(psrld
),
2815 [0xd3] = MMX_OP2(psrlq
),
2816 [0xd4] = MMX_OP2(paddq
),
2817 [0xd5] = MMX_OP2(pmullw
),
2818 [0xd6] = { NULL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
},
2819 [0xd7] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* pmovmskb */
2820 [0xd8] = MMX_OP2(psubusb
),
2821 [0xd9] = MMX_OP2(psubusw
),
2822 [0xda] = MMX_OP2(pminub
),
2823 [0xdb] = MMX_OP2(pand
),
2824 [0xdc] = MMX_OP2(paddusb
),
2825 [0xdd] = MMX_OP2(paddusw
),
2826 [0xde] = MMX_OP2(pmaxub
),
2827 [0xdf] = MMX_OP2(pandn
),
2828 [0xe0] = MMX_OP2(pavgb
),
2829 [0xe1] = MMX_OP2(psraw
),
2830 [0xe2] = MMX_OP2(psrad
),
2831 [0xe3] = MMX_OP2(pavgw
),
2832 [0xe4] = MMX_OP2(pmulhuw
),
2833 [0xe5] = MMX_OP2(pmulhw
),
2834 [0xe6] = { NULL
, gen_helper_cvttpd2dq
, gen_helper_cvtdq2pd
, gen_helper_cvtpd2dq
},
2835 [0xe7] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movntq, movntq */
2836 [0xe8] = MMX_OP2(psubsb
),
2837 [0xe9] = MMX_OP2(psubsw
),
2838 [0xea] = MMX_OP2(pminsw
),
2839 [0xeb] = MMX_OP2(por
),
2840 [0xec] = MMX_OP2(paddsb
),
2841 [0xed] = MMX_OP2(paddsw
),
2842 [0xee] = MMX_OP2(pmaxsw
),
2843 [0xef] = MMX_OP2(pxor
),
2844 [0xf0] = { NULL
, NULL
, NULL
, SSE_SPECIAL
}, /* lddqu */
2845 [0xf1] = MMX_OP2(psllw
),
2846 [0xf2] = MMX_OP2(pslld
),
2847 [0xf3] = MMX_OP2(psllq
),
2848 [0xf4] = MMX_OP2(pmuludq
),
2849 [0xf5] = MMX_OP2(pmaddwd
),
2850 [0xf6] = MMX_OP2(psadbw
),
2851 [0xf7] = { (SSEFunc_0_epp
)gen_helper_maskmov_mmx
,
2852 (SSEFunc_0_epp
)gen_helper_maskmov_xmm
}, /* XXX: casts */
2853 [0xf8] = MMX_OP2(psubb
),
2854 [0xf9] = MMX_OP2(psubw
),
2855 [0xfa] = MMX_OP2(psubl
),
2856 [0xfb] = MMX_OP2(psubq
),
2857 [0xfc] = MMX_OP2(paddb
),
2858 [0xfd] = MMX_OP2(paddw
),
2859 [0xfe] = MMX_OP2(paddl
),
2862 static const SSEFunc_0_epp sse_op_table2
[3 * 8][2] = {
2863 [0 + 2] = MMX_OP2(psrlw
),
2864 [0 + 4] = MMX_OP2(psraw
),
2865 [0 + 6] = MMX_OP2(psllw
),
2866 [8 + 2] = MMX_OP2(psrld
),
2867 [8 + 4] = MMX_OP2(psrad
),
2868 [8 + 6] = MMX_OP2(pslld
),
2869 [16 + 2] = MMX_OP2(psrlq
),
2870 [16 + 3] = { NULL
, gen_helper_psrldq_xmm
},
2871 [16 + 6] = MMX_OP2(psllq
),
2872 [16 + 7] = { NULL
, gen_helper_pslldq_xmm
},
2875 static const SSEFunc_0_epi sse_op_table3ai
[] = {
2876 gen_helper_cvtsi2ss
,
2880 #ifdef TARGET_X86_64
2881 static const SSEFunc_0_epl sse_op_table3aq
[] = {
2882 gen_helper_cvtsq2ss
,
2887 static const SSEFunc_i_ep sse_op_table3bi
[] = {
2888 gen_helper_cvttss2si
,
2889 gen_helper_cvtss2si
,
2890 gen_helper_cvttsd2si
,
2894 #ifdef TARGET_X86_64
2895 static const SSEFunc_l_ep sse_op_table3bq
[] = {
2896 gen_helper_cvttss2sq
,
2897 gen_helper_cvtss2sq
,
2898 gen_helper_cvttsd2sq
,
2903 static const SSEFunc_0_epp sse_op_table4
[8][4] = {
2914 static const SSEFunc_0_epp sse_op_table5
[256] = {
2915 [0x0c] = gen_helper_pi2fw
,
2916 [0x0d] = gen_helper_pi2fd
,
2917 [0x1c] = gen_helper_pf2iw
,
2918 [0x1d] = gen_helper_pf2id
,
2919 [0x8a] = gen_helper_pfnacc
,
2920 [0x8e] = gen_helper_pfpnacc
,
2921 [0x90] = gen_helper_pfcmpge
,
2922 [0x94] = gen_helper_pfmin
,
2923 [0x96] = gen_helper_pfrcp
,
2924 [0x97] = gen_helper_pfrsqrt
,
2925 [0x9a] = gen_helper_pfsub
,
2926 [0x9e] = gen_helper_pfadd
,
2927 [0xa0] = gen_helper_pfcmpgt
,
2928 [0xa4] = gen_helper_pfmax
,
2929 [0xa6] = gen_helper_movq
, /* pfrcpit1; no need to actually increase precision */
2930 [0xa7] = gen_helper_movq
, /* pfrsqit1 */
2931 [0xaa] = gen_helper_pfsubr
,
2932 [0xae] = gen_helper_pfacc
,
2933 [0xb0] = gen_helper_pfcmpeq
,
2934 [0xb4] = gen_helper_pfmul
,
2935 [0xb6] = gen_helper_movq
, /* pfrcpit2 */
2936 [0xb7] = gen_helper_pmulhrw_mmx
,
2937 [0xbb] = gen_helper_pswapd
,
2938 [0xbf] = gen_helper_pavgb_mmx
/* pavgusb */
2941 struct SSEOpHelper_epp
{
2942 SSEFunc_0_epp op
[2];
2946 struct SSEOpHelper_eppi
{
2947 SSEFunc_0_eppi op
[2];
2951 #define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
2952 #define SSE41_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }
2953 #define SSE42_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }
2954 #define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2955 #define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
2956 CPUID_EXT_PCLMULQDQ }
2957 #define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
2959 static const struct SSEOpHelper_epp sse_op_table6
[256] = {
2960 [0x00] = SSSE3_OP(pshufb
),
2961 [0x01] = SSSE3_OP(phaddw
),
2962 [0x02] = SSSE3_OP(phaddd
),
2963 [0x03] = SSSE3_OP(phaddsw
),
2964 [0x04] = SSSE3_OP(pmaddubsw
),
2965 [0x05] = SSSE3_OP(phsubw
),
2966 [0x06] = SSSE3_OP(phsubd
),
2967 [0x07] = SSSE3_OP(phsubsw
),
2968 [0x08] = SSSE3_OP(psignb
),
2969 [0x09] = SSSE3_OP(psignw
),
2970 [0x0a] = SSSE3_OP(psignd
),
2971 [0x0b] = SSSE3_OP(pmulhrsw
),
2972 [0x10] = SSE41_OP(pblendvb
),
2973 [0x14] = SSE41_OP(blendvps
),
2974 [0x15] = SSE41_OP(blendvpd
),
2975 [0x17] = SSE41_OP(ptest
),
2976 [0x1c] = SSSE3_OP(pabsb
),
2977 [0x1d] = SSSE3_OP(pabsw
),
2978 [0x1e] = SSSE3_OP(pabsd
),
2979 [0x20] = SSE41_OP(pmovsxbw
),
2980 [0x21] = SSE41_OP(pmovsxbd
),
2981 [0x22] = SSE41_OP(pmovsxbq
),
2982 [0x23] = SSE41_OP(pmovsxwd
),
2983 [0x24] = SSE41_OP(pmovsxwq
),
2984 [0x25] = SSE41_OP(pmovsxdq
),
2985 [0x28] = SSE41_OP(pmuldq
),
2986 [0x29] = SSE41_OP(pcmpeqq
),
2987 [0x2a] = SSE41_SPECIAL
, /* movntqda */
2988 [0x2b] = SSE41_OP(packusdw
),
2989 [0x30] = SSE41_OP(pmovzxbw
),
2990 [0x31] = SSE41_OP(pmovzxbd
),
2991 [0x32] = SSE41_OP(pmovzxbq
),
2992 [0x33] = SSE41_OP(pmovzxwd
),
2993 [0x34] = SSE41_OP(pmovzxwq
),
2994 [0x35] = SSE41_OP(pmovzxdq
),
2995 [0x37] = SSE42_OP(pcmpgtq
),
2996 [0x38] = SSE41_OP(pminsb
),
2997 [0x39] = SSE41_OP(pminsd
),
2998 [0x3a] = SSE41_OP(pminuw
),
2999 [0x3b] = SSE41_OP(pminud
),
3000 [0x3c] = SSE41_OP(pmaxsb
),
3001 [0x3d] = SSE41_OP(pmaxsd
),
3002 [0x3e] = SSE41_OP(pmaxuw
),
3003 [0x3f] = SSE41_OP(pmaxud
),
3004 [0x40] = SSE41_OP(pmulld
),
3005 [0x41] = SSE41_OP(phminposuw
),
3006 [0xdb] = AESNI_OP(aesimc
),
3007 [0xdc] = AESNI_OP(aesenc
),
3008 [0xdd] = AESNI_OP(aesenclast
),
3009 [0xde] = AESNI_OP(aesdec
),
3010 [0xdf] = AESNI_OP(aesdeclast
),
3013 static const struct SSEOpHelper_eppi sse_op_table7
[256] = {
3014 [0x08] = SSE41_OP(roundps
),
3015 [0x09] = SSE41_OP(roundpd
),
3016 [0x0a] = SSE41_OP(roundss
),
3017 [0x0b] = SSE41_OP(roundsd
),
3018 [0x0c] = SSE41_OP(blendps
),
3019 [0x0d] = SSE41_OP(blendpd
),
3020 [0x0e] = SSE41_OP(pblendw
),
3021 [0x0f] = SSSE3_OP(palignr
),
3022 [0x14] = SSE41_SPECIAL
, /* pextrb */
3023 [0x15] = SSE41_SPECIAL
, /* pextrw */
3024 [0x16] = SSE41_SPECIAL
, /* pextrd/pextrq */
3025 [0x17] = SSE41_SPECIAL
, /* extractps */
3026 [0x20] = SSE41_SPECIAL
, /* pinsrb */
3027 [0x21] = SSE41_SPECIAL
, /* insertps */
3028 [0x22] = SSE41_SPECIAL
, /* pinsrd/pinsrq */
3029 [0x40] = SSE41_OP(dpps
),
3030 [0x41] = SSE41_OP(dppd
),
3031 [0x42] = SSE41_OP(mpsadbw
),
3032 [0x44] = PCLMULQDQ_OP(pclmulqdq
),
3033 [0x60] = SSE42_OP(pcmpestrm
),
3034 [0x61] = SSE42_OP(pcmpestri
),
3035 [0x62] = SSE42_OP(pcmpistrm
),
3036 [0x63] = SSE42_OP(pcmpistri
),
3037 [0xdf] = AESNI_OP(aeskeygenassist
),
3040 static void gen_sse(CPUX86State
*env
, DisasContext
*s
, int b
,
3041 target_ulong pc_start
, int rex_r
)
3043 int b1
, op1_offset
, op2_offset
, is_xmm
, val
;
3044 int modrm
, mod
, rm
, reg
;
3045 SSEFunc_0_epp sse_fn_epp
;
3046 SSEFunc_0_eppi sse_fn_eppi
;
3047 SSEFunc_0_ppi sse_fn_ppi
;
3048 SSEFunc_0_eppt sse_fn_eppt
;
3052 if (s
->prefix
& PREFIX_DATA
)
3054 else if (s
->prefix
& PREFIX_REPZ
)
3056 else if (s
->prefix
& PREFIX_REPNZ
)
3060 sse_fn_epp
= sse_op_table1
[b
][b1
];
3064 if ((b
<= 0x5f && b
>= 0x10) || b
== 0xc6 || b
== 0xc2) {
3074 /* simple MMX/SSE operation */
3075 if (s
->flags
& HF_TS_MASK
) {
3076 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
3079 if (s
->flags
& HF_EM_MASK
) {
3081 gen_illegal_opcode(s
);
3085 && !(s
->flags
& HF_OSFXSR_MASK
)
3086 && ((b
!= 0x38 && b
!= 0x3a) || (s
->prefix
& PREFIX_DATA
))) {
3090 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_3DNOW
)) {
3091 /* If we were fully decoding this we might use illegal_op. */
3095 gen_helper_emms(cpu_env
);
3100 gen_helper_emms(cpu_env
);
3103 /* prepare MMX state (XXX: optimize by storing fptt and fptags in
3104 the static cpu state) */
3106 gen_helper_enter_mmx(cpu_env
);
3109 modrm
= x86_ldub_code(env
, s
);
3110 reg
= ((modrm
>> 3) & 7);
3113 mod
= (modrm
>> 6) & 3;
3114 if (sse_fn_epp
== SSE_SPECIAL
) {
3117 case 0x0e7: /* movntq */
3121 gen_lea_modrm(env
, s
, modrm
);
3122 gen_stq_env_A0(s
, offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3124 case 0x1e7: /* movntdq */
3125 case 0x02b: /* movntps */
3126 case 0x12b: /* movntps */
3129 gen_lea_modrm(env
, s
, modrm
);
3130 gen_sto_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3132 case 0x3f0: /* lddqu */
3135 gen_lea_modrm(env
, s
, modrm
);
3136 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3138 case 0x22b: /* movntss */
3139 case 0x32b: /* movntsd */
3142 gen_lea_modrm(env
, s
, modrm
);
3144 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3145 xmm_regs
[reg
].ZMM_Q(0)));
3147 tcg_gen_ld32u_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
,
3148 xmm_regs
[reg
].ZMM_L(0)));
3149 gen_op_st_v(s
, MO_32
, s
->T0
, s
->A0
);
3152 case 0x6e: /* movd mm, ea */
3153 #ifdef TARGET_X86_64
3154 if (s
->dflag
== MO_64
) {
3155 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 0);
3156 tcg_gen_st_tl(s
->T0
, cpu_env
,
3157 offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3161 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 0);
3162 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
,
3163 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3164 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3165 gen_helper_movl_mm_T0_mmx(s
->ptr0
, s
->tmp2_i32
);
3168 case 0x16e: /* movd xmm, ea */
3169 #ifdef TARGET_X86_64
3170 if (s
->dflag
== MO_64
) {
3171 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 0);
3172 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
,
3173 offsetof(CPUX86State
,xmm_regs
[reg
]));
3174 gen_helper_movq_mm_T0_xmm(s
->ptr0
, s
->T0
);
3178 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 0);
3179 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
,
3180 offsetof(CPUX86State
,xmm_regs
[reg
]));
3181 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3182 gen_helper_movl_mm_T0_xmm(s
->ptr0
, s
->tmp2_i32
);
3185 case 0x6f: /* movq mm, ea */
3187 gen_lea_modrm(env
, s
, modrm
);
3188 gen_ldq_env_A0(s
, offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3191 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
,
3192 offsetof(CPUX86State
,fpregs
[rm
].mmx
));
3193 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
,
3194 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3197 case 0x010: /* movups */
3198 case 0x110: /* movupd */
3199 case 0x028: /* movaps */
3200 case 0x128: /* movapd */
3201 case 0x16f: /* movdqa xmm, ea */
3202 case 0x26f: /* movdqu xmm, ea */
3204 gen_lea_modrm(env
, s
, modrm
);
3205 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3207 rm
= (modrm
& 7) | REX_B(s
);
3208 gen_op_movo(s
, offsetof(CPUX86State
, xmm_regs
[reg
]),
3209 offsetof(CPUX86State
,xmm_regs
[rm
]));
3212 case 0x210: /* movss xmm, ea */
3214 gen_lea_modrm(env
, s
, modrm
);
3215 gen_op_ld_v(s
, MO_32
, s
->T0
, s
->A0
);
3216 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3217 offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(0)));
3218 tcg_gen_movi_tl(s
->T0
, 0);
3219 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3220 offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(1)));
3221 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3222 offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(2)));
3223 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3224 offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(3)));
3226 rm
= (modrm
& 7) | REX_B(s
);
3227 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(0)),
3228 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_L(0)));
3231 case 0x310: /* movsd xmm, ea */
3233 gen_lea_modrm(env
, s
, modrm
);
3234 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3235 xmm_regs
[reg
].ZMM_Q(0)));
3236 tcg_gen_movi_tl(s
->T0
, 0);
3237 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3238 offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(2)));
3239 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3240 offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(3)));
3242 rm
= (modrm
& 7) | REX_B(s
);
3243 gen_op_movq(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_Q(0)),
3244 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_Q(0)));
3247 case 0x012: /* movlps */
3248 case 0x112: /* movlpd */
3250 gen_lea_modrm(env
, s
, modrm
);
3251 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3252 xmm_regs
[reg
].ZMM_Q(0)));
3255 rm
= (modrm
& 7) | REX_B(s
);
3256 gen_op_movq(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_Q(0)),
3257 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_Q(1)));
3260 case 0x212: /* movsldup */
3262 gen_lea_modrm(env
, s
, modrm
);
3263 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3265 rm
= (modrm
& 7) | REX_B(s
);
3266 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(0)),
3267 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_L(0)));
3268 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(2)),
3269 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_L(2)));
3271 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(1)),
3272 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_L(0)));
3273 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(3)),
3274 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_L(2)));
3276 case 0x312: /* movddup */
3278 gen_lea_modrm(env
, s
, modrm
);
3279 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3280 xmm_regs
[reg
].ZMM_Q(0)));
3282 rm
= (modrm
& 7) | REX_B(s
);
3283 gen_op_movq(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_Q(0)),
3284 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_Q(0)));
3286 gen_op_movq(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_Q(1)),
3287 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_Q(0)));
3289 case 0x016: /* movhps */
3290 case 0x116: /* movhpd */
3292 gen_lea_modrm(env
, s
, modrm
);
3293 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3294 xmm_regs
[reg
].ZMM_Q(1)));
3297 rm
= (modrm
& 7) | REX_B(s
);
3298 gen_op_movq(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_Q(1)),
3299 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_Q(0)));
3302 case 0x216: /* movshdup */
3304 gen_lea_modrm(env
, s
, modrm
);
3305 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3307 rm
= (modrm
& 7) | REX_B(s
);
3308 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(1)),
3309 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_L(1)));
3310 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(3)),
3311 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_L(3)));
3313 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(0)),
3314 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_L(1)));
3315 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(2)),
3316 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_L(3)));
3321 int bit_index
, field_length
;
3323 if (b1
== 1 && reg
!= 0)
3325 field_length
= x86_ldub_code(env
, s
) & 0x3F;
3326 bit_index
= x86_ldub_code(env
, s
) & 0x3F;
3327 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
,
3328 offsetof(CPUX86State
,xmm_regs
[reg
]));
3330 gen_helper_extrq_i(cpu_env
, s
->ptr0
,
3331 tcg_const_i32(bit_index
),
3332 tcg_const_i32(field_length
));
3334 gen_helper_insertq_i(cpu_env
, s
->ptr0
,
3335 tcg_const_i32(bit_index
),
3336 tcg_const_i32(field_length
));
3339 case 0x7e: /* movd ea, mm */
3340 #ifdef TARGET_X86_64
3341 if (s
->dflag
== MO_64
) {
3342 tcg_gen_ld_i64(s
->T0
, cpu_env
,
3343 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3344 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 1);
3348 tcg_gen_ld32u_tl(s
->T0
, cpu_env
,
3349 offsetof(CPUX86State
,fpregs
[reg
].mmx
.MMX_L(0)));
3350 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 1);
3353 case 0x17e: /* movd ea, xmm */
3354 #ifdef TARGET_X86_64
3355 if (s
->dflag
== MO_64
) {
3356 tcg_gen_ld_i64(s
->T0
, cpu_env
,
3357 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_Q(0)));
3358 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 1);
3362 tcg_gen_ld32u_tl(s
->T0
, cpu_env
,
3363 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_L(0)));
3364 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 1);
3367 case 0x27e: /* movq xmm, ea */
3369 gen_lea_modrm(env
, s
, modrm
);
3370 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3371 xmm_regs
[reg
].ZMM_Q(0)));
3373 rm
= (modrm
& 7) | REX_B(s
);
3374 gen_op_movq(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_Q(0)),
3375 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_Q(0)));
3377 gen_op_movq_env_0(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_Q(1)));
3379 case 0x7f: /* movq ea, mm */
3381 gen_lea_modrm(env
, s
, modrm
);
3382 gen_stq_env_A0(s
, offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3385 gen_op_movq(s
, offsetof(CPUX86State
, fpregs
[rm
].mmx
),
3386 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3389 case 0x011: /* movups */
3390 case 0x111: /* movupd */
3391 case 0x029: /* movaps */
3392 case 0x129: /* movapd */
3393 case 0x17f: /* movdqa ea, xmm */
3394 case 0x27f: /* movdqu ea, xmm */
3396 gen_lea_modrm(env
, s
, modrm
);
3397 gen_sto_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3399 rm
= (modrm
& 7) | REX_B(s
);
3400 gen_op_movo(s
, offsetof(CPUX86State
, xmm_regs
[rm
]),
3401 offsetof(CPUX86State
,xmm_regs
[reg
]));
3404 case 0x211: /* movss ea, xmm */
3406 gen_lea_modrm(env
, s
, modrm
);
3407 tcg_gen_ld32u_tl(s
->T0
, cpu_env
,
3408 offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_L(0)));
3409 gen_op_st_v(s
, MO_32
, s
->T0
, s
->A0
);
3411 rm
= (modrm
& 7) | REX_B(s
);
3412 gen_op_movl(s
, offsetof(CPUX86State
, xmm_regs
[rm
].ZMM_L(0)),
3413 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_L(0)));
3416 case 0x311: /* movsd ea, xmm */
3418 gen_lea_modrm(env
, s
, modrm
);
3419 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3420 xmm_regs
[reg
].ZMM_Q(0)));
3422 rm
= (modrm
& 7) | REX_B(s
);
3423 gen_op_movq(s
, offsetof(CPUX86State
, xmm_regs
[rm
].ZMM_Q(0)),
3424 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_Q(0)));
3427 case 0x013: /* movlps */
3428 case 0x113: /* movlpd */
3430 gen_lea_modrm(env
, s
, modrm
);
3431 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3432 xmm_regs
[reg
].ZMM_Q(0)));
3437 case 0x017: /* movhps */
3438 case 0x117: /* movhpd */
3440 gen_lea_modrm(env
, s
, modrm
);
3441 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3442 xmm_regs
[reg
].ZMM_Q(1)));
3447 case 0x71: /* shift mm, im */
3450 case 0x171: /* shift xmm, im */
3456 val
= x86_ldub_code(env
, s
);
3458 tcg_gen_movi_tl(s
->T0
, val
);
3459 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3460 offsetof(CPUX86State
, xmm_t0
.ZMM_L(0)));
3461 tcg_gen_movi_tl(s
->T0
, 0);
3462 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3463 offsetof(CPUX86State
, xmm_t0
.ZMM_L(1)));
3464 op1_offset
= offsetof(CPUX86State
,xmm_t0
);
3466 tcg_gen_movi_tl(s
->T0
, val
);
3467 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3468 offsetof(CPUX86State
, mmx_t0
.MMX_L(0)));
3469 tcg_gen_movi_tl(s
->T0
, 0);
3470 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3471 offsetof(CPUX86State
, mmx_t0
.MMX_L(1)));
3472 op1_offset
= offsetof(CPUX86State
,mmx_t0
);
3474 sse_fn_epp
= sse_op_table2
[((b
- 1) & 3) * 8 +
3475 (((modrm
>> 3)) & 7)][b1
];
3480 rm
= (modrm
& 7) | REX_B(s
);
3481 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
3484 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
3486 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op2_offset
);
3487 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op1_offset
);
3488 sse_fn_epp(cpu_env
, s
->ptr0
, s
->ptr1
);
3490 case 0x050: /* movmskps */
3491 rm
= (modrm
& 7) | REX_B(s
);
3492 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
,
3493 offsetof(CPUX86State
,xmm_regs
[rm
]));
3494 gen_helper_movmskps(s
->tmp2_i32
, cpu_env
, s
->ptr0
);
3495 tcg_gen_extu_i32_tl(cpu_regs
[reg
], s
->tmp2_i32
);
3497 case 0x150: /* movmskpd */
3498 rm
= (modrm
& 7) | REX_B(s
);
3499 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
,
3500 offsetof(CPUX86State
,xmm_regs
[rm
]));
3501 gen_helper_movmskpd(s
->tmp2_i32
, cpu_env
, s
->ptr0
);
3502 tcg_gen_extu_i32_tl(cpu_regs
[reg
], s
->tmp2_i32
);
3504 case 0x02a: /* cvtpi2ps */
3505 case 0x12a: /* cvtpi2pd */
3506 gen_helper_enter_mmx(cpu_env
);
3508 gen_lea_modrm(env
, s
, modrm
);
3509 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
3510 gen_ldq_env_A0(s
, op2_offset
);
3513 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
3515 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
3516 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
3517 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op2_offset
);
3520 gen_helper_cvtpi2ps(cpu_env
, s
->ptr0
, s
->ptr1
);
3524 gen_helper_cvtpi2pd(cpu_env
, s
->ptr0
, s
->ptr1
);
3528 case 0x22a: /* cvtsi2ss */
3529 case 0x32a: /* cvtsi2sd */
3530 ot
= mo_64_32(s
->dflag
);
3531 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3532 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
3533 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
3535 SSEFunc_0_epi sse_fn_epi
= sse_op_table3ai
[(b
>> 8) & 1];
3536 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3537 sse_fn_epi(cpu_env
, s
->ptr0
, s
->tmp2_i32
);
3539 #ifdef TARGET_X86_64
3540 SSEFunc_0_epl sse_fn_epl
= sse_op_table3aq
[(b
>> 8) & 1];
3541 sse_fn_epl(cpu_env
, s
->ptr0
, s
->T0
);
3547 case 0x02c: /* cvttps2pi */
3548 case 0x12c: /* cvttpd2pi */
3549 case 0x02d: /* cvtps2pi */
3550 case 0x12d: /* cvtpd2pi */
3551 gen_helper_enter_mmx(cpu_env
);
3553 gen_lea_modrm(env
, s
, modrm
);
3554 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
3555 gen_ldo_env_A0(s
, op2_offset
);
3557 rm
= (modrm
& 7) | REX_B(s
);
3558 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
3560 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
& 7].mmx
);
3561 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
3562 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op2_offset
);
3565 gen_helper_cvttps2pi(cpu_env
, s
->ptr0
, s
->ptr1
);
3568 gen_helper_cvttpd2pi(cpu_env
, s
->ptr0
, s
->ptr1
);
3571 gen_helper_cvtps2pi(cpu_env
, s
->ptr0
, s
->ptr1
);
3574 gen_helper_cvtpd2pi(cpu_env
, s
->ptr0
, s
->ptr1
);
3578 case 0x22c: /* cvttss2si */
3579 case 0x32c: /* cvttsd2si */
3580 case 0x22d: /* cvtss2si */
3581 case 0x32d: /* cvtsd2si */
3582 ot
= mo_64_32(s
->dflag
);
3584 gen_lea_modrm(env
, s
, modrm
);
3586 gen_ldq_env_A0(s
, offsetof(CPUX86State
, xmm_t0
.ZMM_Q(0)));
3588 gen_op_ld_v(s
, MO_32
, s
->T0
, s
->A0
);
3589 tcg_gen_st32_tl(s
->T0
, cpu_env
,
3590 offsetof(CPUX86State
, xmm_t0
.ZMM_L(0)));
3592 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
3594 rm
= (modrm
& 7) | REX_B(s
);
3595 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
3597 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op2_offset
);
3599 SSEFunc_i_ep sse_fn_i_ep
=
3600 sse_op_table3bi
[((b
>> 7) & 2) | (b
& 1)];
3601 sse_fn_i_ep(s
->tmp2_i32
, cpu_env
, s
->ptr0
);
3602 tcg_gen_extu_i32_tl(s
->T0
, s
->tmp2_i32
);
3604 #ifdef TARGET_X86_64
3605 SSEFunc_l_ep sse_fn_l_ep
=
3606 sse_op_table3bq
[((b
>> 7) & 2) | (b
& 1)];
3607 sse_fn_l_ep(s
->T0
, cpu_env
, s
->ptr0
);
3612 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
3614 case 0xc4: /* pinsrw */
3617 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
3618 val
= x86_ldub_code(env
, s
);
3621 tcg_gen_st16_tl(s
->T0
, cpu_env
,
3622 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_W(val
)));
3625 tcg_gen_st16_tl(s
->T0
, cpu_env
,
3626 offsetof(CPUX86State
,fpregs
[reg
].mmx
.MMX_W(val
)));
3629 case 0xc5: /* pextrw */
3633 ot
= mo_64_32(s
->dflag
);
3634 val
= x86_ldub_code(env
, s
);
3637 rm
= (modrm
& 7) | REX_B(s
);
3638 tcg_gen_ld16u_tl(s
->T0
, cpu_env
,
3639 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_W(val
)));
3643 tcg_gen_ld16u_tl(s
->T0
, cpu_env
,
3644 offsetof(CPUX86State
,fpregs
[rm
].mmx
.MMX_W(val
)));
3646 reg
= ((modrm
>> 3) & 7) | rex_r
;
3647 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
3649 case 0x1d6: /* movq ea, xmm */
3651 gen_lea_modrm(env
, s
, modrm
);
3652 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3653 xmm_regs
[reg
].ZMM_Q(0)));
3655 rm
= (modrm
& 7) | REX_B(s
);
3656 gen_op_movq(s
, offsetof(CPUX86State
, xmm_regs
[rm
].ZMM_Q(0)),
3657 offsetof(CPUX86State
,xmm_regs
[reg
].ZMM_Q(0)));
3658 gen_op_movq_env_0(s
,
3659 offsetof(CPUX86State
, xmm_regs
[rm
].ZMM_Q(1)));
3662 case 0x2d6: /* movq2dq */
3663 gen_helper_enter_mmx(cpu_env
);
3665 gen_op_movq(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_Q(0)),
3666 offsetof(CPUX86State
,fpregs
[rm
].mmx
));
3667 gen_op_movq_env_0(s
, offsetof(CPUX86State
, xmm_regs
[reg
].ZMM_Q(1)));
3669 case 0x3d6: /* movdq2q */
3670 gen_helper_enter_mmx(cpu_env
);
3671 rm
= (modrm
& 7) | REX_B(s
);
3672 gen_op_movq(s
, offsetof(CPUX86State
, fpregs
[reg
& 7].mmx
),
3673 offsetof(CPUX86State
,xmm_regs
[rm
].ZMM_Q(0)));
3675 case 0xd7: /* pmovmskb */
3680 rm
= (modrm
& 7) | REX_B(s
);
3681 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
,
3682 offsetof(CPUX86State
, xmm_regs
[rm
]));
3683 gen_helper_pmovmskb_xmm(s
->tmp2_i32
, cpu_env
, s
->ptr0
);
3686 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
,
3687 offsetof(CPUX86State
, fpregs
[rm
].mmx
));
3688 gen_helper_pmovmskb_mmx(s
->tmp2_i32
, cpu_env
, s
->ptr0
);
3690 reg
= ((modrm
>> 3) & 7) | rex_r
;
3691 tcg_gen_extu_i32_tl(cpu_regs
[reg
], s
->tmp2_i32
);
3697 if ((b
& 0xf0) == 0xf0) {
3700 modrm
= x86_ldub_code(env
, s
);
3702 reg
= ((modrm
>> 3) & 7) | rex_r
;
3703 mod
= (modrm
>> 6) & 3;
3708 sse_fn_epp
= sse_op_table6
[b
].op
[b1
];
3712 if (!(s
->cpuid_ext_features
& sse_op_table6
[b
].ext_mask
))
3716 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
3718 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
| REX_B(s
)]);
3720 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
3721 gen_lea_modrm(env
, s
, modrm
);
3723 case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
3724 case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
3725 case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3726 gen_ldq_env_A0(s
, op2_offset
+
3727 offsetof(ZMMReg
, ZMM_Q(0)));
3729 case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
3730 case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3731 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
3732 s
->mem_index
, MO_LEUL
);
3733 tcg_gen_st_i32(s
->tmp2_i32
, cpu_env
, op2_offset
+
3734 offsetof(ZMMReg
, ZMM_L(0)));
3736 case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3737 tcg_gen_qemu_ld_tl(s
->tmp0
, s
->A0
,
3738 s
->mem_index
, MO_LEUW
);
3739 tcg_gen_st16_tl(s
->tmp0
, cpu_env
, op2_offset
+
3740 offsetof(ZMMReg
, ZMM_W(0)));
3742 case 0x2a: /* movntqda */
3743 gen_ldo_env_A0(s
, op1_offset
);
3746 gen_ldo_env_A0(s
, op2_offset
);
3750 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
].mmx
);
3752 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
3754 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
3755 gen_lea_modrm(env
, s
, modrm
);
3756 gen_ldq_env_A0(s
, op2_offset
);
3759 if (sse_fn_epp
== SSE_SPECIAL
) {
3763 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
3764 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op2_offset
);
3765 sse_fn_epp(cpu_env
, s
->ptr0
, s
->ptr1
);
3768 set_cc_op(s
, CC_OP_EFLAGS
);
3775 /* Various integer extensions at 0f 38 f[0-f]. */
3776 b
= modrm
| (b1
<< 8);
3777 modrm
= x86_ldub_code(env
, s
);
3778 reg
= ((modrm
>> 3) & 7) | rex_r
;
3781 case 0x3f0: /* crc32 Gd,Eb */
3782 case 0x3f1: /* crc32 Gd,Ey */
3784 if (!(s
->cpuid_ext_features
& CPUID_EXT_SSE42
)) {
3787 if ((b
& 0xff) == 0xf0) {
3789 } else if (s
->dflag
!= MO_64
) {
3790 ot
= (s
->prefix
& PREFIX_DATA
? MO_16
: MO_32
);
3795 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[reg
]);
3796 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3797 gen_helper_crc32(s
->T0
, s
->tmp2_i32
,
3798 s
->T0
, tcg_const_i32(8 << ot
));
3800 ot
= mo_64_32(s
->dflag
);
3801 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
3804 case 0x1f0: /* crc32 or movbe */
3806 /* For these insns, the f3 prefix is supposed to have priority
3807 over the 66 prefix, but that's not what we implement above
3809 if (s
->prefix
& PREFIX_REPNZ
) {
3813 case 0x0f0: /* movbe Gy,My */
3814 case 0x0f1: /* movbe My,Gy */
3815 if (!(s
->cpuid_ext_features
& CPUID_EXT_MOVBE
)) {
3818 if (s
->dflag
!= MO_64
) {
3819 ot
= (s
->prefix
& PREFIX_DATA
? MO_16
: MO_32
);
3824 gen_lea_modrm(env
, s
, modrm
);
3826 tcg_gen_qemu_ld_tl(s
->T0
, s
->A0
,
3827 s
->mem_index
, ot
| MO_BE
);
3828 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
3830 tcg_gen_qemu_st_tl(cpu_regs
[reg
], s
->A0
,
3831 s
->mem_index
, ot
| MO_BE
);
3835 case 0x0f2: /* andn Gy, By, Ey */
3836 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)
3837 || !(s
->prefix
& PREFIX_VEX
)
3841 ot
= mo_64_32(s
->dflag
);
3842 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3843 tcg_gen_andc_tl(s
->T0
, s
->T0
, cpu_regs
[s
->vex_v
]);
3844 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
3845 gen_op_update1_cc(s
);
3846 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
3849 case 0x0f7: /* bextr Gy, Ey, By */
3850 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)
3851 || !(s
->prefix
& PREFIX_VEX
)
3855 ot
= mo_64_32(s
->dflag
);
3859 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3860 /* Extract START, and shift the operand.
3861 Shifts larger than operand size get zeros. */
3862 tcg_gen_ext8u_tl(s
->A0
, cpu_regs
[s
->vex_v
]);
3863 tcg_gen_shr_tl(s
->T0
, s
->T0
, s
->A0
);
3865 bound
= tcg_const_tl(ot
== MO_64
? 63 : 31);
3866 zero
= tcg_const_tl(0);
3867 tcg_gen_movcond_tl(TCG_COND_LEU
, s
->T0
, s
->A0
, bound
,
3869 tcg_temp_free(zero
);
3871 /* Extract the LEN into a mask. Lengths larger than
3872 operand size get all ones. */
3873 tcg_gen_extract_tl(s
->A0
, cpu_regs
[s
->vex_v
], 8, 8);
3874 tcg_gen_movcond_tl(TCG_COND_LEU
, s
->A0
, s
->A0
, bound
,
3876 tcg_temp_free(bound
);
3877 tcg_gen_movi_tl(s
->T1
, 1);
3878 tcg_gen_shl_tl(s
->T1
, s
->T1
, s
->A0
);
3879 tcg_gen_subi_tl(s
->T1
, s
->T1
, 1);
3880 tcg_gen_and_tl(s
->T0
, s
->T0
, s
->T1
);
3882 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
3883 gen_op_update1_cc(s
);
3884 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
3888 case 0x0f5: /* bzhi Gy, Ey, By */
3889 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3890 || !(s
->prefix
& PREFIX_VEX
)
3894 ot
= mo_64_32(s
->dflag
);
3895 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3896 tcg_gen_ext8u_tl(s
->T1
, cpu_regs
[s
->vex_v
]);
3898 TCGv bound
= tcg_const_tl(ot
== MO_64
? 63 : 31);
3899 /* Note that since we're using BMILG (in order to get O
3900 cleared) we need to store the inverse into C. */
3901 tcg_gen_setcond_tl(TCG_COND_LT
, cpu_cc_src
,
3903 tcg_gen_movcond_tl(TCG_COND_GT
, s
->T1
, s
->T1
,
3904 bound
, bound
, s
->T1
);
3905 tcg_temp_free(bound
);
3907 tcg_gen_movi_tl(s
->A0
, -1);
3908 tcg_gen_shl_tl(s
->A0
, s
->A0
, s
->T1
);
3909 tcg_gen_andc_tl(s
->T0
, s
->T0
, s
->A0
);
3910 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
3911 gen_op_update1_cc(s
);
3912 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
3915 case 0x3f6: /* mulx By, Gy, rdx, Ey */
3916 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3917 || !(s
->prefix
& PREFIX_VEX
)
3921 ot
= mo_64_32(s
->dflag
);
3922 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3925 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3926 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, cpu_regs
[R_EDX
]);
3927 tcg_gen_mulu2_i32(s
->tmp2_i32
, s
->tmp3_i32
,
3928 s
->tmp2_i32
, s
->tmp3_i32
);
3929 tcg_gen_extu_i32_tl(cpu_regs
[s
->vex_v
], s
->tmp2_i32
);
3930 tcg_gen_extu_i32_tl(cpu_regs
[reg
], s
->tmp3_i32
);
3932 #ifdef TARGET_X86_64
3934 tcg_gen_mulu2_i64(s
->T0
, s
->T1
,
3935 s
->T0
, cpu_regs
[R_EDX
]);
3936 tcg_gen_mov_i64(cpu_regs
[s
->vex_v
], s
->T0
);
3937 tcg_gen_mov_i64(cpu_regs
[reg
], s
->T1
);
3943 case 0x3f5: /* pdep Gy, By, Ey */
3944 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3945 || !(s
->prefix
& PREFIX_VEX
)
3949 ot
= mo_64_32(s
->dflag
);
3950 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3951 /* Note that by zero-extending the mask operand, we
3952 automatically handle zero-extending the result. */
3954 tcg_gen_mov_tl(s
->T1
, cpu_regs
[s
->vex_v
]);
3956 tcg_gen_ext32u_tl(s
->T1
, cpu_regs
[s
->vex_v
]);
3958 gen_helper_pdep(cpu_regs
[reg
], s
->T0
, s
->T1
);
3961 case 0x2f5: /* pext Gy, By, Ey */
3962 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3963 || !(s
->prefix
& PREFIX_VEX
)
3967 ot
= mo_64_32(s
->dflag
);
3968 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3969 /* Note that by zero-extending the mask operand, we
3970 automatically handle zero-extending the result. */
3972 tcg_gen_mov_tl(s
->T1
, cpu_regs
[s
->vex_v
]);
3974 tcg_gen_ext32u_tl(s
->T1
, cpu_regs
[s
->vex_v
]);
3976 gen_helper_pext(cpu_regs
[reg
], s
->T0
, s
->T1
);
3979 case 0x1f6: /* adcx Gy, Ey */
3980 case 0x2f6: /* adox Gy, Ey */
3981 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_ADX
)) {
3984 TCGv carry_in
, carry_out
, zero
;
3987 ot
= mo_64_32(s
->dflag
);
3988 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3990 /* Re-use the carry-out from a previous round. */
3992 carry_out
= (b
== 0x1f6 ? cpu_cc_dst
: cpu_cc_src2
);
3996 carry_in
= cpu_cc_dst
;
3997 end_op
= CC_OP_ADCX
;
3999 end_op
= CC_OP_ADCOX
;
4004 end_op
= CC_OP_ADCOX
;
4006 carry_in
= cpu_cc_src2
;
4007 end_op
= CC_OP_ADOX
;
4011 end_op
= CC_OP_ADCOX
;
4012 carry_in
= carry_out
;
4015 end_op
= (b
== 0x1f6 ? CC_OP_ADCX
: CC_OP_ADOX
);
4018 /* If we can't reuse carry-out, get it out of EFLAGS. */
4020 if (s
->cc_op
!= CC_OP_ADCX
&& s
->cc_op
!= CC_OP_ADOX
) {
4021 gen_compute_eflags(s
);
4024 tcg_gen_extract_tl(carry_in
, cpu_cc_src
,
4025 ctz32(b
== 0x1f6 ? CC_C
: CC_O
), 1);
4029 #ifdef TARGET_X86_64
4031 /* If we know TL is 64-bit, and we want a 32-bit
4032 result, just do everything in 64-bit arithmetic. */
4033 tcg_gen_ext32u_i64(cpu_regs
[reg
], cpu_regs
[reg
]);
4034 tcg_gen_ext32u_i64(s
->T0
, s
->T0
);
4035 tcg_gen_add_i64(s
->T0
, s
->T0
, cpu_regs
[reg
]);
4036 tcg_gen_add_i64(s
->T0
, s
->T0
, carry_in
);
4037 tcg_gen_ext32u_i64(cpu_regs
[reg
], s
->T0
);
4038 tcg_gen_shri_i64(carry_out
, s
->T0
, 32);
4042 /* Otherwise compute the carry-out in two steps. */
4043 zero
= tcg_const_tl(0);
4044 tcg_gen_add2_tl(s
->T0
, carry_out
,
4047 tcg_gen_add2_tl(cpu_regs
[reg
], carry_out
,
4048 cpu_regs
[reg
], carry_out
,
4050 tcg_temp_free(zero
);
4053 set_cc_op(s
, end_op
);
4057 case 0x1f7: /* shlx Gy, Ey, By */
4058 case 0x2f7: /* sarx Gy, Ey, By */
4059 case 0x3f7: /* shrx Gy, Ey, By */
4060 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
4061 || !(s
->prefix
& PREFIX_VEX
)
4065 ot
= mo_64_32(s
->dflag
);
4066 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
4068 tcg_gen_andi_tl(s
->T1
, cpu_regs
[s
->vex_v
], 63);
4070 tcg_gen_andi_tl(s
->T1
, cpu_regs
[s
->vex_v
], 31);
4073 tcg_gen_shl_tl(s
->T0
, s
->T0
, s
->T1
);
4074 } else if (b
== 0x2f7) {
4076 tcg_gen_ext32s_tl(s
->T0
, s
->T0
);
4078 tcg_gen_sar_tl(s
->T0
, s
->T0
, s
->T1
);
4081 tcg_gen_ext32u_tl(s
->T0
, s
->T0
);
4083 tcg_gen_shr_tl(s
->T0
, s
->T0
, s
->T1
);
4085 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
4091 case 0x3f3: /* Group 17 */
4092 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)
4093 || !(s
->prefix
& PREFIX_VEX
)
4097 ot
= mo_64_32(s
->dflag
);
4098 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
4100 tcg_gen_mov_tl(cpu_cc_src
, s
->T0
);
4102 case 1: /* blsr By,Ey */
4103 tcg_gen_subi_tl(s
->T1
, s
->T0
, 1);
4104 tcg_gen_and_tl(s
->T0
, s
->T0
, s
->T1
);
4106 case 2: /* blsmsk By,Ey */
4107 tcg_gen_subi_tl(s
->T1
, s
->T0
, 1);
4108 tcg_gen_xor_tl(s
->T0
, s
->T0
, s
->T1
);
4110 case 3: /* blsi By, Ey */
4111 tcg_gen_neg_tl(s
->T1
, s
->T0
);
4112 tcg_gen_and_tl(s
->T0
, s
->T0
, s
->T1
);
4117 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
4118 gen_op_mov_reg_v(s
, ot
, s
->vex_v
, s
->T0
);
4119 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
4130 modrm
= x86_ldub_code(env
, s
);
4132 reg
= ((modrm
>> 3) & 7) | rex_r
;
4133 mod
= (modrm
>> 6) & 3;
4138 sse_fn_eppi
= sse_op_table7
[b
].op
[b1
];
4142 if (!(s
->cpuid_ext_features
& sse_op_table7
[b
].ext_mask
))
4147 if (sse_fn_eppi
== SSE_SPECIAL
) {
4148 ot
= mo_64_32(s
->dflag
);
4149 rm
= (modrm
& 7) | REX_B(s
);
4151 gen_lea_modrm(env
, s
, modrm
);
4152 reg
= ((modrm
>> 3) & 7) | rex_r
;
4153 val
= x86_ldub_code(env
, s
);
4155 case 0x14: /* pextrb */
4156 tcg_gen_ld8u_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
,
4157 xmm_regs
[reg
].ZMM_B(val
& 15)));
4159 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
4161 tcg_gen_qemu_st_tl(s
->T0
, s
->A0
,
4162 s
->mem_index
, MO_UB
);
4165 case 0x15: /* pextrw */
4166 tcg_gen_ld16u_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
,
4167 xmm_regs
[reg
].ZMM_W(val
& 7)));
4169 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
4171 tcg_gen_qemu_st_tl(s
->T0
, s
->A0
,
4172 s
->mem_index
, MO_LEUW
);
4176 if (ot
== MO_32
) { /* pextrd */
4177 tcg_gen_ld_i32(s
->tmp2_i32
, cpu_env
,
4178 offsetof(CPUX86State
,
4179 xmm_regs
[reg
].ZMM_L(val
& 3)));
4181 tcg_gen_extu_i32_tl(cpu_regs
[rm
], s
->tmp2_i32
);
4183 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
4184 s
->mem_index
, MO_LEUL
);
4186 } else { /* pextrq */
4187 #ifdef TARGET_X86_64
4188 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
,
4189 offsetof(CPUX86State
,
4190 xmm_regs
[reg
].ZMM_Q(val
& 1)));
4192 tcg_gen_mov_i64(cpu_regs
[rm
], s
->tmp1_i64
);
4194 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
,
4195 s
->mem_index
, MO_LEQ
);
4202 case 0x17: /* extractps */
4203 tcg_gen_ld32u_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
,
4204 xmm_regs
[reg
].ZMM_L(val
& 3)));
4206 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
4208 tcg_gen_qemu_st_tl(s
->T0
, s
->A0
,
4209 s
->mem_index
, MO_LEUL
);
4212 case 0x20: /* pinsrb */
4214 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, rm
);
4216 tcg_gen_qemu_ld_tl(s
->T0
, s
->A0
,
4217 s
->mem_index
, MO_UB
);
4219 tcg_gen_st8_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
,
4220 xmm_regs
[reg
].ZMM_B(val
& 15)));
4222 case 0x21: /* insertps */
4224 tcg_gen_ld_i32(s
->tmp2_i32
, cpu_env
,
4225 offsetof(CPUX86State
,xmm_regs
[rm
]
4226 .ZMM_L((val
>> 6) & 3)));
4228 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
4229 s
->mem_index
, MO_LEUL
);
4231 tcg_gen_st_i32(s
->tmp2_i32
, cpu_env
,
4232 offsetof(CPUX86State
,xmm_regs
[reg
]
4233 .ZMM_L((val
>> 4) & 3)));
4235 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4236 cpu_env
, offsetof(CPUX86State
,
4237 xmm_regs
[reg
].ZMM_L(0)));
4239 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4240 cpu_env
, offsetof(CPUX86State
,
4241 xmm_regs
[reg
].ZMM_L(1)));
4243 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4244 cpu_env
, offsetof(CPUX86State
,
4245 xmm_regs
[reg
].ZMM_L(2)));
4247 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4248 cpu_env
, offsetof(CPUX86State
,
4249 xmm_regs
[reg
].ZMM_L(3)));
4252 if (ot
== MO_32
) { /* pinsrd */
4254 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[rm
]);
4256 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
4257 s
->mem_index
, MO_LEUL
);
4259 tcg_gen_st_i32(s
->tmp2_i32
, cpu_env
,
4260 offsetof(CPUX86State
,
4261 xmm_regs
[reg
].ZMM_L(val
& 3)));
4262 } else { /* pinsrq */
4263 #ifdef TARGET_X86_64
4265 gen_op_mov_v_reg(s
, ot
, s
->tmp1_i64
, rm
);
4267 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
,
4268 s
->mem_index
, MO_LEQ
);
4270 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
,
4271 offsetof(CPUX86State
,
4272 xmm_regs
[reg
].ZMM_Q(val
& 1)));
4283 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
4285 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
| REX_B(s
)]);
4287 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
4288 gen_lea_modrm(env
, s
, modrm
);
4289 gen_ldo_env_A0(s
, op2_offset
);
4292 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
].mmx
);
4294 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
4296 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
4297 gen_lea_modrm(env
, s
, modrm
);
4298 gen_ldq_env_A0(s
, op2_offset
);
4301 val
= x86_ldub_code(env
, s
);
4303 if ((b
& 0xfc) == 0x60) { /* pcmpXstrX */
4304 set_cc_op(s
, CC_OP_EFLAGS
);
4306 if (s
->dflag
== MO_64
) {
4307 /* The helper must use entire 64-bit gp registers */
4312 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
4313 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op2_offset
);
4314 sse_fn_eppi(cpu_env
, s
->ptr0
, s
->ptr1
, tcg_const_i32(val
));
4318 /* Various integer extensions at 0f 3a f[0-f]. */
4319 b
= modrm
| (b1
<< 8);
4320 modrm
= x86_ldub_code(env
, s
);
4321 reg
= ((modrm
>> 3) & 7) | rex_r
;
4324 case 0x3f0: /* rorx Gy,Ey, Ib */
4325 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
4326 || !(s
->prefix
& PREFIX_VEX
)
4330 ot
= mo_64_32(s
->dflag
);
4331 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
4332 b
= x86_ldub_code(env
, s
);
4334 tcg_gen_rotri_tl(s
->T0
, s
->T0
, b
& 63);
4336 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
4337 tcg_gen_rotri_i32(s
->tmp2_i32
, s
->tmp2_i32
, b
& 31);
4338 tcg_gen_extu_i32_tl(s
->T0
, s
->tmp2_i32
);
4340 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
4350 gen_unknown_opcode(env
, s
);
4354 /* generic MMX or SSE operation */
4356 case 0x70: /* pshufx insn */
4357 case 0xc6: /* pshufx insn */
4358 case 0xc2: /* compare insns */
4365 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
4369 gen_lea_modrm(env
, s
, modrm
);
4370 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
4376 /* Most sse scalar operations. */
4379 } else if (b1
== 3) {
4384 case 0x2e: /* ucomis[sd] */
4385 case 0x2f: /* comis[sd] */
4397 gen_op_ld_v(s
, MO_32
, s
->T0
, s
->A0
);
4398 tcg_gen_st32_tl(s
->T0
, cpu_env
,
4399 offsetof(CPUX86State
,xmm_t0
.ZMM_L(0)));
4403 gen_ldq_env_A0(s
, offsetof(CPUX86State
, xmm_t0
.ZMM_D(0)));
4406 /* 128 bit access */
4407 gen_ldo_env_A0(s
, op2_offset
);
4411 rm
= (modrm
& 7) | REX_B(s
);
4412 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
4415 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
].mmx
);
4417 gen_lea_modrm(env
, s
, modrm
);
4418 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
4419 gen_ldq_env_A0(s
, op2_offset
);
4422 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
4426 case 0x0f: /* 3DNow! data insns */
4427 val
= x86_ldub_code(env
, s
);
4428 sse_fn_epp
= sse_op_table5
[val
];
4432 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_3DNOW
)) {
4435 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
4436 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op2_offset
);
4437 sse_fn_epp(cpu_env
, s
->ptr0
, s
->ptr1
);
4439 case 0x70: /* pshufx insn */
4440 case 0xc6: /* pshufx insn */
4441 val
= x86_ldub_code(env
, s
);
4442 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
4443 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op2_offset
);
4444 /* XXX: introduce a new table? */
4445 sse_fn_ppi
= (SSEFunc_0_ppi
)sse_fn_epp
;
4446 sse_fn_ppi(s
->ptr0
, s
->ptr1
, tcg_const_i32(val
));
4450 val
= x86_ldub_code(env
, s
);
4453 sse_fn_epp
= sse_op_table4
[val
][b1
];
4455 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
4456 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op2_offset
);
4457 sse_fn_epp(cpu_env
, s
->ptr0
, s
->ptr1
);
4460 /* maskmov : we must prepare A0 */
4463 tcg_gen_mov_tl(s
->A0
, cpu_regs
[R_EDI
]);
4464 gen_extu(s
->aflag
, s
->A0
);
4465 gen_add_A0_ds_seg(s
);
4467 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
4468 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op2_offset
);
4469 /* XXX: introduce a new table? */
4470 sse_fn_eppt
= (SSEFunc_0_eppt
)sse_fn_epp
;
4471 sse_fn_eppt(cpu_env
, s
->ptr0
, s
->ptr1
, s
->A0
);
4474 tcg_gen_addi_ptr(s
->ptr0
, cpu_env
, op1_offset
);
4475 tcg_gen_addi_ptr(s
->ptr1
, cpu_env
, op2_offset
);
4476 sse_fn_epp(cpu_env
, s
->ptr0
, s
->ptr1
);
4479 if (b
== 0x2e || b
== 0x2f) {
4480 set_cc_op(s
, CC_OP_EFLAGS
);
4485 /* convert one instruction. s->base.is_jmp is set if the translation must
4486 be stopped. Return the next pc value */
4487 static target_ulong
disas_insn(DisasContext
*s
, CPUState
*cpu
)
4489 CPUX86State
*env
= cpu
->env_ptr
;
4492 MemOp ot
, aflag
, dflag
;
4493 int modrm
, reg
, rm
, mod
, op
, opreg
, val
;
4494 target_ulong next_eip
, tval
;
4496 target_ulong pc_start
= s
->base
.pc_next
;
4498 s
->pc_start
= s
->pc
= pc_start
;
4500 #ifdef TARGET_X86_64
4503 s
->x86_64_hregs
= false;
4505 s
->rip_offset
= 0; /* for relative ip address */
4508 if (sigsetjmp(s
->jmpbuf
, 0) != 0) {
4509 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
4518 b
= x86_ldub_code(env
, s
);
4519 /* Collect prefixes. */
4522 prefixes
|= PREFIX_REPZ
;
4525 prefixes
|= PREFIX_REPNZ
;
4528 prefixes
|= PREFIX_LOCK
;
4549 prefixes
|= PREFIX_DATA
;
4552 prefixes
|= PREFIX_ADR
;
4554 #ifdef TARGET_X86_64
4558 rex_w
= (b
>> 3) & 1;
4559 rex_r
= (b
& 0x4) << 1;
4560 s
->rex_x
= (b
& 0x2) << 2;
4561 REX_B(s
) = (b
& 0x1) << 3;
4562 /* select uniform byte register addressing */
4563 s
->x86_64_hregs
= true;
4568 case 0xc5: /* 2-byte VEX */
4569 case 0xc4: /* 3-byte VEX */
4570 /* VEX prefixes cannot be used except in 32-bit mode.
4571 Otherwise the instruction is LES or LDS. */
4572 if (s
->code32
&& !s
->vm86
) {
4573 static const int pp_prefix
[4] = {
4574 0, PREFIX_DATA
, PREFIX_REPZ
, PREFIX_REPNZ
4576 int vex3
, vex2
= x86_ldub_code(env
, s
);
4578 if (!CODE64(s
) && (vex2
& 0xc0) != 0xc0) {
4579 /* 4.1.4.6: In 32-bit mode, bits [7:6] must be 11b,
4580 otherwise the instruction is LES or LDS. */
4581 s
->pc
--; /* rewind the advance_pc() x86_ldub_code() did */
4585 /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
4586 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
4587 | PREFIX_LOCK
| PREFIX_DATA
)) {
4590 #ifdef TARGET_X86_64
4591 if (s
->x86_64_hregs
) {
4595 rex_r
= (~vex2
>> 4) & 8;
4597 /* 2-byte VEX prefix: RVVVVlpp, implied 0f leading opcode byte */
4599 b
= x86_ldub_code(env
, s
) | 0x100;
4601 /* 3-byte VEX prefix: RXBmmmmm wVVVVlpp */
4602 #ifdef TARGET_X86_64
4603 s
->rex_x
= (~vex2
>> 3) & 8;
4604 s
->rex_b
= (~vex2
>> 2) & 8;
4606 vex3
= x86_ldub_code(env
, s
);
4607 rex_w
= (vex3
>> 7) & 1;
4608 switch (vex2
& 0x1f) {
4609 case 0x01: /* Implied 0f leading opcode bytes. */
4610 b
= x86_ldub_code(env
, s
) | 0x100;
4612 case 0x02: /* Implied 0f 38 leading opcode bytes. */
4615 case 0x03: /* Implied 0f 3a leading opcode bytes. */
4618 default: /* Reserved for future use. */
4622 s
->vex_v
= (~vex3
>> 3) & 0xf;
4623 s
->vex_l
= (vex3
>> 2) & 1;
4624 prefixes
|= pp_prefix
[vex3
& 3] | PREFIX_VEX
;
4629 /* Post-process prefixes. */
4631 /* In 64-bit mode, the default data size is 32-bit. Select 64-bit
4632 data with rex_w, and 16-bit data with 0x66; rex_w takes precedence
4633 over 0x66 if both are present. */
4634 dflag
= (rex_w
> 0 ? MO_64
: prefixes
& PREFIX_DATA
? MO_16
: MO_32
);
4635 /* In 64-bit mode, 0x67 selects 32-bit addressing. */
4636 aflag
= (prefixes
& PREFIX_ADR
? MO_32
: MO_64
);
4638 /* In 16/32-bit mode, 0x66 selects the opposite data size. */
4639 if (s
->code32
^ ((prefixes
& PREFIX_DATA
) != 0)) {
4644 /* In 16/32-bit mode, 0x67 selects the opposite addressing. */
4645 if (s
->code32
^ ((prefixes
& PREFIX_ADR
) != 0)) {
4652 s
->prefix
= prefixes
;
4656 /* now check op code */
4660 /**************************/
4661 /* extended op code */
4662 b
= x86_ldub_code(env
, s
) | 0x100;
4665 /**************************/
4680 ot
= mo_b_d(b
, dflag
);
4683 case 0: /* OP Ev, Gv */
4684 modrm
= x86_ldub_code(env
, s
);
4685 reg
= ((modrm
>> 3) & 7) | rex_r
;
4686 mod
= (modrm
>> 6) & 3;
4687 rm
= (modrm
& 7) | REX_B(s
);
4689 gen_lea_modrm(env
, s
, modrm
);
4691 } else if (op
== OP_XORL
&& rm
== reg
) {
4693 /* xor reg, reg optimisation */
4694 set_cc_op(s
, CC_OP_CLR
);
4695 tcg_gen_movi_tl(s
->T0
, 0);
4696 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
4701 gen_op_mov_v_reg(s
, ot
, s
->T1
, reg
);
4702 gen_op(s
, op
, ot
, opreg
);
4704 case 1: /* OP Gv, Ev */
4705 modrm
= x86_ldub_code(env
, s
);
4706 mod
= (modrm
>> 6) & 3;
4707 reg
= ((modrm
>> 3) & 7) | rex_r
;
4708 rm
= (modrm
& 7) | REX_B(s
);
4710 gen_lea_modrm(env
, s
, modrm
);
4711 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
4712 } else if (op
== OP_XORL
&& rm
== reg
) {
4715 gen_op_mov_v_reg(s
, ot
, s
->T1
, rm
);
4717 gen_op(s
, op
, ot
, reg
);
4719 case 2: /* OP A, Iv */
4720 val
= insn_get(env
, s
, ot
);
4721 tcg_gen_movi_tl(s
->T1
, val
);
4722 gen_op(s
, op
, ot
, OR_EAX
);
4732 case 0x80: /* GRP1 */
4738 ot
= mo_b_d(b
, dflag
);
4740 modrm
= x86_ldub_code(env
, s
);
4741 mod
= (modrm
>> 6) & 3;
4742 rm
= (modrm
& 7) | REX_B(s
);
4743 op
= (modrm
>> 3) & 7;
4749 s
->rip_offset
= insn_const_size(ot
);
4750 gen_lea_modrm(env
, s
, modrm
);
4761 val
= insn_get(env
, s
, ot
);
4764 val
= (int8_t)insn_get(env
, s
, MO_8
);
4767 tcg_gen_movi_tl(s
->T1
, val
);
4768 gen_op(s
, op
, ot
, opreg
);
4772 /**************************/
4773 /* inc, dec, and other misc arith */
4774 case 0x40 ... 0x47: /* inc Gv */
4776 gen_inc(s
, ot
, OR_EAX
+ (b
& 7), 1);
4778 case 0x48 ... 0x4f: /* dec Gv */
4780 gen_inc(s
, ot
, OR_EAX
+ (b
& 7), -1);
4782 case 0xf6: /* GRP3 */
4784 ot
= mo_b_d(b
, dflag
);
4786 modrm
= x86_ldub_code(env
, s
);
4787 mod
= (modrm
>> 6) & 3;
4788 rm
= (modrm
& 7) | REX_B(s
);
4789 op
= (modrm
>> 3) & 7;
4792 s
->rip_offset
= insn_const_size(ot
);
4794 gen_lea_modrm(env
, s
, modrm
);
4795 /* For those below that handle locked memory, don't load here. */
4796 if (!(s
->prefix
& PREFIX_LOCK
)
4798 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
4801 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
4806 val
= insn_get(env
, s
, ot
);
4807 tcg_gen_movi_tl(s
->T1
, val
);
4808 gen_op_testl_T0_T1_cc(s
);
4809 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
4812 if (s
->prefix
& PREFIX_LOCK
) {
4816 tcg_gen_movi_tl(s
->T0
, ~0);
4817 tcg_gen_atomic_xor_fetch_tl(s
->T0
, s
->A0
, s
->T0
,
4818 s
->mem_index
, ot
| MO_LE
);
4820 tcg_gen_not_tl(s
->T0
, s
->T0
);
4822 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
4824 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
4829 if (s
->prefix
& PREFIX_LOCK
) {
4831 TCGv a0
, t0
, t1
, t2
;
4836 a0
= tcg_temp_local_new();
4837 t0
= tcg_temp_local_new();
4838 label1
= gen_new_label();
4840 tcg_gen_mov_tl(a0
, s
->A0
);
4841 tcg_gen_mov_tl(t0
, s
->T0
);
4843 gen_set_label(label1
);
4844 t1
= tcg_temp_new();
4845 t2
= tcg_temp_new();
4846 tcg_gen_mov_tl(t2
, t0
);
4847 tcg_gen_neg_tl(t1
, t0
);
4848 tcg_gen_atomic_cmpxchg_tl(t0
, a0
, t0
, t1
,
4849 s
->mem_index
, ot
| MO_LE
);
4851 tcg_gen_brcond_tl(TCG_COND_NE
, t0
, t2
, label1
);
4855 tcg_gen_mov_tl(s
->T0
, t0
);
4858 tcg_gen_neg_tl(s
->T0
, s
->T0
);
4860 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
4862 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
4865 gen_op_update_neg_cc(s
);
4866 set_cc_op(s
, CC_OP_SUBB
+ ot
);
4871 gen_op_mov_v_reg(s
, MO_8
, s
->T1
, R_EAX
);
4872 tcg_gen_ext8u_tl(s
->T0
, s
->T0
);
4873 tcg_gen_ext8u_tl(s
->T1
, s
->T1
);
4874 /* XXX: use 32 bit mul which could be faster */
4875 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
4876 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
4877 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
4878 tcg_gen_andi_tl(cpu_cc_src
, s
->T0
, 0xff00);
4879 set_cc_op(s
, CC_OP_MULB
);
4882 gen_op_mov_v_reg(s
, MO_16
, s
->T1
, R_EAX
);
4883 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
4884 tcg_gen_ext16u_tl(s
->T1
, s
->T1
);
4885 /* XXX: use 32 bit mul which could be faster */
4886 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
4887 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
4888 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
4889 tcg_gen_shri_tl(s
->T0
, s
->T0
, 16);
4890 gen_op_mov_reg_v(s
, MO_16
, R_EDX
, s
->T0
);
4891 tcg_gen_mov_tl(cpu_cc_src
, s
->T0
);
4892 set_cc_op(s
, CC_OP_MULW
);
4896 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
4897 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, cpu_regs
[R_EAX
]);
4898 tcg_gen_mulu2_i32(s
->tmp2_i32
, s
->tmp3_i32
,
4899 s
->tmp2_i32
, s
->tmp3_i32
);
4900 tcg_gen_extu_i32_tl(cpu_regs
[R_EAX
], s
->tmp2_i32
);
4901 tcg_gen_extu_i32_tl(cpu_regs
[R_EDX
], s
->tmp3_i32
);
4902 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4903 tcg_gen_mov_tl(cpu_cc_src
, cpu_regs
[R_EDX
]);
4904 set_cc_op(s
, CC_OP_MULL
);
4906 #ifdef TARGET_X86_64
4908 tcg_gen_mulu2_i64(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
],
4909 s
->T0
, cpu_regs
[R_EAX
]);
4910 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4911 tcg_gen_mov_tl(cpu_cc_src
, cpu_regs
[R_EDX
]);
4912 set_cc_op(s
, CC_OP_MULQ
);
4920 gen_op_mov_v_reg(s
, MO_8
, s
->T1
, R_EAX
);
4921 tcg_gen_ext8s_tl(s
->T0
, s
->T0
);
4922 tcg_gen_ext8s_tl(s
->T1
, s
->T1
);
4923 /* XXX: use 32 bit mul which could be faster */
4924 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
4925 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
4926 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
4927 tcg_gen_ext8s_tl(s
->tmp0
, s
->T0
);
4928 tcg_gen_sub_tl(cpu_cc_src
, s
->T0
, s
->tmp0
);
4929 set_cc_op(s
, CC_OP_MULB
);
4932 gen_op_mov_v_reg(s
, MO_16
, s
->T1
, R_EAX
);
4933 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
4934 tcg_gen_ext16s_tl(s
->T1
, s
->T1
);
4935 /* XXX: use 32 bit mul which could be faster */
4936 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
4937 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
4938 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
4939 tcg_gen_ext16s_tl(s
->tmp0
, s
->T0
);
4940 tcg_gen_sub_tl(cpu_cc_src
, s
->T0
, s
->tmp0
);
4941 tcg_gen_shri_tl(s
->T0
, s
->T0
, 16);
4942 gen_op_mov_reg_v(s
, MO_16
, R_EDX
, s
->T0
);
4943 set_cc_op(s
, CC_OP_MULW
);
4947 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
4948 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, cpu_regs
[R_EAX
]);
4949 tcg_gen_muls2_i32(s
->tmp2_i32
, s
->tmp3_i32
,
4950 s
->tmp2_i32
, s
->tmp3_i32
);
4951 tcg_gen_extu_i32_tl(cpu_regs
[R_EAX
], s
->tmp2_i32
);
4952 tcg_gen_extu_i32_tl(cpu_regs
[R_EDX
], s
->tmp3_i32
);
4953 tcg_gen_sari_i32(s
->tmp2_i32
, s
->tmp2_i32
, 31);
4954 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4955 tcg_gen_sub_i32(s
->tmp2_i32
, s
->tmp2_i32
, s
->tmp3_i32
);
4956 tcg_gen_extu_i32_tl(cpu_cc_src
, s
->tmp2_i32
);
4957 set_cc_op(s
, CC_OP_MULL
);
4959 #ifdef TARGET_X86_64
4961 tcg_gen_muls2_i64(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
],
4962 s
->T0
, cpu_regs
[R_EAX
]);
4963 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4964 tcg_gen_sari_tl(cpu_cc_src
, cpu_regs
[R_EAX
], 63);
4965 tcg_gen_sub_tl(cpu_cc_src
, cpu_cc_src
, cpu_regs
[R_EDX
]);
4966 set_cc_op(s
, CC_OP_MULQ
);
4974 gen_helper_divb_AL(cpu_env
, s
->T0
);
4977 gen_helper_divw_AX(cpu_env
, s
->T0
);
4981 gen_helper_divl_EAX(cpu_env
, s
->T0
);
4983 #ifdef TARGET_X86_64
4985 gen_helper_divq_EAX(cpu_env
, s
->T0
);
4993 gen_helper_idivb_AL(cpu_env
, s
->T0
);
4996 gen_helper_idivw_AX(cpu_env
, s
->T0
);
5000 gen_helper_idivl_EAX(cpu_env
, s
->T0
);
5002 #ifdef TARGET_X86_64
5004 gen_helper_idivq_EAX(cpu_env
, s
->T0
);
5014 case 0xfe: /* GRP4 */
5015 case 0xff: /* GRP5 */
5016 ot
= mo_b_d(b
, dflag
);
5018 modrm
= x86_ldub_code(env
, s
);
5019 mod
= (modrm
>> 6) & 3;
5020 rm
= (modrm
& 7) | REX_B(s
);
5021 op
= (modrm
>> 3) & 7;
5022 if (op
>= 2 && b
== 0xfe) {
5026 if (op
== 2 || op
== 4) {
5027 /* operand size for jumps is 64 bit */
5029 } else if (op
== 3 || op
== 5) {
5030 ot
= dflag
!= MO_16
? MO_32
+ (rex_w
== 1) : MO_16
;
5031 } else if (op
== 6) {
5032 /* default push size is 64 bit */
5033 ot
= mo_pushpop(s
, dflag
);
5037 gen_lea_modrm(env
, s
, modrm
);
5038 if (op
>= 2 && op
!= 3 && op
!= 5)
5039 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
5041 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
5045 case 0: /* inc Ev */
5050 gen_inc(s
, ot
, opreg
, 1);
5052 case 1: /* dec Ev */
5057 gen_inc(s
, ot
, opreg
, -1);
5059 case 2: /* call Ev */
5060 /* XXX: optimize if memory (no 'and' is necessary) */
5061 if (dflag
== MO_16
) {
5062 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
5064 next_eip
= s
->pc
- s
->cs_base
;
5065 tcg_gen_movi_tl(s
->T1
, next_eip
);
5066 gen_push_v(s
, s
->T1
);
5067 gen_op_jmp_v(s
->T0
);
5071 case 3: /* lcall Ev */
5072 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
5073 gen_add_A0_im(s
, 1 << ot
);
5074 gen_op_ld_v(s
, MO_16
, s
->T0
, s
->A0
);
5076 if (s
->pe
&& !s
->vm86
) {
5077 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
5078 gen_helper_lcall_protected(cpu_env
, s
->tmp2_i32
, s
->T1
,
5079 tcg_const_i32(dflag
- 1),
5080 tcg_const_tl(s
->pc
- s
->cs_base
));
5082 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
5083 gen_helper_lcall_real(cpu_env
, s
->tmp2_i32
, s
->T1
,
5084 tcg_const_i32(dflag
- 1),
5085 tcg_const_i32(s
->pc
- s
->cs_base
));
5087 tcg_gen_ld_tl(s
->tmp4
, cpu_env
, offsetof(CPUX86State
, eip
));
5090 case 4: /* jmp Ev */
5091 if (dflag
== MO_16
) {
5092 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
5094 gen_op_jmp_v(s
->T0
);
5098 case 5: /* ljmp Ev */
5099 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
5100 gen_add_A0_im(s
, 1 << ot
);
5101 gen_op_ld_v(s
, MO_16
, s
->T0
, s
->A0
);
5103 if (s
->pe
&& !s
->vm86
) {
5104 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
5105 gen_helper_ljmp_protected(cpu_env
, s
->tmp2_i32
, s
->T1
,
5106 tcg_const_tl(s
->pc
- s
->cs_base
));
5108 gen_op_movl_seg_T0_vm(s
, R_CS
);
5109 gen_op_jmp_v(s
->T1
);
5111 tcg_gen_ld_tl(s
->tmp4
, cpu_env
, offsetof(CPUX86State
, eip
));
5114 case 6: /* push Ev */
5115 gen_push_v(s
, s
->T0
);
5122 case 0x84: /* test Ev, Gv */
5124 ot
= mo_b_d(b
, dflag
);
5126 modrm
= x86_ldub_code(env
, s
);
5127 reg
= ((modrm
>> 3) & 7) | rex_r
;
5129 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5130 gen_op_mov_v_reg(s
, ot
, s
->T1
, reg
);
5131 gen_op_testl_T0_T1_cc(s
);
5132 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
5135 case 0xa8: /* test eAX, Iv */
5137 ot
= mo_b_d(b
, dflag
);
5138 val
= insn_get(env
, s
, ot
);
5140 gen_op_mov_v_reg(s
, ot
, s
->T0
, OR_EAX
);
5141 tcg_gen_movi_tl(s
->T1
, val
);
5142 gen_op_testl_T0_T1_cc(s
);
5143 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
5146 case 0x98: /* CWDE/CBW */
5148 #ifdef TARGET_X86_64
5150 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, R_EAX
);
5151 tcg_gen_ext32s_tl(s
->T0
, s
->T0
);
5152 gen_op_mov_reg_v(s
, MO_64
, R_EAX
, s
->T0
);
5156 gen_op_mov_v_reg(s
, MO_16
, s
->T0
, R_EAX
);
5157 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
5158 gen_op_mov_reg_v(s
, MO_32
, R_EAX
, s
->T0
);
5161 gen_op_mov_v_reg(s
, MO_8
, s
->T0
, R_EAX
);
5162 tcg_gen_ext8s_tl(s
->T0
, s
->T0
);
5163 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
5169 case 0x99: /* CDQ/CWD */
5171 #ifdef TARGET_X86_64
5173 gen_op_mov_v_reg(s
, MO_64
, s
->T0
, R_EAX
);
5174 tcg_gen_sari_tl(s
->T0
, s
->T0
, 63);
5175 gen_op_mov_reg_v(s
, MO_64
, R_EDX
, s
->T0
);
5179 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, R_EAX
);
5180 tcg_gen_ext32s_tl(s
->T0
, s
->T0
);
5181 tcg_gen_sari_tl(s
->T0
, s
->T0
, 31);
5182 gen_op_mov_reg_v(s
, MO_32
, R_EDX
, s
->T0
);
5185 gen_op_mov_v_reg(s
, MO_16
, s
->T0
, R_EAX
);
5186 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
5187 tcg_gen_sari_tl(s
->T0
, s
->T0
, 15);
5188 gen_op_mov_reg_v(s
, MO_16
, R_EDX
, s
->T0
);
5194 case 0x1af: /* imul Gv, Ev */
5195 case 0x69: /* imul Gv, Ev, I */
5198 modrm
= x86_ldub_code(env
, s
);
5199 reg
= ((modrm
>> 3) & 7) | rex_r
;
5201 s
->rip_offset
= insn_const_size(ot
);
5204 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5206 val
= insn_get(env
, s
, ot
);
5207 tcg_gen_movi_tl(s
->T1
, val
);
5208 } else if (b
== 0x6b) {
5209 val
= (int8_t)insn_get(env
, s
, MO_8
);
5210 tcg_gen_movi_tl(s
->T1
, val
);
5212 gen_op_mov_v_reg(s
, ot
, s
->T1
, reg
);
5215 #ifdef TARGET_X86_64
5217 tcg_gen_muls2_i64(cpu_regs
[reg
], s
->T1
, s
->T0
, s
->T1
);
5218 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[reg
]);
5219 tcg_gen_sari_tl(cpu_cc_src
, cpu_cc_dst
, 63);
5220 tcg_gen_sub_tl(cpu_cc_src
, cpu_cc_src
, s
->T1
);
5224 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
5225 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T1
);
5226 tcg_gen_muls2_i32(s
->tmp2_i32
, s
->tmp3_i32
,
5227 s
->tmp2_i32
, s
->tmp3_i32
);
5228 tcg_gen_extu_i32_tl(cpu_regs
[reg
], s
->tmp2_i32
);
5229 tcg_gen_sari_i32(s
->tmp2_i32
, s
->tmp2_i32
, 31);
5230 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[reg
]);
5231 tcg_gen_sub_i32(s
->tmp2_i32
, s
->tmp2_i32
, s
->tmp3_i32
);
5232 tcg_gen_extu_i32_tl(cpu_cc_src
, s
->tmp2_i32
);
5235 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
5236 tcg_gen_ext16s_tl(s
->T1
, s
->T1
);
5237 /* XXX: use 32 bit mul which could be faster */
5238 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
5239 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
5240 tcg_gen_ext16s_tl(s
->tmp0
, s
->T0
);
5241 tcg_gen_sub_tl(cpu_cc_src
, s
->T0
, s
->tmp0
);
5242 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
5245 set_cc_op(s
, CC_OP_MULB
+ ot
);
5248 case 0x1c1: /* xadd Ev, Gv */
5249 ot
= mo_b_d(b
, dflag
);
5250 modrm
= x86_ldub_code(env
, s
);
5251 reg
= ((modrm
>> 3) & 7) | rex_r
;
5252 mod
= (modrm
>> 6) & 3;
5253 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
5255 rm
= (modrm
& 7) | REX_B(s
);
5256 gen_op_mov_v_reg(s
, ot
, s
->T1
, rm
);
5257 tcg_gen_add_tl(s
->T0
, s
->T0
, s
->T1
);
5258 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
5259 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
5261 gen_lea_modrm(env
, s
, modrm
);
5262 if (s
->prefix
& PREFIX_LOCK
) {
5263 tcg_gen_atomic_fetch_add_tl(s
->T1
, s
->A0
, s
->T0
,
5264 s
->mem_index
, ot
| MO_LE
);
5265 tcg_gen_add_tl(s
->T0
, s
->T0
, s
->T1
);
5267 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
5268 tcg_gen_add_tl(s
->T0
, s
->T0
, s
->T1
);
5269 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
5271 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
5273 gen_op_update2_cc(s
);
5274 set_cc_op(s
, CC_OP_ADDB
+ ot
);
5277 case 0x1b1: /* cmpxchg Ev, Gv */
5279 TCGv oldv
, newv
, cmpv
;
5281 ot
= mo_b_d(b
, dflag
);
5282 modrm
= x86_ldub_code(env
, s
);
5283 reg
= ((modrm
>> 3) & 7) | rex_r
;
5284 mod
= (modrm
>> 6) & 3;
5285 oldv
= tcg_temp_new();
5286 newv
= tcg_temp_new();
5287 cmpv
= tcg_temp_new();
5288 gen_op_mov_v_reg(s
, ot
, newv
, reg
);
5289 tcg_gen_mov_tl(cmpv
, cpu_regs
[R_EAX
]);
5291 if (s
->prefix
& PREFIX_LOCK
) {
5295 gen_lea_modrm(env
, s
, modrm
);
5296 tcg_gen_atomic_cmpxchg_tl(oldv
, s
->A0
, cmpv
, newv
,
5297 s
->mem_index
, ot
| MO_LE
);
5298 gen_op_mov_reg_v(s
, ot
, R_EAX
, oldv
);
5301 rm
= (modrm
& 7) | REX_B(s
);
5302 gen_op_mov_v_reg(s
, ot
, oldv
, rm
);
5304 gen_lea_modrm(env
, s
, modrm
);
5305 gen_op_ld_v(s
, ot
, oldv
, s
->A0
);
5306 rm
= 0; /* avoid warning */
5310 /* store value = (old == cmp ? new : old); */
5311 tcg_gen_movcond_tl(TCG_COND_EQ
, newv
, oldv
, cmpv
, newv
, oldv
);
5313 gen_op_mov_reg_v(s
, ot
, R_EAX
, oldv
);
5314 gen_op_mov_reg_v(s
, ot
, rm
, newv
);
5316 /* Perform an unconditional store cycle like physical cpu;
5317 must be before changing accumulator to ensure
5318 idempotency if the store faults and the instruction
5320 gen_op_st_v(s
, ot
, newv
, s
->A0
);
5321 gen_op_mov_reg_v(s
, ot
, R_EAX
, oldv
);
5324 tcg_gen_mov_tl(cpu_cc_src
, oldv
);
5325 tcg_gen_mov_tl(s
->cc_srcT
, cmpv
);
5326 tcg_gen_sub_tl(cpu_cc_dst
, cmpv
, oldv
);
5327 set_cc_op(s
, CC_OP_SUBB
+ ot
);
5328 tcg_temp_free(oldv
);
5329 tcg_temp_free(newv
);
5330 tcg_temp_free(cmpv
);
5333 case 0x1c7: /* cmpxchg8b */
5334 modrm
= x86_ldub_code(env
, s
);
5335 mod
= (modrm
>> 6) & 3;
5336 switch ((modrm
>> 3) & 7) {
5337 case 1: /* CMPXCHG8, CMPXCHG16 */
5341 #ifdef TARGET_X86_64
5342 if (dflag
== MO_64
) {
5343 if (!(s
->cpuid_ext_features
& CPUID_EXT_CX16
)) {
5346 gen_lea_modrm(env
, s
, modrm
);
5347 if ((s
->prefix
& PREFIX_LOCK
) &&
5348 (tb_cflags(s
->base
.tb
) & CF_PARALLEL
)) {
5349 gen_helper_cmpxchg16b(cpu_env
, s
->A0
);
5351 gen_helper_cmpxchg16b_unlocked(cpu_env
, s
->A0
);
5353 set_cc_op(s
, CC_OP_EFLAGS
);
5357 if (!(s
->cpuid_features
& CPUID_CX8
)) {
5360 gen_lea_modrm(env
, s
, modrm
);
5361 if ((s
->prefix
& PREFIX_LOCK
) &&
5362 (tb_cflags(s
->base
.tb
) & CF_PARALLEL
)) {
5363 gen_helper_cmpxchg8b(cpu_env
, s
->A0
);
5365 gen_helper_cmpxchg8b_unlocked(cpu_env
, s
->A0
);
5367 set_cc_op(s
, CC_OP_EFLAGS
);
5370 case 7: /* RDSEED */
5371 case 6: /* RDRAND */
5373 (s
->prefix
& (PREFIX_LOCK
| PREFIX_REPZ
| PREFIX_REPNZ
)) ||
5374 !(s
->cpuid_ext_features
& CPUID_EXT_RDRAND
)) {
5377 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
5380 gen_helper_rdrand(s
->T0
, cpu_env
);
5381 rm
= (modrm
& 7) | REX_B(s
);
5382 gen_op_mov_reg_v(s
, dflag
, rm
, s
->T0
);
5383 set_cc_op(s
, CC_OP_EFLAGS
);
5384 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
5385 gen_jmp(s
, s
->pc
- s
->cs_base
);
5394 /**************************/
5396 case 0x50 ... 0x57: /* push */
5397 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, (b
& 7) | REX_B(s
));
5398 gen_push_v(s
, s
->T0
);
5400 case 0x58 ... 0x5f: /* pop */
5402 /* NOTE: order is important for pop %sp */
5403 gen_pop_update(s
, ot
);
5404 gen_op_mov_reg_v(s
, ot
, (b
& 7) | REX_B(s
), s
->T0
);
5406 case 0x60: /* pusha */
5411 case 0x61: /* popa */
5416 case 0x68: /* push Iv */
5418 ot
= mo_pushpop(s
, dflag
);
5420 val
= insn_get(env
, s
, ot
);
5422 val
= (int8_t)insn_get(env
, s
, MO_8
);
5423 tcg_gen_movi_tl(s
->T0
, val
);
5424 gen_push_v(s
, s
->T0
);
5426 case 0x8f: /* pop Ev */
5427 modrm
= x86_ldub_code(env
, s
);
5428 mod
= (modrm
>> 6) & 3;
5431 /* NOTE: order is important for pop %sp */
5432 gen_pop_update(s
, ot
);
5433 rm
= (modrm
& 7) | REX_B(s
);
5434 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
5436 /* NOTE: order is important too for MMU exceptions */
5437 s
->popl_esp_hack
= 1 << ot
;
5438 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
5439 s
->popl_esp_hack
= 0;
5440 gen_pop_update(s
, ot
);
5443 case 0xc8: /* enter */
5446 val
= x86_lduw_code(env
, s
);
5447 level
= x86_ldub_code(env
, s
);
5448 gen_enter(s
, val
, level
);
5451 case 0xc9: /* leave */
5454 case 0x06: /* push es */
5455 case 0x0e: /* push cs */
5456 case 0x16: /* push ss */
5457 case 0x1e: /* push ds */
5460 gen_op_movl_T0_seg(s
, b
>> 3);
5461 gen_push_v(s
, s
->T0
);
5463 case 0x1a0: /* push fs */
5464 case 0x1a8: /* push gs */
5465 gen_op_movl_T0_seg(s
, (b
>> 3) & 7);
5466 gen_push_v(s
, s
->T0
);
5468 case 0x07: /* pop es */
5469 case 0x17: /* pop ss */
5470 case 0x1f: /* pop ds */
5475 gen_movl_seg_T0(s
, reg
);
5476 gen_pop_update(s
, ot
);
5477 /* Note that reg == R_SS in gen_movl_seg_T0 always sets is_jmp. */
5478 if (s
->base
.is_jmp
) {
5479 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
5482 gen_eob_inhibit_irq(s
, true);
5488 case 0x1a1: /* pop fs */
5489 case 0x1a9: /* pop gs */
5491 gen_movl_seg_T0(s
, (b
>> 3) & 7);
5492 gen_pop_update(s
, ot
);
5493 if (s
->base
.is_jmp
) {
5494 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
5499 /**************************/
5502 case 0x89: /* mov Gv, Ev */
5503 ot
= mo_b_d(b
, dflag
);
5504 modrm
= x86_ldub_code(env
, s
);
5505 reg
= ((modrm
>> 3) & 7) | rex_r
;
5507 /* generate a generic store */
5508 gen_ldst_modrm(env
, s
, modrm
, ot
, reg
, 1);
5511 case 0xc7: /* mov Ev, Iv */
5512 ot
= mo_b_d(b
, dflag
);
5513 modrm
= x86_ldub_code(env
, s
);
5514 mod
= (modrm
>> 6) & 3;
5516 s
->rip_offset
= insn_const_size(ot
);
5517 gen_lea_modrm(env
, s
, modrm
);
5519 val
= insn_get(env
, s
, ot
);
5520 tcg_gen_movi_tl(s
->T0
, val
);
5522 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
5524 gen_op_mov_reg_v(s
, ot
, (modrm
& 7) | REX_B(s
), s
->T0
);
5528 case 0x8b: /* mov Ev, Gv */
5529 ot
= mo_b_d(b
, dflag
);
5530 modrm
= x86_ldub_code(env
, s
);
5531 reg
= ((modrm
>> 3) & 7) | rex_r
;
5533 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5534 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
5536 case 0x8e: /* mov seg, Gv */
5537 modrm
= x86_ldub_code(env
, s
);
5538 reg
= (modrm
>> 3) & 7;
5539 if (reg
>= 6 || reg
== R_CS
)
5541 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
5542 gen_movl_seg_T0(s
, reg
);
5543 /* Note that reg == R_SS in gen_movl_seg_T0 always sets is_jmp. */
5544 if (s
->base
.is_jmp
) {
5545 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
5548 gen_eob_inhibit_irq(s
, true);
5554 case 0x8c: /* mov Gv, seg */
5555 modrm
= x86_ldub_code(env
, s
);
5556 reg
= (modrm
>> 3) & 7;
5557 mod
= (modrm
>> 6) & 3;
5560 gen_op_movl_T0_seg(s
, reg
);
5561 ot
= mod
== 3 ? dflag
: MO_16
;
5562 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
5565 case 0x1b6: /* movzbS Gv, Eb */
5566 case 0x1b7: /* movzwS Gv, Eb */
5567 case 0x1be: /* movsbS Gv, Eb */
5568 case 0x1bf: /* movswS Gv, Eb */
5573 /* d_ot is the size of destination */
5575 /* ot is the size of source */
5576 ot
= (b
& 1) + MO_8
;
5577 /* s_ot is the sign+size of source */
5578 s_ot
= b
& 8 ? MO_SIGN
| ot
: ot
;
5580 modrm
= x86_ldub_code(env
, s
);
5581 reg
= ((modrm
>> 3) & 7) | rex_r
;
5582 mod
= (modrm
>> 6) & 3;
5583 rm
= (modrm
& 7) | REX_B(s
);
5586 if (s_ot
== MO_SB
&& byte_reg_is_xH(s
, rm
)) {
5587 tcg_gen_sextract_tl(s
->T0
, cpu_regs
[rm
- 4], 8, 8);
5589 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
5592 tcg_gen_ext8u_tl(s
->T0
, s
->T0
);
5595 tcg_gen_ext8s_tl(s
->T0
, s
->T0
);
5598 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
5602 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
5606 gen_op_mov_reg_v(s
, d_ot
, reg
, s
->T0
);
5608 gen_lea_modrm(env
, s
, modrm
);
5609 gen_op_ld_v(s
, s_ot
, s
->T0
, s
->A0
);
5610 gen_op_mov_reg_v(s
, d_ot
, reg
, s
->T0
);
5615 case 0x8d: /* lea */
5616 modrm
= x86_ldub_code(env
, s
);
5617 mod
= (modrm
>> 6) & 3;
5620 reg
= ((modrm
>> 3) & 7) | rex_r
;
5622 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
5623 TCGv ea
= gen_lea_modrm_1(s
, a
);
5624 gen_lea_v_seg(s
, s
->aflag
, ea
, -1, -1);
5625 gen_op_mov_reg_v(s
, dflag
, reg
, s
->A0
);
5629 case 0xa0: /* mov EAX, Ov */
5631 case 0xa2: /* mov Ov, EAX */
5634 target_ulong offset_addr
;
5636 ot
= mo_b_d(b
, dflag
);
5638 #ifdef TARGET_X86_64
5640 offset_addr
= x86_ldq_code(env
, s
);
5644 offset_addr
= insn_get(env
, s
, s
->aflag
);
5647 tcg_gen_movi_tl(s
->A0
, offset_addr
);
5648 gen_add_A0_ds_seg(s
);
5650 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
5651 gen_op_mov_reg_v(s
, ot
, R_EAX
, s
->T0
);
5653 gen_op_mov_v_reg(s
, ot
, s
->T0
, R_EAX
);
5654 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
5658 case 0xd7: /* xlat */
5659 tcg_gen_mov_tl(s
->A0
, cpu_regs
[R_EBX
]);
5660 tcg_gen_ext8u_tl(s
->T0
, cpu_regs
[R_EAX
]);
5661 tcg_gen_add_tl(s
->A0
, s
->A0
, s
->T0
);
5662 gen_extu(s
->aflag
, s
->A0
);
5663 gen_add_A0_ds_seg(s
);
5664 gen_op_ld_v(s
, MO_8
, s
->T0
, s
->A0
);
5665 gen_op_mov_reg_v(s
, MO_8
, R_EAX
, s
->T0
);
5667 case 0xb0 ... 0xb7: /* mov R, Ib */
5668 val
= insn_get(env
, s
, MO_8
);
5669 tcg_gen_movi_tl(s
->T0
, val
);
5670 gen_op_mov_reg_v(s
, MO_8
, (b
& 7) | REX_B(s
), s
->T0
);
5672 case 0xb8 ... 0xbf: /* mov R, Iv */
5673 #ifdef TARGET_X86_64
5674 if (dflag
== MO_64
) {
5677 tmp
= x86_ldq_code(env
, s
);
5678 reg
= (b
& 7) | REX_B(s
);
5679 tcg_gen_movi_tl(s
->T0
, tmp
);
5680 gen_op_mov_reg_v(s
, MO_64
, reg
, s
->T0
);
5685 val
= insn_get(env
, s
, ot
);
5686 reg
= (b
& 7) | REX_B(s
);
5687 tcg_gen_movi_tl(s
->T0
, val
);
5688 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
5692 case 0x91 ... 0x97: /* xchg R, EAX */
5695 reg
= (b
& 7) | REX_B(s
);
5699 case 0x87: /* xchg Ev, Gv */
5700 ot
= mo_b_d(b
, dflag
);
5701 modrm
= x86_ldub_code(env
, s
);
5702 reg
= ((modrm
>> 3) & 7) | rex_r
;
5703 mod
= (modrm
>> 6) & 3;
5705 rm
= (modrm
& 7) | REX_B(s
);
5707 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
5708 gen_op_mov_v_reg(s
, ot
, s
->T1
, rm
);
5709 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
5710 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
5712 gen_lea_modrm(env
, s
, modrm
);
5713 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
5714 /* for xchg, lock is implicit */
5715 tcg_gen_atomic_xchg_tl(s
->T1
, s
->A0
, s
->T0
,
5716 s
->mem_index
, ot
| MO_LE
);
5717 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
5720 case 0xc4: /* les Gv */
5721 /* In CODE64 this is VEX3; see above. */
5724 case 0xc5: /* lds Gv */
5725 /* In CODE64 this is VEX2; see above. */
5728 case 0x1b2: /* lss Gv */
5731 case 0x1b4: /* lfs Gv */
5734 case 0x1b5: /* lgs Gv */
5737 ot
= dflag
!= MO_16
? MO_32
: MO_16
;
5738 modrm
= x86_ldub_code(env
, s
);
5739 reg
= ((modrm
>> 3) & 7) | rex_r
;
5740 mod
= (modrm
>> 6) & 3;
5743 gen_lea_modrm(env
, s
, modrm
);
5744 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
5745 gen_add_A0_im(s
, 1 << ot
);
5746 /* load the segment first to handle exceptions properly */
5747 gen_op_ld_v(s
, MO_16
, s
->T0
, s
->A0
);
5748 gen_movl_seg_T0(s
, op
);
5749 /* then put the data */
5750 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
5751 if (s
->base
.is_jmp
) {
5752 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
5757 /************************/
5765 ot
= mo_b_d(b
, dflag
);
5766 modrm
= x86_ldub_code(env
, s
);
5767 mod
= (modrm
>> 6) & 3;
5768 op
= (modrm
>> 3) & 7;
5774 gen_lea_modrm(env
, s
, modrm
);
5777 opreg
= (modrm
& 7) | REX_B(s
);
5782 gen_shift(s
, op
, ot
, opreg
, OR_ECX
);
5785 shift
= x86_ldub_code(env
, s
);
5787 gen_shifti(s
, op
, ot
, opreg
, shift
);
5802 case 0x1a4: /* shld imm */
5806 case 0x1a5: /* shld cl */
5810 case 0x1ac: /* shrd imm */
5814 case 0x1ad: /* shrd cl */
5819 modrm
= x86_ldub_code(env
, s
);
5820 mod
= (modrm
>> 6) & 3;
5821 rm
= (modrm
& 7) | REX_B(s
);
5822 reg
= ((modrm
>> 3) & 7) | rex_r
;
5824 gen_lea_modrm(env
, s
, modrm
);
5829 gen_op_mov_v_reg(s
, ot
, s
->T1
, reg
);
5832 TCGv imm
= tcg_const_tl(x86_ldub_code(env
, s
));
5833 gen_shiftd_rm_T1(s
, ot
, opreg
, op
, imm
);
5836 gen_shiftd_rm_T1(s
, ot
, opreg
, op
, cpu_regs
[R_ECX
]);
5840 /************************/
5843 if (s
->flags
& (HF_EM_MASK
| HF_TS_MASK
)) {
5844 /* if CR0.EM or CR0.TS are set, generate an FPU exception */
5845 /* XXX: what to do if illegal op ? */
5846 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
5849 modrm
= x86_ldub_code(env
, s
);
5850 mod
= (modrm
>> 6) & 3;
5852 op
= ((b
& 7) << 3) | ((modrm
>> 3) & 7);
5855 gen_lea_modrm(env
, s
, modrm
);
5857 case 0x00 ... 0x07: /* fxxxs */
5858 case 0x10 ... 0x17: /* fixxxl */
5859 case 0x20 ... 0x27: /* fxxxl */
5860 case 0x30 ... 0x37: /* fixxx */
5867 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
5868 s
->mem_index
, MO_LEUL
);
5869 gen_helper_flds_FT0(cpu_env
, s
->tmp2_i32
);
5872 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
5873 s
->mem_index
, MO_LEUL
);
5874 gen_helper_fildl_FT0(cpu_env
, s
->tmp2_i32
);
5877 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
,
5878 s
->mem_index
, MO_LEQ
);
5879 gen_helper_fldl_FT0(cpu_env
, s
->tmp1_i64
);
5883 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
5884 s
->mem_index
, MO_LESW
);
5885 gen_helper_fildl_FT0(cpu_env
, s
->tmp2_i32
);
5889 gen_helper_fp_arith_ST0_FT0(op1
);
5891 /* fcomp needs pop */
5892 gen_helper_fpop(cpu_env
);
5896 case 0x08: /* flds */
5897 case 0x0a: /* fsts */
5898 case 0x0b: /* fstps */
5899 case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
5900 case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
5901 case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
5906 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
5907 s
->mem_index
, MO_LEUL
);
5908 gen_helper_flds_ST0(cpu_env
, s
->tmp2_i32
);
5911 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
5912 s
->mem_index
, MO_LEUL
);
5913 gen_helper_fildl_ST0(cpu_env
, s
->tmp2_i32
);
5916 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
,
5917 s
->mem_index
, MO_LEQ
);
5918 gen_helper_fldl_ST0(cpu_env
, s
->tmp1_i64
);
5922 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
5923 s
->mem_index
, MO_LESW
);
5924 gen_helper_fildl_ST0(cpu_env
, s
->tmp2_i32
);
5929 /* XXX: the corresponding CPUID bit must be tested ! */
5932 gen_helper_fisttl_ST0(s
->tmp2_i32
, cpu_env
);
5933 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
5934 s
->mem_index
, MO_LEUL
);
5937 gen_helper_fisttll_ST0(s
->tmp1_i64
, cpu_env
);
5938 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
,
5939 s
->mem_index
, MO_LEQ
);
5943 gen_helper_fistt_ST0(s
->tmp2_i32
, cpu_env
);
5944 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
5945 s
->mem_index
, MO_LEUW
);
5948 gen_helper_fpop(cpu_env
);
5953 gen_helper_fsts_ST0(s
->tmp2_i32
, cpu_env
);
5954 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
5955 s
->mem_index
, MO_LEUL
);
5958 gen_helper_fistl_ST0(s
->tmp2_i32
, cpu_env
);
5959 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
5960 s
->mem_index
, MO_LEUL
);
5963 gen_helper_fstl_ST0(s
->tmp1_i64
, cpu_env
);
5964 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
,
5965 s
->mem_index
, MO_LEQ
);
5969 gen_helper_fist_ST0(s
->tmp2_i32
, cpu_env
);
5970 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
5971 s
->mem_index
, MO_LEUW
);
5975 gen_helper_fpop(cpu_env
);
5979 case 0x0c: /* fldenv mem */
5980 gen_helper_fldenv(cpu_env
, s
->A0
, tcg_const_i32(dflag
- 1));
5982 case 0x0d: /* fldcw mem */
5983 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
5984 s
->mem_index
, MO_LEUW
);
5985 gen_helper_fldcw(cpu_env
, s
->tmp2_i32
);
5987 case 0x0e: /* fnstenv mem */
5988 gen_helper_fstenv(cpu_env
, s
->A0
, tcg_const_i32(dflag
- 1));
5990 case 0x0f: /* fnstcw mem */
5991 gen_helper_fnstcw(s
->tmp2_i32
, cpu_env
);
5992 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
5993 s
->mem_index
, MO_LEUW
);
5995 case 0x1d: /* fldt mem */
5996 gen_helper_fldt_ST0(cpu_env
, s
->A0
);
5998 case 0x1f: /* fstpt mem */
5999 gen_helper_fstt_ST0(cpu_env
, s
->A0
);
6000 gen_helper_fpop(cpu_env
);
6002 case 0x2c: /* frstor mem */
6003 gen_helper_frstor(cpu_env
, s
->A0
, tcg_const_i32(dflag
- 1));
6005 case 0x2e: /* fnsave mem */
6006 gen_helper_fsave(cpu_env
, s
->A0
, tcg_const_i32(dflag
- 1));
6008 case 0x2f: /* fnstsw mem */
6009 gen_helper_fnstsw(s
->tmp2_i32
, cpu_env
);
6010 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
6011 s
->mem_index
, MO_LEUW
);
6013 case 0x3c: /* fbld */
6014 gen_helper_fbld_ST0(cpu_env
, s
->A0
);
6016 case 0x3e: /* fbstp */
6017 gen_helper_fbst_ST0(cpu_env
, s
->A0
);
6018 gen_helper_fpop(cpu_env
);
6020 case 0x3d: /* fildll */
6021 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
, s
->mem_index
, MO_LEQ
);
6022 gen_helper_fildll_ST0(cpu_env
, s
->tmp1_i64
);
6024 case 0x3f: /* fistpll */
6025 gen_helper_fistll_ST0(s
->tmp1_i64
, cpu_env
);
6026 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
, s
->mem_index
, MO_LEQ
);
6027 gen_helper_fpop(cpu_env
);
6033 /* register float ops */
6037 case 0x08: /* fld sti */
6038 gen_helper_fpush(cpu_env
);
6039 gen_helper_fmov_ST0_STN(cpu_env
,
6040 tcg_const_i32((opreg
+ 1) & 7));
6042 case 0x09: /* fxchg sti */
6043 case 0x29: /* fxchg4 sti, undocumented op */
6044 case 0x39: /* fxchg7 sti, undocumented op */
6045 gen_helper_fxchg_ST0_STN(cpu_env
, tcg_const_i32(opreg
));
6047 case 0x0a: /* grp d9/2 */
6050 /* check exceptions (FreeBSD FPU probe) */
6051 gen_helper_fwait(cpu_env
);
6057 case 0x0c: /* grp d9/4 */
6060 gen_helper_fchs_ST0(cpu_env
);
6063 gen_helper_fabs_ST0(cpu_env
);
6066 gen_helper_fldz_FT0(cpu_env
);
6067 gen_helper_fcom_ST0_FT0(cpu_env
);
6070 gen_helper_fxam_ST0(cpu_env
);
6076 case 0x0d: /* grp d9/5 */
6080 gen_helper_fpush(cpu_env
);
6081 gen_helper_fld1_ST0(cpu_env
);
6084 gen_helper_fpush(cpu_env
);
6085 gen_helper_fldl2t_ST0(cpu_env
);
6088 gen_helper_fpush(cpu_env
);
6089 gen_helper_fldl2e_ST0(cpu_env
);
6092 gen_helper_fpush(cpu_env
);
6093 gen_helper_fldpi_ST0(cpu_env
);
6096 gen_helper_fpush(cpu_env
);
6097 gen_helper_fldlg2_ST0(cpu_env
);
6100 gen_helper_fpush(cpu_env
);
6101 gen_helper_fldln2_ST0(cpu_env
);
6104 gen_helper_fpush(cpu_env
);
6105 gen_helper_fldz_ST0(cpu_env
);
6112 case 0x0e: /* grp d9/6 */
6115 gen_helper_f2xm1(cpu_env
);
6118 gen_helper_fyl2x(cpu_env
);
6121 gen_helper_fptan(cpu_env
);
6123 case 3: /* fpatan */
6124 gen_helper_fpatan(cpu_env
);
6126 case 4: /* fxtract */
6127 gen_helper_fxtract(cpu_env
);
6129 case 5: /* fprem1 */
6130 gen_helper_fprem1(cpu_env
);
6132 case 6: /* fdecstp */
6133 gen_helper_fdecstp(cpu_env
);
6136 case 7: /* fincstp */
6137 gen_helper_fincstp(cpu_env
);
6141 case 0x0f: /* grp d9/7 */
6144 gen_helper_fprem(cpu_env
);
6146 case 1: /* fyl2xp1 */
6147 gen_helper_fyl2xp1(cpu_env
);
6150 gen_helper_fsqrt(cpu_env
);
6152 case 3: /* fsincos */
6153 gen_helper_fsincos(cpu_env
);
6155 case 5: /* fscale */
6156 gen_helper_fscale(cpu_env
);
6158 case 4: /* frndint */
6159 gen_helper_frndint(cpu_env
);
6162 gen_helper_fsin(cpu_env
);
6166 gen_helper_fcos(cpu_env
);
6170 case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
6171 case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
6172 case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
6178 gen_helper_fp_arith_STN_ST0(op1
, opreg
);
6180 gen_helper_fpop(cpu_env
);
6182 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6183 gen_helper_fp_arith_ST0_FT0(op1
);
6187 case 0x02: /* fcom */
6188 case 0x22: /* fcom2, undocumented op */
6189 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6190 gen_helper_fcom_ST0_FT0(cpu_env
);
6192 case 0x03: /* fcomp */
6193 case 0x23: /* fcomp3, undocumented op */
6194 case 0x32: /* fcomp5, undocumented op */
6195 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6196 gen_helper_fcom_ST0_FT0(cpu_env
);
6197 gen_helper_fpop(cpu_env
);
6199 case 0x15: /* da/5 */
6201 case 1: /* fucompp */
6202 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(1));
6203 gen_helper_fucom_ST0_FT0(cpu_env
);
6204 gen_helper_fpop(cpu_env
);
6205 gen_helper_fpop(cpu_env
);
6213 case 0: /* feni (287 only, just do nop here) */
6215 case 1: /* fdisi (287 only, just do nop here) */
6218 gen_helper_fclex(cpu_env
);
6220 case 3: /* fninit */
6221 gen_helper_fninit(cpu_env
);
6223 case 4: /* fsetpm (287 only, just do nop here) */
6229 case 0x1d: /* fucomi */
6230 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6233 gen_update_cc_op(s
);
6234 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6235 gen_helper_fucomi_ST0_FT0(cpu_env
);
6236 set_cc_op(s
, CC_OP_EFLAGS
);
6238 case 0x1e: /* fcomi */
6239 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6242 gen_update_cc_op(s
);
6243 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6244 gen_helper_fcomi_ST0_FT0(cpu_env
);
6245 set_cc_op(s
, CC_OP_EFLAGS
);
6247 case 0x28: /* ffree sti */
6248 gen_helper_ffree_STN(cpu_env
, tcg_const_i32(opreg
));
6250 case 0x2a: /* fst sti */
6251 gen_helper_fmov_STN_ST0(cpu_env
, tcg_const_i32(opreg
));
6253 case 0x2b: /* fstp sti */
6254 case 0x0b: /* fstp1 sti, undocumented op */
6255 case 0x3a: /* fstp8 sti, undocumented op */
6256 case 0x3b: /* fstp9 sti, undocumented op */
6257 gen_helper_fmov_STN_ST0(cpu_env
, tcg_const_i32(opreg
));
6258 gen_helper_fpop(cpu_env
);
6260 case 0x2c: /* fucom st(i) */
6261 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6262 gen_helper_fucom_ST0_FT0(cpu_env
);
6264 case 0x2d: /* fucomp st(i) */
6265 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6266 gen_helper_fucom_ST0_FT0(cpu_env
);
6267 gen_helper_fpop(cpu_env
);
6269 case 0x33: /* de/3 */
6271 case 1: /* fcompp */
6272 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(1));
6273 gen_helper_fcom_ST0_FT0(cpu_env
);
6274 gen_helper_fpop(cpu_env
);
6275 gen_helper_fpop(cpu_env
);
6281 case 0x38: /* ffreep sti, undocumented op */
6282 gen_helper_ffree_STN(cpu_env
, tcg_const_i32(opreg
));
6283 gen_helper_fpop(cpu_env
);
6285 case 0x3c: /* df/4 */
6288 gen_helper_fnstsw(s
->tmp2_i32
, cpu_env
);
6289 tcg_gen_extu_i32_tl(s
->T0
, s
->tmp2_i32
);
6290 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
6296 case 0x3d: /* fucomip */
6297 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6300 gen_update_cc_op(s
);
6301 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6302 gen_helper_fucomi_ST0_FT0(cpu_env
);
6303 gen_helper_fpop(cpu_env
);
6304 set_cc_op(s
, CC_OP_EFLAGS
);
6306 case 0x3e: /* fcomip */
6307 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6310 gen_update_cc_op(s
);
6311 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6312 gen_helper_fcomi_ST0_FT0(cpu_env
);
6313 gen_helper_fpop(cpu_env
);
6314 set_cc_op(s
, CC_OP_EFLAGS
);
6316 case 0x10 ... 0x13: /* fcmovxx */
6321 static const uint8_t fcmov_cc
[8] = {
6328 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6331 op1
= fcmov_cc
[op
& 3] | (((op
>> 3) & 1) ^ 1);
6332 l1
= gen_new_label();
6333 gen_jcc1_noeob(s
, op1
, l1
);
6334 gen_helper_fmov_ST0_STN(cpu_env
, tcg_const_i32(opreg
));
6343 /************************/
6346 case 0xa4: /* movsS */
6348 ot
= mo_b_d(b
, dflag
);
6349 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6350 gen_repz_movs(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6356 case 0xaa: /* stosS */
6358 ot
= mo_b_d(b
, dflag
);
6359 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6360 gen_repz_stos(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6365 case 0xac: /* lodsS */
6367 ot
= mo_b_d(b
, dflag
);
6368 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6369 gen_repz_lods(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6374 case 0xae: /* scasS */
6376 ot
= mo_b_d(b
, dflag
);
6377 if (prefixes
& PREFIX_REPNZ
) {
6378 gen_repz_scas(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 1);
6379 } else if (prefixes
& PREFIX_REPZ
) {
6380 gen_repz_scas(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 0);
6386 case 0xa6: /* cmpsS */
6388 ot
= mo_b_d(b
, dflag
);
6389 if (prefixes
& PREFIX_REPNZ
) {
6390 gen_repz_cmps(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 1);
6391 } else if (prefixes
& PREFIX_REPZ
) {
6392 gen_repz_cmps(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 0);
6397 case 0x6c: /* insS */
6399 ot
= mo_b_d32(b
, dflag
);
6400 tcg_gen_ext16u_tl(s
->T0
, cpu_regs
[R_EDX
]);
6401 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6402 SVM_IOIO_TYPE_MASK
| svm_is_rep(prefixes
) | 4);
6403 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6404 gen_repz_ins(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6407 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6408 gen_jmp(s
, s
->pc
- s
->cs_base
);
6412 case 0x6e: /* outsS */
6414 ot
= mo_b_d32(b
, dflag
);
6415 tcg_gen_ext16u_tl(s
->T0
, cpu_regs
[R_EDX
]);
6416 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6417 svm_is_rep(prefixes
) | 4);
6418 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6419 gen_repz_outs(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6422 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6423 gen_jmp(s
, s
->pc
- s
->cs_base
);
6428 /************************/
6433 ot
= mo_b_d32(b
, dflag
);
6434 val
= x86_ldub_code(env
, s
);
6435 tcg_gen_movi_tl(s
->T0
, val
);
6436 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6437 SVM_IOIO_TYPE_MASK
| svm_is_rep(prefixes
));
6438 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6441 tcg_gen_movi_i32(s
->tmp2_i32
, val
);
6442 gen_helper_in_func(ot
, s
->T1
, s
->tmp2_i32
);
6443 gen_op_mov_reg_v(s
, ot
, R_EAX
, s
->T1
);
6444 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
6445 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6446 gen_jmp(s
, s
->pc
- s
->cs_base
);
6451 ot
= mo_b_d32(b
, dflag
);
6452 val
= x86_ldub_code(env
, s
);
6453 tcg_gen_movi_tl(s
->T0
, val
);
6454 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6455 svm_is_rep(prefixes
));
6456 gen_op_mov_v_reg(s
, ot
, s
->T1
, R_EAX
);
6458 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6461 tcg_gen_movi_i32(s
->tmp2_i32
, val
);
6462 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T1
);
6463 gen_helper_out_func(ot
, s
->tmp2_i32
, s
->tmp3_i32
);
6464 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
6465 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6466 gen_jmp(s
, s
->pc
- s
->cs_base
);
6471 ot
= mo_b_d32(b
, dflag
);
6472 tcg_gen_ext16u_tl(s
->T0
, cpu_regs
[R_EDX
]);
6473 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6474 SVM_IOIO_TYPE_MASK
| svm_is_rep(prefixes
));
6475 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6478 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
6479 gen_helper_in_func(ot
, s
->T1
, s
->tmp2_i32
);
6480 gen_op_mov_reg_v(s
, ot
, R_EAX
, s
->T1
);
6481 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
6482 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6483 gen_jmp(s
, s
->pc
- s
->cs_base
);
6488 ot
= mo_b_d32(b
, dflag
);
6489 tcg_gen_ext16u_tl(s
->T0
, cpu_regs
[R_EDX
]);
6490 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6491 svm_is_rep(prefixes
));
6492 gen_op_mov_v_reg(s
, ot
, s
->T1
, R_EAX
);
6494 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6497 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
6498 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T1
);
6499 gen_helper_out_func(ot
, s
->tmp2_i32
, s
->tmp3_i32
);
6500 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
6501 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6502 gen_jmp(s
, s
->pc
- s
->cs_base
);
6506 /************************/
6508 case 0xc2: /* ret im */
6509 val
= x86_ldsw_code(env
, s
);
6511 gen_stack_update(s
, val
+ (1 << ot
));
6512 /* Note that gen_pop_T0 uses a zero-extending load. */
6513 gen_op_jmp_v(s
->T0
);
6517 case 0xc3: /* ret */
6519 gen_pop_update(s
, ot
);
6520 /* Note that gen_pop_T0 uses a zero-extending load. */
6521 gen_op_jmp_v(s
->T0
);
6525 case 0xca: /* lret im */
6526 val
= x86_ldsw_code(env
, s
);
6528 if (s
->pe
&& !s
->vm86
) {
6529 gen_update_cc_op(s
);
6530 gen_jmp_im(s
, pc_start
- s
->cs_base
);
6531 gen_helper_lret_protected(cpu_env
, tcg_const_i32(dflag
- 1),
6532 tcg_const_i32(val
));
6536 gen_op_ld_v(s
, dflag
, s
->T0
, s
->A0
);
6537 /* NOTE: keeping EIP updated is not a problem in case of
6539 gen_op_jmp_v(s
->T0
);
6541 gen_add_A0_im(s
, 1 << dflag
);
6542 gen_op_ld_v(s
, dflag
, s
->T0
, s
->A0
);
6543 gen_op_movl_seg_T0_vm(s
, R_CS
);
6544 /* add stack offset */
6545 gen_stack_update(s
, val
+ (2 << dflag
));
6549 case 0xcb: /* lret */
6552 case 0xcf: /* iret */
6553 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_IRET
);
6556 gen_helper_iret_real(cpu_env
, tcg_const_i32(dflag
- 1));
6557 set_cc_op(s
, CC_OP_EFLAGS
);
6558 } else if (s
->vm86
) {
6560 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6562 gen_helper_iret_real(cpu_env
, tcg_const_i32(dflag
- 1));
6563 set_cc_op(s
, CC_OP_EFLAGS
);
6566 gen_helper_iret_protected(cpu_env
, tcg_const_i32(dflag
- 1),
6567 tcg_const_i32(s
->pc
- s
->cs_base
));
6568 set_cc_op(s
, CC_OP_EFLAGS
);
6572 case 0xe8: /* call im */
6574 if (dflag
!= MO_16
) {
6575 tval
= (int32_t)insn_get(env
, s
, MO_32
);
6577 tval
= (int16_t)insn_get(env
, s
, MO_16
);
6579 next_eip
= s
->pc
- s
->cs_base
;
6581 if (dflag
== MO_16
) {
6583 } else if (!CODE64(s
)) {
6586 tcg_gen_movi_tl(s
->T0
, next_eip
);
6587 gen_push_v(s
, s
->T0
);
6592 case 0x9a: /* lcall im */
6594 unsigned int selector
, offset
;
6599 offset
= insn_get(env
, s
, ot
);
6600 selector
= insn_get(env
, s
, MO_16
);
6602 tcg_gen_movi_tl(s
->T0
, selector
);
6603 tcg_gen_movi_tl(s
->T1
, offset
);
6606 case 0xe9: /* jmp im */
6607 if (dflag
!= MO_16
) {
6608 tval
= (int32_t)insn_get(env
, s
, MO_32
);
6610 tval
= (int16_t)insn_get(env
, s
, MO_16
);
6612 tval
+= s
->pc
- s
->cs_base
;
6613 if (dflag
== MO_16
) {
6615 } else if (!CODE64(s
)) {
6621 case 0xea: /* ljmp im */
6623 unsigned int selector
, offset
;
6628 offset
= insn_get(env
, s
, ot
);
6629 selector
= insn_get(env
, s
, MO_16
);
6631 tcg_gen_movi_tl(s
->T0
, selector
);
6632 tcg_gen_movi_tl(s
->T1
, offset
);
6635 case 0xeb: /* jmp Jb */
6636 tval
= (int8_t)insn_get(env
, s
, MO_8
);
6637 tval
+= s
->pc
- s
->cs_base
;
6638 if (dflag
== MO_16
) {
6643 case 0x70 ... 0x7f: /* jcc Jb */
6644 tval
= (int8_t)insn_get(env
, s
, MO_8
);
6646 case 0x180 ... 0x18f: /* jcc Jv */
6647 if (dflag
!= MO_16
) {
6648 tval
= (int32_t)insn_get(env
, s
, MO_32
);
6650 tval
= (int16_t)insn_get(env
, s
, MO_16
);
6653 next_eip
= s
->pc
- s
->cs_base
;
6655 if (dflag
== MO_16
) {
6659 gen_jcc(s
, b
, tval
, next_eip
);
6662 case 0x190 ... 0x19f: /* setcc Gv */
6663 modrm
= x86_ldub_code(env
, s
);
6664 gen_setcc1(s
, b
, s
->T0
);
6665 gen_ldst_modrm(env
, s
, modrm
, MO_8
, OR_TMP0
, 1);
6667 case 0x140 ... 0x14f: /* cmov Gv, Ev */
6668 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6672 modrm
= x86_ldub_code(env
, s
);
6673 reg
= ((modrm
>> 3) & 7) | rex_r
;
6674 gen_cmovcc1(env
, s
, ot
, b
, modrm
, reg
);
6677 /************************/
6679 case 0x9c: /* pushf */
6680 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_PUSHF
);
6681 if (s
->vm86
&& s
->iopl
!= 3) {
6682 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6684 gen_update_cc_op(s
);
6685 gen_helper_read_eflags(s
->T0
, cpu_env
);
6686 gen_push_v(s
, s
->T0
);
6689 case 0x9d: /* popf */
6690 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_POPF
);
6691 if (s
->vm86
&& s
->iopl
!= 3) {
6692 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6696 if (dflag
!= MO_16
) {
6697 gen_helper_write_eflags(cpu_env
, s
->T0
,
6698 tcg_const_i32((TF_MASK
| AC_MASK
|
6703 gen_helper_write_eflags(cpu_env
, s
->T0
,
6704 tcg_const_i32((TF_MASK
| AC_MASK
|
6706 IF_MASK
| IOPL_MASK
)
6710 if (s
->cpl
<= s
->iopl
) {
6711 if (dflag
!= MO_16
) {
6712 gen_helper_write_eflags(cpu_env
, s
->T0
,
6713 tcg_const_i32((TF_MASK
|
6719 gen_helper_write_eflags(cpu_env
, s
->T0
,
6720 tcg_const_i32((TF_MASK
|
6728 if (dflag
!= MO_16
) {
6729 gen_helper_write_eflags(cpu_env
, s
->T0
,
6730 tcg_const_i32((TF_MASK
| AC_MASK
|
6731 ID_MASK
| NT_MASK
)));
6733 gen_helper_write_eflags(cpu_env
, s
->T0
,
6734 tcg_const_i32((TF_MASK
| AC_MASK
|
6740 gen_pop_update(s
, ot
);
6741 set_cc_op(s
, CC_OP_EFLAGS
);
6742 /* abort translation because TF/AC flag may change */
6743 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
6747 case 0x9e: /* sahf */
6748 if (CODE64(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_LAHF_LM
))
6750 gen_op_mov_v_reg(s
, MO_8
, s
->T0
, R_AH
);
6751 gen_compute_eflags(s
);
6752 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, CC_O
);
6753 tcg_gen_andi_tl(s
->T0
, s
->T0
, CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
6754 tcg_gen_or_tl(cpu_cc_src
, cpu_cc_src
, s
->T0
);
6756 case 0x9f: /* lahf */
6757 if (CODE64(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_LAHF_LM
))
6759 gen_compute_eflags(s
);
6760 /* Note: gen_compute_eflags() only gives the condition codes */
6761 tcg_gen_ori_tl(s
->T0
, cpu_cc_src
, 0x02);
6762 gen_op_mov_reg_v(s
, MO_8
, R_AH
, s
->T0
);
6764 case 0xf5: /* cmc */
6765 gen_compute_eflags(s
);
6766 tcg_gen_xori_tl(cpu_cc_src
, cpu_cc_src
, CC_C
);
6768 case 0xf8: /* clc */
6769 gen_compute_eflags(s
);
6770 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, ~CC_C
);
6772 case 0xf9: /* stc */
6773 gen_compute_eflags(s
);
6774 tcg_gen_ori_tl(cpu_cc_src
, cpu_cc_src
, CC_C
);
6776 case 0xfc: /* cld */
6777 tcg_gen_movi_i32(s
->tmp2_i32
, 1);
6778 tcg_gen_st_i32(s
->tmp2_i32
, cpu_env
, offsetof(CPUX86State
, df
));
6780 case 0xfd: /* std */
6781 tcg_gen_movi_i32(s
->tmp2_i32
, -1);
6782 tcg_gen_st_i32(s
->tmp2_i32
, cpu_env
, offsetof(CPUX86State
, df
));
6785 /************************/
6786 /* bit operations */
6787 case 0x1ba: /* bt/bts/btr/btc Gv, im */
6789 modrm
= x86_ldub_code(env
, s
);
6790 op
= (modrm
>> 3) & 7;
6791 mod
= (modrm
>> 6) & 3;
6792 rm
= (modrm
& 7) | REX_B(s
);
6795 gen_lea_modrm(env
, s
, modrm
);
6796 if (!(s
->prefix
& PREFIX_LOCK
)) {
6797 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
6800 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
6803 val
= x86_ldub_code(env
, s
);
6804 tcg_gen_movi_tl(s
->T1
, val
);
6809 case 0x1a3: /* bt Gv, Ev */
6812 case 0x1ab: /* bts */
6815 case 0x1b3: /* btr */
6818 case 0x1bb: /* btc */
6822 modrm
= x86_ldub_code(env
, s
);
6823 reg
= ((modrm
>> 3) & 7) | rex_r
;
6824 mod
= (modrm
>> 6) & 3;
6825 rm
= (modrm
& 7) | REX_B(s
);
6826 gen_op_mov_v_reg(s
, MO_32
, s
->T1
, reg
);
6828 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
6829 /* specific case: we need to add a displacement */
6830 gen_exts(ot
, s
->T1
);
6831 tcg_gen_sari_tl(s
->tmp0
, s
->T1
, 3 + ot
);
6832 tcg_gen_shli_tl(s
->tmp0
, s
->tmp0
, ot
);
6833 tcg_gen_add_tl(s
->A0
, gen_lea_modrm_1(s
, a
), s
->tmp0
);
6834 gen_lea_v_seg(s
, s
->aflag
, s
->A0
, a
.def_seg
, s
->override
);
6835 if (!(s
->prefix
& PREFIX_LOCK
)) {
6836 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
6839 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
6842 tcg_gen_andi_tl(s
->T1
, s
->T1
, (1 << (3 + ot
)) - 1);
6843 tcg_gen_movi_tl(s
->tmp0
, 1);
6844 tcg_gen_shl_tl(s
->tmp0
, s
->tmp0
, s
->T1
);
6845 if (s
->prefix
& PREFIX_LOCK
) {
6848 /* Needs no atomic ops; we surpressed the normal
6849 memory load for LOCK above so do it now. */
6850 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
6853 tcg_gen_atomic_fetch_or_tl(s
->T0
, s
->A0
, s
->tmp0
,
6854 s
->mem_index
, ot
| MO_LE
);
6857 tcg_gen_not_tl(s
->tmp0
, s
->tmp0
);
6858 tcg_gen_atomic_fetch_and_tl(s
->T0
, s
->A0
, s
->tmp0
,
6859 s
->mem_index
, ot
| MO_LE
);
6863 tcg_gen_atomic_fetch_xor_tl(s
->T0
, s
->A0
, s
->tmp0
,
6864 s
->mem_index
, ot
| MO_LE
);
6867 tcg_gen_shr_tl(s
->tmp4
, s
->T0
, s
->T1
);
6869 tcg_gen_shr_tl(s
->tmp4
, s
->T0
, s
->T1
);
6872 /* Data already loaded; nothing to do. */
6875 tcg_gen_or_tl(s
->T0
, s
->T0
, s
->tmp0
);
6878 tcg_gen_andc_tl(s
->T0
, s
->T0
, s
->tmp0
);
6882 tcg_gen_xor_tl(s
->T0
, s
->T0
, s
->tmp0
);
6887 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
6889 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
6894 /* Delay all CC updates until after the store above. Note that
6895 C is the result of the test, Z is unchanged, and the others
6896 are all undefined. */
6898 case CC_OP_MULB
... CC_OP_MULQ
:
6899 case CC_OP_ADDB
... CC_OP_ADDQ
:
6900 case CC_OP_ADCB
... CC_OP_ADCQ
:
6901 case CC_OP_SUBB
... CC_OP_SUBQ
:
6902 case CC_OP_SBBB
... CC_OP_SBBQ
:
6903 case CC_OP_LOGICB
... CC_OP_LOGICQ
:
6904 case CC_OP_INCB
... CC_OP_INCQ
:
6905 case CC_OP_DECB
... CC_OP_DECQ
:
6906 case CC_OP_SHLB
... CC_OP_SHLQ
:
6907 case CC_OP_SARB
... CC_OP_SARQ
:
6908 case CC_OP_BMILGB
... CC_OP_BMILGQ
:
6909 /* Z was going to be computed from the non-zero status of CC_DST.
6910 We can get that same Z value (and the new C value) by leaving
6911 CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
6913 tcg_gen_mov_tl(cpu_cc_src
, s
->tmp4
);
6914 set_cc_op(s
, ((s
->cc_op
- CC_OP_MULB
) & 3) + CC_OP_SARB
);
6917 /* Otherwise, generate EFLAGS and replace the C bit. */
6918 gen_compute_eflags(s
);
6919 tcg_gen_deposit_tl(cpu_cc_src
, cpu_cc_src
, s
->tmp4
,
6924 case 0x1bc: /* bsf / tzcnt */
6925 case 0x1bd: /* bsr / lzcnt */
6927 modrm
= x86_ldub_code(env
, s
);
6928 reg
= ((modrm
>> 3) & 7) | rex_r
;
6929 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
6930 gen_extu(ot
, s
->T0
);
6932 /* Note that lzcnt and tzcnt are in different extensions. */
6933 if ((prefixes
& PREFIX_REPZ
)
6935 ? s
->cpuid_ext3_features
& CPUID_EXT3_ABM
6936 : s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)) {
6938 /* For lzcnt/tzcnt, C bit is defined related to the input. */
6939 tcg_gen_mov_tl(cpu_cc_src
, s
->T0
);
6941 /* For lzcnt, reduce the target_ulong result by the
6942 number of zeros that we expect to find at the top. */
6943 tcg_gen_clzi_tl(s
->T0
, s
->T0
, TARGET_LONG_BITS
);
6944 tcg_gen_subi_tl(s
->T0
, s
->T0
, TARGET_LONG_BITS
- size
);
6946 /* For tzcnt, a zero input must return the operand size. */
6947 tcg_gen_ctzi_tl(s
->T0
, s
->T0
, size
);
6949 /* For lzcnt/tzcnt, Z bit is defined related to the result. */
6950 gen_op_update1_cc(s
);
6951 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
6953 /* For bsr/bsf, only the Z bit is defined and it is related
6954 to the input and not the result. */
6955 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
6956 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
6958 /* ??? The manual says that the output is undefined when the
6959 input is zero, but real hardware leaves it unchanged, and
6960 real programs appear to depend on that. Accomplish this
6961 by passing the output as the value to return upon zero. */
6963 /* For bsr, return the bit index of the first 1 bit,
6964 not the count of leading zeros. */
6965 tcg_gen_xori_tl(s
->T1
, cpu_regs
[reg
], TARGET_LONG_BITS
- 1);
6966 tcg_gen_clz_tl(s
->T0
, s
->T0
, s
->T1
);
6967 tcg_gen_xori_tl(s
->T0
, s
->T0
, TARGET_LONG_BITS
- 1);
6969 tcg_gen_ctz_tl(s
->T0
, s
->T0
, cpu_regs
[reg
]);
6972 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
6974 /************************/
6976 case 0x27: /* daa */
6979 gen_update_cc_op(s
);
6980 gen_helper_daa(cpu_env
);
6981 set_cc_op(s
, CC_OP_EFLAGS
);
6983 case 0x2f: /* das */
6986 gen_update_cc_op(s
);
6987 gen_helper_das(cpu_env
);
6988 set_cc_op(s
, CC_OP_EFLAGS
);
6990 case 0x37: /* aaa */
6993 gen_update_cc_op(s
);
6994 gen_helper_aaa(cpu_env
);
6995 set_cc_op(s
, CC_OP_EFLAGS
);
6997 case 0x3f: /* aas */
7000 gen_update_cc_op(s
);
7001 gen_helper_aas(cpu_env
);
7002 set_cc_op(s
, CC_OP_EFLAGS
);
7004 case 0xd4: /* aam */
7007 val
= x86_ldub_code(env
, s
);
7009 gen_exception(s
, EXCP00_DIVZ
, pc_start
- s
->cs_base
);
7011 gen_helper_aam(cpu_env
, tcg_const_i32(val
));
7012 set_cc_op(s
, CC_OP_LOGICB
);
7015 case 0xd5: /* aad */
7018 val
= x86_ldub_code(env
, s
);
7019 gen_helper_aad(cpu_env
, tcg_const_i32(val
));
7020 set_cc_op(s
, CC_OP_LOGICB
);
7022 /************************/
7024 case 0x90: /* nop */
7025 /* XXX: correct lock test for all insn */
7026 if (prefixes
& PREFIX_LOCK
) {
7029 /* If REX_B is set, then this is xchg eax, r8d, not a nop. */
7031 goto do_xchg_reg_eax
;
7033 if (prefixes
& PREFIX_REPZ
) {
7034 gen_update_cc_op(s
);
7035 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7036 gen_helper_pause(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7037 s
->base
.is_jmp
= DISAS_NORETURN
;
7040 case 0x9b: /* fwait */
7041 if ((s
->flags
& (HF_MP_MASK
| HF_TS_MASK
)) ==
7042 (HF_MP_MASK
| HF_TS_MASK
)) {
7043 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
7045 gen_helper_fwait(cpu_env
);
7048 case 0xcc: /* int3 */
7049 gen_interrupt(s
, EXCP03_INT3
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
7051 case 0xcd: /* int N */
7052 val
= x86_ldub_code(env
, s
);
7053 if (s
->vm86
&& s
->iopl
!= 3) {
7054 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7056 gen_interrupt(s
, val
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
7059 case 0xce: /* into */
7062 gen_update_cc_op(s
);
7063 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7064 gen_helper_into(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7067 case 0xf1: /* icebp (undocumented, exits to external debugger) */
7068 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_ICEBP
);
7069 gen_debug(s
, pc_start
- s
->cs_base
);
7072 case 0xfa: /* cli */
7074 if (s
->cpl
<= s
->iopl
) {
7075 gen_helper_cli(cpu_env
);
7077 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7081 gen_helper_cli(cpu_env
);
7083 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7087 case 0xfb: /* sti */
7088 if (s
->vm86
? s
->iopl
== 3 : s
->cpl
<= s
->iopl
) {
7089 gen_helper_sti(cpu_env
);
7090 /* interruptions are enabled only the first insn after sti */
7091 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
7092 gen_eob_inhibit_irq(s
, true);
7094 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7097 case 0x62: /* bound */
7101 modrm
= x86_ldub_code(env
, s
);
7102 reg
= (modrm
>> 3) & 7;
7103 mod
= (modrm
>> 6) & 3;
7106 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
7107 gen_lea_modrm(env
, s
, modrm
);
7108 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
7110 gen_helper_boundw(cpu_env
, s
->A0
, s
->tmp2_i32
);
7112 gen_helper_boundl(cpu_env
, s
->A0
, s
->tmp2_i32
);
7115 case 0x1c8 ... 0x1cf: /* bswap reg */
7116 reg
= (b
& 7) | REX_B(s
);
7117 #ifdef TARGET_X86_64
7118 if (dflag
== MO_64
) {
7119 gen_op_mov_v_reg(s
, MO_64
, s
->T0
, reg
);
7120 tcg_gen_bswap64_i64(s
->T0
, s
->T0
);
7121 gen_op_mov_reg_v(s
, MO_64
, reg
, s
->T0
);
7125 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, reg
);
7126 tcg_gen_ext32u_tl(s
->T0
, s
->T0
);
7127 tcg_gen_bswap32_tl(s
->T0
, s
->T0
);
7128 gen_op_mov_reg_v(s
, MO_32
, reg
, s
->T0
);
7131 case 0xd6: /* salc */
7134 gen_compute_eflags_c(s
, s
->T0
);
7135 tcg_gen_neg_tl(s
->T0
, s
->T0
);
7136 gen_op_mov_reg_v(s
, MO_8
, R_EAX
, s
->T0
);
7138 case 0xe0: /* loopnz */
7139 case 0xe1: /* loopz */
7140 case 0xe2: /* loop */
7141 case 0xe3: /* jecxz */
7143 TCGLabel
*l1
, *l2
, *l3
;
7145 tval
= (int8_t)insn_get(env
, s
, MO_8
);
7146 next_eip
= s
->pc
- s
->cs_base
;
7148 if (dflag
== MO_16
) {
7152 l1
= gen_new_label();
7153 l2
= gen_new_label();
7154 l3
= gen_new_label();
7157 case 0: /* loopnz */
7159 gen_op_add_reg_im(s
, s
->aflag
, R_ECX
, -1);
7160 gen_op_jz_ecx(s
, s
->aflag
, l3
);
7161 gen_jcc1(s
, (JCC_Z
<< 1) | (b
^ 1), l1
);
7164 gen_op_add_reg_im(s
, s
->aflag
, R_ECX
, -1);
7165 gen_op_jnz_ecx(s
, s
->aflag
, l1
);
7169 gen_op_jz_ecx(s
, s
->aflag
, l1
);
7174 gen_jmp_im(s
, next_eip
);
7178 gen_jmp_im(s
, tval
);
7183 case 0x130: /* wrmsr */
7184 case 0x132: /* rdmsr */
7186 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7188 gen_update_cc_op(s
);
7189 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7191 gen_helper_rdmsr(cpu_env
);
7193 gen_helper_wrmsr(cpu_env
);
7197 case 0x131: /* rdtsc */
7198 gen_update_cc_op(s
);
7199 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7200 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
7203 gen_helper_rdtsc(cpu_env
);
7204 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
7205 gen_jmp(s
, s
->pc
- s
->cs_base
);
7208 case 0x133: /* rdpmc */
7209 gen_update_cc_op(s
);
7210 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7211 gen_helper_rdpmc(cpu_env
);
7213 case 0x134: /* sysenter */
7214 /* For Intel SYSENTER is valid on 64-bit */
7215 if (CODE64(s
) && env
->cpuid_vendor1
!= CPUID_VENDOR_INTEL_1
)
7218 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7220 gen_helper_sysenter(cpu_env
);
7224 case 0x135: /* sysexit */
7225 /* For Intel SYSEXIT is valid on 64-bit */
7226 if (CODE64(s
) && env
->cpuid_vendor1
!= CPUID_VENDOR_INTEL_1
)
7229 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7231 gen_helper_sysexit(cpu_env
, tcg_const_i32(dflag
- 1));
7235 #ifdef TARGET_X86_64
7236 case 0x105: /* syscall */
7237 /* XXX: is it usable in real mode ? */
7238 gen_update_cc_op(s
);
7239 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7240 gen_helper_syscall(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7241 /* TF handling for the syscall insn is different. The TF bit is checked
7242 after the syscall insn completes. This allows #DB to not be
7243 generated after one has entered CPL0 if TF is set in FMASK. */
7244 gen_eob_worker(s
, false, true);
7246 case 0x107: /* sysret */
7248 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7250 gen_helper_sysret(cpu_env
, tcg_const_i32(dflag
- 1));
7251 /* condition codes are modified only in long mode */
7253 set_cc_op(s
, CC_OP_EFLAGS
);
7255 /* TF handling for the sysret insn is different. The TF bit is
7256 checked after the sysret insn completes. This allows #DB to be
7257 generated "as if" the syscall insn in userspace has just
7259 gen_eob_worker(s
, false, true);
7263 case 0x1a2: /* cpuid */
7264 gen_update_cc_op(s
);
7265 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7266 gen_helper_cpuid(cpu_env
);
7268 case 0xf4: /* hlt */
7270 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7272 gen_update_cc_op(s
);
7273 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7274 gen_helper_hlt(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7275 s
->base
.is_jmp
= DISAS_NORETURN
;
7279 modrm
= x86_ldub_code(env
, s
);
7280 mod
= (modrm
>> 6) & 3;
7281 op
= (modrm
>> 3) & 7;
7284 if (!s
->pe
|| s
->vm86
)
7286 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_LDTR_READ
);
7287 tcg_gen_ld32u_tl(s
->T0
, cpu_env
,
7288 offsetof(CPUX86State
, ldt
.selector
));
7289 ot
= mod
== 3 ? dflag
: MO_16
;
7290 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
7293 if (!s
->pe
|| s
->vm86
)
7296 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7298 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_LDTR_WRITE
);
7299 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7300 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
7301 gen_helper_lldt(cpu_env
, s
->tmp2_i32
);
7305 if (!s
->pe
|| s
->vm86
)
7307 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_TR_READ
);
7308 tcg_gen_ld32u_tl(s
->T0
, cpu_env
,
7309 offsetof(CPUX86State
, tr
.selector
));
7310 ot
= mod
== 3 ? dflag
: MO_16
;
7311 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
7314 if (!s
->pe
|| s
->vm86
)
7317 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7319 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_TR_WRITE
);
7320 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7321 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
7322 gen_helper_ltr(cpu_env
, s
->tmp2_i32
);
7327 if (!s
->pe
|| s
->vm86
)
7329 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7330 gen_update_cc_op(s
);
7332 gen_helper_verr(cpu_env
, s
->T0
);
7334 gen_helper_verw(cpu_env
, s
->T0
);
7336 set_cc_op(s
, CC_OP_EFLAGS
);
7344 modrm
= x86_ldub_code(env
, s
);
7346 CASE_MODRM_MEM_OP(0): /* sgdt */
7347 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_GDTR_READ
);
7348 gen_lea_modrm(env
, s
, modrm
);
7349 tcg_gen_ld32u_tl(s
->T0
,
7350 cpu_env
, offsetof(CPUX86State
, gdt
.limit
));
7351 gen_op_st_v(s
, MO_16
, s
->T0
, s
->A0
);
7352 gen_add_A0_im(s
, 2);
7353 tcg_gen_ld_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, gdt
.base
));
7354 if (dflag
== MO_16
) {
7355 tcg_gen_andi_tl(s
->T0
, s
->T0
, 0xffffff);
7357 gen_op_st_v(s
, CODE64(s
) + MO_32
, s
->T0
, s
->A0
);
7360 case 0xc8: /* monitor */
7361 if (!(s
->cpuid_ext_features
& CPUID_EXT_MONITOR
) || s
->cpl
!= 0) {
7364 gen_update_cc_op(s
);
7365 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7366 tcg_gen_mov_tl(s
->A0
, cpu_regs
[R_EAX
]);
7367 gen_extu(s
->aflag
, s
->A0
);
7368 gen_add_A0_ds_seg(s
);
7369 gen_helper_monitor(cpu_env
, s
->A0
);
7372 case 0xc9: /* mwait */
7373 if (!(s
->cpuid_ext_features
& CPUID_EXT_MONITOR
) || s
->cpl
!= 0) {
7376 gen_update_cc_op(s
);
7377 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7378 gen_helper_mwait(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7382 case 0xca: /* clac */
7383 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_SMAP
)
7387 gen_helper_clac(cpu_env
);
7388 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
7392 case 0xcb: /* stac */
7393 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_SMAP
)
7397 gen_helper_stac(cpu_env
);
7398 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
7402 CASE_MODRM_MEM_OP(1): /* sidt */
7403 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_IDTR_READ
);
7404 gen_lea_modrm(env
, s
, modrm
);
7405 tcg_gen_ld32u_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, idt
.limit
));
7406 gen_op_st_v(s
, MO_16
, s
->T0
, s
->A0
);
7407 gen_add_A0_im(s
, 2);
7408 tcg_gen_ld_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, idt
.base
));
7409 if (dflag
== MO_16
) {
7410 tcg_gen_andi_tl(s
->T0
, s
->T0
, 0xffffff);
7412 gen_op_st_v(s
, CODE64(s
) + MO_32
, s
->T0
, s
->A0
);
7415 case 0xd0: /* xgetbv */
7416 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
7417 || (s
->prefix
& (PREFIX_LOCK
| PREFIX_DATA
7418 | PREFIX_REPZ
| PREFIX_REPNZ
))) {
7421 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_ECX
]);
7422 gen_helper_xgetbv(s
->tmp1_i64
, cpu_env
, s
->tmp2_i32
);
7423 tcg_gen_extr_i64_tl(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
], s
->tmp1_i64
);
7426 case 0xd1: /* xsetbv */
7427 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
7428 || (s
->prefix
& (PREFIX_LOCK
| PREFIX_DATA
7429 | PREFIX_REPZ
| PREFIX_REPNZ
))) {
7433 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7436 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
7438 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_ECX
]);
7439 gen_helper_xsetbv(cpu_env
, s
->tmp2_i32
, s
->tmp1_i64
);
7440 /* End TB because translation flags may change. */
7441 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
7445 case 0xd8: /* VMRUN */
7446 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
) {
7450 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7453 gen_update_cc_op(s
);
7454 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7455 gen_helper_vmrun(cpu_env
, tcg_const_i32(s
->aflag
- 1),
7456 tcg_const_i32(s
->pc
- pc_start
));
7457 tcg_gen_exit_tb(NULL
, 0);
7458 s
->base
.is_jmp
= DISAS_NORETURN
;
7461 case 0xd9: /* VMMCALL */
7462 if (!(s
->flags
& HF_SVME_MASK
)) {
7465 gen_update_cc_op(s
);
7466 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7467 gen_helper_vmmcall(cpu_env
);
7470 case 0xda: /* VMLOAD */
7471 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
) {
7475 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7478 gen_update_cc_op(s
);
7479 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7480 gen_helper_vmload(cpu_env
, tcg_const_i32(s
->aflag
- 1));
7483 case 0xdb: /* VMSAVE */
7484 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
) {
7488 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7491 gen_update_cc_op(s
);
7492 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7493 gen_helper_vmsave(cpu_env
, tcg_const_i32(s
->aflag
- 1));
7496 case 0xdc: /* STGI */
7497 if ((!(s
->flags
& HF_SVME_MASK
)
7498 && !(s
->cpuid_ext3_features
& CPUID_EXT3_SKINIT
))
7503 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7506 gen_update_cc_op(s
);
7507 gen_helper_stgi(cpu_env
);
7508 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
7512 case 0xdd: /* CLGI */
7513 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
) {
7517 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7520 gen_update_cc_op(s
);
7521 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7522 gen_helper_clgi(cpu_env
);
7525 case 0xde: /* SKINIT */
7526 if ((!(s
->flags
& HF_SVME_MASK
)
7527 && !(s
->cpuid_ext3_features
& CPUID_EXT3_SKINIT
))
7531 gen_update_cc_op(s
);
7532 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7533 gen_helper_skinit(cpu_env
);
7536 case 0xdf: /* INVLPGA */
7537 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
) {
7541 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7544 gen_update_cc_op(s
);
7545 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7546 gen_helper_invlpga(cpu_env
, tcg_const_i32(s
->aflag
- 1));
7549 CASE_MODRM_MEM_OP(2): /* lgdt */
7551 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7554 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_GDTR_WRITE
);
7555 gen_lea_modrm(env
, s
, modrm
);
7556 gen_op_ld_v(s
, MO_16
, s
->T1
, s
->A0
);
7557 gen_add_A0_im(s
, 2);
7558 gen_op_ld_v(s
, CODE64(s
) + MO_32
, s
->T0
, s
->A0
);
7559 if (dflag
== MO_16
) {
7560 tcg_gen_andi_tl(s
->T0
, s
->T0
, 0xffffff);
7562 tcg_gen_st_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, gdt
.base
));
7563 tcg_gen_st32_tl(s
->T1
, cpu_env
, offsetof(CPUX86State
, gdt
.limit
));
7566 CASE_MODRM_MEM_OP(3): /* lidt */
7568 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7571 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_IDTR_WRITE
);
7572 gen_lea_modrm(env
, s
, modrm
);
7573 gen_op_ld_v(s
, MO_16
, s
->T1
, s
->A0
);
7574 gen_add_A0_im(s
, 2);
7575 gen_op_ld_v(s
, CODE64(s
) + MO_32
, s
->T0
, s
->A0
);
7576 if (dflag
== MO_16
) {
7577 tcg_gen_andi_tl(s
->T0
, s
->T0
, 0xffffff);
7579 tcg_gen_st_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, idt
.base
));
7580 tcg_gen_st32_tl(s
->T1
, cpu_env
, offsetof(CPUX86State
, idt
.limit
));
7583 CASE_MODRM_OP(4): /* smsw */
7584 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_READ_CR0
);
7585 tcg_gen_ld_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, cr
[0]));
7587 mod
= (modrm
>> 6) & 3;
7588 ot
= (mod
!= 3 ? MO_16
: s
->dflag
);
7592 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
7594 case 0xee: /* rdpkru */
7595 if (prefixes
& PREFIX_LOCK
) {
7598 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_ECX
]);
7599 gen_helper_rdpkru(s
->tmp1_i64
, cpu_env
, s
->tmp2_i32
);
7600 tcg_gen_extr_i64_tl(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
], s
->tmp1_i64
);
7602 case 0xef: /* wrpkru */
7603 if (prefixes
& PREFIX_LOCK
) {
7606 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
7608 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_ECX
]);
7609 gen_helper_wrpkru(cpu_env
, s
->tmp2_i32
, s
->tmp1_i64
);
7611 CASE_MODRM_OP(6): /* lmsw */
7613 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7616 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_WRITE_CR0
);
7617 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7618 gen_helper_lmsw(cpu_env
, s
->T0
);
7619 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
7623 CASE_MODRM_MEM_OP(7): /* invlpg */
7625 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7628 gen_update_cc_op(s
);
7629 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7630 gen_lea_modrm(env
, s
, modrm
);
7631 gen_helper_invlpg(cpu_env
, s
->A0
);
7632 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
7636 case 0xf8: /* swapgs */
7637 #ifdef TARGET_X86_64
7640 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7642 tcg_gen_mov_tl(s
->T0
, cpu_seg_base
[R_GS
]);
7643 tcg_gen_ld_tl(cpu_seg_base
[R_GS
], cpu_env
,
7644 offsetof(CPUX86State
, kernelgsbase
));
7645 tcg_gen_st_tl(s
->T0
, cpu_env
,
7646 offsetof(CPUX86State
, kernelgsbase
));
7653 case 0xf9: /* rdtscp */
7654 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_RDTSCP
)) {
7657 gen_update_cc_op(s
);
7658 gen_jmp_im(s
, pc_start
- s
->cs_base
);
7659 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
7662 gen_helper_rdtscp(cpu_env
);
7663 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
7664 gen_jmp(s
, s
->pc
- s
->cs_base
);
7673 case 0x108: /* invd */
7674 case 0x109: /* wbinvd */
7676 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7678 gen_svm_check_intercept(s
, pc_start
, (b
& 2) ? SVM_EXIT_INVD
: SVM_EXIT_WBINVD
);
7682 case 0x63: /* arpl or movslS (x86_64) */
7683 #ifdef TARGET_X86_64
7686 /* d_ot is the size of destination */
7689 modrm
= x86_ldub_code(env
, s
);
7690 reg
= ((modrm
>> 3) & 7) | rex_r
;
7691 mod
= (modrm
>> 6) & 3;
7692 rm
= (modrm
& 7) | REX_B(s
);
7695 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, rm
);
7697 if (d_ot
== MO_64
) {
7698 tcg_gen_ext32s_tl(s
->T0
, s
->T0
);
7700 gen_op_mov_reg_v(s
, d_ot
, reg
, s
->T0
);
7702 gen_lea_modrm(env
, s
, modrm
);
7703 gen_op_ld_v(s
, MO_32
| MO_SIGN
, s
->T0
, s
->A0
);
7704 gen_op_mov_reg_v(s
, d_ot
, reg
, s
->T0
);
7710 TCGv t0
, t1
, t2
, a0
;
7712 if (!s
->pe
|| s
->vm86
)
7714 t0
= tcg_temp_local_new();
7715 t1
= tcg_temp_local_new();
7716 t2
= tcg_temp_local_new();
7718 modrm
= x86_ldub_code(env
, s
);
7719 reg
= (modrm
>> 3) & 7;
7720 mod
= (modrm
>> 6) & 3;
7723 gen_lea_modrm(env
, s
, modrm
);
7724 gen_op_ld_v(s
, ot
, t0
, s
->A0
);
7725 a0
= tcg_temp_local_new();
7726 tcg_gen_mov_tl(a0
, s
->A0
);
7728 gen_op_mov_v_reg(s
, ot
, t0
, rm
);
7731 gen_op_mov_v_reg(s
, ot
, t1
, reg
);
7732 tcg_gen_andi_tl(s
->tmp0
, t0
, 3);
7733 tcg_gen_andi_tl(t1
, t1
, 3);
7734 tcg_gen_movi_tl(t2
, 0);
7735 label1
= gen_new_label();
7736 tcg_gen_brcond_tl(TCG_COND_GE
, s
->tmp0
, t1
, label1
);
7737 tcg_gen_andi_tl(t0
, t0
, ~3);
7738 tcg_gen_or_tl(t0
, t0
, t1
);
7739 tcg_gen_movi_tl(t2
, CC_Z
);
7740 gen_set_label(label1
);
7742 gen_op_st_v(s
, ot
, t0
, a0
);
7745 gen_op_mov_reg_v(s
, ot
, rm
, t0
);
7747 gen_compute_eflags(s
);
7748 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, ~CC_Z
);
7749 tcg_gen_or_tl(cpu_cc_src
, cpu_cc_src
, t2
);
7755 case 0x102: /* lar */
7756 case 0x103: /* lsl */
7760 if (!s
->pe
|| s
->vm86
)
7762 ot
= dflag
!= MO_16
? MO_32
: MO_16
;
7763 modrm
= x86_ldub_code(env
, s
);
7764 reg
= ((modrm
>> 3) & 7) | rex_r
;
7765 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7766 t0
= tcg_temp_local_new();
7767 gen_update_cc_op(s
);
7769 gen_helper_lar(t0
, cpu_env
, s
->T0
);
7771 gen_helper_lsl(t0
, cpu_env
, s
->T0
);
7773 tcg_gen_andi_tl(s
->tmp0
, cpu_cc_src
, CC_Z
);
7774 label1
= gen_new_label();
7775 tcg_gen_brcondi_tl(TCG_COND_EQ
, s
->tmp0
, 0, label1
);
7776 gen_op_mov_reg_v(s
, ot
, reg
, t0
);
7777 gen_set_label(label1
);
7778 set_cc_op(s
, CC_OP_EFLAGS
);
7783 modrm
= x86_ldub_code(env
, s
);
7784 mod
= (modrm
>> 6) & 3;
7785 op
= (modrm
>> 3) & 7;
7787 case 0: /* prefetchnta */
7788 case 1: /* prefetchnt0 */
7789 case 2: /* prefetchnt0 */
7790 case 3: /* prefetchnt0 */
7793 gen_nop_modrm(env
, s
, modrm
);
7794 /* nothing more to do */
7796 default: /* nop (multi byte) */
7797 gen_nop_modrm(env
, s
, modrm
);
7802 modrm
= x86_ldub_code(env
, s
);
7803 if (s
->flags
& HF_MPX_EN_MASK
) {
7804 mod
= (modrm
>> 6) & 3;
7805 reg
= ((modrm
>> 3) & 7) | rex_r
;
7806 if (prefixes
& PREFIX_REPZ
) {
7809 || (prefixes
& PREFIX_LOCK
)
7810 || s
->aflag
== MO_16
) {
7813 gen_bndck(env
, s
, modrm
, TCG_COND_LTU
, cpu_bndl
[reg
]);
7814 } else if (prefixes
& PREFIX_REPNZ
) {
7817 || (prefixes
& PREFIX_LOCK
)
7818 || s
->aflag
== MO_16
) {
7821 TCGv_i64 notu
= tcg_temp_new_i64();
7822 tcg_gen_not_i64(notu
, cpu_bndu
[reg
]);
7823 gen_bndck(env
, s
, modrm
, TCG_COND_GTU
, notu
);
7824 tcg_temp_free_i64(notu
);
7825 } else if (prefixes
& PREFIX_DATA
) {
7826 /* bndmov -- from reg/mem */
7827 if (reg
>= 4 || s
->aflag
== MO_16
) {
7831 int reg2
= (modrm
& 7) | REX_B(s
);
7832 if (reg2
>= 4 || (prefixes
& PREFIX_LOCK
)) {
7835 if (s
->flags
& HF_MPX_IU_MASK
) {
7836 tcg_gen_mov_i64(cpu_bndl
[reg
], cpu_bndl
[reg2
]);
7837 tcg_gen_mov_i64(cpu_bndu
[reg
], cpu_bndu
[reg2
]);
7840 gen_lea_modrm(env
, s
, modrm
);
7842 tcg_gen_qemu_ld_i64(cpu_bndl
[reg
], s
->A0
,
7843 s
->mem_index
, MO_LEQ
);
7844 tcg_gen_addi_tl(s
->A0
, s
->A0
, 8);
7845 tcg_gen_qemu_ld_i64(cpu_bndu
[reg
], s
->A0
,
7846 s
->mem_index
, MO_LEQ
);
7848 tcg_gen_qemu_ld_i64(cpu_bndl
[reg
], s
->A0
,
7849 s
->mem_index
, MO_LEUL
);
7850 tcg_gen_addi_tl(s
->A0
, s
->A0
, 4);
7851 tcg_gen_qemu_ld_i64(cpu_bndu
[reg
], s
->A0
,
7852 s
->mem_index
, MO_LEUL
);
7854 /* bnd registers are now in-use */
7855 gen_set_hflag(s
, HF_MPX_IU_MASK
);
7857 } else if (mod
!= 3) {
7859 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
7861 || (prefixes
& PREFIX_LOCK
)
7862 || s
->aflag
== MO_16
7867 tcg_gen_addi_tl(s
->A0
, cpu_regs
[a
.base
], a
.disp
);
7869 tcg_gen_movi_tl(s
->A0
, 0);
7871 gen_lea_v_seg(s
, s
->aflag
, s
->A0
, a
.def_seg
, s
->override
);
7873 tcg_gen_mov_tl(s
->T0
, cpu_regs
[a
.index
]);
7875 tcg_gen_movi_tl(s
->T0
, 0);
7878 gen_helper_bndldx64(cpu_bndl
[reg
], cpu_env
, s
->A0
, s
->T0
);
7879 tcg_gen_ld_i64(cpu_bndu
[reg
], cpu_env
,
7880 offsetof(CPUX86State
, mmx_t0
.MMX_Q(0)));
7882 gen_helper_bndldx32(cpu_bndu
[reg
], cpu_env
, s
->A0
, s
->T0
);
7883 tcg_gen_ext32u_i64(cpu_bndl
[reg
], cpu_bndu
[reg
]);
7884 tcg_gen_shri_i64(cpu_bndu
[reg
], cpu_bndu
[reg
], 32);
7886 gen_set_hflag(s
, HF_MPX_IU_MASK
);
7889 gen_nop_modrm(env
, s
, modrm
);
7892 modrm
= x86_ldub_code(env
, s
);
7893 if (s
->flags
& HF_MPX_EN_MASK
) {
7894 mod
= (modrm
>> 6) & 3;
7895 reg
= ((modrm
>> 3) & 7) | rex_r
;
7896 if (mod
!= 3 && (prefixes
& PREFIX_REPZ
)) {
7899 || (prefixes
& PREFIX_LOCK
)
7900 || s
->aflag
== MO_16
) {
7903 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
7905 tcg_gen_extu_tl_i64(cpu_bndl
[reg
], cpu_regs
[a
.base
]);
7907 tcg_gen_ext32u_i64(cpu_bndl
[reg
], cpu_bndl
[reg
]);
7909 } else if (a
.base
== -1) {
7910 /* no base register has lower bound of 0 */
7911 tcg_gen_movi_i64(cpu_bndl
[reg
], 0);
7913 /* rip-relative generates #ud */
7916 tcg_gen_not_tl(s
->A0
, gen_lea_modrm_1(s
, a
));
7918 tcg_gen_ext32u_tl(s
->A0
, s
->A0
);
7920 tcg_gen_extu_tl_i64(cpu_bndu
[reg
], s
->A0
);
7921 /* bnd registers are now in-use */
7922 gen_set_hflag(s
, HF_MPX_IU_MASK
);
7924 } else if (prefixes
& PREFIX_REPNZ
) {
7927 || (prefixes
& PREFIX_LOCK
)
7928 || s
->aflag
== MO_16
) {
7931 gen_bndck(env
, s
, modrm
, TCG_COND_GTU
, cpu_bndu
[reg
]);
7932 } else if (prefixes
& PREFIX_DATA
) {
7933 /* bndmov -- to reg/mem */
7934 if (reg
>= 4 || s
->aflag
== MO_16
) {
7938 int reg2
= (modrm
& 7) | REX_B(s
);
7939 if (reg2
>= 4 || (prefixes
& PREFIX_LOCK
)) {
7942 if (s
->flags
& HF_MPX_IU_MASK
) {
7943 tcg_gen_mov_i64(cpu_bndl
[reg2
], cpu_bndl
[reg
]);
7944 tcg_gen_mov_i64(cpu_bndu
[reg2
], cpu_bndu
[reg
]);
7947 gen_lea_modrm(env
, s
, modrm
);
7949 tcg_gen_qemu_st_i64(cpu_bndl
[reg
], s
->A0
,
7950 s
->mem_index
, MO_LEQ
);
7951 tcg_gen_addi_tl(s
->A0
, s
->A0
, 8);
7952 tcg_gen_qemu_st_i64(cpu_bndu
[reg
], s
->A0
,
7953 s
->mem_index
, MO_LEQ
);
7955 tcg_gen_qemu_st_i64(cpu_bndl
[reg
], s
->A0
,
7956 s
->mem_index
, MO_LEUL
);
7957 tcg_gen_addi_tl(s
->A0
, s
->A0
, 4);
7958 tcg_gen_qemu_st_i64(cpu_bndu
[reg
], s
->A0
,
7959 s
->mem_index
, MO_LEUL
);
7962 } else if (mod
!= 3) {
7964 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
7966 || (prefixes
& PREFIX_LOCK
)
7967 || s
->aflag
== MO_16
7972 tcg_gen_addi_tl(s
->A0
, cpu_regs
[a
.base
], a
.disp
);
7974 tcg_gen_movi_tl(s
->A0
, 0);
7976 gen_lea_v_seg(s
, s
->aflag
, s
->A0
, a
.def_seg
, s
->override
);
7978 tcg_gen_mov_tl(s
->T0
, cpu_regs
[a
.index
]);
7980 tcg_gen_movi_tl(s
->T0
, 0);
7983 gen_helper_bndstx64(cpu_env
, s
->A0
, s
->T0
,
7984 cpu_bndl
[reg
], cpu_bndu
[reg
]);
7986 gen_helper_bndstx32(cpu_env
, s
->A0
, s
->T0
,
7987 cpu_bndl
[reg
], cpu_bndu
[reg
]);
7991 gen_nop_modrm(env
, s
, modrm
);
7993 case 0x119: case 0x11c ... 0x11f: /* nop (multi byte) */
7994 modrm
= x86_ldub_code(env
, s
);
7995 gen_nop_modrm(env
, s
, modrm
);
7997 case 0x120: /* mov reg, crN */
7998 case 0x122: /* mov crN, reg */
8000 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
8002 modrm
= x86_ldub_code(env
, s
);
8003 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
8004 * AMD documentation (24594.pdf) and testing of
8005 * intel 386 and 486 processors all show that the mod bits
8006 * are assumed to be 1's, regardless of actual values.
8008 rm
= (modrm
& 7) | REX_B(s
);
8009 reg
= ((modrm
>> 3) & 7) | rex_r
;
8014 if ((prefixes
& PREFIX_LOCK
) && (reg
== 0) &&
8015 (s
->cpuid_ext3_features
& CPUID_EXT3_CR8LEG
)) {
8024 gen_update_cc_op(s
);
8025 gen_jmp_im(s
, pc_start
- s
->cs_base
);
8027 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
8030 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
8031 gen_helper_write_crN(cpu_env
, tcg_const_i32(reg
),
8033 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
8036 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
8039 gen_helper_read_crN(s
->T0
, cpu_env
, tcg_const_i32(reg
));
8040 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
8041 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
8051 case 0x121: /* mov reg, drN */
8052 case 0x123: /* mov drN, reg */
8054 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
8056 modrm
= x86_ldub_code(env
, s
);
8057 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
8058 * AMD documentation (24594.pdf) and testing of
8059 * intel 386 and 486 processors all show that the mod bits
8060 * are assumed to be 1's, regardless of actual values.
8062 rm
= (modrm
& 7) | REX_B(s
);
8063 reg
= ((modrm
>> 3) & 7) | rex_r
;
8072 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_WRITE_DR0
+ reg
);
8073 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
8074 tcg_gen_movi_i32(s
->tmp2_i32
, reg
);
8075 gen_helper_set_dr(cpu_env
, s
->tmp2_i32
, s
->T0
);
8076 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
8079 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_READ_DR0
+ reg
);
8080 tcg_gen_movi_i32(s
->tmp2_i32
, reg
);
8081 gen_helper_get_dr(s
->T0
, cpu_env
, s
->tmp2_i32
);
8082 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
8086 case 0x106: /* clts */
8088 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
8090 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_WRITE_CR0
);
8091 gen_helper_clts(cpu_env
);
8092 /* abort block because static cpu state changed */
8093 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
8097 /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
8098 case 0x1c3: /* MOVNTI reg, mem */
8099 if (!(s
->cpuid_features
& CPUID_SSE2
))
8101 ot
= mo_64_32(dflag
);
8102 modrm
= x86_ldub_code(env
, s
);
8103 mod
= (modrm
>> 6) & 3;
8106 reg
= ((modrm
>> 3) & 7) | rex_r
;
8107 /* generate a generic store */
8108 gen_ldst_modrm(env
, s
, modrm
, ot
, reg
, 1);
8111 modrm
= x86_ldub_code(env
, s
);
8113 CASE_MODRM_MEM_OP(0): /* fxsave */
8114 if (!(s
->cpuid_features
& CPUID_FXSR
)
8115 || (prefixes
& PREFIX_LOCK
)) {
8118 if ((s
->flags
& HF_EM_MASK
) || (s
->flags
& HF_TS_MASK
)) {
8119 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
8122 gen_lea_modrm(env
, s
, modrm
);
8123 gen_helper_fxsave(cpu_env
, s
->A0
);
8126 CASE_MODRM_MEM_OP(1): /* fxrstor */
8127 if (!(s
->cpuid_features
& CPUID_FXSR
)
8128 || (prefixes
& PREFIX_LOCK
)) {
8131 if ((s
->flags
& HF_EM_MASK
) || (s
->flags
& HF_TS_MASK
)) {
8132 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
8135 gen_lea_modrm(env
, s
, modrm
);
8136 gen_helper_fxrstor(cpu_env
, s
->A0
);
8139 CASE_MODRM_MEM_OP(2): /* ldmxcsr */
8140 if ((s
->flags
& HF_EM_MASK
) || !(s
->flags
& HF_OSFXSR_MASK
)) {
8143 if (s
->flags
& HF_TS_MASK
) {
8144 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
8147 gen_lea_modrm(env
, s
, modrm
);
8148 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
, s
->mem_index
, MO_LEUL
);
8149 gen_helper_ldmxcsr(cpu_env
, s
->tmp2_i32
);
8152 CASE_MODRM_MEM_OP(3): /* stmxcsr */
8153 if ((s
->flags
& HF_EM_MASK
) || !(s
->flags
& HF_OSFXSR_MASK
)) {
8156 if (s
->flags
& HF_TS_MASK
) {
8157 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
8160 gen_lea_modrm(env
, s
, modrm
);
8161 tcg_gen_ld32u_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, mxcsr
));
8162 gen_op_st_v(s
, MO_32
, s
->T0
, s
->A0
);
8165 CASE_MODRM_MEM_OP(4): /* xsave */
8166 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
8167 || (prefixes
& (PREFIX_LOCK
| PREFIX_DATA
8168 | PREFIX_REPZ
| PREFIX_REPNZ
))) {
8171 gen_lea_modrm(env
, s
, modrm
);
8172 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
8174 gen_helper_xsave(cpu_env
, s
->A0
, s
->tmp1_i64
);
8177 CASE_MODRM_MEM_OP(5): /* xrstor */
8178 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
8179 || (prefixes
& (PREFIX_LOCK
| PREFIX_DATA
8180 | PREFIX_REPZ
| PREFIX_REPNZ
))) {
8183 gen_lea_modrm(env
, s
, modrm
);
8184 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
8186 gen_helper_xrstor(cpu_env
, s
->A0
, s
->tmp1_i64
);
8187 /* XRSTOR is how MPX is enabled, which changes how
8188 we translate. Thus we need to end the TB. */
8189 gen_update_cc_op(s
);
8190 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
8194 CASE_MODRM_MEM_OP(6): /* xsaveopt / clwb */
8195 if (prefixes
& PREFIX_LOCK
) {
8198 if (prefixes
& PREFIX_DATA
) {
8200 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_CLWB
)) {
8203 gen_nop_modrm(env
, s
, modrm
);
8206 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
8207 || (s
->cpuid_xsave_features
& CPUID_XSAVE_XSAVEOPT
) == 0
8208 || (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
))) {
8211 gen_lea_modrm(env
, s
, modrm
);
8212 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
8214 gen_helper_xsaveopt(cpu_env
, s
->A0
, s
->tmp1_i64
);
8218 CASE_MODRM_MEM_OP(7): /* clflush / clflushopt */
8219 if (prefixes
& PREFIX_LOCK
) {
8222 if (prefixes
& PREFIX_DATA
) {
8224 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_CLFLUSHOPT
)) {
8229 if ((s
->prefix
& (PREFIX_REPZ
| PREFIX_REPNZ
))
8230 || !(s
->cpuid_features
& CPUID_CLFLUSH
)) {
8234 gen_nop_modrm(env
, s
, modrm
);
8237 case 0xc0 ... 0xc7: /* rdfsbase (f3 0f ae /0) */
8238 case 0xc8 ... 0xcf: /* rdgsbase (f3 0f ae /1) */
8239 case 0xd0 ... 0xd7: /* wrfsbase (f3 0f ae /2) */
8240 case 0xd8 ... 0xdf: /* wrgsbase (f3 0f ae /3) */
8242 && (prefixes
& PREFIX_REPZ
)
8243 && !(prefixes
& PREFIX_LOCK
)
8244 && (s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_FSGSBASE
)) {
8245 TCGv base
, treg
, src
, dst
;
8247 /* Preserve hflags bits by testing CR4 at runtime. */
8248 tcg_gen_movi_i32(s
->tmp2_i32
, CR4_FSGSBASE_MASK
);
8249 gen_helper_cr4_testbit(cpu_env
, s
->tmp2_i32
);
8251 base
= cpu_seg_base
[modrm
& 8 ? R_GS
: R_FS
];
8252 treg
= cpu_regs
[(modrm
& 7) | REX_B(s
)];
8256 dst
= base
, src
= treg
;
8259 dst
= treg
, src
= base
;
8262 if (s
->dflag
== MO_32
) {
8263 tcg_gen_ext32u_tl(dst
, src
);
8265 tcg_gen_mov_tl(dst
, src
);
8271 case 0xf8: /* sfence / pcommit */
8272 if (prefixes
& PREFIX_DATA
) {
8274 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_PCOMMIT
)
8275 || (prefixes
& PREFIX_LOCK
)) {
8281 case 0xf9 ... 0xff: /* sfence */
8282 if (!(s
->cpuid_features
& CPUID_SSE
)
8283 || (prefixes
& PREFIX_LOCK
)) {
8286 tcg_gen_mb(TCG_MO_ST_ST
| TCG_BAR_SC
);
8288 case 0xe8 ... 0xef: /* lfence */
8289 if (!(s
->cpuid_features
& CPUID_SSE
)
8290 || (prefixes
& PREFIX_LOCK
)) {
8293 tcg_gen_mb(TCG_MO_LD_LD
| TCG_BAR_SC
);
8295 case 0xf0 ... 0xf7: /* mfence */
8296 if (!(s
->cpuid_features
& CPUID_SSE2
)
8297 || (prefixes
& PREFIX_LOCK
)) {
8300 tcg_gen_mb(TCG_MO_ALL
| TCG_BAR_SC
);
8308 case 0x10d: /* 3DNow! prefetch(w) */
8309 modrm
= x86_ldub_code(env
, s
);
8310 mod
= (modrm
>> 6) & 3;
8313 gen_nop_modrm(env
, s
, modrm
);
8315 case 0x1aa: /* rsm */
8316 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_RSM
);
8317 if (!(s
->flags
& HF_SMM_MASK
))
8319 gen_update_cc_op(s
);
8320 gen_jmp_im(s
, s
->pc
- s
->cs_base
);
8321 gen_helper_rsm(cpu_env
);
8324 case 0x1b8: /* SSE4.2 popcnt */
8325 if ((prefixes
& (PREFIX_REPZ
| PREFIX_LOCK
| PREFIX_REPNZ
)) !=
8328 if (!(s
->cpuid_ext_features
& CPUID_EXT_POPCNT
))
8331 modrm
= x86_ldub_code(env
, s
);
8332 reg
= ((modrm
>> 3) & 7) | rex_r
;
8334 if (s
->prefix
& PREFIX_DATA
) {
8337 ot
= mo_64_32(dflag
);
8340 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
8341 gen_extu(ot
, s
->T0
);
8342 tcg_gen_mov_tl(cpu_cc_src
, s
->T0
);
8343 tcg_gen_ctpop_tl(s
->T0
, s
->T0
);
8344 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
8346 set_cc_op(s
, CC_OP_POPCNT
);
8348 case 0x10e ... 0x10f:
8349 /* 3DNow! instructions, ignore prefixes */
8350 s
->prefix
&= ~(PREFIX_REPZ
| PREFIX_REPNZ
| PREFIX_DATA
);
8352 case 0x110 ... 0x117:
8353 case 0x128 ... 0x12f:
8354 case 0x138 ... 0x13a:
8355 case 0x150 ... 0x179:
8356 case 0x17c ... 0x17f:
8358 case 0x1c4 ... 0x1c6:
8359 case 0x1d0 ... 0x1fe:
8360 gen_sse(env
, s
, b
, pc_start
, rex_r
);
8367 gen_illegal_opcode(s
);
8370 gen_unknown_opcode(env
, s
);
8374 void tcg_x86_init(void)
8376 static const char reg_names
[CPU_NB_REGS
][4] = {
8377 #ifdef TARGET_X86_64
8405 static const char seg_base_names
[6][8] = {
8413 static const char bnd_regl_names
[4][8] = {
8414 "bnd0_lb", "bnd1_lb", "bnd2_lb", "bnd3_lb"
8416 static const char bnd_regu_names
[4][8] = {
8417 "bnd0_ub", "bnd1_ub", "bnd2_ub", "bnd3_ub"
8421 cpu_cc_op
= tcg_global_mem_new_i32(cpu_env
,
8422 offsetof(CPUX86State
, cc_op
), "cc_op");
8423 cpu_cc_dst
= tcg_global_mem_new(cpu_env
, offsetof(CPUX86State
, cc_dst
),
8425 cpu_cc_src
= tcg_global_mem_new(cpu_env
, offsetof(CPUX86State
, cc_src
),
8427 cpu_cc_src2
= tcg_global_mem_new(cpu_env
, offsetof(CPUX86State
, cc_src2
),
8430 for (i
= 0; i
< CPU_NB_REGS
; ++i
) {
8431 cpu_regs
[i
] = tcg_global_mem_new(cpu_env
,
8432 offsetof(CPUX86State
, regs
[i
]),
8436 for (i
= 0; i
< 6; ++i
) {
8438 = tcg_global_mem_new(cpu_env
,
8439 offsetof(CPUX86State
, segs
[i
].base
),
8443 for (i
= 0; i
< 4; ++i
) {
8445 = tcg_global_mem_new_i64(cpu_env
,
8446 offsetof(CPUX86State
, bnd_regs
[i
].lb
),
8449 = tcg_global_mem_new_i64(cpu_env
,
8450 offsetof(CPUX86State
, bnd_regs
[i
].ub
),
8455 static void i386_tr_init_disas_context(DisasContextBase
*dcbase
, CPUState
*cpu
)
8457 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
8458 CPUX86State
*env
= cpu
->env_ptr
;
8459 uint32_t flags
= dc
->base
.tb
->flags
;
8460 target_ulong cs_base
= dc
->base
.tb
->cs_base
;
8462 dc
->pe
= (flags
>> HF_PE_SHIFT
) & 1;
8463 dc
->code32
= (flags
>> HF_CS32_SHIFT
) & 1;
8464 dc
->ss32
= (flags
>> HF_SS32_SHIFT
) & 1;
8465 dc
->addseg
= (flags
>> HF_ADDSEG_SHIFT
) & 1;
8467 dc
->vm86
= (flags
>> VM_SHIFT
) & 1;
8468 dc
->cpl
= (flags
>> HF_CPL_SHIFT
) & 3;
8469 dc
->iopl
= (flags
>> IOPL_SHIFT
) & 3;
8470 dc
->tf
= (flags
>> TF_SHIFT
) & 1;
8471 dc
->cc_op
= CC_OP_DYNAMIC
;
8472 dc
->cc_op_dirty
= false;
8473 dc
->cs_base
= cs_base
;
8474 dc
->popl_esp_hack
= 0;
8475 /* select memory access functions */
8477 #ifdef CONFIG_SOFTMMU
8478 dc
->mem_index
= cpu_mmu_index(env
, false);
8480 dc
->cpuid_features
= env
->features
[FEAT_1_EDX
];
8481 dc
->cpuid_ext_features
= env
->features
[FEAT_1_ECX
];
8482 dc
->cpuid_ext2_features
= env
->features
[FEAT_8000_0001_EDX
];
8483 dc
->cpuid_ext3_features
= env
->features
[FEAT_8000_0001_ECX
];
8484 dc
->cpuid_7_0_ebx_features
= env
->features
[FEAT_7_0_EBX
];
8485 dc
->cpuid_xsave_features
= env
->features
[FEAT_XSAVE
];
8486 #ifdef TARGET_X86_64
8487 dc
->lma
= (flags
>> HF_LMA_SHIFT
) & 1;
8488 dc
->code64
= (flags
>> HF_CS64_SHIFT
) & 1;
8491 dc
->jmp_opt
= !(dc
->tf
|| dc
->base
.singlestep_enabled
||
8492 (flags
& HF_INHIBIT_IRQ_MASK
));
8493 /* Do not optimize repz jumps at all in icount mode, because
8494 rep movsS instructions are execured with different paths
8495 in !repz_opt and repz_opt modes. The first one was used
8496 always except single step mode. And this setting
8497 disables jumps optimization and control paths become
8498 equivalent in run and single step modes.
8499 Now there will be no jump optimization for repz in
8500 record/replay modes and there will always be an
8501 additional step for ecx=0 when icount is enabled.
8503 dc
->repz_opt
= !dc
->jmp_opt
&& !(tb_cflags(dc
->base
.tb
) & CF_USE_ICOUNT
);
8505 /* check addseg logic */
8506 if (!dc
->addseg
&& (dc
->vm86
|| !dc
->pe
|| !dc
->code32
))
8507 printf("ERROR addseg\n");
8510 dc
->T0
= tcg_temp_new();
8511 dc
->T1
= tcg_temp_new();
8512 dc
->A0
= tcg_temp_new();
8514 dc
->tmp0
= tcg_temp_new();
8515 dc
->tmp1_i64
= tcg_temp_new_i64();
8516 dc
->tmp2_i32
= tcg_temp_new_i32();
8517 dc
->tmp3_i32
= tcg_temp_new_i32();
8518 dc
->tmp4
= tcg_temp_new();
8519 dc
->ptr0
= tcg_temp_new_ptr();
8520 dc
->ptr1
= tcg_temp_new_ptr();
8521 dc
->cc_srcT
= tcg_temp_local_new();
8524 static void i386_tr_tb_start(DisasContextBase
*db
, CPUState
*cpu
)
8528 static void i386_tr_insn_start(DisasContextBase
*dcbase
, CPUState
*cpu
)
8530 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
8532 tcg_gen_insn_start(dc
->base
.pc_next
, dc
->cc_op
);
8535 static bool i386_tr_breakpoint_check(DisasContextBase
*dcbase
, CPUState
*cpu
,
8536 const CPUBreakpoint
*bp
)
8538 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
8539 /* If RF is set, suppress an internally generated breakpoint. */
8540 int flags
= dc
->base
.tb
->flags
& HF_RF_MASK
? BP_GDB
: BP_ANY
;
8541 if (bp
->flags
& flags
) {
8542 gen_debug(dc
, dc
->base
.pc_next
- dc
->cs_base
);
8543 dc
->base
.is_jmp
= DISAS_NORETURN
;
8544 /* The address covered by the breakpoint must be included in
8545 [tb->pc, tb->pc + tb->size) in order to for it to be
8546 properly cleared -- thus we increment the PC here so that
8547 the generic logic setting tb->size later does the right thing. */
8548 dc
->base
.pc_next
+= 1;
8555 static void i386_tr_translate_insn(DisasContextBase
*dcbase
, CPUState
*cpu
)
8557 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
8558 target_ulong pc_next
= disas_insn(dc
, cpu
);
8560 if (dc
->tf
|| (dc
->base
.tb
->flags
& HF_INHIBIT_IRQ_MASK
)) {
8561 /* if single step mode, we generate only one instruction and
8562 generate an exception */
8563 /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
8564 the flag and abort the translation to give the irqs a
8566 dc
->base
.is_jmp
= DISAS_TOO_MANY
;
8567 } else if ((tb_cflags(dc
->base
.tb
) & CF_USE_ICOUNT
)
8568 && ((pc_next
& TARGET_PAGE_MASK
)
8569 != ((pc_next
+ TARGET_MAX_INSN_SIZE
- 1)
8571 || (pc_next
& ~TARGET_PAGE_MASK
) == 0)) {
8572 /* Do not cross the boundary of the pages in icount mode,
8573 it can cause an exception. Do it only when boundary is
8574 crossed by the first instruction in the block.
8575 If current instruction already crossed the bound - it's ok,
8576 because an exception hasn't stopped this code.
8578 dc
->base
.is_jmp
= DISAS_TOO_MANY
;
8579 } else if ((pc_next
- dc
->base
.pc_first
) >= (TARGET_PAGE_SIZE
- 32)) {
8580 dc
->base
.is_jmp
= DISAS_TOO_MANY
;
8583 dc
->base
.pc_next
= pc_next
;
8586 static void i386_tr_tb_stop(DisasContextBase
*dcbase
, CPUState
*cpu
)
8588 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
8590 if (dc
->base
.is_jmp
== DISAS_TOO_MANY
) {
8591 gen_jmp_im(dc
, dc
->base
.pc_next
- dc
->cs_base
);
8596 static void i386_tr_disas_log(const DisasContextBase
*dcbase
,
8599 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
8601 qemu_log("IN: %s\n", lookup_symbol(dc
->base
.pc_first
));
8602 log_target_disas(cpu
, dc
->base
.pc_first
, dc
->base
.tb
->size
);
8605 static const TranslatorOps i386_tr_ops
= {
8606 .init_disas_context
= i386_tr_init_disas_context
,
8607 .tb_start
= i386_tr_tb_start
,
8608 .insn_start
= i386_tr_insn_start
,
8609 .breakpoint_check
= i386_tr_breakpoint_check
,
8610 .translate_insn
= i386_tr_translate_insn
,
8611 .tb_stop
= i386_tr_tb_stop
,
8612 .disas_log
= i386_tr_disas_log
,
8615 /* generate intermediate code for basic block 'tb'. */
8616 void gen_intermediate_code(CPUState
*cpu
, TranslationBlock
*tb
, int max_insns
)
8620 translator_loop(&i386_tr_ops
, &dc
.base
, cpu
, tb
, max_insns
);
8623 void restore_state_to_opc(CPUX86State
*env
, TranslationBlock
*tb
,
8626 int cc_op
= data
[1];
8627 env
->eip
= data
[0] - tb
->cs_base
;
8628 if (cc_op
!= CC_OP_DYNAMIC
) {