Skip to contentSkip to conversation

target/loongarch: Fix SWI interrupt delivery via CSR_ESTAT

PATCH Applied Andrew S. Rightenburg via qemu development 3 participants 5 messages Apr 15 – Aug 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/loongarch: Fix SWI interrupt delivery via CSR_ESTAT

In TCG mode, helper_csrwr_estat() updates CSR.ESTAT.IS[1:0] (SWI0/SWI1) when the guest writes CSR_ESTAT, but it did not update the CPU interrupt request state. As a result, software interrupts could be observed as pending in CSR.ESTAT while no interrupt exception was taken.

Update CPU_INTERRUPT_HARD after modifying CSR_ESTAT, matching the behavior of loongarch_cpu_set_irq(). The helper runs without the Big QEMU Lock (BQL), so take the BQL while calling cpu_interrupt().

Fixes 5b1dedfe848b ("target/loongarch: Add LoongArch CSR instruction")
---
 target/loongarch/tcg/csr_helper.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
target/loongarch/tcg/csr_helper.c +12 -0
index cd35ca93c7..0a5ed52eb6 100644
@@ -98,10 +98,22 @@ target_ulong helper_csrrd_msgir(CPULoongArchState *env)
 target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
 {
     int64_t old_v = env->CSR_ESTAT;
+    CPUState *cs = env_cpu(env);
 
     /* Only IS[1:0] can be written */
     env->CSR_ESTAT = deposit64(env->CSR_ESTAT, 0, 2, val);
 
+    /*
+     * Software interrupts (SWI0/SWI1) are latched in CSR.ESTAT.IS[1:0].
+     * Make sure the CPU interrupt request state tracks the pending bits,
+     * matching the behavior of loongarch_cpu_set_irq().
+     */
+    if (FIELD_EX64(env->CSR_ESTAT, CSR_ESTAT, IS)) {
+        bql_lock();
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+        bql_unlock();
+    }
+
     return old_v;
 }
 
Signature · 1 line
2.47.3
Applied
#1 lore raw

applied to loongarch-next with small modification.

target/loongarch/tcg/csr_helper.c +13 -0
index 7dc33bc180..d8d54d81e0 100644
@@ -103,9 +103,22 @@ target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
 {
     CPUSysState *sys = env_sys(env);
     int64_t old_v = sys->CSR_ESTAT;
+    CPUState *cs = env_cpu(env);
 
     /* Only IS[1:0] can be written */
     sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, 0, 2, val);
+    /*
+     * Software interrupts (SWI0/SWI1) are latched in CSR.ESTAT.IS[1:0].
+     * Make sure the CPU interrupt request state tracks the pending bits,
+     * matching the behavior of loongarch_cpu_set_irq().
+     */
+    bql_lock();
+    if (FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS)) {
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+    } else {
+        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
+    }
+    bql_unlock();
 
     return old_v;
 }
Quoted 44 lines · Andrew S. Rightenburg via qemu development

In TCG mode, helper_csrwr_estat() updates CSR.ESTAT.IS[1:0] (SWI0/SWI1) when the guest writes CSR_ESTAT, but it did not update the CPU interrupt request state. As a result, software interrupts could be observed as pending in CSR.ESTAT while no interrupt exception was taken.

Update CPU_INTERRUPT_HARD after modifying CSR_ESTAT, matching the behavior of loongarch_cpu_set_irq(). The helper runs without the Big QEMU Lock (BQL), so take the BQL while calling cpu_interrupt().

Fixes 5b1dedfe848b ("target/loongarch: Add LoongArch CSR instruction")
---
  target/loongarch/tcg/csr_helper.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
target/loongarch/tcg/csr_helper.c +12 -0
index cd35ca93c7..0a5ed52eb6 100644
@@ -98,10 +98,22 @@ target_ulong helper_csrrd_msgir(CPULoongArchState *env)
  target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
  {
      int64_t old_v = env->CSR_ESTAT;
+    CPUState *cs = env_cpu(env);
 
      /* Only IS[1:0] can be written */
      env->CSR_ESTAT = deposit64(env->CSR_ESTAT, 0, 2, val);
 
+    /*
+     * Software interrupts (SWI0/SWI1) are latched in CSR.ESTAT.IS[1:0].
+     * Make sure the CPU interrupt request state tracks the pending bits,
+     * matching the behavior of loongarch_cpu_set_irq().
+     */
+    if (FIELD_EX64(env->CSR_ESTAT, CSR_ESTAT, IS)) {
+        bql_lock();
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+        bql_unlock();
+    }
+
      return old_v;
  }
 
#2 lore raw
Quoted 31 lines · Bibo Mao

applied to loongarch-next with small modification.

target/loongarch/tcg/csr_helper.c +13 -0
index 7dc33bc180..d8d54d81e0 100644
@@ -103,9 +103,22 @@ target_ulong helper_csrwr_estat(CPULoongArchState 
 *env, target_ulong val)
  {
      CPUSysState *sys = env_sys(env);
      int64_t old_v = sys->CSR_ESTAT;
+    CPUState *cs = env_cpu(env);
 
      /* Only IS[1:0] can be written */
      sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, 0, 2, val);
+    /*
+     * Software interrupts (SWI0/SWI1) are latched in CSR.ESTAT.IS[1:0].
+     * Make sure the CPU interrupt request state tracks the pending bits,
+     * matching the behavior of loongarch_cpu_set_irq().
+     */
+    bql_lock();
+    if (FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS)) {
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+    } else {
+        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
+    }
+    bql_unlock();
 
      return old_v;

 }

Quoted 9 lines
Quoted 8 lines · Andrew S. Rightenburg via qemu development

In TCG mode, helper_csrwr_estat() updates CSR.ESTAT.IS[1:0] (SWI0/SWI1) when the guest writes CSR_ESTAT, but it did not update the CPU interrupt request state. As a result, software interrupts could be observed as pending in CSR.ESTAT while no interrupt exception was taken.

Update CPU_INTERRUPT_HARD after modifying CSR_ESTAT, matching the behavior of loongarch_cpu_set_irq(). The helper runs without the Big QEMU Lock (BQL), so take the BQL while calling cpu_interrupt().

Eh I don't understand this justification about taking the BQL...

If we can not call loongarch_cpu_set_irq() directly, we should extract the common code in a common method and re-use, not duplicate the same code.

Quoted 37 lines
Quoted 35 lines
Fixes 5b1dedfe848b ("target/loongarch: Add LoongArch CSR instruction")

---
  target/loongarch/tcg/csr_helper.c | 12 ++++++++++++

  1 file changed, 12 insertions(+)
target/loongarch/tcg/csr_helper.c +12 -0
index cd35ca93c7..0a5ed52eb6 100644
@@ -98,10 +98,22 @@ target_ulong helper_csrrd_msgir(CPULoongArchState 
 *env)
   target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
   {
       int64_t old_v = env->CSR_ESTAT;
+    CPUState *cs = env_cpu(env);
       /* Only IS[1:0] can be written */
       env->CSR_ESTAT = deposit64(env->CSR_ESTAT, 0, 2, val);
+    /*
+     * Software interrupts (SWI0/SWI1) are latched in CSR.ESTAT.IS[1:0].
+     * Make sure the CPU interrupt request state tracks the pending 
 bits,
+     * matching the behavior of loongarch_cpu_set_irq().
+     */
+    if (FIELD_EX64(env->CSR_ESTAT, CSR_ESTAT, IS)) {
+        bql_lock();
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+        bql_unlock();
+    }
+
       return old_v;
   }
 
#3 lore raw
Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 38 lines · Bibo Mao

applied to loongarch-next with small modification.

target/loongarch/tcg/csr_helper.c +13 -0
index 7dc33bc180..d8d54d81e0 100644
@@ -103,9 +103,22 @@ target_ulong helper_csrwr_estat(CPULoongArchState 
 *env, target_ulong val)
   {
       CPUSysState *sys = env_sys(env);
       int64_t old_v = sys->CSR_ESTAT;
+    CPUState *cs = env_cpu(env);
 
       /* Only IS[1:0] can be written */
       sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, 0, 2, val);
+    /*
+     * Software interrupts (SWI0/SWI1) are latched in CSR.ESTAT.IS[1:0].
+     * Make sure the CPU interrupt request state tracks the pending 
 bits,
+     * matching the behavior of loongarch_cpu_set_irq().
+     */
+    bql_lock();
+    if (FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS)) {
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+    } else {
+        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
+    }
+    bql_unlock();
 

      return old_v;
  }

Quoted 5 lines · Andrew S. Rightenburg via qemu development

In TCG mode, helper_csrwr_estat() updates CSR.ESTAT.IS[1:0] (SWI0/SWI1) when the guest writes CSR_ESTAT, but it did not update the CPU interrupt request state. As a result, software interrupts could be observed as pending in CSR.ESTAT while no interrupt exception was taken.

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Update CPU_INTERRUPT_HARD after modifying CSR_ESTAT, matching the behavior of loongarch_cpu_set_irq(). The helper runs without the Big QEMU Lock (BQL), so take the BQL while calling cpu_interrupt().

Eh I don't understand this justification about taking the BQL...

I guess there is g_assert(bql_locked()) in cpu_interrupt() -:)

Quoted 4 lines

If we can not call loongarch_cpu_set_irq() directly, we should extract the common code in a common method and re-use, not duplicate the same code.

yes, I think it is better to call loongarch_cpu_set_irq() directly to avoid duplicated code.

Regards
Bibo Mao

Quoted 39 lines
Quoted 39 lines
Quoted 37 lines
Fixes 5b1dedfe848b ("target/loongarch: Add LoongArch CSR instruction")

---
  target/loongarch/tcg/csr_helper.c | 12 ++++++++++++

  1 file changed, 12 insertions(+)
target/loongarch/tcg/csr_helper.c +3 -0
index cd35ca93c7..0a5ed52eb6 100644
@@ -98,10 +98,22 @@ target_ulong helper_csrrd_msgir(CPULoongArchState 
 *env)
   target_ulong helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
   {
       int64_t old_v = env->CSR_ESTAT;
+    CPUState *cs = env_cpu(env);
       /* Only IS[1:0] can be written */
       env->CSR_ESTAT = deposit64(env->CSR_ESTAT, 0, 2, val);
+    /*
+     * Software interrupts (SWI0/SWI1) are latched in 

CSR.ESTAT.IS[1:0].
+     * Make sure the CPU interrupt request state tracks the pending bits,
+     * matching the behavior of loongarch_cpu_set_irq(). +     */
+    if (FIELD_EX64(env->CSR_ESTAT, CSR_ESTAT, IS)) {
+        bql_lock();
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+        bql_unlock();
+    }
+
      return old_v;
  }

#4 lore raw
Quoted 43 lines · Bibo Mao
Quoted 40 lines · Philippe Mathieu-Daudé
Quoted 39 lines · Bibo Mao

applied to loongarch-next with small modification.

target/loongarch/tcg/csr_helper.c +3 -0
index 7dc33bc180..d8d54d81e0 100644
@@ -103,9 +103,22 @@ target_ulong 
 helper_csrwr_estat(CPULoongArchState *env, target_ulong val)
   {
       CPUSysState *sys = env_sys(env);
       int64_t old_v = sys->CSR_ESTAT;
+    CPUState *cs = env_cpu(env);
 
       /* Only IS[1:0] can be written */
       sys->CSR_ESTAT = deposit64(sys->CSR_ESTAT, 0, 2, val);
+    /*
+     * Software interrupts (SWI0/SWI1) are latched in 

CSR.ESTAT.IS[1:0].
+     * Make sure the CPU interrupt request state tracks the pending bits,
+     * matching the behavior of loongarch_cpu_set_irq(). +     */
+    bql_lock();
+    if (FIELD_EX64(sys->CSR_ESTAT, CSR_ESTAT, IS)) {
+        cpu_interrupt(cs, CPU_INTERRUPT_HARD);
+    } else {
+        cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
+    }
+    bql_unlock();

      return old_v;
  }

Quoted 5 lines · Andrew S. Rightenburg via qemu development

In TCG mode, helper_csrwr_estat() updates CSR.ESTAT.IS[1:0] (SWI0/SWI1) when the guest writes CSR_ESTAT, but it did not update the CPU interrupt request state. As a result, software interrupts could be observed as pending in CSR.ESTAT while no interrupt exception was taken.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Update CPU_INTERRUPT_HARD after modifying CSR_ESTAT, matching the behavior of loongarch_cpu_set_irq(). The helper runs without the Big QEMU Lock (BQL), so take the BQL while calling cpu_interrupt().

Eh I don't understand this justification about taking the BQL...

I guess there is g_assert(bql_locked()) in cpu_interrupt() -:)

Hmm OK, thanks.

Quoted 8 lines
Quoted 4 lines

If we can not call loongarch_cpu_set_irq() directly, we should extract the common code in a common method and re-use, not duplicate the same code.

yes, I think it is better to call loongarch_cpu_set_irq() directly to avoid duplicated code.

Regards
Bibo Mao