Skip to contentSkip to conversation

target/arm: clamp value to account for RES0 fields

RFC PATCH In review Alex Bennée 3 participants 4 messages Jun 16, 2025 – Jun 20, 2025

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 target/arm: clamp value to account for RES0 fields

If the user writes a large value to the register but with the bottom bits unset we could end up with something illegal. By clamping ahead of the check we at least assure we won't assert(bpr > 0) later in the GIC interface code.

---
 hw/intc/arm_gicv3_cpuif.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/intc/arm_gicv3_cpuif.c +4 -1
index 4b4cf09157..165f7e9c2f 100644
@@ -1797,6 +1797,9 @@ static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
     trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
                               gicv3_redist_affid(cs), value);
 
+    /* clamp the value to 2:0, the rest os RES0 */
+    value = deposit64(0, 0, 3, value);
+
     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
         grp = GICV3_G1NS;
     }
@@ -1820,7 +1823,7 @@ static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
         value = minval;
     }
 
-    cs->icc_bpr[grp] = value & 7;
+    cs->icc_bpr[grp] = value;
     gicv3_cpuif_update(cs);
 }
 
Signature · 1 line
2.47.2
#1 lore raw
Quoted 15 lines · Alex Bennée

If the user writes a large value to the register but with the bottom bits unset we could end up with something illegal. By clamping ahead of the check we at least assure we won't assert(bpr > 0) later in the GIC interface code.

---
  hw/intc/arm_gicv3_cpuif.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
hw/intc/arm_gicv3_cpuif.c +0 -0
index 4b4cf09157..165f7e9c2f 100644

hw/intc/arm_gicv3_cpuif.c static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1801

Quoted 5 lines
hw/intc/arm_gicv3_cpuif.c +2 -0
@@ -1797,6 +1797,9 @@ static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
      trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
                                gicv3_redist_affid(cs), value);
 
+    /* clamp the value to 2:0, the rest os RES0 */
+    value = deposit64(0, 0, 3, value);

Surely extract, not deposit.

r~

Quoted 13 lines
hw/intc/arm_gicv3_cpuif.c +2 -1
+
      if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
          grp = GICV3_G1NS;
      }
@@ -1820,7 +1823,7 @@ static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
          value = minval;
      }
 
-    cs->icc_bpr[grp] = value & 7;
+    cs->icc_bpr[grp] = value;
      gicv3_cpuif_update(cs);
  }
 
#2 lore raw
In reply to Alex Bennée
Quoted 16 lines · Alex Bennée

If the user writes a large value to the register but with the bottom bits unset we could end up with something illegal. By clamping ahead of the check we at least assure we won't assert(bpr > 0) later in the GIC interface code.

---
 hw/intc/arm_gicv3_cpuif.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/intc/arm_gicv3_cpuif.c +0 -0
index 4b4cf09157..165f7e9c2f 100644

hw/intc/arm_gicv3_cpuif.c static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1801

Quoted 5 lines
hw/intc/arm_gicv3_cpuif.c +2 -0
@@ -1797,6 +1797,9 @@ static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
     trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
                               gicv3_redist_affid(cs), value);
 
+    /* clamp the value to 2:0, the rest os RES0 */
+    value = deposit64(0, 0, 3, value);

Should be extract64(), as RTH notes (or just &= 7 if you like).

hw/intc/arm_gicv3_cpuif.c static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1827

Quoted 11 lines
hw/intc/arm_gicv3_cpuif.c +2 -1
+
     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
         grp = GICV3_G1NS;
     }
@@ -1820,7 +1823,7 @@ static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
         value = minval;
     }
 
-    cs->icc_bpr[grp] = value & 7;
+    cs->icc_bpr[grp] = value;
     gicv3_cpuif_update(cs);

Yes, I agree we should do the "work only on the 3 bit field" part before we do the "enforce the minimum value" logic.

The handling in icv_bpr_write() has a similar issue.

(Why was your guest writing garbage to this register?)

-- PMM

#3 lore raw
Quoted 40 lines · Peter Maydell
Quoted 16 lines · Alex Bennée

If the user writes a large value to the register but with the bottom bits unset we could end up with something illegal. By clamping ahead of the check we at least assure we won't assert(bpr > 0) later in the GIC interface code.

---
 hw/intc/arm_gicv3_cpuif.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/intc/arm_gicv3_cpuif.c +0 -0
index 4b4cf09157..165f7e9c2f 100644

hw/intc/arm_gicv3_cpuif.c static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1801

Quoted 5 lines
hw/intc/arm_gicv3_cpuif.c +2 -0
@@ -1797,6 +1797,9 @@ static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
     trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
                               gicv3_redist_affid(cs), value);
 
+    /* clamp the value to 2:0, the rest os RES0 */
+    value = deposit64(0, 0, 3, value);

Should be extract64(), as RTH notes (or just &= 7 if you like).

hw/intc/arm_gicv3_cpuif.c static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri, · line 1827

Quoted 11 lines
hw/intc/arm_gicv3_cpuif.c +2 -1
+
     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
         grp = GICV3_G1NS;
     }
@@ -1820,7 +1823,7 @@ static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
         value = minval;
     }
 
-    cs->icc_bpr[grp] = value & 7;
+    cs->icc_bpr[grp] = value;
     gicv3_cpuif_update(cs);

Yes, I agree we should do the "work only on the 3 bit field" part before we do the "enforce the minimum value" logic.

Quoted 3 lines

The handling in icv_bpr_write() has a similar issue.

(Why was your guest writing garbage to this register?)

Heh, it was writing the SP instead of the XR, this is how I found out I was getting it wrong ;-)

Quoted 2 lines

-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro