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 /*--------------------------- Forward Declarations --------------------------*/ 149 /** Function signature for shutdown event handlers. */ 150 typedef void (xctrl_shutdown_handler_t)(void); 151 152 static xctrl_shutdown_handler_t xctrl_poweroff; 153 static xctrl_shutdown_handler_t xctrl_reboot; 154 static xctrl_shutdown_handler_t xctrl_suspend; 155 static xctrl_shutdown_handler_t xctrl_crash; 156 157 /*-------------------------- Private Data Structures -------------------------*/ 158 /** Element type for lookup table of event name to handler. */ 159 struct xctrl_shutdown_reason { 160 const char *name; 161 xctrl_shutdown_handler_t *handler; 162 }; 163 164 /** Lookup table for shutdown event name to handler. */ 165 static const struct xctrl_shutdown_reason xctrl_shutdown_reasons[] = { 166 { "poweroff", xctrl_poweroff }, 167 { "reboot", xctrl_reboot }, 168 { "suspend", xctrl_suspend }, 169 { "crash", xctrl_crash }, 170 { "halt", xctrl_poweroff }, 171 }; 172 173 struct xctrl_softc { 174 struct xs_watch xctrl_watch; 175 }; 176 177 /*------------------------------ Event Handlers ------------------------------*/ 178 static void 179 xctrl_poweroff() 180 { 181 shutdown_nice(RB_POWEROFF|RB_HALT); 182 } 183 184 static void 185 xctrl_reboot() 186 { 187 shutdown_nice(0); 188 } 189 190 static void 191 xctrl_suspend() 192 { 193 #ifdef SMP 194 cpuset_t cpu_suspend_map; 195 #endif 196 int suspend_cancelled; 197 198 EVENTHANDLER_INVOKE(power_suspend); 199 200 if (smp_started) { 201 thread_lock(curthread); 202 sched_bind(curthread, 0); 203 thread_unlock(curthread); 204 } 205 KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0")); 206 207 /* 208 * Clear our XenStore node so the toolstack knows we are 209 * responding to the suspend request. 210 */ 211 xs_write(XST_NIL, "control", "shutdown", ""); 212 213 /* 214 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 215 * drivers need this. 216 */ 217 mtx_lock(&Giant); 218 if (DEVICE_SUSPEND(root_bus) != 0) { 219 mtx_unlock(&Giant); 220 printf("%s: device_suspend failed\n", __func__); 221 return; 222 } 223 mtx_unlock(&Giant); 224 225 #ifdef SMP 226 CPU_ZERO(&cpu_suspend_map); /* silence gcc */ 227 if (smp_started) { 228 /* 229 * Suspend other CPUs. This prevents IPIs while we 230 * are resuming, and will allow us to reset per-cpu 231 * vcpu_info on resume. 232 */ 233 cpu_suspend_map = all_cpus; 234 CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map); 235 if (!CPU_EMPTY(&cpu_suspend_map)) 236 suspend_cpus(cpu_suspend_map); 237 } 238 #endif 239 240 /* 241 * Prevent any races with evtchn_interrupt() handler. 242 */ 243 disable_intr(); 244 intr_suspend(); 245 xen_hvm_suspend(); 246 247 suspend_cancelled = HYPERVISOR_suspend(0); 248 249 xen_hvm_resume(suspend_cancelled != 0); 250 intr_resume(suspend_cancelled != 0); 251 enable_intr(); 252 253 /* 254 * Reset grant table info. 255 */ 256 gnttab_resume(NULL); 257 258 #ifdef SMP 259 if (smp_started && !CPU_EMPTY(&cpu_suspend_map)) { 260 /* 261 * Now that event channels have been initialized, 262 * resume CPUs. 263 */ 264 resume_cpus(cpu_suspend_map); 265 } 266 #endif 267 268 /* 269 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or 270 * similar. 271 */ 272 mtx_lock(&Giant); 273 DEVICE_RESUME(root_bus); 274 mtx_unlock(&Giant); 275 276 if (smp_started) { 277 thread_lock(curthread); 278 sched_unbind(curthread); 279 thread_unlock(curthread); 280 } 281 282 EVENTHANDLER_INVOKE(power_resume); 283 284 if (bootverbose) 285 printf("System resumed after suspension\n"); 286 287 } 288 289 static void 290 xctrl_crash() 291 { 292 panic("Xen directed crash"); 293 } 294 295 static void 296 xen_pv_shutdown_final(void *arg, int howto) 297 { 298 /* 299 * Inform the hypervisor that shutdown is complete. 300 * This is not necessary in HVM domains since Xen 301 * emulates ACPI in that mode and FreeBSD's ACPI 302 * support will request this transition. 303 */ 304 if (howto & (RB_HALT | RB_POWEROFF)) 305 HYPERVISOR_shutdown(SHUTDOWN_poweroff); 306 else 307 HYPERVISOR_shutdown(SHUTDOWN_reboot); 308 } 309 310 /*------------------------------ Event Reception -----------------------------*/ 311 static void 312 xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len) 313 { 314 const struct xctrl_shutdown_reason *reason; 315 const struct xctrl_shutdown_reason *last_reason; 316 char *result; 317 int error; 318 int result_len; 319 320 error = xs_read(XST_NIL, "control", "shutdown", 321 &result_len, (void **)&result); 322 if (error != 0) 323 return; 324 325 reason = xctrl_shutdown_reasons; 326 last_reason = reason + nitems(xctrl_shutdown_reasons); 327 while (reason < last_reason) { 328 329 if (!strcmp(result, reason->name)) { 330 reason->handler(); 331 break; 332 } 333 reason++; 334 } 335 336 free(result, M_XENSTORE); 337 } 338 339 /*------------------ Private Device Attachment Functions --------------------*/ 340 /** 341 * \brief Identify instances of this device type in the system. 342 * 343 * \param driver The driver performing this identify action. 344 * \param parent The NewBus parent device for any devices this method adds. 345 */ 346 static void 347 xctrl_identify(driver_t *driver __unused, device_t parent) 348 { 349 /* 350 * A single device instance for our driver is always present 351 * in a system operating under Xen. 352 */ 353 BUS_ADD_CHILD(parent, 0, driver->name, 0); 354 } 355 356 /** 357 * \brief Probe for the existance of the Xen Control device 358 * 359 * \param dev NewBus device_t for this Xen control instance. 360 * 361 * \return Always returns 0 indicating success. 362 */ 363 static int 364 xctrl_probe(device_t dev) 365 { 366 device_set_desc(dev, "Xen Control Device"); 367 368 return (0); 369 } 370 371 /** 372 * \brief Attach the Xen control device. 373 * 374 * \param dev NewBus device_t for this Xen control instance. 375 * 376 * \return On success, 0. Otherwise an errno value indicating the 377 * type of failure. 378 */ 379 static int 380 xctrl_attach(device_t dev) 381 { 382 struct xctrl_softc *xctrl; 383 384 xctrl = device_get_softc(dev); 385 386 /* Activate watch */ 387 xctrl->xctrl_watch.node = "control/shutdown"; 388 xctrl->xctrl_watch.callback = xctrl_on_watch_event; 389 xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl; 390 xs_register_watch(&xctrl->xctrl_watch); 391 392 if (xen_pv_domain()) 393 EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL, 394 SHUTDOWN_PRI_LAST); 395 396 return (0); 397 } 398 399 /** 400 * \brief Detach the Xen control device. 401 * 402 * \param dev NewBus device_t for this Xen control device instance. 403 * 404 * \return On success, 0. Otherwise an errno value indicating the 405 * type of failure. 406 */ 407 static int 408 xctrl_detach(device_t dev) 409 { 410 struct xctrl_softc *xctrl; 411 412 xctrl = device_get_softc(dev); 413 414 /* Release watch */ 415 xs_unregister_watch(&xctrl->xctrl_watch); 416 417 return (0); 418 } 419 420 /*-------------------- Private Device Attachment Data -----------------------*/ 421 static device_method_t xctrl_methods[] = { 422 /* Device interface */ 423 DEVMETHOD(device_identify, xctrl_identify), 424 DEVMETHOD(device_probe, xctrl_probe), 425 DEVMETHOD(device_attach, xctrl_attach), 426 DEVMETHOD(device_detach, xctrl_detach), 427 428 DEVMETHOD_END 429 }; 430 431 DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc)); 432 devclass_t xctrl_devclass; 433 434 DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, NULL, NULL); 435