[PATCH v7 03/10] x86/sev: Implement backup CA
From: Melody Wang
Date: Sun Dec 06 2026 - 00:14:16 EST
Give each CPU a backup CA so an interrupting call can save and restore
the in-use CA around its own call. If both CAs are already in use there
is no safe way to proceed, so panic, as the GHCB code does.
No functional change for guests running without an SVSM.
Signed-off-by: Melody Wang <huibo.wang@xxxxxxx>
---
arch/x86/coco/sev/core.c | 4 +++-
arch/x86/coco/sev/internal.h | 10 ++++++++++
arch/x86/coco/sev/noinstr.c | 19 +++++++++++++++++++
3 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
index d4cd7a0d469f..91a3ff5b1f27 100644
--- a/arch/x86/coco/sev/core.c
+++ b/arch/x86/coco/sev/core.c
@@ -1237,8 +1237,10 @@ static void __init init_ghcb_and_ca(int cpu)
data->ghcb_active = false;
data->backup_ghcb_active = false;
- if (snp_vmpl)
+ if (snp_vmpl) {
data->ca_runtime_data->ca_active = false;
+ data->ca_runtime_data->backup_ca_active = false;
+ }
}
void __init sev_es_init_vc_handling(void)
diff --git a/arch/x86/coco/sev/internal.h b/arch/x86/coco/sev/internal.h
index 540b1a2b1946..8f5dd898b244 100644
--- a/arch/x86/coco/sev/internal.h
+++ b/arch/x86/coco/sev/internal.h
@@ -21,6 +21,15 @@ struct svsm_ca_runtime_data {
struct svsm_ca *ca_page;
u64 caa_pa;
+ /*
+ * Reserve one page per CPU as backup storage for CA.
+ * It is needed when an NMI happens while the SVSM call uses the real
+ * CA, and the NMI handler itself is causing another SVSM call. In
+ * that case the CA content of the first SVSM call needs to be backed up
+ * and restored.
+ */
+ struct svsm_ca backup_ca;
+
/*
* 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
@@ -35,6 +44,7 @@ struct svsm_ca_runtime_data {
* already.
*/
bool ca_active;
+ bool backup_ca_active;
};
/* #VC handler runtime per-CPU data */
diff --git a/arch/x86/coco/sev/noinstr.c b/arch/x86/coco/sev/noinstr.c
index fb178ee1e60a..9e85f64f07ac 100644
--- a/arch/x86/coco/sev/noinstr.c
+++ b/arch/x86/coco/sev/noinstr.c
@@ -215,6 +215,24 @@ noinstr struct svsm_ca *__svsm_get_caa(struct caa_state *state)
if (unlikely(data->ca_active)) {
/* CA is already in use - save its contents */
+ if (unlikely(data->backup_ca_active)) {
+ /*
+ * Backup-CA is also already in use. There is no way
+ * to continue here so just kill the machine.
+ */
+ data->ca_active = false;
+ data->backup_ca_active = false;
+
+ instrumentation_begin();
+ panic("CA and backup CA already in use. Giving up");
+ instrumentation_end();
+ }
+
+ /* Mark backup_ca active before writing to it */
+ data->backup_ca_active = true;
+
+ state->caa = &data->backup_ca;
+
/* Backup CAA content */
*state->caa = *caa;
} else {
@@ -242,6 +260,7 @@ noinstr void __svsm_put_caa(struct caa_state *state)
/* Restore CAA from backup */
*caa = *state->caa;
+ data->backup_ca_active = false;
state->caa = NULL;
} else {
data->ca_active = false;
--
2.43.0