[RFC PATCH v1 24/28] KVM: SVM: add SEV_LAUNCH_FINISH command

From: Brijesh Singh
Date: Mon Aug 22 2016 - 19:29:17 EST


The command is used for finializing the guest launch into SEV mode.

For more information see [1], section 6.3

[1] http://support.amd.com/TechDocs/55766_SEV-KM%20API_Spec.pdf

Signed-off-by: Brijesh Singh <brijesh.singh@xxxxxxx>
---
arch/x86/kvm/svm.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+)

diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index c78bdc6..60cc0f7 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -5497,6 +5497,79 @@ err_1:
return ret;
}

+static int sev_launch_finish(struct kvm *kvm,
+ struct kvm_sev_launch_finish __user *argp,
+ int *psp_ret)
+{
+ int i, ret;
+ void *mask = NULL;
+ int buffer_len, len;
+ struct kvm_vcpu *vcpu;
+ struct psp_data_launch_finish *finish;
+ struct kvm_sev_launch_finish params;
+
+ if (!kvm_sev_guest())
+ return -EINVAL;
+
+ /* Get the parameters from the user */
+ if (copy_from_user(&params, argp, sizeof(*argp)))
+ return -EFAULT;
+
+ buffer_len = sizeof(*finish) + (sizeof(u64) * params.vcpu_count);
+ finish = kzalloc(buffer_len, GFP_KERNEL);
+ if (!finish)
+ return -ENOMEM;
+
+ /* copy the vcpu mask from user */
+ if (params.vcpu_mask_length && params.vcpu_mask_addr) {
+ ret = -ENOMEM;
+ mask = (void *) get_zeroed_page(GFP_KERNEL);
+ if (!mask)
+ goto err_1;
+
+ len = min_t(size_t, PAGE_SIZE, params.vcpu_mask_length);
+ ret = -EFAULT;
+ if (copy_from_user(mask, (uint8_t*)params.vcpu_mask_addr, len))
+ goto err_2;
+ finish->vcpus.state_mask_addr = __psp_pa(mask);
+ }
+
+ finish->handle = kvm_sev_handle();
+ finish->hdr.buffer_len = buffer_len;
+ finish->vcpus.state_count = params.vcpu_count;
+ finish->vcpus.state_length = params.vcpu_length;
+ kvm_for_each_vcpu(i, vcpu, kvm) {
+ finish->vcpus.state_addr[i] =
+ to_svm(vcpu)->vmcb_pa | sme_me_mask;
+ if (i == params.vcpu_count)
+ break;
+ }
+
+ /* launch finish */
+ ret = psp_guest_launch_finish(finish, psp_ret);
+ if (ret) {
+ printk(KERN_ERR "SEV: LAUNCH_FINISH ret=%d (%#010x)\n",
+ ret, *psp_ret);
+ goto err_2;
+ }
+
+ /* Iterate through each vcpus and set SEV KVM_SEV_FEATURE bit in
+ * KVM_CPUID_FEATURE to indicate that SEV is enabled on this vcpu
+ */
+ kvm_for_each_vcpu(i, vcpu, kvm)
+ svm_cpuid_update(vcpu);
+
+ /* copy the measurement for user */
+ if (copy_to_user(argp->measurement, finish->measurement, 32))
+ ret = -EFAULT;
+
+err_2:
+ free_page((unsigned long)mask);
+err_1:
+ kfree(finish);
+ return ret;
+}
+
static int amd_sev_issue_cmd(struct kvm *kvm,
struct kvm_sev_issue_cmd __user *user_data)
{
@@ -5517,6 +5590,11 @@ static int amd_sev_issue_cmd(struct kvm *kvm,
&arg.ret_code);
break;
}
+ case KVM_SEV_LAUNCH_FINISH: {
+ r = sev_launch_finish(kvm, (void *)arg.opaque,
+ &arg.ret_code);
+ break;
+ }
default:
break;
}