/drivers/xen/manage.c

https://github.com/genesi/linux-legacy · C · 262 lines · 197 code · 47 blank · 18 comment · 33 complexity · 35ef9aff1bc477b4484fd85fc66509b3 MD5 · raw file

  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. dpm_resume_noirq(PMSG_RESUME);
  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. dpm_resume_noirq(PMSG_RESUME);
  61. return 0;
  62. }
  63. static void do_suspend(void)
  64. {
  65. int err;
  66. int cancelled = 1;
  67. shutting_down = SHUTDOWN_SUSPEND;
  68. #ifdef CONFIG_PREEMPT
  69. /* If the kernel is preemptible, we need to freeze all the processes
  70. to prevent them from being in the middle of a pagetable update
  71. during suspend. */
  72. err = freeze_processes();
  73. if (err) {
  74. printk(KERN_ERR "xen suspend: freeze failed %d\n", err);
  75. return;
  76. }
  77. #endif
  78. err = dpm_suspend_start(PMSG_SUSPEND);
  79. if (err) {
  80. printk(KERN_ERR "xen suspend: dpm_suspend_start %d\n", err);
  81. goto out;
  82. }
  83. printk(KERN_DEBUG "suspending xenstore...\n");
  84. xs_suspend();
  85. err = dpm_suspend_noirq(PMSG_SUSPEND);
  86. if (err) {
  87. printk(KERN_ERR "dpm_suspend_noirq failed: %d\n", err);
  88. goto resume_devices;
  89. }
  90. err = stop_machine(xen_suspend, &cancelled, cpumask_of(0));
  91. if (err) {
  92. printk(KERN_ERR "failed to start xen_suspend: %d\n", err);
  93. goto out;
  94. }
  95. if (!cancelled) {
  96. xen_arch_resume();
  97. xs_resume();
  98. } else
  99. xs_suspend_cancel();
  100. dpm_resume_noirq(PMSG_RESUME);
  101. resume_devices:
  102. dpm_resume_end(PMSG_RESUME);
  103. /* Make sure timer events get retriggered on all CPUs */
  104. clock_was_set();
  105. out:
  106. #ifdef CONFIG_PREEMPT
  107. thaw_processes();
  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. static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
  154. unsigned int len)
  155. {
  156. char sysrq_key = '\0';
  157. struct xenbus_transaction xbt;
  158. int err;
  159. again:
  160. err = xenbus_transaction_start(&xbt);
  161. if (err)
  162. return;
  163. if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
  164. printk(KERN_ERR "Unable to read sysrq code in "
  165. "control/sysrq\n");
  166. xenbus_transaction_end(xbt, 1);
  167. return;
  168. }
  169. if (sysrq_key != '\0')
  170. xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
  171. err = xenbus_transaction_end(xbt, 0);
  172. if (err == -EAGAIN)
  173. goto again;
  174. if (sysrq_key != '\0')
  175. handle_sysrq(sysrq_key, NULL);
  176. }
  177. static struct xenbus_watch shutdown_watch = {
  178. .node = "control/shutdown",
  179. .callback = shutdown_handler
  180. };
  181. static struct xenbus_watch sysrq_watch = {
  182. .node = "control/sysrq",
  183. .callback = sysrq_handler
  184. };
  185. static int setup_shutdown_watcher(void)
  186. {
  187. int err;
  188. err = register_xenbus_watch(&shutdown_watch);
  189. if (err) {
  190. printk(KERN_ERR "Failed to set shutdown watcher\n");
  191. return err;
  192. }
  193. err = register_xenbus_watch(&sysrq_watch);
  194. if (err) {
  195. printk(KERN_ERR "Failed to set sysrq watcher\n");
  196. return err;
  197. }
  198. return 0;
  199. }
  200. static int shutdown_event(struct notifier_block *notifier,
  201. unsigned long event,
  202. void *data)
  203. {
  204. setup_shutdown_watcher();
  205. return NOTIFY_DONE;
  206. }
  207. static int __init setup_shutdown_event(void)
  208. {
  209. static struct notifier_block xenstore_notifier = {
  210. .notifier_call = shutdown_event
  211. };
  212. register_xenstore_notifier(&xenstore_notifier);
  213. return 0;
  214. }
  215. subsys_initcall(setup_shutdown_event);