target/i386/tcg: implement APX
cover target/i386/tcg: implement APX
This series implements APX support. This is little more than some Christmas time hacking, and I have tested it for now only on user mode emulation. The main reason to do this is actually to have some initial infrastructure for EVEX, without requiring all the complexity of AVX512. It also forces some changes that (hopefully) make QEMU's decoder align a bit more with what Intel processors actually do. These (patches 1, 4, 8, 9 in particular) could be extracted and committed separately, though at this point this would be something for 11.1 anyway.
There are relatively few *new* instructions (CCMP/CTEST, CFCMOV, PUSH2/POP2), all of them trivial except for CCMP/CTEST. Therefore, most of the new code is for decoding. The new data destination feature comes almost for free thanks to the existing support for BMI instructions, and variants such as no flags update and zero-upper are quite easy as well. For CCMP/CTEST, I tried to make them reasonably efficient, also thanks to the changes to AF/CF/OF computation that came over the past year, but that takes quite a few lines of code.
Don't expect any performance gains. APX binaries do produce about 1% fewer TCG ops, but they map to about 1% *more* assembly instructions, at least for x86-on-x86: that's because while the optimizer could already produce roughly the same ops as NDD or NF instructions, the new PUSH2/POP2 instructions include a stack alignment check that isn't there in non-APX code. I don't think it's worth wasting a precious HF_ bit for it, at least not until for 10 years or so.
Other than testing system emulation, the main decision to take is whether to treat VEX and EVEX maps as an extension of the regular maps, or just bite the bullet, copy them over to a new array and define them from scratch. There are some annoying differences already in APX (accepted prefixes and opcodes moved to a different spot) and there would be even more with AVX512, for example mask register instructions use opcodes in VEX map 1 that overlap with two-byte 0F opcodes.
I don't think I will have much time to work on this for a few months, since I did it just out of my own interest, but I thought I'd throw it out there for review.
Paolo
Paolo Bonzini (18):
target/i386/tcg: move check bits out of validate_vex target/i386/tcg: add APX support to XSAVE/XRSTOR target/i386/tcg: treat VEX as disabling high-byte registers target/i386/tcg: add definition for REX2 prefix target/i386/tcg: mark XSAVE* as not allowing REX2 target/i386/tcg: decode REX2 prefix target/i386/tcg: implement JMPABS instruction target/i386/tcg: fetch modrm early target/i386/tcg: move VEX validation early target/i386/tcg: extend VEX.vvvv parsing for APX target/i386/tcg: decode EVEX prefix target/i386/tcg: add ZU writeback target/i386/tcg: add decode functionality for APX target/i386/tcg: implement CCMP/CTEST target/i386/tcg: undo IMUL memory load optimization target/i386/tcg: decode APX instructions target/i386/tcg: mark APX as supported target/i386/tcg: optimize CCMP
configs/targets/x86_64-bsd-user.mak | 2 +- configs/targets/x86_64-linux-user.mak | 2 +- target/i386/cpu.h | 8 + target/i386/helper.h | 1 + target/i386/tcg/decode-new.h | 20 + target/i386/tcg/tcg-cpu.h | 16 +- target/i386/tcg/cc_helper_template.h.inc | 11 + target/i386/cpu.c | 15 +- target/i386/helper.c | 11 + target/i386/tcg/cc_helper.c | 10 + target/i386/tcg/excp_helper.c | 5 + target/i386/tcg/fpu_helper.c | 59 +- target/i386/tcg/tcg-cpu.c | 5 +- target/i386/tcg/translate.c | 106 ++- target/i386/tcg/decode-new.c.inc | 937 ++++++++++++++++++----- target/i386/tcg/emit.c.inc | 243 +++++- 16 files changed, 1226 insertions(+), 225 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/18 target/i386/tcg: move check bits out of validate_vex
JMPABS will need the W0 check; move all of these checks together with the others in disas_insn.
--- target/i386/tcg/decode-new.c.inc | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
target/i386/tcg/decode-new.c.inc +9 -18
@@ -2722,24 +2722,6 @@ static bool validate_vex(DisasContext *s, X86DecodedInsn *decode) if (s->flags & HF_EM_MASK) { goto illegal; } - - if (e->check) { - if (e->check & X86_CHECK_VEX128) { - if (s->vex_l) { - goto illegal; - } - } - if (e->check & X86_CHECK_W0) { - if (s->vex_w) { - goto illegal; - } - } - if (e->check & X86_CHECK_W1) { - if (!s->vex_w) { - goto illegal; - } - } - } return true; nm_exception:
@@ -2991,6 +2973,15 @@ static void disas_insn(DisasContext *s, CPUState *cpu) goto illegal_op; } } + if ((decode.e.check & X86_CHECK_VEX128) && s->vex_l) { + goto illegal_op; + } + if ((decode.e.check & X86_CHECK_W0) && s->vex_w) { + goto illegal_op; + } + if ((decode.e.check & X86_CHECK_W1) && !s->vex_w) { + goto illegal_op; + } } switch (decode.e.special) {
Signature · 1 line
2.52.0
PATCH 2/18 target/i386/tcg: add APX support to XSAVE/XRSTOR
In reply to Paolo BonziniWith it, add a new hidden flag that will be used to enable the REX2 prefix and EVEX map4.
--- target/i386/cpu.h | 3 ++ target/i386/tcg/tcg-cpu.h | 16 +++++++--- target/i386/helper.c | 11 +++++++ target/i386/tcg/fpu_helper.c | 59 +++++++++++++++++++++++++++++++++--- target/i386/tcg/tcg-cpu.c | 5 +-- 5 files changed, 83 insertions(+), 11 deletions(-)
target/i386/cpu.h +3 -0
@@ -186,6 +186,7 @@ typedef enum X86Seg {#define HF_MPX_IU_SHIFT 26/* BND registers in-use */ #define HF_UMIP_SHIFT 27/* CR4.UMIP */ #define HF_AVX_EN_SHIFT 28/* AVX Enabled (CR4+XCR0) */ +#define HF_APX_EN_SHIFT 29/* APX Enabled (CR4+XCR0) */ #define HF_CPL_MASK (3 << HF_CPL_SHIFT)#define HF_INHIBIT_IRQ_MASK (1 << HF_INHIBIT_IRQ_SHIFT)
@@ -213,6 +214,7 @@ typedef enum X86Seg {#define HF_MPX_IU_MASK (1 << HF_MPX_IU_SHIFT)#define HF_UMIP_MASK (1 << HF_UMIP_SHIFT)#define HF_AVX_EN_MASK (1 << HF_AVX_EN_SHIFT) +#define HF_APX_EN_MASK (1 << HF_APX_EN_SHIFT)/* hflags2 */
@@ -2729,6 +2731,7 @@ static inline bool x86_has_cpuid_0x1f(X86CPU *cpu)/* helper.c */ void x86_cpu_set_a20 (X86CPU *cpu,int a20_state);void cpu_sync_avx_hflag (CPUX86State *env); +void cpu_sync_apx_hflag (CPUX86State *env); typedef enum X86ASIdx { X86ASIdx_MEM = 0,
target/i386/tcg/tcg-cpu.h +11 -5
@@ -52,9 +52,15 @@ typedef struct X86XSaveArea { -sizeof (X86XSaveHeader) - sizeof(XSaveAVX)]; -/* MPX State: */ -XSaveBNDREG bndreg_state; -XSaveBNDCSR bndcsr_state; +/* Overlapping MPX and APX States: */ +union { +struct { +XSaveBNDREG bndreg; +XSaveBNDCSR bndcsr; + } mpx_state; +XSaveAPX apx_state; + }; +/* AVX-512 State: */ XSaveOpmask opmask_state;XSaveZMM_Hi256 zmm_hi256_state;
@@ -71,8 +77,8 @@ QEMU_BUILD_BUG_ON(offsetof(X86XSaveArea, legacy.mxcsr) != XSAVE_MXCSR_OFFSET); QEMU_BUILD_BUG_ON(offsetof(X86XSaveArea, legacy.fpregs) != XSAVE_ST_SPACE_OFFSET); QEMU_BUILD_BUG_ON(offsetof(X86XSaveArea, legacy.xmm_regs) != XSAVE_XMM_SPACE_OFFSET);QEMU_BUILD_BUG_ON (offsetof(X86XSaveArea , avx_state) !=XSAVE_AVX_OFFSET ); -QEMU_BUILD_BUG_ON (offsetof(X86XSaveArea , bndreg_state) !=XSAVE_BNDREG_OFFSET ); -QEMU_BUILD_BUG_ON (offsetof(X86XSaveArea , bndcsr_state) !=XSAVE_BNDCSR_OFFSET ); +QEMU_BUILD_BUG_ON(offsetof(X86XSaveArea, mpx_state.bndreg) != XSAVE_BNDREG_OFFSET); +QEMU_BUILD_BUG_ON(offsetof(X86XSaveArea, mpx_state.bndcsr) != XSAVE_BNDCSR_OFFSET);QEMU_BUILD_BUG_ON (offsetof(X86XSaveArea , opmask_state) !=XSAVE_OPMASK_OFFSET );QEMU_BUILD_BUG_ON (offsetof(X86XSaveArea , zmm_hi256_state) !=XSAVE_ZMM_HI256_OFFSET );QEMU_BUILD_BUG_ON (offsetof(X86XSaveArea , hi16_zmm_state) !=XSAVE_HI16_ZMM_OFFSET );
target/i386/helper.c +11 -0
@@ -35,6 +35,16 @@#include "tcg/insn-start-words.h" #endif +void cpu_sync_apx_hflag (CPUX86State *env) +{ +if ((env->cr[4 ] &CR4_OSXSAVE_MASK ) + && (env->xcr0 &XSTATE_APX_MASK ) ==XSTATE_APX_MASK ) { + env->hflags |=HF_APX_EN_MASK ; + }else { + env->hflags &= ~HF_APX_EN_MASK ; + } +} +void cpu_sync_avx_hflag (CPUX86State *env) { if ((env->cr[4] & CR4_OSXSAVE_MASK)
@@ -249,6 +259,7 @@ void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)cpu_sync_bndcs_hflags (env);cpu_sync_avx_hflag (env); +cpu_sync_apx_hflag (env); }#if !defined(CONFIG_USER_ONLY )
target/i386/tcg/fpu_helper.c +55 -4
107 lines not on this page · raw message
target/i386/tcg/tcg-cpu.c +3 -2
@@ -198,12 +198,13 @@ static void x86_tcg_cpu_xsave_init(void)XO (XSTATE_FP_BIT , legacy);XO (XSTATE_SSE_BIT , legacy);XO (XSTATE_YMM_BIT , avx_state); -XO (XSTATE_BNDREGS_BIT , bndreg_state); -XO (XSTATE_BNDCSR_BIT , bndcsr_state); +XO (XSTATE_BNDREGS_BIT , mpx_state.bndreg); +XO (XSTATE_BNDCSR_BIT , mpx_state.bndcsr);XO (XSTATE_OPMASK_BIT , opmask_state);XO (XSTATE_ZMM_Hi256_BIT, zmm_hi256_state);XO (XSTATE_Hi16_ZMM_BIT, hi16_zmm_state);XO (XSTATE_PKRU_BIT , pkru_state); +XO (XSTATE_APX_BIT , apx_state);#undef XO }
Signature · 1 line
2.52.0
PATCH 3/18 target/i386/tcg: treat VEX as disabling high-byte registers
In reply to Paolo BonziniThis does not have any functional effect because the only instruction where it would make a difference is VPINSRB/VPINSRW, and they disable high-byte registers even for the non-AVX encoding. However, with APX it becomes a general property of the EVEX prefix and it makes sense to treat VEX the same way since it *does* include REX bits.
--- target/i386/tcg/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/tcg/translate.c +1 -1
@@ -208,7 +208,7 @@ typedef struct DisasContext {#endif #ifdef TARGET_X86_64 -#define REX_PREFIX (S ) (((S)->prefix & PREFIX_REX) != 0) +#define REX_PREFIX (S ) (((S)->prefix & (PREFIX_REX | PREFIX_VEX)) != 0)#define REX_W (S ) ((S)->vex_w)#define REX_R (S ) ((S)->rex_r + 0)#define REX_X (S ) ((S)->rex_x + 0)
Signature · 1 line
2.52.0
PATCH 4/18 target/i386/tcg: add definition for REX2 prefix
In reply to Paolo Bonzini--- target/i386/tcg/translate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/i386/tcg/translate.c +2 -1
@@ -50,6 +50,7 @@#define PREFIX_ADR 0x10#define PREFIX_VEX 0x20#define PREFIX_REX 0x40 +#define PREFIX_REX2 0x80#ifdef TARGET_X86_64 # define ctztl ctz64
@@ -208,7 +209,7 @@ typedef struct DisasContext {#endif #ifdef TARGET_X86_64 -#define REX_PREFIX (S ) (((S)->prefix & (PREFIX_REX | PREFIX_VEX)) != 0) +#define REX_PREFIX (S ) (((S)->prefix & (PREFIX_REX | PREFIX_REX2 | PREFIX_VEX)) != 0)#define REX_W (S ) ((S)->vex_w)#define REX_R (S ) ((S)->rex_r + 0)#define REX_X (S ) ((S)->rex_x + 0)
Signature · 1 line
2.52.0
PATCH 5/18 target/i386/tcg: mark XSAVE* as not allowing REX2
In reply to Paolo Bonzini--- target/i386/tcg/decode-new.h | 3 +++ target/i386/tcg/decode-new.c.inc | 21 ++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-)
target/i386/tcg/decode-new.h +3 -0
@@ -181,6 +181,9 @@ typedef enum X86InsnCheck {/* Vendor-specific checks for Intel/AMD differences */ X86_CHECK_i64_amd =2048 , X86_CHECK_o64_intel =4096 , + +/* No REX2 prefix allowed */ + X86_CHECK_no_rex2 =8192 , } X86InsnCheck; typedef enum X86InsnSpecial {
target/i386/tcg/decode-new.c.inc +10 -11
@@ -481,9 +481,9 @@ static void decode_group15(DisasContext *s, CPUX86State *env, X86OpEntry *entry, [1] = X86_OP_ENTRYr(FXRSTOR, M,y, cpuid(FXSR) p_00), [2] = X86_OP_ENTRYr(LDMXCSR, E,d, vex5 chk(VEX128) p_00), [3] = X86_OP_ENTRYw(STMXCSR, E,d, vex5 chk(VEX128) p_00), - [4] = X86_OP_ENTRYw(XSAVE, M,y, cpuid(XSAVE) p_00), - [5] = X86_OP_ENTRYr(XRSTOR, M,y, cpuid(XSAVE) p_00), - [6] = X86_OP_ENTRYw(XSAVEOPT, M,b, cpuid(XSAVEOPT) p_00), + [4] = X86_OP_ENTRYw(XSAVE, M,y, cpuid(XSAVE) p_00 chk(no_rex2)), + [5] = X86_OP_ENTRYr(XRSTOR, M,y, cpuid(XSAVE) p_00 chk(no_rex2)), + [6] = X86_OP_ENTRYw(XSAVEOPT, M,b, cpuid(XSAVEOPT) p_00 chk(no_rex2)), [7] = X86_OP_ENTRYw(NOP, M,b, cpuid(CLFLUSH) p_00), };
@@ -2963,15 +2963,14 @@ static void disas_insn(DisasContext *s, CPUState *cpu) goto illegal_op; } } - if (decode.e.check & X86_CHECK_prot_or_vm86) { - if (!PE(s)) { - goto illegal_op; - } + if ((decode.e.check & X86_CHECK_prot_or_vm86) && !PE(s)) { + goto illegal_op; } - if (decode.e.check & X86_CHECK_no_vm86) { - if (VM86(s)) { - goto illegal_op; - } + if ((decode.e.check & X86_CHECK_no_vm86) && VM86(s)) { + goto illegal_op; + } + if ((decode.e.check & X86_CHECK_no_rex2) && (s->prefix & PREFIX_REX2)) { + goto illegal_op; } if ((decode.e.check & X86_CHECK_VEX128) && s->vex_l) { goto illegal_op;
Signature · 1 line
2.52.0
PATCH 6/18 target/i386/tcg: decode REX2 prefix
In reply to Paolo BonziniThe REX2 prefix has two main complications: it does not apply to vector registers, and it disables or mutates some opcodes (thus needing separate decoding functions instead of decode_root and decode_0F). Otherwise, all it does is extend s->rex_r, s->rex_w and s->rex_b to two bits.
Since REX2 provides the ability to access r16...r31, extend cpu_regs[] to CPU_NB_EREGS elements.
--- target/i386/tcg/translate.c | 22 +++++- target/i386/tcg/decode-new.c.inc | 114 +++++++++++++++++++++++++++++-- 2 files changed, 126 insertions(+), 10 deletions(-)
target/i386/tcg/translate.c +19 -3
@@ -78,7 +78,7 @@static TCGv cpu_cc_dst, cpu_cc_src, cpu_cc_src2;static TCGv cpu_eip;static TCGv_i32 cpu_cc_op; -static TCGv cpu_regs[CPU_NB_REGS ]; +static TCGv cpu_regs[CPU_NB_EREGS ];static TCGv cpu_seg_base[6 ];static TCGv_i64 cpu_bndl[4 ];static TCGv_i64 cpu_bndu[4 ];
@@ -3349,7 +3349,7 @@ static void gen_multi0F(DisasContext *s, X86DecodedInsn *decode)void tcg_x86_init (void ) { -static const char reg_names[CPU_NB_REGS ][4 ] = { +static const char reg_names[CPU_NB_EREGS ][4 ] = { #ifdef TARGET_X86_64 [R_EAX ] ="rax" , [R_EBX] = "rbx",
@@ -3367,6 +3367,22 @@ void tcg_x86_init(void) [13] = "r13", [14] = "r14", [15] = "r15", + [16] = "r16", + [17] = "r17", + [18] = "r18", + [19] = "r19", + [20] = "r20", + [21] = "r21", + [22] = "r22", + [23] = "r23", + [24] = "r24", + [25] = "r25", + [26] = "r26", + [27] = "r27", + [28] = "r28", + [29] = "r29", + [30] = "r30", + [31] = "r31", #else [R_EAX] = "eax", [R_EBX] = "ebx",
@@ -3411,7 +3427,7 @@ void tcg_x86_init(void) "cc_src2"); cpu_eip =tcg_global_mem_new (tcg_env, offsetof(CPUX86State , eip), eip_name); -for (i =0 ; i <CPU_NB_REGS ; ++i) { +for (i =0 ; i <CPU_NB_EREGS ; ++i) { cpu_regs[i] =tcg_global_mem_new (tcg_env, offsetof(CPUX86State, regs[i]), reg_names[i]);
target/i386/tcg/decode-new.c.inc +107 -7
150 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH 7/18 target/i386/tcg: implement JMPABS instruction
In reply to Paolo BonziniThis is the same as an indirect jump, just with immediate operand. The requirement of W=0 and 0x67 are already available in the decoder.
--- target/i386/tcg/decode-new.h | 3 +++ target/i386/tcg/decode-new.c.inc | 4 ++++ 2 files changed, 7 insertions(+)
target/i386/tcg/decode-new.h +3 -0
@@ -184,6 +184,9 @@ typedef enum X86InsnCheck {/* No REX2 prefix allowed */ X86_CHECK_no_rex2 =8192 , + +/* No 0x67 prefix allowed */ + X86_CHECK_no_adr =16384 , } X86InsnCheck; typedef enum X86InsnSpecial {
target/i386/tcg/decode-new.c.inc +4 -0
@@ -1990,6 +1990,7 @@ static const X86OpEntry opcodes_root[256] = { #ifdef TARGET_X86_64 static const X86OpEntry opcodes_rex2_map0_A0toAF[16] = { + [1] = X86_OP_ENTRY1(JMP_m, I,q, p_00 chk2(W0, no_adr)), }; static void decode_REX2(DisasContext *s, CPUX86State *env, X86OpEntry *entry, uint8_t *b,
@@ -3072,6 +3073,9 @@ static void disas_insn(DisasContext *s, CPUState *cpu) if ((decode.e.check & X86_CHECK_no_rex2) && (s->prefix & PREFIX_REX2)) { goto illegal_op; } + if ((decode.e.check & X86_CHECK_no_adr) && (s->prefix & PREFIX_ADR)) { + goto illegal_op; + } if ((decode.e.check & X86_CHECK_VEX128) && s->vex_l) { goto illegal_op; }
Signature · 1 line
2.52.0
PATCH 8/18 target/i386/tcg: fetch modrm early
In reply to Paolo BonziniAs a first step towards handling EVEX prefixes, fetch the modrm byte before decode_ops(). This way, EVEX validation can use the mod bits: for example, APX instructions require X4=0 if the r/m operand is a register.
--- target/i386/tcg/decode-new.c.inc | 65 ++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 15 deletions(-)
target/i386/tcg/decode-new.c.inc +50 -15
@@ -2085,9 +2085,8 @@ static int reg_nb_mask(DisasContext *s, int unit) static void decode_modrm(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, X86DecodedOp *op) { - int modrm = get_modrm(s, env); - int mod = (modrm >> 6) & 3; - int rm = modrm & 7; + int mod = (s->modrm >> 6) & 3; + int rm = s->modrm & 7; bool is_vsib = decode->e.vex_class == 12; int sib = -1;
@@ -2296,11 +2295,33 @@ static bool decode_op_size(DisasContext *s, X86OpEntry *e, X86OpSize size, MemOp } } +static bool op_has_modrm(X86OpType type) +{ + switch (type) { + case X86_TYPE_C: /* REG in the modrm byte selects a control register */ + case X86_TYPE_D: /* REG in the modrm byte selects a debug register */ + case X86_TYPE_E: /* ALU modrm operand */ + case X86_TYPE_EM: /* modrm byte selects an ALU memory operand */ + case X86_TYPE_G: /* REG in the modrm byte selects a GPR */ + case X86_TYPE_M: /* modrm byte selects a memory operand */ + case X86_TYPE_nop: /* modrm operand decoded but not fetched */ + case X86_TYPE_N: /* R/M in the modrm byte selects an MMX register */ + case X86_TYPE_P: /* REG in the modrm byte selects an MMX register */ + case X86_TYPE_Q: /* MMX modrm operand */ + case X86_TYPE_R: /* R/M in the modrm byte selects a register */ + case X86_TYPE_U: /* R/M in the modrm byte selects an XMM/YMM register */ + case X86_TYPE_V: /* reg in the modrm byte selects an XMM/YMM register */ + case X86_TYPE_WM: /* modrm byte selects an XMM/YMM memory operand */ + case X86_TYPE_W: /* XMM/YMM modrm operand */ + return true; + default: + return false; + } +} + static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, X86DecodedOp *op, X86OpType type, int b) { - int modrm; - switch (type) { case X86_TYPE_None: /* Implicit or absent */ case X86_TYPE_A: /* Implicit */
@@ -2316,7 +2337,7 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, case X86_TYPE_C: /* REG in the modrm byte selects a control register */ op->unit = X86_OP_CR; - op->n = ((get_modrm(s, env) >> 3) & 7) | REX_R(s); + op->n = ((s->modrm >> 3) & 7) | REX_R(s); if (op->n == 0 && (s->prefix & PREFIX_LOCK) && (s->cpuid_ext3_features & CPUID_EXT3_CR8LEG)) { op->n = 8;
@@ -2332,7 +2353,7 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, case X86_TYPE_D: /* REG in the modrm byte selects a debug register */ op->unit = X86_OP_DR; - op->n = ((get_modrm(s, env) >> 3) & 7) | REX_R(s); + op->n = ((s->modrm >> 3) & 7) | REX_R(s); if (op->n >= 8) { /* * illegal opcode. The DR4 and DR5 case is checked in the generated
@@ -2351,14 +2372,14 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, case X86_TYPE_S: /* reg selects a segment register */ op->unit = X86_OP_SEG; - op->n = (get_modrm(s, env) >> 3) & 7; + op->n = (s->modrm >> 3) & 7; /* Values outside [CDEFGS]S, as well as storing to CS, are invalid. */ if (op->n >= 6 || (op->n == R_CS && op == &decode->op[0])) { return false; } break; - case X86_TYPE_P: + case X86_TYPE_P: /* REG in the modrm byte selects an MMX register */ op->unit = X86_OP_MMX; goto get_reg;
@@ -2370,7 +2391,7 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, op->unit = X86_OP_SSE; } get_reg: - op->n = ((get_modrm(s, env) >> 3) & 7); + op->n = ((s->modrm >> 3) & 7); op->n |= REX_R(s) & reg_nb_mask(s, op->unit); break;
@@ -2407,8 +2428,7 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, case X86_TYPE_R: /* R/M in the modrm byte selects a register */ op->unit = X86_OP_INT; get_modrm_reg: - modrm = get_modrm(s, env); - if ((modrm >> 6) != 3) { + if ((s->modrm >> 6) != 3) { return false; } goto get_modrm;
@@ -2422,8 +2442,7 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, /* fall through */ case X86_TYPE_M: /* modrm byte selects a memory operand */ get_modrm_mem: - modrm = get_modrm(s, env); - if ((modrm >> 6) == 3) { + if ((s->modrm >> 6) == 3) { return false; } /* fall through */
@@ -2531,7 +2550,7 @@ static bool decode_insn(DisasContext *s, CPUX86State *env, X86DecodeFunc decode_ return false; } - /* First compute size of operands in order to initialize s->rip_offset. */ + /* Compute size of operands here in order to initialize s->rip_offset. */ if (e->op0 != X86_TYPE_None) { if (!decode_op_size(s, e, e->s0, &decode->op[0].ot)) { return false;
@@ -2564,6 +2583,13 @@ static bool decode_insn(DisasContext *s, CPUX86State *env, X86DecodeFunc decode_ assert(e->op3 == X86_TYPE_I && e->s3 == X86_SIZE_b); s->rip_offset += 1; } + return true; +} + +static bool decode_ops(DisasContext *s, CPUX86State *env, X86DecodeFunc decode_func, + X86DecodedInsn *decode) +{ + X86OpEntry *e = &decode->e; if (e->op0 != X86_TYPE_None && !decode_op(s, env, decode, &decode->op[0], e->op0, decode->b)) {
@@ -3087,6 +3113,15 @@ static void disas_insn(DisasContext *s, CPUState *cpu) } } + if (op_has_modrm(decode.e.op0) || op_has_modrm(decode.e.op1) || + op_has_modrm(decode.e.op2)) { + get_modrm(s, env); + } + + if (!decode_ops(s, env, decode_func, &decode)) { + goto illegal_op; + } + switch (decode.e.special) { case X86_SPECIAL_None: break;
Signature · 1 line
2.52.0
PATCH 9/18 target/i386/tcg: move VEX validation early
In reply to Paolo BonziniPerform the X86_VEX_REPScalar transformation on X86OpEntry instead of X86DecodedOp; everything else can be checked equally well before immediates are fetched etc.
This is especially important for APX, which may decode operands differently depending on the NDD bit: if EVEX.NDD=0, VEX.vvvv is ignored and must be zero.
--- target/i386/tcg/decode-new.c.inc | 54 ++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 24 deletions(-)
target/i386/tcg/decode-new.c.inc +30 -24
@@ -2610,6 +2610,22 @@ static bool decode_ops(DisasContext *s, CPUX86State *env, X86DecodeFunc decode_f decode->immediate = insn_get_signed(env, s, MO_8); } + if (e->vex_class == 12) { + /* Check no overlap between registers. */ + if (!decode->op[0].has_ea && + (decode->op[0].n == decode->mem.index || decode->op[0].n == decode->op[1].n)) { + return false; + } + assert(!decode->op[1].has_ea); + if (decode->op[1].n == decode->mem.index) { + return false; + } + if (!decode->op[2].has_ea && + (decode->op[2].n == decode->mem.index || decode->op[2].n == decode->op[1].n)) { + return false; + } + } + return true; }
@@ -2716,9 +2732,9 @@ static bool validate_vex(DisasContext *s, X86DecodedInsn *decode) if (s->vex_l) { goto illegal; } - assert(decode->e.s2 == X86_SIZE_x); - if (decode->op[2].has_ea) { - decode->op[2].ot = s->prefix & PREFIX_REPZ ? MO_32 : MO_64; + assert(e->op2 == X86_TYPE_W && e->s2 == X86_SIZE_x); + if ((s->modrm >> 6) != 3) { + e->s2 = s->prefix & PREFIX_REPNZ ? X86_SIZE_sd : X86_SIZE_ss; } } break;
@@ -2735,10 +2751,7 @@ static bool validate_vex(DisasContext *s, X86DecodedInsn *decode) switch (e->vex_class) { case 0: - if (s->prefix & PREFIX_VEX) { - goto illegal; - } - return true; + g_assert_not_reached(); case 1: case 2: case 3:
@@ -2763,19 +2776,6 @@ static bool validate_vex(DisasContext *s, X86DecodedInsn *decode) goto illegal; } - /* Check no overlap between registers. */ - if (!decode->op[0].has_ea && - (decode->op[0].n == decode->mem.index || decode->op[0].n == decode->op[1].n)) { - goto illegal; - } - assert(!decode->op[1].has_ea); - if (decode->op[1].n == decode->mem.index) { - goto illegal; - } - if (!decode->op[2].has_ea && - (decode->op[2].n == decode->mem.index || decode->op[2].n == decode->op[1].n)) { - goto illegal; - } /* fall through */ case 6: case 11:
@@ -3118,6 +3118,16 @@ static void disas_insn(DisasContext *s, CPUState *cpu) get_modrm(s, env); } + if (decode.e.vex_class == 0) { + if (s->prefix & (PREFIX_VEX | PREFIX_EVEX)) { + goto illegal_op; + } + } else { + if (!validate_vex(s, &decode)) { + return; + } + } + if (!decode_ops(s, env, decode_func, &decode)) { goto illegal_op; }
@@ -3179,10 +3189,6 @@ static void disas_insn(DisasContext *s, CPUState *cpu) goto illegal_op; } - if (!validate_vex(s, &decode)) { - return; - } - /* * Checks that result in #GP or VMEXIT come second. Intercepts are * generally checked after non-memory exceptions (i.e. after all
Signature · 1 line
2.52.0
PATCH 10/18 target/i386/tcg: extend VEX.vvvv parsing for APX
In reply to Paolo BonziniReuse the code that is already in place for AVX's operand type H, so that APX can use type B without the VEX/EVEX prefix.
APX mostly uses VEX.vvvv for the destination. This is already supported for AVX, though that one only uses VEX.vvvv for the destination in the case of shifts. However, APX has two extra possibilities that we need to consider:
- instructions with VEX/EVEX prefix but no new data destination. To handle this, store into s->vex_ndd whether the B (or H) operand comes from VEX.vvvv or from modrm.
- instructions with operands "m,r" that are extended to "r,m,r". In this case, the non-NDD format "m,r" expands the B operand to a memory location; when this happens, it is incorrect to parse again op1 as a memory location, because that would read the displacement again from the instruction stream. Instead, transform op1 into X86_TYPE_2op.
--- target/i386/tcg/translate.c | 1 + target/i386/tcg/decode-new.c.inc | 38 +++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 13 deletions(-)
target/i386/tcg/translate.c +1 -0
@@ -105,6 +105,7 @@ typedef struct DisasContext {#endif uint8_t vex_l;/* vex vector length */ uint8_t vex_v;/* vex vvvv register, without 1's complement. */ +bool vex_ndd;/* is this a 3-operand instruction? */ uint8_t popl_esp_hack;/* for correct popl with esp base handling */ uint8_t rip_offset;/* only used in x86_64, but left for simplicity */
target/i386/tcg/decode-new.c.inc +25 -13
@@ -2330,10 +2330,9 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, case X86_TYPE_Y: /* string destination */ break; - case X86_TYPE_B: /* VEX.vvvv selects a GPR */ + case X86_TYPE_B: /* VEX.vvvv selects a GPR, else use modrm */ op->unit = X86_OP_INT; - op->n = s->vex_v; - break; + goto get_vex_v; case X86_TYPE_C: /* REG in the modrm byte selects a control register */ op->unit = X86_OP_CR;
@@ -2463,18 +2462,25 @@ static bool decode_op(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode, }; break; - case X86_TYPE_H: /* For AVX, VEX.vvvv selects an XMM/YMM register */ - if ((s->prefix & PREFIX_VEX)) { - op->unit = X86_OP_SSE; + case X86_TYPE_H: /* VEX.vvvv selects an XMM/YMM register, else use modrm */ + op->unit = X86_OP_SSE; + get_vex_v: + if (s->vex_ndd) { + assert(s->prefix & (PREFIX_VEX | PREFIX_EVEX)); op->n = s->vex_v; break; } + /* Two-operand form: the other one of op0 and op1 is a modrm operand, use it. */ + assert(decode->e.s1 == decode->e.s0); if (op == &decode->op[0]) { - /* shifts place the destination in VEX.vvvv, use modrm */ - return decode_op(s, env, decode, op, decode->e.op1, b); + if (!decode_op(s, env, decode, op, decode->e.op1, b)) { + return false; + } + decode->e.op1 = X86_TYPE_2op; } else { - return decode_op(s, env, decode, op, decode->e.op0, b); + *op = decode->op[0]; } + break; case X86_TYPE_I: /* Immediate */ case X86_TYPE_J: /* Relative offset for a jump */
@@ -2717,6 +2723,14 @@ static bool validate_vex(DisasContext *s, X86DecodedInsn *decode) { X86OpEntry *e = &decode->e; + if (s->prefix & (PREFIX_VEX | PREFIX_EVEX)) { + if (e->op0 == X86_TYPE_H || e->op0 == X86_TYPE_B || + e->op1 == X86_TYPE_H || e->op1 == X86_TYPE_B || + e->op2 == X86_TYPE_H || e->op2 == X86_TYPE_B) { + s->vex_ndd = true; + } + } + switch (e->vex_special) { case X86_VEX_None: break;
@@ -2804,10 +2818,7 @@ static bool validate_vex(DisasContext *s, X86DecodedInsn *decode) return true; } - if (s->vex_v != 0 && - e->op0 != X86_TYPE_H && e->op0 != X86_TYPE_B && - e->op1 != X86_TYPE_H && e->op1 != X86_TYPE_B && - e->op2 != X86_TYPE_H && e->op2 != X86_TYPE_B) { + if (s->vex_v != 0 && !s->vex_ndd) { goto illegal; }
@@ -2886,6 +2897,7 @@ static void disas_insn(DisasContext *s, CPUState *cpu) s->vex_l = 0; s->vex_v = 0; s->vex_w = false; + s->vex_ndd = false; s->has_modrm = false; s->prefix = 0;
Signature · 1 line
2.52.0
PATCH 11/18 target/i386/tcg: decode EVEX prefix
In reply to Paolo BonziniEVEX is really messy and the exact position of the fields is spread all over the place. For now store the three data bytes of the prefix in DisasContext, later the EVEX instruction classes for APX will be added to extract_evex_params and validate_vex.
--- target/i386/tcg/translate.c | 8 +++- target/i386/tcg/decode-new.c.inc | 80 ++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 7 deletions(-)
target/i386/tcg/translate.c +6 -2
@@ -51,6 +51,7 @@#define PREFIX_VEX 0x20#define PREFIX_REX 0x40#define PREFIX_REX2 0x80 +#define PREFIX_EVEX 0x100#ifdef TARGET_X86_64 # define ctztl ctz64
@@ -93,8 +94,8 @@ typedef struct DisasContext {MemOp aflag;MemOp dflag; +uint16_t prefix;int8_t override;/* -1 if no override, else R_CS, R_DS, etc */ -uint8_t prefix;bool has_modrm;uint8_t modrm;
@@ -114,6 +115,9 @@ typedef struct DisasContext {uint8_t rex_x;uint8_t rex_b;#endif +uint8_t evex2; +uint8_t evex3; +uint8_t evex4;bool vex_w;/* used by AVX even on 32-bit processors */ bool jmp_opt;/* use direct block chaining for direct jumps */ bool cc_op_dirty;
@@ -210,7 +214,7 @@ typedef struct DisasContext {#endif #ifdef TARGET_X86_64 -#define REX_PREFIX (S ) (((S)->prefix & (PREFIX_REX | PREFIX_REX2 | PREFIX_VEX)) != 0) +#define REX_PREFIX (S ) (((S)->prefix & (PREFIX_REX | PREFIX_REX2 | PREFIX_VEX | PREFIX_EVEX)) != 0)#define REX_W (S ) ((S)->vex_w)#define REX_R (S ) ((S)->rex_r + 0)#define REX_X (S ) ((S)->rex_x + 0)
target/i386/tcg/decode-new.c.inc +75 -5
128 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH 12/18 target/i386/tcg: add ZU writeback
In reply to Paolo Bonzini--- target/i386/tcg/decode-new.h | 3 +++ target/i386/tcg/decode-new.c.inc | 4 ++++ target/i386/tcg/emit.c.inc | 2 ++ 3 files changed, 9 insertions(+)
target/i386/tcg/decode-new.h +3 -0
@@ -212,6 +212,9 @@ typedef enum X86InsnSpecial {*/ X86_SPECIAL_Op0_Rd, +/* ZU-modified APX instruction. */ + X86_SPECIAL_Op0_ZU, +/* * Ry/Mb in the manual (PINSRB). However, the high bits are never used by * the instruction in either the register or memory cases; the *real* effect
target/i386/tcg/decode-new.c.inc +4 -0
@@ -3228,6 +3228,10 @@ static void disas_insn(DisasContext *s, CPUState *cpu) accept_lock = decode.op[0].has_ea; break; + case X86_SPECIAL_Op0_ZU: + assert(decode.op[0].unit == X86_OP_INT); + break; + case X86_SPECIAL_Op0_Rd: assert(decode.op[0].unit == X86_OP_INT); if (!decode.op[0].has_ea) {
target/i386/tcg/emit.c.inc +2 -0
@@ -347,6 +347,8 @@ static void gen_writeback(DisasContext *s, X86DecodedInsn *decode, int opn, TCGv case X86_OP_INT: if (op->has_ea) { gen_op_st_v(s, op->ot, v, s->A0); + } else if (decode->e.special == X86_SPECIAL_Op0_ZU) { + tcg_gen_ext_tl(cpu_regs[op->n], v, op->ot); } else { gen_op_mov_reg_v(s, op->ot, op->n, v); }
Signature · 1 line
2.52.0
PATCH 13/18 target/i386/tcg: add decode functionality for APX
In reply to Paolo BonziniHandle the EVEX formats for APX (including the extension of BMI and CMPccXADD instructions that already supported VEX) and add their validation to validate_vex(). Exit disas_insn() early if the NF field is set.
--- target/i386/tcg/decode-new.h | 10 ++ target/i386/tcg/translate.c | 11 +- target/i386/tcg/decode-new.c.inc | 168 ++++++++++++++++++++++++++++++- 3 files changed, 181 insertions(+), 8 deletions(-)
target/i386/tcg/decode-new.h +10 -0
@@ -187,6 +187,9 @@ typedef enum X86InsnCheck {/* No 0x67 prefix allowed */ X86_CHECK_no_adr =16384 , + +/* EVEX.NF bit not allowed */ + X86_CHECK_nf0 =32768 , } X86InsnCheck; typedef enum X86InsnSpecial {
@@ -274,6 +277,13 @@ typedef enum X86VEXSpecial {X86_VEX_AVX2_256 , } X86VEXSpecial; +typedef enum X86EVEXClass { +/* Non-vector instruction that can use APX EGPRs. */ +X86_EVEX_APX =128 , + X86_EVEX_APX_cmp =129 , + X86_EVEX_APX_pp2 =130 , + X86_EVEX_APX_zu =131 , +}X86EVEXClass ;typedef struct X86OpEntry X86OpEntry ;typedef struct X86DecodedInsn X86DecodedInsn ;
target/i386/tcg/translate.c +7 -4
@@ -107,18 +107,17 @@ typedef struct DisasContext {uint8_t vex_l;/* vex vector length */ uint8_t vex_v;/* vex vvvv register, without 1's complement. */ bool vex_ndd;/* is this a 3-operand instruction? */ +bool vex_w;/* used by AVX even on 32-bit processors */ uint8_t popl_esp_hack;/* for correct popl with esp base handling */ uint8_t rip_offset;/* only used in x86_64, but left for simplicity */ -#ifdef TARGET_X86_64 -uint8_t rex_r; +uint8_t rex_r;/* 0 for i386, but left for simplicity */ uint8_t rex_x;uint8_t rex_b; -#endif +uint8_t evex2;uint8_t evex3;uint8_t evex4; -bool vex_w;/* used by AVX even on 32-bit processors */ bool jmp_opt;/* use direct block chaining for direct jumps */ bool cc_op_dirty;
@@ -219,12 +218,16 @@ typedef struct DisasContext {#define REX_R (S ) ((S)->rex_r + 0)#define REX_X (S ) ((S)->rex_x + 0)#define REX_B (S ) ((S)->rex_b + 0) +#define EVEX_APX_ND (S ) (((S)->evex4 & 0x10) != 0) +#define EVEX_APX_NF (S ) (((S)->evex4 & 0x04) != 0)#else #define REX_PREFIX (S ) false#define REX_W (S ) false#define REX_R (S ) 0#define REX_X (S ) 0#define REX_B (S ) 0 +#define EVEX_APX_ND (S ) 0 +#define EVEX_APX_NF (S ) 0#endif /*
target/i386/tcg/decode-new.c.inc +164 -4
218 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH 14/18 target/i386/tcg: implement CCMP/CTEST
In reply to Paolo BonziniCCMP/CTEST are new instructions that will either perform a CMP/TEST or set EFLAGS to a predetermined value. To support this double functionality without using CC_OP_DYNAMIC, introduce a new CCOp that is inspired by x86_flags.h.
By shifting the carry computation part of compute_aco_sub* to TCG ops, it provides enough flexibility that the result of a subtraction, the result of an AND, and any value of EFLAGS can be encoded with a single CCOp.
--- target/i386/cpu.h | 5 + target/i386/tcg/cc_helper_template.h.inc | 11 ++ target/i386/tcg/cc_helper.c | 10 ++ target/i386/tcg/translate.c | 63 +++++++++++ target/i386/tcg/decode-new.c.inc | 26 ++--- target/i386/tcg/emit.c.inc | 137 ++++++++++++++++++++++- 6 files changed, 238 insertions(+), 14 deletions(-)
target/i386/cpu.h +5 -0
@@ -1582,6 +1582,11 @@ typedef enum {CC_OP_BLSIL ,CC_OP_BLSIQ , +CC_OP_CCMPB ,/* Z via CC_DST, P,S via CC_SRC2, carry-out in CC_SRC */ +CC_OP_CCMPW , +CC_OP_CCMPL , +CC_OP_CCMPQ , +/* * Note that only CC_OP_POPCNT (i.e. the one with MO_TL size) * is used or implemented, because the translation needs
target/i386/tcg/cc_helper_template.h.inc +11 -0
@@ -60,6 +60,17 @@ static uint32_t glue(compute_aco_cout, SUFFIX)(DATA_TYPE carries) return af_cf + of; } +static uint32_t glue(compute_all_ccmp, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1, DATA_TYPE src2) +{ + uint32_t pf, zf, sf; + + /* Compute all bits here because PF and SF do not come from DST. */ + zf = dst == 0 ? CC_Z : 0; + pf = compute_pf(src2); + sf = lshift(src2, 8 - DATA_BITS) & CC_S; + return pf + zf + sf + glue(compute_aco_cout, SUFFIX)(src1); +} + static uint32_t glue(compute_aco_add, SUFFIX)(DATA_TYPE dst, DATA_TYPE src1) { DATA_TYPE src2 = dst - src1;
target/i386/tcg/cc_helper.c +10 -0
@@ -111,6 +111,13 @@ target_ulong helper_cc_compute_all(target_ulong dst, target_ulong src1,case CC_OP_ADCOX :return compute_all_adcox (dst, src1, src2); +case CC_OP_CCMPB : +return compute_all_ccmpb (dst, src1, src2); +case CC_OP_CCMPW : +return compute_all_ccmpw (dst, src1, src2); +case CC_OP_CCMPL : +return compute_all_ccmpl (dst, src1, src2); +case CC_OP_MULB : flags =compute_aco_mul (src1); goto psz_b;
@@ -232,6 +239,9 @@ target_ulong helper_cc_compute_all(target_ulong dst, target_ulong src1, goto psz_l;#ifdef TARGET_X86_64 +case CC_OP_CCMPQ : +return compute_all_ccmpq (dst, src1, src2); +case CC_OP_MULQ : flags =compute_aco_mul (src1); goto psz_q;
target/i386/tcg/translate.c +63 -0
99 lines not on this page · raw message
target/i386/tcg/decode-new.c.inc +13 -13
@@ -1581,7 +1581,7 @@ static void decode_63(DisasContext *s, CPUX86State *env, X86OpEntry *entry, uint static void decode_group1(DisasContext *s, CPUX86State *env, X86OpEntry *entry, uint8_t *b) { static const X86GenFunc group1_gen[8] = { - gen_ADD, gen_OR, gen_ADC, gen_SBB, gen_AND, gen_SUB, gen_XOR, gen_SUB, + gen_ADD, gen_OR, gen_ADC, gen_SBB, gen_AND, gen_SUB, gen_XOR, gen_CMP, }; int op = (get_modrm(s, env) >> 3) & 7; entry->gen = group1_gen[op];
@@ -1628,7 +1628,7 @@ static void decode_group3(DisasContext *s, CPUX86State *env, X86OpEntry *entry, { static const X86OpEntry opcodes_grp3[16] = { /* 0xf6 */ - [0x00] = X86_OP_ENTRYrr(AND, E,b, I,b), + [0x00] = X86_OP_ENTRYrr(TEST, E,b, I,b), [0x02] = X86_OP_ENTRY1(NOT, E,b, lock), [0x03] = X86_OP_ENTRY1(NEG, E,b, lock), [0x04] = X86_OP_ENTRYrr(MUL, E,b, 0,b, zextT0),
@@ -1637,7 +1637,7 @@ static void decode_group3(DisasContext *s, CPUX86State *env, X86OpEntry *entry, [0x07] = X86_OP_ENTRYr(IDIV, E,b), /* 0xf7 */ - [0x08] = X86_OP_ENTRYrr(AND, E,v, I,z), + [0x08] = X86_OP_ENTRYrr(TEST, E,v, I,z), [0x0a] = X86_OP_ENTRY1(NOT, E,v, lock), [0x0b] = X86_OP_ENTRY1(NEG, E,v, lock), [0x0c] = X86_OP_ENTRYrr(MUL, E,v, 0,v, zextT0),
@@ -1776,8 +1776,8 @@ static const X86OpEntry opcodes_root[256] = { [0x81] = X86_OP_GROUP2(group1, E,v, I,z), [0x82] = X86_OP_GROUP2(group1, E,b, I,b, chk(i64)), [0x83] = X86_OP_GROUP2(group1, E,v, I,b), - [0x84] = X86_OP_ENTRYrr(AND, E,b, G,b), - [0x85] = X86_OP_ENTRYrr(AND, E,v, G,v), + [0x84] = X86_OP_ENTRYrr(TEST, E,b, G,b), + [0x85] = X86_OP_ENTRYrr(TEST, E,v, G,v), [0x86] = X86_OP_ENTRY2(XCHG, E,b, G,b, xchg), [0x87] = X86_OP_ENTRY2(XCHG, E,v, G,v, xchg),
@@ -1868,12 +1868,12 @@ static const X86OpEntry opcodes_root[256] = { [0x2E] = {}, [0x2F] = X86_OP_ENTRY0(DAS, chk(i64)), - [0x38] = X86_OP_ENTRYrr(SUB, E,b, G,b), - [0x39] = X86_OP_ENTRYrr(SUB, E,v, G,v), - [0x3A] = X86_OP_ENTRYrr(SUB, G,b, E,b), - [0x3B] = X86_OP_ENTRYrr(SUB, G,v, E,v), - [0x3C] = X86_OP_ENTRYrr(SUB, 0,b, I,b), /* AL, Ib */ - [0x3D] = X86_OP_ENTRYrr(SUB, 0,v, I,z), /* rAX, Iz */ + [0x38] = X86_OP_ENTRYrr(CMP, E,b, G,b), + [0x39] = X86_OP_ENTRYrr(CMP, E,v, G,v), + [0x3A] = X86_OP_ENTRYrr(CMP, G,b, E,b), + [0x3B] = X86_OP_ENTRYrr(CMP, G,v, E,v), + [0x3C] = X86_OP_ENTRYrr(CMP, 0,b, I,b), /* AL, Ib */ + [0x3D] = X86_OP_ENTRYrr(CMP, 0,v, I,z), /* rAX, Iz */ [0x3E] = {}, [0x3F] = X86_OP_ENTRY0(AAS, chk(i64)),
@@ -1932,8 +1932,8 @@ static const X86OpEntry opcodes_root[256] = { [0x9E] = X86_OP_ENTRY0(SAHF), [0x9F] = X86_OP_ENTRY0(LAHF), - [0xA8] = X86_OP_ENTRYrr(AND, 0,b, I,b), /* AL, Ib */ - [0xA9] = X86_OP_ENTRYrr(AND, 0,v, I,z), /* rAX, Iz */ + [0xA8] = X86_OP_ENTRYrr(TEST, 0,b, I,b), /* AL, Ib */ + [0xA9] = X86_OP_ENTRYrr(TEST, 0,v, I,z), /* rAX, Iz */ [0xAA] = X86_OP_ENTRYwr(STOS, Y,b, 0,b), [0xAB] = X86_OP_ENTRYwr(STOS, Y,v, 0,v), /* Manual writeback because REP LODS (!) has to write EAX/RAX after every LODS. */
target/i386/tcg/emit.c.inc +136 -1
155 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH 15/18 target/i386/tcg: undo IMUL memory load optimization
In reply to Paolo BonziniThe IMUL 0F AF encoding was using T0 for the (possible) memory operand so as to use a load with sign extension. However, this trick will be incompatible with APX's new data destination extension which will use "B,v" for the destination. If no VEX/EVEX prefix is present, the "B" operand type defaults to op1, hence op1 has to be "G,v" for correct decoding of the non-APX instruction.
This is only needed because EVEX map 4's 0xA0-0xAF slots reuse the decode tables for pre-APX instructions.
--- target/i386/tcg/decode-new.c.inc | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
target/i386/tcg/decode-new.c.inc +1 -5
@@ -1504,11 +1504,7 @@ static const X86OpEntry opcodes_0F[256] = { [0xac] = X86_OP_ENTRY4(SHRD, E,v, 2op,v, G,v), [0xad] = X86_OP_ENTRY3(SHRD, E,v, 2op,v, G,v), [0xae] = X86_OP_GROUP0(group15), - /* - * It's slightly more efficient to put Ev operand in T0 and allow gen_IMUL3 - * to assume sextT0. Multiplication is commutative anyway. - */ - [0xaf] = X86_OP_ENTRY3(IMUL3, G,v, E,v, 2op,v, sextT0), + [0xaf] = X86_OP_ENTRY2(IMUL3, G,v, E,v, sextT0), [0xb8] = X86_OP_GROUP0(0FB8), /* decoded as modrm, which is visible as a difference between page fault and #UD */
Signature · 1 line
2.52.0
PATCH 16/18 target/i386/tcg: decode APX instructions
In reply to Paolo BonziniThe bulk of the APX implementation, comprising new map4-specific encodings, extensions to legacy root and 0F tables, and the implementation of new instructions CFCMOV, PUSH2 and POP2.
--- target/i386/helper.h | 1 + target/i386/tcg/decode-new.h | 1 + target/i386/tcg/excp_helper.c | 5 + target/i386/tcg/decode-new.c.inc | 356 ++++++++++++++++++++++--------- target/i386/tcg/emit.c.inc | 55 +++++ 5 files changed, 320 insertions(+), 98 deletions(-)
target/i386/helper.h +1 -0
@@ -57,6 +57,7 @@ DEF_HELPER_2(sysret, void, env, int) DEF_HELPER_FLAGS_1(pause, TCG_CALL_NO_WG, noreturn, env) DEF_HELPER_FLAGS_3(raise_interrupt, TCG_CALL_NO_WG, noreturn, env, int, int) DEF_HELPER_FLAGS_2(raise_exception, TCG_CALL_NO_WG, noreturn, env, int) +DEF_HELPER_FLAGS_1(raise_gpf, TCG_CALL_NO_WG, noreturn, env) DEF_HELPER_FLAGS_1(icebp, TCG_CALL_NO_WG, noreturn, env) DEF_HELPER_3(boundw, void, env, tl, int)DEF_HELPER_3 (boundl, void, env, tl, int)
target/i386/tcg/decode-new.h +1 -0
@@ -53,6 +53,7 @@ typedef enum X86OpType { X86_TYPE_nop,/* modrm operand decoded but not loaded into s->T{0,1} */ X86_TYPE_2op,/* 2-operand RMW instruction */ X86_TYPE_LoBits,/* encoded in bits 0-2 of the operand + REX.B */ +X86_TYPE_ZERO ,/* Constant zero, for CFCMOV */ X86_TYPE_0 ,/* Hard-coded GPRs (RAX..RDI) */ X86_TYPE_1 , X86_TYPE_2,
target/i386/tcg/excp_helper.c +5 -0
@@ -36,6 +36,11 @@ G_NORETURN void helper_raise_exception(CPUX86State *env, int exception_index)raise_exception (env, exception_index); } +G_NORETURN void helper_raise_gpf(CPUX86State *env) +{ +raise_exception_err_ra (env,EXCP0D_GPF ,0 ,GETPC ()); +} +/* * Check nested exceptions and change to double or triple fault if * needed. It should only be called, if this is not an interrupt.
target/i386/tcg/decode-new.c.inc +258 -98
533 lines not on this page · raw message
target/i386/tcg/emit.c.inc +55 -0
73 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH 17/18 target/i386/tcg: mark APX as supported
In reply to Paolo BonziniFinally allow TCG to set the CPUID bit for the APXF feature. The effect of APX is about 0.5% larger binaries and about 1% less TCG ops. QEMU seems to produce about 1% *more* assembly instructions, because the optimizer could already produce roughly the same ops as NDD or NF instructions, and the new PUSH2/POP2 instructions include a stack alignment check that isn't there in non-APX code.
--- configs/targets/x86_64-bsd-user.mak | 2 +- configs/targets/x86_64-linux-user.mak | 2 +- target/i386/cpu.c | 15 +++++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-)
configs/targets/x86_64-bsd-user.mak +1 -1
@@ -1,4 +1,4 @@ TARGET_ARCH=x86_64 TARGET_BASE_ARCH=i386 -TARGET_XML_FILES= gdb-xml/i386-64bit.xml +TARGET_XML_FILES= gdb-xml/i386-64bit.xml gdb-xml/i386-64bit-apx.xml TARGET_LONG_BITS=64
configs/targets/x86_64-linux-user.mak +1 -1
@@ -2,5 +2,5 @@ TARGET_ARCH=x86_64 TARGET_BASE_ARCH=i386 TARGET_SYSTBL_ABI=common,64 TARGET_SYSTBL=syscall_64.tbl -TARGET_XML_FILES= gdb-xml/i386-64bit.xml gdb-xml/i386-64bit-linux.xml +TARGET_XML_FILES= gdb-xml/i386-64bit.xml gdb-xml/i386-64bit-linux.xml gdb-xml/i386-64bit-apx.xml TARGET_LONG_BITS=64
target/i386/cpu.c +13 -2
@@ -1001,7 +1001,7 @@ void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,#define TCG_7_1_EAX_FEATURES (CPUID_7_1_EAX_FZRM | CPUID_7_1_EAX_FSRS | \ CPUID_7_1_EAX_FSRC | CPUID_7_1_EAX_CMPCCXADD)#define TCG_7_1_ECX_FEATURES 0 -#define TCG_7_1_EDX_FEATURES 0 +#define TCG_7_1_EDX_FEATURES CPUID_7_1_EDX_APXF#define TCG_7_2_EDX_FEATURES 0#define TCG_APM_FEATURES 0#define TCG_6_EAX_FEATURES CPUID_6_EAX_ARAT
@@ -1550,7 +1550,7 @@ FeatureWordInfo feature_word_info[FEATURE_WORDS] = { }, .tcg_features = XSTATE_FP_MASK | XSTATE_SSE_MASK |XSTATE_YMM_MASK |XSTATE_BNDREGS_MASK |XSTATE_BNDCSR_MASK | - XSTATE_PKRU_MASK, + XSTATE_PKRU_MASK | XSTATE_APX_MASK, .migratable_flags =XSTATE_FP_MASK |XSTATE_SSE_MASK |XSTATE_YMM_MASK |XSTATE_BNDREGS_MASK |XSTATE_BNDCSR_MASK | XSTATE_OPMASK_MASK | XSTATE_ZMM_Hi256_MASK | XSTATE_Hi16_ZMM_MASK |
@@ -9424,6 +9424,17 @@ void x86_cpu_expand_features(X86CPU *cpu, Error **errp)* inside x86_cpu_parse_featurestr() too. */ if (xcc->max_features) { +/* +* TCG supports both MPX and APX. Since they they cannot be enabled together, +* disable one---prefer APX if none was chosen explicitly. +*/ +if ((x86_cpu_get_supported_feature_word (cpu,FEAT_7_1_EDX ) &CPUID_7_1_EDX_APXF ) && + env->user_features[FEAT_7_0_EBX ] &CPUID_7_0_EBX_MPX ) { + feature_word_info[FEAT_7_1_EDX ].no_autoenable_flags |=CPUID_7_1_EDX_APXF ; + }else { + feature_word_info[FEAT_7_0_EBX ].no_autoenable_flags |=CPUID_7_0_EBX_MPX ; + } +for (w =0 ; w <FEATURE_WORDS ; w++) {/* Override only features that weren't set explicitly * by the user.
Signature · 1 line
2.52.0
PATCH 18/18 target/i386/tcg: optimize CCMP
In reply to Paolo BonziniUse CC_OP_SUB* and CC_OP_LOGIC* if specific dfv values make it possible; this avoids having to compute the carry. This happens relatively often for dfv=0, and also for dfv=sf for code produced by clang.
Of the combinations that cannot be optimized, both GCC and clang generate Z. GCC also generates OS and OSZ.
Do not bother doing this for CTEST; the savings are modest because it does not need complex code to compute the carry-out vector (CC_SRC is always 0). In addition, trivially replacing the arguments to the AND would only support dfv=0 (produced by -2 & 2) or dfv=S (produced by -2 & -2), because TEST cannot produce any value where CF or OF are not zero.
--- target/i386/tcg/emit.c.inc | 63 +++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 7 deletions(-)
target/i386/tcg/emit.c.inc +56 -7
@@ -1748,6 +1748,56 @@ static void gen_dfv_movcond(DisasContext *s, X86DecodedInsn *decode, int cond, u decode->cc_dst, tcg_constant_tl(dst)); } +static bool gen_ccmp_movcond(DisasContext *s, int cond, uint32_t dfv, MemOp ot) +{ + target_ulong op0, op1, max_int; + if (cond == (CCMP_T << 1)) { + return true; + } + + /* + * For some values of dfv, it's possible to overwrite operands + * instead of using CC_OP_CCMP. + */ + max_int = (target_ulong)1 << ((8 << ot) - 1); + switch(dfv) { + case 0: + op0 = 1, op1 = 0; + break; + case CC_P|CC_C: + op0 = 1, op1 = -16; + break; + case CC_S: + op0 = -3, op1 = 0; + break; + case CC_S|CC_P|CC_C: + op0 = 0, op1 = 16; + break; + case CC_O: + /* + * For 8-bit results the sign bit is in bit 7 and, having no copies to the + * right, it flips the parity flag. + */ + op0 = -max_int, op1 = 16 + (ot == MO_8); + break; + case CC_O|CC_S|CC_P|CC_C: + /* Same here. */ + op0 = max_int, op1 = -1 - (ot == MO_8); + break; + default: + return false; + } + + CCPrepare cc = gen_prepare_cc(s, cond, NULL); + if (!cc.use_reg2) { + cc.reg2 = tcg_constant_tl(cc.imm); + } + + tcg_gen_movcond_tl(cc.cond, s->T0, cc.reg, cc.reg2, s->T0, tcg_constant_tl(op0)); + tcg_gen_movcond_tl(cc.cond, s->T1, cc.reg, cc.reg2, s->T1, tcg_constant_tl(op1)); + return true; +} + static void gen_SUB(DisasContext *s, X86DecodedInsn *decode); static void gen_CMP(DisasContext *s, X86DecodedInsn *decode) {
@@ -1756,16 +1806,15 @@ static void gen_CMP(DisasContext *s, X86DecodedInsn *decode) MemOp ot = decode->op[1].ot; TCGv cout; - switch (cond) { - case CCMP_T << 1: - gen_SUB(s, decode); - return; - case (CCMP_T << 1) | 1: + if (cond == ((CCMP_T << 1) | 1)) { decode->cc_op = CC_OP_EFLAGS; decode->cc_src = tcg_constant_tl(dfv); return; - default: - break; + } + + if (gen_ccmp_movcond(s, cond, dfv, ot)) { + gen_SUB(s, decode); + return; } decode->cc_op = CC_OP_CCMPB + ot;
Signature · 1 line
2.52.0