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 ---------------------------*/
62 static void xen_hvm_cpu_resume(void);
64 static void xen_hvm_cpu_init(void);
66 /*---------------------------- Extern Declarations ---------------------------*/
67 /* Variables used by mp_machdep to perform the bitmap IPI */
68 extern volatile u_int cpu_ipi_pending[MAXCPU];
70 /*-------------------------------- Local Types -------------------------------*/
71 enum xen_hvm_init_type {
73 XEN_HVM_INIT_CANCELLED_SUSPEND,
77 /*-------------------------------- Global Data -------------------------------*/
78 enum xen_domain_type xen_domain_type = XEN_NATIVE;
81 struct cpu_ops xen_hvm_cpu_ops = {
82 .cpu_init = xen_hvm_cpu_init,
83 .cpu_resume = xen_hvm_cpu_resume
87 static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support");
90 * If non-zero, the hypervisor has been configured to use a direct
91 * IDT event callback for interrupt injection.
93 int xen_vector_callback_enabled;
95 /*------------------------------- Per-CPU Data -------------------------------*/
96 DPCPU_DEFINE(struct vcpu_info, vcpu_local_info);
97 DPCPU_DEFINE(struct vcpu_info *, vcpu_info);
99 /*------------------ Hypervisor Access Shared Memory Regions -----------------*/
100 shared_info_t *HYPERVISOR_shared_info;
101 start_info_t *HYPERVISOR_start_info;
104 /*------------------------------ Sysctl tunables -----------------------------*/
105 int xen_disable_pv_disks = 0;
106 int xen_disable_pv_nics = 0;
107 TUNABLE_INT("hw.xen.disable_pv_disks", &xen_disable_pv_disks);
108 TUNABLE_INT("hw.xen.disable_pv_nics", &xen_disable_pv_nics);
111 /*---------------------- XEN diverged cpu operations -------------------------*/
113 xen_hvm_cpu_resume(void)
115 u_int cpuid = PCPU_GET(cpuid);
118 * Reset pending bitmap IPIs, because Xen doesn't preserve pending
119 * event channels on migration.
121 cpu_ipi_pending[cpuid] = 0;
123 /* register vcpu_info area */
127 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/
129 xen_hvm_cpuid_base(void)
131 uint32_t base, regs[4];
133 for (base = 0x40000000; base < 0x40010000; base += 0x100) {
134 do_cpuid(base, regs);
135 if (!memcmp("XenVMMXenVMM", ®s[1], 12)
136 && (regs[0] - base) >= 2)
143 * Allocate and fill in the hypcall page.
146 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
148 uint32_t base, regs[4];
151 if (xen_pv_domain()) {
152 /* hypercall page is already set in the PV case */
156 base = xen_hvm_cpuid_base();
160 if (init_type == XEN_HVM_INIT_COLD) {
161 do_cpuid(base + 1, regs);
162 printf("XEN: Hypervisor version %d.%d detected.\n",
163 regs[0] >> 16, regs[0] & 0xffff);
167 * Find the hypercall pages.
169 do_cpuid(base + 2, regs);
171 for (i = 0; i < regs[0]; i++)
172 wrmsr(regs[1], vtophys(&hypercall_page + i * PAGE_SIZE) + i);
178 xen_hvm_init_shared_info_page(void)
180 struct xen_add_to_physmap xatp;
182 if (xen_pv_domain()) {
184 * Already setup in the PV case, shared_info is passed inside
185 * of the start_info struct at start of day.
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");
196 xatp.domid = DOMID_SELF;
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");
205 * Tell the hypervisor how to contact us for event channel callbacks.
208 xen_hvm_set_callback(device_t dev)
210 struct xen_hvm_param xhp;
213 if (xen_vector_callback_enabled)
216 xhp.domid = DOMID_SELF;
217 xhp.index = HVM_PARAM_CALLBACK_IRQ;
218 if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
221 xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
222 error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
224 xen_vector_callback_enabled = 1;
227 printf("Xen HVM callback vector registration failed (%d). "
228 "Falling back to emulated device interrupt\n", error);
230 xen_vector_callback_enabled = 0;
233 * Called from early boot or resume.
234 * xenpci will invoke us again later.
239 irq = pci_get_irq(dev);
241 xhp.value = HVM_CALLBACK_GSI(irq);
246 slot = pci_get_slot(dev);
247 pin = pci_get_intpin(dev) - 1;
248 xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
251 if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
252 panic("Can't set evtchn callback");
255 #define XEN_MAGIC_IOPORT 0x10
258 XMI_UNPLUG_IDE_DISKS = 0x01,
259 XMI_UNPLUG_NICS = 0x02,
260 XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
264 xen_hvm_disable_emulated_devices(void)
266 u_short disable_devs = 0;
268 if (xen_pv_domain()) {
270 * No emulated devices in the PV case, so no need to unplug
273 if (xen_disable_pv_disks != 0 || xen_disable_pv_nics != 0)
274 printf("PV devices cannot be disabled in PV guests\n");
278 if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
281 if (xen_disable_pv_disks == 0) {
283 printf("XEN: disabling emulated disks\n");
284 disable_devs |= XMI_UNPLUG_IDE_DISKS;
286 if (xen_disable_pv_nics == 0) {
288 printf("XEN: disabling emulated nics\n");
289 disable_devs |= XMI_UNPLUG_NICS;
292 if (disable_devs != 0)
293 outw(XEN_MAGIC_IOPORT, disable_devs);
297 xen_hvm_init(enum xen_hvm_init_type init_type)
302 if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
305 error = xen_hvm_init_hypercall_stubs(init_type);
308 case XEN_HVM_INIT_COLD:
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).
319 xen_domain_type = XEN_HVM_DOMAIN;
320 vm_guest = VM_GUEST_XEN;
323 setup_xen_features();
325 cpu_ops = xen_hvm_cpu_ops;
328 case XEN_HVM_INIT_RESUME:
330 panic("Unable to init Xen hypercall stubs on resume");
332 /* Clear stale vcpu_info. */
334 DPCPU_ID_SET(i, vcpu_info, NULL);
337 panic("Unsupported HVM initialization type");
340 xen_vector_callback_enabled = 0;
341 xen_hvm_set_callback(NULL);
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.
349 xen_hvm_init_shared_info_page();
350 xen_hvm_disable_emulated_devices();
354 xen_hvm_suspend(void)
359 xen_hvm_resume(bool suspend_cancelled)
362 xen_hvm_init(suspend_cancelled ?
363 XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
365 /* Register vcpu_info area for CPU#0. */
370 xen_hvm_sysinit(void *arg __unused)
372 xen_hvm_init(XEN_HVM_INIT_COLD);
376 xen_set_vcpu_id(void)
381 if (!xen_hvm_domain())
384 /* Set vcpu_id to acpi_id */
387 pc->pc_vcpu_id = pc->pc_acpi_id;
389 printf("XEN: CPU %u has VCPU ID %u\n",
395 xen_hvm_cpu_init(void)
397 struct vcpu_register_vcpu_info info;
398 struct vcpu_info *vcpu_info;
404 if (DPCPU_GET(vcpu_info) != NULL) {
406 * vcpu_info is already set. We're resuming
407 * from a failed migration and our pre-suspend
408 * configuration is still valid.
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));
418 rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
420 DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
422 DPCPU_SET(vcpu_info, vcpu_info);
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);