AMD: Add Secure AVIC Guest Support
v6 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 8e3f38516462 "Merge branch into tip/master: 'x86/sgx'" 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
Changes since v5
v5: https://lore.kernel.org/lkml/[email protected]/
- Add back RFC tag due to new changes to share code between KVM's lapic emulation and Secure AVIC.
- Minor optimizations to the apic bitwise ops and set/get reg operations.
- Other misc fixes, cleanups and refactoring due to code sharing with KVM lapic implementation.
Change since v4
v4: https://lore.kernel.org/lkml/[email protected]/
- Add separate patch for update_vector() apic callback addition.
- Add a cleanup patch for moving apic_update_irq_cfg() calls to apic_update_vector().
- Cleaned up change logs.
- Rebased to latest tip/tip master. Resolved merge conflicts due to sev code movement to sev-startup.c in mainline.
- Other misc cleanups.
Change since v3
v3: https://lore.kernel.org/lkml/[email protected]/
- Move KVM updates to a separate patch.
- Cleanups to use guard().
- Refactored IPI callbacks addition.
- Misc cleanups.
Change since v2
v2: https://lore.kernel.org/lkml/[email protected]/
- 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.
Change since v1
v1: https://lore.kernel.org/lkml/[email protected]/
- Added Kexec support.
- Instead of doing a 2M aligned allocation for backing pages, allocate individual PAGE_SIZE pages for vCPUs.
- Instead of reading Extended Topology Enumeration CPUID, APIC_ID value is read from Hv and updated in APIC backing page. Hv returned ID is checked for any duplicates.
- Propagate all LVT* register reads and writes to Hv.
- Check that Secure AVIC control MSR is not intercepted by Hv.
- Fix EOI handling for level-triggered interrupts.
- Misc cleanups and commit log updates.
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 (29):
KVM: x86: Move find_highest_vector() to a common header
KVM: x86: Move lapic get/set_reg() helpers to common code
KVM: x86: Move lapic get/set_reg64() helpers to common code
KVM: x86: Move lapic set/clear_vector() helpers to common code
KVM: x86: Move {REG,VEC}_POS() macros to lapic.c
KVM: x86: apic_test_vector() to common code
x86/apic: Remove redundant parentheses around 'bitmap' x86/apic: Rename 'reg_off' to 'reg'
x86/apic: Change apic_*_vector() vector param to unsigned x86/apic: Change get/set reg operations reg param to unsigned x86/apic: Unionize apic regs for 32bit/64bit access w/o type casting x86/apic: Simplify bitwise operations on apic bitmap x86/apic: Move apic_update_irq_cfg() calls to apic_update_vector() 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 apic drivers 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 Secure AVIC 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
Sean Christopherson (1):
x86/apic: KVM: Deduplicate APIC vector => register+bit math
arch/x86/Kconfig | 13 + arch/x86/boot/compressed/sev.c | 10 +- arch/x86/coco/core.c | 3 + arch/x86/coco/sev/core.c | 103 +++++++ arch/x86/coco/sev/vc-handle.c | 20 +- arch/x86/include/asm/apic.h | 103 ++++++- arch/x86/include/asm/apicdef.h | 2 + arch/x86/include/asm/msr-index.h | 9 +- arch/x86/include/asm/sev-internal.h | 2 + arch/x86/include/asm/sev.h | 8 + arch/x86/include/uapi/asm/svm.h | 4 + arch/x86/kernel/apic/Makefile | 1 + arch/x86/kernel/apic/apic.c | 8 + arch/x86/kernel/apic/vector.c | 33 ++- arch/x86/kernel/apic/x2apic_savic.c | 437 ++++++++++++++++++++++++++++ arch/x86/kvm/lapic.c | 77 ++--- arch/x86/kvm/lapic.h | 24 +- include/linux/cc_platform.h | 8 + 18 files changed, 765 insertions(+), 100 deletions(-) create mode 100644 arch/x86/kernel/apic/x2apic_savic.c
base-commit: 8e3f385164626dc6bbf000decf04aa98e943e07e
Signature · 1 line
2.34.1
RFC PATCH v6 1/32 x86/apic: KVM: Deduplicate APIC vector => register+bit math
From: Sean Christopherson <[email protected]>
Consolidate KVM's {REG,VEC}_POS() macros and lapic_vector_set_in_irr()'s open coded equivalent logic in anticipation of the kernel gaining more usage of vector => reg+bit lookups.
Use lapic_vector_set_in_irr()'s math as using divides for both the bit number and register offset makes it easier to connect the dots, and for at least one user, fixup_irqs(), "/ 32 * 0x10" generates ever so slightly better code with gcc-14 (shaves a whole 3 bytes from the code stream):
((v) >> 5) << 4:
c1 ef 05 shr $0x5,%edi c1 e7 04 shl $0x4,%edi 81 c7 00 02 00 00 add $0x200,%edi
(v) / 32 * 0x10:
c1 ef 05 shr $0x5,%edi 83 c7 20 add $0x20,%edi c1 e7 04 shl $0x4,%edi
Keep KVM's tersely named macros as "wrappers" to avoid unnecessary churn in KVM, and because the shorter names yield more readable code overall in KVM.
No functional change intended (clang-19 and gcc-14 generate bit-for-bit identical code for all of kvm.ko).
---
Changes since v5:
- New change.
arch/x86/include/asm/apic.h | 7 +++++-- arch/x86/kvm/lapic.h | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/include/asm/apic.h +5 -2
@@ -488,11 +488,14 @@ static inline void apic_setup_apic_calls(void) { }extern void apic_ack_irq (struct irq_data *data); +#define APIC_VECTOR_TO_BIT_NUMBER (v) ((v) % 32) +#define APIC_VECTOR_TO_REG_OFFSET (v) ((v) / 32 * 0x10) +static inline bool lapic_vector_set_in_irr (unsigned int vector) { -u32 irr =apic_read (APIC_IRR + (vector /32 *0x10 )); +u32 irr =apic_read (APIC_IRR +APIC_VECTOR_TO_REG_OFFSET (vector)); -return !!(irr & (1U << (vector %32 ))); +return !!(irr & (1U <<APIC_VECTOR_TO_BIT_NUMBER (vector))); }static inline bool is_vector_pending (unsigned int vector)
arch/x86/kvm/lapic.h +2 -2
@@ -143,8 +143,8 @@ void kvm_lapic_exit(void);u64 kvm_lapic_readable_reg_mask (struct kvm_lapic *apic); -#define VEC_POS (v) ((v) & (32 - 1)) -#define REG_POS (v) (((v) >> 5) << 4) +#define VEC_POS (v) APIC_VECTOR_TO_BIT_NUMBER(v) +#define REG_POS (v) APIC_VECTOR_TO_REG_OFFSET(v)static inline void kvm_lapic_clear_vector (int vec,void *bitmap) {
Signature · 1 line
2.34.1
RFC PATCH v6 2/32 KVM: x86: Move find_highest_vector() to a common header
In reply to Neeraj UpadhyayIn preparation for using find_highest_vector() in Secure AVIC guest APIC driver, move (and rename) find_highest_vector() to apic.h.
No functional changes intended.
---
Changes since v5:
- Use the same implementation as lapic's find_highest_vector().
arch/x86/include/asm/apic.h | 22 ++++++++++++++++++++++ arch/x86/kvm/lapic.c | 23 +++-------------------- 2 files changed, 25 insertions(+), 20 deletions(-)
arch/x86/include/asm/apic.h +22 -0
@@ -503,6 +503,28 @@ static inline bool is_vector_pending(unsigned int vector)return lapic_vector_set_in_irr (vector) ||pi_pending_this_cpu (vector); } +#define MAX_APIC_VECTOR 256 +#define APIC_VECTORS_PER_REG 32 + +/* +* Vector states are maintained by APIC in 32-bit registers that are +* 16 bytes aligned. The status of each vector is kept in a single +* bit. +*/ +static inline int apic_find_highest_vector (void *bitmap) +{ +u32 *reg; +int vec; + +for (vec =MAX_APIC_VECTOR -APIC_VECTORS_PER_REG ; vec >=0 ; vec -=APIC_VECTORS_PER_REG ) { + reg = bitmap +APIC_VECTOR_TO_REG_OFFSET (vec); +if (*reg) +return __fls (*reg) + vec; + } + +return -1 ; +} +/* * Warm reset vector position: */
arch/x86/kvm/lapic.c +3 -20
@@ -27,6 +27,7 @@#include <linux/export.h> #include <linux/math64.h> #include <linux/slab.h> +#include <asm/apic.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
RFC PATCH v6 3/32 KVM: x86: Move lapic get/set_reg() helpers to common code
In reply to Neeraj UpadhyayMove the __kvm_lapic_get_reg() and __kvm_lapic_set_reg() helper functions to apic.h in order to reuse them in the Secure AVIC guest APIC driver in later patches to read/write 32-bit registers from/to the APIC backing page.
No functional changes intended.
---
Changes since v5:
- New change.
arch/x86/include/asm/apic.h | 10 ++++++++++ arch/x86/kvm/lapic.c | 16 +++++----------- arch/x86/kvm/lapic.h | 9 +++------ 3 files changed, 18 insertions(+), 17 deletions(-)
arch/x86/include/asm/apic.h +10 -0
@@ -525,6 +525,16 @@ static inline int apic_find_highest_vector(void *bitmap)return -1 ; } +static inline u32 apic_get_reg (char *regs,int reg_off) +{ +return *((u32 *) (regs + reg_off)); +} + +static inline void apic_set_reg (char *regs,int reg_off,u32 val) +{ + *((u32 *) (regs + reg_off)) = val; +} +/* * Warm reset vector position: */
arch/x86/kvm/lapic.c +5 -11
@@ -77,14 +77,9 @@ module_param(lapic_timer_advance, bool, 0444);static int kvm_lapic_msr_read (struct kvm_lapic *apic,u32 reg,u64 *data);static int kvm_lapic_msr_write (struct kvm_lapic *apic,u32 reg,u64 data); -static inline void __kvm_lapic_set_reg (char *regs,int reg_off,u32 val) -{ - *((u32 *) (regs + reg_off)) = val; -} -static inline void kvm_lapic_set_reg (struct kvm_lapic *apic,int reg_off,u32 val) { -__kvm_lapic_set_reg (apic->regs, reg_off, val); +apic_set_reg (apic->regs, reg_off, val); } static __always_inline u64 __kvm_lapic_get_reg64(char *regs, int reg)
@@ -3044,12 +3039,12 @@ static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,if (!kvm_x86_ops.x2apic_icr_is_split) {if (set) { - icr =__kvm_lapic_get_reg (s->regs,APIC_ICR ) | - (u64 )__kvm_lapic_get_reg (s->regs,APIC_ICR2 ) <<32 ; + icr =apic_get_reg (s->regs,APIC_ICR ) | + (u64 )apic_get_reg (s->regs,APIC_ICR2 ) <<32 ;__kvm_lapic_set_reg64 (s->regs,APIC_ICR , icr); }else { icr =__kvm_lapic_get_reg64 (s->regs,APIC_ICR ); -__kvm_lapic_set_reg (s->regs,APIC_ICR2 , icr >>32 ); +apic_set_reg (s->regs,APIC_ICR2 , icr >>32 ); } } }
@@ -3065,8 +3060,7 @@ int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)* Get calculated timer current count for remaining timer period (if * any) and store it in the returned register set. */ -__kvm_lapic_set_reg (s->regs,APIC_TMCCT , -__apic_read (vcpu->arch.apic,APIC_TMCCT )); +apic_set_reg (s->regs,APIC_TMCCT ,__apic_read (vcpu->arch.apic,APIC_TMCCT ));return kvm_apic_state_fixup (vcpu, s, false); }
arch/x86/kvm/lapic.h +3 -6
@@ -4,6 +4,8 @@#include <kvm/iodev.h> +#include <asm/apic.h> +#include <linux/kvm_host.h> #include "hyperv.h"
@@ -166,14 +168,9 @@ static inline void kvm_lapic_set_irr(int vec, struct kvm_lapic *apic) apic->irr_pending = true; } -static inline u32 __kvm_lapic_get_reg (char *regs,int reg_off) -{ -return *((u32 *) (regs + reg_off)); -} -static inline u32 kvm_lapic_get_reg (struct kvm_lapic *apic,int reg_off) { -return __kvm_lapic_get_reg (apic->regs, reg_off); +return apic_get_reg (apic->regs, reg_off); }DECLARE_STATIC_KEY_FALSE (kvm_has_noapic_vcpu);
Signature · 1 line
2.34.1
RFC PATCH v6 4/32 KVM: x86: Move lapic get/set_reg64() helpers to common code
In reply to Neeraj UpadhyayMove the __kvm_lapic_get_reg64() and __kvm_lapic_set_reg64() helper functions to apic.h in order to reuse them in the Secure AVIC guest APIC driver in later patches to read/write APIC_ICR from/to the APIC backing page.
No functional changes intended.
---
Changes since v5:
- New change.
arch/x86/include/asm/apic.h | 12 ++++++++++++ arch/x86/kvm/lapic.c | 20 ++++---------------- 2 files changed, 16 insertions(+), 16 deletions(-)
arch/x86/include/asm/apic.h +12 -0
@@ -535,6 +535,18 @@ static inline void apic_set_reg(char *regs, int reg_off, u32 val) *((u32 *) (regs + reg_off)) = val; } +static __always_inline u64 apic_get_reg64(char *regs, int reg) +{ +BUILD_BUG_ON (reg !=APIC_ICR ); +return *((u64 *) (regs + reg)); +} + +static __always_inline void apic_set_reg64(char *regs, int reg, u64 val) +{ +BUILD_BUG_ON (reg !=APIC_ICR ); + *((u64 *) (regs + reg)) = val; +} +/* * Warm reset vector position: */
arch/x86/kvm/lapic.c +4 -16
@@ -82,27 +82,15 @@ static inline void kvm_lapic_set_reg(struct kvm_lapic *apic, int reg_off, u32 vaapic_set_reg (apic->regs, reg_off, val); } -static __always_inline u64 __kvm_lapic_get_reg64(char *regs, int reg) -{ -BUILD_BUG_ON (reg !=APIC_ICR ); -return *((u64 *) (regs + reg)); -} - static __always_inline u64 kvm_lapic_get_reg64(struct kvm_lapic *apic, int reg) { -return __kvm_lapic_get_reg64 (apic->regs, reg); -} - -static __always_inline void __kvm_lapic_set_reg64(char *regs, int reg, u64 val) -{ -BUILD_BUG_ON (reg !=APIC_ICR ); - *((u64 *) (regs + reg)) = val; +return apic_get_reg64 (apic->regs, reg); } static __always_inline void kvm_lapic_set_reg64(struct kvm_lapic *apic,int reg,u64 val) { -__kvm_lapic_set_reg64 (apic->regs, reg, val); +apic_set_reg64 (apic->regs, reg, val); }static inline int apic_test_vector (int vec,void *bitmap)
@@ -3041,9 +3029,9 @@ static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu,if (set) { icr =apic_get_reg (s->regs,APIC_ICR ) | (u64 )apic_get_reg (s->regs,APIC_ICR2 ) <<32 ; -__kvm_lapic_set_reg64 (s->regs,APIC_ICR , icr); +apic_set_reg64 (s->regs,APIC_ICR , icr); }else { - icr =__kvm_lapic_get_reg64 (s->regs,APIC_ICR ); + icr =apic_get_reg64 (s->regs,APIC_ICR );apic_set_reg (s->regs,APIC_ICR2 , icr >>32 ); } }
Signature · 1 line
2.34.1
RFC PATCH v6 5/32 KVM: x86: Move lapic set/clear_vector() helpers to common code
In reply to Neeraj UpadhyayMove the kvm_lapic_clear_vector() and kvm_lapic_set_vector() helper functions to apic.h in order to reuse them in the Secure AVIC guest APIC driver in later patches to atomically set/clear vectors in the APIC backing page.
No functional changes intended.
---
Changes since v5:
- New change.
arch/x86/include/asm/apic.h | 10 ++++++++++ arch/x86/kvm/lapic.c | 10 ++++------ arch/x86/kvm/lapic.h | 12 +----------- 3 files changed, 15 insertions(+), 17 deletions(-)
arch/x86/include/asm/apic.h +10 -0
@@ -547,6 +547,16 @@ static __always_inline void apic_set_reg64(char *regs, int reg, u64 val) *((u64 *) (regs + reg)) = val; } +static inline void apic_clear_vector (int vec,void *bitmap) +{ +clear_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); +} + +static inline void apic_set_vector (int vec,void *bitmap) +{ +set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); +} +/* * Warm reset vector position: */
arch/x86/kvm/lapic.c +4 -6
@@ -693,10 +693,10 @@ static inline int apic_find_highest_irr(struct kvm_lapic *apic)static inline void apic_clear_irr (int vec,struct kvm_lapic *apic) {if (unlikely (apic->apicv_active)) { -kvm_lapic_clear_vector (vec, apic->regs +APIC_IRR ); +apic_clear_vector (vec, apic->regs +APIC_IRR ); }else { apic->irr_pending = false; -kvm_lapic_clear_vector (vec, apic->regs +APIC_IRR ); +apic_clear_vector (vec, apic->regs +APIC_IRR );if (apic_search_irr (apic) !=-1 ) apic->irr_pending = true; }
@@ -1296,11 +1296,9 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,if (apic_test_vector (vector, apic->regs +APIC_TMR ) != !!trig_mode) {if (trig_mode) -kvm_lapic_set_vector (vector, - apic->regs +APIC_TMR ); +apic_set_vector (vector, apic->regs +APIC_TMR );else -kvm_lapic_clear_vector (vector, - apic->regs +APIC_TMR ); +apic_clear_vector (vector, apic->regs +APIC_TMR ); } kvm_x86_call(deliver_interrupt)(apic, delivery_mode,
arch/x86/kvm/lapic.h +1 -11
@@ -148,19 +148,9 @@ u64 kvm_lapic_readable_reg_mask(struct kvm_lapic *apic);#define VEC_POS (v) APIC_VECTOR_TO_BIT_NUMBER(v)#define REG_POS (v) APIC_VECTOR_TO_REG_OFFSET(v) -static inline void kvm_lapic_clear_vector (int vec,void *bitmap) -{ -clear_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); -} - -static inline void kvm_lapic_set_vector (int vec,void *bitmap) -{ -set_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); -} -static inline void kvm_lapic_set_irr (int vec,struct kvm_lapic *apic) { -kvm_lapic_set_vector (vec, apic->regs +APIC_IRR ); +apic_set_vector (vec, apic->regs +APIC_IRR );/* * irr_pending must be true if any interrupt is pending; set it after * APIC_IRR to avoid race with apic_clear_irr
Signature · 1 line
2.34.1
RFC PATCH v6 6/32 KVM: x86: Move {REG,VEC}_POS() macros to lapic.c
In reply to Neeraj UpadhyaySince the {REG,VEC}_POS() macros are no longer used in lapic.h, move them to lapic.c.
No functional changes intended.
---
Changes since v5:
- New change.
arch/x86/kvm/lapic.c | 3 +++ arch/x86/kvm/lapic.h | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/lapic.c +3 -0
@@ -57,6 +57,9 @@#define APIC_VERSION 0x14UL#define LAPIC_MMIO_LENGTH (1 << 12) +#define VEC_POS (v) APIC_VECTOR_TO_BIT_NUMBER(v) +#define REG_POS (v) APIC_VECTOR_TO_REG_OFFSET(v) +/* * Enable local APIC timer advancement (tscdeadline mode only) with adaptive * tuning. When enabled, KVM programs the host timer event to fire early, i.e.
arch/x86/kvm/lapic.h +0 -3
@@ -145,9 +145,6 @@ void kvm_lapic_exit(void);u64 kvm_lapic_readable_reg_mask (struct kvm_lapic *apic); -#define VEC_POS (v) APIC_VECTOR_TO_BIT_NUMBER(v) -#define REG_POS (v) APIC_VECTOR_TO_REG_OFFSET(v) -static inline void kvm_lapic_set_irr (int vec,struct kvm_lapic *apic) {apic_set_vector (vec, apic->regs +APIC_IRR );
Signature · 1 line
2.34.1
RFC PATCH v6 7/32 KVM: x86: apic_test_vector() to common code
In reply to Neeraj UpadhyayMove apic_test_vector() to apic.h in order to reuse it in the Secure AVIC guest APIC driver in later patches to test vector state in the APIC backing page.
No function changes intended.
---
Changes since v5:
- New change.
arch/x86/include/asm/apic.h | 5 +++++ arch/x86/kvm/lapic.c | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/include/asm/apic.h +5 -0
@@ -557,6 +557,11 @@ static inline void apic_set_vector(int vec, void *bitmap)set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); } +static inline int apic_test_vector (int vec,void *bitmap) +{ +return test_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); +} +/* * Warm reset vector position: */
arch/x86/kvm/lapic.c +0 -5
@@ -96,11 +96,6 @@ static __always_inline void kvm_lapic_set_reg64(struct kvm_lapic *apic,apic_set_reg64 (apic->regs, reg, val); } -static inline int apic_test_vector (int vec,void *bitmap) -{ -return test_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); -} -bool kvm_apic_pending_eoi (struct kvm_vcpu *vcpu,int vector) {struct kvm_lapic *apic = vcpu->arch.apic;
Signature · 1 line
2.34.1
Borislav Petkov
maintainerRe: [RFC PATCH v6 07/32] KVM: x86: apic_test_vector() to common code
Quoted 39 lines · Neeraj Upadhyay
Move
apic_test_vector()toapic.hin order to reuse it in the Secure AVIC guest APIC driver in later patches to test vector state in the APIC backing page.No function changes intended.
Signed-off-by Neeraj Upadhyay <[email protected]>---
Changes since v5:- New change.
arch/x86/include/asm/apic.h | 5 +++++ arch/x86/kvm/lapic.c | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-)arch/x86/include/asm/apic.h +5
-0@@ -557,6 +557,11 @@ static inline void apic_set_vector(int vec, void *bitmap)set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); } +static inline int apic_test_vector (int vec,void *bitmap) +{ +return test_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); +} +/* * Warm reset vector position: */ arch/x86/kvm/lapic.c +0
-0@@ -96,11 +96,6 @@ static __always_inline void kvm_lapic_set_reg64(struct kvm_lapic *apic,apic_set_reg64 (apic->regs, reg, val);
arch/x86/kvm/lapic.c static __always_inline void kvm_lapic_set_reg64(struct kvm_lapic *apic, · line 101
Quoted 10 lines
arch/x86/kvm/lapic.c +0
-5} -static inline int apic_test_vector (int vec,void *bitmap) -{ -return test_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); -} -bool kvm_apic_pending_eoi (struct kvm_vcpu *vcpu,int vector) {struct kvm_lapic *apic = vcpu->arch.apic;
The previous patch is moving those *_POS() macros to arch/x86/kvm/lapic.c, now this patch is doing rename-during-move to the new macros.
Why can't you simply do the purely mechanical moves first and then do the renames? Didn't I explain it the last time? Or is it still unclear?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC PATCH v6 07/32] KVM: x86: apic_test_vector() to common code
Quoted 7 lines · Borislav Petkov
The previous patch is moving those
*_POS()macros toarch/x86/kvm/lapic.c, now this patch is doing rename-during-move to the new macros.Why can't you simply do the purely mechanical moves first and then do the renames? Didn't I explain it the last time? Or is it still unclear?
I thought it was clear to me when you explained last time. However, I did this rename-during-move because of below reason. Please correct me if I am wrong here.
VEC_POS, REG_POS are kvm-internal wrappers for APIC_VECTOR_TO_BIT_NUMBER/ APIC_VECTOR_TO_REG_OFFSET macros which got defined in patch 01/32. Prior to patch 06/32, these macros were defined in kvm-internal header arch/x86/kvm/lapic.h. Using VEC_POS, REG_POS kvm-internal macros in x86 common header file (arch/x86/include/asm/apic.h) in this patch did not look correct to me and as APIC_VECTOR_TO_BIT_NUMBER/APIC_VECTOR_TO_REG_OFFSET are already defined in arch/x86/include/asm/apic.h, I used them.
Is adding this information in commit log of this patch sufficient or do you have some other suggestion for doing this?
- Neeraj
Sean Christopherson
maintainerRe: [RFC PATCH v6 07/32] KVM: x86: apic_test_vector() to common code
Quoted 21 lines · Neeraj Upadhyay
Quoted 7 lines · Borislav Petkov
The previous patch is moving those
*_POS()macros toarch/x86/kvm/lapic.c, now this patch is doing rename-during-move to the new macros.Why can't you simply do the purely mechanical moves first and then do the renames? Didn't I explain it the last time? Or is it still unclear?
I thought it was clear to me when you explained last time. However, I did this rename-during-move because of below reason. Please correct me if I am wrong here.
VEC_POS,REG_POSare kvm-internal wrappers for APIC_VECTOR_TO_BIT_NUMBER/APIC_VECTOR_TO_REG_OFFSETmacros which got defined in patch 01/32. Prior to patch 06/32, these macros were defined in kvm-internal headerarch/x86/kvm/lapic.h. UsingVEC_POS,REG_POSkvm-internal macros in x86 common header file (arch/x86/include/asm/apic.h) in this patch did not look correct to me and as APIC_VECTOR_TO_BIT_NUMBER/APIC_VECTOR_TO_REG_OFFSET are already defined inarch/x86/include/asm/apic.h, I used them.
Quoted 2 lines
Is adding this information in commit log of this patch sufficient or do you have some other suggestion for doing this?
I agree that moving VEC_POS/REG_POS to common code would be weird/undesirable, but I also agree with Boris' underlying point that doing renames as part of code movement is also undesirable. And you're doing that all over this series.
So, just one patch at the beginning of the series to replace VEC_POS/REG_POS with APIC_VECTOR_TO_BIT_NUMBER/APIC_VECTOR_TO_REG_OFFSET, but *only* in the functions you intended to move out of KVM. That way you separate code movement and rename patches.
Actually, looking at the end usage, just drop VEC_POS/REG_POS entirely. IIRC, I suggested keeping the shorthand versions for KVM, but I didn't realize there would literally be two helpers left. At that point, keeping VEC_POS and REG_POS is pure stubborness :-)
1. Rename VEC_POS/REG_POS => APIC_VECTOR_TO_BIT_NUMBER/APIC_VECTOR_TO_REG_OFFSET
2. Rename all of the KVM helpers you intend to move out of KVM.
3. Move all of the helpers out of KVM.
That way #1 and #2 are pure KVM changes, and the code review movement is easy to review because it'll be _just_ code movement.
Actually (redux), we should probably kill off __apic_test_and_set_vector() and __apic_test_and_clear_vector(), because the _only_ register that's safe to modify with a non-atomic operation is ISR, because KVM isn't running the vCPU, i.e. hardware can't service an IRQ or process an EOI for the relevant (virtual) APIC.
So this on top somewhere? (completely untested)
arch/x86/kvm/lapic.c +9 -12
@@ -104,16 +104,6 @@ bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)apic_test_vector (vector, apic->regs +APIC_IRR ); } -static inline int __apic_test_and_set_vector (int vec,void *bitmap) -{ -return __test_and_set_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); -} - -static inline int __apic_test_and_clear_vector (int vec,void *bitmap) -{ -return __test_and_clear_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); -} -__read_mostly DEFINE_STATIC_KEY_FALSE (kvm_has_noapic_vcpu);EXPORT_SYMBOL_GPL (kvm_has_noapic_vcpu);
@@ -706,9 +696,15 @@ void kvm_apic_clear_irr(struct kvm_vcpu *vcpu, int vec) }EXPORT_SYMBOL_GPL (kvm_apic_clear_irr); +static void *apic_vector_to_isr (int vec,struct kvm_lapic *apic) +{ +return apic->regs +APIC_ISR +APIC_VECTOR_TO_REG_OFFSET (vec); +} +static inline void apic_set_isr (int vec,struct kvm_lapic *apic) { -if (__apic_test_and_set_vector (vec, apic->regs +APIC_ISR )) +if (__test_and_set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), +apic_vector_to_isr (vec, apic)))return ;/*
@@ -751,7 +747,8 @@ static inline int apic_find_highest_isr(struct kvm_lapic *apic)static inline void apic_clear_isr (int vec,struct kvm_lapic *apic) { -if (!__apic_test_and_clear_vector (vec, apic->regs +APIC_ISR )) +if (!__test_and_clear_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), +apic_vector_to_isr (vec, apic)))return ;/*
Sean Christopherson
maintainerRe: [RFC PATCH v6 07/32] KVM: x86: apic_test_vector() to common code
Quoted 7 lines · Sean Christopherson
Actually, looking at the end usage, just drop VEC_POS/REG_POS entirely. IIRC, I suggested keeping the shorthand versions for KVM, but I didn't realize there would literally be two helpers left. At that point, keeping
VEC_POSandREG_POSis pure stubborness :-)1. Rename VEC_POS/REG_POS => APIC_VECTOR_TO_BIT_NUMBER/APIC_VECTOR_TO_REG_OFFSET
2. Rename all of the KVM helpers you intend to move out of KVM.
Looking at the earlier patches again, I vote to add a 4th:
2a. Replace all "char *" with "void *" in all affected helpers.
Pointer arithmetic for "void *" and "char *" operate identically, and AFAICT that's the only reason why e.g. __kvm_lapic_set_reg64() takes a "char *". That way there is even less of a chance of doing the wrong thing, e.g. neglecting to cast and reading a byte instead of the desired size.
Quoted 1 line
3. Move all of the helpers out of KVM.
Re: [RFC PATCH v6 07/32] KVM: x86: apic_test_vector() to common code
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Sean Christopherson
Actually, looking at the end usage, just drop VEC_POS/REG_POS entirely. IIRC, I suggested keeping the shorthand versions for KVM, but I didn't realize there would literally be two helpers left. At that point, keeping
VEC_POSandREG_POSis pure stubborness :-)1. Rename VEC_POS/REG_POS => APIC_VECTOR_TO_BIT_NUMBER/APIC_VECTOR_TO_REG_OFFSET
2. Rename all of the KVM helpers you intend to move out of KVM.
Quoted 9 lines
Looking at the earlier patches again, I vote to add a 4th:
2a. Replace all "char *" with "void *" in all affected helpers.
Pointer arithmetic for "void *" and "char *" operate identically, and AFAICT that's the only reason why e.g.
__kvm_lapic_set_reg64()takes a "char *". That way there is even less of a chance of doing the wrong thing, e.g. neglecting to cast and reading a byte instead of the desired size.
Sounds good. Will include this in the next version.
- Neeraj
Quoted 1 line
Quoted 1 line
3. Move all of the helpers out of KVM.
Re: [RFC PATCH v6 07/32] KVM: x86: apic_test_vector() to common code
In reply to Sean ChristophersonOn 6/7/2025 12:17 AM, Sean Christopherson wrote:
...
Quoted 8 lines
1. Rename VEC_POS/REG_POS => APIC_VECTOR_TO_BIT_NUMBER/APIC_VECTOR_TO_REG_OFFSET
2. Rename all of the KVM helpers you intend to move out of KVM.
3. Move all of the helpers out of KVM.That way #1 and #2 are pure KVM changes, and the code review movement is easy to review because it'll be _just_ code movement.
Thanks for providing guidance on patch structuring! I am working on this.
Quoted 7 lines
Actually (redux), we should probably kill off
__apic_test_and_set_vector()and__apic_test_and_clear_vector(), because the _only_ register that's safe to modify with a non-atomic operation is ISR, because KVM isn't running the vCPU, i.e. hardware can't service an IRQ or process an EOI for the relevant (virtual) APIC.So this on top somewhere? (completely untested)
Ok, makes sense. I will include this.
- Neeraj
Quoted 49 lines
arch/x86/kvm/lapic.c +9
-12@@ -104,16 +104,6 @@ bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)apic_test_vector (vector, apic->regs +APIC_IRR ); } -static inline int __apic_test_and_set_vector (int vec,void *bitmap) -{ -return __test_and_set_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); -} - -static inline int __apic_test_and_clear_vector (int vec,void *bitmap) -{ -return __test_and_clear_bit (VEC_POS (vec), (bitmap) +REG_POS (vec)); -} -__read_mostly DEFINE_STATIC_KEY_FALSE (kvm_has_noapic_vcpu);EXPORT_SYMBOL_GPL (kvm_has_noapic_vcpu);@@ -706,9 +696,15 @@ void kvm_apic_clear_irr(struct kvm_vcpu *vcpu, int vec) }EXPORT_SYMBOL_GPL (kvm_apic_clear_irr); +static void *apic_vector_to_isr (int vec,struct kvm_lapic *apic) +{ +return apic->regs +APIC_ISR +APIC_VECTOR_TO_REG_OFFSET (vec); +} +static inline void apic_set_isr (int vec,struct kvm_lapic *apic) { -if (__apic_test_and_set_vector (vec, apic->regs +APIC_ISR )) +if (__test_and_set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), +apic_vector_to_isr (vec, apic)))return ;/* @@ -751,7 +747,8 @@ static inline int apic_find_highest_isr(struct kvm_lapic *apic)static inline void apic_clear_isr (int vec,struct kvm_lapic *apic) { -if (!__apic_test_and_clear_vector (vec, apic->regs +APIC_ISR )) +if (!__test_and_clear_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), +apic_vector_to_isr (vec, apic)))return ;/*
RFC PATCH v6 8/32 x86/apic: Remove redundant parentheses around 'bitmap'
In reply to Neeraj UpadhyayWhen doing pointer arithmetic in apic_{clear|set|test}_vector(), remove the unnecessary parentheses surrounding the 'bitmap' parameter.
No functional changes intended.
---
Changes since v5:
- New change.
arch/x86/include/asm/apic.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/asm/apic.h +3 -3
@@ -549,17 +549,17 @@ static __always_inline void apic_set_reg64(char *regs, int reg, u64 val)static inline void apic_clear_vector (int vec,void *bitmap) { -clear_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); +clear_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); }static inline void apic_set_vector (int vec,void *bitmap) { -set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); +set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); }static inline int apic_test_vector (int vec,void *bitmap) { -return test_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); +return test_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); }/*
Signature · 1 line
2.34.1
Borislav Petkov
maintainerRe: [RFC PATCH v6 08/32] x86/apic: Remove redundant parentheses around 'bitmap'
Quoted 31 lines · Neeraj Upadhyay
When doing pointer arithmetic in apic_{clear|set|test}_vector(), remove the unnecessary parentheses surrounding the 'bitmap' parameter.
No functional changes intended.
Signed-off-by Neeraj Upadhyay <[email protected]>---
Changes since v5:- New change.
arch/x86/include/asm/apic.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)arch/x86/include/asm/apic.h +1
-2@@ -549,17 +549,17 @@ static __always_inline void apic_set_reg64(char *regs, int reg, u64 val)static inline void apic_clear_vector (int vec,void *bitmap) { -clear_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); +clear_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); }static inline void apic_set_vector (int vec,void *bitmap) { -set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec));
arch/x86/include/asm/apic.h static __always_inline void apic_set_reg64(char *regs, int reg, u64 val) · line 565
Quoted 10 lines
arch/x86/include/asm/apic.h +2
-1+set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); }static inline int apic_test_vector (int vec,void *bitmap) { -return test_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), (bitmap) +APIC_VECTOR_TO_REG_OFFSET (vec)); +return test_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); }/*
This change needs to go first so that it gets picked up first since it is a cleanup.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC PATCH v6 08/32] x86/apic: Remove redundant parentheses around 'bitmap'
Quoted 8 lines
Quoted 4 lines
Signature · 1 line
--This change needs to go first so that it gets picked up first since it is a cleanup.
Ok will restructure it in the series.
- Neeraj
RFC PATCH v6 9/32 x86/apic: Rename 'reg_off' to 'reg'
In reply to Neeraj UpadhyayRename the 'reg_off' parameter of apic_{set|get}_reg() to 'reg' to match other usages in apic.h.
No functional changes intended.
---
Changes since v5:
- New change.
arch/x86/include/asm/apic.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/apic.h +4 -4
@@ -525,14 +525,14 @@ static inline int apic_find_highest_vector(void *bitmap)return -1 ; } -static inline u32 apic_get_reg (char *regs,int reg_off) +static inline u32 apic_get_reg (char *regs,int reg) { -return *((u32 *) (regs + reg_off)); +return *((u32 *) (regs + reg)); } -static inline void apic_set_reg (char *regs,int reg_off,u32 val) +static inline void apic_set_reg (char *regs,int reg,u32 val) { - *((u32 *) (regs + reg_off)) = val; + *((u32 *) (regs + reg)) = val; } static __always_inline u64 apic_get_reg64(char *regs, int reg)
Signature · 1 line
2.34.1
Re: [RFC PATCH v6 09/32] x86/apic: Rename 'reg_off' to 'reg'
Quoted 8 lines · Neeraj Upadhyay
Rename the '
reg_off' parameter of apic_{set|get}_reg() to 'reg' to match other usages inapic.h.No functional changes intended.
Signed-off-by Neeraj Upadhyay <[email protected]>---
Signature · 2 lines
Thanks Tianyu Lan
RFC PATCH v6 10/32 x86/apic: Change apic_*_vector() vector param to unsigned
In reply to Neeraj UpadhyayChange vector parameter of apic_{set|clear|test}_vector to unsigned int to optimize code generation for modulo operation.
On gcc-14.2, code generation for below C statement is given after it.
long nr = APIC_VECTOR_TO_BIT_NUMBER(vec);
* Without change:
mov eax,edi sar eax,0x1f shr eax,0x1b add edi,eax and edi,0x1f sub edi,eax
movsxd rdi,edi
mov QWORD PTR [rsp-0x8],rdi
* With change:
and edi,0x1f
mov QWORD PTR [rsp-0x8],rdi
---
Changes since v5:
- New change.
arch/x86/include/asm/apic.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/asm/apic.h +3 -3
@@ -547,17 +547,17 @@ static __always_inline void apic_set_reg64(char *regs, int reg, u64 val) *((u64 *) (regs + reg)) = val; } -static inline void apic_clear_vector (int vec,void *bitmap) +static inline void apic_clear_vector (unsigned int vec,void *bitmap) {clear_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); } -static inline void apic_set_vector (int vec,void *bitmap) +static inline void apic_set_vector (unsigned int vec,void *bitmap) {set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); } -static inline int apic_test_vector (int vec,void *bitmap) +static inline int apic_test_vector (unsigned int vec,void *bitmap) {return test_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); }
Signature · 1 line
2.34.1
Borislav Petkov
maintainerRe: [RFC PATCH v6 10/32] x86/apic: Change apic_*_vector() vector param to unsigned
Quoted 16 lines · Neeraj Upadhyay
Change vector parameter of apic_{set|clear|test}_vector to unsigned int to optimize code generation for modulo operation.
On gcc-14.2, code generation for below C statement is given after it.
long nr =
APIC_VECTOR_TO_BIT_NUMBER(vec);* Without change:
mov eax,edi sar eax,0x1f shr eax,0x1b add edi,eax and edi,0x1f sub edi,eax
Quoted 7 lines
movsxd rdi,edi
mov QWORD PTR [rsp-0x8],rdi* With change:
and edi,0x1f
mov QWORD PTR [rsp-0x8],rdi
AT&T assembly please.
This change needs to go first too.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [RFC PATCH v6 10/32] x86/apic: Change apic_*_vector() vector param to unsigned
Quoted 7 lines · Borislav Petkov
Quoted 4 lines
* With change:
and edi,0x1f
mov QWORD PTR [rsp-0x8],rdiAT&T assembly please.
Ok got it.
Quoted 2 lines
This change needs to go first too.
Will update.
- Neeraj
RFC PATCH v6 11/32 x86/apic: Change get/set reg operations reg param to unsigned
In reply to Neeraj UpadhyayChange reg parameter of apic_{set|get}_*() to unsigned int to optimize code generation.
On gcc-14.2, code generation for apic_get_reg() is show below.
* Without change:
apic_get_reg:
48 63 f6 movsxd rsi,esi 8b 04 37 mov eax,DWORD PTR [rdi+rsi*1] c3 ret
* With change:
89 f6 mov esi,esi 8b 04 37 mov eax,DWORD PTR [rdi+rsi*1] c3 ret
---
Changes since v5:
- New change.
arch/x86/include/asm/apic.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/apic.h +4 -4
@@ -525,23 +525,23 @@ static inline int apic_find_highest_vector(void *bitmap)return -1 ; } -static inline u32 apic_get_reg (char *regs,int reg) +static inline u32 apic_get_reg (char *regs,unsigned int reg) {return *((u32 *) (regs + reg)); } -static inline void apic_set_reg (char *regs,int reg,u32 val) +static inline void apic_set_reg (char *regs,unsigned int reg,u32 val) { *((u32 *) (regs + reg)) = val; } -static __always_inline u64 apic_get_reg64(char *regs, int reg) +static __always_inline u64 apic_get_reg64(char *regs, unsigned int reg) {BUILD_BUG_ON (reg !=APIC_ICR );return *((u64 *) (regs + reg)); } -static __always_inline void apic_set_reg64(char *regs, int reg, u64 val) +static __always_inline void apic_set_reg64(char *regs, unsigned int reg, u64 val) {BUILD_BUG_ON (reg !=APIC_ICR ); *((u64 *) (regs + reg)) = val;
Signature · 1 line
2.34.1
RFC PATCH v6 12/32 x86/apic: Unionize apic regs for 32bit/64bit access w/o type casting
In reply to Neeraj UpadhyayDefine apic_page construct to unionize apic register 32bit/64bit accesses and use it in apic_{get|set}*() to avoid using type casting. Also, change 'regs' parameter to void pointer for automatic pointer type conversion.
One caveat of this change is that the generated code is slighly larger. Below is the code generation for apic_get_reg() using gcc-14.2:
* Without change:
apic_get_reg:
89 f6 mov esi,esi 8b 04 37 mov eax,DWORD PTR [rdi+rsi*1] c3 ret
* With change:
apic_get_reg:
c1 ee 02 shr esi,0x2 8b 04 b7 mov eax,DWORD PTR [rdi+rsi*4] c3 ret
---
Changes since v5:
- New change which is refactored out from v5 to apic.h.
arch/x86/include/asm/apic.h | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-)
arch/x86/include/asm/apic.h +25 -8
@@ -525,26 +525,43 @@ static inline int apic_find_highest_vector(void *bitmap)return -1 ; } -static inline u32 apic_get_reg (char *regs,unsigned int reg) +struct apic_page { +union { +u64 regs64[PAGE_SIZE /8 ]; +u32 regs[PAGE_SIZE /4 ]; +u8 bytes[PAGE_SIZE ]; + }; +}__aligned(PAGE_SIZE) ; + +static inline u32 apic_get_reg (void *regs,unsigned int reg) { -return *((u32 *) (regs + reg)); +struct apic_page *ap = regs; + +return ap->regs[reg /4 ]; } -static inline void apic_set_reg (char *regs,unsigned int reg,u32 val) +static inline void apic_set_reg (void *regs,unsigned int reg,u32 val) { - *((u32 *) (regs + reg)) = val; +struct apic_page *ap = regs; + + ap->regs[reg /4 ] = val; } -static __always_inline u64 apic_get_reg64(char *regs, unsigned int reg) +static __always_inline u64 apic_get_reg64(void *regs, unsigned int reg) { +struct apic_page *ap = regs; +BUILD_BUG_ON (reg !=APIC_ICR ); -return *((u64 *) (regs + reg)); + +return ap->regs64[reg /8 ]; } -static __always_inline void apic_set_reg64(char *regs, unsigned int reg, u64 val) +static __always_inline void apic_set_reg64(void *regs, unsigned int reg, u64 val) { +struct apic_page *ap = regs; +BUILD_BUG_ON (reg !=APIC_ICR ); - *((u64 *) (regs + reg)) = val; + ap->regs64[reg /8 ] = val; }static inline void apic_clear_vector (unsigned int vec,void *bitmap)
Signature · 1 line
2.34.1
RFC PATCH v6 13/32 x86/apic: Simplify bitwise operations on apic bitmap
In reply to Neeraj UpadhyayUse 'regs' as a contiguous linear bitmap in apic_{set| clear|test}_vector() while doing bitwise operations. This makes the code simpler by eliminating the need to determine the offset of the 32-bit register and the vector bit location within that register prior to performing bitwise operations.
This change results in slight increase in generated code size for gcc-14.2.
* Without change
apic_set_vector:
89 f8 mov eax,edi 83 e7 1f and edi,0x1f c1 e8 05 shr eax,0x5 c1 e0 04 shl eax,0x4 48 01 c6 add rsi,rax f0 48 0f ab 3e lock bts QWORD PTR [rsi],rdi c3 ret
* With change
apic_set_vector:
89 f8 mov eax,edi c1 e8 05 shr eax,0x5 8d 04 40 lea eax,[rax+rax*2] c1 e0 05 shl eax,0x5 01 f8 add eax,edi 89 c0 mov eax,eax f0 48 0f ab 3e lock bts QWORD PTR [rsi],rax
---
Changes since v5:
- New change which is refactored from v5 into common code (apic.h).
arch/x86/include/asm/apic.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
arch/x86/include/asm/apic.h +12 -3
@@ -564,19 +564,28 @@ static __always_inline void apic_set_reg64(void *regs, unsigned int reg, u64 val ap->regs64[reg /8 ] = val; } +static inline unsigned int get_vec_bit (unsigned int vec) +{ +/* +* The registers are 32-bit wide and 16-byte aligned. +* Compensate for the resulting bit number spacing. +*/ +return vec +96 * (vec /32 ); +} +static inline void apic_clear_vector (unsigned int vec,void *bitmap) { -clear_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); +clear_bit (get_vec_bit (vec), bitmap); }static inline void apic_set_vector (unsigned int vec,void *bitmap) { -set_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); +set_bit (get_vec_bit (vec), bitmap); }static inline int apic_test_vector (unsigned int vec,void *bitmap) { -return test_bit (APIC_VECTOR_TO_BIT_NUMBER (vec), bitmap +APIC_VECTOR_TO_REG_OFFSET (vec)); +return test_bit (get_vec_bit (vec), bitmap); }/*
Signature · 1 line
2.34.1
RFC PATCH v6 14/32 x86/apic: Move apic_update_irq_cfg() calls to apic_update_vector()
In reply to Neeraj UpadhyayAll callers of apic_update_vector() also call apic_update_irq_cfg() after it. So, simplify the code by moving all such apic_update_irq_cfg() calls to apic_update_vector().
No functional change intended.
---
Changes since v5:
- Commit log updates.
arch/x86/kernel/apic/vector.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/apic/vector.c +2 -2
@@ -183,6 +183,7 @@ static void apic_update_vector(struct irq_data *irqd, unsigned int newvec, apicd->cpu = newcpu;BUG_ON (!IS_ERR_OR_NULL (per_cpu (vector_irq, newcpu)[newvec]));per_cpu (vector_irq, newcpu)[newvec] = desc; +apic_update_irq_cfg (irqd, newvec, newcpu); }static void vector_assign_managed_shutdown (struct irq_data *irqd)
@@ -261,7 +262,6 @@ assign_vector_locked(struct irq_data *irqd, const struct cpumask *dest)if (vector <0 )return vector;apic_update_vector (irqd, vector, cpu); -apic_update_irq_cfg (irqd, vector, cpu);return 0 ; }
@@ -338,7 +338,7 @@ assign_managed_vector(struct irq_data *irqd, const struct cpumask *dest)if (vector <0 )return vector;apic_update_vector (irqd, vector, cpu); -apic_update_irq_cfg (irqd, vector, cpu); +return 0 ; }
Signature · 1 line
2.34.1
RFC PATCH v6 15/32 x86/apic: Add new driver for Secure AVIC
In reply to Neeraj UpadhyayThe 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 (without IPI callbacks), but will be modified as features of Secure AVIC 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.
---
Changes since v5:
- Add "SecureAVIC" to sev_status_feat_names.
arch/x86/Kconfig | 13 ++++++ arch/x86/boot/compressed/sev.c | 1 + arch/x86/coco/core.c | 3 ++ arch/x86/coco/sev/core.c | 1 + arch/x86/include/asm/msr-index.h | 4 +- arch/x86/kernel/apic/Makefile | 1 + arch/x86/kernel/apic/x2apic_savic.c | 63 +++++++++++++++++++++++++++++ include/linux/cc_platform.h | 8 ++++ 8 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/apic/x2apic_savic.c
arch/x86/Kconfig +13 -0
@@ -486,6 +486,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
@@ -235,6 +235,7 @@ bool sev_es_check_ghcb_fault(unsigned long address)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
@@ -104,6 +104,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/coco/sev/core.c +1 -0
@@ -79,6 +79,7 @@ static const char * const sev_status_feat_names[] = { [MSR_AMD64_SNP_IBS_VIRT_BIT] = "IBSVirt", [MSR_AMD64_SNP_VMSA_REG_PROT_BIT] = "VMSARegProt", [MSR_AMD64_SNP_SMT_PROT_BIT] = "SMTProt", + [MSR_AMD64_SNP_SECURE_AVIC_BIT] = "SecureAVIC", };/*
arch/x86/include/asm/msr-index.h +3 -1
@@ -689,7 +689,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
@@ -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 +63 -0
63 lines not on this page · raw message
include/linux/cc_platform.h +8 -0
@@ -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
Re: [RFC PATCH v6 15/32] x86/apic: Add new driver for Secure AVIC
Quoted 13 lines · Neeraj Upadhyay
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 (without IPI callbacks), but will be modified as features of Secure AVIC are implemented.
Quoted 8 lines
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.Co-developed-by Kishon Vijay Abraham I <[email protected]>Signed-off-by Kishon Vijay Abraham I <[email protected]>Signed-off-by Neeraj Upadhyay <[email protected]>---
--
Thanks
Tianyu Lan
RFC PATCH v6 16/32 x86/apic: Initialize Secure AVIC APIC backing page
In reply to Neeraj UpadhyayWith 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. To handle this, 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 v5:
- Minor refactoring to remove the struct apic_page definition which is moved to asm/apic.h in previous patch in the series.
arch/x86/coco/sev/core.c | 22 ++++++++++++++++++++ arch/x86/include/asm/apic.h | 1 + arch/x86/include/asm/sev.h | 2 ++ arch/x86/include/uapi/asm/svm.h | 4 ++++ arch/x86/kernel/apic/apic.c | 3 +++ arch/x86/kernel/apic/x2apic_savic.c | 32 +++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+)
arch/x86/coco/sev/core.c +22 -0
@@ -1006,6 +1006,28 @@ int __init sev_es_efi_map_ghcbs(pgd_t *pgd)return 0 ; } +enum es_result savic_register_gpa (u64 gpa) +{ +struct ghcb_state state; +struct es_em_ctxt ctxt; +enum es_result res; +struct ghcb *ghcb; + +guard (irqsave)(); + + ghcb =__sev_get_ghcb (&state); +vc_ghcb_invalidate (ghcb); + +ghcb_set_rax (ghcb,SVM_VMGEXIT_SAVIC_SELF_GPA ); +ghcb_set_rbx (ghcb, gpa); + res =sev_es_ghcb_hv_call (ghcb, &ctxt,SVM_VMGEXIT_SAVIC , +SVM_VMGEXIT_SAVIC_REGISTER_GPA ,0 ); + +__sev_put_ghcb (&state); + +return res; +} +static void snp_register_per_cpu_ghcb (void ) {struct sev_es_runtime_data *data;
arch/x86/include/asm/apic.h +1 -0
@@ -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
@@ -520,6 +520,7 @@ int snp_svsm_vtpm_send_command(u8 *buffer);void __init snp_secure_tsc_prepare (void );void __init snp_secure_tsc_init (void ); +enum es_result savic_register_gpa (u64 gpa); static __always_inline void vc_ghcb_invalidate(struct ghcb *ghcb) {
@@ -592,6 +593,7 @@ static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_static inline int snp_svsm_vtpm_send_command (u8 *buffer) {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 +4 -0
@@ -117,6 +117,10 @@#define SVM_VMGEXIT_AP_CREATE 1#define SVM_VMGEXIT_AP_DESTROY 2#define SVM_VMGEXIT_SNP_RUN_VMPL 0x80000018 +#define SVM_VMGEXIT_SAVIC 0x8000001a +#define SVM_VMGEXIT_SAVIC_REGISTER_GPA 0 +#define SVM_VMGEXIT_SAVIC_UNREGISTER_GPA 1 +#define SVM_VMGEXIT_SAVIC_SELF_GPA ~0ULL#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 +3 -0
@@ -1503,6 +1503,9 @@ 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 +32 -0
@@ -8,17 +8,44 @@*/ #include <linux/cc_platform.h> +#include <linux/percpu-defs.h> #include <asm/apic.h> #include <asm/sev.h> #include "local.h" +static struct apic_page __percpu *apic_page__ro_after_init ; +static int savic_acpi_madt_oem_check (char *oem_id,char *oem_table_id) {return x2apic_enabled () &&cc_platform_has (CC_ATTR_SNP_SECURE_AVIC ); } +static void savic_setup (void ) +{ +void *backing_page; +enum es_result res; +unsigned long gpa; + + backing_page =this_cpu_ptr (apic_page); + gpa =__pa (backing_page); + +/* +* 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 calling savic_register_gpa(). Before executing +* VMRUN, the hypervisor makes use of this information to make sure +* the APIC backing page is mapped in NPT. +*/ + res =savic_register_gpa (gpa); +if (res !=ES_OK ) +snp_abort (); +} +static int savic_probe (void ) {if (!cc_platform_has (CC_ATTR_SNP_SECURE_AVIC ))
@@ -30,6 +57,10 @@ static int savic_probe(void)/* unreachable */ } + apic_page = alloc_percpu(struct apic_page); +if (!apic_page) +snp_abort (); +return 1 ; }
@@ -38,6 +69,7 @@ static struct apic apic_x2apic_savic __ro_after_init = { .name = "secure avic x2apic", .probe = savic_probe, .acpi_madt_oem_check = savic_acpi_madt_oem_check, + .setup = savic_setup, .dest_mode_logical = false,
Signature · 1 line
2.34.1
RFC PATCH v6 17/32 x86/apic: Populate .read()/.write() callbacks of Secure AVIC driver
In reply to Neeraj UpadhyayAdd 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 v5:
- Use common helpers apic_get_reg(), apic_set_reg(), apic_get_reg64(), apic_set_reg64().
- Fix APIC_ICR handling in savic_read() and savic_write().
arch/x86/include/asm/apicdef.h | 2 + arch/x86/kernel/apic/x2apic_savic.c | 113 +++++++++++++++++++++++++++- 2 files changed, 113 insertions(+), 2 deletions(-)
arch/x86/include/asm/apicdef.h +2 -0
@@ -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 +111 -2
131 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH v6 18/32 x86/apic: Initialize APIC ID for Secure AVIC
In reply to Neeraj UpadhyayInitialize 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 v5:
- Use common apic_set_reg().
arch/x86/kernel/apic/x2apic_savic.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
arch/x86/kernel/apic/x2apic_savic.c +13 -0
@@ -131,6 +131,18 @@ static void savic_write(u32 reg, u32 data) } } +static void init_apic_page (struct apic_page *ap) +{ +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 ); +apic_set_reg (ap,APIC_ID , apic_id); +} +static void savic_setup (void ) {void *backing_page;
@@ -138,6 +150,7 @@ static void savic_setup(void)unsigned long gpa; backing_page =this_cpu_ptr (apic_page); +init_apic_page (backing_page); gpa =__pa (backing_page);/*
Signature · 1 line
2.34.1
RFC PATCH v6 19/32 x86/apic: Add update_vector() callback for apic drivers
In reply to Neeraj UpadhyayAdd an update_vector() callback to allow apic drivers to perform driver specific operations on external vector allocation/teardown on a CPU. This callback will be used in subsequent commits by Secure AVIC apic driver to configure the vectors which a guest vCPU allows the hypervisor to send to it.
As system vectors have fixed vector assignments and are not dynamically allocated, add apic_update_vector() public api to facilitate update_vector() callback invocation for them. This will be used for Secure AVIC enabled guests to allow the hypervisor to inject system vectors which are emulated by the hypervisor such as APIC timer vector and HYPERVISOR_CALLBACK_VECTOR.
While at it, cleanup line break in apic_update_irq_cfg().
---
Changes since v5:
- No change.
arch/x86/include/asm/apic.h | 9 +++++++++ arch/x86/kernel/apic/vector.c | 29 ++++++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-)
arch/x86/include/asm/apic.h +9 -0
@@ -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; };
@@ -471,6 +473,12 @@ 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) +{ +if (apic->update_vector) + apic->update_vector (cpu, vector, set); +} +#else /* CONFIG_X86_LOCAL_APIC */ static inline u32 apic_read (u32 reg) {return 0 ; }
@@ -482,6 +490,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/kernel/apic/vector.c +18 -11
@@ -134,13 +134,21 @@ static void apic_update_irq_cfg(struct irq_data *irqd, unsigned int vector, apicd->hw_irq_cfg.vector = vector; apicd->hw_irq_cfg.dest_apicid = apic->calc_dest_apicid (cpu); + +apic_update_vector (cpu, vector, true); +irq_data_update_effective_affinity (irqd,cpumask_of (cpu)); -trace_vector_config (irqd->irq, vector, cpu, - apicd->hw_irq_cfg.dest_apicid); +trace_vector_config (irqd->irq, vector, cpu, apicd->hw_irq_cfg.dest_apicid); } -static void apic_update_vector (struct irq_data *irqd,unsigned int newvec, -unsigned int newcpu) +static void apic_free_vector (unsigned int cpu,unsigned int vector,bool managed) +{ +apic_update_vector (cpu, vector, false); +irq_matrix_free (vector_matrix, cpu, vector, managed); +} + +static void apic_chipd_update_vector (struct irq_data *irqd,unsigned int newvec, +unsigned int newcpu) {struct apic_chip_data *apicd =apic_chip_data (irqd);struct irq_desc *desc =irq_data_to_desc (irqd);
@@ -174,8 +182,7 @@ static void apic_update_vector(struct irq_data *irqd, unsigned int newvec, apicd->prev_cpu = apicd->cpu;WARN_ON_ONCE (apicd->cpu == newcpu); }else { -irq_matrix_free (vector_matrix, apicd->cpu, apicd->vector, - managed); +apic_free_vector (apicd->cpu, apicd->vector, managed); } setnew:
@@ -261,7 +268,7 @@ assign_vector_locked(struct irq_data *irqd, const struct cpumask *dest)trace_vector_alloc (irqd->irq, vector, resvd, vector);if (vector <0 )return vector; -apic_update_vector (irqd, vector, cpu); +apic_chipd_update_vector (irqd, vector, cpu);return 0 ; }
@@ -337,7 +344,7 @@ assign_managed_vector(struct irq_data *irqd, const struct cpumask *dest)trace_vector_alloc_managed (irqd->irq, vector, vector);if (vector <0 )return vector; -apic_update_vector (irqd, vector, cpu); +apic_chipd_update_vector (irqd, vector, cpu);return 0 ; }
@@ -357,7 +364,7 @@ static void clear_irq_vector(struct irq_data *irqd) apicd->prev_cpu);per_cpu (vector_irq, apicd->cpu)[vector] =VECTOR_SHUTDOWN ; -irq_matrix_free (vector_matrix, apicd->cpu, vector, managed); +apic_free_vector (apicd->cpu, vector, managed); apicd->vector =0 ;/* Clean up move in progress */
@@ -366,7 +373,7 @@ static void clear_irq_vector(struct irq_data *irqd)return ;per_cpu (vector_irq, apicd->prev_cpu)[vector] =VECTOR_SHUTDOWN ; -irq_matrix_free (vector_matrix, apicd->prev_cpu, vector, managed); +apic_free_vector (apicd->prev_cpu, vector, managed); apicd->prev_vector =0 ; apicd->move_in_progress =0 ;hlist_del_init (&apicd->clist);
@@ -905,7 +912,7 @@ static void free_moved_vector(struct apic_chip_data *apicd)* affinity mask comes online. */ trace_vector_free_moved (apicd->irq, cpu, vector, managed); -irq_matrix_free (vector_matrix, cpu, vector, managed); +apic_free_vector (cpu, vector, managed);per_cpu (vector_irq, cpu)[vector] =VECTOR_UNUSED ;hlist_del_init (&apicd->clist); apicd->prev_vector =0 ;
Signature · 1 line
2.34.1
RFC PATCH v6 20/32 x86/apic: Add update_vector() callback for Secure AVIC
In reply to Neeraj UpadhyayAdd update_vector() callback to set/clear ALLOWED_IRR field in a vCPU's APIC backing page for vectors which are emulated by the hypervisor.
The ALLOWED_IRR field indicates the interrupt vectors which the guest allows the hypervisor to inject (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, should not be set by the guest in the ALLOWED_IRR fields.
As clearing/setting state of a vector will also be used in subsequent commits for other APIC regs (such as APIC_IRR update for sending IPI), add a common update_vector() in Secure AVIC driver.
---
Changes since v5:
- Use common apic_set_vector(), apic_clear_vector().
arch/x86/kernel/apic/x2apic_savic.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
arch/x86/kernel/apic/x2apic_savic.c +25 -0
@@ -23,6 +23,24 @@ static int savic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)return x2apic_enabled () &&cc_platform_has (CC_ATTR_SNP_SECURE_AVIC ); } +static inline void *get_reg_bitmap (unsigned int cpu,unsigned int offset) +{ +struct apic_page *ap =per_cpu_ptr (apic_page, cpu); + +return &ap->bytes[offset]; +} + +static inline void update_vector (unsigned int cpu,unsigned int offset, +unsigned int vector,bool set) +{ +void *bitmap =get_reg_bitmap (cpu, offset); + +if (set) +apic_set_vector (vector, bitmap); +else +apic_clear_vector (vector, bitmap); +} +#define SAVIC_ALLOWED_IRR 0x204static u32 savic_read (u32 reg)
@@ -131,6 +149,11 @@ static void savic_write(u32 reg, u32 data) } } +static void savic_update_vector (unsigned int cpu,unsigned int vector,bool set) +{ +update_vector (cpu,SAVIC_ALLOWED_IRR , vector, set); +} +static void init_apic_page (struct apic_page *ap) {u32 apic_id;
@@ -212,6 +235,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 = savic_update_vector, };apic_driver (apic_x2apic_savic);
Signature · 1 line
2.34.1
RFC PATCH v6 21/32 x86/apic: Add support to send IPI for Secure AVIC
In reply to Neeraj UpadhyayWith Secure AVIC only Self-IPI is accelerated. To handle all the other IPIs, add new callbacks for sending IPI. These callbacks write to the IRR of the target guest vCPU's APIC backing page and issue GHCB protocol MSR write event for the hypervisor to notify the target vCPU about the new interrupt request.
For Secure AVIC GHCB APIC MSR writes, reuse GHCB msr handling code in vc_handle_msr() by exposing a sev-internal sev_es_ghcb_handle_msr().
---
Changes since v5:
- Fix merge conflicts due to addition of new file vc-handle.c at new base commit.
- Fix savic_icr_write() to update the APIC_ICR value in backing page.
- Call savic_icr_write() APIC_ICR reg in savic_write().
arch/x86/coco/sev/core.c | 28 ++++++ arch/x86/coco/sev/vc-handle.c | 11 ++- arch/x86/include/asm/sev-internal.h | 2 + arch/x86/include/asm/sev.h | 2 + arch/x86/kernel/apic/x2apic_savic.c | 139 +++++++++++++++++++++++++++- 5 files changed, 174 insertions(+), 8 deletions(-)
arch/x86/coco/sev/core.c +28 -0
@@ -1006,6 +1006,34 @@ int __init sev_es_efi_map_ghcbs(pgd_t *pgd)return 0 ; } +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 = ®s }; +struct ghcb_state state; +enum es_result res; +struct ghcb *ghcb; + +guard (irqsave)(); + + ghcb =__sev_get_ghcb (&state); +vc_ghcb_invalidate (ghcb); + + res =sev_es_ghcb_handle_msr (ghcb, &ctxt, true); +if (res !=ES_OK ) { +pr_err ("Secure AVIC msr (0x%llx) write returned error (%d)\n" , msr, res); +/* MSR writes should never fail. Any failure is fatal error for SNP guest */ +snp_abort (); + } + +__sev_put_ghcb (&state); +} +enum es_result savic_register_gpa (u64 gpa) {struct ghcb_state state;
arch/x86/coco/sev/vc-handle.c +6 -5
@@ -394,14 +394,10 @@ static enum es_result __vc_handle_secure_tsc_msrs(struct pt_regs *regs, bool wrireturn ES_OK ; } -static enum es_result vc_handle_msr (struct ghcb *ghcb,struct es_em_ctxt *ctxt) +enum es_result sev_es_ghcb_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 :
@@ -431,6 +427,11 @@ 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 sev_es_ghcb_handle_msr (ghcb, ctxt, ctxt->insn.opcode.bytes[1 ] ==0x30 ); +} +static void __init vc_early_forward_exception (struct es_em_ctxt *ctxt) {int trapnr = ctxt->fi.vector;
arch/x86/include/asm/sev-internal.h +2 -0
@@ -97,6 +97,8 @@ static __always_inline void sev_es_wr_ghcb_msr(u64 val)native_wrmsr (MSR_AMD64_SEV_ES_GHCB , low, high); } +enum es_result sev_es_ghcb_handle_msr (struct ghcb *ghcb,struct es_em_ctxt *ctxt,bool write); +void snp_register_ghcb_early (unsigned long paddr);bool sev_es_negotiate_protocol (void );bool sev_es_check_cpu_features (void );
arch/x86/include/asm/sev.h +2 -0
@@ -521,6 +521,7 @@ int snp_svsm_vtpm_send_command(u8 *buffer);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); static __always_inline void vc_ghcb_invalidate(struct ghcb *ghcb) {
@@ -594,6 +595,7 @@ static inline int snp_svsm_vtpm_send_command(u8 *buffer) { 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 ; } +static inline void savic_ghcb_msr_write (u32 reg,u64 value) { }#endif /* CONFIG_AMD_MEM_ENCRYPT */
arch/x86/kernel/apic/x2apic_savic.c +136 -3
181 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH v6 22/32 x86/apic: Support LAPIC timer for Secure AVIC
In reply to Neeraj UpadhyaySecure 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.
---
Changes since v5:
- Minor refactoring.
arch/x86/coco/sev/core.c | 26 ++++++++++++++++++++++++++ arch/x86/include/asm/sev.h | 2 ++ arch/x86/kernel/apic/apic.c | 2 ++ arch/x86/kernel/apic/x2apic_savic.c | 7 +++++-- 4 files changed, 35 insertions(+), 2 deletions(-)
arch/x86/coco/sev/core.c +26 -0
@@ -1006,6 +1006,32 @@ int __init sev_es_efi_map_ghcbs(pgd_t *pgd)return 0 ; } +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 = ®s }; +struct ghcb_state state; +enum es_result res; +struct ghcb *ghcb; + +guard (irqsave)(); + + ghcb =__sev_get_ghcb (&state); +vc_ghcb_invalidate (ghcb); + + res =sev_es_ghcb_handle_msr (ghcb, &ctxt, false); +if (res !=ES_OK ) { +pr_err ("Secure AVIC msr (0x%llx) read returned error (%d)\n" , msr, res); +/* MSR read failures are treated as fatal errors */ +snp_abort (); + } + +__sev_put_ghcb (&state); + +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/sev.h +2 -0
@@ -521,6 +521,7 @@ int snp_svsm_vtpm_send_command(u8 *buffer);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); static __always_inline void vc_ghcb_invalidate(struct ghcb *ghcb)
@@ -596,6 +597,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
@@ -592,6 +592,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/x2apic_savic.c +5 -2
@@ -64,6 +64,7 @@ static u32 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 :
@@ -184,10 +185,12 @@ static void 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
RFC PATCH v6 23/32 x86/sev: Initialize VGIF for secondary VCPUs for Secure AVIC
In reply to Neeraj UpadhyayFrom: 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 v5:
- No change.
arch/x86/coco/sev/core.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/coco/sev/core.c +3 -0
@@ -859,6 +859,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
RFC PATCH v6 24/32 x86/apic: Add support to send NMI IPI for Secure AVIC
In reply to Neeraj UpadhyaySecure 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.
Sending NMI IPI 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 v5:
- Set NmiReg using apic_set_reg().
arch/x86/kernel/apic/x2apic_savic.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
arch/x86/kernel/apic/x2apic_savic.c +20 -8
@@ -122,12 +122,19 @@ static inline void self_ipi_reg_write(unsigned int vector)native_apic_msr_write (APIC_SELF_IPI , vector); } -static 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); + +apic_set_reg (ap,SAVIC_NMI_REQ ,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;
@@ -138,14 +145,17 @@ 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); } } -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 ); }
@@ -154,22 +164,24 @@ static void savic_icr_write(u32 icr_low, u32 icr_high)struct apic_page *ap =this_cpu_ptr (apic_page);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
RFC PATCH v6 25/32 x86/apic: Allow NMI to be injected from hypervisor for Secure AVIC
In reply to Neeraj UpadhyaySecure 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 v5:
- No change.
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
@@ -693,6 +693,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
@@ -19,6 +19,11 @@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 savic_acpi_madt_oem_check (char *oem_id,char *oem_table_id) {return x2apic_enabled () &&cc_platform_has (CC_ATTR_SNP_SECURE_AVIC );
@@ -330,6 +335,7 @@ static void savic_setup(void) res =savic_register_gpa (gpa);if (res !=ES_OK )snp_abort (); +savic_wr_control_msr (gpa |MSR_AMD64_SECURE_AVIC_ALLOWEDNMI ); }static int savic_probe (void )
Signature · 1 line
2.34.1
RFC PATCH v6 26/32 x86/sev: Enable NMI support for Secure AVIC
In reply to Neeraj UpadhyayFrom: 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 v5:
- No change.
arch/x86/coco/sev/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/coco/sev/core.c +1 -1
@@ -860,7 +860,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
RFC PATCH v6 27/32 x86/apic: Read and write LVT* APIC registers from HV for SAVIC guests
In reply to Neeraj UpadhyayHypervisor 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 enabled guests.
---
Changes since v5:
- 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
@@ -69,6 +69,11 @@ static u32 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 :
@@ -78,11 +83,6 @@ static u32 savic_read(u32 reg)case APIC_LDR :case APIC_SPIV :case APIC_ESR : -case APIC_LVTTHMR : -case APIC_LVTPC : -case APIC_LVT0 : -case APIC_LVT1 : -case APIC_LVTERR :case APIC_EFEAT :case APIC_ECTRL :case APIC_SEOI :
@@ -204,18 +204,18 @@ static void 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_LVTTHMR : -case APIC_LVTPC : -case APIC_LVTERR :case APIC_ECTRL :case APIC_SEOI :case APIC_IER :
Signature · 1 line
2.34.1
RFC PATCH v6 28/32 x86/apic: Handle EOI writes for Secure AVIC guests
In reply to Neeraj UpadhyaySecure 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. To complete EOI handling, the VC exception handler would need to trigger a GHCB protocol MSR write event to notify the hypervisor about completion of the level-triggered interrupt. Hypervisor notification is required for cases like emulated IOAPIC, to complete and clear interrupt in the IOAPIC's interrupt state.
However, VC exception handling adds extra performance overhead for APIC register writes. In addition, for Secure AVIC, some unaccelerated APIC register msr writes are trapped, whereas others are faulted. This results in additional complexity in VC exception handling for unacclerated APIC msr accesses. So, directly do a GHCB protocol based APIC EOI msr 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 APIC EOI msr write.
---
Changes since v5:
- Use apic_test_vector() in apic.h
arch/x86/kernel/apic/x2apic_savic.c | 35 ++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-)
arch/x86/kernel/apic/x2apic_savic.c +34 -1
@@ -300,6 +300,39 @@ static void savic_update_vector(unsigned int cpu, unsigned int vector, bool set)update_vector (cpu,SAVIC_ALLOWED_IRR , vector, set); } +static void savic_eoi (void ) +{ +unsigned int cpu; +void *bitmap; +int vec; + + cpu =raw_smp_processor_id (); + bitmap =get_reg_bitmap (cpu,APIC_ISR ); + vec =apic_find_highest_vector (bitmap); +if (WARN_ONCE (vec ==-1 ,"EOI write while no active interrupt in APIC_ISR" )) +return ; + + bitmap =get_reg_bitmap (cpu,APIC_TMR ); + +/* Is level-triggered interrupt? */ +if (apic_test_vector (vec, bitmap)) { +update_vector (cpu,APIC_ISR , vec, false); +/* +* Propagate the EOI write to hv for level-triggered interrupts. +* Return to guest from GHCB protocol event takes care of +* re-evaluating interrupt state. +*/ +savic_ghcb_msr_write (APIC_EOI ,0 ); + }else { +/* +* Hardware clears APIC_ISR and re-evaluates the interrupt state +* to determine if there is any pending interrupt which can be +* delivered to CPU. +*/ +native_apic_msr_eoi (); + } +} +static void init_apic_page (struct apic_page *ap) {u32 apic_id;
@@ -386,7 +419,7 @@ static struct apic apic_x2apic_savic __ro_after_init = { .read = savic_read, .write = savic_write, - .eoi = native_apic_msr_eoi, + .eoi = savic_eoi, .icr_read = native_x2apic_icr_read, .icr_write = savic_icr_write,
Signature · 1 line
2.34.1
RFC PATCH v6 29/32 x86/apic: Add kexec support for Secure AVIC
In reply to Neeraj UpadhyayAdd 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 v5:
- No change.
arch/x86/coco/sev/core.c | 23 +++++++++++++++++++++++ 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, 37 insertions(+)
arch/x86/coco/sev/core.c +23 -0
@@ -1085,6 +1085,29 @@ 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; +enum es_result res; +struct ghcb *ghcb; + +guard (irqsave)(); + + ghcb =__sev_get_ghcb (&state); +vc_ghcb_invalidate (ghcb); + +ghcb_set_rax (ghcb,SVM_VMGEXIT_SAVIC_SELF_GPA ); + res =sev_es_ghcb_hv_call (ghcb, &ctxt,SVM_VMGEXIT_SAVIC , +SVM_VMGEXIT_SAVIC_UNREGISTER_GPA ,0 ); +if (gpa && res ==ES_OK ) + *gpa = ghcb->save.rbx; + +__sev_put_ghcb (&state); + +return res; +} +static void snp_register_per_cpu_ghcb (void ) {struct sev_es_runtime_data *data;
arch/x86/include/asm/apic.h +1 -0
@@ -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
@@ -521,6 +521,7 @@ int snp_svsm_vtpm_send_command(u8 *buffer);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);
@@ -596,6 +597,7 @@ static inline int snp_svsm_vtpm_send_command(u8 *buffer) { 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 ; } +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
@@ -1170,6 +1170,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
@@ -345,6 +345,13 @@ static void init_apic_page(struct apic_page *ap)apic_set_reg (ap,APIC_ID , apic_id); } +static void savic_teardown (void ) +{ +/* Disable Secure AVIC */ +native_wrmsr (MSR_AMD64_SECURE_AVIC_CONTROL ,0 ,0 ); +savic_unregister_gpa (NULL ); +} +static void savic_setup (void ) {void *backing_page;
@@ -395,6 +402,7 @@ static struct apic apic_x2apic_savic __ro_after_init = { .probe = savic_probe, .acpi_madt_oem_check = savic_acpi_madt_oem_check, .setup = savic_setup, + .teardown = savic_teardown, .dest_mode_logical = false,
Signature · 1 line
2.34.1
RFC PATCH v6 30/32 x86/apic: Enable Secure AVIC in Control MSR
In reply to Neeraj UpadhyayWith 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 v5:
- No change.
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
@@ -694,6 +694,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
@@ -375,7 +375,7 @@ static void savic_setup(void) res =savic_register_gpa (gpa);if (res !=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 savic_probe (void )
Signature · 1 line
2.34.1
RFC PATCH v6 31/32 x86/sev: Prevent SECURE_AVIC_CONTROL MSR interception for Secure AVIC guests
In reply to Neeraj UpadhyayThe 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 v5:
- No change.
arch/x86/coco/sev/vc-handle.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/x86/coco/sev/vc-handle.c +9 -0
@@ -407,6 +407,15 @@ enum es_result sev_es_ghcb_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxtif (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 ; +break ;default :break ; }
Signature · 1 line
2.34.1
Re: [RFC PATCH v6 31/32] x86/sev: Prevent SECURE_AVIC_CONTROL MSR interception for Secure AVIC guests
Quoted 9 lines · Neeraj Upadhyay
The
SECURE_AVIC_CONTROLMSR 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.Signed-off-by Neeraj Upadhyay <[email protected]>---
Signature · 2 lines
Thanks Tianyu Lan
RFC PATCH v6 32/32 x86/sev: Indicate SEV-SNP guest supports Secure AVIC
In reply to Neeraj UpadhyayNow that Secure AVIC support is added in the guest, indicate SEV-SNP guest supports Secure AVIC feature if AMD_SECURE_AVIC config is enabled.
---
Changes since v5:
No change.
arch/x86/boot/compressed/sev.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/sev.c +8 -1
@@ -238,13 +238,20 @@ bool sev_es_check_ghcb_fault(unsigned long address) 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