On 1/9/26 19:33, Brian Cain wrote:
In 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 QEMU_CLOCK_VIRTUAL.
Reviewed-by: Pierrick Bouvier <[email protected]>
Signed-off-by: Brian Cain <[email protected]>
---
target/hexagon/helper.h | 4 ++++
target/hexagon/genptr.c | 29 +++++++++++++++++++++++++++++
target/hexagon/op_helper.c | 21 +++++++++++++++++++++
tests/tcg/hexagon/reg_mut.c | 11 ++++++++---
4 files changed, 62 insertions(+), 3 deletions(-)
diff --git a/target/hexagon/op_helper.c b/target/hexagon/op_helper.c
index 2cea1927263..df31ed2488a 100644
--- a/target/hexagon/op_helper.c
+++ b/target/hexagon/op_helper.c
@@ -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,24 @@
#include "hexswi.h"
#endif
+#ifdef CONFIG_USER_ONLY
+/*
+ * User mode has no qtimer device backing TIMERLO/TIMERHI, so derive the
+ * user timer directly from the virtual clock -- the same clock the qtimer
+ * counts -- 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(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
+ HEX_UTIMER_FREQ_HZ, NANOSECONDS_PER_SECOND) &
+ HEX_UTIMER_CNT_MASK;
+}
+#endif
Long term it could be easier to maintain a new user-specific file,
adding it to hexagon_user_ss[] then target_user_arch (less #ifdefs).