x86/vvmx: Read instruction operands correctly on VM exit
cover x86/vvmx: Read instruction operands correctly on VM exit
decode_vmx_inst() does not read instruction operands correctly on VM exit:
* It incorrectly uses vmx_inst_info's address_size field to calculate the sizes of the exit-causing instruction's operands. The sizes of the operands are specified in the SDM and might depend on whether the guest is running in 32-bit or 64-bit mode, but they have nothing to do with the address_size field.
* It includes its own segmentation logic, duplicating code elsewhere. This segmentation logic is also incorrect and will raise #GP fault rather than a #SS fault in response to an invalid memory access through the stack segment.
Patches 1-6 (up to 'Remove operand decoding from decode_vmx_inst()') refactor decode_vmx_inst() in preparation for fixing the bugs mentioned above. They remove unnecessary code and extract the logic for reading operands from decode_vmx_inst() into a new operand_read() function. These patches should not cause any functional changes.
Patch 7 ('Use correct sizes when reading operands') replaces the incorrect operand size calculations based on address_size with the correct sizes from the SDM.
Patches 8 and 9 add new hvm_copy_{to,from}_guest_virt() helpers and use them to read memory operands in place of the incorrect segmentation logic in decode_vmx_inst().
Euan Harris (9):
x86/vvmx: Remove enum vmx_regs_enc
x86/vvmx: Unify operands in struct vmx_inst_decoded x86/vvmx: Extract operand reading logic into operand_read() x86/vvmx: Remove unnecessary VMX operand reads x86/vvmx: Replace direct calls to reg_read() with operand_read() x86/vvmx: Remove operand reading from decode_vmx_inst() x86/vvmx: Use correct sizes when reading operands x86/hvm: Add hvm_copy_{to,from}_guest_virt() helpers x86/vvmx: Use hvm_copy_{to,from}_guest_virt() to read operands
xen/arch/x86/hvm/hvm.c | 57 ++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 216 +++++++++++++++++++++---------------- xen/include/asm-x86/hvm/support.h | 12 +++ xen/include/asm-x86/hvm/vmx/vvmx.h | 22 ---- 4 files changed, 195 insertions(+), 112 deletions(-)
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 1/9 x86/vvmx: Remove enum vmx_regs_enc
This is the standard register encoding, is not VVMX-specific and is only used in a couple of places.
--- xen/arch/x86/hvm/vmx/vvmx.c | 8 ++++---- xen/include/asm-x86/hvm/vmx/vvmx.h | 22 ---------------------- 2 files changed, 4 insertions(+), 26 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +4 -4
@@ -201,10 +201,10 @@ struct vmx_inst_decoded {unsigned long mem;unsigned int len; }; -enum vmx_regs_enc reg1; +unsigned int reg1; }; -enum vmx_regs_enc reg2; +unsigned int reg2; };enum vmx_ops_result {
@@ -345,7 +345,7 @@ enum vmx_insn_errno set_vvmcs_real_safe(const struct vcpu *v, u32 encoding, }static unsigned long reg_read (struct cpu_user_regs *regs, -enum vmx_regs_enc index) +unsigned int index) {unsigned long *pval =decode_register (index, regs,0 );
@@ -353,7 +353,7 @@ static unsigned long reg_read(struct cpu_user_regs *regs, }static void reg_write (struct cpu_user_regs *regs, -enum vmx_regs_enc index, +unsigned int index,unsigned long value) {unsigned long *pval =decode_register (index, regs,0 );
xen/include/asm-x86/hvm/vmx/vvmx.h +0 -22
@@ -64,28 +64,6 @@ struct nestedvmx {/* bit 0-8, and 12 must be 1 */ #define VMX_ENTRY_CTLS_DEFAULT1 0x11ff -/* -* Encode of VMX instructions base on Table 24-11 & 24-12 of SDM 3B -*/ - -enum vmx_regs_enc { -VMX_REG_RAX , -VMX_REG_RCX , -VMX_REG_RDX , -VMX_REG_RBX , -VMX_REG_RSP , -VMX_REG_RBP , -VMX_REG_RSI , -VMX_REG_RDI , -VMX_REG_R8 , -VMX_REG_R9 , -VMX_REG_R10 , -VMX_REG_R11 , -VMX_REG_R12 , -VMX_REG_R13 , -VMX_REG_R14 , -VMX_REG_R15 , -}; union vmx_inst_info { struct {
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 2/9 x86/vvmx: Unify operands in struct vmx_inst_decoded
In reply to Euan HarrisThis change prepares the way for abstracting the code which reads memory and register operands into a generic operand_read() function in a future patch.
Operand 1 may either be a memory location or a register, depending on
the instruction being handled. Operand 2 may only be a register, but
it is now represented in the same way as operand 1 and so has a type
field. This will always equal VMX_INST_MEMREG_TYPE_REG.
References to the old structure map to the new one as follows:
mem -> op[0].mem
len -> op[0].len
reg1 -> op[0].reg
reg2 -> op[1].reg
References to type always map to op[0].type in this patch because previously operand 2 could only be a register and so type was never checked when accessing it.
--- xen/arch/x86/hvm/vmx/vvmx.c | 52 ++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 24 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +28 -24
@@ -195,16 +195,16 @@ bool_t nvmx_ept_enabled(struct vcpu *v)struct vmx_inst_decoded {#define VMX_INST_MEMREG_TYPE_MEMORY 0#define VMX_INST_MEMREG_TYPE_REG 1 -int type; -union { -struct { -unsigned long mem; -unsigned int len; +struct vmx_inst_op { +int type; +union { +struct { +unsigned long mem; +unsigned int len; + }; +unsigned int reg_idx; }; -unsigned int reg1; - }; - -unsigned int reg2; + } op[2 ]; };enum vmx_ops_result {
@@ -437,16 +437,16 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, info.word = offset;if ( info.fields.memreg ) { - decode->type =VMX_INST_MEMREG_TYPE_REG ; - decode->reg1 = info.fields.reg1; + decode->op[0 ].type =VMX_INST_MEMREG_TYPE_REG ; + decode->op[0 ].reg_idx = info.fields.reg1;if ( poperandS !=NULL ) - *poperandS =reg_read (regs, decode->reg1); + *poperandS =reg_read (regs, decode->op[0 ].reg_idx); }else {bool mode_64bit = (vmx_guest_x86_mode (v) ==8 ); - decode->type =VMX_INST_MEMREG_TYPE_MEMORY ; + decode->op[0 ].type =VMX_INST_MEMREG_TYPE_MEMORY ;if ( info.fields.segment > x86_seg_gs ) goto gp_fault;
@@ -486,11 +486,13 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,if ( rc != HVMTRANS_okay )return X86EMUL_EXCEPTION ; } - decode->mem = base; - decode->len = size; + + decode->op[0 ].mem = base; + decode->op[0 ].len = size; } - decode->reg2 = info.fields.reg2; + decode->op[1 ].type =VMX_INST_MEMREG_TYPE_REG ; + decode->op[1 ].reg_idx = info.fields.reg2;return X86EMUL_OKAY ;
@@ -1770,7 +1772,8 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs) gpa = nvcpu->nv_vvmcxaddr; - rc =hvm_copy_to_guest_linear (decode.mem, &gpa, decode.len,0 , &pfinfo); + rc =hvm_copy_to_guest_linear (decode.op[0 ].mem, &gpa, + decode.op[0 ].len,0 , &pfinfo);if ( rc == HVMTRANS_bad_linear_to_gfn )hvm_inject_page_fault (pfinfo.ec, pfinfo.linear);if ( rc != HVMTRANS_okay )
@@ -1850,23 +1853,24 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)return X86EMUL_OKAY ; } - rc =get_vvmcs_safe (v,reg_read (regs, decode.reg2), &value); + rc =get_vvmcs_safe (v,reg_read (regs, decode.op[1 ].reg_idx), &value);if ( rc !=VMX_INSN_SUCCEED ) {vmfail (regs, rc);return X86EMUL_OKAY ; } -switch ( decode.type ) { +switch ( decode.op[0 ].type ) {case VMX_INST_MEMREG_TYPE_MEMORY : - rc =hvm_copy_to_guest_linear (decode.mem, &value, decode.len,0 , &pfinfo); + rc =hvm_copy_to_guest_linear (decode.op[0 ].mem, &value, + decode.op[0 ].len,0 , &pfinfo);if ( rc == HVMTRANS_bad_linear_to_gfn )hvm_inject_page_fault (pfinfo.ec, pfinfo.linear);if ( rc != HVMTRANS_okay )return X86EMUL_EXCEPTION ;break ;case VMX_INST_MEMREG_TYPE_REG : -reg_write (regs, decode.reg1, value); +reg_write (regs, decode.op[0 ].reg_idx, value);break ; }
@@ -1893,7 +1897,7 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)return X86EMUL_OKAY ; } - vmcs_encoding =reg_read (regs, decode.reg2); + vmcs_encoding =reg_read (regs, decode.op[1 ].reg_idx); err =set_vvmcs_safe (v, vmcs_encoding, operand);if ( err !=VMX_INSN_SUCCEED ) {
@@ -1931,7 +1935,7 @@ int nvmx_handle_invept(struct cpu_user_regs *regs)if ( (ret =decode_vmx_inst (regs, &decode, &eptp,0 )) !=X86EMUL_OKAY )return ret; -switch (reg_read (regs, decode.reg2) ) +switch (reg_read (regs, decode.op[1 ].reg_idx) ) {case INVEPT_SINGLE_CONTEXT : {
@@ -1959,7 +1963,7 @@ int nvmx_handle_invvpid(struct cpu_user_regs *regs)if ( (ret =decode_vmx_inst (regs, &decode, &vpid,0 )) !=X86EMUL_OKAY )return ret; -switch (reg_read (regs, decode.reg2) ) +switch (reg_read (regs, decode.op[1 ].reg_idx) ) {/* Just invalidate all tlb entries for all types! */ case INVVPID_INDIVIDUAL_ADDR :
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 3/9 x86/vvmx: Extract operand reading logic into operand_read()
In reply to Euan HarrisExtract the logic for reading operands from decode_vmx_inst() intooperand_read(). Future patches will replace operand reading logic
in elsewhere with calls to operand_read().
operand_read() must explicitly handle different operand sizes to avoid
corrupting the caller's stack. This patch should not change the overall
behaviour of the code.
--- xen/arch/x86/hvm/vmx/vvmx.c | 59 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +47 -12
@@ -361,6 +361,40 @@ static void reg_write(struct cpu_user_regs *regs, *pval = value; } +static int operand_read (void *buf,struct vmx_inst_op *op, +struct cpu_user_regs *regs,unsigned int bytes) +{ +if ( op->type ==VMX_INST_MEMREG_TYPE_REG ) + { +switch ( bytes ) + { +case 4 : + *(uint32_t *)buf =reg_read (regs, op->reg_idx); + +case 8 : + *(uint64_t *)buf =reg_read (regs, op->reg_idx); + +default : +ASSERT_UNREACHABLE (); +return X86EMUL_UNHANDLEABLE ; + } + +return X86EMUL_OKAY ; + } +else + { +pagefault_info_t pfinfo; +int rc =hvm_copy_from_guest_linear (buf, op->mem, bytes,0 , &pfinfo); + +if ( rc == HVMTRANS_bad_linear_to_gfn ) +hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); +if ( rc != HVMTRANS_okay ) +return X86EMUL_EXCEPTION ; + +return X86EMUL_OKAY ; + } +} +static inline u32 __n2_pin_exec_control (struct vcpu *v) {return get_vvmcs (v,PIN_BASED_VM_EXEC_CONTROL );
@@ -440,7 +474,12 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, decode->op[0 ].type =VMX_INST_MEMREG_TYPE_REG ; decode->op[0 ].reg_idx = info.fields.reg1;if ( poperandS !=NULL ) - *poperandS =reg_read (regs, decode->op[0 ].reg_idx); + { +int rc =operand_read (poperandS, &decode->op[0 ], regs, + decode->op[0 ].len); +if ( rc !=X86EMUL_OKAY ) +return rc; + } }else {
@@ -475,20 +514,16 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, offset + size - 1 > seg.limit) ) goto gp_fault; + decode->op[0 ].mem = base; + decode->op[0 ].len = size; +if ( poperandS !=NULL ) { -pagefault_info_t pfinfo; -int rc =hvm_copy_from_guest_linear (poperandS, base, size, -0 , &pfinfo); - -if ( rc == HVMTRANS_bad_linear_to_gfn ) -hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); -if ( rc != HVMTRANS_okay ) -return X86EMUL_EXCEPTION ; +int rc =operand_read (poperandS, &decode->op[0 ], regs, + decode->op[0 ].len); +if ( rc !=X86EMUL_OKAY ) +return rc; } - - decode->op[0 ].mem = base; - decode->op[0 ].len = size; } decode->op[1 ].type =VMX_INST_MEMREG_TYPE_REG ;
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 3/9] x86/vvmx: Extract operand reading logic into operand_read()
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vvmx.c +7
-0@@ -361,6 +361,40 @@ static void reg_write(struct cpu_user_regs *regs, *pval = value; } +static int operand_read (void *buf,struct vmx_inst_op *op, +struct cpu_user_regs *regs,unsigned int bytes) +{ +if ( op->type ==VMX_INST_MEMREG_TYPE_REG ) + { +switch ( bytes ) + {
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 380
Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +10
-0+case 4 : + *(uint32_t *)buf =reg_read (regs, op->reg_idx); + +case 8 : + *(uint64_t *)buf =reg_read (regs, op->reg_idx); + +default : +ASSERT_UNREACHABLE (); +return X86EMUL_UNHANDLEABLE ; + }
Looks like there are two missing break statements up there.
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 381
Quoted 1 line
xen/arch/x86/hvm/vmx/vvmx.c +1
-0+return X86EMUL_OKAY ;
This and ...
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 394
Quoted 12 lines
xen/arch/x86/hvm/vmx/vvmx.c +12
-0+ } +else + { +pagefault_info_t pfinfo; +int rc =hvm_copy_from_guest_linear (buf, op->mem, bytes,0 , &pfinfo); + +if ( rc == HVMTRANS_bad_linear_to_gfn ) +hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); +if ( rc != HVMTRANS_okay ) +return X86EMUL_EXCEPTION ; + +return X86EMUL_OKAY ;
... this should become a uniform ...
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 394
Quoted 1 line
xen/arch/x86/hvm/vmx/vvmx.c +1
-0+ }
... return here.
xen/arch/x86/hvm/vmx/vvmx.c static int decode_vmx_inst(struct cpu_user_regs *regs, · line 480
Quoted 9 lines
xen/arch/x86/hvm/vmx/vvmx.c +4
-1@@ -440,7 +474,12 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, decode->op[0 ].type =VMX_INST_MEMREG_TYPE_REG ; decode->op[0 ].reg_idx = info.fields.reg1;if ( poperandS !=NULL ) - *poperandS =reg_read (regs, decode->op[0 ].reg_idx); + { +int rc =operand_read (poperandS, &decode->op[0 ], regs, + decode->op[0 ].len); +if ( rc !=X86EMUL_OKAY )
Blank line between declaration(s) and statement(s) please.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/9] x86/vvmx: Extract operand reading logic into operand_read()
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vvmx.c +10
-0@@ -361,6 +361,40 @@ static void reg_write(struct cpu_user_regs *regs, *pval = value; } +static int operand_read (void *buf,struct vmx_inst_op *op, +struct cpu_user_regs *regs,unsigned int bytes) +{ +if ( op->type ==VMX_INST_MEMREG_TYPE_REG ) + { +switch ( bytes ) + { +case 4 : + *(uint32_t *)buf =reg_read (regs, op->reg_idx); +
Quoted 8 lines
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 380
Quoted 7 lines
xen/arch/x86/hvm/vmx/vvmx.c +7
-0+case 8 : + *(uint64_t *)buf =reg_read (regs, op->reg_idx); + +default : +ASSERT_UNREACHABLE (); +return X86EMUL_UNHANDLEABLE ; + }Looks like there are two missing break statements up there.
I fixed these up x86-next. (Although it was only because Coverity noticed and complained at me...)
Quoted 18 lines
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 381
Quoted 2 lines
xen/arch/x86/hvm/vmx/vvmx.c +1
-0+return X86EMUL_OKAY ;This and ...
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 394
Quoted 12 lines
xen/arch/x86/hvm/vmx/vvmx.c +12
-0+ } +else + { +pagefault_info_t pfinfo; +int rc =hvm_copy_from_guest_linear (buf, op->mem, bytes,0 , &pfinfo); + +if ( rc == HVMTRANS_bad_linear_to_gfn ) +hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); +if ( rc != HVMTRANS_okay ) +return X86EMUL_EXCEPTION ; + +return X86EMUL_OKAY ;... this should become a uniform ...
Quoted 2 lines
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 394
Quoted 1 line
xen/arch/x86/hvm/vmx/vvmx.c +1
-0+ }... return here.
I tried this, but later patches in the series need them to move back. Overall, this layout reduces the code churn across the series.
Quoted 11 lines
xen/arch/x86/hvm/vmx/vvmx.c static int decode_vmx_inst(struct cpu_user_regs *regs, · line 480
Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +4
-1@@ -440,7 +474,12 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, decode->op[0 ].type =VMX_INST_MEMREG_TYPE_REG ; decode->op[0 ].reg_idx = info.fields.reg1;if ( poperandS !=NULL ) - *poperandS =reg_read (regs, decode->op[0 ].reg_idx); + { +int rc =operand_read (poperandS, &decode->op[0 ], regs, + decode->op[0 ].len); +if ( rc !=X86EMUL_OKAY )Blank line between declaration(s) and statement(s) please.
I can fix this.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/9] x86/vvmx: Extract operand reading logic into operand_read()
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 27.11.17 at 19:08, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
+ return
X86EMUL_OKAY;This and ...
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 394
Quoted 12 lines
xen/arch/x86/hvm/vmx/vvmx.c +12
-0+ } +else + { +pagefault_info_t pfinfo; +int rc =hvm_copy_from_guest_linear (buf, op->mem, bytes,0 , &pfinfo); + +if ( rc == HVMTRANS_bad_linear_to_gfn ) +hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); +if ( rc != HVMTRANS_okay ) +return X86EMUL_EXCEPTION ; + +return X86EMUL_OKAY ;... this should become a uniform ...
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
+ }
... return here.
I tried this, but later patches in the series need them to move back. Overall, this layout reduces the code churn across the series.
Ah, I see.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 3/9] x86/vvmx: Extract operand reading logic into operand_read()
In reply to Euan Harrisxen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 365
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vvmx.c +2
-0+static int operand_read (void *buf,struct vmx_inst_op *op, +struct cpu_user_regs *regs,unsigned int bytes)
const (twice)
xen/arch/x86/hvm/vmx/vvmx.c static void reg_write(struct cpu_user_regs *regs, · line 372
Quoted 7 lines
xen/arch/x86/hvm/vmx/vvmx.c +7
-0+{ +if ( op->type ==VMX_INST_MEMREG_TYPE_REG ) + { +switch ( bytes ) + { +case 4 : + *(uint32_t *)buf =reg_read (regs, op->reg_idx);
Looking at patch 7, you leave the upper half of 64-bit variables uninitialized here as well as in the memory case further down when passing in a smaller value for "bytes". A decent static analyzer should flag this, and I think things also wouldn't work right in a few cases.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 4/9 x86/vvmx: Remove unnecessary VMX operand reads
In reply to Euan Harris * vpid is never used in nvmx_handle_invept() so there is no point in reading it.
* vmptrst's operand is the memory address to which to write the VMCS
pointer. gpa is the pointer to write. Reading the address into
gpa is meaningless.
--- xen/arch/x86/hvm/vmx/vvmx.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +2 -3
@@ -1801,7 +1801,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)unsigned long gpa =0 ;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa,0 ); + rc =decode_vmx_inst (regs, &decode,NULL ,0 );if ( rc !=X86EMUL_OKAY )return rc;
@@ -1992,10 +1992,9 @@ int nvmx_handle_invept(struct cpu_user_regs *regs)int nvmx_handle_invvpid (struct cpu_user_regs *regs) {struct vmx_inst_decoded decode; -unsigned long vpid;int ret; -if ( (ret =decode_vmx_inst (regs, &decode, &vpid,0 )) !=X86EMUL_OKAY ) +if ( (ret =decode_vmx_inst (regs, &decode,NULL ,0 )) !=X86EMUL_OKAY )return ret; switch ( reg_read(regs, decode.op[1].reg_idx) )
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 4/9] x86/vvmx: Remove unnecessary VMX operand reads
Quoted 5 lines
From: Euan Harris [mailto:[email protected]]
Sent: Friday, October 27, 2017 1:03 AM* vpid is never used in
nvmx_handle_invept()so there is no point in reading it.
I think you meant nvmx_handle_invvpid
Quoted 38 lines
* vmptrst's operand is the memory address to which to write the VMCS
pointer. gpa is the pointer to write. Reading the address into
gpa is meaningless.Signed-off-by Euan Harris <[email protected]>--- xen/arch/x86/hvm/vmx/vvmx.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)xen/arch/x86/hvm/vmx/vvmx.c +2
-3@@ -1801,7 +1801,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)unsigned long gpa =0 ;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa,0 ); + rc =decode_vmx_inst (regs, &decode,NULL ,0 );if ( rc !=X86EMUL_OKAY )return rc;@@ -1992,10 +1992,9 @@ int nvmx_handle_invept(struct cpu_user_regs *regs)int nvmx_handle_invvpid (struct cpu_user_regs *regs) {struct vmx_inst_decoded decode; -unsigned long vpid;int ret; -if ( (ret =decode_vmx_inst (regs, &decode, &vpid,0 )) !=X86EMUL_OKAY ) +if ( (ret =decode_vmx_inst (regs, &decode,NULL ,0 )) !=X86EMUL_OKAY )return ret;switch ( reg_read(regs, decode.op[1].reg_idx) )
Quoted 2 lines
--
2.13.6
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH 4/9] x86/vvmx: Remove unnecessary VMX operand reads
In reply to Euan Harrisxen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_vmptrst(struct cpu_user_regs *regs) · line 1804
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vvmx.c +1
-1@@ -1801,7 +1801,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)unsigned long gpa =0 ;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa,0 ); + rc =decode_vmx_inst (regs, &decode,NULL ,0 );
You should then also drop gpa's initializer.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 5/9 x86/vvmx: Replace direct calls to reg_read() with operand_read()
In reply to Euan HarrisUse operand_read() to read register operands in the following functions:
* nvmx_handle_vmread()
* nvmx_handle_vmwrite()
* nvmx_handle_invept()
Direct reading of memory operands will be replaced in a separate patch.
--- xen/arch/x86/hvm/vmx/vvmx.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +24 -5
@@ -1877,6 +1877,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)pagefault_info_t pfinfo;u64 value =0 ;int rc; +unsigned long vmcs_encoding =0 ; rc =decode_vmx_inst (regs, &decode,NULL ,0 );if ( rc !=X86EMUL_OKAY )
@@ -1888,7 +1889,11 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)return X86EMUL_OKAY ; } - rc =get_vvmcs_safe (v,reg_read (regs, decode.op[1 ].reg_idx), &value); + rc =operand_read (&vmcs_encoding, &decode.op[1 ], regs, decode.op[1 ].len); +if ( rc !=X86EMUL_OKAY ) +return rc; + + rc =get_vvmcs_safe (v, vmcs_encoding, &value);if ( rc !=VMX_INSN_SUCCEED ) {vmfail (regs, rc);
@@ -1918,9 +1923,10 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)struct vcpu *v = current;struct vmx_inst_decoded decode;unsigned long operand; -u64 vmcs_encoding; +unsigned long vmcs_encoding =0 ;bool_t okay =1 ;enum vmx_insn_errno err; +int rc;if (decode_vmx_inst (regs, &decode, &operand,0 ) !=X86EMUL_OKAY )
@@ -1932,7 +1938,10 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)return X86EMUL_OKAY ; } - vmcs_encoding =reg_read (regs, decode.op[1 ].reg_idx); + rc =operand_read (&vmcs_encoding, &decode.op[1 ], regs, decode.op[1 ].len); +if ( rc !=X86EMUL_OKAY ) +return rc; + err =set_vvmcs_safe (v, vmcs_encoding, operand);if ( err !=VMX_INSN_SUCCEED ) {
@@ -1965,12 +1974,17 @@ int nvmx_handle_invept(struct cpu_user_regs *regs) {struct vmx_inst_decoded decode;unsigned long eptp; +unsigned long invept_type =0 ;int ret;if ( (ret =decode_vmx_inst (regs, &decode, &eptp,0 )) !=X86EMUL_OKAY )return ret; -switch (reg_read (regs, decode.op[1 ].reg_idx) ) + ret =operand_read (&invept_type, &decode.op[1 ], regs, decode.op[1 ].len); +if ( ret !=X86EMUL_OKAY ) +return ret; + +switch ( invept_type ) {case INVEPT_SINGLE_CONTEXT : {
@@ -1992,12 +2006,17 @@ int nvmx_handle_invept(struct cpu_user_regs *regs)int nvmx_handle_invvpid (struct cpu_user_regs *regs) {struct vmx_inst_decoded decode; +unsigned long invvpid_type =0 ;int ret;if ( (ret =decode_vmx_inst (regs, &decode,NULL ,0 )) !=X86EMUL_OKAY )return ret; -switch (reg_read (regs, decode.op[1 ].reg_idx) ) + ret =operand_read (&invvpid_type, &decode.op[1 ], regs, decode.op[1 ].len); +if ( ret !=X86EMUL_OKAY ) +return ret; + +switch ( invvpid_type ) {/* Just invalidate all tlb entries for all types! */ case INVVPID_INDIVIDUAL_ADDR :
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 6/9 x86/vvmx: Remove operand reading from decode_vmx_inst()
In reply to Euan HarrisUse operand_read() to read memory operands instead of using the value read by decode_vmx_inst() in the following functions:
* nvmx_handle_invept()
* nvmx_handle_invvpid()
* nvmx_handle_vmclear()
* nvmx_handle_vmptrld()
* nvmx_handle_vmxon()
* nvmx_handle_vmwrite()
--- xen/arch/x86/hvm/vmx/vvmx.c | 57 ++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 26 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +31 -26
@@ -456,7 +456,7 @@ gp_fault:static int decode_vmx_inst (struct cpu_user_regs *regs,struct vmx_inst_decoded *decode, -unsigned long *poperandS,int vmxon_check) +int vmxon_check) {struct vcpu *v = current;union vmx_inst_info info;
@@ -473,13 +473,6 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,if ( info.fields.memreg ) { decode->op[0 ].type =VMX_INST_MEMREG_TYPE_REG ; decode->op[0 ].reg_idx = info.fields.reg1; -if ( poperandS !=NULL ) - { -int rc =operand_read (poperandS, &decode->op[0 ], regs, - decode->op[0 ].len); -if ( rc !=X86EMUL_OKAY ) -return rc; - } }else {
@@ -516,14 +509,6 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, decode->op[0 ].mem = base; decode->op[0 ].len = size; - -if ( poperandS !=NULL ) - { -int rc =operand_read (poperandS, &decode->op[0 ], regs, - decode->op[0 ].len); -if ( rc !=X86EMUL_OKAY ) -return rc; - } } decode->op[1 ].type =VMX_INST_MEMREG_TYPE_REG ;
@@ -1513,7 +1498,11 @@ int nvmx_handle_vmxon(struct cpu_user_regs *regs)uint32_t nvmcs_revid;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa,1 ); + rc =decode_vmx_inst (regs, &decode,1 ); +if ( rc !=X86EMUL_OKAY ) +return rc; + + rc =operand_read (&gpa, &decode.op[0 ], regs, decode.op[0 ].len);if ( rc !=X86EMUL_OKAY )return rc;
@@ -1729,7 +1718,11 @@ int nvmx_handle_vmptrld(struct cpu_user_regs *regs)unsigned long gpa =0 ;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa,0 ); + rc =decode_vmx_inst (regs, &decode,0 ); +if ( rc !=X86EMUL_OKAY ) +return rc; + + rc =operand_read (&gpa, &decode.op[0 ], regs, decode.op[0 ].len);if ( rc !=X86EMUL_OKAY )return rc;
@@ -1801,7 +1794,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)unsigned long gpa =0 ;int rc; - rc =decode_vmx_inst (regs, &decode,NULL ,0 ); + rc =decode_vmx_inst (regs, &decode,0 );if ( rc !=X86EMUL_OKAY )return rc;
@@ -1828,7 +1821,11 @@ int nvmx_handle_vmclear(struct cpu_user_regs *regs)void *vvmcs;int rc; - rc =decode_vmx_inst (regs, &decode, &gpa,0 ); + rc =decode_vmx_inst (regs, &decode,0 ); +if ( rc !=X86EMUL_OKAY ) +return rc; + + rc =operand_read (&gpa, &decode.op[0 ], regs, decode.op[0 ].len);if ( rc !=X86EMUL_OKAY )return rc;
@@ -1879,7 +1876,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)int rc;unsigned long vmcs_encoding =0 ; - rc =decode_vmx_inst (regs, &decode,NULL ,0 ); + rc =decode_vmx_inst (regs, &decode,0 );if ( rc !=X86EMUL_OKAY )return rc;
@@ -1928,10 +1925,13 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)enum vmx_insn_errno err;int rc; -if (decode_vmx_inst (regs, &decode, &operand,0 ) - !=X86EMUL_OKAY ) +if (decode_vmx_inst (regs, &decode,0 ) !=X86EMUL_OKAY )return X86EMUL_EXCEPTION ; + rc =operand_read (&operand, &decode.op[0 ], regs, decode.op[0 ].len); +if ( rc !=X86EMUL_OKAY ) +return rc; +if (vcpu_nestedhvm (v).nv_vvmcxaddr ==INVALID_PADDR ) {vmfail_invalid (regs);
@@ -1973,11 +1973,10 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)int nvmx_handle_invept (struct cpu_user_regs *regs) {struct vmx_inst_decoded decode; -unsigned long eptp;unsigned long invept_type =0 ;int ret; -if ( (ret =decode_vmx_inst (regs, &decode, &eptp,0 )) !=X86EMUL_OKAY ) +if ( (ret =decode_vmx_inst (regs, &decode,0 )) !=X86EMUL_OKAY )return ret; ret =operand_read (&invept_type, &decode.op[1 ], regs, decode.op[1 ].len);
@@ -1988,6 +1987,12 @@ int nvmx_handle_invept(struct cpu_user_regs *regs) {case INVEPT_SINGLE_CONTEXT : { +unsigned long eptp; + + ret =operand_read (&eptp, &decode.op[0 ], regs, decode.op[0 ].len); +if ( ret ) +return ret; +np2m_flush_base (current, eptp);break ; }
@@ -2009,7 +2014,7 @@ int nvmx_handle_invvpid(struct cpu_user_regs *regs)unsigned long invvpid_type =0 ;int ret; -if ( (ret =decode_vmx_inst (regs, &decode,NULL ,0 )) !=X86EMUL_OKAY ) +if ( (ret =decode_vmx_inst (regs, &decode,0 )) !=X86EMUL_OKAY )return ret; ret =operand_read (&invvpid_type, &decode.op[1 ], regs, decode.op[1 ].len);
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 7/9 x86/vvmx: Use correct sizes when reading operands
In reply to Euan HarrisThe sizes of VMX operands are defined in the Intel SDM and have nothing to do with the addr_size field of struct vmx_inst_info:
invept: r32/r64, m128
invvpid: r32/r64, m128
vmclear: m64
vmptrld: m64
vmptrst: m64
vmread: r32/64 or m32/64, r32/64
vmwrite: r32/r64, r32/64 or m32/64
vmon: m64* Register operands are 32-bit or 64-bit depending on the guest mode.
* Memory operands are almost always of fixed size, usually 64-bit, but for vmread and vmwrite their size depends on the guest mode.
* invept has a 128-bit memory operand but the upper 64 bits are reserved and therefore need not be read.
* invvpid has a 128-bit memory operand but we only require the VPID value which lies in the lower 64 bits.
When reading variable-size operands, we pass the operand size calculated
by decode_vmx_inst() and stored in strcut vmx_inst_op. When reading
fixed-size operands, we pass the size of the variable into which the operand is to be read.
--- xen/arch/x86/hvm/vmx/vvmx.c | 48 +++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 23 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +25 -23
@@ -197,11 +197,9 @@ struct vmx_inst_decoded {#define VMX_INST_MEMREG_TYPE_REG 1struct vmx_inst_op {int type; +unsigned int bytes;union { -struct { -unsigned long mem; -unsigned int len; - }; +unsigned long mem;unsigned int reg_idx; }; } op[2 ];
@@ -464,6 +462,8 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,unsigned long base, index, seg_base, disp, offset;int scale, size; +unsigned int bytes =vmx_guest_x86_mode (v); +if (vmx_inst_check_privilege (regs, vmxon_check) !=X86EMUL_OKAY )return X86EMUL_EXCEPTION ;
@@ -473,10 +473,11 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,if ( info.fields.memreg ) { decode->op[0 ].type =VMX_INST_MEMREG_TYPE_REG ; decode->op[0 ].reg_idx = info.fields.reg1; + decode->op[0 ].bytes = bytes; }else { -bool mode_64bit = (vmx_guest_x86_mode (v) ==8 ); +bool mode_64bit = (bytes ==8 ); decode->op[0 ].type =VMX_INST_MEMREG_TYPE_MEMORY ;
@@ -508,11 +509,12 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, goto gp_fault; decode->op[0 ].mem = base; - decode->op[0 ].len = size; + decode->op[0 ].bytes = bytes; } decode->op[1 ].type =VMX_INST_MEMREG_TYPE_REG ; decode->op[1 ].reg_idx = info.fields.reg2; + decode->op[1 ].bytes = bytes;return X86EMUL_OKAY ;
@@ -1494,7 +1496,7 @@ int nvmx_handle_vmxon(struct cpu_user_regs *regs)struct nestedvmx *nvmx = &vcpu_2_nvmx (v);struct nestedvcpu *nvcpu = &vcpu_nestedhvm (v);struct vmx_inst_decoded decode; -unsigned long gpa =0 ; +uint64_t gpa;uint32_t nvmcs_revid;int rc;
@@ -1502,7 +1504,7 @@ int nvmx_handle_vmxon(struct cpu_user_regs *regs)if ( rc !=X86EMUL_OKAY )return rc; - rc =operand_read (&gpa, &decode.op[0 ], regs, decode.op[0 ].len); + rc =operand_read (&gpa, &decode.op[0 ], regs,sizeof (gpa));if ( rc !=X86EMUL_OKAY )return rc;
@@ -1715,14 +1717,14 @@ int nvmx_handle_vmptrld(struct cpu_user_regs *regs)struct vcpu *v = current;struct vmx_inst_decoded decode;struct nestedvcpu *nvcpu = &vcpu_nestedhvm (v); -unsigned long gpa =0 ; +uint64_t gpa;int rc; rc =decode_vmx_inst (regs, &decode,0 );if ( rc !=X86EMUL_OKAY )return rc; - rc =operand_read (&gpa, &decode.op[0 ], regs, decode.op[0 ].len); + rc =operand_read (&gpa, &decode.op[0 ], regs,sizeof (gpa));if ( rc !=X86EMUL_OKAY )return rc;
@@ -1801,7 +1803,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs) gpa = nvcpu->nv_vvmcxaddr; rc =hvm_copy_to_guest_linear (decode.op[0 ].mem, &gpa, - decode.op[0 ].len,0 , &pfinfo); + decode.op[0 ].bytes,0 , &pfinfo);if ( rc == HVMTRANS_bad_linear_to_gfn )hvm_inject_page_fault (pfinfo.ec, pfinfo.linear);if ( rc != HVMTRANS_okay )
@@ -1817,7 +1819,7 @@ int nvmx_handle_vmclear(struct cpu_user_regs *regs)struct vmx_inst_decoded decode;struct nestedvcpu *nvcpu = &vcpu_nestedhvm (v);struct nestedvmx *nvmx = &vcpu_2_nvmx (v); -unsigned long gpa =0 ; +uint64_t gpa;void *vvmcs;int rc;
@@ -1825,7 +1827,7 @@ int nvmx_handle_vmclear(struct cpu_user_regs *regs)if ( rc !=X86EMUL_OKAY )return rc; - rc =operand_read (&gpa, &decode.op[0 ], regs, decode.op[0 ].len); + rc =operand_read (&gpa, &decode.op[0 ], regs,sizeof (gpa));if ( rc !=X86EMUL_OKAY )return rc;
@@ -1886,7 +1888,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)return X86EMUL_OKAY ; } - rc =operand_read (&vmcs_encoding, &decode.op[1 ], regs, decode.op[1 ].len); + rc =operand_read (&vmcs_encoding, &decode.op[1 ], regs, decode.op[1 ].bytes);if ( rc !=X86EMUL_OKAY )return rc;
@@ -1900,7 +1902,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)switch ( decode.op[0 ].type ) {case VMX_INST_MEMREG_TYPE_MEMORY : rc =hvm_copy_to_guest_linear (decode.op[0 ].mem, &value, - decode.op[0 ].len,0 , &pfinfo); + decode.op[0 ].bytes,0 , &pfinfo);if ( rc == HVMTRANS_bad_linear_to_gfn )hvm_inject_page_fault (pfinfo.ec, pfinfo.linear);if ( rc != HVMTRANS_okay )
@@ -1928,7 +1930,7 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)if (decode_vmx_inst (regs, &decode,0 ) !=X86EMUL_OKAY )return X86EMUL_EXCEPTION ; - rc =operand_read (&operand, &decode.op[0 ], regs, decode.op[0 ].len); + rc =operand_read (&operand, &decode.op[0 ], regs, decode.op[0 ].bytes);if ( rc !=X86EMUL_OKAY )return rc;
@@ -1938,7 +1940,7 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)return X86EMUL_OKAY ; } - rc =operand_read (&vmcs_encoding, &decode.op[1 ], regs, decode.op[1 ].len); + rc =operand_read (&vmcs_encoding, &decode.op[1 ], regs, decode.op[1 ].bytes);if ( rc !=X86EMUL_OKAY )return rc;
@@ -1973,13 +1975,13 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)int nvmx_handle_invept (struct cpu_user_regs *regs) {struct vmx_inst_decoded decode; -unsigned long invept_type =0 ; +uint64_t invept_type;int ret;if ( (ret =decode_vmx_inst (regs, &decode,0 )) !=X86EMUL_OKAY )return ret; - ret =operand_read (&invept_type, &decode.op[1 ], regs, decode.op[1 ].len); + ret =operand_read (&invept_type, &decode.op[1 ], regs, decode.op[1 ].bytes);if ( ret !=X86EMUL_OKAY )return ret;
@@ -1987,9 +1989,9 @@ int nvmx_handle_invept(struct cpu_user_regs *regs) {case INVEPT_SINGLE_CONTEXT : { -unsigned long eptp; +uint64_t eptp; - ret =operand_read (&eptp, &decode.op[0 ], regs, decode.op[0 ].len); + ret =operand_read (&eptp, &decode.op[0 ], regs,sizeof (eptp));if ( ret )return ret;
@@ -2011,13 +2013,13 @@ int nvmx_handle_invept(struct cpu_user_regs *regs)int nvmx_handle_invvpid (struct cpu_user_regs *regs) {struct vmx_inst_decoded decode; -unsigned long invvpid_type =0 ; +uint64_t invvpid_type;int ret;if ( (ret =decode_vmx_inst (regs, &decode,0 )) !=X86EMUL_OKAY )return ret; - ret =operand_read (&invvpid_type, &decode.op[1 ], regs, decode.op[1 ].len); + ret =operand_read (&invvpid_type, &decode.op[1 ], regs, decode.op[1 ].bytes);if ( ret !=X86EMUL_OKAY )return ret;
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 7/9] x86/vvmx: Use correct sizes when reading operands
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
* invvpid has a 128-bit memory operand but we only require the VPID value which lies in the lower 64 bits.
The memory operand (wrongly) isn't being read at all - I don't understand the above bullet point for that reason.
xen/arch/x86/hvm/vmx/vvmx.c static int decode_vmx_inst(struct cpu_user_regs *regs, · line 466
Quoted 6 lines
xen/arch/x86/hvm/vmx/vvmx.c +2
-0@@ -464,6 +462,8 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,unsigned long base, index, seg_base, disp, offset;int scale, size; +unsigned int bytes =vmx_guest_x86_mode (v); +
Except in extraordinary circumstances please don't add blank lines in the middle of declarations.
Also - don't you get 2 here for 16-bit protected mode, which you'd need to convert to 4?
xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_vmptrst(struct cpu_user_regs *regs) · line 1806
Quoted 6 lines
xen/arch/x86/hvm/vmx/vvmx.c +1
-1@@ -1801,7 +1803,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs) gpa = nvcpu->nv_vvmcxaddr; rc =hvm_copy_to_guest_linear (decode.op[0 ].mem, &gpa, - decode.op[0 ].len,0 , &pfinfo); + decode.op[0 ].bytes,0 , &pfinfo);
Just like for vmptrld the operand size is uniformly 64 bits here.
xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_invept(struct cpu_user_regs *regs) · line 1994
Quoted 9 lines
xen/arch/x86/hvm/vmx/vvmx.c +2
-2@@ -1987,9 +1989,9 @@ int nvmx_handle_invept(struct cpu_user_regs *regs) {case INVEPT_SINGLE_CONTEXT : { -unsigned long eptp; +uint64_t eptp; - ret =operand_read (&eptp, &decode.op[0 ], regs, decode.op[0 ].len); + ret =operand_read (&eptp, &decode.op[0 ], regs,sizeof (eptp));
I think you should read the full 128 bits for correct faulting behavior (also for invvpid). I also can't derive from the SDM that this reading won't occur for the INVEPT_ALL_CONTEXT case. Sadly the SDM doesn't say whether the reserved upper half is enforced to be zero (which we would then need to emulate), or whether it is being ignored. For invvpid however it does state that bits 16:63 are being checked.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 7/9] x86/vvmx: Use correct sizes when reading operands
Quoted 10 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
* invvpid has a 128-bit memory operand but we only require the VPID value which lies in the lower 64 bits.
The memory operand (wrongly) isn't being read at all - I don't understand the above bullet point for that reason.
Quoted 4 lines
xen/arch/x86/hvm/vmx/vvmx.c +0
-0@@ -464,6 +462,8 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,unsigned long base, index, seg_base, disp, offset;int scale, size;
Quoted 7 lines
xen/arch/x86/hvm/vmx/vvmx.c static int decode_vmx_inst(struct cpu_user_regs *regs, · line 466
Quoted 2 lines
xen/arch/x86/hvm/vmx/vvmx.c +2
-0+unsigned int bytes =vmx_guest_x86_mode (v); +Except in extraordinary circumstances please don't add blank lines in the middle of declarations.
Also - don't you get 2 here for 16-bit protected mode, which you'd need to convert to 4?
We can never reach this point from a 16 bit segment. All VMX instructions #UD if executed outside of a 64bit (in long mode) or 32bit (outside of long mode) segment.
Quoted 16 lines
xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_vmptrst(struct cpu_user_regs *regs) · line 1806
Quoted 7 lines
xen/arch/x86/hvm/vmx/vvmx.c +1
-1@@ -1801,7 +1803,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs) gpa = nvcpu->nv_vvmcxaddr; rc =hvm_copy_to_guest_linear (decode.op[0 ].mem, &gpa, - decode.op[0 ].len,0 , &pfinfo); + decode.op[0 ].bytes,0 , &pfinfo);Just like for vmptrld the operand size is uniformly 64 bits here.
Quoted 7 lines
xen/arch/x86/hvm/vmx/vvmx.c +1
-1@@ -1987,9 +1989,9 @@ int nvmx_handle_invept(struct cpu_user_regs *regs) {case INVEPT_SINGLE_CONTEXT : { -unsigned long eptp; +uint64_t eptp;
Quoted 9 lines
xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_invept(struct cpu_user_regs *regs) · line 1994
Quoted 2 lines
xen/arch/x86/hvm/vmx/vvmx.c +1
-1- ret =operand_read (&eptp, &decode.op[0 ], regs, decode.op[0 ].len); + ret =operand_read (&eptp, &decode.op[0 ], regs,sizeof (eptp));I think you should read the full 128 bits for correct faulting behavior (also for invvpid). I also can't derive from the SDM that this reading won't occur for the
INVEPT_ALL_CONTEXTcase. Sadly the SDM doesn't say whether the reserved upper half is enforced to be zero (which we would then need to emulate), or whether it is being ignored. For invvpid however it does state that bits 16:63 are being checked.
Observations on real hardware to the contrary. Each of the generations I've tried never read the operand, or the part of the operand that they don't need.
It makes sense from a performance point of view. No point having microcode make unnecessary memory accesses.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [PATCH 7/9] x86/vvmx: Use correct sizes when reading operands
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 01.12.17 at 19:45, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
* invvpid has a 128-bit memory operand but we only require the VPID value which lies in the lower 64 bits.
The memory operand (wrongly) isn't being read at all - I don't understand the above bullet point for that reason.
xen/arch/x86/hvm/vmx/vvmx.c static int decode_vmx_inst(struct cpu_user_regs *regs, · line 466
Quoted 6 lines
xen/arch/x86/hvm/vmx/vvmx.c +2
-0@@ -464,6 +462,8 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,unsigned long base, index, seg_base, disp, offset;int scale, size; +unsigned int bytes =vmx_guest_x86_mode (v); +Except in extraordinary circumstances please don't add blank lines in the middle of declarations.
Quoted 6 lines
Quoted 2 lines
Also - don't you get 2 here for 16-bit protected mode, which you'd need to convert to 4?
We can never reach this point from a 16 bit segment. All VMX instructions #UD if executed outside of a 64bit (in long mode) or 32bit (outside of long mode) segment.
That's certainly not what the insn pages say: The common conditional used is
IF (not in VMX operation) or (CR0.PE = 0) or (RFLAGS.VM = 1) or (IA32_EFER.LMA = 1 and CS.L = 0) THEN #UD;
which excludes real, VM86, and compat modes, but not 16-bit protected mode. Of course there's the option of hardware behaving like you say and the manual just being wrong.
Quoted 17 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/hvm/vmx/vvmx.c +1
-1@@ -1987,9 +1989,9 @@ int nvmx_handle_invept(struct cpu_user_regs *regs) {case INVEPT_SINGLE_CONTEXT : { -unsigned long eptp; +uint64_t eptp;Quoted 9 lines
xen/arch/x86/hvm/vmx/vvmx.c int nvmx_handle_invept(struct cpu_user_regs *regs) · line 1994
Quoted 2 lines
xen/arch/x86/hvm/vmx/vvmx.c +1
-1- ret =operand_read (&eptp, &decode.op[0 ], regs, decode.op[0 ].len); + ret =operand_read (&eptp, &decode.op[0 ], regs,sizeof (eptp));I think you should read the full 128 bits for correct faulting behavior (also for invvpid). I also can't derive from the SDM that this reading won't occur for the
INVEPT_ALL_CONTEXTcase. Sadly the SDM doesn't say whether the reserved upper half is enforced to be zero (which we would then need to emulate), or whether it is being ignored. For invvpid however it does state that bits 16:63 are being checked.
Quoted 3 lines
Observations on real hardware to the contrary. Each of the generations I've tried never read the operand, or the part of the operand that they don't need.
Correct faulting behavior implies more than whether the actual read
occurs: A quick test confirms that the ept_sync_all() used during VMX enabling ends in #GP when the memory address of INVEPT is being changed to a non-canonical one, and in #PF if changed to a suitable canonical one (on a Westmere system).
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 8/9 x86/hvm: Add hvm_copy_{to, from}_guest_virt() helpers
In reply to Euan Harrishvm_copy_{to,from}_guest_virt() copy data to and from a guest, performing segmentatino and paging checks on the provided seg:offset virtual address.
--- xen/arch/x86/hvm/hvm.c | 57 +++++++++++++++++++++++++++++++++++++++ xen/include/asm-x86/hvm/support.h | 12 +++++++++ 2 files changed, 69 insertions(+)
xen/arch/x86/hvm/hvm.c +57 -0
@@ -3312,6 +3312,63 @@ unsigned long copy_from_user_hvm(void *to, const void *from, unsigned len)return rc ? len :0 ;/* fake a copy_from_user() return code */ } +static int _hvm_copy_guest_virt ( +enum x86_segment seg,unsigned long offset,void *buf,unsigned int bytes, +uint32_t pfec,unsigned int flags) +{ +struct vcpu *curr = current; +struct segment_register sreg, cs; +enum hvm_translation_result res; +pagefault_info_t pfinfo; +unsigned long linear; + +ASSERT (is_x86_user_segment (seg)); + +hvm_get_segment_register (curr, seg, &sreg); +hvm_get_segment_register (curr, x86_seg_cs, &cs); + +if ( !hvm_virtual_to_linear_addr ( + seg, &sreg, offset, bytes, + flags & HVMCOPY_to_guest ? hvm_access_write : hvm_access_read, + &cs, &linear) ) + { +hvm_inject_hw_exception ( + (seg == x86_seg_ss) ? TRAP_stack_error : TRAP_gp_fault,0 ); +return X86EMUL_EXCEPTION ; + } + +if ( flags & HVMCOPY_to_guest ) + res =hvm_copy_to_guest_linear (linear, buf, bytes, pfec, &pfinfo); +else + res =hvm_copy_from_guest_linear (buf, linear, bytes, pfec, &pfinfo); + +if ( res == HVMTRANS_bad_linear_to_gfn ) + { +hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); +return X86EMUL_EXCEPTION ; + } +else if ( res ) +return X86EMUL_RETRY ; + +return X86EMUL_OKAY ; +} + +int hvm_copy_to_guest_virt ( +enum x86_segment seg,unsigned long offset,void *buf,unsigned int bytes, +uint32_t pfec) +{ +return _hvm_copy_guest_virt (seg, offset, buf, bytes, pfec, + HVMCOPY_to_guest); +} + +int hvm_copy_from_guest_virt ( +void *buf,enum x86_segment seg,unsigned long offset,unsigned int bytes, +uint32_t pfec) +{ +return _hvm_copy_guest_virt (seg, offset, buf, bytes, pfec, + HVMCOPY_from_guest); +} +bool hvm_check_cpuid_faulting (struct vcpu *v) {const struct msr_vcpu_policy *vp = v->arch.msr;
xen/include/asm-x86/hvm/support.h +12 -0
@@ -115,6 +115,18 @@ enum hvm_translation_result hvm_translate_get_page( pagefault_info_t *pfinfo, struct page_info **page_p, gfn_t *gfn_p, p2m_type_t *p2mt_p); +/* +* Copy data to and from a guest, performing segmentation and paging checks +* on the provided seg:offset virtual address. +* Returns X86EMUL_* and raises exceptions with the current vcpu. +*/ +int hvm_copy_to_guest_virt ( +enum x86_segment seg,unsigned long offset,void *buf,unsigned int bytes, +uint32_t pfec); +int hvm_copy_from_guest_virt ( +void *buf,enum x86_segment seg,unsigned long offset,unsigned int bytes, +uint32_t pfec); +#define HVM_HCALL_completed 0/* hypercall completed - no further action */ #define HVM_HCALL_preempted 1/* hypercall preempted - re-execute VMCALL */ int hvm_hypercall (struct cpu_user_regs *regs);
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 8/9] x86/hvm: Add hvm_copy_{to, from}_guest_virt() helpers
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
hvm_copy_{to,from}_guest_virt() copy data to and from a guest, performing segmentatino and paging checks on the provided seg:offset virtual address.
I'm not sure "paging" is worthwhile to mention here, as that's not done by the new functions, but by the existing one acting on linear addresses.
Quoted 29 lines
xen/arch/x86/hvm/hvm.c +29
-0+static int _hvm_copy_guest_virt ( +enum x86_segment seg,unsigned long offset,void *buf,unsigned int bytes, +uint32_t pfec,unsigned int flags) +{ +struct vcpu *curr = current; +struct segment_register sreg, cs; +enum hvm_translation_result res; +pagefault_info_t pfinfo; +unsigned long linear; + +ASSERT (is_x86_user_segment (seg)); + +hvm_get_segment_register (curr, seg, &sreg); +hvm_get_segment_register (curr, x86_seg_cs, &cs); + +if ( !hvm_virtual_to_linear_addr ( + seg, &sreg, offset, bytes, + flags & HVMCOPY_to_guest ? hvm_access_write : hvm_access_read, + &cs, &linear) ) + { +hvm_inject_hw_exception ( + (seg == x86_seg_ss) ? TRAP_stack_error : TRAP_gp_fault,0 ); +return X86EMUL_EXCEPTION ; + } + +if ( flags & HVMCOPY_to_guest ) + res =hvm_copy_to_guest_linear (linear, buf, bytes, pfec, &pfinfo); +else + res =hvm_copy_from_guest_linear (buf, linear, bytes, pfec, &pfinfo);
xen/arch/x86/hvm/hvm.c unsigned long copy_from_user_hvm(void *to, const void *from, unsigned len) · line 3353
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10
-0+ +if ( res == HVMTRANS_bad_linear_to_gfn ) + { +hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); +return X86EMUL_EXCEPTION ; + } +else if ( res ) +return X86EMUL_RETRY ; + +return X86EMUL_OKAY ;
Please either use switch() here, or at least omit the pointless "else".
Quoted 5 lines
xen/arch/x86/hvm/hvm.c +5
-0+int hvm_copy_to_guest_virt ( +enum x86_segment seg,unsigned long offset,void *buf,unsigned int bytes, +uint32_t pfec) +{ + return _hvm_copy_guest_virt(seg, offset, buf, bytes, pfec,
xen/arch/x86/hvm/hvm.c unsigned long copy_from_user_hvm(void *to, const void *from, unsigned len) · line 3370
Quoted 10 lines
xen/arch/x86/hvm/hvm.c +10
-0+ HVMCOPY_to_guest); +} + +int hvm_copy_from_guest_virt ( +void *buf,enum x86_segment seg,unsigned long offset,unsigned int bytes, +uint32_t pfec) +{ +return _hvm_copy_guest_virt (seg, offset, buf, bytes, pfec, + HVMCOPY_from_guest); +}
Is there any reason the parameter order is different between the two wrappers and the actual worker? That'll cause unnecessary register shuffling, when really the wrappers could just be a load of a constant into a register plus a branch.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
PATCH 9/9 x86/vvmx: Use hvm_copy_{to, from}_guest_virt() to read operands
In reply to Euan Harrisdecode_vmx_inst() contains its own segmentation logic. This
unnecessarily duplicates segmentation code used elsewhere and contains
errors: it raises a #GP fault instead of an #SS fault for an invalid memory access through the stack segment.
Replace this logic with hvm_copy_{to,from}_guest_virt(), which use well-tested memory access code used in various other parts of the hypervisor.
--- xen/arch/x86/hvm/vmx/vvmx.c | 80 +++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 54 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +26 -54
@@ -199,7 +199,10 @@ struct vmx_inst_decoded {int type;unsigned int bytes;union { -unsigned long mem; +struct { +enum x86_segment seg; +unsigned long offset; + };unsigned int reg_idx; }; } op[2 ];
@@ -380,17 +383,7 @@ static int operand_read(void *buf, struct vmx_inst_op *op,return X86EMUL_OKAY ; }else - { -pagefault_info_t pfinfo; -int rc =hvm_copy_from_guest_linear (buf, op->mem, bytes,0 , &pfinfo); - -if ( rc == HVMTRANS_bad_linear_to_gfn ) -hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); -if ( rc != HVMTRANS_okay ) -return X86EMUL_EXCEPTION ; - -return X86EMUL_OKAY ; - } +return hvm_copy_from_guest_virt (buf, op->seg, op->offset, bytes,0 ); }static inline u32 __n2_pin_exec_control (struct vcpu *v)
@@ -458,9 +451,8 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, {struct vcpu *v = current;union vmx_inst_info info; -struct segment_register seg; -unsigned long base, index, seg_base, disp, offset; -int scale, size; +unsigned long base, index, disp, offset; +int scale;unsigned int bytes =vmx_guest_x86_mode (v);
@@ -477,14 +469,11 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, }else { -bool mode_64bit = (bytes ==8 ); - - decode->op[0 ].type =VMX_INST_MEMREG_TYPE_MEMORY ; -if ( info.fields.segment > x86_seg_gs ) - goto gp_fault; -hvm_get_segment_register (v, info.fields.segment, &seg); - seg_base = seg.base; + { +ASSERT_UNREACHABLE (); +return X86EMUL_UNHANDLEABLE ; + } base = info.fields.base_reg_invalid ?0 :reg_read (regs, info.fields.base_reg);
@@ -496,19 +485,12 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,__vmread (EXIT_QUALIFICATION , &disp); - size =1 << (info.fields.addr_size +1 ); - - offset = base + index * scale + disp; - base = !mode_64bit || info.fields.segment >= x86_seg_fs ? - seg_base + offset : offset; -if ( offset + size -1 < offset || - (mode_64bit ? - !is_canonical_address ((long )base <0 ? base : - base + size -1 ) : - offset + size -1 > seg.limit) ) - goto gp_fault; + decode->op[0 ].type =VMX_INST_MEMREG_TYPE_MEMORY ; + decode->op[0 ].seg = info.fields.segment; + decode->op[0 ].offset = base + index * scale + disp; +if ( info.fields.addr_size <2 ) + decode->op[0 ].offset = (uint32_t )decode->op[0 ].offset; - decode->op[0 ].mem = base; decode->op[0 ].bytes = bytes; }
@@ -517,10 +499,6 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, decode->op[1 ].bytes = bytes;return X86EMUL_OKAY ; - -gp_fault: -hvm_inject_hw_exception (TRAP_gp_fault,0 ); -return X86EMUL_EXCEPTION ; }static void vmsucceed (struct cpu_user_regs *regs)
@@ -1792,8 +1770,7 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs)struct vcpu *v = current;struct vmx_inst_decoded decode;struct nestedvcpu *nvcpu = &vcpu_nestedhvm (v); -pagefault_info_t pfinfo; -unsigned long gpa =0 ; +uint64_t gpa;int rc; rc =decode_vmx_inst (regs, &decode,0 );
@@ -1802,12 +1779,10 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs) gpa = nvcpu->nv_vvmcxaddr; - rc =hvm_copy_to_guest_linear (decode.op[0 ].mem, &gpa, - decode.op[0 ].bytes,0 , &pfinfo); -if ( rc == HVMTRANS_bad_linear_to_gfn ) -hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); -if ( rc != HVMTRANS_okay ) -return X86EMUL_EXCEPTION ; + rc =hvm_copy_to_guest_virt (decode.op[0 ].seg, decode.op[0 ].offset, + &gpa,sizeof (gpa),0 ); +if ( rc !=X86EMUL_OKAY ) +return rc;vmsucceed (regs);return X86EMUL_OKAY ;
@@ -1873,8 +1848,7 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs) {struct vcpu *v = current;struct vmx_inst_decoded decode; -pagefault_info_t pfinfo; -u64 value =0 ; +uint64_t value;int rc;unsigned long vmcs_encoding =0 ;
@@ -1901,12 +1875,10 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)switch ( decode.op[0 ].type ) {case VMX_INST_MEMREG_TYPE_MEMORY : - rc =hvm_copy_to_guest_linear (decode.op[0 ].mem, &value, - decode.op[0 ].bytes,0 , &pfinfo); -if ( rc == HVMTRANS_bad_linear_to_gfn ) -hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); -if ( rc != HVMTRANS_okay ) -return X86EMUL_EXCEPTION ; + rc =hvm_copy_to_guest_virt (decode.op[0 ].seg, decode.op[0 ].offset, + &value,sizeof (value),0 ); +if ( rc !=X86EMUL_OKAY ) +return rc;break ;case VMX_INST_MEMREG_TYPE_REG :reg_write (regs, decode.op[0 ].reg_idx, value);
Signature · 7 lines
2.13.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH 9/9] x86/vvmx: Use hvm_copy_{to, from}_guest_virt() to read operands
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 26.10.17 at 19:03, <[email protected]> wrote:
In the title please use "read/write" or "access".
Quoted 7 lines
xen/arch/x86/hvm/vmx/vvmx.c +0
-3@@ -380,17 +383,7 @@ static int operand_read(void *buf, struct vmx_inst_op *op,return X86EMUL_OKAY ; }else - { -pagefault_info_t pfinfo; -int rc =hvm_copy_from_guest_linear (buf, op->mem, bytes,0 , &pfinfo);
xen/arch/x86/hvm/vmx/vvmx.c static int operand_read(void *buf, struct vmx_inst_op *op, · line 387
Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +1
-8- -if ( rc == HVMTRANS_bad_linear_to_gfn ) -hvm_inject_page_fault (pfinfo.ec, pfinfo.linear); -if ( rc != HVMTRANS_okay ) -return X86EMUL_EXCEPTION ; - -return X86EMUL_OKAY ; - } +return hvm_copy_from_guest_virt (buf, op->seg, op->offset, bytes,0 ); }
Please also drop the now pointless "else".
xen/arch/x86/hvm/vmx/vvmx.c static int decode_vmx_inst(struct cpu_user_regs *regs, · line 455
Quoted 9 lines
xen/arch/x86/hvm/vmx/vvmx.c +2
-3@@ -458,9 +451,8 @@ static int decode_vmx_inst(struct cpu_user_regs *regs, {struct vcpu *v = current;union vmx_inst_info info; -struct segment_register seg; -unsigned long base, index, seg_base, disp, offset; -int scale, size; +unsigned long base, index, disp, offset; +int scale;
unsigned int please, if you touch it anyway.
Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +0
-6@@ -496,19 +485,12 @@ static int decode_vmx_inst(struct cpu_user_regs *regs,__vmread (EXIT_QUALIFICATION , &disp); - size =1 << (info.fields.addr_size +1 ); - - offset = base + index * scale + disp; - base = !mode_64bit || info.fields.segment >= x86_seg_fs ? - seg_base + offset : offset; - if ( offset + size - 1 < offset ||
xen/arch/x86/hvm/vmx/vvmx.c static int decode_vmx_inst(struct cpu_user_regs *regs, · line 492
Quoted 10 lines
xen/arch/x86/hvm/vmx/vvmx.c +5
-5- (mode_64bit ? - !is_canonical_address ((long )base <0 ? base : - base + size -1 ) : - offset + size - 1 > seg.limit) ) - goto gp_fault; + decode->op[0 ].type =VMX_INST_MEMREG_TYPE_MEMORY ; + decode->op[0 ].seg = info.fields.segment; + decode->op[0 ].offset = base + index * scale + disp; +if ( info.fields.addr_size <2 ) + decode->op[0 ].offset = (uint32_t )decode->op[0 ].offset;
For 16-bit addressing you need to truncate to 16 bits.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 27 lines · Euan Harris
decode_vmx_inst()does not read instruction operands correctly on VM exit:* It incorrectly uses vmx_inst_info's
address_sizefield to calculate the sizes of the exit-causing instruction's operands. The sizes of the operands are specified in the SDM and might depend on whether the guest is running in 32-bit or 64-bit mode, but they have nothing to do with theaddress_sizefield.* It includes its own segmentation logic, duplicating code elsewhere. This segmentation logic is also incorrect and will raise #GP fault rather than a #SS fault in response to an invalid memory access through the stack segment.
Patches 1-6 (up to 'Remove operand decoding from
decode_vmx_inst()') refactordecode_vmx_inst()in preparation for fixing the bugs mentioned above. They remove unnecessary code and extract the logic for reading operands fromdecode_vmx_inst()into a newoperand_read()function. These patches should not cause any functional changes.Patch 7 ('Use correct sizes when reading operands') replaces the incorrect operand size calculations based on
address_sizewith the correct sizes from the SDM.Patches 8 and 9 add new hvm_copy_{to,from}_guest_virt() helpers and use them to read memory operands in place of the incorrect segmentation logic in
decode_vmx_inst().
Quoted 10 lines
Euan Harris (9):
x86/vvmx: Remove enumvmx_regs_enc
x86/vvmx: Unify operands in structvmx_inst_decodedx86/vvmx: Extract operand reading logic intooperand_read()x86/vvmx: Remove unnecessary VMX operand reads x86/vvmx: Replace direct calls toreg_read()withoperand_read()x86/vvmx: Remove operand reading fromdecode_vmx_inst()x86/vvmx: Use correct sizes when reading operands x86/hvm: Add hvm_copy_{to,from}_guest_virt() helpers x86/vvmx: Use hvm_copy_{to,from}_guest_virt() to read operands
All Reviewed-by: Andrew Cooper <[email protected]>. I've noticed a few trivial style issues which can be fixed up on commit if there are no other issues.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Tian, Kevin
Acked-byQuoted 45 lines
From: Andrew Cooper [mailto:[email protected]]
Sent: Friday, October 27, 2017 1:59 AMQuoted 1 line · Euan Harris
decode_vmx_inst()does not read instruction operands correctly on VMexit:
Quoted 4 lines
* It incorrectly uses vmx_inst_info's
address_sizefield to calculate the sizes of the exit-causing instruction's operands. The sizes of the operands are specified in the SDM and might depend on whetherthe
Quoted 14 lines
guest is running in 32-bit or 64-bit mode, but they have nothing to do with the
address_sizefield.* It includes its own segmentation logic, duplicating code elsewhere. This segmentation logic is also incorrect and will raise #GP fault rather than a #SS fault in response to an invalid memory access through the stack segment.
Patches 1-6 (up to 'Remove operand decoding from
decode_vmx_inst()') refactordecode_vmx_inst()in preparation for fixing the bugs mentioned above. They remove unnecessary code and extract the logic for reading operands fromdecode_vmx_inst()into a newoperand_read()function. These patches should not cause any functional changes.Quoted 5 lines
Patch 7 ('Use correct sizes when reading operands') replaces the incorrect operand size calculations based on
address_sizewith the correct sizes from the SDM.Patches 8 and 9 add new hvm_copy_{to,from}_guest_virt() helpers and
use
Quoted 13 lines
them to read memory operands in place of the incorrect segmentation logic in
decode_vmx_inst().Euan Harris (9):
x86/vvmx: Remove enumvmx_regs_enc
x86/vvmx: Unify operands in structvmx_inst_decodedx86/vvmx: Extract operand reading logic intooperand_read()x86/vvmx: Remove unnecessary VMX operand reads x86/vvmx: Replace direct calls toreg_read()withoperand_read()x86/vvmx: Remove operand reading fromdecode_vmx_inst()x86/vvmx: Use correct sizes when reading operands x86/hvm: Add hvm_copy_{to,from}_guest_virt() helpers x86/vvmx: Use hvm_copy_{to,from}_guest_virt() to read operands
Quoted 4 lines
All Reviewed-by: Andrew Cooper <[email protected]>. I've noticed a few trivial style issues which can be fixed up on commit if there are no other issues.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 43 lines · Kevin
Quoted 43 lines
From: Andrew Cooper [mailto:[email protected]]
Sent: Friday, October 27, 2017 1:59 AMQuoted 1 line · Euan Harris
decode_vmx_inst()does not read instruction operands correctly on VMexit:
Quoted 3 lines
* It incorrectly uses vmx_inst_info's
address_sizefield to calculate the sizes of the exit-causing instruction's operands. The sizes of the operands are specified in the SDM and might depend on whetherthe
Quoted 14 lines
guest is running in 32-bit or 64-bit mode, but they have nothing to do with the
address_sizefield.* It includes its own segmentation logic, duplicating code elsewhere. This segmentation logic is also incorrect and will raise #GP fault rather than a #SS fault in response to an invalid memory access through the stack segment.
Patches 1-6 (up to 'Remove operand decoding from
decode_vmx_inst()') refactordecode_vmx_inst()in preparation for fixing the bugs mentioned above. They remove unnecessary code and extract the logic for reading operands fromdecode_vmx_inst()into a newoperand_read()function. These patches should not cause any functional changes.Quoted 5 lines
Patch 7 ('Use correct sizes when reading operands') replaces the incorrect operand size calculations based on
address_sizewith the correct sizes from the SDM.Patches 8 and 9 add new hvm_copy_{to,from}_guest_virt() helpers and
use
Quoted 13 lines
them to read memory operands in place of the incorrect segmentation logic in
decode_vmx_inst().Euan Harris (9):
x86/vvmx: Remove enumvmx_regs_enc
x86/vvmx: Unify operands in structvmx_inst_decodedx86/vvmx: Extract operand reading logic intooperand_read()x86/vvmx: Remove unnecessary VMX operand reads x86/vvmx: Replace direct calls toreg_read()withoperand_read()x86/vvmx: Remove operand reading fromdecode_vmx_inst()x86/vvmx: Use correct sizes when reading operands x86/hvm: Add hvm_copy_{to,from}_guest_virt() helpers x86/vvmx: Use hvm_copy_{to,from}_guest_virt() to read operands
Quoted 5 lines
Quoted 4 lines
All Reviewed-by: Andrew Cooper <[email protected]>. I've noticed a few trivial style issues which can be fixed up on commit if there are no other issues.
Acked-by Kevin Tian <[email protected]>
Pulled into x86-next, with the comments addressed.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel