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