manage.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. err = stop_machine_create();
  68. if (err) {
  69. printk(KERN_ERR "xen suspend: failed to setup stop_machine %d\n", err);
  70. goto out;
  71. }
  72. #ifdef CONFIG_PREEMPT
  73. /* If the kernel is preemptible, we need to freeze all the processes
  74. to prevent them from being in the middle of a pagetable update
  75. during suspend. */
  76. err = freeze_processes();
  77. if (err) {
  78. printk(KERN_ERR "xen suspend: freeze failed %d\n", err);
  79. goto out_destroy_sm;
  80. }
  81. #endif
  82. err = dpm_suspend_start(PMSG_SUSPEND);
  83. if (err) {
  84. printk(KERN_ERR "xen suspend: dpm_suspend_start %d\n", err);
  85. goto out_thaw;
  86. }
  87. printk(KERN_DEBUG "suspending xenstore...\n");
  88. xs_suspend();
  89. err = dpm_suspend_noirq(PMSG_SUSPEND);
  90. if (err) {
  91. printk(KERN_ERR "dpm_suspend_noirq failed: %d\n", err);
  92. goto out_resume;
  93. }
  94. err = stop_machine(xen_suspend, &cancelled, cpumask_of(0));
  95. dpm_resume_noirq(PMSG_RESUME);
  96. if (err) {
  97. printk(KERN_ERR "failed to start xen_suspend: %d\n", err);
  98. cancelled = 1;
  99. }
  100. out_resume:
  101. if (!cancelled) {
  102. xen_arch_resume();
  103. xs_resume();
  104. } else
  105. xs_suspend_cancel();
  106. dpm_resume_end(PMSG_RESUME);
  107. /* Make sure timer events get retriggered on all CPUs */
  108. clock_was_set();
  109. out_thaw:
  110. #ifdef CONFIG_PREEMPT
  111. thaw_processes();
  112. out_destroy_sm:
  113. #endif
  114. stop_machine_destroy();
  115. out:
  116. shutting_down = SHUTDOWN_INVALID;
  117. }
  118. #endif /* CONFIG_PM_SLEEP */
  119. static void shutdown_handler(struct xenbus_watch *watch,
  120. const char **vec, unsigned int len)
  121. {
  122. char *str;
  123. struct xenbus_transaction xbt;
  124. int err;
  125. if (shutting_down != SHUTDOWN_INVALID)
  126. return;
  127. again:
  128. err = xenbus_transaction_start(&xbt);
  129. if (err)
  130. return;
  131. str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
  132. /* Ignore read errors and empty reads. */
  133. if (XENBUS_IS_ERR_READ(str)) {
  134. xenbus_transaction_end(xbt, 1);
  135. return;
  136. }
  137. xenbus_write(xbt, "control", "shutdown", "");
  138. err = xenbus_transaction_end(xbt, 0);
  139. if (err == -EAGAIN) {
  140. kfree(str);
  141. goto again;
  142. }
  143. if (strcmp(str, "poweroff") == 0 ||
  144. strcmp(str, "halt") == 0) {
  145. shutting_down = SHUTDOWN_POWEROFF;
  146. orderly_poweroff(false);
  147. } else if (strcmp(str, "reboot") == 0) {
  148. shutting_down = SHUTDOWN_POWEROFF; /* ? */
  149. ctrl_alt_del();
  150. #ifdef CONFIG_PM_SLEEP
  151. } else if (strcmp(str, "suspend") == 0) {
  152. do_suspend();
  153. #endif
  154. } else {
  155. printk(KERN_INFO "Ignoring shutdown request: %s\n", str);
  156. shutting_down = SHUTDOWN_INVALID;
  157. }
  158. kfree(str);
  159. }
  160. static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
  161. unsigned int len)
  162. {
  163. char sysrq_key = '\0';
  164. struct xenbus_transaction xbt;
  165. int err;
  166. again:
  167. err = xenbus_transaction_start(&xbt);
  168. if (err)
  169. return;
  170. if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
  171. printk(KERN_ERR "Unable to read sysrq code in "
  172. "control/sysrq\n");
  173. xenbus_transaction_end(xbt, 1);
  174. return;
  175. }
  176. if (sysrq_key != '\0')
  177. xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
  178. err = xenbus_transaction_end(xbt, 0);
  179. if (err == -EAGAIN)
  180. goto again;
  181. if (sysrq_key != '\0')
  182. handle_sysrq(sysrq_key, NULL);
  183. }
  184. static struct xenbus_watch shutdown_watch = {
  185. .node = "control/shutdown",
  186. .callback = shutdown_handler
  187. };
  188. static struct xenbus_watch sysrq_watch = {
  189. .node = "control/sysrq",
  190. .callback = sysrq_handler
  191. };
  192. static int setup_shutdown_watcher(void)
  193. {
  194. int err;
  195. err = register_xenbus_watch(&shutdown_watch);
  196. if (err) {
  197. printk(KERN_ERR "Failed to set shutdown watcher\n");
  198. return err;
  199. }
  200. err = register_xenbus_watch(&sysrq_watch);
  201. if (err) {
  202. printk(KERN_ERR "Failed to set sysrq watcher\n");
  203. return err;
  204. }
  205. return 0;
  206. }
  207. static int shutdown_event(struct notifier_block *notifier,
  208. unsigned long event,
  209. void *data)
  210. {
  211. setup_shutdown_watcher();
  212. return NOTIFY_DONE;
  213. }
  214. static int __init setup_shutdown_event(void)
  215. {
  216. static struct notifier_block xenstore_notifier = {
  217. .notifier_call = shutdown_event
  218. };
  219. register_xenstore_notifier(&xenstore_notifier);
  220. return 0;
  221. }
  222. subsys_initcall(setup_shutdown_event);