[PATCH v7 01/10] x86/sev: Add per-CPU CA runtime data and CA get/put helpers

From: Melody Wang

Date: Sun Dec 06 2026 - 00:13:56 EST


SVSM calls pass their arguments through a Calling Area (CA). The CA is
per-CPU, so a nested SVSM call on the same CPU clobbers the arguments of
the call it interrupted.

This can happen in the SVSM CALL -> NMI -> SVSM CALL case: the NMI hits
while the interrupted context is using the CA, and the NMI handler
itself raises a second SVSM call which overwrites the CA contents the
first call still needs. Interrupts are already disabled around SVSM
calls, but that does not keep NMIs out.

The GHCB has the same problem and solves it with a per-CPU backup page
plus an in-use flag. Do the same for the CA.

No functional change for guests running without an SVSM.

Signed-off-by: Melody Wang <huibo.wang@xxxxxxx>
---
arch/x86/coco/sev/internal.h | 39 ++++++++++++++++++++++++
arch/x86/coco/sev/noinstr.c | 57 ++++++++++++++++++++++++++++++++++++
2 files changed, 96 insertions(+)

diff --git a/arch/x86/coco/sev/internal.h b/arch/x86/coco/sev/internal.h
index 2e2ff48a7aae..96e89977fc2a 100644
--- a/arch/x86/coco/sev/internal.h
+++ b/arch/x86/coco/sev/internal.h
@@ -7,6 +7,36 @@
extern u64 sev_hv_features;
extern u64 sev_secrets_pa;

+struct svsm_ca_runtime_data {
+ /*
+ * The CA registered with the SVSM for this CPU: the boot CA for the
+ * BSP, registered via SVSM_CORE_REMAP_CA during early boot, and the
+ * CA for each AP via SVSM_CORE_CREATE_VCPU.
+ *
+ * Unlike the GHCB, whose GPA is handed to the hypervisor on every
+ * VMGEXIT, the CA GPA is registered state inside the SVSM. It cannot
+ * be swapped for another page on the fly, only its contents can be
+ * saved and restored.
+ */
+ struct svsm_ca *ca_page;
+ u64 caa_pa;
+
+ /*
+ * Mark the per-cpu CAs as in-use to detect a nested SVSM call.
+ * There is no need for it to be atomic, because nothing is written to
+ * the CA between the read and the write of ca_active. So it is safe
+ * to use it when a nested SVSM call happens before the write.
+ *
+ * This is necessary for example in the SVSM CALL->NMI->SVSM CALL case
+ * when the NMI happens while the first SVSM call uses the CA. When the
+ * NMI code raises a second SVSM call it might overwrite the contents of
+ * the CA written by the first SVSM call. To avoid this the content of
+ * the CA is saved and restored when the CA is detected to be in use
+ * already.
+ */
+ bool ca_active;
+};
+
/* #VC handler runtime per-CPU data */
struct sev_es_runtime_data {
struct ghcb ghcb_page;
@@ -42,12 +72,18 @@ struct sev_es_runtime_data {
* is currently unsupported in SEV-ES guests.
*/
unsigned long dr7;
+
+ struct svsm_ca_runtime_data *ca_runtime_data;
};

struct ghcb_state {
struct ghcb *ghcb;
};

+struct caa_state {
+ struct svsm_ca *caa;
+};
+
extern struct svsm_ca boot_svsm_ca_page;

struct ghcb *__sev_get_ghcb(struct ghcb_state *state);
@@ -105,6 +141,9 @@ static inline u64 svsm_get_caa_pa(void)
return boot_svsm_caa_pa;
}

+struct svsm_ca *__svsm_get_caa(struct caa_state *state);
+void __svsm_put_caa(struct caa_state *state);
+
static inline void __pval_terminate(u64 pfn, bool action, unsigned int page_size,
int ret, u64 svsm_ret)
{
diff --git a/arch/x86/coco/sev/noinstr.c b/arch/x86/coco/sev/noinstr.c
index 8ff556d28780..fb178ee1e60a 100644
--- a/arch/x86/coco/sev/noinstr.c
+++ b/arch/x86/coco/sev/noinstr.c
@@ -190,3 +190,60 @@ noinstr void __sev_put_ghcb(struct ghcb_state *state)
data->ghcb_active = false;
}
}
+
+/*
+ * Nothing shall interrupt this code path while holding the per-CPU
+ * CA. The backup CA is only for NMIs interrupting this path.
+ *
+ * Callers must disable interrupts around it.
+ */
+noinstr struct svsm_ca *__svsm_get_caa(struct caa_state *state)
+{
+ struct svsm_ca_runtime_data *data;
+ struct svsm_ca *caa;
+
+ WARN_ON(!irqs_disabled());
+
+ if (!sev_cfg.use_cas) {
+ state->caa = NULL;
+
+ return rip_rel_ptr(&boot_svsm_ca_page);
+ }
+
+ data = this_cpu_read(runtime_data)->ca_runtime_data;
+ caa = data->ca_page;
+
+ if (unlikely(data->ca_active)) {
+ /* CA is already in use - save its contents */
+ /* Backup CAA content */
+ *state->caa = *caa;
+ } else {
+ state->caa = NULL;
+ data->ca_active = true;
+ }
+
+ return caa;
+}
+
+noinstr void __svsm_put_caa(struct caa_state *state)
+{
+ struct svsm_ca_runtime_data *data;
+ struct svsm_ca *caa;
+
+ WARN_ON(!irqs_disabled());
+
+ if (!sev_cfg.use_cas)
+ return;
+
+ data = this_cpu_read(runtime_data)->ca_runtime_data;
+ caa = data->ca_page;
+
+ if (state->caa) {
+ /* Restore CAA from backup */
+ *caa = *state->caa;
+
+ state->caa = NULL;
+ } else {
+ data->ca_active = false;
+ }
+}
--
2.43.0