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