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