manage.c 6.6 KB

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