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