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