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