Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Handle extern requests for shutdown, reboot and sysrq |
| 3 | */ |
| 4 | #include <linux/kernel.h> |
| 5 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 6 | #include <linux/slab.h> |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 7 | #include <linux/reboot.h> |
| 8 | #include <linux/sysrq.h> |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 9 | #include <linux/stop_machine.h> |
| 10 | #include <linux/freezer.h> |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 11 | |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 12 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 13 | #include <xen/xenbus.h> |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 14 | #include <xen/grant_table.h> |
| 15 | #include <xen/events.h> |
| 16 | #include <xen/hvc-console.h> |
| 17 | #include <xen/xen-ops.h> |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 18 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 19 | #include <asm/xen/hypercall.h> |
| 20 | #include <asm/xen/page.h> |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 21 | #include <asm/xen/hypervisor.h> |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 22 | |
| 23 | enum shutdown_state { |
| 24 | SHUTDOWN_INVALID = -1, |
| 25 | SHUTDOWN_POWEROFF = 0, |
| 26 | SHUTDOWN_SUSPEND = 2, |
| 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. */ |
| 31 | SHUTDOWN_HALT = 4, |
| 32 | }; |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 33 | |
| 34 | /* Ignore multiple shutdown requests. */ |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 35 | static enum shutdown_state shutting_down = SHUTDOWN_INVALID; |
| 36 | |
Jeremy Fitzhardinge | c782772 | 2008-05-29 09:02:19 +0100 | [diff] [blame] | 37 | #ifdef CONFIG_PM_SLEEP |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 38 | static int xen_hvm_suspend(void *data) |
| 39 | { |
| 40 | struct sched_shutdown r = { .reason = SHUTDOWN_suspend }; |
| 41 | int *cancelled = data; |
| 42 | |
| 43 | BUG_ON(!irqs_disabled()); |
| 44 | |
| 45 | *cancelled = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r); |
| 46 | |
| 47 | xen_hvm_post_suspend(*cancelled); |
| 48 | gnttab_resume(); |
| 49 | |
| 50 | if (!*cancelled) { |
| 51 | xen_irq_resume(); |
Stefano Stabellini | 6411fe69 | 2010-12-01 14:51:44 +0000 | [diff] [blame] | 52 | xen_console_resume(); |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 53 | xen_timer_resume(); |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 59 | static int xen_suspend(void *data) |
| 60 | { |
Jeremy Fitzhardinge | 359cdd3 | 2008-05-26 23:31:28 +0100 | [diff] [blame] | 61 | int err; |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 62 | int *cancelled = data; |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 63 | |
| 64 | BUG_ON(!irqs_disabled()); |
| 65 | |
Rafael J. Wysocki | 770824b | 2009-02-22 18:38:50 +0100 | [diff] [blame] | 66 | err = sysdev_suspend(PMSG_SUSPEND); |
| 67 | if (err) { |
| 68 | printk(KERN_ERR "xen_suspend: sysdev_suspend failed: %d\n", |
| 69 | err); |
Rafael J. Wysocki | 770824b | 2009-02-22 18:38:50 +0100 | [diff] [blame] | 70 | return err; |
| 71 | } |
Jeremy Fitzhardinge | 359cdd3 | 2008-05-26 23:31:28 +0100 | [diff] [blame] | 72 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 73 | xen_mm_pin_all(); |
| 74 | gnttab_suspend(); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 75 | xen_pre_suspend(); |
| 76 | |
| 77 | /* |
| 78 | * This hypercall returns 1 if suspend was cancelled |
| 79 | * or the domain was merely checkpointed, and 0 if it |
| 80 | * is resuming in a new domain. |
| 81 | */ |
| 82 | *cancelled = HYPERVISOR_suspend(virt_to_mfn(xen_start_info)); |
| 83 | |
| 84 | xen_post_suspend(*cancelled); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 85 | gnttab_resume(); |
| 86 | xen_mm_unpin_all(); |
| 87 | |
| 88 | if (!*cancelled) { |
| 89 | xen_irq_resume(); |
| 90 | xen_console_resume(); |
Isaku Yamahata | ad55db9 | 2008-07-08 15:06:32 -0700 | [diff] [blame] | 91 | xen_timer_resume(); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Ian Campbell | 1e6fcf8 | 2009-03-25 17:46:42 +0000 | [diff] [blame] | 94 | sysdev_resume(); |
Ian Campbell | 1e6fcf8 | 2009-03-25 17:46:42 +0000 | [diff] [blame] | 95 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static void do_suspend(void) |
| 100 | { |
| 101 | int err; |
| 102 | int cancelled = 1; |
| 103 | |
| 104 | shutting_down = SHUTDOWN_SUSPEND; |
| 105 | |
| 106 | #ifdef CONFIG_PREEMPT |
| 107 | /* If the kernel is preemptible, we need to freeze all the processes |
| 108 | to prevent them from being in the middle of a pagetable update |
| 109 | during suspend. */ |
| 110 | err = freeze_processes(); |
| 111 | if (err) { |
| 112 | printk(KERN_ERR "xen suspend: freeze failed %d\n", err); |
Tejun Heo | 3fc1f1e | 2010-05-06 18:49:20 +0200 | [diff] [blame] | 113 | goto out; |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 114 | } |
| 115 | #endif |
| 116 | |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 117 | err = dpm_suspend_start(PMSG_SUSPEND); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 118 | if (err) { |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 119 | printk(KERN_ERR "xen suspend: dpm_suspend_start %d\n", err); |
Ian Campbell | 65f6338 | 2009-12-01 11:47:14 +0000 | [diff] [blame] | 120 | goto out_thaw; |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Ian Campbell | c5cae66 | 2009-12-17 13:57:09 +0000 | [diff] [blame] | 123 | printk(KERN_DEBUG "suspending xenstore...\n"); |
| 124 | xs_suspend(); |
| 125 | |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 126 | err = dpm_suspend_noirq(PMSG_SUSPEND); |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 127 | if (err) { |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 128 | printk(KERN_ERR "dpm_suspend_noirq failed: %d\n", err); |
Ian Campbell | 65f6338 | 2009-12-01 11:47:14 +0000 | [diff] [blame] | 129 | goto out_resume; |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 132 | if (xen_hvm_domain()) |
| 133 | err = stop_machine(xen_hvm_suspend, &cancelled, cpumask_of(0)); |
| 134 | else |
| 135 | err = stop_machine(xen_suspend, &cancelled, cpumask_of(0)); |
Jeremy Fitzhardinge | 922cc38a | 2009-11-24 09:58:49 -0800 | [diff] [blame] | 136 | |
| 137 | dpm_resume_noirq(PMSG_RESUME); |
| 138 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 139 | if (err) { |
| 140 | printk(KERN_ERR "failed to start xen_suspend: %d\n", err); |
Ian Campbell | 65f6338 | 2009-12-01 11:47:14 +0000 | [diff] [blame] | 141 | cancelled = 1; |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Ian Campbell | c5cae66 | 2009-12-17 13:57:09 +0000 | [diff] [blame] | 144 | out_resume: |
Isaku Yamahata | ad55db9 | 2008-07-08 15:06:32 -0700 | [diff] [blame] | 145 | if (!cancelled) { |
| 146 | xen_arch_resume(); |
Ian Campbell | de5b31b | 2009-02-09 12:05:50 -0800 | [diff] [blame] | 147 | xs_resume(); |
Isaku Yamahata | ad55db9 | 2008-07-08 15:06:32 -0700 | [diff] [blame] | 148 | } else |
Ian Campbell | de5b31b | 2009-02-09 12:05:50 -0800 | [diff] [blame] | 149 | xs_suspend_cancel(); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 150 | |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 151 | dpm_resume_end(PMSG_RESUME); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 152 | |
Jeremy Fitzhardinge | 359cdd3 | 2008-05-26 23:31:28 +0100 | [diff] [blame] | 153 | /* Make sure timer events get retriggered on all CPUs */ |
| 154 | clock_was_set(); |
Ian Campbell | 65f6338 | 2009-12-01 11:47:14 +0000 | [diff] [blame] | 155 | |
| 156 | out_thaw: |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 157 | #ifdef CONFIG_PREEMPT |
| 158 | thaw_processes(); |
Ian Campbell | 65f6338 | 2009-12-01 11:47:14 +0000 | [diff] [blame] | 159 | out: |
Tejun Heo | 3fc1f1e | 2010-05-06 18:49:20 +0200 | [diff] [blame] | 160 | #endif |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 161 | shutting_down = SHUTDOWN_INVALID; |
| 162 | } |
Jeremy Fitzhardinge | c782772 | 2008-05-29 09:02:19 +0100 | [diff] [blame] | 163 | #endif /* CONFIG_PM_SLEEP */ |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 164 | |
| 165 | static void shutdown_handler(struct xenbus_watch *watch, |
| 166 | const char **vec, unsigned int len) |
| 167 | { |
| 168 | char *str; |
| 169 | struct xenbus_transaction xbt; |
| 170 | int err; |
| 171 | |
| 172 | if (shutting_down != SHUTDOWN_INVALID) |
| 173 | return; |
| 174 | |
| 175 | again: |
| 176 | err = xenbus_transaction_start(&xbt); |
| 177 | if (err) |
| 178 | return; |
| 179 | |
| 180 | str = (char *)xenbus_read(xbt, "control", "shutdown", NULL); |
| 181 | /* Ignore read errors and empty reads. */ |
| 182 | if (XENBUS_IS_ERR_READ(str)) { |
| 183 | xenbus_transaction_end(xbt, 1); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | xenbus_write(xbt, "control", "shutdown", ""); |
| 188 | |
| 189 | err = xenbus_transaction_end(xbt, 0); |
| 190 | if (err == -EAGAIN) { |
| 191 | kfree(str); |
| 192 | goto again; |
| 193 | } |
| 194 | |
| 195 | if (strcmp(str, "poweroff") == 0 || |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 196 | strcmp(str, "halt") == 0) { |
| 197 | shutting_down = SHUTDOWN_POWEROFF; |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 198 | orderly_poweroff(false); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 199 | } else if (strcmp(str, "reboot") == 0) { |
| 200 | shutting_down = SHUTDOWN_POWEROFF; /* ? */ |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 201 | ctrl_alt_del(); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 202 | #ifdef CONFIG_PM_SLEEP |
| 203 | } else if (strcmp(str, "suspend") == 0) { |
| 204 | do_suspend(); |
| 205 | #endif |
| 206 | } else { |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 207 | printk(KERN_INFO "Ignoring shutdown request: %s\n", str); |
| 208 | shutting_down = SHUTDOWN_INVALID; |
| 209 | } |
| 210 | |
| 211 | kfree(str); |
| 212 | } |
| 213 | |
Randy Dunlap | f3bc318 | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 214 | #ifdef CONFIG_MAGIC_SYSRQ |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 215 | static void sysrq_handler(struct xenbus_watch *watch, const char **vec, |
| 216 | unsigned int len) |
| 217 | { |
| 218 | char sysrq_key = '\0'; |
| 219 | struct xenbus_transaction xbt; |
| 220 | int err; |
| 221 | |
| 222 | again: |
| 223 | err = xenbus_transaction_start(&xbt); |
| 224 | if (err) |
| 225 | return; |
| 226 | if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) { |
| 227 | printk(KERN_ERR "Unable to read sysrq code in " |
| 228 | "control/sysrq\n"); |
| 229 | xenbus_transaction_end(xbt, 1); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | if (sysrq_key != '\0') |
| 234 | xenbus_printf(xbt, "control", "sysrq", "%c", '\0'); |
| 235 | |
| 236 | err = xenbus_transaction_end(xbt, 0); |
| 237 | if (err == -EAGAIN) |
| 238 | goto again; |
| 239 | |
| 240 | if (sysrq_key != '\0') |
Dmitry Torokhov | f335397 | 2010-08-17 21:15:47 -0700 | [diff] [blame] | 241 | handle_sysrq(sysrq_key); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 244 | static struct xenbus_watch sysrq_watch = { |
| 245 | .node = "control/sysrq", |
| 246 | .callback = sysrq_handler |
| 247 | }; |
Randy Dunlap | f3bc318 | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 248 | #endif |
| 249 | |
| 250 | static struct xenbus_watch shutdown_watch = { |
| 251 | .node = "control/shutdown", |
| 252 | .callback = shutdown_handler |
| 253 | }; |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 254 | |
| 255 | static int setup_shutdown_watcher(void) |
| 256 | { |
| 257 | int err; |
| 258 | |
| 259 | err = register_xenbus_watch(&shutdown_watch); |
| 260 | if (err) { |
| 261 | printk(KERN_ERR "Failed to set shutdown watcher\n"); |
| 262 | return err; |
| 263 | } |
| 264 | |
Randy Dunlap | f3bc318 | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 265 | #ifdef CONFIG_MAGIC_SYSRQ |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 266 | err = register_xenbus_watch(&sysrq_watch); |
| 267 | if (err) { |
| 268 | printk(KERN_ERR "Failed to set sysrq watcher\n"); |
| 269 | return err; |
| 270 | } |
Randy Dunlap | f3bc318 | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 271 | #endif |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int shutdown_event(struct notifier_block *notifier, |
| 277 | unsigned long event, |
| 278 | void *data) |
| 279 | { |
| 280 | setup_shutdown_watcher(); |
| 281 | return NOTIFY_DONE; |
| 282 | } |
| 283 | |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 284 | static int __init __setup_shutdown_event(void) |
| 285 | { |
| 286 | /* Delay initialization in the PV on HVM case */ |
| 287 | if (xen_hvm_domain()) |
| 288 | return 0; |
| 289 | |
| 290 | if (!xen_pv_domain()) |
| 291 | return -ENODEV; |
| 292 | |
| 293 | return xen_setup_shutdown_event(); |
| 294 | } |
| 295 | |
| 296 | int xen_setup_shutdown_event(void) |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 297 | { |
| 298 | static struct notifier_block xenstore_notifier = { |
| 299 | .notifier_call = shutdown_event |
| 300 | }; |
| 301 | register_xenstore_notifier(&xenstore_notifier); |
| 302 | |
| 303 | return 0; |
| 304 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 305 | EXPORT_SYMBOL_GPL(xen_setup_shutdown_event); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 306 | |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 307 | subsys_initcall(__setup_shutdown_event); |