hexagon: fixes for tlb, badva
cover hexagon: fixes for tlb, badva
Additional test suites uncover some subtle issues with TLBs, interrupts and badva register.
Brian Cain (8):
hw/hexagon: register the V68N_1024 machine target/hexagon: read BADVA as an alias of BADVA0/1 target/hexagon: kick vCPU when re-asserting interrupts hw/intc: name the L2VIC edge trigger check target/hexagon: assert guest register pair alignment
hexagon: raise imprecise exception for multi-TLB matches tests/functional/hexagon: add tests, update to v0.2.14 tests/functional/hexagon: update arch tests to v0.2.14
hw/hexagon/hexagon_dsp.c | 23 +++++++++++++++++++++ hw/hexagon/hexagon_tlb.c | 10 +++++++++ hw/intc/hex-l2vic.c | 13 ++++++++---- target/hexagon/hex_interrupts.c | 2 +- target/hexagon/hex_mmu.c | 12 ++++++++--- target/hexagon/hexswi.c | 1 + target/hexagon/op_helper.c | 9 ++++++++ target/hexagon/translate.c | 22 +++++++------------- tests/functional/hexagon/test_arch_tests.py | 6 +++--- tests/functional/hexagon/test_systests.py | 17 +++++++++++---- 10 files changed, 86 insertions(+), 29 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/8 hw/hexagon: register the V68N_1024 machine
This DSP definition file was present but not referenced. Also: add a 'sim' alias for this machine to indicate a recent machine with memory-map resembling that used by 'hexagon-sim'.
--- hw/hexagon/hexagon_dsp.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
hw/hexagon/hexagon_dsp.c +23 -0
@@ -29,6 +29,7 @@#include "semihosting/semihost.h" #include "machine_cfg_v66g_1024.h.inc" +#include "machine_cfg_v68n_1024.h.inc" #define TYPE_HEXAGON_DSP_MACHINE "hexagon-dsp-machine"OBJECT_DECLARE_SIMPLE_TYPE (HexagonDspMachineState,HEXAGON_DSP_MACHINE )
@@ -181,6 +182,23 @@ static void v66g_1024_init(ObjectClass *oc, const void *data) mc->default_cpus =4 ; } +static void v68n_1024_config_init (MachineState *machine) +{ +hexagon_common_init (machine, v68_rev, &v68n_1024); +} + +static void v68n_1024_init (ObjectClass *oc,const void *data) +{ +MachineClass *mc =MACHINE_CLASS (oc); + + mc->desc ="Hexagon V68N_1024" ; + mc->alias ="sim" ; + mc->init = v68n_1024_config_init; +init_mc (mc); + mc->default_cpu_type =TYPE_HEXAGON_CPU_V68 ; + mc->default_cpus =6 ; +} +static const TypeInfo hexagon_machine_types[] = { { .name =TYPE_HEXAGON_COMMON_MACHINE ,
@@ -199,6 +217,11 @@ static const TypeInfo hexagon_machine_types[] = { .parent = TYPE_HEXAGON_DSP_MACHINE, .class_init = v66g_1024_init, }, + { + .name = MACHINE_TYPE_NAME("V68N_1024"), + .parent = TYPE_HEXAGON_DSP_MACHINE, + .class_init = v68n_1024_init, + }, };DEFINE_TYPES (hexagon_machine_types)
Signature · 1 line
2.34.1
Re: [PATCH 1/8] hw/hexagon: register the V68N_1024 machine
Quoted 9 lines · Brian Cain
This DSP definition file was present but not referenced. Also: add a 'sim' alias for this machine to indicate a recent machine with memory-map resembling that used by 'hexagon-sim'.
Signed-off-by Brian Cain <[email protected]>--- hw/hexagon/hexagon_dsp.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
PATCH 2/8 target/hexagon: read BADVA as an alias of BADVA0/1
In reply to Brian CainBADVA is sort of a virtual register: it reads back as either BADVA0 or BADVA1, selected by SSR[BVS]. sreg_read() instead returned the separate copy that set_badva_regs() stores in t_sreg[HEX_SREG_BADVA].
Guest code that writes BADVA0/BADVA1 directly was reading back a stale BADVA.
Uncovered by the h2 hypervisor kernel/mem/tlbmiss test.
--- target/hexagon/op_helper.c | 7 +++++++ 1 file changed, 7 insertions(+)
target/hexagon/op_helper.c +7 -0
@@ -1889,6 +1889,13 @@ static inline QEMU_ALWAYS_INLINE uint32_t sreg_read(CPUHexagonState *env,HexagonCPU *cpu;g_assert (bql_locked ()); +if (reg ==HEX_SREG_BADVA ) { +uint32_t ssr = env->t_sreg[HEX_SREG_SSR ]; +if (GET_SSR_FIELD (SSR_BVS , ssr)) { +return env->t_sreg[HEX_SREG_BADVA1 ]; + } +return env->t_sreg[HEX_SREG_BADVA0 ]; + }if (reg <HEX_SREG_GLB_START ) {return env->t_sreg[reg]; }
Signature · 1 line
2.34.1
Re: [PATCH 2/8] target/hexagon: read BADVA as an alias of BADVA0/1
Quoted 4 lines · Brian Cain
BADVA is sort of a virtual register: it reads back as either BADVA0 or BADVA1, selected by SSR[BVS].
sreg_read()instead returned the separate copy thatset_badva_regs()stores in t_sreg[HEX_SREG_BADVA].
Quoted 9 lines
Guest code that writes BADVA0/BADVA1 directly was reading back a stale BADVA.
Uncovered by the h2 hypervisor
kernel/mem/tlbmisstest.Signed-off-by Brian Cain <[email protected]>--- target/hexagon/op_helper.c | 7 +++++++ 1 file changed, 7 insertions(+)
PATCH 3/8 target/hexagon: kick vCPU when re-asserting interrupts
In reply to Brian Cainhex_interrupt_update() used cpu_resume() to make a halted vCPU notice a newly pending interrupt. cpu_resume() is meant for a vCPU stopped by the vm-state machinery: besides kicking the thread it clears cpu->stop and cpu->stopped, and resets exception_index to -1. Clearing the stop flags can undo a pause requested via the monitor or by migration, and resetting exception_index discards an exception the vCPU had already latched.
Only the kick is wanted here, so call qemu_cpu_kick() directly.
--- target/hexagon/hex_interrupts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/hexagon/hex_interrupts.c +1 -1
@@ -441,7 +441,7 @@ void hex_interrupt_update(CPUHexagonState *env)const int exe_mode =get_exe_mode (hex_env);if (exe_mode !=HEX_EXE_MODE_OFF ) {cpu_interrupt (cs,CPU_INTERRUPT_SWI ); -cpu_resume (cs); +qemu_cpu_kick (cs); } } }
Signature · 1 line
2.34.1
Re: [PATCH 3/8] target/hexagon: kick vCPU when re-asserting interrupts
Quoted 7 lines · Brian Cain
hex_interrupt_update()usedcpu_resume()to make a halted vCPU notice a newly pending interrupt.cpu_resume()is meant for a vCPU stopped by the vm-state machinery: besides kicking the thread it clearscpu->stopandcpu->stopped, and resetsexception_indexto -1. Clearing the stop flags can undo a pause requested via the monitor or by migration, and resettingexception_indexdiscards an exception the vCPU had already latched.
Quoted 7 lines
Only the kick is wanted here, so call
qemu_cpu_kick()directly.Signed-off-by Brian Cain <[email protected]>--- target/hexagon/hex_interrupts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Re: [PATCH 3/8] target/hexagon: kick vCPU when re-asserting interrupts
In reply to Brian CainQuoted 19 lines · Brian Cain
hex_interrupt_update()usedcpu_resume()to make a halted vCPU notice a newly pending interrupt.cpu_resume()is meant for a vCPU stopped by the vm-state machinery: besides kicking the thread it clearscpu->stopandcpu->stopped, and resetsexception_indexto -1. Clearing the stop flags can undo a pause requested via the monitor or by migration, and resettingexception_indexdiscards an exception the vCPU had already latched.Only the kick is wanted here, so call
qemu_cpu_kick()directly.Signed-off-by Brian Cain <[email protected]>--- target/hexagon/hex_interrupts.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/hexagon/hex_interrupts.c +0
-0
target/hexagon/hex_interrupts.c void hex_interrupt_update(CPUHexagonState *env) · line 447
Quoted 8 lines
target/hexagon/hex_interrupts.c +1
-1@@ -441,7 +441,7 @@ void hex_interrupt_update(CPUHexagonState *env)const int exe_mode =get_exe_mode (hex_env);if (exe_mode !=HEX_EXE_MODE_OFF ) {cpu_interrupt (cs,CPU_INTERRUPT_SWI ); -cpu_resume (cs); +qemu_cpu_kick (cs); } } }
This reminded me another patch I'm carrying locally after your confirmation on
https://lore.kernel.org/qemu-devel/[email protected]/:
-- >8 --
Author: Philippe Mathieu-Daudé <[email protected]> Date: Fri Aug 14 18:49:50 2026 +0200
target/hexagon: Do not open-code cpu_resume() in _resume_thread()
As the name imply, hexagon_resume_thread() wants to resume the vCPU. Better call the appropriate cpu_resume() method, hidding the vCPU 'kick' API.
---
FIXME: can we safely remove "cs->halted = 0;"?
target/hexagon/cpu_helper.c +1 -3
@@ -225,8 +225,7 @@ static void hexagon_resume_thread(CPUHexagonState *env) cs =env_cpu (env);ASSERT_DIRECT_TO_GUEST_UNSET (env, cs->exception_index); cs->halted = false; - cs->exception_index =HEX_EVENT_NONE ; -qemu_cpu_kick (cs); +cpu_resume (cs); } ---
PATCH 4/8 hw/intc: name the L2VIC edge trigger check
In reply to Brian Cain--- hw/intc/hex-l2vic.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
hw/intc/hex-l2vic.c +9 -4
@@ -92,7 +92,7 @@ typedef struct HexL2VICState {DECLARE_BITMAP32 (int_pending,L2VIC_INTERRUPT_MAX );/* Which enabled interrupt is active */ DECLARE_BITMAP32 (int_status,L2VIC_INTERRUPT_MAX ); -/* Edge or Level interrupt */ +/* 1 for edge-triggered, 0 for level-triggered */ DECLARE_BITMAP32 (int_type,L2VIC_INTERRUPT_MAX );DECLARE_BITMAP32 (int_group_n[4 ],L2VIC_INTERRUPT_MAX );qemu_irq irq[8 ];
@@ -249,6 +249,11 @@ static inline bool vid_active(HexL2VICState *s)return active_irq != size; } +static bool edge_triggered_irq (HexL2VICState *s,int irq) +{ +return test_bit32 (irq, s->int_type); +} +static bool l2vic_update (HexL2VICState *s,int irq) {bool pending;
@@ -270,7 +275,7 @@ static bool l2vic_update(HexL2VICState *s, int irq)* enable bit set across deliveries -- the firmware enables once * and expects the interrupt to remain enabled. */ -if (test_bit32 (irq, s->int_type)) { +if (edge_triggered_irq (s, irq)) {clear_bit32 (irq, s->int_enable); } s->vid = irq;
@@ -299,7 +304,7 @@ static void l2vic_set_irq(void *opaque, int irq, int level)if (level) {set_bit32 (irq, s->int_pending); - }else if (!test_bit32 (irq, s->int_type)) { + }else if (!edge_triggered_irq (s, irq)) {clear_bit32 (irq, s->int_pending); }l2vic_update (s, irq);
@@ -328,7 +333,7 @@ static void l2vic_write(void *opaque, hwaddr offset, uint64_t val,while ((bit =ctz32 (bits)) <32 ) {int irq = base_irq + bit; -if (test_bit32 (irq, s->int_type)) { +if (edge_triggered_irq (s, irq)) {set_bit32 (irq, s->int_pending); } bits &= ~(1u << bit);
Signature · 1 line
2.34.1
Re: [PATCH 4/8] hw/intc: name the L2VIC edge trigger check
Quoted 7 lines · Brian Cain
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Brian Cain <[email protected]>--- hw/intc/hex-l2vic.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
Re: [PATCH 4/8] hw/intc: name the L2VIC edge trigger check
In reply to Brian CainQuoted 6 lines · Brian Cain
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Brian Cain <[email protected]>--- hw/intc/hex-l2vic.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
PATCH 5/8 target/hexagon: assert guest register pair alignment
In reply to Brian Cain--- target/hexagon/op_helper.c | 2 ++ 1 file changed, 2 insertions(+)
target/hexagon/op_helper.c +2 -0
@@ -1927,6 +1927,8 @@ uint32_t HELPER(greg_read)(CPUHexagonState *env, uint32_t reg)uint64_t HELPER (greg_read_pair )(CPUHexagonState *env,uint32_t reg) { +g_assert ((reg &1 ) ==0 ); +if (reg ==HEX_GREG_G0 || reg ==HEX_GREG_G2 ) {return (uint64_t )(env->greg[reg]) | (((uint64_t )(env->greg[reg +1 ])) <<32 );
Signature · 1 line
2.34.1
Re: [PATCH 5/8] target/hexagon: assert guest register pair alignment
Quoted 7 lines · Brian Cain
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Brian Cain <[email protected]>--- target/hexagon/op_helper.c | 2 ++ 1 file changed, 2 insertions(+)
Re: [PATCH 5/8] target/hexagon: assert guest register pair alignment
In reply to Brian CainQuoted 6 lines · Brian Cain
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Brian Cain <[email protected]>--- target/hexagon/op_helper.c | 2 ++ 1 file changed, 2 insertions(+)
PATCH 6/8 hexagon: raise imprecise exception for multi-TLB matches
In reply to Brian CainThe TLB walk stopped at the first matching entry, so instruction and data translations did not detect a second valid mapping for the same VA and ASID.
Scan the remaining entries after selecting the translation. On a second match, retain the first entry for the access but record HEX_CAUSE_IMPRECISE_MULTI_TLB_MATCH as a pending imprecise exception.
Move that exception out of the synchronous TLB-fill result and check for it after every packet, rather than only packets containing tlbp. This delivers the architectural imprecise exception after an ordinary memory access and avoids treating it as a precise permission fault. Clear the pending state when starting a new translation and after delivering the exception.
The v0.2.14 mmu_multi_tlb systest uncovered this bug.
--- hw/hexagon/hexagon_tlb.c | 10 ++++++++++ target/hexagon/hex_mmu.c | 12 +++++++++--- target/hexagon/hexswi.c | 1 + target/hexagon/translate.c | 22 ++++++++-------------- 4 files changed, 28 insertions(+), 17 deletions(-)
hw/hexagon/hexagon_tlb.c +10 -0
@@ -326,6 +326,16 @@ bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid,for (uint32_t i =0 ; i < tlb->num_entries; i++) {if (hex_tlb_entry_match (tlb->entries[i], asid,VA , access_type,PA , prot, size, excp, cause_code, mmu_idx)) { +if (*excp ==0 ) { +for (i++; i < tlb->num_entries; i++) { +if (hex_tlb_entry_match_noperm (tlb->entries[i], asid, +VA )) { + *excp =HEX_EVENT_IMPRECISE ; + *cause_code =HEX_CAUSE_IMPRECISE_MULTI_TLB_MATCH ; +break ; + } + } + }return true; } }
target/hexagon/hex_mmu.c +9 -3
@@ -71,10 +71,16 @@ bool hex_tlb_find_match(CPUHexagonState *env, uint32_t VA,uint32_t ssr = env->t_sreg[HEX_SREG_SSR ];uint8_t asid =GET_SSR_FIELD (SSR_ASID , ssr);int cause_code =0 ; +bool found; -bool found =hexagon_tlb_find_match (cpu->tlb, asid,VA , access_type, -PA , prot, size, excp, &cause_code, - mmu_idx); + env->imprecise_exception =0 ; + found =hexagon_tlb_find_match (cpu->tlb, asid,VA , access_type, +PA , prot, size, excp, &cause_code, + mmu_idx); +if (*excp ==HEX_EVENT_IMPRECISE ) { + env->imprecise_exception = *excp; + *excp =0 ; + }if (cause_code) { env->cause_code = cause_code; }
target/hexagon/hexswi.c +1 -0
@@ -934,6 +934,7 @@ void hexagon_cpu_do_interrupt(CPUState *cs)break ;case HEX_EVENT_IMPRECISE : + env->imprecise_exception =0 ;if (get_exe_mode (env) ==HEX_EXE_MODE_WAIT ) { env->gpr[HEX_REG_PC ] = env->wait_next_pc -4 ;clear_wait_mode (env);
target/hexagon/translate.c +8 -14
@@ -1065,20 +1065,14 @@ static void update_exec_counters(DisasContext *ctx)* A tlbp instruction may detect multiple TLB matches and set a pending * imprecise exception. Raise it after the packet that ran the tlbp. */ -static void check_imprecise_exception (Packet *pkt) +static void check_imprecise_exception (DisasContext *ctx) { -for (int i =0 ; i < pkt->num_insns; i++) { -if (pkt->insn[i].opcode == Y2_tlbp) { -TCGv PC =tcg_constant_tl (pkt->pc); -TCGLabel *label =gen_new_label (); -tcg_gen_brcondi_tl (TCG_COND_EQ , hex_imprecise_exception, -0 , label); -gen_helper_raise_exception (tcg_env, - hex_imprecise_exception,PC ); -gen_set_label (label); -return ; - } - } +TCGv PC =tcg_constant_tl (ctx->pkt.pc); +TCGLabel *label =gen_new_label (); + +tcg_gen_brcondi_tl (TCG_COND_EQ , hex_imprecise_exception,0 , label); +gen_helper_raise_exception (tcg_env, hex_imprecise_exception,PC ); +gen_set_label (label); }#endif
@@ -1182,7 +1176,7 @@ static void gen_commit_packet(DisasContext *ctx) }#ifndef CONFIG_USER_ONLY -check_imprecise_exception (&ctx->pkt); +check_imprecise_exception (ctx);#endif if (ctx->pkt_ends_tb || ctx->base.is_jmp ==DISAS_NORETURN ) {
Signature · 1 line
2.34.1
Re: [PATCH 6/8] hexagon: raise imprecise exception for multi-TLB matches
Quoted 15 lines · Brian Cain
The TLB walk stopped at the first matching entry, so instruction and data translations did not detect a second valid mapping for the same VA and ASID.
Scan the remaining entries after selecting the translation. On a second match, retain the first entry for the access but record
HEX_CAUSE_IMPRECISE_MULTI_TLB_MATCHas a pending imprecise exception.Move that exception out of the synchronous TLB-fill result and check for it after every packet, rather than only packets containing tlbp. This delivers the architectural imprecise exception after an ordinary memory access and avoids treating it as a precise permission fault. Clear the pending state when starting a new translation and after delivering the exception.
The v0.2.14
mmu_multi_tlbsystest uncovered this bug.
Quoted 8 lines
Signed-off-by Brian Cain <[email protected]>--- hw/hexagon/hexagon_tlb.c | 10 ++++++++++ target/hexagon/hex_mmu.c | 12 +++++++++--- target/hexagon/hexswi.c | 1 + target/hexagon/translate.c | 22 ++++++++-------------- 4 files changed, 28 insertions(+), 17 deletions(-)
PATCH 7/8 tests/functional/hexagon: add tests, update to v0.2.14
In reply to Brian CainAdd tests for direct-to-guest interrupts, multi-TLB match case, {u,}timer registers.
Update to the v0.2.14 tests and switch to the new 'sim' machine name.
--- tests/functional/hexagon/test_systests.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
tests/functional/hexagon/test_systests.py +13 -4
@@ -21,8 +21,8 @@ class SysTestsStandaloneTests(QemuSystemTest): SYSTEST_TIMEOUT_SEC = 30 ASSET_TARBALL = Asset( - "https://github.com/qualcomm/qemu-hexagon-testing/releases/download/v0.2.11/systests_standalone.tar.gz", - "b5777aa65245de7710a7a08d717953c1362be7c8b60d9014c9fee8b17610ad1c", + "https://github.com/qualcomm/qemu-hexagon-testing/releases/download/v0.2.14/systests_standalone.tar.gz", + "f0c535d746384126954757b6ce54452c5fe82624618c79862495eec24718a6ff", ) def setUp(self):
@@ -34,7 +34,7 @@ def binary(self, name): self.assertTrue(os.path.exists(path)) return path - def run_exit_zero(self, binary_name, *extra_args, machine="V66G_1024"): + def run_exit_zero(self, binary_name, *extra_args, machine="sim"): self.set_machine(machine) self.set_vm_arg("-display", "none") self.set_vm_arg("-kernel", self.binary(binary_name))
@@ -47,7 +47,7 @@ def run_exit_zero(self, binary_name, *extra_args, machine="V66G_1024"): f"code {self.vm.exitcode()}, expected 0") def run_console_pattern(self, binary_name, pattern, *extra_args, - machine="V66G_1024"): + machine="sim"): self.set_machine(machine) self.set_vm_arg("-display", "none") self.set_vm_arg("-kernel", self.binary(binary_name))
@@ -90,5 +90,14 @@ def test_access(self): def test_semihost(self): self.run_console_pattern("semihost", "PASS", "-append", "arg1", "arg2") + def test_dtg_interrupt(self): + self.run_exit_zero("dtg_interrupt") + + def test_mmu_multi_tlb(self): + self.run_exit_zero("mmu_multi_tlb") + + def test_timer_reg(self): + self.run_exit_zero("timer_reg") + if __name__ == "__main__": QemuSystemTest.main()
Signature · 1 line
2.34.1
Re: [PATCH 7/8] tests/functional/hexagon: add tests, update to v0.2.14
Quoted 10 lines · Brian Cain
Add tests for direct-to-guest interrupts, multi-TLB match case, {u,}timer registers.
Update to the v0.2.14 tests and switch to the new 'sim' machine name.
Signed-off-by Brian Cain <[email protected]>--- tests/functional/hexagon/test_systests.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
PATCH 8/8 tests/functional/hexagon: update arch tests to v0.2.14
In reply to Brian CainSome of the tests run longer now, extend the timeout to 180s in order to keep it robust.
--- tests/functional/hexagon/test_arch_tests.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tests/functional/hexagon/test_arch_tests.py +3 -3
@@ -17,12 +17,12 @@ class ArchTestsUart(QemuSystemTest): Tests output results via UART. """ - timeout = 60 + timeout = 180 ASSET_TARBALL = Asset( "https://github.com/qualcomm/qemu-hexagon-testing/releases/" - "download/v0.2.12/arch_tests_uart.tar.gz", - "871a339bf78cac0ebaf1b2509bfcd5b249ad8190be33e0cf848283b2f6915323", + "download/v0.2.14/arch_tests_uart.tar.gz", + "ce93cb90b9d757c1946dfe8fe6abcec8292b08a66546ac51b2dd48650b05fa91", ) def run_uart_test(self, test_name: str,
Signature · 1 line
2.34.1
Re: [PATCH 8/8] tests/functional/hexagon: update arch tests to v0.2.14
Quoted 8 lines · Brian Cain
Some of the tests run longer now, extend the timeout to 180s in order to keep it robust.
Signed-off-by Brian Cain <[email protected]>--- tests/functional/hexagon/test_arch_tests.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)