xref: /freebsd-13.1/sys/dev/hyperv/vmbus/hyperv.c (revision 236764b1)
1 /*-
2  * Copyright (c) 2009-2012,2016 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 Hypver-V/Azure
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/pcpu.h>
39 #include <sys/timetc.h>
40 #include <machine/bus.h>
41 #include <machine/md_var.h>
42 #include <vm/vm.h>
43 #include <vm/vm_param.h>
44 #include <vm/pmap.h>
45 
46 #include <dev/hyperv/include/hyperv_busdma.h>
47 #include <dev/hyperv/vmbus/hv_vmbus_priv.h>
48 #include <dev/hyperv/vmbus/hyperv_machdep.h>
49 #include <dev/hyperv/vmbus/hyperv_reg.h>
50 #include <dev/hyperv/vmbus/hyperv_var.h>
51 #include <dev/hyperv/vmbus/vmbus_var.h>
52 
53 #define HYPERV_FREEBSD_BUILD		0ULL
54 #define HYPERV_FREEBSD_VERSION		((uint64_t)__FreeBSD_version)
55 #define HYPERV_FREEBSD_OSID		0ULL
56 
57 #define MSR_HV_GUESTID_BUILD_FREEBSD	\
58 	(HYPERV_FREEBSD_BUILD & MSR_HV_GUESTID_BUILD_MASK)
59 #define MSR_HV_GUESTID_VERSION_FREEBSD	\
60 	((HYPERV_FREEBSD_VERSION << MSR_HV_GUESTID_VERSION_SHIFT) & \
61 	 MSR_HV_GUESTID_VERSION_MASK)
62 #define MSR_HV_GUESTID_OSID_FREEBSD	\
63 	((HYPERV_FREEBSD_OSID << MSR_HV_GUESTID_OSID_SHIFT) & \
64 	 MSR_HV_GUESTID_OSID_MASK)
65 
66 #define MSR_HV_GUESTID_FREEBSD		\
67 	(MSR_HV_GUESTID_BUILD_FREEBSD |	\
68 	 MSR_HV_GUESTID_VERSION_FREEBSD | \
69 	 MSR_HV_GUESTID_OSID_FREEBSD |	\
70 	 MSR_HV_GUESTID_OSTYPE_FREEBSD)
71 
72 struct hypercall_ctx {
73 	void			*hc_addr;
74 	struct hyperv_dma	hc_dma;
75 };
76 
77 static u_int	hyperv_get_timecount(struct timecounter *tc);
78 
79 u_int		hyperv_features;
80 u_int		hyperv_recommends;
81 
82 static u_int	hyperv_pm_features;
83 static u_int	hyperv_features3;
84 
85 static struct timecounter	hyperv_timecounter = {
86 	.tc_get_timecount	= hyperv_get_timecount,
87 	.tc_poll_pps		= NULL,
88 	.tc_counter_mask	= 0xffffffff,
89 	.tc_frequency		= HYPERV_TIMER_FREQ,
90 	.tc_name		= "Hyper-V",
91 	.tc_quality		= 2000,
92 	.tc_flags		= 0,
93 	.tc_priv		= NULL
94 };
95 
96 static struct hypercall_ctx	hypercall_context;
97 
98 static u_int
99 hyperv_get_timecount(struct timecounter *tc __unused)
100 {
101 	return rdmsr(MSR_HV_TIME_REF_COUNT);
102 }
103 
104 /**
105  * @brief Invoke the specified hypercall
106  */
107 static uint64_t
108 hv_vmbus_do_hypercall(uint64_t value, void *input, void *output)
109 {
110 	uint64_t in_paddr = 0, out_paddr = 0;
111 
112 	if (input != NULL)
113 		in_paddr = hv_get_phys_addr(input);
114 	if (output != NULL)
115 		out_paddr = hv_get_phys_addr(output);
116 
117 	return hypercall_md(hypercall_context.hc_addr, value,
118 	    in_paddr, out_paddr);
119 }
120 
121 uint64_t
122 hypercall_post_message(bus_addr_t msg_paddr)
123 {
124 	return hypercall_md(hypercall_context.hc_addr,
125 	    HYPERCALL_POST_MESSAGE, msg_paddr, 0);
126 }
127 
128 /**
129  * @brief Post a message using the hypervisor message IPC.
130  * (This involves a hypercall.)
131  */
132 hv_vmbus_status
133 hv_vmbus_post_msg_via_msg_ipc(
134 	hv_vmbus_connection_id	connection_id,
135 	hv_vmbus_msg_type	message_type,
136 	void*			payload,
137 	size_t			payload_size)
138 {
139 	struct alignedinput {
140 	    uint64_t alignment8;
141 	    hv_vmbus_input_post_message msg;
142 	};
143 
144 	hv_vmbus_input_post_message*	aligned_msg;
145 	hv_vmbus_status 		status;
146 	size_t				addr;
147 
148 	if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
149 	    return (EMSGSIZE);
150 
151 	addr = (size_t) malloc(sizeof(struct alignedinput), M_DEVBUF,
152 			    M_ZERO | M_NOWAIT);
153 	KASSERT(addr != 0,
154 	    ("Error VMBUS: malloc failed to allocate message buffer!"));
155 	if (addr == 0)
156 	    return (ENOMEM);
157 
158 	aligned_msg = (hv_vmbus_input_post_message*)
159 	    (HV_ALIGN_UP(addr, HV_HYPERCALL_PARAM_ALIGN));
160 
161 	aligned_msg->connection_id = connection_id;
162 	aligned_msg->message_type = message_type;
163 	aligned_msg->payload_size = payload_size;
164 	memcpy((void*) aligned_msg->payload, payload, payload_size);
165 
166 	status = hv_vmbus_do_hypercall(
167 		    HV_CALL_POST_MESSAGE, aligned_msg, 0) & 0xFFFF;
168 
169 	free((void *) addr, M_DEVBUF);
170 	return (status);
171 }
172 
173 /**
174  * @brief Signal an event on the specified connection using the hypervisor
175  * event IPC. (This involves a hypercall.)
176  */
177 hv_vmbus_status
178 hv_vmbus_signal_event(void *con_id)
179 {
180 	hv_vmbus_status status;
181 
182 	status = hv_vmbus_do_hypercall(
183 		    HV_CALL_SIGNAL_EVENT,
184 		    con_id,
185 		    0) & 0xFFFF;
186 
187 	return (status);
188 }
189 
190 int
191 hyperv_guid2str(const struct hv_guid *guid, char *buf, size_t sz)
192 {
193 	const uint8_t *d = guid->data;
194 
195 	return snprintf(buf, sz, "%02x%02x%02x%02x-"
196 	    "%02x%02x-%02x%02x-%02x%02x-"
197 	    "%02x%02x%02x%02x%02x%02x",
198 	    d[3], d[2], d[1], d[0],
199 	    d[5], d[4], d[7], d[6], d[8], d[9],
200 	    d[10], d[11], d[12], d[13], d[14], d[15]);
201 }
202 
203 static bool
204 hyperv_identify(void)
205 {
206 	u_int regs[4];
207 	unsigned int maxleaf;
208 
209 	if (vm_guest != VM_GUEST_HV)
210 		return (false);
211 
212 	do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
213 	maxleaf = regs[0];
214 	if (maxleaf < CPUID_LEAF_HV_LIMITS)
215 		return (false);
216 
217 	do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
218 	if (regs[0] != CPUID_HV_IFACE_HYPERV)
219 		return (false);
220 
221 	do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
222 	if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) {
223 		/*
224 		 * Hyper-V w/o Hypercall is impossible; someone
225 		 * is faking Hyper-V.
226 		 */
227 		return (false);
228 	}
229 	hyperv_features = regs[0];
230 	hyperv_pm_features = regs[2];
231 	hyperv_features3 = regs[3];
232 
233 	do_cpuid(CPUID_LEAF_HV_IDENTITY, regs);
234 	printf("Hyper-V Version: %d.%d.%d [SP%d]\n",
235 	    regs[1] >> 16, regs[1] & 0xffff, regs[0], regs[2]);
236 
237 	printf("  Features=0x%b\n", hyperv_features,
238 	    "\020"
239 	    "\001VPRUNTIME"	/* MSR_HV_VP_RUNTIME */
240 	    "\002TMREFCNT"	/* MSR_HV_TIME_REF_COUNT */
241 	    "\003SYNIC"		/* MSRs for SynIC */
242 	    "\004SYNTM"		/* MSRs for SynTimer */
243 	    "\005APIC"		/* MSR_HV_{EOI,ICR,TPR} */
244 	    "\006HYPERCALL"	/* MSR_HV_{GUEST_OS_ID,HYPERCALL} */
245 	    "\007VPINDEX"	/* MSR_HV_VP_INDEX */
246 	    "\010RESET"		/* MSR_HV_RESET */
247 	    "\011STATS"		/* MSR_HV_STATS_ */
248 	    "\012REFTSC"	/* MSR_HV_REFERENCE_TSC */
249 	    "\013IDLE"		/* MSR_HV_GUEST_IDLE */
250 	    "\014TMFREQ"	/* MSR_HV_{TSC,APIC}_FREQUENCY */
251 	    "\015DEBUG");	/* MSR_HV_SYNTH_DEBUG_ */
252 	printf("  PM Features=0x%b [C%u]\n",
253 	    (hyperv_pm_features & ~CPUPM_HV_CSTATE_MASK),
254 	    "\020"
255 	    "\005C3HPET",	/* HPET is required for C3 state */
256 	    CPUPM_HV_CSTATE(hyperv_pm_features));
257 	printf("  Features3=0x%b\n", hyperv_features3,
258 	    "\020"
259 	    "\001MWAIT"		/* MWAIT */
260 	    "\002DEBUG"		/* guest debug support */
261 	    "\003PERFMON"	/* performance monitor */
262 	    "\004PCPUDPE"	/* physical CPU dynamic partition event */
263 	    "\005XMMHC"		/* hypercall input through XMM regs */
264 	    "\006IDLE"		/* guest idle support */
265 	    "\007SLEEP"		/* hypervisor sleep support */
266 	    "\010NUMA"		/* NUMA distance query support */
267 	    "\011TMFREQ"	/* timer frequency query (TSC, LAPIC) */
268 	    "\012SYNCMC"	/* inject synthetic machine checks */
269 	    "\013CRASH"		/* MSRs for guest crash */
270 	    "\014DEBUGMSR"	/* MSRs for guest debug */
271 	    "\015NPIEP"		/* NPIEP */
272 	    "\016HVDIS");	/* disabling hypervisor */
273 
274 	do_cpuid(CPUID_LEAF_HV_RECOMMENDS, regs);
275 	hyperv_recommends = regs[0];
276 	if (bootverbose)
277 		printf("  Recommends: %08x %08x\n", regs[0], regs[1]);
278 
279 	do_cpuid(CPUID_LEAF_HV_LIMITS, regs);
280 	if (bootverbose) {
281 		printf("  Limits: Vcpu:%d Lcpu:%d Int:%d\n",
282 		    regs[0], regs[1], regs[2]);
283 	}
284 
285 	if (maxleaf >= CPUID_LEAF_HV_HWFEATURES) {
286 		do_cpuid(CPUID_LEAF_HV_HWFEATURES, regs);
287 		if (bootverbose) {
288 			printf("  HW Features: %08x, AMD: %08x\n",
289 			    regs[0], regs[3]);
290 		}
291 	}
292 
293 	return (true);
294 }
295 
296 static void
297 hyperv_init(void *dummy __unused)
298 {
299 	if (!hyperv_identify()) {
300 		/* Not Hyper-V; reset guest id to the generic one. */
301 		if (vm_guest == VM_GUEST_HV)
302 			vm_guest = VM_GUEST_VM;
303 		return;
304 	}
305 
306 	/* Set guest id */
307 	wrmsr(MSR_HV_GUEST_OS_ID, MSR_HV_GUESTID_FREEBSD);
308 
309 	if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) {
310 		/* Register Hyper-V timecounter */
311 		tc_init(&hyperv_timecounter);
312 	}
313 }
314 SYSINIT(hyperv_initialize, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, hyperv_init,
315     NULL);
316 
317 static void
318 hypercall_memfree(void)
319 {
320 	hyperv_dmamem_free(&hypercall_context.hc_dma,
321 	    hypercall_context.hc_addr);
322 	hypercall_context.hc_addr = NULL;
323 }
324 
325 static void
326 hypercall_create(void *arg __unused)
327 {
328 	uint64_t hc, hc_orig;
329 
330 	if (vm_guest != VM_GUEST_HV)
331 		return;
332 
333 	hypercall_context.hc_addr = hyperv_dmamem_alloc(NULL, PAGE_SIZE, 0,
334 	    PAGE_SIZE, &hypercall_context.hc_dma, BUS_DMA_WAITOK);
335 	if (hypercall_context.hc_addr == NULL) {
336 		printf("hyperv: Hypercall page allocation failed\n");
337 		/* Can't perform any Hyper-V specific actions */
338 		vm_guest = VM_GUEST_VM;
339 		return;
340 	}
341 
342 	/* Get the 'reserved' bits, which requires preservation. */
343 	hc_orig = rdmsr(MSR_HV_HYPERCALL);
344 
345 	/*
346 	 * Setup the Hypercall page.
347 	 *
348 	 * NOTE: 'reserved' bits MUST be preserved.
349 	 */
350 	hc = ((hypercall_context.hc_dma.hv_paddr >> PAGE_SHIFT) <<
351 	    MSR_HV_HYPERCALL_PGSHIFT) |
352 	    (hc_orig & MSR_HV_HYPERCALL_RSVD_MASK) |
353 	    MSR_HV_HYPERCALL_ENABLE;
354 	wrmsr(MSR_HV_HYPERCALL, hc);
355 
356 	/*
357 	 * Confirm that Hypercall page did get setup.
358 	 */
359 	hc = rdmsr(MSR_HV_HYPERCALL);
360 	if ((hc & MSR_HV_HYPERCALL_ENABLE) == 0) {
361 		printf("hyperv: Hypercall setup failed\n");
362 		hypercall_memfree();
363 		/* Can't perform any Hyper-V specific actions */
364 		vm_guest = VM_GUEST_VM;
365 		return;
366 	}
367 	if (bootverbose)
368 		printf("hyperv: Hypercall created\n");
369 }
370 SYSINIT(hypercall_ctor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_create, NULL);
371 
372 static void
373 hypercall_destroy(void *arg __unused)
374 {
375 	uint64_t hc;
376 
377 	if (hypercall_context.hc_addr == NULL)
378 		return;
379 
380 	/* Disable Hypercall */
381 	hc = rdmsr(MSR_HV_HYPERCALL);
382 	wrmsr(MSR_HV_HYPERCALL, (hc & MSR_HV_HYPERCALL_RSVD_MASK));
383 	hypercall_memfree();
384 
385 	if (bootverbose)
386 		printf("hyperv: Hypercall destroyed\n");
387 }
388 SYSUNINIT(hypercall_dtor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_destroy,
389     NULL);
390