Path: blob/master/tools/testing/selftests/kvm/lib/x86/hyperv.c
49428 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Hyper-V specific functions.3*4* Copyright (C) 2021, Red Hat Inc.5*/6#include <stdint.h>7#include "processor.h"8#include "hyperv.h"910const struct kvm_cpuid2 *kvm_get_supported_hv_cpuid(void)11{12static struct kvm_cpuid2 *cpuid;13int kvm_fd;1415if (cpuid)16return cpuid;1718cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);19kvm_fd = open_kvm_dev_path_or_exit();2021kvm_ioctl(kvm_fd, KVM_GET_SUPPORTED_HV_CPUID, cpuid);2223close(kvm_fd);24return cpuid;25}2627void vcpu_set_hv_cpuid(struct kvm_vcpu *vcpu)28{29static struct kvm_cpuid2 *cpuid_full;30const struct kvm_cpuid2 *cpuid_sys, *cpuid_hv;31int i, nent = 0;3233if (!cpuid_full) {34cpuid_sys = kvm_get_supported_cpuid();35cpuid_hv = kvm_get_supported_hv_cpuid();3637cpuid_full = allocate_kvm_cpuid2(cpuid_sys->nent + cpuid_hv->nent);38if (!cpuid_full) {39perror("malloc");40abort();41}4243/* Need to skip KVM CPUID leaves 0x400000xx */44for (i = 0; i < cpuid_sys->nent; i++) {45if (cpuid_sys->entries[i].function >= 0x40000000 &&46cpuid_sys->entries[i].function < 0x40000100)47continue;48cpuid_full->entries[nent] = cpuid_sys->entries[i];49nent++;50}5152memcpy(&cpuid_full->entries[nent], cpuid_hv->entries,53cpuid_hv->nent * sizeof(struct kvm_cpuid_entry2));54cpuid_full->nent = nent + cpuid_hv->nent;55}5657vcpu_init_cpuid(vcpu, cpuid_full);58}5960const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu)61{62struct kvm_cpuid2 *cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);6364vcpu_ioctl(vcpu, KVM_GET_SUPPORTED_HV_CPUID, cpuid);6566return cpuid;67}6869bool kvm_hv_cpu_has(struct kvm_x86_cpu_feature feature)70{71if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID))72return false;7374return kvm_cpuid_has(kvm_get_supported_hv_cpuid(), feature);75}7677struct hyperv_test_pages *vcpu_alloc_hyperv_test_pages(struct kvm_vm *vm,78vm_vaddr_t *p_hv_pages_gva)79{80vm_vaddr_t hv_pages_gva = vm_vaddr_alloc_page(vm);81struct hyperv_test_pages *hv = addr_gva2hva(vm, hv_pages_gva);8283/* Setup of a region of guest memory for the VP Assist page. */84hv->vp_assist = (void *)vm_vaddr_alloc_page(vm);85hv->vp_assist_hva = addr_gva2hva(vm, (uintptr_t)hv->vp_assist);86hv->vp_assist_gpa = addr_gva2gpa(vm, (uintptr_t)hv->vp_assist);8788/* Setup of a region of guest memory for the partition assist page. */89hv->partition_assist = (void *)vm_vaddr_alloc_page(vm);90hv->partition_assist_hva = addr_gva2hva(vm, (uintptr_t)hv->partition_assist);91hv->partition_assist_gpa = addr_gva2gpa(vm, (uintptr_t)hv->partition_assist);9293/* Setup of a region of guest memory for the enlightened VMCS. */94hv->enlightened_vmcs = (void *)vm_vaddr_alloc_page(vm);95hv->enlightened_vmcs_hva = addr_gva2hva(vm, (uintptr_t)hv->enlightened_vmcs);96hv->enlightened_vmcs_gpa = addr_gva2gpa(vm, (uintptr_t)hv->enlightened_vmcs);9798*p_hv_pages_gva = hv_pages_gva;99return hv;100}101102int enable_vp_assist(uint64_t vp_assist_pa, void *vp_assist)103{104uint64_t val = (vp_assist_pa & HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_MASK) |105HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;106107wrmsr(HV_X64_MSR_VP_ASSIST_PAGE, val);108109current_vp_assist = vp_assist;110111return 0;112}113114115