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