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/hyperv_var.h>
47 #include <dev/hyperv/vmbus/x86/hyperv_machdep.h>
48 #include <dev/hyperv/vmbus/x86/hyperv_reg.h>
49 #include <dev/hyperv/vmbus/hyperv_common_reg.h>
50
51 #define HYPERV_FREEBSD_BUILD 0ULL
52 #define HYPERV_FREEBSD_VERSION ((uint64_t)__FreeBSD_version)
53 #define HYPERV_FREEBSD_OSID 0ULL
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 u_int hyperv_ver_major;
61 u_int hyperv_features;
62 u_int hyperv_recommends;
63
64 hyperv_tc64_t hyperv_tc64;
65
66 static u_int hyperv_pm_features;
67 static u_int hyperv_features3;
68 static u_int hyperv_get_timecount(struct timecounter *);
69
70 static struct timecounter hyperv_timecounter = {
71 .tc_get_timecount = hyperv_get_timecount,
72 .tc_poll_pps = NULL,
73 .tc_counter_mask = 0xffffffff,
74 .tc_frequency = HYPERV_TIMER_FREQ,
75 .tc_name = "Hyper-V",
76 .tc_quality = 2000,
77 .tc_flags = 0,
78 .tc_priv = NULL
79 };
80
81 static u_int
hyperv_get_timecount(struct timecounter * tc __unused)82 hyperv_get_timecount(struct timecounter *tc __unused)
83 {
84 return rdmsr(MSR_HV_TIME_REF_COUNT);
85 }
86
87 static uint64_t
hyperv_tc64_rdmsr(void)88 hyperv_tc64_rdmsr(void)
89 {
90
91 return (rdmsr(MSR_HV_TIME_REF_COUNT));
92 }
93
94 void
hyperv_init_tc(void)95 hyperv_init_tc(void)
96 {
97 if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) {
98 /*
99 * Register Hyper-V timecounter. This should be done as early
100 * as possible to let DELAY() work, since the 8254 PIT is not
101 * reliably emulated or even available.
102 */
103 tc_init(&hyperv_timecounter);
104
105 /*
106 * Install 64 bits timecounter method for other modules
107 * to use.
108 */
109 hyperv_tc64 = hyperv_tc64_rdmsr;
110 }
111 }
112
113 int
hypercall_page_setup(vm_paddr_t paddr)114 hypercall_page_setup(vm_paddr_t paddr)
115 {
116 uint64_t hc, hc_orig;
117 hc_orig = rdmsr(MSR_HV_HYPERCALL);
118
119 /*
120 * Setup the Hypercall page.
121 *
122 * NOTE: 'reserved' bits MUST be preserved.
123 */
124 hc = ((paddr >> PAGE_SHIFT) << MSR_HV_HYPERCALL_PGSHIFT) |
125 (hc_orig & MSR_HV_HYPERCALL_RSVD_MASK) | MSR_HV_HYPERCALL_ENABLE;
126 wrmsr(MSR_HV_HYPERCALL, hc);
127
128 /*
129 * Confirm that Hypercall page did get setup.
130 */
131 hc = rdmsr(MSR_HV_HYPERCALL);
132 if ((hc & MSR_HV_HYPERCALL_ENABLE) == 0) {
133 printf("hyperv: Hypercall setup failed\n");
134 /* Can't perform any Hyper-V specific actions */
135 vm_guest = VM_GUEST_VM;
136 return (-1);
137 }
138 return (0);
139 }
140
141 void
hypercall_disable(void)142 hypercall_disable(void)
143 {
144 uint64_t hc;
145 /* Disable Hypercall */
146 hc = rdmsr(MSR_HV_HYPERCALL);
147 wrmsr(MSR_HV_HYPERCALL, (hc & MSR_HV_HYPERCALL_RSVD_MASK));
148 }
149
150 bool
hyperv_identify_features(void)151 hyperv_identify_features(void)
152 {
153 u_int regs[4];
154 unsigned int maxleaf;
155
156 if (vm_guest != VM_GUEST_HV)
157 return (false);
158
159 do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
160 maxleaf = regs[0];
161 if (maxleaf < CPUID_LEAF_HV_LIMITS)
162 return (false);
163
164 do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
165 if (regs[0] != CPUID_HV_IFACE_HYPERV)
166 return (false);
167
168 do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
169 if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) {
170 /*
171 * Hyper-V w/o Hypercall is impossible; someone
172 * is faking Hyper-V.
173 */
174 return (false);
175 }
176 hyperv_features = regs[0];
177 hyperv_pm_features = regs[2];
178 hyperv_features3 = regs[3];
179 do_cpuid(CPUID_LEAF_HV_IDENTITY, regs);
180 hyperv_ver_major = regs[1] >> 16;
181 printf("Hyper-V Version: %d.%d.%d [SP%d]\n", hyperv_ver_major,
182 regs[1] & 0xffff, regs[0], regs[2]);
183
184 printf(" Features=0x%b\n", hyperv_features,
185 "\020"
186 "\001VPRUNTIME" /* MSR_HV_VP_RUNTIME */
187 "\002TMREFCNT" /* MSR_HV_TIME_REF_COUNT */
188 "\003SYNIC" /* MSRs for SynIC */
189 "\004SYNTM" /* MSRs for SynTimer */
190 "\005APIC" /* MSR_HV_{EOI,ICR,TPR} */
191 "\006HYPERCALL" /* MSR_HV_{GUEST_OS_ID,HYPERCALL} */
192 "\007VPINDEX" /* MSR_HV_VP_INDEX */
193 "\010RESET" /* MSR_HV_RESET */
194 "\011STATS" /* MSR_HV_STATS_ */
195 "\012REFTSC" /* MSR_HV_REFERENCE_TSC */
196 "\013IDLE" /* MSR_HV_GUEST_IDLE */
197 "\014TMFREQ" /* MSR_HV_{TSC,APIC}_FREQUENCY */
198 "\015DEBUG"); /* MSR_HV_SYNTH_DEBUG_ */
199 printf(" PM Features=0x%b [C%u]\n",
200 (hyperv_pm_features & ~CPUPM_HV_CSTATE_MASK),
201 "\020"
202 "\005C3HPET", /* HPET is required for C3 state */
203 CPUPM_HV_CSTATE(hyperv_pm_features));
204 printf(" Features3=0x%b\n", hyperv_features3,
205 "\020"
206 "\001MWAIT" /* MWAIT */
207 "\002DEBUG" /* guest debug support */
208 "\003PERFMON" /* performance monitor */
209 "\004PCPUDPE" /* physical CPU dynamic partition event */
210 "\005XMMHC" /* hypercall input through XMM regs */
211 "\006IDLE" /* guest idle support */
212 "\007SLEEP" /* hypervisor sleep support */
213 "\010NUMA" /* NUMA distance query support */
214 "\011TMFREQ" /* timer frequency query (TSC, LAPIC) */
215 "\012SYNCMC" /* inject synthetic machine checks */
216 "\013CRASH" /* MSRs for guest crash */
217 "\014DEBUGMSR" /* MSRs for guest debug */
218 "\015NPIEP" /* NPIEP */
219 "\016HVDIS"); /* disabling hypervisor */
220
221 do_cpuid(CPUID_LEAF_HV_RECOMMENDS, regs);
222 hyperv_recommends = regs[0];
223 if (bootverbose)
224 printf(" Recommends: %08x %08x\n", regs[0], regs[1]);
225
226 do_cpuid(CPUID_LEAF_HV_LIMITS, regs);
227 if (bootverbose) {
228 printf(" Limits: Vcpu:%d Lcpu:%d Int:%d\n", regs[0], regs[1],
229 regs[2]);
230 }
231
232 if (maxleaf >= CPUID_LEAF_HV_HWFEATURES) {
233 do_cpuid(CPUID_LEAF_HV_HWFEATURES, regs);
234 if (bootverbose) {
235 printf(" HW Features: %08x, AMD: %08x\n", regs[0],
236 regs[3]);
237 }
238 }
239 return (true);
240 }
241