Skip to contentSkip to conversation

accel/tcg: Fix icount accounting on CPU reset

PATCH New Philippe Mathieu-Daudé 1 participant 3 messages Oct 27 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 7a26b40084db..e97cc4e7a426 100644
@@ -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
index b1b6c005fe71..4c8ceafc112b 100644
@@ -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
index 4f4c87f33a09..a18f05d26bda 100644
@@ -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
index ceb73b4fc2ce..1d28bcf7e2bb 100644
@@ -30,3 +30,6 @@ void icount_notify_exit(void)
 {
     abort();
 }
+void icount_cpu_reset(CPUState *cpu)
+{
+}
Signature · 1 line
2.55.0
#2 lore raw

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

Fixes e4cd96571f00 ("cpus: don't credit executed instructions before they have run")
---
 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
index 4c8ceafc112b..45aadf1416d9 100644
@@ -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
index 46685d6d46fd..d7ae09313e32 100644
@@ -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