Re: [PATCH] KVM: nSVM: fix nested tsc scaling when not enabled but MSR_AMD64_TSC_RATIO set

From: Paolo Bonzini
Date: Mon Feb 21 2022 - 08:22:35 EST


On 2/21/22 14:17, Maxim Levitsky wrote:
It's not clear how QEMU ends up writing MSR_AMD64_TSC_RATIO_DEFAULT
rather than 0, but we clearly have a bug in KVM. It should not allow
writing 0 in the first place if tsc-ratio is not available in the VM.

Qemu currently (the code is very new so it can be changed) writes the initial value of
0 to this msr if tsc scaling is disabled in the guest, or MSR_AMD64_TSC_RATIO_DEFAULT
if the tsc scaling is enabled.

It's released in 6.2, though, right?

The guest can change it only when TSC scaling is enabled for it.
If tsc scaling is not enabled, both guest reads or writes of this MSR get #GP.
I only allowed qemu writes of this msr because I thought that qemu might
first set the MSR and then set guest CPUID.

Also, for example the MSR_IA32_XSS uses the same logic in KVM.

As for why qemu sets this msr regardless of guest CPUID bit,
it seemed to be cleaner this way - kvm_put_msrs in qemu seems not to
check guest CPUID but rather only check that KVM supports this msr,
which will be true regardless of guest's CPUID bit.

Yes, I agree it's cleaner in QEMU and consistent with others in KVM. However the value that is written should be the default one (which is usually zero, but not always and not in this case).

Paolo