Path: blob/master/tools/testing/selftests/kvm/x86/hyperv_extended_hypercalls.c
38247 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Test Hyper-V extended hypercall, HV_EXT_CALL_QUERY_CAPABILITIES (0x8001),3* exit to userspace and receive result in guest.4*5* Negative tests are present in hyperv_features.c6*7* Copyright 2022 Google LLC8* Author: Vipin Sharma <[email protected]>9*/10#include "kvm_util.h"11#include "processor.h"12#include "hyperv.h"1314/* Any value is fine */15#define EXT_CAPABILITIES 0xbull1617static void guest_code(vm_paddr_t in_pg_gpa, vm_paddr_t out_pg_gpa,18vm_vaddr_t out_pg_gva)19{20uint64_t *output_gva;2122wrmsr(HV_X64_MSR_GUEST_OS_ID, HYPERV_LINUX_OS_ID);23wrmsr(HV_X64_MSR_HYPERCALL, in_pg_gpa);2425output_gva = (uint64_t *)out_pg_gva;2627hyperv_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, in_pg_gpa, out_pg_gpa);2829/* TLFS states output will be a uint64_t value */30GUEST_ASSERT_EQ(*output_gva, EXT_CAPABILITIES);3132GUEST_DONE();33}3435int main(void)36{37vm_vaddr_t hcall_out_page;38vm_vaddr_t hcall_in_page;39struct kvm_vcpu *vcpu;40struct kvm_run *run;41struct kvm_vm *vm;42uint64_t *outval;43struct ucall uc;4445TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));4647/* Verify if extended hypercalls are supported */48if (!kvm_cpuid_has(kvm_get_supported_hv_cpuid(),49HV_ENABLE_EXTENDED_HYPERCALLS)) {50print_skip("Extended calls not supported by the kernel");51exit(KSFT_SKIP);52}5354vm = vm_create_with_one_vcpu(&vcpu, guest_code);55run = vcpu->run;56vcpu_set_hv_cpuid(vcpu);5758/* Hypercall input */59hcall_in_page = vm_vaddr_alloc_pages(vm, 1);60memset(addr_gva2hva(vm, hcall_in_page), 0x0, vm->page_size);6162/* Hypercall output */63hcall_out_page = vm_vaddr_alloc_pages(vm, 1);64memset(addr_gva2hva(vm, hcall_out_page), 0x0, vm->page_size);6566vcpu_args_set(vcpu, 3, addr_gva2gpa(vm, hcall_in_page),67addr_gva2gpa(vm, hcall_out_page), hcall_out_page);6869vcpu_run(vcpu);7071TEST_ASSERT(run->exit_reason == KVM_EXIT_HYPERV,72"Unexpected exit reason: %u (%s)",73run->exit_reason, exit_reason_str(run->exit_reason));7475outval = addr_gpa2hva(vm, run->hyperv.u.hcall.params[1]);76*outval = EXT_CAPABILITIES;77run->hyperv.u.hcall.result = HV_STATUS_SUCCESS;7879vcpu_run(vcpu);8081TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,82"Unexpected exit reason: %u (%s)",83run->exit_reason, exit_reason_str(run->exit_reason));8485switch (get_ucall(vcpu, &uc)) {86case UCALL_ABORT:87REPORT_GUEST_ASSERT(uc);88break;89case UCALL_DONE:90break;91default:92TEST_FAIL("Unhandled ucall: %ld", uc.cmd);93}9495kvm_vm_free(vm);96return 0;97}9899100