xref: /freebsd/sys/x86/xen/hvm.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/error.h>
55 #include <xen/features.h>
56 #include <xen/gnttab.h>
57 #include <xen/hypervisor.h>
58 #include <xen/hvm.h>
59 #include <xen/xen_intr.h>
60 
61 #include <contrib/xen/arch-x86/cpuid.h>
62 #include <contrib/xen/hvm/params.h>
63 #include <contrib/xen/vcpu.h>
64 
65 /*--------------------------- Forward Declarations ---------------------------*/
66 static void xen_hvm_cpu_init(void);
67 
68 /*-------------------------------- Global Data -------------------------------*/
69 #ifdef SMP
70 struct cpu_ops xen_hvm_cpu_ops = {
71 	.cpu_init	= xen_hvm_cpu_init,
72 	.cpu_resume	= xen_hvm_cpu_init
73 };
74 #endif
75 
76 static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support");
77 
78 /**
79  * If non-zero, the hypervisor has been configured to use a direct
80  * IDT event callback for interrupt injection.
81  */
82 int xen_vector_callback_enabled;
83 
84 /**
85  * Signal whether the vector injected for the event channel upcall requires to
86  * be EOI'ed on the local APIC.
87  */
88 bool xen_evtchn_needs_ack;
89 
90 /*------------------------------- Per-CPU Data -------------------------------*/
91 DPCPU_DECLARE(struct vcpu_info *, vcpu_info);
92 
93 /*------------------------------ Sysctl tunables -----------------------------*/
94 int xen_disable_pv_disks = 0;
95 int xen_disable_pv_nics = 0;
96 TUNABLE_INT("hw.xen.disable_pv_disks", &xen_disable_pv_disks);
97 TUNABLE_INT("hw.xen.disable_pv_nics", &xen_disable_pv_nics);
98 
99 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/
100 
101 uint32_t xen_cpuid_base;
102 
103 static uint32_t
104 xen_hvm_cpuid_base(void)
105 {
106 	uint32_t base, regs[4];
107 
108 	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
109 		do_cpuid(base, regs);
110 		if (!memcmp("XenVMMXenVMM", &regs[1], 12)
111 		    && (regs[0] - base) >= 2)
112 			return (base);
113 	}
114 	return (0);
115 }
116 
117 static void
118 hypervisor_quirks(unsigned int major, unsigned int minor)
119 {
120 #ifdef SMP
121 	if (((major < 4) || (major == 4 && minor <= 5)) &&
122 	    msix_disable_migration == -1) {
123 		/*
124 		 * Xen hypervisors prior to 4.6.0 do not properly
125 		 * handle updates to enabled MSI-X table entries,
126 		 * so disable MSI-X interrupt migration in that
127 		 * case.
128 		 */
129 		if (bootverbose)
130 			printf(
131 "Disabling MSI-X interrupt migration due to Xen hypervisor bug.\n"
132 "Set machdep.msix_disable_migration=0 to forcefully enable it.\n");
133 		msix_disable_migration = 1;
134 	}
135 #endif
136 }
137 
138 static void
139 hypervisor_version(void)
140 {
141 	uint32_t regs[4];
142 	int major, minor;
143 
144 	do_cpuid(xen_cpuid_base + 1, regs);
145 
146 	major = regs[0] >> 16;
147 	minor = regs[0] & 0xffff;
148 	printf("XEN: Hypervisor version %d.%d detected.\n", major, minor);
149 
150 	hypervisor_quirks(major, minor);
151 }
152 
153 /*
154  * Allocate and fill in the hypcall page.
155  */
156 int
157 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
158 {
159 	uint32_t regs[4];
160 
161 	/* Legacy PVH will get here without the cpuid leaf being set. */
162 	if (xen_cpuid_base == 0)
163 		xen_cpuid_base = xen_hvm_cpuid_base();
164 	if (xen_cpuid_base == 0)
165 		return (ENXIO);
166 
167 	if (xen_domain() && init_type == XEN_HVM_INIT_LATE) {
168 		/*
169 		 * If the domain type is already set we can assume that the
170 		 * hypercall page has been populated too, so just print the
171 		 * version (and apply any quirks) and exit.
172 		 */
173 		hypervisor_version();
174 		return 0;
175 	}
176 
177 	if (init_type == XEN_HVM_INIT_LATE)
178 		hypervisor_version();
179 
180 	/*
181 	 * Find the hypercall pages.
182 	 */
183 	do_cpuid(xen_cpuid_base + 2, regs);
184 	if (regs[0] != 1)
185 		return (EINVAL);
186 
187 	wrmsr(regs[1], (init_type == XEN_HVM_INIT_EARLY)
188 	    ? (vm_paddr_t)((uintptr_t)&hypercall_page - KERNBASE)
189 	    : vtophys(&hypercall_page));
190 
191 	return (0);
192 }
193 
194 static void
195 xen_hvm_init_shared_info_page(void)
196 {
197 	struct xen_add_to_physmap xatp;
198 
199 	if (xen_pv_domain()) {
200 		/*
201 		 * Already setup in the PV case, shared_info is passed inside
202 		 * of the start_info struct at start of day.
203 		 */
204 		return;
205 	}
206 
207 	if (HYPERVISOR_shared_info == NULL) {
208 		HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT);
209 		if (HYPERVISOR_shared_info == NULL)
210 			panic("Unable to allocate Xen shared info page");
211 	}
212 
213 	xatp.domid = DOMID_SELF;
214 	xatp.idx = 0;
215 	xatp.space = XENMAPSPACE_shared_info;
216 	xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT;
217 	if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
218 		panic("HYPERVISOR_memory_op failed");
219 }
220 
221 static int
222 set_percpu_callback(unsigned int vcpu)
223 {
224 	struct xen_hvm_evtchn_upcall_vector vec;
225 	int error;
226 
227 	vec.vcpu = vcpu;
228 	vec.vector = IDT_EVTCHN;
229 	error = HYPERVISOR_hvm_op(HVMOP_set_evtchn_upcall_vector, &vec);
230 
231 	return (error != 0 ? xen_translate_error(error) : 0);
232 }
233 
234 /*
235  * Tell the hypervisor how to contact us for event channel callbacks.
236  */
237 void
238 xen_hvm_set_callback(device_t dev)
239 {
240 	struct xen_hvm_param xhp;
241 	int irq;
242 
243 	if (xen_vector_callback_enabled)
244 		return;
245 
246 	xhp.domid = DOMID_SELF;
247 	xhp.index = HVM_PARAM_CALLBACK_IRQ;
248 	if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
249 		int error;
250 
251 		error = set_percpu_callback(0);
252 		if (error == 0) {
253 			xen_evtchn_needs_ack = true;
254 			/* Trick toolstack to think we are enlightened */
255 			xhp.value = 1;
256 		} else
257 			xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
258 		error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
259 		if (error == 0) {
260 			xen_vector_callback_enabled = 1;
261 			return;
262 		} else if (xen_evtchn_needs_ack)
263 			panic("Unable to setup fake HVM param: %d", error);
264 
265 		printf("Xen HVM callback vector registration failed (%d). "
266 		    "Falling back to emulated device interrupt\n", error);
267 	}
268 	xen_vector_callback_enabled = 0;
269 	if (dev == NULL) {
270 		/*
271 		 * Called from early boot or resume.
272 		 * xenpci will invoke us again later.
273 		 */
274 		return;
275 	}
276 
277 	irq = pci_get_irq(dev);
278 	if (irq < 16) {
279 		xhp.value = HVM_CALLBACK_GSI(irq);
280 	} else {
281 		u_int slot;
282 		u_int pin;
283 
284 		slot = pci_get_slot(dev);
285 		pin = pci_get_intpin(dev) - 1;
286 		xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
287 	}
288 
289 	if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
290 		panic("Can't set evtchn callback");
291 }
292 
293 #define	XEN_MAGIC_IOPORT 0x10
294 enum {
295 	XMI_MAGIC			 = 0x49d2,
296 	XMI_UNPLUG_IDE_DISKS		 = 0x01,
297 	XMI_UNPLUG_NICS			 = 0x02,
298 	XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
299 };
300 
301 static void
302 xen_hvm_disable_emulated_devices(void)
303 {
304 	u_short disable_devs = 0;
305 
306 	if (xen_pv_domain()) {
307 		/*
308 		 * No emulated devices in the PV case, so no need to unplug
309 		 * anything.
310 		 */
311 		if (xen_disable_pv_disks != 0 || xen_disable_pv_nics != 0)
312 			printf("PV devices cannot be disabled in PV guests\n");
313 		return;
314 	}
315 
316 	if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
317 		return;
318 
319 	if (xen_disable_pv_disks == 0) {
320 		if (bootverbose)
321 			printf("XEN: disabling emulated disks\n");
322 		disable_devs |= XMI_UNPLUG_IDE_DISKS;
323 	}
324 	if (xen_disable_pv_nics == 0) {
325 		if (bootverbose)
326 			printf("XEN: disabling emulated nics\n");
327 		disable_devs |= XMI_UNPLUG_NICS;
328 	}
329 
330 	if (disable_devs != 0)
331 		outw(XEN_MAGIC_IOPORT, disable_devs);
332 }
333 
334 static void
335 xen_hvm_init(enum xen_hvm_init_type init_type)
336 {
337 	int error;
338 	int i;
339 
340 	if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
341 		return;
342 
343 	error = xen_hvm_init_hypercall_stubs(init_type);
344 
345 	switch (init_type) {
346 	case XEN_HVM_INIT_LATE:
347 		if (error != 0)
348 			return;
349 
350 		/*
351 		 * If xen_domain_type is not set at this point
352 		 * it means we are inside a (PV)HVM guest, because
353 		 * for PVH the guest type is set much earlier
354 		 * (see hammer_time_xen).
355 		 */
356 		if (!xen_domain()) {
357 			xen_domain_type = XEN_HVM_DOMAIN;
358 			vm_guest = VM_GUEST_XEN;
359 		}
360 
361 		setup_xen_features();
362 #ifdef SMP
363 		cpu_ops = xen_hvm_cpu_ops;
364 #endif
365 		break;
366 	case XEN_HVM_INIT_RESUME:
367 		if (error != 0)
368 			panic("Unable to init Xen hypercall stubs on resume");
369 
370 		/* Clear stale vcpu_info. */
371 		CPU_FOREACH(i)
372 			DPCPU_ID_SET(i, vcpu_info, NULL);
373 		break;
374 	default:
375 		panic("Unsupported HVM initialization type");
376 	}
377 
378 	xen_vector_callback_enabled = 0;
379 	xen_evtchn_needs_ack = false;
380 	xen_hvm_set_callback(NULL);
381 
382 	/*
383 	 * On (PV)HVM domains we need to request the hypervisor to
384 	 * fill the shared info page, for PVH guest the shared_info page
385 	 * is passed inside the start_info struct and is already set, so this
386 	 * functions are no-ops.
387 	 */
388 	xen_hvm_init_shared_info_page();
389 	xen_hvm_disable_emulated_devices();
390 }
391 
392 void
393 xen_hvm_suspend(void)
394 {
395 }
396 
397 void
398 xen_hvm_resume(bool suspend_cancelled)
399 {
400 
401 	xen_hvm_init(suspend_cancelled ?
402 	    XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
403 
404 	/* Register vcpu_info area for CPU#0. */
405 	xen_hvm_cpu_init();
406 }
407 
408 static void
409 xen_hvm_sysinit(void *arg __unused)
410 {
411 	xen_hvm_init(XEN_HVM_INIT_LATE);
412 }
413 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL);
414 
415 static void
416 xen_hvm_cpu_init(void)
417 {
418 	uint32_t regs[4];
419 	int rc;
420 
421 	if (!xen_domain())
422 		return;
423 
424 	if (DPCPU_GET(vcpu_info) != NULL) {
425 		/*
426 		 * vcpu_info is already set.  We're resuming
427 		 * from a failed migration and our pre-suspend
428 		 * configuration is still valid.
429 		 */
430 		return;
431 	}
432 
433 	/*
434 	 * Set vCPU ID. If available fetch the ID from CPUID, if not just use
435 	 * the ACPI ID.
436 	 */
437 	KASSERT(xen_cpuid_base != 0, ("Invalid base Xen CPUID leaf"));
438 	cpuid_count(xen_cpuid_base + 4, 0, regs);
439 	KASSERT((regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ||
440 	    !xen_pv_domain(),
441 	    ("Xen PV domain without vcpu_id in cpuid"));
442 	PCPU_SET(vcpu_id, (regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ?
443 	    regs[1] : PCPU_GET(acpi_id));
444 
445 	if (xen_evtchn_needs_ack && !IS_BSP()) {
446 		/*
447 		 * Setup the per-vpcu event channel upcall vector. This is only
448 		 * required when using the new HVMOP_set_evtchn_upcall_vector
449 		 * hypercall, which allows using a different vector for each
450 		 * vCPU. Note that FreeBSD uses the same vector for all vCPUs
451 		 * because it's not dynamically allocated.
452 		 */
453 		rc = set_percpu_callback(PCPU_GET(vcpu_id));
454 		if (rc != 0)
455 			panic("Event channel upcall vector setup failed: %d",
456 			    rc);
457 	}
458 
459 	xen_setup_vcpu_info();
460 }
461 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL);
462 
463 bool
464 xen_has_iommu_maps(void)
465 {
466 	uint32_t regs[4];
467 
468 	KASSERT(xen_cpuid_base != 0, ("Invalid base Xen CPUID leaf"));
469 	cpuid_count(xen_cpuid_base + 4, 0, regs);
470 
471 	return (regs[0] & XEN_HVM_CPUID_IOMMU_MAPPINGS);
472 }
473