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