1 /*- 2 * Copyright (c) 2010 Justin T. Gibbs, Spectra Logic Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions, and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * substantially similar to the "NO WARRANTY" disclaimer below 13 * ("Disclaimer") and any redistribution must be conditioned upon 14 * including a substantially similar Disclaimer requirement for further 15 * binary redistribution. 16 * 17 * NO WARRANTY 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGES. 29 */ 30 31 /*- 32 * PV suspend/resume support: 33 * 34 * Copyright (c) 2004 Christian Limpach. 35 * Copyright (c) 2004-2006,2008 Kip Macy 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by Christian Limpach. 49 * 4. The name of the author may not be used to endorse or promote products 50 * derived from this software without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 */ 63 64 /*- 65 * HVM suspend/resume support: 66 * 67 * Copyright (c) 2008 Citrix Systems, Inc. 68 * All rights reserved. 69 * 70 * Redistribution and use in source and binary forms, with or without 71 * modification, are permitted provided that the following conditions 72 * are met: 73 * 1. Redistributions of source code must retain the above copyright 74 * notice, this list of conditions and the following disclaimer. 75 * 2. Redistributions in binary form must reproduce the above copyright 76 * notice, this list of conditions and the following disclaimer in the 77 * documentation and/or other materials provided with the distribution. 78 * 79 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 82 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 89 * SUCH DAMAGE. 90 */ 91 #include <sys/cdefs.h> 92 __FBSDID("$FreeBSD$"); 93 94 /** 95 * \file control.c 96 * 97 * \brief Device driver to repond to control domain events that impact 98 * this VM. 99 */ 100 101 #include <sys/param.h> 102 #include <sys/systm.h> 103 #include <sys/kernel.h> 104 #include <sys/malloc.h> 105 106 #include <sys/bio.h> 107 #include <sys/bus.h> 108 #include <sys/conf.h> 109 #include <sys/disk.h> 110 #include <sys/fcntl.h> 111 #include <sys/filedesc.h> 112 #include <sys/kdb.h> 113 #include <sys/module.h> 114 #include <sys/namei.h> 115 #include <sys/proc.h> 116 #include <sys/reboot.h> 117 #include <sys/rman.h> 118 #include <sys/sched.h> 119 #include <sys/taskqueue.h> 120 #include <sys/types.h> 121 #include <sys/vnode.h> 122 #include <sys/sched.h> 123 #include <sys/smp.h> 124 #include <sys/eventhandler.h> 125 126 #include <geom/geom.h> 127 128 #include <machine/_inttypes.h> 129 #include <machine/intr_machdep.h> 130 131 #include <vm/vm.h> 132 #include <vm/vm_extern.h> 133 #include <vm/vm_kern.h> 134 135 #include <xen/xen-os.h> 136 #include <xen/blkif.h> 137 #include <xen/evtchn.h> 138 #include <xen/gnttab.h> 139 #include <xen/xen_intr.h> 140 141 #include <xen/hvm.h> 142 143 #include <xen/interface/event_channel.h> 144 #include <xen/interface/grant_table.h> 145 146 #include <xen/xenbus/xenbusvar.h> 147 148 #include <machine/xen/xenvar.h> 149 #include <machine/xen/xenfunc.h> 150 151 /*--------------------------- Forward Declarations --------------------------*/ 152 /** Function signature for shutdown event handlers. */ 153 typedef void (xctrl_shutdown_handler_t)(void); 154 155 static xctrl_shutdown_handler_t xctrl_poweroff; 156 static xctrl_shutdown_handler_t xctrl_reboot; 157 static xctrl_shutdown_handler_t xctrl_suspend; 158 static xctrl_shutdown_handler_t xctrl_crash; 159 160 /*-------------------------- Private Data Structures -------------------------*/ 161 /** Element type for lookup table of event name to handler. */ 162 struct xctrl_shutdown_reason { 163 const char *name; 164 xctrl_shutdown_handler_t *handler; 165 }; 166 167 /** Lookup table for shutdown event name to handler. */ 168 static const struct xctrl_shutdown_reason xctrl_shutdown_reasons[] = { 169 { "poweroff", xctrl_poweroff }, 170 { "reboot", xctrl_reboot }, 171 { "suspend", xctrl_suspend }, 172 { "crash", xctrl_crash }, 173 { "halt", xctrl_poweroff }, 174 }; 175 176 struct xctrl_softc { 177 struct xs_watch xctrl_watch; 178 }; 179 180 /*------------------------------ Event Handlers ------------------------------*/ 181 static void 182 xctrl_poweroff() 183 { 184 shutdown_nice(RB_POWEROFF|RB_HALT); 185 } 186 187 static void 188 xctrl_reboot() 189 { 190 shutdown_nice(0); 191 } 192 193 static void 194 xctrl_suspend() 195 { 196 #ifdef SMP 197 cpuset_t cpu_suspend_map; 198 #endif 199 int suspend_cancelled; 200 201 EVENTHANDLER_INVOKE(power_suspend); 202 203 if (smp_started) { 204 thread_lock(curthread); 205 sched_bind(curthread, 0); 206 thread_unlock(curthread); 207 } 208 KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0")); 209 210 /* 211 * Clear our XenStore node so the toolstack knows we are 212 * responding to the suspend request. 213 */ 214 xs_write(XST_NIL, "control", "shutdown", ""); 215 216 /* 217 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 218 * drivers need this. 219 */ 220 mtx_lock(&Giant); 221 if (DEVICE_SUSPEND(root_bus) != 0) { 222 mtx_unlock(&Giant); 223 printf("%s: device_suspend failed\n", __func__); 224 return; 225 } 226 mtx_unlock(&Giant); 227 228 #ifdef SMP 229 CPU_ZERO(&cpu_suspend_map); /* silence gcc */ 230 if (smp_started) { 231 /* 232 * Suspend other CPUs. This prevents IPIs while we 233 * are resuming, and will allow us to reset per-cpu 234 * vcpu_info on resume. 235 */ 236 cpu_suspend_map = all_cpus; 237 CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map); 238 if (!CPU_EMPTY(&cpu_suspend_map)) 239 suspend_cpus(cpu_suspend_map); 240 } 241 #endif 242 243 /* 244 * Prevent any races with evtchn_interrupt() handler. 245 */ 246 disable_intr(); 247 intr_suspend(); 248 xen_hvm_suspend(); 249 250 suspend_cancelled = HYPERVISOR_suspend(0); 251 252 xen_hvm_resume(suspend_cancelled != 0); 253 intr_resume(suspend_cancelled != 0); 254 enable_intr(); 255 256 /* 257 * Reset grant table info. 258 */ 259 gnttab_resume(NULL); 260 261 #ifdef SMP 262 if (smp_started && !CPU_EMPTY(&cpu_suspend_map)) { 263 /* 264 * Now that event channels have been initialized, 265 * resume CPUs. 266 */ 267 resume_cpus(cpu_suspend_map); 268 } 269 #endif 270 271 /* 272 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or 273 * similar. 274 */ 275 mtx_lock(&Giant); 276 DEVICE_RESUME(root_bus); 277 mtx_unlock(&Giant); 278 279 if (smp_started) { 280 thread_lock(curthread); 281 sched_unbind(curthread); 282 thread_unlock(curthread); 283 } 284 285 EVENTHANDLER_INVOKE(power_resume); 286 287 if (bootverbose) 288 printf("System resumed after suspension\n"); 289 290 } 291 292 static void 293 xctrl_crash() 294 { 295 panic("Xen directed crash"); 296 } 297 298 static void 299 xen_pv_shutdown_final(void *arg, int howto) 300 { 301 /* 302 * Inform the hypervisor that shutdown is complete. 303 * This is not necessary in HVM domains since Xen 304 * emulates ACPI in that mode and FreeBSD's ACPI 305 * support will request this transition. 306 */ 307 if (howto & (RB_HALT | RB_POWEROFF)) 308 HYPERVISOR_shutdown(SHUTDOWN_poweroff); 309 else 310 HYPERVISOR_shutdown(SHUTDOWN_reboot); 311 } 312 313 /*------------------------------ Event Reception -----------------------------*/ 314 static void 315 xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len) 316 { 317 const struct xctrl_shutdown_reason *reason; 318 const struct xctrl_shutdown_reason *last_reason; 319 char *result; 320 int error; 321 int result_len; 322 323 error = xs_read(XST_NIL, "control", "shutdown", 324 &result_len, (void **)&result); 325 if (error != 0) 326 return; 327 328 reason = xctrl_shutdown_reasons; 329 last_reason = reason + nitems(xctrl_shutdown_reasons); 330 while (reason < last_reason) { 331 332 if (!strcmp(result, reason->name)) { 333 reason->handler(); 334 break; 335 } 336 reason++; 337 } 338 339 free(result, M_XENSTORE); 340 } 341 342 /*------------------ Private Device Attachment Functions --------------------*/ 343 /** 344 * \brief Identify instances of this device type in the system. 345 * 346 * \param driver The driver performing this identify action. 347 * \param parent The NewBus parent device for any devices this method adds. 348 */ 349 static void 350 xctrl_identify(driver_t *driver __unused, device_t parent) 351 { 352 /* 353 * A single device instance for our driver is always present 354 * in a system operating under Xen. 355 */ 356 BUS_ADD_CHILD(parent, 0, driver->name, 0); 357 } 358 359 /** 360 * \brief Probe for the existance of the Xen Control device 361 * 362 * \param dev NewBus device_t for this Xen control instance. 363 * 364 * \return Always returns 0 indicating success. 365 */ 366 static int 367 xctrl_probe(device_t dev) 368 { 369 device_set_desc(dev, "Xen Control Device"); 370 371 return (0); 372 } 373 374 /** 375 * \brief Attach the Xen control device. 376 * 377 * \param dev NewBus device_t for this Xen control instance. 378 * 379 * \return On success, 0. Otherwise an errno value indicating the 380 * type of failure. 381 */ 382 static int 383 xctrl_attach(device_t dev) 384 { 385 struct xctrl_softc *xctrl; 386 387 xctrl = device_get_softc(dev); 388 389 /* Activate watch */ 390 xctrl->xctrl_watch.node = "control/shutdown"; 391 xctrl->xctrl_watch.callback = xctrl_on_watch_event; 392 xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl; 393 xs_register_watch(&xctrl->xctrl_watch); 394 395 if (xen_pv_domain()) 396 EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL, 397 SHUTDOWN_PRI_LAST); 398 399 return (0); 400 } 401 402 /** 403 * \brief Detach the Xen control device. 404 * 405 * \param dev NewBus device_t for this Xen control device instance. 406 * 407 * \return On success, 0. Otherwise an errno value indicating the 408 * type of failure. 409 */ 410 static int 411 xctrl_detach(device_t dev) 412 { 413 struct xctrl_softc *xctrl; 414 415 xctrl = device_get_softc(dev); 416 417 /* Release watch */ 418 xs_unregister_watch(&xctrl->xctrl_watch); 419 420 return (0); 421 } 422 423 /*-------------------- Private Device Attachment Data -----------------------*/ 424 static device_method_t xctrl_methods[] = { 425 /* Device interface */ 426 DEVMETHOD(device_identify, xctrl_identify), 427 DEVMETHOD(device_probe, xctrl_probe), 428 DEVMETHOD(device_attach, xctrl_attach), 429 DEVMETHOD(device_detach, xctrl_detach), 430 431 DEVMETHOD_END 432 }; 433 434 DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc)); 435 devclass_t xctrl_devclass; 436 437 DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, NULL, NULL); 438