^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Handle extern requests for shutdown, reboot and sysrq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sysrq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/stop_machine.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/syscore_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <xen/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <xen/xenbus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <xen/grant_table.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <xen/events.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <xen/hvc-console.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <xen/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <xen/xen-ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <asm/xen/hypercall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <asm/xen/hypervisor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) enum shutdown_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) SHUTDOWN_INVALID = -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) SHUTDOWN_POWEROFF = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) SHUTDOWN_SUSPEND = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* Code 3 is SHUTDOWN_CRASH, which we don't use because the domain can only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) report a crash, not be instructed to crash!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) HALT is the same as POWEROFF, as far as we're concerned. The tools use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) the distinction when we return the reason code to them. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) SHUTDOWN_HALT = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* Ignore multiple shutdown requests. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static enum shutdown_state shutting_down = SHUTDOWN_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct suspend_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int cancelled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static RAW_NOTIFIER_HEAD(xen_resume_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) void xen_resume_notifier_register(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) raw_notifier_chain_register(&xen_resume_notifier, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) EXPORT_SYMBOL_GPL(xen_resume_notifier_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) void xen_resume_notifier_unregister(struct notifier_block *nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) raw_notifier_chain_unregister(&xen_resume_notifier, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) EXPORT_SYMBOL_GPL(xen_resume_notifier_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #ifdef CONFIG_HIBERNATE_CALLBACKS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static int xen_suspend(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct suspend_info *si = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) BUG_ON(!irqs_disabled());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) err = syscore_suspend();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pr_err("%s: system core suspend failed: %d\n", __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) gnttab_suspend();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) xen_manage_runstate_time(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) xen_arch_pre_suspend();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) si->cancelled = HYPERVISOR_suspend(xen_pv_domain()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ? virt_to_gfn(xen_start_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) xen_arch_post_suspend(si->cancelled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) xen_manage_runstate_time(si->cancelled ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) gnttab_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!si->cancelled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) xen_irq_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) xen_timer_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) syscore_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void do_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct suspend_info si;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) shutting_down = SHUTDOWN_SUSPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) err = freeze_processes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) pr_err("%s: freeze processes failed %d\n", __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) err = freeze_kernel_threads();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pr_err("%s: freeze kernel threads failed %d\n", __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) goto out_thaw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) err = dpm_suspend_start(PMSG_FREEZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) pr_err("%s: dpm_suspend_start %d\n", __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) goto out_thaw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) printk(KERN_DEBUG "suspending xenstore...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) xs_suspend();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) err = dpm_suspend_end(PMSG_FREEZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pr_err("dpm_suspend_end failed: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) si.cancelled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) goto out_resume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) xen_arch_suspend();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) si.cancelled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) err = stop_machine(xen_suspend, &si, cpumask_of(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Resume console as early as possible. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (!si.cancelled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) xen_console_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) raw_notifier_call_chain(&xen_resume_notifier, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) dpm_resume_start(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) pr_err("failed to start xen_suspend: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) si.cancelled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) xen_arch_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) out_resume:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!si.cancelled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) xs_resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) xs_suspend_cancel();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) dpm_resume_end(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) out_thaw:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) thaw_processes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) shutting_down = SHUTDOWN_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #endif /* CONFIG_HIBERNATE_CALLBACKS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct shutdown_handler {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define SHUTDOWN_CMD_SIZE 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) const char command[SHUTDOWN_CMD_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) bool flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) void (*cb)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int poweroff_nb(struct notifier_block *cb, unsigned long code, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) switch (code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) case SYS_DOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) case SYS_HALT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) case SYS_POWER_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) shutting_down = SHUTDOWN_POWEROFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static void do_poweroff(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) switch (system_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) case SYSTEM_BOOTING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) case SYSTEM_SCHEDULING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) orderly_poweroff(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) case SYSTEM_RUNNING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) orderly_poweroff(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /* Don't do it when we are halting/rebooting. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pr_info("Ignoring Xen toolstack shutdown.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static void do_reboot(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) shutting_down = SHUTDOWN_POWEROFF; /* ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ctrl_alt_del();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static struct shutdown_handler shutdown_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) { "poweroff", true, do_poweroff },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) { "halt", false, do_poweroff },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) { "reboot", true, do_reboot },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) #ifdef CONFIG_HIBERNATE_CALLBACKS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) { "suspend", true, do_suspend },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static void shutdown_handler(struct xenbus_watch *watch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) const char *path, const char *token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct xenbus_transaction xbt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (shutting_down != SHUTDOWN_INVALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) err = xenbus_transaction_start(&xbt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* Ignore read errors and empty reads. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (XENBUS_IS_ERR_READ(str)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) xenbus_transaction_end(xbt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (strcmp(str, shutdown_handlers[idx].command) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /* Only acknowledge commands which we are prepared to handle. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (idx < ARRAY_SIZE(shutdown_handlers))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) xenbus_write(xbt, "control", "shutdown", "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) err = xenbus_transaction_end(xbt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (err == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) kfree(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (idx < ARRAY_SIZE(shutdown_handlers)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) shutdown_handlers[idx].cb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) pr_info("Ignoring shutdown request: %s\n", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) shutting_down = SHUTDOWN_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) kfree(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) #ifdef CONFIG_MAGIC_SYSRQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static void sysrq_handler(struct xenbus_watch *watch, const char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) const char *token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) char sysrq_key = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct xenbus_transaction xbt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) err = xenbus_transaction_start(&xbt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) err = xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * The Xenstore watch fires directly after registering it and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * after a suspend/resume cycle. So ENOENT is no error but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * might happen in those cases. ERANGE is observed when we get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * an empty value (''), this happens when we acknowledge the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * request by writing '\0' below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (err != -ENOENT && err != -ERANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) pr_err("Error %d reading sysrq code in control/sysrq\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) xenbus_transaction_end(xbt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (sysrq_key != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) err = xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) pr_err("%s: Error %d writing sysrq in control/sysrq\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) __func__, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) xenbus_transaction_end(xbt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) err = xenbus_transaction_end(xbt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (err == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (sysrq_key != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) handle_sysrq(sysrq_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static struct xenbus_watch sysrq_watch = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) .node = "control/sysrq",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) .callback = sysrq_handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static struct xenbus_watch shutdown_watch = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) .node = "control/shutdown",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) .callback = shutdown_handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static struct notifier_block xen_reboot_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .notifier_call = poweroff_nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static int setup_shutdown_watcher(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) #define FEATURE_PATH_SIZE (SHUTDOWN_CMD_SIZE + sizeof("feature-"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) char node[FEATURE_PATH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) err = register_xenbus_watch(&shutdown_watch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) pr_err("Failed to set shutdown watcher\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) #ifdef CONFIG_MAGIC_SYSRQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) err = register_xenbus_watch(&sysrq_watch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) pr_err("Failed to set sysrq watcher\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (!shutdown_handlers[idx].flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) snprintf(node, FEATURE_PATH_SIZE, "feature-%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) shutdown_handlers[idx].command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) err = xenbus_printf(XBT_NIL, "control", node, "%u", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) pr_err("%s: Error %d writing %s\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) err, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int shutdown_event(struct notifier_block *notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) unsigned long event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) setup_shutdown_watcher();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) int xen_setup_shutdown_event(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static struct notifier_block xenstore_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) .notifier_call = shutdown_event
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (!xen_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) register_xenstore_notifier(&xenstore_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) register_reboot_notifier(&xen_reboot_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) EXPORT_SYMBOL_GPL(xen_setup_shutdown_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) subsys_initcall(xen_setup_shutdown_event);