KVM: x86: Clean up MSR access/failure handling
Sean Christopherson
maintainerv2 cover KVM: x86: Clean up MSR access/failure handling
Rework KVM's MSR access handling, and more specific the handling of failures, to begin the march towards removing host_initiated exemptions for CPUID checks, e.g. to eventually turn code like this:
if (!msr_info->host_initiated && !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) return 1;
into
if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) return KVM_MSR_RET_UNSUPPORTED;
For all intents and purposes, KVM already requires setting guest CPUID before setting MSRs, as there are multiple MSR flows that simply cannot work if CPUID isn't in place.
But because KVM's ABI is that userspace is allowed to save/restore MSRs that are advertised to usersepace regardless of the vCPU CPUID model, KVM has ended up with code like the above where KVM unconditionally allows host accesses.
The idea here is to funnel all MSR accesses through a single helper so that KVM can make the "host_initiated" exception in a single location based on KVM_MSR_RET_UNSUPPORTED, i.e. so that KVM doesn't need one-off checks for every MSR, which is especially problematic for CET where a Venn diagram is needed to map CET MSR existence to CPUID feature bits.
This series doesn't actually remove the existing host_initiated checks. I *really* wanted to do that here, but removing all the existing checks is non-trivial and has a high chance of subtly breaking userspace. I still want to eventually get there, but it needs to be a slower, more thoughtful process.
For now, the goal is to allow new features to omit the host_initiated checks without creating a weird userspace ABI, e.g to simplify the aforementioned CET support.
v2:
- Rebase (really the only reason I posted v2).
- Collect reviews. [Weijiang]
v1:
- https://lore.kernel.org/all/[email protected]
Sean Christopherson (10):
KVM: SVM: Disallow guest from changing userspace's MSR_AMD64_DE_CFG
value
KVM: x86: Move MSR_TYPE_{R,W,RW} values from VMX to x86, as enums
KVM: x86: Rename KVM_MSR_RET_INVALID to KVM_MSR_RET_UNSUPPORTED
KVM: x86: Refactor kvm_x86_ops.get_msr_feature() to avoid
kvm_msr_entry
KVM: x86: Rename get_msr_feature() APIs to get_feature_msr()
KVM: x86: Refactor kvm_get_feature_msr() to avoid struct kvm_msr_entry
KVM: x86: Funnel all fancy MSR return value handling into a common
helper
KVM: x86: Hoist x86.c's global msr_* variables up above
kvm_do_msr_access()
KVM: x86: Suppress failures on userspace access to advertised,
unsupported MSRs
KVM: x86: Suppress userspace access failures on unsupported,
"emulated" MSRs
arch/x86/include/asm/kvm-x86-ops.h | 2 +- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/svm.c | 29 +- arch/x86/kvm/vmx/main.c | 2 +- arch/x86/kvm/vmx/vmx.c | 8 +- arch/x86/kvm/vmx/vmx.h | 4 - arch/x86/kvm/vmx/x86_ops.h | 2 +- arch/x86/kvm/x86.c | 513 ++++++++++++++--------------- arch/x86/kvm/x86.h | 21 +- 9 files changed, 294 insertions(+), 289 deletions(-)
base-commit: 332d2c1d713e232e163386c35a3ba0c1b90df83f
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Sean Christopherson
maintainerPATCH v2 1/10 KVM: SVM: Disallow guest from changing userspace's MSR_AMD64_DE_CFG value
Inject a #GP if the guest attempts to change MSR_AMD64_DE_CFG from its *current* value, not if the guest attempts to write a value other than KVM's set of supported bits. As per the comment and the changelog of the original code, the intent is to effectively make MSR_AMD64_DE_CFG read- only for the guest.
Opportunistically use a more conventional equality check instead of an exclusive-OR check to detect attempts to change bits.
--- arch/x86/kvm/svm/svm.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm.c +7 -2
@@ -3189,8 +3189,13 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)if (data & ~msr_entry.data)return 1 ; -/* Don't allow the guest to change a bit, #GP */ -if (!msr->host_initiated && (data ^ msr_entry.data)) +/* +* Don't let the guest change the host-programmed value. The +* MSR is very model specific, i.e. contains multiple bits that +* are completely unknown to KVM, and the one bit known to KVM +* is simply a reflection of hardware capatibilies. +*/ +if (!msr->host_initiated && data != svm->msr_decfg)return 1 ; svm->msr_decfg = data;
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Re: [PATCH v2 01/10] KVM: SVM: Disallow guest from changing userspace's MSR_AMD64_DE_CFG value
Quoted 6 lines · Sean Christopherson
Inject a #GP if the guest attempts to change
MSR_AMD64_DE_CFGfrom its *current* value, not if the guest attempts to write a value other than KVM's set of supported bits. As per the comment and the changelog of the original code, the intent is to effectively makeMSR_AMD64_DE_CFGread- only for the guest.
Quoted 6 lines
Opportunistically use a more conventional equality check instead of an exclusive-OR check to detect attempts to change bits.
Fixes d1d93fa90f1a ("KVM: SVM: Add MSR-based feature support for serializing LFENCE")Cc Tom Lendacky <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
(one spelling correction below)
Quoted 9 lines
--- arch/x86/kvm/svm/svm.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)arch/x86/kvm/svm/svm.c +0
-0
arch/x86/kvm/svm/svm.c static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) · line 3196
Quoted 10 lines
arch/x86/kvm/svm/svm.c +5
-2@@ -3189,8 +3189,13 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)if (data & ~msr_entry.data)return 1 ; -/* Don't allow the guest to change a bit, #GP */ -if (!msr->host_initiated && (data ^ msr_entry.data)) +/* +* Don't let the guest change the host-programmed value. The +* MSR is very model specific, i.e. contains multiple bits that +* are completely unknown to KVM, and the one bit known to KVM +* is simply a reflection of hardware capatibilies.
s/capatibilies/capabilities/
Quoted 5 lines
arch/x86/kvm/svm/svm.c +2
-0+*/ +if (!msr->host_initiated && data != svm->msr_decfg)return 1 ; svm->msr_decfg = data;
Sean Christopherson
maintainerPATCH v2 2/10 KVM: x86: Move MSR_TYPE_{R,W,RW} values from VMX to x86, as enums
In reply to Sean ChristophersonMove VMX's MSR_TYPE_{R,W,RW} #defines to x86.h, as enums, so that they can be used by common x86 code, e.g. instead of doing "bool write".
Opportunistically tweak the definitions to make it more obvious that the values are bitmasks, not arbitrary ascending values.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.h | 4 ---- arch/x86/kvm/x86.h | 6 ++++++ 2 files changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/vmx.h +0 -4
@@ -17,10 +17,6 @@#include "run_flags.h" #include "../mmu.h" -#define MSR_TYPE_R 1 -#define MSR_TYPE_W 2 -#define MSR_TYPE_RW 3 -#define X2APIC_MSR (r) (APIC_BASE_MSR + ((r) >> 4))#ifdef CONFIG_X86_64
arch/x86/kvm/x86.h +6 -0
@@ -504,6 +504,12 @@ int kvm_handle_memory_failure(struct kvm_vcpu *vcpu, int r,int kvm_handle_invpcid (struct kvm_vcpu *vcpu,unsigned long type,gva_t gva);bool kvm_msr_allowed (struct kvm_vcpu *vcpu,u32 index,u32 type); +enum kvm_msr_access { +MSR_TYPE_R =BIT (0 ), +MSR_TYPE_W =BIT (1 ), +MSR_TYPE_RW =MSR_TYPE_R |MSR_TYPE_W , +}; +/* * Internal error codes that are used to indicate that MSR emulation encountered * an error that should result in #GP in the guest, unless userspace
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Sean Christopherson
maintainerPATCH v2 3/10 KVM: x86: Rename KVM_MSR_RET_INVALID to KVM_MSR_RET_UNSUPPORTED
In reply to Sean ChristophersonRename the "INVALID" internal MSR error return code to "UNSUPPORTED" to try and make it more clear that access was denied because the MSR itself is unsupported/unknown. "INVALID" is too ambiguous, as it could just as easily mean the value for WRMSR as invalid.
Avoid UNKNOWN and UNIMPLEMENTED, as the error code is used for MSRs that _are_ actually implemented by KVM, e.g. if the MSR is unsupported because an associated feature flag is not present in guest CPUID.
Opportunistically beef up the comments for the internal MSR error codes.
--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/x86.c | 12 ++++++------ arch/x86/kvm/x86.h | 15 +++++++++++---- 4 files changed, 19 insertions(+), 12 deletions(-)
arch/x86/kvm/svm/svm.c +1 -1
@@ -2835,7 +2835,7 @@ static int svm_get_msr_feature(struct kvm_msr_entry *msr) msr->data |=MSR_AMD64_DE_CFG_LFENCE_SERIALIZE ;break ;default : -return KVM_MSR_RET_INVALID ; +return KVM_MSR_RET_UNSUPPORTED ; }return 0 ;
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -2006,7 +2006,7 @@ int vmx_get_msr_feature(struct kvm_msr_entry *msr)return 1 ;return vmx_get_vmx_msr (&vmcs_config.nested, msr->index, &msr->data);default : -return KVM_MSR_RET_INVALID ; +return KVM_MSR_RET_UNSUPPORTED ; } }
arch/x86/kvm/x86.c +6 -6
@@ -1690,7 +1690,7 @@ static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data) msr.index = index; r =kvm_get_msr_feature (&msr); -if (r ==KVM_MSR_RET_INVALID &&kvm_msr_ignored_check (index,0 , false)) +if (r ==KVM_MSR_RET_UNSUPPORTED &&kvm_msr_ignored_check (index,0 , false)) r =0 ; *data = msr.data;
@@ -1887,7 +1887,7 @@ static int kvm_set_msr_ignored_check(struct kvm_vcpu *vcpu, {int ret =__kvm_set_msr (vcpu, index, data, host_initiated); -if (ret ==KVM_MSR_RET_INVALID ) +if (ret ==KVM_MSR_RET_UNSUPPORTED )if (kvm_msr_ignored_check (index, data, true)) ret =0 ;
@@ -1932,7 +1932,7 @@ static int kvm_get_msr_ignored_check(struct kvm_vcpu *vcpu, {int ret =__kvm_get_msr (vcpu, index, data, host_initiated); -if (ret ==KVM_MSR_RET_INVALID ) { +if (ret ==KVM_MSR_RET_UNSUPPORTED ) {/* Unconditionally clear *data for simplicity */ *data =0 ;if (kvm_msr_ignored_check (index,0 , false))
@@ -2001,7 +2001,7 @@ static int complete_fast_rdmsr(struct kvm_vcpu *vcpu)static u64 kvm_msr_reason (int r) {switch (r) { -case KVM_MSR_RET_INVALID : +case KVM_MSR_RET_UNSUPPORTED :return KVM_MSR_EXIT_REASON_UNKNOWN ;case KVM_MSR_RET_FILTERED :return KVM_MSR_EXIT_REASON_FILTER ;
@@ -4149,7 +4149,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) kvm_is_msr_to_save(msr))break ; -return KVM_MSR_RET_INVALID ; +return KVM_MSR_RET_UNSUPPORTED ; }return 0 ; }
@@ -4510,7 +4510,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ; } -return KVM_MSR_RET_INVALID ; +return KVM_MSR_RET_UNSUPPORTED ; }return 0 ; }
arch/x86/kvm/x86.h +11 -4
@@ -512,11 +512,18 @@ enum kvm_msr_access {/* * Internal error codes that are used to indicate that MSR emulation encountered -* an error that should result in #GP in the guest, unless userspace -* handles it. +* an error that should result in #GP in the guest, unless userspace handles it. +* Note, '1', '0', and negative numbers are off limits, as they are used by KVM +* as part of KVM's lightly documented internal KVM_RUN return codes. +* +* UNSUPPORTED - The MSR isn't supported, either because it is completely +* unknown to KVM, or because the MSR should not exist according +* to the vCPU model. +* +* FILTERED - Access to the MSR is denied by a userspace MSR filter. */ -#define KVM_MSR_RET_INVALID 2/* in-kernel MSR emulation #GP condition */ -#define KVM_MSR_RET_FILTERED 3/* #GP due to userspace MSR filter */ +#define KVM_MSR_RET_UNSUPPORTED 2 +#define KVM_MSR_RET_FILTERED 3#define __cr4_reserved_bits (__cpu_has, __c) \ ({ \
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Sean Christopherson
maintainerPATCH v2 4/10 KVM: x86: Refactor kvm_x86_ops.get_msr_feature() to avoid kvm_msr_entry
In reply to Sean ChristophersonRefactor get_msr_feature() to take the index and data pointer as distinct parameters in anticipation of eliminating "struct kvm_msr_entry" usage further up the primary callchain.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/svm.c | 16 +++++++--------- arch/x86/kvm/vmx/vmx.c | 6 +++--- arch/x86/kvm/vmx/x86_ops.h | 2 +- arch/x86/kvm/x86.c | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1805,7 +1805,7 @@ struct kvm_x86_ops {int (*vm_move_enc_context_from)(struct kvm *kvm,unsigned int source_fd);void (*guest_memory_reclaimed)(struct kvm *kvm); -int (*get_msr_feature)(struct kvm_msr_entry *entry); +int (*get_msr_feature)(u32 msr,u64 *data);int (*check_emulate_instruction)(struct kvm_vcpu *vcpu,int emul_type,void *insn,int insn_len);
arch/x86/kvm/svm/svm.c +7 -9
@@ -2825,14 +2825,14 @@ static int efer_trap(struct kvm_vcpu *vcpu)return kvm_complete_insn_gp (vcpu, ret); } -static int svm_get_msr_feature (struct kvm_msr_entry *msr) +static int svm_get_msr_feature (u32 msr,u64 *data) { - msr->data =0 ; + *data =0 ; -switch (msr->index) { +switch (msr) {case MSR_AMD64_DE_CFG :if (cpu_feature_enabled (X86_FEATURE_LFENCE_RDTSC )) - msr->data |=MSR_AMD64_DE_CFG_LFENCE_SERIALIZE ; + *data |=MSR_AMD64_DE_CFG_LFENCE_SERIALIZE ;break ;default :return KVM_MSR_RET_UNSUPPORTED ;
@@ -3179,14 +3179,12 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)kvm_pr_unimpl_wrmsr (vcpu, ecx, data);break ;case MSR_AMD64_DE_CFG : { -struct kvm_msr_entry msr_entry; +u64 supported_de_cfg; - msr_entry.index = msr->index; -if (svm_get_msr_feature (&msr_entry)) +if (svm_get_msr_feature (ecx, &supported_de_cfg))return 1 ; -/* Check the supported bits */ -if (data & ~msr_entry.data) +if (data & ~supported_de_cfg)return 1 ;/*
arch/x86/kvm/vmx/vmx.c +3 -3
@@ -1998,13 +1998,13 @@ static inline bool is_vmx_feature_control_msr_valid(struct vcpu_vmx *vmx,return !(msr->data & ~valid_bits); } -int vmx_get_msr_feature (struct kvm_msr_entry *msr) +int vmx_get_msr_feature (u32 msr,u64 *data) { -switch (msr->index) { +switch (msr) { case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR:if (!nested)return 1 ; -return vmx_get_vmx_msr (&vmcs_config.nested, msr->index, &msr->data); +return vmx_get_vmx_msr (&vmcs_config.nested, msr, data);default :return KVM_MSR_RET_UNSUPPORTED ; }
arch/x86/kvm/vmx/x86_ops.h +1 -1
@@ -56,7 +56,7 @@ bool vmx_has_emulated_msr(struct kvm *kvm, u32 index);void vmx_msr_filter_changed (struct kvm_vcpu *vcpu);void vmx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void vmx_update_exception_bitmap (struct kvm_vcpu *vcpu); -int vmx_get_msr_feature (struct kvm_msr_entry *msr); +int vmx_get_msr_feature (u32 msr,u64 *data);int vmx_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info);u64 vmx_get_segment_base (struct kvm_vcpu *vcpu,int seg);void vmx_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);
arch/x86/kvm/x86.c +1 -1
@@ -1675,7 +1675,7 @@ static int kvm_get_msr_feature(struct kvm_msr_entry *msr)rdmsrl_safe (msr->index, &msr->data);break ;default : -return kvm_x86_call (get_msr_feature)(msr); +return kvm_x86_call (get_msr_feature)(msr->index, &msr->data); }return 0 ; }
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Sean Christopherson
maintainerPATCH v2 5/10 KVM: x86: Rename get_msr_feature() APIs to get_feature_msr()
In reply to Sean ChristophersonRename all APIs related to feature MSRs from get_feature_msr() to get_feature_msr(). The APIs get "feature MSRs", not "MSR features". And unlike kvm_{g,s}et_msr_common(), the "feature" adjective doesn't describe the helper itself.
No functional change intended.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 +- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/svm.c | 6 +++--- arch/x86/kvm/vmx/main.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/x86_ops.h | 2 +- arch/x86/kvm/x86.c | 12 ++++++------ 7 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -1
@@ -125,7 +125,7 @@ KVM_X86_OP_OPTIONAL(mem_enc_unregister_region) KVM_X86_OP_OPTIONAL(vm_copy_enc_context_from) KVM_X86_OP_OPTIONAL(vm_move_enc_context_from) KVM_X86_OP_OPTIONAL(guest_memory_reclaimed) -KVM_X86_OP(get_msr_feature) +KVM_X86_OP(get_feature_msr) KVM_X86_OP(check_emulate_instruction) KVM_X86_OP(apic_init_signal_blocked) KVM_X86_OP_OPTIONAL(enable_l2_tlb_flush)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1805,7 +1805,7 @@ struct kvm_x86_ops {int (*vm_move_enc_context_from)(struct kvm *kvm,unsigned int source_fd);void (*guest_memory_reclaimed)(struct kvm *kvm); -int (*get_msr_feature)(u32 msr,u64 *data); +int (*get_feature_msr)(u32 msr,u64 *data);int (*check_emulate_instruction)(struct kvm_vcpu *vcpu,int emul_type,void *insn,int insn_len);
arch/x86/kvm/svm/svm.c +3 -3
@@ -2825,7 +2825,7 @@ static int efer_trap(struct kvm_vcpu *vcpu)return kvm_complete_insn_gp (vcpu, ret); } -static int svm_get_msr_feature (u32 msr,u64 *data) +static int svm_get_feature_msr (u32 msr,u64 *data) { *data =0 ;
@@ -3181,7 +3181,7 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)case MSR_AMD64_DE_CFG : {u64 supported_de_cfg; -if (svm_get_msr_feature (ecx, &supported_de_cfg)) +if (svm_get_feature_msr (ecx, &supported_de_cfg))return 1 ;if (data & ~supported_de_cfg)
@@ -5001,7 +5001,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_unblocking = avic_vcpu_unblocking, .update_exception_bitmap = svm_update_exception_bitmap, - .get_msr_feature = svm_get_msr_feature, + .get_feature_msr = svm_get_feature_msr, .get_msr = svm_get_msr, .set_msr = svm_set_msr, .get_segment_base = svm_get_segment_base,
arch/x86/kvm/vmx/main.c +1 -1
@@ -41,7 +41,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_put = vmx_vcpu_put, .update_exception_bitmap = vmx_update_exception_bitmap, - .get_msr_feature = vmx_get_msr_feature, + .get_feature_msr = vmx_get_feature_msr, .get_msr = vmx_get_msr, .set_msr = vmx_set_msr, .get_segment_base = vmx_get_segment_base,
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -1998,7 +1998,7 @@ static inline bool is_vmx_feature_control_msr_valid(struct vcpu_vmx *vmx,return !(msr->data & ~valid_bits); } -int vmx_get_msr_feature (u32 msr,u64 *data) +int vmx_get_feature_msr (u32 msr,u64 *data) {switch (msr) { case KVM_FIRST_EMULATED_VMX_MSR ... KVM_LAST_EMULATED_VMX_MSR:
arch/x86/kvm/vmx/x86_ops.h +1 -1
@@ -56,7 +56,7 @@ bool vmx_has_emulated_msr(struct kvm *kvm, u32 index);void vmx_msr_filter_changed (struct kvm_vcpu *vcpu);void vmx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void vmx_update_exception_bitmap (struct kvm_vcpu *vcpu); -int vmx_get_msr_feature (u32 msr,u64 *data); +int vmx_get_feature_msr (u32 msr,u64 *data);int vmx_get_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info);u64 vmx_get_segment_base (struct kvm_vcpu *vcpu,int seg);void vmx_get_segment (struct kvm_vcpu *vcpu,struct kvm_segment *var,int seg);
arch/x86/kvm/x86.c +6 -6
@@ -1662,7 +1662,7 @@ static u64 kvm_get_arch_capabilities(void)return data; } -static int kvm_get_msr_feature (struct kvm_msr_entry *msr) +static int kvm_get_feature_msr (struct kvm_msr_entry *msr) {switch (msr->index) {case MSR_IA32_ARCH_CAPABILITIES :
@@ -1675,12 +1675,12 @@ static int kvm_get_msr_feature(struct kvm_msr_entry *msr)rdmsrl_safe (msr->index, &msr->data);break ;default : -return kvm_x86_call (get_msr_feature)(msr->index, &msr->data); +return kvm_x86_call (get_feature_msr)(msr->index, &msr->data); }return 0 ; } -static int do_get_msr_feature (struct kvm_vcpu *vcpu,unsigned index,u64 *data) +static int do_get_feature_msr (struct kvm_vcpu *vcpu,unsigned index,u64 *data) {struct kvm_msr_entry msr;int r;
@@ -1688,7 +1688,7 @@ static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)/* Unconditionally clear the output for simplicity */ msr.data =0 ; msr.index = index; - r =kvm_get_msr_feature (&msr); + r =kvm_get_feature_msr (&msr);if (r ==KVM_MSR_RET_UNSUPPORTED &&kvm_msr_ignored_check (index,0 , false)) r =0 ;
@@ -4946,7 +4946,7 @@ long kvm_arch_dev_ioctl(struct file *filp,break ; }case KVM_GET_MSRS : - r =msr_io (NULL , argp, do_get_msr_feature,1 ); + r =msr_io (NULL , argp, do_get_feature_msr,1 );break ;#ifdef CONFIG_KVM_HYPERV case KVM_GET_SUPPORTED_HV_CPUID :
@@ -7385,7 +7385,7 @@ static void kvm_probe_feature_msr(u32 msr_index) .index = msr_index, }; -if (kvm_get_msr_feature (&msr)) +if (kvm_get_feature_msr (&msr))return ; msr_based_features[num_msr_based_features++] = msr_index;
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Re: [PATCH v2 05/10] KVM: x86: Rename get_msr_feature() APIs to get_feature_msr()
Quoted 1 line · Sean Christopherson
Rename all APIs related to feature MSRs from
get_feature_msr()to
s/get_feature_msr/get_msr_feature/
Thanks,
Tom
Quoted 7 lines
get_feature_msr(). The APIs get "feature MSRs", not "MSR features". And unlike kvm_{g,s}et_msr_common(), the "feature" adjective doesn't describe the helper itself.No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>
Sean Christopherson
maintainerPATCH v2 6/10 KVM: x86: Refactor kvm_get_feature_msr() to avoid struct kvm_msr_entry
In reply to Sean ChristophersonRefactor kvm_get_feature_msr() to take the components of kvm_msr_entry as separate parameters, along with a vCPU pointer, i.e. to give it the same prototype as kvm_{g,s}et_msr_ignored_check(). This will allow using a common inner helper for handling accesses to "regular" and feature MSRs.
No functional change intended.
--- arch/x86/kvm/x86.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-)
arch/x86/kvm/x86.c +13 -16
@@ -1662,39 +1662,38 @@ static u64 kvm_get_arch_capabilities(void)return data; } -static int kvm_get_feature_msr (struct kvm_msr_entry *msr) +static int kvm_get_feature_msr (struct kvm_vcpu *vcpu,u32 index,u64 *data, +bool host_initiated) { -switch (msr->index) { +WARN_ON_ONCE (!host_initiated); + +switch (index) {case MSR_IA32_ARCH_CAPABILITIES : - msr->data =kvm_get_arch_capabilities (); + *data =kvm_get_arch_capabilities ();break ;case MSR_IA32_PERF_CAPABILITIES : - msr->data = kvm_caps.supported_perf_cap; + *data = kvm_caps.supported_perf_cap;break ;case MSR_IA32_UCODE_REV : -rdmsrl_safe (msr->index, &msr->data); +rdmsrl_safe (index, data);break ;default : -return kvm_x86_call (get_feature_msr)(msr->index, &msr->data); +return kvm_x86_call (get_feature_msr)(index, data); }return 0 ; }static int do_get_feature_msr (struct kvm_vcpu *vcpu,unsigned index,u64 *data) { -struct kvm_msr_entry msr;int r;/* Unconditionally clear the output for simplicity */ - msr.data =0 ; - msr.index = index; - r =kvm_get_feature_msr (&msr); + *data =0 ; + r =kvm_get_feature_msr (vcpu, index, data, true);if (r ==KVM_MSR_RET_UNSUPPORTED &&kvm_msr_ignored_check (index,0 , false)) r =0 ; - *data = msr.data; -return r; }
@@ -7381,11 +7380,9 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)static void kvm_probe_feature_msr (u32 msr_index) { -struct kvm_msr_entry msr = { - .index = msr_index, - }; +u64 data; -if (kvm_get_feature_msr (&msr)) +if (kvm_get_feature_msr (NULL , msr_index, &data, true))return ; msr_based_features[num_msr_based_features++] = msr_index;
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Sean Christopherson
maintainerPATCH v2 7/10 KVM: x86: Funnel all fancy MSR return value handling into a common helper
In reply to Sean ChristophersonAdd a common helper, kvm_do_msr_access(), to invoke the "leaf" APIs that are type and access specific, and more importantly to handle errors that are returned from the leaf APIs. I.e. turn kvm_msr_ignored_check() from a a helper that is called on an error, into a trampoline that detects errors *and* applies relevant side effects, e.g. logging unimplemented accesses.
Because the leaf APIs are used for guest accesses, userspace accesses, and KVM accesses, and because KVM supports restricting access to MSRs from userspace via filters, the error handling is subtly non-trivial. E.g. KVM has had at least one bug escape due to making each "outer" function handle errors. See commit 3376ca3f1a20 ("KVM: x86: Fix KVM_GET_MSRS stack info leak").
--- arch/x86/kvm/x86.c | 86 +++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 43 deletions(-)
arch/x86/kvm/x86.c +43 -43
@@ -304,25 +304,40 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {static struct kmem_cache *x86_emulator_cache; -/* -* When called, it means the previous get/set msr reached an invalid msr. -* Return true if we want to ignore/silent this failed msr access. -*/ -static bool kvm_msr_ignored_check (u32 msr,u64 data,bool write) +typedef int (*msr_access_t )(struct kvm_vcpu *vcpu,u32 index,u64 *data, +bool host_initiated); + +static __always_inline int kvm_do_msr_access(struct kvm_vcpu *vcpu, u32 msr, +u64 *data,bool host_initiated, +enum kvm_msr_access rw, +msr_access_t msr_access_fn) { -const char *op = write ?"wrmsr" :"rdmsr" ; - -if (ignore_msrs) { -if (report_ignored_msrs) -kvm_pr_unimpl ("ignored %s: 0x%x data 0x%llx\n" , - op, msr, data); -/* Mask the error */ -return true; - }else { +const char *op = rw ==MSR_TYPE_W ?"wrmsr" :"rdmsr" ; +int ret; + +BUILD_BUG_ON (rw !=MSR_TYPE_R && rw !=MSR_TYPE_W ); + +/* +* Zero the data on read failures to avoid leaking stack data to the +* guest and/or userspace, e.g. if the failure is ignored below. +*/ + ret =msr_access_fn (vcpu, msr, data, host_initiated); +if (ret && rw ==MSR_TYPE_R ) + *data =0 ; + +if (ret !=KVM_MSR_RET_UNSUPPORTED ) +return ret; + +if (!ignore_msrs) {kvm_debug_ratelimited ("unhandled %s: 0x%x data 0x%llx\n" , - op, msr, data); -return false; + op, msr, *data); +return ret; } + +if (report_ignored_msrs) +kvm_pr_unimpl ("ignored %s: 0x%x data 0x%llx\n" , op, msr, *data); + +return 0 ; }static struct kmem_cache *kvm_alloc_emulator_cache (void )
@@ -1685,16 +1700,8 @@ static int kvm_get_feature_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,static int do_get_feature_msr (struct kvm_vcpu *vcpu,unsigned index,u64 *data) { -int r; - -/* Unconditionally clear the output for simplicity */ - *data =0 ; - r =kvm_get_feature_msr (vcpu, index, data, true); - -if (r ==KVM_MSR_RET_UNSUPPORTED &&kvm_msr_ignored_check (index,0 , false)) - r =0 ; - -return r; +return kvm_do_msr_access (vcpu, index, data, true,MSR_TYPE_R , + kvm_get_feature_msr); }static bool __kvm_valid_efer (struct kvm_vcpu *vcpu,u64 efer)
@@ -1881,16 +1888,17 @@ static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,return kvm_x86_call (set_msr)(vcpu, &msr); } +static int _kvm_set_msr (struct kvm_vcpu *vcpu,u32 index,u64 *data, +bool host_initiated) +{ +return __kvm_set_msr (vcpu, index, *data, host_initiated); +} +static int kvm_set_msr_ignored_check (struct kvm_vcpu *vcpu,u32 index,u64 data,bool host_initiated) { -int ret =__kvm_set_msr (vcpu, index, data, host_initiated); - -if (ret ==KVM_MSR_RET_UNSUPPORTED ) -if (kvm_msr_ignored_check (index, data, true)) - ret =0 ; - -return ret; +return kvm_do_msr_access (vcpu, index, &data, host_initiated,MSR_TYPE_W , + _kvm_set_msr); }/*
@@ -1929,16 +1937,8 @@ int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,static int kvm_get_msr_ignored_check (struct kvm_vcpu *vcpu,u32 index,u64 *data,bool host_initiated) { -int ret =__kvm_get_msr (vcpu, index, data, host_initiated); - -if (ret ==KVM_MSR_RET_UNSUPPORTED ) { -/* Unconditionally clear *data for simplicity */ - *data =0 ; -if (kvm_msr_ignored_check (index,0 , false)) - ret =0 ; - } - -return ret; +return kvm_do_msr_access (vcpu, index, data, host_initiated,MSR_TYPE_R , + __kvm_get_msr); }static int kvm_get_msr_with_filter (struct kvm_vcpu *vcpu,u32 index,u64 *data)
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Sean Christopherson
maintainerPATCH v2 8/10 KVM: x86: Hoist x86.c's global msr_* variables up above kvm_do_msr_access()
In reply to Sean ChristophersonMove the definitions of the various MSR arrays above kvm_do_msr_access() so that kvm_do_msr_access() can query the arrays when handling failures, e.g. to squash errors if userspace tries to read an MSR that isn't fully supported, but that KVM advertised as being an MSR-to-save.
No functional change intended.
--- arch/x86/kvm/x86.c | 368 ++++++++++++++++++++++----------------------- 1 file changed, 184 insertions(+), 184 deletions(-)
arch/x86/kvm/x86.c +184 -184
@@ -304,6 +304,190 @@ const struct kvm_stats_header kvm_vcpu_stats_header = {static struct kmem_cache *x86_emulator_cache; +/* +* The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features) track +* the set of MSRs that KVM exposes to userspace through KVM_GET_MSRS, +* KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. msrs_to_save holds MSRs that +* require host support, i.e. should be probed via RDMSR. emulated_msrs holds +* MSRs that KVM emulates without strictly requiring host support. +* msr_based_features holds MSRs that enumerate features, i.e. are effectively +* CPUID leafs. Note, msr_based_features isn't mutually exclusive with +* msrs_to_save and emulated_msrs. +*/ + +static const u32 msrs_to_save_base[] = { +MSR_IA32_SYSENTER_CS ,MSR_IA32_SYSENTER_ESP ,MSR_IA32_SYSENTER_EIP , +MSR_STAR , +#ifdef CONFIG_X86_64 +MSR_CSTAR ,MSR_KERNEL_GS_BASE ,MSR_SYSCALL_MASK ,MSR_LSTAR , +#endif +MSR_IA32_TSC ,MSR_IA32_CR_PAT ,MSR_VM_HSAVE_PA , +MSR_IA32_FEAT_CTL ,MSR_IA32_BNDCFGS ,MSR_TSC_AUX , +MSR_IA32_SPEC_CTRL ,MSR_IA32_TSX_CTRL , +MSR_IA32_RTIT_CTL ,MSR_IA32_RTIT_STATUS ,MSR_IA32_RTIT_CR3_MATCH , +MSR_IA32_RTIT_OUTPUT_BASE ,MSR_IA32_RTIT_OUTPUT_MASK , +MSR_IA32_RTIT_ADDR0_A ,MSR_IA32_RTIT_ADDR0_B , +MSR_IA32_RTIT_ADDR1_A ,MSR_IA32_RTIT_ADDR1_B , +MSR_IA32_RTIT_ADDR2_A ,MSR_IA32_RTIT_ADDR2_B , +MSR_IA32_RTIT_ADDR3_A ,MSR_IA32_RTIT_ADDR3_B , +MSR_IA32_UMWAIT_CONTROL , + +MSR_IA32_XFD ,MSR_IA32_XFD_ERR , +}; + +static const u32 msrs_to_save_pmu[] = { +MSR_ARCH_PERFMON_FIXED_CTR0 ,MSR_ARCH_PERFMON_FIXED_CTR1 , +MSR_ARCH_PERFMON_FIXED_CTR0 +2 , +MSR_CORE_PERF_FIXED_CTR_CTRL ,MSR_CORE_PERF_GLOBAL_STATUS , +MSR_CORE_PERF_GLOBAL_CTRL , +MSR_IA32_PEBS_ENABLE ,MSR_IA32_DS_AREA ,MSR_PEBS_DATA_CFG , + +/* This part of MSRs should match KVM_MAX_NR_INTEL_GP_COUNTERS. */ +MSR_ARCH_PERFMON_PERFCTR0 ,MSR_ARCH_PERFMON_PERFCTR1 , +MSR_ARCH_PERFMON_PERFCTR0 +2 ,MSR_ARCH_PERFMON_PERFCTR0 +3 , +MSR_ARCH_PERFMON_PERFCTR0 +4 ,MSR_ARCH_PERFMON_PERFCTR0 +5 , +MSR_ARCH_PERFMON_PERFCTR0 +6 ,MSR_ARCH_PERFMON_PERFCTR0 +7 , +MSR_ARCH_PERFMON_EVENTSEL0 ,MSR_ARCH_PERFMON_EVENTSEL1 , +MSR_ARCH_PERFMON_EVENTSEL0 +2 ,MSR_ARCH_PERFMON_EVENTSEL0 +3 , +MSR_ARCH_PERFMON_EVENTSEL0 +4 ,MSR_ARCH_PERFMON_EVENTSEL0 +5 , +MSR_ARCH_PERFMON_EVENTSEL0 +6 ,MSR_ARCH_PERFMON_EVENTSEL0 +7 , + +MSR_K7_EVNTSEL0 ,MSR_K7_EVNTSEL1 ,MSR_K7_EVNTSEL2 ,MSR_K7_EVNTSEL3 , +MSR_K7_PERFCTR0 ,MSR_K7_PERFCTR1 ,MSR_K7_PERFCTR2 ,MSR_K7_PERFCTR3 , + +/* This part of MSRs should match KVM_MAX_NR_AMD_GP_COUNTERS. */ +MSR_F15H_PERF_CTL0 ,MSR_F15H_PERF_CTL1 ,MSR_F15H_PERF_CTL2 , +MSR_F15H_PERF_CTL3 ,MSR_F15H_PERF_CTL4 ,MSR_F15H_PERF_CTL5 , +MSR_F15H_PERF_CTR0 ,MSR_F15H_PERF_CTR1 ,MSR_F15H_PERF_CTR2 , +MSR_F15H_PERF_CTR3 ,MSR_F15H_PERF_CTR4 ,MSR_F15H_PERF_CTR5 , + +MSR_AMD64_PERF_CNTR_GLOBAL_CTL , +MSR_AMD64_PERF_CNTR_GLOBAL_STATUS , +MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR , +}; + +static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_base) + +ARRAY_SIZE (msrs_to_save_pmu)]; +static unsigned num_msrs_to_save; + +static const u32 emulated_msrs_all[] = { +MSR_KVM_SYSTEM_TIME ,MSR_KVM_WALL_CLOCK , +MSR_KVM_SYSTEM_TIME_NEW ,MSR_KVM_WALL_CLOCK_NEW , + +#ifdef CONFIG_KVM_HYPERV +HV_X64_MSR_GUEST_OS_ID ,HV_X64_MSR_HYPERCALL , +HV_X64_MSR_TIME_REF_COUNT ,HV_X64_MSR_REFERENCE_TSC , +HV_X64_MSR_TSC_FREQUENCY ,HV_X64_MSR_APIC_FREQUENCY , +HV_X64_MSR_CRASH_P0 ,HV_X64_MSR_CRASH_P1 ,HV_X64_MSR_CRASH_P2 , +HV_X64_MSR_CRASH_P3 ,HV_X64_MSR_CRASH_P4 ,HV_X64_MSR_CRASH_CTL , +HV_X64_MSR_RESET , +HV_X64_MSR_VP_INDEX , +HV_X64_MSR_VP_RUNTIME , +HV_X64_MSR_SCONTROL , +HV_X64_MSR_STIMER0_CONFIG , +HV_X64_MSR_VP_ASSIST_PAGE , +HV_X64_MSR_REENLIGHTENMENT_CONTROL ,HV_X64_MSR_TSC_EMULATION_CONTROL , +HV_X64_MSR_TSC_EMULATION_STATUS ,HV_X64_MSR_TSC_INVARIANT_CONTROL , +HV_X64_MSR_SYNDBG_OPTIONS , +HV_X64_MSR_SYNDBG_CONTROL ,HV_X64_MSR_SYNDBG_STATUS , +HV_X64_MSR_SYNDBG_SEND_BUFFER ,HV_X64_MSR_SYNDBG_RECV_BUFFER , +HV_X64_MSR_SYNDBG_PENDING_BUFFER , +#endif + +MSR_KVM_ASYNC_PF_EN ,MSR_KVM_STEAL_TIME , +MSR_KVM_PV_EOI_EN ,MSR_KVM_ASYNC_PF_INT ,MSR_KVM_ASYNC_PF_ACK , + +MSR_IA32_TSC_ADJUST , +MSR_IA32_TSC_DEADLINE , +MSR_IA32_ARCH_CAPABILITIES , +MSR_IA32_PERF_CAPABILITIES , +MSR_IA32_MISC_ENABLE , +MSR_IA32_MCG_STATUS , +MSR_IA32_MCG_CTL , +MSR_IA32_MCG_EXT_CTL , +MSR_IA32_SMBASE , +MSR_SMI_COUNT , +MSR_PLATFORM_INFO , +MSR_MISC_FEATURES_ENABLES , +MSR_AMD64_VIRT_SPEC_CTRL , +MSR_AMD64_TSC_RATIO , +MSR_IA32_POWER_CTL , +MSR_IA32_UCODE_REV , + +/* +* KVM always supports the "true" VMX control MSRs, even if the host +* does not. The VMX MSRs as a whole are considered "emulated" as KVM +* doesn't strictly require them to exist in the host (ignoring that +* KVM would refuse to load in the first place if the core set of MSRs +* aren't supported). +*/ +MSR_IA32_VMX_BASIC , +MSR_IA32_VMX_TRUE_PINBASED_CTLS , +MSR_IA32_VMX_TRUE_PROCBASED_CTLS , +MSR_IA32_VMX_TRUE_EXIT_CTLS , +MSR_IA32_VMX_TRUE_ENTRY_CTLS , +MSR_IA32_VMX_MISC , +MSR_IA32_VMX_CR0_FIXED0 , +MSR_IA32_VMX_CR4_FIXED0 , +MSR_IA32_VMX_VMCS_ENUM , +MSR_IA32_VMX_PROCBASED_CTLS2 , +MSR_IA32_VMX_EPT_VPID_CAP , +MSR_IA32_VMX_VMFUNC , + +MSR_K7_HWCR , +MSR_KVM_POLL_CONTROL , +}; + +static u32 emulated_msrs[ARRAY_SIZE (emulated_msrs_all)]; +static unsigned num_emulated_msrs; + +/* +* List of MSRs that control the existence of MSR-based features, i.e. MSRs +* that are effectively CPUID leafs. VMX MSRs are also included in the set of +* feature MSRs, but are handled separately to allow expedited lookups. +*/ +static const u32 msr_based_features_all_except_vmx[] = { +MSR_AMD64_DE_CFG , +MSR_IA32_UCODE_REV , +MSR_IA32_ARCH_CAPABILITIES , +MSR_IA32_PERF_CAPABILITIES , +}; + +static u32 msr_based_features[ARRAY_SIZE (msr_based_features_all_except_vmx) + + (KVM_LAST_EMULATED_VMX_MSR -KVM_FIRST_EMULATED_VMX_MSR +1 )]; +static unsigned int num_msr_based_features; + +/* +* All feature MSRs except uCode revID, which tracks the currently loaded uCode +* patch, are immutable once the vCPU model is defined. +*/ +static bool kvm_is_immutable_feature_msr (u32 msr) +{ +int i; + +if (msr >=KVM_FIRST_EMULATED_VMX_MSR && msr <=KVM_LAST_EMULATED_VMX_MSR ) +return true; + +for (i =0 ; i <ARRAY_SIZE (msr_based_features_all_except_vmx); i++) { +if (msr == msr_based_features_all_except_vmx[i]) +return msr !=MSR_IA32_UCODE_REV ; + } + +return false; +} + +static bool kvm_is_msr_to_save (u32 msr_index) +{ +unsigned int i; + +for (i =0 ; i < num_msrs_to_save; i++) { +if (msrs_to_save[i] == msr_index) +return true; + } + +return false; +} +typedef int (*msr_access_t )(struct kvm_vcpu *vcpu,u32 index,u64 *data,bool host_initiated);
@@ -1428,178 +1612,6 @@ int kvm_emulate_rdpmc(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_emulate_rdpmc); -/* -* The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features) track -* the set of MSRs that KVM exposes to userspace through KVM_GET_MSRS, -* KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. msrs_to_save holds MSRs that -* require host support, i.e. should be probed via RDMSR. emulated_msrs holds -* MSRs that KVM emulates without strictly requiring host support. -* msr_based_features holds MSRs that enumerate features, i.e. are effectively -* CPUID leafs. Note, msr_based_features isn't mutually exclusive with -* msrs_to_save and emulated_msrs. -*/ - -static const u32 msrs_to_save_base[] = { -MSR_IA32_SYSENTER_CS ,MSR_IA32_SYSENTER_ESP ,MSR_IA32_SYSENTER_EIP , -MSR_STAR , -#ifdef CONFIG_X86_64 -MSR_CSTAR ,MSR_KERNEL_GS_BASE ,MSR_SYSCALL_MASK ,MSR_LSTAR , -#endif -MSR_IA32_TSC ,MSR_IA32_CR_PAT ,MSR_VM_HSAVE_PA , -MSR_IA32_FEAT_CTL ,MSR_IA32_BNDCFGS ,MSR_TSC_AUX , -MSR_IA32_SPEC_CTRL ,MSR_IA32_TSX_CTRL , -MSR_IA32_RTIT_CTL ,MSR_IA32_RTIT_STATUS ,MSR_IA32_RTIT_CR3_MATCH , -MSR_IA32_RTIT_OUTPUT_BASE ,MSR_IA32_RTIT_OUTPUT_MASK , -MSR_IA32_RTIT_ADDR0_A ,MSR_IA32_RTIT_ADDR0_B , -MSR_IA32_RTIT_ADDR1_A ,MSR_IA32_RTIT_ADDR1_B , -MSR_IA32_RTIT_ADDR2_A ,MSR_IA32_RTIT_ADDR2_B , -MSR_IA32_RTIT_ADDR3_A ,MSR_IA32_RTIT_ADDR3_B , -MSR_IA32_UMWAIT_CONTROL , - -MSR_IA32_XFD ,MSR_IA32_XFD_ERR , -}; - -static const u32 msrs_to_save_pmu[] = { -MSR_ARCH_PERFMON_FIXED_CTR0 ,MSR_ARCH_PERFMON_FIXED_CTR1 , -MSR_ARCH_PERFMON_FIXED_CTR0 +2 , -MSR_CORE_PERF_FIXED_CTR_CTRL ,MSR_CORE_PERF_GLOBAL_STATUS , -MSR_CORE_PERF_GLOBAL_CTRL , -MSR_IA32_PEBS_ENABLE ,MSR_IA32_DS_AREA ,MSR_PEBS_DATA_CFG , - -/* This part of MSRs should match KVM_MAX_NR_INTEL_GP_COUNTERS. */ -MSR_ARCH_PERFMON_PERFCTR0 ,MSR_ARCH_PERFMON_PERFCTR1 , -MSR_ARCH_PERFMON_PERFCTR0 +2 ,MSR_ARCH_PERFMON_PERFCTR0 +3 , -MSR_ARCH_PERFMON_PERFCTR0 +4 ,MSR_ARCH_PERFMON_PERFCTR0 +5 , -MSR_ARCH_PERFMON_PERFCTR0 +6 ,MSR_ARCH_PERFMON_PERFCTR0 +7 , -MSR_ARCH_PERFMON_EVENTSEL0 ,MSR_ARCH_PERFMON_EVENTSEL1 , -MSR_ARCH_PERFMON_EVENTSEL0 +2 ,MSR_ARCH_PERFMON_EVENTSEL0 +3 , -MSR_ARCH_PERFMON_EVENTSEL0 +4 ,MSR_ARCH_PERFMON_EVENTSEL0 +5 , -MSR_ARCH_PERFMON_EVENTSEL0 +6 ,MSR_ARCH_PERFMON_EVENTSEL0 +7 , - -MSR_K7_EVNTSEL0 ,MSR_K7_EVNTSEL1 ,MSR_K7_EVNTSEL2 ,MSR_K7_EVNTSEL3 , -MSR_K7_PERFCTR0 ,MSR_K7_PERFCTR1 ,MSR_K7_PERFCTR2 ,MSR_K7_PERFCTR3 , - -/* This part of MSRs should match KVM_MAX_NR_AMD_GP_COUNTERS. */ -MSR_F15H_PERF_CTL0 ,MSR_F15H_PERF_CTL1 ,MSR_F15H_PERF_CTL2 , -MSR_F15H_PERF_CTL3 ,MSR_F15H_PERF_CTL4 ,MSR_F15H_PERF_CTL5 , -MSR_F15H_PERF_CTR0 ,MSR_F15H_PERF_CTR1 ,MSR_F15H_PERF_CTR2 , -MSR_F15H_PERF_CTR3 ,MSR_F15H_PERF_CTR4 ,MSR_F15H_PERF_CTR5 , - -MSR_AMD64_PERF_CNTR_GLOBAL_CTL , -MSR_AMD64_PERF_CNTR_GLOBAL_STATUS , -MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR , -}; - -static u32 msrs_to_save[ARRAY_SIZE (msrs_to_save_base) + -ARRAY_SIZE (msrs_to_save_pmu)]; -static unsigned num_msrs_to_save; - -static const u32 emulated_msrs_all[] = { -MSR_KVM_SYSTEM_TIME ,MSR_KVM_WALL_CLOCK , -MSR_KVM_SYSTEM_TIME_NEW ,MSR_KVM_WALL_CLOCK_NEW , - -#ifdef CONFIG_KVM_HYPERV -HV_X64_MSR_GUEST_OS_ID ,HV_X64_MSR_HYPERCALL , -HV_X64_MSR_TIME_REF_COUNT ,HV_X64_MSR_REFERENCE_TSC , -HV_X64_MSR_TSC_FREQUENCY ,HV_X64_MSR_APIC_FREQUENCY , -HV_X64_MSR_CRASH_P0 ,HV_X64_MSR_CRASH_P1 ,HV_X64_MSR_CRASH_P2 , -HV_X64_MSR_CRASH_P3 ,HV_X64_MSR_CRASH_P4 ,HV_X64_MSR_CRASH_CTL , -HV_X64_MSR_RESET , -HV_X64_MSR_VP_INDEX , -HV_X64_MSR_VP_RUNTIME , -HV_X64_MSR_SCONTROL , -HV_X64_MSR_STIMER0_CONFIG , -HV_X64_MSR_VP_ASSIST_PAGE , -HV_X64_MSR_REENLIGHTENMENT_CONTROL ,HV_X64_MSR_TSC_EMULATION_CONTROL , -HV_X64_MSR_TSC_EMULATION_STATUS ,HV_X64_MSR_TSC_INVARIANT_CONTROL , -HV_X64_MSR_SYNDBG_OPTIONS , -HV_X64_MSR_SYNDBG_CONTROL ,HV_X64_MSR_SYNDBG_STATUS , -HV_X64_MSR_SYNDBG_SEND_BUFFER ,HV_X64_MSR_SYNDBG_RECV_BUFFER , -HV_X64_MSR_SYNDBG_PENDING_BUFFER , -#endif - -MSR_KVM_ASYNC_PF_EN ,MSR_KVM_STEAL_TIME , -MSR_KVM_PV_EOI_EN ,MSR_KVM_ASYNC_PF_INT ,MSR_KVM_ASYNC_PF_ACK , - -MSR_IA32_TSC_ADJUST , -MSR_IA32_TSC_DEADLINE , -MSR_IA32_ARCH_CAPABILITIES , -MSR_IA32_PERF_CAPABILITIES , -MSR_IA32_MISC_ENABLE , -MSR_IA32_MCG_STATUS , -MSR_IA32_MCG_CTL , -MSR_IA32_MCG_EXT_CTL , -MSR_IA32_SMBASE , -MSR_SMI_COUNT , -MSR_PLATFORM_INFO , -MSR_MISC_FEATURES_ENABLES , -MSR_AMD64_VIRT_SPEC_CTRL , -MSR_AMD64_TSC_RATIO , -MSR_IA32_POWER_CTL , -MSR_IA32_UCODE_REV , - -/* -* KVM always supports the "true" VMX control MSRs, even if the host -* does not. The VMX MSRs as a whole are considered "emulated" as KVM -* doesn't strictly require them to exist in the host (ignoring that -* KVM would refuse to load in the first place if the core set of MSRs -* aren't supported). -*/ -MSR_IA32_VMX_BASIC , -MSR_IA32_VMX_TRUE_PINBASED_CTLS , -MSR_IA32_VMX_TRUE_PROCBASED_CTLS , -MSR_IA32_VMX_TRUE_EXIT_CTLS , -MSR_IA32_VMX_TRUE_ENTRY_CTLS , -MSR_IA32_VMX_MISC , -MSR_IA32_VMX_CR0_FIXED0 , -MSR_IA32_VMX_CR4_FIXED0 , -MSR_IA32_VMX_VMCS_ENUM , -MSR_IA32_VMX_PROCBASED_CTLS2 , -MSR_IA32_VMX_EPT_VPID_CAP , -MSR_IA32_VMX_VMFUNC , - -MSR_K7_HWCR , -MSR_KVM_POLL_CONTROL , -}; - -static u32 emulated_msrs[ARRAY_SIZE (emulated_msrs_all)]; -static unsigned num_emulated_msrs; - -/* -* List of MSRs that control the existence of MSR-based features, i.e. MSRs -* that are effectively CPUID leafs. VMX MSRs are also included in the set of -* feature MSRs, but are handled separately to allow expedited lookups. -*/ -static const u32 msr_based_features_all_except_vmx[] = { -MSR_AMD64_DE_CFG , -MSR_IA32_UCODE_REV , -MSR_IA32_ARCH_CAPABILITIES , -MSR_IA32_PERF_CAPABILITIES , -}; - -static u32 msr_based_features[ARRAY_SIZE (msr_based_features_all_except_vmx) + - (KVM_LAST_EMULATED_VMX_MSR -KVM_FIRST_EMULATED_VMX_MSR +1 )]; -static unsigned int num_msr_based_features; - -/* -* All feature MSRs except uCode revID, which tracks the currently loaded uCode -* patch, are immutable once the vCPU model is defined. -*/ -static bool kvm_is_immutable_feature_msr (u32 msr) -{ -int i; - -if (msr >=KVM_FIRST_EMULATED_VMX_MSR && msr <=KVM_LAST_EMULATED_VMX_MSR ) -return true; - -for (i =0 ; i <ARRAY_SIZE (msr_based_features_all_except_vmx); i++) { -if (msr == msr_based_features_all_except_vmx[i]) -return msr !=MSR_IA32_UCODE_REV ; - } - -return false; -} -/* * Some IA32_ARCH_CAPABILITIES bits have dependencies on MSRs that KVM * does not yet virtualize. These include:
@@ -3747,18 +3759,6 @@ static void record_steal_time(struct kvm_vcpu *vcpu)mark_page_dirty_in_slot (vcpu->kvm, ghc->memslot,gpa_to_gfn (ghc->gpa)); } -static bool kvm_is_msr_to_save (u32 msr_index) -{ -unsigned int i; - -for (i =0 ; i < num_msrs_to_save; i++) { -if (msrs_to_save[i] == msr_index) -return true; - } - -return false; -} -int kvm_set_msr_common (struct kvm_vcpu *vcpu,struct msr_data *msr_info) {u32 msr = msr_info->index;
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Sean Christopherson
maintainerPATCH v2 9/10 KVM: x86: Suppress failures on userspace access to advertised, unsupported MSRs
In reply to Sean ChristophersonExtend KVM's suppression of failures due to a userspace access to an unsupported, but advertised as a "to save" MSR to all MSRs, not just those that happen to reach the default case statements in kvm_get_msr_common() and kvm_set_msr_common(). KVM's soon-to-be-established ABI is that if an MSR is advertised to userspace, then userspace is allowed to read the MSR, and write back the value that was read, i.e. why an MSR is unsupported doesn't change KVM's ABI.
Practically speaking, this is very nearly a nop, as the only other paths that return KVM_MSR_RET_UNSUPPORTED are {svm,vmx}_get_feature_msr(), and it's unlikely, though not impossible, that userspace is using KVM_GET_MSRS on unsupported MSRs.
The primary goal of moving the suppression to common code is to allow returning KVM_MSR_RET_UNSUPPORTED as appropriate throughout KVM, without having to manually handle the "is userspace accessing an advertised" waiver. I.e. this will allow formalizing KVM's ABI without incurring a high maintenance cost.
--- arch/x86/kvm/x86.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
arch/x86/kvm/x86.c +9 -18
@@ -512,6 +512,15 @@ static __always_inline int kvm_do_msr_access(struct kvm_vcpu *vcpu, u32 msr,if (ret !=KVM_MSR_RET_UNSUPPORTED )return ret; +/* +* Userspace is allowed to read MSRs, and write '0' to MSRs, that KVM +* reports as to-be-saved, even if an MSR isn't fully supported. +* Simply check that @data is '0', which covers both the write '0' case +* and all reads (in which case @data is zeroed on failure; see above). +*/ +if (host_initiated && !*data &&kvm_is_msr_to_save (msr)) +return 0 ; +if (!ignore_msrs) {kvm_debug_ratelimited ("unhandled %s: 0x%x data 0x%llx\n" , op, msr, *data);
@@ -4140,14 +4149,6 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (kvm_pmu_is_valid_msr (vcpu, msr))return kvm_pmu_set_msr (vcpu, msr_info); -/* -* Userspace is allowed to write '0' to MSRs that KVM reports -* as to-be-saved, even if an MSRs isn't fully supported. -*/ -if (msr_info->host_initiated && !data && -kvm_is_msr_to_save (msr)) -break ; -return KVM_MSR_RET_UNSUPPORTED ; }return 0 ;
@@ -4499,16 +4500,6 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)if (kvm_pmu_is_valid_msr (vcpu, msr_info->index))return kvm_pmu_get_msr (vcpu, msr_info); -/* -* Userspace is allowed to read MSRs that KVM reports as -* to-be-saved, even if an MSR isn't fully supported. -*/ -if (msr_info->host_initiated && -kvm_is_msr_to_save (msr_info->index)) { - msr_info->data =0 ; -break ; - } -return KVM_MSR_RET_UNSUPPORTED ; }return 0 ;
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Sean Christopherson
maintainerPATCH v2 10/10 KVM: x86: Suppress userspace access failures on unsupported, "emulated" MSRs
In reply to Sean ChristophersonExtend KVM's suppression of userspace MSR access failures to MSRs that KVM reports as emulated, but are ultimately unsupported, e.g. if the VMX MSRs are emulated by KVM, but are unsupported given the vCPU model.
--- arch/x86/kvm/x86.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
arch/x86/kvm/x86.c +8 -3
@@ -476,7 +476,7 @@ static bool kvm_is_immutable_feature_msr(u32 msr)return false; } -static bool kvm_is_msr_to_save (u32 msr_index) +static bool kvm_is_advertised_msr (u32 msr_index) {unsigned int i;
@@ -485,6 +485,11 @@ static bool kvm_is_msr_to_save(u32 msr_index)return true; } +for (i =0 ; i < num_emulated_msrs; i++) { +if (emulated_msrs[i] == msr_index) +return true; + } +return false; }
@@ -514,11 +519,11 @@ static __always_inline int kvm_do_msr_access(struct kvm_vcpu *vcpu, u32 msr,/* * Userspace is allowed to read MSRs, and write '0' to MSRs, that KVM -* reports as to-be-saved, even if an MSR isn't fully supported. +* advertises to userspace, even if an MSR isn't fully supported. * Simply check that @data is '0', which covers both the write '0' case * and all reads (in which case @data is zeroed on failure; see above). */ -if (host_initiated && !*data &&kvm_is_msr_to_save (msr)) +if (host_initiated && !*data &&kvm_is_advertised_msr (msr))return 0 ;if (!ignore_msrs) {
Signature · 1 line
2.46.0.rc2.264.g509ed76dc8-goog
Quoted 9 lines · Sean Christopherson
Rework KVM's MSR access handling, and more specific the handling of failures, to begin the march towards removing
host_initiatedexemptions for CPUID checks, e.g. to eventually turn code like this:if (!msr_info->host_initiated && !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) return 1;[...]
Applied to kvm-x86 misc, thanks!
[01/10] KVM: SVM: Disallow guest from changing userspace's MSR_AMD64_DE_CFG value
https://github.com/kvm-x86/linux/commit/74a0e79df68a [02/10] KVM: x86: Move MSR_TYPE_{R,W,RW} values from VMX to x86, as enums https://github.com/kvm-x86/linux/commit/b58b808cbe93 [03/10] KVM: x86: Rename KVM_MSR_RET_INVALID to KVM_MSR_RET_UNSUPPORTED https://github.com/kvm-x86/linux/commit/aaecae7b6a2b [04/10] KVM: x86: Refactor kvm_x86_ops.get_msr_feature() to avoid kvm_msr_entry https://github.com/kvm-x86/linux/commit/74c6c98a598a [05/10] KVM: x86: Rename get_msr_feature() APIs to get_feature_msr() https://github.com/kvm-x86/linux/commit/b848f24bd74a [06/10] KVM: x86: Refactor kvm_get_feature_msr() to avoid struct kvm_msr_entry https://github.com/kvm-x86/linux/commit/7075f1636150 [07/10] KVM: x86: Funnel all fancy MSR return value handling into a common helper https://github.com/kvm-x86/linux/commit/1cec2034980a [08/10] KVM: x86: Hoist x86.c's global msr_* variables up above kvm_do_msr_access() https://github.com/kvm-x86/linux/commit/3adef9034596 [09/10] KVM: x86: Suppress failures on userspace access to advertised, unsupported MSRs https://github.com/kvm-x86/linux/commit/64a5d7a1091f [10/10] KVM: x86: Suppress userspace access failures on unsupported, "emulated" MSRs https://github.com/kvm-x86/linux/commit/44dd0f5732b4