manage.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Handle extern requests for shutdown, reboot and sysrq
  4. */
  5. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  6. #include <linux/kernel.h>
  7. #include <linux/err.h>
  8. #include <linux/slab.h>
  9. #include <linux/reboot.h>
  10. #include <linux/sysrq.h>
  11. #include <linux/stop_machine.h>
  12. #include <linux/freezer.h>
  13. #include <linux/syscore_ops.h>
  14. #include <linux/export.h>
  15. #include <xen/xen.h>
  16. #include <xen/xenbus.h>
  17. #include <xen/grant_table.h>
  18. #include <xen/events.h>
  19. #include <xen/hvc-console.h>
  20. #include <xen/page.h>
  21. #include <xen/xen-ops.h>
  22. #include <asm/xen/hypercall.h>
  23. #include <asm/xen/hypervisor.h>
  24. enum shutdown_state {
  25. SHUTDOWN_INVALID = -1,
  26. SHUTDOWN_POWEROFF = 0,
  27. SHUTDOWN_SUSPEND = 2,
  28. /* Code 3 is SHUTDOWN_CRASH, which we don't use because the domain can only
  29. report a crash, not be instructed to crash!
  30. HALT is the same as POWEROFF, as far as we're concerned. The tools use
  31. the distinction when we return the reason code to them. */
  32. SHUTDOWN_HALT = 4,
  33. };
  34. /* Ignore multiple shutdown requests. */
  35. static enum shutdown_state shutting_down = SHUTDOWN_INVALID;
  36. struct suspend_info {
  37. int cancelled;
  38. };
  39. static RAW_NOTIFIER_HEAD(xen_resume_notifier);
  40. void xen_resume_notifier_register(struct notifier_block *nb)
  41. {
  42. raw_notifier_chain_register(&xen_resume_notifier, nb);
  43. }
  44. EXPORT_SYMBOL_GPL(xen_resume_notifier_register);
  45. void xen_resume_notifier_unregister(struct notifier_block *nb)
  46. {
  47. raw_notifier_chain_unregister(&xen_resume_notifier, nb);
  48. }
  49. EXPORT_SYMBOL_GPL(xen_resume_notifier_unregister);
  50. #ifdef CONFIG_HIBERNATE_CALLBACKS
  51. static int xen_suspend(void *data)
  52. {
  53. struct suspend_info *si = data;
  54. int err;
  55. BUG_ON(!irqs_disabled());
  56. err = syscore_suspend();
  57. if (err) {
  58. pr_err("%s: system core suspend failed: %d\n", __func__, err);
  59. return err;
  60. }
  61. gnttab_suspend();
  62. xen_manage_runstate_time(-1);
  63. xen_arch_pre_suspend();
  64. si->cancelled = HYPERVISOR_suspend(xen_pv_domain()
  65. ? virt_to_gfn(xen_start_info)
  66. : 0);
  67. xen_arch_post_suspend(si->cancelled);
  68. xen_manage_runstate_time(si->cancelled ? 1 : 0);
  69. gnttab_resume();
  70. if (!si->cancelled) {
  71. xen_irq_resume();
  72. xen_timer_resume();
  73. }
  74. syscore_resume();
  75. return 0;
  76. }
  77. static void do_suspend(void)
  78. {
  79. int err;
  80. struct suspend_info si;
  81. shutting_down = SHUTDOWN_SUSPEND;
  82. err = freeze_processes();
  83. if (err) {
  84. pr_err("%s: freeze processes failed %d\n", __func__, err);
  85. goto out;
  86. }
  87. err = freeze_kernel_threads();
  88. if (err) {
  89. pr_err("%s: freeze kernel threads failed %d\n", __func__, err);
  90. goto out_thaw;
  91. }
  92. err = dpm_suspend_start(PMSG_FREEZE);
  93. if (err) {
  94. pr_err("%s: dpm_suspend_start %d\n", __func__, err);
  95. goto out_thaw;
  96. }
  97. printk(KERN_DEBUG "suspending xenstore...\n");
  98. xs_suspend();
  99. err = dpm_suspend_end(PMSG_FREEZE);
  100. if (err) {
  101. pr_err("dpm_suspend_end failed: %d\n", err);
  102. si.cancelled = 0;
  103. goto out_resume;
  104. }
  105. xen_arch_suspend();
  106. si.cancelled = 1;
  107. err = stop_machine(xen_suspend, &si, cpumask_of(0));
  108. /* Resume console as early as possible. */
  109. if (!si.cancelled)
  110. xen_console_resume();
  111. raw_notifier_call_chain(&xen_resume_notifier, 0, NULL);
  112. dpm_resume_start(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
  113. if (err) {
  114. pr_err("failed to start xen_suspend: %d\n", err);
  115. si.cancelled = 1;
  116. }
  117. xen_arch_resume();
  118. out_resume:
  119. if (!si.cancelled)
  120. xs_resume();
  121. else
  122. xs_suspend_cancel();
  123. dpm_resume_end(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
  124. out_thaw:
  125. thaw_processes();
  126. out:
  127. shutting_down = SHUTDOWN_INVALID;
  128. }
  129. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  130. struct shutdown_handler {
  131. #define SHUTDOWN_CMD_SIZE 11
  132. const char command[SHUTDOWN_CMD_SIZE];
  133. bool flag;
  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. case SYSTEM_SCHEDULING:
  153. orderly_poweroff(true);
  154. break;
  155. case SYSTEM_RUNNING:
  156. orderly_poweroff(false);
  157. break;
  158. default:
  159. /* Don't do it when we are halting/rebooting. */
  160. pr_info("Ignoring Xen toolstack shutdown.\n");
  161. break;
  162. }
  163. }
  164. static void do_reboot(void)
  165. {
  166. shutting_down = SHUTDOWN_POWEROFF; /* ? */
  167. ctrl_alt_del();
  168. }
  169. static struct shutdown_handler shutdown_handlers[] = {
  170. { "poweroff", true, do_poweroff },
  171. { "halt", false, do_poweroff },
  172. { "reboot", true, do_reboot },
  173. #ifdef CONFIG_HIBERNATE_CALLBACKS
  174. { "suspend", true, do_suspend },
  175. #endif
  176. };
  177. static void shutdown_handler(struct xenbus_watch *watch,
  178. const char *path, const char *token)
  179. {
  180. char *str;
  181. struct xenbus_transaction xbt;
  182. int err;
  183. int idx;
  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 (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
  197. if (strcmp(str, shutdown_handlers[idx].command) == 0)
  198. break;
  199. }
  200. /* Only acknowledge commands which we are prepared to handle. */
  201. if (idx < ARRAY_SIZE(shutdown_handlers))
  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 (idx < ARRAY_SIZE(shutdown_handlers)) {
  209. shutdown_handlers[idx].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 *path,
  218. const char *token)
  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. err = xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
  244. if (err) {
  245. pr_err("%s: Error %d writing sysrq in control/sysrq\n",
  246. __func__, err);
  247. xenbus_transaction_end(xbt, 1);
  248. return;
  249. }
  250. }
  251. err = xenbus_transaction_end(xbt, 0);
  252. if (err == -EAGAIN)
  253. goto again;
  254. if (sysrq_key != '\0')
  255. handle_sysrq(sysrq_key);
  256. }
  257. static struct xenbus_watch sysrq_watch = {
  258. .node = "control/sysrq",
  259. .callback = sysrq_handler
  260. };
  261. #endif
  262. static struct xenbus_watch shutdown_watch = {
  263. .node = "control/shutdown",
  264. .callback = shutdown_handler
  265. };
  266. static struct notifier_block xen_reboot_nb = {
  267. .notifier_call = poweroff_nb,
  268. };
  269. static int setup_shutdown_watcher(void)
  270. {
  271. int err;
  272. int idx;
  273. #define FEATURE_PATH_SIZE (SHUTDOWN_CMD_SIZE + sizeof("feature-"))
  274. char node[FEATURE_PATH_SIZE];
  275. err = register_xenbus_watch(&shutdown_watch);
  276. if (err) {
  277. pr_err("Failed to set shutdown watcher\n");
  278. return err;
  279. }
  280. #ifdef CONFIG_MAGIC_SYSRQ
  281. err = register_xenbus_watch(&sysrq_watch);
  282. if (err) {
  283. pr_err("Failed to set sysrq watcher\n");
  284. return err;
  285. }
  286. #endif
  287. for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
  288. if (!shutdown_handlers[idx].flag)
  289. continue;
  290. snprintf(node, FEATURE_PATH_SIZE, "feature-%s",
  291. shutdown_handlers[idx].command);
  292. err = xenbus_printf(XBT_NIL, "control", node, "%u", 1);
  293. if (err) {
  294. pr_err("%s: Error %d writing %s\n", __func__,
  295. err, node);
  296. return err;
  297. }
  298. }
  299. return 0;
  300. }
  301. static int shutdown_event(struct notifier_block *notifier,
  302. unsigned long event,
  303. void *data)
  304. {
  305. setup_shutdown_watcher();
  306. return NOTIFY_DONE;
  307. }
  308. int xen_setup_shutdown_event(void)
  309. {
  310. static struct notifier_block xenstore_notifier = {
  311. .notifier_call = shutdown_event
  312. };
  313. if (!xen_domain())
  314. return -ENODEV;
  315. register_xenstore_notifier(&xenstore_notifier);
  316. register_reboot_notifier(&xen_reboot_nb);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_GPL(xen_setup_shutdown_event);
  320. subsys_initcall(xen_setup_shutdown_event);