[PATCH 1/3] KVM: arm64: Add enum support to idreg sanitisation

Wei-Lin Chang weilin.chang at arm.com
Wed Nov 25 07:50:21 PST 2026


On Mon, Nov 23, 2026 at 07:24:55PM +0000, Marc Zyngier wrote:
> 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 at 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;

This looks to me like it just forces the bit pattern of the most capable
enum value the current configuration supports. So if the user sets
something that is different but less capable we will just raise it to
the most capable value, is this what we are going for? Or do we only
want to sanitise it when it is over the maximum allowed value?

Thanks,
Wei-Lin Chang

> +		}
>  	}
>  
>  	val = kvm->arch.id_regs[r->idreg_idx];
> -- 
> 2.47.3
> 



More information about the linux-arm-kernel mailing list