3 * Copyright (c) 2004 Christian Limpach.
4 * Copyright (c) 2004-2006,2008 Kip Macy
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 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christian Limpach.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
41 #include <sys/reboot.h>
42 #include <sys/sched.h>
44 #include <sys/systm.h>
46 #include <machine/xen/xen-os.h>
47 #include <xen/hypervisor.h>
48 #include <xen/gnttab.h>
49 #include <xen/xen_intr.h>
50 #include <xen/xenbus/xenbusvar.h>
57 #include <dev/xen/xenpci/xenpcivar.h>
61 static void xen_suspend(void);
66 shutdown_handler(struct xenbus_watch *watch,
67 const char **vec, unsigned int len)
70 struct xenbus_transaction xbt;
76 error = xenbus_transaction_start(&xbt);
80 error = xenbus_read(xbt, "control", "shutdown", NULL, (void **) &str);
82 /* Ignore read errors and empty reads. */
83 if (error || strlen(str) == 0) {
84 xenbus_transaction_end(xbt, 1);
88 xenbus_write(xbt, "control", "shutdown", "");
90 error = xenbus_transaction_end(xbt, 0);
91 if (error == EAGAIN) {
96 if (strcmp(str, "reboot") == 0)
98 else if (strcmp(str, "poweroff") == 0)
99 howto |= (RB_POWEROFF | RB_HALT);
100 else if (strcmp(str, "halt") == 0)
103 * We rely on acpi powerdown to halt the VM.
105 howto |= (RB_POWEROFF | RB_HALT);
109 else if (strcmp(str, "suspend") == 0)
112 printf("Ignoring shutdown request: %s\n", str);
121 shutdown_nice(howto);
129 * In HV mode, we let acpi take care of halts and reboots.
133 xen_shutdown_final(void *arg, int howto)
136 if (howto & (RB_HALT | RB_POWEROFF))
137 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
139 HYPERVISOR_shutdown(SHUTDOWN_reboot);
144 static struct xenbus_watch shutdown_watch = {
145 .node = "control/shutdown",
146 .callback = shutdown_handler
150 setup_shutdown_watcher(void *unused)
153 if (register_xenbus_watch(&shutdown_watch))
154 printf("Failed to set shutdown watcher\n");
156 EVENTHANDLER_REGISTER(shutdown_final, xen_shutdown_final, NULL,
161 SYSINIT(shutdown, SI_SUB_PSEUDO, SI_ORDER_ANY, setup_shutdown_watcher, NULL);
165 extern void xencons_suspend(void);
166 extern void xencons_resume(void);
172 unsigned long max_pfn, start_info_mfn;
177 * Bind us to CPU 0 and stop any other VCPUs.
179 thread_lock(curthread);
180 sched_bind(curthread, 0);
181 thread_unlock(curthread);
182 KASSERT(PCPU_GET(cpuid) == 0, ("xen_suspend: not running on cpu 0"));
184 map = PCPU_GET(other_cpus) & ~stopped_cpus;
189 if (DEVICE_SUSPEND(root_bus) != 0) {
190 printf("xen_suspend: device_suspend failed\n");
203 max_pfn = HYPERVISOR_shared_info->arch.max_pfn;
205 void *shared_info = HYPERVISOR_shared_info;
206 HYPERVISOR_shared_info = NULL;
207 pmap_kremove((vm_offset_t) shared_info);
210 xen_start_info->store_mfn = MFNTOPFN(xen_start_info->store_mfn);
211 xen_start_info->console.domU.mfn = MFNTOPFN(xen_start_info->console.domU.mfn);
214 * We'll stop somewhere inside this hypercall. When it returns,
215 * we'll start resuming after the restore.
217 start_info_mfn = VTOMFN(xen_start_info);
219 HYPERVISOR_suspend(start_info_mfn);
222 pmap_kenter_ma((vm_offset_t) shared_info, xen_start_info->shared_info);
223 HYPERVISOR_shared_info = shared_info;
225 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
226 VTOMFN(xen_pfn_to_mfn_frame_list_list);
228 fpp = PAGE_SIZE/sizeof(unsigned long);
229 for (i = 0, j = 0, k = -1; i < max_pfn; i += fpp, j++) {
230 if ((j % fpp) == 0) {
232 xen_pfn_to_mfn_frame_list_list[k] =
233 VTOMFN(xen_pfn_to_mfn_frame_list[k]);
236 xen_pfn_to_mfn_frame_list[k][j] =
237 VTOMFN(&xen_phys_machine[i]);
239 HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
252 * Only resume xenbus /after/ we've prepared our VCPUs; otherwise
253 * the VCPU hotplug callback can race with our vcpu_prepare
255 DEVICE_RESUME(root_bus);
258 thread_lock(curthread);
259 sched_unbind(curthread);
260 thread_unlock(curthread);