]> git.proxmox.com Git - mirror_qemu.git/blame - target/i386/translate.c
KVM: x86: Add support for save/load MSR_SMI_COUNT
[mirror_qemu.git] / target / i386 / translate.c
CommitLineData
2c0262af
FB
1/*
2 * i386 translation
5fafdf24 3 *
2c0262af
FB
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
2c0262af 18 */
b6a0aa05 19#include "qemu/osdep.h"
2c0262af 20
bec93d72 21#include "qemu/host-utils.h"
2c0262af 22#include "cpu.h"
76cad711 23#include "disas/disas.h"
63c91552 24#include "exec/exec-all.h"
57fec1fe 25#include "tcg-op.h"
f08b6170 26#include "exec/cpu_ldst.h"
77fc6f5e 27#include "exec/translator.h"
2c0262af 28
2ef6175a
RH
29#include "exec/helper-proto.h"
30#include "exec/helper-gen.h"
a7812ae4 31
a7e30d84 32#include "trace-tcg.h"
508127e2 33#include "exec/log.h"
a7e30d84 34
2c0262af
FB
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
701ed211 40#define PREFIX_VEX 0x20
2c0262af 41
14ce26e7 42#ifdef TARGET_X86_64
14ce26e7
FB
43#define CODE64(s) ((s)->code64)
44#define REX_X(s) ((s)->rex_x)
45#define REX_B(s) ((s)->rex_b)
14ce26e7 46#else
14ce26e7
FB
47#define CODE64(s) 0
48#define REX_X(s) 0
49#define REX_B(s) 0
50#endif
51
bec93d72
RH
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
59
1906b2af 60/* For a switch indexed by MODRM, match all memory operands for a given OP. */
880f8486 61#define CASE_MODRM_MEM_OP(OP) \
1906b2af
RH
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
65
880f8486
PB
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
71
57fec1fe
FB
72//#define MACRO_TEST 1
73
57fec1fe 74/* global register indexes */
a3251186 75static TCGv cpu_A0;
988c3eb0 76static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT;
a7812ae4 77static TCGv_i32 cpu_cc_op;
cc739bb0 78static TCGv cpu_regs[CPU_NB_REGS];
3558f805 79static TCGv cpu_seg_base[6];
149b427b
RH
80static TCGv_i64 cpu_bndl[4];
81static TCGv_i64 cpu_bndu[4];
1e4840bf 82/* local temps */
1d1cc4d0 83static TCGv cpu_T0, cpu_T1;
57fec1fe 84/* local register indexes (only used inside old micro ops) */
a7812ae4
PB
85static TCGv cpu_tmp0, cpu_tmp4;
86static TCGv_ptr cpu_ptr0, cpu_ptr1;
87static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
88static TCGv_i64 cpu_tmp1_i64;
57fec1fe 89
022c62cb 90#include "exec/gen-icount.h"
2e70f6ef 91
57fec1fe
FB
92#ifdef TARGET_X86_64
93static int x86_64_hregs;
ae063a68
FB
94#endif
95
2c0262af 96typedef struct DisasContext {
6cf147aa
LV
97 DisasContextBase base;
98
2c0262af
FB
99 /* current insn context */
100 int override; /* -1 if no override */
101 int prefix;
1d71ddb1 102 TCGMemOp aflag;
ab4e4aec 103 TCGMemOp dflag;
b9f9c5b4 104 target_ulong pc_start;
14ce26e7 105 target_ulong pc; /* pc = eip + cs_base */
2c0262af 106 /* current block context */
14ce26e7 107 target_ulong cs_base; /* base of CS segment */
2c0262af
FB
108 int pe; /* protected mode */
109 int code32; /* 32 bit code segment */
14ce26e7
FB
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
701ed211
RH
115 int vex_l; /* vex vector length */
116 int vex_v; /* vex vvvv register, without 1's compliment. */
2c0262af 117 int ss32; /* 32 bit stack segment */
fee71888 118 CCOp cc_op; /* current CC operation */
e207582f 119 bool cc_op_dirty;
2c0262af
FB
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 */
c4d4525c 127 int repz_opt; /* optimize jumps within repz instructions */
2c0262af 128 int mem_index; /* select memory access functions */
c068688b 129 uint64_t flags; /* all execution flags */
2c0262af 130 int popl_esp_hack; /* for correct popl with esp base handling */
14ce26e7
FB
131 int rip_offset; /* only used in x86_64, but left for simplicity */
132 int cpuid_features;
3d7374c5 133 int cpuid_ext_features;
e771edab 134 int cpuid_ext2_features;
12e26b75 135 int cpuid_ext3_features;
a9321a4d 136 int cpuid_7_0_ebx_features;
c9cfe8f9 137 int cpuid_xsave_features;
b066c537 138 sigjmp_buf jmpbuf;
2c0262af
FB
139} DisasContext;
140
141static void gen_eob(DisasContext *s);
1ebb1af1 142static void gen_jr(DisasContext *s, TCGv dest);
14ce26e7
FB
143static void gen_jmp(DisasContext *s, target_ulong eip);
144static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
d67dc9e6 145static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d);
2c0262af
FB
146
147/* i386 arith/logic operations */
148enum {
5fafdf24
TS
149 OP_ADDL,
150 OP_ORL,
151 OP_ADCL,
2c0262af 152 OP_SBBL,
5fafdf24
TS
153 OP_ANDL,
154 OP_SUBL,
155 OP_XORL,
2c0262af
FB
156 OP_CMPL,
157};
158
159/* i386 shift ops */
160enum {
5fafdf24
TS
161 OP_ROL,
162 OP_ROR,
163 OP_RCL,
164 OP_RCR,
165 OP_SHL,
166 OP_SHR,
2c0262af
FB
167 OP_SHL1, /* undocumented */
168 OP_SAR = 7,
169};
170
8e1c85e3
FB
171enum {
172 JCC_O,
173 JCC_B,
174 JCC_Z,
175 JCC_BE,
176 JCC_S,
177 JCC_P,
178 JCC_L,
179 JCC_LE,
180};
181
2c0262af
FB
182enum {
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,
14ce26e7
FB
192
193 OR_TMP0 = 16, /* temporary operand register */
2c0262af
FB
194 OR_TMP1,
195 OR_A0, /* temporary register used when doing address evaluation */
2c0262af
FB
196};
197
b666265b 198enum {
a3251186
RH
199 USES_CC_DST = 1,
200 USES_CC_SRC = 2,
988c3eb0
RH
201 USES_CC_SRC2 = 4,
202 USES_CC_SRCT = 8,
b666265b
RH
203};
204
205/* Bit set if the global variable is live after setting CC_OP to X. */
206static const uint8_t cc_op_live[CC_OP_NB] = {
988c3eb0 207 [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
b666265b
RH
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,
988c3eb0 211 [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
a3251186 212 [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRCT,
988c3eb0 213 [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
b666265b
RH
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,
bc4b43dc 219 [CC_OP_BMILGB ... CC_OP_BMILGQ] = USES_CC_DST | USES_CC_SRC,
cd7f97ca
RH
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,
436ff2d2 223 [CC_OP_CLR] = 0,
4885c3c4 224 [CC_OP_POPCNT] = USES_CC_SRC,
b666265b
RH
225};
226
e207582f 227static void set_cc_op(DisasContext *s, CCOp op)
3ca51d07 228{
b666265b
RH
229 int dead;
230
231 if (s->cc_op == op) {
232 return;
233 }
234
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);
e207582f 239 }
b666265b
RH
240 if (dead & USES_CC_SRC) {
241 tcg_gen_discard_tl(cpu_cc_src);
242 }
988c3eb0
RH
243 if (dead & USES_CC_SRC2) {
244 tcg_gen_discard_tl(cpu_cc_src2);
245 }
a3251186
RH
246 if (dead & USES_CC_SRCT) {
247 tcg_gen_discard_tl(cpu_cc_srcT);
248 }
b666265b 249
e2f515cf
RH
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);
258 }
259 s->cc_op_dirty = true;
260 }
b666265b 261 s->cc_op = op;
e207582f
RH
262}
263
e207582f
RH
264static void gen_update_cc_op(DisasContext *s)
265{
266 if (s->cc_op_dirty) {
773cdfcc 267 tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
e207582f
RH
268 s->cc_op_dirty = false;
269 }
3ca51d07
RH
270}
271
14ce26e7
FB
272#ifdef TARGET_X86_64
273
274#define NB_OP_SIZES 4
275
14ce26e7
FB
276#else /* !TARGET_X86_64 */
277
278#define NB_OP_SIZES 3
279
14ce26e7
FB
280#endif /* !TARGET_X86_64 */
281
e2542fe2 282#if defined(HOST_WORDS_BIGENDIAN)
57fec1fe
FB
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)
14ce26e7 288#else
57fec1fe
FB
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
14ce26e7 294#endif
57fec1fe 295
96d7073f
PM
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.
301 */
302static inline bool byte_reg_is_xH(int reg)
303{
304 if (reg < 4) {
305 return false;
306 }
307#ifdef TARGET_X86_64
308 if (reg >= 8 || x86_64_hregs) {
309 return false;
310 }
311#endif
312 return true;
313}
314
ab4e4aec
RH
315/* Select the size of a push/pop operation. */
316static inline TCGMemOp mo_pushpop(DisasContext *s, TCGMemOp ot)
317{
318 if (CODE64(s)) {
319 return ot == MO_16 ? MO_16 : MO_64;
320 } else {
321 return ot;
322 }
323}
324
64ae256c
RH
325/* Select the size of the stack pointer. */
326static inline TCGMemOp mo_stacksize(DisasContext *s)
327{
328 return CODE64(s) ? MO_64 : s->ss32 ? MO_32 : MO_16;
329}
330
ab4e4aec
RH
331/* Select only size 64 else 32. Used for SSE operand sizes. */
332static inline TCGMemOp mo_64_32(TCGMemOp ot)
333{
334#ifdef TARGET_X86_64
335 return ot == MO_64 ? MO_64 : MO_32;
336#else
337 return MO_32;
338#endif
339}
340
341/* Select size 8 if lsb of B is clear, else OT. Used for decoding
342 byte vs word opcodes. */
343static inline TCGMemOp mo_b_d(int b, TCGMemOp ot)
344{
345 return b & 1 ? ot : MO_8;
346}
347
348/* Select size 8 if lsb of B is clear, else OT capped at 32.
349 Used for decoding operand size of port opcodes. */
350static inline TCGMemOp mo_b_d32(int b, TCGMemOp ot)
351{
352 return b & 1 ? (ot == MO_16 ? MO_16 : MO_32) : MO_8;
353}
354
d67dc9e6 355static void gen_op_mov_reg_v(TCGMemOp ot, int reg, TCGv t0)
57fec1fe
FB
356{
357 switch(ot) {
4ba9938c 358 case MO_8:
96d7073f 359 if (!byte_reg_is_xH(reg)) {
c832e3de 360 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
57fec1fe 361 } else {
c832e3de 362 tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
57fec1fe
FB
363 }
364 break;
4ba9938c 365 case MO_16:
c832e3de 366 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
57fec1fe 367 break;
4ba9938c 368 case MO_32:
cc739bb0
LD
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);
57fec1fe 372 break;
cc739bb0 373#ifdef TARGET_X86_64
4ba9938c 374 case MO_64:
cc739bb0 375 tcg_gen_mov_tl(cpu_regs[reg], t0);
57fec1fe 376 break;
14ce26e7 377#endif
d67dc9e6
RH
378 default:
379 tcg_abort();
57fec1fe
FB
380 }
381}
2c0262af 382
d67dc9e6 383static inline void gen_op_mov_v_reg(TCGMemOp ot, TCGv t0, int reg)
57fec1fe 384{
4ba9938c 385 if (ot == MO_8 && byte_reg_is_xH(reg)) {
04fc2f1c 386 tcg_gen_extract_tl(t0, cpu_regs[reg - 4], 8, 8);
96d7073f 387 } else {
cc739bb0 388 tcg_gen_mov_tl(t0, cpu_regs[reg]);
57fec1fe
FB
389 }
390}
391
57fec1fe
FB
392static void gen_add_A0_im(DisasContext *s, int val)
393{
4e85057b
RH
394 tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
395 if (!CODE64(s)) {
396 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
397 }
57fec1fe 398}
2c0262af 399
74bdfbda 400static inline void gen_op_jmp_v(TCGv dest)
57fec1fe 401{
74bdfbda 402 tcg_gen_st_tl(dest, cpu_env, offsetof(CPUX86State, eip));
57fec1fe
FB
403}
404
d3f4bbe3 405static inline void gen_op_add_reg_im(TCGMemOp size, int reg, int32_t val)
57fec1fe 406{
d3f4bbe3
RH
407 tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
408 gen_op_mov_reg_v(size, reg, cpu_tmp0);
57fec1fe
FB
409}
410
d3f4bbe3 411static inline void gen_op_add_reg_T0(TCGMemOp size, int reg)
57fec1fe 412{
1d1cc4d0 413 tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T0);
d3f4bbe3 414 gen_op_mov_reg_v(size, reg, cpu_tmp0);
6e0d8677 415}
57fec1fe 416
323d1876 417static inline void gen_op_ld_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
57fec1fe 418{
3c5f4116 419 tcg_gen_qemu_ld_tl(t0, a0, s->mem_index, idx | MO_LE);
57fec1fe 420}
2c0262af 421
323d1876 422static inline void gen_op_st_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
57fec1fe 423{
3523e4bd 424 tcg_gen_qemu_st_tl(t0, a0, s->mem_index, idx | MO_LE);
57fec1fe 425}
4f31916f 426
d4faa3e0
RH
427static inline void gen_op_st_rm_T0_A0(DisasContext *s, int idx, int d)
428{
429 if (d == OR_TMP0) {
1d1cc4d0 430 gen_op_st_v(s, idx, cpu_T0, cpu_A0);
d4faa3e0 431 } else {
1d1cc4d0 432 gen_op_mov_reg_v(idx, d, cpu_T0);
d4faa3e0
RH
433 }
434}
435
14ce26e7
FB
436static inline void gen_jmp_im(target_ulong pc)
437{
57fec1fe 438 tcg_gen_movi_tl(cpu_tmp0, pc);
74bdfbda 439 gen_op_jmp_v(cpu_tmp0);
14ce26e7
FB
440}
441
ca2f29f5
RH
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. */
77ebcad0
RH
445static void gen_lea_v_seg(DisasContext *s, TCGMemOp aflag, TCGv a0,
446 int def_seg, int ovr_seg)
2c0262af 447{
ca2f29f5 448 switch (aflag) {
14ce26e7 449#ifdef TARGET_X86_64
1d71ddb1 450 case MO_64:
ca2f29f5
RH
451 if (ovr_seg < 0) {
452 tcg_gen_mov_tl(cpu_A0, a0);
453 return;
14ce26e7 454 }
1d71ddb1 455 break;
14ce26e7 456#endif
1d71ddb1 457 case MO_32:
2c0262af 458 /* 32 bit address */
620abfb0
PB
459 if (ovr_seg < 0 && s->addseg) {
460 ovr_seg = def_seg;
461 }
ca2f29f5 462 if (ovr_seg < 0) {
620abfb0
PB
463 tcg_gen_ext32u_tl(cpu_A0, a0);
464 return;
2c0262af 465 }
1d71ddb1
RH
466 break;
467 case MO_16:
ca2f29f5 468 /* 16 bit address */
ca2f29f5 469 tcg_gen_ext16u_tl(cpu_A0, a0);
ca2f29f5 470 a0 = cpu_A0;
e2e02a82
PB
471 if (ovr_seg < 0) {
472 if (s->addseg) {
473 ovr_seg = def_seg;
474 } else {
475 return;
476 }
477 }
1d71ddb1
RH
478 break;
479 default:
480 tcg_abort();
2c0262af 481 }
2c0262af 482
ca2f29f5 483 if (ovr_seg >= 0) {
3558f805 484 TCGv seg = cpu_seg_base[ovr_seg];
ca2f29f5
RH
485
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);
2c0262af 491 } else {
ca2f29f5
RH
492 tcg_gen_add_tl(cpu_A0, a0, seg);
493 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
2c0262af 494 }
2c0262af
FB
495 }
496}
497
ca2f29f5
RH
498static inline void gen_string_movl_A0_ESI(DisasContext *s)
499{
77ebcad0 500 gen_lea_v_seg(s, s->aflag, cpu_regs[R_ESI], R_DS, s->override);
ca2f29f5
RH
501}
502
503static inline void gen_string_movl_A0_EDI(DisasContext *s)
504{
77ebcad0 505 gen_lea_v_seg(s, s->aflag, cpu_regs[R_EDI], R_ES, -1);
ca2f29f5
RH
506}
507
d67dc9e6 508static inline void gen_op_movl_T0_Dshift(TCGMemOp ot)
6e0d8677 509{
1d1cc4d0
RH
510 tcg_gen_ld32s_tl(cpu_T0, cpu_env, offsetof(CPUX86State, df));
511 tcg_gen_shli_tl(cpu_T0, cpu_T0, ot);
2c0262af
FB
512};
513
d67dc9e6 514static TCGv gen_ext_tl(TCGv dst, TCGv src, TCGMemOp size, bool sign)
6e0d8677 515{
d824df34 516 switch (size) {
4ba9938c 517 case MO_8:
d824df34
PB
518 if (sign) {
519 tcg_gen_ext8s_tl(dst, src);
520 } else {
521 tcg_gen_ext8u_tl(dst, src);
522 }
523 return dst;
4ba9938c 524 case MO_16:
d824df34
PB
525 if (sign) {
526 tcg_gen_ext16s_tl(dst, src);
527 } else {
528 tcg_gen_ext16u_tl(dst, src);
529 }
530 return dst;
531#ifdef TARGET_X86_64
4ba9938c 532 case MO_32:
d824df34
PB
533 if (sign) {
534 tcg_gen_ext32s_tl(dst, src);
535 } else {
536 tcg_gen_ext32u_tl(dst, src);
537 }
538 return dst;
539#endif
6e0d8677 540 default:
d824df34 541 return src;
6e0d8677
FB
542 }
543}
3b46e624 544
d67dc9e6 545static void gen_extu(TCGMemOp ot, TCGv reg)
d824df34
PB
546{
547 gen_ext_tl(reg, reg, ot, false);
548}
549
d67dc9e6 550static void gen_exts(TCGMemOp ot, TCGv reg)
6e0d8677 551{
d824df34 552 gen_ext_tl(reg, reg, ot, true);
6e0d8677 553}
2c0262af 554
42a268c2 555static inline void gen_op_jnz_ecx(TCGMemOp size, TCGLabel *label1)
6e0d8677 556{
cc739bb0 557 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
c92aa1ad 558 gen_extu(size, cpu_tmp0);
cb63669a 559 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
6e0d8677
FB
560}
561
42a268c2 562static inline void gen_op_jz_ecx(TCGMemOp size, TCGLabel *label1)
6e0d8677 563{
cc739bb0 564 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
c92aa1ad 565 gen_extu(size, cpu_tmp0);
cb63669a 566 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
6e0d8677 567}
2c0262af 568
d67dc9e6 569static void gen_helper_in_func(TCGMemOp ot, TCGv v, TCGv_i32 n)
a7812ae4
PB
570{
571 switch (ot) {
4ba9938c 572 case MO_8:
3f7d8464 573 gen_helper_inb(v, cpu_env, n);
93ab25d7 574 break;
4ba9938c 575 case MO_16:
3f7d8464 576 gen_helper_inw(v, cpu_env, n);
93ab25d7 577 break;
4ba9938c 578 case MO_32:
3f7d8464 579 gen_helper_inl(v, cpu_env, n);
93ab25d7 580 break;
d67dc9e6
RH
581 default:
582 tcg_abort();
a7812ae4 583 }
a7812ae4 584}
2c0262af 585
d67dc9e6 586static void gen_helper_out_func(TCGMemOp ot, TCGv_i32 v, TCGv_i32 n)
a7812ae4
PB
587{
588 switch (ot) {
4ba9938c 589 case MO_8:
3f7d8464 590 gen_helper_outb(cpu_env, v, n);
93ab25d7 591 break;
4ba9938c 592 case MO_16:
3f7d8464 593 gen_helper_outw(cpu_env, v, n);
93ab25d7 594 break;
4ba9938c 595 case MO_32:
3f7d8464 596 gen_helper_outl(cpu_env, v, n);
93ab25d7 597 break;
d67dc9e6
RH
598 default:
599 tcg_abort();
a7812ae4 600 }
a7812ae4 601}
f115e911 602
d67dc9e6 603static void gen_check_io(DisasContext *s, TCGMemOp ot, target_ulong cur_eip,
b8b6a50b 604 uint32_t svm_flags)
f115e911 605{
b8b6a50b
FB
606 target_ulong next_eip;
607
f115e911 608 if (s->pe && (s->cpl > s->iopl || s->vm86)) {
1d1cc4d0 609 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
a7812ae4 610 switch (ot) {
4ba9938c 611 case MO_8:
4a7443be
BS
612 gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
613 break;
4ba9938c 614 case MO_16:
4a7443be
BS
615 gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
616 break;
4ba9938c 617 case MO_32:
4a7443be
BS
618 gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
619 break;
d67dc9e6
RH
620 default:
621 tcg_abort();
a7812ae4 622 }
b8b6a50b 623 }
872929aa 624 if(s->flags & HF_SVMI_MASK) {
100ec099
PD
625 gen_update_cc_op(s);
626 gen_jmp_im(cur_eip);
b8b6a50b
FB
627 svm_flags |= (1 << (4 + ot));
628 next_eip = s->pc - s->cs_base;
1d1cc4d0 629 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
052e80d5
BS
630 gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
631 tcg_const_i32(svm_flags),
a7812ae4 632 tcg_const_i32(next_eip - cur_eip));
f115e911
FB
633 }
634}
635
d67dc9e6 636static inline void gen_movs(DisasContext *s, TCGMemOp ot)
2c0262af
FB
637{
638 gen_string_movl_A0_ESI(s);
1d1cc4d0 639 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
2c0262af 640 gen_string_movl_A0_EDI(s);
1d1cc4d0 641 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
6e0d8677 642 gen_op_movl_T0_Dshift(ot);
1d71ddb1
RH
643 gen_op_add_reg_T0(s->aflag, R_ESI);
644 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
645}
646
b6abf97d
FB
647static void gen_op_update1_cc(void)
648{
1d1cc4d0 649 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
b6abf97d
FB
650}
651
652static void gen_op_update2_cc(void)
653{
1d1cc4d0
RH
654 tcg_gen_mov_tl(cpu_cc_src, cpu_T1);
655 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
b6abf97d
FB
656}
657
988c3eb0
RH
658static void gen_op_update3_cc(TCGv reg)
659{
660 tcg_gen_mov_tl(cpu_cc_src2, reg);
1d1cc4d0
RH
661 tcg_gen_mov_tl(cpu_cc_src, cpu_T1);
662 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
988c3eb0
RH
663}
664
b6abf97d
FB
665static inline void gen_op_testl_T0_T1_cc(void)
666{
1d1cc4d0 667 tcg_gen_and_tl(cpu_cc_dst, cpu_T0, cpu_T1);
b6abf97d
FB
668}
669
670static void gen_op_update_neg_cc(void)
671{
1d1cc4d0
RH
672 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
673 tcg_gen_neg_tl(cpu_cc_src, cpu_T0);
a3251186 674 tcg_gen_movi_tl(cpu_cc_srcT, 0);
b6abf97d
FB
675}
676
d229edce
RH
677/* compute all eflags to cc_src */
678static void gen_compute_eflags(DisasContext *s)
8e1c85e3 679{
988c3eb0 680 TCGv zero, dst, src1, src2;
db9f2597
RH
681 int live, dead;
682
d229edce
RH
683 if (s->cc_op == CC_OP_EFLAGS) {
684 return;
685 }
436ff2d2 686 if (s->cc_op == CC_OP_CLR) {
d2fe51bd 687 tcg_gen_movi_tl(cpu_cc_src, CC_Z | CC_P);
436ff2d2
RH
688 set_cc_op(s, CC_OP_EFLAGS);
689 return;
690 }
db9f2597 691
f764718d 692 zero = NULL;
db9f2597
RH
693 dst = cpu_cc_dst;
694 src1 = cpu_cc_src;
988c3eb0 695 src2 = cpu_cc_src2;
db9f2597
RH
696
697 /* Take care to not read values that are not live. */
698 live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
988c3eb0 699 dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
db9f2597
RH
700 if (dead) {
701 zero = tcg_const_tl(0);
702 if (dead & USES_CC_DST) {
703 dst = zero;
704 }
705 if (dead & USES_CC_SRC) {
706 src1 = zero;
707 }
988c3eb0
RH
708 if (dead & USES_CC_SRC2) {
709 src2 = zero;
710 }
db9f2597
RH
711 }
712
773cdfcc 713 gen_update_cc_op(s);
988c3eb0 714 gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
d229edce 715 set_cc_op(s, CC_OP_EFLAGS);
db9f2597
RH
716
717 if (dead) {
718 tcg_temp_free(zero);
719 }
8e1c85e3
FB
720}
721
bec93d72
RH
722typedef 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;
731
06847f1f 732/* compute eflags.C to reg */
bec93d72 733static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
06847f1f
RH
734{
735 TCGv t0, t1;
bec93d72 736 int size, shift;
06847f1f
RH
737
738 switch (s->cc_op) {
739 case CC_OP_SUBB ... CC_OP_SUBQ:
a3251186 740 /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
06847f1f
RH
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. */
11f4e8f8 744 t0 = t1 == cpu_cc_src ? cpu_tmp0 : reg;
a3251186 745 tcg_gen_mov_tl(t0, cpu_cc_srcT);
06847f1f
RH
746 gen_extu(size, t0);
747 goto add_sub;
748
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:
bec93d72
RH
755 return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
756 .reg2 = t1, .mask = -1, .use_reg2 = true };
06847f1f 757
06847f1f 758 case CC_OP_LOGICB ... CC_OP_LOGICQ:
436ff2d2 759 case CC_OP_CLR:
4885c3c4 760 case CC_OP_POPCNT:
bec93d72 761 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
06847f1f
RH
762
763 case CC_OP_INCB ... CC_OP_INCQ:
764 case CC_OP_DECB ... CC_OP_DECQ:
bec93d72
RH
765 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
766 .mask = -1, .no_setcond = true };
06847f1f
RH
767
768 case CC_OP_SHLB ... CC_OP_SHLQ:
769 /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
770 size = s->cc_op - CC_OP_SHLB;
bec93d72
RH
771 shift = (8 << size) - 1;
772 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
773 .mask = (target_ulong)1 << shift };
06847f1f
RH
774
775 case CC_OP_MULB ... CC_OP_MULQ:
bec93d72
RH
776 return (CCPrepare) { .cond = TCG_COND_NE,
777 .reg = cpu_cc_src, .mask = -1 };
06847f1f 778
bc4b43dc
RH
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 };
783
cd7f97ca
RH
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 };
788
06847f1f
RH
789 case CC_OP_EFLAGS:
790 case CC_OP_SARB ... CC_OP_SARQ:
791 /* CC_SRC & 1 */
bec93d72
RH
792 return (CCPrepare) { .cond = TCG_COND_NE,
793 .reg = cpu_cc_src, .mask = CC_C };
06847f1f
RH
794
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);
988c3eb0
RH
799 gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
800 cpu_cc_src2, cpu_cc_op);
bec93d72
RH
801 return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
802 .mask = -1, .no_setcond = true };
06847f1f
RH
803 }
804}
805
1608ecca 806/* compute eflags.P to reg */
bec93d72 807static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
1608ecca 808{
d229edce 809 gen_compute_eflags(s);
bec93d72
RH
810 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
811 .mask = CC_P };
1608ecca
PB
812}
813
814/* compute eflags.S to reg */
bec93d72 815static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
1608ecca 816{
086c4077
RH
817 switch (s->cc_op) {
818 case CC_OP_DYNAMIC:
819 gen_compute_eflags(s);
820 /* FALLTHRU */
821 case CC_OP_EFLAGS:
cd7f97ca
RH
822 case CC_OP_ADCX:
823 case CC_OP_ADOX:
824 case CC_OP_ADCOX:
bec93d72
RH
825 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
826 .mask = CC_S };
436ff2d2 827 case CC_OP_CLR:
4885c3c4 828 case CC_OP_POPCNT:
436ff2d2 829 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
086c4077
RH
830 default:
831 {
d67dc9e6 832 TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
086c4077 833 TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
bec93d72 834 return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
086c4077 835 }
086c4077 836 }
1608ecca
PB
837}
838
839/* compute eflags.O to reg */
bec93d72 840static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
1608ecca 841{
cd7f97ca
RH
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 };
436ff2d2 847 case CC_OP_CLR:
4885c3c4 848 case CC_OP_POPCNT:
436ff2d2 849 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
cd7f97ca
RH
850 default:
851 gen_compute_eflags(s);
852 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
853 .mask = CC_O };
854 }
1608ecca
PB
855}
856
857/* compute eflags.Z to reg */
bec93d72 858static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
1608ecca 859{
086c4077
RH
860 switch (s->cc_op) {
861 case CC_OP_DYNAMIC:
862 gen_compute_eflags(s);
863 /* FALLTHRU */
864 case CC_OP_EFLAGS:
cd7f97ca
RH
865 case CC_OP_ADCX:
866 case CC_OP_ADOX:
867 case CC_OP_ADCOX:
bec93d72
RH
868 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
869 .mask = CC_Z };
436ff2d2
RH
870 case CC_OP_CLR:
871 return (CCPrepare) { .cond = TCG_COND_ALWAYS, .mask = -1 };
4885c3c4
RH
872 case CC_OP_POPCNT:
873 return (CCPrepare) { .cond = TCG_COND_EQ, .reg = cpu_cc_src,
874 .mask = -1 };
086c4077
RH
875 default:
876 {
d67dc9e6 877 TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
086c4077 878 TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
bec93d72 879 return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
086c4077 880 }
bec93d72
RH
881 }
882}
883
c365395e
PB
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. */
276e6b5f 886static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
8e1c85e3 887{
d67dc9e6
RH
888 int inv, jcc_op, cond;
889 TCGMemOp size;
276e6b5f 890 CCPrepare cc;
c365395e
PB
891 TCGv t0;
892
893 inv = b & 1;
8e1c85e3 894 jcc_op = (b >> 1) & 7;
c365395e
PB
895
896 switch (s->cc_op) {
69d1aa31
RH
897 case CC_OP_SUBB ... CC_OP_SUBQ:
898 /* We optimize relational operators for the cmp/jcc case. */
c365395e
PB
899 size = s->cc_op - CC_OP_SUBB;
900 switch (jcc_op) {
901 case JCC_BE:
a3251186 902 tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
c365395e
PB
903 gen_extu(size, cpu_tmp4);
904 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
276e6b5f
RH
905 cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
906 .reg2 = t0, .mask = -1, .use_reg2 = true };
c365395e 907 break;
8e1c85e3 908
c365395e 909 case JCC_L:
276e6b5f 910 cond = TCG_COND_LT;
c365395e
PB
911 goto fast_jcc_l;
912 case JCC_LE:
276e6b5f 913 cond = TCG_COND_LE;
c365395e 914 fast_jcc_l:
a3251186 915 tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
c365395e
PB
916 gen_exts(size, cpu_tmp4);
917 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
276e6b5f
RH
918 cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
919 .reg2 = t0, .mask = -1, .use_reg2 = true };
c365395e 920 break;
8e1c85e3 921
c365395e 922 default:
8e1c85e3 923 goto slow_jcc;
c365395e 924 }
8e1c85e3 925 break;
c365395e 926
8e1c85e3
FB
927 default:
928 slow_jcc:
69d1aa31
RH
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);
11f4e8f8 953 if (reg == cpu_cc_src) {
69d1aa31
RH
954 reg = cpu_tmp0;
955 }
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);
11f4e8f8 964 if (reg == cpu_cc_src) {
69d1aa31
RH
965 reg = cpu_tmp0;
966 }
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;
972 }
c365395e 973 break;
8e1c85e3 974 }
276e6b5f
RH
975
976 if (inv) {
977 cc.cond = tcg_invert_cond(cc.cond);
978 }
979 return cc;
8e1c85e3
FB
980}
981
cc8b6f5b
PB
982static void gen_setcc1(DisasContext *s, int b, TCGv reg)
983{
984 CCPrepare cc = gen_prepare_cc(s, b, reg);
985
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);
991 }
992 return;
993 }
994
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;
1000 }
1001 if (cc.mask != -1) {
1002 tcg_gen_andi_tl(reg, cc.reg, cc.mask);
1003 cc.reg = reg;
1004 }
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);
1009 }
1010}
1011
1012static inline void gen_compute_eflags_c(DisasContext *s, TCGv reg)
1013{
1014 gen_setcc1(s, JCC_B << 1, reg);
1015}
276e6b5f 1016
8e1c85e3
FB
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. */
42a268c2 1019static inline void gen_jcc1_noeob(DisasContext *s, int b, TCGLabel *l1)
dc259201 1020{
1d1cc4d0 1021 CCPrepare cc = gen_prepare_cc(s, b, cpu_T0);
dc259201
RH
1022
1023 if (cc.mask != -1) {
1d1cc4d0
RH
1024 tcg_gen_andi_tl(cpu_T0, cc.reg, cc.mask);
1025 cc.reg = cpu_T0;
dc259201
RH
1026 }
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);
1031 }
1032}
1033
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. */
42a268c2 1037static inline void gen_jcc1(DisasContext *s, int b, TCGLabel *l1)
8e1c85e3 1038{
1d1cc4d0 1039 CCPrepare cc = gen_prepare_cc(s, b, cpu_T0);
8e1c85e3 1040
dc259201 1041 gen_update_cc_op(s);
943131ca 1042 if (cc.mask != -1) {
1d1cc4d0
RH
1043 tcg_gen_andi_tl(cpu_T0, cc.reg, cc.mask);
1044 cc.reg = cpu_T0;
943131ca 1045 }
dc259201 1046 set_cc_op(s, CC_OP_DYNAMIC);
943131ca
PB
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);
8e1c85e3
FB
1051 }
1052}
1053
14ce26e7
FB
1054/* XXX: does not work with gdbstub "ice" single step - not a
1055 serious problem */
42a268c2 1056static TCGLabel *gen_jz_ecx_string(DisasContext *s, target_ulong next_eip)
2c0262af 1057{
42a268c2
RH
1058 TCGLabel *l1 = gen_new_label();
1059 TCGLabel *l2 = gen_new_label();
1d71ddb1 1060 gen_op_jnz_ecx(s->aflag, l1);
14ce26e7
FB
1061 gen_set_label(l2);
1062 gen_jmp_tb(s, next_eip, 1);
1063 gen_set_label(l1);
1064 return l2;
2c0262af
FB
1065}
1066
d67dc9e6 1067static inline void gen_stos(DisasContext *s, TCGMemOp ot)
2c0262af 1068{
1d1cc4d0 1069 gen_op_mov_v_reg(MO_32, cpu_T0, R_EAX);
2c0262af 1070 gen_string_movl_A0_EDI(s);
1d1cc4d0 1071 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
6e0d8677 1072 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1073 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
1074}
1075
d67dc9e6 1076static inline void gen_lods(DisasContext *s, TCGMemOp ot)
2c0262af
FB
1077{
1078 gen_string_movl_A0_ESI(s);
1d1cc4d0
RH
1079 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1080 gen_op_mov_reg_v(ot, R_EAX, cpu_T0);
6e0d8677 1081 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1082 gen_op_add_reg_T0(s->aflag, R_ESI);
2c0262af
FB
1083}
1084
d67dc9e6 1085static inline void gen_scas(DisasContext *s, TCGMemOp ot)
2c0262af 1086{
2c0262af 1087 gen_string_movl_A0_EDI(s);
1d1cc4d0 1088 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
63633fe6 1089 gen_op(s, OP_CMPL, ot, R_EAX);
6e0d8677 1090 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1091 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
1092}
1093
d67dc9e6 1094static inline void gen_cmps(DisasContext *s, TCGMemOp ot)
2c0262af 1095{
2c0262af 1096 gen_string_movl_A0_EDI(s);
1d1cc4d0 1097 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
63633fe6
RH
1098 gen_string_movl_A0_ESI(s);
1099 gen_op(s, OP_CMPL, ot, OR_TMP0);
6e0d8677 1100 gen_op_movl_T0_Dshift(ot);
1d71ddb1
RH
1101 gen_op_add_reg_T0(s->aflag, R_ESI);
1102 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
1103}
1104
5223a942
EH
1105static void gen_bpt_io(DisasContext *s, TCGv_i32 t_port, int ot)
1106{
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);
1110
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);
1114 }
1115}
1116
1117
d67dc9e6 1118static inline void gen_ins(DisasContext *s, TCGMemOp ot)
2c0262af 1119{
c5a49c63 1120 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef 1121 gen_io_start();
bd79255d 1122 }
2c0262af 1123 gen_string_movl_A0_EDI(s);
6e0d8677
FB
1124 /* Note: we must do this dummy write first to be restartable in
1125 case of page fault. */
1d1cc4d0
RH
1126 tcg_gen_movi_tl(cpu_T0, 0);
1127 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
24b9c00f 1128 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
b6abf97d 1129 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
1d1cc4d0
RH
1130 gen_helper_in_func(ot, cpu_T0, cpu_tmp2_i32);
1131 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
6e0d8677 1132 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1133 gen_op_add_reg_T0(s->aflag, R_EDI);
5223a942 1134 gen_bpt_io(s, cpu_tmp2_i32, ot);
c5a49c63 1135 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef 1136 gen_io_end();
bd79255d 1137 }
2c0262af
FB
1138}
1139
d67dc9e6 1140static inline void gen_outs(DisasContext *s, TCGMemOp ot)
2c0262af 1141{
c5a49c63 1142 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef 1143 gen_io_start();
bd79255d 1144 }
2c0262af 1145 gen_string_movl_A0_ESI(s);
1d1cc4d0 1146 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
b8b6a50b 1147
24b9c00f 1148 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
b6abf97d 1149 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
1d1cc4d0 1150 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T0);
a7812ae4 1151 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
6e0d8677 1152 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1153 gen_op_add_reg_T0(s->aflag, R_ESI);
5223a942 1154 gen_bpt_io(s, cpu_tmp2_i32, ot);
c5a49c63 1155 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef 1156 gen_io_end();
bd79255d 1157 }
2c0262af
FB
1158}
1159
1160/* same method as Valgrind : we generate jumps to current or next
1161 instruction */
1162#define GEN_REPZ(op) \
d67dc9e6 1163static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
14ce26e7 1164 target_ulong cur_eip, target_ulong next_eip) \
2c0262af 1165{ \
42a268c2 1166 TCGLabel *l2; \
2c0262af 1167 gen_update_cc_op(s); \
14ce26e7 1168 l2 = gen_jz_ecx_string(s, next_eip); \
2c0262af 1169 gen_ ## op(s, ot); \
1d71ddb1 1170 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
2c0262af
FB
1171 /* a loop would cause two single step exceptions if ECX = 1 \
1172 before rep string_insn */ \
c4d4525c 1173 if (s->repz_opt) \
1d71ddb1 1174 gen_op_jz_ecx(s->aflag, l2); \
2c0262af
FB
1175 gen_jmp(s, cur_eip); \
1176}
1177
1178#define GEN_REPZ2(op) \
d67dc9e6 1179static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
14ce26e7
FB
1180 target_ulong cur_eip, \
1181 target_ulong next_eip, \
2c0262af
FB
1182 int nz) \
1183{ \
42a268c2 1184 TCGLabel *l2; \
2c0262af 1185 gen_update_cc_op(s); \
14ce26e7 1186 l2 = gen_jz_ecx_string(s, next_eip); \
2c0262af 1187 gen_ ## op(s, ot); \
1d71ddb1 1188 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
773cdfcc 1189 gen_update_cc_op(s); \
b27fc131 1190 gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2); \
c4d4525c 1191 if (s->repz_opt) \
1d71ddb1 1192 gen_op_jz_ecx(s->aflag, l2); \
2c0262af
FB
1193 gen_jmp(s, cur_eip); \
1194}
1195
1196GEN_REPZ(movs)
1197GEN_REPZ(stos)
1198GEN_REPZ(lods)
1199GEN_REPZ(ins)
1200GEN_REPZ(outs)
1201GEN_REPZ2(scas)
1202GEN_REPZ2(cmps)
1203
a7812ae4
PB
1204static void gen_helper_fp_arith_ST0_FT0(int op)
1205{
1206 switch (op) {
d3eb5eae
BS
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;
a7812ae4
PB
1231 }
1232}
2c0262af
FB
1233
1234/* NOTE the exception in "r" op ordering */
a7812ae4
PB
1235static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
1236{
1237 TCGv_i32 tmp = tcg_const_i32(opreg);
1238 switch (op) {
d3eb5eae
BS
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;
a7812ae4
PB
1257 }
1258}
2c0262af
FB
1259
1260/* if d == OR_TMP0, it means memory operand (address in A0) */
d67dc9e6 1261static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d)
2c0262af 1262{
2c0262af 1263 if (d != OR_TMP0) {
1d1cc4d0 1264 gen_op_mov_v_reg(ot, cpu_T0, d);
a7cee522 1265 } else if (!(s1->prefix & PREFIX_LOCK)) {
1d1cc4d0 1266 gen_op_ld_v(s1, ot, cpu_T0, cpu_A0);
2c0262af
FB
1267 }
1268 switch(op) {
1269 case OP_ADCL:
cc8b6f5b 1270 gen_compute_eflags_c(s1, cpu_tmp4);
a7cee522
EC
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);
1279 }
988c3eb0
RH
1280 gen_op_update3_cc(cpu_tmp4);
1281 set_cc_op(s1, CC_OP_ADCB + ot);
cad3a37d 1282 break;
2c0262af 1283 case OP_SBBL:
cc8b6f5b 1284 gen_compute_eflags_c(s1, cpu_tmp4);
a7cee522
EC
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);
1294 }
988c3eb0
RH
1295 gen_op_update3_cc(cpu_tmp4);
1296 set_cc_op(s1, CC_OP_SBBB + ot);
cad3a37d 1297 break;
2c0262af 1298 case OP_ADDL:
a7cee522
EC
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);
1305 }
cad3a37d 1306 gen_op_update2_cc();
3ca51d07 1307 set_cc_op(s1, CC_OP_ADDB + ot);
2c0262af
FB
1308 break;
1309 case OP_SUBL:
a7cee522
EC
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);
1319 }
cad3a37d 1320 gen_op_update2_cc();
3ca51d07 1321 set_cc_op(s1, CC_OP_SUBB + ot);
2c0262af
FB
1322 break;
1323 default:
1324 case OP_ANDL:
a7cee522
EC
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);
1331 }
cad3a37d 1332 gen_op_update1_cc();
3ca51d07 1333 set_cc_op(s1, CC_OP_LOGICB + ot);
57fec1fe 1334 break;
2c0262af 1335 case OP_ORL:
a7cee522
EC
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);
1342 }
cad3a37d 1343 gen_op_update1_cc();
3ca51d07 1344 set_cc_op(s1, CC_OP_LOGICB + ot);
57fec1fe 1345 break;
2c0262af 1346 case OP_XORL:
a7cee522
EC
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);
1353 }
cad3a37d 1354 gen_op_update1_cc();
3ca51d07 1355 set_cc_op(s1, CC_OP_LOGICB + ot);
2c0262af
FB
1356 break;
1357 case OP_CMPL:
1d1cc4d0
RH
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);
3ca51d07 1361 set_cc_op(s1, CC_OP_SUBB + ot);
2c0262af
FB
1362 break;
1363 }
b6abf97d
FB
1364}
1365
2c0262af 1366/* if d == OR_TMP0, it means memory operand (address in A0) */
d67dc9e6 1367static void gen_inc(DisasContext *s1, TCGMemOp ot, int d, int c)
2c0262af 1368{
60e57346
EC
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);
909be183 1373 } else {
60e57346
EC
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);
1378 }
1379 tcg_gen_addi_tl(cpu_T0, cpu_T0, (c > 0 ? 1 : -1));
1380 gen_op_st_rm_T0_A0(s1, ot, d);
909be183 1381 }
60e57346 1382
cc8b6f5b 1383 gen_compute_eflags_c(s1, cpu_cc_src);
1d1cc4d0 1384 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
60e57346 1385 set_cc_op(s1, (c > 0 ? CC_OP_INCB : CC_OP_DECB) + ot);
2c0262af
FB
1386}
1387
d67dc9e6
RH
1388static void gen_shift_flags(DisasContext *s, TCGMemOp ot, TCGv result,
1389 TCGv shm1, TCGv count, bool is_right)
f437d0a3
RH
1390{
1391 TCGv_i32 z32, s32, oldop;
1392 TCGv z_tl;
1393
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);
1403 }
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);
1409 }
1410 tcg_temp_free(z_tl);
1411
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;
1419 }
1420
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);
1428
1429 /* The CC_OP value is no longer predictable. */
1430 set_cc_op(s, CC_OP_DYNAMIC);
1431}
1432
d67dc9e6 1433static void gen_shift_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
b6abf97d 1434 int is_right, int is_arith)
2c0262af 1435{
4ba9938c 1436 target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
3b46e624 1437
b6abf97d 1438 /* load */
82786041 1439 if (op1 == OR_TMP0) {
1d1cc4d0 1440 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
82786041 1441 } else {
1d1cc4d0 1442 gen_op_mov_v_reg(ot, cpu_T0, op1);
82786041 1443 }
b6abf97d 1444
1d1cc4d0
RH
1445 tcg_gen_andi_tl(cpu_T1, cpu_T1, mask);
1446 tcg_gen_subi_tl(cpu_tmp0, cpu_T1, 1);
b6abf97d
FB
1447
1448 if (is_right) {
1449 if (is_arith) {
1d1cc4d0
RH
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);
b6abf97d 1453 } else {
1d1cc4d0
RH
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);
b6abf97d
FB
1457 }
1458 } else {
1d1cc4d0
RH
1459 tcg_gen_shl_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
1460 tcg_gen_shl_tl(cpu_T0, cpu_T0, cpu_T1);
b6abf97d
FB
1461 }
1462
1463 /* store */
d4faa3e0 1464 gen_op_st_rm_T0_A0(s, ot, op1);
82786041 1465
1d1cc4d0 1466 gen_shift_flags(s, ot, cpu_T0, cpu_tmp0, cpu_T1, is_right);
b6abf97d
FB
1467}
1468
d67dc9e6 1469static void gen_shift_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
c1c37968
FB
1470 int is_right, int is_arith)
1471{
4ba9938c 1472 int mask = (ot == MO_64 ? 0x3f : 0x1f);
c1c37968
FB
1473
1474 /* load */
1475 if (op1 == OR_TMP0)
1d1cc4d0 1476 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
c1c37968 1477 else
1d1cc4d0 1478 gen_op_mov_v_reg(ot, cpu_T0, op1);
c1c37968
FB
1479
1480 op2 &= mask;
1481 if (op2 != 0) {
1482 if (is_right) {
1483 if (is_arith) {
1d1cc4d0
RH
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);
c1c37968 1487 } else {
1d1cc4d0
RH
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);
c1c37968
FB
1491 }
1492 } else {
1d1cc4d0
RH
1493 tcg_gen_shli_tl(cpu_tmp4, cpu_T0, op2 - 1);
1494 tcg_gen_shli_tl(cpu_T0, cpu_T0, op2);
c1c37968
FB
1495 }
1496 }
1497
1498 /* store */
d4faa3e0
RH
1499 gen_op_st_rm_T0_A0(s, ot, op1);
1500
c1c37968
FB
1501 /* update eflags if non zero shift */
1502 if (op2 != 0) {
2a449d14 1503 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
1d1cc4d0 1504 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
3ca51d07 1505 set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
c1c37968
FB
1506 }
1507}
1508
d67dc9e6 1509static void gen_rot_rm_T1(DisasContext *s, TCGMemOp ot, int op1, int is_right)
b6abf97d 1510{
4ba9938c 1511 target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
34d80a55 1512 TCGv_i32 t0, t1;
b6abf97d
FB
1513
1514 /* load */
1e4840bf 1515 if (op1 == OR_TMP0) {
1d1cc4d0 1516 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1e4840bf 1517 } else {
1d1cc4d0 1518 gen_op_mov_v_reg(ot, cpu_T0, op1);
1e4840bf 1519 }
b6abf97d 1520
1d1cc4d0 1521 tcg_gen_andi_tl(cpu_T1, cpu_T1, mask);
b6abf97d 1522
34d80a55 1523 switch (ot) {
4ba9938c 1524 case MO_8:
34d80a55 1525 /* Replicate the 8-bit input so that a 32-bit rotate works. */
1d1cc4d0
RH
1526 tcg_gen_ext8u_tl(cpu_T0, cpu_T0);
1527 tcg_gen_muli_tl(cpu_T0, cpu_T0, 0x01010101);
34d80a55 1528 goto do_long;
4ba9938c 1529 case MO_16:
34d80a55 1530 /* Replicate the 16-bit input so that a 32-bit rotate works. */
1d1cc4d0 1531 tcg_gen_deposit_tl(cpu_T0, cpu_T0, cpu_T0, 16, 16);
34d80a55
RH
1532 goto do_long;
1533 do_long:
1534#ifdef TARGET_X86_64
4ba9938c 1535 case MO_32:
1d1cc4d0
RH
1536 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
1537 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
34d80a55
RH
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);
1542 }
1d1cc4d0 1543 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
34d80a55
RH
1544 break;
1545#endif
1546 default:
1547 if (is_right) {
1d1cc4d0 1548 tcg_gen_rotr_tl(cpu_T0, cpu_T0, cpu_T1);
34d80a55 1549 } else {
1d1cc4d0 1550 tcg_gen_rotl_tl(cpu_T0, cpu_T0, cpu_T1);
34d80a55
RH
1551 }
1552 break;
b6abf97d 1553 }
b6abf97d 1554
b6abf97d 1555 /* store */
d4faa3e0 1556 gen_op_st_rm_T0_A0(s, ot, op1);
b6abf97d 1557
34d80a55
RH
1558 /* We'll need the flags computed into CC_SRC. */
1559 gen_compute_eflags(s);
b6abf97d 1560
34d80a55
RH
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. */
b6abf97d 1565 if (is_right) {
1d1cc4d0
RH
1566 tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask - 1);
1567 tcg_gen_shri_tl(cpu_cc_dst, cpu_T0, mask);
089305ac 1568 tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
34d80a55 1569 } else {
1d1cc4d0
RH
1570 tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask);
1571 tcg_gen_andi_tl(cpu_cc_dst, cpu_T0, 1);
b6abf97d 1572 }
34d80a55
RH
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);
1575
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();
1d1cc4d0 1582 tcg_gen_trunc_tl_i32(t1, cpu_T1);
34d80a55
RH
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);
1589
1590 /* The CC_OP value is no longer predictable. */
1591 set_cc_op(s, CC_OP_DYNAMIC);
b6abf97d
FB
1592}
1593
d67dc9e6 1594static void gen_rot_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
8cd6345d 1595 int is_right)
1596{
4ba9938c 1597 int mask = (ot == MO_64 ? 0x3f : 0x1f);
34d80a55 1598 int shift;
8cd6345d 1599
1600 /* load */
1601 if (op1 == OR_TMP0) {
1d1cc4d0 1602 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
8cd6345d 1603 } else {
1d1cc4d0 1604 gen_op_mov_v_reg(ot, cpu_T0, op1);
8cd6345d 1605 }
1606
8cd6345d 1607 op2 &= mask;
8cd6345d 1608 if (op2 != 0) {
34d80a55
RH
1609 switch (ot) {
1610#ifdef TARGET_X86_64
4ba9938c 1611 case MO_32:
1d1cc4d0 1612 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
34d80a55
RH
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);
1617 }
1d1cc4d0 1618 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
34d80a55
RH
1619 break;
1620#endif
1621 default:
1622 if (is_right) {
1d1cc4d0 1623 tcg_gen_rotri_tl(cpu_T0, cpu_T0, op2);
34d80a55 1624 } else {
1d1cc4d0 1625 tcg_gen_rotli_tl(cpu_T0, cpu_T0, op2);
34d80a55
RH
1626 }
1627 break;
4ba9938c 1628 case MO_8:
34d80a55
RH
1629 mask = 7;
1630 goto do_shifts;
4ba9938c 1631 case MO_16:
34d80a55
RH
1632 mask = 15;
1633 do_shifts:
1634 shift = op2 & mask;
1635 if (is_right) {
1636 shift = mask + 1 - shift;
1637 }
1d1cc4d0
RH
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);
34d80a55 1642 break;
8cd6345d 1643 }
8cd6345d 1644 }
1645
1646 /* store */
d4faa3e0 1647 gen_op_st_rm_T0_A0(s, ot, op1);
8cd6345d 1648
1649 if (op2 != 0) {
34d80a55 1650 /* Compute the flags into CC_SRC. */
d229edce 1651 gen_compute_eflags(s);
0ff6addd 1652
34d80a55
RH
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. */
8cd6345d 1657 if (is_right) {
1d1cc4d0
RH
1658 tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask - 1);
1659 tcg_gen_shri_tl(cpu_cc_dst, cpu_T0, mask);
38ebb396 1660 tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
34d80a55 1661 } else {
1d1cc4d0
RH
1662 tcg_gen_shri_tl(cpu_cc_src2, cpu_T0, mask);
1663 tcg_gen_andi_tl(cpu_cc_dst, cpu_T0, 1);
8cd6345d 1664 }
34d80a55
RH
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);
8cd6345d 1668 }
8cd6345d 1669}
1670
b6abf97d 1671/* XXX: add faster immediate = 1 case */
d67dc9e6 1672static void gen_rotc_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
b6abf97d
FB
1673 int is_right)
1674{
d229edce 1675 gen_compute_eflags(s);
c7b3c873 1676 assert(s->cc_op == CC_OP_EFLAGS);
b6abf97d
FB
1677
1678 /* load */
1679 if (op1 == OR_TMP0)
1d1cc4d0 1680 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
b6abf97d 1681 else
1d1cc4d0 1682 gen_op_mov_v_reg(ot, cpu_T0, op1);
b6abf97d 1683
a7812ae4
PB
1684 if (is_right) {
1685 switch (ot) {
4ba9938c 1686 case MO_8:
1d1cc4d0 1687 gen_helper_rcrb(cpu_T0, cpu_env, cpu_T0, cpu_T1);
7923057b 1688 break;
4ba9938c 1689 case MO_16:
1d1cc4d0 1690 gen_helper_rcrw(cpu_T0, cpu_env, cpu_T0, cpu_T1);
7923057b 1691 break;
4ba9938c 1692 case MO_32:
1d1cc4d0 1693 gen_helper_rcrl(cpu_T0, cpu_env, cpu_T0, cpu_T1);
7923057b 1694 break;
a7812ae4 1695#ifdef TARGET_X86_64
4ba9938c 1696 case MO_64:
1d1cc4d0 1697 gen_helper_rcrq(cpu_T0, cpu_env, cpu_T0, cpu_T1);
7923057b 1698 break;
a7812ae4 1699#endif
d67dc9e6
RH
1700 default:
1701 tcg_abort();
a7812ae4
PB
1702 }
1703 } else {
1704 switch (ot) {
4ba9938c 1705 case MO_8:
1d1cc4d0 1706 gen_helper_rclb(cpu_T0, cpu_env, cpu_T0, cpu_T1);
7923057b 1707 break;
4ba9938c 1708 case MO_16:
1d1cc4d0 1709 gen_helper_rclw(cpu_T0, cpu_env, cpu_T0, cpu_T1);
7923057b 1710 break;
4ba9938c 1711 case MO_32:
1d1cc4d0 1712 gen_helper_rcll(cpu_T0, cpu_env, cpu_T0, cpu_T1);
7923057b 1713 break;
a7812ae4 1714#ifdef TARGET_X86_64
4ba9938c 1715 case MO_64:
1d1cc4d0 1716 gen_helper_rclq(cpu_T0, cpu_env, cpu_T0, cpu_T1);
7923057b 1717 break;
a7812ae4 1718#endif
d67dc9e6
RH
1719 default:
1720 tcg_abort();
a7812ae4
PB
1721 }
1722 }
b6abf97d 1723 /* store */
d4faa3e0 1724 gen_op_st_rm_T0_A0(s, ot, op1);
b6abf97d
FB
1725}
1726
1727/* XXX: add faster immediate case */
d67dc9e6 1728static void gen_shiftd_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
f437d0a3 1729 bool is_right, TCGv count_in)
b6abf97d 1730{
4ba9938c 1731 target_ulong mask = (ot == MO_64 ? 63 : 31);
f437d0a3 1732 TCGv count;
b6abf97d
FB
1733
1734 /* load */
1e4840bf 1735 if (op1 == OR_TMP0) {
1d1cc4d0 1736 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
1e4840bf 1737 } else {
1d1cc4d0 1738 gen_op_mov_v_reg(ot, cpu_T0, op1);
1e4840bf 1739 }
b6abf97d 1740
f437d0a3
RH
1741 count = tcg_temp_new();
1742 tcg_gen_andi_tl(count, count_in, mask);
1e4840bf 1743
f437d0a3 1744 switch (ot) {
4ba9938c 1745 case MO_16:
f437d0a3
RH
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. */
b6abf97d 1749 if (is_right) {
1d1cc4d0
RH
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);
b6abf97d 1753 } else {
1d1cc4d0 1754 tcg_gen_deposit_tl(cpu_T1, cpu_T0, cpu_T1, 16, 16);
b6abf97d 1755 }
f437d0a3
RH
1756 /* FALLTHRU */
1757#ifdef TARGET_X86_64
4ba9938c 1758 case MO_32:
f437d0a3
RH
1759 /* Concatenate the two 32-bit values and use a 64-bit shift. */
1760 tcg_gen_subi_tl(cpu_tmp0, count, 1);
b6abf97d 1761 if (is_right) {
1d1cc4d0
RH
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);
f437d0a3 1765 } else {
1d1cc4d0
RH
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);
f437d0a3 1769 tcg_gen_shri_i64(cpu_tmp0, cpu_tmp0, 32);
1d1cc4d0 1770 tcg_gen_shri_i64(cpu_T0, cpu_T0, 32);
f437d0a3
RH
1771 }
1772 break;
1773#endif
1774 default:
1775 tcg_gen_subi_tl(cpu_tmp0, count, 1);
1776 if (is_right) {
1d1cc4d0 1777 tcg_gen_shr_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
b6abf97d 1778
f437d0a3 1779 tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
1d1cc4d0
RH
1780 tcg_gen_shr_tl(cpu_T0, cpu_T0, count);
1781 tcg_gen_shl_tl(cpu_T1, cpu_T1, cpu_tmp4);
b6abf97d 1782 } else {
1d1cc4d0 1783 tcg_gen_shl_tl(cpu_tmp0, cpu_T0, cpu_tmp0);
4ba9938c 1784 if (ot == MO_16) {
f437d0a3
RH
1785 /* Only needed if count > 16, for Intel behaviour. */
1786 tcg_gen_subfi_tl(cpu_tmp4, 33, count);
1d1cc4d0 1787 tcg_gen_shr_tl(cpu_tmp4, cpu_T1, cpu_tmp4);
f437d0a3
RH
1788 tcg_gen_or_tl(cpu_tmp0, cpu_tmp0, cpu_tmp4);
1789 }
1790
1791 tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
1d1cc4d0
RH
1792 tcg_gen_shl_tl(cpu_T0, cpu_T0, count);
1793 tcg_gen_shr_tl(cpu_T1, cpu_T1, cpu_tmp4);
b6abf97d 1794 }
f437d0a3 1795 tcg_gen_movi_tl(cpu_tmp4, 0);
1d1cc4d0
RH
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);
f437d0a3 1799 break;
b6abf97d 1800 }
b6abf97d 1801
b6abf97d 1802 /* store */
d4faa3e0 1803 gen_op_st_rm_T0_A0(s, ot, op1);
1e4840bf 1804
1d1cc4d0 1805 gen_shift_flags(s, ot, cpu_T0, cpu_tmp0, count, is_right);
f437d0a3 1806 tcg_temp_free(count);
b6abf97d
FB
1807}
1808
d67dc9e6 1809static void gen_shift(DisasContext *s1, int op, TCGMemOp ot, int d, int s)
b6abf97d
FB
1810{
1811 if (s != OR_TMP1)
1d1cc4d0 1812 gen_op_mov_v_reg(ot, cpu_T1, s);
b6abf97d
FB
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;
1836 }
2c0262af
FB
1837}
1838
d67dc9e6 1839static void gen_shifti(DisasContext *s1, int op, TCGMemOp ot, int d, int c)
2c0262af 1840{
c1c37968 1841 switch(op) {
8cd6345d 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;
c1c37968
FB
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 */
1d1cc4d0 1860 tcg_gen_movi_tl(cpu_T1, c);
c1c37968
FB
1861 gen_shift(s1, op, ot, d, OR_TMP1);
1862 break;
1863 }
2c0262af
FB
1864}
1865
b066c537
PB
1866#define X86_MAX_INSN_LENGTH 15
1867
e3af7c78
PB
1868static uint64_t advance_pc(CPUX86State *env, DisasContext *s, int num_bytes)
1869{
1870 uint64_t pc = s->pc;
1871
1872 s->pc += num_bytes;
b066c537
PB
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!
1878 */
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;
1883 }
1884 siglongjmp(s->jmpbuf, 1);
1885 }
1886
e3af7c78
PB
1887 return pc;
1888}
1889
1890static inline uint8_t x86_ldub_code(CPUX86State *env, DisasContext *s)
1891{
1892 return cpu_ldub_code(env, advance_pc(env, s, 1));
1893}
1894
1895static inline int16_t x86_ldsw_code(CPUX86State *env, DisasContext *s)
1896{
1897 return cpu_ldsw_code(env, advance_pc(env, s, 2));
1898}
1899
1900static inline uint16_t x86_lduw_code(CPUX86State *env, DisasContext *s)
1901{
1902 return cpu_lduw_code(env, advance_pc(env, s, 2));
1903}
1904
1905static inline uint32_t x86_ldl_code(CPUX86State *env, DisasContext *s)
1906{
1907 return cpu_ldl_code(env, advance_pc(env, s, 4));
1908}
1909
1910#ifdef TARGET_X86_64
1911static inline uint64_t x86_ldq_code(CPUX86State *env, DisasContext *s)
1912{
1913 return cpu_ldq_code(env, advance_pc(env, s, 8));
1914}
1915#endif
1916
a074ce42
RH
1917/* Decompose an address. */
1918
1919typedef struct AddressParts {
1920 int def_seg;
1921 int base;
1922 int index;
1923 int scale;
1924 target_long disp;
1925} AddressParts;
1926
1927static AddressParts gen_lea_modrm_0(CPUX86State *env, DisasContext *s,
1928 int modrm)
2c0262af 1929{
a074ce42 1930 int def_seg, base, index, scale, mod, rm;
14ce26e7 1931 target_long disp;
a074ce42 1932 bool havesib;
2c0262af 1933
d6a29149 1934 def_seg = R_DS;
a074ce42
RH
1935 index = -1;
1936 scale = 0;
1937 disp = 0;
1938
2c0262af
FB
1939 mod = (modrm >> 6) & 3;
1940 rm = modrm & 7;
a074ce42
RH
1941 base = rm | REX_B(s);
1942
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;
1947 }
2c0262af 1948
1d71ddb1
RH
1949 switch (s->aflag) {
1950 case MO_64:
1951 case MO_32:
2c0262af 1952 havesib = 0;
a074ce42 1953 if (rm == 4) {
e3af7c78 1954 int code = x86_ldub_code(env, s);
2c0262af 1955 scale = (code >> 6) & 3;
14ce26e7 1956 index = ((code >> 3) & 7) | REX_X(s);
7865eec4
RH
1957 if (index == 4) {
1958 index = -1; /* no index */
1959 }
a074ce42
RH
1960 base = (code & 7) | REX_B(s);
1961 havesib = 1;
2c0262af
FB
1962 }
1963
1964 switch (mod) {
1965 case 0:
14ce26e7 1966 if ((base & 7) == 5) {
2c0262af 1967 base = -1;
e3af7c78 1968 disp = (int32_t)x86_ldl_code(env, s);
14ce26e7 1969 if (CODE64(s) && !havesib) {
a074ce42 1970 base = -2;
14ce26e7
FB
1971 disp += s->pc + s->rip_offset;
1972 }
2c0262af
FB
1973 }
1974 break;
1975 case 1:
e3af7c78 1976 disp = (int8_t)x86_ldub_code(env, s);
2c0262af
FB
1977 break;
1978 default:
1979 case 2:
e3af7c78 1980 disp = (int32_t)x86_ldl_code(env, s);
2c0262af
FB
1981 break;
1982 }
3b46e624 1983
7865eec4
RH
1984 /* For correct popl handling with esp. */
1985 if (base == R_ESP && s->popl_esp_hack) {
1986 disp += s->popl_esp_hack;
1987 }
d6a29149
RH
1988 if (base == R_EBP || base == R_ESP) {
1989 def_seg = R_SS;
2c0262af 1990 }
1d71ddb1
RH
1991 break;
1992
1993 case MO_16:
d6a29149 1994 if (mod == 0) {
2c0262af 1995 if (rm == 6) {
a074ce42 1996 base = -1;
e3af7c78 1997 disp = x86_lduw_code(env, s);
d6a29149 1998 break;
2c0262af 1999 }
d6a29149 2000 } else if (mod == 1) {
e3af7c78 2001 disp = (int8_t)x86_ldub_code(env, s);
d6a29149 2002 } else {
e3af7c78 2003 disp = (int16_t)x86_lduw_code(env, s);
2c0262af 2004 }
7effd625 2005
7effd625 2006 switch (rm) {
2c0262af 2007 case 0:
a074ce42
RH
2008 base = R_EBX;
2009 index = R_ESI;
2c0262af
FB
2010 break;
2011 case 1:
a074ce42
RH
2012 base = R_EBX;
2013 index = R_EDI;
2c0262af
FB
2014 break;
2015 case 2:
a074ce42
RH
2016 base = R_EBP;
2017 index = R_ESI;
d6a29149 2018 def_seg = R_SS;
2c0262af
FB
2019 break;
2020 case 3:
a074ce42
RH
2021 base = R_EBP;
2022 index = R_EDI;
d6a29149 2023 def_seg = R_SS;
2c0262af
FB
2024 break;
2025 case 4:
a074ce42 2026 base = R_ESI;
2c0262af
FB
2027 break;
2028 case 5:
a074ce42 2029 base = R_EDI;
2c0262af
FB
2030 break;
2031 case 6:
a074ce42 2032 base = R_EBP;
d6a29149 2033 def_seg = R_SS;
2c0262af
FB
2034 break;
2035 default:
2036 case 7:
a074ce42 2037 base = R_EBX;
2c0262af
FB
2038 break;
2039 }
1d71ddb1
RH
2040 break;
2041
2042 default:
2043 tcg_abort();
2c0262af 2044 }
d6a29149 2045
a074ce42
RH
2046 done:
2047 return (AddressParts){ def_seg, base, index, scale, disp };
2c0262af
FB
2048}
2049
a074ce42
RH
2050/* Compute the address, with a minimum number of TCG ops. */
2051static TCGv gen_lea_modrm_1(AddressParts a)
e17a36ce 2052{
f764718d 2053 TCGv ea = NULL;
3b46e624 2054
a074ce42
RH
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;
e17a36ce 2061 }
a074ce42
RH
2062 if (a.base >= 0) {
2063 tcg_gen_add_tl(cpu_A0, ea, cpu_regs[a.base]);
2064 ea = cpu_A0;
e17a36ce 2065 }
a074ce42
RH
2066 } else if (a.base >= 0) {
2067 ea = cpu_regs[a.base];
2068 }
f764718d 2069 if (!ea) {
a074ce42
RH
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;
2075 }
1d71ddb1 2076
a074ce42
RH
2077 return ea;
2078}
1d71ddb1 2079
a074ce42
RH
2080static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm)
2081{
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);
2085}
2086
2087static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
2088{
2089 (void)gen_lea_modrm_0(env, s, modrm);
e17a36ce
FB
2090}
2091
523e28d7
RH
2092/* Used for BNDCL, BNDCU, BNDCN. */
2093static void gen_bndck(CPUX86State *env, DisasContext *s, int modrm,
2094 TCGCond cond, TCGv_i64 bndv)
2095{
2096 TCGv ea = gen_lea_modrm_1(gen_lea_modrm_0(env, s, modrm));
2097
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);
2101 }
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);
2105}
2106
664e0f19
FB
2107/* used for LEA and MOV AX, mem */
2108static void gen_add_A0_ds_seg(DisasContext *s)
2109{
77ebcad0 2110 gen_lea_v_seg(s, s->aflag, cpu_A0, R_DS, s->override);
664e0f19
FB
2111}
2112
222a3336 2113/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
2c0262af 2114 OR_TMP0 */
0af10c86 2115static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
d67dc9e6 2116 TCGMemOp ot, int reg, int is_store)
2c0262af 2117{
4eeb3939 2118 int mod, rm;
2c0262af
FB
2119
2120 mod = (modrm >> 6) & 3;
14ce26e7 2121 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
2122 if (mod == 3) {
2123 if (is_store) {
2124 if (reg != OR_TMP0)
1d1cc4d0
RH
2125 gen_op_mov_v_reg(ot, cpu_T0, reg);
2126 gen_op_mov_reg_v(ot, rm, cpu_T0);
2c0262af 2127 } else {
1d1cc4d0 2128 gen_op_mov_v_reg(ot, cpu_T0, rm);
2c0262af 2129 if (reg != OR_TMP0)
1d1cc4d0 2130 gen_op_mov_reg_v(ot, reg, cpu_T0);
2c0262af
FB
2131 }
2132 } else {
4eeb3939 2133 gen_lea_modrm(env, s, modrm);
2c0262af
FB
2134 if (is_store) {
2135 if (reg != OR_TMP0)
1d1cc4d0
RH
2136 gen_op_mov_v_reg(ot, cpu_T0, reg);
2137 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
2c0262af 2138 } else {
1d1cc4d0 2139 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
2c0262af 2140 if (reg != OR_TMP0)
1d1cc4d0 2141 gen_op_mov_reg_v(ot, reg, cpu_T0);
2c0262af
FB
2142 }
2143 }
2144}
2145
d67dc9e6 2146static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, TCGMemOp ot)
2c0262af
FB
2147{
2148 uint32_t ret;
2149
d67dc9e6 2150 switch (ot) {
4ba9938c 2151 case MO_8:
e3af7c78 2152 ret = x86_ldub_code(env, s);
2c0262af 2153 break;
4ba9938c 2154 case MO_16:
e3af7c78 2155 ret = x86_lduw_code(env, s);
2c0262af 2156 break;
4ba9938c 2157 case MO_32:
d67dc9e6
RH
2158#ifdef TARGET_X86_64
2159 case MO_64:
2160#endif
e3af7c78 2161 ret = x86_ldl_code(env, s);
2c0262af 2162 break;
d67dc9e6
RH
2163 default:
2164 tcg_abort();
2c0262af
FB
2165 }
2166 return ret;
2167}
2168
d67dc9e6 2169static inline int insn_const_size(TCGMemOp ot)
14ce26e7 2170{
4ba9938c 2171 if (ot <= MO_32) {
14ce26e7 2172 return 1 << ot;
4ba9938c 2173 } else {
14ce26e7 2174 return 4;
4ba9938c 2175 }
14ce26e7
FB
2176}
2177
90aa39a1
SF
2178static inline bool use_goto_tb(DisasContext *s, target_ulong pc)
2179{
2180#ifndef CONFIG_USER_ONLY
6cf147aa 2181 return (pc & TARGET_PAGE_MASK) == (s->base.tb->pc & TARGET_PAGE_MASK) ||
90aa39a1
SF
2182 (pc & TARGET_PAGE_MASK) == (s->pc_start & TARGET_PAGE_MASK);
2183#else
2184 return true;
2185#endif
2186}
2187
6e256c93
FB
2188static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip)
2189{
90aa39a1 2190 target_ulong pc = s->cs_base + eip;
6e256c93 2191
90aa39a1 2192 if (use_goto_tb(s, pc)) {
6e256c93 2193 /* jump to same page: we can use a direct jump */
57fec1fe 2194 tcg_gen_goto_tb(tb_num);
6e256c93 2195 gen_jmp_im(eip);
6cf147aa
LV
2196 tcg_gen_exit_tb((uintptr_t)s->base.tb + tb_num);
2197 s->base.is_jmp = DISAS_NORETURN;
6e256c93 2198 } else {
fe620895 2199 /* jump to another page */
6e256c93 2200 gen_jmp_im(eip);
fe620895 2201 gen_jr(s, cpu_tmp0);
6e256c93
FB
2202 }
2203}
2204
5fafdf24 2205static inline void gen_jcc(DisasContext *s, int b,
14ce26e7 2206 target_ulong val, target_ulong next_eip)
2c0262af 2207{
42a268c2 2208 TCGLabel *l1, *l2;
3b46e624 2209
2c0262af 2210 if (s->jmp_opt) {
14ce26e7 2211 l1 = gen_new_label();
b27fc131 2212 gen_jcc1(s, b, l1);
dc259201 2213
6e256c93 2214 gen_goto_tb(s, 0, next_eip);
14ce26e7
FB
2215
2216 gen_set_label(l1);
6e256c93 2217 gen_goto_tb(s, 1, val);
2c0262af 2218 } else {
14ce26e7
FB
2219 l1 = gen_new_label();
2220 l2 = gen_new_label();
b27fc131 2221 gen_jcc1(s, b, l1);
8e1c85e3 2222
14ce26e7 2223 gen_jmp_im(next_eip);
8e1c85e3
FB
2224 tcg_gen_br(l2);
2225
14ce26e7
FB
2226 gen_set_label(l1);
2227 gen_jmp_im(val);
2228 gen_set_label(l2);
2c0262af
FB
2229 gen_eob(s);
2230 }
2231}
2232
d67dc9e6 2233static void gen_cmovcc1(CPUX86State *env, DisasContext *s, TCGMemOp ot, int b,
f32d3781
PB
2234 int modrm, int reg)
2235{
57eb0cc8 2236 CCPrepare cc;
f32d3781 2237
57eb0cc8 2238 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
f32d3781 2239
1d1cc4d0 2240 cc = gen_prepare_cc(s, b, cpu_T1);
57eb0cc8
RH
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;
2245 }
2246 if (!cc.use_reg2) {
2247 cc.reg2 = tcg_const_tl(cc.imm);
f32d3781
PB
2248 }
2249
1d1cc4d0
RH
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);
57eb0cc8
RH
2253
2254 if (cc.mask != -1) {
2255 tcg_temp_free(cc.reg);
2256 }
2257 if (!cc.use_reg2) {
2258 tcg_temp_free(cc.reg2);
2259 }
f32d3781
PB
2260}
2261
3bd7da9e
FB
2262static inline void gen_op_movl_T0_seg(int seg_reg)
2263{
1d1cc4d0 2264 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
3bd7da9e
FB
2265 offsetof(CPUX86State,segs[seg_reg].selector));
2266}
2267
2268static inline void gen_op_movl_seg_T0_vm(int seg_reg)
2269{
1d1cc4d0
RH
2270 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
2271 tcg_gen_st32_tl(cpu_T0, cpu_env,
3bd7da9e 2272 offsetof(CPUX86State,segs[seg_reg].selector));
1d1cc4d0 2273 tcg_gen_shli_tl(cpu_seg_base[seg_reg], cpu_T0, 4);
3bd7da9e
FB
2274}
2275
2c0262af
FB
2276/* move T0 to seg_reg and compute if the CPU state may change. Never
2277 call this function with seg_reg == R_CS */
100ec099 2278static void gen_movl_seg_T0(DisasContext *s, int seg_reg)
2c0262af 2279{
3415a4dd 2280 if (s->pe && !s->vm86) {
1d1cc4d0 2281 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
2999a0b2 2282 gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
dc196a57
FB
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 */
1e39d97a 2287 if (seg_reg == R_SS || (s->code32 && seg_reg < R_FS)) {
6cf147aa 2288 s->base.is_jmp = DISAS_TOO_MANY;
1e39d97a 2289 }
3415a4dd 2290 } else {
3bd7da9e 2291 gen_op_movl_seg_T0_vm(seg_reg);
1e39d97a 2292 if (seg_reg == R_SS) {
6cf147aa 2293 s->base.is_jmp = DISAS_TOO_MANY;
1e39d97a 2294 }
3415a4dd 2295 }
2c0262af
FB
2296}
2297
0573fbfc
TS
2298static inline int svm_is_rep(int prefixes)
2299{
2300 return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
2301}
2302
872929aa 2303static inline void
0573fbfc 2304gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
b8b6a50b 2305 uint32_t type, uint64_t param)
0573fbfc 2306{
872929aa
FB
2307 /* no SVM activated; fast case */
2308 if (likely(!(s->flags & HF_SVMI_MASK)))
2309 return;
773cdfcc 2310 gen_update_cc_op(s);
872929aa 2311 gen_jmp_im(pc_start - s->cs_base);
052e80d5 2312 gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
a7812ae4 2313 tcg_const_i64(param));
0573fbfc
TS
2314}
2315
872929aa 2316static inline void
0573fbfc
TS
2317gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
2318{
872929aa 2319 gen_svm_check_intercept_param(s, pc_start, type, 0);
0573fbfc
TS
2320}
2321
4f31916f
FB
2322static inline void gen_stack_update(DisasContext *s, int addend)
2323{
64ae256c 2324 gen_op_add_reg_im(mo_stacksize(s), R_ESP, addend);
4f31916f
FB
2325}
2326
432baffe
RH
2327/* Generate a push. It depends on ss32, addseg and dflag. */
2328static void gen_push_v(DisasContext *s, TCGv val)
2c0262af 2329{
64ae256c
RH
2330 TCGMemOp d_ot = mo_pushpop(s, s->dflag);
2331 TCGMemOp a_ot = mo_stacksize(s);
432baffe
RH
2332 int size = 1 << d_ot;
2333 TCGv new_esp = cpu_A0;
2334
2335 tcg_gen_subi_tl(cpu_A0, cpu_regs[R_ESP], size);
2c0262af 2336
77ebcad0 2337 if (!CODE64(s)) {
432baffe
RH
2338 if (s->addseg) {
2339 new_esp = cpu_tmp4;
2340 tcg_gen_mov_tl(new_esp, cpu_A0);
2c0262af 2341 }
77ebcad0 2342 gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
2c0262af 2343 }
432baffe
RH
2344
2345 gen_op_st_v(s, d_ot, val, cpu_A0);
2346 gen_op_mov_reg_v(a_ot, R_ESP, new_esp);
2c0262af
FB
2347}
2348
4f31916f 2349/* two step pop is necessary for precise exceptions */
8e31d234 2350static TCGMemOp gen_pop_T0(DisasContext *s)
2c0262af 2351{
8e31d234 2352 TCGMemOp d_ot = mo_pushpop(s, s->dflag);
8e31d234 2353
77ebcad0 2354 gen_lea_v_seg(s, mo_stacksize(s), cpu_regs[R_ESP], R_SS, -1);
1d1cc4d0 2355 gen_op_ld_v(s, d_ot, cpu_T0, cpu_A0);
8e31d234 2356
8e31d234 2357 return d_ot;
2c0262af
FB
2358}
2359
77ebcad0 2360static inline void gen_pop_update(DisasContext *s, TCGMemOp ot)
2c0262af 2361{
8e31d234 2362 gen_stack_update(s, 1 << ot);
2c0262af
FB
2363}
2364
77ebcad0 2365static inline void gen_stack_A0(DisasContext *s)
2c0262af 2366{
77ebcad0 2367 gen_lea_v_seg(s, s->ss32 ? MO_32 : MO_16, cpu_regs[R_ESP], R_SS, -1);
2c0262af
FB
2368}
2369
2c0262af
FB
2370static void gen_pusha(DisasContext *s)
2371{
d37ea0c0
RH
2372 TCGMemOp s_ot = s->ss32 ? MO_32 : MO_16;
2373 TCGMemOp d_ot = s->dflag;
2374 int size = 1 << d_ot;
2c0262af 2375 int i;
d37ea0c0
RH
2376
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);
2381 }
2382
2383 gen_stack_update(s, -8 * size);
2384}
2385
2c0262af
FB
2386static void gen_popa(DisasContext *s)
2387{
d37ea0c0
RH
2388 TCGMemOp s_ot = s->ss32 ? MO_32 : MO_16;
2389 TCGMemOp d_ot = s->dflag;
2390 int size = 1 << d_ot;
2c0262af 2391 int i;
d37ea0c0
RH
2392
2393 for (i = 0; i < 8; i++) {
2c0262af 2394 /* ESP is not reloaded */
d37ea0c0
RH
2395 if (7 - i == R_ESP) {
2396 continue;
2c0262af 2397 }
d37ea0c0
RH
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);
1d1cc4d0
RH
2400 gen_op_ld_v(s, d_ot, cpu_T0, cpu_A0);
2401 gen_op_mov_reg_v(d_ot, 7 - i, cpu_T0);
2c0262af 2402 }
d37ea0c0
RH
2403
2404 gen_stack_update(s, 8 * size);
2c0262af
FB
2405}
2406
2c0262af
FB
2407static void gen_enter(DisasContext *s, int esp_addend, int level)
2408{
743e398e
RH
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;
2c0262af 2412
743e398e 2413 /* Push BP; compute FrameTemp into T1. */
1d1cc4d0
RH
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);
743e398e
RH
2416 gen_op_st_v(s, d_ot, cpu_regs[R_EBP], cpu_A0);
2417
2418 level &= 31;
2419 if (level != 0) {
2420 int i;
2421
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);
2427
1d1cc4d0 2428 tcg_gen_subi_tl(cpu_A0, cpu_T1, size * i);
743e398e
RH
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);
8f091a59 2431 }
743e398e
RH
2432
2433 /* Push the current FrameTemp as the last level. */
1d1cc4d0 2434 tcg_gen_subi_tl(cpu_A0, cpu_T1, size * level);
743e398e 2435 gen_lea_v_seg(s, a_ot, cpu_A0, R_SS, -1);
1d1cc4d0 2436 gen_op_st_v(s, d_ot, cpu_T1, cpu_A0);
2c0262af 2437 }
743e398e
RH
2438
2439 /* Copy the FrameTemp value to EBP. */
1d1cc4d0 2440 gen_op_mov_reg_v(a_ot, R_EBP, cpu_T1);
743e398e
RH
2441
2442 /* Compute the final value of ESP. */
1d1cc4d0
RH
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);
2c0262af
FB
2445}
2446
2045f04c
RH
2447static void gen_leave(DisasContext *s)
2448{
2449 TCGMemOp d_ot = mo_pushpop(s, s->dflag);
2450 TCGMemOp a_ot = mo_stacksize(s);
2451
2452 gen_lea_v_seg(s, a_ot, cpu_regs[R_EBP], R_SS, -1);
1d1cc4d0 2453 gen_op_ld_v(s, d_ot, cpu_T0, cpu_A0);
2045f04c 2454
1d1cc4d0 2455 tcg_gen_addi_tl(cpu_T1, cpu_regs[R_EBP], 1 << d_ot);
2045f04c 2456
1d1cc4d0
RH
2457 gen_op_mov_reg_v(d_ot, R_EBP, cpu_T0);
2458 gen_op_mov_reg_v(a_ot, R_ESP, cpu_T1);
2045f04c
RH
2459}
2460
14ce26e7 2461static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
2c0262af 2462{
773cdfcc 2463 gen_update_cc_op(s);
14ce26e7 2464 gen_jmp_im(cur_eip);
77b2bc2c 2465 gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
6cf147aa 2466 s->base.is_jmp = DISAS_NORETURN;
2c0262af
FB
2467}
2468
b9f9c5b4
RH
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. */
2471static void gen_illegal_opcode(DisasContext *s)
2472{
2473 gen_exception(s, EXCP06_ILLOP, s->pc_start - s->cs_base);
2474}
2475
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. */
2479static void gen_unknown_opcode(CPUX86State *env, DisasContext *s)
2480{
2481 gen_illegal_opcode(s);
2482
2483 if (qemu_loglevel_mask(LOG_UNIMP)) {
2484 target_ulong pc = s->pc_start, end = s->pc;
1ee73216 2485 qemu_log_lock();
b9f9c5b4
RH
2486 qemu_log("ILLOPC: " TARGET_FMT_lx ":", pc);
2487 for (; pc < end; ++pc) {
2488 qemu_log(" %02x", cpu_ldub_code(env, pc));
2489 }
2490 qemu_log("\n");
1ee73216 2491 qemu_log_unlock();
b9f9c5b4
RH
2492 }
2493}
2494
2c0262af 2495/* an interrupt is different from an exception because of the
7f75ffd3 2496 privilege checks */
5fafdf24 2497static void gen_interrupt(DisasContext *s, int intno,
14ce26e7 2498 target_ulong cur_eip, target_ulong next_eip)
2c0262af 2499{
773cdfcc 2500 gen_update_cc_op(s);
14ce26e7 2501 gen_jmp_im(cur_eip);
77b2bc2c 2502 gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
a7812ae4 2503 tcg_const_i32(next_eip - cur_eip));
6cf147aa 2504 s->base.is_jmp = DISAS_NORETURN;
2c0262af
FB
2505}
2506
14ce26e7 2507static void gen_debug(DisasContext *s, target_ulong cur_eip)
2c0262af 2508{
773cdfcc 2509 gen_update_cc_op(s);
14ce26e7 2510 gen_jmp_im(cur_eip);
4a7443be 2511 gen_helper_debug(cpu_env);
6cf147aa 2512 s->base.is_jmp = DISAS_NORETURN;
2c0262af
FB
2513}
2514
7f0b7141
RH
2515static void gen_set_hflag(DisasContext *s, uint32_t mask)
2516{
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;
2524 }
2525}
2526
2527static void gen_reset_hflag(DisasContext *s, uint32_t mask)
2528{
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;
2536 }
2537}
2538
7d117ce8
RH
2539/* Clear BND registers during legacy branches. */
2540static void gen_bnd_jmp(DisasContext *s)
2541{
8b33e82b
PB
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. */
7d117ce8 2545 if ((s->prefix & PREFIX_REPNZ) == 0
8b33e82b
PB
2546 && (s->flags & HF_MPX_EN_MASK) != 0
2547 && (s->flags & HF_MPX_IU_MASK) != 0) {
7d117ce8
RH
2548 gen_helper_bnd_jmp(cpu_env);
2549 }
2550}
2551
f083d92c 2552/* Generate an end of block. Trace exception is also generated if needed.
c52ab08a
DE
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. */
1ebb1af1 2556static void
7f11636d 2557do_gen_eob_worker(DisasContext *s, bool inhibit, bool recheck_tf, bool jr)
2c0262af 2558{
773cdfcc 2559 gen_update_cc_op(s);
f083d92c
RH
2560
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);
2566 }
2567
6cf147aa 2568 if (s->base.tb->flags & HF_RF_MASK) {
f0967a1a 2569 gen_helper_reset_rf(cpu_env);
a2397807 2570 }
6cf147aa 2571 if (s->base.singlestep_enabled) {
4a7443be 2572 gen_helper_debug(cpu_env);
c52ab08a
DE
2573 } else if (recheck_tf) {
2574 gen_helper_rechecking_single_step(cpu_env);
2575 tcg_gen_exit_tb(0);
34865134 2576 } else if (s->tf) {
4a7443be 2577 gen_helper_single_step(cpu_env);
7f11636d
EC
2578 } else if (jr) {
2579 tcg_gen_lookup_and_goto_ptr();
2c0262af 2580 } else {
57fec1fe 2581 tcg_gen_exit_tb(0);
2c0262af 2582 }
6cf147aa 2583 s->base.is_jmp = DISAS_NORETURN;
2c0262af
FB
2584}
2585
1ebb1af1
EC
2586static inline void
2587gen_eob_worker(DisasContext *s, bool inhibit, bool recheck_tf)
2588{
7f11636d 2589 do_gen_eob_worker(s, inhibit, recheck_tf, false);
1ebb1af1
EC
2590}
2591
c52ab08a
DE
2592/* End of block.
2593 If INHIBIT, set HF_INHIBIT_IRQ_MASK if it isn't already set. */
2594static void gen_eob_inhibit_irq(DisasContext *s, bool inhibit)
2595{
2596 gen_eob_worker(s, inhibit, false);
2597}
2598
f083d92c
RH
2599/* End of block, resetting the inhibit irq flag. */
2600static void gen_eob(DisasContext *s)
2601{
c52ab08a 2602 gen_eob_worker(s, false, false);
f083d92c
RH
2603}
2604
1ebb1af1
EC
2605/* Jump to register */
2606static void gen_jr(DisasContext *s, TCGv dest)
2607{
7f11636d 2608 do_gen_eob_worker(s, false, false, true);
1ebb1af1
EC
2609}
2610
2c0262af
FB
2611/* generate a jump to eip. No segment change must happen before as a
2612 direct call to the next block may occur */
14ce26e7 2613static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
2c0262af 2614{
a3251186
RH
2615 gen_update_cc_op(s);
2616 set_cc_op(s, CC_OP_DYNAMIC);
2c0262af 2617 if (s->jmp_opt) {
6e256c93 2618 gen_goto_tb(s, tb_num, eip);
2c0262af 2619 } else {
14ce26e7 2620 gen_jmp_im(eip);
2c0262af
FB
2621 gen_eob(s);
2622 }
2623}
2624
14ce26e7
FB
2625static void gen_jmp(DisasContext *s, target_ulong eip)
2626{
2627 gen_jmp_tb(s, eip, 0);
2628}
2629
323d1876 2630static inline void gen_ldq_env_A0(DisasContext *s, int offset)
8686c490 2631{
3c5f4116 2632 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
b6abf97d 2633 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
8686c490 2634}
664e0f19 2635
323d1876 2636static inline void gen_stq_env_A0(DisasContext *s, int offset)
8686c490 2637{
b6abf97d 2638 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
3523e4bd 2639 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
8686c490 2640}
664e0f19 2641
323d1876 2642static inline void gen_ldo_env_A0(DisasContext *s, int offset)
8686c490 2643{
5c42a7cd 2644 int mem_index = s->mem_index;
3c5f4116 2645 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
19cbd87c 2646 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(0)));
8686c490 2647 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
3c5f4116 2648 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
19cbd87c 2649 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(1)));
8686c490 2650}
14ce26e7 2651
323d1876 2652static inline void gen_sto_env_A0(DisasContext *s, int offset)
8686c490 2653{
5c42a7cd 2654 int mem_index = s->mem_index;
19cbd87c 2655 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(0)));
3523e4bd 2656 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
8686c490 2657 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
19cbd87c 2658 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(ZMMReg, ZMM_Q(1)));
3523e4bd 2659 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
8686c490 2660}
14ce26e7 2661
5af45186
FB
2662static inline void gen_op_movo(int d_offset, int s_offset)
2663{
19cbd87c
EH
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)));
5af45186
FB
2668}
2669
2670static inline void gen_op_movq(int d_offset, int s_offset)
2671{
b6abf97d
FB
2672 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
2673 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
5af45186
FB
2674}
2675
2676static inline void gen_op_movl(int d_offset, int s_offset)
2677{
b6abf97d
FB
2678 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
2679 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
5af45186
FB
2680}
2681
2682static inline void gen_op_movq_env_0(int d_offset)
2683{
b6abf97d
FB
2684 tcg_gen_movi_i64(cpu_tmp1_i64, 0);
2685 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
5af45186 2686}
664e0f19 2687
d3eb5eae
BS
2688typedef void (*SSEFunc_i_ep)(TCGv_i32 val, TCGv_ptr env, TCGv_ptr reg);
2689typedef void (*SSEFunc_l_ep)(TCGv_i64 val, TCGv_ptr env, TCGv_ptr reg);
2690typedef void (*SSEFunc_0_epi)(TCGv_ptr env, TCGv_ptr reg, TCGv_i32 val);
2691typedef void (*SSEFunc_0_epl)(TCGv_ptr env, TCGv_ptr reg, TCGv_i64 val);
2692typedef void (*SSEFunc_0_epp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b);
2693typedef void (*SSEFunc_0_eppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2694 TCGv_i32 val);
c4baa050 2695typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
d3eb5eae
BS
2696typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2697 TCGv val);
c4baa050 2698
5af45186
FB
2699#define SSE_SPECIAL ((void *)1)
2700#define SSE_DUMMY ((void *)2)
664e0f19 2701
a7812ae4
PB
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, }
5af45186 2705
d3eb5eae 2706static const SSEFunc_0_epp sse_op_table1[256][4] = {
a35f3ec7
AJ
2707 /* 3DNow! extensions */
2708 [0x0e] = { SSE_DUMMY }, /* femms */
2709 [0x0f] = { SSE_DUMMY }, /* pf... */
664e0f19
FB
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 */
465e9838 2713 [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
664e0f19 2714 [0x13] = { SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd */
a7812ae4
PB
2715 [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
2716 [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
664e0f19
FB
2717 [0x16] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd, movshdup */
2718 [0x17] = { SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd */
2719
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 */
d9f4bb27 2723 [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
664e0f19
FB
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 */
a7812ae4
PB
2726 [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
2727 [0x2f] = { gen_helper_comiss, gen_helper_comisd },
664e0f19
FB
2728 [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
2729 [0x51] = SSE_FOP(sqrt),
a7812ae4
PB
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 */
664e0f19
FB
2736 [0x58] = SSE_FOP(add),
2737 [0x59] = SSE_FOP(mul),
a7812ae4
PB
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 },
664e0f19
FB
2741 [0x5c] = SSE_FOP(sub),
2742 [0x5d] = SSE_FOP(min),
2743 [0x5e] = SSE_FOP(div),
2744 [0x5f] = SSE_FOP(max),
2745
2746 [0xc2] = SSE_FOP(cmpeq),
d3eb5eae
BS
2747 [0xc6] = { (SSEFunc_0_epp)gen_helper_shufps,
2748 (SSEFunc_0_epp)gen_helper_shufpd }, /* XXX: casts */
664e0f19 2749
7073fbad
RH
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 },
4242b1bd 2753
664e0f19
FB
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),
a7812ae4
PB
2767 [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
2768 [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
664e0f19
FB
2769 [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
2770 [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
d3eb5eae
BS
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 */
664e0f19
FB
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),
a35f3ec7 2781 [0x77] = { SSE_DUMMY }, /* emms */
d9f4bb27
AP
2782 [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
2783 [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
a7812ae4
PB
2784 [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
2785 [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
664e0f19
FB
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 */
a7812ae4 2790 [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
664e0f19
FB
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),
a7812ae4 2812 [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
664e0f19
FB
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),
465e9838 2822 [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
664e0f19
FB
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),
d3eb5eae
BS
2829 [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
2830 (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
664e0f19
FB
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),
2838};
2839
d3eb5eae 2840static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
664e0f19
FB
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),
a7812ae4 2848 [16 + 3] = { NULL, gen_helper_psrldq_xmm },
664e0f19 2849 [16 + 6] = MMX_OP2(psllq),
a7812ae4 2850 [16 + 7] = { NULL, gen_helper_pslldq_xmm },
664e0f19
FB
2851};
2852
d3eb5eae 2853static const SSEFunc_0_epi sse_op_table3ai[] = {
a7812ae4 2854 gen_helper_cvtsi2ss,
11f8cdbc 2855 gen_helper_cvtsi2sd
c4baa050 2856};
a7812ae4 2857
11f8cdbc 2858#ifdef TARGET_X86_64
d3eb5eae 2859static const SSEFunc_0_epl sse_op_table3aq[] = {
11f8cdbc
SW
2860 gen_helper_cvtsq2ss,
2861 gen_helper_cvtsq2sd
2862};
2863#endif
2864
d3eb5eae 2865static const SSEFunc_i_ep sse_op_table3bi[] = {
a7812ae4 2866 gen_helper_cvttss2si,
a7812ae4 2867 gen_helper_cvtss2si,
bedc2ac1 2868 gen_helper_cvttsd2si,
11f8cdbc 2869 gen_helper_cvtsd2si
664e0f19 2870};
3b46e624 2871
11f8cdbc 2872#ifdef TARGET_X86_64
d3eb5eae 2873static const SSEFunc_l_ep sse_op_table3bq[] = {
11f8cdbc 2874 gen_helper_cvttss2sq,
11f8cdbc 2875 gen_helper_cvtss2sq,
bedc2ac1 2876 gen_helper_cvttsd2sq,
11f8cdbc
SW
2877 gen_helper_cvtsd2sq
2878};
2879#endif
2880
d3eb5eae 2881static const SSEFunc_0_epp sse_op_table4[8][4] = {
664e0f19
FB
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),
2890};
3b46e624 2891
d3eb5eae 2892static const SSEFunc_0_epp sse_op_table5[256] = {
a7812ae4
PB
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 */
a35f3ec7
AJ
2917};
2918
d3eb5eae
BS
2919struct SSEOpHelper_epp {
2920 SSEFunc_0_epp op[2];
c4baa050
BS
2921 uint32_t ext_mask;
2922};
2923
d3eb5eae
BS
2924struct SSEOpHelper_eppi {
2925 SSEFunc_0_eppi op[2];
c4baa050 2926 uint32_t ext_mask;
222a3336 2927};
c4baa050 2928
222a3336 2929#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
a7812ae4
PB
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 }
222a3336 2932#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
e71827bc
AJ
2933#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
2934 CPUID_EXT_PCLMULQDQ }
d640045a 2935#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
c4baa050 2936
d3eb5eae 2937static const struct SSEOpHelper_epp sse_op_table6[256] = {
222a3336
AZ
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),
d640045a
AJ
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),
4242b1bd
AZ
2989};
2990
d3eb5eae 2991static const struct SSEOpHelper_eppi sse_op_table7[256] = {
222a3336
AZ
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),
e71827bc 3010 [0x44] = PCLMULQDQ_OP(pclmulqdq),
222a3336
AZ
3011 [0x60] = SSE42_OP(pcmpestrm),
3012 [0x61] = SSE42_OP(pcmpestri),
3013 [0x62] = SSE42_OP(pcmpistrm),
3014 [0x63] = SSE42_OP(pcmpistri),
d640045a 3015 [0xdf] = AESNI_OP(aeskeygenassist),
4242b1bd
AZ
3016};
3017
0af10c86
BS
3018static void gen_sse(CPUX86State *env, DisasContext *s, int b,
3019 target_ulong pc_start, int rex_r)
664e0f19 3020{
d67dc9e6 3021 int b1, op1_offset, op2_offset, is_xmm, val;
4eeb3939 3022 int modrm, mod, rm, reg;
d3eb5eae
BS
3023 SSEFunc_0_epp sse_fn_epp;
3024 SSEFunc_0_eppi sse_fn_eppi;
c4baa050 3025 SSEFunc_0_ppi sse_fn_ppi;
d3eb5eae 3026 SSEFunc_0_eppt sse_fn_eppt;
d67dc9e6 3027 TCGMemOp ot;
664e0f19
FB
3028
3029 b &= 0xff;
5fafdf24 3030 if (s->prefix & PREFIX_DATA)
664e0f19 3031 b1 = 1;
5fafdf24 3032 else if (s->prefix & PREFIX_REPZ)
664e0f19 3033 b1 = 2;
5fafdf24 3034 else if (s->prefix & PREFIX_REPNZ)
664e0f19
FB
3035 b1 = 3;
3036 else
3037 b1 = 0;
d3eb5eae
BS
3038 sse_fn_epp = sse_op_table1[b][b1];
3039 if (!sse_fn_epp) {
b9f9c5b4 3040 goto unknown_op;
c4baa050 3041 }
a35f3ec7 3042 if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
664e0f19
FB
3043 is_xmm = 1;
3044 } else {
3045 if (b1 == 0) {
3046 /* MMX case */
3047 is_xmm = 0;
3048 } else {
3049 is_xmm = 1;
3050 }
3051 }
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;
3056 }
3057 if (s->flags & HF_EM_MASK) {
3058 illegal_op:
b9f9c5b4 3059 gen_illegal_opcode(s);
664e0f19
FB
3060 return;
3061 }
b9f9c5b4
RH
3062 if (is_xmm
3063 && !(s->flags & HF_OSFXSR_MASK)
3064 && ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))) {
3065 goto unknown_op;
3066 }
e771edab 3067 if (b == 0x0e) {
b9f9c5b4
RH
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;
3071 }
e771edab 3072 /* femms */
d3eb5eae 3073 gen_helper_emms(cpu_env);
e771edab
AJ
3074 return;
3075 }
3076 if (b == 0x77) {
3077 /* emms */
d3eb5eae 3078 gen_helper_emms(cpu_env);
664e0f19
FB
3079 return;
3080 }
3081 /* prepare MMX state (XXX: optimize by storing fptt and fptags in
3082 the static cpu state) */
3083 if (!is_xmm) {
d3eb5eae 3084 gen_helper_enter_mmx(cpu_env);
664e0f19
FB
3085 }
3086
e3af7c78 3087 modrm = x86_ldub_code(env, s);
664e0f19
FB
3088 reg = ((modrm >> 3) & 7);
3089 if (is_xmm)
3090 reg |= rex_r;
3091 mod = (modrm >> 6) & 3;
d3eb5eae 3092 if (sse_fn_epp == SSE_SPECIAL) {
664e0f19
FB
3093 b |= (b1 << 8);
3094 switch(b) {
3095 case 0x0e7: /* movntq */
b9f9c5b4 3096 if (mod == 3) {
664e0f19 3097 goto illegal_op;
b9f9c5b4 3098 }
4eeb3939 3099 gen_lea_modrm(env, s, modrm);
323d1876 3100 gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
664e0f19
FB
3101 break;
3102 case 0x1e7: /* movntdq */
3103 case 0x02b: /* movntps */
3104 case 0x12b: /* movntps */
2e21e749
T
3105 if (mod == 3)
3106 goto illegal_op;
4eeb3939 3107 gen_lea_modrm(env, s, modrm);
323d1876 3108 gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
2e21e749 3109 break;
465e9838
FB
3110 case 0x3f0: /* lddqu */
3111 if (mod == 3)
664e0f19 3112 goto illegal_op;
4eeb3939 3113 gen_lea_modrm(env, s, modrm);
323d1876 3114 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
664e0f19 3115 break;
d9f4bb27
AP
3116 case 0x22b: /* movntss */
3117 case 0x32b: /* movntsd */
3118 if (mod == 3)
3119 goto illegal_op;
4eeb3939 3120 gen_lea_modrm(env, s, modrm);
d9f4bb27 3121 if (b1 & 1) {
07958082 3122 gen_stq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3123 xmm_regs[reg].ZMM_Q(0)));
d9f4bb27 3124 } else {
1d1cc4d0 3125 tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
19cbd87c 3126 xmm_regs[reg].ZMM_L(0)));
1d1cc4d0 3127 gen_op_st_v(s, MO_32, cpu_T0, cpu_A0);
d9f4bb27
AP
3128 }
3129 break;
664e0f19 3130 case 0x6e: /* movd mm, ea */
dabd98dd 3131#ifdef TARGET_X86_64
ab4e4aec 3132 if (s->dflag == MO_64) {
4ba9938c 3133 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
1d1cc4d0 3134 tcg_gen_st_tl(cpu_T0, cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
5fafdf24 3135 } else
dabd98dd
FB
3136#endif
3137 {
4ba9938c 3138 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
5af45186
FB
3139 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3140 offsetof(CPUX86State,fpregs[reg].mmx));
1d1cc4d0 3141 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
a7812ae4 3142 gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
dabd98dd 3143 }
664e0f19
FB
3144 break;
3145 case 0x16e: /* movd xmm, ea */
dabd98dd 3146#ifdef TARGET_X86_64
ab4e4aec 3147 if (s->dflag == MO_64) {
4ba9938c 3148 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
5af45186
FB
3149 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3150 offsetof(CPUX86State,xmm_regs[reg]));
1d1cc4d0 3151 gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T0);
5fafdf24 3152 } else
dabd98dd
FB
3153#endif
3154 {
4ba9938c 3155 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
5af45186
FB
3156 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3157 offsetof(CPUX86State,xmm_regs[reg]));
1d1cc4d0 3158 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
a7812ae4 3159 gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
dabd98dd 3160 }
664e0f19
FB
3161 break;
3162 case 0x6f: /* movq mm, ea */
3163 if (mod != 3) {
4eeb3939 3164 gen_lea_modrm(env, s, modrm);
323d1876 3165 gen_ldq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
664e0f19
FB
3166 } else {
3167 rm = (modrm & 7);
b6abf97d 3168 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
5af45186 3169 offsetof(CPUX86State,fpregs[rm].mmx));
b6abf97d 3170 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
5af45186 3171 offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3172 }
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) {
4eeb3939 3181 gen_lea_modrm(env, s, modrm);
323d1876 3182 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
664e0f19
FB
3183 } else {
3184 rm = (modrm & 7) | REX_B(s);
3185 gen_op_movo(offsetof(CPUX86State,xmm_regs[reg]),
3186 offsetof(CPUX86State,xmm_regs[rm]));
3187 }
3188 break;
3189 case 0x210: /* movss xmm, ea */
3190 if (mod != 3) {
4eeb3939 3191 gen_lea_modrm(env, s, modrm);
1d1cc4d0
RH
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)));
664e0f19
FB
3198 } else {
3199 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
3200 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)),
3201 offsetof(CPUX86State,xmm_regs[rm].ZMM_L(0)));
664e0f19
FB
3202 }
3203 break;
3204 case 0x310: /* movsd xmm, ea */
3205 if (mod != 3) {
4eeb3939 3206 gen_lea_modrm(env, s, modrm);
323d1876 3207 gen_ldq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3208 xmm_regs[reg].ZMM_Q(0)));
1d1cc4d0
RH
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)));
664e0f19
FB
3212 } else {
3213 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
3214 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3215 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
664e0f19
FB
3216 }
3217 break;
3218 case 0x012: /* movlps */
3219 case 0x112: /* movlpd */
3220 if (mod != 3) {
4eeb3939 3221 gen_lea_modrm(env, s, modrm);
323d1876 3222 gen_ldq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3223 xmm_regs[reg].ZMM_Q(0)));
664e0f19
FB
3224 } else {
3225 /* movhlps */
3226 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
3227 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3228 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(1)));
664e0f19
FB
3229 }
3230 break;
465e9838
FB
3231 case 0x212: /* movsldup */
3232 if (mod != 3) {
4eeb3939 3233 gen_lea_modrm(env, s, modrm);
323d1876 3234 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
465e9838
FB
3235 } else {
3236 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
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)));
465e9838 3241 }
19cbd87c
EH
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)));
465e9838
FB
3246 break;
3247 case 0x312: /* movddup */
3248 if (mod != 3) {
4eeb3939 3249 gen_lea_modrm(env, s, modrm);
323d1876 3250 gen_ldq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3251 xmm_regs[reg].ZMM_Q(0)));
465e9838
FB
3252 } else {
3253 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
3254 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3255 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
465e9838 3256 }
19cbd87c
EH
3257 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)),
3258 offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
465e9838 3259 break;
664e0f19
FB
3260 case 0x016: /* movhps */
3261 case 0x116: /* movhpd */
3262 if (mod != 3) {
4eeb3939 3263 gen_lea_modrm(env, s, modrm);
323d1876 3264 gen_ldq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3265 xmm_regs[reg].ZMM_Q(1)));
664e0f19
FB
3266 } else {
3267 /* movlhps */
3268 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
3269 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)),
3270 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
664e0f19
FB
3271 }
3272 break;
3273 case 0x216: /* movshdup */
3274 if (mod != 3) {
4eeb3939 3275 gen_lea_modrm(env, s, modrm);
323d1876 3276 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
664e0f19
FB
3277 } else {
3278 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
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)));
664e0f19 3283 }
19cbd87c
EH
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)));
664e0f19 3288 break;
d9f4bb27
AP
3289 case 0x178:
3290 case 0x378:
3291 {
3292 int bit_index, field_length;
3293
3294 if (b1 == 1 && reg != 0)
3295 goto illegal_op;
e3af7c78
PB
3296 field_length = x86_ldub_code(env, s) & 0x3F;
3297 bit_index = x86_ldub_code(env, s) & 0x3F;
d9f4bb27
AP
3298 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3299 offsetof(CPUX86State,xmm_regs[reg]));
3300 if (b1 == 1)
d3eb5eae
BS
3301 gen_helper_extrq_i(cpu_env, cpu_ptr0,
3302 tcg_const_i32(bit_index),
3303 tcg_const_i32(field_length));
d9f4bb27 3304 else
d3eb5eae
BS
3305 gen_helper_insertq_i(cpu_env, cpu_ptr0,
3306 tcg_const_i32(bit_index),
3307 tcg_const_i32(field_length));
d9f4bb27
AP
3308 }
3309 break;
664e0f19 3310 case 0x7e: /* movd ea, mm */
dabd98dd 3311#ifdef TARGET_X86_64
ab4e4aec 3312 if (s->dflag == MO_64) {
1d1cc4d0 3313 tcg_gen_ld_i64(cpu_T0, cpu_env,
5af45186 3314 offsetof(CPUX86State,fpregs[reg].mmx));
4ba9938c 3315 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
5fafdf24 3316 } else
dabd98dd
FB
3317#endif
3318 {
1d1cc4d0 3319 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
5af45186 3320 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
4ba9938c 3321 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
dabd98dd 3322 }
664e0f19
FB
3323 break;
3324 case 0x17e: /* movd ea, xmm */
dabd98dd 3325#ifdef TARGET_X86_64
ab4e4aec 3326 if (s->dflag == MO_64) {
1d1cc4d0 3327 tcg_gen_ld_i64(cpu_T0, cpu_env,
19cbd87c 3328 offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
4ba9938c 3329 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
5fafdf24 3330 } else
dabd98dd
FB
3331#endif
3332 {
1d1cc4d0 3333 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
19cbd87c 3334 offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
4ba9938c 3335 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
dabd98dd 3336 }
664e0f19
FB
3337 break;
3338 case 0x27e: /* movq xmm, ea */
3339 if (mod != 3) {
4eeb3939 3340 gen_lea_modrm(env, s, modrm);
323d1876 3341 gen_ldq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3342 xmm_regs[reg].ZMM_Q(0)));
664e0f19
FB
3343 } else {
3344 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
3345 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
3346 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
664e0f19 3347 }
19cbd87c 3348 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)));
664e0f19
FB
3349 break;
3350 case 0x7f: /* movq ea, mm */
3351 if (mod != 3) {
4eeb3939 3352 gen_lea_modrm(env, s, modrm);
323d1876 3353 gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
664e0f19
FB
3354 } else {
3355 rm = (modrm & 7);
3356 gen_op_movq(offsetof(CPUX86State,fpregs[rm].mmx),
3357 offsetof(CPUX86State,fpregs[reg].mmx));
3358 }
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) {
4eeb3939 3367 gen_lea_modrm(env, s, modrm);
323d1876 3368 gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
664e0f19
FB
3369 } else {
3370 rm = (modrm & 7) | REX_B(s);
3371 gen_op_movo(offsetof(CPUX86State,xmm_regs[rm]),
3372 offsetof(CPUX86State,xmm_regs[reg]));
3373 }
3374 break;
3375 case 0x211: /* movss ea, xmm */
3376 if (mod != 3) {
4eeb3939 3377 gen_lea_modrm(env, s, modrm);
1d1cc4d0
RH
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);
664e0f19
FB
3380 } else {
3381 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
3382 gen_op_movl(offsetof(CPUX86State,xmm_regs[rm].ZMM_L(0)),
3383 offsetof(CPUX86State,xmm_regs[reg].ZMM_L(0)));
664e0f19
FB
3384 }
3385 break;
3386 case 0x311: /* movsd ea, xmm */
3387 if (mod != 3) {
4eeb3939 3388 gen_lea_modrm(env, s, modrm);
323d1876 3389 gen_stq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3390 xmm_regs[reg].ZMM_Q(0)));
664e0f19
FB
3391 } else {
3392 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
3393 gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)),
3394 offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)));
664e0f19
FB
3395 }
3396 break;
3397 case 0x013: /* movlps */
3398 case 0x113: /* movlpd */
3399 if (mod != 3) {
4eeb3939 3400 gen_lea_modrm(env, s, modrm);
323d1876 3401 gen_stq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3402 xmm_regs[reg].ZMM_Q(0)));
664e0f19
FB
3403 } else {
3404 goto illegal_op;
3405 }
3406 break;
3407 case 0x017: /* movhps */
3408 case 0x117: /* movhpd */
3409 if (mod != 3) {
4eeb3939 3410 gen_lea_modrm(env, s, modrm);
323d1876 3411 gen_stq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3412 xmm_regs[reg].ZMM_Q(1)));
664e0f19
FB
3413 } else {
3414 goto illegal_op;
3415 }
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:
c045af25 3423 if (b1 >= 2) {
b9f9c5b4 3424 goto unknown_op;
c045af25 3425 }
e3af7c78 3426 val = x86_ldub_code(env, s);
664e0f19 3427 if (is_xmm) {
1d1cc4d0
RH
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)));
664e0f19
FB
3432 op1_offset = offsetof(CPUX86State,xmm_t0);
3433 } else {
1d1cc4d0
RH
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)));
664e0f19
FB
3438 op1_offset = offsetof(CPUX86State,mmx_t0);
3439 }
d3eb5eae
BS
3440 sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
3441 (((modrm >> 3)) & 7)][b1];
3442 if (!sse_fn_epp) {
b9f9c5b4 3443 goto unknown_op;
c4baa050 3444 }
664e0f19
FB
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);
3451 }
5af45186
FB
3452 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3453 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
d3eb5eae 3454 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3455 break;
3456 case 0x050: /* movmskps */
664e0f19 3457 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3458 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3459 offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3460 gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
a7fbcbe5 3461 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
664e0f19
FB
3462 break;
3463 case 0x150: /* movmskpd */
664e0f19 3464 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3465 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3466 offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3467 gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
a7fbcbe5 3468 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
664e0f19
FB
3469 break;
3470 case 0x02a: /* cvtpi2ps */
3471 case 0x12a: /* cvtpi2pd */
d3eb5eae 3472 gen_helper_enter_mmx(cpu_env);
664e0f19 3473 if (mod != 3) {
4eeb3939 3474 gen_lea_modrm(env, s, modrm);
664e0f19 3475 op2_offset = offsetof(CPUX86State,mmx_t0);
323d1876 3476 gen_ldq_env_A0(s, op2_offset);
664e0f19
FB
3477 } else {
3478 rm = (modrm & 7);
3479 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3480 }
3481 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
5af45186
FB
3482 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3483 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
664e0f19
FB
3484 switch(b >> 8) {
3485 case 0x0:
d3eb5eae 3486 gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3487 break;
3488 default:
3489 case 0x1:
d3eb5eae 3490 gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3491 break;
3492 }
3493 break;
3494 case 0x22a: /* cvtsi2ss */
3495 case 0x32a: /* cvtsi2sd */
ab4e4aec 3496 ot = mo_64_32(s->dflag);
0af10c86 3497 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
664e0f19 3498 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
5af45186 3499 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4ba9938c 3500 if (ot == MO_32) {
d3eb5eae 3501 SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
1d1cc4d0 3502 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
d3eb5eae 3503 sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
28e10711 3504 } else {
11f8cdbc 3505#ifdef TARGET_X86_64
d3eb5eae 3506 SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
1d1cc4d0 3507 sse_fn_epl(cpu_env, cpu_ptr0, cpu_T0);
11f8cdbc
SW
3508#else
3509 goto illegal_op;
3510#endif
28e10711 3511 }
664e0f19
FB
3512 break;
3513 case 0x02c: /* cvttps2pi */
3514 case 0x12c: /* cvttpd2pi */
3515 case 0x02d: /* cvtps2pi */
3516 case 0x12d: /* cvtpd2pi */
d3eb5eae 3517 gen_helper_enter_mmx(cpu_env);
664e0f19 3518 if (mod != 3) {
4eeb3939 3519 gen_lea_modrm(env, s, modrm);
664e0f19 3520 op2_offset = offsetof(CPUX86State,xmm_t0);
323d1876 3521 gen_ldo_env_A0(s, op2_offset);
664e0f19
FB
3522 } else {
3523 rm = (modrm & 7) | REX_B(s);
3524 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3525 }
3526 op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
5af45186
FB
3527 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3528 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
664e0f19
FB
3529 switch(b) {
3530 case 0x02c:
d3eb5eae 3531 gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3532 break;
3533 case 0x12c:
d3eb5eae 3534 gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3535 break;
3536 case 0x02d:
d3eb5eae 3537 gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3538 break;
3539 case 0x12d:
d3eb5eae 3540 gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3541 break;
3542 }
3543 break;
3544 case 0x22c: /* cvttss2si */
3545 case 0x32c: /* cvttsd2si */
3546 case 0x22d: /* cvtss2si */
3547 case 0x32d: /* cvtsd2si */
ab4e4aec 3548 ot = mo_64_32(s->dflag);
31313213 3549 if (mod != 3) {
4eeb3939 3550 gen_lea_modrm(env, s, modrm);
31313213 3551 if ((b >> 8) & 1) {
19cbd87c 3552 gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.ZMM_Q(0)));
31313213 3553 } else {
1d1cc4d0
RH
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)));
31313213
FB
3556 }
3557 op2_offset = offsetof(CPUX86State,xmm_t0);
3558 } else {
3559 rm = (modrm & 7) | REX_B(s);
3560 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3561 }
5af45186 3562 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
4ba9938c 3563 if (ot == MO_32) {
d3eb5eae 3564 SSEFunc_i_ep sse_fn_i_ep =
bedc2ac1 3565 sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
d3eb5eae 3566 sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
1d1cc4d0 3567 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
5af45186 3568 } else {
11f8cdbc 3569#ifdef TARGET_X86_64
d3eb5eae 3570 SSEFunc_l_ep sse_fn_l_ep =
bedc2ac1 3571 sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
1d1cc4d0 3572 sse_fn_l_ep(cpu_T0, cpu_env, cpu_ptr0);
11f8cdbc
SW
3573#else
3574 goto illegal_op;
3575#endif
5af45186 3576 }
1d1cc4d0 3577 gen_op_mov_reg_v(ot, reg, cpu_T0);
664e0f19
FB
3578 break;
3579 case 0xc4: /* pinsrw */
5fafdf24 3580 case 0x1c4:
d1e42c5c 3581 s->rip_offset = 1;
4ba9938c 3582 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
e3af7c78 3583 val = x86_ldub_code(env, s);
664e0f19
FB
3584 if (b1) {
3585 val &= 7;
1d1cc4d0 3586 tcg_gen_st16_tl(cpu_T0, cpu_env,
19cbd87c 3587 offsetof(CPUX86State,xmm_regs[reg].ZMM_W(val)));
664e0f19
FB
3588 } else {
3589 val &= 3;
1d1cc4d0 3590 tcg_gen_st16_tl(cpu_T0, cpu_env,
5af45186 3591 offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
664e0f19
FB
3592 }
3593 break;
3594 case 0xc5: /* pextrw */
5fafdf24 3595 case 0x1c5:
664e0f19
FB
3596 if (mod != 3)
3597 goto illegal_op;
ab4e4aec 3598 ot = mo_64_32(s->dflag);
e3af7c78 3599 val = x86_ldub_code(env, s);
664e0f19
FB
3600 if (b1) {
3601 val &= 7;
3602 rm = (modrm & 7) | REX_B(s);
1d1cc4d0 3603 tcg_gen_ld16u_tl(cpu_T0, cpu_env,
19cbd87c 3604 offsetof(CPUX86State,xmm_regs[rm].ZMM_W(val)));
664e0f19
FB
3605 } else {
3606 val &= 3;
3607 rm = (modrm & 7);
1d1cc4d0 3608 tcg_gen_ld16u_tl(cpu_T0, cpu_env,
5af45186 3609 offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
664e0f19
FB
3610 }
3611 reg = ((modrm >> 3) & 7) | rex_r;
1d1cc4d0 3612 gen_op_mov_reg_v(ot, reg, cpu_T0);
664e0f19
FB
3613 break;
3614 case 0x1d6: /* movq ea, xmm */
3615 if (mod != 3) {
4eeb3939 3616 gen_lea_modrm(env, s, modrm);
323d1876 3617 gen_stq_env_A0(s, offsetof(CPUX86State,
19cbd87c 3618 xmm_regs[reg].ZMM_Q(0)));
664e0f19
FB
3619 } else {
3620 rm = (modrm & 7) | REX_B(s);
19cbd87c
EH
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)));
664e0f19
FB
3624 }
3625 break;
3626 case 0x2d6: /* movq2dq */
d3eb5eae 3627 gen_helper_enter_mmx(cpu_env);
480c1cdb 3628 rm = (modrm & 7);
19cbd87c 3629 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(0)),
480c1cdb 3630 offsetof(CPUX86State,fpregs[rm].mmx));
19cbd87c 3631 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].ZMM_Q(1)));
664e0f19
FB
3632 break;
3633 case 0x3d6: /* movdq2q */
d3eb5eae 3634 gen_helper_enter_mmx(cpu_env);
480c1cdb
FB
3635 rm = (modrm & 7) | REX_B(s);
3636 gen_op_movq(offsetof(CPUX86State,fpregs[reg & 7].mmx),
19cbd87c 3637 offsetof(CPUX86State,xmm_regs[rm].ZMM_Q(0)));
664e0f19
FB
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);
5af45186 3645 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3646 gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
664e0f19
FB
3647 } else {
3648 rm = (modrm & 7);
5af45186 3649 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
d3eb5eae 3650 gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
664e0f19
FB
3651 }
3652 reg = ((modrm >> 3) & 7) | rex_r;
a7fbcbe5 3653 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
664e0f19 3654 break;
111994ee 3655
4242b1bd 3656 case 0x138:
000cacf6 3657 case 0x038:
4242b1bd 3658 b = modrm;
111994ee
RH
3659 if ((b & 0xf0) == 0xf0) {
3660 goto do_0f_38_fx;
3661 }
e3af7c78 3662 modrm = x86_ldub_code(env, s);
4242b1bd
AZ
3663 rm = modrm & 7;
3664 reg = ((modrm >> 3) & 7) | rex_r;
3665 mod = (modrm >> 6) & 3;
c045af25 3666 if (b1 >= 2) {
b9f9c5b4 3667 goto unknown_op;
c045af25 3668 }
4242b1bd 3669
d3eb5eae
BS
3670 sse_fn_epp = sse_op_table6[b].op[b1];
3671 if (!sse_fn_epp) {
b9f9c5b4 3672 goto unknown_op;
c4baa050 3673 }
222a3336
AZ
3674 if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
3675 goto illegal_op;
4242b1bd
AZ
3676
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);
4eeb3939 3683 gen_lea_modrm(env, s, modrm);
222a3336
AZ
3684 switch (b) {
3685 case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
3686 case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
3687 case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
323d1876 3688 gen_ldq_env_A0(s, op2_offset +
19cbd87c 3689 offsetof(ZMMReg, ZMM_Q(0)));
222a3336
AZ
3690 break;
3691 case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
3692 case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3c5f4116
RH
3693 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
3694 s->mem_index, MO_LEUL);
222a3336 3695 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
19cbd87c 3696 offsetof(ZMMReg, ZMM_L(0)));
222a3336
AZ
3697 break;
3698 case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3c5f4116
RH
3699 tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
3700 s->mem_index, MO_LEUW);
222a3336 3701 tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
19cbd87c 3702 offsetof(ZMMReg, ZMM_W(0)));
222a3336
AZ
3703 break;
3704 case 0x2a: /* movntqda */
323d1876 3705 gen_ldo_env_A0(s, op1_offset);
222a3336
AZ
3706 return;
3707 default:
323d1876 3708 gen_ldo_env_A0(s, op2_offset);
222a3336 3709 }
4242b1bd
AZ
3710 }
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);
4eeb3939 3717 gen_lea_modrm(env, s, modrm);
323d1876 3718 gen_ldq_env_A0(s, op2_offset);
4242b1bd
AZ
3719 }
3720 }
d3eb5eae 3721 if (sse_fn_epp == SSE_SPECIAL) {
b9f9c5b4 3722 goto unknown_op;
c4baa050 3723 }
222a3336 3724
4242b1bd
AZ
3725 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3726 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 3727 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
222a3336 3728
3ca51d07
RH
3729 if (b == 0x17) {
3730 set_cc_op(s, CC_OP_EFLAGS);
3731 }
4242b1bd 3732 break;
111994ee
RH
3733
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);
e3af7c78 3739 modrm = x86_ldub_code(env, s);
222a3336
AZ
3740 reg = ((modrm >> 3) & 7) | rex_r;
3741
111994ee
RH
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;
3748 }
3749 if ((b & 0xff) == 0xf0) {
4ba9938c 3750 ot = MO_8;
ab4e4aec 3751 } else if (s->dflag != MO_64) {
4ba9938c 3752 ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
111994ee 3753 } else {
4ba9938c 3754 ot = MO_64;
111994ee 3755 }
4242b1bd 3756
24b9c00f 3757 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
111994ee 3758 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
1d1cc4d0
RH
3759 gen_helper_crc32(cpu_T0, cpu_tmp2_i32,
3760 cpu_T0, tcg_const_i32(8 << ot));
222a3336 3761
ab4e4aec 3762 ot = mo_64_32(s->dflag);
1d1cc4d0 3763 gen_op_mov_reg_v(ot, reg, cpu_T0);
111994ee 3764 break;
222a3336 3765
111994ee
RH
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;
3773 }
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;
3779 }
ab4e4aec 3780 if (s->dflag != MO_64) {
4ba9938c 3781 ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
111994ee 3782 } else {
4ba9938c 3783 ot = MO_64;
111994ee
RH
3784 }
3785
3655a19f 3786 gen_lea_modrm(env, s, modrm);
111994ee 3787 if ((b & 1) == 0) {
1d1cc4d0 3788 tcg_gen_qemu_ld_tl(cpu_T0, cpu_A0,
3655a19f 3789 s->mem_index, ot | MO_BE);
1d1cc4d0 3790 gen_op_mov_reg_v(ot, reg, cpu_T0);
111994ee 3791 } else {
3655a19f
RH
3792 tcg_gen_qemu_st_tl(cpu_regs[reg], cpu_A0,
3793 s->mem_index, ot | MO_BE);
111994ee
RH
3794 }
3795 break;
3796
7073fbad
RH
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;
3802 }
ab4e4aec 3803 ot = mo_64_32(s->dflag);
7073fbad 3804 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
1d1cc4d0
RH
3805 tcg_gen_andc_tl(cpu_T0, cpu_regs[s->vex_v], cpu_T0);
3806 gen_op_mov_reg_v(ot, reg, cpu_T0);
7073fbad
RH
3807 gen_op_update1_cc();
3808 set_cc_op(s, CC_OP_LOGICB + ot);
3809 break;
3810
c7ab7565
RH
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;
3816 }
ab4e4aec 3817 ot = mo_64_32(s->dflag);
c7ab7565
RH
3818 {
3819 TCGv bound, zero;
3820
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]);
1d1cc4d0 3825 tcg_gen_shr_tl(cpu_T0, cpu_T0, cpu_A0);
c7ab7565 3826
4ba9938c 3827 bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
c7ab7565 3828 zero = tcg_const_tl(0);
1d1cc4d0
RH
3829 tcg_gen_movcond_tl(TCG_COND_LEU, cpu_T0, cpu_A0, bound,
3830 cpu_T0, zero);
c7ab7565
RH
3831 tcg_temp_free(zero);
3832
3833 /* Extract the LEN into a mask. Lengths larger than
3834 operand size get all ones. */
04fc2f1c 3835 tcg_gen_extract_tl(cpu_A0, cpu_regs[s->vex_v], 8, 8);
c7ab7565
RH
3836 tcg_gen_movcond_tl(TCG_COND_LEU, cpu_A0, cpu_A0, bound,
3837 cpu_A0, bound);
3838 tcg_temp_free(bound);
1d1cc4d0
RH
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);
c7ab7565 3843
1d1cc4d0 3844 gen_op_mov_reg_v(ot, reg, cpu_T0);
c7ab7565
RH
3845 gen_op_update1_cc();
3846 set_cc_op(s, CC_OP_LOGICB + ot);
3847 }
3848 break;
3849
02ea1e6b
RH
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;
3855 }
ab4e4aec 3856 ot = mo_64_32(s->dflag);
02ea1e6b 3857 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
1d1cc4d0 3858 tcg_gen_ext8u_tl(cpu_T1, cpu_regs[s->vex_v]);
02ea1e6b 3859 {
4ba9938c 3860 TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
02ea1e6b
RH
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,
1d1cc4d0
RH
3864 cpu_T1, bound);
3865 tcg_gen_movcond_tl(TCG_COND_GT, cpu_T1, cpu_T1,
3866 bound, bound, cpu_T1);
02ea1e6b
RH
3867 tcg_temp_free(bound);
3868 }
3869 tcg_gen_movi_tl(cpu_A0, -1);
1d1cc4d0
RH
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);
02ea1e6b
RH
3873 gen_op_update1_cc();
3874 set_cc_op(s, CC_OP_BMILGB + ot);
3875 break;
3876
5f1f4b17
RH
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;
3882 }
ab4e4aec 3883 ot = mo_64_32(s->dflag);
5f1f4b17
RH
3884 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3885 switch (ot) {
5f1f4b17 3886 default:
1d1cc4d0 3887 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
a4bcea3d
RH
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);
5f1f4b17
RH
3893 break;
3894#ifdef TARGET_X86_64
4ba9938c 3895 case MO_64:
1d1cc4d0
RH
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);
5f1f4b17
RH
3900 break;
3901#endif
3902 }
3903 break;
3904
0592f74a
RH
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;
3910 }
ab4e4aec 3911 ot = mo_64_32(s->dflag);
0592f74a
RH
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. */
ab4e4aec 3915 if (ot == MO_64) {
1d1cc4d0 3916 tcg_gen_mov_tl(cpu_T1, cpu_regs[s->vex_v]);
0592f74a 3917 } else {
1d1cc4d0 3918 tcg_gen_ext32u_tl(cpu_T1, cpu_regs[s->vex_v]);
0592f74a 3919 }
1d1cc4d0 3920 gen_helper_pdep(cpu_regs[reg], cpu_T0, cpu_T1);
0592f74a
RH
3921 break;
3922
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;
3928 }
ab4e4aec 3929 ot = mo_64_32(s->dflag);
0592f74a
RH
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. */
ab4e4aec 3933 if (ot == MO_64) {
1d1cc4d0 3934 tcg_gen_mov_tl(cpu_T1, cpu_regs[s->vex_v]);
0592f74a 3935 } else {
1d1cc4d0 3936 tcg_gen_ext32u_tl(cpu_T1, cpu_regs[s->vex_v]);
0592f74a 3937 }
1d1cc4d0 3938 gen_helper_pext(cpu_regs[reg], cpu_T0, cpu_T1);
0592f74a
RH
3939 break;
3940
cd7f97ca
RH
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 {
76f13133 3946 TCGv carry_in, carry_out, zero;
cd7f97ca
RH
3947 int end_op;
3948
ab4e4aec 3949 ot = mo_64_32(s->dflag);
cd7f97ca
RH
3950 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3951
3952 /* Re-use the carry-out from a previous round. */
f764718d 3953 carry_in = NULL;
cd7f97ca
RH
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;
3962 }
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;
3970 }
3971 break;
3972 case CC_OP_ADCOX:
3973 end_op = CC_OP_ADCOX;
3974 carry_in = carry_out;
3975 break;
3976 default:
c53de1a2 3977 end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
cd7f97ca
RH
3978 break;
3979 }
3980 /* If we can't reuse carry-out, get it out of EFLAGS. */
f764718d 3981 if (!carry_in) {
cd7f97ca
RH
3982 if (s->cc_op != CC_OP_ADCX && s->cc_op != CC_OP_ADOX) {
3983 gen_compute_eflags(s);
3984 }
3985 carry_in = cpu_tmp0;
04fc2f1c
RH
3986 tcg_gen_extract_tl(carry_in, cpu_cc_src,
3987 ctz32(b == 0x1f6 ? CC_C : CC_O), 1);
cd7f97ca
RH
3988 }
3989
3990 switch (ot) {
3991#ifdef TARGET_X86_64
4ba9938c 3992 case MO_32:
cd7f97ca
RH
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]);
1d1cc4d0
RH
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);
cd7f97ca
RH
4001 break;
4002#endif
4003 default:
4004 /* Otherwise compute the carry-out in two steps. */
76f13133 4005 zero = tcg_const_tl(0);
1d1cc4d0
RH
4006 tcg_gen_add2_tl(cpu_T0, carry_out,
4007 cpu_T0, zero,
76f13133
RH
4008 carry_in, zero);
4009 tcg_gen_add2_tl(cpu_regs[reg], carry_out,
4010 cpu_regs[reg], carry_out,
1d1cc4d0 4011 cpu_T0, zero);
76f13133 4012 tcg_temp_free(zero);
cd7f97ca
RH
4013 break;
4014 }
cd7f97ca
RH
4015 set_cc_op(s, end_op);
4016 }
4017 break;
4018
4a554890
RH
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;
4026 }
ab4e4aec 4027 ot = mo_64_32(s->dflag);
4a554890 4028 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4ba9938c 4029 if (ot == MO_64) {
1d1cc4d0 4030 tcg_gen_andi_tl(cpu_T1, cpu_regs[s->vex_v], 63);
4a554890 4031 } else {
1d1cc4d0 4032 tcg_gen_andi_tl(cpu_T1, cpu_regs[s->vex_v], 31);
4a554890
RH
4033 }
4034 if (b == 0x1f7) {
1d1cc4d0 4035 tcg_gen_shl_tl(cpu_T0, cpu_T0, cpu_T1);
4a554890 4036 } else if (b == 0x2f7) {
4ba9938c 4037 if (ot != MO_64) {
1d1cc4d0 4038 tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
4a554890 4039 }
1d1cc4d0 4040 tcg_gen_sar_tl(cpu_T0, cpu_T0, cpu_T1);
4a554890 4041 } else {
4ba9938c 4042 if (ot != MO_64) {
1d1cc4d0 4043 tcg_gen_ext32u_tl(cpu_T0, cpu_T0);
4a554890 4044 }
1d1cc4d0 4045 tcg_gen_shr_tl(cpu_T0, cpu_T0, cpu_T1);
4a554890 4046 }
1d1cc4d0 4047 gen_op_mov_reg_v(ot, reg, cpu_T0);
4a554890
RH
4048 break;
4049
bc4b43dc
RH
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;
4058 }
ab4e4aec 4059 ot = mo_64_32(s->dflag);
bc4b43dc
RH
4060 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4061
4062 switch (reg & 7) {
4063 case 1: /* blsr By,Ey */
1d1cc4d0
RH
4064 tcg_gen_neg_tl(cpu_T1, cpu_T0);
4065 tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_T1);
4066 gen_op_mov_reg_v(ot, s->vex_v, cpu_T0);
bc4b43dc
RH
4067 gen_op_update2_cc();
4068 set_cc_op(s, CC_OP_BMILGB + ot);
4069 break;
4070
4071 case 2: /* blsmsk By,Ey */
1d1cc4d0
RH
4072 tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
4073 tcg_gen_subi_tl(cpu_T0, cpu_T0, 1);
4074 tcg_gen_xor_tl(cpu_T0, cpu_T0, cpu_cc_src);
4075 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
bc4b43dc
RH
4076 set_cc_op(s, CC_OP_BMILGB + ot);
4077 break;
4078
4079 case 3: /* blsi By, Ey */
1d1cc4d0
RH
4080 tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
4081 tcg_gen_subi_tl(cpu_T0, cpu_T0, 1);
4082 tcg_gen_and_tl(cpu_T0, cpu_T0, cpu_cc_src);
4083 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
bc4b43dc
RH
4084 set_cc_op(s, CC_OP_BMILGB + ot);
4085 break;
4086
4087 default:
b9f9c5b4 4088 goto unknown_op;
bc4b43dc
RH
4089 }
4090 break;
4091
111994ee 4092 default:
b9f9c5b4 4093 goto unknown_op;
111994ee 4094 }
222a3336 4095 break;
111994ee 4096
222a3336
AZ
4097 case 0x03a:
4098 case 0x13a:
4242b1bd 4099 b = modrm;
e3af7c78 4100 modrm = x86_ldub_code(env, s);
4242b1bd
AZ
4101 rm = modrm & 7;
4102 reg = ((modrm >> 3) & 7) | rex_r;
4103 mod = (modrm >> 6) & 3;
c045af25 4104 if (b1 >= 2) {
b9f9c5b4 4105 goto unknown_op;
c045af25 4106 }
4242b1bd 4107
d3eb5eae
BS
4108 sse_fn_eppi = sse_op_table7[b].op[b1];
4109 if (!sse_fn_eppi) {
b9f9c5b4 4110 goto unknown_op;
c4baa050 4111 }
222a3336
AZ
4112 if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
4113 goto illegal_op;
4114
c6a82429
JM
4115 s->rip_offset = 1;
4116
d3eb5eae 4117 if (sse_fn_eppi == SSE_SPECIAL) {
ab4e4aec 4118 ot = mo_64_32(s->dflag);
222a3336
AZ
4119 rm = (modrm & 7) | REX_B(s);
4120 if (mod != 3)
4eeb3939 4121 gen_lea_modrm(env, s, modrm);
222a3336 4122 reg = ((modrm >> 3) & 7) | rex_r;
e3af7c78 4123 val = x86_ldub_code(env, s);
222a3336
AZ
4124 switch (b) {
4125 case 0x14: /* pextrb */
1d1cc4d0 4126 tcg_gen_ld8u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
19cbd87c 4127 xmm_regs[reg].ZMM_B(val & 15)));
3523e4bd 4128 if (mod == 3) {
1d1cc4d0 4129 gen_op_mov_reg_v(ot, rm, cpu_T0);
3523e4bd 4130 } else {
1d1cc4d0 4131 tcg_gen_qemu_st_tl(cpu_T0, cpu_A0,
3523e4bd
RH
4132 s->mem_index, MO_UB);
4133 }
222a3336
AZ
4134 break;
4135 case 0x15: /* pextrw */
1d1cc4d0 4136 tcg_gen_ld16u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
19cbd87c 4137 xmm_regs[reg].ZMM_W(val & 7)));
3523e4bd 4138 if (mod == 3) {
1d1cc4d0 4139 gen_op_mov_reg_v(ot, rm, cpu_T0);
3523e4bd 4140 } else {
1d1cc4d0 4141 tcg_gen_qemu_st_tl(cpu_T0, cpu_A0,
3523e4bd
RH
4142 s->mem_index, MO_LEUW);
4143 }
222a3336
AZ
4144 break;
4145 case 0x16:
4ba9938c 4146 if (ot == MO_32) { /* pextrd */
222a3336
AZ
4147 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
4148 offsetof(CPUX86State,
19cbd87c 4149 xmm_regs[reg].ZMM_L(val & 3)));
3523e4bd 4150 if (mod == 3) {
a7fbcbe5 4151 tcg_gen_extu_i32_tl(cpu_regs[rm], cpu_tmp2_i32);
3523e4bd 4152 } else {
d5601ad0
RH
4153 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
4154 s->mem_index, MO_LEUL);
3523e4bd 4155 }
222a3336 4156 } else { /* pextrq */
a7812ae4 4157#ifdef TARGET_X86_64
222a3336
AZ
4158 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
4159 offsetof(CPUX86State,
19cbd87c 4160 xmm_regs[reg].ZMM_Q(val & 1)));
3523e4bd 4161 if (mod == 3) {
a7fbcbe5 4162 tcg_gen_mov_i64(cpu_regs[rm], cpu_tmp1_i64);
3523e4bd
RH
4163 } else {
4164 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
4165 s->mem_index, MO_LEQ);
4166 }
a7812ae4
PB
4167#else
4168 goto illegal_op;
4169#endif
222a3336
AZ
4170 }
4171 break;
4172 case 0x17: /* extractps */
1d1cc4d0 4173 tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
19cbd87c 4174 xmm_regs[reg].ZMM_L(val & 3)));
3523e4bd 4175 if (mod == 3) {
1d1cc4d0 4176 gen_op_mov_reg_v(ot, rm, cpu_T0);
3523e4bd 4177 } else {
1d1cc4d0 4178 tcg_gen_qemu_st_tl(cpu_T0, cpu_A0,
3523e4bd
RH
4179 s->mem_index, MO_LEUL);
4180 }
222a3336
AZ
4181 break;
4182 case 0x20: /* pinsrb */
3c5f4116 4183 if (mod == 3) {
1d1cc4d0 4184 gen_op_mov_v_reg(MO_32, cpu_T0, rm);
3c5f4116 4185 } else {
1d1cc4d0 4186 tcg_gen_qemu_ld_tl(cpu_T0, cpu_A0,
3c5f4116
RH
4187 s->mem_index, MO_UB);
4188 }
1d1cc4d0 4189 tcg_gen_st8_tl(cpu_T0, cpu_env, offsetof(CPUX86State,
19cbd87c 4190 xmm_regs[reg].ZMM_B(val & 15)));
222a3336
AZ
4191 break;
4192 case 0x21: /* insertps */
a7812ae4 4193 if (mod == 3) {
222a3336
AZ
4194 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
4195 offsetof(CPUX86State,xmm_regs[rm]
19cbd87c 4196 .ZMM_L((val >> 6) & 3)));
a7812ae4 4197 } else {
3c5f4116
RH
4198 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
4199 s->mem_index, MO_LEUL);
a7812ae4 4200 }
222a3336
AZ
4201 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
4202 offsetof(CPUX86State,xmm_regs[reg]
19cbd87c 4203 .ZMM_L((val >> 4) & 3)));
222a3336
AZ
4204 if ((val >> 0) & 1)
4205 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4206 cpu_env, offsetof(CPUX86State,
19cbd87c 4207 xmm_regs[reg].ZMM_L(0)));
222a3336
AZ
4208 if ((val >> 1) & 1)
4209 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4210 cpu_env, offsetof(CPUX86State,
19cbd87c 4211 xmm_regs[reg].ZMM_L(1)));
222a3336
AZ
4212 if ((val >> 2) & 1)
4213 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4214 cpu_env, offsetof(CPUX86State,
19cbd87c 4215 xmm_regs[reg].ZMM_L(2)));
222a3336
AZ
4216 if ((val >> 3) & 1)
4217 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4218 cpu_env, offsetof(CPUX86State,
19cbd87c 4219 xmm_regs[reg].ZMM_L(3)));
222a3336
AZ
4220 break;
4221 case 0x22:
4ba9938c 4222 if (ot == MO_32) { /* pinsrd */
3c5f4116 4223 if (mod == 3) {
80b02013 4224 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[rm]);
3c5f4116 4225 } else {
80b02013
RH
4226 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
4227 s->mem_index, MO_LEUL);
3c5f4116 4228 }
222a3336
AZ
4229 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
4230 offsetof(CPUX86State,
19cbd87c 4231 xmm_regs[reg].ZMM_L(val & 3)));
222a3336 4232 } else { /* pinsrq */
a7812ae4 4233#ifdef TARGET_X86_64
3c5f4116 4234 if (mod == 3) {
222a3336 4235 gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
3c5f4116
RH
4236 } else {
4237 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
4238 s->mem_index, MO_LEQ);
4239 }
222a3336
AZ
4240 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
4241 offsetof(CPUX86State,
19cbd87c 4242 xmm_regs[reg].ZMM_Q(val & 1)));
a7812ae4
PB
4243#else
4244 goto illegal_op;
4245#endif
222a3336
AZ
4246 }
4247 break;
4248 }
4249 return;
4250 }
4242b1bd
AZ
4251
4252 if (b1) {
4253 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
4254 if (mod == 3) {
4255 op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
4256 } else {
4257 op2_offset = offsetof(CPUX86State,xmm_t0);
4eeb3939 4258 gen_lea_modrm(env, s, modrm);
323d1876 4259 gen_ldo_env_A0(s, op2_offset);
4242b1bd
AZ
4260 }
4261 } else {
4262 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
4263 if (mod == 3) {
4264 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
4265 } else {
4266 op2_offset = offsetof(CPUX86State,mmx_t0);
4eeb3939 4267 gen_lea_modrm(env, s, modrm);
323d1876 4268 gen_ldq_env_A0(s, op2_offset);
4242b1bd
AZ
4269 }
4270 }
e3af7c78 4271 val = x86_ldub_code(env, s);
4242b1bd 4272
222a3336 4273 if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
3ca51d07 4274 set_cc_op(s, CC_OP_EFLAGS);
222a3336 4275
ab4e4aec 4276 if (s->dflag == MO_64) {
222a3336
AZ
4277 /* The helper must use entire 64-bit gp registers */
4278 val |= 1 << 8;
ab4e4aec 4279 }
222a3336
AZ
4280 }
4281
4242b1bd
AZ
4282 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4283 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4284 sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
4242b1bd 4285 break;
e2c3c2c5
RH
4286
4287 case 0x33a:
4288 /* Various integer extensions at 0f 3a f[0-f]. */
4289 b = modrm | (b1 << 8);
e3af7c78 4290 modrm = x86_ldub_code(env, s);
e2c3c2c5
RH
4291 reg = ((modrm >> 3) & 7) | rex_r;
4292
4293 switch (b) {
4294 case 0x3f0: /* rorx Gy,Ey, Ib */
4295 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
4296 || !(s->prefix & PREFIX_VEX)
4297 || s->vex_l != 0) {
4298 goto illegal_op;
4299 }
ab4e4aec 4300 ot = mo_64_32(s->dflag);
e2c3c2c5 4301 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
e3af7c78 4302 b = x86_ldub_code(env, s);
4ba9938c 4303 if (ot == MO_64) {
1d1cc4d0 4304 tcg_gen_rotri_tl(cpu_T0, cpu_T0, b & 63);
e2c3c2c5 4305 } else {
1d1cc4d0 4306 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
e2c3c2c5 4307 tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, b & 31);
1d1cc4d0 4308 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
e2c3c2c5 4309 }
1d1cc4d0 4310 gen_op_mov_reg_v(ot, reg, cpu_T0);
e2c3c2c5
RH
4311 break;
4312
4313 default:
b9f9c5b4 4314 goto unknown_op;
e2c3c2c5
RH
4315 }
4316 break;
4317
664e0f19 4318 default:
b9f9c5b4
RH
4319 unknown_op:
4320 gen_unknown_opcode(env, s);
4321 return;
664e0f19
FB
4322 }
4323 } else {
4324 /* generic MMX or SSE operation */
d1e42c5c 4325 switch(b) {
d1e42c5c
FB
4326 case 0x70: /* pshufx insn */
4327 case 0xc6: /* pshufx insn */
4328 case 0xc2: /* compare insns */
4329 s->rip_offset = 1;
4330 break;
4331 default:
4332 break;
664e0f19
FB
4333 }
4334 if (is_xmm) {
4335 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
4336 if (mod != 3) {
cb48da7f
RH
4337 int sz = 4;
4338
4eeb3939 4339 gen_lea_modrm(env, s, modrm);
664e0f19 4340 op2_offset = offsetof(CPUX86State,xmm_t0);
cb48da7f
RH
4341
4342 switch (b) {
4343 case 0x50 ... 0x5a:
4344 case 0x5c ... 0x5f:
4345 case 0xc2:
4346 /* Most sse scalar operations. */
664e0f19 4347 if (b1 == 2) {
cb48da7f
RH
4348 sz = 2;
4349 } else if (b1 == 3) {
4350 sz = 3;
4351 }
4352 break;
4353
4354 case 0x2e: /* ucomis[sd] */
4355 case 0x2f: /* comis[sd] */
4356 if (b1 == 0) {
4357 sz = 2;
664e0f19 4358 } else {
cb48da7f 4359 sz = 3;
664e0f19 4360 }
cb48da7f
RH
4361 break;
4362 }
4363
4364 switch (sz) {
4365 case 2:
4366 /* 32 bit access */
1d1cc4d0
RH
4367 gen_op_ld_v(s, MO_32, cpu_T0, cpu_A0);
4368 tcg_gen_st32_tl(cpu_T0, cpu_env,
19cbd87c 4369 offsetof(CPUX86State,xmm_t0.ZMM_L(0)));
cb48da7f
RH
4370 break;
4371 case 3:
4372 /* 64 bit access */
19cbd87c 4373 gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.ZMM_D(0)));
cb48da7f
RH
4374 break;
4375 default:
4376 /* 128 bit access */
323d1876 4377 gen_ldo_env_A0(s, op2_offset);
cb48da7f 4378 break;
664e0f19
FB
4379 }
4380 } else {
4381 rm = (modrm & 7) | REX_B(s);
4382 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
4383 }
4384 } else {
4385 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
4386 if (mod != 3) {
4eeb3939 4387 gen_lea_modrm(env, s, modrm);
664e0f19 4388 op2_offset = offsetof(CPUX86State,mmx_t0);
323d1876 4389 gen_ldq_env_A0(s, op2_offset);
664e0f19
FB
4390 } else {
4391 rm = (modrm & 7);
4392 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
4393 }
4394 }
4395 switch(b) {
a35f3ec7 4396 case 0x0f: /* 3DNow! data insns */
e3af7c78 4397 val = x86_ldub_code(env, s);
d3eb5eae
BS
4398 sse_fn_epp = sse_op_table5[val];
4399 if (!sse_fn_epp) {
b9f9c5b4
RH
4400 goto unknown_op;
4401 }
4402 if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW)) {
a35f3ec7 4403 goto illegal_op;
c4baa050 4404 }
5af45186
FB
4405 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4406 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4407 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
a35f3ec7 4408 break;
664e0f19
FB
4409 case 0x70: /* pshufx insn */
4410 case 0xc6: /* pshufx insn */
e3af7c78 4411 val = x86_ldub_code(env, s);
5af45186
FB
4412 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4413 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
c4baa050 4414 /* XXX: introduce a new table? */
d3eb5eae 4415 sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
c4baa050 4416 sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
664e0f19
FB
4417 break;
4418 case 0xc2:
4419 /* compare insns */
e3af7c78 4420 val = x86_ldub_code(env, s);
664e0f19 4421 if (val >= 8)
b9f9c5b4 4422 goto unknown_op;
d3eb5eae 4423 sse_fn_epp = sse_op_table4[val][b1];
c4baa050 4424
5af45186
FB
4425 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4426 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4427 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19 4428 break;
b8b6a50b
FB
4429 case 0xf7:
4430 /* maskmov : we must prepare A0 */
4431 if (mod != 3)
4432 goto illegal_op;
1d71ddb1
RH
4433 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EDI]);
4434 gen_extu(s->aflag, cpu_A0);
b8b6a50b
FB
4435 gen_add_A0_ds_seg(s);
4436
4437 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4438 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
c4baa050 4439 /* XXX: introduce a new table? */
d3eb5eae
BS
4440 sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
4441 sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
b8b6a50b 4442 break;
664e0f19 4443 default:
5af45186
FB
4444 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4445 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4446 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
4447 break;
4448 }
4449 if (b == 0x2e || b == 0x2f) {
3ca51d07 4450 set_cc_op(s, CC_OP_EFLAGS);
664e0f19
FB
4451 }
4452 }
4453}
4454
6cf147aa 4455/* convert one instruction. s->base.is_jmp is set if the translation must
2c0262af 4456 be stopped. Return the next pc value */
2c2f8cac 4457static target_ulong disas_insn(DisasContext *s, CPUState *cpu)
2c0262af 4458{
2c2f8cac 4459 CPUX86State *env = cpu->env_ptr;
ab4e4aec 4460 int b, prefixes;
d67dc9e6 4461 int shift;
ab4e4aec 4462 TCGMemOp ot, aflag, dflag;
4eeb3939 4463 int modrm, reg, rm, mod, op, opreg, val;
14ce26e7
FB
4464 target_ulong next_eip, tval;
4465 int rex_w, rex_r;
2c2f8cac 4466 target_ulong pc_start = s->base.pc_next;
2c0262af 4467
b9f9c5b4 4468 s->pc_start = s->pc = pc_start;
2c0262af 4469 s->override = -1;
14ce26e7
FB
4470#ifdef TARGET_X86_64
4471 s->rex_x = 0;
4472 s->rex_b = 0;
5fafdf24 4473 x86_64_hregs = 0;
14ce26e7
FB
4474#endif
4475 s->rip_offset = 0; /* for relative ip address */
701ed211
RH
4476 s->vex_l = 0;
4477 s->vex_v = 0;
b066c537
PB
4478 if (sigsetjmp(s->jmpbuf, 0) != 0) {
4479 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
4480 return s->pc;
30663fd2 4481 }
b066c537 4482
a4926d99
SW
4483 prefixes = 0;
4484 rex_w = -1;
4485 rex_r = 0;
4486
b066c537 4487 next_byte:
e3af7c78 4488 b = x86_ldub_code(env, s);
4a6fd938
RH
4489 /* Collect prefixes. */
4490 switch (b) {
4491 case 0xf3:
4492 prefixes |= PREFIX_REPZ;
4493 goto next_byte;
4494 case 0xf2:
4495 prefixes |= PREFIX_REPNZ;
4496 goto next_byte;
4497 case 0xf0:
4498 prefixes |= PREFIX_LOCK;
4499 goto next_byte;
4500 case 0x2e:
4501 s->override = R_CS;
4502 goto next_byte;
4503 case 0x36:
4504 s->override = R_SS;
4505 goto next_byte;
4506 case 0x3e:
4507 s->override = R_DS;
4508 goto next_byte;
4509 case 0x26:
4510 s->override = R_ES;
4511 goto next_byte;
4512 case 0x64:
4513 s->override = R_FS;
4514 goto next_byte;
4515 case 0x65:
4516 s->override = R_GS;
4517 goto next_byte;
4518 case 0x66:
4519 prefixes |= PREFIX_DATA;
4520 goto next_byte;
4521 case 0x67:
4522 prefixes |= PREFIX_ADR;
4523 goto next_byte;
14ce26e7 4524#ifdef TARGET_X86_64
4a6fd938
RH
4525 case 0x40 ... 0x4f:
4526 if (CODE64(s)) {
14ce26e7
FB
4527 /* REX prefix */
4528 rex_w = (b >> 3) & 1;
4529 rex_r = (b & 0x4) << 1;
4530 s->rex_x = (b & 0x2) << 2;
4531 REX_B(s) = (b & 0x1) << 3;
4532 x86_64_hregs = 1; /* select uniform byte register addressing */
4533 goto next_byte;
4534 }
4a6fd938
RH
4535 break;
4536#endif
701ed211
RH
4537 case 0xc5: /* 2-byte VEX */
4538 case 0xc4: /* 3-byte VEX */
4539 /* VEX prefixes cannot be used except in 32-bit mode.
4540 Otherwise the instruction is LES or LDS. */
4541 if (s->code32 && !s->vm86) {
4542 static const int pp_prefix[4] = {
4543 0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ
4544 };
e3af7c78 4545 int vex3, vex2 = x86_ldub_code(env, s);
701ed211
RH
4546
4547 if (!CODE64(s) && (vex2 & 0xc0) != 0xc0) {
4548 /* 4.1.4.6: In 32-bit mode, bits [7:6] must be 11b,
4549 otherwise the instruction is LES or LDS. */
cfcca361 4550 s->pc--; /* rewind the advance_pc() x86_ldub_code() did */
701ed211
RH
4551 break;
4552 }
701ed211 4553
085d8134 4554 /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
701ed211
RH
4555 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ
4556 | PREFIX_LOCK | PREFIX_DATA)) {
4557 goto illegal_op;
4558 }
4559#ifdef TARGET_X86_64
4560 if (x86_64_hregs) {
4561 goto illegal_op;
4562 }
4563#endif
4564 rex_r = (~vex2 >> 4) & 8;
4565 if (b == 0xc5) {
4566 vex3 = vex2;
e3af7c78 4567 b = x86_ldub_code(env, s);
701ed211
RH
4568 } else {
4569#ifdef TARGET_X86_64
4570 s->rex_x = (~vex2 >> 3) & 8;
4571 s->rex_b = (~vex2 >> 2) & 8;
4572#endif
e3af7c78 4573 vex3 = x86_ldub_code(env, s);
701ed211
RH
4574 rex_w = (vex3 >> 7) & 1;
4575 switch (vex2 & 0x1f) {
4576 case 0x01: /* Implied 0f leading opcode bytes. */
e3af7c78 4577 b = x86_ldub_code(env, s) | 0x100;
701ed211
RH
4578 break;
4579 case 0x02: /* Implied 0f 38 leading opcode bytes. */
4580 b = 0x138;
4581 break;
4582 case 0x03: /* Implied 0f 3a leading opcode bytes. */
4583 b = 0x13a;
4584 break;
4585 default: /* Reserved for future use. */
b9f9c5b4 4586 goto unknown_op;
701ed211
RH
4587 }
4588 }
4589 s->vex_v = (~vex3 >> 3) & 0xf;
4590 s->vex_l = (vex3 >> 2) & 1;
4591 prefixes |= pp_prefix[vex3 & 3] | PREFIX_VEX;
4592 }
4593 break;
4a6fd938
RH
4594 }
4595
4596 /* Post-process prefixes. */
4a6fd938 4597 if (CODE64(s)) {
dec3fc96
RH
4598 /* In 64-bit mode, the default data size is 32-bit. Select 64-bit
4599 data with rex_w, and 16-bit data with 0x66; rex_w takes precedence
4600 over 0x66 if both are present. */
ab4e4aec 4601 dflag = (rex_w > 0 ? MO_64 : prefixes & PREFIX_DATA ? MO_16 : MO_32);
dec3fc96 4602 /* In 64-bit mode, 0x67 selects 32-bit addressing. */
1d71ddb1 4603 aflag = (prefixes & PREFIX_ADR ? MO_32 : MO_64);
dec3fc96
RH
4604 } else {
4605 /* In 16/32-bit mode, 0x66 selects the opposite data size. */
ab4e4aec
RH
4606 if (s->code32 ^ ((prefixes & PREFIX_DATA) != 0)) {
4607 dflag = MO_32;
4608 } else {
4609 dflag = MO_16;
14ce26e7 4610 }
dec3fc96 4611 /* In 16/32-bit mode, 0x67 selects the opposite addressing. */
1d71ddb1
RH
4612 if (s->code32 ^ ((prefixes & PREFIX_ADR) != 0)) {
4613 aflag = MO_32;
4614 } else {
4615 aflag = MO_16;
14ce26e7 4616 }
2c0262af
FB
4617 }
4618
2c0262af
FB
4619 s->prefix = prefixes;
4620 s->aflag = aflag;
4621 s->dflag = dflag;
4622
2c0262af
FB
4623 /* now check op code */
4624 reswitch:
4625 switch(b) {
4626 case 0x0f:
4627 /**************************/
4628 /* extended op code */
e3af7c78 4629 b = x86_ldub_code(env, s) | 0x100;
2c0262af 4630 goto reswitch;
3b46e624 4631
2c0262af
FB
4632 /**************************/
4633 /* arith & logic */
4634 case 0x00 ... 0x05:
4635 case 0x08 ... 0x0d:
4636 case 0x10 ... 0x15:
4637 case 0x18 ... 0x1d:
4638 case 0x20 ... 0x25:
4639 case 0x28 ... 0x2d:
4640 case 0x30 ... 0x35:
4641 case 0x38 ... 0x3d:
4642 {
4643 int op, f, val;
4644 op = (b >> 3) & 7;
4645 f = (b >> 1) & 3;
4646
ab4e4aec 4647 ot = mo_b_d(b, dflag);
3b46e624 4648
2c0262af
FB
4649 switch(f) {
4650 case 0: /* OP Ev, Gv */
e3af7c78 4651 modrm = x86_ldub_code(env, s);
14ce26e7 4652 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 4653 mod = (modrm >> 6) & 3;
14ce26e7 4654 rm = (modrm & 7) | REX_B(s);
2c0262af 4655 if (mod != 3) {
4eeb3939 4656 gen_lea_modrm(env, s, modrm);
2c0262af
FB
4657 opreg = OR_TMP0;
4658 } else if (op == OP_XORL && rm == reg) {
4659 xor_zero:
4660 /* xor reg, reg optimisation */
436ff2d2 4661 set_cc_op(s, CC_OP_CLR);
1d1cc4d0
RH
4662 tcg_gen_movi_tl(cpu_T0, 0);
4663 gen_op_mov_reg_v(ot, reg, cpu_T0);
2c0262af
FB
4664 break;
4665 } else {
4666 opreg = rm;
4667 }
1d1cc4d0 4668 gen_op_mov_v_reg(ot, cpu_T1, reg);
2c0262af
FB
4669 gen_op(s, op, ot, opreg);
4670 break;
4671 case 1: /* OP Gv, Ev */
e3af7c78 4672 modrm = x86_ldub_code(env, s);
2c0262af 4673 mod = (modrm >> 6) & 3;
14ce26e7
FB
4674 reg = ((modrm >> 3) & 7) | rex_r;
4675 rm = (modrm & 7) | REX_B(s);
2c0262af 4676 if (mod != 3) {
4eeb3939 4677 gen_lea_modrm(env, s, modrm);
1d1cc4d0 4678 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
2c0262af
FB
4679 } else if (op == OP_XORL && rm == reg) {
4680 goto xor_zero;
4681 } else {
1d1cc4d0 4682 gen_op_mov_v_reg(ot, cpu_T1, rm);
2c0262af
FB
4683 }
4684 gen_op(s, op, ot, reg);
4685 break;
4686 case 2: /* OP A, Iv */
0af10c86 4687 val = insn_get(env, s, ot);
1d1cc4d0 4688 tcg_gen_movi_tl(cpu_T1, val);
2c0262af
FB
4689 gen_op(s, op, ot, OR_EAX);
4690 break;
4691 }
4692 }
4693 break;
4694
ec9d6075
FB
4695 case 0x82:
4696 if (CODE64(s))
4697 goto illegal_op;
2c0262af
FB
4698 case 0x80: /* GRP1 */
4699 case 0x81:
4700 case 0x83:
4701 {
4702 int val;
4703
ab4e4aec 4704 ot = mo_b_d(b, dflag);
3b46e624 4705
e3af7c78 4706 modrm = x86_ldub_code(env, s);
2c0262af 4707 mod = (modrm >> 6) & 3;
14ce26e7 4708 rm = (modrm & 7) | REX_B(s);
2c0262af 4709 op = (modrm >> 3) & 7;
3b46e624 4710
2c0262af 4711 if (mod != 3) {
14ce26e7
FB
4712 if (b == 0x83)
4713 s->rip_offset = 1;
4714 else
4715 s->rip_offset = insn_const_size(ot);
4eeb3939 4716 gen_lea_modrm(env, s, modrm);
2c0262af
FB
4717 opreg = OR_TMP0;
4718 } else {
14ce26e7 4719 opreg = rm;
2c0262af
FB
4720 }
4721
4722 switch(b) {
4723 default:
4724 case 0x80:
4725 case 0x81:
d64477af 4726 case 0x82:
0af10c86 4727 val = insn_get(env, s, ot);
2c0262af
FB
4728 break;
4729 case 0x83:
4ba9938c 4730 val = (int8_t)insn_get(env, s, MO_8);
2c0262af
FB
4731 break;
4732 }
1d1cc4d0 4733 tcg_gen_movi_tl(cpu_T1, val);
2c0262af
FB
4734 gen_op(s, op, ot, opreg);
4735 }
4736 break;
4737
4738 /**************************/
4739 /* inc, dec, and other misc arith */
4740 case 0x40 ... 0x47: /* inc Gv */
ab4e4aec 4741 ot = dflag;
2c0262af
FB
4742 gen_inc(s, ot, OR_EAX + (b & 7), 1);
4743 break;
4744 case 0x48 ... 0x4f: /* dec Gv */
ab4e4aec 4745 ot = dflag;
2c0262af
FB
4746 gen_inc(s, ot, OR_EAX + (b & 7), -1);
4747 break;
4748 case 0xf6: /* GRP3 */
4749 case 0xf7:
ab4e4aec 4750 ot = mo_b_d(b, dflag);
2c0262af 4751
e3af7c78 4752 modrm = x86_ldub_code(env, s);
2c0262af 4753 mod = (modrm >> 6) & 3;
14ce26e7 4754 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
4755 op = (modrm >> 3) & 7;
4756 if (mod != 3) {
2a5fe8ae 4757 if (op == 0) {
14ce26e7 4758 s->rip_offset = insn_const_size(ot);
2a5fe8ae 4759 }
4eeb3939 4760 gen_lea_modrm(env, s, modrm);
2a5fe8ae
EC
4761 /* For those below that handle locked memory, don't load here. */
4762 if (!(s->prefix & PREFIX_LOCK)
4763 || op != 2) {
4764 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
4765 }
2c0262af 4766 } else {
1d1cc4d0 4767 gen_op_mov_v_reg(ot, cpu_T0, rm);
2c0262af
FB
4768 }
4769
4770 switch(op) {
4771 case 0: /* test */
0af10c86 4772 val = insn_get(env, s, ot);
1d1cc4d0 4773 tcg_gen_movi_tl(cpu_T1, val);
2c0262af 4774 gen_op_testl_T0_T1_cc();
3ca51d07 4775 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af
FB
4776 break;
4777 case 2: /* not */
2a5fe8ae
EC
4778 if (s->prefix & PREFIX_LOCK) {
4779 if (mod == 3) {
4780 goto illegal_op;
4781 }
4782 tcg_gen_movi_tl(cpu_T0, ~0);
4783 tcg_gen_atomic_xor_fetch_tl(cpu_T0, cpu_A0, cpu_T0,
4784 s->mem_index, ot | MO_LE);
2c0262af 4785 } else {
2a5fe8ae
EC
4786 tcg_gen_not_tl(cpu_T0, cpu_T0);
4787 if (mod != 3) {
4788 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
4789 } else {
4790 gen_op_mov_reg_v(ot, rm, cpu_T0);
4791 }
2c0262af
FB
4792 }
4793 break;
4794 case 3: /* neg */
8eb8c738
EC
4795 if (s->prefix & PREFIX_LOCK) {
4796 TCGLabel *label1;
4797 TCGv a0, t0, t1, t2;
4798
4799 if (mod == 3) {
4800 goto illegal_op;
4801 }
4802 a0 = tcg_temp_local_new();
4803 t0 = tcg_temp_local_new();
4804 label1 = gen_new_label();
4805
4806 tcg_gen_mov_tl(a0, cpu_A0);
4807 tcg_gen_mov_tl(t0, cpu_T0);
4808
4809 gen_set_label(label1);
4810 t1 = tcg_temp_new();
4811 t2 = tcg_temp_new();
4812 tcg_gen_mov_tl(t2, t0);
4813 tcg_gen_neg_tl(t1, t0);
4814 tcg_gen_atomic_cmpxchg_tl(t0, a0, t0, t1,
4815 s->mem_index, ot | MO_LE);
4816 tcg_temp_free(t1);
4817 tcg_gen_brcond_tl(TCG_COND_NE, t0, t2, label1);
4818
4819 tcg_temp_free(t2);
4820 tcg_temp_free(a0);
4821 tcg_gen_mov_tl(cpu_T0, t0);
4822 tcg_temp_free(t0);
2c0262af 4823 } else {
8eb8c738
EC
4824 tcg_gen_neg_tl(cpu_T0, cpu_T0);
4825 if (mod != 3) {
4826 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
4827 } else {
4828 gen_op_mov_reg_v(ot, rm, cpu_T0);
4829 }
2c0262af
FB
4830 }
4831 gen_op_update_neg_cc();
3ca51d07 4832 set_cc_op(s, CC_OP_SUBB + ot);
2c0262af
FB
4833 break;
4834 case 4: /* mul */
4835 switch(ot) {
4ba9938c 4836 case MO_8:
1d1cc4d0
RH
4837 gen_op_mov_v_reg(MO_8, cpu_T1, R_EAX);
4838 tcg_gen_ext8u_tl(cpu_T0, cpu_T0);
4839 tcg_gen_ext8u_tl(cpu_T1, cpu_T1);
0211e5af 4840 /* XXX: use 32 bit mul which could be faster */
1d1cc4d0
RH
4841 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
4842 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
4843 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4844 tcg_gen_andi_tl(cpu_cc_src, cpu_T0, 0xff00);
3ca51d07 4845 set_cc_op(s, CC_OP_MULB);
2c0262af 4846 break;
4ba9938c 4847 case MO_16:
1d1cc4d0
RH
4848 gen_op_mov_v_reg(MO_16, cpu_T1, R_EAX);
4849 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
4850 tcg_gen_ext16u_tl(cpu_T1, cpu_T1);
0211e5af 4851 /* XXX: use 32 bit mul which could be faster */
1d1cc4d0
RH
4852 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
4853 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
4854 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4855 tcg_gen_shri_tl(cpu_T0, cpu_T0, 16);
4856 gen_op_mov_reg_v(MO_16, R_EDX, cpu_T0);
4857 tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
3ca51d07 4858 set_cc_op(s, CC_OP_MULW);
2c0262af
FB
4859 break;
4860 default:
4ba9938c 4861 case MO_32:
1d1cc4d0 4862 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
a4bcea3d
RH
4863 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
4864 tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
4865 cpu_tmp2_i32, cpu_tmp3_i32);
4866 tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
4867 tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
4868 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4869 tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
3ca51d07 4870 set_cc_op(s, CC_OP_MULL);
2c0262af 4871 break;
14ce26e7 4872#ifdef TARGET_X86_64
4ba9938c 4873 case MO_64:
a4bcea3d 4874 tcg_gen_mulu2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
1d1cc4d0 4875 cpu_T0, cpu_regs[R_EAX]);
a4bcea3d
RH
4876 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4877 tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
3ca51d07 4878 set_cc_op(s, CC_OP_MULQ);
14ce26e7
FB
4879 break;
4880#endif
2c0262af 4881 }
2c0262af
FB
4882 break;
4883 case 5: /* imul */
4884 switch(ot) {
4ba9938c 4885 case MO_8:
1d1cc4d0
RH
4886 gen_op_mov_v_reg(MO_8, cpu_T1, R_EAX);
4887 tcg_gen_ext8s_tl(cpu_T0, cpu_T0);
4888 tcg_gen_ext8s_tl(cpu_T1, cpu_T1);
0211e5af 4889 /* XXX: use 32 bit mul which could be faster */
1d1cc4d0
RH
4890 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
4891 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
4892 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4893 tcg_gen_ext8s_tl(cpu_tmp0, cpu_T0);
4894 tcg_gen_sub_tl(cpu_cc_src, cpu_T0, cpu_tmp0);
3ca51d07 4895 set_cc_op(s, CC_OP_MULB);
2c0262af 4896 break;
4ba9938c 4897 case MO_16:
1d1cc4d0
RH
4898 gen_op_mov_v_reg(MO_16, cpu_T1, R_EAX);
4899 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
4900 tcg_gen_ext16s_tl(cpu_T1, cpu_T1);
0211e5af 4901 /* XXX: use 32 bit mul which could be faster */
1d1cc4d0
RH
4902 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
4903 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
4904 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
4905 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T0);
4906 tcg_gen_sub_tl(cpu_cc_src, cpu_T0, cpu_tmp0);
4907 tcg_gen_shri_tl(cpu_T0, cpu_T0, 16);
4908 gen_op_mov_reg_v(MO_16, R_EDX, cpu_T0);
3ca51d07 4909 set_cc_op(s, CC_OP_MULW);
2c0262af
FB
4910 break;
4911 default:
4ba9938c 4912 case MO_32:
1d1cc4d0 4913 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
a4bcea3d
RH
4914 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
4915 tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
4916 cpu_tmp2_i32, cpu_tmp3_i32);
4917 tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
4918 tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
4919 tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
4920 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4921 tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
4922 tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
3ca51d07 4923 set_cc_op(s, CC_OP_MULL);
2c0262af 4924 break;
14ce26e7 4925#ifdef TARGET_X86_64
4ba9938c 4926 case MO_64:
a4bcea3d 4927 tcg_gen_muls2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
1d1cc4d0 4928 cpu_T0, cpu_regs[R_EAX]);
a4bcea3d
RH
4929 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4930 tcg_gen_sari_tl(cpu_cc_src, cpu_regs[R_EAX], 63);
4931 tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_regs[R_EDX]);
3ca51d07 4932 set_cc_op(s, CC_OP_MULQ);
14ce26e7
FB
4933 break;
4934#endif
2c0262af 4935 }
2c0262af
FB
4936 break;
4937 case 6: /* div */
4938 switch(ot) {
4ba9938c 4939 case MO_8:
1d1cc4d0 4940 gen_helper_divb_AL(cpu_env, cpu_T0);
2c0262af 4941 break;
4ba9938c 4942 case MO_16:
1d1cc4d0 4943 gen_helper_divw_AX(cpu_env, cpu_T0);
2c0262af
FB
4944 break;
4945 default:
4ba9938c 4946 case MO_32:
1d1cc4d0 4947 gen_helper_divl_EAX(cpu_env, cpu_T0);
14ce26e7
FB
4948 break;
4949#ifdef TARGET_X86_64
4ba9938c 4950 case MO_64:
1d1cc4d0 4951 gen_helper_divq_EAX(cpu_env, cpu_T0);
2c0262af 4952 break;
14ce26e7 4953#endif
2c0262af
FB
4954 }
4955 break;
4956 case 7: /* idiv */
4957 switch(ot) {
4ba9938c 4958 case MO_8:
1d1cc4d0 4959 gen_helper_idivb_AL(cpu_env, cpu_T0);
2c0262af 4960 break;
4ba9938c 4961 case MO_16:
1d1cc4d0 4962 gen_helper_idivw_AX(cpu_env, cpu_T0);
2c0262af
FB
4963 break;
4964 default:
4ba9938c 4965 case MO_32:
1d1cc4d0 4966 gen_helper_idivl_EAX(cpu_env, cpu_T0);
14ce26e7
FB
4967 break;
4968#ifdef TARGET_X86_64
4ba9938c 4969 case MO_64:
1d1cc4d0 4970 gen_helper_idivq_EAX(cpu_env, cpu_T0);
2c0262af 4971 break;
14ce26e7 4972#endif
2c0262af
FB
4973 }
4974 break;
4975 default:
b9f9c5b4 4976 goto unknown_op;
2c0262af
FB
4977 }
4978 break;
4979
4980 case 0xfe: /* GRP4 */
4981 case 0xff: /* GRP5 */
ab4e4aec 4982 ot = mo_b_d(b, dflag);
2c0262af 4983
e3af7c78 4984 modrm = x86_ldub_code(env, s);
2c0262af 4985 mod = (modrm >> 6) & 3;
14ce26e7 4986 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
4987 op = (modrm >> 3) & 7;
4988 if (op >= 2 && b == 0xfe) {
b9f9c5b4 4989 goto unknown_op;
2c0262af 4990 }
14ce26e7 4991 if (CODE64(s)) {
aba9d61e 4992 if (op == 2 || op == 4) {
14ce26e7 4993 /* operand size for jumps is 64 bit */
4ba9938c 4994 ot = MO_64;
aba9d61e 4995 } else if (op == 3 || op == 5) {
ab4e4aec 4996 ot = dflag != MO_16 ? MO_32 + (rex_w == 1) : MO_16;
14ce26e7
FB
4997 } else if (op == 6) {
4998 /* default push size is 64 bit */
ab4e4aec 4999 ot = mo_pushpop(s, dflag);
14ce26e7
FB
5000 }
5001 }
2c0262af 5002 if (mod != 3) {
4eeb3939 5003 gen_lea_modrm(env, s, modrm);
2c0262af 5004 if (op >= 2 && op != 3 && op != 5)
1d1cc4d0 5005 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
2c0262af 5006 } else {
1d1cc4d0 5007 gen_op_mov_v_reg(ot, cpu_T0, rm);
2c0262af
FB
5008 }
5009
5010 switch(op) {
5011 case 0: /* inc Ev */
5012 if (mod != 3)
5013 opreg = OR_TMP0;
5014 else
5015 opreg = rm;
5016 gen_inc(s, ot, opreg, 1);
5017 break;
5018 case 1: /* dec Ev */
5019 if (mod != 3)
5020 opreg = OR_TMP0;
5021 else
5022 opreg = rm;
5023 gen_inc(s, ot, opreg, -1);
5024 break;
5025 case 2: /* call Ev */
4f31916f 5026 /* XXX: optimize if memory (no 'and' is necessary) */
ab4e4aec 5027 if (dflag == MO_16) {
1d1cc4d0 5028 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
40b90233 5029 }
2c0262af 5030 next_eip = s->pc - s->cs_base;
1d1cc4d0
RH
5031 tcg_gen_movi_tl(cpu_T1, next_eip);
5032 gen_push_v(s, cpu_T1);
5033 gen_op_jmp_v(cpu_T0);
7d117ce8 5034 gen_bnd_jmp(s);
b4aa2977 5035 gen_jr(s, cpu_T0);
2c0262af 5036 break;
61382a50 5037 case 3: /* lcall Ev */
1d1cc4d0 5038 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
830a19a4 5039 gen_add_A0_im(s, 1 << ot);
1d1cc4d0 5040 gen_op_ld_v(s, MO_16, cpu_T0, cpu_A0);
2c0262af
FB
5041 do_lcall:
5042 if (s->pe && !s->vm86) {
1d1cc4d0
RH
5043 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
5044 gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T1,
ab4e4aec 5045 tcg_const_i32(dflag - 1),
100ec099 5046 tcg_const_tl(s->pc - s->cs_base));
2c0262af 5047 } else {
1d1cc4d0
RH
5048 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
5049 gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T1,
ab4e4aec 5050 tcg_const_i32(dflag - 1),
a7812ae4 5051 tcg_const_i32(s->pc - s->cs_base));
2c0262af 5052 }
b4aa2977
EC
5053 tcg_gen_ld_tl(cpu_tmp4, cpu_env, offsetof(CPUX86State, eip));
5054 gen_jr(s, cpu_tmp4);
2c0262af
FB
5055 break;
5056 case 4: /* jmp Ev */
ab4e4aec 5057 if (dflag == MO_16) {
1d1cc4d0 5058 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
40b90233 5059 }
1d1cc4d0 5060 gen_op_jmp_v(cpu_T0);
7d117ce8 5061 gen_bnd_jmp(s);
b4aa2977 5062 gen_jr(s, cpu_T0);
2c0262af
FB
5063 break;
5064 case 5: /* ljmp Ev */
1d1cc4d0 5065 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
830a19a4 5066 gen_add_A0_im(s, 1 << ot);
1d1cc4d0 5067 gen_op_ld_v(s, MO_16, cpu_T0, cpu_A0);
2c0262af
FB
5068 do_ljmp:
5069 if (s->pe && !s->vm86) {
1d1cc4d0
RH
5070 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
5071 gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T1,
100ec099 5072 tcg_const_tl(s->pc - s->cs_base));
2c0262af 5073 } else {
3bd7da9e 5074 gen_op_movl_seg_T0_vm(R_CS);
1d1cc4d0 5075 gen_op_jmp_v(cpu_T1);
2c0262af 5076 }
b4aa2977
EC
5077 tcg_gen_ld_tl(cpu_tmp4, cpu_env, offsetof(CPUX86State, eip));
5078 gen_jr(s, cpu_tmp4);
2c0262af
FB
5079 break;
5080 case 6: /* push Ev */
1d1cc4d0 5081 gen_push_v(s, cpu_T0);
2c0262af
FB
5082 break;
5083 default:
b9f9c5b4 5084 goto unknown_op;
2c0262af
FB
5085 }
5086 break;
5087
5088 case 0x84: /* test Ev, Gv */
5fafdf24 5089 case 0x85:
ab4e4aec 5090 ot = mo_b_d(b, dflag);
2c0262af 5091
e3af7c78 5092 modrm = x86_ldub_code(env, s);
14ce26e7 5093 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5094
0af10c86 5095 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
1d1cc4d0 5096 gen_op_mov_v_reg(ot, cpu_T1, reg);
2c0262af 5097 gen_op_testl_T0_T1_cc();
3ca51d07 5098 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af 5099 break;
3b46e624 5100
2c0262af
FB
5101 case 0xa8: /* test eAX, Iv */
5102 case 0xa9:
ab4e4aec 5103 ot = mo_b_d(b, dflag);
0af10c86 5104 val = insn_get(env, s, ot);
2c0262af 5105
1d1cc4d0
RH
5106 gen_op_mov_v_reg(ot, cpu_T0, OR_EAX);
5107 tcg_gen_movi_tl(cpu_T1, val);
2c0262af 5108 gen_op_testl_T0_T1_cc();
3ca51d07 5109 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af 5110 break;
3b46e624 5111
2c0262af 5112 case 0x98: /* CWDE/CBW */
ab4e4aec 5113 switch (dflag) {
14ce26e7 5114#ifdef TARGET_X86_64
ab4e4aec 5115 case MO_64:
1d1cc4d0
RH
5116 gen_op_mov_v_reg(MO_32, cpu_T0, R_EAX);
5117 tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
5118 gen_op_mov_reg_v(MO_64, R_EAX, cpu_T0);
ab4e4aec 5119 break;
14ce26e7 5120#endif
ab4e4aec 5121 case MO_32:
1d1cc4d0
RH
5122 gen_op_mov_v_reg(MO_16, cpu_T0, R_EAX);
5123 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
5124 gen_op_mov_reg_v(MO_32, R_EAX, cpu_T0);
ab4e4aec
RH
5125 break;
5126 case MO_16:
1d1cc4d0
RH
5127 gen_op_mov_v_reg(MO_8, cpu_T0, R_EAX);
5128 tcg_gen_ext8s_tl(cpu_T0, cpu_T0);
5129 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
ab4e4aec
RH
5130 break;
5131 default:
5132 tcg_abort();
e108dd01 5133 }
2c0262af
FB
5134 break;
5135 case 0x99: /* CDQ/CWD */
ab4e4aec 5136 switch (dflag) {
14ce26e7 5137#ifdef TARGET_X86_64
ab4e4aec 5138 case MO_64:
1d1cc4d0
RH
5139 gen_op_mov_v_reg(MO_64, cpu_T0, R_EAX);
5140 tcg_gen_sari_tl(cpu_T0, cpu_T0, 63);
5141 gen_op_mov_reg_v(MO_64, R_EDX, cpu_T0);
ab4e4aec 5142 break;
14ce26e7 5143#endif
ab4e4aec 5144 case MO_32:
1d1cc4d0
RH
5145 gen_op_mov_v_reg(MO_32, cpu_T0, R_EAX);
5146 tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
5147 tcg_gen_sari_tl(cpu_T0, cpu_T0, 31);
5148 gen_op_mov_reg_v(MO_32, R_EDX, cpu_T0);
ab4e4aec
RH
5149 break;
5150 case MO_16:
1d1cc4d0
RH
5151 gen_op_mov_v_reg(MO_16, cpu_T0, R_EAX);
5152 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
5153 tcg_gen_sari_tl(cpu_T0, cpu_T0, 15);
5154 gen_op_mov_reg_v(MO_16, R_EDX, cpu_T0);
ab4e4aec
RH
5155 break;
5156 default:
5157 tcg_abort();
e108dd01 5158 }
2c0262af
FB
5159 break;
5160 case 0x1af: /* imul Gv, Ev */
5161 case 0x69: /* imul Gv, Ev, I */
5162 case 0x6b:
ab4e4aec 5163 ot = dflag;
e3af7c78 5164 modrm = x86_ldub_code(env, s);
14ce26e7
FB
5165 reg = ((modrm >> 3) & 7) | rex_r;
5166 if (b == 0x69)
5167 s->rip_offset = insn_const_size(ot);
5168 else if (b == 0x6b)
5169 s->rip_offset = 1;
0af10c86 5170 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2c0262af 5171 if (b == 0x69) {
0af10c86 5172 val = insn_get(env, s, ot);
1d1cc4d0 5173 tcg_gen_movi_tl(cpu_T1, val);
2c0262af 5174 } else if (b == 0x6b) {
4ba9938c 5175 val = (int8_t)insn_get(env, s, MO_8);
1d1cc4d0 5176 tcg_gen_movi_tl(cpu_T1, val);
2c0262af 5177 } else {
1d1cc4d0 5178 gen_op_mov_v_reg(ot, cpu_T1, reg);
2c0262af 5179 }
a4bcea3d 5180 switch (ot) {
0211e5af 5181#ifdef TARGET_X86_64
4ba9938c 5182 case MO_64:
1d1cc4d0 5183 tcg_gen_muls2_i64(cpu_regs[reg], cpu_T1, cpu_T0, cpu_T1);
a4bcea3d
RH
5184 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
5185 tcg_gen_sari_tl(cpu_cc_src, cpu_cc_dst, 63);
1d1cc4d0 5186 tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_T1);
a4bcea3d 5187 break;
0211e5af 5188#endif
4ba9938c 5189 case MO_32:
1d1cc4d0
RH
5190 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
5191 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
a4bcea3d
RH
5192 tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
5193 cpu_tmp2_i32, cpu_tmp3_i32);
5194 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
5195 tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
5196 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
5197 tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
5198 tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
5199 break;
5200 default:
1d1cc4d0
RH
5201 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
5202 tcg_gen_ext16s_tl(cpu_T1, cpu_T1);
0211e5af 5203 /* XXX: use 32 bit mul which could be faster */
1d1cc4d0
RH
5204 tcg_gen_mul_tl(cpu_T0, cpu_T0, cpu_T1);
5205 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
5206 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T0);
5207 tcg_gen_sub_tl(cpu_cc_src, cpu_T0, cpu_tmp0);
5208 gen_op_mov_reg_v(ot, reg, cpu_T0);
a4bcea3d 5209 break;
2c0262af 5210 }
3ca51d07 5211 set_cc_op(s, CC_OP_MULB + ot);
2c0262af
FB
5212 break;
5213 case 0x1c0:
5214 case 0x1c1: /* xadd Ev, Gv */
ab4e4aec 5215 ot = mo_b_d(b, dflag);
e3af7c78 5216 modrm = x86_ldub_code(env, s);
14ce26e7 5217 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 5218 mod = (modrm >> 6) & 3;
f53b0181 5219 gen_op_mov_v_reg(ot, cpu_T0, reg);
2c0262af 5220 if (mod == 3) {
14ce26e7 5221 rm = (modrm & 7) | REX_B(s);
1d1cc4d0
RH
5222 gen_op_mov_v_reg(ot, cpu_T1, rm);
5223 tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
5224 gen_op_mov_reg_v(ot, reg, cpu_T1);
5225 gen_op_mov_reg_v(ot, rm, cpu_T0);
2c0262af 5226 } else {
4eeb3939 5227 gen_lea_modrm(env, s, modrm);
f53b0181
EC
5228 if (s->prefix & PREFIX_LOCK) {
5229 tcg_gen_atomic_fetch_add_tl(cpu_T1, cpu_A0, cpu_T0,
5230 s->mem_index, ot | MO_LE);
5231 tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
5232 } else {
5233 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
5234 tcg_gen_add_tl(cpu_T0, cpu_T0, cpu_T1);
5235 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
5236 }
1d1cc4d0 5237 gen_op_mov_reg_v(ot, reg, cpu_T1);
2c0262af
FB
5238 }
5239 gen_op_update2_cc();
3ca51d07 5240 set_cc_op(s, CC_OP_ADDB + ot);
2c0262af
FB
5241 break;
5242 case 0x1b0:
5243 case 0x1b1: /* cmpxchg Ev, Gv */
cad3a37d 5244 {
ae03f8de 5245 TCGv oldv, newv, cmpv;
cad3a37d 5246
ab4e4aec 5247 ot = mo_b_d(b, dflag);
e3af7c78 5248 modrm = x86_ldub_code(env, s);
cad3a37d
FB
5249 reg = ((modrm >> 3) & 7) | rex_r;
5250 mod = (modrm >> 6) & 3;
ae03f8de
EC
5251 oldv = tcg_temp_new();
5252 newv = tcg_temp_new();
5253 cmpv = tcg_temp_new();
5254 gen_op_mov_v_reg(ot, newv, reg);
5255 tcg_gen_mov_tl(cmpv, cpu_regs[R_EAX]);
5256
5257 if (s->prefix & PREFIX_LOCK) {
5258 if (mod == 3) {
5259 goto illegal_op;
5260 }
4eeb3939 5261 gen_lea_modrm(env, s, modrm);
ae03f8de
EC
5262 tcg_gen_atomic_cmpxchg_tl(oldv, cpu_A0, cmpv, newv,
5263 s->mem_index, ot | MO_LE);
5264 gen_op_mov_reg_v(ot, R_EAX, oldv);
cad3a37d 5265 } else {
ae03f8de
EC
5266 if (mod == 3) {
5267 rm = (modrm & 7) | REX_B(s);
5268 gen_op_mov_v_reg(ot, oldv, rm);
5269 } else {
5270 gen_lea_modrm(env, s, modrm);
5271 gen_op_ld_v(s, ot, oldv, cpu_A0);
5272 rm = 0; /* avoid warning */
5273 }
5274 gen_extu(ot, oldv);
5275 gen_extu(ot, cmpv);
5276 /* store value = (old == cmp ? new : old); */
5277 tcg_gen_movcond_tl(TCG_COND_EQ, newv, oldv, cmpv, newv, oldv);
5278 if (mod == 3) {
5279 gen_op_mov_reg_v(ot, R_EAX, oldv);
5280 gen_op_mov_reg_v(ot, rm, newv);
5281 } else {
5282 /* Perform an unconditional store cycle like physical cpu;
5283 must be before changing accumulator to ensure
5284 idempotency if the store faults and the instruction
5285 is restarted */
5286 gen_op_st_v(s, ot, newv, cpu_A0);
5287 gen_op_mov_reg_v(ot, R_EAX, oldv);
5288 }
cad3a37d 5289 }
ae03f8de
EC
5290 tcg_gen_mov_tl(cpu_cc_src, oldv);
5291 tcg_gen_mov_tl(cpu_cc_srcT, cmpv);
5292 tcg_gen_sub_tl(cpu_cc_dst, cmpv, oldv);
3ca51d07 5293 set_cc_op(s, CC_OP_SUBB + ot);
ae03f8de
EC
5294 tcg_temp_free(oldv);
5295 tcg_temp_free(newv);
5296 tcg_temp_free(cmpv);
2c0262af 5297 }
2c0262af
FB
5298 break;
5299 case 0x1c7: /* cmpxchg8b */
e3af7c78 5300 modrm = x86_ldub_code(env, s);
2c0262af 5301 mod = (modrm >> 6) & 3;
71c3558e 5302 if ((mod == 3) || ((modrm & 0x38) != 0x8))
2c0262af 5303 goto illegal_op;
1b9d9ebb 5304#ifdef TARGET_X86_64
ab4e4aec 5305 if (dflag == MO_64) {
1b9d9ebb
FB
5306 if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
5307 goto illegal_op;
4eeb3939 5308 gen_lea_modrm(env, s, modrm);
b5e3b4c2 5309 if ((s->prefix & PREFIX_LOCK) && (tb_cflags(s->base.tb) & CF_PARALLEL)) {
ae03f8de
EC
5310 gen_helper_cmpxchg16b(cpu_env, cpu_A0);
5311 } else {
5312 gen_helper_cmpxchg16b_unlocked(cpu_env, cpu_A0);
5313 }
1b9d9ebb
FB
5314 } else
5315#endif
5316 {
5317 if (!(s->cpuid_features & CPUID_CX8))
5318 goto illegal_op;
4eeb3939 5319 gen_lea_modrm(env, s, modrm);
b5e3b4c2 5320 if ((s->prefix & PREFIX_LOCK) && (tb_cflags(s->base.tb) & CF_PARALLEL)) {
ae03f8de
EC
5321 gen_helper_cmpxchg8b(cpu_env, cpu_A0);
5322 } else {
5323 gen_helper_cmpxchg8b_unlocked(cpu_env, cpu_A0);
5324 }
1b9d9ebb 5325 }
3ca51d07 5326 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 5327 break;
3b46e624 5328
2c0262af
FB
5329 /**************************/
5330 /* push/pop */
5331 case 0x50 ... 0x57: /* push */
1d1cc4d0
RH
5332 gen_op_mov_v_reg(MO_32, cpu_T0, (b & 7) | REX_B(s));
5333 gen_push_v(s, cpu_T0);
2c0262af
FB
5334 break;
5335 case 0x58 ... 0x5f: /* pop */
8e31d234 5336 ot = gen_pop_T0(s);
77729c24 5337 /* NOTE: order is important for pop %sp */
8e31d234 5338 gen_pop_update(s, ot);
1d1cc4d0 5339 gen_op_mov_reg_v(ot, (b & 7) | REX_B(s), cpu_T0);
2c0262af
FB
5340 break;
5341 case 0x60: /* pusha */
14ce26e7
FB
5342 if (CODE64(s))
5343 goto illegal_op;
2c0262af
FB
5344 gen_pusha(s);
5345 break;
5346 case 0x61: /* popa */
14ce26e7
FB
5347 if (CODE64(s))
5348 goto illegal_op;
2c0262af
FB
5349 gen_popa(s);
5350 break;
5351 case 0x68: /* push Iv */
5352 case 0x6a:
ab4e4aec 5353 ot = mo_pushpop(s, dflag);
2c0262af 5354 if (b == 0x68)
0af10c86 5355 val = insn_get(env, s, ot);
2c0262af 5356 else
4ba9938c 5357 val = (int8_t)insn_get(env, s, MO_8);
1d1cc4d0
RH
5358 tcg_gen_movi_tl(cpu_T0, val);
5359 gen_push_v(s, cpu_T0);
2c0262af
FB
5360 break;
5361 case 0x8f: /* pop Ev */
e3af7c78 5362 modrm = x86_ldub_code(env, s);
77729c24 5363 mod = (modrm >> 6) & 3;
8e31d234 5364 ot = gen_pop_T0(s);
77729c24
FB
5365 if (mod == 3) {
5366 /* NOTE: order is important for pop %sp */
8e31d234 5367 gen_pop_update(s, ot);
14ce26e7 5368 rm = (modrm & 7) | REX_B(s);
1d1cc4d0 5369 gen_op_mov_reg_v(ot, rm, cpu_T0);
77729c24
FB
5370 } else {
5371 /* NOTE: order is important too for MMU exceptions */
14ce26e7 5372 s->popl_esp_hack = 1 << ot;
0af10c86 5373 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
77729c24 5374 s->popl_esp_hack = 0;
8e31d234 5375 gen_pop_update(s, ot);
77729c24 5376 }
2c0262af
FB
5377 break;
5378 case 0xc8: /* enter */
5379 {
5380 int level;
e3af7c78
PB
5381 val = x86_lduw_code(env, s);
5382 level = x86_ldub_code(env, s);
2c0262af
FB
5383 gen_enter(s, val, level);
5384 }
5385 break;
5386 case 0xc9: /* leave */
2045f04c 5387 gen_leave(s);
2c0262af
FB
5388 break;
5389 case 0x06: /* push es */
5390 case 0x0e: /* push cs */
5391 case 0x16: /* push ss */
5392 case 0x1e: /* push ds */
14ce26e7
FB
5393 if (CODE64(s))
5394 goto illegal_op;
2c0262af 5395 gen_op_movl_T0_seg(b >> 3);
1d1cc4d0 5396 gen_push_v(s, cpu_T0);
2c0262af
FB
5397 break;
5398 case 0x1a0: /* push fs */
5399 case 0x1a8: /* push gs */
5400 gen_op_movl_T0_seg((b >> 3) & 7);
1d1cc4d0 5401 gen_push_v(s, cpu_T0);
2c0262af
FB
5402 break;
5403 case 0x07: /* pop es */
5404 case 0x17: /* pop ss */
5405 case 0x1f: /* pop ds */
14ce26e7
FB
5406 if (CODE64(s))
5407 goto illegal_op;
2c0262af 5408 reg = b >> 3;
8e31d234 5409 ot = gen_pop_T0(s);
100ec099 5410 gen_movl_seg_T0(s, reg);
8e31d234 5411 gen_pop_update(s, ot);
f083d92c 5412 /* Note that reg == R_SS in gen_movl_seg_T0 always sets is_jmp. */
6cf147aa 5413 if (s->base.is_jmp) {
14ce26e7 5414 gen_jmp_im(s->pc - s->cs_base);
f083d92c
RH
5415 if (reg == R_SS) {
5416 s->tf = 0;
5417 gen_eob_inhibit_irq(s, true);
5418 } else {
5419 gen_eob(s);
5420 }
2c0262af
FB
5421 }
5422 break;
5423 case 0x1a1: /* pop fs */
5424 case 0x1a9: /* pop gs */
8e31d234 5425 ot = gen_pop_T0(s);
100ec099 5426 gen_movl_seg_T0(s, (b >> 3) & 7);
8e31d234 5427 gen_pop_update(s, ot);
6cf147aa 5428 if (s->base.is_jmp) {
14ce26e7 5429 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5430 gen_eob(s);
5431 }
5432 break;
5433
5434 /**************************/
5435 /* mov */
5436 case 0x88:
5437 case 0x89: /* mov Gv, Ev */
ab4e4aec 5438 ot = mo_b_d(b, dflag);
e3af7c78 5439 modrm = x86_ldub_code(env, s);
14ce26e7 5440 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5441
2c0262af 5442 /* generate a generic store */
0af10c86 5443 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
2c0262af
FB
5444 break;
5445 case 0xc6:
5446 case 0xc7: /* mov Ev, Iv */
ab4e4aec 5447 ot = mo_b_d(b, dflag);
e3af7c78 5448 modrm = x86_ldub_code(env, s);
2c0262af 5449 mod = (modrm >> 6) & 3;
14ce26e7
FB
5450 if (mod != 3) {
5451 s->rip_offset = insn_const_size(ot);
4eeb3939 5452 gen_lea_modrm(env, s, modrm);
14ce26e7 5453 }
0af10c86 5454 val = insn_get(env, s, ot);
1d1cc4d0 5455 tcg_gen_movi_tl(cpu_T0, val);
fd8ca9f6 5456 if (mod != 3) {
1d1cc4d0 5457 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
fd8ca9f6 5458 } else {
1d1cc4d0 5459 gen_op_mov_reg_v(ot, (modrm & 7) | REX_B(s), cpu_T0);
fd8ca9f6 5460 }
2c0262af
FB
5461 break;
5462 case 0x8a:
5463 case 0x8b: /* mov Ev, Gv */
ab4e4aec 5464 ot = mo_b_d(b, dflag);
e3af7c78 5465 modrm = x86_ldub_code(env, s);
14ce26e7 5466 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5467
0af10c86 5468 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
1d1cc4d0 5469 gen_op_mov_reg_v(ot, reg, cpu_T0);
2c0262af
FB
5470 break;
5471 case 0x8e: /* mov seg, Gv */
e3af7c78 5472 modrm = x86_ldub_code(env, s);
2c0262af
FB
5473 reg = (modrm >> 3) & 7;
5474 if (reg >= 6 || reg == R_CS)
5475 goto illegal_op;
4ba9938c 5476 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
100ec099 5477 gen_movl_seg_T0(s, reg);
f083d92c 5478 /* Note that reg == R_SS in gen_movl_seg_T0 always sets is_jmp. */
6cf147aa 5479 if (s->base.is_jmp) {
14ce26e7 5480 gen_jmp_im(s->pc - s->cs_base);
f083d92c
RH
5481 if (reg == R_SS) {
5482 s->tf = 0;
5483 gen_eob_inhibit_irq(s, true);
5484 } else {
5485 gen_eob(s);
5486 }
2c0262af
FB
5487 }
5488 break;
5489 case 0x8c: /* mov Gv, seg */
e3af7c78 5490 modrm = x86_ldub_code(env, s);
2c0262af
FB
5491 reg = (modrm >> 3) & 7;
5492 mod = (modrm >> 6) & 3;
5493 if (reg >= 6)
5494 goto illegal_op;
5495 gen_op_movl_T0_seg(reg);
ab4e4aec 5496 ot = mod == 3 ? dflag : MO_16;
0af10c86 5497 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
5498 break;
5499
5500 case 0x1b6: /* movzbS Gv, Eb */
5501 case 0x1b7: /* movzwS Gv, Eb */
5502 case 0x1be: /* movsbS Gv, Eb */
5503 case 0x1bf: /* movswS Gv, Eb */
5504 {
c8fbc479
RH
5505 TCGMemOp d_ot;
5506 TCGMemOp s_ot;
5507
2c0262af 5508 /* d_ot is the size of destination */
ab4e4aec 5509 d_ot = dflag;
2c0262af 5510 /* ot is the size of source */
4ba9938c 5511 ot = (b & 1) + MO_8;
c8fbc479
RH
5512 /* s_ot is the sign+size of source */
5513 s_ot = b & 8 ? MO_SIGN | ot : ot;
5514
e3af7c78 5515 modrm = x86_ldub_code(env, s);
14ce26e7 5516 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 5517 mod = (modrm >> 6) & 3;
14ce26e7 5518 rm = (modrm & 7) | REX_B(s);
3b46e624 5519
2c0262af 5520 if (mod == 3) {
04fc2f1c
RH
5521 if (s_ot == MO_SB && byte_reg_is_xH(rm)) {
5522 tcg_gen_sextract_tl(cpu_T0, cpu_regs[rm - 4], 8, 8);
5523 } else {
5524 gen_op_mov_v_reg(ot, cpu_T0, rm);
5525 switch (s_ot) {
5526 case MO_UB:
5527 tcg_gen_ext8u_tl(cpu_T0, cpu_T0);
5528 break;
5529 case MO_SB:
5530 tcg_gen_ext8s_tl(cpu_T0, cpu_T0);
5531 break;
5532 case MO_UW:
5533 tcg_gen_ext16u_tl(cpu_T0, cpu_T0);
5534 break;
5535 default:
5536 case MO_SW:
5537 tcg_gen_ext16s_tl(cpu_T0, cpu_T0);
5538 break;
5539 }
2c0262af 5540 }
1d1cc4d0 5541 gen_op_mov_reg_v(d_ot, reg, cpu_T0);
2c0262af 5542 } else {
4eeb3939 5543 gen_lea_modrm(env, s, modrm);
1d1cc4d0
RH
5544 gen_op_ld_v(s, s_ot, cpu_T0, cpu_A0);
5545 gen_op_mov_reg_v(d_ot, reg, cpu_T0);
2c0262af
FB
5546 }
5547 }
5548 break;
5549
5550 case 0x8d: /* lea */
e3af7c78 5551 modrm = x86_ldub_code(env, s);
3a1d9b8b
FB
5552 mod = (modrm >> 6) & 3;
5553 if (mod == 3)
5554 goto illegal_op;
14ce26e7 5555 reg = ((modrm >> 3) & 7) | rex_r;
a074ce42
RH
5556 {
5557 AddressParts a = gen_lea_modrm_0(env, s, modrm);
5558 TCGv ea = gen_lea_modrm_1(a);
620abfb0
PB
5559 gen_lea_v_seg(s, s->aflag, ea, -1, -1);
5560 gen_op_mov_reg_v(dflag, reg, cpu_A0);
a074ce42 5561 }
2c0262af 5562 break;
3b46e624 5563
2c0262af
FB
5564 case 0xa0: /* mov EAX, Ov */
5565 case 0xa1:
5566 case 0xa2: /* mov Ov, EAX */
5567 case 0xa3:
2c0262af 5568 {
14ce26e7
FB
5569 target_ulong offset_addr;
5570
ab4e4aec 5571 ot = mo_b_d(b, dflag);
1d71ddb1 5572 switch (s->aflag) {
14ce26e7 5573#ifdef TARGET_X86_64
1d71ddb1 5574 case MO_64:
e3af7c78 5575 offset_addr = x86_ldq_code(env, s);
1d71ddb1 5576 break;
14ce26e7 5577#endif
1d71ddb1
RH
5578 default:
5579 offset_addr = insn_get(env, s, s->aflag);
5580 break;
14ce26e7 5581 }
3250cff8 5582 tcg_gen_movi_tl(cpu_A0, offset_addr);
664e0f19 5583 gen_add_A0_ds_seg(s);
14ce26e7 5584 if ((b & 2) == 0) {
1d1cc4d0
RH
5585 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
5586 gen_op_mov_reg_v(ot, R_EAX, cpu_T0);
14ce26e7 5587 } else {
1d1cc4d0
RH
5588 gen_op_mov_v_reg(ot, cpu_T0, R_EAX);
5589 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
2c0262af
FB
5590 }
5591 }
2c0262af
FB
5592 break;
5593 case 0xd7: /* xlat */
1d71ddb1 5594 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EBX]);
1d1cc4d0
RH
5595 tcg_gen_ext8u_tl(cpu_T0, cpu_regs[R_EAX]);
5596 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T0);
1d71ddb1 5597 gen_extu(s->aflag, cpu_A0);
664e0f19 5598 gen_add_A0_ds_seg(s);
1d1cc4d0
RH
5599 gen_op_ld_v(s, MO_8, cpu_T0, cpu_A0);
5600 gen_op_mov_reg_v(MO_8, R_EAX, cpu_T0);
2c0262af
FB
5601 break;
5602 case 0xb0 ... 0xb7: /* mov R, Ib */
4ba9938c 5603 val = insn_get(env, s, MO_8);
1d1cc4d0
RH
5604 tcg_gen_movi_tl(cpu_T0, val);
5605 gen_op_mov_reg_v(MO_8, (b & 7) | REX_B(s), cpu_T0);
2c0262af
FB
5606 break;
5607 case 0xb8 ... 0xbf: /* mov R, Iv */
14ce26e7 5608#ifdef TARGET_X86_64
ab4e4aec 5609 if (dflag == MO_64) {
14ce26e7
FB
5610 uint64_t tmp;
5611 /* 64 bit case */
e3af7c78 5612 tmp = x86_ldq_code(env, s);
14ce26e7 5613 reg = (b & 7) | REX_B(s);
1d1cc4d0
RH
5614 tcg_gen_movi_tl(cpu_T0, tmp);
5615 gen_op_mov_reg_v(MO_64, reg, cpu_T0);
5fafdf24 5616 } else
14ce26e7
FB
5617#endif
5618 {
ab4e4aec 5619 ot = dflag;
0af10c86 5620 val = insn_get(env, s, ot);
14ce26e7 5621 reg = (b & 7) | REX_B(s);
1d1cc4d0
RH
5622 tcg_gen_movi_tl(cpu_T0, val);
5623 gen_op_mov_reg_v(ot, reg, cpu_T0);
14ce26e7 5624 }
2c0262af
FB
5625 break;
5626
5627 case 0x91 ... 0x97: /* xchg R, EAX */
7418027e 5628 do_xchg_reg_eax:
ab4e4aec 5629 ot = dflag;
14ce26e7 5630 reg = (b & 7) | REX_B(s);
2c0262af
FB
5631 rm = R_EAX;
5632 goto do_xchg_reg;
5633 case 0x86:
5634 case 0x87: /* xchg Ev, Gv */
ab4e4aec 5635 ot = mo_b_d(b, dflag);
e3af7c78 5636 modrm = x86_ldub_code(env, s);
14ce26e7 5637 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5638 mod = (modrm >> 6) & 3;
5639 if (mod == 3) {
14ce26e7 5640 rm = (modrm & 7) | REX_B(s);
2c0262af 5641 do_xchg_reg:
1d1cc4d0
RH
5642 gen_op_mov_v_reg(ot, cpu_T0, reg);
5643 gen_op_mov_v_reg(ot, cpu_T1, rm);
5644 gen_op_mov_reg_v(ot, rm, cpu_T0);
5645 gen_op_mov_reg_v(ot, reg, cpu_T1);
2c0262af 5646 } else {
4eeb3939 5647 gen_lea_modrm(env, s, modrm);
1d1cc4d0 5648 gen_op_mov_v_reg(ot, cpu_T0, reg);
2c0262af 5649 /* for xchg, lock is implicit */
ea97ebe8
EC
5650 tcg_gen_atomic_xchg_tl(cpu_T1, cpu_A0, cpu_T0,
5651 s->mem_index, ot | MO_LE);
1d1cc4d0 5652 gen_op_mov_reg_v(ot, reg, cpu_T1);
2c0262af
FB
5653 }
5654 break;
5655 case 0xc4: /* les Gv */
701ed211 5656 /* In CODE64 this is VEX3; see above. */
2c0262af
FB
5657 op = R_ES;
5658 goto do_lxx;
5659 case 0xc5: /* lds Gv */
701ed211 5660 /* In CODE64 this is VEX2; see above. */
2c0262af
FB
5661 op = R_DS;
5662 goto do_lxx;
5663 case 0x1b2: /* lss Gv */
5664 op = R_SS;
5665 goto do_lxx;
5666 case 0x1b4: /* lfs Gv */
5667 op = R_FS;
5668 goto do_lxx;
5669 case 0x1b5: /* lgs Gv */
5670 op = R_GS;
5671 do_lxx:
ab4e4aec 5672 ot = dflag != MO_16 ? MO_32 : MO_16;
e3af7c78 5673 modrm = x86_ldub_code(env, s);
14ce26e7 5674 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5675 mod = (modrm >> 6) & 3;
5676 if (mod == 3)
5677 goto illegal_op;
4eeb3939 5678 gen_lea_modrm(env, s, modrm);
1d1cc4d0 5679 gen_op_ld_v(s, ot, cpu_T1, cpu_A0);
830a19a4 5680 gen_add_A0_im(s, 1 << ot);
2c0262af 5681 /* load the segment first to handle exceptions properly */
1d1cc4d0 5682 gen_op_ld_v(s, MO_16, cpu_T0, cpu_A0);
100ec099 5683 gen_movl_seg_T0(s, op);
2c0262af 5684 /* then put the data */
1d1cc4d0 5685 gen_op_mov_reg_v(ot, reg, cpu_T1);
6cf147aa 5686 if (s->base.is_jmp) {
14ce26e7 5687 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5688 gen_eob(s);
5689 }
5690 break;
3b46e624 5691
2c0262af
FB
5692 /************************/
5693 /* shifts */
5694 case 0xc0:
5695 case 0xc1:
5696 /* shift Ev,Ib */
5697 shift = 2;
5698 grp2:
5699 {
ab4e4aec 5700 ot = mo_b_d(b, dflag);
e3af7c78 5701 modrm = x86_ldub_code(env, s);
2c0262af 5702 mod = (modrm >> 6) & 3;
2c0262af 5703 op = (modrm >> 3) & 7;
3b46e624 5704
2c0262af 5705 if (mod != 3) {
14ce26e7
FB
5706 if (shift == 2) {
5707 s->rip_offset = 1;
5708 }
4eeb3939 5709 gen_lea_modrm(env, s, modrm);
2c0262af
FB
5710 opreg = OR_TMP0;
5711 } else {
14ce26e7 5712 opreg = (modrm & 7) | REX_B(s);
2c0262af
FB
5713 }
5714
5715 /* simpler op */
5716 if (shift == 0) {
5717 gen_shift(s, op, ot, opreg, OR_ECX);
5718 } else {
5719 if (shift == 2) {
e3af7c78 5720 shift = x86_ldub_code(env, s);
2c0262af
FB
5721 }
5722 gen_shifti(s, op, ot, opreg, shift);
5723 }
5724 }
5725 break;
5726 case 0xd0:
5727 case 0xd1:
5728 /* shift Ev,1 */
5729 shift = 1;
5730 goto grp2;
5731 case 0xd2:
5732 case 0xd3:
5733 /* shift Ev,cl */
5734 shift = 0;
5735 goto grp2;
5736
5737 case 0x1a4: /* shld imm */
5738 op = 0;
5739 shift = 1;
5740 goto do_shiftd;
5741 case 0x1a5: /* shld cl */
5742 op = 0;
5743 shift = 0;
5744 goto do_shiftd;
5745 case 0x1ac: /* shrd imm */
5746 op = 1;
5747 shift = 1;
5748 goto do_shiftd;
5749 case 0x1ad: /* shrd cl */
5750 op = 1;
5751 shift = 0;
5752 do_shiftd:
ab4e4aec 5753 ot = dflag;
e3af7c78 5754 modrm = x86_ldub_code(env, s);
2c0262af 5755 mod = (modrm >> 6) & 3;
14ce26e7
FB
5756 rm = (modrm & 7) | REX_B(s);
5757 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 5758 if (mod != 3) {
4eeb3939 5759 gen_lea_modrm(env, s, modrm);
b6abf97d 5760 opreg = OR_TMP0;
2c0262af 5761 } else {
b6abf97d 5762 opreg = rm;
2c0262af 5763 }
1d1cc4d0 5764 gen_op_mov_v_reg(ot, cpu_T1, reg);
3b46e624 5765
2c0262af 5766 if (shift) {
e3af7c78 5767 TCGv imm = tcg_const_tl(x86_ldub_code(env, s));
3b9d3cf1
PB
5768 gen_shiftd_rm_T1(s, ot, opreg, op, imm);
5769 tcg_temp_free(imm);
2c0262af 5770 } else {
3b9d3cf1 5771 gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
2c0262af
FB
5772 }
5773 break;
5774
5775 /************************/
5776 /* floats */
5fafdf24 5777 case 0xd8 ... 0xdf:
7eee2a50
FB
5778 if (s->flags & (HF_EM_MASK | HF_TS_MASK)) {
5779 /* if CR0.EM or CR0.TS are set, generate an FPU exception */
5780 /* XXX: what to do if illegal op ? */
5781 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
5782 break;
5783 }
e3af7c78 5784 modrm = x86_ldub_code(env, s);
2c0262af
FB
5785 mod = (modrm >> 6) & 3;
5786 rm = modrm & 7;
5787 op = ((b & 7) << 3) | ((modrm >> 3) & 7);
2c0262af
FB
5788 if (mod != 3) {
5789 /* memory op */
4eeb3939 5790 gen_lea_modrm(env, s, modrm);
2c0262af
FB
5791 switch(op) {
5792 case 0x00 ... 0x07: /* fxxxs */
5793 case 0x10 ... 0x17: /* fixxxl */
5794 case 0x20 ... 0x27: /* fxxxl */
5795 case 0x30 ... 0x37: /* fixxx */
5796 {
5797 int op1;
5798 op1 = op & 7;
5799
5800 switch(op >> 4) {
5801 case 0:
80b02013
RH
5802 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5803 s->mem_index, MO_LEUL);
d3eb5eae 5804 gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5805 break;
5806 case 1:
80b02013
RH
5807 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5808 s->mem_index, MO_LEUL);
d3eb5eae 5809 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5810 break;
5811 case 2:
3c5f4116
RH
5812 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
5813 s->mem_index, MO_LEQ);
d3eb5eae 5814 gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5815 break;
5816 case 3:
5817 default:
80b02013
RH
5818 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5819 s->mem_index, MO_LESW);
d3eb5eae 5820 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5821 break;
5822 }
3b46e624 5823
a7812ae4 5824 gen_helper_fp_arith_ST0_FT0(op1);
2c0262af
FB
5825 if (op1 == 3) {
5826 /* fcomp needs pop */
d3eb5eae 5827 gen_helper_fpop(cpu_env);
2c0262af
FB
5828 }
5829 }
5830 break;
5831 case 0x08: /* flds */
5832 case 0x0a: /* fsts */
5833 case 0x0b: /* fstps */
465e9838
FB
5834 case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
5835 case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
5836 case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
2c0262af
FB
5837 switch(op & 7) {
5838 case 0:
5839 switch(op >> 4) {
5840 case 0:
80b02013
RH
5841 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5842 s->mem_index, MO_LEUL);
d3eb5eae 5843 gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5844 break;
5845 case 1:
80b02013
RH
5846 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5847 s->mem_index, MO_LEUL);
d3eb5eae 5848 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5849 break;
5850 case 2:
3c5f4116
RH
5851 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
5852 s->mem_index, MO_LEQ);
d3eb5eae 5853 gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5854 break;
5855 case 3:
5856 default:
80b02013
RH
5857 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5858 s->mem_index, MO_LESW);
d3eb5eae 5859 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5860 break;
5861 }
5862 break;
465e9838 5863 case 1:
19e6c4b8 5864 /* XXX: the corresponding CPUID bit must be tested ! */
465e9838
FB
5865 switch(op >> 4) {
5866 case 1:
d3eb5eae 5867 gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5868 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5869 s->mem_index, MO_LEUL);
465e9838
FB
5870 break;
5871 case 2:
d3eb5eae 5872 gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
3523e4bd
RH
5873 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
5874 s->mem_index, MO_LEQ);
465e9838
FB
5875 break;
5876 case 3:
5877 default:
d3eb5eae 5878 gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5879 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5880 s->mem_index, MO_LEUW);
19e6c4b8 5881 break;
465e9838 5882 }
d3eb5eae 5883 gen_helper_fpop(cpu_env);
465e9838 5884 break;
2c0262af
FB
5885 default:
5886 switch(op >> 4) {
5887 case 0:
d3eb5eae 5888 gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5889 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5890 s->mem_index, MO_LEUL);
2c0262af
FB
5891 break;
5892 case 1:
d3eb5eae 5893 gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5894 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5895 s->mem_index, MO_LEUL);
2c0262af
FB
5896 break;
5897 case 2:
d3eb5eae 5898 gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
3523e4bd
RH
5899 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
5900 s->mem_index, MO_LEQ);
2c0262af
FB
5901 break;
5902 case 3:
5903 default:
d3eb5eae 5904 gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5905 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5906 s->mem_index, MO_LEUW);
2c0262af
FB
5907 break;
5908 }
5909 if ((op & 7) == 3)
d3eb5eae 5910 gen_helper_fpop(cpu_env);
2c0262af
FB
5911 break;
5912 }
5913 break;
5914 case 0x0c: /* fldenv mem */
ab4e4aec 5915 gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
2c0262af
FB
5916 break;
5917 case 0x0d: /* fldcw mem */
80b02013
RH
5918 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5919 s->mem_index, MO_LEUW);
d3eb5eae 5920 gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5921 break;
5922 case 0x0e: /* fnstenv mem */
ab4e4aec 5923 gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
2c0262af
FB
5924 break;
5925 case 0x0f: /* fnstcw mem */
d3eb5eae 5926 gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5927 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5928 s->mem_index, MO_LEUW);
2c0262af
FB
5929 break;
5930 case 0x1d: /* fldt mem */
d3eb5eae 5931 gen_helper_fldt_ST0(cpu_env, cpu_A0);
2c0262af
FB
5932 break;
5933 case 0x1f: /* fstpt mem */
d3eb5eae
BS
5934 gen_helper_fstt_ST0(cpu_env, cpu_A0);
5935 gen_helper_fpop(cpu_env);
2c0262af
FB
5936 break;
5937 case 0x2c: /* frstor mem */
ab4e4aec 5938 gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
2c0262af
FB
5939 break;
5940 case 0x2e: /* fnsave mem */
ab4e4aec 5941 gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
2c0262af
FB
5942 break;
5943 case 0x2f: /* fnstsw mem */
d3eb5eae 5944 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5945 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5946 s->mem_index, MO_LEUW);
2c0262af
FB
5947 break;
5948 case 0x3c: /* fbld */
d3eb5eae 5949 gen_helper_fbld_ST0(cpu_env, cpu_A0);
2c0262af
FB
5950 break;
5951 case 0x3e: /* fbstp */
d3eb5eae
BS
5952 gen_helper_fbst_ST0(cpu_env, cpu_A0);
5953 gen_helper_fpop(cpu_env);
2c0262af
FB
5954 break;
5955 case 0x3d: /* fildll */
3c5f4116 5956 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
d3eb5eae 5957 gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5958 break;
5959 case 0x3f: /* fistpll */
d3eb5eae 5960 gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
3523e4bd 5961 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
d3eb5eae 5962 gen_helper_fpop(cpu_env);
2c0262af
FB
5963 break;
5964 default:
b9f9c5b4 5965 goto unknown_op;
2c0262af
FB
5966 }
5967 } else {
5968 /* register float ops */
5969 opreg = rm;
5970
5971 switch(op) {
5972 case 0x08: /* fld sti */
d3eb5eae
BS
5973 gen_helper_fpush(cpu_env);
5974 gen_helper_fmov_ST0_STN(cpu_env,
5975 tcg_const_i32((opreg + 1) & 7));
2c0262af
FB
5976 break;
5977 case 0x09: /* fxchg sti */
c169c906
FB
5978 case 0x29: /* fxchg4 sti, undocumented op */
5979 case 0x39: /* fxchg7 sti, undocumented op */
d3eb5eae 5980 gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
2c0262af
FB
5981 break;
5982 case 0x0a: /* grp d9/2 */
5983 switch(rm) {
5984 case 0: /* fnop */
023fe10d 5985 /* check exceptions (FreeBSD FPU probe) */
d3eb5eae 5986 gen_helper_fwait(cpu_env);
2c0262af
FB
5987 break;
5988 default:
b9f9c5b4 5989 goto unknown_op;
2c0262af
FB
5990 }
5991 break;
5992 case 0x0c: /* grp d9/4 */
5993 switch(rm) {
5994 case 0: /* fchs */
d3eb5eae 5995 gen_helper_fchs_ST0(cpu_env);
2c0262af
FB
5996 break;
5997 case 1: /* fabs */
d3eb5eae 5998 gen_helper_fabs_ST0(cpu_env);
2c0262af
FB
5999 break;
6000 case 4: /* ftst */
d3eb5eae
BS
6001 gen_helper_fldz_FT0(cpu_env);
6002 gen_helper_fcom_ST0_FT0(cpu_env);
2c0262af
FB
6003 break;
6004 case 5: /* fxam */
d3eb5eae 6005 gen_helper_fxam_ST0(cpu_env);
2c0262af
FB
6006 break;
6007 default:
b9f9c5b4 6008 goto unknown_op;
2c0262af
FB
6009 }
6010 break;
6011 case 0x0d: /* grp d9/5 */
6012 {
6013 switch(rm) {
6014 case 0:
d3eb5eae
BS
6015 gen_helper_fpush(cpu_env);
6016 gen_helper_fld1_ST0(cpu_env);
2c0262af
FB
6017 break;
6018 case 1:
d3eb5eae
BS
6019 gen_helper_fpush(cpu_env);
6020 gen_helper_fldl2t_ST0(cpu_env);
2c0262af
FB
6021 break;
6022 case 2:
d3eb5eae
BS
6023 gen_helper_fpush(cpu_env);
6024 gen_helper_fldl2e_ST0(cpu_env);
2c0262af
FB
6025 break;
6026 case 3:
d3eb5eae
BS
6027 gen_helper_fpush(cpu_env);
6028 gen_helper_fldpi_ST0(cpu_env);
2c0262af
FB
6029 break;
6030 case 4:
d3eb5eae
BS
6031 gen_helper_fpush(cpu_env);
6032 gen_helper_fldlg2_ST0(cpu_env);
2c0262af
FB
6033 break;
6034 case 5:
d3eb5eae
BS
6035 gen_helper_fpush(cpu_env);
6036 gen_helper_fldln2_ST0(cpu_env);
2c0262af
FB
6037 break;
6038 case 6:
d3eb5eae
BS
6039 gen_helper_fpush(cpu_env);
6040 gen_helper_fldz_ST0(cpu_env);
2c0262af
FB
6041 break;
6042 default:
b9f9c5b4 6043 goto unknown_op;
2c0262af
FB
6044 }
6045 }
6046 break;
6047 case 0x0e: /* grp d9/6 */
6048 switch(rm) {
6049 case 0: /* f2xm1 */
d3eb5eae 6050 gen_helper_f2xm1(cpu_env);
2c0262af
FB
6051 break;
6052 case 1: /* fyl2x */
d3eb5eae 6053 gen_helper_fyl2x(cpu_env);
2c0262af
FB
6054 break;
6055 case 2: /* fptan */
d3eb5eae 6056 gen_helper_fptan(cpu_env);
2c0262af
FB
6057 break;
6058 case 3: /* fpatan */
d3eb5eae 6059 gen_helper_fpatan(cpu_env);
2c0262af
FB
6060 break;
6061 case 4: /* fxtract */
d3eb5eae 6062 gen_helper_fxtract(cpu_env);
2c0262af
FB
6063 break;
6064 case 5: /* fprem1 */
d3eb5eae 6065 gen_helper_fprem1(cpu_env);
2c0262af
FB
6066 break;
6067 case 6: /* fdecstp */
d3eb5eae 6068 gen_helper_fdecstp(cpu_env);
2c0262af
FB
6069 break;
6070 default:
6071 case 7: /* fincstp */
d3eb5eae 6072 gen_helper_fincstp(cpu_env);
2c0262af
FB
6073 break;
6074 }
6075 break;
6076 case 0x0f: /* grp d9/7 */
6077 switch(rm) {
6078 case 0: /* fprem */
d3eb5eae 6079 gen_helper_fprem(cpu_env);
2c0262af
FB
6080 break;
6081 case 1: /* fyl2xp1 */
d3eb5eae 6082 gen_helper_fyl2xp1(cpu_env);
2c0262af
FB
6083 break;
6084 case 2: /* fsqrt */
d3eb5eae 6085 gen_helper_fsqrt(cpu_env);
2c0262af
FB
6086 break;
6087 case 3: /* fsincos */
d3eb5eae 6088 gen_helper_fsincos(cpu_env);
2c0262af
FB
6089 break;
6090 case 5: /* fscale */
d3eb5eae 6091 gen_helper_fscale(cpu_env);
2c0262af
FB
6092 break;
6093 case 4: /* frndint */
d3eb5eae 6094 gen_helper_frndint(cpu_env);
2c0262af
FB
6095 break;
6096 case 6: /* fsin */
d3eb5eae 6097 gen_helper_fsin(cpu_env);
2c0262af
FB
6098 break;
6099 default:
6100 case 7: /* fcos */
d3eb5eae 6101 gen_helper_fcos(cpu_env);
2c0262af
FB
6102 break;
6103 }
6104 break;
6105 case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
6106 case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
6107 case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
6108 {
6109 int op1;
3b46e624 6110
2c0262af
FB
6111 op1 = op & 7;
6112 if (op >= 0x20) {
a7812ae4 6113 gen_helper_fp_arith_STN_ST0(op1, opreg);
2c0262af 6114 if (op >= 0x30)
d3eb5eae 6115 gen_helper_fpop(cpu_env);
2c0262af 6116 } else {
d3eb5eae 6117 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
a7812ae4 6118 gen_helper_fp_arith_ST0_FT0(op1);
2c0262af
FB
6119 }
6120 }
6121 break;
6122 case 0x02: /* fcom */
c169c906 6123 case 0x22: /* fcom2, undocumented op */
d3eb5eae
BS
6124 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6125 gen_helper_fcom_ST0_FT0(cpu_env);
2c0262af
FB
6126 break;
6127 case 0x03: /* fcomp */
c169c906
FB
6128 case 0x23: /* fcomp3, undocumented op */
6129 case 0x32: /* fcomp5, undocumented op */
d3eb5eae
BS
6130 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6131 gen_helper_fcom_ST0_FT0(cpu_env);
6132 gen_helper_fpop(cpu_env);
2c0262af
FB
6133 break;
6134 case 0x15: /* da/5 */
6135 switch(rm) {
6136 case 1: /* fucompp */
d3eb5eae
BS
6137 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
6138 gen_helper_fucom_ST0_FT0(cpu_env);
6139 gen_helper_fpop(cpu_env);
6140 gen_helper_fpop(cpu_env);
2c0262af
FB
6141 break;
6142 default:
b9f9c5b4 6143 goto unknown_op;
2c0262af
FB
6144 }
6145 break;
6146 case 0x1c:
6147 switch(rm) {
6148 case 0: /* feni (287 only, just do nop here) */
6149 break;
6150 case 1: /* fdisi (287 only, just do nop here) */
6151 break;
6152 case 2: /* fclex */
d3eb5eae 6153 gen_helper_fclex(cpu_env);
2c0262af
FB
6154 break;
6155 case 3: /* fninit */
d3eb5eae 6156 gen_helper_fninit(cpu_env);
2c0262af
FB
6157 break;
6158 case 4: /* fsetpm (287 only, just do nop here) */
6159 break;
6160 default:
b9f9c5b4 6161 goto unknown_op;
2c0262af
FB
6162 }
6163 break;
6164 case 0x1d: /* fucomi */
bff93281
PM
6165 if (!(s->cpuid_features & CPUID_CMOV)) {
6166 goto illegal_op;
6167 }
773cdfcc 6168 gen_update_cc_op(s);
d3eb5eae
BS
6169 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6170 gen_helper_fucomi_ST0_FT0(cpu_env);
3ca51d07 6171 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6172 break;
6173 case 0x1e: /* fcomi */
bff93281
PM
6174 if (!(s->cpuid_features & CPUID_CMOV)) {
6175 goto illegal_op;
6176 }
773cdfcc 6177 gen_update_cc_op(s);
d3eb5eae
BS
6178 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6179 gen_helper_fcomi_ST0_FT0(cpu_env);
3ca51d07 6180 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 6181 break;
658c8bda 6182 case 0x28: /* ffree sti */
d3eb5eae 6183 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
5fafdf24 6184 break;
2c0262af 6185 case 0x2a: /* fst sti */
d3eb5eae 6186 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
2c0262af
FB
6187 break;
6188 case 0x2b: /* fstp sti */
c169c906
FB
6189 case 0x0b: /* fstp1 sti, undocumented op */
6190 case 0x3a: /* fstp8 sti, undocumented op */
6191 case 0x3b: /* fstp9 sti, undocumented op */
d3eb5eae
BS
6192 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
6193 gen_helper_fpop(cpu_env);
2c0262af
FB
6194 break;
6195 case 0x2c: /* fucom st(i) */
d3eb5eae
BS
6196 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6197 gen_helper_fucom_ST0_FT0(cpu_env);
2c0262af
FB
6198 break;
6199 case 0x2d: /* fucomp st(i) */
d3eb5eae
BS
6200 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6201 gen_helper_fucom_ST0_FT0(cpu_env);
6202 gen_helper_fpop(cpu_env);
2c0262af
FB
6203 break;
6204 case 0x33: /* de/3 */
6205 switch(rm) {
6206 case 1: /* fcompp */
d3eb5eae
BS
6207 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
6208 gen_helper_fcom_ST0_FT0(cpu_env);
6209 gen_helper_fpop(cpu_env);
6210 gen_helper_fpop(cpu_env);
2c0262af
FB
6211 break;
6212 default:
b9f9c5b4 6213 goto unknown_op;
2c0262af
FB
6214 }
6215 break;
c169c906 6216 case 0x38: /* ffreep sti, undocumented op */
d3eb5eae
BS
6217 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6218 gen_helper_fpop(cpu_env);
c169c906 6219 break;
2c0262af
FB
6220 case 0x3c: /* df/4 */
6221 switch(rm) {
6222 case 0:
d3eb5eae 6223 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
1d1cc4d0
RH
6224 tcg_gen_extu_i32_tl(cpu_T0, cpu_tmp2_i32);
6225 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T0);
2c0262af
FB
6226 break;
6227 default:
b9f9c5b4 6228 goto unknown_op;
2c0262af
FB
6229 }
6230 break;
6231 case 0x3d: /* fucomip */
bff93281
PM
6232 if (!(s->cpuid_features & CPUID_CMOV)) {
6233 goto illegal_op;
6234 }
773cdfcc 6235 gen_update_cc_op(s);
d3eb5eae
BS
6236 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6237 gen_helper_fucomi_ST0_FT0(cpu_env);
6238 gen_helper_fpop(cpu_env);
3ca51d07 6239 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6240 break;
6241 case 0x3e: /* fcomip */
bff93281
PM
6242 if (!(s->cpuid_features & CPUID_CMOV)) {
6243 goto illegal_op;
6244 }
773cdfcc 6245 gen_update_cc_op(s);
d3eb5eae
BS
6246 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6247 gen_helper_fcomi_ST0_FT0(cpu_env);
6248 gen_helper_fpop(cpu_env);
3ca51d07 6249 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 6250 break;
a2cc3b24
FB
6251 case 0x10 ... 0x13: /* fcmovxx */
6252 case 0x18 ... 0x1b:
6253 {
42a268c2
RH
6254 int op1;
6255 TCGLabel *l1;
d70040bc 6256 static const uint8_t fcmov_cc[8] = {
a2cc3b24
FB
6257 (JCC_B << 1),
6258 (JCC_Z << 1),
6259 (JCC_BE << 1),
6260 (JCC_P << 1),
6261 };
bff93281
PM
6262
6263 if (!(s->cpuid_features & CPUID_CMOV)) {
6264 goto illegal_op;
6265 }
1e4840bf 6266 op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
19e6c4b8 6267 l1 = gen_new_label();
dc259201 6268 gen_jcc1_noeob(s, op1, l1);
d3eb5eae 6269 gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
19e6c4b8 6270 gen_set_label(l1);
a2cc3b24
FB
6271 }
6272 break;
2c0262af 6273 default:
b9f9c5b4 6274 goto unknown_op;
2c0262af
FB
6275 }
6276 }
6277 break;
6278 /************************/
6279 /* string ops */
6280
6281 case 0xa4: /* movsS */
6282 case 0xa5:
ab4e4aec 6283 ot = mo_b_d(b, dflag);
2c0262af
FB
6284 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6285 gen_repz_movs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6286 } else {
6287 gen_movs(s, ot);
6288 }
6289 break;
3b46e624 6290
2c0262af
FB
6291 case 0xaa: /* stosS */
6292 case 0xab:
ab4e4aec 6293 ot = mo_b_d(b, dflag);
2c0262af
FB
6294 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6295 gen_repz_stos(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6296 } else {
6297 gen_stos(s, ot);
6298 }
6299 break;
6300 case 0xac: /* lodsS */
6301 case 0xad:
ab4e4aec 6302 ot = mo_b_d(b, dflag);
2c0262af
FB
6303 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6304 gen_repz_lods(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6305 } else {
6306 gen_lods(s, ot);
6307 }
6308 break;
6309 case 0xae: /* scasS */
6310 case 0xaf:
ab4e4aec 6311 ot = mo_b_d(b, dflag);
2c0262af
FB
6312 if (prefixes & PREFIX_REPNZ) {
6313 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6314 } else if (prefixes & PREFIX_REPZ) {
6315 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6316 } else {
6317 gen_scas(s, ot);
2c0262af
FB
6318 }
6319 break;
6320
6321 case 0xa6: /* cmpsS */
6322 case 0xa7:
ab4e4aec 6323 ot = mo_b_d(b, dflag);
2c0262af
FB
6324 if (prefixes & PREFIX_REPNZ) {
6325 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6326 } else if (prefixes & PREFIX_REPZ) {
6327 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6328 } else {
6329 gen_cmps(s, ot);
2c0262af
FB
6330 }
6331 break;
6332 case 0x6c: /* insS */
6333 case 0x6d:
ab4e4aec 6334 ot = mo_b_d32(b, dflag);
1d1cc4d0 6335 tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
b8b6a50b
FB
6336 gen_check_io(s, ot, pc_start - s->cs_base,
6337 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
f115e911
FB
6338 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6339 gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
2c0262af 6340 } else {
f115e911 6341 gen_ins(s, ot);
c5a49c63 6342 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef
PB
6343 gen_jmp(s, s->pc - s->cs_base);
6344 }
2c0262af
FB
6345 }
6346 break;
6347 case 0x6e: /* outsS */
6348 case 0x6f:
ab4e4aec 6349 ot = mo_b_d32(b, dflag);
1d1cc4d0 6350 tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
b8b6a50b
FB
6351 gen_check_io(s, ot, pc_start - s->cs_base,
6352 svm_is_rep(prefixes) | 4);
f115e911
FB
6353 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6354 gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
2c0262af 6355 } else {
f115e911 6356 gen_outs(s, ot);
c5a49c63 6357 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef
PB
6358 gen_jmp(s, s->pc - s->cs_base);
6359 }
2c0262af
FB
6360 }
6361 break;
6362
6363 /************************/
6364 /* port I/O */
0573fbfc 6365
2c0262af
FB
6366 case 0xe4:
6367 case 0xe5:
ab4e4aec 6368 ot = mo_b_d32(b, dflag);
e3af7c78 6369 val = x86_ldub_code(env, s);
1d1cc4d0 6370 tcg_gen_movi_tl(cpu_T0, val);
b8b6a50b
FB
6371 gen_check_io(s, ot, pc_start - s->cs_base,
6372 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
c5a49c63 6373 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef 6374 gen_io_start();
bd79255d 6375 }
1b90d56e 6376 tcg_gen_movi_i32(cpu_tmp2_i32, val);
1d1cc4d0
RH
6377 gen_helper_in_func(ot, cpu_T1, cpu_tmp2_i32);
6378 gen_op_mov_reg_v(ot, R_EAX, cpu_T1);
5223a942 6379 gen_bpt_io(s, cpu_tmp2_i32, ot);
c5a49c63 6380 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef
PB
6381 gen_io_end();
6382 gen_jmp(s, s->pc - s->cs_base);
6383 }
2c0262af
FB
6384 break;
6385 case 0xe6:
6386 case 0xe7:
ab4e4aec 6387 ot = mo_b_d32(b, dflag);
e3af7c78 6388 val = x86_ldub_code(env, s);
1d1cc4d0 6389 tcg_gen_movi_tl(cpu_T0, val);
b8b6a50b
FB
6390 gen_check_io(s, ot, pc_start - s->cs_base,
6391 svm_is_rep(prefixes));
1d1cc4d0 6392 gen_op_mov_v_reg(ot, cpu_T1, R_EAX);
b8b6a50b 6393
c5a49c63 6394 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef 6395 gen_io_start();
bd79255d 6396 }
1b90d56e 6397 tcg_gen_movi_i32(cpu_tmp2_i32, val);
1d1cc4d0 6398 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
a7812ae4 6399 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
5223a942 6400 gen_bpt_io(s, cpu_tmp2_i32, ot);
c5a49c63 6401 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef
PB
6402 gen_io_end();
6403 gen_jmp(s, s->pc - s->cs_base);
6404 }
2c0262af
FB
6405 break;
6406 case 0xec:
6407 case 0xed:
ab4e4aec 6408 ot = mo_b_d32(b, dflag);
1d1cc4d0 6409 tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
b8b6a50b
FB
6410 gen_check_io(s, ot, pc_start - s->cs_base,
6411 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
c5a49c63 6412 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef 6413 gen_io_start();
bd79255d 6414 }
1d1cc4d0
RH
6415 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
6416 gen_helper_in_func(ot, cpu_T1, cpu_tmp2_i32);
6417 gen_op_mov_reg_v(ot, R_EAX, cpu_T1);
5223a942 6418 gen_bpt_io(s, cpu_tmp2_i32, ot);
c5a49c63 6419 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef
PB
6420 gen_io_end();
6421 gen_jmp(s, s->pc - s->cs_base);
6422 }
2c0262af
FB
6423 break;
6424 case 0xee:
6425 case 0xef:
ab4e4aec 6426 ot = mo_b_d32(b, dflag);
1d1cc4d0 6427 tcg_gen_ext16u_tl(cpu_T0, cpu_regs[R_EDX]);
b8b6a50b
FB
6428 gen_check_io(s, ot, pc_start - s->cs_base,
6429 svm_is_rep(prefixes));
1d1cc4d0 6430 gen_op_mov_v_reg(ot, cpu_T1, R_EAX);
b8b6a50b 6431
c5a49c63 6432 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef 6433 gen_io_start();
bd79255d 6434 }
1d1cc4d0
RH
6435 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
6436 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T1);
a7812ae4 6437 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
5223a942 6438 gen_bpt_io(s, cpu_tmp2_i32, ot);
c5a49c63 6439 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2e70f6ef
PB
6440 gen_io_end();
6441 gen_jmp(s, s->pc - s->cs_base);
6442 }
2c0262af
FB
6443 break;
6444
6445 /************************/
6446 /* control */
6447 case 0xc2: /* ret im */
e3af7c78 6448 val = x86_ldsw_code(env, s);
8e31d234
RH
6449 ot = gen_pop_T0(s);
6450 gen_stack_update(s, val + (1 << ot));
6451 /* Note that gen_pop_T0 uses a zero-extending load. */
1d1cc4d0 6452 gen_op_jmp_v(cpu_T0);
7d117ce8 6453 gen_bnd_jmp(s);
b4aa2977 6454 gen_jr(s, cpu_T0);
2c0262af
FB
6455 break;
6456 case 0xc3: /* ret */
8e31d234
RH
6457 ot = gen_pop_T0(s);
6458 gen_pop_update(s, ot);
6459 /* Note that gen_pop_T0 uses a zero-extending load. */
1d1cc4d0 6460 gen_op_jmp_v(cpu_T0);
7d117ce8 6461 gen_bnd_jmp(s);
b4aa2977 6462 gen_jr(s, cpu_T0);
2c0262af
FB
6463 break;
6464 case 0xca: /* lret im */
e3af7c78 6465 val = x86_ldsw_code(env, s);
2c0262af
FB
6466 do_lret:
6467 if (s->pe && !s->vm86) {
773cdfcc 6468 gen_update_cc_op(s);
14ce26e7 6469 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 6470 gen_helper_lret_protected(cpu_env, tcg_const_i32(dflag - 1),
a7812ae4 6471 tcg_const_i32(val));
2c0262af
FB
6472 } else {
6473 gen_stack_A0(s);
6474 /* pop offset */
1d1cc4d0 6475 gen_op_ld_v(s, dflag, cpu_T0, cpu_A0);
2c0262af
FB
6476 /* NOTE: keeping EIP updated is not a problem in case of
6477 exception */
1d1cc4d0 6478 gen_op_jmp_v(cpu_T0);
2c0262af 6479 /* pop selector */
4e85057b 6480 gen_add_A0_im(s, 1 << dflag);
1d1cc4d0 6481 gen_op_ld_v(s, dflag, cpu_T0, cpu_A0);
3bd7da9e 6482 gen_op_movl_seg_T0_vm(R_CS);
2c0262af 6483 /* add stack offset */
ab4e4aec 6484 gen_stack_update(s, val + (2 << dflag));
2c0262af
FB
6485 }
6486 gen_eob(s);
6487 break;
6488 case 0xcb: /* lret */
6489 val = 0;
6490 goto do_lret;
6491 case 0xcf: /* iret */
872929aa 6492 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
2c0262af
FB
6493 if (!s->pe) {
6494 /* real mode */
ab4e4aec 6495 gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
3ca51d07 6496 set_cc_op(s, CC_OP_EFLAGS);
f115e911
FB
6497 } else if (s->vm86) {
6498 if (s->iopl != 3) {
6499 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6500 } else {
ab4e4aec 6501 gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
3ca51d07 6502 set_cc_op(s, CC_OP_EFLAGS);
f115e911 6503 }
2c0262af 6504 } else {
ab4e4aec 6505 gen_helper_iret_protected(cpu_env, tcg_const_i32(dflag - 1),
a7812ae4 6506 tcg_const_i32(s->pc - s->cs_base));
3ca51d07 6507 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 6508 }
410e9814 6509 gen_eob(s);
2c0262af
FB
6510 break;
6511 case 0xe8: /* call im */
6512 {
ab4e4aec 6513 if (dflag != MO_16) {
4ba9938c 6514 tval = (int32_t)insn_get(env, s, MO_32);
ab4e4aec 6515 } else {
4ba9938c 6516 tval = (int16_t)insn_get(env, s, MO_16);
ab4e4aec 6517 }
2c0262af 6518 next_eip = s->pc - s->cs_base;
14ce26e7 6519 tval += next_eip;
ab4e4aec 6520 if (dflag == MO_16) {
14ce26e7 6521 tval &= 0xffff;
ab4e4aec 6522 } else if (!CODE64(s)) {
99596385 6523 tval &= 0xffffffff;
ab4e4aec 6524 }
1d1cc4d0
RH
6525 tcg_gen_movi_tl(cpu_T0, next_eip);
6526 gen_push_v(s, cpu_T0);
7d117ce8 6527 gen_bnd_jmp(s);
14ce26e7 6528 gen_jmp(s, tval);
2c0262af
FB
6529 }
6530 break;
6531 case 0x9a: /* lcall im */
6532 {
6533 unsigned int selector, offset;
3b46e624 6534
14ce26e7
FB
6535 if (CODE64(s))
6536 goto illegal_op;
ab4e4aec 6537 ot = dflag;
0af10c86 6538 offset = insn_get(env, s, ot);
4ba9938c 6539 selector = insn_get(env, s, MO_16);
3b46e624 6540
1d1cc4d0
RH
6541 tcg_gen_movi_tl(cpu_T0, selector);
6542 tcg_gen_movi_tl(cpu_T1, offset);
2c0262af
FB
6543 }
6544 goto do_lcall;
ecada8a2 6545 case 0xe9: /* jmp im */
ab4e4aec 6546 if (dflag != MO_16) {
4ba9938c 6547 tval = (int32_t)insn_get(env, s, MO_32);
ab4e4aec 6548 } else {
4ba9938c 6549 tval = (int16_t)insn_get(env, s, MO_16);
ab4e4aec 6550 }
14ce26e7 6551 tval += s->pc - s->cs_base;
ab4e4aec 6552 if (dflag == MO_16) {
14ce26e7 6553 tval &= 0xffff;
ab4e4aec 6554 } else if (!CODE64(s)) {
32938e12 6555 tval &= 0xffffffff;
ab4e4aec 6556 }
7d117ce8 6557 gen_bnd_jmp(s);
14ce26e7 6558 gen_jmp(s, tval);
2c0262af
FB
6559 break;
6560 case 0xea: /* ljmp im */
6561 {
6562 unsigned int selector, offset;
6563
14ce26e7
FB
6564 if (CODE64(s))
6565 goto illegal_op;
ab4e4aec 6566 ot = dflag;
0af10c86 6567 offset = insn_get(env, s, ot);
4ba9938c 6568 selector = insn_get(env, s, MO_16);
3b46e624 6569
1d1cc4d0
RH
6570 tcg_gen_movi_tl(cpu_T0, selector);
6571 tcg_gen_movi_tl(cpu_T1, offset);
2c0262af
FB
6572 }
6573 goto do_ljmp;
6574 case 0xeb: /* jmp Jb */
4ba9938c 6575 tval = (int8_t)insn_get(env, s, MO_8);
14ce26e7 6576 tval += s->pc - s->cs_base;
ab4e4aec 6577 if (dflag == MO_16) {
14ce26e7 6578 tval &= 0xffff;
ab4e4aec 6579 }
14ce26e7 6580 gen_jmp(s, tval);
2c0262af
FB
6581 break;
6582 case 0x70 ... 0x7f: /* jcc Jb */
4ba9938c 6583 tval = (int8_t)insn_get(env, s, MO_8);
2c0262af
FB
6584 goto do_jcc;
6585 case 0x180 ... 0x18f: /* jcc Jv */
ab4e4aec 6586 if (dflag != MO_16) {
4ba9938c 6587 tval = (int32_t)insn_get(env, s, MO_32);
2c0262af 6588 } else {
4ba9938c 6589 tval = (int16_t)insn_get(env, s, MO_16);
2c0262af
FB
6590 }
6591 do_jcc:
6592 next_eip = s->pc - s->cs_base;
14ce26e7 6593 tval += next_eip;
ab4e4aec 6594 if (dflag == MO_16) {
14ce26e7 6595 tval &= 0xffff;
ab4e4aec 6596 }
7d117ce8 6597 gen_bnd_jmp(s);
14ce26e7 6598 gen_jcc(s, b, tval, next_eip);
2c0262af
FB
6599 break;
6600
6601 case 0x190 ... 0x19f: /* setcc Gv */
e3af7c78 6602 modrm = x86_ldub_code(env, s);
1d1cc4d0 6603 gen_setcc1(s, b, cpu_T0);
4ba9938c 6604 gen_ldst_modrm(env, s, modrm, MO_8, OR_TMP0, 1);
2c0262af
FB
6605 break;
6606 case 0x140 ... 0x14f: /* cmov Gv, Ev */
bff93281
PM
6607 if (!(s->cpuid_features & CPUID_CMOV)) {
6608 goto illegal_op;
6609 }
ab4e4aec 6610 ot = dflag;
e3af7c78 6611 modrm = x86_ldub_code(env, s);
f32d3781
PB
6612 reg = ((modrm >> 3) & 7) | rex_r;
6613 gen_cmovcc1(env, s, ot, b, modrm, reg);
2c0262af 6614 break;
3b46e624 6615
2c0262af
FB
6616 /************************/
6617 /* flags */
6618 case 0x9c: /* pushf */
872929aa 6619 gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
2c0262af
FB
6620 if (s->vm86 && s->iopl != 3) {
6621 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6622 } else {
773cdfcc 6623 gen_update_cc_op(s);
1d1cc4d0
RH
6624 gen_helper_read_eflags(cpu_T0, cpu_env);
6625 gen_push_v(s, cpu_T0);
2c0262af
FB
6626 }
6627 break;
6628 case 0x9d: /* popf */
872929aa 6629 gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
2c0262af
FB
6630 if (s->vm86 && s->iopl != 3) {
6631 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6632 } else {
8e31d234 6633 ot = gen_pop_T0(s);
2c0262af 6634 if (s->cpl == 0) {
ab4e4aec 6635 if (dflag != MO_16) {
1d1cc4d0 6636 gen_helper_write_eflags(cpu_env, cpu_T0,
f0967a1a
BS
6637 tcg_const_i32((TF_MASK | AC_MASK |
6638 ID_MASK | NT_MASK |
6639 IF_MASK |
6640 IOPL_MASK)));
2c0262af 6641 } else {
1d1cc4d0 6642 gen_helper_write_eflags(cpu_env, cpu_T0,
f0967a1a
BS
6643 tcg_const_i32((TF_MASK | AC_MASK |
6644 ID_MASK | NT_MASK |
6645 IF_MASK | IOPL_MASK)
6646 & 0xffff));
2c0262af
FB
6647 }
6648 } else {
4136f33c 6649 if (s->cpl <= s->iopl) {
ab4e4aec 6650 if (dflag != MO_16) {
1d1cc4d0 6651 gen_helper_write_eflags(cpu_env, cpu_T0,
f0967a1a
BS
6652 tcg_const_i32((TF_MASK |
6653 AC_MASK |
6654 ID_MASK |
6655 NT_MASK |
6656 IF_MASK)));
4136f33c 6657 } else {
1d1cc4d0 6658 gen_helper_write_eflags(cpu_env, cpu_T0,
f0967a1a
BS
6659 tcg_const_i32((TF_MASK |
6660 AC_MASK |
6661 ID_MASK |
6662 NT_MASK |
6663 IF_MASK)
6664 & 0xffff));
4136f33c 6665 }
2c0262af 6666 } else {
ab4e4aec 6667 if (dflag != MO_16) {
1d1cc4d0 6668 gen_helper_write_eflags(cpu_env, cpu_T0,
f0967a1a
BS
6669 tcg_const_i32((TF_MASK | AC_MASK |
6670 ID_MASK | NT_MASK)));
4136f33c 6671 } else {
1d1cc4d0 6672 gen_helper_write_eflags(cpu_env, cpu_T0,
f0967a1a
BS
6673 tcg_const_i32((TF_MASK | AC_MASK |
6674 ID_MASK | NT_MASK)
6675 & 0xffff));
4136f33c 6676 }
2c0262af
FB
6677 }
6678 }
8e31d234 6679 gen_pop_update(s, ot);
3ca51d07 6680 set_cc_op(s, CC_OP_EFLAGS);
a9321a4d 6681 /* abort translation because TF/AC flag may change */
14ce26e7 6682 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
6683 gen_eob(s);
6684 }
6685 break;
6686 case 0x9e: /* sahf */
12e26b75 6687 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
14ce26e7 6688 goto illegal_op;
1d1cc4d0 6689 gen_op_mov_v_reg(MO_8, cpu_T0, R_AH);
d229edce 6690 gen_compute_eflags(s);
bd7a7b33 6691 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, CC_O);
1d1cc4d0
RH
6692 tcg_gen_andi_tl(cpu_T0, cpu_T0, CC_S | CC_Z | CC_A | CC_P | CC_C);
6693 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_T0);
2c0262af
FB
6694 break;
6695 case 0x9f: /* lahf */
12e26b75 6696 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
14ce26e7 6697 goto illegal_op;
d229edce 6698 gen_compute_eflags(s);
bd7a7b33 6699 /* Note: gen_compute_eflags() only gives the condition codes */
1d1cc4d0
RH
6700 tcg_gen_ori_tl(cpu_T0, cpu_cc_src, 0x02);
6701 gen_op_mov_reg_v(MO_8, R_AH, cpu_T0);
2c0262af
FB
6702 break;
6703 case 0xf5: /* cmc */
d229edce 6704 gen_compute_eflags(s);
bd7a7b33 6705 tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
2c0262af
FB
6706 break;
6707 case 0xf8: /* clc */
d229edce 6708 gen_compute_eflags(s);
bd7a7b33 6709 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
2c0262af
FB
6710 break;
6711 case 0xf9: /* stc */
d229edce 6712 gen_compute_eflags(s);
bd7a7b33 6713 tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
2c0262af
FB
6714 break;
6715 case 0xfc: /* cld */
b6abf97d 6716 tcg_gen_movi_i32(cpu_tmp2_i32, 1);
317ac620 6717 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
2c0262af
FB
6718 break;
6719 case 0xfd: /* std */
b6abf97d 6720 tcg_gen_movi_i32(cpu_tmp2_i32, -1);
317ac620 6721 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
2c0262af
FB
6722 break;
6723
6724 /************************/
6725 /* bit operations */
6726 case 0x1ba: /* bt/bts/btr/btc Gv, im */
ab4e4aec 6727 ot = dflag;
e3af7c78 6728 modrm = x86_ldub_code(env, s);
33698e5f 6729 op = (modrm >> 3) & 7;
2c0262af 6730 mod = (modrm >> 6) & 3;
14ce26e7 6731 rm = (modrm & 7) | REX_B(s);
2c0262af 6732 if (mod != 3) {
14ce26e7 6733 s->rip_offset = 1;
4eeb3939 6734 gen_lea_modrm(env, s, modrm);
cfe819d3
EC
6735 if (!(s->prefix & PREFIX_LOCK)) {
6736 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
6737 }
2c0262af 6738 } else {
1d1cc4d0 6739 gen_op_mov_v_reg(ot, cpu_T0, rm);
2c0262af
FB
6740 }
6741 /* load shift */
e3af7c78 6742 val = x86_ldub_code(env, s);
1d1cc4d0 6743 tcg_gen_movi_tl(cpu_T1, val);
2c0262af 6744 if (op < 4)
b9f9c5b4 6745 goto unknown_op;
2c0262af 6746 op -= 4;
f484d386 6747 goto bt_op;
2c0262af
FB
6748 case 0x1a3: /* bt Gv, Ev */
6749 op = 0;
6750 goto do_btx;
6751 case 0x1ab: /* bts */
6752 op = 1;
6753 goto do_btx;
6754 case 0x1b3: /* btr */
6755 op = 2;
6756 goto do_btx;
6757 case 0x1bb: /* btc */
6758 op = 3;
6759 do_btx:
ab4e4aec 6760 ot = dflag;
e3af7c78 6761 modrm = x86_ldub_code(env, s);
14ce26e7 6762 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 6763 mod = (modrm >> 6) & 3;
14ce26e7 6764 rm = (modrm & 7) | REX_B(s);
1d1cc4d0 6765 gen_op_mov_v_reg(MO_32, cpu_T1, reg);
2c0262af 6766 if (mod != 3) {
cfe819d3 6767 AddressParts a = gen_lea_modrm_0(env, s, modrm);
2c0262af 6768 /* specific case: we need to add a displacement */
1d1cc4d0
RH
6769 gen_exts(ot, cpu_T1);
6770 tcg_gen_sari_tl(cpu_tmp0, cpu_T1, 3 + ot);
f484d386 6771 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, ot);
cfe819d3
EC
6772 tcg_gen_add_tl(cpu_A0, gen_lea_modrm_1(a), cpu_tmp0);
6773 gen_lea_v_seg(s, s->aflag, cpu_A0, a.def_seg, s->override);
6774 if (!(s->prefix & PREFIX_LOCK)) {
6775 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
6776 }
2c0262af 6777 } else {
1d1cc4d0 6778 gen_op_mov_v_reg(ot, cpu_T0, rm);
2c0262af 6779 }
f484d386 6780 bt_op:
1d1cc4d0 6781 tcg_gen_andi_tl(cpu_T1, cpu_T1, (1 << (3 + ot)) - 1);
cfe819d3
EC
6782 tcg_gen_movi_tl(cpu_tmp0, 1);
6783 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T1);
6784 if (s->prefix & PREFIX_LOCK) {
6785 switch (op) {
6786 case 0: /* bt */
6787 /* Needs no atomic ops; we surpressed the normal
6788 memory load for LOCK above so do it now. */
6789 gen_op_ld_v(s, ot, cpu_T0, cpu_A0);
6790 break;
6791 case 1: /* bts */
6792 tcg_gen_atomic_fetch_or_tl(cpu_T0, cpu_A0, cpu_tmp0,
6793 s->mem_index, ot | MO_LE);
6794 break;
6795 case 2: /* btr */
6796 tcg_gen_not_tl(cpu_tmp0, cpu_tmp0);
6797 tcg_gen_atomic_fetch_and_tl(cpu_T0, cpu_A0, cpu_tmp0,
6798 s->mem_index, ot | MO_LE);
6799 break;
6800 default:
6801 case 3: /* btc */
6802 tcg_gen_atomic_fetch_xor_tl(cpu_T0, cpu_A0, cpu_tmp0,
6803 s->mem_index, ot | MO_LE);
6804 break;
6805 }
6806 tcg_gen_shr_tl(cpu_tmp4, cpu_T0, cpu_T1);
6807 } else {
6808 tcg_gen_shr_tl(cpu_tmp4, cpu_T0, cpu_T1);
6809 switch (op) {
6810 case 0: /* bt */
6811 /* Data already loaded; nothing to do. */
6812 break;
6813 case 1: /* bts */
6814 tcg_gen_or_tl(cpu_T0, cpu_T0, cpu_tmp0);
6815 break;
6816 case 2: /* btr */
6817 tcg_gen_andc_tl(cpu_T0, cpu_T0, cpu_tmp0);
6818 break;
6819 default:
6820 case 3: /* btc */
6821 tcg_gen_xor_tl(cpu_T0, cpu_T0, cpu_tmp0);
6822 break;
6823 }
6824 if (op != 0) {
6825 if (mod != 3) {
6826 gen_op_st_v(s, ot, cpu_T0, cpu_A0);
6827 } else {
6828 gen_op_mov_reg_v(ot, rm, cpu_T0);
6829 }
fd8ca9f6 6830 }
dc1823ce
RH
6831 }
6832
6833 /* Delay all CC updates until after the store above. Note that
6834 C is the result of the test, Z is unchanged, and the others
6835 are all undefined. */
6836 switch (s->cc_op) {
6837 case CC_OP_MULB ... CC_OP_MULQ:
6838 case CC_OP_ADDB ... CC_OP_ADDQ:
6839 case CC_OP_ADCB ... CC_OP_ADCQ:
6840 case CC_OP_SUBB ... CC_OP_SUBQ:
6841 case CC_OP_SBBB ... CC_OP_SBBQ:
6842 case CC_OP_LOGICB ... CC_OP_LOGICQ:
6843 case CC_OP_INCB ... CC_OP_INCQ:
6844 case CC_OP_DECB ... CC_OP_DECQ:
6845 case CC_OP_SHLB ... CC_OP_SHLQ:
6846 case CC_OP_SARB ... CC_OP_SARQ:
6847 case CC_OP_BMILGB ... CC_OP_BMILGQ:
6848 /* Z was going to be computed from the non-zero status of CC_DST.
6849 We can get that same Z value (and the new C value) by leaving
6850 CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
6851 same width. */
f484d386 6852 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
dc1823ce
RH
6853 set_cc_op(s, ((s->cc_op - CC_OP_MULB) & 3) + CC_OP_SARB);
6854 break;
6855 default:
6856 /* Otherwise, generate EFLAGS and replace the C bit. */
6857 gen_compute_eflags(s);
6858 tcg_gen_deposit_tl(cpu_cc_src, cpu_cc_src, cpu_tmp4,
6859 ctz32(CC_C), 1);
6860 break;
2c0262af
FB
6861 }
6862 break;
321c5351
RH
6863 case 0x1bc: /* bsf / tzcnt */
6864 case 0x1bd: /* bsr / lzcnt */
ab4e4aec 6865 ot = dflag;
e3af7c78 6866 modrm = x86_ldub_code(env, s);
321c5351
RH
6867 reg = ((modrm >> 3) & 7) | rex_r;
6868 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
1d1cc4d0 6869 gen_extu(ot, cpu_T0);
321c5351
RH
6870
6871 /* Note that lzcnt and tzcnt are in different extensions. */
6872 if ((prefixes & PREFIX_REPZ)
6873 && (b & 1
6874 ? s->cpuid_ext3_features & CPUID_EXT3_ABM
6875 : s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)) {
6876 int size = 8 << ot;
e5143c90 6877 /* For lzcnt/tzcnt, C bit is defined related to the input. */
1d1cc4d0 6878 tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
321c5351
RH
6879 if (b & 1) {
6880 /* For lzcnt, reduce the target_ulong result by the
6881 number of zeros that we expect to find at the top. */
e5143c90 6882 tcg_gen_clzi_tl(cpu_T0, cpu_T0, TARGET_LONG_BITS);
1d1cc4d0 6883 tcg_gen_subi_tl(cpu_T0, cpu_T0, TARGET_LONG_BITS - size);
6191b059 6884 } else {
e5143c90
RH
6885 /* For tzcnt, a zero input must return the operand size. */
6886 tcg_gen_ctzi_tl(cpu_T0, cpu_T0, size);
6191b059 6887 }
e5143c90 6888 /* For lzcnt/tzcnt, Z bit is defined related to the result. */
321c5351
RH
6889 gen_op_update1_cc();
6890 set_cc_op(s, CC_OP_BMILGB + ot);
6891 } else {
6892 /* For bsr/bsf, only the Z bit is defined and it is related
6893 to the input and not the result. */
1d1cc4d0 6894 tcg_gen_mov_tl(cpu_cc_dst, cpu_T0);
321c5351 6895 set_cc_op(s, CC_OP_LOGICB + ot);
e5143c90
RH
6896
6897 /* ??? The manual says that the output is undefined when the
6898 input is zero, but real hardware leaves it unchanged, and
6899 real programs appear to depend on that. Accomplish this
6900 by passing the output as the value to return upon zero. */
321c5351
RH
6901 if (b & 1) {
6902 /* For bsr, return the bit index of the first 1 bit,
6903 not the count of leading zeros. */
e5143c90
RH
6904 tcg_gen_xori_tl(cpu_T1, cpu_regs[reg], TARGET_LONG_BITS - 1);
6905 tcg_gen_clz_tl(cpu_T0, cpu_T0, cpu_T1);
1d1cc4d0 6906 tcg_gen_xori_tl(cpu_T0, cpu_T0, TARGET_LONG_BITS - 1);
321c5351 6907 } else {
e5143c90 6908 tcg_gen_ctz_tl(cpu_T0, cpu_T0, cpu_regs[reg]);
321c5351 6909 }
6191b059 6910 }
1d1cc4d0 6911 gen_op_mov_reg_v(ot, reg, cpu_T0);
2c0262af
FB
6912 break;
6913 /************************/
6914 /* bcd */
6915 case 0x27: /* daa */
14ce26e7
FB
6916 if (CODE64(s))
6917 goto illegal_op;
773cdfcc 6918 gen_update_cc_op(s);
7923057b 6919 gen_helper_daa(cpu_env);
3ca51d07 6920 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6921 break;
6922 case 0x2f: /* das */
14ce26e7
FB
6923 if (CODE64(s))
6924 goto illegal_op;
773cdfcc 6925 gen_update_cc_op(s);
7923057b 6926 gen_helper_das(cpu_env);
3ca51d07 6927 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6928 break;
6929 case 0x37: /* aaa */
14ce26e7
FB
6930 if (CODE64(s))
6931 goto illegal_op;
773cdfcc 6932 gen_update_cc_op(s);
7923057b 6933 gen_helper_aaa(cpu_env);
3ca51d07 6934 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6935 break;
6936 case 0x3f: /* aas */
14ce26e7
FB
6937 if (CODE64(s))
6938 goto illegal_op;
773cdfcc 6939 gen_update_cc_op(s);
7923057b 6940 gen_helper_aas(cpu_env);
3ca51d07 6941 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6942 break;
6943 case 0xd4: /* aam */
14ce26e7
FB
6944 if (CODE64(s))
6945 goto illegal_op;
e3af7c78 6946 val = x86_ldub_code(env, s);
b6d7c3db
TS
6947 if (val == 0) {
6948 gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
6949 } else {
7923057b 6950 gen_helper_aam(cpu_env, tcg_const_i32(val));
3ca51d07 6951 set_cc_op(s, CC_OP_LOGICB);
b6d7c3db 6952 }
2c0262af
FB
6953 break;
6954 case 0xd5: /* aad */
14ce26e7
FB
6955 if (CODE64(s))
6956 goto illegal_op;
e3af7c78 6957 val = x86_ldub_code(env, s);
7923057b 6958 gen_helper_aad(cpu_env, tcg_const_i32(val));
3ca51d07 6959 set_cc_op(s, CC_OP_LOGICB);
2c0262af
FB
6960 break;
6961 /************************/
6962 /* misc */
6963 case 0x90: /* nop */
ab1f142b 6964 /* XXX: correct lock test for all insn */
7418027e 6965 if (prefixes & PREFIX_LOCK) {
ab1f142b 6966 goto illegal_op;
7418027e
RH
6967 }
6968 /* If REX_B is set, then this is xchg eax, r8d, not a nop. */
6969 if (REX_B(s)) {
6970 goto do_xchg_reg_eax;
6971 }
0573fbfc 6972 if (prefixes & PREFIX_REPZ) {
81f3053b
PB
6973 gen_update_cc_op(s);
6974 gen_jmp_im(pc_start - s->cs_base);
6975 gen_helper_pause(cpu_env, tcg_const_i32(s->pc - pc_start));
6cf147aa 6976 s->base.is_jmp = DISAS_NORETURN;
0573fbfc 6977 }
2c0262af
FB
6978 break;
6979 case 0x9b: /* fwait */
5fafdf24 6980 if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
7eee2a50
FB
6981 (HF_MP_MASK | HF_TS_MASK)) {
6982 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
2ee73ac3 6983 } else {
d3eb5eae 6984 gen_helper_fwait(cpu_env);
7eee2a50 6985 }
2c0262af
FB
6986 break;
6987 case 0xcc: /* int3 */
6988 gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
6989 break;
6990 case 0xcd: /* int N */
e3af7c78 6991 val = x86_ldub_code(env, s);
f115e911 6992 if (s->vm86 && s->iopl != 3) {
5fafdf24 6993 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
f115e911
FB
6994 } else {
6995 gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
6996 }
2c0262af
FB
6997 break;
6998 case 0xce: /* into */
14ce26e7
FB
6999 if (CODE64(s))
7000 goto illegal_op;
773cdfcc 7001 gen_update_cc_op(s);
a8ede8ba 7002 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7003 gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
2c0262af 7004 break;
0b97134b 7005#ifdef WANT_ICEBP
2c0262af 7006 case 0xf1: /* icebp (undocumented, exits to external debugger) */
872929aa 7007 gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
aba9d61e 7008#if 1
2c0262af 7009 gen_debug(s, pc_start - s->cs_base);
aba9d61e
FB
7010#else
7011 /* start debug */
bbd77c18 7012 tb_flush(CPU(x86_env_get_cpu(env)));
24537a01 7013 qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
aba9d61e 7014#endif
2c0262af 7015 break;
0b97134b 7016#endif
2c0262af
FB
7017 case 0xfa: /* cli */
7018 if (!s->vm86) {
7019 if (s->cpl <= s->iopl) {
f0967a1a 7020 gen_helper_cli(cpu_env);
2c0262af
FB
7021 } else {
7022 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7023 }
7024 } else {
7025 if (s->iopl == 3) {
f0967a1a 7026 gen_helper_cli(cpu_env);
2c0262af
FB
7027 } else {
7028 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7029 }
7030 }
7031 break;
7032 case 0xfb: /* sti */
f083d92c
RH
7033 if (s->vm86 ? s->iopl == 3 : s->cpl <= s->iopl) {
7034 gen_helper_sti(cpu_env);
7035 /* interruptions are enabled only the first insn after sti */
7036 gen_jmp_im(s->pc - s->cs_base);
7037 gen_eob_inhibit_irq(s, true);
2c0262af 7038 } else {
f083d92c 7039 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
2c0262af
FB
7040 }
7041 break;
7042 case 0x62: /* bound */
14ce26e7
FB
7043 if (CODE64(s))
7044 goto illegal_op;
ab4e4aec 7045 ot = dflag;
e3af7c78 7046 modrm = x86_ldub_code(env, s);
2c0262af
FB
7047 reg = (modrm >> 3) & 7;
7048 mod = (modrm >> 6) & 3;
7049 if (mod == 3)
7050 goto illegal_op;
1d1cc4d0 7051 gen_op_mov_v_reg(ot, cpu_T0, reg);
4eeb3939 7052 gen_lea_modrm(env, s, modrm);
1d1cc4d0 7053 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
4ba9938c 7054 if (ot == MO_16) {
92fc4b58
BS
7055 gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
7056 } else {
7057 gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
7058 }
2c0262af
FB
7059 break;
7060 case 0x1c8 ... 0x1cf: /* bswap reg */
14ce26e7
FB
7061 reg = (b & 7) | REX_B(s);
7062#ifdef TARGET_X86_64
ab4e4aec 7063 if (dflag == MO_64) {
1d1cc4d0
RH
7064 gen_op_mov_v_reg(MO_64, cpu_T0, reg);
7065 tcg_gen_bswap64_i64(cpu_T0, cpu_T0);
7066 gen_op_mov_reg_v(MO_64, reg, cpu_T0);
5fafdf24 7067 } else
8777643e 7068#endif
57fec1fe 7069 {
1d1cc4d0
RH
7070 gen_op_mov_v_reg(MO_32, cpu_T0, reg);
7071 tcg_gen_ext32u_tl(cpu_T0, cpu_T0);
7072 tcg_gen_bswap32_tl(cpu_T0, cpu_T0);
7073 gen_op_mov_reg_v(MO_32, reg, cpu_T0);
14ce26e7 7074 }
2c0262af
FB
7075 break;
7076 case 0xd6: /* salc */
14ce26e7
FB
7077 if (CODE64(s))
7078 goto illegal_op;
1d1cc4d0
RH
7079 gen_compute_eflags_c(s, cpu_T0);
7080 tcg_gen_neg_tl(cpu_T0, cpu_T0);
7081 gen_op_mov_reg_v(MO_8, R_EAX, cpu_T0);
2c0262af
FB
7082 break;
7083 case 0xe0: /* loopnz */
7084 case 0xe1: /* loopz */
2c0262af
FB
7085 case 0xe2: /* loop */
7086 case 0xe3: /* jecxz */
14ce26e7 7087 {
42a268c2 7088 TCGLabel *l1, *l2, *l3;
14ce26e7 7089
4ba9938c 7090 tval = (int8_t)insn_get(env, s, MO_8);
14ce26e7
FB
7091 next_eip = s->pc - s->cs_base;
7092 tval += next_eip;
ab4e4aec 7093 if (dflag == MO_16) {
14ce26e7 7094 tval &= 0xffff;
ab4e4aec 7095 }
3b46e624 7096
14ce26e7
FB
7097 l1 = gen_new_label();
7098 l2 = gen_new_label();
6e0d8677 7099 l3 = gen_new_label();
14ce26e7 7100 b &= 3;
6e0d8677
FB
7101 switch(b) {
7102 case 0: /* loopnz */
7103 case 1: /* loopz */
1d71ddb1
RH
7104 gen_op_add_reg_im(s->aflag, R_ECX, -1);
7105 gen_op_jz_ecx(s->aflag, l3);
5bdb91b0 7106 gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
6e0d8677
FB
7107 break;
7108 case 2: /* loop */
1d71ddb1
RH
7109 gen_op_add_reg_im(s->aflag, R_ECX, -1);
7110 gen_op_jnz_ecx(s->aflag, l1);
6e0d8677
FB
7111 break;
7112 default:
7113 case 3: /* jcxz */
1d71ddb1 7114 gen_op_jz_ecx(s->aflag, l1);
6e0d8677 7115 break;
14ce26e7
FB
7116 }
7117
6e0d8677 7118 gen_set_label(l3);
14ce26e7 7119 gen_jmp_im(next_eip);
8e1c85e3 7120 tcg_gen_br(l2);
6e0d8677 7121
14ce26e7
FB
7122 gen_set_label(l1);
7123 gen_jmp_im(tval);
7124 gen_set_label(l2);
7125 gen_eob(s);
7126 }
2c0262af
FB
7127 break;
7128 case 0x130: /* wrmsr */
7129 case 0x132: /* rdmsr */
7130 if (s->cpl != 0) {
7131 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7132 } else {
773cdfcc 7133 gen_update_cc_op(s);
872929aa 7134 gen_jmp_im(pc_start - s->cs_base);
0573fbfc 7135 if (b & 2) {
4a7443be 7136 gen_helper_rdmsr(cpu_env);
0573fbfc 7137 } else {
4a7443be 7138 gen_helper_wrmsr(cpu_env);
0573fbfc 7139 }
2c0262af
FB
7140 }
7141 break;
7142 case 0x131: /* rdtsc */
773cdfcc 7143 gen_update_cc_op(s);
ecada8a2 7144 gen_jmp_im(pc_start - s->cs_base);
c5a49c63 7145 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
efade670 7146 gen_io_start();
bd79255d 7147 }
4a7443be 7148 gen_helper_rdtsc(cpu_env);
c5a49c63 7149 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
efade670
PB
7150 gen_io_end();
7151 gen_jmp(s, s->pc - s->cs_base);
7152 }
2c0262af 7153 break;
df01e0fc 7154 case 0x133: /* rdpmc */
773cdfcc 7155 gen_update_cc_op(s);
df01e0fc 7156 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7157 gen_helper_rdpmc(cpu_env);
df01e0fc 7158 break;
023fe10d 7159 case 0x134: /* sysenter */
2436b61a 7160 /* For Intel SYSENTER is valid on 64-bit */
0af10c86 7161 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
14ce26e7 7162 goto illegal_op;
023fe10d
FB
7163 if (!s->pe) {
7164 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7165 } else {
2999a0b2 7166 gen_helper_sysenter(cpu_env);
023fe10d
FB
7167 gen_eob(s);
7168 }
7169 break;
7170 case 0x135: /* sysexit */
2436b61a 7171 /* For Intel SYSEXIT is valid on 64-bit */
0af10c86 7172 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
14ce26e7 7173 goto illegal_op;
023fe10d
FB
7174 if (!s->pe) {
7175 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7176 } else {
ab4e4aec 7177 gen_helper_sysexit(cpu_env, tcg_const_i32(dflag - 1));
023fe10d
FB
7178 gen_eob(s);
7179 }
7180 break;
14ce26e7
FB
7181#ifdef TARGET_X86_64
7182 case 0x105: /* syscall */
7183 /* XXX: is it usable in real mode ? */
728d803b 7184 gen_update_cc_op(s);
14ce26e7 7185 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 7186 gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
410e9814
DE
7187 /* TF handling for the syscall insn is different. The TF bit is checked
7188 after the syscall insn completes. This allows #DB to not be
7189 generated after one has entered CPL0 if TF is set in FMASK. */
7190 gen_eob_worker(s, false, true);
14ce26e7
FB
7191 break;
7192 case 0x107: /* sysret */
7193 if (!s->pe) {
7194 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7195 } else {
ab4e4aec 7196 gen_helper_sysret(cpu_env, tcg_const_i32(dflag - 1));
aba9d61e 7197 /* condition codes are modified only in long mode */
3ca51d07
RH
7198 if (s->lma) {
7199 set_cc_op(s, CC_OP_EFLAGS);
7200 }
c52ab08a
DE
7201 /* TF handling for the sysret insn is different. The TF bit is
7202 checked after the sysret insn completes. This allows #DB to be
7203 generated "as if" the syscall insn in userspace has just
7204 completed. */
7205 gen_eob_worker(s, false, true);
14ce26e7
FB
7206 }
7207 break;
7208#endif
2c0262af 7209 case 0x1a2: /* cpuid */
773cdfcc 7210 gen_update_cc_op(s);
9575cb94 7211 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7212 gen_helper_cpuid(cpu_env);
2c0262af
FB
7213 break;
7214 case 0xf4: /* hlt */
7215 if (s->cpl != 0) {
7216 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7217 } else {
773cdfcc 7218 gen_update_cc_op(s);
94451178 7219 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7220 gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
6cf147aa 7221 s->base.is_jmp = DISAS_NORETURN;
2c0262af
FB
7222 }
7223 break;
7224 case 0x100:
e3af7c78 7225 modrm = x86_ldub_code(env, s);
2c0262af
FB
7226 mod = (modrm >> 6) & 3;
7227 op = (modrm >> 3) & 7;
7228 switch(op) {
7229 case 0: /* sldt */
f115e911
FB
7230 if (!s->pe || s->vm86)
7231 goto illegal_op;
872929aa 7232 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
1d1cc4d0
RH
7233 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
7234 offsetof(CPUX86State, ldt.selector));
ab4e4aec 7235 ot = mod == 3 ? dflag : MO_16;
0af10c86 7236 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
7237 break;
7238 case 2: /* lldt */
f115e911
FB
7239 if (!s->pe || s->vm86)
7240 goto illegal_op;
2c0262af
FB
7241 if (s->cpl != 0) {
7242 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7243 } else {
872929aa 7244 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
4ba9938c 7245 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
1d1cc4d0 7246 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
2999a0b2 7247 gen_helper_lldt(cpu_env, cpu_tmp2_i32);
2c0262af
FB
7248 }
7249 break;
7250 case 1: /* str */
f115e911
FB
7251 if (!s->pe || s->vm86)
7252 goto illegal_op;
872929aa 7253 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
1d1cc4d0
RH
7254 tcg_gen_ld32u_tl(cpu_T0, cpu_env,
7255 offsetof(CPUX86State, tr.selector));
ab4e4aec 7256 ot = mod == 3 ? dflag : MO_16;
0af10c86 7257 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
7258 break;
7259 case 3: /* ltr */
f115e911
FB
7260 if (!s->pe || s->vm86)
7261 goto illegal_op;
2c0262af
FB
7262 if (s->cpl != 0) {
7263 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7264 } else {
872929aa 7265 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
4ba9938c 7266 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
1d1cc4d0 7267 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T0);
2999a0b2 7268 gen_helper_ltr(cpu_env, cpu_tmp2_i32);
2c0262af
FB
7269 }
7270 break;
7271 case 4: /* verr */
7272 case 5: /* verw */
f115e911
FB
7273 if (!s->pe || s->vm86)
7274 goto illegal_op;
4ba9938c 7275 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
773cdfcc 7276 gen_update_cc_op(s);
2999a0b2 7277 if (op == 4) {
1d1cc4d0 7278 gen_helper_verr(cpu_env, cpu_T0);
2999a0b2 7279 } else {
1d1cc4d0 7280 gen_helper_verw(cpu_env, cpu_T0);
2999a0b2 7281 }
3ca51d07 7282 set_cc_op(s, CC_OP_EFLAGS);
f115e911 7283 break;
2c0262af 7284 default:
b9f9c5b4 7285 goto unknown_op;
2c0262af
FB
7286 }
7287 break;
1906b2af 7288
2c0262af 7289 case 0x101:
e3af7c78 7290 modrm = x86_ldub_code(env, s);
1906b2af 7291 switch (modrm) {
880f8486 7292 CASE_MODRM_MEM_OP(0): /* sgdt */
872929aa 7293 gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
4eeb3939 7294 gen_lea_modrm(env, s, modrm);
1d1cc4d0
RH
7295 tcg_gen_ld32u_tl(cpu_T0,
7296 cpu_env, offsetof(CPUX86State, gdt.limit));
7297 gen_op_st_v(s, MO_16, cpu_T0, cpu_A0);
aba9d61e 7298 gen_add_A0_im(s, 2);
1d1cc4d0 7299 tcg_gen_ld_tl(cpu_T0, cpu_env, offsetof(CPUX86State, gdt.base));
ab4e4aec 7300 if (dflag == MO_16) {
1d1cc4d0 7301 tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
f0706f0c 7302 }
1d1cc4d0 7303 gen_op_st_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
2c0262af 7304 break;
1906b2af
RH
7305
7306 case 0xc8: /* monitor */
7307 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) || s->cpl != 0) {
7308 goto illegal_op;
3d7374c5 7309 }
1906b2af
RH
7310 gen_update_cc_op(s);
7311 gen_jmp_im(pc_start - s->cs_base);
7312 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EAX]);
7313 gen_extu(s->aflag, cpu_A0);
7314 gen_add_A0_ds_seg(s);
7315 gen_helper_monitor(cpu_env, cpu_A0);
3d7374c5 7316 break;
1906b2af
RH
7317
7318 case 0xc9: /* mwait */
7319 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) || s->cpl != 0) {
7320 goto illegal_op;
7321 }
7322 gen_update_cc_op(s);
7323 gen_jmp_im(pc_start - s->cs_base);
7324 gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
7325 gen_eob(s);
7326 break;
7327
7328 case 0xca: /* clac */
7329 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP)
7330 || s->cpl != 0) {
7331 goto illegal_op;
7332 }
7333 gen_helper_clac(cpu_env);
7334 gen_jmp_im(s->pc - s->cs_base);
7335 gen_eob(s);
7336 break;
7337
7338 case 0xcb: /* stac */
7339 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP)
7340 || s->cpl != 0) {
7341 goto illegal_op;
7342 }
7343 gen_helper_stac(cpu_env);
7344 gen_jmp_im(s->pc - s->cs_base);
7345 gen_eob(s);
7346 break;
7347
880f8486 7348 CASE_MODRM_MEM_OP(1): /* sidt */
1906b2af
RH
7349 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
7350 gen_lea_modrm(env, s, modrm);
7351 tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State, idt.limit));
7352 gen_op_st_v(s, MO_16, cpu_T0, cpu_A0);
7353 gen_add_A0_im(s, 2);
7354 tcg_gen_ld_tl(cpu_T0, cpu_env, offsetof(CPUX86State, idt.base));
7355 if (dflag == MO_16) {
7356 tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
7357 }
7358 gen_op_st_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
7359 break;
7360
19dc85db
RH
7361 case 0xd0: /* xgetbv */
7362 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
7363 || (s->prefix & (PREFIX_LOCK | PREFIX_DATA
7364 | PREFIX_REPZ | PREFIX_REPNZ))) {
7365 goto illegal_op;
7366 }
7367 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
7368 gen_helper_xgetbv(cpu_tmp1_i64, cpu_env, cpu_tmp2_i32);
7369 tcg_gen_extr_i64_tl(cpu_regs[R_EAX], cpu_regs[R_EDX], cpu_tmp1_i64);
7370 break;
7371
7372 case 0xd1: /* xsetbv */
7373 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
7374 || (s->prefix & (PREFIX_LOCK | PREFIX_DATA
7375 | PREFIX_REPZ | PREFIX_REPNZ))) {
7376 goto illegal_op;
7377 }
7378 if (s->cpl != 0) {
7379 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7380 break;
7381 }
7382 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
7383 cpu_regs[R_EDX]);
7384 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
7385 gen_helper_xsetbv(cpu_env, cpu_tmp2_i32, cpu_tmp1_i64);
7386 /* End TB because translation flags may change. */
ba03584f 7387 gen_jmp_im(s->pc - s->cs_base);
19dc85db
RH
7388 gen_eob(s);
7389 break;
7390
1906b2af
RH
7391 case 0xd8: /* VMRUN */
7392 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7393 goto illegal_op;
7394 }
7395 if (s->cpl != 0) {
2c0262af 7396 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
1906b2af 7397 break;
2c0262af 7398 }
1906b2af
RH
7399 gen_update_cc_op(s);
7400 gen_jmp_im(pc_start - s->cs_base);
7401 gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag - 1),
7402 tcg_const_i32(s->pc - pc_start));
7403 tcg_gen_exit_tb(0);
6cf147aa 7404 s->base.is_jmp = DISAS_NORETURN;
2c0262af 7405 break;
1906b2af
RH
7406
7407 case 0xd9: /* VMMCALL */
7408 if (!(s->flags & HF_SVME_MASK)) {
7409 goto illegal_op;
7410 }
7411 gen_update_cc_op(s);
7412 gen_jmp_im(pc_start - s->cs_base);
7413 gen_helper_vmmcall(cpu_env);
7414 break;
7415
7416 case 0xda: /* VMLOAD */
7417 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7418 goto illegal_op;
7419 }
7420 if (s->cpl != 0) {
7421 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7422 break;
7423 }
7424 gen_update_cc_op(s);
7425 gen_jmp_im(pc_start - s->cs_base);
7426 gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag - 1));
7427 break;
7428
7429 case 0xdb: /* VMSAVE */
7430 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7431 goto illegal_op;
7432 }
7433 if (s->cpl != 0) {
7434 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7435 break;
7436 }
7437 gen_update_cc_op(s);
7438 gen_jmp_im(pc_start - s->cs_base);
7439 gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag - 1));
7440 break;
7441
7442 case 0xdc: /* STGI */
7443 if ((!(s->flags & HF_SVME_MASK)
7444 && !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT))
7445 || !s->pe) {
7446 goto illegal_op;
7447 }
7448 if (s->cpl != 0) {
7449 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7450 break;
7451 }
7452 gen_update_cc_op(s);
7453 gen_jmp_im(pc_start - s->cs_base);
7454 gen_helper_stgi(cpu_env);
7455 break;
7456
7457 case 0xdd: /* CLGI */
7458 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7459 goto illegal_op;
7460 }
7461 if (s->cpl != 0) {
7462 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7463 break;
7464 }
7465 gen_update_cc_op(s);
7466 gen_jmp_im(pc_start - s->cs_base);
7467 gen_helper_clgi(cpu_env);
7468 break;
7469
7470 case 0xde: /* SKINIT */
7471 if ((!(s->flags & HF_SVME_MASK)
7472 && !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT))
7473 || !s->pe) {
7474 goto illegal_op;
7475 }
7476 gen_update_cc_op(s);
7477 gen_jmp_im(pc_start - s->cs_base);
7478 gen_helper_skinit(cpu_env);
7479 break;
7480
7481 case 0xdf: /* INVLPGA */
7482 if (!(s->flags & HF_SVME_MASK) || !s->pe) {
7483 goto illegal_op;
7484 }
7485 if (s->cpl != 0) {
7486 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7487 break;
7488 }
7489 gen_update_cc_op(s);
7490 gen_jmp_im(pc_start - s->cs_base);
7491 gen_helper_invlpga(cpu_env, tcg_const_i32(s->aflag - 1));
7492 break;
7493
880f8486 7494 CASE_MODRM_MEM_OP(2): /* lgdt */
1906b2af
RH
7495 if (s->cpl != 0) {
7496 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7497 break;
7498 }
7499 gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_WRITE);
7500 gen_lea_modrm(env, s, modrm);
7501 gen_op_ld_v(s, MO_16, cpu_T1, cpu_A0);
7502 gen_add_A0_im(s, 2);
7503 gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
7504 if (dflag == MO_16) {
7505 tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
7506 }
7507 tcg_gen_st_tl(cpu_T0, cpu_env, offsetof(CPUX86State, gdt.base));
7508 tcg_gen_st32_tl(cpu_T1, cpu_env, offsetof(CPUX86State, gdt.limit));
7509 break;
7510
880f8486 7511 CASE_MODRM_MEM_OP(3): /* lidt */
1906b2af
RH
7512 if (s->cpl != 0) {
7513 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7514 break;
7515 }
7516 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_WRITE);
7517 gen_lea_modrm(env, s, modrm);
7518 gen_op_ld_v(s, MO_16, cpu_T1, cpu_A0);
7519 gen_add_A0_im(s, 2);
7520 gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T0, cpu_A0);
7521 if (dflag == MO_16) {
7522 tcg_gen_andi_tl(cpu_T0, cpu_T0, 0xffffff);
7523 }
7524 tcg_gen_st_tl(cpu_T0, cpu_env, offsetof(CPUX86State, idt.base));
7525 tcg_gen_st32_tl(cpu_T1, cpu_env, offsetof(CPUX86State, idt.limit));
7526 break;
7527
880f8486 7528 CASE_MODRM_OP(4): /* smsw */
872929aa 7529 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
a657f79e
RH
7530 tcg_gen_ld_tl(cpu_T0, cpu_env, offsetof(CPUX86State, cr[0]));
7531 if (CODE64(s)) {
7532 mod = (modrm >> 6) & 3;
7533 ot = (mod != 3 ? MO_16 : s->dflag);
7534 } else {
7535 ot = MO_16;
7536 }
7537 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af 7538 break;
0f70ed47
PB
7539 case 0xee: /* rdpkru */
7540 if (prefixes & PREFIX_LOCK) {
7541 goto illegal_op;
7542 }
7543 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
7544 gen_helper_rdpkru(cpu_tmp1_i64, cpu_env, cpu_tmp2_i32);
7545 tcg_gen_extr_i64_tl(cpu_regs[R_EAX], cpu_regs[R_EDX], cpu_tmp1_i64);
7546 break;
7547 case 0xef: /* wrpkru */
7548 if (prefixes & PREFIX_LOCK) {
7549 goto illegal_op;
7550 }
7551 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
7552 cpu_regs[R_EDX]);
7553 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_ECX]);
7554 gen_helper_wrpkru(cpu_env, cpu_tmp2_i32, cpu_tmp1_i64);
7555 break;
880f8486 7556 CASE_MODRM_OP(6): /* lmsw */
2c0262af
FB
7557 if (s->cpl != 0) {
7558 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
1906b2af 7559 break;
2c0262af 7560 }
1906b2af
RH
7561 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
7562 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
7563 gen_helper_lmsw(cpu_env, cpu_T0);
7564 gen_jmp_im(s->pc - s->cs_base);
7565 gen_eob(s);
2c0262af 7566 break;
1906b2af 7567
880f8486 7568 CASE_MODRM_MEM_OP(7): /* invlpg */
1906b2af
RH
7569 if (s->cpl != 0) {
7570 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7571 break;
7572 }
7573 gen_update_cc_op(s);
7574 gen_jmp_im(pc_start - s->cs_base);
7575 gen_lea_modrm(env, s, modrm);
7576 gen_helper_invlpg(cpu_env, cpu_A0);
7577 gen_jmp_im(s->pc - s->cs_base);
7578 gen_eob(s);
7579 break;
7580
7581 case 0xf8: /* swapgs */
7582#ifdef TARGET_X86_64
7583 if (CODE64(s)) {
1b050077
AP
7584 if (s->cpl != 0) {
7585 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7586 } else {
1906b2af
RH
7587 tcg_gen_mov_tl(cpu_T0, cpu_seg_base[R_GS]);
7588 tcg_gen_ld_tl(cpu_seg_base[R_GS], cpu_env,
7589 offsetof(CPUX86State, kernelgsbase));
7590 tcg_gen_st_tl(cpu_T0, cpu_env,
7591 offsetof(CPUX86State, kernelgsbase));
1b050077 7592 }
1906b2af
RH
7593 break;
7594 }
3558f805 7595#endif
1906b2af
RH
7596 goto illegal_op;
7597
7598 case 0xf9: /* rdtscp */
7599 if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP)) {
7600 goto illegal_op;
7601 }
7602 gen_update_cc_op(s);
7603 gen_jmp_im(pc_start - s->cs_base);
c5a49c63 7604 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
1906b2af
RH
7605 gen_io_start();
7606 }
7607 gen_helper_rdtscp(cpu_env);
c5a49c63 7608 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
1906b2af
RH
7609 gen_io_end();
7610 gen_jmp(s, s->pc - s->cs_base);
2c0262af
FB
7611 }
7612 break;
1906b2af 7613
2c0262af 7614 default:
b9f9c5b4 7615 goto unknown_op;
2c0262af
FB
7616 }
7617 break;
1906b2af 7618
3415a4dd
FB
7619 case 0x108: /* invd */
7620 case 0x109: /* wbinvd */
7621 if (s->cpl != 0) {
7622 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7623 } else {
872929aa 7624 gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
3415a4dd
FB
7625 /* nothing to do */
7626 }
7627 break;
14ce26e7
FB
7628 case 0x63: /* arpl or movslS (x86_64) */
7629#ifdef TARGET_X86_64
7630 if (CODE64(s)) {
7631 int d_ot;
7632 /* d_ot is the size of destination */
ab4e4aec 7633 d_ot = dflag;
14ce26e7 7634
e3af7c78 7635 modrm = x86_ldub_code(env, s);
14ce26e7
FB
7636 reg = ((modrm >> 3) & 7) | rex_r;
7637 mod = (modrm >> 6) & 3;
7638 rm = (modrm & 7) | REX_B(s);
3b46e624 7639
14ce26e7 7640 if (mod == 3) {
1d1cc4d0 7641 gen_op_mov_v_reg(MO_32, cpu_T0, rm);
14ce26e7 7642 /* sign extend */
4ba9938c 7643 if (d_ot == MO_64) {
1d1cc4d0 7644 tcg_gen_ext32s_tl(cpu_T0, cpu_T0);
4ba9938c 7645 }
1d1cc4d0 7646 gen_op_mov_reg_v(d_ot, reg, cpu_T0);
14ce26e7 7647 } else {
4eeb3939 7648 gen_lea_modrm(env, s, modrm);
1d1cc4d0
RH
7649 gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T0, cpu_A0);
7650 gen_op_mov_reg_v(d_ot, reg, cpu_T0);
14ce26e7 7651 }
5fafdf24 7652 } else
14ce26e7
FB
7653#endif
7654 {
42a268c2 7655 TCGLabel *label1;
49d9fdcc 7656 TCGv t0, t1, t2, a0;
1e4840bf 7657
14ce26e7
FB
7658 if (!s->pe || s->vm86)
7659 goto illegal_op;
a7812ae4
PB
7660 t0 = tcg_temp_local_new();
7661 t1 = tcg_temp_local_new();
7662 t2 = tcg_temp_local_new();
4ba9938c 7663 ot = MO_16;
e3af7c78 7664 modrm = x86_ldub_code(env, s);
14ce26e7
FB
7665 reg = (modrm >> 3) & 7;
7666 mod = (modrm >> 6) & 3;
7667 rm = modrm & 7;
7668 if (mod != 3) {
4eeb3939 7669 gen_lea_modrm(env, s, modrm);
323d1876 7670 gen_op_ld_v(s, ot, t0, cpu_A0);
49d9fdcc
LD
7671 a0 = tcg_temp_local_new();
7672 tcg_gen_mov_tl(a0, cpu_A0);
14ce26e7 7673 } else {
1e4840bf 7674 gen_op_mov_v_reg(ot, t0, rm);
f764718d 7675 a0 = NULL;
14ce26e7 7676 }
1e4840bf
FB
7677 gen_op_mov_v_reg(ot, t1, reg);
7678 tcg_gen_andi_tl(cpu_tmp0, t0, 3);
7679 tcg_gen_andi_tl(t1, t1, 3);
7680 tcg_gen_movi_tl(t2, 0);
3bd7da9e 7681 label1 = gen_new_label();
1e4840bf
FB
7682 tcg_gen_brcond_tl(TCG_COND_GE, cpu_tmp0, t1, label1);
7683 tcg_gen_andi_tl(t0, t0, ~3);
7684 tcg_gen_or_tl(t0, t0, t1);
7685 tcg_gen_movi_tl(t2, CC_Z);
3bd7da9e 7686 gen_set_label(label1);
14ce26e7 7687 if (mod != 3) {
323d1876 7688 gen_op_st_v(s, ot, t0, a0);
49d9fdcc
LD
7689 tcg_temp_free(a0);
7690 } else {
1e4840bf 7691 gen_op_mov_reg_v(ot, rm, t0);
14ce26e7 7692 }
d229edce 7693 gen_compute_eflags(s);
3bd7da9e 7694 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
1e4840bf 7695 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
1e4840bf
FB
7696 tcg_temp_free(t0);
7697 tcg_temp_free(t1);
7698 tcg_temp_free(t2);
f115e911 7699 }
f115e911 7700 break;
2c0262af
FB
7701 case 0x102: /* lar */
7702 case 0x103: /* lsl */
cec6843e 7703 {
42a268c2 7704 TCGLabel *label1;
1e4840bf 7705 TCGv t0;
cec6843e
FB
7706 if (!s->pe || s->vm86)
7707 goto illegal_op;
ab4e4aec 7708 ot = dflag != MO_16 ? MO_32 : MO_16;
e3af7c78 7709 modrm = x86_ldub_code(env, s);
cec6843e 7710 reg = ((modrm >> 3) & 7) | rex_r;
4ba9938c 7711 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
a7812ae4 7712 t0 = tcg_temp_local_new();
773cdfcc 7713 gen_update_cc_op(s);
2999a0b2 7714 if (b == 0x102) {
1d1cc4d0 7715 gen_helper_lar(t0, cpu_env, cpu_T0);
2999a0b2 7716 } else {
1d1cc4d0 7717 gen_helper_lsl(t0, cpu_env, cpu_T0);
2999a0b2 7718 }
cec6843e
FB
7719 tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
7720 label1 = gen_new_label();
cb63669a 7721 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
1e4840bf 7722 gen_op_mov_reg_v(ot, reg, t0);
cec6843e 7723 gen_set_label(label1);
3ca51d07 7724 set_cc_op(s, CC_OP_EFLAGS);
1e4840bf 7725 tcg_temp_free(t0);
cec6843e 7726 }
2c0262af
FB
7727 break;
7728 case 0x118:
e3af7c78 7729 modrm = x86_ldub_code(env, s);
2c0262af
FB
7730 mod = (modrm >> 6) & 3;
7731 op = (modrm >> 3) & 7;
7732 switch(op) {
7733 case 0: /* prefetchnta */
7734 case 1: /* prefetchnt0 */
7735 case 2: /* prefetchnt0 */
7736 case 3: /* prefetchnt0 */
7737 if (mod == 3)
7738 goto illegal_op;
26317698 7739 gen_nop_modrm(env, s, modrm);
2c0262af
FB
7740 /* nothing more to do */
7741 break;
e17a36ce 7742 default: /* nop (multi byte) */
0af10c86 7743 gen_nop_modrm(env, s, modrm);
e17a36ce 7744 break;
2c0262af
FB
7745 }
7746 break;
62b58ba5 7747 case 0x11a:
e3af7c78 7748 modrm = x86_ldub_code(env, s);
62b58ba5
RH
7749 if (s->flags & HF_MPX_EN_MASK) {
7750 mod = (modrm >> 6) & 3;
7751 reg = ((modrm >> 3) & 7) | rex_r;
523e28d7
RH
7752 if (prefixes & PREFIX_REPZ) {
7753 /* bndcl */
7754 if (reg >= 4
7755 || (prefixes & PREFIX_LOCK)
7756 || s->aflag == MO_16) {
7757 goto illegal_op;
7758 }
7759 gen_bndck(env, s, modrm, TCG_COND_LTU, cpu_bndl[reg]);
7760 } else if (prefixes & PREFIX_REPNZ) {
7761 /* bndcu */
7762 if (reg >= 4
7763 || (prefixes & PREFIX_LOCK)
7764 || s->aflag == MO_16) {
7765 goto illegal_op;
7766 }
7767 TCGv_i64 notu = tcg_temp_new_i64();
7768 tcg_gen_not_i64(notu, cpu_bndu[reg]);
7769 gen_bndck(env, s, modrm, TCG_COND_GTU, notu);
7770 tcg_temp_free_i64(notu);
7771 } else if (prefixes & PREFIX_DATA) {
62b58ba5
RH
7772 /* bndmov -- from reg/mem */
7773 if (reg >= 4 || s->aflag == MO_16) {
7774 goto illegal_op;
7775 }
7776 if (mod == 3) {
7777 int reg2 = (modrm & 7) | REX_B(s);
7778 if (reg2 >= 4 || (prefixes & PREFIX_LOCK)) {
7779 goto illegal_op;
7780 }
7781 if (s->flags & HF_MPX_IU_MASK) {
7782 tcg_gen_mov_i64(cpu_bndl[reg], cpu_bndl[reg2]);
7783 tcg_gen_mov_i64(cpu_bndu[reg], cpu_bndu[reg2]);
7784 }
7785 } else {
7786 gen_lea_modrm(env, s, modrm);
7787 if (CODE64(s)) {
7788 tcg_gen_qemu_ld_i64(cpu_bndl[reg], cpu_A0,
7789 s->mem_index, MO_LEQ);
7790 tcg_gen_addi_tl(cpu_A0, cpu_A0, 8);
7791 tcg_gen_qemu_ld_i64(cpu_bndu[reg], cpu_A0,
7792 s->mem_index, MO_LEQ);
7793 } else {
7794 tcg_gen_qemu_ld_i64(cpu_bndl[reg], cpu_A0,
7795 s->mem_index, MO_LEUL);
7796 tcg_gen_addi_tl(cpu_A0, cpu_A0, 4);
7797 tcg_gen_qemu_ld_i64(cpu_bndu[reg], cpu_A0,
7798 s->mem_index, MO_LEUL);
7799 }
7800 /* bnd registers are now in-use */
7801 gen_set_hflag(s, HF_MPX_IU_MASK);
7802 }
bdd87b3b
RH
7803 } else if (mod != 3) {
7804 /* bndldx */
7805 AddressParts a = gen_lea_modrm_0(env, s, modrm);
7806 if (reg >= 4
7807 || (prefixes & PREFIX_LOCK)
7808 || s->aflag == MO_16
7809 || a.base < -1) {
7810 goto illegal_op;
7811 }
7812 if (a.base >= 0) {
7813 tcg_gen_addi_tl(cpu_A0, cpu_regs[a.base], a.disp);
7814 } else {
7815 tcg_gen_movi_tl(cpu_A0, 0);
7816 }
7817 gen_lea_v_seg(s, s->aflag, cpu_A0, a.def_seg, s->override);
7818 if (a.index >= 0) {
7819 tcg_gen_mov_tl(cpu_T0, cpu_regs[a.index]);
7820 } else {
7821 tcg_gen_movi_tl(cpu_T0, 0);
7822 }
7823 if (CODE64(s)) {
7824 gen_helper_bndldx64(cpu_bndl[reg], cpu_env, cpu_A0, cpu_T0);
7825 tcg_gen_ld_i64(cpu_bndu[reg], cpu_env,
7826 offsetof(CPUX86State, mmx_t0.MMX_Q(0)));
7827 } else {
7828 gen_helper_bndldx32(cpu_bndu[reg], cpu_env, cpu_A0, cpu_T0);
7829 tcg_gen_ext32u_i64(cpu_bndl[reg], cpu_bndu[reg]);
7830 tcg_gen_shri_i64(cpu_bndu[reg], cpu_bndu[reg], 32);
7831 }
7832 gen_set_hflag(s, HF_MPX_IU_MASK);
62b58ba5
RH
7833 }
7834 }
7835 gen_nop_modrm(env, s, modrm);
7836 break;
149b427b 7837 case 0x11b:
e3af7c78 7838 modrm = x86_ldub_code(env, s);
149b427b
RH
7839 if (s->flags & HF_MPX_EN_MASK) {
7840 mod = (modrm >> 6) & 3;
7841 reg = ((modrm >> 3) & 7) | rex_r;
7842 if (mod != 3 && (prefixes & PREFIX_REPZ)) {
7843 /* bndmk */
7844 if (reg >= 4
7845 || (prefixes & PREFIX_LOCK)
7846 || s->aflag == MO_16) {
7847 goto illegal_op;
7848 }
7849 AddressParts a = gen_lea_modrm_0(env, s, modrm);
7850 if (a.base >= 0) {
7851 tcg_gen_extu_tl_i64(cpu_bndl[reg], cpu_regs[a.base]);
7852 if (!CODE64(s)) {
7853 tcg_gen_ext32u_i64(cpu_bndl[reg], cpu_bndl[reg]);
7854 }
7855 } else if (a.base == -1) {
7856 /* no base register has lower bound of 0 */
7857 tcg_gen_movi_i64(cpu_bndl[reg], 0);
7858 } else {
7859 /* rip-relative generates #ud */
7860 goto illegal_op;
7861 }
7862 tcg_gen_not_tl(cpu_A0, gen_lea_modrm_1(a));
7863 if (!CODE64(s)) {
7864 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
7865 }
7866 tcg_gen_extu_tl_i64(cpu_bndu[reg], cpu_A0);
7867 /* bnd registers are now in-use */
7868 gen_set_hflag(s, HF_MPX_IU_MASK);
7869 break;
523e28d7
RH
7870 } else if (prefixes & PREFIX_REPNZ) {
7871 /* bndcn */
7872 if (reg >= 4
7873 || (prefixes & PREFIX_LOCK)
7874 || s->aflag == MO_16) {
7875 goto illegal_op;
7876 }
7877 gen_bndck(env, s, modrm, TCG_COND_GTU, cpu_bndu[reg]);
62b58ba5
RH
7878 } else if (prefixes & PREFIX_DATA) {
7879 /* bndmov -- to reg/mem */
7880 if (reg >= 4 || s->aflag == MO_16) {
7881 goto illegal_op;
7882 }
7883 if (mod == 3) {
7884 int reg2 = (modrm & 7) | REX_B(s);
7885 if (reg2 >= 4 || (prefixes & PREFIX_LOCK)) {
7886 goto illegal_op;
7887 }
7888 if (s->flags & HF_MPX_IU_MASK) {
7889 tcg_gen_mov_i64(cpu_bndl[reg2], cpu_bndl[reg]);
7890 tcg_gen_mov_i64(cpu_bndu[reg2], cpu_bndu[reg]);
7891 }
7892 } else {
7893 gen_lea_modrm(env, s, modrm);
7894 if (CODE64(s)) {
7895 tcg_gen_qemu_st_i64(cpu_bndl[reg], cpu_A0,
7896 s->mem_index, MO_LEQ);
7897 tcg_gen_addi_tl(cpu_A0, cpu_A0, 8);
7898 tcg_gen_qemu_st_i64(cpu_bndu[reg], cpu_A0,
7899 s->mem_index, MO_LEQ);
7900 } else {
7901 tcg_gen_qemu_st_i64(cpu_bndl[reg], cpu_A0,
7902 s->mem_index, MO_LEUL);
7903 tcg_gen_addi_tl(cpu_A0, cpu_A0, 4);
7904 tcg_gen_qemu_st_i64(cpu_bndu[reg], cpu_A0,
7905 s->mem_index, MO_LEUL);
7906 }
7907 }
bdd87b3b
RH
7908 } else if (mod != 3) {
7909 /* bndstx */
7910 AddressParts a = gen_lea_modrm_0(env, s, modrm);
7911 if (reg >= 4
7912 || (prefixes & PREFIX_LOCK)
7913 || s->aflag == MO_16
7914 || a.base < -1) {
7915 goto illegal_op;
7916 }
7917 if (a.base >= 0) {
7918 tcg_gen_addi_tl(cpu_A0, cpu_regs[a.base], a.disp);
7919 } else {
7920 tcg_gen_movi_tl(cpu_A0, 0);
7921 }
7922 gen_lea_v_seg(s, s->aflag, cpu_A0, a.def_seg, s->override);
7923 if (a.index >= 0) {
7924 tcg_gen_mov_tl(cpu_T0, cpu_regs[a.index]);
7925 } else {
7926 tcg_gen_movi_tl(cpu_T0, 0);
7927 }
7928 if (CODE64(s)) {
7929 gen_helper_bndstx64(cpu_env, cpu_A0, cpu_T0,
7930 cpu_bndl[reg], cpu_bndu[reg]);
7931 } else {
7932 gen_helper_bndstx32(cpu_env, cpu_A0, cpu_T0,
7933 cpu_bndl[reg], cpu_bndu[reg]);
7934 }
149b427b
RH
7935 }
7936 }
7937 gen_nop_modrm(env, s, modrm);
7938 break;
62b58ba5 7939 case 0x119: case 0x11c ... 0x11f: /* nop (multi byte) */
e3af7c78 7940 modrm = x86_ldub_code(env, s);
0af10c86 7941 gen_nop_modrm(env, s, modrm);
e17a36ce 7942 break;
2c0262af
FB
7943 case 0x120: /* mov reg, crN */
7944 case 0x122: /* mov crN, reg */
7945 if (s->cpl != 0) {
7946 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7947 } else {
e3af7c78 7948 modrm = x86_ldub_code(env, s);
5c73b757
MO
7949 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7950 * AMD documentation (24594.pdf) and testing of
7951 * intel 386 and 486 processors all show that the mod bits
7952 * are assumed to be 1's, regardless of actual values.
7953 */
14ce26e7
FB
7954 rm = (modrm & 7) | REX_B(s);
7955 reg = ((modrm >> 3) & 7) | rex_r;
7956 if (CODE64(s))
4ba9938c 7957 ot = MO_64;
14ce26e7 7958 else
4ba9938c 7959 ot = MO_32;
ccd59d09
AP
7960 if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
7961 (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
7962 reg = 8;
7963 }
2c0262af
FB
7964 switch(reg) {
7965 case 0:
7966 case 2:
7967 case 3:
7968 case 4:
9230e66e 7969 case 8:
773cdfcc 7970 gen_update_cc_op(s);
872929aa 7971 gen_jmp_im(pc_start - s->cs_base);
2c0262af 7972 if (b & 2) {
c5a49c63 7973 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
5b003a40
MA
7974 gen_io_start();
7975 }
1d1cc4d0 7976 gen_op_mov_v_reg(ot, cpu_T0, rm);
4a7443be 7977 gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
1d1cc4d0 7978 cpu_T0);
c5a49c63 7979 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
5b003a40
MA
7980 gen_io_end();
7981 }
14ce26e7 7982 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
7983 gen_eob(s);
7984 } else {
c5a49c63 7985 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
5b003a40
MA
7986 gen_io_start();
7987 }
1d1cc4d0
RH
7988 gen_helper_read_crN(cpu_T0, cpu_env, tcg_const_i32(reg));
7989 gen_op_mov_reg_v(ot, rm, cpu_T0);
c5a49c63 7990 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
5b003a40
MA
7991 gen_io_end();
7992 }
2c0262af
FB
7993 }
7994 break;
7995 default:
b9f9c5b4 7996 goto unknown_op;
2c0262af
FB
7997 }
7998 }
7999 break;
8000 case 0x121: /* mov reg, drN */
8001 case 0x123: /* mov drN, reg */
8002 if (s->cpl != 0) {
8003 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
8004 } else {
e3af7c78 8005 modrm = x86_ldub_code(env, s);
5c73b757
MO
8006 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
8007 * AMD documentation (24594.pdf) and testing of
8008 * intel 386 and 486 processors all show that the mod bits
8009 * are assumed to be 1's, regardless of actual values.
8010 */
14ce26e7
FB
8011 rm = (modrm & 7) | REX_B(s);
8012 reg = ((modrm >> 3) & 7) | rex_r;
8013 if (CODE64(s))
4ba9938c 8014 ot = MO_64;
14ce26e7 8015 else
4ba9938c 8016 ot = MO_32;
d0052339 8017 if (reg >= 8) {
2c0262af 8018 goto illegal_op;
d0052339 8019 }
2c0262af 8020 if (b & 2) {
0573fbfc 8021 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
1d1cc4d0 8022 gen_op_mov_v_reg(ot, cpu_T0, rm);
d0052339 8023 tcg_gen_movi_i32(cpu_tmp2_i32, reg);
1d1cc4d0 8024 gen_helper_set_dr(cpu_env, cpu_tmp2_i32, cpu_T0);
14ce26e7 8025 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
8026 gen_eob(s);
8027 } else {
0573fbfc 8028 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
d0052339 8029 tcg_gen_movi_i32(cpu_tmp2_i32, reg);
1d1cc4d0
RH
8030 gen_helper_get_dr(cpu_T0, cpu_env, cpu_tmp2_i32);
8031 gen_op_mov_reg_v(ot, rm, cpu_T0);
2c0262af
FB
8032 }
8033 }
8034 break;
8035 case 0x106: /* clts */
8036 if (s->cpl != 0) {
8037 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
8038 } else {
0573fbfc 8039 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
f0967a1a 8040 gen_helper_clts(cpu_env);
7eee2a50 8041 /* abort block because static cpu state changed */
14ce26e7 8042 gen_jmp_im(s->pc - s->cs_base);
7eee2a50 8043 gen_eob(s);
2c0262af
FB
8044 }
8045 break;
222a3336 8046 /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
664e0f19
FB
8047 case 0x1c3: /* MOVNTI reg, mem */
8048 if (!(s->cpuid_features & CPUID_SSE2))
14ce26e7 8049 goto illegal_op;
ab4e4aec 8050 ot = mo_64_32(dflag);
e3af7c78 8051 modrm = x86_ldub_code(env, s);
664e0f19
FB
8052 mod = (modrm >> 6) & 3;
8053 if (mod == 3)
8054 goto illegal_op;
8055 reg = ((modrm >> 3) & 7) | rex_r;
8056 /* generate a generic store */
0af10c86 8057 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
14ce26e7 8058 break;
664e0f19 8059 case 0x1ae:
e3af7c78 8060 modrm = x86_ldub_code(env, s);
121f3157 8061 switch (modrm) {
880f8486 8062 CASE_MODRM_MEM_OP(0): /* fxsave */
121f3157
RH
8063 if (!(s->cpuid_features & CPUID_FXSR)
8064 || (prefixes & PREFIX_LOCK)) {
14ce26e7 8065 goto illegal_op;
121f3157 8066 }
09d85fb8 8067 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
0fd14b72
FB
8068 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
8069 break;
8070 }
4eeb3939 8071 gen_lea_modrm(env, s, modrm);
64dbaff0 8072 gen_helper_fxsave(cpu_env, cpu_A0);
664e0f19 8073 break;
121f3157 8074
880f8486 8075 CASE_MODRM_MEM_OP(1): /* fxrstor */
121f3157
RH
8076 if (!(s->cpuid_features & CPUID_FXSR)
8077 || (prefixes & PREFIX_LOCK)) {
14ce26e7 8078 goto illegal_op;
121f3157 8079 }
09d85fb8 8080 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
0fd14b72
FB
8081 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
8082 break;
8083 }
4eeb3939 8084 gen_lea_modrm(env, s, modrm);
64dbaff0 8085 gen_helper_fxrstor(cpu_env, cpu_A0);
664e0f19 8086 break;
121f3157 8087
880f8486 8088 CASE_MODRM_MEM_OP(2): /* ldmxcsr */
121f3157
RH
8089 if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK)) {
8090 goto illegal_op;
8091 }
664e0f19
FB
8092 if (s->flags & HF_TS_MASK) {
8093 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
8094 break;
14ce26e7 8095 }
4eeb3939 8096 gen_lea_modrm(env, s, modrm);
121f3157
RH
8097 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0, s->mem_index, MO_LEUL);
8098 gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
664e0f19 8099 break;
121f3157 8100
880f8486 8101 CASE_MODRM_MEM_OP(3): /* stmxcsr */
121f3157 8102 if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK)) {
664e0f19 8103 goto illegal_op;
121f3157
RH
8104 }
8105 if (s->flags & HF_TS_MASK) {
8106 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
8107 break;
8108 }
8109 gen_lea_modrm(env, s, modrm);
8110 tcg_gen_ld32u_tl(cpu_T0, cpu_env, offsetof(CPUX86State, mxcsr));
8111 gen_op_st_v(s, MO_32, cpu_T0, cpu_A0);
664e0f19 8112 break;
121f3157 8113
880f8486 8114 CASE_MODRM_MEM_OP(4): /* xsave */
19dc85db
RH
8115 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
8116 || (prefixes & (PREFIX_LOCK | PREFIX_DATA
8117 | PREFIX_REPZ | PREFIX_REPNZ))) {
8118 goto illegal_op;
8119 }
8120 gen_lea_modrm(env, s, modrm);
8121 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
8122 cpu_regs[R_EDX]);
8123 gen_helper_xsave(cpu_env, cpu_A0, cpu_tmp1_i64);
8124 break;
8125
880f8486 8126 CASE_MODRM_MEM_OP(5): /* xrstor */
19dc85db
RH
8127 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
8128 || (prefixes & (PREFIX_LOCK | PREFIX_DATA
8129 | PREFIX_REPZ | PREFIX_REPNZ))) {
8130 goto illegal_op;
8131 }
8132 gen_lea_modrm(env, s, modrm);
8133 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
8134 cpu_regs[R_EDX]);
8135 gen_helper_xrstor(cpu_env, cpu_A0, cpu_tmp1_i64);
f4f1110e
RH
8136 /* XRSTOR is how MPX is enabled, which changes how
8137 we translate. Thus we need to end the TB. */
8138 gen_update_cc_op(s);
8139 gen_jmp_im(s->pc - s->cs_base);
8140 gen_eob(s);
19dc85db
RH
8141 break;
8142
880f8486 8143 CASE_MODRM_MEM_OP(6): /* xsaveopt / clwb */
121f3157
RH
8144 if (prefixes & PREFIX_LOCK) {
8145 goto illegal_op;
8146 }
8147 if (prefixes & PREFIX_DATA) {
5e1fac2d 8148 /* clwb */
121f3157 8149 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_CLWB)) {
5e1fac2d 8150 goto illegal_op;
121f3157 8151 }
5e1fac2d 8152 gen_nop_modrm(env, s, modrm);
c9cfe8f9
RH
8153 } else {
8154 /* xsaveopt */
8155 if ((s->cpuid_ext_features & CPUID_EXT_XSAVE) == 0
8156 || (s->cpuid_xsave_features & CPUID_XSAVE_XSAVEOPT) == 0
8157 || (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))) {
8158 goto illegal_op;
8159 }
8160 gen_lea_modrm(env, s, modrm);
8161 tcg_gen_concat_tl_i64(cpu_tmp1_i64, cpu_regs[R_EAX],
8162 cpu_regs[R_EDX]);
8163 gen_helper_xsaveopt(cpu_env, cpu_A0, cpu_tmp1_i64);
121f3157 8164 }
c9cfe8f9 8165 break;
121f3157 8166
880f8486 8167 CASE_MODRM_MEM_OP(7): /* clflush / clflushopt */
121f3157
RH
8168 if (prefixes & PREFIX_LOCK) {
8169 goto illegal_op;
8170 }
8171 if (prefixes & PREFIX_DATA) {
8172 /* clflushopt */
8173 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_CLFLUSHOPT)) {
8174 goto illegal_op;
8175 }
5e1fac2d 8176 } else {
121f3157
RH
8177 /* clflush */
8178 if ((s->prefix & (PREFIX_REPZ | PREFIX_REPNZ))
8179 || !(s->cpuid_features & CPUID_CLFLUSH)) {
5e1fac2d 8180 goto illegal_op;
121f3157 8181 }
5e1fac2d 8182 }
121f3157 8183 gen_nop_modrm(env, s, modrm);
5e1fac2d 8184 break;
121f3157 8185
07929f2a 8186 case 0xc0 ... 0xc7: /* rdfsbase (f3 0f ae /0) */
e0dd5fd4 8187 case 0xc8 ... 0xcf: /* rdgsbase (f3 0f ae /1) */
07929f2a 8188 case 0xd0 ... 0xd7: /* wrfsbase (f3 0f ae /2) */
e0dd5fd4 8189 case 0xd8 ... 0xdf: /* wrgsbase (f3 0f ae /3) */
07929f2a
RH
8190 if (CODE64(s)
8191 && (prefixes & PREFIX_REPZ)
8192 && !(prefixes & PREFIX_LOCK)
8193 && (s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_FSGSBASE)) {
8194 TCGv base, treg, src, dst;
8195
8196 /* Preserve hflags bits by testing CR4 at runtime. */
8197 tcg_gen_movi_i32(cpu_tmp2_i32, CR4_FSGSBASE_MASK);
8198 gen_helper_cr4_testbit(cpu_env, cpu_tmp2_i32);
8199
8200 base = cpu_seg_base[modrm & 8 ? R_GS : R_FS];
8201 treg = cpu_regs[(modrm & 7) | REX_B(s)];
8202
8203 if (modrm & 0x10) {
8204 /* wr*base */
8205 dst = base, src = treg;
8206 } else {
8207 /* rd*base */
8208 dst = treg, src = base;
8209 }
8210
8211 if (s->dflag == MO_32) {
8212 tcg_gen_ext32u_tl(dst, src);
8213 } else {
8214 tcg_gen_mov_tl(dst, src);
8215 }
8216 break;
8217 }
b9f9c5b4 8218 goto unknown_op;
07929f2a 8219
121f3157
RH
8220 case 0xf8: /* sfence / pcommit */
8221 if (prefixes & PREFIX_DATA) {
8222 /* pcommit */
8223 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_PCOMMIT)
8224 || (prefixes & PREFIX_LOCK)) {
8225 goto illegal_op;
891bc821 8226 }
121f3157
RH
8227 break;
8228 }
8229 /* fallthru */
8230 case 0xf9 ... 0xff: /* sfence */
14cb949a
PB
8231 if (!(s->cpuid_features & CPUID_SSE)
8232 || (prefixes & PREFIX_LOCK)) {
8233 goto illegal_op;
8234 }
cc19e497 8235 tcg_gen_mb(TCG_MO_ST_ST | TCG_BAR_SC);
14cb949a 8236 break;
121f3157 8237 case 0xe8 ... 0xef: /* lfence */
cc19e497
PK
8238 if (!(s->cpuid_features & CPUID_SSE)
8239 || (prefixes & PREFIX_LOCK)) {
8240 goto illegal_op;
8241 }
8242 tcg_gen_mb(TCG_MO_LD_LD | TCG_BAR_SC);
8243 break;
121f3157
RH
8244 case 0xf0 ... 0xf7: /* mfence */
8245 if (!(s->cpuid_features & CPUID_SSE2)
8246 || (prefixes & PREFIX_LOCK)) {
8247 goto illegal_op;
8f091a59 8248 }
cc19e497 8249 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
8f091a59 8250 break;
121f3157 8251
664e0f19 8252 default:
b9f9c5b4 8253 goto unknown_op;
14ce26e7
FB
8254 }
8255 break;
121f3157 8256
a35f3ec7 8257 case 0x10d: /* 3DNow! prefetch(w) */
e3af7c78 8258 modrm = x86_ldub_code(env, s);
a35f3ec7
AJ
8259 mod = (modrm >> 6) & 3;
8260 if (mod == 3)
8261 goto illegal_op;
26317698 8262 gen_nop_modrm(env, s, modrm);
8f091a59 8263 break;
3b21e03e 8264 case 0x1aa: /* rsm */
872929aa 8265 gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
3b21e03e
FB
8266 if (!(s->flags & HF_SMM_MASK))
8267 goto illegal_op;
728d803b 8268 gen_update_cc_op(s);
3b21e03e 8269 gen_jmp_im(s->pc - s->cs_base);
608badfc 8270 gen_helper_rsm(cpu_env);
3b21e03e
FB
8271 gen_eob(s);
8272 break;
222a3336
AZ
8273 case 0x1b8: /* SSE4.2 popcnt */
8274 if ((prefixes & (PREFIX_REPZ | PREFIX_LOCK | PREFIX_REPNZ)) !=
8275 PREFIX_REPZ)
8276 goto illegal_op;
8277 if (!(s->cpuid_ext_features & CPUID_EXT_POPCNT))
8278 goto illegal_op;
8279
e3af7c78 8280 modrm = x86_ldub_code(env, s);
8b4a3df8 8281 reg = ((modrm >> 3) & 7) | rex_r;
222a3336 8282
ab4e4aec 8283 if (s->prefix & PREFIX_DATA) {
4ba9938c 8284 ot = MO_16;
ab4e4aec
RH
8285 } else {
8286 ot = mo_64_32(dflag);
8287 }
222a3336 8288
0af10c86 8289 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4885c3c4
RH
8290 gen_extu(ot, cpu_T0);
8291 tcg_gen_mov_tl(cpu_cc_src, cpu_T0);
8292 tcg_gen_ctpop_tl(cpu_T0, cpu_T0);
1d1cc4d0 8293 gen_op_mov_reg_v(ot, reg, cpu_T0);
fdb0d09d 8294
4885c3c4 8295 set_cc_op(s, CC_OP_POPCNT);
222a3336 8296 break;
a35f3ec7
AJ
8297 case 0x10e ... 0x10f:
8298 /* 3DNow! instructions, ignore prefixes */
8299 s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
664e0f19
FB
8300 case 0x110 ... 0x117:
8301 case 0x128 ... 0x12f:
4242b1bd 8302 case 0x138 ... 0x13a:
d9f4bb27 8303 case 0x150 ... 0x179:
664e0f19
FB
8304 case 0x17c ... 0x17f:
8305 case 0x1c2:
8306 case 0x1c4 ... 0x1c6:
8307 case 0x1d0 ... 0x1fe:
0af10c86 8308 gen_sse(env, s, b, pc_start, rex_r);
664e0f19 8309 break;
2c0262af 8310 default:
b9f9c5b4 8311 goto unknown_op;
2c0262af 8312 }
2c0262af
FB
8313 return s->pc;
8314 illegal_op:
b9f9c5b4
RH
8315 gen_illegal_opcode(s);
8316 return s->pc;
8317 unknown_op:
b9f9c5b4 8318 gen_unknown_opcode(env, s);
2c0262af
FB
8319 return s->pc;
8320}
8321
63618b4e 8322void tcg_x86_init(void)
2c0262af 8323{
fac0aff9
RH
8324 static const char reg_names[CPU_NB_REGS][4] = {
8325#ifdef TARGET_X86_64
8326 [R_EAX] = "rax",
8327 [R_EBX] = "rbx",
8328 [R_ECX] = "rcx",
8329 [R_EDX] = "rdx",
8330 [R_ESI] = "rsi",
8331 [R_EDI] = "rdi",
8332 [R_EBP] = "rbp",
8333 [R_ESP] = "rsp",
8334 [8] = "r8",
8335 [9] = "r9",
8336 [10] = "r10",
8337 [11] = "r11",
8338 [12] = "r12",
8339 [13] = "r13",
8340 [14] = "r14",
8341 [15] = "r15",
8342#else
8343 [R_EAX] = "eax",
8344 [R_EBX] = "ebx",
8345 [R_ECX] = "ecx",
8346 [R_EDX] = "edx",
8347 [R_ESI] = "esi",
8348 [R_EDI] = "edi",
8349 [R_EBP] = "ebp",
8350 [R_ESP] = "esp",
8351#endif
8352 };
3558f805
RH
8353 static const char seg_base_names[6][8] = {
8354 [R_CS] = "cs_base",
8355 [R_DS] = "ds_base",
8356 [R_ES] = "es_base",
8357 [R_FS] = "fs_base",
8358 [R_GS] = "gs_base",
8359 [R_SS] = "ss_base",
8360 };
149b427b
RH
8361 static const char bnd_regl_names[4][8] = {
8362 "bnd0_lb", "bnd1_lb", "bnd2_lb", "bnd3_lb"
8363 };
8364 static const char bnd_regu_names[4][8] = {
8365 "bnd0_ub", "bnd1_ub", "bnd2_ub", "bnd3_ub"
8366 };
fac0aff9
RH
8367 int i;
8368
e1ccc054 8369 cpu_cc_op = tcg_global_mem_new_i32(cpu_env,
317ac620 8370 offsetof(CPUX86State, cc_op), "cc_op");
e1ccc054 8371 cpu_cc_dst = tcg_global_mem_new(cpu_env, offsetof(CPUX86State, cc_dst),
a7812ae4 8372 "cc_dst");
e1ccc054 8373 cpu_cc_src = tcg_global_mem_new(cpu_env, offsetof(CPUX86State, cc_src),
a3251186 8374 "cc_src");
e1ccc054 8375 cpu_cc_src2 = tcg_global_mem_new(cpu_env, offsetof(CPUX86State, cc_src2),
988c3eb0 8376 "cc_src2");
437a88a5 8377
fac0aff9 8378 for (i = 0; i < CPU_NB_REGS; ++i) {
e1ccc054 8379 cpu_regs[i] = tcg_global_mem_new(cpu_env,
fac0aff9
RH
8380 offsetof(CPUX86State, regs[i]),
8381 reg_names[i]);
8382 }
677ef623 8383
3558f805
RH
8384 for (i = 0; i < 6; ++i) {
8385 cpu_seg_base[i]
8386 = tcg_global_mem_new(cpu_env,
8387 offsetof(CPUX86State, segs[i].base),
8388 seg_base_names[i]);
8389 }
8390
149b427b
RH
8391 for (i = 0; i < 4; ++i) {
8392 cpu_bndl[i]
8393 = tcg_global_mem_new_i64(cpu_env,
8394 offsetof(CPUX86State, bnd_regs[i].lb),
8395 bnd_regl_names[i]);
8396 cpu_bndu[i]
8397 = tcg_global_mem_new_i64(cpu_env,
8398 offsetof(CPUX86State, bnd_regs[i].ub),
8399 bnd_regu_names[i]);
8400 }
2c0262af
FB
8401}
8402
9761d39b
LV
8403static int i386_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cpu,
8404 int max_insns)
2c0262af 8405{
9761d39b
LV
8406 DisasContext *dc = container_of(dcbase, DisasContext, base);
8407 CPUX86State *env = cpu->env_ptr;
8408 uint32_t flags = dc->base.tb->flags;
8409 target_ulong cs_base = dc->base.tb->cs_base;
3a1d9b8b 8410
4f31916f 8411 dc->pe = (flags >> HF_PE_SHIFT) & 1;
2c0262af
FB
8412 dc->code32 = (flags >> HF_CS32_SHIFT) & 1;
8413 dc->ss32 = (flags >> HF_SS32_SHIFT) & 1;
8414 dc->addseg = (flags >> HF_ADDSEG_SHIFT) & 1;
8415 dc->f_st = 0;
8416 dc->vm86 = (flags >> VM_SHIFT) & 1;
8417 dc->cpl = (flags >> HF_CPL_SHIFT) & 3;
8418 dc->iopl = (flags >> IOPL_SHIFT) & 3;
8419 dc->tf = (flags >> TF_SHIFT) & 1;
8420 dc->cc_op = CC_OP_DYNAMIC;
e207582f 8421 dc->cc_op_dirty = false;
2c0262af 8422 dc->cs_base = cs_base;
2c0262af
FB
8423 dc->popl_esp_hack = 0;
8424 /* select memory access functions */
8425 dc->mem_index = 0;
da6d48e3
SF
8426#ifdef CONFIG_SOFTMMU
8427 dc->mem_index = cpu_mmu_index(env, false);
8428#endif
0514ef2f
EH
8429 dc->cpuid_features = env->features[FEAT_1_EDX];
8430 dc->cpuid_ext_features = env->features[FEAT_1_ECX];
8431 dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
8432 dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
8433 dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];
c9cfe8f9 8434 dc->cpuid_xsave_features = env->features[FEAT_XSAVE];
14ce26e7
FB
8435#ifdef TARGET_X86_64
8436 dc->lma = (flags >> HF_LMA_SHIFT) & 1;
8437 dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
8438#endif
7eee2a50 8439 dc->flags = flags;
9761d39b 8440 dc->jmp_opt = !(dc->tf || dc->base.singlestep_enabled ||
da6d48e3 8441 (flags & HF_INHIBIT_IRQ_MASK));
c4d4525c
PD
8442 /* Do not optimize repz jumps at all in icount mode, because
8443 rep movsS instructions are execured with different paths
8444 in !repz_opt and repz_opt modes. The first one was used
8445 always except single step mode. And this setting
8446 disables jumps optimization and control paths become
8447 equivalent in run and single step modes.
8448 Now there will be no jump optimization for repz in
8449 record/replay modes and there will always be an
8450 additional step for ecx=0 when icount is enabled.
8451 */
c5a49c63 8452 dc->repz_opt = !dc->jmp_opt && !(tb_cflags(dc->base.tb) & CF_USE_ICOUNT);
4f31916f
FB
8453#if 0
8454 /* check addseg logic */
dc196a57 8455 if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
4f31916f
FB
8456 printf("ERROR addseg\n");
8457#endif
8458
1d1cc4d0
RH
8459 cpu_T0 = tcg_temp_new();
8460 cpu_T1 = tcg_temp_new();
a7812ae4 8461 cpu_A0 = tcg_temp_new();
a7812ae4
PB
8462
8463 cpu_tmp0 = tcg_temp_new();
8464 cpu_tmp1_i64 = tcg_temp_new_i64();
8465 cpu_tmp2_i32 = tcg_temp_new_i32();
8466 cpu_tmp3_i32 = tcg_temp_new_i32();
8467 cpu_tmp4 = tcg_temp_new();
a7812ae4
PB
8468 cpu_ptr0 = tcg_temp_new_ptr();
8469 cpu_ptr1 = tcg_temp_new_ptr();
a3251186 8470 cpu_cc_srcT = tcg_temp_local_new();
57fec1fe 8471
9761d39b
LV
8472 return max_insns;
8473}
8474
d2e6eedf
LV
8475static void i386_tr_tb_start(DisasContextBase *db, CPUState *cpu)
8476{
8477}
8478
9d75f52b
LV
8479static void i386_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
8480{
8481 DisasContext *dc = container_of(dcbase, DisasContext, base);
8482
8483 tcg_gen_insn_start(dc->base.pc_next, dc->cc_op);
8484}
8485
e6b41ec3
LV
8486static bool i386_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
8487 const CPUBreakpoint *bp)
8488{
8489 DisasContext *dc = container_of(dcbase, DisasContext, base);
8490 /* If RF is set, suppress an internally generated breakpoint. */
8491 int flags = dc->base.tb->flags & HF_RF_MASK ? BP_GDB : BP_ANY;
8492 if (bp->flags & flags) {
8493 gen_debug(dc, dc->base.pc_next - dc->cs_base);
8494 dc->base.is_jmp = DISAS_NORETURN;
8495 /* The address covered by the breakpoint must be included in
8496 [tb->pc, tb->pc + tb->size) in order to for it to be
8497 properly cleared -- thus we increment the PC here so that
d2e6eedf 8498 the generic logic setting tb->size later does the right thing. */
e6b41ec3
LV
8499 dc->base.pc_next += 1;
8500 return true;
8501 } else {
8502 return false;
8503 }
8504}
8505
2c2f8cac
LV
8506static void i386_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
8507{
8508 DisasContext *dc = container_of(dcbase, DisasContext, base);
8509 target_ulong pc_next = disas_insn(dc, cpu);
8510
8511 if (dc->tf || (dc->base.tb->flags & HF_INHIBIT_IRQ_MASK)) {
8512 /* if single step mode, we generate only one instruction and
8513 generate an exception */
8514 /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
8515 the flag and abort the translation to give the irqs a
8516 chance to happen */
2c2f8cac 8517 dc->base.is_jmp = DISAS_TOO_MANY;
c5a49c63 8518 } else if ((tb_cflags(dc->base.tb) & CF_USE_ICOUNT)
2c2f8cac
LV
8519 && ((dc->base.pc_next & TARGET_PAGE_MASK)
8520 != ((dc->base.pc_next + TARGET_MAX_INSN_SIZE - 1)
8521 & TARGET_PAGE_MASK)
8522 || (dc->base.pc_next & ~TARGET_PAGE_MASK) == 0)) {
8523 /* Do not cross the boundary of the pages in icount mode,
8524 it can cause an exception. Do it only when boundary is
8525 crossed by the first instruction in the block.
8526 If current instruction already crossed the bound - it's ok,
8527 because an exception hasn't stopped this code.
8528 */
2c2f8cac
LV
8529 dc->base.is_jmp = DISAS_TOO_MANY;
8530 } else if ((pc_next - dc->base.pc_first) >= (TARGET_PAGE_SIZE - 32)) {
2c2f8cac
LV
8531 dc->base.is_jmp = DISAS_TOO_MANY;
8532 }
8533
8534 dc->base.pc_next = pc_next;
8535}
8536
47e981b4
LV
8537static void i386_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
8538{
8539 DisasContext *dc = container_of(dcbase, DisasContext, base);
8540
8541 if (dc->base.is_jmp == DISAS_TOO_MANY) {
8542 gen_jmp_im(dc->base.pc_next - dc->cs_base);
8543 gen_eob(dc);
8544 }
8545}
8546
e0d110d9
LV
8547static void i386_tr_disas_log(const DisasContextBase *dcbase,
8548 CPUState *cpu)
8549{
8550 DisasContext *dc = container_of(dcbase, DisasContext, base);
e0d110d9
LV
8551
8552 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
1d48474d 8553 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
e0d110d9
LV
8554}
8555
d2e6eedf
LV
8556static const TranslatorOps i386_tr_ops = {
8557 .init_disas_context = i386_tr_init_disas_context,
8558 .tb_start = i386_tr_tb_start,
8559 .insn_start = i386_tr_insn_start,
8560 .breakpoint_check = i386_tr_breakpoint_check,
8561 .translate_insn = i386_tr_translate_insn,
8562 .tb_stop = i386_tr_tb_stop,
8563 .disas_log = i386_tr_disas_log,
8564};
0a7df5da 8565
d2e6eedf
LV
8566/* generate intermediate code for basic block 'tb'. */
8567void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb)
8568{
8569 DisasContext dc;
e0d110d9 8570
d2e6eedf 8571 translator_loop(&i386_tr_ops, &dc.base, cpu, tb);
2c0262af
FB
8572}
8573
bad729e2
RH
8574void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb,
8575 target_ulong *data)
d2856f1a 8576{
bad729e2
RH
8577 int cc_op = data[1];
8578 env->eip = data[0] - tb->cs_base;
8579 if (cc_op != CC_OP_DYNAMIC) {
d2856f1a 8580 env->cc_op = cc_op;
bad729e2 8581 }
d2856f1a 8582}