1 #include "kvm/builtin-run.h" 2 3 #include "kvm/builtin-setup.h" 4 #include "kvm/virtio-balloon.h" 5 #include "kvm/virtio-console.h" 6 #include "kvm/parse-options.h" 7 #include "kvm/8250-serial.h" 8 #include "kvm/framebuffer.h" 9 #include "kvm/disk-image.h" 10 #include "kvm/threadpool.h" 11 #include "kvm/virtio-scsi.h" 12 #include "kvm/virtio-blk.h" 13 #include "kvm/virtio-net.h" 14 #include "kvm/virtio-rng.h" 15 #include "kvm/ioeventfd.h" 16 #include "kvm/virtio-9p.h" 17 #include "kvm/barrier.h" 18 #include "kvm/kvm-cpu.h" 19 #include "kvm/ioport.h" 20 #include "kvm/symbol.h" 21 #include "kvm/i8042.h" 22 #include "kvm/mutex.h" 23 #include "kvm/term.h" 24 #include "kvm/util.h" 25 #include "kvm/strbuf.h" 26 #include "kvm/vesa.h" 27 #include "kvm/irq.h" 28 #include "kvm/kvm.h" 29 #include "kvm/pci.h" 30 #include "kvm/rtc.h" 31 #include "kvm/sdl.h" 32 #include "kvm/vnc.h" 33 #include "kvm/guest_compat.h" 34 #include "kvm/kvm-ipc.h" 35 #include "kvm/builtin-debug.h" 36 37 #include <linux/types.h> 38 #include <linux/err.h> 39 40 #include <sys/utsname.h> 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <termios.h> 44 #include <signal.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <ctype.h> 49 #include <stdio.h> 50 51 #define MB_SHIFT (20) 52 #define KB_SHIFT (10) 53 #define GB_SHIFT (30) 54 55 __thread struct kvm_cpu *current_kvm_cpu; 56 57 static int kvm_run_wrapper; 58 59 bool do_debug_print = false; 60 61 static const char * const run_usage[] = { 62 "lkvm run [<options>] [<kernel image>]", 63 NULL 64 }; 65 66 enum { 67 KVM_RUN_DEFAULT, 68 KVM_RUN_SANDBOX, 69 }; 70 71 static int img_name_parser(const struct option *opt, const char *arg, int unset) 72 { 73 char path[PATH_MAX]; 74 struct stat st; 75 76 snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg); 77 78 if ((stat(arg, &st) == 0 && S_ISDIR(st.st_mode)) || 79 (stat(path, &st) == 0 && S_ISDIR(st.st_mode))) 80 return virtio_9p_img_name_parser(opt, arg, unset); 81 return disk_img_name_parser(opt, arg, unset); 82 } 83 84 void kvm_run_set_wrapper_sandbox(void) 85 { 86 kvm_run_wrapper = KVM_RUN_SANDBOX; 87 } 88 89 #ifndef OPT_ARCH_RUN 90 #define OPT_ARCH_RUN(...) 91 #endif 92 93 #define BUILD_OPTIONS(name, cfg, kvm) \ 94 struct option name[] = { \ 95 OPT_GROUP("Basic options:"), \ 96 OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name", \ 97 "A name for the guest"), \ 98 OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"), \ 99 OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory" \ 100 " size in MiB."), \ 101 OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk " \ 102 " image or rootfs directory", img_name_parser, \ 103 kvm), \ 104 OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio" \ 105 " balloon"), \ 106 OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\ 107 OPT_BOOLEAN('\0', "gtk", &(cfg)->gtk, "Enable GTK framebuffer"),\ 108 OPT_BOOLEAN('\0', "sdl", &(cfg)->sdl, "Enable SDL framebuffer"),\ 109 OPT_BOOLEAN('\0', "rng", &(cfg)->virtio_rng, "Enable virtio" \ 110 " Random Number Generator"), \ 111 OPT_BOOLEAN('\0', "nodefaults", &(cfg)->nodefaults, "Disable" \ 112 " implicit configuration that cannot be" \ 113 " disabled otherwise"), \ 114 OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name", \ 115 "Enable virtio 9p to share files between host and" \ 116 " guest", virtio_9p_rootdir_parser, kvm), \ 117 OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or"\ 118 " hv", "Console to use"), \ 119 OPT_U64('\0', "vsock", &(cfg)->vsock_cid, \ 120 "Guest virtio socket CID"), \ 121 OPT_STRING('\0', "dev", &(cfg)->dev, "device_file", \ 122 "KVM device file"), \ 123 OPT_CALLBACK('\0', "tty", NULL, "tty id", \ 124 "Remap guest TTY into a pty on the host", \ 125 tty_parser, NULL), \ 126 OPT_STRING('\0', "sandbox", &(cfg)->sandbox, "script", \ 127 "Run this script when booting into custom" \ 128 " rootfs"), \ 129 OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path", \ 130 "Hugetlbfs path"), \ 131 OPT_BOOLEAN('\0', "no-pvtime", &(cfg)->no_pvtime, "Disable" \ 132 " stolen time"), \ 133 \ 134 OPT_GROUP("Kernel options:"), \ 135 OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel", \ 136 "Kernel to boot in virtual machine"), \ 137 OPT_STRING('i', "initrd", &(cfg)->initrd_filename, "initrd", \ 138 "Initial RAM disk image"), \ 139 OPT_STRING('p', "params", &(cfg)->kernel_cmdline, "params", \ 140 "Kernel command line arguments"), \ 141 OPT_STRING('f', "firmware", &(cfg)->firmware_filename, "firmware",\ 142 "Firmware image to boot in virtual machine"), \ 143 OPT_STRING('F', "flash", &(cfg)->flash_filename, "flash",\ 144 "Flash image to present to virtual machine"), \ 145 \ 146 OPT_GROUP("Networking options:"), \ 147 OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params", \ 148 "Create a new guest NIC", \ 149 netdev_parser, NULL, kvm), \ 150 OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel" \ 151 " DHCP in rootfs mode"), \ 152 \ 153 OPT_GROUP("VFIO options:"), \ 154 OPT_CALLBACK('\0', "vfio-pci", NULL, "[domain:]bus:dev.fn", \ 155 "Assign a PCI device to the virtual machine", \ 156 vfio_device_parser, kvm), \ 157 \ 158 OPT_GROUP("Debug options:"), \ 159 OPT_BOOLEAN('\0', "debug", &do_debug_print, \ 160 "Enable debug messages"), \ 161 OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step, \ 162 "Enable single stepping"), \ 163 OPT_BOOLEAN('\0', "debug-ioport", &(cfg)->ioport_debug, \ 164 "Enable ioport debugging"), \ 165 OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug, \ 166 "Enable MMIO debugging"), \ 167 OPT_INTEGER('\0', "debug-iodelay", &(cfg)->debug_iodelay, \ 168 "Delay IO by millisecond"), \ 169 \ 170 OPT_ARCH(RUN, cfg) \ 171 OPT_END() \ 172 }; 173 174 static void *kvm_cpu_thread(void *arg) 175 { 176 char name[16]; 177 178 current_kvm_cpu = arg; 179 180 sprintf(name, "kvm-vcpu-%lu", current_kvm_cpu->cpu_id); 181 kvm__set_thread_name(name); 182 183 if (kvm_cpu__start(current_kvm_cpu)) 184 goto panic_kvm; 185 186 return (void *) (intptr_t) 0; 187 188 panic_kvm: 189 fprintf(stderr, "KVM exit reason: %u (\"%s\")\n", 190 current_kvm_cpu->kvm_run->exit_reason, 191 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 192 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 193 fprintf(stderr, "KVM exit code: 0x%llu\n", 194 (unsigned long long)current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 195 196 kvm_cpu__set_debug_fd(STDOUT_FILENO); 197 kvm_cpu__show_registers(current_kvm_cpu); 198 kvm_cpu__show_code(current_kvm_cpu); 199 kvm_cpu__show_page_tables(current_kvm_cpu); 200 201 return (void *) (intptr_t) 1; 202 } 203 204 static char kernel[PATH_MAX]; 205 206 static const char *host_kernels[] = { 207 "/boot/vmlinuz", 208 "/boot/bzImage", 209 NULL 210 }; 211 212 static const char *default_kernels[] = { 213 "./bzImage", 214 "arch/" BUILD_ARCH "/boot/bzImage", 215 "../../arch/" BUILD_ARCH "/boot/bzImage", 216 NULL 217 }; 218 219 static const char *default_vmlinux[] = { 220 "vmlinux", 221 "../../../vmlinux", 222 "../../vmlinux", 223 NULL 224 }; 225 226 static void kernel_usage_with_options(void) 227 { 228 const char **k; 229 struct utsname uts; 230 231 fprintf(stderr, "Fatal: could not find default kernel image in:\n"); 232 k = &default_kernels[0]; 233 while (*k) { 234 fprintf(stderr, "\t%s\n", *k); 235 k++; 236 } 237 238 if (uname(&uts) < 0) 239 return; 240 241 k = &host_kernels[0]; 242 while (*k) { 243 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 244 return; 245 fprintf(stderr, "\t%s\n", kernel); 246 k++; 247 } 248 fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n", 249 KVM_BINARY_NAME); 250 } 251 252 static u64 host_ram_size(void) 253 { 254 long page_size; 255 long nr_pages; 256 257 nr_pages = sysconf(_SC_PHYS_PAGES); 258 if (nr_pages < 0) { 259 pr_warning("sysconf(_SC_PHYS_PAGES) failed"); 260 return 0; 261 } 262 263 page_size = sysconf(_SC_PAGE_SIZE); 264 if (page_size < 0) { 265 pr_warning("sysconf(_SC_PAGE_SIZE) failed"); 266 return 0; 267 } 268 269 return (nr_pages * page_size) >> MB_SHIFT; 270 } 271 272 /* 273 * If user didn't specify how much memory it wants to allocate for the guest, 274 * avoid filling the whole host RAM. 275 */ 276 #define RAM_SIZE_RATIO 0.8 277 278 static u64 get_ram_size(int nr_cpus) 279 { 280 u64 available; 281 u64 ram_size; 282 283 ram_size = 64 * (nr_cpus + 3); 284 285 available = host_ram_size() * RAM_SIZE_RATIO; 286 if (!available) 287 available = MIN_RAM_SIZE_MB; 288 289 if (ram_size > available) 290 ram_size = available; 291 292 return ram_size; 293 } 294 295 static const char *find_kernel(void) 296 { 297 const char **k; 298 struct stat st; 299 struct utsname uts; 300 301 k = &default_kernels[0]; 302 while (*k) { 303 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) { 304 k++; 305 continue; 306 } 307 strlcpy(kernel, *k, PATH_MAX); 308 return kernel; 309 } 310 311 if (uname(&uts) < 0) 312 return NULL; 313 314 k = &host_kernels[0]; 315 while (*k) { 316 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 317 return NULL; 318 319 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) { 320 k++; 321 continue; 322 } 323 return kernel; 324 325 } 326 return NULL; 327 } 328 329 static const char *find_vmlinux(void) 330 { 331 const char **vmlinux; 332 333 vmlinux = &default_vmlinux[0]; 334 while (*vmlinux) { 335 struct stat st; 336 337 if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) { 338 vmlinux++; 339 continue; 340 } 341 return *vmlinux; 342 } 343 return NULL; 344 } 345 346 void kvm_run_help(void) 347 { 348 struct kvm *kvm = NULL; 349 350 BUILD_OPTIONS(options, &kvm->cfg, kvm); 351 usage_with_options(run_usage, options); 352 } 353 354 static int kvm_run_set_sandbox(struct kvm *kvm) 355 { 356 const char *guestfs_name = kvm->cfg.custom_rootfs_name; 357 char path[PATH_MAX], script[PATH_MAX], *tmp; 358 359 snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name); 360 361 remove(path); 362 363 if (kvm->cfg.sandbox == NULL) 364 return 0; 365 366 tmp = realpath(kvm->cfg.sandbox, NULL); 367 if (tmp == NULL) 368 return -ENOMEM; 369 370 snprintf(script, PATH_MAX, "/host/%s", tmp); 371 free(tmp); 372 373 return symlink(script, path); 374 } 375 376 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg) 377 { 378 const char *single_quote; 379 380 if (!*arg) { /* zero length string */ 381 if (write(fd, "''", 2) <= 0) 382 die("Failed writing sandbox script"); 383 return; 384 } 385 386 while (*arg) { 387 single_quote = strchrnul(arg, '\''); 388 389 /* write non-single-quote string as #('string') */ 390 if (arg != single_quote) { 391 if (write(fd, "'", 1) <= 0 || 392 write(fd, arg, single_quote - arg) <= 0 || 393 write(fd, "'", 1) <= 0) 394 die("Failed writing sandbox script"); 395 } 396 397 /* write single quote as #("'") */ 398 if (*single_quote) { 399 if (write(fd, "\"'\"", 3) <= 0) 400 die("Failed writing sandbox script"); 401 } else 402 break; 403 404 arg = single_quote + 1; 405 } 406 } 407 408 static void resolve_program(const char *src, char *dst, size_t len) 409 { 410 struct stat st; 411 int err; 412 413 err = stat(src, &st); 414 415 if (!err && S_ISREG(st.st_mode)) { 416 char resolved_path[PATH_MAX]; 417 418 if (!realpath(src, resolved_path)) 419 die("Unable to resolve program %s: %s\n", src, strerror(errno)); 420 421 if (snprintf(dst, len, "/host%s", resolved_path) >= (int)len) 422 die("Pathname too long: %s -> %s\n", src, resolved_path); 423 424 } else 425 strlcpy(dst, src, len); 426 } 427 428 static void kvm_run_write_sandbox_cmd(struct kvm *kvm, const char **argv, int argc) 429 { 430 const char script_hdr[] = "#! /bin/bash\n\n"; 431 char program[PATH_MAX]; 432 int fd; 433 434 remove(kvm->cfg.sandbox); 435 436 fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777); 437 if (fd < 0) 438 die("Failed creating sandbox script"); 439 440 if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0) 441 die("Failed writing sandbox script"); 442 443 resolve_program(argv[0], program, PATH_MAX); 444 kvm_write_sandbox_cmd_exactly(fd, program); 445 446 argv++; 447 argc--; 448 449 while (argc) { 450 if (write(fd, " ", 1) <= 0) 451 die("Failed writing sandbox script"); 452 453 kvm_write_sandbox_cmd_exactly(fd, argv[0]); 454 argv++; 455 argc--; 456 } 457 if (write(fd, "\n", 1) <= 0) 458 die("Failed writing sandbox script"); 459 460 close(fd); 461 } 462 463 static void kvm_run_set_real_cmdline(struct kvm *kvm) 464 { 465 static char real_cmdline[2048]; 466 bool video; 467 468 video = kvm->cfg.vnc || kvm->cfg.sdl || kvm->cfg.gtk; 469 470 memset(real_cmdline, 0, sizeof(real_cmdline)); 471 kvm__arch_set_cmdline(real_cmdline, video); 472 473 if (video) { 474 strcat(real_cmdline, " console=tty0"); 475 } else { 476 switch (kvm->cfg.active_console) { 477 case CONSOLE_HV: 478 /* Fallthrough */ 479 case CONSOLE_VIRTIO: 480 strcat(real_cmdline, " console=hvc0"); 481 break; 482 case CONSOLE_8250: 483 strcat(real_cmdline, " console=ttyS0"); 484 break; 485 } 486 } 487 488 if (kvm->cfg.using_rootfs) { 489 strcat(real_cmdline, " rw rootflags=trans=virtio,version=9p2000.L,cache=loose rootfstype=9p"); 490 if (kvm->cfg.custom_rootfs) { 491 #ifdef CONFIG_GUEST_PRE_INIT 492 strcat(real_cmdline, " init=/virt/pre_init"); 493 #else 494 strcat(real_cmdline, " init=/virt/init"); 495 #endif 496 if (!kvm->cfg.no_dhcp) 497 strcat(real_cmdline, " ip=dhcp"); 498 } 499 } else if (!kvm->cfg.kernel_cmdline || !strstr(kvm->cfg.kernel_cmdline, "root=")) { 500 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); 501 } 502 503 if (kvm->cfg.kernel_cmdline) { 504 strcat(real_cmdline, " "); 505 strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline)); 506 } 507 508 kvm->cfg.real_cmdline = real_cmdline; 509 } 510 511 static void kvm_run_validate_cfg(struct kvm *kvm) 512 { 513 if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename) 514 die("Only one of --kernel or --firmware can be specified"); 515 516 if ((kvm->cfg.vnc && (kvm->cfg.sdl || kvm->cfg.gtk)) || 517 (kvm->cfg.sdl && kvm->cfg.gtk)) 518 die("Only one of --vnc, --sdl or --gtk can be specified"); 519 520 if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename) 521 pr_warning("Ignoring initrd file when loading a firmware image"); 522 } 523 524 static struct kvm *kvm_cmd_run_init(int argc, const char **argv) 525 { 526 static char default_name[20]; 527 unsigned int nr_online_cpus; 528 struct kvm *kvm = kvm__new(); 529 530 if (IS_ERR(kvm)) 531 return kvm; 532 533 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); 534 kvm->cfg.custom_rootfs_name = "default"; 535 536 while (argc != 0) { 537 BUILD_OPTIONS(options, &kvm->cfg, kvm); 538 argc = parse_options(argc, argv, options, run_usage, 539 PARSE_OPT_STOP_AT_NON_OPTION | 540 PARSE_OPT_KEEP_DASHDASH); 541 if (argc != 0) { 542 /* Cusrom options, should have been handled elsewhere */ 543 if (strcmp(argv[0], "--") == 0) { 544 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 545 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 546 kvm_run_write_sandbox_cmd(kvm, argv+1, argc-1); 547 break; 548 } 549 } 550 551 if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) || 552 (kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) { 553 fprintf(stderr, "Cannot handle parameter: " 554 "%s\n", argv[0]); 555 usage_with_options(run_usage, options); 556 free(kvm); 557 return ERR_PTR(-EINVAL); 558 } 559 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 560 /* 561 * first unhandled parameter is treated as 562 * sandbox command 563 */ 564 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 565 kvm_run_write_sandbox_cmd(kvm, argv, argc); 566 } else { 567 /* 568 * first unhandled parameter is treated as a kernel 569 * image 570 */ 571 kvm->cfg.kernel_filename = argv[0]; 572 } 573 argv++; 574 argc--; 575 } 576 577 } 578 579 kvm_run_validate_cfg(kvm); 580 581 if (!kvm->cfg.kernel_filename && !kvm->cfg.firmware_filename) { 582 kvm->cfg.kernel_filename = find_kernel(); 583 584 if (!kvm->cfg.kernel_filename) { 585 kernel_usage_with_options(); 586 return ERR_PTR(-EINVAL); 587 } 588 } 589 590 if (kvm->cfg.kernel_filename) { 591 kvm->cfg.vmlinux_filename = find_vmlinux(); 592 kvm->vmlinux = kvm->cfg.vmlinux_filename; 593 } 594 595 if (kvm->cfg.nrcpus == 0) 596 kvm->cfg.nrcpus = nr_online_cpus; 597 598 if (!kvm->cfg.ram_size) 599 kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus); 600 601 if (kvm->cfg.ram_size > host_ram_size()) 602 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", 603 (unsigned long long)kvm->cfg.ram_size, 604 (unsigned long long)host_ram_size()); 605 606 kvm->cfg.ram_size <<= MB_SHIFT; 607 608 if (!kvm->cfg.dev) 609 kvm->cfg.dev = DEFAULT_KVM_DEV; 610 611 if (!kvm->cfg.console) 612 kvm->cfg.console = DEFAULT_CONSOLE; 613 614 if (!strncmp(kvm->cfg.console, "virtio", 6)) 615 kvm->cfg.active_console = CONSOLE_VIRTIO; 616 else if (!strncmp(kvm->cfg.console, "serial", 6)) 617 kvm->cfg.active_console = CONSOLE_8250; 618 else if (!strncmp(kvm->cfg.console, "hv", 2)) 619 kvm->cfg.active_console = CONSOLE_HV; 620 else 621 pr_warning("No console!"); 622 623 if (!kvm->cfg.host_ip) 624 kvm->cfg.host_ip = DEFAULT_HOST_ADDR; 625 626 if (!kvm->cfg.guest_ip) 627 kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR; 628 629 if (!kvm->cfg.guest_mac) 630 kvm->cfg.guest_mac = DEFAULT_GUEST_MAC; 631 632 if (!kvm->cfg.host_mac) 633 kvm->cfg.host_mac = DEFAULT_HOST_MAC; 634 635 if (!kvm->cfg.script) 636 kvm->cfg.script = DEFAULT_SCRIPT; 637 638 if (!kvm->cfg.network) 639 kvm->cfg.network = DEFAULT_NETWORK; 640 641 if (!kvm->cfg.guest_name) { 642 if (kvm->cfg.custom_rootfs) { 643 kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name; 644 } else { 645 sprintf(default_name, "guest-%u", getpid()); 646 kvm->cfg.guest_name = default_name; 647 } 648 } 649 650 if (!kvm->cfg.nodefaults && 651 !kvm->cfg.using_rootfs && 652 !kvm->cfg.disk_image[0].filename && 653 !kvm->cfg.initrd_filename) { 654 char tmp[PATH_MAX]; 655 656 kvm_setup_create_new(kvm->cfg.custom_rootfs_name); 657 kvm_setup_resolv(kvm->cfg.custom_rootfs_name); 658 659 snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default"); 660 if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) 661 die("Unable to initialize virtio 9p"); 662 if (virtio_9p__register(kvm, "/", "hostfs") < 0) 663 die("Unable to initialize virtio 9p"); 664 kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; 665 } 666 667 if (kvm->cfg.custom_rootfs) { 668 kvm_run_set_sandbox(kvm); 669 if (kvm_setup_guest_init(kvm->cfg.custom_rootfs_name)) 670 die("Failed to setup init for guest."); 671 } 672 673 if (kvm->cfg.nodefaults) 674 kvm->cfg.real_cmdline = kvm->cfg.kernel_cmdline; 675 else 676 kvm_run_set_real_cmdline(kvm); 677 678 if (kvm->cfg.kernel_filename) { 679 printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, 680 kvm->cfg.kernel_filename, 681 (unsigned long long)kvm->cfg.ram_size / 1024 / 1024, 682 kvm->cfg.nrcpus, kvm->cfg.guest_name); 683 } else if (kvm->cfg.firmware_filename) { 684 printf(" # %s run --firmware %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, 685 kvm->cfg.firmware_filename, 686 (unsigned long long)kvm->cfg.ram_size / 1024 / 1024, 687 kvm->cfg.nrcpus, kvm->cfg.guest_name); 688 } 689 690 if (init_list__init(kvm) < 0) 691 die ("Initialisation failed"); 692 693 return kvm; 694 } 695 696 static int kvm_cmd_run_work(struct kvm *kvm) 697 { 698 int i; 699 700 for (i = 0; i < kvm->nrcpus; i++) { 701 if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0) 702 die("unable to create KVM VCPU thread"); 703 } 704 705 /* Only VCPU #0 is going to exit by itself when shutting down */ 706 if (pthread_join(kvm->cpus[0]->thread, NULL) != 0) 707 die("unable to join with vcpu 0"); 708 709 return kvm_cpu__exit(kvm); 710 } 711 712 static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret) 713 { 714 compat__print_all_messages(); 715 716 init_list__exit(kvm); 717 718 if (guest_ret == 0) 719 printf("\n # KVM session ended normally.\n"); 720 } 721 722 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 723 { 724 int ret = -EFAULT; 725 struct kvm *kvm; 726 727 kvm = kvm_cmd_run_init(argc, argv); 728 if (IS_ERR(kvm)) 729 return PTR_ERR(kvm); 730 731 ret = kvm_cmd_run_work(kvm); 732 kvm_cmd_run_exit(kvm, ret); 733 734 return ret; 735 } 736