2 * Copyright (c) 2010 Justin T. Gibbs, Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * substantially similar to the "NO WARRANTY" disclaimer below
13 * ("Disclaimer") and any redistribution must be conditioned upon
14 * including a substantially similar Disclaimer requirement for further
15 * binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
32 * PV suspend/resume support:
34 * Copyright (c) 2004 Christian Limpach.
35 * Copyright (c) 2004-2006,2008 Kip Macy
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by Christian Limpach.
49 * 4. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 * HVM suspend/resume support:
67 * Copyright (c) 2008 Citrix Systems, Inc.
68 * All rights reserved.
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
73 * 1. Redistributions of source code must retain the above copyright
74 * notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright
76 * notice, this list of conditions and the following disclaimer in the
77 * documentation and/or other materials provided with the distribution.
79 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 #include <sys/cdefs.h>
92 __FBSDID("$FreeBSD$");
97 * \brief Device driver to repond to control domain events that impact
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/kernel.h>
104 #include <sys/malloc.h>
108 #include <sys/conf.h>
109 #include <sys/disk.h>
110 #include <sys/fcntl.h>
111 #include <sys/filedesc.h>
113 #include <sys/module.h>
114 #include <sys/namei.h>
115 #include <sys/proc.h>
116 #include <sys/reboot.h>
117 #include <sys/rman.h>
118 #include <sys/taskqueue.h>
119 #include <sys/types.h>
120 #include <sys/vnode.h>
123 #include <sys/sched.h>
128 #include <geom/geom.h>
130 #include <machine/_inttypes.h>
131 #include <machine/xen/xen-os.h>
134 #include <vm/vm_extern.h>
135 #include <vm/vm_kern.h>
137 #include <xen/blkif.h>
138 #include <xen/evtchn.h>
139 #include <xen/gnttab.h>
140 #include <xen/xen_intr.h>
142 #include <xen/interface/event_channel.h>
143 #include <xen/interface/grant_table.h>
145 #include <xen/xenbus/xenbusvar.h>
147 #define NUM_ELEMENTS(x) (sizeof(x) / sizeof(*(x)))
149 /*--------------------------- Forward Declarations --------------------------*/
150 /** Function signature for shutdown event handlers. */
151 typedef void (xctrl_shutdown_handler_t)(void);
153 static xctrl_shutdown_handler_t xctrl_poweroff;
154 static xctrl_shutdown_handler_t xctrl_reboot;
155 static xctrl_shutdown_handler_t xctrl_suspend;
156 static xctrl_shutdown_handler_t xctrl_crash;
157 static xctrl_shutdown_handler_t xctrl_halt;
159 /*-------------------------- Private Data Structures -------------------------*/
160 /** Element type for lookup table of event name to handler. */
161 struct xctrl_shutdown_reason {
163 xctrl_shutdown_handler_t *handler;
166 /** Lookup table for shutdown event name to handler. */
167 static struct xctrl_shutdown_reason xctrl_shutdown_reasons[] = {
168 { "poweroff", xctrl_poweroff },
169 { "reboot", xctrl_reboot },
170 { "suspend", xctrl_suspend },
171 { "crash", xctrl_crash },
172 { "halt", xctrl_halt },
178 struct xs_watch xctrl_watch;
181 /*------------------------------ Event Handlers ------------------------------*/
185 shutdown_nice(RB_POWEROFF|RB_HALT);
195 extern void xencons_suspend(void);
196 extern void xencons_resume(void);
198 /* Full PV mode suspension. */
203 unsigned long max_pfn, start_info_mfn;
208 * Bind us to CPU 0 and stop any other VCPUs.
210 thread_lock(curthread);
211 sched_bind(curthread, 0);
212 thread_unlock(curthread);
213 KASSERT(PCPU_GET(cpuid) == 0, ("xen_suspend: not running on cpu 0"));
215 map = PCPU_GET(other_cpus) & ~stopped_cpus;
220 if (DEVICE_SUSPEND(root_bus) != 0) {
221 printf("xen_suspend: device_suspend failed\n");
234 max_pfn = HYPERVISOR_shared_info->arch.max_pfn;
236 void *shared_info = HYPERVISOR_shared_info;
237 HYPERVISOR_shared_info = NULL;
238 pmap_kremove((vm_offset_t) shared_info);
241 xen_start_info->store_mfn = MFNTOPFN(xen_start_info->store_mfn);
242 xen_start_info->console.domU.mfn = MFNTOPFN(xen_start_info->console.domU.mfn);
245 * We'll stop somewhere inside this hypercall. When it returns,
246 * we'll start resuming after the restore.
248 start_info_mfn = VTOMFN(xen_start_info);
250 HYPERVISOR_suspend(start_info_mfn);
253 pmap_kenter_ma((vm_offset_t) shared_info, xen_start_info->shared_info);
254 HYPERVISOR_shared_info = shared_info;
256 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
257 VTOMFN(xen_pfn_to_mfn_frame_list_list);
259 fpp = PAGE_SIZE/sizeof(unsigned long);
260 for (i = 0, j = 0, k = -1; i < max_pfn; i += fpp, j++) {
261 if ((j % fpp) == 0) {
263 xen_pfn_to_mfn_frame_list_list[k] =
264 VTOMFN(xen_pfn_to_mfn_frame_list[k]);
267 xen_pfn_to_mfn_frame_list[k][j] =
268 VTOMFN(&xen_phys_machine[i]);
270 HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
283 * Only resume xenbus /after/ we've prepared our VCPUs; otherwise
284 * the VCPU hotplug callback can race with our vcpu_prepare
286 DEVICE_RESUME(root_bus);
289 thread_lock(curthread);
290 sched_unbind(curthread);
291 thread_unlock(curthread);
298 xen_pv_shutdown_final(void *arg, int howto)
301 * Inform the hypervisor that shutdown is complete.
302 * This is not necessary in HVM domains since Xen
303 * emulates ACPI in that mode and FreeBSD's ACPI
304 * support will request this transition.
306 if (howto & (RB_HALT | RB_POWEROFF))
307 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
309 HYPERVISOR_shutdown(SHUTDOWN_reboot);
313 extern void xenpci_resume(void);
315 /* HVM mode suspension. */
319 int suspend_cancelled;
321 if (DEVICE_SUSPEND(root_bus)) {
322 printf("xen_suspend: device_suspend failed\n");
327 * Make sure we don't change cpus or switch to some other
328 * thread. for the duration.
333 * Prevent any races with evtchn_interrupt() handler.
338 suspend_cancelled = HYPERVISOR_suspend(0);
339 if (!suspend_cancelled)
343 * Re-enable interrupts and put the scheduler back to normal.
349 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or
352 if (!suspend_cancelled)
353 DEVICE_RESUME(root_bus);
360 panic("Xen directed crash");
366 shutdown_nice(RB_HALT);
369 /*------------------------------ Event Reception -----------------------------*/
371 xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len)
373 struct xctrl_shutdown_reason *reason;
374 struct xctrl_shutdown_reason *last_reason;
379 error = xs_read(XST_NIL, "control", "shutdown",
380 &result_len, (void **)&result);
384 reason = xctrl_shutdown_reasons;
385 last_reason = reason + NUM_ELEMENTS(xctrl_shutdown_reasons);
386 while (reason < last_reason) {
388 if (!strcmp(result, reason->name)) {
395 free(result, M_XENSTORE);
398 /*------------------ Private Device Attachment Functions --------------------*/
400 * \brief Identify instances of this device type in the system.
402 * \param driver The driver performing this identify action.
403 * \param parent The NewBus parent device for any devices this method adds.
406 xctrl_identify(driver_t *driver __unused, device_t parent)
409 * A single device instance for our driver is always present
410 * in a system operating under Xen.
412 BUS_ADD_CHILD(parent, 0, driver->name, 0);
416 * \brief Probe for the existance of the Xen Control device
418 * \param dev NewBus device_t for this Xen control instance.
420 * \return Always returns 0 indicating success.
423 xctrl_probe(device_t dev)
425 device_set_desc(dev, "Xen Control Device");
431 * \brief Attach the Xen control device.
433 * \param dev NewBus device_t for this Xen control instance.
435 * \return On success, 0. Otherwise an errno value indicating the
439 xctrl_attach(device_t dev)
441 struct xctrl_softc *xctrl;
443 xctrl = device_get_softc(dev);
446 xctrl->xctrl_watch.node = "control/shutdown";
447 xctrl->xctrl_watch.callback = xctrl_on_watch_event;
448 xs_register_watch(&xctrl->xctrl_watch);
451 EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL,
459 * \brief Detach the Xen control device.
461 * \param dev NewBus device_t for this Xen control device instance.
463 * \return On success, 0. Otherwise an errno value indicating the
467 xctrl_detach(device_t dev)
469 struct xctrl_softc *xctrl;
471 xctrl = device_get_softc(dev);
474 xs_unregister_watch(&xctrl->xctrl_watch);
479 /*-------------------- Private Device Attachment Data -----------------------*/
480 static device_method_t xctrl_methods[] = {
481 /* Device interface */
482 DEVMETHOD(device_identify, xctrl_identify),
483 DEVMETHOD(device_probe, xctrl_probe),
484 DEVMETHOD(device_attach, xctrl_attach),
485 DEVMETHOD(device_detach, xctrl_detach),
490 DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc));
491 devclass_t xctrl_devclass;
493 DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, 0, 0);