Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Handle extern requests for shutdown, reboot and sysrq |
| 3 | */ |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 4 | |
| 5 | #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt |
| 6 | |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 10 | #include <linux/reboot.h> |
| 11 | #include <linux/sysrq.h> |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 12 | #include <linux/stop_machine.h> |
| 13 | #include <linux/freezer.h> |
Rafael J. Wysocki | 19234c0 | 2011-04-20 00:36:11 +0200 | [diff] [blame] | 14 | #include <linux/syscore_ops.h> |
Paul Gortmaker | 63c9744 | 2011-07-10 13:22:07 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 16 | |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 17 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 18 | #include <xen/xenbus.h> |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 19 | #include <xen/grant_table.h> |
| 20 | #include <xen/events.h> |
| 21 | #include <xen/hvc-console.h> |
Julien Grall | a9fd60e | 2015-06-17 15:28:02 +0100 | [diff] [blame] | 22 | #include <xen/page.h> |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 23 | #include <xen/xen-ops.h> |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 24 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 25 | #include <asm/xen/hypercall.h> |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 26 | #include <asm/xen/hypervisor.h> |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 27 | |
| 28 | enum shutdown_state { |
| 29 | SHUTDOWN_INVALID = -1, |
| 30 | SHUTDOWN_POWEROFF = 0, |
| 31 | SHUTDOWN_SUSPEND = 2, |
| 32 | /* Code 3 is SHUTDOWN_CRASH, which we don't use because the domain can only |
| 33 | report a crash, not be instructed to crash! |
| 34 | HALT is the same as POWEROFF, as far as we're concerned. The tools use |
| 35 | the distinction when we return the reason code to them. */ |
| 36 | SHUTDOWN_HALT = 4, |
| 37 | }; |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 38 | |
| 39 | /* Ignore multiple shutdown requests. */ |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 40 | static enum shutdown_state shutting_down = SHUTDOWN_INVALID; |
| 41 | |
Ian Campbell | ceb1802 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 42 | struct suspend_info { |
| 43 | int cancelled; |
| 44 | }; |
| 45 | |
Stanislaw Gruszka | cd97988 | 2014-02-26 11:30:30 +0100 | [diff] [blame] | 46 | static RAW_NOTIFIER_HEAD(xen_resume_notifier); |
| 47 | |
| 48 | void xen_resume_notifier_register(struct notifier_block *nb) |
| 49 | { |
| 50 | raw_notifier_chain_register(&xen_resume_notifier, nb); |
| 51 | } |
| 52 | EXPORT_SYMBOL_GPL(xen_resume_notifier_register); |
| 53 | |
| 54 | void xen_resume_notifier_unregister(struct notifier_block *nb) |
| 55 | { |
| 56 | raw_notifier_chain_unregister(&xen_resume_notifier, nb); |
| 57 | } |
| 58 | EXPORT_SYMBOL_GPL(xen_resume_notifier_unregister); |
| 59 | |
Stefano Stabellini | 65e053a | 2013-06-27 14:28:38 +0100 | [diff] [blame] | 60 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 61 | static int xen_suspend(void *data) |
| 62 | { |
Ian Campbell | ceb1802 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 63 | struct suspend_info *si = data; |
Jeremy Fitzhardinge | 359cdd3 | 2008-05-26 23:31:28 +0100 | [diff] [blame] | 64 | int err; |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 65 | |
| 66 | BUG_ON(!irqs_disabled()); |
| 67 | |
Rafael J. Wysocki | 2e711c0 | 2011-04-26 19:15:07 +0200 | [diff] [blame] | 68 | err = syscore_suspend(); |
Rafael J. Wysocki | 770824b | 2009-02-22 18:38:50 +0100 | [diff] [blame] | 69 | if (err) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 70 | pr_err("%s: system core suspend failed: %d\n", __func__, err); |
Rafael J. Wysocki | 770824b | 2009-02-22 18:38:50 +0100 | [diff] [blame] | 71 | return err; |
| 72 | } |
Jeremy Fitzhardinge | 359cdd3 | 2008-05-26 23:31:28 +0100 | [diff] [blame] | 73 | |
David Vrabel | aa8532c | 2014-05-08 11:09:23 +0100 | [diff] [blame] | 74 | gnttab_suspend(); |
Dongli Zhang | 5e25f5d | 2017-11-01 09:46:33 +0800 | [diff] [blame] | 75 | xen_manage_runstate_time(-1); |
David Vrabel | aa8532c | 2014-05-08 11:09:23 +0100 | [diff] [blame] | 76 | xen_arch_pre_suspend(); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 77 | |
David Vrabel | aa8532c | 2014-05-08 11:09:23 +0100 | [diff] [blame] | 78 | si->cancelled = HYPERVISOR_suspend(xen_pv_domain() |
Julien Grall | 0df4f26 | 2015-08-07 17:34:37 +0100 | [diff] [blame] | 79 | ? virt_to_gfn(xen_start_info) |
David Vrabel | aa8532c | 2014-05-08 11:09:23 +0100 | [diff] [blame] | 80 | : 0); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 81 | |
David Vrabel | aa8532c | 2014-05-08 11:09:23 +0100 | [diff] [blame] | 82 | xen_arch_post_suspend(si->cancelled); |
Dongli Zhang | 5e25f5d | 2017-11-01 09:46:33 +0800 | [diff] [blame] | 83 | xen_manage_runstate_time(si->cancelled ? 1 : 0); |
David Vrabel | aa8532c | 2014-05-08 11:09:23 +0100 | [diff] [blame] | 84 | gnttab_resume(); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 85 | |
Ian Campbell | ceb1802 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 86 | if (!si->cancelled) { |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 87 | xen_irq_resume(); |
Isaku Yamahata | ad55db9 | 2008-07-08 15:06:32 -0700 | [diff] [blame] | 88 | xen_timer_resume(); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Rafael J. Wysocki | 19234c0 | 2011-04-20 00:36:11 +0200 | [diff] [blame] | 91 | syscore_resume(); |
Ian Campbell | 1e6fcf8 | 2009-03-25 17:46:42 +0000 | [diff] [blame] | 92 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static void do_suspend(void) |
| 97 | { |
| 98 | int err; |
Ian Campbell | ceb1802 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 99 | struct suspend_info si; |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 100 | |
| 101 | shutting_down = SHUTDOWN_SUSPEND; |
| 102 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 103 | err = freeze_processes(); |
| 104 | if (err) { |
Ross Lagerwall | 72978b2 | 2015-01-19 13:19:38 +0000 | [diff] [blame] | 105 | pr_err("%s: freeze processes failed %d\n", __func__, err); |
Tejun Heo | 3fc1f1e | 2010-05-06 18:49:20 +0200 | [diff] [blame] | 106 | goto out; |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 107 | } |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 108 | |
Ross Lagerwall | 72978b2 | 2015-01-19 13:19:38 +0000 | [diff] [blame] | 109 | err = freeze_kernel_threads(); |
| 110 | if (err) { |
| 111 | pr_err("%s: freeze kernel threads failed %d\n", __func__, err); |
| 112 | goto out_thaw; |
| 113 | } |
| 114 | |
Shriram Rajagopalan | b3e96c0 | 2011-02-22 14:59:06 -0800 | [diff] [blame] | 115 | err = dpm_suspend_start(PMSG_FREEZE); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 116 | if (err) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 117 | pr_err("%s: dpm_suspend_start %d\n", __func__, err); |
Ian Campbell | 65f6338 | 2009-12-01 11:47:14 +0000 | [diff] [blame] | 118 | goto out_thaw; |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Ian Campbell | c5cae66 | 2009-12-17 13:57:09 +0000 | [diff] [blame] | 121 | printk(KERN_DEBUG "suspending xenstore...\n"); |
| 122 | xs_suspend(); |
| 123 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 124 | err = dpm_suspend_end(PMSG_FREEZE); |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 125 | if (err) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 126 | pr_err("dpm_suspend_end failed: %d\n", err); |
Konrad Rzeszutek Wilk | 186bab1 | 2012-04-17 14:35:49 -0400 | [diff] [blame] | 127 | si.cancelled = 0; |
Ian Campbell | 65f6338 | 2009-12-01 11:47:14 +0000 | [diff] [blame] | 128 | goto out_resume; |
Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Boris Ostrovsky | 2b953a5 | 2015-04-28 18:46:20 -0400 | [diff] [blame] | 131 | xen_arch_suspend(); |
| 132 | |
Ian Campbell | ceb1802 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 133 | si.cancelled = 1; |
| 134 | |
Ian Campbell | b056b6a | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 135 | err = stop_machine(xen_suspend, &si, cpumask_of(0)); |
Jeremy Fitzhardinge | 922cc38a | 2009-11-24 09:58:49 -0800 | [diff] [blame] | 136 | |
David Vrabel | 1b64782 | 2014-07-02 17:25:23 +0100 | [diff] [blame] | 137 | /* Resume console as early as possible. */ |
| 138 | if (!si.cancelled) |
| 139 | xen_console_resume(); |
| 140 | |
Stanislaw Gruszka | cd97988 | 2014-02-26 11:30:30 +0100 | [diff] [blame] | 141 | raw_notifier_call_chain(&xen_resume_notifier, 0, NULL); |
| 142 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 143 | dpm_resume_start(si.cancelled ? PMSG_THAW : PMSG_RESTORE); |
Jeremy Fitzhardinge | 922cc38a | 2009-11-24 09:58:49 -0800 | [diff] [blame] | 144 | |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 145 | if (err) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 146 | pr_err("failed to start xen_suspend: %d\n", err); |
Ian Campbell | ceb1802 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 147 | si.cancelled = 1; |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Boris Ostrovsky | 2b953a5 | 2015-04-28 18:46:20 -0400 | [diff] [blame] | 150 | xen_arch_resume(); |
| 151 | |
Ian Campbell | c5cae66 | 2009-12-17 13:57:09 +0000 | [diff] [blame] | 152 | out_resume: |
Boris Ostrovsky | 2b953a5 | 2015-04-28 18:46:20 -0400 | [diff] [blame] | 153 | if (!si.cancelled) |
Ian Campbell | de5b31b | 2009-02-09 12:05:50 -0800 | [diff] [blame] | 154 | xs_resume(); |
Boris Ostrovsky | 2b953a5 | 2015-04-28 18:46:20 -0400 | [diff] [blame] | 155 | else |
Ian Campbell | de5b31b | 2009-02-09 12:05:50 -0800 | [diff] [blame] | 156 | xs_suspend_cancel(); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 157 | |
Shriram Rajagopalan | b3e96c0 | 2011-02-22 14:59:06 -0800 | [diff] [blame] | 158 | dpm_resume_end(si.cancelled ? PMSG_THAW : PMSG_RESTORE); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 159 | |
Ian Campbell | 65f6338 | 2009-12-01 11:47:14 +0000 | [diff] [blame] | 160 | out_thaw: |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 161 | thaw_processes(); |
Ian Campbell | 65f6338 | 2009-12-01 11:47:14 +0000 | [diff] [blame] | 162 | out: |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 163 | shutting_down = SHUTDOWN_INVALID; |
| 164 | } |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 165 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 166 | |
Ian Campbell | 5527172 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 167 | struct shutdown_handler { |
Juergen Gross | 44b3c7a | 2016-10-11 13:34:16 +0200 | [diff] [blame] | 168 | #define SHUTDOWN_CMD_SIZE 11 |
| 169 | const char command[SHUTDOWN_CMD_SIZE]; |
| 170 | bool flag; |
Ian Campbell | 5527172 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 171 | void (*cb)(void); |
| 172 | }; |
| 173 | |
Konrad Rzeszutek Wilk | eb47f71 | 2014-04-04 14:53:41 -0400 | [diff] [blame] | 174 | static int poweroff_nb(struct notifier_block *cb, unsigned long code, void *unused) |
| 175 | { |
| 176 | switch (code) { |
| 177 | case SYS_DOWN: |
| 178 | case SYS_HALT: |
| 179 | case SYS_POWER_OFF: |
| 180 | shutting_down = SHUTDOWN_POWEROFF; |
| 181 | default: |
| 182 | break; |
| 183 | } |
| 184 | return NOTIFY_DONE; |
| 185 | } |
Ian Campbell | 5527172 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 186 | static void do_poweroff(void) |
| 187 | { |
Konrad Rzeszutek Wilk | eb47f71 | 2014-04-04 14:53:41 -0400 | [diff] [blame] | 188 | switch (system_state) { |
| 189 | case SYSTEM_BOOTING: |
Thomas Gleixner | 69a78ff | 2017-05-16 20:42:47 +0200 | [diff] [blame] | 190 | case SYSTEM_SCHEDULING: |
Konrad Rzeszutek Wilk | eb47f71 | 2014-04-04 14:53:41 -0400 | [diff] [blame] | 191 | orderly_poweroff(true); |
| 192 | break; |
| 193 | case SYSTEM_RUNNING: |
| 194 | orderly_poweroff(false); |
| 195 | break; |
| 196 | default: |
| 197 | /* Don't do it when we are halting/rebooting. */ |
| 198 | pr_info("Ignoring Xen toolstack shutdown.\n"); |
| 199 | break; |
| 200 | } |
Ian Campbell | 5527172 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static void do_reboot(void) |
| 204 | { |
| 205 | shutting_down = SHUTDOWN_POWEROFF; /* ? */ |
| 206 | ctrl_alt_del(); |
| 207 | } |
| 208 | |
Juergen Gross | 44b3c7a | 2016-10-11 13:34:16 +0200 | [diff] [blame] | 209 | static struct shutdown_handler shutdown_handlers[] = { |
| 210 | { "poweroff", true, do_poweroff }, |
| 211 | { "halt", false, do_poweroff }, |
| 212 | { "reboot", true, do_reboot }, |
| 213 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
| 214 | { "suspend", true, do_suspend }, |
| 215 | #endif |
| 216 | }; |
| 217 | |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 218 | static void shutdown_handler(struct xenbus_watch *watch, |
Juergen Gross | 5584ea2 | 2017-02-09 14:39:57 +0100 | [diff] [blame] | 219 | const char *path, const char *token) |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 220 | { |
| 221 | char *str; |
| 222 | struct xenbus_transaction xbt; |
| 223 | int err; |
Juergen Gross | 44b3c7a | 2016-10-11 13:34:16 +0200 | [diff] [blame] | 224 | int idx; |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 225 | |
| 226 | if (shutting_down != SHUTDOWN_INVALID) |
| 227 | return; |
| 228 | |
| 229 | again: |
| 230 | err = xenbus_transaction_start(&xbt); |
| 231 | if (err) |
| 232 | return; |
| 233 | |
| 234 | str = (char *)xenbus_read(xbt, "control", "shutdown", NULL); |
| 235 | /* Ignore read errors and empty reads. */ |
| 236 | if (XENBUS_IS_ERR_READ(str)) { |
| 237 | xenbus_transaction_end(xbt, 1); |
| 238 | return; |
| 239 | } |
| 240 | |
Juergen Gross | 44b3c7a | 2016-10-11 13:34:16 +0200 | [diff] [blame] | 241 | for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) { |
| 242 | if (strcmp(str, shutdown_handlers[idx].command) == 0) |
Ian Campbell | 5527172 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | |
| 246 | /* Only acknowledge commands which we are prepared to handle. */ |
Juergen Gross | 44b3c7a | 2016-10-11 13:34:16 +0200 | [diff] [blame] | 247 | if (idx < ARRAY_SIZE(shutdown_handlers)) |
Ian Campbell | 5527172 | 2011-02-17 11:04:20 +0000 | [diff] [blame] | 248 | xenbus_write(xbt, "control", "shutdown", ""); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 249 | |
| 250 | err = xenbus_transaction_end(xbt, 0); |
| 251 | if (err == -EAGAIN) { |
| 252 | kfree(str); |
| 253 | goto again; |
| 254 | } |
| 255 | |
Juergen Gross | 44b3c7a | 2016-10-11 13:34:16 +0200 | [diff] [blame] | 256 | if (idx < ARRAY_SIZE(shutdown_handlers)) { |
| 257 | shutdown_handlers[idx].cb(); |
Jeremy Fitzhardinge | 0e91398 | 2008-05-26 23:31:27 +0100 | [diff] [blame] | 258 | } else { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 259 | pr_info("Ignoring shutdown request: %s\n", str); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 260 | shutting_down = SHUTDOWN_INVALID; |
| 261 | } |
| 262 | |
| 263 | kfree(str); |
| 264 | } |
| 265 | |
Randy Dunlap | f3bc318 | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 266 | #ifdef CONFIG_MAGIC_SYSRQ |
Juergen Gross | 5584ea2 | 2017-02-09 14:39:57 +0100 | [diff] [blame] | 267 | static void sysrq_handler(struct xenbus_watch *watch, const char *path, |
| 268 | const char *token) |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 269 | { |
| 270 | char sysrq_key = '\0'; |
| 271 | struct xenbus_transaction xbt; |
| 272 | int err; |
| 273 | |
| 274 | again: |
| 275 | err = xenbus_transaction_start(&xbt); |
| 276 | if (err) |
| 277 | return; |
Juergen Gross | 4e93b648 | 2017-05-30 20:52:26 +0200 | [diff] [blame] | 278 | err = xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key); |
| 279 | if (err < 0) { |
| 280 | /* |
| 281 | * The Xenstore watch fires directly after registering it and |
| 282 | * after a suspend/resume cycle. So ENOENT is no error but |
| 283 | * might happen in those cases. |
| 284 | */ |
| 285 | if (err != -ENOENT) |
| 286 | pr_err("Error %d reading sysrq code in control/sysrq\n", |
| 287 | err); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 288 | xenbus_transaction_end(xbt, 1); |
| 289 | return; |
| 290 | } |
| 291 | |
Zhouyang Jia | 84c029a | 2018-06-15 07:34:52 +0800 | [diff] [blame^] | 292 | if (sysrq_key != '\0') { |
| 293 | err = xenbus_printf(xbt, "control", "sysrq", "%c", '\0'); |
| 294 | if (err) { |
| 295 | pr_err("%s: Error %d writing sysrq in control/sysrq\n", |
| 296 | __func__, err); |
| 297 | xenbus_transaction_end(xbt, 1); |
| 298 | return; |
| 299 | } |
| 300 | } |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 301 | |
| 302 | err = xenbus_transaction_end(xbt, 0); |
| 303 | if (err == -EAGAIN) |
| 304 | goto again; |
| 305 | |
| 306 | if (sysrq_key != '\0') |
Dmitry Torokhov | f335397 | 2010-08-17 21:15:47 -0700 | [diff] [blame] | 307 | handle_sysrq(sysrq_key); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 310 | static struct xenbus_watch sysrq_watch = { |
| 311 | .node = "control/sysrq", |
| 312 | .callback = sysrq_handler |
| 313 | }; |
Randy Dunlap | f3bc318 | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 314 | #endif |
| 315 | |
| 316 | static struct xenbus_watch shutdown_watch = { |
| 317 | .node = "control/shutdown", |
| 318 | .callback = shutdown_handler |
| 319 | }; |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 320 | |
Konrad Rzeszutek Wilk | eb47f71 | 2014-04-04 14:53:41 -0400 | [diff] [blame] | 321 | static struct notifier_block xen_reboot_nb = { |
| 322 | .notifier_call = poweroff_nb, |
| 323 | }; |
| 324 | |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 325 | static int setup_shutdown_watcher(void) |
| 326 | { |
| 327 | int err; |
Juergen Gross | 44b3c7a | 2016-10-11 13:34:16 +0200 | [diff] [blame] | 328 | int idx; |
| 329 | #define FEATURE_PATH_SIZE (SHUTDOWN_CMD_SIZE + sizeof("feature-")) |
| 330 | char node[FEATURE_PATH_SIZE]; |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 331 | |
| 332 | err = register_xenbus_watch(&shutdown_watch); |
| 333 | if (err) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 334 | pr_err("Failed to set shutdown watcher\n"); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 335 | return err; |
| 336 | } |
| 337 | |
Konrad Rzeszutek Wilk | eb47f71 | 2014-04-04 14:53:41 -0400 | [diff] [blame] | 338 | |
Randy Dunlap | f3bc318 | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 339 | #ifdef CONFIG_MAGIC_SYSRQ |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 340 | err = register_xenbus_watch(&sysrq_watch); |
| 341 | if (err) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 342 | pr_err("Failed to set sysrq watcher\n"); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 343 | return err; |
| 344 | } |
Randy Dunlap | f3bc318 | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 345 | #endif |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 346 | |
Juergen Gross | 44b3c7a | 2016-10-11 13:34:16 +0200 | [diff] [blame] | 347 | for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) { |
| 348 | if (!shutdown_handlers[idx].flag) |
| 349 | continue; |
| 350 | snprintf(node, FEATURE_PATH_SIZE, "feature-%s", |
| 351 | shutdown_handlers[idx].command); |
Zhouyang Jia | 84c029a | 2018-06-15 07:34:52 +0800 | [diff] [blame^] | 352 | err = xenbus_printf(XBT_NIL, "control", node, "%u", 1); |
| 353 | if (err) { |
| 354 | pr_err("%s: Error %d writing %s\n", __func__, |
| 355 | err, node); |
| 356 | return err; |
| 357 | } |
Juergen Gross | 44b3c7a | 2016-10-11 13:34:16 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | static int shutdown_event(struct notifier_block *notifier, |
| 364 | unsigned long event, |
| 365 | void *data) |
| 366 | { |
| 367 | setup_shutdown_watcher(); |
| 368 | return NOTIFY_DONE; |
| 369 | } |
| 370 | |
Stefano Stabellini | 016b6f5 | 2010-05-14 12:45:07 +0100 | [diff] [blame] | 371 | int xen_setup_shutdown_event(void) |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 372 | { |
| 373 | static struct notifier_block xenstore_notifier = { |
| 374 | .notifier_call = shutdown_event |
| 375 | }; |
Stefano Stabellini | 702d4eb9 | 2010-12-02 17:54:50 +0000 | [diff] [blame] | 376 | |
| 377 | if (!xen_domain()) |
| 378 | return -ENODEV; |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 379 | register_xenstore_notifier(&xenstore_notifier); |
Konrad Rzeszutek Wilk | eb47f71 | 2014-04-04 14:53:41 -0400 | [diff] [blame] | 380 | register_reboot_notifier(&xen_reboot_nb); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 381 | |
| 382 | return 0; |
| 383 | } |
Stefano Stabellini | 183d03c | 2010-05-17 17:08:21 +0100 | [diff] [blame] | 384 | EXPORT_SYMBOL_GPL(xen_setup_shutdown_event); |
Jeremy Fitzhardinge | 3e2b8fb | 2007-07-17 18:37:07 -0700 | [diff] [blame] | 385 | |
Stefano Stabellini | 702d4eb9 | 2010-12-02 17:54:50 +0000 | [diff] [blame] | 386 | subsys_initcall(xen_setup_shutdown_event); |