manage.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. load_cr3(swapper_pg_dir);
  36. err = device_power_down(PMSG_SUSPEND);
  37. if (err) {
  38. printk(KERN_ERR "xen_suspend: device_power_down 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. device_power_up(PMSG_RESUME);
  55. if (!*cancelled) {
  56. xen_irq_resume();
  57. xen_console_resume();
  58. xen_timer_resume();
  59. }
  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. return;
  75. }
  76. #endif
  77. err = device_suspend(PMSG_SUSPEND);
  78. if (err) {
  79. printk(KERN_ERR "xen suspend: device_suspend %d\n", err);
  80. goto out;
  81. }
  82. printk("suspending xenbus...\n");
  83. /* XXX use normal device tree? */
  84. xenbus_suspend();
  85. err = stop_machine(xen_suspend, &cancelled, &cpumask_of_cpu(0));
  86. if (err) {
  87. printk(KERN_ERR "failed to start xen_suspend: %d\n", err);
  88. goto out;
  89. }
  90. if (!cancelled) {
  91. xen_arch_resume();
  92. xenbus_resume();
  93. } else
  94. xenbus_suspend_cancel();
  95. device_resume(PMSG_RESUME);
  96. /* Make sure timer events get retriggered on all CPUs */
  97. clock_was_set();
  98. out:
  99. #ifdef CONFIG_PREEMPT
  100. thaw_processes();
  101. #endif
  102. shutting_down = SHUTDOWN_INVALID;
  103. }
  104. #endif /* CONFIG_PM_SLEEP */
  105. static void shutdown_handler(struct xenbus_watch *watch,
  106. const char **vec, unsigned int len)
  107. {
  108. char *str;
  109. struct xenbus_transaction xbt;
  110. int err;
  111. if (shutting_down != SHUTDOWN_INVALID)
  112. return;
  113. again:
  114. err = xenbus_transaction_start(&xbt);
  115. if (err)
  116. return;
  117. str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
  118. /* Ignore read errors and empty reads. */
  119. if (XENBUS_IS_ERR_READ(str)) {
  120. xenbus_transaction_end(xbt, 1);
  121. return;
  122. }
  123. xenbus_write(xbt, "control", "shutdown", "");
  124. err = xenbus_transaction_end(xbt, 0);
  125. if (err == -EAGAIN) {
  126. kfree(str);
  127. goto again;
  128. }
  129. if (strcmp(str, "poweroff") == 0 ||
  130. strcmp(str, "halt") == 0) {
  131. shutting_down = SHUTDOWN_POWEROFF;
  132. orderly_poweroff(false);
  133. } else if (strcmp(str, "reboot") == 0) {
  134. shutting_down = SHUTDOWN_POWEROFF; /* ? */
  135. ctrl_alt_del();
  136. #ifdef CONFIG_PM_SLEEP
  137. } else if (strcmp(str, "suspend") == 0) {
  138. do_suspend();
  139. #endif
  140. } else {
  141. printk(KERN_INFO "Ignoring shutdown request: %s\n", str);
  142. shutting_down = SHUTDOWN_INVALID;
  143. }
  144. kfree(str);
  145. }
  146. static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
  147. unsigned int len)
  148. {
  149. char sysrq_key = '\0';
  150. struct xenbus_transaction xbt;
  151. int err;
  152. again:
  153. err = xenbus_transaction_start(&xbt);
  154. if (err)
  155. return;
  156. if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
  157. printk(KERN_ERR "Unable to read sysrq code in "
  158. "control/sysrq\n");
  159. xenbus_transaction_end(xbt, 1);
  160. return;
  161. }
  162. if (sysrq_key != '\0')
  163. xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
  164. err = xenbus_transaction_end(xbt, 0);
  165. if (err == -EAGAIN)
  166. goto again;
  167. if (sysrq_key != '\0')
  168. handle_sysrq(sysrq_key, NULL);
  169. }
  170. static struct xenbus_watch shutdown_watch = {
  171. .node = "control/shutdown",
  172. .callback = shutdown_handler
  173. };
  174. static struct xenbus_watch sysrq_watch = {
  175. .node = "control/sysrq",
  176. .callback = sysrq_handler
  177. };
  178. static int setup_shutdown_watcher(void)
  179. {
  180. int err;
  181. err = register_xenbus_watch(&shutdown_watch);
  182. if (err) {
  183. printk(KERN_ERR "Failed to set shutdown watcher\n");
  184. return err;
  185. }
  186. err = register_xenbus_watch(&sysrq_watch);
  187. if (err) {
  188. printk(KERN_ERR "Failed to set sysrq watcher\n");
  189. return err;
  190. }
  191. return 0;
  192. }
  193. static int shutdown_event(struct notifier_block *notifier,
  194. unsigned long event,
  195. void *data)
  196. {
  197. setup_shutdown_watcher();
  198. return NOTIFY_DONE;
  199. }
  200. static int __init setup_shutdown_event(void)
  201. {
  202. static struct notifier_block xenstore_notifier = {
  203. .notifier_call = shutdown_event
  204. };
  205. register_xenstore_notifier(&xenstore_notifier);
  206. return 0;
  207. }
  208. subsys_initcall(setup_shutdown_event);