[PATCH v4 3/7] KVM: SVM: Implement GET_AP_APIC_IDS NAE event

From: Jörg Rödel

Date: Tue Dec 01 2026 - 04:45:59 EST


From: Joerg Roedel <joerg.roedel@xxxxxxx>

Implement the GHCB APIC ID List event so an SNP guest can discover its
vCPUs without a broadcast INIT-SIPI sequence. Use the direct VMSA
capability as the userspace opt-in, as Sean suggested, to keep guest
feature discovery stable for existing SNP VMs across kernel updates.

Gate both GHCB feature queries and event dispatch on the same condition.
Check the entire supplied range for address overflow and shared writable
backing and write the exact descriptor under the vCPU's SRCU read lock.
For undersized requests, return the required page count without writing.

Codex assisted with feature gating, buffer validation and build checks.

Signed-off-by: Joerg Roedel <joerg.roedel@xxxxxxx>
---
arch/x86/include/asm/sev-common.h | 1 +
arch/x86/include/uapi/asm/svm.h | 1 +
arch/x86/kvm/svm/sev.c | 102 +++++++++++++++++++++++++++++-
3 files changed, 101 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h
index 01a6e4dbe423..c041f85dfd5c 100644
--- a/arch/x86/include/asm/sev-common.h
+++ b/arch/x86/include/asm/sev-common.h
@@ -136,6 +136,7 @@ enum psc_op {

#define GHCB_HV_FT_SNP BIT_ULL(0)
#define GHCB_HV_FT_SNP_AP_CREATION BIT_ULL(1)
+#define GHCB_HV_FT_APIC_ID_LIST BIT_ULL(4)
#define GHCB_HV_FT_SNP_MULTI_VMPL BIT_ULL(5)

/*
diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h
index e80676185092..c9c85891971a 100644
--- a/arch/x86/include/uapi/asm/svm.h
+++ b/arch/x86/include/uapi/asm/svm.h
@@ -118,6 +118,7 @@
#define SVM_VMGEXIT_AP_CREATE_ON_INIT 0
#define SVM_VMGEXIT_AP_CREATE 1
#define SVM_VMGEXIT_AP_DESTROY 2
+#define SVM_VMGEXIT_GET_APIC_IDS 0x80000017ull
#define SVM_VMGEXIT_SNP_RUN_VMPL 0x80000018ull
#define SVM_VMGEXIT_SAVIC 0x8000001aull
#define SVM_VMGEXIT_SAVIC_REGISTER_GPA 0
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 18ccab113989..f22490f86a4a 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -3544,7 +3544,8 @@ static bool sev_es_are_required_ghcb_fields_valid(struct vcpu_svm *svm)
case SVM_VMGEXIT_AP_CREATION:
return kvm_ghcb_rax_is_valid(svm) ||
lower_32_bits(control->exit_info_1) == SVM_VMGEXIT_AP_DESTROY;
- break;
+ case SVM_VMGEXIT_GET_APIC_IDS:
+ return kvm_ghcb_rax_is_valid(svm);
case SVM_VMGEXIT_MMIO_READ:
case SVM_VMGEXIT_MMIO_WRITE:
case SVM_VMGEXIT_PSC:
@@ -4261,6 +4262,93 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm)
return 0;
}

+struct sev_apic_id_desc {
+ u32 num_entries;
+ u32 apic_ids[];
+};
+
+static bool sev_snp_apic_id_list_enabled(struct kvm_vcpu *vcpu)
+{
+ return is_sev_snp_guest(vcpu) &&
+ to_kvm_sev_info(vcpu->kvm)->snp_direct_vmsa;
+}
+
+static u64 sev_ghcb_hv_features(struct kvm_vcpu *vcpu)
+{
+ u64 features = GHCB_HV_FT_SUPPORTED;
+
+ if (sev_snp_apic_id_list_enabled(vcpu))
+ features |= GHCB_HV_FT_APIC_ID_LIST;
+
+ return features;
+}
+
+static int sev_snp_get_apic_ids(struct vcpu_svm *svm)
+{
+ struct kvm_vcpu *vcpu = &svm->vcpu;
+ struct kvm *kvm = vcpu->kvm;
+ struct sev_apic_id_desc *desc;
+ unsigned int nr_vcpus, size;
+ unsigned int i;
+ gpa_t gpa, end_gpa;
+ gfn_t gfn;
+ u64 pages, len;
+
+ nr_vcpus = atomic_read(&kvm->online_vcpus);
+ size = sizeof(*desc) + (nr_vcpus * sizeof(desc->apic_ids[0]));
+
+ pages = vcpu->arch.regs[VCPU_REGS_RAX];
+ if (pages < PFN_UP(size)) {
+ vcpu->arch.regs[VCPU_REGS_RAX] = PFN_UP(size);
+ return 1;
+ }
+
+ gpa = svm->vmcb->control.exit_info_1;
+ if (!PAGE_ALIGNED(gpa) ||
+ check_mul_overflow(pages, (u64)PAGE_SIZE, &len) ||
+ check_add_overflow(gpa, len - 1, &end_gpa) ||
+ !page_address_valid(vcpu, gpa) ||
+ !page_address_valid(vcpu, end_gpa & PAGE_MASK))
+ goto invalid_buffer;
+
+ for (gfn = gpa_to_gfn(gpa); gfn <= gpa_to_gfn(end_gpa); gfn++) {
+ struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
+
+ if (kvm_is_error_hva(gfn_to_hva_memslot(slot, gfn)) ||
+ kvm_is_private_gfn(kvm, gfn))
+ goto invalid_buffer;
+
+ if (signal_pending(current))
+ return -EINTR;
+ cond_resched();
+ }
+
+ desc = kvzalloc(size, GFP_KERNEL_ACCOUNT);
+ if (!desc)
+ return -ENOMEM;
+
+ desc->num_entries = nr_vcpus;
+ for (i = 0; i < nr_vcpus; i++) {
+ struct kvm_vcpu *listed_vcpu = kvm_get_vcpu(kvm, i);
+
+ if (WARN_ON_ONCE(!listed_vcpu))
+ goto invalid_buffer_free;
+ desc->apic_ids[i] = listed_vcpu->vcpu_id;
+ }
+
+ if (kvm_write_guest(kvm, gpa, desc, size))
+ goto invalid_buffer_free;
+
+ kvfree(desc);
+ return 1;
+
+invalid_buffer_free:
+ kvfree(desc);
+invalid_buffer:
+ svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
+ return 1;
+}
+
static int snp_handle_guest_req(struct vcpu_svm *svm, gpa_t req_gpa, gpa_t resp_gpa)
{
struct sev_data_snp_guest_request data = {0};
@@ -4491,7 +4579,7 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
GHCB_MSR_INFO_POS);
break;
case GHCB_MSR_HV_FT_REQ:
- set_ghcb_msr_bits(svm, GHCB_HV_FT_SUPPORTED,
+ set_ghcb_msr_bits(svm, sev_ghcb_hv_features(&svm->vcpu),
GHCB_MSR_HV_FT_MASK, GHCB_MSR_HV_FT_POS);
set_ghcb_msr_bits(svm, GHCB_MSR_HV_FT_RESP,
GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS);
@@ -4631,6 +4719,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
return 1;
}

+ if (control->exit_code == SVM_VMGEXIT_GET_APIC_IDS &&
+ !sev_snp_apic_id_list_enabled(vcpu)) {
+ svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_EVENT);
+ return 1;
+ }
+
if (!sev_es_are_required_ghcb_fields_valid(svm)) {
/*
* Print the exit code even though it may not be marked valid
@@ -4713,7 +4807,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
return 1;
}
case SVM_VMGEXIT_HV_FEATURES:
- svm_vmgexit_success(svm, GHCB_HV_FT_SUPPORTED);
+ svm_vmgexit_success(svm, sev_ghcb_hv_features(vcpu));
return 1;
case SVM_VMGEXIT_TERM_REQUEST:
pr_info("SEV-ES guest requested termination: reason %#llx info %#llx\n",
@@ -4736,6 +4830,8 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)
if (sev_snp_ap_creation(svm))
svm_vmgexit_bad_input(svm, GHCB_ERR_INVALID_INPUT);
return 1;
+ case SVM_VMGEXIT_GET_APIC_IDS:
+ return sev_snp_get_apic_ids(svm);
case SVM_VMGEXIT_GUEST_REQUEST:
case SVM_VMGEXIT_EXT_GUEST_REQUEST:
if (!PAGE_ALIGNED(control->exit_info_1) ||
--
2.53.0