Skip to contentSkip to conversation

QEMU: necessary simple pre-requisites for VMBus emulation

PATCH In review Denis V. Lunev 3 participants 6 messages Sep 16, 2015

lore
Share & tools
lore
#0 lore raw

cover QEMU: necessary simple pre-requisites for VMBus emulation

Hyper-V reset, vp index, vp runtime support is required to support loading Windows guest driver Winhv.sys. Winhv.sys in guest is required to support Windows VMBus.

These changes are simple and straightforward. Let's them go first.

Paolo, I am sending these patches without taking into account your patchset with header cleanup. Should we do something? I would be also be happy to hear news about remaining patches from Andrey about HyperV panic. Please let us know is something from our side will improve the situation.

CC Paolo Bonzini <[email protected]>
CC Richard Henderson <[email protected]>
CC Eduardo Habkost <[email protected]>
CC "Andreas Färber" <[email protected]>
CC Marcelo Tosatti <[email protected]>

Andrey Smetanin (3):
target-i386/kvm: Hyper-V HV_X64_MSR_RESET support target-i386/kvm: set Hyper-V features cpuid bit
HV_X64_MSR_VP_INDEX_AVAILABLE
target-i386/kvm: Hyper-V HV_X64_MSR_VP_RUNTIME support

 linux-headers/asm-x86/hyperv.h |  6 ++++++
 target-i386/cpu-qom.h          |  3 +++
 target-i386/cpu.c              |  3 +++
 target-i386/cpu.h              |  1 +
 target-i386/kvm.c              | 39 ++++++++++++++++++++++++++++++++++++++-
 target-i386/machine.c          | 20 ++++++++++++++++++++
 6 files changed, 71 insertions(+), 1 deletion(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH 1/3 target-i386/kvm: Hyper-V HV_X64_MSR_RESET support

From: Andrey Smetanin <[email protected]>

HV_X64_MSR_RESET msr is used by Hyper-V based Windows guest to reset guest VM by hypervisor. This msr is stateless so no migration/fetch/update is required.

This code checks cpu option "hv-reset" and support by kernel. If both conditions are met appropriate Hyper-V features cpuid bit is set.

CC Paolo Bonzini <[email protected]>
CC Richard Henderson <[email protected]>
CC Eduardo Habkost <[email protected]>
CC "Andreas Färber" <[email protected]>
CC Marcelo Tosatti <[email protected]>
---
 linux-headers/asm-x86/hyperv.h |  3 +++
 target-i386/cpu-qom.h          |  1 +
 target-i386/cpu.c              |  1 +
 target-i386/kvm.c              | 11 ++++++++++-
 4 files changed, 15 insertions(+), 1 deletion(-)
linux-headers/asm-x86/hyperv.h +3 -0
index 8fba544..ba3a63b 100644
@@ -149,6 +149,9 @@
 /* MSR used to provide vcpu index */
 #define HV_X64_MSR_VP_INDEX			0x40000002
 
+/* MSR used to reset the guest OS. */
+#define HV_X64_MSR_RESET               0x40000003
+
 /* MSR used to read the per-partition time reference counter */
 #define HV_X64_MSR_TIME_REF_COUNT		0x40000020
 
target-i386/cpu-qom.h +1 -0
index 7a4fddd..3789a2f 100644
@@ -89,6 +89,7 @@ typedef struct X86CPU {
     bool hyperv_relaxed_timing;
     int hyperv_spinlock_attempts;
     bool hyperv_time;
+    bool hyperv_reset;
     bool check_cpuid;
     bool enforce_cpuid;
     bool expose_kvm;
target-i386/cpu.c +1 -0
index cfb8aa7..21828ad 100644
@@ -3121,6 +3121,7 @@ static Property x86_cpu_properties[] = {
     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("hv-reset", X86CPU, hyperv_reset, false),
     DEFINE_PROP_BOOL("check", X86CPU, check_cpuid, false),
     DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false),
     DEFINE_PROP_BOOL("kvm", X86CPU, expose_kvm, true),
target-i386/kvm.c +10 -1
index 066d03d..37e82fb 100644
@@ -80,6 +80,7 @@ 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_hv_reset;
 static bool has_msr_mtrr;
 static bool has_msr_xss;
 
@@ -457,7 +458,8 @@ static bool hyperv_enabled(X86CPU *cpu)
     return kvm_check_extension(cs->kvm_state, KVM_CAP_HYPERV) > 0 &&
            (hyperv_hypercall_available(cpu) ||
             cpu->hyperv_time  ||
-            cpu->hyperv_relaxed_timing);
+            cpu->hyperv_relaxed_timing ||
+            cpu->hyperv_reset);
 }
 
 static Error *invtsc_mig_blocker;
@@ -523,6 +525,9 @@ int kvm_arch_init_vcpu(CPUState *cs)
             c->eax |= 0x200;
             has_msr_hv_tsc = true;
         }
+        if (cpu->hyperv_reset && has_msr_hv_reset) {
+            c->eax |= HV_X64_MSR_RESET_AVAILABLE;
+        }
         c = &cpuid_data.entries[cpuid_i++];
         c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
         if (cpu->hyperv_relaxed_timing) {
@@ -843,6 +848,10 @@ static int kvm_get_supported_msrs(KVMState *s)
                     has_msr_xss = true;
                     continue;
                 }
+                if (kvm_msr_list->indices[i] == HV_X64_MSR_RESET) {
+                    has_msr_hv_reset = true;
+                    continue;
+                }
             }
         }
 
Signature · 1 line
2.1.4
#2 lore raw

PATCH 2/3 target-i386/kvm: set Hyper-V features cpuid bit HV_X64_MSR_VP_INDEX_AVAILABLE

In reply to Denis V. Lunev

From: Andrey Smetanin <[email protected]>

Hyper-V features bit HV_X64_MSR_VP_INDEX_AVAILABLE value is based on cpu option "hv-vpindex" and kernel support of HV_X64_MSR_VP_INDEX.

CC Paolo Bonzini <[email protected]>
CC Richard Henderson <[email protected]>
CC Eduardo Habkost <[email protected]>
CC "Andreas Färber" <[email protected]>
CC Marcelo Tosatti <[email protected]>
---
 target-i386/cpu-qom.h |  1 +
 target-i386/cpu.c     |  1 +
 target-i386/kvm.c     | 11 ++++++++++-
 3 files changed, 12 insertions(+), 1 deletion(-)
target-i386/cpu-qom.h +1 -0
index 3789a2f..85b9bd2 100644
@@ -90,6 +90,7 @@ typedef struct X86CPU {
     int hyperv_spinlock_attempts;
     bool hyperv_time;
     bool hyperv_reset;
+    bool hyperv_vpindex;
     bool check_cpuid;
     bool enforce_cpuid;
     bool expose_kvm;
target-i386/cpu.c +1 -0
index 21828ad..86dc8fa 100644
@@ -3122,6 +3122,7 @@ static Property x86_cpu_properties[] = {
     DEFINE_PROP_BOOL("hv-vapic", X86CPU, hyperv_vapic, false),
     DEFINE_PROP_BOOL("hv-time", X86CPU, hyperv_time, false),
     DEFINE_PROP_BOOL("hv-reset", X86CPU, hyperv_reset, false),
+    DEFINE_PROP_BOOL("hv-vpindex", X86CPU, hyperv_vpindex, false),
     DEFINE_PROP_BOOL("check", X86CPU, check_cpuid, false),
     DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false),
     DEFINE_PROP_BOOL("kvm", X86CPU, expose_kvm, true),
target-i386/kvm.c +10 -1
index 37e82fb..5bf2c51 100644
@@ -81,6 +81,7 @@ static bool has_msr_hv_hypercall;
 static bool has_msr_hv_vapic;
 static bool has_msr_hv_tsc;
 static bool has_msr_hv_reset;
+static bool has_msr_hv_vpindex;
 static bool has_msr_mtrr;
 static bool has_msr_xss;
 
@@ -459,7 +460,8 @@ static bool hyperv_enabled(X86CPU *cpu)
            (hyperv_hypercall_available(cpu) ||
             cpu->hyperv_time  ||
             cpu->hyperv_relaxed_timing ||
-            cpu->hyperv_reset);
+            cpu->hyperv_reset ||
+            cpu->hyperv_vpindex);
 }
 
 static Error *invtsc_mig_blocker;
@@ -528,6 +530,9 @@ int kvm_arch_init_vcpu(CPUState *cs)
         if (cpu->hyperv_reset && has_msr_hv_reset) {
             c->eax |= HV_X64_MSR_RESET_AVAILABLE;
         }
+        if (cpu->hyperv_vpindex && has_msr_hv_vpindex) {
+            c->eax |= HV_X64_MSR_VP_INDEX_AVAILABLE;
+        }
         c = &cpuid_data.entries[cpuid_i++];
         c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
         if (cpu->hyperv_relaxed_timing) {
@@ -852,6 +857,10 @@ static int kvm_get_supported_msrs(KVMState *s)
                     has_msr_hv_reset = true;
                     continue;
                 }
+                if (kvm_msr_list->indices[i] == HV_X64_MSR_VP_INDEX) {
+                    has_msr_hv_vpindex = true;
+                    continue;
+                }
             }
         }
 
Signature · 1 line
2.1.4
#3 lore raw

PATCH 3/3 target-i386/kvm: Hyper-V HV_X64_MSR_VP_RUNTIME support

In reply to Denis V. Lunev

From: Andrey Smetanin <[email protected]>

HV_X64_MSR_VP_RUNTIME msr used by guest to get "the time the virtual processor consumes running guest code, and the time the associated logical processor spends running hypervisor code on behalf of that guest."

Calculation of that time is performed by task_cputime_adjusted() for vcpu task by KVM side.

CC Paolo Bonzini <[email protected]>
CC Richard Henderson <[email protected]>
CC Eduardo Habkost <[email protected]>
CC "Andreas Färber" <[email protected]>
CC Marcelo Tosatti <[email protected]>
---
 linux-headers/asm-x86/hyperv.h |  3 +++
 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          | 20 ++++++++++++++++++++
 6 files changed, 46 insertions(+), 1 deletion(-)
linux-headers/asm-x86/hyperv.h +3 -0
index ba3a63b..77ecfb9 100644
@@ -152,6 +152,9 @@
 /* MSR used to reset the guest OS. */
 #define HV_X64_MSR_RESET               0x40000003
 
+/* MSR used to provide vcpu runtime in 100ns units */
+#define HV_X64_MSR_VP_RUNTIME          0x40000010
+
 /* MSR used to read the per-partition time reference counter */
 #define HV_X64_MSR_TIME_REF_COUNT		0x40000020
 
target-i386/cpu-qom.h +1 -0
index 85b9bd2..97068ba 100644
@@ -91,6 +91,7 @@ typedef struct X86CPU {
     bool hyperv_time;
     bool hyperv_reset;
     bool hyperv_vpindex;
+    bool hyperv_runtime;
     bool check_cpuid;
     bool enforce_cpuid;
     bool expose_kvm;
target-i386/cpu.c +1 -0
index 86dc8fa..f9e9b20 100644
@@ -3123,6 +3123,7 @@ static Property x86_cpu_properties[] = {
     DEFINE_PROP_BOOL("hv-time", X86CPU, hyperv_time, false),
     DEFINE_PROP_BOOL("hv-reset", X86CPU, hyperv_reset, false),
     DEFINE_PROP_BOOL("hv-vpindex", X86CPU, hyperv_vpindex, false),
+    DEFINE_PROP_BOOL("hv-runtime", X86CPU, hyperv_runtime, false),
     DEFINE_PROP_BOOL("check", X86CPU, check_cpuid, false),
     DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false),
     DEFINE_PROP_BOOL("kvm", X86CPU, expose_kvm, true),
target-i386/cpu.h +1 -0
index af97772..4feede0 100644
@@ -908,6 +908,7 @@ typedef struct CPUX86State {
     uint64_t msr_hv_guest_os_id;
     uint64_t msr_hv_vapic;
     uint64_t msr_hv_tsc;
+    uint64_t msr_hv_runtime;
 
     /* exception/interrupt handling */
     int error_code;
target-i386/kvm.c +20 -1
index 5bf2c51..9dc6057 100644
@@ -82,6 +82,7 @@ static bool has_msr_hv_vapic;
 static bool has_msr_hv_tsc;
 static bool has_msr_hv_reset;
 static bool has_msr_hv_vpindex;
+static bool has_msr_hv_runtime;
 static bool has_msr_mtrr;
 static bool has_msr_xss;
 
@@ -461,7 +462,8 @@ static bool hyperv_enabled(X86CPU *cpu)
             cpu->hyperv_time  ||
             cpu->hyperv_relaxed_timing ||
             cpu->hyperv_reset ||
-            cpu->hyperv_vpindex);
+            cpu->hyperv_vpindex ||
+            cpu->hyperv_runtime);
 }
 
 static Error *invtsc_mig_blocker;
@@ -533,6 +535,9 @@ int kvm_arch_init_vcpu(CPUState *cs)
         if (cpu->hyperv_vpindex && has_msr_hv_vpindex) {
             c->eax |= HV_X64_MSR_VP_INDEX_AVAILABLE;
         }
+        if (cpu->hyperv_runtime && has_msr_hv_runtime) {
+            c->eax |= HV_X64_MSR_VP_RUNTIME_AVAILABLE;
+        }
         c = &cpuid_data.entries[cpuid_i++];
         c->function = HYPERV_CPUID_ENLIGHTMENT_INFO;
         if (cpu->hyperv_relaxed_timing) {
@@ -861,6 +866,10 @@ static int kvm_get_supported_msrs(KVMState *s)
                     has_msr_hv_vpindex = true;
                     continue;
                 }
+                if (kvm_msr_list->indices[i] == HV_X64_MSR_VP_RUNTIME) {
+                    has_msr_hv_runtime = true;
+                    continue;
+                }
             }
         }
 
@@ -1393,6 +1402,10 @@ static int kvm_put_msrs(X86CPU *cpu, int level)
             kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_REFERENCE_TSC,
                               env->msr_hv_tsc);
         }
+        if (has_msr_hv_runtime) {
+            kvm_msr_entry_set(&msrs[n++], HV_X64_MSR_VP_RUNTIME,
+                              env->msr_hv_runtime);
+        }
         if (has_msr_mtrr) {
             kvm_msr_entry_set(&msrs[n++], MSR_MTRRdefType, env->mtrr_deftype);
             kvm_msr_entry_set(&msrs[n++],
@@ -1748,6 +1761,9 @@ static int kvm_get_msrs(X86CPU *cpu)
     if (has_msr_hv_tsc) {
         msrs[n++].index = HV_X64_MSR_REFERENCE_TSC;
     }
+    if (has_msr_hv_runtime) {
+        msrs[n++].index = HV_X64_MSR_VP_RUNTIME;
+    }
     if (has_msr_mtrr) {
         msrs[n++].index = MSR_MTRRdefType;
         msrs[n++].index = MSR_MTRRfix64K_00000;
@@ -1895,6 +1911,9 @@ static int kvm_get_msrs(X86CPU *cpu)
         case HV_X64_MSR_REFERENCE_TSC:
             env->msr_hv_tsc = msrs[i].data;
             break;
+        case HV_X64_MSR_VP_RUNTIME:
+            env->msr_hv_runtime = msrs[i].data;
+            break;
         case MSR_MTRRdefType:
             env->mtrr_deftype = msrs[i].data;
             break;
target-i386/machine.c +20 -0
index a0df64b..b219052 100644
@@ -661,6 +661,25 @@ static const VMStateDescription vmstate_msr_hyperv_time = {
     }
 };
 
+static bool hyperv_runtime_enable_needed(void *opaque)
+{
+    X86CPU *cpu = opaque;
+    CPUX86State *env = &cpu->env;
+
+    return env->msr_hv_runtime != 0;
+}
+
+static const VMStateDescription vmstate_msr_hyperv_runtime = {
+    .name = "cpu/msr_hyperv_runtime",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = hyperv_runtime_enable_needed,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT64(env.msr_hv_runtime, X86CPU),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 static bool avx512_needed(void *opaque)
 {
     X86CPU *cpu = opaque;
@@ -842,6 +861,7 @@ VMStateDescription vmstate_x86_cpu = {
         &vmstate_msr_hypercall_hypercall,
         &vmstate_msr_hyperv_vapic,
         &vmstate_msr_hyperv_time,
+        &vmstate_msr_hyperv_runtime,
         &vmstate_avx512,
         &vmstate_xss,
         NULL
Signature · 1 line
2.1.4
#4 lore raw
In reply to Denis V. Lunev
Quoted 10 lines · Denis V. Lunev

Hyper-V reset, vp index, vp runtime support is required to support loading Windows guest driver Winhv.sys. Winhv.sys in guest is required to support Windows VMBus.

These changes are simple and straightforward. Let's them go first.

Paolo, I am sending these patches without taking into account your patchset with header cleanup. Should we do something? I would be also be happy to hear news about remaining patches from Andrey about HyperV panic.

They didn't compile on ARM or Win32, hence the header file changes which delayed them a bit. But I still plan to send out the pull request very soon, maybe today (ouch, it's already 12:30).

I wanted to include the record-replay patches, but they are big enough that they probably deserve their own pull request, and my queue already has 23 patches...

Quoted 2 lines

Please let us know is something from our side will improve the situation.

No, it's just my issue.

However, there is something we can improve about the process. QEMU typically updates header files around 4.3-rc5 (by importing the changes destined to 4.4), so we need a place to put this kind of patch that depends on Linux header changes. Therefore:

1) I've made a staging branch uq/hyperv (uq = upstream queue) on
git://git.kernel.org/pub/scm/virt/kvm/qemu-kvm.git - you can base your patches on that branch as soon as I create it.

2) if you have more changes to asm/hyperv.h, bundle them and send them to [email protected]. I'll include them in kvm/queue all at the same time, which will make header file synchronization less of a problem in the future.

Paolo

#5 lore raw
Quoted 33 lines · Paolo Bonzini
Quoted 10 lines · Denis V. Lunev

Hyper-V reset, vp index, vp runtime support is required to support loading Windows guest driver Winhv.sys. Winhv.sys in guest is required to support Windows VMBus.

These changes are simple and straightforward. Let's them go first.

Paolo, I am sending these patches without taking into account your patchset with header cleanup. Should we do something? I would be also be happy to hear news about remaining patches from Andrey about HyperV panic.

They didn't compile on ARM or Win32, hence the header file changes which delayed them a bit. But I still plan to send out the pull request very soon, maybe today (ouch, it's already 12:30).

I wanted to include the record-replay patches, but they are big enough that they probably deserve their own pull request, and my queue already has 23 patches...

Quoted 2 lines

Please let us know is something from our side will improve the situation.

No, it's just my issue.

However, there is something we can improve about the process. QEMU typically updates header files around 4.3-rc5 (by importing the changes destined to 4.4), so we need a place to put this kind of patch that depends on Linux header changes. Therefore:

1) I've made a staging branch uq/hyperv (uq = upstream queue) on
git://git.kernel.org/pub/scm/virt/kvm/qemu-kvm.git - you can base your patches on that branch as soon as I create it.

Quoted 7 lines

2) if you have more changes to asm/hyperv.h, bundle them and send them to [email protected]. I'll include them in kvm/queue all at the same time, which will make header file synchronization less of a problem in the future.

Paolo

that sounds great to me. OK, will do.

Den