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