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