From: Melody Wang <huibo.wang@amd.com>
To: <x86@kernel.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
Tom Lendacky <thomas.lendacky@amd.com>, <ardb@kernel.org>,
Melody Wang <huibo.wang@amd.com>
Subject: [PATCH v7 01/10] x86/sev: Add per-CPU CA runtime data and CA get/put helpers
Date: Sun, 6 Dec 2026 05:13:03 +0000 [thread overview]
Message-ID: <74ca13f71d6eb43aa85ff906d33f27e469b5f163.1796524449.git.huibo.wang@amd.com> (raw)
In-Reply-To: <cover.1796524449.git.huibo.wang@amd.com>
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@amd.com>
---
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
next prev parent reply other threads:[~2026-12-06 5:13 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-06 5:13 [PATCH v7 00/10] Alternate Injection: Secure Interrupt Delivery for SEV-SNP Guests - Guest Support Melody Wang
2026-12-06 5:13 ` Melody Wang [this message]
2026-12-06 5:13 ` [PATCH v7 02/10] x86/sev: Switch the CA from per-CPU variables to the per-CPU runtime data Melody Wang
2026-12-06 5:13 ` [PATCH v7 03/10] x86/sev: Implement backup CA Melody Wang
2026-12-06 5:13 ` [PATCH v7 04/10] x86/sev: Add a config option for Alternate Injection Melody Wang
2026-12-06 5:13 ` [PATCH v7 05/10] x86/apic: Add an SVSM APIC driver Melody Wang
2026-12-06 5:13 ` [PATCH v7 06/10] x86/sev: Route unsupported APIC register accesses to the hypervisor APIC emulation Melody Wang
2026-12-06 5:13 ` [PATCH v7 07/10] x86/sev: Add a function to contain all SEV-specific setup operations Melody Wang
2026-12-06 5:13 ` [PATCH v7 08/10] x86/sev: Register Alternate Injection protocol during OVMF boot Melody Wang
2026-12-06 5:13 ` [PATCH v7 09/10] x86/sev: Allow the guest to configure interrupt vectors for the hypervisor Melody Wang
2026-12-06 5:13 ` [PATCH v7 10/10] x86/sev: Indicate that Alternate Injection is supported in the guest Melody Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=74ca13f71d6eb43aa85ff906d33f27e469b5f163.1796524449.git.huibo.wang@amd.com \
--to=huibo.wang@amd.com \
--cc=ardb@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=thomas.lendacky@amd.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®