manage.c 6.8 KB

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