manage.c 6.7 KB

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