[PATCH 3/3] KVM: arm64: Drop open-coded GIC idreg sanitisation

Marc Zyngier maz at kernel.org
Mon Nov 23 11:24:57 PST 2026


Now that we have a generic mechanism to sanitise the GIC idregs,
drop the open-coded version and replace it with a call to the
new code.

Signed-off-by: Marc Zyngier <maz at kernel.org>
---
 arch/arm64/kvm/sys_regs.c       | 45 ---------------------------------
 arch/arm64/kvm/vgic/vgic-init.c | 31 +----------------------
 include/kvm/arm_vgic.h          |  1 -
 3 files changed, 1 insertion(+), 76 deletions(-)

diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 0687b78cd7c2b..d145541420b3a 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -5874,51 +5874,6 @@ static int kvm_vm_finalize_sys_regs(struct kvm *kvm)
 
 	kvm_idregs_enforce_dependencies(kvm);
 
-	/*
-	 * This hacks into the ID registers, so only perform it when the
-	 * first vcpu runs, or the kvm_set_vm_id_reg() helper will scream.
-	 */
-	if (!irqchip_in_kernel(kvm)) {
-		u64 val;
-
-		val = kvm_read_vm_id_reg(kvm, SYS_ID_AA64PFR0_EL1) & ~ID_AA64PFR0_EL1_GIC;
-		kvm_set_vm_id_reg(kvm, SYS_ID_AA64PFR0_EL1, val);
-		val = kvm_read_vm_id_reg(kvm, SYS_ID_AA64PFR2_EL1) & ~ID_AA64PFR2_EL1_GCIE;
-		kvm_set_vm_id_reg(kvm, SYS_ID_AA64PFR2_EL1, val);
-		val = kvm_read_vm_id_reg(kvm, SYS_ID_PFR1_EL1) & ~ID_PFR1_EL1_GIC;
-		kvm_set_vm_id_reg(kvm, SYS_ID_PFR1_EL1, val);
-	} else {
-		/*
-		 * Certain userspace software - QEMU - samples the system
-		 * register state without creating an irqchip, then blindly
-		 * restores the state prior to running the final guest. This
-		 * means that it restores the virtualization & emulation
-		 * capabilities of the host system, rather than something that
-		 * reflects the final guest state. Moreover, it checks that the
-		 * state was "correctly" restored (i.e., verbatim), bailing if
-		 * it isn't, so masking off invalid state isn't an option.
-		 *
-		 * On GICv5 hardware that supports FEAT_GCIE_LEGACY we can run
-		 * both GICv3- and GICv5-based guests. Therefore, we initially
-		 * present both ID_AA64PFR0.GIC and ID_AA64PFR2.GCIE as IMP to
-		 * reflect that userspace can create EITHER a vGICv3 or a
-		 * vGICv5. This is an architecturally invalid combination, of
-		 * course. Once an in-kernel GIC is created, the sysreg state is
-		 * updated to reflect the actual, valid configuration.
-		 *
-		 * Setting both the GIC and GCIE features to IMP unsurprisingly
-		 * results in guests falling over, and hence we need to fix up
-		 * this mess in KVM. Before running for the first time we yet
-		 * again ensure that the GIC and GCIE fields accurately reflect
-		 * the actual hardware the guest should see.
-		 *
-		 * This hack allows legacy QEMU-based GICv3 guests to run
-		 * unmodified on compatible GICv5 hosts, and avoids the inverse
-		 * problem for GICv5-based guests in the future.
-		 */
-		kvm_vgic_finalize_idregs(kvm);
-	}
-
 	set_bit(KVM_ARCH_FLAG_ID_REGS_FINAL, &kvm->arch.flags);
 
 	return 0;
diff --git a/arch/arm64/kvm/vgic/vgic-init.c b/arch/arm64/kvm/vgic/vgic-init.c
index c9991e24cb6b7..b6236146602d8 100644
--- a/arch/arm64/kvm/vgic/vgic-init.c
+++ b/arch/arm64/kvm/vgic/vgic-init.c
@@ -180,7 +180,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
 	 * We've now created the GIC. Update the system register state
 	 * to accurately reflect what we've created.
 	 */
-	kvm_vgic_finalize_idregs(kvm);
+	kvm_idregs_enforce_dependencies(kvm);
 
 	kvm_for_each_vcpu(i, vcpu, kvm) {
 		ret = vgic_allocate_private_irqs_locked(vcpu, type);
@@ -770,35 +770,6 @@ int kvm_vgic_map_resources(struct kvm *kvm)
 	return ret;
 }
 
-void kvm_vgic_finalize_idregs(struct kvm *kvm)
-{
-	u32 type = kvm->arch.vgic.vgic_model;
-	u64 aa64pfr0, aa64pfr2, pfr1;
-
-	aa64pfr0 = kvm_read_vm_id_reg(kvm, SYS_ID_AA64PFR0_EL1) & ~ID_AA64PFR0_EL1_GIC;
-	aa64pfr2 = kvm_read_vm_id_reg(kvm, SYS_ID_AA64PFR2_EL1) & ~ID_AA64PFR2_EL1_GCIE;
-	pfr1 = kvm_read_vm_id_reg(kvm, SYS_ID_PFR1_EL1) & ~ID_PFR1_EL1_GIC;
-
-	switch (type) {
-	case KVM_DEV_TYPE_ARM_VGIC_V2:
-		break;
-	case KVM_DEV_TYPE_ARM_VGIC_V3:
-		aa64pfr0 |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, GIC, IMP);
-		if (kvm_supports_32bit_el0())
-			pfr1 |= SYS_FIELD_PREP_ENUM(ID_PFR1_EL1, GIC, GICv3);
-		break;
-	case KVM_DEV_TYPE_ARM_VGIC_V5:
-		aa64pfr2 |= SYS_FIELD_PREP_ENUM(ID_AA64PFR2_EL1, GCIE, IMP);
-		break;
-	default:
-		WARN_ONCE(1, "Unknown VGIC type!!!\n");
-	}
-
-	kvm_set_vm_id_reg(kvm, SYS_ID_AA64PFR0_EL1, aa64pfr0);
-	kvm_set_vm_id_reg(kvm, SYS_ID_AA64PFR2_EL1, aa64pfr2);
-	kvm_set_vm_id_reg(kvm, SYS_ID_PFR1_EL1, pfr1);
-}
-
 /* GENERIC PROBE */
 
 void kvm_vgic_cpu_up(void)
diff --git a/include/kvm/arm_vgic.h b/include/kvm/arm_vgic.h
index 4b2c41917b77c..956c68f75e7c0 100644
--- a/include/kvm/arm_vgic.h
+++ b/include/kvm/arm_vgic.h
@@ -689,7 +689,6 @@ int kvm_vgic_create(struct kvm *kvm, u32 type);
 void kvm_vgic_destroy(struct kvm *kvm);
 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
 int kvm_vgic_map_resources(struct kvm *kvm);
-void kvm_vgic_finalize_idregs(struct kvm *kvm);
 int kvm_vgic_hyp_init(void);
 void kvm_vgic_init_cpu_hardware(void);
 
-- 
2.47.3




More information about the linux-arm-kernel mailing list