[PATCH v2 04/45] KVM: arm64: Turn vgic-v3 errata traps into a patched-in constant

Marc Zyngier maz at kernel.org
Mon Nov 10 03:47:40 PST 2025


On Mon, 10 Nov 2025 10:40:21 +0000,
Suzuki K Poulose <suzuki.poulose at arm.com> wrote:
> 
> Hi Marc,
> 
> On 09/11/2025 17:15, Marc Zyngier wrote:
> > The trap bits are currently only set to manage CPU errata. However,
> > we are about to make use of them for purposes beyond beating broken
> > CPUs into submission.
> > 
> > For this purpose, turn these errata-driven bits into a patched-in
> > constant that is merged with the KVM-driven value at the point of
> > programming the ICH_HCR_EL2 register, rather than being directly
> > stored with with the shadow value..
> > 
> > This allows the KVM code to distinguish between a trap being handled
> > for the purpose of an erratum workaround, or for KVM's own need.
> > 
> > Signed-off-by: Marc Zyngier <maz at kernel.org>
> > ---
> 
> ...
> 
> > diff --git a/arch/arm64/kvm/vgic/vgic.h b/arch/arm64/kvm/vgic/vgic.h
> > index ac5f9c5d2b980..0ecadfa00397d 100644
> > --- a/arch/arm64/kvm/vgic/vgic.h
> > +++ b/arch/arm64/kvm/vgic/vgic.h
> > @@ -164,6 +164,22 @@ static inline int vgic_write_guest_lock(struct kvm *kvm, gpa_t gpa,
> >   	return ret;
> >   }
> >   +void kvm_compute_ich_hcr_trap_bits(struct alt_instr *alt,
> > +				   __le32 *origptr, __le32 *updptr, int nr_inst);
> > +
> > +static inline u64 vgic_ich_hcr_trap_bits(void)
> > +{
> > +	u64 hcr;
> 
> minor nit: Do we need a guard to make sure this isn't called before
> the capabilities are finalized (given we may use it outside VM
> context, e.g. VGIC probe). perhaps :
> 
> WARN_ON(!system_capabilities_finalized());

We already have a BUG_ON() for that at the point of setting up the
vectors for pKVM. It wouldn't hurt to move this up as a general check
for KVM, but I don't think placing these checks in random leaf
functions is very appealing.

And if we do, it should be gated to not emit code in the hot path,
with something like this (which I find awful):

diff --git a/arch/arm64/kvm/vgic/vgic.h b/arch/arm64/kvm/vgic/vgic.h
index ec3a61e8e6b30..43f202cb83b48 100644
--- a/arch/arm64/kvm/vgic/vgic.h
+++ b/arch/arm64/kvm/vgic/vgic.h
@@ -171,6 +171,9 @@ static inline u64 vgic_ich_hcr_trap_bits(void)
 {
 	u64 hcr;
 
+#if !defined(__KVM_VHE_HYPERVISOR__) && !defined(__KVM_NVHE_HYPERVISOR__)
+	WARN_ON(!system_capabilities_finalized());
+#endif
 	/* All the traps are in the bottom 16bits */
 	asm volatile(ALTERNATIVE_CB("movz %0, #0\n",
 				    ARM64_ALWAYS_SYSTEM,

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.



More information about the linux-arm-kernel mailing list