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