@samitouri / QOSamiQemu / commits / 3687ff02bd

target/mips: Move count_clock to MIPSCPU struct

The count_clock pointer is not something we can do a shallow copy of, as linux-user cpu_copy() does, and although it is a system-mode piece of state we unconditionally create it, so it is present also in user-mode. There isn't any need to keep this in the env struct rather than the CPU struct, so move it to avoid possible memory leaks or double-usage. This also puts it next to the other Clocks that this CPU has. I haven't seen any sanitizer reports about this field, so this is averting a possible problem rather than correcting an observed one. Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-ID: <20260317175031.3035740-4-peter.maydell@linaro.org> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>

Peter Maydell committed Mar 17, 2026 at 17:50 UTC 3687ff02bd16410d1a3e2b28e55da02cb84838a9
3 files changed +11 -7
target/mips/cpu.c
+2 -2
@@ -449,7 +449,7 @@ static void mips_cp0_period_set(MIPSCPU *cpu)
449
450 clock_set_mul_div(cpu->count_div, env->cpu_model->CCRes, 1);
451 clock_set_source(cpu->count_div, cpu->clock);
452 - clock_set_source(env->count_clock, cpu->count_div);
452 + clock_set_source(cpu->count_clock, cpu->count_div);
453 }
454
455 static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
@@ -520,7 +520,7 @@ static void mips_cpu_initfn(Object *obj)
520
521 cpu->clock = qdev_init_clock_in(DEVICE(obj), "clk-in", NULL, cpu, 0);
522 cpu->count_div = clock_new(OBJECT(obj), "clk-div-count");
523 - env->count_clock = clock_new(OBJECT(obj), "clk-count");
523 + cpu->count_clock = clock_new(OBJECT(obj), "clk-count");
524 env->cpu_model = mcc->cpu_def;
525 }
526
target/mips/cpu.h
+1 -1
@@ -1188,7 +1188,6 @@ typedef struct CPUArchState {
1188
1189 const mips_def_t *cpu_model;
1190 QEMUTimer *timer; /* Internal timer */
1191 - Clock *count_clock; /* CP0_Count clock */
1191 target_ulong exception_base; /* ExceptionBase input to the core */
1192 } CPUMIPSState;
1193
@@ -1206,6 +1205,7 @@ struct ArchCPU {
1205 CPUMIPSState env;
1206
1207 Clock *clock;
1208 + Clock *count_clock; /* CP0_Count clock */
1209 Clock *count_div; /* Divider for CP0_Count clock */
1210
1211 CPUMIPSMVPContext *mvp;
target/mips/system/cp0_timer.c
+8 -4
@@ -29,14 +29,16 @@
29 /* MIPS R4K timer */
30 static uint32_t cpu_mips_get_count_val(CPUMIPSState *env)
31 {
32 + MIPSCPU *cpu = env_archcpu(env);
33 int64_t now_ns;
34 now_ns = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
35 return env->CP0_Count +
35 - (uint32_t)clock_ns_to_ticks(env->count_clock, now_ns);
36 + (uint32_t)clock_ns_to_ticks(cpu->count_clock, now_ns);
37 }
38
39 static void cpu_mips_timer_update(CPUMIPSState *env)
40 {
41 + MIPSCPU *cpu = env_archcpu(env);
42 uint64_t now_ns, next_ns;
43 uint32_t wait;
44
@@ -46,7 +48,7 @@ static void cpu_mips_timer_update(CPUMIPSState *env)
48 if (!wait) {
49 wait = UINT32_MAX;
50 }
49 - next_ns = now_ns + clock_ticks_to_ns(env->count_clock, wait);
51 + next_ns = now_ns + clock_ticks_to_ns(cpu->count_clock, wait);
52 timer_mod(env->timer, next_ns);
53 }
54
@@ -85,11 +87,12 @@ void cpu_mips_store_count(CPUMIPSState *env, uint32_t count)
87 * So env->timer may be NULL, which is also the case with KVM enabled so
88 * treat timer as disabled in that case.
89 */
90 + MIPSCPU *cpu = env_archcpu(env);
91 if (env->CP0_Cause & (1 << CP0Ca_DC) || !env->timer) {
92 env->CP0_Count = count;
93 } else {
94 /* Store new count register */
92 - env->CP0_Count = count - (uint32_t)clock_ns_to_ticks(env->count_clock,
95 + env->CP0_Count = count - (uint32_t)clock_ns_to_ticks(cpu->count_clock,
96 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
97 /* Update timer timer */
98 cpu_mips_timer_update(env);
@@ -116,7 +119,8 @@ void cpu_mips_start_count(CPUMIPSState *env)
119 void cpu_mips_stop_count(CPUMIPSState *env)
120 {
121 /* Store the current value */
119 - env->CP0_Count += (uint32_t)clock_ns_to_ticks(env->count_clock,
122 + MIPSCPU *cpu = env_archcpu(env);
123 + env->CP0_Count += (uint32_t)clock_ns_to_ticks(cpu->count_clock,
124 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
125 }
126