Skip to contentSkip to conversation

target/arm: Fix Coverity issues in GICv5 code

PATCH In review Peter Maydell 3 participants 12 messages May 12 – May 16

lore
Share & tools
lore
#0 lore raw

cover target/arm: Fix Coverity issues in GICv5 code

Coverity found a handful of bugs in the newly-landed GICv5 code; this patchset provides fixes for them.

thanks
-- PMM

Peter Maydell (4):
target/arm: GICv5 cpuif: Fix overflow in left shift target/arm: GICv5 cpuif: Don't set HPPIV bit in GICv5PendingIrq::intid hw/intc/arm_gicv5: Avoid NULL dereference in trace line hw/intc/arm_gicv5: Add missing early return in gicv5_set_handling()

 hw/intc/arm_gicv5.c               |  3 ++-
 include/hw/intc/arm_gicv5_types.h |  2 ++
 target/arm/tcg/gicv5-cpuif.c      | 26 ++++++++++++++------------
 3 files changed, 18 insertions(+), 13 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 target/arm: GICv5 cpuif: Fix overflow in left shift

Coverity points out that we forgot the "ULL" suffix when shifting 1 right by a bitcount in various places, so for bit counts above 31 we end up shifting off the end of the word. Fix the three problems Coverity noticed and one more of the same kind that it didn't.

CID: 1659588, 1659591, 1659559

Fixes ce245ac6957 ("target/arm: GICv5 cpuif: Calculate the highest priority PPI")
Fixes 3f79212abae ("target/arm: GICv5 cpuif: Implement GICR CDIA command")
Fixes 49f4c98648c ("target/arm: GICv5 cpuif: Implement GIC CDDI")
---
 target/arm/tcg/gicv5-cpuif.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/arm/tcg/gicv5-cpuif.c +4 -4
index bc44a7fc11..98238ada19 100644
@@ -275,7 +275,7 @@ static void gic_recalc_ppi_hppi(CPUARMState *env)
             int ppi;
             int bit = ctz64(en_pend_nact);
 
-            en_pend_nact &= ~(1 << bit);
+            en_pend_nact &= ~(1ULL << bit);
 
             ppi = i * 64 + bit;
             prio = extract64(env->gicv5_cpuif.ppi_priority[ppi / 8],
@@ -631,7 +631,7 @@ static uint64_t gicr_cdia_read(CPUARMState *env, const ARMCPRegInfo *ri)
      * gicv5_activate() cause a re-evaluation of HPPIs they use the
      * right (new) running priority.
      */
-    env->gicv5_cpuif.icc_apr[domain] |= (1 << hppi.prio);
+    env->gicv5_cpuif.icc_apr[domain] |= (1ULL << hppi.prio);
     switch (type) {
     case GICV5_PPI:
     {
@@ -639,7 +639,7 @@ static uint64_t gicr_cdia_read(CPUARMState *env, const ARMCPRegInfo *ri)
 
         assert(id < GICV5_NUM_PPIS);
         ppireg = id / 64;
-        ppibit = 1 << (id % 64);
+        ppibit = 1ULL << (id % 64);
 
         env->gicv5_cpuif.ppi_active[ppireg] |= ppibit;
         if (!(env->gicv5_cpuif.ppi_hm[ppireg] & ppibit)) {
@@ -707,7 +707,7 @@ static void gic_cddi_write(CPUARMState *env, const ARMCPRegInfo *ri,
         }
 
         ppireg = id / 64;
-        ppibit = 1 << (id % 64);
+        ppibit = 1ULL << (id % 64);
 
         env->gicv5_cpuif.ppi_active[ppireg] &= ~ppibit;
         gic_recalc_ppi_hppi(env);
Signature · 1 line
2.43.0
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] target/arm: GICv5 cpuif: Fix overflow in left shift

Quoted 5 lines · Peter Maydell

Coverity points out that we forgot the "ULL" suffix when shifting 1 right by a bitcount in various places, so for bit counts above 31 we end up shifting off the end of the word. Fix the three problems Coverity noticed and one more of the same kind that it didn't.

Quoted 8 lines

CID: 1659588, 1659591, 1659559

Fixes ce245ac6957 ("target/arm: GICv5 cpuif: Calculate the highest priority PPI")
Fixes 3f79212abae ("target/arm: GICv5 cpuif: Implement GICR CDIA command")
Fixes 49f4c98648c ("target/arm: GICv5 cpuif: Implement GIC CDDI")
---
  target/arm/tcg/gicv5-cpuif.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
Reviewed-by on v1 1/4
#3 lore raw

Re: [PATCH 1/4] target/arm: GICv5 cpuif: Fix overflow in left shift

In reply to Peter Maydell
Quoted 5 lines · Peter Maydell

Coverity points out that we forgot the "ULL" suffix when shifting 1 right by a bitcount in various places, so for bit counts above 31 we end up shifting off the end of the word. Fix the three problems Coverity noticed and one more of the same kind that it didn't.

Quoted 8 lines

CID: 1659588, 1659591, 1659559

Fixes ce245ac6957 ("target/arm: GICv5 cpuif: Calculate the highest priority PPI")
Fixes 3f79212abae ("target/arm: GICv5 cpuif: Implement GICR CDIA command")
Fixes 49f4c98648c ("target/arm: GICv5 cpuif: Implement GIC CDDI")
---
  target/arm/tcg/gicv5-cpuif.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)

r~

#4 lore raw

PATCH 2/4 target/arm: GICv5 cpuif: Don't set HPPIV bit in GICv5PendingIrq::intid

In reply to Peter Maydell

In gic_hppi() we return the current highest priority pending interrupt in a GICv5PendingIrq struct. We try to set up the intid field of that struct to be the form that is used by the ICC_HPPIR register, which has a "valid" bit in bit 33. Unfortunately the GICv5PendingIrq defines the intid field as a uint32_t, so Coverity points out that the bit doesn't actually fit. Move the handling of the valid bit to the callsite, and make this function report "no pending interrupt" with GICv5PendingIrq::prio == PRIO_IDLE, consistently with how we use this struct in other places.

CID: 1659594

Fixes 9edad4ff3 ("target/arm: GICv5 cpuif: Implement ICC_HPPIR_EL1")
---
 include/hw/intc/arm_gicv5_types.h |  2 ++
 target/arm/tcg/gicv5-cpuif.c      | 18 ++++++++++--------
 2 files changed, 12 insertions(+), 8 deletions(-)
include/hw/intc/arm_gicv5_types.h +2 -0
index eaed42f49f..de4f78a149 100644
@@ -97,6 +97,8 @@ typedef enum GICv5TriggerMode {
  *
  * In this struct the intid includes the interrupt type in bits
  * [31:29] (i.e. it is in the form defined by R_TJPHS).
+ *
+ * "No pending interrupt" is represented by @prio == PRIO_IDLE.
  */
 typedef struct GICv5PendingIrq {
     uint32_t intid;
target/arm/tcg/gicv5-cpuif.c +10 -8
index 98238ada19..dd2f696511 100644
@@ -129,10 +129,9 @@ static GICv5PendingIrq gic_hppi(CPUARMState *env, GICv5Domain domain)
 {
     /*
      * Return the current highest priority pending interrupt for the
-     * specified domain, if it has sufficient priority to preempt. The
-     * intid field of the return value will be in the format of the
-     * ICC_HPPIR register (and will be zero if and only if there is no
-     * interrupt that can preempt).
+     * specified domain, if it has sufficient priority to preempt.
+     * If there is no interrupt that can preempt we signal this by
+     * returning a struct with prio == PRIO_IDLE.
      */
 
     GICv5Common *gic = gicv5_get_gic(env);
@@ -166,7 +165,6 @@ static GICv5PendingIrq gic_hppi(CPUARMState *env, GICv5Domain domain)
         best.prio >= gic_running_prio(env, domain)) {
         return (GICv5PendingIrq) { .intid = 0, .prio = PRIO_IDLE };
     }
-    best.intid |= R_ICC_HPPIR_EL1_HPPIV_MASK;
     return best;
 }
 
@@ -575,7 +573,12 @@ static uint64_t gic_icc_hppir_el1_read(CPUARMState *env, const ARMCPRegInfo *ri)
 {
     GICv5Domain domain = gicv5_logical_domain(env);
     GICv5PendingIrq hppi = gic_hppi(env, domain);
-    return hppi.intid;
+
+    if (hppi.prio == PRIO_IDLE) {
+        /* No valid interrupt */
+        return 0;
+    }
+    return hppi.intid | R_ICC_HPPIR_EL1_HPPIV_MASK;
 }
 
 static bool gic_hppi_is_nmi(CPUARMState *env, GICv5PendingIrq hppi,
@@ -602,13 +605,12 @@ static uint64_t gicr_cdia_read(CPUARMState *env, const ARMCPRegInfo *ri)
 
     bool cdnmia = ri->opc2 == 1;
 
-    if (!hppi.intid) {
+    if (hppi.prio == PRIO_IDLE) {
         /* No interrupt available to acknowledge */
         trace_gicv5_gicr_cdia_fail(domain,
                                    "no available interrupt to acknowledge");
         return 0;
     }
-    assert(hppi.prio != PRIO_IDLE);
 
     if (gic_hppi_is_nmi(env, hppi, domain) != cdnmia) {
         /* GICR CDIA only acknowledges non-NMI; GICR CDNMIA only NMI */
Signature · 1 line
2.43.0
Reviewed-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] target/arm: GICv5 cpuif: Don't set HPPIV bit in GICv5PendingIrq::intid

Quoted 10 lines · Peter Maydell

In gic_hppi() we return the current highest priority pending interrupt in a GICv5PendingIrq struct. We try to set up the intid field of that struct to be the form that is used by the ICC_HPPIR register, which has a "valid" bit in bit 33. Unfortunately the GICv5PendingIrq defines the intid field as a uint32_t, so Coverity points out that the bit doesn't actually fit. Move the handling of the valid bit to the callsite, and make this function report "no pending interrupt" with GICv5PendingIrq::prio == PRIO_IDLE, consistently with how we use this struct in other places.

Quoted 7 lines

CID: 1659594

Fixes 9edad4ff3 ("target/arm: GICv5 cpuif: Implement ICC_HPPIR_EL1")
---
  include/hw/intc/arm_gicv5_types.h |  2 ++
  target/arm/tcg/gicv5-cpuif.c      | 18 ++++++++++--------
  2 files changed, 12 insertions(+), 8 deletions(-)
#6 lore raw

PATCH 3/4 hw/intc/arm_gicv5: Avoid NULL dereference in trace line

In reply to Peter Maydell

In the handling of writes to the IRS_SPI_RESAMPLER register, we call a trace function, passing it information about the SPI being resampled. However, spi could be NULL if the guest tried to resample a nonexistent SPI or one configured for a different domain. Move the trace statement inside the "if (spi)" block, as it's only interesting trace if we actually did a resample and potentially changed the state of the SPI.

CID: 1959593

Fixes 33185e1d64e ("hw/intc/arm_gicv5: Update SPI state for CLEAR/SET events")
---
 hw/intc/arm_gicv5.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/intc/arm_gicv5.c +1 -1
index 493d664625..7ad274e369 100644
@@ -1643,8 +1643,8 @@ static bool config_writel(GICv5 *s, GICv5Domain domain, hwaddr offset,
         if (spi) {
             spi_sample(spi);
             irs_recalc_hppi(s, spi->domain, spi->iaffid);
+            trace_gicv5_spi_state(id, spi->level, spi->pending, spi->active);
         }
-        trace_gicv5_spi_state(id, spi->level, spi->pending, spi->active);
         return true;
     }
     case A_IRS_CR0:
Signature · 1 line
2.43.0
Reviewed-by on v1 3/4
#7 lore raw

Re: [PATCH 3/4] hw/intc/arm_gicv5: Avoid NULL dereference in trace line

Quoted 8 lines · Peter Maydell

In the handling of writes to the IRS_SPI_RESAMPLER register, we call a trace function, passing it information about the SPI being resampled. However, spi could be NULL if the guest tried to resample a nonexistent SPI or one configured for a different domain. Move the trace statement inside the "if (spi)" block, as it's only interesting trace if we actually did a resample and potentially changed the state of the SPI.

Quoted 6 lines

CID: 1959593

Fixes 33185e1d64e ("hw/intc/arm_gicv5: Update SPI state for CLEAR/SET events")
---
  hw/intc/arm_gicv5.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] hw/intc/arm_gicv5: Avoid NULL dereference in trace line

In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

In the handling of writes to the IRS_SPI_RESAMPLER register, we call a trace function, passing it information about the SPI being resampled. However, spi could be NULL if the guest tried to resample a nonexistent SPI or one configured for a different domain. Move the trace statement inside the "if (spi)" block, as it's only interesting trace if we actually did a resample and potentially changed the state of the SPI.

Quoted 6 lines

CID: 1959593

Fixes 33185e1d64e ("hw/intc/arm_gicv5: Update SPI state for CLEAR/SET events")
---
  hw/intc/arm_gicv5.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

#9 lore raw

PATCH 4/4 hw/intc/arm_gicv5: Add missing early return in gicv5_set_handling()

In reply to Peter Maydell

In gicv5_set_handling(), if the guest tried to set the handling mode on a nonexistent SPI then we print a GUEST_ERROR log message. However, we forgot to then return, so execution continues into a NULL pointer dereference.

Add the missing "return", bringing the code structure in to line with the equivalent parts in other functions like gicv5_set_pending() and gicv5_set_target().

CID: 1659596

Fixes 5beb48ab53d ("hw/intc/arm_gicv5: Make gicv5_set_* update SPI state")
---
 hw/intc/arm_gicv5.c | 1 +
 1 file changed, 1 insertion(+)
hw/intc/arm_gicv5.c +1 -0
index 7ad274e369..6b1dd04991 100644
@@ -926,6 +926,7 @@ void gicv5_set_handling(GICv5Common *cs, uint32_t id,
         if (!spi) {
             qemu_log_mask(LOG_GUEST_ERROR, "gicv5_set_handling: tried to set "
                           "priority of unreachable SPI %d\n", id);
+            return;
         }
 
         spi->hm = handling;
Signature · 1 line
2.43.0
Reviewed-by on v1 4/4
#10 lore raw

Re: [PATCH 4/4] hw/intc/arm_gicv5: Add missing early return in gicv5_set_handling()

Quoted 9 lines · Peter Maydell

In gicv5_set_handling(), if the guest tried to set the handling mode on a nonexistent SPI then we print a GUEST_ERROR log message. However, we forgot to then return, so execution continues into a NULL pointer dereference.

Add the missing "return", bringing the code structure in to line with the equivalent parts in other functions like gicv5_set_pending() and gicv5_set_target().

Quoted 6 lines

CID: 1659596

Fixes 5beb48ab53d ("hw/intc/arm_gicv5: Make gicv5_set_* update SPI state")
---
  hw/intc/arm_gicv5.c | 1 +
  1 file changed, 1 insertion(+)
Reviewed-by on v1 4/4
#11 lore raw

Re: [PATCH 4/4] hw/intc/arm_gicv5: Add missing early return in gicv5_set_handling()

In reply to Peter Maydell
Quoted 21 lines · Peter Maydell

In gicv5_set_handling(), if the guest tried to set the handling mode on a nonexistent SPI then we print a GUEST_ERROR log message. However, we forgot to then return, so execution continues into a NULL pointer dereference.

Add the missing "return", bringing the code structure in to line with the equivalent parts in other functions like gicv5_set_pending() and gicv5_set_target().

CID: 1659596

Fixes 5beb48ab53d ("hw/intc/arm_gicv5: Make gicv5_set_* update SPI state")
---
  hw/intc/arm_gicv5.c | 1 +
  1 file changed, 1 insertion(+)
hw/intc/arm_gicv5.c +0 -0
index 7ad274e369..6b1dd04991 100644

hw/intc/arm_gicv5.c void gicv5_set_handling(GICv5Common *cs, uint32_t id, · line 932

Quoted 7 lines
hw/intc/arm_gicv5.c +1 -0
@@ -926,6 +926,7 @@ void gicv5_set_handling(GICv5Common *cs, uint32_t id,
          if (!spi) {
              qemu_log_mask(LOG_GUEST_ERROR, "gicv5_set_handling: tried to set "
                            "priority of unreachable SPI %d\n", id);
+            return;
          }
 
          spi->hm = handling;

r~