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 123 #ifndef XENHVM 124 #include <sys/sched.h> 125 #include <sys/smp.h> 126 #endif 127 128 #include <geom/geom.h> 129 130 #include <machine/_inttypes.h> 131 #include <machine/xen/xen-os.h> 132 133 #include <vm/vm.h> 134 #include <vm/vm_extern.h> 135 #include <vm/vm_kern.h> 136 137 #include <xen/blkif.h> 138 #include <xen/evtchn.h> 139 #include <xen/gnttab.h> 140 #include <xen/xen_intr.h> 141 142 #include <xen/interface/event_channel.h> 143 #include <xen/interface/grant_table.h> 144 145 #include <xen/xenbus/xenbusvar.h> 146 147 /*--------------------------- Forward Declarations --------------------------*/ 148 /** Function signature for shutdown event handlers. */ 149 typedef void (xctrl_shutdown_handler_t)(void); 150 151 static xctrl_shutdown_handler_t xctrl_poweroff; 152 static xctrl_shutdown_handler_t xctrl_reboot; 153 static xctrl_shutdown_handler_t xctrl_suspend; 154 static xctrl_shutdown_handler_t xctrl_crash; 155 static xctrl_shutdown_handler_t xctrl_halt; 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_halt }, 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 #ifndef XENHVM 191 extern void xencons_suspend(void); 192 extern void xencons_resume(void); 193 194 /* Full PV mode suspension. */ 195 static void 196 xctrl_suspend() 197 { 198 int i, j, k, fpp; 199 unsigned long max_pfn, start_info_mfn; 200 201 EVENTHANDLER_INVOKE(power_suspend); 202 203 #ifdef SMP 204 struct thread *td; 205 cpuset_t map; 206 u_int cpuid; 207 208 /* 209 * Bind us to CPU 0 and stop any other VCPUs. 210 */ 211 td = curthread; 212 thread_lock(td); 213 sched_bind(td, 0); 214 thread_unlock(td); 215 cpuid = PCPU_GET(cpuid); 216 KASSERT(cpuid == 0, ("xen_suspend: not running on cpu 0")); 217 218 map = all_cpus; 219 CPU_CLR(cpuid, &map); 220 CPU_NAND(&map, &stopped_cpus); 221 if (!CPU_EMPTY(&map)) 222 stop_cpus(map); 223 #endif 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 #ifdef SMP 234 if (!CPU_EMPTY(&map)) 235 restart_cpus(map); 236 #endif 237 return; 238 } 239 mtx_unlock(&Giant); 240 241 local_irq_disable(); 242 243 xencons_suspend(); 244 gnttab_suspend(); 245 246 max_pfn = HYPERVISOR_shared_info->arch.max_pfn; 247 248 void *shared_info = HYPERVISOR_shared_info; 249 HYPERVISOR_shared_info = NULL; 250 pmap_kremove((vm_offset_t) shared_info); 251 PT_UPDATES_FLUSH(); 252 253 xen_start_info->store_mfn = MFNTOPFN(xen_start_info->store_mfn); 254 xen_start_info->console.domU.mfn = MFNTOPFN(xen_start_info->console.domU.mfn); 255 256 /* 257 * We'll stop somewhere inside this hypercall. When it returns, 258 * we'll start resuming after the restore. 259 */ 260 start_info_mfn = VTOMFN(xen_start_info); 261 pmap_suspend(); 262 HYPERVISOR_suspend(start_info_mfn); 263 pmap_resume(); 264 265 pmap_kenter_ma((vm_offset_t) shared_info, xen_start_info->shared_info); 266 HYPERVISOR_shared_info = shared_info; 267 268 HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list = 269 VTOMFN(xen_pfn_to_mfn_frame_list_list); 270 271 fpp = PAGE_SIZE/sizeof(unsigned long); 272 for (i = 0, j = 0, k = -1; i < max_pfn; i += fpp, j++) { 273 if ((j % fpp) == 0) { 274 k++; 275 xen_pfn_to_mfn_frame_list_list[k] = 276 VTOMFN(xen_pfn_to_mfn_frame_list[k]); 277 j = 0; 278 } 279 xen_pfn_to_mfn_frame_list[k][j] = 280 VTOMFN(&xen_phys_machine[i]); 281 } 282 HYPERVISOR_shared_info->arch.max_pfn = max_pfn; 283 284 gnttab_resume(); 285 irq_resume(); 286 local_irq_enable(); 287 xencons_resume(); 288 289 #ifdef CONFIG_SMP 290 for_each_cpu(i) 291 vcpu_prepare(i); 292 293 #endif 294 295 /* 296 * Only resume xenbus /after/ we've prepared our VCPUs; otherwise 297 * the VCPU hotplug callback can race with our vcpu_prepare 298 */ 299 mtx_lock(&Giant); 300 DEVICE_RESUME(root_bus); 301 mtx_unlock(&Giant); 302 303 #ifdef SMP 304 thread_lock(curthread); 305 sched_unbind(curthread); 306 thread_unlock(curthread); 307 if (!CPU_EMPTY(&map)) 308 restart_cpus(map); 309 #endif 310 EVENTHANDLER_INVOKE(power_resume); 311 } 312 313 static void 314 xen_pv_shutdown_final(void *arg, int howto) 315 { 316 /* 317 * Inform the hypervisor that shutdown is complete. 318 * This is not necessary in HVM domains since Xen 319 * emulates ACPI in that mode and FreeBSD's ACPI 320 * support will request this transition. 321 */ 322 if (howto & (RB_HALT | RB_POWEROFF)) 323 HYPERVISOR_shutdown(SHUTDOWN_poweroff); 324 else 325 HYPERVISOR_shutdown(SHUTDOWN_reboot); 326 } 327 328 #else 329 extern void xenpci_resume(void); 330 331 /* HVM mode suspension. */ 332 static void 333 xctrl_suspend() 334 { 335 int suspend_cancelled; 336 337 EVENTHANDLER_INVOKE(power_suspend); 338 339 /* 340 * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 341 * drivers need this. 342 */ 343 mtx_lock(&Giant); 344 if (DEVICE_SUSPEND(root_bus) != 0) { 345 mtx_unlock(&Giant); 346 printf("%s: device_suspend failed\n", __func__); 347 return; 348 } 349 mtx_unlock(&Giant); 350 351 /* 352 * Prevent any races with evtchn_interrupt() handler. 353 */ 354 disable_intr(); 355 irq_suspend(); 356 357 suspend_cancelled = HYPERVISOR_suspend(0); 358 if (suspend_cancelled) 359 irq_resume(); 360 else 361 xenpci_resume(); 362 363 /* 364 * Re-enable interrupts and put the scheduler back to normal. 365 */ 366 enable_intr(); 367 368 /* 369 * FreeBSD really needs to add DEVICE_SUSPEND_CANCEL or 370 * similar. 371 */ 372 mtx_lock(&Giant); 373 if (!suspend_cancelled) 374 DEVICE_RESUME(root_bus); 375 mtx_unlock(&Giant); 376 377 EVENTHANDLER_INVOKE(power_resume); 378 } 379 #endif 380 381 static void 382 xctrl_crash() 383 { 384 panic("Xen directed crash"); 385 } 386 387 static void 388 xctrl_halt() 389 { 390 shutdown_nice(RB_HALT); 391 } 392 393 /*------------------------------ Event Reception -----------------------------*/ 394 static void 395 xctrl_on_watch_event(struct xs_watch *watch, const char **vec, unsigned int len) 396 { 397 const struct xctrl_shutdown_reason *reason; 398 const struct xctrl_shutdown_reason *last_reason; 399 char *result; 400 int error; 401 int result_len; 402 403 error = xs_read(XST_NIL, "control", "shutdown", 404 &result_len, (void **)&result); 405 if (error != 0) 406 return; 407 408 reason = xctrl_shutdown_reasons; 409 last_reason = reason + nitems(xctrl_shutdown_reasons); 410 while (reason < last_reason) { 411 412 if (!strcmp(result, reason->name)) { 413 reason->handler(); 414 break; 415 } 416 reason++; 417 } 418 419 free(result, M_XENSTORE); 420 } 421 422 /*------------------ Private Device Attachment Functions --------------------*/ 423 /** 424 * \brief Identify instances of this device type in the system. 425 * 426 * \param driver The driver performing this identify action. 427 * \param parent The NewBus parent device for any devices this method adds. 428 */ 429 static void 430 xctrl_identify(driver_t *driver __unused, device_t parent) 431 { 432 /* 433 * A single device instance for our driver is always present 434 * in a system operating under Xen. 435 */ 436 BUS_ADD_CHILD(parent, 0, driver->name, 0); 437 } 438 439 /** 440 * \brief Probe for the existance of the Xen Control device 441 * 442 * \param dev NewBus device_t for this Xen control instance. 443 * 444 * \return Always returns 0 indicating success. 445 */ 446 static int 447 xctrl_probe(device_t dev) 448 { 449 device_set_desc(dev, "Xen Control Device"); 450 451 return (0); 452 } 453 454 /** 455 * \brief Attach the Xen control device. 456 * 457 * \param dev NewBus device_t for this Xen control instance. 458 * 459 * \return On success, 0. Otherwise an errno value indicating the 460 * type of failure. 461 */ 462 static int 463 xctrl_attach(device_t dev) 464 { 465 struct xctrl_softc *xctrl; 466 467 xctrl = device_get_softc(dev); 468 469 /* Activate watch */ 470 xctrl->xctrl_watch.node = "control/shutdown"; 471 xctrl->xctrl_watch.callback = xctrl_on_watch_event; 472 xctrl->xctrl_watch.callback_data = (uintptr_t)xctrl; 473 xs_register_watch(&xctrl->xctrl_watch); 474 475 #ifndef XENHVM 476 EVENTHANDLER_REGISTER(shutdown_final, xen_pv_shutdown_final, NULL, 477 SHUTDOWN_PRI_LAST); 478 #endif 479 480 return (0); 481 } 482 483 /** 484 * \brief Detach the Xen control device. 485 * 486 * \param dev NewBus device_t for this Xen control device instance. 487 * 488 * \return On success, 0. Otherwise an errno value indicating the 489 * type of failure. 490 */ 491 static int 492 xctrl_detach(device_t dev) 493 { 494 struct xctrl_softc *xctrl; 495 496 xctrl = device_get_softc(dev); 497 498 /* Release watch */ 499 xs_unregister_watch(&xctrl->xctrl_watch); 500 501 return (0); 502 } 503 504 /*-------------------- Private Device Attachment Data -----------------------*/ 505 static device_method_t xctrl_methods[] = { 506 /* Device interface */ 507 DEVMETHOD(device_identify, xctrl_identify), 508 DEVMETHOD(device_probe, xctrl_probe), 509 DEVMETHOD(device_attach, xctrl_attach), 510 DEVMETHOD(device_detach, xctrl_detach), 511 512 DEVMETHOD_END 513 }; 514 515 DEFINE_CLASS_0(xctrl, xctrl_driver, xctrl_methods, sizeof(struct xctrl_softc)); 516 devclass_t xctrl_devclass; 517 518 DRIVER_MODULE(xctrl, xenstore, xctrl_driver, xctrl_devclass, NULL, NULL); 519