manage.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Handle extern requests for shutdown, reboot and sysrq
  3. */
  4. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  5. #include <linux/kernel.h>
  6. #include <linux/err.h>
  7. #include <linux/slab.h>
  8. #include <linux/reboot.h>
  9. #include <linux/sysrq.h>
  10. #include <linux/stop_machine.h>
  11. #include <linux/freezer.h>
  12. #include <linux/syscore_ops.h>
  13. #include <linux/export.h>
  14. #include <xen/xen.h>
  15. #include <xen/xenbus.h>
  16. #include <xen/grant_table.h>
  17. #include <xen/events.h>
  18. #include <xen/hvc-console.h>
  19. #include <xen/page.h>
  20. #include <xen/xen-ops.h>
  21. #include <asm/xen/hypercall.h>
  22. #include <asm/xen/hypervisor.h>
  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. };
  33. /* Ignore multiple shutdown requests. */
  34. static enum shutdown_state shutting_down = SHUTDOWN_INVALID;
  35. struct suspend_info {
  36. int cancelled;
  37. };
  38. static RAW_NOTIFIER_HEAD(xen_resume_notifier);
  39. void xen_resume_notifier_register(struct notifier_block *nb)
  40. {
  41. raw_notifier_chain_register(&xen_resume_notifier, nb);
  42. }
  43. EXPORT_SYMBOL_GPL(xen_resume_notifier_register);
  44. void xen_resume_notifier_unregister(struct notifier_block *nb)
  45. {
  46. raw_notifier_chain_unregister(&xen_resume_notifier, nb);
  47. }
  48. EXPORT_SYMBOL_GPL(xen_resume_notifier_unregister);
  49. #ifdef CONFIG_HIBERNATE_CALLBACKS
  50. static int xen_suspend(void *data)
  51. {
  52. struct suspend_info *si = data;
  53. int err;
  54. BUG_ON(!irqs_disabled());
  55. err = syscore_suspend();
  56. if (err) {
  57. pr_err("%s: system core suspend failed: %d\n", __func__, err);
  58. return err;
  59. }
  60. gnttab_suspend();
  61. xen_manage_runstate_time(-1);
  62. xen_arch_pre_suspend();
  63. si->cancelled = HYPERVISOR_suspend(xen_pv_domain()
  64. ? virt_to_gfn(xen_start_info)
  65. : 0);
  66. xen_arch_post_suspend(si->cancelled);
  67. xen_manage_runstate_time(si->cancelled ? 1 : 0);
  68. gnttab_resume();
  69. if (!si->cancelled) {
  70. xen_irq_resume();
  71. xen_timer_resume();
  72. }
  73. syscore_resume();
  74. return 0;
  75. }
  76. static void do_suspend(void)
  77. {
  78. int err;
  79. struct suspend_info si;
  80. shutting_down = SHUTDOWN_SUSPEND;
  81. err = freeze_processes();
  82. if (err) {
  83. pr_err("%s: freeze processes failed %d\n", __func__, err);
  84. goto out;
  85. }
  86. err = freeze_kernel_threads();
  87. if (err) {
  88. pr_err("%s: freeze kernel threads failed %d\n", __func__, err);
  89. goto out_thaw;
  90. }
  91. err = dpm_suspend_start(PMSG_FREEZE);
  92. if (err) {
  93. pr_err("%s: dpm_suspend_start %d\n", __func__, err);
  94. goto out_thaw;
  95. }
  96. printk(KERN_DEBUG "suspending xenstore...\n");
  97. xs_suspend();
  98. err = dpm_suspend_end(PMSG_FREEZE);
  99. if (err) {
  100. pr_err("dpm_suspend_end failed: %d\n", err);
  101. si.cancelled = 0;
  102. goto out_resume;
  103. }
  104. xen_arch_suspend();
  105. si.cancelled = 1;
  106. err = stop_machine(xen_suspend, &si, cpumask_of(0));
  107. /* Resume console as early as possible. */
  108. if (!si.cancelled)
  109. xen_console_resume();
  110. raw_notifier_call_chain(&xen_resume_notifier, 0, NULL);
  111. dpm_resume_start(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
  112. if (err) {
  113. pr_err("failed to start xen_suspend: %d\n", err);
  114. si.cancelled = 1;
  115. }
  116. xen_arch_resume();
  117. out_resume:
  118. if (!si.cancelled)
  119. xs_resume();
  120. else
  121. xs_suspend_cancel();
  122. dpm_resume_end(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
  123. out_thaw:
  124. thaw_processes();
  125. out:
  126. shutting_down = SHUTDOWN_INVALID;
  127. }
  128. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  129. struct shutdown_handler {
  130. #define SHUTDOWN_CMD_SIZE 11
  131. const char command[SHUTDOWN_CMD_SIZE];
  132. bool flag;
  133. void (*cb)(void);
  134. };
  135. static int poweroff_nb(struct notifier_block *cb, unsigned long code, void *unused)
  136. {
  137. switch (code) {
  138. case SYS_DOWN:
  139. case SYS_HALT:
  140. case SYS_POWER_OFF:
  141. shutting_down = SHUTDOWN_POWEROFF;
  142. default:
  143. break;
  144. }
  145. return NOTIFY_DONE;
  146. }
  147. static void do_poweroff(void)
  148. {
  149. switch (system_state) {
  150. case SYSTEM_BOOTING:
  151. case SYSTEM_SCHEDULING:
  152. orderly_poweroff(true);
  153. break;
  154. case SYSTEM_RUNNING:
  155. orderly_poweroff(false);
  156. break;
  157. default:
  158. /* Don't do it when we are halting/rebooting. */
  159. pr_info("Ignoring Xen toolstack shutdown.\n");
  160. break;
  161. }
  162. }
  163. static void do_reboot(void)
  164. {
  165. shutting_down = SHUTDOWN_POWEROFF; /* ? */
  166. ctrl_alt_del();
  167. }
  168. static struct shutdown_handler shutdown_handlers[] = {
  169. { "poweroff", true, do_poweroff },
  170. { "halt", false, do_poweroff },
  171. { "reboot", true, do_reboot },
  172. #ifdef CONFIG_HIBERNATE_CALLBACKS
  173. { "suspend", true, do_suspend },
  174. #endif
  175. };
  176. static void shutdown_handler(struct xenbus_watch *watch,
  177. const char *path, const char *token)
  178. {
  179. char *str;
  180. struct xenbus_transaction xbt;
  181. int err;
  182. int idx;
  183. if (shutting_down != SHUTDOWN_INVALID)
  184. return;
  185. again:
  186. err = xenbus_transaction_start(&xbt);
  187. if (err)
  188. return;
  189. str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
  190. /* Ignore read errors and empty reads. */
  191. if (XENBUS_IS_ERR_READ(str)) {
  192. xenbus_transaction_end(xbt, 1);
  193. return;
  194. }
  195. for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
  196. if (strcmp(str, shutdown_handlers[idx].command) == 0)
  197. break;
  198. }
  199. /* Only acknowledge commands which we are prepared to handle. */
  200. if (idx < ARRAY_SIZE(shutdown_handlers))
  201. xenbus_write(xbt, "control", "shutdown", "");
  202. err = xenbus_transaction_end(xbt, 0);
  203. if (err == -EAGAIN) {
  204. kfree(str);
  205. goto again;
  206. }
  207. if (idx < ARRAY_SIZE(shutdown_handlers)) {
  208. shutdown_handlers[idx].cb();
  209. } else {
  210. pr_info("Ignoring shutdown request: %s\n", str);
  211. shutting_down = SHUTDOWN_INVALID;
  212. }
  213. kfree(str);
  214. }
  215. #ifdef CONFIG_MAGIC_SYSRQ
  216. static void sysrq_handler(struct xenbus_watch *watch, const char *path,
  217. const char *token)
  218. {
  219. char sysrq_key = '\0';
  220. struct xenbus_transaction xbt;
  221. int err;
  222. again:
  223. err = xenbus_transaction_start(&xbt);
  224. if (err)
  225. return;
  226. err = xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key);
  227. if (err < 0) {
  228. /*
  229. * The Xenstore watch fires directly after registering it and
  230. * after a suspend/resume cycle. So ENOENT is no error but
  231. * might happen in those cases. ERANGE is observed when we get
  232. * an empty value (''), this happens when we acknowledge the
  233. * request by writing '\0' below.
  234. */
  235. if (err != -ENOENT && err != -ERANGE)
  236. pr_err("Error %d reading sysrq code in control/sysrq\n",
  237. err);
  238. xenbus_transaction_end(xbt, 1);
  239. return;
  240. }
  241. if (sysrq_key != '\0') {
  242. err = xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
  243. if (err) {
  244. pr_err("%s: Error %d writing sysrq in control/sysrq\n",
  245. __func__, err);
  246. xenbus_transaction_end(xbt, 1);
  247. return;
  248. }
  249. }
  250. err = xenbus_transaction_end(xbt, 0);
  251. if (err == -EAGAIN)
  252. goto again;
  253. if (sysrq_key != '\0')
  254. handle_sysrq(sysrq_key);
  255. }
  256. static struct xenbus_watch sysrq_watch = {
  257. .node = "control/sysrq",
  258. .callback = sysrq_handler
  259. };
  260. #endif
  261. static struct xenbus_watch shutdown_watch = {
  262. .node = "control/shutdown",
  263. .callback = shutdown_handler
  264. };
  265. static struct notifier_block xen_reboot_nb = {
  266. .notifier_call = poweroff_nb,
  267. };
  268. static int setup_shutdown_watcher(void)
  269. {
  270. int err;
  271. int idx;
  272. #define FEATURE_PATH_SIZE (SHUTDOWN_CMD_SIZE + sizeof("feature-"))
  273. char node[FEATURE_PATH_SIZE];
  274. err = register_xenbus_watch(&shutdown_watch);
  275. if (err) {
  276. pr_err("Failed to set shutdown watcher\n");
  277. return err;
  278. }
  279. #ifdef CONFIG_MAGIC_SYSRQ
  280. err = register_xenbus_watch(&sysrq_watch);
  281. if (err) {
  282. pr_err("Failed to set sysrq watcher\n");
  283. return err;
  284. }
  285. #endif
  286. for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
  287. if (!shutdown_handlers[idx].flag)
  288. continue;
  289. snprintf(node, FEATURE_PATH_SIZE, "feature-%s",
  290. shutdown_handlers[idx].command);
  291. err = xenbus_printf(XBT_NIL, "control", node, "%u", 1);
  292. if (err) {
  293. pr_err("%s: Error %d writing %s\n", __func__,
  294. err, node);
  295. return err;
  296. }
  297. }
  298. return 0;
  299. }
  300. static int shutdown_event(struct notifier_block *notifier,
  301. unsigned long event,
  302. void *data)
  303. {
  304. setup_shutdown_watcher();
  305. return NOTIFY_DONE;
  306. }
  307. int xen_setup_shutdown_event(void)
  308. {
  309. static struct notifier_block xenstore_notifier = {
  310. .notifier_call = shutdown_event
  311. };
  312. if (!xen_domain())
  313. return -ENODEV;
  314. register_xenstore_notifier(&xenstore_notifier);
  315. register_reboot_notifier(&xen_reboot_nb);
  316. return 0;
  317. }
  318. EXPORT_SYMBOL_GPL(xen_setup_shutdown_event);
  319. subsys_initcall(xen_setup_shutdown_event);