2 * Copyright (c) 2008, 2013 Citrix Systems, Inc.
3 * Copyright (c) 2012 Spectra Logic Corporation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
37 #include <sys/systm.h>
42 #include <dev/pci/pcivar.h>
44 #include <machine/cpufunc.h>
45 #include <machine/cpu.h>
46 #include <machine/smp.h>
48 #include <x86/apicreg.h>
50 #include <xen/xen-os.h>
51 #include <xen/features.h>
52 #include <xen/gnttab.h>
53 #include <xen/hypervisor.h>
55 #include <xen/xen_intr.h>
57 #include <xen/interface/hvm/params.h>
58 #include <xen/interface/vcpu.h>
60 /*--------------------------- Forward Declarations ---------------------------*/
61 static void xen_hvm_cpu_init(void);
63 /*-------------------------------- Local Types -------------------------------*/
64 enum xen_hvm_init_type {
66 XEN_HVM_INIT_CANCELLED_SUSPEND,
70 /*-------------------------------- Global Data -------------------------------*/
71 enum xen_domain_type xen_domain_type = XEN_NATIVE;
74 struct cpu_ops xen_hvm_cpu_ops = {
75 .cpu_init = xen_hvm_cpu_init,
76 .cpu_resume = xen_hvm_cpu_init
80 static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support");
83 * If non-zero, the hypervisor has been configured to use a direct
84 * IDT event callback for interrupt injection.
86 int xen_vector_callback_enabled;
88 /*------------------------------- Per-CPU Data -------------------------------*/
89 DPCPU_DEFINE(struct vcpu_info, vcpu_local_info);
90 DPCPU_DEFINE(struct vcpu_info *, vcpu_info);
92 /*------------------ Hypervisor Access Shared Memory Regions -----------------*/
93 shared_info_t *HYPERVISOR_shared_info;
94 start_info_t *HYPERVISOR_start_info;
97 /*------------------------------ Sysctl tunables -----------------------------*/
98 int xen_disable_pv_disks = 0;
99 int xen_disable_pv_nics = 0;
100 TUNABLE_INT("hw.xen.disable_pv_disks", &xen_disable_pv_disks);
101 TUNABLE_INT("hw.xen.disable_pv_nics", &xen_disable_pv_nics);
103 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/
105 xen_hvm_cpuid_base(void)
107 uint32_t base, regs[4];
109 for (base = 0x40000000; base < 0x40010000; base += 0x100) {
110 do_cpuid(base, regs);
111 if (!memcmp("XenVMMXenVMM", ®s[1], 12)
112 && (regs[0] - base) >= 2)
119 * Allocate and fill in the hypcall page.
122 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
124 uint32_t base, regs[4];
127 if (xen_pv_domain()) {
128 /* hypercall page is already set in the PV case */
132 base = xen_hvm_cpuid_base();
136 if (init_type == XEN_HVM_INIT_COLD) {
139 do_cpuid(base + 1, regs);
141 major = regs[0] >> 16;
142 minor = regs[0] & 0xffff;
143 printf("XEN: Hypervisor version %d.%d detected.\n", major,
147 if (((major < 4) || (major == 4 && minor <= 5)) &&
148 msix_disable_migration == -1) {
150 * Xen hypervisors prior to 4.6.0 do not properly
151 * handle updates to enabled MSI-X table entries,
152 * so disable MSI-X interrupt migration in that
157 "Disabling MSI-X interrupt migration due to Xen hypervisor bug.\n"
158 "Set machdep.msix_disable_migration=0 to forcefully enable it.\n");
159 msix_disable_migration = 1;
165 * Find the hypercall pages.
167 do_cpuid(base + 2, regs);
169 for (i = 0; i < regs[0]; i++)
170 wrmsr(regs[1], vtophys(&hypercall_page + i * PAGE_SIZE) + i);
176 xen_hvm_init_shared_info_page(void)
178 struct xen_add_to_physmap xatp;
180 if (xen_pv_domain()) {
182 * Already setup in the PV case, shared_info is passed inside
183 * of the start_info struct at start of day.
188 if (HYPERVISOR_shared_info == NULL) {
189 HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT);
190 if (HYPERVISOR_shared_info == NULL)
191 panic("Unable to allocate Xen shared info page");
194 xatp.domid = DOMID_SELF;
196 xatp.space = XENMAPSPACE_shared_info;
197 xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT;
198 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
199 panic("HYPERVISOR_memory_op failed");
203 * Tell the hypervisor how to contact us for event channel callbacks.
206 xen_hvm_set_callback(device_t dev)
208 struct xen_hvm_param xhp;
211 if (xen_vector_callback_enabled)
214 xhp.domid = DOMID_SELF;
215 xhp.index = HVM_PARAM_CALLBACK_IRQ;
216 if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
219 xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
220 error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
222 xen_vector_callback_enabled = 1;
225 printf("Xen HVM callback vector registration failed (%d). "
226 "Falling back to emulated device interrupt\n", error);
228 xen_vector_callback_enabled = 0;
231 * Called from early boot or resume.
232 * xenpci will invoke us again later.
237 irq = pci_get_irq(dev);
239 xhp.value = HVM_CALLBACK_GSI(irq);
244 slot = pci_get_slot(dev);
245 pin = pci_get_intpin(dev) - 1;
246 xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
249 if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
250 panic("Can't set evtchn callback");
253 #define XEN_MAGIC_IOPORT 0x10
256 XMI_UNPLUG_IDE_DISKS = 0x01,
257 XMI_UNPLUG_NICS = 0x02,
258 XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
262 xen_hvm_disable_emulated_devices(void)
264 u_short disable_devs = 0;
266 if (xen_pv_domain()) {
268 * No emulated devices in the PV case, so no need to unplug
271 if (xen_disable_pv_disks != 0 || xen_disable_pv_nics != 0)
272 printf("PV devices cannot be disabled in PV guests\n");
276 if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
279 if (xen_disable_pv_disks == 0) {
281 printf("XEN: disabling emulated disks\n");
282 disable_devs |= XMI_UNPLUG_IDE_DISKS;
284 if (xen_disable_pv_nics == 0) {
286 printf("XEN: disabling emulated nics\n");
287 disable_devs |= XMI_UNPLUG_NICS;
290 if (disable_devs != 0)
291 outw(XEN_MAGIC_IOPORT, disable_devs);
295 xen_hvm_init(enum xen_hvm_init_type init_type)
300 if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
303 error = xen_hvm_init_hypercall_stubs(init_type);
306 case XEN_HVM_INIT_COLD:
311 * If xen_domain_type is not set at this point
312 * it means we are inside a (PV)HVM guest, because
313 * for PVH the guest type is set much earlier
314 * (see hammer_time_xen).
317 xen_domain_type = XEN_HVM_DOMAIN;
318 vm_guest = VM_GUEST_XEN;
321 setup_xen_features();
323 cpu_ops = xen_hvm_cpu_ops;
326 case XEN_HVM_INIT_RESUME:
328 panic("Unable to init Xen hypercall stubs on resume");
330 /* Clear stale vcpu_info. */
332 DPCPU_ID_SET(i, vcpu_info, NULL);
335 panic("Unsupported HVM initialization type");
338 xen_vector_callback_enabled = 0;
339 xen_hvm_set_callback(NULL);
342 * On (PV)HVM domains we need to request the hypervisor to
343 * fill the shared info page, for PVH guest the shared_info page
344 * is passed inside the start_info struct and is already set, so this
345 * functions are no-ops.
347 xen_hvm_init_shared_info_page();
348 xen_hvm_disable_emulated_devices();
352 xen_hvm_suspend(void)
357 xen_hvm_resume(bool suspend_cancelled)
360 xen_hvm_init(suspend_cancelled ?
361 XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
363 /* Register vcpu_info area for CPU#0. */
368 xen_hvm_sysinit(void *arg __unused)
370 xen_hvm_init(XEN_HVM_INIT_COLD);
374 xen_set_vcpu_id(void)
379 if (!xen_hvm_domain())
382 /* Set vcpu_id to acpi_id */
385 pc->pc_vcpu_id = pc->pc_acpi_id;
387 printf("XEN: CPU %u has VCPU ID %u\n",
393 xen_hvm_cpu_init(void)
395 struct vcpu_register_vcpu_info info;
396 struct vcpu_info *vcpu_info;
402 if (DPCPU_GET(vcpu_info) != NULL) {
404 * vcpu_info is already set. We're resuming
405 * from a failed migration and our pre-suspend
406 * configuration is still valid.
411 vcpu_info = DPCPU_PTR(vcpu_local_info);
412 cpu = PCPU_GET(vcpu_id);
413 info.mfn = vtophys(vcpu_info) >> PAGE_SHIFT;
414 info.offset = vtophys(vcpu_info) - trunc_page(vtophys(vcpu_info));
416 rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
418 DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
420 DPCPU_SET(vcpu_info, vcpu_info);
423 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL);
424 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL);
425 SYSINIT(xen_set_vcpu_id, SI_SUB_CPU, SI_ORDER_ANY, xen_set_vcpu_id, NULL);