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