From ed37ee0c98bf2b6162ff889d911122ddf6ae5d2a Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 3 Nov 2026 00:39:37 +0000 Subject: [PATCH] [ot] hw/opentitan: rstmgr: defer reset bus and align combo timing Fix race conditions and timing discrepancies during reset handling in `ot_rstmgr` and `ot_sysrst_ctrl`: - In `ot_rstmgr_reset_bus`: when called while the vCPU is running (`s->cpu->running`), queue the bus reset via `async_run_on_cpu` and issue `cpu_exit(s->cpu)`. This prevents synchronous reset handling during an in-flight TCG translation block, avoiding PC corruption (`0x8080` -> `0x8084` via `CF_PCREL` `+4` epilogue) and subsequent illegal instruction exceptions in mask ROM. - In `ot_rstmgr_reset_req`: when on the vCPU thread (`qemu_cpu_is_self`), only call `cpu_exit(s->cpu)` instead of `cpu_pause(s->cpu)` to avoid prematurely setting `s->cpu->stopped = true` before the execution loop has unwound. - Clean up the vCPU stop-wait logic in `ot_rstmgr_reset_bus` to guard the wait loop on `!qemu_cpu_is_self && !s->cpu->stopped`, avoiding self-deadlock and ensuring `s->cpu->stop = false` across all paths. - In `ot_sysrst_ctrl`: switch `combo_rst_timer` from `QEMU_CLOCK_REALTIME` + hardcoded 3 ms to `OT_VIRTUAL_CLOCK` using `(rst_det_cyc + 1) * 5000 ns` to accurately match the 200 kHz AON clock cycle timing in `COM_DET_CTL`. - In `ot_sysrst_ctrl`: delete `combo_rst_timer` and lower `rst_req` on combo deassertion (`prev_trig && !curr_trig`), matching `sysrst_ctrl_combo.sv`, and remove ungrounded timer re-arming on input pin changes. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I8c6e38a670d60bedf5133ebd6ea32d046a6a6964 --- hw/opentitan/ot_rstmgr.c | 42 +++++++++++++++++++++++++++++++---- hw/opentitan/ot_sysrst_ctrl.c | 14 +++++------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/hw/opentitan/ot_rstmgr.c b/hw/opentitan/ot_rstmgr.c index fd78a263d4d7d..a6d758567d0f3 100644 --- a/hw/opentitan/ot_rstmgr.c +++ b/hw/opentitan/ot_rstmgr.c @@ -37,6 +37,7 @@ #include "qemu/timer.h" #include "qemu/typedefs.h" #include "qapi/error.h" +#include "hw/core/cpu.h" #include "hw/opentitan/ot_alert.h" #include "hw/opentitan/ot_common.h" #include "hw/opentitan/ot_i2c.h" @@ -173,6 +174,7 @@ struct OtRstMgrState { QEMUBH *bus_reset_bh; QEMUTimer *sw_reset_timer; CPUState *cpu; + bool reset_bus_queued; uint32_t *regs; uint32_t cpu_info_dump[8]; @@ -330,13 +332,41 @@ static void ot_rstmgr_sw_reset_timer_cb(void *opaque) extern void riscv_cpu_get_crash_dump(CPUState *cs, uint32_t dump[8]); +static void ot_rstmgr_reset_bus(void *opaque); + +static void ot_rstmgr_reset_bus_on_cpu(CPUState *cpu, run_on_cpu_data data) +{ + OtRstMgrState *s = data.host_ptr; + + (void)cpu; + if (!s->reset_bus_queued) { + return; + } + s->reset_bus_queued = false; + ot_rstmgr_reset_bus(s); +} + static void ot_rstmgr_reset_bus(void *opaque) { OtRstMgrState *s = opaque; g_assert(s->cpu); - if (!s->cpu->stopped) { + if (s->cpu->running) { + if (!s->reset_bus_queued) { + s->reset_bus_queued = true; + cpu_exit(s->cpu); + async_run_on_cpu(s->cpu, &ot_rstmgr_reset_bus_on_cpu, + RUN_ON_CPU_HOST_PTR(s)); + } + return; + } + + if (s->reset_bus_queued) { + return; + } + + if (!qemu_cpu_is_self(s->cpu) && !s->cpu->stopped) { /* request the vCPU to stop */ s->cpu->stop = true; @@ -355,9 +385,8 @@ static void ot_rstmgr_reset_bus(void *opaque) replay_mutex_lock(); bql_lock(); qemu_notify_event(); - } else { - s->cpu->stop = false; } + s->cpu->stop = false; if (s->regs[R_CPU_INFO_CTRL] & R_CPU_INFO_CTRL_EN_MASK) { riscv_cpu_get_crash_dump(s->cpu, s->cpu_info_dump); @@ -477,7 +506,11 @@ static void ot_rstmgr_reset_req(void *opaque, int irq, int level) trace_ot_rstmgr_reset_req(s->ot_id, REQ_NAME(level), req, fastclk); if (s->cpu) { - cpu_pause(s->cpu); + if (qemu_cpu_is_self(s->cpu)) { + cpu_exit(s->cpu); + } else { + cpu_pause(s->cpu); + } } if (level == OT_RSTMGR_RESET_RV_DM) { @@ -748,6 +781,7 @@ static void ot_rstmgr_reset_enter(Object *obj, ResetType type) qemu_bh_cancel(s->bus_reset_bh); timer_del(s->sw_reset_timer); + s->reset_bus_queued = false; uint32_t reset_info = s->regs[R_RESET_INFO]; uint32_t alert_info_ctrl = s->regs[R_ALERT_INFO_CTRL]; diff --git a/hw/opentitan/ot_sysrst_ctrl.c b/hw/opentitan/ot_sysrst_ctrl.c index 05d2c26cb706a..47519a3f55b5b 100644 --- a/hw/opentitan/ot_sysrst_ctrl.c +++ b/hw/opentitan/ot_sysrst_ctrl.c @@ -388,6 +388,7 @@ static void ot_sysrst_ctrl_check_events(OtSysrstCtrlState *s) } else if (prev_trig && !curr_trig) { if (out_ctl & R_COM_OUT_CTL_0_RST_REQ_MASK) { timer_del(s->combo_rst_timer); + ibex_irq_set(&s->rst_req, 0); } if (out_ctl & (R_COM_OUT_CTL_0_EC_RST_MASK | R_COM_OUT_CTL_0_RST_REQ_MASK)) { @@ -414,8 +415,8 @@ static void ot_sysrst_ctrl_check_events(OtSysrstCtrlState *s) if (trigger_rst) { timer_mod_ns(s->combo_rst_timer, - qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + - (rst_det_cyc > 0u ? 3000000LL : 0LL)); + qemu_clock_get_ns(OT_VIRTUAL_CLOCK) + + (int64_t)(rst_det_cyc + 1u) * 5000LL); } ot_sysrst_ctrl_update_outputs(s); @@ -437,11 +438,6 @@ static void ot_sysrst_ctrl_input_change(void *opaque, int irq, int level) } s->poll_count = 0; - if (timer_pending(s->combo_rst_timer)) { - timer_mod_ns(s->combo_rst_timer, - qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 3000000LL); - } - /* * Per sysrst_ctrl_comboact.sv: ec_rst_l_i High -> Low transition * (ec_rst_l_det_pulse) asserts ec_rst_l_q = 0 and stretches it for @@ -689,8 +685,8 @@ static void ot_sysrst_ctrl_init(Object *obj) s->ec_rst_l_hw = true; s->ec_rst_timer = timer_new_ns(QEMU_CLOCK_REALTIME, &ot_sysrst_ctrl_ec_rst_timer_cb, s); - s->combo_rst_timer = timer_new_ns(QEMU_CLOCK_REALTIME, - &ot_sysrst_ctrl_combo_rst_timer_cb, s); + s->combo_rst_timer = + timer_new_ns(OT_VIRTUAL_CLOCK, &ot_sysrst_ctrl_combo_rst_timer_cb, s); s->ulp_timer = timer_new_ns(OT_VIRTUAL_CLOCK, &ot_sysrst_ctrl_ulp_timer_cb, s);