]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/xen/reboot.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / xen / reboot.c
1 /*
2  *
3  * Copyright (c) 2004 Christian Limpach.
4  * Copyright (c) 2004-2006,2008 Kip Macy
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  * 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.
20  *
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.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/reboot.h>
42 #include <sys/sched.h>
43 #include <sys/smp.h>
44 #include <sys/systm.h>
45
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>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54
55 #ifdef XENHVM
56
57 #include <dev/xen/xenpci/xenpcivar.h>
58
59 #else
60
61 static void xen_suspend(void);
62
63 #endif
64
65 static void 
66 shutdown_handler(struct xenbus_watch *watch,
67                  const char **vec, unsigned int len)
68 {
69         char *str;
70         struct xenbus_transaction xbt;
71         int error, howto;
72         
73         howto = 0;
74
75  again:
76         error = xenbus_transaction_start(&xbt);
77         if (error)
78                 return;
79
80         error = xenbus_read(xbt, "control", "shutdown", NULL, (void **) &str);
81
82         /* Ignore read errors and empty reads. */
83         if (error || strlen(str) == 0) {
84                 xenbus_transaction_end(xbt, 1);
85                 return;
86         }
87
88         xenbus_write(xbt, "control", "shutdown", "");
89
90         error = xenbus_transaction_end(xbt, 0);
91         if (error == EAGAIN) {
92                 free(str, M_DEVBUF);
93                 goto again;
94         }
95
96         if (strcmp(str, "reboot") == 0)
97                 howto = 0;
98         else if (strcmp(str, "poweroff") == 0)
99                 howto |= (RB_POWEROFF | RB_HALT);
100         else if (strcmp(str, "halt") == 0)
101 #ifdef XENHVM
102                 /*
103                  * We rely on acpi powerdown to halt the VM.
104                  */
105                 howto |= (RB_POWEROFF | RB_HALT);
106 #else
107                 howto |= RB_HALT;
108 #endif
109         else if (strcmp(str, "suspend") == 0)
110                 howto = -1;
111         else {
112                 printf("Ignoring shutdown request: %s\n", str);
113                 goto done;
114         }
115
116         if (howto == -1) {
117                 xen_suspend();
118                 goto done;
119         }
120
121         shutdown_nice(howto);
122  done:
123         free(str, M_DEVBUF);
124 }
125
126 #ifndef XENHVM
127
128 /*
129  * In HV mode, we let acpi take care of halts and reboots.
130  */
131
132 static void
133 xen_shutdown_final(void *arg, int howto)
134 {
135
136         if (howto & (RB_HALT | RB_POWEROFF))
137                 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
138         else
139                 HYPERVISOR_shutdown(SHUTDOWN_reboot);
140 }
141
142 #endif
143
144 static struct xenbus_watch shutdown_watch = {
145         .node = "control/shutdown",
146         .callback = shutdown_handler
147 };
148
149 static void
150 setup_shutdown_watcher(void *unused)
151 {
152
153         if (register_xenbus_watch(&shutdown_watch))
154                 printf("Failed to set shutdown watcher\n");
155 #ifndef XENHVM
156         EVENTHANDLER_REGISTER(shutdown_final, xen_shutdown_final, NULL,
157             SHUTDOWN_PRI_LAST);
158 #endif
159 }
160
161 SYSINIT(shutdown, SI_SUB_PSEUDO, SI_ORDER_ANY, setup_shutdown_watcher, NULL);
162
163 #ifndef XENHVM
164
165 extern void xencons_suspend(void);
166 extern void xencons_resume(void);
167
168 static void 
169 xen_suspend()
170 {
171         int i, j, k, fpp;
172         unsigned long max_pfn, start_info_mfn;
173
174 #ifdef SMP
175         cpumask_t map;
176         /*
177          * Bind us to CPU 0 and stop any other VCPUs.
178          */
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"));
183
184         map = PCPU_GET(other_cpus) & ~stopped_cpus;
185         if (map)
186                 stop_cpus(map);
187 #endif
188
189         if (DEVICE_SUSPEND(root_bus) != 0) {
190                 printf("xen_suspend: device_suspend failed\n");
191 #ifdef SMP
192                 if (map)
193                         restart_cpus(map);
194 #endif
195                 return;
196         }
197
198         local_irq_disable();
199
200         xencons_suspend();
201         gnttab_suspend();
202
203         max_pfn = HYPERVISOR_shared_info->arch.max_pfn;
204
205         void *shared_info = HYPERVISOR_shared_info;
206         HYPERVISOR_shared_info = NULL;
207         pmap_kremove((vm_offset_t) shared_info);
208         PT_UPDATES_FLUSH();
209
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);
212
213         /*
214          * We'll stop somewhere inside this hypercall. When it returns,
215          * we'll start resuming after the restore.
216          */
217         start_info_mfn = VTOMFN(xen_start_info);
218         pmap_suspend();
219         HYPERVISOR_suspend(start_info_mfn);
220         pmap_resume();
221
222         pmap_kenter_ma((vm_offset_t) shared_info, xen_start_info->shared_info);
223         HYPERVISOR_shared_info = shared_info;
224
225         HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list =
226                 VTOMFN(xen_pfn_to_mfn_frame_list_list);
227   
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) {
231                         k++;
232                         xen_pfn_to_mfn_frame_list_list[k] = 
233                                 VTOMFN(xen_pfn_to_mfn_frame_list[k]);
234                         j = 0;
235                 }
236                 xen_pfn_to_mfn_frame_list[k][j] = 
237                         VTOMFN(&xen_phys_machine[i]);
238         }
239         HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
240
241         gnttab_resume();
242         irq_resume();
243         local_irq_enable();
244         xencons_resume();
245
246 #ifdef CONFIG_SMP
247         for_each_cpu(i)
248                 vcpu_prepare(i);
249
250 #endif
251         /* 
252          * Only resume xenbus /after/ we've prepared our VCPUs; otherwise
253          * the VCPU hotplug callback can race with our vcpu_prepare
254          */
255         DEVICE_RESUME(root_bus);
256
257 #ifdef SMP
258         thread_lock(curthread);
259         sched_unbind(curthread);
260         thread_unlock(curthread);
261         if (map)
262                 restart_cpus(map);
263 #endif
264 }
265
266 #endif