]> git.proxmox.com Git - mirror_qemu.git/blame - target-i386/translate.c
target-i386: fbld instruction doesn't set minus sign
[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
FB
18 */
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24#include <signal.h>
2c0262af 25
bec93d72 26#include "qemu/host-utils.h"
2c0262af 27#include "cpu.h"
76cad711 28#include "disas/disas.h"
57fec1fe 29#include "tcg-op.h"
f08b6170 30#include "exec/cpu_ldst.h"
2c0262af 31
2ef6175a
RH
32#include "exec/helper-proto.h"
33#include "exec/helper-gen.h"
a7812ae4 34
a7e30d84
LV
35#include "trace-tcg.h"
36
37
2c0262af
FB
38#define PREFIX_REPZ 0x01
39#define PREFIX_REPNZ 0x02
40#define PREFIX_LOCK 0x04
41#define PREFIX_DATA 0x08
42#define PREFIX_ADR 0x10
701ed211 43#define PREFIX_VEX 0x20
2c0262af 44
14ce26e7 45#ifdef TARGET_X86_64
14ce26e7
FB
46#define CODE64(s) ((s)->code64)
47#define REX_X(s) ((s)->rex_x)
48#define REX_B(s) ((s)->rex_b)
14ce26e7 49#else
14ce26e7
FB
50#define CODE64(s) 0
51#define REX_X(s) 0
52#define REX_B(s) 0
53#endif
54
bec93d72
RH
55#ifdef TARGET_X86_64
56# define ctztl ctz64
57# define clztl clz64
58#else
59# define ctztl ctz32
60# define clztl clz32
61#endif
62
57fec1fe
FB
63//#define MACRO_TEST 1
64
57fec1fe 65/* global register indexes */
a7812ae4 66static TCGv_ptr cpu_env;
a3251186 67static TCGv cpu_A0;
988c3eb0 68static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2, cpu_cc_srcT;
a7812ae4 69static TCGv_i32 cpu_cc_op;
cc739bb0 70static TCGv cpu_regs[CPU_NB_REGS];
1e4840bf 71/* local temps */
3b9d3cf1 72static TCGv cpu_T[2];
57fec1fe 73/* local register indexes (only used inside old micro ops) */
a7812ae4
PB
74static TCGv cpu_tmp0, cpu_tmp4;
75static TCGv_ptr cpu_ptr0, cpu_ptr1;
76static TCGv_i32 cpu_tmp2_i32, cpu_tmp3_i32;
77static TCGv_i64 cpu_tmp1_i64;
57fec1fe 78
1a7ff922
PB
79static uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
80
022c62cb 81#include "exec/gen-icount.h"
2e70f6ef 82
57fec1fe
FB
83#ifdef TARGET_X86_64
84static int x86_64_hregs;
ae063a68
FB
85#endif
86
2c0262af
FB
87typedef struct DisasContext {
88 /* current insn context */
89 int override; /* -1 if no override */
90 int prefix;
1d71ddb1 91 TCGMemOp aflag;
ab4e4aec 92 TCGMemOp dflag;
14ce26e7 93 target_ulong pc; /* pc = eip + cs_base */
2c0262af
FB
94 int is_jmp; /* 1 = means jump (stop translation), 2 means CPU
95 static state change (stop translation) */
96 /* current block context */
14ce26e7 97 target_ulong cs_base; /* base of CS segment */
2c0262af
FB
98 int pe; /* protected mode */
99 int code32; /* 32 bit code segment */
14ce26e7
FB
100#ifdef TARGET_X86_64
101 int lma; /* long mode active */
102 int code64; /* 64 bit code segment */
103 int rex_x, rex_b;
104#endif
701ed211
RH
105 int vex_l; /* vex vector length */
106 int vex_v; /* vex vvvv register, without 1's compliment. */
2c0262af 107 int ss32; /* 32 bit stack segment */
fee71888 108 CCOp cc_op; /* current CC operation */
e207582f 109 bool cc_op_dirty;
2c0262af
FB
110 int addseg; /* non zero if either DS/ES/SS have a non zero base */
111 int f_st; /* currently unused */
112 int vm86; /* vm86 mode */
113 int cpl;
114 int iopl;
115 int tf; /* TF cpu flag */
34865134 116 int singlestep_enabled; /* "hardware" single step enabled */
2c0262af
FB
117 int jmp_opt; /* use direct block chaining for direct jumps */
118 int mem_index; /* select memory access functions */
c068688b 119 uint64_t flags; /* all execution flags */
2c0262af
FB
120 struct TranslationBlock *tb;
121 int popl_esp_hack; /* for correct popl with esp base handling */
14ce26e7
FB
122 int rip_offset; /* only used in x86_64, but left for simplicity */
123 int cpuid_features;
3d7374c5 124 int cpuid_ext_features;
e771edab 125 int cpuid_ext2_features;
12e26b75 126 int cpuid_ext3_features;
a9321a4d 127 int cpuid_7_0_ebx_features;
2c0262af
FB
128} DisasContext;
129
130static void gen_eob(DisasContext *s);
14ce26e7
FB
131static void gen_jmp(DisasContext *s, target_ulong eip);
132static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num);
d67dc9e6 133static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d);
2c0262af
FB
134
135/* i386 arith/logic operations */
136enum {
5fafdf24
TS
137 OP_ADDL,
138 OP_ORL,
139 OP_ADCL,
2c0262af 140 OP_SBBL,
5fafdf24
TS
141 OP_ANDL,
142 OP_SUBL,
143 OP_XORL,
2c0262af
FB
144 OP_CMPL,
145};
146
147/* i386 shift ops */
148enum {
5fafdf24
TS
149 OP_ROL,
150 OP_ROR,
151 OP_RCL,
152 OP_RCR,
153 OP_SHL,
154 OP_SHR,
2c0262af
FB
155 OP_SHL1, /* undocumented */
156 OP_SAR = 7,
157};
158
8e1c85e3
FB
159enum {
160 JCC_O,
161 JCC_B,
162 JCC_Z,
163 JCC_BE,
164 JCC_S,
165 JCC_P,
166 JCC_L,
167 JCC_LE,
168};
169
2c0262af
FB
170enum {
171 /* I386 int registers */
172 OR_EAX, /* MUST be even numbered */
173 OR_ECX,
174 OR_EDX,
175 OR_EBX,
176 OR_ESP,
177 OR_EBP,
178 OR_ESI,
179 OR_EDI,
14ce26e7
FB
180
181 OR_TMP0 = 16, /* temporary operand register */
2c0262af
FB
182 OR_TMP1,
183 OR_A0, /* temporary register used when doing address evaluation */
2c0262af
FB
184};
185
b666265b 186enum {
a3251186
RH
187 USES_CC_DST = 1,
188 USES_CC_SRC = 2,
988c3eb0
RH
189 USES_CC_SRC2 = 4,
190 USES_CC_SRCT = 8,
b666265b
RH
191};
192
193/* Bit set if the global variable is live after setting CC_OP to X. */
194static const uint8_t cc_op_live[CC_OP_NB] = {
988c3eb0 195 [CC_OP_DYNAMIC] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
b666265b
RH
196 [CC_OP_EFLAGS] = USES_CC_SRC,
197 [CC_OP_MULB ... CC_OP_MULQ] = USES_CC_DST | USES_CC_SRC,
198 [CC_OP_ADDB ... CC_OP_ADDQ] = USES_CC_DST | USES_CC_SRC,
988c3eb0 199 [CC_OP_ADCB ... CC_OP_ADCQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
a3251186 200 [CC_OP_SUBB ... CC_OP_SUBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRCT,
988c3eb0 201 [CC_OP_SBBB ... CC_OP_SBBQ] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
b666265b
RH
202 [CC_OP_LOGICB ... CC_OP_LOGICQ] = USES_CC_DST,
203 [CC_OP_INCB ... CC_OP_INCQ] = USES_CC_DST | USES_CC_SRC,
204 [CC_OP_DECB ... CC_OP_DECQ] = USES_CC_DST | USES_CC_SRC,
205 [CC_OP_SHLB ... CC_OP_SHLQ] = USES_CC_DST | USES_CC_SRC,
206 [CC_OP_SARB ... CC_OP_SARQ] = USES_CC_DST | USES_CC_SRC,
bc4b43dc 207 [CC_OP_BMILGB ... CC_OP_BMILGQ] = USES_CC_DST | USES_CC_SRC,
cd7f97ca
RH
208 [CC_OP_ADCX] = USES_CC_DST | USES_CC_SRC,
209 [CC_OP_ADOX] = USES_CC_SRC | USES_CC_SRC2,
210 [CC_OP_ADCOX] = USES_CC_DST | USES_CC_SRC | USES_CC_SRC2,
436ff2d2 211 [CC_OP_CLR] = 0,
b666265b
RH
212};
213
e207582f 214static void set_cc_op(DisasContext *s, CCOp op)
3ca51d07 215{
b666265b
RH
216 int dead;
217
218 if (s->cc_op == op) {
219 return;
220 }
221
222 /* Discard CC computation that will no longer be used. */
223 dead = cc_op_live[s->cc_op] & ~cc_op_live[op];
224 if (dead & USES_CC_DST) {
225 tcg_gen_discard_tl(cpu_cc_dst);
e207582f 226 }
b666265b
RH
227 if (dead & USES_CC_SRC) {
228 tcg_gen_discard_tl(cpu_cc_src);
229 }
988c3eb0
RH
230 if (dead & USES_CC_SRC2) {
231 tcg_gen_discard_tl(cpu_cc_src2);
232 }
a3251186
RH
233 if (dead & USES_CC_SRCT) {
234 tcg_gen_discard_tl(cpu_cc_srcT);
235 }
b666265b 236
e2f515cf
RH
237 if (op == CC_OP_DYNAMIC) {
238 /* The DYNAMIC setting is translator only, and should never be
239 stored. Thus we always consider it clean. */
240 s->cc_op_dirty = false;
241 } else {
242 /* Discard any computed CC_OP value (see shifts). */
243 if (s->cc_op == CC_OP_DYNAMIC) {
244 tcg_gen_discard_i32(cpu_cc_op);
245 }
246 s->cc_op_dirty = true;
247 }
b666265b 248 s->cc_op = op;
e207582f
RH
249}
250
e207582f
RH
251static void gen_update_cc_op(DisasContext *s)
252{
253 if (s->cc_op_dirty) {
773cdfcc 254 tcg_gen_movi_i32(cpu_cc_op, s->cc_op);
e207582f
RH
255 s->cc_op_dirty = false;
256 }
3ca51d07
RH
257}
258
14ce26e7
FB
259#ifdef TARGET_X86_64
260
261#define NB_OP_SIZES 4
262
14ce26e7
FB
263#else /* !TARGET_X86_64 */
264
265#define NB_OP_SIZES 3
266
14ce26e7
FB
267#endif /* !TARGET_X86_64 */
268
e2542fe2 269#if defined(HOST_WORDS_BIGENDIAN)
57fec1fe
FB
270#define REG_B_OFFSET (sizeof(target_ulong) - 1)
271#define REG_H_OFFSET (sizeof(target_ulong) - 2)
272#define REG_W_OFFSET (sizeof(target_ulong) - 2)
273#define REG_L_OFFSET (sizeof(target_ulong) - 4)
274#define REG_LH_OFFSET (sizeof(target_ulong) - 8)
14ce26e7 275#else
57fec1fe
FB
276#define REG_B_OFFSET 0
277#define REG_H_OFFSET 1
278#define REG_W_OFFSET 0
279#define REG_L_OFFSET 0
280#define REG_LH_OFFSET 4
14ce26e7 281#endif
57fec1fe 282
96d7073f
PM
283/* In instruction encodings for byte register accesses the
284 * register number usually indicates "low 8 bits of register N";
285 * however there are some special cases where N 4..7 indicates
286 * [AH, CH, DH, BH], ie "bits 15..8 of register N-4". Return
287 * true for this special case, false otherwise.
288 */
289static inline bool byte_reg_is_xH(int reg)
290{
291 if (reg < 4) {
292 return false;
293 }
294#ifdef TARGET_X86_64
295 if (reg >= 8 || x86_64_hregs) {
296 return false;
297 }
298#endif
299 return true;
300}
301
ab4e4aec
RH
302/* Select the size of a push/pop operation. */
303static inline TCGMemOp mo_pushpop(DisasContext *s, TCGMemOp ot)
304{
305 if (CODE64(s)) {
306 return ot == MO_16 ? MO_16 : MO_64;
307 } else {
308 return ot;
309 }
310}
311
312/* Select only size 64 else 32. Used for SSE operand sizes. */
313static inline TCGMemOp mo_64_32(TCGMemOp ot)
314{
315#ifdef TARGET_X86_64
316 return ot == MO_64 ? MO_64 : MO_32;
317#else
318 return MO_32;
319#endif
320}
321
322/* Select size 8 if lsb of B is clear, else OT. Used for decoding
323 byte vs word opcodes. */
324static inline TCGMemOp mo_b_d(int b, TCGMemOp ot)
325{
326 return b & 1 ? ot : MO_8;
327}
328
329/* Select size 8 if lsb of B is clear, else OT capped at 32.
330 Used for decoding operand size of port opcodes. */
331static inline TCGMemOp mo_b_d32(int b, TCGMemOp ot)
332{
333 return b & 1 ? (ot == MO_16 ? MO_16 : MO_32) : MO_8;
334}
335
d67dc9e6 336static void gen_op_mov_reg_v(TCGMemOp ot, int reg, TCGv t0)
57fec1fe
FB
337{
338 switch(ot) {
4ba9938c 339 case MO_8:
96d7073f 340 if (!byte_reg_is_xH(reg)) {
c832e3de 341 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 8);
57fec1fe 342 } else {
c832e3de 343 tcg_gen_deposit_tl(cpu_regs[reg - 4], cpu_regs[reg - 4], t0, 8, 8);
57fec1fe
FB
344 }
345 break;
4ba9938c 346 case MO_16:
c832e3de 347 tcg_gen_deposit_tl(cpu_regs[reg], cpu_regs[reg], t0, 0, 16);
57fec1fe 348 break;
4ba9938c 349 case MO_32:
cc739bb0
LD
350 /* For x86_64, this sets the higher half of register to zero.
351 For i386, this is equivalent to a mov. */
352 tcg_gen_ext32u_tl(cpu_regs[reg], t0);
57fec1fe 353 break;
cc739bb0 354#ifdef TARGET_X86_64
4ba9938c 355 case MO_64:
cc739bb0 356 tcg_gen_mov_tl(cpu_regs[reg], t0);
57fec1fe 357 break;
14ce26e7 358#endif
d67dc9e6
RH
359 default:
360 tcg_abort();
57fec1fe
FB
361 }
362}
2c0262af 363
d67dc9e6 364static inline void gen_op_mov_v_reg(TCGMemOp ot, TCGv t0, int reg)
57fec1fe 365{
4ba9938c 366 if (ot == MO_8 && byte_reg_is_xH(reg)) {
96d7073f
PM
367 tcg_gen_shri_tl(t0, cpu_regs[reg - 4], 8);
368 tcg_gen_ext8u_tl(t0, t0);
369 } else {
cc739bb0 370 tcg_gen_mov_tl(t0, cpu_regs[reg]);
57fec1fe
FB
371 }
372}
373
374static inline void gen_op_movl_A0_reg(int reg)
375{
cc739bb0 376 tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
57fec1fe
FB
377}
378
379static inline void gen_op_addl_A0_im(int32_t val)
380{
381 tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
14ce26e7 382#ifdef TARGET_X86_64
57fec1fe 383 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
14ce26e7 384#endif
57fec1fe 385}
2c0262af 386
14ce26e7 387#ifdef TARGET_X86_64
57fec1fe
FB
388static inline void gen_op_addq_A0_im(int64_t val)
389{
390 tcg_gen_addi_tl(cpu_A0, cpu_A0, val);
391}
14ce26e7 392#endif
57fec1fe
FB
393
394static void gen_add_A0_im(DisasContext *s, int val)
395{
396#ifdef TARGET_X86_64
397 if (CODE64(s))
398 gen_op_addq_A0_im(val);
399 else
400#endif
401 gen_op_addl_A0_im(val);
402}
2c0262af 403
74bdfbda 404static inline void gen_op_jmp_v(TCGv dest)
57fec1fe 405{
74bdfbda 406 tcg_gen_st_tl(dest, cpu_env, offsetof(CPUX86State, eip));
57fec1fe
FB
407}
408
d3f4bbe3 409static inline void gen_op_add_reg_im(TCGMemOp size, int reg, int32_t val)
57fec1fe 410{
d3f4bbe3
RH
411 tcg_gen_addi_tl(cpu_tmp0, cpu_regs[reg], val);
412 gen_op_mov_reg_v(size, reg, cpu_tmp0);
57fec1fe
FB
413}
414
d3f4bbe3 415static inline void gen_op_add_reg_T0(TCGMemOp size, int reg)
57fec1fe 416{
d3f4bbe3
RH
417 tcg_gen_add_tl(cpu_tmp0, cpu_regs[reg], cpu_T[0]);
418 gen_op_mov_reg_v(size, reg, cpu_tmp0);
6e0d8677 419}
57fec1fe 420
57fec1fe
FB
421static inline void gen_op_addl_A0_reg_sN(int shift, int reg)
422{
cc739bb0
LD
423 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
424 if (shift != 0)
57fec1fe
FB
425 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
426 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
cc739bb0
LD
427 /* For x86_64, this sets the higher half of register to zero.
428 For i386, this is equivalent to a nop. */
429 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
57fec1fe 430}
2c0262af 431
57fec1fe
FB
432static inline void gen_op_movl_A0_seg(int reg)
433{
317ac620 434 tcg_gen_ld32u_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base) + REG_L_OFFSET);
57fec1fe 435}
2c0262af 436
7162ab21 437static inline void gen_op_addl_A0_seg(DisasContext *s, int reg)
57fec1fe 438{
317ac620 439 tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
57fec1fe 440#ifdef TARGET_X86_64
7162ab21
VC
441 if (CODE64(s)) {
442 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
443 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
444 } else {
445 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
446 tcg_gen_andi_tl(cpu_A0, cpu_A0, 0xffffffff);
447 }
448#else
449 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
57fec1fe
FB
450#endif
451}
2c0262af 452
14ce26e7 453#ifdef TARGET_X86_64
57fec1fe
FB
454static inline void gen_op_movq_A0_seg(int reg)
455{
317ac620 456 tcg_gen_ld_tl(cpu_A0, cpu_env, offsetof(CPUX86State, segs[reg].base));
57fec1fe 457}
14ce26e7 458
57fec1fe
FB
459static inline void gen_op_addq_A0_seg(int reg)
460{
317ac620 461 tcg_gen_ld_tl(cpu_tmp0, cpu_env, offsetof(CPUX86State, segs[reg].base));
57fec1fe
FB
462 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
463}
464
465static inline void gen_op_movq_A0_reg(int reg)
466{
cc739bb0 467 tcg_gen_mov_tl(cpu_A0, cpu_regs[reg]);
57fec1fe
FB
468}
469
470static inline void gen_op_addq_A0_reg_sN(int shift, int reg)
471{
cc739bb0
LD
472 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[reg]);
473 if (shift != 0)
57fec1fe
FB
474 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, shift);
475 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
476}
14ce26e7
FB
477#endif
478
323d1876 479static inline void gen_op_ld_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
57fec1fe 480{
3c5f4116 481 tcg_gen_qemu_ld_tl(t0, a0, s->mem_index, idx | MO_LE);
57fec1fe 482}
2c0262af 483
323d1876 484static inline void gen_op_st_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
57fec1fe 485{
3523e4bd 486 tcg_gen_qemu_st_tl(t0, a0, s->mem_index, idx | MO_LE);
57fec1fe 487}
4f31916f 488
d4faa3e0
RH
489static inline void gen_op_st_rm_T0_A0(DisasContext *s, int idx, int d)
490{
491 if (d == OR_TMP0) {
fd8ca9f6 492 gen_op_st_v(s, idx, cpu_T[0], cpu_A0);
d4faa3e0 493 } else {
480a762d 494 gen_op_mov_reg_v(idx, d, cpu_T[0]);
d4faa3e0
RH
495 }
496}
497
14ce26e7
FB
498static inline void gen_jmp_im(target_ulong pc)
499{
57fec1fe 500 tcg_gen_movi_tl(cpu_tmp0, pc);
74bdfbda 501 gen_op_jmp_v(cpu_tmp0);
14ce26e7
FB
502}
503
2c0262af
FB
504static inline void gen_string_movl_A0_ESI(DisasContext *s)
505{
506 int override;
507
508 override = s->override;
1d71ddb1 509 switch (s->aflag) {
14ce26e7 510#ifdef TARGET_X86_64
1d71ddb1 511 case MO_64:
14ce26e7 512 if (override >= 0) {
57fec1fe
FB
513 gen_op_movq_A0_seg(override);
514 gen_op_addq_A0_reg_sN(0, R_ESI);
14ce26e7 515 } else {
57fec1fe 516 gen_op_movq_A0_reg(R_ESI);
14ce26e7 517 }
1d71ddb1 518 break;
14ce26e7 519#endif
1d71ddb1 520 case MO_32:
2c0262af
FB
521 /* 32 bit address */
522 if (s->addseg && override < 0)
523 override = R_DS;
524 if (override >= 0) {
57fec1fe
FB
525 gen_op_movl_A0_seg(override);
526 gen_op_addl_A0_reg_sN(0, R_ESI);
2c0262af 527 } else {
57fec1fe 528 gen_op_movl_A0_reg(R_ESI);
2c0262af 529 }
1d71ddb1
RH
530 break;
531 case MO_16:
2c0262af
FB
532 /* 16 address, always override */
533 if (override < 0)
534 override = R_DS;
a7e5c7de 535 tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_ESI]);
7162ab21 536 gen_op_addl_A0_seg(s, override);
1d71ddb1
RH
537 break;
538 default:
539 tcg_abort();
2c0262af
FB
540 }
541}
542
543static inline void gen_string_movl_A0_EDI(DisasContext *s)
544{
1d71ddb1 545 switch (s->aflag) {
14ce26e7 546#ifdef TARGET_X86_64
1d71ddb1 547 case MO_64:
57fec1fe 548 gen_op_movq_A0_reg(R_EDI);
1d71ddb1 549 break;
14ce26e7 550#endif
1d71ddb1 551 case MO_32:
2c0262af 552 if (s->addseg) {
57fec1fe
FB
553 gen_op_movl_A0_seg(R_ES);
554 gen_op_addl_A0_reg_sN(0, R_EDI);
2c0262af 555 } else {
57fec1fe 556 gen_op_movl_A0_reg(R_EDI);
2c0262af 557 }
1d71ddb1
RH
558 break;
559 case MO_16:
a7e5c7de 560 tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_EDI]);
7162ab21 561 gen_op_addl_A0_seg(s, R_ES);
1d71ddb1
RH
562 break;
563 default:
564 tcg_abort();
2c0262af
FB
565 }
566}
567
d67dc9e6 568static inline void gen_op_movl_T0_Dshift(TCGMemOp ot)
6e0d8677 569{
317ac620 570 tcg_gen_ld32s_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, df));
6e0d8677 571 tcg_gen_shli_tl(cpu_T[0], cpu_T[0], ot);
2c0262af
FB
572};
573
d67dc9e6 574static TCGv gen_ext_tl(TCGv dst, TCGv src, TCGMemOp size, bool sign)
6e0d8677 575{
d824df34 576 switch (size) {
4ba9938c 577 case MO_8:
d824df34
PB
578 if (sign) {
579 tcg_gen_ext8s_tl(dst, src);
580 } else {
581 tcg_gen_ext8u_tl(dst, src);
582 }
583 return dst;
4ba9938c 584 case MO_16:
d824df34
PB
585 if (sign) {
586 tcg_gen_ext16s_tl(dst, src);
587 } else {
588 tcg_gen_ext16u_tl(dst, src);
589 }
590 return dst;
591#ifdef TARGET_X86_64
4ba9938c 592 case MO_32:
d824df34
PB
593 if (sign) {
594 tcg_gen_ext32s_tl(dst, src);
595 } else {
596 tcg_gen_ext32u_tl(dst, src);
597 }
598 return dst;
599#endif
6e0d8677 600 default:
d824df34 601 return src;
6e0d8677
FB
602 }
603}
3b46e624 604
d67dc9e6 605static void gen_extu(TCGMemOp ot, TCGv reg)
d824df34
PB
606{
607 gen_ext_tl(reg, reg, ot, false);
608}
609
d67dc9e6 610static void gen_exts(TCGMemOp ot, TCGv reg)
6e0d8677 611{
d824df34 612 gen_ext_tl(reg, reg, ot, true);
6e0d8677 613}
2c0262af 614
c92aa1ad 615static inline void gen_op_jnz_ecx(TCGMemOp size, int label1)
6e0d8677 616{
cc739bb0 617 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
c92aa1ad 618 gen_extu(size, cpu_tmp0);
cb63669a 619 tcg_gen_brcondi_tl(TCG_COND_NE, cpu_tmp0, 0, label1);
6e0d8677
FB
620}
621
c92aa1ad 622static inline void gen_op_jz_ecx(TCGMemOp size, int label1)
6e0d8677 623{
cc739bb0 624 tcg_gen_mov_tl(cpu_tmp0, cpu_regs[R_ECX]);
c92aa1ad 625 gen_extu(size, cpu_tmp0);
cb63669a 626 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
6e0d8677 627}
2c0262af 628
d67dc9e6 629static void gen_helper_in_func(TCGMemOp ot, TCGv v, TCGv_i32 n)
a7812ae4
PB
630{
631 switch (ot) {
4ba9938c 632 case MO_8:
93ab25d7
PB
633 gen_helper_inb(v, n);
634 break;
4ba9938c 635 case MO_16:
93ab25d7
PB
636 gen_helper_inw(v, n);
637 break;
4ba9938c 638 case MO_32:
93ab25d7
PB
639 gen_helper_inl(v, n);
640 break;
d67dc9e6
RH
641 default:
642 tcg_abort();
a7812ae4 643 }
a7812ae4 644}
2c0262af 645
d67dc9e6 646static void gen_helper_out_func(TCGMemOp ot, TCGv_i32 v, TCGv_i32 n)
a7812ae4
PB
647{
648 switch (ot) {
4ba9938c 649 case MO_8:
93ab25d7
PB
650 gen_helper_outb(v, n);
651 break;
4ba9938c 652 case MO_16:
93ab25d7
PB
653 gen_helper_outw(v, n);
654 break;
4ba9938c 655 case MO_32:
93ab25d7
PB
656 gen_helper_outl(v, n);
657 break;
d67dc9e6
RH
658 default:
659 tcg_abort();
a7812ae4 660 }
a7812ae4 661}
f115e911 662
d67dc9e6 663static void gen_check_io(DisasContext *s, TCGMemOp ot, target_ulong cur_eip,
b8b6a50b 664 uint32_t svm_flags)
f115e911 665{
b8b6a50b
FB
666 int state_saved;
667 target_ulong next_eip;
668
669 state_saved = 0;
f115e911 670 if (s->pe && (s->cpl > s->iopl || s->vm86)) {
773cdfcc 671 gen_update_cc_op(s);
14ce26e7 672 gen_jmp_im(cur_eip);
b8b6a50b 673 state_saved = 1;
b6abf97d 674 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 675 switch (ot) {
4ba9938c 676 case MO_8:
4a7443be
BS
677 gen_helper_check_iob(cpu_env, cpu_tmp2_i32);
678 break;
4ba9938c 679 case MO_16:
4a7443be
BS
680 gen_helper_check_iow(cpu_env, cpu_tmp2_i32);
681 break;
4ba9938c 682 case MO_32:
4a7443be
BS
683 gen_helper_check_iol(cpu_env, cpu_tmp2_i32);
684 break;
d67dc9e6
RH
685 default:
686 tcg_abort();
a7812ae4 687 }
b8b6a50b 688 }
872929aa 689 if(s->flags & HF_SVMI_MASK) {
b8b6a50b 690 if (!state_saved) {
773cdfcc 691 gen_update_cc_op(s);
b8b6a50b 692 gen_jmp_im(cur_eip);
b8b6a50b
FB
693 }
694 svm_flags |= (1 << (4 + ot));
695 next_eip = s->pc - s->cs_base;
b6abf97d 696 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
052e80d5
BS
697 gen_helper_svm_check_io(cpu_env, cpu_tmp2_i32,
698 tcg_const_i32(svm_flags),
a7812ae4 699 tcg_const_i32(next_eip - cur_eip));
f115e911
FB
700 }
701}
702
d67dc9e6 703static inline void gen_movs(DisasContext *s, TCGMemOp ot)
2c0262af
FB
704{
705 gen_string_movl_A0_ESI(s);
909be183 706 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 707 gen_string_movl_A0_EDI(s);
fd8ca9f6 708 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
6e0d8677 709 gen_op_movl_T0_Dshift(ot);
1d71ddb1
RH
710 gen_op_add_reg_T0(s->aflag, R_ESI);
711 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
712}
713
b6abf97d
FB
714static void gen_op_update1_cc(void)
715{
b6abf97d
FB
716 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
717}
718
719static void gen_op_update2_cc(void)
720{
721 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
722 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
723}
724
988c3eb0
RH
725static void gen_op_update3_cc(TCGv reg)
726{
727 tcg_gen_mov_tl(cpu_cc_src2, reg);
728 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
729 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
730}
731
b6abf97d
FB
732static inline void gen_op_testl_T0_T1_cc(void)
733{
b6abf97d
FB
734 tcg_gen_and_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
735}
736
737static void gen_op_update_neg_cc(void)
738{
b6abf97d 739 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
a3251186
RH
740 tcg_gen_neg_tl(cpu_cc_src, cpu_T[0]);
741 tcg_gen_movi_tl(cpu_cc_srcT, 0);
b6abf97d
FB
742}
743
d229edce
RH
744/* compute all eflags to cc_src */
745static void gen_compute_eflags(DisasContext *s)
8e1c85e3 746{
988c3eb0 747 TCGv zero, dst, src1, src2;
db9f2597
RH
748 int live, dead;
749
d229edce
RH
750 if (s->cc_op == CC_OP_EFLAGS) {
751 return;
752 }
436ff2d2 753 if (s->cc_op == CC_OP_CLR) {
d2fe51bd 754 tcg_gen_movi_tl(cpu_cc_src, CC_Z | CC_P);
436ff2d2
RH
755 set_cc_op(s, CC_OP_EFLAGS);
756 return;
757 }
db9f2597
RH
758
759 TCGV_UNUSED(zero);
760 dst = cpu_cc_dst;
761 src1 = cpu_cc_src;
988c3eb0 762 src2 = cpu_cc_src2;
db9f2597
RH
763
764 /* Take care to not read values that are not live. */
765 live = cc_op_live[s->cc_op] & ~USES_CC_SRCT;
988c3eb0 766 dead = live ^ (USES_CC_DST | USES_CC_SRC | USES_CC_SRC2);
db9f2597
RH
767 if (dead) {
768 zero = tcg_const_tl(0);
769 if (dead & USES_CC_DST) {
770 dst = zero;
771 }
772 if (dead & USES_CC_SRC) {
773 src1 = zero;
774 }
988c3eb0
RH
775 if (dead & USES_CC_SRC2) {
776 src2 = zero;
777 }
db9f2597
RH
778 }
779
773cdfcc 780 gen_update_cc_op(s);
988c3eb0 781 gen_helper_cc_compute_all(cpu_cc_src, dst, src1, src2, cpu_cc_op);
d229edce 782 set_cc_op(s, CC_OP_EFLAGS);
db9f2597
RH
783
784 if (dead) {
785 tcg_temp_free(zero);
786 }
8e1c85e3
FB
787}
788
bec93d72
RH
789typedef struct CCPrepare {
790 TCGCond cond;
791 TCGv reg;
792 TCGv reg2;
793 target_ulong imm;
794 target_ulong mask;
795 bool use_reg2;
796 bool no_setcond;
797} CCPrepare;
798
06847f1f 799/* compute eflags.C to reg */
bec93d72 800static CCPrepare gen_prepare_eflags_c(DisasContext *s, TCGv reg)
06847f1f
RH
801{
802 TCGv t0, t1;
bec93d72 803 int size, shift;
06847f1f
RH
804
805 switch (s->cc_op) {
806 case CC_OP_SUBB ... CC_OP_SUBQ:
a3251186 807 /* (DATA_TYPE)CC_SRCT < (DATA_TYPE)CC_SRC */
06847f1f
RH
808 size = s->cc_op - CC_OP_SUBB;
809 t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
810 /* If no temporary was used, be careful not to alias t1 and t0. */
811 t0 = TCGV_EQUAL(t1, cpu_cc_src) ? cpu_tmp0 : reg;
a3251186 812 tcg_gen_mov_tl(t0, cpu_cc_srcT);
06847f1f
RH
813 gen_extu(size, t0);
814 goto add_sub;
815
816 case CC_OP_ADDB ... CC_OP_ADDQ:
817 /* (DATA_TYPE)CC_DST < (DATA_TYPE)CC_SRC */
818 size = s->cc_op - CC_OP_ADDB;
819 t1 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
820 t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
821 add_sub:
bec93d72
RH
822 return (CCPrepare) { .cond = TCG_COND_LTU, .reg = t0,
823 .reg2 = t1, .mask = -1, .use_reg2 = true };
06847f1f 824
06847f1f 825 case CC_OP_LOGICB ... CC_OP_LOGICQ:
436ff2d2 826 case CC_OP_CLR:
bec93d72 827 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
06847f1f
RH
828
829 case CC_OP_INCB ... CC_OP_INCQ:
830 case CC_OP_DECB ... CC_OP_DECQ:
bec93d72
RH
831 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
832 .mask = -1, .no_setcond = true };
06847f1f
RH
833
834 case CC_OP_SHLB ... CC_OP_SHLQ:
835 /* (CC_SRC >> (DATA_BITS - 1)) & 1 */
836 size = s->cc_op - CC_OP_SHLB;
bec93d72
RH
837 shift = (8 << size) - 1;
838 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
839 .mask = (target_ulong)1 << shift };
06847f1f
RH
840
841 case CC_OP_MULB ... CC_OP_MULQ:
bec93d72
RH
842 return (CCPrepare) { .cond = TCG_COND_NE,
843 .reg = cpu_cc_src, .mask = -1 };
06847f1f 844
bc4b43dc
RH
845 case CC_OP_BMILGB ... CC_OP_BMILGQ:
846 size = s->cc_op - CC_OP_BMILGB;
847 t0 = gen_ext_tl(reg, cpu_cc_src, size, false);
848 return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
849
cd7f97ca
RH
850 case CC_OP_ADCX:
851 case CC_OP_ADCOX:
852 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_dst,
853 .mask = -1, .no_setcond = true };
854
06847f1f
RH
855 case CC_OP_EFLAGS:
856 case CC_OP_SARB ... CC_OP_SARQ:
857 /* CC_SRC & 1 */
bec93d72
RH
858 return (CCPrepare) { .cond = TCG_COND_NE,
859 .reg = cpu_cc_src, .mask = CC_C };
06847f1f
RH
860
861 default:
862 /* The need to compute only C from CC_OP_DYNAMIC is important
863 in efficiently implementing e.g. INC at the start of a TB. */
864 gen_update_cc_op(s);
988c3eb0
RH
865 gen_helper_cc_compute_c(reg, cpu_cc_dst, cpu_cc_src,
866 cpu_cc_src2, cpu_cc_op);
bec93d72
RH
867 return (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
868 .mask = -1, .no_setcond = true };
06847f1f
RH
869 }
870}
871
1608ecca 872/* compute eflags.P to reg */
bec93d72 873static CCPrepare gen_prepare_eflags_p(DisasContext *s, TCGv reg)
1608ecca 874{
d229edce 875 gen_compute_eflags(s);
bec93d72
RH
876 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
877 .mask = CC_P };
1608ecca
PB
878}
879
880/* compute eflags.S to reg */
bec93d72 881static CCPrepare gen_prepare_eflags_s(DisasContext *s, TCGv reg)
1608ecca 882{
086c4077
RH
883 switch (s->cc_op) {
884 case CC_OP_DYNAMIC:
885 gen_compute_eflags(s);
886 /* FALLTHRU */
887 case CC_OP_EFLAGS:
cd7f97ca
RH
888 case CC_OP_ADCX:
889 case CC_OP_ADOX:
890 case CC_OP_ADCOX:
bec93d72
RH
891 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
892 .mask = CC_S };
436ff2d2
RH
893 case CC_OP_CLR:
894 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
086c4077
RH
895 default:
896 {
d67dc9e6 897 TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
086c4077 898 TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, true);
bec93d72 899 return (CCPrepare) { .cond = TCG_COND_LT, .reg = t0, .mask = -1 };
086c4077 900 }
086c4077 901 }
1608ecca
PB
902}
903
904/* compute eflags.O to reg */
bec93d72 905static CCPrepare gen_prepare_eflags_o(DisasContext *s, TCGv reg)
1608ecca 906{
cd7f97ca
RH
907 switch (s->cc_op) {
908 case CC_OP_ADOX:
909 case CC_OP_ADCOX:
910 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src2,
911 .mask = -1, .no_setcond = true };
436ff2d2
RH
912 case CC_OP_CLR:
913 return (CCPrepare) { .cond = TCG_COND_NEVER, .mask = -1 };
cd7f97ca
RH
914 default:
915 gen_compute_eflags(s);
916 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
917 .mask = CC_O };
918 }
1608ecca
PB
919}
920
921/* compute eflags.Z to reg */
bec93d72 922static CCPrepare gen_prepare_eflags_z(DisasContext *s, TCGv reg)
1608ecca 923{
086c4077
RH
924 switch (s->cc_op) {
925 case CC_OP_DYNAMIC:
926 gen_compute_eflags(s);
927 /* FALLTHRU */
928 case CC_OP_EFLAGS:
cd7f97ca
RH
929 case CC_OP_ADCX:
930 case CC_OP_ADOX:
931 case CC_OP_ADCOX:
bec93d72
RH
932 return (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
933 .mask = CC_Z };
436ff2d2
RH
934 case CC_OP_CLR:
935 return (CCPrepare) { .cond = TCG_COND_ALWAYS, .mask = -1 };
086c4077
RH
936 default:
937 {
d67dc9e6 938 TCGMemOp size = (s->cc_op - CC_OP_ADDB) & 3;
086c4077 939 TCGv t0 = gen_ext_tl(reg, cpu_cc_dst, size, false);
bec93d72 940 return (CCPrepare) { .cond = TCG_COND_EQ, .reg = t0, .mask = -1 };
086c4077 941 }
bec93d72
RH
942 }
943}
944
c365395e
PB
945/* perform a conditional store into register 'reg' according to jump opcode
946 value 'b'. In the fast case, T0 is guaranted not to be used. */
276e6b5f 947static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
8e1c85e3 948{
d67dc9e6
RH
949 int inv, jcc_op, cond;
950 TCGMemOp size;
276e6b5f 951 CCPrepare cc;
c365395e
PB
952 TCGv t0;
953
954 inv = b & 1;
8e1c85e3 955 jcc_op = (b >> 1) & 7;
c365395e
PB
956
957 switch (s->cc_op) {
69d1aa31
RH
958 case CC_OP_SUBB ... CC_OP_SUBQ:
959 /* We optimize relational operators for the cmp/jcc case. */
c365395e
PB
960 size = s->cc_op - CC_OP_SUBB;
961 switch (jcc_op) {
962 case JCC_BE:
a3251186 963 tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
c365395e
PB
964 gen_extu(size, cpu_tmp4);
965 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, false);
276e6b5f
RH
966 cc = (CCPrepare) { .cond = TCG_COND_LEU, .reg = cpu_tmp4,
967 .reg2 = t0, .mask = -1, .use_reg2 = true };
c365395e 968 break;
8e1c85e3 969
c365395e 970 case JCC_L:
276e6b5f 971 cond = TCG_COND_LT;
c365395e
PB
972 goto fast_jcc_l;
973 case JCC_LE:
276e6b5f 974 cond = TCG_COND_LE;
c365395e 975 fast_jcc_l:
a3251186 976 tcg_gen_mov_tl(cpu_tmp4, cpu_cc_srcT);
c365395e
PB
977 gen_exts(size, cpu_tmp4);
978 t0 = gen_ext_tl(cpu_tmp0, cpu_cc_src, size, true);
276e6b5f
RH
979 cc = (CCPrepare) { .cond = cond, .reg = cpu_tmp4,
980 .reg2 = t0, .mask = -1, .use_reg2 = true };
c365395e 981 break;
8e1c85e3 982
c365395e 983 default:
8e1c85e3 984 goto slow_jcc;
c365395e 985 }
8e1c85e3 986 break;
c365395e 987
8e1c85e3
FB
988 default:
989 slow_jcc:
69d1aa31
RH
990 /* This actually generates good code for JC, JZ and JS. */
991 switch (jcc_op) {
992 case JCC_O:
993 cc = gen_prepare_eflags_o(s, reg);
994 break;
995 case JCC_B:
996 cc = gen_prepare_eflags_c(s, reg);
997 break;
998 case JCC_Z:
999 cc = gen_prepare_eflags_z(s, reg);
1000 break;
1001 case JCC_BE:
1002 gen_compute_eflags(s);
1003 cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = cpu_cc_src,
1004 .mask = CC_Z | CC_C };
1005 break;
1006 case JCC_S:
1007 cc = gen_prepare_eflags_s(s, reg);
1008 break;
1009 case JCC_P:
1010 cc = gen_prepare_eflags_p(s, reg);
1011 break;
1012 case JCC_L:
1013 gen_compute_eflags(s);
1014 if (TCGV_EQUAL(reg, cpu_cc_src)) {
1015 reg = cpu_tmp0;
1016 }
1017 tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
1018 tcg_gen_xor_tl(reg, reg, cpu_cc_src);
1019 cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
1020 .mask = CC_S };
1021 break;
1022 default:
1023 case JCC_LE:
1024 gen_compute_eflags(s);
1025 if (TCGV_EQUAL(reg, cpu_cc_src)) {
1026 reg = cpu_tmp0;
1027 }
1028 tcg_gen_shri_tl(reg, cpu_cc_src, 4); /* CC_O -> CC_S */
1029 tcg_gen_xor_tl(reg, reg, cpu_cc_src);
1030 cc = (CCPrepare) { .cond = TCG_COND_NE, .reg = reg,
1031 .mask = CC_S | CC_Z };
1032 break;
1033 }
c365395e 1034 break;
8e1c85e3 1035 }
276e6b5f
RH
1036
1037 if (inv) {
1038 cc.cond = tcg_invert_cond(cc.cond);
1039 }
1040 return cc;
8e1c85e3
FB
1041}
1042
cc8b6f5b
PB
1043static void gen_setcc1(DisasContext *s, int b, TCGv reg)
1044{
1045 CCPrepare cc = gen_prepare_cc(s, b, reg);
1046
1047 if (cc.no_setcond) {
1048 if (cc.cond == TCG_COND_EQ) {
1049 tcg_gen_xori_tl(reg, cc.reg, 1);
1050 } else {
1051 tcg_gen_mov_tl(reg, cc.reg);
1052 }
1053 return;
1054 }
1055
1056 if (cc.cond == TCG_COND_NE && !cc.use_reg2 && cc.imm == 0 &&
1057 cc.mask != 0 && (cc.mask & (cc.mask - 1)) == 0) {
1058 tcg_gen_shri_tl(reg, cc.reg, ctztl(cc.mask));
1059 tcg_gen_andi_tl(reg, reg, 1);
1060 return;
1061 }
1062 if (cc.mask != -1) {
1063 tcg_gen_andi_tl(reg, cc.reg, cc.mask);
1064 cc.reg = reg;
1065 }
1066 if (cc.use_reg2) {
1067 tcg_gen_setcond_tl(cc.cond, reg, cc.reg, cc.reg2);
1068 } else {
1069 tcg_gen_setcondi_tl(cc.cond, reg, cc.reg, cc.imm);
1070 }
1071}
1072
1073static inline void gen_compute_eflags_c(DisasContext *s, TCGv reg)
1074{
1075 gen_setcc1(s, JCC_B << 1, reg);
1076}
276e6b5f 1077
8e1c85e3
FB
1078/* generate a conditional jump to label 'l1' according to jump opcode
1079 value 'b'. In the fast case, T0 is guaranted not to be used. */
dc259201
RH
1080static inline void gen_jcc1_noeob(DisasContext *s, int b, int l1)
1081{
1082 CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
1083
1084 if (cc.mask != -1) {
1085 tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
1086 cc.reg = cpu_T[0];
1087 }
1088 if (cc.use_reg2) {
1089 tcg_gen_brcond_tl(cc.cond, cc.reg, cc.reg2, l1);
1090 } else {
1091 tcg_gen_brcondi_tl(cc.cond, cc.reg, cc.imm, l1);
1092 }
1093}
1094
1095/* Generate a conditional jump to label 'l1' according to jump opcode
1096 value 'b'. In the fast case, T0 is guaranted not to be used.
1097 A translation block must end soon. */
b27fc131 1098static inline void gen_jcc1(DisasContext *s, int b, int l1)
8e1c85e3 1099{
943131ca 1100 CCPrepare cc = gen_prepare_cc(s, b, cpu_T[0]);
8e1c85e3 1101
dc259201 1102 gen_update_cc_op(s);
943131ca
PB
1103 if (cc.mask != -1) {
1104 tcg_gen_andi_tl(cpu_T[0], cc.reg, cc.mask);
1105 cc.reg = cpu_T[0];
1106 }
dc259201 1107 set_cc_op(s, CC_OP_DYNAMIC);
943131ca
PB
1108 if (cc.use_reg2) {
1109 tcg_gen_brcond_tl(cc.cond, cc.reg, cc.reg2, l1);
1110 } else {
1111 tcg_gen_brcondi_tl(cc.cond, cc.reg, cc.imm, l1);
8e1c85e3
FB
1112 }
1113}
1114
14ce26e7
FB
1115/* XXX: does not work with gdbstub "ice" single step - not a
1116 serious problem */
1117static int gen_jz_ecx_string(DisasContext *s, target_ulong next_eip)
2c0262af 1118{
14ce26e7
FB
1119 int l1, l2;
1120
1121 l1 = gen_new_label();
1122 l2 = gen_new_label();
1d71ddb1 1123 gen_op_jnz_ecx(s->aflag, l1);
14ce26e7
FB
1124 gen_set_label(l2);
1125 gen_jmp_tb(s, next_eip, 1);
1126 gen_set_label(l1);
1127 return l2;
2c0262af
FB
1128}
1129
d67dc9e6 1130static inline void gen_stos(DisasContext *s, TCGMemOp ot)
2c0262af 1131{
c56baccf 1132 gen_op_mov_v_reg(MO_32, cpu_T[0], R_EAX);
2c0262af 1133 gen_string_movl_A0_EDI(s);
fd8ca9f6 1134 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
6e0d8677 1135 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1136 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
1137}
1138
d67dc9e6 1139static inline void gen_lods(DisasContext *s, TCGMemOp ot)
2c0262af
FB
1140{
1141 gen_string_movl_A0_ESI(s);
909be183 1142 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
480a762d 1143 gen_op_mov_reg_v(ot, R_EAX, cpu_T[0]);
6e0d8677 1144 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1145 gen_op_add_reg_T0(s->aflag, R_ESI);
2c0262af
FB
1146}
1147
d67dc9e6 1148static inline void gen_scas(DisasContext *s, TCGMemOp ot)
2c0262af 1149{
2c0262af 1150 gen_string_movl_A0_EDI(s);
0f712e10 1151 gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
63633fe6 1152 gen_op(s, OP_CMPL, ot, R_EAX);
6e0d8677 1153 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1154 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
1155}
1156
d67dc9e6 1157static inline void gen_cmps(DisasContext *s, TCGMemOp ot)
2c0262af 1158{
2c0262af 1159 gen_string_movl_A0_EDI(s);
0f712e10 1160 gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
63633fe6
RH
1161 gen_string_movl_A0_ESI(s);
1162 gen_op(s, OP_CMPL, ot, OR_TMP0);
6e0d8677 1163 gen_op_movl_T0_Dshift(ot);
1d71ddb1
RH
1164 gen_op_add_reg_T0(s->aflag, R_ESI);
1165 gen_op_add_reg_T0(s->aflag, R_EDI);
2c0262af
FB
1166}
1167
d67dc9e6 1168static inline void gen_ins(DisasContext *s, TCGMemOp ot)
2c0262af 1169{
2e70f6ef
PB
1170 if (use_icount)
1171 gen_io_start();
2c0262af 1172 gen_string_movl_A0_EDI(s);
6e0d8677
FB
1173 /* Note: we must do this dummy write first to be restartable in
1174 case of page fault. */
97212c88 1175 tcg_gen_movi_tl(cpu_T[0], 0);
fd8ca9f6 1176 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
24b9c00f 1177 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
b6abf97d 1178 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
a7812ae4 1179 gen_helper_in_func(ot, cpu_T[0], cpu_tmp2_i32);
fd8ca9f6 1180 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
6e0d8677 1181 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1182 gen_op_add_reg_T0(s->aflag, R_EDI);
2e70f6ef
PB
1183 if (use_icount)
1184 gen_io_end();
2c0262af
FB
1185}
1186
d67dc9e6 1187static inline void gen_outs(DisasContext *s, TCGMemOp ot)
2c0262af 1188{
2e70f6ef
PB
1189 if (use_icount)
1190 gen_io_start();
2c0262af 1191 gen_string_movl_A0_ESI(s);
909be183 1192 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
b8b6a50b 1193
24b9c00f 1194 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[R_EDX]);
b6abf97d
FB
1195 tcg_gen_andi_i32(cpu_tmp2_i32, cpu_tmp2_i32, 0xffff);
1196 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[0]);
a7812ae4 1197 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
b8b6a50b 1198
6e0d8677 1199 gen_op_movl_T0_Dshift(ot);
1d71ddb1 1200 gen_op_add_reg_T0(s->aflag, R_ESI);
2e70f6ef
PB
1201 if (use_icount)
1202 gen_io_end();
2c0262af
FB
1203}
1204
1205/* same method as Valgrind : we generate jumps to current or next
1206 instruction */
1207#define GEN_REPZ(op) \
d67dc9e6 1208static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
14ce26e7 1209 target_ulong cur_eip, target_ulong next_eip) \
2c0262af 1210{ \
14ce26e7 1211 int l2;\
2c0262af 1212 gen_update_cc_op(s); \
14ce26e7 1213 l2 = gen_jz_ecx_string(s, next_eip); \
2c0262af 1214 gen_ ## op(s, ot); \
1d71ddb1 1215 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
2c0262af
FB
1216 /* a loop would cause two single step exceptions if ECX = 1 \
1217 before rep string_insn */ \
1218 if (!s->jmp_opt) \
1d71ddb1 1219 gen_op_jz_ecx(s->aflag, l2); \
2c0262af
FB
1220 gen_jmp(s, cur_eip); \
1221}
1222
1223#define GEN_REPZ2(op) \
d67dc9e6 1224static inline void gen_repz_ ## op(DisasContext *s, TCGMemOp ot, \
14ce26e7
FB
1225 target_ulong cur_eip, \
1226 target_ulong next_eip, \
2c0262af
FB
1227 int nz) \
1228{ \
14ce26e7 1229 int l2;\
2c0262af 1230 gen_update_cc_op(s); \
14ce26e7 1231 l2 = gen_jz_ecx_string(s, next_eip); \
2c0262af 1232 gen_ ## op(s, ot); \
1d71ddb1 1233 gen_op_add_reg_im(s->aflag, R_ECX, -1); \
773cdfcc 1234 gen_update_cc_op(s); \
b27fc131 1235 gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2); \
2c0262af 1236 if (!s->jmp_opt) \
1d71ddb1 1237 gen_op_jz_ecx(s->aflag, l2); \
2c0262af
FB
1238 gen_jmp(s, cur_eip); \
1239}
1240
1241GEN_REPZ(movs)
1242GEN_REPZ(stos)
1243GEN_REPZ(lods)
1244GEN_REPZ(ins)
1245GEN_REPZ(outs)
1246GEN_REPZ2(scas)
1247GEN_REPZ2(cmps)
1248
a7812ae4
PB
1249static void gen_helper_fp_arith_ST0_FT0(int op)
1250{
1251 switch (op) {
d3eb5eae
BS
1252 case 0:
1253 gen_helper_fadd_ST0_FT0(cpu_env);
1254 break;
1255 case 1:
1256 gen_helper_fmul_ST0_FT0(cpu_env);
1257 break;
1258 case 2:
1259 gen_helper_fcom_ST0_FT0(cpu_env);
1260 break;
1261 case 3:
1262 gen_helper_fcom_ST0_FT0(cpu_env);
1263 break;
1264 case 4:
1265 gen_helper_fsub_ST0_FT0(cpu_env);
1266 break;
1267 case 5:
1268 gen_helper_fsubr_ST0_FT0(cpu_env);
1269 break;
1270 case 6:
1271 gen_helper_fdiv_ST0_FT0(cpu_env);
1272 break;
1273 case 7:
1274 gen_helper_fdivr_ST0_FT0(cpu_env);
1275 break;
a7812ae4
PB
1276 }
1277}
2c0262af
FB
1278
1279/* NOTE the exception in "r" op ordering */
a7812ae4
PB
1280static void gen_helper_fp_arith_STN_ST0(int op, int opreg)
1281{
1282 TCGv_i32 tmp = tcg_const_i32(opreg);
1283 switch (op) {
d3eb5eae
BS
1284 case 0:
1285 gen_helper_fadd_STN_ST0(cpu_env, tmp);
1286 break;
1287 case 1:
1288 gen_helper_fmul_STN_ST0(cpu_env, tmp);
1289 break;
1290 case 4:
1291 gen_helper_fsubr_STN_ST0(cpu_env, tmp);
1292 break;
1293 case 5:
1294 gen_helper_fsub_STN_ST0(cpu_env, tmp);
1295 break;
1296 case 6:
1297 gen_helper_fdivr_STN_ST0(cpu_env, tmp);
1298 break;
1299 case 7:
1300 gen_helper_fdiv_STN_ST0(cpu_env, tmp);
1301 break;
a7812ae4
PB
1302 }
1303}
2c0262af
FB
1304
1305/* if d == OR_TMP0, it means memory operand (address in A0) */
d67dc9e6 1306static void gen_op(DisasContext *s1, int op, TCGMemOp ot, int d)
2c0262af 1307{
2c0262af 1308 if (d != OR_TMP0) {
c56baccf 1309 gen_op_mov_v_reg(ot, cpu_T[0], d);
2c0262af 1310 } else {
909be183 1311 gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
2c0262af
FB
1312 }
1313 switch(op) {
1314 case OP_ADCL:
cc8b6f5b 1315 gen_compute_eflags_c(s1, cpu_tmp4);
cad3a37d
FB
1316 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1317 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
d4faa3e0 1318 gen_op_st_rm_T0_A0(s1, ot, d);
988c3eb0
RH
1319 gen_op_update3_cc(cpu_tmp4);
1320 set_cc_op(s1, CC_OP_ADCB + ot);
cad3a37d 1321 break;
2c0262af 1322 case OP_SBBL:
cc8b6f5b 1323 gen_compute_eflags_c(s1, cpu_tmp4);
cad3a37d
FB
1324 tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1325 tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_tmp4);
d4faa3e0 1326 gen_op_st_rm_T0_A0(s1, ot, d);
988c3eb0
RH
1327 gen_op_update3_cc(cpu_tmp4);
1328 set_cc_op(s1, CC_OP_SBBB + ot);
cad3a37d 1329 break;
2c0262af 1330 case OP_ADDL:
fd5185ec 1331 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
d4faa3e0 1332 gen_op_st_rm_T0_A0(s1, ot, d);
cad3a37d 1333 gen_op_update2_cc();
3ca51d07 1334 set_cc_op(s1, CC_OP_ADDB + ot);
2c0262af
FB
1335 break;
1336 case OP_SUBL:
a3251186 1337 tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
57fec1fe 1338 tcg_gen_sub_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
d4faa3e0 1339 gen_op_st_rm_T0_A0(s1, ot, d);
cad3a37d 1340 gen_op_update2_cc();
3ca51d07 1341 set_cc_op(s1, CC_OP_SUBB + ot);
2c0262af
FB
1342 break;
1343 default:
1344 case OP_ANDL:
57fec1fe 1345 tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
d4faa3e0 1346 gen_op_st_rm_T0_A0(s1, ot, d);
cad3a37d 1347 gen_op_update1_cc();
3ca51d07 1348 set_cc_op(s1, CC_OP_LOGICB + ot);
57fec1fe 1349 break;
2c0262af 1350 case OP_ORL:
57fec1fe 1351 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
d4faa3e0 1352 gen_op_st_rm_T0_A0(s1, ot, d);
cad3a37d 1353 gen_op_update1_cc();
3ca51d07 1354 set_cc_op(s1, CC_OP_LOGICB + ot);
57fec1fe 1355 break;
2c0262af 1356 case OP_XORL:
57fec1fe 1357 tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
d4faa3e0 1358 gen_op_st_rm_T0_A0(s1, ot, d);
cad3a37d 1359 gen_op_update1_cc();
3ca51d07 1360 set_cc_op(s1, CC_OP_LOGICB + ot);
2c0262af
FB
1361 break;
1362 case OP_CMPL:
63633fe6 1363 tcg_gen_mov_tl(cpu_cc_src, cpu_T[1]);
a3251186 1364 tcg_gen_mov_tl(cpu_cc_srcT, cpu_T[0]);
63633fe6 1365 tcg_gen_sub_tl(cpu_cc_dst, cpu_T[0], cpu_T[1]);
3ca51d07 1366 set_cc_op(s1, CC_OP_SUBB + ot);
2c0262af
FB
1367 break;
1368 }
b6abf97d
FB
1369}
1370
2c0262af 1371/* if d == OR_TMP0, it means memory operand (address in A0) */
d67dc9e6 1372static void gen_inc(DisasContext *s1, TCGMemOp ot, int d, int c)
2c0262af 1373{
909be183 1374 if (d != OR_TMP0) {
c56baccf 1375 gen_op_mov_v_reg(ot, cpu_T[0], d);
909be183
RH
1376 } else {
1377 gen_op_ld_v(s1, ot, cpu_T[0], cpu_A0);
1378 }
cc8b6f5b 1379 gen_compute_eflags_c(s1, cpu_cc_src);
2c0262af 1380 if (c > 0) {
b6abf97d 1381 tcg_gen_addi_tl(cpu_T[0], cpu_T[0], 1);
3ca51d07 1382 set_cc_op(s1, CC_OP_INCB + ot);
2c0262af 1383 } else {
b6abf97d 1384 tcg_gen_addi_tl(cpu_T[0], cpu_T[0], -1);
3ca51d07 1385 set_cc_op(s1, CC_OP_DECB + ot);
2c0262af 1386 }
d4faa3e0 1387 gen_op_st_rm_T0_A0(s1, ot, d);
cd31fefa 1388 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
2c0262af
FB
1389}
1390
d67dc9e6
RH
1391static void gen_shift_flags(DisasContext *s, TCGMemOp ot, TCGv result,
1392 TCGv shm1, TCGv count, bool is_right)
f437d0a3
RH
1393{
1394 TCGv_i32 z32, s32, oldop;
1395 TCGv z_tl;
1396
1397 /* Store the results into the CC variables. If we know that the
1398 variable must be dead, store unconditionally. Otherwise we'll
1399 need to not disrupt the current contents. */
1400 z_tl = tcg_const_tl(0);
1401 if (cc_op_live[s->cc_op] & USES_CC_DST) {
1402 tcg_gen_movcond_tl(TCG_COND_NE, cpu_cc_dst, count, z_tl,
1403 result, cpu_cc_dst);
1404 } else {
1405 tcg_gen_mov_tl(cpu_cc_dst, result);
1406 }
1407 if (cc_op_live[s->cc_op] & USES_CC_SRC) {
1408 tcg_gen_movcond_tl(TCG_COND_NE, cpu_cc_src, count, z_tl,
1409 shm1, cpu_cc_src);
1410 } else {
1411 tcg_gen_mov_tl(cpu_cc_src, shm1);
1412 }
1413 tcg_temp_free(z_tl);
1414
1415 /* Get the two potential CC_OP values into temporaries. */
1416 tcg_gen_movi_i32(cpu_tmp2_i32, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
1417 if (s->cc_op == CC_OP_DYNAMIC) {
1418 oldop = cpu_cc_op;
1419 } else {
1420 tcg_gen_movi_i32(cpu_tmp3_i32, s->cc_op);
1421 oldop = cpu_tmp3_i32;
1422 }
1423
1424 /* Conditionally store the CC_OP value. */
1425 z32 = tcg_const_i32(0);
1426 s32 = tcg_temp_new_i32();
1427 tcg_gen_trunc_tl_i32(s32, count);
1428 tcg_gen_movcond_i32(TCG_COND_NE, cpu_cc_op, s32, z32, cpu_tmp2_i32, oldop);
1429 tcg_temp_free_i32(z32);
1430 tcg_temp_free_i32(s32);
1431
1432 /* The CC_OP value is no longer predictable. */
1433 set_cc_op(s, CC_OP_DYNAMIC);
1434}
1435
d67dc9e6 1436static void gen_shift_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
b6abf97d 1437 int is_right, int is_arith)
2c0262af 1438{
4ba9938c 1439 target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
3b46e624 1440
b6abf97d 1441 /* load */
82786041 1442 if (op1 == OR_TMP0) {
909be183 1443 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
82786041 1444 } else {
c56baccf 1445 gen_op_mov_v_reg(ot, cpu_T[0], op1);
82786041 1446 }
b6abf97d 1447
a41f62f5
RH
1448 tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
1449 tcg_gen_subi_tl(cpu_tmp0, cpu_T[1], 1);
b6abf97d
FB
1450
1451 if (is_right) {
1452 if (is_arith) {
f484d386 1453 gen_exts(ot, cpu_T[0]);
a41f62f5
RH
1454 tcg_gen_sar_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1455 tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
b6abf97d 1456 } else {
cad3a37d 1457 gen_extu(ot, cpu_T[0]);
a41f62f5
RH
1458 tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1459 tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
b6abf97d
FB
1460 }
1461 } else {
a41f62f5
RH
1462 tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
1463 tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
b6abf97d
FB
1464 }
1465
1466 /* store */
d4faa3e0 1467 gen_op_st_rm_T0_A0(s, ot, op1);
82786041 1468
f437d0a3 1469 gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, cpu_T[1], is_right);
b6abf97d
FB
1470}
1471
d67dc9e6 1472static void gen_shift_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
c1c37968
FB
1473 int is_right, int is_arith)
1474{
4ba9938c 1475 int mask = (ot == MO_64 ? 0x3f : 0x1f);
c1c37968
FB
1476
1477 /* load */
1478 if (op1 == OR_TMP0)
909be183 1479 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
c1c37968 1480 else
c56baccf 1481 gen_op_mov_v_reg(ot, cpu_T[0], op1);
c1c37968
FB
1482
1483 op2 &= mask;
1484 if (op2 != 0) {
1485 if (is_right) {
1486 if (is_arith) {
1487 gen_exts(ot, cpu_T[0]);
2a449d14 1488 tcg_gen_sari_tl(cpu_tmp4, cpu_T[0], op2 - 1);
c1c37968
FB
1489 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], op2);
1490 } else {
1491 gen_extu(ot, cpu_T[0]);
2a449d14 1492 tcg_gen_shri_tl(cpu_tmp4, cpu_T[0], op2 - 1);
c1c37968
FB
1493 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], op2);
1494 }
1495 } else {
2a449d14 1496 tcg_gen_shli_tl(cpu_tmp4, cpu_T[0], op2 - 1);
c1c37968
FB
1497 tcg_gen_shli_tl(cpu_T[0], cpu_T[0], op2);
1498 }
1499 }
1500
1501 /* store */
d4faa3e0
RH
1502 gen_op_st_rm_T0_A0(s, ot, op1);
1503
c1c37968
FB
1504 /* update eflags if non zero shift */
1505 if (op2 != 0) {
2a449d14 1506 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
c1c37968 1507 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
3ca51d07 1508 set_cc_op(s, (is_right ? CC_OP_SARB : CC_OP_SHLB) + ot);
c1c37968
FB
1509 }
1510}
1511
d67dc9e6 1512static void gen_rot_rm_T1(DisasContext *s, TCGMemOp ot, int op1, int is_right)
b6abf97d 1513{
4ba9938c 1514 target_ulong mask = (ot == MO_64 ? 0x3f : 0x1f);
34d80a55 1515 TCGv_i32 t0, t1;
b6abf97d
FB
1516
1517 /* load */
1e4840bf 1518 if (op1 == OR_TMP0) {
909be183 1519 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1e4840bf 1520 } else {
c56baccf 1521 gen_op_mov_v_reg(ot, cpu_T[0], op1);
1e4840bf 1522 }
b6abf97d 1523
34d80a55 1524 tcg_gen_andi_tl(cpu_T[1], cpu_T[1], mask);
b6abf97d 1525
34d80a55 1526 switch (ot) {
4ba9938c 1527 case MO_8:
34d80a55
RH
1528 /* Replicate the 8-bit input so that a 32-bit rotate works. */
1529 tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
1530 tcg_gen_muli_tl(cpu_T[0], cpu_T[0], 0x01010101);
1531 goto do_long;
4ba9938c 1532 case MO_16:
34d80a55
RH
1533 /* Replicate the 16-bit input so that a 32-bit rotate works. */
1534 tcg_gen_deposit_tl(cpu_T[0], cpu_T[0], cpu_T[0], 16, 16);
1535 goto do_long;
1536 do_long:
1537#ifdef TARGET_X86_64
4ba9938c 1538 case MO_32:
34d80a55
RH
1539 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
1540 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
1541 if (is_right) {
1542 tcg_gen_rotr_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
1543 } else {
1544 tcg_gen_rotl_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
1545 }
1546 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
1547 break;
1548#endif
1549 default:
1550 if (is_right) {
1551 tcg_gen_rotr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1552 } else {
1553 tcg_gen_rotl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1554 }
1555 break;
b6abf97d 1556 }
b6abf97d 1557
b6abf97d 1558 /* store */
d4faa3e0 1559 gen_op_st_rm_T0_A0(s, ot, op1);
b6abf97d 1560
34d80a55
RH
1561 /* We'll need the flags computed into CC_SRC. */
1562 gen_compute_eflags(s);
b6abf97d 1563
34d80a55
RH
1564 /* The value that was "rotated out" is now present at the other end
1565 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1566 since we've computed the flags into CC_SRC, these variables are
1567 currently dead. */
b6abf97d 1568 if (is_right) {
34d80a55
RH
1569 tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
1570 tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
089305ac 1571 tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
34d80a55
RH
1572 } else {
1573 tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
1574 tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
b6abf97d 1575 }
34d80a55
RH
1576 tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
1577 tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);
1578
1579 /* Now conditionally store the new CC_OP value. If the shift count
1580 is 0 we keep the CC_OP_EFLAGS setting so that only CC_SRC is live.
1581 Otherwise reuse CC_OP_ADCOX which have the C and O flags split out
1582 exactly as we computed above. */
1583 t0 = tcg_const_i32(0);
1584 t1 = tcg_temp_new_i32();
1585 tcg_gen_trunc_tl_i32(t1, cpu_T[1]);
1586 tcg_gen_movi_i32(cpu_tmp2_i32, CC_OP_ADCOX);
1587 tcg_gen_movi_i32(cpu_tmp3_i32, CC_OP_EFLAGS);
1588 tcg_gen_movcond_i32(TCG_COND_NE, cpu_cc_op, t1, t0,
1589 cpu_tmp2_i32, cpu_tmp3_i32);
1590 tcg_temp_free_i32(t0);
1591 tcg_temp_free_i32(t1);
1592
1593 /* The CC_OP value is no longer predictable. */
1594 set_cc_op(s, CC_OP_DYNAMIC);
b6abf97d
FB
1595}
1596
d67dc9e6 1597static void gen_rot_rm_im(DisasContext *s, TCGMemOp ot, int op1, int op2,
8cd6345d 1598 int is_right)
1599{
4ba9938c 1600 int mask = (ot == MO_64 ? 0x3f : 0x1f);
34d80a55 1601 int shift;
8cd6345d 1602
1603 /* load */
1604 if (op1 == OR_TMP0) {
909be183 1605 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
8cd6345d 1606 } else {
c56baccf 1607 gen_op_mov_v_reg(ot, cpu_T[0], op1);
8cd6345d 1608 }
1609
8cd6345d 1610 op2 &= mask;
8cd6345d 1611 if (op2 != 0) {
34d80a55
RH
1612 switch (ot) {
1613#ifdef TARGET_X86_64
4ba9938c 1614 case MO_32:
34d80a55
RH
1615 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
1616 if (is_right) {
1617 tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
1618 } else {
1619 tcg_gen_rotli_i32(cpu_tmp2_i32, cpu_tmp2_i32, op2);
1620 }
1621 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
1622 break;
1623#endif
1624 default:
1625 if (is_right) {
1626 tcg_gen_rotri_tl(cpu_T[0], cpu_T[0], op2);
1627 } else {
1628 tcg_gen_rotli_tl(cpu_T[0], cpu_T[0], op2);
1629 }
1630 break;
4ba9938c 1631 case MO_8:
34d80a55
RH
1632 mask = 7;
1633 goto do_shifts;
4ba9938c 1634 case MO_16:
34d80a55
RH
1635 mask = 15;
1636 do_shifts:
1637 shift = op2 & mask;
1638 if (is_right) {
1639 shift = mask + 1 - shift;
1640 }
1641 gen_extu(ot, cpu_T[0]);
1642 tcg_gen_shli_tl(cpu_tmp0, cpu_T[0], shift);
1643 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], mask + 1 - shift);
1644 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
1645 break;
8cd6345d 1646 }
8cd6345d 1647 }
1648
1649 /* store */
d4faa3e0 1650 gen_op_st_rm_T0_A0(s, ot, op1);
8cd6345d 1651
1652 if (op2 != 0) {
34d80a55 1653 /* Compute the flags into CC_SRC. */
d229edce 1654 gen_compute_eflags(s);
0ff6addd 1655
34d80a55
RH
1656 /* The value that was "rotated out" is now present at the other end
1657 of the word. Compute C into CC_DST and O into CC_SRC2. Note that
1658 since we've computed the flags into CC_SRC, these variables are
1659 currently dead. */
8cd6345d 1660 if (is_right) {
34d80a55
RH
1661 tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask - 1);
1662 tcg_gen_shri_tl(cpu_cc_dst, cpu_T[0], mask);
38ebb396 1663 tcg_gen_andi_tl(cpu_cc_dst, cpu_cc_dst, 1);
34d80a55
RH
1664 } else {
1665 tcg_gen_shri_tl(cpu_cc_src2, cpu_T[0], mask);
1666 tcg_gen_andi_tl(cpu_cc_dst, cpu_T[0], 1);
8cd6345d 1667 }
34d80a55
RH
1668 tcg_gen_andi_tl(cpu_cc_src2, cpu_cc_src2, 1);
1669 tcg_gen_xor_tl(cpu_cc_src2, cpu_cc_src2, cpu_cc_dst);
1670 set_cc_op(s, CC_OP_ADCOX);
8cd6345d 1671 }
8cd6345d 1672}
1673
b6abf97d 1674/* XXX: add faster immediate = 1 case */
d67dc9e6 1675static void gen_rotc_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
b6abf97d
FB
1676 int is_right)
1677{
d229edce 1678 gen_compute_eflags(s);
c7b3c873 1679 assert(s->cc_op == CC_OP_EFLAGS);
b6abf97d
FB
1680
1681 /* load */
1682 if (op1 == OR_TMP0)
909be183 1683 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
b6abf97d 1684 else
c56baccf 1685 gen_op_mov_v_reg(ot, cpu_T[0], op1);
b6abf97d 1686
a7812ae4
PB
1687 if (is_right) {
1688 switch (ot) {
4ba9938c 1689 case MO_8:
7923057b
BS
1690 gen_helper_rcrb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1691 break;
4ba9938c 1692 case MO_16:
7923057b
BS
1693 gen_helper_rcrw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1694 break;
4ba9938c 1695 case MO_32:
7923057b
BS
1696 gen_helper_rcrl(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1697 break;
a7812ae4 1698#ifdef TARGET_X86_64
4ba9938c 1699 case MO_64:
7923057b
BS
1700 gen_helper_rcrq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1701 break;
a7812ae4 1702#endif
d67dc9e6
RH
1703 default:
1704 tcg_abort();
a7812ae4
PB
1705 }
1706 } else {
1707 switch (ot) {
4ba9938c 1708 case MO_8:
7923057b
BS
1709 gen_helper_rclb(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1710 break;
4ba9938c 1711 case MO_16:
7923057b
BS
1712 gen_helper_rclw(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1713 break;
4ba9938c 1714 case MO_32:
7923057b
BS
1715 gen_helper_rcll(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1716 break;
a7812ae4 1717#ifdef TARGET_X86_64
4ba9938c 1718 case MO_64:
7923057b
BS
1719 gen_helper_rclq(cpu_T[0], cpu_env, cpu_T[0], cpu_T[1]);
1720 break;
a7812ae4 1721#endif
d67dc9e6
RH
1722 default:
1723 tcg_abort();
a7812ae4
PB
1724 }
1725 }
b6abf97d 1726 /* store */
d4faa3e0 1727 gen_op_st_rm_T0_A0(s, ot, op1);
b6abf97d
FB
1728}
1729
1730/* XXX: add faster immediate case */
d67dc9e6 1731static void gen_shiftd_rm_T1(DisasContext *s, TCGMemOp ot, int op1,
f437d0a3 1732 bool is_right, TCGv count_in)
b6abf97d 1733{
4ba9938c 1734 target_ulong mask = (ot == MO_64 ? 63 : 31);
f437d0a3 1735 TCGv count;
b6abf97d
FB
1736
1737 /* load */
1e4840bf 1738 if (op1 == OR_TMP0) {
909be183 1739 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
1e4840bf 1740 } else {
c56baccf 1741 gen_op_mov_v_reg(ot, cpu_T[0], op1);
1e4840bf 1742 }
b6abf97d 1743
f437d0a3
RH
1744 count = tcg_temp_new();
1745 tcg_gen_andi_tl(count, count_in, mask);
1e4840bf 1746
f437d0a3 1747 switch (ot) {
4ba9938c 1748 case MO_16:
f437d0a3
RH
1749 /* Note: we implement the Intel behaviour for shift count > 16.
1750 This means "shrdw C, B, A" shifts A:B:A >> C. Build the B:A
1751 portion by constructing it as a 32-bit value. */
b6abf97d 1752 if (is_right) {
f437d0a3
RH
1753 tcg_gen_deposit_tl(cpu_tmp0, cpu_T[0], cpu_T[1], 16, 16);
1754 tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
1755 tcg_gen_mov_tl(cpu_T[0], cpu_tmp0);
b6abf97d 1756 } else {
f437d0a3 1757 tcg_gen_deposit_tl(cpu_T[1], cpu_T[0], cpu_T[1], 16, 16);
b6abf97d 1758 }
f437d0a3
RH
1759 /* FALLTHRU */
1760#ifdef TARGET_X86_64
4ba9938c 1761 case MO_32:
f437d0a3
RH
1762 /* Concatenate the two 32-bit values and use a 64-bit shift. */
1763 tcg_gen_subi_tl(cpu_tmp0, count, 1);
b6abf97d 1764 if (is_right) {
f437d0a3
RH
1765 tcg_gen_concat_tl_i64(cpu_T[0], cpu_T[0], cpu_T[1]);
1766 tcg_gen_shr_i64(cpu_tmp0, cpu_T[0], cpu_tmp0);
1767 tcg_gen_shr_i64(cpu_T[0], cpu_T[0], count);
1768 } else {
1769 tcg_gen_concat_tl_i64(cpu_T[0], cpu_T[1], cpu_T[0]);
1770 tcg_gen_shl_i64(cpu_tmp0, cpu_T[0], cpu_tmp0);
1771 tcg_gen_shl_i64(cpu_T[0], cpu_T[0], count);
1772 tcg_gen_shri_i64(cpu_tmp0, cpu_tmp0, 32);
1773 tcg_gen_shri_i64(cpu_T[0], cpu_T[0], 32);
1774 }
1775 break;
1776#endif
1777 default:
1778 tcg_gen_subi_tl(cpu_tmp0, count, 1);
1779 if (is_right) {
1780 tcg_gen_shr_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
b6abf97d 1781
f437d0a3
RH
1782 tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
1783 tcg_gen_shr_tl(cpu_T[0], cpu_T[0], count);
1784 tcg_gen_shl_tl(cpu_T[1], cpu_T[1], cpu_tmp4);
b6abf97d 1785 } else {
f437d0a3 1786 tcg_gen_shl_tl(cpu_tmp0, cpu_T[0], cpu_tmp0);
4ba9938c 1787 if (ot == MO_16) {
f437d0a3
RH
1788 /* Only needed if count > 16, for Intel behaviour. */
1789 tcg_gen_subfi_tl(cpu_tmp4, 33, count);
1790 tcg_gen_shr_tl(cpu_tmp4, cpu_T[1], cpu_tmp4);
1791 tcg_gen_or_tl(cpu_tmp0, cpu_tmp0, cpu_tmp4);
1792 }
1793
1794 tcg_gen_subfi_tl(cpu_tmp4, mask + 1, count);
1795 tcg_gen_shl_tl(cpu_T[0], cpu_T[0], count);
1796 tcg_gen_shr_tl(cpu_T[1], cpu_T[1], cpu_tmp4);
b6abf97d 1797 }
f437d0a3
RH
1798 tcg_gen_movi_tl(cpu_tmp4, 0);
1799 tcg_gen_movcond_tl(TCG_COND_EQ, cpu_T[1], count, cpu_tmp4,
1800 cpu_tmp4, cpu_T[1]);
1801 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
1802 break;
b6abf97d 1803 }
b6abf97d 1804
b6abf97d 1805 /* store */
d4faa3e0 1806 gen_op_st_rm_T0_A0(s, ot, op1);
1e4840bf 1807
f437d0a3
RH
1808 gen_shift_flags(s, ot, cpu_T[0], cpu_tmp0, count, is_right);
1809 tcg_temp_free(count);
b6abf97d
FB
1810}
1811
d67dc9e6 1812static void gen_shift(DisasContext *s1, int op, TCGMemOp ot, int d, int s)
b6abf97d
FB
1813{
1814 if (s != OR_TMP1)
c56baccf 1815 gen_op_mov_v_reg(ot, cpu_T[1], s);
b6abf97d
FB
1816 switch(op) {
1817 case OP_ROL:
1818 gen_rot_rm_T1(s1, ot, d, 0);
1819 break;
1820 case OP_ROR:
1821 gen_rot_rm_T1(s1, ot, d, 1);
1822 break;
1823 case OP_SHL:
1824 case OP_SHL1:
1825 gen_shift_rm_T1(s1, ot, d, 0, 0);
1826 break;
1827 case OP_SHR:
1828 gen_shift_rm_T1(s1, ot, d, 1, 0);
1829 break;
1830 case OP_SAR:
1831 gen_shift_rm_T1(s1, ot, d, 1, 1);
1832 break;
1833 case OP_RCL:
1834 gen_rotc_rm_T1(s1, ot, d, 0);
1835 break;
1836 case OP_RCR:
1837 gen_rotc_rm_T1(s1, ot, d, 1);
1838 break;
1839 }
2c0262af
FB
1840}
1841
d67dc9e6 1842static void gen_shifti(DisasContext *s1, int op, TCGMemOp ot, int d, int c)
2c0262af 1843{
c1c37968 1844 switch(op) {
8cd6345d 1845 case OP_ROL:
1846 gen_rot_rm_im(s1, ot, d, c, 0);
1847 break;
1848 case OP_ROR:
1849 gen_rot_rm_im(s1, ot, d, c, 1);
1850 break;
c1c37968
FB
1851 case OP_SHL:
1852 case OP_SHL1:
1853 gen_shift_rm_im(s1, ot, d, c, 0, 0);
1854 break;
1855 case OP_SHR:
1856 gen_shift_rm_im(s1, ot, d, c, 1, 0);
1857 break;
1858 case OP_SAR:
1859 gen_shift_rm_im(s1, ot, d, c, 1, 1);
1860 break;
1861 default:
1862 /* currently not optimized */
0ae657b1 1863 tcg_gen_movi_tl(cpu_T[1], c);
c1c37968
FB
1864 gen_shift(s1, op, ot, d, OR_TMP1);
1865 break;
1866 }
2c0262af
FB
1867}
1868
4eeb3939 1869static void gen_lea_modrm(CPUX86State *env, DisasContext *s, int modrm)
2c0262af 1870{
14ce26e7 1871 target_long disp;
2c0262af 1872 int havesib;
14ce26e7 1873 int base;
2c0262af
FB
1874 int index;
1875 int scale;
2c0262af 1876 int mod, rm, code, override, must_add_seg;
7865eec4 1877 TCGv sum;
2c0262af
FB
1878
1879 override = s->override;
1880 must_add_seg = s->addseg;
1881 if (override >= 0)
1882 must_add_seg = 1;
1883 mod = (modrm >> 6) & 3;
1884 rm = modrm & 7;
1885
1d71ddb1
RH
1886 switch (s->aflag) {
1887 case MO_64:
1888 case MO_32:
2c0262af
FB
1889 havesib = 0;
1890 base = rm;
7865eec4 1891 index = -1;
2c0262af 1892 scale = 0;
3b46e624 1893
2c0262af
FB
1894 if (base == 4) {
1895 havesib = 1;
0af10c86 1896 code = cpu_ldub_code(env, s->pc++);
2c0262af 1897 scale = (code >> 6) & 3;
14ce26e7 1898 index = ((code >> 3) & 7) | REX_X(s);
7865eec4
RH
1899 if (index == 4) {
1900 index = -1; /* no index */
1901 }
14ce26e7 1902 base = (code & 7);
2c0262af 1903 }
14ce26e7 1904 base |= REX_B(s);
2c0262af
FB
1905
1906 switch (mod) {
1907 case 0:
14ce26e7 1908 if ((base & 7) == 5) {
2c0262af 1909 base = -1;
0af10c86 1910 disp = (int32_t)cpu_ldl_code(env, s->pc);
2c0262af 1911 s->pc += 4;
14ce26e7
FB
1912 if (CODE64(s) && !havesib) {
1913 disp += s->pc + s->rip_offset;
1914 }
2c0262af
FB
1915 } else {
1916 disp = 0;
1917 }
1918 break;
1919 case 1:
0af10c86 1920 disp = (int8_t)cpu_ldub_code(env, s->pc++);
2c0262af
FB
1921 break;
1922 default:
1923 case 2:
0af10c86 1924 disp = (int32_t)cpu_ldl_code(env, s->pc);
2c0262af
FB
1925 s->pc += 4;
1926 break;
1927 }
3b46e624 1928
7865eec4
RH
1929 /* For correct popl handling with esp. */
1930 if (base == R_ESP && s->popl_esp_hack) {
1931 disp += s->popl_esp_hack;
1932 }
1933
1934 /* Compute the address, with a minimum number of TCG ops. */
1935 TCGV_UNUSED(sum);
1936 if (index >= 0) {
1937 if (scale == 0) {
1938 sum = cpu_regs[index];
1939 } else {
1940 tcg_gen_shli_tl(cpu_A0, cpu_regs[index], scale);
1941 sum = cpu_A0;
14ce26e7 1942 }
7865eec4
RH
1943 if (base >= 0) {
1944 tcg_gen_add_tl(cpu_A0, sum, cpu_regs[base]);
1945 sum = cpu_A0;
14ce26e7 1946 }
7865eec4
RH
1947 } else if (base >= 0) {
1948 sum = cpu_regs[base];
2c0262af 1949 }
7865eec4
RH
1950 if (TCGV_IS_UNUSED(sum)) {
1951 tcg_gen_movi_tl(cpu_A0, disp);
1952 } else {
1953 tcg_gen_addi_tl(cpu_A0, sum, disp);
2c0262af 1954 }
7865eec4 1955
2c0262af
FB
1956 if (must_add_seg) {
1957 if (override < 0) {
7865eec4 1958 if (base == R_EBP || base == R_ESP) {
2c0262af 1959 override = R_SS;
7865eec4 1960 } else {
2c0262af 1961 override = R_DS;
7865eec4 1962 }
2c0262af 1963 }
7865eec4
RH
1964
1965 tcg_gen_ld_tl(cpu_tmp0, cpu_env,
1966 offsetof(CPUX86State, segs[override].base));
1967 if (CODE64(s)) {
1d71ddb1 1968 if (s->aflag == MO_32) {
7865eec4
RH
1969 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
1970 }
1971 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
4eeb3939 1972 return;
14ce26e7 1973 }
7865eec4
RH
1974
1975 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
1976 }
1977
1d71ddb1 1978 if (s->aflag == MO_32) {
7865eec4 1979 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
2c0262af 1980 }
1d71ddb1
RH
1981 break;
1982
1983 case MO_16:
2c0262af
FB
1984 switch (mod) {
1985 case 0:
1986 if (rm == 6) {
0af10c86 1987 disp = cpu_lduw_code(env, s->pc);
2c0262af 1988 s->pc += 2;
3250cff8 1989 tcg_gen_movi_tl(cpu_A0, disp);
2c0262af
FB
1990 rm = 0; /* avoid SS override */
1991 goto no_rm;
1992 } else {
1993 disp = 0;
1994 }
1995 break;
1996 case 1:
0af10c86 1997 disp = (int8_t)cpu_ldub_code(env, s->pc++);
2c0262af
FB
1998 break;
1999 default:
2000 case 2:
7effd625 2001 disp = (int16_t)cpu_lduw_code(env, s->pc);
2c0262af
FB
2002 s->pc += 2;
2003 break;
2004 }
7effd625
RH
2005
2006 sum = cpu_A0;
2007 switch (rm) {
2c0262af 2008 case 0:
7effd625 2009 tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBX], cpu_regs[R_ESI]);
2c0262af
FB
2010 break;
2011 case 1:
7effd625 2012 tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBX], cpu_regs[R_EDI]);
2c0262af
FB
2013 break;
2014 case 2:
7effd625 2015 tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBP], cpu_regs[R_ESI]);
2c0262af
FB
2016 break;
2017 case 3:
7effd625 2018 tcg_gen_add_tl(cpu_A0, cpu_regs[R_EBP], cpu_regs[R_EDI]);
2c0262af
FB
2019 break;
2020 case 4:
7effd625 2021 sum = cpu_regs[R_ESI];
2c0262af
FB
2022 break;
2023 case 5:
7effd625 2024 sum = cpu_regs[R_EDI];
2c0262af
FB
2025 break;
2026 case 6:
7effd625 2027 sum = cpu_regs[R_EBP];
2c0262af
FB
2028 break;
2029 default:
2030 case 7:
7effd625 2031 sum = cpu_regs[R_EBX];
2c0262af
FB
2032 break;
2033 }
7effd625 2034 tcg_gen_addi_tl(cpu_A0, sum, disp);
a7e5c7de 2035 tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2c0262af
FB
2036 no_rm:
2037 if (must_add_seg) {
2038 if (override < 0) {
7effd625 2039 if (rm == 2 || rm == 3 || rm == 6) {
2c0262af 2040 override = R_SS;
7effd625 2041 } else {
2c0262af 2042 override = R_DS;
7effd625 2043 }
2c0262af 2044 }
7162ab21 2045 gen_op_addl_A0_seg(s, override);
2c0262af 2046 }
1d71ddb1
RH
2047 break;
2048
2049 default:
2050 tcg_abort();
2c0262af 2051 }
2c0262af
FB
2052}
2053
0af10c86 2054static void gen_nop_modrm(CPUX86State *env, DisasContext *s, int modrm)
e17a36ce
FB
2055{
2056 int mod, rm, base, code;
2057
2058 mod = (modrm >> 6) & 3;
2059 if (mod == 3)
2060 return;
2061 rm = modrm & 7;
2062
1d71ddb1
RH
2063 switch (s->aflag) {
2064 case MO_64:
2065 case MO_32:
e17a36ce 2066 base = rm;
3b46e624 2067
e17a36ce 2068 if (base == 4) {
0af10c86 2069 code = cpu_ldub_code(env, s->pc++);
e17a36ce
FB
2070 base = (code & 7);
2071 }
3b46e624 2072
e17a36ce
FB
2073 switch (mod) {
2074 case 0:
2075 if (base == 5) {
2076 s->pc += 4;
2077 }
2078 break;
2079 case 1:
2080 s->pc++;
2081 break;
2082 default:
2083 case 2:
2084 s->pc += 4;
2085 break;
2086 }
1d71ddb1
RH
2087 break;
2088
2089 case MO_16:
e17a36ce
FB
2090 switch (mod) {
2091 case 0:
2092 if (rm == 6) {
2093 s->pc += 2;
2094 }
2095 break;
2096 case 1:
2097 s->pc++;
2098 break;
2099 default:
2100 case 2:
2101 s->pc += 2;
2102 break;
2103 }
1d71ddb1
RH
2104 break;
2105
2106 default:
2107 tcg_abort();
e17a36ce
FB
2108 }
2109}
2110
664e0f19
FB
2111/* used for LEA and MOV AX, mem */
2112static void gen_add_A0_ds_seg(DisasContext *s)
2113{
2114 int override, must_add_seg;
2115 must_add_seg = s->addseg;
2116 override = R_DS;
2117 if (s->override >= 0) {
2118 override = s->override;
2119 must_add_seg = 1;
664e0f19
FB
2120 }
2121 if (must_add_seg) {
8f091a59
FB
2122#ifdef TARGET_X86_64
2123 if (CODE64(s)) {
57fec1fe 2124 gen_op_addq_A0_seg(override);
5fafdf24 2125 } else
8f091a59
FB
2126#endif
2127 {
7162ab21 2128 gen_op_addl_A0_seg(s, override);
8f091a59 2129 }
664e0f19
FB
2130 }
2131}
2132
222a3336 2133/* generate modrm memory load or store of 'reg'. TMP0 is used if reg ==
2c0262af 2134 OR_TMP0 */
0af10c86 2135static void gen_ldst_modrm(CPUX86State *env, DisasContext *s, int modrm,
d67dc9e6 2136 TCGMemOp ot, int reg, int is_store)
2c0262af 2137{
4eeb3939 2138 int mod, rm;
2c0262af
FB
2139
2140 mod = (modrm >> 6) & 3;
14ce26e7 2141 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
2142 if (mod == 3) {
2143 if (is_store) {
2144 if (reg != OR_TMP0)
c56baccf 2145 gen_op_mov_v_reg(ot, cpu_T[0], reg);
480a762d 2146 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
2c0262af 2147 } else {
c56baccf 2148 gen_op_mov_v_reg(ot, cpu_T[0], rm);
2c0262af 2149 if (reg != OR_TMP0)
480a762d 2150 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
2c0262af
FB
2151 }
2152 } else {
4eeb3939 2153 gen_lea_modrm(env, s, modrm);
2c0262af
FB
2154 if (is_store) {
2155 if (reg != OR_TMP0)
c56baccf 2156 gen_op_mov_v_reg(ot, cpu_T[0], reg);
fd8ca9f6 2157 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 2158 } else {
909be183 2159 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 2160 if (reg != OR_TMP0)
480a762d 2161 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
2c0262af
FB
2162 }
2163 }
2164}
2165
d67dc9e6 2166static inline uint32_t insn_get(CPUX86State *env, DisasContext *s, TCGMemOp ot)
2c0262af
FB
2167{
2168 uint32_t ret;
2169
d67dc9e6 2170 switch (ot) {
4ba9938c 2171 case MO_8:
0af10c86 2172 ret = cpu_ldub_code(env, s->pc);
2c0262af
FB
2173 s->pc++;
2174 break;
4ba9938c 2175 case MO_16:
0af10c86 2176 ret = cpu_lduw_code(env, s->pc);
2c0262af
FB
2177 s->pc += 2;
2178 break;
4ba9938c 2179 case MO_32:
d67dc9e6
RH
2180#ifdef TARGET_X86_64
2181 case MO_64:
2182#endif
0af10c86 2183 ret = cpu_ldl_code(env, s->pc);
2c0262af
FB
2184 s->pc += 4;
2185 break;
d67dc9e6
RH
2186 default:
2187 tcg_abort();
2c0262af
FB
2188 }
2189 return ret;
2190}
2191
d67dc9e6 2192static inline int insn_const_size(TCGMemOp ot)
14ce26e7 2193{
4ba9938c 2194 if (ot <= MO_32) {
14ce26e7 2195 return 1 << ot;
4ba9938c 2196 } else {
14ce26e7 2197 return 4;
4ba9938c 2198 }
14ce26e7
FB
2199}
2200
6e256c93
FB
2201static inline void gen_goto_tb(DisasContext *s, int tb_num, target_ulong eip)
2202{
2203 TranslationBlock *tb;
2204 target_ulong pc;
2205
2206 pc = s->cs_base + eip;
2207 tb = s->tb;
2208 /* NOTE: we handle the case where the TB spans two pages here */
2209 if ((pc & TARGET_PAGE_MASK) == (tb->pc & TARGET_PAGE_MASK) ||
2210 (pc & TARGET_PAGE_MASK) == ((s->pc - 1) & TARGET_PAGE_MASK)) {
2211 /* jump to same page: we can use a direct jump */
57fec1fe 2212 tcg_gen_goto_tb(tb_num);
6e256c93 2213 gen_jmp_im(eip);
8cfd0495 2214 tcg_gen_exit_tb((uintptr_t)tb + tb_num);
6e256c93
FB
2215 } else {
2216 /* jump to another page: currently not optimized */
2217 gen_jmp_im(eip);
2218 gen_eob(s);
2219 }
2220}
2221
5fafdf24 2222static inline void gen_jcc(DisasContext *s, int b,
14ce26e7 2223 target_ulong val, target_ulong next_eip)
2c0262af 2224{
b27fc131 2225 int l1, l2;
3b46e624 2226
2c0262af 2227 if (s->jmp_opt) {
14ce26e7 2228 l1 = gen_new_label();
b27fc131 2229 gen_jcc1(s, b, l1);
dc259201 2230
6e256c93 2231 gen_goto_tb(s, 0, next_eip);
14ce26e7
FB
2232
2233 gen_set_label(l1);
6e256c93 2234 gen_goto_tb(s, 1, val);
5779406a 2235 s->is_jmp = DISAS_TB_JUMP;
2c0262af 2236 } else {
14ce26e7
FB
2237 l1 = gen_new_label();
2238 l2 = gen_new_label();
b27fc131 2239 gen_jcc1(s, b, l1);
8e1c85e3 2240
14ce26e7 2241 gen_jmp_im(next_eip);
8e1c85e3
FB
2242 tcg_gen_br(l2);
2243
14ce26e7
FB
2244 gen_set_label(l1);
2245 gen_jmp_im(val);
2246 gen_set_label(l2);
2c0262af
FB
2247 gen_eob(s);
2248 }
2249}
2250
d67dc9e6 2251static void gen_cmovcc1(CPUX86State *env, DisasContext *s, TCGMemOp ot, int b,
f32d3781
PB
2252 int modrm, int reg)
2253{
57eb0cc8 2254 CCPrepare cc;
f32d3781 2255
57eb0cc8 2256 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
f32d3781 2257
57eb0cc8
RH
2258 cc = gen_prepare_cc(s, b, cpu_T[1]);
2259 if (cc.mask != -1) {
2260 TCGv t0 = tcg_temp_new();
2261 tcg_gen_andi_tl(t0, cc.reg, cc.mask);
2262 cc.reg = t0;
2263 }
2264 if (!cc.use_reg2) {
2265 cc.reg2 = tcg_const_tl(cc.imm);
f32d3781
PB
2266 }
2267
57eb0cc8
RH
2268 tcg_gen_movcond_tl(cc.cond, cpu_T[0], cc.reg, cc.reg2,
2269 cpu_T[0], cpu_regs[reg]);
480a762d 2270 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
57eb0cc8
RH
2271
2272 if (cc.mask != -1) {
2273 tcg_temp_free(cc.reg);
2274 }
2275 if (!cc.use_reg2) {
2276 tcg_temp_free(cc.reg2);
2277 }
f32d3781
PB
2278}
2279
3bd7da9e
FB
2280static inline void gen_op_movl_T0_seg(int seg_reg)
2281{
2282 tcg_gen_ld32u_tl(cpu_T[0], cpu_env,
2283 offsetof(CPUX86State,segs[seg_reg].selector));
2284}
2285
2286static inline void gen_op_movl_seg_T0_vm(int seg_reg)
2287{
2288 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffff);
2289 tcg_gen_st32_tl(cpu_T[0], cpu_env,
2290 offsetof(CPUX86State,segs[seg_reg].selector));
2291 tcg_gen_shli_tl(cpu_T[0], cpu_T[0], 4);
2292 tcg_gen_st_tl(cpu_T[0], cpu_env,
2293 offsetof(CPUX86State,segs[seg_reg].base));
2294}
2295
2c0262af
FB
2296/* move T0 to seg_reg and compute if the CPU state may change. Never
2297 call this function with seg_reg == R_CS */
14ce26e7 2298static void gen_movl_seg_T0(DisasContext *s, int seg_reg, target_ulong cur_eip)
2c0262af 2299{
3415a4dd
FB
2300 if (s->pe && !s->vm86) {
2301 /* XXX: optimize by finding processor state dynamically */
773cdfcc 2302 gen_update_cc_op(s);
14ce26e7 2303 gen_jmp_im(cur_eip);
b6abf97d 2304 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 2305 gen_helper_load_seg(cpu_env, tcg_const_i32(seg_reg), cpu_tmp2_i32);
dc196a57
FB
2306 /* abort translation because the addseg value may change or
2307 because ss32 may change. For R_SS, translation must always
2308 stop as a special handling must be done to disable hardware
2309 interrupts for the next instruction */
2310 if (seg_reg == R_SS || (s->code32 && seg_reg < R_FS))
5779406a 2311 s->is_jmp = DISAS_TB_JUMP;
3415a4dd 2312 } else {
3bd7da9e 2313 gen_op_movl_seg_T0_vm(seg_reg);
dc196a57 2314 if (seg_reg == R_SS)
5779406a 2315 s->is_jmp = DISAS_TB_JUMP;
3415a4dd 2316 }
2c0262af
FB
2317}
2318
0573fbfc
TS
2319static inline int svm_is_rep(int prefixes)
2320{
2321 return ((prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) ? 8 : 0);
2322}
2323
872929aa 2324static inline void
0573fbfc 2325gen_svm_check_intercept_param(DisasContext *s, target_ulong pc_start,
b8b6a50b 2326 uint32_t type, uint64_t param)
0573fbfc 2327{
872929aa
FB
2328 /* no SVM activated; fast case */
2329 if (likely(!(s->flags & HF_SVMI_MASK)))
2330 return;
773cdfcc 2331 gen_update_cc_op(s);
872929aa 2332 gen_jmp_im(pc_start - s->cs_base);
052e80d5 2333 gen_helper_svm_check_intercept_param(cpu_env, tcg_const_i32(type),
a7812ae4 2334 tcg_const_i64(param));
0573fbfc
TS
2335}
2336
872929aa 2337static inline void
0573fbfc
TS
2338gen_svm_check_intercept(DisasContext *s, target_ulong pc_start, uint64_t type)
2339{
872929aa 2340 gen_svm_check_intercept_param(s, pc_start, type, 0);
0573fbfc
TS
2341}
2342
4f31916f
FB
2343static inline void gen_stack_update(DisasContext *s, int addend)
2344{
14ce26e7
FB
2345#ifdef TARGET_X86_64
2346 if (CODE64(s)) {
d3f4bbe3 2347 gen_op_add_reg_im(MO_64, R_ESP, addend);
14ce26e7
FB
2348 } else
2349#endif
4f31916f 2350 if (s->ss32) {
d3f4bbe3 2351 gen_op_add_reg_im(MO_32, R_ESP, addend);
4f31916f 2352 } else {
d3f4bbe3 2353 gen_op_add_reg_im(MO_16, R_ESP, addend);
4f31916f
FB
2354 }
2355}
2356
432baffe
RH
2357/* Generate a push. It depends on ss32, addseg and dflag. */
2358static void gen_push_v(DisasContext *s, TCGv val)
2c0262af 2359{
432baffe
RH
2360 TCGMemOp a_ot, d_ot = mo_pushpop(s, s->dflag);
2361 int size = 1 << d_ot;
2362 TCGv new_esp = cpu_A0;
2363
2364 tcg_gen_subi_tl(cpu_A0, cpu_regs[R_ESP], size);
2c0262af 2365
14ce26e7 2366 if (CODE64(s)) {
432baffe
RH
2367 a_ot = MO_64;
2368 } else if (s->ss32) {
2369 a_ot = MO_32;
2370 if (s->addseg) {
2371 new_esp = cpu_tmp4;
2372 tcg_gen_mov_tl(new_esp, cpu_A0);
7162ab21 2373 gen_op_addl_A0_seg(s, R_SS);
432baffe
RH
2374 } else {
2375 tcg_gen_ext32u_tl(cpu_A0, cpu_A0);
2c0262af 2376 }
432baffe
RH
2377 } else {
2378 a_ot = MO_16;
2379 new_esp = cpu_tmp4;
2380 tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
2381 tcg_gen_mov_tl(new_esp, cpu_A0);
2382 gen_op_addl_A0_seg(s, R_SS);
2c0262af 2383 }
432baffe
RH
2384
2385 gen_op_st_v(s, d_ot, val, cpu_A0);
2386 gen_op_mov_reg_v(a_ot, R_ESP, new_esp);
2c0262af
FB
2387}
2388
4f31916f 2389/* two step pop is necessary for precise exceptions */
8e31d234 2390static TCGMemOp gen_pop_T0(DisasContext *s)
2c0262af 2391{
8e31d234
RH
2392 TCGMemOp d_ot = mo_pushpop(s, s->dflag);
2393 TCGv addr = cpu_A0;
2394
14ce26e7 2395 if (CODE64(s)) {
8e31d234
RH
2396 addr = cpu_regs[R_ESP];
2397 } else if (!s->ss32) {
2398 tcg_gen_ext16u_tl(cpu_A0, cpu_regs[R_ESP]);
2399 gen_op_addl_A0_seg(s, R_SS);
2400 } else if (s->addseg) {
2401 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_ESP]);
2402 gen_op_addl_A0_seg(s, R_SS);
2403 } else {
2404 tcg_gen_ext32u_tl(cpu_A0, cpu_regs[R_ESP]);
2c0262af 2405 }
8e31d234
RH
2406
2407 gen_op_ld_v(s, d_ot, cpu_T[0], addr);
2408 return d_ot;
2c0262af
FB
2409}
2410
8e31d234 2411static void gen_pop_update(DisasContext *s, TCGMemOp ot)
2c0262af 2412{
8e31d234 2413 gen_stack_update(s, 1 << ot);
2c0262af
FB
2414}
2415
2416static void gen_stack_A0(DisasContext *s)
2417{
57fec1fe 2418 gen_op_movl_A0_reg(R_ESP);
2c0262af 2419 if (!s->ss32)
a7e5c7de 2420 tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
bbf662ee 2421 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2c0262af 2422 if (s->addseg)
7162ab21 2423 gen_op_addl_A0_seg(s, R_SS);
2c0262af
FB
2424}
2425
2426/* NOTE: wrap around in 16 bit not fully handled */
2427static void gen_pusha(DisasContext *s)
2428{
2429 int i;
57fec1fe 2430 gen_op_movl_A0_reg(R_ESP);
ab4e4aec 2431 gen_op_addl_A0_im(-8 << s->dflag);
2c0262af 2432 if (!s->ss32)
a7e5c7de 2433 tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
bbf662ee 2434 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
2c0262af 2435 if (s->addseg)
7162ab21 2436 gen_op_addl_A0_seg(s, R_SS);
2c0262af 2437 for(i = 0;i < 8; i++) {
c56baccf 2438 gen_op_mov_v_reg(MO_32, cpu_T[0], 7 - i);
ab4e4aec
RH
2439 gen_op_st_v(s, s->dflag, cpu_T[0], cpu_A0);
2440 gen_op_addl_A0_im(1 << s->dflag);
2c0262af 2441 }
68773f84 2442 gen_op_mov_reg_v(MO_16 + s->ss32, R_ESP, cpu_T[1]);
2c0262af
FB
2443}
2444
2445/* NOTE: wrap around in 16 bit not fully handled */
2446static void gen_popa(DisasContext *s)
2447{
2448 int i;
57fec1fe 2449 gen_op_movl_A0_reg(R_ESP);
2c0262af 2450 if (!s->ss32)
a7e5c7de 2451 tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
bbf662ee 2452 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
ab4e4aec 2453 tcg_gen_addi_tl(cpu_T[1], cpu_T[1], 8 << s->dflag);
2c0262af 2454 if (s->addseg)
7162ab21 2455 gen_op_addl_A0_seg(s, R_SS);
2c0262af
FB
2456 for(i = 0;i < 8; i++) {
2457 /* ESP is not reloaded */
2458 if (i != 3) {
ab4e4aec 2459 gen_op_ld_v(s, s->dflag, cpu_T[0], cpu_A0);
480a762d 2460 gen_op_mov_reg_v(s->dflag, 7 - i, cpu_T[0]);
2c0262af 2461 }
ab4e4aec 2462 gen_op_addl_A0_im(1 << s->dflag);
2c0262af 2463 }
68773f84 2464 gen_op_mov_reg_v(MO_16 + s->ss32, R_ESP, cpu_T[1]);
2c0262af
FB
2465}
2466
2c0262af
FB
2467static void gen_enter(DisasContext *s, int esp_addend, int level)
2468{
ab4e4aec
RH
2469 TCGMemOp ot = mo_pushpop(s, s->dflag);
2470 int opsize = 1 << ot;
2c0262af 2471
2c0262af 2472 level &= 0x1f;
8f091a59
FB
2473#ifdef TARGET_X86_64
2474 if (CODE64(s)) {
57fec1fe 2475 gen_op_movl_A0_reg(R_ESP);
8f091a59 2476 gen_op_addq_A0_im(-opsize);
bbf662ee 2477 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
8f091a59
FB
2478
2479 /* push bp */
c56baccf 2480 gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
fd8ca9f6 2481 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
8f091a59 2482 if (level) {
b5b38f61 2483 /* XXX: must save state */
2999a0b2 2484 gen_helper_enter64_level(cpu_env, tcg_const_i32(level),
4ba9938c 2485 tcg_const_i32((ot == MO_64)),
a7812ae4 2486 cpu_T[1]);
8f091a59 2487 }
68773f84 2488 gen_op_mov_reg_v(ot, R_EBP, cpu_T[1]);
bbf662ee 2489 tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
68773f84 2490 gen_op_mov_reg_v(MO_64, R_ESP, cpu_T[1]);
5fafdf24 2491 } else
8f091a59
FB
2492#endif
2493 {
57fec1fe 2494 gen_op_movl_A0_reg(R_ESP);
8f091a59
FB
2495 gen_op_addl_A0_im(-opsize);
2496 if (!s->ss32)
a7e5c7de 2497 tcg_gen_ext16u_tl(cpu_A0, cpu_A0);
bbf662ee 2498 tcg_gen_mov_tl(cpu_T[1], cpu_A0);
8f091a59 2499 if (s->addseg)
7162ab21 2500 gen_op_addl_A0_seg(s, R_SS);
8f091a59 2501 /* push bp */
c56baccf 2502 gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
fd8ca9f6 2503 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
8f091a59 2504 if (level) {
b5b38f61 2505 /* XXX: must save state */
2999a0b2 2506 gen_helper_enter_level(cpu_env, tcg_const_i32(level),
ab4e4aec 2507 tcg_const_i32(s->dflag - 1),
a7812ae4 2508 cpu_T[1]);
8f091a59 2509 }
68773f84 2510 gen_op_mov_reg_v(ot, R_EBP, cpu_T[1]);
bbf662ee 2511 tcg_gen_addi_tl(cpu_T[1], cpu_T[1], -esp_addend + (-opsize * level));
68773f84 2512 gen_op_mov_reg_v(MO_16 + s->ss32, R_ESP, cpu_T[1]);
2c0262af 2513 }
2c0262af
FB
2514}
2515
14ce26e7 2516static void gen_exception(DisasContext *s, int trapno, target_ulong cur_eip)
2c0262af 2517{
773cdfcc 2518 gen_update_cc_op(s);
14ce26e7 2519 gen_jmp_im(cur_eip);
77b2bc2c 2520 gen_helper_raise_exception(cpu_env, tcg_const_i32(trapno));
5779406a 2521 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2522}
2523
2524/* an interrupt is different from an exception because of the
7f75ffd3 2525 privilege checks */
5fafdf24 2526static void gen_interrupt(DisasContext *s, int intno,
14ce26e7 2527 target_ulong cur_eip, target_ulong next_eip)
2c0262af 2528{
773cdfcc 2529 gen_update_cc_op(s);
14ce26e7 2530 gen_jmp_im(cur_eip);
77b2bc2c 2531 gen_helper_raise_interrupt(cpu_env, tcg_const_i32(intno),
a7812ae4 2532 tcg_const_i32(next_eip - cur_eip));
5779406a 2533 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2534}
2535
14ce26e7 2536static void gen_debug(DisasContext *s, target_ulong cur_eip)
2c0262af 2537{
773cdfcc 2538 gen_update_cc_op(s);
14ce26e7 2539 gen_jmp_im(cur_eip);
4a7443be 2540 gen_helper_debug(cpu_env);
5779406a 2541 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2542}
2543
2544/* generate a generic end of block. Trace exception is also generated
2545 if needed */
2546static void gen_eob(DisasContext *s)
2547{
773cdfcc 2548 gen_update_cc_op(s);
a2cc3b24 2549 if (s->tb->flags & HF_INHIBIT_IRQ_MASK) {
f0967a1a 2550 gen_helper_reset_inhibit_irq(cpu_env);
a2cc3b24 2551 }
a2397807 2552 if (s->tb->flags & HF_RF_MASK) {
f0967a1a 2553 gen_helper_reset_rf(cpu_env);
a2397807 2554 }
34865134 2555 if (s->singlestep_enabled) {
4a7443be 2556 gen_helper_debug(cpu_env);
34865134 2557 } else if (s->tf) {
4a7443be 2558 gen_helper_single_step(cpu_env);
2c0262af 2559 } else {
57fec1fe 2560 tcg_gen_exit_tb(0);
2c0262af 2561 }
5779406a 2562 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
2563}
2564
2565/* generate a jump to eip. No segment change must happen before as a
2566 direct call to the next block may occur */
14ce26e7 2567static void gen_jmp_tb(DisasContext *s, target_ulong eip, int tb_num)
2c0262af 2568{
a3251186
RH
2569 gen_update_cc_op(s);
2570 set_cc_op(s, CC_OP_DYNAMIC);
2c0262af 2571 if (s->jmp_opt) {
6e256c93 2572 gen_goto_tb(s, tb_num, eip);
5779406a 2573 s->is_jmp = DISAS_TB_JUMP;
2c0262af 2574 } else {
14ce26e7 2575 gen_jmp_im(eip);
2c0262af
FB
2576 gen_eob(s);
2577 }
2578}
2579
14ce26e7
FB
2580static void gen_jmp(DisasContext *s, target_ulong eip)
2581{
2582 gen_jmp_tb(s, eip, 0);
2583}
2584
323d1876 2585static inline void gen_ldq_env_A0(DisasContext *s, int offset)
8686c490 2586{
3c5f4116 2587 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
b6abf97d 2588 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset);
8686c490 2589}
664e0f19 2590
323d1876 2591static inline void gen_stq_env_A0(DisasContext *s, int offset)
8686c490 2592{
b6abf97d 2593 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset);
3523e4bd 2594 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
8686c490 2595}
664e0f19 2596
323d1876 2597static inline void gen_ldo_env_A0(DisasContext *s, int offset)
8686c490 2598{
5c42a7cd 2599 int mem_index = s->mem_index;
3c5f4116 2600 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
b6abf97d 2601 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
8686c490 2602 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
3c5f4116 2603 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
b6abf97d 2604 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
8686c490 2605}
14ce26e7 2606
323d1876 2607static inline void gen_sto_env_A0(DisasContext *s, int offset)
8686c490 2608{
5c42a7cd 2609 int mem_index = s->mem_index;
b6abf97d 2610 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(0)));
3523e4bd 2611 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, mem_index, MO_LEQ);
8686c490 2612 tcg_gen_addi_tl(cpu_tmp0, cpu_A0, 8);
b6abf97d 2613 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, offset + offsetof(XMMReg, XMM_Q(1)));
3523e4bd 2614 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_tmp0, mem_index, MO_LEQ);
8686c490 2615}
14ce26e7 2616
5af45186
FB
2617static inline void gen_op_movo(int d_offset, int s_offset)
2618{
b6abf97d
FB
2619 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
2620 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
2621 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset + 8);
2622 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset + 8);
5af45186
FB
2623}
2624
2625static inline void gen_op_movq(int d_offset, int s_offset)
2626{
b6abf97d
FB
2627 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env, s_offset);
2628 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
5af45186
FB
2629}
2630
2631static inline void gen_op_movl(int d_offset, int s_offset)
2632{
b6abf97d
FB
2633 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env, s_offset);
2634 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, d_offset);
5af45186
FB
2635}
2636
2637static inline void gen_op_movq_env_0(int d_offset)
2638{
b6abf97d
FB
2639 tcg_gen_movi_i64(cpu_tmp1_i64, 0);
2640 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env, d_offset);
5af45186 2641}
664e0f19 2642
d3eb5eae
BS
2643typedef void (*SSEFunc_i_ep)(TCGv_i32 val, TCGv_ptr env, TCGv_ptr reg);
2644typedef void (*SSEFunc_l_ep)(TCGv_i64 val, TCGv_ptr env, TCGv_ptr reg);
2645typedef void (*SSEFunc_0_epi)(TCGv_ptr env, TCGv_ptr reg, TCGv_i32 val);
2646typedef void (*SSEFunc_0_epl)(TCGv_ptr env, TCGv_ptr reg, TCGv_i64 val);
2647typedef void (*SSEFunc_0_epp)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b);
2648typedef void (*SSEFunc_0_eppi)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2649 TCGv_i32 val);
c4baa050 2650typedef void (*SSEFunc_0_ppi)(TCGv_ptr reg_a, TCGv_ptr reg_b, TCGv_i32 val);
d3eb5eae
BS
2651typedef void (*SSEFunc_0_eppt)(TCGv_ptr env, TCGv_ptr reg_a, TCGv_ptr reg_b,
2652 TCGv val);
c4baa050 2653
5af45186
FB
2654#define SSE_SPECIAL ((void *)1)
2655#define SSE_DUMMY ((void *)2)
664e0f19 2656
a7812ae4
PB
2657#define MMX_OP2(x) { gen_helper_ ## x ## _mmx, gen_helper_ ## x ## _xmm }
2658#define SSE_FOP(x) { gen_helper_ ## x ## ps, gen_helper_ ## x ## pd, \
2659 gen_helper_ ## x ## ss, gen_helper_ ## x ## sd, }
5af45186 2660
d3eb5eae 2661static const SSEFunc_0_epp sse_op_table1[256][4] = {
a35f3ec7
AJ
2662 /* 3DNow! extensions */
2663 [0x0e] = { SSE_DUMMY }, /* femms */
2664 [0x0f] = { SSE_DUMMY }, /* pf... */
664e0f19
FB
2665 /* pure SSE operations */
2666 [0x10] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
2667 [0x11] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movups, movupd, movss, movsd */
465e9838 2668 [0x12] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd, movsldup, movddup */
664e0f19 2669 [0x13] = { SSE_SPECIAL, SSE_SPECIAL }, /* movlps, movlpd */
a7812ae4
PB
2670 [0x14] = { gen_helper_punpckldq_xmm, gen_helper_punpcklqdq_xmm },
2671 [0x15] = { gen_helper_punpckhdq_xmm, gen_helper_punpckhqdq_xmm },
664e0f19
FB
2672 [0x16] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd, movshdup */
2673 [0x17] = { SSE_SPECIAL, SSE_SPECIAL }, /* movhps, movhpd */
2674
2675 [0x28] = { SSE_SPECIAL, SSE_SPECIAL }, /* movaps, movapd */
2676 [0x29] = { SSE_SPECIAL, SSE_SPECIAL }, /* movaps, movapd */
2677 [0x2a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtpi2ps, cvtpi2pd, cvtsi2ss, cvtsi2sd */
d9f4bb27 2678 [0x2b] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movntps, movntpd, movntss, movntsd */
664e0f19
FB
2679 [0x2c] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvttps2pi, cvttpd2pi, cvttsd2si, cvttss2si */
2680 [0x2d] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* cvtps2pi, cvtpd2pi, cvtsd2si, cvtss2si */
a7812ae4
PB
2681 [0x2e] = { gen_helper_ucomiss, gen_helper_ucomisd },
2682 [0x2f] = { gen_helper_comiss, gen_helper_comisd },
664e0f19
FB
2683 [0x50] = { SSE_SPECIAL, SSE_SPECIAL }, /* movmskps, movmskpd */
2684 [0x51] = SSE_FOP(sqrt),
a7812ae4
PB
2685 [0x52] = { gen_helper_rsqrtps, NULL, gen_helper_rsqrtss, NULL },
2686 [0x53] = { gen_helper_rcpps, NULL, gen_helper_rcpss, NULL },
2687 [0x54] = { gen_helper_pand_xmm, gen_helper_pand_xmm }, /* andps, andpd */
2688 [0x55] = { gen_helper_pandn_xmm, gen_helper_pandn_xmm }, /* andnps, andnpd */
2689 [0x56] = { gen_helper_por_xmm, gen_helper_por_xmm }, /* orps, orpd */
2690 [0x57] = { gen_helper_pxor_xmm, gen_helper_pxor_xmm }, /* xorps, xorpd */
664e0f19
FB
2691 [0x58] = SSE_FOP(add),
2692 [0x59] = SSE_FOP(mul),
a7812ae4
PB
2693 [0x5a] = { gen_helper_cvtps2pd, gen_helper_cvtpd2ps,
2694 gen_helper_cvtss2sd, gen_helper_cvtsd2ss },
2695 [0x5b] = { gen_helper_cvtdq2ps, gen_helper_cvtps2dq, gen_helper_cvttps2dq },
664e0f19
FB
2696 [0x5c] = SSE_FOP(sub),
2697 [0x5d] = SSE_FOP(min),
2698 [0x5e] = SSE_FOP(div),
2699 [0x5f] = SSE_FOP(max),
2700
2701 [0xc2] = SSE_FOP(cmpeq),
d3eb5eae
BS
2702 [0xc6] = { (SSEFunc_0_epp)gen_helper_shufps,
2703 (SSEFunc_0_epp)gen_helper_shufpd }, /* XXX: casts */
664e0f19 2704
7073fbad
RH
2705 /* SSSE3, SSE4, MOVBE, CRC32, BMI1, BMI2, ADX. */
2706 [0x38] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
2707 [0x3a] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
4242b1bd 2708
664e0f19
FB
2709 /* MMX ops and their SSE extensions */
2710 [0x60] = MMX_OP2(punpcklbw),
2711 [0x61] = MMX_OP2(punpcklwd),
2712 [0x62] = MMX_OP2(punpckldq),
2713 [0x63] = MMX_OP2(packsswb),
2714 [0x64] = MMX_OP2(pcmpgtb),
2715 [0x65] = MMX_OP2(pcmpgtw),
2716 [0x66] = MMX_OP2(pcmpgtl),
2717 [0x67] = MMX_OP2(packuswb),
2718 [0x68] = MMX_OP2(punpckhbw),
2719 [0x69] = MMX_OP2(punpckhwd),
2720 [0x6a] = MMX_OP2(punpckhdq),
2721 [0x6b] = MMX_OP2(packssdw),
a7812ae4
PB
2722 [0x6c] = { NULL, gen_helper_punpcklqdq_xmm },
2723 [0x6d] = { NULL, gen_helper_punpckhqdq_xmm },
664e0f19
FB
2724 [0x6e] = { SSE_SPECIAL, SSE_SPECIAL }, /* movd mm, ea */
2725 [0x6f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, , movqdu */
d3eb5eae
BS
2726 [0x70] = { (SSEFunc_0_epp)gen_helper_pshufw_mmx,
2727 (SSEFunc_0_epp)gen_helper_pshufd_xmm,
2728 (SSEFunc_0_epp)gen_helper_pshufhw_xmm,
2729 (SSEFunc_0_epp)gen_helper_pshuflw_xmm }, /* XXX: casts */
664e0f19
FB
2730 [0x71] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftw */
2731 [0x72] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftd */
2732 [0x73] = { SSE_SPECIAL, SSE_SPECIAL }, /* shiftq */
2733 [0x74] = MMX_OP2(pcmpeqb),
2734 [0x75] = MMX_OP2(pcmpeqw),
2735 [0x76] = MMX_OP2(pcmpeql),
a35f3ec7 2736 [0x77] = { SSE_DUMMY }, /* emms */
d9f4bb27
AP
2737 [0x78] = { NULL, SSE_SPECIAL, NULL, SSE_SPECIAL }, /* extrq_i, insertq_i */
2738 [0x79] = { NULL, gen_helper_extrq_r, NULL, gen_helper_insertq_r },
a7812ae4
PB
2739 [0x7c] = { NULL, gen_helper_haddpd, NULL, gen_helper_haddps },
2740 [0x7d] = { NULL, gen_helper_hsubpd, NULL, gen_helper_hsubps },
664e0f19
FB
2741 [0x7e] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movd, movd, , movq */
2742 [0x7f] = { SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL }, /* movq, movdqa, movdqu */
2743 [0xc4] = { SSE_SPECIAL, SSE_SPECIAL }, /* pinsrw */
2744 [0xc5] = { SSE_SPECIAL, SSE_SPECIAL }, /* pextrw */
a7812ae4 2745 [0xd0] = { NULL, gen_helper_addsubpd, NULL, gen_helper_addsubps },
664e0f19
FB
2746 [0xd1] = MMX_OP2(psrlw),
2747 [0xd2] = MMX_OP2(psrld),
2748 [0xd3] = MMX_OP2(psrlq),
2749 [0xd4] = MMX_OP2(paddq),
2750 [0xd5] = MMX_OP2(pmullw),
2751 [0xd6] = { NULL, SSE_SPECIAL, SSE_SPECIAL, SSE_SPECIAL },
2752 [0xd7] = { SSE_SPECIAL, SSE_SPECIAL }, /* pmovmskb */
2753 [0xd8] = MMX_OP2(psubusb),
2754 [0xd9] = MMX_OP2(psubusw),
2755 [0xda] = MMX_OP2(pminub),
2756 [0xdb] = MMX_OP2(pand),
2757 [0xdc] = MMX_OP2(paddusb),
2758 [0xdd] = MMX_OP2(paddusw),
2759 [0xde] = MMX_OP2(pmaxub),
2760 [0xdf] = MMX_OP2(pandn),
2761 [0xe0] = MMX_OP2(pavgb),
2762 [0xe1] = MMX_OP2(psraw),
2763 [0xe2] = MMX_OP2(psrad),
2764 [0xe3] = MMX_OP2(pavgw),
2765 [0xe4] = MMX_OP2(pmulhuw),
2766 [0xe5] = MMX_OP2(pmulhw),
a7812ae4 2767 [0xe6] = { NULL, gen_helper_cvttpd2dq, gen_helper_cvtdq2pd, gen_helper_cvtpd2dq },
664e0f19
FB
2768 [0xe7] = { SSE_SPECIAL , SSE_SPECIAL }, /* movntq, movntq */
2769 [0xe8] = MMX_OP2(psubsb),
2770 [0xe9] = MMX_OP2(psubsw),
2771 [0xea] = MMX_OP2(pminsw),
2772 [0xeb] = MMX_OP2(por),
2773 [0xec] = MMX_OP2(paddsb),
2774 [0xed] = MMX_OP2(paddsw),
2775 [0xee] = MMX_OP2(pmaxsw),
2776 [0xef] = MMX_OP2(pxor),
465e9838 2777 [0xf0] = { NULL, NULL, NULL, SSE_SPECIAL }, /* lddqu */
664e0f19
FB
2778 [0xf1] = MMX_OP2(psllw),
2779 [0xf2] = MMX_OP2(pslld),
2780 [0xf3] = MMX_OP2(psllq),
2781 [0xf4] = MMX_OP2(pmuludq),
2782 [0xf5] = MMX_OP2(pmaddwd),
2783 [0xf6] = MMX_OP2(psadbw),
d3eb5eae
BS
2784 [0xf7] = { (SSEFunc_0_epp)gen_helper_maskmov_mmx,
2785 (SSEFunc_0_epp)gen_helper_maskmov_xmm }, /* XXX: casts */
664e0f19
FB
2786 [0xf8] = MMX_OP2(psubb),
2787 [0xf9] = MMX_OP2(psubw),
2788 [0xfa] = MMX_OP2(psubl),
2789 [0xfb] = MMX_OP2(psubq),
2790 [0xfc] = MMX_OP2(paddb),
2791 [0xfd] = MMX_OP2(paddw),
2792 [0xfe] = MMX_OP2(paddl),
2793};
2794
d3eb5eae 2795static const SSEFunc_0_epp sse_op_table2[3 * 8][2] = {
664e0f19
FB
2796 [0 + 2] = MMX_OP2(psrlw),
2797 [0 + 4] = MMX_OP2(psraw),
2798 [0 + 6] = MMX_OP2(psllw),
2799 [8 + 2] = MMX_OP2(psrld),
2800 [8 + 4] = MMX_OP2(psrad),
2801 [8 + 6] = MMX_OP2(pslld),
2802 [16 + 2] = MMX_OP2(psrlq),
a7812ae4 2803 [16 + 3] = { NULL, gen_helper_psrldq_xmm },
664e0f19 2804 [16 + 6] = MMX_OP2(psllq),
a7812ae4 2805 [16 + 7] = { NULL, gen_helper_pslldq_xmm },
664e0f19
FB
2806};
2807
d3eb5eae 2808static const SSEFunc_0_epi sse_op_table3ai[] = {
a7812ae4 2809 gen_helper_cvtsi2ss,
11f8cdbc 2810 gen_helper_cvtsi2sd
c4baa050 2811};
a7812ae4 2812
11f8cdbc 2813#ifdef TARGET_X86_64
d3eb5eae 2814static const SSEFunc_0_epl sse_op_table3aq[] = {
11f8cdbc
SW
2815 gen_helper_cvtsq2ss,
2816 gen_helper_cvtsq2sd
2817};
2818#endif
2819
d3eb5eae 2820static const SSEFunc_i_ep sse_op_table3bi[] = {
a7812ae4 2821 gen_helper_cvttss2si,
a7812ae4 2822 gen_helper_cvtss2si,
bedc2ac1 2823 gen_helper_cvttsd2si,
11f8cdbc 2824 gen_helper_cvtsd2si
664e0f19 2825};
3b46e624 2826
11f8cdbc 2827#ifdef TARGET_X86_64
d3eb5eae 2828static const SSEFunc_l_ep sse_op_table3bq[] = {
11f8cdbc 2829 gen_helper_cvttss2sq,
11f8cdbc 2830 gen_helper_cvtss2sq,
bedc2ac1 2831 gen_helper_cvttsd2sq,
11f8cdbc
SW
2832 gen_helper_cvtsd2sq
2833};
2834#endif
2835
d3eb5eae 2836static const SSEFunc_0_epp sse_op_table4[8][4] = {
664e0f19
FB
2837 SSE_FOP(cmpeq),
2838 SSE_FOP(cmplt),
2839 SSE_FOP(cmple),
2840 SSE_FOP(cmpunord),
2841 SSE_FOP(cmpneq),
2842 SSE_FOP(cmpnlt),
2843 SSE_FOP(cmpnle),
2844 SSE_FOP(cmpord),
2845};
3b46e624 2846
d3eb5eae 2847static const SSEFunc_0_epp sse_op_table5[256] = {
a7812ae4
PB
2848 [0x0c] = gen_helper_pi2fw,
2849 [0x0d] = gen_helper_pi2fd,
2850 [0x1c] = gen_helper_pf2iw,
2851 [0x1d] = gen_helper_pf2id,
2852 [0x8a] = gen_helper_pfnacc,
2853 [0x8e] = gen_helper_pfpnacc,
2854 [0x90] = gen_helper_pfcmpge,
2855 [0x94] = gen_helper_pfmin,
2856 [0x96] = gen_helper_pfrcp,
2857 [0x97] = gen_helper_pfrsqrt,
2858 [0x9a] = gen_helper_pfsub,
2859 [0x9e] = gen_helper_pfadd,
2860 [0xa0] = gen_helper_pfcmpgt,
2861 [0xa4] = gen_helper_pfmax,
2862 [0xa6] = gen_helper_movq, /* pfrcpit1; no need to actually increase precision */
2863 [0xa7] = gen_helper_movq, /* pfrsqit1 */
2864 [0xaa] = gen_helper_pfsubr,
2865 [0xae] = gen_helper_pfacc,
2866 [0xb0] = gen_helper_pfcmpeq,
2867 [0xb4] = gen_helper_pfmul,
2868 [0xb6] = gen_helper_movq, /* pfrcpit2 */
2869 [0xb7] = gen_helper_pmulhrw_mmx,
2870 [0xbb] = gen_helper_pswapd,
2871 [0xbf] = gen_helper_pavgb_mmx /* pavgusb */
a35f3ec7
AJ
2872};
2873
d3eb5eae
BS
2874struct SSEOpHelper_epp {
2875 SSEFunc_0_epp op[2];
c4baa050
BS
2876 uint32_t ext_mask;
2877};
2878
d3eb5eae
BS
2879struct SSEOpHelper_eppi {
2880 SSEFunc_0_eppi op[2];
c4baa050 2881 uint32_t ext_mask;
222a3336 2882};
c4baa050 2883
222a3336 2884#define SSSE3_OP(x) { MMX_OP2(x), CPUID_EXT_SSSE3 }
a7812ae4
PB
2885#define SSE41_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE41 }
2886#define SSE42_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_SSE42 }
222a3336 2887#define SSE41_SPECIAL { { NULL, SSE_SPECIAL }, CPUID_EXT_SSE41 }
e71827bc
AJ
2888#define PCLMULQDQ_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, \
2889 CPUID_EXT_PCLMULQDQ }
d640045a 2890#define AESNI_OP(x) { { NULL, gen_helper_ ## x ## _xmm }, CPUID_EXT_AES }
c4baa050 2891
d3eb5eae 2892static const struct SSEOpHelper_epp sse_op_table6[256] = {
222a3336
AZ
2893 [0x00] = SSSE3_OP(pshufb),
2894 [0x01] = SSSE3_OP(phaddw),
2895 [0x02] = SSSE3_OP(phaddd),
2896 [0x03] = SSSE3_OP(phaddsw),
2897 [0x04] = SSSE3_OP(pmaddubsw),
2898 [0x05] = SSSE3_OP(phsubw),
2899 [0x06] = SSSE3_OP(phsubd),
2900 [0x07] = SSSE3_OP(phsubsw),
2901 [0x08] = SSSE3_OP(psignb),
2902 [0x09] = SSSE3_OP(psignw),
2903 [0x0a] = SSSE3_OP(psignd),
2904 [0x0b] = SSSE3_OP(pmulhrsw),
2905 [0x10] = SSE41_OP(pblendvb),
2906 [0x14] = SSE41_OP(blendvps),
2907 [0x15] = SSE41_OP(blendvpd),
2908 [0x17] = SSE41_OP(ptest),
2909 [0x1c] = SSSE3_OP(pabsb),
2910 [0x1d] = SSSE3_OP(pabsw),
2911 [0x1e] = SSSE3_OP(pabsd),
2912 [0x20] = SSE41_OP(pmovsxbw),
2913 [0x21] = SSE41_OP(pmovsxbd),
2914 [0x22] = SSE41_OP(pmovsxbq),
2915 [0x23] = SSE41_OP(pmovsxwd),
2916 [0x24] = SSE41_OP(pmovsxwq),
2917 [0x25] = SSE41_OP(pmovsxdq),
2918 [0x28] = SSE41_OP(pmuldq),
2919 [0x29] = SSE41_OP(pcmpeqq),
2920 [0x2a] = SSE41_SPECIAL, /* movntqda */
2921 [0x2b] = SSE41_OP(packusdw),
2922 [0x30] = SSE41_OP(pmovzxbw),
2923 [0x31] = SSE41_OP(pmovzxbd),
2924 [0x32] = SSE41_OP(pmovzxbq),
2925 [0x33] = SSE41_OP(pmovzxwd),
2926 [0x34] = SSE41_OP(pmovzxwq),
2927 [0x35] = SSE41_OP(pmovzxdq),
2928 [0x37] = SSE42_OP(pcmpgtq),
2929 [0x38] = SSE41_OP(pminsb),
2930 [0x39] = SSE41_OP(pminsd),
2931 [0x3a] = SSE41_OP(pminuw),
2932 [0x3b] = SSE41_OP(pminud),
2933 [0x3c] = SSE41_OP(pmaxsb),
2934 [0x3d] = SSE41_OP(pmaxsd),
2935 [0x3e] = SSE41_OP(pmaxuw),
2936 [0x3f] = SSE41_OP(pmaxud),
2937 [0x40] = SSE41_OP(pmulld),
2938 [0x41] = SSE41_OP(phminposuw),
d640045a
AJ
2939 [0xdb] = AESNI_OP(aesimc),
2940 [0xdc] = AESNI_OP(aesenc),
2941 [0xdd] = AESNI_OP(aesenclast),
2942 [0xde] = AESNI_OP(aesdec),
2943 [0xdf] = AESNI_OP(aesdeclast),
4242b1bd
AZ
2944};
2945
d3eb5eae 2946static const struct SSEOpHelper_eppi sse_op_table7[256] = {
222a3336
AZ
2947 [0x08] = SSE41_OP(roundps),
2948 [0x09] = SSE41_OP(roundpd),
2949 [0x0a] = SSE41_OP(roundss),
2950 [0x0b] = SSE41_OP(roundsd),
2951 [0x0c] = SSE41_OP(blendps),
2952 [0x0d] = SSE41_OP(blendpd),
2953 [0x0e] = SSE41_OP(pblendw),
2954 [0x0f] = SSSE3_OP(palignr),
2955 [0x14] = SSE41_SPECIAL, /* pextrb */
2956 [0x15] = SSE41_SPECIAL, /* pextrw */
2957 [0x16] = SSE41_SPECIAL, /* pextrd/pextrq */
2958 [0x17] = SSE41_SPECIAL, /* extractps */
2959 [0x20] = SSE41_SPECIAL, /* pinsrb */
2960 [0x21] = SSE41_SPECIAL, /* insertps */
2961 [0x22] = SSE41_SPECIAL, /* pinsrd/pinsrq */
2962 [0x40] = SSE41_OP(dpps),
2963 [0x41] = SSE41_OP(dppd),
2964 [0x42] = SSE41_OP(mpsadbw),
e71827bc 2965 [0x44] = PCLMULQDQ_OP(pclmulqdq),
222a3336
AZ
2966 [0x60] = SSE42_OP(pcmpestrm),
2967 [0x61] = SSE42_OP(pcmpestri),
2968 [0x62] = SSE42_OP(pcmpistrm),
2969 [0x63] = SSE42_OP(pcmpistri),
d640045a 2970 [0xdf] = AESNI_OP(aeskeygenassist),
4242b1bd
AZ
2971};
2972
0af10c86
BS
2973static void gen_sse(CPUX86State *env, DisasContext *s, int b,
2974 target_ulong pc_start, int rex_r)
664e0f19 2975{
d67dc9e6 2976 int b1, op1_offset, op2_offset, is_xmm, val;
4eeb3939 2977 int modrm, mod, rm, reg;
d3eb5eae
BS
2978 SSEFunc_0_epp sse_fn_epp;
2979 SSEFunc_0_eppi sse_fn_eppi;
c4baa050 2980 SSEFunc_0_ppi sse_fn_ppi;
d3eb5eae 2981 SSEFunc_0_eppt sse_fn_eppt;
d67dc9e6 2982 TCGMemOp ot;
664e0f19
FB
2983
2984 b &= 0xff;
5fafdf24 2985 if (s->prefix & PREFIX_DATA)
664e0f19 2986 b1 = 1;
5fafdf24 2987 else if (s->prefix & PREFIX_REPZ)
664e0f19 2988 b1 = 2;
5fafdf24 2989 else if (s->prefix & PREFIX_REPNZ)
664e0f19
FB
2990 b1 = 3;
2991 else
2992 b1 = 0;
d3eb5eae
BS
2993 sse_fn_epp = sse_op_table1[b][b1];
2994 if (!sse_fn_epp) {
664e0f19 2995 goto illegal_op;
c4baa050 2996 }
a35f3ec7 2997 if ((b <= 0x5f && b >= 0x10) || b == 0xc6 || b == 0xc2) {
664e0f19
FB
2998 is_xmm = 1;
2999 } else {
3000 if (b1 == 0) {
3001 /* MMX case */
3002 is_xmm = 0;
3003 } else {
3004 is_xmm = 1;
3005 }
3006 }
3007 /* simple MMX/SSE operation */
3008 if (s->flags & HF_TS_MASK) {
3009 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
3010 return;
3011 }
3012 if (s->flags & HF_EM_MASK) {
3013 illegal_op:
3014 gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
3015 return;
3016 }
3017 if (is_xmm && !(s->flags & HF_OSFXSR_MASK))
4242b1bd
AZ
3018 if ((b != 0x38 && b != 0x3a) || (s->prefix & PREFIX_DATA))
3019 goto illegal_op;
e771edab
AJ
3020 if (b == 0x0e) {
3021 if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
3022 goto illegal_op;
3023 /* femms */
d3eb5eae 3024 gen_helper_emms(cpu_env);
e771edab
AJ
3025 return;
3026 }
3027 if (b == 0x77) {
3028 /* emms */
d3eb5eae 3029 gen_helper_emms(cpu_env);
664e0f19
FB
3030 return;
3031 }
3032 /* prepare MMX state (XXX: optimize by storing fptt and fptags in
3033 the static cpu state) */
3034 if (!is_xmm) {
d3eb5eae 3035 gen_helper_enter_mmx(cpu_env);
664e0f19
FB
3036 }
3037
0af10c86 3038 modrm = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3039 reg = ((modrm >> 3) & 7);
3040 if (is_xmm)
3041 reg |= rex_r;
3042 mod = (modrm >> 6) & 3;
d3eb5eae 3043 if (sse_fn_epp == SSE_SPECIAL) {
664e0f19
FB
3044 b |= (b1 << 8);
3045 switch(b) {
3046 case 0x0e7: /* movntq */
5fafdf24 3047 if (mod == 3)
664e0f19 3048 goto illegal_op;
4eeb3939 3049 gen_lea_modrm(env, s, modrm);
323d1876 3050 gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
664e0f19
FB
3051 break;
3052 case 0x1e7: /* movntdq */
3053 case 0x02b: /* movntps */
3054 case 0x12b: /* movntps */
2e21e749
T
3055 if (mod == 3)
3056 goto illegal_op;
4eeb3939 3057 gen_lea_modrm(env, s, modrm);
323d1876 3058 gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
2e21e749 3059 break;
465e9838
FB
3060 case 0x3f0: /* lddqu */
3061 if (mod == 3)
664e0f19 3062 goto illegal_op;
4eeb3939 3063 gen_lea_modrm(env, s, modrm);
323d1876 3064 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
664e0f19 3065 break;
d9f4bb27
AP
3066 case 0x22b: /* movntss */
3067 case 0x32b: /* movntsd */
3068 if (mod == 3)
3069 goto illegal_op;
4eeb3939 3070 gen_lea_modrm(env, s, modrm);
d9f4bb27 3071 if (b1 & 1) {
323d1876 3072 gen_stq_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
d9f4bb27
AP
3073 } else {
3074 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
3075 xmm_regs[reg].XMM_L(0)));
fd8ca9f6 3076 gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
d9f4bb27
AP
3077 }
3078 break;
664e0f19 3079 case 0x6e: /* movd mm, ea */
dabd98dd 3080#ifdef TARGET_X86_64
ab4e4aec 3081 if (s->dflag == MO_64) {
4ba9938c 3082 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
5af45186 3083 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,fpregs[reg].mmx));
5fafdf24 3084 } else
dabd98dd
FB
3085#endif
3086 {
4ba9938c 3087 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
5af45186
FB
3088 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3089 offsetof(CPUX86State,fpregs[reg].mmx));
a7812ae4
PB
3090 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
3091 gen_helper_movl_mm_T0_mmx(cpu_ptr0, cpu_tmp2_i32);
dabd98dd 3092 }
664e0f19
FB
3093 break;
3094 case 0x16e: /* movd xmm, ea */
dabd98dd 3095#ifdef TARGET_X86_64
ab4e4aec 3096 if (s->dflag == MO_64) {
4ba9938c 3097 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 0);
5af45186
FB
3098 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3099 offsetof(CPUX86State,xmm_regs[reg]));
a7812ae4 3100 gen_helper_movq_mm_T0_xmm(cpu_ptr0, cpu_T[0]);
5fafdf24 3101 } else
dabd98dd
FB
3102#endif
3103 {
4ba9938c 3104 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 0);
5af45186
FB
3105 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3106 offsetof(CPUX86State,xmm_regs[reg]));
b6abf97d 3107 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 3108 gen_helper_movl_mm_T0_xmm(cpu_ptr0, cpu_tmp2_i32);
dabd98dd 3109 }
664e0f19
FB
3110 break;
3111 case 0x6f: /* movq mm, ea */
3112 if (mod != 3) {
4eeb3939 3113 gen_lea_modrm(env, s, modrm);
323d1876 3114 gen_ldq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
664e0f19
FB
3115 } else {
3116 rm = (modrm & 7);
b6abf97d 3117 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
5af45186 3118 offsetof(CPUX86State,fpregs[rm].mmx));
b6abf97d 3119 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
5af45186 3120 offsetof(CPUX86State,fpregs[reg].mmx));
664e0f19
FB
3121 }
3122 break;
3123 case 0x010: /* movups */
3124 case 0x110: /* movupd */
3125 case 0x028: /* movaps */
3126 case 0x128: /* movapd */
3127 case 0x16f: /* movdqa xmm, ea */
3128 case 0x26f: /* movdqu xmm, ea */
3129 if (mod != 3) {
4eeb3939 3130 gen_lea_modrm(env, s, modrm);
323d1876 3131 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
664e0f19
FB
3132 } else {
3133 rm = (modrm & 7) | REX_B(s);
3134 gen_op_movo(offsetof(CPUX86State,xmm_regs[reg]),
3135 offsetof(CPUX86State,xmm_regs[rm]));
3136 }
3137 break;
3138 case 0x210: /* movss xmm, ea */
3139 if (mod != 3) {
4eeb3939 3140 gen_lea_modrm(env, s, modrm);
909be183 3141 gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
651ba608 3142 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
97212c88 3143 tcg_gen_movi_tl(cpu_T[0], 0);
651ba608
FB
3144 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
3145 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
3146 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
664e0f19
FB
3147 } else {
3148 rm = (modrm & 7) | REX_B(s);
3149 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
3150 offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
3151 }
3152 break;
3153 case 0x310: /* movsd xmm, ea */
3154 if (mod != 3) {
4eeb3939 3155 gen_lea_modrm(env, s, modrm);
323d1876
RH
3156 gen_ldq_env_A0(s, offsetof(CPUX86State,
3157 xmm_regs[reg].XMM_Q(0)));
97212c88 3158 tcg_gen_movi_tl(cpu_T[0], 0);
651ba608
FB
3159 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
3160 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
664e0f19
FB
3161 } else {
3162 rm = (modrm & 7) | REX_B(s);
3163 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3164 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3165 }
3166 break;
3167 case 0x012: /* movlps */
3168 case 0x112: /* movlpd */
3169 if (mod != 3) {
4eeb3939 3170 gen_lea_modrm(env, s, modrm);
323d1876
RH
3171 gen_ldq_env_A0(s, offsetof(CPUX86State,
3172 xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3173 } else {
3174 /* movhlps */
3175 rm = (modrm & 7) | REX_B(s);
3176 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3177 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
3178 }
3179 break;
465e9838
FB
3180 case 0x212: /* movsldup */
3181 if (mod != 3) {
4eeb3939 3182 gen_lea_modrm(env, s, modrm);
323d1876 3183 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
465e9838
FB
3184 } else {
3185 rm = (modrm & 7) | REX_B(s);
3186 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
3187 offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)));
3188 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
3189 offsetof(CPUX86State,xmm_regs[rm].XMM_L(2)));
3190 }
3191 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
3192 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3193 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
3194 offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)));
3195 break;
3196 case 0x312: /* movddup */
3197 if (mod != 3) {
4eeb3939 3198 gen_lea_modrm(env, s, modrm);
323d1876
RH
3199 gen_ldq_env_A0(s, offsetof(CPUX86State,
3200 xmm_regs[reg].XMM_Q(0)));
465e9838
FB
3201 } else {
3202 rm = (modrm & 7) | REX_B(s);
3203 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3204 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3205 }
3206 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
ba6526df 3207 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
465e9838 3208 break;
664e0f19
FB
3209 case 0x016: /* movhps */
3210 case 0x116: /* movhpd */
3211 if (mod != 3) {
4eeb3939 3212 gen_lea_modrm(env, s, modrm);
323d1876
RH
3213 gen_ldq_env_A0(s, offsetof(CPUX86State,
3214 xmm_regs[reg].XMM_Q(1)));
664e0f19
FB
3215 } else {
3216 /* movlhps */
3217 rm = (modrm & 7) | REX_B(s);
3218 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)),
3219 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3220 }
3221 break;
3222 case 0x216: /* movshdup */
3223 if (mod != 3) {
4eeb3939 3224 gen_lea_modrm(env, s, modrm);
323d1876 3225 gen_ldo_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
664e0f19
FB
3226 } else {
3227 rm = (modrm & 7) | REX_B(s);
3228 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)),
3229 offsetof(CPUX86State,xmm_regs[rm].XMM_L(1)));
3230 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)),
3231 offsetof(CPUX86State,xmm_regs[rm].XMM_L(3)));
3232 }
3233 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)),
3234 offsetof(CPUX86State,xmm_regs[reg].XMM_L(1)));
3235 gen_op_movl(offsetof(CPUX86State,xmm_regs[reg].XMM_L(2)),
3236 offsetof(CPUX86State,xmm_regs[reg].XMM_L(3)));
3237 break;
d9f4bb27
AP
3238 case 0x178:
3239 case 0x378:
3240 {
3241 int bit_index, field_length;
3242
3243 if (b1 == 1 && reg != 0)
3244 goto illegal_op;
0af10c86
BS
3245 field_length = cpu_ldub_code(env, s->pc++) & 0x3F;
3246 bit_index = cpu_ldub_code(env, s->pc++) & 0x3F;
d9f4bb27
AP
3247 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3248 offsetof(CPUX86State,xmm_regs[reg]));
3249 if (b1 == 1)
d3eb5eae
BS
3250 gen_helper_extrq_i(cpu_env, cpu_ptr0,
3251 tcg_const_i32(bit_index),
3252 tcg_const_i32(field_length));
d9f4bb27 3253 else
d3eb5eae
BS
3254 gen_helper_insertq_i(cpu_env, cpu_ptr0,
3255 tcg_const_i32(bit_index),
3256 tcg_const_i32(field_length));
d9f4bb27
AP
3257 }
3258 break;
664e0f19 3259 case 0x7e: /* movd ea, mm */
dabd98dd 3260#ifdef TARGET_X86_64
ab4e4aec 3261 if (s->dflag == MO_64) {
5af45186
FB
3262 tcg_gen_ld_i64(cpu_T[0], cpu_env,
3263 offsetof(CPUX86State,fpregs[reg].mmx));
4ba9938c 3264 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
5fafdf24 3265 } else
dabd98dd
FB
3266#endif
3267 {
5af45186
FB
3268 tcg_gen_ld32u_tl(cpu_T[0], cpu_env,
3269 offsetof(CPUX86State,fpregs[reg].mmx.MMX_L(0)));
4ba9938c 3270 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
dabd98dd 3271 }
664e0f19
FB
3272 break;
3273 case 0x17e: /* movd ea, xmm */
dabd98dd 3274#ifdef TARGET_X86_64
ab4e4aec 3275 if (s->dflag == MO_64) {
5af45186
FB
3276 tcg_gen_ld_i64(cpu_T[0], cpu_env,
3277 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
4ba9938c 3278 gen_ldst_modrm(env, s, modrm, MO_64, OR_TMP0, 1);
5fafdf24 3279 } else
dabd98dd
FB
3280#endif
3281 {
5af45186
FB
3282 tcg_gen_ld32u_tl(cpu_T[0], cpu_env,
3283 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
4ba9938c 3284 gen_ldst_modrm(env, s, modrm, MO_32, OR_TMP0, 1);
dabd98dd 3285 }
664e0f19
FB
3286 break;
3287 case 0x27e: /* movq xmm, ea */
3288 if (mod != 3) {
4eeb3939 3289 gen_lea_modrm(env, s, modrm);
323d1876
RH
3290 gen_ldq_env_A0(s, offsetof(CPUX86State,
3291 xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3292 } else {
3293 rm = (modrm & 7) | REX_B(s);
3294 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3295 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
3296 }
3297 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
3298 break;
3299 case 0x7f: /* movq ea, mm */
3300 if (mod != 3) {
4eeb3939 3301 gen_lea_modrm(env, s, modrm);
323d1876 3302 gen_stq_env_A0(s, offsetof(CPUX86State, fpregs[reg].mmx));
664e0f19
FB
3303 } else {
3304 rm = (modrm & 7);
3305 gen_op_movq(offsetof(CPUX86State,fpregs[rm].mmx),
3306 offsetof(CPUX86State,fpregs[reg].mmx));
3307 }
3308 break;
3309 case 0x011: /* movups */
3310 case 0x111: /* movupd */
3311 case 0x029: /* movaps */
3312 case 0x129: /* movapd */
3313 case 0x17f: /* movdqa ea, xmm */
3314 case 0x27f: /* movdqu ea, xmm */
3315 if (mod != 3) {
4eeb3939 3316 gen_lea_modrm(env, s, modrm);
323d1876 3317 gen_sto_env_A0(s, offsetof(CPUX86State, xmm_regs[reg]));
664e0f19
FB
3318 } else {
3319 rm = (modrm & 7) | REX_B(s);
3320 gen_op_movo(offsetof(CPUX86State,xmm_regs[rm]),
3321 offsetof(CPUX86State,xmm_regs[reg]));
3322 }
3323 break;
3324 case 0x211: /* movss ea, xmm */
3325 if (mod != 3) {
4eeb3939 3326 gen_lea_modrm(env, s, modrm);
651ba608 3327 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
fd8ca9f6 3328 gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
664e0f19
FB
3329 } else {
3330 rm = (modrm & 7) | REX_B(s);
3331 gen_op_movl(offsetof(CPUX86State,xmm_regs[rm].XMM_L(0)),
3332 offsetof(CPUX86State,xmm_regs[reg].XMM_L(0)));
3333 }
3334 break;
3335 case 0x311: /* movsd ea, xmm */
3336 if (mod != 3) {
4eeb3939 3337 gen_lea_modrm(env, s, modrm);
323d1876
RH
3338 gen_stq_env_A0(s, offsetof(CPUX86State,
3339 xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3340 } else {
3341 rm = (modrm & 7) | REX_B(s);
3342 gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
3343 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3344 }
3345 break;
3346 case 0x013: /* movlps */
3347 case 0x113: /* movlpd */
3348 if (mod != 3) {
4eeb3939 3349 gen_lea_modrm(env, s, modrm);
323d1876
RH
3350 gen_stq_env_A0(s, offsetof(CPUX86State,
3351 xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3352 } else {
3353 goto illegal_op;
3354 }
3355 break;
3356 case 0x017: /* movhps */
3357 case 0x117: /* movhpd */
3358 if (mod != 3) {
4eeb3939 3359 gen_lea_modrm(env, s, modrm);
323d1876
RH
3360 gen_stq_env_A0(s, offsetof(CPUX86State,
3361 xmm_regs[reg].XMM_Q(1)));
664e0f19
FB
3362 } else {
3363 goto illegal_op;
3364 }
3365 break;
3366 case 0x71: /* shift mm, im */
3367 case 0x72:
3368 case 0x73:
3369 case 0x171: /* shift xmm, im */
3370 case 0x172:
3371 case 0x173:
c045af25
AK
3372 if (b1 >= 2) {
3373 goto illegal_op;
3374 }
0af10c86 3375 val = cpu_ldub_code(env, s->pc++);
664e0f19 3376 if (is_xmm) {
1b90d56e 3377 tcg_gen_movi_tl(cpu_T[0], val);
651ba608 3378 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
97212c88 3379 tcg_gen_movi_tl(cpu_T[0], 0);
651ba608 3380 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(1)));
664e0f19
FB
3381 op1_offset = offsetof(CPUX86State,xmm_t0);
3382 } else {
1b90d56e 3383 tcg_gen_movi_tl(cpu_T[0], val);
651ba608 3384 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(0)));
97212c88 3385 tcg_gen_movi_tl(cpu_T[0], 0);
651ba608 3386 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,mmx_t0.MMX_L(1)));
664e0f19
FB
3387 op1_offset = offsetof(CPUX86State,mmx_t0);
3388 }
d3eb5eae
BS
3389 sse_fn_epp = sse_op_table2[((b - 1) & 3) * 8 +
3390 (((modrm >> 3)) & 7)][b1];
3391 if (!sse_fn_epp) {
664e0f19 3392 goto illegal_op;
c4baa050 3393 }
664e0f19
FB
3394 if (is_xmm) {
3395 rm = (modrm & 7) | REX_B(s);
3396 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3397 } else {
3398 rm = (modrm & 7);
3399 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3400 }
5af45186
FB
3401 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
3402 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op1_offset);
d3eb5eae 3403 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3404 break;
3405 case 0x050: /* movmskps */
664e0f19 3406 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3407 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3408 offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3409 gen_helper_movmskps(cpu_tmp2_i32, cpu_env, cpu_ptr0);
a7fbcbe5 3410 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
664e0f19
FB
3411 break;
3412 case 0x150: /* movmskpd */
664e0f19 3413 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3414 tcg_gen_addi_ptr(cpu_ptr0, cpu_env,
3415 offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3416 gen_helper_movmskpd(cpu_tmp2_i32, cpu_env, cpu_ptr0);
a7fbcbe5 3417 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
664e0f19
FB
3418 break;
3419 case 0x02a: /* cvtpi2ps */
3420 case 0x12a: /* cvtpi2pd */
d3eb5eae 3421 gen_helper_enter_mmx(cpu_env);
664e0f19 3422 if (mod != 3) {
4eeb3939 3423 gen_lea_modrm(env, s, modrm);
664e0f19 3424 op2_offset = offsetof(CPUX86State,mmx_t0);
323d1876 3425 gen_ldq_env_A0(s, op2_offset);
664e0f19
FB
3426 } else {
3427 rm = (modrm & 7);
3428 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3429 }
3430 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
5af45186
FB
3431 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3432 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
664e0f19
FB
3433 switch(b >> 8) {
3434 case 0x0:
d3eb5eae 3435 gen_helper_cvtpi2ps(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3436 break;
3437 default:
3438 case 0x1:
d3eb5eae 3439 gen_helper_cvtpi2pd(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3440 break;
3441 }
3442 break;
3443 case 0x22a: /* cvtsi2ss */
3444 case 0x32a: /* cvtsi2sd */
ab4e4aec 3445 ot = mo_64_32(s->dflag);
0af10c86 3446 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
664e0f19 3447 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
5af45186 3448 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4ba9938c 3449 if (ot == MO_32) {
d3eb5eae 3450 SSEFunc_0_epi sse_fn_epi = sse_op_table3ai[(b >> 8) & 1];
28e10711 3451 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
d3eb5eae 3452 sse_fn_epi(cpu_env, cpu_ptr0, cpu_tmp2_i32);
28e10711 3453 } else {
11f8cdbc 3454#ifdef TARGET_X86_64
d3eb5eae
BS
3455 SSEFunc_0_epl sse_fn_epl = sse_op_table3aq[(b >> 8) & 1];
3456 sse_fn_epl(cpu_env, cpu_ptr0, cpu_T[0]);
11f8cdbc
SW
3457#else
3458 goto illegal_op;
3459#endif
28e10711 3460 }
664e0f19
FB
3461 break;
3462 case 0x02c: /* cvttps2pi */
3463 case 0x12c: /* cvttpd2pi */
3464 case 0x02d: /* cvtps2pi */
3465 case 0x12d: /* cvtpd2pi */
d3eb5eae 3466 gen_helper_enter_mmx(cpu_env);
664e0f19 3467 if (mod != 3) {
4eeb3939 3468 gen_lea_modrm(env, s, modrm);
664e0f19 3469 op2_offset = offsetof(CPUX86State,xmm_t0);
323d1876 3470 gen_ldo_env_A0(s, op2_offset);
664e0f19
FB
3471 } else {
3472 rm = (modrm & 7) | REX_B(s);
3473 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3474 }
3475 op1_offset = offsetof(CPUX86State,fpregs[reg & 7].mmx);
5af45186
FB
3476 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3477 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
664e0f19
FB
3478 switch(b) {
3479 case 0x02c:
d3eb5eae 3480 gen_helper_cvttps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3481 break;
3482 case 0x12c:
d3eb5eae 3483 gen_helper_cvttpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3484 break;
3485 case 0x02d:
d3eb5eae 3486 gen_helper_cvtps2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3487 break;
3488 case 0x12d:
d3eb5eae 3489 gen_helper_cvtpd2pi(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
3490 break;
3491 }
3492 break;
3493 case 0x22c: /* cvttss2si */
3494 case 0x32c: /* cvttsd2si */
3495 case 0x22d: /* cvtss2si */
3496 case 0x32d: /* cvtsd2si */
ab4e4aec 3497 ot = mo_64_32(s->dflag);
31313213 3498 if (mod != 3) {
4eeb3939 3499 gen_lea_modrm(env, s, modrm);
31313213 3500 if ((b >> 8) & 1) {
323d1876 3501 gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.XMM_Q(0)));
31313213 3502 } else {
909be183 3503 gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
651ba608 3504 tcg_gen_st32_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,xmm_t0.XMM_L(0)));
31313213
FB
3505 }
3506 op2_offset = offsetof(CPUX86State,xmm_t0);
3507 } else {
3508 rm = (modrm & 7) | REX_B(s);
3509 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
3510 }
5af45186 3511 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op2_offset);
4ba9938c 3512 if (ot == MO_32) {
d3eb5eae 3513 SSEFunc_i_ep sse_fn_i_ep =
bedc2ac1 3514 sse_op_table3bi[((b >> 7) & 2) | (b & 1)];
d3eb5eae 3515 sse_fn_i_ep(cpu_tmp2_i32, cpu_env, cpu_ptr0);
b6abf97d 3516 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
5af45186 3517 } else {
11f8cdbc 3518#ifdef TARGET_X86_64
d3eb5eae 3519 SSEFunc_l_ep sse_fn_l_ep =
bedc2ac1 3520 sse_op_table3bq[((b >> 7) & 2) | (b & 1)];
d3eb5eae 3521 sse_fn_l_ep(cpu_T[0], cpu_env, cpu_ptr0);
11f8cdbc
SW
3522#else
3523 goto illegal_op;
3524#endif
5af45186 3525 }
480a762d 3526 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
664e0f19
FB
3527 break;
3528 case 0xc4: /* pinsrw */
5fafdf24 3529 case 0x1c4:
d1e42c5c 3530 s->rip_offset = 1;
4ba9938c 3531 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
0af10c86 3532 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3533 if (b1) {
3534 val &= 7;
5af45186
FB
3535 tcg_gen_st16_tl(cpu_T[0], cpu_env,
3536 offsetof(CPUX86State,xmm_regs[reg].XMM_W(val)));
664e0f19
FB
3537 } else {
3538 val &= 3;
5af45186
FB
3539 tcg_gen_st16_tl(cpu_T[0], cpu_env,
3540 offsetof(CPUX86State,fpregs[reg].mmx.MMX_W(val)));
664e0f19
FB
3541 }
3542 break;
3543 case 0xc5: /* pextrw */
5fafdf24 3544 case 0x1c5:
664e0f19
FB
3545 if (mod != 3)
3546 goto illegal_op;
ab4e4aec 3547 ot = mo_64_32(s->dflag);
0af10c86 3548 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
3549 if (b1) {
3550 val &= 7;
3551 rm = (modrm & 7) | REX_B(s);
5af45186
FB
3552 tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
3553 offsetof(CPUX86State,xmm_regs[rm].XMM_W(val)));
664e0f19
FB
3554 } else {
3555 val &= 3;
3556 rm = (modrm & 7);
5af45186
FB
3557 tcg_gen_ld16u_tl(cpu_T[0], cpu_env,
3558 offsetof(CPUX86State,fpregs[rm].mmx.MMX_W(val)));
664e0f19
FB
3559 }
3560 reg = ((modrm >> 3) & 7) | rex_r;
480a762d 3561 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
664e0f19
FB
3562 break;
3563 case 0x1d6: /* movq ea, xmm */
3564 if (mod != 3) {
4eeb3939 3565 gen_lea_modrm(env, s, modrm);
323d1876
RH
3566 gen_stq_env_A0(s, offsetof(CPUX86State,
3567 xmm_regs[reg].XMM_Q(0)));
664e0f19
FB
3568 } else {
3569 rm = (modrm & 7) | REX_B(s);
3570 gen_op_movq(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)),
3571 offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)));
3572 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[rm].XMM_Q(1)));
3573 }
3574 break;
3575 case 0x2d6: /* movq2dq */
d3eb5eae 3576 gen_helper_enter_mmx(cpu_env);
480c1cdb
FB
3577 rm = (modrm & 7);
3578 gen_op_movq(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(0)),
3579 offsetof(CPUX86State,fpregs[rm].mmx));
3580 gen_op_movq_env_0(offsetof(CPUX86State,xmm_regs[reg].XMM_Q(1)));
664e0f19
FB
3581 break;
3582 case 0x3d6: /* movdq2q */
d3eb5eae 3583 gen_helper_enter_mmx(cpu_env);
480c1cdb
FB
3584 rm = (modrm & 7) | REX_B(s);
3585 gen_op_movq(offsetof(CPUX86State,fpregs[reg & 7].mmx),
3586 offsetof(CPUX86State,xmm_regs[rm].XMM_Q(0)));
664e0f19
FB
3587 break;
3588 case 0xd7: /* pmovmskb */
3589 case 0x1d7:
3590 if (mod != 3)
3591 goto illegal_op;
3592 if (b1) {
3593 rm = (modrm & 7) | REX_B(s);
5af45186 3594 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,xmm_regs[rm]));
d3eb5eae 3595 gen_helper_pmovmskb_xmm(cpu_tmp2_i32, cpu_env, cpu_ptr0);
664e0f19
FB
3596 } else {
3597 rm = (modrm & 7);
5af45186 3598 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, offsetof(CPUX86State,fpregs[rm].mmx));
d3eb5eae 3599 gen_helper_pmovmskb_mmx(cpu_tmp2_i32, cpu_env, cpu_ptr0);
664e0f19
FB
3600 }
3601 reg = ((modrm >> 3) & 7) | rex_r;
a7fbcbe5 3602 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
664e0f19 3603 break;
111994ee 3604
4242b1bd 3605 case 0x138:
000cacf6 3606 case 0x038:
4242b1bd 3607 b = modrm;
111994ee
RH
3608 if ((b & 0xf0) == 0xf0) {
3609 goto do_0f_38_fx;
3610 }
0af10c86 3611 modrm = cpu_ldub_code(env, s->pc++);
4242b1bd
AZ
3612 rm = modrm & 7;
3613 reg = ((modrm >> 3) & 7) | rex_r;
3614 mod = (modrm >> 6) & 3;
c045af25
AK
3615 if (b1 >= 2) {
3616 goto illegal_op;
3617 }
4242b1bd 3618
d3eb5eae
BS
3619 sse_fn_epp = sse_op_table6[b].op[b1];
3620 if (!sse_fn_epp) {
4242b1bd 3621 goto illegal_op;
c4baa050 3622 }
222a3336
AZ
3623 if (!(s->cpuid_ext_features & sse_op_table6[b].ext_mask))
3624 goto illegal_op;
4242b1bd
AZ
3625
3626 if (b1) {
3627 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
3628 if (mod == 3) {
3629 op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
3630 } else {
3631 op2_offset = offsetof(CPUX86State,xmm_t0);
4eeb3939 3632 gen_lea_modrm(env, s, modrm);
222a3336
AZ
3633 switch (b) {
3634 case 0x20: case 0x30: /* pmovsxbw, pmovzxbw */
3635 case 0x23: case 0x33: /* pmovsxwd, pmovzxwd */
3636 case 0x25: case 0x35: /* pmovsxdq, pmovzxdq */
323d1876 3637 gen_ldq_env_A0(s, op2_offset +
222a3336
AZ
3638 offsetof(XMMReg, XMM_Q(0)));
3639 break;
3640 case 0x21: case 0x31: /* pmovsxbd, pmovzxbd */
3641 case 0x24: case 0x34: /* pmovsxwq, pmovzxwq */
3c5f4116
RH
3642 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
3643 s->mem_index, MO_LEUL);
222a3336
AZ
3644 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, op2_offset +
3645 offsetof(XMMReg, XMM_L(0)));
3646 break;
3647 case 0x22: case 0x32: /* pmovsxbq, pmovzxbq */
3c5f4116
RH
3648 tcg_gen_qemu_ld_tl(cpu_tmp0, cpu_A0,
3649 s->mem_index, MO_LEUW);
222a3336
AZ
3650 tcg_gen_st16_tl(cpu_tmp0, cpu_env, op2_offset +
3651 offsetof(XMMReg, XMM_W(0)));
3652 break;
3653 case 0x2a: /* movntqda */
323d1876 3654 gen_ldo_env_A0(s, op1_offset);
222a3336
AZ
3655 return;
3656 default:
323d1876 3657 gen_ldo_env_A0(s, op2_offset);
222a3336 3658 }
4242b1bd
AZ
3659 }
3660 } else {
3661 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
3662 if (mod == 3) {
3663 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
3664 } else {
3665 op2_offset = offsetof(CPUX86State,mmx_t0);
4eeb3939 3666 gen_lea_modrm(env, s, modrm);
323d1876 3667 gen_ldq_env_A0(s, op2_offset);
4242b1bd
AZ
3668 }
3669 }
d3eb5eae 3670 if (sse_fn_epp == SSE_SPECIAL) {
222a3336 3671 goto illegal_op;
c4baa050 3672 }
222a3336 3673
4242b1bd
AZ
3674 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
3675 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 3676 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
222a3336 3677
3ca51d07
RH
3678 if (b == 0x17) {
3679 set_cc_op(s, CC_OP_EFLAGS);
3680 }
4242b1bd 3681 break;
111994ee
RH
3682
3683 case 0x238:
3684 case 0x338:
3685 do_0f_38_fx:
3686 /* Various integer extensions at 0f 38 f[0-f]. */
3687 b = modrm | (b1 << 8);
0af10c86 3688 modrm = cpu_ldub_code(env, s->pc++);
222a3336
AZ
3689 reg = ((modrm >> 3) & 7) | rex_r;
3690
111994ee
RH
3691 switch (b) {
3692 case 0x3f0: /* crc32 Gd,Eb */
3693 case 0x3f1: /* crc32 Gd,Ey */
3694 do_crc32:
3695 if (!(s->cpuid_ext_features & CPUID_EXT_SSE42)) {
3696 goto illegal_op;
3697 }
3698 if ((b & 0xff) == 0xf0) {
4ba9938c 3699 ot = MO_8;
ab4e4aec 3700 } else if (s->dflag != MO_64) {
4ba9938c 3701 ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
111994ee 3702 } else {
4ba9938c 3703 ot = MO_64;
111994ee 3704 }
4242b1bd 3705
24b9c00f 3706 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[reg]);
111994ee
RH
3707 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3708 gen_helper_crc32(cpu_T[0], cpu_tmp2_i32,
3709 cpu_T[0], tcg_const_i32(8 << ot));
222a3336 3710
ab4e4aec 3711 ot = mo_64_32(s->dflag);
480a762d 3712 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
111994ee 3713 break;
222a3336 3714
111994ee
RH
3715 case 0x1f0: /* crc32 or movbe */
3716 case 0x1f1:
3717 /* For these insns, the f3 prefix is supposed to have priority
3718 over the 66 prefix, but that's not what we implement above
3719 setting b1. */
3720 if (s->prefix & PREFIX_REPNZ) {
3721 goto do_crc32;
3722 }
3723 /* FALLTHRU */
3724 case 0x0f0: /* movbe Gy,My */
3725 case 0x0f1: /* movbe My,Gy */
3726 if (!(s->cpuid_ext_features & CPUID_EXT_MOVBE)) {
3727 goto illegal_op;
3728 }
ab4e4aec 3729 if (s->dflag != MO_64) {
4ba9938c 3730 ot = (s->prefix & PREFIX_DATA ? MO_16 : MO_32);
111994ee 3731 } else {
4ba9938c 3732 ot = MO_64;
111994ee
RH
3733 }
3734
3655a19f 3735 gen_lea_modrm(env, s, modrm);
111994ee 3736 if ((b & 1) == 0) {
3655a19f
RH
3737 tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
3738 s->mem_index, ot | MO_BE);
480a762d 3739 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
111994ee 3740 } else {
3655a19f
RH
3741 tcg_gen_qemu_st_tl(cpu_regs[reg], cpu_A0,
3742 s->mem_index, ot | MO_BE);
111994ee
RH
3743 }
3744 break;
3745
7073fbad
RH
3746 case 0x0f2: /* andn Gy, By, Ey */
3747 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
3748 || !(s->prefix & PREFIX_VEX)
3749 || s->vex_l != 0) {
3750 goto illegal_op;
3751 }
ab4e4aec 3752 ot = mo_64_32(s->dflag);
7073fbad
RH
3753 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3754 tcg_gen_andc_tl(cpu_T[0], cpu_regs[s->vex_v], cpu_T[0]);
480a762d 3755 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
7073fbad
RH
3756 gen_op_update1_cc();
3757 set_cc_op(s, CC_OP_LOGICB + ot);
3758 break;
3759
c7ab7565
RH
3760 case 0x0f7: /* bextr Gy, Ey, By */
3761 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
3762 || !(s->prefix & PREFIX_VEX)
3763 || s->vex_l != 0) {
3764 goto illegal_op;
3765 }
ab4e4aec 3766 ot = mo_64_32(s->dflag);
c7ab7565
RH
3767 {
3768 TCGv bound, zero;
3769
3770 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3771 /* Extract START, and shift the operand.
3772 Shifts larger than operand size get zeros. */
3773 tcg_gen_ext8u_tl(cpu_A0, cpu_regs[s->vex_v]);
3774 tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_A0);
3775
4ba9938c 3776 bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
c7ab7565
RH
3777 zero = tcg_const_tl(0);
3778 tcg_gen_movcond_tl(TCG_COND_LEU, cpu_T[0], cpu_A0, bound,
3779 cpu_T[0], zero);
3780 tcg_temp_free(zero);
3781
3782 /* Extract the LEN into a mask. Lengths larger than
3783 operand size get all ones. */
3784 tcg_gen_shri_tl(cpu_A0, cpu_regs[s->vex_v], 8);
3785 tcg_gen_ext8u_tl(cpu_A0, cpu_A0);
3786 tcg_gen_movcond_tl(TCG_COND_LEU, cpu_A0, cpu_A0, bound,
3787 cpu_A0, bound);
3788 tcg_temp_free(bound);
3789 tcg_gen_movi_tl(cpu_T[1], 1);
3790 tcg_gen_shl_tl(cpu_T[1], cpu_T[1], cpu_A0);
3791 tcg_gen_subi_tl(cpu_T[1], cpu_T[1], 1);
3792 tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
3793
480a762d 3794 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
c7ab7565
RH
3795 gen_op_update1_cc();
3796 set_cc_op(s, CC_OP_LOGICB + ot);
3797 }
3798 break;
3799
02ea1e6b
RH
3800 case 0x0f5: /* bzhi Gy, Ey, By */
3801 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
3802 || !(s->prefix & PREFIX_VEX)
3803 || s->vex_l != 0) {
3804 goto illegal_op;
3805 }
ab4e4aec 3806 ot = mo_64_32(s->dflag);
02ea1e6b
RH
3807 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3808 tcg_gen_ext8u_tl(cpu_T[1], cpu_regs[s->vex_v]);
3809 {
4ba9938c 3810 TCGv bound = tcg_const_tl(ot == MO_64 ? 63 : 31);
02ea1e6b
RH
3811 /* Note that since we're using BMILG (in order to get O
3812 cleared) we need to store the inverse into C. */
3813 tcg_gen_setcond_tl(TCG_COND_LT, cpu_cc_src,
3814 cpu_T[1], bound);
3815 tcg_gen_movcond_tl(TCG_COND_GT, cpu_T[1], cpu_T[1],
3816 bound, bound, cpu_T[1]);
3817 tcg_temp_free(bound);
3818 }
3819 tcg_gen_movi_tl(cpu_A0, -1);
3820 tcg_gen_shl_tl(cpu_A0, cpu_A0, cpu_T[1]);
3821 tcg_gen_andc_tl(cpu_T[0], cpu_T[0], cpu_A0);
480a762d 3822 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
02ea1e6b
RH
3823 gen_op_update1_cc();
3824 set_cc_op(s, CC_OP_BMILGB + ot);
3825 break;
3826
5f1f4b17
RH
3827 case 0x3f6: /* mulx By, Gy, rdx, Ey */
3828 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
3829 || !(s->prefix & PREFIX_VEX)
3830 || s->vex_l != 0) {
3831 goto illegal_op;
3832 }
ab4e4aec 3833 ot = mo_64_32(s->dflag);
5f1f4b17
RH
3834 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3835 switch (ot) {
5f1f4b17 3836 default:
a4bcea3d
RH
3837 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
3838 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EDX]);
3839 tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
3840 cpu_tmp2_i32, cpu_tmp3_i32);
3841 tcg_gen_extu_i32_tl(cpu_regs[s->vex_v], cpu_tmp2_i32);
3842 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp3_i32);
5f1f4b17
RH
3843 break;
3844#ifdef TARGET_X86_64
4ba9938c 3845 case MO_64:
a4bcea3d
RH
3846 tcg_gen_mulu2_i64(cpu_regs[s->vex_v], cpu_regs[reg],
3847 cpu_T[0], cpu_regs[R_EDX]);
5f1f4b17
RH
3848 break;
3849#endif
3850 }
3851 break;
3852
0592f74a
RH
3853 case 0x3f5: /* pdep Gy, By, Ey */
3854 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
3855 || !(s->prefix & PREFIX_VEX)
3856 || s->vex_l != 0) {
3857 goto illegal_op;
3858 }
ab4e4aec 3859 ot = mo_64_32(s->dflag);
0592f74a
RH
3860 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3861 /* Note that by zero-extending the mask operand, we
3862 automatically handle zero-extending the result. */
ab4e4aec 3863 if (ot == MO_64) {
0592f74a
RH
3864 tcg_gen_mov_tl(cpu_T[1], cpu_regs[s->vex_v]);
3865 } else {
3866 tcg_gen_ext32u_tl(cpu_T[1], cpu_regs[s->vex_v]);
3867 }
3868 gen_helper_pdep(cpu_regs[reg], cpu_T[0], cpu_T[1]);
3869 break;
3870
3871 case 0x2f5: /* pext Gy, By, Ey */
3872 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
3873 || !(s->prefix & PREFIX_VEX)
3874 || s->vex_l != 0) {
3875 goto illegal_op;
3876 }
ab4e4aec 3877 ot = mo_64_32(s->dflag);
0592f74a
RH
3878 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3879 /* Note that by zero-extending the mask operand, we
3880 automatically handle zero-extending the result. */
ab4e4aec 3881 if (ot == MO_64) {
0592f74a
RH
3882 tcg_gen_mov_tl(cpu_T[1], cpu_regs[s->vex_v]);
3883 } else {
3884 tcg_gen_ext32u_tl(cpu_T[1], cpu_regs[s->vex_v]);
3885 }
3886 gen_helper_pext(cpu_regs[reg], cpu_T[0], cpu_T[1]);
3887 break;
3888
cd7f97ca
RH
3889 case 0x1f6: /* adcx Gy, Ey */
3890 case 0x2f6: /* adox Gy, Ey */
3891 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_ADX)) {
3892 goto illegal_op;
3893 } else {
76f13133 3894 TCGv carry_in, carry_out, zero;
cd7f97ca
RH
3895 int end_op;
3896
ab4e4aec 3897 ot = mo_64_32(s->dflag);
cd7f97ca
RH
3898 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
3899
3900 /* Re-use the carry-out from a previous round. */
3901 TCGV_UNUSED(carry_in);
3902 carry_out = (b == 0x1f6 ? cpu_cc_dst : cpu_cc_src2);
3903 switch (s->cc_op) {
3904 case CC_OP_ADCX:
3905 if (b == 0x1f6) {
3906 carry_in = cpu_cc_dst;
3907 end_op = CC_OP_ADCX;
3908 } else {
3909 end_op = CC_OP_ADCOX;
3910 }
3911 break;
3912 case CC_OP_ADOX:
3913 if (b == 0x1f6) {
3914 end_op = CC_OP_ADCOX;
3915 } else {
3916 carry_in = cpu_cc_src2;
3917 end_op = CC_OP_ADOX;
3918 }
3919 break;
3920 case CC_OP_ADCOX:
3921 end_op = CC_OP_ADCOX;
3922 carry_in = carry_out;
3923 break;
3924 default:
c53de1a2 3925 end_op = (b == 0x1f6 ? CC_OP_ADCX : CC_OP_ADOX);
cd7f97ca
RH
3926 break;
3927 }
3928 /* If we can't reuse carry-out, get it out of EFLAGS. */
3929 if (TCGV_IS_UNUSED(carry_in)) {
3930 if (s->cc_op != CC_OP_ADCX && s->cc_op != CC_OP_ADOX) {
3931 gen_compute_eflags(s);
3932 }
3933 carry_in = cpu_tmp0;
3934 tcg_gen_shri_tl(carry_in, cpu_cc_src,
3935 ctz32(b == 0x1f6 ? CC_C : CC_O));
3936 tcg_gen_andi_tl(carry_in, carry_in, 1);
3937 }
3938
3939 switch (ot) {
3940#ifdef TARGET_X86_64
4ba9938c 3941 case MO_32:
cd7f97ca
RH
3942 /* If we know TL is 64-bit, and we want a 32-bit
3943 result, just do everything in 64-bit arithmetic. */
3944 tcg_gen_ext32u_i64(cpu_regs[reg], cpu_regs[reg]);
3945 tcg_gen_ext32u_i64(cpu_T[0], cpu_T[0]);
3946 tcg_gen_add_i64(cpu_T[0], cpu_T[0], cpu_regs[reg]);
3947 tcg_gen_add_i64(cpu_T[0], cpu_T[0], carry_in);
3948 tcg_gen_ext32u_i64(cpu_regs[reg], cpu_T[0]);
3949 tcg_gen_shri_i64(carry_out, cpu_T[0], 32);
3950 break;
3951#endif
3952 default:
3953 /* Otherwise compute the carry-out in two steps. */
76f13133
RH
3954 zero = tcg_const_tl(0);
3955 tcg_gen_add2_tl(cpu_T[0], carry_out,
3956 cpu_T[0], zero,
3957 carry_in, zero);
3958 tcg_gen_add2_tl(cpu_regs[reg], carry_out,
3959 cpu_regs[reg], carry_out,
3960 cpu_T[0], zero);
3961 tcg_temp_free(zero);
cd7f97ca
RH
3962 break;
3963 }
cd7f97ca
RH
3964 set_cc_op(s, end_op);
3965 }
3966 break;
3967
4a554890
RH
3968 case 0x1f7: /* shlx Gy, Ey, By */
3969 case 0x2f7: /* sarx Gy, Ey, By */
3970 case 0x3f7: /* shrx Gy, Ey, By */
3971 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
3972 || !(s->prefix & PREFIX_VEX)
3973 || s->vex_l != 0) {
3974 goto illegal_op;
3975 }
ab4e4aec 3976 ot = mo_64_32(s->dflag);
4a554890 3977 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4ba9938c 3978 if (ot == MO_64) {
4a554890
RH
3979 tcg_gen_andi_tl(cpu_T[1], cpu_regs[s->vex_v], 63);
3980 } else {
3981 tcg_gen_andi_tl(cpu_T[1], cpu_regs[s->vex_v], 31);
3982 }
3983 if (b == 0x1f7) {
3984 tcg_gen_shl_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
3985 } else if (b == 0x2f7) {
4ba9938c 3986 if (ot != MO_64) {
4a554890
RH
3987 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
3988 }
3989 tcg_gen_sar_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
3990 } else {
4ba9938c 3991 if (ot != MO_64) {
4a554890
RH
3992 tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
3993 }
3994 tcg_gen_shr_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
3995 }
480a762d 3996 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
4a554890
RH
3997 break;
3998
bc4b43dc
RH
3999 case 0x0f3:
4000 case 0x1f3:
4001 case 0x2f3:
4002 case 0x3f3: /* Group 17 */
4003 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)
4004 || !(s->prefix & PREFIX_VEX)
4005 || s->vex_l != 0) {
4006 goto illegal_op;
4007 }
ab4e4aec 4008 ot = mo_64_32(s->dflag);
bc4b43dc
RH
4009 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4010
4011 switch (reg & 7) {
4012 case 1: /* blsr By,Ey */
4013 tcg_gen_neg_tl(cpu_T[1], cpu_T[0]);
4014 tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
480a762d 4015 gen_op_mov_reg_v(ot, s->vex_v, cpu_T[0]);
bc4b43dc
RH
4016 gen_op_update2_cc();
4017 set_cc_op(s, CC_OP_BMILGB + ot);
4018 break;
4019
4020 case 2: /* blsmsk By,Ey */
4021 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4022 tcg_gen_subi_tl(cpu_T[0], cpu_T[0], 1);
4023 tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_cc_src);
4024 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4025 set_cc_op(s, CC_OP_BMILGB + ot);
4026 break;
4027
4028 case 3: /* blsi By, Ey */
4029 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
4030 tcg_gen_subi_tl(cpu_T[0], cpu_T[0], 1);
4031 tcg_gen_and_tl(cpu_T[0], cpu_T[0], cpu_cc_src);
4032 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4033 set_cc_op(s, CC_OP_BMILGB + ot);
4034 break;
4035
4036 default:
4037 goto illegal_op;
4038 }
4039 break;
4040
111994ee
RH
4041 default:
4042 goto illegal_op;
4043 }
222a3336 4044 break;
111994ee 4045
222a3336
AZ
4046 case 0x03a:
4047 case 0x13a:
4242b1bd 4048 b = modrm;
0af10c86 4049 modrm = cpu_ldub_code(env, s->pc++);
4242b1bd
AZ
4050 rm = modrm & 7;
4051 reg = ((modrm >> 3) & 7) | rex_r;
4052 mod = (modrm >> 6) & 3;
c045af25
AK
4053 if (b1 >= 2) {
4054 goto illegal_op;
4055 }
4242b1bd 4056
d3eb5eae
BS
4057 sse_fn_eppi = sse_op_table7[b].op[b1];
4058 if (!sse_fn_eppi) {
4242b1bd 4059 goto illegal_op;
c4baa050 4060 }
222a3336
AZ
4061 if (!(s->cpuid_ext_features & sse_op_table7[b].ext_mask))
4062 goto illegal_op;
4063
d3eb5eae 4064 if (sse_fn_eppi == SSE_SPECIAL) {
ab4e4aec 4065 ot = mo_64_32(s->dflag);
222a3336
AZ
4066 rm = (modrm & 7) | REX_B(s);
4067 if (mod != 3)
4eeb3939 4068 gen_lea_modrm(env, s, modrm);
222a3336 4069 reg = ((modrm >> 3) & 7) | rex_r;
0af10c86 4070 val = cpu_ldub_code(env, s->pc++);
222a3336
AZ
4071 switch (b) {
4072 case 0x14: /* pextrb */
4073 tcg_gen_ld8u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
4074 xmm_regs[reg].XMM_B(val & 15)));
3523e4bd 4075 if (mod == 3) {
480a762d 4076 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
3523e4bd
RH
4077 } else {
4078 tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
4079 s->mem_index, MO_UB);
4080 }
222a3336
AZ
4081 break;
4082 case 0x15: /* pextrw */
4083 tcg_gen_ld16u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
4084 xmm_regs[reg].XMM_W(val & 7)));
3523e4bd 4085 if (mod == 3) {
480a762d 4086 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
3523e4bd
RH
4087 } else {
4088 tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
4089 s->mem_index, MO_LEUW);
4090 }
222a3336
AZ
4091 break;
4092 case 0x16:
4ba9938c 4093 if (ot == MO_32) { /* pextrd */
222a3336
AZ
4094 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
4095 offsetof(CPUX86State,
4096 xmm_regs[reg].XMM_L(val & 3)));
3523e4bd 4097 if (mod == 3) {
a7fbcbe5 4098 tcg_gen_extu_i32_tl(cpu_regs[rm], cpu_tmp2_i32);
3523e4bd 4099 } else {
d5601ad0
RH
4100 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
4101 s->mem_index, MO_LEUL);
3523e4bd 4102 }
222a3336 4103 } else { /* pextrq */
a7812ae4 4104#ifdef TARGET_X86_64
222a3336
AZ
4105 tcg_gen_ld_i64(cpu_tmp1_i64, cpu_env,
4106 offsetof(CPUX86State,
4107 xmm_regs[reg].XMM_Q(val & 1)));
3523e4bd 4108 if (mod == 3) {
a7fbcbe5 4109 tcg_gen_mov_i64(cpu_regs[rm], cpu_tmp1_i64);
3523e4bd
RH
4110 } else {
4111 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
4112 s->mem_index, MO_LEQ);
4113 }
a7812ae4
PB
4114#else
4115 goto illegal_op;
4116#endif
222a3336
AZ
4117 }
4118 break;
4119 case 0x17: /* extractps */
4120 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
4121 xmm_regs[reg].XMM_L(val & 3)));
3523e4bd 4122 if (mod == 3) {
480a762d 4123 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
3523e4bd
RH
4124 } else {
4125 tcg_gen_qemu_st_tl(cpu_T[0], cpu_A0,
4126 s->mem_index, MO_LEUL);
4127 }
222a3336
AZ
4128 break;
4129 case 0x20: /* pinsrb */
3c5f4116 4130 if (mod == 3) {
c56baccf 4131 gen_op_mov_v_reg(MO_32, cpu_T[0], rm);
3c5f4116
RH
4132 } else {
4133 tcg_gen_qemu_ld_tl(cpu_T[0], cpu_A0,
4134 s->mem_index, MO_UB);
4135 }
34c6addd 4136 tcg_gen_st8_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,
222a3336
AZ
4137 xmm_regs[reg].XMM_B(val & 15)));
4138 break;
4139 case 0x21: /* insertps */
a7812ae4 4140 if (mod == 3) {
222a3336
AZ
4141 tcg_gen_ld_i32(cpu_tmp2_i32, cpu_env,
4142 offsetof(CPUX86State,xmm_regs[rm]
4143 .XMM_L((val >> 6) & 3)));
a7812ae4 4144 } else {
3c5f4116
RH
4145 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
4146 s->mem_index, MO_LEUL);
a7812ae4 4147 }
222a3336
AZ
4148 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
4149 offsetof(CPUX86State,xmm_regs[reg]
4150 .XMM_L((val >> 4) & 3)));
4151 if ((val >> 0) & 1)
4152 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4153 cpu_env, offsetof(CPUX86State,
4154 xmm_regs[reg].XMM_L(0)));
4155 if ((val >> 1) & 1)
4156 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4157 cpu_env, offsetof(CPUX86State,
4158 xmm_regs[reg].XMM_L(1)));
4159 if ((val >> 2) & 1)
4160 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4161 cpu_env, offsetof(CPUX86State,
4162 xmm_regs[reg].XMM_L(2)));
4163 if ((val >> 3) & 1)
4164 tcg_gen_st_i32(tcg_const_i32(0 /*float32_zero*/),
4165 cpu_env, offsetof(CPUX86State,
4166 xmm_regs[reg].XMM_L(3)));
4167 break;
4168 case 0x22:
4ba9938c 4169 if (ot == MO_32) { /* pinsrd */
3c5f4116 4170 if (mod == 3) {
80b02013 4171 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_regs[rm]);
3c5f4116 4172 } else {
80b02013
RH
4173 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
4174 s->mem_index, MO_LEUL);
3c5f4116 4175 }
222a3336
AZ
4176 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env,
4177 offsetof(CPUX86State,
4178 xmm_regs[reg].XMM_L(val & 3)));
4179 } else { /* pinsrq */
a7812ae4 4180#ifdef TARGET_X86_64
3c5f4116 4181 if (mod == 3) {
222a3336 4182 gen_op_mov_v_reg(ot, cpu_tmp1_i64, rm);
3c5f4116
RH
4183 } else {
4184 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
4185 s->mem_index, MO_LEQ);
4186 }
222a3336
AZ
4187 tcg_gen_st_i64(cpu_tmp1_i64, cpu_env,
4188 offsetof(CPUX86State,
4189 xmm_regs[reg].XMM_Q(val & 1)));
a7812ae4
PB
4190#else
4191 goto illegal_op;
4192#endif
222a3336
AZ
4193 }
4194 break;
4195 }
4196 return;
4197 }
4242b1bd
AZ
4198
4199 if (b1) {
4200 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
4201 if (mod == 3) {
4202 op2_offset = offsetof(CPUX86State,xmm_regs[rm | REX_B(s)]);
4203 } else {
4204 op2_offset = offsetof(CPUX86State,xmm_t0);
4eeb3939 4205 gen_lea_modrm(env, s, modrm);
323d1876 4206 gen_ldo_env_A0(s, op2_offset);
4242b1bd
AZ
4207 }
4208 } else {
4209 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
4210 if (mod == 3) {
4211 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
4212 } else {
4213 op2_offset = offsetof(CPUX86State,mmx_t0);
4eeb3939 4214 gen_lea_modrm(env, s, modrm);
323d1876 4215 gen_ldq_env_A0(s, op2_offset);
4242b1bd
AZ
4216 }
4217 }
0af10c86 4218 val = cpu_ldub_code(env, s->pc++);
4242b1bd 4219
222a3336 4220 if ((b & 0xfc) == 0x60) { /* pcmpXstrX */
3ca51d07 4221 set_cc_op(s, CC_OP_EFLAGS);
222a3336 4222
ab4e4aec 4223 if (s->dflag == MO_64) {
222a3336
AZ
4224 /* The helper must use entire 64-bit gp registers */
4225 val |= 1 << 8;
ab4e4aec 4226 }
222a3336
AZ
4227 }
4228
4242b1bd
AZ
4229 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4230 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4231 sse_fn_eppi(cpu_env, cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
4242b1bd 4232 break;
e2c3c2c5
RH
4233
4234 case 0x33a:
4235 /* Various integer extensions at 0f 3a f[0-f]. */
4236 b = modrm | (b1 << 8);
4237 modrm = cpu_ldub_code(env, s->pc++);
4238 reg = ((modrm >> 3) & 7) | rex_r;
4239
4240 switch (b) {
4241 case 0x3f0: /* rorx Gy,Ey, Ib */
4242 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI2)
4243 || !(s->prefix & PREFIX_VEX)
4244 || s->vex_l != 0) {
4245 goto illegal_op;
4246 }
ab4e4aec 4247 ot = mo_64_32(s->dflag);
e2c3c2c5
RH
4248 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
4249 b = cpu_ldub_code(env, s->pc++);
4ba9938c 4250 if (ot == MO_64) {
e2c3c2c5
RH
4251 tcg_gen_rotri_tl(cpu_T[0], cpu_T[0], b & 63);
4252 } else {
4253 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4254 tcg_gen_rotri_i32(cpu_tmp2_i32, cpu_tmp2_i32, b & 31);
4255 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
4256 }
480a762d 4257 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
e2c3c2c5
RH
4258 break;
4259
4260 default:
4261 goto illegal_op;
4262 }
4263 break;
4264
664e0f19
FB
4265 default:
4266 goto illegal_op;
4267 }
4268 } else {
4269 /* generic MMX or SSE operation */
d1e42c5c 4270 switch(b) {
d1e42c5c
FB
4271 case 0x70: /* pshufx insn */
4272 case 0xc6: /* pshufx insn */
4273 case 0xc2: /* compare insns */
4274 s->rip_offset = 1;
4275 break;
4276 default:
4277 break;
664e0f19
FB
4278 }
4279 if (is_xmm) {
4280 op1_offset = offsetof(CPUX86State,xmm_regs[reg]);
4281 if (mod != 3) {
cb48da7f
RH
4282 int sz = 4;
4283
4eeb3939 4284 gen_lea_modrm(env, s, modrm);
664e0f19 4285 op2_offset = offsetof(CPUX86State,xmm_t0);
cb48da7f
RH
4286
4287 switch (b) {
4288 case 0x50 ... 0x5a:
4289 case 0x5c ... 0x5f:
4290 case 0xc2:
4291 /* Most sse scalar operations. */
664e0f19 4292 if (b1 == 2) {
cb48da7f
RH
4293 sz = 2;
4294 } else if (b1 == 3) {
4295 sz = 3;
4296 }
4297 break;
4298
4299 case 0x2e: /* ucomis[sd] */
4300 case 0x2f: /* comis[sd] */
4301 if (b1 == 0) {
4302 sz = 2;
664e0f19 4303 } else {
cb48da7f 4304 sz = 3;
664e0f19 4305 }
cb48da7f
RH
4306 break;
4307 }
4308
4309 switch (sz) {
4310 case 2:
4311 /* 32 bit access */
4312 gen_op_ld_v(s, MO_32, cpu_T[0], cpu_A0);
4313 tcg_gen_st32_tl(cpu_T[0], cpu_env,
4314 offsetof(CPUX86State,xmm_t0.XMM_L(0)));
4315 break;
4316 case 3:
4317 /* 64 bit access */
4318 gen_ldq_env_A0(s, offsetof(CPUX86State, xmm_t0.XMM_D(0)));
4319 break;
4320 default:
4321 /* 128 bit access */
323d1876 4322 gen_ldo_env_A0(s, op2_offset);
cb48da7f 4323 break;
664e0f19
FB
4324 }
4325 } else {
4326 rm = (modrm & 7) | REX_B(s);
4327 op2_offset = offsetof(CPUX86State,xmm_regs[rm]);
4328 }
4329 } else {
4330 op1_offset = offsetof(CPUX86State,fpregs[reg].mmx);
4331 if (mod != 3) {
4eeb3939 4332 gen_lea_modrm(env, s, modrm);
664e0f19 4333 op2_offset = offsetof(CPUX86State,mmx_t0);
323d1876 4334 gen_ldq_env_A0(s, op2_offset);
664e0f19
FB
4335 } else {
4336 rm = (modrm & 7);
4337 op2_offset = offsetof(CPUX86State,fpregs[rm].mmx);
4338 }
4339 }
4340 switch(b) {
a35f3ec7 4341 case 0x0f: /* 3DNow! data insns */
e771edab
AJ
4342 if (!(s->cpuid_ext2_features & CPUID_EXT2_3DNOW))
4343 goto illegal_op;
0af10c86 4344 val = cpu_ldub_code(env, s->pc++);
d3eb5eae
BS
4345 sse_fn_epp = sse_op_table5[val];
4346 if (!sse_fn_epp) {
a35f3ec7 4347 goto illegal_op;
c4baa050 4348 }
5af45186
FB
4349 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4350 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4351 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
a35f3ec7 4352 break;
664e0f19
FB
4353 case 0x70: /* pshufx insn */
4354 case 0xc6: /* pshufx insn */
0af10c86 4355 val = cpu_ldub_code(env, s->pc++);
5af45186
FB
4356 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4357 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
c4baa050 4358 /* XXX: introduce a new table? */
d3eb5eae 4359 sse_fn_ppi = (SSEFunc_0_ppi)sse_fn_epp;
c4baa050 4360 sse_fn_ppi(cpu_ptr0, cpu_ptr1, tcg_const_i32(val));
664e0f19
FB
4361 break;
4362 case 0xc2:
4363 /* compare insns */
0af10c86 4364 val = cpu_ldub_code(env, s->pc++);
664e0f19
FB
4365 if (val >= 8)
4366 goto illegal_op;
d3eb5eae 4367 sse_fn_epp = sse_op_table4[val][b1];
c4baa050 4368
5af45186
FB
4369 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4370 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4371 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19 4372 break;
b8b6a50b
FB
4373 case 0xf7:
4374 /* maskmov : we must prepare A0 */
4375 if (mod != 3)
4376 goto illegal_op;
1d71ddb1
RH
4377 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EDI]);
4378 gen_extu(s->aflag, cpu_A0);
b8b6a50b
FB
4379 gen_add_A0_ds_seg(s);
4380
4381 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4382 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
c4baa050 4383 /* XXX: introduce a new table? */
d3eb5eae
BS
4384 sse_fn_eppt = (SSEFunc_0_eppt)sse_fn_epp;
4385 sse_fn_eppt(cpu_env, cpu_ptr0, cpu_ptr1, cpu_A0);
b8b6a50b 4386 break;
664e0f19 4387 default:
5af45186
FB
4388 tcg_gen_addi_ptr(cpu_ptr0, cpu_env, op1_offset);
4389 tcg_gen_addi_ptr(cpu_ptr1, cpu_env, op2_offset);
d3eb5eae 4390 sse_fn_epp(cpu_env, cpu_ptr0, cpu_ptr1);
664e0f19
FB
4391 break;
4392 }
4393 if (b == 0x2e || b == 0x2f) {
3ca51d07 4394 set_cc_op(s, CC_OP_EFLAGS);
664e0f19
FB
4395 }
4396 }
4397}
4398
2c0262af
FB
4399/* convert one instruction. s->is_jmp is set if the translation must
4400 be stopped. Return the next pc value */
0af10c86
BS
4401static target_ulong disas_insn(CPUX86State *env, DisasContext *s,
4402 target_ulong pc_start)
2c0262af 4403{
ab4e4aec 4404 int b, prefixes;
d67dc9e6 4405 int shift;
ab4e4aec 4406 TCGMemOp ot, aflag, dflag;
4eeb3939 4407 int modrm, reg, rm, mod, op, opreg, val;
14ce26e7
FB
4408 target_ulong next_eip, tval;
4409 int rex_w, rex_r;
2c0262af 4410
fdefe51c 4411 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
70cff25e 4412 tcg_gen_debug_insn_start(pc_start);
fdefe51c 4413 }
2c0262af
FB
4414 s->pc = pc_start;
4415 prefixes = 0;
2c0262af 4416 s->override = -1;
14ce26e7
FB
4417 rex_w = -1;
4418 rex_r = 0;
4419#ifdef TARGET_X86_64
4420 s->rex_x = 0;
4421 s->rex_b = 0;
5fafdf24 4422 x86_64_hregs = 0;
14ce26e7
FB
4423#endif
4424 s->rip_offset = 0; /* for relative ip address */
701ed211
RH
4425 s->vex_l = 0;
4426 s->vex_v = 0;
2c0262af 4427 next_byte:
0af10c86 4428 b = cpu_ldub_code(env, s->pc);
2c0262af 4429 s->pc++;
4a6fd938
RH
4430 /* Collect prefixes. */
4431 switch (b) {
4432 case 0xf3:
4433 prefixes |= PREFIX_REPZ;
4434 goto next_byte;
4435 case 0xf2:
4436 prefixes |= PREFIX_REPNZ;
4437 goto next_byte;
4438 case 0xf0:
4439 prefixes |= PREFIX_LOCK;
4440 goto next_byte;
4441 case 0x2e:
4442 s->override = R_CS;
4443 goto next_byte;
4444 case 0x36:
4445 s->override = R_SS;
4446 goto next_byte;
4447 case 0x3e:
4448 s->override = R_DS;
4449 goto next_byte;
4450 case 0x26:
4451 s->override = R_ES;
4452 goto next_byte;
4453 case 0x64:
4454 s->override = R_FS;
4455 goto next_byte;
4456 case 0x65:
4457 s->override = R_GS;
4458 goto next_byte;
4459 case 0x66:
4460 prefixes |= PREFIX_DATA;
4461 goto next_byte;
4462 case 0x67:
4463 prefixes |= PREFIX_ADR;
4464 goto next_byte;
14ce26e7 4465#ifdef TARGET_X86_64
4a6fd938
RH
4466 case 0x40 ... 0x4f:
4467 if (CODE64(s)) {
14ce26e7
FB
4468 /* REX prefix */
4469 rex_w = (b >> 3) & 1;
4470 rex_r = (b & 0x4) << 1;
4471 s->rex_x = (b & 0x2) << 2;
4472 REX_B(s) = (b & 0x1) << 3;
4473 x86_64_hregs = 1; /* select uniform byte register addressing */
4474 goto next_byte;
4475 }
4a6fd938
RH
4476 break;
4477#endif
701ed211
RH
4478 case 0xc5: /* 2-byte VEX */
4479 case 0xc4: /* 3-byte VEX */
4480 /* VEX prefixes cannot be used except in 32-bit mode.
4481 Otherwise the instruction is LES or LDS. */
4482 if (s->code32 && !s->vm86) {
4483 static const int pp_prefix[4] = {
4484 0, PREFIX_DATA, PREFIX_REPZ, PREFIX_REPNZ
4485 };
4486 int vex3, vex2 = cpu_ldub_code(env, s->pc);
4487
4488 if (!CODE64(s) && (vex2 & 0xc0) != 0xc0) {
4489 /* 4.1.4.6: In 32-bit mode, bits [7:6] must be 11b,
4490 otherwise the instruction is LES or LDS. */
4491 break;
4492 }
4493 s->pc++;
4494
085d8134 4495 /* 4.1.1-4.1.3: No preceding lock, 66, f2, f3, or rex prefixes. */
701ed211
RH
4496 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ
4497 | PREFIX_LOCK | PREFIX_DATA)) {
4498 goto illegal_op;
4499 }
4500#ifdef TARGET_X86_64
4501 if (x86_64_hregs) {
4502 goto illegal_op;
4503 }
4504#endif
4505 rex_r = (~vex2 >> 4) & 8;
4506 if (b == 0xc5) {
4507 vex3 = vex2;
4508 b = cpu_ldub_code(env, s->pc++);
4509 } else {
4510#ifdef TARGET_X86_64
4511 s->rex_x = (~vex2 >> 3) & 8;
4512 s->rex_b = (~vex2 >> 2) & 8;
4513#endif
4514 vex3 = cpu_ldub_code(env, s->pc++);
4515 rex_w = (vex3 >> 7) & 1;
4516 switch (vex2 & 0x1f) {
4517 case 0x01: /* Implied 0f leading opcode bytes. */
4518 b = cpu_ldub_code(env, s->pc++) | 0x100;
4519 break;
4520 case 0x02: /* Implied 0f 38 leading opcode bytes. */
4521 b = 0x138;
4522 break;
4523 case 0x03: /* Implied 0f 3a leading opcode bytes. */
4524 b = 0x13a;
4525 break;
4526 default: /* Reserved for future use. */
4527 goto illegal_op;
4528 }
4529 }
4530 s->vex_v = (~vex3 >> 3) & 0xf;
4531 s->vex_l = (vex3 >> 2) & 1;
4532 prefixes |= pp_prefix[vex3 & 3] | PREFIX_VEX;
4533 }
4534 break;
4a6fd938
RH
4535 }
4536
4537 /* Post-process prefixes. */
4a6fd938 4538 if (CODE64(s)) {
dec3fc96
RH
4539 /* In 64-bit mode, the default data size is 32-bit. Select 64-bit
4540 data with rex_w, and 16-bit data with 0x66; rex_w takes precedence
4541 over 0x66 if both are present. */
ab4e4aec 4542 dflag = (rex_w > 0 ? MO_64 : prefixes & PREFIX_DATA ? MO_16 : MO_32);
dec3fc96 4543 /* In 64-bit mode, 0x67 selects 32-bit addressing. */
1d71ddb1 4544 aflag = (prefixes & PREFIX_ADR ? MO_32 : MO_64);
dec3fc96
RH
4545 } else {
4546 /* In 16/32-bit mode, 0x66 selects the opposite data size. */
ab4e4aec
RH
4547 if (s->code32 ^ ((prefixes & PREFIX_DATA) != 0)) {
4548 dflag = MO_32;
4549 } else {
4550 dflag = MO_16;
14ce26e7 4551 }
dec3fc96 4552 /* In 16/32-bit mode, 0x67 selects the opposite addressing. */
1d71ddb1
RH
4553 if (s->code32 ^ ((prefixes & PREFIX_ADR) != 0)) {
4554 aflag = MO_32;
4555 } else {
4556 aflag = MO_16;
14ce26e7 4557 }
2c0262af
FB
4558 }
4559
2c0262af
FB
4560 s->prefix = prefixes;
4561 s->aflag = aflag;
4562 s->dflag = dflag;
4563
4564 /* lock generation */
4565 if (prefixes & PREFIX_LOCK)
a7812ae4 4566 gen_helper_lock();
2c0262af
FB
4567
4568 /* now check op code */
4569 reswitch:
4570 switch(b) {
4571 case 0x0f:
4572 /**************************/
4573 /* extended op code */
0af10c86 4574 b = cpu_ldub_code(env, s->pc++) | 0x100;
2c0262af 4575 goto reswitch;
3b46e624 4576
2c0262af
FB
4577 /**************************/
4578 /* arith & logic */
4579 case 0x00 ... 0x05:
4580 case 0x08 ... 0x0d:
4581 case 0x10 ... 0x15:
4582 case 0x18 ... 0x1d:
4583 case 0x20 ... 0x25:
4584 case 0x28 ... 0x2d:
4585 case 0x30 ... 0x35:
4586 case 0x38 ... 0x3d:
4587 {
4588 int op, f, val;
4589 op = (b >> 3) & 7;
4590 f = (b >> 1) & 3;
4591
ab4e4aec 4592 ot = mo_b_d(b, dflag);
3b46e624 4593
2c0262af
FB
4594 switch(f) {
4595 case 0: /* OP Ev, Gv */
0af10c86 4596 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 4597 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 4598 mod = (modrm >> 6) & 3;
14ce26e7 4599 rm = (modrm & 7) | REX_B(s);
2c0262af 4600 if (mod != 3) {
4eeb3939 4601 gen_lea_modrm(env, s, modrm);
2c0262af
FB
4602 opreg = OR_TMP0;
4603 } else if (op == OP_XORL && rm == reg) {
4604 xor_zero:
4605 /* xor reg, reg optimisation */
436ff2d2 4606 set_cc_op(s, CC_OP_CLR);
97212c88 4607 tcg_gen_movi_tl(cpu_T[0], 0);
480a762d 4608 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
2c0262af
FB
4609 break;
4610 } else {
4611 opreg = rm;
4612 }
c56baccf 4613 gen_op_mov_v_reg(ot, cpu_T[1], reg);
2c0262af
FB
4614 gen_op(s, op, ot, opreg);
4615 break;
4616 case 1: /* OP Gv, Ev */
0af10c86 4617 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4618 mod = (modrm >> 6) & 3;
14ce26e7
FB
4619 reg = ((modrm >> 3) & 7) | rex_r;
4620 rm = (modrm & 7) | REX_B(s);
2c0262af 4621 if (mod != 3) {
4eeb3939 4622 gen_lea_modrm(env, s, modrm);
0f712e10 4623 gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
2c0262af
FB
4624 } else if (op == OP_XORL && rm == reg) {
4625 goto xor_zero;
4626 } else {
c56baccf 4627 gen_op_mov_v_reg(ot, cpu_T[1], rm);
2c0262af
FB
4628 }
4629 gen_op(s, op, ot, reg);
4630 break;
4631 case 2: /* OP A, Iv */
0af10c86 4632 val = insn_get(env, s, ot);
0ae657b1 4633 tcg_gen_movi_tl(cpu_T[1], val);
2c0262af
FB
4634 gen_op(s, op, ot, OR_EAX);
4635 break;
4636 }
4637 }
4638 break;
4639
ec9d6075
FB
4640 case 0x82:
4641 if (CODE64(s))
4642 goto illegal_op;
2c0262af
FB
4643 case 0x80: /* GRP1 */
4644 case 0x81:
4645 case 0x83:
4646 {
4647 int val;
4648
ab4e4aec 4649 ot = mo_b_d(b, dflag);
3b46e624 4650
0af10c86 4651 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4652 mod = (modrm >> 6) & 3;
14ce26e7 4653 rm = (modrm & 7) | REX_B(s);
2c0262af 4654 op = (modrm >> 3) & 7;
3b46e624 4655
2c0262af 4656 if (mod != 3) {
14ce26e7
FB
4657 if (b == 0x83)
4658 s->rip_offset = 1;
4659 else
4660 s->rip_offset = insn_const_size(ot);
4eeb3939 4661 gen_lea_modrm(env, s, modrm);
2c0262af
FB
4662 opreg = OR_TMP0;
4663 } else {
14ce26e7 4664 opreg = rm;
2c0262af
FB
4665 }
4666
4667 switch(b) {
4668 default:
4669 case 0x80:
4670 case 0x81:
d64477af 4671 case 0x82:
0af10c86 4672 val = insn_get(env, s, ot);
2c0262af
FB
4673 break;
4674 case 0x83:
4ba9938c 4675 val = (int8_t)insn_get(env, s, MO_8);
2c0262af
FB
4676 break;
4677 }
0ae657b1 4678 tcg_gen_movi_tl(cpu_T[1], val);
2c0262af
FB
4679 gen_op(s, op, ot, opreg);
4680 }
4681 break;
4682
4683 /**************************/
4684 /* inc, dec, and other misc arith */
4685 case 0x40 ... 0x47: /* inc Gv */
ab4e4aec 4686 ot = dflag;
2c0262af
FB
4687 gen_inc(s, ot, OR_EAX + (b & 7), 1);
4688 break;
4689 case 0x48 ... 0x4f: /* dec Gv */
ab4e4aec 4690 ot = dflag;
2c0262af
FB
4691 gen_inc(s, ot, OR_EAX + (b & 7), -1);
4692 break;
4693 case 0xf6: /* GRP3 */
4694 case 0xf7:
ab4e4aec 4695 ot = mo_b_d(b, dflag);
2c0262af 4696
0af10c86 4697 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4698 mod = (modrm >> 6) & 3;
14ce26e7 4699 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
4700 op = (modrm >> 3) & 7;
4701 if (mod != 3) {
14ce26e7
FB
4702 if (op == 0)
4703 s->rip_offset = insn_const_size(ot);
4eeb3939 4704 gen_lea_modrm(env, s, modrm);
909be183 4705 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 4706 } else {
c56baccf 4707 gen_op_mov_v_reg(ot, cpu_T[0], rm);
2c0262af
FB
4708 }
4709
4710 switch(op) {
4711 case 0: /* test */
0af10c86 4712 val = insn_get(env, s, ot);
0ae657b1 4713 tcg_gen_movi_tl(cpu_T[1], val);
2c0262af 4714 gen_op_testl_T0_T1_cc();
3ca51d07 4715 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af
FB
4716 break;
4717 case 2: /* not */
b6abf97d 4718 tcg_gen_not_tl(cpu_T[0], cpu_T[0]);
2c0262af 4719 if (mod != 3) {
fd8ca9f6 4720 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 4721 } else {
480a762d 4722 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
2c0262af
FB
4723 }
4724 break;
4725 case 3: /* neg */
b6abf97d 4726 tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
2c0262af 4727 if (mod != 3) {
fd8ca9f6 4728 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 4729 } else {
480a762d 4730 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
2c0262af
FB
4731 }
4732 gen_op_update_neg_cc();
3ca51d07 4733 set_cc_op(s, CC_OP_SUBB + ot);
2c0262af
FB
4734 break;
4735 case 4: /* mul */
4736 switch(ot) {
4ba9938c 4737 case MO_8:
c56baccf 4738 gen_op_mov_v_reg(MO_8, cpu_T[1], R_EAX);
0211e5af
FB
4739 tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
4740 tcg_gen_ext8u_tl(cpu_T[1], cpu_T[1]);
4741 /* XXX: use 32 bit mul which could be faster */
4742 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
480a762d 4743 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
0211e5af
FB
4744 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4745 tcg_gen_andi_tl(cpu_cc_src, cpu_T[0], 0xff00);
3ca51d07 4746 set_cc_op(s, CC_OP_MULB);
2c0262af 4747 break;
4ba9938c 4748 case MO_16:
c56baccf 4749 gen_op_mov_v_reg(MO_16, cpu_T[1], R_EAX);
0211e5af
FB
4750 tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
4751 tcg_gen_ext16u_tl(cpu_T[1], cpu_T[1]);
4752 /* XXX: use 32 bit mul which could be faster */
4753 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
480a762d 4754 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
0211e5af
FB
4755 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4756 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
480a762d 4757 gen_op_mov_reg_v(MO_16, R_EDX, cpu_T[0]);
0211e5af 4758 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
3ca51d07 4759 set_cc_op(s, CC_OP_MULW);
2c0262af
FB
4760 break;
4761 default:
4ba9938c 4762 case MO_32:
a4bcea3d
RH
4763 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4764 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
4765 tcg_gen_mulu2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
4766 cpu_tmp2_i32, cpu_tmp3_i32);
4767 tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
4768 tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
4769 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4770 tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
3ca51d07 4771 set_cc_op(s, CC_OP_MULL);
2c0262af 4772 break;
14ce26e7 4773#ifdef TARGET_X86_64
4ba9938c 4774 case MO_64:
a4bcea3d
RH
4775 tcg_gen_mulu2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
4776 cpu_T[0], cpu_regs[R_EAX]);
4777 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4778 tcg_gen_mov_tl(cpu_cc_src, cpu_regs[R_EDX]);
3ca51d07 4779 set_cc_op(s, CC_OP_MULQ);
14ce26e7
FB
4780 break;
4781#endif
2c0262af 4782 }
2c0262af
FB
4783 break;
4784 case 5: /* imul */
4785 switch(ot) {
4ba9938c 4786 case MO_8:
c56baccf 4787 gen_op_mov_v_reg(MO_8, cpu_T[1], R_EAX);
0211e5af
FB
4788 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
4789 tcg_gen_ext8s_tl(cpu_T[1], cpu_T[1]);
4790 /* XXX: use 32 bit mul which could be faster */
4791 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
480a762d 4792 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
0211e5af
FB
4793 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4794 tcg_gen_ext8s_tl(cpu_tmp0, cpu_T[0]);
4795 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
3ca51d07 4796 set_cc_op(s, CC_OP_MULB);
2c0262af 4797 break;
4ba9938c 4798 case MO_16:
c56baccf 4799 gen_op_mov_v_reg(MO_16, cpu_T[1], R_EAX);
0211e5af
FB
4800 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
4801 tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
4802 /* XXX: use 32 bit mul which could be faster */
4803 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
480a762d 4804 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
0211e5af
FB
4805 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
4806 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
4807 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
4808 tcg_gen_shri_tl(cpu_T[0], cpu_T[0], 16);
480a762d 4809 gen_op_mov_reg_v(MO_16, R_EDX, cpu_T[0]);
3ca51d07 4810 set_cc_op(s, CC_OP_MULW);
2c0262af
FB
4811 break;
4812 default:
4ba9938c 4813 case MO_32:
a4bcea3d
RH
4814 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4815 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_regs[R_EAX]);
4816 tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
4817 cpu_tmp2_i32, cpu_tmp3_i32);
4818 tcg_gen_extu_i32_tl(cpu_regs[R_EAX], cpu_tmp2_i32);
4819 tcg_gen_extu_i32_tl(cpu_regs[R_EDX], cpu_tmp3_i32);
4820 tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
4821 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4822 tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
4823 tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
3ca51d07 4824 set_cc_op(s, CC_OP_MULL);
2c0262af 4825 break;
14ce26e7 4826#ifdef TARGET_X86_64
4ba9938c 4827 case MO_64:
a4bcea3d
RH
4828 tcg_gen_muls2_i64(cpu_regs[R_EAX], cpu_regs[R_EDX],
4829 cpu_T[0], cpu_regs[R_EAX]);
4830 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[R_EAX]);
4831 tcg_gen_sari_tl(cpu_cc_src, cpu_regs[R_EAX], 63);
4832 tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_regs[R_EDX]);
3ca51d07 4833 set_cc_op(s, CC_OP_MULQ);
14ce26e7
FB
4834 break;
4835#endif
2c0262af 4836 }
2c0262af
FB
4837 break;
4838 case 6: /* div */
4839 switch(ot) {
4ba9938c 4840 case MO_8:
14ce26e7 4841 gen_jmp_im(pc_start - s->cs_base);
7923057b 4842 gen_helper_divb_AL(cpu_env, cpu_T[0]);
2c0262af 4843 break;
4ba9938c 4844 case MO_16:
14ce26e7 4845 gen_jmp_im(pc_start - s->cs_base);
7923057b 4846 gen_helper_divw_AX(cpu_env, cpu_T[0]);
2c0262af
FB
4847 break;
4848 default:
4ba9938c 4849 case MO_32:
14ce26e7 4850 gen_jmp_im(pc_start - s->cs_base);
7923057b 4851 gen_helper_divl_EAX(cpu_env, cpu_T[0]);
14ce26e7
FB
4852 break;
4853#ifdef TARGET_X86_64
4ba9938c 4854 case MO_64:
14ce26e7 4855 gen_jmp_im(pc_start - s->cs_base);
7923057b 4856 gen_helper_divq_EAX(cpu_env, cpu_T[0]);
2c0262af 4857 break;
14ce26e7 4858#endif
2c0262af
FB
4859 }
4860 break;
4861 case 7: /* idiv */
4862 switch(ot) {
4ba9938c 4863 case MO_8:
14ce26e7 4864 gen_jmp_im(pc_start - s->cs_base);
7923057b 4865 gen_helper_idivb_AL(cpu_env, cpu_T[0]);
2c0262af 4866 break;
4ba9938c 4867 case MO_16:
14ce26e7 4868 gen_jmp_im(pc_start - s->cs_base);
7923057b 4869 gen_helper_idivw_AX(cpu_env, cpu_T[0]);
2c0262af
FB
4870 break;
4871 default:
4ba9938c 4872 case MO_32:
14ce26e7 4873 gen_jmp_im(pc_start - s->cs_base);
7923057b 4874 gen_helper_idivl_EAX(cpu_env, cpu_T[0]);
14ce26e7
FB
4875 break;
4876#ifdef TARGET_X86_64
4ba9938c 4877 case MO_64:
14ce26e7 4878 gen_jmp_im(pc_start - s->cs_base);
7923057b 4879 gen_helper_idivq_EAX(cpu_env, cpu_T[0]);
2c0262af 4880 break;
14ce26e7 4881#endif
2c0262af
FB
4882 }
4883 break;
4884 default:
4885 goto illegal_op;
4886 }
4887 break;
4888
4889 case 0xfe: /* GRP4 */
4890 case 0xff: /* GRP5 */
ab4e4aec 4891 ot = mo_b_d(b, dflag);
2c0262af 4892
0af10c86 4893 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 4894 mod = (modrm >> 6) & 3;
14ce26e7 4895 rm = (modrm & 7) | REX_B(s);
2c0262af
FB
4896 op = (modrm >> 3) & 7;
4897 if (op >= 2 && b == 0xfe) {
4898 goto illegal_op;
4899 }
14ce26e7 4900 if (CODE64(s)) {
aba9d61e 4901 if (op == 2 || op == 4) {
14ce26e7 4902 /* operand size for jumps is 64 bit */
4ba9938c 4903 ot = MO_64;
aba9d61e 4904 } else if (op == 3 || op == 5) {
ab4e4aec 4905 ot = dflag != MO_16 ? MO_32 + (rex_w == 1) : MO_16;
14ce26e7
FB
4906 } else if (op == 6) {
4907 /* default push size is 64 bit */
ab4e4aec 4908 ot = mo_pushpop(s, dflag);
14ce26e7
FB
4909 }
4910 }
2c0262af 4911 if (mod != 3) {
4eeb3939 4912 gen_lea_modrm(env, s, modrm);
2c0262af 4913 if (op >= 2 && op != 3 && op != 5)
909be183 4914 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 4915 } else {
c56baccf 4916 gen_op_mov_v_reg(ot, cpu_T[0], rm);
2c0262af
FB
4917 }
4918
4919 switch(op) {
4920 case 0: /* inc Ev */
4921 if (mod != 3)
4922 opreg = OR_TMP0;
4923 else
4924 opreg = rm;
4925 gen_inc(s, ot, opreg, 1);
4926 break;
4927 case 1: /* dec Ev */
4928 if (mod != 3)
4929 opreg = OR_TMP0;
4930 else
4931 opreg = rm;
4932 gen_inc(s, ot, opreg, -1);
4933 break;
4934 case 2: /* call Ev */
4f31916f 4935 /* XXX: optimize if memory (no 'and' is necessary) */
ab4e4aec 4936 if (dflag == MO_16) {
40b90233
RH
4937 tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
4938 }
2c0262af 4939 next_eip = s->pc - s->cs_base;
cc0bce88 4940 tcg_gen_movi_tl(cpu_T[1], next_eip);
432baffe 4941 gen_push_v(s, cpu_T[1]);
74bdfbda 4942 gen_op_jmp_v(cpu_T[0]);
2c0262af
FB
4943 gen_eob(s);
4944 break;
61382a50 4945 case 3: /* lcall Ev */
0f712e10 4946 gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
830a19a4 4947 gen_add_A0_im(s, 1 << ot);
cc1a80df 4948 gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
2c0262af
FB
4949 do_lcall:
4950 if (s->pe && !s->vm86) {
773cdfcc 4951 gen_update_cc_op(s);
14ce26e7 4952 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 4953 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 4954 gen_helper_lcall_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
ab4e4aec 4955 tcg_const_i32(dflag - 1),
a7812ae4 4956 tcg_const_i32(s->pc - pc_start));
2c0262af 4957 } else {
b6abf97d 4958 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 4959 gen_helper_lcall_real(cpu_env, cpu_tmp2_i32, cpu_T[1],
ab4e4aec 4960 tcg_const_i32(dflag - 1),
a7812ae4 4961 tcg_const_i32(s->pc - s->cs_base));
2c0262af
FB
4962 }
4963 gen_eob(s);
4964 break;
4965 case 4: /* jmp Ev */
ab4e4aec 4966 if (dflag == MO_16) {
40b90233
RH
4967 tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
4968 }
74bdfbda 4969 gen_op_jmp_v(cpu_T[0]);
2c0262af
FB
4970 gen_eob(s);
4971 break;
4972 case 5: /* ljmp Ev */
0f712e10 4973 gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
830a19a4 4974 gen_add_A0_im(s, 1 << ot);
cc1a80df 4975 gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
2c0262af
FB
4976 do_ljmp:
4977 if (s->pe && !s->vm86) {
773cdfcc 4978 gen_update_cc_op(s);
14ce26e7 4979 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 4980 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 4981 gen_helper_ljmp_protected(cpu_env, cpu_tmp2_i32, cpu_T[1],
a7812ae4 4982 tcg_const_i32(s->pc - pc_start));
2c0262af 4983 } else {
3bd7da9e 4984 gen_op_movl_seg_T0_vm(R_CS);
78261634 4985 gen_op_jmp_v(cpu_T[1]);
2c0262af
FB
4986 }
4987 gen_eob(s);
4988 break;
4989 case 6: /* push Ev */
432baffe 4990 gen_push_v(s, cpu_T[0]);
2c0262af
FB
4991 break;
4992 default:
4993 goto illegal_op;
4994 }
4995 break;
4996
4997 case 0x84: /* test Ev, Gv */
5fafdf24 4998 case 0x85:
ab4e4aec 4999 ot = mo_b_d(b, dflag);
2c0262af 5000
0af10c86 5001 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5002 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5003
0af10c86 5004 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
c56baccf 5005 gen_op_mov_v_reg(ot, cpu_T[1], reg);
2c0262af 5006 gen_op_testl_T0_T1_cc();
3ca51d07 5007 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af 5008 break;
3b46e624 5009
2c0262af
FB
5010 case 0xa8: /* test eAX, Iv */
5011 case 0xa9:
ab4e4aec 5012 ot = mo_b_d(b, dflag);
0af10c86 5013 val = insn_get(env, s, ot);
2c0262af 5014
c56baccf 5015 gen_op_mov_v_reg(ot, cpu_T[0], OR_EAX);
0ae657b1 5016 tcg_gen_movi_tl(cpu_T[1], val);
2c0262af 5017 gen_op_testl_T0_T1_cc();
3ca51d07 5018 set_cc_op(s, CC_OP_LOGICB + ot);
2c0262af 5019 break;
3b46e624 5020
2c0262af 5021 case 0x98: /* CWDE/CBW */
ab4e4aec 5022 switch (dflag) {
14ce26e7 5023#ifdef TARGET_X86_64
ab4e4aec 5024 case MO_64:
c56baccf 5025 gen_op_mov_v_reg(MO_32, cpu_T[0], R_EAX);
e108dd01 5026 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
480a762d 5027 gen_op_mov_reg_v(MO_64, R_EAX, cpu_T[0]);
ab4e4aec 5028 break;
14ce26e7 5029#endif
ab4e4aec 5030 case MO_32:
c56baccf 5031 gen_op_mov_v_reg(MO_16, cpu_T[0], R_EAX);
e108dd01 5032 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
480a762d 5033 gen_op_mov_reg_v(MO_32, R_EAX, cpu_T[0]);
ab4e4aec
RH
5034 break;
5035 case MO_16:
c56baccf 5036 gen_op_mov_v_reg(MO_8, cpu_T[0], R_EAX);
e108dd01 5037 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
480a762d 5038 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
ab4e4aec
RH
5039 break;
5040 default:
5041 tcg_abort();
e108dd01 5042 }
2c0262af
FB
5043 break;
5044 case 0x99: /* CDQ/CWD */
ab4e4aec 5045 switch (dflag) {
14ce26e7 5046#ifdef TARGET_X86_64
ab4e4aec 5047 case MO_64:
c56baccf 5048 gen_op_mov_v_reg(MO_64, cpu_T[0], R_EAX);
e108dd01 5049 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 63);
480a762d 5050 gen_op_mov_reg_v(MO_64, R_EDX, cpu_T[0]);
ab4e4aec 5051 break;
14ce26e7 5052#endif
ab4e4aec 5053 case MO_32:
c56baccf 5054 gen_op_mov_v_reg(MO_32, cpu_T[0], R_EAX);
e108dd01
FB
5055 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
5056 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 31);
480a762d 5057 gen_op_mov_reg_v(MO_32, R_EDX, cpu_T[0]);
ab4e4aec
RH
5058 break;
5059 case MO_16:
c56baccf 5060 gen_op_mov_v_reg(MO_16, cpu_T[0], R_EAX);
e108dd01
FB
5061 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
5062 tcg_gen_sari_tl(cpu_T[0], cpu_T[0], 15);
480a762d 5063 gen_op_mov_reg_v(MO_16, R_EDX, cpu_T[0]);
ab4e4aec
RH
5064 break;
5065 default:
5066 tcg_abort();
e108dd01 5067 }
2c0262af
FB
5068 break;
5069 case 0x1af: /* imul Gv, Ev */
5070 case 0x69: /* imul Gv, Ev, I */
5071 case 0x6b:
ab4e4aec 5072 ot = dflag;
0af10c86 5073 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7
FB
5074 reg = ((modrm >> 3) & 7) | rex_r;
5075 if (b == 0x69)
5076 s->rip_offset = insn_const_size(ot);
5077 else if (b == 0x6b)
5078 s->rip_offset = 1;
0af10c86 5079 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
2c0262af 5080 if (b == 0x69) {
0af10c86 5081 val = insn_get(env, s, ot);
0ae657b1 5082 tcg_gen_movi_tl(cpu_T[1], val);
2c0262af 5083 } else if (b == 0x6b) {
4ba9938c 5084 val = (int8_t)insn_get(env, s, MO_8);
0ae657b1 5085 tcg_gen_movi_tl(cpu_T[1], val);
2c0262af 5086 } else {
c56baccf 5087 gen_op_mov_v_reg(ot, cpu_T[1], reg);
2c0262af 5088 }
a4bcea3d 5089 switch (ot) {
0211e5af 5090#ifdef TARGET_X86_64
4ba9938c 5091 case MO_64:
a4bcea3d
RH
5092 tcg_gen_muls2_i64(cpu_regs[reg], cpu_T[1], cpu_T[0], cpu_T[1]);
5093 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
5094 tcg_gen_sari_tl(cpu_cc_src, cpu_cc_dst, 63);
5095 tcg_gen_sub_tl(cpu_cc_src, cpu_cc_src, cpu_T[1]);
5096 break;
0211e5af 5097#endif
4ba9938c 5098 case MO_32:
a4bcea3d
RH
5099 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
5100 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
5101 tcg_gen_muls2_i32(cpu_tmp2_i32, cpu_tmp3_i32,
5102 cpu_tmp2_i32, cpu_tmp3_i32);
5103 tcg_gen_extu_i32_tl(cpu_regs[reg], cpu_tmp2_i32);
5104 tcg_gen_sari_i32(cpu_tmp2_i32, cpu_tmp2_i32, 31);
5105 tcg_gen_mov_tl(cpu_cc_dst, cpu_regs[reg]);
5106 tcg_gen_sub_i32(cpu_tmp2_i32, cpu_tmp2_i32, cpu_tmp3_i32);
5107 tcg_gen_extu_i32_tl(cpu_cc_src, cpu_tmp2_i32);
5108 break;
5109 default:
0211e5af
FB
5110 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
5111 tcg_gen_ext16s_tl(cpu_T[1], cpu_T[1]);
5112 /* XXX: use 32 bit mul which could be faster */
5113 tcg_gen_mul_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
5114 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
5115 tcg_gen_ext16s_tl(cpu_tmp0, cpu_T[0]);
5116 tcg_gen_sub_tl(cpu_cc_src, cpu_T[0], cpu_tmp0);
480a762d 5117 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
a4bcea3d 5118 break;
2c0262af 5119 }
3ca51d07 5120 set_cc_op(s, CC_OP_MULB + ot);
2c0262af
FB
5121 break;
5122 case 0x1c0:
5123 case 0x1c1: /* xadd Ev, Gv */
ab4e4aec 5124 ot = mo_b_d(b, dflag);
0af10c86 5125 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5126 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5127 mod = (modrm >> 6) & 3;
5128 if (mod == 3) {
14ce26e7 5129 rm = (modrm & 7) | REX_B(s);
c56baccf
RH
5130 gen_op_mov_v_reg(ot, cpu_T[0], reg);
5131 gen_op_mov_v_reg(ot, cpu_T[1], rm);
fd5185ec 5132 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
68773f84 5133 gen_op_mov_reg_v(ot, reg, cpu_T[1]);
480a762d 5134 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
2c0262af 5135 } else {
4eeb3939 5136 gen_lea_modrm(env, s, modrm);
c56baccf 5137 gen_op_mov_v_reg(ot, cpu_T[0], reg);
0f712e10 5138 gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
fd5185ec 5139 tcg_gen_add_tl(cpu_T[0], cpu_T[0], cpu_T[1]);
fd8ca9f6 5140 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
68773f84 5141 gen_op_mov_reg_v(ot, reg, cpu_T[1]);
2c0262af
FB
5142 }
5143 gen_op_update2_cc();
3ca51d07 5144 set_cc_op(s, CC_OP_ADDB + ot);
2c0262af
FB
5145 break;
5146 case 0x1b0:
5147 case 0x1b1: /* cmpxchg Ev, Gv */
cad3a37d 5148 {
1130328e 5149 int label1, label2;
1e4840bf 5150 TCGv t0, t1, t2, a0;
cad3a37d 5151
ab4e4aec 5152 ot = mo_b_d(b, dflag);
0af10c86 5153 modrm = cpu_ldub_code(env, s->pc++);
cad3a37d
FB
5154 reg = ((modrm >> 3) & 7) | rex_r;
5155 mod = (modrm >> 6) & 3;
a7812ae4
PB
5156 t0 = tcg_temp_local_new();
5157 t1 = tcg_temp_local_new();
5158 t2 = tcg_temp_local_new();
5159 a0 = tcg_temp_local_new();
1e4840bf 5160 gen_op_mov_v_reg(ot, t1, reg);
cad3a37d
FB
5161 if (mod == 3) {
5162 rm = (modrm & 7) | REX_B(s);
1e4840bf 5163 gen_op_mov_v_reg(ot, t0, rm);
cad3a37d 5164 } else {
4eeb3939 5165 gen_lea_modrm(env, s, modrm);
1e4840bf 5166 tcg_gen_mov_tl(a0, cpu_A0);
323d1876 5167 gen_op_ld_v(s, ot, t0, a0);
cad3a37d
FB
5168 rm = 0; /* avoid warning */
5169 }
5170 label1 = gen_new_label();
a3251186
RH
5171 tcg_gen_mov_tl(t2, cpu_regs[R_EAX]);
5172 gen_extu(ot, t0);
1e4840bf 5173 gen_extu(ot, t2);
a3251186 5174 tcg_gen_brcond_tl(TCG_COND_EQ, t2, t0, label1);
f7e80adf 5175 label2 = gen_new_label();
cad3a37d 5176 if (mod == 3) {
1e4840bf 5177 gen_op_mov_reg_v(ot, R_EAX, t0);
1130328e
FB
5178 tcg_gen_br(label2);
5179 gen_set_label(label1);
1e4840bf 5180 gen_op_mov_reg_v(ot, rm, t1);
cad3a37d 5181 } else {
f7e80adf
AG
5182 /* perform no-op store cycle like physical cpu; must be
5183 before changing accumulator to ensure idempotency if
5184 the store faults and the instruction is restarted */
323d1876 5185 gen_op_st_v(s, ot, t0, a0);
1e4840bf 5186 gen_op_mov_reg_v(ot, R_EAX, t0);
f7e80adf 5187 tcg_gen_br(label2);
1130328e 5188 gen_set_label(label1);
323d1876 5189 gen_op_st_v(s, ot, t1, a0);
cad3a37d 5190 }
f7e80adf 5191 gen_set_label(label2);
1e4840bf 5192 tcg_gen_mov_tl(cpu_cc_src, t0);
a3251186
RH
5193 tcg_gen_mov_tl(cpu_cc_srcT, t2);
5194 tcg_gen_sub_tl(cpu_cc_dst, t2, t0);
3ca51d07 5195 set_cc_op(s, CC_OP_SUBB + ot);
1e4840bf
FB
5196 tcg_temp_free(t0);
5197 tcg_temp_free(t1);
5198 tcg_temp_free(t2);
5199 tcg_temp_free(a0);
2c0262af 5200 }
2c0262af
FB
5201 break;
5202 case 0x1c7: /* cmpxchg8b */
0af10c86 5203 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5204 mod = (modrm >> 6) & 3;
71c3558e 5205 if ((mod == 3) || ((modrm & 0x38) != 0x8))
2c0262af 5206 goto illegal_op;
1b9d9ebb 5207#ifdef TARGET_X86_64
ab4e4aec 5208 if (dflag == MO_64) {
1b9d9ebb
FB
5209 if (!(s->cpuid_ext_features & CPUID_EXT_CX16))
5210 goto illegal_op;
5211 gen_jmp_im(pc_start - s->cs_base);
773cdfcc 5212 gen_update_cc_op(s);
4eeb3939 5213 gen_lea_modrm(env, s, modrm);
92fc4b58 5214 gen_helper_cmpxchg16b(cpu_env, cpu_A0);
1b9d9ebb
FB
5215 } else
5216#endif
5217 {
5218 if (!(s->cpuid_features & CPUID_CX8))
5219 goto illegal_op;
5220 gen_jmp_im(pc_start - s->cs_base);
773cdfcc 5221 gen_update_cc_op(s);
4eeb3939 5222 gen_lea_modrm(env, s, modrm);
92fc4b58 5223 gen_helper_cmpxchg8b(cpu_env, cpu_A0);
1b9d9ebb 5224 }
3ca51d07 5225 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 5226 break;
3b46e624 5227
2c0262af
FB
5228 /**************************/
5229 /* push/pop */
5230 case 0x50 ... 0x57: /* push */
c56baccf 5231 gen_op_mov_v_reg(MO_32, cpu_T[0], (b & 7) | REX_B(s));
432baffe 5232 gen_push_v(s, cpu_T[0]);
2c0262af
FB
5233 break;
5234 case 0x58 ... 0x5f: /* pop */
8e31d234 5235 ot = gen_pop_T0(s);
77729c24 5236 /* NOTE: order is important for pop %sp */
8e31d234 5237 gen_pop_update(s, ot);
480a762d 5238 gen_op_mov_reg_v(ot, (b & 7) | REX_B(s), cpu_T[0]);
2c0262af
FB
5239 break;
5240 case 0x60: /* pusha */
14ce26e7
FB
5241 if (CODE64(s))
5242 goto illegal_op;
2c0262af
FB
5243 gen_pusha(s);
5244 break;
5245 case 0x61: /* popa */
14ce26e7
FB
5246 if (CODE64(s))
5247 goto illegal_op;
2c0262af
FB
5248 gen_popa(s);
5249 break;
5250 case 0x68: /* push Iv */
5251 case 0x6a:
ab4e4aec 5252 ot = mo_pushpop(s, dflag);
2c0262af 5253 if (b == 0x68)
0af10c86 5254 val = insn_get(env, s, ot);
2c0262af 5255 else
4ba9938c 5256 val = (int8_t)insn_get(env, s, MO_8);
1b90d56e 5257 tcg_gen_movi_tl(cpu_T[0], val);
432baffe 5258 gen_push_v(s, cpu_T[0]);
2c0262af
FB
5259 break;
5260 case 0x8f: /* pop Ev */
0af10c86 5261 modrm = cpu_ldub_code(env, s->pc++);
77729c24 5262 mod = (modrm >> 6) & 3;
8e31d234 5263 ot = gen_pop_T0(s);
77729c24
FB
5264 if (mod == 3) {
5265 /* NOTE: order is important for pop %sp */
8e31d234 5266 gen_pop_update(s, ot);
14ce26e7 5267 rm = (modrm & 7) | REX_B(s);
480a762d 5268 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
77729c24
FB
5269 } else {
5270 /* NOTE: order is important too for MMU exceptions */
14ce26e7 5271 s->popl_esp_hack = 1 << ot;
0af10c86 5272 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
77729c24 5273 s->popl_esp_hack = 0;
8e31d234 5274 gen_pop_update(s, ot);
77729c24 5275 }
2c0262af
FB
5276 break;
5277 case 0xc8: /* enter */
5278 {
5279 int level;
0af10c86 5280 val = cpu_lduw_code(env, s->pc);
2c0262af 5281 s->pc += 2;
0af10c86 5282 level = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5283 gen_enter(s, val, level);
5284 }
5285 break;
5286 case 0xc9: /* leave */
5287 /* XXX: exception not precise (ESP is updated before potential exception) */
14ce26e7 5288 if (CODE64(s)) {
c56baccf 5289 gen_op_mov_v_reg(MO_64, cpu_T[0], R_EBP);
480a762d 5290 gen_op_mov_reg_v(MO_64, R_ESP, cpu_T[0]);
14ce26e7 5291 } else if (s->ss32) {
c56baccf 5292 gen_op_mov_v_reg(MO_32, cpu_T[0], R_EBP);
480a762d 5293 gen_op_mov_reg_v(MO_32, R_ESP, cpu_T[0]);
2c0262af 5294 } else {
c56baccf 5295 gen_op_mov_v_reg(MO_16, cpu_T[0], R_EBP);
480a762d 5296 gen_op_mov_reg_v(MO_16, R_ESP, cpu_T[0]);
2c0262af 5297 }
8e31d234 5298 ot = gen_pop_T0(s);
480a762d 5299 gen_op_mov_reg_v(ot, R_EBP, cpu_T[0]);
8e31d234 5300 gen_pop_update(s, ot);
2c0262af
FB
5301 break;
5302 case 0x06: /* push es */
5303 case 0x0e: /* push cs */
5304 case 0x16: /* push ss */
5305 case 0x1e: /* push ds */
14ce26e7
FB
5306 if (CODE64(s))
5307 goto illegal_op;
2c0262af 5308 gen_op_movl_T0_seg(b >> 3);
432baffe 5309 gen_push_v(s, cpu_T[0]);
2c0262af
FB
5310 break;
5311 case 0x1a0: /* push fs */
5312 case 0x1a8: /* push gs */
5313 gen_op_movl_T0_seg((b >> 3) & 7);
432baffe 5314 gen_push_v(s, cpu_T[0]);
2c0262af
FB
5315 break;
5316 case 0x07: /* pop es */
5317 case 0x17: /* pop ss */
5318 case 0x1f: /* pop ds */
14ce26e7
FB
5319 if (CODE64(s))
5320 goto illegal_op;
2c0262af 5321 reg = b >> 3;
8e31d234 5322 ot = gen_pop_T0(s);
2c0262af 5323 gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
8e31d234 5324 gen_pop_update(s, ot);
2c0262af 5325 if (reg == R_SS) {
a2cc3b24
FB
5326 /* if reg == SS, inhibit interrupts/trace. */
5327 /* If several instructions disable interrupts, only the
5328 _first_ does it */
5329 if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
f0967a1a 5330 gen_helper_set_inhibit_irq(cpu_env);
2c0262af
FB
5331 s->tf = 0;
5332 }
5333 if (s->is_jmp) {
14ce26e7 5334 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5335 gen_eob(s);
5336 }
5337 break;
5338 case 0x1a1: /* pop fs */
5339 case 0x1a9: /* pop gs */
8e31d234 5340 ot = gen_pop_T0(s);
2c0262af 5341 gen_movl_seg_T0(s, (b >> 3) & 7, pc_start - s->cs_base);
8e31d234 5342 gen_pop_update(s, ot);
2c0262af 5343 if (s->is_jmp) {
14ce26e7 5344 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5345 gen_eob(s);
5346 }
5347 break;
5348
5349 /**************************/
5350 /* mov */
5351 case 0x88:
5352 case 0x89: /* mov Gv, Ev */
ab4e4aec 5353 ot = mo_b_d(b, dflag);
0af10c86 5354 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5355 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5356
2c0262af 5357 /* generate a generic store */
0af10c86 5358 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
2c0262af
FB
5359 break;
5360 case 0xc6:
5361 case 0xc7: /* mov Ev, Iv */
ab4e4aec 5362 ot = mo_b_d(b, dflag);
0af10c86 5363 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5364 mod = (modrm >> 6) & 3;
14ce26e7
FB
5365 if (mod != 3) {
5366 s->rip_offset = insn_const_size(ot);
4eeb3939 5367 gen_lea_modrm(env, s, modrm);
14ce26e7 5368 }
0af10c86 5369 val = insn_get(env, s, ot);
1b90d56e 5370 tcg_gen_movi_tl(cpu_T[0], val);
fd8ca9f6
RH
5371 if (mod != 3) {
5372 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
5373 } else {
480a762d 5374 gen_op_mov_reg_v(ot, (modrm & 7) | REX_B(s), cpu_T[0]);
fd8ca9f6 5375 }
2c0262af
FB
5376 break;
5377 case 0x8a:
5378 case 0x8b: /* mov Ev, Gv */
ab4e4aec 5379 ot = mo_b_d(b, dflag);
0af10c86 5380 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5381 reg = ((modrm >> 3) & 7) | rex_r;
3b46e624 5382
0af10c86 5383 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
480a762d 5384 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
2c0262af
FB
5385 break;
5386 case 0x8e: /* mov seg, Gv */
0af10c86 5387 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5388 reg = (modrm >> 3) & 7;
5389 if (reg >= 6 || reg == R_CS)
5390 goto illegal_op;
4ba9938c 5391 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
2c0262af
FB
5392 gen_movl_seg_T0(s, reg, pc_start - s->cs_base);
5393 if (reg == R_SS) {
5394 /* if reg == SS, inhibit interrupts/trace */
a2cc3b24
FB
5395 /* If several instructions disable interrupts, only the
5396 _first_ does it */
5397 if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
f0967a1a 5398 gen_helper_set_inhibit_irq(cpu_env);
2c0262af
FB
5399 s->tf = 0;
5400 }
5401 if (s->is_jmp) {
14ce26e7 5402 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5403 gen_eob(s);
5404 }
5405 break;
5406 case 0x8c: /* mov Gv, seg */
0af10c86 5407 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5408 reg = (modrm >> 3) & 7;
5409 mod = (modrm >> 6) & 3;
5410 if (reg >= 6)
5411 goto illegal_op;
5412 gen_op_movl_T0_seg(reg);
ab4e4aec 5413 ot = mod == 3 ? dflag : MO_16;
0af10c86 5414 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
5415 break;
5416
5417 case 0x1b6: /* movzbS Gv, Eb */
5418 case 0x1b7: /* movzwS Gv, Eb */
5419 case 0x1be: /* movsbS Gv, Eb */
5420 case 0x1bf: /* movswS Gv, Eb */
5421 {
c8fbc479
RH
5422 TCGMemOp d_ot;
5423 TCGMemOp s_ot;
5424
2c0262af 5425 /* d_ot is the size of destination */
ab4e4aec 5426 d_ot = dflag;
2c0262af 5427 /* ot is the size of source */
4ba9938c 5428 ot = (b & 1) + MO_8;
c8fbc479
RH
5429 /* s_ot is the sign+size of source */
5430 s_ot = b & 8 ? MO_SIGN | ot : ot;
5431
0af10c86 5432 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5433 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 5434 mod = (modrm >> 6) & 3;
14ce26e7 5435 rm = (modrm & 7) | REX_B(s);
3b46e624 5436
2c0262af 5437 if (mod == 3) {
c56baccf 5438 gen_op_mov_v_reg(ot, cpu_T[0], rm);
c8fbc479
RH
5439 switch (s_ot) {
5440 case MO_UB:
e108dd01 5441 tcg_gen_ext8u_tl(cpu_T[0], cpu_T[0]);
2c0262af 5442 break;
c8fbc479 5443 case MO_SB:
e108dd01 5444 tcg_gen_ext8s_tl(cpu_T[0], cpu_T[0]);
2c0262af 5445 break;
c8fbc479 5446 case MO_UW:
e108dd01 5447 tcg_gen_ext16u_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5448 break;
5449 default:
c8fbc479 5450 case MO_SW:
e108dd01 5451 tcg_gen_ext16s_tl(cpu_T[0], cpu_T[0]);
2c0262af
FB
5452 break;
5453 }
480a762d 5454 gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
2c0262af 5455 } else {
4eeb3939 5456 gen_lea_modrm(env, s, modrm);
c8fbc479 5457 gen_op_ld_v(s, s_ot, cpu_T[0], cpu_A0);
480a762d 5458 gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
2c0262af
FB
5459 }
5460 }
5461 break;
5462
5463 case 0x8d: /* lea */
ab4e4aec 5464 ot = dflag;
0af10c86 5465 modrm = cpu_ldub_code(env, s->pc++);
3a1d9b8b
FB
5466 mod = (modrm >> 6) & 3;
5467 if (mod == 3)
5468 goto illegal_op;
14ce26e7 5469 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5470 /* we must ensure that no segment is added */
5471 s->override = -1;
5472 val = s->addseg;
5473 s->addseg = 0;
4eeb3939 5474 gen_lea_modrm(env, s, modrm);
2c0262af 5475 s->addseg = val;
33b7891b 5476 gen_op_mov_reg_v(ot, reg, cpu_A0);
2c0262af 5477 break;
3b46e624 5478
2c0262af
FB
5479 case 0xa0: /* mov EAX, Ov */
5480 case 0xa1:
5481 case 0xa2: /* mov Ov, EAX */
5482 case 0xa3:
2c0262af 5483 {
14ce26e7
FB
5484 target_ulong offset_addr;
5485
ab4e4aec 5486 ot = mo_b_d(b, dflag);
1d71ddb1 5487 switch (s->aflag) {
14ce26e7 5488#ifdef TARGET_X86_64
1d71ddb1 5489 case MO_64:
0af10c86 5490 offset_addr = cpu_ldq_code(env, s->pc);
14ce26e7 5491 s->pc += 8;
1d71ddb1 5492 break;
14ce26e7 5493#endif
1d71ddb1
RH
5494 default:
5495 offset_addr = insn_get(env, s, s->aflag);
5496 break;
14ce26e7 5497 }
3250cff8 5498 tcg_gen_movi_tl(cpu_A0, offset_addr);
664e0f19 5499 gen_add_A0_ds_seg(s);
14ce26e7 5500 if ((b & 2) == 0) {
909be183 5501 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
480a762d 5502 gen_op_mov_reg_v(ot, R_EAX, cpu_T[0]);
14ce26e7 5503 } else {
c56baccf 5504 gen_op_mov_v_reg(ot, cpu_T[0], R_EAX);
fd8ca9f6 5505 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2c0262af
FB
5506 }
5507 }
2c0262af
FB
5508 break;
5509 case 0xd7: /* xlat */
1d71ddb1
RH
5510 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EBX]);
5511 tcg_gen_ext8u_tl(cpu_T[0], cpu_regs[R_EAX]);
5512 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_T[0]);
5513 gen_extu(s->aflag, cpu_A0);
664e0f19 5514 gen_add_A0_ds_seg(s);
cc1a80df 5515 gen_op_ld_v(s, MO_8, cpu_T[0], cpu_A0);
480a762d 5516 gen_op_mov_reg_v(MO_8, R_EAX, cpu_T[0]);
2c0262af
FB
5517 break;
5518 case 0xb0 ... 0xb7: /* mov R, Ib */
4ba9938c 5519 val = insn_get(env, s, MO_8);
1b90d56e 5520 tcg_gen_movi_tl(cpu_T[0], val);
480a762d 5521 gen_op_mov_reg_v(MO_8, (b & 7) | REX_B(s), cpu_T[0]);
2c0262af
FB
5522 break;
5523 case 0xb8 ... 0xbf: /* mov R, Iv */
14ce26e7 5524#ifdef TARGET_X86_64
ab4e4aec 5525 if (dflag == MO_64) {
14ce26e7
FB
5526 uint64_t tmp;
5527 /* 64 bit case */
0af10c86 5528 tmp = cpu_ldq_code(env, s->pc);
14ce26e7
FB
5529 s->pc += 8;
5530 reg = (b & 7) | REX_B(s);
cc0bce88 5531 tcg_gen_movi_tl(cpu_T[0], tmp);
480a762d 5532 gen_op_mov_reg_v(MO_64, reg, cpu_T[0]);
5fafdf24 5533 } else
14ce26e7
FB
5534#endif
5535 {
ab4e4aec 5536 ot = dflag;
0af10c86 5537 val = insn_get(env, s, ot);
14ce26e7 5538 reg = (b & 7) | REX_B(s);
1b90d56e 5539 tcg_gen_movi_tl(cpu_T[0], val);
480a762d 5540 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
14ce26e7 5541 }
2c0262af
FB
5542 break;
5543
5544 case 0x91 ... 0x97: /* xchg R, EAX */
7418027e 5545 do_xchg_reg_eax:
ab4e4aec 5546 ot = dflag;
14ce26e7 5547 reg = (b & 7) | REX_B(s);
2c0262af
FB
5548 rm = R_EAX;
5549 goto do_xchg_reg;
5550 case 0x86:
5551 case 0x87: /* xchg Ev, Gv */
ab4e4aec 5552 ot = mo_b_d(b, dflag);
0af10c86 5553 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5554 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5555 mod = (modrm >> 6) & 3;
5556 if (mod == 3) {
14ce26e7 5557 rm = (modrm & 7) | REX_B(s);
2c0262af 5558 do_xchg_reg:
c56baccf
RH
5559 gen_op_mov_v_reg(ot, cpu_T[0], reg);
5560 gen_op_mov_v_reg(ot, cpu_T[1], rm);
480a762d 5561 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
68773f84 5562 gen_op_mov_reg_v(ot, reg, cpu_T[1]);
2c0262af 5563 } else {
4eeb3939 5564 gen_lea_modrm(env, s, modrm);
c56baccf 5565 gen_op_mov_v_reg(ot, cpu_T[0], reg);
2c0262af
FB
5566 /* for xchg, lock is implicit */
5567 if (!(prefixes & PREFIX_LOCK))
a7812ae4 5568 gen_helper_lock();
0f712e10 5569 gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
fd8ca9f6 5570 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 5571 if (!(prefixes & PREFIX_LOCK))
a7812ae4 5572 gen_helper_unlock();
68773f84 5573 gen_op_mov_reg_v(ot, reg, cpu_T[1]);
2c0262af
FB
5574 }
5575 break;
5576 case 0xc4: /* les Gv */
701ed211 5577 /* In CODE64 this is VEX3; see above. */
2c0262af
FB
5578 op = R_ES;
5579 goto do_lxx;
5580 case 0xc5: /* lds Gv */
701ed211 5581 /* In CODE64 this is VEX2; see above. */
2c0262af
FB
5582 op = R_DS;
5583 goto do_lxx;
5584 case 0x1b2: /* lss Gv */
5585 op = R_SS;
5586 goto do_lxx;
5587 case 0x1b4: /* lfs Gv */
5588 op = R_FS;
5589 goto do_lxx;
5590 case 0x1b5: /* lgs Gv */
5591 op = R_GS;
5592 do_lxx:
ab4e4aec 5593 ot = dflag != MO_16 ? MO_32 : MO_16;
0af10c86 5594 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 5595 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af
FB
5596 mod = (modrm >> 6) & 3;
5597 if (mod == 3)
5598 goto illegal_op;
4eeb3939 5599 gen_lea_modrm(env, s, modrm);
0f712e10 5600 gen_op_ld_v(s, ot, cpu_T[1], cpu_A0);
830a19a4 5601 gen_add_A0_im(s, 1 << ot);
2c0262af 5602 /* load the segment first to handle exceptions properly */
cc1a80df 5603 gen_op_ld_v(s, MO_16, cpu_T[0], cpu_A0);
2c0262af
FB
5604 gen_movl_seg_T0(s, op, pc_start - s->cs_base);
5605 /* then put the data */
68773f84 5606 gen_op_mov_reg_v(ot, reg, cpu_T[1]);
2c0262af 5607 if (s->is_jmp) {
14ce26e7 5608 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
5609 gen_eob(s);
5610 }
5611 break;
3b46e624 5612
2c0262af
FB
5613 /************************/
5614 /* shifts */
5615 case 0xc0:
5616 case 0xc1:
5617 /* shift Ev,Ib */
5618 shift = 2;
5619 grp2:
5620 {
ab4e4aec 5621 ot = mo_b_d(b, dflag);
0af10c86 5622 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5623 mod = (modrm >> 6) & 3;
2c0262af 5624 op = (modrm >> 3) & 7;
3b46e624 5625
2c0262af 5626 if (mod != 3) {
14ce26e7
FB
5627 if (shift == 2) {
5628 s->rip_offset = 1;
5629 }
4eeb3939 5630 gen_lea_modrm(env, s, modrm);
2c0262af
FB
5631 opreg = OR_TMP0;
5632 } else {
14ce26e7 5633 opreg = (modrm & 7) | REX_B(s);
2c0262af
FB
5634 }
5635
5636 /* simpler op */
5637 if (shift == 0) {
5638 gen_shift(s, op, ot, opreg, OR_ECX);
5639 } else {
5640 if (shift == 2) {
0af10c86 5641 shift = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5642 }
5643 gen_shifti(s, op, ot, opreg, shift);
5644 }
5645 }
5646 break;
5647 case 0xd0:
5648 case 0xd1:
5649 /* shift Ev,1 */
5650 shift = 1;
5651 goto grp2;
5652 case 0xd2:
5653 case 0xd3:
5654 /* shift Ev,cl */
5655 shift = 0;
5656 goto grp2;
5657
5658 case 0x1a4: /* shld imm */
5659 op = 0;
5660 shift = 1;
5661 goto do_shiftd;
5662 case 0x1a5: /* shld cl */
5663 op = 0;
5664 shift = 0;
5665 goto do_shiftd;
5666 case 0x1ac: /* shrd imm */
5667 op = 1;
5668 shift = 1;
5669 goto do_shiftd;
5670 case 0x1ad: /* shrd cl */
5671 op = 1;
5672 shift = 0;
5673 do_shiftd:
ab4e4aec 5674 ot = dflag;
0af10c86 5675 modrm = cpu_ldub_code(env, s->pc++);
2c0262af 5676 mod = (modrm >> 6) & 3;
14ce26e7
FB
5677 rm = (modrm & 7) | REX_B(s);
5678 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 5679 if (mod != 3) {
4eeb3939 5680 gen_lea_modrm(env, s, modrm);
b6abf97d 5681 opreg = OR_TMP0;
2c0262af 5682 } else {
b6abf97d 5683 opreg = rm;
2c0262af 5684 }
c56baccf 5685 gen_op_mov_v_reg(ot, cpu_T[1], reg);
3b46e624 5686
2c0262af 5687 if (shift) {
3b9d3cf1
PB
5688 TCGv imm = tcg_const_tl(cpu_ldub_code(env, s->pc++));
5689 gen_shiftd_rm_T1(s, ot, opreg, op, imm);
5690 tcg_temp_free(imm);
2c0262af 5691 } else {
3b9d3cf1 5692 gen_shiftd_rm_T1(s, ot, opreg, op, cpu_regs[R_ECX]);
2c0262af
FB
5693 }
5694 break;
5695
5696 /************************/
5697 /* floats */
5fafdf24 5698 case 0xd8 ... 0xdf:
7eee2a50
FB
5699 if (s->flags & (HF_EM_MASK | HF_TS_MASK)) {
5700 /* if CR0.EM or CR0.TS are set, generate an FPU exception */
5701 /* XXX: what to do if illegal op ? */
5702 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
5703 break;
5704 }
0af10c86 5705 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
5706 mod = (modrm >> 6) & 3;
5707 rm = modrm & 7;
5708 op = ((b & 7) << 3) | ((modrm >> 3) & 7);
2c0262af
FB
5709 if (mod != 3) {
5710 /* memory op */
4eeb3939 5711 gen_lea_modrm(env, s, modrm);
2c0262af
FB
5712 switch(op) {
5713 case 0x00 ... 0x07: /* fxxxs */
5714 case 0x10 ... 0x17: /* fixxxl */
5715 case 0x20 ... 0x27: /* fxxxl */
5716 case 0x30 ... 0x37: /* fixxx */
5717 {
5718 int op1;
5719 op1 = op & 7;
5720
5721 switch(op >> 4) {
5722 case 0:
80b02013
RH
5723 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5724 s->mem_index, MO_LEUL);
d3eb5eae 5725 gen_helper_flds_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5726 break;
5727 case 1:
80b02013
RH
5728 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5729 s->mem_index, MO_LEUL);
d3eb5eae 5730 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5731 break;
5732 case 2:
3c5f4116
RH
5733 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
5734 s->mem_index, MO_LEQ);
d3eb5eae 5735 gen_helper_fldl_FT0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5736 break;
5737 case 3:
5738 default:
80b02013
RH
5739 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5740 s->mem_index, MO_LESW);
d3eb5eae 5741 gen_helper_fildl_FT0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5742 break;
5743 }
3b46e624 5744
a7812ae4 5745 gen_helper_fp_arith_ST0_FT0(op1);
2c0262af
FB
5746 if (op1 == 3) {
5747 /* fcomp needs pop */
d3eb5eae 5748 gen_helper_fpop(cpu_env);
2c0262af
FB
5749 }
5750 }
5751 break;
5752 case 0x08: /* flds */
5753 case 0x0a: /* fsts */
5754 case 0x0b: /* fstps */
465e9838
FB
5755 case 0x18 ... 0x1b: /* fildl, fisttpl, fistl, fistpl */
5756 case 0x28 ... 0x2b: /* fldl, fisttpll, fstl, fstpl */
5757 case 0x38 ... 0x3b: /* filds, fisttps, fists, fistps */
2c0262af
FB
5758 switch(op & 7) {
5759 case 0:
5760 switch(op >> 4) {
5761 case 0:
80b02013
RH
5762 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5763 s->mem_index, MO_LEUL);
d3eb5eae 5764 gen_helper_flds_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5765 break;
5766 case 1:
80b02013
RH
5767 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5768 s->mem_index, MO_LEUL);
d3eb5eae 5769 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5770 break;
5771 case 2:
3c5f4116
RH
5772 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0,
5773 s->mem_index, MO_LEQ);
d3eb5eae 5774 gen_helper_fldl_ST0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5775 break;
5776 case 3:
5777 default:
80b02013
RH
5778 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5779 s->mem_index, MO_LESW);
d3eb5eae 5780 gen_helper_fildl_ST0(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5781 break;
5782 }
5783 break;
465e9838 5784 case 1:
19e6c4b8 5785 /* XXX: the corresponding CPUID bit must be tested ! */
465e9838
FB
5786 switch(op >> 4) {
5787 case 1:
d3eb5eae 5788 gen_helper_fisttl_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5789 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5790 s->mem_index, MO_LEUL);
465e9838
FB
5791 break;
5792 case 2:
d3eb5eae 5793 gen_helper_fisttll_ST0(cpu_tmp1_i64, cpu_env);
3523e4bd
RH
5794 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
5795 s->mem_index, MO_LEQ);
465e9838
FB
5796 break;
5797 case 3:
5798 default:
d3eb5eae 5799 gen_helper_fistt_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5800 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5801 s->mem_index, MO_LEUW);
19e6c4b8 5802 break;
465e9838 5803 }
d3eb5eae 5804 gen_helper_fpop(cpu_env);
465e9838 5805 break;
2c0262af
FB
5806 default:
5807 switch(op >> 4) {
5808 case 0:
d3eb5eae 5809 gen_helper_fsts_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5810 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5811 s->mem_index, MO_LEUL);
2c0262af
FB
5812 break;
5813 case 1:
d3eb5eae 5814 gen_helper_fistl_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5815 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5816 s->mem_index, MO_LEUL);
2c0262af
FB
5817 break;
5818 case 2:
d3eb5eae 5819 gen_helper_fstl_ST0(cpu_tmp1_i64, cpu_env);
3523e4bd
RH
5820 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0,
5821 s->mem_index, MO_LEQ);
2c0262af
FB
5822 break;
5823 case 3:
5824 default:
d3eb5eae 5825 gen_helper_fist_ST0(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5826 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5827 s->mem_index, MO_LEUW);
2c0262af
FB
5828 break;
5829 }
5830 if ((op & 7) == 3)
d3eb5eae 5831 gen_helper_fpop(cpu_env);
2c0262af
FB
5832 break;
5833 }
5834 break;
5835 case 0x0c: /* fldenv mem */
773cdfcc 5836 gen_update_cc_op(s);
19e6c4b8 5837 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 5838 gen_helper_fldenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
2c0262af
FB
5839 break;
5840 case 0x0d: /* fldcw mem */
80b02013
RH
5841 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
5842 s->mem_index, MO_LEUW);
d3eb5eae 5843 gen_helper_fldcw(cpu_env, cpu_tmp2_i32);
2c0262af
FB
5844 break;
5845 case 0x0e: /* fnstenv mem */
773cdfcc 5846 gen_update_cc_op(s);
19e6c4b8 5847 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 5848 gen_helper_fstenv(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
2c0262af
FB
5849 break;
5850 case 0x0f: /* fnstcw mem */
d3eb5eae 5851 gen_helper_fnstcw(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5852 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5853 s->mem_index, MO_LEUW);
2c0262af
FB
5854 break;
5855 case 0x1d: /* fldt mem */
773cdfcc 5856 gen_update_cc_op(s);
19e6c4b8 5857 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5858 gen_helper_fldt_ST0(cpu_env, cpu_A0);
2c0262af
FB
5859 break;
5860 case 0x1f: /* fstpt mem */
773cdfcc 5861 gen_update_cc_op(s);
19e6c4b8 5862 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae
BS
5863 gen_helper_fstt_ST0(cpu_env, cpu_A0);
5864 gen_helper_fpop(cpu_env);
2c0262af
FB
5865 break;
5866 case 0x2c: /* frstor mem */
773cdfcc 5867 gen_update_cc_op(s);
19e6c4b8 5868 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 5869 gen_helper_frstor(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
2c0262af
FB
5870 break;
5871 case 0x2e: /* fnsave mem */
773cdfcc 5872 gen_update_cc_op(s);
19e6c4b8 5873 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 5874 gen_helper_fsave(cpu_env, cpu_A0, tcg_const_i32(dflag - 1));
2c0262af
FB
5875 break;
5876 case 0x2f: /* fnstsw mem */
d3eb5eae 5877 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
d5601ad0
RH
5878 tcg_gen_qemu_st_i32(cpu_tmp2_i32, cpu_A0,
5879 s->mem_index, MO_LEUW);
2c0262af
FB
5880 break;
5881 case 0x3c: /* fbld */
773cdfcc 5882 gen_update_cc_op(s);
19e6c4b8 5883 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5884 gen_helper_fbld_ST0(cpu_env, cpu_A0);
2c0262af
FB
5885 break;
5886 case 0x3e: /* fbstp */
773cdfcc 5887 gen_update_cc_op(s);
19e6c4b8 5888 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae
BS
5889 gen_helper_fbst_ST0(cpu_env, cpu_A0);
5890 gen_helper_fpop(cpu_env);
2c0262af
FB
5891 break;
5892 case 0x3d: /* fildll */
3c5f4116 5893 tcg_gen_qemu_ld_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
d3eb5eae 5894 gen_helper_fildll_ST0(cpu_env, cpu_tmp1_i64);
2c0262af
FB
5895 break;
5896 case 0x3f: /* fistpll */
d3eb5eae 5897 gen_helper_fistll_ST0(cpu_tmp1_i64, cpu_env);
3523e4bd 5898 tcg_gen_qemu_st_i64(cpu_tmp1_i64, cpu_A0, s->mem_index, MO_LEQ);
d3eb5eae 5899 gen_helper_fpop(cpu_env);
2c0262af
FB
5900 break;
5901 default:
5902 goto illegal_op;
5903 }
5904 } else {
5905 /* register float ops */
5906 opreg = rm;
5907
5908 switch(op) {
5909 case 0x08: /* fld sti */
d3eb5eae
BS
5910 gen_helper_fpush(cpu_env);
5911 gen_helper_fmov_ST0_STN(cpu_env,
5912 tcg_const_i32((opreg + 1) & 7));
2c0262af
FB
5913 break;
5914 case 0x09: /* fxchg sti */
c169c906
FB
5915 case 0x29: /* fxchg4 sti, undocumented op */
5916 case 0x39: /* fxchg7 sti, undocumented op */
d3eb5eae 5917 gen_helper_fxchg_ST0_STN(cpu_env, tcg_const_i32(opreg));
2c0262af
FB
5918 break;
5919 case 0x0a: /* grp d9/2 */
5920 switch(rm) {
5921 case 0: /* fnop */
023fe10d 5922 /* check exceptions (FreeBSD FPU probe) */
773cdfcc 5923 gen_update_cc_op(s);
14ce26e7 5924 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 5925 gen_helper_fwait(cpu_env);
2c0262af
FB
5926 break;
5927 default:
5928 goto illegal_op;
5929 }
5930 break;
5931 case 0x0c: /* grp d9/4 */
5932 switch(rm) {
5933 case 0: /* fchs */
d3eb5eae 5934 gen_helper_fchs_ST0(cpu_env);
2c0262af
FB
5935 break;
5936 case 1: /* fabs */
d3eb5eae 5937 gen_helper_fabs_ST0(cpu_env);
2c0262af
FB
5938 break;
5939 case 4: /* ftst */
d3eb5eae
BS
5940 gen_helper_fldz_FT0(cpu_env);
5941 gen_helper_fcom_ST0_FT0(cpu_env);
2c0262af
FB
5942 break;
5943 case 5: /* fxam */
d3eb5eae 5944 gen_helper_fxam_ST0(cpu_env);
2c0262af
FB
5945 break;
5946 default:
5947 goto illegal_op;
5948 }
5949 break;
5950 case 0x0d: /* grp d9/5 */
5951 {
5952 switch(rm) {
5953 case 0:
d3eb5eae
BS
5954 gen_helper_fpush(cpu_env);
5955 gen_helper_fld1_ST0(cpu_env);
2c0262af
FB
5956 break;
5957 case 1:
d3eb5eae
BS
5958 gen_helper_fpush(cpu_env);
5959 gen_helper_fldl2t_ST0(cpu_env);
2c0262af
FB
5960 break;
5961 case 2:
d3eb5eae
BS
5962 gen_helper_fpush(cpu_env);
5963 gen_helper_fldl2e_ST0(cpu_env);
2c0262af
FB
5964 break;
5965 case 3:
d3eb5eae
BS
5966 gen_helper_fpush(cpu_env);
5967 gen_helper_fldpi_ST0(cpu_env);
2c0262af
FB
5968 break;
5969 case 4:
d3eb5eae
BS
5970 gen_helper_fpush(cpu_env);
5971 gen_helper_fldlg2_ST0(cpu_env);
2c0262af
FB
5972 break;
5973 case 5:
d3eb5eae
BS
5974 gen_helper_fpush(cpu_env);
5975 gen_helper_fldln2_ST0(cpu_env);
2c0262af
FB
5976 break;
5977 case 6:
d3eb5eae
BS
5978 gen_helper_fpush(cpu_env);
5979 gen_helper_fldz_ST0(cpu_env);
2c0262af
FB
5980 break;
5981 default:
5982 goto illegal_op;
5983 }
5984 }
5985 break;
5986 case 0x0e: /* grp d9/6 */
5987 switch(rm) {
5988 case 0: /* f2xm1 */
d3eb5eae 5989 gen_helper_f2xm1(cpu_env);
2c0262af
FB
5990 break;
5991 case 1: /* fyl2x */
d3eb5eae 5992 gen_helper_fyl2x(cpu_env);
2c0262af
FB
5993 break;
5994 case 2: /* fptan */
d3eb5eae 5995 gen_helper_fptan(cpu_env);
2c0262af
FB
5996 break;
5997 case 3: /* fpatan */
d3eb5eae 5998 gen_helper_fpatan(cpu_env);
2c0262af
FB
5999 break;
6000 case 4: /* fxtract */
d3eb5eae 6001 gen_helper_fxtract(cpu_env);
2c0262af
FB
6002 break;
6003 case 5: /* fprem1 */
d3eb5eae 6004 gen_helper_fprem1(cpu_env);
2c0262af
FB
6005 break;
6006 case 6: /* fdecstp */
d3eb5eae 6007 gen_helper_fdecstp(cpu_env);
2c0262af
FB
6008 break;
6009 default:
6010 case 7: /* fincstp */
d3eb5eae 6011 gen_helper_fincstp(cpu_env);
2c0262af
FB
6012 break;
6013 }
6014 break;
6015 case 0x0f: /* grp d9/7 */
6016 switch(rm) {
6017 case 0: /* fprem */
d3eb5eae 6018 gen_helper_fprem(cpu_env);
2c0262af
FB
6019 break;
6020 case 1: /* fyl2xp1 */
d3eb5eae 6021 gen_helper_fyl2xp1(cpu_env);
2c0262af
FB
6022 break;
6023 case 2: /* fsqrt */
d3eb5eae 6024 gen_helper_fsqrt(cpu_env);
2c0262af
FB
6025 break;
6026 case 3: /* fsincos */
d3eb5eae 6027 gen_helper_fsincos(cpu_env);
2c0262af
FB
6028 break;
6029 case 5: /* fscale */
d3eb5eae 6030 gen_helper_fscale(cpu_env);
2c0262af
FB
6031 break;
6032 case 4: /* frndint */
d3eb5eae 6033 gen_helper_frndint(cpu_env);
2c0262af
FB
6034 break;
6035 case 6: /* fsin */
d3eb5eae 6036 gen_helper_fsin(cpu_env);
2c0262af
FB
6037 break;
6038 default:
6039 case 7: /* fcos */
d3eb5eae 6040 gen_helper_fcos(cpu_env);
2c0262af
FB
6041 break;
6042 }
6043 break;
6044 case 0x00: case 0x01: case 0x04 ... 0x07: /* fxxx st, sti */
6045 case 0x20: case 0x21: case 0x24 ... 0x27: /* fxxx sti, st */
6046 case 0x30: case 0x31: case 0x34 ... 0x37: /* fxxxp sti, st */
6047 {
6048 int op1;
3b46e624 6049
2c0262af
FB
6050 op1 = op & 7;
6051 if (op >= 0x20) {
a7812ae4 6052 gen_helper_fp_arith_STN_ST0(op1, opreg);
2c0262af 6053 if (op >= 0x30)
d3eb5eae 6054 gen_helper_fpop(cpu_env);
2c0262af 6055 } else {
d3eb5eae 6056 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
a7812ae4 6057 gen_helper_fp_arith_ST0_FT0(op1);
2c0262af
FB
6058 }
6059 }
6060 break;
6061 case 0x02: /* fcom */
c169c906 6062 case 0x22: /* fcom2, undocumented op */
d3eb5eae
BS
6063 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6064 gen_helper_fcom_ST0_FT0(cpu_env);
2c0262af
FB
6065 break;
6066 case 0x03: /* fcomp */
c169c906
FB
6067 case 0x23: /* fcomp3, undocumented op */
6068 case 0x32: /* fcomp5, undocumented op */
d3eb5eae
BS
6069 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6070 gen_helper_fcom_ST0_FT0(cpu_env);
6071 gen_helper_fpop(cpu_env);
2c0262af
FB
6072 break;
6073 case 0x15: /* da/5 */
6074 switch(rm) {
6075 case 1: /* fucompp */
d3eb5eae
BS
6076 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
6077 gen_helper_fucom_ST0_FT0(cpu_env);
6078 gen_helper_fpop(cpu_env);
6079 gen_helper_fpop(cpu_env);
2c0262af
FB
6080 break;
6081 default:
6082 goto illegal_op;
6083 }
6084 break;
6085 case 0x1c:
6086 switch(rm) {
6087 case 0: /* feni (287 only, just do nop here) */
6088 break;
6089 case 1: /* fdisi (287 only, just do nop here) */
6090 break;
6091 case 2: /* fclex */
d3eb5eae 6092 gen_helper_fclex(cpu_env);
2c0262af
FB
6093 break;
6094 case 3: /* fninit */
d3eb5eae 6095 gen_helper_fninit(cpu_env);
2c0262af
FB
6096 break;
6097 case 4: /* fsetpm (287 only, just do nop here) */
6098 break;
6099 default:
6100 goto illegal_op;
6101 }
6102 break;
6103 case 0x1d: /* fucomi */
bff93281
PM
6104 if (!(s->cpuid_features & CPUID_CMOV)) {
6105 goto illegal_op;
6106 }
773cdfcc 6107 gen_update_cc_op(s);
d3eb5eae
BS
6108 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6109 gen_helper_fucomi_ST0_FT0(cpu_env);
3ca51d07 6110 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6111 break;
6112 case 0x1e: /* fcomi */
bff93281
PM
6113 if (!(s->cpuid_features & CPUID_CMOV)) {
6114 goto illegal_op;
6115 }
773cdfcc 6116 gen_update_cc_op(s);
d3eb5eae
BS
6117 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6118 gen_helper_fcomi_ST0_FT0(cpu_env);
3ca51d07 6119 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 6120 break;
658c8bda 6121 case 0x28: /* ffree sti */
d3eb5eae 6122 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
5fafdf24 6123 break;
2c0262af 6124 case 0x2a: /* fst sti */
d3eb5eae 6125 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
2c0262af
FB
6126 break;
6127 case 0x2b: /* fstp sti */
c169c906
FB
6128 case 0x0b: /* fstp1 sti, undocumented op */
6129 case 0x3a: /* fstp8 sti, undocumented op */
6130 case 0x3b: /* fstp9 sti, undocumented op */
d3eb5eae
BS
6131 gen_helper_fmov_STN_ST0(cpu_env, tcg_const_i32(opreg));
6132 gen_helper_fpop(cpu_env);
2c0262af
FB
6133 break;
6134 case 0x2c: /* fucom st(i) */
d3eb5eae
BS
6135 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6136 gen_helper_fucom_ST0_FT0(cpu_env);
2c0262af
FB
6137 break;
6138 case 0x2d: /* fucomp st(i) */
d3eb5eae
BS
6139 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6140 gen_helper_fucom_ST0_FT0(cpu_env);
6141 gen_helper_fpop(cpu_env);
2c0262af
FB
6142 break;
6143 case 0x33: /* de/3 */
6144 switch(rm) {
6145 case 1: /* fcompp */
d3eb5eae
BS
6146 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(1));
6147 gen_helper_fcom_ST0_FT0(cpu_env);
6148 gen_helper_fpop(cpu_env);
6149 gen_helper_fpop(cpu_env);
2c0262af
FB
6150 break;
6151 default:
6152 goto illegal_op;
6153 }
6154 break;
c169c906 6155 case 0x38: /* ffreep sti, undocumented op */
d3eb5eae
BS
6156 gen_helper_ffree_STN(cpu_env, tcg_const_i32(opreg));
6157 gen_helper_fpop(cpu_env);
c169c906 6158 break;
2c0262af
FB
6159 case 0x3c: /* df/4 */
6160 switch(rm) {
6161 case 0:
d3eb5eae 6162 gen_helper_fnstsw(cpu_tmp2_i32, cpu_env);
b6abf97d 6163 tcg_gen_extu_i32_tl(cpu_T[0], cpu_tmp2_i32);
480a762d 6164 gen_op_mov_reg_v(MO_16, R_EAX, cpu_T[0]);
2c0262af
FB
6165 break;
6166 default:
6167 goto illegal_op;
6168 }
6169 break;
6170 case 0x3d: /* fucomip */
bff93281
PM
6171 if (!(s->cpuid_features & CPUID_CMOV)) {
6172 goto illegal_op;
6173 }
773cdfcc 6174 gen_update_cc_op(s);
d3eb5eae
BS
6175 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6176 gen_helper_fucomi_ST0_FT0(cpu_env);
6177 gen_helper_fpop(cpu_env);
3ca51d07 6178 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6179 break;
6180 case 0x3e: /* fcomip */
bff93281
PM
6181 if (!(s->cpuid_features & CPUID_CMOV)) {
6182 goto illegal_op;
6183 }
773cdfcc 6184 gen_update_cc_op(s);
d3eb5eae
BS
6185 gen_helper_fmov_FT0_STN(cpu_env, tcg_const_i32(opreg));
6186 gen_helper_fcomi_ST0_FT0(cpu_env);
6187 gen_helper_fpop(cpu_env);
3ca51d07 6188 set_cc_op(s, CC_OP_EFLAGS);
2c0262af 6189 break;
a2cc3b24
FB
6190 case 0x10 ... 0x13: /* fcmovxx */
6191 case 0x18 ... 0x1b:
6192 {
19e6c4b8 6193 int op1, l1;
d70040bc 6194 static const uint8_t fcmov_cc[8] = {
a2cc3b24
FB
6195 (JCC_B << 1),
6196 (JCC_Z << 1),
6197 (JCC_BE << 1),
6198 (JCC_P << 1),
6199 };
bff93281
PM
6200
6201 if (!(s->cpuid_features & CPUID_CMOV)) {
6202 goto illegal_op;
6203 }
1e4840bf 6204 op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
19e6c4b8 6205 l1 = gen_new_label();
dc259201 6206 gen_jcc1_noeob(s, op1, l1);
d3eb5eae 6207 gen_helper_fmov_ST0_STN(cpu_env, tcg_const_i32(opreg));
19e6c4b8 6208 gen_set_label(l1);
a2cc3b24
FB
6209 }
6210 break;
2c0262af
FB
6211 default:
6212 goto illegal_op;
6213 }
6214 }
6215 break;
6216 /************************/
6217 /* string ops */
6218
6219 case 0xa4: /* movsS */
6220 case 0xa5:
ab4e4aec 6221 ot = mo_b_d(b, dflag);
2c0262af
FB
6222 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6223 gen_repz_movs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6224 } else {
6225 gen_movs(s, ot);
6226 }
6227 break;
3b46e624 6228
2c0262af
FB
6229 case 0xaa: /* stosS */
6230 case 0xab:
ab4e4aec 6231 ot = mo_b_d(b, dflag);
2c0262af
FB
6232 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6233 gen_repz_stos(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6234 } else {
6235 gen_stos(s, ot);
6236 }
6237 break;
6238 case 0xac: /* lodsS */
6239 case 0xad:
ab4e4aec 6240 ot = mo_b_d(b, dflag);
2c0262af
FB
6241 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6242 gen_repz_lods(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
6243 } else {
6244 gen_lods(s, ot);
6245 }
6246 break;
6247 case 0xae: /* scasS */
6248 case 0xaf:
ab4e4aec 6249 ot = mo_b_d(b, dflag);
2c0262af
FB
6250 if (prefixes & PREFIX_REPNZ) {
6251 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6252 } else if (prefixes & PREFIX_REPZ) {
6253 gen_repz_scas(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6254 } else {
6255 gen_scas(s, ot);
2c0262af
FB
6256 }
6257 break;
6258
6259 case 0xa6: /* cmpsS */
6260 case 0xa7:
ab4e4aec 6261 ot = mo_b_d(b, dflag);
2c0262af
FB
6262 if (prefixes & PREFIX_REPNZ) {
6263 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 1);
6264 } else if (prefixes & PREFIX_REPZ) {
6265 gen_repz_cmps(s, ot, pc_start - s->cs_base, s->pc - s->cs_base, 0);
6266 } else {
6267 gen_cmps(s, ot);
2c0262af
FB
6268 }
6269 break;
6270 case 0x6c: /* insS */
6271 case 0x6d:
ab4e4aec 6272 ot = mo_b_d32(b, dflag);
40b90233 6273 tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
b8b6a50b
FB
6274 gen_check_io(s, ot, pc_start - s->cs_base,
6275 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes) | 4);
f115e911
FB
6276 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6277 gen_repz_ins(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
2c0262af 6278 } else {
f115e911 6279 gen_ins(s, ot);
2e70f6ef
PB
6280 if (use_icount) {
6281 gen_jmp(s, s->pc - s->cs_base);
6282 }
2c0262af
FB
6283 }
6284 break;
6285 case 0x6e: /* outsS */
6286 case 0x6f:
ab4e4aec 6287 ot = mo_b_d32(b, dflag);
40b90233 6288 tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
b8b6a50b
FB
6289 gen_check_io(s, ot, pc_start - s->cs_base,
6290 svm_is_rep(prefixes) | 4);
f115e911
FB
6291 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ)) {
6292 gen_repz_outs(s, ot, pc_start - s->cs_base, s->pc - s->cs_base);
2c0262af 6293 } else {
f115e911 6294 gen_outs(s, ot);
2e70f6ef
PB
6295 if (use_icount) {
6296 gen_jmp(s, s->pc - s->cs_base);
6297 }
2c0262af
FB
6298 }
6299 break;
6300
6301 /************************/
6302 /* port I/O */
0573fbfc 6303
2c0262af
FB
6304 case 0xe4:
6305 case 0xe5:
ab4e4aec 6306 ot = mo_b_d32(b, dflag);
0af10c86 6307 val = cpu_ldub_code(env, s->pc++);
6d093a4f 6308 tcg_gen_movi_tl(cpu_T[0], val);
b8b6a50b
FB
6309 gen_check_io(s, ot, pc_start - s->cs_base,
6310 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
2e70f6ef
PB
6311 if (use_icount)
6312 gen_io_start();
1b90d56e 6313 tcg_gen_movi_i32(cpu_tmp2_i32, val);
a7812ae4 6314 gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
68773f84 6315 gen_op_mov_reg_v(ot, R_EAX, cpu_T[1]);
2e70f6ef
PB
6316 if (use_icount) {
6317 gen_io_end();
6318 gen_jmp(s, s->pc - s->cs_base);
6319 }
2c0262af
FB
6320 break;
6321 case 0xe6:
6322 case 0xe7:
ab4e4aec 6323 ot = mo_b_d32(b, dflag);
0af10c86 6324 val = cpu_ldub_code(env, s->pc++);
6d093a4f 6325 tcg_gen_movi_tl(cpu_T[0], val);
b8b6a50b
FB
6326 gen_check_io(s, ot, pc_start - s->cs_base,
6327 svm_is_rep(prefixes));
c56baccf 6328 gen_op_mov_v_reg(ot, cpu_T[1], R_EAX);
b8b6a50b 6329
2e70f6ef
PB
6330 if (use_icount)
6331 gen_io_start();
1b90d56e 6332 tcg_gen_movi_i32(cpu_tmp2_i32, val);
b6abf97d 6333 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
a7812ae4 6334 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
2e70f6ef
PB
6335 if (use_icount) {
6336 gen_io_end();
6337 gen_jmp(s, s->pc - s->cs_base);
6338 }
2c0262af
FB
6339 break;
6340 case 0xec:
6341 case 0xed:
ab4e4aec 6342 ot = mo_b_d32(b, dflag);
40b90233 6343 tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
b8b6a50b
FB
6344 gen_check_io(s, ot, pc_start - s->cs_base,
6345 SVM_IOIO_TYPE_MASK | svm_is_rep(prefixes));
2e70f6ef
PB
6346 if (use_icount)
6347 gen_io_start();
b6abf97d 6348 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
a7812ae4 6349 gen_helper_in_func(ot, cpu_T[1], cpu_tmp2_i32);
68773f84 6350 gen_op_mov_reg_v(ot, R_EAX, cpu_T[1]);
2e70f6ef
PB
6351 if (use_icount) {
6352 gen_io_end();
6353 gen_jmp(s, s->pc - s->cs_base);
6354 }
2c0262af
FB
6355 break;
6356 case 0xee:
6357 case 0xef:
ab4e4aec 6358 ot = mo_b_d32(b, dflag);
40b90233 6359 tcg_gen_ext16u_tl(cpu_T[0], cpu_regs[R_EDX]);
b8b6a50b
FB
6360 gen_check_io(s, ot, pc_start - s->cs_base,
6361 svm_is_rep(prefixes));
c56baccf 6362 gen_op_mov_v_reg(ot, cpu_T[1], R_EAX);
b8b6a50b 6363
2e70f6ef
PB
6364 if (use_icount)
6365 gen_io_start();
b6abf97d 6366 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
b6abf97d 6367 tcg_gen_trunc_tl_i32(cpu_tmp3_i32, cpu_T[1]);
a7812ae4 6368 gen_helper_out_func(ot, cpu_tmp2_i32, cpu_tmp3_i32);
2e70f6ef
PB
6369 if (use_icount) {
6370 gen_io_end();
6371 gen_jmp(s, s->pc - s->cs_base);
6372 }
2c0262af
FB
6373 break;
6374
6375 /************************/
6376 /* control */
6377 case 0xc2: /* ret im */
0af10c86 6378 val = cpu_ldsw_code(env, s->pc);
2c0262af 6379 s->pc += 2;
8e31d234
RH
6380 ot = gen_pop_T0(s);
6381 gen_stack_update(s, val + (1 << ot));
6382 /* Note that gen_pop_T0 uses a zero-extending load. */
74bdfbda 6383 gen_op_jmp_v(cpu_T[0]);
2c0262af
FB
6384 gen_eob(s);
6385 break;
6386 case 0xc3: /* ret */
8e31d234
RH
6387 ot = gen_pop_T0(s);
6388 gen_pop_update(s, ot);
6389 /* Note that gen_pop_T0 uses a zero-extending load. */
74bdfbda 6390 gen_op_jmp_v(cpu_T[0]);
2c0262af
FB
6391 gen_eob(s);
6392 break;
6393 case 0xca: /* lret im */
0af10c86 6394 val = cpu_ldsw_code(env, s->pc);
2c0262af
FB
6395 s->pc += 2;
6396 do_lret:
6397 if (s->pe && !s->vm86) {
773cdfcc 6398 gen_update_cc_op(s);
14ce26e7 6399 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 6400 gen_helper_lret_protected(cpu_env, tcg_const_i32(dflag - 1),
a7812ae4 6401 tcg_const_i32(val));
2c0262af
FB
6402 } else {
6403 gen_stack_A0(s);
6404 /* pop offset */
ab4e4aec 6405 gen_op_ld_v(s, dflag, cpu_T[0], cpu_A0);
2c0262af
FB
6406 /* NOTE: keeping EIP updated is not a problem in case of
6407 exception */
74bdfbda 6408 gen_op_jmp_v(cpu_T[0]);
2c0262af 6409 /* pop selector */
ab4e4aec
RH
6410 gen_op_addl_A0_im(1 << dflag);
6411 gen_op_ld_v(s, dflag, cpu_T[0], cpu_A0);
3bd7da9e 6412 gen_op_movl_seg_T0_vm(R_CS);
2c0262af 6413 /* add stack offset */
ab4e4aec 6414 gen_stack_update(s, val + (2 << dflag));
2c0262af
FB
6415 }
6416 gen_eob(s);
6417 break;
6418 case 0xcb: /* lret */
6419 val = 0;
6420 goto do_lret;
6421 case 0xcf: /* iret */
872929aa 6422 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IRET);
2c0262af
FB
6423 if (!s->pe) {
6424 /* real mode */
ab4e4aec 6425 gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
3ca51d07 6426 set_cc_op(s, CC_OP_EFLAGS);
f115e911
FB
6427 } else if (s->vm86) {
6428 if (s->iopl != 3) {
6429 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6430 } else {
ab4e4aec 6431 gen_helper_iret_real(cpu_env, tcg_const_i32(dflag - 1));
3ca51d07 6432 set_cc_op(s, CC_OP_EFLAGS);
f115e911 6433 }
2c0262af 6434 } else {
773cdfcc 6435 gen_update_cc_op(s);
14ce26e7 6436 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 6437 gen_helper_iret_protected(cpu_env, tcg_const_i32(dflag - 1),
a7812ae4 6438 tcg_const_i32(s->pc - s->cs_base));
3ca51d07 6439 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6440 }
6441 gen_eob(s);
6442 break;
6443 case 0xe8: /* call im */
6444 {
ab4e4aec 6445 if (dflag != MO_16) {
4ba9938c 6446 tval = (int32_t)insn_get(env, s, MO_32);
ab4e4aec 6447 } else {
4ba9938c 6448 tval = (int16_t)insn_get(env, s, MO_16);
ab4e4aec 6449 }
2c0262af 6450 next_eip = s->pc - s->cs_base;
14ce26e7 6451 tval += next_eip;
ab4e4aec 6452 if (dflag == MO_16) {
14ce26e7 6453 tval &= 0xffff;
ab4e4aec 6454 } else if (!CODE64(s)) {
99596385 6455 tval &= 0xffffffff;
ab4e4aec 6456 }
cc0bce88 6457 tcg_gen_movi_tl(cpu_T[0], next_eip);
432baffe 6458 gen_push_v(s, cpu_T[0]);
14ce26e7 6459 gen_jmp(s, tval);
2c0262af
FB
6460 }
6461 break;
6462 case 0x9a: /* lcall im */
6463 {
6464 unsigned int selector, offset;
3b46e624 6465
14ce26e7
FB
6466 if (CODE64(s))
6467 goto illegal_op;
ab4e4aec 6468 ot = dflag;
0af10c86 6469 offset = insn_get(env, s, ot);
4ba9938c 6470 selector = insn_get(env, s, MO_16);
3b46e624 6471
1b90d56e 6472 tcg_gen_movi_tl(cpu_T[0], selector);
0ae657b1 6473 tcg_gen_movi_tl(cpu_T[1], offset);
2c0262af
FB
6474 }
6475 goto do_lcall;
ecada8a2 6476 case 0xe9: /* jmp im */
ab4e4aec 6477 if (dflag != MO_16) {
4ba9938c 6478 tval = (int32_t)insn_get(env, s, MO_32);
ab4e4aec 6479 } else {
4ba9938c 6480 tval = (int16_t)insn_get(env, s, MO_16);
ab4e4aec 6481 }
14ce26e7 6482 tval += s->pc - s->cs_base;
ab4e4aec 6483 if (dflag == MO_16) {
14ce26e7 6484 tval &= 0xffff;
ab4e4aec 6485 } else if (!CODE64(s)) {
32938e12 6486 tval &= 0xffffffff;
ab4e4aec 6487 }
14ce26e7 6488 gen_jmp(s, tval);
2c0262af
FB
6489 break;
6490 case 0xea: /* ljmp im */
6491 {
6492 unsigned int selector, offset;
6493
14ce26e7
FB
6494 if (CODE64(s))
6495 goto illegal_op;
ab4e4aec 6496 ot = dflag;
0af10c86 6497 offset = insn_get(env, s, ot);
4ba9938c 6498 selector = insn_get(env, s, MO_16);
3b46e624 6499
1b90d56e 6500 tcg_gen_movi_tl(cpu_T[0], selector);
0ae657b1 6501 tcg_gen_movi_tl(cpu_T[1], offset);
2c0262af
FB
6502 }
6503 goto do_ljmp;
6504 case 0xeb: /* jmp Jb */
4ba9938c 6505 tval = (int8_t)insn_get(env, s, MO_8);
14ce26e7 6506 tval += s->pc - s->cs_base;
ab4e4aec 6507 if (dflag == MO_16) {
14ce26e7 6508 tval &= 0xffff;
ab4e4aec 6509 }
14ce26e7 6510 gen_jmp(s, tval);
2c0262af
FB
6511 break;
6512 case 0x70 ... 0x7f: /* jcc Jb */
4ba9938c 6513 tval = (int8_t)insn_get(env, s, MO_8);
2c0262af
FB
6514 goto do_jcc;
6515 case 0x180 ... 0x18f: /* jcc Jv */
ab4e4aec 6516 if (dflag != MO_16) {
4ba9938c 6517 tval = (int32_t)insn_get(env, s, MO_32);
2c0262af 6518 } else {
4ba9938c 6519 tval = (int16_t)insn_get(env, s, MO_16);
2c0262af
FB
6520 }
6521 do_jcc:
6522 next_eip = s->pc - s->cs_base;
14ce26e7 6523 tval += next_eip;
ab4e4aec 6524 if (dflag == MO_16) {
14ce26e7 6525 tval &= 0xffff;
ab4e4aec 6526 }
14ce26e7 6527 gen_jcc(s, b, tval, next_eip);
2c0262af
FB
6528 break;
6529
6530 case 0x190 ... 0x19f: /* setcc Gv */
0af10c86 6531 modrm = cpu_ldub_code(env, s->pc++);
cc8b6f5b 6532 gen_setcc1(s, b, cpu_T[0]);
4ba9938c 6533 gen_ldst_modrm(env, s, modrm, MO_8, OR_TMP0, 1);
2c0262af
FB
6534 break;
6535 case 0x140 ... 0x14f: /* cmov Gv, Ev */
bff93281
PM
6536 if (!(s->cpuid_features & CPUID_CMOV)) {
6537 goto illegal_op;
6538 }
ab4e4aec 6539 ot = dflag;
f32d3781
PB
6540 modrm = cpu_ldub_code(env, s->pc++);
6541 reg = ((modrm >> 3) & 7) | rex_r;
6542 gen_cmovcc1(env, s, ot, b, modrm, reg);
2c0262af 6543 break;
3b46e624 6544
2c0262af
FB
6545 /************************/
6546 /* flags */
6547 case 0x9c: /* pushf */
872929aa 6548 gen_svm_check_intercept(s, pc_start, SVM_EXIT_PUSHF);
2c0262af
FB
6549 if (s->vm86 && s->iopl != 3) {
6550 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6551 } else {
773cdfcc 6552 gen_update_cc_op(s);
f0967a1a 6553 gen_helper_read_eflags(cpu_T[0], cpu_env);
432baffe 6554 gen_push_v(s, cpu_T[0]);
2c0262af
FB
6555 }
6556 break;
6557 case 0x9d: /* popf */
872929aa 6558 gen_svm_check_intercept(s, pc_start, SVM_EXIT_POPF);
2c0262af
FB
6559 if (s->vm86 && s->iopl != 3) {
6560 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6561 } else {
8e31d234 6562 ot = gen_pop_T0(s);
2c0262af 6563 if (s->cpl == 0) {
ab4e4aec 6564 if (dflag != MO_16) {
f0967a1a
BS
6565 gen_helper_write_eflags(cpu_env, cpu_T[0],
6566 tcg_const_i32((TF_MASK | AC_MASK |
6567 ID_MASK | NT_MASK |
6568 IF_MASK |
6569 IOPL_MASK)));
2c0262af 6570 } else {
f0967a1a
BS
6571 gen_helper_write_eflags(cpu_env, cpu_T[0],
6572 tcg_const_i32((TF_MASK | AC_MASK |
6573 ID_MASK | NT_MASK |
6574 IF_MASK | IOPL_MASK)
6575 & 0xffff));
2c0262af
FB
6576 }
6577 } else {
4136f33c 6578 if (s->cpl <= s->iopl) {
ab4e4aec 6579 if (dflag != MO_16) {
f0967a1a
BS
6580 gen_helper_write_eflags(cpu_env, cpu_T[0],
6581 tcg_const_i32((TF_MASK |
6582 AC_MASK |
6583 ID_MASK |
6584 NT_MASK |
6585 IF_MASK)));
4136f33c 6586 } else {
f0967a1a
BS
6587 gen_helper_write_eflags(cpu_env, cpu_T[0],
6588 tcg_const_i32((TF_MASK |
6589 AC_MASK |
6590 ID_MASK |
6591 NT_MASK |
6592 IF_MASK)
6593 & 0xffff));
4136f33c 6594 }
2c0262af 6595 } else {
ab4e4aec 6596 if (dflag != MO_16) {
f0967a1a
BS
6597 gen_helper_write_eflags(cpu_env, cpu_T[0],
6598 tcg_const_i32((TF_MASK | AC_MASK |
6599 ID_MASK | NT_MASK)));
4136f33c 6600 } else {
f0967a1a
BS
6601 gen_helper_write_eflags(cpu_env, cpu_T[0],
6602 tcg_const_i32((TF_MASK | AC_MASK |
6603 ID_MASK | NT_MASK)
6604 & 0xffff));
4136f33c 6605 }
2c0262af
FB
6606 }
6607 }
8e31d234 6608 gen_pop_update(s, ot);
3ca51d07 6609 set_cc_op(s, CC_OP_EFLAGS);
a9321a4d 6610 /* abort translation because TF/AC flag may change */
14ce26e7 6611 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
6612 gen_eob(s);
6613 }
6614 break;
6615 case 0x9e: /* sahf */
12e26b75 6616 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
14ce26e7 6617 goto illegal_op;
c56baccf 6618 gen_op_mov_v_reg(MO_8, cpu_T[0], R_AH);
d229edce 6619 gen_compute_eflags(s);
bd7a7b33
FB
6620 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, CC_O);
6621 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], CC_S | CC_Z | CC_A | CC_P | CC_C);
6622 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, cpu_T[0]);
2c0262af
FB
6623 break;
6624 case 0x9f: /* lahf */
12e26b75 6625 if (CODE64(s) && !(s->cpuid_ext3_features & CPUID_EXT3_LAHF_LM))
14ce26e7 6626 goto illegal_op;
d229edce 6627 gen_compute_eflags(s);
bd7a7b33 6628 /* Note: gen_compute_eflags() only gives the condition codes */
d229edce 6629 tcg_gen_ori_tl(cpu_T[0], cpu_cc_src, 0x02);
480a762d 6630 gen_op_mov_reg_v(MO_8, R_AH, cpu_T[0]);
2c0262af
FB
6631 break;
6632 case 0xf5: /* cmc */
d229edce 6633 gen_compute_eflags(s);
bd7a7b33 6634 tcg_gen_xori_tl(cpu_cc_src, cpu_cc_src, CC_C);
2c0262af
FB
6635 break;
6636 case 0xf8: /* clc */
d229edce 6637 gen_compute_eflags(s);
bd7a7b33 6638 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_C);
2c0262af
FB
6639 break;
6640 case 0xf9: /* stc */
d229edce 6641 gen_compute_eflags(s);
bd7a7b33 6642 tcg_gen_ori_tl(cpu_cc_src, cpu_cc_src, CC_C);
2c0262af
FB
6643 break;
6644 case 0xfc: /* cld */
b6abf97d 6645 tcg_gen_movi_i32(cpu_tmp2_i32, 1);
317ac620 6646 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
2c0262af
FB
6647 break;
6648 case 0xfd: /* std */
b6abf97d 6649 tcg_gen_movi_i32(cpu_tmp2_i32, -1);
317ac620 6650 tcg_gen_st_i32(cpu_tmp2_i32, cpu_env, offsetof(CPUX86State, df));
2c0262af
FB
6651 break;
6652
6653 /************************/
6654 /* bit operations */
6655 case 0x1ba: /* bt/bts/btr/btc Gv, im */
ab4e4aec 6656 ot = dflag;
0af10c86 6657 modrm = cpu_ldub_code(env, s->pc++);
33698e5f 6658 op = (modrm >> 3) & 7;
2c0262af 6659 mod = (modrm >> 6) & 3;
14ce26e7 6660 rm = (modrm & 7) | REX_B(s);
2c0262af 6661 if (mod != 3) {
14ce26e7 6662 s->rip_offset = 1;
4eeb3939 6663 gen_lea_modrm(env, s, modrm);
909be183 6664 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 6665 } else {
c56baccf 6666 gen_op_mov_v_reg(ot, cpu_T[0], rm);
2c0262af
FB
6667 }
6668 /* load shift */
0af10c86 6669 val = cpu_ldub_code(env, s->pc++);
0ae657b1 6670 tcg_gen_movi_tl(cpu_T[1], val);
2c0262af
FB
6671 if (op < 4)
6672 goto illegal_op;
6673 op -= 4;
f484d386 6674 goto bt_op;
2c0262af
FB
6675 case 0x1a3: /* bt Gv, Ev */
6676 op = 0;
6677 goto do_btx;
6678 case 0x1ab: /* bts */
6679 op = 1;
6680 goto do_btx;
6681 case 0x1b3: /* btr */
6682 op = 2;
6683 goto do_btx;
6684 case 0x1bb: /* btc */
6685 op = 3;
6686 do_btx:
ab4e4aec 6687 ot = dflag;
0af10c86 6688 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7 6689 reg = ((modrm >> 3) & 7) | rex_r;
2c0262af 6690 mod = (modrm >> 6) & 3;
14ce26e7 6691 rm = (modrm & 7) | REX_B(s);
c56baccf 6692 gen_op_mov_v_reg(MO_32, cpu_T[1], reg);
2c0262af 6693 if (mod != 3) {
4eeb3939 6694 gen_lea_modrm(env, s, modrm);
2c0262af 6695 /* specific case: we need to add a displacement */
f484d386
FB
6696 gen_exts(ot, cpu_T[1]);
6697 tcg_gen_sari_tl(cpu_tmp0, cpu_T[1], 3 + ot);
6698 tcg_gen_shli_tl(cpu_tmp0, cpu_tmp0, ot);
6699 tcg_gen_add_tl(cpu_A0, cpu_A0, cpu_tmp0);
909be183 6700 gen_op_ld_v(s, ot, cpu_T[0], cpu_A0);
2c0262af 6701 } else {
c56baccf 6702 gen_op_mov_v_reg(ot, cpu_T[0], rm);
2c0262af 6703 }
f484d386
FB
6704 bt_op:
6705 tcg_gen_andi_tl(cpu_T[1], cpu_T[1], (1 << (3 + ot)) - 1);
dc1823ce 6706 tcg_gen_shr_tl(cpu_tmp4, cpu_T[0], cpu_T[1]);
f484d386
FB
6707 switch(op) {
6708 case 0:
f484d386
FB
6709 break;
6710 case 1:
f484d386
FB
6711 tcg_gen_movi_tl(cpu_tmp0, 1);
6712 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6713 tcg_gen_or_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
6714 break;
6715 case 2:
f484d386
FB
6716 tcg_gen_movi_tl(cpu_tmp0, 1);
6717 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
dc1823ce 6718 tcg_gen_andc_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
f484d386
FB
6719 break;
6720 default:
6721 case 3:
f484d386
FB
6722 tcg_gen_movi_tl(cpu_tmp0, 1);
6723 tcg_gen_shl_tl(cpu_tmp0, cpu_tmp0, cpu_T[1]);
6724 tcg_gen_xor_tl(cpu_T[0], cpu_T[0], cpu_tmp0);
6725 break;
6726 }
2c0262af 6727 if (op != 0) {
fd8ca9f6
RH
6728 if (mod != 3) {
6729 gen_op_st_v(s, ot, cpu_T[0], cpu_A0);
6730 } else {
480a762d 6731 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
fd8ca9f6 6732 }
dc1823ce
RH
6733 }
6734
6735 /* Delay all CC updates until after the store above. Note that
6736 C is the result of the test, Z is unchanged, and the others
6737 are all undefined. */
6738 switch (s->cc_op) {
6739 case CC_OP_MULB ... CC_OP_MULQ:
6740 case CC_OP_ADDB ... CC_OP_ADDQ:
6741 case CC_OP_ADCB ... CC_OP_ADCQ:
6742 case CC_OP_SUBB ... CC_OP_SUBQ:
6743 case CC_OP_SBBB ... CC_OP_SBBQ:
6744 case CC_OP_LOGICB ... CC_OP_LOGICQ:
6745 case CC_OP_INCB ... CC_OP_INCQ:
6746 case CC_OP_DECB ... CC_OP_DECQ:
6747 case CC_OP_SHLB ... CC_OP_SHLQ:
6748 case CC_OP_SARB ... CC_OP_SARQ:
6749 case CC_OP_BMILGB ... CC_OP_BMILGQ:
6750 /* Z was going to be computed from the non-zero status of CC_DST.
6751 We can get that same Z value (and the new C value) by leaving
6752 CC_DST alone, setting CC_SRC, and using a CC_OP_SAR of the
6753 same width. */
f484d386 6754 tcg_gen_mov_tl(cpu_cc_src, cpu_tmp4);
dc1823ce
RH
6755 set_cc_op(s, ((s->cc_op - CC_OP_MULB) & 3) + CC_OP_SARB);
6756 break;
6757 default:
6758 /* Otherwise, generate EFLAGS and replace the C bit. */
6759 gen_compute_eflags(s);
6760 tcg_gen_deposit_tl(cpu_cc_src, cpu_cc_src, cpu_tmp4,
6761 ctz32(CC_C), 1);
6762 break;
2c0262af
FB
6763 }
6764 break;
321c5351
RH
6765 case 0x1bc: /* bsf / tzcnt */
6766 case 0x1bd: /* bsr / lzcnt */
ab4e4aec 6767 ot = dflag;
321c5351
RH
6768 modrm = cpu_ldub_code(env, s->pc++);
6769 reg = ((modrm >> 3) & 7) | rex_r;
6770 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
6771 gen_extu(ot, cpu_T[0]);
6772
6773 /* Note that lzcnt and tzcnt are in different extensions. */
6774 if ((prefixes & PREFIX_REPZ)
6775 && (b & 1
6776 ? s->cpuid_ext3_features & CPUID_EXT3_ABM
6777 : s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_BMI1)) {
6778 int size = 8 << ot;
6779 tcg_gen_mov_tl(cpu_cc_src, cpu_T[0]);
6780 if (b & 1) {
6781 /* For lzcnt, reduce the target_ulong result by the
6782 number of zeros that we expect to find at the top. */
6783 gen_helper_clz(cpu_T[0], cpu_T[0]);
6784 tcg_gen_subi_tl(cpu_T[0], cpu_T[0], TARGET_LONG_BITS - size);
6191b059 6785 } else {
321c5351
RH
6786 /* For tzcnt, a zero input must return the operand size:
6787 force all bits outside the operand size to 1. */
6788 target_ulong mask = (target_ulong)-2 << (size - 1);
6789 tcg_gen_ori_tl(cpu_T[0], cpu_T[0], mask);
6790 gen_helper_ctz(cpu_T[0], cpu_T[0]);
6191b059 6791 }
321c5351
RH
6792 /* For lzcnt/tzcnt, C and Z bits are defined and are
6793 related to the result. */
6794 gen_op_update1_cc();
6795 set_cc_op(s, CC_OP_BMILGB + ot);
6796 } else {
6797 /* For bsr/bsf, only the Z bit is defined and it is related
6798 to the input and not the result. */
6799 tcg_gen_mov_tl(cpu_cc_dst, cpu_T[0]);
6800 set_cc_op(s, CC_OP_LOGICB + ot);
6801 if (b & 1) {
6802 /* For bsr, return the bit index of the first 1 bit,
6803 not the count of leading zeros. */
6804 gen_helper_clz(cpu_T[0], cpu_T[0]);
6805 tcg_gen_xori_tl(cpu_T[0], cpu_T[0], TARGET_LONG_BITS - 1);
6806 } else {
6807 gen_helper_ctz(cpu_T[0], cpu_T[0]);
6808 }
6809 /* ??? The manual says that the output is undefined when the
6810 input is zero, but real hardware leaves it unchanged, and
6811 real programs appear to depend on that. */
6812 tcg_gen_movi_tl(cpu_tmp0, 0);
6813 tcg_gen_movcond_tl(TCG_COND_EQ, cpu_T[0], cpu_cc_dst, cpu_tmp0,
6814 cpu_regs[reg], cpu_T[0]);
6191b059 6815 }
480a762d 6816 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
2c0262af
FB
6817 break;
6818 /************************/
6819 /* bcd */
6820 case 0x27: /* daa */
14ce26e7
FB
6821 if (CODE64(s))
6822 goto illegal_op;
773cdfcc 6823 gen_update_cc_op(s);
7923057b 6824 gen_helper_daa(cpu_env);
3ca51d07 6825 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6826 break;
6827 case 0x2f: /* das */
14ce26e7
FB
6828 if (CODE64(s))
6829 goto illegal_op;
773cdfcc 6830 gen_update_cc_op(s);
7923057b 6831 gen_helper_das(cpu_env);
3ca51d07 6832 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6833 break;
6834 case 0x37: /* aaa */
14ce26e7
FB
6835 if (CODE64(s))
6836 goto illegal_op;
773cdfcc 6837 gen_update_cc_op(s);
7923057b 6838 gen_helper_aaa(cpu_env);
3ca51d07 6839 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6840 break;
6841 case 0x3f: /* aas */
14ce26e7
FB
6842 if (CODE64(s))
6843 goto illegal_op;
773cdfcc 6844 gen_update_cc_op(s);
7923057b 6845 gen_helper_aas(cpu_env);
3ca51d07 6846 set_cc_op(s, CC_OP_EFLAGS);
2c0262af
FB
6847 break;
6848 case 0xd4: /* aam */
14ce26e7
FB
6849 if (CODE64(s))
6850 goto illegal_op;
0af10c86 6851 val = cpu_ldub_code(env, s->pc++);
b6d7c3db
TS
6852 if (val == 0) {
6853 gen_exception(s, EXCP00_DIVZ, pc_start - s->cs_base);
6854 } else {
7923057b 6855 gen_helper_aam(cpu_env, tcg_const_i32(val));
3ca51d07 6856 set_cc_op(s, CC_OP_LOGICB);
b6d7c3db 6857 }
2c0262af
FB
6858 break;
6859 case 0xd5: /* aad */
14ce26e7
FB
6860 if (CODE64(s))
6861 goto illegal_op;
0af10c86 6862 val = cpu_ldub_code(env, s->pc++);
7923057b 6863 gen_helper_aad(cpu_env, tcg_const_i32(val));
3ca51d07 6864 set_cc_op(s, CC_OP_LOGICB);
2c0262af
FB
6865 break;
6866 /************************/
6867 /* misc */
6868 case 0x90: /* nop */
ab1f142b 6869 /* XXX: correct lock test for all insn */
7418027e 6870 if (prefixes & PREFIX_LOCK) {
ab1f142b 6871 goto illegal_op;
7418027e
RH
6872 }
6873 /* If REX_B is set, then this is xchg eax, r8d, not a nop. */
6874 if (REX_B(s)) {
6875 goto do_xchg_reg_eax;
6876 }
0573fbfc 6877 if (prefixes & PREFIX_REPZ) {
81f3053b
PB
6878 gen_update_cc_op(s);
6879 gen_jmp_im(pc_start - s->cs_base);
6880 gen_helper_pause(cpu_env, tcg_const_i32(s->pc - pc_start));
6881 s->is_jmp = DISAS_TB_JUMP;
0573fbfc 6882 }
2c0262af
FB
6883 break;
6884 case 0x9b: /* fwait */
5fafdf24 6885 if ((s->flags & (HF_MP_MASK | HF_TS_MASK)) ==
7eee2a50
FB
6886 (HF_MP_MASK | HF_TS_MASK)) {
6887 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
2ee73ac3 6888 } else {
773cdfcc 6889 gen_update_cc_op(s);
14ce26e7 6890 gen_jmp_im(pc_start - s->cs_base);
d3eb5eae 6891 gen_helper_fwait(cpu_env);
7eee2a50 6892 }
2c0262af
FB
6893 break;
6894 case 0xcc: /* int3 */
6895 gen_interrupt(s, EXCP03_INT3, pc_start - s->cs_base, s->pc - s->cs_base);
6896 break;
6897 case 0xcd: /* int N */
0af10c86 6898 val = cpu_ldub_code(env, s->pc++);
f115e911 6899 if (s->vm86 && s->iopl != 3) {
5fafdf24 6900 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
f115e911
FB
6901 } else {
6902 gen_interrupt(s, val, pc_start - s->cs_base, s->pc - s->cs_base);
6903 }
2c0262af
FB
6904 break;
6905 case 0xce: /* into */
14ce26e7
FB
6906 if (CODE64(s))
6907 goto illegal_op;
773cdfcc 6908 gen_update_cc_op(s);
a8ede8ba 6909 gen_jmp_im(pc_start - s->cs_base);
4a7443be 6910 gen_helper_into(cpu_env, tcg_const_i32(s->pc - pc_start));
2c0262af 6911 break;
0b97134b 6912#ifdef WANT_ICEBP
2c0262af 6913 case 0xf1: /* icebp (undocumented, exits to external debugger) */
872929aa 6914 gen_svm_check_intercept(s, pc_start, SVM_EXIT_ICEBP);
aba9d61e 6915#if 1
2c0262af 6916 gen_debug(s, pc_start - s->cs_base);
aba9d61e
FB
6917#else
6918 /* start debug */
0af10c86 6919 tb_flush(env);
24537a01 6920 qemu_set_log(CPU_LOG_INT | CPU_LOG_TB_IN_ASM);
aba9d61e 6921#endif
2c0262af 6922 break;
0b97134b 6923#endif
2c0262af
FB
6924 case 0xfa: /* cli */
6925 if (!s->vm86) {
6926 if (s->cpl <= s->iopl) {
f0967a1a 6927 gen_helper_cli(cpu_env);
2c0262af
FB
6928 } else {
6929 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6930 }
6931 } else {
6932 if (s->iopl == 3) {
f0967a1a 6933 gen_helper_cli(cpu_env);
2c0262af
FB
6934 } else {
6935 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6936 }
6937 }
6938 break;
6939 case 0xfb: /* sti */
6940 if (!s->vm86) {
6941 if (s->cpl <= s->iopl) {
6942 gen_sti:
f0967a1a 6943 gen_helper_sti(cpu_env);
2c0262af 6944 /* interruptions are enabled only the first insn after sti */
a2cc3b24
FB
6945 /* If several instructions disable interrupts, only the
6946 _first_ does it */
6947 if (!(s->tb->flags & HF_INHIBIT_IRQ_MASK))
f0967a1a 6948 gen_helper_set_inhibit_irq(cpu_env);
2c0262af 6949 /* give a chance to handle pending irqs */
14ce26e7 6950 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
6951 gen_eob(s);
6952 } else {
6953 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6954 }
6955 } else {
6956 if (s->iopl == 3) {
6957 goto gen_sti;
6958 } else {
6959 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
6960 }
6961 }
6962 break;
6963 case 0x62: /* bound */
14ce26e7
FB
6964 if (CODE64(s))
6965 goto illegal_op;
ab4e4aec 6966 ot = dflag;
0af10c86 6967 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
6968 reg = (modrm >> 3) & 7;
6969 mod = (modrm >> 6) & 3;
6970 if (mod == 3)
6971 goto illegal_op;
c56baccf 6972 gen_op_mov_v_reg(ot, cpu_T[0], reg);
4eeb3939 6973 gen_lea_modrm(env, s, modrm);
14ce26e7 6974 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 6975 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
4ba9938c 6976 if (ot == MO_16) {
92fc4b58
BS
6977 gen_helper_boundw(cpu_env, cpu_A0, cpu_tmp2_i32);
6978 } else {
6979 gen_helper_boundl(cpu_env, cpu_A0, cpu_tmp2_i32);
6980 }
2c0262af
FB
6981 break;
6982 case 0x1c8 ... 0x1cf: /* bswap reg */
14ce26e7
FB
6983 reg = (b & 7) | REX_B(s);
6984#ifdef TARGET_X86_64
ab4e4aec 6985 if (dflag == MO_64) {
c56baccf 6986 gen_op_mov_v_reg(MO_64, cpu_T[0], reg);
66896cb8 6987 tcg_gen_bswap64_i64(cpu_T[0], cpu_T[0]);
480a762d 6988 gen_op_mov_reg_v(MO_64, reg, cpu_T[0]);
5fafdf24 6989 } else
8777643e 6990#endif
57fec1fe 6991 {
c56baccf 6992 gen_op_mov_v_reg(MO_32, cpu_T[0], reg);
8777643e
AJ
6993 tcg_gen_ext32u_tl(cpu_T[0], cpu_T[0]);
6994 tcg_gen_bswap32_tl(cpu_T[0], cpu_T[0]);
480a762d 6995 gen_op_mov_reg_v(MO_32, reg, cpu_T[0]);
14ce26e7 6996 }
2c0262af
FB
6997 break;
6998 case 0xd6: /* salc */
14ce26e7
FB
6999 if (CODE64(s))
7000 goto illegal_op;
cc8b6f5b 7001 gen_compute_eflags_c(s, cpu_T[0]);
bd7a7b33 7002 tcg_gen_neg_tl(cpu_T[0], cpu_T[0]);
480a762d 7003 gen_op_mov_reg_v(MO_8, R_EAX, cpu_T[0]);
2c0262af
FB
7004 break;
7005 case 0xe0: /* loopnz */
7006 case 0xe1: /* loopz */
2c0262af
FB
7007 case 0xe2: /* loop */
7008 case 0xe3: /* jecxz */
14ce26e7 7009 {
6e0d8677 7010 int l1, l2, l3;
14ce26e7 7011
4ba9938c 7012 tval = (int8_t)insn_get(env, s, MO_8);
14ce26e7
FB
7013 next_eip = s->pc - s->cs_base;
7014 tval += next_eip;
ab4e4aec 7015 if (dflag == MO_16) {
14ce26e7 7016 tval &= 0xffff;
ab4e4aec 7017 }
3b46e624 7018
14ce26e7
FB
7019 l1 = gen_new_label();
7020 l2 = gen_new_label();
6e0d8677 7021 l3 = gen_new_label();
14ce26e7 7022 b &= 3;
6e0d8677
FB
7023 switch(b) {
7024 case 0: /* loopnz */
7025 case 1: /* loopz */
1d71ddb1
RH
7026 gen_op_add_reg_im(s->aflag, R_ECX, -1);
7027 gen_op_jz_ecx(s->aflag, l3);
5bdb91b0 7028 gen_jcc1(s, (JCC_Z << 1) | (b ^ 1), l1);
6e0d8677
FB
7029 break;
7030 case 2: /* loop */
1d71ddb1
RH
7031 gen_op_add_reg_im(s->aflag, R_ECX, -1);
7032 gen_op_jnz_ecx(s->aflag, l1);
6e0d8677
FB
7033 break;
7034 default:
7035 case 3: /* jcxz */
1d71ddb1 7036 gen_op_jz_ecx(s->aflag, l1);
6e0d8677 7037 break;
14ce26e7
FB
7038 }
7039
6e0d8677 7040 gen_set_label(l3);
14ce26e7 7041 gen_jmp_im(next_eip);
8e1c85e3 7042 tcg_gen_br(l2);
6e0d8677 7043
14ce26e7
FB
7044 gen_set_label(l1);
7045 gen_jmp_im(tval);
7046 gen_set_label(l2);
7047 gen_eob(s);
7048 }
2c0262af
FB
7049 break;
7050 case 0x130: /* wrmsr */
7051 case 0x132: /* rdmsr */
7052 if (s->cpl != 0) {
7053 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7054 } else {
773cdfcc 7055 gen_update_cc_op(s);
872929aa 7056 gen_jmp_im(pc_start - s->cs_base);
0573fbfc 7057 if (b & 2) {
4a7443be 7058 gen_helper_rdmsr(cpu_env);
0573fbfc 7059 } else {
4a7443be 7060 gen_helper_wrmsr(cpu_env);
0573fbfc 7061 }
2c0262af
FB
7062 }
7063 break;
7064 case 0x131: /* rdtsc */
773cdfcc 7065 gen_update_cc_op(s);
ecada8a2 7066 gen_jmp_im(pc_start - s->cs_base);
efade670
PB
7067 if (use_icount)
7068 gen_io_start();
4a7443be 7069 gen_helper_rdtsc(cpu_env);
efade670
PB
7070 if (use_icount) {
7071 gen_io_end();
7072 gen_jmp(s, s->pc - s->cs_base);
7073 }
2c0262af 7074 break;
df01e0fc 7075 case 0x133: /* rdpmc */
773cdfcc 7076 gen_update_cc_op(s);
df01e0fc 7077 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7078 gen_helper_rdpmc(cpu_env);
df01e0fc 7079 break;
023fe10d 7080 case 0x134: /* sysenter */
2436b61a 7081 /* For Intel SYSENTER is valid on 64-bit */
0af10c86 7082 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
14ce26e7 7083 goto illegal_op;
023fe10d
FB
7084 if (!s->pe) {
7085 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7086 } else {
728d803b 7087 gen_update_cc_op(s);
14ce26e7 7088 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 7089 gen_helper_sysenter(cpu_env);
023fe10d
FB
7090 gen_eob(s);
7091 }
7092 break;
7093 case 0x135: /* sysexit */
2436b61a 7094 /* For Intel SYSEXIT is valid on 64-bit */
0af10c86 7095 if (CODE64(s) && env->cpuid_vendor1 != CPUID_VENDOR_INTEL_1)
14ce26e7 7096 goto illegal_op;
023fe10d
FB
7097 if (!s->pe) {
7098 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7099 } else {
728d803b 7100 gen_update_cc_op(s);
14ce26e7 7101 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 7102 gen_helper_sysexit(cpu_env, tcg_const_i32(dflag - 1));
023fe10d
FB
7103 gen_eob(s);
7104 }
7105 break;
14ce26e7
FB
7106#ifdef TARGET_X86_64
7107 case 0x105: /* syscall */
7108 /* XXX: is it usable in real mode ? */
728d803b 7109 gen_update_cc_op(s);
14ce26e7 7110 gen_jmp_im(pc_start - s->cs_base);
2999a0b2 7111 gen_helper_syscall(cpu_env, tcg_const_i32(s->pc - pc_start));
14ce26e7
FB
7112 gen_eob(s);
7113 break;
7114 case 0x107: /* sysret */
7115 if (!s->pe) {
7116 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7117 } else {
728d803b 7118 gen_update_cc_op(s);
14ce26e7 7119 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 7120 gen_helper_sysret(cpu_env, tcg_const_i32(dflag - 1));
aba9d61e 7121 /* condition codes are modified only in long mode */
3ca51d07
RH
7122 if (s->lma) {
7123 set_cc_op(s, CC_OP_EFLAGS);
7124 }
14ce26e7
FB
7125 gen_eob(s);
7126 }
7127 break;
7128#endif
2c0262af 7129 case 0x1a2: /* cpuid */
773cdfcc 7130 gen_update_cc_op(s);
9575cb94 7131 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7132 gen_helper_cpuid(cpu_env);
2c0262af
FB
7133 break;
7134 case 0xf4: /* hlt */
7135 if (s->cpl != 0) {
7136 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7137 } else {
773cdfcc 7138 gen_update_cc_op(s);
94451178 7139 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7140 gen_helper_hlt(cpu_env, tcg_const_i32(s->pc - pc_start));
5779406a 7141 s->is_jmp = DISAS_TB_JUMP;
2c0262af
FB
7142 }
7143 break;
7144 case 0x100:
0af10c86 7145 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
7146 mod = (modrm >> 6) & 3;
7147 op = (modrm >> 3) & 7;
7148 switch(op) {
7149 case 0: /* sldt */
f115e911
FB
7150 if (!s->pe || s->vm86)
7151 goto illegal_op;
872929aa 7152 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_READ);
651ba608 7153 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,ldt.selector));
ab4e4aec 7154 ot = mod == 3 ? dflag : MO_16;
0af10c86 7155 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
7156 break;
7157 case 2: /* lldt */
f115e911
FB
7158 if (!s->pe || s->vm86)
7159 goto illegal_op;
2c0262af
FB
7160 if (s->cpl != 0) {
7161 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7162 } else {
872929aa 7163 gen_svm_check_intercept(s, pc_start, SVM_EXIT_LDTR_WRITE);
4ba9938c 7164 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
14ce26e7 7165 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 7166 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 7167 gen_helper_lldt(cpu_env, cpu_tmp2_i32);
2c0262af
FB
7168 }
7169 break;
7170 case 1: /* str */
f115e911
FB
7171 if (!s->pe || s->vm86)
7172 goto illegal_op;
872929aa 7173 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_READ);
651ba608 7174 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,tr.selector));
ab4e4aec 7175 ot = mod == 3 ? dflag : MO_16;
0af10c86 7176 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 1);
2c0262af
FB
7177 break;
7178 case 3: /* ltr */
f115e911
FB
7179 if (!s->pe || s->vm86)
7180 goto illegal_op;
2c0262af
FB
7181 if (s->cpl != 0) {
7182 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7183 } else {
872929aa 7184 gen_svm_check_intercept(s, pc_start, SVM_EXIT_TR_WRITE);
4ba9938c 7185 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
14ce26e7 7186 gen_jmp_im(pc_start - s->cs_base);
b6abf97d 7187 tcg_gen_trunc_tl_i32(cpu_tmp2_i32, cpu_T[0]);
2999a0b2 7188 gen_helper_ltr(cpu_env, cpu_tmp2_i32);
2c0262af
FB
7189 }
7190 break;
7191 case 4: /* verr */
7192 case 5: /* verw */
f115e911
FB
7193 if (!s->pe || s->vm86)
7194 goto illegal_op;
4ba9938c 7195 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
773cdfcc 7196 gen_update_cc_op(s);
2999a0b2
BS
7197 if (op == 4) {
7198 gen_helper_verr(cpu_env, cpu_T[0]);
7199 } else {
7200 gen_helper_verw(cpu_env, cpu_T[0]);
7201 }
3ca51d07 7202 set_cc_op(s, CC_OP_EFLAGS);
f115e911 7203 break;
2c0262af
FB
7204 default:
7205 goto illegal_op;
7206 }
7207 break;
7208 case 0x101:
0af10c86 7209 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
7210 mod = (modrm >> 6) & 3;
7211 op = (modrm >> 3) & 7;
3d7374c5 7212 rm = modrm & 7;
2c0262af
FB
7213 switch(op) {
7214 case 0: /* sgdt */
2c0262af
FB
7215 if (mod == 3)
7216 goto illegal_op;
872929aa 7217 gen_svm_check_intercept(s, pc_start, SVM_EXIT_GDTR_READ);
4eeb3939 7218 gen_lea_modrm(env, s, modrm);
651ba608 7219 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.limit));
fd8ca9f6 7220 gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
aba9d61e 7221 gen_add_A0_im(s, 2);
651ba608 7222 tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, gdt.base));
ab4e4aec 7223 if (dflag == MO_16) {
f0706f0c
RH
7224 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
7225 }
fd8ca9f6 7226 gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
2c0262af 7227 break;
3d7374c5
FB
7228 case 1:
7229 if (mod == 3) {
7230 switch (rm) {
7231 case 0: /* monitor */
7232 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
7233 s->cpl != 0)
7234 goto illegal_op;
773cdfcc 7235 gen_update_cc_op(s);
3d7374c5 7236 gen_jmp_im(pc_start - s->cs_base);
1d71ddb1
RH
7237 tcg_gen_mov_tl(cpu_A0, cpu_regs[R_EAX]);
7238 gen_extu(s->aflag, cpu_A0);
3d7374c5 7239 gen_add_A0_ds_seg(s);
4a7443be 7240 gen_helper_monitor(cpu_env, cpu_A0);
3d7374c5
FB
7241 break;
7242 case 1: /* mwait */
7243 if (!(s->cpuid_ext_features & CPUID_EXT_MONITOR) ||
7244 s->cpl != 0)
7245 goto illegal_op;
728d803b 7246 gen_update_cc_op(s);
94451178 7247 gen_jmp_im(pc_start - s->cs_base);
4a7443be 7248 gen_helper_mwait(cpu_env, tcg_const_i32(s->pc - pc_start));
3d7374c5
FB
7249 gen_eob(s);
7250 break;
a9321a4d
PA
7251 case 2: /* clac */
7252 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP) ||
7253 s->cpl != 0) {
7254 goto illegal_op;
7255 }
7256 gen_helper_clac(cpu_env);
7257 gen_jmp_im(s->pc - s->cs_base);
7258 gen_eob(s);
7259 break;
7260 case 3: /* stac */
7261 if (!(s->cpuid_7_0_ebx_features & CPUID_7_0_EBX_SMAP) ||
7262 s->cpl != 0) {
7263 goto illegal_op;
7264 }
7265 gen_helper_stac(cpu_env);
7266 gen_jmp_im(s->pc - s->cs_base);
7267 gen_eob(s);
7268 break;
3d7374c5
FB
7269 default:
7270 goto illegal_op;
7271 }
7272 } else { /* sidt */
872929aa 7273 gen_svm_check_intercept(s, pc_start, SVM_EXIT_IDTR_READ);
4eeb3939 7274 gen_lea_modrm(env, s, modrm);
651ba608 7275 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.limit));
fd8ca9f6 7276 gen_op_st_v(s, MO_16, cpu_T[0], cpu_A0);
3d7374c5 7277 gen_add_A0_im(s, 2);
651ba608 7278 tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, idt.base));
ab4e4aec 7279 if (dflag == MO_16) {
f0706f0c
RH
7280 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
7281 }
fd8ca9f6 7282 gen_op_st_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
3d7374c5
FB
7283 }
7284 break;
2c0262af
FB
7285 case 2: /* lgdt */
7286 case 3: /* lidt */
0573fbfc 7287 if (mod == 3) {
773cdfcc 7288 gen_update_cc_op(s);
872929aa 7289 gen_jmp_im(pc_start - s->cs_base);
0573fbfc
TS
7290 switch(rm) {
7291 case 0: /* VMRUN */
872929aa
FB
7292 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7293 goto illegal_op;
7294 if (s->cpl != 0) {
7295 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
0573fbfc 7296 break;
872929aa 7297 } else {
1d71ddb1 7298 gen_helper_vmrun(cpu_env, tcg_const_i32(s->aflag - 1),
a7812ae4 7299 tcg_const_i32(s->pc - pc_start));
db620f46 7300 tcg_gen_exit_tb(0);
5779406a 7301 s->is_jmp = DISAS_TB_JUMP;
872929aa 7302 }
0573fbfc
TS
7303 break;
7304 case 1: /* VMMCALL */
872929aa
FB
7305 if (!(s->flags & HF_SVME_MASK))
7306 goto illegal_op;
052e80d5 7307 gen_helper_vmmcall(cpu_env);
0573fbfc
TS
7308 break;
7309 case 2: /* VMLOAD */
872929aa
FB
7310 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7311 goto illegal_op;
7312 if (s->cpl != 0) {
7313 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7314 break;
7315 } else {
1d71ddb1 7316 gen_helper_vmload(cpu_env, tcg_const_i32(s->aflag - 1));
872929aa 7317 }
0573fbfc
TS
7318 break;
7319 case 3: /* VMSAVE */
872929aa
FB
7320 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7321 goto illegal_op;
7322 if (s->cpl != 0) {
7323 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7324 break;
7325 } else {
1d71ddb1 7326 gen_helper_vmsave(cpu_env, tcg_const_i32(s->aflag - 1));
872929aa 7327 }
0573fbfc
TS
7328 break;
7329 case 4: /* STGI */
872929aa
FB
7330 if ((!(s->flags & HF_SVME_MASK) &&
7331 !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) ||
7332 !s->pe)
7333 goto illegal_op;
7334 if (s->cpl != 0) {
7335 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7336 break;
7337 } else {
052e80d5 7338 gen_helper_stgi(cpu_env);
872929aa 7339 }
0573fbfc
TS
7340 break;
7341 case 5: /* CLGI */
872929aa
FB
7342 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7343 goto illegal_op;
7344 if (s->cpl != 0) {
7345 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7346 break;
7347 } else {
052e80d5 7348 gen_helper_clgi(cpu_env);
872929aa 7349 }
0573fbfc
TS
7350 break;
7351 case 6: /* SKINIT */
872929aa
FB
7352 if ((!(s->flags & HF_SVME_MASK) &&
7353 !(s->cpuid_ext3_features & CPUID_EXT3_SKINIT)) ||
7354 !s->pe)
7355 goto illegal_op;
052e80d5 7356 gen_helper_skinit(cpu_env);
0573fbfc
TS
7357 break;
7358 case 7: /* INVLPGA */
872929aa
FB
7359 if (!(s->flags & HF_SVME_MASK) || !s->pe)
7360 goto illegal_op;
7361 if (s->cpl != 0) {
7362 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7363 break;
7364 } else {
1d71ddb1
RH
7365 gen_helper_invlpga(cpu_env,
7366 tcg_const_i32(s->aflag - 1));
872929aa 7367 }
0573fbfc
TS
7368 break;
7369 default:
7370 goto illegal_op;
7371 }
7372 } else if (s->cpl != 0) {
2c0262af
FB
7373 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7374 } else {
872929aa
FB
7375 gen_svm_check_intercept(s, pc_start,
7376 op==2 ? SVM_EXIT_GDTR_WRITE : SVM_EXIT_IDTR_WRITE);
4eeb3939 7377 gen_lea_modrm(env, s, modrm);
0f712e10 7378 gen_op_ld_v(s, MO_16, cpu_T[1], cpu_A0);
aba9d61e 7379 gen_add_A0_im(s, 2);
909be183 7380 gen_op_ld_v(s, CODE64(s) + MO_32, cpu_T[0], cpu_A0);
ab4e4aec 7381 if (dflag == MO_16) {
f0706f0c
RH
7382 tcg_gen_andi_tl(cpu_T[0], cpu_T[0], 0xffffff);
7383 }
2c0262af 7384 if (op == 2) {
651ba608
FB
7385 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,gdt.base));
7386 tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,gdt.limit));
2c0262af 7387 } else {
651ba608
FB
7388 tcg_gen_st_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,idt.base));
7389 tcg_gen_st32_tl(cpu_T[1], cpu_env, offsetof(CPUX86State,idt.limit));
2c0262af
FB
7390 }
7391 }
7392 break;
7393 case 4: /* smsw */
872929aa 7394 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_CR0);
e2542fe2 7395#if defined TARGET_X86_64 && defined HOST_WORDS_BIGENDIAN
f60d2728 7396 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]) + 4);
7397#else
651ba608 7398 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,cr[0]));
f60d2728 7399#endif
4ba9938c 7400 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 1);
2c0262af
FB
7401 break;
7402 case 6: /* lmsw */
7403 if (s->cpl != 0) {
7404 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7405 } else {
872929aa 7406 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
4ba9938c 7407 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
4a7443be 7408 gen_helper_lmsw(cpu_env, cpu_T[0]);
14ce26e7 7409 gen_jmp_im(s->pc - s->cs_base);
d71b9a8b 7410 gen_eob(s);
2c0262af
FB
7411 }
7412 break;
1b050077
AP
7413 case 7:
7414 if (mod != 3) { /* invlpg */
7415 if (s->cpl != 0) {
7416 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7417 } else {
773cdfcc 7418 gen_update_cc_op(s);
1b050077 7419 gen_jmp_im(pc_start - s->cs_base);
4eeb3939 7420 gen_lea_modrm(env, s, modrm);
4a7443be 7421 gen_helper_invlpg(cpu_env, cpu_A0);
1b050077
AP
7422 gen_jmp_im(s->pc - s->cs_base);
7423 gen_eob(s);
7424 }
2c0262af 7425 } else {
1b050077
AP
7426 switch (rm) {
7427 case 0: /* swapgs */
14ce26e7 7428#ifdef TARGET_X86_64
1b050077
AP
7429 if (CODE64(s)) {
7430 if (s->cpl != 0) {
7431 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7432 } else {
7433 tcg_gen_ld_tl(cpu_T[0], cpu_env,
7434 offsetof(CPUX86State,segs[R_GS].base));
7435 tcg_gen_ld_tl(cpu_T[1], cpu_env,
7436 offsetof(CPUX86State,kernelgsbase));
7437 tcg_gen_st_tl(cpu_T[1], cpu_env,
7438 offsetof(CPUX86State,segs[R_GS].base));
7439 tcg_gen_st_tl(cpu_T[0], cpu_env,
7440 offsetof(CPUX86State,kernelgsbase));
7441 }
5fafdf24 7442 } else
14ce26e7
FB
7443#endif
7444 {
7445 goto illegal_op;
7446 }
1b050077
AP
7447 break;
7448 case 1: /* rdtscp */
7449 if (!(s->cpuid_ext2_features & CPUID_EXT2_RDTSCP))
7450 goto illegal_op;
773cdfcc 7451 gen_update_cc_op(s);
9575cb94 7452 gen_jmp_im(pc_start - s->cs_base);
1b050077
AP
7453 if (use_icount)
7454 gen_io_start();
4a7443be 7455 gen_helper_rdtscp(cpu_env);
1b050077
AP
7456 if (use_icount) {
7457 gen_io_end();
7458 gen_jmp(s, s->pc - s->cs_base);
7459 }
7460 break;
7461 default:
7462 goto illegal_op;
14ce26e7 7463 }
2c0262af
FB
7464 }
7465 break;
7466 default:
7467 goto illegal_op;
7468 }
7469 break;
3415a4dd
FB
7470 case 0x108: /* invd */
7471 case 0x109: /* wbinvd */
7472 if (s->cpl != 0) {
7473 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7474 } else {
872929aa 7475 gen_svm_check_intercept(s, pc_start, (b & 2) ? SVM_EXIT_INVD : SVM_EXIT_WBINVD);
3415a4dd
FB
7476 /* nothing to do */
7477 }
7478 break;
14ce26e7
FB
7479 case 0x63: /* arpl or movslS (x86_64) */
7480#ifdef TARGET_X86_64
7481 if (CODE64(s)) {
7482 int d_ot;
7483 /* d_ot is the size of destination */
ab4e4aec 7484 d_ot = dflag;
14ce26e7 7485
0af10c86 7486 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7
FB
7487 reg = ((modrm >> 3) & 7) | rex_r;
7488 mod = (modrm >> 6) & 3;
7489 rm = (modrm & 7) | REX_B(s);
3b46e624 7490
14ce26e7 7491 if (mod == 3) {
c56baccf 7492 gen_op_mov_v_reg(MO_32, cpu_T[0], rm);
14ce26e7 7493 /* sign extend */
4ba9938c 7494 if (d_ot == MO_64) {
e108dd01 7495 tcg_gen_ext32s_tl(cpu_T[0], cpu_T[0]);
4ba9938c 7496 }
480a762d 7497 gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
14ce26e7 7498 } else {
4eeb3939 7499 gen_lea_modrm(env, s, modrm);
4b1fe067 7500 gen_op_ld_v(s, MO_32 | MO_SIGN, cpu_T[0], cpu_A0);
480a762d 7501 gen_op_mov_reg_v(d_ot, reg, cpu_T[0]);
14ce26e7 7502 }
5fafdf24 7503 } else
14ce26e7
FB
7504#endif
7505 {
3bd7da9e 7506 int label1;
49d9fdcc 7507 TCGv t0, t1, t2, a0;
1e4840bf 7508
14ce26e7
FB
7509 if (!s->pe || s->vm86)
7510 goto illegal_op;
a7812ae4
PB
7511 t0 = tcg_temp_local_new();
7512 t1 = tcg_temp_local_new();
7513 t2 = tcg_temp_local_new();
4ba9938c 7514 ot = MO_16;
0af10c86 7515 modrm = cpu_ldub_code(env, s->pc++);
14ce26e7
FB
7516 reg = (modrm >> 3) & 7;
7517 mod = (modrm >> 6) & 3;
7518 rm = modrm & 7;
7519 if (mod != 3) {
4eeb3939 7520 gen_lea_modrm(env, s, modrm);
323d1876 7521 gen_op_ld_v(s, ot, t0, cpu_A0);
49d9fdcc
LD
7522 a0 = tcg_temp_local_new();
7523 tcg_gen_mov_tl(a0, cpu_A0);
14ce26e7 7524 } else {
1e4840bf 7525 gen_op_mov_v_reg(ot, t0, rm);
49d9fdcc 7526 TCGV_UNUSED(a0);
14ce26e7 7527 }
1e4840bf
FB
7528 gen_op_mov_v_reg(ot, t1, reg);
7529 tcg_gen_andi_tl(cpu_tmp0, t0, 3);
7530 tcg_gen_andi_tl(t1, t1, 3);
7531 tcg_gen_movi_tl(t2, 0);
3bd7da9e 7532 label1 = gen_new_label();
1e4840bf
FB
7533 tcg_gen_brcond_tl(TCG_COND_GE, cpu_tmp0, t1, label1);
7534 tcg_gen_andi_tl(t0, t0, ~3);
7535 tcg_gen_or_tl(t0, t0, t1);
7536 tcg_gen_movi_tl(t2, CC_Z);
3bd7da9e 7537 gen_set_label(label1);
14ce26e7 7538 if (mod != 3) {
323d1876 7539 gen_op_st_v(s, ot, t0, a0);
49d9fdcc
LD
7540 tcg_temp_free(a0);
7541 } else {
1e4840bf 7542 gen_op_mov_reg_v(ot, rm, t0);
14ce26e7 7543 }
d229edce 7544 gen_compute_eflags(s);
3bd7da9e 7545 tcg_gen_andi_tl(cpu_cc_src, cpu_cc_src, ~CC_Z);
1e4840bf 7546 tcg_gen_or_tl(cpu_cc_src, cpu_cc_src, t2);
1e4840bf
FB
7547 tcg_temp_free(t0);
7548 tcg_temp_free(t1);
7549 tcg_temp_free(t2);
f115e911 7550 }
f115e911 7551 break;
2c0262af
FB
7552 case 0x102: /* lar */
7553 case 0x103: /* lsl */
cec6843e
FB
7554 {
7555 int label1;
1e4840bf 7556 TCGv t0;
cec6843e
FB
7557 if (!s->pe || s->vm86)
7558 goto illegal_op;
ab4e4aec 7559 ot = dflag != MO_16 ? MO_32 : MO_16;
0af10c86 7560 modrm = cpu_ldub_code(env, s->pc++);
cec6843e 7561 reg = ((modrm >> 3) & 7) | rex_r;
4ba9938c 7562 gen_ldst_modrm(env, s, modrm, MO_16, OR_TMP0, 0);
a7812ae4 7563 t0 = tcg_temp_local_new();
773cdfcc 7564 gen_update_cc_op(s);
2999a0b2
BS
7565 if (b == 0x102) {
7566 gen_helper_lar(t0, cpu_env, cpu_T[0]);
7567 } else {
7568 gen_helper_lsl(t0, cpu_env, cpu_T[0]);
7569 }
cec6843e
FB
7570 tcg_gen_andi_tl(cpu_tmp0, cpu_cc_src, CC_Z);
7571 label1 = gen_new_label();
cb63669a 7572 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_tmp0, 0, label1);
1e4840bf 7573 gen_op_mov_reg_v(ot, reg, t0);
cec6843e 7574 gen_set_label(label1);
3ca51d07 7575 set_cc_op(s, CC_OP_EFLAGS);
1e4840bf 7576 tcg_temp_free(t0);
cec6843e 7577 }
2c0262af
FB
7578 break;
7579 case 0x118:
0af10c86 7580 modrm = cpu_ldub_code(env, s->pc++);
2c0262af
FB
7581 mod = (modrm >> 6) & 3;
7582 op = (modrm >> 3) & 7;
7583 switch(op) {
7584 case 0: /* prefetchnta */
7585 case 1: /* prefetchnt0 */
7586 case 2: /* prefetchnt0 */
7587 case 3: /* prefetchnt0 */
7588 if (mod == 3)
7589 goto illegal_op;
4eeb3939 7590 gen_lea_modrm(env, s, modrm);
2c0262af
FB
7591 /* nothing more to do */
7592 break;
e17a36ce 7593 default: /* nop (multi byte) */
0af10c86 7594 gen_nop_modrm(env, s, modrm);
e17a36ce 7595 break;
2c0262af
FB
7596 }
7597 break;
e17a36ce 7598 case 0x119 ... 0x11f: /* nop (multi byte) */
0af10c86
BS
7599 modrm = cpu_ldub_code(env, s->pc++);
7600 gen_nop_modrm(env, s, modrm);
e17a36ce 7601 break;
2c0262af
FB
7602 case 0x120: /* mov reg, crN */
7603 case 0x122: /* mov crN, reg */
7604 if (s->cpl != 0) {
7605 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7606 } else {
0af10c86 7607 modrm = cpu_ldub_code(env, s->pc++);
5c73b757
MO
7608 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7609 * AMD documentation (24594.pdf) and testing of
7610 * intel 386 and 486 processors all show that the mod bits
7611 * are assumed to be 1's, regardless of actual values.
7612 */
14ce26e7
FB
7613 rm = (modrm & 7) | REX_B(s);
7614 reg = ((modrm >> 3) & 7) | rex_r;
7615 if (CODE64(s))
4ba9938c 7616 ot = MO_64;
14ce26e7 7617 else
4ba9938c 7618 ot = MO_32;
ccd59d09
AP
7619 if ((prefixes & PREFIX_LOCK) && (reg == 0) &&
7620 (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) {
7621 reg = 8;
7622 }
2c0262af
FB
7623 switch(reg) {
7624 case 0:
7625 case 2:
7626 case 3:
7627 case 4:
9230e66e 7628 case 8:
773cdfcc 7629 gen_update_cc_op(s);
872929aa 7630 gen_jmp_im(pc_start - s->cs_base);
2c0262af 7631 if (b & 2) {
c56baccf 7632 gen_op_mov_v_reg(ot, cpu_T[0], rm);
4a7443be
BS
7633 gen_helper_write_crN(cpu_env, tcg_const_i32(reg),
7634 cpu_T[0]);
14ce26e7 7635 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
7636 gen_eob(s);
7637 } else {
4a7443be 7638 gen_helper_read_crN(cpu_T[0], cpu_env, tcg_const_i32(reg));
480a762d 7639 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
2c0262af
FB
7640 }
7641 break;
7642 default:
7643 goto illegal_op;
7644 }
7645 }
7646 break;
7647 case 0x121: /* mov reg, drN */
7648 case 0x123: /* mov drN, reg */
7649 if (s->cpl != 0) {
7650 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7651 } else {
0af10c86 7652 modrm = cpu_ldub_code(env, s->pc++);
5c73b757
MO
7653 /* Ignore the mod bits (assume (modrm&0xc0)==0xc0).
7654 * AMD documentation (24594.pdf) and testing of
7655 * intel 386 and 486 processors all show that the mod bits
7656 * are assumed to be 1's, regardless of actual values.
7657 */
14ce26e7
FB
7658 rm = (modrm & 7) | REX_B(s);
7659 reg = ((modrm >> 3) & 7) | rex_r;
7660 if (CODE64(s))
4ba9938c 7661 ot = MO_64;
14ce26e7 7662 else
4ba9938c 7663 ot = MO_32;
2c0262af 7664 /* XXX: do it dynamically with CR4.DE bit */
14ce26e7 7665 if (reg == 4 || reg == 5 || reg >= 8)
2c0262af
FB
7666 goto illegal_op;
7667 if (b & 2) {
0573fbfc 7668 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_DR0 + reg);
c56baccf 7669 gen_op_mov_v_reg(ot, cpu_T[0], rm);
4a7443be 7670 gen_helper_movl_drN_T0(cpu_env, tcg_const_i32(reg), cpu_T[0]);
14ce26e7 7671 gen_jmp_im(s->pc - s->cs_base);
2c0262af
FB
7672 gen_eob(s);
7673 } else {
0573fbfc 7674 gen_svm_check_intercept(s, pc_start, SVM_EXIT_READ_DR0 + reg);
651ba608 7675 tcg_gen_ld_tl(cpu_T[0], cpu_env, offsetof(CPUX86State,dr[reg]));
480a762d 7676 gen_op_mov_reg_v(ot, rm, cpu_T[0]);
2c0262af
FB
7677 }
7678 }
7679 break;
7680 case 0x106: /* clts */
7681 if (s->cpl != 0) {
7682 gen_exception(s, EXCP0D_GPF, pc_start - s->cs_base);
7683 } else {
0573fbfc 7684 gen_svm_check_intercept(s, pc_start, SVM_EXIT_WRITE_CR0);
f0967a1a 7685 gen_helper_clts(cpu_env);
7eee2a50 7686 /* abort block because static cpu state changed */
14ce26e7 7687 gen_jmp_im(s->pc - s->cs_base);
7eee2a50 7688 gen_eob(s);
2c0262af
FB
7689 }
7690 break;
222a3336 7691 /* MMX/3DNow!/SSE/SSE2/SSE3/SSSE3/SSE4 support */
664e0f19
FB
7692 case 0x1c3: /* MOVNTI reg, mem */
7693 if (!(s->cpuid_features & CPUID_SSE2))
14ce26e7 7694 goto illegal_op;
ab4e4aec 7695 ot = mo_64_32(dflag);
0af10c86 7696 modrm = cpu_ldub_code(env, s->pc++);
664e0f19
FB
7697 mod = (modrm >> 6) & 3;
7698 if (mod == 3)
7699 goto illegal_op;
7700 reg = ((modrm >> 3) & 7) | rex_r;
7701 /* generate a generic store */
0af10c86 7702 gen_ldst_modrm(env, s, modrm, ot, reg, 1);
14ce26e7 7703 break;
664e0f19 7704 case 0x1ae:
0af10c86 7705 modrm = cpu_ldub_code(env, s->pc++);
664e0f19
FB
7706 mod = (modrm >> 6) & 3;
7707 op = (modrm >> 3) & 7;
7708 switch(op) {
7709 case 0: /* fxsave */
5fafdf24 7710 if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
09d85fb8 7711 (s->prefix & PREFIX_LOCK))
14ce26e7 7712 goto illegal_op;
09d85fb8 7713 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
0fd14b72
FB
7714 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
7715 break;
7716 }
4eeb3939 7717 gen_lea_modrm(env, s, modrm);
773cdfcc 7718 gen_update_cc_op(s);
19e6c4b8 7719 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 7720 gen_helper_fxsave(cpu_env, cpu_A0, tcg_const_i32(dflag == MO_64));
664e0f19
FB
7721 break;
7722 case 1: /* fxrstor */
5fafdf24 7723 if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
09d85fb8 7724 (s->prefix & PREFIX_LOCK))
14ce26e7 7725 goto illegal_op;
09d85fb8 7726 if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
0fd14b72
FB
7727 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
7728 break;
7729 }
4eeb3939 7730 gen_lea_modrm(env, s, modrm);
773cdfcc 7731 gen_update_cc_op(s);
19e6c4b8 7732 gen_jmp_im(pc_start - s->cs_base);
ab4e4aec 7733 gen_helper_fxrstor(cpu_env, cpu_A0, tcg_const_i32(dflag == MO_64));
664e0f19
FB
7734 break;
7735 case 2: /* ldmxcsr */
7736 case 3: /* stmxcsr */
7737 if (s->flags & HF_TS_MASK) {
7738 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
7739 break;
14ce26e7 7740 }
664e0f19
FB
7741 if ((s->flags & HF_EM_MASK) || !(s->flags & HF_OSFXSR_MASK) ||
7742 mod == 3)
14ce26e7 7743 goto illegal_op;
4eeb3939 7744 gen_lea_modrm(env, s, modrm);
664e0f19 7745 if (op == 2) {
80b02013
RH
7746 tcg_gen_qemu_ld_i32(cpu_tmp2_i32, cpu_A0,
7747 s->mem_index, MO_LEUL);
d3eb5eae 7748 gen_helper_ldmxcsr(cpu_env, cpu_tmp2_i32);
14ce26e7 7749 } else {
651ba608 7750 tcg_gen_ld32u_tl(cpu_T[0], cpu_env, offsetof(CPUX86State, mxcsr));
fd8ca9f6 7751 gen_op_st_v(s, MO_32, cpu_T[0], cpu_A0);
14ce26e7 7752 }
664e0f19
FB
7753 break;
7754 case 5: /* lfence */
7755 case 6: /* mfence */
8001c294 7756 if ((modrm & 0xc7) != 0xc0 || !(s->cpuid_features & CPUID_SSE2))
664e0f19
FB
7757 goto illegal_op;
7758 break;
8f091a59
FB
7759 case 7: /* sfence / clflush */
7760 if ((modrm & 0xc7) == 0xc0) {
7761 /* sfence */
a35f3ec7 7762 /* XXX: also check for cpuid_ext2_features & CPUID_EXT2_EMMX */
8f091a59
FB
7763 if (!(s->cpuid_features & CPUID_SSE))
7764 goto illegal_op;
7765 } else {
7766 /* clflush */
7767 if (!(s->cpuid_features & CPUID_CLFLUSH))
7768 goto illegal_op;
4eeb3939 7769 gen_lea_modrm(env, s, modrm);
8f091a59
FB
7770 }
7771 break;
664e0f19 7772 default:
14ce26e7
FB
7773 goto illegal_op;
7774 }
7775 break;
a35f3ec7 7776 case 0x10d: /* 3DNow! prefetch(w) */
0af10c86 7777 modrm = cpu_ldub_code(env, s->pc++);
a35f3ec7
AJ
7778 mod = (modrm >> 6) & 3;
7779 if (mod == 3)
7780 goto illegal_op;
4eeb3939 7781 gen_lea_modrm(env, s, modrm);
8f091a59
FB
7782 /* ignore for now */
7783 break;
3b21e03e 7784 case 0x1aa: /* rsm */
872929aa 7785 gen_svm_check_intercept(s, pc_start, SVM_EXIT_RSM);
3b21e03e
FB
7786 if (!(s->flags & HF_SMM_MASK))
7787 goto illegal_op;
728d803b 7788 gen_update_cc_op(s);
3b21e03e 7789 gen_jmp_im(s->pc - s->cs_base);
608badfc 7790 gen_helper_rsm(cpu_env);
3b21e03e
FB
7791 gen_eob(s);
7792 break;
222a3336
AZ
7793 case 0x1b8: /* SSE4.2 popcnt */
7794 if ((prefixes & (PREFIX_REPZ | PREFIX_LOCK | PREFIX_REPNZ)) !=
7795 PREFIX_REPZ)
7796 goto illegal_op;
7797 if (!(s->cpuid_ext_features & CPUID_EXT_POPCNT))
7798 goto illegal_op;
7799
0af10c86 7800 modrm = cpu_ldub_code(env, s->pc++);
8b4a3df8 7801 reg = ((modrm >> 3) & 7) | rex_r;
222a3336 7802
ab4e4aec 7803 if (s->prefix & PREFIX_DATA) {
4ba9938c 7804 ot = MO_16;
ab4e4aec
RH
7805 } else {
7806 ot = mo_64_32(dflag);
7807 }
222a3336 7808
0af10c86 7809 gen_ldst_modrm(env, s, modrm, ot, OR_TMP0, 0);
d3eb5eae 7810 gen_helper_popcnt(cpu_T[0], cpu_env, cpu_T[0], tcg_const_i32(ot));
480a762d 7811 gen_op_mov_reg_v(ot, reg, cpu_T[0]);
fdb0d09d 7812
3ca51d07 7813 set_cc_op(s, CC_OP_EFLAGS);
222a3336 7814 break;
a35f3ec7
AJ
7815 case 0x10e ... 0x10f:
7816 /* 3DNow! instructions, ignore prefixes */
7817 s->prefix &= ~(PREFIX_REPZ | PREFIX_REPNZ | PREFIX_DATA);
664e0f19
FB
7818 case 0x110 ... 0x117:
7819 case 0x128 ... 0x12f:
4242b1bd 7820 case 0x138 ... 0x13a:
d9f4bb27 7821 case 0x150 ... 0x179:
664e0f19
FB
7822 case 0x17c ... 0x17f:
7823 case 0x1c2:
7824 case 0x1c4 ... 0x1c6:
7825 case 0x1d0 ... 0x1fe:
0af10c86 7826 gen_sse(env, s, b, pc_start, rex_r);
664e0f19 7827 break;
2c0262af
FB
7828 default:
7829 goto illegal_op;
7830 }
7831 /* lock generation */
7832 if (s->prefix & PREFIX_LOCK)
a7812ae4 7833 gen_helper_unlock();
2c0262af
FB
7834 return s->pc;
7835 illegal_op:
ab1f142b 7836 if (s->prefix & PREFIX_LOCK)
a7812ae4 7837 gen_helper_unlock();
2c0262af
FB
7838 /* XXX: ensure that no lock was generated */
7839 gen_exception(s, EXCP06_ILLOP, pc_start - s->cs_base);
7840 return s->pc;
7841}
7842
2c0262af
FB
7843void optimize_flags_init(void)
7844{
fac0aff9
RH
7845 static const char reg_names[CPU_NB_REGS][4] = {
7846#ifdef TARGET_X86_64
7847 [R_EAX] = "rax",
7848 [R_EBX] = "rbx",
7849 [R_ECX] = "rcx",
7850 [R_EDX] = "rdx",
7851 [R_ESI] = "rsi",
7852 [R_EDI] = "rdi",
7853 [R_EBP] = "rbp",
7854 [R_ESP] = "rsp",
7855 [8] = "r8",
7856 [9] = "r9",
7857 [10] = "r10",
7858 [11] = "r11",
7859 [12] = "r12",
7860 [13] = "r13",
7861 [14] = "r14",
7862 [15] = "r15",
7863#else
7864 [R_EAX] = "eax",
7865 [R_EBX] = "ebx",
7866 [R_ECX] = "ecx",
7867 [R_EDX] = "edx",
7868 [R_ESI] = "esi",
7869 [R_EDI] = "edi",
7870 [R_EBP] = "ebp",
7871 [R_ESP] = "esp",
7872#endif
7873 };
7874 int i;
7875
a7812ae4
PB
7876 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
7877 cpu_cc_op = tcg_global_mem_new_i32(TCG_AREG0,
317ac620 7878 offsetof(CPUX86State, cc_op), "cc_op");
317ac620 7879 cpu_cc_dst = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_dst),
a7812ae4 7880 "cc_dst");
a3251186
RH
7881 cpu_cc_src = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src),
7882 "cc_src");
988c3eb0
RH
7883 cpu_cc_src2 = tcg_global_mem_new(TCG_AREG0, offsetof(CPUX86State, cc_src2),
7884 "cc_src2");
437a88a5 7885
fac0aff9
RH
7886 for (i = 0; i < CPU_NB_REGS; ++i) {
7887 cpu_regs[i] = tcg_global_mem_new(TCG_AREG0,
7888 offsetof(CPUX86State, regs[i]),
7889 reg_names[i]);
7890 }
2c0262af
FB
7891}
7892
7893/* generate intermediate code in gen_opc_buf and gen_opparam_buf for
7894 basic block 'tb'. If search_pc is TRUE, also generate PC
7895 information for each intermediate instruction. */
467215c2 7896static inline void gen_intermediate_code_internal(X86CPU *cpu,
2cfc5f17 7897 TranslationBlock *tb,
467215c2 7898 bool search_pc)
2c0262af 7899{
ed2803da 7900 CPUState *cs = CPU(cpu);
467215c2 7901 CPUX86State *env = &cpu->env;
2c0262af 7902 DisasContext dc1, *dc = &dc1;
14ce26e7 7903 target_ulong pc_ptr;
2c0262af 7904 uint16_t *gen_opc_end;
a1d1bb31 7905 CPUBreakpoint *bp;
7f5b7d3e 7906 int j, lj;
c068688b 7907 uint64_t flags;
14ce26e7
FB
7908 target_ulong pc_start;
7909 target_ulong cs_base;
2e70f6ef
PB
7910 int num_insns;
7911 int max_insns;
3b46e624 7912
2c0262af 7913 /* generate intermediate code */
14ce26e7
FB
7914 pc_start = tb->pc;
7915 cs_base = tb->cs_base;
2c0262af 7916 flags = tb->flags;
3a1d9b8b 7917
4f31916f 7918 dc->pe = (flags >> HF_PE_SHIFT) & 1;
2c0262af
FB
7919 dc->code32 = (flags >> HF_CS32_SHIFT) & 1;
7920 dc->ss32 = (flags >> HF_SS32_SHIFT) & 1;
7921 dc->addseg = (flags >> HF_ADDSEG_SHIFT) & 1;
7922 dc->f_st = 0;
7923 dc->vm86 = (flags >> VM_SHIFT) & 1;
7924 dc->cpl = (flags >> HF_CPL_SHIFT) & 3;
7925 dc->iopl = (flags >> IOPL_SHIFT) & 3;
7926 dc->tf = (flags >> TF_SHIFT) & 1;
ed2803da 7927 dc->singlestep_enabled = cs->singlestep_enabled;
2c0262af 7928 dc->cc_op = CC_OP_DYNAMIC;
e207582f 7929 dc->cc_op_dirty = false;
2c0262af
FB
7930 dc->cs_base = cs_base;
7931 dc->tb = tb;
7932 dc->popl_esp_hack = 0;
7933 /* select memory access functions */
7934 dc->mem_index = 0;
7935 if (flags & HF_SOFTMMU_MASK) {
5c42a7cd 7936 dc->mem_index = cpu_mmu_index(env);
2c0262af 7937 }
0514ef2f
EH
7938 dc->cpuid_features = env->features[FEAT_1_EDX];
7939 dc->cpuid_ext_features = env->features[FEAT_1_ECX];
7940 dc->cpuid_ext2_features = env->features[FEAT_8000_0001_EDX];
7941 dc->cpuid_ext3_features = env->features[FEAT_8000_0001_ECX];
7942 dc->cpuid_7_0_ebx_features = env->features[FEAT_7_0_EBX];
14ce26e7
FB
7943#ifdef TARGET_X86_64
7944 dc->lma = (flags >> HF_LMA_SHIFT) & 1;
7945 dc->code64 = (flags >> HF_CS64_SHIFT) & 1;
7946#endif
7eee2a50 7947 dc->flags = flags;
ed2803da 7948 dc->jmp_opt = !(dc->tf || cs->singlestep_enabled ||
a2cc3b24 7949 (flags & HF_INHIBIT_IRQ_MASK)
415fa2ea 7950#ifndef CONFIG_SOFTMMU
2c0262af
FB
7951 || (flags & HF_SOFTMMU_MASK)
7952#endif
7953 );
4f31916f
FB
7954#if 0
7955 /* check addseg logic */
dc196a57 7956 if (!dc->addseg && (dc->vm86 || !dc->pe || !dc->code32))
4f31916f
FB
7957 printf("ERROR addseg\n");
7958#endif
7959
a7812ae4
PB
7960 cpu_T[0] = tcg_temp_new();
7961 cpu_T[1] = tcg_temp_new();
7962 cpu_A0 = tcg_temp_new();
a7812ae4
PB
7963
7964 cpu_tmp0 = tcg_temp_new();
7965 cpu_tmp1_i64 = tcg_temp_new_i64();
7966 cpu_tmp2_i32 = tcg_temp_new_i32();
7967 cpu_tmp3_i32 = tcg_temp_new_i32();
7968 cpu_tmp4 = tcg_temp_new();
a7812ae4
PB
7969 cpu_ptr0 = tcg_temp_new_ptr();
7970 cpu_ptr1 = tcg_temp_new_ptr();
a3251186 7971 cpu_cc_srcT = tcg_temp_local_new();
57fec1fe 7972
92414b31 7973 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
2c0262af
FB
7974
7975 dc->is_jmp = DISAS_NEXT;
7976 pc_ptr = pc_start;
7977 lj = -1;
2e70f6ef
PB
7978 num_insns = 0;
7979 max_insns = tb->cflags & CF_COUNT_MASK;
7980 if (max_insns == 0)
7981 max_insns = CF_COUNT_MASK;
2c0262af 7982
806f352d 7983 gen_tb_start();
2c0262af 7984 for(;;) {
f0c3c505
AF
7985 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
7986 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
a2397807
JK
7987 if (bp->pc == pc_ptr &&
7988 !((bp->flags & BP_CPU) && (tb->flags & HF_RF_MASK))) {
2c0262af 7989 gen_debug(dc, pc_ptr - dc->cs_base);
e64e3535 7990 goto done_generating;
2c0262af
FB
7991 }
7992 }
7993 }
7994 if (search_pc) {
92414b31 7995 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
2c0262af
FB
7996 if (lj < j) {
7997 lj++;
7998 while (lj < j)
ab1103de 7999 tcg_ctx.gen_opc_instr_start[lj++] = 0;
2c0262af 8000 }
25983cad 8001 tcg_ctx.gen_opc_pc[lj] = pc_ptr;
2c0262af 8002 gen_opc_cc_op[lj] = dc->cc_op;
ab1103de 8003 tcg_ctx.gen_opc_instr_start[lj] = 1;
c9c99c22 8004 tcg_ctx.gen_opc_icount[lj] = num_insns;
2c0262af 8005 }
2e70f6ef
PB
8006 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
8007 gen_io_start();
8008
0af10c86 8009 pc_ptr = disas_insn(env, dc, pc_ptr);
2e70f6ef 8010 num_insns++;
2c0262af
FB
8011 /* stop translation if indicated */
8012 if (dc->is_jmp)
8013 break;
8014 /* if single step mode, we generate only one instruction and
8015 generate an exception */
a2cc3b24
FB
8016 /* if irq were inhibited with HF_INHIBIT_IRQ_MASK, we clear
8017 the flag and abort the translation to give the irqs a
8018 change to be happen */
5fafdf24 8019 if (dc->tf || dc->singlestep_enabled ||
2e70f6ef 8020 (flags & HF_INHIBIT_IRQ_MASK)) {
14ce26e7 8021 gen_jmp_im(pc_ptr - dc->cs_base);
2c0262af
FB
8022 gen_eob(dc);
8023 break;
8024 }
8025 /* if too long translation, stop generation too */
efd7f486 8026 if (tcg_ctx.gen_opc_ptr >= gen_opc_end ||
2e70f6ef
PB
8027 (pc_ptr - pc_start) >= (TARGET_PAGE_SIZE - 32) ||
8028 num_insns >= max_insns) {
14ce26e7 8029 gen_jmp_im(pc_ptr - dc->cs_base);
2c0262af
FB
8030 gen_eob(dc);
8031 break;
8032 }
1b530a6d
AJ
8033 if (singlestep) {
8034 gen_jmp_im(pc_ptr - dc->cs_base);
8035 gen_eob(dc);
8036 break;
8037 }
2c0262af 8038 }
2e70f6ef
PB
8039 if (tb->cflags & CF_LAST_IO)
8040 gen_io_end();
e64e3535 8041done_generating:
806f352d 8042 gen_tb_end(tb, num_insns);
efd7f486 8043 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
2c0262af
FB
8044 /* we don't forget to fill the last values */
8045 if (search_pc) {
92414b31 8046 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
2c0262af
FB
8047 lj++;
8048 while (lj <= j)
ab1103de 8049 tcg_ctx.gen_opc_instr_start[lj++] = 0;
2c0262af 8050 }
3b46e624 8051
2c0262af 8052#ifdef DEBUG_DISAS
8fec2b8c 8053 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
14ce26e7 8054 int disas_flags;
93fcfe39
AL
8055 qemu_log("----------------\n");
8056 qemu_log("IN: %s\n", lookup_symbol(pc_start));
14ce26e7
FB
8057#ifdef TARGET_X86_64
8058 if (dc->code64)
8059 disas_flags = 2;
8060 else
8061#endif
8062 disas_flags = !dc->code32;
f4359b9f 8063 log_target_disas(env, pc_start, pc_ptr - pc_start, disas_flags);
93fcfe39 8064 qemu_log("\n");
2c0262af
FB
8065 }
8066#endif
8067
2e70f6ef 8068 if (!search_pc) {
2c0262af 8069 tb->size = pc_ptr - pc_start;
2e70f6ef
PB
8070 tb->icount = num_insns;
8071 }
2c0262af
FB
8072}
8073
317ac620 8074void gen_intermediate_code(CPUX86State *env, TranslationBlock *tb)
2c0262af 8075{
467215c2 8076 gen_intermediate_code_internal(x86_env_get_cpu(env), tb, false);
2c0262af
FB
8077}
8078
317ac620 8079void gen_intermediate_code_pc(CPUX86State *env, TranslationBlock *tb)
2c0262af 8080{
467215c2 8081 gen_intermediate_code_internal(x86_env_get_cpu(env), tb, true);
2c0262af
FB
8082}
8083
317ac620 8084void restore_state_to_opc(CPUX86State *env, TranslationBlock *tb, int pc_pos)
d2856f1a
AJ
8085{
8086 int cc_op;
8087#ifdef DEBUG_DISAS
8fec2b8c 8088 if (qemu_loglevel_mask(CPU_LOG_TB_OP)) {
d2856f1a 8089 int i;
93fcfe39 8090 qemu_log("RESTORE:\n");
d2856f1a 8091 for(i = 0;i <= pc_pos; i++) {
ab1103de 8092 if (tcg_ctx.gen_opc_instr_start[i]) {
25983cad
EV
8093 qemu_log("0x%04x: " TARGET_FMT_lx "\n", i,
8094 tcg_ctx.gen_opc_pc[i]);
d2856f1a
AJ
8095 }
8096 }
e87b7cb0 8097 qemu_log("pc_pos=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
25983cad 8098 pc_pos, tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base,
d2856f1a
AJ
8099 (uint32_t)tb->cs_base);
8100 }
8101#endif
25983cad 8102 env->eip = tcg_ctx.gen_opc_pc[pc_pos] - tb->cs_base;
d2856f1a
AJ
8103 cc_op = gen_opc_cc_op[pc_pos];
8104 if (cc_op != CC_OP_DYNAMIC)
8105 env->cc_op = cc_op;
8106}