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