Re: [PATCH v5 34/34] KVM: SVM: Provide support to launch and run an SEV-ES guest

2020-12-16 Thread Tom Lendacky
On 12/10/20 11:10 AM, Tom Lendacky wrote:
> From: Tom Lendacky 
>
> An SEV-ES guest is started by invoking a new SEV initialization ioctl,
> KVM_SEV_ES_INIT. This identifies the guest as an SEV-ES guest, which is
> used to drive the appropriate ASID allocation, VMSA encryption, etc.
>
> Before being able to run an SEV-ES vCPU, the vCPU VMSA must be encrypted
> and measured. This is done using the LAUNCH_UPDATE_VMSA command after all
> calls to LAUNCH_UPDATE_DATA have been performed, but before LAUNCH_MEASURE
> has been performed. In order to establish the encrypted VMSA, the current
> (traditional) VMSA and the GPRs are synced to the page that will hold the
> encrypted VMSA and then LAUNCH_UPDATE_VMSA is invoked. The vCPU is then
> marked as having protected guest state.
>
> Signed-off-by: Tom Lendacky 
> ---
> +
> + /* Sync registgers */
> + save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX];
> + save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX];
> + save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
> + save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX];
> + save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP];
> + save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
> + save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
> + save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
> + save->r8  = svm->vcpu.arch.regs[VCPU_REGS_R8];
> + save->r9  = svm->vcpu.arch.regs[VCPU_REGS_R9];
> + save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
> + save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11];
> + save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12];
> + save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
> + save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
> + save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
> + save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
> +

Paolo, I just noticed that a 32-bit build will fail because of R8-R15
references, sorry about that (I'm kind of surprised krobot hasn't
complained). This should take care of it:

---
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 4045de7f8f8b..84b3ee15f4ec 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -529,6 +529,7 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm)
save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
+#ifdef X86_64
save->r8  = svm->vcpu.arch.regs[VCPU_REGS_R8];
save->r9  = svm->vcpu.arch.regs[VCPU_REGS_R9];
save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
@@ -537,6 +538,7 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm)
save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
+#endif
save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];

/* Sync some non-GPR registers before encrypting */


[PATCH v5 34/34] KVM: SVM: Provide support to launch and run an SEV-ES guest

2020-12-10 Thread Tom Lendacky
From: Tom Lendacky 

An SEV-ES guest is started by invoking a new SEV initialization ioctl,
KVM_SEV_ES_INIT. This identifies the guest as an SEV-ES guest, which is
used to drive the appropriate ASID allocation, VMSA encryption, etc.

Before being able to run an SEV-ES vCPU, the vCPU VMSA must be encrypted
and measured. This is done using the LAUNCH_UPDATE_VMSA command after all
calls to LAUNCH_UPDATE_DATA have been performed, but before LAUNCH_MEASURE
has been performed. In order to establish the encrypted VMSA, the current
(traditional) VMSA and the GPRs are synced to the page that will hold the
encrypted VMSA and then LAUNCH_UPDATE_VMSA is invoked. The vCPU is then
marked as having protected guest state.

Signed-off-by: Tom Lendacky 
---
 arch/x86/kvm/svm/sev.c | 104 +
 1 file changed, 104 insertions(+)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 225f18dbf522..89f6fe4468c5 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -203,6 +203,16 @@ static int sev_guest_init(struct kvm *kvm, struct 
kvm_sev_cmd *argp)
return ret;
 }
 
+static int sev_es_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+   if (!sev_es)
+   return -ENOTTY;
+
+   to_kvm_svm(kvm)->sev_info.es_active = true;
+
+   return sev_guest_init(kvm, argp);
+}
+
 static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
 {
struct sev_data_activate *data;
@@ -502,6 +512,94 @@ static int sev_launch_update_data(struct kvm *kvm, struct 
kvm_sev_cmd *argp)
return ret;
 }
 
+static int sev_es_sync_vmsa(struct vcpu_svm *svm)
+{
+   struct vmcb_save_area *save = &svm->vmcb->save;
+
+   /* Check some debug related fields before encrypting the VMSA */
+   if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1))
+   return -EINVAL;
+
+   /* Sync registgers */
+   save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX];
+   save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX];
+   save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
+   save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX];
+   save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP];
+   save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP];
+   save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI];
+   save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI];
+   save->r8  = svm->vcpu.arch.regs[VCPU_REGS_R8];
+   save->r9  = svm->vcpu.arch.regs[VCPU_REGS_R9];
+   save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10];
+   save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11];
+   save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12];
+   save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13];
+   save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14];
+   save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15];
+   save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP];
+
+   /* Sync some non-GPR registers before encrypting */
+   save->xcr0 = svm->vcpu.arch.xcr0;
+   save->pkru = svm->vcpu.arch.pkru;
+   save->xss  = svm->vcpu.arch.ia32_xss;
+
+   /*
+* SEV-ES will use a VMSA that is pointed to by the VMCB, not
+* the traditional VMSA that is part of the VMCB. Copy the
+* traditional VMSA as it has been built so far (in prep
+* for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state.
+*/
+   memcpy(svm->vmsa, save, sizeof(*save));
+
+   return 0;
+}
+
+static int sev_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)
+{
+   struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
+   struct sev_data_launch_update_vmsa *vmsa;
+   int i, ret;
+
+   if (!sev_es_guest(kvm))
+   return -ENOTTY;
+
+   vmsa = kzalloc(sizeof(*vmsa), GFP_KERNEL);
+   if (!vmsa)
+   return -ENOMEM;
+
+   for (i = 0; i < kvm->created_vcpus; i++) {
+   struct vcpu_svm *svm = to_svm(kvm->vcpus[i]);
+
+   /* Perform some pre-encryption checks against the VMSA */
+   ret = sev_es_sync_vmsa(svm);
+   if (ret)
+   goto e_free;
+
+   /*
+* The LAUNCH_UPDATE_VMSA command will perform in-place
+* encryption of the VMSA memory content (i.e it will write
+* the same memory region with the guest's key), so invalidate
+* it first.
+*/
+   clflush_cache_range(svm->vmsa, PAGE_SIZE);
+
+   vmsa->handle = sev->handle;
+   vmsa->address = __sme_pa(svm->vmsa);
+   vmsa->len = PAGE_SIZE;
+   ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_VMSA, vmsa,
+   &argp->error);
+   if (ret)
+   goto e_free;
+
+   svm->vcpu.arch.guest_state_protected = true;
+   }
+
+e_free:
+   kfree(vmsa);
+   return ret;
+}
+
 static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)