x86emul: further work
v10 cover x86emul: further work
The first two patches are bug fixes, in part pointed out by the 3rd patch. The remainder is further enabling.
1: address x86_insn_is_mem_{access,write}() omissions 2: rework CMP and TEST emulation
3: also test decoding and mem access / write logic 4: disable FPU/MMX/SIMD insn emulation when !HVM 5: support MOVDIR{I,64B} insns
6: support ENQCMD insn
7: support FNSTENV and FNSAVE
8: support FLDENV and FRSTOR
9: support FXSAVE/FXRSTOR
Main changes from v9 are several fixes in patch 1 and the new patch 2, both a result of the new patch 3. For other changes see the individual patches.
Jan
PATCH v10 1/9 x86emul: address x86_insn_is_mem_{access,write}() omissions
First of all explain in comments what the functions' purposes are. Then make them actually match their comments.
Note that fc6fa977be54 ("x86emul: extend x86_insn_is_mem_write() coverage") didn't actually fix the function's behavior for {,V}STMXCSR:
Both are covered by generic code higher up in the function, due to x86_decode_twobyte() already doing suitable adjustments. And VSTMXCSR wouldn't have been covered anyway without a further X86EMUL_OPC_VEX() case label. Keep the inner case label in a comment for reference.
---
v10: Move ARPL case to earlier switch() x86_insn_is_mem_write(). Make
group 5 handling actually work there. Drop VMPTRST case. Also handle CLFLUSH*, CLWB, UDn, and remaining PREFETCH* in x86_insn_is_mem_access().
v9: New.
xen/arch/x86/x86_emulate/x86_emulate.c +118 -8
@@ -11474,25 +11474,87 @@ x86_insn_operand_ea(const struct x86_emureturn state->ea.mem.off; } +/* +* This function means to return 'true' for all supported insns with explicit +* accesses to memory. This means also insns which don't have an explicit +* memory operand (like POP), but it does not mean e.g. segment selector +* loads, where the descriptor table access is considered an implicit one. +*/ bool x86_insn_is_mem_access (const struct x86_emulate_state *state,const struct x86_emulate_ctxt *ctxt) { +if (mode_64bit () && state->not_64bit ) +return false; +if ( state->ea.type ==OP_MEM ) -return ctxt->opcode !=0x8d /* LEA */ && - (ctxt->opcode !=X86EMUL_OPC (0x0f ,0x01 ) || - (state->modrm_reg &7 ) !=7 )/* INVLPG */ ; + { +switch ( ctxt->opcode ) + { +case 0x8d :/* LEA */ +case X86EMUL_OPC (0x0f ,0x0d ):/* PREFETCH */ +case X86EMUL_OPC (0x0f ,0x18 ) + ... X86EMUL_OPC(0x0f, 0x1f): /* NOP space */ +case X86EMUL_OPC_66 (0x0f ,0x18 ) + ... X86EMUL_OPC_66(0x0f, 0x1f): /* NOP space */ +case X86EMUL_OPC_F3 (0x0f ,0x18 ) + ... X86EMUL_OPC_F3(0x0f, 0x1f): /* NOP space */ +case X86EMUL_OPC_F2 (0x0f ,0x18 ) + ... X86EMUL_OPC_F2(0x0f, 0x1f): /* NOP space */ +case X86EMUL_OPC (0x0f ,0xb9 ):/* UD1 */ +case X86EMUL_OPC (0x0f ,0xff ):/* UD0 */ +return false; + +case X86EMUL_OPC (0x0f ,0x01 ): +return (state->modrm_reg &7 ) !=7 ;/* INVLPG */ + +case X86EMUL_OPC (0x0f ,0xae ): +return (state->modrm_reg &7 ) !=7 ;/* CLFLUSH */ + +case X86EMUL_OPC_66 (0x0f ,0xae ): +return (state->modrm_reg &7 ) <6 ;/* CLWB, CLFLUSHOPT */ + } + +return true; + }switch ( ctxt->opcode ) { + case 0x06 ... 0x07: /* PUSH / POP %es */ +case 0x0e :/* PUSH %cs */ + case 0x16 ... 0x17: /* PUSH / POP %ss */ + case 0x1e ... 0x1f: /* PUSH / POP %ds */ + case 0x50 ... 0x5f: /* PUSH / POP reg */ + case 0x60 ... 0x61: /* PUSHA / POPA */ +case 0x68 :case 0x6a :/* PUSH imm */ case 0x6c ... 0x6f: /* INS / OUTS */ +case 0x8f :/* POP r/m */ +case 0x9a :/* CALL (far, direct) */ + case 0x9c ... 0x9d: /* PUSHF / POPF */ case 0xa4 ... 0xa7: /* MOVS / CMPS */ case 0xaa ... 0xaf: /* STOS / LODS / SCAS */ + case 0xc2 ... 0xc3: /* RET (near) */ + case 0xc8 ... 0xc9: /* ENTER / LEAVE */ + case 0xca ... 0xcb: /* RET (far) */case 0xd7 :/* XLAT */ +case 0xe8 :/* CALL (near, direct) */ +case X86EMUL_OPC (0x0f ,0xa0 ):/* PUSH %fs */ +case X86EMUL_OPC (0x0f ,0xa1 ):/* POP %fs */ +case X86EMUL_OPC (0x0f ,0xa8 ):/* PUSH %gs */ +case X86EMUL_OPC (0x0f ,0xa9 ):/* POP %gs */ CASE_SIMD_PACKED_INT_VEX(0x0f, 0xf7): /* MASKMOV{Q,DQU} *//* VMASKMOVDQU */ return true; +case 0xff : +switch ( state->modrm_reg &7 ) + { +case 2 :/* CALL (near, indirect) */ +case 6 :/* PUSH r/m */ +return true; + } +break ; +case X86EMUL_OPC (0x0f ,0x01 ):/* Cover CLZERO. */ return (state->modrm_rm &7 ) ==4 && (state->modrm_reg &7 ) ==7 ;
@@ -11501,10 +11563,20 @@ x86_insn_is_mem_access(const struct x86_return false; } +/* +* This function means to return 'true' for all supported insns with explicit +* writes to memory. This means also insns which don't have an explicit +* memory operand (like PUSH), but it does not mean e.g. segment selector +* loads, where the (possible) descriptor table write is considered an +* implicit access. +*/ bool x86_insn_is_mem_write (const struct x86_emulate_state *state,const struct x86_emulate_ctxt *ctxt) { +if (mode_64bit () && state->not_64bit ) +return false; +switch ( state->desc & DstMask ) {case DstMem:
@@ -11516,19 +11588,48 @@ x86_insn_is_mem_write(const struct x86_ebreak ;default : +switch ( ctxt->opcode ) + { +case 0x63 :/* ARPL */ +return !mode_64bit (); + } +return false; }if ( state->modrm_mod ==3 ) -/* CLZERO is the odd one. */ -return ctxt->opcode ==X86EMUL_OPC (0x0f ,0x01 ) && - (state->modrm_rm &7 ) ==4 && (state->modrm_reg &7 ) ==7 ; + { +switch ( ctxt->opcode ) + { +case 0xff :/* Grp5 */ +break ; + +case X86EMUL_OPC (0x0f ,0x01 ):/* CLZERO is the odd one. */ +return (state->modrm_rm &7 ) ==4 && (state->modrm_reg &7 ) ==7 ; + +default : +return false; + } + }switch ( ctxt->opcode ) { +case 0x06 :/* PUSH %es */ +case 0x0e :/* PUSH %cs */ +case 0x16 :/* PUSH %ss */ +case 0x1e :/* PUSH %ds */ + case 0x50 ... 0x57: /* PUSH reg */ +case 0x60 :/* PUSHA */ +case 0x68 :case 0x6a :/* PUSH imm */ case 0x6c :case 0x6d :/* INS */ +case 0x9a :/* CALL (far, direct) */ +case 0x9c :/* PUSHF */ case 0xa4 :case 0xa5 :/* MOVS */ case 0xaa :case 0xab :/* STOS */ +case 0xc8 :/* ENTER */ +case 0xe8 :/* CALL (near, direct) */ +case X86EMUL_OPC (0x0f ,0xa0 ):/* PUSH %fs */ +case X86EMUL_OPC (0x0f ,0xa8 ):/* PUSH %gs */ case X86EMUL_OPC (0x0f ,0xab ):/* BTS */ case X86EMUL_OPC (0x0f ,0xb3 ):/* BTR */ case X86EMUL_OPC (0x0f ,0xbb ):/* BTC */
@@ -11586,6 +11687,16 @@ x86_insn_is_mem_write(const struct x86_e }break ; +case 0xff : +switch ( state->modrm_reg &7 ) + { +case 2 :/* CALL (near, indirect) */ +case 3 :/* CALL (far, indirect) */ +case 6 :/* PUSH r/m */ +return true; + } +break ; +case X86EMUL_OPC (0x0f ,0x01 ):switch ( state->modrm_reg &7 ) {
@@ -11600,7 +11711,7 @@ x86_insn_is_mem_write(const struct x86_eswitch ( state->modrm_reg &7 ) {case 0 :/* FXSAVE */ -case 3 :/* {,V}STMXCSR */ +/* case 3: STMXCSR - handled above */ case 4 :/* XSAVE */ case 6 :/* XSAVEOPT */ return true;
@@ -11616,7 +11727,6 @@ x86_insn_is_mem_write(const struct x86_ecase 1 :/* CMPXCHG{8,16}B */ case 4 :/* XSAVEC */ case 5 :/* XSAVES */ -case 7 :/* VMPTRST */ return true; }break ;
Re: [PATCH v10 1/9] x86emul: address x86_insn_is_mem_{access, write}() omissions
Quoted 26 lines · Jan Beulich
First of all explain in comments what the functions' purposes are. Then make them actually match their comments.
Note that
fc6fa977be54("x86emul: extendx86_insn_is_mem_write()coverage") didn't actually fix the function's behavior for {,V}STMXCSR:
Both are covered by generic code higher up in the function, due tox86_decode_twobyte()already doing suitable adjustments. And VSTMXCSR wouldn't have been covered anyway without a furtherX86EMUL_OPC_VEX()case label. Keep the inner case label in a comment for reference.Signed-off-by Jan Beulich <[email protected]>---
v10: Move ARPL case to earlierswitch()x86_insn_is_mem_write(). Make
group 5 handling actually work there. Drop VMPTRST case. Also handle CLFLUSH*, CLWB, UDn, and remaining PREFETCH* inx86_insn_is_mem_access().
v9: New.xen/arch/x86/x86_emulate/x86_emulate.c +2
-0@@ -11474,25 +11474,87 @@ x86_insn_operand_ea(const struct x86_emureturn state->ea.mem.off; } +/* +* This function means to return 'true' for all supported insns with explicit
xen/arch/x86/x86_emulate/x86_emulate.c x86_insn_operand_ea(const struct x86_emu · line 11488
Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +6
-0+* accesses to memory. This means also insns which don't have an explicit +* memory operand (like POP), but it does not mean e.g. segment selector +* loads, where the descriptor table access is considered an implicit one. +*/ bool x86_insn_is_mem_access (const struct x86_emulate_state *state,const struct x86_emulate_ctxt *ctxt) { +if (mode_64bit () && state->not_64bit ) +return false;
Is this path actually used? state->not_64bit ought to fail instruction decode, at which point we wouldn't have a valid state to be used here.
Everything else looks ok, so Acked-by: Andrew Cooper <[email protected]>
Re: [PATCH v10 1/9] x86emul: address x86_insn_is_mem_{access, write}() omissions
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +0
-0@@ -11474,25 +11474,87 @@ x86_insn_operand_ea(const struct x86_emureturn state->ea.mem.off; }
Quoted 14 lines
xen/arch/x86/x86_emulate/x86_emulate.c x86_insn_operand_ea(const struct x86_emu · line 11488
Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.c +8
-0+/* +* This function means to return 'true' for all supported insns with explicit +* accesses to memory. This means also insns which don't have an explicit +* memory operand (like POP), but it does not mean e.g. segment selector +* loads, where the descriptor table access is considered an implicit one. +*/ bool x86_insn_is_mem_access (const struct x86_emulate_state *state,const struct x86_emulate_ctxt *ctxt) { +if (mode_64bit () && state->not_64bit ) +return false;Is this path actually used?
Yes, it is. It's only x86_emulate() which has
generate_exception_if(state->not_64bit && mode_64bit(), EXC_UD);
right now.
Quoted 2 lines
state->not_64bitought to fail instruction
decode, at which point we wouldn't have a valid state to be used here.
x86_decode() currently doesn't have much raising of #UD at all, I think. If it wasn't like this, the not_64bit field wouldn't be needed - it's used only to communicate from decode to execute. We're not entirely consistent with this though, seeing in x86_decode_onebyte(), a few lines below the block of case labels setting that field,
case 0x9a: /* call (far, absolute) */
case 0xea: /* jmp (far, absolute) */
generate_exception_if(mode_64bit(), EXC_UD);We could certainly drop the field and raise #UD during decode already, but don't you think we then should do so for all encodings that ultimately lead to #UD, e.g. also for AVX insns without AVX available to the guest? This would make x86_decode() quite a bit larger, as it would then also need to have a giant switch() (or something else that's suitable to cover all cases).
Quoted 2 lines
Everything else looks ok, so Acked-by: Andrew Cooper <[email protected]>
Thanks.
Jan
Re: [PATCH v10 1/9] x86emul: address x86_insn_is_mem_{access, write}() omissions
Quoted 21 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +0
-0@@ -11474,25 +11474,87 @@ x86_insn_operand_ea(const struct x86_emureturn state->ea.mem.off; }Quoted 13 lines
xen/arch/x86/x86_emulate/x86_emulate.c x86_insn_operand_ea(const struct x86_emu · line 11488
Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.c +8
-0+/* +* This function means to return 'true' for all supported insns with explicit +* accesses to memory. This means also insns which don't have an explicit +* memory operand (like POP), but it does not mean e.g. segment selector +* loads, where the descriptor table access is considered an implicit one. +*/ bool x86_insn_is_mem_access (const struct x86_emulate_state *state,const struct x86_emulate_ctxt *ctxt) { +if (mode_64bit () && state->not_64bit ) +return false;Is this path actually used?
Quoted 5 lines
Yes, it is. It's only
x86_emulate()which hasgenerate_exception_if(state->not_64bit && mode_64bit(), EXC_UD);right now.
Oh. That is a bit awkward.
Quoted 9 lines
Quoted 2 lines
state->not_64bitought to fail instruction
decode, at which point we wouldn't have a valid state to be used here.
x86_decode()currently doesn't have much raising of #UD at all, I think. If it wasn't like this, thenot_64bitfield wouldn't be needed - it's used only to communicate from decode to execute. We're not entirely consistent with this though, seeing inx86_decode_onebyte(), a few lines below the block of case labels setting that field,
Quoted 3 lines
case 0x9a: /* call (far, absolute) */
case 0xea: /* jmp (far, absolute) */
generate_exception_if(mode_64bit(), EXC_UD);This is because there is no legitimate way to determine the end of the instruction.
Most of the not_64bit instructions do have a well-defined end, even if they aren't permitted for use.
Quoted 7 lines
We could certainly drop the field and raise #UD during decode already, but don't you think we then should do so for all encodings that ultimately lead to #UD, e.g. also for AVX insns without AVX available to the guest? This would make
x86_decode()quite a bit larger, as it would then also need to have a giantswitch()(or something else that's suitable to cover all cases).
I think there is a semantic difference between "we can't parse the instruction", and "we can parse it, but it's not legitimate in this context".
I don't think our exact split is correct, but I don't think moving all #UD checking into x86_decode() is correct either.
~Andrew
Re: [PATCH v10 1/9] x86emul: address x86_insn_is_mem_{access, write}() omissions
Quoted 43 lines · Andrew Cooper
Quoted 21 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Jan Beulich
xen/arch/x86/x86_emulate/x86_emulate.c +0
-0@@ -11474,25 +11474,87 @@ x86_insn_operand_ea(const struct x86_emureturn state->ea.mem.off; }Quoted 13 lines
xen/arch/x86/x86_emulate/x86_emulate.c x86_insn_operand_ea(const struct x86_emu · line 11488
Quoted 12 lines
xen/arch/x86/x86_emulate/x86_emulate.c +8
-0+/* +* This function means to return 'true' for all supported insns with explicit +* accesses to memory. This means also insns which don't have an explicit +* memory operand (like POP), but it does not mean e.g. segment selector +* loads, where the descriptor table access is considered an implicit one. +*/ bool x86_insn_is_mem_access (const struct x86_emulate_state *state,const struct x86_emulate_ctxt *ctxt) { +if (mode_64bit () && state->not_64bit ) +return false;Is this path actually used?
Quoted 5 lines
Yes, it is. It's only
x86_emulate()which hasgenerate_exception_if(state->not_64bit && mode_64bit(), EXC_UD);right now.
Oh. That is a bit awkward.
Quoted 9 lines
Quoted 2 lines
state->not_64bitought to fail instruction
decode, at which point we wouldn't have a valid state to be used here.
x86_decode()currently doesn't have much raising of #UD at all, I think. If it wasn't like this, thenot_64bitfield wouldn't be needed - it's used only to communicate from decode to execute. We're not entirely consistent with this though, seeing inx86_decode_onebyte(), a few lines below the block of case labels setting that field,Quoted 3 lines
case 0x9a: /* call (far, absolute) */ case 0xea: /* jmp (far, absolute) */ generate_exception_if(mode_64bit(), EXC_UD);
Quoted 5 lines
This is because there is no legitimate way to determine the end of the instruction.
Most of the
not_64bitinstructions do have a well-defined end, even if they aren't permitted for use.
I wouldn't bet on that. Just look at the re-use of opcode D6 (salc in 32-bit) by L1OM for an extreme example. There's nothing we can say on how any of the reserved opcodes may get re-used. Prior to AVX / AVX512 we'd have estimated C4, C5, and 62 wrongly as well (but that's true independent of execution mode).
Quoted 8 lines
Quoted 7 lines
We could certainly drop the field and raise #UD during decode already, but don't you think we then should do so for all encodings that ultimately lead to #UD, e.g. also for AVX insns without AVX available to the guest? This would make
x86_decode()quite a bit larger, as it would then also need to have a giantswitch()(or something else that's suitable to cover all cases).
Quoted 6 lines
I think there is a semantic difference between "we can't parse the instruction", and "we can parse it, but it's not legitimate in this context".
I don't think our exact split is correct, but I don't think moving all #UD checking into
x86_decode()is correct either.
Do you have any clear, sufficiently simple rule in mind for where we could draw the boundary?
Jan
PATCH v10 2/9 x86emul: rework CMP and TEST emulation
In reply to Jan BeulichUnlike similarly encoded insns these don't write their memory operands, and hence x86_is_mem_write() should return false for them. However, rather than adding special logic there, rework how their emulation gets done, by making decoding attributes properly describe the r/o nature of their memory operands.
Note how this also allows dropping custom LOCK prefix checks.
---
v10: New.
xen/arch/x86/x86_emulate/x86_emulate.c +23 -14
@@ -84,7 +84,7 @@ static const opcode_desc_t opcode_table[ ByteOp|DstReg|SrcMem|ModRM, DstReg|SrcMem|ModRM, ByteOp|DstEax|SrcImm, DstEax|SrcImm,0 , ImplicitOps,/* 0x38 - 0x3F */ - ByteOp|DstMem|SrcReg|ModRM, DstMem|SrcReg|ModRM, + ByteOp|DstReg|SrcMem|ModRM, DstReg|SrcMem|ModRM, ByteOp|DstReg|SrcMem|ModRM, DstReg|SrcMem|ModRM, ByteOp|DstEax|SrcImm, DstEax|SrcImm,0 , ImplicitOps, /* 0x40 - 0x4F */
@@ -2481,7 +2481,6 @@ x86_decode_onebyte(case 0x60 :/* pusha */ case 0x61 :/* popa */ case 0x62 :/* bound */ -case 0x82 :/* Grp1 (x86/32 only) */ case 0xc4 :/* les */ case 0xc5 :/* lds */ case 0xce :/* into */
@@ -2491,6 +2490,14 @@ x86_decode_onebyte( state->not_64bit = true;break ; +case 0x82 :/* Grp1 (x86/32 only) */ + state->not_64bit = true; +/* fall through */ +case 0x80 :case 0x81 :case 0x83 :/* Grp1 */ +if ( (modrm_reg &7 ) ==7 )/* cmp */ + state->desc = (state->desc & ByteOp) | DstNone | SrcMem; +break ; +case 0x90 :/* nop / pause */ if (repe_prefix () ) ctxt->opcode |=X86EMUL_OPC_F3 (0 ,0 );
@@ -2521,6 +2528,11 @@ x86_decode_onebyte( imm2 =insn_fetch_type (uint8_t);break ; +case 0xf6 :case 0xf7 :/* Grp3 */ +if ( !(modrm_reg &6 ) )/* test */ + state->desc = (state->desc & ByteOp) | DstNone | SrcMem; +break ; +case 0xff :/* Grp5 */ switch ( modrm_reg &7 ) {
@@ -3928,13 +3940,11 @@ x86_emulate(break ;case 0x38 :case 0x39 : cmp:/* cmp reg,mem */ -if ( ops->rmw && dst.type ==OP_MEM && - (rc =read_ulong (dst.mem.seg, dst.mem.off, &dst.val, - dst.bytes, ctxt, ops)) !=X86EMUL_OKAY ) - goto done; -/* fall through */ +emulate_2op_SrcV ("cmp" , dst, src, _regs.eflags); + dst.type =OP_NONE ; +break ; + case 0x3a ... 0x3d: /* cmp */ -generate_exception_if (lock_prefix,EXC_UD );emulate_2op_SrcV ("cmp" , src, dst, _regs.eflags); dst.type =OP_NONE ;break ;
@@ -4239,7 +4249,9 @@ x86_emulate(case 4 : goto and;case 5 : goto sub;case 6 : goto xor; -case 7 : goto cmp; +case 7 : + dst.val = imm1; + goto cmp; }break ;
@@ -5233,11 +5245,8 @@ x86_emulate(unsigned long u[2 ], v; case 0 ... 1: /* test */ -generate_exception_if (lock_prefix,EXC_UD ); -if ( ops->rmw && dst.type ==OP_MEM && - (rc =read_ulong (dst.mem.seg, dst.mem.off, &dst.val, - dst.bytes, ctxt, ops)) !=X86EMUL_OKAY ) - goto done; + dst.val = imm1; + dst.bytes = src.bytes; goto test;case 2 :/* not */ if ( ops->rmw && dst.type ==OP_MEM )
Re: [PATCH v10 2/9] x86emul: rework CMP and TEST emulation
Quoted 1 line · Jan Beulich
Unlike similarly encoded insns these don't write their memory operands,
"write to their".
Quoted 4 lines
and hence
x86_is_mem_write()should return false for them. However, rather than adding special logic there, rework how their emulation gets done, by making decoding attributes properly describe the r/o nature of their memory operands.
Describe how? I see you've change the order of operands encoding, but then override it back?
~Andrew
Quoted 96 lines
Note how this also allows dropping custom LOCK prefix checks.
Signed-off-by Jan Beulich <[email protected]>---
v10: New.xen/arch/x86/x86_emulate/x86_emulate.c +23
-14@@ -84,7 +84,7 @@ static const opcode_desc_t opcode_table[ ByteOp|DstReg|SrcMem|ModRM, DstReg|SrcMem|ModRM, ByteOp|DstEax|SrcImm, DstEax|SrcImm,0 , ImplicitOps,/* 0x38 - 0x3F */ - ByteOp|DstMem|SrcReg|ModRM, DstMem|SrcReg|ModRM, + ByteOp|DstReg|SrcMem|ModRM, DstReg|SrcMem|ModRM, ByteOp|DstReg|SrcMem|ModRM, DstReg|SrcMem|ModRM, ByteOp|DstEax|SrcImm, DstEax|SrcImm,0 , ImplicitOps, /* 0x40 - 0x4F */@@ -2481,7 +2481,6 @@ x86_decode_onebyte(case 0x60 :/* pusha */ case 0x61 :/* popa */ case 0x62 :/* bound */ -case 0x82 :/* Grp1 (x86/32 only) */ case 0xc4 :/* les */ case 0xc5 :/* lds */ case 0xce :/* into */ @@ -2491,6 +2490,14 @@ x86_decode_onebyte( state->not_64bit = true;break ; +case 0x82 :/* Grp1 (x86/32 only) */ + state->not_64bit = true; +/* fall through */ +case 0x80 :case 0x81 :case 0x83 :/* Grp1 */ +if ( (modrm_reg &7 ) ==7 )/* cmp */ + state->desc = (state->desc & ByteOp) | DstNone | SrcMem; +break ; +case 0x90 :/* nop / pause */ if (repe_prefix () ) ctxt->opcode |=X86EMUL_OPC_F3 (0 ,0 );@@ -2521,6 +2528,11 @@ x86_decode_onebyte( imm2 =insn_fetch_type (uint8_t);break ; +case 0xf6 :case 0xf7 :/* Grp3 */ +if ( !(modrm_reg &6 ) )/* test */ + state->desc = (state->desc & ByteOp) | DstNone | SrcMem; +break ; +case 0xff :/* Grp5 */ switch ( modrm_reg &7 ) {@@ -3928,13 +3940,11 @@ x86_emulate(break ;case 0x38 :case 0x39 : cmp:/* cmp reg,mem */ -if ( ops->rmw && dst.type ==OP_MEM && - (rc =read_ulong (dst.mem.seg, dst.mem.off, &dst.val, - dst.bytes, ctxt, ops)) !=X86EMUL_OKAY ) - goto done; -/* fall through */ +emulate_2op_SrcV ("cmp" , dst, src, _regs.eflags); + dst.type =OP_NONE ; +break ; + case 0x3a ... 0x3d: /* cmp */ -generate_exception_if (lock_prefix,EXC_UD );emulate_2op_SrcV ("cmp" , src, dst, _regs.eflags); dst.type =OP_NONE ;break ;@@ -4239,7 +4249,9 @@ x86_emulate(case 4 : goto and;case 5 : goto sub;case 6 : goto xor; -case 7 : goto cmp; +case 7 : + dst.val = imm1; + goto cmp; }break ;@@ -5233,11 +5245,8 @@ x86_emulate(unsigned long u[2 ], v; case 0 ... 1: /* test */ -generate_exception_if (lock_prefix,EXC_UD ); -if ( ops->rmw && dst.type ==OP_MEM && - (rc =read_ulong (dst.mem.seg, dst.mem.off, &dst.val, - dst.bytes, ctxt, ops)) !=X86EMUL_OKAY ) - goto done; + dst.val = imm1; + dst.bytes = src.bytes; goto test;case 2 :/* not */ if ( ops->rmw && dst.type ==OP_MEM )
Re: [PATCH v10 2/9] x86emul: rework CMP and TEST emulation
Quoted 5 lines · Andrew Cooper
Quoted 1 line · Jan Beulich
Unlike similarly encoded insns these don't write their memory operands,
"write to their".
Quoted 7 lines
Quoted 4 lines
and hence
x86_is_mem_write()should return false for them. However, rather than adding special logic there, rework how their emulation gets done, by making decoding attributes properly describe the r/o nature of their memory operands.Describe how? I see you've change the order of operands encoding, but then override it back?
There's no overriding back, I don't think: I change the table entries for opcodes 0x38 and 0x39, with no other adjustments the the attributes later on. For the other opcodes I leave the table entries as they are, and override the attributes for the specific sub-cases (identified by ModRM.reg).
For opcodes 0x38 and 0x39 the change of the table entries implies changing the order of operands as passed to emulate_2op_SrcV(), hence the splitting of the cases in the main switch().
I didn't think this was necessary to spell out in the commit message, but of course I can re-use most of the text above and add it into there, if you think that would help.
Jan
Re: [PATCH v10 2/9] x86emul: rework CMP and TEST emulation
Quoted 17 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
Quoted 1 line · Jan Beulich
Unlike similarly encoded insns these don't write their memory operands,
"write to their".
Quoted 4 lines
and hence
x86_is_mem_write()should return false for them. However, rather than adding special logic there, rework how their emulation gets done, by making decoding attributes properly describe the r/o nature of their memory operands.Describe how? I see you've change the order of operands encoding, but then override it back?
There's no overriding back, I don't think: I change the table entries for opcodes
0x38and0x39, with no other adjustments the the attributes later on. For the other opcodes I leave the table entries as they are, and override the attributes for the specific sub-cases (identified by ModRM.reg).
Quoted 6 lines
For opcodes
0x38and0x39the change of the table entries implies changing the order of operands as passed toemulate_2op_SrcV(), hence the splitting of the cases in the mainswitch().
I didn't think this was necessary to spell out in the commit message, but of course I can re-use most of the text above and add it into there, if you think that would help.
Yes please. With something suitable, Acked-by: Andrew Cooper <[email protected]>
~Andrew
PATCH v10 3/9 x86emul: also test decoding and mem access / write logic
In reply to Jan Beulichx86emul_is_mem_{access,write}() (and their interaction with x86_decode()) have become sufficiently complex that we should have a way to test this logic. Start by covering legacy encoded GPR insns, with the exception of a few the main emulator doesn't support yet (left as comments in the respective tables, or about to be added by subsequent patches). This has already helped spot a few flaws in said logic, addressed by (revised) earlier patches.
---
v10: New.
tools/tests/x86_emulator/Makefile +2 -2
@@ -250,7 +250,7 @@ xop.h avx512f.h: simd-fma.c endif # 32-bit override -$(TARGET): x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o wrappers.o +$(TARGET): x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o $(HOSTCC) $(HOSTCFLAGS) -o $@ $^ .PHONY: clean
@@ -289,7 +289,7 @@ x86.h := $(addprefix $(XEN_ROOT)/tools/i cpuid.h cpuid-autogen.h) x86_emulate.h := x86-emulate.h x86_emulate/x86_emulate.h $(x86.h) -x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o wrappers.o: %.o: %.c $(x86_emulate.h) +x86-emulate.o cpuid.o test_x86_emulator.o evex-disp8.o predicates.o wrappers.o: %.o: %.c $(x86_emulate.h) $(HOSTCC) $(HOSTCFLAGS) -c -g -o $@ $< x86-emulate.o: x86_emulate/x86_emulate.c
tools/tests/x86_emulator/predicates.c new file +671 -0
671 lines not on this page · raw message
tools/tests/x86_emulator/test_x86_emulator.c +2 -0
@@ -4810,6 +4810,8 @@ int main(int argc, char **argv)if ( stack_exec )evex_disp8_test (instr, &ctxt, &emulops); +predicates_test (instr, &ctxt, fetch); +for ( j =0 ; j <ARRAY_SIZE (blobs); j++ ) {if ( blobs[j].check_cpu && !blobs[j].check_cpu () )
tools/tests/x86_emulator/x86-emulate.c +6 -0
@@ -1,7 +1,13 @@#include "x86-emulate.h" +#include <errno.h> #include <sys/mman.h> +#define DEFINE_PER_CPU (type, var) type per_cpu_##var +#define this_cpu (var) per_cpu_##var + +#define ERR_PTR (val) NULL +#define cpu_has_amd_erratum (nr) 0#define cpu_has_mpx false#define read_bndcfgu () 0
tools/tests/x86_emulator/x86-emulate.h +6 -0
@@ -101,6 +101,12 @@ WRAP(puts);void evex_disp8_test (void *instr,struct x86_emulate_ctxt *ctxt,const struct x86_emulate_ops *ops); +void predicates_test (void *instr,struct x86_emulate_ctxt *ctxt, +int (*fetch)(enum x86_segment seg, +unsigned long offset, +void *p_data, +unsigned int bytes, +struct x86_emulate_ctxt *ctxt));static inline uint64_t xgetbv (uint32_t xcr) {
xen/arch/x86/x86_emulate.c +1 -0
@@ -10,6 +10,7 @@*/ #include <xen/domain_page.h> +#include <xen/err.h> #include <xen/event.h> #include <asm/x86_emulate.h> #include <asm/processor.h> /* current_cpu_info */
xen/arch/x86/x86_emulate/x86_emulate.c +3 -9
@@ -11382,10 +11382,6 @@ int x86_emulate_wrapper( }#endif -#ifdef __XEN__ - -#include <xen/err.h> -struct x86_emulate_state * x86_decode_insn( struct x86_emulate_ctxt *ctxt,
@@ -11408,7 +11404,7 @@ x86_decode_insn(if (unlikely (rc !=X86EMUL_OKAY ) )return ERR_PTR (-rc); -#ifndef NDEBUG +#if defined(__XEN__) && !defined(NDEBUG )/* * While we avoid memory allocation (by use of per-CPU data) above, * nevertheless make sure callers properly release the state structure
@@ -11428,12 +11424,12 @@ x86_decode_insn(static inline void check_state (const struct x86_emulate_state *state) { -#ifndef NDEBUG +#if defined(__XEN__) && !defined(NDEBUG )ASSERT (state->caller);#endif } -#ifndef NDEBUG +#if defined(__XEN__) && !defined(NDEBUG )void x86_emulate_free_state (struct x86_emulate_state *state) {check_state (state);
@@ -11806,5 +11802,3 @@ x86_insn_length(const struct x86_emulatereturn state->ip - ctxt->regs->r (ip); } - -#endif
xen/arch/x86/x86_emulate/x86_emulate.h +3 -3
@@ -730,8 +730,6 @@ x86emul_unhandleable_rw(unsigned int bytes, struct x86_emulate_ctxt *ctxt); -#ifdef __XEN__ -struct x86_emulate_state * x86_decode_insn( struct x86_emulate_ctxt *ctxt,
@@ -767,12 +765,14 @@ bool x86_insn_is_cr_access(const struct x86_emulate_state *state, const struct x86_emulate_ctxt *ctxt); -#ifdef NDEBUG +#if !defined(__XEN__) || defined(NDEBUG )static inline void x86_emulate_free_state (struct x86_emulate_state *state) {}#else void x86_emulate_free_state (struct x86_emulate_state *state);#endif +#ifdef __XEN__ +int x86emul_read_xcr (unsigned int reg,uint64_t *val,struct x86_emulate_ctxt *ctxt); int x86emul_write_xcr(unsigned int reg, uint64_t val,
Re: [PATCH v10 3/9] x86emul: also test decoding and mem access / write logic
Quoted 9 lines · Jan Beulich
x86emul_is_mem_{access,write}() (and their interaction with
x86_decode()) have become sufficiently complex that we should have a way to test this logic. Start by covering legacy encoded GPR insns, with the exception of a few the main emulator doesn't support yet (left as comments in the respective tables, or about to be added by subsequent patches). This has already helped spot a few flaws in said logic, addressed by (revised) earlier patches.Signed-off-by Jan Beulich <[email protected]>
Extra testing is always good.
PATCH v10 4/9 x86emul: disable FPU/MMX/SIMD insn emulation when !HVM
In reply to Jan BeulichIn a pure PV environment (the PV shim in particular) we don't really need emulation of all these. To limit #ifdef-ary utilize some of the CASE_*() macros we have, by providing variants expanding to (effectively) nothing (really a label, which in turn requires passing -Wno-unused-label to the compiler when build such configurations).
Due to the mixture of macro and #ifdef use, the placement of some of the #ifdef-s is a little arbitrary.
The resulting object file's .text is less than half the size of the original, and looks to also be compiling a little more quickly.
This is meant as a first step; more parts can likely be disabled down the road.
---
v7: Integrate into this series. Re-base.
---
I'll be happy to take suggestions allowing to avoid -Wno-unused-label.
xen/arch/x86/Makefile +3 -0
@@ -73,6 +73,9 @@ obj-y += vm_event.o obj-y += xstate.o extra-y += asm-macros.i +ifneq ($(CONFIG_HVM),y) +x86_emulate.o: CFLAGS-y += -Wno-unused-label +endif x86_emulate.o: x86_emulate/x86_emulate.c x86_emulate/x86_emulate.h efi-y := $(shell if [ ! -r $(BASEDIR)/include/xen/compile.h -o \
xen/arch/x86/x86_emulate.c +6 -0
@@ -43,6 +43,12 @@ } \ }) +#ifndef CONFIG_HVM +# define X86EMUL_NO_FPU +# define X86EMUL_NO_MMX +# define X86EMUL_NO_SIMD +#endif +#include "x86_emulate/x86_emulate.c" int x86emul_read_xcr(unsigned int reg, uint64_t *val,
xen/arch/x86/x86_emulate/x86_emulate.c +172 -10
623 lines not on this page · raw message
Re: [PATCH v10 4/9] x86emul: disable FPU/MMX/SIMD insn emulation when !HVM
Quoted 15 lines · Jan Beulich
In a pure PV environment (the PV shim in particular) we don't really need emulation of all these. To limit #ifdef-ary utilize some of the CASE_*() macros we have, by providing variants expanding to (effectively) nothing (really a label, which in turn requires passing -Wno-unused-label to the compiler when build such configurations).
Due to the mixture of macro and #ifdef use, the placement of some of the #ifdef-s is a little arbitrary.
The resulting object file's .text is less than half the size of the original, and looks to also be compiling a little more quickly.
This is meant as a first step; more parts can likely be disabled down the road.
Quoted 6 lines
Suggested-by Andrew Cooper <[email protected]>Signed-off-by Jan Beulich <[email protected]>---
v7: Integrate into this series. Re-base.
---
I'll be happy to take suggestions allowing to avoid -Wno-unused-label.
I really would prefer the simplified version, which doesn't need -Wno-unused-label at all.
I specifically don't see a need for these to be selected individually, and a consequence of that is a vastly simplified patch.
However, to avoid this stalemate, Bregrudingly-acked-by: Andrew Cooper <[email protected]> if you still insist on taking this route.
PATCH v10 5/9 x86emul: support MOVDIR{I,64B} insns
In reply to Jan BeulichIntroduce a new blk() hook, paralleling the rmw() one in a certain way, but being intended for larger data sizes, and hence its HVM intermediate handling function doesn't fall back to splitting the operation if the requested virtual address can't be mapped.
Note that SDM revision 071 doesn't specify exception behavior for ModRM.mod == 0b11; assuming #UD here.
---
v10: Re-base over changes earlier in the series as well as later parts
comitted already.
v9: Fold in "x86/HVM: make hvmemul_blk() capable of handling r/o
operations". Also adjust x86_insn_is_mem_write().
v7: Add blk_NONE. Move harness'es setting of .blk. Correct indentation.
Re-base.
v6: Fold MOVDIRI and MOVDIR64B changes again. Use blk() for both. All
tags dropped.
v5: Introduce/use ->blk() hook. Correct asm() operands.
v4: Split MOVDIRI and MOVDIR64B and move this one ahead. Re-base.
v3: Update description.
---
(SDE: -tnt)
tools/tests/x86_emulator/predicates.c +2 -0
@@ -510,6 +510,8 @@ static const struct {/*{ 0xf6 }, { 2, 2 }, F, W }, wrss */ { { 0xf6 }, { 2, 2 }, T, R, pfx_66 }, /* adcx */ { { 0xf6 }, { 2, 2 }, T, R, pfx_f3 }, /* adox */ + { { 0xf8 }, { 2, 2 }, F, W, pfx_66 }, /* movdir64b */ + { { 0xf9 }, { 2, 2 }, F, W }, /* movdiri */ }; #undef CND #undef REG
tools/tests/x86_emulator/test_x86_emulator.c +57 -0
75 lines not on this page · raw message
tools/tests/x86_emulator/x86-emulate.h +2 -0
@@ -160,6 +160,8 @@ static inline bool xcr0_mask(uint64_t ma#define cpu_has_avx512_vnni (cp.feat.avx512_vnni && xcr0_mask(0xe6))#define cpu_has_avx512_bitalg (cp.feat.avx512_bitalg && xcr0_mask(0xe6))#define cpu_has_avx512_vpopcntdq (cp.feat.avx512_vpopcntdq && xcr0_mask(0xe6)) +#define cpu_has_movdiri cp.feat.movdiri +#define cpu_has_movdir64b cp.feat.movdir64b#define cpu_has_avx512_4vnniw (cp.feat.avx512_4vnniw && xcr0_mask(0xe6))#define cpu_has_avx512_4fmaps (cp.feat.avx512_4fmaps && xcr0_mask(0xe6))#define cpu_has_serialize cp.feat.serialize
xen/arch/x86/arch.mk +1 -0
@@ -47,6 +47,7 @@ $(call as-option-add,CFLAGS,CC,"rdseed % $(call as-option-add,CFLAGS,CC,"clwb (%rax)",-DHAVE_AS_CLWB) $(call as-option-add,CFLAGS,CC,".equ \"x\"$$(comma)1",-DHAVE_AS_QUOTED_SYM) $(call as-option-add,CFLAGS,CC,"invpcid (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID) +$(call as-option-add,CFLAGS,CC,"movdiri %rax$$(comma)(%rax)",-DHAVE_AS_MOVDIR) # GAS's idea of true is -1. Clang's idea is 1 $(call as-option-add,CFLAGS,CC,\
xen/arch/x86/hvm/emulate.c +42 -0
54 lines not on this page · raw message
xen/arch/x86/x86_emulate/x86_emulate.c +113 -1
159 lines not on this page · raw message
xen/arch/x86/x86_emulate/x86_emulate.h +24 -0
@@ -310,6 +310,22 @@ struct x86_emulate_ops struct x86_emulate_ctxt *ctxt);/* +* blk: Emulate a large (block) memory access. +* @p_data: [IN/OUT] (optional) Pointer to source/destination buffer. +* @eflags: [IN/OUT] Pointer to EFLAGS to be updated according to +* instruction effects. +* @state: [IN/OUT] Pointer to (opaque) emulator state. +*/ +int (*blk)( +enum x86_segment seg, +unsigned long offset, +void *p_data, +unsigned int bytes, +uint32_t *eflags, +struct x86_emulate_state *state, +struct x86_emulate_ctxt *ctxt); + +/* * validate: Post-decode, pre-emulate hook to allow caller controlled * filtering. */
@@ -793,6 +809,14 @@ x86_emul_rmw(unsigned int bytes, uint32_t *eflags, struct x86_emulate_state *state, + struct x86_emulate_ctxt *ctxt); +int +x86_emul_blk ( +void *ptr, +void *data, +unsigned int bytes, +uint32_t *eflags, +struct x86_emulate_state *state,struct x86_emulate_ctxt *ctxt); static inline void x86_emul_hw_exception(
xen/include/asm-x86/cpufeature.h +2 -0
@@ -118,6 +118,8 @@#define cpu_has_avx512_bitalg boot_cpu_has(X86_FEATURE_AVX512_BITALG)#define cpu_has_avx512_vpopcntdq boot_cpu_has(X86_FEATURE_AVX512_VPOPCNTDQ)#define cpu_has_rdpid boot_cpu_has(X86_FEATURE_RDPID) +#define cpu_has_movdiri boot_cpu_has(X86_FEATURE_MOVDIRI) +#define cpu_has_movdir64b boot_cpu_has(X86_FEATURE_MOVDIR64B)/* CPUID level 0x80000007.edx */ #define cpu_has_itsc boot_cpu_has(X86_FEATURE_ITSC)
xen/include/public/arch-x86/cpufeatureset.h +2 -0
@@ -241,6 +241,8 @@ XEN_CPUFEATURE(AVX512_VPOPCNTDQ, 6*32+14 XEN_CPUFEATURE(TSXLDTRK, 6*32+16) /*a TSX load tracking suspend/resume insns */ XEN_CPUFEATURE(RDPID, 6*32+22) /*A RDPID instruction */ XEN_CPUFEATURE(CLDEMOTE, 6*32+25) /*A CLDEMOTE instruction */ +XEN_CPUFEATURE(MOVDIRI, 6*32+27) /*A MOVDIRI instruction */ +XEN_CPUFEATURE (MOVDIR64B ,6 *32 +28 )/*A MOVDIR64B instruction */ /* AMD-defined CPU features, CPUID level 0x80000007.edx, word 7 */XEN_CPUFEATURE (ITSC ,7 *32 +8 )/* Invariant TSC */
Re: [PATCH v10 5/9] x86emul: support MOVDIR{I,64B} insns
Quoted 7 lines · Jan Beulich
Introduce a new
blk()hook, paralleling thermw()one in a certain way, but being intended for larger data sizes, and hence its HVM intermediate handling function doesn't fall back to splitting the operation if the requested virtual address can't be mapped.Note that SDM revision 071 doesn't specify exception behavior for ModRM.mod == 0b11; assuming #UD here.
Once again - I don't think this wants calling out. That encoding space will be used for a new Grp at some point in the future, and be a different instruction.
Quoted 3 lines
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Paul Durrant <[email protected]>
recommendation...
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(AVX512_VPOPCNTDQ, 6*32+14 · line 245
Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +2
-0@@ -241,6 +241,8 @@ XEN_CPUFEATURE(AVX512_VPOPCNTDQ, 6*32+14 XEN_CPUFEATURE(TSXLDTRK, 6*32+16) /*a TSX load tracking suspend/resume insns */ XEN_CPUFEATURE(RDPID, 6*32+22) /*A RDPID instruction */ XEN_CPUFEATURE(CLDEMOTE, 6*32+25) /*A CLDEMOTE instruction */ +XEN_CPUFEATURE(MOVDIRI, 6*32+27) /*A MOVDIRI instruction */ +XEN_CPUFEATURE (MOVDIR64B ,6 *32 +28 )/*A MOVDIR64B instruction */
I'd be tempted to leave these as 'a' for now, seeing as we have the ability.
These instructions aren't actually of any use for domains without PCI devices, and a "default" will be more migrateable as a consequence.
We're going to need further toolstack changes to make CXL-passthrough viable, so instruction adjustments can be part of that work.
~Andrew
Re: [PATCH v10 5/9] x86emul: support MOVDIR{I,64B} insns
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
Introduce a new
blk()hook, paralleling thermw()one in a certain way, but being intended for larger data sizes, and hence its HVM intermediate handling function doesn't fall back to splitting the operation if the requested virtual address can't be mapped.
Quoted 6 lines
Quoted 2 lines
Note that SDM revision 071 doesn't specify exception behavior for ModRM.mod == 0b11; assuming #UD here.
Once again - I don't think this wants calling out. That encoding space will be used for a new Grp at some point in the future, and be a different instruction.
Possible; without it spelled out one may also think (at least for MOVDIRI) that the register-only encoding could be a re-encoding of plain MOV. I'd prefer to keep it.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Jan Beulich <[email protected]>Reviewed-by Paul Durrant <[email protected]>recommendation...
Thanks and ...
Quoted 11 lines
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(AVX512_VPOPCNTDQ, 6*32+14 · line 245
Quoted 8 lines
xen/include/public/arch-x86/cpufeatureset.h +2
-0@@ -241,6 +241,8 @@ XEN_CPUFEATURE(AVX512_VPOPCNTDQ, 6*32+14 XEN_CPUFEATURE(TSXLDTRK, 6*32+16) /*a TSX load tracking suspend/resume insns */ XEN_CPUFEATURE(RDPID, 6*32+22) /*A RDPID instruction */ XEN_CPUFEATURE(CLDEMOTE, 6*32+25) /*A CLDEMOTE instruction */ +XEN_CPUFEATURE(MOVDIRI, 6*32+27) /*A MOVDIRI instruction */ +XEN_CPUFEATURE (MOVDIR64B ,6 *32 +28 )/*A MOVDIR64B instruction */ I'd be tempted to leave these as 'a' for now, seeing as we have the ability.
Quoted 2 lines
These instructions aren't actually of any use for domains without PCI devices, and a "default" will be more migrateable as a consequence.
... okay, done.
Jan
PATCH v10 6/9 x86emul: support ENQCMD insns
In reply to Jan BeulichNote that the ISA extensions document revision 038 doesn't specify exception behavior for ModRM.mod == 0b11; assuming #UD here.
No tests are being added to the harness - this would be quite hard, we can't just issue the insns against RAM. Their similarity with MOVDIR64B should have the test case there be god enough to cover any fundamental flaws.
---
TBD: This doesn't (can't) consult PASID translation tables yet, as we
have no VMX code for this so far. I guess for this we will want to
replace the direct ->read_msr(MSR_PASID, ...) with a new
->read_pasid() hook.---
v10: Re-base over changes earlier in the series as well as later parts
comitted already.
v9: Consistently use named asm() operands. Also adjust
x86_insn_is_mem_write(). A -> a in public header. Move asm-x86/msr-index.h addition and drop _IA32 from their names. Introduce _AC() into the emulator harness as a result.
v7: Re-base.
v6: Re-base.
v5: New.
tools/tests/x86_emulator/predicates.c +2 -0
@@ -511,6 +511,8 @@ static const struct { { { 0xf6 }, { 2, 2 }, T, R, pfx_66 }, /* adcx */ { { 0xf6 }, { 2, 2 }, T, R, pfx_f3 }, /* adox */ { { 0xf8 }, { 2, 2 }, F, W, pfx_66 }, /* movdir64b */ + { { 0xf8 }, { 2, 2 }, F, W, pfx_f3 }, /* enqcmds */ + { { 0xf8 }, { 2, 2 }, F, W, pfx_f2 }, /* enqcmd */ { { 0xf9 }, { 2, 2 }, F, W }, /* movdiri */ }; #undef CND
tools/tests/x86_emulator/x86-emulate.h +3 -0
@@ -59,6 +59,9 @@ (type *)((char *)mptr__ - offsetof(type , member)); \ }) +#define AC_ (n,t) (n##t) +#define _AC (n,t) AC_(n,t) +#define hweight32 __builtin_popcount#define hweight64 __builtin_popcountll
xen/arch/x86/arch.mk +1 -0
@@ -48,6 +48,7 @@ $(call as-option-add,CFLAGS,CC,"clwb (%r $(call as-option-add,CFLAGS,CC,".equ \"x\"$$(comma)1",-DHAVE_AS_QUOTED_SYM) $(call as-option-add,CFLAGS,CC,"invpcid (%rax)$$(comma)%rax",-DHAVE_AS_INVPCID) $(call as-option-add,CFLAGS,CC,"movdiri %rax$$(comma)(%rax)",-DHAVE_AS_MOVDIR) +$(call as-option-add,CFLAGS,CC,"enqcmd (%rax)$$(comma)%rax",-DHAVE_AS_ENQCMD) # GAS's idea of true is -1. Clang's idea is 1 $(call as-option-add,CFLAGS,CC,\
xen/arch/x86/x86_emulate/x86_emulate.c +60 -0
101 lines not on this page · raw message
xen/include/asm-x86/cpufeature.h +1 -0
@@ -120,6 +120,7 @@#define cpu_has_rdpid boot_cpu_has(X86_FEATURE_RDPID)#define cpu_has_movdiri boot_cpu_has(X86_FEATURE_MOVDIRI)#define cpu_has_movdir64b boot_cpu_has(X86_FEATURE_MOVDIR64B) +#define cpu_has_enqcmd boot_cpu_has(X86_FEATURE_ENQCMD)/* CPUID level 0x80000007.edx */ #define cpu_has_itsc boot_cpu_has(X86_FEATURE_ITSC)
xen/include/asm-x86/msr-index.h +4 -0
@@ -74,6 +74,10 @@#define MSR_PL3_SSP 0x000006a7#define MSR_INTERRUPT_SSP_TABLE 0x000006a8 +#define MSR_PASID 0x00000d93 +#define PASID_PASID_MASK 0x000fffff +#define PASID_VALID (_AC(1, ULL) << 31) +/* * Legacy MSR constants in need of cleanup. No new MSRs below this comment. */
xen/include/public/arch-x86/cpufeatureset.h +1 -0
@@ -243,6 +243,7 @@ XEN_CPUFEATURE(RDPID, 6*32+22) / XEN_CPUFEATURE(CLDEMOTE, 6*32+25) /*A CLDEMOTE instruction */ XEN_CPUFEATURE(MOVDIRI, 6*32+27) /*A MOVDIRI instruction */ XEN_CPUFEATURE(MOVDIR64B, 6*32+28) /*A MOVDIR64B instruction */ +XEN_CPUFEATURE (ENQCMD ,6 *32 +29 )/* ENQCMD{,S} instructions */ /* AMD-defined CPU features, CPUID level 0x80000007.edx, word 7 */XEN_CPUFEATURE (ITSC ,7 *32 +8 )/* Invariant TSC */
PATCH v10 7/9 x86emul: support FNSTENV and FNSAVE
In reply to Jan BeulichTo avoid introducing another boolean into emulator state, the rex_prefix field gets (ab)used to convey the real/VM86 vs protected mode info (affecting structure layout, albeit not size) to x86_emul_blk().
---
TBD: The full 16-bit padding fields in the 32-bit structures get filled
with all ones by modern CPUs (i.e. unlike what the comment says for FIP and FDP). We may want to mirror this as well (for the real mode variant), even if those fields' contents are unspecified.
---
v9: Fix !HVM build. Add /*state->*/ comments. Add memset(). Add/extend
comments.
v7: New.
tools/tests/x86_emulator/x86-emulate.h +1 -0
@@ -129,6 +129,7 @@ static inline bool xcr0_mask(uint64_t ma }#define cache_line_size () (cp.basic.clflush_size * 8) +#define cpu_has_fpu cp.basic.fpu#define cpu_has_mmx cp.basic.mmx#define cpu_has_fxsr cp.basic.fxsr#define cpu_has_sse cp.basic.sse
tools/tests/x86_emulator/test_x86_emulator.c +75 -0
87 lines not on this page · raw message
xen/arch/x86/x86_emulate/x86_emulate.c +178 -3
220 lines not on this page · raw message
Re: [PATCH v10 7/9] x86emul: support FNSTENV and FNSAVE
Quoted 5 lines · Jan Beulich
To avoid introducing another boolean into emulator state, the
rex_prefixfield gets (ab)used to convey the real/VM86 vs protected mode info (affecting structure layout, albeit not size) tox86_emul_blk().Signed-off-by Jan Beulich <[email protected]>
Quoted 31 lines
xen/arch/x86/x86_emulate/x86_emulate.c +18
-0@@ -856,6 +856,9 @@ struct x86_emulate_state {enum { blk_NONE, blk_enqcmd, +#ifndef X86EMUL_NO_FPU + blk_fst,/* FNSTENV, FNSAVE */ +#endif blk_movdir, } blk;uint8_t modrm, modrm_mod, modrm_reg, modrm_rm;@@ -897,6 +900,50 @@ struct x86_emulate_state {#define PTR_POISON NULL/* 32-bit builds are for user-space, so NULL is OK. */ #endif +#ifndef X86EMUL_NO_FPU +struct x87_env16 { + uint16_t fcw; + uint16_t fsw; + uint16_t ftw; + union { + struct { + uint16_t fip_lo; + uint16_t fop:11, :1, fip_hi:4; + uint16_t fdp_lo; + uint16_t :12, fdp_hi:4; + } real; + struct { + uint16_t fip; + uint16_t fcs;
xen/arch/x86/x86_emulate/x86_emulate.c struct x86_emulate_state { · line 927
Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +10
-0+uint16_t fdp; +uint16_t fds; + } prot; + } mode; +}; + +struct x87_env32 { + uint32_t fcw:16, :16; + uint32_t fsw:16, :16; + uint32_t ftw:16, :16;
uint16_t fcw, :16;uint16_t fsw, :16;uint16_t ftw, :16;
?
~Andrew
Re: [PATCH v10 7/9] x86emul: support FNSTENV and FNSAVE
Quoted 8 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich
To avoid introducing another boolean into emulator state, the
rex_prefixfield gets (ab)used to convey the real/VM86 vs protected mode info (affecting structure layout, albeit not size) tox86_emul_blk().Signed-off-by Jan Beulich <[email protected]>Acked-by Andrew Cooper <[email protected]>,
Thanks.
Quoted 48 lines
with one suggestion.
Quoted 31 lines
xen/arch/x86/x86_emulate/x86_emulate.c +18
-0@@ -856,6 +856,9 @@ struct x86_emulate_state {enum { blk_NONE, blk_enqcmd, +#ifndef X86EMUL_NO_FPU + blk_fst,/* FNSTENV, FNSAVE */ +#endif blk_movdir, } blk;uint8_t modrm, modrm_mod, modrm_reg, modrm_rm;@@ -897,6 +900,50 @@ struct x86_emulate_state {#define PTR_POISON NULL/* 32-bit builds are for user-space, so NULL is OK. */ #endif +#ifndef X86EMUL_NO_FPU +struct x87_env16 { + uint16_t fcw; + uint16_t fsw; + uint16_t ftw; + union { + struct { + uint16_t fip_lo; + uint16_t fop:11, :1, fip_hi:4; + uint16_t fdp_lo; + uint16_t :12, fdp_hi:4; + } real; + struct { + uint16_t fip; + uint16_t fcs;xen/arch/x86/x86_emulate/x86_emulate.c struct x86_emulate_state { · line 927
Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +10
-0+uint16_t fdp; +uint16_t fds; + } prot; + } mode; +}; + +struct x87_env32 { + uint32_t fcw:16, :16; + uint32_t fsw:16, :16; + uint32_t ftw:16, :16;
uint16_tfcw, :16;uint16_tfsw, :16;uint16_tftw, :16;
Quoted 1 line
?
You had suggested this before, and I did reply that my intention was to have x87_env16 use uint16_t throughout, and x87_env32 uint32_t respectively for all its pieces. In the end it doesn't make a difference, and hence this cosmetic aspect is what made me pick one of the various possible options.
Jan
PATCH v10 8/9 x86emul: support FLDENV and FRSTOR
In reply to Jan BeulichWhile the Intel SDM claims that FRSTOR itself may raise #MF upon completion, this was confirmed by Intel to be a doc error which will be corrected in due course; behavior is like FLDENV, and like old hard copy manuals describe it.
Re-arrange a switch() statement's case label order to allow for fall-through from FLDENV handling to FNSTENV's.
---
v9: Refine description. Re-base over changes to earlier patch. Add
comments.
v7: New.
tools/tests/x86_emulator/test_x86_emulator.c +46 -0
@@ -2442,6 +2442,27 @@ int main(int argc, char **argv)else printf ("skipped\n" ); +printf ("%-40s" ,"Testing fldenv 8(%edx)..." ); +if ( stack_exec && cpu_has_fpu ) + { + asmvolatile ("fnstenv %0\n\t" +"fninit" + :"=m" (res[2 ]) ::"memory" ); +zap_fpsel (&res[2 ], true); + instr[0 ] =0xd9 ; instr[1 ] =0x62 ; instr[2 ] =0x08 ; + regs.eip = (unsigned long )&instr[0 ]; + regs.edx = (unsigned long )res; + rc =x86_emulate (&ctxt, &emulops); + asmvolatile ("fnstenv %0" :"=m" (res[9 ]) ::"memory" ); +if ( (rc !=X86EMUL_OKAY ) || +memcmp (res +2 , res +9 ,28 ) || + (regs.eip != (unsigned long )&instr[3 ]) ) + goto fail; +printf ("okay\n" ); + } +else +printf ("skipped\n" ); +printf ("%-40s" ,"Testing 16-bit fnsave (%ecx)..." );if ( stack_exec && cpu_has_fpu ) {
@@ -2468,6 +2489,31 @@ int main(int argc, char **argv) goto fail;printf ("okay\n" ); } +else +printf ("skipped\n" ); + +printf ("%-40s" ,"Testing frstor (%edx)..." ); +if ( stack_exec && cpu_has_fpu ) + { +const uint16_t seven =7 ; + + asmvolatile ("fninit\n\t" +"fld1\n\t" +"fidivs %1\n\t" +"fnsave %0\n\t" + :"=&m" (res[0 ]) :"m" (seven) :"memory" ); +zap_fpsel (&res[0 ], true); + instr[0 ] =0xdd ; instr[1 ] =0x22 ; + regs.eip = (unsigned long )&instr[0 ]; + regs.edx = (unsigned long )res; + rc =x86_emulate (&ctxt, &emulops); + asmvolatile ("fnsave %0" :"=m" (res[27 ]) ::"memory" ); +if ( (rc !=X86EMUL_OKAY ) || +memcmp (res, res +27 ,108 ) || + (regs.eip != (unsigned long )&instr[2 ]) ) + goto fail; +printf ("okay\n" ); + }else printf ("skipped\n" );
xen/arch/x86/x86_emulate/x86_emulate.c +107 -18
161 lines not on this page · raw message
Re: [PATCH v10 8/9] x86emul: support FLDENV and FRSTOR
Quoted 9 lines · Jan Beulich
While the Intel SDM claims that FRSTOR itself may raise #MF upon completion, this was confirmed by Intel to be a doc error which will be corrected in due course; behavior is like FLDENV, and like old hard copy manuals describe it.
Re-arrange a
switch()statement's case label order to allow for fall-through from FLDENV handling to FNSTENV's.Signed-off-by Jan Beulich <[email protected]>
PATCH v10 9/9 x86emul: support FXSAVE/FXRSTOR
In reply to Jan BeulichNote that FPU selector handling as well as MXCSR mask saving for now does not honor differences between host and guest visible featuresets.
While for Intel operation of the insns with CR4.OSFXSR=0 is implementation dependent, use the easiest solution there: Simply don't look at the bit in the first place. For AMD and alike the behavior is well defined, so it gets handled together with FFXSR.
---
v9: Change a few field types in struct x86_fxsr. Leave reserved fields
either entirely unnamed, or named "rsvd". Set state->fpu_ctrl. Avoid memory clobbers. Add memset() to FXSAVE logic. Add comments.
v8: Respect EFER.FFXSE and CR4.OSFXSR. Correct wrong X86EMUL_NO_*
dependencies. Reduce #ifdef-ary.
v7: New.
tools/tests/x86_emulator/test_x86_emulator.c +91 -0
@@ -767,6 +767,12 @@ static void zap_fpsel(unsigned int *env, } } +static void zap_xfpsel (unsigned int *env) +{ + env[3 ] &= ~0xffff ; + env[5 ] &= ~0xffff ; +} + #ifdef __x86_64__ # define STKVAL_DISP 64 static const struct {
@@ -2517,6 +2523,91 @@ int main(int argc, char **argv)else printf ("skipped\n" ); +printf ("%-40s" ,"Testing fxsave 4(%ecx)..." ); +if ( stack_exec && cpu_has_fxsr ) + { +const uint16_t nine =9 ; + +memset (res +0x80 ,0xcc ,0x400 ); +if ( cpu_has_sse2 ) + asmvolatile ("pcmpeqd %xmm7, %xmm7\n\t" +"pxor %xmm6, %xmm6\n\t" +"psubw %xmm7, %xmm6" ); + asmvolatile ("fninit\n\t" +"fld1\n\t" +"fidivs %1\n\t" +"fxsave %0" + :"=m" (res[0x100 ]) :"m" (nine) :"memory" ); +zap_xfpsel (&res[0x100 ]); + instr[0 ] =0x0f ; instr[1 ] =0xae ; instr[2 ] =0x41 ; instr[3 ] =0x04 ; + regs.eip = (unsigned long )&instr[0 ]; + regs.ecx = (unsigned long )(res +0x7f ); +memset (res +0x100 +0x74 ,0x33 ,0x30 ); +memset (res +0x80 +0x74 ,0x33 ,0x30 ); + rc =x86_emulate (&ctxt, &emulops); +zap_xfpsel (&res[0x80 ]); +if ( (rc !=X86EMUL_OKAY ) || +memcmp (res +0x80 , res +0x100 ,0x200 ) || + (regs.eip != (unsigned long )&instr[4 ]) ) + goto fail; +printf ("okay\n" ); + } +else +printf ("skipped\n" ); + +printf ("%-40s" ,"Testing fxrstor -4(%ecx)..." ); +if ( stack_exec && cpu_has_fxsr ) + { +const uint16_t eleven =11 ; + +memset (res +0x80 ,0xcc ,0x400 ); + asmvolatile ("fxsave %0" :"=m" (res[0x80 ]) ::"memory" ); +zap_xfpsel (&res[0x80 ]); +if ( cpu_has_sse2 ) + asmvolatile ("pxor %xmm7, %xmm6\n\t" +"pxor %xmm7, %xmm3\n\t" +"pxor %xmm7, %xmm0\n\t" +"pxor %xmm7, %xmm7" ); + asmvolatile ("fninit\n\t" +"fld1\n\t" +"fidivs %0\n\t" + ::"m" (eleven) ); + instr[0 ] =0x0f ; instr[1 ] =0xae ; instr[2 ] =0x49 ; instr[3 ] =0xfc ; + regs.eip = (unsigned long )&instr[0 ]; + regs.ecx = (unsigned long )(res +0x81 ); + rc =x86_emulate (&ctxt, &emulops); + asmvolatile ("fxsave %0" :"=m" (res[0x100 ]) ::"memory" ); +if ( (rc !=X86EMUL_OKAY ) || +memcmp (res +0x100 , res +0x80 ,0x200 ) || + (regs.eip != (unsigned long )&instr[4 ]) ) + goto fail; +printf ("okay\n" ); + } +else +printf ("skipped\n" ); + +#ifdef __x86_64__ +printf ("%-40s" ,"Testing fxsaveq 8(%edx)..." ); +if ( stack_exec && cpu_has_fxsr ) + { +memset (res +0x80 ,0xcc ,0x400 ); + asmvolatile ("fxsaveq %0" :"=m" (res[0x100 ]) ::"memory" ); + instr[0 ] =0x48 ; instr[1 ] =0x0f ; instr[2 ] =0xae ; instr[3 ] =0x42 ; instr[4 ] =0x08 ; + regs.eip = (unsigned long )&instr[0 ]; + regs.edx = (unsigned long )(res +0x7e ); +memset (res +0x100 +0x74 ,0x33 ,0x30 ); +memset (res +0x80 +0x74 ,0x33 ,0x30 ); + rc =x86_emulate (&ctxt, &emulops); +if ( (rc !=X86EMUL_OKAY ) || +memcmp (res +0x80 , res +0x100 ,0x200 ) || + (regs.eip != (unsigned long )&instr[5 ]) ) + goto fail; +printf ("okay\n" ); + } +else +printf ("skipped\n" ); +#endif +printf ("%-40s" ,"Testing movq %mm3,(%ecx)..." );if ( stack_exec && cpu_has_mmx ) {
tools/tests/x86_emulator/x86-emulate.c +7 -0
@@ -36,6 +36,13 @@ struct cpuid_policy cp; static char fpu_save_area[4096] __attribute__((__aligned__((64))));static bool use_xsave; +/* +* Re-use the area above also as scratch space for the emulator itself. +* (When debugging the emulator, care needs to be taken when inserting +* printf() or alike function calls into regions using this.) +*/ +#define FXSAVE_AREA ((struct x86_fxsr *)fpu_save_area) +void emul_save_fpu_state (void ) {if ( use_xsave )
xen/arch/x86/x86_emulate/x86_emulate.c +156 -1
199 lines not on this page · raw message
xen/arch/x86/x86_emulate.c +2 -0
@@ -43,6 +43,8 @@ } \ }) +#define FXSAVE_AREA current->arch.fpu_ctxt +#ifndef CONFIG_HVM # define X86EMUL_NO_FPU # define X86EMUL_NO_MMX
Re: [PATCH v10 9/9] x86emul: support FXSAVE/FXRSTOR
Quoted 9 lines · Jan Beulich
Note that FPU selector handling as well as MXCSR mask saving for now does not honor differences between host and guest visible featuresets.
While for Intel operation of the insns with CR4.OSFXSR=0 is implementation dependent, use the easiest solution there: Simply don't look at the bit in the first place. For AMD and alike the behavior is well defined, so it gets handled together with FFXSR.
Signed-off-by Jan Beulich <[email protected]>