manage.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Handle extern requests for shutdown, reboot and sysrq
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/err.h>
  6. #include <linux/slab.h>
  7. #include <linux/reboot.h>
  8. #include <linux/sysrq.h>
  9. #include <linux/stop_machine.h>
  10. #include <linux/freezer.h>
  11. #include <linux/syscore_ops.h>
  12. #include <xen/xen.h>
  13. #include <xen/xenbus.h>
  14. #include <xen/grant_table.h>
  15. #include <xen/events.h>
  16. #include <xen/hvc-console.h>
  17. #include <xen/xen-ops.h>
  18. #include <asm/xen/hypercall.h>
  19. #include <asm/xen/page.h>
  20. #include <asm/xen/hypervisor.h>
  21. enum shutdown_state {
  22. SHUTDOWN_INVALID = -1,
  23. SHUTDOWN_POWEROFF = 0,
  24. SHUTDOWN_SUSPEND = 2,
  25. /* Code 3 is SHUTDOWN_CRASH, which we don't use because the domain can only
  26. report a crash, not be instructed to crash!
  27. HALT is the same as POWEROFF, as far as we're concerned. The tools use
  28. the distinction when we return the reason code to them. */
  29. SHUTDOWN_HALT = 4,
  30. };
  31. /* Ignore multiple shutdown requests. */
  32. static enum shutdown_state shutting_down = SHUTDOWN_INVALID;
  33. struct suspend_info {
  34. int cancelled;
  35. unsigned long arg; /* extra hypercall argument */
  36. void (*pre)(void);
  37. void (*post)(int cancelled);
  38. };
  39. static void xen_hvm_post_suspend(int cancelled)
  40. {
  41. xen_arch_hvm_post_suspend(cancelled);
  42. gnttab_resume();
  43. }
  44. static void xen_pre_suspend(void)
  45. {
  46. xen_mm_pin_all();
  47. gnttab_suspend();
  48. xen_arch_pre_suspend();
  49. }
  50. static void xen_post_suspend(int cancelled)
  51. {
  52. xen_arch_post_suspend(cancelled);
  53. gnttab_resume();
  54. xen_mm_unpin_all();
  55. }
  56. #ifdef CONFIG_HIBERNATE_CALLBACKS
  57. static int xen_suspend(void *data)
  58. {
  59. struct suspend_info *si = data;
  60. int err;
  61. BUG_ON(!irqs_disabled());
  62. err = sysdev_suspend(PMSG_FREEZE);
  63. if (!err) {
  64. err = syscore_suspend();
  65. if (err)
  66. sysdev_resume();
  67. }
  68. if (err) {
  69. printk(KERN_ERR "xen_suspend: system core suspend failed: %d\n",
  70. err);
  71. return err;
  72. }
  73. if (si->pre)
  74. si->pre();
  75. /*
  76. * This hypercall returns 1 if suspend was cancelled
  77. * or the domain was merely checkpointed, and 0 if it
  78. * is resuming in a new domain.
  79. */
  80. si->cancelled = HYPERVISOR_suspend(si->arg);
  81. if (si->post)
  82. si->post(si->cancelled);
  83. if (!si->cancelled) {
  84. xen_irq_resume();
  85. xen_console_resume();
  86. xen_timer_resume();
  87. }
  88. syscore_resume();
  89. sysdev_resume();
  90. return 0;
  91. }
  92. static void do_suspend(void)
  93. {
  94. int err;
  95. struct suspend_info si;
  96. shutting_down = SHUTDOWN_SUSPEND;
  97. #ifdef CONFIG_PREEMPT
  98. /* If the kernel is preemptible, we need to freeze all the processes
  99. to prevent them from being in the middle of a pagetable update
  100. during suspend. */
  101. err = freeze_processes();
  102. if (err) {
  103. printk(KERN_ERR "xen suspend: freeze failed %d\n", err);
  104. goto out;
  105. }
  106. #endif
  107. err = dpm_suspend_start(PMSG_FREEZE);
  108. if (err) {
  109. printk(KERN_ERR "xen suspend: dpm_suspend_start %d\n", err);
  110. goto out_thaw;
  111. }
  112. printk(KERN_DEBUG "suspending xenstore...\n");
  113. xs_suspend();
  114. err = dpm_suspend_noirq(PMSG_FREEZE);
  115. if (err) {
  116. printk(KERN_ERR "dpm_suspend_noirq failed: %d\n", err);
  117. goto out_resume;
  118. }
  119. si.cancelled = 1;
  120. if (xen_hvm_domain()) {
  121. si.arg = 0UL;
  122. si.pre = NULL;
  123. si.post = &xen_hvm_post_suspend;
  124. } else {
  125. si.arg = virt_to_mfn(xen_start_info);
  126. si.pre = &xen_pre_suspend;
  127. si.post = &xen_post_suspend;
  128. }
  129. err = stop_machine(xen_suspend, &si, cpumask_of(0));
  130. dpm_resume_noirq(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
  131. if (err) {
  132. printk(KERN_ERR "failed to start xen_suspend: %d\n", err);
  133. si.cancelled = 1;
  134. }
  135. out_resume:
  136. if (!si.cancelled) {
  137. xen_arch_resume();
  138. xs_resume();
  139. } else
  140. xs_suspend_cancel();
  141. dpm_resume_end(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
  142. /* Make sure timer events get retriggered on all CPUs */
  143. clock_was_set();
  144. out_thaw:
  145. #ifdef CONFIG_PREEMPT
  146. thaw_processes();
  147. out:
  148. #endif
  149. shutting_down = SHUTDOWN_INVALID;
  150. }
  151. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  152. struct shutdown_handler {
  153. const char *command;
  154. void (*cb)(void);
  155. };
  156. static void do_poweroff(void)
  157. {
  158. shutting_down = SHUTDOWN_POWEROFF;
  159. orderly_poweroff(false);
  160. }
  161. static void do_reboot(void)
  162. {
  163. shutting_down = SHUTDOWN_POWEROFF; /* ? */
  164. ctrl_alt_del();
  165. }
  166. static void shutdown_handler(struct xenbus_watch *watch,
  167. const char **vec, unsigned int len)
  168. {
  169. char *str;
  170. struct xenbus_transaction xbt;
  171. int err;
  172. static struct shutdown_handler handlers[] = {
  173. { "poweroff", do_poweroff },
  174. { "halt", do_poweroff },
  175. { "reboot", do_reboot },
  176. #ifdef CONFIG_HIBERNATE_CALLBACKS
  177. { "suspend", do_suspend },
  178. #endif
  179. {NULL, NULL},
  180. };
  181. static struct shutdown_handler *handler;
  182. if (shutting_down != SHUTDOWN_INVALID)
  183. return;
  184. again:
  185. err = xenbus_transaction_start(&xbt);
  186. if (err)
  187. return;
  188. str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
  189. /* Ignore read errors and empty reads. */
  190. if (XENBUS_IS_ERR_READ(str)) {
  191. xenbus_transaction_end(xbt, 1);
  192. return;
  193. }
  194. for (handler = &handlers[0]; handler->command; handler++) {
  195. if (strcmp(str, handler->command) == 0)
  196. break;
  197. }
  198. /* Only acknowledge commands which we are prepared to handle. */
  199. if (handler->cb)
  200. xenbus_write(xbt, "control", "shutdown", "");
  201. err = xenbus_transaction_end(xbt, 0);
  202. if (err == -EAGAIN) {
  203. kfree(str);
  204. goto again;
  205. }
  206. if (handler->cb) {
  207. handler->cb();
  208. } else {
  209. printk(KERN_INFO "Ignoring shutdown request: %s\n", str);
  210. shutting_down = SHUTDOWN_INVALID;
  211. }
  212. kfree(str);
  213. }
  214. #ifdef CONFIG_MAGIC_SYSRQ
  215. static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
  216. unsigned int len)
  217. {
  218. char sysrq_key = '\0';
  219. struct xenbus_transaction xbt;
  220. int err;
  221. again:
  222. err = xenbus_transaction_start(&xbt);
  223. if (err)
  224. return;
  225. if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
  226. printk(KERN_ERR "Unable to read sysrq code in "
  227. "control/sysrq\n");
  228. xenbus_transaction_end(xbt, 1);
  229. return;
  230. }
  231. if (sysrq_key != '\0')
  232. xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
  233. err = xenbus_transaction_end(xbt, 0);
  234. if (err == -EAGAIN)
  235. goto again;
  236. if (sysrq_key != '\0')
  237. handle_sysrq(sysrq_key);
  238. }
  239. static struct xenbus_watch sysrq_watch = {
  240. .node = "control/sysrq",
  241. .callback = sysrq_handler
  242. };
  243. #endif
  244. static struct xenbus_watch shutdown_watch = {
  245. .node = "control/shutdown",
  246. .callback = shutdown_handler
  247. };
  248. static int setup_shutdown_watcher(void)
  249. {
  250. int err;
  251. err = register_xenbus_watch(&shutdown_watch);
  252. if (err) {
  253. printk(KERN_ERR "Failed to set shutdown watcher\n");
  254. return err;
  255. }
  256. #ifdef CONFIG_MAGIC_SYSRQ
  257. err = register_xenbus_watch(&sysrq_watch);
  258. if (err) {
  259. printk(KERN_ERR "Failed to set sysrq watcher\n");
  260. return err;
  261. }
  262. #endif
  263. return 0;
  264. }
  265. static int shutdown_event(struct notifier_block *notifier,
  266. unsigned long event,
  267. void *data)
  268. {
  269. setup_shutdown_watcher();
  270. return NOTIFY_DONE;
  271. }
  272. int xen_setup_shutdown_event(void)
  273. {
  274. static struct notifier_block xenstore_notifier = {
  275. .notifier_call = shutdown_event
  276. };
  277. if (!xen_domain())
  278. return -ENODEV;
  279. register_xenstore_notifier(&xenstore_notifier);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL_GPL(xen_setup_shutdown_event);
  283. subsys_initcall(xen_setup_shutdown_event);