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