1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Test Hyper-V extended hypercall, HV_EXT_CALL_QUERY_CAPABILITIES (0x8001),
4 * exit to userspace and receive result in guest.
5 *
6 * Negative tests are present in hyperv_features.c
7 *
8 * Copyright 2022 Google LLC
9 * Author: Vipin Sharma <vipinsh@google.com>
10 */
11 #include "kvm_util.h"
12 #include "processor.h"
13 #include "hyperv.h"
14
15 /* Any value is fine */
16 #define EXT_CAPABILITIES 0xbull
17
guest_code(vm_paddr_t in_pg_gpa,vm_paddr_t out_pg_gpa,vm_vaddr_t out_pg_gva)18 static void guest_code(vm_paddr_t in_pg_gpa, vm_paddr_t out_pg_gpa,
19 vm_vaddr_t out_pg_gva)
20 {
21 uint64_t *output_gva;
22
23 wrmsr(HV_X64_MSR_GUEST_OS_ID, HYPERV_LINUX_OS_ID);
24 wrmsr(HV_X64_MSR_HYPERCALL, in_pg_gpa);
25
26 output_gva = (uint64_t *)out_pg_gva;
27
28 hyperv_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, in_pg_gpa, out_pg_gpa);
29
30 /* TLFS states output will be a uint64_t value */
31 GUEST_ASSERT_EQ(*output_gva, EXT_CAPABILITIES);
32
33 GUEST_DONE();
34 }
35
main(void)36 int main(void)
37 {
38 vm_vaddr_t hcall_out_page;
39 vm_vaddr_t hcall_in_page;
40 struct kvm_vcpu *vcpu;
41 struct kvm_run *run;
42 struct kvm_vm *vm;
43 uint64_t *outval;
44 struct ucall uc;
45
46 /* Verify if extended hypercalls are supported */
47 if (!kvm_cpuid_has(kvm_get_supported_hv_cpuid(),
48 HV_ENABLE_EXTENDED_HYPERCALLS)) {
49 print_skip("Extended calls not supported by the kernel");
50 exit(KSFT_SKIP);
51 }
52
53 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
54 run = vcpu->run;
55 vcpu_set_hv_cpuid(vcpu);
56
57 /* Hypercall input */
58 hcall_in_page = vm_vaddr_alloc_pages(vm, 1);
59 memset(addr_gva2hva(vm, hcall_in_page), 0x0, vm->page_size);
60
61 /* Hypercall output */
62 hcall_out_page = vm_vaddr_alloc_pages(vm, 1);
63 memset(addr_gva2hva(vm, hcall_out_page), 0x0, vm->page_size);
64
65 vcpu_args_set(vcpu, 3, addr_gva2gpa(vm, hcall_in_page),
66 addr_gva2gpa(vm, hcall_out_page), hcall_out_page);
67
68 vcpu_run(vcpu);
69
70 TEST_ASSERT(run->exit_reason == KVM_EXIT_HYPERV,
71 "Unexpected exit reason: %u (%s)",
72 run->exit_reason, exit_reason_str(run->exit_reason));
73
74 outval = addr_gpa2hva(vm, run->hyperv.u.hcall.params[1]);
75 *outval = EXT_CAPABILITIES;
76 run->hyperv.u.hcall.result = HV_STATUS_SUCCESS;
77
78 vcpu_run(vcpu);
79
80 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
81 "Unexpected exit reason: %u (%s)",
82 run->exit_reason, exit_reason_str(run->exit_reason));
83
84 switch (get_ucall(vcpu, &uc)) {
85 case UCALL_ABORT:
86 REPORT_GUEST_ASSERT(uc);
87 break;
88 case UCALL_DONE:
89 break;
90 default:
91 TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
92 }
93
94 kvm_vm_free(vm);
95 return 0;
96 }
97