xref: /freebsd/sys/x86/xen/hvm.c (revision 00e9473388d3f7ac84d8e51829b532ca476ab14e)
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 start_info_t *HYPERVISOR_start_info;
98 
99 
100 /*------------------------------ Sysctl tunables -----------------------------*/
101 int xen_disable_pv_disks = 0;
102 int xen_disable_pv_nics = 0;
103 TUNABLE_INT("hw.xen.disable_pv_disks", &xen_disable_pv_disks);
104 TUNABLE_INT("hw.xen.disable_pv_nics", &xen_disable_pv_nics);
105 
106 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/
107 
108 static uint32_t cpuid_base;
109 
110 static uint32_t
111 xen_hvm_cpuid_base(void)
112 {
113 	uint32_t base, regs[4];
114 
115 	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
116 		do_cpuid(base, regs);
117 		if (!memcmp("XenVMMXenVMM", &regs[1], 12)
118 		    && (regs[0] - base) >= 2)
119 			return (base);
120 	}
121 	return (0);
122 }
123 
124 /*
125  * Allocate and fill in the hypcall page.
126  */
127 static int
128 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
129 {
130 	uint32_t regs[4];
131 
132 	if (xen_pv_domain()) {
133 		/* hypercall page is already set in the PV case */
134 		return (0);
135 	}
136 
137 	cpuid_base = xen_hvm_cpuid_base();
138 	if (cpuid_base == 0)
139 		return (ENXIO);
140 
141 	if (init_type == XEN_HVM_INIT_COLD) {
142 		int major, minor;
143 
144 		do_cpuid(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,
149 			minor);
150 
151 #ifdef SMP
152 		if (((major < 4) || (major == 4 && minor <= 5)) &&
153 		    msix_disable_migration == -1) {
154 			/*
155 			 * Xen hypervisors prior to 4.6.0 do not properly
156 			 * handle updates to enabled MSI-X table entries,
157 			 * so disable MSI-X interrupt migration in that
158 			 * case.
159 			 */
160 			if (bootverbose)
161 				printf(
162 "Disabling MSI-X interrupt migration due to Xen hypervisor bug.\n"
163 "Set machdep.msix_disable_migration=0 to forcefully enable it.\n");
164 			msix_disable_migration = 1;
165 		}
166 #endif
167 	}
168 
169 	/*
170 	 * Find the hypercall pages.
171 	 */
172 	do_cpuid(cpuid_base + 2, regs);
173 	if (regs[0] != 1)
174 		return (EINVAL);
175 
176 	wrmsr(regs[1], vtophys(&hypercall_page));
177 
178 	return (0);
179 }
180 
181 static void
182 xen_hvm_init_shared_info_page(void)
183 {
184 	struct xen_add_to_physmap xatp;
185 
186 	if (xen_pv_domain()) {
187 		/*
188 		 * Already setup in the PV case, shared_info is passed inside
189 		 * of the start_info struct at start of day.
190 		 */
191 		return;
192 	}
193 
194 	if (HYPERVISOR_shared_info == NULL) {
195 		HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT);
196 		if (HYPERVISOR_shared_info == NULL)
197 			panic("Unable to allocate Xen shared info page");
198 	}
199 
200 	xatp.domid = DOMID_SELF;
201 	xatp.idx = 0;
202 	xatp.space = XENMAPSPACE_shared_info;
203 	xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT;
204 	if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
205 		panic("HYPERVISOR_memory_op failed");
206 }
207 
208 /*
209  * Tell the hypervisor how to contact us for event channel callbacks.
210  */
211 void
212 xen_hvm_set_callback(device_t dev)
213 {
214 	struct xen_hvm_param xhp;
215 	int irq;
216 
217 	if (xen_vector_callback_enabled)
218 		return;
219 
220 	xhp.domid = DOMID_SELF;
221 	xhp.index = HVM_PARAM_CALLBACK_IRQ;
222 	if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
223 		int error;
224 
225 		xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
226 		error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
227 		if (error == 0) {
228 			xen_vector_callback_enabled = 1;
229 			return;
230 		}
231 		printf("Xen HVM callback vector registration failed (%d). "
232 		    "Falling back to emulated device interrupt\n", error);
233 	}
234 	xen_vector_callback_enabled = 0;
235 	if (dev == NULL) {
236 		/*
237 		 * Called from early boot or resume.
238 		 * xenpci will invoke us again later.
239 		 */
240 		return;
241 	}
242 
243 	irq = pci_get_irq(dev);
244 	if (irq < 16) {
245 		xhp.value = HVM_CALLBACK_GSI(irq);
246 	} else {
247 		u_int slot;
248 		u_int pin;
249 
250 		slot = pci_get_slot(dev);
251 		pin = pci_get_intpin(dev) - 1;
252 		xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
253 	}
254 
255 	if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
256 		panic("Can't set evtchn callback");
257 }
258 
259 #define	XEN_MAGIC_IOPORT 0x10
260 enum {
261 	XMI_MAGIC			 = 0x49d2,
262 	XMI_UNPLUG_IDE_DISKS		 = 0x01,
263 	XMI_UNPLUG_NICS			 = 0x02,
264 	XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
265 };
266 
267 static void
268 xen_hvm_disable_emulated_devices(void)
269 {
270 	u_short disable_devs = 0;
271 
272 	if (xen_pv_domain()) {
273 		/*
274 		 * No emulated devices in the PV case, so no need to unplug
275 		 * anything.
276 		 */
277 		if (xen_disable_pv_disks != 0 || xen_disable_pv_nics != 0)
278 			printf("PV devices cannot be disabled in PV guests\n");
279 		return;
280 	}
281 
282 	if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
283 		return;
284 
285 	if (xen_disable_pv_disks == 0) {
286 		if (bootverbose)
287 			printf("XEN: disabling emulated disks\n");
288 		disable_devs |= XMI_UNPLUG_IDE_DISKS;
289 	}
290 	if (xen_disable_pv_nics == 0) {
291 		if (bootverbose)
292 			printf("XEN: disabling emulated nics\n");
293 		disable_devs |= XMI_UNPLUG_NICS;
294 	}
295 
296 	if (disable_devs != 0)
297 		outw(XEN_MAGIC_IOPORT, disable_devs);
298 }
299 
300 static void
301 xen_hvm_init(enum xen_hvm_init_type init_type)
302 {
303 	int error;
304 	int i;
305 
306 	if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
307 		return;
308 
309 	error = xen_hvm_init_hypercall_stubs(init_type);
310 
311 	switch (init_type) {
312 	case XEN_HVM_INIT_COLD:
313 		if (error != 0)
314 			return;
315 
316 		/*
317 		 * If xen_domain_type is not set at this point
318 		 * it means we are inside a (PV)HVM guest, because
319 		 * for PVH the guest type is set much earlier
320 		 * (see hammer_time_xen).
321 		 */
322 		if (!xen_domain()) {
323 			xen_domain_type = XEN_HVM_DOMAIN;
324 			vm_guest = VM_GUEST_XEN;
325 		}
326 
327 		setup_xen_features();
328 #ifdef SMP
329 		cpu_ops = xen_hvm_cpu_ops;
330 #endif
331 		break;
332 	case XEN_HVM_INIT_RESUME:
333 		if (error != 0)
334 			panic("Unable to init Xen hypercall stubs on resume");
335 
336 		/* Clear stale vcpu_info. */
337 		CPU_FOREACH(i)
338 			DPCPU_ID_SET(i, vcpu_info, NULL);
339 		break;
340 	default:
341 		panic("Unsupported HVM initialization type");
342 	}
343 
344 	xen_vector_callback_enabled = 0;
345 	xen_hvm_set_callback(NULL);
346 
347 	/*
348 	 * On (PV)HVM domains we need to request the hypervisor to
349 	 * fill the shared info page, for PVH guest the shared_info page
350 	 * is passed inside the start_info struct and is already set, so this
351 	 * functions are no-ops.
352 	 */
353 	xen_hvm_init_shared_info_page();
354 	xen_hvm_disable_emulated_devices();
355 }
356 
357 void
358 xen_hvm_suspend(void)
359 {
360 }
361 
362 void
363 xen_hvm_resume(bool suspend_cancelled)
364 {
365 
366 	xen_hvm_init(suspend_cancelled ?
367 	    XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
368 
369 	/* Register vcpu_info area for CPU#0. */
370 	xen_hvm_cpu_init();
371 }
372 
373 static void
374 xen_hvm_sysinit(void *arg __unused)
375 {
376 	xen_hvm_init(XEN_HVM_INIT_COLD);
377 }
378 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL);
379 
380 static void
381 xen_hvm_cpu_init(void)
382 {
383 	struct vcpu_register_vcpu_info info;
384 	struct vcpu_info *vcpu_info;
385 	uint32_t regs[4];
386 	int cpu, rc;
387 
388 	if (!xen_domain())
389 		return;
390 
391 	if (DPCPU_GET(vcpu_info) != NULL) {
392 		/*
393 		 * vcpu_info is already set.  We're resuming
394 		 * from a failed migration and our pre-suspend
395 		 * configuration is still valid.
396 		 */
397 		return;
398 	}
399 
400 	/*
401 	 * Set vCPU ID. If available fetch the ID from CPUID, if not just use
402 	 * the ACPI ID.
403 	 */
404 	KASSERT(cpuid_base != 0, ("Invalid base Xen CPUID leaf"));
405 	cpuid_count(cpuid_base + 4, 0, regs);
406 	PCPU_SET(vcpu_id, (regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ?
407 	    regs[1] : PCPU_GET(acpi_id));
408 
409 	/*
410 	 * Set the vCPU info.
411 	 *
412 	 * NB: the vCPU info for vCPUs < 32 can be fetched from the shared info
413 	 * page, but in order to make sure the mapping code is correct always
414 	 * attempt to map the vCPU info at a custom place.
415 	 */
416 	vcpu_info = DPCPU_PTR(vcpu_local_info);
417 	cpu = PCPU_GET(vcpu_id);
418 	info.mfn = vtophys(vcpu_info) >> PAGE_SHIFT;
419 	info.offset = vtophys(vcpu_info) - trunc_page(vtophys(vcpu_info));
420 
421 	rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
422 	if (rc != 0)
423 		DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
424 	else
425 		DPCPU_SET(vcpu_info, vcpu_info);
426 }
427 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL);
428