x86emul: conditionally clear BNDn for branches
RFC PATCH 1/1 x86emul: conditionally clear BNDn for branches
Considering that we surface MPX to HVM guests, instructions we emulate should also correctly deal with MPX state. While for now BND* instructions don't get emulated, the effect of branches (which we do emulate) without BND prefix should be taken care of.
---
RFC reasons:
- I haven't been able to find a layout specification for the BNDCSR area in the SDM, therefore struct xstate_bndcsr's layout is guessed.
- The SDM volumes contradict one another regarding short conditional branch behavior: SDM Vol 1 says they init BNDn (except for J{,E,R}CXZ), while Vol 2 says BND only affects near branches. The code for now assumes only near ones are affected.
- The SDM leaves open whether only taken Jcc will cause BNDn init, or also not taken ones. The code for now assumes only taken ones do.
- It is also left unclear whether addressing forms without base address register are valid for BNDMK (the instruction page references SRCMEM.base without indicating what this means in that specific case). The code uses an explicit base register for that reason (which also yields slightly smaller code).
tools/tests/x86_emulator/x86_emulate.c +4 -0
@@ -10,11 +10,15 @@ typedef bool bool_t;#define is_canonical_address (x) (((int64_t)(x) >> 47) == ((int64_t)(x) >> 63)) +#define EFER_SCE (1 << 0) +#define EFER_LMA (1 << 10) +#define BUG () abort()#define ASSERT assert#define cpu_has_amd_erratum (nr) 0#define mark_regs_dirty (r) ((void)(r)) +#define read_bndcfgu () 0#define __packed __attribute__((packed))
xen/arch/x86/Rules.mk +1 -0
@@ -14,6 +14,7 @@ $(call as-insn-check,CFLAGS,CC,"vmcall", $(call as-insn-check,CFLAGS,CC,"vmcall",-DHAVE_GAS_VMX) $(call as-insn-check,CFLAGS,CC,"invept (%rax)$$(comma)%rax",-DHAVE_GAS_EPT) $(call as-insn-check,CFLAGS,CC,"rdfsbase %rax",-DHAVE_GAS_FSGSBASE) +$(call as-insn-check,CFLAGS,CC,"bndmov %bnd0$$(comma)%bnd0",-DHAVE_GAS_MPX) $(call as-insn-check,CFLAGS,CC,".equ \"x\"$$(comma)1", \ -U__OBJECT_LABEL__ -DHAVE_GAS_QUOTED_SYM \ '-D__OBJECT_LABEL__=$(subst $(BASEDIR)/,,$(CURDIR))/$$@')
xen/arch/x86/domain.c +2 -0
@@ -421,6 +421,8 @@ int vcpu_initialise(struct vcpu *v)vmce_init_vcpu (v); } +else if ( (rc =xstate_alloc_save_area (v)) !=0 ) +return rc;spin_lock_init (&v->arch.vpmu.vpmu_lock);
xen/arch/x86/hvm/vmx/vmx.c +5 -0
@@ -2623,6 +2623,11 @@ static int vmx_msr_read_intercept(unsigncase MSR_IA32_DEBUGCTLMSR :__vmread (GUEST_IA32_DEBUGCTL , msr_content);break ; +case MSR_IA32_BNDCFGS : +if ( !cpu_has_mpx ) + goto gp_fault; +__vmread (GUEST_BNDCFGS , msr_content); +break ;case IA32_FEATURE_CONTROL_MSR : case MSR_IA32_VMX_BASIC...MSR_IA32_VMX_TRUE_ENTRY_CTLS:if ( !nvmx_msr_read_intercept (msr, msr_content) )
xen/arch/x86/x86_emulate.c +1 -0
@@ -13,6 +13,7 @@#include <asm/x86_emulate.h> #include <asm/asm_defns.h> /* mark_regs_dirty() */ #include <asm/processor.h> /* current_cpu_info */ +#include <asm/xstate.h> /* read_bndcfgu() */ #include <asm/amd.h> /* cpu_has_amd_erratum() */ /* Avoid namespace pollution. */
xen/arch/x86/x86_emulate/x86_emulate.c +45 -2
91 lines not on this page · raw message
xen/arch/x86/xstate.c +55 -3
76 lines not on this page · raw message
xen/include/asm-x86/xstate.h +30 -10
@@ -26,16 +26,30 @@#define XSAVE_HDR_OFFSET FXSAVE_SIZE#define XSTATE_AREA_MIN_SIZE (FXSAVE_SIZE + XSAVE_HDR_SIZE) -#define XSTATE_FP (1ULL << 0) -#define XSTATE_SSE (1ULL << 1) -#define XSTATE_YMM (1ULL << 2) -#define XSTATE_BNDREGS (1ULL << 3) -#define XSTATE_BNDCSR (1ULL << 4) -#define XSTATE_OPMASK (1ULL << 5) -#define XSTATE_ZMM (1ULL << 6) -#define XSTATE_HI_ZMM (1ULL << 7) -#define XSTATE_PKRU (1ULL << 9) -#define XSTATE_LWP (1ULL << 62)/* AMD lightweight profiling */ +#define _XSTATE_FP 0 +#define _XSTATE_SSE 1 +#define _XSTATE_YMM 2 +#define _XSTATE_BNDREGS 3 +#define _XSTATE_BNDCSR 4 +#define _XSTATE_OPMASK 5 +#define _XSTATE_ZMM 6 +#define _XSTATE_HI_ZMM 7 +#define _XSTATE_PKRU 9 +#define _XSTATE_LWP 62/* AMD lightweight profiling */ + +#define XSTATE (n) (1ULL << _XSTATE_##n) + +#define XSTATE_FP XSTATE(FP) +#define XSTATE_SSE XSTATE(SSE) +#define XSTATE_YMM XSTATE(YMM) +#define XSTATE_BNDREGS XSTATE(BNDREGS) +#define XSTATE_BNDCSR XSTATE(BNDCSR) +#define XSTATE_OPMASK XSTATE(OPMASK) +#define XSTATE_ZMM XSTATE(ZMM) +#define XSTATE_HI_ZMM XSTATE(HI_ZMM) +#define XSTATE_PKRU XSTATE(PKRU) +#define XSTATE_LWP XSTATE(LWP) +#define XSTATE_FP_SSE (XSTATE_FP | XSTATE_SSE)#define XCNTXT_MASK (XSTATE_FP | XSTATE_SSE | XSTATE_YMM | XSTATE_OPMASK | \ XSTATE_ZMM | XSTATE_HI_ZMM | XSTATE_NONLAZY)
@@ -87,11 +101,17 @@ struct __attribute__((aligned (64))) xsachar data[];/* Variable layout states */ }; +struct xstate_bndcsr { +uint64_t bndcfgu; +uint64_t bndstatus; +}; +/* extended state operations */ bool_t __must_check set_xcr0 (u64 xfeatures);uint64_t get_xcr0 (void );void set_msr_xss (u64 xss);uint64_t get_msr_xss (void ); +uint64_t read_bndcfgu (void );void xsave (struct vcpu *v,uint64_t mask);void xrstor (struct vcpu *v,uint64_t mask);bool_t xsave_enabled (const struct vcpu *v);
x86emul: conditionally clear BNDn for branches
Considering that we surface MPX to HVM guests, instructions we emulate should also correctly deal with MPX state. While for now BND* instructions don't get emulated, the effect of branches (which we do emulate) without BND prefix should be taken care of.
---
RFC reasons:
- I haven't been able to find a layout specification for the BNDCSR area in the SDM, therefore struct xstate_bndcsr's layout is guessed.
- The SDM volumes contradict one another regarding short conditional branch behavior: SDM Vol 1 says they init BNDn (except for J{,E,R}CXZ), while Vol 2 says BND only affects near branches. The code for now assumes only near ones are affected.
- The SDM leaves open whether only taken Jcc will cause BNDn init, or also not taken ones. The code for now assumes only taken ones do.
- It is also left unclear whether addressing forms without base address register are valid for BNDMK (the instruction page references SRCMEM.base without indicating what this means in that specific case). The code uses an explicit base register for that reason (which also yields slightly smaller code).
tools/tests/x86_emulator/x86_emulate.c +4 -0
@@ -10,11 +10,15 @@ typedef bool bool_t;#define is_canonical_address (x) (((int64_t)(x) >> 47) == ((int64_t)(x) >> 63)) +#define EFER_SCE (1 << 0) +#define EFER_LMA (1 << 10) +#define BUG () abort()#define ASSERT assert#define cpu_has_amd_erratum (nr) 0#define mark_regs_dirty (r) ((void)(r)) +#define read_bndcfgu () 0#define __packed __attribute__((packed))
xen/arch/x86/Rules.mk +1 -0
@@ -14,6 +14,7 @@ $(call as-insn-check,CFLAGS,CC,"vmcall", $(call as-insn-check,CFLAGS,CC,"vmcall",-DHAVE_GAS_VMX) $(call as-insn-check,CFLAGS,CC,"invept (%rax)$$(comma)%rax",-DHAVE_GAS_EPT) $(call as-insn-check,CFLAGS,CC,"rdfsbase %rax",-DHAVE_GAS_FSGSBASE) +$(call as-insn-check,CFLAGS,CC,"bndmov %bnd0$$(comma)%bnd0",-DHAVE_GAS_MPX) $(call as-insn-check,CFLAGS,CC,".equ \"x\"$$(comma)1", \ -U__OBJECT_LABEL__ -DHAVE_GAS_QUOTED_SYM \ '-D__OBJECT_LABEL__=$(subst $(BASEDIR)/,,$(CURDIR))/$$@')
xen/arch/x86/domain.c +2 -0
@@ -421,6 +421,8 @@ int vcpu_initialise(struct vcpu *v)vmce_init_vcpu (v); } +else if ( (rc =xstate_alloc_save_area (v)) !=0 ) +return rc;spin_lock_init (&v->arch.vpmu.vpmu_lock);
xen/arch/x86/hvm/vmx/vmx.c +5 -0
@@ -2623,6 +2623,11 @@ static int vmx_msr_read_intercept(unsigncase MSR_IA32_DEBUGCTLMSR :__vmread (GUEST_IA32_DEBUGCTL , msr_content);break ; +case MSR_IA32_BNDCFGS : +if ( !cpu_has_mpx ) + goto gp_fault; +__vmread (GUEST_BNDCFGS , msr_content); +break ;case IA32_FEATURE_CONTROL_MSR : case MSR_IA32_VMX_BASIC...MSR_IA32_VMX_TRUE_ENTRY_CTLS:if ( !nvmx_msr_read_intercept (msr, msr_content) )
xen/arch/x86/x86_emulate.c +1 -0
@@ -13,6 +13,7 @@#include <asm/x86_emulate.h> #include <asm/asm_defns.h> /* mark_regs_dirty() */ #include <asm/processor.h> /* current_cpu_info */ +#include <asm/xstate.h> /* read_bndcfgu() */ #include <asm/amd.h> /* cpu_has_amd_erratum() */ /* Avoid namespace pollution. */
xen/arch/x86/x86_emulate/x86_emulate.c +45 -2
91 lines not on this page · raw message
xen/arch/x86/xstate.c +55 -3
76 lines not on this page · raw message
xen/include/asm-x86/xstate.h +30 -10
@@ -26,16 +26,30 @@#define XSAVE_HDR_OFFSET FXSAVE_SIZE#define XSTATE_AREA_MIN_SIZE (FXSAVE_SIZE + XSAVE_HDR_SIZE) -#define XSTATE_FP (1ULL << 0) -#define XSTATE_SSE (1ULL << 1) -#define XSTATE_YMM (1ULL << 2) -#define XSTATE_BNDREGS (1ULL << 3) -#define XSTATE_BNDCSR (1ULL << 4) -#define XSTATE_OPMASK (1ULL << 5) -#define XSTATE_ZMM (1ULL << 6) -#define XSTATE_HI_ZMM (1ULL << 7) -#define XSTATE_PKRU (1ULL << 9) -#define XSTATE_LWP (1ULL << 62)/* AMD lightweight profiling */ +#define _XSTATE_FP 0 +#define _XSTATE_SSE 1 +#define _XSTATE_YMM 2 +#define _XSTATE_BNDREGS 3 +#define _XSTATE_BNDCSR 4 +#define _XSTATE_OPMASK 5 +#define _XSTATE_ZMM 6 +#define _XSTATE_HI_ZMM 7 +#define _XSTATE_PKRU 9 +#define _XSTATE_LWP 62/* AMD lightweight profiling */ + +#define XSTATE (n) (1ULL << _XSTATE_##n) + +#define XSTATE_FP XSTATE(FP) +#define XSTATE_SSE XSTATE(SSE) +#define XSTATE_YMM XSTATE(YMM) +#define XSTATE_BNDREGS XSTATE(BNDREGS) +#define XSTATE_BNDCSR XSTATE(BNDCSR) +#define XSTATE_OPMASK XSTATE(OPMASK) +#define XSTATE_ZMM XSTATE(ZMM) +#define XSTATE_HI_ZMM XSTATE(HI_ZMM) +#define XSTATE_PKRU XSTATE(PKRU) +#define XSTATE_LWP XSTATE(LWP) +#define XSTATE_FP_SSE (XSTATE_FP | XSTATE_SSE)#define XCNTXT_MASK (XSTATE_FP | XSTATE_SSE | XSTATE_YMM | XSTATE_OPMASK | \ XSTATE_ZMM | XSTATE_HI_ZMM | XSTATE_NONLAZY)
@@ -87,11 +101,17 @@ struct __attribute__((aligned (64))) xsachar data[];/* Variable layout states */ }; +struct xstate_bndcsr { +uint64_t bndcfgu; +uint64_t bndstatus; +}; +/* extended state operations */ bool_t __must_check set_xcr0 (u64 xfeatures);uint64_t get_xcr0 (void );void set_msr_xss (u64 xss);uint64_t get_msr_xss (void ); +uint64_t read_bndcfgu (void );void xsave (struct vcpu *v,uint64_t mask);void xrstor (struct vcpu *v,uint64_t mask);bool_t xsave_enabled (const struct vcpu *v);
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel