Skip to contentSkip to conversation

target/riscv: preserve interrupt-pending state under qtest

PATCH New Zixin Wang 1 participant 3 messages Nov 23

Subsystems (2)
lore
Share & tools
lore

Zixin Wang

author
#0 lore raw

cover target/riscv: preserve interrupt-pending state under qtest

Commit 6c91b423a9a4 ("target/riscv: gate riscv_cpu_update_mip with tcg_enabled()") gated the architectural mip updates with tcg_enabled(). The qtest accelerator is neither TCG nor KVM, so device models still inject interrupts but mip is never updated. qtests that drive ACLINT, APLIC or IMSIC inputs therefore observe stale mip values.

Patch 1/2 updates the call sites in riscv_cpu_set_irq() and riscv_accel_set_irq() to use "tcg_enabled() || qtest_enabled()", matching the existing virt_aclint_allowed() logic. The update blocks are wrapped in "#ifdef CONFIG_TCG" because riscv_cpu_update_mip() is only built with TCG; without this compile-time guard, the new qtest_enabled() runtime check keeps the call in the link and breaks --disable-tcg builds. In --disable-tcg builds the update remains compiled out; the qtest "csr" command is unavailable there as well, so those builds keep their previous behavior.

Patch 2/2 adds a qtest regression that drives the ACLINT MSIP and MTIMER inputs and checks mip through the qtest "csr" command. It fails with mip == 0 without patch 1/2 and passes with it. The "csr" command is only registered in TCG-enabled builds, so the test is only registered when TCG is enabled.

Validation on 08c9bbb825 (v11.2.0-rc1):

- TCG build, qtest reproducers from the report:
MSIP: OK / OK / OK 0 8
MTIP: OK / OK 1000000 / OK 0 80
(both read mip as 0 without patch 1/2)

- TCG build, new regression test:
/riscv64/riscv/mip/msip and /riscv64/riscv/mip/mtip pass with patch 1/2; without it, the MSIP assertion fails with mip == 0.

- Full riscv64 qtest suite:
18 tests passed, 1 skipped (cdrom-test, registers no subtests in this configuration), 0 failed; 375 subtests passed in total. This includes the K230, MilkV Duo, IOMMU, riscv-csr, ACPI and virt device tests.

- --disable-tcg build:
without the CONFIG_TCG guard, the link fails with undefined references to riscv_cpu_update_mip from target/riscv/cpu.c and target/riscv/time_helper.c;
with the guard, qemu-system-riscv64 links successfully.

The test host is x86_64, where KVM cannot be enabled for riscv64, so the link test used a local no-accelerator configure; the compile-time condition (CONFIG_TCG undefined, target/riscv/tcg/ excluded) is the same as in a RISC-V KVM-only build. I can re-test on a RISC-V host if that is preferred.

Reported as: https://gitlab.com/qemu-project/qemu/-/issues/4929

Zixin Wang (2):
target/riscv: preserve interrupt-pending state under qtest tests/qtest: add a RISC-V mip regression test

 target/riscv/cpu.c           | 13 +++++--
 target/riscv/time_helper.c   |  5 ++-
 tests/qtest/meson.build      |  1 +
 tests/qtest/riscv-mip-test.c | 71 ++++++++++++++++++++++++++++++++++++
 4 files changed, 86 insertions(+), 4 deletions(-)
 create mode 100644 tests/qtest/riscv-mip-test.c
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH 1/2 target/riscv: preserve interrupt-pending state under qtest

The qtest accelerator is neither TCG nor KVM, so the mip updates gated by tcg_enabled() in commit 6c91b423a9a4 are skipped even though device models still inject interrupts. Treat qtest like TCG for the architectural pending-state updates in TCG-enabled builds, where the qtest csr command is available.

The update blocks are additionally wrapped in #ifdef CONFIG_TCG because riscv_cpu_update_mip() is only built with TCG. Without this guard, the runtime qtest_enabled() check keeps the reference in the link and breaks --disable-tcg builds.

Fixes 6c91b423a9a4 ("target/riscv: gate riscv_cpu_update_mip with tcg_enabled()")

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4929

---
 target/riscv/cpu.c         | 13 ++++++++++---
 target/riscv/time_helper.c |  5 ++++-
 2 files changed, 14 insertions(+), 4 deletions(-)
target/riscv/cpu.c +10 -3
index 15c27f4ce4..a8eba90704 100644
@@ -39,6 +39,7 @@
 #include "fpu/softfloat-helpers.h"
 #include "system/device_tree.h"
 #include "system/kvm.h"
+#include "system/qtest.h"
 #include "system/tcg.h"
 #include "kvm/kvm_riscv.h"
 #include "tcg/tcg-cpu.h"
@@ -1459,20 +1460,24 @@ static void riscv_cpu_set_irq(void *opaque, int irq, int level)
                 kvm_riscv_set_irq(cpu, irq, level);
             }
 
-            if (tcg_enabled()) {
+#ifdef CONFIG_TCG
+            if (tcg_enabled() || qtest_enabled()) {
                 riscv_cpu_update_mip(env, 1 << irq, BOOL_TO_MASK(level));
             }
+#endif
              break;
         case IRQ_S_EXT:
             if (kvm_enabled()) {
                 kvm_riscv_set_irq(cpu, irq, level);
             }
 
-            if (tcg_enabled()) {
+#ifdef CONFIG_TCG
+            if (tcg_enabled() || qtest_enabled()) {
                 env->external_seip = level;
                 riscv_cpu_update_mip(env, 1 << irq,
                                      BOOL_TO_MASK(level | env->software_seip));
             }
+#endif
             break;
         default:
             g_assert_not_reached();
@@ -1498,11 +1503,13 @@ static void riscv_cpu_set_irq(void *opaque, int irq, int level)
         if (kvm_enabled()) {
             kvm_riscv_set_irq(cpu, irq, level);
         }
-        if (tcg_enabled()) {
+#ifdef CONFIG_TCG
+        if (tcg_enabled() || qtest_enabled()) {
             /* Update mip.SGEIP bit */
             riscv_cpu_update_mip(env, MIP_SGEIP,
                                  BOOL_TO_MASK(!!(env->hgeie & env->hgeip)));
         }
+#endif
     } else {
         g_assert_not_reached();
     }
target/riscv/time_helper.c +4 -1
index 29bbebb96f..579e05f7ee 100644
@@ -23,6 +23,7 @@
 #include "hw/intc/riscv_aclint.h"
 #include "kvm/kvm_riscv.h"
 #include "system/kvm.h"
+#include "system/qtest.h"
 #include "system/tcg.h"
 
 static void riscv_accel_set_irq(RISCVCPU *cpu,
@@ -35,12 +36,14 @@ static void riscv_accel_set_irq(RISCVCPU *cpu,
         kvm_riscv_set_irq(cpu, irq, level);
     }
 
-    if (tcg_enabled()) {
+#ifdef CONFIG_TCG
+    if (tcg_enabled() || qtest_enabled()) {
         uint64_t mask = irq_or_mask;
         uint64_t value = level_or_value;
 
         riscv_cpu_update_mip(&cpu->env, mask, value);
     }
+#endif
 }
 
 static void riscv_vstimer_cb(void *opaque)
Signature · 1 line
2.54.0
author
#2 lore raw

PATCH 2/2 tests/qtest: add a RISC-V mip regression test

In reply to Zixin Wang

Drive the ACLINT MSIP and MTIMER inputs under the qtest accelerator and check mip through the qtest "csr" command. The test fails while the architectural pending state is not updated and passes with the corresponding fix.

The "csr" command is only registered in CONFIG_TCG builds, so register this test only when TCG is enabled.

---
 tests/qtest/meson.build      |  1 +
 tests/qtest/riscv-mip-test.c | 71 ++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
 create mode 100644 tests/qtest/riscv-mip-test.c
tests/qtest/meson.build +1 -0
index 6e2938d913..c5e2cd6bed 100644
@@ -320,6 +320,7 @@ qtests_riscv32 = \
   (config_all_devices.has_key('CONFIG_SIFIVE_E_AON') ? ['sifive-e-aon-watchdog-test'] : [])
 
 qtests_riscv64 = ['riscv-csr-test'] + \
+  (config_all_accel.has_key('CONFIG_TCG') ? ['riscv-mip-test'] : []) + \
   (unpack_edk2_blobs ? ['bios-tables-test'] : []) + \
   (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
    config_all_devices.has_key('CONFIG_RISCV_IOMMU') ?
tests/qtest/riscv-mip-test.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.54.0