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 v4 3/7] x86/sev: Route unsupported APIC register accesses to the hypervisor APIC emulation
Date: Tue, 13 Oct 2026 17:50:50 +0000 [thread overview]
Message-ID: <2646bd1474e25dcb837fef5a3c24269664432a8a.1791912902.git.huibo.wang@amd.com> (raw)
In-Reply-To: <cover.1791912902.git.huibo.wang@amd.com>
The SVSM APIC emulation supports only a subset of the X2APIC MSRs.
Right now only basic APIC functionality related to interrupt delivery is
supported. Therefore, route the unsupported ones to the hypervisor's
X2APIC emulation.
Signed-off-by: Melody Wang <huibo.wang@amd.com>
---
arch/x86/coco/sev/core.c | 46 +++++++++++------------------
arch/x86/include/asm/sev-common.h | 1 +
arch/x86/include/asm/sev.h | 8 ++---
arch/x86/kernel/apic/x2apic_savic.c | 8 ++---
arch/x86/kernel/apic/x2apic_svsm.c | 11 +++++--
5 files changed, 35 insertions(+), 39 deletions(-)
diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
index 900a7ff8b639..99df8d7f3e98 100644
--- a/arch/x86/coco/sev/core.c
+++ b/arch/x86/coco/sev/core.c
@@ -972,7 +972,7 @@ int __init sev_es_efi_map_ghcbs_cas(pgd_t *pgd)
return 0;
}
-u64 savic_ghcb_msr_read(u32 reg)
+static u64 __sev_apic_ghcb_msr_rw(u32 reg, u64 value, bool write)
{
u64 msr = APIC_BASE_MSR + (reg >> 4);
struct pt_regs regs = { .cx = msr };
@@ -981,49 +981,37 @@ u64 savic_ghcb_msr_read(u32 reg)
enum es_result res;
struct ghcb *ghcb;
+ if (write) {
+ regs.ax = lower_32_bits(value);
+ regs.dx = upper_32_bits(value);
+ }
+
guard(irqsave)();
ghcb = __sev_get_ghcb(&state);
vc_ghcb_invalidate(ghcb);
- res = __vc_handle_msr(ghcb, &ctxt, false);
+ res = __vc_handle_msr(ghcb, &ctxt, write);
if (res != ES_OK) {
- pr_err("Secure AVIC MSR (0x%llx) read returned error (%d)\n", msr, res);
- /* MSR read failures are treated as fatal errors */
- sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SAVIC_FAIL);
+ pr_err("APIC MSR via GHCB (0x%llx) %s returned error (%d)\n", msr, write ? "write" : "read", res);
+ /* MSR read/write failures are treated as fatal errors */
+ sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_APIC_MSR_FAIL);
}
__sev_put_ghcb(&state);
+ /* The return value will be ignored by the caller for a write */
return regs.ax | regs.dx << 32;
}
-void savic_ghcb_msr_write(u32 reg, u64 value)
+u64 sev_apic_ghcb_msr_read(u32 reg)
{
- u64 msr = APIC_BASE_MSR + (reg >> 4);
- struct pt_regs regs = {
- .cx = msr,
- .ax = lower_32_bits(value),
- .dx = upper_32_bits(value)
- };
- struct es_em_ctxt ctxt = { .regs = ®s };
- struct ghcb_state state;
- enum es_result res;
- struct ghcb *ghcb;
-
- guard(irqsave)();
-
- ghcb = __sev_get_ghcb(&state);
- vc_ghcb_invalidate(ghcb);
-
- res = __vc_handle_msr(ghcb, &ctxt, true);
- if (res != ES_OK) {
- pr_err("Secure AVIC MSR (0x%llx) write returned error (%d)\n", msr, res);
- /* MSR writes should never fail. Any failure is fatal error for SNP guest */
- sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SAVIC_FAIL);
- }
+ return __sev_apic_ghcb_msr_rw(reg, 0, false);
+}
- __sev_put_ghcb(&state);
+void sev_apic_ghcb_msr_write(u32 reg, u64 value)
+{
+ __sev_apic_ghcb_msr_rw(reg, value, true);
}
enum es_result savic_register_gpa(u64 gpa)
diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h
index 095fbafe8a2c..585ddc44b6b4 100644
--- a/arch/x86/include/asm/sev-common.h
+++ b/arch/x86/include/asm/sev-common.h
@@ -210,6 +210,7 @@ struct snp_psc_desc {
#define GHCB_TERM_SVSM_CA_REMAP_FAIL 11 /* SVSM is present but CA could not be remapped */
#define GHCB_TERM_SAVIC_FAIL 12 /* Secure AVIC-specific failure */
#define GHCB_TERM_ALT_INJ_FAIL 13 /* Alternate Injection-specific failure */
+#define GHCB_TERM_APIC_MSR_FAIL 14 /* APIC MSR failure */
#define GHCB_RESP_CODE(v) ((v) & GHCB_MSR_INFO_MASK)
diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
index ea248d9e1c49..93161b663d9b 100644
--- a/arch/x86/include/asm/sev.h
+++ b/arch/x86/include/asm/sev.h
@@ -541,8 +541,8 @@ void __init snp_secure_tsc_prepare(void);
void __init snp_secure_tsc_init(void);
enum es_result savic_register_gpa(u64 gpa);
enum es_result savic_unregister_gpa(u64 *gpa);
-u64 savic_ghcb_msr_read(u32 reg);
-void savic_ghcb_msr_write(u32 reg, u64 value);
+u64 sev_apic_ghcb_msr_read(u32 reg);
+void sev_apic_ghcb_msr_write(u32 reg, u64 value);
static __always_inline void vc_ghcb_invalidate(struct ghcb *ghcb)
{
@@ -650,8 +650,8 @@ static inline void __init snp_secure_tsc_init(void) { }
static inline void sev_evict_cache(void *va, int npages) {}
static inline enum es_result savic_register_gpa(u64 gpa) { return ES_UNSUPPORTED; }
static inline enum es_result savic_unregister_gpa(u64 *gpa) { return ES_UNSUPPORTED; }
-static inline void savic_ghcb_msr_write(u32 reg, u64 value) { }
-static inline u64 savic_ghcb_msr_read(u32 reg) { return 0; }
+static inline void sev_apic_ghcb_msr_write(u32 reg, u64 value) { }
+static inline u64 sev_apic_ghcb_msr_read(u32 reg) { return 0; }
#endif /* CONFIG_AMD_MEM_ENCRYPT */
diff --git a/arch/x86/kernel/apic/x2apic_savic.c b/arch/x86/kernel/apic/x2apic_savic.c
index 4bc6d7e018a5..990e4a379e07 100644
--- a/arch/x86/kernel/apic/x2apic_savic.c
+++ b/arch/x86/kernel/apic/x2apic_savic.c
@@ -72,7 +72,7 @@ static u32 savic_read(u32 reg)
case APIC_LVT0:
case APIC_LVT1:
case APIC_LVTERR:
- return savic_ghcb_msr_read(reg);
+ return sev_apic_ghcb_msr_read(reg);
case APIC_ID:
case APIC_LVR:
case APIC_TASKPRI:
@@ -193,7 +193,7 @@ static void savic_icr_write(u32 icr_low, u32 icr_high)
icr_data = ((u64)icr_high) << 32 | icr_low;
if (dsh != APIC_DEST_SELF)
- savic_ghcb_msr_write(APIC_ICR, icr_data);
+ sev_apic_ghcb_msr_write(APIC_ICR, icr_data);
apic_set_reg64(this_cpu_ptr(savic_page), APIC_ICR, icr_data);
}
@@ -210,7 +210,7 @@ static void savic_write(u32 reg, u32 data)
case APIC_LVTTHMR:
case APIC_LVTPC:
case APIC_LVTERR:
- savic_ghcb_msr_write(reg, data);
+ sev_apic_ghcb_msr_write(reg, data);
break;
case APIC_TASKPRI:
case APIC_EOI:
@@ -316,7 +316,7 @@ static void savic_eoi(void)
* interrupts. Return to the guest from GHCB protocol event takes
* care of re-evaluating interrupt state.
*/
- savic_ghcb_msr_write(APIC_EOI, 0);
+ sev_apic_ghcb_msr_write(APIC_EOI, 0);
} else {
/*
* Hardware clears APIC_ISR and re-evaluates the interrupt state
diff --git a/arch/x86/kernel/apic/x2apic_svsm.c b/arch/x86/kernel/apic/x2apic_svsm.c
index 6edfbcca2958..0a89c5b357f0 100644
--- a/arch/x86/kernel/apic/x2apic_svsm.c
+++ b/arch/x86/kernel/apic/x2apic_svsm.c
@@ -81,8 +81,15 @@ static u32 __svsm_apic_msr_rw(u32 reg, u32 v, bool write)
}
break;
default:
- pr_err("%s 0x%x not supported\n", call_reg_str, reg);
- return 0;
+ if (write) {
+ /*
+ * Even if this will end up returning call.rdx_out,
+ * that is being ignored by the caller for a write.
+ */
+ sev_apic_ghcb_msr_write(reg, v);
+ } else {
+ return sev_apic_ghcb_msr_read(reg);
+ }
}
return call.rdx_out;
--
2.43.0
next prev parent reply other threads:[~2026-10-13 17:51 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 17:50 [PATCH v4 0/7] Alternate Injection: Secure Interrupt Delivery for SEV-SNP Guests - Guest Support Melody Wang
2026-10-13 17:50 ` [PATCH v4 1/7] x86/sev: Add support for Alternate Injection Melody Wang
2026-10-13 17:50 ` [PATCH v4 2/7] x86/apic: Add an SVSM APIC driver Melody Wang
2026-10-13 17:50 ` Melody Wang [this message]
2026-10-13 17:50 ` [PATCH v4 4/7] x86/sev: Add a function to contain all SEV-specific setup operations Melody Wang
2026-10-13 17:50 ` [PATCH v4 5/7] x86/sev: Register the guest with the SVSM APIC protocol Melody Wang
2026-10-14 9:43 ` Ard Biesheuvel
2026-10-15 3:56 ` Melody Wang
2026-10-15 6:04 ` Ard Biesheuvel
2026-10-13 17:50 ` [PATCH v4 6/7] x86/sev: Allow the guest to configure interrupt vectors for the hypervisor Melody Wang
2026-10-13 17:50 ` [PATCH v4 7/7] 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=2646bd1474e25dcb837fef5a3c24269664432a8a.1791912902.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®