manage.c 8.2 KB
Newer Older
1 2 3
/*
 * Handle extern requests for shutdown, reboot and sysrq
 */
J
Joe Perches 已提交
4 5 6

#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt

7 8
#include <linux/kernel.h>
#include <linux/err.h>
9
#include <linux/slab.h>
10 11
#include <linux/reboot.h>
#include <linux/sysrq.h>
12 13
#include <linux/stop_machine.h>
#include <linux/freezer.h>
14
#include <linux/syscore_ops.h>
15
#include <linux/export.h>
16

17
#include <xen/xen.h>
18
#include <xen/xenbus.h>
19 20 21
#include <xen/grant_table.h>
#include <xen/events.h>
#include <xen/hvc-console.h>
22
#include <xen/page.h>
23
#include <xen/xen-ops.h>
24

25
#include <asm/xen/hypercall.h>
26
#include <asm/xen/hypervisor.h>
27 28 29 30 31 32 33 34 35 36 37

enum shutdown_state {
	SHUTDOWN_INVALID = -1,
	SHUTDOWN_POWEROFF = 0,
	SHUTDOWN_SUSPEND = 2,
	/* Code 3 is SHUTDOWN_CRASH, which we don't use because the domain can only
	   report a crash, not be instructed to crash!
	   HALT is the same as POWEROFF, as far as we're concerned.  The tools use
	   the distinction when we return the reason code to them.  */
	 SHUTDOWN_HALT = 4,
};
38 39

/* Ignore multiple shutdown requests. */
40 41
static enum shutdown_state shutting_down = SHUTDOWN_INVALID;

42 43 44 45
struct suspend_info {
	int cancelled;
};

46 47 48 49 50 51 52 53 54 55 56 57 58 59
static RAW_NOTIFIER_HEAD(xen_resume_notifier);

void xen_resume_notifier_register(struct notifier_block *nb)
{
	raw_notifier_chain_register(&xen_resume_notifier, nb);
}
EXPORT_SYMBOL_GPL(xen_resume_notifier_register);

void xen_resume_notifier_unregister(struct notifier_block *nb)
{
	raw_notifier_chain_unregister(&xen_resume_notifier, nb);
}
EXPORT_SYMBOL_GPL(xen_resume_notifier_unregister);

60
#ifdef CONFIG_HIBERNATE_CALLBACKS
61 62
static int xen_suspend(void *data)
{
63
	struct suspend_info *si = data;
64
	int err;
65 66 67

	BUG_ON(!irqs_disabled());

68
	err = syscore_suspend();
69
	if (err) {
J
Joe Perches 已提交
70
		pr_err("%s: system core suspend failed: %d\n", __func__, err);
71 72
		return err;
	}
73

74
	gnttab_suspend();
75
	xen_manage_runstate_time(-1);
76
	xen_arch_pre_suspend();
77

78
	si->cancelled = HYPERVISOR_suspend(xen_pv_domain()
79
                                           ? virt_to_gfn(xen_start_info)
80
                                           : 0);
81

82
	xen_arch_post_suspend(si->cancelled);
83
	xen_manage_runstate_time(si->cancelled ? 1 : 0);
84
	gnttab_resume();
85

86
	if (!si->cancelled) {
87
		xen_irq_resume();
88
		xen_timer_resume();
89 90
	}

91
	syscore_resume();
92

93 94 95 96 97 98
	return 0;
}

static void do_suspend(void)
{
	int err;
99
	struct suspend_info si;
100 101 102 103 104

	shutting_down = SHUTDOWN_SUSPEND;

	err = freeze_processes();
	if (err) {
105
		pr_err("%s: freeze processes failed %d\n", __func__, err);
106
		goto out;
107 108
	}

109 110 111 112 113 114
	err = freeze_kernel_threads();
	if (err) {
		pr_err("%s: freeze kernel threads failed %d\n", __func__, err);
		goto out_thaw;
	}

115
	err = dpm_suspend_start(PMSG_FREEZE);
116
	if (err) {
J
Joe Perches 已提交
117
		pr_err("%s: dpm_suspend_start %d\n", __func__, err);
118
		goto out_thaw;
119 120
	}

I
Ian Campbell 已提交
121 122 123
	printk(KERN_DEBUG "suspending xenstore...\n");
	xs_suspend();

124
	err = dpm_suspend_end(PMSG_FREEZE);
125
	if (err) {
J
Joe Perches 已提交
126
		pr_err("dpm_suspend_end failed: %d\n", err);
127
		si.cancelled = 0;
128
		goto out_resume;
129 130
	}

131 132
	xen_arch_suspend();

133 134
	si.cancelled = 1;

135
	err = stop_machine(xen_suspend, &si, cpumask_of(0));
136

137 138 139 140
	/* Resume console as early as possible. */
	if (!si.cancelled)
		xen_console_resume();

141 142
	raw_notifier_call_chain(&xen_resume_notifier, 0, NULL);

143
	dpm_resume_start(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
144

145
	if (err) {
J
Joe Perches 已提交
146
		pr_err("failed to start xen_suspend: %d\n", err);
147
		si.cancelled = 1;
148 149
	}

150 151
	xen_arch_resume();

I
Ian Campbell 已提交
152
out_resume:
153
	if (!si.cancelled)
154
		xs_resume();
155
	else
156
		xs_suspend_cancel();
157

158
	dpm_resume_end(si.cancelled ? PMSG_THAW : PMSG_RESTORE);
159

160
out_thaw:
161
	thaw_processes();
162
out:
163 164
	shutting_down = SHUTDOWN_INVALID;
}
165
#endif	/* CONFIG_HIBERNATE_CALLBACKS */
166

167
struct shutdown_handler {
168 169 170
#define SHUTDOWN_CMD_SIZE 11
	const char command[SHUTDOWN_CMD_SIZE];
	bool flag;
171 172 173
	void (*cb)(void);
};

174 175 176 177 178 179 180 181 182 183 184 185
static int poweroff_nb(struct notifier_block *cb, unsigned long code, void *unused)
{
	switch (code) {
	case SYS_DOWN:
	case SYS_HALT:
	case SYS_POWER_OFF:
		shutting_down = SHUTDOWN_POWEROFF;
	default:
		break;
	}
	return NOTIFY_DONE;
}
186 187
static void do_poweroff(void)
{
188 189
	switch (system_state) {
	case SYSTEM_BOOTING:
190
	case SYSTEM_SCHEDULING:
191 192 193 194 195 196 197 198 199 200
		orderly_poweroff(true);
		break;
	case SYSTEM_RUNNING:
		orderly_poweroff(false);
		break;
	default:
		/* Don't do it when we are halting/rebooting. */
		pr_info("Ignoring Xen toolstack shutdown.\n");
		break;
	}
201 202 203 204 205 206 207 208
}

static void do_reboot(void)
{
	shutting_down = SHUTDOWN_POWEROFF; /* ? */
	ctrl_alt_del();
}

209 210 211 212 213 214 215 216 217
static struct shutdown_handler shutdown_handlers[] = {
	{ "poweroff",	true,	do_poweroff },
	{ "halt",	false,	do_poweroff },
	{ "reboot",	true,	do_reboot   },
#ifdef CONFIG_HIBERNATE_CALLBACKS
	{ "suspend",	true,	do_suspend  },
#endif
};

218
static void shutdown_handler(struct xenbus_watch *watch,
219
			     const char *path, const char *token)
220 221 222 223
{
	char *str;
	struct xenbus_transaction xbt;
	int err;
224
	int idx;
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

	if (shutting_down != SHUTDOWN_INVALID)
		return;

 again:
	err = xenbus_transaction_start(&xbt);
	if (err)
		return;

	str = (char *)xenbus_read(xbt, "control", "shutdown", NULL);
	/* Ignore read errors and empty reads. */
	if (XENBUS_IS_ERR_READ(str)) {
		xenbus_transaction_end(xbt, 1);
		return;
	}

241 242
	for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
		if (strcmp(str, shutdown_handlers[idx].command) == 0)
243 244 245 246
			break;
	}

	/* Only acknowledge commands which we are prepared to handle. */
247
	if (idx < ARRAY_SIZE(shutdown_handlers))
248
		xenbus_write(xbt, "control", "shutdown", "");
249 250 251 252 253 254 255

	err = xenbus_transaction_end(xbt, 0);
	if (err == -EAGAIN) {
		kfree(str);
		goto again;
	}

256 257
	if (idx < ARRAY_SIZE(shutdown_handlers)) {
		shutdown_handlers[idx].cb();
258
	} else {
J
Joe Perches 已提交
259
		pr_info("Ignoring shutdown request: %s\n", str);
260 261 262 263 264 265
		shutting_down = SHUTDOWN_INVALID;
	}

	kfree(str);
}

266
#ifdef CONFIG_MAGIC_SYSRQ
267 268
static void sysrq_handler(struct xenbus_watch *watch, const char *path,
			  const char *token)
269 270 271 272 273 274 275 276 277
{
	char sysrq_key = '\0';
	struct xenbus_transaction xbt;
	int err;

 again:
	err = xenbus_transaction_start(&xbt);
	if (err)
		return;
278 279 280 281 282
	err = xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key);
	if (err < 0) {
		/*
		 * The Xenstore watch fires directly after registering it and
		 * after a suspend/resume cycle. So ENOENT is no error but
283 284 285
		 * might happen in those cases. ERANGE is observed when we get
		 * an empty value (''), this happens when we acknowledge the
		 * request by writing '\0' below.
286
		 */
287
		if (err != -ENOENT && err != -ERANGE)
288 289
			pr_err("Error %d reading sysrq code in control/sysrq\n",
			       err);
290 291 292 293
		xenbus_transaction_end(xbt, 1);
		return;
	}

294 295 296 297 298 299 300 301 302
	if (sysrq_key != '\0') {
		err = xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
		if (err) {
			pr_err("%s: Error %d writing sysrq in control/sysrq\n",
			       __func__, err);
			xenbus_transaction_end(xbt, 1);
			return;
		}
	}
303 304 305 306 307 308

	err = xenbus_transaction_end(xbt, 0);
	if (err == -EAGAIN)
		goto again;

	if (sysrq_key != '\0')
309
		handle_sysrq(sysrq_key);
310 311 312 313 314 315
}

static struct xenbus_watch sysrq_watch = {
	.node = "control/sysrq",
	.callback = sysrq_handler
};
316 317 318 319 320 321
#endif

static struct xenbus_watch shutdown_watch = {
	.node = "control/shutdown",
	.callback = shutdown_handler
};
322

323 324 325 326
static struct notifier_block xen_reboot_nb = {
	.notifier_call = poweroff_nb,
};

327 328 329
static int setup_shutdown_watcher(void)
{
	int err;
330 331 332
	int idx;
#define FEATURE_PATH_SIZE (SHUTDOWN_CMD_SIZE + sizeof("feature-"))
	char node[FEATURE_PATH_SIZE];
333 334 335

	err = register_xenbus_watch(&shutdown_watch);
	if (err) {
J
Joe Perches 已提交
336
		pr_err("Failed to set shutdown watcher\n");
337 338 339
		return err;
	}

340

341
#ifdef CONFIG_MAGIC_SYSRQ
342 343
	err = register_xenbus_watch(&sysrq_watch);
	if (err) {
J
Joe Perches 已提交
344
		pr_err("Failed to set sysrq watcher\n");
345 346
		return err;
	}
347
#endif
348

349 350 351 352 353
	for (idx = 0; idx < ARRAY_SIZE(shutdown_handlers); idx++) {
		if (!shutdown_handlers[idx].flag)
			continue;
		snprintf(node, FEATURE_PATH_SIZE, "feature-%s",
			 shutdown_handlers[idx].command);
354 355 356 357 358 359
		err = xenbus_printf(XBT_NIL, "control", node, "%u", 1);
		if (err) {
			pr_err("%s: Error %d writing %s\n", __func__,
				err, node);
			return err;
		}
360 361
	}

362 363 364 365 366 367 368 369 370 371 372
	return 0;
}

static int shutdown_event(struct notifier_block *notifier,
			  unsigned long event,
			  void *data)
{
	setup_shutdown_watcher();
	return NOTIFY_DONE;
}

373
int xen_setup_shutdown_event(void)
374 375 376 377
{
	static struct notifier_block xenstore_notifier = {
		.notifier_call = shutdown_event
	};
378 379 380

	if (!xen_domain())
		return -ENODEV;
381
	register_xenstore_notifier(&xenstore_notifier);
382
	register_reboot_notifier(&xen_reboot_nb);
383 384 385

	return 0;
}
386
EXPORT_SYMBOL_GPL(xen_setup_shutdown_event);
387

388
subsys_initcall(xen_setup_shutdown_event);