manage.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #ifdef CONFIG_PM_SLEEP
  33. static int xen_hvm_suspend(void *data)
  34. {
  35. int err;
  36. struct sched_shutdown r = { .reason = SHUTDOWN_suspend };
  37. int *cancelled = data;
  38. BUG_ON(!irqs_disabled());
  39. err = sysdev_suspend(PMSG_SUSPEND);
  40. if (err) {
  41. printk(KERN_ERR "xen_hvm_suspend: sysdev_suspend failed: %d\n",
  42. err);
  43. return err;
  44. }
  45. *cancelled = HYPERVISOR_sched_op(SCHEDOP_shutdown, &r);
  46. xen_hvm_post_suspend(*cancelled);
  47. gnttab_resume();
  48. if (!*cancelled) {
  49. xen_irq_resume();
  50. xen_console_resume();
  51. xen_timer_resume();
  52. }
  53. sysdev_resume();
  54. return 0;
  55. }
  56. static int xen_suspend(void *data)
  57. {
  58. int err;
  59. int *cancelled = data;
  60. BUG_ON(!irqs_disabled());
  61. err = sysdev_suspend(PMSG_SUSPEND);
  62. if (err) {
  63. printk(KERN_ERR "xen_suspend: sysdev_suspend failed: %d\n",
  64. err);
  65. return err;
  66. }
  67. xen_mm_pin_all();
  68. gnttab_suspend();
  69. xen_pre_suspend();
  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. *cancelled = HYPERVISOR_suspend(virt_to_mfn(xen_start_info));
  76. xen_post_suspend(*cancelled);
  77. gnttab_resume();
  78. xen_mm_unpin_all();
  79. if (!*cancelled) {
  80. xen_irq_resume();
  81. xen_console_resume();
  82. xen_timer_resume();
  83. }
  84. sysdev_resume();
  85. return 0;
  86. }
  87. static void do_suspend(void)
  88. {
  89. int err;
  90. int cancelled = 1;
  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_SUSPEND);
  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_SUSPEND);
  110. if (err) {
  111. printk(KERN_ERR "dpm_suspend_noirq failed: %d\n", err);
  112. goto out_resume;
  113. }
  114. if (xen_hvm_domain())
  115. err = stop_machine(xen_hvm_suspend, &cancelled, cpumask_of(0));
  116. else
  117. err = stop_machine(xen_suspend, &cancelled, cpumask_of(0));
  118. dpm_resume_noirq(PMSG_RESUME);
  119. if (err) {
  120. printk(KERN_ERR "failed to start xen_suspend: %d\n", err);
  121. cancelled = 1;
  122. }
  123. out_resume:
  124. if (!cancelled) {
  125. xen_arch_resume();
  126. xs_resume();
  127. } else
  128. xs_suspend_cancel();
  129. dpm_resume_end(PMSG_RESUME);
  130. /* Make sure timer events get retriggered on all CPUs */
  131. clock_was_set();
  132. out_thaw:
  133. #ifdef CONFIG_PREEMPT
  134. thaw_processes();
  135. out:
  136. #endif
  137. shutting_down = SHUTDOWN_INVALID;
  138. }
  139. #endif /* CONFIG_PM_SLEEP */
  140. static void shutdown_handler(struct xenbus_watch *watch,
  141. const char **vec, unsigned int len)
  142. {
  143. char *str;
  144. struct xenbus_transaction xbt;
  145. int err;
  146. if (shutting_down != SHUTDOWN_INVALID)
  147. return;
  148. again:
  149. err = xenbus_transaction_start(&xbt);
  150. if (err)
  151. return;
  152. str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
  153. /* Ignore read errors and empty reads. */
  154. if (XENBUS_IS_ERR_READ(str)) {
  155. xenbus_transaction_end(xbt, 1);
  156. return;
  157. }
  158. xenbus_write(xbt, "control", "shutdown", "");
  159. err = xenbus_transaction_end(xbt, 0);
  160. if (err == -EAGAIN) {
  161. kfree(str);
  162. goto again;
  163. }
  164. if (strcmp(str, "poweroff") == 0 ||
  165. strcmp(str, "halt") == 0) {
  166. shutting_down = SHUTDOWN_POWEROFF;
  167. orderly_poweroff(false);
  168. } else if (strcmp(str, "reboot") == 0) {
  169. shutting_down = SHUTDOWN_POWEROFF; /* ? */
  170. ctrl_alt_del();
  171. #ifdef CONFIG_PM_SLEEP
  172. } else if (strcmp(str, "suspend") == 0) {
  173. do_suspend();
  174. #endif
  175. } else {
  176. printk(KERN_INFO "Ignoring shutdown request: %s\n", str);
  177. shutting_down = SHUTDOWN_INVALID;
  178. }
  179. kfree(str);
  180. }
  181. #ifdef CONFIG_MAGIC_SYSRQ
  182. static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
  183. unsigned int len)
  184. {
  185. char sysrq_key = '\0';
  186. struct xenbus_transaction xbt;
  187. int err;
  188. again:
  189. err = xenbus_transaction_start(&xbt);
  190. if (err)
  191. return;
  192. if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
  193. printk(KERN_ERR "Unable to read sysrq code in "
  194. "control/sysrq\n");
  195. xenbus_transaction_end(xbt, 1);
  196. return;
  197. }
  198. if (sysrq_key != '\0')
  199. xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
  200. err = xenbus_transaction_end(xbt, 0);
  201. if (err == -EAGAIN)
  202. goto again;
  203. if (sysrq_key != '\0')
  204. handle_sysrq(sysrq_key);
  205. }
  206. static struct xenbus_watch sysrq_watch = {
  207. .node = "control/sysrq",
  208. .callback = sysrq_handler
  209. };
  210. #endif
  211. static struct xenbus_watch shutdown_watch = {
  212. .node = "control/shutdown",
  213. .callback = shutdown_handler
  214. };
  215. static int setup_shutdown_watcher(void)
  216. {
  217. int err;
  218. err = register_xenbus_watch(&shutdown_watch);
  219. if (err) {
  220. printk(KERN_ERR "Failed to set shutdown watcher\n");
  221. return err;
  222. }
  223. #ifdef CONFIG_MAGIC_SYSRQ
  224. err = register_xenbus_watch(&sysrq_watch);
  225. if (err) {
  226. printk(KERN_ERR "Failed to set sysrq watcher\n");
  227. return err;
  228. }
  229. #endif
  230. return 0;
  231. }
  232. static int shutdown_event(struct notifier_block *notifier,
  233. unsigned long event,
  234. void *data)
  235. {
  236. setup_shutdown_watcher();
  237. return NOTIFY_DONE;
  238. }
  239. static int __init __setup_shutdown_event(void)
  240. {
  241. /* Delay initialization in the PV on HVM case */
  242. if (xen_hvm_domain())
  243. return 0;
  244. if (!xen_pv_domain())
  245. return -ENODEV;
  246. return xen_setup_shutdown_event();
  247. }
  248. int xen_setup_shutdown_event(void)
  249. {
  250. static struct notifier_block xenstore_notifier = {
  251. .notifier_call = shutdown_event
  252. };
  253. register_xenstore_notifier(&xenstore_notifier);
  254. return 0;
  255. }
  256. EXPORT_SYMBOL_GPL(xen_setup_shutdown_event);
  257. subsys_initcall(__setup_shutdown_event);