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