From: Andrew Cooper Date: Mon, 19 Feb 2018 10:40:20 +0000 (+0000) Subject: x86/pv: Avoid leaking other guests' MSR_TSC_AUX values into PV context X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=cc0e45db277922b5723a7b1d9657d6f744230cf1;p=people%2Froyger%2Fxen.git x86/pv: Avoid leaking other guests' MSR_TSC_AUX values into PV context If the CPU pipeline supports RDTSCP or RDPID, a guest can observe the value in MSR_TSC_AUX, irrespective of whether the relevant CPUID features are advertised/hidden. At the moment, paravirt_ctxt_switch_to() only writes to MSR_TSC_AUX if TSC_MODE_PVRDTSCP mode is enabled, but this is not the default mode. Therefore, default PV guests can read the value from a previously scheduled HVM vcpu, or TSC_MODE_PVRDTSCP-enabled PV guest. Alter the PV path to always write to MSR_TSC_AUX, using 0 in the common case. To amortise overhead cost, introduce wrmsr_tsc_aux() which performs a lazy update of the MSR, and use this function consistently across the codebase. Signed-off-by: Andrew Cooper Reviewed-by: Roger Pau Monné Reviewed-by: Wei Liu Acked-by: Jan Beulich Reviewed-by: Kevin Tian Reviewed-by: Boris Ostrovsky --- diff --git a/xen/arch/x86/domain.c b/xen/arch/x86/domain.c index f93327b0a2..9c3527f077 100644 --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -1533,9 +1533,9 @@ void paravirt_ctxt_switch_to(struct vcpu *v) if ( unlikely(v->arch.debugreg[7] & DR7_ACTIVE_MASK) ) activate_debugregs(v); - if ( (v->domain->arch.tsc_mode == TSC_MODE_PVRDTSCP) && - boot_cpu_has(X86_FEATURE_RDTSCP) ) - write_rdtscp_aux(v->domain->arch.incarnation); + if ( cpu_has_rdtscp ) + wrmsr_tsc_aux(v->domain->arch.tsc_mode == TSC_MODE_PVRDTSCP + ? v->domain->arch.incarnation : 0); } /* Update per-VCPU guest runstate shared memory area (if registered). */ diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 5d3921051b..0539551851 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -3579,7 +3579,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content, v->arch.hvm_vcpu.msr_tsc_aux = (uint32_t)msr_content; if ( cpu_has_rdtscp && (v->domain->arch.tsc_mode != TSC_MODE_PVRDTSCP) ) - wrmsrl(MSR_TSC_AUX, (uint32_t)msr_content); + wrmsr_tsc_aux(msr_content); break; case MSR_IA32_APICBASE: diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 1bc95da12c..277dcffe17 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -1099,7 +1099,7 @@ static void svm_ctxt_switch_to(struct vcpu *v) svm_tsc_ratio_load(v); if ( cpu_has_rdtscp ) - wrmsrl(MSR_TSC_AUX, hvm_msr_tsc_aux(v)); + wrmsr_tsc_aux(hvm_msr_tsc_aux(v)); } static void noreturn svm_do_resume(struct vcpu *v) diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c index 5cd689e823..31acb0e19e 100644 --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -618,7 +618,7 @@ static void vmx_restore_guest_msrs(struct vcpu *v) } if ( cpu_has_rdtscp ) - wrmsrl(MSR_TSC_AUX, hvm_msr_tsc_aux(v)); + wrmsr_tsc_aux(hvm_msr_tsc_aux(v)); } void vmx_update_cpu_exec_control(struct vcpu *v) diff --git a/xen/arch/x86/msr.c b/xen/arch/x86/msr.c index 7aaa2b0406..8ae3b4e616 100644 --- a/xen/arch/x86/msr.c +++ b/xen/arch/x86/msr.c @@ -24,6 +24,8 @@ #include #include +DEFINE_PER_CPU(uint32_t, tsc_aux); + struct msr_domain_policy __read_mostly raw_msr_domain_policy, __read_mostly host_msr_domain_policy, __read_mostly hvm_max_msr_domain_policy, diff --git a/xen/include/asm-x86/msr.h b/xen/include/asm-x86/msr.h index 94c142289b..a5072a2d5e 100644 --- a/xen/include/asm-x86/msr.h +++ b/xen/include/asm-x86/msr.h @@ -115,8 +115,6 @@ static inline uint64_t rdtsc_ordered(void) __write_tsc(val); \ }) -#define write_rdtscp_aux(val) wrmsr(MSR_TSC_AUX, (val), 0) - #define rdpmc(counter,low,high) \ __asm__ __volatile__("rdpmc" \ : "=a" (low), "=d" (high) \ @@ -210,6 +208,20 @@ static inline void write_efer(uint64_t val) DECLARE_PER_CPU(u32, ler_msr); +DECLARE_PER_CPU(uint32_t, tsc_aux); + +/* Lazy update of MSR_TSC_AUX */ +static inline void wrmsr_tsc_aux(uint32_t val) +{ + uint32_t *this_tsc_aux = &this_cpu(tsc_aux); + + if ( *this_tsc_aux != val ) + { + wrmsr(MSR_TSC_AUX, val, 0); + *this_tsc_aux = val; + } +} + /* MSR policy object for shared per-domain MSRs */ struct msr_domain_policy {