block: drop empty .bdrv_close handlers
[qemu/ar7.git] / target / i386 / translate.c
blob07d185e7b6086593e55bcb589a7f90959ef49aa0
1 /*
2 * i386 translation
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "qemu/host-utils.h"
22 #include "cpu.h"
23 #include "disas/disas.h"
24 #include "exec/exec-all.h"
25 #include "tcg-op.h"
26 #include "exec/cpu_ldst.h"
27 #include "exec/translator.h"
29 #include "exec/helper-proto.h"
30 #include "exec/helper-gen.h"
32 #include "trace-tcg.h"
33 #include "exec/log.h"
35 #define PREFIX_REPZ 0x01
36 #define PREFIX_REPNZ 0x02
37 #define PREFIX_LOCK 0x04
38 #define PREFIX_DATA 0x08
39 #define PREFIX_ADR 0x10
40 #define PREFIX_VEX 0x20
42 #ifdef TARGET_X86_64
43 #define CODE64(s) ((s)->code64)
44 #define REX_X(s) ((s)->rex_x)
45 #define REX_B(s) ((s)->rex_b)
46 #else
47 #define CODE64(s) 0
48 #define REX_X(s) 0
49 #define REX_B(s) 0
50 #endif
52 #ifdef TARGET_X86_64
53 # define ctztl ctz64
54 # define clztl clz64
55 #else
56 # define ctztl ctz32
57 # define clztl clz32
58 #endif
60 /* For a switch indexed by MODRM, match all memory operands for a given OP. */
61 #define CASE_MODRM_MEM_OP(OP) \
62 case (0 << 6) | (OP << 3) | 0 ... (0 << 6) | (OP << 3) | 7: \
63 case (1 << 6) | (OP << 3) | 0 ... (1 << 6) | (OP << 3) | 7: \
64 case (2 << 6) | (OP << 3) | 0 ... (2 << 6) | (OP << 3) | 7
66 #define CASE_MODRM_OP(OP) \
67 case (0 << 6) | (OP << 3) | 0 ... (0 << 6) | (OP << 3) | 7: \
68 case (1 << 6) | (OP << 3) | 0 ... (1 << 6) | (OP << 3) | 7: \
69 case (2 << 6) | (OP << 3) | 0 ... (2 << 6) | (OP << 3) | 7: \
70 case (3 << 6) | (OP << 3) | 0 ... (3 << 6) | (OP << 3) | 7
72 //#define MACRO_TEST 1
74 /* global register indexes */
75 static TCGv cpu_A0;
76 static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT;
77 static TCGv_i32 cpu_cc_op;
78 static TCGv cpu_regs[CPU_NB_REGS];
79 static TCGv cpu_seg_base[6];
80 static TCGv_i64 cpu_bndl[4];
81 static TCGv_i64 cpu_bndu[4];
82 /* local temps */
83 static TCGv cpu_T0, cpu_T1;
84 /* local register indexes (only used inside old micro ops) */
85 static TCGv cpu_tmp0, cpu_tmp4;
86 static TCGv_ptr cpu_ptr0, cpu_ptr1;
87 static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
88 static TCGv_i64 cpu_tmp1_i64;
90 #include "exec/gen-icount.h"
92 #ifdef TARGET_X86_64
93 static int x86_64_hregs;
94 #endif
96 typedef struct DisasContext {
97 DisasContextBase base;
99 /* current insn context */
100 int override; /* -1 if no override */
101 int prefix;
102 TCGMemOp aflag;
103 TCGMemOp dflag;
104 target_ulong pc_start;
105 target_ulong pc; /* pc = eip + cs_base */
106 /* current block context */
107 target_ulong cs_base; /* base of CS segment */
108 int pe; /* protected mode */
109 int code32; /* 32 bit code segment */
110 #ifdef TARGET_X86_64
111 int lma; /* long mode active */
112 int code64; /* 64 bit code segment */
113 int rex_x, rex_b;
114 #endif
115 int vex_l; /* vex vector length */
116 int vex_v; /* vex vvvv register, without 1's complement. */
117 int ss32; /* 32 bit stack segment */
118 CCOp cc_op; /* current CC operation */
119 bool cc_op_dirty;
120 int addseg; /* non zero if either DS/ES/SS have a non zero base */
121 int f_st; /* currently unused */
122 int vm86; /* vm86 mode */
123 int cpl;
124 int iopl;
125 int tf; /* TF cpu flag */
126 int jmp_opt; /* use direct block chaining for direct jumps */
127 int repz_opt; /* optimize jumps within repz instructions */
128 int mem_index; /* select memory access functions */
129 uint64_t flags; /* all execution flags */
130 int popl_esp_hack; /* for correct popl with esp base handling */
131 int rip_offset; /* only used in x86_64, but left for simplicity */
132 int cpuid_features;
133 int cpuid_ext_features;
134 int cpuid_ext2_features;
135 int cpuid_ext3_features;
136 int cpuid_7_0_ebx_features;
137 int cpuid_xsave_features;
138 sigjmp_buf jmpbuf;
139 } DisasContext;
141 static void gen_eob(DisasContext *s);
142 static void gen_jr(DisasContext *s, TCGv dest);
143 static void gen_jmp(DisasContext *s, target_ulong eip);
144 static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
145 static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d);
147 /* i386 arith/logic operations */
148 enum {
149 OP_ADDL,
150 OP_ORL,
151 OP_ADCL,
152 OP_SBBL,
153 OP_ANDL,
154 OP_SUBL,
155 OP_XORL,
156 OP_CMPL,
159 /* i386 shift ops */
160 enum {
161 OP_ROL,
162 OP_ROR,
163 OP_RCL,
164 OP_RCR,
165 OP_SHL,
166 OP_SHR,
167 OP_SHL1, /* undocumented */
168 OP_SAR = 7,
171 enum {
172 JCC_O,
173 JCC_B,
174 JCC_Z,
175 JCC_BE,
176 JCC_S,
177 JCC_P,
178 JCC_L,
179 JCC_LE,
182 enum {
183 /* I386 int registers */
184 OR_EAX, /* MUST be even numbered */
185 OR_ECX,
186 OR_EDX,
187 OR_EBX,
188 OR_ESP,
189 OR_EBP,
190 OR_ESI,
191 OR_EDI,
193 OR_TMP0 = 16, /* temporary operand register */
194 OR_TMP1,
195 OR_A0, /* temporary register used when doing address evaluation */
198 enum {
199 USES_CC_DST = 1,
200 USES_CC_SRC = 2,
201 USES_CC_SRC2 = 4,
202 USES_CC_SRCT = 8,
205 /* Bit set if the global variable is live after setting CC_OP to X. */
206 static const uint8_t cc_op_live[CC_OP_NB] = {
207 [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
208 [CC_OP_EFLAGS] = USES_CC_SRC,
209 [CC_OP_MULB ... CC_OP_MULQ] = USES_CC_DST | USES_CC_SRC,
210 [CC_OP_ADDB ... CC_OP_ADDQ] = USES_CC_DST | USES_CC_SRC,
211 [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
212 [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRCT,
213 [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
214 [CC_OP_LOGICB ... CC_OP_LOGICQ] = USES_CC_DST,
215 [CC_OP_INCB ... CC_OP_INCQ] = USES_CC_DST | USES_CC_SRC,
216 [CC_OP_DECB ... CC_OP_DECQ] = USES_CC_DST | USES_CC_SRC,
217 [CC_OP_SHLB ... CC_OP_SHLQ] = USES_CC_DST | USES_CC_SRC,
218 [CC_OP_SARB ... CC_OP_SARQ] = USES_CC_DST | USES_CC_SRC,
219 [CC_OP_BMILGB ... CC_OP_BMILGQ] = USES_CC_DST | USES_CC_SRC,
220 [CC_OP_ADCX] = USES_CC_DST | USES_CC_SRC,
221 [CC_OP_ADOX] = USES_CC_SRC | USES_CC_SRC2,
222 [CC_OP_ADCOX] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
223 [CC_OP_CLR] = 0,
224 [CC_OP_POPCNT] = USES_CC_SRC,
227 static void set_cc_op(DisasContext *s, CCOp op)
229 int dead;
231 if (s->cc_op == op) {
232 return;
235 /* Discard CC computation that will no longer be used. */
236 dead = cc_op_live[s->cc_op] & ~cc_op_live[op];
237 if (dead & USES_CC_DST) {
238 tcg_gen_discard_tl(cpu_cc_dst);
240 if (dead & USES_CC_SRC) {
241 tcg_gen_discard_tl(cpu_cc_src);
243 if (dead & USES_CC_SRC2) {
244 tcg_gen_discard_tl(cpu_cc_src2);
246 if (dead & USES_CC_SRCT) {
247 tcg_gen_discard_tl(cpu_cc_srcT);
250 if (op == CC_OP_DYNAMIC) {
251 /* The DYNAMIC setting is translator only, and should never be
252 stored. Thus we always consider it clean. */
253 s->cc_op_dirty = false;
254 } else {
255 /* Discard any computed CC_OP value (see shifts). */
256 if (s->cc_op == CC_OP_DYNAMIC) {
257 tcg_gen_discard_i32(cpu_cc_op);
259 s->cc_op_dirty = true;
261 s->cc_op = op;
264 static void gen_update_cc_op(DisasContext *s)
266 if (s->cc_op_dirty) {
267 tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
268 s->cc_op_dirty = false;
272 #ifdef TARGET_X86_64
274 #define NB_OP_SIZES 4
276 #else /* !TARGET_X86_64 */
278 #define NB_OP_SIZES 3
280 #endif /* !TARGET_X86_64 */
282 #if defined(HOST_WORDS_BIGENDIAN)
283 #define REG_B_OFFSET (sizeof(target_ulong) - 1)
284 #define REG_H_OFFSET (sizeof(target_ulong) - 2)
285 #define REG_W_OFFSET (sizeof(target_ulong) - 2)
286 #define REG_L_OFFSET (sizeof(target_ulong) - 4)
287 #define REG_LH_OFFSET (sizeof(target_ulong) - 8)
288 #else
289 #define REG_B_OFFSET 0
290 #define REG_H_OFFSET 1
291 #define REG_W_OFFSET 0
292 #define REG_L_OFFSET 0
293 #define REG_LH_OFFSET 4
294 #endif
296 /* In instruction encodings for byte register accesses the
297 * register number usually indicates "low 8 bits of register N";
298 * however there are some special cases where N 4..7 indicates
299 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
300 * true for this special case, false otherwise.
302 static inline bool byte_reg_is_xH(int reg)
304 if (reg < 4) {
305 return false;
307 #ifdef TARGET_X86_64
308 if (reg >= 8 || x86_64_hregs) {
309 return false;
311 #endif
312 return true;
315 /* Select the size of a push/pop operation. */
316 static inline TCGMemOp mo_pushpop(DisasContext *s, TCGMemOp ot)
318 if (CODE64(s)) {
319 return ot == MO_16 ? MO_16 : MO_64;
320 } else {
321 return ot;
325 /* Select the size of the stack pointer. */
326 static inline TCGMemOp mo_stacksize(DisasContext *s)
328 return CODE64(s) ? MO_64 : s->ss32 ? MO_32 : MO_16;
331 /* Select only size 64 else 32. Used for SSE operand sizes. */
332 static inline TCGMemOp mo_64_32(TCGMemOp ot)
334 #ifdef TARGET_X86_64
335 return ot == MO_64 ? MO_64 : MO_32;
336 #else
337 return MO_32;
338 #endif
341 /* Select size 8 if lsb of B is clear, else OT. Used for decoding
342 byte vs word opcodes. */
343 static inline TCGMemOp mo_b_d(int b, TCGMemOp ot)
345 return b & 1 ? ot : MO_8;
348 /* Select size 8 if lsb of B is clear, else OT capped at 32.
349 Used for decoding operand size of port opcodes. */
350 static inline TCGMemOp mo_b_d32(int b, TCGMemOp ot)
352 return b & 1 ? (ot == MO_16 ? MO_16 : MO_32) : MO_8;
355 static void gen_op_mov_reg_v(TCGMemOp ot, int reg, TCGv t0)
357 switch(ot) {
358 case MO_8:
359 if (!byte_reg_is_xH(reg)) {
360 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
361 } else {
362 tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
364 break;
365 case MO_16:
366 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
367 break;
368 case MO_32:
369 /* For x86_64, this sets the higher half of register to zero.
370 For i386, this is equivalent to a mov. */
371 tcg_gen_ext32u_tl(cpu_regs[reg], t0);
372 break;
373 #ifdef TARGET_X86_64
374 case MO_64:
375 tcg_gen_mov_tl(cpu_regs[reg], t0);
376 break;
377 #endif
378 default:
379 tcg_abort();
383 static inline void gen_op_mov_v_reg(TCGMemOp ot, TCGv t0, int reg)
385 if (ot == MO_8 && byte_reg_is_xH(reg)) {
386 tcg_gen_extract_tl(t0, cpu_regs[reg - 4], 8, 8);
387 } else {
388 tcg_gen_mov_tl(t0, cpu_regs[reg]);
392 static void gen_add_A0_im(DisasContext *s, int val)
394 tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
395 if (!CODE64(s)) {
396 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
400 static inline void gen_op_jmp_v(TCGv dest)
402 tcg_gen_st_tl(dest, cpu_env, offsetof(CPUX86State, eip));
405 static inline void gen_op_add_reg_im(TCGMemOp size, int reg, int32_t val)
407 tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
408 gen_op_mov_reg_v(size, reg, cpu_tmp0);
411 static inline void gen_op_add_reg_T0(TCGMemOp size, int reg)
413 tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T0);
414 gen_op_mov_reg_v(size, reg, cpu_tmp0);
417 static inline void gen_op_ld_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
419 tcg_gen_qemu_ld_tl(t0, a0, s->mem_index, idx | MO_LE);
422 static inline void gen_op_st_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
424 tcg_gen_qemu_st_tl(t0, a0, s->mem_index, idx | MO_LE);
427 static inline void gen_op_st_rm_T0_A0(DisasContext *s, int idx, int d)
429 if (d == OR_TMP0) {
430 gen_op_st_v(s, idx, cpu_T0, cpu_A0);
431 } else {
432 gen_op_mov_reg_v(idx, d, cpu_T0);
436 static inline void gen_jmp_im(target_ulong pc)
438 tcg_gen_movi_tl(cpu_tmp0, pc);
439 gen_op_jmp_v(cpu_tmp0);
442 /* Compute SEG:REG into A0. SEG is selected from the override segment
443 (OVR_SEG) and the default segment (DEF_SEG). OVR_SEG may be -1 to
444 indicate no override. */
445 static void gen_lea_v_seg(DisasContext *s, TCGMemOp aflag, TCGv a0,
446 int def_seg, int ovr_seg)
448 switch (aflag) {
449 #ifdef TARGET_X86_64
450 case MO_64:
451 if (ovr_seg < 0) {
452 tcg_gen_mov_tl(cpu_A0, a0);
453 return;
455 break;
456 #endif
457 case MO_32:
458 /* 32 bit address */
459 if (ovr_seg < 0 && s->addseg) {
460 ovr_seg = def_seg;
462 if (ovr_seg < 0) {
463 tcg_gen_ext32u_tl(cpu_A0, a0);
464 return;
466 break;
467 case MO_16:
468 /* 16 bit address */
469 tcg_gen_ext16u_tl(cpu_A0, a0);
470 a0 = cpu_A0;
471 if (ovr_seg < 0) {
472 if (s->addseg) {
473 ovr_seg = def_seg;
474 } else {
475 return;
478 break;
479 default:
480 tcg_abort();
483 if (ovr_seg >= 0) {
484 TCGv seg = cpu_seg_base[ovr_seg];
486 if (aflag == MO_64) {
487 tcg_gen_add_tl(cpu_A0, a0, seg);
488 } else if (CODE64(s)) {
489 tcg_gen_ext32u_tl(cpu_A0, a0);
490 tcg_gen_add_tl(cpu_A0, cpu_A0, seg);
491 } else {
492 tcg_gen_add_tl(cpu_A0, a0, seg);
493 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
498 static inline void gen_string_movl_A0_ESI(DisasContext *s)
500 gen_lea_v_seg(s, s->aflag, cpu_regs[R_ESI], R_DS, s->override);
503 static inline void gen_string_movl_A0_EDI(DisasContext *s)
505 gen_lea_v_seg(s, s->aflag, cpu_regs[R_EDI], R_ES, -1);
508 static inline void gen_op_movl_T0_Dshift(TCGMemOp ot)
510 tcg_gen_ld32s_tl(cpu_T0, cpu_env, offsetof(CPUX86State, df));
511 tcg_gen_shli_tl(cpu_T0, cpu_T0, ot);
514 static TCGv gen_ext_tl(TCGv dst, TCGv src, TCGMemOp size, bool sign)
516 switch (size) {
517 case MO_8:
518 if (sign) {
519 tcg_gen_ext8s_tl(dst, src);
520 } else {
521 tcg_gen_ext8u_tl(dst, src);
523 return dst;
524 case MO_16:
525 if (sign) {
526 tcg_gen_ext16s_tl(dst, src);
527 } else {
528 tcg_gen_ext16u_tl(dst, src);
530 return dst;
531 #ifdef TARGET_X86_64
532 case MO_32:
533 if (sign) {
534 tcg_gen_ext32s_tl(dst, src);
535 } else {
536 tcg_gen_ext32u_tl(dst, src);
538 return dst;
539 #endif
540 default:
541 return src;
545 static void gen_extu(TCGMemOp ot, TCGv reg)
547 gen_ext_tl(reg, reg, ot, false);
550 static void gen_exts(TCGMemOp ot, TCGv reg)
552 gen_ext_tl(reg, reg, ot, true);
555 static inline void gen_op_jnz_ecx(TCGMemOp size, TCGLabel *label1)
557 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
558 gen_extu(size, cpu_tmp0);
559 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
562 static inline void gen_op_jz_ecx(TCGMemOp size, TCGLabel *label1)
564 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
565 gen_extu(size, cpu_tmp0);
566 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
569 static void gen_helper_in_func(TCGMemOp ot, TCGv v, TCGv_i32 n)
571 switch (ot) {
572 case MO_8:
573 gen_helper_inb(v, cpu_env, n);
574 break;
575 case MO_16:
576 gen_helper_inw(v, cpu_env, n);
577 break;
578 case MO_32:
579 gen_helper_inl(v, cpu_env, n);
580 break;
581 default:
582 tcg_abort();
586 static void gen_helper_out_func(TCGMemOp ot, TCGv_i32 v, TCGv_i32 n)
588 switch (ot) {
589 case MO_8:
590 gen_helper_outb(cpu_env, v, n);
591 break;
592 case MO_16:
593 gen_helper_outw(cpu_env, v, n);
594 break;
595 case MO_32:
596 gen_helper_outl(cpu_env, v, n);
597 break;
598 default:
599 tcg_abort();
603 static void gen_check_io(DisasContext *s, TCGMemOp ot, target_ulong cur_eip,
604 uint32_t svm_flags)
606 target_ulong next_eip;
608 if (s->pe && (s->cpl > s->iopl || s->vm86)) {
609 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
610 switch (ot) {
611 case MO_8:
612 gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
613 break;
614 case MO_16:
615 gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
616 break;
617 case MO_32:
618 gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
619 break;
620 default:
621 tcg_abort();
624 if(s->flags & HF_SVMI_MASK) {
625 gen_update_cc_op(s);
626 gen_jmp_im(cur_eip);
627 svm_flags |= (1 << (4 + ot));
628 next_eip = s->pc - s->cs_base;
629 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
630 gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
631 tcg_const_i32(svm_flags),
632 tcg_const_i32(next_eip - cur_eip));
636 static inline void gen_movs(DisasContext *s, TCGMemOp ot)
638 gen_string_movl_A0_ESI(s);
639 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
640 gen_string_movl_A0_EDI(s);
641 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
642 gen_op_movl_T0_Dshift(ot);
643 gen_op_add_reg_T0(s->aflag, R_ESI);
644 gen_op_add_reg_T0(s->aflag, R_EDI);
647 static void gen_op_update1_cc(void)
649 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
652 static void gen_op_update2_cc(void)
654 tcg_gen_mov_tl(cpu_cc_src, cpu_T1);
655 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
658 static void gen_op_update3_cc(TCGv reg)
660 tcg_gen_mov_tl(cpu_cc_src2, reg);
661 tcg_gen_mov_tl(cpu_cc_src, cpu_T1);
662 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
665 static inline void gen_op_testl_T0_T1_cc(void)
667 tcg_gen_and_tl(cpu_cc_dst, cpu_T0, cpu_T1);
670 static void gen_op_update_neg_cc(void)
672 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
673 tcg_gen_neg_tl(cpu_cc_src, cpu_T0);
674 tcg_gen_movi_tl(cpu_cc_srcT, 0);
677 /* compute all eflags to cc_src */
678 static void gen_compute_eflags(DisasContext *s)
680 TCGv zero, dst, src1, src2;
681 int live, dead;
683 if (s->cc_op == CC_OP_EFLAGS) {
684 return;
686 if (s->cc_op == CC_OP_CLR) {
687 tcg_gen_movi_tl(cpu_cc_src, CC_Z | CC_P);
688 set_cc_op(s, CC_OP_EFLAGS);
689 return;
692 zero = NULL;
693 dst = cpu_cc_dst;
694 src1 = cpu_cc_src;
695 src2 = cpu_cc_src2;
697 /* Take care to not read values that are not live. */
698 live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
699 dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
700 if (dead) {
701 zero = tcg_const_tl(0);
702 if (dead & USES_CC_DST) {
703 dst = zero;
705 if (dead & USES_CC_SRC) {
706 src1 = zero;
708 if (dead & USES_CC_SRC2) {
709 src2 = zero;
713 gen_update_cc_op(s);
714 gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
715 set_cc_op(s, CC_OP_EFLAGS);
717 if (dead) {
718 tcg_temp_free(zero);
722 typedef struct CCPrepare {
723 TCGCond cond;
724 TCGv reg;
725 TCGv reg2;
726 target_ulong imm;
727 target_ulong mask;
728 bool use_reg2;
729 bool no_setcond;
730 } CCPrepare;
732 /* compute eflags.C to reg */
733 static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
735 TCGv t0, t1;
736 int size, shift;
738 switch (s->cc_op) {
739 case CC_OP_SUBB ... CC_OP_SUBQ:
740 /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
741 size = s->cc_op - CC_OP_SUBB;
742 t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
743 /* If no temporary was used, be careful not to alias t1 and t0. */
744 t0 = t1 == cpu_cc_src ? cpu_tmp0 : reg;
745 tcg_gen_mov_tl(t0, cpu_cc_srcT);
746 gen_extu(size, t0);
747 goto add_sub;
749 case CC_OP_ADDB ... CC_OP_ADDQ:
750 /* (DATA_TYPE)CC_DST < (DATA_TYPE)CC_SRC */
751 size = s->cc_op - CC_OP_ADDB;
752 t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
753 t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
754 add_sub:
755 return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
756 .reg2 = t1, .mask = -1, .use_reg2 = true };
758 case CC_OP_LOGICB ... CC_OP_LOGICQ:
759 case CC_OP_CLR:
760 case CC_OP_POPCNT:
761 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
763 case CC_OP_INCB ... CC_OP_INCQ:
764 case CC_OP_DECB ... CC_OP_DECQ:
765 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
766 .mask = -1, .no_setcond = true };
768 case CC_OP_SHLB ... CC_OP_SHLQ:
769 /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
770 size = s->cc_op - CC_OP_SHLB;
771 shift = (8 << size) - 1;
772 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
773 .mask = (target_ulong)1 << shift };
775 case CC_OP_MULB ... CC_OP_MULQ:
776 return (CCPrepare) { .cond = TCG_COND_NE,
777 .reg = cpu_cc_src, .mask = -1 };
779 case CC_OP_BMILGB ... CC_OP_BMILGQ:
780 size = s->cc_op - CC_OP_BMILGB;
781 t0 = gen_ext_tl(reg, cpu_cc_src, size, false);
782 return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
784 case CC_OP_ADCX:
785 case CC_OP_ADCOX:
786 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
787 .mask = -1, .no_setcond = true };
789 case CC_OP_EFLAGS:
790 case CC_OP_SARB ... CC_OP_SARQ:
791 /* CC_SRC & 1 */
792 return (CCPrepare) { .cond = TCG_COND_NE,
793 .reg = cpu_cc_src, .mask = CC_C };
795 default:
796 /* The need to compute only C from CC_OP_DYNAMIC is important
797 in efficiently implementing e.g. INC at the start of a TB. */
798 gen_update_cc_op(s);
799 gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
800 cpu_cc_src2, cpu_cc_op);
801 return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
802 .mask = -1, .no_setcond = true };
806 /* compute eflags.P to reg */
807 static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
809 gen_compute_eflags(s);
810 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
811 .mask = CC_P };
814 /* compute eflags.S to reg */
815 static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
817 switch (s->cc_op) {
818 case CC_OP_DYNAMIC:
819 gen_compute_eflags(s);
820 /* FALLTHRU */
821 case CC_OP_EFLAGS:
822 case CC_OP_ADCX:
823 case CC_OP_ADOX:
824 case CC_OP_ADCOX:
825 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
826 .mask = CC_S };
827 case CC_OP_CLR:
828 case CC_OP_POPCNT:
829 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
830 default:
832 TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
833 TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
834 return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
839 /* compute eflags.O to reg */
840 static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
842 switch (s->cc_op) {
843 case CC_OP_ADOX:
844 case CC_OP_ADCOX:
845 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src2,
846 .mask = -1, .no_setcond = true };
847 case CC_OP_CLR:
848 case CC_OP_POPCNT:
849 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
850 default:
851 gen_compute_eflags(s);
852 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
853 .mask = CC_O };
857 /* compute eflags.Z to reg */
858 static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
860 switch (s->cc_op) {
861 case CC_OP_DYNAMIC:
862 gen_compute_eflags(s);
863 /* FALLTHRU */
864 case CC_OP_EFLAGS:
865 case CC_OP_ADCX:
866 case CC_OP_ADOX:
867 case CC_OP_ADCOX:
868 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
869 .mask = CC_Z };
870 case CC_OP_CLR:
871 return (CCPrepare) { .cond = TCG_COND_ALWAYS, .mask = -1 };
872 case CC_OP_POPCNT:
873 return (CCPrepare) { .cond = TCG_COND_EQ, .reg = cpu_cc_src,
874 .mask = -1 };
875 default:
877 TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
878 TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
879 return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
884 /* perform a conditional store into register 'reg' according to jump opcode
885 value 'b'. In the fast case, T0 is guaranted not to be used. */
886 static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
888 int inv, jcc_op, cond;
889 TCGMemOp size;
890 CCPrepare cc;
891 TCGv t0;
893 inv = b & 1;
894 jcc_op = (b >> 1) & 7;
896 switch (s->cc_op) {
897 case CC_OP_SUBB ... CC_OP_SUBQ:
898 /* We optimize relational operators for the cmp/jcc case. */
899 size = s->cc_op - CC_OP_SUBB;
900 switch (jcc_op) {
901 case JCC_BE:
902 tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
903 gen_extu(size, cpu_tmp4);
904 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
905 cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
906 .reg2 = t0, .mask = -1, .use_reg2 = true };
907 break;
909 case JCC_L:
910 cond = TCG_COND_LT;
911 goto fast_jcc_l;
912 case JCC_LE:
913 cond = TCG_COND_LE;
914 fast_jcc_l:
915 tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
916 gen_exts(size, cpu_tmp4);
917 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
918 cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
919 .reg2 = t0, .mask = -1, .use_reg2 = true };
920 break;
922 default:
923 goto slow_jcc;
925 break;
927 default:
928 slow_jcc:
929 /* This actually generates good code for JC, JZ and JS. */
930 switch (jcc_op) {
931 case JCC_O:
932 cc = gen_prepare_eflags_o(s, reg);
933 break;
934 case JCC_B:
935 cc = gen_prepare_eflags_c(s, reg);
936 break;
937 case JCC_Z:
938 cc = gen_prepare_eflags_z(s, reg);
939 break;
940 case JCC_BE:
941 gen_compute_eflags(s);
942 cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
943 .mask = CC_Z | CC_C };
944 break;
945 case JCC_S:
946 cc = gen_prepare_eflags_s(s, reg);
947 break;
948 case JCC_P:
949 cc = gen_prepare_eflags_p(s, reg);
950 break;
951 case JCC_L:
952 gen_compute_eflags(s);
953 if (reg == cpu_cc_src) {
954 reg = cpu_tmp0;
956 tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
957 tcg_gen_xor_tl(reg, reg, cpu_cc_src);
958 cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
959 .mask = CC_S };
960 break;
961 default:
962 case JCC_LE:
963 gen_compute_eflags(s);
964 if (reg == cpu_cc_src) {
965 reg = cpu_tmp0;
967 tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
968 tcg_gen_xor_tl(reg, reg, cpu_cc_src);
969 cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
970 .mask = CC_S | CC_Z };
971 break;
973 break;
976 if (inv) {
977 cc.cond = tcg_invert_cond(cc.cond);
979 return cc;
982 static void gen_setcc1(DisasContext *s, int b, TCGv reg)
984 CCPrepare cc = gen_prepare_cc(s, b, reg);
986 if (cc.no_setcond) {
987 if (cc.cond == TCG_COND_EQ) {
988 tcg_gen_xori_tl(reg, cc.reg, 1);
989 } else {
990 tcg_gen_mov_tl(reg, cc.reg);
992 return;
995 if (cc.cond == TCG_COND_NE && !cc.use_reg2 && cc.imm == 0 &&
996 cc.mask != 0 && (cc.mask & (cc.mask - 1)) == 0) {
997 tcg_gen_shri_tl(reg, cc.reg, ctztl(cc.mask));
998 tcg_gen_andi_tl(reg, reg, 1);
999 return;
1001 if (cc.mask != -1) {
1002 tcg_gen_andi_tl(reg, cc.reg, cc.mask);
1003 cc.reg = reg;
1005 if (cc.use_reg2) {
1006 tcg_gen_setcond_tl(cc.cond, reg, cc.reg, cc.reg2);
1007 } else {
1008 tcg_gen_setcondi_tl(cc.cond, reg, cc.reg, cc.imm);
1012 static inline void gen_compute_eflags_c(DisasContext *s, TCGv reg)
1014 gen_setcc1(s, JCC_B << 1, reg);
1017 /* generate a conditional jump to label 'l1' according to jump opcode
1018 value 'b'. In the fast case, T0 is guaranted not to be used. */
1019 static inline void gen_jcc1_noeob(DisasContext *s, int b, TCGLabel *l1)
1021 CCPrepare cc = gen_prepare_cc(s, b, cpu_T0);
1023 if (cc.mask != -1) {
1024 tcg_gen_andi_tl(cpu_T0, cc.reg, cc.mask);
1025 cc.reg = cpu_T0;
1027 if (cc.use_reg2) {
1028 tcg_gen_brcond_tl(cc.cond, cc.reg, cc.reg2, l1);
1029 } else {
1030 tcg_gen_brcondi_tl(cc.cond, cc.reg, cc.imm, l1);
1034 /* Generate a conditional jump to label 'l1' according to jump opcode
1035 value 'b'. In the fast case, T0 is guaranted not to be used.
1036 A translation block must end soon. */
1037 static inline void gen_jcc1(DisasContext *s, int b, TCGLabel *l1)
1039 CCPrepare cc = gen_prepare_cc(s, b, cpu_T0);
1041 gen_update_cc_op(s);
1042 if (cc.mask != -1) {
1043 tcg_gen_andi_tl(cpu_T0, cc.reg, cc.mask);
1044 cc.reg = cpu_T0;
1046 set_cc_op(s, CC_OP_DYNAMIC);
1047 if (cc.use_reg2) {
1048 tcg_gen_brcond_tl(cc.cond, cc.reg, cc.reg2, l1);
1049 } else {
1050 tcg_gen_brcondi_tl(cc.cond, cc.reg, cc.imm, l1);
1054 /* XXX: does not work with gdbstub "ice" single step - not a
1055 serious problem */
1056 static TCGLabel *gen_jz_ecx_string(DisasContext *s, target_ulong next_eip)
1058 TCGLabel *l1 = gen_new_label();
1059 TCGLabel *l2 = gen_new_label();
1060 gen_op_jnz_ecx(s->aflag, l1);
1061 gen_set_label(l2);
1062 gen_jmp_tb(s, next_eip, 1);
1063 gen_set_label(l1);
1064 return l2;
1067 static inline void gen_stos(DisasContext *s, TCGMemOp ot)
1069 gen_op_mov_v_reg(MO_32, cpu_T0, R_EAX);
1070 gen_string_movl_A0_EDI(s);
1071 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
1072 gen_op_movl_T0_Dshift(ot);
1073 gen_op_add_reg_T0(s->aflag, R_EDI);
1076 static inline void gen_lods(DisasContext *s, TCGMemOp ot)
1078 gen_string_movl_A0_ESI(s);
1079 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1080 gen_op_mov_reg_v(ot, R_EAX, cpu_T0);
1081 gen_op_movl_T0_Dshift(ot);
1082 gen_op_add_reg_T0(s->aflag, R_ESI);
1085 static inline void gen_scas(DisasContext *s, TCGMemOp ot)
1087 gen_string_movl_A0_EDI(s);
1088 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
1089 gen_op(s, OP_CMPL, ot, R_EAX);
1090 gen_op_movl_T0_Dshift(ot);
1091 gen_op_add_reg_T0(s->aflag, R_EDI);
1094 static inline void gen_cmps(DisasContext *s, TCGMemOp ot)
1096 gen_string_movl_A0_EDI(s);
1097 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
1098 gen_string_movl_A0_ESI(s);
1099 gen_op(s, OP_CMPL, ot, OR_TMP0);
1100 gen_op_movl_T0_Dshift(ot);
1101 gen_op_add_reg_T0(s->aflag, R_ESI);
1102 gen_op_add_reg_T0(s->aflag, R_EDI);
1105 static void gen_bpt_io(DisasContext *s, TCGv_i32 t_port, int ot)
1107 if (s->flags & HF_IOBPT_MASK) {
1108 TCGv_i32 t_size = tcg_const_i32(1 << ot);
1109 TCGv t_next = tcg_const_tl(s->pc - s->cs_base);
1111 gen_helper_bpt_io(cpu_env, t_port, t_size, t_next);
1112 tcg_temp_free_i32(t_size);
1113 tcg_temp_free(t_next);
1118 static inline void gen_ins(DisasContext *s, TCGMemOp ot)
1120 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
1121 gen_io_start();
1123 gen_string_movl_A0_EDI(s);
1124 /* Note: we must do this dummy write first to be restartable in
1125 case of page fault. */
1126 tcg_gen_movi_tl(cpu_T0, 0);
1127 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
1128 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1129 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
1130 gen_helper_in_func(ot, cpu_T0, cpu_tmp2_i32);
1131 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
1132 gen_op_movl_T0_Dshift(ot);
1133 gen_op_add_reg_T0(s->aflag, R_EDI);
1134 gen_bpt_io(s, cpu_tmp2_i32, ot);
1135 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
1136 gen_io_end();
1140 static inline void gen_outs(DisasContext *s, TCGMemOp ot)
1142 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
1143 gen_io_start();
1145 gen_string_movl_A0_ESI(s);
1146 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1148 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
1149 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
1150 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T0);
1151 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
1152 gen_op_movl_T0_Dshift(ot);
1153 gen_op_add_reg_T0(s->aflag, R_ESI);
1154 gen_bpt_io(s, cpu_tmp2_i32, ot);
1155 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
1156 gen_io_end();
1160 /* same method as Valgrind : we generate jumps to current or next
1161 instruction */
1162 #define GEN_REPZ(op) \
1163 static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
1164 target_ulong cur_eip, target_ulong next_eip) \
1166 TCGLabel *l2; \
1167 gen_update_cc_op(s); \
1168 l2 = gen_jz_ecx_string(s, next_eip); \
1169 gen_ ## op(s, ot); \
1170 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
1171 /* a loop would cause two single step exceptions if ECX = 1 \
1172 before rep string_insn */ \
1173 if (s->repz_opt) \
1174 gen_op_jz_ecx(s->aflag, l2); \
1175 gen_jmp(s, cur_eip); \
1178 #define GEN_REPZ2(op) \
1179 static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
1180 target_ulong cur_eip, \
1181 target_ulong next_eip, \
1182 int nz) \
1184 TCGLabel *l2; \
1185 gen_update_cc_op(s); \
1186 l2 = gen_jz_ecx_string(s, next_eip); \
1187 gen_ ## op(s, ot); \
1188 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
1189 gen_update_cc_op(s); \
1190 gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2); \
1191 if (s->repz_opt) \
1192 gen_op_jz_ecx(s->aflag, l2); \
1193 gen_jmp(s, cur_eip); \
1196 GEN_REPZ(movs)
1197 GEN_REPZ(stos)
1198 GEN_REPZ(lods)
1199 GEN_REPZ(ins)
1200 GEN_REPZ(outs)
1201 GEN_REPZ2(scas)
1202 GEN_REPZ2(cmps)
1204 static void gen_helper_fp_arith_ST0_FT0(int op)
1206 switch (op) {
1207 case 0:
1208 gen_helper_fadd_ST0_FT0(cpu_env);
1209 break;
1210 case 1:
1211 gen_helper_fmul_ST0_FT0(cpu_env);
1212 break;
1213 case 2:
1214 gen_helper_fcom_ST0_FT0(cpu_env);
1215 break;
1216 case 3:
1217 gen_helper_fcom_ST0_FT0(cpu_env);
1218 break;
1219 case 4:
1220 gen_helper_fsub_ST0_FT0(cpu_env);
1221 break;
1222 case 5:
1223 gen_helper_fsubr_ST0_FT0(cpu_env);
1224 break;
1225 case 6:
1226 gen_helper_fdiv_ST0_FT0(cpu_env);
1227 break;
1228 case 7:
1229 gen_helper_fdivr_ST0_FT0(cpu_env);
1230 break;
1234 /* NOTE the exception in "r" op ordering */
1235 static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
1237 TCGv_i32 tmp = tcg_const_i32(opreg);
1238 switch (op) {
1239 case 0:
1240 gen_helper_fadd_STN_ST0(cpu_env, tmp);
1241 break;
1242 case 1:
1243 gen_helper_fmul_STN_ST0(cpu_env, tmp);
1244 break;
1245 case 4:
1246 gen_helper_fsubr_STN_ST0(cpu_env, tmp);
1247 break;
1248 case 5:
1249 gen_helper_fsub_STN_ST0(cpu_env, tmp);
1250 break;
1251 case 6:
1252 gen_helper_fdivr_STN_ST0(cpu_env, tmp);
1253 break;
1254 case 7:
1255 gen_helper_fdiv_STN_ST0(cpu_env, tmp);
1256 break;
1260 /* if d == OR_TMP0, it means memory operand (address in A0) */
1261 static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d)
1263 if (d != OR_TMP0) {
1264 gen_op_mov_v_reg(ot, cpu_T0, d);
1265 } else if (!(s1->prefix & PREFIX_LOCK)) {
1266 gen_op_ld_v(s1, ot, cpu_T0, cpu_A0);
1268 switch(op) {
1269 case OP_ADCL:
1270 gen_compute_eflags_c(s1, cpu_tmp4);
1271 if (s1->prefix & PREFIX_LOCK) {
1272 tcg_gen_add_tl(cpu_T0, cpu_tmp4, cpu_T1);
1273 tcg_gen_atomic_add_fetch_tl(cpu_T0, cpu_A0, cpu_T0,
1274 s1->mem_index, ot | MO_LE);
1275 } else {
1276 tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
1277 tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_tmp4);
1278 gen_op_st_rm_T0_A0(s1, ot, d);
1280 gen_op_update3_cc(cpu_tmp4);
1281 set_cc_op(s1, CC_OP_ADCB + ot);
1282 break;
1283 case OP_SBBL:
1284 gen_compute_eflags_c(s1, cpu_tmp4);
1285 if (s1->prefix & PREFIX_LOCK) {
1286 tcg_gen_add_tl(cpu_T0, cpu_T1, cpu_tmp4);
1287 tcg_gen_neg_tl(cpu_T0, cpu_T0);
1288 tcg_gen_atomic_add_fetch_tl(cpu_T0, cpu_A0, cpu_T0,
1289 s1->mem_index, ot | MO_LE);
1290 } else {
1291 tcg_gen_sub_tl(cpu_T0, cpu_T0, cpu_T1);
1292 tcg_gen_sub_tl(cpu_T0, cpu_T0, cpu_tmp4);
1293 gen_op_st_rm_T0_A0(s1, ot, d);
1295 gen_op_update3_cc(cpu_tmp4);
1296 set_cc_op(s1, CC_OP_SBBB + ot);
1297 break;
1298 case OP_ADDL:
1299 if (s1->prefix & PREFIX_LOCK) {
1300 tcg_gen_atomic_add_fetch_tl(cpu_T0, cpu_A0, cpu_T1,
1301 s1->mem_index, ot | MO_LE);
1302 } else {
1303 tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
1304 gen_op_st_rm_T0_A0(s1, ot, d);
1306 gen_op_update2_cc();
1307 set_cc_op(s1, CC_OP_ADDB + ot);
1308 break;
1309 case OP_SUBL:
1310 if (s1->prefix & PREFIX_LOCK) {
1311 tcg_gen_neg_tl(cpu_T0, cpu_T1);
1312 tcg_gen_atomic_fetch_add_tl(cpu_cc_srcT, cpu_A0, cpu_T0,
1313 s1->mem_index, ot | MO_LE);
1314 tcg_gen_sub_tl(cpu_T0, cpu_cc_srcT, cpu_T1);
1315 } else {
1316 tcg_gen_mov_tl(cpu_cc_srcT, cpu_T0);
1317 tcg_gen_sub_tl(cpu_T0, cpu_T0, cpu_T1);
1318 gen_op_st_rm_T0_A0(s1, ot, d);
1320 gen_op_update2_cc();
1321 set_cc_op(s1, CC_OP_SUBB + ot);
1322 break;
1323 default:
1324 case OP_ANDL:
1325 if (s1->prefix & PREFIX_LOCK) {
1326 tcg_gen_atomic_and_fetch_tl(cpu_T0, cpu_A0, cpu_T1,
1327 s1->mem_index, ot | MO_LE);
1328 } else {
1329 tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_T1);
1330 gen_op_st_rm_T0_A0(s1, ot, d);
1332 gen_op_update1_cc();
1333 set_cc_op(s1, CC_OP_LOGICB + ot);
1334 break;
1335 case OP_ORL:
1336 if (s1->prefix & PREFIX_LOCK) {
1337 tcg_gen_atomic_or_fetch_tl(cpu_T0, cpu_A0, cpu_T1,
1338 s1->mem_index, ot | MO_LE);
1339 } else {
1340 tcg_gen_or_tl(cpu_T0, cpu_T0, cpu_T1);
1341 gen_op_st_rm_T0_A0(s1, ot, d);
1343 gen_op_update1_cc();
1344 set_cc_op(s1, CC_OP_LOGICB + ot);
1345 break;
1346 case OP_XORL:
1347 if (s1->prefix & PREFIX_LOCK) {
1348 tcg_gen_atomic_xor_fetch_tl(cpu_T0, cpu_A0, cpu_T1,
1349 s1->mem_index, ot | MO_LE);
1350 } else {
1351 tcg_gen_xor_tl(cpu_T0, cpu_T0, cpu_T1);
1352 gen_op_st_rm_T0_A0(s1, ot, d);
1354 gen_op_update1_cc();
1355 set_cc_op(s1, CC_OP_LOGICB + ot);
1356 break;
1357 case OP_CMPL:
1358 tcg_gen_mov_tl(cpu_cc_src, cpu_T1);
1359 tcg_gen_mov_tl(cpu_cc_srcT, cpu_T0);
1360 tcg_gen_sub_tl(cpu_cc_dst, cpu_T0, cpu_T1);
1361 set_cc_op(s1, CC_OP_SUBB + ot);
1362 break;
1366 /* if d == OR_TMP0, it means memory operand (address in A0) */
1367 static void gen_inc(DisasContext *s1, TCGMemOp ot, int d, int c)
1369 if (s1->prefix & PREFIX_LOCK) {
1370 tcg_gen_movi_tl(cpu_T0, c > 0 ? 1 : -1);
1371 tcg_gen_atomic_add_fetch_tl(cpu_T0, cpu_A0, cpu_T0,
1372 s1->mem_index, ot | MO_LE);
1373 } else {
1374 if (d != OR_TMP0) {
1375 gen_op_mov_v_reg(ot, cpu_T0, d);
1376 } else {
1377 gen_op_ld_v(s1, ot, cpu_T0, cpu_A0);
1379 tcg_gen_addi_tl(cpu_T0, cpu_T0, (c > 0 ? 1 : -1));
1380 gen_op_st_rm_T0_A0(s1, ot, d);
1383 gen_compute_eflags_c(s1, cpu_cc_src);
1384 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
1385 set_cc_op(s1, (c > 0 ? CC_OP_INCB : CC_OP_DECB) + ot);
1388 static void gen_shift_flags(DisasContext *s, TCGMemOp ot, TCGv result,
1389 TCGv shm1, TCGv count, bool is_right)
1391 TCGv_i32 z32, s32, oldop;
1392 TCGv z_tl;
1394 /* Store the results into the CC variables. If we know that the
1395 variable must be dead, store unconditionally. Otherwise we'll
1396 need to not disrupt the current contents. */
1397 z_tl = tcg_const_tl(0);
1398 if (cc_op_live[s->cc_op] & USES_CC_DST) {
1399 tcg_gen_movcond_tl(TCG_COND_NE, cpu_cc_dst, count, z_tl,
1400 result, cpu_cc_dst);
1401 } else {
1402 tcg_gen_mov_tl(cpu_cc_dst, result);
1404 if (cc_op_live[s->cc_op] & USES_CC_SRC) {
1405 tcg_gen_movcond_tl(TCG_COND_NE, cpu_cc_src, count, z_tl,
1406 shm1, cpu_cc_src);
1407 } else {
1408 tcg_gen_mov_tl(cpu_cc_src, shm1);
1410 tcg_temp_free(z_tl);
1412 /* Get the two potential CC_OP values into temporaries. */
1413 tcg_gen_movi_i32(cpu_tmp2_i32, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
1414 if (s->cc_op == CC_OP_DYNAMIC) {
1415 oldop = cpu_cc_op;
1416 } else {
1417 tcg_gen_movi_i32(cpu_tmp3_i32, s->cc_op);
1418 oldop = cpu_tmp3_i32;
1421 /* Conditionally store the CC_OP value. */
1422 z32 = tcg_const_i32(0);
1423 s32 = tcg_temp_new_i32();
1424 tcg_gen_trunc_tl_i32(s32, count);
1425 tcg_gen_movcond_i32(TCG_COND_NE, cpu_cc_op, s32, z32, cpu_tmp2_i32, oldop);
1426 tcg_temp_free_i32(z32);
1427 tcg_temp_free_i32(s32);
1429 /* The CC_OP value is no longer predictable. */
1430 set_cc_op(s, CC_OP_DYNAMIC);
1433 static void gen_shift_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1434 int is_right, int is_arith)
1436 target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1438 /* load */
1439 if (op1 == OR_TMP0) {
1440 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1441 } else {
1442 gen_op_mov_v_reg(ot, cpu_T0, op1);
1445 tcg_gen_andi_tl(cpu_T1, cpu_T1, mask);
1446 tcg_gen_subi_tl(cpu_tmp0, cpu_T1, 1);
1448 if (is_right) {
1449 if (is_arith) {
1450 gen_exts(ot, cpu_T0);
1451 tcg_gen_sar_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
1452 tcg_gen_sar_tl(cpu_T0, cpu_T0, cpu_T1);
1453 } else {
1454 gen_extu(ot, cpu_T0);
1455 tcg_gen_shr_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
1456 tcg_gen_shr_tl(cpu_T0, cpu_T0, cpu_T1);
1458 } else {
1459 tcg_gen_shl_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
1460 tcg_gen_shl_tl(cpu_T0, cpu_T0, cpu_T1);
1463 /* store */
1464 gen_op_st_rm_T0_A0(s, ot, op1);
1466 gen_shift_flags(s, ot, cpu_T0, cpu_tmp0, cpu_T1, is_right);
1469 static void gen_shift_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
1470 int is_right, int is_arith)
1472 int mask = (ot == MO_64 ? 0x3f : 0x1f);
1474 /* load */
1475 if (op1 == OR_TMP0)
1476 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1477 else
1478 gen_op_mov_v_reg(ot, cpu_T0, op1);
1480 op2 &= mask;
1481 if (op2 != 0) {
1482 if (is_right) {
1483 if (is_arith) {
1484 gen_exts(ot, cpu_T0);
1485 tcg_gen_sari_tl(cpu_tmp4, cpu_T0, op2 - 1);
1486 tcg_gen_sari_tl(cpu_T0, cpu_T0, op2);
1487 } else {
1488 gen_extu(ot, cpu_T0);
1489 tcg_gen_shri_tl(cpu_tmp4, cpu_T0, op2 - 1);
1490 tcg_gen_shri_tl(cpu_T0, cpu_T0, op2);
1492 } else {
1493 tcg_gen_shli_tl(cpu_tmp4, cpu_T0, op2 - 1);
1494 tcg_gen_shli_tl(cpu_T0, cpu_T0, op2);
1498 /* store */
1499 gen_op_st_rm_T0_A0(s, ot, op1);
1501 /* update eflags if non zero shift */
1502 if (op2 != 0) {
1503 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
1504 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
1505 set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
1509 static void gen_rot_rm_T1(DisasContext *s, TCGMemOp ot, int op1, int is_right)
1511 target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
1512 TCGv_i32 t0, t1;
1514 /* load */
1515 if (op1 == OR_TMP0) {
1516 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1517 } else {
1518 gen_op_mov_v_reg(ot, cpu_T0, op1);
1521 tcg_gen_andi_tl(cpu_T1, cpu_T1, mask);
1523 switch (ot) {
1524 case MO_8:
1525 /* Replicate the 8-bit input so that a 32-bit rotate works. */
1526 tcg_gen_ext8u_tl(cpu_T0, cpu_T0);
1527 tcg_gen_muli_tl(cpu_T0, cpu_T0, 0x01010101);
1528 goto do_long;
1529 case MO_16:
1530 /* Replicate the 16-bit input so that a 32-bit rotate works. */
1531 tcg_gen_deposit_tl(cpu_T0, cpu_T0, cpu_T0, 16, 16);
1532 goto do_long;
1533 do_long:
1534 #ifdef TARGET_X86_64
1535 case MO_32:
1536 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
1537 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
1538 if (is_right) {
1539 tcg_gen_rotr_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
1540 } else {
1541 tcg_gen_rotl_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
1543 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
1544 break;
1545 #endif
1546 default:
1547 if (is_right) {
1548 tcg_gen_rotr_tl(cpu_T0, cpu_T0, cpu_T1);
1549 } else {
1550 tcg_gen_rotl_tl(cpu_T0, cpu_T0, cpu_T1);
1552 break;
1555 /* store */
1556 gen_op_st_rm_T0_A0(s, ot, op1);
1558 /* We'll need the flags computed into CC_SRC. */
1559 gen_compute_eflags(s);
1561 /* The value that was "rotated out" is now present at the other end
1562 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1563 since we've computed the flags into CC_SRC, these variables are
1564 currently dead. */
1565 if (is_right) {
1566 tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask - 1);
1567 tcg_gen_shri_tl(cpu_cc_dst, cpu_T0, mask);
1568 tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1569 } else {
1570 tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask);
1571 tcg_gen_andi_tl(cpu_cc_dst, cpu_T0, 1);
1573 tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
1574 tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);
1576 /* Now conditionally store the new CC_OP value. If the shift count
1577 is 0 we keep the CC_OP_EFLAGS setting so that only CC_SRC is live.
1578 Otherwise reuse CC_OP_ADCOX which have the C and O flags split out
1579 exactly as we computed above. */
1580 t0 = tcg_const_i32(0);
1581 t1 = tcg_temp_new_i32();
1582 tcg_gen_trunc_tl_i32(t1, cpu_T1);
1583 tcg_gen_movi_i32(cpu_tmp2_i32, CC_OP_ADCOX);
1584 tcg_gen_movi_i32(cpu_tmp3_i32, CC_OP_EFLAGS);
1585 tcg_gen_movcond_i32(TCG_COND_NE, cpu_cc_op, t1, t0,
1586 cpu_tmp2_i32, cpu_tmp3_i32);
1587 tcg_temp_free_i32(t0);
1588 tcg_temp_free_i32(t1);
1590 /* The CC_OP value is no longer predictable. */
1591 set_cc_op(s, CC_OP_DYNAMIC);
1594 static void gen_rot_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
1595 int is_right)
1597 int mask = (ot == MO_64 ? 0x3f : 0x1f);
1598 int shift;
1600 /* load */
1601 if (op1 == OR_TMP0) {
1602 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1603 } else {
1604 gen_op_mov_v_reg(ot, cpu_T0, op1);
1607 op2 &= mask;
1608 if (op2 != 0) {
1609 switch (ot) {
1610 #ifdef TARGET_X86_64
1611 case MO_32:
1612 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
1613 if (is_right) {
1614 tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
1615 } else {
1616 tcg_gen_rotli_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
1618 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
1619 break;
1620 #endif
1621 default:
1622 if (is_right) {
1623 tcg_gen_rotri_tl(cpu_T0, cpu_T0, op2);
1624 } else {
1625 tcg_gen_rotli_tl(cpu_T0, cpu_T0, op2);
1627 break;
1628 case MO_8:
1629 mask = 7;
1630 goto do_shifts;
1631 case MO_16:
1632 mask = 15;
1633 do_shifts:
1634 shift = op2 & mask;
1635 if (is_right) {
1636 shift = mask + 1 - shift;
1638 gen_extu(ot, cpu_T0);
1639 tcg_gen_shli_tl(cpu_tmp0, cpu_T0, shift);
1640 tcg_gen_shri_tl(cpu_T0, cpu_T0, mask + 1 - shift);
1641 tcg_gen_or_tl(cpu_T0, cpu_T0, cpu_tmp0);
1642 break;
1646 /* store */
1647 gen_op_st_rm_T0_A0(s, ot, op1);
1649 if (op2 != 0) {
1650 /* Compute the flags into CC_SRC. */
1651 gen_compute_eflags(s);
1653 /* The value that was "rotated out" is now present at the other end
1654 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1655 since we've computed the flags into CC_SRC, these variables are
1656 currently dead. */
1657 if (is_right) {
1658 tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask - 1);
1659 tcg_gen_shri_tl(cpu_cc_dst, cpu_T0, mask);
1660 tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
1661 } else {
1662 tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask);
1663 tcg_gen_andi_tl(cpu_cc_dst, cpu_T0, 1);
1665 tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
1666 tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);
1667 set_cc_op(s, CC_OP_ADCOX);
1671 /* XXX: add faster immediate = 1 case */
1672 static void gen_rotc_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1673 int is_right)
1675 gen_compute_eflags(s);
1676 assert(s->cc_op == CC_OP_EFLAGS);
1678 /* load */
1679 if (op1 == OR_TMP0)
1680 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1681 else
1682 gen_op_mov_v_reg(ot, cpu_T0, op1);
1684 if (is_right) {
1685 switch (ot) {
1686 case MO_8:
1687 gen_helper_rcrb(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1688 break;
1689 case MO_16:
1690 gen_helper_rcrw(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1691 break;
1692 case MO_32:
1693 gen_helper_rcrl(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1694 break;
1695 #ifdef TARGET_X86_64
1696 case MO_64:
1697 gen_helper_rcrq(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1698 break;
1699 #endif
1700 default:
1701 tcg_abort();
1703 } else {
1704 switch (ot) {
1705 case MO_8:
1706 gen_helper_rclb(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1707 break;
1708 case MO_16:
1709 gen_helper_rclw(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1710 break;
1711 case MO_32:
1712 gen_helper_rcll(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1713 break;
1714 #ifdef TARGET_X86_64
1715 case MO_64:
1716 gen_helper_rclq(cpu_T0, cpu_env, cpu_T0, cpu_T1);
1717 break;
1718 #endif
1719 default:
1720 tcg_abort();
1723 /* store */
1724 gen_op_st_rm_T0_A0(s, ot, op1);
1727 /* XXX: add faster immediate case */
1728 static void gen_shiftd_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
1729 bool is_right, TCGv count_in)
1731 target_ulong mask = (ot == MO_64 ? 63 : 31);
1732 TCGv count;
1734 /* load */
1735 if (op1 == OR_TMP0) {
1736 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1737 } else {
1738 gen_op_mov_v_reg(ot, cpu_T0, op1);
1741 count = tcg_temp_new();
1742 tcg_gen_andi_tl(count, count_in, mask);
1744 switch (ot) {
1745 case MO_16:
1746 /* Note: we implement the Intel behaviour for shift count > 16.
1747 This means "shrdw C, B, A" shifts A:B:A >> C. Build the B:A
1748 portion by constructing it as a 32-bit value. */
1749 if (is_right) {
1750 tcg_gen_deposit_tl(cpu_tmp0, cpu_T0, cpu_T1, 16, 16);
1751 tcg_gen_mov_tl(cpu_T1, cpu_T0);
1752 tcg_gen_mov_tl(cpu_T0, cpu_tmp0);
1753 } else {
1754 tcg_gen_deposit_tl(cpu_T1, cpu_T0, cpu_T1, 16, 16);
1756 /* FALLTHRU */
1757 #ifdef TARGET_X86_64
1758 case MO_32:
1759 /* Concatenate the two 32-bit values and use a 64-bit shift. */
1760 tcg_gen_subi_tl(cpu_tmp0, count, 1);
1761 if (is_right) {
1762 tcg_gen_concat_tl_i64(cpu_T0, cpu_T0, cpu_T1);
1763 tcg_gen_shr_i64(cpu_tmp0, cpu_T0, cpu_tmp0);
1764 tcg_gen_shr_i64(cpu_T0, cpu_T0, count);
1765 } else {
1766 tcg_gen_concat_tl_i64(cpu_T0, cpu_T1, cpu_T0);
1767 tcg_gen_shl_i64(cpu_tmp0, cpu_T0, cpu_tmp0);
1768 tcg_gen_shl_i64(cpu_T0, cpu_T0, count);
1769 tcg_gen_shri_i64(cpu_tmp0, cpu_tmp0, 32);
1770 tcg_gen_shri_i64(cpu_T0, cpu_T0, 32);
1772 break;
1773 #endif
1774 default:
1775 tcg_gen_subi_tl(cpu_tmp0, count, 1);
1776 if (is_right) {
1777 tcg_gen_shr_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
1779 tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
1780 tcg_gen_shr_tl(cpu_T0, cpu_T0, count);
1781 tcg_gen_shl_tl(cpu_T1, cpu_T1, cpu_tmp4);
1782 } else {
1783 tcg_gen_shl_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
1784 if (ot == MO_16) {
1785 /* Only needed if count > 16, for Intel behaviour. */
1786 tcg_gen_subfi_tl(cpu_tmp4, 33, count);
1787 tcg_gen_shr_tl(cpu_tmp4, cpu_T1, cpu_tmp4);
1788 tcg_gen_or_tl(cpu_tmp0, cpu_tmp0, cpu_tmp4);
1791 tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
1792 tcg_gen_shl_tl(cpu_T0, cpu_T0, count);
1793 tcg_gen_shr_tl(cpu_T1, cpu_T1, cpu_tmp4);
1795 tcg_gen_movi_tl(cpu_tmp4, 0);
1796 tcg_gen_movcond_tl(TCG_COND_EQ, cpu_T1, count, cpu_tmp4,
1797 cpu_tmp4, cpu_T1);
1798 tcg_gen_or_tl(cpu_T0, cpu_T0, cpu_T1);
1799 break;
1802 /* store */
1803 gen_op_st_rm_T0_A0(s, ot, op1);
1805 gen_shift_flags(s, ot, cpu_T0, cpu_tmp0, count, is_right);
1806 tcg_temp_free(count);
1809 static void gen_shift(DisasContext *s1, int op, TCGMemOp ot, int d, int s)
1811 if (s != OR_TMP1)
1812 gen_op_mov_v_reg(ot, cpu_T1, s);
1813 switch(op) {
1814 case OP_ROL:
1815 gen_rot_rm_T1(s1, ot, d, 0);
1816 break;
1817 case OP_ROR:
1818 gen_rot_rm_T1(s1, ot, d, 1);
1819 break;
1820 case OP_SHL:
1821 case OP_SHL1:
1822 gen_shift_rm_T1(s1, ot, d, 0, 0);
1823 break;
1824 case OP_SHR:
1825 gen_shift_rm_T1(s1, ot, d, 1, 0);
1826 break;
1827 case OP_SAR:
1828 gen_shift_rm_T1(s1, ot, d, 1, 1);
1829 break;
1830 case OP_RCL:
1831 gen_rotc_rm_T1(s1, ot, d, 0);
1832 break;
1833 case OP_RCR:
1834 gen_rotc_rm_T1(s1, ot, d, 1);
1835 break;
1839 static void gen_shifti(DisasContext *s1, int op, TCGMemOp ot, int d, int c)
1841 switch(op) {
1842 case OP_ROL:
1843 gen_rot_rm_im(s1, ot, d, c, 0);
1844 break;
1845 case OP_ROR:
1846 gen_rot_rm_im(s1, ot, d, c, 1);
1847 break;
1848 case OP_SHL:
1849 case OP_SHL1:
1850 gen_shift_rm_im(s1, ot, d, c, 0, 0);
1851 break;
1852 case OP_SHR:
1853 gen_shift_rm_im(s1, ot, d, c, 1, 0);
1854 break;
1855 case OP_SAR:
1856 gen_shift_rm_im(s1, ot, d, c, 1, 1);
1857 break;
1858 default:
1859 /* currently not optimized */
1860 tcg_gen_movi_tl(cpu_T1, c);
1861 gen_shift(s1, op, ot, d, OR_TMP1);
1862 break;
1866 #define X86_MAX_INSN_LENGTH 15
1868 static uint64_t advance_pc(CPUX86State *env, DisasContext *s, int num_bytes)
1870 uint64_t pc = s->pc;
1872 s->pc += num_bytes;
1873 if (unlikely(s->pc - s->pc_start > X86_MAX_INSN_LENGTH)) {
1874 /* If the instruction's 16th byte is on a different page than the 1st, a
1875 * page fault on the second page wins over the general protection fault
1876 * caused by the instruction being too long.
1877 * This can happen even if the operand is only one byte long!
1879 if (((s->pc - 1) ^ (pc - 1)) & TARGET_PAGE_MASK) {
1880 volatile uint8_t unused =
1881 cpu_ldub_code(env, (s->pc - 1) & TARGET_PAGE_MASK);
1882 (void) unused;
1884 siglongjmp(s->jmpbuf, 1);
1887 return pc;
1890 static inline uint8_t x86_ldub_code(CPUX86State *env, DisasContext *s)
1892 return cpu_ldub_code(env, advance_pc(env, s, 1));
1895 static inline int16_t x86_ldsw_code(CPUX86State *env, DisasContext *s)
1897 return cpu_ldsw_code(env, advance_pc(env, s, 2));
1900 static inline uint16_t x86_lduw_code(CPUX86State *env, DisasContext *s)
1902 return cpu_lduw_code(env, advance_pc(env, s, 2));
1905 static inline uint32_t x86_ldl_code(CPUX86State *env, DisasContext *s)
1907 return cpu_ldl_code(env, advance_pc(env, s, 4));
1910 #ifdef TARGET_X86_64
1911 static inline uint64_t x86_ldq_code(CPUX86State *env, DisasContext *s)
1913 return cpu_ldq_code(env, advance_pc(env, s, 8));
1915 #endif
1917 /* Decompose an address. */
1919 typedef struct AddressParts {
1920 int def_seg;
1921 int base;
1922 int index;
1923 int scale;
1924 target_long disp;
1925 } AddressParts;
1927 static AddressParts gen_lea_modrm_0(CPUX86State *env, DisasContext *s,
1928 int modrm)
1930 int def_seg, base, index, scale, mod, rm;
1931 target_long disp;
1932 bool havesib;
1934 def_seg = R_DS;
1935 index = -1;
1936 scale = 0;
1937 disp = 0;
1939 mod = (modrm >> 6) & 3;
1940 rm = modrm & 7;
1941 base = rm | REX_B(s);
1943 if (mod == 3) {
1944 /* Normally filtered out earlier, but including this path
1945 simplifies multi-byte nop, as well as bndcl, bndcu, bndcn. */
1946 goto done;
1949 switch (s->aflag) {
1950 case MO_64:
1951 case MO_32:
1952 havesib = 0;
1953 if (rm == 4) {
1954 int code = x86_ldub_code(env, s);
1955 scale = (code >> 6) & 3;
1956 index = ((code >> 3) & 7) | REX_X(s);
1957 if (index == 4) {
1958 index = -1; /* no index */
1960 base = (code & 7) | REX_B(s);
1961 havesib = 1;
1964 switch (mod) {
1965 case 0:
1966 if ((base & 7) == 5) {
1967 base = -1;
1968 disp = (int32_t)x86_ldl_code(env, s);
1969 if (CODE64(s) && !havesib) {
1970 base = -2;
1971 disp += s->pc + s->rip_offset;
1974 break;
1975 case 1:
1976 disp = (int8_t)x86_ldub_code(env, s);
1977 break;
1978 default:
1979 case 2:
1980 disp = (int32_t)x86_ldl_code(env, s);
1981 break;
1984 /* For correct popl handling with esp. */
1985 if (base == R_ESP && s->popl_esp_hack) {
1986 disp += s->popl_esp_hack;
1988 if (base == R_EBP || base == R_ESP) {
1989 def_seg = R_SS;
1991 break;
1993 case MO_16:
1994 if (mod == 0) {
1995 if (rm == 6) {
1996 base = -1;
1997 disp = x86_lduw_code(env, s);
1998 break;
2000 } else if (mod == 1) {
2001 disp = (int8_t)x86_ldub_code(env, s);
2002 } else {
2003 disp = (int16_t)x86_lduw_code(env, s);
2006 switch (rm) {
2007 case 0:
2008 base = R_EBX;
2009 index = R_ESI;
2010 break;
2011 case 1:
2012 base = R_EBX;
2013 index = R_EDI;
2014 break;
2015 case 2:
2016 base = R_EBP;
2017 index = R_ESI;
2018 def_seg = R_SS;
2019 break;
2020 case 3:
2021 base = R_EBP;
2022 index = R_EDI;
2023 def_seg = R_SS;
2024 break;
2025 case 4:
2026 base = R_ESI;
2027 break;
2028 case 5:
2029 base = R_EDI;
2030 break;
2031 case 6:
2032 base = R_EBP;
2033 def_seg = R_SS;
2034 break;
2035 default:
2036 case 7:
2037 base = R_EBX;
2038 break;
2040 break;
2042 default:
2043 tcg_abort();
2046 done:
2047 return (AddressParts){ def_seg, base, index, scale, disp };
2050 /* Compute the address, with a minimum number of TCG ops. */
2051 static TCGv gen_lea_modrm_1(AddressParts a)
2053 TCGv ea = NULL;
2055 if (a.index >= 0) {
2056 if (a.scale == 0) {
2057 ea = cpu_regs[a.index];
2058 } else {
2059 tcg_gen_shli_tl(cpu_A0, cpu_regs[a.index], a.scale);
2060 ea = cpu_A0;
2062 if (a.base >= 0) {
2063 tcg_gen_add_tl(cpu_A0, ea, cpu_regs[a.base]);
2064 ea = cpu_A0;
2066 } else if (a.base >= 0) {
2067 ea = cpu_regs[a.base];
2069 if (!ea) {
2070 tcg_gen_movi_tl(cpu_A0, a.disp);
2071 ea = cpu_A0;
2072 } else if (a.disp != 0) {
2073 tcg_gen_addi_tl(cpu_A0, ea, a.disp);
2074 ea = cpu_A0;
2077 return ea;
2080 static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm)
2082 AddressParts a = gen_lea_modrm_0(env, s, modrm);
2083 TCGv ea = gen_lea_modrm_1(a);
2084 gen_lea_v_seg(s, s->aflag, ea, a.def_seg, s->override);
2087 static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
2089 (void)gen_lea_modrm_0(env, s, modrm);
2092 /* Used for BNDCL, BNDCU, BNDCN. */
2093 static void gen_bndck(CPUX86State *env, DisasContext *s, int modrm,
2094 TCGCond cond, TCGv_i64 bndv)
2096 TCGv ea = gen_lea_modrm_1(gen_lea_modrm_0(env, s, modrm));
2098 tcg_gen_extu_tl_i64(cpu_tmp1_i64, ea);
2099 if (!CODE64(s)) {
2100 tcg_gen_ext32u_i64(cpu_tmp1_i64, cpu_tmp1_i64);
2102 tcg_gen_setcond_i64(cond, cpu_tmp1_i64, cpu_tmp1_i64, bndv);
2103 tcg_gen_extrl_i64_i32(cpu_tmp2_i32, cpu_tmp1_i64);
2104 gen_helper_bndck(cpu_env, cpu_tmp2_i32);
2107 /* used for LEA and MOV AX, mem */
2108 static void gen_add_A0_ds_seg(DisasContext *s)
2110 gen_lea_v_seg(s, s->aflag, cpu_A0, R_DS, s->override);
2113 /* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
2114 OR_TMP0 */
2115 static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
2116 TCGMemOp ot, int reg, int is_store)
2118 int mod, rm;
2120 mod = (modrm >> 6) & 3;
2121 rm = (modrm & 7) | REX_B(s);
2122 if (mod == 3) {
2123 if (is_store) {
2124 if (reg != OR_TMP0)
2125 gen_op_mov_v_reg(ot, cpu_T0, reg);
2126 gen_op_mov_reg_v(ot, rm, cpu_T0);
2127 } else {
2128 gen_op_mov_v_reg(ot, cpu_T0, rm);
2129 if (reg != OR_TMP0)
2130 gen_op_mov_reg_v(ot, reg, cpu_T0);
2132 } else {
2133 gen_lea_modrm(env, s, modrm);
2134 if (is_store) {
2135 if (reg != OR_TMP0)
2136 gen_op_mov_v_reg(ot, cpu_T0, reg);
2137 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
2138 } else {
2139 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
2140 if (reg != OR_TMP0)
2141 gen_op_mov_reg_v(ot, reg, cpu_T0);
2146 static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, TCGMemOp ot)
2148 uint32_t ret;
2150 switch (ot) {
2151 case MO_8:
2152 ret = x86_ldub_code(env, s);
2153 break;
2154 case MO_16:
2155 ret = x86_lduw_code(env, s);
2156 break;
2157 case MO_32:
2158 #ifdef TARGET_X86_64
2159 case MO_64:
2160 #endif
2161 ret = x86_ldl_code(env, s);
2162 break;
2163 default:
2164 tcg_abort();
2166 return ret;
2169 static inline int insn_const_size(TCGMemOp ot)
2171 if (ot <= MO_32) {
2172 return 1 << ot;
2173 } else {
2174 return 4;
2178 static inline bool use_goto_tb(DisasContext *s, target_ulong pc)
2180 #ifndef CONFIG_USER_ONLY
2181 return (pc & TARGET_PAGE_MASK) == (s->base.tb->pc & TARGET_PAGE_MASK) ||
2182 (pc & TARGET_PAGE_MASK) == (s->pc_start & TARGET_PAGE_MASK);
2183 #else
2184 return true;
2185 #endif
2188 static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip)
2190 target_ulong pc = s->cs_base + eip;
2192 if (use_goto_tb(s, pc)) {
2193 /* jump to same page: we can use a direct jump */
2194 tcg_gen_goto_tb(tb_num);
2195 gen_jmp_im(eip);
2196 tcg_gen_exit_tb(s->base.tb, tb_num);
2197 s->base.is_jmp = DISAS_NORETURN;
2198 } else {
2199 /* jump to another page */
2200 gen_jmp_im(eip);
2201 gen_jr(s, cpu_tmp0);
2205 static inline void gen_jcc(DisasContext *s, int b,
2206 target_ulong val, target_ulong next_eip)
2208 TCGLabel *l1, *l2;
2210 if (s->jmp_opt) {
2211 l1 = gen_new_label();
2212 gen_jcc1(s, b, l1);
2214 gen_goto_tb(s, 0, next_eip);
2216 gen_set_label(l1);
2217 gen_goto_tb(s, 1, val);
2218 } else {
2219 l1 = gen_new_label();
2220 l2 = gen_new_label();
2221 gen_jcc1(s, b, l1);
2223 gen_jmp_im(next_eip);
2224 tcg_gen_br(l2);
2226 gen_set_label(l1);
2227 gen_jmp_im(val);
2228 gen_set_label(l2);
2229 gen_eob(s);
2233 static void gen_cmovcc1(CPUX86State *env, DisasContext *s, TCGMemOp ot, int b,
2234 int modrm, int reg)
2236 CCPrepare cc;
2238 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2240 cc = gen_prepare_cc(s, b, cpu_T1);
2241 if (cc.mask != -1) {
2242 TCGv t0 = tcg_temp_new();
2243 tcg_gen_andi_tl(t0, cc.reg, cc.mask);
2244 cc.reg = t0;
2246 if (!cc.use_reg2) {
2247 cc.reg2 = tcg_const_tl(cc.imm);
2250 tcg_gen_movcond_tl(cc.cond, cpu_T0, cc.reg, cc.reg2,
2251 cpu_T0, cpu_regs[reg]);
2252 gen_op_mov_reg_v(ot, reg, cpu_T0);
2254 if (cc.mask != -1) {
2255 tcg_temp_free(cc.reg);
2257 if (!cc.use_reg2) {
2258 tcg_temp_free(cc.reg2);
2262 static inline void gen_op_movl_T0_seg(int seg_reg)
2264 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
2265 offsetof(CPUX86State,segs[seg_reg].selector));
2268 static inline void gen_op_movl_seg_T0_vm(int seg_reg)
2270 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
2271 tcg_gen_st32_tl(cpu_T0, cpu_env,
2272 offsetof(CPUX86State,segs[seg_reg].selector));
2273 tcg_gen_shli_tl(cpu_seg_base[seg_reg], cpu_T0, 4);
2276 /* move T0 to seg_reg and compute if the CPU state may change. Never
2277 call this function with seg_reg == R_CS */
2278 static void gen_movl_seg_T0(DisasContext *s, int seg_reg)
2280 if (s->pe && !s->vm86) {
2281 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
2282 gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
2283 /* abort translation because the addseg value may change or
2284 because ss32 may change. For R_SS, translation must always
2285 stop as a special handling must be done to disable hardware
2286 interrupts for the next instruction */
2287 if (seg_reg == R_SS || (s->code32 && seg_reg < R_FS)) {
2288 s->base.is_jmp = DISAS_TOO_MANY;
2290 } else {
2291 gen_op_movl_seg_T0_vm(seg_reg);
2292 if (seg_reg == R_SS) {
2293 s->base.is_jmp = DISAS_TOO_MANY;
2298 static inline int svm_is_rep(int prefixes)
2300 return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
2303 static inline void
2304 gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
2305 uint32_t type, uint64_t param)
2307 /* no SVM activated; fast case */
2308 if (likely(!(s->flags & HF_SVMI_MASK)))
2309 return;
2310 gen_update_cc_op(s);
2311 gen_jmp_im(pc_start - s->cs_base);
2312 gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
2313 tcg_const_i64(param));
2316 static inline void
2317 gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
2319 gen_svm_check_intercept_param(s, pc_start, type, 0);
2322 static inline void gen_stack_update(DisasContext *s, int addend)
2324 gen_op_add_reg_im(mo_stacksize(s), R_ESP, addend);
2327 /* Generate a push. It depends on ss32, addseg and dflag. */
2328 static void gen_push_v(DisasContext *s, TCGv val)
2330 TCGMemOp d_ot = mo_pushpop(s, s->dflag);
2331 TCGMemOp a_ot = mo_stacksize(s);
2332 int size = 1 << d_ot;
2333 TCGv new_esp = cpu_A0;
2335 tcg_gen_subi_tl(cpu_A0, cpu_regs[R_ESP], size);
2337 if (!CODE64(s)) {
2338 if (s->addseg) {
2339 new_esp = cpu_tmp4;
2340 tcg_gen_mov_tl(new_esp, cpu_A0);
2342 gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
2345 gen_op_st_v(s, d_ot, val, cpu_A0);
2346 gen_op_mov_reg_v(a_ot, R_ESP, new_esp);
2349 /* two step pop is necessary for precise exceptions */
2350 static TCGMemOp gen_pop_T0(DisasContext *s)
2352 TCGMemOp d_ot = mo_pushpop(s, s->dflag);
2354 gen_lea_v_seg(s, mo_stacksize(s), cpu_regs[R_ESP], R_SS, -1);
2355 gen_op_ld_v(s, d_ot, cpu_T0, cpu_A0);
2357 return d_ot;
2360 static inline void gen_pop_update(DisasContext *s, TCGMemOp ot)
2362 gen_stack_update(s, 1 << ot);
2365 static inline void gen_stack_A0(DisasContext *s)
2367 gen_lea_v_seg(s, s->ss32 ? MO_32 : MO_16, cpu_regs[R_ESP], R_SS, -1);
2370 static void gen_pusha(DisasContext *s)
2372 TCGMemOp s_ot = s->ss32 ? MO_32 : MO_16;
2373 TCGMemOp d_ot = s->dflag;
2374 int size = 1 << d_ot;
2375 int i;
2377 for (i = 0; i < 8; i++) {
2378 tcg_gen_addi_tl(cpu_A0, cpu_regs[R_ESP], (i - 8) * size);
2379 gen_lea_v_seg(s, s_ot, cpu_A0, R_SS, -1);
2380 gen_op_st_v(s, d_ot, cpu_regs[7 - i], cpu_A0);
2383 gen_stack_update(s, -8 * size);
2386 static void gen_popa(DisasContext *s)
2388 TCGMemOp s_ot = s->ss32 ? MO_32 : MO_16;
2389 TCGMemOp d_ot = s->dflag;
2390 int size = 1 << d_ot;
2391 int i;
2393 for (i = 0; i < 8; i++) {
2394 /* ESP is not reloaded */
2395 if (7 - i == R_ESP) {
2396 continue;
2398 tcg_gen_addi_tl(cpu_A0, cpu_regs[R_ESP], i * size);
2399 gen_lea_v_seg(s, s_ot, cpu_A0, R_SS, -1);
2400 gen_op_ld_v(s, d_ot, cpu_T0, cpu_A0);
2401 gen_op_mov_reg_v(d_ot, 7 - i, cpu_T0);
2404 gen_stack_update(s, 8 * size);
2407 static void gen_enter(DisasContext *s, int esp_addend, int level)
2409 TCGMemOp d_ot = mo_pushpop(s, s->dflag);
2410 TCGMemOp a_ot = CODE64(s) ? MO_64 : s->ss32 ? MO_32 : MO_16;
2411 int size = 1 << d_ot;
2413 /* Push BP; compute FrameTemp into T1. */
2414 tcg_gen_subi_tl(cpu_T1, cpu_regs[R_ESP], size);
2415 gen_lea_v_seg(s, a_ot, cpu_T1, R_SS, -1);
2416 gen_op_st_v(s, d_ot, cpu_regs[R_EBP], cpu_A0);
2418 level &= 31;
2419 if (level != 0) {
2420 int i;
2422 /* Copy level-1 pointers from the previous frame. */
2423 for (i = 1; i < level; ++i) {
2424 tcg_gen_subi_tl(cpu_A0, cpu_regs[R_EBP], size * i);
2425 gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
2426 gen_op_ld_v(s, d_ot, cpu_tmp0, cpu_A0);
2428 tcg_gen_subi_tl(cpu_A0, cpu_T1, size * i);
2429 gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
2430 gen_op_st_v(s, d_ot, cpu_tmp0, cpu_A0);
2433 /* Push the current FrameTemp as the last level. */
2434 tcg_gen_subi_tl(cpu_A0, cpu_T1, size * level);
2435 gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
2436 gen_op_st_v(s, d_ot, cpu_T1, cpu_A0);
2439 /* Copy the FrameTemp value to EBP. */
2440 gen_op_mov_reg_v(a_ot, R_EBP, cpu_T1);
2442 /* Compute the final value of ESP. */
2443 tcg_gen_subi_tl(cpu_T1, cpu_T1, esp_addend + size * level);
2444 gen_op_mov_reg_v(a_ot, R_ESP, cpu_T1);
2447 static void gen_leave(DisasContext *s)
2449 TCGMemOp d_ot = mo_pushpop(s, s->dflag);
2450 TCGMemOp a_ot = mo_stacksize(s);
2452 gen_lea_v_seg(s, a_ot, cpu_regs[R_EBP], R_SS, -1);
2453 gen_op_ld_v(s, d_ot, cpu_T0, cpu_A0);
2455 tcg_gen_addi_tl(cpu_T1, cpu_regs[R_EBP], 1 << d_ot);
2457 gen_op_mov_reg_v(d_ot, R_EBP, cpu_T0);
2458 gen_op_mov_reg_v(a_ot, R_ESP, cpu_T1);
2461 static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
2463 gen_update_cc_op(s);
2464 gen_jmp_im(cur_eip);
2465 gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
2466 s->base.is_jmp = DISAS_NORETURN;
2469 /* Generate #UD for the current instruction. The assumption here is that
2470 the instruction is known, but it isn't allowed in the current cpu mode. */
2471 static void gen_illegal_opcode(DisasContext *s)
2473 gen_exception(s, EXCP06_ILLOP, s->pc_start - s->cs_base);
2476 /* Similarly, except that the assumption here is that we don't decode
2477 the instruction at all -- either a missing opcode, an unimplemented
2478 feature, or just a bogus instruction stream. */
2479 static void gen_unknown_opcode(CPUX86State *env, DisasContext *s)
2481 gen_illegal_opcode(s);
2483 if (qemu_loglevel_mask(LOG_UNIMP)) {
2484 target_ulong pc = s->pc_start, end = s->pc;
2485 qemu_log_lock();
2486 qemu_log("ILLOPC: " TARGET_FMT_lx ":", pc);
2487 for (; pc < end; ++pc) {
2488 qemu_log(" %02x", cpu_ldub_code(env, pc));
2490 qemu_log("\n");
2491 qemu_log_unlock();
2495 /* an interrupt is different from an exception because of the
2496 privilege checks */
2497 static void gen_interrupt(DisasContext *s, int intno,
2498 target_ulong cur_eip, target_ulong next_eip)
2500 gen_update_cc_op(s);
2501 gen_jmp_im(cur_eip);
2502 gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
2503 tcg_const_i32(next_eip - cur_eip));
2504 s->base.is_jmp = DISAS_NORETURN;
2507 static void gen_debug(DisasContext *s, target_ulong cur_eip)
2509 gen_update_cc_op(s);
2510 gen_jmp_im(cur_eip);
2511 gen_helper_debug(cpu_env);
2512 s->base.is_jmp = DISAS_NORETURN;
2515 static void gen_set_hflag(DisasContext *s, uint32_t mask)
2517 if ((s->flags & mask) == 0) {
2518 TCGv_i32 t = tcg_temp_new_i32();
2519 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUX86State, hflags));
2520 tcg_gen_ori_i32(t, t, mask);
2521 tcg_gen_st_i32(t, cpu_env, offsetof(CPUX86State, hflags));
2522 tcg_temp_free_i32(t);
2523 s->flags |= mask;
2527 static void gen_reset_hflag(DisasContext *s, uint32_t mask)
2529 if (s->flags & mask) {
2530 TCGv_i32 t = tcg_temp_new_i32();
2531 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUX86State, hflags));
2532 tcg_gen_andi_i32(t, t, ~mask);
2533 tcg_gen_st_i32(t, cpu_env, offsetof(CPUX86State, hflags));
2534 tcg_temp_free_i32(t);
2535 s->flags &= ~mask;
2539 /* Clear BND registers during legacy branches. */
2540 static void gen_bnd_jmp(DisasContext *s)
2542 /* Clear the registers only if BND prefix is missing, MPX is enabled,
2543 and if the BNDREGs are known to be in use (non-zero) already.
2544 The helper itself will check BNDPRESERVE at runtime. */
2545 if ((s->prefix & PREFIX_REPNZ) == 0
2546 && (s->flags & HF_MPX_EN_MASK) != 0
2547 && (s->flags & HF_MPX_IU_MASK) != 0) {
2548 gen_helper_bnd_jmp(cpu_env);
2552 /* Generate an end of block. Trace exception is also generated if needed.
2553 If INHIBIT, set HF_INHIBIT_IRQ_MASK if it isn't already set.
2554 If RECHECK_TF, emit a rechecking helper for #DB, ignoring the state of
2555 S->TF. This is used by the syscall/sysret insns. */
2556 static void
2557 do_gen_eob_worker(DisasContext *s, bool inhibit, bool recheck_tf, bool jr)
2559 gen_update_cc_op(s);
2561 /* If several instructions disable interrupts, only the first does it. */
2562 if (inhibit && !(s->flags & HF_INHIBIT_IRQ_MASK)) {
2563 gen_set_hflag(s, HF_INHIBIT_IRQ_MASK);
2564 } else {
2565 gen_reset_hflag(s, HF_INHIBIT_IRQ_MASK);
2568 if (s->base.tb->flags & HF_RF_MASK) {
2569 gen_helper_reset_rf(cpu_env);
2571 if (s->base.singlestep_enabled) {
2572 gen_helper_debug(cpu_env);
2573 } else if (recheck_tf) {
2574 gen_helper_rechecking_single_step(cpu_env);
2575 tcg_gen_exit_tb(NULL, 0);
2576 } else if (s->tf) {
2577 gen_helper_single_step(cpu_env);
2578 } else if (jr) {
2579 tcg_gen_lookup_and_goto_ptr();
2580 } else {
2581 tcg_gen_exit_tb(NULL, 0);
2583 s->base.is_jmp = DISAS_NORETURN;
2586 static inline void
2587 gen_eob_worker(DisasContext *s, bool inhibit, bool recheck_tf)
2589 do_gen_eob_worker(s, inhibit, recheck_tf, false);
2592 /* End of block.
2593 If INHIBIT, set HF_INHIBIT_IRQ_MASK if it isn't already set. */
2594 static void gen_eob_inhibit_irq(DisasContext *s, bool inhibit)
2596 gen_eob_worker(s, inhibit, false);
2599 /* End of block, resetting the inhibit irq flag. */
2600 static void gen_eob(DisasContext *s)
2602 gen_eob_worker(s, false, false);
2605 /* Jump to register */
2606 static void gen_jr(DisasContext *s, TCGv dest)
2608 do_gen_eob_worker(s, false, false, true);
2611 /* generate a jump to eip. No segment change must happen before as a
2612 direct call to the next block may occur */
2613 static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
2615 gen_update_cc_op(s);
2616 set_cc_op(s, CC_OP_DYNAMIC);
2617 if (s->jmp_opt) {
2618 gen_goto_tb(s, tb_num, eip);
2619 } else {
2620 gen_jmp_im(eip);
2621 gen_eob(s);
2625 static void gen_jmp(DisasContext *s, target_ulong eip)
2627 gen_jmp_tb(s, eip, 0);
2630 static inline void gen_ldq_env_A0(DisasContext *s, int offset)
2632 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
2633 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
2636 static inline void gen_stq_env_A0(DisasContext *s, int offset)
2638 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
2639 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
2642 static inline void gen_ldo_env_A0(DisasContext *s, int offset)
2644 int mem_index = s->mem_index;
2645 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
2646 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(0)));
2647 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2648 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
2649 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(1)));
2652 static inline void gen_sto_env_A0(DisasContext *s, int offset)
2654 int mem_index = s->mem_index;
2655 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(0)));
2656 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
2657 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
2658 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(1)));
2659 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
2662 static inline void gen_op_movo(int d_offset, int s_offset)
2664 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset + offsetof(ZMMReg, ZMM_Q(0)));
2665 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset + offsetof(ZMMReg, ZMM_Q(0)));
2666 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset + offsetof(ZMMReg, ZMM_Q(1)));
2667 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset + offsetof(ZMMReg, ZMM_Q(1)));
2670 static inline void gen_op_movq(int d_offset, int s_offset)
2672 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
2673 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
2676 static inline void gen_op_movl(int d_offset, int s_offset)
2678 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
2679 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
2682 static inline void gen_op_movq_env_0(int d_offset)
2684 tcg_gen_movi_i64(cpu_tmp1_i64, 0);
2685 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
2688 typedef void (*SSEFunc_i_ep)(TCGv_i32 val, TCGv_ptr env, TCGv_ptr reg);
2689 typedef void (*SSEFunc_l_ep)(TCGv_i64 val, TCGv_ptr env, TCGv_ptr reg);
2690 typedef void (*SSEFunc_0_epi)(TCGv_ptr env, TCGv_ptr reg, TCGv_i32 val);
2691 typedef void (*SSEFunc_0_epl)(TCGv_ptr env, TCGv_ptr reg, TCGv_i64 val);
2692 typedef void (*SSEFunc_0_epp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b);
2693 typedef void (*SSEFunc_0_eppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2694 TCGv_i32 val);
2695 typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
2696 typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2697 TCGv val);
2699 #define SSE_SPECIAL ((void *)1)
2700 #define SSE_DUMMY ((void *)2)
2702 #define MMX_OP2(x) { gen_helper_ ## x ## _mmx, gen_helper_ ## x ## _xmm }
2703 #define SSE_FOP(x) { gen_helper_ ## x ## ps, gen_helper_ ## x ## pd, \
2704 gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, }
2706 static const SSEFunc_0_epp sse_op_table1[256][4] = {
2707 /* 3DNow! extensions */
2708 [0x0e] = { SSE_DUMMY }, /* femms */
2709 [0x0f] = { SSE_DUMMY }, /* pf... */
2710 /* pure SSE operations */
2711 [0x10] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
2712 [0x11] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
2713 [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
2714 [0x13] = { SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd */
2715 [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
2716 [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
2717 [0x16] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd, movshdup */
2718 [0x17] = { SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd */
2720 [0x28] = { SSE_SPECIAL, SSE_SPECIAL }, /* movaps, movapd */
2721 [0x29] = { SSE_SPECIAL, SSE_SPECIAL }, /* movaps, movapd */
2722 [0x2a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */
2723 [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
2724 [0x2c] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */
2725 [0x2d] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */
2726 [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
2727 [0x2f] = { gen_helper_comiss, gen_helper_comisd },
2728 [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
2729 [0x51] = SSE_FOP(sqrt),
2730 [0x52] = { gen_helper_rsqrtps, NULL, gen_helper_rsqrtss, NULL },
2731 [0x53] = { gen_helper_rcpps, NULL, gen_helper_rcpss, NULL },
2732 [0x54] = { gen_helper_pand_xmm, gen_helper_pand_xmm }, /* andps, andpd */
2733 [0x55] = { gen_helper_pandn_xmm, gen_helper_pandn_xmm }, /* andnps, andnpd */
2734 [0x56] = { gen_helper_por_xmm, gen_helper_por_xmm }, /* orps, orpd */
2735 [0x57] = { gen_helper_pxor_xmm, gen_helper_pxor_xmm }, /* xorps, xorpd */
2736 [0x58] = SSE_FOP(add),
2737 [0x59] = SSE_FOP(mul),
2738 [0x5a] = { gen_helper_cvtps2pd, gen_helper_cvtpd2ps,
2739 gen_helper_cvtss2sd, gen_helper_cvtsd2ss },
2740 [0x5b] = { gen_helper_cvtdq2ps, gen_helper_cvtps2dq, gen_helper_cvttps2dq },
2741 [0x5c] = SSE_FOP(sub),
2742 [0x5d] = SSE_FOP(min),
2743 [0x5e] = SSE_FOP(div),
2744 [0x5f] = SSE_FOP(max),
2746 [0xc2] = SSE_FOP(cmpeq),
2747 [0xc6] = { (SSEFunc_0_epp)gen_helper_shufps,
2748 (SSEFunc_0_epp)gen_helper_shufpd }, /* XXX: casts */
2750 /* SSSE3, SSE4, MOVBE, CRC32, BMI1, BMI2, ADX. */
2751 [0x38] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
2752 [0x3a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
2754 /* MMX ops and their SSE extensions */
2755 [0x60] = MMX_OP2(punpcklbw),
2756 [0x61] = MMX_OP2(punpcklwd),
2757 [0x62] = MMX_OP2(punpckldq),
2758 [0x63] = MMX_OP2(packsswb),
2759 [0x64] = MMX_OP2(pcmpgtb),
2760 [0x65] = MMX_OP2(pcmpgtw),
2761 [0x66] = MMX_OP2(pcmpgtl),
2762 [0x67] = MMX_OP2(packuswb),
2763 [0x68] = MMX_OP2(punpckhbw),
2764 [0x69] = MMX_OP2(punpckhwd),
2765 [0x6a] = MMX_OP2(punpckhdq),
2766 [0x6b] = MMX_OP2(packssdw),
2767 [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
2768 [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
2769 [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
2770 [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
2771 [0x70] = { (SSEFunc_0_epp)gen_helper_pshufw_mmx,
2772 (SSEFunc_0_epp)gen_helper_pshufd_xmm,
2773 (SSEFunc_0_epp)gen_helper_pshufhw_xmm,
2774 (SSEFunc_0_epp)gen_helper_pshuflw_xmm }, /* XXX: casts */
2775 [0x71] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftw */
2776 [0x72] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftd */
2777 [0x73] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftq */
2778 [0x74] = MMX_OP2(pcmpeqb),
2779 [0x75] = MMX_OP2(pcmpeqw),
2780 [0x76] = MMX_OP2(pcmpeql),
2781 [0x77] = { SSE_DUMMY }, /* emms */
2782 [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
2783 [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
2784 [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
2785 [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
2786 [0x7e] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movd, movd, , movq */
2787 [0x7f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, movdqu */
2788 [0xc4] = { SSE_SPECIAL, SSE_SPECIAL }, /* pinsrw */
2789 [0xc5] = { SSE_SPECIAL, SSE_SPECIAL }, /* pextrw */
2790 [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
2791 [0xd1] = MMX_OP2(psrlw),
2792 [0xd2] = MMX_OP2(psrld),
2793 [0xd3] = MMX_OP2(psrlq),
2794 [0xd4] = MMX_OP2(paddq),
2795 [0xd5] = MMX_OP2(pmullw),
2796 [0xd6] = { NULL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
2797 [0xd7] = { SSE_SPECIAL, SSE_SPECIAL }, /* pmovmskb */
2798 [0xd8] = MMX_OP2(psubusb),
2799 [0xd9] = MMX_OP2(psubusw),
2800 [0xda] = MMX_OP2(pminub),
2801 [0xdb] = MMX_OP2(pand),
2802 [0xdc] = MMX_OP2(paddusb),
2803 [0xdd] = MMX_OP2(paddusw),
2804 [0xde] = MMX_OP2(pmaxub),
2805 [0xdf] = MMX_OP2(pandn),
2806 [0xe0] = MMX_OP2(pavgb),
2807 [0xe1] = MMX_OP2(psraw),
2808 [0xe2] = MMX_OP2(psrad),
2809 [0xe3] = MMX_OP2(pavgw),
2810 [0xe4] = MMX_OP2(pmulhuw),
2811 [0xe5] = MMX_OP2(pmulhw),
2812 [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
2813 [0xe7] = { SSE_SPECIAL , SSE_SPECIAL }, /* movntq, movntq */
2814 [0xe8] = MMX_OP2(psubsb),
2815 [0xe9] = MMX_OP2(psubsw),
2816 [0xea] = MMX_OP2(pminsw),
2817 [0xeb] = MMX_OP2(por),
2818 [0xec] = MMX_OP2(paddsb),
2819 [0xed] = MMX_OP2(paddsw),
2820 [0xee] = MMX_OP2(pmaxsw),
2821 [0xef] = MMX_OP2(pxor),
2822 [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
2823 [0xf1] = MMX_OP2(psllw),
2824 [0xf2] = MMX_OP2(pslld),
2825 [0xf3] = MMX_OP2(psllq),
2826 [0xf4] = MMX_OP2(pmuludq),
2827 [0xf5] = MMX_OP2(pmaddwd),
2828 [0xf6] = MMX_OP2(psadbw),
2829 [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
2830 (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
2831 [0xf8] = MMX_OP2(psubb),
2832 [0xf9] = MMX_OP2(psubw),
2833 [0xfa] = MMX_OP2(psubl),
2834 [0xfb] = MMX_OP2(psubq),
2835 [0xfc] = MMX_OP2(paddb),
2836 [0xfd] = MMX_OP2(paddw),
2837 [0xfe] = MMX_OP2(paddl),
2840 static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
2841 [0 + 2] = MMX_OP2(psrlw),
2842 [0 + 4] = MMX_OP2(psraw),
2843 [0 + 6] = MMX_OP2(psllw),
2844 [8 + 2] = MMX_OP2(psrld),
2845 [8 + 4] = MMX_OP2(psrad),
2846 [8 + 6] = MMX_OP2(pslld),
2847 [16 + 2] = MMX_OP2(psrlq),
2848 [16 + 3] = { NULL, gen_helper_psrldq_xmm },
2849 [16 + 6] = MMX_OP2(psllq),
2850 [16 + 7] = { NULL, gen_helper_pslldq_xmm },
2853 static const SSEFunc_0_epi sse_op_table3ai[] = {
2854 gen_helper_cvtsi2ss,
2855 gen_helper_cvtsi2sd
2858 #ifdef TARGET_X86_64
2859 static const SSEFunc_0_epl sse_op_table3aq[] = {
2860 gen_helper_cvtsq2ss,
2861 gen_helper_cvtsq2sd
2863 #endif
2865 static const SSEFunc_i_ep sse_op_table3bi[] = {
2866 gen_helper_cvttss2si,
2867 gen_helper_cvtss2si,
2868 gen_helper_cvttsd2si,
2869 gen_helper_cvtsd2si
2872 #ifdef TARGET_X86_64
2873 static const SSEFunc_l_ep sse_op_table3bq[] = {
2874 gen_helper_cvttss2sq,
2875 gen_helper_cvtss2sq,
2876 gen_helper_cvttsd2sq,
2877 gen_helper_cvtsd2sq
2879 #endif
2881 static const SSEFunc_0_epp sse_op_table4[8][4] = {
2882 SSE_FOP(cmpeq),
2883 SSE_FOP(cmplt),
2884 SSE_FOP(cmple),
2885 SSE_FOP(cmpunord),
2886 SSE_FOP(cmpneq),
2887 SSE_FOP(cmpnlt),
2888 SSE_FOP(cmpnle),
2889 SSE_FOP(cmpord),
2892 static const SSEFunc_0_epp sse_op_table5[256] = {
2893 [0x0c] = gen_helper_pi2fw,
2894 [0x0d] = gen_helper_pi2fd,
2895 [0x1c] = gen_helper_pf2iw,
2896 [0x1d] = gen_helper_pf2id,
2897 [0x8a] = gen_helper_pfnacc,
2898 [0x8e] = gen_helper_pfpnacc,
2899 [0x90] = gen_helper_pfcmpge,
2900 [0x94] = gen_helper_pfmin,
2901 [0x96] = gen_helper_pfrcp,
2902 [0x97] = gen_helper_pfrsqrt,
2903 [0x9a] = gen_helper_pfsub,
2904 [0x9e] = gen_helper_pfadd,
2905 [0xa0] = gen_helper_pfcmpgt,
2906 [0xa4] = gen_helper_pfmax,
2907 [0xa6] = gen_helper_movq, /* pfrcpit1; no need to actually increase precision */
2908 [0xa7] = gen_helper_movq, /* pfrsqit1 */
2909 [0xaa] = gen_helper_pfsubr,
2910 [0xae] = gen_helper_pfacc,
2911 [0xb0] = gen_helper_pfcmpeq,
2912 [0xb4] = gen_helper_pfmul,
2913 [0xb6] = gen_helper_movq, /* pfrcpit2 */
2914 [0xb7] = gen_helper_pmulhrw_mmx,
2915 [0xbb] = gen_helper_pswapd,
2916 [0xbf] = gen_helper_pavgb_mmx /* pavgusb */
2919 struct SSEOpHelper_epp {
2920 SSEFunc_0_epp op[2];
2921 uint32_t ext_mask;
2924 struct SSEOpHelper_eppi {
2925 SSEFunc_0_eppi op[2];
2926 uint32_t ext_mask;
2929 #define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
2930 #define SSE41_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }
2931 #define SSE42_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }
2932 #define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
2933 #define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
2934 CPUID_EXT_PCLMULQDQ }
2935 #define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
2937 static const struct SSEOpHelper_epp sse_op_table6[256] = {
2938 [0x00] = SSSE3_OP(pshufb),
2939 [0x01] = SSSE3_OP(phaddw),
2940 [0x02] = SSSE3_OP(phaddd),
2941 [0x03] = SSSE3_OP(phaddsw),
2942 [0x04] = SSSE3_OP(pmaddubsw),
2943 [0x05] = SSSE3_OP(phsubw),
2944 [0x06] = SSSE3_OP(phsubd),
2945 [0x07] = SSSE3_OP(phsubsw),
2946 [0x08] = SSSE3_OP(psignb),
2947 [0x09] = SSSE3_OP(psignw),
2948 [0x0a] = SSSE3_OP(psignd),
2949 [0x0b] = SSSE3_OP(pmulhrsw),
2950 [0x10] = SSE41_OP(pblendvb),
2951 [0x14] = SSE41_OP(blendvps),
2952 [0x15] = SSE41_OP(blendvpd),
2953 [0x17] = SSE41_OP(ptest),
2954 [0x1c] = SSSE3_OP(pabsb),
2955 [0x1d] = SSSE3_OP(pabsw),
2956 [0x1e] = SSSE3_OP(pabsd),
2957 [0x20] = SSE41_OP(pmovsxbw),
2958 [0x21] = SSE41_OP(pmovsxbd),
2959 [0x22] = SSE41_OP(pmovsxbq),
2960 [0x23] = SSE41_OP(pmovsxwd),
2961 [0x24] = SSE41_OP(pmovsxwq),
2962 [0x25] = SSE41_OP(pmovsxdq),
2963 [0x28] = SSE41_OP(pmuldq),
2964 [0x29] = SSE41_OP(pcmpeqq),
2965 [0x2a] = SSE41_SPECIAL, /* movntqda */
2966 [0x2b] = SSE41_OP(packusdw),
2967 [0x30] = SSE41_OP(pmovzxbw),
2968 [0x31] = SSE41_OP(pmovzxbd),
2969 [0x32] = SSE41_OP(pmovzxbq),
2970 [0x33] = SSE41_OP(pmovzxwd),
2971 [0x34] = SSE41_OP(pmovzxwq),
2972 [0x35] = SSE41_OP(pmovzxdq),
2973 [0x37] = SSE42_OP(pcmpgtq),
2974 [0x38] = SSE41_OP(pminsb),
2975 [0x39] = SSE41_OP(pminsd),
2976 [0x3a] = SSE41_OP(pminuw),
2977 [0x3b] = SSE41_OP(pminud),
2978 [0x3c] = SSE41_OP(pmaxsb),
2979 [0x3d] = SSE41_OP(pmaxsd),
2980 [0x3e] = SSE41_OP(pmaxuw),
2981 [0x3f] = SSE41_OP(pmaxud),
2982 [0x40] = SSE41_OP(pmulld),
2983 [0x41] = SSE41_OP(phminposuw),
2984 [0xdb] = AESNI_OP(aesimc),
2985 [0xdc] = AESNI_OP(aesenc),
2986 [0xdd] = AESNI_OP(aesenclast),
2987 [0xde] = AESNI_OP(aesdec),
2988 [0xdf] = AESNI_OP(aesdeclast),
2991 static const struct SSEOpHelper_eppi sse_op_table7[256] = {
2992 [0x08] = SSE41_OP(roundps),
2993 [0x09] = SSE41_OP(roundpd),
2994 [0x0a] = SSE41_OP(roundss),
2995 [0x0b] = SSE41_OP(roundsd),
2996 [0x0c] = SSE41_OP(blendps),
2997 [0x0d] = SSE41_OP(blendpd),
2998 [0x0e] = SSE41_OP(pblendw),
2999 [0x0f] = SSSE3_OP(palignr),
3000 [0x14] = SSE41_SPECIAL, /* pextrb */
3001 [0x15] = SSE41_SPECIAL, /* pextrw */
3002 [0x16] = SSE41_SPECIAL, /* pextrd/pextrq */
3003 [0x17] = SSE41_SPECIAL, /* extractps */
3004 [0x20] = SSE41_SPECIAL, /* pinsrb */
3005 [0x21] = SSE41_SPECIAL, /* insertps */
3006 [0x22] = SSE41_SPECIAL, /* pinsrd/pinsrq */
3007 [0x40] = SSE41_OP(dpps),
3008 [0x41] = SSE41_OP(dppd),
3009 [0x42] = SSE41_OP(mpsadbw),
3010 [0x44] = PCLMULQDQ_OP(pclmulqdq),
3011 [0x60] = SSE42_OP(pcmpestrm),
3012 [0x61] = SSE42_OP(pcmpestri),
3013 [0x62] = SSE42_OP(pcmpistrm),
3014 [0x63] = SSE42_OP(pcmpistri),
3015 [0xdf] = AESNI_OP(aeskeygenassist),
3018 static void gen_sse(CPUX86State *env, DisasContext *s, int b,
3019 target_ulong pc_start, int rex_r)
3021 int b1, op1_offset, op2_offset, is_xmm, val;
3022 int modrm, mod, rm, reg;
3023 SSEFunc_0_epp sse_fn_epp;
3024 SSEFunc_0_eppi sse_fn_eppi;
3025 SSEFunc_0_ppi sse_fn_ppi;
3026 SSEFunc_0_eppt sse_fn_eppt;
3027 TCGMemOp ot;
3029 b &= 0xff;
3030 if (s->prefix & PREFIX_DATA)
3031 b1 = 1;
3032 else if (s->prefix & PREFIX_REPZ)
3033 b1 = 2;
3034 else if (s->prefix & PREFIX_REPNZ)
3035 b1 = 3;
3036 else
3037 b1 = 0;
3038 sse_fn_epp = sse_op_table1[b][b1];
3039 if (!sse_fn_epp) {
3040 goto unknown_op;
3042 if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
3043 is_xmm = 1;
3044 } else {
3045 if (b1 == 0) {
3046 /* MMX case */
3047 is_xmm = 0;
3048 } else {
3049 is_xmm = 1;
3052 /* simple MMX/SSE operation */
3053 if (s->flags & HF_TS_MASK) {
3054 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
3055 return;
3057 if (s->flags & HF_EM_MASK) {
3058 illegal_op:
3059 gen_illegal_opcode(s);
3060 return;
3062 if (is_xmm
3063 && !(s->flags & HF_OSFXSR_MASK)
3064 && ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))) {
3065 goto unknown_op;
3067 if (b == 0x0e) {
3068 if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW)) {
3069 /* If we were fully decoding this we might use illegal_op. */
3070 goto unknown_op;
3072 /* femms */
3073 gen_helper_emms(cpu_env);
3074 return;
3076 if (b == 0x77) {
3077 /* emms */
3078 gen_helper_emms(cpu_env);
3079 return;
3081 /* prepare MMX state (XXX: optimize by storing fptt and fptags in
3082 the static cpu state) */
3083 if (!is_xmm) {
3084 gen_helper_enter_mmx(cpu_env);
3087 modrm = x86_ldub_code(env, s);
3088 reg = ((modrm >> 3) & 7);
3089 if (is_xmm)
3090 reg |= rex_r;
3091 mod = (modrm >> 6) & 3;
3092 if (sse_fn_epp == SSE_SPECIAL) {
3093 b |= (b1 << 8);
3094 switch(b) {
3095 case 0x0e7: /* movntq */
3096 if (mod == 3) {
3097 goto illegal_op;
3099 gen_lea_modrm(env, s, modrm);
3100 gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
3101 break;
3102 case 0x1e7: /* movntdq */
3103 case 0x02b: /* movntps */
3104 case 0x12b: /* movntps */
3105 if (mod == 3)
3106 goto illegal_op;
3107 gen_lea_modrm(env, s, modrm);
3108 gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3109 break;
3110 case 0x3f0: /* lddqu */
3111 if (mod == 3)
3112 goto illegal_op;
3113 gen_lea_modrm(env, s, modrm);
3114 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3115 break;
3116 case 0x22b: /* movntss */
3117 case 0x32b: /* movntsd */
3118 if (mod == 3)
3119 goto illegal_op;
3120 gen_lea_modrm(env, s, modrm);
3121 if (b1 & 1) {
3122 gen_stq_env_A0(s, offsetof(CPUX86State,
3123 xmm_regs[reg].ZMM_Q(0)));
3124 } else {
3125 tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
3126 xmm_regs[reg].ZMM_L(0)));
3127 gen_op_st_v(s, MO_32, cpu_T0, cpu_A0);
3129 break;
3130 case 0x6e: /* movd mm, ea */
3131 #ifdef TARGET_X86_64
3132 if (s->dflag == MO_64) {
3133 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
3134 tcg_gen_st_tl(cpu_T0, cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
3135 } else
3136 #endif
3138 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
3139 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3140 offsetof(CPUX86State,fpregs[reg].mmx));
3141 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
3142 gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
3144 break;
3145 case 0x16e: /* movd xmm, ea */
3146 #ifdef TARGET_X86_64
3147 if (s->dflag == MO_64) {
3148 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
3149 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3150 offsetof(CPUX86State,xmm_regs[reg]));
3151 gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T0);
3152 } else
3153 #endif
3155 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
3156 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3157 offsetof(CPUX86State,xmm_regs[reg]));
3158 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
3159 gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
3161 break;
3162 case 0x6f: /* movq mm, ea */
3163 if (mod != 3) {
3164 gen_lea_modrm(env, s, modrm);
3165 gen_ldq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
3166 } else {
3167 rm = (modrm & 7);
3168 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
3169 offsetof(CPUX86State,fpregs[rm].mmx));
3170 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
3171 offsetof(CPUX86State,fpregs[reg].mmx));
3173 break;
3174 case 0x010: /* movups */
3175 case 0x110: /* movupd */
3176 case 0x028: /* movaps */
3177 case 0x128: /* movapd */
3178 case 0x16f: /* movdqa xmm, ea */
3179 case 0x26f: /* movdqu xmm, ea */
3180 if (mod != 3) {
3181 gen_lea_modrm(env, s, modrm);
3182 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3183 } else {
3184 rm = (modrm & 7) | REX_B(s);
3185 gen_op_movo(offsetof(CPUX86State,xmm_regs[reg]),
3186 offsetof(CPUX86State,xmm_regs[rm]));
3188 break;
3189 case 0x210: /* movss xmm, ea */
3190 if (mod != 3) {
3191 gen_lea_modrm(env, s, modrm);
3192 gen_op_ld_v(s, MO_32, cpu_T0, cpu_A0);
3193 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
3194 tcg_gen_movi_tl(cpu_T0, 0);
3195 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(1)));
3196 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)));
3197 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)));
3198 } else {
3199 rm = (modrm & 7) | REX_B(s);
3200 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)),
3201 offsetof(CPUX86State,xmm_regs[rm].ZMM_L(0)));
3203 break;
3204 case 0x310: /* movsd xmm, ea */
3205 if (mod != 3) {
3206 gen_lea_modrm(env, s, modrm);
3207 gen_ldq_env_A0(s, offsetof(CPUX86State,
3208 xmm_regs[reg].ZMM_Q(0)));
3209 tcg_gen_movi_tl(cpu_T0, 0);
3210 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)));
3211 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)));
3212 } else {
3213 rm = (modrm & 7) | REX_B(s);
3214 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3215 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
3217 break;
3218 case 0x012: /* movlps */
3219 case 0x112: /* movlpd */
3220 if (mod != 3) {
3221 gen_lea_modrm(env, s, modrm);
3222 gen_ldq_env_A0(s, offsetof(CPUX86State,
3223 xmm_regs[reg].ZMM_Q(0)));
3224 } else {
3225 /* movhlps */
3226 rm = (modrm & 7) | REX_B(s);
3227 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3228 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(1)));
3230 break;
3231 case 0x212: /* movsldup */
3232 if (mod != 3) {
3233 gen_lea_modrm(env, s, modrm);
3234 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3235 } else {
3236 rm = (modrm & 7) | REX_B(s);
3237 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)),
3238 offsetof(CPUX86State,xmm_regs[rm].ZMM_L(0)));
3239 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)),
3240 offsetof(CPUX86State,xmm_regs[rm].ZMM_L(2)));
3242 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(1)),
3243 offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
3244 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)),
3245 offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)));
3246 break;
3247 case 0x312: /* movddup */
3248 if (mod != 3) {
3249 gen_lea_modrm(env, s, modrm);
3250 gen_ldq_env_A0(s, offsetof(CPUX86State,
3251 xmm_regs[reg].ZMM_Q(0)));
3252 } else {
3253 rm = (modrm & 7) | REX_B(s);
3254 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3255 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
3257 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)),
3258 offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
3259 break;
3260 case 0x016: /* movhps */
3261 case 0x116: /* movhpd */
3262 if (mod != 3) {
3263 gen_lea_modrm(env, s, modrm);
3264 gen_ldq_env_A0(s, offsetof(CPUX86State,
3265 xmm_regs[reg].ZMM_Q(1)));
3266 } else {
3267 /* movlhps */
3268 rm = (modrm & 7) | REX_B(s);
3269 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)),
3270 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
3272 break;
3273 case 0x216: /* movshdup */
3274 if (mod != 3) {
3275 gen_lea_modrm(env, s, modrm);
3276 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3277 } else {
3278 rm = (modrm & 7) | REX_B(s);
3279 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(1)),
3280 offsetof(CPUX86State,xmm_regs[rm].ZMM_L(1)));
3281 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)),
3282 offsetof(CPUX86State,xmm_regs[rm].ZMM_L(3)));
3284 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)),
3285 offsetof(CPUX86State,xmm_regs[reg].ZMM_L(1)));
3286 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(2)),
3287 offsetof(CPUX86State,xmm_regs[reg].ZMM_L(3)));
3288 break;
3289 case 0x178:
3290 case 0x378:
3292 int bit_index, field_length;
3294 if (b1 == 1 && reg != 0)
3295 goto illegal_op;
3296 field_length = x86_ldub_code(env, s) & 0x3F;
3297 bit_index = x86_ldub_code(env, s) & 0x3F;
3298 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3299 offsetof(CPUX86State,xmm_regs[reg]));
3300 if (b1 == 1)
3301 gen_helper_extrq_i(cpu_env, cpu_ptr0,
3302 tcg_const_i32(bit_index),
3303 tcg_const_i32(field_length));
3304 else
3305 gen_helper_insertq_i(cpu_env, cpu_ptr0,
3306 tcg_const_i32(bit_index),
3307 tcg_const_i32(field_length));
3309 break;
3310 case 0x7e: /* movd ea, mm */
3311 #ifdef TARGET_X86_64
3312 if (s->dflag == MO_64) {
3313 tcg_gen_ld_i64(cpu_T0, cpu_env,
3314 offsetof(CPUX86State,fpregs[reg].mmx));
3315 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3316 } else
3317 #endif
3319 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
3320 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
3321 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
3323 break;
3324 case 0x17e: /* movd ea, xmm */
3325 #ifdef TARGET_X86_64
3326 if (s->dflag == MO_64) {
3327 tcg_gen_ld_i64(cpu_T0, cpu_env,
3328 offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
3329 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
3330 } else
3331 #endif
3333 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
3334 offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
3335 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
3337 break;
3338 case 0x27e: /* movq xmm, ea */
3339 if (mod != 3) {
3340 gen_lea_modrm(env, s, modrm);
3341 gen_ldq_env_A0(s, offsetof(CPUX86State,
3342 xmm_regs[reg].ZMM_Q(0)));
3343 } else {
3344 rm = (modrm & 7) | REX_B(s);
3345 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3346 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
3348 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)));
3349 break;
3350 case 0x7f: /* movq ea, mm */
3351 if (mod != 3) {
3352 gen_lea_modrm(env, s, modrm);
3353 gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
3354 } else {
3355 rm = (modrm & 7);
3356 gen_op_movq(offsetof(CPUX86State,fpregs[rm].mmx),
3357 offsetof(CPUX86State,fpregs[reg].mmx));
3359 break;
3360 case 0x011: /* movups */
3361 case 0x111: /* movupd */
3362 case 0x029: /* movaps */
3363 case 0x129: /* movapd */
3364 case 0x17f: /* movdqa ea, xmm */
3365 case 0x27f: /* movdqu ea, xmm */
3366 if (mod != 3) {
3367 gen_lea_modrm(env, s, modrm);
3368 gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
3369 } else {
3370 rm = (modrm & 7) | REX_B(s);
3371 gen_op_movo(offsetof(CPUX86State,xmm_regs[rm]),
3372 offsetof(CPUX86State,xmm_regs[reg]));
3374 break;
3375 case 0x211: /* movss ea, xmm */
3376 if (mod != 3) {
3377 gen_lea_modrm(env, s, modrm);
3378 tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
3379 gen_op_st_v(s, MO_32, cpu_T0, cpu_A0);
3380 } else {
3381 rm = (modrm & 7) | REX_B(s);
3382 gen_op_movl(offsetof(CPUX86State,xmm_regs[rm].ZMM_L(0)),
3383 offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
3385 break;
3386 case 0x311: /* movsd ea, xmm */
3387 if (mod != 3) {
3388 gen_lea_modrm(env, s, modrm);
3389 gen_stq_env_A0(s, offsetof(CPUX86State,
3390 xmm_regs[reg].ZMM_Q(0)));
3391 } else {
3392 rm = (modrm & 7) | REX_B(s);
3393 gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)),
3394 offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
3396 break;
3397 case 0x013: /* movlps */
3398 case 0x113: /* movlpd */
3399 if (mod != 3) {
3400 gen_lea_modrm(env, s, modrm);
3401 gen_stq_env_A0(s, offsetof(CPUX86State,
3402 xmm_regs[reg].ZMM_Q(0)));
3403 } else {
3404 goto illegal_op;
3406 break;
3407 case 0x017: /* movhps */
3408 case 0x117: /* movhpd */
3409 if (mod != 3) {
3410 gen_lea_modrm(env, s, modrm);
3411 gen_stq_env_A0(s, offsetof(CPUX86State,
3412 xmm_regs[reg].ZMM_Q(1)));
3413 } else {
3414 goto illegal_op;
3416 break;
3417 case 0x71: /* shift mm, im */
3418 case 0x72:
3419 case 0x73:
3420 case 0x171: /* shift xmm, im */
3421 case 0x172:
3422 case 0x173:
3423 if (b1 >= 2) {
3424 goto unknown_op;
3426 val = x86_ldub_code(env, s);
3427 if (is_xmm) {
3428 tcg_gen_movi_tl(cpu_T0, val);
3429 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_t0.ZMM_L(0)));
3430 tcg_gen_movi_tl(cpu_T0, 0);
3431 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_t0.ZMM_L(1)));
3432 op1_offset = offsetof(CPUX86State,xmm_t0);
3433 } else {
3434 tcg_gen_movi_tl(cpu_T0, val);
3435 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
3436 tcg_gen_movi_tl(cpu_T0, 0);
3437 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
3438 op1_offset = offsetof(CPUX86State,mmx_t0);
3440 sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
3441 (((modrm >> 3)) & 7)][b1];
3442 if (!sse_fn_epp) {
3443 goto unknown_op;
3445 if (is_xmm) {
3446 rm = (modrm & 7) | REX_B(s);
3447 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3448 } else {
3449 rm = (modrm & 7);
3450 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3452 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3453 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
3454 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
3455 break;
3456 case 0x050: /* movmskps */
3457 rm = (modrm & 7) | REX_B(s);
3458 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3459 offsetof(CPUX86State,xmm_regs[rm]));
3460 gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3461 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
3462 break;
3463 case 0x150: /* movmskpd */
3464 rm = (modrm & 7) | REX_B(s);
3465 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3466 offsetof(CPUX86State,xmm_regs[rm]));
3467 gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3468 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
3469 break;
3470 case 0x02a: /* cvtpi2ps */
3471 case 0x12a: /* cvtpi2pd */
3472 gen_helper_enter_mmx(cpu_env);
3473 if (mod != 3) {
3474 gen_lea_modrm(env, s, modrm);
3475 op2_offset = offsetof(CPUX86State,mmx_t0);
3476 gen_ldq_env_A0(s, op2_offset);
3477 } else {
3478 rm = (modrm & 7);
3479 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3481 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
3482 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3483 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
3484 switch(b >> 8) {
3485 case 0x0:
3486 gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
3487 break;
3488 default:
3489 case 0x1:
3490 gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
3491 break;
3493 break;
3494 case 0x22a: /* cvtsi2ss */
3495 case 0x32a: /* cvtsi2sd */
3496 ot = mo_64_32(s->dflag);
3497 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3498 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
3499 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3500 if (ot == MO_32) {
3501 SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
3502 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
3503 sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
3504 } else {
3505 #ifdef TARGET_X86_64
3506 SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
3507 sse_fn_epl(cpu_env, cpu_ptr0, cpu_T0);
3508 #else
3509 goto illegal_op;
3510 #endif
3512 break;
3513 case 0x02c: /* cvttps2pi */
3514 case 0x12c: /* cvttpd2pi */
3515 case 0x02d: /* cvtps2pi */
3516 case 0x12d: /* cvtpd2pi */
3517 gen_helper_enter_mmx(cpu_env);
3518 if (mod != 3) {
3519 gen_lea_modrm(env, s, modrm);
3520 op2_offset = offsetof(CPUX86State,xmm_t0);
3521 gen_ldo_env_A0(s, op2_offset);
3522 } else {
3523 rm = (modrm & 7) | REX_B(s);
3524 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3526 op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
3527 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3528 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
3529 switch(b) {
3530 case 0x02c:
3531 gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
3532 break;
3533 case 0x12c:
3534 gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
3535 break;
3536 case 0x02d:
3537 gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
3538 break;
3539 case 0x12d:
3540 gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
3541 break;
3543 break;
3544 case 0x22c: /* cvttss2si */
3545 case 0x32c: /* cvttsd2si */
3546 case 0x22d: /* cvtss2si */
3547 case 0x32d: /* cvtsd2si */
3548 ot = mo_64_32(s->dflag);
3549 if (mod != 3) {
3550 gen_lea_modrm(env, s, modrm);
3551 if ((b >> 8) & 1) {
3552 gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.ZMM_Q(0)));
3553 } else {
3554 gen_op_ld_v(s, MO_32, cpu_T0, cpu_A0);
3555 tcg_gen_st32_tl(cpu_T0, cpu_env, offsetof(CPUX86State,xmm_t0.ZMM_L(0)));
3557 op2_offset = offsetof(CPUX86State,xmm_t0);
3558 } else {
3559 rm = (modrm & 7) | REX_B(s);
3560 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3562 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3563 if (ot == MO_32) {
3564 SSEFunc_i_ep sse_fn_i_ep =
3565 sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
3566 sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3567 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
3568 } else {
3569 #ifdef TARGET_X86_64
3570 SSEFunc_l_ep sse_fn_l_ep =
3571 sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
3572 sse_fn_l_ep(cpu_T0, cpu_env, cpu_ptr0);
3573 #else
3574 goto illegal_op;
3575 #endif
3577 gen_op_mov_reg_v(ot, reg, cpu_T0);
3578 break;
3579 case 0xc4: /* pinsrw */
3580 case 0x1c4:
3581 s->rip_offset = 1;
3582 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
3583 val = x86_ldub_code(env, s);
3584 if (b1) {
3585 val &= 7;
3586 tcg_gen_st16_tl(cpu_T0, cpu_env,
3587 offsetof(CPUX86State,xmm_regs[reg].ZMM_W(val)));
3588 } else {
3589 val &= 3;
3590 tcg_gen_st16_tl(cpu_T0, cpu_env,
3591 offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
3593 break;
3594 case 0xc5: /* pextrw */
3595 case 0x1c5:
3596 if (mod != 3)
3597 goto illegal_op;
3598 ot = mo_64_32(s->dflag);
3599 val = x86_ldub_code(env, s);
3600 if (b1) {
3601 val &= 7;
3602 rm = (modrm & 7) | REX_B(s);
3603 tcg_gen_ld16u_tl(cpu_T0, cpu_env,
3604 offsetof(CPUX86State,xmm_regs[rm].ZMM_W(val)));
3605 } else {
3606 val &= 3;
3607 rm = (modrm & 7);
3608 tcg_gen_ld16u_tl(cpu_T0, cpu_env,
3609 offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
3611 reg = ((modrm >> 3) & 7) | rex_r;
3612 gen_op_mov_reg_v(ot, reg, cpu_T0);
3613 break;
3614 case 0x1d6: /* movq ea, xmm */
3615 if (mod != 3) {
3616 gen_lea_modrm(env, s, modrm);
3617 gen_stq_env_A0(s, offsetof(CPUX86State,
3618 xmm_regs[reg].ZMM_Q(0)));
3619 } else {
3620 rm = (modrm & 7) | REX_B(s);
3621 gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)),
3622 offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
3623 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(1)));
3625 break;
3626 case 0x2d6: /* movq2dq */
3627 gen_helper_enter_mmx(cpu_env);
3628 rm = (modrm & 7);
3629 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3630 offsetof(CPUX86State,fpregs[rm].mmx));
3631 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)));
3632 break;
3633 case 0x3d6: /* movdq2q */
3634 gen_helper_enter_mmx(cpu_env);
3635 rm = (modrm & 7) | REX_B(s);
3636 gen_op_movq(offsetof(CPUX86State,fpregs[reg & 7].mmx),
3637 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
3638 break;
3639 case 0xd7: /* pmovmskb */
3640 case 0x1d7:
3641 if (mod != 3)
3642 goto illegal_op;
3643 if (b1) {
3644 rm = (modrm & 7) | REX_B(s);
3645 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
3646 gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3647 } else {
3648 rm = (modrm & 7);
3649 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
3650 gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
3652 reg = ((modrm >> 3) & 7) | rex_r;
3653 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
3654 break;
3656 case 0x138:
3657 case 0x038:
3658 b = modrm;
3659 if ((b & 0xf0) == 0xf0) {
3660 goto do_0f_38_fx;
3662 modrm = x86_ldub_code(env, s);
3663 rm = modrm & 7;
3664 reg = ((modrm >> 3) & 7) | rex_r;
3665 mod = (modrm >> 6) & 3;
3666 if (b1 >= 2) {
3667 goto unknown_op;
3670 sse_fn_epp = sse_op_table6[b].op[b1];
3671 if (!sse_fn_epp) {
3672 goto unknown_op;
3674 if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
3675 goto illegal_op;
3677 if (b1) {
3678 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
3679 if (mod == 3) {
3680 op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
3681 } else {
3682 op2_offset = offsetof(CPUX86State,xmm_t0);
3683 gen_lea_modrm(env, s, modrm);
3684 switch (b) {
3685 case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
3686 case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
3687 case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
3688 gen_ldq_env_A0(s, op2_offset +
3689 offsetof(ZMMReg, ZMM_Q(0)));
3690 break;
3691 case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
3692 case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3693 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
3694 s->mem_index, MO_LEUL);
3695 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
3696 offsetof(ZMMReg, ZMM_L(0)));
3697 break;
3698 case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3699 tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
3700 s->mem_index, MO_LEUW);
3701 tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
3702 offsetof(ZMMReg, ZMM_W(0)));
3703 break;
3704 case 0x2a: /* movntqda */
3705 gen_ldo_env_A0(s, op1_offset);
3706 return;
3707 default:
3708 gen_ldo_env_A0(s, op2_offset);
3711 } else {
3712 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
3713 if (mod == 3) {
3714 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3715 } else {
3716 op2_offset = offsetof(CPUX86State,mmx_t0);
3717 gen_lea_modrm(env, s, modrm);
3718 gen_ldq_env_A0(s, op2_offset);
3721 if (sse_fn_epp == SSE_SPECIAL) {
3722 goto unknown_op;
3725 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3726 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
3727 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
3729 if (b == 0x17) {
3730 set_cc_op(s, CC_OP_EFLAGS);
3732 break;
3734 case 0x238:
3735 case 0x338:
3736 do_0f_38_fx:
3737 /* Various integer extensions at 0f 38 f[0-f]. */
3738 b = modrm | (b1 << 8);
3739 modrm = x86_ldub_code(env, s);
3740 reg = ((modrm >> 3) & 7) | rex_r;
3742 switch (b) {
3743 case 0x3f0: /* crc32 Gd,Eb */
3744 case 0x3f1: /* crc32 Gd,Ey */
3745 do_crc32:
3746 if (!(s->cpuid_ext_features & CPUID_EXT_SSE42)) {
3747 goto illegal_op;
3749 if ((b & 0xff) == 0xf0) {
3750 ot = MO_8;
3751 } else if (s->dflag != MO_64) {
3752 ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
3753 } else {
3754 ot = MO_64;
3757 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
3758 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3759 gen_helper_crc32(cpu_T0, cpu_tmp2_i32,
3760 cpu_T0, tcg_const_i32(8 << ot));
3762 ot = mo_64_32(s->dflag);
3763 gen_op_mov_reg_v(ot, reg, cpu_T0);
3764 break;
3766 case 0x1f0: /* crc32 or movbe */
3767 case 0x1f1:
3768 /* For these insns, the f3 prefix is supposed to have priority
3769 over the 66 prefix, but that's not what we implement above
3770 setting b1. */
3771 if (s->prefix & PREFIX_REPNZ) {
3772 goto do_crc32;
3774 /* FALLTHRU */
3775 case 0x0f0: /* movbe Gy,My */
3776 case 0x0f1: /* movbe My,Gy */
3777 if (!(s->cpuid_ext_features & CPUID_EXT_MOVBE)) {
3778 goto illegal_op;
3780 if (s->dflag != MO_64) {
3781 ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
3782 } else {
3783 ot = MO_64;
3786 gen_lea_modrm(env, s, modrm);
3787 if ((b & 1) == 0) {
3788 tcg_gen_qemu_ld_tl(cpu_T0, cpu_A0,
3789 s->mem_index, ot | MO_BE);
3790 gen_op_mov_reg_v(ot, reg, cpu_T0);
3791 } else {
3792 tcg_gen_qemu_st_tl(cpu_regs[reg], cpu_A0,
3793 s->mem_index, ot | MO_BE);
3795 break;
3797 case 0x0f2: /* andn Gy, By, Ey */
3798 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
3799 || !(s->prefix & PREFIX_VEX)
3800 || s->vex_l != 0) {
3801 goto illegal_op;
3803 ot = mo_64_32(s->dflag);
3804 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3805 tcg_gen_andc_tl(cpu_T0, cpu_T0, cpu_regs[s->vex_v]);
3806 gen_op_mov_reg_v(ot, reg, cpu_T0);
3807 gen_op_update1_cc();
3808 set_cc_op(s, CC_OP_LOGICB + ot);
3809 break;
3811 case 0x0f7: /* bextr Gy, Ey, By */
3812 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
3813 || !(s->prefix & PREFIX_VEX)
3814 || s->vex_l != 0) {
3815 goto illegal_op;
3817 ot = mo_64_32(s->dflag);
3819 TCGv bound, zero;
3821 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3822 /* Extract START, and shift the operand.
3823 Shifts larger than operand size get zeros. */
3824 tcg_gen_ext8u_tl(cpu_A0, cpu_regs[s->vex_v]);
3825 tcg_gen_shr_tl(cpu_T0, cpu_T0, cpu_A0);
3827 bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
3828 zero = tcg_const_tl(0);
3829 tcg_gen_movcond_tl(TCG_COND_LEU, cpu_T0, cpu_A0, bound,
3830 cpu_T0, zero);
3831 tcg_temp_free(zero);
3833 /* Extract the LEN into a mask. Lengths larger than
3834 operand size get all ones. */
3835 tcg_gen_extract_tl(cpu_A0, cpu_regs[s->vex_v], 8, 8);
3836 tcg_gen_movcond_tl(TCG_COND_LEU, cpu_A0, cpu_A0, bound,
3837 cpu_A0, bound);
3838 tcg_temp_free(bound);
3839 tcg_gen_movi_tl(cpu_T1, 1);
3840 tcg_gen_shl_tl(cpu_T1, cpu_T1, cpu_A0);
3841 tcg_gen_subi_tl(cpu_T1, cpu_T1, 1);
3842 tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_T1);
3844 gen_op_mov_reg_v(ot, reg, cpu_T0);
3845 gen_op_update1_cc();
3846 set_cc_op(s, CC_OP_LOGICB + ot);
3848 break;
3850 case 0x0f5: /* bzhi Gy, Ey, By */
3851 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
3852 || !(s->prefix & PREFIX_VEX)
3853 || s->vex_l != 0) {
3854 goto illegal_op;
3856 ot = mo_64_32(s->dflag);
3857 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3858 tcg_gen_ext8u_tl(cpu_T1, cpu_regs[s->vex_v]);
3860 TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
3861 /* Note that since we're using BMILG (in order to get O
3862 cleared) we need to store the inverse into C. */
3863 tcg_gen_setcond_tl(TCG_COND_LT, cpu_cc_src,
3864 cpu_T1, bound);
3865 tcg_gen_movcond_tl(TCG_COND_GT, cpu_T1, cpu_T1,
3866 bound, bound, cpu_T1);
3867 tcg_temp_free(bound);
3869 tcg_gen_movi_tl(cpu_A0, -1);
3870 tcg_gen_shl_tl(cpu_A0, cpu_A0, cpu_T1);
3871 tcg_gen_andc_tl(cpu_T0, cpu_T0, cpu_A0);
3872 gen_op_mov_reg_v(ot, reg, cpu_T0);
3873 gen_op_update1_cc();
3874 set_cc_op(s, CC_OP_BMILGB + ot);
3875 break;
3877 case 0x3f6: /* mulx By, Gy, rdx, Ey */
3878 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
3879 || !(s->prefix & PREFIX_VEX)
3880 || s->vex_l != 0) {
3881 goto illegal_op;
3883 ot = mo_64_32(s->dflag);
3884 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3885 switch (ot) {
3886 default:
3887 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
3888 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EDX]);
3889 tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
3890 cpu_tmp2_i32, cpu_tmp3_i32);
3891 tcg_gen_extu_i32_tl(cpu_regs[s->vex_v], cpu_tmp2_i32);
3892 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp3_i32);
3893 break;
3894 #ifdef TARGET_X86_64
3895 case MO_64:
3896 tcg_gen_mulu2_i64(cpu_T0, cpu_T1,
3897 cpu_T0, cpu_regs[R_EDX]);
3898 tcg_gen_mov_i64(cpu_regs[s->vex_v], cpu_T0);
3899 tcg_gen_mov_i64(cpu_regs[reg], cpu_T1);
3900 break;
3901 #endif
3903 break;
3905 case 0x3f5: /* pdep Gy, By, Ey */
3906 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
3907 || !(s->prefix & PREFIX_VEX)
3908 || s->vex_l != 0) {
3909 goto illegal_op;
3911 ot = mo_64_32(s->dflag);
3912 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3913 /* Note that by zero-extending the mask operand, we
3914 automatically handle zero-extending the result. */
3915 if (ot == MO_64) {
3916 tcg_gen_mov_tl(cpu_T1, cpu_regs[s->vex_v]);
3917 } else {
3918 tcg_gen_ext32u_tl(cpu_T1, cpu_regs[s->vex_v]);
3920 gen_helper_pdep(cpu_regs[reg], cpu_T0, cpu_T1);
3921 break;
3923 case 0x2f5: /* pext Gy, By, Ey */
3924 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
3925 || !(s->prefix & PREFIX_VEX)
3926 || s->vex_l != 0) {
3927 goto illegal_op;
3929 ot = mo_64_32(s->dflag);
3930 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3931 /* Note that by zero-extending the mask operand, we
3932 automatically handle zero-extending the result. */
3933 if (ot == MO_64) {
3934 tcg_gen_mov_tl(cpu_T1, cpu_regs[s->vex_v]);
3935 } else {
3936 tcg_gen_ext32u_tl(cpu_T1, cpu_regs[s->vex_v]);
3938 gen_helper_pext(cpu_regs[reg], cpu_T0, cpu_T1);
3939 break;
3941 case 0x1f6: /* adcx Gy, Ey */
3942 case 0x2f6: /* adox Gy, Ey */
3943 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_ADX)) {
3944 goto illegal_op;
3945 } else {
3946 TCGv carry_in, carry_out, zero;
3947 int end_op;
3949 ot = mo_64_32(s->dflag);
3950 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3952 /* Re-use the carry-out from a previous round. */
3953 carry_in = NULL;
3954 carry_out = (b == 0x1f6 ? cpu_cc_dst : cpu_cc_src2);
3955 switch (s->cc_op) {
3956 case CC_OP_ADCX:
3957 if (b == 0x1f6) {
3958 carry_in = cpu_cc_dst;
3959 end_op = CC_OP_ADCX;
3960 } else {
3961 end_op = CC_OP_ADCOX;
3963 break;
3964 case CC_OP_ADOX:
3965 if (b == 0x1f6) {
3966 end_op = CC_OP_ADCOX;
3967 } else {
3968 carry_in = cpu_cc_src2;
3969 end_op = CC_OP_ADOX;
3971 break;
3972 case CC_OP_ADCOX:
3973 end_op = CC_OP_ADCOX;
3974 carry_in = carry_out;
3975 break;
3976 default:
3977 end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
3978 break;
3980 /* If we can't reuse carry-out, get it out of EFLAGS. */
3981 if (!carry_in) {
3982 if (s->cc_op != CC_OP_ADCX && s->cc_op != CC_OP_ADOX) {
3983 gen_compute_eflags(s);
3985 carry_in = cpu_tmp0;
3986 tcg_gen_extract_tl(carry_in, cpu_cc_src,
3987 ctz32(b == 0x1f6 ? CC_C : CC_O), 1);
3990 switch (ot) {
3991 #ifdef TARGET_X86_64
3992 case MO_32:
3993 /* If we know TL is 64-bit, and we want a 32-bit
3994 result, just do everything in 64-bit arithmetic. */
3995 tcg_gen_ext32u_i64(cpu_regs[reg], cpu_regs[reg]);
3996 tcg_gen_ext32u_i64(cpu_T0, cpu_T0);
3997 tcg_gen_add_i64(cpu_T0, cpu_T0, cpu_regs[reg]);
3998 tcg_gen_add_i64(cpu_T0, cpu_T0, carry_in);
3999 tcg_gen_ext32u_i64(cpu_regs[reg], cpu_T0);
4000 tcg_gen_shri_i64(carry_out, cpu_T0, 32);
4001 break;
4002 #endif
4003 default:
4004 /* Otherwise compute the carry-out in two steps. */
4005 zero = tcg_const_tl(0);
4006 tcg_gen_add2_tl(cpu_T0, carry_out,
4007 cpu_T0, zero,
4008 carry_in, zero);
4009 tcg_gen_add2_tl(cpu_regs[reg], carry_out,
4010 cpu_regs[reg], carry_out,
4011 cpu_T0, zero);
4012 tcg_temp_free(zero);
4013 break;
4015 set_cc_op(s, end_op);
4017 break;
4019 case 0x1f7: /* shlx Gy, Ey, By */
4020 case 0x2f7: /* sarx Gy, Ey, By */
4021 case 0x3f7: /* shrx Gy, Ey, By */
4022 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
4023 || !(s->prefix & PREFIX_VEX)
4024 || s->vex_l != 0) {
4025 goto illegal_op;
4027 ot = mo_64_32(s->dflag);
4028 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4029 if (ot == MO_64) {
4030 tcg_gen_andi_tl(cpu_T1, cpu_regs[s->vex_v], 63);
4031 } else {
4032 tcg_gen_andi_tl(cpu_T1, cpu_regs[s->vex_v], 31);
4034 if (b == 0x1f7) {
4035 tcg_gen_shl_tl(cpu_T0, cpu_T0, cpu_T1);
4036 } else if (b == 0x2f7) {
4037 if (ot != MO_64) {
4038 tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
4040 tcg_gen_sar_tl(cpu_T0, cpu_T0, cpu_T1);
4041 } else {
4042 if (ot != MO_64) {
4043 tcg_gen_ext32u_tl(cpu_T0, cpu_T0);
4045 tcg_gen_shr_tl(cpu_T0, cpu_T0, cpu_T1);
4047 gen_op_mov_reg_v(ot, reg, cpu_T0);
4048 break;
4050 case 0x0f3:
4051 case 0x1f3:
4052 case 0x2f3:
4053 case 0x3f3: /* Group 17 */
4054 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
4055 || !(s->prefix & PREFIX_VEX)
4056 || s->vex_l != 0) {
4057 goto illegal_op;
4059 ot = mo_64_32(s->dflag);
4060 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4062 tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
4063 switch (reg & 7) {
4064 case 1: /* blsr By,Ey */
4065 tcg_gen_subi_tl(cpu_T1, cpu_T0, 1);
4066 tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_T1);
4067 break;
4068 case 2: /* blsmsk By,Ey */
4069 tcg_gen_subi_tl(cpu_T1, cpu_T0, 1);
4070 tcg_gen_xor_tl(cpu_T0, cpu_T0, cpu_T1);
4071 break;
4072 case 3: /* blsi By, Ey */
4073 tcg_gen_neg_tl(cpu_T1, cpu_T0);
4074 tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_T1);
4075 break;
4076 default:
4077 goto unknown_op;
4079 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4080 gen_op_mov_reg_v(ot, s->vex_v, cpu_T0);
4081 set_cc_op(s, CC_OP_BMILGB + ot);
4082 break;
4084 default:
4085 goto unknown_op;
4087 break;
4089 case 0x03a:
4090 case 0x13a:
4091 b = modrm;
4092 modrm = x86_ldub_code(env, s);
4093 rm = modrm & 7;
4094 reg = ((modrm >> 3) & 7) | rex_r;
4095 mod = (modrm >> 6) & 3;
4096 if (b1 >= 2) {
4097 goto unknown_op;
4100 sse_fn_eppi = sse_op_table7[b].op[b1];
4101 if (!sse_fn_eppi) {
4102 goto unknown_op;
4104 if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
4105 goto illegal_op;
4107 s->rip_offset = 1;
4109 if (sse_fn_eppi == SSE_SPECIAL) {
4110 ot = mo_64_32(s->dflag);
4111 rm = (modrm & 7) | REX_B(s);
4112 if (mod != 3)
4113 gen_lea_modrm(env, s, modrm);
4114 reg = ((modrm >> 3) & 7) | rex_r;
4115 val = x86_ldub_code(env, s);
4116 switch (b) {
4117 case 0x14: /* pextrb */
4118 tcg_gen_ld8u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
4119 xmm_regs[reg].ZMM_B(val & 15)));
4120 if (mod == 3) {
4121 gen_op_mov_reg_v(ot, rm, cpu_T0);
4122 } else {
4123 tcg_gen_qemu_st_tl(cpu_T0, cpu_A0,
4124 s->mem_index, MO_UB);
4126 break;
4127 case 0x15: /* pextrw */
4128 tcg_gen_ld16u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
4129 xmm_regs[reg].ZMM_W(val & 7)));
4130 if (mod == 3) {
4131 gen_op_mov_reg_v(ot, rm, cpu_T0);
4132 } else {
4133 tcg_gen_qemu_st_tl(cpu_T0, cpu_A0,
4134 s->mem_index, MO_LEUW);
4136 break;
4137 case 0x16:
4138 if (ot == MO_32) { /* pextrd */
4139 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
4140 offsetof(CPUX86State,
4141 xmm_regs[reg].ZMM_L(val & 3)));
4142 if (mod == 3) {
4143 tcg_gen_extu_i32_tl(cpu_regs[rm], cpu_tmp2_i32);
4144 } else {
4145 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
4146 s->mem_index, MO_LEUL);
4148 } else { /* pextrq */
4149 #ifdef TARGET_X86_64
4150 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
4151 offsetof(CPUX86State,
4152 xmm_regs[reg].ZMM_Q(val & 1)));
4153 if (mod == 3) {
4154 tcg_gen_mov_i64(cpu_regs[rm], cpu_tmp1_i64);
4155 } else {
4156 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
4157 s->mem_index, MO_LEQ);
4159 #else
4160 goto illegal_op;
4161 #endif
4163 break;
4164 case 0x17: /* extractps */
4165 tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
4166 xmm_regs[reg].ZMM_L(val & 3)));
4167 if (mod == 3) {
4168 gen_op_mov_reg_v(ot, rm, cpu_T0);
4169 } else {
4170 tcg_gen_qemu_st_tl(cpu_T0, cpu_A0,
4171 s->mem_index, MO_LEUL);
4173 break;
4174 case 0x20: /* pinsrb */
4175 if (mod == 3) {
4176 gen_op_mov_v_reg(MO_32, cpu_T0, rm);
4177 } else {
4178 tcg_gen_qemu_ld_tl(cpu_T0, cpu_A0,
4179 s->mem_index, MO_UB);
4181 tcg_gen_st8_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
4182 xmm_regs[reg].ZMM_B(val & 15)));
4183 break;
4184 case 0x21: /* insertps */
4185 if (mod == 3) {
4186 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
4187 offsetof(CPUX86State,xmm_regs[rm]
4188 .ZMM_L((val >> 6) & 3)));
4189 } else {
4190 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
4191 s->mem_index, MO_LEUL);
4193 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
4194 offsetof(CPUX86State,xmm_regs[reg]
4195 .ZMM_L((val >> 4) & 3)));
4196 if ((val >> 0) & 1)
4197 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4198 cpu_env, offsetof(CPUX86State,
4199 xmm_regs[reg].ZMM_L(0)));
4200 if ((val >> 1) & 1)
4201 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4202 cpu_env, offsetof(CPUX86State,
4203 xmm_regs[reg].ZMM_L(1)));
4204 if ((val >> 2) & 1)
4205 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4206 cpu_env, offsetof(CPUX86State,
4207 xmm_regs[reg].ZMM_L(2)));
4208 if ((val >> 3) & 1)
4209 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4210 cpu_env, offsetof(CPUX86State,
4211 xmm_regs[reg].ZMM_L(3)));
4212 break;
4213 case 0x22:
4214 if (ot == MO_32) { /* pinsrd */
4215 if (mod == 3) {
4216 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[rm]);
4217 } else {
4218 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
4219 s->mem_index, MO_LEUL);
4221 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
4222 offsetof(CPUX86State,
4223 xmm_regs[reg].ZMM_L(val & 3)));
4224 } else { /* pinsrq */
4225 #ifdef TARGET_X86_64
4226 if (mod == 3) {
4227 gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
4228 } else {
4229 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
4230 s->mem_index, MO_LEQ);
4232 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
4233 offsetof(CPUX86State,
4234 xmm_regs[reg].ZMM_Q(val & 1)));
4235 #else
4236 goto illegal_op;
4237 #endif
4239 break;
4241 return;
4244 if (b1) {
4245 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
4246 if (mod == 3) {
4247 op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
4248 } else {
4249 op2_offset = offsetof(CPUX86State,xmm_t0);
4250 gen_lea_modrm(env, s, modrm);
4251 gen_ldo_env_A0(s, op2_offset);
4253 } else {
4254 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
4255 if (mod == 3) {
4256 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
4257 } else {
4258 op2_offset = offsetof(CPUX86State,mmx_t0);
4259 gen_lea_modrm(env, s, modrm);
4260 gen_ldq_env_A0(s, op2_offset);
4263 val = x86_ldub_code(env, s);
4265 if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
4266 set_cc_op(s, CC_OP_EFLAGS);
4268 if (s->dflag == MO_64) {
4269 /* The helper must use entire 64-bit gp registers */
4270 val |= 1 << 8;
4274 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4275 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
4276 sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
4277 break;
4279 case 0x33a:
4280 /* Various integer extensions at 0f 3a f[0-f]. */
4281 b = modrm | (b1 << 8);
4282 modrm = x86_ldub_code(env, s);
4283 reg = ((modrm >> 3) & 7) | rex_r;
4285 switch (b) {
4286 case 0x3f0: /* rorx Gy,Ey, Ib */
4287 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
4288 || !(s->prefix & PREFIX_VEX)
4289 || s->vex_l != 0) {
4290 goto illegal_op;
4292 ot = mo_64_32(s->dflag);
4293 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4294 b = x86_ldub_code(env, s);
4295 if (ot == MO_64) {
4296 tcg_gen_rotri_tl(cpu_T0, cpu_T0, b & 63);
4297 } else {
4298 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
4299 tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, b & 31);
4300 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
4302 gen_op_mov_reg_v(ot, reg, cpu_T0);
4303 break;
4305 default:
4306 goto unknown_op;
4308 break;
4310 default:
4311 unknown_op:
4312 gen_unknown_opcode(env, s);
4313 return;
4315 } else {
4316 /* generic MMX or SSE operation */
4317 switch(b) {
4318 case 0x70: /* pshufx insn */
4319 case 0xc6: /* pshufx insn */
4320 case 0xc2: /* compare insns */
4321 s->rip_offset = 1;
4322 break;
4323 default:
4324 break;
4326 if (is_xmm) {
4327 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
4328 if (mod != 3) {
4329 int sz = 4;
4331 gen_lea_modrm(env, s, modrm);
4332 op2_offset = offsetof(CPUX86State,xmm_t0);
4334 switch (b) {
4335 case 0x50 ... 0x5a:
4336 case 0x5c ... 0x5f:
4337 case 0xc2:
4338 /* Most sse scalar operations. */
4339 if (b1 == 2) {
4340 sz = 2;
4341 } else if (b1 == 3) {
4342 sz = 3;
4344 break;
4346 case 0x2e: /* ucomis[sd] */
4347 case 0x2f: /* comis[sd] */
4348 if (b1 == 0) {
4349 sz = 2;
4350 } else {
4351 sz = 3;
4353 break;
4356 switch (sz) {
4357 case 2:
4358 /* 32 bit access */
4359 gen_op_ld_v(s, MO_32, cpu_T0, cpu_A0);
4360 tcg_gen_st32_tl(cpu_T0, cpu_env,
4361 offsetof(CPUX86State,xmm_t0.ZMM_L(0)));
4362 break;
4363 case 3:
4364 /* 64 bit access */
4365 gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.ZMM_D(0)));
4366 break;
4367 default:
4368 /* 128 bit access */
4369 gen_ldo_env_A0(s, op2_offset);
4370 break;
4372 } else {
4373 rm = (modrm & 7) | REX_B(s);
4374 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
4376 } else {
4377 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
4378 if (mod != 3) {
4379 gen_lea_modrm(env, s, modrm);
4380 op2_offset = offsetof(CPUX86State,mmx_t0);
4381 gen_ldq_env_A0(s, op2_offset);
4382 } else {
4383 rm = (modrm & 7);
4384 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
4387 switch(b) {
4388 case 0x0f: /* 3DNow! data insns */
4389 val = x86_ldub_code(env, s);
4390 sse_fn_epp = sse_op_table5[val];
4391 if (!sse_fn_epp) {
4392 goto unknown_op;
4394 if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW)) {
4395 goto illegal_op;
4397 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4398 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
4399 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
4400 break;
4401 case 0x70: /* pshufx insn */
4402 case 0xc6: /* pshufx insn */
4403 val = x86_ldub_code(env, s);
4404 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4405 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
4406 /* XXX: introduce a new table? */
4407 sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
4408 sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
4409 break;
4410 case 0xc2:
4411 /* compare insns */
4412 val = x86_ldub_code(env, s);
4413 if (val >= 8)
4414 goto unknown_op;
4415 sse_fn_epp = sse_op_table4[val][b1];
4417 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4418 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
4419 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
4420 break;
4421 case 0xf7:
4422 /* maskmov : we must prepare A0 */
4423 if (mod != 3)
4424 goto illegal_op;
4425 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EDI]);
4426 gen_extu(s->aflag, cpu_A0);
4427 gen_add_A0_ds_seg(s);
4429 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4430 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
4431 /* XXX: introduce a new table? */
4432 sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
4433 sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
4434 break;
4435 default:
4436 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4437 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
4438 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
4439 break;
4441 if (b == 0x2e || b == 0x2f) {
4442 set_cc_op(s, CC_OP_EFLAGS);
4447 /* convert one instruction. s->base.is_jmp is set if the translation must
4448 be stopped. Return the next pc value */
4449 static target_ulong disas_insn(DisasContext *s, CPUState *cpu)
4451 CPUX86State *env = cpu->env_ptr;
4452 int b, prefixes;
4453 int shift;
4454 TCGMemOp ot, aflag, dflag;
4455 int modrm, reg, rm, mod, op, opreg, val;
4456 target_ulong next_eip, tval;
4457 int rex_w, rex_r;
4458 target_ulong pc_start = s->base.pc_next;
4460 s->pc_start = s->pc = pc_start;
4461 s->override = -1;
4462 #ifdef TARGET_X86_64
4463 s->rex_x = 0;
4464 s->rex_b = 0;
4465 x86_64_hregs = 0;
4466 #endif
4467 s->rip_offset = 0; /* for relative ip address */
4468 s->vex_l = 0;
4469 s->vex_v = 0;
4470 if (sigsetjmp(s->jmpbuf, 0) != 0) {
4471 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
4472 return s->pc;
4475 prefixes = 0;
4476 rex_w = -1;
4477 rex_r = 0;
4479 next_byte:
4480 b = x86_ldub_code(env, s);
4481 /* Collect prefixes. */
4482 switch (b) {
4483 case 0xf3:
4484 prefixes |= PREFIX_REPZ;
4485 goto next_byte;
4486 case 0xf2:
4487 prefixes |= PREFIX_REPNZ;
4488 goto next_byte;
4489 case 0xf0:
4490 prefixes |= PREFIX_LOCK;
4491 goto next_byte;
4492 case 0x2e:
4493 s->override = R_CS;
4494 goto next_byte;
4495 case 0x36:
4496 s->override = R_SS;
4497 goto next_byte;
4498 case 0x3e:
4499 s->override = R_DS;
4500 goto next_byte;
4501 case 0x26:
4502 s->override = R_ES;
4503 goto next_byte;
4504 case 0x64:
4505 s->override = R_FS;
4506 goto next_byte;
4507 case 0x65:
4508 s->override = R_GS;
4509 goto next_byte;
4510 case 0x66:
4511 prefixes |= PREFIX_DATA;
4512 goto next_byte;
4513 case 0x67:
4514 prefixes |= PREFIX_ADR;
4515 goto next_byte;
4516 #ifdef TARGET_X86_64
4517 case 0x40 ... 0x4f:
4518 if (CODE64(s)) {
4519 /* REX prefix */
4520 rex_w = (b >> 3) & 1;
4521 rex_r = (b & 0x4) << 1;
4522 s->rex_x = (b & 0x2) << 2;
4523 REX_B(s) = (b & 0x1) << 3;
4524 x86_64_hregs = 1; /* select uniform byte register addressing */
4525 goto next_byte;
4527 break;
4528 #endif
4529 case 0xc5: /* 2-byte VEX */
4530 case 0xc4: /* 3-byte VEX */
4531 /* VEX prefixes cannot be used except in 32-bit mode.
4532 Otherwise the instruction is LES or LDS. */
4533 if (s->code32 && !s->vm86) {
4534 static const int pp_prefix[4] = {
4535 0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ
4537 int vex3, vex2 = x86_ldub_code(env, s);
4539 if (!CODE64(s) && (vex2 & 0xc0) != 0xc0) {
4540 /* 4.1.4.6: In 32-bit mode, bits [7:6] must be 11b,
4541 otherwise the instruction is LES or LDS. */
4542 s->pc--; /* rewind the advance_pc() x86_ldub_code() did */
4543 break;
4546 /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
4547 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ
4548 | PREFIX_LOCK | PREFIX_DATA)) {
4549 goto illegal_op;
4551 #ifdef TARGET_X86_64
4552 if (x86_64_hregs) {
4553 goto illegal_op;
4555 #endif
4556 rex_r = (~vex2 >> 4) & 8;
4557 if (b == 0xc5) {
4558 /* 2-byte VEX prefix: RVVVVlpp, implied 0f leading opcode byte */
4559 vex3 = vex2;
4560 b = x86_ldub_code(env, s) | 0x100;
4561 } else {
4562 /* 3-byte VEX prefix: RXBmmmmm wVVVVlpp */
4563 #ifdef TARGET_X86_64
4564 s->rex_x = (~vex2 >> 3) & 8;
4565 s->rex_b = (~vex2 >> 2) & 8;
4566 #endif
4567 vex3 = x86_ldub_code(env, s);
4568 rex_w = (vex3 >> 7) & 1;
4569 switch (vex2 & 0x1f) {
4570 case 0x01: /* Implied 0f leading opcode bytes. */
4571 b = x86_ldub_code(env, s) | 0x100;
4572 break;
4573 case 0x02: /* Implied 0f 38 leading opcode bytes. */
4574 b = 0x138;
4575 break;
4576 case 0x03: /* Implied 0f 3a leading opcode bytes. */
4577 b = 0x13a;
4578 break;
4579 default: /* Reserved for future use. */
4580 goto unknown_op;
4583 s->vex_v = (~vex3 >> 3) & 0xf;
4584 s->vex_l = (vex3 >> 2) & 1;
4585 prefixes |= pp_prefix[vex3 & 3] | PREFIX_VEX;
4587 break;
4590 /* Post-process prefixes. */
4591 if (CODE64(s)) {
4592 /* In 64-bit mode, the default data size is 32-bit. Select 64-bit
4593 data with rex_w, and 16-bit data with 0x66; rex_w takes precedence
4594 over 0x66 if both are present. */
4595 dflag = (rex_w > 0 ? MO_64 : prefixes & PREFIX_DATA ? MO_16 : MO_32);
4596 /* In 64-bit mode, 0x67 selects 32-bit addressing. */
4597 aflag = (prefixes & PREFIX_ADR ? MO_32 : MO_64);
4598 } else {
4599 /* In 16/32-bit mode, 0x66 selects the opposite data size. */
4600 if (s->code32 ^ ((prefixes & PREFIX_DATA) != 0)) {
4601 dflag = MO_32;
4602 } else {
4603 dflag = MO_16;
4605 /* In 16/32-bit mode, 0x67 selects the opposite addressing. */
4606 if (s->code32 ^ ((prefixes & PREFIX_ADR) != 0)) {
4607 aflag = MO_32;
4608 } else {
4609 aflag = MO_16;
4613 s->prefix = prefixes;
4614 s->aflag = aflag;
4615 s->dflag = dflag;
4617 /* now check op code */
4618 reswitch:
4619 switch(b) {
4620 case 0x0f:
4621 /**************************/
4622 /* extended op code */
4623 b = x86_ldub_code(env, s) | 0x100;
4624 goto reswitch;
4626 /**************************/
4627 /* arith & logic */
4628 case 0x00 ... 0x05:
4629 case 0x08 ... 0x0d:
4630 case 0x10 ... 0x15:
4631 case 0x18 ... 0x1d:
4632 case 0x20 ... 0x25:
4633 case 0x28 ... 0x2d:
4634 case 0x30 ... 0x35:
4635 case 0x38 ... 0x3d:
4637 int op, f, val;
4638 op = (b >> 3) & 7;
4639 f = (b >> 1) & 3;
4641 ot = mo_b_d(b, dflag);
4643 switch(f) {
4644 case 0: /* OP Ev, Gv */
4645 modrm = x86_ldub_code(env, s);
4646 reg = ((modrm >> 3) & 7) | rex_r;
4647 mod = (modrm >> 6) & 3;
4648 rm = (modrm & 7) | REX_B(s);
4649 if (mod != 3) {
4650 gen_lea_modrm(env, s, modrm);
4651 opreg = OR_TMP0;
4652 } else if (op == OP_XORL && rm == reg) {
4653 xor_zero:
4654 /* xor reg, reg optimisation */
4655 set_cc_op(s, CC_OP_CLR);
4656 tcg_gen_movi_tl(cpu_T0, 0);
4657 gen_op_mov_reg_v(ot, reg, cpu_T0);
4658 break;
4659 } else {
4660 opreg = rm;
4662 gen_op_mov_v_reg(ot, cpu_T1, reg);
4663 gen_op(s, op, ot, opreg);
4664 break;
4665 case 1: /* OP Gv, Ev */
4666 modrm = x86_ldub_code(env, s);
4667 mod = (modrm >> 6) & 3;
4668 reg = ((modrm >> 3) & 7) | rex_r;
4669 rm = (modrm & 7) | REX_B(s);
4670 if (mod != 3) {
4671 gen_lea_modrm(env, s, modrm);
4672 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
4673 } else if (op == OP_XORL && rm == reg) {
4674 goto xor_zero;
4675 } else {
4676 gen_op_mov_v_reg(ot, cpu_T1, rm);
4678 gen_op(s, op, ot, reg);
4679 break;
4680 case 2: /* OP A, Iv */
4681 val = insn_get(env, s, ot);
4682 tcg_gen_movi_tl(cpu_T1, val);
4683 gen_op(s, op, ot, OR_EAX);
4684 break;
4687 break;
4689 case 0x82:
4690 if (CODE64(s))
4691 goto illegal_op;
4692 case 0x80: /* GRP1 */
4693 case 0x81:
4694 case 0x83:
4696 int val;
4698 ot = mo_b_d(b, dflag);
4700 modrm = x86_ldub_code(env, s);
4701 mod = (modrm >> 6) & 3;
4702 rm = (modrm & 7) | REX_B(s);
4703 op = (modrm >> 3) & 7;
4705 if (mod != 3) {
4706 if (b == 0x83)
4707 s->rip_offset = 1;
4708 else
4709 s->rip_offset = insn_const_size(ot);
4710 gen_lea_modrm(env, s, modrm);
4711 opreg = OR_TMP0;
4712 } else {
4713 opreg = rm;
4716 switch(b) {
4717 default:
4718 case 0x80:
4719 case 0x81:
4720 case 0x82:
4721 val = insn_get(env, s, ot);
4722 break;
4723 case 0x83:
4724 val = (int8_t)insn_get(env, s, MO_8);
4725 break;
4727 tcg_gen_movi_tl(cpu_T1, val);
4728 gen_op(s, op, ot, opreg);
4730 break;
4732 /**************************/
4733 /* inc, dec, and other misc arith */
4734 case 0x40 ... 0x47: /* inc Gv */
4735 ot = dflag;
4736 gen_inc(s, ot, OR_EAX + (b & 7), 1);
4737 break;
4738 case 0x48 ... 0x4f: /* dec Gv */
4739 ot = dflag;
4740 gen_inc(s, ot, OR_EAX + (b & 7), -1);
4741 break;
4742 case 0xf6: /* GRP3 */
4743 case 0xf7:
4744 ot = mo_b_d(b, dflag);
4746 modrm = x86_ldub_code(env, s);
4747 mod = (modrm >> 6) & 3;
4748 rm = (modrm & 7) | REX_B(s);
4749 op = (modrm >> 3) & 7;
4750 if (mod != 3) {
4751 if (op == 0) {
4752 s->rip_offset = insn_const_size(ot);
4754 gen_lea_modrm(env, s, modrm);
4755 /* For those below that handle locked memory, don't load here. */
4756 if (!(s->prefix & PREFIX_LOCK)
4757 || op != 2) {
4758 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
4760 } else {
4761 gen_op_mov_v_reg(ot, cpu_T0, rm);
4764 switch(op) {
4765 case 0: /* test */
4766 val = insn_get(env, s, ot);
4767 tcg_gen_movi_tl(cpu_T1, val);
4768 gen_op_testl_T0_T1_cc();
4769 set_cc_op(s, CC_OP_LOGICB + ot);
4770 break;
4771 case 2: /* not */
4772 if (s->prefix & PREFIX_LOCK) {
4773 if (mod == 3) {
4774 goto illegal_op;
4776 tcg_gen_movi_tl(cpu_T0, ~0);
4777 tcg_gen_atomic_xor_fetch_tl(cpu_T0, cpu_A0, cpu_T0,
4778 s->mem_index, ot | MO_LE);
4779 } else {
4780 tcg_gen_not_tl(cpu_T0, cpu_T0);
4781 if (mod != 3) {
4782 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
4783 } else {
4784 gen_op_mov_reg_v(ot, rm, cpu_T0);
4787 break;
4788 case 3: /* neg */
4789 if (s->prefix & PREFIX_LOCK) {
4790 TCGLabel *label1;
4791 TCGv a0, t0, t1, t2;
4793 if (mod == 3) {
4794 goto illegal_op;
4796 a0 = tcg_temp_local_new();
4797 t0 = tcg_temp_local_new();
4798 label1 = gen_new_label();
4800 tcg_gen_mov_tl(a0, cpu_A0);
4801 tcg_gen_mov_tl(t0, cpu_T0);
4803 gen_set_label(label1);
4804 t1 = tcg_temp_new();
4805 t2 = tcg_temp_new();
4806 tcg_gen_mov_tl(t2, t0);
4807 tcg_gen_neg_tl(t1, t0);
4808 tcg_gen_atomic_cmpxchg_tl(t0, a0, t0, t1,
4809 s->mem_index, ot | MO_LE);
4810 tcg_temp_free(t1);
4811 tcg_gen_brcond_tl(TCG_COND_NE, t0, t2, label1);
4813 tcg_temp_free(t2);
4814 tcg_temp_free(a0);
4815 tcg_gen_mov_tl(cpu_T0, t0);
4816 tcg_temp_free(t0);
4817 } else {
4818 tcg_gen_neg_tl(cpu_T0, cpu_T0);
4819 if (mod != 3) {
4820 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
4821 } else {
4822 gen_op_mov_reg_v(ot, rm, cpu_T0);
4825 gen_op_update_neg_cc();
4826 set_cc_op(s, CC_OP_SUBB + ot);
4827 break;
4828 case 4: /* mul */
4829 switch(ot) {
4830 case MO_8:
4831 gen_op_mov_v_reg(MO_8, cpu_T1, R_EAX);
4832 tcg_gen_ext8u_tl(cpu_T0, cpu_T0);
4833 tcg_gen_ext8u_tl(cpu_T1, cpu_T1);
4834 /* XXX: use 32 bit mul which could be faster */
4835 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
4836 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
4837 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4838 tcg_gen_andi_tl(cpu_cc_src, cpu_T0, 0xff00);
4839 set_cc_op(s, CC_OP_MULB);
4840 break;
4841 case MO_16:
4842 gen_op_mov_v_reg(MO_16, cpu_T1, R_EAX);
4843 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
4844 tcg_gen_ext16u_tl(cpu_T1, cpu_T1);
4845 /* XXX: use 32 bit mul which could be faster */
4846 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
4847 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
4848 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4849 tcg_gen_shri_tl(cpu_T0, cpu_T0, 16);
4850 gen_op_mov_reg_v(MO_16, R_EDX, cpu_T0);
4851 tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
4852 set_cc_op(s, CC_OP_MULW);
4853 break;
4854 default:
4855 case MO_32:
4856 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
4857 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
4858 tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
4859 cpu_tmp2_i32, cpu_tmp3_i32);
4860 tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
4861 tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
4862 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4863 tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
4864 set_cc_op(s, CC_OP_MULL);
4865 break;
4866 #ifdef TARGET_X86_64
4867 case MO_64:
4868 tcg_gen_mulu2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
4869 cpu_T0, cpu_regs[R_EAX]);
4870 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4871 tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
4872 set_cc_op(s, CC_OP_MULQ);
4873 break;
4874 #endif
4876 break;
4877 case 5: /* imul */
4878 switch(ot) {
4879 case MO_8:
4880 gen_op_mov_v_reg(MO_8, cpu_T1, R_EAX);
4881 tcg_gen_ext8s_tl(cpu_T0, cpu_T0);
4882 tcg_gen_ext8s_tl(cpu_T1, cpu_T1);
4883 /* XXX: use 32 bit mul which could be faster */
4884 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
4885 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
4886 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4887 tcg_gen_ext8s_tl(cpu_tmp0, cpu_T0);
4888 tcg_gen_sub_tl(cpu_cc_src, cpu_T0, cpu_tmp0);
4889 set_cc_op(s, CC_OP_MULB);
4890 break;
4891 case MO_16:
4892 gen_op_mov_v_reg(MO_16, cpu_T1, R_EAX);
4893 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
4894 tcg_gen_ext16s_tl(cpu_T1, cpu_T1);
4895 /* XXX: use 32 bit mul which could be faster */
4896 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
4897 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
4898 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4899 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T0);
4900 tcg_gen_sub_tl(cpu_cc_src, cpu_T0, cpu_tmp0);
4901 tcg_gen_shri_tl(cpu_T0, cpu_T0, 16);
4902 gen_op_mov_reg_v(MO_16, R_EDX, cpu_T0);
4903 set_cc_op(s, CC_OP_MULW);
4904 break;
4905 default:
4906 case MO_32:
4907 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
4908 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
4909 tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
4910 cpu_tmp2_i32, cpu_tmp3_i32);
4911 tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
4912 tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
4913 tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
4914 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4915 tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
4916 tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
4917 set_cc_op(s, CC_OP_MULL);
4918 break;
4919 #ifdef TARGET_X86_64
4920 case MO_64:
4921 tcg_gen_muls2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
4922 cpu_T0, cpu_regs[R_EAX]);
4923 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4924 tcg_gen_sari_tl(cpu_cc_src, cpu_regs[R_EAX], 63);
4925 tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_regs[R_EDX]);
4926 set_cc_op(s, CC_OP_MULQ);
4927 break;
4928 #endif
4930 break;
4931 case 6: /* div */
4932 switch(ot) {
4933 case MO_8:
4934 gen_helper_divb_AL(cpu_env, cpu_T0);
4935 break;
4936 case MO_16:
4937 gen_helper_divw_AX(cpu_env, cpu_T0);
4938 break;
4939 default:
4940 case MO_32:
4941 gen_helper_divl_EAX(cpu_env, cpu_T0);
4942 break;
4943 #ifdef TARGET_X86_64
4944 case MO_64:
4945 gen_helper_divq_EAX(cpu_env, cpu_T0);
4946 break;
4947 #endif
4949 break;
4950 case 7: /* idiv */
4951 switch(ot) {
4952 case MO_8:
4953 gen_helper_idivb_AL(cpu_env, cpu_T0);
4954 break;
4955 case MO_16:
4956 gen_helper_idivw_AX(cpu_env, cpu_T0);
4957 break;
4958 default:
4959 case MO_32:
4960 gen_helper_idivl_EAX(cpu_env, cpu_T0);
4961 break;
4962 #ifdef TARGET_X86_64
4963 case MO_64:
4964 gen_helper_idivq_EAX(cpu_env, cpu_T0);
4965 break;
4966 #endif
4968 break;
4969 default:
4970 goto unknown_op;
4972 break;
4974 case 0xfe: /* GRP4 */
4975 case 0xff: /* GRP5 */
4976 ot = mo_b_d(b, dflag);
4978 modrm = x86_ldub_code(env, s);
4979 mod = (modrm >> 6) & 3;
4980 rm = (modrm & 7) | REX_B(s);
4981 op = (modrm >> 3) & 7;
4982 if (op >= 2 && b == 0xfe) {
4983 goto unknown_op;
4985 if (CODE64(s)) {
4986 if (op == 2 || op == 4) {
4987 /* operand size for jumps is 64 bit */
4988 ot = MO_64;
4989 } else if (op == 3 || op == 5) {
4990 ot = dflag != MO_16 ? MO_32 + (rex_w == 1) : MO_16;
4991 } else if (op == 6) {
4992 /* default push size is 64 bit */
4993 ot = mo_pushpop(s, dflag);
4996 if (mod != 3) {
4997 gen_lea_modrm(env, s, modrm);
4998 if (op >= 2 && op != 3 && op != 5)
4999 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
5000 } else {
5001 gen_op_mov_v_reg(ot, cpu_T0, rm);
5004 switch(op) {
5005 case 0: /* inc Ev */
5006 if (mod != 3)
5007 opreg = OR_TMP0;
5008 else
5009 opreg = rm;
5010 gen_inc(s, ot, opreg, 1);
5011 break;
5012 case 1: /* dec Ev */
5013 if (mod != 3)
5014 opreg = OR_TMP0;
5015 else
5016 opreg = rm;
5017 gen_inc(s, ot, opreg, -1);
5018 break;
5019 case 2: /* call Ev */
5020 /* XXX: optimize if memory (no 'and' is necessary) */
5021 if (dflag == MO_16) {
5022 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
5024 next_eip = s->pc - s->cs_base;
5025 tcg_gen_movi_tl(cpu_T1, next_eip);
5026 gen_push_v(s, cpu_T1);
5027 gen_op_jmp_v(cpu_T0);
5028 gen_bnd_jmp(s);
5029 gen_jr(s, cpu_T0);
5030 break;
5031 case 3: /* lcall Ev */
5032 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
5033 gen_add_A0_im(s, 1 << ot);
5034 gen_op_ld_v(s, MO_16, cpu_T0, cpu_A0);
5035 do_lcall:
5036 if (s->pe && !s->vm86) {
5037 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
5038 gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T1,
5039 tcg_const_i32(dflag - 1),
5040 tcg_const_tl(s->pc - s->cs_base));
5041 } else {
5042 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
5043 gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T1,
5044 tcg_const_i32(dflag - 1),
5045 tcg_const_i32(s->pc - s->cs_base));
5047 tcg_gen_ld_tl(cpu_tmp4, cpu_env, offsetof(CPUX86State, eip));
5048 gen_jr(s, cpu_tmp4);
5049 break;
5050 case 4: /* jmp Ev */
5051 if (dflag == MO_16) {
5052 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
5054 gen_op_jmp_v(cpu_T0);
5055 gen_bnd_jmp(s);
5056 gen_jr(s, cpu_T0);
5057 break;
5058 case 5: /* ljmp Ev */
5059 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
5060 gen_add_A0_im(s, 1 << ot);
5061 gen_op_ld_v(s, MO_16, cpu_T0, cpu_A0);
5062 do_ljmp:
5063 if (s->pe && !s->vm86) {
5064 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
5065 gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T1,
5066 tcg_const_tl(s->pc - s->cs_base));
5067 } else {
5068 gen_op_movl_seg_T0_vm(R_CS);
5069 gen_op_jmp_v(cpu_T1);
5071 tcg_gen_ld_tl(cpu_tmp4, cpu_env, offsetof(CPUX86State, eip));
5072 gen_jr(s, cpu_tmp4);
5073 break;
5074 case 6: /* push Ev */
5075 gen_push_v(s, cpu_T0);
5076 break;
5077 default:
5078 goto unknown_op;
5080 break;
5082 case 0x84: /* test Ev, Gv */
5083 case 0x85:
5084 ot = mo_b_d(b, dflag);
5086 modrm = x86_ldub_code(env, s);
5087 reg = ((modrm >> 3) & 7) | rex_r;
5089 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
5090 gen_op_mov_v_reg(ot, cpu_T1, reg);
5091 gen_op_testl_T0_T1_cc();
5092 set_cc_op(s, CC_OP_LOGICB + ot);
5093 break;
5095 case 0xa8: /* test eAX, Iv */
5096 case 0xa9:
5097 ot = mo_b_d(b, dflag);
5098 val = insn_get(env, s, ot);
5100 gen_op_mov_v_reg(ot, cpu_T0, OR_EAX);
5101 tcg_gen_movi_tl(cpu_T1, val);
5102 gen_op_testl_T0_T1_cc();
5103 set_cc_op(s, CC_OP_LOGICB + ot);
5104 break;
5106 case 0x98: /* CWDE/CBW */
5107 switch (dflag) {
5108 #ifdef TARGET_X86_64
5109 case MO_64:
5110 gen_op_mov_v_reg(MO_32, cpu_T0, R_EAX);
5111 tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
5112 gen_op_mov_reg_v(MO_64, R_EAX, cpu_T0);
5113 break;
5114 #endif
5115 case MO_32:
5116 gen_op_mov_v_reg(MO_16, cpu_T0, R_EAX);
5117 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
5118 gen_op_mov_reg_v(MO_32, R_EAX, cpu_T0);
5119 break;
5120 case MO_16:
5121 gen_op_mov_v_reg(MO_8, cpu_T0, R_EAX);
5122 tcg_gen_ext8s_tl(cpu_T0, cpu_T0);
5123 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
5124 break;
5125 default:
5126 tcg_abort();
5128 break;
5129 case 0x99: /* CDQ/CWD */
5130 switch (dflag) {
5131 #ifdef TARGET_X86_64
5132 case MO_64:
5133 gen_op_mov_v_reg(MO_64, cpu_T0, R_EAX);
5134 tcg_gen_sari_tl(cpu_T0, cpu_T0, 63);
5135 gen_op_mov_reg_v(MO_64, R_EDX, cpu_T0);
5136 break;
5137 #endif
5138 case MO_32:
5139 gen_op_mov_v_reg(MO_32, cpu_T0, R_EAX);
5140 tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
5141 tcg_gen_sari_tl(cpu_T0, cpu_T0, 31);
5142 gen_op_mov_reg_v(MO_32, R_EDX, cpu_T0);
5143 break;
5144 case MO_16:
5145 gen_op_mov_v_reg(MO_16, cpu_T0, R_EAX);
5146 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
5147 tcg_gen_sari_tl(cpu_T0, cpu_T0, 15);
5148 gen_op_mov_reg_v(MO_16, R_EDX, cpu_T0);
5149 break;
5150 default:
5151 tcg_abort();
5153 break;
5154 case 0x1af: /* imul Gv, Ev */
5155 case 0x69: /* imul Gv, Ev, I */
5156 case 0x6b:
5157 ot = dflag;
5158 modrm = x86_ldub_code(env, s);
5159 reg = ((modrm >> 3) & 7) | rex_r;
5160 if (b == 0x69)
5161 s->rip_offset = insn_const_size(ot);
5162 else if (b == 0x6b)
5163 s->rip_offset = 1;
5164 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
5165 if (b == 0x69) {
5166 val = insn_get(env, s, ot);
5167 tcg_gen_movi_tl(cpu_T1, val);
5168 } else if (b == 0x6b) {
5169 val = (int8_t)insn_get(env, s, MO_8);
5170 tcg_gen_movi_tl(cpu_T1, val);
5171 } else {
5172 gen_op_mov_v_reg(ot, cpu_T1, reg);
5174 switch (ot) {
5175 #ifdef TARGET_X86_64
5176 case MO_64:
5177 tcg_gen_muls2_i64(cpu_regs[reg], cpu_T1, cpu_T0, cpu_T1);
5178 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
5179 tcg_gen_sari_tl(cpu_cc_src, cpu_cc_dst, 63);
5180 tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_T1);
5181 break;
5182 #endif
5183 case MO_32:
5184 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
5185 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
5186 tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
5187 cpu_tmp2_i32, cpu_tmp3_i32);
5188 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
5189 tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
5190 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
5191 tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
5192 tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
5193 break;
5194 default:
5195 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
5196 tcg_gen_ext16s_tl(cpu_T1, cpu_T1);
5197 /* XXX: use 32 bit mul which could be faster */
5198 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
5199 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
5200 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T0);
5201 tcg_gen_sub_tl(cpu_cc_src, cpu_T0, cpu_tmp0);
5202 gen_op_mov_reg_v(ot, reg, cpu_T0);
5203 break;
5205 set_cc_op(s, CC_OP_MULB + ot);
5206 break;
5207 case 0x1c0:
5208 case 0x1c1: /* xadd Ev, Gv */
5209 ot = mo_b_d(b, dflag);
5210 modrm = x86_ldub_code(env, s);
5211 reg = ((modrm >> 3) & 7) | rex_r;
5212 mod = (modrm >> 6) & 3;
5213 gen_op_mov_v_reg(ot, cpu_T0, reg);
5214 if (mod == 3) {
5215 rm = (modrm & 7) | REX_B(s);
5216 gen_op_mov_v_reg(ot, cpu_T1, rm);
5217 tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
5218 gen_op_mov_reg_v(ot, reg, cpu_T1);
5219 gen_op_mov_reg_v(ot, rm, cpu_T0);
5220 } else {
5221 gen_lea_modrm(env, s, modrm);
5222 if (s->prefix & PREFIX_LOCK) {
5223 tcg_gen_atomic_fetch_add_tl(cpu_T1, cpu_A0, cpu_T0,
5224 s->mem_index, ot | MO_LE);
5225 tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
5226 } else {
5227 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
5228 tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
5229 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
5231 gen_op_mov_reg_v(ot, reg, cpu_T1);
5233 gen_op_update2_cc();
5234 set_cc_op(s, CC_OP_ADDB + ot);
5235 break;
5236 case 0x1b0:
5237 case 0x1b1: /* cmpxchg Ev, Gv */
5239 TCGv oldv, newv, cmpv;
5241 ot = mo_b_d(b, dflag);
5242 modrm = x86_ldub_code(env, s);
5243 reg = ((modrm >> 3) & 7) | rex_r;
5244 mod = (modrm >> 6) & 3;
5245 oldv = tcg_temp_new();
5246 newv = tcg_temp_new();
5247 cmpv = tcg_temp_new();
5248 gen_op_mov_v_reg(ot, newv, reg);
5249 tcg_gen_mov_tl(cmpv, cpu_regs[R_EAX]);
5251 if (s->prefix & PREFIX_LOCK) {
5252 if (mod == 3) {
5253 goto illegal_op;
5255 gen_lea_modrm(env, s, modrm);
5256 tcg_gen_atomic_cmpxchg_tl(oldv, cpu_A0, cmpv, newv,
5257 s->mem_index, ot | MO_LE);
5258 gen_op_mov_reg_v(ot, R_EAX, oldv);
5259 } else {
5260 if (mod == 3) {
5261 rm = (modrm & 7) | REX_B(s);
5262 gen_op_mov_v_reg(ot, oldv, rm);
5263 } else {
5264 gen_lea_modrm(env, s, modrm);
5265 gen_op_ld_v(s, ot, oldv, cpu_A0);
5266 rm = 0; /* avoid warning */
5268 gen_extu(ot, oldv);
5269 gen_extu(ot, cmpv);
5270 /* store value = (old == cmp ? new : old); */
5271 tcg_gen_movcond_tl(TCG_COND_EQ, newv, oldv, cmpv, newv, oldv);
5272 if (mod == 3) {
5273 gen_op_mov_reg_v(ot, R_EAX, oldv);
5274 gen_op_mov_reg_v(ot, rm, newv);
5275 } else {
5276 /* Perform an unconditional store cycle like physical cpu;
5277 must be before changing accumulator to ensure
5278 idempotency if the store faults and the instruction
5279 is restarted */
5280 gen_op_st_v(s, ot, newv, cpu_A0);
5281 gen_op_mov_reg_v(ot, R_EAX, oldv);
5284 tcg_gen_mov_tl(cpu_cc_src, oldv);
5285 tcg_gen_mov_tl(cpu_cc_srcT, cmpv);
5286 tcg_gen_sub_tl(cpu_cc_dst, cmpv, oldv);
5287 set_cc_op(s, CC_OP_SUBB + ot);
5288 tcg_temp_free(oldv);
5289 tcg_temp_free(newv);
5290 tcg_temp_free(cmpv);
5292 break;
5293 case 0x1c7: /* cmpxchg8b */
5294 modrm = x86_ldub_code(env, s);
5295 mod = (modrm >> 6) & 3;
5296 if ((mod == 3) || ((modrm & 0x38) != 0x8))
5297 goto illegal_op;
5298 #ifdef TARGET_X86_64
5299 if (dflag == MO_64) {
5300 if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
5301 goto illegal_op;
5302 gen_lea_modrm(env, s, modrm);
5303 if ((s->prefix & PREFIX_LOCK) && (tb_cflags(s->base.tb) & CF_PARALLEL)) {
5304 gen_helper_cmpxchg16b(cpu_env, cpu_A0);
5305 } else {
5306 gen_helper_cmpxchg16b_unlocked(cpu_env, cpu_A0);
5308 } else
5309 #endif
5311 if (!(s->cpuid_features & CPUID_CX8))
5312 goto illegal_op;
5313 gen_lea_modrm(env, s, modrm);
5314 if ((s->prefix & PREFIX_LOCK) && (tb_cflags(s->base.tb) & CF_PARALLEL)) {
5315 gen_helper_cmpxchg8b(cpu_env, cpu_A0);
5316 } else {
5317 gen_helper_cmpxchg8b_unlocked(cpu_env, cpu_A0);
5320 set_cc_op(s, CC_OP_EFLAGS);
5321 break;
5323 /**************************/
5324 /* push/pop */
5325 case 0x50 ... 0x57: /* push */
5326 gen_op_mov_v_reg(MO_32, cpu_T0, (b & 7) | REX_B(s));
5327 gen_push_v(s, cpu_T0);
5328 break;
5329 case 0x58 ... 0x5f: /* pop */
5330 ot = gen_pop_T0(s);
5331 /* NOTE: order is important for pop %sp */
5332 gen_pop_update(s, ot);
5333 gen_op_mov_reg_v(ot, (b & 7) | REX_B(s), cpu_T0);
5334 break;
5335 case 0x60: /* pusha */
5336 if (CODE64(s))
5337 goto illegal_op;
5338 gen_pusha(s);
5339 break;
5340 case 0x61: /* popa */
5341 if (CODE64(s))
5342 goto illegal_op;
5343 gen_popa(s);
5344 break;
5345 case 0x68: /* push Iv */
5346 case 0x6a:
5347 ot = mo_pushpop(s, dflag);
5348 if (b == 0x68)
5349 val = insn_get(env, s, ot);
5350 else
5351 val = (int8_t)insn_get(env, s, MO_8);
5352 tcg_gen_movi_tl(cpu_T0, val);
5353 gen_push_v(s, cpu_T0);
5354 break;
5355 case 0x8f: /* pop Ev */
5356 modrm = x86_ldub_code(env, s);
5357 mod = (modrm >> 6) & 3;
5358 ot = gen_pop_T0(s);
5359 if (mod == 3) {
5360 /* NOTE: order is important for pop %sp */
5361 gen_pop_update(s, ot);
5362 rm = (modrm & 7) | REX_B(s);
5363 gen_op_mov_reg_v(ot, rm, cpu_T0);
5364 } else {
5365 /* NOTE: order is important too for MMU exceptions */
5366 s->popl_esp_hack = 1 << ot;
5367 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
5368 s->popl_esp_hack = 0;
5369 gen_pop_update(s, ot);
5371 break;
5372 case 0xc8: /* enter */
5374 int level;
5375 val = x86_lduw_code(env, s);
5376 level = x86_ldub_code(env, s);
5377 gen_enter(s, val, level);
5379 break;
5380 case 0xc9: /* leave */
5381 gen_leave(s);
5382 break;
5383 case 0x06: /* push es */
5384 case 0x0e: /* push cs */
5385 case 0x16: /* push ss */
5386 case 0x1e: /* push ds */
5387 if (CODE64(s))
5388 goto illegal_op;
5389 gen_op_movl_T0_seg(b >> 3);
5390 gen_push_v(s, cpu_T0);
5391 break;
5392 case 0x1a0: /* push fs */
5393 case 0x1a8: /* push gs */
5394 gen_op_movl_T0_seg((b >> 3) & 7);
5395 gen_push_v(s, cpu_T0);
5396 break;
5397 case 0x07: /* pop es */
5398 case 0x17: /* pop ss */
5399 case 0x1f: /* pop ds */
5400 if (CODE64(s))
5401 goto illegal_op;
5402 reg = b >> 3;
5403 ot = gen_pop_T0(s);
5404 gen_movl_seg_T0(s, reg);
5405 gen_pop_update(s, ot);
5406 /* Note that reg == R_SS in gen_movl_seg_T0 always sets is_jmp. */
5407 if (s->base.is_jmp) {
5408 gen_jmp_im(s->pc - s->cs_base);
5409 if (reg == R_SS) {
5410 s->tf = 0;
5411 gen_eob_inhibit_irq(s, true);
5412 } else {
5413 gen_eob(s);
5416 break;
5417 case 0x1a1: /* pop fs */
5418 case 0x1a9: /* pop gs */
5419 ot = gen_pop_T0(s);
5420 gen_movl_seg_T0(s, (b >> 3) & 7);
5421 gen_pop_update(s, ot);
5422 if (s->base.is_jmp) {
5423 gen_jmp_im(s->pc - s->cs_base);
5424 gen_eob(s);
5426 break;
5428 /**************************/
5429 /* mov */
5430 case 0x88:
5431 case 0x89: /* mov Gv, Ev */
5432 ot = mo_b_d(b, dflag);
5433 modrm = x86_ldub_code(env, s);
5434 reg = ((modrm >> 3) & 7) | rex_r;
5436 /* generate a generic store */
5437 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
5438 break;
5439 case 0xc6:
5440 case 0xc7: /* mov Ev, Iv */
5441 ot = mo_b_d(b, dflag);
5442 modrm = x86_ldub_code(env, s);
5443 mod = (modrm >> 6) & 3;
5444 if (mod != 3) {
5445 s->rip_offset = insn_const_size(ot);
5446 gen_lea_modrm(env, s, modrm);
5448 val = insn_get(env, s, ot);
5449 tcg_gen_movi_tl(cpu_T0, val);
5450 if (mod != 3) {
5451 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
5452 } else {
5453 gen_op_mov_reg_v(ot, (modrm & 7) | REX_B(s), cpu_T0);
5455 break;
5456 case 0x8a:
5457 case 0x8b: /* mov Ev, Gv */
5458 ot = mo_b_d(b, dflag);
5459 modrm = x86_ldub_code(env, s);
5460 reg = ((modrm >> 3) & 7) | rex_r;
5462 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
5463 gen_op_mov_reg_v(ot, reg, cpu_T0);
5464 break;
5465 case 0x8e: /* mov seg, Gv */
5466 modrm = x86_ldub_code(env, s);
5467 reg = (modrm >> 3) & 7;
5468 if (reg >= 6 || reg == R_CS)
5469 goto illegal_op;
5470 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
5471 gen_movl_seg_T0(s, reg);
5472 /* Note that reg == R_SS in gen_movl_seg_T0 always sets is_jmp. */
5473 if (s->base.is_jmp) {
5474 gen_jmp_im(s->pc - s->cs_base);
5475 if (reg == R_SS) {
5476 s->tf = 0;
5477 gen_eob_inhibit_irq(s, true);
5478 } else {
5479 gen_eob(s);
5482 break;
5483 case 0x8c: /* mov Gv, seg */
5484 modrm = x86_ldub_code(env, s);
5485 reg = (modrm >> 3) & 7;
5486 mod = (modrm >> 6) & 3;
5487 if (reg >= 6)
5488 goto illegal_op;
5489 gen_op_movl_T0_seg(reg);
5490 ot = mod == 3 ? dflag : MO_16;
5491 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
5492 break;
5494 case 0x1b6: /* movzbS Gv, Eb */
5495 case 0x1b7: /* movzwS Gv, Eb */
5496 case 0x1be: /* movsbS Gv, Eb */
5497 case 0x1bf: /* movswS Gv, Eb */
5499 TCGMemOp d_ot;
5500 TCGMemOp s_ot;
5502 /* d_ot is the size of destination */
5503 d_ot = dflag;
5504 /* ot is the size of source */
5505 ot = (b & 1) + MO_8;
5506 /* s_ot is the sign+size of source */
5507 s_ot = b & 8 ? MO_SIGN | ot : ot;
5509 modrm = x86_ldub_code(env, s);
5510 reg = ((modrm >> 3) & 7) | rex_r;
5511 mod = (modrm >> 6) & 3;
5512 rm = (modrm & 7) | REX_B(s);
5514 if (mod == 3) {
5515 if (s_ot == MO_SB && byte_reg_is_xH(rm)) {
5516 tcg_gen_sextract_tl(cpu_T0, cpu_regs[rm - 4], 8, 8);
5517 } else {
5518 gen_op_mov_v_reg(ot, cpu_T0, rm);
5519 switch (s_ot) {
5520 case MO_UB:
5521 tcg_gen_ext8u_tl(cpu_T0, cpu_T0);
5522 break;
5523 case MO_SB:
5524 tcg_gen_ext8s_tl(cpu_T0, cpu_T0);
5525 break;
5526 case MO_UW:
5527 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
5528 break;
5529 default:
5530 case MO_SW:
5531 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
5532 break;
5535 gen_op_mov_reg_v(d_ot, reg, cpu_T0);
5536 } else {
5537 gen_lea_modrm(env, s, modrm);
5538 gen_op_ld_v(s, s_ot, cpu_T0, cpu_A0);
5539 gen_op_mov_reg_v(d_ot, reg, cpu_T0);
5542 break;
5544 case 0x8d: /* lea */
5545 modrm = x86_ldub_code(env, s);
5546 mod = (modrm >> 6) & 3;
5547 if (mod == 3)
5548 goto illegal_op;
5549 reg = ((modrm >> 3) & 7) | rex_r;
5551 AddressParts a = gen_lea_modrm_0(env, s, modrm);
5552 TCGv ea = gen_lea_modrm_1(a);
5553 gen_lea_v_seg(s, s->aflag, ea, -1, -1);
5554 gen_op_mov_reg_v(dflag, reg, cpu_A0);
5556 break;
5558 case 0xa0: /* mov EAX, Ov */
5559 case 0xa1:
5560 case 0xa2: /* mov Ov, EAX */
5561 case 0xa3:
5563 target_ulong offset_addr;
5565 ot = mo_b_d(b, dflag);
5566 switch (s->aflag) {
5567 #ifdef TARGET_X86_64
5568 case MO_64:
5569 offset_addr = x86_ldq_code(env, s);
5570 break;
5571 #endif
5572 default:
5573 offset_addr = insn_get(env, s, s->aflag);
5574 break;
5576 tcg_gen_movi_tl(cpu_A0, offset_addr);
5577 gen_add_A0_ds_seg(s);
5578 if ((b & 2) == 0) {
5579 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
5580 gen_op_mov_reg_v(ot, R_EAX, cpu_T0);
5581 } else {
5582 gen_op_mov_v_reg(ot, cpu_T0, R_EAX);
5583 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
5586 break;
5587 case 0xd7: /* xlat */
5588 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EBX]);
5589 tcg_gen_ext8u_tl(cpu_T0, cpu_regs[R_EAX]);
5590 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T0);
5591 gen_extu(s->aflag, cpu_A0);
5592 gen_add_A0_ds_seg(s);
5593 gen_op_ld_v(s, MO_8, cpu_T0, cpu_A0);
5594 gen_op_mov_reg_v(MO_8, R_EAX, cpu_T0);
5595 break;
5596 case 0xb0 ... 0xb7: /* mov R, Ib */
5597 val = insn_get(env, s, MO_8);
5598 tcg_gen_movi_tl(cpu_T0, val);
5599 gen_op_mov_reg_v(MO_8, (b & 7) | REX_B(s), cpu_T0);
5600 break;
5601 case 0xb8 ... 0xbf: /* mov R, Iv */
5602 #ifdef TARGET_X86_64
5603 if (dflag == MO_64) {
5604 uint64_t tmp;
5605 /* 64 bit case */
5606 tmp = x86_ldq_code(env, s);
5607 reg = (b & 7) | REX_B(s);
5608 tcg_gen_movi_tl(cpu_T0, tmp);
5609 gen_op_mov_reg_v(MO_64, reg, cpu_T0);
5610 } else
5611 #endif
5613 ot = dflag;
5614 val = insn_get(env, s, ot);
5615 reg = (b & 7) | REX_B(s);
5616 tcg_gen_movi_tl(cpu_T0, val);
5617 gen_op_mov_reg_v(ot, reg, cpu_T0);
5619 break;
5621 case 0x91 ... 0x97: /* xchg R, EAX */
5622 do_xchg_reg_eax:
5623 ot = dflag;
5624 reg = (b & 7) | REX_B(s);
5625 rm = R_EAX;
5626 goto do_xchg_reg;
5627 case 0x86:
5628 case 0x87: /* xchg Ev, Gv */
5629 ot = mo_b_d(b, dflag);
5630 modrm = x86_ldub_code(env, s);
5631 reg = ((modrm >> 3) & 7) | rex_r;
5632 mod = (modrm >> 6) & 3;
5633 if (mod == 3) {
5634 rm = (modrm & 7) | REX_B(s);
5635 do_xchg_reg:
5636 gen_op_mov_v_reg(ot, cpu_T0, reg);
5637 gen_op_mov_v_reg(ot, cpu_T1, rm);
5638 gen_op_mov_reg_v(ot, rm, cpu_T0);
5639 gen_op_mov_reg_v(ot, reg, cpu_T1);
5640 } else {
5641 gen_lea_modrm(env, s, modrm);
5642 gen_op_mov_v_reg(ot, cpu_T0, reg);
5643 /* for xchg, lock is implicit */
5644 tcg_gen_atomic_xchg_tl(cpu_T1, cpu_A0, cpu_T0,
5645 s->mem_index, ot | MO_LE);
5646 gen_op_mov_reg_v(ot, reg, cpu_T1);
5648 break;
5649 case 0xc4: /* les Gv */
5650 /* In CODE64 this is VEX3; see above. */
5651 op = R_ES;
5652 goto do_lxx;
5653 case 0xc5: /* lds Gv */
5654 /* In CODE64 this is VEX2; see above. */
5655 op = R_DS;
5656 goto do_lxx;
5657 case 0x1b2: /* lss Gv */
5658 op = R_SS;
5659 goto do_lxx;
5660 case 0x1b4: /* lfs Gv */
5661 op = R_FS;
5662 goto do_lxx;
5663 case 0x1b5: /* lgs Gv */
5664 op = R_GS;
5665 do_lxx:
5666 ot = dflag != MO_16 ? MO_32 : MO_16;
5667 modrm = x86_ldub_code(env, s);
5668 reg = ((modrm >> 3) & 7) | rex_r;
5669 mod = (modrm >> 6) & 3;
5670 if (mod == 3)
5671 goto illegal_op;
5672 gen_lea_modrm(env, s, modrm);
5673 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
5674 gen_add_A0_im(s, 1 << ot);
5675 /* load the segment first to handle exceptions properly */
5676 gen_op_ld_v(s, MO_16, cpu_T0, cpu_A0);
5677 gen_movl_seg_T0(s, op);
5678 /* then put the data */
5679 gen_op_mov_reg_v(ot, reg, cpu_T1);
5680 if (s->base.is_jmp) {
5681 gen_jmp_im(s->pc - s->cs_base);
5682 gen_eob(s);
5684 break;
5686 /************************/
5687 /* shifts */
5688 case 0xc0:
5689 case 0xc1:
5690 /* shift Ev,Ib */
5691 shift = 2;
5692 grp2:
5694 ot = mo_b_d(b, dflag);
5695 modrm = x86_ldub_code(env, s);
5696 mod = (modrm >> 6) & 3;
5697 op = (modrm >> 3) & 7;
5699 if (mod != 3) {
5700 if (shift == 2) {
5701 s->rip_offset = 1;
5703 gen_lea_modrm(env, s, modrm);
5704 opreg = OR_TMP0;
5705 } else {
5706 opreg = (modrm & 7) | REX_B(s);
5709 /* simpler op */
5710 if (shift == 0) {
5711 gen_shift(s, op, ot, opreg, OR_ECX);
5712 } else {
5713 if (shift == 2) {
5714 shift = x86_ldub_code(env, s);
5716 gen_shifti(s, op, ot, opreg, shift);
5719 break;
5720 case 0xd0:
5721 case 0xd1:
5722 /* shift Ev,1 */
5723 shift = 1;
5724 goto grp2;
5725 case 0xd2:
5726 case 0xd3:
5727 /* shift Ev,cl */
5728 shift = 0;
5729 goto grp2;
5731 case 0x1a4: /* shld imm */
5732 op = 0;
5733 shift = 1;
5734 goto do_shiftd;
5735 case 0x1a5: /* shld cl */
5736 op = 0;
5737 shift = 0;
5738 goto do_shiftd;
5739 case 0x1ac: /* shrd imm */
5740 op = 1;
5741 shift = 1;
5742 goto do_shiftd;
5743 case 0x1ad: /* shrd cl */
5744 op = 1;
5745 shift = 0;
5746 do_shiftd:
5747 ot = dflag;
5748 modrm = x86_ldub_code(env, s);
5749 mod = (modrm >> 6) & 3;
5750 rm = (modrm & 7) | REX_B(s);
5751 reg = ((modrm >> 3) & 7) | rex_r;
5752 if (mod != 3) {
5753 gen_lea_modrm(env, s, modrm);
5754 opreg = OR_TMP0;
5755 } else {
5756 opreg = rm;
5758 gen_op_mov_v_reg(ot, cpu_T1, reg);
5760 if (shift) {
5761 TCGv imm = tcg_const_tl(x86_ldub_code(env, s));
5762 gen_shiftd_rm_T1(s, ot, opreg, op, imm);
5763 tcg_temp_free(imm);
5764 } else {
5765 gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
5767 break;
5769 /************************/
5770 /* floats */
5771 case 0xd8 ... 0xdf:
5772 if (s->flags & (HF_EM_MASK | HF_TS_MASK)) {
5773 /* if CR0.EM or CR0.TS are set, generate an FPU exception */
5774 /* XXX: what to do if illegal op ? */
5775 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
5776 break;
5778 modrm = x86_ldub_code(env, s);
5779 mod = (modrm >> 6) & 3;
5780 rm = modrm & 7;
5781 op = ((b & 7) << 3) | ((modrm >> 3) & 7);
5782 if (mod != 3) {
5783 /* memory op */
5784 gen_lea_modrm(env, s, modrm);
5785 switch(op) {
5786 case 0x00 ... 0x07: /* fxxxs */
5787 case 0x10 ... 0x17: /* fixxxl */
5788 case 0x20 ... 0x27: /* fxxxl */
5789 case 0x30 ... 0x37: /* fixxx */
5791 int op1;
5792 op1 = op & 7;
5794 switch(op >> 4) {
5795 case 0:
5796 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5797 s->mem_index, MO_LEUL);
5798 gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
5799 break;
5800 case 1:
5801 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5802 s->mem_index, MO_LEUL);
5803 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
5804 break;
5805 case 2:
5806 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
5807 s->mem_index, MO_LEQ);
5808 gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
5809 break;
5810 case 3:
5811 default:
5812 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5813 s->mem_index, MO_LESW);
5814 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
5815 break;
5818 gen_helper_fp_arith_ST0_FT0(op1);
5819 if (op1 == 3) {
5820 /* fcomp needs pop */
5821 gen_helper_fpop(cpu_env);
5824 break;
5825 case 0x08: /* flds */
5826 case 0x0a: /* fsts */
5827 case 0x0b: /* fstps */
5828 case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
5829 case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
5830 case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
5831 switch(op & 7) {
5832 case 0:
5833 switch(op >> 4) {
5834 case 0:
5835 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5836 s->mem_index, MO_LEUL);
5837 gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
5838 break;
5839 case 1:
5840 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5841 s->mem_index, MO_LEUL);
5842 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
5843 break;
5844 case 2:
5845 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
5846 s->mem_index, MO_LEQ);
5847 gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
5848 break;
5849 case 3:
5850 default:
5851 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5852 s->mem_index, MO_LESW);
5853 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
5854 break;
5856 break;
5857 case 1:
5858 /* XXX: the corresponding CPUID bit must be tested ! */
5859 switch(op >> 4) {
5860 case 1:
5861 gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
5862 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5863 s->mem_index, MO_LEUL);
5864 break;
5865 case 2:
5866 gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
5867 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
5868 s->mem_index, MO_LEQ);
5869 break;
5870 case 3:
5871 default:
5872 gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
5873 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5874 s->mem_index, MO_LEUW);
5875 break;
5877 gen_helper_fpop(cpu_env);
5878 break;
5879 default:
5880 switch(op >> 4) {
5881 case 0:
5882 gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
5883 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5884 s->mem_index, MO_LEUL);
5885 break;
5886 case 1:
5887 gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
5888 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5889 s->mem_index, MO_LEUL);
5890 break;
5891 case 2:
5892 gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
5893 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
5894 s->mem_index, MO_LEQ);
5895 break;
5896 case 3:
5897 default:
5898 gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
5899 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5900 s->mem_index, MO_LEUW);
5901 break;
5903 if ((op & 7) == 3)
5904 gen_helper_fpop(cpu_env);
5905 break;
5907 break;
5908 case 0x0c: /* fldenv mem */
5909 gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
5910 break;
5911 case 0x0d: /* fldcw mem */
5912 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5913 s->mem_index, MO_LEUW);
5914 gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
5915 break;
5916 case 0x0e: /* fnstenv mem */
5917 gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
5918 break;
5919 case 0x0f: /* fnstcw mem */
5920 gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
5921 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5922 s->mem_index, MO_LEUW);
5923 break;
5924 case 0x1d: /* fldt mem */
5925 gen_helper_fldt_ST0(cpu_env, cpu_A0);
5926 break;
5927 case 0x1f: /* fstpt mem */
5928 gen_helper_fstt_ST0(cpu_env, cpu_A0);
5929 gen_helper_fpop(cpu_env);
5930 break;
5931 case 0x2c: /* frstor mem */
5932 gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
5933 break;
5934 case 0x2e: /* fnsave mem */
5935 gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
5936 break;
5937 case 0x2f: /* fnstsw mem */
5938 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
5939 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5940 s->mem_index, MO_LEUW);
5941 break;
5942 case 0x3c: /* fbld */
5943 gen_helper_fbld_ST0(cpu_env, cpu_A0);
5944 break;
5945 case 0x3e: /* fbstp */
5946 gen_helper_fbst_ST0(cpu_env, cpu_A0);
5947 gen_helper_fpop(cpu_env);
5948 break;
5949 case 0x3d: /* fildll */
5950 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
5951 gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
5952 break;
5953 case 0x3f: /* fistpll */
5954 gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
5955 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
5956 gen_helper_fpop(cpu_env);
5957 break;
5958 default:
5959 goto unknown_op;
5961 } else {
5962 /* register float ops */
5963 opreg = rm;
5965 switch(op) {
5966 case 0x08: /* fld sti */
5967 gen_helper_fpush(cpu_env);
5968 gen_helper_fmov_ST0_STN(cpu_env,
5969 tcg_const_i32((opreg + 1) & 7));
5970 break;
5971 case 0x09: /* fxchg sti */
5972 case 0x29: /* fxchg4 sti, undocumented op */
5973 case 0x39: /* fxchg7 sti, undocumented op */
5974 gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
5975 break;
5976 case 0x0a: /* grp d9/2 */
5977 switch(rm) {
5978 case 0: /* fnop */
5979 /* check exceptions (FreeBSD FPU probe) */
5980 gen_helper_fwait(cpu_env);
5981 break;
5982 default:
5983 goto unknown_op;
5985 break;
5986 case 0x0c: /* grp d9/4 */
5987 switch(rm) {
5988 case 0: /* fchs */
5989 gen_helper_fchs_ST0(cpu_env);
5990 break;
5991 case 1: /* fabs */
5992 gen_helper_fabs_ST0(cpu_env);
5993 break;
5994 case 4: /* ftst */
5995 gen_helper_fldz_FT0(cpu_env);
5996 gen_helper_fcom_ST0_FT0(cpu_env);
5997 break;
5998 case 5: /* fxam */
5999 gen_helper_fxam_ST0(cpu_env);
6000 break;
6001 default:
6002 goto unknown_op;
6004 break;
6005 case 0x0d: /* grp d9/5 */
6007 switch(rm) {
6008 case 0:
6009 gen_helper_fpush(cpu_env);
6010 gen_helper_fld1_ST0(cpu_env);
6011 break;
6012 case 1:
6013 gen_helper_fpush(cpu_env);
6014 gen_helper_fldl2t_ST0(cpu_env);
6015 break;
6016 case 2:
6017 gen_helper_fpush(cpu_env);
6018 gen_helper_fldl2e_ST0(cpu_env);
6019 break;
6020 case 3:
6021 gen_helper_fpush(cpu_env);
6022 gen_helper_fldpi_ST0(cpu_env);
6023 break;
6024 case 4:
6025 gen_helper_fpush(cpu_env);
6026 gen_helper_fldlg2_ST0(cpu_env);
6027 break;
6028 case 5:
6029 gen_helper_fpush(cpu_env);
6030 gen_helper_fldln2_ST0(cpu_env);
6031 break;
6032 case 6:
6033 gen_helper_fpush(cpu_env);
6034 gen_helper_fldz_ST0(cpu_env);
6035 break;
6036 default:
6037 goto unknown_op;
6040 break;
6041 case 0x0e: /* grp d9/6 */
6042 switch(rm) {
6043 case 0: /* f2xm1 */
6044 gen_helper_f2xm1(cpu_env);
6045 break;
6046 case 1: /* fyl2x */
6047 gen_helper_fyl2x(cpu_env);
6048 break;
6049 case 2: /* fptan */
6050 gen_helper_fptan(cpu_env);
6051 break;
6052 case 3: /* fpatan */
6053 gen_helper_fpatan(cpu_env);
6054 break;
6055 case 4: /* fxtract */
6056 gen_helper_fxtract(cpu_env);
6057 break;
6058 case 5: /* fprem1 */
6059 gen_helper_fprem1(cpu_env);
6060 break;
6061 case 6: /* fdecstp */
6062 gen_helper_fdecstp(cpu_env);
6063 break;
6064 default:
6065 case 7: /* fincstp */
6066 gen_helper_fincstp(cpu_env);
6067 break;
6069 break;
6070 case 0x0f: /* grp d9/7 */
6071 switch(rm) {
6072 case 0: /* fprem */
6073 gen_helper_fprem(cpu_env);
6074 break;
6075 case 1: /* fyl2xp1 */
6076 gen_helper_fyl2xp1(cpu_env);
6077 break;
6078 case 2: /* fsqrt */
6079 gen_helper_fsqrt(cpu_env);
6080 break;
6081 case 3: /* fsincos */
6082 gen_helper_fsincos(cpu_env);
6083 break;
6084 case 5: /* fscale */
6085 gen_helper_fscale(cpu_env);
6086 break;
6087 case 4: /* frndint */
6088 gen_helper_frndint(cpu_env);
6089 break;
6090 case 6: /* fsin */
6091 gen_helper_fsin(cpu_env);
6092 break;
6093 default:
6094 case 7: /* fcos */
6095 gen_helper_fcos(cpu_env);
6096 break;
6098 break;
6099 case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
6100 case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
6101 case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
6103 int op1;
6105 op1 = op & 7;
6106 if (op >= 0x20) {
6107 gen_helper_fp_arith_STN_ST0(op1, opreg);
6108 if (op >= 0x30)
6109 gen_helper_fpop(cpu_env);
6110 } else {
6111 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6112 gen_helper_fp_arith_ST0_FT0(op1);
6115 break;
6116 case 0x02: /* fcom */
6117 case 0x22: /* fcom2, undocumented op */
6118 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6119 gen_helper_fcom_ST0_FT0(cpu_env);
6120 break;
6121 case 0x03: /* fcomp */
6122 case 0x23: /* fcomp3, undocumented op */
6123 case 0x32: /* fcomp5, undocumented op */
6124 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6125 gen_helper_fcom_ST0_FT0(cpu_env);
6126 gen_helper_fpop(cpu_env);
6127 break;
6128 case 0x15: /* da/5 */
6129 switch(rm) {
6130 case 1: /* fucompp */
6131 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
6132 gen_helper_fucom_ST0_FT0(cpu_env);
6133 gen_helper_fpop(cpu_env);
6134 gen_helper_fpop(cpu_env);
6135 break;
6136 default:
6137 goto unknown_op;
6139 break;
6140 case 0x1c:
6141 switch(rm) {
6142 case 0: /* feni (287 only, just do nop here) */
6143 break;
6144 case 1: /* fdisi (287 only, just do nop here) */
6145 break;
6146 case 2: /* fclex */
6147 gen_helper_fclex(cpu_env);
6148 break;
6149 case 3: /* fninit */
6150 gen_helper_fninit(cpu_env);
6151 break;
6152 case 4: /* fsetpm (287 only, just do nop here) */
6153 break;
6154 default:
6155 goto unknown_op;
6157 break;
6158 case 0x1d: /* fucomi */
6159 if (!(s->cpuid_features & CPUID_CMOV)) {
6160 goto illegal_op;
6162 gen_update_cc_op(s);
6163 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6164 gen_helper_fucomi_ST0_FT0(cpu_env);
6165 set_cc_op(s, CC_OP_EFLAGS);
6166 break;
6167 case 0x1e: /* fcomi */
6168 if (!(s->cpuid_features & CPUID_CMOV)) {
6169 goto illegal_op;
6171 gen_update_cc_op(s);
6172 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6173 gen_helper_fcomi_ST0_FT0(cpu_env);
6174 set_cc_op(s, CC_OP_EFLAGS);
6175 break;
6176 case 0x28: /* ffree sti */
6177 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6178 break;
6179 case 0x2a: /* fst sti */
6180 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
6181 break;
6182 case 0x2b: /* fstp sti */
6183 case 0x0b: /* fstp1 sti, undocumented op */
6184 case 0x3a: /* fstp8 sti, undocumented op */
6185 case 0x3b: /* fstp9 sti, undocumented op */
6186 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
6187 gen_helper_fpop(cpu_env);
6188 break;
6189 case 0x2c: /* fucom st(i) */
6190 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6191 gen_helper_fucom_ST0_FT0(cpu_env);
6192 break;
6193 case 0x2d: /* fucomp st(i) */
6194 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6195 gen_helper_fucom_ST0_FT0(cpu_env);
6196 gen_helper_fpop(cpu_env);
6197 break;
6198 case 0x33: /* de/3 */
6199 switch(rm) {
6200 case 1: /* fcompp */
6201 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
6202 gen_helper_fcom_ST0_FT0(cpu_env);
6203 gen_helper_fpop(cpu_env);
6204 gen_helper_fpop(cpu_env);
6205 break;
6206 default:
6207 goto unknown_op;
6209 break;
6210 case 0x38: /* ffreep sti, undocumented op */
6211 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6212 gen_helper_fpop(cpu_env);
6213 break;
6214 case 0x3c: /* df/4 */
6215 switch(rm) {
6216 case 0:
6217 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
6218 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
6219 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
6220 break;
6221 default:
6222 goto unknown_op;
6224 break;
6225 case 0x3d: /* fucomip */
6226 if (!(s->cpuid_features & CPUID_CMOV)) {
6227 goto illegal_op;
6229 gen_update_cc_op(s);
6230 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6231 gen_helper_fucomi_ST0_FT0(cpu_env);
6232 gen_helper_fpop(cpu_env);
6233 set_cc_op(s, CC_OP_EFLAGS);
6234 break;
6235 case 0x3e: /* fcomip */
6236 if (!(s->cpuid_features & CPUID_CMOV)) {
6237 goto illegal_op;
6239 gen_update_cc_op(s);
6240 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6241 gen_helper_fcomi_ST0_FT0(cpu_env);
6242 gen_helper_fpop(cpu_env);
6243 set_cc_op(s, CC_OP_EFLAGS);
6244 break;
6245 case 0x10 ... 0x13: /* fcmovxx */
6246 case 0x18 ... 0x1b:
6248 int op1;
6249 TCGLabel *l1;
6250 static const uint8_t fcmov_cc[8] = {
6251 (JCC_B << 1),
6252 (JCC_Z << 1),
6253 (JCC_BE << 1),
6254 (JCC_P << 1),
6257 if (!(s->cpuid_features & CPUID_CMOV)) {
6258 goto illegal_op;
6260 op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
6261 l1 = gen_new_label();
6262 gen_jcc1_noeob(s, op1, l1);
6263 gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
6264 gen_set_label(l1);
6266 break;
6267 default:
6268 goto unknown_op;
6271 break;
6272 /************************/
6273 /* string ops */
6275 case 0xa4: /* movsS */
6276 case 0xa5:
6277 ot = mo_b_d(b, dflag);
6278 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6279 gen_repz_movs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6280 } else {
6281 gen_movs(s, ot);
6283 break;
6285 case 0xaa: /* stosS */
6286 case 0xab:
6287 ot = mo_b_d(b, dflag);
6288 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6289 gen_repz_stos(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6290 } else {
6291 gen_stos(s, ot);
6293 break;
6294 case 0xac: /* lodsS */
6295 case 0xad:
6296 ot = mo_b_d(b, dflag);
6297 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6298 gen_repz_lods(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6299 } else {
6300 gen_lods(s, ot);
6302 break;
6303 case 0xae: /* scasS */
6304 case 0xaf:
6305 ot = mo_b_d(b, dflag);
6306 if (prefixes & PREFIX_REPNZ) {
6307 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6308 } else if (prefixes & PREFIX_REPZ) {
6309 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6310 } else {
6311 gen_scas(s, ot);
6313 break;
6315 case 0xa6: /* cmpsS */
6316 case 0xa7:
6317 ot = mo_b_d(b, dflag);
6318 if (prefixes & PREFIX_REPNZ) {
6319 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6320 } else if (prefixes & PREFIX_REPZ) {
6321 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6322 } else {
6323 gen_cmps(s, ot);
6325 break;
6326 case 0x6c: /* insS */
6327 case 0x6d:
6328 ot = mo_b_d32(b, dflag);
6329 tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
6330 gen_check_io(s, ot, pc_start - s->cs_base,
6331 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
6332 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6333 gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6334 } else {
6335 gen_ins(s, ot);
6336 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6337 gen_jmp(s, s->pc - s->cs_base);
6340 break;
6341 case 0x6e: /* outsS */
6342 case 0x6f:
6343 ot = mo_b_d32(b, dflag);
6344 tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
6345 gen_check_io(s, ot, pc_start - s->cs_base,
6346 svm_is_rep(prefixes) | 4);
6347 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6348 gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6349 } else {
6350 gen_outs(s, ot);
6351 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6352 gen_jmp(s, s->pc - s->cs_base);
6355 break;
6357 /************************/
6358 /* port I/O */
6360 case 0xe4:
6361 case 0xe5:
6362 ot = mo_b_d32(b, dflag);
6363 val = x86_ldub_code(env, s);
6364 tcg_gen_movi_tl(cpu_T0, val);
6365 gen_check_io(s, ot, pc_start - s->cs_base,
6366 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
6367 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6368 gen_io_start();
6370 tcg_gen_movi_i32(cpu_tmp2_i32, val);
6371 gen_helper_in_func(ot, cpu_T1, cpu_tmp2_i32);
6372 gen_op_mov_reg_v(ot, R_EAX, cpu_T1);
6373 gen_bpt_io(s, cpu_tmp2_i32, ot);
6374 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6375 gen_io_end();
6376 gen_jmp(s, s->pc - s->cs_base);
6378 break;
6379 case 0xe6:
6380 case 0xe7:
6381 ot = mo_b_d32(b, dflag);
6382 val = x86_ldub_code(env, s);
6383 tcg_gen_movi_tl(cpu_T0, val);
6384 gen_check_io(s, ot, pc_start - s->cs_base,
6385 svm_is_rep(prefixes));
6386 gen_op_mov_v_reg(ot, cpu_T1, R_EAX);
6388 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6389 gen_io_start();
6391 tcg_gen_movi_i32(cpu_tmp2_i32, val);
6392 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
6393 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
6394 gen_bpt_io(s, cpu_tmp2_i32, ot);
6395 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6396 gen_io_end();
6397 gen_jmp(s, s->pc - s->cs_base);
6399 break;
6400 case 0xec:
6401 case 0xed:
6402 ot = mo_b_d32(b, dflag);
6403 tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
6404 gen_check_io(s, ot, pc_start - s->cs_base,
6405 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
6406 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6407 gen_io_start();
6409 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
6410 gen_helper_in_func(ot, cpu_T1, cpu_tmp2_i32);
6411 gen_op_mov_reg_v(ot, R_EAX, cpu_T1);
6412 gen_bpt_io(s, cpu_tmp2_i32, ot);
6413 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6414 gen_io_end();
6415 gen_jmp(s, s->pc - s->cs_base);
6417 break;
6418 case 0xee:
6419 case 0xef:
6420 ot = mo_b_d32(b, dflag);
6421 tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
6422 gen_check_io(s, ot, pc_start - s->cs_base,
6423 svm_is_rep(prefixes));
6424 gen_op_mov_v_reg(ot, cpu_T1, R_EAX);
6426 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6427 gen_io_start();
6429 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
6430 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
6431 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
6432 gen_bpt_io(s, cpu_tmp2_i32, ot);
6433 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
6434 gen_io_end();
6435 gen_jmp(s, s->pc - s->cs_base);
6437 break;
6439 /************************/
6440 /* control */
6441 case 0xc2: /* ret im */
6442 val = x86_ldsw_code(env, s);
6443 ot = gen_pop_T0(s);
6444 gen_stack_update(s, val + (1 << ot));
6445 /* Note that gen_pop_T0 uses a zero-extending load. */
6446 gen_op_jmp_v(cpu_T0);
6447 gen_bnd_jmp(s);
6448 gen_jr(s, cpu_T0);
6449 break;
6450 case 0xc3: /* ret */
6451 ot = gen_pop_T0(s);
6452 gen_pop_update(s, ot);
6453 /* Note that gen_pop_T0 uses a zero-extending load. */
6454 gen_op_jmp_v(cpu_T0);
6455 gen_bnd_jmp(s);
6456 gen_jr(s, cpu_T0);
6457 break;
6458 case 0xca: /* lret im */
6459 val = x86_ldsw_code(env, s);
6460 do_lret:
6461 if (s->pe && !s->vm86) {
6462 gen_update_cc_op(s);
6463 gen_jmp_im(pc_start - s->cs_base);
6464 gen_helper_lret_protected(cpu_env, tcg_const_i32(dflag - 1),
6465 tcg_const_i32(val));
6466 } else {
6467 gen_stack_A0(s);
6468 /* pop offset */
6469 gen_op_ld_v(s, dflag, cpu_T0, cpu_A0);
6470 /* NOTE: keeping EIP updated is not a problem in case of
6471 exception */
6472 gen_op_jmp_v(cpu_T0);
6473 /* pop selector */
6474 gen_add_A0_im(s, 1 << dflag);
6475 gen_op_ld_v(s, dflag, cpu_T0, cpu_A0);
6476 gen_op_movl_seg_T0_vm(R_CS);
6477 /* add stack offset */
6478 gen_stack_update(s, val + (2 << dflag));
6480 gen_eob(s);
6481 break;
6482 case 0xcb: /* lret */
6483 val = 0;
6484 goto do_lret;
6485 case 0xcf: /* iret */
6486 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
6487 if (!s->pe) {
6488 /* real mode */
6489 gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
6490 set_cc_op(s, CC_OP_EFLAGS);
6491 } else if (s->vm86) {
6492 if (s->iopl != 3) {
6493 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6494 } else {
6495 gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
6496 set_cc_op(s, CC_OP_EFLAGS);
6498 } else {
6499 gen_helper_iret_protected(cpu_env, tcg_const_i32(dflag - 1),
6500 tcg_const_i32(s->pc - s->cs_base));
6501 set_cc_op(s, CC_OP_EFLAGS);
6503 gen_eob(s);
6504 break;
6505 case 0xe8: /* call im */
6507 if (dflag != MO_16) {
6508 tval = (int32_t)insn_get(env, s, MO_32);
6509 } else {
6510 tval = (int16_t)insn_get(env, s, MO_16);
6512 next_eip = s->pc - s->cs_base;
6513 tval += next_eip;
6514 if (dflag == MO_16) {
6515 tval &= 0xffff;
6516 } else if (!CODE64(s)) {
6517 tval &= 0xffffffff;
6519 tcg_gen_movi_tl(cpu_T0, next_eip);
6520 gen_push_v(s, cpu_T0);
6521 gen_bnd_jmp(s);
6522 gen_jmp(s, tval);
6524 break;
6525 case 0x9a: /* lcall im */
6527 unsigned int selector, offset;
6529 if (CODE64(s))
6530 goto illegal_op;
6531 ot = dflag;
6532 offset = insn_get(env, s, ot);
6533 selector = insn_get(env, s, MO_16);
6535 tcg_gen_movi_tl(cpu_T0, selector);
6536 tcg_gen_movi_tl(cpu_T1, offset);
6538 goto do_lcall;
6539 case 0xe9: /* jmp im */
6540 if (dflag != MO_16) {
6541 tval = (int32_t)insn_get(env, s, MO_32);
6542 } else {
6543 tval = (int16_t)insn_get(env, s, MO_16);
6545 tval += s->pc - s->cs_base;
6546 if (dflag == MO_16) {
6547 tval &= 0xffff;
6548 } else if (!CODE64(s)) {
6549 tval &= 0xffffffff;
6551 gen_bnd_jmp(s);
6552 gen_jmp(s, tval);
6553 break;
6554 case 0xea: /* ljmp im */
6556 unsigned int selector, offset;
6558 if (CODE64(s))
6559 goto illegal_op;
6560 ot = dflag;
6561 offset = insn_get(env, s, ot);
6562 selector = insn_get(env, s, MO_16);
6564 tcg_gen_movi_tl(cpu_T0, selector);
6565 tcg_gen_movi_tl(cpu_T1, offset);
6567 goto do_ljmp;
6568 case 0xeb: /* jmp Jb */
6569 tval = (int8_t)insn_get(env, s, MO_8);
6570 tval += s->pc - s->cs_base;
6571 if (dflag == MO_16) {
6572 tval &= 0xffff;
6574 gen_jmp(s, tval);
6575 break;
6576 case 0x70 ... 0x7f: /* jcc Jb */
6577 tval = (int8_t)insn_get(env, s, MO_8);
6578 goto do_jcc;
6579 case 0x180 ... 0x18f: /* jcc Jv */
6580 if (dflag != MO_16) {
6581 tval = (int32_t)insn_get(env, s, MO_32);
6582 } else {
6583 tval = (int16_t)insn_get(env, s, MO_16);
6585 do_jcc:
6586 next_eip = s->pc - s->cs_base;
6587 tval += next_eip;
6588 if (dflag == MO_16) {
6589 tval &= 0xffff;
6591 gen_bnd_jmp(s);
6592 gen_jcc(s, b, tval, next_eip);
6593 break;
6595 case 0x190 ... 0x19f: /* setcc Gv */
6596 modrm = x86_ldub_code(env, s);
6597 gen_setcc1(s, b, cpu_T0);
6598 gen_ldst_modrm(env, s, modrm, MO_8, OR_TMP0, 1);
6599 break;
6600 case 0x140 ... 0x14f: /* cmov Gv, Ev */
6601 if (!(s->cpuid_features & CPUID_CMOV)) {
6602 goto illegal_op;
6604 ot = dflag;
6605 modrm = x86_ldub_code(env, s);
6606 reg = ((modrm >> 3) & 7) | rex_r;
6607 gen_cmovcc1(env, s, ot, b, modrm, reg);
6608 break;
6610 /************************/
6611 /* flags */
6612 case 0x9c: /* pushf */
6613 gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
6614 if (s->vm86 && s->iopl != 3) {
6615 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6616 } else {
6617 gen_update_cc_op(s);
6618 gen_helper_read_eflags(cpu_T0, cpu_env);
6619 gen_push_v(s, cpu_T0);
6621 break;
6622 case 0x9d: /* popf */
6623 gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
6624 if (s->vm86 && s->iopl != 3) {
6625 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6626 } else {
6627 ot = gen_pop_T0(s);
6628 if (s->cpl == 0) {
6629 if (dflag != MO_16) {
6630 gen_helper_write_eflags(cpu_env, cpu_T0,
6631 tcg_const_i32((TF_MASK | AC_MASK |
6632 ID_MASK | NT_MASK |
6633 IF_MASK |
6634 IOPL_MASK)));
6635 } else {
6636 gen_helper_write_eflags(cpu_env, cpu_T0,
6637 tcg_const_i32((TF_MASK | AC_MASK |
6638 ID_MASK | NT_MASK |
6639 IF_MASK | IOPL_MASK)
6640 & 0xffff));
6642 } else {
6643 if (s->cpl <= s->iopl) {
6644 if (dflag != MO_16) {
6645 gen_helper_write_eflags(cpu_env, cpu_T0,
6646 tcg_const_i32((TF_MASK |
6647 AC_MASK |
6648 ID_MASK |
6649 NT_MASK |
6650 IF_MASK)));
6651 } else {
6652 gen_helper_write_eflags(cpu_env, cpu_T0,
6653 tcg_const_i32((TF_MASK |
6654 AC_MASK |
6655 ID_MASK |
6656 NT_MASK |
6657 IF_MASK)
6658 & 0xffff));
6660 } else {
6661 if (dflag != MO_16) {
6662 gen_helper_write_eflags(cpu_env, cpu_T0,
6663 tcg_const_i32((TF_MASK | AC_MASK |
6664 ID_MASK | NT_MASK)));
6665 } else {
6666 gen_helper_write_eflags(cpu_env, cpu_T0,
6667 tcg_const_i32((TF_MASK | AC_MASK |
6668 ID_MASK | NT_MASK)
6669 & 0xffff));
6673 gen_pop_update(s, ot);
6674 set_cc_op(s, CC_OP_EFLAGS);
6675 /* abort translation because TF/AC flag may change */
6676 gen_jmp_im(s->pc - s->cs_base);
6677 gen_eob(s);
6679 break;
6680 case 0x9e: /* sahf */
6681 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
6682 goto illegal_op;
6683 gen_op_mov_v_reg(MO_8, cpu_T0, R_AH);
6684 gen_compute_eflags(s);
6685 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, CC_O);
6686 tcg_gen_andi_tl(cpu_T0, cpu_T0, CC_S | CC_Z | CC_A | CC_P | CC_C);
6687 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_T0);
6688 break;
6689 case 0x9f: /* lahf */
6690 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
6691 goto illegal_op;
6692 gen_compute_eflags(s);
6693 /* Note: gen_compute_eflags() only gives the condition codes */
6694 tcg_gen_ori_tl(cpu_T0, cpu_cc_src, 0x02);
6695 gen_op_mov_reg_v(MO_8, R_AH, cpu_T0);
6696 break;
6697 case 0xf5: /* cmc */
6698 gen_compute_eflags(s);
6699 tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
6700 break;
6701 case 0xf8: /* clc */
6702 gen_compute_eflags(s);
6703 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
6704 break;
6705 case 0xf9: /* stc */
6706 gen_compute_eflags(s);
6707 tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
6708 break;
6709 case 0xfc: /* cld */
6710 tcg_gen_movi_i32(cpu_tmp2_i32, 1);
6711 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
6712 break;
6713 case 0xfd: /* std */
6714 tcg_gen_movi_i32(cpu_tmp2_i32, -1);
6715 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
6716 break;
6718 /************************/
6719 /* bit operations */
6720 case 0x1ba: /* bt/bts/btr/btc Gv, im */
6721 ot = dflag;
6722 modrm = x86_ldub_code(env, s);
6723 op = (modrm >> 3) & 7;
6724 mod = (modrm >> 6) & 3;
6725 rm = (modrm & 7) | REX_B(s);
6726 if (mod != 3) {
6727 s->rip_offset = 1;
6728 gen_lea_modrm(env, s, modrm);
6729 if (!(s->prefix & PREFIX_LOCK)) {
6730 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
6732 } else {
6733 gen_op_mov_v_reg(ot, cpu_T0, rm);
6735 /* load shift */
6736 val = x86_ldub_code(env, s);
6737 tcg_gen_movi_tl(cpu_T1, val);
6738 if (op < 4)
6739 goto unknown_op;
6740 op -= 4;
6741 goto bt_op;
6742 case 0x1a3: /* bt Gv, Ev */
6743 op = 0;
6744 goto do_btx;
6745 case 0x1ab: /* bts */
6746 op = 1;
6747 goto do_btx;
6748 case 0x1b3: /* btr */
6749 op = 2;
6750 goto do_btx;
6751 case 0x1bb: /* btc */
6752 op = 3;
6753 do_btx:
6754 ot = dflag;
6755 modrm = x86_ldub_code(env, s);
6756 reg = ((modrm >> 3) & 7) | rex_r;
6757 mod = (modrm >> 6) & 3;
6758 rm = (modrm & 7) | REX_B(s);
6759 gen_op_mov_v_reg(MO_32, cpu_T1, reg);
6760 if (mod != 3) {
6761 AddressParts a = gen_lea_modrm_0(env, s, modrm);
6762 /* specific case: we need to add a displacement */
6763 gen_exts(ot, cpu_T1);
6764 tcg_gen_sari_tl(cpu_tmp0, cpu_T1, 3 + ot);
6765 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, ot);
6766 tcg_gen_add_tl(cpu_A0, gen_lea_modrm_1(a), cpu_tmp0);
6767 gen_lea_v_seg(s, s->aflag, cpu_A0, a.def_seg, s->override);
6768 if (!(s->prefix & PREFIX_LOCK)) {
6769 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
6771 } else {
6772 gen_op_mov_v_reg(ot, cpu_T0, rm);
6774 bt_op:
6775 tcg_gen_andi_tl(cpu_T1, cpu_T1, (1 << (3 + ot)) - 1);
6776 tcg_gen_movi_tl(cpu_tmp0, 1);
6777 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T1);
6778 if (s->prefix & PREFIX_LOCK) {
6779 switch (op) {
6780 case 0: /* bt */
6781 /* Needs no atomic ops; we surpressed the normal
6782 memory load for LOCK above so do it now. */
6783 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
6784 break;
6785 case 1: /* bts */
6786 tcg_gen_atomic_fetch_or_tl(cpu_T0, cpu_A0, cpu_tmp0,
6787 s->mem_index, ot | MO_LE);
6788 break;
6789 case 2: /* btr */
6790 tcg_gen_not_tl(cpu_tmp0, cpu_tmp0);
6791 tcg_gen_atomic_fetch_and_tl(cpu_T0, cpu_A0, cpu_tmp0,
6792 s->mem_index, ot | MO_LE);
6793 break;
6794 default:
6795 case 3: /* btc */
6796 tcg_gen_atomic_fetch_xor_tl(cpu_T0, cpu_A0, cpu_tmp0,
6797 s->mem_index, ot | MO_LE);
6798 break;
6800 tcg_gen_shr_tl(cpu_tmp4, cpu_T0, cpu_T1);
6801 } else {
6802 tcg_gen_shr_tl(cpu_tmp4, cpu_T0, cpu_T1);
6803 switch (op) {
6804 case 0: /* bt */
6805 /* Data already loaded; nothing to do. */
6806 break;
6807 case 1: /* bts */
6808 tcg_gen_or_tl(cpu_T0, cpu_T0, cpu_tmp0);
6809 break;
6810 case 2: /* btr */
6811 tcg_gen_andc_tl(cpu_T0, cpu_T0, cpu_tmp0);
6812 break;
6813 default:
6814 case 3: /* btc */
6815 tcg_gen_xor_tl(cpu_T0, cpu_T0, cpu_tmp0);
6816 break;
6818 if (op != 0) {
6819 if (mod != 3) {
6820 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
6821 } else {
6822 gen_op_mov_reg_v(ot, rm, cpu_T0);
6827 /* Delay all CC updates until after the store above. Note that
6828 C is the result of the test, Z is unchanged, and the others
6829 are all undefined. */
6830 switch (s->cc_op) {
6831 case CC_OP_MULB ... CC_OP_MULQ:
6832 case CC_OP_ADDB ... CC_OP_ADDQ:
6833 case CC_OP_ADCB ... CC_OP_ADCQ:
6834 case CC_OP_SUBB ... CC_OP_SUBQ:
6835 case CC_OP_SBBB ... CC_OP_SBBQ:
6836 case CC_OP_LOGICB ... CC_OP_LOGICQ:
6837 case CC_OP_INCB ... CC_OP_INCQ:
6838 case CC_OP_DECB ... CC_OP_DECQ:
6839 case CC_OP_SHLB ... CC_OP_SHLQ:
6840 case CC_OP_SARB ... CC_OP_SARQ:
6841 case CC_OP_BMILGB ... CC_OP_BMILGQ:
6842 /* Z was going to be computed from the non-zero status of CC_DST.
6843 We can get that same Z value (and the new C value) by leaving
6844 CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
6845 same width. */
6846 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
6847 set_cc_op(s, ((s->cc_op - CC_OP_MULB) & 3) + CC_OP_SARB);
6848 break;
6849 default:
6850 /* Otherwise, generate EFLAGS and replace the C bit. */
6851 gen_compute_eflags(s);
6852 tcg_gen_deposit_tl(cpu_cc_src, cpu_cc_src, cpu_tmp4,
6853 ctz32(CC_C), 1);
6854 break;
6856 break;
6857 case 0x1bc: /* bsf / tzcnt */
6858 case 0x1bd: /* bsr / lzcnt */
6859 ot = dflag;
6860 modrm = x86_ldub_code(env, s);
6861 reg = ((modrm >> 3) & 7) | rex_r;
6862 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
6863 gen_extu(ot, cpu_T0);
6865 /* Note that lzcnt and tzcnt are in different extensions. */
6866 if ((prefixes & PREFIX_REPZ)
6867 && (b & 1
6868 ? s->cpuid_ext3_features & CPUID_EXT3_ABM
6869 : s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)) {
6870 int size = 8 << ot;
6871 /* For lzcnt/tzcnt, C bit is defined related to the input. */
6872 tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
6873 if (b & 1) {
6874 /* For lzcnt, reduce the target_ulong result by the
6875 number of zeros that we expect to find at the top. */
6876 tcg_gen_clzi_tl(cpu_T0, cpu_T0, TARGET_LONG_BITS);
6877 tcg_gen_subi_tl(cpu_T0, cpu_T0, TARGET_LONG_BITS - size);
6878 } else {
6879 /* For tzcnt, a zero input must return the operand size. */
6880 tcg_gen_ctzi_tl(cpu_T0, cpu_T0, size);
6882 /* For lzcnt/tzcnt, Z bit is defined related to the result. */
6883 gen_op_update1_cc();
6884 set_cc_op(s, CC_OP_BMILGB + ot);
6885 } else {
6886 /* For bsr/bsf, only the Z bit is defined and it is related
6887 to the input and not the result. */
6888 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
6889 set_cc_op(s, CC_OP_LOGICB + ot);
6891 /* ??? The manual says that the output is undefined when the
6892 input is zero, but real hardware leaves it unchanged, and
6893 real programs appear to depend on that. Accomplish this
6894 by passing the output as the value to return upon zero. */
6895 if (b & 1) {
6896 /* For bsr, return the bit index of the first 1 bit,
6897 not the count of leading zeros. */
6898 tcg_gen_xori_tl(cpu_T1, cpu_regs[reg], TARGET_LONG_BITS - 1);
6899 tcg_gen_clz_tl(cpu_T0, cpu_T0, cpu_T1);
6900 tcg_gen_xori_tl(cpu_T0, cpu_T0, TARGET_LONG_BITS - 1);
6901 } else {
6902 tcg_gen_ctz_tl(cpu_T0, cpu_T0, cpu_regs[reg]);
6905 gen_op_mov_reg_v(ot, reg, cpu_T0);
6906 break;
6907 /************************/
6908 /* bcd */
6909 case 0x27: /* daa */
6910 if (CODE64(s))
6911 goto illegal_op;
6912 gen_update_cc_op(s);
6913 gen_helper_daa(cpu_env);
6914 set_cc_op(s, CC_OP_EFLAGS);
6915 break;
6916 case 0x2f: /* das */
6917 if (CODE64(s))
6918 goto illegal_op;
6919 gen_update_cc_op(s);
6920 gen_helper_das(cpu_env);
6921 set_cc_op(s, CC_OP_EFLAGS);
6922 break;
6923 case 0x37: /* aaa */
6924 if (CODE64(s))
6925 goto illegal_op;
6926 gen_update_cc_op(s);
6927 gen_helper_aaa(cpu_env);
6928 set_cc_op(s, CC_OP_EFLAGS);
6929 break;
6930 case 0x3f: /* aas */
6931 if (CODE64(s))
6932 goto illegal_op;
6933 gen_update_cc_op(s);
6934 gen_helper_aas(cpu_env);
6935 set_cc_op(s, CC_OP_EFLAGS);
6936 break;
6937 case 0xd4: /* aam */
6938 if (CODE64(s))
6939 goto illegal_op;
6940 val = x86_ldub_code(env, s);
6941 if (val == 0) {
6942 gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
6943 } else {
6944 gen_helper_aam(cpu_env, tcg_const_i32(val));
6945 set_cc_op(s, CC_OP_LOGICB);
6947 break;
6948 case 0xd5: /* aad */
6949 if (CODE64(s))
6950 goto illegal_op;
6951 val = x86_ldub_code(env, s);
6952 gen_helper_aad(cpu_env, tcg_const_i32(val));
6953 set_cc_op(s, CC_OP_LOGICB);
6954 break;
6955 /************************/
6956 /* misc */
6957 case 0x90: /* nop */
6958 /* XXX: correct lock test for all insn */
6959 if (prefixes & PREFIX_LOCK) {
6960 goto illegal_op;
6962 /* If REX_B is set, then this is xchg eax, r8d, not a nop. */
6963 if (REX_B(s)) {
6964 goto do_xchg_reg_eax;
6966 if (prefixes & PREFIX_REPZ) {
6967 gen_update_cc_op(s);
6968 gen_jmp_im(pc_start - s->cs_base);
6969 gen_helper_pause(cpu_env, tcg_const_i32(s->pc - pc_start));
6970 s->base.is_jmp = DISAS_NORETURN;
6972 break;
6973 case 0x9b: /* fwait */
6974 if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
6975 (HF_MP_MASK | HF_TS_MASK)) {
6976 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
6977 } else {
6978 gen_helper_fwait(cpu_env);
6980 break;
6981 case 0xcc: /* int3 */
6982 gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
6983 break;
6984 case 0xcd: /* int N */
6985 val = x86_ldub_code(env, s);
6986 if (s->vm86 && s->iopl != 3) {
6987 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6988 } else {
6989 gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
6991 break;
6992 case 0xce: /* into */
6993 if (CODE64(s))
6994 goto illegal_op;
6995 gen_update_cc_op(s);
6996 gen_jmp_im(pc_start - s->cs_base);
6997 gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
6998 break;
6999 #ifdef WANT_ICEBP
7000 case 0xf1: /* icebp (undocumented, exits to external debugger) */
7001 gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
7002 #if 1
7003 gen_debug(s, pc_start - s->cs_base);
7004 #else
7005 /* start debug */
7006 tb_flush(CPU(x86_env_get_cpu(env)));
7007 qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
7008 #endif
7009 break;
7010 #endif
7011 case 0xfa: /* cli */
7012 if (!s->vm86) {
7013 if (s->cpl <= s->iopl) {
7014 gen_helper_cli(cpu_env);
7015 } else {
7016 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7018 } else {
7019 if (s->iopl == 3) {
7020 gen_helper_cli(cpu_env);
7021 } else {
7022 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7025 break;
7026 case 0xfb: /* sti */
7027 if (s->vm86 ? s->iopl == 3 : s->cpl <= s->iopl) {
7028 gen_helper_sti(cpu_env);
7029 /* interruptions are enabled only the first insn after sti */
7030 gen_jmp_im(s->pc - s->cs_base);
7031 gen_eob_inhibit_irq(s, true);
7032 } else {
7033 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7035 break;
7036 case 0x62: /* bound */
7037 if (CODE64(s))
7038 goto illegal_op;
7039 ot = dflag;
7040 modrm = x86_ldub_code(env, s);
7041 reg = (modrm >> 3) & 7;
7042 mod = (modrm >> 6) & 3;
7043 if (mod == 3)
7044 goto illegal_op;
7045 gen_op_mov_v_reg(ot, cpu_T0, reg);
7046 gen_lea_modrm(env, s, modrm);
7047 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
7048 if (ot == MO_16) {
7049 gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
7050 } else {
7051 gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
7053 break;
7054 case 0x1c8 ... 0x1cf: /* bswap reg */
7055 reg = (b & 7) | REX_B(s);
7056 #ifdef TARGET_X86_64
7057 if (dflag == MO_64) {
7058 gen_op_mov_v_reg(MO_64, cpu_T0, reg);
7059 tcg_gen_bswap64_i64(cpu_T0, cpu_T0);
7060 gen_op_mov_reg_v(MO_64, reg, cpu_T0);
7061 } else
7062 #endif
7064 gen_op_mov_v_reg(MO_32, cpu_T0, reg);
7065 tcg_gen_ext32u_tl(cpu_T0, cpu_T0);
7066 tcg_gen_bswap32_tl(cpu_T0, cpu_T0);
7067 gen_op_mov_reg_v(MO_32, reg, cpu_T0);
7069 break;
7070 case 0xd6: /* salc */
7071 if (CODE64(s))
7072 goto illegal_op;
7073 gen_compute_eflags_c(s, cpu_T0);
7074 tcg_gen_neg_tl(cpu_T0, cpu_T0);
7075 gen_op_mov_reg_v(MO_8, R_EAX, cpu_T0);
7076 break;
7077 case 0xe0: /* loopnz */
7078 case 0xe1: /* loopz */
7079 case 0xe2: /* loop */
7080 case 0xe3: /* jecxz */
7082 TCGLabel *l1, *l2, *l3;
7084 tval = (int8_t)insn_get(env, s, MO_8);
7085 next_eip = s->pc - s->cs_base;
7086 tval += next_eip;
7087 if (dflag == MO_16) {
7088 tval &= 0xffff;
7091 l1 = gen_new_label();
7092 l2 = gen_new_label();
7093 l3 = gen_new_label();
7094 b &= 3;
7095 switch(b) {
7096 case 0: /* loopnz */
7097 case 1: /* loopz */
7098 gen_op_add_reg_im(s->aflag, R_ECX, -1);
7099 gen_op_jz_ecx(s->aflag, l3);
7100 gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
7101 break;
7102 case 2: /* loop */
7103 gen_op_add_reg_im(s->aflag, R_ECX, -1);
7104 gen_op_jnz_ecx(s->aflag, l1);
7105 break;
7106 default:
7107 case 3: /* jcxz */
7108 gen_op_jz_ecx(s->aflag, l1);
7109 break;
7112 gen_set_label(l3);
7113 gen_jmp_im(next_eip);
7114 tcg_gen_br(l2);
7116 gen_set_label(l1);
7117 gen_jmp_im(tval);
7118 gen_set_label(l2);
7119 gen_eob(s);
7121 break;
7122 case 0x130: /* wrmsr */
7123 case 0x132: /* rdmsr */
7124 if (s->cpl != 0) {
7125 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7126 } else {
7127 gen_update_cc_op(s);
7128 gen_jmp_im(pc_start - s->cs_base);
7129 if (b & 2) {
7130 gen_helper_rdmsr(cpu_env);
7131 } else {
7132 gen_helper_wrmsr(cpu_env);
7135 break;
7136 case 0x131: /* rdtsc */
7137 gen_update_cc_op(s);
7138 gen_jmp_im(pc_start - s->cs_base);
7139 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7140 gen_io_start();
7142 gen_helper_rdtsc(cpu_env);
7143 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7144 gen_io_end();
7145 gen_jmp(s, s->pc - s->cs_base);
7147 break;
7148 case 0x133: /* rdpmc */
7149 gen_update_cc_op(s);
7150 gen_jmp_im(pc_start - s->cs_base);
7151 gen_helper_rdpmc(cpu_env);
7152 break;
7153 case 0x134: /* sysenter */
7154 /* For Intel SYSENTER is valid on 64-bit */
7155 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
7156 goto illegal_op;
7157 if (!s->pe) {
7158 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7159 } else {
7160 gen_helper_sysenter(cpu_env);
7161 gen_eob(s);
7163 break;
7164 case 0x135: /* sysexit */
7165 /* For Intel SYSEXIT is valid on 64-bit */
7166 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
7167 goto illegal_op;
7168 if (!s->pe) {
7169 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7170 } else {
7171 gen_helper_sysexit(cpu_env, tcg_const_i32(dflag - 1));
7172 gen_eob(s);
7174 break;
7175 #ifdef TARGET_X86_64
7176 case 0x105: /* syscall */
7177 /* XXX: is it usable in real mode ? */
7178 gen_update_cc_op(s);
7179 gen_jmp_im(pc_start - s->cs_base);
7180 gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
7181 /* TF handling for the syscall insn is different. The TF bit is checked
7182 after the syscall insn completes. This allows #DB to not be
7183 generated after one has entered CPL0 if TF is set in FMASK. */
7184 gen_eob_worker(s, false, true);
7185 break;
7186 case 0x107: /* sysret */
7187 if (!s->pe) {
7188 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7189 } else {
7190 gen_helper_sysret(cpu_env, tcg_const_i32(dflag - 1));
7191 /* condition codes are modified only in long mode */
7192 if (s->lma) {
7193 set_cc_op(s, CC_OP_EFLAGS);
7195 /* TF handling for the sysret insn is different. The TF bit is
7196 checked after the sysret insn completes. This allows #DB to be
7197 generated "as if" the syscall insn in userspace has just
7198 completed. */
7199 gen_eob_worker(s, false, true);
7201 break;
7202 #endif
7203 case 0x1a2: /* cpuid */
7204 gen_update_cc_op(s);
7205 gen_jmp_im(pc_start - s->cs_base);
7206 gen_helper_cpuid(cpu_env);
7207 break;
7208 case 0xf4: /* hlt */
7209 if (s->cpl != 0) {
7210 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7211 } else {
7212 gen_update_cc_op(s);
7213 gen_jmp_im(pc_start - s->cs_base);
7214 gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
7215 s->base.is_jmp = DISAS_NORETURN;
7217 break;
7218 case 0x100:
7219 modrm = x86_ldub_code(env, s);
7220 mod = (modrm >> 6) & 3;
7221 op = (modrm >> 3) & 7;
7222 switch(op) {
7223 case 0: /* sldt */
7224 if (!s->pe || s->vm86)
7225 goto illegal_op;
7226 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
7227 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
7228 offsetof(CPUX86State, ldt.selector));
7229 ot = mod == 3 ? dflag : MO_16;
7230 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
7231 break;
7232 case 2: /* lldt */
7233 if (!s->pe || s->vm86)
7234 goto illegal_op;
7235 if (s->cpl != 0) {
7236 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7237 } else {
7238 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
7239 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7240 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
7241 gen_helper_lldt(cpu_env, cpu_tmp2_i32);
7243 break;
7244 case 1: /* str */
7245 if (!s->pe || s->vm86)
7246 goto illegal_op;
7247 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
7248 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
7249 offsetof(CPUX86State, tr.selector));
7250 ot = mod == 3 ? dflag : MO_16;
7251 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
7252 break;
7253 case 3: /* ltr */
7254 if (!s->pe || s->vm86)
7255 goto illegal_op;
7256 if (s->cpl != 0) {
7257 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7258 } else {
7259 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
7260 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7261 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
7262 gen_helper_ltr(cpu_env, cpu_tmp2_i32);
7264 break;
7265 case 4: /* verr */
7266 case 5: /* verw */
7267 if (!s->pe || s->vm86)
7268 goto illegal_op;
7269 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7270 gen_update_cc_op(s);
7271 if (op == 4) {
7272 gen_helper_verr(cpu_env, cpu_T0);
7273 } else {
7274 gen_helper_verw(cpu_env, cpu_T0);
7276 set_cc_op(s, CC_OP_EFLAGS);
7277 break;
7278 default:
7279 goto unknown_op;
7281 break;
7283 case 0x101:
7284 modrm = x86_ldub_code(env, s);
7285 switch (modrm) {
7286 CASE_MODRM_MEM_OP(0): /* sgdt */
7287 gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
7288 gen_lea_modrm(env, s, modrm);
7289 tcg_gen_ld32u_tl(cpu_T0,
7290 cpu_env, offsetof(CPUX86State, gdt.limit));
7291 gen_op_st_v(s, MO_16, cpu_T0, cpu_A0);
7292 gen_add_A0_im(s, 2);
7293 tcg_gen_ld_tl(cpu_T0, cpu_env, offsetof(CPUX86State, gdt.base));
7294 if (dflag == MO_16) {
7295 tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
7297 gen_op_st_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
7298 break;
7300 case 0xc8: /* monitor */
7301 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) || s->cpl != 0) {
7302 goto illegal_op;
7304 gen_update_cc_op(s);
7305 gen_jmp_im(pc_start - s->cs_base);
7306 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EAX]);
7307 gen_extu(s->aflag, cpu_A0);
7308 gen_add_A0_ds_seg(s);
7309 gen_helper_monitor(cpu_env, cpu_A0);
7310 break;
7312 case 0xc9: /* mwait */
7313 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) || s->cpl != 0) {
7314 goto illegal_op;
7316 gen_update_cc_op(s);
7317 gen_jmp_im(pc_start - s->cs_base);
7318 gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
7319 gen_eob(s);
7320 break;
7322 case 0xca: /* clac */
7323 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP)
7324 || s->cpl != 0) {
7325 goto illegal_op;
7327 gen_helper_clac(cpu_env);
7328 gen_jmp_im(s->pc - s->cs_base);
7329 gen_eob(s);
7330 break;
7332 case 0xcb: /* stac */
7333 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP)
7334 || s->cpl != 0) {
7335 goto illegal_op;
7337 gen_helper_stac(cpu_env);
7338 gen_jmp_im(s->pc - s->cs_base);
7339 gen_eob(s);
7340 break;
7342 CASE_MODRM_MEM_OP(1): /* sidt */
7343 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
7344 gen_lea_modrm(env, s, modrm);
7345 tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State, idt.limit));
7346 gen_op_st_v(s, MO_16, cpu_T0, cpu_A0);
7347 gen_add_A0_im(s, 2);
7348 tcg_gen_ld_tl(cpu_T0, cpu_env, offsetof(CPUX86State, idt.base));
7349 if (dflag == MO_16) {
7350 tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
7352 gen_op_st_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
7353 break;
7355 case 0xd0: /* xgetbv */
7356 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
7357 || (s->prefix & (PREFIX_LOCK | PREFIX_DATA
7358 | PREFIX_REPZ | PREFIX_REPNZ))) {
7359 goto illegal_op;
7361 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
7362 gen_helper_xgetbv(cpu_tmp1_i64, cpu_env, cpu_tmp2_i32);
7363 tcg_gen_extr_i64_tl(cpu_regs[R_EAX], cpu_regs[R_EDX], cpu_tmp1_i64);
7364 break;
7366 case 0xd1: /* xsetbv */
7367 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
7368 || (s->prefix & (PREFIX_LOCK | PREFIX_DATA
7369 | PREFIX_REPZ | PREFIX_REPNZ))) {
7370 goto illegal_op;
7372 if (s->cpl != 0) {
7373 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7374 break;
7376 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
7377 cpu_regs[R_EDX]);
7378 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
7379 gen_helper_xsetbv(cpu_env, cpu_tmp2_i32, cpu_tmp1_i64);
7380 /* End TB because translation flags may change. */
7381 gen_jmp_im(s->pc - s->cs_base);
7382 gen_eob(s);
7383 break;
7385 case 0xd8: /* VMRUN */
7386 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7387 goto illegal_op;
7389 if (s->cpl != 0) {
7390 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7391 break;
7393 gen_update_cc_op(s);
7394 gen_jmp_im(pc_start - s->cs_base);
7395 gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag - 1),
7396 tcg_const_i32(s->pc - pc_start));
7397 tcg_gen_exit_tb(NULL, 0);
7398 s->base.is_jmp = DISAS_NORETURN;
7399 break;
7401 case 0xd9: /* VMMCALL */
7402 if (!(s->flags & HF_SVME_MASK)) {
7403 goto illegal_op;
7405 gen_update_cc_op(s);
7406 gen_jmp_im(pc_start - s->cs_base);
7407 gen_helper_vmmcall(cpu_env);
7408 break;
7410 case 0xda: /* VMLOAD */
7411 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7412 goto illegal_op;
7414 if (s->cpl != 0) {
7415 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7416 break;
7418 gen_update_cc_op(s);
7419 gen_jmp_im(pc_start - s->cs_base);
7420 gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag - 1));
7421 break;
7423 case 0xdb: /* VMSAVE */
7424 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7425 goto illegal_op;
7427 if (s->cpl != 0) {
7428 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7429 break;
7431 gen_update_cc_op(s);
7432 gen_jmp_im(pc_start - s->cs_base);
7433 gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag - 1));
7434 break;
7436 case 0xdc: /* STGI */
7437 if ((!(s->flags & HF_SVME_MASK)
7438 && !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT))
7439 || !s->pe) {
7440 goto illegal_op;
7442 if (s->cpl != 0) {
7443 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7444 break;
7446 gen_update_cc_op(s);
7447 gen_helper_stgi(cpu_env);
7448 gen_jmp_im(s->pc - s->cs_base);
7449 gen_eob(s);
7450 break;
7452 case 0xdd: /* CLGI */
7453 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7454 goto illegal_op;
7456 if (s->cpl != 0) {
7457 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7458 break;
7460 gen_update_cc_op(s);
7461 gen_jmp_im(pc_start - s->cs_base);
7462 gen_helper_clgi(cpu_env);
7463 break;
7465 case 0xde: /* SKINIT */
7466 if ((!(s->flags & HF_SVME_MASK)
7467 && !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT))
7468 || !s->pe) {
7469 goto illegal_op;
7471 gen_update_cc_op(s);
7472 gen_jmp_im(pc_start - s->cs_base);
7473 gen_helper_skinit(cpu_env);
7474 break;
7476 case 0xdf: /* INVLPGA */
7477 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7478 goto illegal_op;
7480 if (s->cpl != 0) {
7481 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7482 break;
7484 gen_update_cc_op(s);
7485 gen_jmp_im(pc_start - s->cs_base);
7486 gen_helper_invlpga(cpu_env, tcg_const_i32(s->aflag - 1));
7487 break;
7489 CASE_MODRM_MEM_OP(2): /* lgdt */
7490 if (s->cpl != 0) {
7491 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7492 break;
7494 gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_WRITE);
7495 gen_lea_modrm(env, s, modrm);
7496 gen_op_ld_v(s, MO_16, cpu_T1, cpu_A0);
7497 gen_add_A0_im(s, 2);
7498 gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
7499 if (dflag == MO_16) {
7500 tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
7502 tcg_gen_st_tl(cpu_T0, cpu_env, offsetof(CPUX86State, gdt.base));
7503 tcg_gen_st32_tl(cpu_T1, cpu_env, offsetof(CPUX86State, gdt.limit));
7504 break;
7506 CASE_MODRM_MEM_OP(3): /* lidt */
7507 if (s->cpl != 0) {
7508 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7509 break;
7511 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_WRITE);
7512 gen_lea_modrm(env, s, modrm);
7513 gen_op_ld_v(s, MO_16, cpu_T1, cpu_A0);
7514 gen_add_A0_im(s, 2);
7515 gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
7516 if (dflag == MO_16) {
7517 tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
7519 tcg_gen_st_tl(cpu_T0, cpu_env, offsetof(CPUX86State, idt.base));
7520 tcg_gen_st32_tl(cpu_T1, cpu_env, offsetof(CPUX86State, idt.limit));
7521 break;
7523 CASE_MODRM_OP(4): /* smsw */
7524 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
7525 tcg_gen_ld_tl(cpu_T0, cpu_env, offsetof(CPUX86State, cr[0]));
7526 if (CODE64(s)) {
7527 mod = (modrm >> 6) & 3;
7528 ot = (mod != 3 ? MO_16 : s->dflag);
7529 } else {
7530 ot = MO_16;
7532 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
7533 break;
7534 case 0xee: /* rdpkru */
7535 if (prefixes & PREFIX_LOCK) {
7536 goto illegal_op;
7538 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
7539 gen_helper_rdpkru(cpu_tmp1_i64, cpu_env, cpu_tmp2_i32);
7540 tcg_gen_extr_i64_tl(cpu_regs[R_EAX], cpu_regs[R_EDX], cpu_tmp1_i64);
7541 break;
7542 case 0xef: /* wrpkru */
7543 if (prefixes & PREFIX_LOCK) {
7544 goto illegal_op;
7546 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
7547 cpu_regs[R_EDX]);
7548 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
7549 gen_helper_wrpkru(cpu_env, cpu_tmp2_i32, cpu_tmp1_i64);
7550 break;
7551 CASE_MODRM_OP(6): /* lmsw */
7552 if (s->cpl != 0) {
7553 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7554 break;
7556 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7557 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7558 gen_helper_lmsw(cpu_env, cpu_T0);
7559 gen_jmp_im(s->pc - s->cs_base);
7560 gen_eob(s);
7561 break;
7563 CASE_MODRM_MEM_OP(7): /* invlpg */
7564 if (s->cpl != 0) {
7565 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7566 break;
7568 gen_update_cc_op(s);
7569 gen_jmp_im(pc_start - s->cs_base);
7570 gen_lea_modrm(env, s, modrm);
7571 gen_helper_invlpg(cpu_env, cpu_A0);
7572 gen_jmp_im(s->pc - s->cs_base);
7573 gen_eob(s);
7574 break;
7576 case 0xf8: /* swapgs */
7577 #ifdef TARGET_X86_64
7578 if (CODE64(s)) {
7579 if (s->cpl != 0) {
7580 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7581 } else {
7582 tcg_gen_mov_tl(cpu_T0, cpu_seg_base[R_GS]);
7583 tcg_gen_ld_tl(cpu_seg_base[R_GS], cpu_env,
7584 offsetof(CPUX86State, kernelgsbase));
7585 tcg_gen_st_tl(cpu_T0, cpu_env,
7586 offsetof(CPUX86State, kernelgsbase));
7588 break;
7590 #endif
7591 goto illegal_op;
7593 case 0xf9: /* rdtscp */
7594 if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP)) {
7595 goto illegal_op;
7597 gen_update_cc_op(s);
7598 gen_jmp_im(pc_start - s->cs_base);
7599 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7600 gen_io_start();
7602 gen_helper_rdtscp(cpu_env);
7603 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7604 gen_io_end();
7605 gen_jmp(s, s->pc - s->cs_base);
7607 break;
7609 default:
7610 goto unknown_op;
7612 break;
7614 case 0x108: /* invd */
7615 case 0x109: /* wbinvd */
7616 if (s->cpl != 0) {
7617 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7618 } else {
7619 gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
7620 /* nothing to do */
7622 break;
7623 case 0x63: /* arpl or movslS (x86_64) */
7624 #ifdef TARGET_X86_64
7625 if (CODE64(s)) {
7626 int d_ot;
7627 /* d_ot is the size of destination */
7628 d_ot = dflag;
7630 modrm = x86_ldub_code(env, s);
7631 reg = ((modrm >> 3) & 7) | rex_r;
7632 mod = (modrm >> 6) & 3;
7633 rm = (modrm & 7) | REX_B(s);
7635 if (mod == 3) {
7636 gen_op_mov_v_reg(MO_32, cpu_T0, rm);
7637 /* sign extend */
7638 if (d_ot == MO_64) {
7639 tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
7641 gen_op_mov_reg_v(d_ot, reg, cpu_T0);
7642 } else {
7643 gen_lea_modrm(env, s, modrm);
7644 gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T0, cpu_A0);
7645 gen_op_mov_reg_v(d_ot, reg, cpu_T0);
7647 } else
7648 #endif
7650 TCGLabel *label1;
7651 TCGv t0, t1, t2, a0;
7653 if (!s->pe || s->vm86)
7654 goto illegal_op;
7655 t0 = tcg_temp_local_new();
7656 t1 = tcg_temp_local_new();
7657 t2 = tcg_temp_local_new();
7658 ot = MO_16;
7659 modrm = x86_ldub_code(env, s);
7660 reg = (modrm >> 3) & 7;
7661 mod = (modrm >> 6) & 3;
7662 rm = modrm & 7;
7663 if (mod != 3) {
7664 gen_lea_modrm(env, s, modrm);
7665 gen_op_ld_v(s, ot, t0, cpu_A0);
7666 a0 = tcg_temp_local_new();
7667 tcg_gen_mov_tl(a0, cpu_A0);
7668 } else {
7669 gen_op_mov_v_reg(ot, t0, rm);
7670 a0 = NULL;
7672 gen_op_mov_v_reg(ot, t1, reg);
7673 tcg_gen_andi_tl(cpu_tmp0, t0, 3);
7674 tcg_gen_andi_tl(t1, t1, 3);
7675 tcg_gen_movi_tl(t2, 0);
7676 label1 = gen_new_label();
7677 tcg_gen_brcond_tl(TCG_COND_GE, cpu_tmp0, t1, label1);
7678 tcg_gen_andi_tl(t0, t0, ~3);
7679 tcg_gen_or_tl(t0, t0, t1);
7680 tcg_gen_movi_tl(t2, CC_Z);
7681 gen_set_label(label1);
7682 if (mod != 3) {
7683 gen_op_st_v(s, ot, t0, a0);
7684 tcg_temp_free(a0);
7685 } else {
7686 gen_op_mov_reg_v(ot, rm, t0);
7688 gen_compute_eflags(s);
7689 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
7690 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
7691 tcg_temp_free(t0);
7692 tcg_temp_free(t1);
7693 tcg_temp_free(t2);
7695 break;
7696 case 0x102: /* lar */
7697 case 0x103: /* lsl */
7699 TCGLabel *label1;
7700 TCGv t0;
7701 if (!s->pe || s->vm86)
7702 goto illegal_op;
7703 ot = dflag != MO_16 ? MO_32 : MO_16;
7704 modrm = x86_ldub_code(env, s);
7705 reg = ((modrm >> 3) & 7) | rex_r;
7706 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7707 t0 = tcg_temp_local_new();
7708 gen_update_cc_op(s);
7709 if (b == 0x102) {
7710 gen_helper_lar(t0, cpu_env, cpu_T0);
7711 } else {
7712 gen_helper_lsl(t0, cpu_env, cpu_T0);
7714 tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
7715 label1 = gen_new_label();
7716 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
7717 gen_op_mov_reg_v(ot, reg, t0);
7718 gen_set_label(label1);
7719 set_cc_op(s, CC_OP_EFLAGS);
7720 tcg_temp_free(t0);
7722 break;
7723 case 0x118:
7724 modrm = x86_ldub_code(env, s);
7725 mod = (modrm >> 6) & 3;
7726 op = (modrm >> 3) & 7;
7727 switch(op) {
7728 case 0: /* prefetchnta */
7729 case 1: /* prefetchnt0 */
7730 case 2: /* prefetchnt0 */
7731 case 3: /* prefetchnt0 */
7732 if (mod == 3)
7733 goto illegal_op;
7734 gen_nop_modrm(env, s, modrm);
7735 /* nothing more to do */
7736 break;
7737 default: /* nop (multi byte) */
7738 gen_nop_modrm(env, s, modrm);
7739 break;
7741 break;
7742 case 0x11a:
7743 modrm = x86_ldub_code(env, s);
7744 if (s->flags & HF_MPX_EN_MASK) {
7745 mod = (modrm >> 6) & 3;
7746 reg = ((modrm >> 3) & 7) | rex_r;
7747 if (prefixes & PREFIX_REPZ) {
7748 /* bndcl */
7749 if (reg >= 4
7750 || (prefixes & PREFIX_LOCK)
7751 || s->aflag == MO_16) {
7752 goto illegal_op;
7754 gen_bndck(env, s, modrm, TCG_COND_LTU, cpu_bndl[reg]);
7755 } else if (prefixes & PREFIX_REPNZ) {
7756 /* bndcu */
7757 if (reg >= 4
7758 || (prefixes & PREFIX_LOCK)
7759 || s->aflag == MO_16) {
7760 goto illegal_op;
7762 TCGv_i64 notu = tcg_temp_new_i64();
7763 tcg_gen_not_i64(notu, cpu_bndu[reg]);
7764 gen_bndck(env, s, modrm, TCG_COND_GTU, notu);
7765 tcg_temp_free_i64(notu);
7766 } else if (prefixes & PREFIX_DATA) {
7767 /* bndmov -- from reg/mem */
7768 if (reg >= 4 || s->aflag == MO_16) {
7769 goto illegal_op;
7771 if (mod == 3) {
7772 int reg2 = (modrm & 7) | REX_B(s);
7773 if (reg2 >= 4 || (prefixes & PREFIX_LOCK)) {
7774 goto illegal_op;
7776 if (s->flags & HF_MPX_IU_MASK) {
7777 tcg_gen_mov_i64(cpu_bndl[reg], cpu_bndl[reg2]);
7778 tcg_gen_mov_i64(cpu_bndu[reg], cpu_bndu[reg2]);
7780 } else {
7781 gen_lea_modrm(env, s, modrm);
7782 if (CODE64(s)) {
7783 tcg_gen_qemu_ld_i64(cpu_bndl[reg], cpu_A0,
7784 s->mem_index, MO_LEQ);
7785 tcg_gen_addi_tl(cpu_A0, cpu_A0, 8);
7786 tcg_gen_qemu_ld_i64(cpu_bndu[reg], cpu_A0,
7787 s->mem_index, MO_LEQ);
7788 } else {
7789 tcg_gen_qemu_ld_i64(cpu_bndl[reg], cpu_A0,
7790 s->mem_index, MO_LEUL);
7791 tcg_gen_addi_tl(cpu_A0, cpu_A0, 4);
7792 tcg_gen_qemu_ld_i64(cpu_bndu[reg], cpu_A0,
7793 s->mem_index, MO_LEUL);
7795 /* bnd registers are now in-use */
7796 gen_set_hflag(s, HF_MPX_IU_MASK);
7798 } else if (mod != 3) {
7799 /* bndldx */
7800 AddressParts a = gen_lea_modrm_0(env, s, modrm);
7801 if (reg >= 4
7802 || (prefixes & PREFIX_LOCK)
7803 || s->aflag == MO_16
7804 || a.base < -1) {
7805 goto illegal_op;
7807 if (a.base >= 0) {
7808 tcg_gen_addi_tl(cpu_A0, cpu_regs[a.base], a.disp);
7809 } else {
7810 tcg_gen_movi_tl(cpu_A0, 0);
7812 gen_lea_v_seg(s, s->aflag, cpu_A0, a.def_seg, s->override);
7813 if (a.index >= 0) {
7814 tcg_gen_mov_tl(cpu_T0, cpu_regs[a.index]);
7815 } else {
7816 tcg_gen_movi_tl(cpu_T0, 0);
7818 if (CODE64(s)) {
7819 gen_helper_bndldx64(cpu_bndl[reg], cpu_env, cpu_A0, cpu_T0);
7820 tcg_gen_ld_i64(cpu_bndu[reg], cpu_env,
7821 offsetof(CPUX86State, mmx_t0.MMX_Q(0)));
7822 } else {
7823 gen_helper_bndldx32(cpu_bndu[reg], cpu_env, cpu_A0, cpu_T0);
7824 tcg_gen_ext32u_i64(cpu_bndl[reg], cpu_bndu[reg]);
7825 tcg_gen_shri_i64(cpu_bndu[reg], cpu_bndu[reg], 32);
7827 gen_set_hflag(s, HF_MPX_IU_MASK);
7830 gen_nop_modrm(env, s, modrm);
7831 break;
7832 case 0x11b:
7833 modrm = x86_ldub_code(env, s);
7834 if (s->flags & HF_MPX_EN_MASK) {
7835 mod = (modrm >> 6) & 3;
7836 reg = ((modrm >> 3) & 7) | rex_r;
7837 if (mod != 3 && (prefixes & PREFIX_REPZ)) {
7838 /* bndmk */
7839 if (reg >= 4
7840 || (prefixes & PREFIX_LOCK)
7841 || s->aflag == MO_16) {
7842 goto illegal_op;
7844 AddressParts a = gen_lea_modrm_0(env, s, modrm);
7845 if (a.base >= 0) {
7846 tcg_gen_extu_tl_i64(cpu_bndl[reg], cpu_regs[a.base]);
7847 if (!CODE64(s)) {
7848 tcg_gen_ext32u_i64(cpu_bndl[reg], cpu_bndl[reg]);
7850 } else if (a.base == -1) {
7851 /* no base register has lower bound of 0 */
7852 tcg_gen_movi_i64(cpu_bndl[reg], 0);
7853 } else {
7854 /* rip-relative generates #ud */
7855 goto illegal_op;
7857 tcg_gen_not_tl(cpu_A0, gen_lea_modrm_1(a));
7858 if (!CODE64(s)) {
7859 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
7861 tcg_gen_extu_tl_i64(cpu_bndu[reg], cpu_A0);
7862 /* bnd registers are now in-use */
7863 gen_set_hflag(s, HF_MPX_IU_MASK);
7864 break;
7865 } else if (prefixes & PREFIX_REPNZ) {
7866 /* bndcn */
7867 if (reg >= 4
7868 || (prefixes & PREFIX_LOCK)
7869 || s->aflag == MO_16) {
7870 goto illegal_op;
7872 gen_bndck(env, s, modrm, TCG_COND_GTU, cpu_bndu[reg]);
7873 } else if (prefixes & PREFIX_DATA) {
7874 /* bndmov -- to reg/mem */
7875 if (reg >= 4 || s->aflag == MO_16) {
7876 goto illegal_op;
7878 if (mod == 3) {
7879 int reg2 = (modrm & 7) | REX_B(s);
7880 if (reg2 >= 4 || (prefixes & PREFIX_LOCK)) {
7881 goto illegal_op;
7883 if (s->flags & HF_MPX_IU_MASK) {
7884 tcg_gen_mov_i64(cpu_bndl[reg2], cpu_bndl[reg]);
7885 tcg_gen_mov_i64(cpu_bndu[reg2], cpu_bndu[reg]);
7887 } else {
7888 gen_lea_modrm(env, s, modrm);
7889 if (CODE64(s)) {
7890 tcg_gen_qemu_st_i64(cpu_bndl[reg], cpu_A0,
7891 s->mem_index, MO_LEQ);
7892 tcg_gen_addi_tl(cpu_A0, cpu_A0, 8);
7893 tcg_gen_qemu_st_i64(cpu_bndu[reg], cpu_A0,
7894 s->mem_index, MO_LEQ);
7895 } else {
7896 tcg_gen_qemu_st_i64(cpu_bndl[reg], cpu_A0,
7897 s->mem_index, MO_LEUL);
7898 tcg_gen_addi_tl(cpu_A0, cpu_A0, 4);
7899 tcg_gen_qemu_st_i64(cpu_bndu[reg], cpu_A0,
7900 s->mem_index, MO_LEUL);
7903 } else if (mod != 3) {
7904 /* bndstx */
7905 AddressParts a = gen_lea_modrm_0(env, s, modrm);
7906 if (reg >= 4
7907 || (prefixes & PREFIX_LOCK)
7908 || s->aflag == MO_16
7909 || a.base < -1) {
7910 goto illegal_op;
7912 if (a.base >= 0) {
7913 tcg_gen_addi_tl(cpu_A0, cpu_regs[a.base], a.disp);
7914 } else {
7915 tcg_gen_movi_tl(cpu_A0, 0);
7917 gen_lea_v_seg(s, s->aflag, cpu_A0, a.def_seg, s->override);
7918 if (a.index >= 0) {
7919 tcg_gen_mov_tl(cpu_T0, cpu_regs[a.index]);
7920 } else {
7921 tcg_gen_movi_tl(cpu_T0, 0);
7923 if (CODE64(s)) {
7924 gen_helper_bndstx64(cpu_env, cpu_A0, cpu_T0,
7925 cpu_bndl[reg], cpu_bndu[reg]);
7926 } else {
7927 gen_helper_bndstx32(cpu_env, cpu_A0, cpu_T0,
7928 cpu_bndl[reg], cpu_bndu[reg]);
7932 gen_nop_modrm(env, s, modrm);
7933 break;
7934 case 0x119: case 0x11c ... 0x11f: /* nop (multi byte) */
7935 modrm = x86_ldub_code(env, s);
7936 gen_nop_modrm(env, s, modrm);
7937 break;
7938 case 0x120: /* mov reg, crN */
7939 case 0x122: /* mov crN, reg */
7940 if (s->cpl != 0) {
7941 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7942 } else {
7943 modrm = x86_ldub_code(env, s);
7944 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7945 * AMD documentation (24594.pdf) and testing of
7946 * intel 386 and 486 processors all show that the mod bits
7947 * are assumed to be 1's, regardless of actual values.
7949 rm = (modrm & 7) | REX_B(s);
7950 reg = ((modrm >> 3) & 7) | rex_r;
7951 if (CODE64(s))
7952 ot = MO_64;
7953 else
7954 ot = MO_32;
7955 if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
7956 (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
7957 reg = 8;
7959 switch(reg) {
7960 case 0:
7961 case 2:
7962 case 3:
7963 case 4:
7964 case 8:
7965 gen_update_cc_op(s);
7966 gen_jmp_im(pc_start - s->cs_base);
7967 if (b & 2) {
7968 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7969 gen_io_start();
7971 gen_op_mov_v_reg(ot, cpu_T0, rm);
7972 gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
7973 cpu_T0);
7974 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7975 gen_io_end();
7977 gen_jmp_im(s->pc - s->cs_base);
7978 gen_eob(s);
7979 } else {
7980 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7981 gen_io_start();
7983 gen_helper_read_crN(cpu_T0, cpu_env, tcg_const_i32(reg));
7984 gen_op_mov_reg_v(ot, rm, cpu_T0);
7985 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
7986 gen_io_end();
7989 break;
7990 default:
7991 goto unknown_op;
7994 break;
7995 case 0x121: /* mov reg, drN */
7996 case 0x123: /* mov drN, reg */
7997 if (s->cpl != 0) {
7998 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7999 } else {
8000 modrm = x86_ldub_code(env, s);
8001 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
8002 * AMD documentation (24594.pdf) and testing of
8003 * intel 386 and 486 processors all show that the mod bits
8004 * are assumed to be 1's, regardless of actual values.
8006 rm = (modrm & 7) | REX_B(s);
8007 reg = ((modrm >> 3) & 7) | rex_r;
8008 if (CODE64(s))
8009 ot = MO_64;
8010 else
8011 ot = MO_32;
8012 if (reg >= 8) {
8013 goto illegal_op;
8015 if (b & 2) {
8016 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
8017 gen_op_mov_v_reg(ot, cpu_T0, rm);
8018 tcg_gen_movi_i32(cpu_tmp2_i32, reg);
8019 gen_helper_set_dr(cpu_env, cpu_tmp2_i32, cpu_T0);
8020 gen_jmp_im(s->pc - s->cs_base);
8021 gen_eob(s);
8022 } else {
8023 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
8024 tcg_gen_movi_i32(cpu_tmp2_i32, reg);
8025 gen_helper_get_dr(cpu_T0, cpu_env, cpu_tmp2_i32);
8026 gen_op_mov_reg_v(ot, rm, cpu_T0);
8029 break;
8030 case 0x106: /* clts */
8031 if (s->cpl != 0) {
8032 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
8033 } else {
8034 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
8035 gen_helper_clts(cpu_env);
8036 /* abort block because static cpu state changed */
8037 gen_jmp_im(s->pc - s->cs_base);
8038 gen_eob(s);
8040 break;
8041 /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
8042 case 0x1c3: /* MOVNTI reg, mem */
8043 if (!(s->cpuid_features & CPUID_SSE2))
8044 goto illegal_op;
8045 ot = mo_64_32(dflag);
8046 modrm = x86_ldub_code(env, s);
8047 mod = (modrm >> 6) & 3;
8048 if (mod == 3)
8049 goto illegal_op;
8050 reg = ((modrm >> 3) & 7) | rex_r;
8051 /* generate a generic store */
8052 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
8053 break;
8054 case 0x1ae:
8055 modrm = x86_ldub_code(env, s);
8056 switch (modrm) {
8057 CASE_MODRM_MEM_OP(0): /* fxsave */
8058 if (!(s->cpuid_features & CPUID_FXSR)
8059 || (prefixes & PREFIX_LOCK)) {
8060 goto illegal_op;
8062 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
8063 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
8064 break;
8066 gen_lea_modrm(env, s, modrm);
8067 gen_helper_fxsave(cpu_env, cpu_A0);
8068 break;
8070 CASE_MODRM_MEM_OP(1): /* fxrstor */
8071 if (!(s->cpuid_features & CPUID_FXSR)
8072 || (prefixes & PREFIX_LOCK)) {
8073 goto illegal_op;
8075 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
8076 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
8077 break;
8079 gen_lea_modrm(env, s, modrm);
8080 gen_helper_fxrstor(cpu_env, cpu_A0);
8081 break;
8083 CASE_MODRM_MEM_OP(2): /* ldmxcsr */
8084 if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK)) {
8085 goto illegal_op;
8087 if (s->flags & HF_TS_MASK) {
8088 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
8089 break;
8091 gen_lea_modrm(env, s, modrm);
8092 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0, s->mem_index, MO_LEUL);
8093 gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
8094 break;
8096 CASE_MODRM_MEM_OP(3): /* stmxcsr */
8097 if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK)) {
8098 goto illegal_op;
8100 if (s->flags & HF_TS_MASK) {
8101 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
8102 break;
8104 gen_lea_modrm(env, s, modrm);
8105 tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State, mxcsr));
8106 gen_op_st_v(s, MO_32, cpu_T0, cpu_A0);
8107 break;
8109 CASE_MODRM_MEM_OP(4): /* xsave */
8110 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
8111 || (prefixes & (PREFIX_LOCK | PREFIX_DATA
8112 | PREFIX_REPZ | PREFIX_REPNZ))) {
8113 goto illegal_op;
8115 gen_lea_modrm(env, s, modrm);
8116 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
8117 cpu_regs[R_EDX]);
8118 gen_helper_xsave(cpu_env, cpu_A0, cpu_tmp1_i64);
8119 break;
8121 CASE_MODRM_MEM_OP(5): /* xrstor */
8122 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
8123 || (prefixes & (PREFIX_LOCK | PREFIX_DATA
8124 | PREFIX_REPZ | PREFIX_REPNZ))) {
8125 goto illegal_op;
8127 gen_lea_modrm(env, s, modrm);
8128 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
8129 cpu_regs[R_EDX]);
8130 gen_helper_xrstor(cpu_env, cpu_A0, cpu_tmp1_i64);
8131 /* XRSTOR is how MPX is enabled, which changes how
8132 we translate. Thus we need to end the TB. */
8133 gen_update_cc_op(s);
8134 gen_jmp_im(s->pc - s->cs_base);
8135 gen_eob(s);
8136 break;
8138 CASE_MODRM_MEM_OP(6): /* xsaveopt / clwb */
8139 if (prefixes & PREFIX_LOCK) {
8140 goto illegal_op;
8142 if (prefixes & PREFIX_DATA) {
8143 /* clwb */
8144 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_CLWB)) {
8145 goto illegal_op;
8147 gen_nop_modrm(env, s, modrm);
8148 } else {
8149 /* xsaveopt */
8150 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
8151 || (s->cpuid_xsave_features & CPUID_XSAVE_XSAVEOPT) == 0
8152 || (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))) {
8153 goto illegal_op;
8155 gen_lea_modrm(env, s, modrm);
8156 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
8157 cpu_regs[R_EDX]);
8158 gen_helper_xsaveopt(cpu_env, cpu_A0, cpu_tmp1_i64);
8160 break;
8162 CASE_MODRM_MEM_OP(7): /* clflush / clflushopt */
8163 if (prefixes & PREFIX_LOCK) {
8164 goto illegal_op;
8166 if (prefixes & PREFIX_DATA) {
8167 /* clflushopt */
8168 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_CLFLUSHOPT)) {
8169 goto illegal_op;
8171 } else {
8172 /* clflush */
8173 if ((s->prefix & (PREFIX_REPZ | PREFIX_REPNZ))
8174 || !(s->cpuid_features & CPUID_CLFLUSH)) {
8175 goto illegal_op;
8178 gen_nop_modrm(env, s, modrm);
8179 break;
8181 case 0xc0 ... 0xc7: /* rdfsbase (f3 0f ae /0) */
8182 case 0xc8 ... 0xcf: /* rdgsbase (f3 0f ae /1) */
8183 case 0xd0 ... 0xd7: /* wrfsbase (f3 0f ae /2) */
8184 case 0xd8 ... 0xdf: /* wrgsbase (f3 0f ae /3) */
8185 if (CODE64(s)
8186 && (prefixes & PREFIX_REPZ)
8187 && !(prefixes & PREFIX_LOCK)
8188 && (s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_FSGSBASE)) {
8189 TCGv base, treg, src, dst;
8191 /* Preserve hflags bits by testing CR4 at runtime. */
8192 tcg_gen_movi_i32(cpu_tmp2_i32, CR4_FSGSBASE_MASK);
8193 gen_helper_cr4_testbit(cpu_env, cpu_tmp2_i32);
8195 base = cpu_seg_base[modrm & 8 ? R_GS : R_FS];
8196 treg = cpu_regs[(modrm & 7) | REX_B(s)];
8198 if (modrm & 0x10) {
8199 /* wr*base */
8200 dst = base, src = treg;
8201 } else {
8202 /* rd*base */
8203 dst = treg, src = base;
8206 if (s->dflag == MO_32) {
8207 tcg_gen_ext32u_tl(dst, src);
8208 } else {
8209 tcg_gen_mov_tl(dst, src);
8211 break;
8213 goto unknown_op;
8215 case 0xf8: /* sfence / pcommit */
8216 if (prefixes & PREFIX_DATA) {
8217 /* pcommit */
8218 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_PCOMMIT)
8219 || (prefixes & PREFIX_LOCK)) {
8220 goto illegal_op;
8222 break;
8224 /* fallthru */
8225 case 0xf9 ... 0xff: /* sfence */
8226 if (!(s->cpuid_features & CPUID_SSE)
8227 || (prefixes & PREFIX_LOCK)) {
8228 goto illegal_op;
8230 tcg_gen_mb(TCG_MO_ST_ST | TCG_BAR_SC);
8231 break;
8232 case 0xe8 ... 0xef: /* lfence */
8233 if (!(s->cpuid_features & CPUID_SSE)
8234 || (prefixes & PREFIX_LOCK)) {
8235 goto illegal_op;
8237 tcg_gen_mb(TCG_MO_LD_LD | TCG_BAR_SC);
8238 break;
8239 case 0xf0 ... 0xf7: /* mfence */
8240 if (!(s->cpuid_features & CPUID_SSE2)
8241 || (prefixes & PREFIX_LOCK)) {
8242 goto illegal_op;
8244 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
8245 break;
8247 default:
8248 goto unknown_op;
8250 break;
8252 case 0x10d: /* 3DNow! prefetch(w) */
8253 modrm = x86_ldub_code(env, s);
8254 mod = (modrm >> 6) & 3;
8255 if (mod == 3)
8256 goto illegal_op;
8257 gen_nop_modrm(env, s, modrm);
8258 break;
8259 case 0x1aa: /* rsm */
8260 gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
8261 if (!(s->flags & HF_SMM_MASK))
8262 goto illegal_op;
8263 gen_update_cc_op(s);
8264 gen_jmp_im(s->pc - s->cs_base);
8265 gen_helper_rsm(cpu_env);
8266 gen_eob(s);
8267 break;
8268 case 0x1b8: /* SSE4.2 popcnt */
8269 if ((prefixes & (PREFIX_REPZ | PREFIX_LOCK | PREFIX_REPNZ)) !=
8270 PREFIX_REPZ)
8271 goto illegal_op;
8272 if (!(s->cpuid_ext_features & CPUID_EXT_POPCNT))
8273 goto illegal_op;
8275 modrm = x86_ldub_code(env, s);
8276 reg = ((modrm >> 3) & 7) | rex_r;
8278 if (s->prefix & PREFIX_DATA) {
8279 ot = MO_16;
8280 } else {
8281 ot = mo_64_32(dflag);
8284 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
8285 gen_extu(ot, cpu_T0);
8286 tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
8287 tcg_gen_ctpop_tl(cpu_T0, cpu_T0);
8288 gen_op_mov_reg_v(ot, reg, cpu_T0);
8290 set_cc_op(s, CC_OP_POPCNT);
8291 break;
8292 case 0x10e ... 0x10f:
8293 /* 3DNow! instructions, ignore prefixes */
8294 s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
8295 case 0x110 ... 0x117:
8296 case 0x128 ... 0x12f:
8297 case 0x138 ... 0x13a:
8298 case 0x150 ... 0x179:
8299 case 0x17c ... 0x17f:
8300 case 0x1c2:
8301 case 0x1c4 ... 0x1c6:
8302 case 0x1d0 ... 0x1fe:
8303 gen_sse(env, s, b, pc_start, rex_r);
8304 break;
8305 default:
8306 goto unknown_op;
8308 return s->pc;
8309 illegal_op:
8310 gen_illegal_opcode(s);
8311 return s->pc;
8312 unknown_op:
8313 gen_unknown_opcode(env, s);
8314 return s->pc;
8317 void tcg_x86_init(void)
8319 static const char reg_names[CPU_NB_REGS][4] = {
8320 #ifdef TARGET_X86_64
8321 [R_EAX] = "rax",
8322 [R_EBX] = "rbx",
8323 [R_ECX] = "rcx",
8324 [R_EDX] = "rdx",
8325 [R_ESI] = "rsi",
8326 [R_EDI] = "rdi",
8327 [R_EBP] = "rbp",
8328 [R_ESP] = "rsp",
8329 [8] = "r8",
8330 [9] = "r9",
8331 [10] = "r10",
8332 [11] = "r11",
8333 [12] = "r12",
8334 [13] = "r13",
8335 [14] = "r14",
8336 [15] = "r15",
8337 #else
8338 [R_EAX] = "eax",
8339 [R_EBX] = "ebx",
8340 [R_ECX] = "ecx",
8341 [R_EDX] = "edx",
8342 [R_ESI] = "esi",
8343 [R_EDI] = "edi",
8344 [R_EBP] = "ebp",
8345 [R_ESP] = "esp",
8346 #endif
8348 static const char seg_base_names[6][8] = {
8349 [R_CS] = "cs_base",
8350 [R_DS] = "ds_base",
8351 [R_ES] = "es_base",
8352 [R_FS] = "fs_base",
8353 [R_GS] = "gs_base",
8354 [R_SS] = "ss_base",
8356 static const char bnd_regl_names[4][8] = {
8357 "bnd0_lb", "bnd1_lb", "bnd2_lb", "bnd3_lb"
8359 static const char bnd_regu_names[4][8] = {
8360 "bnd0_ub", "bnd1_ub", "bnd2_ub", "bnd3_ub"
8362 int i;
8364 cpu_cc_op = tcg_global_mem_new_i32(cpu_env,
8365 offsetof(CPUX86State, cc_op), "cc_op");
8366 cpu_cc_dst = tcg_global_mem_new(cpu_env, offsetof(CPUX86State, cc_dst),
8367 "cc_dst");
8368 cpu_cc_src = tcg_global_mem_new(cpu_env, offsetof(CPUX86State, cc_src),
8369 "cc_src");
8370 cpu_cc_src2 = tcg_global_mem_new(cpu_env, offsetof(CPUX86State, cc_src2),
8371 "cc_src2");
8373 for (i = 0; i < CPU_NB_REGS; ++i) {
8374 cpu_regs[i] = tcg_global_mem_new(cpu_env,
8375 offsetof(CPUX86State, regs[i]),
8376 reg_names[i]);
8379 for (i = 0; i < 6; ++i) {
8380 cpu_seg_base[i]
8381 = tcg_global_mem_new(cpu_env,
8382 offsetof(CPUX86State, segs[i].base),
8383 seg_base_names[i]);
8386 for (i = 0; i < 4; ++i) {
8387 cpu_bndl[i]
8388 = tcg_global_mem_new_i64(cpu_env,
8389 offsetof(CPUX86State, bnd_regs[i].lb),
8390 bnd_regl_names[i]);
8391 cpu_bndu[i]
8392 = tcg_global_mem_new_i64(cpu_env,
8393 offsetof(CPUX86State, bnd_regs[i].ub),
8394 bnd_regu_names[i]);
8398 static void i386_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cpu)
8400 DisasContext *dc = container_of(dcbase, DisasContext, base);
8401 CPUX86State *env = cpu->env_ptr;
8402 uint32_t flags = dc->base.tb->flags;
8403 target_ulong cs_base = dc->base.tb->cs_base;
8405 dc->pe = (flags >> HF_PE_SHIFT) & 1;
8406 dc->code32 = (flags >> HF_CS32_SHIFT) & 1;
8407 dc->ss32 = (flags >> HF_SS32_SHIFT) & 1;
8408 dc->addseg = (flags >> HF_ADDSEG_SHIFT) & 1;
8409 dc->f_st = 0;
8410 dc->vm86 = (flags >> VM_SHIFT) & 1;
8411 dc->cpl = (flags >> HF_CPL_SHIFT) & 3;
8412 dc->iopl = (flags >> IOPL_SHIFT) & 3;
8413 dc->tf = (flags >> TF_SHIFT) & 1;
8414 dc->cc_op = CC_OP_DYNAMIC;
8415 dc->cc_op_dirty = false;
8416 dc->cs_base = cs_base;
8417 dc->popl_esp_hack = 0;
8418 /* select memory access functions */
8419 dc->mem_index = 0;
8420 #ifdef CONFIG_SOFTMMU
8421 dc->mem_index = cpu_mmu_index(env, false);
8422 #endif
8423 dc->cpuid_features = env->features[FEAT_1_EDX];
8424 dc->cpuid_ext_features = env->features[FEAT_1_ECX];
8425 dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
8426 dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
8427 dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];
8428 dc->cpuid_xsave_features = env->features[FEAT_XSAVE];
8429 #ifdef TARGET_X86_64
8430 dc->lma = (flags >> HF_LMA_SHIFT) & 1;
8431 dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
8432 #endif
8433 dc->flags = flags;
8434 dc->jmp_opt = !(dc->tf || dc->base.singlestep_enabled ||
8435 (flags & HF_INHIBIT_IRQ_MASK));
8436 /* Do not optimize repz jumps at all in icount mode, because
8437 rep movsS instructions are execured with different paths
8438 in !repz_opt and repz_opt modes. The first one was used
8439 always except single step mode. And this setting
8440 disables jumps optimization and control paths become
8441 equivalent in run and single step modes.
8442 Now there will be no jump optimization for repz in
8443 record/replay modes and there will always be an
8444 additional step for ecx=0 when icount is enabled.
8446 dc->repz_opt = !dc->jmp_opt && !(tb_cflags(dc->base.tb) & CF_USE_ICOUNT);
8447 #if 0
8448 /* check addseg logic */
8449 if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
8450 printf("ERROR addseg\n");
8451 #endif
8453 cpu_T0 = tcg_temp_new();
8454 cpu_T1 = tcg_temp_new();
8455 cpu_A0 = tcg_temp_new();
8457 cpu_tmp0 = tcg_temp_new();
8458 cpu_tmp1_i64 = tcg_temp_new_i64();
8459 cpu_tmp2_i32 = tcg_temp_new_i32();
8460 cpu_tmp3_i32 = tcg_temp_new_i32();
8461 cpu_tmp4 = tcg_temp_new();
8462 cpu_ptr0 = tcg_temp_new_ptr();
8463 cpu_ptr1 = tcg_temp_new_ptr();
8464 cpu_cc_srcT = tcg_temp_local_new();
8467 static void i386_tr_tb_start(DisasContextBase *db, CPUState *cpu)
8471 static void i386_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
8473 DisasContext *dc = container_of(dcbase, DisasContext, base);
8475 tcg_gen_insn_start(dc->base.pc_next, dc->cc_op);
8478 static bool i386_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
8479 const CPUBreakpoint *bp)
8481 DisasContext *dc = container_of(dcbase, DisasContext, base);
8482 /* If RF is set, suppress an internally generated breakpoint. */
8483 int flags = dc->base.tb->flags & HF_RF_MASK ? BP_GDB : BP_ANY;
8484 if (bp->flags & flags) {
8485 gen_debug(dc, dc->base.pc_next - dc->cs_base);
8486 dc->base.is_jmp = DISAS_NORETURN;
8487 /* The address covered by the breakpoint must be included in
8488 [tb->pc, tb->pc + tb->size) in order to for it to be
8489 properly cleared -- thus we increment the PC here so that
8490 the generic logic setting tb->size later does the right thing. */
8491 dc->base.pc_next += 1;
8492 return true;
8493 } else {
8494 return false;
8498 static void i386_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
8500 DisasContext *dc = container_of(dcbase, DisasContext, base);
8501 target_ulong pc_next = disas_insn(dc, cpu);
8503 if (dc->tf || (dc->base.tb->flags & HF_INHIBIT_IRQ_MASK)) {
8504 /* if single step mode, we generate only one instruction and
8505 generate an exception */
8506 /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
8507 the flag and abort the translation to give the irqs a
8508 chance to happen */
8509 dc->base.is_jmp = DISAS_TOO_MANY;
8510 } else if ((tb_cflags(dc->base.tb) & CF_USE_ICOUNT)
8511 && ((dc->base.pc_next & TARGET_PAGE_MASK)
8512 != ((dc->base.pc_next + TARGET_MAX_INSN_SIZE - 1)
8513 & TARGET_PAGE_MASK)
8514 || (dc->base.pc_next & ~TARGET_PAGE_MASK) == 0)) {
8515 /* Do not cross the boundary of the pages in icount mode,
8516 it can cause an exception. Do it only when boundary is
8517 crossed by the first instruction in the block.
8518 If current instruction already crossed the bound - it's ok,
8519 because an exception hasn't stopped this code.
8521 dc->base.is_jmp = DISAS_TOO_MANY;
8522 } else if ((pc_next - dc->base.pc_first) >= (TARGET_PAGE_SIZE - 32)) {
8523 dc->base.is_jmp = DISAS_TOO_MANY;
8526 dc->base.pc_next = pc_next;
8529 static void i386_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
8531 DisasContext *dc = container_of(dcbase, DisasContext, base);
8533 if (dc->base.is_jmp == DISAS_TOO_MANY) {
8534 gen_jmp_im(dc->base.pc_next - dc->cs_base);
8535 gen_eob(dc);
8539 static void i386_tr_disas_log(const DisasContextBase *dcbase,
8540 CPUState *cpu)
8542 DisasContext *dc = container_of(dcbase, DisasContext, base);
8544 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
8545 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
8548 static const TranslatorOps i386_tr_ops = {
8549 .init_disas_context = i386_tr_init_disas_context,
8550 .tb_start = i386_tr_tb_start,
8551 .insn_start = i386_tr_insn_start,
8552 .breakpoint_check = i386_tr_breakpoint_check,
8553 .translate_insn = i386_tr_translate_insn,
8554 .tb_stop = i386_tr_tb_stop,
8555 .disas_log = i386_tr_disas_log,
8558 /* generate intermediate code for basic block 'tb'. */
8559 void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb)
8561 DisasContext dc;
8563 translator_loop(&i386_tr_ops, &dc.base, cpu, tb);
8566 void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb,
8567 target_ulong *data)
8569 int cc_op = data[1];
8570 env->eip = data[0] - tb->cs_base;
8571 if (cc_op != CC_OP_DYNAMIC) {
8572 env->cc_op = cc_op;