All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/6] Add Hyper-v extended hypercall support in KVM
@ 2022-11-05  4:56 Vipin Sharma
  2022-11-05  4:56 ` [PATCH 1/6] KVM: x86: hyper-v: Use common code for hypercall userspace exit Vipin Sharma
                   ` (5 more replies)
  0 siblings, 6 replies; 23+ messages in thread
From: Vipin Sharma @ 2022-11-05  4:56 UTC (permalink / raw)
  To: seanjc, pbonzini, vkuznets; +Cc: dmatlack, kvm, linux-kernel, Vipin Sharma

This patch series adds Hyper-V extended hypercall support. All
hypercalls will exit to userspace if CPUID.0x40000003.EBX BIT(20) is
set.

Patch 4 and 5 are prep patches, they move some code to hyperv.h later
used by newly introduced test hyperv_extended_hcalls in Patch 6.

RFC: https://lore.kernel.org/lkml/20221021185916.1494314-1-vipinsh@google.com/

Vipin Sharma (6):
  KVM: x86: hyper-v: Use common code for hypercall userspace exit
  KVM: x86: hyper-v: Add extended hypercall support in Hyper-v
  KVM: selftests: Test Hyper-V extended hypercall enablement
  KVM: selftests: Make Hyper-V guest OS ID common
  KVM: selftests: Move hypercall() to hyper.h
  KVM: selftests: Test Hyper-V extended hypercall exit to userspace

 arch/x86/kvm/hyperv.c                         | 43 +++++----
 tools/testing/selftests/kvm/.gitignore        |  1 +
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../selftests/kvm/include/x86_64/hyperv.h     | 31 +++++++
 .../selftests/kvm/x86_64/hyperv_clock.c       |  2 +-
 .../kvm/x86_64/hyperv_extended_hcalls.c       | 90 +++++++++++++++++++
 .../selftests/kvm/x86_64/hyperv_features.c    | 32 +++----
 .../selftests/kvm/x86_64/hyperv_svm_test.c    |  2 +-
 8 files changed, 163 insertions(+), 39 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c

-- 
2.38.1.273.g43a17bfeac-goog


^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 1/6] KVM: x86: hyper-v: Use common code for hypercall userspace exit
  2022-11-05  4:56 [PATCH 0/6] Add Hyper-v extended hypercall support in KVM Vipin Sharma
@ 2022-11-05  4:56 ` Vipin Sharma
  2022-11-05  4:57 ` [PATCH 2/6] KVM: x86: hyper-v: Add extended hypercall support in Hyper-v Vipin Sharma
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 23+ messages in thread
From: Vipin Sharma @ 2022-11-05  4:56 UTC (permalink / raw)
  To: seanjc, pbonzini, vkuznets; +Cc: dmatlack, kvm, linux-kernel, Vipin Sharma

Remove duplicate code to exit to userspace for hyper-v hypercalls and
use a common place to exit.

Signed-off-by: Vipin Sharma <vipinsh@google.com>
Suggested-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/hyperv.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)

diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index 0adf4a437e85..db2f96a0ae23 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -2270,14 +2270,7 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
 			break;
 		}
-		vcpu->run->exit_reason = KVM_EXIT_HYPERV;
-		vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
-		vcpu->run->hyperv.u.hcall.input = hc.param;
-		vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
-		vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
-		vcpu->arch.complete_userspace_io =
-				kvm_hv_hypercall_complete_userspace;
-		return 0;
+		goto hypercall_userspace_exit;
 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
 		if (unlikely(hc.var_cnt)) {
 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
@@ -2336,14 +2329,7 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 			ret = HV_STATUS_OPERATION_DENIED;
 			break;
 		}
-		vcpu->run->exit_reason = KVM_EXIT_HYPERV;
-		vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
-		vcpu->run->hyperv.u.hcall.input = hc.param;
-		vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
-		vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
-		vcpu->arch.complete_userspace_io =
-				kvm_hv_hypercall_complete_userspace;
-		return 0;
+		goto hypercall_userspace_exit;
 	}
 	default:
 		ret = HV_STATUS_INVALID_HYPERCALL_CODE;
@@ -2352,6 +2338,15 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 
 hypercall_complete:
 	return kvm_hv_hypercall_complete(vcpu, ret);
+
+hypercall_userspace_exit:
+	vcpu->run->exit_reason = KVM_EXIT_HYPERV;
+	vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
+	vcpu->run->hyperv.u.hcall.input = hc.param;
+	vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
+	vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
+	vcpu->arch.complete_userspace_io = kvm_hv_hypercall_complete_userspace;
+	return 0;
 }
 
 void kvm_hv_init_vm(struct kvm *kvm)
-- 
2.38.1.273.g43a17bfeac-goog


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH 2/6] KVM: x86: hyper-v: Add extended hypercall support in Hyper-v
  2022-11-05  4:56 [PATCH 0/6] Add Hyper-v extended hypercall support in KVM Vipin Sharma
  2022-11-05  4:56 ` [PATCH 1/6] KVM: x86: hyper-v: Use common code for hypercall userspace exit Vipin Sharma
@ 2022-11-05  4:57 ` Vipin Sharma
  2022-11-05  4:57 ` [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement Vipin Sharma
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 23+ messages in thread
From: Vipin Sharma @ 2022-11-05  4:57 UTC (permalink / raw)
  To: seanjc, pbonzini, vkuznets; +Cc: dmatlack, kvm, linux-kernel, Vipin Sharma

Add support for extended hypercall in Hyper-v. Hyper-v TLFS 6.0b
describes hypercalls above call code 0x8000 as extended hypercalls.

A Hyper-v hypervisor's guest VM finds availability of extended
hypercalls via CPUID.0x40000003.EBX BIT(20). If the bit is set then the
guest can call extended hypercalls.

All extended hypercalls will exit to userspace by default. This allows
for easy support of future hypercalls without being dependent on KVM
releases.

If there will be need to process the hypercall in KVM instead of
userspace then KVM can create a capability which userspace can query to
know which hypercalls can be handled by the KVM and enable handling
of those hypercalls.

Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
 arch/x86/kvm/hyperv.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index db2f96a0ae23..85c6100f4451 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -40,6 +40,12 @@
 
 #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64)
 
+/*
+ * The TLFS carves out 64 possible extended hypercalls, numbered sequentially
+ * after the base capabilities extended hypercall.
+ */
+#define HV_EXT_CALL_MAX (HV_EXT_CALL_QUERY_CAPABILITIES + 64)
+
 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
 				bool vcpu_kick);
 
@@ -2178,6 +2184,9 @@ static bool hv_check_hypercall_access(struct kvm_vcpu_hv *hv_vcpu, u16 code)
 	case HVCALL_SEND_IPI:
 		return hv_vcpu->cpuid_cache.enlightenments_eax &
 			HV_X64_CLUSTER_IPI_RECOMMENDED;
+	case HV_EXT_CALL_QUERY_CAPABILITIES ... HV_EXT_CALL_MAX:
+		return hv_vcpu->cpuid_cache.features_ebx &
+				HV_ENABLE_EXTENDED_HYPERCALLS;
 	default:
 		break;
 	}
@@ -2331,6 +2340,12 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 		}
 		goto hypercall_userspace_exit;
 	}
+	case HV_EXT_CALL_QUERY_CAPABILITIES ... HV_EXT_CALL_MAX:
+		if (unlikely(hc.fast)) {
+			ret = HV_STATUS_INVALID_PARAMETER;
+			break;
+		}
+		goto hypercall_userspace_exit;
 	default:
 		ret = HV_STATUS_INVALID_HYPERCALL_CODE;
 		break;
@@ -2489,6 +2504,7 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
 
 			ent->ebx |= HV_POST_MESSAGES;
 			ent->ebx |= HV_SIGNAL_EVENTS;
+			ent->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS;
 
 			ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE;
 			ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE;
-- 
2.38.1.273.g43a17bfeac-goog


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement
  2022-11-05  4:56 [PATCH 0/6] Add Hyper-v extended hypercall support in KVM Vipin Sharma
  2022-11-05  4:56 ` [PATCH 1/6] KVM: x86: hyper-v: Use common code for hypercall userspace exit Vipin Sharma
  2022-11-05  4:57 ` [PATCH 2/6] KVM: x86: hyper-v: Add extended hypercall support in Hyper-v Vipin Sharma
@ 2022-11-05  4:57 ` Vipin Sharma
  2022-11-07 18:27   ` David Matlack
  2022-11-05  4:57 ` [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common Vipin Sharma
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 23+ messages in thread
From: Vipin Sharma @ 2022-11-05  4:57 UTC (permalink / raw)
  To: seanjc, pbonzini, vkuznets; +Cc: dmatlack, kvm, linux-kernel, Vipin Sharma

Test Extended hypercall CPUID enablement

Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
 tools/testing/selftests/kvm/include/x86_64/hyperv.h  | 4 ++++
 tools/testing/selftests/kvm/x86_64/hyperv_features.c | 9 +++++++++
 2 files changed, 13 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
index b66910702c0a..075fd29071a6 100644
--- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
+++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
@@ -110,6 +110,7 @@
 #define HV_ACCESS_STATS				BIT(8)
 #define HV_DEBUGGING				BIT(11)
 #define HV_CPU_MANAGEMENT			BIT(12)
+#define HV_ENABLE_EXTENDED_HYPERCALLS		BIT(20)
 #define HV_ISOLATION				BIT(22)
 
 /* HYPERV_CPUID_FEATURES.EDX */
@@ -164,6 +165,9 @@
 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0
 
+/* Extended hypercalls */
+#define HV_EXT_CALL_QUERY_CAPABILITIES		0x8001
+
 #define HV_FLUSH_ALL_PROCESSORS			BIT(0)
 #define HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES	BIT(1)
 #define HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY	BIT(2)
diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
index 05b32e550a80..6b443ce456b6 100644
--- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
+++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
@@ -602,6 +602,15 @@ static void guest_test_hcalls_access(void)
 			hcall->expect = HV_STATUS_SUCCESS;
 			break;
 		case 19:
+			hcall->control = HV_EXT_CALL_QUERY_CAPABILITIES;
+			hcall->expect = HV_STATUS_ACCESS_DENIED;
+			break;
+		case 20:
+			feat->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS;
+			hcall->control = HV_EXT_CALL_QUERY_CAPABILITIES | HV_HYPERCALL_FAST_BIT;
+			hcall->expect = HV_STATUS_INVALID_PARAMETER;
+			break;
+		case 21:
 			kvm_vm_free(vm);
 			return;
 		}
-- 
2.38.1.273.g43a17bfeac-goog


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common
  2022-11-05  4:56 [PATCH 0/6] Add Hyper-v extended hypercall support in KVM Vipin Sharma
                   ` (2 preceding siblings ...)
  2022-11-05  4:57 ` [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement Vipin Sharma
@ 2022-11-05  4:57 ` Vipin Sharma
  2022-11-07 19:08   ` David Matlack
  2022-11-09 13:48   ` Vitaly Kuznetsov
  2022-11-05  4:57 ` [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h Vipin Sharma
  2022-11-05  4:57 ` [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace Vipin Sharma
  5 siblings, 2 replies; 23+ messages in thread
From: Vipin Sharma @ 2022-11-05  4:57 UTC (permalink / raw)
  To: seanjc, pbonzini, vkuznets; +Cc: dmatlack, kvm, linux-kernel, Vipin Sharma

Make guest OS ID calculation common to all hyperv tests and similar to
hv_generate_guest_id().

Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
 tools/testing/selftests/kvm/include/x86_64/hyperv.h  | 10 ++++++++++
 tools/testing/selftests/kvm/x86_64/hyperv_clock.c    |  2 +-
 tools/testing/selftests/kvm/x86_64/hyperv_features.c |  6 ++----
 tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c |  2 +-
 4 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
index 075fd29071a6..9d8c325af1d9 100644
--- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
+++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
@@ -9,6 +9,10 @@
 #ifndef SELFTEST_KVM_HYPERV_H
 #define SELFTEST_KVM_HYPERV_H
 
+#include <linux/version.h>
+
+#define HV_LINUX_VENDOR_ID			0x8100
+
 #define HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS	0x40000000
 #define HYPERV_CPUID_INTERFACE			0x40000001
 #define HYPERV_CPUID_VERSION			0x40000002
@@ -189,4 +193,10 @@
 /* hypercall options */
 #define HV_HYPERCALL_FAST_BIT		BIT(16)
 
+static inline uint64_t hv_linux_guest_id(void)
+{
+	return ((uint64_t)HV_LINUX_VENDOR_ID << 48) |
+	       ((uint64_t)LINUX_VERSION_CODE << 16);
+}
+
 #endif /* !SELFTEST_KVM_HYPERV_H */
diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
index d576bc8ce823..f9112c5dc3f7 100644
--- a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
+++ b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
@@ -104,7 +104,7 @@ static void guest_main(struct ms_hyperv_tsc_page *tsc_page, vm_paddr_t tsc_page_
 
 	/* Set Guest OS id to enable Hyper-V emulation */
 	GUEST_SYNC(1);
-	wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
+	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
 	GUEST_SYNC(2);
 
 	check_tsc_msr_rdtsc();
diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
index 6b443ce456b6..b5a42cf1ad9d 100644
--- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
+++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
@@ -13,8 +13,6 @@
 #include "processor.h"
 #include "hyperv.h"
 
-#define LINUX_OS_ID ((u64)0x8100 << 48)
-
 static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
 				vm_vaddr_t output_address, uint64_t *hv_status)
 {
@@ -71,7 +69,7 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
 
 	GUEST_ASSERT(hcall->control);
 
-	wrmsr(HV_X64_MSR_GUEST_OS_ID, LINUX_OS_ID);
+	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
 	wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
 
 	if (!(hcall->control & HV_HYPERCALL_FAST_BIT)) {
@@ -169,7 +167,7 @@ static void guest_test_msrs_access(void)
 			 */
 			msr->idx = HV_X64_MSR_GUEST_OS_ID;
 			msr->write = 1;
-			msr->write_val = LINUX_OS_ID;
+			msr->write_val = hv_linux_guest_id();
 			msr->available = 1;
 			break;
 		case 3:
diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
index a380ad7bb9b3..2c13a144b04c 100644
--- a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
+++ b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
@@ -69,7 +69,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm)
 
 	GUEST_SYNC(1);
 
-	wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
+	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
 
 	GUEST_ASSERT(svm->vmcb_gpa);
 	/* Prepare for L2 execution. */
-- 
2.38.1.273.g43a17bfeac-goog


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h
  2022-11-05  4:56 [PATCH 0/6] Add Hyper-v extended hypercall support in KVM Vipin Sharma
                   ` (3 preceding siblings ...)
  2022-11-05  4:57 ` [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common Vipin Sharma
@ 2022-11-05  4:57 ` Vipin Sharma
  2022-11-07 18:30   ` David Matlack
  2022-11-09 13:46   ` Vitaly Kuznetsov
  2022-11-05  4:57 ` [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace Vipin Sharma
  5 siblings, 2 replies; 23+ messages in thread
From: Vipin Sharma @ 2022-11-05  4:57 UTC (permalink / raw)
  To: seanjc, pbonzini, vkuznets; +Cc: dmatlack, kvm, linux-kernel, Vipin Sharma

hypercall() can be used by other hyperv tests, move it to hyperv.h.

Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
 .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
 .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
 2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
index 9d8c325af1d9..87d8d9e444f7 100644
--- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
+++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
@@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
 	       ((uint64_t)LINUX_VERSION_CODE << 16);
 }
 
+static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
+				vm_vaddr_t output_address, uint64_t *hv_status)
+{
+	uint8_t vector;
+
+	/* Note both the hypercall and the "asm safe" clobber r9-r11. */
+	asm volatile("mov %[output_address], %%r8\n\t"
+		     KVM_ASM_SAFE("vmcall")
+		     : "=a" (*hv_status),
+		       "+c" (control), "+d" (input_address),
+		       KVM_ASM_SAFE_OUTPUTS(vector)
+		     : [output_address] "r"(output_address),
+		       "a" (-EFAULT)
+		     : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
+	return vector;
+}
+
 #endif /* !SELFTEST_KVM_HYPERV_H */
diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
index b5a42cf1ad9d..31b22ee07dfb 100644
--- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
+++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
@@ -13,23 +13,6 @@
 #include "processor.h"
 #include "hyperv.h"
 
-static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
-				vm_vaddr_t output_address, uint64_t *hv_status)
-{
-	uint8_t vector;
-
-	/* Note both the hypercall and the "asm safe" clobber r9-r11. */
-	asm volatile("mov %[output_address], %%r8\n\t"
-		     KVM_ASM_SAFE("vmcall")
-		     : "=a" (*hv_status),
-		       "+c" (control), "+d" (input_address),
-		       KVM_ASM_SAFE_OUTPUTS(vector)
-		     : [output_address] "r"(output_address),
-		       "a" (-EFAULT)
-		     : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
-	return vector;
-}
-
 struct msr_data {
 	uint32_t idx;
 	bool available;
-- 
2.38.1.273.g43a17bfeac-goog


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace
  2022-11-05  4:56 [PATCH 0/6] Add Hyper-v extended hypercall support in KVM Vipin Sharma
                   ` (4 preceding siblings ...)
  2022-11-05  4:57 ` [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h Vipin Sharma
@ 2022-11-05  4:57 ` Vipin Sharma
  2022-11-07 19:01   ` David Matlack
  5 siblings, 1 reply; 23+ messages in thread
From: Vipin Sharma @ 2022-11-05  4:57 UTC (permalink / raw)
  To: seanjc, pbonzini, vkuznets; +Cc: dmatlack, kvm, linux-kernel, Vipin Sharma

Hyper-V extended hypercalls by default exit to userspace. Verify
userspace gets the call, update the result and then guest verifies
result it received.

Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
 tools/testing/selftests/kvm/.gitignore        |  1 +
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../kvm/x86_64/hyperv_extended_hcalls.c       | 90 +++++++++++++++++++
 3 files changed, 92 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c

diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
index 2f0d705db9db..ffe06dd1cc6e 100644
--- a/tools/testing/selftests/kvm/.gitignore
+++ b/tools/testing/selftests/kvm/.gitignore
@@ -24,6 +24,7 @@
 /x86_64/kvm_pv_test
 /x86_64/hyperv_clock
 /x86_64/hyperv_cpuid
+/x86_64/hyperv_extended_hcalls
 /x86_64/hyperv_features
 /x86_64/hyperv_svm_test
 /x86_64/max_vcpuid_cap_test
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index 0172eb6cb6ee..366345099363 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -85,6 +85,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/emulator_error_test
 TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
+TEST_GEN_PROGS_x86_64 += x86_64/hyperv_extended_hcalls
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_features
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_svm_test
 TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test
diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
new file mode 100644
index 000000000000..d378877235d4
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test Hyper-V extended hypercalls
+ *
+ * Copyright 2020 Google LLC
+ * Author: Vipin Sharma <vipinsh@google.com>
+ */
+
+#include "kvm_util.h"
+#include "processor.h"
+#include "hyperv.h"
+
+/* Any value is fine */
+#define EXT_CAPABILITIES 0xbull
+
+static void guest_code(vm_vaddr_t pgs_gpa, vm_vaddr_t output_pg_gva)
+{
+	uint64_t res, vector;
+	uint64_t *output_gva;
+
+	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
+	wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
+
+	output_gva = (uint64_t *)output_pg_gva;
+
+	vector = hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, pgs_gpa,
+			   pgs_gpa + 4096, &res);
+
+	GUEST_ASSERT_1(!vector, vector);
+	GUEST_ASSERT_2(res == HV_STATUS_SUCCESS, res, HV_STATUS_SUCCESS);
+
+	/* TLFS states output will be a uint64_t value */
+	GUEST_ASSERT_2(*output_gva == EXT_CAPABILITIES, *output_gva,
+		       EXT_CAPABILITIES);
+
+	GUEST_DONE();
+}
+
+static void guest_extended_hcall_test(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_run *run;
+	struct kvm_vm *vm;
+	struct ucall uc;
+	vm_vaddr_t hcall_page;
+	uint64_t *outval;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+	run = vcpu->run;
+	vcpu_enable_cap(vcpu, KVM_CAP_HYPERV_ENFORCE_CPUID, 1);
+	vcpu_set_hv_cpuid(vcpu);
+
+	/* Hypercall input/output */
+	hcall_page = vm_vaddr_alloc_pages(vm, 2);
+	memset(addr_gva2hva(vm, hcall_page), 0x0, 2 * getpagesize());
+	vcpu_args_set(vcpu, 2, addr_gva2gpa(vm, hcall_page), hcall_page + 4096);
+
+	vcpu_run(vcpu);
+
+	TEST_ASSERT((run->exit_reason == KVM_EXIT_HYPERV),
+		    "unexpected exit reason: %u (%s)", run->exit_reason,
+		    exit_reason_str(run->exit_reason));
+
+	outval = addr_gpa2hva(vm, run->hyperv.u.hcall.params[1]);
+	*outval = EXT_CAPABILITIES;
+	run->hyperv.u.hcall.result = HV_STATUS_SUCCESS;
+
+	vcpu_run(vcpu);
+
+	TEST_ASSERT((run->exit_reason == KVM_EXIT_IO),
+		    "unexpected exit reason: %u (%s)", run->exit_reason,
+		    exit_reason_str(run->exit_reason));
+
+	switch (get_ucall(vcpu, &uc)) {
+	case UCALL_ABORT:
+		REPORT_GUEST_ASSERT_2(uc, "arg1 = %ld, arg2 = %ld");
+		break;
+	case UCALL_DONE:
+		break;
+	default:
+		TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
+	}
+
+	kvm_vm_free(vm);
+}
+
+int main(void)
+{
+	guest_extended_hcall_test();
+}
-- 
2.38.1.273.g43a17bfeac-goog


^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement
  2022-11-05  4:57 ` [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement Vipin Sharma
@ 2022-11-07 18:27   ` David Matlack
  2022-11-08  1:46     ` Vipin Sharma
  0 siblings, 1 reply; 23+ messages in thread
From: David Matlack @ 2022-11-07 18:27 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Fri, Nov 04, 2022 at 09:57:01PM -0700, Vipin Sharma wrote:
> Test Extended hypercall CPUID enablement

nit: Use complete sentences and provide more details on what exactly
is being tested.

The rest of the commit messages look pretty good so I assume you just
forgot to fill this one in fully before sending to the mailing list :)

> 
> Signed-off-by: Vipin Sharma <vipinsh@google.com>
> ---
>  tools/testing/selftests/kvm/include/x86_64/hyperv.h  | 4 ++++
>  tools/testing/selftests/kvm/x86_64/hyperv_features.c | 9 +++++++++
>  2 files changed, 13 insertions(+)
> 
> diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> index b66910702c0a..075fd29071a6 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> @@ -110,6 +110,7 @@
>  #define HV_ACCESS_STATS				BIT(8)
>  #define HV_DEBUGGING				BIT(11)
>  #define HV_CPU_MANAGEMENT			BIT(12)
> +#define HV_ENABLE_EXTENDED_HYPERCALLS		BIT(20)
>  #define HV_ISOLATION				BIT(22)
>  
>  /* HYPERV_CPUID_FEATURES.EDX */
> @@ -164,6 +165,9 @@
>  #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
>  #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0
>  
> +/* Extended hypercalls */
> +#define HV_EXT_CALL_QUERY_CAPABILITIES		0x8001
> +
>  #define HV_FLUSH_ALL_PROCESSORS			BIT(0)
>  #define HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES	BIT(1)
>  #define HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY	BIT(2)
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> index 05b32e550a80..6b443ce456b6 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> @@ -602,6 +602,15 @@ static void guest_test_hcalls_access(void)
>  			hcall->expect = HV_STATUS_SUCCESS;
>  			break;
>  		case 19:
> +			hcall->control = HV_EXT_CALL_QUERY_CAPABILITIES;
> +			hcall->expect = HV_STATUS_ACCESS_DENIED;
> +			break;
> +		case 20:
> +			feat->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS;
> +			hcall->control = HV_EXT_CALL_QUERY_CAPABILITIES | HV_HYPERCALL_FAST_BIT;
> +			hcall->expect = HV_STATUS_INVALID_PARAMETER;
> +			break;
> +		case 21:
>  			kvm_vm_free(vm);
>  			return;
>  		}
> -- 
> 2.38.1.273.g43a17bfeac-goog
> 

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h
  2022-11-05  4:57 ` [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h Vipin Sharma
@ 2022-11-07 18:30   ` David Matlack
  2022-11-08  1:48     ` Vipin Sharma
  2022-11-09 13:46   ` Vitaly Kuznetsov
  1 sibling, 1 reply; 23+ messages in thread
From: David Matlack @ 2022-11-07 18:30 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Fri, Nov 04, 2022 at 09:57:03PM -0700, Vipin Sharma wrote:
> hypercall() can be used by other hyperv tests, move it to hyperv.h.
> 
> Signed-off-by: Vipin Sharma <vipinsh@google.com>
> ---
>  .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
>  .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
>  2 files changed, 17 insertions(+), 17 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> index 9d8c325af1d9..87d8d9e444f7 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> @@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
>  	       ((uint64_t)LINUX_VERSION_CODE << 16);
>  }
>  
> +static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> +				vm_vaddr_t output_address, uint64_t *hv_status)
> +{
> +	uint8_t vector;
> +
> +	/* Note both the hypercall and the "asm safe" clobber r9-r11. */
> +	asm volatile("mov %[output_address], %%r8\n\t"
> +		     KVM_ASM_SAFE("vmcall")
> +		     : "=a" (*hv_status),
> +		       "+c" (control), "+d" (input_address),
> +		       KVM_ASM_SAFE_OUTPUTS(vector)
> +		     : [output_address] "r"(output_address),
> +		       "a" (-EFAULT)
> +		     : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> +	return vector;
> +}

Since this function is Hyper-V specific it probably makes sense to
rename it to hyperv_hypercall() as part of moving it to library, e.g. to
differentiate it from kvm_hypercall().

> +
>  #endif /* !SELFTEST_KVM_HYPERV_H */
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> index b5a42cf1ad9d..31b22ee07dfb 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> @@ -13,23 +13,6 @@
>  #include "processor.h"
>  #include "hyperv.h"
>  
> -static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> -				vm_vaddr_t output_address, uint64_t *hv_status)
> -{
> -	uint8_t vector;
> -
> -	/* Note both the hypercall and the "asm safe" clobber r9-r11. */
> -	asm volatile("mov %[output_address], %%r8\n\t"
> -		     KVM_ASM_SAFE("vmcall")
> -		     : "=a" (*hv_status),
> -		       "+c" (control), "+d" (input_address),
> -		       KVM_ASM_SAFE_OUTPUTS(vector)
> -		     : [output_address] "r"(output_address),
> -		       "a" (-EFAULT)
> -		     : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> -	return vector;
> -}
> -
>  struct msr_data {
>  	uint32_t idx;
>  	bool available;
> -- 
> 2.38.1.273.g43a17bfeac-goog
> 

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace
  2022-11-05  4:57 ` [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace Vipin Sharma
@ 2022-11-07 19:01   ` David Matlack
  2022-11-08  2:04     ` Vipin Sharma
  0 siblings, 1 reply; 23+ messages in thread
From: David Matlack @ 2022-11-07 19:01 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Fri, Nov 04, 2022 at 09:57:04PM -0700, Vipin Sharma wrote:
> Hyper-V extended hypercalls by default exit to userspace. Verify
> userspace gets the call, update the result and then guest verifies
> result it received.
> 
> Signed-off-by: Vipin Sharma <vipinsh@google.com>
> ---
>  tools/testing/selftests/kvm/.gitignore        |  1 +
>  tools/testing/selftests/kvm/Makefile          |  1 +
>  .../kvm/x86_64/hyperv_extended_hcalls.c       | 90 +++++++++++++++++++
>  3 files changed, 92 insertions(+)
>  create mode 100644 tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
> 
> diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
> index 2f0d705db9db..ffe06dd1cc6e 100644
> --- a/tools/testing/selftests/kvm/.gitignore
> +++ b/tools/testing/selftests/kvm/.gitignore
> @@ -24,6 +24,7 @@
>  /x86_64/kvm_pv_test
>  /x86_64/hyperv_clock
>  /x86_64/hyperv_cpuid
> +/x86_64/hyperv_extended_hcalls

nit: Any reason not to name this hyperv_extended_hypercalls? It's not
too long and as a non-Hyper-V developer it's easier to read.

>  /x86_64/hyperv_features
>  /x86_64/hyperv_svm_test
>  /x86_64/max_vcpuid_cap_test
> diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
> index 0172eb6cb6ee..366345099363 100644
> --- a/tools/testing/selftests/kvm/Makefile
> +++ b/tools/testing/selftests/kvm/Makefile
> @@ -85,6 +85,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/emulator_error_test
>  TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test
>  TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
>  TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
> +TEST_GEN_PROGS_x86_64 += x86_64/hyperv_extended_hcalls
>  TEST_GEN_PROGS_x86_64 += x86_64/hyperv_features
>  TEST_GEN_PROGS_x86_64 += x86_64/hyperv_svm_test
>  TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
> new file mode 100644
> index 000000000000..d378877235d4
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
> @@ -0,0 +1,90 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Test Hyper-V extended hypercalls

It would probably be worth adding a note in this comment that the
negative tests for extended hypercalls live in hyperv_features.c, that
way someone doesn't accidentally go down the rabbit hole of adding
negative tests here in the future.

> + *
> + * Copyright 2020 Google LLC

2022 :)

> + * Author: Vipin Sharma <vipinsh@google.com>
> + */
> +
> +#include "kvm_util.h"
> +#include "processor.h"
> +#include "hyperv.h"
> +
> +/* Any value is fine */
> +#define EXT_CAPABILITIES 0xbull
> +
> +static void guest_code(vm_vaddr_t pgs_gpa, vm_vaddr_t output_pg_gva)
> +{
> +	uint64_t res, vector;
> +	uint64_t *output_gva;
> +
> +	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
> +	wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
> +
> +	output_gva = (uint64_t *)output_pg_gva;
> +
> +	vector = hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, pgs_gpa,
> +			   pgs_gpa + 4096, &res);
> +
> +	GUEST_ASSERT_1(!vector, vector);
> +	GUEST_ASSERT_2(res == HV_STATUS_SUCCESS, res, HV_STATUS_SUCCESS);

GUEST_ASSERT_EQ(res, HV_STATUS_SUCCESS);

> +
> +	/* TLFS states output will be a uint64_t value */
> +	GUEST_ASSERT_2(*output_gva == EXT_CAPABILITIES, *output_gva,
> +		       EXT_CAPABILITIES);

GUEST_ASSERT_EQ(*output_gva, EXT_CAPABILITIES);

> +
> +	GUEST_DONE();
> +}
> +
> +static void guest_extended_hcall_test(void)
> +{
> +	struct kvm_vcpu *vcpu;
> +	struct kvm_run *run;
> +	struct kvm_vm *vm;
> +	struct ucall uc;
> +	vm_vaddr_t hcall_page;
> +	uint64_t *outval;
> +
> +	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
> +	run = vcpu->run;
> +	vcpu_enable_cap(vcpu, KVM_CAP_HYPERV_ENFORCE_CPUID, 1);
> +	vcpu_set_hv_cpuid(vcpu);

Check if KVM offers HV_ENABLE_EXTENDED_HYPERCALLS in CPUID, and skip the
test if not.

> +
> +	/* Hypercall input/output */
> +	hcall_page = vm_vaddr_alloc_pages(vm, 2);
> +	memset(addr_gva2hva(vm, hcall_page), 0x0, 2 * getpagesize());

s/getpagesize()/vm->page_size/

> +	vcpu_args_set(vcpu, 2, addr_gva2gpa(vm, hcall_page), hcall_page + 4096);

s/4096/vm->page_size/

And to avoid hard-coding 4096 in guest_code(), you could pass in the GPA
of the ouput page as another argument.

> +
> +	vcpu_run(vcpu);
> +
> +	TEST_ASSERT((run->exit_reason == KVM_EXIT_HYPERV),
> +		    "unexpected exit reason: %u (%s)", run->exit_reason,
> +		    exit_reason_str(run->exit_reason));
> +
> +	outval = addr_gpa2hva(vm, run->hyperv.u.hcall.params[1]);
> +	*outval = EXT_CAPABILITIES;
> +	run->hyperv.u.hcall.result = HV_STATUS_SUCCESS;
> +
> +	vcpu_run(vcpu);
> +
> +	TEST_ASSERT((run->exit_reason == KVM_EXIT_IO),
> +		    "unexpected exit reason: %u (%s)", run->exit_reason,
> +		    exit_reason_str(run->exit_reason));

Optional: Asserting a specific exit reason is a pretty common pattern in
the x86 selftests. It'd be nice to create a common macro for it. e.g.

	ASSERT_EXIT_REASON(vcpu, KVM_EXIT_IO);

> +
> +	switch (get_ucall(vcpu, &uc)) {
> +	case UCALL_ABORT:
> +		REPORT_GUEST_ASSERT_2(uc, "arg1 = %ld, arg2 = %ld");
> +		break;
> +	case UCALL_DONE:
> +		break;
> +	default:
> +		TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
> +	}
> +
> +	kvm_vm_free(vm);
> +}
> +
> +int main(void)
> +{
> +	guest_extended_hcall_test();

Why not just put all this in main()?

> +}

return 0?

> -- 
> 2.38.1.273.g43a17bfeac-goog
> 

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common
  2022-11-05  4:57 ` [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common Vipin Sharma
@ 2022-11-07 19:08   ` David Matlack
  2022-11-08  1:45     ` Vipin Sharma
  2022-11-09 13:48   ` Vitaly Kuznetsov
  1 sibling, 1 reply; 23+ messages in thread
From: David Matlack @ 2022-11-07 19:08 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Fri, Nov 04, 2022 at 09:57:02PM -0700, Vipin Sharma wrote:
> Make guest OS ID calculation common to all hyperv tests and similar to
> hv_generate_guest_id().

This commit makes the HV_LINUX_VENDOR_ID and adds LINUX_VERSION_CODE
to existing tests. Can you split out the latter to a separate commit?
Also what's the reason to add LINUX_VERSION_CODE to the mix?

> 
> Signed-off-by: Vipin Sharma <vipinsh@google.com>
> ---
>  tools/testing/selftests/kvm/include/x86_64/hyperv.h  | 10 ++++++++++
>  tools/testing/selftests/kvm/x86_64/hyperv_clock.c    |  2 +-
>  tools/testing/selftests/kvm/x86_64/hyperv_features.c |  6 ++----
>  tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c |  2 +-
>  4 files changed, 14 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> index 075fd29071a6..9d8c325af1d9 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> @@ -9,6 +9,10 @@
>  #ifndef SELFTEST_KVM_HYPERV_H
>  #define SELFTEST_KVM_HYPERV_H
>  
> +#include <linux/version.h>
> +
> +#define HV_LINUX_VENDOR_ID			0x8100
> +
>  #define HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS	0x40000000
>  #define HYPERV_CPUID_INTERFACE			0x40000001
>  #define HYPERV_CPUID_VERSION			0x40000002
> @@ -189,4 +193,10 @@
>  /* hypercall options */
>  #define HV_HYPERCALL_FAST_BIT		BIT(16)
>  
> +static inline uint64_t hv_linux_guest_id(void)
> +{
> +	return ((uint64_t)HV_LINUX_VENDOR_ID << 48) |
> +	       ((uint64_t)LINUX_VERSION_CODE << 16);

This can be a compile-time constant (i.e. macro).

> +}
> +
>  #endif /* !SELFTEST_KVM_HYPERV_H */
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> index d576bc8ce823..f9112c5dc3f7 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> @@ -104,7 +104,7 @@ static void guest_main(struct ms_hyperv_tsc_page *tsc_page, vm_paddr_t tsc_page_
>  
>  	/* Set Guest OS id to enable Hyper-V emulation */
>  	GUEST_SYNC(1);
> -	wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
> +	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
>  	GUEST_SYNC(2);
>  
>  	check_tsc_msr_rdtsc();
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> index 6b443ce456b6..b5a42cf1ad9d 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> @@ -13,8 +13,6 @@
>  #include "processor.h"
>  #include "hyperv.h"
>  
> -#define LINUX_OS_ID ((u64)0x8100 << 48)
> -
>  static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
>  				vm_vaddr_t output_address, uint64_t *hv_status)
>  {
> @@ -71,7 +69,7 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
>  
>  	GUEST_ASSERT(hcall->control);
>  
> -	wrmsr(HV_X64_MSR_GUEST_OS_ID, LINUX_OS_ID);
> +	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
>  	wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
>  
>  	if (!(hcall->control & HV_HYPERCALL_FAST_BIT)) {
> @@ -169,7 +167,7 @@ static void guest_test_msrs_access(void)
>  			 */
>  			msr->idx = HV_X64_MSR_GUEST_OS_ID;
>  			msr->write = 1;
> -			msr->write_val = LINUX_OS_ID;
> +			msr->write_val = hv_linux_guest_id();
>  			msr->available = 1;
>  			break;
>  		case 3:
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> index a380ad7bb9b3..2c13a144b04c 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> @@ -69,7 +69,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm)
>  
>  	GUEST_SYNC(1);
>  
> -	wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
> +	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
>  
>  	GUEST_ASSERT(svm->vmcb_gpa);
>  	/* Prepare for L2 execution. */
> -- 
> 2.38.1.273.g43a17bfeac-goog
> 

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common
  2022-11-07 19:08   ` David Matlack
@ 2022-11-08  1:45     ` Vipin Sharma
  2022-11-08 17:56       ` David Matlack
  0 siblings, 1 reply; 23+ messages in thread
From: Vipin Sharma @ 2022-11-08  1:45 UTC (permalink / raw)
  To: David Matlack; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Mon, Nov 7, 2022 at 11:08 AM David Matlack <dmatlack@google.com> wrote:
>
> On Fri, Nov 04, 2022 at 09:57:02PM -0700, Vipin Sharma wrote:
> > Make guest OS ID calculation common to all hyperv tests and similar to
> > hv_generate_guest_id().
>
> This commit makes the HV_LINUX_VENDOR_ID and adds LINUX_VERSION_CODE
> to existing tests. Can you split out the latter to a separate commit?
> Also what's the reason to add LINUX_VERSION_CODE to the mix?
>

I looked at the implementation in selftest and what is in the
include/asm-generic/mshyperv.h for the hv_generate_guest_id()
function, both looked different. I thought it would be nice to have
consistent logic at both places.

I can remove LINUX_VERISON_CODE if you prefer.

> >
> > Signed-off-by: Vipin Sharma <vipinsh@google.com>
> > ---
> >  tools/testing/selftests/kvm/include/x86_64/hyperv.h  | 10 ++++++++++
> >  tools/testing/selftests/kvm/x86_64/hyperv_clock.c    |  2 +-
> >  tools/testing/selftests/kvm/x86_64/hyperv_features.c |  6 ++----
> >  tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c |  2 +-
> >  4 files changed, 14 insertions(+), 6 deletions(-)
> >
> > diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > index 075fd29071a6..9d8c325af1d9 100644
> > --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > @@ -9,6 +9,10 @@
> >  #ifndef SELFTEST_KVM_HYPERV_H
> >  #define SELFTEST_KVM_HYPERV_H
> >
> > +#include <linux/version.h>
> > +
> > +#define HV_LINUX_VENDOR_ID                   0x8100
> > +
> >  #define HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS        0x40000000
> >  #define HYPERV_CPUID_INTERFACE                       0x40000001
> >  #define HYPERV_CPUID_VERSION                 0x40000002
> > @@ -189,4 +193,10 @@
> >  /* hypercall options */
> >  #define HV_HYPERCALL_FAST_BIT                BIT(16)
> >
> > +static inline uint64_t hv_linux_guest_id(void)
> > +{
> > +     return ((uint64_t)HV_LINUX_VENDOR_ID << 48) |
> > +            ((uint64_t)LINUX_VERSION_CODE << 16);
>
> This can be a compile-time constant (i.e. macro).
>

Yes, I will make it a macro in the next version.

> > +}
> > +
> >  #endif /* !SELFTEST_KVM_HYPERV_H */
> > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> > index d576bc8ce823..f9112c5dc3f7 100644
> > --- a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> > @@ -104,7 +104,7 @@ static void guest_main(struct ms_hyperv_tsc_page *tsc_page, vm_paddr_t tsc_page_
> >
> >       /* Set Guest OS id to enable Hyper-V emulation */
> >       GUEST_SYNC(1);
> > -     wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
> > +     wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
> >       GUEST_SYNC(2);
> >
> >       check_tsc_msr_rdtsc();
> > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > index 6b443ce456b6..b5a42cf1ad9d 100644
> > --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > @@ -13,8 +13,6 @@
> >  #include "processor.h"
> >  #include "hyperv.h"
> >
> > -#define LINUX_OS_ID ((u64)0x8100 << 48)
> > -
> >  static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> >                               vm_vaddr_t output_address, uint64_t *hv_status)
> >  {
> > @@ -71,7 +69,7 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
> >
> >       GUEST_ASSERT(hcall->control);
> >
> > -     wrmsr(HV_X64_MSR_GUEST_OS_ID, LINUX_OS_ID);
> > +     wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
> >       wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
> >
> >       if (!(hcall->control & HV_HYPERCALL_FAST_BIT)) {
> > @@ -169,7 +167,7 @@ static void guest_test_msrs_access(void)
> >                        */
> >                       msr->idx = HV_X64_MSR_GUEST_OS_ID;
> >                       msr->write = 1;
> > -                     msr->write_val = LINUX_OS_ID;
> > +                     msr->write_val = hv_linux_guest_id();
> >                       msr->available = 1;
> >                       break;
> >               case 3:
> > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> > index a380ad7bb9b3..2c13a144b04c 100644
> > --- a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> > @@ -69,7 +69,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm)
> >
> >       GUEST_SYNC(1);
> >
> > -     wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
> > +     wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
> >
> >       GUEST_ASSERT(svm->vmcb_gpa);
> >       /* Prepare for L2 execution. */
> > --
> > 2.38.1.273.g43a17bfeac-goog
> >

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement
  2022-11-07 18:27   ` David Matlack
@ 2022-11-08  1:46     ` Vipin Sharma
  0 siblings, 0 replies; 23+ messages in thread
From: Vipin Sharma @ 2022-11-08  1:46 UTC (permalink / raw)
  To: David Matlack; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Mon, Nov 7, 2022 at 10:27 AM David Matlack <dmatlack@google.com> wrote:
>
> On Fri, Nov 04, 2022 at 09:57:01PM -0700, Vipin Sharma wrote:
> > Test Extended hypercall CPUID enablement
>
> nit: Use complete sentences and provide more details on what exactly
> is being tested.
>
> The rest of the commit messages look pretty good so I assume you just
> forgot to fill this one in fully before sending to the mailing list :)
>

Missed it, I will update it.

> >
> > Signed-off-by: Vipin Sharma <vipinsh@google.com>
> > ---
> >  tools/testing/selftests/kvm/include/x86_64/hyperv.h  | 4 ++++
> >  tools/testing/selftests/kvm/x86_64/hyperv_features.c | 9 +++++++++
> >  2 files changed, 13 insertions(+)
> >
> > diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > index b66910702c0a..075fd29071a6 100644
> > --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > @@ -110,6 +110,7 @@
> >  #define HV_ACCESS_STATS                              BIT(8)
> >  #define HV_DEBUGGING                         BIT(11)
> >  #define HV_CPU_MANAGEMENT                    BIT(12)
> > +#define HV_ENABLE_EXTENDED_HYPERCALLS                BIT(20)
> >  #define HV_ISOLATION                         BIT(22)
> >
> >  /* HYPERV_CPUID_FEATURES.EDX */
> > @@ -164,6 +165,9 @@
> >  #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
> >  #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0
> >
> > +/* Extended hypercalls */
> > +#define HV_EXT_CALL_QUERY_CAPABILITIES               0x8001
> > +
> >  #define HV_FLUSH_ALL_PROCESSORS                      BIT(0)
> >  #define HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES  BIT(1)
> >  #define HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY    BIT(2)
> > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > index 05b32e550a80..6b443ce456b6 100644
> > --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > @@ -602,6 +602,15 @@ static void guest_test_hcalls_access(void)
> >                       hcall->expect = HV_STATUS_SUCCESS;
> >                       break;
> >               case 19:
> > +                     hcall->control = HV_EXT_CALL_QUERY_CAPABILITIES;
> > +                     hcall->expect = HV_STATUS_ACCESS_DENIED;
> > +                     break;
> > +             case 20:
> > +                     feat->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS;
> > +                     hcall->control = HV_EXT_CALL_QUERY_CAPABILITIES | HV_HYPERCALL_FAST_BIT;
> > +                     hcall->expect = HV_STATUS_INVALID_PARAMETER;
> > +                     break;
> > +             case 21:
> >                       kvm_vm_free(vm);
> >                       return;
> >               }
> > --
> > 2.38.1.273.g43a17bfeac-goog
> >

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h
  2022-11-07 18:30   ` David Matlack
@ 2022-11-08  1:48     ` Vipin Sharma
  2022-11-08 17:40       ` David Matlack
  0 siblings, 1 reply; 23+ messages in thread
From: Vipin Sharma @ 2022-11-08  1:48 UTC (permalink / raw)
  To: David Matlack; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Mon, Nov 7, 2022 at 10:30 AM David Matlack <dmatlack@google.com> wrote:
>
> On Fri, Nov 04, 2022 at 09:57:03PM -0700, Vipin Sharma wrote:
> > hypercall() can be used by other hyperv tests, move it to hyperv.h.
> >
> > Signed-off-by: Vipin Sharma <vipinsh@google.com>
> > ---
> >  .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
> >  .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
> >  2 files changed, 17 insertions(+), 17 deletions(-)
> >
> > diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > index 9d8c325af1d9..87d8d9e444f7 100644
> > --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > @@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
> >              ((uint64_t)LINUX_VERSION_CODE << 16);
> >  }
> >
> > +static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> > +                             vm_vaddr_t output_address, uint64_t *hv_status)
> > +{
> > +     uint8_t vector;
> > +
> > +     /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> > +     asm volatile("mov %[output_address], %%r8\n\t"
> > +                  KVM_ASM_SAFE("vmcall")
> > +                  : "=a" (*hv_status),
> > +                    "+c" (control), "+d" (input_address),
> > +                    KVM_ASM_SAFE_OUTPUTS(vector)
> > +                  : [output_address] "r"(output_address),
> > +                    "a" (-EFAULT)
> > +                  : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> > +     return vector;
> > +}
>
> Since this function is Hyper-V specific it probably makes sense to
> rename it to hyperv_hypercall() as part of moving it to library, e.g. to
> differentiate it from kvm_hypercall().
>

Sounds good. Does it keeping it in header file "hyperv.h" seems fine
or should I create a new "hyperv.c" lib file and move function
definition there?

> > +
> >  #endif /* !SELFTEST_KVM_HYPERV_H */
> > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > index b5a42cf1ad9d..31b22ee07dfb 100644
> > --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > @@ -13,23 +13,6 @@
> >  #include "processor.h"
> >  #include "hyperv.h"
> >
> > -static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> > -                             vm_vaddr_t output_address, uint64_t *hv_status)
> > -{
> > -     uint8_t vector;
> > -
> > -     /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> > -     asm volatile("mov %[output_address], %%r8\n\t"
> > -                  KVM_ASM_SAFE("vmcall")
> > -                  : "=a" (*hv_status),
> > -                    "+c" (control), "+d" (input_address),
> > -                    KVM_ASM_SAFE_OUTPUTS(vector)
> > -                  : [output_address] "r"(output_address),
> > -                    "a" (-EFAULT)
> > -                  : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> > -     return vector;
> > -}
> > -
> >  struct msr_data {
> >       uint32_t idx;
> >       bool available;
> > --
> > 2.38.1.273.g43a17bfeac-goog
> >

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace
  2022-11-07 19:01   ` David Matlack
@ 2022-11-08  2:04     ` Vipin Sharma
  2022-11-08 17:44       ` David Matlack
  0 siblings, 1 reply; 23+ messages in thread
From: Vipin Sharma @ 2022-11-08  2:04 UTC (permalink / raw)
  To: David Matlack; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Mon, Nov 7, 2022 at 11:01 AM David Matlack <dmatlack@google.com> wrote:
>
> On Fri, Nov 04, 2022 at 09:57:04PM -0700, Vipin Sharma wrote:
> > Hyper-V extended hypercalls by default exit to userspace. Verify
> > userspace gets the call, update the result and then guest verifies
> > result it received.
> >
> > Signed-off-by: Vipin Sharma <vipinsh@google.com>
> > ---
> >  tools/testing/selftests/kvm/.gitignore        |  1 +
> >  tools/testing/selftests/kvm/Makefile          |  1 +
> >  .../kvm/x86_64/hyperv_extended_hcalls.c       | 90 +++++++++++++++++++
> >  3 files changed, 92 insertions(+)
> >  create mode 100644 tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
> >
> > diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
> > index 2f0d705db9db..ffe06dd1cc6e 100644
> > --- a/tools/testing/selftests/kvm/.gitignore
> > +++ b/tools/testing/selftests/kvm/.gitignore
> > @@ -24,6 +24,7 @@
> >  /x86_64/kvm_pv_test
> >  /x86_64/hyperv_clock
> >  /x86_64/hyperv_cpuid
> > +/x86_64/hyperv_extended_hcalls
>
> nit: Any reason not to name this hyperv_extended_hypercalls? It's not
> too long and as a non-Hyper-V developer it's easier to read.
>

I was keeping it consistent with other names like
KVM_EXIT_HYPERV_HCALL, and struct hcall{} in struct kvm_hyperv_exit{}.

I am fine with renaming it to hyperv_extended_hypercalls.

> >  /x86_64/hyperv_features
> >  /x86_64/hyperv_svm_test
> >  /x86_64/max_vcpuid_cap_test
> > diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
> > index 0172eb6cb6ee..366345099363 100644
> > --- a/tools/testing/selftests/kvm/Makefile
> > +++ b/tools/testing/selftests/kvm/Makefile
> > @@ -85,6 +85,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/emulator_error_test
> >  TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test
> >  TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
> >  TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
> > +TEST_GEN_PROGS_x86_64 += x86_64/hyperv_extended_hcalls
> >  TEST_GEN_PROGS_x86_64 += x86_64/hyperv_features
> >  TEST_GEN_PROGS_x86_64 += x86_64/hyperv_svm_test
> >  TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test
> > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
> > new file mode 100644
> > index 000000000000..d378877235d4
> > --- /dev/null
> > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c
> > @@ -0,0 +1,90 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/*
> > + * Test Hyper-V extended hypercalls
>
> It would probably be worth adding a note in this comment that the
> negative tests for extended hypercalls live in hyperv_features.c, that
> way someone doesn't accidentally go down the rabbit hole of adding
> negative tests here in the future.
>

Sure.

> > + *
> > + * Copyright 2020 Google LLC
>
> 2022 :)
>
> > + * Author: Vipin Sharma <vipinsh@google.com>
> > + */
> > +
> > +#include "kvm_util.h"
> > +#include "processor.h"
> > +#include "hyperv.h"
> > +
> > +/* Any value is fine */
> > +#define EXT_CAPABILITIES 0xbull
> > +
> > +static void guest_code(vm_vaddr_t pgs_gpa, vm_vaddr_t output_pg_gva)
> > +{
> > +     uint64_t res, vector;
> > +     uint64_t *output_gva;
> > +
> > +     wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
> > +     wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
> > +
> > +     output_gva = (uint64_t *)output_pg_gva;
> > +
> > +     vector = hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, pgs_gpa,
> > +                        pgs_gpa + 4096, &res);
> > +
> > +     GUEST_ASSERT_1(!vector, vector);
> > +     GUEST_ASSERT_2(res == HV_STATUS_SUCCESS, res, HV_STATUS_SUCCESS);
>
> GUEST_ASSERT_EQ(res, HV_STATUS_SUCCESS);
>

Copied things from hyperv_features test. This looks better, I will change it.

> > +
> > +     /* TLFS states output will be a uint64_t value */
> > +     GUEST_ASSERT_2(*output_gva == EXT_CAPABILITIES, *output_gva,
> > +                    EXT_CAPABILITIES);
>
> GUEST_ASSERT_EQ(*output_gva, EXT_CAPABILITIES);
>
> > +
> > +     GUEST_DONE();
> > +}
> > +
> > +static void guest_extended_hcall_test(void)
> > +{
> > +     struct kvm_vcpu *vcpu;
> > +     struct kvm_run *run;
> > +     struct kvm_vm *vm;
> > +     struct ucall uc;
> > +     vm_vaddr_t hcall_page;
> > +     uint64_t *outval;
> > +
> > +     vm = vm_create_with_one_vcpu(&vcpu, guest_code);
> > +     run = vcpu->run;
> > +     vcpu_enable_cap(vcpu, KVM_CAP_HYPERV_ENFORCE_CPUID, 1);
> > +     vcpu_set_hv_cpuid(vcpu);
>
> Check if KVM offers HV_ENABLE_EXTENDED_HYPERCALLS in CPUID, and skip the
> test if not.
>

Sure.

> > +
> > +     /* Hypercall input/output */
> > +     hcall_page = vm_vaddr_alloc_pages(vm, 2);
> > +     memset(addr_gva2hva(vm, hcall_page), 0x0, 2 * getpagesize());
>
> s/getpagesize()/vm->page_size/
>
> > +     vcpu_args_set(vcpu, 2, addr_gva2gpa(vm, hcall_page), hcall_page + 4096);
>
> s/4096/vm->page_size/
>
> And to avoid hard-coding 4096 in guest_code(), you could pass in the GPA
> of the ouput page as another argument.
>

Sounds good.

> > +
> > +     vcpu_run(vcpu);
> > +
> > +     TEST_ASSERT((run->exit_reason == KVM_EXIT_HYPERV),
> > +                 "unexpected exit reason: %u (%s)", run->exit_reason,
> > +                 exit_reason_str(run->exit_reason));
> > +
> > +     outval = addr_gpa2hva(vm, run->hyperv.u.hcall.params[1]);
> > +     *outval = EXT_CAPABILITIES;
> > +     run->hyperv.u.hcall.result = HV_STATUS_SUCCESS;
> > +
> > +     vcpu_run(vcpu);
> > +
> > +     TEST_ASSERT((run->exit_reason == KVM_EXIT_IO),
> > +                 "unexpected exit reason: %u (%s)", run->exit_reason,
> > +                 exit_reason_str(run->exit_reason));
>
> Optional: Asserting a specific exit reason is a pretty common pattern in
> the x86 selftests. It'd be nice to create a common macro for it. e.g.
>
>         ASSERT_EXIT_REASON(vcpu, KVM_EXIT_IO);
>

This is much better. I can add a patch which creates this API.

Should it be run or vcpu? Seems like everything needed is in struct kvm_run{}.

> > +
> > +     switch (get_ucall(vcpu, &uc)) {
> > +     case UCALL_ABORT:
> > +             REPORT_GUEST_ASSERT_2(uc, "arg1 = %ld, arg2 = %ld");
> > +             break;
> > +     case UCALL_DONE:
> > +             break;
> > +     default:
> > +             TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
> > +     }
> > +
> > +     kvm_vm_free(vm);
> > +}
> > +
> > +int main(void)
> > +{
> > +     guest_extended_hcall_test();
>
> Why not just put all this in main()?
>

I will.

> > +}
>
> return 0?
>
> > --
> > 2.38.1.273.g43a17bfeac-goog
> >

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h
  2022-11-08  1:48     ` Vipin Sharma
@ 2022-11-08 17:40       ` David Matlack
  0 siblings, 0 replies; 23+ messages in thread
From: David Matlack @ 2022-11-08 17:40 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Mon, Nov 7, 2022 at 5:49 PM Vipin Sharma <vipinsh@google.com> wrote:
>
> On Mon, Nov 7, 2022 at 10:30 AM David Matlack <dmatlack@google.com> wrote:
> >
> > On Fri, Nov 04, 2022 at 09:57:03PM -0700, Vipin Sharma wrote:
> > > hypercall() can be used by other hyperv tests, move it to hyperv.h.
> > >
> > > Signed-off-by: Vipin Sharma <vipinsh@google.com>
> > > ---
> > >  .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
> > >  .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
> > >  2 files changed, 17 insertions(+), 17 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > > index 9d8c325af1d9..87d8d9e444f7 100644
> > > --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > > +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > > @@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
> > >              ((uint64_t)LINUX_VERSION_CODE << 16);
> > >  }
> > >
> > > +static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> > > +                             vm_vaddr_t output_address, uint64_t *hv_status)
> > > +{
> > > +     uint8_t vector;
> > > +
> > > +     /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> > > +     asm volatile("mov %[output_address], %%r8\n\t"
> > > +                  KVM_ASM_SAFE("vmcall")
> > > +                  : "=a" (*hv_status),
> > > +                    "+c" (control), "+d" (input_address),
> > > +                    KVM_ASM_SAFE_OUTPUTS(vector)
> > > +                  : [output_address] "r"(output_address),
> > > +                    "a" (-EFAULT)
> > > +                  : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> > > +     return vector;
> > > +}
> >
> > Since this function is Hyper-V specific it probably makes sense to
> > rename it to hyperv_hypercall() as part of moving it to library, e.g. to
> > differentiate it from kvm_hypercall().
> >
>
> Sounds good. Does it keeping it in header file "hyperv.h" seems fine
> or should I create a new "hyperv.c" lib file and move function
> definition there?

I think it's fine to keep in hyperv.h. It seems like the type of
function we'd want to be inlined anyway, and the implementation is
short.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace
  2022-11-08  2:04     ` Vipin Sharma
@ 2022-11-08 17:44       ` David Matlack
  0 siblings, 0 replies; 23+ messages in thread
From: David Matlack @ 2022-11-08 17:44 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Mon, Nov 7, 2022 at 6:05 PM Vipin Sharma <vipinsh@google.com> wrote:
>
> On Mon, Nov 7, 2022 at 11:01 AM David Matlack <dmatlack@google.com> wrote:
> >
> > On Fri, Nov 04, 2022 at 09:57:04PM -0700, Vipin Sharma wrote:
> > > +
> > > +     TEST_ASSERT((run->exit_reason == KVM_EXIT_IO),
> > > +                 "unexpected exit reason: %u (%s)", run->exit_reason,
> > > +                 exit_reason_str(run->exit_reason));
> >
> > Optional: Asserting a specific exit reason is a pretty common pattern in
> > the x86 selftests. It'd be nice to create a common macro for it. e.g.
> >
> >         ASSERT_EXIT_REASON(vcpu, KVM_EXIT_IO);
> >
>
> This is much better. I can add a patch which creates this API.
>
> Should it be run or vcpu? Seems like everything needed is in struct kvm_run{}.

Either one but I suspect vcpu will produce a cleaner final result
since all tests pass around struct kvm_vcpu and only use kvm_run to
check the exit reason. i.e. I suspect you'll be able to delete several
struct kvm_run local variables as part of adding ASSERT_EXIT_REASON().

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common
  2022-11-08  1:45     ` Vipin Sharma
@ 2022-11-08 17:56       ` David Matlack
  0 siblings, 0 replies; 23+ messages in thread
From: David Matlack @ 2022-11-08 17:56 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: seanjc, pbonzini, vkuznets, kvm, linux-kernel

On Mon, Nov 7, 2022 at 5:45 PM Vipin Sharma <vipinsh@google.com> wrote:
>
> On Mon, Nov 7, 2022 at 11:08 AM David Matlack <dmatlack@google.com> wrote:
> >
> > On Fri, Nov 04, 2022 at 09:57:02PM -0700, Vipin Sharma wrote:
> > > Make guest OS ID calculation common to all hyperv tests and similar to
> > > hv_generate_guest_id().
> >
> > This commit makes the HV_LINUX_VENDOR_ID and adds LINUX_VERSION_CODE
> > to existing tests. Can you split out the latter to a separate commit?
> > Also what's the reason to add LINUX_VERSION_CODE to the mix?
> >
>
> I looked at the implementation in selftest and what is in the
> include/asm-generic/mshyperv.h for the hv_generate_guest_id()
> function, both looked different. I thought it would be nice to have
> consistent logic at both places.
>
> I can remove LINUX_VERISON_CODE if you prefer.

Using LINUX_VERSION_CODE here assumes the selftest will run on the
same kernel that it was compiled with. This might not be the case in
practice, e.g. if anyone runs the latest upstream selftests against
their internal kernel (something we've discussed doing recently).

The right way to incorporate the Linux version code would be for the
selftest to query it from the host dynamically and use that (at which
point hv_linux_guest_id() would actually have to be a function).

But since you don't strictly need it, it's probably best to just omit
it for the time being.



>
> > >
> > > Signed-off-by: Vipin Sharma <vipinsh@google.com>
> > > ---
> > >  tools/testing/selftests/kvm/include/x86_64/hyperv.h  | 10 ++++++++++
> > >  tools/testing/selftests/kvm/x86_64/hyperv_clock.c    |  2 +-
> > >  tools/testing/selftests/kvm/x86_64/hyperv_features.c |  6 ++----
> > >  tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c |  2 +-
> > >  4 files changed, 14 insertions(+), 6 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > > index 075fd29071a6..9d8c325af1d9 100644
> > > --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > > +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > > @@ -9,6 +9,10 @@
> > >  #ifndef SELFTEST_KVM_HYPERV_H
> > >  #define SELFTEST_KVM_HYPERV_H
> > >
> > > +#include <linux/version.h>
> > > +
> > > +#define HV_LINUX_VENDOR_ID                   0x8100
> > > +
> > >  #define HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS        0x40000000
> > >  #define HYPERV_CPUID_INTERFACE                       0x40000001
> > >  #define HYPERV_CPUID_VERSION                 0x40000002
> > > @@ -189,4 +193,10 @@
> > >  /* hypercall options */
> > >  #define HV_HYPERCALL_FAST_BIT                BIT(16)
> > >
> > > +static inline uint64_t hv_linux_guest_id(void)
> > > +{
> > > +     return ((uint64_t)HV_LINUX_VENDOR_ID << 48) |
> > > +            ((uint64_t)LINUX_VERSION_CODE << 16);
> >
> > This can be a compile-time constant (i.e. macro).
> >
>
> Yes, I will make it a macro in the next version.
>
> > > +}
> > > +
> > >  #endif /* !SELFTEST_KVM_HYPERV_H */
> > > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> > > index d576bc8ce823..f9112c5dc3f7 100644
> > > --- a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> > > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> > > @@ -104,7 +104,7 @@ static void guest_main(struct ms_hyperv_tsc_page *tsc_page, vm_paddr_t tsc_page_
> > >
> > >       /* Set Guest OS id to enable Hyper-V emulation */
> > >       GUEST_SYNC(1);
> > > -     wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
> > > +     wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
> > >       GUEST_SYNC(2);
> > >
> > >       check_tsc_msr_rdtsc();
> > > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > > index 6b443ce456b6..b5a42cf1ad9d 100644
> > > --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > > @@ -13,8 +13,6 @@
> > >  #include "processor.h"
> > >  #include "hyperv.h"
> > >
> > > -#define LINUX_OS_ID ((u64)0x8100 << 48)
> > > -
> > >  static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> > >                               vm_vaddr_t output_address, uint64_t *hv_status)
> > >  {
> > > @@ -71,7 +69,7 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
> > >
> > >       GUEST_ASSERT(hcall->control);
> > >
> > > -     wrmsr(HV_X64_MSR_GUEST_OS_ID, LINUX_OS_ID);
> > > +     wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
> > >       wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
> > >
> > >       if (!(hcall->control & HV_HYPERCALL_FAST_BIT)) {
> > > @@ -169,7 +167,7 @@ static void guest_test_msrs_access(void)
> > >                        */
> > >                       msr->idx = HV_X64_MSR_GUEST_OS_ID;
> > >                       msr->write = 1;
> > > -                     msr->write_val = LINUX_OS_ID;
> > > +                     msr->write_val = hv_linux_guest_id();
> > >                       msr->available = 1;
> > >                       break;
> > >               case 3:
> > > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> > > index a380ad7bb9b3..2c13a144b04c 100644
> > > --- a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> > > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> > > @@ -69,7 +69,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm)
> > >
> > >       GUEST_SYNC(1);
> > >
> > > -     wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
> > > +     wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
> > >
> > >       GUEST_ASSERT(svm->vmcb_gpa);
> > >       /* Prepare for L2 execution. */
> > > --
> > > 2.38.1.273.g43a17bfeac-goog
> > >

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h
  2022-11-05  4:57 ` [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h Vipin Sharma
  2022-11-07 18:30   ` David Matlack
@ 2022-11-09 13:46   ` Vitaly Kuznetsov
  1 sibling, 0 replies; 23+ messages in thread
From: Vitaly Kuznetsov @ 2022-11-09 13:46 UTC (permalink / raw)
  To: Vipin Sharma, seanjc, pbonzini; +Cc: dmatlack, kvm, linux-kernel, Vipin Sharma

Vipin Sharma <vipinsh@google.com> writes:

> hypercall() can be used by other hyperv tests, move it to hyperv.h.
>

A similar patch is pending in my TLB flush series:

https://lore.kernel.org/kvm/20221101145426.251680-33-vkuznets@redhat.com/

> Signed-off-by: Vipin Sharma <vipinsh@google.com>
> ---
>  .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
>  .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
>  2 files changed, 17 insertions(+), 17 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> index 9d8c325af1d9..87d8d9e444f7 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> @@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
>  	       ((uint64_t)LINUX_VERSION_CODE << 16);
>  }
>  
> +static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> +				vm_vaddr_t output_address, uint64_t *hv_status)
> +{
> +	uint8_t vector;
> +
> +	/* Note both the hypercall and the "asm safe" clobber r9-r11. */
> +	asm volatile("mov %[output_address], %%r8\n\t"
> +		     KVM_ASM_SAFE("vmcall")
> +		     : "=a" (*hv_status),
> +		       "+c" (control), "+d" (input_address),
> +		       KVM_ASM_SAFE_OUTPUTS(vector)
> +		     : [output_address] "r"(output_address),
> +		       "a" (-EFAULT)
> +		     : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> +	return vector;
> +}
> +
>  #endif /* !SELFTEST_KVM_HYPERV_H */
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> index b5a42cf1ad9d..31b22ee07dfb 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> @@ -13,23 +13,6 @@
>  #include "processor.h"
>  #include "hyperv.h"
>  
> -static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> -				vm_vaddr_t output_address, uint64_t *hv_status)
> -{
> -	uint8_t vector;
> -
> -	/* Note both the hypercall and the "asm safe" clobber r9-r11. */
> -	asm volatile("mov %[output_address], %%r8\n\t"
> -		     KVM_ASM_SAFE("vmcall")
> -		     : "=a" (*hv_status),
> -		       "+c" (control), "+d" (input_address),
> -		       KVM_ASM_SAFE_OUTPUTS(vector)
> -		     : [output_address] "r"(output_address),
> -		       "a" (-EFAULT)
> -		     : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> -	return vector;
> -}
> -
>  struct msr_data {
>  	uint32_t idx;
>  	bool available;

-- 
Vitaly


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common
  2022-11-05  4:57 ` [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common Vipin Sharma
  2022-11-07 19:08   ` David Matlack
@ 2022-11-09 13:48   ` Vitaly Kuznetsov
  2022-11-09 18:52     ` Vipin Sharma
  1 sibling, 1 reply; 23+ messages in thread
From: Vitaly Kuznetsov @ 2022-11-09 13:48 UTC (permalink / raw)
  To: Vipin Sharma, seanjc, pbonzini; +Cc: dmatlack, kvm, linux-kernel, Vipin Sharma

Vipin Sharma <vipinsh@google.com> writes:

> Make guest OS ID calculation common to all hyperv tests and similar to
> hv_generate_guest_id().

A similar (but without hv_linux_guest_id()) patch is present in my
Hyper-V TLB flush update:

https://lore.kernel.org/kvm/20221101145426.251680-32-vkuznets@redhat.com/

>
> Signed-off-by: Vipin Sharma <vipinsh@google.com>
> ---
>  tools/testing/selftests/kvm/include/x86_64/hyperv.h  | 10 ++++++++++
>  tools/testing/selftests/kvm/x86_64/hyperv_clock.c    |  2 +-
>  tools/testing/selftests/kvm/x86_64/hyperv_features.c |  6 ++----
>  tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c |  2 +-
>  4 files changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> index 075fd29071a6..9d8c325af1d9 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> @@ -9,6 +9,10 @@
>  #ifndef SELFTEST_KVM_HYPERV_H
>  #define SELFTEST_KVM_HYPERV_H
>  
> +#include <linux/version.h>
> +
> +#define HV_LINUX_VENDOR_ID			0x8100
> +
>  #define HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS	0x40000000
>  #define HYPERV_CPUID_INTERFACE			0x40000001
>  #define HYPERV_CPUID_VERSION			0x40000002
> @@ -189,4 +193,10 @@
>  /* hypercall options */
>  #define HV_HYPERCALL_FAST_BIT		BIT(16)
>  
> +static inline uint64_t hv_linux_guest_id(void)
> +{
> +	return ((uint64_t)HV_LINUX_VENDOR_ID << 48) |
> +	       ((uint64_t)LINUX_VERSION_CODE << 16);
> +}
> +
>  #endif /* !SELFTEST_KVM_HYPERV_H */
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> index d576bc8ce823..f9112c5dc3f7 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c
> @@ -104,7 +104,7 @@ static void guest_main(struct ms_hyperv_tsc_page *tsc_page, vm_paddr_t tsc_page_
>  
>  	/* Set Guest OS id to enable Hyper-V emulation */
>  	GUEST_SYNC(1);
> -	wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
> +	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
>  	GUEST_SYNC(2);
>  
>  	check_tsc_msr_rdtsc();
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> index 6b443ce456b6..b5a42cf1ad9d 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> @@ -13,8 +13,6 @@
>  #include "processor.h"
>  #include "hyperv.h"
>  
> -#define LINUX_OS_ID ((u64)0x8100 << 48)
> -
>  static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
>  				vm_vaddr_t output_address, uint64_t *hv_status)
>  {
> @@ -71,7 +69,7 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall)
>  
>  	GUEST_ASSERT(hcall->control);
>  
> -	wrmsr(HV_X64_MSR_GUEST_OS_ID, LINUX_OS_ID);
> +	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
>  	wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa);
>  
>  	if (!(hcall->control & HV_HYPERCALL_FAST_BIT)) {
> @@ -169,7 +167,7 @@ static void guest_test_msrs_access(void)
>  			 */
>  			msr->idx = HV_X64_MSR_GUEST_OS_ID;
>  			msr->write = 1;
> -			msr->write_val = LINUX_OS_ID;
> +			msr->write_val = hv_linux_guest_id();
>  			msr->available = 1;
>  			break;
>  		case 3:
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> index a380ad7bb9b3..2c13a144b04c 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c
> @@ -69,7 +69,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm)
>  
>  	GUEST_SYNC(1);
>  
> -	wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48);
> +	wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id());
>  
>  	GUEST_ASSERT(svm->vmcb_gpa);
>  	/* Prepare for L2 execution. */

-- 
Vitaly


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common
  2022-11-09 13:48   ` Vitaly Kuznetsov
@ 2022-11-09 18:52     ` Vipin Sharma
  2022-11-09 20:18       ` Sean Christopherson
  2022-11-10 10:02       ` Vitaly Kuznetsov
  0 siblings, 2 replies; 23+ messages in thread
From: Vipin Sharma @ 2022-11-09 18:52 UTC (permalink / raw)
  To: Vitaly Kuznetsov, seanjc, pbonzini; +Cc: dmatlack, kvm, linux-kernel

On Wed, Nov 9, 2022 at 5:48 AM Vitaly Kuznetsov <vkuznets@redhat.com> wrote:
>
> Vipin Sharma <vipinsh@google.com> writes:
>
> > Make guest OS ID calculation common to all hyperv tests and similar to
> > hv_generate_guest_id().
>
> A similar (but without hv_linux_guest_id()) patch is present in my
> Hyper-V TLB flush update:
>
> https://lore.kernel.org/kvm/20221101145426.251680-32-vkuznets@redhat.com/
>

After getting feedback from David, I decided to remove
LINUX_VERSION_CODE in v2. Our patches are functionally identical now.

@Sean, Paolo, Vitaly
Should I be rebasing my v2 on top of TLB flush patch series and remove
patch 4 and 5 from my series? I am not sure how these situations are
handled.

@Vitaly
Are you planning to send v14?

If yes, then for v13 Patch 31 (KVM: selftests: Move HYPERV_LINUX_OS_ID
definition to a common header) will you keep it same or will you
modify it to add  HYPERV_LINUX_OS_ID  in hyperv_clock.c and
hyperv_svm_test.c?

If not, then I can add a patch in my series to change those two files
if I end up rebasing on top of your series.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common
  2022-11-09 18:52     ` Vipin Sharma
@ 2022-11-09 20:18       ` Sean Christopherson
  2022-11-10 10:02       ` Vitaly Kuznetsov
  1 sibling, 0 replies; 23+ messages in thread
From: Sean Christopherson @ 2022-11-09 20:18 UTC (permalink / raw)
  To: Vipin Sharma; +Cc: Vitaly Kuznetsov, pbonzini, dmatlack, kvm, linux-kernel

On Wed, Nov 09, 2022, Vipin Sharma wrote:
> On Wed, Nov 9, 2022 at 5:48 AM Vitaly Kuznetsov <vkuznets@redhat.com> wrote:
> >
> > Vipin Sharma <vipinsh@google.com> writes:
> >
> > > Make guest OS ID calculation common to all hyperv tests and similar to
> > > hv_generate_guest_id().
> >
> > A similar (but without hv_linux_guest_id()) patch is present in my
> > Hyper-V TLB flush update:
> >
> > https://lore.kernel.org/kvm/20221101145426.251680-32-vkuznets@redhat.com/
> >
> 
> After getting feedback from David, I decided to remove
> LINUX_VERSION_CODE in v2. Our patches are functionally identical now.
> 
> @Sean, Paolo, Vitaly
> Should I be rebasing my v2 on top of TLB flush patch series and remove
> patch 4 and 5 from my series? I am not sure how these situations are
> handled.

In this case, unless Paolo is NOT planning on merging Vitaly's series for 6.2, I
would just wait for Vitaly's series to get pushed to kvm/queue.  I'm banking on
Paolo going on a queueing spree soon ;-)

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common
  2022-11-09 18:52     ` Vipin Sharma
  2022-11-09 20:18       ` Sean Christopherson
@ 2022-11-10 10:02       ` Vitaly Kuznetsov
  1 sibling, 0 replies; 23+ messages in thread
From: Vitaly Kuznetsov @ 2022-11-10 10:02 UTC (permalink / raw)
  To: Vipin Sharma, seanjc, pbonzini; +Cc: dmatlack, kvm, linux-kernel

Vipin Sharma <vipinsh@google.com> writes:

> On Wed, Nov 9, 2022 at 5:48 AM Vitaly Kuznetsov <vkuznets@redhat.com> wrote:
>>
>> Vipin Sharma <vipinsh@google.com> writes:
>>
>> > Make guest OS ID calculation common to all hyperv tests and similar to
>> > hv_generate_guest_id().
>>
>> A similar (but without hv_linux_guest_id()) patch is present in my
>> Hyper-V TLB flush update:
>>
>> https://lore.kernel.org/kvm/20221101145426.251680-32-vkuznets@redhat.com/
>>
>
> After getting feedback from David, I decided to remove
> LINUX_VERSION_CODE in v2. Our patches are functionally identical now.
>
> @Sean, Paolo, Vitaly
> Should I be rebasing my v2 on top of TLB flush patch series and remove
> patch 4 and 5 from my series? I am not sure how these situations are
> handled.
>
> @Vitaly
> Are you planning to send v14?
>
> If yes, then for v13 Patch 31 (KVM: selftests: Move HYPERV_LINUX_OS_ID
> definition to a common header) will you keep it same or will you
> modify it to add  HYPERV_LINUX_OS_ID  in hyperv_clock.c and
> hyperv_svm_test.c?
>
> If not, then I can add a patch in my series to change those two files
> if I end up rebasing on top of your series.
>

Rumor has it that v13 is going to be merged to kvm/queue soon so I have
no plans for v14 at this point. Fingers crossed)

-- 
Vitaly


^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2022-11-10 10:03 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-05  4:56 [PATCH 0/6] Add Hyper-v extended hypercall support in KVM Vipin Sharma
2022-11-05  4:56 ` [PATCH 1/6] KVM: x86: hyper-v: Use common code for hypercall userspace exit Vipin Sharma
2022-11-05  4:57 ` [PATCH 2/6] KVM: x86: hyper-v: Add extended hypercall support in Hyper-v Vipin Sharma
2022-11-05  4:57 ` [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement Vipin Sharma
2022-11-07 18:27   ` David Matlack
2022-11-08  1:46     ` Vipin Sharma
2022-11-05  4:57 ` [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common Vipin Sharma
2022-11-07 19:08   ` David Matlack
2022-11-08  1:45     ` Vipin Sharma
2022-11-08 17:56       ` David Matlack
2022-11-09 13:48   ` Vitaly Kuznetsov
2022-11-09 18:52     ` Vipin Sharma
2022-11-09 20:18       ` Sean Christopherson
2022-11-10 10:02       ` Vitaly Kuznetsov
2022-11-05  4:57 ` [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h Vipin Sharma
2022-11-07 18:30   ` David Matlack
2022-11-08  1:48     ` Vipin Sharma
2022-11-08 17:40       ` David Matlack
2022-11-09 13:46   ` Vitaly Kuznetsov
2022-11-05  4:57 ` [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace Vipin Sharma
2022-11-07 19:01   ` David Matlack
2022-11-08  2:04     ` Vipin Sharma
2022-11-08 17:44       ` David Matlack

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.