Skip to content

Commit

Permalink
target/i386: properly reset TSC on reset
Browse files Browse the repository at this point in the history
Some versions of Windows hang on reboot if their TSC value is greater
than 2^54.  The calibration of the Hyper-V reference time overflows
and fails; as a result the processors' clock sources are out of sync.

The issue is that the TSC _should_ be reset to 0 on CPU reset and
QEMU tries to do that.  However, KVM special cases writing 0 to the
TSC and thinks that QEMU is trying to hot-plug a CPU, which is
correct the first time through but not later.  Thwart this valiant
effort and reset the TSC to 1 instead, but only if the CPU has been
run once.

For this to work, env->tsc has to be moved to the part of CPUArchState
that is not zeroed at the beginning of x86_cpu_reset.

Reported-by: Vadim Rozenfeld <vrozenfe@redhat.com>
Supersedes: <20220324082346.72180-1-pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
bonzini committed Mar 24, 2022
1 parent de65b39 commit 5286c36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions target/i386/cpu.c
Expand Up @@ -5931,6 +5931,19 @@ static void x86_cpu_reset(DeviceState *dev)
env->xstate_bv = 0;

env->pat = 0x0007040600070406ULL;

if (kvm_enabled()) {
/*
* KVM handles TSC = 0 specially and thinks we are hot-plugging
* a new CPU, use 1 instead to force a reset.
*/
if (env->tsc != 0) {
env->tsc = 1;
}
} else {
env->tsc = 0;
}

env->msr_ia32_misc_enable = MSR_IA32_MISC_ENABLE_DEFAULT;
if (env->features[FEAT_1_ECX] & CPUID_EXT_MONITOR) {
env->msr_ia32_misc_enable |= MSR_IA32_MISC_ENABLE_MWAIT;
Expand Down
2 changes: 1 addition & 1 deletion target/i386/cpu.h
Expand Up @@ -1554,7 +1554,6 @@ typedef struct CPUArchState {
target_ulong kernelgsbase;
#endif

uint64_t tsc;
uint64_t tsc_adjust;
uint64_t tsc_deadline;
uint64_t tsc_aux;
Expand Down Expand Up @@ -1708,6 +1707,7 @@ typedef struct CPUArchState {
int64_t tsc_khz;
int64_t user_tsc_khz; /* for sanity check only */
uint64_t apic_bus_freq;
uint64_t tsc;
#if defined(CONFIG_KVM) || defined(CONFIG_HVF)
void *xsave_buf;
uint32_t xsave_buf_len;
Expand Down

0 comments on commit 5286c36

Please sign in to comment.