manage.c 6.8 KB

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