[PATCH Part1 RFC v3 10/22] x86/sev: Register GHCB memory when SEV-SNP is active

From: Brijesh Singh
Date: Wed Jun 02 2021 - 10:05:45 EST


The SEV-SNP guest is required to perform GHCB GPA registration. This is
because the hypervisor may prefer that a guest use a consistent and/or
specific GPA for the GHCB associated with a vCPU. For more information,
see the GHCB specification section GHCB GPA Registration.

During the boot, init_ghcb() allocates a per-cpu GHCB page. On very first
VC exception, the exception handler switch to using the per-cpu GHCB page
allocated during the init_ghcb(). The GHCB page must be registered in
the current vcpu context.

Signed-off-by: Brijesh Singh <brijesh.singh@xxxxxxx>
---
arch/x86/kernel/sev-internal.h | 12 ++++++++++++
arch/x86/kernel/sev.c | 28 ++++++++++++++++++++++++++++
2 files changed, 40 insertions(+)
create mode 100644 arch/x86/kernel/sev-internal.h

diff --git a/arch/x86/kernel/sev-internal.h b/arch/x86/kernel/sev-internal.h
new file mode 100644
index 000000000000..d23c81013a8e
--- /dev/null
+++ b/arch/x86/kernel/sev-internal.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Forward declarations for sev-shared.c
+ *
+ * Author: Brijesh Singh <brijesh.singh@xxxxxxx>
+ */
+
+#ifndef _ARCH_X86_KERNEL_SEV_INTERNAL_H
+
+static void snp_register_ghcb_early(unsigned long paddr);
+
+#endif /* _ARCH_X86_KERNEL_SEV_INTERNAL_H */
diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c
index 9b70b7332614..455c09a9b2c2 100644
--- a/arch/x86/kernel/sev.c
+++ b/arch/x86/kernel/sev.c
@@ -32,6 +32,8 @@
#include <asm/smp.h>
#include <asm/cpu.h>

+#include "sev-internal.h"
+
#define DR7_RESET_VALUE 0x400

/* For early boot hypervisor communication in SEV-ES enabled guests */
@@ -88,6 +90,13 @@ struct sev_es_runtime_data {
* is currently unsupported in SEV-ES guests.
*/
unsigned long dr7;
+
+ /*
+ * SEV-SNP requires that the GHCB must be registered before using it.
+ * The flag below will indicate whether the GHCB is registered, if its
+ * not registered then sev_es_get_ghcb() will perform the registration.
+ */
+ bool snp_ghcb_registered;
};

struct ghcb_state {
@@ -192,6 +201,16 @@ void noinstr __sev_es_ist_exit(void)
this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], *(unsigned long *)ist);
}

+static void snp_register_ghcb(struct sev_es_runtime_data *data, unsigned long paddr)
+{
+ if (data->snp_ghcb_registered)
+ return;
+
+ snp_register_ghcb_early(paddr);
+
+ data->snp_ghcb_registered = true;
+}
+
static __always_inline struct ghcb *sev_es_get_ghcb(struct ghcb_state *state)
{
struct sev_es_runtime_data *data;
@@ -218,6 +237,10 @@ static __always_inline struct ghcb *sev_es_get_ghcb(struct ghcb_state *state)
data->ghcb_active = true;
}

+ /* SEV-SNP guest requires that GHCB must be registered. */
+ if (sev_feature_enabled(SEV_SNP))
+ snp_register_ghcb(data, __pa(ghcb));
+
return ghcb;
}

@@ -622,6 +645,10 @@ static bool __init sev_es_setup_ghcb(void)
/* Alright - Make the boot-ghcb public */
boot_ghcb = &boot_ghcb_page;

+ /* SEV-SNP guest requires that GHCB GPA must be registered. */
+ if (sev_feature_enabled(SEV_SNP))
+ snp_register_ghcb_early(__pa(&boot_ghcb_page));
+
return true;
}

@@ -711,6 +738,7 @@ static void __init init_ghcb(int cpu)

data->ghcb_active = false;
data->backup_ghcb_active = false;
+ data->snp_ghcb_registered = false;
}

void __init sev_es_init_vc_handling(void)
--
2.17.1