4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "qemu/host-utils.h"
28 #include "disas/disas.h"
30 #include "exec/cpu_ldst.h"
32 #include "exec/helper-proto.h"
33 #include "exec/helper-gen.h"
35 #include "trace-tcg.h"
38 #define PREFIX_REPZ 0x01
39 #define PREFIX_REPNZ 0x02
40 #define PREFIX_LOCK 0x04
41 #define PREFIX_DATA 0x08
42 #define PREFIX_ADR 0x10
43 #define PREFIX_VEX 0x20
46 #define CODE64(s) ((s)->code64)
47 #define REX_X(s) ((s)->rex_x)
48 #define REX_B(s) ((s)->rex_b)
63 //#define MACRO_TEST 1
65 /* global register indexes */
66 static TCGv_ptr cpu_env
;
68 static TCGv cpu_cc_dst
, cpu_cc_src
, cpu_cc_src2
, cpu_cc_srcT
;
69 static TCGv_i32 cpu_cc_op
;
70 static TCGv cpu_regs
[CPU_NB_REGS
];
73 /* local register indexes (only used inside old micro ops) */
74 static TCGv cpu_tmp0
, cpu_tmp4
;
75 static TCGv_ptr cpu_ptr0
, cpu_ptr1
;
76 static TCGv_i32 cpu_tmp2_i32
, cpu_tmp3_i32
;
77 static TCGv_i64 cpu_tmp1_i64
;
79 static uint8_t gen_opc_cc_op
[OPC_BUF_SIZE
];
81 #include "exec/gen-icount.h"
84 static int x86_64_hregs
;
87 typedef struct DisasContext
{
88 /* current insn context */
89 int override
; /* -1 if no override */
93 target_ulong pc
; /* pc = eip + cs_base */
94 int is_jmp
; /* 1 = means jump (stop translation), 2 means CPU
95 static state change (stop translation) */
96 /* current block context */
97 target_ulong cs_base
; /* base of CS segment */
98 int pe
; /* protected mode */
99 int code32
; /* 32 bit code segment */
101 int lma
; /* long mode active */
102 int code64
; /* 64 bit code segment */
105 int vex_l
; /* vex vector length */
106 int vex_v
; /* vex vvvv register, without 1's compliment. */
107 int ss32
; /* 32 bit stack segment */
108 CCOp cc_op
; /* current CC operation */
110 int addseg
; /* non zero if either DS/ES/SS have a non zero base */
111 int f_st
; /* currently unused */
112 int vm86
; /* vm86 mode */
115 int tf
; /* TF cpu flag */
116 int singlestep_enabled
; /* "hardware" single step enabled */
117 int jmp_opt
; /* use direct block chaining for direct jumps */
118 int mem_index
; /* select memory access functions */
119 uint64_t flags
; /* all execution flags */
120 struct TranslationBlock
*tb
;
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
;
130 static void gen_eob(DisasContext
*s
);
131 static void gen_jmp(DisasContext
*s
, target_ulong eip
);
132 static void gen_jmp_tb(DisasContext
*s
, target_ulong eip
, int tb_num
);
133 static void gen_op(DisasContext
*s1
, int op
, TCGMemOp ot
, int d
);
135 /* i386 arith/logic operations */
155 OP_SHL1
, /* undocumented */
171 /* I386 int registers */
172 OR_EAX
, /* MUST be even numbered */
181 OR_TMP0
= 16, /* temporary operand register */
183 OR_A0
, /* temporary register used when doing address evaluation */
193 /* Bit set if the global variable is live after setting CC_OP to X. */
194 static const uint8_t cc_op_live
[CC_OP_NB
] = {
195 [CC_OP_DYNAMIC
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
196 [CC_OP_EFLAGS
] = USES_CC_SRC
,
197 [CC_OP_MULB
... CC_OP_MULQ
] = USES_CC_DST
| USES_CC_SRC
,
198 [CC_OP_ADDB
... CC_OP_ADDQ
] = USES_CC_DST
| USES_CC_SRC
,
199 [CC_OP_ADCB
... CC_OP_ADCQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
200 [CC_OP_SUBB
... CC_OP_SUBQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRCT
,
201 [CC_OP_SBBB
... CC_OP_SBBQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
202 [CC_OP_LOGICB
... CC_OP_LOGICQ
] = USES_CC_DST
,
203 [CC_OP_INCB
... CC_OP_INCQ
] = USES_CC_DST
| USES_CC_SRC
,
204 [CC_OP_DECB
... CC_OP_DECQ
] = USES_CC_DST
| USES_CC_SRC
,
205 [CC_OP_SHLB
... CC_OP_SHLQ
] = USES_CC_DST
| USES_CC_SRC
,
206 [CC_OP_SARB
... CC_OP_SARQ
] = USES_CC_DST
| USES_CC_SRC
,
207 [CC_OP_BMILGB
... CC_OP_BMILGQ
] = USES_CC_DST
| USES_CC_SRC
,
208 [CC_OP_ADCX
] = USES_CC_DST
| USES_CC_SRC
,
209 [CC_OP_ADOX
] = USES_CC_SRC
| USES_CC_SRC2
,
210 [CC_OP_ADCOX
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
214 static void set_cc_op(DisasContext
*s
, CCOp op
)
218 if (s
->cc_op
== op
) {
222 /* Discard CC computation that will no longer be used. */
223 dead
= cc_op_live
[s
->cc_op
] & ~cc_op_live
[op
];
224 if (dead
& USES_CC_DST
) {
225 tcg_gen_discard_tl(cpu_cc_dst
);
227 if (dead
& USES_CC_SRC
) {
228 tcg_gen_discard_tl(cpu_cc_src
);
230 if (dead
& USES_CC_SRC2
) {
231 tcg_gen_discard_tl(cpu_cc_src2
);
233 if (dead
& USES_CC_SRCT
) {
234 tcg_gen_discard_tl(cpu_cc_srcT
);
237 if (op
== CC_OP_DYNAMIC
) {
238 /* The DYNAMIC setting is translator only, and should never be
239 stored. Thus we always consider it clean. */
240 s
->cc_op_dirty
= false;
242 /* Discard any computed CC_OP value (see shifts). */
243 if (s
->cc_op
== CC_OP_DYNAMIC
) {
244 tcg_gen_discard_i32(cpu_cc_op
);
246 s
->cc_op_dirty
= true;
251 static void gen_update_cc_op(DisasContext
*s
)
253 if (s
->cc_op_dirty
) {
254 tcg_gen_movi_i32(cpu_cc_op
, s
->cc_op
);
255 s
->cc_op_dirty
= false;
261 #define NB_OP_SIZES 4
263 #else /* !TARGET_X86_64 */
265 #define NB_OP_SIZES 3
267 #endif /* !TARGET_X86_64 */
269 #if defined(HOST_WORDS_BIGENDIAN)
270 #define REG_B_OFFSET (sizeof(target_ulong) - 1)
271 #define REG_H_OFFSET (sizeof(target_ulong) - 2)
272 #define REG_W_OFFSET (sizeof(target_ulong) - 2)
273 #define REG_L_OFFSET (sizeof(target_ulong) - 4)
274 #define REG_LH_OFFSET (sizeof(target_ulong) - 8)
276 #define REG_B_OFFSET 0
277 #define REG_H_OFFSET 1
278 #define REG_W_OFFSET 0
279 #define REG_L_OFFSET 0
280 #define REG_LH_OFFSET 4
283 /* In instruction encodings for byte register accesses the
284 * register number usually indicates "low 8 bits of register N";
285 * however there are some special cases where N 4..7 indicates
286 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
287 * true for this special case, false otherwise.
289 static inline bool byte_reg_is_xH(int reg
)
295 if (reg
>= 8 || x86_64_hregs
) {
302 /* Select the size of a push/pop operation. */
303 static inline TCGMemOp
mo_pushpop(DisasContext
*s
, TCGMemOp ot
)
306 return ot
== MO_16
? MO_16
: MO_64
;
312 /* Select only size 64 else 32. Used for SSE operand sizes. */
313 static inline TCGMemOp
mo_64_32(TCGMemOp ot
)
316 return ot
== MO_64
? MO_64
: MO_32
;
322 /* Select size 8 if lsb of B is clear, else OT. Used for decoding
323 byte vs word opcodes. */
324 static inline TCGMemOp
mo_b_d(int b
, TCGMemOp ot
)
326 return b
& 1 ? ot
: MO_8
;
329 /* Select size 8 if lsb of B is clear, else OT capped at 32.
330 Used for decoding operand size of port opcodes. */
331 static inline TCGMemOp
mo_b_d32(int b
, TCGMemOp ot
)
333 return b
& 1 ? (ot
== MO_16
? MO_16
: MO_32
) : MO_8
;
336 static void gen_op_mov_reg_v(TCGMemOp ot
, int reg
, TCGv t0
)
340 if (!byte_reg_is_xH(reg
)) {
341 tcg_gen_deposit_tl(cpu_regs
[reg
], cpu_regs
[reg
], t0
, 0, 8);
343 tcg_gen_deposit_tl(cpu_regs
[reg
- 4], cpu_regs
[reg
- 4], t0
, 8, 8);
347 tcg_gen_deposit_tl(cpu_regs
[reg
], cpu_regs
[reg
], t0
, 0, 16);
350 /* For x86_64, this sets the higher half of register to zero.
351 For i386, this is equivalent to a mov. */
352 tcg_gen_ext32u_tl(cpu_regs
[reg
], t0
);
356 tcg_gen_mov_tl(cpu_regs
[reg
], t0
);
364 static inline void gen_op_mov_v_reg(TCGMemOp ot
, TCGv t0
, int reg
)
366 if (ot
== MO_8
&& byte_reg_is_xH(reg
)) {
367 tcg_gen_shri_tl(t0
, cpu_regs
[reg
- 4], 8);
368 tcg_gen_ext8u_tl(t0
, t0
);
370 tcg_gen_mov_tl(t0
, cpu_regs
[reg
]);
374 static inline void gen_op_movl_A0_reg(int reg
)
376 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[reg
]);
379 static inline void gen_op_addl_A0_im(int32_t val
)
381 tcg_gen_addi_tl(cpu_A0
, cpu_A0
, val
);
383 tcg_gen_andi_tl(cpu_A0
, cpu_A0
, 0xffffffff);
388 static inline void gen_op_addq_A0_im(int64_t val
)
390 tcg_gen_addi_tl(cpu_A0
, cpu_A0
, val
);
394 static void gen_add_A0_im(DisasContext
*s
, int val
)
398 gen_op_addq_A0_im(val
);
401 gen_op_addl_A0_im(val
);
404 static inline void gen_op_jmp_v(TCGv dest
)
406 tcg_gen_st_tl(dest
, cpu_env
, offsetof(CPUX86State
, eip
));
409 static inline void gen_op_add_reg_im(TCGMemOp size
, int reg
, int32_t val
)
411 tcg_gen_addi_tl(cpu_tmp0
, cpu_regs
[reg
], val
);
412 gen_op_mov_reg_v(size
, reg
, cpu_tmp0
);
415 static inline void gen_op_add_reg_T0(TCGMemOp size
, int reg
)
417 tcg_gen_add_tl(cpu_tmp0
, cpu_regs
[reg
], cpu_T
[0]);
418 gen_op_mov_reg_v(size
, reg
, cpu_tmp0
);
421 static inline void gen_op_addl_A0_reg_sN(int shift
, int reg
)
423 tcg_gen_mov_tl(cpu_tmp0
, cpu_regs
[reg
]);
425 tcg_gen_shli_tl(cpu_tmp0
, cpu_tmp0
, shift
);
426 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
427 /* For x86_64, this sets the higher half of register to zero.
428 For i386, this is equivalent to a nop. */
429 tcg_gen_ext32u_tl(cpu_A0
, cpu_A0
);
432 static inline void gen_op_movl_A0_seg(int reg
)
434 tcg_gen_ld32u_tl(cpu_A0
, cpu_env
, offsetof(CPUX86State
, segs
[reg
].base
) + REG_L_OFFSET
);
437 static inline void gen_op_addl_A0_seg(DisasContext
*s
, int reg
)
439 tcg_gen_ld_tl(cpu_tmp0
, cpu_env
, offsetof(CPUX86State
, segs
[reg
].base
));
442 tcg_gen_andi_tl(cpu_A0
, cpu_A0
, 0xffffffff);
443 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
445 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
446 tcg_gen_andi_tl(cpu_A0
, cpu_A0
, 0xffffffff);
449 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
454 static inline void gen_op_movq_A0_seg(int reg
)
456 tcg_gen_ld_tl(cpu_A0
, cpu_env
, offsetof(CPUX86State
, segs
[reg
].base
));
459 static inline void gen_op_addq_A0_seg(int reg
)
461 tcg_gen_ld_tl(cpu_tmp0
, cpu_env
, offsetof(CPUX86State
, segs
[reg
].base
));
462 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
465 static inline void gen_op_movq_A0_reg(int reg
)
467 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[reg
]);
470 static inline void gen_op_addq_A0_reg_sN(int shift
, int reg
)
472 tcg_gen_mov_tl(cpu_tmp0
, cpu_regs
[reg
]);
474 tcg_gen_shli_tl(cpu_tmp0
, cpu_tmp0
, shift
);
475 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
479 static inline void gen_op_ld_v(DisasContext
*s
, int idx
, TCGv t0
, TCGv a0
)
481 tcg_gen_qemu_ld_tl(t0
, a0
, s
->mem_index
, idx
| MO_LE
);
484 static inline void gen_op_st_v(DisasContext
*s
, int idx
, TCGv t0
, TCGv a0
)
486 tcg_gen_qemu_st_tl(t0
, a0
, s
->mem_index
, idx
| MO_LE
);
489 static inline void gen_op_st_rm_T0_A0(DisasContext
*s
, int idx
, int d
)
492 gen_op_st_v(s
, idx
, cpu_T
[0], cpu_A0
);
494 gen_op_mov_reg_v(idx
, d
, cpu_T
[0]);
498 static inline void gen_jmp_im(target_ulong pc
)
500 tcg_gen_movi_tl(cpu_tmp0
, pc
);
501 gen_op_jmp_v(cpu_tmp0
);
504 static inline void gen_string_movl_A0_ESI(DisasContext
*s
)
508 override
= s
->override
;
513 gen_op_movq_A0_seg(override
);
514 gen_op_addq_A0_reg_sN(0, R_ESI
);
516 gen_op_movq_A0_reg(R_ESI
);
522 if (s
->addseg
&& override
< 0)
525 gen_op_movl_A0_seg(override
);
526 gen_op_addl_A0_reg_sN(0, R_ESI
);
528 gen_op_movl_A0_reg(R_ESI
);
532 /* 16 address, always override */
535 tcg_gen_ext16u_tl(cpu_A0
, cpu_regs
[R_ESI
]);
536 gen_op_addl_A0_seg(s
, override
);
543 static inline void gen_string_movl_A0_EDI(DisasContext
*s
)
548 gen_op_movq_A0_reg(R_EDI
);
553 gen_op_movl_A0_seg(R_ES
);
554 gen_op_addl_A0_reg_sN(0, R_EDI
);
556 gen_op_movl_A0_reg(R_EDI
);
560 tcg_gen_ext16u_tl(cpu_A0
, cpu_regs
[R_EDI
]);
561 gen_op_addl_A0_seg(s
, R_ES
);
568 static inline void gen_op_movl_T0_Dshift(TCGMemOp ot
)
570 tcg_gen_ld32s_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, df
));
571 tcg_gen_shli_tl(cpu_T
[0], cpu_T
[0], ot
);
574 static TCGv
gen_ext_tl(TCGv dst
, TCGv src
, TCGMemOp size
, bool sign
)
579 tcg_gen_ext8s_tl(dst
, src
);
581 tcg_gen_ext8u_tl(dst
, src
);
586 tcg_gen_ext16s_tl(dst
, src
);
588 tcg_gen_ext16u_tl(dst
, src
);
594 tcg_gen_ext32s_tl(dst
, src
);
596 tcg_gen_ext32u_tl(dst
, src
);
605 static void gen_extu(TCGMemOp ot
, TCGv reg
)
607 gen_ext_tl(reg
, reg
, ot
, false);
610 static void gen_exts(TCGMemOp ot
, TCGv reg
)
612 gen_ext_tl(reg
, reg
, ot
, true);
615 static inline void gen_op_jnz_ecx(TCGMemOp size
, int label1
)
617 tcg_gen_mov_tl(cpu_tmp0
, cpu_regs
[R_ECX
]);
618 gen_extu(size
, cpu_tmp0
);
619 tcg_gen_brcondi_tl(TCG_COND_NE
, cpu_tmp0
, 0, label1
);
622 static inline void gen_op_jz_ecx(TCGMemOp size
, int label1
)
624 tcg_gen_mov_tl(cpu_tmp0
, cpu_regs
[R_ECX
]);
625 gen_extu(size
, cpu_tmp0
);
626 tcg_gen_brcondi_tl(TCG_COND_EQ
, cpu_tmp0
, 0, label1
);
629 static void gen_helper_in_func(TCGMemOp ot
, TCGv v
, TCGv_i32 n
)
633 gen_helper_inb(v
, n
);
636 gen_helper_inw(v
, n
);
639 gen_helper_inl(v
, n
);
646 static void gen_helper_out_func(TCGMemOp ot
, TCGv_i32 v
, TCGv_i32 n
)
650 gen_helper_outb(v
, n
);
653 gen_helper_outw(v
, n
);
656 gen_helper_outl(v
, n
);
663 static void gen_check_io(DisasContext
*s
, TCGMemOp ot
, target_ulong cur_eip
,
667 target_ulong next_eip
;
670 if (s
->pe
&& (s
->cpl
> s
->iopl
|| s
->vm86
)) {
674 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
677 gen_helper_check_iob(cpu_env
, cpu_tmp2_i32
);
680 gen_helper_check_iow(cpu_env
, cpu_tmp2_i32
);
683 gen_helper_check_iol(cpu_env
, cpu_tmp2_i32
);
689 if(s
->flags
& HF_SVMI_MASK
) {
694 svm_flags
|= (1 << (4 + ot
));
695 next_eip
= s
->pc
- s
->cs_base
;
696 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
697 gen_helper_svm_check_io(cpu_env
, cpu_tmp2_i32
,
698 tcg_const_i32(svm_flags
),
699 tcg_const_i32(next_eip
- cur_eip
));
703 static inline void gen_movs(DisasContext
*s
, TCGMemOp ot
)
705 gen_string_movl_A0_ESI(s
);
706 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
707 gen_string_movl_A0_EDI(s
);
708 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
709 gen_op_movl_T0_Dshift(ot
);
710 gen_op_add_reg_T0(s
->aflag
, R_ESI
);
711 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
714 static void gen_op_update1_cc(void)
716 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
719 static void gen_op_update2_cc(void)
721 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[1]);
722 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
725 static void gen_op_update3_cc(TCGv reg
)
727 tcg_gen_mov_tl(cpu_cc_src2
, reg
);
728 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[1]);
729 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
732 static inline void gen_op_testl_T0_T1_cc(void)
734 tcg_gen_and_tl(cpu_cc_dst
, cpu_T
[0], cpu_T
[1]);
737 static void gen_op_update_neg_cc(void)
739 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
740 tcg_gen_neg_tl(cpu_cc_src
, cpu_T
[0]);
741 tcg_gen_movi_tl(cpu_cc_srcT
, 0);
744 /* compute all eflags to cc_src */
745 static void gen_compute_eflags(DisasContext
*s
)
747 TCGv zero
, dst
, src1
, src2
;
750 if (s
->cc_op
== CC_OP_EFLAGS
) {
753 if (s
->cc_op
== CC_OP_CLR
) {
754 tcg_gen_movi_tl(cpu_cc_src
, CC_Z
| CC_P
);
755 set_cc_op(s
, CC_OP_EFLAGS
);
764 /* Take care to not read values that are not live. */
765 live
= cc_op_live
[s
->cc_op
] & ~USES_CC_SRCT
;
766 dead
= live
^ (USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
);
768 zero
= tcg_const_tl(0);
769 if (dead
& USES_CC_DST
) {
772 if (dead
& USES_CC_SRC
) {
775 if (dead
& USES_CC_SRC2
) {
781 gen_helper_cc_compute_all(cpu_cc_src
, dst
, src1
, src2
, cpu_cc_op
);
782 set_cc_op(s
, CC_OP_EFLAGS
);
789 typedef struct CCPrepare
{
799 /* compute eflags.C to reg */
800 static CCPrepare
gen_prepare_eflags_c(DisasContext
*s
, TCGv reg
)
806 case CC_OP_SUBB
... CC_OP_SUBQ
:
807 /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
808 size
= s
->cc_op
- CC_OP_SUBB
;
809 t1
= gen_ext_tl(cpu_tmp0
, cpu_cc_src
, size
, false);
810 /* If no temporary was used, be careful not to alias t1 and t0. */
811 t0
= TCGV_EQUAL(t1
, cpu_cc_src
) ? cpu_tmp0
: reg
;
812 tcg_gen_mov_tl(t0
, cpu_cc_srcT
);
816 case CC_OP_ADDB
... CC_OP_ADDQ
:
817 /* (DATA_TYPE)CC_DST < (DATA_TYPE)CC_SRC */
818 size
= s
->cc_op
- CC_OP_ADDB
;
819 t1
= gen_ext_tl(cpu_tmp0
, cpu_cc_src
, size
, false);
820 t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, false);
822 return (CCPrepare
) { .cond
= TCG_COND_LTU
, .reg
= t0
,
823 .reg2
= t1
, .mask
= -1, .use_reg2
= true };
825 case CC_OP_LOGICB
... CC_OP_LOGICQ
:
827 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
829 case CC_OP_INCB
... CC_OP_INCQ
:
830 case CC_OP_DECB
... CC_OP_DECQ
:
831 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
832 .mask
= -1, .no_setcond
= true };
834 case CC_OP_SHLB
... CC_OP_SHLQ
:
835 /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
836 size
= s
->cc_op
- CC_OP_SHLB
;
837 shift
= (8 << size
) - 1;
838 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
839 .mask
= (target_ulong
)1 << shift
};
841 case CC_OP_MULB
... CC_OP_MULQ
:
842 return (CCPrepare
) { .cond
= TCG_COND_NE
,
843 .reg
= cpu_cc_src
, .mask
= -1 };
845 case CC_OP_BMILGB
... CC_OP_BMILGQ
:
846 size
= s
->cc_op
- CC_OP_BMILGB
;
847 t0
= gen_ext_tl(reg
, cpu_cc_src
, size
, false);
848 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= t0
, .mask
= -1 };
852 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_dst
,
853 .mask
= -1, .no_setcond
= true };
856 case CC_OP_SARB
... CC_OP_SARQ
:
858 return (CCPrepare
) { .cond
= TCG_COND_NE
,
859 .reg
= cpu_cc_src
, .mask
= CC_C
};
862 /* The need to compute only C from CC_OP_DYNAMIC is important
863 in efficiently implementing e.g. INC at the start of a TB. */
865 gen_helper_cc_compute_c(reg
, cpu_cc_dst
, cpu_cc_src
,
866 cpu_cc_src2
, cpu_cc_op
);
867 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
868 .mask
= -1, .no_setcond
= true };
872 /* compute eflags.P to reg */
873 static CCPrepare
gen_prepare_eflags_p(DisasContext
*s
, TCGv reg
)
875 gen_compute_eflags(s
);
876 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
880 /* compute eflags.S to reg */
881 static CCPrepare
gen_prepare_eflags_s(DisasContext
*s
, TCGv reg
)
885 gen_compute_eflags(s
);
891 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
894 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
897 TCGMemOp size
= (s
->cc_op
- CC_OP_ADDB
) & 3;
898 TCGv t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, true);
899 return (CCPrepare
) { .cond
= TCG_COND_LT
, .reg
= t0
, .mask
= -1 };
904 /* compute eflags.O to reg */
905 static CCPrepare
gen_prepare_eflags_o(DisasContext
*s
, TCGv reg
)
910 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src2
,
911 .mask
= -1, .no_setcond
= true };
913 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
915 gen_compute_eflags(s
);
916 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
921 /* compute eflags.Z to reg */
922 static CCPrepare
gen_prepare_eflags_z(DisasContext
*s
, TCGv reg
)
926 gen_compute_eflags(s
);
932 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
935 return (CCPrepare
) { .cond
= TCG_COND_ALWAYS
, .mask
= -1 };
938 TCGMemOp size
= (s
->cc_op
- CC_OP_ADDB
) & 3;
939 TCGv t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, false);
940 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= t0
, .mask
= -1 };
945 /* perform a conditional store into register 'reg' according to jump opcode
946 value 'b'. In the fast case, T0 is guaranted not to be used. */
947 static CCPrepare
gen_prepare_cc(DisasContext
*s
, int b
, TCGv reg
)
949 int inv
, jcc_op
, cond
;
955 jcc_op
= (b
>> 1) & 7;
958 case CC_OP_SUBB
... CC_OP_SUBQ
:
959 /* We optimize relational operators for the cmp/jcc case. */
960 size
= s
->cc_op
- CC_OP_SUBB
;
963 tcg_gen_mov_tl(cpu_tmp4
, cpu_cc_srcT
);
964 gen_extu(size
, cpu_tmp4
);
965 t0
= gen_ext_tl(cpu_tmp0
, cpu_cc_src
, size
, false);
966 cc
= (CCPrepare
) { .cond
= TCG_COND_LEU
, .reg
= cpu_tmp4
,
967 .reg2
= t0
, .mask
= -1, .use_reg2
= true };
976 tcg_gen_mov_tl(cpu_tmp4
, cpu_cc_srcT
);
977 gen_exts(size
, cpu_tmp4
);
978 t0
= gen_ext_tl(cpu_tmp0
, cpu_cc_src
, size
, true);
979 cc
= (CCPrepare
) { .cond
= cond
, .reg
= cpu_tmp4
,
980 .reg2
= t0
, .mask
= -1, .use_reg2
= true };
990 /* This actually generates good code for JC, JZ and JS. */
993 cc
= gen_prepare_eflags_o(s
, reg
);
996 cc
= gen_prepare_eflags_c(s
, reg
);
999 cc
= gen_prepare_eflags_z(s
, reg
);
1002 gen_compute_eflags(s
);
1003 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
1004 .mask
= CC_Z
| CC_C
};
1007 cc
= gen_prepare_eflags_s(s
, reg
);
1010 cc
= gen_prepare_eflags_p(s
, reg
);
1013 gen_compute_eflags(s
);
1014 if (TCGV_EQUAL(reg
, cpu_cc_src
)) {
1017 tcg_gen_shri_tl(reg
, cpu_cc_src
, 4); /* CC_O -> CC_S */
1018 tcg_gen_xor_tl(reg
, reg
, cpu_cc_src
);
1019 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
1024 gen_compute_eflags(s
);
1025 if (TCGV_EQUAL(reg
, cpu_cc_src
)) {
1028 tcg_gen_shri_tl(reg
, cpu_cc_src
, 4); /* CC_O -> CC_S */
1029 tcg_gen_xor_tl(reg
, reg
, cpu_cc_src
);
1030 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
1031 .mask
= CC_S
| CC_Z
};
1038 cc
.cond
= tcg_invert_cond(cc
.cond
);
1043 static void gen_setcc1(DisasContext
*s
, int b
, TCGv reg
)
1045 CCPrepare cc
= gen_prepare_cc(s
, b
, reg
);
1047 if (cc
.no_setcond
) {
1048 if (cc
.cond
== TCG_COND_EQ
) {
1049 tcg_gen_xori_tl(reg
, cc
.reg
, 1);
1051 tcg_gen_mov_tl(reg
, cc
.reg
);
1056 if (cc
.cond
== TCG_COND_NE
&& !cc
.use_reg2
&& cc
.imm
== 0 &&
1057 cc
.mask
!= 0 && (cc
.mask
& (cc
.mask
- 1)) == 0) {
1058 tcg_gen_shri_tl(reg
, cc
.reg
, ctztl(cc
.mask
));
1059 tcg_gen_andi_tl(reg
, reg
, 1);
1062 if (cc
.mask
!= -1) {
1063 tcg_gen_andi_tl(reg
, cc
.reg
, cc
.mask
);
1067 tcg_gen_setcond_tl(cc
.cond
, reg
, cc
.reg
, cc
.reg2
);
1069 tcg_gen_setcondi_tl(cc
.cond
, reg
, cc
.reg
, cc
.imm
);
1073 static inline void gen_compute_eflags_c(DisasContext
*s
, TCGv reg
)
1075 gen_setcc1(s
, JCC_B
<< 1, reg
);
1078 /* generate a conditional jump to label 'l1' according to jump opcode
1079 value 'b'. In the fast case, T0 is guaranted not to be used. */
1080 static inline void gen_jcc1_noeob(DisasContext
*s
, int b
, int l1
)
1082 CCPrepare cc
= gen_prepare_cc(s
, b
, cpu_T
[0]);
1084 if (cc
.mask
!= -1) {
1085 tcg_gen_andi_tl(cpu_T
[0], cc
.reg
, cc
.mask
);
1089 tcg_gen_brcond_tl(cc
.cond
, cc
.reg
, cc
.reg2
, l1
);
1091 tcg_gen_brcondi_tl(cc
.cond
, cc
.reg
, cc
.imm
, l1
);
1095 /* Generate a conditional jump to label 'l1' according to jump opcode
1096 value 'b'. In the fast case, T0 is guaranted not to be used.
1097 A translation block must end soon. */
1098 static inline void gen_jcc1(DisasContext
*s
, int b
, int l1
)
1100 CCPrepare cc
= gen_prepare_cc(s
, b
, cpu_T
[0]);
1102 gen_update_cc_op(s
);
1103 if (cc
.mask
!= -1) {
1104 tcg_gen_andi_tl(cpu_T
[0], cc
.reg
, cc
.mask
);
1107 set_cc_op(s
, CC_OP_DYNAMIC
);
1109 tcg_gen_brcond_tl(cc
.cond
, cc
.reg
, cc
.reg2
, l1
);
1111 tcg_gen_brcondi_tl(cc
.cond
, cc
.reg
, cc
.imm
, l1
);
1115 /* XXX: does not work with gdbstub "ice" single step - not a
1117 static int gen_jz_ecx_string(DisasContext
*s
, target_ulong next_eip
)
1121 l1
= gen_new_label();
1122 l2
= gen_new_label();
1123 gen_op_jnz_ecx(s
->aflag
, l1
);
1125 gen_jmp_tb(s
, next_eip
, 1);
1130 static inline void gen_stos(DisasContext
*s
, TCGMemOp ot
)
1132 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EAX
);
1133 gen_string_movl_A0_EDI(s
);
1134 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
1135 gen_op_movl_T0_Dshift(ot
);
1136 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
1139 static inline void gen_lods(DisasContext
*s
, TCGMemOp ot
)
1141 gen_string_movl_A0_ESI(s
);
1142 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1143 gen_op_mov_reg_v(ot
, R_EAX
, cpu_T
[0]);
1144 gen_op_movl_T0_Dshift(ot
);
1145 gen_op_add_reg_T0(s
->aflag
, R_ESI
);
1148 static inline void gen_scas(DisasContext
*s
, TCGMemOp ot
)
1150 gen_string_movl_A0_EDI(s
);
1151 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
1152 gen_op(s
, OP_CMPL
, ot
, R_EAX
);
1153 gen_op_movl_T0_Dshift(ot
);
1154 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
1157 static inline void gen_cmps(DisasContext
*s
, TCGMemOp ot
)
1159 gen_string_movl_A0_EDI(s
);
1160 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
1161 gen_string_movl_A0_ESI(s
);
1162 gen_op(s
, OP_CMPL
, ot
, OR_TMP0
);
1163 gen_op_movl_T0_Dshift(ot
);
1164 gen_op_add_reg_T0(s
->aflag
, R_ESI
);
1165 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
1168 static inline void gen_ins(DisasContext
*s
, TCGMemOp ot
)
1172 gen_string_movl_A0_EDI(s
);
1173 /* Note: we must do this dummy write first to be restartable in
1174 case of page fault. */
1175 tcg_gen_movi_tl(cpu_T
[0], 0);
1176 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
1177 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_regs
[R_EDX
]);
1178 tcg_gen_andi_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, 0xffff);
1179 gen_helper_in_func(ot
, cpu_T
[0], cpu_tmp2_i32
);
1180 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
1181 gen_op_movl_T0_Dshift(ot
);
1182 gen_op_add_reg_T0(s
->aflag
, R_EDI
);
1187 static inline void gen_outs(DisasContext
*s
, TCGMemOp ot
)
1191 gen_string_movl_A0_ESI(s
);
1192 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1194 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_regs
[R_EDX
]);
1195 tcg_gen_andi_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, 0xffff);
1196 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[0]);
1197 gen_helper_out_func(ot
, cpu_tmp2_i32
, cpu_tmp3_i32
);
1199 gen_op_movl_T0_Dshift(ot
);
1200 gen_op_add_reg_T0(s
->aflag
, R_ESI
);
1205 /* same method as Valgrind : we generate jumps to current or next
1207 #define GEN_REPZ(op) \
1208 static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
1209 target_ulong cur_eip, target_ulong next_eip) \
1212 gen_update_cc_op(s); \
1213 l2 = gen_jz_ecx_string(s, next_eip); \
1214 gen_ ## op(s, ot); \
1215 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
1216 /* a loop would cause two single step exceptions if ECX = 1 \
1217 before rep string_insn */ \
1219 gen_op_jz_ecx(s->aflag, l2); \
1220 gen_jmp(s, cur_eip); \
1223 #define GEN_REPZ2(op) \
1224 static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
1225 target_ulong cur_eip, \
1226 target_ulong next_eip, \
1230 gen_update_cc_op(s); \
1231 l2 = gen_jz_ecx_string(s, next_eip); \
1232 gen_ ## op(s, ot); \
1233 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
1234 gen_update_cc_op(s); \
1235 gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2); \
1237 gen_op_jz_ecx(s->aflag, l2); \
1238 gen_jmp(s, cur_eip); \
1249 static void gen_helper_fp_arith_ST0_FT0(int op
)
1253 gen_helper_fadd_ST0_FT0(cpu_env
);
1256 gen_helper_fmul_ST0_FT0(cpu_env
);
1259 gen_helper_fcom_ST0_FT0(cpu_env
);
1262 gen_helper_fcom_ST0_FT0(cpu_env
);
1265 gen_helper_fsub_ST0_FT0(cpu_env
);
1268 gen_helper_fsubr_ST0_FT0(cpu_env
);
1271 gen_helper_fdiv_ST0_FT0(cpu_env
);
1274 gen_helper_fdivr_ST0_FT0(cpu_env
);
1279 /* NOTE the exception in "r" op ordering */
1280 static void gen_helper_fp_arith_STN_ST0(int op
, int opreg
)
1282 TCGv_i32 tmp
= tcg_const_i32(opreg
);
1285 gen_helper_fadd_STN_ST0(cpu_env
, tmp
);
1288 gen_helper_fmul_STN_ST0(cpu_env
, tmp
);
1291 gen_helper_fsubr_STN_ST0(cpu_env
, tmp
);
1294 gen_helper_fsub_STN_ST0(cpu_env
, tmp
);
1297 gen_helper_fdivr_STN_ST0(cpu_env
, tmp
);
1300 gen_helper_fdiv_STN_ST0(cpu_env
, tmp
);
1305 /* if d == OR_TMP0, it means memory operand (address in A0) */
1306 static void gen_op(DisasContext
*s1
, int op
, TCGMemOp ot
, int d
)
1309 gen_op_mov_v_reg(ot
, cpu_T
[0], d
);
1311 gen_op_ld_v(s1
, ot
, cpu_T
[0], cpu_A0
);
1315 gen_compute_eflags_c(s1
, cpu_tmp4
);
1316 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1317 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_tmp4
);
1318 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1319 gen_op_update3_cc(cpu_tmp4
);
1320 set_cc_op(s1
, CC_OP_ADCB
+ ot
);
1323 gen_compute_eflags_c(s1
, cpu_tmp4
);
1324 tcg_gen_sub_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1325 tcg_gen_sub_tl(cpu_T
[0], cpu_T
[0], cpu_tmp4
);
1326 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1327 gen_op_update3_cc(cpu_tmp4
);
1328 set_cc_op(s1
, CC_OP_SBBB
+ ot
);
1331 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1332 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1333 gen_op_update2_cc();
1334 set_cc_op(s1
, CC_OP_ADDB
+ ot
);
1337 tcg_gen_mov_tl(cpu_cc_srcT
, cpu_T
[0]);
1338 tcg_gen_sub_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1339 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1340 gen_op_update2_cc();
1341 set_cc_op(s1
, CC_OP_SUBB
+ ot
);
1345 tcg_gen_and_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1346 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1347 gen_op_update1_cc();
1348 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1351 tcg_gen_or_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1352 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1353 gen_op_update1_cc();
1354 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1357 tcg_gen_xor_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1358 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1359 gen_op_update1_cc();
1360 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1363 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[1]);
1364 tcg_gen_mov_tl(cpu_cc_srcT
, cpu_T
[0]);
1365 tcg_gen_sub_tl(cpu_cc_dst
, cpu_T
[0], cpu_T
[1]);
1366 set_cc_op(s1
, CC_OP_SUBB
+ ot
);
1371 /* if d == OR_TMP0, it means memory operand (address in A0) */
1372 static void gen_inc(DisasContext
*s1
, TCGMemOp ot
, int d
, int c
)
1375 gen_op_mov_v_reg(ot
, cpu_T
[0], d
);
1377 gen_op_ld_v(s1
, ot
, cpu_T
[0], cpu_A0
);
1379 gen_compute_eflags_c(s1
, cpu_cc_src
);
1381 tcg_gen_addi_tl(cpu_T
[0], cpu_T
[0], 1);
1382 set_cc_op(s1
, CC_OP_INCB
+ ot
);
1384 tcg_gen_addi_tl(cpu_T
[0], cpu_T
[0], -1);
1385 set_cc_op(s1
, CC_OP_DECB
+ ot
);
1387 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1388 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
1391 static void gen_shift_flags(DisasContext
*s
, TCGMemOp ot
, TCGv result
,
1392 TCGv shm1
, TCGv count
, bool is_right
)
1394 TCGv_i32 z32
, s32
, oldop
;
1397 /* Store the results into the CC variables. If we know that the
1398 variable must be dead, store unconditionally. Otherwise we'll
1399 need to not disrupt the current contents. */
1400 z_tl
= tcg_const_tl(0);
1401 if (cc_op_live
[s
->cc_op
] & USES_CC_DST
) {
1402 tcg_gen_movcond_tl(TCG_COND_NE
, cpu_cc_dst
, count
, z_tl
,
1403 result
, cpu_cc_dst
);
1405 tcg_gen_mov_tl(cpu_cc_dst
, result
);
1407 if (cc_op_live
[s
->cc_op
] & USES_CC_SRC
) {
1408 tcg_gen_movcond_tl(TCG_COND_NE
, cpu_cc_src
, count
, z_tl
,
1411 tcg_gen_mov_tl(cpu_cc_src
, shm1
);
1413 tcg_temp_free(z_tl
);
1415 /* Get the two potential CC_OP values into temporaries. */
1416 tcg_gen_movi_i32(cpu_tmp2_i32
, (is_right
? CC_OP_SARB
: CC_OP_SHLB
) + ot
);
1417 if (s
->cc_op
== CC_OP_DYNAMIC
) {
1420 tcg_gen_movi_i32(cpu_tmp3_i32
, s
->cc_op
);
1421 oldop
= cpu_tmp3_i32
;
1424 /* Conditionally store the CC_OP value. */
1425 z32
= tcg_const_i32(0);
1426 s32
= tcg_temp_new_i32();
1427 tcg_gen_trunc_tl_i32(s32
, count
);
1428 tcg_gen_movcond_i32(TCG_COND_NE
, cpu_cc_op
, s32
, z32
, cpu_tmp2_i32
, oldop
);
1429 tcg_temp_free_i32(z32
);
1430 tcg_temp_free_i32(s32
);
1432 /* The CC_OP value is no longer predictable. */
1433 set_cc_op(s
, CC_OP_DYNAMIC
);
1436 static void gen_shift_rm_T1(DisasContext
*s
, TCGMemOp ot
, int op1
,
1437 int is_right
, int is_arith
)
1439 target_ulong mask
= (ot
== MO_64
? 0x3f : 0x1f);
1442 if (op1
== OR_TMP0
) {
1443 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1445 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1448 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], mask
);
1449 tcg_gen_subi_tl(cpu_tmp0
, cpu_T
[1], 1);
1453 gen_exts(ot
, cpu_T
[0]);
1454 tcg_gen_sar_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1455 tcg_gen_sar_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1457 gen_extu(ot
, cpu_T
[0]);
1458 tcg_gen_shr_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1459 tcg_gen_shr_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1462 tcg_gen_shl_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1463 tcg_gen_shl_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1467 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1469 gen_shift_flags(s
, ot
, cpu_T
[0], cpu_tmp0
, cpu_T
[1], is_right
);
1472 static void gen_shift_rm_im(DisasContext
*s
, TCGMemOp ot
, int op1
, int op2
,
1473 int is_right
, int is_arith
)
1475 int mask
= (ot
== MO_64
? 0x3f : 0x1f);
1479 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1481 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1487 gen_exts(ot
, cpu_T
[0]);
1488 tcg_gen_sari_tl(cpu_tmp4
, cpu_T
[0], op2
- 1);
1489 tcg_gen_sari_tl(cpu_T
[0], cpu_T
[0], op2
);
1491 gen_extu(ot
, cpu_T
[0]);
1492 tcg_gen_shri_tl(cpu_tmp4
, cpu_T
[0], op2
- 1);
1493 tcg_gen_shri_tl(cpu_T
[0], cpu_T
[0], op2
);
1496 tcg_gen_shli_tl(cpu_tmp4
, cpu_T
[0], op2
- 1);
1497 tcg_gen_shli_tl(cpu_T
[0], cpu_T
[0], op2
);
1502 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1504 /* update eflags if non zero shift */
1506 tcg_gen_mov_tl(cpu_cc_src
, cpu_tmp4
);
1507 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
1508 set_cc_op(s
, (is_right
? CC_OP_SARB
: CC_OP_SHLB
) + ot
);
1512 static void gen_rot_rm_T1(DisasContext
*s
, TCGMemOp ot
, int op1
, int is_right
)
1514 target_ulong mask
= (ot
== MO_64
? 0x3f : 0x1f);
1518 if (op1
== OR_TMP0
) {
1519 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1521 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1524 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], mask
);
1528 /* Replicate the 8-bit input so that a 32-bit rotate works. */
1529 tcg_gen_ext8u_tl(cpu_T
[0], cpu_T
[0]);
1530 tcg_gen_muli_tl(cpu_T
[0], cpu_T
[0], 0x01010101);
1533 /* Replicate the 16-bit input so that a 32-bit rotate works. */
1534 tcg_gen_deposit_tl(cpu_T
[0], cpu_T
[0], cpu_T
[0], 16, 16);
1537 #ifdef TARGET_X86_64
1539 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
1540 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[1]);
1542 tcg_gen_rotr_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, cpu_tmp3_i32
);
1544 tcg_gen_rotl_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, cpu_tmp3_i32
);
1546 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
1551 tcg_gen_rotr_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1553 tcg_gen_rotl_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1559 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1561 /* We'll need the flags computed into CC_SRC. */
1562 gen_compute_eflags(s
);
1564 /* The value that was "rotated out" is now present at the other end
1565 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1566 since we've computed the flags into CC_SRC, these variables are
1569 tcg_gen_shri_tl(cpu_cc_src2
, cpu_T
[0], mask
- 1);
1570 tcg_gen_shri_tl(cpu_cc_dst
, cpu_T
[0], mask
);
1571 tcg_gen_andi_tl(cpu_cc_dst
, cpu_cc_dst
, 1);
1573 tcg_gen_shri_tl(cpu_cc_src2
, cpu_T
[0], mask
);
1574 tcg_gen_andi_tl(cpu_cc_dst
, cpu_T
[0], 1);
1576 tcg_gen_andi_tl(cpu_cc_src2
, cpu_cc_src2
, 1);
1577 tcg_gen_xor_tl(cpu_cc_src2
, cpu_cc_src2
, cpu_cc_dst
);
1579 /* Now conditionally store the new CC_OP value. If the shift count
1580 is 0 we keep the CC_OP_EFLAGS setting so that only CC_SRC is live.
1581 Otherwise reuse CC_OP_ADCOX which have the C and O flags split out
1582 exactly as we computed above. */
1583 t0
= tcg_const_i32(0);
1584 t1
= tcg_temp_new_i32();
1585 tcg_gen_trunc_tl_i32(t1
, cpu_T
[1]);
1586 tcg_gen_movi_i32(cpu_tmp2_i32
, CC_OP_ADCOX
);
1587 tcg_gen_movi_i32(cpu_tmp3_i32
, CC_OP_EFLAGS
);
1588 tcg_gen_movcond_i32(TCG_COND_NE
, cpu_cc_op
, t1
, t0
,
1589 cpu_tmp2_i32
, cpu_tmp3_i32
);
1590 tcg_temp_free_i32(t0
);
1591 tcg_temp_free_i32(t1
);
1593 /* The CC_OP value is no longer predictable. */
1594 set_cc_op(s
, CC_OP_DYNAMIC
);
1597 static void gen_rot_rm_im(DisasContext
*s
, TCGMemOp ot
, int op1
, int op2
,
1600 int mask
= (ot
== MO_64
? 0x3f : 0x1f);
1604 if (op1
== OR_TMP0
) {
1605 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1607 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1613 #ifdef TARGET_X86_64
1615 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
1617 tcg_gen_rotri_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, op2
);
1619 tcg_gen_rotli_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, op2
);
1621 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
1626 tcg_gen_rotri_tl(cpu_T
[0], cpu_T
[0], op2
);
1628 tcg_gen_rotli_tl(cpu_T
[0], cpu_T
[0], op2
);
1639 shift
= mask
+ 1 - shift
;
1641 gen_extu(ot
, cpu_T
[0]);
1642 tcg_gen_shli_tl(cpu_tmp0
, cpu_T
[0], shift
);
1643 tcg_gen_shri_tl(cpu_T
[0], cpu_T
[0], mask
+ 1 - shift
);
1644 tcg_gen_or_tl(cpu_T
[0], cpu_T
[0], cpu_tmp0
);
1650 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1653 /* Compute the flags into CC_SRC. */
1654 gen_compute_eflags(s
);
1656 /* The value that was "rotated out" is now present at the other end
1657 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1658 since we've computed the flags into CC_SRC, these variables are
1661 tcg_gen_shri_tl(cpu_cc_src2
, cpu_T
[0], mask
- 1);
1662 tcg_gen_shri_tl(cpu_cc_dst
, cpu_T
[0], mask
);
1663 tcg_gen_andi_tl(cpu_cc_dst
, cpu_cc_dst
, 1);
1665 tcg_gen_shri_tl(cpu_cc_src2
, cpu_T
[0], mask
);
1666 tcg_gen_andi_tl(cpu_cc_dst
, cpu_T
[0], 1);
1668 tcg_gen_andi_tl(cpu_cc_src2
, cpu_cc_src2
, 1);
1669 tcg_gen_xor_tl(cpu_cc_src2
, cpu_cc_src2
, cpu_cc_dst
);
1670 set_cc_op(s
, CC_OP_ADCOX
);
1674 /* XXX: add faster immediate = 1 case */
1675 static void gen_rotc_rm_T1(DisasContext
*s
, TCGMemOp ot
, int op1
,
1678 gen_compute_eflags(s
);
1679 assert(s
->cc_op
== CC_OP_EFLAGS
);
1683 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1685 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1690 gen_helper_rcrb(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1693 gen_helper_rcrw(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1696 gen_helper_rcrl(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1698 #ifdef TARGET_X86_64
1700 gen_helper_rcrq(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1709 gen_helper_rclb(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1712 gen_helper_rclw(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1715 gen_helper_rcll(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1717 #ifdef TARGET_X86_64
1719 gen_helper_rclq(cpu_T
[0], cpu_env
, cpu_T
[0], cpu_T
[1]);
1727 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1730 /* XXX: add faster immediate case */
1731 static void gen_shiftd_rm_T1(DisasContext
*s
, TCGMemOp ot
, int op1
,
1732 bool is_right
, TCGv count_in
)
1734 target_ulong mask
= (ot
== MO_64
? 63 : 31);
1738 if (op1
== OR_TMP0
) {
1739 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
1741 gen_op_mov_v_reg(ot
, cpu_T
[0], op1
);
1744 count
= tcg_temp_new();
1745 tcg_gen_andi_tl(count
, count_in
, mask
);
1749 /* Note: we implement the Intel behaviour for shift count > 16.
1750 This means "shrdw C, B, A" shifts A:B:A >> C. Build the B:A
1751 portion by constructing it as a 32-bit value. */
1753 tcg_gen_deposit_tl(cpu_tmp0
, cpu_T
[0], cpu_T
[1], 16, 16);
1754 tcg_gen_mov_tl(cpu_T
[1], cpu_T
[0]);
1755 tcg_gen_mov_tl(cpu_T
[0], cpu_tmp0
);
1757 tcg_gen_deposit_tl(cpu_T
[1], cpu_T
[0], cpu_T
[1], 16, 16);
1760 #ifdef TARGET_X86_64
1762 /* Concatenate the two 32-bit values and use a 64-bit shift. */
1763 tcg_gen_subi_tl(cpu_tmp0
, count
, 1);
1765 tcg_gen_concat_tl_i64(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1766 tcg_gen_shr_i64(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1767 tcg_gen_shr_i64(cpu_T
[0], cpu_T
[0], count
);
1769 tcg_gen_concat_tl_i64(cpu_T
[0], cpu_T
[1], cpu_T
[0]);
1770 tcg_gen_shl_i64(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1771 tcg_gen_shl_i64(cpu_T
[0], cpu_T
[0], count
);
1772 tcg_gen_shri_i64(cpu_tmp0
, cpu_tmp0
, 32);
1773 tcg_gen_shri_i64(cpu_T
[0], cpu_T
[0], 32);
1778 tcg_gen_subi_tl(cpu_tmp0
, count
, 1);
1780 tcg_gen_shr_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1782 tcg_gen_subfi_tl(cpu_tmp4
, mask
+ 1, count
);
1783 tcg_gen_shr_tl(cpu_T
[0], cpu_T
[0], count
);
1784 tcg_gen_shl_tl(cpu_T
[1], cpu_T
[1], cpu_tmp4
);
1786 tcg_gen_shl_tl(cpu_tmp0
, cpu_T
[0], cpu_tmp0
);
1788 /* Only needed if count > 16, for Intel behaviour. */
1789 tcg_gen_subfi_tl(cpu_tmp4
, 33, count
);
1790 tcg_gen_shr_tl(cpu_tmp4
, cpu_T
[1], cpu_tmp4
);
1791 tcg_gen_or_tl(cpu_tmp0
, cpu_tmp0
, cpu_tmp4
);
1794 tcg_gen_subfi_tl(cpu_tmp4
, mask
+ 1, count
);
1795 tcg_gen_shl_tl(cpu_T
[0], cpu_T
[0], count
);
1796 tcg_gen_shr_tl(cpu_T
[1], cpu_T
[1], cpu_tmp4
);
1798 tcg_gen_movi_tl(cpu_tmp4
, 0);
1799 tcg_gen_movcond_tl(TCG_COND_EQ
, cpu_T
[1], count
, cpu_tmp4
,
1800 cpu_tmp4
, cpu_T
[1]);
1801 tcg_gen_or_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
1806 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1808 gen_shift_flags(s
, ot
, cpu_T
[0], cpu_tmp0
, count
, is_right
);
1809 tcg_temp_free(count
);
1812 static void gen_shift(DisasContext
*s1
, int op
, TCGMemOp ot
, int d
, int s
)
1815 gen_op_mov_v_reg(ot
, cpu_T
[1], s
);
1818 gen_rot_rm_T1(s1
, ot
, d
, 0);
1821 gen_rot_rm_T1(s1
, ot
, d
, 1);
1825 gen_shift_rm_T1(s1
, ot
, d
, 0, 0);
1828 gen_shift_rm_T1(s1
, ot
, d
, 1, 0);
1831 gen_shift_rm_T1(s1
, ot
, d
, 1, 1);
1834 gen_rotc_rm_T1(s1
, ot
, d
, 0);
1837 gen_rotc_rm_T1(s1
, ot
, d
, 1);
1842 static void gen_shifti(DisasContext
*s1
, int op
, TCGMemOp ot
, int d
, int c
)
1846 gen_rot_rm_im(s1
, ot
, d
, c
, 0);
1849 gen_rot_rm_im(s1
, ot
, d
, c
, 1);
1853 gen_shift_rm_im(s1
, ot
, d
, c
, 0, 0);
1856 gen_shift_rm_im(s1
, ot
, d
, c
, 1, 0);
1859 gen_shift_rm_im(s1
, ot
, d
, c
, 1, 1);
1862 /* currently not optimized */
1863 tcg_gen_movi_tl(cpu_T
[1], c
);
1864 gen_shift(s1
, op
, ot
, d
, OR_TMP1
);
1869 static void gen_lea_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
)
1876 int mod
, rm
, code
, override
, must_add_seg
;
1879 override
= s
->override
;
1880 must_add_seg
= s
->addseg
;
1883 mod
= (modrm
>> 6) & 3;
1896 code
= cpu_ldub_code(env
, s
->pc
++);
1897 scale
= (code
>> 6) & 3;
1898 index
= ((code
>> 3) & 7) | REX_X(s
);
1900 index
= -1; /* no index */
1908 if ((base
& 7) == 5) {
1910 disp
= (int32_t)cpu_ldl_code(env
, s
->pc
);
1912 if (CODE64(s
) && !havesib
) {
1913 disp
+= s
->pc
+ s
->rip_offset
;
1920 disp
= (int8_t)cpu_ldub_code(env
, s
->pc
++);
1924 disp
= (int32_t)cpu_ldl_code(env
, s
->pc
);
1929 /* For correct popl handling with esp. */
1930 if (base
== R_ESP
&& s
->popl_esp_hack
) {
1931 disp
+= s
->popl_esp_hack
;
1934 /* Compute the address, with a minimum number of TCG ops. */
1938 sum
= cpu_regs
[index
];
1940 tcg_gen_shli_tl(cpu_A0
, cpu_regs
[index
], scale
);
1944 tcg_gen_add_tl(cpu_A0
, sum
, cpu_regs
[base
]);
1947 } else if (base
>= 0) {
1948 sum
= cpu_regs
[base
];
1950 if (TCGV_IS_UNUSED(sum
)) {
1951 tcg_gen_movi_tl(cpu_A0
, disp
);
1953 tcg_gen_addi_tl(cpu_A0
, sum
, disp
);
1958 if (base
== R_EBP
|| base
== R_ESP
) {
1965 tcg_gen_ld_tl(cpu_tmp0
, cpu_env
,
1966 offsetof(CPUX86State
, segs
[override
].base
));
1968 if (s
->aflag
== MO_32
) {
1969 tcg_gen_ext32u_tl(cpu_A0
, cpu_A0
);
1971 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
1975 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
1978 if (s
->aflag
== MO_32
) {
1979 tcg_gen_ext32u_tl(cpu_A0
, cpu_A0
);
1987 disp
= cpu_lduw_code(env
, s
->pc
);
1989 tcg_gen_movi_tl(cpu_A0
, disp
);
1990 rm
= 0; /* avoid SS override */
1997 disp
= (int8_t)cpu_ldub_code(env
, s
->pc
++);
2001 disp
= (int16_t)cpu_lduw_code(env
, s
->pc
);
2009 tcg_gen_add_tl(cpu_A0
, cpu_regs
[R_EBX
], cpu_regs
[R_ESI
]);
2012 tcg_gen_add_tl(cpu_A0
, cpu_regs
[R_EBX
], cpu_regs
[R_EDI
]);
2015 tcg_gen_add_tl(cpu_A0
, cpu_regs
[R_EBP
], cpu_regs
[R_ESI
]);
2018 tcg_gen_add_tl(cpu_A0
, cpu_regs
[R_EBP
], cpu_regs
[R_EDI
]);
2021 sum
= cpu_regs
[R_ESI
];
2024 sum
= cpu_regs
[R_EDI
];
2027 sum
= cpu_regs
[R_EBP
];
2031 sum
= cpu_regs
[R_EBX
];
2034 tcg_gen_addi_tl(cpu_A0
, sum
, disp
);
2035 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2039 if (rm
== 2 || rm
== 3 || rm
== 6) {
2045 gen_op_addl_A0_seg(s
, override
);
2054 static void gen_nop_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
)
2056 int mod
, rm
, base
, code
;
2058 mod
= (modrm
>> 6) & 3;
2069 code
= cpu_ldub_code(env
, s
->pc
++);
2111 /* used for LEA and MOV AX, mem */
2112 static void gen_add_A0_ds_seg(DisasContext
*s
)
2114 int override
, must_add_seg
;
2115 must_add_seg
= s
->addseg
;
2117 if (s
->override
>= 0) {
2118 override
= s
->override
;
2122 #ifdef TARGET_X86_64
2124 gen_op_addq_A0_seg(override
);
2128 gen_op_addl_A0_seg(s
, override
);
2133 /* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
2135 static void gen_ldst_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
,
2136 TCGMemOp ot
, int reg
, int is_store
)
2140 mod
= (modrm
>> 6) & 3;
2141 rm
= (modrm
& 7) | REX_B(s
);
2145 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
2146 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
2148 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
2150 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
2153 gen_lea_modrm(env
, s
, modrm
);
2156 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
2157 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
2159 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
2161 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
2166 static inline uint32_t insn_get(CPUX86State
*env
, DisasContext
*s
, TCGMemOp ot
)
2172 ret
= cpu_ldub_code(env
, s
->pc
);
2176 ret
= cpu_lduw_code(env
, s
->pc
);
2180 #ifdef TARGET_X86_64
2183 ret
= cpu_ldl_code(env
, s
->pc
);
2192 static inline int insn_const_size(TCGMemOp ot
)
2201 static inline void gen_goto_tb(DisasContext
*s
, int tb_num
, target_ulong eip
)
2203 TranslationBlock
*tb
;
2206 pc
= s
->cs_base
+ eip
;
2208 /* NOTE: we handle the case where the TB spans two pages here */
2209 if ((pc
& TARGET_PAGE_MASK
) == (tb
->pc
& TARGET_PAGE_MASK
) ||
2210 (pc
& TARGET_PAGE_MASK
) == ((s
->pc
- 1) & TARGET_PAGE_MASK
)) {
2211 /* jump to same page: we can use a direct jump */
2212 tcg_gen_goto_tb(tb_num
);
2214 tcg_gen_exit_tb((uintptr_t)tb
+ tb_num
);
2216 /* jump to another page: currently not optimized */
2222 static inline void gen_jcc(DisasContext
*s
, int b
,
2223 target_ulong val
, target_ulong next_eip
)
2228 l1
= gen_new_label();
2231 gen_goto_tb(s
, 0, next_eip
);
2234 gen_goto_tb(s
, 1, val
);
2235 s
->is_jmp
= DISAS_TB_JUMP
;
2237 l1
= gen_new_label();
2238 l2
= gen_new_label();
2241 gen_jmp_im(next_eip
);
2251 static void gen_cmovcc1(CPUX86State
*env
, DisasContext
*s
, TCGMemOp ot
, int b
,
2256 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
2258 cc
= gen_prepare_cc(s
, b
, cpu_T
[1]);
2259 if (cc
.mask
!= -1) {
2260 TCGv t0
= tcg_temp_new();
2261 tcg_gen_andi_tl(t0
, cc
.reg
, cc
.mask
);
2265 cc
.reg2
= tcg_const_tl(cc
.imm
);
2268 tcg_gen_movcond_tl(cc
.cond
, cpu_T
[0], cc
.reg
, cc
.reg2
,
2269 cpu_T
[0], cpu_regs
[reg
]);
2270 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
2272 if (cc
.mask
!= -1) {
2273 tcg_temp_free(cc
.reg
);
2276 tcg_temp_free(cc
.reg2
);
2280 static inline void gen_op_movl_T0_seg(int seg_reg
)
2282 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
,
2283 offsetof(CPUX86State
,segs
[seg_reg
].selector
));
2286 static inline void gen_op_movl_seg_T0_vm(int seg_reg
)
2288 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 0xffff);
2289 tcg_gen_st32_tl(cpu_T
[0], cpu_env
,
2290 offsetof(CPUX86State
,segs
[seg_reg
].selector
));
2291 tcg_gen_shli_tl(cpu_T
[0], cpu_T
[0], 4);
2292 tcg_gen_st_tl(cpu_T
[0], cpu_env
,
2293 offsetof(CPUX86State
,segs
[seg_reg
].base
));
2296 /* move T0 to seg_reg and compute if the CPU state may change. Never
2297 call this function with seg_reg == R_CS */
2298 static void gen_movl_seg_T0(DisasContext
*s
, int seg_reg
, target_ulong cur_eip
)
2300 if (s
->pe
&& !s
->vm86
) {
2301 /* XXX: optimize by finding processor state dynamically */
2302 gen_update_cc_op(s
);
2303 gen_jmp_im(cur_eip
);
2304 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
2305 gen_helper_load_seg(cpu_env
, tcg_const_i32(seg_reg
), cpu_tmp2_i32
);
2306 /* abort translation because the addseg value may change or
2307 because ss32 may change. For R_SS, translation must always
2308 stop as a special handling must be done to disable hardware
2309 interrupts for the next instruction */
2310 if (seg_reg
== R_SS
|| (s
->code32
&& seg_reg
< R_FS
))
2311 s
->is_jmp
= DISAS_TB_JUMP
;
2313 gen_op_movl_seg_T0_vm(seg_reg
);
2314 if (seg_reg
== R_SS
)
2315 s
->is_jmp
= DISAS_TB_JUMP
;
2319 static inline int svm_is_rep(int prefixes
)
2321 return ((prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) ? 8 : 0);
2325 gen_svm_check_intercept_param(DisasContext
*s
, target_ulong pc_start
,
2326 uint32_t type
, uint64_t param
)
2328 /* no SVM activated; fast case */
2329 if (likely(!(s
->flags
& HF_SVMI_MASK
)))
2331 gen_update_cc_op(s
);
2332 gen_jmp_im(pc_start
- s
->cs_base
);
2333 gen_helper_svm_check_intercept_param(cpu_env
, tcg_const_i32(type
),
2334 tcg_const_i64(param
));
2338 gen_svm_check_intercept(DisasContext
*s
, target_ulong pc_start
, uint64_t type
)
2340 gen_svm_check_intercept_param(s
, pc_start
, type
, 0);
2343 static inline void gen_stack_update(DisasContext
*s
, int addend
)
2345 #ifdef TARGET_X86_64
2347 gen_op_add_reg_im(MO_64
, R_ESP
, addend
);
2351 gen_op_add_reg_im(MO_32
, R_ESP
, addend
);
2353 gen_op_add_reg_im(MO_16
, R_ESP
, addend
);
2357 /* Generate a push. It depends on ss32, addseg and dflag. */
2358 static void gen_push_v(DisasContext
*s
, TCGv val
)
2360 TCGMemOp a_ot
, d_ot
= mo_pushpop(s
, s
->dflag
);
2361 int size
= 1 << d_ot
;
2362 TCGv new_esp
= cpu_A0
;
2364 tcg_gen_subi_tl(cpu_A0
, cpu_regs
[R_ESP
], size
);
2368 } else if (s
->ss32
) {
2372 tcg_gen_mov_tl(new_esp
, cpu_A0
);
2373 gen_op_addl_A0_seg(s
, R_SS
);
2375 tcg_gen_ext32u_tl(cpu_A0
, cpu_A0
);
2380 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2381 tcg_gen_mov_tl(new_esp
, cpu_A0
);
2382 gen_op_addl_A0_seg(s
, R_SS
);
2385 gen_op_st_v(s
, d_ot
, val
, cpu_A0
);
2386 gen_op_mov_reg_v(a_ot
, R_ESP
, new_esp
);
2389 /* two step pop is necessary for precise exceptions */
2390 static TCGMemOp
gen_pop_T0(DisasContext
*s
)
2392 TCGMemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2396 addr
= cpu_regs
[R_ESP
];
2397 } else if (!s
->ss32
) {
2398 tcg_gen_ext16u_tl(cpu_A0
, cpu_regs
[R_ESP
]);
2399 gen_op_addl_A0_seg(s
, R_SS
);
2400 } else if (s
->addseg
) {
2401 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[R_ESP
]);
2402 gen_op_addl_A0_seg(s
, R_SS
);
2404 tcg_gen_ext32u_tl(cpu_A0
, cpu_regs
[R_ESP
]);
2407 gen_op_ld_v(s
, d_ot
, cpu_T
[0], addr
);
2411 static void gen_pop_update(DisasContext
*s
, TCGMemOp ot
)
2413 gen_stack_update(s
, 1 << ot
);
2416 static void gen_stack_A0(DisasContext
*s
)
2418 gen_op_movl_A0_reg(R_ESP
);
2420 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2421 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2423 gen_op_addl_A0_seg(s
, R_SS
);
2426 /* NOTE: wrap around in 16 bit not fully handled */
2427 static void gen_pusha(DisasContext
*s
)
2430 gen_op_movl_A0_reg(R_ESP
);
2431 gen_op_addl_A0_im(-8 << s
->dflag
);
2433 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2434 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2436 gen_op_addl_A0_seg(s
, R_SS
);
2437 for(i
= 0;i
< 8; i
++) {
2438 gen_op_mov_v_reg(MO_32
, cpu_T
[0], 7 - i
);
2439 gen_op_st_v(s
, s
->dflag
, cpu_T
[0], cpu_A0
);
2440 gen_op_addl_A0_im(1 << s
->dflag
);
2442 gen_op_mov_reg_v(MO_16
+ s
->ss32
, R_ESP
, cpu_T
[1]);
2445 /* NOTE: wrap around in 16 bit not fully handled */
2446 static void gen_popa(DisasContext
*s
)
2449 gen_op_movl_A0_reg(R_ESP
);
2451 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2452 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2453 tcg_gen_addi_tl(cpu_T
[1], cpu_T
[1], 8 << s
->dflag
);
2455 gen_op_addl_A0_seg(s
, R_SS
);
2456 for(i
= 0;i
< 8; i
++) {
2457 /* ESP is not reloaded */
2459 gen_op_ld_v(s
, s
->dflag
, cpu_T
[0], cpu_A0
);
2460 gen_op_mov_reg_v(s
->dflag
, 7 - i
, cpu_T
[0]);
2462 gen_op_addl_A0_im(1 << s
->dflag
);
2464 gen_op_mov_reg_v(MO_16
+ s
->ss32
, R_ESP
, cpu_T
[1]);
2467 static void gen_enter(DisasContext
*s
, int esp_addend
, int level
)
2469 TCGMemOp ot
= mo_pushpop(s
, s
->dflag
);
2470 int opsize
= 1 << ot
;
2473 #ifdef TARGET_X86_64
2475 gen_op_movl_A0_reg(R_ESP
);
2476 gen_op_addq_A0_im(-opsize
);
2477 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2480 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EBP
);
2481 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
2483 /* XXX: must save state */
2484 gen_helper_enter64_level(cpu_env
, tcg_const_i32(level
),
2485 tcg_const_i32((ot
== MO_64
)),
2488 gen_op_mov_reg_v(ot
, R_EBP
, cpu_T
[1]);
2489 tcg_gen_addi_tl(cpu_T
[1], cpu_T
[1], -esp_addend
+ (-opsize
* level
));
2490 gen_op_mov_reg_v(MO_64
, R_ESP
, cpu_T
[1]);
2494 gen_op_movl_A0_reg(R_ESP
);
2495 gen_op_addl_A0_im(-opsize
);
2497 tcg_gen_ext16u_tl(cpu_A0
, cpu_A0
);
2498 tcg_gen_mov_tl(cpu_T
[1], cpu_A0
);
2500 gen_op_addl_A0_seg(s
, R_SS
);
2502 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EBP
);
2503 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
2505 /* XXX: must save state */
2506 gen_helper_enter_level(cpu_env
, tcg_const_i32(level
),
2507 tcg_const_i32(s
->dflag
- 1),
2510 gen_op_mov_reg_v(ot
, R_EBP
, cpu_T
[1]);
2511 tcg_gen_addi_tl(cpu_T
[1], cpu_T
[1], -esp_addend
+ (-opsize
* level
));
2512 gen_op_mov_reg_v(MO_16
+ s
->ss32
, R_ESP
, cpu_T
[1]);
2516 static void gen_exception(DisasContext
*s
, int trapno
, target_ulong cur_eip
)
2518 gen_update_cc_op(s
);
2519 gen_jmp_im(cur_eip
);
2520 gen_helper_raise_exception(cpu_env
, tcg_const_i32(trapno
));
2521 s
->is_jmp
= DISAS_TB_JUMP
;
2524 /* an interrupt is different from an exception because of the
2526 static void gen_interrupt(DisasContext
*s
, int intno
,
2527 target_ulong cur_eip
, target_ulong next_eip
)
2529 gen_update_cc_op(s
);
2530 gen_jmp_im(cur_eip
);
2531 gen_helper_raise_interrupt(cpu_env
, tcg_const_i32(intno
),
2532 tcg_const_i32(next_eip
- cur_eip
));
2533 s
->is_jmp
= DISAS_TB_JUMP
;
2536 static void gen_debug(DisasContext
*s
, target_ulong cur_eip
)
2538 gen_update_cc_op(s
);
2539 gen_jmp_im(cur_eip
);
2540 gen_helper_debug(cpu_env
);
2541 s
->is_jmp
= DISAS_TB_JUMP
;
2544 /* generate a generic end of block. Trace exception is also generated
2546 static void gen_eob(DisasContext
*s
)
2548 gen_update_cc_op(s
);
2549 if (s
->tb
->flags
& HF_INHIBIT_IRQ_MASK
) {
2550 gen_helper_reset_inhibit_irq(cpu_env
);
2552 if (s
->tb
->flags
& HF_RF_MASK
) {
2553 gen_helper_reset_rf(cpu_env
);
2555 if (s
->singlestep_enabled
) {
2556 gen_helper_debug(cpu_env
);
2558 gen_helper_single_step(cpu_env
);
2562 s
->is_jmp
= DISAS_TB_JUMP
;
2565 /* generate a jump to eip. No segment change must happen before as a
2566 direct call to the next block may occur */
2567 static void gen_jmp_tb(DisasContext
*s
, target_ulong eip
, int tb_num
)
2569 gen_update_cc_op(s
);
2570 set_cc_op(s
, CC_OP_DYNAMIC
);
2572 gen_goto_tb(s
, tb_num
, eip
);
2573 s
->is_jmp
= DISAS_TB_JUMP
;
2580 static void gen_jmp(DisasContext
*s
, target_ulong eip
)
2582 gen_jmp_tb(s
, eip
, 0);
2585 static inline void gen_ldq_env_A0(DisasContext
*s
, int offset
)
2587 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
, s
->mem_index
, MO_LEQ
);
2588 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, offset
);
2591 static inline void gen_stq_env_A0(DisasContext
*s
, int offset
)
2593 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, offset
);
2594 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
, s
->mem_index
, MO_LEQ
);
2597 static inline void gen_ldo_env_A0(DisasContext
*s
, int offset
)
2599 int mem_index
= s
->mem_index
;
2600 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
, mem_index
, MO_LEQ
);
2601 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(0)));
2602 tcg_gen_addi_tl(cpu_tmp0
, cpu_A0
, 8);
2603 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_tmp0
, mem_index
, MO_LEQ
);
2604 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(1)));
2607 static inline void gen_sto_env_A0(DisasContext
*s
, int offset
)
2609 int mem_index
= s
->mem_index
;
2610 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(0)));
2611 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
, mem_index
, MO_LEQ
);
2612 tcg_gen_addi_tl(cpu_tmp0
, cpu_A0
, 8);
2613 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(1)));
2614 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_tmp0
, mem_index
, MO_LEQ
);
2617 static inline void gen_op_movo(int d_offset
, int s_offset
)
2619 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, s_offset
);
2620 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, d_offset
);
2621 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, s_offset
+ 8);
2622 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, d_offset
+ 8);
2625 static inline void gen_op_movq(int d_offset
, int s_offset
)
2627 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
, s_offset
);
2628 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, d_offset
);
2631 static inline void gen_op_movl(int d_offset
, int s_offset
)
2633 tcg_gen_ld_i32(cpu_tmp2_i32
, cpu_env
, s_offset
);
2634 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
, d_offset
);
2637 static inline void gen_op_movq_env_0(int d_offset
)
2639 tcg_gen_movi_i64(cpu_tmp1_i64
, 0);
2640 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
, d_offset
);
2643 typedef void (*SSEFunc_i_ep
)(TCGv_i32 val
, TCGv_ptr env
, TCGv_ptr reg
);
2644 typedef void (*SSEFunc_l_ep
)(TCGv_i64 val
, TCGv_ptr env
, TCGv_ptr reg
);
2645 typedef void (*SSEFunc_0_epi
)(TCGv_ptr env
, TCGv_ptr reg
, TCGv_i32 val
);
2646 typedef void (*SSEFunc_0_epl
)(TCGv_ptr env
, TCGv_ptr reg
, TCGv_i64 val
);
2647 typedef void (*SSEFunc_0_epp
)(TCGv_ptr env
, TCGv_ptr reg_a
, TCGv_ptr reg_b
);
2648 typedef void (*SSEFunc_0_eppi
)(TCGv_ptr env
, TCGv_ptr reg_a
, TCGv_ptr reg_b
,
2650 typedef void (*SSEFunc_0_ppi
)(TCGv_ptr reg_a
, TCGv_ptr reg_b
, TCGv_i32 val
);
2651 typedef void (*SSEFunc_0_eppt
)(TCGv_ptr env
, TCGv_ptr reg_a
, TCGv_ptr reg_b
,
2654 #define SSE_SPECIAL ((void *)1)
2655 #define SSE_DUMMY ((void *)2)
2657 #define MMX_OP2(x) { gen_helper_ ## x ## _mmx, gen_helper_ ## x ## _xmm }
2658 #define SSE_FOP(x) { gen_helper_ ## x ## ps, gen_helper_ ## x ## pd, \
2659 gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, }
2661 static const SSEFunc_0_epp sse_op_table1
[256][4] = {
2662 /* 3DNow! extensions */
2663 [0x0e] = { SSE_DUMMY
}, /* femms */
2664 [0x0f] = { SSE_DUMMY
}, /* pf... */
2665 /* pure SSE operations */
2666 [0x10] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movups, movupd, movss, movsd */
2667 [0x11] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movups, movupd, movss, movsd */
2668 [0x12] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movlps, movlpd, movsldup, movddup */
2669 [0x13] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movlps, movlpd */
2670 [0x14] = { gen_helper_punpckldq_xmm
, gen_helper_punpcklqdq_xmm
},
2671 [0x15] = { gen_helper_punpckhdq_xmm
, gen_helper_punpckhqdq_xmm
},
2672 [0x16] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movhps, movhpd, movshdup */
2673 [0x17] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movhps, movhpd */
2675 [0x28] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movaps, movapd */
2676 [0x29] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movaps, movapd */
2677 [0x2a] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */
2678 [0x2b] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movntps, movntpd, movntss, movntsd */
2679 [0x2c] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */
2680 [0x2d] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */
2681 [0x2e] = { gen_helper_ucomiss
, gen_helper_ucomisd
},
2682 [0x2f] = { gen_helper_comiss
, gen_helper_comisd
},
2683 [0x50] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movmskps, movmskpd */
2684 [0x51] = SSE_FOP(sqrt
),
2685 [0x52] = { gen_helper_rsqrtps
, NULL
, gen_helper_rsqrtss
, NULL
},
2686 [0x53] = { gen_helper_rcpps
, NULL
, gen_helper_rcpss
, NULL
},
2687 [0x54] = { gen_helper_pand_xmm
, gen_helper_pand_xmm
}, /* andps, andpd */
2688 [0x55] = { gen_helper_pandn_xmm
, gen_helper_pandn_xmm
}, /* andnps, andnpd */
2689 [0x56] = { gen_helper_por_xmm
, gen_helper_por_xmm
}, /* orps, orpd */
2690 [0x57] = { gen_helper_pxor_xmm
, gen_helper_pxor_xmm
}, /* xorps, xorpd */
2691 [0x58] = SSE_FOP(add
),
2692 [0x59] = SSE_FOP(mul
),
2693 [0x5a] = { gen_helper_cvtps2pd
, gen_helper_cvtpd2ps
,
2694 gen_helper_cvtss2sd
, gen_helper_cvtsd2ss
},
2695 [0x5b] = { gen_helper_cvtdq2ps
, gen_helper_cvtps2dq
, gen_helper_cvttps2dq
},
2696 [0x5c] = SSE_FOP(sub
),
2697 [0x5d] = SSE_FOP(min
),
2698 [0x5e] = SSE_FOP(div
),
2699 [0x5f] = SSE_FOP(max
),
2701 [0xc2] = SSE_FOP(cmpeq
),
2702 [0xc6] = { (SSEFunc_0_epp
)gen_helper_shufps
,
2703 (SSEFunc_0_epp
)gen_helper_shufpd
}, /* XXX: casts */
2705 /* SSSE3, SSE4, MOVBE, CRC32, BMI1, BMI2, ADX. */
2706 [0x38] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
},
2707 [0x3a] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
},
2709 /* MMX ops and their SSE extensions */
2710 [0x60] = MMX_OP2(punpcklbw
),
2711 [0x61] = MMX_OP2(punpcklwd
),
2712 [0x62] = MMX_OP2(punpckldq
),
2713 [0x63] = MMX_OP2(packsswb
),
2714 [0x64] = MMX_OP2(pcmpgtb
),
2715 [0x65] = MMX_OP2(pcmpgtw
),
2716 [0x66] = MMX_OP2(pcmpgtl
),
2717 [0x67] = MMX_OP2(packuswb
),
2718 [0x68] = MMX_OP2(punpckhbw
),
2719 [0x69] = MMX_OP2(punpckhwd
),
2720 [0x6a] = MMX_OP2(punpckhdq
),
2721 [0x6b] = MMX_OP2(packssdw
),
2722 [0x6c] = { NULL
, gen_helper_punpcklqdq_xmm
},
2723 [0x6d] = { NULL
, gen_helper_punpckhqdq_xmm
},
2724 [0x6e] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movd mm, ea */
2725 [0x6f] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movq, movdqa, , movqdu */
2726 [0x70] = { (SSEFunc_0_epp
)gen_helper_pshufw_mmx
,
2727 (SSEFunc_0_epp
)gen_helper_pshufd_xmm
,
2728 (SSEFunc_0_epp
)gen_helper_pshufhw_xmm
,
2729 (SSEFunc_0_epp
)gen_helper_pshuflw_xmm
}, /* XXX: casts */
2730 [0x71] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* shiftw */
2731 [0x72] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* shiftd */
2732 [0x73] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* shiftq */
2733 [0x74] = MMX_OP2(pcmpeqb
),
2734 [0x75] = MMX_OP2(pcmpeqw
),
2735 [0x76] = MMX_OP2(pcmpeql
),
2736 [0x77] = { SSE_DUMMY
}, /* emms */
2737 [0x78] = { NULL
, SSE_SPECIAL
, NULL
, SSE_SPECIAL
}, /* extrq_i, insertq_i */
2738 [0x79] = { NULL
, gen_helper_extrq_r
, NULL
, gen_helper_insertq_r
},
2739 [0x7c] = { NULL
, gen_helper_haddpd
, NULL
, gen_helper_haddps
},
2740 [0x7d] = { NULL
, gen_helper_hsubpd
, NULL
, gen_helper_hsubps
},
2741 [0x7e] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movd, movd, , movq */
2742 [0x7f] = { SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
}, /* movq, movdqa, movdqu */
2743 [0xc4] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* pinsrw */
2744 [0xc5] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* pextrw */
2745 [0xd0] = { NULL
, gen_helper_addsubpd
, NULL
, gen_helper_addsubps
},
2746 [0xd1] = MMX_OP2(psrlw
),
2747 [0xd2] = MMX_OP2(psrld
),
2748 [0xd3] = MMX_OP2(psrlq
),
2749 [0xd4] = MMX_OP2(paddq
),
2750 [0xd5] = MMX_OP2(pmullw
),
2751 [0xd6] = { NULL
, SSE_SPECIAL
, SSE_SPECIAL
, SSE_SPECIAL
},
2752 [0xd7] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* pmovmskb */
2753 [0xd8] = MMX_OP2(psubusb
),
2754 [0xd9] = MMX_OP2(psubusw
),
2755 [0xda] = MMX_OP2(pminub
),
2756 [0xdb] = MMX_OP2(pand
),
2757 [0xdc] = MMX_OP2(paddusb
),
2758 [0xdd] = MMX_OP2(paddusw
),
2759 [0xde] = MMX_OP2(pmaxub
),
2760 [0xdf] = MMX_OP2(pandn
),
2761 [0xe0] = MMX_OP2(pavgb
),
2762 [0xe1] = MMX_OP2(psraw
),
2763 [0xe2] = MMX_OP2(psrad
),
2764 [0xe3] = MMX_OP2(pavgw
),
2765 [0xe4] = MMX_OP2(pmulhuw
),
2766 [0xe5] = MMX_OP2(pmulhw
),
2767 [0xe6] = { NULL
, gen_helper_cvttpd2dq
, gen_helper_cvtdq2pd
, gen_helper_cvtpd2dq
},
2768 [0xe7] = { SSE_SPECIAL
, SSE_SPECIAL
}, /* movntq, movntq */
2769 [0xe8] = MMX_OP2(psubsb
),
2770 [0xe9] = MMX_OP2(psubsw
),
2771 [0xea] = MMX_OP2(pminsw
),
2772 [0xeb] = MMX_OP2(por
),
2773 [0xec] = MMX_OP2(paddsb
),
2774 [0xed] = MMX_OP2(paddsw
),
2775 [0xee] = MMX_OP2(pmaxsw
),
2776 [0xef] = MMX_OP2(pxor
),
2777 [0xf0] = { NULL
, NULL
, NULL
, SSE_SPECIAL
}, /* lddqu */
2778 [0xf1] = MMX_OP2(psllw
),
2779 [0xf2] = MMX_OP2(pslld
),
2780 [0xf3] = MMX_OP2(psllq
),
2781 [0xf4] = MMX_OP2(pmuludq
),
2782 [0xf5] = MMX_OP2(pmaddwd
),
2783 [0xf6] = MMX_OP2(psadbw
),
2784 [0xf7] = { (SSEFunc_0_epp
)gen_helper_maskmov_mmx
,
2785 (SSEFunc_0_epp
)gen_helper_maskmov_xmm
}, /* XXX: casts */
2786 [0xf8] = MMX_OP2(psubb
),
2787 [0xf9] = MMX_OP2(psubw
),
2788 [0xfa] = MMX_OP2(psubl
),
2789 [0xfb] = MMX_OP2(psubq
),
2790 [0xfc] = MMX_OP2(paddb
),
2791 [0xfd] = MMX_OP2(paddw
),
2792 [0xfe] = MMX_OP2(paddl
),
2795 static const SSEFunc_0_epp sse_op_table2
[3 * 8][2] = {
2796 [0 + 2] = MMX_OP2(psrlw
),
2797 [0 + 4] = MMX_OP2(psraw
),
2798 [0 + 6] = MMX_OP2(psllw
),
2799 [8 + 2] = MMX_OP2(psrld
),
2800 [8 + 4] = MMX_OP2(psrad
),
2801 [8 + 6] = MMX_OP2(pslld
),
2802 [16 + 2] = MMX_OP2(psrlq
),
2803 [16 + 3] = { NULL
, gen_helper_psrldq_xmm
},
2804 [16 + 6] = MMX_OP2(psllq
),
2805 [16 + 7] = { NULL
, gen_helper_pslldq_xmm
},
2808 static const SSEFunc_0_epi sse_op_table3ai
[] = {
2809 gen_helper_cvtsi2ss
,
2813 #ifdef TARGET_X86_64
2814 static const SSEFunc_0_epl sse_op_table3aq
[] = {
2815 gen_helper_cvtsq2ss
,
2820 static const SSEFunc_i_ep sse_op_table3bi
[] = {
2821 gen_helper_cvttss2si
,
2822 gen_helper_cvtss2si
,
2823 gen_helper_cvttsd2si
,
2827 #ifdef TARGET_X86_64
2828 static const SSEFunc_l_ep sse_op_table3bq
[] = {
2829 gen_helper_cvttss2sq
,
2830 gen_helper_cvtss2sq
,
2831 gen_helper_cvttsd2sq
,
2836 static const SSEFunc_0_epp sse_op_table4
[8][4] = {
2847 static const SSEFunc_0_epp sse_op_table5
[256] = {
2848 [0x0c] = gen_helper_pi2fw
,
2849 [0x0d] = gen_helper_pi2fd
,
2850 [0x1c] = gen_helper_pf2iw
,
2851 [0x1d] = gen_helper_pf2id
,
2852 [0x8a] = gen_helper_pfnacc
,
2853 [0x8e] = gen_helper_pfpnacc
,
2854 [0x90] = gen_helper_pfcmpge
,
2855 [0x94] = gen_helper_pfmin
,
2856 [0x96] = gen_helper_pfrcp
,
2857 [0x97] = gen_helper_pfrsqrt
,
2858 [0x9a] = gen_helper_pfsub
,
2859 [0x9e] = gen_helper_pfadd
,
2860 [0xa0] = gen_helper_pfcmpgt
,
2861 [0xa4] = gen_helper_pfmax
,
2862 [0xa6] = gen_helper_movq
, /* pfrcpit1; no need to actually increase precision */
2863 [0xa7] = gen_helper_movq
, /* pfrsqit1 */
2864 [0xaa] = gen_helper_pfsubr
,
2865 [0xae] = gen_helper_pfacc
,
2866 [0xb0] = gen_helper_pfcmpeq
,
2867 [0xb4] = gen_helper_pfmul
,
2868 [0xb6] = gen_helper_movq
, /* pfrcpit2 */
2869 [0xb7] = gen_helper_pmulhrw_mmx
,
2870 [0xbb] = gen_helper_pswapd
,
2871 [0xbf] = gen_helper_pavgb_mmx
/* pavgusb */
2874 struct SSEOpHelper_epp
{
2875 SSEFunc_0_epp op
[2];
2879 struct SSEOpHelper_eppi
{
2880 SSEFunc_0_eppi op
[2];
2884 #define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
2885 #define SSE41_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }
2886 #define SSE42_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }
2887 #define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2888 #define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
2889 CPUID_EXT_PCLMULQDQ }
2890 #define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
2892 static const struct SSEOpHelper_epp sse_op_table6
[256] = {
2893 [0x00] = SSSE3_OP(pshufb
),
2894 [0x01] = SSSE3_OP(phaddw
),
2895 [0x02] = SSSE3_OP(phaddd
),
2896 [0x03] = SSSE3_OP(phaddsw
),
2897 [0x04] = SSSE3_OP(pmaddubsw
),
2898 [0x05] = SSSE3_OP(phsubw
),
2899 [0x06] = SSSE3_OP(phsubd
),
2900 [0x07] = SSSE3_OP(phsubsw
),
2901 [0x08] = SSSE3_OP(psignb
),
2902 [0x09] = SSSE3_OP(psignw
),
2903 [0x0a] = SSSE3_OP(psignd
),
2904 [0x0b] = SSSE3_OP(pmulhrsw
),
2905 [0x10] = SSE41_OP(pblendvb
),
2906 [0x14] = SSE41_OP(blendvps
),
2907 [0x15] = SSE41_OP(blendvpd
),
2908 [0x17] = SSE41_OP(ptest
),
2909 [0x1c] = SSSE3_OP(pabsb
),
2910 [0x1d] = SSSE3_OP(pabsw
),
2911 [0x1e] = SSSE3_OP(pabsd
),
2912 [0x20] = SSE41_OP(pmovsxbw
),
2913 [0x21] = SSE41_OP(pmovsxbd
),
2914 [0x22] = SSE41_OP(pmovsxbq
),
2915 [0x23] = SSE41_OP(pmovsxwd
),
2916 [0x24] = SSE41_OP(pmovsxwq
),
2917 [0x25] = SSE41_OP(pmovsxdq
),
2918 [0x28] = SSE41_OP(pmuldq
),
2919 [0x29] = SSE41_OP(pcmpeqq
),
2920 [0x2a] = SSE41_SPECIAL
, /* movntqda */
2921 [0x2b] = SSE41_OP(packusdw
),
2922 [0x30] = SSE41_OP(pmovzxbw
),
2923 [0x31] = SSE41_OP(pmovzxbd
),
2924 [0x32] = SSE41_OP(pmovzxbq
),
2925 [0x33] = SSE41_OP(pmovzxwd
),
2926 [0x34] = SSE41_OP(pmovzxwq
),
2927 [0x35] = SSE41_OP(pmovzxdq
),
2928 [0x37] = SSE42_OP(pcmpgtq
),
2929 [0x38] = SSE41_OP(pminsb
),
2930 [0x39] = SSE41_OP(pminsd
),
2931 [0x3a] = SSE41_OP(pminuw
),
2932 [0x3b] = SSE41_OP(pminud
),
2933 [0x3c] = SSE41_OP(pmaxsb
),
2934 [0x3d] = SSE41_OP(pmaxsd
),
2935 [0x3e] = SSE41_OP(pmaxuw
),
2936 [0x3f] = SSE41_OP(pmaxud
),
2937 [0x40] = SSE41_OP(pmulld
),
2938 [0x41] = SSE41_OP(phminposuw
),
2939 [0xdb] = AESNI_OP(aesimc
),
2940 [0xdc] = AESNI_OP(aesenc
),
2941 [0xdd] = AESNI_OP(aesenclast
),
2942 [0xde] = AESNI_OP(aesdec
),
2943 [0xdf] = AESNI_OP(aesdeclast
),
2946 static const struct SSEOpHelper_eppi sse_op_table7
[256] = {
2947 [0x08] = SSE41_OP(roundps
),
2948 [0x09] = SSE41_OP(roundpd
),
2949 [0x0a] = SSE41_OP(roundss
),
2950 [0x0b] = SSE41_OP(roundsd
),
2951 [0x0c] = SSE41_OP(blendps
),
2952 [0x0d] = SSE41_OP(blendpd
),
2953 [0x0e] = SSE41_OP(pblendw
),
2954 [0x0f] = SSSE3_OP(palignr
),
2955 [0x14] = SSE41_SPECIAL
, /* pextrb */
2956 [0x15] = SSE41_SPECIAL
, /* pextrw */
2957 [0x16] = SSE41_SPECIAL
, /* pextrd/pextrq */
2958 [0x17] = SSE41_SPECIAL
, /* extractps */
2959 [0x20] = SSE41_SPECIAL
, /* pinsrb */
2960 [0x21] = SSE41_SPECIAL
, /* insertps */
2961 [0x22] = SSE41_SPECIAL
, /* pinsrd/pinsrq */
2962 [0x40] = SSE41_OP(dpps
),
2963 [0x41] = SSE41_OP(dppd
),
2964 [0x42] = SSE41_OP(mpsadbw
),
2965 [0x44] = PCLMULQDQ_OP(pclmulqdq
),
2966 [0x60] = SSE42_OP(pcmpestrm
),
2967 [0x61] = SSE42_OP(pcmpestri
),
2968 [0x62] = SSE42_OP(pcmpistrm
),
2969 [0x63] = SSE42_OP(pcmpistri
),
2970 [0xdf] = AESNI_OP(aeskeygenassist
),
2973 static void gen_sse(CPUX86State
*env
, DisasContext
*s
, int b
,
2974 target_ulong pc_start
, int rex_r
)
2976 int b1
, op1_offset
, op2_offset
, is_xmm
, val
;
2977 int modrm
, mod
, rm
, reg
;
2978 SSEFunc_0_epp sse_fn_epp
;
2979 SSEFunc_0_eppi sse_fn_eppi
;
2980 SSEFunc_0_ppi sse_fn_ppi
;
2981 SSEFunc_0_eppt sse_fn_eppt
;
2985 if (s
->prefix
& PREFIX_DATA
)
2987 else if (s
->prefix
& PREFIX_REPZ
)
2989 else if (s
->prefix
& PREFIX_REPNZ
)
2993 sse_fn_epp
= sse_op_table1
[b
][b1
];
2997 if ((b
<= 0x5f && b
>= 0x10) || b
== 0xc6 || b
== 0xc2) {
3007 /* simple MMX/SSE operation */
3008 if (s
->flags
& HF_TS_MASK
) {
3009 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
3012 if (s
->flags
& HF_EM_MASK
) {
3014 gen_exception(s
, EXCP06_ILLOP
, pc_start
- s
->cs_base
);
3017 if (is_xmm
&& !(s
->flags
& HF_OSFXSR_MASK
))
3018 if ((b
!= 0x38 && b
!= 0x3a) || (s
->prefix
& PREFIX_DATA
))
3021 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_3DNOW
))
3024 gen_helper_emms(cpu_env
);
3029 gen_helper_emms(cpu_env
);
3032 /* prepare MMX state (XXX: optimize by storing fptt and fptags in
3033 the static cpu state) */
3035 gen_helper_enter_mmx(cpu_env
);
3038 modrm
= cpu_ldub_code(env
, s
->pc
++);
3039 reg
= ((modrm
>> 3) & 7);
3042 mod
= (modrm
>> 6) & 3;
3043 if (sse_fn_epp
== SSE_SPECIAL
) {
3046 case 0x0e7: /* movntq */
3049 gen_lea_modrm(env
, s
, modrm
);
3050 gen_stq_env_A0(s
, offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3052 case 0x1e7: /* movntdq */
3053 case 0x02b: /* movntps */
3054 case 0x12b: /* movntps */
3057 gen_lea_modrm(env
, s
, modrm
);
3058 gen_sto_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3060 case 0x3f0: /* lddqu */
3063 gen_lea_modrm(env
, s
, modrm
);
3064 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3066 case 0x22b: /* movntss */
3067 case 0x32b: /* movntsd */
3070 gen_lea_modrm(env
, s
, modrm
);
3072 gen_stq_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3074 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
3075 xmm_regs
[reg
].XMM_L(0)));
3076 gen_op_st_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
3079 case 0x6e: /* movd mm, ea */
3080 #ifdef TARGET_X86_64
3081 if (s
->dflag
== MO_64
) {
3082 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 0);
3083 tcg_gen_st_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3087 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 0);
3088 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3089 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3090 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
3091 gen_helper_movl_mm_T0_mmx(cpu_ptr0
, cpu_tmp2_i32
);
3094 case 0x16e: /* movd xmm, ea */
3095 #ifdef TARGET_X86_64
3096 if (s
->dflag
== MO_64
) {
3097 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 0);
3098 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3099 offsetof(CPUX86State
,xmm_regs
[reg
]));
3100 gen_helper_movq_mm_T0_xmm(cpu_ptr0
, cpu_T
[0]);
3104 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 0);
3105 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3106 offsetof(CPUX86State
,xmm_regs
[reg
]));
3107 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
3108 gen_helper_movl_mm_T0_xmm(cpu_ptr0
, cpu_tmp2_i32
);
3111 case 0x6f: /* movq mm, ea */
3113 gen_lea_modrm(env
, s
, modrm
);
3114 gen_ldq_env_A0(s
, offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3117 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
,
3118 offsetof(CPUX86State
,fpregs
[rm
].mmx
));
3119 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
,
3120 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3123 case 0x010: /* movups */
3124 case 0x110: /* movupd */
3125 case 0x028: /* movaps */
3126 case 0x128: /* movapd */
3127 case 0x16f: /* movdqa xmm, ea */
3128 case 0x26f: /* movdqu xmm, ea */
3130 gen_lea_modrm(env
, s
, modrm
);
3131 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3133 rm
= (modrm
& 7) | REX_B(s
);
3134 gen_op_movo(offsetof(CPUX86State
,xmm_regs
[reg
]),
3135 offsetof(CPUX86State
,xmm_regs
[rm
]));
3138 case 0x210: /* movss xmm, ea */
3140 gen_lea_modrm(env
, s
, modrm
);
3141 gen_op_ld_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
3142 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3143 tcg_gen_movi_tl(cpu_T
[0], 0);
3144 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(1)));
3145 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)));
3146 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)));
3148 rm
= (modrm
& 7) | REX_B(s
);
3149 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)),
3150 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(0)));
3153 case 0x310: /* movsd xmm, ea */
3155 gen_lea_modrm(env
, s
, modrm
);
3156 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3157 xmm_regs
[reg
].XMM_Q(0)));
3158 tcg_gen_movi_tl(cpu_T
[0], 0);
3159 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)));
3160 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)));
3162 rm
= (modrm
& 7) | REX_B(s
);
3163 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3164 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3167 case 0x012: /* movlps */
3168 case 0x112: /* movlpd */
3170 gen_lea_modrm(env
, s
, modrm
);
3171 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3172 xmm_regs
[reg
].XMM_Q(0)));
3175 rm
= (modrm
& 7) | REX_B(s
);
3176 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3177 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(1)));
3180 case 0x212: /* movsldup */
3182 gen_lea_modrm(env
, s
, modrm
);
3183 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3185 rm
= (modrm
& 7) | REX_B(s
);
3186 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)),
3187 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(0)));
3188 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)),
3189 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(2)));
3191 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(1)),
3192 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3193 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)),
3194 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)));
3196 case 0x312: /* movddup */
3198 gen_lea_modrm(env
, s
, modrm
);
3199 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3200 xmm_regs
[reg
].XMM_Q(0)));
3202 rm
= (modrm
& 7) | REX_B(s
);
3203 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3204 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3206 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(1)),
3207 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)));
3209 case 0x016: /* movhps */
3210 case 0x116: /* movhpd */
3212 gen_lea_modrm(env
, s
, modrm
);
3213 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3214 xmm_regs
[reg
].XMM_Q(1)));
3217 rm
= (modrm
& 7) | REX_B(s
);
3218 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(1)),
3219 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3222 case 0x216: /* movshdup */
3224 gen_lea_modrm(env
, s
, modrm
);
3225 gen_ldo_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3227 rm
= (modrm
& 7) | REX_B(s
);
3228 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(1)),
3229 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(1)));
3230 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)),
3231 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(3)));
3233 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)),
3234 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(1)));
3235 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(2)),
3236 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(3)));
3241 int bit_index
, field_length
;
3243 if (b1
== 1 && reg
!= 0)
3245 field_length
= cpu_ldub_code(env
, s
->pc
++) & 0x3F;
3246 bit_index
= cpu_ldub_code(env
, s
->pc
++) & 0x3F;
3247 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3248 offsetof(CPUX86State
,xmm_regs
[reg
]));
3250 gen_helper_extrq_i(cpu_env
, cpu_ptr0
,
3251 tcg_const_i32(bit_index
),
3252 tcg_const_i32(field_length
));
3254 gen_helper_insertq_i(cpu_env
, cpu_ptr0
,
3255 tcg_const_i32(bit_index
),
3256 tcg_const_i32(field_length
));
3259 case 0x7e: /* movd ea, mm */
3260 #ifdef TARGET_X86_64
3261 if (s
->dflag
== MO_64
) {
3262 tcg_gen_ld_i64(cpu_T
[0], cpu_env
,
3263 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3264 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 1);
3268 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
,
3269 offsetof(CPUX86State
,fpregs
[reg
].mmx
.MMX_L(0)));
3270 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 1);
3273 case 0x17e: /* movd ea, xmm */
3274 #ifdef TARGET_X86_64
3275 if (s
->dflag
== MO_64
) {
3276 tcg_gen_ld_i64(cpu_T
[0], cpu_env
,
3277 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)));
3278 gen_ldst_modrm(env
, s
, modrm
, MO_64
, OR_TMP0
, 1);
3282 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
,
3283 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3284 gen_ldst_modrm(env
, s
, modrm
, MO_32
, OR_TMP0
, 1);
3287 case 0x27e: /* movq xmm, ea */
3289 gen_lea_modrm(env
, s
, modrm
);
3290 gen_ldq_env_A0(s
, offsetof(CPUX86State
,
3291 xmm_regs
[reg
].XMM_Q(0)));
3293 rm
= (modrm
& 7) | REX_B(s
);
3294 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3295 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3297 gen_op_movq_env_0(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(1)));
3299 case 0x7f: /* movq ea, mm */
3301 gen_lea_modrm(env
, s
, modrm
);
3302 gen_stq_env_A0(s
, offsetof(CPUX86State
, fpregs
[reg
].mmx
));
3305 gen_op_movq(offsetof(CPUX86State
,fpregs
[rm
].mmx
),
3306 offsetof(CPUX86State
,fpregs
[reg
].mmx
));
3309 case 0x011: /* movups */
3310 case 0x111: /* movupd */
3311 case 0x029: /* movaps */
3312 case 0x129: /* movapd */
3313 case 0x17f: /* movdqa ea, xmm */
3314 case 0x27f: /* movdqu ea, xmm */
3316 gen_lea_modrm(env
, s
, modrm
);
3317 gen_sto_env_A0(s
, offsetof(CPUX86State
, xmm_regs
[reg
]));
3319 rm
= (modrm
& 7) | REX_B(s
);
3320 gen_op_movo(offsetof(CPUX86State
,xmm_regs
[rm
]),
3321 offsetof(CPUX86State
,xmm_regs
[reg
]));
3324 case 0x211: /* movss ea, xmm */
3326 gen_lea_modrm(env
, s
, modrm
);
3327 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3328 gen_op_st_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
3330 rm
= (modrm
& 7) | REX_B(s
);
3331 gen_op_movl(offsetof(CPUX86State
,xmm_regs
[rm
].XMM_L(0)),
3332 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_L(0)));
3335 case 0x311: /* movsd ea, xmm */
3337 gen_lea_modrm(env
, s
, modrm
);
3338 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3339 xmm_regs
[reg
].XMM_Q(0)));
3341 rm
= (modrm
& 7) | REX_B(s
);
3342 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)),
3343 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)));
3346 case 0x013: /* movlps */
3347 case 0x113: /* movlpd */
3349 gen_lea_modrm(env
, s
, modrm
);
3350 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3351 xmm_regs
[reg
].XMM_Q(0)));
3356 case 0x017: /* movhps */
3357 case 0x117: /* movhpd */
3359 gen_lea_modrm(env
, s
, modrm
);
3360 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3361 xmm_regs
[reg
].XMM_Q(1)));
3366 case 0x71: /* shift mm, im */
3369 case 0x171: /* shift xmm, im */
3375 val
= cpu_ldub_code(env
, s
->pc
++);
3377 tcg_gen_movi_tl(cpu_T
[0], val
);
3378 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_t0
.XMM_L(0)));
3379 tcg_gen_movi_tl(cpu_T
[0], 0);
3380 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_t0
.XMM_L(1)));
3381 op1_offset
= offsetof(CPUX86State
,xmm_t0
);
3383 tcg_gen_movi_tl(cpu_T
[0], val
);
3384 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,mmx_t0
.MMX_L(0)));
3385 tcg_gen_movi_tl(cpu_T
[0], 0);
3386 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,mmx_t0
.MMX_L(1)));
3387 op1_offset
= offsetof(CPUX86State
,mmx_t0
);
3389 sse_fn_epp
= sse_op_table2
[((b
- 1) & 3) * 8 +
3390 (((modrm
>> 3)) & 7)][b1
];
3395 rm
= (modrm
& 7) | REX_B(s
);
3396 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
3399 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
3401 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op2_offset
);
3402 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op1_offset
);
3403 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3405 case 0x050: /* movmskps */
3406 rm
= (modrm
& 7) | REX_B(s
);
3407 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3408 offsetof(CPUX86State
,xmm_regs
[rm
]));
3409 gen_helper_movmskps(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3410 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp2_i32
);
3412 case 0x150: /* movmskpd */
3413 rm
= (modrm
& 7) | REX_B(s
);
3414 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
,
3415 offsetof(CPUX86State
,xmm_regs
[rm
]));
3416 gen_helper_movmskpd(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3417 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp2_i32
);
3419 case 0x02a: /* cvtpi2ps */
3420 case 0x12a: /* cvtpi2pd */
3421 gen_helper_enter_mmx(cpu_env
);
3423 gen_lea_modrm(env
, s
, modrm
);
3424 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
3425 gen_ldq_env_A0(s
, op2_offset
);
3428 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
3430 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
3431 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
3432 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
3435 gen_helper_cvtpi2ps(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3439 gen_helper_cvtpi2pd(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3443 case 0x22a: /* cvtsi2ss */
3444 case 0x32a: /* cvtsi2sd */
3445 ot
= mo_64_32(s
->dflag
);
3446 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3447 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
3448 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
3450 SSEFunc_0_epi sse_fn_epi
= sse_op_table3ai
[(b
>> 8) & 1];
3451 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
3452 sse_fn_epi(cpu_env
, cpu_ptr0
, cpu_tmp2_i32
);
3454 #ifdef TARGET_X86_64
3455 SSEFunc_0_epl sse_fn_epl
= sse_op_table3aq
[(b
>> 8) & 1];
3456 sse_fn_epl(cpu_env
, cpu_ptr0
, cpu_T
[0]);
3462 case 0x02c: /* cvttps2pi */
3463 case 0x12c: /* cvttpd2pi */
3464 case 0x02d: /* cvtps2pi */
3465 case 0x12d: /* cvtpd2pi */
3466 gen_helper_enter_mmx(cpu_env
);
3468 gen_lea_modrm(env
, s
, modrm
);
3469 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
3470 gen_ldo_env_A0(s
, op2_offset
);
3472 rm
= (modrm
& 7) | REX_B(s
);
3473 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
3475 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
& 7].mmx
);
3476 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
3477 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
3480 gen_helper_cvttps2pi(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3483 gen_helper_cvttpd2pi(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3486 gen_helper_cvtps2pi(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3489 gen_helper_cvtpd2pi(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3493 case 0x22c: /* cvttss2si */
3494 case 0x32c: /* cvttsd2si */
3495 case 0x22d: /* cvtss2si */
3496 case 0x32d: /* cvtsd2si */
3497 ot
= mo_64_32(s
->dflag
);
3499 gen_lea_modrm(env
, s
, modrm
);
3501 gen_ldq_env_A0(s
, offsetof(CPUX86State
, xmm_t0
.XMM_Q(0)));
3503 gen_op_ld_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
3504 tcg_gen_st32_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,xmm_t0
.XMM_L(0)));
3506 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
3508 rm
= (modrm
& 7) | REX_B(s
);
3509 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
3511 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op2_offset
);
3513 SSEFunc_i_ep sse_fn_i_ep
=
3514 sse_op_table3bi
[((b
>> 7) & 2) | (b
& 1)];
3515 sse_fn_i_ep(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3516 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
3518 #ifdef TARGET_X86_64
3519 SSEFunc_l_ep sse_fn_l_ep
=
3520 sse_op_table3bq
[((b
>> 7) & 2) | (b
& 1)];
3521 sse_fn_l_ep(cpu_T
[0], cpu_env
, cpu_ptr0
);
3526 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3528 case 0xc4: /* pinsrw */
3531 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
3532 val
= cpu_ldub_code(env
, s
->pc
++);
3535 tcg_gen_st16_tl(cpu_T
[0], cpu_env
,
3536 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_W(val
)));
3539 tcg_gen_st16_tl(cpu_T
[0], cpu_env
,
3540 offsetof(CPUX86State
,fpregs
[reg
].mmx
.MMX_W(val
)));
3543 case 0xc5: /* pextrw */
3547 ot
= mo_64_32(s
->dflag
);
3548 val
= cpu_ldub_code(env
, s
->pc
++);
3551 rm
= (modrm
& 7) | REX_B(s
);
3552 tcg_gen_ld16u_tl(cpu_T
[0], cpu_env
,
3553 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_W(val
)));
3557 tcg_gen_ld16u_tl(cpu_T
[0], cpu_env
,
3558 offsetof(CPUX86State
,fpregs
[rm
].mmx
.MMX_W(val
)));
3560 reg
= ((modrm
>> 3) & 7) | rex_r
;
3561 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3563 case 0x1d6: /* movq ea, xmm */
3565 gen_lea_modrm(env
, s
, modrm
);
3566 gen_stq_env_A0(s
, offsetof(CPUX86State
,
3567 xmm_regs
[reg
].XMM_Q(0)));
3569 rm
= (modrm
& 7) | REX_B(s
);
3570 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)),
3571 offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)));
3572 gen_op_movq_env_0(offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(1)));
3575 case 0x2d6: /* movq2dq */
3576 gen_helper_enter_mmx(cpu_env
);
3578 gen_op_movq(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(0)),
3579 offsetof(CPUX86State
,fpregs
[rm
].mmx
));
3580 gen_op_movq_env_0(offsetof(CPUX86State
,xmm_regs
[reg
].XMM_Q(1)));
3582 case 0x3d6: /* movdq2q */
3583 gen_helper_enter_mmx(cpu_env
);
3584 rm
= (modrm
& 7) | REX_B(s
);
3585 gen_op_movq(offsetof(CPUX86State
,fpregs
[reg
& 7].mmx
),
3586 offsetof(CPUX86State
,xmm_regs
[rm
].XMM_Q(0)));
3588 case 0xd7: /* pmovmskb */
3593 rm
= (modrm
& 7) | REX_B(s
);
3594 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, offsetof(CPUX86State
,xmm_regs
[rm
]));
3595 gen_helper_pmovmskb_xmm(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3598 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, offsetof(CPUX86State
,fpregs
[rm
].mmx
));
3599 gen_helper_pmovmskb_mmx(cpu_tmp2_i32
, cpu_env
, cpu_ptr0
);
3601 reg
= ((modrm
>> 3) & 7) | rex_r
;
3602 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp2_i32
);
3608 if ((b
& 0xf0) == 0xf0) {
3611 modrm
= cpu_ldub_code(env
, s
->pc
++);
3613 reg
= ((modrm
>> 3) & 7) | rex_r
;
3614 mod
= (modrm
>> 6) & 3;
3619 sse_fn_epp
= sse_op_table6
[b
].op
[b1
];
3623 if (!(s
->cpuid_ext_features
& sse_op_table6
[b
].ext_mask
))
3627 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
3629 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
| REX_B(s
)]);
3631 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
3632 gen_lea_modrm(env
, s
, modrm
);
3634 case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
3635 case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
3636 case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3637 gen_ldq_env_A0(s
, op2_offset
+
3638 offsetof(XMMReg
, XMM_Q(0)));
3640 case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
3641 case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3642 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
3643 s
->mem_index
, MO_LEUL
);
3644 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
, op2_offset
+
3645 offsetof(XMMReg
, XMM_L(0)));
3647 case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3648 tcg_gen_qemu_ld_tl(cpu_tmp0
, cpu_A0
,
3649 s
->mem_index
, MO_LEUW
);
3650 tcg_gen_st16_tl(cpu_tmp0
, cpu_env
, op2_offset
+
3651 offsetof(XMMReg
, XMM_W(0)));
3653 case 0x2a: /* movntqda */
3654 gen_ldo_env_A0(s
, op1_offset
);
3657 gen_ldo_env_A0(s
, op2_offset
);
3661 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
].mmx
);
3663 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
3665 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
3666 gen_lea_modrm(env
, s
, modrm
);
3667 gen_ldq_env_A0(s
, op2_offset
);
3670 if (sse_fn_epp
== SSE_SPECIAL
) {
3674 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
3675 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
3676 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
3679 set_cc_op(s
, CC_OP_EFLAGS
);
3686 /* Various integer extensions at 0f 38 f[0-f]. */
3687 b
= modrm
| (b1
<< 8);
3688 modrm
= cpu_ldub_code(env
, s
->pc
++);
3689 reg
= ((modrm
>> 3) & 7) | rex_r
;
3692 case 0x3f0: /* crc32 Gd,Eb */
3693 case 0x3f1: /* crc32 Gd,Ey */
3695 if (!(s
->cpuid_ext_features
& CPUID_EXT_SSE42
)) {
3698 if ((b
& 0xff) == 0xf0) {
3700 } else if (s
->dflag
!= MO_64
) {
3701 ot
= (s
->prefix
& PREFIX_DATA
? MO_16
: MO_32
);
3706 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_regs
[reg
]);
3707 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3708 gen_helper_crc32(cpu_T
[0], cpu_tmp2_i32
,
3709 cpu_T
[0], tcg_const_i32(8 << ot
));
3711 ot
= mo_64_32(s
->dflag
);
3712 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3715 case 0x1f0: /* crc32 or movbe */
3717 /* For these insns, the f3 prefix is supposed to have priority
3718 over the 66 prefix, but that's not what we implement above
3720 if (s
->prefix
& PREFIX_REPNZ
) {
3724 case 0x0f0: /* movbe Gy,My */
3725 case 0x0f1: /* movbe My,Gy */
3726 if (!(s
->cpuid_ext_features
& CPUID_EXT_MOVBE
)) {
3729 if (s
->dflag
!= MO_64
) {
3730 ot
= (s
->prefix
& PREFIX_DATA
? MO_16
: MO_32
);
3735 gen_lea_modrm(env
, s
, modrm
);
3737 tcg_gen_qemu_ld_tl(cpu_T
[0], cpu_A0
,
3738 s
->mem_index
, ot
| MO_BE
);
3739 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3741 tcg_gen_qemu_st_tl(cpu_regs
[reg
], cpu_A0
,
3742 s
->mem_index
, ot
| MO_BE
);
3746 case 0x0f2: /* andn Gy, By, Ey */
3747 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)
3748 || !(s
->prefix
& PREFIX_VEX
)
3752 ot
= mo_64_32(s
->dflag
);
3753 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3754 tcg_gen_andc_tl(cpu_T
[0], cpu_regs
[s
->vex_v
], cpu_T
[0]);
3755 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3756 gen_op_update1_cc();
3757 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
3760 case 0x0f7: /* bextr Gy, Ey, By */
3761 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)
3762 || !(s
->prefix
& PREFIX_VEX
)
3766 ot
= mo_64_32(s
->dflag
);
3770 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3771 /* Extract START, and shift the operand.
3772 Shifts larger than operand size get zeros. */
3773 tcg_gen_ext8u_tl(cpu_A0
, cpu_regs
[s
->vex_v
]);
3774 tcg_gen_shr_tl(cpu_T
[0], cpu_T
[0], cpu_A0
);
3776 bound
= tcg_const_tl(ot
== MO_64
? 63 : 31);
3777 zero
= tcg_const_tl(0);
3778 tcg_gen_movcond_tl(TCG_COND_LEU
, cpu_T
[0], cpu_A0
, bound
,
3780 tcg_temp_free(zero
);
3782 /* Extract the LEN into a mask. Lengths larger than
3783 operand size get all ones. */
3784 tcg_gen_shri_tl(cpu_A0
, cpu_regs
[s
->vex_v
], 8);
3785 tcg_gen_ext8u_tl(cpu_A0
, cpu_A0
);
3786 tcg_gen_movcond_tl(TCG_COND_LEU
, cpu_A0
, cpu_A0
, bound
,
3788 tcg_temp_free(bound
);
3789 tcg_gen_movi_tl(cpu_T
[1], 1);
3790 tcg_gen_shl_tl(cpu_T
[1], cpu_T
[1], cpu_A0
);
3791 tcg_gen_subi_tl(cpu_T
[1], cpu_T
[1], 1);
3792 tcg_gen_and_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
3794 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3795 gen_op_update1_cc();
3796 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
3800 case 0x0f5: /* bzhi Gy, Ey, By */
3801 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3802 || !(s
->prefix
& PREFIX_VEX
)
3806 ot
= mo_64_32(s
->dflag
);
3807 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3808 tcg_gen_ext8u_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3810 TCGv bound
= tcg_const_tl(ot
== MO_64
? 63 : 31);
3811 /* Note that since we're using BMILG (in order to get O
3812 cleared) we need to store the inverse into C. */
3813 tcg_gen_setcond_tl(TCG_COND_LT
, cpu_cc_src
,
3815 tcg_gen_movcond_tl(TCG_COND_GT
, cpu_T
[1], cpu_T
[1],
3816 bound
, bound
, cpu_T
[1]);
3817 tcg_temp_free(bound
);
3819 tcg_gen_movi_tl(cpu_A0
, -1);
3820 tcg_gen_shl_tl(cpu_A0
, cpu_A0
, cpu_T
[1]);
3821 tcg_gen_andc_tl(cpu_T
[0], cpu_T
[0], cpu_A0
);
3822 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
3823 gen_op_update1_cc();
3824 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
3827 case 0x3f6: /* mulx By, Gy, rdx, Ey */
3828 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3829 || !(s
->prefix
& PREFIX_VEX
)
3833 ot
= mo_64_32(s
->dflag
);
3834 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3837 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
3838 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_regs
[R_EDX
]);
3839 tcg_gen_mulu2_i32(cpu_tmp2_i32
, cpu_tmp3_i32
,
3840 cpu_tmp2_i32
, cpu_tmp3_i32
);
3841 tcg_gen_extu_i32_tl(cpu_regs
[s
->vex_v
], cpu_tmp2_i32
);
3842 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp3_i32
);
3844 #ifdef TARGET_X86_64
3846 tcg_gen_mulu2_i64(cpu_regs
[s
->vex_v
], cpu_regs
[reg
],
3847 cpu_T
[0], cpu_regs
[R_EDX
]);
3853 case 0x3f5: /* pdep Gy, By, Ey */
3854 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3855 || !(s
->prefix
& PREFIX_VEX
)
3859 ot
= mo_64_32(s
->dflag
);
3860 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3861 /* Note that by zero-extending the mask operand, we
3862 automatically handle zero-extending the result. */
3864 tcg_gen_mov_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3866 tcg_gen_ext32u_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3868 gen_helper_pdep(cpu_regs
[reg
], cpu_T
[0], cpu_T
[1]);
3871 case 0x2f5: /* pext Gy, By, Ey */
3872 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3873 || !(s
->prefix
& PREFIX_VEX
)
3877 ot
= mo_64_32(s
->dflag
);
3878 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3879 /* Note that by zero-extending the mask operand, we
3880 automatically handle zero-extending the result. */
3882 tcg_gen_mov_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3884 tcg_gen_ext32u_tl(cpu_T
[1], cpu_regs
[s
->vex_v
]);
3886 gen_helper_pext(cpu_regs
[reg
], cpu_T
[0], cpu_T
[1]);
3889 case 0x1f6: /* adcx Gy, Ey */
3890 case 0x2f6: /* adox Gy, Ey */
3891 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_ADX
)) {
3894 TCGv carry_in
, carry_out
, zero
;
3897 ot
= mo_64_32(s
->dflag
);
3898 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3900 /* Re-use the carry-out from a previous round. */
3901 TCGV_UNUSED(carry_in
);
3902 carry_out
= (b
== 0x1f6 ? cpu_cc_dst
: cpu_cc_src2
);
3906 carry_in
= cpu_cc_dst
;
3907 end_op
= CC_OP_ADCX
;
3909 end_op
= CC_OP_ADCOX
;
3914 end_op
= CC_OP_ADCOX
;
3916 carry_in
= cpu_cc_src2
;
3917 end_op
= CC_OP_ADOX
;
3921 end_op
= CC_OP_ADCOX
;
3922 carry_in
= carry_out
;
3925 end_op
= (b
== 0x1f6 ? CC_OP_ADCX
: CC_OP_ADOX
);
3928 /* If we can't reuse carry-out, get it out of EFLAGS. */
3929 if (TCGV_IS_UNUSED(carry_in
)) {
3930 if (s
->cc_op
!= CC_OP_ADCX
&& s
->cc_op
!= CC_OP_ADOX
) {
3931 gen_compute_eflags(s
);
3933 carry_in
= cpu_tmp0
;
3934 tcg_gen_shri_tl(carry_in
, cpu_cc_src
,
3935 ctz32(b
== 0x1f6 ? CC_C
: CC_O
));
3936 tcg_gen_andi_tl(carry_in
, carry_in
, 1);
3940 #ifdef TARGET_X86_64
3942 /* If we know TL is 64-bit, and we want a 32-bit
3943 result, just do everything in 64-bit arithmetic. */
3944 tcg_gen_ext32u_i64(cpu_regs
[reg
], cpu_regs
[reg
]);
3945 tcg_gen_ext32u_i64(cpu_T
[0], cpu_T
[0]);
3946 tcg_gen_add_i64(cpu_T
[0], cpu_T
[0], cpu_regs
[reg
]);
3947 tcg_gen_add_i64(cpu_T
[0], cpu_T
[0], carry_in
);
3948 tcg_gen_ext32u_i64(cpu_regs
[reg
], cpu_T
[0]);
3949 tcg_gen_shri_i64(carry_out
, cpu_T
[0], 32);
3953 /* Otherwise compute the carry-out in two steps. */
3954 zero
= tcg_const_tl(0);
3955 tcg_gen_add2_tl(cpu_T
[0], carry_out
,
3958 tcg_gen_add2_tl(cpu_regs
[reg
], carry_out
,
3959 cpu_regs
[reg
], carry_out
,
3961 tcg_temp_free(zero
);
3964 set_cc_op(s
, end_op
);
3968 case 0x1f7: /* shlx Gy, Ey, By */
3969 case 0x2f7: /* sarx Gy, Ey, By */
3970 case 0x3f7: /* shrx Gy, Ey, By */
3971 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
3972 || !(s
->prefix
& PREFIX_VEX
)
3976 ot
= mo_64_32(s
->dflag
);
3977 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3979 tcg_gen_andi_tl(cpu_T
[1], cpu_regs
[s
->vex_v
], 63);
3981 tcg_gen_andi_tl(cpu_T
[1], cpu_regs
[s
->vex_v
], 31);
3984 tcg_gen_shl_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
3985 } else if (b
== 0x2f7) {
3987 tcg_gen_ext32s_tl(cpu_T
[0], cpu_T
[0]);
3989 tcg_gen_sar_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
3992 tcg_gen_ext32u_tl(cpu_T
[0], cpu_T
[0]);
3994 tcg_gen_shr_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
3996 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
4002 case 0x3f3: /* Group 17 */
4003 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)
4004 || !(s
->prefix
& PREFIX_VEX
)
4008 ot
= mo_64_32(s
->dflag
);
4009 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
4012 case 1: /* blsr By,Ey */
4013 tcg_gen_neg_tl(cpu_T
[1], cpu_T
[0]);
4014 tcg_gen_and_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4015 gen_op_mov_reg_v(ot
, s
->vex_v
, cpu_T
[0]);
4016 gen_op_update2_cc();
4017 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
4020 case 2: /* blsmsk By,Ey */
4021 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[0]);
4022 tcg_gen_subi_tl(cpu_T
[0], cpu_T
[0], 1);
4023 tcg_gen_xor_tl(cpu_T
[0], cpu_T
[0], cpu_cc_src
);
4024 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4025 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
4028 case 3: /* blsi By, Ey */
4029 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[0]);
4030 tcg_gen_subi_tl(cpu_T
[0], cpu_T
[0], 1);
4031 tcg_gen_and_tl(cpu_T
[0], cpu_T
[0], cpu_cc_src
);
4032 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4033 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
4049 modrm
= cpu_ldub_code(env
, s
->pc
++);
4051 reg
= ((modrm
>> 3) & 7) | rex_r
;
4052 mod
= (modrm
>> 6) & 3;
4057 sse_fn_eppi
= sse_op_table7
[b
].op
[b1
];
4061 if (!(s
->cpuid_ext_features
& sse_op_table7
[b
].ext_mask
))
4064 if (sse_fn_eppi
== SSE_SPECIAL
) {
4065 ot
= mo_64_32(s
->dflag
);
4066 rm
= (modrm
& 7) | REX_B(s
);
4068 gen_lea_modrm(env
, s
, modrm
);
4069 reg
= ((modrm
>> 3) & 7) | rex_r
;
4070 val
= cpu_ldub_code(env
, s
->pc
++);
4072 case 0x14: /* pextrb */
4073 tcg_gen_ld8u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
4074 xmm_regs
[reg
].XMM_B(val
& 15)));
4076 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4078 tcg_gen_qemu_st_tl(cpu_T
[0], cpu_A0
,
4079 s
->mem_index
, MO_UB
);
4082 case 0x15: /* pextrw */
4083 tcg_gen_ld16u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
4084 xmm_regs
[reg
].XMM_W(val
& 7)));
4086 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4088 tcg_gen_qemu_st_tl(cpu_T
[0], cpu_A0
,
4089 s
->mem_index
, MO_LEUW
);
4093 if (ot
== MO_32
) { /* pextrd */
4094 tcg_gen_ld_i32(cpu_tmp2_i32
, cpu_env
,
4095 offsetof(CPUX86State
,
4096 xmm_regs
[reg
].XMM_L(val
& 3)));
4098 tcg_gen_extu_i32_tl(cpu_regs
[rm
], cpu_tmp2_i32
);
4100 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
4101 s
->mem_index
, MO_LEUL
);
4103 } else { /* pextrq */
4104 #ifdef TARGET_X86_64
4105 tcg_gen_ld_i64(cpu_tmp1_i64
, cpu_env
,
4106 offsetof(CPUX86State
,
4107 xmm_regs
[reg
].XMM_Q(val
& 1)));
4109 tcg_gen_mov_i64(cpu_regs
[rm
], cpu_tmp1_i64
);
4111 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
,
4112 s
->mem_index
, MO_LEQ
);
4119 case 0x17: /* extractps */
4120 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
4121 xmm_regs
[reg
].XMM_L(val
& 3)));
4123 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4125 tcg_gen_qemu_st_tl(cpu_T
[0], cpu_A0
,
4126 s
->mem_index
, MO_LEUL
);
4129 case 0x20: /* pinsrb */
4131 gen_op_mov_v_reg(MO_32
, cpu_T
[0], rm
);
4133 tcg_gen_qemu_ld_tl(cpu_T
[0], cpu_A0
,
4134 s
->mem_index
, MO_UB
);
4136 tcg_gen_st8_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,
4137 xmm_regs
[reg
].XMM_B(val
& 15)));
4139 case 0x21: /* insertps */
4141 tcg_gen_ld_i32(cpu_tmp2_i32
, cpu_env
,
4142 offsetof(CPUX86State
,xmm_regs
[rm
]
4143 .XMM_L((val
>> 6) & 3)));
4145 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
4146 s
->mem_index
, MO_LEUL
);
4148 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
,
4149 offsetof(CPUX86State
,xmm_regs
[reg
]
4150 .XMM_L((val
>> 4) & 3)));
4152 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4153 cpu_env
, offsetof(CPUX86State
,
4154 xmm_regs
[reg
].XMM_L(0)));
4156 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4157 cpu_env
, offsetof(CPUX86State
,
4158 xmm_regs
[reg
].XMM_L(1)));
4160 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4161 cpu_env
, offsetof(CPUX86State
,
4162 xmm_regs
[reg
].XMM_L(2)));
4164 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4165 cpu_env
, offsetof(CPUX86State
,
4166 xmm_regs
[reg
].XMM_L(3)));
4169 if (ot
== MO_32
) { /* pinsrd */
4171 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_regs
[rm
]);
4173 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
4174 s
->mem_index
, MO_LEUL
);
4176 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
,
4177 offsetof(CPUX86State
,
4178 xmm_regs
[reg
].XMM_L(val
& 3)));
4179 } else { /* pinsrq */
4180 #ifdef TARGET_X86_64
4182 gen_op_mov_v_reg(ot
, cpu_tmp1_i64
, rm
);
4184 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
,
4185 s
->mem_index
, MO_LEQ
);
4187 tcg_gen_st_i64(cpu_tmp1_i64
, cpu_env
,
4188 offsetof(CPUX86State
,
4189 xmm_regs
[reg
].XMM_Q(val
& 1)));
4200 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
4202 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
| REX_B(s
)]);
4204 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
4205 gen_lea_modrm(env
, s
, modrm
);
4206 gen_ldo_env_A0(s
, op2_offset
);
4209 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
].mmx
);
4211 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
4213 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
4214 gen_lea_modrm(env
, s
, modrm
);
4215 gen_ldq_env_A0(s
, op2_offset
);
4218 val
= cpu_ldub_code(env
, s
->pc
++);
4220 if ((b
& 0xfc) == 0x60) { /* pcmpXstrX */
4221 set_cc_op(s
, CC_OP_EFLAGS
);
4223 if (s
->dflag
== MO_64
) {
4224 /* The helper must use entire 64-bit gp registers */
4229 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4230 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4231 sse_fn_eppi(cpu_env
, cpu_ptr0
, cpu_ptr1
, tcg_const_i32(val
));
4235 /* Various integer extensions at 0f 3a f[0-f]. */
4236 b
= modrm
| (b1
<< 8);
4237 modrm
= cpu_ldub_code(env
, s
->pc
++);
4238 reg
= ((modrm
>> 3) & 7) | rex_r
;
4241 case 0x3f0: /* rorx Gy,Ey, Ib */
4242 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI2
)
4243 || !(s
->prefix
& PREFIX_VEX
)
4247 ot
= mo_64_32(s
->dflag
);
4248 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
4249 b
= cpu_ldub_code(env
, s
->pc
++);
4251 tcg_gen_rotri_tl(cpu_T
[0], cpu_T
[0], b
& 63);
4253 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4254 tcg_gen_rotri_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, b
& 31);
4255 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
4257 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
4269 /* generic MMX or SSE operation */
4271 case 0x70: /* pshufx insn */
4272 case 0xc6: /* pshufx insn */
4273 case 0xc2: /* compare insns */
4280 op1_offset
= offsetof(CPUX86State
,xmm_regs
[reg
]);
4284 gen_lea_modrm(env
, s
, modrm
);
4285 op2_offset
= offsetof(CPUX86State
,xmm_t0
);
4291 /* Most sse scalar operations. */
4294 } else if (b1
== 3) {
4299 case 0x2e: /* ucomis[sd] */
4300 case 0x2f: /* comis[sd] */
4312 gen_op_ld_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
4313 tcg_gen_st32_tl(cpu_T
[0], cpu_env
,
4314 offsetof(CPUX86State
,xmm_t0
.XMM_L(0)));
4318 gen_ldq_env_A0(s
, offsetof(CPUX86State
, xmm_t0
.XMM_D(0)));
4321 /* 128 bit access */
4322 gen_ldo_env_A0(s
, op2_offset
);
4326 rm
= (modrm
& 7) | REX_B(s
);
4327 op2_offset
= offsetof(CPUX86State
,xmm_regs
[rm
]);
4330 op1_offset
= offsetof(CPUX86State
,fpregs
[reg
].mmx
);
4332 gen_lea_modrm(env
, s
, modrm
);
4333 op2_offset
= offsetof(CPUX86State
,mmx_t0
);
4334 gen_ldq_env_A0(s
, op2_offset
);
4337 op2_offset
= offsetof(CPUX86State
,fpregs
[rm
].mmx
);
4341 case 0x0f: /* 3DNow! data insns */
4342 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_3DNOW
))
4344 val
= cpu_ldub_code(env
, s
->pc
++);
4345 sse_fn_epp
= sse_op_table5
[val
];
4349 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4350 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4351 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
4353 case 0x70: /* pshufx insn */
4354 case 0xc6: /* pshufx insn */
4355 val
= cpu_ldub_code(env
, s
->pc
++);
4356 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4357 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4358 /* XXX: introduce a new table? */
4359 sse_fn_ppi
= (SSEFunc_0_ppi
)sse_fn_epp
;
4360 sse_fn_ppi(cpu_ptr0
, cpu_ptr1
, tcg_const_i32(val
));
4364 val
= cpu_ldub_code(env
, s
->pc
++);
4367 sse_fn_epp
= sse_op_table4
[val
][b1
];
4369 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4370 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4371 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
4374 /* maskmov : we must prepare A0 */
4377 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[R_EDI
]);
4378 gen_extu(s
->aflag
, cpu_A0
);
4379 gen_add_A0_ds_seg(s
);
4381 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4382 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4383 /* XXX: introduce a new table? */
4384 sse_fn_eppt
= (SSEFunc_0_eppt
)sse_fn_epp
;
4385 sse_fn_eppt(cpu_env
, cpu_ptr0
, cpu_ptr1
, cpu_A0
);
4388 tcg_gen_addi_ptr(cpu_ptr0
, cpu_env
, op1_offset
);
4389 tcg_gen_addi_ptr(cpu_ptr1
, cpu_env
, op2_offset
);
4390 sse_fn_epp(cpu_env
, cpu_ptr0
, cpu_ptr1
);
4393 if (b
== 0x2e || b
== 0x2f) {
4394 set_cc_op(s
, CC_OP_EFLAGS
);
4399 /* convert one instruction. s->is_jmp is set if the translation must
4400 be stopped. Return the next pc value */
4401 static target_ulong
disas_insn(CPUX86State
*env
, DisasContext
*s
,
4402 target_ulong pc_start
)
4406 TCGMemOp ot
, aflag
, dflag
;
4407 int modrm
, reg
, rm
, mod
, op
, opreg
, val
;
4408 target_ulong next_eip
, tval
;
4411 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
| CPU_LOG_TB_OP_OPT
))) {
4412 tcg_gen_debug_insn_start(pc_start
);
4419 #ifdef TARGET_X86_64
4424 s
->rip_offset
= 0; /* for relative ip address */
4428 b
= cpu_ldub_code(env
, s
->pc
);
4430 /* Collect prefixes. */
4433 prefixes
|= PREFIX_REPZ
;
4436 prefixes
|= PREFIX_REPNZ
;
4439 prefixes
|= PREFIX_LOCK
;
4460 prefixes
|= PREFIX_DATA
;
4463 prefixes
|= PREFIX_ADR
;
4465 #ifdef TARGET_X86_64
4469 rex_w
= (b
>> 3) & 1;
4470 rex_r
= (b
& 0x4) << 1;
4471 s
->rex_x
= (b
& 0x2) << 2;
4472 REX_B(s
) = (b
& 0x1) << 3;
4473 x86_64_hregs
= 1; /* select uniform byte register addressing */
4478 case 0xc5: /* 2-byte VEX */
4479 case 0xc4: /* 3-byte VEX */
4480 /* VEX prefixes cannot be used except in 32-bit mode.
4481 Otherwise the instruction is LES or LDS. */
4482 if (s
->code32
&& !s
->vm86
) {
4483 static const int pp_prefix
[4] = {
4484 0, PREFIX_DATA
, PREFIX_REPZ
, PREFIX_REPNZ
4486 int vex3
, vex2
= cpu_ldub_code(env
, s
->pc
);
4488 if (!CODE64(s
) && (vex2
& 0xc0) != 0xc0) {
4489 /* 4.1.4.6: In 32-bit mode, bits [7:6] must be 11b,
4490 otherwise the instruction is LES or LDS. */
4495 /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
4496 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
4497 | PREFIX_LOCK
| PREFIX_DATA
)) {
4500 #ifdef TARGET_X86_64
4505 rex_r
= (~vex2
>> 4) & 8;
4508 b
= cpu_ldub_code(env
, s
->pc
++);
4510 #ifdef TARGET_X86_64
4511 s
->rex_x
= (~vex2
>> 3) & 8;
4512 s
->rex_b
= (~vex2
>> 2) & 8;
4514 vex3
= cpu_ldub_code(env
, s
->pc
++);
4515 rex_w
= (vex3
>> 7) & 1;
4516 switch (vex2
& 0x1f) {
4517 case 0x01: /* Implied 0f leading opcode bytes. */
4518 b
= cpu_ldub_code(env
, s
->pc
++) | 0x100;
4520 case 0x02: /* Implied 0f 38 leading opcode bytes. */
4523 case 0x03: /* Implied 0f 3a leading opcode bytes. */
4526 default: /* Reserved for future use. */
4530 s
->vex_v
= (~vex3
>> 3) & 0xf;
4531 s
->vex_l
= (vex3
>> 2) & 1;
4532 prefixes
|= pp_prefix
[vex3
& 3] | PREFIX_VEX
;
4537 /* Post-process prefixes. */
4539 /* In 64-bit mode, the default data size is 32-bit. Select 64-bit
4540 data with rex_w, and 16-bit data with 0x66; rex_w takes precedence
4541 over 0x66 if both are present. */
4542 dflag
= (rex_w
> 0 ? MO_64
: prefixes
& PREFIX_DATA
? MO_16
: MO_32
);
4543 /* In 64-bit mode, 0x67 selects 32-bit addressing. */
4544 aflag
= (prefixes
& PREFIX_ADR
? MO_32
: MO_64
);
4546 /* In 16/32-bit mode, 0x66 selects the opposite data size. */
4547 if (s
->code32
^ ((prefixes
& PREFIX_DATA
) != 0)) {
4552 /* In 16/32-bit mode, 0x67 selects the opposite addressing. */
4553 if (s
->code32
^ ((prefixes
& PREFIX_ADR
) != 0)) {
4560 s
->prefix
= prefixes
;
4564 /* lock generation */
4565 if (prefixes
& PREFIX_LOCK
)
4568 /* now check op code */
4572 /**************************/
4573 /* extended op code */
4574 b
= cpu_ldub_code(env
, s
->pc
++) | 0x100;
4577 /**************************/
4592 ot
= mo_b_d(b
, dflag
);
4595 case 0: /* OP Ev, Gv */
4596 modrm
= cpu_ldub_code(env
, s
->pc
++);
4597 reg
= ((modrm
>> 3) & 7) | rex_r
;
4598 mod
= (modrm
>> 6) & 3;
4599 rm
= (modrm
& 7) | REX_B(s
);
4601 gen_lea_modrm(env
, s
, modrm
);
4603 } else if (op
== OP_XORL
&& rm
== reg
) {
4605 /* xor reg, reg optimisation */
4606 set_cc_op(s
, CC_OP_CLR
);
4607 tcg_gen_movi_tl(cpu_T
[0], 0);
4608 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
4613 gen_op_mov_v_reg(ot
, cpu_T
[1], reg
);
4614 gen_op(s
, op
, ot
, opreg
);
4616 case 1: /* OP Gv, Ev */
4617 modrm
= cpu_ldub_code(env
, s
->pc
++);
4618 mod
= (modrm
>> 6) & 3;
4619 reg
= ((modrm
>> 3) & 7) | rex_r
;
4620 rm
= (modrm
& 7) | REX_B(s
);
4622 gen_lea_modrm(env
, s
, modrm
);
4623 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
4624 } else if (op
== OP_XORL
&& rm
== reg
) {
4627 gen_op_mov_v_reg(ot
, cpu_T
[1], rm
);
4629 gen_op(s
, op
, ot
, reg
);
4631 case 2: /* OP A, Iv */
4632 val
= insn_get(env
, s
, ot
);
4633 tcg_gen_movi_tl(cpu_T
[1], val
);
4634 gen_op(s
, op
, ot
, OR_EAX
);
4643 case 0x80: /* GRP1 */
4649 ot
= mo_b_d(b
, dflag
);
4651 modrm
= cpu_ldub_code(env
, s
->pc
++);
4652 mod
= (modrm
>> 6) & 3;
4653 rm
= (modrm
& 7) | REX_B(s
);
4654 op
= (modrm
>> 3) & 7;
4660 s
->rip_offset
= insn_const_size(ot
);
4661 gen_lea_modrm(env
, s
, modrm
);
4672 val
= insn_get(env
, s
, ot
);
4675 val
= (int8_t)insn_get(env
, s
, MO_8
);
4678 tcg_gen_movi_tl(cpu_T
[1], val
);
4679 gen_op(s
, op
, ot
, opreg
);
4683 /**************************/
4684 /* inc, dec, and other misc arith */
4685 case 0x40 ... 0x47: /* inc Gv */
4687 gen_inc(s
, ot
, OR_EAX
+ (b
& 7), 1);
4689 case 0x48 ... 0x4f: /* dec Gv */
4691 gen_inc(s
, ot
, OR_EAX
+ (b
& 7), -1);
4693 case 0xf6: /* GRP3 */
4695 ot
= mo_b_d(b
, dflag
);
4697 modrm
= cpu_ldub_code(env
, s
->pc
++);
4698 mod
= (modrm
>> 6) & 3;
4699 rm
= (modrm
& 7) | REX_B(s
);
4700 op
= (modrm
>> 3) & 7;
4703 s
->rip_offset
= insn_const_size(ot
);
4704 gen_lea_modrm(env
, s
, modrm
);
4705 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
4707 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
4712 val
= insn_get(env
, s
, ot
);
4713 tcg_gen_movi_tl(cpu_T
[1], val
);
4714 gen_op_testl_T0_T1_cc();
4715 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
4718 tcg_gen_not_tl(cpu_T
[0], cpu_T
[0]);
4720 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
4722 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4726 tcg_gen_neg_tl(cpu_T
[0], cpu_T
[0]);
4728 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
4730 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
4732 gen_op_update_neg_cc();
4733 set_cc_op(s
, CC_OP_SUBB
+ ot
);
4738 gen_op_mov_v_reg(MO_8
, cpu_T
[1], R_EAX
);
4739 tcg_gen_ext8u_tl(cpu_T
[0], cpu_T
[0]);
4740 tcg_gen_ext8u_tl(cpu_T
[1], cpu_T
[1]);
4741 /* XXX: use 32 bit mul which could be faster */
4742 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4743 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
4744 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4745 tcg_gen_andi_tl(cpu_cc_src
, cpu_T
[0], 0xff00);
4746 set_cc_op(s
, CC_OP_MULB
);
4749 gen_op_mov_v_reg(MO_16
, cpu_T
[1], R_EAX
);
4750 tcg_gen_ext16u_tl(cpu_T
[0], cpu_T
[0]);
4751 tcg_gen_ext16u_tl(cpu_T
[1], cpu_T
[1]);
4752 /* XXX: use 32 bit mul which could be faster */
4753 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4754 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
4755 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4756 tcg_gen_shri_tl(cpu_T
[0], cpu_T
[0], 16);
4757 gen_op_mov_reg_v(MO_16
, R_EDX
, cpu_T
[0]);
4758 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[0]);
4759 set_cc_op(s
, CC_OP_MULW
);
4763 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4764 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_regs
[R_EAX
]);
4765 tcg_gen_mulu2_i32(cpu_tmp2_i32
, cpu_tmp3_i32
,
4766 cpu_tmp2_i32
, cpu_tmp3_i32
);
4767 tcg_gen_extu_i32_tl(cpu_regs
[R_EAX
], cpu_tmp2_i32
);
4768 tcg_gen_extu_i32_tl(cpu_regs
[R_EDX
], cpu_tmp3_i32
);
4769 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4770 tcg_gen_mov_tl(cpu_cc_src
, cpu_regs
[R_EDX
]);
4771 set_cc_op(s
, CC_OP_MULL
);
4773 #ifdef TARGET_X86_64
4775 tcg_gen_mulu2_i64(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
],
4776 cpu_T
[0], cpu_regs
[R_EAX
]);
4777 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4778 tcg_gen_mov_tl(cpu_cc_src
, cpu_regs
[R_EDX
]);
4779 set_cc_op(s
, CC_OP_MULQ
);
4787 gen_op_mov_v_reg(MO_8
, cpu_T
[1], R_EAX
);
4788 tcg_gen_ext8s_tl(cpu_T
[0], cpu_T
[0]);
4789 tcg_gen_ext8s_tl(cpu_T
[1], cpu_T
[1]);
4790 /* XXX: use 32 bit mul which could be faster */
4791 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4792 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
4793 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4794 tcg_gen_ext8s_tl(cpu_tmp0
, cpu_T
[0]);
4795 tcg_gen_sub_tl(cpu_cc_src
, cpu_T
[0], cpu_tmp0
);
4796 set_cc_op(s
, CC_OP_MULB
);
4799 gen_op_mov_v_reg(MO_16
, cpu_T
[1], R_EAX
);
4800 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
4801 tcg_gen_ext16s_tl(cpu_T
[1], cpu_T
[1]);
4802 /* XXX: use 32 bit mul which could be faster */
4803 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
4804 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
4805 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
4806 tcg_gen_ext16s_tl(cpu_tmp0
, cpu_T
[0]);
4807 tcg_gen_sub_tl(cpu_cc_src
, cpu_T
[0], cpu_tmp0
);
4808 tcg_gen_shri_tl(cpu_T
[0], cpu_T
[0], 16);
4809 gen_op_mov_reg_v(MO_16
, R_EDX
, cpu_T
[0]);
4810 set_cc_op(s
, CC_OP_MULW
);
4814 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4815 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_regs
[R_EAX
]);
4816 tcg_gen_muls2_i32(cpu_tmp2_i32
, cpu_tmp3_i32
,
4817 cpu_tmp2_i32
, cpu_tmp3_i32
);
4818 tcg_gen_extu_i32_tl(cpu_regs
[R_EAX
], cpu_tmp2_i32
);
4819 tcg_gen_extu_i32_tl(cpu_regs
[R_EDX
], cpu_tmp3_i32
);
4820 tcg_gen_sari_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, 31);
4821 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4822 tcg_gen_sub_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, cpu_tmp3_i32
);
4823 tcg_gen_extu_i32_tl(cpu_cc_src
, cpu_tmp2_i32
);
4824 set_cc_op(s
, CC_OP_MULL
);
4826 #ifdef TARGET_X86_64
4828 tcg_gen_muls2_i64(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
],
4829 cpu_T
[0], cpu_regs
[R_EAX
]);
4830 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
4831 tcg_gen_sari_tl(cpu_cc_src
, cpu_regs
[R_EAX
], 63);
4832 tcg_gen_sub_tl(cpu_cc_src
, cpu_cc_src
, cpu_regs
[R_EDX
]);
4833 set_cc_op(s
, CC_OP_MULQ
);
4841 gen_jmp_im(pc_start
- s
->cs_base
);
4842 gen_helper_divb_AL(cpu_env
, cpu_T
[0]);
4845 gen_jmp_im(pc_start
- s
->cs_base
);
4846 gen_helper_divw_AX(cpu_env
, cpu_T
[0]);
4850 gen_jmp_im(pc_start
- s
->cs_base
);
4851 gen_helper_divl_EAX(cpu_env
, cpu_T
[0]);
4853 #ifdef TARGET_X86_64
4855 gen_jmp_im(pc_start
- s
->cs_base
);
4856 gen_helper_divq_EAX(cpu_env
, cpu_T
[0]);
4864 gen_jmp_im(pc_start
- s
->cs_base
);
4865 gen_helper_idivb_AL(cpu_env
, cpu_T
[0]);
4868 gen_jmp_im(pc_start
- s
->cs_base
);
4869 gen_helper_idivw_AX(cpu_env
, cpu_T
[0]);
4873 gen_jmp_im(pc_start
- s
->cs_base
);
4874 gen_helper_idivl_EAX(cpu_env
, cpu_T
[0]);
4876 #ifdef TARGET_X86_64
4878 gen_jmp_im(pc_start
- s
->cs_base
);
4879 gen_helper_idivq_EAX(cpu_env
, cpu_T
[0]);
4889 case 0xfe: /* GRP4 */
4890 case 0xff: /* GRP5 */
4891 ot
= mo_b_d(b
, dflag
);
4893 modrm
= cpu_ldub_code(env
, s
->pc
++);
4894 mod
= (modrm
>> 6) & 3;
4895 rm
= (modrm
& 7) | REX_B(s
);
4896 op
= (modrm
>> 3) & 7;
4897 if (op
>= 2 && b
== 0xfe) {
4901 if (op
== 2 || op
== 4) {
4902 /* operand size for jumps is 64 bit */
4904 } else if (op
== 3 || op
== 5) {
4905 ot
= dflag
!= MO_16
? MO_32
+ (rex_w
== 1) : MO_16
;
4906 } else if (op
== 6) {
4907 /* default push size is 64 bit */
4908 ot
= mo_pushpop(s
, dflag
);
4912 gen_lea_modrm(env
, s
, modrm
);
4913 if (op
>= 2 && op
!= 3 && op
!= 5)
4914 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
4916 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
4920 case 0: /* inc Ev */
4925 gen_inc(s
, ot
, opreg
, 1);
4927 case 1: /* dec Ev */
4932 gen_inc(s
, ot
, opreg
, -1);
4934 case 2: /* call Ev */
4935 /* XXX: optimize if memory (no 'and' is necessary) */
4936 if (dflag
== MO_16
) {
4937 tcg_gen_ext16u_tl(cpu_T
[0], cpu_T
[0]);
4939 next_eip
= s
->pc
- s
->cs_base
;
4940 tcg_gen_movi_tl(cpu_T
[1], next_eip
);
4941 gen_push_v(s
, cpu_T
[1]);
4942 gen_op_jmp_v(cpu_T
[0]);
4945 case 3: /* lcall Ev */
4946 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
4947 gen_add_A0_im(s
, 1 << ot
);
4948 gen_op_ld_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
4950 if (s
->pe
&& !s
->vm86
) {
4951 gen_update_cc_op(s
);
4952 gen_jmp_im(pc_start
- s
->cs_base
);
4953 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4954 gen_helper_lcall_protected(cpu_env
, cpu_tmp2_i32
, cpu_T
[1],
4955 tcg_const_i32(dflag
- 1),
4956 tcg_const_i32(s
->pc
- pc_start
));
4958 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4959 gen_helper_lcall_real(cpu_env
, cpu_tmp2_i32
, cpu_T
[1],
4960 tcg_const_i32(dflag
- 1),
4961 tcg_const_i32(s
->pc
- s
->cs_base
));
4965 case 4: /* jmp Ev */
4966 if (dflag
== MO_16
) {
4967 tcg_gen_ext16u_tl(cpu_T
[0], cpu_T
[0]);
4969 gen_op_jmp_v(cpu_T
[0]);
4972 case 5: /* ljmp Ev */
4973 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
4974 gen_add_A0_im(s
, 1 << ot
);
4975 gen_op_ld_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
4977 if (s
->pe
&& !s
->vm86
) {
4978 gen_update_cc_op(s
);
4979 gen_jmp_im(pc_start
- s
->cs_base
);
4980 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
4981 gen_helper_ljmp_protected(cpu_env
, cpu_tmp2_i32
, cpu_T
[1],
4982 tcg_const_i32(s
->pc
- pc_start
));
4984 gen_op_movl_seg_T0_vm(R_CS
);
4985 gen_op_jmp_v(cpu_T
[1]);
4989 case 6: /* push Ev */
4990 gen_push_v(s
, cpu_T
[0]);
4997 case 0x84: /* test Ev, Gv */
4999 ot
= mo_b_d(b
, dflag
);
5001 modrm
= cpu_ldub_code(env
, s
->pc
++);
5002 reg
= ((modrm
>> 3) & 7) | rex_r
;
5004 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5005 gen_op_mov_v_reg(ot
, cpu_T
[1], reg
);
5006 gen_op_testl_T0_T1_cc();
5007 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
5010 case 0xa8: /* test eAX, Iv */
5012 ot
= mo_b_d(b
, dflag
);
5013 val
= insn_get(env
, s
, ot
);
5015 gen_op_mov_v_reg(ot
, cpu_T
[0], OR_EAX
);
5016 tcg_gen_movi_tl(cpu_T
[1], val
);
5017 gen_op_testl_T0_T1_cc();
5018 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
5021 case 0x98: /* CWDE/CBW */
5023 #ifdef TARGET_X86_64
5025 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EAX
);
5026 tcg_gen_ext32s_tl(cpu_T
[0], cpu_T
[0]);
5027 gen_op_mov_reg_v(MO_64
, R_EAX
, cpu_T
[0]);
5031 gen_op_mov_v_reg(MO_16
, cpu_T
[0], R_EAX
);
5032 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
5033 gen_op_mov_reg_v(MO_32
, R_EAX
, cpu_T
[0]);
5036 gen_op_mov_v_reg(MO_8
, cpu_T
[0], R_EAX
);
5037 tcg_gen_ext8s_tl(cpu_T
[0], cpu_T
[0]);
5038 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
5044 case 0x99: /* CDQ/CWD */
5046 #ifdef TARGET_X86_64
5048 gen_op_mov_v_reg(MO_64
, cpu_T
[0], R_EAX
);
5049 tcg_gen_sari_tl(cpu_T
[0], cpu_T
[0], 63);
5050 gen_op_mov_reg_v(MO_64
, R_EDX
, cpu_T
[0]);
5054 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EAX
);
5055 tcg_gen_ext32s_tl(cpu_T
[0], cpu_T
[0]);
5056 tcg_gen_sari_tl(cpu_T
[0], cpu_T
[0], 31);
5057 gen_op_mov_reg_v(MO_32
, R_EDX
, cpu_T
[0]);
5060 gen_op_mov_v_reg(MO_16
, cpu_T
[0], R_EAX
);
5061 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
5062 tcg_gen_sari_tl(cpu_T
[0], cpu_T
[0], 15);
5063 gen_op_mov_reg_v(MO_16
, R_EDX
, cpu_T
[0]);
5069 case 0x1af: /* imul Gv, Ev */
5070 case 0x69: /* imul Gv, Ev, I */
5073 modrm
= cpu_ldub_code(env
, s
->pc
++);
5074 reg
= ((modrm
>> 3) & 7) | rex_r
;
5076 s
->rip_offset
= insn_const_size(ot
);
5079 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5081 val
= insn_get(env
, s
, ot
);
5082 tcg_gen_movi_tl(cpu_T
[1], val
);
5083 } else if (b
== 0x6b) {
5084 val
= (int8_t)insn_get(env
, s
, MO_8
);
5085 tcg_gen_movi_tl(cpu_T
[1], val
);
5087 gen_op_mov_v_reg(ot
, cpu_T
[1], reg
);
5090 #ifdef TARGET_X86_64
5092 tcg_gen_muls2_i64(cpu_regs
[reg
], cpu_T
[1], cpu_T
[0], cpu_T
[1]);
5093 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[reg
]);
5094 tcg_gen_sari_tl(cpu_cc_src
, cpu_cc_dst
, 63);
5095 tcg_gen_sub_tl(cpu_cc_src
, cpu_cc_src
, cpu_T
[1]);
5099 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
5100 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[1]);
5101 tcg_gen_muls2_i32(cpu_tmp2_i32
, cpu_tmp3_i32
,
5102 cpu_tmp2_i32
, cpu_tmp3_i32
);
5103 tcg_gen_extu_i32_tl(cpu_regs
[reg
], cpu_tmp2_i32
);
5104 tcg_gen_sari_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, 31);
5105 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[reg
]);
5106 tcg_gen_sub_i32(cpu_tmp2_i32
, cpu_tmp2_i32
, cpu_tmp3_i32
);
5107 tcg_gen_extu_i32_tl(cpu_cc_src
, cpu_tmp2_i32
);
5110 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
5111 tcg_gen_ext16s_tl(cpu_T
[1], cpu_T
[1]);
5112 /* XXX: use 32 bit mul which could be faster */
5113 tcg_gen_mul_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
5114 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
5115 tcg_gen_ext16s_tl(cpu_tmp0
, cpu_T
[0]);
5116 tcg_gen_sub_tl(cpu_cc_src
, cpu_T
[0], cpu_tmp0
);
5117 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
5120 set_cc_op(s
, CC_OP_MULB
+ ot
);
5123 case 0x1c1: /* xadd Ev, Gv */
5124 ot
= mo_b_d(b
, dflag
);
5125 modrm
= cpu_ldub_code(env
, s
->pc
++);
5126 reg
= ((modrm
>> 3) & 7) | rex_r
;
5127 mod
= (modrm
>> 6) & 3;
5129 rm
= (modrm
& 7) | REX_B(s
);
5130 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
5131 gen_op_mov_v_reg(ot
, cpu_T
[1], rm
);
5132 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
5133 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5134 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
5136 gen_lea_modrm(env
, s
, modrm
);
5137 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
5138 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
5139 tcg_gen_add_tl(cpu_T
[0], cpu_T
[0], cpu_T
[1]);
5140 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
5141 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5143 gen_op_update2_cc();
5144 set_cc_op(s
, CC_OP_ADDB
+ ot
);
5147 case 0x1b1: /* cmpxchg Ev, Gv */
5150 TCGv t0
, t1
, t2
, a0
;
5152 ot
= mo_b_d(b
, dflag
);
5153 modrm
= cpu_ldub_code(env
, s
->pc
++);
5154 reg
= ((modrm
>> 3) & 7) | rex_r
;
5155 mod
= (modrm
>> 6) & 3;
5156 t0
= tcg_temp_local_new();
5157 t1
= tcg_temp_local_new();
5158 t2
= tcg_temp_local_new();
5159 a0
= tcg_temp_local_new();
5160 gen_op_mov_v_reg(ot
, t1
, reg
);
5162 rm
= (modrm
& 7) | REX_B(s
);
5163 gen_op_mov_v_reg(ot
, t0
, rm
);
5165 gen_lea_modrm(env
, s
, modrm
);
5166 tcg_gen_mov_tl(a0
, cpu_A0
);
5167 gen_op_ld_v(s
, ot
, t0
, a0
);
5168 rm
= 0; /* avoid warning */
5170 label1
= gen_new_label();
5171 tcg_gen_mov_tl(t2
, cpu_regs
[R_EAX
]);
5174 tcg_gen_brcond_tl(TCG_COND_EQ
, t2
, t0
, label1
);
5175 label2
= gen_new_label();
5177 gen_op_mov_reg_v(ot
, R_EAX
, t0
);
5179 gen_set_label(label1
);
5180 gen_op_mov_reg_v(ot
, rm
, t1
);
5182 /* perform no-op store cycle like physical cpu; must be
5183 before changing accumulator to ensure idempotency if
5184 the store faults and the instruction is restarted */
5185 gen_op_st_v(s
, ot
, t0
, a0
);
5186 gen_op_mov_reg_v(ot
, R_EAX
, t0
);
5188 gen_set_label(label1
);
5189 gen_op_st_v(s
, ot
, t1
, a0
);
5191 gen_set_label(label2
);
5192 tcg_gen_mov_tl(cpu_cc_src
, t0
);
5193 tcg_gen_mov_tl(cpu_cc_srcT
, t2
);
5194 tcg_gen_sub_tl(cpu_cc_dst
, t2
, t0
);
5195 set_cc_op(s
, CC_OP_SUBB
+ ot
);
5202 case 0x1c7: /* cmpxchg8b */
5203 modrm
= cpu_ldub_code(env
, s
->pc
++);
5204 mod
= (modrm
>> 6) & 3;
5205 if ((mod
== 3) || ((modrm
& 0x38) != 0x8))
5207 #ifdef TARGET_X86_64
5208 if (dflag
== MO_64
) {
5209 if (!(s
->cpuid_ext_features
& CPUID_EXT_CX16
))
5211 gen_jmp_im(pc_start
- s
->cs_base
);
5212 gen_update_cc_op(s
);
5213 gen_lea_modrm(env
, s
, modrm
);
5214 gen_helper_cmpxchg16b(cpu_env
, cpu_A0
);
5218 if (!(s
->cpuid_features
& CPUID_CX8
))
5220 gen_jmp_im(pc_start
- s
->cs_base
);
5221 gen_update_cc_op(s
);
5222 gen_lea_modrm(env
, s
, modrm
);
5223 gen_helper_cmpxchg8b(cpu_env
, cpu_A0
);
5225 set_cc_op(s
, CC_OP_EFLAGS
);
5228 /**************************/
5230 case 0x50 ... 0x57: /* push */
5231 gen_op_mov_v_reg(MO_32
, cpu_T
[0], (b
& 7) | REX_B(s
));
5232 gen_push_v(s
, cpu_T
[0]);
5234 case 0x58 ... 0x5f: /* pop */
5236 /* NOTE: order is important for pop %sp */
5237 gen_pop_update(s
, ot
);
5238 gen_op_mov_reg_v(ot
, (b
& 7) | REX_B(s
), cpu_T
[0]);
5240 case 0x60: /* pusha */
5245 case 0x61: /* popa */
5250 case 0x68: /* push Iv */
5252 ot
= mo_pushpop(s
, dflag
);
5254 val
= insn_get(env
, s
, ot
);
5256 val
= (int8_t)insn_get(env
, s
, MO_8
);
5257 tcg_gen_movi_tl(cpu_T
[0], val
);
5258 gen_push_v(s
, cpu_T
[0]);
5260 case 0x8f: /* pop Ev */
5261 modrm
= cpu_ldub_code(env
, s
->pc
++);
5262 mod
= (modrm
>> 6) & 3;
5265 /* NOTE: order is important for pop %sp */
5266 gen_pop_update(s
, ot
);
5267 rm
= (modrm
& 7) | REX_B(s
);
5268 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
5270 /* NOTE: order is important too for MMU exceptions */
5271 s
->popl_esp_hack
= 1 << ot
;
5272 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
5273 s
->popl_esp_hack
= 0;
5274 gen_pop_update(s
, ot
);
5277 case 0xc8: /* enter */
5280 val
= cpu_lduw_code(env
, s
->pc
);
5282 level
= cpu_ldub_code(env
, s
->pc
++);
5283 gen_enter(s
, val
, level
);
5286 case 0xc9: /* leave */
5287 /* XXX: exception not precise (ESP is updated before potential exception) */
5289 gen_op_mov_v_reg(MO_64
, cpu_T
[0], R_EBP
);
5290 gen_op_mov_reg_v(MO_64
, R_ESP
, cpu_T
[0]);
5291 } else if (s
->ss32
) {
5292 gen_op_mov_v_reg(MO_32
, cpu_T
[0], R_EBP
);
5293 gen_op_mov_reg_v(MO_32
, R_ESP
, cpu_T
[0]);
5295 gen_op_mov_v_reg(MO_16
, cpu_T
[0], R_EBP
);
5296 gen_op_mov_reg_v(MO_16
, R_ESP
, cpu_T
[0]);
5299 gen_op_mov_reg_v(ot
, R_EBP
, cpu_T
[0]);
5300 gen_pop_update(s
, ot
);
5302 case 0x06: /* push es */
5303 case 0x0e: /* push cs */
5304 case 0x16: /* push ss */
5305 case 0x1e: /* push ds */
5308 gen_op_movl_T0_seg(b
>> 3);
5309 gen_push_v(s
, cpu_T
[0]);
5311 case 0x1a0: /* push fs */
5312 case 0x1a8: /* push gs */
5313 gen_op_movl_T0_seg((b
>> 3) & 7);
5314 gen_push_v(s
, cpu_T
[0]);
5316 case 0x07: /* pop es */
5317 case 0x17: /* pop ss */
5318 case 0x1f: /* pop ds */
5323 gen_movl_seg_T0(s
, reg
, pc_start
- s
->cs_base
);
5324 gen_pop_update(s
, ot
);
5326 /* if reg == SS, inhibit interrupts/trace. */
5327 /* If several instructions disable interrupts, only the
5329 if (!(s
->tb
->flags
& HF_INHIBIT_IRQ_MASK
))
5330 gen_helper_set_inhibit_irq(cpu_env
);
5334 gen_jmp_im(s
->pc
- s
->cs_base
);
5338 case 0x1a1: /* pop fs */
5339 case 0x1a9: /* pop gs */
5341 gen_movl_seg_T0(s
, (b
>> 3) & 7, pc_start
- s
->cs_base
);
5342 gen_pop_update(s
, ot
);
5344 gen_jmp_im(s
->pc
- s
->cs_base
);
5349 /**************************/
5352 case 0x89: /* mov Gv, Ev */
5353 ot
= mo_b_d(b
, dflag
);
5354 modrm
= cpu_ldub_code(env
, s
->pc
++);
5355 reg
= ((modrm
>> 3) & 7) | rex_r
;
5357 /* generate a generic store */
5358 gen_ldst_modrm(env
, s
, modrm
, ot
, reg
, 1);
5361 case 0xc7: /* mov Ev, Iv */
5362 ot
= mo_b_d(b
, dflag
);
5363 modrm
= cpu_ldub_code(env
, s
->pc
++);
5364 mod
= (modrm
>> 6) & 3;
5366 s
->rip_offset
= insn_const_size(ot
);
5367 gen_lea_modrm(env
, s
, modrm
);
5369 val
= insn_get(env
, s
, ot
);
5370 tcg_gen_movi_tl(cpu_T
[0], val
);
5372 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
5374 gen_op_mov_reg_v(ot
, (modrm
& 7) | REX_B(s
), cpu_T
[0]);
5378 case 0x8b: /* mov Ev, Gv */
5379 ot
= mo_b_d(b
, dflag
);
5380 modrm
= cpu_ldub_code(env
, s
->pc
++);
5381 reg
= ((modrm
>> 3) & 7) | rex_r
;
5383 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5384 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
5386 case 0x8e: /* mov seg, Gv */
5387 modrm
= cpu_ldub_code(env
, s
->pc
++);
5388 reg
= (modrm
>> 3) & 7;
5389 if (reg
>= 6 || reg
== R_CS
)
5391 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
5392 gen_movl_seg_T0(s
, reg
, pc_start
- s
->cs_base
);
5394 /* if reg == SS, inhibit interrupts/trace */
5395 /* If several instructions disable interrupts, only the
5397 if (!(s
->tb
->flags
& HF_INHIBIT_IRQ_MASK
))
5398 gen_helper_set_inhibit_irq(cpu_env
);
5402 gen_jmp_im(s
->pc
- s
->cs_base
);
5406 case 0x8c: /* mov Gv, seg */
5407 modrm
= cpu_ldub_code(env
, s
->pc
++);
5408 reg
= (modrm
>> 3) & 7;
5409 mod
= (modrm
>> 6) & 3;
5412 gen_op_movl_T0_seg(reg
);
5413 ot
= mod
== 3 ? dflag
: MO_16
;
5414 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
5417 case 0x1b6: /* movzbS Gv, Eb */
5418 case 0x1b7: /* movzwS Gv, Eb */
5419 case 0x1be: /* movsbS Gv, Eb */
5420 case 0x1bf: /* movswS Gv, Eb */
5425 /* d_ot is the size of destination */
5427 /* ot is the size of source */
5428 ot
= (b
& 1) + MO_8
;
5429 /* s_ot is the sign+size of source */
5430 s_ot
= b
& 8 ? MO_SIGN
| ot
: ot
;
5432 modrm
= cpu_ldub_code(env
, s
->pc
++);
5433 reg
= ((modrm
>> 3) & 7) | rex_r
;
5434 mod
= (modrm
>> 6) & 3;
5435 rm
= (modrm
& 7) | REX_B(s
);
5438 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
5441 tcg_gen_ext8u_tl(cpu_T
[0], cpu_T
[0]);
5444 tcg_gen_ext8s_tl(cpu_T
[0], cpu_T
[0]);
5447 tcg_gen_ext16u_tl(cpu_T
[0], cpu_T
[0]);
5451 tcg_gen_ext16s_tl(cpu_T
[0], cpu_T
[0]);
5454 gen_op_mov_reg_v(d_ot
, reg
, cpu_T
[0]);
5456 gen_lea_modrm(env
, s
, modrm
);
5457 gen_op_ld_v(s
, s_ot
, cpu_T
[0], cpu_A0
);
5458 gen_op_mov_reg_v(d_ot
, reg
, cpu_T
[0]);
5463 case 0x8d: /* lea */
5465 modrm
= cpu_ldub_code(env
, s
->pc
++);
5466 mod
= (modrm
>> 6) & 3;
5469 reg
= ((modrm
>> 3) & 7) | rex_r
;
5470 /* we must ensure that no segment is added */
5474 gen_lea_modrm(env
, s
, modrm
);
5476 gen_op_mov_reg_v(ot
, reg
, cpu_A0
);
5479 case 0xa0: /* mov EAX, Ov */
5481 case 0xa2: /* mov Ov, EAX */
5484 target_ulong offset_addr
;
5486 ot
= mo_b_d(b
, dflag
);
5488 #ifdef TARGET_X86_64
5490 offset_addr
= cpu_ldq_code(env
, s
->pc
);
5495 offset_addr
= insn_get(env
, s
, s
->aflag
);
5498 tcg_gen_movi_tl(cpu_A0
, offset_addr
);
5499 gen_add_A0_ds_seg(s
);
5501 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
5502 gen_op_mov_reg_v(ot
, R_EAX
, cpu_T
[0]);
5504 gen_op_mov_v_reg(ot
, cpu_T
[0], R_EAX
);
5505 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
5509 case 0xd7: /* xlat */
5510 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[R_EBX
]);
5511 tcg_gen_ext8u_tl(cpu_T
[0], cpu_regs
[R_EAX
]);
5512 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_T
[0]);
5513 gen_extu(s
->aflag
, cpu_A0
);
5514 gen_add_A0_ds_seg(s
);
5515 gen_op_ld_v(s
, MO_8
, cpu_T
[0], cpu_A0
);
5516 gen_op_mov_reg_v(MO_8
, R_EAX
, cpu_T
[0]);
5518 case 0xb0 ... 0xb7: /* mov R, Ib */
5519 val
= insn_get(env
, s
, MO_8
);
5520 tcg_gen_movi_tl(cpu_T
[0], val
);
5521 gen_op_mov_reg_v(MO_8
, (b
& 7) | REX_B(s
), cpu_T
[0]);
5523 case 0xb8 ... 0xbf: /* mov R, Iv */
5524 #ifdef TARGET_X86_64
5525 if (dflag
== MO_64
) {
5528 tmp
= cpu_ldq_code(env
, s
->pc
);
5530 reg
= (b
& 7) | REX_B(s
);
5531 tcg_gen_movi_tl(cpu_T
[0], tmp
);
5532 gen_op_mov_reg_v(MO_64
, reg
, cpu_T
[0]);
5537 val
= insn_get(env
, s
, ot
);
5538 reg
= (b
& 7) | REX_B(s
);
5539 tcg_gen_movi_tl(cpu_T
[0], val
);
5540 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
5544 case 0x91 ... 0x97: /* xchg R, EAX */
5547 reg
= (b
& 7) | REX_B(s
);
5551 case 0x87: /* xchg Ev, Gv */
5552 ot
= mo_b_d(b
, dflag
);
5553 modrm
= cpu_ldub_code(env
, s
->pc
++);
5554 reg
= ((modrm
>> 3) & 7) | rex_r
;
5555 mod
= (modrm
>> 6) & 3;
5557 rm
= (modrm
& 7) | REX_B(s
);
5559 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
5560 gen_op_mov_v_reg(ot
, cpu_T
[1], rm
);
5561 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
5562 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5564 gen_lea_modrm(env
, s
, modrm
);
5565 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
5566 /* for xchg, lock is implicit */
5567 if (!(prefixes
& PREFIX_LOCK
))
5569 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
5570 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
5571 if (!(prefixes
& PREFIX_LOCK
))
5572 gen_helper_unlock();
5573 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5576 case 0xc4: /* les Gv */
5577 /* In CODE64 this is VEX3; see above. */
5580 case 0xc5: /* lds Gv */
5581 /* In CODE64 this is VEX2; see above. */
5584 case 0x1b2: /* lss Gv */
5587 case 0x1b4: /* lfs Gv */
5590 case 0x1b5: /* lgs Gv */
5593 ot
= dflag
!= MO_16
? MO_32
: MO_16
;
5594 modrm
= cpu_ldub_code(env
, s
->pc
++);
5595 reg
= ((modrm
>> 3) & 7) | rex_r
;
5596 mod
= (modrm
>> 6) & 3;
5599 gen_lea_modrm(env
, s
, modrm
);
5600 gen_op_ld_v(s
, ot
, cpu_T
[1], cpu_A0
);
5601 gen_add_A0_im(s
, 1 << ot
);
5602 /* load the segment first to handle exceptions properly */
5603 gen_op_ld_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
5604 gen_movl_seg_T0(s
, op
, pc_start
- s
->cs_base
);
5605 /* then put the data */
5606 gen_op_mov_reg_v(ot
, reg
, cpu_T
[1]);
5608 gen_jmp_im(s
->pc
- s
->cs_base
);
5613 /************************/
5621 ot
= mo_b_d(b
, dflag
);
5622 modrm
= cpu_ldub_code(env
, s
->pc
++);
5623 mod
= (modrm
>> 6) & 3;
5624 op
= (modrm
>> 3) & 7;
5630 gen_lea_modrm(env
, s
, modrm
);
5633 opreg
= (modrm
& 7) | REX_B(s
);
5638 gen_shift(s
, op
, ot
, opreg
, OR_ECX
);
5641 shift
= cpu_ldub_code(env
, s
->pc
++);
5643 gen_shifti(s
, op
, ot
, opreg
, shift
);
5658 case 0x1a4: /* shld imm */
5662 case 0x1a5: /* shld cl */
5666 case 0x1ac: /* shrd imm */
5670 case 0x1ad: /* shrd cl */
5675 modrm
= cpu_ldub_code(env
, s
->pc
++);
5676 mod
= (modrm
>> 6) & 3;
5677 rm
= (modrm
& 7) | REX_B(s
);
5678 reg
= ((modrm
>> 3) & 7) | rex_r
;
5680 gen_lea_modrm(env
, s
, modrm
);
5685 gen_op_mov_v_reg(ot
, cpu_T
[1], reg
);
5688 TCGv imm
= tcg_const_tl(cpu_ldub_code(env
, s
->pc
++));
5689 gen_shiftd_rm_T1(s
, ot
, opreg
, op
, imm
);
5692 gen_shiftd_rm_T1(s
, ot
, opreg
, op
, cpu_regs
[R_ECX
]);
5696 /************************/
5699 if (s
->flags
& (HF_EM_MASK
| HF_TS_MASK
)) {
5700 /* if CR0.EM or CR0.TS are set, generate an FPU exception */
5701 /* XXX: what to do if illegal op ? */
5702 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
5705 modrm
= cpu_ldub_code(env
, s
->pc
++);
5706 mod
= (modrm
>> 6) & 3;
5708 op
= ((b
& 7) << 3) | ((modrm
>> 3) & 7);
5711 gen_lea_modrm(env
, s
, modrm
);
5713 case 0x00 ... 0x07: /* fxxxs */
5714 case 0x10 ... 0x17: /* fixxxl */
5715 case 0x20 ... 0x27: /* fxxxl */
5716 case 0x30 ... 0x37: /* fixxx */
5723 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5724 s
->mem_index
, MO_LEUL
);
5725 gen_helper_flds_FT0(cpu_env
, cpu_tmp2_i32
);
5728 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5729 s
->mem_index
, MO_LEUL
);
5730 gen_helper_fildl_FT0(cpu_env
, cpu_tmp2_i32
);
5733 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
,
5734 s
->mem_index
, MO_LEQ
);
5735 gen_helper_fldl_FT0(cpu_env
, cpu_tmp1_i64
);
5739 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5740 s
->mem_index
, MO_LESW
);
5741 gen_helper_fildl_FT0(cpu_env
, cpu_tmp2_i32
);
5745 gen_helper_fp_arith_ST0_FT0(op1
);
5747 /* fcomp needs pop */
5748 gen_helper_fpop(cpu_env
);
5752 case 0x08: /* flds */
5753 case 0x0a: /* fsts */
5754 case 0x0b: /* fstps */
5755 case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
5756 case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
5757 case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
5762 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5763 s
->mem_index
, MO_LEUL
);
5764 gen_helper_flds_ST0(cpu_env
, cpu_tmp2_i32
);
5767 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5768 s
->mem_index
, MO_LEUL
);
5769 gen_helper_fildl_ST0(cpu_env
, cpu_tmp2_i32
);
5772 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
,
5773 s
->mem_index
, MO_LEQ
);
5774 gen_helper_fldl_ST0(cpu_env
, cpu_tmp1_i64
);
5778 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5779 s
->mem_index
, MO_LESW
);
5780 gen_helper_fildl_ST0(cpu_env
, cpu_tmp2_i32
);
5785 /* XXX: the corresponding CPUID bit must be tested ! */
5788 gen_helper_fisttl_ST0(cpu_tmp2_i32
, cpu_env
);
5789 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5790 s
->mem_index
, MO_LEUL
);
5793 gen_helper_fisttll_ST0(cpu_tmp1_i64
, cpu_env
);
5794 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
,
5795 s
->mem_index
, MO_LEQ
);
5799 gen_helper_fistt_ST0(cpu_tmp2_i32
, cpu_env
);
5800 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5801 s
->mem_index
, MO_LEUW
);
5804 gen_helper_fpop(cpu_env
);
5809 gen_helper_fsts_ST0(cpu_tmp2_i32
, cpu_env
);
5810 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5811 s
->mem_index
, MO_LEUL
);
5814 gen_helper_fistl_ST0(cpu_tmp2_i32
, cpu_env
);
5815 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5816 s
->mem_index
, MO_LEUL
);
5819 gen_helper_fstl_ST0(cpu_tmp1_i64
, cpu_env
);
5820 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
,
5821 s
->mem_index
, MO_LEQ
);
5825 gen_helper_fist_ST0(cpu_tmp2_i32
, cpu_env
);
5826 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5827 s
->mem_index
, MO_LEUW
);
5831 gen_helper_fpop(cpu_env
);
5835 case 0x0c: /* fldenv mem */
5836 gen_update_cc_op(s
);
5837 gen_jmp_im(pc_start
- s
->cs_base
);
5838 gen_helper_fldenv(cpu_env
, cpu_A0
, tcg_const_i32(dflag
- 1));
5840 case 0x0d: /* fldcw mem */
5841 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
5842 s
->mem_index
, MO_LEUW
);
5843 gen_helper_fldcw(cpu_env
, cpu_tmp2_i32
);
5845 case 0x0e: /* fnstenv mem */
5846 gen_update_cc_op(s
);
5847 gen_jmp_im(pc_start
- s
->cs_base
);
5848 gen_helper_fstenv(cpu_env
, cpu_A0
, tcg_const_i32(dflag
- 1));
5850 case 0x0f: /* fnstcw mem */
5851 gen_helper_fnstcw(cpu_tmp2_i32
, cpu_env
);
5852 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5853 s
->mem_index
, MO_LEUW
);
5855 case 0x1d: /* fldt mem */
5856 gen_update_cc_op(s
);
5857 gen_jmp_im(pc_start
- s
->cs_base
);
5858 gen_helper_fldt_ST0(cpu_env
, cpu_A0
);
5860 case 0x1f: /* fstpt mem */
5861 gen_update_cc_op(s
);
5862 gen_jmp_im(pc_start
- s
->cs_base
);
5863 gen_helper_fstt_ST0(cpu_env
, cpu_A0
);
5864 gen_helper_fpop(cpu_env
);
5866 case 0x2c: /* frstor mem */
5867 gen_update_cc_op(s
);
5868 gen_jmp_im(pc_start
- s
->cs_base
);
5869 gen_helper_frstor(cpu_env
, cpu_A0
, tcg_const_i32(dflag
- 1));
5871 case 0x2e: /* fnsave mem */
5872 gen_update_cc_op(s
);
5873 gen_jmp_im(pc_start
- s
->cs_base
);
5874 gen_helper_fsave(cpu_env
, cpu_A0
, tcg_const_i32(dflag
- 1));
5876 case 0x2f: /* fnstsw mem */
5877 gen_helper_fnstsw(cpu_tmp2_i32
, cpu_env
);
5878 tcg_gen_qemu_st_i32(cpu_tmp2_i32
, cpu_A0
,
5879 s
->mem_index
, MO_LEUW
);
5881 case 0x3c: /* fbld */
5882 gen_update_cc_op(s
);
5883 gen_jmp_im(pc_start
- s
->cs_base
);
5884 gen_helper_fbld_ST0(cpu_env
, cpu_A0
);
5886 case 0x3e: /* fbstp */
5887 gen_update_cc_op(s
);
5888 gen_jmp_im(pc_start
- s
->cs_base
);
5889 gen_helper_fbst_ST0(cpu_env
, cpu_A0
);
5890 gen_helper_fpop(cpu_env
);
5892 case 0x3d: /* fildll */
5893 tcg_gen_qemu_ld_i64(cpu_tmp1_i64
, cpu_A0
, s
->mem_index
, MO_LEQ
);
5894 gen_helper_fildll_ST0(cpu_env
, cpu_tmp1_i64
);
5896 case 0x3f: /* fistpll */
5897 gen_helper_fistll_ST0(cpu_tmp1_i64
, cpu_env
);
5898 tcg_gen_qemu_st_i64(cpu_tmp1_i64
, cpu_A0
, s
->mem_index
, MO_LEQ
);
5899 gen_helper_fpop(cpu_env
);
5905 /* register float ops */
5909 case 0x08: /* fld sti */
5910 gen_helper_fpush(cpu_env
);
5911 gen_helper_fmov_ST0_STN(cpu_env
,
5912 tcg_const_i32((opreg
+ 1) & 7));
5914 case 0x09: /* fxchg sti */
5915 case 0x29: /* fxchg4 sti, undocumented op */
5916 case 0x39: /* fxchg7 sti, undocumented op */
5917 gen_helper_fxchg_ST0_STN(cpu_env
, tcg_const_i32(opreg
));
5919 case 0x0a: /* grp d9/2 */
5922 /* check exceptions (FreeBSD FPU probe) */
5923 gen_update_cc_op(s
);
5924 gen_jmp_im(pc_start
- s
->cs_base
);
5925 gen_helper_fwait(cpu_env
);
5931 case 0x0c: /* grp d9/4 */
5934 gen_helper_fchs_ST0(cpu_env
);
5937 gen_helper_fabs_ST0(cpu_env
);
5940 gen_helper_fldz_FT0(cpu_env
);
5941 gen_helper_fcom_ST0_FT0(cpu_env
);
5944 gen_helper_fxam_ST0(cpu_env
);
5950 case 0x0d: /* grp d9/5 */
5954 gen_helper_fpush(cpu_env
);
5955 gen_helper_fld1_ST0(cpu_env
);
5958 gen_helper_fpush(cpu_env
);
5959 gen_helper_fldl2t_ST0(cpu_env
);
5962 gen_helper_fpush(cpu_env
);
5963 gen_helper_fldl2e_ST0(cpu_env
);
5966 gen_helper_fpush(cpu_env
);
5967 gen_helper_fldpi_ST0(cpu_env
);
5970 gen_helper_fpush(cpu_env
);
5971 gen_helper_fldlg2_ST0(cpu_env
);
5974 gen_helper_fpush(cpu_env
);
5975 gen_helper_fldln2_ST0(cpu_env
);
5978 gen_helper_fpush(cpu_env
);
5979 gen_helper_fldz_ST0(cpu_env
);
5986 case 0x0e: /* grp d9/6 */
5989 gen_helper_f2xm1(cpu_env
);
5992 gen_helper_fyl2x(cpu_env
);
5995 gen_helper_fptan(cpu_env
);
5997 case 3: /* fpatan */
5998 gen_helper_fpatan(cpu_env
);
6000 case 4: /* fxtract */
6001 gen_helper_fxtract(cpu_env
);
6003 case 5: /* fprem1 */
6004 gen_helper_fprem1(cpu_env
);
6006 case 6: /* fdecstp */
6007 gen_helper_fdecstp(cpu_env
);
6010 case 7: /* fincstp */
6011 gen_helper_fincstp(cpu_env
);
6015 case 0x0f: /* grp d9/7 */
6018 gen_helper_fprem(cpu_env
);
6020 case 1: /* fyl2xp1 */
6021 gen_helper_fyl2xp1(cpu_env
);
6024 gen_helper_fsqrt(cpu_env
);
6026 case 3: /* fsincos */
6027 gen_helper_fsincos(cpu_env
);
6029 case 5: /* fscale */
6030 gen_helper_fscale(cpu_env
);
6032 case 4: /* frndint */
6033 gen_helper_frndint(cpu_env
);
6036 gen_helper_fsin(cpu_env
);
6040 gen_helper_fcos(cpu_env
);
6044 case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
6045 case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
6046 case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
6052 gen_helper_fp_arith_STN_ST0(op1
, opreg
);
6054 gen_helper_fpop(cpu_env
);
6056 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6057 gen_helper_fp_arith_ST0_FT0(op1
);
6061 case 0x02: /* fcom */
6062 case 0x22: /* fcom2, undocumented op */
6063 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6064 gen_helper_fcom_ST0_FT0(cpu_env
);
6066 case 0x03: /* fcomp */
6067 case 0x23: /* fcomp3, undocumented op */
6068 case 0x32: /* fcomp5, undocumented op */
6069 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6070 gen_helper_fcom_ST0_FT0(cpu_env
);
6071 gen_helper_fpop(cpu_env
);
6073 case 0x15: /* da/5 */
6075 case 1: /* fucompp */
6076 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(1));
6077 gen_helper_fucom_ST0_FT0(cpu_env
);
6078 gen_helper_fpop(cpu_env
);
6079 gen_helper_fpop(cpu_env
);
6087 case 0: /* feni (287 only, just do nop here) */
6089 case 1: /* fdisi (287 only, just do nop here) */
6092 gen_helper_fclex(cpu_env
);
6094 case 3: /* fninit */
6095 gen_helper_fninit(cpu_env
);
6097 case 4: /* fsetpm (287 only, just do nop here) */
6103 case 0x1d: /* fucomi */
6104 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6107 gen_update_cc_op(s
);
6108 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6109 gen_helper_fucomi_ST0_FT0(cpu_env
);
6110 set_cc_op(s
, CC_OP_EFLAGS
);
6112 case 0x1e: /* fcomi */
6113 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6116 gen_update_cc_op(s
);
6117 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6118 gen_helper_fcomi_ST0_FT0(cpu_env
);
6119 set_cc_op(s
, CC_OP_EFLAGS
);
6121 case 0x28: /* ffree sti */
6122 gen_helper_ffree_STN(cpu_env
, tcg_const_i32(opreg
));
6124 case 0x2a: /* fst sti */
6125 gen_helper_fmov_STN_ST0(cpu_env
, tcg_const_i32(opreg
));
6127 case 0x2b: /* fstp sti */
6128 case 0x0b: /* fstp1 sti, undocumented op */
6129 case 0x3a: /* fstp8 sti, undocumented op */
6130 case 0x3b: /* fstp9 sti, undocumented op */
6131 gen_helper_fmov_STN_ST0(cpu_env
, tcg_const_i32(opreg
));
6132 gen_helper_fpop(cpu_env
);
6134 case 0x2c: /* fucom st(i) */
6135 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6136 gen_helper_fucom_ST0_FT0(cpu_env
);
6138 case 0x2d: /* fucomp st(i) */
6139 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6140 gen_helper_fucom_ST0_FT0(cpu_env
);
6141 gen_helper_fpop(cpu_env
);
6143 case 0x33: /* de/3 */
6145 case 1: /* fcompp */
6146 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(1));
6147 gen_helper_fcom_ST0_FT0(cpu_env
);
6148 gen_helper_fpop(cpu_env
);
6149 gen_helper_fpop(cpu_env
);
6155 case 0x38: /* ffreep sti, undocumented op */
6156 gen_helper_ffree_STN(cpu_env
, tcg_const_i32(opreg
));
6157 gen_helper_fpop(cpu_env
);
6159 case 0x3c: /* df/4 */
6162 gen_helper_fnstsw(cpu_tmp2_i32
, cpu_env
);
6163 tcg_gen_extu_i32_tl(cpu_T
[0], cpu_tmp2_i32
);
6164 gen_op_mov_reg_v(MO_16
, R_EAX
, cpu_T
[0]);
6170 case 0x3d: /* fucomip */
6171 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6174 gen_update_cc_op(s
);
6175 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6176 gen_helper_fucomi_ST0_FT0(cpu_env
);
6177 gen_helper_fpop(cpu_env
);
6178 set_cc_op(s
, CC_OP_EFLAGS
);
6180 case 0x3e: /* fcomip */
6181 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6184 gen_update_cc_op(s
);
6185 gen_helper_fmov_FT0_STN(cpu_env
, tcg_const_i32(opreg
));
6186 gen_helper_fcomi_ST0_FT0(cpu_env
);
6187 gen_helper_fpop(cpu_env
);
6188 set_cc_op(s
, CC_OP_EFLAGS
);
6190 case 0x10 ... 0x13: /* fcmovxx */
6194 static const uint8_t fcmov_cc
[8] = {
6201 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6204 op1
= fcmov_cc
[op
& 3] | (((op
>> 3) & 1) ^ 1);
6205 l1
= gen_new_label();
6206 gen_jcc1_noeob(s
, op1
, l1
);
6207 gen_helper_fmov_ST0_STN(cpu_env
, tcg_const_i32(opreg
));
6216 /************************/
6219 case 0xa4: /* movsS */
6221 ot
= mo_b_d(b
, dflag
);
6222 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6223 gen_repz_movs(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6229 case 0xaa: /* stosS */
6231 ot
= mo_b_d(b
, dflag
);
6232 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6233 gen_repz_stos(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6238 case 0xac: /* lodsS */
6240 ot
= mo_b_d(b
, dflag
);
6241 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6242 gen_repz_lods(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6247 case 0xae: /* scasS */
6249 ot
= mo_b_d(b
, dflag
);
6250 if (prefixes
& PREFIX_REPNZ
) {
6251 gen_repz_scas(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 1);
6252 } else if (prefixes
& PREFIX_REPZ
) {
6253 gen_repz_scas(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 0);
6259 case 0xa6: /* cmpsS */
6261 ot
= mo_b_d(b
, dflag
);
6262 if (prefixes
& PREFIX_REPNZ
) {
6263 gen_repz_cmps(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 1);
6264 } else if (prefixes
& PREFIX_REPZ
) {
6265 gen_repz_cmps(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
, 0);
6270 case 0x6c: /* insS */
6272 ot
= mo_b_d32(b
, dflag
);
6273 tcg_gen_ext16u_tl(cpu_T
[0], cpu_regs
[R_EDX
]);
6274 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6275 SVM_IOIO_TYPE_MASK
| svm_is_rep(prefixes
) | 4);
6276 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6277 gen_repz_ins(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6281 gen_jmp(s
, s
->pc
- s
->cs_base
);
6285 case 0x6e: /* outsS */
6287 ot
= mo_b_d32(b
, dflag
);
6288 tcg_gen_ext16u_tl(cpu_T
[0], cpu_regs
[R_EDX
]);
6289 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6290 svm_is_rep(prefixes
) | 4);
6291 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
6292 gen_repz_outs(s
, ot
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6296 gen_jmp(s
, s
->pc
- s
->cs_base
);
6301 /************************/
6306 ot
= mo_b_d32(b
, dflag
);
6307 val
= cpu_ldub_code(env
, s
->pc
++);
6308 tcg_gen_movi_tl(cpu_T
[0], val
);
6309 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6310 SVM_IOIO_TYPE_MASK
| svm_is_rep(prefixes
));
6313 tcg_gen_movi_i32(cpu_tmp2_i32
, val
);
6314 gen_helper_in_func(ot
, cpu_T
[1], cpu_tmp2_i32
);
6315 gen_op_mov_reg_v(ot
, R_EAX
, cpu_T
[1]);
6318 gen_jmp(s
, s
->pc
- s
->cs_base
);
6323 ot
= mo_b_d32(b
, dflag
);
6324 val
= cpu_ldub_code(env
, s
->pc
++);
6325 tcg_gen_movi_tl(cpu_T
[0], val
);
6326 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6327 svm_is_rep(prefixes
));
6328 gen_op_mov_v_reg(ot
, cpu_T
[1], R_EAX
);
6332 tcg_gen_movi_i32(cpu_tmp2_i32
, val
);
6333 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[1]);
6334 gen_helper_out_func(ot
, cpu_tmp2_i32
, cpu_tmp3_i32
);
6337 gen_jmp(s
, s
->pc
- s
->cs_base
);
6342 ot
= mo_b_d32(b
, dflag
);
6343 tcg_gen_ext16u_tl(cpu_T
[0], cpu_regs
[R_EDX
]);
6344 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6345 SVM_IOIO_TYPE_MASK
| svm_is_rep(prefixes
));
6348 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
6349 gen_helper_in_func(ot
, cpu_T
[1], cpu_tmp2_i32
);
6350 gen_op_mov_reg_v(ot
, R_EAX
, cpu_T
[1]);
6353 gen_jmp(s
, s
->pc
- s
->cs_base
);
6358 ot
= mo_b_d32(b
, dflag
);
6359 tcg_gen_ext16u_tl(cpu_T
[0], cpu_regs
[R_EDX
]);
6360 gen_check_io(s
, ot
, pc_start
- s
->cs_base
,
6361 svm_is_rep(prefixes
));
6362 gen_op_mov_v_reg(ot
, cpu_T
[1], R_EAX
);
6366 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
6367 tcg_gen_trunc_tl_i32(cpu_tmp3_i32
, cpu_T
[1]);
6368 gen_helper_out_func(ot
, cpu_tmp2_i32
, cpu_tmp3_i32
);
6371 gen_jmp(s
, s
->pc
- s
->cs_base
);
6375 /************************/
6377 case 0xc2: /* ret im */
6378 val
= cpu_ldsw_code(env
, s
->pc
);
6381 gen_stack_update(s
, val
+ (1 << ot
));
6382 /* Note that gen_pop_T0 uses a zero-extending load. */
6383 gen_op_jmp_v(cpu_T
[0]);
6386 case 0xc3: /* ret */
6388 gen_pop_update(s
, ot
);
6389 /* Note that gen_pop_T0 uses a zero-extending load. */
6390 gen_op_jmp_v(cpu_T
[0]);
6393 case 0xca: /* lret im */
6394 val
= cpu_ldsw_code(env
, s
->pc
);
6397 if (s
->pe
&& !s
->vm86
) {
6398 gen_update_cc_op(s
);
6399 gen_jmp_im(pc_start
- s
->cs_base
);
6400 gen_helper_lret_protected(cpu_env
, tcg_const_i32(dflag
- 1),
6401 tcg_const_i32(val
));
6405 gen_op_ld_v(s
, dflag
, cpu_T
[0], cpu_A0
);
6406 /* NOTE: keeping EIP updated is not a problem in case of
6408 gen_op_jmp_v(cpu_T
[0]);
6410 gen_op_addl_A0_im(1 << dflag
);
6411 gen_op_ld_v(s
, dflag
, cpu_T
[0], cpu_A0
);
6412 gen_op_movl_seg_T0_vm(R_CS
);
6413 /* add stack offset */
6414 gen_stack_update(s
, val
+ (2 << dflag
));
6418 case 0xcb: /* lret */
6421 case 0xcf: /* iret */
6422 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_IRET
);
6425 gen_helper_iret_real(cpu_env
, tcg_const_i32(dflag
- 1));
6426 set_cc_op(s
, CC_OP_EFLAGS
);
6427 } else if (s
->vm86
) {
6429 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6431 gen_helper_iret_real(cpu_env
, tcg_const_i32(dflag
- 1));
6432 set_cc_op(s
, CC_OP_EFLAGS
);
6435 gen_update_cc_op(s
);
6436 gen_jmp_im(pc_start
- s
->cs_base
);
6437 gen_helper_iret_protected(cpu_env
, tcg_const_i32(dflag
- 1),
6438 tcg_const_i32(s
->pc
- s
->cs_base
));
6439 set_cc_op(s
, CC_OP_EFLAGS
);
6443 case 0xe8: /* call im */
6445 if (dflag
!= MO_16
) {
6446 tval
= (int32_t)insn_get(env
, s
, MO_32
);
6448 tval
= (int16_t)insn_get(env
, s
, MO_16
);
6450 next_eip
= s
->pc
- s
->cs_base
;
6452 if (dflag
== MO_16
) {
6454 } else if (!CODE64(s
)) {
6457 tcg_gen_movi_tl(cpu_T
[0], next_eip
);
6458 gen_push_v(s
, cpu_T
[0]);
6462 case 0x9a: /* lcall im */
6464 unsigned int selector
, offset
;
6469 offset
= insn_get(env
, s
, ot
);
6470 selector
= insn_get(env
, s
, MO_16
);
6472 tcg_gen_movi_tl(cpu_T
[0], selector
);
6473 tcg_gen_movi_tl(cpu_T
[1], offset
);
6476 case 0xe9: /* jmp im */
6477 if (dflag
!= MO_16
) {
6478 tval
= (int32_t)insn_get(env
, s
, MO_32
);
6480 tval
= (int16_t)insn_get(env
, s
, MO_16
);
6482 tval
+= s
->pc
- s
->cs_base
;
6483 if (dflag
== MO_16
) {
6485 } else if (!CODE64(s
)) {
6490 case 0xea: /* ljmp im */
6492 unsigned int selector
, offset
;
6497 offset
= insn_get(env
, s
, ot
);
6498 selector
= insn_get(env
, s
, MO_16
);
6500 tcg_gen_movi_tl(cpu_T
[0], selector
);
6501 tcg_gen_movi_tl(cpu_T
[1], offset
);
6504 case 0xeb: /* jmp Jb */
6505 tval
= (int8_t)insn_get(env
, s
, MO_8
);
6506 tval
+= s
->pc
- s
->cs_base
;
6507 if (dflag
== MO_16
) {
6512 case 0x70 ... 0x7f: /* jcc Jb */
6513 tval
= (int8_t)insn_get(env
, s
, MO_8
);
6515 case 0x180 ... 0x18f: /* jcc Jv */
6516 if (dflag
!= MO_16
) {
6517 tval
= (int32_t)insn_get(env
, s
, MO_32
);
6519 tval
= (int16_t)insn_get(env
, s
, MO_16
);
6522 next_eip
= s
->pc
- s
->cs_base
;
6524 if (dflag
== MO_16
) {
6527 gen_jcc(s
, b
, tval
, next_eip
);
6530 case 0x190 ... 0x19f: /* setcc Gv */
6531 modrm
= cpu_ldub_code(env
, s
->pc
++);
6532 gen_setcc1(s
, b
, cpu_T
[0]);
6533 gen_ldst_modrm(env
, s
, modrm
, MO_8
, OR_TMP0
, 1);
6535 case 0x140 ... 0x14f: /* cmov Gv, Ev */
6536 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
6540 modrm
= cpu_ldub_code(env
, s
->pc
++);
6541 reg
= ((modrm
>> 3) & 7) | rex_r
;
6542 gen_cmovcc1(env
, s
, ot
, b
, modrm
, reg
);
6545 /************************/
6547 case 0x9c: /* pushf */
6548 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_PUSHF
);
6549 if (s
->vm86
&& s
->iopl
!= 3) {
6550 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6552 gen_update_cc_op(s
);
6553 gen_helper_read_eflags(cpu_T
[0], cpu_env
);
6554 gen_push_v(s
, cpu_T
[0]);
6557 case 0x9d: /* popf */
6558 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_POPF
);
6559 if (s
->vm86
&& s
->iopl
!= 3) {
6560 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6564 if (dflag
!= MO_16
) {
6565 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6566 tcg_const_i32((TF_MASK
| AC_MASK
|
6571 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6572 tcg_const_i32((TF_MASK
| AC_MASK
|
6574 IF_MASK
| IOPL_MASK
)
6578 if (s
->cpl
<= s
->iopl
) {
6579 if (dflag
!= MO_16
) {
6580 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6581 tcg_const_i32((TF_MASK
|
6587 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6588 tcg_const_i32((TF_MASK
|
6596 if (dflag
!= MO_16
) {
6597 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6598 tcg_const_i32((TF_MASK
| AC_MASK
|
6599 ID_MASK
| NT_MASK
)));
6601 gen_helper_write_eflags(cpu_env
, cpu_T
[0],
6602 tcg_const_i32((TF_MASK
| AC_MASK
|
6608 gen_pop_update(s
, ot
);
6609 set_cc_op(s
, CC_OP_EFLAGS
);
6610 /* abort translation because TF/AC flag may change */
6611 gen_jmp_im(s
->pc
- s
->cs_base
);
6615 case 0x9e: /* sahf */
6616 if (CODE64(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_LAHF_LM
))
6618 gen_op_mov_v_reg(MO_8
, cpu_T
[0], R_AH
);
6619 gen_compute_eflags(s
);
6620 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, CC_O
);
6621 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
6622 tcg_gen_or_tl(cpu_cc_src
, cpu_cc_src
, cpu_T
[0]);
6624 case 0x9f: /* lahf */
6625 if (CODE64(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_LAHF_LM
))
6627 gen_compute_eflags(s
);
6628 /* Note: gen_compute_eflags() only gives the condition codes */
6629 tcg_gen_ori_tl(cpu_T
[0], cpu_cc_src
, 0x02);
6630 gen_op_mov_reg_v(MO_8
, R_AH
, cpu_T
[0]);
6632 case 0xf5: /* cmc */
6633 gen_compute_eflags(s
);
6634 tcg_gen_xori_tl(cpu_cc_src
, cpu_cc_src
, CC_C
);
6636 case 0xf8: /* clc */
6637 gen_compute_eflags(s
);
6638 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, ~CC_C
);
6640 case 0xf9: /* stc */
6641 gen_compute_eflags(s
);
6642 tcg_gen_ori_tl(cpu_cc_src
, cpu_cc_src
, CC_C
);
6644 case 0xfc: /* cld */
6645 tcg_gen_movi_i32(cpu_tmp2_i32
, 1);
6646 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
, offsetof(CPUX86State
, df
));
6648 case 0xfd: /* std */
6649 tcg_gen_movi_i32(cpu_tmp2_i32
, -1);
6650 tcg_gen_st_i32(cpu_tmp2_i32
, cpu_env
, offsetof(CPUX86State
, df
));
6653 /************************/
6654 /* bit operations */
6655 case 0x1ba: /* bt/bts/btr/btc Gv, im */
6657 modrm
= cpu_ldub_code(env
, s
->pc
++);
6658 op
= (modrm
>> 3) & 7;
6659 mod
= (modrm
>> 6) & 3;
6660 rm
= (modrm
& 7) | REX_B(s
);
6663 gen_lea_modrm(env
, s
, modrm
);
6664 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
6666 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
6669 val
= cpu_ldub_code(env
, s
->pc
++);
6670 tcg_gen_movi_tl(cpu_T
[1], val
);
6675 case 0x1a3: /* bt Gv, Ev */
6678 case 0x1ab: /* bts */
6681 case 0x1b3: /* btr */
6684 case 0x1bb: /* btc */
6688 modrm
= cpu_ldub_code(env
, s
->pc
++);
6689 reg
= ((modrm
>> 3) & 7) | rex_r
;
6690 mod
= (modrm
>> 6) & 3;
6691 rm
= (modrm
& 7) | REX_B(s
);
6692 gen_op_mov_v_reg(MO_32
, cpu_T
[1], reg
);
6694 gen_lea_modrm(env
, s
, modrm
);
6695 /* specific case: we need to add a displacement */
6696 gen_exts(ot
, cpu_T
[1]);
6697 tcg_gen_sari_tl(cpu_tmp0
, cpu_T
[1], 3 + ot
);
6698 tcg_gen_shli_tl(cpu_tmp0
, cpu_tmp0
, ot
);
6699 tcg_gen_add_tl(cpu_A0
, cpu_A0
, cpu_tmp0
);
6700 gen_op_ld_v(s
, ot
, cpu_T
[0], cpu_A0
);
6702 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
6705 tcg_gen_andi_tl(cpu_T
[1], cpu_T
[1], (1 << (3 + ot
)) - 1);
6706 tcg_gen_shr_tl(cpu_tmp4
, cpu_T
[0], cpu_T
[1]);
6711 tcg_gen_movi_tl(cpu_tmp0
, 1);
6712 tcg_gen_shl_tl(cpu_tmp0
, cpu_tmp0
, cpu_T
[1]);
6713 tcg_gen_or_tl(cpu_T
[0], cpu_T
[0], cpu_tmp0
);
6716 tcg_gen_movi_tl(cpu_tmp0
, 1);
6717 tcg_gen_shl_tl(cpu_tmp0
, cpu_tmp0
, cpu_T
[1]);
6718 tcg_gen_andc_tl(cpu_T
[0], cpu_T
[0], cpu_tmp0
);
6722 tcg_gen_movi_tl(cpu_tmp0
, 1);
6723 tcg_gen_shl_tl(cpu_tmp0
, cpu_tmp0
, cpu_T
[1]);
6724 tcg_gen_xor_tl(cpu_T
[0], cpu_T
[0], cpu_tmp0
);
6729 gen_op_st_v(s
, ot
, cpu_T
[0], cpu_A0
);
6731 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
6735 /* Delay all CC updates until after the store above. Note that
6736 C is the result of the test, Z is unchanged, and the others
6737 are all undefined. */
6739 case CC_OP_MULB
... CC_OP_MULQ
:
6740 case CC_OP_ADDB
... CC_OP_ADDQ
:
6741 case CC_OP_ADCB
... CC_OP_ADCQ
:
6742 case CC_OP_SUBB
... CC_OP_SUBQ
:
6743 case CC_OP_SBBB
... CC_OP_SBBQ
:
6744 case CC_OP_LOGICB
... CC_OP_LOGICQ
:
6745 case CC_OP_INCB
... CC_OP_INCQ
:
6746 case CC_OP_DECB
... CC_OP_DECQ
:
6747 case CC_OP_SHLB
... CC_OP_SHLQ
:
6748 case CC_OP_SARB
... CC_OP_SARQ
:
6749 case CC_OP_BMILGB
... CC_OP_BMILGQ
:
6750 /* Z was going to be computed from the non-zero status of CC_DST.
6751 We can get that same Z value (and the new C value) by leaving
6752 CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
6754 tcg_gen_mov_tl(cpu_cc_src
, cpu_tmp4
);
6755 set_cc_op(s
, ((s
->cc_op
- CC_OP_MULB
) & 3) + CC_OP_SARB
);
6758 /* Otherwise, generate EFLAGS and replace the C bit. */
6759 gen_compute_eflags(s
);
6760 tcg_gen_deposit_tl(cpu_cc_src
, cpu_cc_src
, cpu_tmp4
,
6765 case 0x1bc: /* bsf / tzcnt */
6766 case 0x1bd: /* bsr / lzcnt */
6768 modrm
= cpu_ldub_code(env
, s
->pc
++);
6769 reg
= ((modrm
>> 3) & 7) | rex_r
;
6770 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
6771 gen_extu(ot
, cpu_T
[0]);
6773 /* Note that lzcnt and tzcnt are in different extensions. */
6774 if ((prefixes
& PREFIX_REPZ
)
6776 ? s
->cpuid_ext3_features
& CPUID_EXT3_ABM
6777 : s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)) {
6779 tcg_gen_mov_tl(cpu_cc_src
, cpu_T
[0]);
6781 /* For lzcnt, reduce the target_ulong result by the
6782 number of zeros that we expect to find at the top. */
6783 gen_helper_clz(cpu_T
[0], cpu_T
[0]);
6784 tcg_gen_subi_tl(cpu_T
[0], cpu_T
[0], TARGET_LONG_BITS
- size
);
6786 /* For tzcnt, a zero input must return the operand size:
6787 force all bits outside the operand size to 1. */
6788 target_ulong mask
= (target_ulong
)-2 << (size
- 1);
6789 tcg_gen_ori_tl(cpu_T
[0], cpu_T
[0], mask
);
6790 gen_helper_ctz(cpu_T
[0], cpu_T
[0]);
6792 /* For lzcnt/tzcnt, C and Z bits are defined and are
6793 related to the result. */
6794 gen_op_update1_cc();
6795 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
6797 /* For bsr/bsf, only the Z bit is defined and it is related
6798 to the input and not the result. */
6799 tcg_gen_mov_tl(cpu_cc_dst
, cpu_T
[0]);
6800 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
6802 /* For bsr, return the bit index of the first 1 bit,
6803 not the count of leading zeros. */
6804 gen_helper_clz(cpu_T
[0], cpu_T
[0]);
6805 tcg_gen_xori_tl(cpu_T
[0], cpu_T
[0], TARGET_LONG_BITS
- 1);
6807 gen_helper_ctz(cpu_T
[0], cpu_T
[0]);
6809 /* ??? The manual says that the output is undefined when the
6810 input is zero, but real hardware leaves it unchanged, and
6811 real programs appear to depend on that. */
6812 tcg_gen_movi_tl(cpu_tmp0
, 0);
6813 tcg_gen_movcond_tl(TCG_COND_EQ
, cpu_T
[0], cpu_cc_dst
, cpu_tmp0
,
6814 cpu_regs
[reg
], cpu_T
[0]);
6816 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
6818 /************************/
6820 case 0x27: /* daa */
6823 gen_update_cc_op(s
);
6824 gen_helper_daa(cpu_env
);
6825 set_cc_op(s
, CC_OP_EFLAGS
);
6827 case 0x2f: /* das */
6830 gen_update_cc_op(s
);
6831 gen_helper_das(cpu_env
);
6832 set_cc_op(s
, CC_OP_EFLAGS
);
6834 case 0x37: /* aaa */
6837 gen_update_cc_op(s
);
6838 gen_helper_aaa(cpu_env
);
6839 set_cc_op(s
, CC_OP_EFLAGS
);
6841 case 0x3f: /* aas */
6844 gen_update_cc_op(s
);
6845 gen_helper_aas(cpu_env
);
6846 set_cc_op(s
, CC_OP_EFLAGS
);
6848 case 0xd4: /* aam */
6851 val
= cpu_ldub_code(env
, s
->pc
++);
6853 gen_exception(s
, EXCP00_DIVZ
, pc_start
- s
->cs_base
);
6855 gen_helper_aam(cpu_env
, tcg_const_i32(val
));
6856 set_cc_op(s
, CC_OP_LOGICB
);
6859 case 0xd5: /* aad */
6862 val
= cpu_ldub_code(env
, s
->pc
++);
6863 gen_helper_aad(cpu_env
, tcg_const_i32(val
));
6864 set_cc_op(s
, CC_OP_LOGICB
);
6866 /************************/
6868 case 0x90: /* nop */
6869 /* XXX: correct lock test for all insn */
6870 if (prefixes
& PREFIX_LOCK
) {
6873 /* If REX_B is set, then this is xchg eax, r8d, not a nop. */
6875 goto do_xchg_reg_eax
;
6877 if (prefixes
& PREFIX_REPZ
) {
6878 gen_update_cc_op(s
);
6879 gen_jmp_im(pc_start
- s
->cs_base
);
6880 gen_helper_pause(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
6881 s
->is_jmp
= DISAS_TB_JUMP
;
6884 case 0x9b: /* fwait */
6885 if ((s
->flags
& (HF_MP_MASK
| HF_TS_MASK
)) ==
6886 (HF_MP_MASK
| HF_TS_MASK
)) {
6887 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
6889 gen_update_cc_op(s
);
6890 gen_jmp_im(pc_start
- s
->cs_base
);
6891 gen_helper_fwait(cpu_env
);
6894 case 0xcc: /* int3 */
6895 gen_interrupt(s
, EXCP03_INT3
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6897 case 0xcd: /* int N */
6898 val
= cpu_ldub_code(env
, s
->pc
++);
6899 if (s
->vm86
&& s
->iopl
!= 3) {
6900 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6902 gen_interrupt(s
, val
, pc_start
- s
->cs_base
, s
->pc
- s
->cs_base
);
6905 case 0xce: /* into */
6908 gen_update_cc_op(s
);
6909 gen_jmp_im(pc_start
- s
->cs_base
);
6910 gen_helper_into(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
6913 case 0xf1: /* icebp (undocumented, exits to external debugger) */
6914 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_ICEBP
);
6916 gen_debug(s
, pc_start
- s
->cs_base
);
6920 qemu_set_log(CPU_LOG_INT
| CPU_LOG_TB_IN_ASM
);
6924 case 0xfa: /* cli */
6926 if (s
->cpl
<= s
->iopl
) {
6927 gen_helper_cli(cpu_env
);
6929 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6933 gen_helper_cli(cpu_env
);
6935 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6939 case 0xfb: /* sti */
6941 if (s
->cpl
<= s
->iopl
) {
6943 gen_helper_sti(cpu_env
);
6944 /* interruptions are enabled only the first insn after sti */
6945 /* If several instructions disable interrupts, only the
6947 if (!(s
->tb
->flags
& HF_INHIBIT_IRQ_MASK
))
6948 gen_helper_set_inhibit_irq(cpu_env
);
6949 /* give a chance to handle pending irqs */
6950 gen_jmp_im(s
->pc
- s
->cs_base
);
6953 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6959 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
6963 case 0x62: /* bound */
6967 modrm
= cpu_ldub_code(env
, s
->pc
++);
6968 reg
= (modrm
>> 3) & 7;
6969 mod
= (modrm
>> 6) & 3;
6972 gen_op_mov_v_reg(ot
, cpu_T
[0], reg
);
6973 gen_lea_modrm(env
, s
, modrm
);
6974 gen_jmp_im(pc_start
- s
->cs_base
);
6975 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
6977 gen_helper_boundw(cpu_env
, cpu_A0
, cpu_tmp2_i32
);
6979 gen_helper_boundl(cpu_env
, cpu_A0
, cpu_tmp2_i32
);
6982 case 0x1c8 ... 0x1cf: /* bswap reg */
6983 reg
= (b
& 7) | REX_B(s
);
6984 #ifdef TARGET_X86_64
6985 if (dflag
== MO_64
) {
6986 gen_op_mov_v_reg(MO_64
, cpu_T
[0], reg
);
6987 tcg_gen_bswap64_i64(cpu_T
[0], cpu_T
[0]);
6988 gen_op_mov_reg_v(MO_64
, reg
, cpu_T
[0]);
6992 gen_op_mov_v_reg(MO_32
, cpu_T
[0], reg
);
6993 tcg_gen_ext32u_tl(cpu_T
[0], cpu_T
[0]);
6994 tcg_gen_bswap32_tl(cpu_T
[0], cpu_T
[0]);
6995 gen_op_mov_reg_v(MO_32
, reg
, cpu_T
[0]);
6998 case 0xd6: /* salc */
7001 gen_compute_eflags_c(s
, cpu_T
[0]);
7002 tcg_gen_neg_tl(cpu_T
[0], cpu_T
[0]);
7003 gen_op_mov_reg_v(MO_8
, R_EAX
, cpu_T
[0]);
7005 case 0xe0: /* loopnz */
7006 case 0xe1: /* loopz */
7007 case 0xe2: /* loop */
7008 case 0xe3: /* jecxz */
7012 tval
= (int8_t)insn_get(env
, s
, MO_8
);
7013 next_eip
= s
->pc
- s
->cs_base
;
7015 if (dflag
== MO_16
) {
7019 l1
= gen_new_label();
7020 l2
= gen_new_label();
7021 l3
= gen_new_label();
7024 case 0: /* loopnz */
7026 gen_op_add_reg_im(s
->aflag
, R_ECX
, -1);
7027 gen_op_jz_ecx(s
->aflag
, l3
);
7028 gen_jcc1(s
, (JCC_Z
<< 1) | (b
^ 1), l1
);
7031 gen_op_add_reg_im(s
->aflag
, R_ECX
, -1);
7032 gen_op_jnz_ecx(s
->aflag
, l1
);
7036 gen_op_jz_ecx(s
->aflag
, l1
);
7041 gen_jmp_im(next_eip
);
7050 case 0x130: /* wrmsr */
7051 case 0x132: /* rdmsr */
7053 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7055 gen_update_cc_op(s
);
7056 gen_jmp_im(pc_start
- s
->cs_base
);
7058 gen_helper_rdmsr(cpu_env
);
7060 gen_helper_wrmsr(cpu_env
);
7064 case 0x131: /* rdtsc */
7065 gen_update_cc_op(s
);
7066 gen_jmp_im(pc_start
- s
->cs_base
);
7069 gen_helper_rdtsc(cpu_env
);
7072 gen_jmp(s
, s
->pc
- s
->cs_base
);
7075 case 0x133: /* rdpmc */
7076 gen_update_cc_op(s
);
7077 gen_jmp_im(pc_start
- s
->cs_base
);
7078 gen_helper_rdpmc(cpu_env
);
7080 case 0x134: /* sysenter */
7081 /* For Intel SYSENTER is valid on 64-bit */
7082 if (CODE64(s
) && env
->cpuid_vendor1
!= CPUID_VENDOR_INTEL_1
)
7085 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7087 gen_update_cc_op(s
);
7088 gen_jmp_im(pc_start
- s
->cs_base
);
7089 gen_helper_sysenter(cpu_env
);
7093 case 0x135: /* sysexit */
7094 /* For Intel SYSEXIT is valid on 64-bit */
7095 if (CODE64(s
) && env
->cpuid_vendor1
!= CPUID_VENDOR_INTEL_1
)
7098 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7100 gen_update_cc_op(s
);
7101 gen_jmp_im(pc_start
- s
->cs_base
);
7102 gen_helper_sysexit(cpu_env
, tcg_const_i32(dflag
- 1));
7106 #ifdef TARGET_X86_64
7107 case 0x105: /* syscall */
7108 /* XXX: is it usable in real mode ? */
7109 gen_update_cc_op(s
);
7110 gen_jmp_im(pc_start
- s
->cs_base
);
7111 gen_helper_syscall(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7114 case 0x107: /* sysret */
7116 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7118 gen_update_cc_op(s
);
7119 gen_jmp_im(pc_start
- s
->cs_base
);
7120 gen_helper_sysret(cpu_env
, tcg_const_i32(dflag
- 1));
7121 /* condition codes are modified only in long mode */
7123 set_cc_op(s
, CC_OP_EFLAGS
);
7129 case 0x1a2: /* cpuid */
7130 gen_update_cc_op(s
);
7131 gen_jmp_im(pc_start
- s
->cs_base
);
7132 gen_helper_cpuid(cpu_env
);
7134 case 0xf4: /* hlt */
7136 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7138 gen_update_cc_op(s
);
7139 gen_jmp_im(pc_start
- s
->cs_base
);
7140 gen_helper_hlt(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7141 s
->is_jmp
= DISAS_TB_JUMP
;
7145 modrm
= cpu_ldub_code(env
, s
->pc
++);
7146 mod
= (modrm
>> 6) & 3;
7147 op
= (modrm
>> 3) & 7;
7150 if (!s
->pe
|| s
->vm86
)
7152 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_LDTR_READ
);
7153 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,ldt
.selector
));
7154 ot
= mod
== 3 ? dflag
: MO_16
;
7155 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
7158 if (!s
->pe
|| s
->vm86
)
7161 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7163 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_LDTR_WRITE
);
7164 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7165 gen_jmp_im(pc_start
- s
->cs_base
);
7166 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
7167 gen_helper_lldt(cpu_env
, cpu_tmp2_i32
);
7171 if (!s
->pe
|| s
->vm86
)
7173 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_TR_READ
);
7174 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,tr
.selector
));
7175 ot
= mod
== 3 ? dflag
: MO_16
;
7176 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
7179 if (!s
->pe
|| s
->vm86
)
7182 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7184 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_TR_WRITE
);
7185 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7186 gen_jmp_im(pc_start
- s
->cs_base
);
7187 tcg_gen_trunc_tl_i32(cpu_tmp2_i32
, cpu_T
[0]);
7188 gen_helper_ltr(cpu_env
, cpu_tmp2_i32
);
7193 if (!s
->pe
|| s
->vm86
)
7195 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7196 gen_update_cc_op(s
);
7198 gen_helper_verr(cpu_env
, cpu_T
[0]);
7200 gen_helper_verw(cpu_env
, cpu_T
[0]);
7202 set_cc_op(s
, CC_OP_EFLAGS
);
7209 modrm
= cpu_ldub_code(env
, s
->pc
++);
7210 mod
= (modrm
>> 6) & 3;
7211 op
= (modrm
>> 3) & 7;
7217 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_GDTR_READ
);
7218 gen_lea_modrm(env
, s
, modrm
);
7219 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, gdt
.limit
));
7220 gen_op_st_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
7221 gen_add_A0_im(s
, 2);
7222 tcg_gen_ld_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, gdt
.base
));
7223 if (dflag
== MO_16
) {
7224 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 0xffffff);
7226 gen_op_st_v(s
, CODE64(s
) + MO_32
, cpu_T
[0], cpu_A0
);
7231 case 0: /* monitor */
7232 if (!(s
->cpuid_ext_features
& CPUID_EXT_MONITOR
) ||
7235 gen_update_cc_op(s
);
7236 gen_jmp_im(pc_start
- s
->cs_base
);
7237 tcg_gen_mov_tl(cpu_A0
, cpu_regs
[R_EAX
]);
7238 gen_extu(s
->aflag
, cpu_A0
);
7239 gen_add_A0_ds_seg(s
);
7240 gen_helper_monitor(cpu_env
, cpu_A0
);
7243 if (!(s
->cpuid_ext_features
& CPUID_EXT_MONITOR
) ||
7246 gen_update_cc_op(s
);
7247 gen_jmp_im(pc_start
- s
->cs_base
);
7248 gen_helper_mwait(cpu_env
, tcg_const_i32(s
->pc
- pc_start
));
7252 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_SMAP
) ||
7256 gen_helper_clac(cpu_env
);
7257 gen_jmp_im(s
->pc
- s
->cs_base
);
7261 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_SMAP
) ||
7265 gen_helper_stac(cpu_env
);
7266 gen_jmp_im(s
->pc
- s
->cs_base
);
7273 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_IDTR_READ
);
7274 gen_lea_modrm(env
, s
, modrm
);
7275 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, idt
.limit
));
7276 gen_op_st_v(s
, MO_16
, cpu_T
[0], cpu_A0
);
7277 gen_add_A0_im(s
, 2);
7278 tcg_gen_ld_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, idt
.base
));
7279 if (dflag
== MO_16
) {
7280 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 0xffffff);
7282 gen_op_st_v(s
, CODE64(s
) + MO_32
, cpu_T
[0], cpu_A0
);
7288 gen_update_cc_op(s
);
7289 gen_jmp_im(pc_start
- s
->cs_base
);
7292 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7295 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7298 gen_helper_vmrun(cpu_env
, tcg_const_i32(s
->aflag
- 1),
7299 tcg_const_i32(s
->pc
- pc_start
));
7301 s
->is_jmp
= DISAS_TB_JUMP
;
7304 case 1: /* VMMCALL */
7305 if (!(s
->flags
& HF_SVME_MASK
))
7307 gen_helper_vmmcall(cpu_env
);
7309 case 2: /* VMLOAD */
7310 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7313 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7316 gen_helper_vmload(cpu_env
, tcg_const_i32(s
->aflag
- 1));
7319 case 3: /* VMSAVE */
7320 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7323 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7326 gen_helper_vmsave(cpu_env
, tcg_const_i32(s
->aflag
- 1));
7330 if ((!(s
->flags
& HF_SVME_MASK
) &&
7331 !(s
->cpuid_ext3_features
& CPUID_EXT3_SKINIT
)) ||
7335 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7338 gen_helper_stgi(cpu_env
);
7342 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7345 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7348 gen_helper_clgi(cpu_env
);
7351 case 6: /* SKINIT */
7352 if ((!(s
->flags
& HF_SVME_MASK
) &&
7353 !(s
->cpuid_ext3_features
& CPUID_EXT3_SKINIT
)) ||
7356 gen_helper_skinit(cpu_env
);
7358 case 7: /* INVLPGA */
7359 if (!(s
->flags
& HF_SVME_MASK
) || !s
->pe
)
7362 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7365 gen_helper_invlpga(cpu_env
,
7366 tcg_const_i32(s
->aflag
- 1));
7372 } else if (s
->cpl
!= 0) {
7373 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7375 gen_svm_check_intercept(s
, pc_start
,
7376 op
==2 ? SVM_EXIT_GDTR_WRITE
: SVM_EXIT_IDTR_WRITE
);
7377 gen_lea_modrm(env
, s
, modrm
);
7378 gen_op_ld_v(s
, MO_16
, cpu_T
[1], cpu_A0
);
7379 gen_add_A0_im(s
, 2);
7380 gen_op_ld_v(s
, CODE64(s
) + MO_32
, cpu_T
[0], cpu_A0
);
7381 if (dflag
== MO_16
) {
7382 tcg_gen_andi_tl(cpu_T
[0], cpu_T
[0], 0xffffff);
7385 tcg_gen_st_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,gdt
.base
));
7386 tcg_gen_st32_tl(cpu_T
[1], cpu_env
, offsetof(CPUX86State
,gdt
.limit
));
7388 tcg_gen_st_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,idt
.base
));
7389 tcg_gen_st32_tl(cpu_T
[1], cpu_env
, offsetof(CPUX86State
,idt
.limit
));
7394 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_READ_CR0
);
7395 #if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
7396 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,cr
[0]) + 4);
7398 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,cr
[0]));
7400 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 1);
7404 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7406 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_WRITE_CR0
);
7407 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7408 gen_helper_lmsw(cpu_env
, cpu_T
[0]);
7409 gen_jmp_im(s
->pc
- s
->cs_base
);
7414 if (mod
!= 3) { /* invlpg */
7416 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7418 gen_update_cc_op(s
);
7419 gen_jmp_im(pc_start
- s
->cs_base
);
7420 gen_lea_modrm(env
, s
, modrm
);
7421 gen_helper_invlpg(cpu_env
, cpu_A0
);
7422 gen_jmp_im(s
->pc
- s
->cs_base
);
7427 case 0: /* swapgs */
7428 #ifdef TARGET_X86_64
7431 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7433 tcg_gen_ld_tl(cpu_T
[0], cpu_env
,
7434 offsetof(CPUX86State
,segs
[R_GS
].base
));
7435 tcg_gen_ld_tl(cpu_T
[1], cpu_env
,
7436 offsetof(CPUX86State
,kernelgsbase
));
7437 tcg_gen_st_tl(cpu_T
[1], cpu_env
,
7438 offsetof(CPUX86State
,segs
[R_GS
].base
));
7439 tcg_gen_st_tl(cpu_T
[0], cpu_env
,
7440 offsetof(CPUX86State
,kernelgsbase
));
7448 case 1: /* rdtscp */
7449 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_RDTSCP
))
7451 gen_update_cc_op(s
);
7452 gen_jmp_im(pc_start
- s
->cs_base
);
7455 gen_helper_rdtscp(cpu_env
);
7458 gen_jmp(s
, s
->pc
- s
->cs_base
);
7470 case 0x108: /* invd */
7471 case 0x109: /* wbinvd */
7473 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7475 gen_svm_check_intercept(s
, pc_start
, (b
& 2) ? SVM_EXIT_INVD
: SVM_EXIT_WBINVD
);
7479 case 0x63: /* arpl or movslS (x86_64) */
7480 #ifdef TARGET_X86_64
7483 /* d_ot is the size of destination */
7486 modrm
= cpu_ldub_code(env
, s
->pc
++);
7487 reg
= ((modrm
>> 3) & 7) | rex_r
;
7488 mod
= (modrm
>> 6) & 3;
7489 rm
= (modrm
& 7) | REX_B(s
);
7492 gen_op_mov_v_reg(MO_32
, cpu_T
[0], rm
);
7494 if (d_ot
== MO_64
) {
7495 tcg_gen_ext32s_tl(cpu_T
[0], cpu_T
[0]);
7497 gen_op_mov_reg_v(d_ot
, reg
, cpu_T
[0]);
7499 gen_lea_modrm(env
, s
, modrm
);
7500 gen_op_ld_v(s
, MO_32
| MO_SIGN
, cpu_T
[0], cpu_A0
);
7501 gen_op_mov_reg_v(d_ot
, reg
, cpu_T
[0]);
7507 TCGv t0
, t1
, t2
, a0
;
7509 if (!s
->pe
|| s
->vm86
)
7511 t0
= tcg_temp_local_new();
7512 t1
= tcg_temp_local_new();
7513 t2
= tcg_temp_local_new();
7515 modrm
= cpu_ldub_code(env
, s
->pc
++);
7516 reg
= (modrm
>> 3) & 7;
7517 mod
= (modrm
>> 6) & 3;
7520 gen_lea_modrm(env
, s
, modrm
);
7521 gen_op_ld_v(s
, ot
, t0
, cpu_A0
);
7522 a0
= tcg_temp_local_new();
7523 tcg_gen_mov_tl(a0
, cpu_A0
);
7525 gen_op_mov_v_reg(ot
, t0
, rm
);
7528 gen_op_mov_v_reg(ot
, t1
, reg
);
7529 tcg_gen_andi_tl(cpu_tmp0
, t0
, 3);
7530 tcg_gen_andi_tl(t1
, t1
, 3);
7531 tcg_gen_movi_tl(t2
, 0);
7532 label1
= gen_new_label();
7533 tcg_gen_brcond_tl(TCG_COND_GE
, cpu_tmp0
, t1
, label1
);
7534 tcg_gen_andi_tl(t0
, t0
, ~3);
7535 tcg_gen_or_tl(t0
, t0
, t1
);
7536 tcg_gen_movi_tl(t2
, CC_Z
);
7537 gen_set_label(label1
);
7539 gen_op_st_v(s
, ot
, t0
, a0
);
7542 gen_op_mov_reg_v(ot
, rm
, t0
);
7544 gen_compute_eflags(s
);
7545 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, ~CC_Z
);
7546 tcg_gen_or_tl(cpu_cc_src
, cpu_cc_src
, t2
);
7552 case 0x102: /* lar */
7553 case 0x103: /* lsl */
7557 if (!s
->pe
|| s
->vm86
)
7559 ot
= dflag
!= MO_16
? MO_32
: MO_16
;
7560 modrm
= cpu_ldub_code(env
, s
->pc
++);
7561 reg
= ((modrm
>> 3) & 7) | rex_r
;
7562 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
7563 t0
= tcg_temp_local_new();
7564 gen_update_cc_op(s
);
7566 gen_helper_lar(t0
, cpu_env
, cpu_T
[0]);
7568 gen_helper_lsl(t0
, cpu_env
, cpu_T
[0]);
7570 tcg_gen_andi_tl(cpu_tmp0
, cpu_cc_src
, CC_Z
);
7571 label1
= gen_new_label();
7572 tcg_gen_brcondi_tl(TCG_COND_EQ
, cpu_tmp0
, 0, label1
);
7573 gen_op_mov_reg_v(ot
, reg
, t0
);
7574 gen_set_label(label1
);
7575 set_cc_op(s
, CC_OP_EFLAGS
);
7580 modrm
= cpu_ldub_code(env
, s
->pc
++);
7581 mod
= (modrm
>> 6) & 3;
7582 op
= (modrm
>> 3) & 7;
7584 case 0: /* prefetchnta */
7585 case 1: /* prefetchnt0 */
7586 case 2: /* prefetchnt0 */
7587 case 3: /* prefetchnt0 */
7590 gen_lea_modrm(env
, s
, modrm
);
7591 /* nothing more to do */
7593 default: /* nop (multi byte) */
7594 gen_nop_modrm(env
, s
, modrm
);
7598 case 0x119 ... 0x11f: /* nop (multi byte) */
7599 modrm
= cpu_ldub_code(env
, s
->pc
++);
7600 gen_nop_modrm(env
, s
, modrm
);
7602 case 0x120: /* mov reg, crN */
7603 case 0x122: /* mov crN, reg */
7605 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7607 modrm
= cpu_ldub_code(env
, s
->pc
++);
7608 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7609 * AMD documentation (24594.pdf) and testing of
7610 * intel 386 and 486 processors all show that the mod bits
7611 * are assumed to be 1's, regardless of actual values.
7613 rm
= (modrm
& 7) | REX_B(s
);
7614 reg
= ((modrm
>> 3) & 7) | rex_r
;
7619 if ((prefixes
& PREFIX_LOCK
) && (reg
== 0) &&
7620 (s
->cpuid_ext3_features
& CPUID_EXT3_CR8LEG
)) {
7629 gen_update_cc_op(s
);
7630 gen_jmp_im(pc_start
- s
->cs_base
);
7632 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
7633 gen_helper_write_crN(cpu_env
, tcg_const_i32(reg
),
7635 gen_jmp_im(s
->pc
- s
->cs_base
);
7638 gen_helper_read_crN(cpu_T
[0], cpu_env
, tcg_const_i32(reg
));
7639 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
7647 case 0x121: /* mov reg, drN */
7648 case 0x123: /* mov drN, reg */
7650 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7652 modrm
= cpu_ldub_code(env
, s
->pc
++);
7653 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7654 * AMD documentation (24594.pdf) and testing of
7655 * intel 386 and 486 processors all show that the mod bits
7656 * are assumed to be 1's, regardless of actual values.
7658 rm
= (modrm
& 7) | REX_B(s
);
7659 reg
= ((modrm
>> 3) & 7) | rex_r
;
7664 /* XXX: do it dynamically with CR4.DE bit */
7665 if (reg
== 4 || reg
== 5 || reg
>= 8)
7668 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_WRITE_DR0
+ reg
);
7669 gen_op_mov_v_reg(ot
, cpu_T
[0], rm
);
7670 gen_helper_movl_drN_T0(cpu_env
, tcg_const_i32(reg
), cpu_T
[0]);
7671 gen_jmp_im(s
->pc
- s
->cs_base
);
7674 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_READ_DR0
+ reg
);
7675 tcg_gen_ld_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
,dr
[reg
]));
7676 gen_op_mov_reg_v(ot
, rm
, cpu_T
[0]);
7680 case 0x106: /* clts */
7682 gen_exception(s
, EXCP0D_GPF
, pc_start
- s
->cs_base
);
7684 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_WRITE_CR0
);
7685 gen_helper_clts(cpu_env
);
7686 /* abort block because static cpu state changed */
7687 gen_jmp_im(s
->pc
- s
->cs_base
);
7691 /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
7692 case 0x1c3: /* MOVNTI reg, mem */
7693 if (!(s
->cpuid_features
& CPUID_SSE2
))
7695 ot
= mo_64_32(dflag
);
7696 modrm
= cpu_ldub_code(env
, s
->pc
++);
7697 mod
= (modrm
>> 6) & 3;
7700 reg
= ((modrm
>> 3) & 7) | rex_r
;
7701 /* generate a generic store */
7702 gen_ldst_modrm(env
, s
, modrm
, ot
, reg
, 1);
7705 modrm
= cpu_ldub_code(env
, s
->pc
++);
7706 mod
= (modrm
>> 6) & 3;
7707 op
= (modrm
>> 3) & 7;
7709 case 0: /* fxsave */
7710 if (mod
== 3 || !(s
->cpuid_features
& CPUID_FXSR
) ||
7711 (s
->prefix
& PREFIX_LOCK
))
7713 if ((s
->flags
& HF_EM_MASK
) || (s
->flags
& HF_TS_MASK
)) {
7714 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
7717 gen_lea_modrm(env
, s
, modrm
);
7718 gen_update_cc_op(s
);
7719 gen_jmp_im(pc_start
- s
->cs_base
);
7720 gen_helper_fxsave(cpu_env
, cpu_A0
, tcg_const_i32(dflag
== MO_64
));
7722 case 1: /* fxrstor */
7723 if (mod
== 3 || !(s
->cpuid_features
& CPUID_FXSR
) ||
7724 (s
->prefix
& PREFIX_LOCK
))
7726 if ((s
->flags
& HF_EM_MASK
) || (s
->flags
& HF_TS_MASK
)) {
7727 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
7730 gen_lea_modrm(env
, s
, modrm
);
7731 gen_update_cc_op(s
);
7732 gen_jmp_im(pc_start
- s
->cs_base
);
7733 gen_helper_fxrstor(cpu_env
, cpu_A0
, tcg_const_i32(dflag
== MO_64
));
7735 case 2: /* ldmxcsr */
7736 case 3: /* stmxcsr */
7737 if (s
->flags
& HF_TS_MASK
) {
7738 gen_exception(s
, EXCP07_PREX
, pc_start
- s
->cs_base
);
7741 if ((s
->flags
& HF_EM_MASK
) || !(s
->flags
& HF_OSFXSR_MASK
) ||
7744 gen_lea_modrm(env
, s
, modrm
);
7746 tcg_gen_qemu_ld_i32(cpu_tmp2_i32
, cpu_A0
,
7747 s
->mem_index
, MO_LEUL
);
7748 gen_helper_ldmxcsr(cpu_env
, cpu_tmp2_i32
);
7750 tcg_gen_ld32u_tl(cpu_T
[0], cpu_env
, offsetof(CPUX86State
, mxcsr
));
7751 gen_op_st_v(s
, MO_32
, cpu_T
[0], cpu_A0
);
7754 case 5: /* lfence */
7755 case 6: /* mfence */
7756 if ((modrm
& 0xc7) != 0xc0 || !(s
->cpuid_features
& CPUID_SSE2
))
7759 case 7: /* sfence / clflush */
7760 if ((modrm
& 0xc7) == 0xc0) {
7762 /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
7763 if (!(s
->cpuid_features
& CPUID_SSE
))
7767 if (!(s
->cpuid_features
& CPUID_CLFLUSH
))
7769 gen_lea_modrm(env
, s
, modrm
);
7776 case 0x10d: /* 3DNow! prefetch(w) */
7777 modrm
= cpu_ldub_code(env
, s
->pc
++);
7778 mod
= (modrm
>> 6) & 3;
7781 gen_lea_modrm(env
, s
, modrm
);
7782 /* ignore for now */
7784 case 0x1aa: /* rsm */
7785 gen_svm_check_intercept(s
, pc_start
, SVM_EXIT_RSM
);
7786 if (!(s
->flags
& HF_SMM_MASK
))
7788 gen_update_cc_op(s
);
7789 gen_jmp_im(s
->pc
- s
->cs_base
);
7790 gen_helper_rsm(cpu_env
);
7793 case 0x1b8: /* SSE4.2 popcnt */
7794 if ((prefixes
& (PREFIX_REPZ
| PREFIX_LOCK
| PREFIX_REPNZ
)) !=
7797 if (!(s
->cpuid_ext_features
& CPUID_EXT_POPCNT
))
7800 modrm
= cpu_ldub_code(env
, s
->pc
++);
7801 reg
= ((modrm
>> 3) & 7) | rex_r
;
7803 if (s
->prefix
& PREFIX_DATA
) {
7806 ot
= mo_64_32(dflag
);
7809 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
7810 gen_helper_popcnt(cpu_T
[0], cpu_env
, cpu_T
[0], tcg_const_i32(ot
));
7811 gen_op_mov_reg_v(ot
, reg
, cpu_T
[0]);
7813 set_cc_op(s
, CC_OP_EFLAGS
);
7815 case 0x10e ... 0x10f:
7816 /* 3DNow! instructions, ignore prefixes */
7817 s
->prefix
&= ~(PREFIX_REPZ
| PREFIX_REPNZ
| PREFIX_DATA
);
7818 case 0x110 ... 0x117:
7819 case 0x128 ... 0x12f:
7820 case 0x138 ... 0x13a:
7821 case 0x150 ... 0x179:
7822 case 0x17c ... 0x17f:
7824 case 0x1c4 ... 0x1c6:
7825 case 0x1d0 ... 0x1fe:
7826 gen_sse(env
, s
, b
, pc_start
, rex_r
);
7831 /* lock generation */
7832 if (s
->prefix
& PREFIX_LOCK
)
7833 gen_helper_unlock();
7836 if (s
->prefix
& PREFIX_LOCK
)
7837 gen_helper_unlock();
7838 /* XXX: ensure that no lock was generated */
7839 gen_exception(s
, EXCP06_ILLOP
, pc_start
- s
->cs_base
);
7843 void optimize_flags_init(void)
7845 static const char reg_names
[CPU_NB_REGS
][4] = {
7846 #ifdef TARGET_X86_64
7876 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
7877 cpu_cc_op
= tcg_global_mem_new_i32(TCG_AREG0
,
7878 offsetof(CPUX86State
, cc_op
), "cc_op");
7879 cpu_cc_dst
= tcg_global_mem_new(TCG_AREG0
, offsetof(CPUX86State
, cc_dst
),
7881 cpu_cc_src
= tcg_global_mem_new(TCG_AREG0
, offsetof(CPUX86State
, cc_src
),
7883 cpu_cc_src2
= tcg_global_mem_new(TCG_AREG0
, offsetof(CPUX86State
, cc_src2
),
7886 for (i
= 0; i
< CPU_NB_REGS
; ++i
) {
7887 cpu_regs
[i
] = tcg_global_mem_new(TCG_AREG0
,
7888 offsetof(CPUX86State
, regs
[i
]),
7893 /* generate intermediate code in gen_opc_buf and gen_opparam_buf for
7894 basic block 'tb'. If search_pc is TRUE, also generate PC
7895 information for each intermediate instruction. */
7896 static inline void gen_intermediate_code_internal(X86CPU
*cpu
,
7897 TranslationBlock
*tb
,
7900 CPUState
*cs
= CPU(cpu
);
7901 CPUX86State
*env
= &cpu
->env
;
7902 DisasContext dc1
, *dc
= &dc1
;
7903 target_ulong pc_ptr
;
7904 uint16_t *gen_opc_end
;
7908 target_ulong pc_start
;
7909 target_ulong cs_base
;
7913 /* generate intermediate code */
7915 cs_base
= tb
->cs_base
;
7918 dc
->pe
= (flags
>> HF_PE_SHIFT
) & 1;
7919 dc
->code32
= (flags
>> HF_CS32_SHIFT
) & 1;
7920 dc
->ss32
= (flags
>> HF_SS32_SHIFT
) & 1;
7921 dc
->addseg
= (flags
>> HF_ADDSEG_SHIFT
) & 1;
7923 dc
->vm86
= (flags
>> VM_SHIFT
) & 1;
7924 dc
->cpl
= (flags
>> HF_CPL_SHIFT
) & 3;
7925 dc
->iopl
= (flags
>> IOPL_SHIFT
) & 3;
7926 dc
->tf
= (flags
>> TF_SHIFT
) & 1;
7927 dc
->singlestep_enabled
= cs
->singlestep_enabled
;
7928 dc
->cc_op
= CC_OP_DYNAMIC
;
7929 dc
->cc_op_dirty
= false;
7930 dc
->cs_base
= cs_base
;
7932 dc
->popl_esp_hack
= 0;
7933 /* select memory access functions */
7935 if (flags
& HF_SOFTMMU_MASK
) {
7936 dc
->mem_index
= cpu_mmu_index(env
);
7938 dc
->cpuid_features
= env
->features
[FEAT_1_EDX
];
7939 dc
->cpuid_ext_features
= env
->features
[FEAT_1_ECX
];
7940 dc
->cpuid_ext2_features
= env
->features
[FEAT_8000_0001_EDX
];
7941 dc
->cpuid_ext3_features
= env
->features
[FEAT_8000_0001_ECX
];
7942 dc
->cpuid_7_0_ebx_features
= env
->features
[FEAT_7_0_EBX
];
7943 #ifdef TARGET_X86_64
7944 dc
->lma
= (flags
>> HF_LMA_SHIFT
) & 1;
7945 dc
->code64
= (flags
>> HF_CS64_SHIFT
) & 1;
7948 dc
->jmp_opt
= !(dc
->tf
|| cs
->singlestep_enabled
||
7949 (flags
& HF_INHIBIT_IRQ_MASK
)
7950 #ifndef CONFIG_SOFTMMU
7951 || (flags
& HF_SOFTMMU_MASK
)
7955 /* check addseg logic */
7956 if (!dc
->addseg
&& (dc
->vm86
|| !dc
->pe
|| !dc
->code32
))
7957 printf("ERROR addseg\n");
7960 cpu_T
[0] = tcg_temp_new();
7961 cpu_T
[1] = tcg_temp_new();
7962 cpu_A0
= tcg_temp_new();
7964 cpu_tmp0
= tcg_temp_new();
7965 cpu_tmp1_i64
= tcg_temp_new_i64();
7966 cpu_tmp2_i32
= tcg_temp_new_i32();
7967 cpu_tmp3_i32
= tcg_temp_new_i32();
7968 cpu_tmp4
= tcg_temp_new();
7969 cpu_ptr0
= tcg_temp_new_ptr();
7970 cpu_ptr1
= tcg_temp_new_ptr();
7971 cpu_cc_srcT
= tcg_temp_local_new();
7973 gen_opc_end
= tcg_ctx
.gen_opc_buf
+ OPC_MAX_SIZE
;
7975 dc
->is_jmp
= DISAS_NEXT
;
7979 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
7981 max_insns
= CF_COUNT_MASK
;
7985 if (unlikely(!QTAILQ_EMPTY(&cs
->breakpoints
))) {
7986 QTAILQ_FOREACH(bp
, &cs
->breakpoints
, entry
) {
7987 if (bp
->pc
== pc_ptr
&&
7988 !((bp
->flags
& BP_CPU
) && (tb
->flags
& HF_RF_MASK
))) {
7989 gen_debug(dc
, pc_ptr
- dc
->cs_base
);
7995 j
= tcg_ctx
.gen_opc_ptr
- tcg_ctx
.gen_opc_buf
;
7999 tcg_ctx
.gen_opc_instr_start
[lj
++] = 0;
8001 tcg_ctx
.gen_opc_pc
[lj
] = pc_ptr
;
8002 gen_opc_cc_op
[lj
] = dc
->cc_op
;
8003 tcg_ctx
.gen_opc_instr_start
[lj
] = 1;
8004 tcg_ctx
.gen_opc_icount
[lj
] = num_insns
;
8006 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
))
8009 pc_ptr
= disas_insn(env
, dc
, pc_ptr
);
8011 /* stop translation if indicated */
8014 /* if single step mode, we generate only one instruction and
8015 generate an exception */
8016 /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
8017 the flag and abort the translation to give the irqs a
8018 change to be happen */
8019 if (dc
->tf
|| dc
->singlestep_enabled
||
8020 (flags
& HF_INHIBIT_IRQ_MASK
)) {
8021 gen_jmp_im(pc_ptr
- dc
->cs_base
);
8025 /* if too long translation, stop generation too */
8026 if (tcg_ctx
.gen_opc_ptr
>= gen_opc_end
||
8027 (pc_ptr
- pc_start
) >= (TARGET_PAGE_SIZE
- 32) ||
8028 num_insns
>= max_insns
) {
8029 gen_jmp_im(pc_ptr
- dc
->cs_base
);
8034 gen_jmp_im(pc_ptr
- dc
->cs_base
);
8039 if (tb
->cflags
& CF_LAST_IO
)
8041 gen_tb_end(tb
, num_insns
);
8042 *tcg_ctx
.gen_opc_ptr
= INDEX_op_end
;
8043 /* we don't forget to fill the last values */
8045 j
= tcg_ctx
.gen_opc_ptr
- tcg_ctx
.gen_opc_buf
;
8048 tcg_ctx
.gen_opc_instr_start
[lj
++] = 0;
8052 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
8054 qemu_log("----------------\n");
8055 qemu_log("IN: %s\n", lookup_symbol(pc_start
));
8056 #ifdef TARGET_X86_64
8061 disas_flags
= !dc
->code32
;
8062 log_target_disas(env
, pc_start
, pc_ptr
- pc_start
, disas_flags
);
8068 tb
->size
= pc_ptr
- pc_start
;
8069 tb
->icount
= num_insns
;
8073 void gen_intermediate_code(CPUX86State
*env
, TranslationBlock
*tb
)
8075 gen_intermediate_code_internal(x86_env_get_cpu(env
), tb
, false);
8078 void gen_intermediate_code_pc(CPUX86State
*env
, TranslationBlock
*tb
)
8080 gen_intermediate_code_internal(x86_env_get_cpu(env
), tb
, true);
8083 void restore_state_to_opc(CPUX86State
*env
, TranslationBlock
*tb
, int pc_pos
)
8087 if (qemu_loglevel_mask(CPU_LOG_TB_OP
)) {
8089 qemu_log("RESTORE:\n");
8090 for(i
= 0;i
<= pc_pos
; i
++) {
8091 if (tcg_ctx
.gen_opc_instr_start
[i
]) {
8092 qemu_log("0x%04x: " TARGET_FMT_lx
"\n", i
,
8093 tcg_ctx
.gen_opc_pc
[i
]);
8096 qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx
" cs_base=%x\n",
8097 pc_pos
, tcg_ctx
.gen_opc_pc
[pc_pos
] - tb
->cs_base
,
8098 (uint32_t)tb
->cs_base
);
8101 env
->eip
= tcg_ctx
.gen_opc_pc
[pc_pos
] - tb
->cs_base
;
8102 cc_op
= gen_opc_cc_op
[pc_pos
];
8103 if (cc_op
!= CC_OP_DYNAMIC
)