0001
0002
0003
0004
0005
0006 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
0007
0008 #include <linux/kernel.h>
0009 #include <linux/err.h>
0010 #include <linux/slab.h>
0011 #include <linux/reboot.h>
0012 #include <linux/sysrq.h>
0013 #include <linux/stop_machine.h>
0014 #include <linux/freezer.h>
0015 #include <linux/syscore_ops.h>
0016 #include <linux/export.h>
0017
0018 #include <xen/xen.h>
0019 #include <xen/xenbus.h>
0020 #include <xen/grant_table.h>
0021 #include <xen/events.h>
0022 #include <xen/hvc-console.h>
0023 #include <xen/page.h>
0024 #include <xen/xen-ops.h>
0025
0026 #include <asm/xen/hypercall.h>
0027 #include <asm/xen/hypervisor.h>
0028
0029 enum shutdown_state {
0030 SHUTDOWN_INVALID = -1,
0031 SHUTDOWN_POWEROFF = 0,
0032 SHUTDOWN_SUSPEND = 2,
0033
0034
0035
0036
0037 SHUTDOWN_HALT = 4,
0038 };
0039
0040
0041 static enum shutdown_state shutting_down = SHUTDOWN_INVALID;
0042
0043 struct suspend_info {
0044 int cancelled;
0045 };
0046
0047 static RAW_NOTIFIER_HEAD(xen_resume_notifier);
0048
0049 void xen_resume_notifier_register(struct notifier_block *nb)
0050 {
0051 raw_notifier_chain_register(&xen_resume_notifier, nb);
0052 }
0053 EXPORT_SYMBOL_GPL(xen_resume_notifier_register);
0054
0055 void xen_resume_notifier_unregister(struct notifier_block *nb)
0056 {
0057 raw_notifier_chain_unregister(&xen_resume_notifier, nb);
0058 }
0059 EXPORT_SYMBOL_GPL(xen_resume_notifier_unregister);
0060
0061 #ifdef CONFIG_HIBERNATE_CALLBACKS
0062 static int xen_suspend(void *data)
0063 {
0064 struct suspend_info *si = data;
0065 int err;
0066
0067 BUG_ON(!irqs_disabled());
0068
0069 err = syscore_suspend();
0070 if (err) {
0071 pr_err("%s: system core suspend failed: %d\n", __func__, err);
0072 return err;
0073 }
0074
0075 gnttab_suspend();
0076 xen_manage_runstate_time(-1);
0077 xen_arch_pre_suspend();
0078
0079 si->cancelled = HYPERVISOR_suspend(xen_pv_domain()
0080 ? virt_to_gfn(xen_start_info)
0081 : 0);
0082
0083 xen_arch_post_suspend(si->cancelled);
0084 xen_manage_runstate_time(si->cancelled ? 1 : 0);
0085 gnttab_resume();
0086
0087 if (!si->cancelled) {
0088 xen_irq_resume();
0089 xen_timer_resume();
0090 }
0091
0092 syscore_resume();
0093
0094 return 0;
0095 }
0096
0097 static void do_suspend(void)
0098 {
0099 int err;
0100 struct suspend_info si;
0101
0102 shutting_down = SHUTDOWN_SUSPEND;
0103
0104 err = freeze_processes();
0105 if (err) {
0106 pr_err("%s: freeze processes failed %d\n", __func__, err);
0107 goto out;
0108 }
0109
0110 err = freeze_kernel_threads();
0111 if (err) {
0112 pr_err("%s: freeze kernel threads failed %d\n", __func__, err);
0113 goto out_thaw;
0114 }
0115
0116 err = dpm_suspend_start(PMSG_FREEZE);
0117 if (err) {
0118 pr_err("%s: dpm_suspend_start %d\n", __func__, err);
0119 goto out_thaw;
0120 }
0121
0122 printk(KERN_DEBUG "suspending xenstore...\n");
0123 xs_suspend();
0124
0125 err = dpm_suspend_end(PMSG_FREEZE);
0126 if (err) {
0127 pr_err("dpm_suspend_end failed: %d\n", err);
0128 si.cancelled = 0;
0129 goto out_resume;
0130 }
0131
0132 xen_arch_suspend();
0133
0134 si.cancelled = 1;
0135
0136 err = stop_machine(xen_suspend, &si, cpumask_of(0));
0137
0138
0139 if (!si.cancelled)
0140 xen_console_resume();
0141
0142 raw_notifier_call_chain(&xen_resume_notifier, 0, NULL);
0143
0144 xen_arch_resume();
0145
0146 dpm_resume_start(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
0147
0148 if (err) {
0149 pr_err("failed to start xen_suspend: %d\n", err);
0150 si.cancelled = 1;
0151 }
0152
0153 out_resume:
0154 if (!si.cancelled)
0155 xs_resume();
0156 else
0157 xs_suspend_cancel();
0158
0159 dpm_resume_end(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
0160
0161 out_thaw:
0162 thaw_processes();
0163 out:
0164 shutting_down = SHUTDOWN_INVALID;
0165 }
0166 #endif
0167
0168 struct shutdown_handler {
0169 #define SHUTDOWN_CMD_SIZE 11
0170 const char command[SHUTDOWN_CMD_SIZE];
0171 bool flag;
0172 void (*cb)(void);
0173 };
0174
0175 static int poweroff_nb(struct notifier_block *cb, unsigned long code, void *unused)
0176 {
0177 switch (code) {
0178 case SYS_DOWN:
0179 case SYS_HALT:
0180 case SYS_POWER_OFF:
0181 shutting_down = SHUTDOWN_POWEROFF;
0182 break;
0183 default:
0184 break;
0185 }
0186 return NOTIFY_DONE;
0187 }
0188 static void do_poweroff(void)
0189 {
0190 switch (system_state) {
0191 case SYSTEM_BOOTING:
0192 case SYSTEM_SCHEDULING:
0193 orderly_poweroff(true);
0194 break;
0195 case SYSTEM_RUNNING:
0196 orderly_poweroff(false);
0197 break;
0198 default:
0199
0200 pr_info("Ignoring Xen toolstack shutdown.\n");
0201 break;
0202 }
0203 }
0204
0205 static void do_reboot(void)
0206 {
0207 shutting_down = SHUTDOWN_POWEROFF;
0208 orderly_reboot();
0209 }
0210
0211 static struct shutdown_handler shutdown_handlers[] = {
0212 { "poweroff", true, do_poweroff },
0213 { "halt", false, do_poweroff },
0214 { "reboot", true, do_reboot },
0215 #ifdef CONFIG_HIBERNATE_CALLBACKS
0216 { "suspend", true, do_suspend },
0217 #endif
0218 };
0219
0220 static void shutdown_handler(struct xenbus_watch *watch,
0221 const char *path, const char *token)
0222 {
0223 char *str;
0224 struct xenbus_transaction xbt;
0225 int err;
0226 int idx;
0227
0228 if (shutting_down != SHUTDOWN_INVALID)
0229 return;
0230
0231 again:
0232 err = xenbus_transaction_start(&xbt);
0233 if (err)
0234 return;
0235
0236 str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
0237
0238 if (XENBUS_IS_ERR_READ(str)) {
0239 xenbus_transaction_end(xbt, 1);
0240 return;
0241 }
0242
0243 for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
0244 if (strcmp(str, shutdown_handlers[idx].command) == 0)
0245 break;
0246 }
0247
0248
0249 if (idx < ARRAY_SIZE(shutdown_handlers))
0250 xenbus_write(xbt, "control", "shutdown", "");
0251
0252 err = xenbus_transaction_end(xbt, 0);
0253 if (err == -EAGAIN) {
0254 kfree(str);
0255 goto again;
0256 }
0257
0258 if (idx < ARRAY_SIZE(shutdown_handlers)) {
0259 shutdown_handlers[idx].cb();
0260 } else {
0261 pr_info("Ignoring shutdown request: %s\n", str);
0262 shutting_down = SHUTDOWN_INVALID;
0263 }
0264
0265 kfree(str);
0266 }
0267
0268 #ifdef CONFIG_MAGIC_SYSRQ
0269 static void sysrq_handler(struct xenbus_watch *watch, const char *path,
0270 const char *token)
0271 {
0272 char sysrq_key = '\0';
0273 struct xenbus_transaction xbt;
0274 int err;
0275
0276 again:
0277 err = xenbus_transaction_start(&xbt);
0278 if (err)
0279 return;
0280 err = xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key);
0281 if (err < 0) {
0282
0283
0284
0285
0286
0287
0288
0289 if (err != -ENOENT && err != -ERANGE)
0290 pr_err("Error %d reading sysrq code in control/sysrq\n",
0291 err);
0292 xenbus_transaction_end(xbt, 1);
0293 return;
0294 }
0295
0296 if (sysrq_key != '\0') {
0297 err = xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
0298 if (err) {
0299 pr_err("%s: Error %d writing sysrq in control/sysrq\n",
0300 __func__, err);
0301 xenbus_transaction_end(xbt, 1);
0302 return;
0303 }
0304 }
0305
0306 err = xenbus_transaction_end(xbt, 0);
0307 if (err == -EAGAIN)
0308 goto again;
0309
0310 if (sysrq_key != '\0')
0311 handle_sysrq(sysrq_key);
0312 }
0313
0314 static struct xenbus_watch sysrq_watch = {
0315 .node = "control/sysrq",
0316 .callback = sysrq_handler
0317 };
0318 #endif
0319
0320 static struct xenbus_watch shutdown_watch = {
0321 .node = "control/shutdown",
0322 .callback = shutdown_handler
0323 };
0324
0325 static struct notifier_block xen_reboot_nb = {
0326 .notifier_call = poweroff_nb,
0327 };
0328
0329 static int setup_shutdown_watcher(void)
0330 {
0331 int err;
0332 int idx;
0333 #define FEATURE_PATH_SIZE (SHUTDOWN_CMD_SIZE + sizeof("feature-"))
0334 char node[FEATURE_PATH_SIZE];
0335
0336 err = register_xenbus_watch(&shutdown_watch);
0337 if (err) {
0338 pr_err("Failed to set shutdown watcher\n");
0339 return err;
0340 }
0341
0342
0343 #ifdef CONFIG_MAGIC_SYSRQ
0344 err = register_xenbus_watch(&sysrq_watch);
0345 if (err) {
0346 pr_err("Failed to set sysrq watcher\n");
0347 return err;
0348 }
0349 #endif
0350
0351 for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
0352 if (!shutdown_handlers[idx].flag)
0353 continue;
0354 snprintf(node, FEATURE_PATH_SIZE, "feature-%s",
0355 shutdown_handlers[idx].command);
0356 err = xenbus_printf(XBT_NIL, "control", node, "%u", 1);
0357 if (err) {
0358 pr_err("%s: Error %d writing %s\n", __func__,
0359 err, node);
0360 return err;
0361 }
0362 }
0363
0364 return 0;
0365 }
0366
0367 static int shutdown_event(struct notifier_block *notifier,
0368 unsigned long event,
0369 void *data)
0370 {
0371 setup_shutdown_watcher();
0372 return NOTIFY_DONE;
0373 }
0374
0375 int xen_setup_shutdown_event(void)
0376 {
0377 static struct notifier_block xenstore_notifier = {
0378 .notifier_call = shutdown_event
0379 };
0380
0381 if (!xen_domain())
0382 return -ENODEV;
0383 register_xenstore_notifier(&xenstore_notifier);
0384 register_reboot_notifier(&xen_reboot_nb);
0385
0386 return 0;
0387 }
0388 EXPORT_SYMBOL_GPL(xen_setup_shutdown_event);
0389
0390 subsys_initcall(xen_setup_shutdown_event);