Provide a new KVM capability that allows bits within MSRs to be recognized as features. Two new ioctls are added to the VM ioctl routine to retrieve the list of these MSRs and then retrieve their values. An x86_kvm_ops callback is used to determine support for the listed MSR-based features. Signed-off-by: Tom Lendacky <thomas.lendacky@xxxxxxx> --- Documentation/virtual/kvm/api.txt | 47 ++++++++++++++++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 2 + arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/x86.c | 51 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 4 +++ 5 files changed, 105 insertions(+) diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt index 792fa87..cd580e4 100644 --- a/Documentation/virtual/kvm/api.txt +++ b/Documentation/virtual/kvm/api.txt @@ -3500,6 +3500,53 @@ Returns: 0 on success; -1 on error This ioctl can be used to unregister the guest memory region registered with KVM_MEMORY_ENCRYPT_REG_REGION ioctl above. +4.113 KVM_GET_MSR_INDEX_LIST + +Capability: KVM_CAP_GET_MSR_FEATURES +Architectures: x86 +Type: vm ioctl +Parameters: struct kvm_msr_list (in/out) +Returns: 0 on success; -1 on error +Errors: + EFAULT: the msr index list cannot be read from or written to + E2BIG: the msr index list is to big to fit in the array specified by + the user. + +struct kvm_msr_list { + __u32 nmsrs; /* number of msrs in entries */ + __u32 indices[0]; +}; + +This ioctl returns the msrs that represent possible supported features. +This list varies by kvm version and host processor. The user fills in +in the size of the indices array in nmsrs, and in return kvm adjusts nmsrs +to reflect the actual number of msrs and fills in the indices array with +their numbers. To verify if an msr-based feature is available, the user +should invoke KVM_GET_MSR for the msr in question. + +4.114 KVM_GET_MSR + +Capability: KVM_CAP_GET_MSR_FEATURES +Architectures: x86 +Type: vm ioctl +Parameters: struct kvm_msr_entry (in/out) +Returns: 0 on MSR feature supported; + 1 on MSR feature not supported; + -1 on error +Errors: + EFAULT: the msr entry cannot be read from or written to + +struct kvm_msr_entry { + __u32 index; + __u32 reserved; + __u64 data; +}; + +Using the list of msr-based features returned from KVM_GET_MSR_INDEX_LIST, +the user can determine support for the msr-based feature using this ioctl. +When a value of 0 is returned, the msr-based feature is supported and the +data member of kvm_msr_entry contains the msr-based feature value. + 5. The kvm_run structure ------------------------ diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index dd6f57a..e466bce 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1095,6 +1095,8 @@ struct kvm_x86_ops { int (*mem_enc_op)(struct kvm *kvm, void __user *argp); int (*mem_enc_reg_region)(struct kvm *kvm, struct kvm_enc_region *argp); int (*mem_enc_unreg_region)(struct kvm *kvm, struct kvm_enc_region *argp); + + int (*msr_feature)(struct kvm_msr_entry *entry); }; struct kvm_arch_async_pf { diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index f3a9604..d5536f1 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -172,6 +172,7 @@ struct kvm_fpu { __u32 pad2; }; +/* for KVM_GET_MSR */ struct kvm_msr_entry { __u32 index; __u32 reserved; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c8a0b54..0219c5c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1049,6 +1049,15 @@ bool kvm_rdpmc(struct kvm_vcpu *vcpu) static unsigned num_emulated_msrs; +/* + * List of msr numbers which are used to expose MSR-based features that + * can be used by a hypervisor to validate requested CPU features. + */ +static u32 msr_based_features[] = { +}; + +static unsigned int num_msr_based_features = ARRAY_SIZE(msr_based_features); + bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) { if (efer & efer_reserved_bits) @@ -2785,6 +2794,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_SET_BOOT_CPU_ID: case KVM_CAP_SPLIT_IRQCHIP: case KVM_CAP_IMMEDIATE_EXIT: + case KVM_CAP_GET_MSR_FEATURES: r = 1; break; case KVM_CAP_ADJUST_CLOCK: @@ -4410,6 +4420,47 @@ long kvm_arch_vm_ioctl(struct file *filp, r = kvm_x86_ops->mem_enc_unreg_region(kvm, ®ion); break; } + case KVM_GET_MSR_INDEX_LIST: { + struct kvm_msr_list __user *user_msr_list = argp; + struct kvm_msr_list msr_list; + unsigned int n; + + r = -EFAULT; + if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) + goto out; + n = msr_list.nmsrs; + msr_list.nmsrs = num_msr_based_features; + if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) + goto out; + r = -E2BIG; + if (n < msr_list.nmsrs) + goto out; + r = -EFAULT; + if (copy_to_user(user_msr_list->indices, &msr_based_features, + num_msr_based_features * sizeof(u32))) + goto out; + r = 0; + break; + } + case KVM_GET_MSR: { + struct kvm_msr_entry __user *user_msr = argp; + struct kvm_msr_entry msr; + + r = -EFAULT; + if (copy_from_user(&msr, user_msr, sizeof(msr))) + goto out; + + r = 1; + if (!kvm_x86_ops->msr_feature || kvm_x86_ops->msr_feature(&msr)) + goto out; + + r = -EFAULT; + if (copy_to_user(user_msr, &msr, sizeof(msr))) + goto out; + + r = 0; + break; + } default: r = -ENOTTY; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 0fb5ef9..48e0368 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -934,6 +934,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_S390_AIS_MIGRATION 150 #define KVM_CAP_PPC_GET_CPU_CHAR 151 #define KVM_CAP_S390_BPB 152 +#define KVM_CAP_GET_MSR_FEATURES 153 #ifdef KVM_CAP_IRQ_ROUTING @@ -1373,6 +1374,9 @@ struct kvm_enc_region { #define KVM_MEMORY_ENCRYPT_REG_REGION _IOR(KVMIO, 0xbb, struct kvm_enc_region) #define KVM_MEMORY_ENCRYPT_UNREG_REGION _IOR(KVMIO, 0xbc, struct kvm_enc_region) +/* Available with KVM_CAP_GET_MSR_FEATURES */ +#define KVM_GET_MSR _IOR(KVMIO, 0xbd, struct kvm_msr_entry) + /* Secure Encrypted Virtualization command */ enum sev_cmd_id { /* Guest initialization commands */