[PATCH v4 5/7] x86/sev: Register the guest with the SVSM APIC protocol

From: Melody Wang

Date: Tue Oct 13 2026 - 15:36:21 EST


The SVSM APIC protocol supports 5 calls. SVSM_APIC_CONFIGURE_EMULATION
(shortened to SVSM_APIC_CONFIG_EMULATION for brevity), call 1, provides
the controls whether the guest can make use of the SVSM APIC protocol.

Implement this call, and register Alternate Injection for the guest by
default.

Signed-off-by: Melody Wang <huibo.wang@xxxxxxx>
---
arch/x86/boot/compressed/sev.h | 2 --
arch/x86/include/asm/sev.h | 2 ++
drivers/firmware/efi/libstub/efistub.h | 11 ++++++++
drivers/firmware/efi/libstub/x86-stub.c | 34 +++++++++++++++++++++++++
include/linux/efi.h | 2 ++
5 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/arch/x86/boot/compressed/sev.h b/arch/x86/boot/compressed/sev.h
index 22637b416b46..62e50c2e71ed 100644
--- a/arch/x86/boot/compressed/sev.h
+++ b/arch/x86/boot/compressed/sev.h
@@ -14,7 +14,6 @@

void snp_accept_memory(phys_addr_t start, phys_addr_t end);
u64 sev_get_status(void);
-bool early_is_sevsnp_guest(void);

static inline u64 sev_es_rd_ghcb_msr(void)
{
@@ -37,7 +36,6 @@ static inline void sev_es_wr_ghcb_msr(u64 val)

static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }
static inline u64 sev_get_status(void) { return 0; }
-static inline bool early_is_sevsnp_guest(void) { return false; }

#endif

diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
index 93161b663d9b..13f2e8c009b4 100644
--- a/arch/x86/include/asm/sev.h
+++ b/arch/x86/include/asm/sev.h
@@ -471,6 +471,8 @@ static __always_inline void sev_es_nmi_complete(void)
extern int __init sev_es_efi_map_ghcbs_cas(pgd_t *pgd);
extern void sev_enable(struct boot_params *bp);

+bool early_is_sevsnp_guest(void);
+
/*
* RMPADJUST modifies the RMP permissions of a page of a lesser-
* privileged (numerically higher) VMPL.
diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h
index fd91fc15ec81..d65cdc8f2acd 100644
--- a/drivers/firmware/efi/libstub/efistub.h
+++ b/drivers/firmware/efi/libstub/efistub.h
@@ -177,6 +177,17 @@ void efi_set_u64_split(u64 data, u32 *lo, u32 *hi)
*/
#define EFI_MMAP_NR_SLACK_SLOTS 32

+typedef union sev_alt_inj_register_protocol sev_alt_inj_register_protocol_t;
+union sev_alt_inj_register_protocol {
+ struct {
+ efi_status_t (__efiapi * sev_register_alt_inj)
+ (sev_alt_inj_register_protocol_t *);
+ };
+ struct {
+ u32 sev_register_alt_inj;
+ } mixed_mode;
+};
+
typedef struct efi_generic_dev_path efi_device_path_protocol_t;

union efi_device_path_to_text_protocol {
diff --git a/drivers/firmware/efi/libstub/x86-stub.c b/drivers/firmware/efi/libstub/x86-stub.c
index 2a1fa773d241..b6e58ade4162 100644
--- a/drivers/firmware/efi/libstub/x86-stub.c
+++ b/drivers/firmware/efi/libstub/x86-stub.c
@@ -783,6 +783,36 @@ static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
return EFI_SUCCESS;
}

+/*
+ * When the guest is running at VMPL2 with Alternate Injection enabled,
+ * register Alternate Injection before ExitBootServices().
+ */
+static int svsm_register_alt_inj(void)
+{
+ efi_guid_t alt_inj_proto = OVMF_SEV_ALT_INJ_REGISTER_PROTOCOL_GUID;
+ sev_alt_inj_register_protocol_t *proto;
+ efi_status_t status;
+
+ if (early_is_sevsnp_guest() && snp_vmpl) {
+ if (!(sev_get_status() & MSR_AMD64_SNP_ALTERNATE_INJ))
+ return 0;
+ }
+
+ status = efi_bs_call(locate_protocol, &alt_inj_proto, NULL, (void **)&proto);
+ if (status != EFI_SUCCESS) {
+ efi_err("Alternate Injection registration protocol not exist\n");
+ return 1;
+ }
+
+ status = efi_call_proto(proto, sev_register_alt_inj);
+ if (status != EFI_SUCCESS) {
+ efi_err("Alternate Injection registration protocol failed\n");
+ return 2;
+ }
+
+ return 0;
+}
+
static bool sev_prepare(void)
{
u64 unsupported;
@@ -793,6 +823,10 @@ static bool sev_prepare(void)
unsupported);
return false;
}
+
+ if (svsm_register_alt_inj())
+ return false;
+
return true;
}

diff --git a/include/linux/efi.h b/include/linux/efi.h
index aa15ff88539b..ebf7b4b9a292 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -444,6 +444,8 @@ void efi_native_runtime_setup(void);
#define OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID EFI_GUID(0xc5a010fe, 0x38a7, 0x4531, 0x8a, 0x4a, 0x05, 0x00, 0xd2, 0xfd, 0x16, 0x49)
#define OVMF_MEMORY_LOG_TABLE_GUID EFI_GUID(0x95305139, 0xb20f, 0x4723, 0x84, 0x25, 0x62, 0x7c, 0x88, 0x8f, 0xf1, 0x21)

+#define OVMF_SEV_ALT_INJ_REGISTER_PROTOCOL_GUID EFI_GUID(0x3cf00587, 0x2329, 0x4c44, 0xb1, 0x84, 0x8f, 0xd9, 0x87, 0x00, 0x42, 0xe0)
+
typedef struct {
efi_guid_t guid;
u64 table;
--
2.43.0