2 * Handle extern requests for shutdown, reboot and sysrq
4 #include <linux/kernel.h>
6 #include <linux/slab.h>
7 #include <linux/reboot.h>
8 #include <linux/sysrq.h>
9 #include <linux/stop_machine.h>
10 #include <linux/freezer.h>
13 #include <xen/xenbus.h>
14 #include <xen/grant_table.h>
15 #include <xen/events.h>
16 #include <xen/hvc-console.h>
17 #include <xen/xen-ops.h>
19 #include <asm/xen/hypercall.h>
20 #include <asm/xen/page.h>
21 #include <asm/xen/hypervisor.h>
24 SHUTDOWN_INVALID = -1,
25 SHUTDOWN_POWEROFF = 0,
27 /* Code 3 is SHUTDOWN_CRASH, which we don't use because the domain can only
28 report a crash, not be instructed to crash!
29 HALT is the same as POWEROFF, as far as we're concerned. The tools use
30 the distinction when we return the reason code to them. */
34 /* Ignore multiple shutdown requests. */
35 static enum shutdown_state shutting_down = SHUTDOWN_INVALID;
39 unsigned long arg; /* extra hypercall argument */
42 #ifdef CONFIG_PM_SLEEP
43 static int xen_hvm_suspend(void *data)
45 struct suspend_info *si = data;
48 BUG_ON(!irqs_disabled());
50 err = sysdev_suspend(PMSG_SUSPEND);
52 printk(KERN_ERR "xen_hvm_suspend: sysdev_suspend failed: %d\n",
58 * This hypercall returns 1 if suspend was cancelled
59 * or the domain was merely checkpointed, and 0 if it
60 * is resuming in a new domain.
62 si->cancelled = HYPERVISOR_suspend(si->arg);
64 xen_hvm_post_suspend(si->cancelled);
78 static int xen_suspend(void *data)
80 struct suspend_info *si = data;
83 BUG_ON(!irqs_disabled());
85 err = sysdev_suspend(PMSG_SUSPEND);
87 printk(KERN_ERR "xen_suspend: sysdev_suspend failed: %d\n",
97 * This hypercall returns 1 if suspend was cancelled
98 * or the domain was merely checkpointed, and 0 if it
99 * is resuming in a new domain.
101 si->cancelled = HYPERVISOR_suspend(si->arg);
103 xen_post_suspend(si->cancelled);
107 if (!si->cancelled) {
109 xen_console_resume();
118 static void do_suspend(void)
121 struct suspend_info si;
123 shutting_down = SHUTDOWN_SUSPEND;
125 #ifdef CONFIG_PREEMPT
126 /* If the kernel is preemptible, we need to freeze all the processes
127 to prevent them from being in the middle of a pagetable update
129 err = freeze_processes();
131 printk(KERN_ERR "xen suspend: freeze failed %d\n", err);
136 err = dpm_suspend_start(PMSG_SUSPEND);
138 printk(KERN_ERR "xen suspend: dpm_suspend_start %d\n", err);
142 printk(KERN_DEBUG "suspending xenstore...\n");
145 err = dpm_suspend_noirq(PMSG_SUSPEND);
147 printk(KERN_ERR "dpm_suspend_noirq failed: %d\n", err);
153 if (xen_hvm_domain())
156 si.arg = virt_to_mfn(xen_start_info);
158 if (xen_hvm_domain())
159 err = stop_machine(xen_hvm_suspend, &si, cpumask_of(0));
161 err = stop_machine(xen_suspend, &si, cpumask_of(0));
163 dpm_resume_noirq(PMSG_RESUME);
166 printk(KERN_ERR "failed to start xen_suspend: %d\n", err);
177 dpm_resume_end(PMSG_RESUME);
179 /* Make sure timer events get retriggered on all CPUs */
183 #ifdef CONFIG_PREEMPT
187 shutting_down = SHUTDOWN_INVALID;
189 #endif /* CONFIG_PM_SLEEP */
191 struct shutdown_handler {
196 static void do_poweroff(void)
198 shutting_down = SHUTDOWN_POWEROFF;
199 orderly_poweroff(false);
202 static void do_reboot(void)
204 shutting_down = SHUTDOWN_POWEROFF; /* ? */
208 static void shutdown_handler(struct xenbus_watch *watch,
209 const char **vec, unsigned int len)
212 struct xenbus_transaction xbt;
214 static struct shutdown_handler handlers[] = {
215 { "poweroff", do_poweroff },
216 { "halt", do_poweroff },
217 { "reboot", do_reboot },
218 #ifdef CONFIG_PM_SLEEP
219 { "suspend", do_suspend },
223 static struct shutdown_handler *handler;
225 if (shutting_down != SHUTDOWN_INVALID)
229 err = xenbus_transaction_start(&xbt);
233 str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
234 /* Ignore read errors and empty reads. */
235 if (XENBUS_IS_ERR_READ(str)) {
236 xenbus_transaction_end(xbt, 1);
240 for (handler = &handlers[0]; handler->command; handler++) {
241 if (strcmp(str, handler->command) == 0)
245 /* Only acknowledge commands which we are prepared to handle. */
247 xenbus_write(xbt, "control", "shutdown", "");
249 err = xenbus_transaction_end(xbt, 0);
250 if (err == -EAGAIN) {
258 printk(KERN_INFO "Ignoring shutdown request: %s\n", str);
259 shutting_down = SHUTDOWN_INVALID;
265 #ifdef CONFIG_MAGIC_SYSRQ
266 static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
269 char sysrq_key = '\0';
270 struct xenbus_transaction xbt;
274 err = xenbus_transaction_start(&xbt);
277 if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
278 printk(KERN_ERR "Unable to read sysrq code in "
280 xenbus_transaction_end(xbt, 1);
284 if (sysrq_key != '\0')
285 xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
287 err = xenbus_transaction_end(xbt, 0);
291 if (sysrq_key != '\0')
292 handle_sysrq(sysrq_key);
295 static struct xenbus_watch sysrq_watch = {
296 .node = "control/sysrq",
297 .callback = sysrq_handler
301 static struct xenbus_watch shutdown_watch = {
302 .node = "control/shutdown",
303 .callback = shutdown_handler
306 static int setup_shutdown_watcher(void)
310 err = register_xenbus_watch(&shutdown_watch);
312 printk(KERN_ERR "Failed to set shutdown watcher\n");
316 #ifdef CONFIG_MAGIC_SYSRQ
317 err = register_xenbus_watch(&sysrq_watch);
319 printk(KERN_ERR "Failed to set sysrq watcher\n");
327 static int shutdown_event(struct notifier_block *notifier,
331 setup_shutdown_watcher();
335 int xen_setup_shutdown_event(void)
337 static struct notifier_block xenstore_notifier = {
338 .notifier_call = shutdown_event
343 register_xenstore_notifier(&xenstore_notifier);
347 EXPORT_SYMBOL_GPL(xen_setup_shutdown_event);
349 subsys_initcall(xen_setup_shutdown_event);