Skip to contentSkip to conversation

Add support for some HYPER-V PV features

PATCH Superseded Gleb Natapov 3 participants 13 messages Jan 13, 2010

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Add support for some HYPER-V PV features

HYPER-V provides PV capabilities for its guests and most new MS Windows detect and use them automatically. Older Windows guests need additional drivers to uses PV. This patch series implements some PV capabilities defined by HYPER-V spec for KVM. Windows guests running on KVM will be able to take advantage of them.

Gleb Natapov (3):
Implement bare minimum of HYPER-V MSRs.
Add HYPER-V apic access MSRs.
Implement NotifyLongSpinWait HYPER-V hypercall.

 arch/x86/include/asm/kvm_host.h   |    6 +
 arch/x86/include/asm/kvm_hyperv.h |  187 +++++++++++++++++++++++++++++++
 arch/x86/include/asm/kvm_para.h   |    1 +
 arch/x86/kvm/lapic.c              |   53 +++++++++
 arch/x86/kvm/lapic.h              |    8 ++
 arch/x86/kvm/trace.h              |   32 ++++++
 arch/x86/kvm/x86.c                |  218 ++++++++++++++++++++++++++++++++++++-
 include/linux/kvm.h               |    3 +
 8 files changed, 507 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/include/asm/kvm_hyperv.h
#1 lore raw

PATCH 1/3 Implement bare minimum of HYPER-V MSRs.

Minimum HYPER-V implementation should have GUEST_OS_ID, HYPERCALL and VP_INDEX MSRs.

---
 arch/x86/include/asm/kvm_host.h   |    4 +
 arch/x86/include/asm/kvm_hyperv.h |  187 +++++++++++++++++++++++++++++++++++++
 arch/x86/include/asm/kvm_para.h   |    1 +
 arch/x86/kvm/trace.h              |   32 +++++++
 arch/x86/kvm/x86.c                |  184 ++++++++++++++++++++++++++++++++++++-
 include/linux/kvm.h               |    1 +
 6 files changed, 408 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/include/asm/kvm_hyperv.h
arch/x86/include/asm/kvm_host.h +4 -0
index a4de557..7bfd468 100644
@@ -412,6 +412,10 @@ struct kvm_arch {
 	s64 kvmclock_offset;
 
 	struct kvm_xen_hvm_config xen_hvm_config;
+
+	/* fields used by HYPER-V emulation */
+	u64 hv_guest_os_id;
+	u64 hv_hypercall;
 };
 
 struct kvm_vm_stat {
arch/x86/include/asm/kvm_hyperv.h new file +187 -0

187 lines not on this page · raw message

arch/x86/include/asm/kvm_para.h +1 -0
index c584076..2892e43 100644
@@ -2,6 +2,7 @@
 #define _ASM_X86_KVM_PARA_H
 
 #include <linux/types.h>
+#include <asm/kvm_hyperv.h>
 
 /* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx.  It
  * should be used to determine that a VM is running under KVM.
arch/x86/kvm/trace.h +32 -0
index 816e044..0bfff0c 100644
@@ -56,6 +56,38 @@ TRACE_EVENT(kvm_hypercall,
 );
 
 /*
+ * Tracepoint for hypercall.
+ */
+TRACE_EVENT(kvm_hv_hypercall,
+	TP_PROTO(__u16 code, bool fast, __u16 rep_cnt, __u16 rep_idx,
+		 __u64 ingpa, __u64 outgpa),
+	TP_ARGS(code, fast, rep_cnt, rep_idx, ingpa, outgpa),
+
+	TP_STRUCT__entry(
+		__field(	__u16, 		code		)
+		__field(	bool,		fast		)
+		__field(	__u16,		rep_cnt		)
+		__field(	__u16,		rep_idx		)
+		__field(	__u64,		ingpa		)
+		__field(	__u64,		outgpa		)
+	),
+
+	TP_fast_assign(
+		__entry->code		= code;
+		__entry->fast		= fast;
+		__entry->rep_cnt	= rep_cnt;
+		__entry->rep_idx	= rep_idx;
+		__entry->ingpa		= ingpa;
+		__entry->outgpa		= outgpa;
+	),
+
+	TP_printk("code 0x%x %s cnt 0x%x idx 0x%x in 0x%llx out 0x%llx",
+		  __entry->code, __entry->fast ? "fast":"slow",
+		  __entry->rep_cnt, __entry->rep_idx,  __entry->ingpa,
+		  __entry->outgpa)
+);
+
+/*
  * Tracepoint for PIO.
  */
 TRACE_EVENT(kvm_pio,
arch/x86/kvm/x86.c +183 -1

231 lines not on this page · raw message

include/linux/kvm.h +1 -0
index f2feef6..e227cba 100644
@@ -497,6 +497,7 @@ struct kvm_ioeventfd {
 #endif
 #define KVM_CAP_S390_PSW 42
 #define KVM_CAP_PPC_SEGSTATE 43
+#define KVM_CAP_HYPERV 44
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
1.6.5
#6 lore raw

PATCH 2/3 Add HYPER-V apic access MSRs.

In reply to Gleb Natapov
---
 arch/x86/include/asm/kvm_host.h |    2 +
 arch/x86/kvm/lapic.c            |   53 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/lapic.h            |    8 ++++++
 arch/x86/kvm/x86.c              |   34 ++++++++++++++++++++++---
 include/linux/kvm.h             |    1 +
 5 files changed, 94 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
index 7bfd468..4d82c52 100644
@@ -362,6 +362,8 @@ struct kvm_vcpu_arch {
 	/* used for guest single stepping over the given code position */
 	u16 singlestep_cs;
 	unsigned long singlestep_rip;
+	/* fields used by HYPER-V emulation */
+	u64 hv_vapic;
 };
 
 struct kvm_mem_alias {
arch/x86/kvm/lapic.c +53 -0

56 lines not on this page · raw message

arch/x86/kvm/lapic.h +8 -0
index 40010b0..d081cb6 100644
@@ -48,4 +48,12 @@ void kvm_lapic_sync_to_vapic(struct kvm_vcpu *vcpu);
 
 int kvm_x2apic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
 int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
+
+int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
+int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
+
+static inline bool kvm_hv_vapic_enabled(struct kvm_vcpu *vcpu)
+{
+	return !!(vcpu->arch.hv_vapic & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE);
+}
 #endif
arch/x86/kvm/x86.c +30 -4
index 6972b2b..e058898 100644
@@ -629,7 +629,8 @@ static u32 msrs_to_save[] = {
 	MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
 	MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
-	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL
+	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
+	HV_X64_MSR_APIC_ASSIST_PAGE
 };
 
 static unsigned num_msrs_to_save;
@@ -1061,10 +1062,30 @@ static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 
 static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 {
-	pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x data 0x%llx\n",
-		  msr, data);
+	switch (msr) {
+	case HV_X64_MSR_APIC_ASSIST_PAGE: {
+		unsigned long addr;
+		vcpu->arch.hv_vapic = data;
+		if(!kvm_hv_vapic_enabled(vcpu))
+			break;
+		addr = gfn_to_hva(vcpu->kvm, data >>
+				  HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
+		if (kvm_is_error_hva(addr))
+			return 1;
+		memset((void*)addr, 0, PAGE_SIZE);
+		break;
+	}
+	case HV_X64_MSR_EOI:
+	case HV_X64_MSR_ICR:
+	case HV_X64_MSR_TPR:
+		return kvm_hv_vapic_msr_write(vcpu, msr, data);
+	default:
+		pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
+			  "data 0x%llx\n", msr, data);
+		return 1;
+	}
 
-	return 1;
+	return 0;
 }
 
 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
@@ -1324,6 +1345,10 @@ static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
 				data = r;
 		break;
 	}
+	case HV_X64_MSR_EOI:
+	case HV_X64_MSR_ICR:
+	case HV_X64_MSR_TPR:
+		return kvm_hv_vapic_msr_read(vcpu, msr, pdata);
 	default:
 		pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
 		return 1;
@@ -1524,6 +1549,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_ADJUST_CLOCK:
 	case KVM_CAP_VCPU_EVENTS:
 	case KVM_CAP_HYPERV:
+	case KVM_CAP_HYPERV_VAPIC:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +1 -0
index e227cba..5ce6173 100644
@@ -498,6 +498,7 @@ struct kvm_ioeventfd {
 #define KVM_CAP_S390_PSW 42
 #define KVM_CAP_PPC_SEGSTATE 43
 #define KVM_CAP_HYPERV 44
+#define KVM_CAP_HYPERV_VAPIC 45
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
1.6.5
#12 lore raw

PATCH 3/3 Implement NotifyLongSpinWait HYPER-V hypercall.

In reply to Gleb Natapov
---
 arch/x86/kvm/x86.c  |   10 +++++++++-
 include/linux/kvm.h |    1 +
 2 files changed, 10 insertions(+), 1 deletions(-)
arch/x86/kvm/x86.c +9 -1
index e058898..b7d2eec 100644
@@ -1550,6 +1550,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_VCPU_EVENTS:
 	case KVM_CAP_HYPERV:
 	case KVM_CAP_HYPERV_VAPIC:
+	case KVM_CAP_HYPERV_SPIN:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
@@ -3809,7 +3810,14 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 
 	trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
 
-	res = HV_STATUS_INVALID_HYPERCALL_CODE;
+	switch(code) {
+	case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
+		kvm_vcpu_on_spin(vcpu);
+		break;
+	default:
+		res = HV_STATUS_INVALID_HYPERCALL_CODE;
+		break;
+	}
 
 	ret = res | (((u64)rep_done & 0xfff) << 32);
 	if (longmode) {
include/linux/kvm.h +1 -0
index 5ce6173..4c4937e 100644
@@ -499,6 +499,7 @@ struct kvm_ioeventfd {
 #define KVM_CAP_PPC_SEGSTATE 43
 #define KVM_CAP_HYPERV 44
 #define KVM_CAP_HYPERV_VAPIC 45
+#define KVM_CAP_HYPERV_SPIN 46
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
Signature · 1 line
1.6.5
#2 lore raw

Re: [PATCH 1/3] Implement bare minimum of HYPER-V MSRs.

In reply to Gleb Natapov

arch/x86/include/asm/kvm_hyperv.h

Quoted 10 lines · Gleb Natapov

Minimum HYPER-V implementation should have GUEST_OS_ID, HYPERCALL and VP_INDEX MSRs.

arch/x86/include/asm/kvm_hyperv.h new file +0 -0
new file mode 100644
index 0000000..91211f3

Please name this asm/hyperv.h, so it can be used for Linux-as-Hyper-V-guest, not just Linux-as-host-impersonating-Hyper-V.+

Also put this in a separate patch.

arch/x86/kvm/x86.c static u32 msrs_to_save[] = { · line 633

Quoted 12 lines
arch/x86/kvm/x86.c +2 -1
index 6811e5e..6972b2b 100644
@@ -628,7 +628,8 @@ static u32 msrs_to_save[] = {
  #ifdef CONFIG_X86_64
  	MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  #endif
-	MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
+	MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
+	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL
 

End with trailing comma so future patches are nicer.

arch/x86/kvm/x86.c out: · line 1010

Quoted 6 lines
arch/x86/kvm/x86.c +4 -0
+static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
+{
+	return !!(kvm->arch.hv_hypercall&  HV_X64_MSR_HYPERCALL_ENABLE);
+}

!! is unnecessary for bool:

_Bool and(unsigned x) { return x & 16; }

0000000000000010 <and>:

  10:   c1 ef 04                shr    $0x4,%edi
  13:   89 f8                   mov    %edi,%eax
  15:   83 e0 01                and    $0x1,%eax
  18:   c3                      retq
Quoted 28 lines
arch/x86/kvm/x86.c +27 -0
+
+static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
+{
+	struct kvm *kvm = vcpu->kvm;
+
+	switch (msr) {
+	case HV_X64_MSR_GUEST_OS_ID:
+		kvm->arch.hv_guest_os_id = data;
+		/* setting guest os id to zero disables hypercall page */
+		if (!kvm->arch.hv_guest_os_id)
+			kvm->arch.hv_hypercall&= ~HV_X64_MSR_HYPERCALL_ENABLE;
+		break;
+	case HV_X64_MSR_HYPERCALL: {
+		u64 gfn;
+		unsigned long addr;
+		/* if guest os id is not set hypercall should remain disabled */
+		if (!kvm->arch.hv_guest_os_id&&  data)
+			break;
+		kvm->arch.hv_hypercall = data;
+		if(!kvm_hv_hypercall_enabled(kvm))
+			break;
+		gfn = kvm->arch.hv_hypercall>>
+			HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
+		addr = gfn_to_hva(kvm, gfn);
+		if (kvm_is_error_hva(addr))
+			return 1;
+		kvm_x86_ops->patch_hypercall(vcpu, (unsigned char*)addr);

arch/x86/kvm/x86.c out: · line 1060

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+		((unsigned char*)addr)[3] = 0xc3; /* ret */
+		break;
+	}
+	default:
+		pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
+			  "data 0x%llx\n", msr, data);
+		return 1;
+	}
+	return 0;
+}

We need locking in case a malicious guest issues partition-wide msrs from multiple vcpus simultaneously.

Quoted 4 lines
  int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  {
  	switch (msr) {

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1190

Quoted 10 lines
arch/x86/kvm/x86.c +6 -0
@@ -1117,6 +1181,16 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  		pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  			"0x%x data 0x%llx\n", msr, data);
  		break;
+	case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
+		if (kvm_hv_msr_partition_wide(msr)) {
+			int r;
+			mutex_lock(&vcpu->kvm->lock);
+			r = set_msr_hyperv_pw(vcpu, msr, data);
+			mutex_unlock(&vcpu->kvm->lock);
 

We do have locking. Any reason not to put it in set_msr_hyperv_pw? Seems cleaner.

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

Quoted 13 lines
arch/x86/kvm/x86.c +12 -0
+static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
+{
+	u64 data = 0;
+	struct kvm *kvm = vcpu->kvm;
+
+	switch (msr) {
+	case HV_X64_MSR_GUEST_OS_ID:
+		data = kvm->arch.hv_guest_os_id;
+		break;
+	case HV_X64_MSR_HYPERCALL:
+		data = kvm->arch.hv_hypercall;
+		break;

This could be non-atomic on i386. I don't think it matters.

arch/x86/kvm/x86.c static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0, · line 3806

Quoted 9 lines
  int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  {
  	unsigned long nr, a0, a1, a2, a3, ret;
  	int r = 1;
arch/x86/kvm/x86.c +3 -0
+	if(kvm_hv_hypercall_enabled(vcpu->kvm))
+		return kvm_hv_hypercall(vcpu);
+

Space after if.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw

Re: [PATCH 1/3] Implement bare minimum of HYPER-V MSRs.

Quoted 8 lines · Avi Kivity
Quoted 8 lines

int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) {

arch/x86/kvm/x86.c +1 -0
 	switch (msr) {
@@ -1117,6 +1181,16 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
 			"0x%x data 0x%llx\n", msr, data);
 		break;
+	case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Quoted 9 lines

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1189

Quoted 5 lines
arch/x86/kvm/x86.c +5 -0
+		if (kvm_hv_msr_partition_wide(msr)) {
+			int r;
+			mutex_lock(&vcpu->kvm->lock);
+			r = set_msr_hyperv_pw(vcpu, msr, data);
+			mutex_unlock(&vcpu->kvm->lock);

We do have locking. Any reason not to put it in set_msr_hyperv_pw? Seems cleaner.

Actually the way I did it looks cleaner to me. If locking is done inside set_msr_hyperv_pw() then each simple "return" statement there will have to be changed into {ret=val; goto unlock;}.

--
Gleb.

#9 lore raw

Re: [PATCH 2/3] Add HYPER-V apic access MSRs.

In reply to Gleb Natapov
Quoted 75 lines · Gleb Natapov
---
  arch/x86/include/asm/kvm_host.h |    2 +
  arch/x86/kvm/lapic.c            |   53 +++++++++++++++++++++++++++++++++++++++
  arch/x86/kvm/lapic.h            |    8 ++++++
  arch/x86/kvm/x86.c              |   34 ++++++++++++++++++++++---
  include/linux/kvm.h             |    1 +
  5 files changed, 94 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
index 7bfd468..4d82c52 100644
@@ -362,6 +362,8 @@ struct kvm_vcpu_arch {
  	/* used for guest single stepping over the given code position */
  	u16 singlestep_cs;
  	unsigned long singlestep_rip;
+	/* fields used by HYPER-V emulation */
+	u64 hv_vapic;
  };
 
  struct kvm_mem_alias {
arch/x86/kvm/lapic.c +43 -0
index ba8c045..063963f 100644
@@ -1246,3 +1246,56 @@ int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
 
  	return 0;
  }
+
+static u32 kvm_hv_vapic_msr2reg(u32 msr)
+{
+	u32 reg;
+	switch (msr) {
+	case HV_X64_MSR_EOI:
+		reg = APIC_EOI;
+		break;
+	case HV_X64_MSR_ICR:
+		reg = APIC_ICR;
+		break;
+	case HV_X64_MSR_TPR:
+		reg = APIC_TASKPRI;
+		break;
+	default:
+		reg = -1;
+		break;
+	}
+
+	return reg;
+}
+
+int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+	u32 reg = kvm_hv_vapic_msr2reg(msr);
+
+	if (!irqchip_in_kernel(vcpu->kvm))
+		return 1;
+
+	/* if this is ICR write vector before command */
+	if (reg == APIC_ICR)
+		apic_reg_write(apic, APIC_ICR2, (u32)(data>>  32));
+	return apic_reg_write(apic, reg, (u32)data);
+}
+
+int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+	u32 reg = kvm_hv_vapic_msr2reg(msr), low, high = 0;
+
+	if (!irqchip_in_kernel(vcpu->kvm))
+		return 1;

arch/x86/kvm/lapic.c int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data) · line 1301

Quoted 10 lines
arch/x86/kvm/lapic.c +10 -0
+
+	if (apic_reg_read(apic, reg, 4,&low))
+		return 1;
+	if (reg == APIC_ICR)
+		apic_reg_read(apic, APIC_ICR2, 4,&high);
+
+	*data = (((u64)high)<<  32) | low;
+
+	return 0;
+}

I think it's cleaner to put all this into the (set_msr and get_msr). You're just converting register numbers back and forth, while they are known at the call site.

Quoted 6 lines
  #endif
arch/x86/kvm/x86.c +0 -0
index 6972b2b..e058898 100644

arch/x86/kvm/x86.c static u32 msrs_to_save[] = { · line 635

Quoted 8 lines
arch/x86/kvm/x86.c +2 -1
@@ -629,7 +629,8 @@ static u32 msrs_to_save[] = {
  	MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  #endif
  	MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
-	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL
+	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
+	HV_X64_MSR_APIC_ASSIST_PAGE
  };
 

, at the end.

Quoted 5 lines
arch/x86/kvm/x86.c +0 -0
  static unsigned num_msrs_to_save;
@@ -1061,10 +1062,30 @@ static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 
  static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)

arch/x86/kvm/x86.c static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1071

Quoted 10 lines
arch/x86/kvm/x86.c +6 -2
  {
-	pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x data 0x%llx\n",
-		  msr, data);
+	switch (msr) {
+	case HV_X64_MSR_APIC_ASSIST_PAGE: {
+		unsigned long addr;
+		vcpu->arch.hv_vapic = data;
+		if(!kvm_hv_vapic_enabled(vcpu))
+			break;
 

Space after if.

arch/x86/kvm/x86.c static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1087

Quoted 5 lines
arch/x86/kvm/x86.c +4 -0
+		addr = gfn_to_hva(vcpu->kvm, data>>
+				  HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
+		if (kvm_is_error_hva(addr))
+			return 1;
 

But the spec actually permits addresses outside RAM?

arch/x86/kvm/x86.c static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1087

Quoted 2 lines
arch/x86/kvm/x86.c +1 -0
+		memset((void*)addr, 0, PAGE_SIZE);
 

clear_user_page()!

arch/x86/kvm/x86.c static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1087

Quoted 7 lines
arch/x86/kvm/x86.c +6 -0
+		break;
+	}
+	case HV_X64_MSR_EOI:
+	case HV_X64_MSR_ICR:
+	case HV_X64_MSR_TPR:
+		return kvm_hv_vapic_msr_write(vcpu, msr, data);
 

Here, just call the apic function with the known apic register number.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

Re: [PATCH 1/3] Implement bare minimum of HYPER-V MSRs.

In reply to Gleb Natapov
Quoted 20 lines · Gleb Natapov
Quoted 8 lines · Avi Kivity
Quoted 8 lines
  int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  {
  	switch (msr) {
arch/x86/kvm/x86.c +1 -0
@@ -1117,6 +1181,16 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  		pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  			"0x%x data 0x%llx\n", msr, data);
  		break;
+	case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
Quoted 10 lines

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)

Quoted 6 lines
arch/x86/kvm/x86.c +5 -0
+		if (kvm_hv_msr_partition_wide(msr)) {
+			int r;
+			mutex_lock(&vcpu->kvm->lock);
+			r = set_msr_hyperv_pw(vcpu, msr, data);
+			mutex_unlock(&vcpu->kvm->lock);
 

We do have locking. Any reason not to put it in set_msr_hyperv_pw? Seems cleaner.

Quoted 5 lines

Actually the way I did it looks cleaner to me. If locking is done inside set_msr_hyperv_pw() then each simple "return" statement there will have to be changed into {ret=val; goto unlock;}.

A break should suffice.

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw

Re: [PATCH 2/3] Add HYPER-V apic access MSRs.

In reply to Anthony Liguori
Quoted 6 lines · Anthony Liguori
Quoted 2 lines · Gleb Natapov

And perf results from this? Even just a rough idea of how helpful this is?

For those changes specifically no, but we already know that x2apic is very beneficial from performance POV and what HYPER-V defines here is exactly what makes x2apic improve performance.

--
Gleb.

#5 lore raw

Re: [PATCH 1/3] Implement bare minimum of HYPER-V MSRs.

In reply to Avi Kivity
Quoted 18 lines · Avi Kivity
Quoted 17 lines · Gleb Natapov
Quoted 16 lines · Avi Kivity

arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1189

Quoted 13 lines

int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) {

arch/x86/kvm/x86.c +6 -0
 	switch (msr) {
@@ -1117,6 +1181,16 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 		pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
 			"0x%x data 0x%llx\n", msr, data);
 		break;
+	case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
+		if (kvm_hv_msr_partition_wide(msr)) {
+			int r;
+			mutex_lock(&vcpu->kvm->lock);
+			r = set_msr_hyperv_pw(vcpu, msr, data);
+			mutex_unlock(&vcpu->kvm->lock);

We do have locking. Any reason not to put it in set_msr_hyperv_pw? Seems cleaner.

Quoted 7 lines
Quoted 4 lines

Actually the way I did it looks cleaner to me. If locking is done inside set_msr_hyperv_pw() then each simple "return" statement there will have to be changed into {ret=val; goto unlock;}.

A break should suffice.

It will still be {ret=val; break;}. 3 lines instead of one.

--
Gleb.

#10 lore raw

Re: [PATCH 2/3] Add HYPER-V apic access MSRs.

In reply to Avi Kivity
Quoted 81 lines · Avi Kivity
Quoted 80 lines · Gleb Natapov
---
 arch/x86/include/asm/kvm_host.h |    2 +
 arch/x86/kvm/lapic.c            |   53 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/lapic.h            |    8 ++++++
 arch/x86/kvm/x86.c              |   34 ++++++++++++++++++++++---
 include/linux/kvm.h             |    1 +
 5 files changed, 94 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
index 7bfd468..4d82c52 100644
@@ -362,6 +362,8 @@ struct kvm_vcpu_arch {
 	/* used for guest single stepping over the given code position */
 	u16 singlestep_cs;
 	unsigned long singlestep_rip;
+	/* fields used by HYPER-V emulation */
+	u64 hv_vapic;
 };
 
 struct kvm_mem_alias {
arch/x86/kvm/lapic.c +49 -0
index ba8c045..063963f 100644
@@ -1246,3 +1246,56 @@ int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
 
 	return 0;
 }
+
+static u32 kvm_hv_vapic_msr2reg(u32 msr)
+{
+	u32 reg;
+	switch (msr) {
+	case HV_X64_MSR_EOI:
+		reg = APIC_EOI;
+		break;
+	case HV_X64_MSR_ICR:
+		reg = APIC_ICR;
+		break;
+	case HV_X64_MSR_TPR:
+		reg = APIC_TASKPRI;
+		break;
+	default:
+		reg = -1;
+		break;
+	}
+
+	return reg;
+}
+
+int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+	u32 reg = kvm_hv_vapic_msr2reg(msr);
+
+	if (!irqchip_in_kernel(vcpu->kvm))
+		return 1;
+
+	/* if this is ICR write vector before command */
+	if (reg == APIC_ICR)
+		apic_reg_write(apic, APIC_ICR2, (u32)(data>>  32));
+	return apic_reg_write(apic, reg, (u32)data);
+}
+
+int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+	u32 reg = kvm_hv_vapic_msr2reg(msr), low, high = 0;
+
+	if (!irqchip_in_kernel(vcpu->kvm))
+		return 1;
+
+	if (apic_reg_read(apic, reg, 4,&low))
+		return 1;
+	if (reg == APIC_ICR)
+		apic_reg_read(apic, APIC_ICR2, 4,&high);
+
Quoted 9 lines

arch/x86/kvm/lapic.c int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data) · line 1301

Quoted 4 lines
arch/x86/kvm/lapic.c +4 -0
+	*data = (((u64)high)<<  32) | low;
+
+	return 0;
+}

I think it's cleaner to put all this into the (set_msr and get_msr). You're just converting register numbers back and forth, while they are known at the call site.

I thought about it. It will bring apic internals into x86.c. If you are OK with that I'll do it like you say.

Quoted 20 lines

arch/x86/kvm/x86.c static u32 msrs_to_save[] = { · line 634

Quoted 13 lines

#endif

arch/x86/kvm/x86.c +2 -1
index 6972b2b..e058898 100644
@@ -629,7 +629,8 @@ static u32 msrs_to_save[] = {
 	MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
 	MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
-	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL
+	HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
+	HV_X64_MSR_APIC_ASSIST_PAGE
 };

, at the end.

Quoted 4 lines
arch/x86/kvm/x86.c +0 -0
 static unsigned num_msrs_to_save;
@@ -1061,10 +1062,30 @@ static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 
Quoted 12 lines

arch/x86/kvm/x86.c static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1070

Quoted 10 lines
arch/x86/kvm/x86.c +6 -2
 static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
 {
-	pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x data 0x%llx\n",
-		  msr, data);
+	switch (msr) {
+	case HV_X64_MSR_APIC_ASSIST_PAGE: {
+		unsigned long addr;
+		vcpu->arch.hv_vapic = data;
+		if(!kvm_hv_vapic_enabled(vcpu))
+			break;

Space after if.

Forgot about checkpatch again :(

Quoted 7 lines

arch/x86/kvm/x86.c static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1074

Quoted 5 lines
arch/x86/kvm/x86.c +4 -0
+		addr = gfn_to_hva(vcpu->kvm, data>>
+				  HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
+		if (kvm_is_error_hva(addr))
+			return 1;

But the spec actually permits addresses outside RAM?

Is says "in GPA space". Not sure how to interpret it. The guest I run uses address in real RAM.

Quoted 5 lines

arch/x86/kvm/x86.c static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1075

Quoted 2 lines
arch/x86/kvm/x86.c +1 -0
+		memset((void*)addr, 0, PAGE_SIZE);

clear_user_page()!

Ugh.

Quoted 11 lines

arch/x86/kvm/x86.c static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1081

Quoted 6 lines
arch/x86/kvm/x86.c +6 -0
+		break;
+	}
+	case HV_X64_MSR_EOI:
+	case HV_X64_MSR_ICR:
+	case HV_X64_MSR_TPR:
+		return kvm_hv_vapic_msr_write(vcpu, msr, data);

Here, just call the apic function with the known apic register number.

Signature · 1 line
error compiling committee.c: too many arguments to function

--
Gleb.

#11 lore raw

Re: [PATCH 2/3] Add HYPER-V apic access MSRs.

Quoted 24 lines · Gleb Natapov
Quoted 14 lines
Quoted 14 lines
arch/x86/kvm/lapic.c +13 -0
+int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+	u32 reg = kvm_hv_vapic_msr2reg(msr), low, high = 0;
+
+	if (!irqchip_in_kernel(vcpu->kvm))
+		return 1;
+
+	if (apic_reg_read(apic, reg, 4,&low))
+		return 1;
+	if (reg == APIC_ICR)
+		apic_reg_read(apic, APIC_ICR2, 4,&high);
+
Quoted 10 lines

arch/x86/kvm/lapic.c int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data) · line 1301

Quoted 5 lines
arch/x86/kvm/lapic.c +4 -0
+	*data = (((u64)high)<<   32) | low;
+
+	return 0;
+}

I think it's cleaner to put all this into the (set_msr and get_msr). You're just converting register numbers back and forth, while they are known at the call site.

Quoted 3 lines

I thought about it. It will bring apic internals into x86.c. If you are OK with that I'll do it like you say.

I think it's better than that tangle.

Quoted 5 lines
Quoted 2 lines

Space after if.

Forgot about checkpatch again :(

Should be hardwired into your neurons by now...

Quoted 10 lines
Quoted 7 lines

arch/x86/kvm/x86.c static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1074

Quoted 5 lines
arch/x86/kvm/x86.c +4 -0
+		addr = gfn_to_hva(vcpu->kvm, data>>
+				  HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
+		if (kvm_is_error_hva(addr))
+			return 1;

But the spec actually permits addresses outside RAM?

Is says "in GPA space". Not sure how to interpret it. The guest I run uses address in real RAM.

My interpretation is that it can be anywhere. But that will be difficult, let's stick with this and hope we don't run into trouble.

Signature · 1 line
error compiling committee.c: too many arguments to function