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 #include <sys/timetc.h> 126 127 #include <geom/geom.h> 128 129 #include <machine/_inttypes.h> 130 #include <machine/intr_machdep.h> 131 132 #include <x86/apicvar.h> 133 134 #include <vm/vm.h> 135 #include <vm/vm_extern.h> 136 #include <vm/vm_kern.h> 137 138 #include <xen/xen-os.h> 139 #include <xen/blkif.h> 140 #include <xen/evtchn.h> 141 #include <xen/gnttab.h> 142 #include <xen/xen_intr.h> 143 144 #include <xen/hvm.h> 145 146 #include <xen/interface/event_channel.h> 147 #include <xen/interface/grant_table.h> 148 149 #include <xen/xenbus/xenbusvar.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_early); 202 stop_all_proc(); 203 EVENTHANDLER_INVOKE(power_suspend); 204 205 #ifdef EARLY_AP_STARTUP 206 MPASS(mp_ncpus == 1 || smp_started); 207 thread_lock(curthread); 208 sched_bind(curthread, 0); 209 thread_unlock(curthread); 210 #else 211 if (smp_started) { 212 thread_lock(curthread); 213 sched_bind(curthread, 0); 214 thread_unlock(curthread); 215 } 216 #endif 217 KASSERT((PCPU_GET(cpuid) == 0), ("Not running on CPU#0")); 218 219 /* 220 * Clear our XenStore node so the toolstack knows we are 221 * responding to the suspend request. 222 */ 223 xs_write(XST_NIL, "control", "shutdown", ""); 224 225 /* 226 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 227 * drivers need this. 228 */ 229 mtx_lock(&Giant); 230 if (DEVICE_SUSPEND(root_bus) != 0) { 231 mtx_unlock(&Giant); 232 printf("%s: device_suspend failed\n", __func__); 233 return; 234 } 235 236 #ifdef SMP 237 #ifdef EARLY_AP_STARTUP 238 /* 239 * Suspend other CPUs. This prevents IPIs while we 240 * are resuming, and will allow us to reset per-cpu 241 * vcpu_info on resume. 242 */ 243 cpu_suspend_map = all_cpus; 244 CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map); 245 if (!CPU_EMPTY(&cpu_suspend_map)) 246 suspend_cpus(cpu_suspend_map); 247 #else 248 CPU_ZERO(&cpu_suspend_map); /* silence gcc */ 249 if (smp_started) { 250 /* 251 * Suspend other CPUs. This prevents IPIs while we 252 * are resuming, and will allow us to reset per-cpu 253 * vcpu_info on resume. 254 */ 255 cpu_suspend_map = all_cpus; 256 CPU_CLR(PCPU_GET(cpuid), &cpu_suspend_map); 257 if (!CPU_EMPTY(&cpu_suspend_map)) 258 suspend_cpus(cpu_suspend_map); 259 } 260 #endif 261 #endif 262 263 /* 264 * Prevent any races with evtchn_interrupt() handler. 265 */ 266 disable_intr(); 267 intr_suspend(); 268 xen_hvm_suspend(); 269 270 suspend_cancelled = HYPERVISOR_suspend(0); 271 272 xen_hvm_resume(suspend_cancelled != 0); 273 intr_resume(suspend_cancelled != 0); 274 enable_intr(); 275 276 /* 277 * Reset grant table info. 278 */ 279 gnttab_resume(NULL); 280 281 #ifdef SMP 282 if (!CPU_EMPTY(&cpu_suspend_map)) { 283 /* 284 * Now that event channels have been initialized, 285 * resume CPUs. 286 */ 287 resume_cpus(cpu_suspend_map); 288 /* Send an IPI_BITMAP in case there are pending bitmap IPIs. */ 289 lapic_ipi_vectored(IPI_BITMAP_VECTOR, APIC_IPI_DEST_ALL); 290 } 291 #endif 292 293 /* 294 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or 295 * similar. 296 */ 297 DEVICE_RESUME(root_bus); 298 mtx_unlock(&Giant); 299 300 /* 301 * Warm up timecounter again and reset system clock. 302 */ 303 timecounter->tc_get_timecount(timecounter); 304 timecounter->tc_get_timecount(timecounter); 305 inittodr(time_second); 306 307 #ifdef EARLY_AP_STARTUP 308 thread_lock(curthread); 309 sched_unbind(curthread); 310 thread_unlock(curthread); 311 #else 312 if (smp_started) { 313 thread_lock(curthread); 314 sched_unbind(curthread); 315 thread_unlock(curthread); 316 } 317 #endif 318 319 resume_all_proc(); 320 321 EVENTHANDLER_INVOKE(power_resume); 322 323 if (bootverbose) 324 printf("System resumed after suspension\n"); 325 326 } 327 328 static void 329 xctrl_crash() 330 { 331 panic("Xen directed crash"); 332 } 333 334 static void 335 xen_pv_shutdown_final(void *arg, int howto) 336 { 337 /* 338 * Inform the hypervisor that shutdown is complete. 339 * This is not necessary in HVM domains since Xen 340 * emulates ACPI in that mode and FreeBSD's ACPI 341 * support will request this transition. 342 */ 343 if (howto & (RB_HALT | RB_POWEROFF)) 344 HYPERVISOR_shutdown(SHUTDOWN_poweroff); 345 else 346 HYPERVISOR_shutdown(SHUTDOWN_reboot); 347 } 348 349 /*------------------------------ Event Reception -----------------------------*/ 350 static void 351 xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len) 352 { 353 const struct xctrl_shutdown_reason *reason; 354 const struct xctrl_shutdown_reason *last_reason; 355 char *result; 356 int error; 357 int result_len; 358 359 error = xs_read(XST_NIL, "control", "shutdown", 360 &result_len, (void **)&result); 361 if (error != 0) 362 return; 363 364 reason = xctrl_shutdown_reasons; 365 last_reason = reason + nitems(xctrl_shutdown_reasons); 366 while (reason < last_reason) { 367 368 if (!strcmp(result, reason->name)) { 369 reason->handler(); 370 break; 371 } 372 reason++; 373 } 374 375 free(result, M_XENSTORE); 376 } 377 378 /*------------------ Private Device Attachment Functions --------------------*/ 379 /** 380 * \brief Identify instances of this device type in the system. 381 * 382 * \param driver The driver performing this identify action. 383 * \param parent The NewBus parent device for any devices this method adds. 384 */ 385 static void 386 xctrl_identify(driver_t *driver __unused, device_t parent) 387 { 388 /* 389 * A single device instance for our driver is always present 390 * in a system operating under Xen. 391 */ 392 BUS_ADD_CHILD(parent, 0, driver->name, 0); 393 } 394 395 /** 396 * \brief Probe for the existence of the Xen Control device 397 * 398 * \param dev NewBus device_t for this Xen control instance. 399 * 400 * \return Always returns 0 indicating success. 401 */ 402 static int 403 xctrl_probe(device_t dev) 404 { 405 device_set_desc(dev, "Xen Control Device"); 406 407 return (BUS_PROBE_NOWILDCARD); 408 } 409 410 /** 411 * \brief Attach the Xen control device. 412 * 413 * \param dev NewBus device_t for this Xen control instance. 414 * 415 * \return On success, 0. Otherwise an errno value indicating the 416 * type of failure. 417 */ 418 static int 419 xctrl_attach(device_t dev) 420 { 421 struct xctrl_softc *xctrl; 422 423 xctrl = device_get_softc(dev); 424 425 /* Activate watch */ 426 xctrl->xctrl_watch.node = "control/shutdown"; 427 xctrl->xctrl_watch.callback = xctrl_on_watch_event; 428 xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl; 429 xs_register_watch(&xctrl->xctrl_watch); 430 431 if (xen_pv_domain()) 432 EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL, 433 SHUTDOWN_PRI_LAST); 434 435 return (0); 436 } 437 438 /** 439 * \brief Detach the Xen control device. 440 * 441 * \param dev NewBus device_t for this Xen control device instance. 442 * 443 * \return On success, 0. Otherwise an errno value indicating the 444 * type of failure. 445 */ 446 static int 447 xctrl_detach(device_t dev) 448 { 449 struct xctrl_softc *xctrl; 450 451 xctrl = device_get_softc(dev); 452 453 /* Release watch */ 454 xs_unregister_watch(&xctrl->xctrl_watch); 455 456 return (0); 457 } 458 459 /*-------------------- Private Device Attachment Data -----------------------*/ 460 static device_method_t xctrl_methods[] = { 461 /* Device interface */ 462 DEVMETHOD(device_identify, xctrl_identify), 463 DEVMETHOD(device_probe, xctrl_probe), 464 DEVMETHOD(device_attach, xctrl_attach), 465 DEVMETHOD(device_detach, xctrl_detach), 466 467 DEVMETHOD_END 468 }; 469 470 DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc)); 471 devclass_t xctrl_devclass; 472 473 DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, NULL, NULL); 474