manage.c 5.6 KB

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