manage.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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_arch_pre_suspend();
  62. /*
  63. * This hypercall returns 1 if suspend was cancelled
  64. * or the domain was merely checkpointed, and 0 if it
  65. * is resuming in a new domain.
  66. */
  67. si->cancelled = HYPERVISOR_suspend(xen_pv_domain()
  68. ? virt_to_gfn(xen_start_info)
  69. : 0);
  70. xen_arch_post_suspend(si->cancelled);
  71. gnttab_resume();
  72. if (!si->cancelled) {
  73. xen_irq_resume();
  74. xen_timer_resume();
  75. }
  76. syscore_resume();
  77. return 0;
  78. }
  79. static void do_suspend(void)
  80. {
  81. int err;
  82. struct suspend_info si;
  83. shutting_down = SHUTDOWN_SUSPEND;
  84. err = freeze_processes();
  85. if (err) {
  86. pr_err("%s: freeze processes failed %d\n", __func__, err);
  87. goto out;
  88. }
  89. err = freeze_kernel_threads();
  90. if (err) {
  91. pr_err("%s: freeze kernel threads failed %d\n", __func__, err);
  92. goto out_thaw;
  93. }
  94. err = dpm_suspend_start(PMSG_FREEZE);
  95. if (err) {
  96. pr_err("%s: dpm_suspend_start %d\n", __func__, err);
  97. goto out_thaw;
  98. }
  99. printk(KERN_DEBUG "suspending xenstore...\n");
  100. xs_suspend();
  101. err = dpm_suspend_end(PMSG_FREEZE);
  102. if (err) {
  103. pr_err("dpm_suspend_end failed: %d\n", err);
  104. si.cancelled = 0;
  105. goto out_resume;
  106. }
  107. xen_arch_suspend();
  108. si.cancelled = 1;
  109. err = stop_machine(xen_suspend, &si, cpumask_of(0));
  110. /* Resume console as early as possible. */
  111. if (!si.cancelled)
  112. xen_console_resume();
  113. raw_notifier_call_chain(&xen_resume_notifier, 0, NULL);
  114. dpm_resume_start(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
  115. if (err) {
  116. pr_err("failed to start xen_suspend: %d\n", err);
  117. si.cancelled = 1;
  118. }
  119. xen_arch_resume();
  120. out_resume:
  121. if (!si.cancelled)
  122. xs_resume();
  123. else
  124. xs_suspend_cancel();
  125. dpm_resume_end(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
  126. out_thaw:
  127. thaw_processes();
  128. out:
  129. shutting_down = SHUTDOWN_INVALID;
  130. }
  131. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  132. struct shutdown_handler {
  133. const char *command;
  134. void (*cb)(void);
  135. };
  136. static int poweroff_nb(struct notifier_block *cb, unsigned long code, void *unused)
  137. {
  138. switch (code) {
  139. case SYS_DOWN:
  140. case SYS_HALT:
  141. case SYS_POWER_OFF:
  142. shutting_down = SHUTDOWN_POWEROFF;
  143. default:
  144. break;
  145. }
  146. return NOTIFY_DONE;
  147. }
  148. static void do_poweroff(void)
  149. {
  150. switch (system_state) {
  151. case SYSTEM_BOOTING:
  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 void shutdown_handler(struct xenbus_watch *watch,
  169. const char **vec, unsigned int len)
  170. {
  171. char *str;
  172. struct xenbus_transaction xbt;
  173. int err;
  174. static struct shutdown_handler handlers[] = {
  175. { "poweroff", do_poweroff },
  176. { "halt", do_poweroff },
  177. { "reboot", do_reboot },
  178. #ifdef CONFIG_HIBERNATE_CALLBACKS
  179. { "suspend", do_suspend },
  180. #endif
  181. {NULL, NULL},
  182. };
  183. static struct shutdown_handler *handler;
  184. if (shutting_down != SHUTDOWN_INVALID)
  185. return;
  186. again:
  187. err = xenbus_transaction_start(&xbt);
  188. if (err)
  189. return;
  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. for (handler = &handlers[0]; handler->command; handler++) {
  197. if (strcmp(str, handler->command) == 0)
  198. break;
  199. }
  200. /* Only acknowledge commands which we are prepared to handle. */
  201. if (handler->cb)
  202. xenbus_write(xbt, "control", "shutdown", "");
  203. err = xenbus_transaction_end(xbt, 0);
  204. if (err == -EAGAIN) {
  205. kfree(str);
  206. goto again;
  207. }
  208. if (handler->cb) {
  209. handler->cb();
  210. } else {
  211. pr_info("Ignoring shutdown request: %s\n", str);
  212. shutting_down = SHUTDOWN_INVALID;
  213. }
  214. kfree(str);
  215. }
  216. #ifdef CONFIG_MAGIC_SYSRQ
  217. static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
  218. unsigned int len)
  219. {
  220. char sysrq_key = '\0';
  221. struct xenbus_transaction xbt;
  222. int err;
  223. again:
  224. err = xenbus_transaction_start(&xbt);
  225. if (err)
  226. return;
  227. err = xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key);
  228. if (err < 0) {
  229. /*
  230. * The Xenstore watch fires directly after registering it and
  231. * after a suspend/resume cycle. So ENOENT is no error but
  232. * might happen in those cases. ERANGE is observed when we get
  233. * an empty value (''), this happens when we acknowledge the
  234. * request by writing '\0' below.
  235. */
  236. if (err != -ENOENT && err != -ERANGE)
  237. pr_err("Error %d reading sysrq code in control/sysrq\n",
  238. err);
  239. xenbus_transaction_end(xbt, 1);
  240. return;
  241. }
  242. if (sysrq_key != '\0')
  243. xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
  244. err = xenbus_transaction_end(xbt, 0);
  245. if (err == -EAGAIN)
  246. goto again;
  247. if (sysrq_key != '\0')
  248. handle_sysrq(sysrq_key);
  249. }
  250. static struct xenbus_watch sysrq_watch = {
  251. .node = "control/sysrq",
  252. .callback = sysrq_handler
  253. };
  254. #endif
  255. static struct xenbus_watch shutdown_watch = {
  256. .node = "control/shutdown",
  257. .callback = shutdown_handler
  258. };
  259. static struct notifier_block xen_reboot_nb = {
  260. .notifier_call = poweroff_nb,
  261. };
  262. static int setup_shutdown_watcher(void)
  263. {
  264. int err;
  265. err = register_xenbus_watch(&shutdown_watch);
  266. if (err) {
  267. pr_err("Failed to set shutdown watcher\n");
  268. return err;
  269. }
  270. #ifdef CONFIG_MAGIC_SYSRQ
  271. err = register_xenbus_watch(&sysrq_watch);
  272. if (err) {
  273. pr_err("Failed to set sysrq watcher\n");
  274. return err;
  275. }
  276. #endif
  277. return 0;
  278. }
  279. static int shutdown_event(struct notifier_block *notifier,
  280. unsigned long event,
  281. void *data)
  282. {
  283. setup_shutdown_watcher();
  284. return NOTIFY_DONE;
  285. }
  286. int xen_setup_shutdown_event(void)
  287. {
  288. static struct notifier_block xenstore_notifier = {
  289. .notifier_call = shutdown_event
  290. };
  291. if (!xen_domain())
  292. return -ENODEV;
  293. register_xenstore_notifier(&xenstore_notifier);
  294. register_reboot_notifier(&xen_reboot_nb);
  295. return 0;
  296. }
  297. EXPORT_SYMBOL_GPL(xen_setup_shutdown_event);
  298. subsys_initcall(xen_setup_shutdown_event);