accel/tcg: Fix icount accounting on CPU reset
cover accel/tcg: Fix icount accounting on CPU reset
When TCG runs with '-icount', resetting a vCPU from inside cpu_exec() can advance the virtual clock by the whole unused part of that vCPU's current instruction budget.
Fix by accounting the instructions executed so far immediately before the common CPU reset path clears the decrementer. Resets outside active cpu_exec() remain unchanged.
Philippe Mathieu-Daudé (2):
accel/tcg: Extract icount_cpu_reset() helper
accel/tcg: Fix icount accounting on CPU reset
include/exec/icount.h | 2 + accel/tcg/icount-common.c | 16 +++++ hw/core/cpu-common.c | 4 +- stubs/icount.c | 3 + tests/qtest/icount-x86-test.c | 119 ++++++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 + 6 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 tests/qtest/icount-x86-test.c
Signature · 1 line
2.55.0
PATCH 1/2 accel/tcg: Extract icount_cpu_reset() helper
Move the icount system-emulation reset code out of cpu_common_reset_hold(). Add stub for user emulation.
--- include/exec/icount.h | 2 ++ accel/tcg/icount-common.c | 6 ++++++ hw/core/cpu-common.c | 4 ++-- stubs/icount.c | 3 +++ 4 files changed, 13 insertions(+), 2 deletions(-)
include/exec/icount.h +2 -0
@@ -43,6 +43,8 @@ extern ICountMode use_icount;*/ void icount_update (CPUState *cpu); +void icount_cpu_reset (CPUState *cpu); +/* get raw icount value */ int64_t icount_get_raw (void );
accel/tcg/icount-common.c +6 -0
@@ -104,6 +104,12 @@ void icount_update(CPUState *cpu) &timers_state.vm_clock_lock); } +void icount_cpu_reset (CPUState *cpu) +{ + cpu->icount_extra =0 ; +qatomic_set (&cpu->neg.icount_decr.u32,0 ); +} +static int64_t icount_get_raw_locked (void ) {CPUState *cpu = current_cpu;
hw/core/cpu-common.c +2 -2
@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include "exec/icount.h" #include "exec/cpu-common.h" #include "qapi/error.h" #include "hw/core/cpu.h"
@@ -110,11 +111,10 @@ static void cpu_common_reset_hold(Object *obj, ResetType type) {CPUState *cpu =CPU (obj); +icount_cpu_reset (cpu); cpu->interrupt_request =0 ; cpu->halted = cpu->start_powered_off; cpu->mem_io_pc =0 ; - cpu->icount_extra =0 ; -qatomic_set (&cpu->neg.icount_decr.u32,0 ); cpu->neg.can_do_io = true; cpu->exception_index =-1 ; cpu->crash_occurred = false;
stubs/icount.c +3 -0
@@ -30,3 +30,6 @@ void icount_notify_exit(void) {abort (); } +void icount_cpu_reset (CPUState *cpu) +{ +}
Signature · 1 line
2.55.0
PATCH 2/2 accel/tcg: Fix icount accounting on CPU reset
In reply to Philippe Mathieu-DaudéA CPU reset from cpu_exec() clears the remaining icount budget, causing it to be counted as executed and advancing virtual time.
x86 INIT exposes this as a large TSC jump during AP startup.
Account executed instructions before reset and discard the unused budget. Add an x86 qtest covering INIT/SIPI through x2APIC.
AI-used-for: qtest reproducer
--- accel/tcg/icount-common.c | 10 +++ tests/qtest/icount-x86-test.c | 119 ++++++++++++++++++++++++++++++++++ tests/qtest/meson.build | 2 + 3 files changed, 131 insertions(+) create mode 100644 tests/qtest/icount-x86-test.c
accel/tcg/icount-common.c +10 -0
@@ -106,6 +106,16 @@ void icount_update(CPUState *cpu)void icount_cpu_reset (CPUState *cpu) { +if (qemu_cpu_is_self (cpu) + &&qatomic_read (&cpu->running) + && cpu->icount_budget) { +/* +* Account the instructions executed so far and discard the +* unused part of the current budget before the counters are cleared. +*/ +icount_update (cpu); + cpu->icount_budget =0 ; + } cpu->icount_extra =0 ;qatomic_set (&cpu->neg.icount_decr.u32,0 ); }
tests/qtest/icount-x86-test.c new file +119 -0
119 lines not on this page · raw message
tests/qtest/meson.build +2 -0
@@ -57,6 +57,8 @@ qtests_filter = \ (get_option('default_devices') and host_os != 'windows' ? ['test-filter-redirector'] : []) qtests_i386 = \ + (config_all_devices.has_key('CONFIG_I440FX') and + config_all_devices.has_key('CONFIG_FDC_ISA') ? ['icount-x86-test'] : []) + \ (slirp.found() ? ['pxe-test'] : []) + \ qtests_filter + \ (config_all_devices.has_key('CONFIG_ACPI_VMGENID') ? ['vmgenid-test'] : []) + \
Signature · 1 line
2.55.0