@samitouri / QOSamiQemu / commits / 418376351d

target/hexagon: Add implementation of cycle counters

Add cycle counting infrastructure for system emulation: - PCYCLE_ENABLED TB flag to gate cycle counting - gen_pcycle_counters() to emit cycle count increments - Real implementations replacing pcycle stubs in cpu_helper.c - hex_cycle_count TCG global for t_cycle_count - pcycle_enabled context field in DisasContext All pcycle code is guarded by #ifndef CONFIG_USER_ONLY. Reviewed-by: Taylor Simpson <ltaylorsimpson@gmail.com> Signed-off-by: Brian Cain <brian.cain@oss.qualcomm.com>

Brian Cain committed Jun 22, 2026 at 15:28 UTC 418376351d05dfb8abf4074ad10e26dd2a2e035f
5 files changed +45 -3
target/hexagon/cpu.c
+4
@@ -268,6 +268,10 @@ static TCGTBCPUState hexagon_get_tb_cpu_state(CPUState *cs)
268 hexagon_raise_exception_err(env, HEX_CAUSE_PC_NOT_ALIGNED, 0);
269 }
270
271 +#ifndef CONFIG_USER_ONLY
272 + hex_flags = FIELD_DP32(hex_flags, TB_FLAGS, PCYCLE_ENABLED, 1);
273 +#endif
274 +
275 return (TCGTBCPUState){ .pc = pc, .flags = hex_flags };
276 }
277
target/hexagon/cpu.h
+1
@@ -164,6 +164,7 @@ struct ArchCPU {
164 #include "cpu_bits.h"
165
166 FIELD(TB_FLAGS, IS_TIGHT_LOOP, 0, 1)
167 +FIELD(TB_FLAGS, PCYCLE_ENABLED, 4, 1)
168
169 G_NORETURN void hexagon_raise_exception_err(CPUHexagonState *env,
170 uint32_t exception,
target/hexagon/cpu_helper.c
+11 -3
@@ -33,17 +33,25 @@ uint32_t hexagon_get_pmu_counter(CPUHexagonState *cur_env, int index)
33
34 uint64_t hexagon_get_sys_pcycle_count(CPUHexagonState *env)
35 {
36 - g_assert_not_reached();
36 + uint64_t total = 0;
37 + CPUState *cs;
38 +
39 + g_assert(bql_locked());
40 + CPU_FOREACH(cs) {
41 + CPUHexagonState *thread_env = cpu_env(cs);
42 + total += thread_env->t_cycle_count;
43 + }
44 + return total;
45 }
46
47 uint32_t hexagon_get_sys_pcycle_count_high(CPUHexagonState *env)
48 {
41 - g_assert_not_reached();
49 + return (uint32_t)(hexagon_get_sys_pcycle_count(env) >> 32);
50 }
51
52 uint32_t hexagon_get_sys_pcycle_count_low(CPUHexagonState *env)
53 {
46 - g_assert_not_reached();
54 + return (uint32_t)(hexagon_get_sys_pcycle_count(env));
55 }
56
57 void hexagon_set_sys_pcycle_count_high(CPUHexagonState *env, uint32_t val)
target/hexagon/translate.c
+26
@@ -61,6 +61,9 @@ TCGv_i64 hex_store_val64[STORES_MAX];
61 TCGv hex_llsc_addr;
62 TCGv hex_llsc_val;
63 TCGv_i64 hex_llsc_val_i64;
64 +#ifndef CONFIG_USER_ONLY
65 +TCGv_i64 hex_cycle_count;
66 +#endif
67 TCGv hex_vstore_addr[VSTORES_MAX];
68 TCGv hex_vstore_size[VSTORES_MAX];
69 TCGv hex_vstore_pending[VSTORES_MAX];
@@ -128,6 +131,15 @@ static void gen_exception_raw(int excp)
131 gen_helper_raise_exception(tcg_env, tcg_constant_i32(excp));
132 }
133
134 +#ifndef CONFIG_USER_ONLY
135 +static void gen_pcycle_counters(DisasContext *ctx)
136 +{
137 + if (ctx->pcycle_enabled) {
138 + tcg_gen_addi_i64(hex_cycle_count, hex_cycle_count, ctx->num_cycles);
139 + }
140 +}
141 +#endif
142 +
143 static void gen_exec_counters(DisasContext *ctx)
144 {
145 tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_PKT_CNT],
@@ -136,6 +148,9 @@ static void gen_exec_counters(DisasContext *ctx)
148 hex_gpr[HEX_REG_QEMU_INSN_CNT], ctx->num_insns);
149 tcg_gen_addi_tl(hex_gpr[HEX_REG_QEMU_HVX_CNT],
150 hex_gpr[HEX_REG_QEMU_HVX_CNT], ctx->num_hvx_insns);
151 +#ifndef CONFIG_USER_ONLY
152 + gen_pcycle_counters(ctx);
153 +#endif
154 }
155
156 static bool use_goto_tb(DisasContext *ctx, target_ulong dest)
@@ -810,6 +825,8 @@ static void gen_commit_hvx(DisasContext *ctx)
825 }
826 }
827
828 +#define PCYCLES_PER_PACKET 1
829 +
830 static void update_exec_counters(DisasContext *ctx)
831 {
832 int num_real_insns = 0;
@@ -829,6 +846,7 @@ static void update_exec_counters(DisasContext *ctx)
846 ctx->num_packets++;
847 ctx->num_insns += num_real_insns;
848 ctx->num_hvx_insns += num_hvx_insns;
849 + ctx->num_cycles += PCYCLES_PER_PACKET;
850 }
851
852 static void gen_commit_packet(DisasContext *ctx)
@@ -978,6 +996,10 @@ static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
996 ctx->is_tight_loop = FIELD_EX32(hex_flags, TB_FLAGS, IS_TIGHT_LOOP);
997 ctx->short_circuit = hex_cpu->short_circuit;
998 ctx->hex_def = HEXAGON_CPU_GET_CLASS(hex_cpu)->hex_def;
999 +#ifndef CONFIG_USER_ONLY
1000 + ctx->num_cycles = 0;
1001 + ctx->pcycle_enabled = FIELD_EX32(hex_flags, TB_FLAGS, PCYCLE_ENABLED);
1002 +#endif
1003 }
1004
1005 static void hexagon_tr_tb_start(DisasContextBase *db, CPUState *cpu)
@@ -1121,6 +1143,10 @@ void hexagon_translate_init(void)
1143 offsetof(CPUHexagonState, llsc_val), "llsc_val");
1144 hex_llsc_val_i64 = tcg_global_mem_new_i64(tcg_env,
1145 offsetof(CPUHexagonState, llsc_val_i64), "llsc_val_i64");
1146 +#ifndef CONFIG_USER_ONLY
1147 + hex_cycle_count = tcg_global_mem_new_i64(tcg_env,
1148 + offsetof(CPUHexagonState, t_cycle_count), "t_cycle_count");
1149 +#endif
1150 for (i = 0; i < STORES_MAX; i++) {
1151 snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i);
1152 hex_store_addr[i] = tcg_global_mem_new(tcg_env,
target/hexagon/translate.h
+3
@@ -85,6 +85,9 @@ typedef struct DisasContext {
85 TCGv new_pred_value[NUM_PREGS];
86 TCGv branch_taken;
87 TCGv dczero_addr;
88 + bool pcycle_enabled;
89 + bool pkt_ends_tb;
90 + uint32_t num_cycles;
91 } DisasContext;
92
93 bool is_gather_store_insn(DisasContext *ctx);