Skip to contentSkip to conversation

Hyper-V parameters update

PATCH In review Vadim Rozenfeld 3 participants 11 messages Jan 23, 2014

lore
Share & tools
lore
#0 lore raw

v2 cover Hyper-V parameters update

This series consists of several clean-ups, hyper-v MSRs migration fixes, and adding support for new "hv-time" parameter, which designed for activating hyper-v timers feature.

v2 -> v1
Split the previous series into small pieces.

Vadim Rozenfeld (7):
Don't report "Microsoft" as the vendor ID signature. Don't specify hypervisor system identity
Don't specify implementation limits
Don't report the KVM signature twice, since it's already reported
in KVM_CPUID_SIGNATURE CPUID leaf.
make hyperv hypercall and guest os id MSRs migratable. make hyperv vapic assist page migratable
add support for hyper-v timers

 linux-headers/asm-x86/hyperv.h |  3 ++
 linux-headers/linux/kvm.h      |  1 +
 target-i386/cpu-qom.h          |  1 +
 target-i386/cpu.c              |  1 +
 target-i386/cpu.h              |  4 ++
 target-i386/kvm.c              | 85 +++++++++++++++++++++++++++---------------
 target-i386/machine.c          | 66 ++++++++++++++++++++++++++++++++
 7 files changed, 130 insertions(+), 31 deletions(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v2 1/7 Don't report "Microsoft" as the vendor ID signature.

According to "Requirements for Implementing the Microsoft Hypervisor Interface" (http://msdn.microsoft.com/library/windows/hardware/hh975392)
page 4, conformant hypervisors are not required to report the vendor id signature. Guest use this information for reporting and diagnostic purposes only.

---
 target-i386/kvm.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
target-i386/kvm.c +2 -5
index 7522e98..db1583a 100644
@@ -463,11 +463,8 @@ int kvm_arch_init_vcpu(CPUState *cs)
     /* Paravirtualization CPUIDs */
     c = &cpuid_data.entries[cpuid_i++];
     c->function = KVM_CPUID_SIGNATURE;
-    if (!hyperv_enabled(cpu)) {
-        memcpy(signature, "KVMKVMKVM\0\0\0", 12);
-        c->eax = 0;
-    } else {
-        memcpy(signature, "Microsoft Hv", 12);
+    memcpy(signature, "KVMKVMKVM\0\0\0", 12);
+    if (hyperv_enabled(cpu)) {
         c->eax = HYPERV_CPUID_MIN;
     }
     c->ebx = signature[0];
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v2 2/7 Don't specify hypervisor system identity

In reply to Vadim Rozenfeld

According to "Requirements for Implementing the Microsoft Hypervisor Interface" (http://msdn.microsoft.com/library/windows/hardware/hh975392)
page 5, this leaf is recommended but not required. It may be used for diagnostic and reporting purposes.

---
 target-i386/kvm.c | 5 -----
 1 file changed, 5 deletions(-)
target-i386/kvm.c +0 -5
index db1583a..0aa85a3 100644
@@ -480,11 +480,6 @@ int kvm_arch_init_vcpu(CPUState *cs)
         c->eax = signature[0];
 
         c = &cpuid_data.entries[cpuid_i++];
-        c->function = HYPERV_CPUID_VERSION;
-        c->eax = 0x00001bbc;
-        c->ebx = 0x00060001;
-
-        c = &cpuid_data.entries[cpuid_i++];
         c->function = HYPERV_CPUID_FEATURES;
         if (cpu->hyperv_relaxed_timing) {
             c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
Signature · 1 line
1.8.1.4
#3 lore raw

PATCH v2 3/7 Don't specify implementation limits

In reply to Vadim Rozenfeld

According to "Requirements for Implementing the Microsoft Hypervisor Interface" (http://msdn.microsoft.com/library/windows/hardware/hh975392)
page 7, parameters "The maximum number of virtual processors supported" and "The maximum number of logical processors supported" are not required and may be set to zero

---
 target-i386/kvm.c | 5 -----
 1 file changed, 5 deletions(-)
target-i386/kvm.c +0 -5
index 0aa85a3..8adf27e 100644
@@ -500,11 +500,6 @@ int kvm_arch_init_vcpu(CPUState *cs)
         c->ebx = cpu->hyperv_spinlock_attempts;
 
         c = &cpuid_data.entries[cpuid_i++];
-        c->function = HYPERV_CPUID_IMPLEMENT_LIMITS;
-        c->eax = 0x40;
-        c->ebx = 0x40;
-
-        c = &cpuid_data.entries[cpuid_i++];
         c->function = KVM_CPUID_SIGNATURE_NEXT;
         memcpy(signature, "KVMKVMKVM\0\0\0", 12);
         c->eax = 0;
Signature · 1 line
1.8.1.4
#4 lore raw

PATCH v2 4/7 Don't report the KVM signature twice, since it's already reported in KVM_CPUID_SIGNATURE CPUID leaf.

In reply to Vadim Rozenfeld
---
 target-i386/kvm.c | 8 --------
 1 file changed, 8 deletions(-)
target-i386/kvm.c +0 -8
index 8adf27e..c1bc8c0 100644
@@ -498,14 +498,6 @@ int kvm_arch_init_vcpu(CPUState *cs)
             c->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
         }
         c->ebx = cpu->hyperv_spinlock_attempts;
-
-        c = &cpuid_data.entries[cpuid_i++];
-        c->function = KVM_CPUID_SIGNATURE_NEXT;
-        memcpy(signature, "KVMKVMKVM\0\0\0", 12);
-        c->eax = 0;
-        c->ebx = signature[0];
-        c->ecx = signature[1];
-        c->edx = signature[2];
     }
 
     has_msr_async_pf_en = c->eax & (1 << KVM_FEATURE_ASYNC_PF);
Signature · 1 line
1.8.1.4
#5 lore raw

PATCH v2 5/7 make hyperv hypercall and guest os id MSRs migratable.

In reply to Vadim Rozenfeld
---
 target-i386/cpu.h     |  2 ++
 target-i386/kvm.c     | 25 ++++++++++++++++++++-----
 target-i386/machine.c | 23 +++++++++++++++++++++++
 3 files changed, 45 insertions(+), 5 deletions(-)
target-i386/cpu.h +2 -0
index 1d94a9d..3bfaf87 100644
@@ -846,6 +846,8 @@ typedef struct CPUX86State {
     uint64_t msr_fixed_counters[MAX_FIXED_COUNTERS];
     uint64_t msr_gp_counters[MAX_GP_COUNTERS];
     uint64_t msr_gp_evtsel[MAX_GP_COUNTERS];
+    uint64_t msr_hv_hypercall;
+    uint64_t msr_hv_guest_os_id;
 
     /* exception/interrupt handling */
     int error_code;
target-i386/kvm.c +20 -5
index c1bc8c0..48a747e 100644
@@ -71,6 +71,7 @@ static bool has_msr_pv_eoi_en;
 static bool has_msr_misc_enable;
 static bool has_msr_kvm_steal_time;
 static int lm_capable_kernel;
+static bool has_msr_hv_hypercall;
 
 static bool has_msr_architectural_pmu;
 static uint32_t num_architectural_pmu_counters;
@@ -464,8 +465,10 @@ int kvm_arch_init_vcpu(CPUState *cs)
     c = &cpuid_data.entries[cpuid_i++];
     c->function = KVM_CPUID_SIGNATURE;
     memcpy(signature, "KVMKVMKVM\0\0\0", 12);
-    if (hyperv_enabled(cpu)) {
+    if (hyperv_enabled(cpu) &&
+        kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0) {
         c->eax = HYPERV_CPUID_MIN;
+        has_msr_hv_hypercall = true;
     }
     c->ebx = signature[0];
     c->ecx = signature[1];
@@ -475,7 +478,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     c->function = KVM_CPUID_FEATURES;
     c->eax = env->features[FEAT_KVM];
 
-    if (hyperv_enabled(cpu)) {
+    if (has_msr_hv_hypercall) {
         memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
         c->eax = signature[0];
 
@@ -1176,9 +1179,11 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
             kvm_msr_entry_set(&msrs[n++], MSR_CORE_PERF_GLOBAL_CTRL,
                               env->msr_global_ctrl);
         }
-        if (hyperv_hypercall_available(cpu)) {
-            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_GUEST_OS_ID, 0);
-            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_HYPERCALL, 0);
+        if (has_msr_hv_hypercall) {
+            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_GUEST_OS_ID,
+                              env->msr_hv_guest_os_id);
+            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_HYPERCALL,
+                              env->msr_hv_hypercall);
         }
         if (cpu->hyperv_vapic) {
             kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE, 0);
@@ -1461,6 +1466,10 @@ static int kvm_get_msrs(X86CPU *cpu)
         }
     }
 
+    if (has_msr_hv_hypercall) {
+        msrs[n++].index = HV_X64_MSR_HYPERCALL;
+        msrs[n++].index = HV_X64_MSR_GUEST_OS_ID;
+    }
     msr_data.info.nmsrs = n;
     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
     if (ret < 0) {
@@ -1565,6 +1574,12 @@ static int kvm_get_msrs(X86CPU *cpu)
         case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL0 + MAX_GP_COUNTERS - 1:
             env->msr_gp_evtsel[index - MSR_P6_EVNTSEL0] = msrs[i].data;
             break;
+        case HV_X64_MSR_HYPERCALL:
+            env->msr_hv_hypercall = msrs[i].data;
+            break;
+        case HV_X64_MSR_GUEST_OS_ID:
+            env->msr_hv_guest_os_id = msrs[i].data;
+            break;
         }
     }
 
target-i386/machine.c +23 -0
index e568da2..3809d06 100644
@@ -506,6 +506,26 @@ static const VMStateDescription vmstate_msr_architectural_pmu = {
     }
 };
 
+static bool hyperv_hypercall_enable_needed(void *opaque)
+{
+    X86CPU *cpu = opaque;
+    CPUX86State *env = &cpu->env;
+
+    return env->msr_hv_hypercall != 0 || env->msr_hv_guest_os_id != 0;
+}
+
+static const VMStateDescription vmstate_msr_hypercall_hypercall = {
+    .name = "cpu/msr_hyperv_hypercall",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(env.msr_hv_hypercall, X86CPU),
+        VMSTATE_UINT64(env.msr_hv_guest_os_id, X86CPU),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 const VMStateDescription vmstate_x86_cpu = {
     .name = "cpu",
     .version_id = 12,
@@ -637,6 +657,9 @@ const VMStateDescription vmstate_x86_cpu = {
         }, {
             .vmsd = &vmstate_msr_architectural_pmu,
             .needed = pmu_enable_needed,
+        }, {
+            .vmsd = &vmstate_msr_hypercall_hypercall,
+            .needed = hyperv_hypercall_enable_needed,
         } , {
             /* empty */
         }
Signature · 1 line
1.8.1.4
#6 lore raw

Re: [Qemu-devel] [PATCH v2 5/7] make hyperv hypercall and guest os id MSRs migratable.

Il 23/01/2014 14:40, Vadim Rozenfeld ha scritto:

Review

Quoted 4 lines
(quoted diff) +3 -0
+    if (hyperv_enabled(cpu) &&
+        kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0) {
         c->eax = HYPERV_CPUID_MIN;
+        has_msr_hv_hypercall = true;

The commit message should mention that this is done because the MS docs specify HV_X64_MSR_HYPERCALL as a mandatory interface, thus we cannot separate relaxed timing (say)---even though in principle it doesn't require the MSR.

We probably should split this part of the patch as follows:

target-i386/kvm.c +6 -3
index 0a21c30..19b1c94 100644
@@ -72,6 +72,7 @@ static bool has_msr_misc_enable;
 static bool has_msr_bndcfgs;
 static bool has_msr_kvm_steal_time;
 static int lm_capable_kernel;
+static bool has_msr_hv_hypercall;
 
 static bool has_msr_architectural_pmu;
 static uint32_t num_architectural_pmu_counters;
@@ -437,8 +438,9 @@ static bool hyperv_hypercall_available(X86CPU *cpu)
 
 static bool hyperv_enabled(X86CPU *cpu)
 {
-    return hyperv_hypercall_available(cpu) ||
-           cpu->hyperv_relaxed_timing;
+    return kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0 &&
+           (hyperv_hypercall_available(cpu) ||
+            cpu->hyperv_relaxed_timing);
 }
 
 #define KVM_MAX_CPUID_ENTRIES  100
@@ -470,6 +472,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     } else {
         memcpy(signature, "Microsoft Hv", 12);
         c->eax = HYPERV_CPUID_MIN;
+        has_msr_hv_hypercall = true;
     }
     c->ebx = signature[0];
     c->ecx = signature[1];
@@ -479,7 +482,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     c->function = KVM_CPUID_FEATURES;
     c->eax = env->features[FEAT_KVM];
 
-    if (hyperv_enabled(cpu)) {
+    if (has_msr_hv_hypercall) {
         memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
         c->eax = signature[0];
 

Paolo

#7 lore raw

PATCH v2 6/7 make hyperv vapic assist page migratable

In reply to Vadim Rozenfeld
---
 target-i386/cpu.h     |  1 +
 target-i386/kvm.c     | 16 +++++++++++++---
 target-i386/machine.c | 22 ++++++++++++++++++++++
 3 files changed, 36 insertions(+), 3 deletions(-)
target-i386/cpu.h +1 -0
index 3bfaf87..d75a793 100644
@@ -848,6 +848,7 @@ typedef struct CPUX86State {
     uint64_t msr_gp_evtsel[MAX_GP_COUNTERS];
     uint64_t msr_hv_hypercall;
     uint64_t msr_hv_guest_os_id;
+    uint64_t msr_hv_vapic;
 
     /* exception/interrupt handling */
     int error_code;
target-i386/kvm.c +13 -3
index 48a747e..6563dcb 100644
@@ -72,6 +72,7 @@ static bool has_msr_misc_enable;
 static bool has_msr_kvm_steal_time;
 static int lm_capable_kernel;
 static bool has_msr_hv_hypercall;
+static bool has_msr_hv_vapic;
 
 static bool has_msr_architectural_pmu;
 static uint32_t num_architectural_pmu_counters;
@@ -490,6 +491,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
         if (cpu->hyperv_vapic) {
             c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
             c->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
+            has_msr_hv_vapic = true;
         }
 
         c = &cpuid_data.entries[cpuid_i++];
@@ -497,7 +499,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
         if (cpu->hyperv_relaxed_timing) {
             c->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
         }
-        if (cpu->hyperv_vapic) {
+        if (has_msr_hv_vapic) {
             c->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
         }
         c->ebx = cpu->hyperv_spinlock_attempts;
@@ -1185,8 +1187,9 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
             kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_HYPERCALL,
                               env->msr_hv_hypercall);
         }
-        if (cpu->hyperv_vapic) {
-            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE, 0);
+        if (has_msr_hv_vapic) {
+            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE,
+                              env->msr_hv_vapic);
         }
         if (has_msr_feature_control) {
             kvm_msr_entry_set(&msrs[n++], MSR_IA32_FEATURE_CONTROL,
@@ -1470,6 +1473,10 @@ static int kvm_get_msrs(X86CPU *cpu)
         msrs[n++].index = HV_X64_MSR_HYPERCALL;
         msrs[n++].index = HV_X64_MSR_GUEST_OS_ID;
     }
+    if (has_msr_hv_vapic) {
+        msrs[n++].index = HV_X64_MSR_APIC_ASSIST_PAGE;
+    }
+
     msr_data.info.nmsrs = n;
     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
     if (ret < 0) {
@@ -1580,6 +1587,9 @@ static int kvm_get_msrs(X86CPU *cpu)
         case HV_X64_MSR_GUEST_OS_ID:
             env->msr_hv_guest_os_id = msrs[i].data;
             break;
+        case HV_X64_MSR_APIC_ASSIST_PAGE:
+            env->msr_hv_vapic = msrs[i].data;
+            break;
         }
     }
 
target-i386/machine.c +22 -0
index 3809d06..8fc81bc 100644
@@ -526,6 +526,25 @@ static const VMStateDescription vmstate_msr_hypercall_hypercall = {
     }
 };
 
+static bool hyperv_vapic_enable_needed(void *opaque)
+{
+    X86CPU *cpu = opaque;
+    CPUX86State *env = &cpu->env;
+
+    return env->msr_hv_vapic != 0;
+}
+
+static const VMStateDescription vmstate_msr_hyperv_vapic = {
+    .name = "cpu/msr_hyperv_vapic",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(env.msr_hv_vapic, X86CPU),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 const VMStateDescription vmstate_x86_cpu = {
     .name = "cpu",
     .version_id = 12,
@@ -660,6 +679,9 @@ const VMStateDescription vmstate_x86_cpu = {
         }, {
             .vmsd = &vmstate_msr_hypercall_hypercall,
             .needed = hyperv_hypercall_enable_needed,
+        }, {
+            .vmsd = &vmstate_msr_hyperv_vapic,
+            .needed = hyperv_vapic_enable_needed,
         } , {
             /* empty */
         }
Signature · 1 line
1.8.1.4
#8 lore raw

PATCH v2 7/7 add support for hyper-v timers http://msdn.microsoft.com/en-us/library/windows/hardware/ff541625%28v=vs.85%29.aspx This code is generic for activating reference time counter or virtual reference time stamp counter

In reply to Vadim Rozenfeld
---
 linux-headers/asm-x86/hyperv.h |  3 +++
 linux-headers/linux/kvm.h      |  1 +
 target-i386/cpu-qom.h          |  1 +
 target-i386/cpu.c              |  1 +
 target-i386/cpu.h              |  1 +
 target-i386/kvm.c              | 21 ++++++++++++++++++++-
 target-i386/machine.c          | 21 +++++++++++++++++++++
 7 files changed, 48 insertions(+), 1 deletion(-)
linux-headers/asm-x86/hyperv.h +3 -0
index b8f1c01..3b400ee 100644
@@ -149,6 +149,9 @@
 /* MSR used to read the per-partition time reference counter */
 #define HV_X64_MSR_TIME_REF_COUNT		0x40000020
 
+/* A partition's reference time stamp counter (TSC) page */
+#define HV_X64_MSR_REFERENCE_TSC		0x40000021
+
 /* MSR used to retrieve the TSC frequency */
 #define HV_X64_MSR_TSC_FREQUENCY		0x40000022
 
linux-headers/linux/kvm.h +1 -0
index 5a49671..999fb13 100644
@@ -674,6 +674,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_ARM_EL1_32BIT 93
 #define KVM_CAP_SPAPR_MULTITCE 94
 #define KVM_CAP_EXT_EMUL_CPUID 95
+#define KVM_CAP_HYPERV_TIME 96
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
target-i386/cpu-qom.h +1 -0
index d1751a4..722f11a 100644
@@ -69,6 +69,7 @@ typedef struct X86CPU {
     bool hyperv_vapic;
     bool hyperv_relaxed_timing;
     int hyperv_spinlock_attempts;
+    bool hyperv_time;
     bool check_cpuid;
     bool enforce_cpuid;
 
target-i386/cpu.c +1 -0
index 0eea8c7..ff3290c 100644
@@ -2744,6 +2744,7 @@ static Property x86_cpu_properties[] = {
     { .name  = "hv-spinlocks", .info  = &qdev_prop_spinlocks },
     DEFINE_PROP_BOOL("hv-relaxed", X86CPU, hyperv_relaxed_timing, false),
     DEFINE_PROP_BOOL("hv-vapic", X86CPU, hyperv_vapic, false),
+    DEFINE_PROP_BOOL("hv-time", X86CPU, hyperv_time, false),
     DEFINE_PROP_BOOL("check", X86CPU, check_cpuid, false),
     DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false),
     DEFINE_PROP_END_OF_LIST()
target-i386/cpu.h +1 -0
index d75a793..73174a3 100644
@@ -849,6 +849,7 @@ typedef struct CPUX86State {
     uint64_t msr_hv_hypercall;
     uint64_t msr_hv_guest_os_id;
     uint64_t msr_hv_vapic;
+    uint64_t msr_hv_tsc;
 
     /* exception/interrupt handling */
     int error_code;
target-i386/kvm.c +20 -1
index 6563dcb..135d369 100644
@@ -73,6 +73,7 @@ static bool has_msr_kvm_steal_time;
 static int lm_capable_kernel;
 static bool has_msr_hv_hypercall;
 static bool has_msr_hv_vapic;
+static bool has_msr_hv_tsc;
 
 static bool has_msr_architectural_pmu;
 static uint32_t num_architectural_pmu_counters;
@@ -439,6 +440,7 @@ static bool hyperv_hypercall_available(X86CPU *cpu)
 static bool hyperv_enabled(X86CPU *cpu)
 {
     return hyperv_hypercall_available(cpu) ||
+           cpu->hyperv_time  ||
            cpu->hyperv_relaxed_timing;
 }
 
@@ -493,7 +495,13 @@ int kvm_arch_init_vcpu(CPUState *cs)
             c->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
             has_msr_hv_vapic = true;
         }
-
+        if (cpu->hyperv_time &&
+            kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) > 0) {
+            c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
+            c->eax |= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE;
+            c->eax |= 0x200;
+            has_msr_hv_tsc = true;
+        }
         c = &cpuid_data.entries[cpuid_i++];
         c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
         if (cpu->hyperv_relaxed_timing) {
@@ -1191,6 +1199,11 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
             kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE,
                               env->msr_hv_vapic);
         }
+        if (has_msr_hv_tsc) {
+            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_REFERENCE_TSC,
+                              env->msr_hv_tsc);
+        }
+
         if (has_msr_feature_control) {
             kvm_msr_entry_set(&msrs[n++], MSR_IA32_FEATURE_CONTROL,
                               env->msr_ia32_feature_control);
@@ -1476,6 +1489,9 @@ static int kvm_get_msrs(X86CPU *cpu)
     if (has_msr_hv_vapic) {
         msrs[n++].index = HV_X64_MSR_APIC_ASSIST_PAGE;
     }
+    if (has_msr_hv_tsc) {
+        msrs[n++].index = HV_X64_MSR_REFERENCE_TSC;
+    }
 
     msr_data.info.nmsrs = n;
     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
@@ -1590,6 +1606,9 @@ static int kvm_get_msrs(X86CPU *cpu)
         case HV_X64_MSR_APIC_ASSIST_PAGE:
             env->msr_hv_vapic = msrs[i].data;
             break;
+        case HV_X64_MSR_REFERENCE_TSC:
+            env->msr_hv_tsc = msrs[i].data;
+            break;
         }
     }
 
target-i386/machine.c +21 -0
index 8fc81bc..07cc87b 100644
@@ -545,6 +545,24 @@ static const VMStateDescription vmstate_msr_hyperv_vapic = {
     }
 };
 
+static bool hyperv_time_enable_needed(void *opaque)
+{
+    X86CPU *cpu = opaque;
+    CPUX86State *env = &cpu->env;
+
+    return env->msr_hv_tsc != 0;
+}
+
+static const VMStateDescription vmstate_msr_hyperv_time = {
+    .name = "cpu/msr_hyperv_time",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(env.msr_hv_tsc, X86CPU),
+        VMSTATE_END_OF_LIST()
+    }
+};
 const VMStateDescription vmstate_x86_cpu = {
     .name = "cpu",
     .version_id = 12,
@@ -682,6 +700,9 @@ const VMStateDescription vmstate_x86_cpu = {
         }, {
             .vmsd = &vmstate_msr_hyperv_vapic,
             .needed = hyperv_vapic_enable_needed,
+        }, {
+            .vmsd = &vmstate_msr_hyperv_time,
+            .needed = hyperv_time_enable_needed,
         } , {
             /* empty */
         }
Signature · 1 line
1.8.1.4
Reviewed-by on v2 7/7
#9 lore raw

Re: [Qemu-devel] [PATCH v2 7/7] add support for hyper-v timers http://msdn.microsoft.com/en-us/library/windows/hardware/ff541625%28v=vs.85%29.aspx This code is generic for activating reference time counter or virtual reference time stamp counter

Subject for 7/7 contains commit log, otherwise

Quoted 181 lines · Vadim Rozenfeld
---
 linux-headers/asm-x86/hyperv.h |  3 +++
 linux-headers/linux/kvm.h      |  1 +
 target-i386/cpu-qom.h          |  1 +
 target-i386/cpu.c              |  1 +
 target-i386/cpu.h              |  1 +
 target-i386/kvm.c              | 21 ++++++++++++++++++++-
 target-i386/machine.c          | 21 +++++++++++++++++++++
 7 files changed, 48 insertions(+), 1 deletion(-)
linux-headers/asm-x86/hyperv.h +3 -0
index b8f1c01..3b400ee 100644
@@ -149,6 +149,9 @@
 /* MSR used to read the per-partition time reference counter */
 #define HV_X64_MSR_TIME_REF_COUNT		0x40000020
 
+/* A partition's reference time stamp counter (TSC) page */
+#define HV_X64_MSR_REFERENCE_TSC		0x40000021
+
 /* MSR used to retrieve the TSC frequency */
 #define HV_X64_MSR_TSC_FREQUENCY		0x40000022
 
linux-headers/linux/kvm.h +1 -0
index 5a49671..999fb13 100644
@@ -674,6 +674,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_ARM_EL1_32BIT 93
 #define KVM_CAP_SPAPR_MULTITCE 94
 #define KVM_CAP_EXT_EMUL_CPUID 95
+#define KVM_CAP_HYPERV_TIME 96
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
target-i386/cpu-qom.h +1 -0
index d1751a4..722f11a 100644
@@ -69,6 +69,7 @@ typedef struct X86CPU {
     bool hyperv_vapic;
     bool hyperv_relaxed_timing;
     int hyperv_spinlock_attempts;
+    bool hyperv_time;
     bool check_cpuid;
     bool enforce_cpuid;
 
target-i386/cpu.c +1 -0
index 0eea8c7..ff3290c 100644
@@ -2744,6 +2744,7 @@ static Property x86_cpu_properties[] = {
     { .name  = "hv-spinlocks", .info  = &qdev_prop_spinlocks },
     DEFINE_PROP_BOOL("hv-relaxed", X86CPU, hyperv_relaxed_timing, false),
     DEFINE_PROP_BOOL("hv-vapic", X86CPU, hyperv_vapic, false),
+    DEFINE_PROP_BOOL("hv-time", X86CPU, hyperv_time, false),
     DEFINE_PROP_BOOL("check", X86CPU, check_cpuid, false),
     DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false),
     DEFINE_PROP_END_OF_LIST()
target-i386/cpu.h +1 -0
index d75a793..73174a3 100644
@@ -849,6 +849,7 @@ typedef struct CPUX86State {
     uint64_t msr_hv_hypercall;
     uint64_t msr_hv_guest_os_id;
     uint64_t msr_hv_vapic;
+    uint64_t msr_hv_tsc;
 
     /* exception/interrupt handling */
     int error_code;
target-i386/kvm.c +20 -1
index 6563dcb..135d369 100644
@@ -73,6 +73,7 @@ static bool has_msr_kvm_steal_time;
 static int lm_capable_kernel;
 static bool has_msr_hv_hypercall;
 static bool has_msr_hv_vapic;
+static bool has_msr_hv_tsc;
 
 static bool has_msr_architectural_pmu;
 static uint32_t num_architectural_pmu_counters;
@@ -439,6 +440,7 @@ static bool hyperv_hypercall_available(X86CPU *cpu)
 static bool hyperv_enabled(X86CPU *cpu)
 {
     return hyperv_hypercall_available(cpu) ||
+           cpu->hyperv_time  ||
            cpu->hyperv_relaxed_timing;
 }
 
@@ -493,7 +495,13 @@ int kvm_arch_init_vcpu(CPUState *cs)
             c->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
             has_msr_hv_vapic = true;
         }
-
+        if (cpu->hyperv_time &&
+            kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV_TIME) > 0) {
+            c->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
+            c->eax |= HV_X64_MSR_TIME_REF_COUNT_AVAILABLE;
+            c->eax |= 0x200;
+            has_msr_hv_tsc = true;
+        }
         c = &cpuid_data.entries[cpuid_i++];
         c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
         if (cpu->hyperv_relaxed_timing) {
@@ -1191,6 +1199,11 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
             kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_APIC_ASSIST_PAGE,
                               env->msr_hv_vapic);
         }
+        if (has_msr_hv_tsc) {
+            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_REFERENCE_TSC,
+                              env->msr_hv_tsc);
+        }
+
         if (has_msr_feature_control) {
             kvm_msr_entry_set(&msrs[n++], MSR_IA32_FEATURE_CONTROL,
                               env->msr_ia32_feature_control);
@@ -1476,6 +1489,9 @@ static int kvm_get_msrs(X86CPU *cpu)
     if (has_msr_hv_vapic) {
         msrs[n++].index = HV_X64_MSR_APIC_ASSIST_PAGE;
     }
+    if (has_msr_hv_tsc) {
+        msrs[n++].index = HV_X64_MSR_REFERENCE_TSC;
+    }
 
     msr_data.info.nmsrs = n;
     ret = kvm_vcpu_ioctl(CPU(cpu), KVM_GET_MSRS, &msr_data);
@@ -1590,6 +1606,9 @@ static int kvm_get_msrs(X86CPU *cpu)
         case HV_X64_MSR_APIC_ASSIST_PAGE:
             env->msr_hv_vapic = msrs[i].data;
             break;
+        case HV_X64_MSR_REFERENCE_TSC:
+            env->msr_hv_tsc = msrs[i].data;
+            break;
         }
     }
 
target-i386/machine.c +21 -0
index 8fc81bc..07cc87b 100644
@@ -545,6 +545,24 @@ static const VMStateDescription vmstate_msr_hyperv_vapic = {
     }
 };
 
+static bool hyperv_time_enable_needed(void *opaque)
+{
+    X86CPU *cpu = opaque;
+    CPUX86State *env = &cpu->env;
+
+    return env->msr_hv_tsc != 0;
+}
+
+static const VMStateDescription vmstate_msr_hyperv_time = {
+    .name = "cpu/msr_hyperv_time",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields      = (VMStateField []) {
+        VMSTATE_UINT64(env.msr_hv_tsc, X86CPU),
+        VMSTATE_END_OF_LIST()
+    }
+};
 const VMStateDescription vmstate_x86_cpu = {
     .name = "cpu",
     .version_id = 12,
@@ -682,6 +700,9 @@ const VMStateDescription vmstate_x86_cpu = {
         }, {
             .vmsd = &vmstate_msr_hyperv_vapic,
             .needed = hyperv_vapic_enable_needed,
+        }, {
+            .vmsd = &vmstate_msr_hyperv_time,
+            .needed = hyperv_time_enable_needed,
         } , {
             /* empty */
         }
Signature · 1 line
1.8.1.4
#10 lore raw
In reply to Vadim Rozenfeld

Il 23/01/2014 14:40, Vadim Rozenfeld ha scritto:

Quoted 3 lines

This series consists of several clean-ups, hyper-v MSRs migration fixes, and adding support for new "hv-time" parameter, which designed for activating hyper-v timers feature.

Hi Vadim!

I think patches 1-4 have some problems:

(1) patches 1 and 4: the "KVMKVMKVM" and "Microsoft Hv" signatures are used by Linux to understand the format of the leaves starting at 0x40000001. These are of course different between KVM and Hyper-V.

Microsoft suggests that guests detect Hyper-V by checking if eax=0x31237648 at CPUID[0x40000001]. Linux should be corrected to do this check (and KVM probably should reserve some bit, e.g. bit 16, to avoid that its own feature mask ever is 0x31237648), but anyway we cannot change the vendor signature as that conflicts with the detection scheme for KVM's own leaves.

(2) patches 2 and 3 should not be applied without some command-line option or versioning scheme, because they would cause CPUID to change across migration.

(3) patches 4, in addition, misses the point of
KVM_CPUID_SIGNATURE_NEXT, which is to signal that the KVM_CPUID_FEATURES
leaf is not at 0x40000001 but rather at 0x40000101 (KVM_CPUID_FEATURES +
KVM_CPUID_SIGNATURE_NEXT - KVM_CPUID_SIGNATURE, if you want). This way, Linux can use the KVM features even if Hyper-V enlightenments enabled.

Unfortunately, the logic is broken because KVM_CPUID_FEATURES is not to 0x40000101 if hyperv_enabled(). I include an untested patch to fix this at the end of the message.

Luckily they are unnecessary, I'll review patches 5-7 tomorrow but at a first glance they seem good.

Paolo

target-i386/kvm.c +25 -22
index 7522e98..d5cff89 100644
@@ -454,6 +454,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
     uint32_t unused;
     struct kvm_cpuid_entry2 *c;
     uint32_t signature[3];
+    int kvm_base = KVM_CPUID_SIGNATURE;
     int r;
 
     memset(&cpuid_data, 0, sizeof(cpuid_data));
@@ -461,26 +462,22 @@ int kvm_arch_init_vcpu(CPUState *cs)
     cpuid_i = 0;
 
     /* Paravirtualization CPUIDs */
-    c = &cpuid_data.entries[cpuid_i++];
-    c->function = KVM_CPUID_SIGNATURE;
-    if (!hyperv_enabled(cpu)) {
-        memcpy(signature, "KVMKVMKVM\0\0\0", 12);
-        c->eax = 0;
-    } else {
+    if (hyperv_enabled(cpu)) {
+        c = &cpuid_data.entries[cpuid_i++];
+        c->function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS;
         memcpy(signature, "Microsoft Hv", 12);
         c->eax = HYPERV_CPUID_MIN;
-    }
-    c->ebx = signature[0];
-    c->ecx = signature[1];
-    c->edx = signature[2];
+        c->ebx = signature[0];
+        c->ecx = signature[1];
+        c->edx = signature[2];
 
-    c = &cpuid_data.entries[cpuid_i++];
-    c->function = KVM_CPUID_FEATURES;
-    c->eax = env->features[FEAT_KVM];
-
-    if (hyperv_enabled(cpu)) {
+        c = &cpuid_data.entries[cpuid_i++];
+        c->function = HYPERV_CPUID_INTERFACE;
         memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
         c->eax = signature[0];
+        c->ebx = 0;
+        c->ecx = 0;
+        c->edx = 0;
 
         c = &cpuid_data.entries[cpuid_i++];
         c->function = HYPERV_CPUID_VERSION;
@@ -512,15 +509,21 @@ int kvm_arch_init_vcpu(CPUState *cs)
         c->eax = 0x40;
         c->ebx = 0x40;
 
-        c = &cpuid_data.entries[cpuid_i++];
-        c->function = KVM_CPUID_SIGNATURE_NEXT;
-        memcpy(signature, "KVMKVMKVM\0\0\0", 12);
-        c->eax = 0;
-        c->ebx = signature[0];
-        c->ecx = signature[1];
-        c->edx = signature[2];
+        kvm_base = KVM_CPUID_SIGNATURE_NEXT;
     }
 
+    memcpy(signature, "KVMKVMKVM\0\0\0", 12);
+    c = &cpuid_data.entries[cpuid_i++];
+    c->function = KVM_CPUID_SIGNATURE | kvm_base;
+    c->eax = 0;
+    c->ebx = signature[0];
+    c->ecx = signature[1];
+    c->edx = signature[2];
+
+    c = &cpuid_data.entries[cpuid_i++];
+    c->function = KVM_CPUID_FEATURES | kvm_base;
+    c->eax = env->features[FEAT_KVM];
+
     has_msr_async_pf_en = c->eax & (1 << KVM_FEATURE_ASYNC_PF);
 
     has_msr_pv_eoi_en = c->eax & (1 << KVM_FEATURE_PV_EOI);