xref: /freebsd/sys/x86/xen/hvm.c (revision cfa0b7b82fbdda56d7160569def5c6133eb045aa)
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 
44 #include <dev/pci/pcivar.h>
45 
46 #include <machine/cpufunc.h>
47 #include <machine/cpu.h>
48 #include <machine/smp.h>
49 
50 #include <x86/apicreg.h>
51 
52 #include <xen/xen-os.h>
53 #include <xen/features.h>
54 #include <xen/gnttab.h>
55 #include <xen/hypervisor.h>
56 #include <xen/hvm.h>
57 #include <xen/xen_intr.h>
58 
59 #include <xen/interface/arch-x86/cpuid.h>
60 #include <xen/interface/hvm/params.h>
61 #include <xen/interface/vcpu.h>
62 
63 /*--------------------------- Forward Declarations ---------------------------*/
64 static void xen_hvm_cpu_init(void);
65 
66 /*-------------------------------- Local Types -------------------------------*/
67 enum xen_hvm_init_type {
68 	XEN_HVM_INIT_COLD,
69 	XEN_HVM_INIT_CANCELLED_SUSPEND,
70 	XEN_HVM_INIT_RESUME
71 };
72 
73 /*-------------------------------- Global Data -------------------------------*/
74 enum xen_domain_type xen_domain_type = XEN_NATIVE;
75 
76 #ifdef SMP
77 struct cpu_ops xen_hvm_cpu_ops = {
78 	.cpu_init	= xen_hvm_cpu_init,
79 	.cpu_resume	= xen_hvm_cpu_init
80 };
81 #endif
82 
83 static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support");
84 
85 /**
86  * If non-zero, the hypervisor has been configured to use a direct
87  * IDT event callback for interrupt injection.
88  */
89 int xen_vector_callback_enabled;
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 /*
123  * Allocate and fill in the hypcall page.
124  */
125 static int
126 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
127 {
128 	uint32_t regs[4];
129 
130 	if (xen_pv_domain()) {
131 		/* hypercall page is already set in the PV case */
132 		return (0);
133 	}
134 
135 	cpuid_base = xen_hvm_cpuid_base();
136 	if (cpuid_base == 0)
137 		return (ENXIO);
138 
139 	if (init_type == XEN_HVM_INIT_COLD) {
140 		int major, minor;
141 
142 		do_cpuid(cpuid_base + 1, regs);
143 
144 		major = regs[0] >> 16;
145 		minor = regs[0] & 0xffff;
146 		printf("XEN: Hypervisor version %d.%d detected.\n", major,
147 			minor);
148 
149 #ifdef SMP
150 		if (((major < 4) || (major == 4 && minor <= 5)) &&
151 		    msix_disable_migration == -1) {
152 			/*
153 			 * Xen hypervisors prior to 4.6.0 do not properly
154 			 * handle updates to enabled MSI-X table entries,
155 			 * so disable MSI-X interrupt migration in that
156 			 * case.
157 			 */
158 			if (bootverbose)
159 				printf(
160 "Disabling MSI-X interrupt migration due to Xen hypervisor bug.\n"
161 "Set machdep.msix_disable_migration=0 to forcefully enable it.\n");
162 			msix_disable_migration = 1;
163 		}
164 #endif
165 	}
166 
167 	/*
168 	 * Find the hypercall pages.
169 	 */
170 	do_cpuid(cpuid_base + 2, regs);
171 	if (regs[0] != 1)
172 		return (EINVAL);
173 
174 	wrmsr(regs[1], vtophys(&hypercall_page));
175 
176 	return (0);
177 }
178 
179 static void
180 xen_hvm_init_shared_info_page(void)
181 {
182 	struct xen_add_to_physmap xatp;
183 
184 	if (xen_pv_domain()) {
185 		/*
186 		 * Already setup in the PV case, shared_info is passed inside
187 		 * of the start_info struct at start of day.
188 		 */
189 		return;
190 	}
191 
192 	if (HYPERVISOR_shared_info == NULL) {
193 		HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT);
194 		if (HYPERVISOR_shared_info == NULL)
195 			panic("Unable to allocate Xen shared info page");
196 	}
197 
198 	xatp.domid = DOMID_SELF;
199 	xatp.idx = 0;
200 	xatp.space = XENMAPSPACE_shared_info;
201 	xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT;
202 	if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
203 		panic("HYPERVISOR_memory_op failed");
204 }
205 
206 /*
207  * Tell the hypervisor how to contact us for event channel callbacks.
208  */
209 void
210 xen_hvm_set_callback(device_t dev)
211 {
212 	struct xen_hvm_param xhp;
213 	int irq;
214 
215 	if (xen_vector_callback_enabled)
216 		return;
217 
218 	xhp.domid = DOMID_SELF;
219 	xhp.index = HVM_PARAM_CALLBACK_IRQ;
220 	if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
221 		int error;
222 
223 		xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
224 		error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
225 		if (error == 0) {
226 			xen_vector_callback_enabled = 1;
227 			return;
228 		}
229 		printf("Xen HVM callback vector registration failed (%d). "
230 		    "Falling back to emulated device interrupt\n", error);
231 	}
232 	xen_vector_callback_enabled = 0;
233 	if (dev == NULL) {
234 		/*
235 		 * Called from early boot or resume.
236 		 * xenpci will invoke us again later.
237 		 */
238 		return;
239 	}
240 
241 	irq = pci_get_irq(dev);
242 	if (irq < 16) {
243 		xhp.value = HVM_CALLBACK_GSI(irq);
244 	} else {
245 		u_int slot;
246 		u_int pin;
247 
248 		slot = pci_get_slot(dev);
249 		pin = pci_get_intpin(dev) - 1;
250 		xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
251 	}
252 
253 	if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
254 		panic("Can't set evtchn callback");
255 }
256 
257 #define	XEN_MAGIC_IOPORT 0x10
258 enum {
259 	XMI_MAGIC			 = 0x49d2,
260 	XMI_UNPLUG_IDE_DISKS		 = 0x01,
261 	XMI_UNPLUG_NICS			 = 0x02,
262 	XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
263 };
264 
265 static void
266 xen_hvm_disable_emulated_devices(void)
267 {
268 	u_short disable_devs = 0;
269 
270 	if (xen_pv_domain()) {
271 		/*
272 		 * No emulated devices in the PV case, so no need to unplug
273 		 * anything.
274 		 */
275 		if (xen_disable_pv_disks != 0 || xen_disable_pv_nics != 0)
276 			printf("PV devices cannot be disabled in PV guests\n");
277 		return;
278 	}
279 
280 	if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
281 		return;
282 
283 	if (xen_disable_pv_disks == 0) {
284 		if (bootverbose)
285 			printf("XEN: disabling emulated disks\n");
286 		disable_devs |= XMI_UNPLUG_IDE_DISKS;
287 	}
288 	if (xen_disable_pv_nics == 0) {
289 		if (bootverbose)
290 			printf("XEN: disabling emulated nics\n");
291 		disable_devs |= XMI_UNPLUG_NICS;
292 	}
293 
294 	if (disable_devs != 0)
295 		outw(XEN_MAGIC_IOPORT, disable_devs);
296 }
297 
298 static void
299 xen_hvm_init(enum xen_hvm_init_type init_type)
300 {
301 	int error;
302 	int i;
303 
304 	if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
305 		return;
306 
307 	error = xen_hvm_init_hypercall_stubs(init_type);
308 
309 	switch (init_type) {
310 	case XEN_HVM_INIT_COLD:
311 		if (error != 0)
312 			return;
313 
314 		/*
315 		 * If xen_domain_type is not set at this point
316 		 * it means we are inside a (PV)HVM guest, because
317 		 * for PVH the guest type is set much earlier
318 		 * (see hammer_time_xen).
319 		 */
320 		if (!xen_domain()) {
321 			xen_domain_type = XEN_HVM_DOMAIN;
322 			vm_guest = VM_GUEST_XEN;
323 		}
324 
325 		setup_xen_features();
326 #ifdef SMP
327 		cpu_ops = xen_hvm_cpu_ops;
328 #endif
329 		break;
330 	case XEN_HVM_INIT_RESUME:
331 		if (error != 0)
332 			panic("Unable to init Xen hypercall stubs on resume");
333 
334 		/* Clear stale vcpu_info. */
335 		CPU_FOREACH(i)
336 			DPCPU_ID_SET(i, vcpu_info, NULL);
337 		break;
338 	default:
339 		panic("Unsupported HVM initialization type");
340 	}
341 
342 	xen_vector_callback_enabled = 0;
343 	xen_hvm_set_callback(NULL);
344 
345 	/*
346 	 * On (PV)HVM domains we need to request the hypervisor to
347 	 * fill the shared info page, for PVH guest the shared_info page
348 	 * is passed inside the start_info struct and is already set, so this
349 	 * functions are no-ops.
350 	 */
351 	xen_hvm_init_shared_info_page();
352 	xen_hvm_disable_emulated_devices();
353 }
354 
355 void
356 xen_hvm_suspend(void)
357 {
358 }
359 
360 void
361 xen_hvm_resume(bool suspend_cancelled)
362 {
363 
364 	xen_hvm_init(suspend_cancelled ?
365 	    XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
366 
367 	/* Register vcpu_info area for CPU#0. */
368 	xen_hvm_cpu_init();
369 }
370 
371 static void
372 xen_hvm_sysinit(void *arg __unused)
373 {
374 	xen_hvm_init(XEN_HVM_INIT_COLD);
375 }
376 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL);
377 
378 static void
379 xen_hvm_cpu_init(void)
380 {
381 	struct vcpu_register_vcpu_info info;
382 	struct vcpu_info *vcpu_info;
383 	uint32_t regs[4];
384 	int cpu, rc;
385 
386 	if (!xen_domain())
387 		return;
388 
389 	if (DPCPU_GET(vcpu_info) != NULL) {
390 		/*
391 		 * vcpu_info is already set.  We're resuming
392 		 * from a failed migration and our pre-suspend
393 		 * configuration is still valid.
394 		 */
395 		return;
396 	}
397 
398 	/*
399 	 * Set vCPU ID. If available fetch the ID from CPUID, if not just use
400 	 * the ACPI ID.
401 	 */
402 	KASSERT(cpuid_base != 0, ("Invalid base Xen CPUID leaf"));
403 	cpuid_count(cpuid_base + 4, 0, regs);
404 	PCPU_SET(vcpu_id, (regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ?
405 	    regs[1] : PCPU_GET(acpi_id));
406 
407 	/*
408 	 * Set the vCPU info.
409 	 *
410 	 * NB: the vCPU info for vCPUs < 32 can be fetched from the shared info
411 	 * page, but in order to make sure the mapping code is correct always
412 	 * attempt to map the vCPU info at a custom place.
413 	 */
414 	vcpu_info = DPCPU_PTR(vcpu_local_info);
415 	cpu = PCPU_GET(vcpu_id);
416 	info.mfn = vtophys(vcpu_info) >> PAGE_SHIFT;
417 	info.offset = vtophys(vcpu_info) - trunc_page(vtophys(vcpu_info));
418 
419 	rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
420 	if (rc != 0)
421 		DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
422 	else
423 		DPCPU_SET(vcpu_info, vcpu_info);
424 }
425 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL);
426 
427 /* HVM/PVH start_info accessors */
428 static vm_paddr_t
429 hvm_get_xenstore_mfn(void)
430 {
431 
432 	return (hvm_get_parameter(HVM_PARAM_STORE_PFN));
433 }
434 
435 static evtchn_port_t
436 hvm_get_xenstore_evtchn(void)
437 {
438 
439 	return (hvm_get_parameter(HVM_PARAM_STORE_EVTCHN));
440 }
441 
442 static vm_paddr_t
443 hvm_get_console_mfn(void)
444 {
445 
446 	return (hvm_get_parameter(HVM_PARAM_CONSOLE_PFN));
447 }
448 
449 static evtchn_port_t
450 hvm_get_console_evtchn(void)
451 {
452 
453 	return (hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN));
454 }
455 
456 static uint32_t
457 hvm_get_start_flags(void)
458 {
459 
460 	return (0);
461 }
462 
463 struct hypervisor_info hypervisor_info = {
464 	.get_xenstore_mfn		= hvm_get_xenstore_mfn,
465 	.get_xenstore_evtchn		= hvm_get_xenstore_evtchn,
466 	.get_console_mfn		= hvm_get_console_mfn,
467 	.get_console_evtchn		= hvm_get_console_evtchn,
468 	.get_start_flags		= hvm_get_start_flags,
469 };
470