diff mbox series

gicv2: do not hardcode minimum binary point

Message ID 20261105-gic_read_bpr_from_property-v1-1-7e7392bfd321@linaro.org
State New
Headers show
Series gicv2: do not hardcode minimum binary point | expand

Commit Message

Manos Pitsidianakis Nov. 5, 2026, 12:26 p.m. UTC
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 <manos.pitsidianakis@linaro.org>
---
 hw/intc/arm_gic.c                | 14 +++++++-------
 hw/intc/arm_gic_common.c         |  4 ++--
 include/hw/intc/arm_gic.h        |  5 -----
 include/hw/intc/arm_gic_common.h | 20 ++++++++++++++++++--
 4 files changed, 27 insertions(+), 16 deletions(-)


---
base-commit: f29ccc287a6cfdd51d3a4fd17fbe9af3310a0ac5
change-id: 20261105-gic_read_bpr_from_property-0882afdb60f3

Best regards,

Comments

Peter Maydell Nov. 5, 2026, 7:29 p.m. UTC | #1
On Thu, 5 Nov 2026 at 12:26, Manos Pitsidianakis
<manos.pitsidianakis@linaro.org> 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 <manos.pitsidianakis@linaro.org>
> ---

> 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
Manos Pitsidianakis Nov. 6, 2026, 7:01 a.m. UTC | #2
On Thu, 05 Nov 2026 21:29, Peter Maydell <peter.maydell@linaro.org> wrote:
>On Thu, 5 Nov 2026 at 12:26, Manos Pitsidianakis
><manos.pitsidianakis@linaro.org> 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 <manos.pitsidianakis@linaro.org>
>> ---
>
>> 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().

I will send a v2 with this suggestion included, thanks.

>
>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.)

I will have to read the spec a bit more on this, so I might send a patch 
separately instead of in the same series.

>
>thanks
>-- PMM
diff mbox series

Patch

diff --git a/hw/intc/arm_gic.c b/hw/intc/arm_gic.c
index 4d4b79e6f3408c43eae19c222edc75e41987b7ea..79a68a8648467c1f18dab69cf691f8e44ec2f011 100644
--- a/hw/intc/arm_gic.c
+++ b/hw/intc/arm_gic.c
@@ -479,7 +479,7 @@  static void gic_activate_irq(GICState *s, int cpu, int irq)
      * and update the running priority.
      */
     int prio = gic_get_group_priority(s, cpu, irq);
-    int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
+    int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : gic_min_bpr(s);
     int preemption_level = prio >> (min_bpr + 1);
     int regno = preemption_level / 32;
     int bitno = preemption_level % 32;
@@ -521,7 +521,7 @@  static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
         if (!apr) {
             continue;
         }
-        return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
+        return (i * 32 + ctz32(apr)) << (gic_min_bpr(s) + 1);
     }
     return 0x100;
 }
@@ -1561,7 +1561,7 @@  static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
     /* Return the Nonsecure view of GICC_APR<regno>. This is the
      * second half of GICC_NSAPR.
      */
-    switch (GIC_MIN_BPR) {
+    switch (gic_min_bpr(s)) {
     case 0:
         if (regno < 2) {
             return s->nsapr[regno + 2][cpu];
@@ -1592,7 +1592,7 @@  static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
                                          uint32_t value)
 {
     /* Write the Nonsecure view of GICC_APR<regno>. */
-    switch (GIC_MIN_BPR) {
+    switch (gic_min_bpr(s)) {
     case 0:
         if (regno < 2) {
             s->nsapr[regno + 2][cpu] = value;
@@ -1731,10 +1731,10 @@  static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
                 /* WI when CBPR is 1 */
                 return MEMTX_OK;
             } else {
-                s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
+                s->abpr[cpu] = MAX(value & 0x7, gic_min_abpr(s));
             }
         } else {
-            int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
+            int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : gic_min_bpr(s);
             s->bpr[cpu] = MAX(value & 0x7, min_bpr);
         }
         break;
@@ -1746,7 +1746,7 @@  static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
             /* unimplemented, or NS access: RAZ/WI */
             return MEMTX_OK;
         } else {
-            s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
+            s->abpr[cpu] = MAX(value & 0x7, gic_min_abpr(s));
         }
         break;
     case 0xd0: case 0xd4: case 0xd8: case 0xdc:
diff --git a/hw/intc/arm_gic_common.c b/hw/intc/arm_gic_common.c
index 304d89cf56219bf76eeeb24e55dbfe60be23ba43..7ffd94561202f49d767315893085df57a4f984d8 100644
--- a/hw/intc/arm_gic_common.c
+++ b/hw/intc/arm_gic_common.c
@@ -249,8 +249,8 @@  static inline void arm_gic_common_reset_irq_state(GICState *s, int cidx,
         s->current_pending[i] = 1023;
         s->running_priority[i] = 0x100;
         s->cpu_ctlr[i] = 0;
-        s->bpr[i] = gic_is_vcpu(i) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
-        s->abpr[i] = gic_is_vcpu(i) ? GIC_VIRT_MIN_ABPR : GIC_MIN_ABPR;
+        s->bpr[i] = gic_is_vcpu(i) ? GIC_VIRT_MIN_BPR : gic_min_bpr(s);
+        s->abpr[i] = gic_is_vcpu(i) ? GIC_VIRT_MIN_ABPR : gic_min_abpr(s);
 
         if (!gic_is_vcpu(i)) {
             for (j = 0; j < GIC_INTERNAL; j++) {
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
-
 #define TYPE_ARM_GIC "arm_gic"
 typedef struct ARMGICClass ARMGICClass;
 /* This is reusing the GICState typedef from TYPE_ARM_GIC_COMMON */
diff --git a/include/hw/intc/arm_gic_common.h b/include/hw/intc/arm_gic_common.h
index b169394e6ca28d787d70e9c103ebbe5b12202ecc..c89f8a3292dff5460e2baa49c9a55de6866fc33a 100644
--- a/include/hw/intc/arm_gic_common.h
+++ b/include/hw/intc/arm_gic_common.h
@@ -37,8 +37,10 @@ 
 #define MAX_NR_GROUP_PRIO 128
 #define GIC_NR_APRS (MAX_NR_GROUP_PRIO / 32)
 
-#define GIC_MIN_BPR 0
-#define GIC_MIN_ABPR (GIC_MIN_BPR + 1)
+/* Number of SGI target-list bits */
+#define GIC_TARGETLIST_BITS 8
+#define GIC_MAX_PRIORITY_BITS 8
+#define GIC_MIN_PRIORITY_BITS 4
 
 /* Architectural maximum number of list registers in the virtual interface */
 #define GIC_MAX_LR 64
@@ -151,6 +153,20 @@  struct GICState {
 };
 typedef struct GICState GICState;
 
+
+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));
+    return 7 - num_preemption_bits;
+}
+
+static inline uint32_t gic_min_abpr(GICState *s)
+{
+    return gic_min_bpr(s) + 1;
+}
+
 #define TYPE_ARM_GIC_COMMON "arm_gic_common"
 typedef struct ARMGICCommonClass ARMGICCommonClass;
 DECLARE_OBJ_CHECKERS(GICState, ARMGICCommonClass,