Skip to contentSkip to conversation

KVM: hyperv: support HV_X64_MSR_TSC_FREQUENCY and HV_X64_MSR_APIC_FREQUENCY

PATCH Superseded Ladi Prosek 3 participants 6 messages Jul 17, 2017 – Jul 26, 2017

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: hyperv: support HV_X64_MSR_TSC_FREQUENCY and HV_X64_MSR_APIC_FREQUENCY

It has been experimentally confirmed that supporting these two MSRs is one of the necessary conditions for nested Hyper-V to use the TSC page. Modern Windows guests are noticeably slower when they fall back to reading timestamps from the HV_X64_MSR_TIME_REF_COUNT MSR instead of using the TSC page.

The newly supported MSRs are advertised with the AccessFrequencyRegs partition privilege flag and CPUID.40000003H:EDX[8] "Support for determining timer frequencies is available" (both outside of the scope of this KVM patch).

Hypervisor Top Level Functional Specification version 5.0b does not explicitly mark these MSRs as partition wide. This is, however, implied by the way Windows uses them (one read on Hyper-V startup regardless of the number of VCPUs) as well as our implementation.

---
 arch/x86/kvm/hyperv.c | 8 ++++++++
 arch/x86/kvm/lapic.c  | 2 --
 arch/x86/kvm/lapic.h  | 3 +++
 arch/x86/kvm/x86.c    | 1 +
 4 files changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/hyperv.c +8 -0
index ebae57a..c93ed3a 100644
@@ -707,6 +707,8 @@ static bool kvm_hv_msr_partition_wide(u32 msr)
 	case HV_X64_MSR_HYPERCALL:
 	case HV_X64_MSR_REFERENCE_TSC:
 	case HV_X64_MSR_TIME_REF_COUNT:
+	case HV_X64_MSR_TSC_FREQUENCY:
+	case HV_X64_MSR_APIC_FREQUENCY:
 	case HV_X64_MSR_CRASH_CTL:
 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
 	case HV_X64_MSR_RESET:
@@ -1065,6 +1067,12 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 	case HV_X64_MSR_REFERENCE_TSC:
 		data = hv->hv_tsc_page;
 		break;
+	case HV_X64_MSR_TSC_FREQUENCY:
+		data = (u64)tsc_khz * 1000;
+		break;
+	case HV_X64_MSR_APIC_FREQUENCY:
+		data = APIC_BUS_FREQUENCY;
+		break;
 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
 		return kvm_hv_msr_get_crash_data(vcpu,
 						 msr - HV_X64_MSR_CRASH_P0,
arch/x86/kvm/lapic.c +0 -2
index 2819d4c..41110c4 100644
@@ -54,8 +54,6 @@
 #define PRIu64 "u"
 #define PRIo64 "o"
 
-#define APIC_BUS_CYCLE_NS 1
-
 /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
 #define apic_debug(fmt, arg...)
 
arch/x86/kvm/lapic.h +3 -0
index 29caa2c..215721e1 100644
@@ -12,6 +12,9 @@
 #define KVM_APIC_SHORT_MASK	0xc0000
 #define KVM_APIC_DEST_MASK	0x800
 
+#define APIC_BUS_CYCLE_NS       1
+#define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
+
 struct kvm_timer {
 	struct hrtimer timer;
 	s64 period; 				/* unit: ns */
arch/x86/kvm/x86.c +1 -0
index 6c7266f..94a2eb5 100644
@@ -990,6 +990,7 @@ static u32 emulated_msrs[] = {
 	MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
 	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
 	HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
+	HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
 	HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
 	HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
 	HV_X64_MSR_RESET,
Signature · 1 line
2.9.3
#1 lore raw

I missed this patch last week, sorry.

2017-07-17 15:28+0200, Ladi Prosek:

Quoted 10 lines

It has been experimentally confirmed that supporting these two MSRs is one of the necessary conditions for nested Hyper-V to use the TSC page. Modern Windows guests are noticeably slower when they fall back to reading timestamps from the HV_X64_MSR_TIME_REF_COUNT MSR instead of using the TSC page.

The newly supported MSRs are advertised with the AccessFrequencyRegs partition privilege flag and CPUID.40000003H:EDX[8] "Support for determining timer frequencies is available" (both outside of the scope of this KVM patch).

Do Windows ignore the AccessFrequencyMsrs (CPUID.40000003H:EAX[11]) bit?

Quoted 9 lines

Hypervisor Top Level Functional Specification version 5.0b does not explicitly mark these MSRs as partition wide. This is, however, implied by the way Windows uses them (one read on Hyper-V startup regardless of the number of VCPUs) as well as our implementation.

---

arch/x86/kvm/hyperv.c static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) · line 1072

Quoted 6 lines
arch/x86/kvm/hyperv.c +3 -0
@@ -1065,6 +1067,12 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 	case HV_X64_MSR_REFERENCE_TSC:
 		data = hv->hv_tsc_page;
 		break;
+	case HV_X64_MSR_TSC_FREQUENCY:
+		data = (u64)tsc_khz * 1000;
+		break;

This should be based on vcpu->arch.virtual_tsc_khz, because KVM can use guest TSC scaling,

thanks.

Quoted 6 lines
arch/x86/kvm/hyperv.c +3 -0
+	case HV_X64_MSR_APIC_FREQUENCY:
+		data = APIC_BUS_FREQUENCY;
+		break;
 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
 		return kvm_hv_msr_get_crash_data(vcpu,
 						 msr - HV_X64_MSR_CRASH_P0,
maintainer
#4 lore raw
In reply to Ladi Prosek
Quoted 11 lines · Ladi Prosek

It has been experimentally confirmed that supporting these two MSRs is one of the necessary conditions for nested Hyper-V to use the TSC page. Modern Windows guests are noticeably slower when they fall back to reading timestamps from the HV_X64_MSR_TIME_REF_COUNT MSR instead of using the TSC page.

The newly supported MSRs are advertised with the AccessFrequencyRegs partition privilege flag and CPUID.40000003H:EDX[8] "Support for determining timer frequencies is available" (both outside of the scope of this KVM patch).

Quoted 6 lines

Hypervisor Top Level Functional Specification version 5.0b does not explicitly mark these MSRs as partition wide. This is, however, implied by the way Windows uses them (one read on Hyper-V startup regardless of the number of VCPUs) as well as our implementation.

No opposition to adding these registers, but QEMU must only present them if the TSC frequency is stable across migration and known, that is

        /* TSC clock must be stable and known for this feature. */
        && ((env->features[FEAT_8000_0007_EDX] & CPUID_APM_INVTSC)
            || env->user_tsc_khz != 0)
        && env->tsc_khz != 0

(A similar condition is already used for cpu->vmware_cpuid_freq).

Paolo

Quoted 72 lines
---
 arch/x86/kvm/hyperv.c | 8 ++++++++
 arch/x86/kvm/lapic.c  | 2 --
 arch/x86/kvm/lapic.h  | 3 +++
 arch/x86/kvm/x86.c    | 1 +
 4 files changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/hyperv.c +8 -0
index ebae57a..c93ed3a 100644
@@ -707,6 +707,8 @@ static bool kvm_hv_msr_partition_wide(u32 msr)
 	case HV_X64_MSR_HYPERCALL:
 	case HV_X64_MSR_REFERENCE_TSC:
 	case HV_X64_MSR_TIME_REF_COUNT:
+	case HV_X64_MSR_TSC_FREQUENCY:
+	case HV_X64_MSR_APIC_FREQUENCY:
 	case HV_X64_MSR_CRASH_CTL:
 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
 	case HV_X64_MSR_RESET:
@@ -1065,6 +1067,12 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 	case HV_X64_MSR_REFERENCE_TSC:
 		data = hv->hv_tsc_page;
 		break;
+	case HV_X64_MSR_TSC_FREQUENCY:
+		data = (u64)tsc_khz * 1000;
+		break;
+	case HV_X64_MSR_APIC_FREQUENCY:
+		data = APIC_BUS_FREQUENCY;
+		break;
 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
 		return kvm_hv_msr_get_crash_data(vcpu,
 						 msr - HV_X64_MSR_CRASH_P0,
arch/x86/kvm/lapic.c +0 -2
index 2819d4c..41110c4 100644
@@ -54,8 +54,6 @@
 #define PRIu64 "u"
 #define PRIo64 "o"
 
-#define APIC_BUS_CYCLE_NS 1
-
 /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
 #define apic_debug(fmt, arg...)
 
arch/x86/kvm/lapic.h +3 -0
index 29caa2c..215721e1 100644
@@ -12,6 +12,9 @@
 #define KVM_APIC_SHORT_MASK	0xc0000
 #define KVM_APIC_DEST_MASK	0x800
 
+#define APIC_BUS_CYCLE_NS       1
+#define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
+
 struct kvm_timer {
 	struct hrtimer timer;
 	s64 period; 				/* unit: ns */
arch/x86/kvm/x86.c +1 -0
index 6c7266f..94a2eb5 100644
@@ -990,6 +990,7 @@ static u32 emulated_msrs[] = {
 	MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
 	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
 	HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
+	HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
 	HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
 	HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
 	HV_X64_MSR_RESET,
#2 lore raw
In reply to Radim Krčmář
Quoted 9 lines · Radim Krčmář

I missed this patch last week, sorry.

2017-07-17 15:28+0200, Ladi Prosek:

Quoted 6 lines

It has been experimentally confirmed that supporting these two MSRs is one of the necessary conditions for nested Hyper-V to use the TSC page. Modern Windows guests are noticeably slower when they fall back to reading timestamps from the HV_X64_MSR_TIME_REF_COUNT MSR instead of using the TSC page.

Quoted 6 lines
Quoted 4 lines

The newly supported MSRs are advertised with the AccessFrequencyRegs partition privilege flag and CPUID.40000003H:EDX[8] "Support for determining timer frequencies is available" (both outside of the scope of this KVM patch).

Do Windows ignore the AccessFrequencyMsrs (CPUID.40000003H:EAX[11]) bit?

No, Windows won't read the MSRs without CPUID.40000003H:EAX[11]. The other bit (CPUID.40000003H:EDX[8]) appears to be ignored, or at least I couldn't find any difference in behavior.

I wonder why you're asking, i.e. what is it that I'm misunderstanding :)

Quoted 5 lines
Quoted 5 lines

Hypervisor Top Level Functional Specification version 5.0b does not explicitly mark these MSRs as partition wide. This is, however, implied by the way Windows uses them (one read on Hyper-V startup regardless of the number of VCPUs) as well as our implementation.

Quoted 13 lines

arch/x86/kvm/hyperv.c static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) · line 1072

Quoted 10 lines

---

arch/x86/kvm/hyperv.c +3 -0
@@ -1065,6 +1067,12 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
      case HV_X64_MSR_REFERENCE_TSC:
              data = hv->hv_tsc_page;
              break;
+     case HV_X64_MSR_TSC_FREQUENCY:
+             data = (u64)tsc_khz * 1000;
+             break;

This should be based on vcpu->arch.virtual_tsc_khz, because KVM can use guest TSC scaling,

Ah, of course. Thanks!

Quoted 8 lines

thanks.

Quoted 6 lines
arch/x86/kvm/hyperv.c +3 -0
+     case HV_X64_MSR_APIC_FREQUENCY:
+             data = APIC_BUS_FREQUENCY;
+             break;
      case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
              return kvm_hv_msr_get_crash_data(vcpu,
                                               msr - HV_X64_MSR_CRASH_P0,
#5 lore raw
In reply to Paolo Bonzini
Quoted 22 lines · Paolo Bonzini
Quoted 11 lines · Ladi Prosek

It has been experimentally confirmed that supporting these two MSRs is one of the necessary conditions for nested Hyper-V to use the TSC page. Modern Windows guests are noticeably slower when they fall back to reading timestamps from the HV_X64_MSR_TIME_REF_COUNT MSR instead of using the TSC page.

The newly supported MSRs are advertised with the AccessFrequencyRegs partition privilege flag and CPUID.40000003H:EDX[8] "Support for determining timer frequencies is available" (both outside of the scope of this KVM patch).

Quoted 6 lines

Hypervisor Top Level Functional Specification version 5.0b does not explicitly mark these MSRs as partition wide. This is, however, implied by the way Windows uses them (one read on Hyper-V startup regardless of the number of VCPUs) as well as our implementation.

No opposition to adding these registers, but QEMU must only present them if the TSC frequency is stable across migration and known, that is

Quoted 6 lines
        /* TSC clock must be stable and known for this feature. */
        && ((env->features[FEAT_8000_0007_EDX] & CPUID_APM_INVTSC)
            || env->user_tsc_khz != 0)
        && env->tsc_khz != 0

(A similar condition is already used for cpu->vmware_cpuid_freq).

Will do! Hyper-V won't initialize L2's TSC pages without CPUID_APM_INVTSC anyway.

Quoted 75 lines

Paolo

Quoted 72 lines
---
 arch/x86/kvm/hyperv.c | 8 ++++++++
 arch/x86/kvm/lapic.c  | 2 --
 arch/x86/kvm/lapic.h  | 3 +++
 arch/x86/kvm/x86.c    | 1 +
 4 files changed, 12 insertions(+), 2 deletions(-)
arch/x86/kvm/hyperv.c +8 -0
index ebae57a..c93ed3a 100644
@@ -707,6 +707,8 @@ static bool kvm_hv_msr_partition_wide(u32 msr)
      case HV_X64_MSR_HYPERCALL:
      case HV_X64_MSR_REFERENCE_TSC:
      case HV_X64_MSR_TIME_REF_COUNT:
+     case HV_X64_MSR_TSC_FREQUENCY:
+     case HV_X64_MSR_APIC_FREQUENCY:
      case HV_X64_MSR_CRASH_CTL:
      case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
      case HV_X64_MSR_RESET:
@@ -1065,6 +1067,12 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
      case HV_X64_MSR_REFERENCE_TSC:
              data = hv->hv_tsc_page;
              break;
+     case HV_X64_MSR_TSC_FREQUENCY:
+             data = (u64)tsc_khz * 1000;
+             break;
+     case HV_X64_MSR_APIC_FREQUENCY:
+             data = APIC_BUS_FREQUENCY;
+             break;
      case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
              return kvm_hv_msr_get_crash_data(vcpu,
                                               msr - HV_X64_MSR_CRASH_P0,
arch/x86/kvm/lapic.c +0 -2
index 2819d4c..41110c4 100644
@@ -54,8 +54,6 @@
 #define PRIu64 "u"
 #define PRIo64 "o"
 
-#define APIC_BUS_CYCLE_NS 1
-
 /* #define apic_debug(fmt,arg...) printk(KERN_WARNING fmt,##arg) */
 #define apic_debug(fmt, arg...)
 
arch/x86/kvm/lapic.h +3 -0
index 29caa2c..215721e1 100644
@@ -12,6 +12,9 @@
 #define KVM_APIC_SHORT_MASK  0xc0000
 #define KVM_APIC_DEST_MASK   0x800
 
+#define APIC_BUS_CYCLE_NS       1
+#define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
+
 struct kvm_timer {
      struct hrtimer timer;
      s64 period;                             /* unit: ns */
arch/x86/kvm/x86.c +1 -0
index 6c7266f..94a2eb5 100644
@@ -990,6 +990,7 @@ static u32 emulated_msrs[] = {
      MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
      HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
      HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
+     HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
      HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
      HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
      HV_X64_MSR_RESET,
#3 lore raw
In reply to Ladi Prosek

2017-07-26 11:47+0200, Ladi Prosek:

Quoted 7 lines · Radim Krčmář
Quoted 6 lines

2017-07-17 15:28+0200, Ladi Prosek:

Quoted 4 lines

The newly supported MSRs are advertised with the AccessFrequencyRegs partition privilege flag and CPUID.40000003H:EDX[8] "Support for determining timer frequencies is available" (both outside of the scope of this KVM patch).

Quoted 5 lines
Quoted 1 line

Do Windows ignore the AccessFrequencyMsrs (CPUID.40000003H:EAX[11]) bit?

No, Windows won't read the MSRs without CPUID.40000003H:EAX[11]. The other bit (CPUID.40000003H:EDX[8]) appears to be ignored, or at least I couldn't find any difference in behavior.

Looking more into it, we are emulating non-root partitions, where CPUID.40000003H:EDX[8] would probably be clear and then the check Linux has in ms_hyperv_init_platform() could be wrong. I am not sure what FrequencyMsrsAvailable is for, though -- to allow AccessFrequencyMsrs in non-root even if root has AccessFrequencyMsrs disabled?

Quoted 1 line

I wonder why you're asking, i.e. what is it that I'm misunderstanding :)

I was just curious why you didn't mention both in the same way (CamelCase name or CPUID bit) and the best explanation was that you didn't need to care about one, because Windows worked without it. :) I think you got it all right.

Thanks.