Skip to contentSkip to conversation

Add Hyper-v extended hypercall support in KVM

PATCH Superseded Vipin Sharma 4 participants 23 messages Nov 5, 2022 – Nov 10, 2022

Subsystems (5)
x86 KVM Kselftest KVM x86 +1 more subsystems

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Add Hyper-v extended hypercall support in KVM

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/[email protected]/

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
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#1 lore raw

PATCH 1/6 KVM: x86: hyper-v: Use common code for hypercall userspace exit

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

---
 arch/x86/kvm/hyperv.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)
arch/x86/kvm/hyperv.c +11 -16
index 0adf4a437e85..db2f96a0ae23 100644
@@ -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)
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#2 lore raw

PATCH 2/6 KVM: x86: hyper-v: Add extended hypercall support in Hyper-v

In reply to 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.

---
 arch/x86/kvm/hyperv.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/x86/kvm/hyperv.c +16 -0
index db2f96a0ae23..85c6100f4451 100644
@@ -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;
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#3 lore raw

PATCH 3/6 KVM: selftests: Test Hyper-V extended hypercall enablement

In reply to Vipin Sharma

Test Extended hypercall CPUID enablement

---
 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(+)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
index b66910702c0a..075fd29071a6 100644
@@ -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)
tools/testing/selftests/kvm/x86_64/hyperv_features.c +9 -0
index 05b32e550a80..6b443ce456b6 100644
@@ -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;
 		}
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#4 lore raw

Re: [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement

Quoted 1 line · Vipin Sharma

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 :)

Quoted 52 lines
---
 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(+)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
index b66910702c0a..075fd29071a6 100644
@@ -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)
tools/testing/selftests/kvm/x86_64/hyperv_features.c +9 -0
index 05b32e550a80..6b443ce456b6 100644
@@ -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;
 		}
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#5 lore raw

Re: [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement

Quoted 10 lines · David Matlack
Quoted 1 line · Vipin Sharma

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.

Quoted 52 lines
Quoted 52 lines
---
 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(+)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
index b66910702c0a..075fd29071a6 100644
@@ -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)
tools/testing/selftests/kvm/x86_64/hyperv_features.c +9 -1
index 05b32e550a80..6b443ce456b6 100644
@@ -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

#6 lore raw

PATCH 4/6 KVM: selftests: Make Hyper-V guest OS ID common

In reply to Vipin Sharma

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

---
 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(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +10 -0
index 075fd29071a6..9d8c325af1d9 100644
@@ -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 */
tools/testing/selftests/kvm/x86_64/hyperv_clock.c +1 -1
index d576bc8ce823..f9112c5dc3f7 100644
@@ -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();
tools/testing/selftests/kvm/x86_64/hyperv_features.c +2 -4
index 6b443ce456b6..b5a42cf1ad9d 100644
@@ -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:
tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +1 -1
index a380ad7bb9b3..2c13a144b04c 100644
@@ -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. */
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#7 lore raw

Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common

Quoted 2 lines · Vipin Sharma

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?

Quoted 25 lines
---
 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(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
index 075fd29071a6..9d8c325af1d9 100644
@@ -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

tools/testing/selftests/kvm/include/x86_64/hyperv.h line 199

Quoted 7 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
@@ -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).

Quoted 63 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +2 -0
+}
+
 #endif /* !SELFTEST_KVM_HYPERV_H */
tools/testing/selftests/kvm/x86_64/hyperv_clock.c +1 -1
index d576bc8ce823..f9112c5dc3f7 100644
@@ -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();
tools/testing/selftests/kvm/x86_64/hyperv_features.c +2 -4
index 6b443ce456b6..b5a42cf1ad9d 100644
@@ -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:
tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +1 -1
index a380ad7bb9b3..2c13a144b04c 100644
@@ -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. */
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#8 lore raw

Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common

Quoted 9 lines · David Matlack
Quoted 2 lines · Vipin Sharma

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.

Quoted 28 lines
Quoted 28 lines
---
 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(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
index 075fd29071a6..9d8c325af1d9 100644
@@ -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)
 
Quoted 7 lines

tools/testing/selftests/kvm/include/x86_64/hyperv.h line 199

Quoted 4 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
+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.

Quoted 63 lines
Quoted 63 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +2 -0
+}
+
 #endif /* !SELFTEST_KVM_HYPERV_H */
tools/testing/selftests/kvm/x86_64/hyperv_clock.c +1 -1
index d576bc8ce823..f9112c5dc3f7 100644
@@ -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();
tools/testing/selftests/kvm/x86_64/hyperv_features.c +2 -4
index 6b443ce456b6..b5a42cf1ad9d 100644
@@ -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:
tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +1 -2
index a380ad7bb9b3..2c13a144b04c 100644
@@ -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

#9 lore raw

Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common

Quoted 12 lines · Vipin Sharma
Quoted 9 lines · David Matlack
Quoted 2 lines · Vipin Sharma

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?

Quoted 6 lines

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.

Quoted 102 lines
Quoted 29 lines
Quoted 29 lines
---
 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(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
index 075fd29071a6..9d8c325af1d9 100644
@@ -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)
 
Quoted 7 lines

tools/testing/selftests/kvm/include/x86_64/hyperv.h line 199

Quoted 4 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
+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.

Quoted 63 lines
Quoted 63 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +2 -0
+}
+
 #endif /* !SELFTEST_KVM_HYPERV_H */
tools/testing/selftests/kvm/x86_64/hyperv_clock.c +1 -1
index d576bc8ce823..f9112c5dc3f7 100644
@@ -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();
tools/testing/selftests/kvm/x86_64/hyperv_features.c +2 -4
index 6b443ce456b6..b5a42cf1ad9d 100644
@@ -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:
tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +1 -2
index a380ad7bb9b3..2c13a144b04c 100644
@@ -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

maintainer
#10 lore raw

Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common

In reply to Vipin Sharma
Quoted 2 lines · Vipin Sharma

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/[email protected]/

Quoted 92 lines
---
 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(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +10 -0
index 075fd29071a6..9d8c325af1d9 100644
@@ -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 */
tools/testing/selftests/kvm/x86_64/hyperv_clock.c +1 -1
index d576bc8ce823..f9112c5dc3f7 100644
@@ -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();
tools/testing/selftests/kvm/x86_64/hyperv_features.c +2 -4
index 6b443ce456b6..b5a42cf1ad9d 100644
@@ -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:
tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +1 -1
index a380ad7bb9b3..2c13a144b04c 100644
@@ -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. */
Signature · 1 line
Vitaly
#11 lore raw

Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common

Quoted 11 lines · Vitaly Kuznetsov
Quoted 2 lines · Vipin Sharma

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/[email protected]/

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.

#12 lore raw

Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common

Quoted 13 lines · Vipin Sharma
Quoted 11 lines · Vitaly Kuznetsov
Quoted 2 lines · Vipin Sharma

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/[email protected]/

Quoted 7 lines

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 ;-)

maintainer
#13 lore raw

Re: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common

In reply to Vipin Sharma
Quoted 24 lines · Vipin Sharma
Quoted 11 lines · Vitaly Kuznetsov
Quoted 2 lines · Vipin Sharma

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/[email protected]/

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?

Quoted 8 lines

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)

Signature · 1 line
Vitaly
#14 lore raw

PATCH 5/6 KVM: selftests: Move hypercall() to hyper.h

In reply to Vipin Sharma

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

---
 .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
 .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
 2 files changed, 17 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +17 -0
index 9d8c325af1d9..87d8d9e444f7 100644
@@ -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 */
tools/testing/selftests/kvm/x86_64/hyperv_features.c +0 -17
index b5a42cf1ad9d..31b22ee07dfb 100644
@@ -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;
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#15 lore raw

Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h

Quoted 22 lines · Vipin Sharma

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

---
 .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
 .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
 2 files changed, 17 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +6 -0
index 9d8c325af1d9..87d8d9e444f7 100644
@@ -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. */

tools/testing/selftests/kvm/include/x86_64/hyperv.h static inline uint64_t hv_linux_guest_id(void) · line 217

Quoted 10 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +10 -0
+	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().

Quoted 33 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +1 -0
+
 #endif /* !SELFTEST_KVM_HYPERV_H */
tools/testing/selftests/kvm/x86_64/hyperv_features.c +0 -17
index b5a42cf1ad9d..31b22ee07dfb 100644
@@ -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;
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#16 lore raw

Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h

Quoted 30 lines · David Matlack
Quoted 28 lines · Vipin Sharma

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

---
 .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
 .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
 2 files changed, 17 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +12 -0
index 9d8c325af1d9..87d8d9e444f7 100644
@@ -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),
Quoted 9 lines

tools/testing/selftests/kvm/include/x86_64/hyperv.h static inline uint64_t hv_linux_guest_id(void) · line 217

Quoted 4 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
+                    "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?

Quoted 33 lines
Quoted 33 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +1 -0
+
 #endif /* !SELFTEST_KVM_HYPERV_H */
tools/testing/selftests/kvm/x86_64/hyperv_features.c +0 -18
index b5a42cf1ad9d..31b22ee07dfb 100644
@@ -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

#17 lore raw

Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h

Quoted 42 lines · Vipin Sharma
Quoted 30 lines · David Matlack
Quoted 28 lines · Vipin Sharma

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

---
 .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
 .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
 2 files changed, 17 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +12 -0
index 9d8c325af1d9..87d8d9e444f7 100644
@@ -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),
Quoted 9 lines

tools/testing/selftests/kvm/include/x86_64/hyperv.h static inline uint64_t hv_linux_guest_id(void) · line 217

Quoted 4 lines
tools/testing/selftests/kvm/include/x86_64/hyperv.h +4 -0
+                    "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().

Quoted 3 lines

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.

maintainer
#18 lore raw

Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h

In reply to Vipin Sharma
Quoted 2 lines · Vipin Sharma

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/[email protected]/

Quoted 60 lines
---
 .../selftests/kvm/include/x86_64/hyperv.h       | 17 +++++++++++++++++
 .../selftests/kvm/x86_64/hyperv_features.c      | 17 -----------------
 2 files changed, 17 insertions(+), 17 deletions(-)
tools/testing/selftests/kvm/include/x86_64/hyperv.h +17 -0
index 9d8c325af1d9..87d8d9e444f7 100644
@@ -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 */
tools/testing/selftests/kvm/x86_64/hyperv_features.c +0 -17
index b5a42cf1ad9d..31b22ee07dfb 100644
@@ -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;
Signature · 1 line
Vitaly
#19 lore raw

PATCH 6/6 KVM: selftests: Test Hyper-V extended hypercall exit to userspace

In reply to 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.

---
 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
tools/testing/selftests/kvm/.gitignore +1 -0
index 2f0d705db9db..ffe06dd1cc6e 100644
@@ -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
tools/testing/selftests/kvm/Makefile +1 -0
index 0172eb6cb6ee..366345099363 100644
@@ -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
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +90 -0

90 lines not on this page · raw message

Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#20 lore raw

Re: [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace

Quoted 17 lines · 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.

---
 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
tools/testing/selftests/kvm/.gitignore +0 -0
index 2f0d705db9db..ffe06dd1cc6e 100644

tools/testing/selftests/kvm/.gitignore line 27

Quoted 4 lines
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -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.

Quoted 21 lines
tools/testing/selftests/kvm/.gitignore +0 -0
 /x86_64/hyperv_features
 /x86_64/hyperv_svm_test
 /x86_64/max_vcpuid_cap_test
tools/testing/selftests/kvm/Makefile +1 -0
index 0172eb6cb6ee..366345099363 100644
@@ -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
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +0 -0
new file mode 100644
index 000000000000..d378877235d4

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 3

Quoted 3 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +3 -0
@@ -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.

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 5

Quoted 2 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +2 -0
+ *
+ * Copyright 2020 Google LLC

2022 :)

Quoted 15 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +15 -0
+ * Author: Vipin Sharma <[email protected]>
+ */
+
+#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;
+

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 30

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +10 -0
+	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);

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 34

Quoted 4 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +4 -0
+
+	/* 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);

Quoted 7 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +7 -0
+
+	GUEST_DONE();
+}
+
+static void guest_extended_hcall_test(void)
+{
+	struct kvm_vcpu *vcpu;

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 51

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +10 -0
+	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.

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 55

Quoted 4 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +4 -0
+
+	/* 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/

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 56

Quoted 1 line
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +1 -0
+	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.

Quoted 6 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +6 -0
+
+	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));

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 72

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +10 -0
+
+	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);
Quoted 7 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +7 -0
+
+	switch (get_ucall(vcpu, &uc)) {
+	case UCALL_ABORT:
+		REPORT_GUEST_ASSERT_2(uc, "arg1 = %ld, arg2 = %ld");
+		break;
+	case UCALL_DONE:
+		break;

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 89

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +10 -0
+	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()?

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 90

Quoted 1 line
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +1 -0
+}

return 0?

Quoted 3 lines
Signature · 1 line
2.38.1.273.g43a17bfeac-goog
#21 lore raw

Re: [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace

Quoted 14 lines · David Matlack
Quoted 12 lines · 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.

---
 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
Quoted 13 lines

tools/testing/selftests/kvm/.gitignore line 27

Quoted 9 lines
tools/testing/selftests/kvm/.gitignore +1 -0
index 2f0d705db9db..ffe06dd1cc6e 100644
@@ -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.

Quoted 21 lines
Quoted 21 lines
tools/testing/selftests/kvm/.gitignore +0 -0
 /x86_64/hyperv_features
 /x86_64/hyperv_svm_test
 /x86_64/max_vcpuid_cap_test
tools/testing/selftests/kvm/Makefile +1 -0
index 0172eb6cb6ee..366345099363 100644
@@ -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
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +0 -0
new file mode 100644
index 000000000000..d378877235d4
@@ -0,0 +1,90 @@
Quoted 9 lines

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 3

Quoted 3 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +3 -0
+// 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.

Quoted 24 lines

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 5

Quoted 2 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +2 -0
+ *
+ * Copyright 2020 Google LLC

2022 :)

Quoted 19 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +19 -0
+ * Author: Vipin Sharma <[email protected]>
+ */
+
+#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;
Quoted 9 lines

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 30

Quoted 6 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +6 -0
+
+     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.

Quoted 19 lines

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 34

Quoted 4 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +4 -0
+
+     /* 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);

Quoted 12 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +12 -0
+
+     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;
Quoted 9 lines

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 51

Quoted 5 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +5 -0
+
+     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.

Quoted 5 lines

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 55

Quoted 4 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +4 -0
+
+     /* Hypercall input/output */
+     hcall_page = vm_vaddr_alloc_pages(vm, 2);
+     memset(addr_gva2hva(vm, hcall_page), 0x0, 2 * getpagesize());
Quoted 9 lines

s/getpagesize()/vm->page_size/

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 56

Quoted 1 line
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +1 -0
+     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.

Quoted 20 lines
Quoted 6 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +6 -0
+
+     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));

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 72

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +10 -0
+
+     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.

Quoted 2 lines
        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{}.

Quoted 11 lines
Quoted 11 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +11 -0
+
+     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);
+     }
+
Quoted 9 lines

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 89

Quoted 6 lines
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +6 -0
+     kvm_vm_free(vm);
+}
+
+int main(void)
+{
+     guest_extended_hcall_test();

Why not just put all this in main()?

I will.

Quoted 7 lines

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 90

Quoted 1 line
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +1 -0
+}

return 0?

Quoted 3 lines

--
2.38.1.273.g43a17bfeac-goog

#22 lore raw

Re: [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace

Quoted 12 lines · Vipin Sharma
Quoted 10 lines · David Matlack

tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c line 72

Quoted 4 lines · Vipin Sharma
tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file +4 -0
+
+     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.

Quoted 6 lines
Quoted 2 lines
        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().