manage.c 7.4 KB

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