1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2011 NetApp, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/types.h> 30 #ifndef WITHOUT_CAPSICUM 31 #include <sys/capsicum.h> 32 #endif 33 #include <sys/mman.h> 34 #ifdef BHYVE_SNAPSHOT 35 #include <sys/socket.h> 36 #include <sys/stat.h> 37 #endif 38 #include <sys/time.h> 39 #ifdef BHYVE_SNAPSHOT 40 #include <sys/un.h> 41 #endif 42 43 #include <machine/atomic.h> 44 45 #ifndef WITHOUT_CAPSICUM 46 #include <capsicum_helpers.h> 47 #endif 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <err.h> 52 #include <errno.h> 53 #ifdef BHYVE_SNAPSHOT 54 #include <fcntl.h> 55 #endif 56 #include <libgen.h> 57 #include <unistd.h> 58 #include <assert.h> 59 #include <pthread.h> 60 #include <pthread_np.h> 61 #include <sysexits.h> 62 #include <stdbool.h> 63 #include <stdint.h> 64 #ifdef BHYVE_SNAPSHOT 65 #include <ucl.h> 66 #include <unistd.h> 67 68 #include <libxo/xo.h> 69 #endif 70 71 #include <vmmapi.h> 72 73 #include "acpi.h" 74 #include "bhyverun.h" 75 #include "bootrom.h" 76 #include "config.h" 77 #include "debug.h" 78 #ifdef BHYVE_GDB 79 #include "gdb.h" 80 #endif 81 #include "mem.h" 82 #include "mevent.h" 83 #include "pci_emul.h" 84 #ifdef __amd64__ 85 #include "amd64/pci_lpc.h" 86 #endif 87 #include "qemu_fwcfg.h" 88 #ifdef BHYVE_SNAPSHOT 89 #include "snapshot.h" 90 #endif 91 #include "tpm_device.h" 92 #include "vmgenc.h" 93 #include "vmexit.h" 94 95 #define MB (1024UL * 1024) 96 #define GB (1024UL * MB) 97 98 int guest_ncpus; 99 uint16_t cpu_cores, cpu_sockets, cpu_threads; 100 101 int raw_stdio = 0; 102 103 static const int BSP = 0; 104 105 static cpuset_t cpumask; 106 107 static void vm_loop(struct vmctx *ctx, struct vcpu *vcpu); 108 109 static struct vcpu_info { 110 struct vmctx *ctx; 111 struct vcpu *vcpu; 112 int vcpuid; 113 } *vcpu_info; 114 115 static cpuset_t **vcpumap; 116 117 /* 118 * XXX This parser is known to have the following issues: 119 * 1. It accepts null key=value tokens ",," as setting "cpus" to an 120 * empty string. 121 * 122 * The acceptance of a null specification ('-c ""') is by design to match the 123 * manual page syntax specification, this results in a topology of 1 vCPU. 124 */ 125 int 126 bhyve_topology_parse(const char *opt) 127 { 128 char *cp, *str, *tofree; 129 130 if (*opt == '\0') { 131 set_config_value("sockets", "1"); 132 set_config_value("cores", "1"); 133 set_config_value("threads", "1"); 134 set_config_value("cpus", "1"); 135 return (0); 136 } 137 138 tofree = str = strdup(opt); 139 if (str == NULL) 140 errx(4, "Failed to allocate memory"); 141 142 while ((cp = strsep(&str, ",")) != NULL) { 143 if (strncmp(cp, "cpus=", strlen("cpus=")) == 0) 144 set_config_value("cpus", cp + strlen("cpus=")); 145 else if (strncmp(cp, "sockets=", strlen("sockets=")) == 0) 146 set_config_value("sockets", cp + strlen("sockets=")); 147 else if (strncmp(cp, "cores=", strlen("cores=")) == 0) 148 set_config_value("cores", cp + strlen("cores=")); 149 else if (strncmp(cp, "threads=", strlen("threads=")) == 0) 150 set_config_value("threads", cp + strlen("threads=")); 151 else if (strchr(cp, '=') != NULL) 152 goto out; 153 else 154 set_config_value("cpus", cp); 155 } 156 free(tofree); 157 return (0); 158 159 out: 160 free(tofree); 161 return (-1); 162 } 163 164 static int 165 parse_int_value(const char *key, const char *value, int minval, int maxval) 166 { 167 char *cp; 168 long lval; 169 170 errno = 0; 171 lval = strtol(value, &cp, 0); 172 if (errno != 0 || *cp != '\0' || cp == value || lval < minval || 173 lval > maxval) 174 errx(4, "Invalid value for %s: '%s'", key, value); 175 return (lval); 176 } 177 178 /* 179 * Set the sockets, cores, threads, and guest_cpus variables based on 180 * the configured topology. 181 * 182 * The limits of UINT16_MAX are due to the types passed to 183 * vm_set_topology(). vmm.ko may enforce tighter limits. 184 */ 185 static void 186 calc_topology(void) 187 { 188 const char *value; 189 bool explicit_cpus; 190 uint64_t ncpus; 191 192 value = get_config_value("cpus"); 193 if (value != NULL) { 194 guest_ncpus = parse_int_value("cpus", value, 1, UINT16_MAX); 195 explicit_cpus = true; 196 } else { 197 guest_ncpus = 1; 198 explicit_cpus = false; 199 } 200 value = get_config_value("cores"); 201 if (value != NULL) 202 cpu_cores = parse_int_value("cores", value, 1, UINT16_MAX); 203 else 204 cpu_cores = 1; 205 value = get_config_value("threads"); 206 if (value != NULL) 207 cpu_threads = parse_int_value("threads", value, 1, UINT16_MAX); 208 else 209 cpu_threads = 1; 210 value = get_config_value("sockets"); 211 if (value != NULL) 212 cpu_sockets = parse_int_value("sockets", value, 1, UINT16_MAX); 213 else 214 cpu_sockets = guest_ncpus; 215 216 /* 217 * Compute sockets * cores * threads avoiding overflow. The 218 * range check above insures these are 16 bit values. 219 */ 220 ncpus = (uint64_t)cpu_sockets * cpu_cores * cpu_threads; 221 if (ncpus > UINT16_MAX) 222 errx(4, "Computed number of vCPUs too high: %ju", 223 (uintmax_t)ncpus); 224 225 if (explicit_cpus) { 226 if (guest_ncpus != (int)ncpus) 227 errx(4, "Topology (%d sockets, %d cores, %d threads) " 228 "does not match %d vCPUs", 229 cpu_sockets, cpu_cores, cpu_threads, 230 guest_ncpus); 231 } else 232 guest_ncpus = ncpus; 233 } 234 235 int 236 bhyve_pincpu_parse(const char *opt) 237 { 238 const char *value; 239 char *newval; 240 char key[16]; 241 int vcpu, pcpu; 242 243 if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) { 244 fprintf(stderr, "invalid format: %s\n", opt); 245 return (-1); 246 } 247 248 if (vcpu < 0) { 249 fprintf(stderr, "invalid vcpu '%d'\n", vcpu); 250 return (-1); 251 } 252 253 if (pcpu < 0 || pcpu >= CPU_SETSIZE) { 254 fprintf(stderr, "hostcpu '%d' outside valid range from " 255 "0 to %d\n", pcpu, CPU_SETSIZE - 1); 256 return (-1); 257 } 258 259 snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu); 260 value = get_config_value(key); 261 262 if (asprintf(&newval, "%s%s%d", value != NULL ? value : "", 263 value != NULL ? "," : "", pcpu) == -1) { 264 perror("failed to build new cpuset string"); 265 return (-1); 266 } 267 268 set_config_value(key, newval); 269 free(newval); 270 return (0); 271 } 272 273 static void 274 parse_cpuset(int vcpu, const char *list, cpuset_t *set) 275 { 276 char *cp, *token; 277 int pcpu, start; 278 279 CPU_ZERO(set); 280 start = -1; 281 token = __DECONST(char *, list); 282 for (;;) { 283 pcpu = strtoul(token, &cp, 0); 284 if (cp == token) 285 errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list); 286 if (pcpu < 0 || pcpu >= CPU_SETSIZE) 287 errx(4, "hostcpu '%d' outside valid range from 0 to %d", 288 pcpu, CPU_SETSIZE - 1); 289 switch (*cp) { 290 case ',': 291 case '\0': 292 if (start >= 0) { 293 if (start > pcpu) 294 errx(4, "Invalid hostcpu range %d-%d", 295 start, pcpu); 296 while (start < pcpu) { 297 CPU_SET(start, set); 298 start++; 299 } 300 start = -1; 301 } 302 CPU_SET(pcpu, set); 303 break; 304 case '-': 305 if (start >= 0) 306 errx(4, "invalid cpuset for vcpu %d: '%s'", 307 vcpu, list); 308 start = pcpu; 309 break; 310 default: 311 errx(4, "invalid cpuset for vcpu %d: '%s'", vcpu, list); 312 } 313 if (*cp == '\0') 314 break; 315 token = cp + 1; 316 } 317 } 318 319 static void 320 build_vcpumaps(void) 321 { 322 char key[16]; 323 const char *value; 324 int vcpu; 325 326 vcpumap = calloc(guest_ncpus, sizeof(*vcpumap)); 327 for (vcpu = 0; vcpu < guest_ncpus; vcpu++) { 328 snprintf(key, sizeof(key), "vcpu.%d.cpuset", vcpu); 329 value = get_config_value(key); 330 if (value == NULL) 331 continue; 332 vcpumap[vcpu] = malloc(sizeof(cpuset_t)); 333 if (vcpumap[vcpu] == NULL) 334 err(4, "Failed to allocate cpuset for vcpu %d", vcpu); 335 parse_cpuset(vcpu, value, vcpumap[vcpu]); 336 } 337 } 338 339 void * 340 paddr_guest2host(struct vmctx *ctx, uintptr_t gaddr, size_t len) 341 { 342 343 return (vm_map_gpa(ctx, gaddr, len)); 344 } 345 346 #ifdef BHYVE_SNAPSHOT 347 uintptr_t 348 paddr_host2guest(struct vmctx *ctx, void *addr) 349 { 350 return (vm_rev_map_gpa(ctx, addr)); 351 } 352 #endif 353 354 int 355 fbsdrun_virtio_msix(void) 356 { 357 358 return (get_config_bool_default("virtio_msix", true)); 359 } 360 361 struct vcpu * 362 fbsdrun_vcpu(int vcpuid) 363 { 364 return (vcpu_info[vcpuid].vcpu); 365 } 366 367 static void * 368 fbsdrun_start_thread(void *param) 369 { 370 char tname[MAXCOMLEN + 1]; 371 struct vcpu_info *vi = param; 372 int error; 373 374 snprintf(tname, sizeof(tname), "vcpu %d", vi->vcpuid); 375 pthread_set_name_np(pthread_self(), tname); 376 377 if (vcpumap[vi->vcpuid] != NULL) { 378 error = pthread_setaffinity_np(pthread_self(), 379 sizeof(cpuset_t), vcpumap[vi->vcpuid]); 380 assert(error == 0); 381 } 382 383 #ifdef BHYVE_SNAPSHOT 384 checkpoint_cpu_add(vi->vcpuid); 385 #endif 386 #ifdef BHYVE_GDB 387 gdb_cpu_add(vi->vcpu); 388 #endif 389 390 vm_loop(vi->ctx, vi->vcpu); 391 392 /* not reached */ 393 exit(1); 394 return (NULL); 395 } 396 397 void 398 fbsdrun_addcpu(int vcpuid) 399 { 400 struct vcpu_info *vi; 401 pthread_t thr; 402 int error; 403 404 vi = &vcpu_info[vcpuid]; 405 406 error = vm_activate_cpu(vi->vcpu); 407 if (error != 0) 408 err(EX_OSERR, "could not activate CPU %d", vi->vcpuid); 409 410 CPU_SET_ATOMIC(vcpuid, &cpumask); 411 412 vm_suspend_cpu(vi->vcpu); 413 414 error = pthread_create(&thr, NULL, fbsdrun_start_thread, vi); 415 assert(error == 0); 416 } 417 418 void 419 fbsdrun_deletecpu(int vcpu) 420 { 421 static pthread_mutex_t resetcpu_mtx = PTHREAD_MUTEX_INITIALIZER; 422 static pthread_cond_t resetcpu_cond = PTHREAD_COND_INITIALIZER; 423 424 pthread_mutex_lock(&resetcpu_mtx); 425 if (!CPU_ISSET(vcpu, &cpumask)) { 426 EPRINTLN("Attempting to delete unknown cpu %d", vcpu); 427 exit(4); 428 } 429 430 CPU_CLR(vcpu, &cpumask); 431 432 if (vcpu != BSP) { 433 pthread_cond_signal(&resetcpu_cond); 434 pthread_mutex_unlock(&resetcpu_mtx); 435 pthread_exit(NULL); 436 /* NOTREACHED */ 437 } 438 439 while (!CPU_EMPTY(&cpumask)) { 440 pthread_cond_wait(&resetcpu_cond, &resetcpu_mtx); 441 } 442 pthread_mutex_unlock(&resetcpu_mtx); 443 } 444 445 int 446 fbsdrun_suspendcpu(int vcpuid) 447 { 448 return (vm_suspend_cpu(vcpu_info[vcpuid].vcpu)); 449 } 450 451 static void 452 vm_loop(struct vmctx *ctx, struct vcpu *vcpu) 453 { 454 struct vm_exit vme; 455 struct vm_run vmrun; 456 int error, rc; 457 enum vm_exitcode exitcode; 458 cpuset_t active_cpus, dmask; 459 460 error = vm_active_cpus(ctx, &active_cpus); 461 assert(CPU_ISSET(vcpu_id(vcpu), &active_cpus)); 462 463 vmrun.vm_exit = &vme; 464 vmrun.cpuset = &dmask; 465 vmrun.cpusetsize = sizeof(dmask); 466 467 while (1) { 468 error = vm_run(vcpu, &vmrun); 469 if (error != 0) 470 break; 471 472 exitcode = vme.exitcode; 473 if (exitcode >= VM_EXITCODE_MAX || 474 vmexit_handlers[exitcode] == NULL) { 475 warnx("vm_loop: unexpected exitcode 0x%x", exitcode); 476 exit(4); 477 } 478 479 rc = (*vmexit_handlers[exitcode])(ctx, vcpu, &vmrun); 480 481 switch (rc) { 482 case VMEXIT_CONTINUE: 483 break; 484 case VMEXIT_ABORT: 485 abort(); 486 default: 487 exit(4); 488 } 489 } 490 EPRINTLN("vm_run error %d, errno %d", error, errno); 491 } 492 493 static int 494 num_vcpus_allowed(struct vmctx *ctx, struct vcpu *vcpu) 495 { 496 uint16_t sockets, cores, threads, maxcpus; 497 int tmp, error; 498 499 /* 500 * The guest is allowed to spinup more than one processor only if the 501 * UNRESTRICTED_GUEST capability is available. 502 */ 503 error = vm_get_capability(vcpu, VM_CAP_UNRESTRICTED_GUEST, &tmp); 504 if (error != 0) 505 return (1); 506 507 error = vm_get_topology(ctx, &sockets, &cores, &threads, &maxcpus); 508 if (error == 0) 509 return (maxcpus); 510 else 511 return (1); 512 } 513 514 static struct vmctx * 515 do_open(const char *vmname) 516 { 517 struct vmctx *ctx; 518 int error; 519 bool reinit, romboot; 520 521 reinit = false; 522 523 #ifdef __amd64__ 524 romboot = lpc_bootrom() != NULL; 525 #else 526 romboot = true; 527 #endif 528 529 error = vm_create(vmname); 530 if (error) { 531 if (errno == EEXIST) { 532 if (romboot) { 533 reinit = true; 534 } else { 535 /* 536 * The virtual machine has been setup by the 537 * userspace bootloader. 538 */ 539 } 540 } else { 541 perror("vm_create"); 542 exit(4); 543 } 544 } else { 545 if (!romboot) { 546 /* 547 * If the virtual machine was just created then a 548 * bootrom must be configured to boot it. 549 */ 550 fprintf(stderr, "virtual machine cannot be booted\n"); 551 exit(4); 552 } 553 } 554 555 ctx = vm_open(vmname); 556 if (ctx == NULL) { 557 perror("vm_open"); 558 exit(4); 559 } 560 561 #ifndef WITHOUT_CAPSICUM 562 if (vm_limit_rights(ctx) != 0) 563 err(EX_OSERR, "vm_limit_rights"); 564 #endif 565 566 if (reinit) { 567 error = vm_reinit(ctx); 568 if (error) { 569 perror("vm_reinit"); 570 exit(4); 571 } 572 } 573 error = vm_set_topology(ctx, cpu_sockets, cpu_cores, cpu_threads, 0); 574 if (error) 575 errx(EX_OSERR, "vm_set_topology"); 576 return (ctx); 577 } 578 579 bool 580 bhyve_parse_config_option(const char *option) 581 { 582 const char *value; 583 char *path; 584 585 value = strchr(option, '='); 586 if (value == NULL || value[1] == '\0') 587 return (false); 588 path = strndup(option, value - option); 589 if (path == NULL) 590 err(4, "Failed to allocate memory"); 591 set_config_value(path, value + 1); 592 return (true); 593 } 594 595 void 596 bhyve_parse_simple_config_file(const char *path) 597 { 598 FILE *fp; 599 char *line, *cp; 600 size_t linecap; 601 unsigned int lineno; 602 603 fp = fopen(path, "r"); 604 if (fp == NULL) 605 err(4, "Failed to open configuration file %s", path); 606 line = NULL; 607 linecap = 0; 608 lineno = 1; 609 for (lineno = 1; getline(&line, &linecap, fp) > 0; lineno++) { 610 if (*line == '#' || *line == '\n') 611 continue; 612 cp = strchr(line, '\n'); 613 if (cp != NULL) 614 *cp = '\0'; 615 if (!bhyve_parse_config_option(line)) 616 errx(4, "%s line %u: invalid config option '%s'", path, 617 lineno, line); 618 } 619 free(line); 620 fclose(fp); 621 } 622 623 #ifdef BHYVE_GDB 624 void 625 bhyve_parse_gdb_options(const char *opt) 626 { 627 const char *sport; 628 char *colon; 629 630 if (opt[0] == 'w') { 631 set_config_bool("gdb.wait", true); 632 opt++; 633 } 634 635 colon = strrchr(opt, ':'); 636 if (colon == NULL) { 637 sport = opt; 638 } else { 639 *colon = '\0'; 640 colon++; 641 sport = colon; 642 set_config_value("gdb.address", opt); 643 } 644 645 set_config_value("gdb.port", sport); 646 } 647 #endif 648 649 int 650 main(int argc, char *argv[]) 651 { 652 int error; 653 int max_vcpus, memflags; 654 struct vcpu *bsp; 655 struct vmctx *ctx; 656 size_t memsize; 657 const char *value, *vmname; 658 #ifdef BHYVE_SNAPSHOT 659 char *restore_file; 660 struct restore_state rstate; 661 662 restore_file = NULL; 663 #endif 664 665 bhyve_init_config(); 666 bhyve_optparse(argc, argv); 667 argc -= optind; 668 argv += optind; 669 670 if (argc > 1) 671 bhyve_usage(1); 672 673 #ifdef BHYVE_SNAPSHOT 674 if (restore_file != NULL) { 675 error = load_restore_file(restore_file, &rstate); 676 if (error) { 677 fprintf(stderr, "Failed to read checkpoint info from " 678 "file: '%s'.\n", restore_file); 679 exit(1); 680 } 681 vmname = lookup_vmname(&rstate); 682 if (vmname != NULL) 683 set_config_value("name", vmname); 684 } 685 #endif 686 687 if (argc == 1) 688 set_config_value("name", argv[0]); 689 690 vmname = get_config_value("name"); 691 if (vmname == NULL) 692 bhyve_usage(1); 693 694 if (get_config_bool_default("config.dump", false)) { 695 dump_config(); 696 exit(1); 697 } 698 699 calc_topology(); 700 build_vcpumaps(); 701 702 value = get_config_value("memory.size"); 703 error = vm_parse_memsize(value, &memsize); 704 if (error) 705 errx(EX_USAGE, "invalid memsize '%s'", value); 706 707 ctx = do_open(vmname); 708 709 #ifdef BHYVE_SNAPSHOT 710 if (restore_file != NULL) { 711 guest_ncpus = lookup_guest_ncpus(&rstate); 712 memflags = lookup_memflags(&rstate); 713 memsize = lookup_memsize(&rstate); 714 } 715 716 if (guest_ncpus < 1) { 717 fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus); 718 exit(1); 719 } 720 #endif 721 722 bsp = vm_vcpu_open(ctx, BSP); 723 max_vcpus = num_vcpus_allowed(ctx, bsp); 724 if (guest_ncpus > max_vcpus) { 725 fprintf(stderr, "%d vCPUs requested but only %d available\n", 726 guest_ncpus, max_vcpus); 727 exit(4); 728 } 729 730 bhyve_init_vcpu(bsp); 731 732 /* Allocate per-VCPU resources. */ 733 vcpu_info = calloc(guest_ncpus, sizeof(*vcpu_info)); 734 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) { 735 vcpu_info[vcpuid].ctx = ctx; 736 vcpu_info[vcpuid].vcpuid = vcpuid; 737 if (vcpuid == BSP) 738 vcpu_info[vcpuid].vcpu = bsp; 739 else 740 vcpu_info[vcpuid].vcpu = vm_vcpu_open(ctx, vcpuid); 741 } 742 743 memflags = 0; 744 if (get_config_bool_default("memory.wired", false)) 745 memflags |= VM_MEM_F_WIRED; 746 if (get_config_bool_default("memory.guest_in_core", false)) 747 memflags |= VM_MEM_F_INCORE; 748 vm_set_memflags(ctx, memflags); 749 error = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); 750 if (error) { 751 fprintf(stderr, "Unable to setup memory (%d)\n", errno); 752 exit(4); 753 } 754 755 init_mem(guest_ncpus); 756 init_bootrom(ctx); 757 if (bhyve_init_platform(ctx, bsp) != 0) 758 exit(4); 759 760 if (qemu_fwcfg_init(ctx) != 0) { 761 fprintf(stderr, "qemu fwcfg initialization error\n"); 762 exit(4); 763 } 764 765 if (qemu_fwcfg_add_file("opt/bhyve/hw.ncpu", sizeof(guest_ncpus), 766 &guest_ncpus) != 0) { 767 fprintf(stderr, "Could not add qemu fwcfg opt/bhyve/hw.ncpu\n"); 768 exit(4); 769 } 770 771 /* 772 * Exit if a device emulation finds an error in its initialization 773 */ 774 if (init_pci(ctx) != 0) { 775 EPRINTLN("Device emulation initialization error: %s", 776 strerror(errno)); 777 exit(4); 778 } 779 if (init_tpm(ctx) != 0) { 780 EPRINTLN("Failed to init TPM device"); 781 exit(4); 782 } 783 784 /* 785 * Initialize after PCI, to allow a bootrom file to reserve the high 786 * region. 787 */ 788 if (get_config_bool("acpi_tables")) 789 vmgenc_init(ctx); 790 791 #ifdef BHYVE_GDB 792 init_gdb(ctx); 793 #endif 794 795 /* 796 * Add all vCPUs. 797 */ 798 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) 799 bhyve_start_vcpu(vcpu_info[vcpuid].vcpu, vcpuid == BSP); 800 801 #ifdef BHYVE_SNAPSHOT 802 if (restore_file != NULL) { 803 FPRINTLN(stdout, "Pausing pci devs..."); 804 if (vm_pause_devices() != 0) { 805 EPRINTLN("Failed to pause PCI device state."); 806 exit(1); 807 } 808 809 FPRINTLN(stdout, "Restoring vm mem..."); 810 if (restore_vm_mem(ctx, &rstate) != 0) { 811 EPRINTLN("Failed to restore VM memory."); 812 exit(1); 813 } 814 815 FPRINTLN(stdout, "Restoring pci devs..."); 816 if (vm_restore_devices(&rstate) != 0) { 817 EPRINTLN("Failed to restore PCI device state."); 818 exit(1); 819 } 820 821 FPRINTLN(stdout, "Restoring kernel structs..."); 822 if (vm_restore_kern_structs(ctx, &rstate) != 0) { 823 EPRINTLN("Failed to restore kernel structs."); 824 exit(1); 825 } 826 827 FPRINTLN(stdout, "Resuming pci devs..."); 828 if (vm_resume_devices() != 0) { 829 EPRINTLN("Failed to resume PCI device state."); 830 exit(1); 831 } 832 } 833 #endif 834 835 if (bhyve_init_platform_late(ctx, bsp) != 0) 836 exit(4); 837 838 /* 839 * Change the proc title to include the VM name. 840 */ 841 setproctitle("%s", vmname); 842 843 #ifdef BHYVE_SNAPSHOT 844 /* 845 * checkpointing thread for communication with bhyvectl 846 */ 847 if (init_checkpoint_thread(ctx) != 0) 848 errx(EX_OSERR, "Failed to start checkpoint thread"); 849 #endif 850 851 #ifndef WITHOUT_CAPSICUM 852 caph_cache_catpages(); 853 854 if (caph_limit_stdout() == -1 || caph_limit_stderr() == -1) 855 errx(EX_OSERR, "Unable to apply rights for sandbox"); 856 857 if (caph_enter() == -1) 858 errx(EX_OSERR, "cap_enter() failed"); 859 #endif 860 861 #ifdef BHYVE_SNAPSHOT 862 if (restore_file != NULL) { 863 destroy_restore_state(&rstate); 864 if (vm_restore_time(ctx) < 0) 865 err(EX_OSERR, "Unable to restore time"); 866 867 for (int vcpuid = 0; vcpuid < guest_ncpus; vcpuid++) 868 vm_resume_cpu(vcpu_info[vcpuid].vcpu); 869 } else 870 #endif 871 vm_resume_cpu(bsp); 872 873 /* 874 * Head off to the main event dispatch loop 875 */ 876 mevent_dispatch(); 877 878 exit(4); 879 } 880