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#if defined(__amd64__) || defined(__i386__)
132#include <machine/intr_machdep.h>
133
134#include <x86/apicvar.h>
135#endif
136
137#include <vm/vm.h>
138#include <vm/vm_extern.h>
139#include <vm/vm_kern.h>
140
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>
146
147#include <xen/hvm.h>
148
149#include <contrib/xen/event_channel.h>
150#include <contrib/xen/grant_table.h>
151
152#include <xen/xenbus/xenbusvar.h>
153
154bool xen_suspend_cancelled;
155/*--------------------------- Forward Declarations --------------------------*/
156/** Function signature for shutdown event handlers. */
157typedef	void (xctrl_shutdown_handler_t)(void);
158
159static xctrl_shutdown_handler_t xctrl_poweroff;
160static xctrl_shutdown_handler_t xctrl_reboot;
161static xctrl_shutdown_handler_t xctrl_suspend;
162static xctrl_shutdown_handler_t xctrl_crash;
163
164/*-------------------------- Private Data Structures -------------------------*/
165/** Element type for lookup table of event name to handler. */
166struct xctrl_shutdown_reason {
167	const char		 *name;
168	xctrl_shutdown_handler_t *handler;
169};
170
171/** Lookup table for shutdown event name to handler. */
172static 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 },
178};
179
180struct xctrl_softc {
181	struct xs_watch    xctrl_watch;
182};
183
184/*------------------------------ Event Handlers ------------------------------*/
185static void
186xctrl_poweroff(void)
187{
188	shutdown_nice(RB_POWEROFF|RB_HALT);
189}
190
191static void
192xctrl_reboot(void)
193{
194	shutdown_nice(0);
195}
196
197#if !defined(__amd64__) && !defined(__i386__)
198static void
199xctrl_suspend(void)
200{
201	printf("WARNING: xen/control: Suspend not supported!\n");
202}
203#else /* __amd64__ || __i386__ */
204static void
205xctrl_suspend(void)
206{
207#ifdef SMP
208	cpuset_t cpu_suspend_map;
209#endif
210
211	EVENTHANDLER_INVOKE(power_suspend_early);
212	xs_lock();
213	stop_all_proc();
214	xs_unlock();
215	suspend_all_fs();
216	EVENTHANDLER_INVOKE(power_suspend);
217
218#ifdef EARLY_AP_STARTUP
219	MPASS(mp_ncpus == 1 || smp_started);
220	thread_lock(curthread);
221	sched_bind(curthread, 0);
222	thread_unlock(curthread);
223#else
224	if (smp_started) {
225		thread_lock(curthread);
226		sched_bind(curthread, 0);
227		thread_unlock(curthread);
228	}
229#endif
230	KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0"));
231
232	/*
233	 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME.
234	 */
235	bus_topo_lock();
236	if (DEVICE_SUSPEND(root_bus) != 0) {
237		bus_topo_unlock();
238		printf("%s: device_suspend failed\n", __func__);
239		return;
240	}
241
242#ifdef SMP
243#ifdef EARLY_AP_STARTUP
244	/*
245	 * Suspend other CPUs. This prevents IPIs while we
246	 * are resuming, and will allow us to reset per-cpu
247	 * vcpu_info on resume.
248	 */
249	cpu_suspend_map = all_cpus;
250	CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
251	if (!CPU_EMPTY(&cpu_suspend_map))
252		suspend_cpus(cpu_suspend_map);
253#else
254	CPU_ZERO(&cpu_suspend_map);	/* silence gcc */
255	if (smp_started) {
256		/*
257		 * Suspend other CPUs. This prevents IPIs while we
258		 * are resuming, and will allow us to reset per-cpu
259		 * vcpu_info on resume.
260		 */
261		cpu_suspend_map = all_cpus;
262		CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map);
263		if (!CPU_EMPTY(&cpu_suspend_map))
264			suspend_cpus(cpu_suspend_map);
265	}
266#endif
267#endif
268
269	/*
270	 * Prevent any races with evtchn_interrupt() handler.
271	 */
272	disable_intr();
273	intr_suspend();
274	xen_hvm_suspend();
275
276	xen_suspend_cancelled = !!HYPERVISOR_suspend(0);
277
278	if (!xen_suspend_cancelled) {
279		xen_hvm_resume(false);
280	}
281	intr_resume(xen_suspend_cancelled != 0);
282	enable_intr();
283
284	/*
285	 * Reset grant table info.
286	 */
287	if (!xen_suspend_cancelled) {
288		gnttab_resume(NULL);
289	}
290
291#ifdef SMP
292	if (!CPU_EMPTY(&cpu_suspend_map)) {
293		/*
294		 * Now that event channels have been initialized,
295		 * resume CPUs.
296		 */
297		resume_cpus(cpu_suspend_map);
298#if defined(__amd64__) || defined(__i386__)
299		/* Send an IPI_BITMAP in case there are pending bitmap IPIs. */
300		lapic_ipi_vectored(IPI_BITMAP_VECTOR, APIC_IPI_DEST_ALL);
301#endif
302	}
303#endif
304
305	/*
306	 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or
307	 * similar.
308	 */
309	DEVICE_RESUME(root_bus);
310	bus_topo_unlock();
311
312	/*
313	 * Warm up timecounter again and reset system clock.
314	 */
315	timecounter->tc_get_timecount(timecounter);
316	inittodr(time_second);
317
318#ifdef EARLY_AP_STARTUP
319	thread_lock(curthread);
320	sched_unbind(curthread);
321	thread_unlock(curthread);
322#else
323	if (smp_started) {
324		thread_lock(curthread);
325		sched_unbind(curthread);
326		thread_unlock(curthread);
327	}
328#endif
329
330	resume_all_fs();
331	resume_all_proc();
332
333	EVENTHANDLER_INVOKE(power_resume);
334
335	if (bootverbose)
336		printf("System resumed after suspension\n");
337
338}
339#endif /* __amd64__ || __i386__ */
340
341static void
342xctrl_crash(void)
343{
344	panic("Xen directed crash");
345}
346
347static void
348xctrl_shutdown_final(void *arg, int howto)
349{
350	/*
351	 * Inform the hypervisor that shutdown is complete, and specify the
352	 * nature of the shutdown. RB_HALT is not handled by this function.
353	 */
354	if (KERNEL_PANICKED())
355		HYPERVISOR_shutdown(SHUTDOWN_crash);
356	else if ((howto & RB_POWEROFF) != 0)
357		HYPERVISOR_shutdown(SHUTDOWN_poweroff);
358	else if ((howto & RB_HALT) == 0)
359		/* RB_POWERCYCLE or regular reset. */
360		HYPERVISOR_shutdown(SHUTDOWN_reboot);
361}
362
363/*------------------------------ Event Reception -----------------------------*/
364static void
365xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len)
366{
367	const struct xctrl_shutdown_reason *reason;
368	const struct xctrl_shutdown_reason *last_reason;
369	char *result;
370	int   error;
371	int   result_len;
372
373	error = xs_read(XST_NIL, "control", "shutdown",
374			&result_len, (void **)&result);
375	if (error != 0 || result_len == 0)
376		return;
377
378	/* Acknowledge the request by writing back an empty string. */
379	error = xs_write(XST_NIL, "control", "shutdown", "");
380	if (error != 0)
381		printf("unable to ack shutdown request, proceeding anyway\n");
382
383	reason = xctrl_shutdown_reasons;
384	last_reason = reason + nitems(xctrl_shutdown_reasons);
385	while (reason < last_reason) {
386		if (!strcmp(result, reason->name)) {
387			reason->handler();
388			break;
389		}
390		reason++;
391	}
392
393	free(result, M_XENSTORE);
394}
395
396/*------------------ Private Device Attachment Functions  --------------------*/
397/**
398 * \brief Identify instances of this device type in the system.
399 *
400 * \param driver  The driver performing this identify action.
401 * \param parent  The NewBus parent device for any devices this method adds.
402 */
403static void
404xctrl_identify(driver_t *driver, device_t parent)
405{
406	/*
407	 * A single device instance for our driver is always present
408	 * in a system operating under Xen.
409	 */
410	BUS_ADD_CHILD(parent, 0, driver->name, 0);
411}
412
413/**
414 * \brief Probe for the existence of the Xen Control device
415 *
416 * \param dev  NewBus device_t for this Xen control instance.
417 *
418 * \return  Always returns 0 indicating success.
419 */
420static int
421xctrl_probe(device_t dev)
422{
423	device_set_desc(dev, "Xen Control Device");
424
425	return (BUS_PROBE_NOWILDCARD);
426}
427
428/**
429 * \brief Attach the Xen control device.
430 *
431 * \param dev  NewBus device_t for this Xen control instance.
432 *
433 * \return  On success, 0. Otherwise an errno value indicating the
434 *          type of failure.
435 */
436static int
437xctrl_attach(device_t dev)
438{
439	struct xctrl_softc *xctrl;
440
441	xctrl = device_get_softc(dev);
442
443	/* Activate watch */
444	xctrl->xctrl_watch.node = "control/shutdown";
445	xctrl->xctrl_watch.callback = xctrl_on_watch_event;
446	xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl;
447	/*
448	 * We don't care about the path updated, just about the value changes
449	 * on that single node, hence there's no need to queue more that one
450	 * event.
451	 */
452	xctrl->xctrl_watch.max_pending = 1;
453	xs_register_watch(&xctrl->xctrl_watch);
454
455	EVENTHANDLER_REGISTER(shutdown_final, xctrl_shutdown_final, NULL,
456	    SHUTDOWN_PRI_LAST);
457
458	return (0);
459}
460
461/**
462 * \brief Detach the Xen control device.
463 *
464 * \param dev  NewBus device_t for this Xen control device instance.
465 *
466 * \return  On success, 0. Otherwise an errno value indicating the
467 *          type of failure.
468 */
469static int
470xctrl_detach(device_t dev)
471{
472	struct xctrl_softc *xctrl;
473
474	xctrl = device_get_softc(dev);
475
476	/* Release watch */
477	xs_unregister_watch(&xctrl->xctrl_watch);
478
479	return (0);
480}
481
482/*-------------------- Private Device Attachment Data  -----------------------*/
483static device_method_t xctrl_methods[] = {
484	/* Device interface */
485	DEVMETHOD(device_identify,	xctrl_identify),
486	DEVMETHOD(device_probe,         xctrl_probe),
487	DEVMETHOD(device_attach,        xctrl_attach),
488	DEVMETHOD(device_detach,        xctrl_detach),
489
490	DEVMETHOD_END
491};
492
493DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc));
494
495DRIVER_MODULE(xctrl, xenstore, xctrl_driver, NULL, NULL);
496