x86/vvmx: correctly emulate VMREAD and VMWRITE
v4 cover x86/vvmx: correctly emulate VMREAD and VMWRITE
Currently, emulation of vmread and vmwrite for a guest leads to BUG() in cases when actual VMREAD or VMWRITE ends up in VMfail due to invalid arguments. The goal of this patch series is to prevent the BUG() from happening and report any kind of VMfail back to the guest, just like it would be done by H/W.
v3 --> v4:
* First 2 patches were merged into master
* added "x86/vmx: introduce VMX_INSN_SUCCEED" patch
* added TODO comments into set/get_vvmcs_virtual_safe() based on Andrew's email
v2 --> v3:
* vmwrite_safe() and vmread_safe() now return "enum vmx_insn_errno" (32-bit value)
* vmwrite_safe() is changed from always_inline to plain inline
* set_vvmcs_safe() and related functions are added to match get_vvmcs_safe()
* virtual_vmcs_vmwrite_safe() and virtual_vmcs_vmread_safe() are added in order to preserve the old behaviour for existing callers
v1 --> v2:
* Removed "ASM_FLAG_OUT" from tools/tests/x86_emulator/x86_emulate.h
* Replaced "~0UL" with "~0" for VMX_INSN_FAIL_INVALID
* Removed double underscore prefix from vmwrite_safe() and vmread_safe()
* Replaced "setb --> setc" and "sete --> setz"
* Removed "fail_" prefix from "invalid" and "valid" asm constraints
* Added "\t" to asm
* Added unlikely() for checking fail conditions
* Moved "ASM_FLAG_OUT" from lib.h to asm_defns.h
Sergey Dyasli (3):
x86/vmx: introduce VMX_INSN_SUCCEED
x86/vvmx: correctly emulate VMWRITE
x86/vvmx: correctly emulate VMREAD
xen/arch/x86/hvm/vmx/vmcs.c | 24 ++++++++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 56 ++++++++++++++++++++++++++++++++++++-- xen/include/asm-x86/hvm/vmx/vmcs.h | 5 ++++ xen/include/asm-x86/hvm/vmx/vmx.h | 4 +-- xen/include/asm-x86/hvm/vmx/vvmx.h | 16 +++++++++++ 5 files changed, 101 insertions(+), 4 deletions(-)
Signature · 7 lines
2.9.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v4 1/3 x86/vmx: introduce VMX_INSN_SUCCEED
The new value corresponds to VMsucceed status of VMX instructions. This will replace usage of literal zeroes in related functions.
Update vmfail(), vmread_safe() and vmwrite_safe().
--- xen/arch/x86/hvm/vmx/vvmx.c | 3 +++ xen/include/asm-x86/hvm/vmx/vmcs.h | 1 + xen/include/asm-x86/hvm/vmx/vmx.h | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/vmx/vvmx.c +3 -0
@@ -483,6 +483,9 @@ static void vmfail_invalid(struct cpu_user_regs *regs)static void vmfail (struct cpu_user_regs *regs,enum vmx_insn_errno errno) { +if ( errno ==VMX_INSN_SUCCEED ) +return ; +if (vcpu_nestedhvm (current).nv_vvmcxaddr !=INVALID_PADDR && errno !=VMX_INSN_FAIL_INVALID )vmfail_valid (regs, errno);
xen/include/asm-x86/hvm/vmx/vmcs.h +1 -0
@@ -514,6 +514,7 @@ enum vmcs_field {/* VM Instruction error numbers */ enum vmx_insn_errno { +VMX_INSN_SUCCEED =0 ,VMX_INSN_VMCLEAR_INVALID_PHYADDR =2 ,VMX_INSN_VMLAUNCH_NONCLEAR_VMCS =4 ,VMX_INSN_VMRESUME_NONLAUNCHED_VMCS =5 ,
xen/include/asm-x86/hvm/vmx/vmx.h +2 -2
@@ -404,7 +404,7 @@ static always_inline void __vmwrite(unsigned long field, unsigned long value)static inline enum vmx_insn_errno vmread_safe (unsigned long field,unsigned long *value) { -unsigned long ret =0 ; +unsigned long ret =VMX_INSN_SUCCEED ;bool fail_invalid, fail_valid; asm volatile ( GAS_VMX_OP("vmread %[field], %[value]\n\t",
@@ -427,7 +427,7 @@ static inline enum vmx_insn_errno vmread_safe(unsigned long field,static inline enum vmx_insn_errno vmwrite_safe (unsigned long field,unsigned long value) { -unsigned long ret =0 ; +unsigned long ret =VMX_INSN_SUCCEED ;bool fail_invalid, fail_valid; asm volatile ( GAS_VMX_OP("vmwrite %[value], %[field]\n\t",
Signature · 7 lines
2.9.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v4 1/3] x86/vmx: introduce VMX_INSN_SUCCEED
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 13.02.17 at 15:21, <[email protected]> wrote:
The new value corresponds to VMsucceed status of VMX instructions. This will replace usage of literal zeroes in related functions.
Update
vmfail(),vmread_safe()andvmwrite_safe().Signed-off-by Sergey Dyasli <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v4 1/3] x86/vmx: introduce VMX_INSN_SUCCEED
In reply to Sergey DyasliQuoted 9 lines
From: Sergey Dyasli [mailto:[email protected]]
Sent: Monday, February 13, 2017 10:21 PMThe new value corresponds to VMsucceed status of VMX instructions. This will replace usage of literal zeroes in related functions.
Update
vmfail(),vmread_safe()andvmwrite_safe().Signed-off-by Sergey Dyasli <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v4 2/3 x86/vvmx: correctly emulate VMWRITE
In reply to Sergey DyasliThere is an issue with the original __vmwrite() in nested vmx mode:
emulation of a guest's VMWRITE with invalid arguments leads to BUG().
Fix this by using vmwrite_safe() and reporting any kind of VMfail back to the guest.
A new safe versions of set_vvmcs() macro and related functions are introduced because of new function signatures and lots of existing users.
---
v3 --> v4:
* VMX_INSN_SUCCEED is used instead of 0
* added TODO comment into set_vvmcs_virtual_safe() based on Andrew's email
xen/arch/x86/hvm/vmx/vmcs.c | 12 ++++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 27 ++++++++++++++++++++++++++- xen/include/asm-x86/hvm/vmx/vmcs.h | 2 ++ xen/include/asm-x86/hvm/vmx/vvmx.h | 8 ++++++++ 4 files changed, 48 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/vmx/vmcs.c +12 -0
@@ -950,6 +950,18 @@ void virtual_vmcs_vmwrite(const struct vcpu *v, u32 vmcs_encoding, u64 val)virtual_vmcs_exit (v); } +enum vmx_insn_errno virtual_vmcs_vmwrite_safe (const struct vcpu *v, +u32 vmcs_encoding,u64 val) +{ +enum vmx_insn_errno ret; + +virtual_vmcs_enter (v); + ret =vmwrite_safe (vmcs_encoding, val); +virtual_vmcs_exit (v); + +return ret; +} +/* * This function is only called in a vCPU's initialization phase, * so we can update the posted-interrupt descriptor in non-atomic way.
xen/arch/x86/hvm/vmx/vvmx.c +26 -1
@@ -305,6 +305,25 @@ void set_vvmcs_real(const struct vcpu *v, u32 encoding, u64 val)virtual_vmcs_vmwrite (v, encoding, val); } +enum vmx_insn_errno set_vvmcs_virtual_safe (void *vvmcs,u32 encoding,u64 val) +{ +set_vvmcs_virtual (vvmcs, encoding, val); + +/* +* TODO: This should not always succeed. Fields and values need to be +* audited against the features offered to the guest in the VT-x MSRs. +* This should be fixed when the MSR levelling work is started, at which +* point there will be a cpuid_policy-like object. +*/ +return VMX_INSN_SUCCEED ; +} + +enum vmx_insn_errno set_vvmcs_real_safe (const struct vcpu *v,u32 encoding, +u64 val) +{ +return virtual_vmcs_vmwrite_safe (v, encoding, val); +} +static unsigned long reg_read (struct cpu_user_regs *regs,enum vmx_regs_enc index) {
@@ -1740,13 +1759,19 @@ int nvmx_handle_vmwrite(struct cpu_user_regs *regs)unsigned long operand;u64 vmcs_encoding;bool_t okay =1 ; +enum vmx_insn_errno err;if (decode_vmx_inst (regs, &decode, &operand,0 ) !=X86EMUL_OKAY )return X86EMUL_EXCEPTION ; vmcs_encoding =reg_read (regs, decode.reg2); -set_vvmcs (v, vmcs_encoding, operand); + err =set_vvmcs_safe (v, vmcs_encoding, operand); +if ( err !=VMX_INSN_SUCCEED ) + { +vmfail (regs, err); +return X86EMUL_OKAY ; + }switch ( vmcs_encoding & ~VMCS_HIGH (0 ) ) {
xen/include/asm-x86/hvm/vmx/vmcs.h +2 -0
@@ -539,6 +539,8 @@ void virtual_vmcs_enter(const struct vcpu *);void virtual_vmcs_exit (const struct vcpu *);u64 virtual_vmcs_vmread (const struct vcpu *,u32 encoding);void virtual_vmcs_vmwrite (const struct vcpu *,u32 encoding,u64 val); +enum vmx_insn_errno virtual_vmcs_vmwrite_safe (const struct vcpu *v, +u32 vmcs_encoding,u64 val);static inline int vmx_add_guest_msr (u32 msr) {
xen/include/asm-x86/hvm/vmx/vvmx.h +8 -0
@@ -183,6 +183,9 @@ u64 get_vvmcs_virtual(void *vvmcs, u32 encoding);u64 get_vvmcs_real (const struct vcpu *,u32 encoding);void set_vvmcs_virtual (void *vvmcs,u32 encoding,u64 val);void set_vvmcs_real (const struct vcpu *,u32 encoding,u64 val); +enum vmx_insn_errno set_vvmcs_virtual_safe (void *vvmcs,u32 encoding,u64 val); +enum vmx_insn_errno set_vvmcs_real_safe (const struct vcpu *,u32 encoding, +u64 val);#define get_vvmcs (vcpu, encoding) \ (cpu_has_vmx_vmcs_shadowing ? \
@@ -194,6 +197,11 @@ void set_vvmcs_real(const struct vcpu *, u32 encoding, u64 val); set_vvmcs_real(vcpu, encoding, val) : \ set_vvmcs_virtual(vcpu_nestedhvm(vcpu).nv_vvmcx, encoding, val)) +#define set_vvmcs_safe (vcpu, encoding, val) \ + (cpu_has_vmx_vmcs_shadowing ? \ + set_vvmcs_real_safe(vcpu, encoding, val) : \ + set_vvmcs_virtual_safe(vcpu_nestedhvm(vcpu).nv_vvmcx, encoding, val)) +uint64_t get_shadow_eptp (struct vcpu *v);void nvmx_destroy_vmcs (struct vcpu *v);
Signature · 7 lines
2.9.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v4 2/3] x86/vvmx: correctly emulate VMWRITE
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 13.02.17 at 15:21, <[email protected]> wrote:
There is an issue with the original
__vmwrite()in nested vmx mode:
emulation of a guest's VMWRITE with invalid arguments leads toBUG().
Quoted 8 lines
Fix this by using
vmwrite_safe()and reporting any kind of VMfail back to the guest.A new safe versions of
set_vvmcs()macro and related functions are introduced because of new function signatures and lots of existing users.Signed-off-by Sergey Dyasli <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v4 2/3] x86/vvmx: correctly emulate VMWRITE
In reply to Sergey DyasliQuoted 6 lines
From: Sergey Dyasli [mailto:[email protected]]
Sent: Monday, February 13, 2017 10:21 PMThere is an issue with the original
__vmwrite()in nested vmx mode:
emulation of a guest's VMWRITE with invalid arguments leads toBUG().
Quoted 8 lines
Fix this by using
vmwrite_safe()and reporting any kind of VMfail back to the guest.A new safe versions of
set_vvmcs()macro and related functions are introduced because of new function signatures and lots of existing users.Signed-off-by Sergey Dyasli <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v4 3/3 x86/vvmx: correctly emulate VMREAD
In reply to Sergey DyasliThere is an issue with the original __vmread() in nested vmx mode:
emulation of a guest's VMREAD with invalid arguments leads to BUG().
Fix this by using vmread_safe() and reporting any kind of VMfail back to the guest.
A new safe versions of get_vvmcs() macro and related functions are introduced because of new function signatures and lots of existing users.
---
v3 --> v4:
* VMX_INSN_SUCCEED is used instead of 0
* added TODO comment into get_vvmcs_virtual_safe() based on Andrew's email
xen/arch/x86/hvm/vmx/vmcs.c | 12 ++++++++++++ xen/arch/x86/hvm/vmx/vvmx.c | 26 +++++++++++++++++++++++++- xen/include/asm-x86/hvm/vmx/vmcs.h | 2 ++ xen/include/asm-x86/hvm/vmx/vvmx.h | 8 ++++++++ 4 files changed, 47 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/vmx/vmcs.c +12 -0
@@ -943,6 +943,18 @@ u64 virtual_vmcs_vmread(const struct vcpu *v, u32 vmcs_encoding)return res; } +enum vmx_insn_errno virtual_vmcs_vmread_safe (const struct vcpu *v, +u32 vmcs_encoding,u64 *val) +{ +enum vmx_insn_errno ret; + +virtual_vmcs_enter (v); + ret =vmread_safe (vmcs_encoding, val); +virtual_vmcs_exit (v); + +return ret; +} +void virtual_vmcs_vmwrite (const struct vcpu *v,u32 vmcs_encoding,u64 val) {virtual_vmcs_enter (v);
xen/arch/x86/hvm/vmx/vvmx.c +25 -1
@@ -264,6 +264,25 @@ u64 get_vvmcs_real(const struct vcpu *v, u32 encoding)return virtual_vmcs_vmread (v, encoding); } +enum vmx_insn_errno get_vvmcs_virtual_safe (void *vvmcs,u32 encoding,u64 *val) +{ + *val =get_vvmcs_virtual (vvmcs, encoding); + +/* +* TODO: This should not always succeed. Fields and values need to be +* audited against the features offered to the guest in the VT-x MSRs. +* This should be fixed when the MSR levelling work is started, at which +* point there will be a cpuid_policy-like object. +*/ +return VMX_INSN_SUCCEED ; +} + +enum vmx_insn_errno get_vvmcs_real_safe (const struct vcpu *v,u32 encoding, +u64 *val) +{ +return virtual_vmcs_vmread_safe (v, encoding, val); +} +void set_vvmcs_virtual (void *vvmcs,u32 vmcs_encoding,u64 val) {union vmcs_encoding enc;
@@ -1733,7 +1752,12 @@ int nvmx_handle_vmread(struct cpu_user_regs *regs)if ( rc !=X86EMUL_OKAY )return rc; - value =get_vvmcs (v,reg_read (regs, decode.reg2)); + rc =get_vvmcs_safe (v,reg_read (regs, decode.reg2), &value); +if ( rc !=VMX_INSN_SUCCEED ) + { +vmfail (regs, rc); +return X86EMUL_OKAY ; + }switch ( decode.type ) {case VMX_INST_MEMREG_TYPE_MEMORY :
xen/include/asm-x86/hvm/vmx/vmcs.h +2 -0
@@ -538,6 +538,8 @@ int vmx_check_msr_bitmap(unsigned long *msr_bitmap, u32 msr, int access_type);void virtual_vmcs_enter (const struct vcpu *);void virtual_vmcs_exit (const struct vcpu *);u64 virtual_vmcs_vmread (const struct vcpu *,u32 encoding); +enum vmx_insn_errno virtual_vmcs_vmread_safe (const struct vcpu *v, +u32 vmcs_encoding,u64 *val);void virtual_vmcs_vmwrite (const struct vcpu *,u32 encoding,u64 val);enum vmx_insn_errno virtual_vmcs_vmwrite_safe (const struct vcpu *v,u32 vmcs_encoding,u64 val);
xen/include/asm-x86/hvm/vmx/vvmx.h +8 -0
@@ -183,6 +183,9 @@ u64 get_vvmcs_virtual(void *vvmcs, u32 encoding);u64 get_vvmcs_real (const struct vcpu *,u32 encoding);void set_vvmcs_virtual (void *vvmcs,u32 encoding,u64 val);void set_vvmcs_real (const struct vcpu *,u32 encoding,u64 val); +enum vmx_insn_errno get_vvmcs_virtual_safe (void *vvmcs,u32 encoding,u64 *val); +enum vmx_insn_errno get_vvmcs_real_safe (const struct vcpu *,u32 encoding, +u64 *val);enum vmx_insn_errno set_vvmcs_virtual_safe (void *vvmcs,u32 encoding,u64 val);enum vmx_insn_errno set_vvmcs_real_safe (const struct vcpu *,u32 encoding,u64 val);
@@ -197,6 +200,11 @@ enum vmx_insn_errno set_vvmcs_real_safe(const struct vcpu *, u32 encoding, set_vvmcs_real(vcpu, encoding, val) : \ set_vvmcs_virtual(vcpu_nestedhvm(vcpu).nv_vvmcx, encoding, val)) +#define get_vvmcs_safe (vcpu, encoding, val) \ + (cpu_has_vmx_vmcs_shadowing ? \ + get_vvmcs_real_safe(vcpu, encoding, val) : \ + get_vvmcs_virtual_safe(vcpu_nestedhvm(vcpu).nv_vvmcx, encoding, val)) +#define set_vvmcs_safe (vcpu, encoding, val) \ (cpu_has_vmx_vmcs_shadowing ? \ set_vvmcs_real_safe(vcpu, encoding, val) : \
Signature · 7 lines
2.9.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v4 3/3] x86/vvmx: correctly emulate VMREAD
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 13.02.17 at 15:21, <[email protected]> wrote:
There is an issue with the original
__vmread()in nested vmx mode:
emulation of a guest's VMREAD with invalid arguments leads toBUG().
Quoted 8 lines
Fix this by using
vmread_safe()and reporting any kind of VMfail back to the guest.A new safe versions of
get_vvmcs()macro and related functions are introduced because of new function signatures and lots of existing users.Signed-off-by Sergey Dyasli <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v4 3/3] x86/vvmx: correctly emulate VMREAD
In reply to Sergey DyasliQuoted 6 lines
From: Sergey Dyasli [mailto:[email protected]]
Sent: Monday, February 13, 2017 10:21 PMThere is an issue with the original
__vmread()in nested vmx mode:
emulation of a guest's VMREAD with invalid arguments leads toBUG().
Quoted 8 lines
Fix this by using
vmread_safe()and reporting any kind of VMfail back to the guest.A new safe versions of
get_vvmcs()macro and related functions are introduced because of new function signatures and lots of existing users.Signed-off-by Sergey Dyasli <[email protected]>
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 4 lines · Sergey Dyasli
Sergey Dyasli (3):
x86/vmx: introduceVMX_INSN_SUCCEED
x86/vvmx: correctly emulate VMWRITE
x86/vvmx: correctly emulate VMREAD
Committed, thanks.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel