Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/apic: Add new driver for Secure AVIC

The Secure AVIC feature provides SEV-SNP guests hardware acceleration for
performance sensitive APIC accesses while securely managing the guest-owned
APIC state through the use of a private APIC backing page.

This helps prevent the hypervisor from generating unexpected interrupts for
a vCPU or otherwise violate architectural assumptions around the APIC
behavior.

Add a new x2APIC driver that will serve as the base of the Secure AVIC
support. It is initially the same as the x2APIC physical driver (without IPI
callbacks), but will be modified as features are implemented.

As the new driver does not implement Secure AVIC features yet, if the
hypervisor sets the Secure AVIC bit in SEV_STATUS, maintain the existing
behavior to enforce the guest termination.

[ bp: Massage commit message. ]

Co-developed-by: Kishon Vijay Abraham I <kvijayab@amd.com>
Signed-off-by: Kishon Vijay Abraham I <kvijayab@amd.com>
Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Tianyu Lan <tiala@microsoft.com>
Link: https://lore.kernel.org/20250828070334.208401-2-Neeraj.Upadhyay@amd.com

authored by

Neeraj Upadhyay and committed by
Borislav Petkov (AMD)
30c2b98a 1b558e14

+93 -1
+13
arch/x86/Kconfig
··· 483 483 484 484 If in doubt, say Y. 485 485 486 + config AMD_SECURE_AVIC 487 + bool "AMD Secure AVIC" 488 + depends on AMD_MEM_ENCRYPT && X86_X2APIC 489 + help 490 + Enable this to get AMD Secure AVIC support on guests that have this feature. 491 + 492 + AMD Secure AVIC provides hardware acceleration for performance sensitive 493 + APIC accesses and support for managing guest owned APIC state for SEV-SNP 494 + guests. Secure AVIC does not support xAPIC mode. It has functional 495 + dependency on x2apic being enabled in the guest. 496 + 497 + If you don't know what to do here, say N. 498 + 486 499 config X86_POSTED_MSI 487 500 bool "Enable MSI and MSI-x delivery by posted interrupts" 488 501 depends on X86_64 && IRQ_REMAP
+1
arch/x86/boot/compressed/sev.c
··· 235 235 MSR_AMD64_SNP_VMSA_REG_PROT | \ 236 236 MSR_AMD64_SNP_RESERVED_BIT13 | \ 237 237 MSR_AMD64_SNP_RESERVED_BIT15 | \ 238 + MSR_AMD64_SNP_SECURE_AVIC | \ 238 239 MSR_AMD64_SNP_RESERVED_MASK) 239 240 240 241 /*
+3
arch/x86/coco/core.c
··· 104 104 case CC_ATTR_HOST_SEV_SNP: 105 105 return cc_flags.host_sev_snp; 106 106 107 + case CC_ATTR_SNP_SECURE_AVIC: 108 + return sev_status & MSR_AMD64_SNP_SECURE_AVIC; 109 + 107 110 default: 108 111 return false; 109 112 }
+1
arch/x86/coco/sev/core.c
··· 79 79 [MSR_AMD64_SNP_IBS_VIRT_BIT] = "IBSVirt", 80 80 [MSR_AMD64_SNP_VMSA_REG_PROT_BIT] = "VMSARegProt", 81 81 [MSR_AMD64_SNP_SMT_PROT_BIT] = "SMTProt", 82 + [MSR_AMD64_SNP_SECURE_AVIC_BIT] = "SecureAVIC", 82 83 }; 83 84 84 85 /*
+3 -1
arch/x86/include/asm/msr-index.h
··· 699 699 #define MSR_AMD64_SNP_VMSA_REG_PROT BIT_ULL(MSR_AMD64_SNP_VMSA_REG_PROT_BIT) 700 700 #define MSR_AMD64_SNP_SMT_PROT_BIT 17 701 701 #define MSR_AMD64_SNP_SMT_PROT BIT_ULL(MSR_AMD64_SNP_SMT_PROT_BIT) 702 - #define MSR_AMD64_SNP_RESV_BIT 18 702 + #define MSR_AMD64_SNP_SECURE_AVIC_BIT 18 703 + #define MSR_AMD64_SNP_SECURE_AVIC BIT_ULL(MSR_AMD64_SNP_SECURE_AVIC_BIT) 704 + #define MSR_AMD64_SNP_RESV_BIT 19 703 705 #define MSR_AMD64_SNP_RESERVED_MASK GENMASK_ULL(63, MSR_AMD64_SNP_RESV_BIT) 704 706 #define MSR_AMD64_RMP_BASE 0xc0010132 705 707 #define MSR_AMD64_RMP_END 0xc0010133
+1
arch/x86/kernel/apic/Makefile
··· 18 18 # APIC probe will depend on the listing order here 19 19 obj-$(CONFIG_X86_NUMACHIP) += apic_numachip.o 20 20 obj-$(CONFIG_X86_UV) += x2apic_uv_x.o 21 + obj-$(CONFIG_AMD_SECURE_AVIC) += x2apic_savic.o 21 22 obj-$(CONFIG_X86_X2APIC) += x2apic_phys.o 22 23 obj-$(CONFIG_X86_X2APIC) += x2apic_cluster.o 23 24 obj-y += apic_flat_64.o
+63
arch/x86/kernel/apic/x2apic_savic.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * AMD Secure AVIC Support (SEV-SNP Guests) 4 + * 5 + * Copyright (C) 2024 Advanced Micro Devices, Inc. 6 + * 7 + * Author: Neeraj Upadhyay <Neeraj.Upadhyay@amd.com> 8 + */ 9 + 10 + #include <linux/cc_platform.h> 11 + 12 + #include <asm/apic.h> 13 + #include <asm/sev.h> 14 + 15 + #include "local.h" 16 + 17 + static int savic_acpi_madt_oem_check(char *oem_id, char *oem_table_id) 18 + { 19 + return x2apic_enabled() && cc_platform_has(CC_ATTR_SNP_SECURE_AVIC); 20 + } 21 + 22 + static int savic_probe(void) 23 + { 24 + if (!cc_platform_has(CC_ATTR_SNP_SECURE_AVIC)) 25 + return 0; 26 + 27 + if (!x2apic_mode) { 28 + pr_err("Secure AVIC enabled in non x2APIC mode\n"); 29 + snp_abort(); 30 + /* unreachable */ 31 + } 32 + 33 + return 1; 34 + } 35 + 36 + static struct apic apic_x2apic_savic __ro_after_init = { 37 + 38 + .name = "secure avic x2apic", 39 + .probe = savic_probe, 40 + .acpi_madt_oem_check = savic_acpi_madt_oem_check, 41 + 42 + .dest_mode_logical = false, 43 + 44 + .disable_esr = 0, 45 + 46 + .cpu_present_to_apicid = default_cpu_present_to_apicid, 47 + 48 + .max_apic_id = UINT_MAX, 49 + .x2apic_set_max_apicid = true, 50 + .get_apic_id = x2apic_get_apic_id, 51 + 52 + .calc_dest_apicid = apic_default_calc_apicid, 53 + 54 + .nmi_to_offline_cpu = true, 55 + 56 + .read = native_apic_msr_read, 57 + .write = native_apic_msr_write, 58 + .eoi = native_apic_msr_eoi, 59 + .icr_read = native_x2apic_icr_read, 60 + .icr_write = native_x2apic_icr_write, 61 + }; 62 + 63 + apic_driver(apic_x2apic_savic);
+8
include/linux/cc_platform.h
··· 96 96 * enabled to run SEV-SNP guests. 97 97 */ 98 98 CC_ATTR_HOST_SEV_SNP, 99 + 100 + /** 101 + * @CC_ATTR_SNP_SECURE_AVIC: Secure AVIC mode is active. 102 + * 103 + * The host kernel is running with the necessary features enabled 104 + * to run SEV-SNP guests with full Secure AVIC capabilities. 105 + */ 106 + CC_ATTR_SNP_SECURE_AVIC, 99 107 }; 100 108 101 109 #ifdef CONFIG_ARCH_HAS_CC_PLATFORM