1 /*- SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/timetc.h>
39
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 #include <vm/vm_extern.h>
43 #include <vm/vm_kern.h>
44
45 #include <dev/hyperv/include/hyperv.h>
46 #include <dev/hyperv/include/hyperv_busdma.h>
47 #include <dev/hyperv/vmbus/aarch64/hyperv_machdep.h>
48 #include <dev/hyperv/vmbus/aarch64/hyperv_reg.h>
49 #include <dev/hyperv/vmbus/hyperv_var.h>
50 #include <dev/hyperv/vmbus/hyperv_common_reg.h>
51 #include <contrib/dev/acpica/include/acpi.h>
52
53 void hyperv_init_tc(void);
54 int hypercall_page_setup(vm_paddr_t);
55 void hypercall_disable(void);
56 bool hyperv_identify_features(void);
57
58 static bool is_hyperv(void);
59
60 u_int hyperv_ver_major;
61 u_int hyperv_features;
62 u_int hyperv_recommends;
63
64 hyperv_tc64_t hyperv_tc64;
65
66 void
hyperv_init_tc(void)67 hyperv_init_tc(void)
68 {
69 hyperv_tc64 = NULL;
70 }
71
72 int
hypercall_page_setup(vm_paddr_t hc)73 hypercall_page_setup(vm_paddr_t hc)
74 {
75 return (0);
76 }
77
78 void
hypercall_disable(void)79 hypercall_disable(void)
80 {
81 return;
82 }
83
84 /*
85 * This function verifies if the platform is Hyper-V or not.
86 * To do that we are using ACPI FADT and for that, acpi
87 * fadt is mapped first.
88 */
89 static bool
is_hyperv(void)90 is_hyperv(void)
91 {
92 ACPI_TABLE_FADT *fadt;
93 vm_paddr_t physaddr;
94 uint64_t hypervid;
95 bool ret;
96
97 physaddr = acpi_find_table(ACPI_SIG_FADT);
98 if (physaddr == 0)
99 return (false);
100
101 fadt = acpi_map_table(physaddr, ACPI_SIG_FADT);
102 if (fadt == NULL) {
103 printf("hyperv: Unable to map the FADT\n");
104 return (false);
105 }
106
107 hypervid = fadt->HypervisorId;
108 acpi_unmap_table(fadt);
109 ret = strncmp((char *)&hypervid, "MsHyperV", 8) == 0 ? true : false;
110 return (ret);
111 }
112
113 bool
hyperv_identify_features(void)114 hyperv_identify_features(void)
115 {
116 struct hv_get_vp_registers_output result;
117
118 if (resource_disabled("acpi", 0))
119 return (false);
120 if (!is_hyperv())
121 return (false);
122
123 vm_guest = VM_GUEST_HV;
124 /* use MSRs to get the hyperv specific
125 * attributes.
126 */
127 hv_get_vpreg_128(CPUID_LEAF_HV_FEATURES, &result);
128 hyperv_features = result.as32.a;
129 hv_get_vpreg_128(CPUID_LEAF_HV_IDENTITY, &result);
130 hyperv_ver_major = result.as32.b >> 16;
131 hv_get_vpreg_128(CPUID_LEAF_HV_RECOMMENDS, &result);
132 hyperv_recommends = result.as32.a;
133 return (true);
134 }
135