xref: /freebsd/sys/x86/xen/hvm.c (revision d34048812292b714a0bf99967270d18fe3097c62)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008, 2013 Citrix Systems, Inc.
5  * Copyright (c) 2012 Spectra Logic Corporation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following 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 AND CONTRIBUTORS AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/proc.h>
38 #include <sys/smp.h>
39 #include <sys/systm.h>
40 
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 #include <vm/vm_param.h>
44 
45 #include <dev/pci/pcivar.h>
46 
47 #include <machine/cpufunc.h>
48 #include <machine/cpu.h>
49 #include <machine/smp.h>
50 
51 #include <x86/apicreg.h>
52 
53 #include <xen/xen-os.h>
54 #include <xen/features.h>
55 #include <xen/gnttab.h>
56 #include <xen/hypervisor.h>
57 #include <xen/hvm.h>
58 #include <xen/xen_intr.h>
59 
60 #include <xen/interface/arch-x86/cpuid.h>
61 #include <xen/interface/hvm/params.h>
62 #include <xen/interface/vcpu.h>
63 
64 /*--------------------------- Forward Declarations ---------------------------*/
65 static void xen_hvm_cpu_init(void);
66 
67 /*-------------------------------- Global Data -------------------------------*/
68 enum xen_domain_type xen_domain_type = XEN_NATIVE;
69 
70 #ifdef SMP
71 struct cpu_ops xen_hvm_cpu_ops = {
72 	.cpu_init	= xen_hvm_cpu_init,
73 	.cpu_resume	= xen_hvm_cpu_init
74 };
75 #endif
76 
77 static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support");
78 
79 /**
80  * If non-zero, the hypervisor has been configured to use a direct
81  * IDT event callback for interrupt injection.
82  */
83 int xen_vector_callback_enabled;
84 
85 /**
86  * Start info flags. ATM this only used to store the initial domain flag for
87  * PVHv2, and it's always empty for HVM guests.
88  */
89 uint32_t hvm_start_flags;
90 
91 /*------------------------------- Per-CPU Data -------------------------------*/
92 DPCPU_DEFINE(struct vcpu_info, vcpu_local_info);
93 DPCPU_DEFINE(struct vcpu_info *, vcpu_info);
94 
95 /*------------------ Hypervisor Access Shared Memory Regions -----------------*/
96 shared_info_t *HYPERVISOR_shared_info;
97 
98 /*------------------------------ Sysctl tunables -----------------------------*/
99 int xen_disable_pv_disks = 0;
100 int xen_disable_pv_nics = 0;
101 TUNABLE_INT("hw.xen.disable_pv_disks", &xen_disable_pv_disks);
102 TUNABLE_INT("hw.xen.disable_pv_nics", &xen_disable_pv_nics);
103 
104 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/
105 
106 static uint32_t cpuid_base;
107 
108 static uint32_t
109 xen_hvm_cpuid_base(void)
110 {
111 	uint32_t base, regs[4];
112 
113 	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
114 		do_cpuid(base, regs);
115 		if (!memcmp("XenVMMXenVMM", &regs[1], 12)
116 		    && (regs[0] - base) >= 2)
117 			return (base);
118 	}
119 	return (0);
120 }
121 
122 static void
123 hypervisor_quirks(unsigned int major, unsigned int minor)
124 {
125 #ifdef SMP
126 	if (((major < 4) || (major == 4 && minor <= 5)) &&
127 	    msix_disable_migration == -1) {
128 		/*
129 		 * Xen hypervisors prior to 4.6.0 do not properly
130 		 * handle updates to enabled MSI-X table entries,
131 		 * so disable MSI-X interrupt migration in that
132 		 * case.
133 		 */
134 		if (bootverbose)
135 			printf(
136 "Disabling MSI-X interrupt migration due to Xen hypervisor bug.\n"
137 "Set machdep.msix_disable_migration=0 to forcefully enable it.\n");
138 		msix_disable_migration = 1;
139 	}
140 #endif
141 }
142 
143 static void
144 hypervisor_version(void)
145 {
146 	uint32_t regs[4];
147 	int major, minor;
148 
149 	do_cpuid(cpuid_base + 1, regs);
150 
151 	major = regs[0] >> 16;
152 	minor = regs[0] & 0xffff;
153 	printf("XEN: Hypervisor version %d.%d detected.\n", major, minor);
154 
155 	hypervisor_quirks(major, minor);
156 }
157 
158 /*
159  * Allocate and fill in the hypcall page.
160  */
161 int
162 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
163 {
164 	uint32_t regs[4];
165 
166 	/* Legacy PVH will get here without the cpuid leaf being set. */
167 	if (cpuid_base == 0)
168 		cpuid_base = xen_hvm_cpuid_base();
169 	if (cpuid_base == 0)
170 		return (ENXIO);
171 
172 	if (xen_domain() && init_type == XEN_HVM_INIT_LATE) {
173 		/*
174 		 * If the domain type is already set we can assume that the
175 		 * hypercall page has been populated too, so just print the
176 		 * version (and apply any quirks) and exit.
177 		 */
178 		hypervisor_version();
179 		return 0;
180 	}
181 
182 	if (init_type == XEN_HVM_INIT_LATE)
183 		hypervisor_version();
184 
185 	/*
186 	 * Find the hypercall pages.
187 	 */
188 	do_cpuid(cpuid_base + 2, regs);
189 	if (regs[0] != 1)
190 		return (EINVAL);
191 
192 	wrmsr(regs[1], (init_type == XEN_HVM_INIT_EARLY)
193 	    ? ((vm_paddr_t)&hypercall_page - KERNBASE)
194 	    : vtophys(&hypercall_page));
195 
196 	return (0);
197 }
198 
199 static void
200 xen_hvm_init_shared_info_page(void)
201 {
202 	struct xen_add_to_physmap xatp;
203 
204 	if (xen_pv_domain()) {
205 		/*
206 		 * Already setup in the PV case, shared_info is passed inside
207 		 * of the start_info struct at start of day.
208 		 */
209 		return;
210 	}
211 
212 	if (HYPERVISOR_shared_info == NULL) {
213 		HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT);
214 		if (HYPERVISOR_shared_info == NULL)
215 			panic("Unable to allocate Xen shared info page");
216 	}
217 
218 	xatp.domid = DOMID_SELF;
219 	xatp.idx = 0;
220 	xatp.space = XENMAPSPACE_shared_info;
221 	xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT;
222 	if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
223 		panic("HYPERVISOR_memory_op failed");
224 }
225 
226 /*
227  * Tell the hypervisor how to contact us for event channel callbacks.
228  */
229 void
230 xen_hvm_set_callback(device_t dev)
231 {
232 	struct xen_hvm_param xhp;
233 	int irq;
234 
235 	if (xen_vector_callback_enabled)
236 		return;
237 
238 	xhp.domid = DOMID_SELF;
239 	xhp.index = HVM_PARAM_CALLBACK_IRQ;
240 	if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
241 		int error;
242 
243 		xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
244 		error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
245 		if (error == 0) {
246 			xen_vector_callback_enabled = 1;
247 			return;
248 		}
249 		printf("Xen HVM callback vector registration failed (%d). "
250 		    "Falling back to emulated device interrupt\n", error);
251 	}
252 	xen_vector_callback_enabled = 0;
253 	if (dev == NULL) {
254 		/*
255 		 * Called from early boot or resume.
256 		 * xenpci will invoke us again later.
257 		 */
258 		return;
259 	}
260 
261 	irq = pci_get_irq(dev);
262 	if (irq < 16) {
263 		xhp.value = HVM_CALLBACK_GSI(irq);
264 	} else {
265 		u_int slot;
266 		u_int pin;
267 
268 		slot = pci_get_slot(dev);
269 		pin = pci_get_intpin(dev) - 1;
270 		xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
271 	}
272 
273 	if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
274 		panic("Can't set evtchn callback");
275 }
276 
277 #define	XEN_MAGIC_IOPORT 0x10
278 enum {
279 	XMI_MAGIC			 = 0x49d2,
280 	XMI_UNPLUG_IDE_DISKS		 = 0x01,
281 	XMI_UNPLUG_NICS			 = 0x02,
282 	XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
283 };
284 
285 static void
286 xen_hvm_disable_emulated_devices(void)
287 {
288 	u_short disable_devs = 0;
289 
290 	if (xen_pv_domain()) {
291 		/*
292 		 * No emulated devices in the PV case, so no need to unplug
293 		 * anything.
294 		 */
295 		if (xen_disable_pv_disks != 0 || xen_disable_pv_nics != 0)
296 			printf("PV devices cannot be disabled in PV guests\n");
297 		return;
298 	}
299 
300 	if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
301 		return;
302 
303 	if (xen_disable_pv_disks == 0) {
304 		if (bootverbose)
305 			printf("XEN: disabling emulated disks\n");
306 		disable_devs |= XMI_UNPLUG_IDE_DISKS;
307 	}
308 	if (xen_disable_pv_nics == 0) {
309 		if (bootverbose)
310 			printf("XEN: disabling emulated nics\n");
311 		disable_devs |= XMI_UNPLUG_NICS;
312 	}
313 
314 	if (disable_devs != 0)
315 		outw(XEN_MAGIC_IOPORT, disable_devs);
316 }
317 
318 static void
319 xen_hvm_init(enum xen_hvm_init_type init_type)
320 {
321 	int error;
322 	int i;
323 
324 	if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
325 		return;
326 
327 	error = xen_hvm_init_hypercall_stubs(init_type);
328 
329 	switch (init_type) {
330 	case XEN_HVM_INIT_LATE:
331 		if (error != 0)
332 			return;
333 
334 		/*
335 		 * If xen_domain_type is not set at this point
336 		 * it means we are inside a (PV)HVM guest, because
337 		 * for PVH the guest type is set much earlier
338 		 * (see hammer_time_xen).
339 		 */
340 		if (!xen_domain()) {
341 			xen_domain_type = XEN_HVM_DOMAIN;
342 			vm_guest = VM_GUEST_XEN;
343 		}
344 
345 		setup_xen_features();
346 #ifdef SMP
347 		cpu_ops = xen_hvm_cpu_ops;
348 #endif
349 		break;
350 	case XEN_HVM_INIT_RESUME:
351 		if (error != 0)
352 			panic("Unable to init Xen hypercall stubs on resume");
353 
354 		/* Clear stale vcpu_info. */
355 		CPU_FOREACH(i)
356 			DPCPU_ID_SET(i, vcpu_info, NULL);
357 		break;
358 	default:
359 		panic("Unsupported HVM initialization type");
360 	}
361 
362 	xen_vector_callback_enabled = 0;
363 	xen_hvm_set_callback(NULL);
364 
365 	/*
366 	 * On (PV)HVM domains we need to request the hypervisor to
367 	 * fill the shared info page, for PVH guest the shared_info page
368 	 * is passed inside the start_info struct and is already set, so this
369 	 * functions are no-ops.
370 	 */
371 	xen_hvm_init_shared_info_page();
372 	xen_hvm_disable_emulated_devices();
373 }
374 
375 void
376 xen_hvm_suspend(void)
377 {
378 }
379 
380 void
381 xen_hvm_resume(bool suspend_cancelled)
382 {
383 
384 	xen_hvm_init(suspend_cancelled ?
385 	    XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
386 
387 	/* Register vcpu_info area for CPU#0. */
388 	xen_hvm_cpu_init();
389 }
390 
391 static void
392 xen_hvm_sysinit(void *arg __unused)
393 {
394 	xen_hvm_init(XEN_HVM_INIT_LATE);
395 }
396 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL);
397 
398 static void
399 xen_hvm_cpu_init(void)
400 {
401 	struct vcpu_register_vcpu_info info;
402 	struct vcpu_info *vcpu_info;
403 	uint32_t regs[4];
404 	int cpu, rc;
405 
406 	if (!xen_domain())
407 		return;
408 
409 	if (DPCPU_GET(vcpu_info) != NULL) {
410 		/*
411 		 * vcpu_info is already set.  We're resuming
412 		 * from a failed migration and our pre-suspend
413 		 * configuration is still valid.
414 		 */
415 		return;
416 	}
417 
418 	/*
419 	 * Set vCPU ID. If available fetch the ID from CPUID, if not just use
420 	 * the ACPI ID.
421 	 */
422 	KASSERT(cpuid_base != 0, ("Invalid base Xen CPUID leaf"));
423 	cpuid_count(cpuid_base + 4, 0, regs);
424 	KASSERT((regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ||
425 	    !xen_pv_domain(),
426 	    ("Xen PV domain without vcpu_id in cpuid"));
427 	PCPU_SET(vcpu_id, (regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ?
428 	    regs[1] : PCPU_GET(acpi_id));
429 
430 	/*
431 	 * Set the vCPU info.
432 	 *
433 	 * NB: the vCPU info for vCPUs < 32 can be fetched from the shared info
434 	 * page, but in order to make sure the mapping code is correct always
435 	 * attempt to map the vCPU info at a custom place.
436 	 */
437 	vcpu_info = DPCPU_PTR(vcpu_local_info);
438 	cpu = PCPU_GET(vcpu_id);
439 	info.mfn = vtophys(vcpu_info) >> PAGE_SHIFT;
440 	info.offset = vtophys(vcpu_info) - trunc_page(vtophys(vcpu_info));
441 
442 	rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
443 	if (rc != 0)
444 		DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
445 	else
446 		DPCPU_SET(vcpu_info, vcpu_info);
447 }
448 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL);
449 
450 /* HVM/PVH start_info accessors */
451 static vm_paddr_t
452 hvm_get_xenstore_mfn(void)
453 {
454 
455 	return (hvm_get_parameter(HVM_PARAM_STORE_PFN));
456 }
457 
458 static evtchn_port_t
459 hvm_get_xenstore_evtchn(void)
460 {
461 
462 	return (hvm_get_parameter(HVM_PARAM_STORE_EVTCHN));
463 }
464 
465 static vm_paddr_t
466 hvm_get_console_mfn(void)
467 {
468 
469 	return (hvm_get_parameter(HVM_PARAM_CONSOLE_PFN));
470 }
471 
472 static evtchn_port_t
473 hvm_get_console_evtchn(void)
474 {
475 
476 	return (hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN));
477 }
478 
479 static uint32_t
480 hvm_get_start_flags(void)
481 {
482 
483 	return (hvm_start_flags);
484 }
485 
486 struct hypervisor_info hypervisor_info = {
487 	.get_xenstore_mfn		= hvm_get_xenstore_mfn,
488 	.get_xenstore_evtchn		= hvm_get_xenstore_evtchn,
489 	.get_console_mfn		= hvm_get_console_mfn,
490 	.get_console_evtchn		= hvm_get_console_evtchn,
491 	.get_start_flags		= hvm_get_start_flags,
492 };
493