From: Brijesh Singh <brijesh.singh@xxxxxxx> The ioctl is used to retrieve a guest's shared pages list. Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx> Cc: Ingo Molnar <mingo@xxxxxxxxxx> Cc: "H. Peter Anvin" <hpa@xxxxxxxxx> Cc: Paolo Bonzini <pbonzini@xxxxxxxxxx> Cc: "Radim Krčmář" <rkrcmar@xxxxxxxxxx> Cc: Joerg Roedel <joro@xxxxxxxxxx> Cc: Borislav Petkov <bp@xxxxxxx> Cc: Tom Lendacky <thomas.lendacky@xxxxxxx> Cc: x86@xxxxxxxxxx Cc: kvm@xxxxxxxxxxxxxxx Cc: linux-kernel@xxxxxxxxxxxxxxx Signed-off-by: Brijesh Singh <brijesh.singh@xxxxxxx> Co-developed-by: Ashish Kalra <ashish.kalra@xxxxxxx> Signed-off-by: Ashish Kalra <ashish.kalra@xxxxxxx> --- Documentation/virt/kvm/api.rst | 24 ++++++++++++++++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/svm/sev.c | 49 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/x86.c | 12 ++++++++ include/uapi/linux/kvm.h | 9 ++++++ 7 files changed, 98 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 99ceb978c8b0..59ef537c0cdd 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -4677,6 +4677,30 @@ This ioctl resets VCPU registers and control structures according to the clear cpu reset definition in the POP. However, the cpu is not put into ESA mode. This reset is a superset of the initial reset. +4.125 KVM_GET_SHARED_PAGES_LIST (vm ioctl) +--------------------------------------- + +:Capability: basic +:Architectures: x86 +:Type: vm ioctl +:Parameters: struct kvm_shared_pages_list (in/out) +:Returns: 0 on success, -1 on error + +/* for KVM_GET_SHARED_PAGES_LIST */ +struct kvm_shared_pages_list { + int __user *pnents; + void __user *buffer; + __u32 size; +}; + +The encrypted VMs have the concept of private and shared pages. The private +pages are encrypted with the guest-specific key, while the shared pages may +be encrypted with the hypervisor key. The KVM_GET_SHARED_PAGES_LIST can +be used to get guest's shared/unencrypted memory regions list. +This list can be used during the guest migration. If the page +is private then the userspace need to use SEV migration commands to transmit +the page. + 4.125 KVM_S390_PV_COMMAND ------------------------- diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 2da5f5e2a10e..cd354d830e13 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1303,6 +1303,8 @@ struct kvm_x86_ops { void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector); int (*page_enc_status_hc)(struct kvm *kvm, unsigned long gpa, unsigned long sz, unsigned long mode); + int (*get_shared_pages_list)(struct kvm *kvm, + struct kvm_shared_pages_list *list); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 55c628df5155..701d74c8b15b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -50,6 +50,11 @@ struct shared_region { unsigned long gfn_start, gfn_end; }; +struct shared_region_array_entry { + unsigned long gfn_start; + unsigned long gfn_end; +}; + static int sev_flush_asids(void) { int ret, error = 0; @@ -1622,6 +1627,50 @@ int svm_page_enc_status_hc(struct kvm *kvm, unsigned long gpa, return ret; } +int svm_get_shared_pages_list(struct kvm *kvm, + struct kvm_shared_pages_list *list) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct shared_region_array_entry *array; + struct shared_region *pos; + int ret, nents = 0; + unsigned long sz; + + if (!sev_guest(kvm)) + return -ENOTTY; + + if (!list->size) + return -EINVAL; + + if (!sev->shared_pages_list_count) + return put_user(0, list->pnents); + + sz = sev->shared_pages_list_count * sizeof(struct shared_region_array_entry); + if (sz > list->size) + return -E2BIG; + + array = kmalloc(sz, GFP_KERNEL); + if (!array) + return -ENOMEM; + + mutex_lock(&kvm->lock); + list_for_each_entry(pos, &sev->shared_pages_list, list) { + array[nents].gfn_start = pos->gfn_start; + array[nents++].gfn_end = pos->gfn_end; + } + mutex_unlock(&kvm->lock); + + ret = -EFAULT; + if (copy_to_user(list->buffer, array, sz)) + goto out; + if (put_user(nents, list->pnents)) + goto out; + ret = 0; +out: + kfree(array); + return ret; +} + int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index bb249ec625fc..533ce47ff158 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4538,6 +4538,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .page_enc_status_hc = svm_page_enc_status_hc, + .get_shared_pages_list = svm_get_shared_pages_list, }; static struct kvm_x86_init_ops svm_init_ops __initdata = { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 6437c1fa1f24..6a777c61373c 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -477,6 +477,7 @@ int nested_svm_exit_special(struct vcpu_svm *svm); void sync_nested_vmcb_control(struct vcpu_svm *svm); int svm_page_enc_status_hc(struct kvm *kvm, unsigned long gpa, unsigned long npages, unsigned long enc); +int svm_get_shared_pages_list(struct kvm *kvm, struct kvm_shared_pages_list *list); extern struct kvm_x86_nested_ops svm_nested_ops; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2f17f0f9ace7..acfec2ae1402 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -5719,6 +5719,18 @@ long kvm_arch_vm_ioctl(struct file *filp, case KVM_X86_SET_MSR_FILTER: r = kvm_vm_ioctl_set_msr_filter(kvm, argp); break; + case KVM_GET_SHARED_PAGES_LIST: { + struct kvm_shared_pages_list list; + + r = -EFAULT; + if (copy_from_user(&list, argp, sizeof(list))) + goto out; + + r = -ENOTTY; + if (kvm_x86_ops.get_shared_pages_list) + r = kvm_x86_ops.get_shared_pages_list(kvm, &list); + break; + } default: r = -ENOTTY; } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index c4e195a4220f..0529ba80498a 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -544,6 +544,13 @@ struct kvm_clear_dirty_log { }; }; +/* for KVM_GET_SHARED_PAGES_LIST */ +struct kvm_shared_pages_list { + int __user *pnents; + void __user *buffer; + __u32 size; +}; + /* for KVM_SET_SIGNAL_MASK */ struct kvm_signal_mask { __u32 len; @@ -1565,6 +1572,8 @@ struct kvm_pv_cmd { /* Available with KVM_CAP_DIRTY_LOG_RING */ #define KVM_RESET_DIRTY_RINGS _IO(KVMIO, 0xc7) +#define KVM_GET_SHARED_PAGES_LIST _IOW(KVMIO, 0xc8, struct kvm_shared_pages_list) + /* Secure Encrypted Virtualization command */ enum sev_cmd_id { /* Guest initialization commands */ -- 2.17.1