Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] KVM: arm64: Convert GIC idreg sanitisation to the confic framework
@ 2026-11-23 19:24 Marc Zyngier
  2026-11-23 19:24 ` [PATCH 1/3] KVM: arm64: Add enum support to idreg sanitisation Marc Zyngier
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Marc Zyngier @ 2026-11-23 19:24 UTC (permalink / raw)
  To: kvmarm, kvm, linux-arm-kernel
  Cc: Steffen Eiden, Joey Gouly, Suzuki K Poulose, Oliver Upton,
	Zenghui Yu, Fuad Tabba

Having started adding some sanitisation code in the idreg department
with ID_AA64MMFR3_EL1.S1P[IO]E, I'm back with some more and trying to
wrestle the GIC into submission...

These patches add zero functionality, but aim at setting the basis of
a much larger sanitisation goal, where all idregs are checked for
consistency and the resulting constraints used to enforce the other
sysregs' behaviour.

Patches on top of kvmarm-fixes-7.4-1 (the only one that is currently
in Linus' tree...).

Marc Zyngier (3):
  KVM: arm64: Add enum support to idreg sanitisation
  KVM: arm64: Add GIC idregs sanitisation to common framework
  KVM: arm64: Drop open-coded GIC idreg sanitisation

 arch/arm64/kvm/config.c         | 100 +++++++++++++++++++++++++++++++-
 arch/arm64/kvm/sys_regs.c       |  45 --------------
 arch/arm64/kvm/vgic/vgic-init.c |  31 +---------
 include/kvm/arm_vgic.h          |   1 -
 4 files changed, 100 insertions(+), 77 deletions(-)

-- 
2.47.3



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] KVM: arm64: Add enum support to idreg sanitisation
  2026-11-23 19:24 [PATCH 0/3] KVM: arm64: Convert GIC idreg sanitisation to the confic framework Marc Zyngier
@ 2026-11-23 19:24 ` Marc Zyngier
  2026-11-23 19:24 ` [PATCH 2/3] KVM: arm64: Add GIC idregs sanitisation to common framework Marc Zyngier
  2026-11-23 19:24 ` [PATCH 3/3] KVM: arm64: Drop open-coded GIC idreg sanitisation Marc Zyngier
  2 siblings, 0 replies; 4+ messages in thread
From: Marc Zyngier @ 2026-11-23 19:24 UTC (permalink / raw)
  To: kvmarm, kvm, linux-arm-kernel
  Cc: Steffen Eiden, Joey Gouly, Suzuki K Poulose, Oliver Upton,
	Zenghui Yu, Fuad Tabba

While a good deal of ID register fields are simple booleans, simply
indicating whether a feature is supported, a number of them are more
structured and indicate a level of support.

Reuse the existing framework in a nested way to expose which level
applies for the current configuration. This relies on the levels
being ordered from the most to the least capable, and the fallback
to have a NULL callback.

Yes, this is pretty obscure, but the following patches will make
this clearer (at least one hopes...).

Signed-off-by: Marc Zyngier <maz@kernel.org>
---
 arch/arm64/kvm/config.c | 44 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index cfdb6c7133204..40965513ebacf 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -28,6 +28,7 @@ struct reg_bits_to_feat_map {
 #define	REQUIRES_E2H1	BIT(5)	/* Add HCR_EL2.E2H RES1 as a pre-condition */
 #define	RES1_WHEN_E2H0	BIT(6)	/* RES1 when E2H=0 and not supported */
 #define	RES1_WHEN_E2H1	BIT(7)	/* RES1 when E2H=1 and not supported */
+#define	IDREG_FIELDS	BIT(8)	/* idreg fields */
 
 	unsigned long	flags;
 
@@ -40,6 +41,7 @@ struct reg_bits_to_feat_map {
 			s8	lo_lim;
 		};
 		bool	(*match)(struct kvm *);
+		const struct reg_bits_to_feat_map *idreg_field_deps;
 	};
 };
 
@@ -73,6 +75,13 @@ struct reg_feat_map_desc {
 		.lo_lim	= id ##_## fld ##_## lim	\
 	}
 
+#define __NEEDS_FEAT_2(m, f, w, dummy, deps)		\
+	{						\
+		.w	= (m),				\
+		.flags = (f),				\
+		.idreg_field_deps = (deps),		\
+	}
+
 #define __NEEDS_FEAT_1(m, f, w, fun)			\
 	{						\
 		.w	= (m),				\
@@ -95,6 +104,11 @@ struct reg_feat_map_desc {
 #define NEEDS_FEAT_MASKS(p, ...)				\
 	__NEEDS_FEAT_FLAG(p, MASKS_POINTER, masks, __VA_ARGS__)
 
+#define NEEDS_FEAT_FIELD(m, ...)				\
+	__NEEDS_FEAT_FLAG(m, IDREG_FIELDS, bits, blah, __VA_ARGS__)
+
+#define ENUM_FEAT(e, f)	{ .bits = e, .match = f }
+
 /*
  * Declare the dependency between a set of bits and a set of features,
  * generating a struct reg_bit_to_feat_map.
@@ -1456,6 +1470,27 @@ static const struct reg_feat_map_desc *idreg_feat_maps[] = {
 	&id_aa64mmfr3_el1_desc,
 };
 
+static struct resx compute_idreg_field_deps(struct kvm *kvm,
+					    const struct reg_bits_to_feat_map *map)
+{
+	const struct reg_bits_to_feat_map *tmp;
+	struct resx resx;
+
+	/*
+	 * Assume the enum values are ordered in reverse order of
+	 * capability (from most to least capable). A NULL callback
+	 * value indicates the ultimate fallback (not implemented).
+	 */
+	for (tmp = map->idreg_field_deps; tmp->match; tmp++)
+		if (tmp->match(kvm))
+			break;
+
+	resx.res0 = field_prep(map->bits, ~(tmp->bits));
+	resx.res1 = field_prep(map->bits, tmp->bits);
+
+	return resx;
+}
+
 static bool idreg_apply_deps(struct kvm *kvm, const struct reg_feat_map_desc *r)
 {
 	struct resx resx = {};
@@ -1463,8 +1498,15 @@ static bool idreg_apply_deps(struct kvm *kvm, const struct reg_feat_map_desc *r)
 
 	for (int i = 0; i < r->bit_feat_map_sz; i++) {
 		if ((r->bit_feat_map[i].flags & CALL_FUNC) &&
-		    !r->bit_feat_map[i].match(kvm))
+		    !r->bit_feat_map[i].match(kvm)) {
 			resx.res0 |= reg_feat_map_bits(&r->bit_feat_map[i]);
+		} else if (r->bit_feat_map[i].flags & IDREG_FIELDS) {
+			struct resx tmp;
+
+			tmp = compute_idreg_field_deps(kvm, &r->bit_feat_map[i]);
+			resx.res0 |= tmp.res0;
+			resx.res1 |= tmp.res1;
+		}
 	}
 
 	val = kvm->arch.id_regs[r->idreg_idx];
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] KVM: arm64: Add GIC idregs sanitisation to common framework
  2026-11-23 19:24 [PATCH 0/3] KVM: arm64: Convert GIC idreg sanitisation to the confic framework Marc Zyngier
  2026-11-23 19:24 ` [PATCH 1/3] KVM: arm64: Add enum support to idreg sanitisation Marc Zyngier
@ 2026-11-23 19:24 ` Marc Zyngier
  2026-11-23 19:24 ` [PATCH 3/3] KVM: arm64: Drop open-coded GIC idreg sanitisation Marc Zyngier
  2 siblings, 0 replies; 4+ messages in thread
From: Marc Zyngier @ 2026-11-23 19:24 UTC (permalink / raw)
  To: kvmarm, kvm, linux-arm-kernel
  Cc: Steffen Eiden, Joey Gouly, Suzuki K Poulose, Oliver Upton,
	Zenghui Yu, Fuad Tabba

It takes no least than 3 ID registers to describe whether a GICv3 or
GICv5 is supported. Define the sanitisation tables for the relevant
fields and the set of relevant callbacks.

Signed-off-by: Marc Zyngier <maz@kernel.org>
---
 arch/arm64/kvm/config.c | 56 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index 40965513ebacf..7e702ce587a48 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -436,6 +436,23 @@ static bool feat_vmid16(struct kvm *kvm)
 	return kvm_has_feat_enum(kvm, ID_AA64MMFR1_EL1, VMIDBits, 16);
 }
 
+static bool feat_gicv3(struct kvm *kvm)
+{
+	return (irqchip_in_kernel(kvm) &&
+		kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3);
+}
+
+static bool feat_gicv3_aarch32(struct kvm *kvm)
+{
+	return (kvm_supports_32bit_el0() && feat_gicv3(kvm));
+}
+
+static bool feat_gcie(struct kvm *kvm)
+{
+	return (irqchip_in_kernel(kvm) &&
+		kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V5);
+}
+
 static const struct reg_bits_to_feat_map hfgrtr_feat_map[] = {
 	NEEDS_FEAT(HFGRTR_EL2_nAMAIR2_EL1	|
 		   HFGRTR_EL2_nMAIR2_EL1,
@@ -1357,6 +1374,42 @@ static const struct reg_bits_to_feat_map id_aa64mmfr3_el1_feat_map[] = {
 static const DECLARE_IDREG_FEAT_MAP(id_aa64mmfr3_el1_desc, ID_AA64MMFR3_EL1,
 				    id_aa64mmfr3_el1_feat_map, FEAT_AA64EL1);
 
+static const struct reg_bits_to_feat_map aa64pfr0_gicv3[] = {
+	ENUM_FEAT(ID_AA64PFR0_EL1_GIC_IMP, feat_gicv3 ),
+	ENUM_FEAT(ID_AA64PFR0_EL1_GIC_NI, NULL ),
+};
+
+static const struct reg_bits_to_feat_map id_aa64pfr0_el1_feat_map[] = {
+	NEEDS_FEAT_FIELD(ID_AA64PFR0_EL1_GIC, aa64pfr0_gicv3),
+};
+
+static const DECLARE_IDREG_FEAT_MAP(id_aa64pfr0_el1_desc, ID_AA64PFR0_EL1,
+				    id_aa64pfr0_el1_feat_map, FEAT_AA64EL1);
+
+static const struct reg_bits_to_feat_map pfr1_gicv3[] = {
+	ENUM_FEAT(ID_PFR1_EL1_GIC_GICv3, feat_gicv3_aarch32),
+	ENUM_FEAT(ID_PFR1_EL1_GIC_NI, NULL),
+};
+
+static const struct reg_bits_to_feat_map id_pfr1_el1_feat_map[] = {
+	NEEDS_FEAT_FIELD(ID_PFR1_EL1_GIC, pfr1_gicv3),
+};
+
+static const DECLARE_IDREG_FEAT_MAP(id_pfr1_el1_desc, ID_PFR1_EL1,
+				    id_pfr1_el1_feat_map, FEAT_AA64EL1);
+
+static const struct reg_bits_to_feat_map aa64pfr2_gcie[] = {
+	ENUM_FEAT(ID_AA64PFR2_EL1_GCIE_IMP, feat_gcie),
+	ENUM_FEAT(ID_AA64PFR2_EL1_GCIE_NI, NULL),
+};
+
+static const struct reg_bits_to_feat_map id_aa64pfr2_el1_feat_map[] = {
+	NEEDS_FEAT_FIELD(ID_AA64PFR2_EL1_GCIE, aa64pfr2_gcie),
+};
+
+static const DECLARE_IDREG_FEAT_MAP(id_aa64pfr2_el1_desc, ID_AA64PFR2_EL1,
+				    id_aa64pfr2_el1_feat_map, FEAT_AA64EL1);
+
 static const struct reg_bits_to_feat_map ich_hfgrtr_feat_map[] = {
 	NEEDS_FEAT(ICH_HFGRTR_EL2_ICC_APR_EL1 |
 		   ICH_HFGRTR_EL2_ICC_IDRn_EL1 |
@@ -1468,6 +1521,9 @@ void __init check_feature_map(void)
 
 static const struct reg_feat_map_desc *idreg_feat_maps[] = {
 	&id_aa64mmfr3_el1_desc,
+	&id_aa64pfr0_el1_desc,
+	&id_aa64pfr2_el1_desc,
+	&id_pfr1_el1_desc,
 };
 
 static struct resx compute_idreg_field_deps(struct kvm *kvm,
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] KVM: arm64: Drop open-coded GIC idreg sanitisation
  2026-11-23 19:24 [PATCH 0/3] KVM: arm64: Convert GIC idreg sanitisation to the confic framework Marc Zyngier
  2026-11-23 19:24 ` [PATCH 1/3] KVM: arm64: Add enum support to idreg sanitisation Marc Zyngier
  2026-11-23 19:24 ` [PATCH 2/3] KVM: arm64: Add GIC idregs sanitisation to common framework Marc Zyngier
@ 2026-11-23 19:24 ` Marc Zyngier
  2 siblings, 0 replies; 4+ messages in thread
From: Marc Zyngier @ 2026-11-23 19:24 UTC (permalink / raw)
  To: kvmarm, kvm, linux-arm-kernel
  Cc: Steffen Eiden, Joey Gouly, Suzuki K Poulose, Oliver Upton,
	Zenghui Yu, Fuad Tabba

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@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



^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-23 19:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 19:24 [PATCH 0/3] KVM: arm64: Convert GIC idreg sanitisation to the confic framework Marc Zyngier
2026-11-23 19:24 ` [PATCH 1/3] KVM: arm64: Add enum support to idreg sanitisation Marc Zyngier
2026-11-23 19:24 ` [PATCH 2/3] KVM: arm64: Add GIC idregs sanitisation to common framework Marc Zyngier
2026-11-23 19:24 ` [PATCH 3/3] KVM: arm64: Drop open-coded GIC idreg sanitisation Marc Zyngier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox