1 /*- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 2 * Copyright (c) 2009-2012,2016-2017, 2022 Microsoft Corp. 3 * Copyright (c) 2012 NetApp Inc. 4 * Copyright (c) 2012 Citrix Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * Implements low-level interactions with Hyper-V/Azure 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/timetc.h> 41 42 #include <vm/vm.h> 43 #include <vm/pmap.h> 44 #include <vm/vm_extern.h> 45 #include <vm/vm_kern.h> 46 47 #include <dev/hyperv/include/hyperv.h> 48 #include <dev/hyperv/include/hyperv_busdma.h> 49 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h> 50 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h> 51 #include <dev/hyperv/vmbus/hyperv_var.h> 52 #include <dev/hyperv/vmbus/hyperv_common_reg.h> 53 #include <contrib/dev/acpica/include/acpi.h> 54 55 void hyperv_init_tc(void); 56 int hypercall_page_setup(vm_paddr_t); 57 void hypercall_disable(void); 58 bool hyperv_identify_features(void); 59 60 static bool is_hyperv(void); 61 62 u_int hyperv_ver_major; 63 u_int hyperv_features; 64 u_int hyperv_recommends; 65 66 hyperv_tc64_t hyperv_tc64; 67 68 void 69 hyperv_init_tc(void) 70 { 71 hyperv_tc64 = NULL; 72 } 73 74 int 75 hypercall_page_setup(vm_paddr_t hc) 76 { 77 return (0); 78 } 79 80 void 81 hypercall_disable(void) 82 { 83 return; 84 } 85 86 /* 87 * This function verifies if the platform is Hyper-V or not. 88 * To do that we are using ACPI FADT and for that, acpi 89 * fadt is mapped first. 90 */ 91 static bool 92 is_hyperv(void) 93 { 94 ACPI_TABLE_FADT *fadt; 95 vm_paddr_t physaddr; 96 uint64_t hypervid; 97 bool ret; 98 99 physaddr = acpi_find_table(ACPI_SIG_FADT); 100 if (physaddr == 0) 101 return (false); 102 103 fadt = acpi_map_table(physaddr, ACPI_SIG_FADT); 104 if (fadt == NULL) { 105 printf("hyperv: Unable to map the FADT\n"); 106 return (false); 107 } 108 109 hypervid = fadt->HypervisorId; 110 acpi_unmap_table(fadt); 111 ret = strncmp((char *)&hypervid, "MsHyperV", 8) == 0 ? true : false; 112 return (ret); 113 } 114 115 bool 116 hyperv_identify_features(void) 117 { 118 struct hv_get_vp_registers_output result; 119 120 if (resource_disabled("acpi", 0)) 121 return (false); 122 if (!is_hyperv()) 123 return (false); 124 125 vm_guest = VM_GUEST_HV; 126 /* use MSRs to get the hyperv specific 127 * attributes. 128 */ 129 hv_get_vpreg_128(CPUID_LEAF_HV_FEATURES, &result); 130 hyperv_features = result.as32.a; 131 hv_get_vpreg_128(CPUID_LEAF_HV_IDENTITY, &result); 132 hyperv_ver_major = result.as32.b >> 16; 133 hv_get_vpreg_128(CPUID_LEAF_HV_RECOMMENDS, &result); 134 hyperv_recommends = result.as32.a; 135 return (true); 136 } 137