1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause AND BSD-4-Clause
3 *
4 * Copyright (c) 2010 Justin T. Gibbs, Spectra Logic Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
19 * NO WARRANTY
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.
31 */
32
33 /*-
34 * PV suspend/resume support:
35 *
36 * Copyright (c) 2004 Christian Limpach.
37 * Copyright (c) 2004-2006,2008 Kip Macy
38 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
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.
53 *
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.
64 */
65
66 /*-
67 * HVM suspend/resume support:
68 *
69 * Copyright (c) 2008 Citrix Systems, Inc.
70 * All rights reserved.
71 *
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
74 * are met:
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.
80 *
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
91 * SUCH DAMAGE.
92 */
93 #include <sys/cdefs.h>
94 /**
95 * \file control.c
96 *
97 * \brief Device driver to repond to control domain events that impact
98 * this VM.
99 */
100
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/kernel.h>
104 #include <sys/malloc.h>
105
106 #include <sys/bio.h>
107 #include <sys/bus.h>
108 #include <sys/conf.h>
109 #include <sys/disk.h>
110 #include <sys/fcntl.h>
111 #include <sys/filedesc.h>
112 #include <sys/kdb.h>
113 #include <sys/module.h>
114 #include <sys/mount.h>
115 #include <sys/namei.h>
116 #include <sys/proc.h>
117 #include <sys/reboot.h>
118 #include <sys/rman.h>
119 #include <sys/sched.h>
120 #include <sys/taskqueue.h>
121 #include <sys/types.h>
122 #include <sys/vnode.h>
123 #include <sys/sched.h>
124 #include <sys/smp.h>
125 #include <sys/eventhandler.h>
126 #include <sys/timetc.h>
127
128 #include <geom/geom.h>
129
130 #include <machine/_inttypes.h>
131 #include <machine/intr_machdep.h>
132
133 #include <x86/apicvar.h>
134
135 #include <vm/vm.h>
136 #include <vm/vm_extern.h>
137 #include <vm/vm_kern.h>
138
139 #include <xen/xen-os.h>
140 #include <xen/blkif.h>
141 #include <xen/evtchn.h>
142 #include <xen/gnttab.h>
143 #include <xen/xen_intr.h>
144
145 #include <xen/hvm.h>
146
147 #include <xen/interface/event_channel.h>
148 #include <xen/interface/grant_table.h>
149
150 #include <xen/xenbus/xenbusvar.h>
151
152 bool xen_suspend_cancelled;
153 /*--------------------------- Forward Declarations --------------------------*/
154 /** Function signature for shutdown event handlers. */
155 typedef void (xctrl_shutdown_handler_t)(void);
156
157 static xctrl_shutdown_handler_t xctrl_poweroff;
158 static xctrl_shutdown_handler_t xctrl_reboot;
159 static xctrl_shutdown_handler_t xctrl_suspend;
160 static xctrl_shutdown_handler_t xctrl_crash;
161
162 /*-------------------------- Private Data Structures -------------------------*/
163 /** Element type for lookup table of event name to handler. */
164 struct xctrl_shutdown_reason {
165 const char *name;
166 xctrl_shutdown_handler_t *handler;
167 };
168
169 /** Lookup table for shutdown event name to handler. */
170 static const struct xctrl_shutdown_reason xctrl_shutdown_reasons[] = {
171 { "poweroff", xctrl_poweroff },
172 { "reboot", xctrl_reboot },
173 { "suspend", xctrl_suspend },
174 { "crash", xctrl_crash },
175 { "halt", xctrl_poweroff },
176 };
177
178 struct xctrl_softc {
179 struct xs_watch xctrl_watch;
180 };
181
182 /*------------------------------ Event Handlers ------------------------------*/
183 static void
xctrl_poweroff(void)184 xctrl_poweroff(void)
185 {
186 shutdown_nice(RB_POWEROFF|RB_HALT);
187 }
188
189 static void
xctrl_reboot(void)190 xctrl_reboot(void)
191 {
192 shutdown_nice(0);
193 }
194
195 static void
xctrl_suspend(void)196 xctrl_suspend(void)
197 {
198 #ifdef SMP
199 cpuset_t cpu_suspend_map;
200 #endif
201
202 EVENTHANDLER_INVOKE(power_suspend_early);
203 xs_lock();
204 stop_all_proc();
205 xs_unlock();
206 suspend_all_fs();
207 EVENTHANDLER_INVOKE(power_suspend);
208
209 #ifdef EARLY_AP_STARTUP
210 MPASS(mp_ncpus == 1 || smp_started);
211 thread_lock(curthread);
212 sched_bind(curthread, 0);
213 thread_unlock(curthread);
214 #else
215 if (smp_started) {
216 thread_lock(curthread);
217 sched_bind(curthread, 0);
218 thread_unlock(curthread);
219 }
220 #endif
221 KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0"));
222
223 /*
224 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME.
225 */
226 bus_topo_lock();
227 if (DEVICE_SUSPEND(root_bus) != 0) {
228 bus_topo_unlock();
229 printf("%s: device_suspend failed\n", __func__);
230 return;
231 }
232
233 #ifdef SMP
234 #ifdef EARLY_AP_STARTUP
235 /*
236 * Suspend other CPUs. This prevents IPIs while we
237 * are resuming, and will allow us to reset per-cpu
238 * vcpu_info on resume.
239 */
240 cpu_suspend_map = all_cpus;
241 CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
242 if (!CPU_EMPTY(&cpu_suspend_map))
243 suspend_cpus(cpu_suspend_map);
244 #else
245 CPU_ZERO(&cpu_suspend_map); /* silence gcc */
246 if (smp_started) {
247 /*
248 * Suspend other CPUs. This prevents IPIs while we
249 * are resuming, and will allow us to reset per-cpu
250 * vcpu_info on resume.
251 */
252 cpu_suspend_map = all_cpus;
253 CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
254 if (!CPU_EMPTY(&cpu_suspend_map))
255 suspend_cpus(cpu_suspend_map);
256 }
257 #endif
258 #endif
259
260 /*
261 * Prevent any races with evtchn_interrupt() handler.
262 */
263 disable_intr();
264 intr_suspend();
265 xen_hvm_suspend();
266
267 xen_suspend_cancelled = !!HYPERVISOR_suspend(0);
268
269 if (!xen_suspend_cancelled) {
270 xen_hvm_resume(false);
271 }
272 intr_resume(xen_suspend_cancelled != 0);
273 enable_intr();
274
275 /*
276 * Reset grant table info.
277 */
278 if (!xen_suspend_cancelled) {
279 gnttab_resume(NULL);
280 }
281
282 #ifdef SMP
283 if (!CPU_EMPTY(&cpu_suspend_map)) {
284 /*
285 * Now that event channels have been initialized,
286 * resume CPUs.
287 */
288 resume_cpus(cpu_suspend_map);
289 /* Send an IPI_BITMAP in case there are pending bitmap IPIs. */
290 lapic_ipi_vectored(IPI_BITMAP_VECTOR, APIC_IPI_DEST_ALL);
291 }
292 #endif
293
294 /*
295 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or
296 * similar.
297 */
298 DEVICE_RESUME(root_bus);
299 bus_topo_unlock();
300
301 /*
302 * Warm up timecounter again and reset system clock.
303 */
304 timecounter->tc_get_timecount(timecounter);
305 inittodr(time_second);
306
307 #ifdef EARLY_AP_STARTUP
308 thread_lock(curthread);
309 sched_unbind(curthread);
310 thread_unlock(curthread);
311 #else
312 if (smp_started) {
313 thread_lock(curthread);
314 sched_unbind(curthread);
315 thread_unlock(curthread);
316 }
317 #endif
318
319 resume_all_fs();
320 resume_all_proc();
321
322 EVENTHANDLER_INVOKE(power_resume);
323
324 if (bootverbose)
325 printf("System resumed after suspension\n");
326
327 }
328
329 static void
xctrl_crash(void)330 xctrl_crash(void)
331 {
332 panic("Xen directed crash");
333 }
334
335 static void
xen_pv_shutdown_final(void * arg,int howto)336 xen_pv_shutdown_final(void *arg, int howto)
337 {
338 /*
339 * Inform the hypervisor that shutdown is complete.
340 * This is not necessary in HVM domains since Xen
341 * emulates ACPI in that mode and FreeBSD's ACPI
342 * support will request this transition.
343 */
344 if (howto & (RB_HALT | RB_POWEROFF))
345 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
346 else
347 HYPERVISOR_shutdown(SHUTDOWN_reboot);
348 }
349
350 /*------------------------------ Event Reception -----------------------------*/
351 static void
xctrl_on_watch_event(struct xs_watch * watch,const char ** vec,unsigned int len)352 xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len)
353 {
354 const struct xctrl_shutdown_reason *reason;
355 const struct xctrl_shutdown_reason *last_reason;
356 char *result;
357 int error;
358 int result_len;
359
360 error = xs_read(XST_NIL, "control", "shutdown",
361 &result_len, (void **)&result);
362 if (error != 0 || result_len == 0)
363 return;
364
365 /* Acknowledge the request by writing back an empty string. */
366 error = xs_write(XST_NIL, "control", "shutdown", "");
367 if (error != 0)
368 printf("unable to ack shutdown request, proceeding anyway\n");
369
370 reason = xctrl_shutdown_reasons;
371 last_reason = reason + nitems(xctrl_shutdown_reasons);
372 while (reason < last_reason) {
373 if (!strcmp(result, reason->name)) {
374 reason->handler();
375 break;
376 }
377 reason++;
378 }
379
380 free(result, M_XENSTORE);
381 }
382
383 /*------------------ Private Device Attachment Functions --------------------*/
384 /**
385 * \brief Identify instances of this device type in the system.
386 *
387 * \param driver The driver performing this identify action.
388 * \param parent The NewBus parent device for any devices this method adds.
389 */
390 static void
xctrl_identify(driver_t * driver __unused,device_t parent)391 xctrl_identify(driver_t *driver __unused, device_t parent)
392 {
393 /*
394 * A single device instance for our driver is always present
395 * in a system operating under Xen.
396 */
397 BUS_ADD_CHILD(parent, 0, driver->name, 0);
398 }
399
400 /**
401 * \brief Probe for the existence of the Xen Control device
402 *
403 * \param dev NewBus device_t for this Xen control instance.
404 *
405 * \return Always returns 0 indicating success.
406 */
407 static int
xctrl_probe(device_t dev)408 xctrl_probe(device_t dev)
409 {
410 device_set_desc(dev, "Xen Control Device");
411
412 return (BUS_PROBE_NOWILDCARD);
413 }
414
415 /**
416 * \brief Attach the Xen control device.
417 *
418 * \param dev NewBus device_t for this Xen control instance.
419 *
420 * \return On success, 0. Otherwise an errno value indicating the
421 * type of failure.
422 */
423 static int
xctrl_attach(device_t dev)424 xctrl_attach(device_t dev)
425 {
426 struct xctrl_softc *xctrl;
427
428 xctrl = device_get_softc(dev);
429
430 /* Activate watch */
431 xctrl->xctrl_watch.node = "control/shutdown";
432 xctrl->xctrl_watch.callback = xctrl_on_watch_event;
433 xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl;
434 /*
435 * We don't care about the path updated, just about the value changes
436 * on that single node, hence there's no need to queue more that one
437 * event.
438 */
439 xctrl->xctrl_watch.max_pending = 1;
440 xs_register_watch(&xctrl->xctrl_watch);
441
442 if (xen_pv_domain())
443 EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL,
444 SHUTDOWN_PRI_LAST);
445
446 return (0);
447 }
448
449 /**
450 * \brief Detach the Xen control device.
451 *
452 * \param dev NewBus device_t for this Xen control device instance.
453 *
454 * \return On success, 0. Otherwise an errno value indicating the
455 * type of failure.
456 */
457 static int
xctrl_detach(device_t dev)458 xctrl_detach(device_t dev)
459 {
460 struct xctrl_softc *xctrl;
461
462 xctrl = device_get_softc(dev);
463
464 /* Release watch */
465 xs_unregister_watch(&xctrl->xctrl_watch);
466
467 return (0);
468 }
469
470 /*-------------------- Private Device Attachment Data -----------------------*/
471 static device_method_t xctrl_methods[] = {
472 /* Device interface */
473 DEVMETHOD(device_identify, xctrl_identify),
474 DEVMETHOD(device_probe, xctrl_probe),
475 DEVMETHOD(device_attach, xctrl_attach),
476 DEVMETHOD(device_detach, xctrl_detach),
477
478 DEVMETHOD_END
479 };
480
481 DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc));
482 devclass_t xctrl_devclass;
483
484 DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, NULL, NULL);
485