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