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.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "qemu/host-utils.h"
23 #include "disas/disas.h"
24 #include "exec/exec-all.h"
25 #include "tcg/tcg-op.h"
26 #include "tcg/tcg-op-gvec.h"
27 #include "exec/cpu_ldst.h"
28 #include "exec/translator.h"
29 #include "fpu/softfloat.h"
31 #include "exec/helper-proto.h"
32 #include "exec/helper-gen.h"
33 #include "helper-tcg.h"
37 #define PREFIX_REPZ 0x01
38 #define PREFIX_REPNZ 0x02
39 #define PREFIX_LOCK 0x04
40 #define PREFIX_DATA 0x08
41 #define PREFIX_ADR 0x10
42 #define PREFIX_VEX 0x20
43 #define PREFIX_REX 0x40
53 /* For a switch indexed by MODRM, match all memory operands for a given OP. */
54 #define CASE_MODRM_MEM_OP(OP) \
55 case (0 << 6) | (OP << 3) | 0 ... (0 << 6) | (OP << 3) | 7: \
56 case (1 << 6) | (OP << 3) | 0 ... (1 << 6) | (OP << 3) | 7: \
57 case (2 << 6) | (OP << 3) | 0 ... (2 << 6) | (OP << 3) | 7
59 #define CASE_MODRM_OP(OP) \
60 case (0 << 6) | (OP << 3) | 0 ... (0 << 6) | (OP << 3) | 7: \
61 case (1 << 6) | (OP << 3) | 0 ... (1 << 6) | (OP << 3) | 7: \
62 case (2 << 6) | (OP << 3) | 0 ... (2 << 6) | (OP << 3) | 7: \
63 case (3 << 6) | (OP << 3) | 0 ... (3 << 6) | (OP << 3) | 7
65 //#define MACRO_TEST 1
67 /* global register indexes */
68 static TCGv cpu_cc_dst
, cpu_cc_src
, cpu_cc_src2
;
70 static TCGv_i32 cpu_cc_op
;
71 static TCGv cpu_regs
[CPU_NB_REGS
];
72 static TCGv cpu_seg_base
[6];
73 static TCGv_i64 cpu_bndl
[4];
74 static TCGv_i64 cpu_bndu
[4];
76 #include "exec/gen-icount.h"
78 typedef struct DisasContext
{
79 DisasContextBase base
;
81 target_ulong pc
; /* pc = eip + cs_base */
82 target_ulong cs_base
; /* base of CS segment */
88 int8_t override
; /* -1 if no override, else R_CS, R_DS, etc */
94 #ifndef CONFIG_USER_ONLY
95 uint8_t cpl
; /* code priv level */
96 uint8_t iopl
; /* i/o priv level */
98 uint8_t vex_l
; /* vex vector length */
99 uint8_t vex_v
; /* vex vvvv register, without 1's complement. */
100 uint8_t popl_esp_hack
; /* for correct popl with esp base handling */
101 uint8_t rip_offset
; /* only used in x86_64, but left for simplicity */
108 bool vex_w
; /* used by AVX even on 32-bit processors */
109 bool jmp_opt
; /* use direct block chaining for direct jumps */
110 bool repz_opt
; /* optimize jumps within repz instructions */
113 CCOp cc_op
; /* current CC operation */
114 int mem_index
; /* select memory access functions */
115 uint32_t flags
; /* all execution flags */
117 int cpuid_ext_features
;
118 int cpuid_ext2_features
;
119 int cpuid_ext3_features
;
120 int cpuid_7_0_ebx_features
;
121 int cpuid_7_0_ecx_features
;
122 int cpuid_xsave_features
;
124 /* TCG local temps */
130 /* TCG local register indexes (only used inside old micro ops) */
138 TCGOp
*prev_insn_end
;
141 #define DISAS_EOB_ONLY DISAS_TARGET_0
142 #define DISAS_EOB_NEXT DISAS_TARGET_1
143 #define DISAS_EOB_INHIBIT_IRQ DISAS_TARGET_2
144 #define DISAS_JUMP DISAS_TARGET_3
146 /* The environment in which user-only runs is constrained. */
147 #ifdef CONFIG_USER_ONLY
151 #define SVME(S) false
152 #define GUEST(S) false
154 #define PE(S) (((S)->flags & HF_PE_MASK) != 0)
155 #define CPL(S) ((S)->cpl)
156 #define IOPL(S) ((S)->iopl)
157 #define SVME(S) (((S)->flags & HF_SVME_MASK) != 0)
158 #define GUEST(S) (((S)->flags & HF_GUEST_MASK) != 0)
160 #if defined(CONFIG_USER_ONLY) && defined(TARGET_X86_64)
161 #define VM86(S) false
162 #define CODE32(S) true
164 #define ADDSEG(S) false
166 #define VM86(S) (((S)->flags & HF_VM_MASK) != 0)
167 #define CODE32(S) (((S)->flags & HF_CS32_MASK) != 0)
168 #define SS32(S) (((S)->flags & HF_SS32_MASK) != 0)
169 #define ADDSEG(S) (((S)->flags & HF_ADDSEG_MASK) != 0)
171 #if !defined(TARGET_X86_64)
172 #define CODE64(S) false
174 #elif defined(CONFIG_USER_ONLY)
175 #define CODE64(S) true
178 #define CODE64(S) (((S)->flags & HF_CS64_MASK) != 0)
179 #define LMA(S) (((S)->flags & HF_LMA_MASK) != 0)
183 #define REX_PREFIX(S) (((S)->prefix & PREFIX_REX) != 0)
184 #define REX_W(S) ((S)->vex_w)
185 #define REX_R(S) ((S)->rex_r + 0)
186 #define REX_X(S) ((S)->rex_x + 0)
187 #define REX_B(S) ((S)->rex_b + 0)
189 #define REX_PREFIX(S) false
190 #define REX_W(S) false
197 * Many sysemu-only helpers are not reachable for user-only.
198 * Define stub generators here, so that we need not either sprinkle
199 * ifdefs through the translator, nor provide the helper function.
201 #define STUB_HELPER(NAME, ...) \
202 static inline void gen_helper_##NAME(__VA_ARGS__) \
203 { qemu_build_not_reached(); }
205 #ifdef CONFIG_USER_ONLY
206 STUB_HELPER(clgi
, TCGv_env env
)
207 STUB_HELPER(flush_page
, TCGv_env env
, TCGv addr
)
208 STUB_HELPER(hlt
, TCGv_env env
, TCGv_i32 pc_ofs
)
209 STUB_HELPER(inb
, TCGv ret
, TCGv_env env
, TCGv_i32 port
)
210 STUB_HELPER(inw
, TCGv ret
, TCGv_env env
, TCGv_i32 port
)
211 STUB_HELPER(inl
, TCGv ret
, TCGv_env env
, TCGv_i32 port
)
212 STUB_HELPER(monitor
, TCGv_env env
, TCGv addr
)
213 STUB_HELPER(mwait
, TCGv_env env
, TCGv_i32 pc_ofs
)
214 STUB_HELPER(outb
, TCGv_env env
, TCGv_i32 port
, TCGv_i32 val
)
215 STUB_HELPER(outw
, TCGv_env env
, TCGv_i32 port
, TCGv_i32 val
)
216 STUB_HELPER(outl
, TCGv_env env
, TCGv_i32 port
, TCGv_i32 val
)
217 STUB_HELPER(rdmsr
, TCGv_env env
)
218 STUB_HELPER(read_crN
, TCGv ret
, TCGv_env env
, TCGv_i32 reg
)
219 STUB_HELPER(get_dr
, TCGv ret
, TCGv_env env
, TCGv_i32 reg
)
220 STUB_HELPER(set_dr
, TCGv_env env
, TCGv_i32 reg
, TCGv val
)
221 STUB_HELPER(stgi
, TCGv_env env
)
222 STUB_HELPER(svm_check_intercept
, TCGv_env env
, TCGv_i32 type
)
223 STUB_HELPER(vmload
, TCGv_env env
, TCGv_i32 aflag
)
224 STUB_HELPER(vmmcall
, TCGv_env env
)
225 STUB_HELPER(vmrun
, TCGv_env env
, TCGv_i32 aflag
, TCGv_i32 pc_ofs
)
226 STUB_HELPER(vmsave
, TCGv_env env
, TCGv_i32 aflag
)
227 STUB_HELPER(write_crN
, TCGv_env env
, TCGv_i32 reg
, TCGv val
)
228 STUB_HELPER(wrmsr
, TCGv_env env
)
231 static void gen_eob(DisasContext
*s
);
232 static void gen_jr(DisasContext
*s
);
233 static void gen_jmp_rel(DisasContext
*s
, MemOp ot
, int diff
, int tb_num
);
234 static void gen_jmp_rel_csize(DisasContext
*s
, int diff
, int tb_num
);
235 static void gen_op(DisasContext
*s1
, int op
, MemOp ot
, int d
);
236 static void gen_exception_gpf(DisasContext
*s
);
238 /* i386 arith/logic operations */
258 OP_SHL1
, /* undocumented */
274 /* I386 int registers */
275 OR_EAX
, /* MUST be even numbered */
284 OR_TMP0
= 16, /* temporary operand register */
286 OR_A0
, /* temporary register used when doing address evaluation */
296 /* Bit set if the global variable is live after setting CC_OP to X. */
297 static const uint8_t cc_op_live
[CC_OP_NB
] = {
298 [CC_OP_DYNAMIC
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
299 [CC_OP_EFLAGS
] = USES_CC_SRC
,
300 [CC_OP_MULB
... CC_OP_MULQ
] = USES_CC_DST
| USES_CC_SRC
,
301 [CC_OP_ADDB
... CC_OP_ADDQ
] = USES_CC_DST
| USES_CC_SRC
,
302 [CC_OP_ADCB
... CC_OP_ADCQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
303 [CC_OP_SUBB
... CC_OP_SUBQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRCT
,
304 [CC_OP_SBBB
... CC_OP_SBBQ
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
305 [CC_OP_LOGICB
... CC_OP_LOGICQ
] = USES_CC_DST
,
306 [CC_OP_INCB
... CC_OP_INCQ
] = USES_CC_DST
| USES_CC_SRC
,
307 [CC_OP_DECB
... CC_OP_DECQ
] = USES_CC_DST
| USES_CC_SRC
,
308 [CC_OP_SHLB
... CC_OP_SHLQ
] = USES_CC_DST
| USES_CC_SRC
,
309 [CC_OP_SARB
... CC_OP_SARQ
] = USES_CC_DST
| USES_CC_SRC
,
310 [CC_OP_BMILGB
... CC_OP_BMILGQ
] = USES_CC_DST
| USES_CC_SRC
,
311 [CC_OP_ADCX
] = USES_CC_DST
| USES_CC_SRC
,
312 [CC_OP_ADOX
] = USES_CC_SRC
| USES_CC_SRC2
,
313 [CC_OP_ADCOX
] = USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
,
315 [CC_OP_POPCNT
] = USES_CC_SRC
,
318 static void set_cc_op(DisasContext
*s
, CCOp op
)
322 if (s
->cc_op
== op
) {
326 /* Discard CC computation that will no longer be used. */
327 dead
= cc_op_live
[s
->cc_op
] & ~cc_op_live
[op
];
328 if (dead
& USES_CC_DST
) {
329 tcg_gen_discard_tl(cpu_cc_dst
);
331 if (dead
& USES_CC_SRC
) {
332 tcg_gen_discard_tl(cpu_cc_src
);
334 if (dead
& USES_CC_SRC2
) {
335 tcg_gen_discard_tl(cpu_cc_src2
);
337 if (dead
& USES_CC_SRCT
) {
338 tcg_gen_discard_tl(s
->cc_srcT
);
341 if (op
== CC_OP_DYNAMIC
) {
342 /* The DYNAMIC setting is translator only, and should never be
343 stored. Thus we always consider it clean. */
344 s
->cc_op_dirty
= false;
346 /* Discard any computed CC_OP value (see shifts). */
347 if (s
->cc_op
== CC_OP_DYNAMIC
) {
348 tcg_gen_discard_i32(cpu_cc_op
);
350 s
->cc_op_dirty
= true;
355 static void gen_update_cc_op(DisasContext
*s
)
357 if (s
->cc_op_dirty
) {
358 tcg_gen_movi_i32(cpu_cc_op
, s
->cc_op
);
359 s
->cc_op_dirty
= false;
365 #define NB_OP_SIZES 4
367 #else /* !TARGET_X86_64 */
369 #define NB_OP_SIZES 3
371 #endif /* !TARGET_X86_64 */
374 #define REG_B_OFFSET (sizeof(target_ulong) - 1)
375 #define REG_H_OFFSET (sizeof(target_ulong) - 2)
376 #define REG_W_OFFSET (sizeof(target_ulong) - 2)
377 #define REG_L_OFFSET (sizeof(target_ulong) - 4)
378 #define REG_LH_OFFSET (sizeof(target_ulong) - 8)
380 #define REG_B_OFFSET 0
381 #define REG_H_OFFSET 1
382 #define REG_W_OFFSET 0
383 #define REG_L_OFFSET 0
384 #define REG_LH_OFFSET 4
387 /* In instruction encodings for byte register accesses the
388 * register number usually indicates "low 8 bits of register N";
389 * however there are some special cases where N 4..7 indicates
390 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
391 * true for this special case, false otherwise.
393 static inline bool byte_reg_is_xH(DisasContext
*s
, int reg
)
395 /* Any time the REX prefix is present, byte registers are uniform */
396 if (reg
< 4 || REX_PREFIX(s
)) {
402 /* Select the size of a push/pop operation. */
403 static inline MemOp
mo_pushpop(DisasContext
*s
, MemOp ot
)
406 return ot
== MO_16
? MO_16
: MO_64
;
412 /* Select the size of the stack pointer. */
413 static inline MemOp
mo_stacksize(DisasContext
*s
)
415 return CODE64(s
) ? MO_64
: SS32(s
) ? MO_32
: MO_16
;
418 /* Select only size 64 else 32. Used for SSE operand sizes. */
419 static inline MemOp
mo_64_32(MemOp ot
)
422 return ot
== MO_64
? MO_64
: MO_32
;
428 /* Select size 8 if lsb of B is clear, else OT. Used for decoding
429 byte vs word opcodes. */
430 static inline MemOp
mo_b_d(int b
, MemOp ot
)
432 return b
& 1 ? ot
: MO_8
;
435 /* Select size 8 if lsb of B is clear, else OT capped at 32.
436 Used for decoding operand size of port opcodes. */
437 static inline MemOp
mo_b_d32(int b
, MemOp ot
)
439 return b
& 1 ? (ot
== MO_16
? MO_16
: MO_32
) : MO_8
;
442 /* Compute the result of writing t0 to the OT-sized register REG.
444 * If DEST is NULL, store the result into the register and return the
447 * If DEST is not NULL, store the result into DEST and return the
450 static TCGv
gen_op_deposit_reg_v(DisasContext
*s
, MemOp ot
, int reg
, TCGv dest
, TCGv t0
)
454 if (byte_reg_is_xH(s
, reg
)) {
455 dest
= dest
? dest
: cpu_regs
[reg
- 4];
456 tcg_gen_deposit_tl(dest
, cpu_regs
[reg
- 4], t0
, 8, 8);
457 return cpu_regs
[reg
- 4];
459 dest
= dest
? dest
: cpu_regs
[reg
];
460 tcg_gen_deposit_tl(dest
, cpu_regs
[reg
], t0
, 0, 8);
463 dest
= dest
? dest
: cpu_regs
[reg
];
464 tcg_gen_deposit_tl(dest
, cpu_regs
[reg
], t0
, 0, 16);
467 /* For x86_64, this sets the higher half of register to zero.
468 For i386, this is equivalent to a mov. */
469 dest
= dest
? dest
: cpu_regs
[reg
];
470 tcg_gen_ext32u_tl(dest
, t0
);
474 dest
= dest
? dest
: cpu_regs
[reg
];
475 tcg_gen_mov_tl(dest
, t0
);
479 g_assert_not_reached();
481 return cpu_regs
[reg
];
484 static void gen_op_mov_reg_v(DisasContext
*s
, MemOp ot
, int reg
, TCGv t0
)
486 gen_op_deposit_reg_v(s
, ot
, reg
, NULL
, t0
);
490 void gen_op_mov_v_reg(DisasContext
*s
, MemOp ot
, TCGv t0
, int reg
)
492 if (ot
== MO_8
&& byte_reg_is_xH(s
, reg
)) {
493 tcg_gen_extract_tl(t0
, cpu_regs
[reg
- 4], 8, 8);
495 tcg_gen_mov_tl(t0
, cpu_regs
[reg
]);
499 static void gen_add_A0_im(DisasContext
*s
, int val
)
501 tcg_gen_addi_tl(s
->A0
, s
->A0
, val
);
503 tcg_gen_ext32u_tl(s
->A0
, s
->A0
);
507 static inline void gen_op_jmp_v(DisasContext
*s
, TCGv dest
)
509 tcg_gen_mov_tl(cpu_eip
, dest
);
514 void gen_op_add_reg_im(DisasContext
*s
, MemOp size
, int reg
, int32_t val
)
516 tcg_gen_addi_tl(s
->tmp0
, cpu_regs
[reg
], val
);
517 gen_op_mov_reg_v(s
, size
, reg
, s
->tmp0
);
520 static inline void gen_op_add_reg_T0(DisasContext
*s
, MemOp size
, int reg
)
522 tcg_gen_add_tl(s
->tmp0
, cpu_regs
[reg
], s
->T0
);
523 gen_op_mov_reg_v(s
, size
, reg
, s
->tmp0
);
526 static inline void gen_op_ld_v(DisasContext
*s
, int idx
, TCGv t0
, TCGv a0
)
528 tcg_gen_qemu_ld_tl(t0
, a0
, s
->mem_index
, idx
| MO_LE
);
531 static inline void gen_op_st_v(DisasContext
*s
, int idx
, TCGv t0
, TCGv a0
)
533 tcg_gen_qemu_st_tl(t0
, a0
, s
->mem_index
, idx
| MO_LE
);
536 static inline void gen_op_st_rm_T0_A0(DisasContext
*s
, int idx
, int d
)
539 gen_op_st_v(s
, idx
, s
->T0
, s
->A0
);
541 gen_op_mov_reg_v(s
, idx
, d
, s
->T0
);
545 static void gen_update_eip_cur(DisasContext
*s
)
547 assert(s
->pc_save
!= -1);
548 if (tb_cflags(s
->base
.tb
) & CF_PCREL
) {
549 tcg_gen_addi_tl(cpu_eip
, cpu_eip
, s
->base
.pc_next
- s
->pc_save
);
551 tcg_gen_movi_tl(cpu_eip
, s
->base
.pc_next
- s
->cs_base
);
553 s
->pc_save
= s
->base
.pc_next
;
556 static void gen_update_eip_next(DisasContext
*s
)
558 assert(s
->pc_save
!= -1);
559 if (tb_cflags(s
->base
.tb
) & CF_PCREL
) {
560 tcg_gen_addi_tl(cpu_eip
, cpu_eip
, s
->pc
- s
->pc_save
);
562 tcg_gen_movi_tl(cpu_eip
, s
->pc
- s
->cs_base
);
567 static int cur_insn_len(DisasContext
*s
)
569 return s
->pc
- s
->base
.pc_next
;
572 static TCGv_i32
cur_insn_len_i32(DisasContext
*s
)
574 return tcg_constant_i32(cur_insn_len(s
));
577 static TCGv_i32
eip_next_i32(DisasContext
*s
)
579 assert(s
->pc_save
!= -1);
581 * This function has two users: lcall_real (always 16-bit mode), and
582 * iret_protected (16, 32, or 64-bit mode). IRET only uses the value
583 * when EFLAGS.NT is set, which is illegal in 64-bit mode, which is
584 * why passing a 32-bit value isn't broken. To avoid using this where
585 * we shouldn't, return -1 in 64-bit mode so that execution goes into
589 return tcg_constant_i32(-1);
591 if (tb_cflags(s
->base
.tb
) & CF_PCREL
) {
592 TCGv_i32 ret
= tcg_temp_new_i32();
593 tcg_gen_trunc_tl_i32(ret
, cpu_eip
);
594 tcg_gen_addi_i32(ret
, ret
, s
->pc
- s
->pc_save
);
597 return tcg_constant_i32(s
->pc
- s
->cs_base
);
601 static TCGv
eip_next_tl(DisasContext
*s
)
603 assert(s
->pc_save
!= -1);
604 if (tb_cflags(s
->base
.tb
) & CF_PCREL
) {
605 TCGv ret
= tcg_temp_new();
606 tcg_gen_addi_tl(ret
, cpu_eip
, s
->pc
- s
->pc_save
);
609 return tcg_constant_tl(s
->pc
- s
->cs_base
);
613 static TCGv
eip_cur_tl(DisasContext
*s
)
615 assert(s
->pc_save
!= -1);
616 if (tb_cflags(s
->base
.tb
) & CF_PCREL
) {
617 TCGv ret
= tcg_temp_new();
618 tcg_gen_addi_tl(ret
, cpu_eip
, s
->base
.pc_next
- s
->pc_save
);
621 return tcg_constant_tl(s
->base
.pc_next
- s
->cs_base
);
625 /* Compute SEG:REG into A0. SEG is selected from the override segment
626 (OVR_SEG) and the default segment (DEF_SEG). OVR_SEG may be -1 to
627 indicate no override. */
628 static void gen_lea_v_seg(DisasContext
*s
, MemOp aflag
, TCGv a0
,
629 int def_seg
, int ovr_seg
)
635 tcg_gen_mov_tl(s
->A0
, a0
);
642 if (ovr_seg
< 0 && ADDSEG(s
)) {
646 tcg_gen_ext32u_tl(s
->A0
, a0
);
652 tcg_gen_ext16u_tl(s
->A0
, a0
);
663 g_assert_not_reached();
667 TCGv seg
= cpu_seg_base
[ovr_seg
];
669 if (aflag
== MO_64
) {
670 tcg_gen_add_tl(s
->A0
, a0
, seg
);
671 } else if (CODE64(s
)) {
672 tcg_gen_ext32u_tl(s
->A0
, a0
);
673 tcg_gen_add_tl(s
->A0
, s
->A0
, seg
);
675 tcg_gen_add_tl(s
->A0
, a0
, seg
);
676 tcg_gen_ext32u_tl(s
->A0
, s
->A0
);
681 static inline void gen_string_movl_A0_ESI(DisasContext
*s
)
683 gen_lea_v_seg(s
, s
->aflag
, cpu_regs
[R_ESI
], R_DS
, s
->override
);
686 static inline void gen_string_movl_A0_EDI(DisasContext
*s
)
688 gen_lea_v_seg(s
, s
->aflag
, cpu_regs
[R_EDI
], R_ES
, -1);
691 static inline void gen_op_movl_T0_Dshift(DisasContext
*s
, MemOp ot
)
693 tcg_gen_ld32s_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, df
));
694 tcg_gen_shli_tl(s
->T0
, s
->T0
, ot
);
697 static TCGv
gen_ext_tl(TCGv dst
, TCGv src
, MemOp size
, bool sign
)
702 tcg_gen_ext8s_tl(dst
, src
);
704 tcg_gen_ext8u_tl(dst
, src
);
709 tcg_gen_ext16s_tl(dst
, src
);
711 tcg_gen_ext16u_tl(dst
, src
);
717 tcg_gen_ext32s_tl(dst
, src
);
719 tcg_gen_ext32u_tl(dst
, src
);
728 static void gen_extu(MemOp ot
, TCGv reg
)
730 gen_ext_tl(reg
, reg
, ot
, false);
733 static void gen_exts(MemOp ot
, TCGv reg
)
735 gen_ext_tl(reg
, reg
, ot
, true);
738 static void gen_op_j_ecx(DisasContext
*s
, TCGCond cond
, TCGLabel
*label1
)
740 tcg_gen_mov_tl(s
->tmp0
, cpu_regs
[R_ECX
]);
741 gen_extu(s
->aflag
, s
->tmp0
);
742 tcg_gen_brcondi_tl(cond
, s
->tmp0
, 0, label1
);
745 static inline void gen_op_jz_ecx(DisasContext
*s
, TCGLabel
*label1
)
747 gen_op_j_ecx(s
, TCG_COND_EQ
, label1
);
750 static inline void gen_op_jnz_ecx(DisasContext
*s
, TCGLabel
*label1
)
752 gen_op_j_ecx(s
, TCG_COND_NE
, label1
);
755 static void gen_helper_in_func(MemOp ot
, TCGv v
, TCGv_i32 n
)
759 gen_helper_inb(v
, cpu_env
, n
);
762 gen_helper_inw(v
, cpu_env
, n
);
765 gen_helper_inl(v
, cpu_env
, n
);
768 g_assert_not_reached();
772 static void gen_helper_out_func(MemOp ot
, TCGv_i32 v
, TCGv_i32 n
)
776 gen_helper_outb(cpu_env
, v
, n
);
779 gen_helper_outw(cpu_env
, v
, n
);
782 gen_helper_outl(cpu_env
, v
, n
);
785 g_assert_not_reached();
790 * Validate that access to [port, port + 1<<ot) is allowed.
791 * Raise #GP, or VMM exit if not.
793 static bool gen_check_io(DisasContext
*s
, MemOp ot
, TCGv_i32 port
,
796 #ifdef CONFIG_USER_ONLY
798 * We do not implement the ioperm(2) syscall, so the TSS check
801 gen_exception_gpf(s
);
804 if (PE(s
) && (CPL(s
) > IOPL(s
) || VM86(s
))) {
805 gen_helper_check_io(cpu_env
, port
, tcg_constant_i32(1 << ot
));
809 gen_update_eip_cur(s
);
810 if (s
->prefix
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
811 svm_flags
|= SVM_IOIO_REP_MASK
;
813 svm_flags
|= 1 << (SVM_IOIO_SIZE_SHIFT
+ ot
);
814 gen_helper_svm_check_io(cpu_env
, port
,
815 tcg_constant_i32(svm_flags
),
816 cur_insn_len_i32(s
));
822 static void gen_movs(DisasContext
*s
, MemOp ot
)
824 gen_string_movl_A0_ESI(s
);
825 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
826 gen_string_movl_A0_EDI(s
);
827 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
828 gen_op_movl_T0_Dshift(s
, ot
);
829 gen_op_add_reg_T0(s
, s
->aflag
, R_ESI
);
830 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
833 static void gen_op_update1_cc(DisasContext
*s
)
835 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
838 static void gen_op_update2_cc(DisasContext
*s
)
840 tcg_gen_mov_tl(cpu_cc_src
, s
->T1
);
841 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
844 static void gen_op_update3_cc(DisasContext
*s
, TCGv reg
)
846 tcg_gen_mov_tl(cpu_cc_src2
, reg
);
847 tcg_gen_mov_tl(cpu_cc_src
, s
->T1
);
848 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
851 static inline void gen_op_testl_T0_T1_cc(DisasContext
*s
)
853 tcg_gen_and_tl(cpu_cc_dst
, s
->T0
, s
->T1
);
856 static void gen_op_update_neg_cc(DisasContext
*s
)
858 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
859 tcg_gen_neg_tl(cpu_cc_src
, s
->T0
);
860 tcg_gen_movi_tl(s
->cc_srcT
, 0);
863 /* compute all eflags to cc_src */
864 static void gen_compute_eflags(DisasContext
*s
)
866 TCGv zero
, dst
, src1
, src2
;
869 if (s
->cc_op
== CC_OP_EFLAGS
) {
872 if (s
->cc_op
== CC_OP_CLR
) {
873 tcg_gen_movi_tl(cpu_cc_src
, CC_Z
| CC_P
);
874 set_cc_op(s
, CC_OP_EFLAGS
);
883 /* Take care to not read values that are not live. */
884 live
= cc_op_live
[s
->cc_op
] & ~USES_CC_SRCT
;
885 dead
= live
^ (USES_CC_DST
| USES_CC_SRC
| USES_CC_SRC2
);
887 zero
= tcg_constant_tl(0);
888 if (dead
& USES_CC_DST
) {
891 if (dead
& USES_CC_SRC
) {
894 if (dead
& USES_CC_SRC2
) {
900 gen_helper_cc_compute_all(cpu_cc_src
, dst
, src1
, src2
, cpu_cc_op
);
901 set_cc_op(s
, CC_OP_EFLAGS
);
904 typedef struct CCPrepare
{
914 /* compute eflags.C to reg */
915 static CCPrepare
gen_prepare_eflags_c(DisasContext
*s
, TCGv reg
)
921 case CC_OP_SUBB
... CC_OP_SUBQ
:
922 /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
923 size
= s
->cc_op
- CC_OP_SUBB
;
924 t1
= gen_ext_tl(s
->tmp0
, cpu_cc_src
, size
, false);
925 /* If no temporary was used, be careful not to alias t1 and t0. */
926 t0
= t1
== cpu_cc_src
? s
->tmp0
: reg
;
927 tcg_gen_mov_tl(t0
, s
->cc_srcT
);
931 case CC_OP_ADDB
... CC_OP_ADDQ
:
932 /* (DATA_TYPE)CC_DST < (DATA_TYPE)CC_SRC */
933 size
= s
->cc_op
- CC_OP_ADDB
;
934 t1
= gen_ext_tl(s
->tmp0
, cpu_cc_src
, size
, false);
935 t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, false);
937 return (CCPrepare
) { .cond
= TCG_COND_LTU
, .reg
= t0
,
938 .reg2
= t1
, .mask
= -1, .use_reg2
= true };
940 case CC_OP_LOGICB
... CC_OP_LOGICQ
:
943 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
945 case CC_OP_INCB
... CC_OP_INCQ
:
946 case CC_OP_DECB
... CC_OP_DECQ
:
947 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
948 .mask
= -1, .no_setcond
= true };
950 case CC_OP_SHLB
... CC_OP_SHLQ
:
951 /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
952 size
= s
->cc_op
- CC_OP_SHLB
;
953 shift
= (8 << size
) - 1;
954 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
955 .mask
= (target_ulong
)1 << shift
};
957 case CC_OP_MULB
... CC_OP_MULQ
:
958 return (CCPrepare
) { .cond
= TCG_COND_NE
,
959 .reg
= cpu_cc_src
, .mask
= -1 };
961 case CC_OP_BMILGB
... CC_OP_BMILGQ
:
962 size
= s
->cc_op
- CC_OP_BMILGB
;
963 t0
= gen_ext_tl(reg
, cpu_cc_src
, size
, false);
964 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= t0
, .mask
= -1 };
968 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_dst
,
969 .mask
= -1, .no_setcond
= true };
972 case CC_OP_SARB
... CC_OP_SARQ
:
974 return (CCPrepare
) { .cond
= TCG_COND_NE
,
975 .reg
= cpu_cc_src
, .mask
= CC_C
};
978 /* The need to compute only C from CC_OP_DYNAMIC is important
979 in efficiently implementing e.g. INC at the start of a TB. */
981 gen_helper_cc_compute_c(reg
, cpu_cc_dst
, cpu_cc_src
,
982 cpu_cc_src2
, cpu_cc_op
);
983 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
984 .mask
= -1, .no_setcond
= true };
988 /* compute eflags.P to reg */
989 static CCPrepare
gen_prepare_eflags_p(DisasContext
*s
, TCGv reg
)
991 gen_compute_eflags(s
);
992 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
996 /* compute eflags.S to reg */
997 static CCPrepare
gen_prepare_eflags_s(DisasContext
*s
, TCGv reg
)
1001 gen_compute_eflags(s
);
1007 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
1011 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
1014 MemOp size
= (s
->cc_op
- CC_OP_ADDB
) & 3;
1015 TCGv t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, true);
1016 return (CCPrepare
) { .cond
= TCG_COND_LT
, .reg
= t0
, .mask
= -1 };
1021 /* compute eflags.O to reg */
1022 static CCPrepare
gen_prepare_eflags_o(DisasContext
*s
, TCGv reg
)
1027 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src2
,
1028 .mask
= -1, .no_setcond
= true };
1031 return (CCPrepare
) { .cond
= TCG_COND_NEVER
, .mask
= -1 };
1033 gen_compute_eflags(s
);
1034 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
1039 /* compute eflags.Z to reg */
1040 static CCPrepare
gen_prepare_eflags_z(DisasContext
*s
, TCGv reg
)
1044 gen_compute_eflags(s
);
1050 return (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
1053 return (CCPrepare
) { .cond
= TCG_COND_ALWAYS
, .mask
= -1 };
1055 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= cpu_cc_src
,
1059 MemOp size
= (s
->cc_op
- CC_OP_ADDB
) & 3;
1060 TCGv t0
= gen_ext_tl(reg
, cpu_cc_dst
, size
, false);
1061 return (CCPrepare
) { .cond
= TCG_COND_EQ
, .reg
= t0
, .mask
= -1 };
1066 /* perform a conditional store into register 'reg' according to jump opcode
1067 value 'b'. In the fast case, T0 is guaranted not to be used. */
1068 static CCPrepare
gen_prepare_cc(DisasContext
*s
, int b
, TCGv reg
)
1070 int inv
, jcc_op
, cond
;
1076 jcc_op
= (b
>> 1) & 7;
1079 case CC_OP_SUBB
... CC_OP_SUBQ
:
1080 /* We optimize relational operators for the cmp/jcc case. */
1081 size
= s
->cc_op
- CC_OP_SUBB
;
1084 tcg_gen_mov_tl(s
->tmp4
, s
->cc_srcT
);
1085 gen_extu(size
, s
->tmp4
);
1086 t0
= gen_ext_tl(s
->tmp0
, cpu_cc_src
, size
, false);
1087 cc
= (CCPrepare
) { .cond
= TCG_COND_LEU
, .reg
= s
->tmp4
,
1088 .reg2
= t0
, .mask
= -1, .use_reg2
= true };
1097 tcg_gen_mov_tl(s
->tmp4
, s
->cc_srcT
);
1098 gen_exts(size
, s
->tmp4
);
1099 t0
= gen_ext_tl(s
->tmp0
, cpu_cc_src
, size
, true);
1100 cc
= (CCPrepare
) { .cond
= cond
, .reg
= s
->tmp4
,
1101 .reg2
= t0
, .mask
= -1, .use_reg2
= true };
1111 /* This actually generates good code for JC, JZ and JS. */
1114 cc
= gen_prepare_eflags_o(s
, reg
);
1117 cc
= gen_prepare_eflags_c(s
, reg
);
1120 cc
= gen_prepare_eflags_z(s
, reg
);
1123 gen_compute_eflags(s
);
1124 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= cpu_cc_src
,
1125 .mask
= CC_Z
| CC_C
};
1128 cc
= gen_prepare_eflags_s(s
, reg
);
1131 cc
= gen_prepare_eflags_p(s
, reg
);
1134 gen_compute_eflags(s
);
1135 if (reg
== cpu_cc_src
) {
1138 tcg_gen_shri_tl(reg
, cpu_cc_src
, 4); /* CC_O -> CC_S */
1139 tcg_gen_xor_tl(reg
, reg
, cpu_cc_src
);
1140 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
1145 gen_compute_eflags(s
);
1146 if (reg
== cpu_cc_src
) {
1149 tcg_gen_shri_tl(reg
, cpu_cc_src
, 4); /* CC_O -> CC_S */
1150 tcg_gen_xor_tl(reg
, reg
, cpu_cc_src
);
1151 cc
= (CCPrepare
) { .cond
= TCG_COND_NE
, .reg
= reg
,
1152 .mask
= CC_S
| CC_Z
};
1159 cc
.cond
= tcg_invert_cond(cc
.cond
);
1164 static void gen_setcc1(DisasContext
*s
, int b
, TCGv reg
)
1166 CCPrepare cc
= gen_prepare_cc(s
, b
, reg
);
1168 if (cc
.no_setcond
) {
1169 if (cc
.cond
== TCG_COND_EQ
) {
1170 tcg_gen_xori_tl(reg
, cc
.reg
, 1);
1172 tcg_gen_mov_tl(reg
, cc
.reg
);
1177 if (cc
.cond
== TCG_COND_NE
&& !cc
.use_reg2
&& cc
.imm
== 0 &&
1178 cc
.mask
!= 0 && (cc
.mask
& (cc
.mask
- 1)) == 0) {
1179 tcg_gen_shri_tl(reg
, cc
.reg
, ctztl(cc
.mask
));
1180 tcg_gen_andi_tl(reg
, reg
, 1);
1183 if (cc
.mask
!= -1) {
1184 tcg_gen_andi_tl(reg
, cc
.reg
, cc
.mask
);
1188 tcg_gen_setcond_tl(cc
.cond
, reg
, cc
.reg
, cc
.reg2
);
1190 tcg_gen_setcondi_tl(cc
.cond
, reg
, cc
.reg
, cc
.imm
);
1194 static inline void gen_compute_eflags_c(DisasContext
*s
, TCGv reg
)
1196 gen_setcc1(s
, JCC_B
<< 1, reg
);
1199 /* generate a conditional jump to label 'l1' according to jump opcode
1200 value 'b'. In the fast case, T0 is guaranted not to be used. */
1201 static inline void gen_jcc1_noeob(DisasContext
*s
, int b
, TCGLabel
*l1
)
1203 CCPrepare cc
= gen_prepare_cc(s
, b
, s
->T0
);
1205 if (cc
.mask
!= -1) {
1206 tcg_gen_andi_tl(s
->T0
, cc
.reg
, cc
.mask
);
1210 tcg_gen_brcond_tl(cc
.cond
, cc
.reg
, cc
.reg2
, l1
);
1212 tcg_gen_brcondi_tl(cc
.cond
, cc
.reg
, cc
.imm
, l1
);
1216 /* Generate a conditional jump to label 'l1' according to jump opcode
1217 value 'b'. In the fast case, T0 is guaranted not to be used.
1218 A translation block must end soon. */
1219 static inline void gen_jcc1(DisasContext
*s
, int b
, TCGLabel
*l1
)
1221 CCPrepare cc
= gen_prepare_cc(s
, b
, s
->T0
);
1223 gen_update_cc_op(s
);
1224 if (cc
.mask
!= -1) {
1225 tcg_gen_andi_tl(s
->T0
, cc
.reg
, cc
.mask
);
1228 set_cc_op(s
, CC_OP_DYNAMIC
);
1230 tcg_gen_brcond_tl(cc
.cond
, cc
.reg
, cc
.reg2
, l1
);
1232 tcg_gen_brcondi_tl(cc
.cond
, cc
.reg
, cc
.imm
, l1
);
1236 /* XXX: does not work with gdbstub "ice" single step - not a
1238 static TCGLabel
*gen_jz_ecx_string(DisasContext
*s
)
1240 TCGLabel
*l1
= gen_new_label();
1241 TCGLabel
*l2
= gen_new_label();
1242 gen_op_jnz_ecx(s
, l1
);
1244 gen_jmp_rel_csize(s
, 0, 1);
1249 static void gen_stos(DisasContext
*s
, MemOp ot
)
1251 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, R_EAX
);
1252 gen_string_movl_A0_EDI(s
);
1253 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
1254 gen_op_movl_T0_Dshift(s
, ot
);
1255 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
1258 static void gen_lods(DisasContext
*s
, MemOp ot
)
1260 gen_string_movl_A0_ESI(s
);
1261 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1262 gen_op_mov_reg_v(s
, ot
, R_EAX
, s
->T0
);
1263 gen_op_movl_T0_Dshift(s
, ot
);
1264 gen_op_add_reg_T0(s
, s
->aflag
, R_ESI
);
1267 static void gen_scas(DisasContext
*s
, MemOp ot
)
1269 gen_string_movl_A0_EDI(s
);
1270 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
1271 gen_op(s
, OP_CMPL
, ot
, R_EAX
);
1272 gen_op_movl_T0_Dshift(s
, ot
);
1273 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
1276 static void gen_cmps(DisasContext
*s
, MemOp ot
)
1278 gen_string_movl_A0_EDI(s
);
1279 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
1280 gen_string_movl_A0_ESI(s
);
1281 gen_op(s
, OP_CMPL
, ot
, OR_TMP0
);
1282 gen_op_movl_T0_Dshift(s
, ot
);
1283 gen_op_add_reg_T0(s
, s
->aflag
, R_ESI
);
1284 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
1287 static void gen_bpt_io(DisasContext
*s
, TCGv_i32 t_port
, int ot
)
1289 if (s
->flags
& HF_IOBPT_MASK
) {
1290 #ifdef CONFIG_USER_ONLY
1291 /* user-mode cpu should not be in IOBPT mode */
1292 g_assert_not_reached();
1294 TCGv_i32 t_size
= tcg_constant_i32(1 << ot
);
1295 TCGv t_next
= eip_next_tl(s
);
1296 gen_helper_bpt_io(cpu_env
, t_port
, t_size
, t_next
);
1297 #endif /* CONFIG_USER_ONLY */
1301 static void gen_ins(DisasContext
*s
, MemOp ot
)
1303 gen_string_movl_A0_EDI(s
);
1304 /* Note: we must do this dummy write first to be restartable in
1305 case of page fault. */
1306 tcg_gen_movi_tl(s
->T0
, 0);
1307 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
1308 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_EDX
]);
1309 tcg_gen_andi_i32(s
->tmp2_i32
, s
->tmp2_i32
, 0xffff);
1310 gen_helper_in_func(ot
, s
->T0
, s
->tmp2_i32
);
1311 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
1312 gen_op_movl_T0_Dshift(s
, ot
);
1313 gen_op_add_reg_T0(s
, s
->aflag
, R_EDI
);
1314 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
1317 static void gen_outs(DisasContext
*s
, MemOp ot
)
1319 gen_string_movl_A0_ESI(s
);
1320 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1322 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_EDX
]);
1323 tcg_gen_andi_i32(s
->tmp2_i32
, s
->tmp2_i32
, 0xffff);
1324 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T0
);
1325 gen_helper_out_func(ot
, s
->tmp2_i32
, s
->tmp3_i32
);
1326 gen_op_movl_T0_Dshift(s
, ot
);
1327 gen_op_add_reg_T0(s
, s
->aflag
, R_ESI
);
1328 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
1331 /* Generate jumps to current or next instruction */
1332 static void gen_repz(DisasContext
*s
, MemOp ot
,
1333 void (*fn
)(DisasContext
*s
, MemOp ot
))
1336 gen_update_cc_op(s
);
1337 l2
= gen_jz_ecx_string(s
);
1339 gen_op_add_reg_im(s
, s
->aflag
, R_ECX
, -1);
1341 * A loop would cause two single step exceptions if ECX = 1
1342 * before rep string_insn
1345 gen_op_jz_ecx(s
, l2
);
1347 gen_jmp_rel_csize(s
, -cur_insn_len(s
), 0);
1350 #define GEN_REPZ(op) \
1351 static inline void gen_repz_ ## op(DisasContext *s, MemOp ot) \
1352 { gen_repz(s, ot, gen_##op); }
1354 static void gen_repz2(DisasContext
*s
, MemOp ot
, int nz
,
1355 void (*fn
)(DisasContext
*s
, MemOp ot
))
1358 gen_update_cc_op(s
);
1359 l2
= gen_jz_ecx_string(s
);
1361 gen_op_add_reg_im(s
, s
->aflag
, R_ECX
, -1);
1362 gen_update_cc_op(s
);
1363 gen_jcc1(s
, (JCC_Z
<< 1) | (nz
^ 1), l2
);
1365 gen_op_jz_ecx(s
, l2
);
1367 gen_jmp_rel_csize(s
, -cur_insn_len(s
), 0);
1370 #define GEN_REPZ2(op) \
1371 static inline void gen_repz_ ## op(DisasContext *s, MemOp ot, int nz) \
1372 { gen_repz2(s, ot, nz, gen_##op); }
1382 static void gen_helper_fp_arith_ST0_FT0(int op
)
1386 gen_helper_fadd_ST0_FT0(cpu_env
);
1389 gen_helper_fmul_ST0_FT0(cpu_env
);
1392 gen_helper_fcom_ST0_FT0(cpu_env
);
1395 gen_helper_fcom_ST0_FT0(cpu_env
);
1398 gen_helper_fsub_ST0_FT0(cpu_env
);
1401 gen_helper_fsubr_ST0_FT0(cpu_env
);
1404 gen_helper_fdiv_ST0_FT0(cpu_env
);
1407 gen_helper_fdivr_ST0_FT0(cpu_env
);
1412 /* NOTE the exception in "r" op ordering */
1413 static void gen_helper_fp_arith_STN_ST0(int op
, int opreg
)
1415 TCGv_i32 tmp
= tcg_constant_i32(opreg
);
1418 gen_helper_fadd_STN_ST0(cpu_env
, tmp
);
1421 gen_helper_fmul_STN_ST0(cpu_env
, tmp
);
1424 gen_helper_fsubr_STN_ST0(cpu_env
, tmp
);
1427 gen_helper_fsub_STN_ST0(cpu_env
, tmp
);
1430 gen_helper_fdivr_STN_ST0(cpu_env
, tmp
);
1433 gen_helper_fdiv_STN_ST0(cpu_env
, tmp
);
1438 static void gen_exception(DisasContext
*s
, int trapno
)
1440 gen_update_cc_op(s
);
1441 gen_update_eip_cur(s
);
1442 gen_helper_raise_exception(cpu_env
, tcg_constant_i32(trapno
));
1443 s
->base
.is_jmp
= DISAS_NORETURN
;
1446 /* Generate #UD for the current instruction. The assumption here is that
1447 the instruction is known, but it isn't allowed in the current cpu mode. */
1448 static void gen_illegal_opcode(DisasContext
*s
)
1450 gen_exception(s
, EXCP06_ILLOP
);
1453 /* Generate #GP for the current instruction. */
1454 static void gen_exception_gpf(DisasContext
*s
)
1456 gen_exception(s
, EXCP0D_GPF
);
1459 /* Check for cpl == 0; if not, raise #GP and return false. */
1460 static bool check_cpl0(DisasContext
*s
)
1465 gen_exception_gpf(s
);
1469 /* If vm86, check for iopl == 3; if not, raise #GP and return false. */
1470 static bool check_vm86_iopl(DisasContext
*s
)
1472 if (!VM86(s
) || IOPL(s
) == 3) {
1475 gen_exception_gpf(s
);
1479 /* Check for iopl allowing access; if not, raise #GP and return false. */
1480 static bool check_iopl(DisasContext
*s
)
1482 if (VM86(s
) ? IOPL(s
) == 3 : CPL(s
) <= IOPL(s
)) {
1485 gen_exception_gpf(s
);
1489 /* if d == OR_TMP0, it means memory operand (address in A0) */
1490 static void gen_op(DisasContext
*s1
, int op
, MemOp ot
, int d
)
1493 if (s1
->prefix
& PREFIX_LOCK
) {
1494 /* Lock prefix when destination is not memory. */
1495 gen_illegal_opcode(s1
);
1498 gen_op_mov_v_reg(s1
, ot
, s1
->T0
, d
);
1499 } else if (!(s1
->prefix
& PREFIX_LOCK
)) {
1500 gen_op_ld_v(s1
, ot
, s1
->T0
, s1
->A0
);
1504 gen_compute_eflags_c(s1
, s1
->tmp4
);
1505 if (s1
->prefix
& PREFIX_LOCK
) {
1506 tcg_gen_add_tl(s1
->T0
, s1
->tmp4
, s1
->T1
);
1507 tcg_gen_atomic_add_fetch_tl(s1
->T0
, s1
->A0
, s1
->T0
,
1508 s1
->mem_index
, ot
| MO_LE
);
1510 tcg_gen_add_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1511 tcg_gen_add_tl(s1
->T0
, s1
->T0
, s1
->tmp4
);
1512 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1514 gen_op_update3_cc(s1
, s1
->tmp4
);
1515 set_cc_op(s1
, CC_OP_ADCB
+ ot
);
1518 gen_compute_eflags_c(s1
, s1
->tmp4
);
1519 if (s1
->prefix
& PREFIX_LOCK
) {
1520 tcg_gen_add_tl(s1
->T0
, s1
->T1
, s1
->tmp4
);
1521 tcg_gen_neg_tl(s1
->T0
, s1
->T0
);
1522 tcg_gen_atomic_add_fetch_tl(s1
->T0
, s1
->A0
, s1
->T0
,
1523 s1
->mem_index
, ot
| MO_LE
);
1525 tcg_gen_sub_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1526 tcg_gen_sub_tl(s1
->T0
, s1
->T0
, s1
->tmp4
);
1527 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1529 gen_op_update3_cc(s1
, s1
->tmp4
);
1530 set_cc_op(s1
, CC_OP_SBBB
+ ot
);
1533 if (s1
->prefix
& PREFIX_LOCK
) {
1534 tcg_gen_atomic_add_fetch_tl(s1
->T0
, s1
->A0
, s1
->T1
,
1535 s1
->mem_index
, ot
| MO_LE
);
1537 tcg_gen_add_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1538 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1540 gen_op_update2_cc(s1
);
1541 set_cc_op(s1
, CC_OP_ADDB
+ ot
);
1544 if (s1
->prefix
& PREFIX_LOCK
) {
1545 tcg_gen_neg_tl(s1
->T0
, s1
->T1
);
1546 tcg_gen_atomic_fetch_add_tl(s1
->cc_srcT
, s1
->A0
, s1
->T0
,
1547 s1
->mem_index
, ot
| MO_LE
);
1548 tcg_gen_sub_tl(s1
->T0
, s1
->cc_srcT
, s1
->T1
);
1550 tcg_gen_mov_tl(s1
->cc_srcT
, s1
->T0
);
1551 tcg_gen_sub_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1552 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1554 gen_op_update2_cc(s1
);
1555 set_cc_op(s1
, CC_OP_SUBB
+ ot
);
1559 if (s1
->prefix
& PREFIX_LOCK
) {
1560 tcg_gen_atomic_and_fetch_tl(s1
->T0
, s1
->A0
, s1
->T1
,
1561 s1
->mem_index
, ot
| MO_LE
);
1563 tcg_gen_and_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1564 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1566 gen_op_update1_cc(s1
);
1567 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1570 if (s1
->prefix
& PREFIX_LOCK
) {
1571 tcg_gen_atomic_or_fetch_tl(s1
->T0
, s1
->A0
, s1
->T1
,
1572 s1
->mem_index
, ot
| MO_LE
);
1574 tcg_gen_or_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1575 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1577 gen_op_update1_cc(s1
);
1578 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1581 if (s1
->prefix
& PREFIX_LOCK
) {
1582 tcg_gen_atomic_xor_fetch_tl(s1
->T0
, s1
->A0
, s1
->T1
,
1583 s1
->mem_index
, ot
| MO_LE
);
1585 tcg_gen_xor_tl(s1
->T0
, s1
->T0
, s1
->T1
);
1586 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1588 gen_op_update1_cc(s1
);
1589 set_cc_op(s1
, CC_OP_LOGICB
+ ot
);
1592 tcg_gen_mov_tl(cpu_cc_src
, s1
->T1
);
1593 tcg_gen_mov_tl(s1
->cc_srcT
, s1
->T0
);
1594 tcg_gen_sub_tl(cpu_cc_dst
, s1
->T0
, s1
->T1
);
1595 set_cc_op(s1
, CC_OP_SUBB
+ ot
);
1600 /* if d == OR_TMP0, it means memory operand (address in A0) */
1601 static void gen_inc(DisasContext
*s1
, MemOp ot
, int d
, int c
)
1603 if (s1
->prefix
& PREFIX_LOCK
) {
1605 /* Lock prefix when destination is not memory */
1606 gen_illegal_opcode(s1
);
1609 tcg_gen_movi_tl(s1
->T0
, c
> 0 ? 1 : -1);
1610 tcg_gen_atomic_add_fetch_tl(s1
->T0
, s1
->A0
, s1
->T0
,
1611 s1
->mem_index
, ot
| MO_LE
);
1614 gen_op_mov_v_reg(s1
, ot
, s1
->T0
, d
);
1616 gen_op_ld_v(s1
, ot
, s1
->T0
, s1
->A0
);
1618 tcg_gen_addi_tl(s1
->T0
, s1
->T0
, (c
> 0 ? 1 : -1));
1619 gen_op_st_rm_T0_A0(s1
, ot
, d
);
1622 gen_compute_eflags_c(s1
, cpu_cc_src
);
1623 tcg_gen_mov_tl(cpu_cc_dst
, s1
->T0
);
1624 set_cc_op(s1
, (c
> 0 ? CC_OP_INCB
: CC_OP_DECB
) + ot
);
1627 static void gen_shift_flags(DisasContext
*s
, MemOp ot
, TCGv result
,
1628 TCGv shm1
, TCGv count
, bool is_right
)
1630 TCGv_i32 z32
, s32
, oldop
;
1633 /* Store the results into the CC variables. If we know that the
1634 variable must be dead, store unconditionally. Otherwise we'll
1635 need to not disrupt the current contents. */
1636 z_tl
= tcg_constant_tl(0);
1637 if (cc_op_live
[s
->cc_op
] & USES_CC_DST
) {
1638 tcg_gen_movcond_tl(TCG_COND_NE
, cpu_cc_dst
, count
, z_tl
,
1639 result
, cpu_cc_dst
);
1641 tcg_gen_mov_tl(cpu_cc_dst
, result
);
1643 if (cc_op_live
[s
->cc_op
] & USES_CC_SRC
) {
1644 tcg_gen_movcond_tl(TCG_COND_NE
, cpu_cc_src
, count
, z_tl
,
1647 tcg_gen_mov_tl(cpu_cc_src
, shm1
);
1650 /* Get the two potential CC_OP values into temporaries. */
1651 tcg_gen_movi_i32(s
->tmp2_i32
, (is_right
? CC_OP_SARB
: CC_OP_SHLB
) + ot
);
1652 if (s
->cc_op
== CC_OP_DYNAMIC
) {
1655 tcg_gen_movi_i32(s
->tmp3_i32
, s
->cc_op
);
1656 oldop
= s
->tmp3_i32
;
1659 /* Conditionally store the CC_OP value. */
1660 z32
= tcg_constant_i32(0);
1661 s32
= tcg_temp_new_i32();
1662 tcg_gen_trunc_tl_i32(s32
, count
);
1663 tcg_gen_movcond_i32(TCG_COND_NE
, cpu_cc_op
, s32
, z32
, s
->tmp2_i32
, oldop
);
1665 /* The CC_OP value is no longer predictable. */
1666 set_cc_op(s
, CC_OP_DYNAMIC
);
1669 static void gen_shift_rm_T1(DisasContext
*s
, MemOp ot
, int op1
,
1670 int is_right
, int is_arith
)
1672 target_ulong mask
= (ot
== MO_64
? 0x3f : 0x1f);
1675 if (op1
== OR_TMP0
) {
1676 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1678 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1681 tcg_gen_andi_tl(s
->T1
, s
->T1
, mask
);
1682 tcg_gen_subi_tl(s
->tmp0
, s
->T1
, 1);
1686 gen_exts(ot
, s
->T0
);
1687 tcg_gen_sar_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
1688 tcg_gen_sar_tl(s
->T0
, s
->T0
, s
->T1
);
1690 gen_extu(ot
, s
->T0
);
1691 tcg_gen_shr_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
1692 tcg_gen_shr_tl(s
->T0
, s
->T0
, s
->T1
);
1695 tcg_gen_shl_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
1696 tcg_gen_shl_tl(s
->T0
, s
->T0
, s
->T1
);
1700 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1702 gen_shift_flags(s
, ot
, s
->T0
, s
->tmp0
, s
->T1
, is_right
);
1705 static void gen_shift_rm_im(DisasContext
*s
, MemOp ot
, int op1
, int op2
,
1706 int is_right
, int is_arith
)
1708 int mask
= (ot
== MO_64
? 0x3f : 0x1f);
1712 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1714 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1720 gen_exts(ot
, s
->T0
);
1721 tcg_gen_sari_tl(s
->tmp4
, s
->T0
, op2
- 1);
1722 tcg_gen_sari_tl(s
->T0
, s
->T0
, op2
);
1724 gen_extu(ot
, s
->T0
);
1725 tcg_gen_shri_tl(s
->tmp4
, s
->T0
, op2
- 1);
1726 tcg_gen_shri_tl(s
->T0
, s
->T0
, op2
);
1729 tcg_gen_shli_tl(s
->tmp4
, s
->T0
, op2
- 1);
1730 tcg_gen_shli_tl(s
->T0
, s
->T0
, op2
);
1735 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1737 /* update eflags if non zero shift */
1739 tcg_gen_mov_tl(cpu_cc_src
, s
->tmp4
);
1740 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
1741 set_cc_op(s
, (is_right
? CC_OP_SARB
: CC_OP_SHLB
) + ot
);
1745 static void gen_rot_rm_T1(DisasContext
*s
, MemOp ot
, int op1
, int is_right
)
1747 target_ulong mask
= (ot
== MO_64
? 0x3f : 0x1f);
1751 if (op1
== OR_TMP0
) {
1752 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1754 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1757 tcg_gen_andi_tl(s
->T1
, s
->T1
, mask
);
1761 /* Replicate the 8-bit input so that a 32-bit rotate works. */
1762 tcg_gen_ext8u_tl(s
->T0
, s
->T0
);
1763 tcg_gen_muli_tl(s
->T0
, s
->T0
, 0x01010101);
1766 /* Replicate the 16-bit input so that a 32-bit rotate works. */
1767 tcg_gen_deposit_tl(s
->T0
, s
->T0
, s
->T0
, 16, 16);
1770 #ifdef TARGET_X86_64
1772 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
1773 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T1
);
1775 tcg_gen_rotr_i32(s
->tmp2_i32
, s
->tmp2_i32
, s
->tmp3_i32
);
1777 tcg_gen_rotl_i32(s
->tmp2_i32
, s
->tmp2_i32
, s
->tmp3_i32
);
1779 tcg_gen_extu_i32_tl(s
->T0
, s
->tmp2_i32
);
1784 tcg_gen_rotr_tl(s
->T0
, s
->T0
, s
->T1
);
1786 tcg_gen_rotl_tl(s
->T0
, s
->T0
, s
->T1
);
1792 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1794 /* We'll need the flags computed into CC_SRC. */
1795 gen_compute_eflags(s
);
1797 /* The value that was "rotated out" is now present at the other end
1798 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1799 since we've computed the flags into CC_SRC, these variables are
1802 tcg_gen_shri_tl(cpu_cc_src2
, s
->T0
, mask
- 1);
1803 tcg_gen_shri_tl(cpu_cc_dst
, s
->T0
, mask
);
1804 tcg_gen_andi_tl(cpu_cc_dst
, cpu_cc_dst
, 1);
1806 tcg_gen_shri_tl(cpu_cc_src2
, s
->T0
, mask
);
1807 tcg_gen_andi_tl(cpu_cc_dst
, s
->T0
, 1);
1809 tcg_gen_andi_tl(cpu_cc_src2
, cpu_cc_src2
, 1);
1810 tcg_gen_xor_tl(cpu_cc_src2
, cpu_cc_src2
, cpu_cc_dst
);
1812 /* Now conditionally store the new CC_OP value. If the shift count
1813 is 0 we keep the CC_OP_EFLAGS setting so that only CC_SRC is live.
1814 Otherwise reuse CC_OP_ADCOX which have the C and O flags split out
1815 exactly as we computed above. */
1816 t0
= tcg_constant_i32(0);
1817 t1
= tcg_temp_new_i32();
1818 tcg_gen_trunc_tl_i32(t1
, s
->T1
);
1819 tcg_gen_movi_i32(s
->tmp2_i32
, CC_OP_ADCOX
);
1820 tcg_gen_movi_i32(s
->tmp3_i32
, CC_OP_EFLAGS
);
1821 tcg_gen_movcond_i32(TCG_COND_NE
, cpu_cc_op
, t1
, t0
,
1822 s
->tmp2_i32
, s
->tmp3_i32
);
1824 /* The CC_OP value is no longer predictable. */
1825 set_cc_op(s
, CC_OP_DYNAMIC
);
1828 static void gen_rot_rm_im(DisasContext
*s
, MemOp ot
, int op1
, int op2
,
1831 int mask
= (ot
== MO_64
? 0x3f : 0x1f);
1835 if (op1
== OR_TMP0
) {
1836 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1838 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1844 #ifdef TARGET_X86_64
1846 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
1848 tcg_gen_rotri_i32(s
->tmp2_i32
, s
->tmp2_i32
, op2
);
1850 tcg_gen_rotli_i32(s
->tmp2_i32
, s
->tmp2_i32
, op2
);
1852 tcg_gen_extu_i32_tl(s
->T0
, s
->tmp2_i32
);
1857 tcg_gen_rotri_tl(s
->T0
, s
->T0
, op2
);
1859 tcg_gen_rotli_tl(s
->T0
, s
->T0
, op2
);
1870 shift
= mask
+ 1 - shift
;
1872 gen_extu(ot
, s
->T0
);
1873 tcg_gen_shli_tl(s
->tmp0
, s
->T0
, shift
);
1874 tcg_gen_shri_tl(s
->T0
, s
->T0
, mask
+ 1 - shift
);
1875 tcg_gen_or_tl(s
->T0
, s
->T0
, s
->tmp0
);
1881 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1884 /* Compute the flags into CC_SRC. */
1885 gen_compute_eflags(s
);
1887 /* The value that was "rotated out" is now present at the other end
1888 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1889 since we've computed the flags into CC_SRC, these variables are
1892 tcg_gen_shri_tl(cpu_cc_src2
, s
->T0
, mask
- 1);
1893 tcg_gen_shri_tl(cpu_cc_dst
, s
->T0
, mask
);
1894 tcg_gen_andi_tl(cpu_cc_dst
, cpu_cc_dst
, 1);
1896 tcg_gen_shri_tl(cpu_cc_src2
, s
->T0
, mask
);
1897 tcg_gen_andi_tl(cpu_cc_dst
, s
->T0
, 1);
1899 tcg_gen_andi_tl(cpu_cc_src2
, cpu_cc_src2
, 1);
1900 tcg_gen_xor_tl(cpu_cc_src2
, cpu_cc_src2
, cpu_cc_dst
);
1901 set_cc_op(s
, CC_OP_ADCOX
);
1905 /* XXX: add faster immediate = 1 case */
1906 static void gen_rotc_rm_T1(DisasContext
*s
, MemOp ot
, int op1
,
1909 gen_compute_eflags(s
);
1910 assert(s
->cc_op
== CC_OP_EFLAGS
);
1914 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1916 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1921 gen_helper_rcrb(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1924 gen_helper_rcrw(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1927 gen_helper_rcrl(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1929 #ifdef TARGET_X86_64
1931 gen_helper_rcrq(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1935 g_assert_not_reached();
1940 gen_helper_rclb(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1943 gen_helper_rclw(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1946 gen_helper_rcll(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1948 #ifdef TARGET_X86_64
1950 gen_helper_rclq(s
->T0
, cpu_env
, s
->T0
, s
->T1
);
1954 g_assert_not_reached();
1958 gen_op_st_rm_T0_A0(s
, ot
, op1
);
1961 /* XXX: add faster immediate case */
1962 static void gen_shiftd_rm_T1(DisasContext
*s
, MemOp ot
, int op1
,
1963 bool is_right
, TCGv count_in
)
1965 target_ulong mask
= (ot
== MO_64
? 63 : 31);
1969 if (op1
== OR_TMP0
) {
1970 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
1972 gen_op_mov_v_reg(s
, ot
, s
->T0
, op1
);
1975 count
= tcg_temp_new();
1976 tcg_gen_andi_tl(count
, count_in
, mask
);
1980 /* Note: we implement the Intel behaviour for shift count > 16.
1981 This means "shrdw C, B, A" shifts A:B:A >> C. Build the B:A
1982 portion by constructing it as a 32-bit value. */
1984 tcg_gen_deposit_tl(s
->tmp0
, s
->T0
, s
->T1
, 16, 16);
1985 tcg_gen_mov_tl(s
->T1
, s
->T0
);
1986 tcg_gen_mov_tl(s
->T0
, s
->tmp0
);
1988 tcg_gen_deposit_tl(s
->T1
, s
->T0
, s
->T1
, 16, 16);
1991 * If TARGET_X86_64 defined then fall through into MO_32 case,
1992 * otherwise fall through default case.
1995 #ifdef TARGET_X86_64
1996 /* Concatenate the two 32-bit values and use a 64-bit shift. */
1997 tcg_gen_subi_tl(s
->tmp0
, count
, 1);
1999 tcg_gen_concat_tl_i64(s
->T0
, s
->T0
, s
->T1
);
2000 tcg_gen_shr_i64(s
->tmp0
, s
->T0
, s
->tmp0
);
2001 tcg_gen_shr_i64(s
->T0
, s
->T0
, count
);
2003 tcg_gen_concat_tl_i64(s
->T0
, s
->T1
, s
->T0
);
2004 tcg_gen_shl_i64(s
->tmp0
, s
->T0
, s
->tmp0
);
2005 tcg_gen_shl_i64(s
->T0
, s
->T0
, count
);
2006 tcg_gen_shri_i64(s
->tmp0
, s
->tmp0
, 32);
2007 tcg_gen_shri_i64(s
->T0
, s
->T0
, 32);
2012 tcg_gen_subi_tl(s
->tmp0
, count
, 1);
2014 tcg_gen_shr_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
2016 tcg_gen_subfi_tl(s
->tmp4
, mask
+ 1, count
);
2017 tcg_gen_shr_tl(s
->T0
, s
->T0
, count
);
2018 tcg_gen_shl_tl(s
->T1
, s
->T1
, s
->tmp4
);
2020 tcg_gen_shl_tl(s
->tmp0
, s
->T0
, s
->tmp0
);
2022 /* Only needed if count > 16, for Intel behaviour. */
2023 tcg_gen_subfi_tl(s
->tmp4
, 33, count
);
2024 tcg_gen_shr_tl(s
->tmp4
, s
->T1
, s
->tmp4
);
2025 tcg_gen_or_tl(s
->tmp0
, s
->tmp0
, s
->tmp4
);
2028 tcg_gen_subfi_tl(s
->tmp4
, mask
+ 1, count
);
2029 tcg_gen_shl_tl(s
->T0
, s
->T0
, count
);
2030 tcg_gen_shr_tl(s
->T1
, s
->T1
, s
->tmp4
);
2032 tcg_gen_movi_tl(s
->tmp4
, 0);
2033 tcg_gen_movcond_tl(TCG_COND_EQ
, s
->T1
, count
, s
->tmp4
,
2035 tcg_gen_or_tl(s
->T0
, s
->T0
, s
->T1
);
2040 gen_op_st_rm_T0_A0(s
, ot
, op1
);
2042 gen_shift_flags(s
, ot
, s
->T0
, s
->tmp0
, count
, is_right
);
2045 static void gen_shift(DisasContext
*s1
, int op
, MemOp ot
, int d
, int s
)
2048 gen_op_mov_v_reg(s1
, ot
, s1
->T1
, s
);
2051 gen_rot_rm_T1(s1
, ot
, d
, 0);
2054 gen_rot_rm_T1(s1
, ot
, d
, 1);
2058 gen_shift_rm_T1(s1
, ot
, d
, 0, 0);
2061 gen_shift_rm_T1(s1
, ot
, d
, 1, 0);
2064 gen_shift_rm_T1(s1
, ot
, d
, 1, 1);
2067 gen_rotc_rm_T1(s1
, ot
, d
, 0);
2070 gen_rotc_rm_T1(s1
, ot
, d
, 1);
2075 static void gen_shifti(DisasContext
*s1
, int op
, MemOp ot
, int d
, int c
)
2079 gen_rot_rm_im(s1
, ot
, d
, c
, 0);
2082 gen_rot_rm_im(s1
, ot
, d
, c
, 1);
2086 gen_shift_rm_im(s1
, ot
, d
, c
, 0, 0);
2089 gen_shift_rm_im(s1
, ot
, d
, c
, 1, 0);
2092 gen_shift_rm_im(s1
, ot
, d
, c
, 1, 1);
2095 /* currently not optimized */
2096 tcg_gen_movi_tl(s1
->T1
, c
);
2097 gen_shift(s1
, op
, ot
, d
, OR_TMP1
);
2102 #define X86_MAX_INSN_LENGTH 15
2104 static uint64_t advance_pc(CPUX86State
*env
, DisasContext
*s
, int num_bytes
)
2106 uint64_t pc
= s
->pc
;
2108 /* This is a subsequent insn that crosses a page boundary. */
2109 if (s
->base
.num_insns
> 1 &&
2110 !is_same_page(&s
->base
, s
->pc
+ num_bytes
- 1)) {
2111 siglongjmp(s
->jmpbuf
, 2);
2115 if (unlikely(cur_insn_len(s
) > X86_MAX_INSN_LENGTH
)) {
2116 /* If the instruction's 16th byte is on a different page than the 1st, a
2117 * page fault on the second page wins over the general protection fault
2118 * caused by the instruction being too long.
2119 * This can happen even if the operand is only one byte long!
2121 if (((s
->pc
- 1) ^ (pc
- 1)) & TARGET_PAGE_MASK
) {
2122 volatile uint8_t unused
=
2123 cpu_ldub_code(env
, (s
->pc
- 1) & TARGET_PAGE_MASK
);
2126 siglongjmp(s
->jmpbuf
, 1);
2132 static inline uint8_t x86_ldub_code(CPUX86State
*env
, DisasContext
*s
)
2134 return translator_ldub(env
, &s
->base
, advance_pc(env
, s
, 1));
2137 static inline int16_t x86_ldsw_code(CPUX86State
*env
, DisasContext
*s
)
2139 return translator_lduw(env
, &s
->base
, advance_pc(env
, s
, 2));
2142 static inline uint16_t x86_lduw_code(CPUX86State
*env
, DisasContext
*s
)
2144 return translator_lduw(env
, &s
->base
, advance_pc(env
, s
, 2));
2147 static inline uint32_t x86_ldl_code(CPUX86State
*env
, DisasContext
*s
)
2149 return translator_ldl(env
, &s
->base
, advance_pc(env
, s
, 4));
2152 #ifdef TARGET_X86_64
2153 static inline uint64_t x86_ldq_code(CPUX86State
*env
, DisasContext
*s
)
2155 return translator_ldq(env
, &s
->base
, advance_pc(env
, s
, 8));
2159 /* Decompose an address. */
2161 typedef struct AddressParts
{
2169 static AddressParts
gen_lea_modrm_0(CPUX86State
*env
, DisasContext
*s
,
2172 int def_seg
, base
, index
, scale
, mod
, rm
;
2181 mod
= (modrm
>> 6) & 3;
2183 base
= rm
| REX_B(s
);
2186 /* Normally filtered out earlier, but including this path
2187 simplifies multi-byte nop, as well as bndcl, bndcu, bndcn. */
2196 int code
= x86_ldub_code(env
, s
);
2197 scale
= (code
>> 6) & 3;
2198 index
= ((code
>> 3) & 7) | REX_X(s
);
2200 index
= -1; /* no index */
2202 base
= (code
& 7) | REX_B(s
);
2208 if ((base
& 7) == 5) {
2210 disp
= (int32_t)x86_ldl_code(env
, s
);
2211 if (CODE64(s
) && !havesib
) {
2213 disp
+= s
->pc
+ s
->rip_offset
;
2218 disp
= (int8_t)x86_ldub_code(env
, s
);
2222 disp
= (int32_t)x86_ldl_code(env
, s
);
2226 /* For correct popl handling with esp. */
2227 if (base
== R_ESP
&& s
->popl_esp_hack
) {
2228 disp
+= s
->popl_esp_hack
;
2230 if (base
== R_EBP
|| base
== R_ESP
) {
2239 disp
= x86_lduw_code(env
, s
);
2242 } else if (mod
== 1) {
2243 disp
= (int8_t)x86_ldub_code(env
, s
);
2245 disp
= (int16_t)x86_lduw_code(env
, s
);
2285 g_assert_not_reached();
2289 return (AddressParts
){ def_seg
, base
, index
, scale
, disp
};
2292 /* Compute the address, with a minimum number of TCG ops. */
2293 static TCGv
gen_lea_modrm_1(DisasContext
*s
, AddressParts a
, bool is_vsib
)
2297 if (a
.index
>= 0 && !is_vsib
) {
2299 ea
= cpu_regs
[a
.index
];
2301 tcg_gen_shli_tl(s
->A0
, cpu_regs
[a
.index
], a
.scale
);
2305 tcg_gen_add_tl(s
->A0
, ea
, cpu_regs
[a
.base
]);
2308 } else if (a
.base
>= 0) {
2309 ea
= cpu_regs
[a
.base
];
2312 if (tb_cflags(s
->base
.tb
) & CF_PCREL
&& a
.base
== -2) {
2313 /* With cpu_eip ~= pc_save, the expression is pc-relative. */
2314 tcg_gen_addi_tl(s
->A0
, cpu_eip
, a
.disp
- s
->pc_save
);
2316 tcg_gen_movi_tl(s
->A0
, a
.disp
);
2319 } else if (a
.disp
!= 0) {
2320 tcg_gen_addi_tl(s
->A0
, ea
, a
.disp
);
2327 static void gen_lea_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
)
2329 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
2330 TCGv ea
= gen_lea_modrm_1(s
, a
, false);
2331 gen_lea_v_seg(s
, s
->aflag
, ea
, a
.def_seg
, s
->override
);
2334 static void gen_nop_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
)
2336 (void)gen_lea_modrm_0(env
, s
, modrm
);
2339 /* Used for BNDCL, BNDCU, BNDCN. */
2340 static void gen_bndck(CPUX86State
*env
, DisasContext
*s
, int modrm
,
2341 TCGCond cond
, TCGv_i64 bndv
)
2343 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
2344 TCGv ea
= gen_lea_modrm_1(s
, a
, false);
2346 tcg_gen_extu_tl_i64(s
->tmp1_i64
, ea
);
2348 tcg_gen_ext32u_i64(s
->tmp1_i64
, s
->tmp1_i64
);
2350 tcg_gen_setcond_i64(cond
, s
->tmp1_i64
, s
->tmp1_i64
, bndv
);
2351 tcg_gen_extrl_i64_i32(s
->tmp2_i32
, s
->tmp1_i64
);
2352 gen_helper_bndck(cpu_env
, s
->tmp2_i32
);
2355 /* used for LEA and MOV AX, mem */
2356 static void gen_add_A0_ds_seg(DisasContext
*s
)
2358 gen_lea_v_seg(s
, s
->aflag
, s
->A0
, R_DS
, s
->override
);
2361 /* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
2363 static void gen_ldst_modrm(CPUX86State
*env
, DisasContext
*s
, int modrm
,
2364 MemOp ot
, int reg
, int is_store
)
2368 mod
= (modrm
>> 6) & 3;
2369 rm
= (modrm
& 7) | REX_B(s
);
2373 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
2374 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
2376 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
2378 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
2381 gen_lea_modrm(env
, s
, modrm
);
2384 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
2385 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
2387 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
2389 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
2394 static target_ulong
insn_get_addr(CPUX86State
*env
, DisasContext
*s
, MemOp ot
)
2400 ret
= x86_ldub_code(env
, s
);
2403 ret
= x86_lduw_code(env
, s
);
2406 ret
= x86_ldl_code(env
, s
);
2408 #ifdef TARGET_X86_64
2410 ret
= x86_ldq_code(env
, s
);
2414 g_assert_not_reached();
2419 static inline uint32_t insn_get(CPUX86State
*env
, DisasContext
*s
, MemOp ot
)
2425 ret
= x86_ldub_code(env
, s
);
2428 ret
= x86_lduw_code(env
, s
);
2431 #ifdef TARGET_X86_64
2434 ret
= x86_ldl_code(env
, s
);
2437 g_assert_not_reached();
2442 static target_long
insn_get_signed(CPUX86State
*env
, DisasContext
*s
, MemOp ot
)
2448 ret
= (int8_t) x86_ldub_code(env
, s
);
2451 ret
= (int16_t) x86_lduw_code(env
, s
);
2454 ret
= (int32_t) x86_ldl_code(env
, s
);
2456 #ifdef TARGET_X86_64
2458 ret
= x86_ldq_code(env
, s
);
2462 g_assert_not_reached();
2467 static inline int insn_const_size(MemOp ot
)
2476 static void gen_jcc(DisasContext
*s
, int b
, int diff
)
2478 TCGLabel
*l1
= gen_new_label();
2481 gen_jmp_rel_csize(s
, 0, 1);
2483 gen_jmp_rel(s
, s
->dflag
, diff
, 0);
2486 static void gen_cmovcc1(CPUX86State
*env
, DisasContext
*s
, MemOp ot
, int b
,
2491 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
2493 cc
= gen_prepare_cc(s
, b
, s
->T1
);
2494 if (cc
.mask
!= -1) {
2495 TCGv t0
= tcg_temp_new();
2496 tcg_gen_andi_tl(t0
, cc
.reg
, cc
.mask
);
2500 cc
.reg2
= tcg_constant_tl(cc
.imm
);
2503 tcg_gen_movcond_tl(cc
.cond
, s
->T0
, cc
.reg
, cc
.reg2
,
2504 s
->T0
, cpu_regs
[reg
]);
2505 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
2508 static inline void gen_op_movl_T0_seg(DisasContext
*s
, X86Seg seg_reg
)
2510 tcg_gen_ld32u_tl(s
->T0
, cpu_env
,
2511 offsetof(CPUX86State
,segs
[seg_reg
].selector
));
2514 static inline void gen_op_movl_seg_T0_vm(DisasContext
*s
, X86Seg seg_reg
)
2516 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
2517 tcg_gen_st32_tl(s
->T0
, cpu_env
,
2518 offsetof(CPUX86State
,segs
[seg_reg
].selector
));
2519 tcg_gen_shli_tl(cpu_seg_base
[seg_reg
], s
->T0
, 4);
2522 /* move T0 to seg_reg and compute if the CPU state may change. Never
2523 call this function with seg_reg == R_CS */
2524 static void gen_movl_seg_T0(DisasContext
*s
, X86Seg seg_reg
)
2526 if (PE(s
) && !VM86(s
)) {
2527 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
2528 gen_helper_load_seg(cpu_env
, tcg_constant_i32(seg_reg
), s
->tmp2_i32
);
2529 /* abort translation because the addseg value may change or
2530 because ss32 may change. For R_SS, translation must always
2531 stop as a special handling must be done to disable hardware
2532 interrupts for the next instruction */
2533 if (seg_reg
== R_SS
) {
2534 s
->base
.is_jmp
= DISAS_EOB_INHIBIT_IRQ
;
2535 } else if (CODE32(s
) && seg_reg
< R_FS
) {
2536 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
2539 gen_op_movl_seg_T0_vm(s
, seg_reg
);
2540 if (seg_reg
== R_SS
) {
2541 s
->base
.is_jmp
= DISAS_EOB_INHIBIT_IRQ
;
2546 static void gen_svm_check_intercept(DisasContext
*s
, uint32_t type
)
2548 /* no SVM activated; fast case */
2549 if (likely(!GUEST(s
))) {
2552 gen_helper_svm_check_intercept(cpu_env
, tcg_constant_i32(type
));
2555 static inline void gen_stack_update(DisasContext
*s
, int addend
)
2557 gen_op_add_reg_im(s
, mo_stacksize(s
), R_ESP
, addend
);
2560 /* Generate a push. It depends on ss32, addseg and dflag. */
2561 static void gen_push_v(DisasContext
*s
, TCGv val
)
2563 MemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2564 MemOp a_ot
= mo_stacksize(s
);
2565 int size
= 1 << d_ot
;
2566 TCGv new_esp
= s
->A0
;
2568 tcg_gen_subi_tl(s
->A0
, cpu_regs
[R_ESP
], size
);
2573 tcg_gen_mov_tl(new_esp
, s
->A0
);
2575 gen_lea_v_seg(s
, a_ot
, s
->A0
, R_SS
, -1);
2578 gen_op_st_v(s
, d_ot
, val
, s
->A0
);
2579 gen_op_mov_reg_v(s
, a_ot
, R_ESP
, new_esp
);
2582 /* two step pop is necessary for precise exceptions */
2583 static MemOp
gen_pop_T0(DisasContext
*s
)
2585 MemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2587 gen_lea_v_seg(s
, mo_stacksize(s
), cpu_regs
[R_ESP
], R_SS
, -1);
2588 gen_op_ld_v(s
, d_ot
, s
->T0
, s
->A0
);
2593 static inline void gen_pop_update(DisasContext
*s
, MemOp ot
)
2595 gen_stack_update(s
, 1 << ot
);
2598 static inline void gen_stack_A0(DisasContext
*s
)
2600 gen_lea_v_seg(s
, SS32(s
) ? MO_32
: MO_16
, cpu_regs
[R_ESP
], R_SS
, -1);
2603 static void gen_pusha(DisasContext
*s
)
2605 MemOp s_ot
= SS32(s
) ? MO_32
: MO_16
;
2606 MemOp d_ot
= s
->dflag
;
2607 int size
= 1 << d_ot
;
2610 for (i
= 0; i
< 8; i
++) {
2611 tcg_gen_addi_tl(s
->A0
, cpu_regs
[R_ESP
], (i
- 8) * size
);
2612 gen_lea_v_seg(s
, s_ot
, s
->A0
, R_SS
, -1);
2613 gen_op_st_v(s
, d_ot
, cpu_regs
[7 - i
], s
->A0
);
2616 gen_stack_update(s
, -8 * size
);
2619 static void gen_popa(DisasContext
*s
)
2621 MemOp s_ot
= SS32(s
) ? MO_32
: MO_16
;
2622 MemOp d_ot
= s
->dflag
;
2623 int size
= 1 << d_ot
;
2626 for (i
= 0; i
< 8; i
++) {
2627 /* ESP is not reloaded */
2628 if (7 - i
== R_ESP
) {
2631 tcg_gen_addi_tl(s
->A0
, cpu_regs
[R_ESP
], i
* size
);
2632 gen_lea_v_seg(s
, s_ot
, s
->A0
, R_SS
, -1);
2633 gen_op_ld_v(s
, d_ot
, s
->T0
, s
->A0
);
2634 gen_op_mov_reg_v(s
, d_ot
, 7 - i
, s
->T0
);
2637 gen_stack_update(s
, 8 * size
);
2640 static void gen_enter(DisasContext
*s
, int esp_addend
, int level
)
2642 MemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2643 MemOp a_ot
= CODE64(s
) ? MO_64
: SS32(s
) ? MO_32
: MO_16
;
2644 int size
= 1 << d_ot
;
2646 /* Push BP; compute FrameTemp into T1. */
2647 tcg_gen_subi_tl(s
->T1
, cpu_regs
[R_ESP
], size
);
2648 gen_lea_v_seg(s
, a_ot
, s
->T1
, R_SS
, -1);
2649 gen_op_st_v(s
, d_ot
, cpu_regs
[R_EBP
], s
->A0
);
2655 /* Copy level-1 pointers from the previous frame. */
2656 for (i
= 1; i
< level
; ++i
) {
2657 tcg_gen_subi_tl(s
->A0
, cpu_regs
[R_EBP
], size
* i
);
2658 gen_lea_v_seg(s
, a_ot
, s
->A0
, R_SS
, -1);
2659 gen_op_ld_v(s
, d_ot
, s
->tmp0
, s
->A0
);
2661 tcg_gen_subi_tl(s
->A0
, s
->T1
, size
* i
);
2662 gen_lea_v_seg(s
, a_ot
, s
->A0
, R_SS
, -1);
2663 gen_op_st_v(s
, d_ot
, s
->tmp0
, s
->A0
);
2666 /* Push the current FrameTemp as the last level. */
2667 tcg_gen_subi_tl(s
->A0
, s
->T1
, size
* level
);
2668 gen_lea_v_seg(s
, a_ot
, s
->A0
, R_SS
, -1);
2669 gen_op_st_v(s
, d_ot
, s
->T1
, s
->A0
);
2672 /* Copy the FrameTemp value to EBP. */
2673 gen_op_mov_reg_v(s
, a_ot
, R_EBP
, s
->T1
);
2675 /* Compute the final value of ESP. */
2676 tcg_gen_subi_tl(s
->T1
, s
->T1
, esp_addend
+ size
* level
);
2677 gen_op_mov_reg_v(s
, a_ot
, R_ESP
, s
->T1
);
2680 static void gen_leave(DisasContext
*s
)
2682 MemOp d_ot
= mo_pushpop(s
, s
->dflag
);
2683 MemOp a_ot
= mo_stacksize(s
);
2685 gen_lea_v_seg(s
, a_ot
, cpu_regs
[R_EBP
], R_SS
, -1);
2686 gen_op_ld_v(s
, d_ot
, s
->T0
, s
->A0
);
2688 tcg_gen_addi_tl(s
->T1
, cpu_regs
[R_EBP
], 1 << d_ot
);
2690 gen_op_mov_reg_v(s
, d_ot
, R_EBP
, s
->T0
);
2691 gen_op_mov_reg_v(s
, a_ot
, R_ESP
, s
->T1
);
2694 /* Similarly, except that the assumption here is that we don't decode
2695 the instruction at all -- either a missing opcode, an unimplemented
2696 feature, or just a bogus instruction stream. */
2697 static void gen_unknown_opcode(CPUX86State
*env
, DisasContext
*s
)
2699 gen_illegal_opcode(s
);
2701 if (qemu_loglevel_mask(LOG_UNIMP
)) {
2702 FILE *logfile
= qemu_log_trylock();
2704 target_ulong pc
= s
->base
.pc_next
, end
= s
->pc
;
2706 fprintf(logfile
, "ILLOPC: " TARGET_FMT_lx
":", pc
);
2707 for (; pc
< end
; ++pc
) {
2708 fprintf(logfile
, " %02x", cpu_ldub_code(env
, pc
));
2710 fprintf(logfile
, "\n");
2711 qemu_log_unlock(logfile
);
2716 /* an interrupt is different from an exception because of the
2718 static void gen_interrupt(DisasContext
*s
, int intno
)
2720 gen_update_cc_op(s
);
2721 gen_update_eip_cur(s
);
2722 gen_helper_raise_interrupt(cpu_env
, tcg_constant_i32(intno
),
2723 cur_insn_len_i32(s
));
2724 s
->base
.is_jmp
= DISAS_NORETURN
;
2727 static void gen_set_hflag(DisasContext
*s
, uint32_t mask
)
2729 if ((s
->flags
& mask
) == 0) {
2730 TCGv_i32 t
= tcg_temp_new_i32();
2731 tcg_gen_ld_i32(t
, cpu_env
, offsetof(CPUX86State
, hflags
));
2732 tcg_gen_ori_i32(t
, t
, mask
);
2733 tcg_gen_st_i32(t
, cpu_env
, offsetof(CPUX86State
, hflags
));
2738 static void gen_reset_hflag(DisasContext
*s
, uint32_t mask
)
2740 if (s
->flags
& mask
) {
2741 TCGv_i32 t
= tcg_temp_new_i32();
2742 tcg_gen_ld_i32(t
, cpu_env
, offsetof(CPUX86State
, hflags
));
2743 tcg_gen_andi_i32(t
, t
, ~mask
);
2744 tcg_gen_st_i32(t
, cpu_env
, offsetof(CPUX86State
, hflags
));
2749 static void gen_set_eflags(DisasContext
*s
, target_ulong mask
)
2751 TCGv t
= tcg_temp_new();
2753 tcg_gen_ld_tl(t
, cpu_env
, offsetof(CPUX86State
, eflags
));
2754 tcg_gen_ori_tl(t
, t
, mask
);
2755 tcg_gen_st_tl(t
, cpu_env
, offsetof(CPUX86State
, eflags
));
2758 static void gen_reset_eflags(DisasContext
*s
, target_ulong mask
)
2760 TCGv t
= tcg_temp_new();
2762 tcg_gen_ld_tl(t
, cpu_env
, offsetof(CPUX86State
, eflags
));
2763 tcg_gen_andi_tl(t
, t
, ~mask
);
2764 tcg_gen_st_tl(t
, cpu_env
, offsetof(CPUX86State
, eflags
));
2767 /* Clear BND registers during legacy branches. */
2768 static void gen_bnd_jmp(DisasContext
*s
)
2770 /* Clear the registers only if BND prefix is missing, MPX is enabled,
2771 and if the BNDREGs are known to be in use (non-zero) already.
2772 The helper itself will check BNDPRESERVE at runtime. */
2773 if ((s
->prefix
& PREFIX_REPNZ
) == 0
2774 && (s
->flags
& HF_MPX_EN_MASK
) != 0
2775 && (s
->flags
& HF_MPX_IU_MASK
) != 0) {
2776 gen_helper_bnd_jmp(cpu_env
);
2780 /* Generate an end of block. Trace exception is also generated if needed.
2781 If INHIBIT, set HF_INHIBIT_IRQ_MASK if it isn't already set.
2782 If RECHECK_TF, emit a rechecking helper for #DB, ignoring the state of
2783 S->TF. This is used by the syscall/sysret insns. */
2785 do_gen_eob_worker(DisasContext
*s
, bool inhibit
, bool recheck_tf
, bool jr
)
2787 gen_update_cc_op(s
);
2789 /* If several instructions disable interrupts, only the first does it. */
2790 if (inhibit
&& !(s
->flags
& HF_INHIBIT_IRQ_MASK
)) {
2791 gen_set_hflag(s
, HF_INHIBIT_IRQ_MASK
);
2793 gen_reset_hflag(s
, HF_INHIBIT_IRQ_MASK
);
2796 if (s
->base
.tb
->flags
& HF_RF_MASK
) {
2797 gen_reset_eflags(s
, RF_MASK
);
2800 gen_helper_rechecking_single_step(cpu_env
);
2801 tcg_gen_exit_tb(NULL
, 0);
2802 } else if (s
->flags
& HF_TF_MASK
) {
2803 gen_helper_single_step(cpu_env
);
2805 tcg_gen_lookup_and_goto_ptr();
2807 tcg_gen_exit_tb(NULL
, 0);
2809 s
->base
.is_jmp
= DISAS_NORETURN
;
2813 gen_eob_worker(DisasContext
*s
, bool inhibit
, bool recheck_tf
)
2815 do_gen_eob_worker(s
, inhibit
, recheck_tf
, false);
2819 If INHIBIT, set HF_INHIBIT_IRQ_MASK if it isn't already set. */
2820 static void gen_eob_inhibit_irq(DisasContext
*s
, bool inhibit
)
2822 gen_eob_worker(s
, inhibit
, false);
2825 /* End of block, resetting the inhibit irq flag. */
2826 static void gen_eob(DisasContext
*s
)
2828 gen_eob_worker(s
, false, false);
2831 /* Jump to register */
2832 static void gen_jr(DisasContext
*s
)
2834 do_gen_eob_worker(s
, false, false, true);
2837 /* Jump to eip+diff, truncating the result to OT. */
2838 static void gen_jmp_rel(DisasContext
*s
, MemOp ot
, int diff
, int tb_num
)
2840 bool use_goto_tb
= s
->jmp_opt
;
2841 target_ulong mask
= -1;
2842 target_ulong new_pc
= s
->pc
+ diff
;
2843 target_ulong new_eip
= new_pc
- s
->cs_base
;
2845 /* In 64-bit mode, operand size is fixed at 64 bits. */
2849 if (tb_cflags(s
->base
.tb
) & CF_PCREL
&& CODE32(s
)) {
2850 use_goto_tb
= false;
2858 gen_update_cc_op(s
);
2859 set_cc_op(s
, CC_OP_DYNAMIC
);
2861 if (tb_cflags(s
->base
.tb
) & CF_PCREL
) {
2862 tcg_gen_addi_tl(cpu_eip
, cpu_eip
, new_pc
- s
->pc_save
);
2864 * If we can prove the branch does not leave the page and we have
2865 * no extra masking to apply (data16 branch in code32, see above),
2866 * then we have also proven that the addition does not wrap.
2868 if (!use_goto_tb
|| !is_same_page(&s
->base
, new_pc
)) {
2869 tcg_gen_andi_tl(cpu_eip
, cpu_eip
, mask
);
2870 use_goto_tb
= false;
2875 translator_use_goto_tb(&s
->base
, new_eip
+ s
->cs_base
)) {
2876 /* jump to same page: we can use a direct jump */
2877 tcg_gen_goto_tb(tb_num
);
2878 if (!(tb_cflags(s
->base
.tb
) & CF_PCREL
)) {
2879 tcg_gen_movi_tl(cpu_eip
, new_eip
);
2881 tcg_gen_exit_tb(s
->base
.tb
, tb_num
);
2882 s
->base
.is_jmp
= DISAS_NORETURN
;
2884 if (!(tb_cflags(s
->base
.tb
) & CF_PCREL
)) {
2885 tcg_gen_movi_tl(cpu_eip
, new_eip
);
2888 gen_jr(s
); /* jump to another page */
2890 gen_eob(s
); /* exit to main loop */
2895 /* Jump to eip+diff, truncating to the current code size. */
2896 static void gen_jmp_rel_csize(DisasContext
*s
, int diff
, int tb_num
)
2898 /* CODE64 ignores the OT argument, so we need not consider it. */
2899 gen_jmp_rel(s
, CODE32(s
) ? MO_32
: MO_16
, diff
, tb_num
);
2902 static inline void gen_ldq_env_A0(DisasContext
*s
, int offset
)
2904 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
, s
->mem_index
, MO_LEUQ
);
2905 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
);
2908 static inline void gen_stq_env_A0(DisasContext
*s
, int offset
)
2910 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
);
2911 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
, s
->mem_index
, MO_LEUQ
);
2914 static inline void gen_ldo_env_A0(DisasContext
*s
, int offset
, bool align
)
2916 int mem_index
= s
->mem_index
;
2917 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
, mem_index
,
2918 MO_LEUQ
| (align
? MO_ALIGN_16
: 0));
2919 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(0)));
2920 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 8);
2921 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEUQ
);
2922 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(1)));
2925 static inline void gen_sto_env_A0(DisasContext
*s
, int offset
, bool align
)
2927 int mem_index
= s
->mem_index
;
2928 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(0)));
2929 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
, mem_index
,
2930 MO_LEUQ
| (align
? MO_ALIGN_16
: 0));
2931 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 8);
2932 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(XMMReg
, XMM_Q(1)));
2933 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEUQ
);
2936 static void gen_ldy_env_A0(DisasContext
*s
, int offset
, bool align
)
2938 int mem_index
= s
->mem_index
;
2939 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
, mem_index
,
2940 MO_LEUQ
| (align
? MO_ALIGN_32
: 0));
2941 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(YMMReg
, YMM_Q(0)));
2942 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 8);
2943 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEUQ
);
2944 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(YMMReg
, YMM_Q(1)));
2946 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 16);
2947 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEUQ
);
2948 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(YMMReg
, YMM_Q(2)));
2949 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 24);
2950 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEUQ
);
2951 tcg_gen_st_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(YMMReg
, YMM_Q(3)));
2954 static void gen_sty_env_A0(DisasContext
*s
, int offset
, bool align
)
2956 int mem_index
= s
->mem_index
;
2957 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(YMMReg
, YMM_Q(0)));
2958 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
, mem_index
,
2959 MO_LEUQ
| (align
? MO_ALIGN_32
: 0));
2960 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 8);
2961 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(YMMReg
, YMM_Q(1)));
2962 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEUQ
);
2963 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 16);
2964 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(YMMReg
, YMM_Q(2)));
2965 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEUQ
);
2966 tcg_gen_addi_tl(s
->tmp0
, s
->A0
, 24);
2967 tcg_gen_ld_i64(s
->tmp1_i64
, cpu_env
, offset
+ offsetof(YMMReg
, YMM_Q(3)));
2968 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->tmp0
, mem_index
, MO_LEUQ
);
2971 #include "decode-new.h"
2972 #include "emit.c.inc"
2973 #include "decode-new.c.inc"
2975 static void gen_cmpxchg8b(DisasContext
*s
, CPUX86State
*env
, int modrm
)
2977 TCGv_i64 cmp
, val
, old
;
2980 gen_lea_modrm(env
, s
, modrm
);
2982 cmp
= tcg_temp_new_i64();
2983 val
= tcg_temp_new_i64();
2984 old
= tcg_temp_new_i64();
2986 /* Construct the comparison values from the register pair. */
2987 tcg_gen_concat_tl_i64(cmp
, cpu_regs
[R_EAX
], cpu_regs
[R_EDX
]);
2988 tcg_gen_concat_tl_i64(val
, cpu_regs
[R_EBX
], cpu_regs
[R_ECX
]);
2990 /* Only require atomic with LOCK; non-parallel handled in generator. */
2991 if (s
->prefix
& PREFIX_LOCK
) {
2992 tcg_gen_atomic_cmpxchg_i64(old
, s
->A0
, cmp
, val
, s
->mem_index
, MO_TEUQ
);
2994 tcg_gen_nonatomic_cmpxchg_i64(old
, s
->A0
, cmp
, val
,
2995 s
->mem_index
, MO_TEUQ
);
2998 /* Set tmp0 to match the required value of Z. */
2999 tcg_gen_setcond_i64(TCG_COND_EQ
, cmp
, old
, cmp
);
3001 tcg_gen_trunc_i64_tl(Z
, cmp
);
3004 * Extract the result values for the register pair.
3005 * For 32-bit, we may do this unconditionally, because on success (Z=1),
3006 * the old value matches the previous value in EDX:EAX. For x86_64,
3007 * the store must be conditional, because we must leave the source
3008 * registers unchanged on success, and zero-extend the writeback
3011 if (TARGET_LONG_BITS
== 32) {
3012 tcg_gen_extr_i64_tl(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
], old
);
3014 TCGv zero
= tcg_constant_tl(0);
3016 tcg_gen_extr_i64_tl(s
->T0
, s
->T1
, old
);
3017 tcg_gen_movcond_tl(TCG_COND_EQ
, cpu_regs
[R_EAX
], Z
, zero
,
3018 s
->T0
, cpu_regs
[R_EAX
]);
3019 tcg_gen_movcond_tl(TCG_COND_EQ
, cpu_regs
[R_EDX
], Z
, zero
,
3020 s
->T1
, cpu_regs
[R_EDX
]);
3024 gen_compute_eflags(s
);
3025 tcg_gen_deposit_tl(cpu_cc_src
, cpu_cc_src
, Z
, ctz32(CC_Z
), 1);
3028 #ifdef TARGET_X86_64
3029 static void gen_cmpxchg16b(DisasContext
*s
, CPUX86State
*env
, int modrm
)
3031 MemOp mop
= MO_TE
| MO_128
| MO_ALIGN
;
3035 gen_lea_modrm(env
, s
, modrm
);
3037 cmp
= tcg_temp_new_i128();
3038 val
= tcg_temp_new_i128();
3039 tcg_gen_concat_i64_i128(cmp
, cpu_regs
[R_EAX
], cpu_regs
[R_EDX
]);
3040 tcg_gen_concat_i64_i128(val
, cpu_regs
[R_EBX
], cpu_regs
[R_ECX
]);
3042 /* Only require atomic with LOCK; non-parallel handled in generator. */
3043 if (s
->prefix
& PREFIX_LOCK
) {
3044 tcg_gen_atomic_cmpxchg_i128(val
, s
->A0
, cmp
, val
, s
->mem_index
, mop
);
3046 tcg_gen_nonatomic_cmpxchg_i128(val
, s
->A0
, cmp
, val
, s
->mem_index
, mop
);
3049 tcg_gen_extr_i128_i64(s
->T0
, s
->T1
, val
);
3051 /* Determine success after the fact. */
3052 t0
= tcg_temp_new_i64();
3053 t1
= tcg_temp_new_i64();
3054 tcg_gen_xor_i64(t0
, s
->T0
, cpu_regs
[R_EAX
]);
3055 tcg_gen_xor_i64(t1
, s
->T1
, cpu_regs
[R_EDX
]);
3056 tcg_gen_or_i64(t0
, t0
, t1
);
3059 gen_compute_eflags(s
);
3060 tcg_gen_setcondi_i64(TCG_COND_EQ
, t0
, t0
, 0);
3061 tcg_gen_deposit_tl(cpu_cc_src
, cpu_cc_src
, t0
, ctz32(CC_Z
), 1);
3064 * Extract the result values for the register pair. We may do this
3065 * unconditionally, because on success (Z=1), the old value matches
3066 * the previous value in RDX:RAX.
3068 tcg_gen_mov_i64(cpu_regs
[R_EAX
], s
->T0
);
3069 tcg_gen_mov_i64(cpu_regs
[R_EDX
], s
->T1
);
3073 /* convert one instruction. s->base.is_jmp is set if the translation must
3074 be stopped. Return the next pc value */
3075 static bool disas_insn(DisasContext
*s
, CPUState
*cpu
)
3077 CPUX86State
*env
= cpu
->env_ptr
;
3080 MemOp ot
, aflag
, dflag
;
3081 int modrm
, reg
, rm
, mod
, op
, opreg
, val
;
3082 bool orig_cc_op_dirty
= s
->cc_op_dirty
;
3083 CCOp orig_cc_op
= s
->cc_op
;
3084 target_ulong orig_pc_save
= s
->pc_save
;
3086 s
->pc
= s
->base
.pc_next
;
3088 #ifdef TARGET_X86_64
3093 s
->rip_offset
= 0; /* for relative ip address */
3097 switch (sigsetjmp(s
->jmpbuf
, 0)) {
3101 gen_exception_gpf(s
);
3104 /* Restore state that may affect the next instruction. */
3105 s
->pc
= s
->base
.pc_next
;
3107 * TODO: These save/restore can be removed after the table-based
3108 * decoder is complete; we will be decoding the insn completely
3109 * before any code generation that might affect these variables.
3111 s
->cc_op_dirty
= orig_cc_op_dirty
;
3112 s
->cc_op
= orig_cc_op
;
3113 s
->pc_save
= orig_pc_save
;
3115 s
->base
.num_insns
--;
3116 tcg_remove_ops_after(s
->prev_insn_end
);
3117 s
->base
.is_jmp
= DISAS_TOO_MANY
;
3120 g_assert_not_reached();
3126 s
->prefix
= prefixes
;
3127 b
= x86_ldub_code(env
, s
);
3128 /* Collect prefixes. */
3133 b
= x86_ldub_code(env
, s
) + 0x100;
3136 prefixes
|= PREFIX_REPZ
;
3137 prefixes
&= ~PREFIX_REPNZ
;
3140 prefixes
|= PREFIX_REPNZ
;
3141 prefixes
&= ~PREFIX_REPZ
;
3144 prefixes
|= PREFIX_LOCK
;
3165 prefixes
|= PREFIX_DATA
;
3168 prefixes
|= PREFIX_ADR
;
3170 #ifdef TARGET_X86_64
3174 prefixes
|= PREFIX_REX
;
3175 s
->vex_w
= (b
>> 3) & 1;
3176 s
->rex_r
= (b
& 0x4) << 1;
3177 s
->rex_x
= (b
& 0x2) << 2;
3178 s
->rex_b
= (b
& 0x1) << 3;
3183 case 0xc5: /* 2-byte VEX */
3184 case 0xc4: /* 3-byte VEX */
3185 if (CODE32(s
) && !VM86(s
)) {
3186 int vex2
= x86_ldub_code(env
, s
);
3187 s
->pc
--; /* rewind the advance_pc() x86_ldub_code() did */
3189 if (!CODE64(s
) && (vex2
& 0xc0) != 0xc0) {
3190 /* 4.1.4.6: In 32-bit mode, bits [7:6] must be 11b,
3191 otherwise the instruction is LES or LDS. */
3194 disas_insn_new(s
, cpu
, b
);
3200 /* Post-process prefixes. */
3202 /* In 64-bit mode, the default data size is 32-bit. Select 64-bit
3203 data with rex_w, and 16-bit data with 0x66; rex_w takes precedence
3204 over 0x66 if both are present. */
3205 dflag
= (REX_W(s
) ? MO_64
: prefixes
& PREFIX_DATA
? MO_16
: MO_32
);
3206 /* In 64-bit mode, 0x67 selects 32-bit addressing. */
3207 aflag
= (prefixes
& PREFIX_ADR
? MO_32
: MO_64
);
3209 /* In 16/32-bit mode, 0x66 selects the opposite data size. */
3210 if (CODE32(s
) ^ ((prefixes
& PREFIX_DATA
) != 0)) {
3215 /* In 16/32-bit mode, 0x67 selects the opposite addressing. */
3216 if (CODE32(s
) ^ ((prefixes
& PREFIX_ADR
) != 0)) {
3223 s
->prefix
= prefixes
;
3227 /* now check op code */
3229 /**************************/
3244 ot
= mo_b_d(b
, dflag
);
3247 case 0: /* OP Ev, Gv */
3248 modrm
= x86_ldub_code(env
, s
);
3249 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
3250 mod
= (modrm
>> 6) & 3;
3251 rm
= (modrm
& 7) | REX_B(s
);
3253 gen_lea_modrm(env
, s
, modrm
);
3255 } else if (op
== OP_XORL
&& rm
== reg
) {
3257 /* xor reg, reg optimisation */
3258 set_cc_op(s
, CC_OP_CLR
);
3259 tcg_gen_movi_tl(s
->T0
, 0);
3260 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
3265 gen_op_mov_v_reg(s
, ot
, s
->T1
, reg
);
3266 gen_op(s
, op
, ot
, opreg
);
3268 case 1: /* OP Gv, Ev */
3269 modrm
= x86_ldub_code(env
, s
);
3270 mod
= (modrm
>> 6) & 3;
3271 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
3272 rm
= (modrm
& 7) | REX_B(s
);
3274 gen_lea_modrm(env
, s
, modrm
);
3275 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
3276 } else if (op
== OP_XORL
&& rm
== reg
) {
3279 gen_op_mov_v_reg(s
, ot
, s
->T1
, rm
);
3281 gen_op(s
, op
, ot
, reg
);
3283 case 2: /* OP A, Iv */
3284 val
= insn_get(env
, s
, ot
);
3285 tcg_gen_movi_tl(s
->T1
, val
);
3286 gen_op(s
, op
, ot
, OR_EAX
);
3296 case 0x80: /* GRP1 */
3302 ot
= mo_b_d(b
, dflag
);
3304 modrm
= x86_ldub_code(env
, s
);
3305 mod
= (modrm
>> 6) & 3;
3306 rm
= (modrm
& 7) | REX_B(s
);
3307 op
= (modrm
>> 3) & 7;
3313 s
->rip_offset
= insn_const_size(ot
);
3314 gen_lea_modrm(env
, s
, modrm
);
3325 val
= insn_get(env
, s
, ot
);
3328 val
= (int8_t)insn_get(env
, s
, MO_8
);
3331 tcg_gen_movi_tl(s
->T1
, val
);
3332 gen_op(s
, op
, ot
, opreg
);
3336 /**************************/
3337 /* inc, dec, and other misc arith */
3338 case 0x40 ... 0x47: /* inc Gv */
3340 gen_inc(s
, ot
, OR_EAX
+ (b
& 7), 1);
3342 case 0x48 ... 0x4f: /* dec Gv */
3344 gen_inc(s
, ot
, OR_EAX
+ (b
& 7), -1);
3346 case 0xf6: /* GRP3 */
3348 ot
= mo_b_d(b
, dflag
);
3350 modrm
= x86_ldub_code(env
, s
);
3351 mod
= (modrm
>> 6) & 3;
3352 rm
= (modrm
& 7) | REX_B(s
);
3353 op
= (modrm
>> 3) & 7;
3356 s
->rip_offset
= insn_const_size(ot
);
3358 gen_lea_modrm(env
, s
, modrm
);
3359 /* For those below that handle locked memory, don't load here. */
3360 if (!(s
->prefix
& PREFIX_LOCK
)
3362 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
3365 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
3370 val
= insn_get(env
, s
, ot
);
3371 tcg_gen_movi_tl(s
->T1
, val
);
3372 gen_op_testl_T0_T1_cc(s
);
3373 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
3376 if (s
->prefix
& PREFIX_LOCK
) {
3380 tcg_gen_movi_tl(s
->T0
, ~0);
3381 tcg_gen_atomic_xor_fetch_tl(s
->T0
, s
->A0
, s
->T0
,
3382 s
->mem_index
, ot
| MO_LE
);
3384 tcg_gen_not_tl(s
->T0
, s
->T0
);
3386 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
3388 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
3393 if (s
->prefix
& PREFIX_LOCK
) {
3395 TCGv a0
, t0
, t1
, t2
;
3402 label1
= gen_new_label();
3404 gen_set_label(label1
);
3405 t1
= tcg_temp_new();
3406 t2
= tcg_temp_new();
3407 tcg_gen_mov_tl(t2
, t0
);
3408 tcg_gen_neg_tl(t1
, t0
);
3409 tcg_gen_atomic_cmpxchg_tl(t0
, a0
, t0
, t1
,
3410 s
->mem_index
, ot
| MO_LE
);
3411 tcg_gen_brcond_tl(TCG_COND_NE
, t0
, t2
, label1
);
3413 tcg_gen_neg_tl(s
->T0
, t0
);
3415 tcg_gen_neg_tl(s
->T0
, s
->T0
);
3417 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
3419 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
3422 gen_op_update_neg_cc(s
);
3423 set_cc_op(s
, CC_OP_SUBB
+ ot
);
3428 gen_op_mov_v_reg(s
, MO_8
, s
->T1
, R_EAX
);
3429 tcg_gen_ext8u_tl(s
->T0
, s
->T0
);
3430 tcg_gen_ext8u_tl(s
->T1
, s
->T1
);
3431 /* XXX: use 32 bit mul which could be faster */
3432 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
3433 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
3434 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
3435 tcg_gen_andi_tl(cpu_cc_src
, s
->T0
, 0xff00);
3436 set_cc_op(s
, CC_OP_MULB
);
3439 gen_op_mov_v_reg(s
, MO_16
, s
->T1
, R_EAX
);
3440 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
3441 tcg_gen_ext16u_tl(s
->T1
, s
->T1
);
3442 /* XXX: use 32 bit mul which could be faster */
3443 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
3444 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
3445 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
3446 tcg_gen_shri_tl(s
->T0
, s
->T0
, 16);
3447 gen_op_mov_reg_v(s
, MO_16
, R_EDX
, s
->T0
);
3448 tcg_gen_mov_tl(cpu_cc_src
, s
->T0
);
3449 set_cc_op(s
, CC_OP_MULW
);
3453 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3454 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, cpu_regs
[R_EAX
]);
3455 tcg_gen_mulu2_i32(s
->tmp2_i32
, s
->tmp3_i32
,
3456 s
->tmp2_i32
, s
->tmp3_i32
);
3457 tcg_gen_extu_i32_tl(cpu_regs
[R_EAX
], s
->tmp2_i32
);
3458 tcg_gen_extu_i32_tl(cpu_regs
[R_EDX
], s
->tmp3_i32
);
3459 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
3460 tcg_gen_mov_tl(cpu_cc_src
, cpu_regs
[R_EDX
]);
3461 set_cc_op(s
, CC_OP_MULL
);
3463 #ifdef TARGET_X86_64
3465 tcg_gen_mulu2_i64(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
],
3466 s
->T0
, cpu_regs
[R_EAX
]);
3467 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
3468 tcg_gen_mov_tl(cpu_cc_src
, cpu_regs
[R_EDX
]);
3469 set_cc_op(s
, CC_OP_MULQ
);
3477 gen_op_mov_v_reg(s
, MO_8
, s
->T1
, R_EAX
);
3478 tcg_gen_ext8s_tl(s
->T0
, s
->T0
);
3479 tcg_gen_ext8s_tl(s
->T1
, s
->T1
);
3480 /* XXX: use 32 bit mul which could be faster */
3481 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
3482 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
3483 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
3484 tcg_gen_ext8s_tl(s
->tmp0
, s
->T0
);
3485 tcg_gen_sub_tl(cpu_cc_src
, s
->T0
, s
->tmp0
);
3486 set_cc_op(s
, CC_OP_MULB
);
3489 gen_op_mov_v_reg(s
, MO_16
, s
->T1
, R_EAX
);
3490 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
3491 tcg_gen_ext16s_tl(s
->T1
, s
->T1
);
3492 /* XXX: use 32 bit mul which could be faster */
3493 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
3494 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
3495 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
3496 tcg_gen_ext16s_tl(s
->tmp0
, s
->T0
);
3497 tcg_gen_sub_tl(cpu_cc_src
, s
->T0
, s
->tmp0
);
3498 tcg_gen_shri_tl(s
->T0
, s
->T0
, 16);
3499 gen_op_mov_reg_v(s
, MO_16
, R_EDX
, s
->T0
);
3500 set_cc_op(s
, CC_OP_MULW
);
3504 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3505 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, cpu_regs
[R_EAX
]);
3506 tcg_gen_muls2_i32(s
->tmp2_i32
, s
->tmp3_i32
,
3507 s
->tmp2_i32
, s
->tmp3_i32
);
3508 tcg_gen_extu_i32_tl(cpu_regs
[R_EAX
], s
->tmp2_i32
);
3509 tcg_gen_extu_i32_tl(cpu_regs
[R_EDX
], s
->tmp3_i32
);
3510 tcg_gen_sari_i32(s
->tmp2_i32
, s
->tmp2_i32
, 31);
3511 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
3512 tcg_gen_sub_i32(s
->tmp2_i32
, s
->tmp2_i32
, s
->tmp3_i32
);
3513 tcg_gen_extu_i32_tl(cpu_cc_src
, s
->tmp2_i32
);
3514 set_cc_op(s
, CC_OP_MULL
);
3516 #ifdef TARGET_X86_64
3518 tcg_gen_muls2_i64(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
],
3519 s
->T0
, cpu_regs
[R_EAX
]);
3520 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[R_EAX
]);
3521 tcg_gen_sari_tl(cpu_cc_src
, cpu_regs
[R_EAX
], 63);
3522 tcg_gen_sub_tl(cpu_cc_src
, cpu_cc_src
, cpu_regs
[R_EDX
]);
3523 set_cc_op(s
, CC_OP_MULQ
);
3531 gen_helper_divb_AL(cpu_env
, s
->T0
);
3534 gen_helper_divw_AX(cpu_env
, s
->T0
);
3538 gen_helper_divl_EAX(cpu_env
, s
->T0
);
3540 #ifdef TARGET_X86_64
3542 gen_helper_divq_EAX(cpu_env
, s
->T0
);
3550 gen_helper_idivb_AL(cpu_env
, s
->T0
);
3553 gen_helper_idivw_AX(cpu_env
, s
->T0
);
3557 gen_helper_idivl_EAX(cpu_env
, s
->T0
);
3559 #ifdef TARGET_X86_64
3561 gen_helper_idivq_EAX(cpu_env
, s
->T0
);
3571 case 0xfe: /* GRP4 */
3572 case 0xff: /* GRP5 */
3573 ot
= mo_b_d(b
, dflag
);
3575 modrm
= x86_ldub_code(env
, s
);
3576 mod
= (modrm
>> 6) & 3;
3577 rm
= (modrm
& 7) | REX_B(s
);
3578 op
= (modrm
>> 3) & 7;
3579 if (op
>= 2 && b
== 0xfe) {
3583 if (op
== 2 || op
== 4) {
3584 /* operand size for jumps is 64 bit */
3586 } else if (op
== 3 || op
== 5) {
3587 ot
= dflag
!= MO_16
? MO_32
+ REX_W(s
) : MO_16
;
3588 } else if (op
== 6) {
3589 /* default push size is 64 bit */
3590 ot
= mo_pushpop(s
, dflag
);
3594 gen_lea_modrm(env
, s
, modrm
);
3595 if (op
>= 2 && op
!= 3 && op
!= 5)
3596 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
3598 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
3602 case 0: /* inc Ev */
3607 gen_inc(s
, ot
, opreg
, 1);
3609 case 1: /* dec Ev */
3614 gen_inc(s
, ot
, opreg
, -1);
3616 case 2: /* call Ev */
3617 /* XXX: optimize if memory (no 'and' is necessary) */
3618 if (dflag
== MO_16
) {
3619 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
3621 gen_push_v(s
, eip_next_tl(s
));
3622 gen_op_jmp_v(s
, s
->T0
);
3624 s
->base
.is_jmp
= DISAS_JUMP
;
3626 case 3: /* lcall Ev */
3630 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
3631 gen_add_A0_im(s
, 1 << ot
);
3632 gen_op_ld_v(s
, MO_16
, s
->T0
, s
->A0
);
3634 if (PE(s
) && !VM86(s
)) {
3635 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3636 gen_helper_lcall_protected(cpu_env
, s
->tmp2_i32
, s
->T1
,
3637 tcg_constant_i32(dflag
- 1),
3640 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3641 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T1
);
3642 gen_helper_lcall_real(cpu_env
, s
->tmp2_i32
, s
->tmp3_i32
,
3643 tcg_constant_i32(dflag
- 1),
3646 s
->base
.is_jmp
= DISAS_JUMP
;
3648 case 4: /* jmp Ev */
3649 if (dflag
== MO_16
) {
3650 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
3652 gen_op_jmp_v(s
, s
->T0
);
3654 s
->base
.is_jmp
= DISAS_JUMP
;
3656 case 5: /* ljmp Ev */
3660 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
3661 gen_add_A0_im(s
, 1 << ot
);
3662 gen_op_ld_v(s
, MO_16
, s
->T0
, s
->A0
);
3664 if (PE(s
) && !VM86(s
)) {
3665 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3666 gen_helper_ljmp_protected(cpu_env
, s
->tmp2_i32
, s
->T1
,
3669 gen_op_movl_seg_T0_vm(s
, R_CS
);
3670 gen_op_jmp_v(s
, s
->T1
);
3672 s
->base
.is_jmp
= DISAS_JUMP
;
3674 case 6: /* push Ev */
3675 gen_push_v(s
, s
->T0
);
3682 case 0x84: /* test Ev, Gv */
3684 ot
= mo_b_d(b
, dflag
);
3686 modrm
= x86_ldub_code(env
, s
);
3687 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
3689 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3690 gen_op_mov_v_reg(s
, ot
, s
->T1
, reg
);
3691 gen_op_testl_T0_T1_cc(s
);
3692 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
3695 case 0xa8: /* test eAX, Iv */
3697 ot
= mo_b_d(b
, dflag
);
3698 val
= insn_get(env
, s
, ot
);
3700 gen_op_mov_v_reg(s
, ot
, s
->T0
, OR_EAX
);
3701 tcg_gen_movi_tl(s
->T1
, val
);
3702 gen_op_testl_T0_T1_cc(s
);
3703 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
3706 case 0x98: /* CWDE/CBW */
3708 #ifdef TARGET_X86_64
3710 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, R_EAX
);
3711 tcg_gen_ext32s_tl(s
->T0
, s
->T0
);
3712 gen_op_mov_reg_v(s
, MO_64
, R_EAX
, s
->T0
);
3716 gen_op_mov_v_reg(s
, MO_16
, s
->T0
, R_EAX
);
3717 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
3718 gen_op_mov_reg_v(s
, MO_32
, R_EAX
, s
->T0
);
3721 gen_op_mov_v_reg(s
, MO_8
, s
->T0
, R_EAX
);
3722 tcg_gen_ext8s_tl(s
->T0
, s
->T0
);
3723 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
3726 g_assert_not_reached();
3729 case 0x99: /* CDQ/CWD */
3731 #ifdef TARGET_X86_64
3733 gen_op_mov_v_reg(s
, MO_64
, s
->T0
, R_EAX
);
3734 tcg_gen_sari_tl(s
->T0
, s
->T0
, 63);
3735 gen_op_mov_reg_v(s
, MO_64
, R_EDX
, s
->T0
);
3739 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, R_EAX
);
3740 tcg_gen_ext32s_tl(s
->T0
, s
->T0
);
3741 tcg_gen_sari_tl(s
->T0
, s
->T0
, 31);
3742 gen_op_mov_reg_v(s
, MO_32
, R_EDX
, s
->T0
);
3745 gen_op_mov_v_reg(s
, MO_16
, s
->T0
, R_EAX
);
3746 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
3747 tcg_gen_sari_tl(s
->T0
, s
->T0
, 15);
3748 gen_op_mov_reg_v(s
, MO_16
, R_EDX
, s
->T0
);
3751 g_assert_not_reached();
3754 case 0x1af: /* imul Gv, Ev */
3755 case 0x69: /* imul Gv, Ev, I */
3758 modrm
= x86_ldub_code(env
, s
);
3759 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
3761 s
->rip_offset
= insn_const_size(ot
);
3764 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
3766 val
= insn_get(env
, s
, ot
);
3767 tcg_gen_movi_tl(s
->T1
, val
);
3768 } else if (b
== 0x6b) {
3769 val
= (int8_t)insn_get(env
, s
, MO_8
);
3770 tcg_gen_movi_tl(s
->T1
, val
);
3772 gen_op_mov_v_reg(s
, ot
, s
->T1
, reg
);
3775 #ifdef TARGET_X86_64
3777 tcg_gen_muls2_i64(cpu_regs
[reg
], s
->T1
, s
->T0
, s
->T1
);
3778 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[reg
]);
3779 tcg_gen_sari_tl(cpu_cc_src
, cpu_cc_dst
, 63);
3780 tcg_gen_sub_tl(cpu_cc_src
, cpu_cc_src
, s
->T1
);
3784 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
3785 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T1
);
3786 tcg_gen_muls2_i32(s
->tmp2_i32
, s
->tmp3_i32
,
3787 s
->tmp2_i32
, s
->tmp3_i32
);
3788 tcg_gen_extu_i32_tl(cpu_regs
[reg
], s
->tmp2_i32
);
3789 tcg_gen_sari_i32(s
->tmp2_i32
, s
->tmp2_i32
, 31);
3790 tcg_gen_mov_tl(cpu_cc_dst
, cpu_regs
[reg
]);
3791 tcg_gen_sub_i32(s
->tmp2_i32
, s
->tmp2_i32
, s
->tmp3_i32
);
3792 tcg_gen_extu_i32_tl(cpu_cc_src
, s
->tmp2_i32
);
3795 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
3796 tcg_gen_ext16s_tl(s
->T1
, s
->T1
);
3797 /* XXX: use 32 bit mul which could be faster */
3798 tcg_gen_mul_tl(s
->T0
, s
->T0
, s
->T1
);
3799 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
3800 tcg_gen_ext16s_tl(s
->tmp0
, s
->T0
);
3801 tcg_gen_sub_tl(cpu_cc_src
, s
->T0
, s
->tmp0
);
3802 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
3805 set_cc_op(s
, CC_OP_MULB
+ ot
);
3808 case 0x1c1: /* xadd Ev, Gv */
3809 ot
= mo_b_d(b
, dflag
);
3810 modrm
= x86_ldub_code(env
, s
);
3811 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
3812 mod
= (modrm
>> 6) & 3;
3813 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
3815 rm
= (modrm
& 7) | REX_B(s
);
3816 gen_op_mov_v_reg(s
, ot
, s
->T1
, rm
);
3817 tcg_gen_add_tl(s
->T0
, s
->T0
, s
->T1
);
3818 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
3819 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
3821 gen_lea_modrm(env
, s
, modrm
);
3822 if (s
->prefix
& PREFIX_LOCK
) {
3823 tcg_gen_atomic_fetch_add_tl(s
->T1
, s
->A0
, s
->T0
,
3824 s
->mem_index
, ot
| MO_LE
);
3825 tcg_gen_add_tl(s
->T0
, s
->T0
, s
->T1
);
3827 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
3828 tcg_gen_add_tl(s
->T0
, s
->T0
, s
->T1
);
3829 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
3831 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
3833 gen_op_update2_cc(s
);
3834 set_cc_op(s
, CC_OP_ADDB
+ ot
);
3837 case 0x1b1: /* cmpxchg Ev, Gv */
3839 TCGv oldv
, newv
, cmpv
, dest
;
3841 ot
= mo_b_d(b
, dflag
);
3842 modrm
= x86_ldub_code(env
, s
);
3843 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
3844 mod
= (modrm
>> 6) & 3;
3845 oldv
= tcg_temp_new();
3846 newv
= tcg_temp_new();
3847 cmpv
= tcg_temp_new();
3848 gen_op_mov_v_reg(s
, ot
, newv
, reg
);
3849 tcg_gen_mov_tl(cmpv
, cpu_regs
[R_EAX
]);
3851 if (s
->prefix
& PREFIX_LOCK
) {
3855 gen_lea_modrm(env
, s
, modrm
);
3856 tcg_gen_atomic_cmpxchg_tl(oldv
, s
->A0
, cmpv
, newv
,
3857 s
->mem_index
, ot
| MO_LE
);
3860 rm
= (modrm
& 7) | REX_B(s
);
3861 gen_op_mov_v_reg(s
, ot
, oldv
, rm
);
3865 * Unlike the memory case, where "the destination operand receives
3866 * a write cycle without regard to the result of the comparison",
3867 * rm must not be touched altogether if the write fails, including
3868 * not zero-extending it on 64-bit processors. So, precompute
3869 * the result of a successful writeback and perform the movcond
3870 * directly on cpu_regs. Also need to write accumulator first, in
3871 * case rm is part of RAX too.
3873 dest
= gen_op_deposit_reg_v(s
, ot
, rm
, newv
, newv
);
3874 tcg_gen_movcond_tl(TCG_COND_EQ
, dest
, oldv
, cmpv
, newv
, dest
);
3876 gen_lea_modrm(env
, s
, modrm
);
3877 gen_op_ld_v(s
, ot
, oldv
, s
->A0
);
3880 * Perform an unconditional store cycle like physical cpu;
3881 * must be before changing accumulator to ensure
3882 * idempotency if the store faults and the instruction
3885 tcg_gen_movcond_tl(TCG_COND_EQ
, newv
, oldv
, cmpv
, newv
, oldv
);
3886 gen_op_st_v(s
, ot
, newv
, s
->A0
);
3890 * Write EAX only if the cmpxchg fails; reuse newv as the destination,
3891 * since it's dead here.
3893 dest
= gen_op_deposit_reg_v(s
, ot
, R_EAX
, newv
, oldv
);
3894 tcg_gen_movcond_tl(TCG_COND_EQ
, dest
, oldv
, cmpv
, dest
, newv
);
3895 tcg_gen_mov_tl(cpu_cc_src
, oldv
);
3896 tcg_gen_mov_tl(s
->cc_srcT
, cmpv
);
3897 tcg_gen_sub_tl(cpu_cc_dst
, cmpv
, oldv
);
3898 set_cc_op(s
, CC_OP_SUBB
+ ot
);
3901 case 0x1c7: /* cmpxchg8b */
3902 modrm
= x86_ldub_code(env
, s
);
3903 mod
= (modrm
>> 6) & 3;
3904 switch ((modrm
>> 3) & 7) {
3905 case 1: /* CMPXCHG8, CMPXCHG16 */
3909 #ifdef TARGET_X86_64
3910 if (dflag
== MO_64
) {
3911 if (!(s
->cpuid_ext_features
& CPUID_EXT_CX16
)) {
3914 gen_cmpxchg16b(s
, env
, modrm
);
3918 if (!(s
->cpuid_features
& CPUID_CX8
)) {
3921 gen_cmpxchg8b(s
, env
, modrm
);
3924 case 7: /* RDSEED */
3925 case 6: /* RDRAND */
3927 (s
->prefix
& (PREFIX_LOCK
| PREFIX_REPZ
| PREFIX_REPNZ
)) ||
3928 !(s
->cpuid_ext_features
& CPUID_EXT_RDRAND
)) {
3931 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
3933 s
->base
.is_jmp
= DISAS_TOO_MANY
;
3935 gen_helper_rdrand(s
->T0
, cpu_env
);
3936 rm
= (modrm
& 7) | REX_B(s
);
3937 gen_op_mov_reg_v(s
, dflag
, rm
, s
->T0
);
3938 set_cc_op(s
, CC_OP_EFLAGS
);
3946 /**************************/
3948 case 0x50 ... 0x57: /* push */
3949 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, (b
& 7) | REX_B(s
));
3950 gen_push_v(s
, s
->T0
);
3952 case 0x58 ... 0x5f: /* pop */
3954 /* NOTE: order is important for pop %sp */
3955 gen_pop_update(s
, ot
);
3956 gen_op_mov_reg_v(s
, ot
, (b
& 7) | REX_B(s
), s
->T0
);
3958 case 0x60: /* pusha */
3963 case 0x61: /* popa */
3968 case 0x68: /* push Iv */
3970 ot
= mo_pushpop(s
, dflag
);
3972 val
= insn_get(env
, s
, ot
);
3974 val
= (int8_t)insn_get(env
, s
, MO_8
);
3975 tcg_gen_movi_tl(s
->T0
, val
);
3976 gen_push_v(s
, s
->T0
);
3978 case 0x8f: /* pop Ev */
3979 modrm
= x86_ldub_code(env
, s
);
3980 mod
= (modrm
>> 6) & 3;
3983 /* NOTE: order is important for pop %sp */
3984 gen_pop_update(s
, ot
);
3985 rm
= (modrm
& 7) | REX_B(s
);
3986 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
3988 /* NOTE: order is important too for MMU exceptions */
3989 s
->popl_esp_hack
= 1 << ot
;
3990 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
3991 s
->popl_esp_hack
= 0;
3992 gen_pop_update(s
, ot
);
3995 case 0xc8: /* enter */
3998 val
= x86_lduw_code(env
, s
);
3999 level
= x86_ldub_code(env
, s
);
4000 gen_enter(s
, val
, level
);
4003 case 0xc9: /* leave */
4006 case 0x06: /* push es */
4007 case 0x0e: /* push cs */
4008 case 0x16: /* push ss */
4009 case 0x1e: /* push ds */
4012 gen_op_movl_T0_seg(s
, b
>> 3);
4013 gen_push_v(s
, s
->T0
);
4015 case 0x1a0: /* push fs */
4016 case 0x1a8: /* push gs */
4017 gen_op_movl_T0_seg(s
, (b
>> 3) & 7);
4018 gen_push_v(s
, s
->T0
);
4020 case 0x07: /* pop es */
4021 case 0x17: /* pop ss */
4022 case 0x1f: /* pop ds */
4027 gen_movl_seg_T0(s
, reg
);
4028 gen_pop_update(s
, ot
);
4030 case 0x1a1: /* pop fs */
4031 case 0x1a9: /* pop gs */
4033 gen_movl_seg_T0(s
, (b
>> 3) & 7);
4034 gen_pop_update(s
, ot
);
4037 /**************************/
4040 case 0x89: /* mov Gv, Ev */
4041 ot
= mo_b_d(b
, dflag
);
4042 modrm
= x86_ldub_code(env
, s
);
4043 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
4045 /* generate a generic store */
4046 gen_ldst_modrm(env
, s
, modrm
, ot
, reg
, 1);
4049 case 0xc7: /* mov Ev, Iv */
4050 ot
= mo_b_d(b
, dflag
);
4051 modrm
= x86_ldub_code(env
, s
);
4052 mod
= (modrm
>> 6) & 3;
4054 s
->rip_offset
= insn_const_size(ot
);
4055 gen_lea_modrm(env
, s
, modrm
);
4057 val
= insn_get(env
, s
, ot
);
4058 tcg_gen_movi_tl(s
->T0
, val
);
4060 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
4062 gen_op_mov_reg_v(s
, ot
, (modrm
& 7) | REX_B(s
), s
->T0
);
4066 case 0x8b: /* mov Ev, Gv */
4067 ot
= mo_b_d(b
, dflag
);
4068 modrm
= x86_ldub_code(env
, s
);
4069 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
4071 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
4072 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
4074 case 0x8e: /* mov seg, Gv */
4075 modrm
= x86_ldub_code(env
, s
);
4076 reg
= (modrm
>> 3) & 7;
4077 if (reg
>= 6 || reg
== R_CS
)
4079 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
4080 gen_movl_seg_T0(s
, reg
);
4082 case 0x8c: /* mov Gv, seg */
4083 modrm
= x86_ldub_code(env
, s
);
4084 reg
= (modrm
>> 3) & 7;
4085 mod
= (modrm
>> 6) & 3;
4088 gen_op_movl_T0_seg(s
, reg
);
4089 ot
= mod
== 3 ? dflag
: MO_16
;
4090 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
4093 case 0x1b6: /* movzbS Gv, Eb */
4094 case 0x1b7: /* movzwS Gv, Eb */
4095 case 0x1be: /* movsbS Gv, Eb */
4096 case 0x1bf: /* movswS Gv, Eb */
4101 /* d_ot is the size of destination */
4103 /* ot is the size of source */
4104 ot
= (b
& 1) + MO_8
;
4105 /* s_ot is the sign+size of source */
4106 s_ot
= b
& 8 ? MO_SIGN
| ot
: ot
;
4108 modrm
= x86_ldub_code(env
, s
);
4109 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
4110 mod
= (modrm
>> 6) & 3;
4111 rm
= (modrm
& 7) | REX_B(s
);
4114 if (s_ot
== MO_SB
&& byte_reg_is_xH(s
, rm
)) {
4115 tcg_gen_sextract_tl(s
->T0
, cpu_regs
[rm
- 4], 8, 8);
4117 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
4120 tcg_gen_ext8u_tl(s
->T0
, s
->T0
);
4123 tcg_gen_ext8s_tl(s
->T0
, s
->T0
);
4126 tcg_gen_ext16u_tl(s
->T0
, s
->T0
);
4130 tcg_gen_ext16s_tl(s
->T0
, s
->T0
);
4134 gen_op_mov_reg_v(s
, d_ot
, reg
, s
->T0
);
4136 gen_lea_modrm(env
, s
, modrm
);
4137 gen_op_ld_v(s
, s_ot
, s
->T0
, s
->A0
);
4138 gen_op_mov_reg_v(s
, d_ot
, reg
, s
->T0
);
4143 case 0x8d: /* lea */
4144 modrm
= x86_ldub_code(env
, s
);
4145 mod
= (modrm
>> 6) & 3;
4148 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
4150 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
4151 TCGv ea
= gen_lea_modrm_1(s
, a
, false);
4152 gen_lea_v_seg(s
, s
->aflag
, ea
, -1, -1);
4153 gen_op_mov_reg_v(s
, dflag
, reg
, s
->A0
);
4157 case 0xa0: /* mov EAX, Ov */
4159 case 0xa2: /* mov Ov, EAX */
4162 target_ulong offset_addr
;
4164 ot
= mo_b_d(b
, dflag
);
4165 offset_addr
= insn_get_addr(env
, s
, s
->aflag
);
4166 tcg_gen_movi_tl(s
->A0
, offset_addr
);
4167 gen_add_A0_ds_seg(s
);
4169 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
4170 gen_op_mov_reg_v(s
, ot
, R_EAX
, s
->T0
);
4172 gen_op_mov_v_reg(s
, ot
, s
->T0
, R_EAX
);
4173 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
4177 case 0xd7: /* xlat */
4178 tcg_gen_mov_tl(s
->A0
, cpu_regs
[R_EBX
]);
4179 tcg_gen_ext8u_tl(s
->T0
, cpu_regs
[R_EAX
]);
4180 tcg_gen_add_tl(s
->A0
, s
->A0
, s
->T0
);
4181 gen_extu(s
->aflag
, s
->A0
);
4182 gen_add_A0_ds_seg(s
);
4183 gen_op_ld_v(s
, MO_8
, s
->T0
, s
->A0
);
4184 gen_op_mov_reg_v(s
, MO_8
, R_EAX
, s
->T0
);
4186 case 0xb0 ... 0xb7: /* mov R, Ib */
4187 val
= insn_get(env
, s
, MO_8
);
4188 tcg_gen_movi_tl(s
->T0
, val
);
4189 gen_op_mov_reg_v(s
, MO_8
, (b
& 7) | REX_B(s
), s
->T0
);
4191 case 0xb8 ... 0xbf: /* mov R, Iv */
4192 #ifdef TARGET_X86_64
4193 if (dflag
== MO_64
) {
4196 tmp
= x86_ldq_code(env
, s
);
4197 reg
= (b
& 7) | REX_B(s
);
4198 tcg_gen_movi_tl(s
->T0
, tmp
);
4199 gen_op_mov_reg_v(s
, MO_64
, reg
, s
->T0
);
4204 val
= insn_get(env
, s
, ot
);
4205 reg
= (b
& 7) | REX_B(s
);
4206 tcg_gen_movi_tl(s
->T0
, val
);
4207 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
4211 case 0x91 ... 0x97: /* xchg R, EAX */
4214 reg
= (b
& 7) | REX_B(s
);
4218 case 0x87: /* xchg Ev, Gv */
4219 ot
= mo_b_d(b
, dflag
);
4220 modrm
= x86_ldub_code(env
, s
);
4221 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
4222 mod
= (modrm
>> 6) & 3;
4224 rm
= (modrm
& 7) | REX_B(s
);
4226 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
4227 gen_op_mov_v_reg(s
, ot
, s
->T1
, rm
);
4228 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
4229 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
4231 gen_lea_modrm(env
, s
, modrm
);
4232 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
4233 /* for xchg, lock is implicit */
4234 tcg_gen_atomic_xchg_tl(s
->T1
, s
->A0
, s
->T0
,
4235 s
->mem_index
, ot
| MO_LE
);
4236 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
4239 case 0xc4: /* les Gv */
4240 /* In CODE64 this is VEX3; see above. */
4243 case 0xc5: /* lds Gv */
4244 /* In CODE64 this is VEX2; see above. */
4247 case 0x1b2: /* lss Gv */
4250 case 0x1b4: /* lfs Gv */
4253 case 0x1b5: /* lgs Gv */
4256 ot
= dflag
!= MO_16
? MO_32
: MO_16
;
4257 modrm
= x86_ldub_code(env
, s
);
4258 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
4259 mod
= (modrm
>> 6) & 3;
4262 gen_lea_modrm(env
, s
, modrm
);
4263 gen_op_ld_v(s
, ot
, s
->T1
, s
->A0
);
4264 gen_add_A0_im(s
, 1 << ot
);
4265 /* load the segment first to handle exceptions properly */
4266 gen_op_ld_v(s
, MO_16
, s
->T0
, s
->A0
);
4267 gen_movl_seg_T0(s
, op
);
4268 /* then put the data */
4269 gen_op_mov_reg_v(s
, ot
, reg
, s
->T1
);
4272 /************************/
4280 ot
= mo_b_d(b
, dflag
);
4281 modrm
= x86_ldub_code(env
, s
);
4282 mod
= (modrm
>> 6) & 3;
4283 op
= (modrm
>> 3) & 7;
4289 gen_lea_modrm(env
, s
, modrm
);
4292 opreg
= (modrm
& 7) | REX_B(s
);
4297 gen_shift(s
, op
, ot
, opreg
, OR_ECX
);
4300 shift
= x86_ldub_code(env
, s
);
4302 gen_shifti(s
, op
, ot
, opreg
, shift
);
4317 case 0x1a4: /* shld imm */
4321 case 0x1a5: /* shld cl */
4325 case 0x1ac: /* shrd imm */
4329 case 0x1ad: /* shrd cl */
4334 modrm
= x86_ldub_code(env
, s
);
4335 mod
= (modrm
>> 6) & 3;
4336 rm
= (modrm
& 7) | REX_B(s
);
4337 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
4339 gen_lea_modrm(env
, s
, modrm
);
4344 gen_op_mov_v_reg(s
, ot
, s
->T1
, reg
);
4347 TCGv imm
= tcg_constant_tl(x86_ldub_code(env
, s
));
4348 gen_shiftd_rm_T1(s
, ot
, opreg
, op
, imm
);
4350 gen_shiftd_rm_T1(s
, ot
, opreg
, op
, cpu_regs
[R_ECX
]);
4354 /************************/
4358 bool update_fip
= true;
4360 if (s
->flags
& (HF_EM_MASK
| HF_TS_MASK
)) {
4361 /* if CR0.EM or CR0.TS are set, generate an FPU exception */
4362 /* XXX: what to do if illegal op ? */
4363 gen_exception(s
, EXCP07_PREX
);
4366 modrm
= x86_ldub_code(env
, s
);
4367 mod
= (modrm
>> 6) & 3;
4369 op
= ((b
& 7) << 3) | ((modrm
>> 3) & 7);
4372 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
4373 TCGv ea
= gen_lea_modrm_1(s
, a
, false);
4374 TCGv last_addr
= tcg_temp_new();
4375 bool update_fdp
= true;
4377 tcg_gen_mov_tl(last_addr
, ea
);
4378 gen_lea_v_seg(s
, s
->aflag
, ea
, a
.def_seg
, s
->override
);
4381 case 0x00 ... 0x07: /* fxxxs */
4382 case 0x10 ... 0x17: /* fixxxl */
4383 case 0x20 ... 0x27: /* fxxxl */
4384 case 0x30 ... 0x37: /* fixxx */
4391 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
4392 s
->mem_index
, MO_LEUL
);
4393 gen_helper_flds_FT0(cpu_env
, s
->tmp2_i32
);
4396 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
4397 s
->mem_index
, MO_LEUL
);
4398 gen_helper_fildl_FT0(cpu_env
, s
->tmp2_i32
);
4401 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
,
4402 s
->mem_index
, MO_LEUQ
);
4403 gen_helper_fldl_FT0(cpu_env
, s
->tmp1_i64
);
4407 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
4408 s
->mem_index
, MO_LESW
);
4409 gen_helper_fildl_FT0(cpu_env
, s
->tmp2_i32
);
4413 gen_helper_fp_arith_ST0_FT0(op1
);
4415 /* fcomp needs pop */
4416 gen_helper_fpop(cpu_env
);
4420 case 0x08: /* flds */
4421 case 0x0a: /* fsts */
4422 case 0x0b: /* fstps */
4423 case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
4424 case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
4425 case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
4430 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
4431 s
->mem_index
, MO_LEUL
);
4432 gen_helper_flds_ST0(cpu_env
, s
->tmp2_i32
);
4435 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
4436 s
->mem_index
, MO_LEUL
);
4437 gen_helper_fildl_ST0(cpu_env
, s
->tmp2_i32
);
4440 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
,
4441 s
->mem_index
, MO_LEUQ
);
4442 gen_helper_fldl_ST0(cpu_env
, s
->tmp1_i64
);
4446 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
4447 s
->mem_index
, MO_LESW
);
4448 gen_helper_fildl_ST0(cpu_env
, s
->tmp2_i32
);
4453 /* XXX: the corresponding CPUID bit must be tested ! */
4456 gen_helper_fisttl_ST0(s
->tmp2_i32
, cpu_env
);
4457 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
4458 s
->mem_index
, MO_LEUL
);
4461 gen_helper_fisttll_ST0(s
->tmp1_i64
, cpu_env
);
4462 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
,
4463 s
->mem_index
, MO_LEUQ
);
4467 gen_helper_fistt_ST0(s
->tmp2_i32
, cpu_env
);
4468 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
4469 s
->mem_index
, MO_LEUW
);
4472 gen_helper_fpop(cpu_env
);
4477 gen_helper_fsts_ST0(s
->tmp2_i32
, cpu_env
);
4478 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
4479 s
->mem_index
, MO_LEUL
);
4482 gen_helper_fistl_ST0(s
->tmp2_i32
, cpu_env
);
4483 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
4484 s
->mem_index
, MO_LEUL
);
4487 gen_helper_fstl_ST0(s
->tmp1_i64
, cpu_env
);
4488 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
,
4489 s
->mem_index
, MO_LEUQ
);
4493 gen_helper_fist_ST0(s
->tmp2_i32
, cpu_env
);
4494 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
4495 s
->mem_index
, MO_LEUW
);
4498 if ((op
& 7) == 3) {
4499 gen_helper_fpop(cpu_env
);
4504 case 0x0c: /* fldenv mem */
4505 gen_helper_fldenv(cpu_env
, s
->A0
,
4506 tcg_constant_i32(dflag
- 1));
4507 update_fip
= update_fdp
= false;
4509 case 0x0d: /* fldcw mem */
4510 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
,
4511 s
->mem_index
, MO_LEUW
);
4512 gen_helper_fldcw(cpu_env
, s
->tmp2_i32
);
4513 update_fip
= update_fdp
= false;
4515 case 0x0e: /* fnstenv mem */
4516 gen_helper_fstenv(cpu_env
, s
->A0
,
4517 tcg_constant_i32(dflag
- 1));
4518 update_fip
= update_fdp
= false;
4520 case 0x0f: /* fnstcw mem */
4521 gen_helper_fnstcw(s
->tmp2_i32
, cpu_env
);
4522 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
4523 s
->mem_index
, MO_LEUW
);
4524 update_fip
= update_fdp
= false;
4526 case 0x1d: /* fldt mem */
4527 gen_helper_fldt_ST0(cpu_env
, s
->A0
);
4529 case 0x1f: /* fstpt mem */
4530 gen_helper_fstt_ST0(cpu_env
, s
->A0
);
4531 gen_helper_fpop(cpu_env
);
4533 case 0x2c: /* frstor mem */
4534 gen_helper_frstor(cpu_env
, s
->A0
,
4535 tcg_constant_i32(dflag
- 1));
4536 update_fip
= update_fdp
= false;
4538 case 0x2e: /* fnsave mem */
4539 gen_helper_fsave(cpu_env
, s
->A0
,
4540 tcg_constant_i32(dflag
- 1));
4541 update_fip
= update_fdp
= false;
4543 case 0x2f: /* fnstsw mem */
4544 gen_helper_fnstsw(s
->tmp2_i32
, cpu_env
);
4545 tcg_gen_qemu_st_i32(s
->tmp2_i32
, s
->A0
,
4546 s
->mem_index
, MO_LEUW
);
4547 update_fip
= update_fdp
= false;
4549 case 0x3c: /* fbld */
4550 gen_helper_fbld_ST0(cpu_env
, s
->A0
);
4552 case 0x3e: /* fbstp */
4553 gen_helper_fbst_ST0(cpu_env
, s
->A0
);
4554 gen_helper_fpop(cpu_env
);
4556 case 0x3d: /* fildll */
4557 tcg_gen_qemu_ld_i64(s
->tmp1_i64
, s
->A0
,
4558 s
->mem_index
, MO_LEUQ
);
4559 gen_helper_fildll_ST0(cpu_env
, s
->tmp1_i64
);
4561 case 0x3f: /* fistpll */
4562 gen_helper_fistll_ST0(s
->tmp1_i64
, cpu_env
);
4563 tcg_gen_qemu_st_i64(s
->tmp1_i64
, s
->A0
,
4564 s
->mem_index
, MO_LEUQ
);
4565 gen_helper_fpop(cpu_env
);
4572 int last_seg
= s
->override
>= 0 ? s
->override
: a
.def_seg
;
4574 tcg_gen_ld_i32(s
->tmp2_i32
, cpu_env
,
4575 offsetof(CPUX86State
,
4576 segs
[last_seg
].selector
));
4577 tcg_gen_st16_i32(s
->tmp2_i32
, cpu_env
,
4578 offsetof(CPUX86State
, fpds
));
4579 tcg_gen_st_tl(last_addr
, cpu_env
,
4580 offsetof(CPUX86State
, fpdp
));
4583 /* register float ops */
4587 case 0x08: /* fld sti */
4588 gen_helper_fpush(cpu_env
);
4589 gen_helper_fmov_ST0_STN(cpu_env
,
4590 tcg_constant_i32((opreg
+ 1) & 7));
4592 case 0x09: /* fxchg sti */
4593 case 0x29: /* fxchg4 sti, undocumented op */
4594 case 0x39: /* fxchg7 sti, undocumented op */
4595 gen_helper_fxchg_ST0_STN(cpu_env
, tcg_constant_i32(opreg
));
4597 case 0x0a: /* grp d9/2 */
4600 /* check exceptions (FreeBSD FPU probe) */
4601 gen_helper_fwait(cpu_env
);
4608 case 0x0c: /* grp d9/4 */
4611 gen_helper_fchs_ST0(cpu_env
);
4614 gen_helper_fabs_ST0(cpu_env
);
4617 gen_helper_fldz_FT0(cpu_env
);
4618 gen_helper_fcom_ST0_FT0(cpu_env
);
4621 gen_helper_fxam_ST0(cpu_env
);
4627 case 0x0d: /* grp d9/5 */
4631 gen_helper_fpush(cpu_env
);
4632 gen_helper_fld1_ST0(cpu_env
);
4635 gen_helper_fpush(cpu_env
);
4636 gen_helper_fldl2t_ST0(cpu_env
);
4639 gen_helper_fpush(cpu_env
);
4640 gen_helper_fldl2e_ST0(cpu_env
);
4643 gen_helper_fpush(cpu_env
);
4644 gen_helper_fldpi_ST0(cpu_env
);
4647 gen_helper_fpush(cpu_env
);
4648 gen_helper_fldlg2_ST0(cpu_env
);
4651 gen_helper_fpush(cpu_env
);
4652 gen_helper_fldln2_ST0(cpu_env
);
4655 gen_helper_fpush(cpu_env
);
4656 gen_helper_fldz_ST0(cpu_env
);
4663 case 0x0e: /* grp d9/6 */
4666 gen_helper_f2xm1(cpu_env
);
4669 gen_helper_fyl2x(cpu_env
);
4672 gen_helper_fptan(cpu_env
);
4674 case 3: /* fpatan */
4675 gen_helper_fpatan(cpu_env
);
4677 case 4: /* fxtract */
4678 gen_helper_fxtract(cpu_env
);
4680 case 5: /* fprem1 */
4681 gen_helper_fprem1(cpu_env
);
4683 case 6: /* fdecstp */
4684 gen_helper_fdecstp(cpu_env
);
4687 case 7: /* fincstp */
4688 gen_helper_fincstp(cpu_env
);
4692 case 0x0f: /* grp d9/7 */
4695 gen_helper_fprem(cpu_env
);
4697 case 1: /* fyl2xp1 */
4698 gen_helper_fyl2xp1(cpu_env
);
4701 gen_helper_fsqrt(cpu_env
);
4703 case 3: /* fsincos */
4704 gen_helper_fsincos(cpu_env
);
4706 case 5: /* fscale */
4707 gen_helper_fscale(cpu_env
);
4709 case 4: /* frndint */
4710 gen_helper_frndint(cpu_env
);
4713 gen_helper_fsin(cpu_env
);
4717 gen_helper_fcos(cpu_env
);
4721 case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
4722 case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
4723 case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
4729 gen_helper_fp_arith_STN_ST0(op1
, opreg
);
4731 gen_helper_fpop(cpu_env
);
4734 gen_helper_fmov_FT0_STN(cpu_env
,
4735 tcg_constant_i32(opreg
));
4736 gen_helper_fp_arith_ST0_FT0(op1
);
4740 case 0x02: /* fcom */
4741 case 0x22: /* fcom2, undocumented op */
4742 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(opreg
));
4743 gen_helper_fcom_ST0_FT0(cpu_env
);
4745 case 0x03: /* fcomp */
4746 case 0x23: /* fcomp3, undocumented op */
4747 case 0x32: /* fcomp5, undocumented op */
4748 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(opreg
));
4749 gen_helper_fcom_ST0_FT0(cpu_env
);
4750 gen_helper_fpop(cpu_env
);
4752 case 0x15: /* da/5 */
4754 case 1: /* fucompp */
4755 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(1));
4756 gen_helper_fucom_ST0_FT0(cpu_env
);
4757 gen_helper_fpop(cpu_env
);
4758 gen_helper_fpop(cpu_env
);
4766 case 0: /* feni (287 only, just do nop here) */
4768 case 1: /* fdisi (287 only, just do nop here) */
4771 gen_helper_fclex(cpu_env
);
4774 case 3: /* fninit */
4775 gen_helper_fninit(cpu_env
);
4778 case 4: /* fsetpm (287 only, just do nop here) */
4784 case 0x1d: /* fucomi */
4785 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
4788 gen_update_cc_op(s
);
4789 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(opreg
));
4790 gen_helper_fucomi_ST0_FT0(cpu_env
);
4791 set_cc_op(s
, CC_OP_EFLAGS
);
4793 case 0x1e: /* fcomi */
4794 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
4797 gen_update_cc_op(s
);
4798 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(opreg
));
4799 gen_helper_fcomi_ST0_FT0(cpu_env
);
4800 set_cc_op(s
, CC_OP_EFLAGS
);
4802 case 0x28: /* ffree sti */
4803 gen_helper_ffree_STN(cpu_env
, tcg_constant_i32(opreg
));
4805 case 0x2a: /* fst sti */
4806 gen_helper_fmov_STN_ST0(cpu_env
, tcg_constant_i32(opreg
));
4808 case 0x2b: /* fstp sti */
4809 case 0x0b: /* fstp1 sti, undocumented op */
4810 case 0x3a: /* fstp8 sti, undocumented op */
4811 case 0x3b: /* fstp9 sti, undocumented op */
4812 gen_helper_fmov_STN_ST0(cpu_env
, tcg_constant_i32(opreg
));
4813 gen_helper_fpop(cpu_env
);
4815 case 0x2c: /* fucom st(i) */
4816 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(opreg
));
4817 gen_helper_fucom_ST0_FT0(cpu_env
);
4819 case 0x2d: /* fucomp st(i) */
4820 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(opreg
));
4821 gen_helper_fucom_ST0_FT0(cpu_env
);
4822 gen_helper_fpop(cpu_env
);
4824 case 0x33: /* de/3 */
4826 case 1: /* fcompp */
4827 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(1));
4828 gen_helper_fcom_ST0_FT0(cpu_env
);
4829 gen_helper_fpop(cpu_env
);
4830 gen_helper_fpop(cpu_env
);
4836 case 0x38: /* ffreep sti, undocumented op */
4837 gen_helper_ffree_STN(cpu_env
, tcg_constant_i32(opreg
));
4838 gen_helper_fpop(cpu_env
);
4840 case 0x3c: /* df/4 */
4843 gen_helper_fnstsw(s
->tmp2_i32
, cpu_env
);
4844 tcg_gen_extu_i32_tl(s
->T0
, s
->tmp2_i32
);
4845 gen_op_mov_reg_v(s
, MO_16
, R_EAX
, s
->T0
);
4851 case 0x3d: /* fucomip */
4852 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
4855 gen_update_cc_op(s
);
4856 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(opreg
));
4857 gen_helper_fucomi_ST0_FT0(cpu_env
);
4858 gen_helper_fpop(cpu_env
);
4859 set_cc_op(s
, CC_OP_EFLAGS
);
4861 case 0x3e: /* fcomip */
4862 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
4865 gen_update_cc_op(s
);
4866 gen_helper_fmov_FT0_STN(cpu_env
, tcg_constant_i32(opreg
));
4867 gen_helper_fcomi_ST0_FT0(cpu_env
);
4868 gen_helper_fpop(cpu_env
);
4869 set_cc_op(s
, CC_OP_EFLAGS
);
4871 case 0x10 ... 0x13: /* fcmovxx */
4876 static const uint8_t fcmov_cc
[8] = {
4883 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
4886 op1
= fcmov_cc
[op
& 3] | (((op
>> 3) & 1) ^ 1);
4887 l1
= gen_new_label();
4888 gen_jcc1_noeob(s
, op1
, l1
);
4889 gen_helper_fmov_ST0_STN(cpu_env
,
4890 tcg_constant_i32(opreg
));
4900 tcg_gen_ld_i32(s
->tmp2_i32
, cpu_env
,
4901 offsetof(CPUX86State
, segs
[R_CS
].selector
));
4902 tcg_gen_st16_i32(s
->tmp2_i32
, cpu_env
,
4903 offsetof(CPUX86State
, fpcs
));
4904 tcg_gen_st_tl(eip_cur_tl(s
),
4905 cpu_env
, offsetof(CPUX86State
, fpip
));
4909 /************************/
4912 case 0xa4: /* movsS */
4914 ot
= mo_b_d(b
, dflag
);
4915 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
4916 gen_repz_movs(s
, ot
);
4922 case 0xaa: /* stosS */
4924 ot
= mo_b_d(b
, dflag
);
4925 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
4926 gen_repz_stos(s
, ot
);
4931 case 0xac: /* lodsS */
4933 ot
= mo_b_d(b
, dflag
);
4934 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
4935 gen_repz_lods(s
, ot
);
4940 case 0xae: /* scasS */
4942 ot
= mo_b_d(b
, dflag
);
4943 if (prefixes
& PREFIX_REPNZ
) {
4944 gen_repz_scas(s
, ot
, 1);
4945 } else if (prefixes
& PREFIX_REPZ
) {
4946 gen_repz_scas(s
, ot
, 0);
4952 case 0xa6: /* cmpsS */
4954 ot
= mo_b_d(b
, dflag
);
4955 if (prefixes
& PREFIX_REPNZ
) {
4956 gen_repz_cmps(s
, ot
, 1);
4957 } else if (prefixes
& PREFIX_REPZ
) {
4958 gen_repz_cmps(s
, ot
, 0);
4963 case 0x6c: /* insS */
4965 ot
= mo_b_d32(b
, dflag
);
4966 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_EDX
]);
4967 tcg_gen_ext16u_i32(s
->tmp2_i32
, s
->tmp2_i32
);
4968 if (!gen_check_io(s
, ot
, s
->tmp2_i32
,
4969 SVM_IOIO_TYPE_MASK
| SVM_IOIO_STR_MASK
)) {
4972 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
4974 s
->base
.is_jmp
= DISAS_TOO_MANY
;
4976 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
4977 gen_repz_ins(s
, ot
);
4982 case 0x6e: /* outsS */
4984 ot
= mo_b_d32(b
, dflag
);
4985 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_EDX
]);
4986 tcg_gen_ext16u_i32(s
->tmp2_i32
, s
->tmp2_i32
);
4987 if (!gen_check_io(s
, ot
, s
->tmp2_i32
, SVM_IOIO_STR_MASK
)) {
4990 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
4992 s
->base
.is_jmp
= DISAS_TOO_MANY
;
4994 if (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
)) {
4995 gen_repz_outs(s
, ot
);
5001 /************************/
5006 ot
= mo_b_d32(b
, dflag
);
5007 val
= x86_ldub_code(env
, s
);
5008 tcg_gen_movi_i32(s
->tmp2_i32
, val
);
5009 if (!gen_check_io(s
, ot
, s
->tmp2_i32
, SVM_IOIO_TYPE_MASK
)) {
5012 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
5014 s
->base
.is_jmp
= DISAS_TOO_MANY
;
5016 gen_helper_in_func(ot
, s
->T1
, s
->tmp2_i32
);
5017 gen_op_mov_reg_v(s
, ot
, R_EAX
, s
->T1
);
5018 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
5022 ot
= mo_b_d32(b
, dflag
);
5023 val
= x86_ldub_code(env
, s
);
5024 tcg_gen_movi_i32(s
->tmp2_i32
, val
);
5025 if (!gen_check_io(s
, ot
, s
->tmp2_i32
, 0)) {
5028 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
5030 s
->base
.is_jmp
= DISAS_TOO_MANY
;
5032 gen_op_mov_v_reg(s
, ot
, s
->T1
, R_EAX
);
5033 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T1
);
5034 gen_helper_out_func(ot
, s
->tmp2_i32
, s
->tmp3_i32
);
5035 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
5039 ot
= mo_b_d32(b
, dflag
);
5040 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_EDX
]);
5041 tcg_gen_ext16u_i32(s
->tmp2_i32
, s
->tmp2_i32
);
5042 if (!gen_check_io(s
, ot
, s
->tmp2_i32
, SVM_IOIO_TYPE_MASK
)) {
5045 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
5047 s
->base
.is_jmp
= DISAS_TOO_MANY
;
5049 gen_helper_in_func(ot
, s
->T1
, s
->tmp2_i32
);
5050 gen_op_mov_reg_v(s
, ot
, R_EAX
, s
->T1
);
5051 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
5055 ot
= mo_b_d32(b
, dflag
);
5056 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_EDX
]);
5057 tcg_gen_ext16u_i32(s
->tmp2_i32
, s
->tmp2_i32
);
5058 if (!gen_check_io(s
, ot
, s
->tmp2_i32
, 0)) {
5061 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
5063 s
->base
.is_jmp
= DISAS_TOO_MANY
;
5065 gen_op_mov_v_reg(s
, ot
, s
->T1
, R_EAX
);
5066 tcg_gen_trunc_tl_i32(s
->tmp3_i32
, s
->T1
);
5067 gen_helper_out_func(ot
, s
->tmp2_i32
, s
->tmp3_i32
);
5068 gen_bpt_io(s
, s
->tmp2_i32
, ot
);
5071 /************************/
5073 case 0xc2: /* ret im */
5074 val
= x86_ldsw_code(env
, s
);
5076 gen_stack_update(s
, val
+ (1 << ot
));
5077 /* Note that gen_pop_T0 uses a zero-extending load. */
5078 gen_op_jmp_v(s
, s
->T0
);
5080 s
->base
.is_jmp
= DISAS_JUMP
;
5082 case 0xc3: /* ret */
5084 gen_pop_update(s
, ot
);
5085 /* Note that gen_pop_T0 uses a zero-extending load. */
5086 gen_op_jmp_v(s
, s
->T0
);
5088 s
->base
.is_jmp
= DISAS_JUMP
;
5090 case 0xca: /* lret im */
5091 val
= x86_ldsw_code(env
, s
);
5093 if (PE(s
) && !VM86(s
)) {
5094 gen_update_cc_op(s
);
5095 gen_update_eip_cur(s
);
5096 gen_helper_lret_protected(cpu_env
, tcg_constant_i32(dflag
- 1),
5097 tcg_constant_i32(val
));
5101 gen_op_ld_v(s
, dflag
, s
->T0
, s
->A0
);
5102 /* NOTE: keeping EIP updated is not a problem in case of
5104 gen_op_jmp_v(s
, s
->T0
);
5106 gen_add_A0_im(s
, 1 << dflag
);
5107 gen_op_ld_v(s
, dflag
, s
->T0
, s
->A0
);
5108 gen_op_movl_seg_T0_vm(s
, R_CS
);
5109 /* add stack offset */
5110 gen_stack_update(s
, val
+ (2 << dflag
));
5112 s
->base
.is_jmp
= DISAS_EOB_ONLY
;
5114 case 0xcb: /* lret */
5117 case 0xcf: /* iret */
5118 gen_svm_check_intercept(s
, SVM_EXIT_IRET
);
5119 if (!PE(s
) || VM86(s
)) {
5120 /* real mode or vm86 mode */
5121 if (!check_vm86_iopl(s
)) {
5124 gen_helper_iret_real(cpu_env
, tcg_constant_i32(dflag
- 1));
5126 gen_helper_iret_protected(cpu_env
, tcg_constant_i32(dflag
- 1),
5129 set_cc_op(s
, CC_OP_EFLAGS
);
5130 s
->base
.is_jmp
= DISAS_EOB_ONLY
;
5132 case 0xe8: /* call im */
5134 int diff
= (dflag
!= MO_16
5135 ? (int32_t)insn_get(env
, s
, MO_32
)
5136 : (int16_t)insn_get(env
, s
, MO_16
));
5137 gen_push_v(s
, eip_next_tl(s
));
5139 gen_jmp_rel(s
, dflag
, diff
, 0);
5142 case 0x9a: /* lcall im */
5144 unsigned int selector
, offset
;
5149 offset
= insn_get(env
, s
, ot
);
5150 selector
= insn_get(env
, s
, MO_16
);
5152 tcg_gen_movi_tl(s
->T0
, selector
);
5153 tcg_gen_movi_tl(s
->T1
, offset
);
5156 case 0xe9: /* jmp im */
5158 int diff
= (dflag
!= MO_16
5159 ? (int32_t)insn_get(env
, s
, MO_32
)
5160 : (int16_t)insn_get(env
, s
, MO_16
));
5162 gen_jmp_rel(s
, dflag
, diff
, 0);
5165 case 0xea: /* ljmp im */
5167 unsigned int selector
, offset
;
5172 offset
= insn_get(env
, s
, ot
);
5173 selector
= insn_get(env
, s
, MO_16
);
5175 tcg_gen_movi_tl(s
->T0
, selector
);
5176 tcg_gen_movi_tl(s
->T1
, offset
);
5179 case 0xeb: /* jmp Jb */
5181 int diff
= (int8_t)insn_get(env
, s
, MO_8
);
5182 gen_jmp_rel(s
, dflag
, diff
, 0);
5185 case 0x70 ... 0x7f: /* jcc Jb */
5187 int diff
= (int8_t)insn_get(env
, s
, MO_8
);
5189 gen_jcc(s
, b
, diff
);
5192 case 0x180 ... 0x18f: /* jcc Jv */
5194 int diff
= (dflag
!= MO_16
5195 ? (int32_t)insn_get(env
, s
, MO_32
)
5196 : (int16_t)insn_get(env
, s
, MO_16
));
5198 gen_jcc(s
, b
, diff
);
5202 case 0x190 ... 0x19f: /* setcc Gv */
5203 modrm
= x86_ldub_code(env
, s
);
5204 gen_setcc1(s
, b
, s
->T0
);
5205 gen_ldst_modrm(env
, s
, modrm
, MO_8
, OR_TMP0
, 1);
5207 case 0x140 ... 0x14f: /* cmov Gv, Ev */
5208 if (!(s
->cpuid_features
& CPUID_CMOV
)) {
5212 modrm
= x86_ldub_code(env
, s
);
5213 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
5214 gen_cmovcc1(env
, s
, ot
, b
, modrm
, reg
);
5217 /************************/
5219 case 0x9c: /* pushf */
5220 gen_svm_check_intercept(s
, SVM_EXIT_PUSHF
);
5221 if (check_vm86_iopl(s
)) {
5222 gen_update_cc_op(s
);
5223 gen_helper_read_eflags(s
->T0
, cpu_env
);
5224 gen_push_v(s
, s
->T0
);
5227 case 0x9d: /* popf */
5228 gen_svm_check_intercept(s
, SVM_EXIT_POPF
);
5229 if (check_vm86_iopl(s
)) {
5230 int mask
= TF_MASK
| AC_MASK
| ID_MASK
| NT_MASK
;
5233 mask
|= IF_MASK
| IOPL_MASK
;
5234 } else if (CPL(s
) <= IOPL(s
)) {
5237 if (dflag
== MO_16
) {
5242 gen_helper_write_eflags(cpu_env
, s
->T0
, tcg_constant_i32(mask
));
5243 gen_pop_update(s
, ot
);
5244 set_cc_op(s
, CC_OP_EFLAGS
);
5245 /* abort translation because TF/AC flag may change */
5246 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
5249 case 0x9e: /* sahf */
5250 if (CODE64(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_LAHF_LM
))
5252 tcg_gen_shri_tl(s
->T0
, cpu_regs
[R_EAX
], 8);
5253 gen_compute_eflags(s
);
5254 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, CC_O
);
5255 tcg_gen_andi_tl(s
->T0
, s
->T0
, CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
5256 tcg_gen_or_tl(cpu_cc_src
, cpu_cc_src
, s
->T0
);
5258 case 0x9f: /* lahf */
5259 if (CODE64(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_LAHF_LM
))
5261 gen_compute_eflags(s
);
5262 /* Note: gen_compute_eflags() only gives the condition codes */
5263 tcg_gen_ori_tl(s
->T0
, cpu_cc_src
, 0x02);
5264 tcg_gen_deposit_tl(cpu_regs
[R_EAX
], cpu_regs
[R_EAX
], s
->T0
, 8, 8);
5266 case 0xf5: /* cmc */
5267 gen_compute_eflags(s
);
5268 tcg_gen_xori_tl(cpu_cc_src
, cpu_cc_src
, CC_C
);
5270 case 0xf8: /* clc */
5271 gen_compute_eflags(s
);
5272 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, ~CC_C
);
5274 case 0xf9: /* stc */
5275 gen_compute_eflags(s
);
5276 tcg_gen_ori_tl(cpu_cc_src
, cpu_cc_src
, CC_C
);
5278 case 0xfc: /* cld */
5279 tcg_gen_movi_i32(s
->tmp2_i32
, 1);
5280 tcg_gen_st_i32(s
->tmp2_i32
, cpu_env
, offsetof(CPUX86State
, df
));
5282 case 0xfd: /* std */
5283 tcg_gen_movi_i32(s
->tmp2_i32
, -1);
5284 tcg_gen_st_i32(s
->tmp2_i32
, cpu_env
, offsetof(CPUX86State
, df
));
5287 /************************/
5288 /* bit operations */
5289 case 0x1ba: /* bt/bts/btr/btc Gv, im */
5291 modrm
= x86_ldub_code(env
, s
);
5292 op
= (modrm
>> 3) & 7;
5293 mod
= (modrm
>> 6) & 3;
5294 rm
= (modrm
& 7) | REX_B(s
);
5297 gen_lea_modrm(env
, s
, modrm
);
5298 if (!(s
->prefix
& PREFIX_LOCK
)) {
5299 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
5302 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
5305 val
= x86_ldub_code(env
, s
);
5306 tcg_gen_movi_tl(s
->T1
, val
);
5311 case 0x1a3: /* bt Gv, Ev */
5314 case 0x1ab: /* bts */
5317 case 0x1b3: /* btr */
5320 case 0x1bb: /* btc */
5324 modrm
= x86_ldub_code(env
, s
);
5325 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
5326 mod
= (modrm
>> 6) & 3;
5327 rm
= (modrm
& 7) | REX_B(s
);
5328 gen_op_mov_v_reg(s
, MO_32
, s
->T1
, reg
);
5330 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
5331 /* specific case: we need to add a displacement */
5332 gen_exts(ot
, s
->T1
);
5333 tcg_gen_sari_tl(s
->tmp0
, s
->T1
, 3 + ot
);
5334 tcg_gen_shli_tl(s
->tmp0
, s
->tmp0
, ot
);
5335 tcg_gen_add_tl(s
->A0
, gen_lea_modrm_1(s
, a
, false), s
->tmp0
);
5336 gen_lea_v_seg(s
, s
->aflag
, s
->A0
, a
.def_seg
, s
->override
);
5337 if (!(s
->prefix
& PREFIX_LOCK
)) {
5338 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
5341 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
5344 tcg_gen_andi_tl(s
->T1
, s
->T1
, (1 << (3 + ot
)) - 1);
5345 tcg_gen_movi_tl(s
->tmp0
, 1);
5346 tcg_gen_shl_tl(s
->tmp0
, s
->tmp0
, s
->T1
);
5347 if (s
->prefix
& PREFIX_LOCK
) {
5350 /* Needs no atomic ops; we surpressed the normal
5351 memory load for LOCK above so do it now. */
5352 gen_op_ld_v(s
, ot
, s
->T0
, s
->A0
);
5355 tcg_gen_atomic_fetch_or_tl(s
->T0
, s
->A0
, s
->tmp0
,
5356 s
->mem_index
, ot
| MO_LE
);
5359 tcg_gen_not_tl(s
->tmp0
, s
->tmp0
);
5360 tcg_gen_atomic_fetch_and_tl(s
->T0
, s
->A0
, s
->tmp0
,
5361 s
->mem_index
, ot
| MO_LE
);
5365 tcg_gen_atomic_fetch_xor_tl(s
->T0
, s
->A0
, s
->tmp0
,
5366 s
->mem_index
, ot
| MO_LE
);
5369 tcg_gen_shr_tl(s
->tmp4
, s
->T0
, s
->T1
);
5371 tcg_gen_shr_tl(s
->tmp4
, s
->T0
, s
->T1
);
5374 /* Data already loaded; nothing to do. */
5377 tcg_gen_or_tl(s
->T0
, s
->T0
, s
->tmp0
);
5380 tcg_gen_andc_tl(s
->T0
, s
->T0
, s
->tmp0
);
5384 tcg_gen_xor_tl(s
->T0
, s
->T0
, s
->tmp0
);
5389 gen_op_st_v(s
, ot
, s
->T0
, s
->A0
);
5391 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
5396 /* Delay all CC updates until after the store above. Note that
5397 C is the result of the test, Z is unchanged, and the others
5398 are all undefined. */
5400 case CC_OP_MULB
... CC_OP_MULQ
:
5401 case CC_OP_ADDB
... CC_OP_ADDQ
:
5402 case CC_OP_ADCB
... CC_OP_ADCQ
:
5403 case CC_OP_SUBB
... CC_OP_SUBQ
:
5404 case CC_OP_SBBB
... CC_OP_SBBQ
:
5405 case CC_OP_LOGICB
... CC_OP_LOGICQ
:
5406 case CC_OP_INCB
... CC_OP_INCQ
:
5407 case CC_OP_DECB
... CC_OP_DECQ
:
5408 case CC_OP_SHLB
... CC_OP_SHLQ
:
5409 case CC_OP_SARB
... CC_OP_SARQ
:
5410 case CC_OP_BMILGB
... CC_OP_BMILGQ
:
5411 /* Z was going to be computed from the non-zero status of CC_DST.
5412 We can get that same Z value (and the new C value) by leaving
5413 CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
5415 tcg_gen_mov_tl(cpu_cc_src
, s
->tmp4
);
5416 set_cc_op(s
, ((s
->cc_op
- CC_OP_MULB
) & 3) + CC_OP_SARB
);
5419 /* Otherwise, generate EFLAGS and replace the C bit. */
5420 gen_compute_eflags(s
);
5421 tcg_gen_deposit_tl(cpu_cc_src
, cpu_cc_src
, s
->tmp4
,
5426 case 0x1bc: /* bsf / tzcnt */
5427 case 0x1bd: /* bsr / lzcnt */
5429 modrm
= x86_ldub_code(env
, s
);
5430 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
5431 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
5432 gen_extu(ot
, s
->T0
);
5434 /* Note that lzcnt and tzcnt are in different extensions. */
5435 if ((prefixes
& PREFIX_REPZ
)
5437 ? s
->cpuid_ext3_features
& CPUID_EXT3_ABM
5438 : s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_BMI1
)) {
5440 /* For lzcnt/tzcnt, C bit is defined related to the input. */
5441 tcg_gen_mov_tl(cpu_cc_src
, s
->T0
);
5443 /* For lzcnt, reduce the target_ulong result by the
5444 number of zeros that we expect to find at the top. */
5445 tcg_gen_clzi_tl(s
->T0
, s
->T0
, TARGET_LONG_BITS
);
5446 tcg_gen_subi_tl(s
->T0
, s
->T0
, TARGET_LONG_BITS
- size
);
5448 /* For tzcnt, a zero input must return the operand size. */
5449 tcg_gen_ctzi_tl(s
->T0
, s
->T0
, size
);
5451 /* For lzcnt/tzcnt, Z bit is defined related to the result. */
5452 gen_op_update1_cc(s
);
5453 set_cc_op(s
, CC_OP_BMILGB
+ ot
);
5455 /* For bsr/bsf, only the Z bit is defined and it is related
5456 to the input and not the result. */
5457 tcg_gen_mov_tl(cpu_cc_dst
, s
->T0
);
5458 set_cc_op(s
, CC_OP_LOGICB
+ ot
);
5460 /* ??? The manual says that the output is undefined when the
5461 input is zero, but real hardware leaves it unchanged, and
5462 real programs appear to depend on that. Accomplish this
5463 by passing the output as the value to return upon zero. */
5465 /* For bsr, return the bit index of the first 1 bit,
5466 not the count of leading zeros. */
5467 tcg_gen_xori_tl(s
->T1
, cpu_regs
[reg
], TARGET_LONG_BITS
- 1);
5468 tcg_gen_clz_tl(s
->T0
, s
->T0
, s
->T1
);
5469 tcg_gen_xori_tl(s
->T0
, s
->T0
, TARGET_LONG_BITS
- 1);
5471 tcg_gen_ctz_tl(s
->T0
, s
->T0
, cpu_regs
[reg
]);
5474 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
5476 /************************/
5478 case 0x27: /* daa */
5481 gen_update_cc_op(s
);
5482 gen_helper_daa(cpu_env
);
5483 set_cc_op(s
, CC_OP_EFLAGS
);
5485 case 0x2f: /* das */
5488 gen_update_cc_op(s
);
5489 gen_helper_das(cpu_env
);
5490 set_cc_op(s
, CC_OP_EFLAGS
);
5492 case 0x37: /* aaa */
5495 gen_update_cc_op(s
);
5496 gen_helper_aaa(cpu_env
);
5497 set_cc_op(s
, CC_OP_EFLAGS
);
5499 case 0x3f: /* aas */
5502 gen_update_cc_op(s
);
5503 gen_helper_aas(cpu_env
);
5504 set_cc_op(s
, CC_OP_EFLAGS
);
5506 case 0xd4: /* aam */
5509 val
= x86_ldub_code(env
, s
);
5511 gen_exception(s
, EXCP00_DIVZ
);
5513 gen_helper_aam(cpu_env
, tcg_constant_i32(val
));
5514 set_cc_op(s
, CC_OP_LOGICB
);
5517 case 0xd5: /* aad */
5520 val
= x86_ldub_code(env
, s
);
5521 gen_helper_aad(cpu_env
, tcg_constant_i32(val
));
5522 set_cc_op(s
, CC_OP_LOGICB
);
5524 /************************/
5526 case 0x90: /* nop */
5527 /* XXX: correct lock test for all insn */
5528 if (prefixes
& PREFIX_LOCK
) {
5531 /* If REX_B is set, then this is xchg eax, r8d, not a nop. */
5533 goto do_xchg_reg_eax
;
5535 if (prefixes
& PREFIX_REPZ
) {
5536 gen_update_cc_op(s
);
5537 gen_update_eip_cur(s
);
5538 gen_helper_pause(cpu_env
, cur_insn_len_i32(s
));
5539 s
->base
.is_jmp
= DISAS_NORETURN
;
5542 case 0x9b: /* fwait */
5543 if ((s
->flags
& (HF_MP_MASK
| HF_TS_MASK
)) ==
5544 (HF_MP_MASK
| HF_TS_MASK
)) {
5545 gen_exception(s
, EXCP07_PREX
);
5547 gen_helper_fwait(cpu_env
);
5550 case 0xcc: /* int3 */
5551 gen_interrupt(s
, EXCP03_INT3
);
5553 case 0xcd: /* int N */
5554 val
= x86_ldub_code(env
, s
);
5555 if (check_vm86_iopl(s
)) {
5556 gen_interrupt(s
, val
);
5559 case 0xce: /* into */
5562 gen_update_cc_op(s
);
5563 gen_update_eip_cur(s
);
5564 gen_helper_into(cpu_env
, cur_insn_len_i32(s
));
5567 case 0xf1: /* icebp (undocumented, exits to external debugger) */
5568 gen_svm_check_intercept(s
, SVM_EXIT_ICEBP
);
5572 case 0xfa: /* cli */
5573 if (check_iopl(s
)) {
5574 gen_reset_eflags(s
, IF_MASK
);
5577 case 0xfb: /* sti */
5578 if (check_iopl(s
)) {
5579 gen_set_eflags(s
, IF_MASK
);
5580 /* interruptions are enabled only the first insn after sti */
5581 gen_update_eip_next(s
);
5582 gen_eob_inhibit_irq(s
, true);
5585 case 0x62: /* bound */
5589 modrm
= x86_ldub_code(env
, s
);
5590 reg
= (modrm
>> 3) & 7;
5591 mod
= (modrm
>> 6) & 3;
5594 gen_op_mov_v_reg(s
, ot
, s
->T0
, reg
);
5595 gen_lea_modrm(env
, s
, modrm
);
5596 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
5598 gen_helper_boundw(cpu_env
, s
->A0
, s
->tmp2_i32
);
5600 gen_helper_boundl(cpu_env
, s
->A0
, s
->tmp2_i32
);
5603 case 0x1c8 ... 0x1cf: /* bswap reg */
5604 reg
= (b
& 7) | REX_B(s
);
5605 #ifdef TARGET_X86_64
5606 if (dflag
== MO_64
) {
5607 tcg_gen_bswap64_i64(cpu_regs
[reg
], cpu_regs
[reg
]);
5611 tcg_gen_bswap32_tl(cpu_regs
[reg
], cpu_regs
[reg
], TCG_BSWAP_OZ
);
5613 case 0xd6: /* salc */
5616 gen_compute_eflags_c(s
, s
->T0
);
5617 tcg_gen_neg_tl(s
->T0
, s
->T0
);
5618 gen_op_mov_reg_v(s
, MO_8
, R_EAX
, s
->T0
);
5620 case 0xe0: /* loopnz */
5621 case 0xe1: /* loopz */
5622 case 0xe2: /* loop */
5623 case 0xe3: /* jecxz */
5626 int diff
= (int8_t)insn_get(env
, s
, MO_8
);
5628 l1
= gen_new_label();
5629 l2
= gen_new_label();
5630 gen_update_cc_op(s
);
5633 case 0: /* loopnz */
5635 gen_op_add_reg_im(s
, s
->aflag
, R_ECX
, -1);
5636 gen_op_jz_ecx(s
, l2
);
5637 gen_jcc1(s
, (JCC_Z
<< 1) | (b
^ 1), l1
);
5640 gen_op_add_reg_im(s
, s
->aflag
, R_ECX
, -1);
5641 gen_op_jnz_ecx(s
, l1
);
5645 gen_op_jz_ecx(s
, l1
);
5650 gen_jmp_rel_csize(s
, 0, 1);
5653 gen_jmp_rel(s
, dflag
, diff
, 0);
5656 case 0x130: /* wrmsr */
5657 case 0x132: /* rdmsr */
5658 if (check_cpl0(s
)) {
5659 gen_update_cc_op(s
);
5660 gen_update_eip_cur(s
);
5662 gen_helper_rdmsr(cpu_env
);
5664 gen_helper_wrmsr(cpu_env
);
5665 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
5669 case 0x131: /* rdtsc */
5670 gen_update_cc_op(s
);
5671 gen_update_eip_cur(s
);
5672 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
5674 s
->base
.is_jmp
= DISAS_TOO_MANY
;
5676 gen_helper_rdtsc(cpu_env
);
5678 case 0x133: /* rdpmc */
5679 gen_update_cc_op(s
);
5680 gen_update_eip_cur(s
);
5681 gen_helper_rdpmc(cpu_env
);
5682 s
->base
.is_jmp
= DISAS_NORETURN
;
5684 case 0x134: /* sysenter */
5685 /* For Intel SYSENTER is valid on 64-bit */
5686 if (CODE64(s
) && env
->cpuid_vendor1
!= CPUID_VENDOR_INTEL_1
)
5689 gen_exception_gpf(s
);
5691 gen_helper_sysenter(cpu_env
);
5692 s
->base
.is_jmp
= DISAS_EOB_ONLY
;
5695 case 0x135: /* sysexit */
5696 /* For Intel SYSEXIT is valid on 64-bit */
5697 if (CODE64(s
) && env
->cpuid_vendor1
!= CPUID_VENDOR_INTEL_1
)
5700 gen_exception_gpf(s
);
5702 gen_helper_sysexit(cpu_env
, tcg_constant_i32(dflag
- 1));
5703 s
->base
.is_jmp
= DISAS_EOB_ONLY
;
5706 #ifdef TARGET_X86_64
5707 case 0x105: /* syscall */
5708 /* XXX: is it usable in real mode ? */
5709 gen_update_cc_op(s
);
5710 gen_update_eip_cur(s
);
5711 gen_helper_syscall(cpu_env
, cur_insn_len_i32(s
));
5712 /* TF handling for the syscall insn is different. The TF bit is checked
5713 after the syscall insn completes. This allows #DB to not be
5714 generated after one has entered CPL0 if TF is set in FMASK. */
5715 gen_eob_worker(s
, false, true);
5717 case 0x107: /* sysret */
5719 gen_exception_gpf(s
);
5721 gen_helper_sysret(cpu_env
, tcg_constant_i32(dflag
- 1));
5722 /* condition codes are modified only in long mode */
5724 set_cc_op(s
, CC_OP_EFLAGS
);
5726 /* TF handling for the sysret insn is different. The TF bit is
5727 checked after the sysret insn completes. This allows #DB to be
5728 generated "as if" the syscall insn in userspace has just
5730 gen_eob_worker(s
, false, true);
5734 case 0x1a2: /* cpuid */
5735 gen_update_cc_op(s
);
5736 gen_update_eip_cur(s
);
5737 gen_helper_cpuid(cpu_env
);
5739 case 0xf4: /* hlt */
5740 if (check_cpl0(s
)) {
5741 gen_update_cc_op(s
);
5742 gen_update_eip_cur(s
);
5743 gen_helper_hlt(cpu_env
, cur_insn_len_i32(s
));
5744 s
->base
.is_jmp
= DISAS_NORETURN
;
5748 modrm
= x86_ldub_code(env
, s
);
5749 mod
= (modrm
>> 6) & 3;
5750 op
= (modrm
>> 3) & 7;
5753 if (!PE(s
) || VM86(s
))
5755 if (s
->flags
& HF_UMIP_MASK
&& !check_cpl0(s
)) {
5758 gen_svm_check_intercept(s
, SVM_EXIT_LDTR_READ
);
5759 tcg_gen_ld32u_tl(s
->T0
, cpu_env
,
5760 offsetof(CPUX86State
, ldt
.selector
));
5761 ot
= mod
== 3 ? dflag
: MO_16
;
5762 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
5765 if (!PE(s
) || VM86(s
))
5767 if (check_cpl0(s
)) {
5768 gen_svm_check_intercept(s
, SVM_EXIT_LDTR_WRITE
);
5769 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
5770 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
5771 gen_helper_lldt(cpu_env
, s
->tmp2_i32
);
5775 if (!PE(s
) || VM86(s
))
5777 if (s
->flags
& HF_UMIP_MASK
&& !check_cpl0(s
)) {
5780 gen_svm_check_intercept(s
, SVM_EXIT_TR_READ
);
5781 tcg_gen_ld32u_tl(s
->T0
, cpu_env
,
5782 offsetof(CPUX86State
, tr
.selector
));
5783 ot
= mod
== 3 ? dflag
: MO_16
;
5784 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
5787 if (!PE(s
) || VM86(s
))
5789 if (check_cpl0(s
)) {
5790 gen_svm_check_intercept(s
, SVM_EXIT_TR_WRITE
);
5791 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
5792 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, s
->T0
);
5793 gen_helper_ltr(cpu_env
, s
->tmp2_i32
);
5798 if (!PE(s
) || VM86(s
))
5800 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
5801 gen_update_cc_op(s
);
5803 gen_helper_verr(cpu_env
, s
->T0
);
5805 gen_helper_verw(cpu_env
, s
->T0
);
5807 set_cc_op(s
, CC_OP_EFLAGS
);
5815 modrm
= x86_ldub_code(env
, s
);
5817 CASE_MODRM_MEM_OP(0): /* sgdt */
5818 if (s
->flags
& HF_UMIP_MASK
&& !check_cpl0(s
)) {
5821 gen_svm_check_intercept(s
, SVM_EXIT_GDTR_READ
);
5822 gen_lea_modrm(env
, s
, modrm
);
5823 tcg_gen_ld32u_tl(s
->T0
,
5824 cpu_env
, offsetof(CPUX86State
, gdt
.limit
));
5825 gen_op_st_v(s
, MO_16
, s
->T0
, s
->A0
);
5826 gen_add_A0_im(s
, 2);
5827 tcg_gen_ld_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, gdt
.base
));
5828 if (dflag
== MO_16
) {
5829 tcg_gen_andi_tl(s
->T0
, s
->T0
, 0xffffff);
5831 gen_op_st_v(s
, CODE64(s
) + MO_32
, s
->T0
, s
->A0
);
5834 case 0xc8: /* monitor */
5835 if (!(s
->cpuid_ext_features
& CPUID_EXT_MONITOR
) || CPL(s
) != 0) {
5838 gen_update_cc_op(s
);
5839 gen_update_eip_cur(s
);
5840 tcg_gen_mov_tl(s
->A0
, cpu_regs
[R_EAX
]);
5841 gen_extu(s
->aflag
, s
->A0
);
5842 gen_add_A0_ds_seg(s
);
5843 gen_helper_monitor(cpu_env
, s
->A0
);
5846 case 0xc9: /* mwait */
5847 if (!(s
->cpuid_ext_features
& CPUID_EXT_MONITOR
) || CPL(s
) != 0) {
5850 gen_update_cc_op(s
);
5851 gen_update_eip_cur(s
);
5852 gen_helper_mwait(cpu_env
, cur_insn_len_i32(s
));
5853 s
->base
.is_jmp
= DISAS_NORETURN
;
5856 case 0xca: /* clac */
5857 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_SMAP
)
5861 gen_reset_eflags(s
, AC_MASK
);
5862 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
5865 case 0xcb: /* stac */
5866 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_SMAP
)
5870 gen_set_eflags(s
, AC_MASK
);
5871 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
5874 CASE_MODRM_MEM_OP(1): /* sidt */
5875 if (s
->flags
& HF_UMIP_MASK
&& !check_cpl0(s
)) {
5878 gen_svm_check_intercept(s
, SVM_EXIT_IDTR_READ
);
5879 gen_lea_modrm(env
, s
, modrm
);
5880 tcg_gen_ld32u_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, idt
.limit
));
5881 gen_op_st_v(s
, MO_16
, s
->T0
, s
->A0
);
5882 gen_add_A0_im(s
, 2);
5883 tcg_gen_ld_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, idt
.base
));
5884 if (dflag
== MO_16
) {
5885 tcg_gen_andi_tl(s
->T0
, s
->T0
, 0xffffff);
5887 gen_op_st_v(s
, CODE64(s
) + MO_32
, s
->T0
, s
->A0
);
5890 case 0xd0: /* xgetbv */
5891 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
5892 || (s
->prefix
& (PREFIX_LOCK
| PREFIX_DATA
5893 | PREFIX_REPZ
| PREFIX_REPNZ
))) {
5896 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_ECX
]);
5897 gen_helper_xgetbv(s
->tmp1_i64
, cpu_env
, s
->tmp2_i32
);
5898 tcg_gen_extr_i64_tl(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
], s
->tmp1_i64
);
5901 case 0xd1: /* xsetbv */
5902 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
5903 || (s
->prefix
& (PREFIX_LOCK
| PREFIX_DATA
5904 | PREFIX_REPZ
| PREFIX_REPNZ
))) {
5907 if (!check_cpl0(s
)) {
5910 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
5912 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_ECX
]);
5913 gen_helper_xsetbv(cpu_env
, s
->tmp2_i32
, s
->tmp1_i64
);
5914 /* End TB because translation flags may change. */
5915 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
5918 case 0xd8: /* VMRUN */
5919 if (!SVME(s
) || !PE(s
)) {
5922 if (!check_cpl0(s
)) {
5925 gen_update_cc_op(s
);
5926 gen_update_eip_cur(s
);
5927 gen_helper_vmrun(cpu_env
, tcg_constant_i32(s
->aflag
- 1),
5928 cur_insn_len_i32(s
));
5929 tcg_gen_exit_tb(NULL
, 0);
5930 s
->base
.is_jmp
= DISAS_NORETURN
;
5933 case 0xd9: /* VMMCALL */
5937 gen_update_cc_op(s
);
5938 gen_update_eip_cur(s
);
5939 gen_helper_vmmcall(cpu_env
);
5942 case 0xda: /* VMLOAD */
5943 if (!SVME(s
) || !PE(s
)) {
5946 if (!check_cpl0(s
)) {
5949 gen_update_cc_op(s
);
5950 gen_update_eip_cur(s
);
5951 gen_helper_vmload(cpu_env
, tcg_constant_i32(s
->aflag
- 1));
5954 case 0xdb: /* VMSAVE */
5955 if (!SVME(s
) || !PE(s
)) {
5958 if (!check_cpl0(s
)) {
5961 gen_update_cc_op(s
);
5962 gen_update_eip_cur(s
);
5963 gen_helper_vmsave(cpu_env
, tcg_constant_i32(s
->aflag
- 1));
5966 case 0xdc: /* STGI */
5967 if ((!SVME(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_SKINIT
))
5971 if (!check_cpl0(s
)) {
5974 gen_update_cc_op(s
);
5975 gen_helper_stgi(cpu_env
);
5976 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
5979 case 0xdd: /* CLGI */
5980 if (!SVME(s
) || !PE(s
)) {
5983 if (!check_cpl0(s
)) {
5986 gen_update_cc_op(s
);
5987 gen_update_eip_cur(s
);
5988 gen_helper_clgi(cpu_env
);
5991 case 0xde: /* SKINIT */
5992 if ((!SVME(s
) && !(s
->cpuid_ext3_features
& CPUID_EXT3_SKINIT
))
5996 gen_svm_check_intercept(s
, SVM_EXIT_SKINIT
);
5997 /* If not intercepted, not implemented -- raise #UD. */
6000 case 0xdf: /* INVLPGA */
6001 if (!SVME(s
) || !PE(s
)) {
6004 if (!check_cpl0(s
)) {
6007 gen_svm_check_intercept(s
, SVM_EXIT_INVLPGA
);
6008 if (s
->aflag
== MO_64
) {
6009 tcg_gen_mov_tl(s
->A0
, cpu_regs
[R_EAX
]);
6011 tcg_gen_ext32u_tl(s
->A0
, cpu_regs
[R_EAX
]);
6013 gen_helper_flush_page(cpu_env
, s
->A0
);
6014 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
6017 CASE_MODRM_MEM_OP(2): /* lgdt */
6018 if (!check_cpl0(s
)) {
6021 gen_svm_check_intercept(s
, SVM_EXIT_GDTR_WRITE
);
6022 gen_lea_modrm(env
, s
, modrm
);
6023 gen_op_ld_v(s
, MO_16
, s
->T1
, s
->A0
);
6024 gen_add_A0_im(s
, 2);
6025 gen_op_ld_v(s
, CODE64(s
) + MO_32
, s
->T0
, s
->A0
);
6026 if (dflag
== MO_16
) {
6027 tcg_gen_andi_tl(s
->T0
, s
->T0
, 0xffffff);
6029 tcg_gen_st_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, gdt
.base
));
6030 tcg_gen_st32_tl(s
->T1
, cpu_env
, offsetof(CPUX86State
, gdt
.limit
));
6033 CASE_MODRM_MEM_OP(3): /* lidt */
6034 if (!check_cpl0(s
)) {
6037 gen_svm_check_intercept(s
, SVM_EXIT_IDTR_WRITE
);
6038 gen_lea_modrm(env
, s
, modrm
);
6039 gen_op_ld_v(s
, MO_16
, s
->T1
, s
->A0
);
6040 gen_add_A0_im(s
, 2);
6041 gen_op_ld_v(s
, CODE64(s
) + MO_32
, s
->T0
, s
->A0
);
6042 if (dflag
== MO_16
) {
6043 tcg_gen_andi_tl(s
->T0
, s
->T0
, 0xffffff);
6045 tcg_gen_st_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, idt
.base
));
6046 tcg_gen_st32_tl(s
->T1
, cpu_env
, offsetof(CPUX86State
, idt
.limit
));
6049 CASE_MODRM_OP(4): /* smsw */
6050 if (s
->flags
& HF_UMIP_MASK
&& !check_cpl0(s
)) {
6053 gen_svm_check_intercept(s
, SVM_EXIT_READ_CR0
);
6054 tcg_gen_ld_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, cr
[0]));
6056 * In 32-bit mode, the higher 16 bits of the destination
6057 * register are undefined. In practice CR0[31:0] is stored
6058 * just like in 64-bit mode.
6060 mod
= (modrm
>> 6) & 3;
6061 ot
= (mod
!= 3 ? MO_16
: s
->dflag
);
6062 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 1);
6064 case 0xee: /* rdpkru */
6065 if (prefixes
& PREFIX_LOCK
) {
6068 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_ECX
]);
6069 gen_helper_rdpkru(s
->tmp1_i64
, cpu_env
, s
->tmp2_i32
);
6070 tcg_gen_extr_i64_tl(cpu_regs
[R_EAX
], cpu_regs
[R_EDX
], s
->tmp1_i64
);
6072 case 0xef: /* wrpkru */
6073 if (prefixes
& PREFIX_LOCK
) {
6076 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
6078 tcg_gen_trunc_tl_i32(s
->tmp2_i32
, cpu_regs
[R_ECX
]);
6079 gen_helper_wrpkru(cpu_env
, s
->tmp2_i32
, s
->tmp1_i64
);
6082 CASE_MODRM_OP(6): /* lmsw */
6083 if (!check_cpl0(s
)) {
6086 gen_svm_check_intercept(s
, SVM_EXIT_WRITE_CR0
);
6087 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
6089 * Only the 4 lower bits of CR0 are modified.
6090 * PE cannot be set to zero if already set to one.
6092 tcg_gen_ld_tl(s
->T1
, cpu_env
, offsetof(CPUX86State
, cr
[0]));
6093 tcg_gen_andi_tl(s
->T0
, s
->T0
, 0xf);
6094 tcg_gen_andi_tl(s
->T1
, s
->T1
, ~0xe);
6095 tcg_gen_or_tl(s
->T0
, s
->T0
, s
->T1
);
6096 gen_helper_write_crN(cpu_env
, tcg_constant_i32(0), s
->T0
);
6097 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
6100 CASE_MODRM_MEM_OP(7): /* invlpg */
6101 if (!check_cpl0(s
)) {
6104 gen_svm_check_intercept(s
, SVM_EXIT_INVLPG
);
6105 gen_lea_modrm(env
, s
, modrm
);
6106 gen_helper_flush_page(cpu_env
, s
->A0
);
6107 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
6110 case 0xf8: /* swapgs */
6111 #ifdef TARGET_X86_64
6113 if (check_cpl0(s
)) {
6114 tcg_gen_mov_tl(s
->T0
, cpu_seg_base
[R_GS
]);
6115 tcg_gen_ld_tl(cpu_seg_base
[R_GS
], cpu_env
,
6116 offsetof(CPUX86State
, kernelgsbase
));
6117 tcg_gen_st_tl(s
->T0
, cpu_env
,
6118 offsetof(CPUX86State
, kernelgsbase
));
6125 case 0xf9: /* rdtscp */
6126 if (!(s
->cpuid_ext2_features
& CPUID_EXT2_RDTSCP
)) {
6129 gen_update_cc_op(s
);
6130 gen_update_eip_cur(s
);
6131 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6133 s
->base
.is_jmp
= DISAS_TOO_MANY
;
6135 gen_helper_rdtscp(cpu_env
);
6143 case 0x108: /* invd */
6144 case 0x109: /* wbinvd */
6145 if (check_cpl0(s
)) {
6146 gen_svm_check_intercept(s
, (b
& 2) ? SVM_EXIT_INVD
: SVM_EXIT_WBINVD
);
6150 case 0x63: /* arpl or movslS (x86_64) */
6151 #ifdef TARGET_X86_64
6154 /* d_ot is the size of destination */
6157 modrm
= x86_ldub_code(env
, s
);
6158 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
6159 mod
= (modrm
>> 6) & 3;
6160 rm
= (modrm
& 7) | REX_B(s
);
6163 gen_op_mov_v_reg(s
, MO_32
, s
->T0
, rm
);
6165 if (d_ot
== MO_64
) {
6166 tcg_gen_ext32s_tl(s
->T0
, s
->T0
);
6168 gen_op_mov_reg_v(s
, d_ot
, reg
, s
->T0
);
6170 gen_lea_modrm(env
, s
, modrm
);
6171 gen_op_ld_v(s
, MO_32
| MO_SIGN
, s
->T0
, s
->A0
);
6172 gen_op_mov_reg_v(s
, d_ot
, reg
, s
->T0
);
6180 if (!PE(s
) || VM86(s
))
6182 t0
= tcg_temp_new();
6183 t1
= tcg_temp_new();
6184 t2
= tcg_temp_new();
6186 modrm
= x86_ldub_code(env
, s
);
6187 reg
= (modrm
>> 3) & 7;
6188 mod
= (modrm
>> 6) & 3;
6191 gen_lea_modrm(env
, s
, modrm
);
6192 gen_op_ld_v(s
, ot
, t0
, s
->A0
);
6194 gen_op_mov_v_reg(s
, ot
, t0
, rm
);
6196 gen_op_mov_v_reg(s
, ot
, t1
, reg
);
6197 tcg_gen_andi_tl(s
->tmp0
, t0
, 3);
6198 tcg_gen_andi_tl(t1
, t1
, 3);
6199 tcg_gen_movi_tl(t2
, 0);
6200 label1
= gen_new_label();
6201 tcg_gen_brcond_tl(TCG_COND_GE
, s
->tmp0
, t1
, label1
);
6202 tcg_gen_andi_tl(t0
, t0
, ~3);
6203 tcg_gen_or_tl(t0
, t0
, t1
);
6204 tcg_gen_movi_tl(t2
, CC_Z
);
6205 gen_set_label(label1
);
6207 gen_op_st_v(s
, ot
, t0
, s
->A0
);
6209 gen_op_mov_reg_v(s
, ot
, rm
, t0
);
6211 gen_compute_eflags(s
);
6212 tcg_gen_andi_tl(cpu_cc_src
, cpu_cc_src
, ~CC_Z
);
6213 tcg_gen_or_tl(cpu_cc_src
, cpu_cc_src
, t2
);
6216 case 0x102: /* lar */
6217 case 0x103: /* lsl */
6221 if (!PE(s
) || VM86(s
))
6223 ot
= dflag
!= MO_16
? MO_32
: MO_16
;
6224 modrm
= x86_ldub_code(env
, s
);
6225 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
6226 gen_ldst_modrm(env
, s
, modrm
, MO_16
, OR_TMP0
, 0);
6227 t0
= tcg_temp_new();
6228 gen_update_cc_op(s
);
6230 gen_helper_lar(t0
, cpu_env
, s
->T0
);
6232 gen_helper_lsl(t0
, cpu_env
, s
->T0
);
6234 tcg_gen_andi_tl(s
->tmp0
, cpu_cc_src
, CC_Z
);
6235 label1
= gen_new_label();
6236 tcg_gen_brcondi_tl(TCG_COND_EQ
, s
->tmp0
, 0, label1
);
6237 gen_op_mov_reg_v(s
, ot
, reg
, t0
);
6238 gen_set_label(label1
);
6239 set_cc_op(s
, CC_OP_EFLAGS
);
6243 modrm
= x86_ldub_code(env
, s
);
6244 mod
= (modrm
>> 6) & 3;
6245 op
= (modrm
>> 3) & 7;
6247 case 0: /* prefetchnta */
6248 case 1: /* prefetchnt0 */
6249 case 2: /* prefetchnt0 */
6250 case 3: /* prefetchnt0 */
6253 gen_nop_modrm(env
, s
, modrm
);
6254 /* nothing more to do */
6256 default: /* nop (multi byte) */
6257 gen_nop_modrm(env
, s
, modrm
);
6262 modrm
= x86_ldub_code(env
, s
);
6263 if (s
->flags
& HF_MPX_EN_MASK
) {
6264 mod
= (modrm
>> 6) & 3;
6265 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
6266 if (prefixes
& PREFIX_REPZ
) {
6269 || (prefixes
& PREFIX_LOCK
)
6270 || s
->aflag
== MO_16
) {
6273 gen_bndck(env
, s
, modrm
, TCG_COND_LTU
, cpu_bndl
[reg
]);
6274 } else if (prefixes
& PREFIX_REPNZ
) {
6277 || (prefixes
& PREFIX_LOCK
)
6278 || s
->aflag
== MO_16
) {
6281 TCGv_i64 notu
= tcg_temp_new_i64();
6282 tcg_gen_not_i64(notu
, cpu_bndu
[reg
]);
6283 gen_bndck(env
, s
, modrm
, TCG_COND_GTU
, notu
);
6284 } else if (prefixes
& PREFIX_DATA
) {
6285 /* bndmov -- from reg/mem */
6286 if (reg
>= 4 || s
->aflag
== MO_16
) {
6290 int reg2
= (modrm
& 7) | REX_B(s
);
6291 if (reg2
>= 4 || (prefixes
& PREFIX_LOCK
)) {
6294 if (s
->flags
& HF_MPX_IU_MASK
) {
6295 tcg_gen_mov_i64(cpu_bndl
[reg
], cpu_bndl
[reg2
]);
6296 tcg_gen_mov_i64(cpu_bndu
[reg
], cpu_bndu
[reg2
]);
6299 gen_lea_modrm(env
, s
, modrm
);
6301 tcg_gen_qemu_ld_i64(cpu_bndl
[reg
], s
->A0
,
6302 s
->mem_index
, MO_LEUQ
);
6303 tcg_gen_addi_tl(s
->A0
, s
->A0
, 8);
6304 tcg_gen_qemu_ld_i64(cpu_bndu
[reg
], s
->A0
,
6305 s
->mem_index
, MO_LEUQ
);
6307 tcg_gen_qemu_ld_i64(cpu_bndl
[reg
], s
->A0
,
6308 s
->mem_index
, MO_LEUL
);
6309 tcg_gen_addi_tl(s
->A0
, s
->A0
, 4);
6310 tcg_gen_qemu_ld_i64(cpu_bndu
[reg
], s
->A0
,
6311 s
->mem_index
, MO_LEUL
);
6313 /* bnd registers are now in-use */
6314 gen_set_hflag(s
, HF_MPX_IU_MASK
);
6316 } else if (mod
!= 3) {
6318 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
6320 || (prefixes
& PREFIX_LOCK
)
6321 || s
->aflag
== MO_16
6326 tcg_gen_addi_tl(s
->A0
, cpu_regs
[a
.base
], a
.disp
);
6328 tcg_gen_movi_tl(s
->A0
, 0);
6330 gen_lea_v_seg(s
, s
->aflag
, s
->A0
, a
.def_seg
, s
->override
);
6332 tcg_gen_mov_tl(s
->T0
, cpu_regs
[a
.index
]);
6334 tcg_gen_movi_tl(s
->T0
, 0);
6337 gen_helper_bndldx64(cpu_bndl
[reg
], cpu_env
, s
->A0
, s
->T0
);
6338 tcg_gen_ld_i64(cpu_bndu
[reg
], cpu_env
,
6339 offsetof(CPUX86State
, mmx_t0
.MMX_Q(0)));
6341 gen_helper_bndldx32(cpu_bndu
[reg
], cpu_env
, s
->A0
, s
->T0
);
6342 tcg_gen_ext32u_i64(cpu_bndl
[reg
], cpu_bndu
[reg
]);
6343 tcg_gen_shri_i64(cpu_bndu
[reg
], cpu_bndu
[reg
], 32);
6345 gen_set_hflag(s
, HF_MPX_IU_MASK
);
6348 gen_nop_modrm(env
, s
, modrm
);
6351 modrm
= x86_ldub_code(env
, s
);
6352 if (s
->flags
& HF_MPX_EN_MASK
) {
6353 mod
= (modrm
>> 6) & 3;
6354 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
6355 if (mod
!= 3 && (prefixes
& PREFIX_REPZ
)) {
6358 || (prefixes
& PREFIX_LOCK
)
6359 || s
->aflag
== MO_16
) {
6362 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
6364 tcg_gen_extu_tl_i64(cpu_bndl
[reg
], cpu_regs
[a
.base
]);
6366 tcg_gen_ext32u_i64(cpu_bndl
[reg
], cpu_bndl
[reg
]);
6368 } else if (a
.base
== -1) {
6369 /* no base register has lower bound of 0 */
6370 tcg_gen_movi_i64(cpu_bndl
[reg
], 0);
6372 /* rip-relative generates #ud */
6375 tcg_gen_not_tl(s
->A0
, gen_lea_modrm_1(s
, a
, false));
6377 tcg_gen_ext32u_tl(s
->A0
, s
->A0
);
6379 tcg_gen_extu_tl_i64(cpu_bndu
[reg
], s
->A0
);
6380 /* bnd registers are now in-use */
6381 gen_set_hflag(s
, HF_MPX_IU_MASK
);
6383 } else if (prefixes
& PREFIX_REPNZ
) {
6386 || (prefixes
& PREFIX_LOCK
)
6387 || s
->aflag
== MO_16
) {
6390 gen_bndck(env
, s
, modrm
, TCG_COND_GTU
, cpu_bndu
[reg
]);
6391 } else if (prefixes
& PREFIX_DATA
) {
6392 /* bndmov -- to reg/mem */
6393 if (reg
>= 4 || s
->aflag
== MO_16
) {
6397 int reg2
= (modrm
& 7) | REX_B(s
);
6398 if (reg2
>= 4 || (prefixes
& PREFIX_LOCK
)) {
6401 if (s
->flags
& HF_MPX_IU_MASK
) {
6402 tcg_gen_mov_i64(cpu_bndl
[reg2
], cpu_bndl
[reg
]);
6403 tcg_gen_mov_i64(cpu_bndu
[reg2
], cpu_bndu
[reg
]);
6406 gen_lea_modrm(env
, s
, modrm
);
6408 tcg_gen_qemu_st_i64(cpu_bndl
[reg
], s
->A0
,
6409 s
->mem_index
, MO_LEUQ
);
6410 tcg_gen_addi_tl(s
->A0
, s
->A0
, 8);
6411 tcg_gen_qemu_st_i64(cpu_bndu
[reg
], s
->A0
,
6412 s
->mem_index
, MO_LEUQ
);
6414 tcg_gen_qemu_st_i64(cpu_bndl
[reg
], s
->A0
,
6415 s
->mem_index
, MO_LEUL
);
6416 tcg_gen_addi_tl(s
->A0
, s
->A0
, 4);
6417 tcg_gen_qemu_st_i64(cpu_bndu
[reg
], s
->A0
,
6418 s
->mem_index
, MO_LEUL
);
6421 } else if (mod
!= 3) {
6423 AddressParts a
= gen_lea_modrm_0(env
, s
, modrm
);
6425 || (prefixes
& PREFIX_LOCK
)
6426 || s
->aflag
== MO_16
6431 tcg_gen_addi_tl(s
->A0
, cpu_regs
[a
.base
], a
.disp
);
6433 tcg_gen_movi_tl(s
->A0
, 0);
6435 gen_lea_v_seg(s
, s
->aflag
, s
->A0
, a
.def_seg
, s
->override
);
6437 tcg_gen_mov_tl(s
->T0
, cpu_regs
[a
.index
]);
6439 tcg_gen_movi_tl(s
->T0
, 0);
6442 gen_helper_bndstx64(cpu_env
, s
->A0
, s
->T0
,
6443 cpu_bndl
[reg
], cpu_bndu
[reg
]);
6445 gen_helper_bndstx32(cpu_env
, s
->A0
, s
->T0
,
6446 cpu_bndl
[reg
], cpu_bndu
[reg
]);
6450 gen_nop_modrm(env
, s
, modrm
);
6452 case 0x119: case 0x11c ... 0x11f: /* nop (multi byte) */
6453 modrm
= x86_ldub_code(env
, s
);
6454 gen_nop_modrm(env
, s
, modrm
);
6457 case 0x120: /* mov reg, crN */
6458 case 0x122: /* mov crN, reg */
6459 if (!check_cpl0(s
)) {
6462 modrm
= x86_ldub_code(env
, s
);
6464 * Ignore the mod bits (assume (modrm&0xc0)==0xc0).
6465 * AMD documentation (24594.pdf) and testing of Intel 386 and 486
6466 * processors all show that the mod bits are assumed to be 1's,
6467 * regardless of actual values.
6469 rm
= (modrm
& 7) | REX_B(s
);
6470 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
6473 if ((prefixes
& PREFIX_LOCK
) &&
6474 (s
->cpuid_ext3_features
& CPUID_EXT3_CR8LEG
)) {
6486 ot
= (CODE64(s
) ? MO_64
: MO_32
);
6488 if (tb_cflags(s
->base
.tb
) & CF_USE_ICOUNT
) {
6490 s
->base
.is_jmp
= DISAS_TOO_MANY
;
6493 gen_svm_check_intercept(s
, SVM_EXIT_WRITE_CR0
+ reg
);
6494 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
6495 gen_helper_write_crN(cpu_env
, tcg_constant_i32(reg
), s
->T0
);
6496 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
6498 gen_svm_check_intercept(s
, SVM_EXIT_READ_CR0
+ reg
);
6499 gen_helper_read_crN(s
->T0
, cpu_env
, tcg_constant_i32(reg
));
6500 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
6504 case 0x121: /* mov reg, drN */
6505 case 0x123: /* mov drN, reg */
6506 if (check_cpl0(s
)) {
6507 modrm
= x86_ldub_code(env
, s
);
6508 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
6509 * AMD documentation (24594.pdf) and testing of
6510 * intel 386 and 486 processors all show that the mod bits
6511 * are assumed to be 1's, regardless of actual values.
6513 rm
= (modrm
& 7) | REX_B(s
);
6514 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
6523 gen_svm_check_intercept(s
, SVM_EXIT_WRITE_DR0
+ reg
);
6524 gen_op_mov_v_reg(s
, ot
, s
->T0
, rm
);
6525 tcg_gen_movi_i32(s
->tmp2_i32
, reg
);
6526 gen_helper_set_dr(cpu_env
, s
->tmp2_i32
, s
->T0
);
6527 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
6529 gen_svm_check_intercept(s
, SVM_EXIT_READ_DR0
+ reg
);
6530 tcg_gen_movi_i32(s
->tmp2_i32
, reg
);
6531 gen_helper_get_dr(s
->T0
, cpu_env
, s
->tmp2_i32
);
6532 gen_op_mov_reg_v(s
, ot
, rm
, s
->T0
);
6536 case 0x106: /* clts */
6537 if (check_cpl0(s
)) {
6538 gen_svm_check_intercept(s
, SVM_EXIT_WRITE_CR0
);
6539 gen_helper_clts(cpu_env
);
6540 /* abort block because static cpu state changed */
6541 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
6544 /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
6545 case 0x1c3: /* MOVNTI reg, mem */
6546 if (!(s
->cpuid_features
& CPUID_SSE2
))
6548 ot
= mo_64_32(dflag
);
6549 modrm
= x86_ldub_code(env
, s
);
6550 mod
= (modrm
>> 6) & 3;
6553 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
6554 /* generate a generic store */
6555 gen_ldst_modrm(env
, s
, modrm
, ot
, reg
, 1);
6558 modrm
= x86_ldub_code(env
, s
);
6560 CASE_MODRM_MEM_OP(0): /* fxsave */
6561 if (!(s
->cpuid_features
& CPUID_FXSR
)
6562 || (prefixes
& PREFIX_LOCK
)) {
6565 if ((s
->flags
& HF_EM_MASK
) || (s
->flags
& HF_TS_MASK
)) {
6566 gen_exception(s
, EXCP07_PREX
);
6569 gen_lea_modrm(env
, s
, modrm
);
6570 gen_helper_fxsave(cpu_env
, s
->A0
);
6573 CASE_MODRM_MEM_OP(1): /* fxrstor */
6574 if (!(s
->cpuid_features
& CPUID_FXSR
)
6575 || (prefixes
& PREFIX_LOCK
)) {
6578 if ((s
->flags
& HF_EM_MASK
) || (s
->flags
& HF_TS_MASK
)) {
6579 gen_exception(s
, EXCP07_PREX
);
6582 gen_lea_modrm(env
, s
, modrm
);
6583 gen_helper_fxrstor(cpu_env
, s
->A0
);
6586 CASE_MODRM_MEM_OP(2): /* ldmxcsr */
6587 if ((s
->flags
& HF_EM_MASK
) || !(s
->flags
& HF_OSFXSR_MASK
)) {
6590 if (s
->flags
& HF_TS_MASK
) {
6591 gen_exception(s
, EXCP07_PREX
);
6594 gen_lea_modrm(env
, s
, modrm
);
6595 tcg_gen_qemu_ld_i32(s
->tmp2_i32
, s
->A0
, s
->mem_index
, MO_LEUL
);
6596 gen_helper_ldmxcsr(cpu_env
, s
->tmp2_i32
);
6599 CASE_MODRM_MEM_OP(3): /* stmxcsr */
6600 if ((s
->flags
& HF_EM_MASK
) || !(s
->flags
& HF_OSFXSR_MASK
)) {
6603 if (s
->flags
& HF_TS_MASK
) {
6604 gen_exception(s
, EXCP07_PREX
);
6607 gen_helper_update_mxcsr(cpu_env
);
6608 gen_lea_modrm(env
, s
, modrm
);
6609 tcg_gen_ld32u_tl(s
->T0
, cpu_env
, offsetof(CPUX86State
, mxcsr
));
6610 gen_op_st_v(s
, MO_32
, s
->T0
, s
->A0
);
6613 CASE_MODRM_MEM_OP(4): /* xsave */
6614 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
6615 || (prefixes
& (PREFIX_LOCK
| PREFIX_DATA
6616 | PREFIX_REPZ
| PREFIX_REPNZ
))) {
6619 gen_lea_modrm(env
, s
, modrm
);
6620 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
6622 gen_helper_xsave(cpu_env
, s
->A0
, s
->tmp1_i64
);
6625 CASE_MODRM_MEM_OP(5): /* xrstor */
6626 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
6627 || (prefixes
& (PREFIX_LOCK
| PREFIX_DATA
6628 | PREFIX_REPZ
| PREFIX_REPNZ
))) {
6631 gen_lea_modrm(env
, s
, modrm
);
6632 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
6634 gen_helper_xrstor(cpu_env
, s
->A0
, s
->tmp1_i64
);
6635 /* XRSTOR is how MPX is enabled, which changes how
6636 we translate. Thus we need to end the TB. */
6637 s
->base
.is_jmp
= DISAS_EOB_NEXT
;
6640 CASE_MODRM_MEM_OP(6): /* xsaveopt / clwb */
6641 if (prefixes
& PREFIX_LOCK
) {
6644 if (prefixes
& PREFIX_DATA
) {
6646 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_CLWB
)) {
6649 gen_nop_modrm(env
, s
, modrm
);
6652 if ((s
->cpuid_ext_features
& CPUID_EXT_XSAVE
) == 0
6653 || (s
->cpuid_xsave_features
& CPUID_XSAVE_XSAVEOPT
) == 0
6654 || (prefixes
& (PREFIX_REPZ
| PREFIX_REPNZ
))) {
6657 gen_lea_modrm(env
, s
, modrm
);
6658 tcg_gen_concat_tl_i64(s
->tmp1_i64
, cpu_regs
[R_EAX
],
6660 gen_helper_xsaveopt(cpu_env
, s
->A0
, s
->tmp1_i64
);
6664 CASE_MODRM_MEM_OP(7): /* clflush / clflushopt */
6665 if (prefixes
& PREFIX_LOCK
) {
6668 if (prefixes
& PREFIX_DATA
) {
6670 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_CLFLUSHOPT
)) {
6675 if ((s
->prefix
& (PREFIX_REPZ
| PREFIX_REPNZ
))
6676 || !(s
->cpuid_features
& CPUID_CLFLUSH
)) {
6680 gen_nop_modrm(env
, s
, modrm
);
6683 case 0xc0 ... 0xc7: /* rdfsbase (f3 0f ae /0) */
6684 case 0xc8 ... 0xcf: /* rdgsbase (f3 0f ae /1) */
6685 case 0xd0 ... 0xd7: /* wrfsbase (f3 0f ae /2) */
6686 case 0xd8 ... 0xdf: /* wrgsbase (f3 0f ae /3) */
6688 && (prefixes
& PREFIX_REPZ
)
6689 && !(prefixes
& PREFIX_LOCK
)
6690 && (s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_FSGSBASE
)) {
6691 TCGv base
, treg
, src
, dst
;
6693 /* Preserve hflags bits by testing CR4 at runtime. */
6694 tcg_gen_movi_i32(s
->tmp2_i32
, CR4_FSGSBASE_MASK
);
6695 gen_helper_cr4_testbit(cpu_env
, s
->tmp2_i32
);
6697 base
= cpu_seg_base
[modrm
& 8 ? R_GS
: R_FS
];
6698 treg
= cpu_regs
[(modrm
& 7) | REX_B(s
)];
6702 dst
= base
, src
= treg
;
6705 dst
= treg
, src
= base
;
6708 if (s
->dflag
== MO_32
) {
6709 tcg_gen_ext32u_tl(dst
, src
);
6711 tcg_gen_mov_tl(dst
, src
);
6717 case 0xf8: /* sfence / pcommit */
6718 if (prefixes
& PREFIX_DATA
) {
6720 if (!(s
->cpuid_7_0_ebx_features
& CPUID_7_0_EBX_PCOMMIT
)
6721 || (prefixes
& PREFIX_LOCK
)) {
6727 case 0xf9 ... 0xff: /* sfence */
6728 if (!(s
->cpuid_features
& CPUID_SSE
)
6729 || (prefixes
& PREFIX_LOCK
)) {
6732 tcg_gen_mb(TCG_MO_ST_ST
| TCG_BAR_SC
);
6734 case 0xe8 ... 0xef: /* lfence */
6735 if (!(s
->cpuid_features
& CPUID_SSE
)
6736 || (prefixes
& PREFIX_LOCK
)) {
6739 tcg_gen_mb(TCG_MO_LD_LD
| TCG_BAR_SC
);
6741 case 0xf0 ... 0xf7: /* mfence */
6742 if (!(s
->cpuid_features
& CPUID_SSE2
)
6743 || (prefixes
& PREFIX_LOCK
)) {
6746 tcg_gen_mb(TCG_MO_ALL
| TCG_BAR_SC
);
6754 case 0x10d: /* 3DNow! prefetch(w) */
6755 modrm
= x86_ldub_code(env
, s
);
6756 mod
= (modrm
>> 6) & 3;
6759 gen_nop_modrm(env
, s
, modrm
);
6761 case 0x1aa: /* rsm */
6762 gen_svm_check_intercept(s
, SVM_EXIT_RSM
);
6763 if (!(s
->flags
& HF_SMM_MASK
))
6765 #ifdef CONFIG_USER_ONLY
6766 /* we should not be in SMM mode */
6767 g_assert_not_reached();
6769 gen_update_cc_op(s
);
6770 gen_update_eip_next(s
);
6771 gen_helper_rsm(cpu_env
);
6772 #endif /* CONFIG_USER_ONLY */
6773 s
->base
.is_jmp
= DISAS_EOB_ONLY
;
6775 case 0x1b8: /* SSE4.2 popcnt */
6776 if ((prefixes
& (PREFIX_REPZ
| PREFIX_LOCK
| PREFIX_REPNZ
)) !=
6779 if (!(s
->cpuid_ext_features
& CPUID_EXT_POPCNT
))
6782 modrm
= x86_ldub_code(env
, s
);
6783 reg
= ((modrm
>> 3) & 7) | REX_R(s
);
6785 if (s
->prefix
& PREFIX_DATA
) {
6788 ot
= mo_64_32(dflag
);
6791 gen_ldst_modrm(env
, s
, modrm
, ot
, OR_TMP0
, 0);
6792 gen_extu(ot
, s
->T0
);
6793 tcg_gen_mov_tl(cpu_cc_src
, s
->T0
);
6794 tcg_gen_ctpop_tl(s
->T0
, s
->T0
);
6795 gen_op_mov_reg_v(s
, ot
, reg
, s
->T0
);
6797 set_cc_op(s
, CC_OP_POPCNT
);
6799 case 0x10e ... 0x117:
6800 case 0x128 ... 0x12f:
6801 case 0x138 ... 0x13a:
6802 case 0x150 ... 0x179:
6803 case 0x17c ... 0x17f:
6805 case 0x1c4 ... 0x1c6:
6806 case 0x1d0 ... 0x1fe:
6807 disas_insn_new(s
, cpu
, b
);
6814 gen_illegal_opcode(s
);
6817 gen_unknown_opcode(env
, s
);
6821 void tcg_x86_init(void)
6823 static const char reg_names
[CPU_NB_REGS
][4] = {
6824 #ifdef TARGET_X86_64
6852 static const char eip_name
[] = {
6853 #ifdef TARGET_X86_64
6859 static const char seg_base_names
[6][8] = {
6867 static const char bnd_regl_names
[4][8] = {
6868 "bnd0_lb", "bnd1_lb", "bnd2_lb", "bnd3_lb"
6870 static const char bnd_regu_names
[4][8] = {
6871 "bnd0_ub", "bnd1_ub", "bnd2_ub", "bnd3_ub"
6875 cpu_cc_op
= tcg_global_mem_new_i32(cpu_env
,
6876 offsetof(CPUX86State
, cc_op
), "cc_op");
6877 cpu_cc_dst
= tcg_global_mem_new(cpu_env
, offsetof(CPUX86State
, cc_dst
),
6879 cpu_cc_src
= tcg_global_mem_new(cpu_env
, offsetof(CPUX86State
, cc_src
),
6881 cpu_cc_src2
= tcg_global_mem_new(cpu_env
, offsetof(CPUX86State
, cc_src2
),
6883 cpu_eip
= tcg_global_mem_new(cpu_env
, offsetof(CPUX86State
, eip
), eip_name
);
6885 for (i
= 0; i
< CPU_NB_REGS
; ++i
) {
6886 cpu_regs
[i
] = tcg_global_mem_new(cpu_env
,
6887 offsetof(CPUX86State
, regs
[i
]),
6891 for (i
= 0; i
< 6; ++i
) {
6893 = tcg_global_mem_new(cpu_env
,
6894 offsetof(CPUX86State
, segs
[i
].base
),
6898 for (i
= 0; i
< 4; ++i
) {
6900 = tcg_global_mem_new_i64(cpu_env
,
6901 offsetof(CPUX86State
, bnd_regs
[i
].lb
),
6904 = tcg_global_mem_new_i64(cpu_env
,
6905 offsetof(CPUX86State
, bnd_regs
[i
].ub
),
6910 static void i386_tr_init_disas_context(DisasContextBase
*dcbase
, CPUState
*cpu
)
6912 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
6913 CPUX86State
*env
= cpu
->env_ptr
;
6914 uint32_t flags
= dc
->base
.tb
->flags
;
6915 uint32_t cflags
= tb_cflags(dc
->base
.tb
);
6916 int cpl
= (flags
>> HF_CPL_SHIFT
) & 3;
6917 int iopl
= (flags
>> IOPL_SHIFT
) & 3;
6919 dc
->cs_base
= dc
->base
.tb
->cs_base
;
6920 dc
->pc_save
= dc
->base
.pc_next
;
6922 #ifndef CONFIG_USER_ONLY
6927 /* We make some simplifying assumptions; validate they're correct. */
6928 g_assert(PE(dc
) == ((flags
& HF_PE_MASK
) != 0));
6929 g_assert(CPL(dc
) == cpl
);
6930 g_assert(IOPL(dc
) == iopl
);
6931 g_assert(VM86(dc
) == ((flags
& HF_VM_MASK
) != 0));
6932 g_assert(CODE32(dc
) == ((flags
& HF_CS32_MASK
) != 0));
6933 g_assert(CODE64(dc
) == ((flags
& HF_CS64_MASK
) != 0));
6934 g_assert(SS32(dc
) == ((flags
& HF_SS32_MASK
) != 0));
6935 g_assert(LMA(dc
) == ((flags
& HF_LMA_MASK
) != 0));
6936 g_assert(ADDSEG(dc
) == ((flags
& HF_ADDSEG_MASK
) != 0));
6937 g_assert(SVME(dc
) == ((flags
& HF_SVME_MASK
) != 0));
6938 g_assert(GUEST(dc
) == ((flags
& HF_GUEST_MASK
) != 0));
6940 dc
->cc_op
= CC_OP_DYNAMIC
;
6941 dc
->cc_op_dirty
= false;
6942 dc
->popl_esp_hack
= 0;
6943 /* select memory access functions */
6945 #ifdef CONFIG_SOFTMMU
6946 dc
->mem_index
= cpu_mmu_index(env
, false);
6948 dc
->cpuid_features
= env
->features
[FEAT_1_EDX
];
6949 dc
->cpuid_ext_features
= env
->features
[FEAT_1_ECX
];
6950 dc
->cpuid_ext2_features
= env
->features
[FEAT_8000_0001_EDX
];
6951 dc
->cpuid_ext3_features
= env
->features
[FEAT_8000_0001_ECX
];
6952 dc
->cpuid_7_0_ebx_features
= env
->features
[FEAT_7_0_EBX
];
6953 dc
->cpuid_7_0_ecx_features
= env
->features
[FEAT_7_0_ECX
];
6954 dc
->cpuid_xsave_features
= env
->features
[FEAT_XSAVE
];
6955 dc
->jmp_opt
= !((cflags
& CF_NO_GOTO_TB
) ||
6956 (flags
& (HF_TF_MASK
| HF_INHIBIT_IRQ_MASK
)));
6958 * If jmp_opt, we want to handle each string instruction individually.
6959 * For icount also disable repz optimization so that each iteration
6960 * is accounted separately.
6962 dc
->repz_opt
= !dc
->jmp_opt
&& !(cflags
& CF_USE_ICOUNT
);
6964 dc
->T0
= tcg_temp_new();
6965 dc
->T1
= tcg_temp_new();
6966 dc
->A0
= tcg_temp_new();
6968 dc
->tmp0
= tcg_temp_new();
6969 dc
->tmp1_i64
= tcg_temp_new_i64();
6970 dc
->tmp2_i32
= tcg_temp_new_i32();
6971 dc
->tmp3_i32
= tcg_temp_new_i32();
6972 dc
->tmp4
= tcg_temp_new();
6973 dc
->cc_srcT
= tcg_temp_new();
6976 static void i386_tr_tb_start(DisasContextBase
*db
, CPUState
*cpu
)
6980 static void i386_tr_insn_start(DisasContextBase
*dcbase
, CPUState
*cpu
)
6982 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
6983 target_ulong pc_arg
= dc
->base
.pc_next
;
6985 dc
->prev_insn_end
= tcg_last_op();
6986 if (tb_cflags(dcbase
->tb
) & CF_PCREL
) {
6987 pc_arg
-= dc
->cs_base
;
6988 pc_arg
&= ~TARGET_PAGE_MASK
;
6990 tcg_gen_insn_start(pc_arg
, dc
->cc_op
);
6993 static void i386_tr_translate_insn(DisasContextBase
*dcbase
, CPUState
*cpu
)
6995 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
6997 #ifdef TARGET_VSYSCALL_PAGE
6999 * Detect entry into the vsyscall page and invoke the syscall.
7001 if ((dc
->base
.pc_next
& TARGET_PAGE_MASK
) == TARGET_VSYSCALL_PAGE
) {
7002 gen_exception(dc
, EXCP_VSYSCALL
);
7003 dc
->base
.pc_next
= dc
->pc
+ 1;
7008 if (disas_insn(dc
, cpu
)) {
7009 target_ulong pc_next
= dc
->pc
;
7010 dc
->base
.pc_next
= pc_next
;
7012 if (dc
->base
.is_jmp
== DISAS_NEXT
) {
7013 if (dc
->flags
& (HF_TF_MASK
| HF_INHIBIT_IRQ_MASK
)) {
7015 * If single step mode, we generate only one instruction and
7016 * generate an exception.
7017 * If irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
7018 * the flag and abort the translation to give the irqs a
7021 dc
->base
.is_jmp
= DISAS_EOB_NEXT
;
7022 } else if (!is_same_page(&dc
->base
, pc_next
)) {
7023 dc
->base
.is_jmp
= DISAS_TOO_MANY
;
7029 static void i386_tr_tb_stop(DisasContextBase
*dcbase
, CPUState
*cpu
)
7031 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
7033 switch (dc
->base
.is_jmp
) {
7034 case DISAS_NORETURN
:
7036 case DISAS_TOO_MANY
:
7037 gen_update_cc_op(dc
);
7038 gen_jmp_rel_csize(dc
, 0, 0);
7040 case DISAS_EOB_NEXT
:
7041 gen_update_cc_op(dc
);
7042 gen_update_eip_cur(dc
);
7044 case DISAS_EOB_ONLY
:
7047 case DISAS_EOB_INHIBIT_IRQ
:
7048 gen_update_cc_op(dc
);
7049 gen_update_eip_cur(dc
);
7050 gen_eob_inhibit_irq(dc
, true);
7056 g_assert_not_reached();
7060 static void i386_tr_disas_log(const DisasContextBase
*dcbase
,
7061 CPUState
*cpu
, FILE *logfile
)
7063 DisasContext
*dc
= container_of(dcbase
, DisasContext
, base
);
7065 fprintf(logfile
, "IN: %s\n", lookup_symbol(dc
->base
.pc_first
));
7066 target_disas(logfile
, cpu
, dc
->base
.pc_first
, dc
->base
.tb
->size
);
7069 static const TranslatorOps i386_tr_ops
= {
7070 .init_disas_context
= i386_tr_init_disas_context
,
7071 .tb_start
= i386_tr_tb_start
,
7072 .insn_start
= i386_tr_insn_start
,
7073 .translate_insn
= i386_tr_translate_insn
,
7074 .tb_stop
= i386_tr_tb_stop
,
7075 .disas_log
= i386_tr_disas_log
,
7078 /* generate intermediate code for basic block 'tb'. */
7079 void gen_intermediate_code(CPUState
*cpu
, TranslationBlock
*tb
, int *max_insns
,
7080 target_ulong pc
, void *host_pc
)
7084 translator_loop(cpu
, tb
, max_insns
, pc
, host_pc
, &i386_tr_ops
, &dc
.base
);