xref: /freebsd/sys/dev/xen/control/control.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
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 
126 #include <geom/geom.h>
127 
128 #include <machine/_inttypes.h>
129 #include <machine/intr_machdep.h>
130 
131 #include <x86/apicvar.h>
132 
133 #include <vm/vm.h>
134 #include <vm/vm_extern.h>
135 #include <vm/vm_kern.h>
136 
137 #include <xen/xen-os.h>
138 #include <xen/blkif.h>
139 #include <xen/evtchn.h>
140 #include <xen/gnttab.h>
141 #include <xen/xen_intr.h>
142 
143 #include <xen/hvm.h>
144 
145 #include <xen/interface/event_channel.h>
146 #include <xen/interface/grant_table.h>
147 
148 #include <xen/xenbus/xenbusvar.h>
149 
150 /*--------------------------- Forward Declarations --------------------------*/
151 /** Function signature for shutdown event handlers. */
152 typedef	void (xctrl_shutdown_handler_t)(void);
153 
154 static xctrl_shutdown_handler_t xctrl_poweroff;
155 static xctrl_shutdown_handler_t xctrl_reboot;
156 static xctrl_shutdown_handler_t xctrl_suspend;
157 static xctrl_shutdown_handler_t xctrl_crash;
158 
159 /*-------------------------- Private Data Structures -------------------------*/
160 /** Element type for lookup table of event name to handler. */
161 struct xctrl_shutdown_reason {
162 	const char		 *name;
163 	xctrl_shutdown_handler_t *handler;
164 };
165 
166 /** Lookup table for shutdown event name to handler. */
167 static const 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_poweroff },
173 };
174 
175 struct xctrl_softc {
176 	struct xs_watch    xctrl_watch;
177 };
178 
179 /*------------------------------ Event Handlers ------------------------------*/
180 static void
181 xctrl_poweroff()
182 {
183 	shutdown_nice(RB_POWEROFF|RB_HALT);
184 }
185 
186 static void
187 xctrl_reboot()
188 {
189 	shutdown_nice(0);
190 }
191 
192 static void
193 xctrl_suspend()
194 {
195 #ifdef SMP
196 	cpuset_t cpu_suspend_map;
197 #endif
198 	int suspend_cancelled;
199 
200 	EVENTHANDLER_INVOKE(power_suspend);
201 
202 	if (smp_started) {
203 		thread_lock(curthread);
204 		sched_bind(curthread, 0);
205 		thread_unlock(curthread);
206 	}
207 	KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0"));
208 
209 	/*
210 	 * Clear our XenStore node so the toolstack knows we are
211 	 * responding to the suspend request.
212 	 */
213 	xs_write(XST_NIL, "control", "shutdown", "");
214 
215 	/*
216 	 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE
217 	 * drivers need this.
218 	 */
219 	mtx_lock(&Giant);
220 	if (DEVICE_SUSPEND(root_bus) != 0) {
221 		mtx_unlock(&Giant);
222 		printf("%s: device_suspend failed\n", __func__);
223 		return;
224 	}
225 	mtx_unlock(&Giant);
226 
227 #ifdef SMP
228 	CPU_ZERO(&cpu_suspend_map);	/* silence gcc */
229 	if (smp_started) {
230 		/*
231 		 * Suspend other CPUs. This prevents IPIs while we
232 		 * are resuming, and will allow us to reset per-cpu
233 		 * vcpu_info on resume.
234 		 */
235 		cpu_suspend_map = all_cpus;
236 		CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
237 		if (!CPU_EMPTY(&cpu_suspend_map))
238 			suspend_cpus(cpu_suspend_map);
239 	}
240 #endif
241 
242 	/*
243 	 * Prevent any races with evtchn_interrupt() handler.
244 	 */
245 	disable_intr();
246 	intr_suspend();
247 	xen_hvm_suspend();
248 
249 	suspend_cancelled = HYPERVISOR_suspend(0);
250 
251 	xen_hvm_resume(suspend_cancelled != 0);
252 	intr_resume(suspend_cancelled != 0);
253 	enable_intr();
254 
255 	/*
256 	 * Reset grant table info.
257 	 */
258 	gnttab_resume(NULL);
259 
260 #ifdef SMP
261 	/* Send an IPI_BITMAP in case there are pending bitmap IPIs. */
262 	lapic_ipi_vectored(IPI_BITMAP_VECTOR, APIC_IPI_DEST_ALL);
263 	if (!CPU_EMPTY(&cpu_suspend_map)) {
264 		/*
265 		 * Now that event channels have been initialized,
266 		 * resume CPUs.
267 		 */
268 		resume_cpus(cpu_suspend_map);
269 	}
270 #endif
271 
272 	/*
273 	 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or
274 	 * similar.
275 	 */
276 	mtx_lock(&Giant);
277 	DEVICE_RESUME(root_bus);
278 	mtx_unlock(&Giant);
279 
280 	if (smp_started) {
281 		thread_lock(curthread);
282 		sched_unbind(curthread);
283 		thread_unlock(curthread);
284 	}
285 
286 	EVENTHANDLER_INVOKE(power_resume);
287 
288 	if (bootverbose)
289 		printf("System resumed after suspension\n");
290 
291 }
292 
293 static void
294 xctrl_crash()
295 {
296 	panic("Xen directed crash");
297 }
298 
299 static void
300 xen_pv_shutdown_final(void *arg, int howto)
301 {
302 	/*
303 	 * Inform the hypervisor that shutdown is complete.
304 	 * This is not necessary in HVM domains since Xen
305 	 * emulates ACPI in that mode and FreeBSD's ACPI
306 	 * support will request this transition.
307 	 */
308 	if (howto & (RB_HALT | RB_POWEROFF))
309 		HYPERVISOR_shutdown(SHUTDOWN_poweroff);
310 	else
311 		HYPERVISOR_shutdown(SHUTDOWN_reboot);
312 }
313 
314 /*------------------------------ Event Reception -----------------------------*/
315 static void
316 xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len)
317 {
318 	const struct xctrl_shutdown_reason *reason;
319 	const struct xctrl_shutdown_reason *last_reason;
320 	char *result;
321 	int   error;
322 	int   result_len;
323 
324 	error = xs_read(XST_NIL, "control", "shutdown",
325 			&result_len, (void **)&result);
326 	if (error != 0)
327 		return;
328 
329 	reason = xctrl_shutdown_reasons;
330 	last_reason = reason + nitems(xctrl_shutdown_reasons);
331 	while (reason < last_reason) {
332 
333 		if (!strcmp(result, reason->name)) {
334 			reason->handler();
335 			break;
336 		}
337 		reason++;
338 	}
339 
340 	free(result, M_XENSTORE);
341 }
342 
343 /*------------------ Private Device Attachment Functions  --------------------*/
344 /**
345  * \brief Identify instances of this device type in the system.
346  *
347  * \param driver  The driver performing this identify action.
348  * \param parent  The NewBus parent device for any devices this method adds.
349  */
350 static void
351 xctrl_identify(driver_t *driver __unused, device_t parent)
352 {
353 	/*
354 	 * A single device instance for our driver is always present
355 	 * in a system operating under Xen.
356 	 */
357 	BUS_ADD_CHILD(parent, 0, driver->name, 0);
358 }
359 
360 /**
361  * \brief Probe for the existance of the Xen Control device
362  *
363  * \param dev  NewBus device_t for this Xen control instance.
364  *
365  * \return  Always returns 0 indicating success.
366  */
367 static int
368 xctrl_probe(device_t dev)
369 {
370 	device_set_desc(dev, "Xen Control Device");
371 
372 	return (BUS_PROBE_NOWILDCARD);
373 }
374 
375 /**
376  * \brief Attach the Xen control device.
377  *
378  * \param dev  NewBus device_t for this Xen control instance.
379  *
380  * \return  On success, 0. Otherwise an errno value indicating the
381  *          type of failure.
382  */
383 static int
384 xctrl_attach(device_t dev)
385 {
386 	struct xctrl_softc *xctrl;
387 
388 	xctrl = device_get_softc(dev);
389 
390 	/* Activate watch */
391 	xctrl->xctrl_watch.node = "control/shutdown";
392 	xctrl->xctrl_watch.callback = xctrl_on_watch_event;
393 	xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl;
394 	xs_register_watch(&xctrl->xctrl_watch);
395 
396 	if (xen_pv_domain())
397 		EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL,
398 		                      SHUTDOWN_PRI_LAST);
399 
400 	return (0);
401 }
402 
403 /**
404  * \brief Detach the Xen control device.
405  *
406  * \param dev  NewBus device_t for this Xen control device instance.
407  *
408  * \return  On success, 0. Otherwise an errno value indicating the
409  *          type of failure.
410  */
411 static int
412 xctrl_detach(device_t dev)
413 {
414 	struct xctrl_softc *xctrl;
415 
416 	xctrl = device_get_softc(dev);
417 
418 	/* Release watch */
419 	xs_unregister_watch(&xctrl->xctrl_watch);
420 
421 	return (0);
422 }
423 
424 /*-------------------- Private Device Attachment Data  -----------------------*/
425 static device_method_t xctrl_methods[] = {
426 	/* Device interface */
427 	DEVMETHOD(device_identify,	xctrl_identify),
428 	DEVMETHOD(device_probe,         xctrl_probe),
429 	DEVMETHOD(device_attach,        xctrl_attach),
430 	DEVMETHOD(device_detach,        xctrl_detach),
431 
432 	DEVMETHOD_END
433 };
434 
435 DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc));
436 devclass_t xctrl_devclass;
437 
438 DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, NULL, NULL);
439