Re: [PATCH 1/2] kvm/x86: Hyper-V based guest crash data handling

2015-06-19 Thread Paolo Bonzini


On 19/06/2015 12:28, Andrey Smetanin wrote:
 On Wed, 2015-06-17 at 14:44 +0200, Paolo Bonzini wrote:

 On 11/06/2015 15:18, Denis V. Lunev wrote:
 From: Andrey Smetanin asmeta...@virtuozzo.com

 Windows 2012 guests can notify hypervisor about occurred guest crash
 (Windows bugcheck(BSOD)) by writing specific Hyper-V msrs. This patch does
 handling of this MSR's by KVM and sending notification to user space that
 allows to gather Windows guest crash dump by QEMU/LIBVIRT.

 The idea is to provide functionality equal to pvpanic device without
 QEMU guest agent for Windows.

 The idea is borrowed from Linux HyperV bus driver and validated against
 Windows 2k12.

 Signed-off-by: Andrey Smetanin asmeta...@virtuozzo.com
 Signed-off-by: Denis V. Lunev d...@openvz.org
 CC: Gleb Natapov g...@kernel.org
 CC: Paolo Bonzini pbonz...@redhat.com
 ---
  arch/x86/include/uapi/asm/hyperv.h | 10 +
  arch/x86/kvm/Makefile  |  2 +-
  arch/x86/kvm/mshv.c| 84 
 ++
  arch/x86/kvm/mshv.h| 32 +++

 Please use hyperv.[ch] or hyper-v.[ch] and name the functions kvm_hv_*.
  We can later move more functions from x86.c to the new file, so it's
 better to keep the names consistent.

 Should we prepare a 1st patch in this series where
 we move all hyper-v related code from x86.c into hyperv.c new file with
 copyright extension ?

If you want to do that, I certainly wouldn't complain.

Paolo
--
To unsubscribe from this list: send the line unsubscribe kvm in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH 1/2] kvm/x86: Hyper-V based guest crash data handling

2015-06-19 Thread Andrey Smetanin
On Wed, 2015-06-17 at 14:44 +0200, Paolo Bonzini wrote:
 
 On 11/06/2015 15:18, Denis V. Lunev wrote:
  From: Andrey Smetanin asmeta...@virtuozzo.com
  
  Windows 2012 guests can notify hypervisor about occurred guest crash
  (Windows bugcheck(BSOD)) by writing specific Hyper-V msrs. This patch does
  handling of this MSR's by KVM and sending notification to user space that
  allows to gather Windows guest crash dump by QEMU/LIBVIRT.
  
  The idea is to provide functionality equal to pvpanic device without
  QEMU guest agent for Windows.
  
  The idea is borrowed from Linux HyperV bus driver and validated against
  Windows 2k12.
  
  Signed-off-by: Andrey Smetanin asmeta...@virtuozzo.com
  Signed-off-by: Denis V. Lunev d...@openvz.org
  CC: Gleb Natapov g...@kernel.org
  CC: Paolo Bonzini pbonz...@redhat.com
  ---
   arch/x86/include/uapi/asm/hyperv.h | 10 +
   arch/x86/kvm/Makefile  |  2 +-
   arch/x86/kvm/mshv.c| 84 
  ++
   arch/x86/kvm/mshv.h| 32 +++
 
 Please use hyperv.[ch] or hyper-v.[ch] and name the functions kvm_hv_*.
  We can later move more functions from x86.c to the new file, so it's
 better to keep the names consistent.
 
Should we prepare a 1st patch in this series where
we move all hyper-v related code from x86.c into hyperv.c new file with
copyright extension ?
   arch/x86/kvm/x86.c | 25 
   include/linux/kvm_host.h   | 17 
   include/uapi/linux/kvm.h   | 11 +
   7 files changed, 180 insertions(+), 1 deletion(-)
   create mode 100644 arch/x86/kvm/mshv.c
   create mode 100644 arch/x86/kvm/mshv.h
  
  diff --git a/arch/x86/include/uapi/asm/hyperv.h 
  b/arch/x86/include/uapi/asm/hyperv.h
  index ce6068d..25f3064 100644
  --- a/arch/x86/include/uapi/asm/hyperv.h
  +++ b/arch/x86/include/uapi/asm/hyperv.h
  @@ -199,6 +199,16 @@
   #define HV_X64_MSR_STIMER3_CONFIG  0x40B6
   #define HV_X64_MSR_STIMER3_COUNT   0x40B7
   
  +
  +/* Hypev-V guest crash notification MSR's */
  +#define HV_X64_MSR_CRASH_P00x4100
  +#define HV_X64_MSR_CRASH_P10x4101
  +#define HV_X64_MSR_CRASH_P20x4102
  +#define HV_X64_MSR_CRASH_P30x4103
  +#define HV_X64_MSR_CRASH_P40x4104
  +#define HV_X64_MSR_CRASH_CTL   0x4105
  +#define HV_CRASH_CTL_CRASH_NOTIFY  (1ULL  63)
  +
   #define HV_X64_MSR_HYPERCALL_ENABLE0x0001
   #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT12
   #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK \
  diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
  index 16e8f96..b1ec24d 100644
  --- a/arch/x86/kvm/Makefile
  +++ b/arch/x86/kvm/Makefile
  @@ -12,7 +12,7 @@ kvm-y += $(KVM)/kvm_main.o 
  $(KVM)/coalesced_mmio.o \
   kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
   
   kvm-y  += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
  -  i8254.o ioapic.o irq_comm.o cpuid.o pmu.o
  +  i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mshv.o
   kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT)+= assigned-dev.o iommu.o
   kvm-intel-y+= vmx.o
   kvm-amd-y  += svm.o
  diff --git a/arch/x86/kvm/mshv.c b/arch/x86/kvm/mshv.c
  new file mode 100644
  index 000..ad367c44
  --- /dev/null
  +++ b/arch/x86/kvm/mshv.c
  @@ -0,0 +1,84 @@
  +/*
  + * KVM Microsoft Hyper-V extended paravirtualization
  + *
  + * This work is licensed under the terms of the GNU GPL, version 2.  See
  + * the COPYING file in the top-level directory.
  + *
  + * Copyright (C) 2015 Andrey Smetanin asmeta...@virtuozzo.com
  + *
  + * Authors: Andrey Smetanin asmeta...@virtuozzo.com
  + */
  +
  +#include linux/kvm_host.h
  +#include mshv.h
  +
  +int kvm_mshv_ctx_create(struct kvm *kvm)
  +{
  +   struct kvm_mshv_ctx *ctx;
  +
  +   ctx = kzalloc(sizeof(struct kvm_mshv_ctx), GFP_KERNEL);
  +   if (!ctx)
  +   return -ENOMEM;
  +
  +   ctx-kvm = kvm;
  +   atomic_set(ctx-crash_pending, 0);
  +   kvm-mshv_ctx = ctx;
  +   return 0;
  +}
  +
  +void kvm_mshv_ctx_destroy(struct kvm *kvm)
  +{
  +   kfree(kvm-mshv_ctx);
  +}
  +
  +int kvm_mshv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  +{
  +   struct kvm_mshv_ctx *ctx = kvm_vcpu_get_mshv_ctx(vcpu);
  +
  +   atomic_set(ctx-crash_pending, 1);
  +
  +   /* Response that KVM ready to receive crash data */
  +   *pdata = HV_CRASH_CTL_CRASH_NOTIFY;
  +   return 0;
  +}
  +
  +int kvm_mshv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  +{
  +   struct kvm_mshv_ctx *ctx = kvm_vcpu_get_mshv_ctx(vcpu);
  +
  +   if (atomic_dec_and_test(ctx-crash_pending)) {
  +   pr_debug(vcpu %p 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx,
  +vcpu, ctx-crash_p0, ctx-crash_p1, ctx-crash_p2,

Re: [PATCH 1/2] kvm/x86: Hyper-V based guest crash data handling

2015-06-17 Thread Paolo Bonzini


On 11/06/2015 15:18, Denis V. Lunev wrote:
 From: Andrey Smetanin asmeta...@virtuozzo.com
 
 Windows 2012 guests can notify hypervisor about occurred guest crash
 (Windows bugcheck(BSOD)) by writing specific Hyper-V msrs. This patch does
 handling of this MSR's by KVM and sending notification to user space that
 allows to gather Windows guest crash dump by QEMU/LIBVIRT.
 
 The idea is to provide functionality equal to pvpanic device without
 QEMU guest agent for Windows.
 
 The idea is borrowed from Linux HyperV bus driver and validated against
 Windows 2k12.
 
 Signed-off-by: Andrey Smetanin asmeta...@virtuozzo.com
 Signed-off-by: Denis V. Lunev d...@openvz.org
 CC: Gleb Natapov g...@kernel.org
 CC: Paolo Bonzini pbonz...@redhat.com
 ---
  arch/x86/include/uapi/asm/hyperv.h | 10 +
  arch/x86/kvm/Makefile  |  2 +-
  arch/x86/kvm/mshv.c| 84 
 ++
  arch/x86/kvm/mshv.h| 32 +++

Please use hyperv.[ch] or hyper-v.[ch] and name the functions kvm_hv_*.
 We can later move more functions from x86.c to the new file, so it's
better to keep the names consistent.

  arch/x86/kvm/x86.c | 25 
  include/linux/kvm_host.h   | 17 
  include/uapi/linux/kvm.h   | 11 +
  7 files changed, 180 insertions(+), 1 deletion(-)
  create mode 100644 arch/x86/kvm/mshv.c
  create mode 100644 arch/x86/kvm/mshv.h
 
 diff --git a/arch/x86/include/uapi/asm/hyperv.h 
 b/arch/x86/include/uapi/asm/hyperv.h
 index ce6068d..25f3064 100644
 --- a/arch/x86/include/uapi/asm/hyperv.h
 +++ b/arch/x86/include/uapi/asm/hyperv.h
 @@ -199,6 +199,16 @@
  #define HV_X64_MSR_STIMER3_CONFIG0x40B6
  #define HV_X64_MSR_STIMER3_COUNT 0x40B7
  
 +
 +/* Hypev-V guest crash notification MSR's */
 +#define HV_X64_MSR_CRASH_P0  0x4100
 +#define HV_X64_MSR_CRASH_P1  0x4101
 +#define HV_X64_MSR_CRASH_P2  0x4102
 +#define HV_X64_MSR_CRASH_P3  0x4103
 +#define HV_X64_MSR_CRASH_P4  0x4104
 +#define HV_X64_MSR_CRASH_CTL 0x4105
 +#define HV_CRASH_CTL_CRASH_NOTIFY(1ULL  63)
 +
  #define HV_X64_MSR_HYPERCALL_ENABLE  0x0001
  #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT  12
  #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK   \
 diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
 index 16e8f96..b1ec24d 100644
 --- a/arch/x86/kvm/Makefile
 +++ b/arch/x86/kvm/Makefile
 @@ -12,7 +12,7 @@ kvm-y   += $(KVM)/kvm_main.o 
 $(KVM)/coalesced_mmio.o \
  kvm-$(CONFIG_KVM_ASYNC_PF)   += $(KVM)/async_pf.o
  
  kvm-y+= x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
 -i8254.o ioapic.o irq_comm.o cpuid.o pmu.o
 +i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mshv.o
  kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT)  += assigned-dev.o iommu.o
  kvm-intel-y  += vmx.o
  kvm-amd-y+= svm.o
 diff --git a/arch/x86/kvm/mshv.c b/arch/x86/kvm/mshv.c
 new file mode 100644
 index 000..ad367c44
 --- /dev/null
 +++ b/arch/x86/kvm/mshv.c
 @@ -0,0 +1,84 @@
 +/*
 + * KVM Microsoft Hyper-V extended paravirtualization
 + *
 + * This work is licensed under the terms of the GNU GPL, version 2.  See
 + * the COPYING file in the top-level directory.
 + *
 + * Copyright (C) 2015 Andrey Smetanin asmeta...@virtuozzo.com
 + *
 + * Authors: Andrey Smetanin asmeta...@virtuozzo.com
 + */
 +
 +#include linux/kvm_host.h
 +#include mshv.h
 +
 +int kvm_mshv_ctx_create(struct kvm *kvm)
 +{
 + struct kvm_mshv_ctx *ctx;
 +
 + ctx = kzalloc(sizeof(struct kvm_mshv_ctx), GFP_KERNEL);
 + if (!ctx)
 + return -ENOMEM;
 +
 + ctx-kvm = kvm;
 + atomic_set(ctx-crash_pending, 0);
 + kvm-mshv_ctx = ctx;
 + return 0;
 +}
 +
 +void kvm_mshv_ctx_destroy(struct kvm *kvm)
 +{
 + kfree(kvm-mshv_ctx);
 +}
 +
 +int kvm_mshv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 +{
 + struct kvm_mshv_ctx *ctx = kvm_vcpu_get_mshv_ctx(vcpu);
 +
 + atomic_set(ctx-crash_pending, 1);
 +
 + /* Response that KVM ready to receive crash data */
 + *pdata = HV_CRASH_CTL_CRASH_NOTIFY;
 + return 0;
 +}
 +
 +int kvm_mshv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 +{
 + struct kvm_mshv_ctx *ctx = kvm_vcpu_get_mshv_ctx(vcpu);
 +
 + if (atomic_dec_and_test(ctx-crash_pending)) {
 + pr_debug(vcpu %p 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx,
 +  vcpu, ctx-crash_p0, ctx-crash_p1, ctx-crash_p2,
 +  ctx-crash_p3, ctx-crash_p4);
 +
 + /* Crash data almost gathered so notify user space */

Why almost gathered?

 + kvm_make_request(KVM_REQ_MSHV_CRASH, vcpu);
 + }
 +
 + return 0;
 +}
 +
 +int kvm_mshv_msr_set_crash_data(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 +{
 + 

Re: [PATCH 1/2] kvm/x86: Hyper-V based guest crash data handling

2015-06-15 Thread Andrey Smetanin
Hi Peter,
thank you for comments. See answers below.
On Fri, 2015-06-12 at 16:03 -0700, Peter Hornyack wrote:
 Hi Denis, Andrey, I have a few comments and questions. (re-sending in
 plain-text mode, apologies for sending twice.)
 
 On Thu, Jun 11, 2015 at 6:18 AM, Denis V. Lunev d...@openvz.org wrote:
  From: Andrey Smetanin asmeta...@virtuozzo.com
 
  Windows 2012 guests can notify hypervisor about occurred guest crash
  (Windows bugcheck(BSOD)) by writing specific Hyper-V msrs. This patch does
  handling of this MSR's by KVM and sending notification to user space that
  allows to gather Windows guest crash dump by QEMU/LIBVIRT.
 
  The idea is to provide functionality equal to pvpanic device without
  QEMU guest agent for Windows.
 
  The idea is borrowed from Linux HyperV bus driver and validated against
  Windows 2k12.
 
  Signed-off-by: Andrey Smetanin asmeta...@virtuozzo.com
  Signed-off-by: Denis V. Lunev d...@openvz.org
  CC: Gleb Natapov g...@kernel.org
  CC: Paolo Bonzini pbonz...@redhat.com
  ---
   arch/x86/include/uapi/asm/hyperv.h | 10 +
   arch/x86/kvm/Makefile  |  2 +-
   arch/x86/kvm/mshv.c| 84 
  ++
   arch/x86/kvm/mshv.h| 32 +++
   arch/x86/kvm/x86.c | 25 
   include/linux/kvm_host.h   | 17 
   include/uapi/linux/kvm.h   | 11 +
   7 files changed, 180 insertions(+), 1 deletion(-)
   create mode 100644 arch/x86/kvm/mshv.c
   create mode 100644 arch/x86/kvm/mshv.h
 
  diff --git a/arch/x86/include/uapi/asm/hyperv.h 
  b/arch/x86/include/uapi/asm/hyperv.h
  index ce6068d..25f3064 100644
  --- a/arch/x86/include/uapi/asm/hyp erv.h
  +++ b/arch/x86/include/uapi/asm/hyperv.h
  @@ -199,6 +199,16 @@
   #define HV_X64_MSR_STIMER3_CONFIG  0x40B6
   #define HV_X64_MSR_STIMER3_COUNT   0x40B7
 
  +
  +/* Hypev-V guest crash notification MSR's */
  +#define HV_X64_MSR_CRASH_P00x4100
  +#define HV_X64_MSR_CRASH_P10x4101
  +#define HV_X64_MSR_CRASH_P20x4102
  +#define HV_X64_MSR_CRASH_P30x4103
  +#define HV_X64_MSR_CRASH_P40x4104
  +#define HV_X64_MSR_CRASH_CTL   0x4105
  +#define HV_CRASH_CTL_CRASH_NOTIFY  (1ULL  63)
  +
   #define HV_X64_MSR_HYPERCALL_ENABLE0x0001
   #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT12
   #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK \
  diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
  index 16e8f96..b1ec24d 100644
  --- a/arch/x86/kvm/Makefile
  +++ b/arch/x86/kvm/Makefile
  @@ -12,7 +12,7 @@ kvm-y += $(KVM)/kvm_main.o 
  $(KVM)/coalesced_mmio.o \
   kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
 
   kvm-y  += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
  -  i8254.o ioapic.o irq_comm.o cpuid.o pmu.o
  +  i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mshv.o
   kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT)+= assigned-dev.o iommu.o
   kvm-intel-y+= vmx.o
   kvm-amd-y  += svm.o
  diff --git a/arch/x86/kvm/mshv.c b/arch/x86/kvm/mshv.c
  new file mode 100644
  index 000..ad367c44
  --- /dev/null
  +++ b/arch/x86/kvm/mshv.c
  @@ -0,0 +1,84 @@
  +/*
  + * KVM Microsoft Hyper-V extended paravirtualization
  + *
  + * This work is licensed under the terms of the GNU GPL, version 2.  See
  + * the COPYING file in the top-level directory.
  + *
  + * Copyright (C) 2015 Andrey Smetanin asmeta...@virtuozzo.com
  + *
  + * Authors: Andrey Smetanin asmeta...@virtuozzo.com
  + */
  +
  +#include linux/kvm_host.h
  +#include mshv.h
  +
  +int kvm_mshv_ctx_create(struct kvm *kvm)
  +{
  +   struct kvm_mshv_ctx *ctx;
  +
  +   ctx = kzalloc(sizeof(struct kvm_mshv_ctx), GFP_KERNEL);
  +   if (!ctx)
  +   return -ENOMEM;
  +
  +   ctx-kvm = kvm;
  +   atomic_set(ctx-crash_pending, 0);
  +   kvm-mshv_ctx = ctx;
  +   return 0;
  +}
  +
  +void kvm_mshv_ctx_destroy(struct kvm *kvm)
  +{
  +   kfree(kvm-mshv_ctx);
  +}
  +
  +int kvm_mshv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  +{
  +   struct kvm_mshv_ctx *ctx = kvm_vcpu_get_mshv_ctx(vcpu);
  +
  +   atomic_set(ctx-crash_pending, 1);
 
 Can you explain why crash_pending is needed?
 
 Do you know what the Windows guest behavior is here? From my reading
 of the Hyper-V TLFS 4.0, the guest will read HV_X64_MSR_CRASH_CTL at
 some point (not necessarily at the time of the crash, potentially at
 boot time?) to determine the crash actions that the hypervisor
 supports. I'm not sure why kvm needs to remember any state on reads
 from HV_X64_MSR_CRASH_CTL.
Thank you for notice, I agree that crash_pending is useless. We'll drop
it and will send new version of patch.
 
   +
  +   /* Response that KVM ready 

Re: [PATCH 1/2] kvm/x86: Hyper-V based guest crash data handling

2015-06-12 Thread Peter Hornyack
Hi Denis, Andrey, I have a few comments and questions. (re-sending in
plain-text mode, apologies for sending twice.)

On Thu, Jun 11, 2015 at 6:18 AM, Denis V. Lunev d...@openvz.org wrote:
 From: Andrey Smetanin asmeta...@virtuozzo.com

 Windows 2012 guests can notify hypervisor about occurred guest crash
 (Windows bugcheck(BSOD)) by writing specific Hyper-V msrs. This patch does
 handling of this MSR's by KVM and sending notification to user space that
 allows to gather Windows guest crash dump by QEMU/LIBVIRT.

 The idea is to provide functionality equal to pvpanic device without
 QEMU guest agent for Windows.

 The idea is borrowed from Linux HyperV bus driver and validated against
 Windows 2k12.

 Signed-off-by: Andrey Smetanin asmeta...@virtuozzo.com
 Signed-off-by: Denis V. Lunev d...@openvz.org
 CC: Gleb Natapov g...@kernel.org
 CC: Paolo Bonzini pbonz...@redhat.com
 ---
  arch/x86/include/uapi/asm/hyperv.h | 10 +
  arch/x86/kvm/Makefile  |  2 +-
  arch/x86/kvm/mshv.c| 84 
 ++
  arch/x86/kvm/mshv.h| 32 +++
  arch/x86/kvm/x86.c | 25 
  include/linux/kvm_host.h   | 17 
  include/uapi/linux/kvm.h   | 11 +
  7 files changed, 180 insertions(+), 1 deletion(-)
  create mode 100644 arch/x86/kvm/mshv.c
  create mode 100644 arch/x86/kvm/mshv.h

 diff --git a/arch/x86/include/uapi/asm/hyperv.h 
 b/arch/x86/include/uapi/asm/hyperv.h
 index ce6068d..25f3064 100644
 --- a/arch/x86/include/uapi/asm/hyperv.h
 +++ b/arch/x86/include/uapi/asm/hyperv.h
 @@ -199,6 +199,16 @@
  #define HV_X64_MSR_STIMER3_CONFIG  0x40B6
  #define HV_X64_MSR_STIMER3_COUNT   0x40B7

 +
 +/* Hypev-V guest crash notification MSR's */
 +#define HV_X64_MSR_CRASH_P00x4100
 +#define HV_X64_MSR_CRASH_P10x4101
 +#define HV_X64_MSR_CRASH_P20x4102
 +#define HV_X64_MSR_CRASH_P30x4103
 +#define HV_X64_MSR_CRASH_P40x4104
 +#define HV_X64_MSR_CRASH_CTL   0x4105
 +#define HV_CRASH_CTL_CRASH_NOTIFY  (1ULL  63)
 +
  #define HV_X64_MSR_HYPERCALL_ENABLE0x0001
  #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT12
  #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK \
 diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
 index 16e8f96..b1ec24d 100644
 --- a/arch/x86/kvm/Makefile
 +++ b/arch/x86/kvm/Makefile
 @@ -12,7 +12,7 @@ kvm-y += $(KVM)/kvm_main.o 
 $(KVM)/coalesced_mmio.o \
  kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o

  kvm-y  += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
 -  i8254.o ioapic.o irq_comm.o cpuid.o pmu.o
 +  i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mshv.o
  kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT)+= assigned-dev.o iommu.o
  kvm-intel-y+= vmx.o
  kvm-amd-y  += svm.o
 diff --git a/arch/x86/kvm/mshv.c b/arch/x86/kvm/mshv.c
 new file mode 100644
 index 000..ad367c44
 --- /dev/null
 +++ b/arch/x86/kvm/mshv.c
 @@ -0,0 +1,84 @@
 +/*
 + * KVM Microsoft Hyper-V extended paravirtualization
 + *
 + * This work is licensed under the terms of the GNU GPL, version 2.  See
 + * the COPYING file in the top-level directory.
 + *
 + * Copyright (C) 2015 Andrey Smetanin asmeta...@virtuozzo.com
 + *
 + * Authors: Andrey Smetanin asmeta...@virtuozzo.com
 + */
 +
 +#include linux/kvm_host.h
 +#include mshv.h
 +
 +int kvm_mshv_ctx_create(struct kvm *kvm)
 +{
 +   struct kvm_mshv_ctx *ctx;
 +
 +   ctx = kzalloc(sizeof(struct kvm_mshv_ctx), GFP_KERNEL);
 +   if (!ctx)
 +   return -ENOMEM;
 +
 +   ctx-kvm = kvm;
 +   atomic_set(ctx-crash_pending, 0);
 +   kvm-mshv_ctx = ctx;
 +   return 0;
 +}
 +
 +void kvm_mshv_ctx_destroy(struct kvm *kvm)
 +{
 +   kfree(kvm-mshv_ctx);
 +}
 +
 +int kvm_mshv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 +{
 +   struct kvm_mshv_ctx *ctx = kvm_vcpu_get_mshv_ctx(vcpu);
 +
 +   atomic_set(ctx-crash_pending, 1);

Can you explain why crash_pending is needed?

Do you know what the Windows guest behavior is here? From my reading
of the Hyper-V TLFS 4.0, the guest will read HV_X64_MSR_CRASH_CTL at
some point (not necessarily at the time of the crash, potentially at
boot time?) to determine the crash actions that the hypervisor
supports. I'm not sure why kvm needs to remember any state on reads
from HV_X64_MSR_CRASH_CTL.

  +
 +   /* Response that KVM ready to receive crash data */
 +   *pdata = HV_CRASH_CTL_CRASH_NOTIFY;

The TLFS says that CrashNotify is the only current action that is
supported on a guest crash, but other actions may be supported in the
future. I suggest defining something like
HV_X64_MSR_CRASH_CTL_CONTENTS (see below), which will only have
HV_CRASH_CTL_CRASH_NOTIFY set for 

[PATCH 1/2] kvm/x86: Hyper-V based guest crash data handling

2015-06-11 Thread Denis V. Lunev
From: Andrey Smetanin asmeta...@virtuozzo.com

Windows 2012 guests can notify hypervisor about occurred guest crash
(Windows bugcheck(BSOD)) by writing specific Hyper-V msrs. This patch does
handling of this MSR's by KVM and sending notification to user space that
allows to gather Windows guest crash dump by QEMU/LIBVIRT.

The idea is to provide functionality equal to pvpanic device without
QEMU guest agent for Windows.

The idea is borrowed from Linux HyperV bus driver and validated against
Windows 2k12.

Signed-off-by: Andrey Smetanin asmeta...@virtuozzo.com
Signed-off-by: Denis V. Lunev d...@openvz.org
CC: Gleb Natapov g...@kernel.org
CC: Paolo Bonzini pbonz...@redhat.com
---
 arch/x86/include/uapi/asm/hyperv.h | 10 +
 arch/x86/kvm/Makefile  |  2 +-
 arch/x86/kvm/mshv.c| 84 ++
 arch/x86/kvm/mshv.h| 32 +++
 arch/x86/kvm/x86.c | 25 
 include/linux/kvm_host.h   | 17 
 include/uapi/linux/kvm.h   | 11 +
 7 files changed, 180 insertions(+), 1 deletion(-)
 create mode 100644 arch/x86/kvm/mshv.c
 create mode 100644 arch/x86/kvm/mshv.h

diff --git a/arch/x86/include/uapi/asm/hyperv.h 
b/arch/x86/include/uapi/asm/hyperv.h
index ce6068d..25f3064 100644
--- a/arch/x86/include/uapi/asm/hyperv.h
+++ b/arch/x86/include/uapi/asm/hyperv.h
@@ -199,6 +199,16 @@
 #define HV_X64_MSR_STIMER3_CONFIG  0x40B6
 #define HV_X64_MSR_STIMER3_COUNT   0x40B7
 
+
+/* Hypev-V guest crash notification MSR's */
+#define HV_X64_MSR_CRASH_P00x4100
+#define HV_X64_MSR_CRASH_P10x4101
+#define HV_X64_MSR_CRASH_P20x4102
+#define HV_X64_MSR_CRASH_P30x4103
+#define HV_X64_MSR_CRASH_P40x4104
+#define HV_X64_MSR_CRASH_CTL   0x4105
+#define HV_CRASH_CTL_CRASH_NOTIFY  (1ULL  63)
+
 #define HV_X64_MSR_HYPERCALL_ENABLE0x0001
 #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT12
 #define HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK \
diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
index 16e8f96..b1ec24d 100644
--- a/arch/x86/kvm/Makefile
+++ b/arch/x86/kvm/Makefile
@@ -12,7 +12,7 @@ kvm-y += $(KVM)/kvm_main.o 
$(KVM)/coalesced_mmio.o \
 kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
 
 kvm-y  += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
-  i8254.o ioapic.o irq_comm.o cpuid.o pmu.o
+  i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mshv.o
 kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT)+= assigned-dev.o iommu.o
 kvm-intel-y+= vmx.o
 kvm-amd-y  += svm.o
diff --git a/arch/x86/kvm/mshv.c b/arch/x86/kvm/mshv.c
new file mode 100644
index 000..ad367c44
--- /dev/null
+++ b/arch/x86/kvm/mshv.c
@@ -0,0 +1,84 @@
+/*
+ * KVM Microsoft Hyper-V extended paravirtualization
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * Copyright (C) 2015 Andrey Smetanin asmeta...@virtuozzo.com
+ *
+ * Authors: Andrey Smetanin asmeta...@virtuozzo.com
+ */
+
+#include linux/kvm_host.h
+#include mshv.h
+
+int kvm_mshv_ctx_create(struct kvm *kvm)
+{
+   struct kvm_mshv_ctx *ctx;
+
+   ctx = kzalloc(sizeof(struct kvm_mshv_ctx), GFP_KERNEL);
+   if (!ctx)
+   return -ENOMEM;
+
+   ctx-kvm = kvm;
+   atomic_set(ctx-crash_pending, 0);
+   kvm-mshv_ctx = ctx;
+   return 0;
+}
+
+void kvm_mshv_ctx_destroy(struct kvm *kvm)
+{
+   kfree(kvm-mshv_ctx);
+}
+
+int kvm_mshv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
+{
+   struct kvm_mshv_ctx *ctx = kvm_vcpu_get_mshv_ctx(vcpu);
+
+   atomic_set(ctx-crash_pending, 1);
+
+   /* Response that KVM ready to receive crash data */
+   *pdata = HV_CRASH_CTL_CRASH_NOTIFY;
+   return 0;
+}
+
+int kvm_mshv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u32 msr, u64 data)
+{
+   struct kvm_mshv_ctx *ctx = kvm_vcpu_get_mshv_ctx(vcpu);
+
+   if (atomic_dec_and_test(ctx-crash_pending)) {
+   pr_debug(vcpu %p 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx,
+vcpu, ctx-crash_p0, ctx-crash_p1, ctx-crash_p2,
+ctx-crash_p3, ctx-crash_p4);
+
+   /* Crash data almost gathered so notify user space */
+   kvm_make_request(KVM_REQ_MSHV_CRASH, vcpu);
+   }
+
+   return 0;
+}
+
+int kvm_mshv_msr_set_crash_data(struct kvm_vcpu *vcpu, u32 msr, u64 data)
+{
+   struct kvm_mshv_ctx *ctx = kvm_vcpu_get_mshv_ctx(vcpu);
+
+   switch (msr) {
+   case HV_X64_MSR_CRASH_P0:
+   ctx-crash_p0 = data;
+   return 0;
+   case HV_X64_MSR_CRASH_P1:
+   ctx-crash_p1 = data;
+   return 0;
+   case HV_X64_MSR_CRASH_P2:
+