Skip to contentSkip to conversation

AMD: Add Secure AVIC Guest Support

PATCH Superseded Neeraj Upadhyay 5 participants 30 messages Apr 1, 2025 – Apr 14, 2025

Subsystems (3)

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v3 cover AMD: Add Secure AVIC Guest Support

Introduction
------------

Secure AVIC is a new hardware feature in the AMD64 architecture to allow SEV-SNP guests to prevent the hypervisor from generating unexpected interrupts to a vCPU or otherwise violate architectural assumptions around APIC behavior.

One of the significant differences from AVIC or emulated x2APIC is that Secure AVIC uses a guest-owned and managed APIC backing page. It also introduces additional fields in both the VMCB and the Secure AVIC backing page to aid the guest in limiting which interrupt vectors can be injected into the guest.

Guest APIC Backing Page
-----------------------
Each vCPU has a guest-allocated APIC backing page of size 4K, which maintains APIC state for that vCPU. The x2APIC MSRs are mapped at their corresposing x2APIC MMIO offset within the guest APIC backing page. All x2APIC accesses by guest or Secure AVIC hardware operate on this backing page. The backing page should be pinned and NPT entry for it should be always mapped while the corresponding vCPU is running.

MSR Accesses
------------
Secure AVIC only supports x2APIC MSR accesses. xAPIC MMIO offset based accesses are not supported.

Some of the MSR accesses such as ICR writes (with shorthand equal to self), SELF_IPI, EOI, TPR writes are accelerated by Secure AVIC hardware. Other MSR accesses generate a #VC exception. The #VC exception handler reads/writes to the guest APIC backing page. As guest APIC backing page is accessible to the guest, the Secure AVIC driver code optimizes APIC register access by directly reading/writing to the guest APIC backing page (instead of taking the #VC exception route).

In addition to the architected MSRs, following new fields are added to the guest APIC backing page which can be modified directly by the guest:

a. ALLOWED_IRR

ALLOWED_IRR reg offset indicates the interrupt vectors which the guest allows the hypervisor to send. The combination of host-controlled REQUESTED_IRR vectors (part of VMCB) and ALLOWED_IRR is used by hardware to update the IRR vectors of the Guest APIC backing page.

#Offset        #bits        Description
204h           31:0         Guest allowed vectors 0-31
214h           31:0         Guest allowed vectors 32-63

...
274h 31:0 Guest allowed vectors 224-255

ALLOWED_IRR is meant to be used specifically for vectors that the hypervisor is allowed to inject, such as device interrupts. Interrupt vectors used exclusively by the guest itself (like IPI vectors) should not be allowed to be injected into the guest for security reasons.

b. NMI Request

#Offset        #bits        Description
278h           0            Set by Guest to request Virtual NMI

Guest need to set NMI Request register to allow the Hypervisor to inject vNMI to it.

LAPIC Timer Support
-------------------
LAPIC timer is emulated by the hypervisor. So, APIC_LVTT, APIC_TMICT and APIC_TDCR, APIC_TMCCT APIC registers are not read/written to the guest APIC backing page and are communicated to the hypervisor using SVM_EXIT_MSR VMGEXIT.

IPI Support
-----------
Only SELF_IPI is accelerated by Secure AVIC hardware. Other IPIs require writing (from the Secure AVIC driver) to the IRR vector of the target CPU backing page and then issuing VMGEXIT for the hypervisor to notify the target vCPU.

KEXEC Support
-------------
Secure AVIC enabled guest can kexec to another kernel which has Secure AVIC enabled, as the Hypervisor has Secure AVIC feature bit set in the sev_status.

Open Points
-----------

The Secure AVIC driver only supports physical destination mode. If logical destination mode need to be supported, then a separate x2apic driver would be required for supporting logical destination mode.

Testing
-------

This series is based on top of commit 535bd326c565 "Merge branch into tip/master: 'x86/tdx'" of tip/tip master branch.

Host Secure AVIC support patch series is at [1].

Qemu support patch is at [2].

QEMU commandline for testing Secure AVIC enabled guest:

qemu-system-x86_64 <...> -object sev-snp-guest,id=sev0,policy=0xb0000,cbitpos=51,reduced-phys-bits=1,allowed-sev-features=true,secure-avic=true

Following tests are done:

1) Boot to Prompt using initramfs and ubuntu fs.
2) Verified timer and IPI as part of the guest bootup.
3) Verified long run SCF TORTURE IPI test.

[1] https://github.com/AMDESE/linux-kvm/tree/savic-host-latest
[2] https://github.com/AMDESE/qemu/tree/secure-avic

Change since v2

- Removed RFC tag.
- Change config rule to not select AMD_SECURE_AVIC config if AMD_MEM_ENCRYPT config is enabled.
- Fix broken backing page GFP_KERNEL allocation in setup_local_APIC(). Use alloc_percpu() for APIC backing pages allocation during Secure AVIC driver probe.
- Remove code to check for duplicate APIC_ID returned by the Hypervisor. Topology evaluation code already does that during boot.
- Fix missing update_vector() callback invocation during vector cleanup paths. Invoke update_vector() during setup and tearing down of a vector.
- Reuse find_highest_vector() from kvm/lapic.c.
- Change savic_register_gpa/savic_unregister_gpa() interface to be invoked only for the local CPU.
- Misc cleanups.

Kishon Vijay Abraham I (2):
x86/sev: Initialize VGIF for secondary VCPUs for Secure AVIC x86/sev: Enable NMI support for Secure AVIC

Neeraj Upadhyay (15):
x86/apic: Add new driver for Secure AVIC
x86/apic: Initialize Secure AVIC APIC backing page x86/apic: Populate .read()/.write() callbacks of Secure AVIC driver x86/apic: Initialize APIC ID for Secure AVIC
x86/apic: Add update_vector callback for Secure AVIC x86/apic: Add support to send IPI for Secure AVIC x86/apic: Support LAPIC timer for Secure AVIC
x86/apic: Add support to send NMI IPI for Secure AVIC x86/apic: Allow NMI to be injected from hypervisor for Secure AVIC x86/apic: Read and write LVT* APIC registers from HV for SAVIC guests x86/apic: Handle EOI writes for SAVIC guests
x86/apic: Add kexec support for Secure AVIC
x86/apic: Enable Secure AVIC in Control MSR
x86/sev: Prevent SECURE_AVIC_CONTROL MSR interception for Secure AVIC
guests
x86/sev: Indicate SEV-SNP guest supports Secure AVIC

 arch/x86/Kconfig                    |  13 +
 arch/x86/boot/compressed/sev.c      |  10 +-
 arch/x86/coco/core.c                |   3 +
 arch/x86/coco/sev/core.c            | 131 +++++++-
 arch/x86/include/asm/apic-emul.h    |  28 ++
 arch/x86/include/asm/apic.h         |  12 +
 arch/x86/include/asm/apicdef.h      |   2 +
 arch/x86/include/asm/msr-index.h    |   9 +-
 arch/x86/include/asm/sev.h          |   8 +
 arch/x86/include/uapi/asm/svm.h     |   3 +
 arch/x86/kernel/apic/Makefile       |   1 +
 arch/x86/kernel/apic/apic.c         |   7 +
 arch/x86/kernel/apic/init.c         |   3 +
 arch/x86/kernel/apic/vector.c       |  53 +++-
 arch/x86/kernel/apic/x2apic_savic.c | 467 ++++++++++++++++++++++++++++
 arch/x86/kvm/lapic.c                |  23 +-
 include/linux/cc_platform.h         |   8 +
 17 files changed, 742 insertions(+), 39 deletions(-)
 create mode 100644 arch/x86/include/asm/apic-emul.h
 create mode 100644 arch/x86/kernel/apic/x2apic_savic.c

base-commit: 535bd326c5657fe570f41b1f76941e449d9e2062

Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/17 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 hypervisor from generating unexpected interrupts for a vCPU or otherwise violate architectural assumptions around 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 phys driver, but will be modified as features of Secure AVIC are implemented.

If the hypervisor sets the Secure AVIC bit in SEV_STATUS and the bit is not set in SNP_FEATURES_PRESENT, maintain the current behavior to enforce the guest termination.

---
Changes since v2:

- Do not autoselect AMD_SECURE_AVIC config when AMD_MEM_ENCRYPT config is enabled. Make AMD_SECURE_AVIC depend on AMD_MEM_ENCRYPT.
- Misc cleanups.

 arch/x86/Kconfig                    |  13 ++++
 arch/x86/boot/compressed/sev.c      |   1 +
 arch/x86/coco/core.c                |   3 +
 arch/x86/include/asm/msr-index.h    |   4 +-
 arch/x86/kernel/apic/Makefile       |   1 +
 arch/x86/kernel/apic/x2apic_savic.c | 109 ++++++++++++++++++++++++++++
 include/linux/cc_platform.h         |   8 ++
 7 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/kernel/apic/x2apic_savic.c
arch/x86/Kconfig +13 -0
index 6db2e925eb35..3695a6cd0d4e 100644
@@ -483,6 +483,19 @@ config X86_X2APIC
 
 	  If in doubt, say Y.
 
+config AMD_SECURE_AVIC
+	bool "AMD Secure AVIC"
+	depends on AMD_MEM_ENCRYPT && X86_X2APIC
+	help
+	  Enable this to get AMD Secure AVIC support on guests that have this feature.
+
+	  AMD Secure AVIC provides hardware acceleration for performance sensitive
+	  APIC accesses and support for managing guest owned APIC state for SEV-SNP
+	  guests. Secure AVIC does not support xapic mode. It has functional
+	  dependency on x2apic being enabled in the guest.
+
+	  If you don't know what to do here, say N.
+
 config X86_POSTED_MSI
 	bool "Enable MSI and MSI-x delivery by posted interrupts"
 	depends on X86_64 && IRQ_REMAP
arch/x86/boot/compressed/sev.c +1 -0
index bb55934c1cee..798fdd3dbd1e 100644
@@ -394,6 +394,7 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
 				 MSR_AMD64_SNP_VMSA_REG_PROT |		\
 				 MSR_AMD64_SNP_RESERVED_BIT13 |		\
 				 MSR_AMD64_SNP_RESERVED_BIT15 |		\
+				 MSR_AMD64_SNP_SECURE_AVIC |		\
 				 MSR_AMD64_SNP_RESERVED_MASK)
 
 /*
arch/x86/coco/core.c +3 -0
index 9a0ddda3aa69..3d7bf37e2155 100644
@@ -102,6 +102,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)
 	case CC_ATTR_HOST_SEV_SNP:
 		return cc_flags.host_sev_snp;
 
+	case CC_ATTR_SNP_SECURE_AVIC:
+		return sev_status & MSR_AMD64_SNP_SECURE_AVIC;
+
 	default:
 		return false;
 	}
arch/x86/include/asm/msr-index.h +3 -1
index e6134ef2263d..0090b6f1d6f9 100644
@@ -685,7 +685,9 @@
 #define MSR_AMD64_SNP_VMSA_REG_PROT	BIT_ULL(MSR_AMD64_SNP_VMSA_REG_PROT_BIT)
 #define MSR_AMD64_SNP_SMT_PROT_BIT	17
 #define MSR_AMD64_SNP_SMT_PROT		BIT_ULL(MSR_AMD64_SNP_SMT_PROT_BIT)
-#define MSR_AMD64_SNP_RESV_BIT		18
+#define MSR_AMD64_SNP_SECURE_AVIC_BIT	18
+#define MSR_AMD64_SNP_SECURE_AVIC	BIT_ULL(MSR_AMD64_SNP_SECURE_AVIC_BIT)
+#define MSR_AMD64_SNP_RESV_BIT		19
 #define MSR_AMD64_SNP_RESERVED_MASK	GENMASK_ULL(63, MSR_AMD64_SNP_RESV_BIT)
 #define MSR_AMD64_RMP_BASE		0xc0010132
 #define MSR_AMD64_RMP_END		0xc0010133
arch/x86/kernel/apic/Makefile +1 -0
index 52d1808ee360..581db89477f9 100644
@@ -18,6 +18,7 @@ ifeq ($(CONFIG_X86_64),y)
 # APIC probe will depend on the listing order here
 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_X86_X2APIC)	+= x2apic_phys.o
 obj-$(CONFIG_X86_X2APIC)	+= x2apic_cluster.o
 obj-y				+= apic_flat_64.o
arch/x86/kernel/apic/x2apic_savic.c new file +109 -0

109 lines not on this page · raw message

include/linux/cc_platform.h +8 -0
index 0bf7d33a1048..7fcec025c5e0 100644
@@ -96,6 +96,14 @@ enum cc_attr {
 	 * enabled to run SEV-SNP guests.
 	 */
 	CC_ATTR_HOST_SEV_SNP,
+
+	/**
+	 * @CC_ATTR_SNP_SECURE_AVIC: Secure AVIC mode is active.
+	 *
+	 * The host kernel is running with the necessary features enabled
+	 * to run SEV-SNP guests with full Secure AVIC capabilities.
+	 */
+	CC_ATTR_SNP_SECURE_AVIC,
 };
 
 #ifdef CONFIG_ARCH_HAS_CC_PLATFORM
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 01/17] x86/apic: Add new driver for Secure AVIC

arch/x86/kernel/apic/x2apic_savic.c line 36

Quoted 5 lines · Neeraj Upadhyay
arch/x86/kernel/apic/x2apic_savic.c new file +5 -0
+static void __send_ipi_mask(const struct cpumask *mask, int vector, bool excl_self)
+{
+	unsigned long query_cpu;
+	unsigned long this_cpu;
+	unsigned long flags;

Just coalesce them into a single line: 'unsigned long a, b;'

arch/x86/kernel/apic/x2apic_savic.c line 41

Quoted 4 lines
arch/x86/kernel/apic/x2apic_savic.c new file +4 -0
+	/* x2apic MSRs are special and need a special fence: */
+	weak_wrmsr_fence();
+
+	local_irq_save(flags);
        guard(irqsave)();
Quoted 8 lines
arch/x86/kernel/apic/x2apic_savic.c new file +8 -0
+	this_cpu = smp_processor_id();
+	for_each_cpu(query_cpu, mask) {
+		if (excl_self && this_cpu == query_cpu)
+			continue;
+		__x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu),
+				       vector, APIC_DEST_PHYSICAL);
+	}
+	local_irq_restore(flags);

arch/x86/kernel/apic/x2apic_savic.c line 70

Quoted 10 lines
arch/x86/kernel/apic/x2apic_savic.c new file +9 -0
+}
 
+static int x2apic_savic_probe(void)
+{
+	if (!cc_platform_has(CC_ATTR_SNP_SECURE_AVIC))
+		return 0;
+
+	if (!x2apic_mode) {
+		pr_err("Secure AVIC enabled in non x2APIC mode\n");
+		snp_abort();

Why does this return 1?

Quoted 4 lines
arch/x86/kernel/apic/x2apic_savic.c new file +4 -0
+	}
+
+	return 1;
+}
#3 lore raw

Re: [PATCH v3 01/17] x86/apic: Add new driver for Secure AVIC

Quoted 9 lines · Thomas Gleixner

arch/x86/kernel/apic/x2apic_savic.c line 36

Quoted 5 lines · Neeraj Upadhyay
arch/x86/kernel/apic/x2apic_savic.c new file +5 -0
+static void __send_ipi_mask(const struct cpumask *mask, int vector, bool excl_self)
+{
+	unsigned long query_cpu;
+	unsigned long this_cpu;
+	unsigned long flags;

Just coalesce them into a single line: 'unsigned long a, b;'

Ok

Quoted 7 lines

arch/x86/kernel/apic/x2apic_savic.c line 41

Quoted 4 lines
arch/x86/kernel/apic/x2apic_savic.c new file +4 -0
+	/* x2apic MSRs are special and need a special fence: */
+	weak_wrmsr_fence();
+
+	local_irq_save(flags);
        guard(irqsave)();

Ok

Quoted 10 lines
Quoted 9 lines
arch/x86/kernel/apic/x2apic_savic.c new file +9 -0
+	this_cpu = smp_processor_id();
+	for_each_cpu(query_cpu, mask) {
+		if (excl_self && this_cpu == query_cpu)
+			continue;
+		__x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu),
+				       vector, APIC_DEST_PHYSICAL);
+	}
+	local_irq_restore(flags);
+}
Quoted 11 lines

arch/x86/kernel/apic/x2apic_savic.c line 70

Quoted 8 lines
arch/x86/kernel/apic/x2apic_savic.c new file +8 -0
+static int x2apic_savic_probe(void)
+{
+	if (!cc_platform_has(CC_ATTR_SNP_SECURE_AVIC))
+		return 0;
+
+	if (!x2apic_mode) {
+		pr_err("Secure AVIC enabled in non x2APIC mode\n");
+		snp_abort();

Why does this return 1?

snp_abort() would terminate guest execution. So, code below it is not reachable if we reach here. I will add a comment here.

- Neeraj

Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/apic/x2apic_savic.c new file +4 -0
+	}
+
+	return 1;
+}
#4 lore raw

PATCH v3 2/17 x86/apic: Initialize Secure AVIC APIC backing page

In reply to Neeraj Upadhyay

With Secure AVIC, the APIC backing page is owned and managed by guest. Allocate and initialize APIC backing page for all guest CPUs.

The NPT entry for a vCPU's APIC backing page must always be present when the vCPU is running in order for Secure AVIC to function. A VMEXIT_BUSY is returned on VMRUN and the vCPU cannot be resumed if the NPT entry for the APIC backing page is not present. Notify GPA of the vCPU's APIC backing page to the hypervisor by using the SVM_VMGEXIT_SECURE_AVIC GHCB protocol event. Before executing VMRUN, the hypervisor makes use of this information to make sure the APIC backing page is mapped in NPT.

---
Changes since v2:

- Fix broken AP bringup due to GFP_KERNEL allocation in setup() callback.
- Define apic_page struct and allocate per CPU API backing pages for all CPUs in Secure AVIC driver probe.
- Change savic_register_gpa() to only allow local CPU GPA registration.
- Misc cleanups.

 arch/x86/coco/sev/core.c            | 27 +++++++++++++++++++
 arch/x86/coco/sev/core.c            | 27 +++++++++++++++++++
 arch/x86/include/asm/apic.h         |  1 +
 arch/x86/include/asm/sev.h          |  2 ++
 arch/x86/include/uapi/asm/svm.h     |  3 +++
 arch/x86/kernel/apic/apic.c         |  2 ++
 arch/x86/kernel/apic/x2apic_savic.c | 42 +++++++++++++++++++++++++++++
 6 files changed, 77 insertions(+)
arch/x86/coco/sev/core.c +27 -0
index b0c1a7a57497..036833ac17e1 100644
@@ -1501,6 +1501,33 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	return ret;
 }
 
+enum es_result savic_register_gpa(u64 gpa)
+{
+	struct ghcb_state state;
+	struct es_em_ctxt ctxt;
+	unsigned long flags;
+	enum es_result res;
+	struct ghcb *ghcb;
+
+	local_irq_save(flags);
+
+	ghcb = __sev_get_ghcb(&state);
+
+	vc_ghcb_invalidate(ghcb);
+
+	/* Register GPA for the local CPU */
+	ghcb_set_rax(ghcb, -1ULL);
+	ghcb_set_rbx(ghcb, gpa);
+	res = sev_es_ghcb_hv_call(ghcb, &ctxt, SVM_VMGEXIT_SECURE_AVIC,
+			SVM_VMGEXIT_SECURE_AVIC_REGISTER_GPA, 0);
+
+	__sev_put_ghcb(&state);
+
+	local_irq_restore(flags);
+
+	return res;
+}
+
 static void snp_register_per_cpu_ghcb(void)
 {
 	struct sev_es_runtime_data *data;
arch/x86/include/asm/apic.h +1 -0
index c903d358405d..e17c8cb810a2 100644
@@ -305,6 +305,7 @@ struct apic {
 
 	/* Probe, setup and smpboot functions */
 	int	(*probe)(void);
+	void	(*setup)(void);
 	int	(*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
 
 	void	(*init_apic_ldr)(void);
arch/x86/include/asm/sev.h +2 -0
index ba7999f66abe..3448032bae8c 100644
@@ -483,6 +483,7 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req
 
 void __init snp_secure_tsc_prepare(void);
 void __init snp_secure_tsc_init(void);
+enum es_result savic_register_gpa(u64 gpa);
 
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
@@ -526,6 +527,7 @@ static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_
 					 struct snp_guest_request_ioctl *rio) { return -ENODEV; }
 static inline void __init snp_secure_tsc_prepare(void) { }
 static inline void __init snp_secure_tsc_init(void) { }
+static inline enum es_result savic_register_gpa(u64 gpa) { return ES_UNSUPPORTED; }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/include/uapi/asm/svm.h +3 -0
index ec1321248dac..36fc87bdb859 100644
@@ -117,6 +117,9 @@
 #define SVM_VMGEXIT_AP_CREATE			1
 #define SVM_VMGEXIT_AP_DESTROY			2
 #define SVM_VMGEXIT_SNP_RUN_VMPL		0x80000018
+#define SVM_VMGEXIT_SECURE_AVIC			0x8000001a
+#define SVM_VMGEXIT_SECURE_AVIC_REGISTER_GPA	0
+#define SVM_VMGEXIT_SECURE_AVIC_UNREGISTER_GPA	1
 #define SVM_VMGEXIT_HV_FEATURES			0x8000fffd
 #define SVM_VMGEXIT_TERM_REQUEST		0x8000fffe
 #define SVM_VMGEXIT_TERM_REASON(reason_set, reason_code)	\
arch/x86/kernel/apic/apic.c +2 -0
index 62584a347931..f59ed284ec5b 100644
@@ -1502,6 +1502,8 @@ static void setup_local_APIC(void)
 		return;
 	}
 
+	if (apic->setup)
+		apic->setup();
 	/*
 	 * If this comes from kexec/kcrash the APIC might be enabled in
 	 * SPIV. Soft disable it before doing further initialization.
arch/x86/kernel/apic/x2apic_savic.c +42 -0

72 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v3 02/17] x86/apic: Initialize Secure AVIC APIC backing page

arch/x86/coco/sev/core.c static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) · line 1512

Quoted 9 lines · Neeraj Upadhyay
arch/x86/coco/sev/core.c +9 -0
+enum es_result savic_register_gpa(u64 gpa)
+{
+	struct ghcb_state state;
+	struct es_em_ctxt ctxt;
+	unsigned long flags;
+	enum es_result res;
+	struct ghcb *ghcb;
+
+	local_irq_save(flags);
        guard(irqsave)();

arch/x86/coco/sev/core.c static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) · line 1522

Quoted 9 lines
arch/x86/coco/sev/core.c +9 -0
+	ghcb = __sev_get_ghcb(&state);
+
+	vc_ghcb_invalidate(ghcb);
+
+	/* Register GPA for the local CPU */
+	ghcb_set_rax(ghcb, -1ULL);
+	ghcb_set_rbx(ghcb, gpa);
+	res = sev_es_ghcb_hv_call(ghcb, &ctxt, SVM_VMGEXIT_SECURE_AVIC,
+			SVM_VMGEXIT_SECURE_AVIC_REGISTER_GPA, 0);

https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#line-breaks

#6 lore raw

Re: [PATCH v3 02/17] x86/apic: Initialize Secure AVIC APIC backing page

Quoted 13 lines · Thomas Gleixner

arch/x86/coco/sev/core.c static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) · line 1512

Quoted 9 lines · Neeraj Upadhyay
arch/x86/coco/sev/core.c +9 -0
+enum es_result savic_register_gpa(u64 gpa)
+{
+	struct ghcb_state state;
+	struct es_em_ctxt ctxt;
+	unsigned long flags;
+	enum es_result res;
+	struct ghcb *ghcb;
+
+	local_irq_save(flags);
        guard(irqsave)();

Ok

Quoted 12 lines

arch/x86/coco/sev/core.c static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) · line 1522

Quoted 9 lines
arch/x86/coco/sev/core.c +9 -0
+	ghcb = __sev_get_ghcb(&state);
+
+	vc_ghcb_invalidate(ghcb);
+
+	/* Register GPA for the local CPU */
+	ghcb_set_rax(ghcb, -1ULL);
+	ghcb_set_rbx(ghcb, gpa);
+	res = sev_es_ghcb_hv_call(ghcb, &ctxt, SVM_VMGEXIT_SECURE_AVIC,
+			SVM_VMGEXIT_SECURE_AVIC_REGISTER_GPA, 0);

https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#line-breaks

Ok, will align with "ghcb" arg on previous line.

- Neeraj

#7 lore raw

PATCH v3 3/17 x86/apic: Populate .read()/.write() callbacks of Secure AVIC driver

In reply to Neeraj Upadhyay

Add read() and write() APIC callback functions to read and write x2APIC registers directly from the guest APIC backing page of a vCPU.

The x2APIC registers are mapped at an offset within the guest APIC backing page which is same as their x2APIC MMIO offset. Secure AVIC adds new registers such as ALLOWED_IRRs (which are at 4-byte offset within the IRR register offset range) and NMI_REQ to the APIC register space.

When Secure AVIC is enabled, guest's rdmsr/wrmsr of APIC registers result in VC exception (for non-accelerated register accesses) with error code VMEXIT_AVIC_NOACCEL. The VC exception handler can read/write the x2APIC register in the guest APIC backing page to complete the rdmsr/wrmsr. Since doing this would increase the latency of accessing x2APIC registers, instead of doing rdmsr/wrmsr based reg accesses and handling reads/writes in VC exception, directly read/write APIC registers from/to the guest APIC backing page of the vCPU in read() and write() callbacks of the Secure AVIC APIC driver.

---
Changes since v2:
- Use this_cpu_ptr() instead of type casting in get_reg() and set_reg().

 arch/x86/include/asm/apicdef.h      |   2 +
 arch/x86/kernel/apic/x2apic_savic.c | 116 +++++++++++++++++++++++++++-
 2 files changed, 116 insertions(+), 2 deletions(-)
arch/x86/include/asm/apicdef.h +2 -0
index 094106b6a538..be39a543fbe5 100644
@@ -135,6 +135,8 @@
 #define		APIC_TDR_DIV_128	0xA
 #define	APIC_EFEAT	0x400
 #define	APIC_ECTRL	0x410
+#define APIC_SEOI	0x420
+#define APIC_IER	0x480
 #define APIC_EILVTn(n)	(0x500 + 0x10 * n)
 #define		APIC_EILVT_NR_AMD_K8	1	/* # of extended interrupts */
 #define		APIC_EILVT_NR_AMD_10H	4
arch/x86/kernel/apic/x2apic_savic.c +114 -2

134 lines not on this page · raw message

Signature · 1 line
2.34.1
#8 lore raw

PATCH v3 4/17 x86/apic: Initialize APIC ID for Secure AVIC

In reply to Neeraj Upadhyay

Initialize the APIC ID in the Secure AVIC APIC backing page with the APIC_ID msr value read from Hypervisor. CPU topology evaluation later during boot would catch and report any duplicate APIC ID for two CPUs.

---
Changes since v2:
- Drop duplicate APIC ID checks.

 arch/x86/kernel/apic/x2apic_savic.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/x86/kernel/apic/x2apic_savic.c +13 -0
index f1dd74724769..21f7c055995e 100644
@@ -185,12 +185,25 @@ static void x2apic_savic_send_ipi_mask_allbutself(const struct cpumask *mask, in
 	__send_ipi_mask(mask, vector, true);
 }
 
+static void init_apic_page(void)
+{
+	u32 apic_id;
+
+	/*
+	 * Before Secure AVIC is enabled, APIC msr reads are intercepted.
+	 * APIC_ID msr read returns the value from the Hypervisor.
+	 */
+	apic_id = native_apic_msr_read(APIC_ID);
+	set_reg(APIC_ID, apic_id);
+}
+
 static void x2apic_savic_setup(void)
 {
 	void *backing_page;
 	enum es_result ret;
 	unsigned long gpa;
 
+	init_apic_page();
 	backing_page = this_cpu_ptr(apic_page);
 	gpa = __pa(backing_page);
 
Signature · 1 line
2.34.1
#9 lore raw

PATCH v3 5/17 x86/apic: Add update_vector callback for Secure AVIC

In reply to Neeraj Upadhyay

Add update_vector callback to set/clear ALLOWED_IRR field in a vCPU's APIC backing page for external vectors. The ALLOWED_IRR field indicates the interrupt vectors which the guest allows the hypervisor to send (typically for emulated devices). Interrupt vectors used exclusively by the guest itself and the vectors which are not emulated by the hypervisor, such as IPI vectors, are part of system vectors and are not set in the ALLOWED_IRR.

---
Changes since v2:

- Associate update_vector() invocation with vector allocation/free calls.
- Cleanup and simplify vector bitmap calculation for ALLOWED_IRR.

 arch/x86/include/asm/apic.h         |  2 +
 arch/x86/include/asm/apic.h         |  2 +
 arch/x86/kernel/apic/vector.c       | 59 +++++++++++++++++++++++------
 arch/x86/kernel/apic/x2apic_savic.c | 20 ++++++++++
 3 files changed, 69 insertions(+), 12 deletions(-)
arch/x86/include/asm/apic.h +2 -0
index e17c8cb810a2..b510008c586f 100644
@@ -318,6 +318,8 @@ struct apic {
 	/* wakeup secondary CPU using 64-bit wakeup point */
 	int	(*wakeup_secondary_cpu_64)(u32 apicid, unsigned long start_eip);
 
+	void	(*update_vector)(unsigned int cpu, unsigned int vector, bool set);
+
 	char	*name;
 };
 
arch/x86/kernel/apic/vector.c +47 -12

113 lines not on this page · raw message

arch/x86/kernel/apic/x2apic_savic.c +20 -0
index 21f7c055995e..0bb649e3527d 100644
@@ -185,6 +185,24 @@ static void x2apic_savic_send_ipi_mask_allbutself(const struct cpumask *mask, in
 	__send_ipi_mask(mask, vector, true);
 }
 
+static void x2apic_savic_update_vector(unsigned int cpu, unsigned int vector, bool set)
+{
+	struct apic_page *ap = per_cpu_ptr(apic_page, cpu);
+	unsigned long *sirr = (unsigned long *) &ap->bytes[SAVIC_ALLOWED_IRR];
+	unsigned int bit;
+
+	/*
+	 * The registers are 32-bit wide and 16-byte aligned.
+	 * Compensate for the resulting bit number spacing.
+	 */
+	bit = vector + 96 * (vector / 32);
+
+	if (set)
+		set_bit(bit, sirr);
+	else
+		clear_bit(bit, sirr);
+}
+
 static void init_apic_page(void)
 {
 	u32 apic_id;
@@ -271,6 +289,8 @@ static struct apic apic_x2apic_savic __ro_after_init = {
 	.eoi				= native_apic_msr_eoi,
 	.icr_read			= native_x2apic_icr_read,
 	.icr_write			= native_x2apic_icr_write,
+
+	.update_vector			= x2apic_savic_update_vector,
 };
 
 apic_driver(apic_x2apic_savic);
Signature · 1 line
2.34.1
#10 lore raw

PATCH v3 6/17 x86/apic: Add support to send IPI for Secure AVIC

In reply to Neeraj Upadhyay

With Secure AVIC only Self-IPI is accelerated. To handle all the other IPIs, add new callbacks for sending IPI, which write to the IRR of the target guest vCPU's APIC backing page and then issue GHCB protocol MSR write event for the hypervisor to notify the target vCPU.

---
Changes since v2:
- Simplify vector updates in bitmap.
- Cleanup icr_data parcelling and unparcelling.
- Misc cleanups.
- Fix warning reported by kernel test robot.

 arch/x86/coco/sev/core.c            |  40 ++++++-
 arch/x86/include/asm/sev.h          |   2 +
 arch/x86/kernel/apic/x2apic_savic.c | 164 ++++++++++++++++++++++------
 3 files changed, 167 insertions(+), 39 deletions(-)
arch/x86/coco/sev/core.c +35 -5
index 036833ac17e1..e53147a630c3 100644
@@ -1464,14 +1464,10 @@ static enum es_result __vc_handle_secure_tsc_msrs(struct pt_regs *regs, bool wri
 	return ES_OK;
 }
 
-static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
+static enum es_result __vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt, bool write)
 {
 	struct pt_regs *regs = ctxt->regs;
 	enum es_result ret;
-	bool write;
-
-	/* Is it a WRMSR? */
-	write = ctxt->insn.opcode.bytes[1] == 0x30;
 
 	switch (regs->cx) {
 	case MSR_SVSM_CAA:
@@ -1501,6 +1497,40 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	return ret;
 }
 
+static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
+{
+	return __vc_handle_msr(ghcb, ctxt, ctxt->insn.opcode.bytes[1] == 0x30);
+}
+
+void savic_ghcb_msr_write(u32 reg, u64 value)
+{
+	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 = &regs };
+	struct ghcb_state state;
+	unsigned long flags;
+	enum es_result ret;
+	struct ghcb *ghcb;
+
+	local_irq_save(flags);
+	ghcb = __sev_get_ghcb(&state);
+	vc_ghcb_invalidate(ghcb);
+
+	ret = __vc_handle_msr(ghcb, &ctxt, true);
+	if (ret != ES_OK) {
+		pr_err("Secure AVIC msr (0x%llx) write returned error (%d)\n", msr, ret);
+		/* MSR writes should never fail. Any failure is fatal error for SNP guest */
+		snp_abort();
+	}
+
+	__sev_put_ghcb(&state);
+	local_irq_restore(flags);
+}
+
 enum es_result savic_register_gpa(u64 gpa)
 {
 	struct ghcb_state state;
arch/x86/include/asm/sev.h +2 -0
index 3448032bae8c..855c705ee074 100644
@@ -484,6 +484,7 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req
 void __init snp_secure_tsc_prepare(void);
 void __init snp_secure_tsc_init(void);
 enum es_result savic_register_gpa(u64 gpa);
+void savic_ghcb_msr_write(u32 reg, u64 value);
 
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
@@ -528,6 +529,7 @@ static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_
 static inline void __init snp_secure_tsc_prepare(void) { }
 static inline void __init snp_secure_tsc_init(void) { }
 static inline enum es_result savic_register_gpa(u64 gpa) { return ES_UNSUPPORTED; }
+static inline void savic_ghcb_msr_write(u32 reg, u64 value) { }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/kernel/apic/x2apic_savic.c +130 -34

233 lines not on this page · raw message

Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v3 06/17] x86/apic: Add support to send IPI for Secure AVIC

arch/x86/kernel/apic/x2apic_savic.c static __always_inline void set_reg(unsigned int offset, u32 val) · line 50

Quoted 8 lines · Neeraj Upadhyay
arch/x86/kernel/apic/x2apic_savic.c +2 -0
@@ -46,6 +46,25 @@ static __always_inline void set_reg(unsigned int offset, u32 val)
 
 #define SAVIC_ALLOWED_IRR	0x204
 
+static inline void update_vector(unsigned int cpu, unsigned int offset,
+				 unsigned int vector, bool set)

Why aren't you placing that function right away there instead of adding it first somewhere else and then shuffle it around?

arch/x86/kernel/apic/x2apic_savic.c static void x2apic_savic_write(u32 reg, u32 data) · line 256

Quoted 7 lines
arch/x86/kernel/apic/x2apic_savic.c +3 -3
-static void __send_ipi_mask(const struct cpumask *mask, int vector, bool excl_self)
+static void send_ipi_mask(const struct cpumask *mask, unsigned int vector, bool excl_self)
 {
-	unsigned long query_cpu;
-	unsigned long this_cpu;
+	unsigned int this_cpu;
+	unsigned int cpu;

Again. Do it right in the first place and not later. Same for the underscores of the function name.

#12 lore raw

Re: [PATCH v3 06/17] x86/apic: Add support to send IPI for Secure AVIC

Quoted 5 lines · Thomas Gleixner
Quoted 4 lines · Neeraj Upadhyay
arch/x86/kernel/apic/x2apic_savic.c +0 -0
@@ -46,6 +46,25 @@ static __always_inline void set_reg(unsigned int offset, u32 val)
 
Quoted 8 lines

arch/x86/kernel/apic/x2apic_savic.c static __always_inline void set_reg(unsigned int offset, u32 val) · line 50

Quoted 4 lines
arch/x86/kernel/apic/x2apic_savic.c +2 -0
 #define SAVIC_ALLOWED_IRR	0x204
 
+static inline void update_vector(unsigned int cpu, unsigned int offset,
+				 unsigned int vector, bool set)

Why aren't you placing that function right away there instead of adding it first somewhere else and then shuffle it around?

Ok. Intent was to move this logic to a function only when second usage came during the series. I will move it to "05/17 x86/apic: Add update_vector callback for Secure AVIC"

Quoted 11 lines

arch/x86/kernel/apic/x2apic_savic.c static void x2apic_savic_write(u32 reg, u32 data) · line 256

Quoted 7 lines
arch/x86/kernel/apic/x2apic_savic.c +3 -3
-static void __send_ipi_mask(const struct cpumask *mask, int vector, bool excl_self)
+static void send_ipi_mask(const struct cpumask *mask, unsigned int vector, bool excl_self)
 {
-	unsigned long query_cpu;
-	unsigned long this_cpu;
+	unsigned int this_cpu;
+	unsigned int cpu;

Again. Do it right in the first place and not later. Same for the underscores of the function name.

Ok. Another approach I was thinking to try next was, to remove IPI callbacks definition from "01/17 x86/apic: Add new driver for Secure AVIC" (where I mostly copy logic from x2apic_phys.c) and add define then in this patch. Then the diff will be clearer.

- Neeraj

#13 lore raw

PATCH v3 7/17 x86/apic: Support LAPIC timer for Secure AVIC

In reply to Neeraj Upadhyay

Secure AVIC requires LAPIC timer to be emulated by the hypervisor. KVM already supports emulating LAPIC timer using hrtimers. In order to emulate LAPIC timer, APIC_LVTT, APIC_TMICT and APIC_TDCR register values need to be propagated to the hypervisor for arming the timer. APIC_TMCCT register value has to be read from the hypervisor, which is required for calibrating the APIC timer. So, read/write all APIC timer registers from/to the hypervisor.

In addition, add a static call for apic's update_vector() callback, to configure ALLOWED_IRR for the hypervisor to inject timer interrupt using LOCAL_TIMER_VECTOR.

---
Changes since v2:

- Add static call for apic_update_vector()

 arch/x86/coco/sev/core.c            | 27 +++++++++++++++++++++++++++
 arch/x86/include/asm/apic.h         |  8 ++++++++
 arch/x86/include/asm/sev.h          |  2 ++
 arch/x86/kernel/apic/apic.c         |  2 ++
 arch/x86/kernel/apic/init.c         |  3 +++
 arch/x86/kernel/apic/vector.c       |  6 ------
 arch/x86/kernel/apic/x2apic_savic.c |  7 +++++--
 7 files changed, 47 insertions(+), 8 deletions(-)
arch/x86/coco/sev/core.c +27 -0
index e53147a630c3..1122cf93983d 100644
@@ -1502,6 +1502,33 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	return __vc_handle_msr(ghcb, ctxt, ctxt->insn.opcode.bytes[1] == 0x30);
 }
 
+u64 savic_ghcb_msr_read(u32 reg)
+{
+	u64 msr = APIC_BASE_MSR + (reg >> 4);
+	struct pt_regs regs = { .cx = msr };
+	struct es_em_ctxt ctxt = { .regs = &regs };
+	struct ghcb_state state;
+	unsigned long flags;
+	enum es_result ret;
+	struct ghcb *ghcb;
+
+	local_irq_save(flags);
+	ghcb = __sev_get_ghcb(&state);
+	vc_ghcb_invalidate(ghcb);
+
+	ret = __vc_handle_msr(ghcb, &ctxt, false);
+	if (ret != ES_OK) {
+		pr_err("Secure AVIC msr (0x%llx) read returned error (%d)\n", msr, ret);
+		/* MSR read failures are treated as fatal errors */
+		snp_abort();
+	}
+
+	__sev_put_ghcb(&state);
+	local_irq_restore(flags);
+
+	return regs.ax | regs.dx << 32;
+}
+
 void savic_ghcb_msr_write(u32 reg, u64 value)
 {
 	u64 msr = APIC_BASE_MSR + (reg >> 4);
arch/x86/include/asm/apic.h +8 -0
index b510008c586f..7616a622248c 100644
@@ -338,6 +338,7 @@ struct apic_override {
 	void	(*icr_write)(u32 low, u32 high);
 	int	(*wakeup_secondary_cpu)(u32 apicid, unsigned long start_eip);
 	int	(*wakeup_secondary_cpu_64)(u32 apicid, unsigned long start_eip);
+	void	(*update_vector)(unsigned int cpu, unsigned int vector, bool set);
 };
 
 /*
@@ -397,6 +398,7 @@ DECLARE_APIC_CALL(wait_icr_idle);
 DECLARE_APIC_CALL(wakeup_secondary_cpu);
 DECLARE_APIC_CALL(wakeup_secondary_cpu_64);
 DECLARE_APIC_CALL(write);
+DECLARE_APIC_CALL(update_vector);
 
 static __always_inline u32 apic_read(u32 reg)
 {
@@ -473,6 +475,11 @@ static __always_inline bool apic_id_valid(u32 apic_id)
 	return apic_id <= apic->max_apic_id;
 }
 
+static __always_inline void apic_update_vector(unsigned int cpu, unsigned int vector, bool set)
+{
+	static_call(apic_call_update_vector)(cpu, vector, set);
+}
+
 #else /* CONFIG_X86_LOCAL_APIC */
 
 static inline u32 apic_read(u32 reg) { return 0; }
@@ -484,6 +491,7 @@ static inline void apic_wait_icr_idle(void) { }
 static inline u32 safe_apic_wait_icr_idle(void) { return 0; }
 static inline void apic_native_eoi(void) { WARN_ON_ONCE(1); }
 static inline void apic_setup_apic_calls(void) { }
+static inline void apic_update_vector(unsigned int cpu, unsigned int vector, bool set) { }
 
 #define apic_update_callback(_callback, _fn) do { } while (0)
 
arch/x86/include/asm/sev.h +2 -0
index 855c705ee074..7c942b9c593a 100644
@@ -484,6 +484,7 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req
 void __init snp_secure_tsc_prepare(void);
 void __init snp_secure_tsc_init(void);
 enum es_result savic_register_gpa(u64 gpa);
+u64 savic_ghcb_msr_read(u32 reg);
 void savic_ghcb_msr_write(u32 reg, u64 value);
 
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
@@ -530,6 +531,7 @@ static inline void __init snp_secure_tsc_prepare(void) { }
 static inline void __init snp_secure_tsc_init(void) { }
 static inline enum es_result savic_register_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; }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/kernel/apic/apic.c +2 -0
index f59ed284ec5b..86f9c3c7df1c 100644
@@ -591,6 +591,8 @@ static void setup_APIC_timer(void)
 						0xF, ~0UL);
 	} else
 		clockevents_register_device(levt);
+
+	apic_update_vector(smp_processor_id(), LOCAL_TIMER_VECTOR, true);
 }
 
 /*
arch/x86/kernel/apic/init.c +3 -0
index 821e2e536f19..b420f9cd0ddb 100644
@@ -29,6 +29,7 @@ DEFINE_APIC_CALL(wait_icr_idle);
 DEFINE_APIC_CALL(wakeup_secondary_cpu);
 DEFINE_APIC_CALL(wakeup_secondary_cpu_64);
 DEFINE_APIC_CALL(write);
+DEFINE_APIC_CALL(update_vector);
 
 EXPORT_STATIC_CALL_TRAMP_GPL(apic_call_send_IPI_mask);
 EXPORT_STATIC_CALL_TRAMP_GPL(apic_call_send_IPI_self);
@@ -56,6 +57,7 @@ static __init void restore_override_callbacks(void)
 	apply_override(icr_write);
 	apply_override(wakeup_secondary_cpu);
 	apply_override(wakeup_secondary_cpu_64);
+	apply_override(update_vector);
 }
 
 #define update_call(__cb)					\
@@ -78,6 +80,7 @@ static __init void update_static_calls(void)
 	update_call(wait_icr_idle);
 	update_call(wakeup_secondary_cpu);
 	update_call(wakeup_secondary_cpu_64);
+	update_call(update_vector);
 }
 
 void __init apic_setup_apic_calls(void)
arch/x86/kernel/apic/vector.c +0 -6
index 897e85e58139..09eb553269b8 100644
@@ -139,12 +139,6 @@ static void apic_update_irq_cfg(struct irq_data *irqd, unsigned int vector,
 			    apicd->hw_irq_cfg.dest_apicid);
 }
 
-static inline void apic_update_vector(unsigned int cpu, unsigned int vector, bool set)
-{
-	if (apic->update_vector)
-		apic->update_vector(cpu, vector, set);
-}
-
 static int irq_alloc_vector(const struct cpumask *dest, bool resvd, unsigned int *cpu)
 {
 	int vector;
arch/x86/kernel/apic/x2apic_savic.c +5 -2
index 657e560978e7..1088d82e3adb 100644
@@ -83,6 +83,7 @@ static u32 x2apic_savic_read(u32 reg)
 	case APIC_TMICT:
 	case APIC_TMCCT:
 	case APIC_TDCR:
+		return savic_ghcb_msr_read(reg);
 	case APIC_ID:
 	case APIC_LVR:
 	case APIC_TASKPRI:
@@ -143,10 +144,12 @@ static void x2apic_savic_write(u32 reg, u32 data)
 {
 	switch (reg) {
 	case APIC_LVTT:
-	case APIC_LVT0:
-	case APIC_LVT1:
 	case APIC_TMICT:
 	case APIC_TDCR:
+		savic_ghcb_msr_write(reg, data);
+		break;
+	case APIC_LVT0:
+	case APIC_LVT1:
 	case APIC_TASKPRI:
 	case APIC_EOI:
 	case APIC_SPIV:
Signature · 1 line
2.34.1
#14 lore raw

Re: [PATCH v3 07/17] x86/apic: Support LAPIC timer for Secure AVIC

Quoted 3 lines · Neeraj Upadhyay

In addition, add a static call for apic's update_vector() callback, to configure ALLOWED_IRR for the hypervisor to inject timer interrupt using LOCAL_TIMER_VECTOR.

How is this static call related to the timer vector? It just works with the conditional callback. apic_update_vector() is not used in a hotpath.

Even if there is a valid reason for the static call, why is this not part of the patch, which adds the update_vector() callback?

It's well documented that you should not do random unrelated things in patches.

You really try hard to make review a pain.

Thanks,

tglx

#15 lore raw

Re: [PATCH v3 07/17] x86/apic: Support LAPIC timer for Secure AVIC

Quoted 9 lines · Thomas Gleixner
Quoted 3 lines · Neeraj Upadhyay

In addition, add a static call for apic's update_vector() callback, to configure ALLOWED_IRR for the hypervisor to inject timer interrupt using LOCAL_TIMER_VECTOR.

How is this static call related to the timer vector? It just works with the conditional callback. apic_update_vector() is not used in a hotpath.

No it is not related. I missed the point that static call is only for callbacks which are used in hotpaths. I thought that they are for callbacks which are called from multiple call sites. So, when I added second caller here (first was added in 05/17, in vector.c), I converted local apic_update_vector() in vector.c to static call.

Quoted 3 lines

Even if there is a valid reason for the static call, why is this not part of the patch, which adds the update_vector() callback?

Again, this was based on incorrect understanding mentioned above.

Quoted 5 lines

It's well documented that you should not do random unrelated things in patches.

You really try hard to make review a pain.

Apologies for that.

- Neeraj

Quoted 5 lines

Thanks,

tglx

#16 lore raw

PATCH v3 8/17 x86/sev: Initialize VGIF for secondary VCPUs for Secure AVIC

In reply to Neeraj Upadhyay

From: Kishon Vijay Abraham I <[email protected]>

Secure AVIC requires VGIF to be configured in VMSA. Configure for secondary vCPUs (the configuration for boot CPU is done by the hypervisor).

---
Changes since v2:
- No change

 arch/x86/coco/sev/core.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/coco/sev/core.c +3 -0
index 1122cf93983d..bc8c3b596dd1 100644
@@ -1270,6 +1270,9 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
 	vmsa->x87_ftw		= AP_INIT_X87_FTW_DEFAULT;
 	vmsa->x87_fcw		= AP_INIT_X87_FCW_DEFAULT;
 
+	if (cc_platform_has(CC_ATTR_SNP_SECURE_AVIC))
+		vmsa->vintr_ctrl	|= V_GIF_MASK;
+
 	/* SVME must be set. */
 	vmsa->efer		= EFER_SVME;
 
Signature · 1 line
2.34.1
#17 lore raw

PATCH v3 9/17 x86/apic: Add support to send NMI IPI for Secure AVIC

In reply to Neeraj Upadhyay

Secure AVIC has introduced a new field in the APIC backing page "NmiReq" that has to be set by the guest to request a NMI IPI through APIC_ICR write.

Add support to set NmiReq appropriately to send NMI IPI.

This also requires Virtual NMI feature to be enabled in VINTRL_CTRL field in the VMSA. However this would be added by a later commit after adding support for injecting NMI from the hypervisor.

---
Changes since v2:
- Updates to use per_cpu_ptr() on apic_page struct.

 arch/x86/kernel/apic/x2apic_savic.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)
arch/x86/kernel/apic/x2apic_savic.c +20 -8
index 1088d82e3adb..f2310d90443d 100644
@@ -180,12 +180,19 @@ static void x2apic_savic_write(u32 reg, u32 data)
 	}
 }
 
-static inline void send_ipi_dest(unsigned int cpu, unsigned int vector)
+static void send_ipi_dest(unsigned int cpu, unsigned int vector, bool nmi)
 {
+	if (nmi) {
+		struct apic_page *ap = per_cpu_ptr(apic_page, cpu);
+
+		WRITE_ONCE(ap->regs[SAVIC_NMI_REQ >> 2], 1);
+		return;
+	}
+
 	update_vector(cpu, APIC_IRR, vector, true);
 }
 
-static void send_ipi_allbut(unsigned int vector)
+static void send_ipi_allbut(unsigned int vector, bool nmi)
 {
 	unsigned int cpu, src_cpu;
 	unsigned long flags;
@@ -197,16 +204,19 @@ static void send_ipi_allbut(unsigned int vector)
 	for_each_cpu(cpu, cpu_online_mask) {
 		if (cpu == src_cpu)
 			continue;
-		send_ipi_dest(cpu, vector);
+		send_ipi_dest(cpu, vector, nmi);
 	}
 
 	local_irq_restore(flags);
 }
 
-static inline void self_ipi(unsigned int vector)
+static inline void self_ipi(unsigned int vector, bool nmi)
 {
 	u32 icr_low = APIC_SELF_IPI | vector;
 
+	if (nmi)
+		icr_low |= APIC_DM_NMI;
+
 	native_x2apic_icr_write(icr_low, 0);
 }
 
@@ -214,22 +224,24 @@ static void x2apic_savic_icr_write(u32 icr_low, u32 icr_high)
 {
 	unsigned int dsh, vector;
 	u64 icr_data;
+	bool nmi;
 
 	dsh = icr_low & APIC_DEST_ALLBUT;
 	vector = icr_low & APIC_VECTOR_MASK;
+	nmi = ((icr_low & APIC_DM_FIXED_MASK) == APIC_DM_NMI);
 
 	switch (dsh) {
 	case APIC_DEST_SELF:
-		self_ipi(vector);
+		self_ipi(vector, nmi);
 		break;
 	case APIC_DEST_ALLINC:
-		self_ipi(vector);
+		self_ipi(vector, nmi);
 		fallthrough;
 	case APIC_DEST_ALLBUT:
-		send_ipi_allbut(vector);
+		send_ipi_allbut(vector, nmi);
 		break;
 	default:
-		send_ipi_dest(icr_high, vector);
+		send_ipi_dest(icr_high, vector, nmi);
 		break;
 	}
 
Signature · 1 line
2.34.1
#18 lore raw

PATCH v3 10/17 x86/apic: Allow NMI to be injected from hypervisor for Secure AVIC

In reply to Neeraj Upadhyay

Secure AVIC requires "AllowedNmi" bit in the Secure AVIC Control MSR to be set for NMI to be injected from hypervisor. Set "AllowedNmi" bit in Secure AVIC Control MSR to allow NMI interrupts to be injected from hypervisor.

---
Changes since v2:
- Remove MSR_AMD64_SECURE_AVIC_EN macros from this patch.

 arch/x86/include/asm/msr-index.h    | 3 +++
 arch/x86/kernel/apic/x2apic_savic.c | 6 ++++++
 2 files changed, 9 insertions(+)
arch/x86/include/asm/msr-index.h +3 -0
index 0090b6f1d6f9..28cec4460918 100644
@@ -689,6 +689,9 @@
 #define MSR_AMD64_SNP_SECURE_AVIC	BIT_ULL(MSR_AMD64_SNP_SECURE_AVIC_BIT)
 #define MSR_AMD64_SNP_RESV_BIT		19
 #define MSR_AMD64_SNP_RESERVED_MASK	GENMASK_ULL(63, MSR_AMD64_SNP_RESV_BIT)
+#define MSR_AMD64_SECURE_AVIC_CONTROL	0xc0010138
+#define MSR_AMD64_SECURE_AVIC_ALLOWEDNMI_BIT 1
+#define MSR_AMD64_SECURE_AVIC_ALLOWEDNMI BIT_ULL(MSR_AMD64_SECURE_AVIC_ALLOWEDNMI_BIT)
 #define MSR_AMD64_RMP_BASE		0xc0010132
 #define MSR_AMD64_RMP_END		0xc0010133
 #define MSR_AMD64_RMP_CFG		0xc0010136
arch/x86/kernel/apic/x2apic_savic.c +6 -0
index f2310d90443d..845d90cbdcdf 100644
@@ -29,6 +29,11 @@ struct apic_page {
 
 static struct apic_page __percpu *apic_page __ro_after_init;
 
+static inline void savic_wr_control_msr(u64 val)
+{
+	native_wrmsr(MSR_AMD64_SECURE_AVIC_CONTROL, lower_32_bits(val), upper_32_bits(val));
+}
+
 static int x2apic_savic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
 {
 	return x2apic_enabled() && cc_platform_has(CC_ATTR_SNP_SECURE_AVIC);
@@ -349,6 +354,7 @@ static void x2apic_savic_setup(void)
 	ret = savic_register_gpa(gpa);
 	if (ret != ES_OK)
 		snp_abort();
+	savic_wr_control_msr(gpa | MSR_AMD64_SECURE_AVIC_ALLOWEDNMI);
 }
 
 static int x2apic_savic_probe(void)
Signature · 1 line
2.34.1
#19 lore raw

PATCH v3 11/17 x86/sev: Enable NMI support for Secure AVIC

In reply to Neeraj Upadhyay

From: Kishon Vijay Abraham I <[email protected]>

Now that support to send NMI IPI and support to inject NMI from the hypervisor has been added, set V_NMI_ENABLE in VINTR_CTRL field of VMSA to enable NMI for Secure AVIC guests.

---
Changes since v2:
- No change.

 arch/x86/coco/sev/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/coco/sev/core.c +1 -1
index bc8c3b596dd1..9ade2b1993ad 100644
@@ -1271,7 +1271,7 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
 	vmsa->x87_fcw		= AP_INIT_X87_FCW_DEFAULT;
 
 	if (cc_platform_has(CC_ATTR_SNP_SECURE_AVIC))
-		vmsa->vintr_ctrl	|= V_GIF_MASK;
+		vmsa->vintr_ctrl	|= (V_GIF_MASK | V_NMI_ENABLE_MASK);
 
 	/* SVME must be set. */
 	vmsa->efer		= EFER_SVME;
Signature · 1 line
2.34.1
#20 lore raw

PATCH v3 12/17 x86/apic: Read and write LVT* APIC registers from HV for SAVIC guests

In reply to Neeraj Upadhyay

Hypervisor need information about the current state of LVT registers for device emulation and NMI. So, forward reads and write of these registers to the Hypervisor for Secure AVIC guests.

---
Changes since v2:
- No change.

 arch/x86/kernel/apic/x2apic_savic.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
arch/x86/kernel/apic/x2apic_savic.c +10 -10
index 845d90cbdcdf..4adb9cad0a0c 100644
@@ -88,6 +88,11 @@ static u32 x2apic_savic_read(u32 reg)
 	case APIC_TMICT:
 	case APIC_TMCCT:
 	case APIC_TDCR:
+	case APIC_LVTTHMR:
+	case APIC_LVTPC:
+	case APIC_LVT0:
+	case APIC_LVT1:
+	case APIC_LVTERR:
 		return savic_ghcb_msr_read(reg);
 	case APIC_ID:
 	case APIC_LVR:
@@ -98,11 +103,6 @@ static u32 x2apic_savic_read(u32 reg)
 	case APIC_SPIV:
 	case APIC_ESR:
 	case APIC_ICR:
-	case APIC_LVTTHMR:
-	case APIC_LVTPC:
-	case APIC_LVT0:
-	case APIC_LVT1:
-	case APIC_LVTERR:
 	case APIC_EFEAT:
 	case APIC_ECTRL:
 	case APIC_SEOI:
@@ -151,19 +151,19 @@ static void x2apic_savic_write(u32 reg, u32 data)
 	case APIC_LVTT:
 	case APIC_TMICT:
 	case APIC_TDCR:
-		savic_ghcb_msr_write(reg, data);
-		break;
 	case APIC_LVT0:
 	case APIC_LVT1:
+	case APIC_LVTTHMR:
+	case APIC_LVTPC:
+	case APIC_LVTERR:
+		savic_ghcb_msr_write(reg, data);
+		break;
 	case APIC_TASKPRI:
 	case APIC_EOI:
 	case APIC_SPIV:
 	case SAVIC_NMI_REQ:
 	case APIC_ESR:
 	case APIC_ICR:
-	case APIC_LVTTHMR:
-	case APIC_LVTPC:
-	case APIC_LVTERR:
 	case APIC_ECTRL:
 	case APIC_SEOI:
 	case APIC_IER:
Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH v3 12/17] x86/apic: Read and write LVT* APIC registers from HV for SAVIC guests

__ __ _ _ _ _____ _____

|  \/  (_) |    | |   / ____|     |  __ \
| \  / |_| |    | | _| |  __  ___ | |  | |
| |\/| | | |    | |/ / | |_ |/ _ \| |  | |
| |  | | | |____|   <| |__| | (_) | |__| |
|_|  |_|_|______|_|\_\\_____|\___/|_____/

________________________________
From: Neeraj Upadhyay <[email protected]>
Sent: Tuesday, April 1, 2025 1:41:05 AM
To: [email protected] <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: [PATCH v3 12/17] x86/apic: Read and write LVT* APIC registers from HV for SAVIC guests

Hypervisor need information about the current state of LVT registers for device emulation and NMI. So, forward reads and write of these registers to the Hypervisor for Secure AVIC guests.

---
Changes since v2:
- No change.

 arch/x86/kernel/apic/x2apic_savic.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
arch/x86/kernel/apic/x2apic_savic.c +10 -10
index 845d90cbdcdf..4adb9cad0a0c 100644
@@ -88,6 +88,11 @@ static u32 x2apic_savic_read(u32 reg)
         case APIC_TMICT:
         case APIC_TMCCT:
         case APIC_TDCR:
+       case APIC_LVTTHMR:
+       case APIC_LVTPC:
+       case APIC_LVT0:
+       case APIC_LVT1:
+       case APIC_LVTERR:
                 return savic_ghcb_msr_read(reg);
         case APIC_ID:
         case APIC_LVR:
@@ -98,11 +103,6 @@ static u32 x2apic_savic_read(u32 reg)
         case APIC_SPIV:
         case APIC_ESR:
         case APIC_ICR:
-       case APIC_LVTTHMR:
-       case APIC_LVTPC:
-       case APIC_LVT0:
-       case APIC_LVT1:
-       case APIC_LVTERR:
         case APIC_EFEAT:
         case APIC_ECTRL:
         case APIC_SEOI:
@@ -151,19 +151,19 @@ static void x2apic_savic_write(u32 reg, u32 data)
         case APIC_LVTT:
         case APIC_TMICT:
         case APIC_TDCR:
-               savic_ghcb_msr_write(reg, data);
-               break;
         case APIC_LVT0:
         case APIC_LVT1:
+       case APIC_LVTTHMR:
+       case APIC_LVTPC:
+       case APIC_LVTERR:
+               savic_ghcb_msr_write(reg, data);
+               break;
         case APIC_TASKPRI:
         case APIC_EOI:
         case APIC_SPIV:
         case SAVIC_NMI_REQ:
         case APIC_ESR:
         case APIC_ICR:
-       case APIC_LVTTHMR:
-       case APIC_LVTPC:
-       case APIC_LVTERR:
         case APIC_ECTRL:
         case APIC_SEOI:
         case APIC_IER:

--
2.34.1

#22 lore raw

PATCH v3 13/17 x86/apic: Handle EOI writes for SAVIC guests

In reply to Neeraj Upadhyay

Secure AVIC accelerates guest's EOI msr writes for edge-triggered interrupts. For level-triggered interrupts, EOI msr writes trigger VC exception with SVM_EXIT_AVIC_UNACCELERATED_ACCESS error code. The VC handler would need to trigger a GHCB protocol MSR write event to to notify the Hypervisor about completion of the level-triggered interrupt. This is required for cases like emulated IOAPIC. VC exception handling adds extra performance overhead for APIC register write. In addition, some unaccelerated APIC register msr writes are trapped, whereas others are faulted. This results in additional complexity in VC exception handling for unacclerated accesses. So, directly do a GHCB protocol based EOI write from apic->eoi() callback for level-triggered interrupts. Use wrmsr for edge-triggered interrupts, so that hardware re-evaluates any pending interrupt which can be delivered to guest vCPU. For level-triggered interrupts, re-evaluation happens on return from VMGEXIT corresponding to the GHCB event for EOI msr write.

---
Changes since v2:
- Reuse find_highest_vector() from kvm/lapic.c
- Misc cleanups.

 arch/x86/include/asm/apic-emul.h    | 28 +++++++++++++
 arch/x86/kernel/apic/x2apic_savic.c | 62 +++++++++++++++++++++++++----
 arch/x86/kvm/lapic.c                | 23 ++---------
 3 files changed, 85 insertions(+), 28 deletions(-)
 create mode 100644 arch/x86/include/asm/apic-emul.h
arch/x86/include/asm/apic-emul.h new file +28 -0
new file mode 100644
index 000000000000..60d9e88fefc6
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _ASM_X86_APIC_EMUL_H
+#define _ASM_X86_APIC_EMUL_H
+
+#define MAX_APIC_VECTOR			256
+#define APIC_VECTORS_PER_REG		32
+
+static inline int apic_find_highest_vector(void *bitmap)
+{
+	unsigned int regno;
+	unsigned int vec;
+	u32 *reg;
+
+	/*
+	 * The registers int the bitmap are 32-bit wide and 16-byte
+	 * aligned. State of a vector is stored in a single bit.
+	 */
+	for (regno = MAX_APIC_VECTOR / APIC_VECTORS_PER_REG - 1; regno >= 0; regno--) {
+		vec = regno * APIC_VECTORS_PER_REG;
+		reg = bitmap + regno * 16;
+		if (*reg)
+			return __fls(*reg) + vec;
+	}
+
+	return -1;
+}
+
+#endif /* _ASM_X86_APIC_EMUL_H */
arch/x86/kernel/apic/x2apic_savic.c +54 -8

99 lines not on this page · raw message

arch/x86/kvm/lapic.c +3 -20
index 28e3317124fd..8269af8666b8 100644
@@ -27,6 +27,7 @@
 #include <linux/export.h>
 #include <linux/math64.h>
 #include <linux/slab.h>
+#include <asm/apic-emul.h>
 #include <asm/processor.h>
 #include <asm/mce.h>
 #include <asm/msr.h>
@@ -55,9 +56,6 @@
 /* 14 is the version for Xeon and Pentium 8.4.8*/
 #define APIC_VERSION			0x14UL
 #define LAPIC_MMIO_LENGTH		(1 << 12)
-/* followed define is not in apicdef.h */
-#define MAX_APIC_VECTOR			256
-#define APIC_VECTORS_PER_REG		32
 
 /*
  * Enable local APIC timer advancement (tscdeadline mode only) with adaptive
@@ -626,21 +624,6 @@ static const unsigned int apic_lvt_mask[KVM_APIC_MAX_NR_LVT_ENTRIES] = {
 	[LVT_CMCI] = LVT_MASK | APIC_MODE_MASK
 };
 
-static int find_highest_vector(void *bitmap)
-{
-	int vec;
-	u32 *reg;
-
-	for (vec = MAX_APIC_VECTOR - APIC_VECTORS_PER_REG;
-	     vec >= 0; vec -= APIC_VECTORS_PER_REG) {
-		reg = bitmap + REG_POS(vec);
-		if (*reg)
-			return __fls(*reg) + vec;
-	}
-
-	return -1;
-}
-
 static u8 count_vectors(void *bitmap)
 {
 	int vec;
@@ -704,7 +687,7 @@ EXPORT_SYMBOL_GPL(kvm_apic_update_irr);
 
 static inline int apic_search_irr(struct kvm_lapic *apic)
 {
-	return find_highest_vector(apic->regs + APIC_IRR);
+	return apic_find_highest_vector(apic->regs + APIC_IRR);
 }
 
 static inline int apic_find_highest_irr(struct kvm_lapic *apic)
@@ -779,7 +762,7 @@ static inline int apic_find_highest_isr(struct kvm_lapic *apic)
 	if (likely(apic->highest_isr_cache != -1))
 		return apic->highest_isr_cache;
 
-	result = find_highest_vector(apic->regs + APIC_ISR);
+	result = apic_find_highest_vector(apic->regs + APIC_ISR);
 	ASSERT(result == -1 || result >= 16);
 
 	return result;
Signature · 1 line
2.34.1
#23 lore raw

Re: [PATCH v3 13/17] x86/apic: Handle EOI writes for SAVIC guests

Quoted 17 lines · Neeraj Upadhyay

Secure AVIC accelerates guest's EOI msr writes for edge-triggered interrupts. For level-triggered interrupts, EOI msr writes trigger VC exception with SVM_EXIT_AVIC_UNACCELERATED_ACCESS error code. The VC handler would need to trigger a GHCB protocol MSR write event to to notify the Hypervisor about completion of the level-triggered interrupt. This is required for cases like emulated IOAPIC. VC exception handling adds extra performance overhead for APIC register write. In addition, some unaccelerated APIC register msr writes are trapped, whereas others are faulted. This results in additional complexity in VC exception handling for unacclerated accesses. So, directly do a GHCB protocol based EOI write from apic->eoi() callback for level-triggered interrupts. Use wrmsr for edge-triggered interrupts, so that hardware re-evaluates any pending interrupt which can be delivered to guest vCPU. For level-triggered interrupts, re-evaluation happens on return from VMGEXIT corresponding to the GHCB event for EOI msr write.

Quoted 8 lines

---
Changes since v2:
- Reuse find_highest_vector() from kvm/lapic.c
- Misc cleanups.

 arch/x86/include/asm/apic-emul.h    | 28 +++++++++++++
 arch/x86/kernel/apic/x2apic_savic.c | 62 +++++++++++++++++++++++++----
 arch/x86/kvm/lapic.c                | 23 ++---------

Please isolate the KVM changes to a standalone patch.

arch/x86/include/asm/apic-emul.h

Quoted 8 lines
 3 files changed, 85 insertions(+), 28 deletions(-)
 create mode 100644 arch/x86/include/asm/apic-emul.h
arch/x86/include/asm/apic-emul.h new file +0 -0
new file mode 100644
index 000000000000..60d9e88fefc6

I don't see any reason for a new file. arch/x86/include/asm/apic.h already has is_vector_pending() and lapic_vector_set_in_irr(), this functionality is more or less the same.

#24 lore raw

Re: [PATCH v3 13/17] x86/apic: Handle EOI writes for SAVIC guests

Quoted 23 lines · Sean Christopherson
Quoted 22 lines · Neeraj Upadhyay

Secure AVIC accelerates guest's EOI msr writes for edge-triggered interrupts. For level-triggered interrupts, EOI msr writes trigger VC exception with SVM_EXIT_AVIC_UNACCELERATED_ACCESS error code. The VC handler would need to trigger a GHCB protocol MSR write event to to notify the Hypervisor about completion of the level-triggered interrupt. This is required for cases like emulated IOAPIC. VC exception handling adds extra performance overhead for APIC register write. In addition, some unaccelerated APIC register msr writes are trapped, whereas others are faulted. This results in additional complexity in VC exception handling for unacclerated accesses. So, directly do a GHCB protocol based EOI write from apic->eoi() callback for level-triggered interrupts. Use wrmsr for edge-triggered interrupts, so that hardware re-evaluates any pending interrupt which can be delivered to guest vCPU. For level-triggered interrupts, re-evaluation happens on return from VMGEXIT corresponding to the GHCB event for EOI msr write.

---
Changes since v2:
- Reuse find_highest_vector() from kvm/lapic.c
- Misc cleanups.

Quoted 6 lines
Quoted 3 lines
 arch/x86/include/asm/apic-emul.h    | 28 +++++++++++++
 arch/x86/kernel/apic/x2apic_savic.c | 62 +++++++++++++++++++++++++----
 arch/x86/kvm/lapic.c                | 23 ++---------

Please isolate the KVM changes to a standalone patch.

Ok sure.

Quoted 12 lines

arch/x86/include/asm/apic-emul.h

Quoted 8 lines
 3 files changed, 85 insertions(+), 28 deletions(-)
 create mode 100644 arch/x86/include/asm/apic-emul.h
arch/x86/include/asm/apic-emul.h new file +0 -0
new file mode 100644
index 000000000000..60d9e88fefc6

I don't see any reason for a new file. arch/x86/include/asm/apic.h already has is_vector_pending() and lapic_vector_set_in_irr(), this functionality is more or less the same.

Ok. The intent here was to separate out emulated apic operations from the one which go through the native apic driver.

- Neeraj

#25 lore raw

PATCH v3 14/17 x86/apic: Add kexec support for Secure AVIC

In reply to Neeraj Upadhyay

Add a apic->teardown() callback to disable Secure AVIC before rebooting into the new kernel. This ensures that the new kernel does not access the old APIC backing page which was allocated by the previous kernel. Such accesses can happen if there are any APIC accesses done during guest boot before Secure AVIC driver probe is done by the new kernel (as Secure AVIC would have remained enabled in the Secure AVIC control msr).

---
Changes since v2:
- Change savic_unregister_gpa() interface to allow GPA unregistration only for local CPU.

 arch/x86/coco/sev/core.c            | 25 +++++++++++++++++++++++++
 arch/x86/include/asm/apic.h         |  1 +
 arch/x86/include/asm/sev.h          |  2 ++
 arch/x86/kernel/apic/apic.c         |  3 +++
 arch/x86/kernel/apic/x2apic_savic.c |  8 ++++++++
 5 files changed, 39 insertions(+)
arch/x86/coco/sev/core.c +25 -0
index 9ade2b1993ad..2381859491db 100644
@@ -1588,6 +1588,31 @@ enum es_result savic_register_gpa(u64 gpa)
 	return res;
 }
 
+enum es_result savic_unregister_gpa(u64 *gpa)
+{
+	struct ghcb_state state;
+	struct es_em_ctxt ctxt;
+	unsigned long flags;
+	struct ghcb *ghcb;
+	int ret = 0;
+
+	local_irq_save(flags);
+
+	ghcb = __sev_get_ghcb(&state);
+
+	vc_ghcb_invalidate(ghcb);
+
+	ghcb_set_rax(ghcb, -1ULL);
+	ret = sev_es_ghcb_hv_call(ghcb, &ctxt, SVM_VMGEXIT_SECURE_AVIC,
+			SVM_VMGEXIT_SECURE_AVIC_UNREGISTER_GPA, 0);
+	if (gpa && ret == ES_OK)
+		*gpa = ghcb->save.rbx;
+	__sev_put_ghcb(&state);
+
+	local_irq_restore(flags);
+	return ret;
+}
+
 static void snp_register_per_cpu_ghcb(void)
 {
 	struct sev_es_runtime_data *data;
arch/x86/include/asm/apic.h +1 -0
index 7616a622248c..0cd9315226d2 100644
@@ -306,6 +306,7 @@ struct apic {
 	/* Probe, setup and smpboot functions */
 	int	(*probe)(void);
 	void	(*setup)(void);
+	void	(*teardown)(void);
 	int	(*acpi_madt_oem_check)(char *oem_id, char *oem_table_id);
 
 	void	(*init_apic_ldr)(void);
arch/x86/include/asm/sev.h +2 -0
index 7c942b9c593a..8a08a03183b4 100644
@@ -484,6 +484,7 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req
 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);
 
@@ -530,6 +531,7 @@ static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_
 static inline void __init snp_secure_tsc_prepare(void) { }
 static inline void __init snp_secure_tsc_init(void) { }
 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; }
 
arch/x86/kernel/apic/apic.c +3 -0
index 86f9c3c7df1c..b5236c8c3032 100644
@@ -1169,6 +1169,9 @@ void disable_local_APIC(void)
 	if (!apic_accessible())
 		return;
 
+	if (apic->teardown)
+		apic->teardown();
+
 	apic_soft_disable();
 
 #ifdef CONFIG_X86_32
arch/x86/kernel/apic/x2apic_savic.c +8 -0
index 9e2a9bdb0762..8cfffdc4cf8b 100644
@@ -349,6 +349,13 @@ static void init_apic_page(void)
 	set_reg(APIC_ID, apic_id);
 }
 
+static void x2apic_savic_teardown(void)
+{
+	/* Disable Secure AVIC */
+	native_wrmsr(MSR_AMD64_SECURE_AVIC_CONTROL, 0, 0);
+	savic_unregister_gpa(NULL);
+}
+
 static void x2apic_savic_setup(void)
 {
 	void *backing_page;
@@ -426,6 +433,7 @@ static struct apic apic_x2apic_savic __ro_after_init = {
 	.probe				= x2apic_savic_probe,
 	.acpi_madt_oem_check		= x2apic_savic_acpi_madt_oem_check,
 	.setup				= x2apic_savic_setup,
+	.teardown			= x2apic_savic_teardown,
 
 	.dest_mode_logical		= false,
 
Signature · 1 line
2.34.1
#26 lore raw

Re: [PATCH v3 14/17] x86/apic: Add kexec support for Secure AVIC

Quoted 31 lines · Neeraj Upadhyay

Add a apic->teardown() callback to disable Secure AVIC before rebooting into the new kernel. This ensures that the new kernel does not access the old APIC backing page which was allocated by the previous kernel. Such accesses can happen if there are any APIC accesses done during guest boot before Secure AVIC driver probe is done by the new kernel (as Secure AVIC would have remained enabled in the Secure AVIC control msr).

---
Changes since v2:
 - Change savic_unregister_gpa() interface to allow GPA unregistration
   only for local CPU.

 arch/x86/coco/sev/core.c            | 25 +++++++++++++++++++++++++ arch/x86/include/asm/apic.h         |  1 + arch/x86/include/asm/sev.h          |  2 ++ arch/x86/kernel/apic/apic.c         |  3 +++ arch/x86/kernel/apic/x2apic_savic.c |  8 ++++++++

 5 files changed, 39 insertions(+)
arch/x86/coco/sev/core.c +0 -0
index 9ade2b1993ad..2381859491db 100644
@@ -1588,6 +1588,31 @@ enum es_result savic_register_gpa(u64 gpa)
         return res;
  }

arch/x86/coco/sev/core.c enum es_result savic_register_gpa(u64 gpa) · line 1597

Quoted 7 lines
arch/x86/coco/sev/core.c +7 -0
+enum es_result savic_unregister_gpa(u64 *gpa)
+{
+       struct ghcb_state state;
+       struct es_em_ctxt ctxt;
+       unsigned long flags;
+       struct ghcb *ghcb;
+       int ret = 0;

This should be an enum es_result, and there is no need to zero- initialize it.

Quoted 2 lines

+
+       local_irq_save(flags);

guard(irqsave)();

Quoted 16 lines
arch/x86/coco/sev/core.c +7 -0
+
+       ghcb = __sev_get_ghcb(&state);
+
+       vc_ghcb_invalidate(ghcb);
+
+       ghcb_set_rax(ghcb, -1ULL);
+       ret = sev_es_ghcb_hv_call(ghcb, &ctxt,

SVM_VMGEXIT_SECURE_AVIC,
+                       SVM_VMGEXIT_SECURE_AVIC_UNREGISTER_GPA, 0); +       if (gpa && ret == ES_OK)
+               *gpa = ghcb->save.rbx;
+       __sev_put_ghcb(&state);

arch/x86/coco/sev/core.c +4 -0
+
+       local_irq_restore(flags);
+       return ret;
+}
#27 lore raw

PATCH v3 15/17 x86/apic: Enable Secure AVIC in Control MSR

In reply to Neeraj Upadhyay

With all the pieces in place now, enable Secure AVIC in Secure AVIC Control MSR. Any access to x2APIC MSRs are emulated by the hypervisor before Secure AVIC is enabled in the control MSR. Post Secure AVIC enablement, all x2APIC MSR accesses (whether accelerated by AVIC hardware or trapped as VC exception) operate on vCPU's APIC backing page.

---
Changes since v2:
- Move MSR_AMD64_SECURE_AVIC_EN* macros to this patch.

 arch/x86/include/asm/msr-index.h    | 2 ++
 arch/x86/kernel/apic/x2apic_savic.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +2 -0
index 28cec4460918..16745040f5f8 100644
@@ -690,6 +690,8 @@
 #define MSR_AMD64_SNP_RESV_BIT		19
 #define MSR_AMD64_SNP_RESERVED_MASK	GENMASK_ULL(63, MSR_AMD64_SNP_RESV_BIT)
 #define MSR_AMD64_SECURE_AVIC_CONTROL	0xc0010138
+#define MSR_AMD64_SECURE_AVIC_EN_BIT	0
+#define MSR_AMD64_SECURE_AVIC_EN	BIT_ULL(MSR_AMD64_SECURE_AVIC_EN_BIT)
 #define MSR_AMD64_SECURE_AVIC_ALLOWEDNMI_BIT 1
 #define MSR_AMD64_SECURE_AVIC_ALLOWEDNMI BIT_ULL(MSR_AMD64_SECURE_AVIC_ALLOWEDNMI_BIT)
 #define MSR_AMD64_RMP_BASE		0xc0010132
arch/x86/kernel/apic/x2apic_savic.c +1 -1
index 8cfffdc4cf8b..5b6fd08f2c2e 100644
@@ -379,7 +379,7 @@ static void x2apic_savic_setup(void)
 	ret = savic_register_gpa(gpa);
 	if (ret != ES_OK)
 		snp_abort();
-	savic_wr_control_msr(gpa | MSR_AMD64_SECURE_AVIC_ALLOWEDNMI);
+	savic_wr_control_msr(gpa | MSR_AMD64_SECURE_AVIC_EN | MSR_AMD64_SECURE_AVIC_ALLOWEDNMI);
 }
 
 static int x2apic_savic_probe(void)
Signature · 1 line
2.34.1
#28 lore raw

PATCH v3 16/17 x86/sev: Prevent SECURE_AVIC_CONTROL MSR interception for Secure AVIC guests

In reply to Neeraj Upadhyay

The SECURE_AVIC_CONTROL MSR holds the GPA of the guest APIC backing page and bitfields to control enablement of Secure AVIC and NMI by guest vCPUs. This MSR is populated by the guest and the hypervisor should not intercept it. A #VC exception will be generated otherwise. If this occurs and Secure AVIC is enabled, terminate guest execution.

---
Changes since v2:
- No change

 arch/x86/coco/sev/core.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/coco/sev/core.c +9 -0
index 2381859491db..3707813c421e 100644
@@ -1480,6 +1480,15 @@ static enum es_result __vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt
 		if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
 			return __vc_handle_secure_tsc_msrs(regs, write);
 		break;
+	case MSR_AMD64_SECURE_AVIC_CONTROL:
+		/*
+		 * AMD64_SECURE_AVIC_CONTROL should not be intercepted when
+		 * Secure AVIC is enabled. Terminate the Secure AVIC guest
+		 * if the interception is enabled.
+		 */
+		if (cc_platform_has(CC_ATTR_SNP_SECURE_AVIC))
+			return ES_VMM_ERROR;
+		fallthrough;
 	default:
 		break;
 	}
Signature · 1 line
2.34.1
#29 lore raw

PATCH v3 17/17 x86/sev: Indicate SEV-SNP guest supports Secure AVIC

In reply to Neeraj Upadhyay

Now that Secure AVIC support is added in the guest, indicate SEV-SNP guest supports Secure AVIC feature if CONFIG_AMD_SECURE_AVIC is enabled.

---
Changes since v2:
- Set SNP_FEATURE_SECURE_AVIC in SNP_FEATURES_PRESENT only when CONFIG_AMD_SECURE_AVIC is enabled.

 arch/x86/boot/compressed/sev.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/sev.c +8 -1
index 798fdd3dbd1e..adcbf53ad50d 100644
@@ -397,13 +397,20 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
 				 MSR_AMD64_SNP_SECURE_AVIC |		\
 				 MSR_AMD64_SNP_RESERVED_MASK)
 
+#ifdef CONFIG_AMD_SECURE_AVIC
+#define SNP_FEATURE_SECURE_AVIC		MSR_AMD64_SNP_SECURE_AVIC
+#else
+#define SNP_FEATURE_SECURE_AVIC		0
+#endif
+
 /*
  * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
  * by the guest kernel. As and when a new feature is implemented in the
  * guest kernel, a corresponding bit should be added to the mask.
  */
 #define SNP_FEATURES_PRESENT	(MSR_AMD64_SNP_DEBUG_SWAP |	\
-				 MSR_AMD64_SNP_SECURE_TSC)
+				 MSR_AMD64_SNP_SECURE_TSC |	\
+				 SNP_FEATURE_SECURE_AVIC)
 
 u64 snp_get_unsupported_features(u64 status)
 {
Signature · 1 line
2.34.1