manage.c 6.2 KB

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