manage.c 5.5 KB

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