From 467e0c9853502a21c2993d2b385698296c307b73 Mon Sep 17 00:00:00 2001 From: yamt Date: Fri, 11 Aug 2006 13:22:43 +0000 Subject: [PATCH] "xm shutdown" support for xen3. ok'ed by Manuel Bouyer. --- sys/arch/xen/conf/files.xen | 3 +- sys/arch/xen/include/shutdown_xenbus.h | 34 ++++++ sys/arch/xen/xen/hypervisor.c | 13 ++- sys/arch/xen/xen/shutdown_xenbus.c | 155 +++++++++++++++++++++++++ sys/arch/xen/xenbus/xenbus_probe.c | 6 +- 5 files changed, 203 insertions(+), 8 deletions(-) create mode 100644 sys/arch/xen/include/shutdown_xenbus.h create mode 100644 sys/arch/xen/xen/shutdown_xenbus.c diff --git a/sys/arch/xen/conf/files.xen b/sys/arch/xen/conf/files.xen index 9a8266c45726..5103ba6a9421 100644 --- a/sys/arch/xen/conf/files.xen +++ b/sys/arch/xen/conf/files.xen @@ -1,4 +1,4 @@ -# $NetBSD: files.xen,v 1.45 2006/07/12 15:02:15 yamt Exp $ +# $NetBSD: files.xen,v 1.46 2006/08/11 13:22:43 yamt Exp $ # NetBSD: files.x86,v 1.10 2003/10/08 17:30:00 bouyer Exp # NetBSD: files.i386,v 1.254 2004/03/25 23:32:10 jmc Exp @@ -125,6 +125,7 @@ file arch/xen/i386/mainbus.c mainbus device hypervisor { }: isabus, pcibus, sysmon_power, xendevbus, acpibus attach hypervisor at hypervisorbus file arch/xen/xen/hypervisor.c hypervisor needs-flag +file arch/xen/xen/shutdown_xenbus.c hypervisor & xen3 # Xenbus (xen3 only) device xenbus {[id = -1]} diff --git a/sys/arch/xen/include/shutdown_xenbus.h b/sys/arch/xen/include/shutdown_xenbus.h new file mode 100644 index 000000000000..f2e371e4e3a9 --- /dev/null +++ b/sys/arch/xen/include/shutdown_xenbus.h @@ -0,0 +1,34 @@ +/* $Id: shutdown_xenbus.h,v 1.1 2006/08/11 13:22:43 yamt Exp $ */ + +/*- + * Copyright (c)2006 YAMAMOTO Takashi, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _XEN_SHUTDOWN_XENBUS_H_ +#define _XEN_SHUTDOWN_XENBUS_H_ + +void shutdown_xenbus_setup(void); + +#endif /* _XEN_SHUTDOWN_XENBUS_H_ */ diff --git a/sys/arch/xen/xen/hypervisor.c b/sys/arch/xen/xen/hypervisor.c index 5b3911d13574..af6047120f4a 100644 --- a/sys/arch/xen/xen/hypervisor.c +++ b/sys/arch/xen/xen/hypervisor.c @@ -1,4 +1,4 @@ -/* $NetBSD: hypervisor.c,v 1.25 2006/07/07 18:15:53 yamt Exp $ */ +/* $NetBSD: hypervisor.c,v 1.26 2006/08/11 13:22:43 yamt Exp $ */ /* * Copyright (c) 2005 Manuel Bouyer. @@ -63,13 +63,16 @@ #include -__KERNEL_RCSID(0, "$NetBSD: hypervisor.c,v 1.25 2006/07/07 18:15:53 yamt Exp $"); +__KERNEL_RCSID(0, "$NetBSD: hypervisor.c,v 1.26 2006/08/11 13:22:43 yamt Exp $"); #include #include #include #include + +#ifndef XEN3 #include +#endif #include "xenbus.h" #include "xencons.h" @@ -191,7 +194,6 @@ struct x86_isa_chipset x86_isa_chipset; /* shutdown/reboot message stuff */ #ifndef XEN3 static void hypervisor_shutdown_handler(ctrl_msg_t *, unsigned long); -#endif static struct sysmon_pswitch hysw_shutdown = { .smpsw_type = PSWITCH_TYPE_POWER, .smpsw_name = "hypervisor", @@ -200,6 +202,7 @@ static struct sysmon_pswitch hysw_reboot = { .smpsw_type = PSWITCH_TYPE_RESET, .smpsw_name = "hypervisor", }; +#endif /* * Probe for the hypervisor; always succeeds. @@ -361,12 +364,12 @@ hypervisor_attach(parent, self, aux) #endif } #endif +#ifndef XEN3 if (sysmon_pswitch_register(&hysw_reboot) != 0 || sysmon_pswitch_register(&hysw_shutdown) != 0) printf("%s: unable to register with sysmon\n", self->dv_xname); -#ifndef XEN3 - else + else ctrl_if_register_receiver(CMSG_SHUTDOWN, hypervisor_shutdown_handler, CALLBACK_IN_BLOCKING_CONTEXT); #endif diff --git a/sys/arch/xen/xen/shutdown_xenbus.c b/sys/arch/xen/xen/shutdown_xenbus.c new file mode 100644 index 000000000000..4dc889ffa3ad --- /dev/null +++ b/sys/arch/xen/xen/shutdown_xenbus.c @@ -0,0 +1,155 @@ +/* $Id: shutdown_xenbus.c,v 1.1 2006/08/11 13:22:43 yamt Exp $ */ + +/*- + * Copyright (c)2006 YAMAMOTO Takashi, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Copyright (c) 2005 Manuel Bouyer. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Manuel Bouyer. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +/* + * watch "control/shutdown" and generate sysmon events. + */ + +#include +__KERNEL_RCSID(0, "$NetBSD: shutdown_xenbus.c,v 1.1 2006/08/11 13:22:43 yamt Exp $"); + +#include +#include + +#include + +#include +#include + +#define SHUTDOWN_PATH "control" +#define SHUTDOWN_NAME "shutdown" + +static struct sysmon_pswitch xenbus_power = { + .smpsw_type = PSWITCH_TYPE_POWER, + .smpsw_name = "xenbus", +}; +static struct sysmon_pswitch xenbus_reset = { + .smpsw_type = PSWITCH_TYPE_RESET, + .smpsw_name = "xenbus", +}; + +static void +xenbus_shutdown_handler(struct xenbus_watch *watch, const char **vec, + unsigned int len) +{ + + struct xenbus_transaction *xbt; + int error; + char *reqstr; + unsigned int reqstrlen; + +again: + xbt = xenbus_transaction_start(); + if (xbt == NULL) { + return; + } + error = xenbus_read(xbt, SHUTDOWN_PATH, SHUTDOWN_NAME, + &reqstrlen, &reqstr); + if (error) { + if (error != ENOENT) { + printf("%s: xenbus_read %d\n", __func__, error); + } + error = xenbus_transaction_end(xbt, 1); + if (error != 0) { + printf("%s: xenbus_transaction_end 1 %d\n", + __func__, error); + } + return; + } + KASSERT(strlen(reqstr) == reqstrlen); + error = xenbus_rm(xbt, SHUTDOWN_PATH, SHUTDOWN_NAME); + if (error) { + printf("%s: xenbus_rm %d\n", __func__, error); + } + error = xenbus_transaction_end(xbt, 0); + if (error == EAGAIN) { + free(reqstr, M_DEVBUF); + goto again; + } + if (error != 0) { + printf("%s: xenbus_transaction_end 2 %d\n", __func__, error); + } + if (strcmp(reqstr, "poweroff") == 0) { + sysmon_pswitch_event(&xenbus_power, PSWITCH_EVENT_PRESSED); + } else if (strcmp(reqstr, "reboot") == 0) { + sysmon_pswitch_event(&xenbus_reset, PSWITCH_EVENT_PRESSED); + } else { + /* XXX halt, suspend */ + printf("ignore shutdown request: %s\n", reqstr); + } + free(reqstr, M_DEVBUF); +} + +static struct xenbus_watch xenbus_shutdown_watch = { + .node = __UNCONST(SHUTDOWN_PATH "/" SHUTDOWN_NAME), /* XXX */ + .xbw_callback = xenbus_shutdown_handler, +}; + +void +shutdown_xenbus_setup(void) +{ + + if (sysmon_pswitch_register(&xenbus_power) != 0 || + sysmon_pswitch_register(&xenbus_reset) != 0) { + printf("%s: unable to register with sysmon\n", __func__); + return; + } + if (register_xenbus_watch(&xenbus_shutdown_watch)) { + printf("%s: unable to watch control/shutdown\n", __func__); + } +} diff --git a/sys/arch/xen/xenbus/xenbus_probe.c b/sys/arch/xen/xenbus/xenbus_probe.c index cfba892d06b1..4f0fb744d9f0 100644 --- a/sys/arch/xen/xenbus/xenbus_probe.c +++ b/sys/arch/xen/xenbus/xenbus_probe.c @@ -1,4 +1,4 @@ -/* $NetBSD: xenbus_probe.c,v 1.12 2006/06/25 16:46:59 bouyer Exp $ */ +/* $NetBSD: xenbus_probe.c,v 1.13 2006/08/11 13:22:43 yamt Exp $ */ /****************************************************************************** * Talks to Xen Store to figure out what devices we have. * @@ -29,7 +29,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.12 2006/06/25 16:46:59 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.13 2006/08/11 13:22:43 yamt Exp $"); #if 0 #define DPRINTK(fmt, args...) \ @@ -52,6 +52,7 @@ __KERNEL_RCSID(0, "$NetBSD: xenbus_probe.c,v 1.12 2006/06/25 16:46:59 bouyer Exp #include #include #include +#include #include "xenbus_comms.h" @@ -504,6 +505,7 @@ xenbus_probe(void *unused) strcpy(be_watch.node, "backend"); be_watch.xbw_callback = backend_changed; register_xenbus_watch(&be_watch); + shutdown_xenbus_setup(); /* Notify others that xenstore is up */ //notifier_call_chain(&xenstore_chain, 0, NULL);