On Thu, 5 Nov 2026 at 12:26, Manos Pitsidianakis
<[email protected]> wrote:
>
> The number of priority bits is implementation defined, and indeed
> machines such as a9mpcore and arm11mpcore define it in the
> num-priority-bits property. However we still hardcoded GIC_MIN_BPR.
>
> This commit replaces the GIC_MIN_BPR and GIC_MIN_ABPR defs with macros
> that read the property.
>
> The calculation is based on "Table 4-47 Active Priorities register
> implementation" in the GICv2 spec (page 4-150 of ARM IHI 0048B.b
> ID072613):
>
> +---------------+---------------+---------------+
> |               |               |    Maximum    |
> | Minimum value | Minimum value |    number of  |
> |   of Secure   | of Non-secure |     group     |
> |   GICC_BPR    |   GICC_BPR    | priority bits |
> +===============+===============+===============+
> |             3 |             4 |             4 |
> |             2 |             3 |             5 |
> |             1 |             2 |             6 |
> |             0 |             1 |             7 |
> +---------------+---------------+---------------+
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4375
> Signed-off-by: Manos Pitsidianakis <[email protected]>
> ---

> diff --git a/include/hw/intc/arm_gic.h b/include/hw/intc/arm_gic.h
> index 
> be923f7ed8861620d17fbf3d8569c42ae7c95487..e090bd0b283297d0863e13ef07e5738a549ea59d
>  100644
> --- a/include/hw/intc/arm_gic.h
> +++ b/include/hw/intc/arm_gic.h
> @@ -70,11 +70,6 @@
>  #include "arm_gic_common.h"
>  #include "qom/object.h"
>
> -/* Number of SGI target-list bits */
> -#define GIC_TARGETLIST_BITS 8
> -#define GIC_MAX_PRIORITY_BITS 8
> -#define GIC_MIN_PRIORITY_BITS 4

You don't need to move GIC_TARGETLIST_BITS (or its comment), as it's
unrelated to the priority bit config.

> +static inline uint32_t gic_min_bpr(GICState *s)
> +{
> +    uint32_t num_preemption_bits = s->n_prio_bits == 8 ? 7 : s->n_prio_bits;
> +    assert(!(s->n_prio_bits > GIC_MAX_PRIORITY_BITS
> +             || s->n_prio_bits < GIC_MIN_PRIORITY_BITS));

It's possible to hit this assertion, because I put the "check that
num-priority-bits is set sensibly" code in the wrong place. We
check that in arm_gic_realize(), but the property is defined on
the "common" superclass, so it is present also for TYPE_KVM_ARM_GIC.
Most of the uses of gic_min_bpr() are in the emulated-GIC code,
but the calls from arm_gic_common_reset_irq_state() will be
used also if you have a KVM GICv2. So if the user sets a silly
num-priority-bits value with KVM GICv2 they'll run into the assert.

We should move the checks on n_prio_bits from arm_gic_realize()
to arm_gic_common_realize().

For GICv3 I pre-calculate the number of preemption bits into
cs->prebits, but that's partly because there are other places
in the code where things are written in terms of looking at
the number of preemption bits; for GICv2 it looks like we
mostly look at the min BPR instead so not precalculating is fine.

I do notice one other place where we need to adjust for the
number of priority bits not being 8, which is that the number
of GICC_APRn registers depends on the number of implemented
preemption levels (table 4-47 in the gicv2 spec). However we
check against GIC_NR_APRS in the read/write code for these.
I think that the only consequence here is "we let the guest
read and write some values that if it isn't buggy it will never
do", so we can fix that as a separate bug. (If it does write
to the nonexistent registers, we are still looking at them in
e.g. gic_get_prio_from_apr_bits() so we will generate a bogus
running priority value and odd things might happen.)

thanks
-- PMM

Reply via email to