All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chris Wright <chrisw@sous-sol.org>
To: linux-kernel@vger.kernel.org
Cc: virtualization@lists.osdl.org, xen-devel@lists.xensource.com,
	Jeremy Fitzhardinge <jeremy@goop.org>, Andi Kleen <ak@suse.de>,
	Andrew Morton <akpm@osdl.org>,
	Rusty Russell <rusty@rustcorp.com.au>,
	Zachary Amsden <zach@vmware.com>,
	Ian Pratt <ian.pratt@xensource.com>,
	Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Subject: [RFC PATCH 31/33] Add Xen subarch reboot support
Date: Tue, 18 Jul 2006 00:00:31 -0700	[thread overview]
Message-ID: <20060718091958.119562000@sous-sol.org> (raw)
In-Reply-To: 20060718091807.467468000@sous-sol.org

[-- Attachment #1: reboot --]
[-- Type: text/plain, Size: 6836 bytes --]

Add remote reboot capability, so that a virtual machine can be
rebooted, halted or 'powered off' by external management tools.

Signed-off-by: Ian Pratt <ian.pratt@xensource.com>
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>

---
 arch/i386/kernel/Makefile |    1 
 drivers/xen/core/reboot.c |  232 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 233 insertions(+)


diff -r 3c95ca587d82 arch/i386/kernel/Makefile
--- a/arch/i386/kernel/Makefile	Thu Jul 13 18:50:17 2006 -0700
+++ b/arch/i386/kernel/Makefile	Thu Jul 13 18:50:49 2006 -0700
@@ -48,6 +48,7 @@ hw_irq-y			:= i8259.o
 hw_irq-y			:= i8259.o
 
 hw_irq-$(CONFIG_XEN)		:= ../../../drivers/xen/core/evtchn.o
+reboot-$(CONFIG_XEN)		:= ../../../drivers/xen/core/reboot.o
 
 # vsyscall.o contains the vsyscall DSO images as __initdata.
 # We must build both images before we can assemble it.
diff -r 3c95ca587d82 drivers/xen/core/reboot.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/xen/core/reboot.c	Thu Jul 13 18:50:17 2006 -0700
@@ -0,0 +1,232 @@
+#define __KERNEL_SYSCALLS__
+#include <linux/version.h>
+#include <linux/kernel.h>
+#include <linux/mm.h>
+#include <linux/unistd.h>
+#include <linux/module.h>
+#include <linux/reboot.h>
+#include <linux/sysrq.h>
+#include <linux/stringify.h>
+#include <linux/syscalls.h>
+#include <linux/cpu.h>
+#include <linux/kthread.h>
+
+#include <xen/evtchn.h>
+#include <xen/xenbus.h>
+#include <xen/xencons.h>
+
+#include <asm/irq.h>
+#include <asm/mmu_context.h>
+#include <asm/hypervisor.h>
+
+#if defined(__i386__) || defined(__x86_64__)
+/*
+ * Power off function, if any
+ */
+void (*pm_power_off)(void);
+EXPORT_SYMBOL(pm_power_off);
+#endif
+
+extern void ctrl_alt_del(void);
+
+#define SHUTDOWN_INVALID  -1
+#define SHUTDOWN_POWEROFF  0
+#define 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.
+ */
+#define SHUTDOWN_HALT      4
+
+void machine_emergency_restart(void)
+{
+	/* We really want to get pending console data out before we die. */
+	xencons_force_flush();
+	HYPERVISOR_sched_op(SCHEDOP_shutdown, SHUTDOWN_reboot);
+}
+
+void machine_restart(char * __unused)
+{
+	machine_emergency_restart();
+}
+
+void machine_halt(void)
+{
+	machine_power_off();
+}
+
+void machine_power_off(void)
+{
+	/* We really want to get pending console data out before we die. */
+	xencons_force_flush();
+	HYPERVISOR_sched_op(SCHEDOP_shutdown, SHUTDOWN_poweroff);
+}
+
+int reboot_thru_bios = 0;	/* for dmi_scan.c */
+EXPORT_SYMBOL(machine_restart);
+EXPORT_SYMBOL(machine_halt);
+EXPORT_SYMBOL(machine_power_off);
+
+
+/******************************************************************************
+ * Stop/pickle callback handling.
+ */
+
+/* Ignore multiple shutdown requests. */
+static int shutting_down = SHUTDOWN_INVALID;
+static void __shutdown_handler(void *unused);
+static DECLARE_WORK(shutdown_work, __shutdown_handler, NULL);
+
+static int shutdown_process(void *__unused)
+{
+	static char *envp[] = { "HOME=/", "TERM=linux",
+				"PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
+	static char *poweroff_argv[] = { "/sbin/poweroff", NULL };
+
+	if ((shutting_down == SHUTDOWN_POWEROFF) ||
+	    (shutting_down == SHUTDOWN_HALT)) {
+		if (execve(poweroff_argv[0], poweroff_argv, envp) < 0) {
+			sys_reboot(LINUX_REBOOT_MAGIC1,
+				   LINUX_REBOOT_MAGIC2,
+				   LINUX_REBOOT_CMD_POWER_OFF,
+				   NULL);
+		}
+	}
+
+	shutting_down = SHUTDOWN_INVALID; /* could try again */
+
+	return 0;
+}
+
+static void __shutdown_handler(void *unused)
+{
+	int err = 0;
+
+	if (shutting_down != SHUTDOWN_SUSPEND)
+		err = kernel_thread(shutdown_process, NULL,
+				    CLONE_FS | CLONE_FILES);
+
+	if (err < 0) {
+		printk(KERN_WARNING "Error creating shutdown process (%d): "
+		       "retrying...\n", -err);
+		schedule_delayed_work(&shutdown_work, HZ/2);
+	}
+}
+
+static void shutdown_handler(struct xenbus_watch *watch,
+			     const char **vec, unsigned int len)
+{
+	char *str;
+	struct xenbus_transaction xbt;
+	int err;
+
+	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;
+	}
+
+	xenbus_write(xbt, "control", "shutdown", "");
+
+	err = xenbus_transaction_end(xbt, 0);
+	if (err == -EAGAIN) {
+		kfree(str);
+		goto again;
+	}
+
+	if (strcmp(str, "poweroff") == 0)
+		shutting_down = SHUTDOWN_POWEROFF;
+	else if (strcmp(str, "reboot") == 0)
+		ctrl_alt_del();
+	else if (strcmp(str, "suspend") == 0)
+		shutting_down = SHUTDOWN_SUSPEND;
+	else if (strcmp(str, "halt") == 0)
+		shutting_down = SHUTDOWN_HALT;
+	else {
+		printk("Ignoring shutdown request: %s\n", str);
+		shutting_down = SHUTDOWN_INVALID;
+	}
+
+	if (shutting_down != SHUTDOWN_INVALID)
+		schedule_work(&shutdown_work);
+
+	kfree(str);
+}
+
+static void sysrq_handler(struct xenbus_watch *watch, const char **vec,
+			  unsigned int len)
+{
+	char sysrq_key = '\0';
+	struct xenbus_transaction xbt;
+	int err;
+
+ again:
+	err = xenbus_transaction_start(&xbt);
+	if (err)
+		return;
+	if (!xenbus_scanf(xbt, "control", "sysrq", "%c", &sysrq_key)) {
+		printk(KERN_ERR "Unable to read sysrq code in "
+		       "control/sysrq\n");
+		xenbus_transaction_end(xbt, 1);
+		return;
+	}
+
+	if (sysrq_key != '\0')
+		xenbus_printf(xbt, "control", "sysrq", "%c", '\0');
+
+	err = xenbus_transaction_end(xbt, 0);
+	if (err == -EAGAIN)
+		goto again;
+
+#ifdef CONFIG_MAGIC_SYSRQ
+	if (sysrq_key != '\0')
+		handle_sysrq(sysrq_key, NULL, NULL);
+#endif
+}
+
+static struct xenbus_watch shutdown_watch = {
+	.node = "control/shutdown",
+	.callback = shutdown_handler
+};
+
+static struct xenbus_watch sysrq_watch = {
+	.node ="control/sysrq",
+	.callback = sysrq_handler
+};
+
+static int setup_shutdown_watcher(struct notifier_block *notifier,
+                                  unsigned long event,
+                                  void *data)
+{
+	int err;
+
+	err = register_xenbus_watch(&shutdown_watch);
+	if (err)
+		printk(KERN_ERR "Failed to set shutdown watcher\n");
+
+	err = register_xenbus_watch(&sysrq_watch);
+	if (err)
+		printk(KERN_ERR "Failed to set sysrq watcher\n");
+
+	return NOTIFY_DONE;
+}
+
+static int __init setup_shutdown_event(void)
+{
+	static struct notifier_block xenstore_notifier = {
+		.notifier_call = setup_shutdown_watcher
+	};
+	register_xenstore_notifier(&xenstore_notifier);
+	return 0;
+}
+
+subsys_initcall(setup_shutdown_event);

--

  parent reply	other threads:[~2006-07-18  9:26 UTC|newest]

Thread overview: 135+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-18  9:18 [RFC PATCH 00/33] Xen i386 paravirtualization support Chris Wright
2006-07-18  9:18 ` Chris Wright
2006-07-18  7:00 ` [RFC PATCH 01/33] Add apply_to_page_range() function Chris Wright
2006-07-18 10:38   ` Adrian Bunk
2006-07-18 19:29     ` Chris Wright
2006-07-18 19:29       ` Chris Wright
2006-07-20  6:17       ` Adrian Bunk
2006-07-18  7:00 ` [RFC PATCH 02/33] Add sync bitops Chris Wright
2006-07-18  9:56   ` Arjan van de Ven
2006-07-18 10:18     ` Keir Fraser
2006-07-18 10:18       ` Keir Fraser
2006-07-19 12:54     ` Andi Kleen
2006-07-18 10:34   ` Adrian Bunk
2006-07-18  7:00 ` [RFC PATCH 03/33] Add nosegneg capability to the vsyscall page notes Chris Wright
2006-07-18  7:00 ` [RFC PATCH 04/33] Add XEN config options and disable unsupported config options Chris Wright
2006-07-18  9:59   ` Arjan van de Ven
2006-07-18 10:21     ` Keir Fraser
2006-07-18 10:21       ` Keir Fraser
2006-07-18  7:00 ` [RFC PATCH 05/33] Makefile support to build Xen subarch Chris Wright
2006-07-18  7:00   ` Chris Wright
2006-07-18 10:00   ` Arjan van de Ven
2006-07-18 11:40     ` Andrew Morton
2006-07-18 20:41       ` Jeremy Fitzhardinge
2006-07-18 20:15     ` Jeremy Fitzhardinge
2006-07-18  7:00 ` [RFC PATCH 06/33] Add Xen interface header files Chris Wright
2006-07-18  7:00 ` [RFC PATCH 07/33] Hypervisor " Chris Wright
2006-07-18  7:00 ` [RFC PATCH 08/33] Add vmlinuz build target Chris Wright
2006-07-18  7:00 ` [RFC PATCH 09/33] Add start-of-day setup hooks to subarch Chris Wright
2006-07-18 10:03   ` Arjan van de Ven
2006-07-18 20:49     ` Jeremy Fitzhardinge
2006-07-20  6:07   ` Adrian Bunk
2006-07-20 12:10     ` Keir Fraser
2006-07-20 13:27       ` Jeremy Fitzhardinge
2006-07-18  7:00 ` [RFC PATCH 10/33] add support for Xen feature queries Chris Wright
2006-07-18  7:00   ` Chris Wright
2006-07-18  7:00 ` [RFC PATCH 11/33] Add Xen-specific memory management definitions Chris Wright
2006-07-18  7:00 ` [RFC PATCH 12/33] Change __FIXADDR_TOP to leave room for the hypervisor Chris Wright
2006-07-18  7:00   ` Chris Wright
2006-07-18  7:00 ` [RFC PATCH 13/33] Add a new head.S start-of-day file for booting on Xen Chris Wright
2006-07-18 10:06   ` Arjan van de Ven
2006-07-18 20:13     ` Jeremy Fitzhardinge
2006-07-18  7:00 ` [RFC PATCH 14/33] subarch support for controlling interrupt delivery Chris Wright
2006-07-18  7:00 ` [RFC PATCH 15/33] move segment checks to subarch Chris Wright
2006-07-18  7:00   ` Chris Wright
2006-07-18 10:09   ` Arjan van de Ven
2006-07-18 11:28     ` Zachary Amsden
2006-07-18 11:28       ` Zachary Amsden
2006-07-18 19:06   ` Rusty Russell
2006-07-18 19:06     ` Rusty Russell
2006-07-18 19:25     ` Chris Wright
2006-07-18 19:25       ` Chris Wright
2006-07-18 20:00       ` [Xen-devel] " Rusty Russell
2006-07-18  7:00 ` [RFC PATCH 16/33] Add support for Xen to entry.S Chris Wright
2006-07-18 10:11   ` Arjan van de Ven
2006-07-18 20:04     ` Jeremy Fitzhardinge
2006-07-18 19:17   ` Rusty Russell
2006-07-18 19:17     ` Rusty Russell
2006-07-18 20:43     ` Chris Wright
2006-07-18 20:43       ` Chris Wright
2006-07-18 23:03       ` Jeremy Fitzhardinge
2006-07-19  5:30         ` Chris Wright
2006-07-19  5:30           ` Chris Wright
2006-07-18  7:00 ` [RFC PATCH 17/33] Support loading an initrd when running on Xen Chris Wright
2006-07-18  7:00 ` [RFC PATCH 18/33] Subarch support for CPUID instruction Chris Wright
2006-07-18 10:14   ` Arjan van de Ven
2006-07-18 10:26     ` Keir Fraser
2006-07-18 10:26       ` Keir Fraser
2006-07-18 10:38       ` Arjan van de Ven
2006-07-18 11:33     ` Zachary Amsden
2006-07-18 11:33       ` Zachary Amsden
2006-07-18 20:46       ` David Miller
2006-07-18 21:00         ` Chris Wright
2006-07-18  7:00 ` [RFC PATCH 19/33] Support gdt/idt/ldt handling on Xen Chris Wright
2006-07-18  7:00 ` [RFC PATCH 20/33] subarch support for interrupt and exception gates Chris Wright
2006-07-18  7:00 ` [RFC PATCH 21/33] subarch support for control register accesses Chris Wright
2006-07-18  7:00   ` Chris Wright
2006-07-18  7:00 ` [RFC PATCH 22/33] subarch stack pointer update Chris Wright
2006-07-18  7:00 ` [RFC PATCH 23/33] subarch TLB support Chris Wright
2006-07-18 20:39   ` David Miller
2006-07-18 21:00     ` Chris Wright
2006-07-18 21:00       ` Chris Wright
2006-07-18  7:00 ` [RFC PATCH 24/33] Add support for Xen event channels Chris Wright
2006-07-18  7:00 ` [RFC PATCH 25/33] Implement timekeeping for Xen Chris Wright
2006-07-18  7:00   ` Chris Wright
2006-07-25  2:49   ` john stultz
2006-07-25 20:05     ` Jeremy Fitzhardinge
2006-07-18  7:00 ` [RFC PATCH 26/33] subarch suport for idle loop (NO_IDLE_HZ for Xen) Chris Wright
2006-07-18  7:00 ` [RFC PATCH 27/33] Add the Xen virtual console driver Chris Wright
2006-07-18  7:00   ` Chris Wright
2006-07-18 10:24   ` Arjan van de Ven
2006-07-18 10:31     ` Keir Fraser
2006-07-18 10:31       ` Keir Fraser
2006-07-27 15:05   ` Hollis Blanchard
2006-07-18  7:00 ` [RFC PATCH 28/33] Add Xen grant table support Chris Wright
2006-07-18  7:00   ` Chris Wright
2006-07-19 10:04   ` [Xen-devel] " Harry Butterworth
2006-07-19 10:04     ` Harry Butterworth
2006-07-25 18:30   ` Hollis Blanchard
2006-07-25 18:30     ` Hollis Blanchard
2006-07-25 18:45     ` Keir Fraser
2006-07-25 18:45       ` Keir Fraser
2006-07-25 19:06     ` Segher Boessenkool
2006-07-25 19:06       ` [XenPPC] " Segher Boessenkool
2006-07-18  7:00 ` [RFC PATCH 29/33] Add Xen driver utility functions Chris Wright
2006-07-18  7:00 ` [RFC PATCH 30/33] Add the Xenbus sysfs and virtual device hotplug driver Chris Wright
2006-07-18  7:00   ` Chris Wright
2006-07-18  7:00 ` Chris Wright [this message]
2006-07-20  6:16   ` [RFC PATCH 31/33] Add Xen subarch reboot support Adrian Bunk
2006-07-18  7:00 ` [RFC PATCH 32/33] Add the Xen virtual network device driver Chris Wright
2006-07-18 10:27   ` Arjan van de Ven
2006-07-18 10:35     ` Keir Fraser
2006-07-18 10:35       ` Keir Fraser
2006-07-18 10:42       ` Arjan van de Ven
2006-07-18 12:18         ` Dave Boutcher
2006-07-18 12:39     ` jamal
2006-07-18 13:08       ` Herbert Xu
2006-07-18 13:08         ` Herbert Xu
2006-07-18 13:25         ` John Haller
2006-07-18 15:22           ` Herbert Xu
2006-07-18 15:22             ` Herbert Xu
2006-07-18 15:44   ` Stephen Hemminger
2006-07-19  3:55     ` Herbert Xu
2006-07-19  3:55       ` Herbert Xu
2006-07-19  3:55       ` Herbert Xu
2006-07-18 20:42   ` David Miller
2006-07-18 21:09     ` Chris Wright
2006-07-18  7:00 ` [RFC PATCH 33/33] Add Xen virtual block " Chris Wright
2006-07-18 10:34   ` Arjan van de Ven
2006-07-18 20:57     ` Jeremy Fitzhardinge
2006-07-18 13:01   ` Dave Boutcher
2006-07-18 16:25     ` Jeff Garzik
2006-07-18 16:25       ` Jeff Garzik
2006-07-18 19:28     ` Chris Wright
2006-07-18 19:28       ` Chris Wright
2006-07-18 21:22     ` Jeremy Fitzhardinge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060718091958.119562000@sous-sol.org \
    --to=chrisw@sous-sol.org \
    --cc=Christian.Limpach@cl.cam.ac.uk \
    --cc=ak@suse.de \
    --cc=akpm@osdl.org \
    --cc=ian.pratt@xensource.com \
    --cc=jeremy@goop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    --cc=virtualization@lists.osdl.org \
    --cc=xen-devel@lists.xensource.com \
    --cc=zach@vmware.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.