hex queue
Subsystems (2)
The following changes since commit eb710c255642cd7dc947f25c9a06c9fc8191e98d:
Merge tag 'firmware-20260901-pull-request' of https://gitlab.com/kraxel/qemu into staging (2026-09-01 10:38:45 +0100)
are available in the Git repository at:
https://github.com/qualcomm/qemu tags/pull-hex-20260901
for you to fetch changes up to 4a4ba15b2b0de009c662674dd365ff56048e2b22:
hw/hexagon: decode an empty TLB size field as 4KB (2026-09-01 18:23:23 -0700)
----------------------------------------------------------------
This PR adds semihosting, and fixes for exceptions, icinva, TLB, htid
----------------------------------------------------------------
Brian Cain (18):
semihosting: add APIs for chardev-aware guest fd routing target/hexagon: Add an errno mapping
python/machine: support routing semihosting output to the test console tests/functional: Add hexagon semihosting systests target/hexagon: align exceptions for user/sysemu tests/tcg/hexagon: check priv instructions raise SIGILL target/hexagon: guard writes to unimplemented guest registers target/hexagon: take BQL when reading the system pcycle count target/hexagon: gate GPCYCLE guest register reads on SSR:CE target/hexagon: read UTIMERLO/UTIMERHI from the global timer target/hexagon: raise imprecise exception on multi-TLB match target/hexagon: implement direct-to-guest interrupt delivery target/hexagon: fix iassign{r, w} to cover all threads tests/functional/hexagon: update to v0.2.12, +test_{interrupts, sys_regs} target/hexagon: invalidate translated code for user icinva tests/tcg/hexagon: add icinva test
hw/hexagon: assign a distinct htid to each DSP machine CPU hw/hexagon: decode an empty TLB size field as 4KB
Matheus Tavares Bernardino (9):
disas/hexagon: honour target byte order when printing instructions target/hexagon: fix store-conditional on big-endian hosts target/hexagon: fix PC advancement for non-COF TB terminators target/hexagon: add aux functions for guest mem load/store
semihosting: add callback to set error
target/hexagon: add semihosting support
semihosting: add ftruncate helper (to be used for hexagon) target/hexagon: add main arch-specific semihosting operations target/hexagon: add COREDUMP semihosting operation
Sid Manning (1):
hw/intc: clear pending bit on l2vic de-assertion
docs/system/target-hexagon.rst | 8 +- configs/targets/hexagon-softmmu.mak | 2 + include/hw/hexagon/hexagon_tlb.h | 3 +- include/semihosting/common-semi.h | 2 + include/semihosting/console.h | 9 + include/semihosting/guestfd.h | 9 + include/semihosting/syscalls.h | 2 + target/hexagon/cpu.h | 1 + target/hexagon/cpu_helper.h | 6 + target/hexagon/gen_tcg.h | 9 + target/hexagon/helper.h | 5 + target/hexagon/internal.h | 1 + target/hexagon/translate.h | 6 +- target/hexagon/reg_fields_def.h.inc | 7 + disas/hexagon.c | 6 +- hw/hexagon/hexagon_dsp.c | 4 + hw/hexagon/hexagon_tlb.c | 29 +- hw/intc/hex-l2vic.c | 2 + linux-user/hexagon/cpu_loop.c | 30 +- semihosting/arm-compat-semi.c | 3 +- semihosting/console.c | 5 + semihosting/guestfd.c | 8 + semihosting/syscalls.c | 29 ++ target/arm/common-semi-target.c | 4 + target/hexagon/common-semi-target.c | 51 ++ target/hexagon/cpu.c | 17 +- target/hexagon/cpu_helper.c | 112 ++++- target/hexagon/genptr.c | 33 +- target/hexagon/hex_interrupts.c | 91 +++- target/hexagon/hex_mmu.c | 5 +- target/hexagon/hexswi.c | 734 +++++++++++++++++++++++++++- target/hexagon/machine.c | 5 +- target/hexagon/op_helper.c | 85 ++-- target/hexagon/translate.c | 72 ++- target/riscv/common-semi-target.c | 4 + tests/tcg/hexagon/icinva.c | 70 +++ tests/tcg/hexagon/privileged-insn.c | 79 +++ tests/tcg/hexagon/reg_mut.c | 11 +- hw/hexagon/Kconfig | 1 + python/qemu/machine/machine.py | 10 +- qemu-options.hx | 8 +- target/hexagon/hex_common.py | 18 +- target/hexagon/meson.build | 1 + tests/functional/hexagon/meson.build | 5 + tests/functional/hexagon/test_arch_tests.py | 12 +- tests/functional/hexagon/test_systests.py | 94 ++++ tests/tcg/hexagon/Makefile.target | 3 + 47 files changed, 1573 insertions(+), 138 deletions(-) create mode 100644 target/hexagon/common-semi-target.c create mode 100644 tests/tcg/hexagon/icinva.c create mode 100644 tests/tcg/hexagon/privileged-insn.c create mode 100755 tests/functional/hexagon/test_systests.py
[PULL 08/28] semihosting: add ftruncate helper (to be used for hexagon)
From: Matheus Tavares Bernardino <[email protected]>
The common semihosting syscall layer has no ftruncate helper, but the Hexagon semihosting ABI includes an FTRUNC operation. Add semihost_sys_ftruncate() so that Hexagon can delegate to it rather than calling ftruncate directly.
--- include/semihosting/syscalls.h | 2 ++ semihosting/syscalls.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
include/semihosting/syscalls.h +2 -0
@@ -75,4 +75,6 @@ void semihost_sys_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete,void semihost_sys_poll_one (CPUState *cs,gdb_syscall_complete_cb complete,int fd,GIOCondition cond,int timeout); +void semihost_sys_ftruncate (CPUState *cs,gdb_syscall_complete_cb complete, +int fd,off_t len);#endif /* SEMIHOSTING_SYSCALLS_H */
semihosting/syscalls.c +29 -0
@@ -8,10 +8,12 @@#include "qemu/osdep.h" #include "qemu/log.h" +#include "qemu/error-report.h" #include "gdbstub/syscalls.h" #include "semihosting/guestfd.h" #include "semihosting/syscalls.h" #include "semihosting/console.h" +#include "semihosting/semihost.h" #ifdef CONFIG_USER_ONLY #include "qemu.h" #else
@@ -541,6 +543,13 @@ static void host_poll_one(CPUState *cs, gdb_syscall_complete_cb complete, }#endif +static void host_ftruncate (CPUState *cs,gdb_syscall_complete_cb complete, +GuestFD *gf,off_t len) +{ +int err =ftruncate (gf->hostfd, len); +complete (cs, err, err <0 ? errno :0 ); +} +/* * Static file semihosting syscall implementations. */
@@ -982,3 +991,23 @@ void semihost_sys_poll_one(CPUState *cs, gdb_syscall_complete_cb complete, } }#endif + +void semihost_sys_ftruncate (CPUState *cs,gdb_syscall_complete_cb complete, +int fd,off_t len) +{ +GuestFD *gf =get_guestfd (fd); +if (!gf) { +complete (cs,-1 ,EBADF ); +return ; + } + +switch (gf->type) { +case GuestFDHost: +host_ftruncate (cs, complete, gf, len); +break ; +default : +error_report ("ftruncate call not implemented " +"for this semihosting mode." ); +g_assert_not_reached (); + } +}
Signature · 1 line
2.34.1
[PULL 14/28] target/hexagon: align exceptions for user/sysemu
In reply to Brian CainSystem mode reports an exception as cs->exception_index = HEX_EVENT_* plus env->cause_code = HEX_CAUSE_*, but translated code in user mode put the cause code straight into exception_index, so cpu_loop() was decoding both forms. gen_exception_decode_fail() and the misaligned-PC check used the raw form unconditionally, so in system mode the cause code was misread as an event number.
Use the {event, cause} everywhere and drop the duplicated cases from cpu_loop(), which fixes HEX_CAUSE_PRIV_USER_NO_SINSN and HEX_CAUSE_PRIV_USER_NO_GINSN. The misaligned PC is no longer zeroed on its way out either, so it reaches the signal frame as si_addr instead of whatever r31 held.
--- target/hexagon/translate.h | 2 +- linux-user/hexagon/cpu_loop.c | 30 +++++++++++------------------- target/hexagon/cpu.c | 3 ++- target/hexagon/translate.c | 27 +++++++++------------------ 4 files changed, 23 insertions(+), 39 deletions(-)
target/hexagon/translate.h +1 -1
@@ -330,7 +330,7 @@ extern TCGv_i32 hex_t_sreg[NUM_SREGS];#endif -void hex_gen_exception_end_tb (DisasContext *ctx,int excp); +void hex_gen_exception_end_tb (DisasContext *ctx,int cause);void process_store (DisasContext *ctx,int slot_num);
linux-user/hexagon/cpu_loop.c +11 -19
@@ -66,21 +66,22 @@ void cpu_loop(CPUHexagonState *env)case HEX_CAUSE_FETCH_NO_UPAGE :case HEX_CAUSE_PRIV_NO_UREAD :case HEX_CAUSE_PRIV_NO_UWRITE : -force_sig_fault (TARGET_SIGSEGV ,TARGET_SEGV_MAPERR , - env->gpr[HEX_REG_PC ]); - -break ; +force_sig_fault (TARGET_SIGSEGV ,TARGET_SEGV_MAPERR , + env->gpr[HEX_REG_PC ]); +break ;case HEX_CAUSE_PRIV_USER_NO_GINSN :case HEX_CAUSE_PRIV_USER_NO_SINSN :case HEX_CAUSE_INVALID_PACKET : -force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPC , - env->gpr[HEX_REG_PC ]); -break ; +case HEX_CAUSE_REG_WRITE_CONFLICT : +force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPC , + env->gpr[HEX_REG_PC ]); +break ;case HEX_CAUSE_MISALIGNED_LOAD :case HEX_CAUSE_MISALIGNED_STORE : -force_sig_fault (TARGET_SIGBUS ,TARGET_BUS_ADRALN , - env->gpr[HEX_REG_PC ]); -break ; +case HEX_CAUSE_PC_NOT_ALIGNED : +force_sig_fault (TARGET_SIGBUS ,TARGET_BUS_ADRALN , + env->gpr[HEX_REG_PC ]); +break ;default : EXCP_DUMP(env, "\nqemu: unhandled CPU precise exception " "cause code 0x%x - aborting\n",
@@ -88,15 +89,6 @@ void cpu_loop(CPUHexagonState *env)exit (EXIT_FAILURE ); }break ; -case HEX_CAUSE_PC_NOT_ALIGNED : -force_sig_fault (TARGET_SIGBUS ,TARGET_BUS_ADRALN , - env->gpr[HEX_REG_R31 ]); -break ; -case HEX_CAUSE_INVALID_PACKET : -case HEX_CAUSE_REG_WRITE_CONFLICT : -force_sig_fault (TARGET_SIGILL ,TARGET_ILL_ILLOPC , - env->gpr[HEX_REG_PC ]); -break ;case EXCP_ATOMIC :cpu_exec_step_atomic (cs);break ;
target/hexagon/cpu.c +2 -1
@@ -323,7 +323,8 @@ static TCGTBCPUState hexagon_get_tb_cpu_state(CPUState *cs) hex_flags =FIELD_DP32 (hex_flags,TB_FLAGS ,IS_TIGHT_LOOP ,1 ); }if (pc &PCALIGN_MASK ) { -hexagon_raise_exception_err (env,HEX_CAUSE_PC_NOT_ALIGNED ,0 ); + env->cause_code =HEX_CAUSE_PC_NOT_ALIGNED ; +hexagon_raise_exception_err (env,HEX_EVENT_PRECISE , pc); }#ifndef CONFIG_USER_ONLY
target/hexagon/translate.c +9 -18
@@ -73,8 +73,8 @@ TCGv hex_vstore_pending[VSTORES_MAX];#ifndef CONFIG_USER_ONLY TCGv_i32 hex_greg[NUM_GREGS ];TCGv_i32 hex_t_sreg[NUM_SREGS ]; -TCGv_i32 hex_cause_code;#endif +static TCGv_i32 hex_cause_code;static const char *const hexagon_prednames[] = {"p0" ,"p1" ,"p2" ,"p3"
@@ -128,19 +128,14 @@ intptr_t ctx_tmp_vreg_off(DisasContext *ctx, int regnum,return offset; } -static void gen_exception (int excp,uint32_t PC ) +static void gen_precise_exception (int cause,uint32_t PC ) { -gen_helper_raise_exception (tcg_env,tcg_constant_i32 (excp), +tcg_gen_movi_i32 (hex_cause_code, cause); +gen_helper_raise_exception (tcg_env,tcg_constant_i32 (HEX_EVENT_PRECISE ),tcg_constant_i32 (PC )); }#ifndef CONFIG_USER_ONLY -static inline void gen_precise_exception (int excp,uint32_t PC ) -{ -tcg_gen_movi_i32 (hex_cause_code, excp); -gen_exception (HEX_EVENT_PRECISE ,PC ); -} -static void gen_pcycle_counters (DisasContext *ctx) {if (ctx->pcycle_enabled) {
@@ -230,14 +225,10 @@ static void gen_end_tb(DisasContext *ctx) ctx->base.is_jmp =DISAS_NORETURN ; } -void hex_gen_exception_end_tb (DisasContext *ctx,int excp) +void hex_gen_exception_end_tb (DisasContext *ctx,int cause) {gen_exec_counters (ctx); -#ifdef CONFIG_USER_ONLY -gen_exception (excp, ctx->pkt.pc); -#else -gen_precise_exception (excp, ctx->pkt.pc); -#endif +gen_precise_exception (cause, ctx->pkt.pc); ctx->base.is_jmp =DISAS_NORETURN ; }
@@ -245,13 +236,13 @@ void hex_gen_exception_end_tb(DisasContext *ctx, int excp)* Generate exception for decode failures. Unlike gen_exception_end_tb, * this is used when decode fails before ctx->next_PC is initialized. */ -static void gen_exception_decode_fail (DisasContext *ctx,int nwords,int excp) +static void gen_exception_decode_fail (DisasContext *ctx,int nwords,int cause) {target_ulong fail_pc = ctx->base.pc_next + nwords *sizeof (uint32_t );gen_exec_counters (ctx);tcg_gen_movi_tl (hex_gpr[HEX_REG_PC ], fail_pc); -gen_exception (excp, fail_pc); +gen_precise_exception (cause, fail_pc); ctx->base.is_jmp =DISAS_NORETURN ; ctx->base.pc_next = fail_pc; }
@@ -1366,9 +1357,9 @@ void hexagon_translate_init(void) offsetof(CPUHexagonState, llsc_val), "llsc_val"); hex_llsc_val_i64 =tcg_global_mem_new_i64 (tcg_env, offsetof(CPUHexagonState , llsc_val_i64),"llsc_val_i64" ); -#ifndef CONFIG_USER_ONLY hex_cause_code =tcg_global_mem_new_i32 (tcg_env, offsetof(CPUHexagonState , cause_code),"cause_code" ); +#ifndef CONFIG_USER_ONLY hex_cycle_count =tcg_global_mem_new_i64 (tcg_env, offsetof(CPUHexagonState , t_cycle_count),"t_cycle_count" );#endif
Signature · 1 line
2.34.1
[PULL 07/28] target/hexagon: add semihosting support
In reply to Brian CainFrom: Matheus Tavares Bernardino <[email protected]>
Baremetal Hexagon programs use trap0 #0 to invoke semihosting calls for I/O and process control. Wire up the arm-compatible semihosting framework for softmmu by enabling CONFIG_ARM_COMPATIBLE_SEMIHOSTING and routing trap0 to the semihosting handler.
checkpatch: style fixes.
--- docs/system/target-hexagon.rst | 8 +- configs/targets/hexagon-softmmu.mak | 2 + hw/hexagon/hexagon_dsp.c | 2 + target/hexagon/common-semi-target.c | 51 ++++++++ target/hexagon/hexswi.c | 191 +++++++++++++++++++++++++++- hw/hexagon/Kconfig | 1 + qemu-options.hx | 8 +- target/hexagon/meson.build | 1 + 8 files changed, 254 insertions(+), 10 deletions(-) create mode 100644 target/hexagon/common-semi-target.c
docs/system/target-hexagon.rst +5 -3
@@ -91,9 +91,11 @@ Semihosting ----------- Hexagon supports a semihosting interface similar to other architectures'. The ``trap0`` instruction can activate these semihosting calls so that the -guest software can access the host console and filesystem. Semihosting -is not yet implemented in QEMU hexagon. - +guest software can access the host console and filesystem. Read the +`Hexagon Semihosting Specification +<https://docs.qualcomm.com/doc/80-N2040-101_102648/topic/semihosting-specification.html>`__ +for details. Semihosting is enabled by default on hexagon-sim-compatible +machines. This can be further configured through ``-semihosting-config``. Hexagon Features ================
configs/targets/hexagon-softmmu.mak +2 -0
@@ -6,3 +6,5 @@ TARGET_LONG_BITS=32 TARGET_NOT_USING_LEGACY_LDST_PHYS_API=y TARGET_NOT_USING_LEGACY_NATIVE_ENDIAN_API=y TARGET_NEED_FDT=y +CONFIG_SEMIHOSTING=y +CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y
hw/hexagon/hexagon_dsp.c +2 -0
@@ -26,6 +26,7 @@#include "target/hexagon/internal.h" #include "system/physmem.h" #include "system/reset.h" +#include "semihosting/semihost.h" #include "machine_cfg_v66g_1024.h.inc"
@@ -153,6 +154,7 @@ static void init_mc(MachineClass *mc) mc->no_serial =1 ; mc->is_default = false; mc->max_cpus =8 ; +qemu_semihosting_enable (); }/* ----------------------------------------------------------------- */
target/hexagon/common-semi-target.c new file +51 -0
51 lines not on this page · raw message
target/hexagon/hexswi.c +188 -3
205 lines not on this page · raw message
hw/hexagon/Kconfig +1 -0
@@ -5,6 +5,7 @@ config HEX_DSP select CPU_CLUSTER select HEX_L2VIC select HEX_QTIMER + select ARM_COMPATIBLE_SEMIHOSTING config HEX_VIRT bool
qemu-options.hx +4 -4
@@ -5515,7 +5515,7 @@ ERST DEF("semihosting", 0, QEMU_OPTION_semihosting, "-semihosting semihosting mode\n", QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | - QEMU_ARCH_MIPS | QEMU_ARCH_RISCV) + QEMU_ARCH_MIPS | QEMU_ARCH_RISCV | QEMU_ARCH_HEXAGON) SRST ``-semihosting`` Enable :ref:`Semihosting` mode (ARM, M68K, Xtensa, MIPS, RISC-V only).
@@ -5531,11 +5531,11 @@ DEF("semihosting-config", HAS_ARG, QEMU_OPTION_semihosting_config, "-semihosting-config [enable=on|off][,target=native|gdb|auto][,chardev=id][,userspace=on|off][,arg=str[,...]]\n" \ " semihosting configuration\n", QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | -QEMU_ARCH_MIPS | QEMU_ARCH_RISCV) +QEMU_ARCH_MIPS | QEMU_ARCH_RISCV | QEMU_ARCH_HEXAGON) SRST ``-semihosting-config [enable=on|off][,target=native|gdb|auto][,chardev=id][,userspace=on|off][,arg=str[,...]]`` - Enable and configure :ref:`Semihosting` (ARM, M68K, Xtensa, MIPS, RISC-V - only). + Enable and configure :ref:`Semihosting` (ARM, M68K, Xtensa, MIPS, RISC-V, + Hexagon only). .. warning:: Note that this allows guest direct access to the host filesystem, so
target/hexagon/meson.build +1 -0
@@ -261,6 +261,7 @@ hexagon_softmmu_ss.add(files( 'hex_interrupts.c', 'hexswi.c', 'machine.c', + 'common-semi-target.c', )) #
Signature · 1 line
2.34.1
[PULL 09/28] target/hexagon: add main arch-specific semihosting operations
In reply to Brian CainFrom: Matheus Tavares Bernardino <[email protected]>
The Hexagon semihosting ABI extends the arm-compatible set with operations like OPEN, WRITECREG, WRITE0, ISTTY, STAT, FSTAT, FTELL, SEEK, FTRUNC, ACCESS, and GETCWD. Implement these trap0 handlers so that baremetal programs using the standard Hexagon simulator ABI can perform file I/O when running on qemu-system-hexagon.
checkpatch: style fixes.
--- include/semihosting/common-semi.h | 1 + semihosting/arm-compat-semi.c | 2 +- target/hexagon/hexswi.c | 290 +++++++++++++++++++++++++++++- 3 files changed, 291 insertions(+), 2 deletions(-)
include/semihosting/common-semi.h +1 -0
@@ -34,6 +34,7 @@#ifndef COMMON_SEMI_H #define COMMON_SEMI_H +void common_semi_cb (CPUState *cs,uint64_t ret,int err);void do_common_semihosting (CPUState *cs);uint64_t common_semi_arg (CPUState *cs,int argno);void common_semi_set_ret (CPUState *cs,uint64_t ret);
semihosting/arm-compat-semi.c +1 -1
@@ -229,7 +229,7 @@ static inline uint32_t get_swi_errno(CPUState *cs)#endif } -static void common_semi_cb (CPUState *cs,uint64_t ret,int err) +void common_semi_cb (CPUState *cs,uint64_t ret,int err) {if (err) {#ifdef CONFIG_USER_ONLY
target/hexagon/hexswi.c +289 -1
312 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PULL 09/28] target/hexagon: add main arch-specific semihosting operations
Quoted 8 lines · Brian Cain
From: Matheus Tavares Bernardino <[email protected]>
The Hexagon semihosting ABI extends the arm-compatible set with operations like OPEN, WRITECREG, WRITE0, ISTTY, STAT, FSTAT, FTELL, SEEK, FTRUNC, ACCESS, and GETCWD. Implement these trap0 handlers so that baremetal programs using the standard Hexagon simulator ABI can perform file I/O when running on qemu-system-hexagon.
Hi; Coverity points out an issue in the filename buffer handling in this code.
Quoted 18 lines
target/hexagon/hexswi.c +17
-1-/* TODO: implement other hexagon-specific semihosting calls */ +case HEX_SYS_OPEN : + { +char filename[BUFSIZ ]; +target_ulong physical_filename_addr; +unsigned int filemode; +int length; +int real_openmode; +int ret, err =0 ; +int i =0 ; + +hexagon_read_memory (env, swi_info,4 , &physical_filename_addr, retaddr); +hexagon_read_memory (env, swi_info +4 ,4 , &filemode, retaddr); +hexagon_read_memory (env, swi_info +8 ,4 , &length, retaddr); + +if (length >=BUFSIZ ) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: filename too large (%d)\n",
target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 274
Quoted 10 lines
target/hexagon/hexswi.c +10
-0+ __func__, length); +common_semi_cb (cs,-1 ,ENAMETOOLONG ); +break ; + } + +do { +hexagon_read_memory (env, physical_filename_addr + i,1 , + &filename[i], retaddr); + i++; + }while (filename[i -1 ]);
Coverity didn't notice this one, but I did. We read into a local buffer from guest memory, so we can't trust that the guest actually put a NUL byte terminator in. If it doesn't, then we'll happily keep loading from guest memory and overflow the filename[] array. This needs a termination check involving sizeof(filename).
Quoted 8 lines
target/hexagon/hexswi.c +8
-0+case HEX_SYS_STAT : +case HEX_SYS_FSTAT : + { +struct stat st_buf; +uint8_t *st_bufptr = (uint8_t *)&sys_stat; +int rc, err =0 ; +char filename[BUFSIZ ]; +target_ulong physical_filename_addr;
target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 353
Quoted 10 lines
target/hexagon/hexswi.c +10
-0+target_ulong statBufferAddr; +hexagon_read_memory (env, swi_info,4 , &physical_filename_addr, retaddr); + +if (what_swi ==HEX_SYS_STAT ) { +int i =0 ; +do { +hexagon_read_memory (env, physical_filename_addr + i,1 , + &filename[i], retaddr); + i++; + }while ((i <BUFSIZ ) && filename[i -1 ]);
This one does have a check on BUFSIZ, but if we terminate the loop because of that then filename[] won't be NUL terminated and stat() might blow up when we pass it.
Quoted 9 lines
target/hexagon/hexswi.c +8
-0+ rc =stat (filename, &st_buf); + err = errno; +case HEX_SYS_ACCESS : + { +char filename[BUFSIZ ]; +uint32_t FileNameAddr; +uint32_t BufferMode; +int rc;
target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 414
Quoted 10 lines
target/hexagon/hexswi.c +10
-0+ +int i =0 ; + +hexagon_read_memory (env, swi_info,4 , &FileNameAddr, retaddr); +do { +hexagon_read_memory (env, FileNameAddr + i,1 , &filename[i], + retaddr); + i++; + }while ((i <BUFSIZ ) && (filename[i -1 ])); + filename[i] =0 ;
This one is wrong in yet a third way: we do check against BUFSIZ, and we try to add in a possible missing NUL terminator, but if we stopped the loop because i == BUFSIZ, then we will write the NUL terminator off the end of the array. (This is the one Coverity found: CID 1685942.)
Better still, can't we use the lock_user_string() handling that the common-semihosting code does? That avoids all this need for tedious manual byte-at-a-time looping and fixed sized buffers and potential for bugs.
thanks
-- PMM
Re: [PULL 09/28] target/hexagon: add main arch-specific semihosting operations
Quoted 91 lines · Peter Maydell
Quoted 7 lines · Brian Cain
From: Matheus Tavares Bernardino <[email protected]>
The Hexagon semihosting ABI extends the arm-compatible set with operations like OPEN, WRITECREG, WRITE0, ISTTY, STAT, FSTAT, FTELL, SEEK, FTRUNC, ACCESS, and GETCWD. Implement these trap0 handlers so that baremetal programs using the standard Hexagon simulator ABI can perform file I/O when running on qemu-system-hexagon.
Hi; Coverity points out an issue in the filename buffer handling in this code.
Quoted 18 lines
target/hexagon/hexswi.c +17
-1-/* TODO: implement other hexagon-specific semihosting calls */ +case HEX_SYS_OPEN : + { +char filename[BUFSIZ ]; +target_ulong physical_filename_addr; +unsigned int filemode; +int length; +int real_openmode; +int ret, err =0 ; +int i =0 ; + +hexagon_read_memory (env, swi_info,4 , &physical_filename_addr, retaddr); +hexagon_read_memory (env, swi_info +4 ,4 , &filemode, retaddr); +hexagon_read_memory (env, swi_info +8 ,4 , &length, retaddr); + +if (length >=BUFSIZ ) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: filename too large (%d)\n",target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 274
Quoted 10 lines
target/hexagon/hexswi.c +10
-0+ __func__, length); +common_semi_cb (cs,-1 ,ENAMETOOLONG ); +break ; + } + +do { +hexagon_read_memory (env, physical_filename_addr + i,1 , + &filename[i], retaddr); + i++; + }while (filename[i -1 ]);Coverity didn't notice this one, but I did. We read into a local buffer from guest memory, so we can't trust that the guest actually put a NUL byte terminator in. If it doesn't, then we'll happily keep loading from guest memory and overflow the filename[] array. This needs a termination check involving sizeof(filename).
Quoted 8 lines
target/hexagon/hexswi.c +8
-0+case HEX_SYS_STAT : +case HEX_SYS_FSTAT : + { +struct stat st_buf; +uint8_t *st_bufptr = (uint8_t *)&sys_stat; +int rc, err =0 ; +char filename[BUFSIZ ]; +target_ulong physical_filename_addr;target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 353
Quoted 10 lines
target/hexagon/hexswi.c +10
-0+target_ulong statBufferAddr; +hexagon_read_memory (env, swi_info,4 , &physical_filename_addr, retaddr); + +if (what_swi ==HEX_SYS_STAT ) { +int i =0 ; +do { +hexagon_read_memory (env, physical_filename_addr + i,1 , + &filename[i], retaddr); + i++; + }while ((i <BUFSIZ ) && filename[i -1 ]);This one does have a check on BUFSIZ, but if we terminate the loop because of that then filename[] won't be NUL terminated and
stat()might blow up when we pass it.Quoted 8 lines
target/hexagon/hexswi.c +8
-0+ rc =stat (filename, &st_buf); + err = errno; +case HEX_SYS_ACCESS : + { +char filename[BUFSIZ ]; +uint32_t FileNameAddr; +uint32_t BufferMode; +int rc;target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 414
Quoted 10 lines
target/hexagon/hexswi.c +10
-0+ +int i =0 ; + +hexagon_read_memory (env, swi_info,4 , &FileNameAddr, retaddr); +do { +hexagon_read_memory (env, FileNameAddr + i,1 , &filename[i], + retaddr); + i++; + }while ((i <BUFSIZ ) && (filename[i -1 ])); + filename[i] =0 ;This one is wrong in yet a third way: we do check against BUFSIZ, and we try to add in a possible missing NUL terminator, but if we stopped the loop because i == BUFSIZ, then we will write the NUL terminator off the end of the array. (This is the one Coverity found: CID 1685942.)
Quoted 4 lines
Better still, can't we use the
lock_user_string()handling that the common-semihosting code does? That avoids all this need for tedious manual byte-at-a-time looping and fixed sized buffers and potential for bugs.
I'll investigate and fix the issues raised - thanks for pointing them out, Peter.
[PULL 04/28] target/hexagon: add aux functions for guest mem load/store
In reply to Brian CainFrom: Matheus Tavares Bernardino <[email protected]>
Will be used for semihosting.
--- target/hexagon/cpu_helper.h | 6 +++ target/hexagon/cpu_helper.c | 100 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+)
target/hexagon/cpu_helper.h +6 -0
@@ -7,6 +7,12 @@#ifndef HEXAGON_CPU_HELPER_H #define HEXAGON_CPU_HELPER_H +void hexagon_read_memory (CPUHexagonState *env,target_ulong vaddr,int size, +void *retptr,uintptr_t retaddr); +void hexagon_write_memory (CPUHexagonState *env,target_ulong vaddr, +int size,uint64_t data,uintptr_t retaddr); +void hexagon_peek_memory_range (CPUHexagonState *env,uint32_t start_addr, +uint32_t length,uintptr_t retaddr);uint32_t hexagon_get_pmu_counter (CPUHexagonState *cur_env,int index);void hexagon_modify_ssr (CPUHexagonState *env,uint32_t new,uint32_t old);int get_cpu_mode (CPUHexagonState *env);
target/hexagon/cpu_helper.c +100 -0
106 lines not on this page · raw message
Signature · 1 line
2.34.1
[PULL 02/28] target/hexagon: fix store-conditional on big-endian hosts
In reply to Brian CainFrom: Matheus Tavares Bernardino <[email protected]>
gen_load_locked4u() and gen_load_locked8u() read guest memory with an explicit MO_LE, but gen_store_conditional4() and gen_store_conditional8() are missing that, so we wrongly access guest memory in host byte order.
--- target/hexagon/genptr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
target/hexagon/genptr.c +2 -2
@@ -576,7 +576,7 @@ static inline void gen_store_conditional4(DisasContext *ctx, zero =tcg_constant_tl (0 ); tmp =tcg_temp_new ();tcg_gen_atomic_cmpxchg_tl (tmp, hex_llsc_addr, hex_llsc_val, src, - ctx->mem_idx,MO_32 |MO_ALIGN ); + ctx->mem_idx,MO_LE |MO_32 |MO_ALIGN );tcg_gen_movcond_tl (TCG_COND_EQ , pred, tmp, hex_llsc_val, one, zero);tcg_gen_br (done);
@@ -601,7 +601,7 @@ static inline void gen_store_conditional8(DisasContext *ctx, zero =tcg_constant_i64 (0 ); tmp =tcg_temp_new_i64 ();tcg_gen_atomic_cmpxchg_i64 (tmp, hex_llsc_addr, hex_llsc_val_i64, src, - ctx->mem_idx,MO_64 |MO_ALIGN ); + ctx->mem_idx,MO_LE |MO_64 |MO_ALIGN );tcg_gen_movcond_i64 (TCG_COND_EQ , tmp, tmp, hex_llsc_val_i64, one, zero);tcg_gen_extrl_i64_i32 (pred, tmp);
Signature · 1 line
2.34.1
[PULL 20/28] target/hexagon: read UTIMERLO/UTIMERHI from the global timer
In reply to Brian CainIn system mode, route accesses to the qtimer-backed global TIMERLO/TIMERHI so guest reads see a live, monotonically increasing timer.
In user mode we derive it from the host monotonic clock.
--- target/hexagon/helper.h | 4 ++++ target/hexagon/genptr.c | 29 +++++++++++++++++++++++++++++ target/hexagon/op_helper.c | 19 +++++++++++++++++++ tests/tcg/hexagon/reg_mut.c | 11 ++++++++--- 4 files changed, 60 insertions(+), 3 deletions(-)
target/hexagon/helper.h +4 -0
@@ -113,6 +113,10 @@ DEF_HELPER_FLAGS_4(gvec_sabsdiff_w, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32) DEF_HELPER_FLAGS_4(gvec_uabsdiff_b, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32) DEF_HELPER_FLAGS_4(gvec_uabsdiff_h, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32) +#if defined(CONFIG_USER_ONLY ) +DEF_HELPER_FLAGS_0(utimer, TCG_CALL_NO_RWG, i64) +#endif +#if !defined(CONFIG_USER_ONLY ) DEF_HELPER_3(raise_stack_overflow, void, env, i32, i32)DEF_HELPER_2 (swi, void, env, i32)
target/hexagon/genptr.c +29 -0
@@ -411,6 +411,23 @@ static inline void gen_read_ctrl_reg(DisasContext *ctx, const int reg_num, }else if (reg_num ==HEX_REG_QEMU_HVX_CNT ) {tcg_gen_addi_tl (dest, hex_gpr[HEX_REG_QEMU_HVX_CNT ], ctx->num_hvx_insns); +#ifndef CONFIG_USER_ONLY + }else if (reg_num ==HEX_REG_UTIMERLO ) { +gen_helper_sreg_read (dest, tcg_env, +tcg_constant_i32 (HEX_SREG_TIMERLO )); + }else if (reg_num ==HEX_REG_UTIMERHI ) { +gen_helper_sreg_read (dest, tcg_env, +tcg_constant_i32 (HEX_SREG_TIMERHI )); +#else + }else if (reg_num ==HEX_REG_UTIMERLO ) { +TCGv_i64 utimer =tcg_temp_new_i64 (); +gen_helper_utimer (utimer); +tcg_gen_extrl_i64_i32 (dest, utimer); + }else if (reg_num ==HEX_REG_UTIMERHI ) { +TCGv_i64 utimer =tcg_temp_new_i64 (); +gen_helper_utimer (utimer); +tcg_gen_extrh_i64_i32 (dest, utimer); +#endif }else {tcg_gen_mov_tl (dest, hex_gpr[reg_num]); }
@@ -439,6 +456,18 @@ static inline void gen_read_ctrl_reg_pair(DisasContext *ctx, const int reg_num,tcg_gen_addi_tl (hvx_cnt, hex_gpr[HEX_REG_QEMU_HVX_CNT ], ctx->num_hvx_insns);tcg_gen_concat_i32_i64 (dest, hvx_cnt, hex_gpr[reg_num +1 ]); +#ifndef CONFIG_USER_ONLY + }else if (reg_num ==HEX_REG_UTIMERLO ) { +TCGv lo =tcg_temp_new (); +TCGv hi =tcg_temp_new (); +gen_helper_sreg_read (lo, tcg_env,tcg_constant_i32 (HEX_SREG_TIMERLO )); +gen_helper_sreg_read (hi, tcg_env,tcg_constant_i32 (HEX_SREG_TIMERHI )); +tcg_gen_concat_i32_i64 (dest, lo, hi); +#else + }else if (reg_num ==HEX_REG_UTIMERLO ) { +/* One helper call, so the pair is a coherent 64-bit snapshot. */ +gen_helper_utimer (dest); +#endif }else { tcg_gen_concat_i32_i64(dest, hex_gpr[reg_num],
target/hexagon/op_helper.c +19 -0
@@ -37,6 +37,9 @@#include "cpu_helper.h" #include "tcg/tcg-gvec-desc.h" #include "translate.h" +#ifdef CONFIG_USER_ONLY +#include "qemu/timer.h" +#endif #ifndef CONFIG_USER_ONLY #include "hw/hexagon/hexagon_globalreg.h" #include "hex_mmu.h"
@@ -46,6 +49,22 @@#include "hexswi.h" #endif +#ifdef CONFIG_USER_ONLY +/* +* User mode has no qtimer device backing TIMERLO/TIMERHI, so derive the +* user timer from the host monotonic clock at the qtimer's default 19.2MHz +* tick rate, masked to the qtimer's counter width. +*/ +#define HEX_UTIMER_FREQ_HZ 19200000ULL +#define HEX_UTIMER_CNT_MASK 0x00ffffffffffffffULL + +uint64_t HELPER (utimer )(void ) +{ +return muldiv64 (get_clock (),HEX_UTIMER_FREQ_HZ ,NANOSECONDS_PER_SECOND ) & +HEX_UTIMER_CNT_MASK ; +} +#endif +#define SF_BIAS 127#define SF_MANTBITS 23
tests/tcg/hexagon/reg_mut.c +8 -3
@@ -77,10 +77,10 @@ static inline void write_control_registers(void)check32 (result,0x00000000 );WRITE_REG_NOCLOBBER (result,"utimerlo" ,0xffffffff ); -check32 (result,0x00000000 ); +check32_ne (result,0xffffffff );WRITE_REG_NOCLOBBER (result,"utimerhi" ,0xffffffff ); -check32 (result,0x00000000 ); +check32_ne (result,0xffffffff );/* * PC is special. Setting it to these values
@@ -106,8 +106,13 @@ static inline void write_control_register_pairs(void)WRITE_REG_NOCLOBBER (result,"c15:14" ,0xffffffffffffffff );check64 (result,0x0000000000000000 ); +/* +* c31:30 is UTIMERHI:UTIMERLO, a read-only free-running counter. The +* write must be discarded; the read-back is whatever the timer says, +* so only check that the written value did not stick. +*/ WRITE_REG_NOCLOBBER (result,"c31:30" ,0xffffffffffffffff ); -check64 (result,0x0000000000000000 ); +check64_ne (result,0xffffffffffffffff );WRITE_REG_PAIR_ENCODED (result,"c9:8" , (uint64_t )0x0000000000000000 ,C9_8_EQ_R1_0 );
Signature · 1 line
2.34.1
[PULL 03/28] target/hexagon: fix PC advancement for non-COF TB terminators
In reply to Brian CainFrom: Matheus Tavares Bernardino <[email protected]>
Some packets end a translation block without a change-of-flow. These do not update the PC during commit, so gen_end_tb() leaves hex_gpr[HEX_REG_PC] pointing at the same packet and the block gets re-entered forever. Set PC explicitly to next_PC in that gen_end_tb() branch to fix execution of next blocks.
Note that gen_start_packet() used to have a special case for tlblock/k0lock, setting ctx->next_PC to the packet's own address. This relies on the tlblock/k0lock helpers to properly advance the PC, which is never done (only env->next_PC is updated, but that never reaches hex_gpr[HEX_REG_PC]). Drop the special case so these packets advance to the following packet like any other non-COF TB terminator.
--- target/hexagon/translate.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
target/hexagon/translate.c +9 -4
@@ -217,6 +217,12 @@ static void gen_end_tb(DisasContext *ctx)gen_goto_tb (ctx,0 , ctx->base.tb->pc, true);gen_set_label (skip);gen_goto_tb (ctx,1 , ctx->next_PC, false); + }else if (!ctx->pkt.pkt_has_cof) { +/* +* Packet with no deferred COF that still ends the TB. PC is +* not updated during commit, so set it explicitly to next_PC. +*/ +gen_goto_tb (ctx,0 , ctx->next_PC, true); }else {tcg_gen_lookup_and_goto_ptr (); }
@@ -289,6 +295,7 @@ static bool check_for_attrib(Packet *pkt, int attrib)return false; } +#ifndef CONFIG_USER_ONLY static bool check_for_opcode (Packet *pkt,uint16_t opcode) {for (int i =0 ; i < pkt->num_insns; i++) {
@@ -298,6 +305,7 @@ static bool check_for_opcode(Packet *pkt, uint16_t opcode) }return false; } +#endif static bool need_slot_cancelled (Packet *pkt) {
@@ -577,10 +585,7 @@ static void analyze_packet(DisasContext *ctx)static void gen_start_packet (DisasContext *ctx) {Packet *pkt = &ctx->pkt; -target_ulong next_PC = (check_for_opcode (pkt, Y2_k0lock) || -check_for_opcode (pkt, Y2_tlblock)) ? - ctx->base.pc_next : - ctx->base.pc_next + pkt->encod_pkt_size_in_bytes; +target_ulong next_PC = ctx->base.pc_next + pkt->encod_pkt_size_in_bytes;int i;/* Clear out the disassembly context */
Signature · 1 line
2.34.1
[PULL 17/28] target/hexagon: guard writes to unimplemented guest registers
In reply to Brian CainGuest register G4 and above have PMU-ish features that are yet to be implemented. We'll skip these when we encounter this code rather than fault on an assertion.
Note that the generated call to greg_writable() has the side-effect of doing a LOG_UNIMP for these register accesses.
--- target/hexagon/translate.h | 4 +++- target/hexagon/hex_common.py | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-)
target/hexagon/translate.h +3 -1
@@ -97,7 +97,9 @@ bool is_gather_store_insn(DisasContext *ctx);#ifndef CONFIG_USER_ONLY static inline void ctx_log_greg_write (DisasContext *ctx,int rnum) { -assert (rnum <=HEX_GREG_G3 ); +if (rnum >HEX_GREG_G3 ) { +return ; + } ctx->greg_log[ctx->greg_log_idx] = rnum; ctx->greg_log_idx++; }
target/hexagon/hex_common.py +17 -1
@@ -1097,11 +1097,24 @@ def analyze_write(self, f, tag, regno): """)) class GuestRegister(Register): - pass + def gen_check_impl(self, f, regno): + if self.is_written(): + f.write(code_fmt(f"""\ + if (!greg_writable(insn->regno[{regno}], + {str(self.is_pair()).lower()})) {{ + return; + }} + """)) + else: + f.write(code_fmt(f"""\ + check_greg_impl(insn->regno[{regno}], + {str(self.is_pair()).lower()}); + """)) class GuestDest(GuestRegister, Single, Dest): def decl_tcg(self, f, tag, regno): self.decl_reg_num(f, regno) + self.gen_check_impl(f, regno) f.write(code_fmt(f"""\ TCGv_i32 {self.reg_tcg()} = tcg_temp_new_i32(); """))
@@ -1121,6 +1134,7 @@ def decl_reg_num(self, f, regno): """)) def decl_tcg(self, f, tag, regno): self.decl_reg_num(f, regno) + self.gen_check_impl(f, regno) f.write(code_fmt(f"""\ TCGv_i32 {self.reg_tcg()} = tcg_temp_new_i32(); gen_read_greg({self.reg_tcg()}, {self.reg_num});
@@ -1131,6 +1145,7 @@ def analyze_read(self, f, regno): class GuestPairDest(GuestRegister, Pair, Dest): def decl_tcg(self, f, tag, regno): self.decl_reg_num(f, regno) + self.gen_check_impl(f, regno) f.write(code_fmt(f"""\ TCGv_i64 {self.reg_tcg()} = tcg_temp_new_i64(); """))
@@ -1150,6 +1165,7 @@ def decl_reg_num(self, f, regno): """)) def decl_tcg(self, f, tag, regno): self.decl_reg_num(f, regno) + self.gen_check_impl(f, regno) f.write(code_fmt(f"""\ TCGv_i64 {self.reg_tcg()} = tcg_temp_new_i64(); gen_read_greg_pair({self.reg_tcg()}, {self.reg_num});
Signature · 1 line
2.34.1
[PULL 21/28] target/hexagon: raise imprecise exception on multi-TLB match
In reply to Brian Caintlbp now records a pending imprecise exception (via env->imprecise_exception) when the lookup matches multiple entries, and the translator raises it after the tlbp packet. Implement the HEX_EVENT_IMPRECISE delivery path so the guest sees HEX_CAUSE_IMPRECISE_MULTI_TLB_MATCH instead of the exception being silently dropped.
--- include/hw/hexagon/hexagon_tlb.h | 3 ++- target/hexagon/cpu.h | 1 + hw/hexagon/hexagon_tlb.c | 5 ++++- target/hexagon/hex_mmu.c | 5 ++++- target/hexagon/hexswi.c | 37 +++++++++++++++++++++++++++++--- target/hexagon/machine.c | 5 +++-- target/hexagon/translate.c | 29 +++++++++++++++++++++++++ 7 files changed, 77 insertions(+), 8 deletions(-)
include/hw/hexagon/hexagon_tlb.h +2 -1
@@ -32,7 +32,8 @@ bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid, int32_t *excp, int *cause_code, int mmu_idx);uint32_t hexagon_tlb_lookup (HexagonTLBState *tlb,uint32_t asid, -uint32_t VA ,int *cause_code); +uint32_t VA ,uint32_t *imprecise_exception, +int *cause_code);int hexagon_tlb_check_overlap (HexagonTLBState *tlb,uint64_t entry,uint64_t index);
target/hexagon/cpu.h +1 -0
@@ -147,6 +147,7 @@ typedef struct CPUArchState {uint64_t t_cycle_count;#endif uint32_t next_PC; +uint32_t imprecise_exception;target_ulong new_value_usr;MemLog mem_log_stores[STORES_MAX ];
hw/hexagon/hexagon_tlb.c +4 -1
@@ -319,15 +319,18 @@ bool hexagon_tlb_find_match(HexagonTLBState *tlb, uint32_t asid, }uint32_t hexagon_tlb_lookup (HexagonTLBState *tlb,uint32_t asid, -uint32_t VA ,int *cause_code) +uint32_t VA ,uint32_t *imprecise_exception, +int *cause_code) {uint32_t not_found =0x80000000 ;uint32_t idx = not_found; + *imprecise_exception =0 ;for (uint32_t i =0 ; i < tlb->num_entries; i++) {uint64_t entry = tlb->entries[i];if (hex_tlb_entry_match_noperm (entry, asid,VA )) {if (idx != not_found) { + *imprecise_exception =HEX_EVENT_IMPRECISE ; *cause_code =HEX_CAUSE_IMPRECISE_MULTI_TLB_MATCH ;break ; }
target/hexagon/hex_mmu.c +4 -1
@@ -86,9 +86,12 @@ uint32_t hex_tlb_lookup(CPUHexagonState *env, uint32_t ssr, uint32_t VA) {HexagonCPU *cpu =env_archcpu (env);uint8_t asid =GET_SSR_FIELD (SSR_ASID , ssr); +uint32_t imprecise_exception =0 ;int cause_code =0 ; -uint32_t result =hexagon_tlb_lookup (cpu->tlb, asid,VA , &cause_code); +uint32_t result =hexagon_tlb_lookup (cpu->tlb, asid,VA , + &imprecise_exception, &cause_code); + env->imprecise_exception = imprecise_exception;if (cause_code) { env->cause_code = cause_code; }
target/hexagon/hexswi.c +34 -3
@@ -761,6 +761,7 @@ void hexagon_cpu_do_interrupt(CPUState *cs) {CPUHexagonState *env =cpu_env (cs); +HexagonCPU *cpu =HEXAGON_CPU (cs);uint32_t ssr;BQL_LOCK_GUARD ();
@@ -775,7 +776,6 @@ void hexagon_cpu_do_interrupt(CPUState *cs) ssr = env->t_sreg[HEX_SREG_SSR ];if (GET_SSR_FIELD (SSR_EX , ssr) ==1 ) { -HexagonCPU *cpu =env_archcpu (env);if (cpu->globalregs) {hexagon_globalreg_write (cpu->globalregs,HEX_SREG_DIAG , env->cause_code, env->threadId);
@@ -934,8 +934,39 @@ void hexagon_cpu_do_interrupt(CPUState *cs)break ;case HEX_EVENT_IMPRECISE : -qemu_log_mask (LOG_UNIMP , -"Imprecise exception: this case is not yet handled" ); +if (get_exe_mode (env) ==HEX_EXE_MODE_WAIT ) { + env->gpr[HEX_REG_PC ] = env->wait_next_pc -4 ; +clear_wait_mode (env); + } +switch (env->cause_code) { +case HEX_CAUSE_IMPRECISE_MULTI_TLB_MATCH : +hexagon_ssr_set_cause (env, env->cause_code); +set_addresses (env,4 , cs->exception_index); +if (cpu->globalregs) { +hexagon_globalreg_write (cpu->globalregs,HEX_SREG_DIAG , + (0x4 <<4 ) | (env->t_sreg[HEX_SREG_HTID ] &0xF ), + env->threadId); + } +break ; + +case HEX_CAUSE_IMPRECISE_NMI : +hexagon_ssr_set_cause (env, env->cause_code); +set_addresses (env,4 , cs->exception_index); +if (cpu->globalregs) { +hexagon_globalreg_write (cpu->globalregs,HEX_SREG_DIAG , + (0x3 <<4 ) | (env->t_sreg[HEX_SREG_HTID ] &0xF ), + env->threadId); + } +break ; + +default : +qemu_log_mask (LOG_GUEST_ERROR , +"Imprecise exception with unhandled cause 0x%x\n" , + env->cause_code); +hexagon_ssr_set_cause (env, env->cause_code); +set_addresses (env,4 , cs->exception_index); +break ; + }break ;default :
target/hexagon/machine.c +3 -2
@@ -10,8 +10,8 @@const VMStateDescription vmstate_hexagon_cpu = { .name ="cpu" , - .version_id =1 , - .minimum_version_id =1 , + .version_id =2 , + .minimum_version_id =2 , .fields = (const VMStateField []) {VMSTATE_UINT32_ARRAY (env.gpr, HexagonCPU,TOTAL_PER_THREAD_REGS ),VMSTATE_UINT32_ARRAY (env.pred, HexagonCPU,NUM_PREGS ),
@@ -26,6 +26,7 @@ const VMStateDescription vmstate_hexagon_cpu = {VMSTATE_UINT32 (env.cause_code, HexagonCPU),VMSTATE_UINT32 (env.wait_next_pc, HexagonCPU),VMSTATE_UINT64 (env.t_cycle_count, HexagonCPU), +VMSTATE_UINT32 (env.imprecise_exception, HexagonCPU),VMSTATE_END_OF_LIST () },
target/hexagon/translate.c +29 -0
@@ -65,6 +65,7 @@ TCGv hex_llsc_val;TCGv_i64 hex_llsc_val_i64;#ifndef CONFIG_USER_ONLY TCGv_i64 hex_cycle_count; +TCGv hex_imprecise_exception;#endif TCGv hex_vstore_addr[VSTORES_MAX ];TCGv hex_vstore_size[VSTORES_MAX ];
@@ -1056,6 +1057,28 @@ static void update_exec_counters(DisasContext *ctx) ctx->num_cycles +=PCYCLES_PER_PACKET ; } +#ifndef CONFIG_USER_ONLY +/* +* 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) +{ +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 ; + } + } +} +#endif +static void gen_commit_packet (DisasContext *ctx) {/*
@@ -1155,6 +1178,10 @@ static void gen_commit_packet(DisasContext *ctx) ctx->pkt.vhist_insn->generate (ctx); } +#ifndef CONFIG_USER_ONLY +check_imprecise_exception (&ctx->pkt); +#endif +if (ctx->pkt_ends_tb || ctx->base.is_jmp ==DISAS_NORETURN ) {gen_end_tb (ctx); }
@@ -1362,6 +1389,8 @@ void hexagon_translate_init(void)#ifndef CONFIG_USER_ONLY hex_cycle_count =tcg_global_mem_new_i64 (tcg_env, offsetof(CPUHexagonState , t_cycle_count),"t_cycle_count" ); + hex_imprecise_exception =tcg_global_mem_new (tcg_env, + offsetof(CPUHexagonState , imprecise_exception),"imprecise_exception" );#endif for (i =0 ; i <STORES_MAX ; i++) {snprintf (store_addr_names[i],NAME_LEN ,"store_addr_%d" , i);
Signature · 1 line
2.34.1
[PULL 01/28] disas/hexagon: honour target byte order when printing instructions
In reply to Brian CainFrom: Matheus Tavares Bernardino <[email protected]>
print_insn_hexagon() read each instruction word as a host-order integer, thus getting them inverted on a big-endian host. Instead, read into a byte buffer and convert with ldl_le_p().
--- disas/hexagon.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
disas/hexagon.c +4 -2
@@ -20,6 +20,7 @@*/ #include "qemu/osdep.h" +#include "qemu/bswap.h" #include "disas/dis-asm.h" #include "target/hexagon/cpu_bits.h"
@@ -37,9 +38,9 @@ int print_insn_hexagon(bfd_vma memaddr, struct disassemble_info *info)int i, len;for (i =0 ; i <PACKET_WORDS_MAX && !found_end; i++) { +bfd_byte insn[sizeof (uint32_t )];int status = (*info->read_memory_func)(memaddr + i *sizeof (uint32_t ), - (bfd_byte *)&words[i], -sizeof (uint32_t ), info); + insn,sizeof (uint32_t ), info);if (status) {if (i >0 ) {break ;
@@ -47,6 +48,7 @@ int print_insn_hexagon(bfd_vma memaddr, struct disassemble_info *info) (*info->memory_error_func)(status, memaddr, info);return status; } + words[i] =ldl_le_p (insn);if (is_packet_end (words[i])) { found_end = true; }
Signature · 1 line
2.34.1
[PULL 11/28] target/hexagon: Add an errno mapping
In reply to Brian CainThe Hexagon semihosting ABI defines its own errno values which may differ from the host's. Translate host errno to the Hexagon-specific values before returning results to the guest so that baremetal programs see the expected error codes.
checkpatch: style fixes.
--- target/hexagon/hexswi.c | 106 +++++++++++++++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 12 deletions(-)
target/hexagon/hexswi.c +94 -12
@@ -178,12 +178,94 @@ static void do_preload(CPUHexagonState *env, target_ulong swi_info, bool load)hexagon_peek_memory_range (env, addr, count, retaddr); } +/* Hexagon semihosting errno values */ +enum { +TARGET_EPERM =1 , +TARGET_ENOENT =2 , +TARGET_EINTR =4 , +TARGET_EIO =5 , +TARGET_ENXIO =6 , +TARGET_EBADF =9 , +TARGET_EAGAIN =11 , +TARGET_ENOMEM =12 , +TARGET_EACCES =13 , +TARGET_EFAULT =14 , +TARGET_EBUSY =16 , +TARGET_EEXIST =17 , +TARGET_EXDEV =18 , +TARGET_ENODEV =19 , +TARGET_ENOTDIR =20 , +TARGET_EISDIR =21 , +TARGET_EINVAL =22 , +TARGET_ENFILE =23 , +TARGET_EMFILE =24 , +TARGET_ENOTTY =25 , +TARGET_ETXTBSY =26 , +TARGET_EFBIG =27 , +TARGET_ENOSPC =28 , +TARGET_ESPIPE =29 , +TARGET_EROFS =30 , +TARGET_EMLINK =31 , +TARGET_EPIPE =32 , +TARGET_ERANGE =34 , +TARGET_ENAMETOOLONG =36 , +TARGET_ENOSYS =38 , +TARGET_ELOOP =40 , +TARGET_EOVERFLOW =75 , +}; + +/* Map host errno to hexagon semihosting errno */ +static uint32_t errno_h2g (uint32_t err) +{ +switch (err) { +case 0 :return 0 ; +case EPERM :return TARGET_EPERM ; +case ENOENT :return TARGET_ENOENT ; +case EINTR :return TARGET_EINTR ; +case EIO :return TARGET_EIO ; +case ENXIO :return TARGET_ENXIO ; +case EBADF :return TARGET_EBADF ; +case EAGAIN :return TARGET_EAGAIN ; +case ENOMEM :return TARGET_ENOMEM ; +case EACCES :return TARGET_EACCES ; +case EFAULT :return TARGET_EFAULT ; +case EBUSY :return TARGET_EBUSY ; +case EEXIST :return TARGET_EEXIST ; +case EXDEV :return TARGET_EXDEV ; +case ENODEV :return TARGET_ENODEV ; +case ENOTDIR :return TARGET_ENOTDIR ; +case EISDIR :return TARGET_EISDIR ; +case EINVAL :return TARGET_EINVAL ; +case ENFILE :return TARGET_ENFILE ; +case EMFILE :return TARGET_EMFILE ; +case ENOTTY :return TARGET_ENOTTY ; +case ETXTBSY :return TARGET_ETXTBSY ; +case EFBIG :return TARGET_EFBIG ; +case ENOSPC :return TARGET_ENOSPC ; +case ESPIPE :return TARGET_ESPIPE ; +case EROFS :return TARGET_EROFS ; +case EMLINK :return TARGET_EMLINK ; +case EPIPE :return TARGET_EPIPE ; +case ERANGE :return TARGET_ERANGE ; +case ENAMETOOLONG :return TARGET_ENAMETOOLONG ; +case ENOSYS :return TARGET_ENOSYS ; +case ELOOP :return TARGET_ELOOP ; +case EOVERFLOW :return TARGET_EOVERFLOW ; + } +return TARGET_EINVAL ; +} + +static void semi_cb (CPUState *cs,uint64_t ret,int err) +{ +common_semi_cb (cs, ret,errno_h2g (err)); +} +static void common_semi_ftell_cb (CPUState *cs,uint64_t ret,int err) {if (err) { ret =-1 ; } -common_semi_cb (cs, ret, err); +semi_cb (cs, ret, err); }static void coredump (CPUHexagonState *env)
@@ -395,7 +477,7 @@ static void sim_handle_trap0(CPUHexagonState *env)qemu_log_mask (LOG_GUEST_ERROR ,"%s: filename too large (%d)\n" , __func__, length); -common_semi_cb (cs,-1 ,ENAMETOOLONG ); +semi_cb (cs,-1 ,ENAMETOOLONG );break ; }
@@ -412,7 +494,7 @@ static void sim_handle_trap0(CPUHexagonState *env)qemu_log_mask (LOG_GUEST_ERROR ,"%s: invalid OPEN mode: %u\n" , __func__, filemode); -common_semi_cb (cs,-1 ,EINVAL ); +semi_cb (cs,-1 ,EINVAL );break ; }
@@ -431,7 +513,7 @@ static void sim_handle_trap0(CPUHexagonState *env) ret = guestfd; } } -common_semi_cb (cs, ret, err); +semi_cb (cs, ret, err); }break ;
@@ -450,7 +532,7 @@ static void sim_handle_trap0(CPUHexagonState *env) {int fd;hexagon_read_memory (env, swi_info,4 , &fd, retaddr); -common_semi_cb (cs,isatty (fd),0 ); +semi_cb (cs,isatty (fd),0 ); }break ;
@@ -514,7 +596,7 @@ static void sim_handle_trap0(CPUHexagonState *env)hexagon_write_memory (env, statBufferAddr + i,1 , st_bufptr[i], retaddr); } -common_semi_cb (cs, rc, rc ==0 ?0 : err); +semi_cb (cs, rc, rc ==0 ?0 : err); }break ;
@@ -524,7 +606,7 @@ static void sim_handle_trap0(CPUHexagonState *env)off_t size_limit;hexagon_read_memory (env, swi_info,4 , &fd, retaddr);hexagon_read_memory (env, swi_info +4 ,8 , &size_limit, retaddr); -semihost_sys_ftruncate (cs, common_semi_cb, fd, size_limit); +semihost_sys_ftruncate (cs, semi_cb, fd, size_limit); }break ;
@@ -548,7 +630,7 @@ static void sim_handle_trap0(CPUHexagonState *env)hexagon_read_memory (env, swi_info +4 ,4 , &BufferMode, retaddr); rc =access (filename, BufferMode); -common_semi_cb (cs, rc, rc ==0 ?0 : errno); +semi_cb (cs, rc, rc ==0 ?0 : errno); }break ;
@@ -576,14 +658,14 @@ static void sim_handle_trap0(CPUHexagonState *env) rc = BufferAddr; } } -common_semi_cb (cs, rc, rc !=0 ?0 : err); +semi_cb (cs, rc, rc !=0 ?0 : err);break ; }case HEX_SYS_EXEC : {qemu_log_mask (LOG_UNIMP ,"SYS_EXEC is deprecated\n" ); -common_semi_cb (cs,-1 ,ENOSYS ); +semi_cb (cs,-1 ,ENOSYS ); }break ;
@@ -620,7 +702,7 @@ static void sim_handle_trap0(CPUHexagonState *env)case HEX_SYS_PROF_STATSRESET :case HEX_SYS_DUMP_PMU_STATS :case HEX_SYS_HEAPINFO : -common_semi_cb (cs,-1 ,ENOSYS ); +semi_cb (cs,-1 ,ENOSYS ); qemu_log_mask(LOG_UNIMP, "SWI call %" PRIx32 " is unimplemented in QEMU\n",
@@ -631,7 +713,7 @@ static void sim_handle_trap0(CPUHexagonState *env)qemu_log_mask (LOG_UNIMP ,"unknown swi request: 0x%" PRIx32"\n" , (uint32_t )what_swi); -common_semi_cb (cs,-1 ,ENOSYS ); +semi_cb (cs,-1 ,ENOSYS ); } }
Signature · 1 line
2.34.1
[PULL 18/28] target/hexagon: take BQL when reading the system pcycle count
In reply to Brian Cainhexagon_get_sys_pcycle_count() iterates all CPUs, so take the BQL with BQL_LOCK_GUARD() instead of asserting the caller already holds it. Convert the matching setters the same way, which keeps the locking contract symmetric and makes the read-modify-write in the _low/_high setters atomic. BQL_LOCK_GUARD() is a no-op when the lock is already held, so the nested guards on the guest-register read path cost nothing.
--- target/hexagon/cpu_helper.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
target/hexagon/cpu_helper.c +8 -4
@@ -136,7 +136,7 @@ uint64_t hexagon_get_sys_pcycle_count(CPUHexagonState *env)uint64_t total =0 ;CPUState *cs; -g_assert (bql_locked ()); +BQL_LOCK_GUARD ();CPU_FOREACH (cs) {CPUHexagonState *thread_env =cpu_env (cs); total += thread_env->t_cycle_count;
@@ -154,11 +154,15 @@ uint32_t hexagon_get_sys_pcycle_count_low(CPUHexagonState *env)return (uint32_t )(hexagon_get_sys_pcycle_count (env)); } +/* +* Every function in this family takes the BQL itself, so the guard below +* holds it across the read-modify-write. Nested guards are no-ops. +*/ void hexagon_set_sys_pcycle_count_high (CPUHexagonState *env,uint32_t val) {uint64_t old; -g_assert (bql_locked ()); +BQL_LOCK_GUARD (); old =hexagon_get_sys_pcycle_count (env); old =deposit64 (old,32 ,32 , val);hexagon_set_sys_pcycle_count (env, old);
@@ -168,7 +172,7 @@ void hexagon_set_sys_pcycle_count_low(CPUHexagonState *env, uint32_t val) {uint64_t old; -g_assert (bql_locked ()); +BQL_LOCK_GUARD (); old =hexagon_get_sys_pcycle_count (env); old =deposit64 (old,0 ,32 , val);hexagon_set_sys_pcycle_count (env, old);
@@ -181,7 +185,7 @@ void hexagon_set_sys_pcycle_count(CPUHexagonState *env, uint64_t val)int num_threads;int64_t delta, per_thread, remainder; -g_assert (bql_locked ()); +BQL_LOCK_GUARD (); total =hexagon_get_sys_pcycle_count (env);/* Count active threads */
Signature · 1 line
2.34.1
[PULL 16/28] hw/intc: clear pending bit on l2vic de-assertion
In reply to Brian CainFrom: Sid Manning <[email protected]>
l2vic_set_irq() clears a level-triggered source's pending bit on de-assertion, so a source that drops before it is latched into int_status is not spuriously re-delivered on the next ciad. Edge-triggered sources (int_type set) keep the pulse semantics they already have via SOFT_INT/set_irq(level=1).
--- hw/intc/hex-l2vic.c | 2 ++ 1 file changed, 2 insertions(+)
hw/intc/hex-l2vic.c +2 -0
@@ -299,6 +299,8 @@ 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)) { +clear_bit32 (irq, s->int_pending); }l2vic_update (s, irq); }
Signature · 1 line
2.34.1
[PULL 23/28] target/hexagon: fix iassign{r, w} to cover all threads
In reply to Brian Cainiassignw/iassignr were filtering CPU_FOREACH by MODECTL_E, the thread-enabled mask. That mask reflects whether a thread has been started, not whether it exists, so IMASK writes/reads never reached threads that hadn't been started yet, leaving their IMASK stale or unreadable. Drop the MODECTL_E filtering and operate on every thread.
--- target/hexagon/op_helper.c | 42 +++++++++----------------------------- 1 file changed, 10 insertions(+), 32 deletions(-)
target/hexagon/op_helper.c +10 -32
@@ -1635,61 +1635,39 @@ void HELPER(cswi)(CPUHexagonState *env, uint32_t mask)void HELPER (iassignw )(CPUHexagonState *env,uint32_t src) { -uint32_t modectl; -uint32_t thread_enabled_mask;CPUState *cpu; -HexagonCPU *hex_cpu;BQL_LOCK_GUARD (); - hex_cpu =env_archcpu (env); - modectl = hex_cpu->globalregs ? -hexagon_globalreg_read (hex_cpu->globalregs,HEX_SREG_MODECTL , - env->threadId) :0 ; - thread_enabled_mask =GET_FIELD (MODECTL_E , modectl);CPU_FOREACH (cpu) {CPUHexagonState *thread_env = &(HEXAGON_CPU (cpu)->env); -uint32_t thread_id_mask =0x1 << thread_env->threadId; -if (thread_enabled_mask & thread_id_mask) { -uint32_t imask = thread_env->t_sreg[HEX_SREG_IMASK ]; -uint32_t intbitpos = (src >>16 ) &0xF ; -uint32_t val = (src >> thread_env->threadId) &0x1 ; - imask =deposit32 (imask, intbitpos,1 , val); - thread_env->t_sreg[HEX_SREG_IMASK ] = imask; +uint32_t imask = thread_env->t_sreg[HEX_SREG_IMASK ]; +uint32_t intbitpos = (src >>16 ) &0xF ; +uint32_t val = (src >> thread_env->threadId) &0x1 ; + imask =deposit32 (imask, intbitpos,1 , val); + thread_env->t_sreg[HEX_SREG_IMASK ] = imask; -qemu_log_mask (CPU_LOG_INT ,"%s: thread " TARGET_FMT_ld -", new imask 0x%" PRIx32"\n" , __func__, - thread_env->threadId, imask); - } +qemu_log_mask (CPU_LOG_INT ,"%s: thread " TARGET_FMT_ld +", new imask 0x%" PRIx32"\n" , __func__, + thread_env->threadId, imask); }hex_interrupt_update (env); }uint32_t HELPER (iassignr )(CPUHexagonState *env,uint32_t src) { -uint32_t modectl; -uint32_t thread_enabled_mask;uint32_t intbitpos;uint32_t dest_reg;CPUState *cpu; -HexagonCPU *hex_cpu;BQL_LOCK_GUARD (); - hex_cpu =env_archcpu (env); - modectl = hex_cpu->globalregs ? -hexagon_globalreg_read (hex_cpu->globalregs,HEX_SREG_MODECTL , - env->threadId) :0 ; - thread_enabled_mask =GET_FIELD (MODECTL_E , modectl);/* src fields are in same position as modectl, but mean different things */ intbitpos =GET_FIELD (MODECTL_W , src); dest_reg =0 ;CPU_FOREACH (cpu) {CPUHexagonState *thread_env = &(HEXAGON_CPU (cpu)->env); -uint32_t thread_id_mask =0x1 << thread_env->threadId; -if (thread_enabled_mask & thread_id_mask) { -uint32_t imask = thread_env->t_sreg[HEX_SREG_IMASK ]; - dest_reg |= ((imask >> intbitpos) &0x1 ) << thread_env->threadId; - } +uint32_t imask = thread_env->t_sreg[HEX_SREG_IMASK ]; + dest_reg |= ((imask >> intbitpos) &0x1 ) << thread_env->threadId; }return dest_reg;
Signature · 1 line
2.34.1
[PULL 12/28] python/machine: support routing semihosting output to the test console
In reply to Brian CainThe hexagon semihosting systests will wait for output on the semihosting console.
--- python/qemu/machine/machine.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
python/qemu/machine/machine.py +8 -2
@@ -186,6 +186,7 @@ def __init__(self, self._console_index = 0 self._console_set = False self._console_device_type: Optional[str] = None + self._console_semihosting = False self._console_socket: Optional[socket.socket] = None self._console_file: Optional[socket.SocketIO] = None self._remove_files: List[str] = []
@@ -321,7 +322,10 @@ def _console_args(self, interactive: bool = False) -> List[str]: fd = self._cons_sock_pair[0].fileno() chardev = f"socket,id=console,fd={fd}" args.extend(['-chardev', chardev]) - if self._console_device_type is None: + if self._console_semihosting: + args.extend(['-semihosting-config', + 'enable=on,chardev=console']) + elif self._console_device_type is None: args.extend(['-serial', 'chardev:console']) else: device = '%s,chardev=console' % self._console_device_type
@@ -892,7 +896,8 @@ def set_machine(self, machine_type: str) -> None: def set_console(self, device_type: Optional[str] = None, - console_index: int = 0) -> None: + console_index: int = 0, + semihosting: bool = False) -> None: """ Sets the device type for a console device
@@ -921,6 +926,7 @@ def set_console(self, self._console_set = True self._console_device_type = device_type self._console_index = console_index + self._console_semihosting = semihosting @property def console_socket(self) -> socket.socket:
Signature · 1 line
2.34.1
[PULL 06/28] semihosting: add callback to set error
In reply to Brian CainFrom: Matheus Tavares Bernardino <[email protected]>
This is currently unused by existing semihosting targets, but it will be used by hexagon in the following commit.
--- include/semihosting/common-semi.h | 1 + semihosting/arm-compat-semi.c | 1 + target/arm/common-semi-target.c | 4 ++++ target/riscv/common-semi-target.c | 4 ++++ 4 files changed, 10 insertions(+)
include/semihosting/common-semi.h +1 -0
@@ -37,6 +37,7 @@void do_common_semihosting (CPUState *cs);uint64_t common_semi_arg (CPUState *cs,int argno);void common_semi_set_ret (CPUState *cs,uint64_t ret); +void common_semi_set_err (CPUState *cs,int err);bool is_64bit_semihosting (CPUArchState *env);bool common_semi_sys_exit_is_extended (CPUState *cs);uint64_t common_semi_stack_bottom (CPUState *cs);
semihosting/arm-compat-semi.c +1 -0
@@ -237,6 +237,7 @@ static void common_semi_cb(CPUState *cs, uint64_t ret, int err) ts->swi_errno = err;#else syscall_err = err; +common_semi_set_err (cs, err);#endif }common_semi_set_ret (cs, ret);
target/arm/common-semi-target.c +4 -0
@@ -34,6 +34,10 @@ void common_semi_set_ret(CPUState *cs, uint64_t ret) } } +void common_semi_set_err (CPUState *cs,int err) +{ +} +bool common_semi_sys_exit_is_extended (CPUState *cs) {return is_a64 (cpu_env (cs));
target/riscv/common-semi-target.c +4 -0
@@ -26,6 +26,10 @@ void common_semi_set_ret(CPUState *cs, uint64_t ret) env->gpr[xA0] = ret; } +void common_semi_set_err (CPUState *cs,int err) +{ +} +bool is_64bit_semihosting (CPUArchState *env) {return riscv_cpu_mxl (env) !=MXL_RV32 ;
Signature · 1 line
2.34.1
[PULL 27/28] hw/hexagon: assign a distinct htid to each DSP machine CPU
In reply to Brian CainThe htid property defaults to 0 and hexagon_dsp.c never set it, so every CPU on the DSP machines came up as hardware thread 0.
This bug was causing h2 test failures.
--- hw/hexagon/hexagon_dsp.c | 2 ++ 1 file changed, 2 insertions(+)
hw/hexagon/hexagon_dsp.c +2 -0
@@ -125,6 +125,8 @@ static void hexagon_common_init(MachineState *machine, Rev_t rev,HexagonCPU *cpu =HEXAGON_CPU (object_new (machine->cpu_type));qemu_register_reset (do_cpu_reset, cpu); +qdev_prop_set_uint32 (DEVICE (cpu),"htid" , i); +/* * CPU #0 is the only CPU running at boot, others must be * explicitly enabled via start instruction.
Signature · 1 line
2.34.1
[PULL 05/28] semihosting: add APIs for chardev-aware guest fd routing
In reply to Brian CainAdd qemu_semihosting_console_has_chardev() to query whether the semihosting console is backed by a chardev, and console_guestfd() to initialize a guest file descriptor as GuestFDConsole.
These APIs enable callers to route semihosting I/O through a chardev rather than directly to host stdio.
--- include/semihosting/console.h | 9 +++++++++ include/semihosting/guestfd.h | 9 +++++++++ semihosting/console.c | 5 +++++ semihosting/guestfd.c | 8 ++++++++ 4 files changed, 31 insertions(+)
include/semihosting/console.h +9 -0
@@ -54,4 +54,13 @@ void qemu_semihosting_console_block_until_ready(CPUState *cs);*/ bool qemu_semihosting_console_ready (void ); +/** +* qemu_semihosting_console_has_chardev: +* +* Return true if the semihosting console is backed by a chardev. +* When true, console I/O goes through the chardev rather than +* host stdio. +*/ +bool qemu_semihosting_console_has_chardev (void ); +#endif /* SEMIHOST_CONSOLE_H */
include/semihosting/guestfd.h +9 -0
@@ -70,6 +70,15 @@ GuestFD *get_guestfd(int guestfd);*/ void associate_guestfd (int guestfd,int hostfd); +/** +* console_guestfd: +* @guestfd: GuestFD index +* +* Initialize the GuestFD for @guestfd to GuestFDConsole. +* I/O will be routed through the semihosting console chardev. +*/ +void console_guestfd (int guestfd); +/** * staticfile_guestfd: * @guestfd: GuestFD index
semihosting/console.c +5 -0
@@ -108,6 +108,11 @@ int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)return ret; } +bool qemu_semihosting_console_has_chardev (void ) +{ +return console.chr !=NULL ; +} +int qemu_semihosting_console_write (void *buf,int len) {if (console.chr) {
semihosting/guestfd.c +8 -0
@@ -117,6 +117,14 @@ void associate_guestfd(int guestfd, int hostfd) gf->hostfd = hostfd; } +void console_guestfd (int guestfd) +{ +GuestFD *gf =do_get_guestfd (guestfd); + +assert (gf); + gf->type = GuestFDConsole; +} +void staticfile_guestfd (int guestfd,const uint8_t *data,size_t len) {GuestFD *gf =do_get_guestfd (guestfd);
Signature · 1 line
2.34.1
[PULL 25/28] target/hexagon: invalidate translated code for user icinva
In reply to Brian CainRoute linux-user icinva through a helper that invalidates the addressed icache line under mmap_lock
For system mode icinva has nothing left to do, so it stays a nop.
--- target/hexagon/gen_tcg.h | 9 +++++++++ target/hexagon/helper.h | 1 + target/hexagon/op_helper.c | 15 +++++++++++++++ target/hexagon/translate.c | 3 +++ 4 files changed, 28 insertions(+)
target/hexagon/gen_tcg.h +9 -0
@@ -497,8 +497,17 @@ do { RsV = RsV; } while (0)#define fGEN_TCG_Y2_dccleana (SHORTCODE ) \ do { RsV = RsV; } while (0) + +#ifdef CONFIG_USER_ONLY +#define fGEN_TCG_Y2_icinva (SHORTCODE ) \ + gen_helper_insn_cache_op(tcg_env, RsV, \ + tcg_constant_tl(insn->slot), \ + tcg_constant_tl(ctx->mem_idx), \ + tcg_constant_tl(ctx->pkt.pc)) +#else #define fGEN_TCG_Y2_icinva (SHORTCODE ) \ do { RsV = RsV; } while (0) +#endif /* * allocframe(#uiV)
target/hexagon/helper.h +1 -0
@@ -115,6 +115,7 @@ DEF_HELPER_FLAGS_4(gvec_uabsdiff_h, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32)#if defined(CONFIG_USER_ONLY ) DEF_HELPER_FLAGS_0(utimer, TCG_CALL_NO_RWG, i64) +DEF_HELPER_5(insn_cache_op, void, env, i32, int, int, i32)#endif #if !defined(CONFIG_USER_ONLY )
target/hexagon/op_helper.c +15 -0
@@ -23,6 +23,9 @@#include "qemu/main-loop.h" #include "cpu.h" #include "exec/helper-proto.h" +#include "exec/mmap-lock.h" +#include "exec/target_page.h" +#include "exec/translation-block.h" #include "fpu/softfloat.h" #include "exec/cpu-interrupt.h" #include "internal.h"
@@ -330,6 +333,18 @@ int32_t HELPER(vacsh_pred)(CPUHexagonState *env,return PeV; } +#ifdef CONFIG_USER_ONLY +void HELPER (insn_cache_op )(CPUHexagonState *env,target_ulong RsV, +int slot,int mmu_idx,target_ulong PC ) +{ +target_ulong start = RsV & ~31 ; + +mmap_lock (); +tb_invalidate_phys_range (env_cpu (env), start, start +31 ); +mmap_unlock (); +} +#endif +int64_t HELPER (cabacdecbin_val )(int64_t RssV,int64_t RttV) {int64_t RddV =0 ;
target/hexagon/translate.c +3 -0
@@ -354,6 +354,9 @@ static bool pkt_ends_tb(Packet *pkt)if (pkt->pkt_has_cof) {return true; } +if (check_for_attrib (pkt,A_ICFLUSHOP )) { +return true; + }#ifndef CONFIG_USER_ONLY /* System mode instructions that end TLB */ if (check_for_opcode(pkt, Y2_swi) ||
Signature · 1 line
2.34.1
[PULL 13/28] tests/functional: Add hexagon semihosting systests
In reply to Brian Cain--- tests/functional/hexagon/meson.build | 5 ++ tests/functional/hexagon/test_systests.py | 94 +++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100755 tests/functional/hexagon/test_systests.py
tests/functional/hexagon/meson.build +5 -0
@@ -2,4 +2,9 @@ tests_hexagon_system_thorough = [ 'arch_tests', + 'systests', ] + +test_hexagon_timeouts = { + 'systests': 180, +}
tests/functional/hexagon/test_systests.py new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.34.1
[PULL 22/28] target/hexagon: implement direct-to-guest interrupt delivery
In reply to Brian CainInterrupts 3 through 5 are routed to guest mode when CCR:GIE and the matching CCR:VV bit are set. Enter through GEVB rather than EVB, record the pre-entry state in GSR and the return address in GELR, and read the vector ID from the l2vic.
--- target/hexagon/reg_fields_def.h.inc | 7 +++ target/hexagon/cpu.c | 5 ++ target/hexagon/hex_interrupts.c | 91 ++++++++++++++++++++++++++--- 3 files changed, 96 insertions(+), 7 deletions(-)
target/hexagon/reg_fields_def.h.inc +7 -0
@@ -136,6 +136,13 @@ DEF_REG_FIELD(CCR_VV1, 29, 1) DEF_REG_FIELD(CCR_VV2, 30, 1) DEF_REG_FIELD(CCR_VV3, 31, 1) +/* GSR fields */ +DEF_REG_FIELD(GSR_CAUSE, 0, 16) +DEF_REG_FIELD(GSR_CFI, 28, 1) +DEF_REG_FIELD(GSR_SS, 29, 1) +DEF_REG_FIELD(GSR_IE, 30, 1) +DEF_REG_FIELD(GSR_UM, 31, 1) + /* ISDB ST fields */ DEF_REG_FIELD(ISDBST_WAITRUN, 24, 8) DEF_REG_FIELD(ISDBST_ONOFF, 16, 8)
target/hexagon/cpu.c +5 -0
@@ -475,6 +475,11 @@ static void hexagon_cpu_realize(DeviceState *dev, Error **errp)error_setg (errp,"hexagon cpu requires 'tlb' link property to be set" ); return; } +if (!HEXAGON_CPU (dev)->l2vic) { +error_setg (errp, +"hexagon cpu requires 'l2vic' link property to be set" ); +return ; + }#endif qemu_init_vcpu (cs);
target/hexagon/hex_interrupts.c +84 -7
124 lines not on this page · raw message
Signature · 1 line
2.34.1
[PULL 28/28] hw/hexagon: decode an empty TLB size field as 4KB
In reply to Brian Cainhex_tlb_pgsize_type() decoded a TLB entry's page size by scanning for the lowest set bit across the entry with ctz.
A guest TLB write with an empty PPD[9:0] size field but any other bit set caused an assertion.
--- hw/hexagon/hexagon_tlb.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
hw/hexagon/hexagon_tlb.c +14 -10
@@ -69,8 +69,6 @@ static const char *pgsize_str[NUM_PGSIZE_TYPES] = { "1G", }; -#define INVALID_MASK 0xffffffffLL -static const uint64_t encmask_2_mask[] = {0x0fffLL ,/* 4k, 0000 */ 0x3fffLL ,/* 16k, 0001 */
@@ -82,19 +80,25 @@ static const uint64_t encmask_2_mask[] = {0x3ffffffLL ,/* 64m, 0111 */ 0xfffffffLL ,/* 256m, 1000 */ 0x3fffffffLL, /* 1g, 1001 */ - INVALID_MASK, /* RSVD, 1010 */ }; +/* +* The page size is encoded as the position of the lowest set bit of +* PPD[9:0]. Bits outside that field belong to the cacheability and +* permission fields and must not take part in the decode. An all-zero +* field denotes the smallest page, matching get_pgsize() in the reference +* simulator. +*/ +#define PGSIZE_FIELD_MASK ((1 << NUM_PGSIZE_TYPES) - 1) +static inline tlb_pgsize_t hex_tlb_pgsize_type (uint64_t entry) { -if (entry ==0 ) { -qemu_log_mask (CPU_LOG_MMU ,"%s: Supplied TLB entry was 0!\n" , - __func__); -return 0 ; +uint32_t field =GET_PTE_PPD (entry) &PGSIZE_FIELD_MASK ; + +if (field ==0 ) { +return PGSIZE_4K ; } -tlb_pgsize_t size =ctz64 (entry); -g_assert (size <NUM_PGSIZE_TYPES ); -return size; +return ctz32 (field); }static inline uint64_t hex_tlb_page_size_bytes (uint64_t entry)
Signature · 1 line
2.34.1
[PULL 26/28] tests/tcg/hexagon: add icinva test
In reply to Brian Cain--- tests/tcg/hexagon/icinva.c | 70 +++++++++++++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 2 + 2 files changed, 72 insertions(+) create mode 100644 tests/tcg/hexagon/icinva.c
tests/tcg/hexagon/icinva.c new file +70 -0
@@ -0,0 +1,70 @@ +/* +* Test that icinva ends the current translation block. +* +* icinva only invalidates the emulator's cached translation for a +* code range; it doesn't retroactively fix up code that has already +* been decoded as part of the still-executing translation block. If +* icinva doesn't force a new TB to start right after it, a packet +* patched via a store immediately before icinva (with no +* change-of-flow in between) still runs the stale decode baked into +* the current TB instead of the freshly-patched instruction. +* +* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <stdio.h> +#include <stdint.h> +#include <sys/mman.h> + +int err; + +#include "hex_test.h" + +/* Encoding of "r0 = #99" */ +#define ICINVA_NEW_INSN 0x7800cc60 + +static uint32_t __attribute__((noinline))test_icinva_smc (void ) +{ +uint32_t result; + +/* +* r1 = address of the "patch_slot" packet below (1:) +* Overwrite it with the "r0 = #99" encoding, invalidate the +* icache for that address, then fall straight through into it +* with no intervening jump/call. +*/ + asmvolatile ( +"r1 = ##1f\n" +"r2 = ##%[newinsn]\n" +"memw(r1) = r2\n" +"icinva(r1)\n" +"1:\n" +" r0 = #11\n" +"%[out] = r0\n" + : [out]"=r" (result) + : [newinsn]"i" (ICINVA_NEW_INSN ) + :"r0" ,"r1" ,"r2" ,"memory" + ); + +return result; +} + +int main (void ) +{ +int pagesize =4096 ; +uintptr_t page = (uintptr_t )test_icinva_smc & ~(pagesize -1 ); +uint32_t result; + +if (mprotect ((void *)page,2 * pagesize, +PROT_READ |PROT_WRITE |PROT_EXEC ) !=0 ) { +perror ("mprotect" ); +return 1 ; + } + + result =test_icinva_smc (); +check32 (result,99 ); + +puts (err ?"FAIL" :"PASS" ); +return err; +}
tests/tcg/hexagon/Makefile.target +2 -0
@@ -61,6 +61,7 @@ HEX_TESTS += multiple-writes HEX_TESTS += privileged-insn HEX_TESTS += unaligned_pc HEX_TESTS += unaligned_data +HEX_TESTS += icinva HEX_TESTS += test_abs HEX_TESTS += test_bitcnt
@@ -104,6 +105,7 @@ circ: circ.c hex_test.h dual_stores: dual_stores.c hex_test.h fpstuff: fpstuff.c hex_test.h hex_sigsegv: hex_sigsegv.c hex_test.h +icinva: icinva.c hex_test.h load_align: load_align.c hex_test.h load_unpack: load_unpack.c hex_test.h mem_noshuf_exception: mem_noshuf_exception.c hex_test.h
Signature · 1 line
2.34.1
[PULL 19/28] target/hexagon: gate GPCYCLE guest register reads on SSR:CE
In reply to Brian CainGPCYCLELO/GPCYCLEHI read as zero unless the cycle counter is enabled (SSR:CE), matching the hardware.
--- target/hexagon/cpu.c | 7 +++++-- target/hexagon/op_helper.c | 9 ++------- 2 files changed, 7 insertions(+), 9 deletions(-)
target/hexagon/cpu.c +5 -2
@@ -809,14 +809,17 @@ static void hexagon_cpu_class_init(ObjectClass *c, const void *data)#ifndef CONFIG_USER_ONLY uint32_t hexagon_greg_read (CPUHexagonState *env,uint32_t reg) { +uint32_t ssr = env->t_sreg[HEX_SREG_SSR ]; +int ssr_ce =GET_SSR_FIELD (SSR_CE , ssr); +if (reg <=HEX_GREG_G3 ) {return env->greg[reg]; }switch (reg) {case HEX_GREG_GPCYCLELO : -return hexagon_get_sys_pcycle_count_low (env); +return ssr_ce ?hexagon_get_sys_pcycle_count_low (env) :0 ;case HEX_GREG_GPCYCLEHI : -return hexagon_get_sys_pcycle_count_high (env); +return ssr_ce ?hexagon_get_sys_pcycle_count_high (env) :0 ;default :qemu_log_mask (LOG_UNIMP ,"reading greg %" PRId32" not yet supported.\n" , reg);
target/hexagon/op_helper.c +2 -7
@@ -1912,13 +1912,8 @@ uint64_t HELPER(greg_read_pair)(CPUHexagonState *env, uint32_t reg)return (uint64_t )(env->greg[reg]) | (((uint64_t )(env->greg[reg +1 ])) <<32 ); } -switch (reg) { -case HEX_GREG_GPCYCLELO : -return hexagon_get_sys_pcycle_count (env); -default : -return (uint64_t )hexagon_greg_read (env, reg) | - ((uint64_t )(hexagon_greg_read (env, reg +1 )) <<32 ); - } +return (uint64_t )hexagon_greg_read (env, reg) | + ((uint64_t )(hexagon_greg_read (env, reg +1 )) <<32 ); }/*
Signature · 1 line
2.34.1
[PULL 10/28] target/hexagon: add COREDUMP semihosting operation
In reply to Brian CainFrom: Matheus Tavares Bernardino <[email protected]>
Baremetal Hexagon programs invoke SYS_COREDUMP (0xCD) through semihosting to dump CPU state on a fatal exception. Implement the handler to decode the SSR cause field and print the full register file, matching hexagon-sim behavior.
--- target/hexagon/internal.h | 1 + target/hexagon/cpu.c | 2 +- target/hexagon/hexswi.c | 136 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-)
target/hexagon/internal.h +1 -0
@@ -28,6 +28,7 @@ int hexagon_hvx_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n);void hexagon_debug_vreg (CPUHexagonState *env,int regnum);void hexagon_debug_qreg (CPUHexagonState *env,int regnum);void hexagon_debug (CPUHexagonState *env); +void hexagon_dump (CPUHexagonState *env,FILE *f,int flags);extern const char *const hexagon_regnames[TOTAL_PER_THREAD_REGS ];#ifndef CONFIG_USER_ONLY
target/hexagon/cpu.c +1 -1
@@ -235,7 +235,7 @@ void hexagon_debug_qreg(CPUHexagonState *env, int regnum)print_qreg (stdout, env, regnum, false); } -static void hexagon_dump (CPUHexagonState *env,FILE *f,int flags) +void hexagon_dump (CPUHexagonState *env,FILE *f,int flags) {HexagonCPU *cpu =env_archcpu (env);
target/hexagon/hexswi.c +136 -0
148 lines not on this page · raw message
Signature · 1 line
2.34.1
[PULL 15/28] tests/tcg/hexagon: check priv instructions raise SIGILL
In reply to Brian CainA_PRIV and A_GUEST instructions executed from user mode raise HEX_CAUSE_PRIV_USER_NO_SINSN and HEX_CAUSE_PRIV_USER_NO_GINSN, which aborted qemu-hexagon until the preceding patch.
--- tests/tcg/hexagon/privileged-insn.c | 79 +++++++++++++++++++++++++++++ tests/tcg/hexagon/Makefile.target | 1 + 2 files changed, 80 insertions(+) create mode 100644 tests/tcg/hexagon/privileged-insn.c
tests/tcg/hexagon/privileged-insn.c new file +79 -0
@@ -0,0 +1,79 @@ +/* +* Test that privileged and guest-mode instructions raise SIGILL in user mode. +* +* Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <assert.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static void *resume_pc; + +static void handle_sigill (int sig,siginfo_t *info,void *puc) +{ +ucontext_t *uc = (ucontext_t *)puc; + +if (sig !=SIGILL ) { +_exit (EXIT_FAILURE ); + } + + uc->uc_mcontext.r0 =SIGILL ; + uc->uc_mcontext.pc = (unsigned long )resume_pc; +} + +static int test_priv_insn (void ) +{ +int sig; + + asmvolatile ( +"r0 = #0\n" +"r1 = ##1f\n" +"memw(%[pc]) = r1\n" +"stop(r0)\n" +"1:\n" +"%[sig] = r0\n" + : [sig]"=r" (sig) + : [pc]"r" (&resume_pc) + :"r0" ,"r1" ,"memory" ); + +return sig; +} + +static int test_guest_insn (void ) +{ +int sig; + + asmvolatile ( +"r0 = #0\n" +"r1 = ##1f\n" +"memw(%[pc]) = r1\n" +"r0 = g0\n" +"1:\n" +"%[sig] = r0\n" + : [sig]"=r" (sig) + : [pc]"r" (&resume_pc) + :"r0" ,"r1" ,"memory" ); + +return sig; +} + +int main () +{ +struct sigaction act; + +memset (&act,0 ,sizeof (act)); + act.sa_sigaction = handle_sigill; + act.sa_flags =SA_SIGINFO ; +assert (sigaction (SIGILL , &act,NULL ) ==0 ); + +assert (test_priv_insn () ==SIGILL ); +assert (test_guest_insn () ==SIGILL ); + +puts ("PASS" ); +return EXIT_SUCCESS ; +}
tests/tcg/hexagon/Makefile.target +1 -0
@@ -58,6 +58,7 @@ HEX_TESTS += invalid-slots HEX_TESTS += valid-slots HEX_TESTS += invalid-encoding HEX_TESTS += multiple-writes +HEX_TESTS += privileged-insn HEX_TESTS += unaligned_pc HEX_TESTS += unaligned_data
Signature · 1 line
2.34.1
[PULL 24/28] tests/functional/hexagon: update to v0.2.12, +test_{interrupts, sys_regs}
In reply to Brian Caintest_interrupts also now passes in full following the iassign{r,w} fix, so enable it alongside the other arch tests.
--- tests/functional/hexagon/test_arch_tests.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
tests/functional/hexagon/test_arch_tests.py +10 -2
@@ -21,8 +21,8 @@ class ArchTestsUart(QemuSystemTest): ASSET_TARBALL = Asset( "https://github.com/qualcomm/qemu-hexagon-testing/releases/" - "download/v0.2.5/arch_tests_uart.tar.gz", - "edb4f37b877a3a72a0e10920477458a43b40045d34398fee8cb763fefd342f4f", + "download/v0.2.12/arch_tests_uart.tar.gz", + "871a339bf78cac0ebaf1b2509bfcd5b249ad8190be33e0cf848283b2f6915323", ) def run_uart_test(self, test_name: str,
@@ -58,6 +58,10 @@ def test_int_steering(self) -> None: """ self.run_uart_test("test_int_steering") + def test_interrupts(self) -> None: + """Tests interrupt delivery.""" + self.run_uart_test("test_interrupts") + def test_cache(self) -> None: """Tests cache operations: dckill/ickill, l2kill, dczeroa, dccleaninva, cache disable/enable, barriers, and dcinva/dccleana.
@@ -70,6 +74,10 @@ def test_l2vic(self) -> None: """ self.run_uart_test("test_l2vic") + def test_sys_regs(self) -> None: + """Tests system registers.""" + self.run_uart_test("test_sys_regs") + def test_threads(self) -> None: """Tests hardware thread management: start/stop, MODECTL state, per-thread HTID, shared memory, wait/resume, STID priority, and
Signature · 1 line
2.34.1
Peter Maydell
AppliedQuoted 7 lines · Brian Cain
The following changes since commit
eb710c255642cd7dc947f25c9a06c9fc8191e98d:Merge tag 'firmware-20260901-pull-request' of https://gitlab.com/kraxel/qemu into staging (2026-09-01 10:38:45 +0100)
are available in the Git repository at:
Quoted 9 lines
https://github.com/qualcomm/qemu tags/pull-hex-20260901
for you to fetch changes up to
4a4ba15b2b0de009c662674dd365ff56048e2b22:hw/hexagon: decode an empty TLB size field as 4KB (2026-09-01 18:23:23 -0700)
----------------------------------------------------------------
This PR adds semihosting, and fixes for exceptions, icinva, TLB, htid
Applied, thanks.
Please update the changelog at https://wiki.qemu.org/ChangeLog/11.2
for any user-visible changes.
-- PMM