0727d3
From 416de21d11540a927cceb533bf54ce28ffa15ad6 Mon Sep 17 00:00:00 2001
0727d3
From: Paolo Bonzini <pbonzini@redhat.com>
0727d3
Date: Thu, 24 Mar 2022 09:21:41 +0100
0727d3
Subject: [PATCH 2/3] target/i386: properly reset TSC on reset
0727d3
0727d3
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
0727d3
RH-MergeRequest: 172: target/i386: properly reset TSC on reset
0727d3
RH-Commit: [1/1] 7008bc5d02ad0a2d8b78259459d22d8f0986c989
0727d3
RH-Bugzilla: 2070417
0727d3
RH-Acked-by: Marcelo Tosatti <None>
0727d3
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
0727d3
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
0727d3
0727d3
Some versions of Windows hang on reboot if their TSC value is greater
0727d3
than 2^54.  The calibration of the Hyper-V reference time overflows
0727d3
and fails; as a result the processors' clock sources are out of sync.
0727d3
0727d3
The issue is that the TSC _should_ be reset to 0 on CPU reset and
0727d3
QEMU tries to do that.  However, KVM special cases writing 0 to the
0727d3
TSC and thinks that QEMU is trying to hot-plug a CPU, which is
0727d3
correct the first time through but not later.  Thwart this valiant
0727d3
effort and reset the TSC to 1 instead, but only if the CPU has been
0727d3
run once.
0727d3
0727d3
For this to work, env->tsc has to be moved to the part of CPUArchState
0727d3
that is not zeroed at the beginning of x86_cpu_reset.
0727d3
0727d3
Reported-by: Vadim Rozenfeld <vrozenfe@redhat.com>
0727d3
Supersedes: <20220324082346.72180-1-pbonzini@redhat.com>
0727d3
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
0727d3
(cherry picked from commit 5286c3662294119dc2dd1e9296757337211451f6)
0727d3
---
0727d3
 target/i386/cpu.c | 13 +++++++++++++
0727d3
 target/i386/cpu.h |  2 +-
0727d3
 2 files changed, 14 insertions(+), 1 deletion(-)
0727d3
0727d3
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
0727d3
index 6e25d13339..dd6935b1dd 100644
0727d3
--- a/target/i386/cpu.c
0727d3
+++ b/target/i386/cpu.c
0727d3
@@ -5871,6 +5871,19 @@ static void x86_cpu_reset(DeviceState *dev)
0727d3
     env->xstate_bv = 0;
0727d3
 
0727d3
     env->pat = 0x0007040600070406ULL;
0727d3
+
0727d3
+    if (kvm_enabled()) {
0727d3
+        /*
0727d3
+         * KVM handles TSC = 0 specially and thinks we are hot-plugging
0727d3
+         * a new CPU, use 1 instead to force a reset.
0727d3
+         */
0727d3
+        if (env->tsc != 0) {
0727d3
+            env->tsc = 1;
0727d3
+        }
0727d3
+    } else {
0727d3
+        env->tsc = 0;
0727d3
+    }
0727d3
+
0727d3
     env->msr_ia32_misc_enable = MSR_IA32_MISC_ENABLE_DEFAULT;
0727d3
     if (env->features[FEAT_1_ECX] & CPUID_EXT_MONITOR) {
0727d3
         env->msr_ia32_misc_enable |= MSR_IA32_MISC_ENABLE_MWAIT;
0727d3
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
0727d3
index 04f2b790c9..c6a6c871f1 100644
0727d3
--- a/target/i386/cpu.h
0727d3
+++ b/target/i386/cpu.h
0727d3
@@ -1510,7 +1510,6 @@ typedef struct CPUX86State {
0727d3
     target_ulong kernelgsbase;
0727d3
 #endif
0727d3
 
0727d3
-    uint64_t tsc;
0727d3
     uint64_t tsc_adjust;
0727d3
     uint64_t tsc_deadline;
0727d3
     uint64_t tsc_aux;
0727d3
@@ -1660,6 +1659,7 @@ typedef struct CPUX86State {
0727d3
     int64_t tsc_khz;
0727d3
     int64_t user_tsc_khz; /* for sanity check only */
0727d3
     uint64_t apic_bus_freq;
0727d3
+    uint64_t tsc;
0727d3
 #if defined(CONFIG_KVM) || defined(CONFIG_HVF)
0727d3
     void *xsave_buf;
0727d3
     uint32_t xsave_buf_len;
0727d3
-- 
0727d3
2.35.1
0727d3