Skip to contentSkip to conversation

intc/riscv_aplic: Fix target register read when source is inactive

PATCH Superseded Yang Jialong 2 participants 3 messages Jul 24, 2025 – Jul 25, 2025 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 intc/riscv_aplic: Fix target register read when source is inactive

The RISC-V Advanced interrupt Architecture:
4.5.16. Interrupt targets:
If interrupt source i is inactive in this domain, register target[i] is read-only zero.

---
 hw/intc/riscv_aplic.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/intc/riscv_aplic.c +5 -1
index 4fa5f75..cfef69f 100644
@@ -628,7 +628,7 @@ static void riscv_aplic_request(void *opaque, int irq, int level)
 
 static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size)
 {
-    uint32_t irq, word, idc;
+    uint32_t irq, word, idc, sm;
     RISCVAPLICState *aplic = opaque;
 
     /* Reads must be 4 byte words */
@@ -696,6 +696,10 @@ static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size)
     } else if ((APLIC_TARGET_BASE <= addr) &&
             (addr < (APLIC_TARGET_BASE + (aplic->num_irqs - 1) * 4))) {
         irq = ((addr - APLIC_TARGET_BASE) >> 2) + 1;
+        sm = aplic->sourcecfg[irq] * APLIC_SOURCECFG_SM_MASK;
+        if (sm == APLIC_SOURCECFG_SM_INACTIVE) {
+            return 0;
+        }
         return aplic->target[irq];
     } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) &&
             (addr < (APLIC_IDC_BASE + aplic->num_harts * APLIC_IDC_SIZE))) {
Signature · 1 line
2.34.1
#1 lore raw
Quoted 24 lines · Yang Jialong

The RISC-V Advanced interrupt Architecture:
4.5.16. Interrupt targets:
If interrupt source i is inactive in this domain, register target[i] is read-only zero.

---
  hw/intc/riscv_aplic.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
hw/intc/riscv_aplic.c +1 -1
index 4fa5f75..cfef69f 100644
@@ -628,7 +628,7 @@ static void riscv_aplic_request(void *opaque, int irq, int level)
 
  static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size)
  {
-    uint32_t irq, word, idc;
+    uint32_t irq, word, idc, sm;
      RISCVAPLICState *aplic = opaque;
 
      /* Reads must be 4 byte words */

hw/intc/riscv_aplic.c static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size) · line 699

Quoted 4 lines
hw/intc/riscv_aplic.c +1 -0
@@ -696,6 +696,10 @@ static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size)
      } else if ((APLIC_TARGET_BASE <= addr) &&
              (addr < (APLIC_TARGET_BASE + (aplic->num_irqs - 1) * 4))) {
          irq = ((addr - APLIC_TARGET_BASE) >> 2) + 1;
+        sm = aplic->sourcecfg[irq] * APLIC_SOURCECFG_SM_MASK;

I believe you want '&' here:

sm = aplic->sourcecfg[irq] & APLIC_SOURCECFG_SM_MASK;

Otherwise, given that APLIC_SOURCECFG_SM_INACTIVE is 0x0, the only way

sm == APLIC_SOURCECFG_SM_INACTIVE

will happen is aplic->sourcecfg[irq] being 0.

Thanks,

Daniel

Quoted 6 lines
hw/intc/riscv_aplic.c +3 -0
+        if (sm == APLIC_SOURCECFG_SM_INACTIVE) {
+            return 0;
+        }
          return aplic->target[irq];
      } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) &&
              (addr < (APLIC_IDC_BASE + aplic->num_harts * APLIC_IDC_SIZE))) {
#2 lore raw
Quoted 23 lines · Yang Jialong
Quoted 22 lines

The RISC-V Advanced interrupt Architecture:
4.5.16. Interrupt targets:
If interrupt source i is inactive in this domain, register target[i] is read-only zero.

---
   hw/intc/riscv_aplic.c | 6 +++++-

   1 file changed, 5 insertions(+), 1 deletion(-)
hw/intc/riscv_aplic.c +0 -0
index 4fa5f75..cfef69f 100644
@@ -628,7 +628,7 @@ static void riscv_aplic_request(void *opaque, int irq, int level)


   static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size) {
-    uint32_t irq, word, idc;
+    uint32_t irq, word, idc, sm;
       RISCVAPLICState *aplic = opaque;

Quoted 9 lines

hw/intc/riscv_aplic.c static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size) · line 699

Quoted 6 lines

       /* Reads must be 4 byte words */

hw/intc/riscv_aplic.c +1 -0
@@ -696,6 +696,10 @@ static uint64_t riscv_aplic_read(void *opaque, hwaddr addr, unsigned size)
        } else if ((APLIC_TARGET_BASE <= addr) &&
                (addr < (APLIC_TARGET_BASE + (aplic->num_irqs - 1) * 4))) {
            irq = ((addr - APLIC_TARGET_BASE) >> 2) + 1;
+        sm = aplic->sourcecfg[irq] * APLIC_SOURCECFG_SM_MASK;

I believe you want '&' here:

... Yes. Thanks. I will submit a newer one.

Quoted 20 lines

sm = aplic->sourcecfg[irq] & APLIC_SOURCECFG_SM_MASK;

Otherwise, given that APLIC_SOURCECFG_SM_INACTIVE is 0x0, the only way

sm == APLIC_SOURCECFG_SM_INACTIVE

will happen is aplic->sourcecfg[irq] being 0.

Thanks,

Daniel

Quoted 6 lines
hw/intc/riscv_aplic.c +3 -0
+        if (sm == APLIC_SOURCECFG_SM_INACTIVE) {
+            return 0;
+        }
            return aplic->target[irq];
        } else if (!aplic->msimode && (APLIC_IDC_BASE <= addr) &&
                (addr < (APLIC_IDC_BASE + aplic->num_harts * APLIC_IDC_SIZE))) {