xref: /freebsd-13-stable/sys/dev/hyperv/vmbus/hyperv.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * Copyright (c) 2009-2012,2016-2017 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 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
37 #include <sys/timetc.h>
38 
39 #include <vm/vm.h>
40 #include <vm/vm_extern.h>
41 #include <vm/vm_kern.h>
42 #include <vm/pmap.h>
43 
44 #include <dev/hyperv/include/hyperv.h>
45 #include <dev/hyperv/include/hyperv_busdma.h>
46 #include <dev/hyperv/vmbus/hyperv_machdep.h>
47 #include <dev/hyperv/vmbus/hyperv_reg.h>
48 #include <dev/hyperv/vmbus/hyperv_var.h>
49 
50 #define HYPERV_FREEBSD_BUILD		0ULL
51 #define HYPERV_FREEBSD_VERSION		((uint64_t)__FreeBSD_version)
52 #define HYPERV_FREEBSD_OSID		0ULL
53 
54 #define MSR_HV_GUESTID_BUILD_FREEBSD	\
55 	(HYPERV_FREEBSD_BUILD & MSR_HV_GUESTID_BUILD_MASK)
56 #define MSR_HV_GUESTID_VERSION_FREEBSD	\
57 	((HYPERV_FREEBSD_VERSION << MSR_HV_GUESTID_VERSION_SHIFT) & \
58 	 MSR_HV_GUESTID_VERSION_MASK)
59 #define MSR_HV_GUESTID_OSID_FREEBSD	\
60 	((HYPERV_FREEBSD_OSID << MSR_HV_GUESTID_OSID_SHIFT) & \
61 	 MSR_HV_GUESTID_OSID_MASK)
62 
63 #define MSR_HV_GUESTID_FREEBSD		\
64 	(MSR_HV_GUESTID_BUILD_FREEBSD |	\
65 	 MSR_HV_GUESTID_VERSION_FREEBSD | \
66 	 MSR_HV_GUESTID_OSID_FREEBSD |	\
67 	 MSR_HV_GUESTID_OSTYPE_FREEBSD)
68 
69 struct hypercall_ctx {
70 	void			*hc_addr;
71 	vm_paddr_t		hc_paddr;
72 };
73 
74 static u_int			hyperv_get_timecount(struct timecounter *);
75 static bool			hyperv_identify(void);
76 static void			hypercall_memfree(void);
77 
78 u_int				hyperv_ver_major;
79 
80 u_int				hyperv_features;
81 u_int				hyperv_recommends;
82 
83 static u_int			hyperv_pm_features;
84 static u_int			hyperv_features3;
85 
86 hyperv_tc64_t			hyperv_tc64;
87 
88 static struct timecounter	hyperv_timecounter = {
89 	.tc_get_timecount	= hyperv_get_timecount,
90 	.tc_poll_pps		= NULL,
91 	.tc_counter_mask	= 0xffffffff,
92 	.tc_frequency		= HYPERV_TIMER_FREQ,
93 	.tc_name		= "Hyper-V",
94 	.tc_quality		= 2000,
95 	.tc_flags		= 0,
96 	.tc_priv		= NULL
97 };
98 
99 static struct hypercall_ctx	hypercall_context;
100 
101 static u_int
hyperv_get_timecount(struct timecounter * tc __unused)102 hyperv_get_timecount(struct timecounter *tc __unused)
103 {
104 	return rdmsr(MSR_HV_TIME_REF_COUNT);
105 }
106 
107 static uint64_t
hyperv_tc64_rdmsr(void)108 hyperv_tc64_rdmsr(void)
109 {
110 
111 	return (rdmsr(MSR_HV_TIME_REF_COUNT));
112 }
113 
114 uint64_t
hypercall_post_message(bus_addr_t msg_paddr)115 hypercall_post_message(bus_addr_t msg_paddr)
116 {
117 	return hypercall_md(hypercall_context.hc_addr,
118 	    HYPERCALL_POST_MESSAGE, msg_paddr, 0);
119 }
120 
121 uint64_t
hypercall_signal_event(bus_addr_t monprm_paddr)122 hypercall_signal_event(bus_addr_t monprm_paddr)
123 {
124 	return hypercall_md(hypercall_context.hc_addr,
125 	    HYPERCALL_SIGNAL_EVENT, monprm_paddr, 0);
126 }
127 
128 int
hyperv_guid2str(const struct hyperv_guid * guid,char * buf,size_t sz)129 hyperv_guid2str(const struct hyperv_guid *guid, char *buf, size_t sz)
130 {
131 	const uint8_t *d = guid->hv_guid;
132 
133 	return snprintf(buf, sz, "%02x%02x%02x%02x-"
134 	    "%02x%02x-%02x%02x-%02x%02x-"
135 	    "%02x%02x%02x%02x%02x%02x",
136 	    d[3], d[2], d[1], d[0],
137 	    d[5], d[4], d[7], d[6], d[8], d[9],
138 	    d[10], d[11], d[12], d[13], d[14], d[15]);
139 }
140 
141 static bool
hyperv_identify(void)142 hyperv_identify(void)
143 {
144 	u_int regs[4];
145 	unsigned int maxleaf;
146 
147 	if (vm_guest != VM_GUEST_HV)
148 		return (false);
149 
150 	do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
151 	maxleaf = regs[0];
152 	if (maxleaf < CPUID_LEAF_HV_LIMITS)
153 		return (false);
154 
155 	do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
156 	if (regs[0] != CPUID_HV_IFACE_HYPERV)
157 		return (false);
158 
159 	do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
160 	if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) {
161 		/*
162 		 * Hyper-V w/o Hypercall is impossible; someone
163 		 * is faking Hyper-V.
164 		 */
165 		return (false);
166 	}
167 	hyperv_features = regs[0];
168 	hyperv_pm_features = regs[2];
169 	hyperv_features3 = regs[3];
170 
171 	do_cpuid(CPUID_LEAF_HV_IDENTITY, regs);
172 	hyperv_ver_major = regs[1] >> 16;
173 	printf("Hyper-V Version: %d.%d.%d [SP%d]\n",
174 	    hyperv_ver_major, regs[1] & 0xffff, regs[0], regs[2]);
175 
176 	printf("  Features=0x%b\n", hyperv_features,
177 	    "\020"
178 	    "\001VPRUNTIME"	/* MSR_HV_VP_RUNTIME */
179 	    "\002TMREFCNT"	/* MSR_HV_TIME_REF_COUNT */
180 	    "\003SYNIC"		/* MSRs for SynIC */
181 	    "\004SYNTM"		/* MSRs for SynTimer */
182 	    "\005APIC"		/* MSR_HV_{EOI,ICR,TPR} */
183 	    "\006HYPERCALL"	/* MSR_HV_{GUEST_OS_ID,HYPERCALL} */
184 	    "\007VPINDEX"	/* MSR_HV_VP_INDEX */
185 	    "\010RESET"		/* MSR_HV_RESET */
186 	    "\011STATS"		/* MSR_HV_STATS_ */
187 	    "\012REFTSC"	/* MSR_HV_REFERENCE_TSC */
188 	    "\013IDLE"		/* MSR_HV_GUEST_IDLE */
189 	    "\014TMFREQ"	/* MSR_HV_{TSC,APIC}_FREQUENCY */
190 	    "\015DEBUG");	/* MSR_HV_SYNTH_DEBUG_ */
191 	printf("  PM Features=0x%b [C%u]\n",
192 	    (hyperv_pm_features & ~CPUPM_HV_CSTATE_MASK),
193 	    "\020"
194 	    "\005C3HPET",	/* HPET is required for C3 state */
195 	    CPUPM_HV_CSTATE(hyperv_pm_features));
196 	printf("  Features3=0x%b\n", hyperv_features3,
197 	    "\020"
198 	    "\001MWAIT"		/* MWAIT */
199 	    "\002DEBUG"		/* guest debug support */
200 	    "\003PERFMON"	/* performance monitor */
201 	    "\004PCPUDPE"	/* physical CPU dynamic partition event */
202 	    "\005XMMHC"		/* hypercall input through XMM regs */
203 	    "\006IDLE"		/* guest idle support */
204 	    "\007SLEEP"		/* hypervisor sleep support */
205 	    "\010NUMA"		/* NUMA distance query support */
206 	    "\011TMFREQ"	/* timer frequency query (TSC, LAPIC) */
207 	    "\012SYNCMC"	/* inject synthetic machine checks */
208 	    "\013CRASH"		/* MSRs for guest crash */
209 	    "\014DEBUGMSR"	/* MSRs for guest debug */
210 	    "\015NPIEP"		/* NPIEP */
211 	    "\016HVDIS");	/* disabling hypervisor */
212 
213 	do_cpuid(CPUID_LEAF_HV_RECOMMENDS, regs);
214 	hyperv_recommends = regs[0];
215 	if (bootverbose)
216 		printf("  Recommends: %08x %08x\n", regs[0], regs[1]);
217 
218 	do_cpuid(CPUID_LEAF_HV_LIMITS, regs);
219 	if (bootverbose) {
220 		printf("  Limits: Vcpu:%d Lcpu:%d Int:%d\n",
221 		    regs[0], regs[1], regs[2]);
222 	}
223 
224 	if (maxleaf >= CPUID_LEAF_HV_HWFEATURES) {
225 		do_cpuid(CPUID_LEAF_HV_HWFEATURES, regs);
226 		if (bootverbose) {
227 			printf("  HW Features: %08x, AMD: %08x\n",
228 			    regs[0], regs[3]);
229 		}
230 	}
231 
232 	return (true);
233 }
234 
235 static void
hyperv_init(void * dummy __unused)236 hyperv_init(void *dummy __unused)
237 {
238 	if (!hyperv_identify()) {
239 		/* Not Hyper-V; reset guest id to the generic one. */
240 		if (vm_guest == VM_GUEST_HV)
241 			vm_guest = VM_GUEST_VM;
242 		return;
243 	}
244 
245 	/* Set guest id */
246 	wrmsr(MSR_HV_GUEST_OS_ID, MSR_HV_GUESTID_FREEBSD);
247 
248 	if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) {
249 		/*
250 		 * Register Hyper-V timecounter.  This should be done as early
251 		 * as possible to let DELAY() work, since the 8254 PIT is not
252 		 * reliably emulated or even available.
253 		 */
254 		tc_init(&hyperv_timecounter);
255 
256 		/*
257 		 * Install 64 bits timecounter method for other modules
258 		 * to use.
259 		 */
260 		hyperv_tc64 = hyperv_tc64_rdmsr;
261 	}
262 }
263 SYSINIT(hyperv_initialize, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, hyperv_init,
264     NULL);
265 
266 static void
hypercall_memfree(void)267 hypercall_memfree(void)
268 {
269 	kmem_free((vm_offset_t)hypercall_context.hc_addr, PAGE_SIZE);
270 	hypercall_context.hc_addr = NULL;
271 }
272 
273 static void
hypercall_create(void * arg __unused)274 hypercall_create(void *arg __unused)
275 {
276 	uint64_t hc, hc_orig;
277 
278 	if (vm_guest != VM_GUEST_HV)
279 		return;
280 
281 	/*
282 	 * NOTE:
283 	 * - busdma(9), i.e. hyperv_dmamem APIs, can _not_ be used due to
284 	 *   the NX bit.
285 	 * - Assume kmem_malloc() returns properly aligned memory.
286 	 */
287 	hypercall_context.hc_addr = (void *)kmem_malloc(PAGE_SIZE, M_EXEC |
288 	    M_WAITOK);
289 	hypercall_context.hc_paddr = vtophys(hypercall_context.hc_addr);
290 
291 	/* Get the 'reserved' bits, which requires preservation. */
292 	hc_orig = rdmsr(MSR_HV_HYPERCALL);
293 
294 	/*
295 	 * Setup the Hypercall page.
296 	 *
297 	 * NOTE: 'reserved' bits MUST be preserved.
298 	 */
299 	hc = ((hypercall_context.hc_paddr >> PAGE_SHIFT) <<
300 	    MSR_HV_HYPERCALL_PGSHIFT) |
301 	    (hc_orig & MSR_HV_HYPERCALL_RSVD_MASK) |
302 	    MSR_HV_HYPERCALL_ENABLE;
303 	wrmsr(MSR_HV_HYPERCALL, hc);
304 
305 	/*
306 	 * Confirm that Hypercall page did get setup.
307 	 */
308 	hc = rdmsr(MSR_HV_HYPERCALL);
309 	if ((hc & MSR_HV_HYPERCALL_ENABLE) == 0) {
310 		printf("hyperv: Hypercall setup failed\n");
311 		hypercall_memfree();
312 		/* Can't perform any Hyper-V specific actions */
313 		vm_guest = VM_GUEST_VM;
314 		return;
315 	}
316 	if (bootverbose)
317 		printf("hyperv: Hypercall created\n");
318 }
319 SYSINIT(hypercall_ctor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_create, NULL);
320 
321 static void
hypercall_destroy(void * arg __unused)322 hypercall_destroy(void *arg __unused)
323 {
324 	uint64_t hc;
325 
326 	if (hypercall_context.hc_addr == NULL)
327 		return;
328 
329 	/* Disable Hypercall */
330 	hc = rdmsr(MSR_HV_HYPERCALL);
331 	wrmsr(MSR_HV_HYPERCALL, (hc & MSR_HV_HYPERCALL_RSVD_MASK));
332 	hypercall_memfree();
333 
334 	if (bootverbose)
335 		printf("hyperv: Hypercall destroyed\n");
336 }
337 SYSUNINIT(hypercall_dtor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_destroy,
338     NULL);
339