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