KVM: ecessary simple pre-requisites for VMBus emulation
cover KVM: ecessary 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.
Andrey Smetanin (3):
kvm/x86: Hyper-V HV_X64_MSR_RESET msr kvm/x86: Hyper-V HV_X64_MSR_VP_INDEX export for QEMU. kvm/x86: Hyper-V HV_X64_MSR_VP_RUNTIME support
arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/hyperv.h | 6 ++++++ arch/x86/kvm/hyperv.c | 31 +++++++++++++++++++++++++++++-- arch/x86/kvm/x86.c | 9 +++++++++ include/linux/kvm_host.h | 1 + kernel/sched/cputime.c | 2 ++ 6 files changed, 48 insertions(+), 2 deletions(-)
Signature · 1 line
2.1.4
PATCH 1/3 kvm/x86: Hyper-V HV_X64_MSR_RESET msr
From: Andrey Smetanin <[email protected]>
HV_X64_MSR_RESET msr is used by Hyper-V based Windows guest to reset guest VM by hypervisor.
Necessary to support loading of winhv.sys in guest, which in turn is required to support Windows VMBus.
--- arch/x86/include/uapi/asm/hyperv.h | 3 +++ arch/x86/kvm/hyperv.c | 10 ++++++++++ arch/x86/kvm/x86.c | 7 +++++++ include/linux/kvm_host.h | 1 + 4 files changed, 21 insertions(+)
arch/x86/include/uapi/asm/hyperv.h +3 -0
@@ -153,6 +153,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
arch/x86/kvm/hyperv.c +10 -0
@@ -41,6 +41,7 @@ static bool kvm_hv_msr_partition_wide(u32 msr)case HV_X64_MSR_TIME_REF_COUNT :case HV_X64_MSR_CRASH_CTL : case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: +case HV_X64_MSR_RESET : r = true;break ; }
@@ -163,6 +164,12 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data, data);case HV_X64_MSR_CRASH_CTL :return kvm_hv_msr_set_crash_ctl (vcpu, data, host); +case HV_X64_MSR_RESET : +if (data ==1 ) { +vcpu_debug (vcpu,"hyper-v reset requested\n" ); +kvm_make_request (KVM_REQ_HV_RESET , vcpu); + } +break ;default :vcpu_unimpl (vcpu,"Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n" , msr, data);
@@ -241,6 +248,9 @@ static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) pdata);case HV_X64_MSR_CRASH_CTL :return kvm_hv_msr_get_crash_ctl (vcpu, pdata); +case HV_X64_MSR_RESET : + data =0 ; +break ;default :vcpu_unimpl (vcpu,"Hyper-V unhandled rdmsr: 0x%x\n" , msr);return 1 ;
arch/x86/kvm/x86.c +7 -0
@@ -951,6 +951,7 @@ static u32 emulated_msrs[] = {HV_X64_MSR_TIME_REF_COUNT ,HV_X64_MSR_REFERENCE_TSC ,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 ,HV_X64_MSR_APIC_ASSIST_PAGE ,MSR_KVM_ASYNC_PF_EN ,MSR_KVM_STEAL_TIME , MSR_KVM_PV_EOI_EN,
@@ -6268,6 +6269,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) r =0 ; goto out; } +if (kvm_check_request (KVM_REQ_HV_RESET , vcpu)) { + vcpu->run->exit_reason =KVM_EXIT_SYSTEM_EVENT ; + vcpu->run->system_event.type =KVM_SYSTEM_EVENT_RESET ; + r =0 ; + goto out; + } }if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {
include/linux/kvm_host.h +1 -0
@@ -140,6 +140,7 @@ static inline bool is_error_page(struct page *page)#define KVM_REQ_APIC_PAGE_RELOAD 25#define KVM_REQ_SMI 26#define KVM_REQ_HV_CRASH 27 +#define KVM_REQ_HV_RESET 28#define KVM_USERSPACE_IRQ_SOURCE_ID 0#define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1
Signature · 1 line
2.1.4
PATCH 2/3 kvm/x86: Hyper-V HV_X64_MSR_VP_INDEX export for QEMU.
In reply to Denis V. LunevFrom: Andrey Smetanin <[email protected]>
Insert Hyper-V HV_X64_MSR_VP_INDEX into msr's emulated list, so QEMU can set Hyper-V features cpuid HV_X64_MSR_VP_INDEX_AVAILABLE bit correctly. KVM emulation part is in place already.
Necessary to support loading of winhv.sys in guest, which in turn is required to support Windows VMBus.
--- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/kvm/x86.c +1 -0
@@ -952,6 +952,7 @@ static u32 emulated_msrs[] = {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 , +HV_X64_MSR_VP_INDEX ,HV_X64_MSR_APIC_ASSIST_PAGE ,MSR_KVM_ASYNC_PF_EN ,MSR_KVM_STEAL_TIME , MSR_KVM_PV_EOI_EN,
Signature · 1 line
2.1.4
PATCH 3/3 kvm/x86: Hyper-V HV_X64_MSR_VP_RUNTIME support
In reply to Denis V. LunevFrom: 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 this time is performed by task_cputime_adjusted() for vcpu task.
Necessary to support loading of winhv.sys in guest, which in turn is required to support Windows VMBus.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/hyperv.h | 3 +++ arch/x86/kvm/hyperv.c | 21 +++++++++++++++++++-- arch/x86/kvm/x86.c | 1 + kernel/sched/cputime.c | 2 ++ 5 files changed, 26 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -373,6 +373,7 @@ struct kvm_mtrr {/* Hyper-V per vcpu emulation context */ struct kvm_vcpu_hv {u64 hv_vapic; +s64 runtime_offset; };struct kvm_vcpu_arch {
arch/x86/include/uapi/asm/hyperv.h +3 -0
@@ -156,6 +156,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
arch/x86/kvm/hyperv.c +19 -2
@@ -178,7 +178,16 @@ static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,return 0 ; } -static int kvm_hv_set_msr (struct kvm_vcpu *vcpu,u32 msr,u64 data) +/* Calculate cpu time spent by current task in 100ns units */ +static u64 current_task_runtime_100ns (void ) +{ +cputime_t utime, stime; + +task_cputime_adjusted (current, &utime, &stime); +return div_u64 (cputime_to_nsecs (utime + stime),100 ); +} + +static int kvm_hv_set_msr (struct kvm_vcpu *vcpu,u32 msr,u64 data,bool host) {struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
@@ -212,6 +221,11 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data)return kvm_hv_vapic_msr_write (vcpu,APIC_ICR , data);case HV_X64_MSR_TPR :return kvm_hv_vapic_msr_write (vcpu,APIC_TASKPRI , data); +case HV_X64_MSR_VP_RUNTIME : +if (!host) +return 1 ; + hv->runtime_offset = data -current_task_runtime_100ns (); +break ;default :vcpu_unimpl (vcpu,"Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n" , msr, data);
@@ -287,6 +301,9 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)case HV_X64_MSR_APIC_ASSIST_PAGE : data = hv->hv_vapic;break ; +case HV_X64_MSR_VP_RUNTIME : + data =current_task_runtime_100ns () + hv->runtime_offset; +break ;default :vcpu_unimpl (vcpu,"Hyper-V unhandled rdmsr: 0x%x\n" , msr);return 1 ;
@@ -305,7 +322,7 @@ int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)mutex_unlock (&vcpu->kvm->lock);return r; }else -return kvm_hv_set_msr (vcpu, msr, data); +return kvm_hv_set_msr (vcpu, msr, data, host); }int kvm_hv_get_msr_common (struct kvm_vcpu *vcpu,u32 msr,u64 *pdata)
arch/x86/kvm/x86.c +1 -0
@@ -953,6 +953,7 @@ static u32 emulated_msrs[] = {HV_X64_MSR_CRASH_P3 ,HV_X64_MSR_CRASH_P4 ,HV_X64_MSR_CRASH_CTL ,HV_X64_MSR_RESET ,HV_X64_MSR_VP_INDEX , +HV_X64_MSR_VP_RUNTIME ,HV_X64_MSR_APIC_ASSIST_PAGE ,MSR_KVM_ASYNC_PF_EN ,MSR_KVM_STEAL_TIME , MSR_KVM_PV_EOI_EN,
kernel/sched/cputime.c +2 -0
@@ -444,6 +444,7 @@ void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st) *ut = p->utime; *st = p->stime; } +EXPORT_SYMBOL_GPL (task_cputime_adjusted);void thread_group_cputime_adjusted (struct task_struct *p,cputime_t *ut,cputime_t *st) {
@@ -652,6 +653,7 @@ void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)task_cputime (p, &cputime.utime, &cputime.stime);cputime_adjust (&cputime, &p->prev_cputime, ut, st); } +EXPORT_SYMBOL_GPL (task_cputime_adjusted);void thread_group_cputime_adjusted (struct task_struct *p,cputime_t *ut,cputime_t *st) {
Signature · 1 line
2.1.4
Paolo Bonzini
maintainerQuoted 17 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.
Signed-off-by Andrey Smetanin <[email protected]>Reviewed-by Roman Kagan <[email protected]>Signed-off-by Denis V. Lunev <[email protected]>CC Paolo Bonzini <[email protected]>CC Gleb Natapov <[email protected]>Andrey Smetanin (3):
kvm/x86: Hyper-VHV_X64_MSR_RESETmsr kvm/x86: Hyper-VHV_X64_MSR_VP_INDEXexport for QEMU. kvm/x86: Hyper-VHV_X64_MSR_VP_RUNTIMEsupport
Quoted 8 lines
arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/hyperv.h | 6 ++++++ arch/x86/kvm/hyperv.c | 31 +++++++++++++++++++++++++++++-- arch/x86/kvm/x86.c | 9 +++++++++ include/linux/kvm_host.h | 1 + kernel/sched/cputime.c | 2 ++ 6 files changed, 48 insertions(+), 2 deletions(-)
Thanks, applying this to kvm/queue.
I had to change the request number to 29 in the first patch.
Paolo