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