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