bcm2835_systmr_reset() cleared the match bits and the compare
registers, but a channel whose match had raised its interrupt kept the
line raised, and a compare armed before the reset still fired after
it, setting its match bit and raising the interrupt in a guest that
had not armed it. Both happen when a guest reboots with a system timer
channel in use (the firmware or a bare-metal guest's tick).
Delete the timers and lower the interrupts in the reset, as the
register values now say.
Test that a reset drops a raised channel and that a compare set before
it does not fire.
Fixes: be95dffa326a ("hw/timer/bcm2835: Support the timer COMPARE registers")
Signed-off-by: Lorand Marton <[email protected]>
---
hw/timer/bcm2835_systmr.c | 4 +++
tests/qtest/bcm2835-systmr-test.c | 54 +++++++++++++++++++++++++++++++
tests/qtest/meson.build | 2 +-
3 files changed, 59 insertions(+), 1 deletion(-)
create mode 100644 tests/qtest/bcm2835-systmr-test.c
diff --git a/hw/timer/bcm2835_systmr.c b/hw/timer/bcm2835_systmr.c
index 06a919b1f8..3c03c98193 100644
--- a/hw/timer/bcm2835_systmr.c
+++ b/hw/timer/bcm2835_systmr.c
@@ -123,6 +123,10 @@ static void bcm2835_systmr_reset(DeviceState *dev)
BCM2835SystemTimerState *s = BCM2835_SYSTIMER(dev);
memset(&s->reg, 0, sizeof(s->reg));
+ for (size_t i = 0; i < ARRAY_SIZE(s->tmr); i++) {
+ timer_del(&s->tmr[i].timer);
+ qemu_set_irq(s->tmr[i].irq, 0);
+ }
}
static void bcm2835_systmr_realize(DeviceState *dev, Error **errp)
diff --git a/tests/qtest/bcm2835-systmr-test.c
b/tests/qtest/bcm2835-systmr-test.c
new file mode 100644
index 0000000000..a70fc46b92
--- /dev/null
+++ b/tests/qtest/bcm2835-systmr-test.c
@@ -0,0 +1,54 @@
+/*
+ * QTest testcase for the BCM2835 system timer
+ *
+ * A compare match raises the interrupt of its channel; a reset clears the
+ * matches, lowers the interrupts and cancels the pending compares.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/bitops.h"
+#include "libqtest.h"
+
+#define SYSTMR_BASE 0xfe003000
+#define SYSTMR_PATH "/machine/soc/peripherals/systimer"
+#define CS 0x00
+#define CLO 0x04
+#define C(n) (0x0c + (n) * 4)
+
+#define US 1000 /* the timer counts microseconds */
+
+static void test_reset(void)
+{
+ QTestState *qts = qtest_init("-M raspi4b");
+ uint32_t clo;
+
+ qtest_irq_intercept_out_named(qts, SYSTMR_PATH, "sysbus-irq");
+
+ clo = qtest_readl(qts, SYSTMR_BASE + CLO);
+ qtest_writel(qts, SYSTMR_BASE + C(1), clo + 1000);
+ qtest_writel(qts, SYSTMR_BASE + C(3), clo + 100000);
+ qtest_clock_step(qts, 2000 * US);
+ g_assert_cmphex(qtest_readl(qts, SYSTMR_BASE + CS), ==, BIT(1));
+ g_assert_true(qtest_get_irq(qts, 1));
+ g_assert_false(qtest_get_irq(qts, 3));
+
+ qtest_system_reset(qts);
+ g_assert_cmphex(qtest_readl(qts, SYSTMR_BASE + CS), ==, 0);
+ g_assert_false(qtest_get_irq(qts, 1));
+
+ /* the compare of channel 3 was set before the reset */
+ qtest_clock_step(qts, 200000 * US);
+ g_assert_cmphex(qtest_readl(qts, SYSTMR_BASE + CS), ==, 0);
+ g_assert_false(qtest_get_irq(qts, 3));
+
+ qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+ g_test_init(&argc, &argv, NULL);
+ qtest_add_func("/bcm2835-systmr/reset", test_reset);
+ return g_test_run();
+}
diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
index d4a20cd408..76448c5d4f 100644
--- a/tests/qtest/meson.build
+++ b/tests/qtest/meson.build
@@ -286,7 +286,7 @@ qtests_aarch64 = \
(config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test',
'fuzz-xlnx-dp-test'] : []) + \
(config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test',
'xlnx-versal-trng-test'] : []) + \
(config_all_devices.has_key('CONFIG_RASPI') ? ['bcm2835-dma-test',
'bcm2835-i2c-test', 'bcm2835-ic-test',
- 'bcm2835-spi-test'] : [])
+ \
+ 'bcm2835-spi-test',
'bcm2835-systmr-test'] : []) + \
(config_all_accel.has_key('CONFIG_TCG') and
\
config_all_devices.has_key('CONFIG_TPM_TIS_I2C') ? ['tpm-tis-i2c-test',
'tpm-tis-i2c-swtpm-test'] : []) + \
(config_all_devices.has_key('CONFIG_ASPEED_SOC') ? qtests_aspeed64 : []) + \
--
2.56.0