From: Tom Lendacky <thomas.lendacky@amd.com>
To: Melody Wang <huibo.wang@amd.com>, x86@kernel.org
Cc: LKML <linux-kernel@vger.kernel.org>, ardb@kernel.org
Subject: Re: [PATCH v6 3/7] x86/sev: Route unsupported APIC register accesses to the hypervisor APIC emulation
Date: Fri, 20 Nov 2026 14:56:33 -0600 [thread overview]
Message-ID: <be974f51-e738-4933-b0f1-475d2e82fd03@amd.com> (raw)
In-Reply-To: <054e8a3991ed4f541afded6fb9622f08f7bd8526.1793926736.git.huibo.wang@amd.com>
On 11/5/26 19:25, Melody Wang wrote:
> 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 | 9 +++---
> 5 files changed, 31 insertions(+), 41 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);
I guess this kind of answsers my question in patch #2 about what to do
when there is an error.
> - }
> + 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 6496b376452f..d916a5dcd6af 100644
> --- a/arch/x86/include/asm/sev.h
> +++ b/arch/x86/include/asm/sev.h
> @@ -542,8 +542,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)
> {
> @@ -652,8 +652,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 fea61b929122..c7f7a9175e17 100644
> --- a/arch/x86/kernel/apic/x2apic_svsm.c
> +++ b/arch/x86/kernel/apic/x2apic_svsm.c
> @@ -68,7 +68,7 @@ static void svsm_apic_msr_write(u32 reg, u32 v)
> }
> break;
> default:
> - pr_err("SVSM_APIC_WRITE_REGISTER 0x%x not supported\n", reg);
> + sev_apic_ghcb_msr_write(reg, v);
> break;
> }
> }
> @@ -77,6 +77,7 @@ static u32 svsm_apic_msr_read(u32 reg)
> {
> u32 msr = APIC_BASE_MSR + (reg >> 4);
> struct svsm_call call = {};
> + u64 val;
> int ret;
>
> switch (reg) {
> @@ -100,17 +101,17 @@ static u32 svsm_apic_msr_read(u32 reg)
> call.rcx = msr;
>
> ret = svsm_perform_call_protocol(&call);
> + val = call.rdx_out;
Move this to after the "if" below.
Thanks,
Tom
> if (ret) {
> pr_err("SVSM_APIC_READ_REGISTER: 0x%x, error: %d\n", reg, ret);
> sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_ALT_INJ_FAIL);
> }
> break;
> default:
> - pr_err("SVSM_APIC_READ_REGISTER: 0x%x not supported\n", reg);
> - return 0;
> + val = sev_apic_ghcb_msr_read(reg);
> }
>
> - return call.rdx_out;
> + return val;
> }
>
> static inline void svsm_apic_msr_eoi(void)
next prev parent reply other threads:[~2026-11-20 20:56 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-06 1:25 [PATCH v6 0/7] Alternate Injection: Secure Interrupt Delivery for SEV-SNP Guests - Guest Support Melody Wang
2026-11-06 1:25 ` [PATCH v6 1/7] x86/sev: Add support for Alternate Injection Melody Wang
2026-11-08 2:12 ` Borislav Petkov
2026-11-20 17:04 ` Tom Lendacky
2026-11-06 1:25 ` [PATCH v6 2/7] x86/apic: Add an SVSM APIC driver Melody Wang
2026-11-20 20:47 ` Tom Lendacky
2026-11-06 1:25 ` [PATCH v6 3/7] x86/sev: Route unsupported APIC register accesses to the hypervisor APIC emulation Melody Wang
2026-11-19 6:19 ` Borislav Petkov
2026-11-20 20:56 ` Tom Lendacky [this message]
2026-11-06 1:25 ` [PATCH v6 4/7] x86/sev: Add a function to contain all SEV-specific setup operations Melody Wang
2026-11-06 1:25 ` [PATCH v6 5/7] x86/sev: Register Alternate Injection protocol during OVMF boot Melody Wang
2026-11-06 1:25 ` [PATCH v6 6/7] x86/sev: Allow the guest to configure interrupt vectors for the hypervisor Melody Wang
2026-11-06 1:25 ` [PATCH v6 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=be974f51-e738-4933-b0f1-475d2e82fd03@amd.com \
--to=thomas.lendacky@amd.com \
--cc=ardb@kernel.org \
--cc=huibo.wang@amd.com \
--cc=linux-kernel@vger.kernel.org \
--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®