From 97d67aaedd3a2d3b3ba671e93d2b8c05dd1377ad Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:24 +0000 Subject: [PATCH 1/9] [ot] build: meson: add qemu-system-riscv32-cov and gcno packaging Build a gcov-instrumented QEMU binary (qemu-system-riscv32-cov) linked against --coverage static libraries (libsystem-ot-cov.a and libqemu-riscv32-softmmu-ot-cov.a) for the riscv32-softmmu target, and package qemu-system-riscv32-cov together with its .gcno note files in scripts/opentitan/make_release.sh so Bazel test runs can collect host hardware model line/branch coverage. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I6af52fbd3d1537704e491b047b41ac1d6a6a6964 --- meson.build | 36 +++++++++++++++++++++++++++++++ scripts/opentitan/make_release.sh | 3 ++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index f1a4c504ebc97..76c6ea4140d82 100644 --- a/meson.build +++ b/meson.build @@ -4399,6 +4399,42 @@ foreach target : target_dirs c_args: c_args, build_by_default: false) + if target == 'riscv32-softmmu' + src_sys = system_ss.apply(config_target, strict: false) + libsystem_ot_cov = static_library('system-ot-cov', + src_sys.sources() + genh, + c_args: ['-DCONFIG_SOFTMMU', + '-DCOMPILING_SYSTEM_VS_USER', + '--coverage'], + dependencies: system_ss.all_dependencies(), + build_by_default: false) + objects_cov = [common_all.extract_objects(target_common.sources())] + if target_base_arch in target_common_arch_libs + src_ca = target_common_arch[target_base_arch].apply(config_target, strict: false) + objects_cov += target_common_arch_libs[target_base_arch].extract_objects(src_ca.sources()) + endif + objects_cov += libsystem_ot_cov.extract_all_objects(recursive: true) + if target_base_arch in target_common_system_arch_libs + src_csa = target_common_system_arch[target_base_arch].apply(config_target, strict: false) + objects_cov += target_common_system_arch_libs[target_base_arch].extract_objects(src_csa.sources()) + endif + lib_ot_cov = static_library('qemu-' + target + '-ot-cov', + sources: arch_srcs + genh, + dependencies: lib_deps, + objects: objects_cov, + include_directories: target_inc, + c_args: c_args + ['--coverage'], + build_by_default: false) + executable('qemu-system-riscv32-cov', files('system/main.c'), + install: true, + c_args: c_args, + dependencies: arch_deps + [sdl], + objects: lib_ot_cov.extract_all_objects(recursive: true), + link_depends: [block_syms, qemu_syms], + link_args: link_args + ['--coverage'], + win_subsystem: 'console') + endif + if target.endswith('-softmmu') execs = [{ 'name': 'qemu-system-' + target_name, diff --git a/scripts/opentitan/make_release.sh b/scripts/opentitan/make_release.sh index 88fbae0c262f4..d9e6b2398f24f 100755 --- a/scripts/opentitan/make_release.sh +++ b/scripts/opentitan/make_release.sh @@ -17,6 +17,7 @@ QEMU_BUILD="$3" # Create archive. tar --create --auto-compress --verbose --file="$OUT_TARBALL" \ --directory="$QEMU_DIR" \ - "$QEMU_BUILD"/qemu-{system-riscv32,img} \ + "$QEMU_BUILD"/qemu-{system-riscv32,system-riscv32-cov,img} \ + "$QEMU_BUILD"/lib{system,qemu-riscv32-softmmu}-ot-cov.a.p/*.gcno \ scripts/opentitan/{otptool,flashgen,cfggen}.py \ python/qemu/ot From d47fe63e41d5be64a86c11734cd853cffd6abb7b Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:25 +0000 Subject: [PATCH 2/9] [ot] accel/tcg: icount: add exact freq, warp clamp, and idle check Support exact non-power-of-two CPU clock frequencies and deterministic sleep/idle virtual clock warping in TCG icount mode for OpenTitan Earlgrey: - Add icount_set_freq_hz() and update icount_to_ns() / icount_round() to use muldiv64() against icount_freq_hz when set, avoiding power-of-two icount_time_shift rounding drift relative to 24 MHz / 500 kHz peripheral clocks. - Add icount_get_cycles() and icount_advance_bias_ns() to track total elapsed CPU cycles (executed instructions plus idle warp and host I/O poll bias). - Clamp warp_delta in icount_warp_rt() to the nearest non-external QEMU_CLOCK_VIRTUAL timer deadline so idle time warps never overshoot scheduled virtual timer expirations. - Treat cpu->disabled as idle in cpu_thread_is_idle() so QEMU_CLOCK_VIRTUAL warps forward to AON wakeup timer deadlines while the Ibex core is powered down in deep sleep. - Configure icount_set_freq_hz(24 MHz / 500 kHz) in Earlgrey AST initialization (ot_eg_soc_ast_configure). Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I4ceb2556ebaa94e832a702d3331617426a6a6964 --- accel/tcg/icount-common.c | 42 +++++++++++++++++++++++++++++++++++++++ hw/riscv/ot_earlgrey.c | 3 +++ include/exec/icount.h | 9 +++++++++ system/cpus.c | 2 +- 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/accel/tcg/icount-common.c b/accel/tcg/icount-common.c index d6471174a3aae..b9e5622404a73 100644 --- a/accel/tcg/icount-common.c +++ b/accel/tcg/icount-common.c @@ -154,11 +154,40 @@ int64_t icount_get(void) return icount; } +static uint64_t icount_freq_hz; + +void icount_set_freq_hz(uint64_t freq_hz) +{ + qatomic_set(&icount_freq_hz, freq_hz); +} + int64_t icount_to_ns(int64_t icount) { + uint64_t freq = qatomic_read(&icount_freq_hz); + if (freq) { + return (int64_t)muldiv64(icount, NANOSECONDS_PER_SECOND, freq); + } return icount << qatomic_read(&timers_state.icount_time_shift); } +int64_t icount_get_cycles(void) +{ + return icount_get_raw() + + icount_round(qatomic_read_i64(&timers_state.qemu_icount_bias)); +} + +void icount_advance_bias_ns(int64_t delta_ns) +{ + if (delta_ns > 0 && icount_enabled()) { + seqlock_write_lock(&timers_state.vm_clock_seqlock, + &timers_state.vm_clock_lock); + qatomic_set_i64(&timers_state.qemu_icount_bias, + timers_state.qemu_icount_bias + delta_ns); + seqlock_write_unlock(&timers_state.vm_clock_seqlock, + &timers_state.vm_clock_lock); + } +} + /* * Correlation between real and virtual time is always going to be * fairly approximate, so ignore small variation. @@ -225,6 +254,13 @@ static void icount_adjust_vm(void *opaque) int64_t icount_round(int64_t count) { + if (count <= 0) { + return 0; + } + uint64_t freq = qatomic_read(&icount_freq_hz); + if (freq) { + return (int64_t)muldiv64(count, freq, NANOSECONDS_PER_SECOND) ?: 1; + } int shift = qatomic_read(&timers_state.icount_time_shift); return (count + (1 << shift) - 1) >> shift; } @@ -247,6 +283,9 @@ static void icount_warp_rt(void) return; } + int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, + ~QEMU_TIMER_ATTR_EXTERNAL); + seqlock_write_lock(&timers_state.vm_clock_seqlock, &timers_state.vm_clock_lock); if (runstate_is_running()) { @@ -255,6 +294,9 @@ static void icount_warp_rt(void) int64_t warp_delta; warp_delta = clock - timers_state.vm_clock_warp_start; + if (deadline >= 0) { + warp_delta = MIN(warp_delta, deadline); + } if (icount_enabled() == ICOUNT_ADAPTATIVE) { /* * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too far diff --git a/hw/riscv/ot_earlgrey.c b/hw/riscv/ot_earlgrey.c index 36034aae9161b..6ca4a6a6da8fb 100644 --- a/hw/riscv/ot_earlgrey.c +++ b/hw/riscv/ot_earlgrey.c @@ -29,6 +29,7 @@ #include "qemu/typedefs.h" #include "qapi/error.h" #include "qom/object.h" +#include "exec/icount.h" #include "hw/block/flash.h" #include "hw/boards.h" #include "hw/core/split-irq.h" @@ -1588,6 +1589,8 @@ static void ot_eg_soc_ast_configure(DeviceState *dev, const IbexDeviceDef *def, } qdev_prop_set_string(dev, "topclocks", clock_cfg); + /* Align icount virtual clock frequency with the active AST main clock */ + icount_set_freq_hz(!verilator_mode ? 24000000u : 500000u); } static void ot_eg_soc_dm_configure(DeviceState *dev, const IbexDeviceDef *def, diff --git a/include/exec/icount.h b/include/exec/icount.h index 7a26b40084dbf..d8c9b515e2085 100644 --- a/include/exec/icount.h +++ b/include/exec/icount.h @@ -46,6 +46,15 @@ void icount_update(CPUState *cpu); /* get raw icount value */ int64_t icount_get_raw(void); +/* get elapsed CPU cycles (executed instructions + idle warp cycles) */ +int64_t icount_get_cycles(void); + +/* set nominal CPU clock frequency in Hz for exact cycle-to-ns conversion */ +void icount_set_freq_hz(uint64_t freq_hz); + +/* advance icount_bias by delta_ns when sleeping for host I/O */ +void icount_advance_bias_ns(int64_t delta_ns); + /* return the virtual CPU time in ns, based on the instruction counter. */ int64_t icount_get(void); /* diff --git a/system/cpus.c b/system/cpus.c index b574a03d135e0..03b0b6fdb5fe3 100644 --- a/system/cpus.c +++ b/system/cpus.c @@ -89,7 +89,7 @@ bool cpu_thread_is_idle(CPUState *cpu) if (cpu->stop || !cpu_work_list_empty(cpu)) { return false; } - if (cpu_is_stopped(cpu)) { + if (cpu_is_stopped(cpu) || cpu->disabled) { return true; } if (!cpu->halted || cpu_has_work(cpu)) { From e2b6621e3805e2f6b64fe65590497bd44b0c2b52 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:25 +0000 Subject: [PATCH 3/9] [ot] accel/tcg: cputlb: support sub-page offsets and empty-TLB hit Support sub-page (< 4 KiB) address translation offsets and instruction fetches required by OpenTitan's Ibex address translation (ot_vmapper) and sub-page ePMP regions: - Check tlb_entry_is_empty() in tlb_hit_page_mask_anyprot() and mask with TARGET_PAGE_MASK so empty TLB entries (-1) do not falsely match sub-page masked TLB invalidations. - Preserve the intra-page offset delta ((full->phys_addr & ~TARGET_PAGE_MASK) - (addr & ~TARGET_PAGE_MASK)) in tlb_set_page_full() and use addition in io_failed() so sub-page NAPOT windows remapped to different intra-page offsets translate accurately. - Allow get_page_addr_code_hostp() to resolve host code pointers when full->lg_page_size < TARGET_PAGE_BITS so instruction fetches inside sub-page ePMP and vmapper regions execute without faulting. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Iefd28c41c65b315d9a80f6c8199fe0bb6a6a6964 --- accel/tcg/cputlb.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c index fd1606c85656e..f50acde2462a8 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -435,11 +435,23 @@ void tlb_flush_all_cpus_synced(CPUState *src_cpu) tlb_flush_by_mmuidx_all_cpus_synced(src_cpu, ALL_MMUIDX_BITS); } +/** + * tlb_entry_is_empty - return true if the entry is not in use + * @te: pointer to CPUTLBEntry + */ +static inline bool tlb_entry_is_empty(const CPUTLBEntry *te) +{ + return te->addr_read == -1 && te->addr_write == -1 && te->addr_code == -1; +} + static bool tlb_hit_page_mask_anyprot(CPUTLBEntry *tlb_entry, vaddr page, vaddr mask) { - page &= mask; - mask &= TARGET_PAGE_MASK | TLB_INVALID_MASK; + if (tlb_entry_is_empty(tlb_entry)) { + return false; + } + page &= mask & TARGET_PAGE_MASK; + mask &= TARGET_PAGE_MASK; return (page == (tlb_entry->addr_read & mask) || page == (tlb_addr_write(tlb_entry) & mask) || @@ -451,15 +463,6 @@ static inline bool tlb_hit_page_anyprot(CPUTLBEntry *tlb_entry, vaddr page) return tlb_hit_page_mask_anyprot(tlb_entry, page, -1); } -/** - * tlb_entry_is_empty - return true if the entry is not in use - * @te: pointer to CPUTLBEntry - */ -static inline bool tlb_entry_is_empty(const CPUTLBEntry *te) -{ - return te->addr_read == -1 && te->addr_write == -1 && te->addr_code == -1; -} - /* Called with tlb_c.lock held */ static bool tlb_flush_entry_mask_locked(CPUTLBEntry *tlb_entry, vaddr page, @@ -1151,13 +1154,15 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, * subtract here is that of the page base, and not the same as the * vaddr we add back in io_prepare()/get_page_addr_code(). */ + hwaddr ofs_diff = (full->phys_addr & ~TARGET_PAGE_MASK) - + (addr & ~TARGET_PAGE_MASK); desc->fulltlb[index] = *full; full = &desc->fulltlb[index]; full->xlat_section = iotlb - addr_page; - full->phys_addr = paddr_page; + full->phys_addr = paddr_page + ofs_diff; /* Now calculate the new entry */ - tn.addend = addend - addr_page; + tn.addend = addend - addr_page + ofs_diff; tlb_set_compare(full, &tn, addr_page, read_flags, MMU_INST_FETCH, prot & PAGE_EXEC); @@ -1292,7 +1297,7 @@ static void io_failed(CPUState *cpu, CPUTLBEntryFull *full, vaddr addr, { if (!cpu->ignore_memory_transaction_failures && cpu->cc->tcg_ops->do_transaction_failed) { - hwaddr physaddr = full->phys_addr | (addr & ~TARGET_PAGE_MASK); + hwaddr physaddr = full->phys_addr + (addr & ~TARGET_PAGE_MASK); cpu->cc->tcg_ops->do_transaction_failed(cpu, physaddr, addr, size, access_type, mmu_idx, @@ -1549,10 +1554,6 @@ tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, vaddr addr, return -1; } - if (full->lg_page_size < TARGET_PAGE_BITS) { - return -1; - } - if (hostp) { *hostp = p; } From bdab3db7c66a4f513e5a87ab4978544477e7d49f Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:25 +0000 Subject: [PATCH 4/9] [ot] system: memory: align sub-word accesses to access_size_min In access_with_adjusted_size(), when a sub-word access (size < access_size, such as an 8-bit or 16-bit access to a MemoryRegion with valid.min_access_size = 1 and impl.min_access_size = 4) targets a little-endian device, align the dispatch address to the containing word (addr - byte_off), shift by -(byte_off * 8), and pass the byte-lane mask MAKE_64BIT_MASK(byte_off * 8, size * 8). Without this alignment and shift, sub-word reads/writes at non-zero intra-word offsets (+1, +2, +3) pass unaligned offsets with shift = 0 and a full 32-bit access_mask to the 32-bit MMIO handler, returning wrong byte lanes on sub-word reads and mis-evaluating TL-UL sub-word write permit masks. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ie88da62f10dbe52772e2078c828304cc6a6a6964 --- system/memory.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/system/memory.c b/system/memory.c index 8b84661ae36c5..68ddf2afd2780 100644 --- a/system/memory.c +++ b/system/memory.c @@ -557,7 +557,12 @@ static MemTxResult access_with_adjusted_size(hwaddr addr, /* FIXME: support unaligned access? */ access_size = MAX(MIN(size, access_size_max), access_size_min); access_mask = MAKE_64BIT_MASK(0, access_size * 8); - if (devend_big_endian(mr->ops->endianness)) { + if (size < access_size && !devend_big_endian(mr->ops->endianness)) { + unsigned byte_off = addr & (access_size - 1); + access_mask = MAKE_64BIT_MASK(byte_off * 8, size * 8); + r |= access_fn(mr, addr - byte_off, value, access_size, + -(signed)(byte_off * 8), access_mask, attrs); + } else if (devend_big_endian(mr->ops->endianness)) { for (i = 0; i < size; i += access_size) { r |= access_fn(mr, addr + i, value, access_size, (size - access_size - i) * 8, access_mask, attrs); From 0465e597285c925582f5bb713905d67f7f7acd7d Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:26 +0000 Subject: [PATCH 5/9] [ot] target/riscv: pmp: flush TB on CSR write and enforce sub-page TLB When software updates pmpcfg, pmpaddr, or mseccfg CSRs, tlb_flush() alone does not invalidate already-translated TCG blocks in the current page, and multi-instruction TBs can cross 4-byte sub-page ePMP boundaries. - Call queue_tb_flush(env_cpu(env)) alongside tlb_flush() in pmpcfg_csr_write(), pmpaddr_csr_write(), and mseccfg_csr_write(). - Limit DisasContext max_insns to 1 in riscv_tr_init_disas_context() when pmp_get_tlb_size(env, ctx->base.pc_first) == 1. - Add env->tlb_subpage and enforce tlb_size = 1 in riscv_cpu_tlb_fill() when tlb_subpage or sub-page address translation is active. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: Ie467ca9827763118aa7e8c7b2eeb4a0d6a6a6964 --- target/riscv/cpu.h | 1 + target/riscv/cpu_helper.c | 3 +++ target/riscv/pmp.c | 4 ++++ target/riscv/translate.c | 6 ++++++ 4 files changed, 14 insertions(+) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index faf8a905ac812..3bbeba0de312e 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -411,6 +411,7 @@ struct CPUArchState { /* Whether to use virtual address for PMP (default: physical address) */ bool vaddr_pmp; + bool tlb_subpage; /* * Signals whether the current exception occurred while doing two-stage diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 69d73ac59d0f0..eb0ad95812d09 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1978,6 +1978,9 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, } if (ret == TRANSLATE_SUCCESS) { + if (env->tlb_subpage || ((address ^ pa) & ~TARGET_PAGE_MASK)) { + tlb_size = 1; + } tlb_set_page(cs, address & ~(tlb_size - 1), pa & ~(tlb_size - 1), prot, mmu_idx, tlb_size); return true; diff --git a/target/riscv/pmp.c b/target/riscv/pmp.c index 3ef62d26adbc0..d345c865b9cea 100644 --- a/target/riscv/pmp.c +++ b/target/riscv/pmp.c @@ -27,6 +27,7 @@ #include "exec/cputlb.h" #include "exec/page-protection.h" #include "exec/target_page.h" +#include "exec/tb-flush.h" static bool pmp_write_cfg(CPURISCVState *env, uint32_t addr_index, uint8_t val); @@ -528,6 +529,7 @@ void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index, if (modified) { pmp_update_rule_nums(env); tlb_flush(env_cpu(env)); + queue_tb_flush(env_cpu(env)); } } @@ -590,6 +592,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, pmp_update_rule_addr(env, addr_index + 1); } tlb_flush(env_cpu(env)); + queue_tb_flush(env_cpu(env)); } else { qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpaddr write - read only\n"); @@ -672,6 +675,7 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val) val |= (env->mseccfg & mask); if ((val ^ env->mseccfg) & mask) { tlb_flush(env_cpu(env)); + queue_tb_flush(env_cpu(env)); } } else { mask |= MSECCFG_RLB; diff --git a/target/riscv/translate.c b/target/riscv/translate.c index 82d24ea421fda..914a489e77077 100644 --- a/target/riscv/translate.c +++ b/target/riscv/translate.c @@ -30,6 +30,7 @@ #include "semihosting/semihost.h" #include "internals.h" +#include "pmp.h" #define HELPER_H "helper.h" #include "exec/helper-info.c.inc" @@ -1338,6 +1339,11 @@ static void riscv_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs) ctx->zero = tcg_constant_tl(0); ctx->virt_inst_excp = false; ctx->decoders = cpu->decoders; +#ifndef CONFIG_USER_ONLY + if (pmp_get_tlb_size(env, ctx->base.pc_first) == 1) { + ctx->base.max_insns = 1; + } +#endif } static void riscv_tr_tb_start(DisasContextBase *db, CPUState *cpu) From 291b5b26260fdbcf3379a99996ff42e41d0e0f73 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:27 +0000 Subject: [PATCH 6/9] [ot] target/riscv: ibex: add NMI delivery and mstack CSR save/restore Earlgrey's Ibex core (ibex_controller.sv and ibex_cs_registers.sv) implements custom Non-Maskable Interrupt (NMI) handling rather than the ratified Smrnmi extension: - External NMI (irq 31, mcause 0x8000001f) and internal NMI causes (cause 0..30, mcause 0xffffffe0 | cause) both vector to mtvec + 31 * 4. - Entering an NMI saves mstatus.{mpie,mpp}, mepc, and mcause into internal mstack shadow CSRs and asserts nmi_mode, masking all further interrupts and nested NMIs until MRET. - Executing MRET while in nmi_mode restores mstatus.{mpie,mpp}, mepc, and mcause from mstack, exits nmi_mode, and re-evaluates pending interrupts. Add riscv_cpu_set_rnmi_int(), mstack/nmi_mode state tracking in riscv_cpu_do_interrupt() and helper_mret(), and WFI wake on rnmip. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I4a014d5e33ac0f5804e4a58c5fb75b5d6a6a6964 --- target/riscv/cpu.c | 4 ++++ target/riscv/cpu.h | 10 +++++++++ target/riscv/cpu_helper.c | 44 ++++++++++++++++++++++++++++++++++++++- target/riscv/op_helper.c | 26 ++++++++++++++++++++--- 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 04a2a4c286aae..856226dad53ed 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -787,6 +787,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) riscv_trigger_reset_hold(env); } + env->rnmip = 0; + env->nmi_mode = false; + env->rnmi_int_mtval = 0; + env->rnmi_int_pending_count = 0; if (cpu->cfg.ext_smrnmi) { env->rnmip = 0; env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, false); diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 3bbeba0de312e..5313f95f885cb 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -536,6 +536,13 @@ struct CPUArchState { target_ulong rnmip; uint64_t rnmi_irqvec; uint64_t rnmi_excpvec; + bool nmi_mode; + target_ulong mstack_mpie; + target_ulong mstack_mpp; + target_ulong mstack_epc; + target_ulong mstack_cause; + target_ulong rnmi_int_mtval; + uint32_t rnmi_int_pending_count; }; /* @@ -679,6 +686,9 @@ int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint64_t interrupts); uint64_t riscv_cpu_update_mip(CPURISCVState *env, uint64_t mask, uint64_t value); void riscv_cpu_set_rnmi(RISCVCPU *cpu, uint32_t irq, bool level); +void riscv_cpu_set_rnmi_int(RISCVCPU *cpu, uint32_t cause, target_ulong mtval); +void riscv_cpu_get_crash_dump(CPUState *cs, uint32_t dump[8]); +void riscv_cpu_stall_on_unclocked_mmio(CPUState *cs, uint64_t phys_addr); void riscv_cpu_interrupt(CPURISCVState *env); #define BOOL_TO_MASK(x) (-!!(x)) /* helper for riscv_cpu_update_mip value */ void riscv_cpu_set_rdtime_fn(CPURISCVState *env, uint64_t (*fn)(void *), diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index eb0ad95812d09..55c74957f0f41 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -714,6 +714,18 @@ void riscv_cpu_set_rnmi(RISCVCPU *cpu, uint32_t irq, bool level) } } +void riscv_cpu_set_rnmi_int(RISCVCPU *cpu, uint32_t cause, target_ulong mtval) +{ + CPURISCVState *env = &cpu->env; + + if (!env->nmi_mode) { + env->rnmi_int_pending_count++; + env->rnmi_int_mtval = mtval; + env->rnmip |= (1ULL << cause); + cpu_interrupt(CPU(cpu), CPU_INTERRUPT_RNMI); + } +} + int riscv_cpu_claim_interrupts(RISCVCPU *cpu, uint64_t interrupts) { CPURISCVState *env = &cpu->env; @@ -2533,6 +2545,31 @@ void riscv_cpu_do_interrupt(CPUState *cs) !get_field(env->mnstatus, MNSTATUS_NMIE) && !async; + bool is_ibex_nmi = async && env->rnmip && !cpu->cfg.ext_smrnmi; + if (is_ibex_nmi) { + /* + * Ibex saves previous status to mstack CSRs and enters nmi_mode + * (ibex_cs_registers.sv & ibex_controller.sv). + */ + env->mstack_mpie = get_field(env->mstatus, MSTATUS_MPIE); + env->mstack_mpp = get_field(env->mstatus, MSTATUS_MPP); + env->mstack_epc = env->mepc; + env->mstack_cause = env->mcause; + env->nmi_mode = true; + if (cause < 31) { + tval = env->rnmi_int_mtval; + if (env->rnmi_int_pending_count > 0) { + env->rnmi_int_pending_count--; + } + if (env->rnmi_int_pending_count == 0) { + env->rnmip &= ~(1ULL << cause); + if (!env->rnmip) { + cpu_reset_interrupt(cs, CPU_INTERRUPT_RNMI); + } + } + } + } + s = env->mstatus; s = set_field(s, MSTATUS_MPIE, get_field(s, MSTATUS_MIE)); s = set_field(s, MSTATUS_MPP, env->priv); @@ -2551,7 +2588,11 @@ void riscv_cpu_do_interrupt(CPUState *cs) s = set_field(s, MSTATUS_MDT, 1); } env->mstatus = s; - env->mcause = cause | ((target_ulong)async << (mxlen - 1)); + if (is_ibex_nmi && cause < 31) { + env->mcause = 0xffffffe0ULL | (cause & 0x1fULL); + } else { + env->mcause = cause | ((target_ulong)async << (mxlen - 1)); + } if (smode_double_trap) { env->mtval2 = env->mcause; env->mcause = RISCV_EXCP_DOUBLE_TRAP; @@ -2569,6 +2610,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) if (nnmi_excep) { env->pc = env->rnmi_excpvec; } else { + target_ulong vec_cause = is_ibex_nmi ? 31 : cause; env->pc = (env->mtvec >> 2 << 2) + ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); } diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index 275e6687cf49f..7f8b798e1c8e0 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -30,6 +30,7 @@ #include "trace.h" #ifndef CONFIG_USER_ONLY +#include "qemu/main-loop.h" static inline MemOp mo_endian_env(CPURISCVState *env) { /* @@ -418,9 +419,22 @@ target_ulong helper_mret(CPURISCVState *env) (prev_priv != PRV_M); mstatus = set_field(mstatus, MSTATUS_MIE, get_field(mstatus, MSTATUS_MPIE)); - mstatus = set_field(mstatus, MSTATUS_MPIE, 1); - mstatus = set_field(mstatus, MSTATUS_MPP, - riscv_has_ext(env, RVU) ? PRV_U : PRV_M); + if (env->nmi_mode) { + /* + * Returning from Ibex NMI restores mstack CSRs and exits nmi_mode + * (ibex_cs_registers.sv & ibex_controller.sv). + */ + mstatus = set_field(mstatus, MSTATUS_MPIE, env->mstack_mpie); + mstatus = set_field(mstatus, MSTATUS_MPP, env->mstack_mpp); + env->mepc = env->mstack_epc; + env->mcause = env->mstack_cause; + env->nmi_mode = false; + } else { + env->sync_exc_seen = false; + mstatus = set_field(mstatus, MSTATUS_MPIE, 1); + mstatus = set_field(mstatus, MSTATUS_MPP, + riscv_has_ext(env, RVU) ? PRV_U : PRV_M); + } mstatus = set_field(mstatus, MSTATUS_MPV, 0); if (riscv_cpu_cfg(env)->ext_ssdbltrp) { mstatus = ssdbltrp_mxret(env, mstatus, prev_priv, prev_virt); @@ -433,6 +447,12 @@ target_ulong helper_mret(CPURISCVState *env) } env->mstatus = mstatus; + if (env->rnmip || riscv_cpu_all_pending(env)) { + BQL_LOCK_GUARD(); + cpu_interrupt(env_cpu(env), + env->rnmip ? CPU_INTERRUPT_RNMI : CPU_INTERRUPT_HARD); + } + if (riscv_has_ext(env, RVH) && prev_virt) { riscv_cpu_swap_hypervisor_regs(env); } From aa6de059839c76241dab69aef6c944face1a457f Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:27 +0000 Subject: [PATCH 7/9] [ot] target/riscv: ibex: add double-fault alert, crash dump, and WFI Model Earlgrey Ibex core crash dump and pipeline stall state (ibex_core.sv and ibex_controller.sv): - Track synchronous exception nesting (sync_exc_seen) across traps and MRET; when a second synchronous fault occurs before MRET, latch double_fault_seen, preserve prev_exception_pc/prev_exception_addr, and halt the CPU. - Track WFI instruction PC (wfi_pc) and the preceding store address (last_data_addr) in helper_wfi() so resets triggered during WFI report accurate current_pc, next_pc, and mdaa in rstmgr's CPU_INFO dump. - Implement riscv_cpu_get_crash_dump() (8-word crash dump state) and riscv_cpu_stall_on_unclocked_mmio() for bus accesses to clock-gated peripherals. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I9fe781b5536cb36ad107efbd25b973f66a6a6964 --- target/riscv/cpu.c | 13 ++++++- target/riscv/cpu.h | 8 +++++ target/riscv/cpu_helper.c | 72 ++++++++++++++++++++++++++++++++++++++- target/riscv/op_helper.c | 7 ++++ 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 856226dad53ed..c6afe8315efab 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -670,6 +670,9 @@ bool riscv_cpu_has_work(CPUState *cs) { RISCVCPU *cpu = RISCV_CPU(cs); CPURISCVState *env = &cpu->env; + if (env->double_fault_seen || env->unclocked_mmio_stall) { + return false; + } /* * Definition of the WFI instruction requires it to ignore the privilege * mode and delegation registers, but respect individual enables @@ -677,6 +680,7 @@ bool riscv_cpu_has_work(CPUState *cs) return riscv_cpu_all_pending(env) != 0 || riscv_cpu_sirq_pending(env) != RISCV_EXCP_NONE || riscv_cpu_vsirq_pending(env) != RISCV_EXCP_NONE || + env->rnmip != 0 || env->debug_cs; } #endif /* !CONFIG_USER_ONLY */ @@ -792,7 +796,6 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) env->rnmi_int_mtval = 0; env->rnmi_int_pending_count = 0; if (cpu->cfg.ext_smrnmi) { - env->rnmip = 0; env->mnstatus = set_field(env->mnstatus, MNSTATUS_NMIE, false); } @@ -818,6 +821,14 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type) pmp_update_rule_nums(env); env->mseccfg = (target_ulong)cfg->mseccfg; + env->sync_exc_seen = false; + env->double_fault_seen = false; + env->unclocked_mmio_stall = false; + env->prev_exception_pc = 0; + env->prev_exception_addr = 0; + env->last_data_addr = 0; + env->wfi_pc = 0; + cs->halted = 0; #endif } diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 5313f95f885cb..3a033effd2a1d 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -511,6 +511,14 @@ struct CPUArchState { target_ulong secureseed; #endif + bool sync_exc_seen; + bool double_fault_seen; + bool unclocked_mmio_stall; + target_ulong prev_exception_pc; + target_ulong prev_exception_addr; + target_ulong last_data_addr; + target_ulong wfi_pc; + /* Fields from here on are preserved across CPU reset. */ QEMUTimer *stimer; /* Internal timer for S-mode interrupt */ QEMUTimer *vstimer; /* Internal timer for VS-mode interrupt */ diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 55c74957f0f41..3456ff56bffdc 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -474,6 +474,14 @@ static int riscv_cpu_local_irq_pending(CPURISCVState *env) uint64_t vsbits, irq_delegated; int virq; + /* + * Interrupts including NMI are ignored while in NMI mode + * (ibex_controller.sv: handle_irq = ... & ~nmi_mode_q & ...). + */ + if (env->nmi_mode) { + return RISCV_EXCP_NONE; + } + /* Priority: RNMI > Other interrupt. */ if (riscv_cpu_cfg(env)->ext_smrnmi) { /* If mnstatus.NMIE == 0, all interrupts are disabled. */ @@ -484,6 +492,8 @@ static int riscv_cpu_local_irq_pending(CPURISCVState *env) if (env->rnmip) { return ctz64(env->rnmip); /* since non-zero */ } + } else if (env->rnmip) { + return (env->rnmip & (1ULL << 31)) ? 31 : ctz64(env->rnmip); } /* Determine interrupt enable state of all privilege modes */ @@ -553,6 +563,9 @@ bool riscv_cpu_exec_interrupt(CPUState *cs, int interrupt_request) if (interrupt_request & mask) { RISCVCPU *cpu = RISCV_CPU(cs); CPURISCVState *env = &cpu->env; + if (env->double_fault_seen || env->unclocked_mmio_stall) { + return false; + } if (unlikely(env->debug_dm && (env->debugger || get_field(env->dcsr, DCSR_STEP)))) { return false; @@ -2306,6 +2319,8 @@ void riscv_cpu_do_interrupt(CPUState *cs) int mxlen = 16 << riscv_cpu_mxl(env); bool nnmi_excep = false; + env->wfi_pc = 0; + if (cpu->cfg.ext_smrnmi && env->rnmip && async) { riscv_do_nmi(env, cause | ((target_ulong)1U << (mxlen - 1)), env->virt_enabled); @@ -2405,6 +2420,19 @@ void riscv_cpu_do_interrupt(CPUState *cs) return; } } + + if (cause == RISCV_EXCP_LOAD_ACCESS_FAULT || + cause == RISCV_EXCP_STORE_AMO_ACCESS_FAULT || + cause == RISCV_EXCP_LOAD_ADDR_MIS || + cause == RISCV_EXCP_STORE_AMO_ADDR_MIS) { + env->last_data_addr = tval; + } + if (env->sync_exc_seen) { + env->double_fault_seen = true; + env->prev_exception_pc = env->mepc; + env->prev_exception_addr = env->mtval; + } + env->sync_exc_seen = true; } trace_riscv_trap(env->mhartid, async, cause, env->pc, tval, @@ -2612,7 +2640,7 @@ void riscv_cpu_do_interrupt(CPUState *cs) } else { target_ulong vec_cause = is_ibex_nmi ? 31 : cause; env->pc = (env->mtvec >> 2 << 2) + - ((async && (env->mtvec & 3) == 1) ? cause * 4 : 0); + ((async && (env->mtvec & 3) == 1) ? vec_cause * 4 : 0); } riscv_cpu_set_mode(env, PRV_M, virt); src = env->mepc; @@ -2652,6 +2680,48 @@ void riscv_cpu_do_interrupt(CPUState *cs) env->two_stage_lookup = false; env->two_stage_indirect_lookup = false; + + if (env->double_fault_seen) { + cs->halted = 1; + cs->exception_index = EXCP_HLT; + cpu_loop_exit(cs); + } +} + +void riscv_cpu_get_crash_dump(CPUState *cs, uint32_t dump[8]) +{ + RISCVCPU *cpu = RISCV_CPU(cs); + CPURISCVState *env = &cpu->env; + + dump[0] = env->mtval; + dump[1] = env->mepc; + dump[2] = env->last_data_addr; + if (env->double_fault_seen) { + dump[3] = env->pc; + dump[4] = env->mepc; + } else if (env->wfi_pc != 0) { + dump[3] = env->wfi_pc + 4; + dump[4] = env->wfi_pc; + } else { + dump[3] = env->pc + 4; + dump[4] = env->pc; + } + dump[5] = env->prev_exception_addr; + dump[6] = env->prev_exception_pc; + dump[7] = env->double_fault_seen ? 1u : 0u; +} + +G_NORETURN void riscv_cpu_stall_on_unclocked_mmio(CPUState *cs, + uint64_t phys_addr) +{ + RISCVCPU *cpu = RISCV_CPU(cs); + CPURISCVState *env = &cpu->env; + + env->last_data_addr = (target_ulong)phys_addr; + env->unclocked_mmio_stall = true; + cs->halted = 1; + cs->exception_index = EXCP_HLT; + cpu_loop_exit(cs); } #endif /* !CONFIG_USER_ONLY */ diff --git a/target/riscv/op_helper.c b/target/riscv/op_helper.c index 7f8b798e1c8e0..4fa2d4631458d 100644 --- a/target/riscv/op_helper.c +++ b/target/riscv/op_helper.c @@ -611,6 +611,13 @@ void helper_wfi(CPURISCVState *env) riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, GETPC()); } else { if (!unlikely(env->debugger || env->debug_cs)) { + env->wfi_pc = env->pc - 4; + uint32_t insn = cpu_ldl_code(env, env->wfi_pc - 4); + if ((insn & 0x7f) == 0x23) { + int32_t imm = (((int32_t)insn >> 25) << 5) | + ((insn >> 7) & 0x1f); + env->last_data_addr = env->gpr[(insn >> 15) & 0x1f] + imm; + } cs->halted = 1; cs->exception_index = EXCP_HLT; cpu_loop_exit(cs); From 323b83b6710a4e7be86c3b30cd728f10833225d4 Mon Sep 17 00:00:00 2001 From: Yi-Hsuan Deng Date: Tue, 22 Sep 2026 17:18:30 +0000 Subject: [PATCH 8/9] [ot] hw/intc: sifive_plic: align claim, level IRQ, and TL-UL with RTL Align `hw/intc/sifive_plic.c` with OpenTitan Earlgrey `rv_plic` RTL (`rv_plic_target.sv` and `rv_plic_reg_top.sv`): - Allow `CC0` claim reads (`sifive_plic_claimed(..., ignore_threshold = true)`) to claim the highest-priority enabled and pending interrupt even when `prio <= THRESHOLD0`, while keeping `THRESHOLD0` gating on the external `MEIP` line (`ignore_threshold = false`). - Track claimed state (`sifive_plic_is_claimed()`) so level-triggered IRQs are not re-marked pending while claimed, and re-set `IP` upon `CC0` completion write (masked to `src_bits`) when the input level remains high. - Allow 2-bit WARL writes to `PRIO0`, mask unimplemented upper bits in the last `IE` word (`(1u << (num_sources & 31)) - 1u`), and check `pending_base` against `bitfield_words << 2`. - Implement `sifive_plic_accepts()` to enforce `RV_PLIC_PERMIT` sub-word byte-enable rules and raise TL-UL access faults on unmapped offsets. Co-authored-by: Google Antigravity <242056456+google-antigravity@users.noreply.github.com> Signed-off-by: Yi-Hsuan Deng Change-Id: I59b5a09073e5466cdaa19fe09d00edb36a6a6964 --- hw/intc/sifive_plic.c | 94 +++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 30 deletions(-) diff --git a/hw/intc/sifive_plic.c b/hw/intc/sifive_plic.c index 80b6a4e131960..b337148c7f78f 100644 --- a/hw/intc/sifive_plic.c +++ b/hw/intc/sifive_plic.c @@ -91,10 +91,17 @@ static void sifive_plic_set_claimed(SiFivePLICState *plic, int irq, bool level) } } -static uint32_t sifive_plic_claimed(SiFivePLICState *plic, uint32_t addrid) +static bool sifive_plic_is_claimed(SiFivePLICState *plic, int irq) +{ + return (plic->claimed[irq >> 5] & (1 << (irq & 31))) != 0; +} + +static uint32_t sifive_plic_claimed(SiFivePLICState *plic, uint32_t addrid, + bool ignore_threshold) { uint32_t max_irq = 0; - uint32_t max_prio = plic->target_priority[addrid]; + uint32_t max_prio = ignore_threshold ? 0 : plic->target_priority[addrid]; + bool found = false; int i, j; int num_irq_in_word = 32; @@ -121,7 +128,8 @@ static uint32_t sifive_plic_claimed(SiFivePLICState *plic, uint32_t addrid) uint32_t prio = plic->source_priority[irq]; int enabled = pending_enabled_not_claimed & (1 << j); - if (enabled && prio > max_prio) { + if (enabled && ((ignore_threshold && !found) || prio > max_prio)) { + found = true; max_irq = irq; max_prio = prio; } @@ -139,7 +147,7 @@ static void sifive_plic_update(SiFivePLICState *plic) for (addrid = 0; addrid < plic->num_addrs; addrid++) { uint32_t hartid = plic->addr_config[addrid].hartid; PLICMode mode = plic->addr_config[addrid].mode; - bool level = !!sifive_plic_claimed(plic, addrid); + bool level = !!sifive_plic_claimed(plic, addrid, false); switch (mode) { case PLICMode_M: @@ -156,6 +164,34 @@ static void sifive_plic_update(SiFivePLICState *plic) } } +static bool sifive_plic_accepts(void *opaque, hwaddr addr, unsigned size, + bool is_write, MemTxAttrs attrs) +{ + SiFivePLICState *plic = opaque; + uint32_t reg_be = ((1u << size) - 1u) << (addr & 3u); + uint32_t permit = 0; + + if (addr_between(addr, plic->priority_base, plic->num_sources << 2)) { + permit = 0x1u; + } else if (addr_between(addr, plic->pending_base, + plic->bitfield_words << 2)) { + permit = 0xfu; + } else if (addr_between(addr, plic->enable_base, + plic->num_addrs * plic->enable_stride)) { + if (((addr & (plic->enable_stride - 1)) >> 2) < + plic->bitfield_words) { + permit = 0xfu; + } + } else if (addr_between(addr, plic->context_base, + plic->num_addrs * plic->context_stride)) { + uint32_t contextid = addr & (plic->context_stride - 4); + if (contextid == 0 || contextid == 4) { + permit = 0x1u; + } + } + return permit != 0 && (!is_write || !(permit & ~reg_be)); +} + static uint64_t sifive_plic_read(void *opaque, hwaddr addr, unsigned size) { SiFivePLICState *plic = opaque; @@ -165,7 +201,7 @@ static uint64_t sifive_plic_read(void *opaque, hwaddr addr, unsigned size) return plic->source_priority[irq]; } else if (addr_between(addr, plic->pending_base, - (plic->num_sources + 31) >> 3)) { + plic->bitfield_words << 2)) { uint32_t word = (addr - plic->pending_base) >> 2; return plic->pending[word]; @@ -185,7 +221,7 @@ static uint64_t sifive_plic_read(void *opaque, hwaddr addr, unsigned size) if (contextid == 0) { return plic->target_priority[addrid]; } else if (contextid == 4) { - uint32_t max_irq = sifive_plic_claimed(plic, addrid); + uint32_t max_irq = sifive_plic_claimed(plic, addrid, true); if (max_irq) { sifive_plic_set_pending(plic, max_irq, false); @@ -210,13 +246,7 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, if (addr_between(addr, plic->priority_base, plic->num_sources << 2)) { uint32_t irq = (addr - plic->priority_base) >> 2; - if (irq == 0) { - /* IRQ 0 source prioority is reserved */ - qemu_log_mask(LOG_GUEST_ERROR, - "%s: Invalid source priority write 0x%" - HWADDR_PRIx "\n", __func__, addr); - return; - } else if (((plic->num_priorities + 1) & plic->num_priorities) == 0) { + if (((plic->num_priorities + 1) & plic->num_priorities) == 0) { /* * if "num_priorities + 1" is power-of-2, make each register bit of * interrupt priority WARL (Write-Any-Read-Legal). Just filter @@ -231,7 +261,7 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, sifive_plic_update(plic); } } else if (addr_between(addr, plic->pending_base, - (plic->num_sources + 31) >> 3)) { + plic->bitfield_words << 2)) { qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid pending write: 0x%" HWADDR_PRIx "", __func__, addr); @@ -241,12 +271,12 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, uint32_t wordid = (addr & (plic->enable_stride - 1)) >> 2; if (wordid < plic->bitfield_words) { + if (wordid == (plic->bitfield_words - 1) && + (plic->num_sources & 31)) { + value &= (1u << (plic->num_sources & 31)) - 1u; + } plic->enable[addrid * plic->bitfield_words + wordid] = value; sifive_plic_update(plic); - } else { - qemu_log_mask(LOG_GUEST_ERROR, - "%s: Invalid enable write 0x%" HWADDR_PRIx "\n", - __func__, addr); } } else if (addr_between(addr, plic->context_base, plic->num_addrs * plic->context_stride)) { @@ -268,19 +298,20 @@ static void sifive_plic_write(void *opaque, hwaddr addr, uint64_t value, sifive_plic_update(plic); } } else if (contextid == 4) { - if (value < plic->num_sources) { + uint32_t src_bits = (plic->num_sources > 1u) ? + (32u - clz32(plic->num_sources - 1u)) : + 1u; + value &= (src_bits >= 32u) ? 0xffffffffu : ((1u << src_bits) - 1u); + if (value < plic->num_sources && + sifive_plic_is_claimed(plic, value)) { sifive_plic_set_claimed(plic, value, false); + if (!plic->edge_triggered && + sifive_plic_get_level(plic, value)) { + sifive_plic_set_pending(plic, value, true); + } sifive_plic_update(plic); } - } else { - qemu_log_mask(LOG_GUEST_ERROR, - "%s: Invalid context write 0x%" HWADDR_PRIx "\n", - __func__, addr); } - } else { - qemu_log_mask(LOG_GUEST_ERROR, - "%s: Invalid register write 0x%" HWADDR_PRIx "\n", - __func__, addr); } } @@ -288,9 +319,11 @@ static const MemoryRegionOps sifive_plic_ops = { .read = sifive_plic_read, .write = sifive_plic_write, .endianness = DEVICE_LITTLE_ENDIAN, + .impl.min_access_size = 4, .valid = { - .min_access_size = 4, - .max_access_size = 4 + .min_access_size = 1, + .max_access_size = 4, + .accepts = sifive_plic_accepts, } }; @@ -387,7 +420,8 @@ static void sifive_plic_irq_request(void *opaque, int irq, int level) } sifive_plic_set_level(s, irq, !!level); } else { - if (level > 0) { + sifive_plic_set_level(s, irq, !!level); + if (level > 0 && !sifive_plic_is_claimed(s, irq)) { sifive_plic_set_pending(s, irq, true); } } From 751c4c549d53bb0e57fd657d4284129ac265de33 Mon Sep 17 00:00:00 2001 From: Siemen Dhooghe Date: Fri, 30 Oct 2026 14:38:32 +0100 Subject: [PATCH 9/9] [ot] tests/opentitan: earlgrey-tests: update renamed mldsa test targets Remove the removed flash_storage_ ML-DSA test target aliases and replace print_flash_storage_mldsa_certs_overlapped_test_sim_qemu_rom_ext with print_mldsa_certs_overlapped_test_sim_qemu_rom_ext following OpenTitan commit c21599172c. Signed-off-by: Siemen Dhooghe --- tests/opentitan/data/earlgrey-tests.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/opentitan/data/earlgrey-tests.txt b/tests/opentitan/data/earlgrey-tests.txt index 0153abd1eec40..303ed1d41a302 100644 --- a/tests/opentitan/data/earlgrey-tests.txt +++ b/tests/opentitan/data/earlgrey-tests.txt @@ -80,8 +80,7 @@ pass: //sw/device/silicon_creator/manuf/lib:individualize_functest_sim_qemu_rom_ pass: //sw/device/silicon_creator/manuf/lib:individualize_sw_cfg_functest_sim_qemu_rom_with_fake_keys pass: //sw/device/silicon_creator/manuf/tests:otp_ctrl_functest_sim_qemu_rom_with_fake_keys pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_certs_test_sim_qemu_rom_ext -pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_flash_storage_mldsa_certs_overlapped_test_sim_qemu_rom_ext -pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_flash_storage_mldsa_certs_test_sim_qemu_rom_ext +pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_mldsa_certs_overlapped_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/attestation:print_mldsa_certs_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/boot_svc:boot_svc_bad_next_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/boot_svc:boot_svc_bad_primary_test_sim_qemu_rom_ext @@ -104,7 +103,6 @@ pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:debug_mode_on_dice_x509 pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_dice_cwt_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_dice_mldsa_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_dice_x509_test_sim_qemu_rom_ext -pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_flash_storage_dice_mldsa_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:no_refresh_on_change_dice_mldsa_test_sim_qemu_rom_ext pass: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:on_demand_refresh_test_sim_qemu_rom_ext flaky: //sw/device/silicon_creator/rom_ext/e2e/dice_chain:variation_interop_x509_first_test_sim_qemu_rom_with_fake_keys # Expected to pass but can timeout in GitHub CI due to taking > 60 seconds.