2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-4-Clause
4 * Copyright (c) 2010 Justin T. Gibbs, Spectra Logic Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * substantially similar to the "NO WARRANTY" disclaimer below
15 * ("Disclaimer") and any redistribution must be conditioned upon
16 * including a substantially similar Disclaimer requirement for further
17 * binary redistribution.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGES.
34 * PV suspend/resume support:
36 * Copyright (c) 2004 Christian Limpach.
37 * Copyright (c) 2004-2006,2008 Kip Macy
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by Christian Limpach.
51 * 4. The name of the author may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 * HVM suspend/resume support:
69 * Copyright (c) 2008 Citrix Systems, Inc.
70 * All rights reserved.
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
81 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 #include <sys/cdefs.h>
94 __FBSDID("$FreeBSD$");
99 * \brief Device driver to repond to control domain events that impact
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/malloc.h>
110 #include <sys/conf.h>
111 #include <sys/disk.h>
112 #include <sys/fcntl.h>
113 #include <sys/filedesc.h>
115 #include <sys/module.h>
116 #include <sys/mount.h>
117 #include <sys/namei.h>
118 #include <sys/proc.h>
119 #include <sys/reboot.h>
120 #include <sys/rman.h>
121 #include <sys/sched.h>
122 #include <sys/taskqueue.h>
123 #include <sys/types.h>
124 #include <sys/vnode.h>
125 #include <sys/sched.h>
127 #include <sys/eventhandler.h>
128 #include <sys/timetc.h>
130 #include <geom/geom.h>
132 #include <machine/_inttypes.h>
133 #include <machine/intr_machdep.h>
135 #include <x86/apicvar.h>
138 #include <vm/vm_extern.h>
139 #include <vm/vm_kern.h>
141 #include <xen/xen-os.h>
142 #include <xen/blkif.h>
143 #include <xen/evtchn.h>
144 #include <xen/gnttab.h>
145 #include <xen/xen_intr.h>
149 #include <xen/interface/event_channel.h>
150 #include <xen/interface/grant_table.h>
152 #include <xen/xenbus/xenbusvar.h>
154 bool xen_suspend_cancelled;
155 /*--------------------------- Forward Declarations --------------------------*/
156 /** Function signature for shutdown event handlers. */
157 typedef void (xctrl_shutdown_handler_t)(void);
159 static xctrl_shutdown_handler_t xctrl_poweroff;
160 static xctrl_shutdown_handler_t xctrl_reboot;
161 static xctrl_shutdown_handler_t xctrl_suspend;
162 static xctrl_shutdown_handler_t xctrl_crash;
164 /*-------------------------- Private Data Structures -------------------------*/
165 /** Element type for lookup table of event name to handler. */
166 struct xctrl_shutdown_reason {
168 xctrl_shutdown_handler_t *handler;
171 /** Lookup table for shutdown event name to handler. */
172 static const struct xctrl_shutdown_reason xctrl_shutdown_reasons[] = {
173 { "poweroff", xctrl_poweroff },
174 { "reboot", xctrl_reboot },
175 { "suspend", xctrl_suspend },
176 { "crash", xctrl_crash },
177 { "halt", xctrl_poweroff },
181 struct xs_watch xctrl_watch;
184 /*------------------------------ Event Handlers ------------------------------*/
188 shutdown_nice(RB_POWEROFF|RB_HALT);
201 cpuset_t cpu_suspend_map;
204 EVENTHANDLER_INVOKE(power_suspend_early);
209 EVENTHANDLER_INVOKE(power_suspend);
211 #ifdef EARLY_AP_STARTUP
212 MPASS(mp_ncpus == 1 || smp_started);
213 thread_lock(curthread);
214 sched_bind(curthread, 0);
215 thread_unlock(curthread);
218 thread_lock(curthread);
219 sched_bind(curthread, 0);
220 thread_unlock(curthread);
223 KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0"));
226 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
230 if (DEVICE_SUSPEND(root_bus) != 0) {
232 printf("%s: device_suspend failed\n", __func__);
237 #ifdef EARLY_AP_STARTUP
239 * Suspend other CPUs. This prevents IPIs while we
240 * are resuming, and will allow us to reset per-cpu
241 * vcpu_info on resume.
243 cpu_suspend_map = all_cpus;
244 CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
245 if (!CPU_EMPTY(&cpu_suspend_map))
246 suspend_cpus(cpu_suspend_map);
248 CPU_ZERO(&cpu_suspend_map); /* silence gcc */
251 * Suspend other CPUs. This prevents IPIs while we
252 * are resuming, and will allow us to reset per-cpu
253 * vcpu_info on resume.
255 cpu_suspend_map = all_cpus;
256 CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
257 if (!CPU_EMPTY(&cpu_suspend_map))
258 suspend_cpus(cpu_suspend_map);
264 * Prevent any races with evtchn_interrupt() handler.
270 xen_suspend_cancelled = !!HYPERVISOR_suspend(0);
272 if (!xen_suspend_cancelled) {
273 xen_hvm_resume(false);
275 intr_resume(xen_suspend_cancelled != 0);
279 * Reset grant table info.
281 if (!xen_suspend_cancelled) {
286 if (!CPU_EMPTY(&cpu_suspend_map)) {
288 * Now that event channels have been initialized,
291 resume_cpus(cpu_suspend_map);
292 /* Send an IPI_BITMAP in case there are pending bitmap IPIs. */
293 lapic_ipi_vectored(IPI_BITMAP_VECTOR, APIC_IPI_DEST_ALL);
298 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or
301 DEVICE_RESUME(root_bus);
305 * Warm up timecounter again and reset system clock.
307 timecounter->tc_get_timecount(timecounter);
308 inittodr(time_second);
310 #ifdef EARLY_AP_STARTUP
311 thread_lock(curthread);
312 sched_unbind(curthread);
313 thread_unlock(curthread);
316 thread_lock(curthread);
317 sched_unbind(curthread);
318 thread_unlock(curthread);
325 EVENTHANDLER_INVOKE(power_resume);
328 printf("System resumed after suspension\n");
335 panic("Xen directed crash");
339 xen_pv_shutdown_final(void *arg, int howto)
342 * Inform the hypervisor that shutdown is complete.
343 * This is not necessary in HVM domains since Xen
344 * emulates ACPI in that mode and FreeBSD's ACPI
345 * support will request this transition.
347 if (howto & (RB_HALT | RB_POWEROFF))
348 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
350 HYPERVISOR_shutdown(SHUTDOWN_reboot);
353 /*------------------------------ Event Reception -----------------------------*/
355 xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len)
357 const struct xctrl_shutdown_reason *reason;
358 const struct xctrl_shutdown_reason *last_reason;
363 error = xs_read(XST_NIL, "control", "shutdown",
364 &result_len, (void **)&result);
365 if (error != 0 || result_len == 0)
368 /* Acknowledge the request by writing back an empty string. */
369 error = xs_write(XST_NIL, "control", "shutdown", "");
371 printf("unable to ack shutdown request, proceeding anyway\n");
373 reason = xctrl_shutdown_reasons;
374 last_reason = reason + nitems(xctrl_shutdown_reasons);
375 while (reason < last_reason) {
376 if (!strcmp(result, reason->name)) {
383 free(result, M_XENSTORE);
386 /*------------------ Private Device Attachment Functions --------------------*/
388 * \brief Identify instances of this device type in the system.
390 * \param driver The driver performing this identify action.
391 * \param parent The NewBus parent device for any devices this method adds.
394 xctrl_identify(driver_t *driver __unused, device_t parent)
397 * A single device instance for our driver is always present
398 * in a system operating under Xen.
400 BUS_ADD_CHILD(parent, 0, driver->name, 0);
404 * \brief Probe for the existence of the Xen Control device
406 * \param dev NewBus device_t for this Xen control instance.
408 * \return Always returns 0 indicating success.
411 xctrl_probe(device_t dev)
413 device_set_desc(dev, "Xen Control Device");
415 return (BUS_PROBE_NOWILDCARD);
419 * \brief Attach the Xen control device.
421 * \param dev NewBus device_t for this Xen control instance.
423 * \return On success, 0. Otherwise an errno value indicating the
427 xctrl_attach(device_t dev)
429 struct xctrl_softc *xctrl;
431 xctrl = device_get_softc(dev);
434 xctrl->xctrl_watch.node = "control/shutdown";
435 xctrl->xctrl_watch.callback = xctrl_on_watch_event;
436 xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl;
438 * We don't care about the path updated, just about the value changes
439 * on that single node, hence there's no need to queue more that one
442 xctrl->xctrl_watch.max_pending = 1;
443 xs_register_watch(&xctrl->xctrl_watch);
446 EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL,
453 * \brief Detach the Xen control device.
455 * \param dev NewBus device_t for this Xen control device instance.
457 * \return On success, 0. Otherwise an errno value indicating the
461 xctrl_detach(device_t dev)
463 struct xctrl_softc *xctrl;
465 xctrl = device_get_softc(dev);
468 xs_unregister_watch(&xctrl->xctrl_watch);
473 /*-------------------- Private Device Attachment Data -----------------------*/
474 static device_method_t xctrl_methods[] = {
475 /* Device interface */
476 DEVMETHOD(device_identify, xctrl_identify),
477 DEVMETHOD(device_probe, xctrl_probe),
478 DEVMETHOD(device_attach, xctrl_attach),
479 DEVMETHOD(device_detach, xctrl_detach),
484 DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc));
485 devclass_t xctrl_devclass;
487 DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, NULL, NULL);