mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 2/7] x86/apic: Add an SVSM APIC driver
Date: Fri, 20 Nov 2026 14:47:59 -0600	[thread overview]
Message-ID: <65f8c473-8931-4e94-9d03-29175ade5fbd@amd.com> (raw)
In-Reply-To: <c8889d168ee6ed808cd96b461446a1e0f0114755.1793926736.git.huibo.wang@amd.com>

On 11/5/26 19:25, Melody Wang wrote:
> Alternate Injection replaces hypervisor-based interrupt queuing and
> event injection in favor of guest-controlled queuing and injection. In
> order to complish that, an SVSM is used to update the guest VMSA to
> perform the required actions.
> 
> The guest uses the SVSM APIC protocol to communicate with the SVSM to
> perform selected APIC related operations instead of using standard APIC
> MSR access.
> 
> Add such a SVSM APIC driver (which implements a subset of an X2APIC),
> for the APIC emulation supported by the SVSM.
> 
> Signed-off-by: Melody Wang <huibo.wang@amd.com>
> ---
>  arch/x86/include/asm/sev-common.h  |   1 +
>  arch/x86/include/asm/sev.h         |   9 ++
>  arch/x86/kernel/apic/Makefile      |   1 +
>  arch/x86/kernel/apic/x2apic_svsm.c | 252 +++++++++++++++++++++++++++++
>  4 files changed, 263 insertions(+)
>  create mode 100644 arch/x86/kernel/apic/x2apic_svsm.c
> 
> diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h
> index 01a6e4dbe423..095fbafe8a2c 100644
> --- a/arch/x86/include/asm/sev-common.h
> +++ b/arch/x86/include/asm/sev-common.h
> @@ -209,6 +209,7 @@ struct snp_psc_desc {
>  #define GHCB_TERM_SECURE_TSC		10	/* Secure TSC initialization failed */
>  #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_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 c7ea9845351e..6496b376452f 100644
> --- a/arch/x86/include/asm/sev.h
> +++ b/arch/x86/include/asm/sev.h
> @@ -435,6 +435,13 @@ struct svsm_call {
>  #define SVSM_VTPM_QUERY			0
>  #define SVSM_VTPM_CMD			1
>  
> +#define SVSM_APIC_CALL(x)		((3ULL << 32) | (x))
> +#define SVSM_APIC_QUERY_FEATURES	0
> +#define SVSM_APIC_CONFIG_EMULATION	1
> +#define SVSM_APIC_READ_REGISTER		2
> +#define SVSM_APIC_WRITE_REGISTER	3
> +#define SVSM_APIC_CONFIG_VECTOR		4
> +
>  #ifdef CONFIG_AMD_MEM_ENCRYPT
>  
>  extern u8 snp_vmpl;
> @@ -529,6 +536,7 @@ void snp_msg_free(struct snp_msg_desc *mdesc);
>  int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req);
>  
>  int snp_svsm_vtpm_send_command(u8 *buffer);
> +int svsm_perform_call_protocol(struct svsm_call *call);
>  
>  void __init snp_secure_tsc_prepare(void);
>  void __init snp_secure_tsc_init(void);
> @@ -638,6 +646,7 @@ static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
>  static inline int snp_send_guest_request(struct snp_msg_desc *mdesc,
>  					 struct snp_guest_req *req) { return -ENODEV; }
>  static inline int snp_svsm_vtpm_send_command(u8 *buffer) { return -ENODEV; }
> +static inline int svsm_perform_call_protocol(struct svsm_call *call) { return -ENODEV; }
>  static inline void __init snp_secure_tsc_prepare(void) { }
>  static inline void __init snp_secure_tsc_init(void) { }
>  static inline void sev_evict_cache(void *va, int npages) {}
> diff --git a/arch/x86/kernel/apic/Makefile b/arch/x86/kernel/apic/Makefile
> index 581db89477f9..fef51024025b 100644
> --- a/arch/x86/kernel/apic/Makefile
> +++ b/arch/x86/kernel/apic/Makefile
> @@ -19,6 +19,7 @@ ifeq ($(CONFIG_X86_64),y)
>  obj-$(CONFIG_X86_NUMACHIP)	+= apic_numachip.o
>  obj-$(CONFIG_X86_UV)		+= x2apic_uv_x.o
>  obj-$(CONFIG_AMD_SECURE_AVIC)	+= x2apic_savic.o
> +obj-$(CONFIG_AMD_ALTERNATE_INJ)	+= x2apic_svsm.o
>  obj-$(CONFIG_X86_X2APIC)	+= x2apic_phys.o
>  obj-$(CONFIG_X86_X2APIC)	+= x2apic_cluster.o
>  obj-y				+= apic_flat_64.o
> diff --git a/arch/x86/kernel/apic/x2apic_svsm.c b/arch/x86/kernel/apic/x2apic_svsm.c
> new file mode 100644
> index 000000000000..fea61b929122
> --- /dev/null
> +++ b/arch/x86/kernel/apic/x2apic_svsm.c
> @@ -0,0 +1,252 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * AMD Alternate Injection Support (SEV-SNP Guests)
> + *
> + * Copyright (C) 2026 Advanced Micro Devices, Inc.
> + *
> + * Author: Melody Wang <huibo.wang@amd.com>
> + */
> +
> +#include <linux/cpumask.h>
> +#include <linux/cc_platform.h>
> +
> +#include <asm/apic.h>
> +#include <asm/sev.h>
> +
> +#include "local.h"
> +
> +static int svsm_apic_probe(void)
> +{
> +	if (!cc_platform_has(CC_ATTR_SNP_ALTERNATE_INJECTION)) {
> +		return 0;
> +	} else {

No need for an else here if the "if" above has a return.

> +		if (!snp_vmpl) {
> +			pr_err("Alternate Injection in VMPL0 impossible. Terminating.\n");
> +			goto terminate;
> +		}
> +	}
> +
> +	if (!x2apic_mode) {
> +		pr_err("Alternate Injection in non x2APIC mode impossible. Terminating.\n");
> +		goto terminate;
> +	}
> +
> +	pr_info("Alternate Injection SVSM APIC enabled\n");
> +
> +	return 1;
> +
> +terminate:
> +	sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_ALT_INJ_FAIL);

Should you come up with individual fail codes for each of the above?

> +	return -EINVAL;
> +
> +}
> +
> +static int svsm_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
> +{
> +	return x2apic_enabled() && cc_platform_has(CC_ATTR_SNP_ALTERNATE_INJECTION) && snp_vmpl;
> +}
> +
> +static void svsm_apic_msr_write(u32 reg, u32 v)
> +{
> +	u32 msr = APIC_BASE_MSR + (reg >> 4);
> +	struct svsm_call call = {};
> +	int ret;
> +
> +	switch (reg) {
> +	case APIC_TASKPRI:
> +	case APIC_EOI:
> +	case APIC_ICR:
> +	case APIC_SELF_IPI:
> +		call.rax = SVSM_APIC_CALL(SVSM_APIC_WRITE_REGISTER);
> +		call.rcx = msr;
> +		call.rdx = v;
> +
> +		ret = svsm_perform_call_protocol(&call);
> +		if (ret) {
> +			pr_err("SVSM_APIC_WRITE_REGISTER: 0x%x, error: %d\n", reg, ret);
> +			sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_ALT_INJ_FAIL);

Do we really want to terminate here vs just report the error?

> +		}
> +		break;
> +	default:
> +		pr_err("SVSM_APIC_WRITE_REGISTER 0x%x not supported\n", reg);
> +		break;
> +	}
> +}
> +
> +static u32 svsm_apic_msr_read(u32 reg)
> +{
> +	u32 msr = APIC_BASE_MSR + (reg >> 4);
> +	struct svsm_call call = {};
> +	int ret;
> +
> +	switch (reg) {
> +	case APIC_ID:
> +	case APIC_TASKPRI:
> +	case APIC_PROCPRI:
> +	case APIC_ISR ... APIC_ISR + 0x70:
> +	case APIC_TMR ... APIC_TMR + 0x70:
> +		if (WARN_ONCE(!IS_ALIGNED(reg, 16),
> +			      "SVSM APIC register read offset 0x%x not aligned at 16 bytes", reg))
> +			return 0;
> +		fallthrough;
> +	case APIC_IRR ... APIC_IRR + 0x70:
> +		if (WARN_ONCE(!(IS_ALIGNED(reg, 16) ||
> +				IS_ALIGNED(reg - 4, 16)),
> +			      "Misaligned APIC_IRR/ALLOWED_IRR SVSM APIC register read offset 0x%x", reg))
> +			return 0;
> +		fallthrough;
> +	case APIC_ICR:
> +		call.rax = SVSM_APIC_CALL(SVSM_APIC_READ_REGISTER);
> +		call.rcx = msr;
> +
> +		ret = svsm_perform_call_protocol(&call);
> +		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);

Ditto here... ?

> +		}
> +		break;
> +	default:
> +		pr_err("SVSM_APIC_READ_REGISTER: 0x%x not supported\n", reg);
> +		return 0;
> +	}
> +
> +	return call.rdx_out;
> +}
> +
> +static inline void svsm_apic_msr_eoi(void)
> +{
> +	svsm_apic_msr_write(APIC_EOI, APIC_EOI_ACK);
> +}
> +
> +static u64 __svsm_apic_icr_rw(u32 low, u32 id, bool write)
> +{
> +	struct svsm_call call = {};
> +	const char *call_reg_str;
> +	unsigned int call_reg;
> +	u32 reg;
> +	int ret;
> +
> +	reg = APIC_ICR;
> +
> +	call_reg = write ? SVSM_APIC_WRITE_REGISTER
> +			 : SVSM_APIC_READ_REGISTER;
> +
> +	call_reg_str = write ? "SVSM_APIC_WRITE_REGISTER"
> +			     : "SVSM_APIC_READ_REGISTER";

You only use call_reg and call_reg_str in one place each, no need to
create variables.

> +
> +	call.rax = SVSM_APIC_CALL(call_reg);

i.e.

	call.rax = write ? SVSM_APIC_CALL(SVSM_APIC_WRITE_REGISTER)
			 : SVSM_APIC_CALL(SVSM_APIC_READ_REGISTER);

> +	call.rcx = APIC_BASE_MSR + (reg >> 4);
> +	call.rdx = ((u64)id) << 32 | low;
> +
> +	ret = svsm_perform_call_protocol(&call);
> +	if (ret) {
> +		pr_err("%s: 0x%x, error: %d\n", call_reg_str, reg, ret);
> +		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_ALT_INJ_FAIL);
> +	}
> +
> +	return call.rdx_out;
> +}
> +
> +
> +static inline u64 svsm_apic_icr_read(void)
> +{
> +	return __svsm_apic_icr_rw(0, 0, false);
> +}
> +
> +static void svsm_apic_icr_write(u32 low, u32 id)
> +{
> +	__svsm_apic_icr_rw(low, id, true);
> +
> +}
> +
> +static void svsm_apic_send_IPI(int cpu, int vector)
> +{
> +	u32 dest = per_cpu(x86_cpu_to_apicid, cpu);
> +
> +	svsm_apic_icr_write(__prepare_ICR(0, vector, APIC_DEST_PHYSICAL), dest);
> +}
> +
> +static void __svsm_apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest)
> +{
> +	unsigned long query_cpu, this_cpu;
> +
> +	guard(irqsave)();
> +
> +	this_cpu = smp_processor_id();
> +	for_each_cpu(query_cpu, mask) {
> +		if (apic_dest == APIC_DEST_ALLBUT && this_cpu == query_cpu)
> +			continue;
> +
> +		svsm_apic_send_IPI(query_cpu, vector);
> +	}
> +}
> +
> +static void svsm_apic_send_IPI_mask(const struct cpumask *mask, int vector)
> +{
> +	__svsm_apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC);
> +}
> +
> +static void svsm_apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
> +{
> +	__svsm_apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT);
> +}
> +
> +static void __svsm_apic_send_IPI_shorthand(int vector, u32 which)
> +{
> +	svsm_apic_icr_write(__prepare_ICR(which, vector, 0), 0);
> +}
> +
> +static void svsm_apic_send_IPI_allbutself(int vector)
> +{
> +	__svsm_apic_send_IPI_shorthand(vector, APIC_DEST_ALLBUT);
> +}
> +
> +static void svsm_apic_send_IPI_all(int vector)
> +{
> +	__svsm_apic_send_IPI_shorthand(vector, APIC_DEST_ALLINC);
> +}
> +
> +static void svsm_apic_send_IPI_self(int vector)
> +{
> +	svsm_apic_msr_write(APIC_SELF_IPI, vector);
> +}
> +
> +static u32 svsm_apic_get_apic_id(u32 id)
> +{
> +	return id;
> +}
> +
> +static struct apic svsm_apic __ro_after_init = {
> +
> +	.name				= "svsm apic",
> +	.probe				= svsm_apic_probe,
> +	.acpi_madt_oem_check		= svsm_acpi_madt_oem_check,
> +
> +	.dest_mode_logical		= false,
> +
> +	.disable_esr			= 0,
> +
> +	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
> +
> +	.max_apic_id			= UINT_MAX,
> +	.x2apic_set_max_apicid		= true,
> +	.get_apic_id			= svsm_apic_get_apic_id,

Should this just use x2apic_get_apic_id ?

Thanks,
Tom

> +
> +	.calc_dest_apicid		= apic_default_calc_apicid,
> +
> +	.send_IPI			= svsm_apic_send_IPI,
> +	.send_IPI_mask			= svsm_apic_send_IPI_mask,
> +	.send_IPI_mask_allbutself	= svsm_apic_send_IPI_mask_allbutself,
> +	.send_IPI_allbutself		= svsm_apic_send_IPI_allbutself,
> +	.send_IPI_all			= svsm_apic_send_IPI_all,
> +	.send_IPI_self			= svsm_apic_send_IPI_self,
> +	.nmi_to_offline_cpu		= true,
> +
> +	.read				= svsm_apic_msr_read,
> +	.write				= svsm_apic_msr_write,
> +	.eoi				= svsm_apic_msr_eoi,
> +	.icr_read			= svsm_apic_icr_read,
> +	.icr_write			= svsm_apic_icr_write,
> +
> +};
> +apic_driver(svsm_apic);


  reply	other threads:[~2026-11-20 20:48 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 [this message]
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
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=65f8c473-8931-4e94-9d03-29175ade5fbd@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®