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 u64 available; 404 u64 ram_size; 405 406 ram_size = (u64)SZ_64M * (nr_cpus + 3); 407 408 available = host_ram_size() * RAM_SIZE_RATIO; 409 if (!available) 410 available = MIN_RAM_SIZE; 411 412 if (ram_size > available) 413 ram_size = available; 414 415 return ram_size; 416 } 417 418 static const char *find_kernel(void) 419 { 420 const char **k; 421 struct stat st; 422 struct utsname uts; 423 424 k = &default_kernels[0]; 425 while (*k) { 426 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) { 427 k++; 428 continue; 429 } 430 strlcpy(kernel, *k, PATH_MAX); 431 return kernel; 432 } 433 434 if (uname(&uts) < 0) 435 return NULL; 436 437 k = &host_kernels[0]; 438 while (*k) { 439 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 440 return NULL; 441 442 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) { 443 k++; 444 continue; 445 } 446 return kernel; 447 448 } 449 return NULL; 450 } 451 452 static const char *find_vmlinux(void) 453 { 454 const char **vmlinux; 455 456 vmlinux = &default_vmlinux[0]; 457 while (*vmlinux) { 458 struct stat st; 459 460 if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) { 461 vmlinux++; 462 continue; 463 } 464 return *vmlinux; 465 } 466 return NULL; 467 } 468 469 void kvm_run_help(void) 470 { 471 struct kvm *kvm = NULL; 472 473 BUILD_OPTIONS(options, &kvm->cfg, kvm); 474 usage_with_options(run_usage, options); 475 } 476 477 static int kvm_run_set_sandbox(struct kvm *kvm) 478 { 479 const char *guestfs_name = kvm->cfg.custom_rootfs_name; 480 char path[PATH_MAX], script[PATH_MAX], *tmp; 481 482 snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name); 483 484 remove(path); 485 486 if (kvm->cfg.sandbox == NULL) 487 return 0; 488 489 tmp = realpath(kvm->cfg.sandbox, NULL); 490 if (tmp == NULL) 491 return -ENOMEM; 492 493 snprintf(script, PATH_MAX, "/host/%s", tmp); 494 free(tmp); 495 496 return symlink(script, path); 497 } 498 499 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg) 500 { 501 const char *single_quote; 502 503 if (!*arg) { /* zero length string */ 504 if (write(fd, "''", 2) <= 0) 505 die("Failed writing sandbox script"); 506 return; 507 } 508 509 while (*arg) { 510 single_quote = strchrnul(arg, '\''); 511 512 /* write non-single-quote string as #('string') */ 513 if (arg != single_quote) { 514 if (write(fd, "'", 1) <= 0 || 515 write(fd, arg, single_quote - arg) <= 0 || 516 write(fd, "'", 1) <= 0) 517 die("Failed writing sandbox script"); 518 } 519 520 /* write single quote as #("'") */ 521 if (*single_quote) { 522 if (write(fd, "\"'\"", 3) <= 0) 523 die("Failed writing sandbox script"); 524 } else 525 break; 526 527 arg = single_quote + 1; 528 } 529 } 530 531 static void resolve_program(const char *src, char *dst, size_t len) 532 { 533 struct stat st; 534 int err; 535 536 err = stat(src, &st); 537 538 if (!err && S_ISREG(st.st_mode)) { 539 char resolved_path[PATH_MAX]; 540 541 if (!realpath(src, resolved_path)) 542 die("Unable to resolve program %s: %s\n", src, strerror(errno)); 543 544 if (snprintf(dst, len, "/host%s", resolved_path) >= (int)len) 545 die("Pathname too long: %s -> %s\n", src, resolved_path); 546 547 } else 548 strlcpy(dst, src, len); 549 } 550 551 static void kvm_run_write_sandbox_cmd(struct kvm *kvm, const char **argv, int argc) 552 { 553 const char script_hdr[] = "#! /bin/bash\n\n"; 554 char program[PATH_MAX]; 555 int fd; 556 557 remove(kvm->cfg.sandbox); 558 559 fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777); 560 if (fd < 0) 561 die("Failed creating sandbox script"); 562 563 if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0) 564 die("Failed writing sandbox script"); 565 566 resolve_program(argv[0], program, PATH_MAX); 567 kvm_write_sandbox_cmd_exactly(fd, program); 568 569 argv++; 570 argc--; 571 572 while (argc) { 573 if (write(fd, " ", 1) <= 0) 574 die("Failed writing sandbox script"); 575 576 kvm_write_sandbox_cmd_exactly(fd, argv[0]); 577 argv++; 578 argc--; 579 } 580 if (write(fd, "\n", 1) <= 0) 581 die("Failed writing sandbox script"); 582 583 close(fd); 584 } 585 586 static void kvm_run_set_real_cmdline(struct kvm *kvm) 587 { 588 static char real_cmdline[2048]; 589 bool video; 590 591 video = kvm->cfg.vnc || kvm->cfg.sdl || kvm->cfg.gtk; 592 593 memset(real_cmdline, 0, sizeof(real_cmdline)); 594 kvm__arch_set_cmdline(real_cmdline, video); 595 596 if (video) { 597 strcat(real_cmdline, " console=tty0"); 598 } else { 599 switch (kvm->cfg.active_console) { 600 case CONSOLE_HV: 601 /* Fallthrough */ 602 case CONSOLE_VIRTIO: 603 strcat(real_cmdline, " console=hvc0"); 604 break; 605 case CONSOLE_8250: 606 strcat(real_cmdline, " console=ttyS0"); 607 break; 608 } 609 } 610 611 if (kvm->cfg.using_rootfs) { 612 strcat(real_cmdline, " rw rootflags=trans=virtio,version=9p2000.L,cache=loose rootfstype=9p"); 613 if (kvm->cfg.custom_rootfs) { 614 #ifdef CONFIG_GUEST_PRE_INIT 615 strcat(real_cmdline, " init=/virt/pre_init"); 616 #else 617 strcat(real_cmdline, " init=/virt/init"); 618 #endif 619 if (!kvm->cfg.no_dhcp) 620 strcat(real_cmdline, " ip=dhcp"); 621 } 622 } else if (!kvm->cfg.kernel_cmdline || !strstr(kvm->cfg.kernel_cmdline, "root=")) { 623 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); 624 } 625 626 if (kvm->cfg.kernel_cmdline) { 627 strcat(real_cmdline, " "); 628 strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline)); 629 } 630 631 kvm->cfg.real_cmdline = real_cmdline; 632 } 633 634 static void kvm_run_validate_cfg(struct kvm *kvm) 635 { 636 u64 available_ram; 637 638 if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename) 639 die("Only one of --kernel or --firmware can be specified"); 640 641 if ((kvm->cfg.vnc && (kvm->cfg.sdl || kvm->cfg.gtk)) || 642 (kvm->cfg.sdl && kvm->cfg.gtk)) 643 die("Only one of --vnc, --sdl or --gtk can be specified"); 644 645 if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename) 646 pr_warning("Ignoring initrd file when loading a firmware image"); 647 648 if (kvm->cfg.ram_size) { 649 available_ram = host_ram_size(); 650 if (available_ram && kvm->cfg.ram_size > available_ram) { 651 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", 652 (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, 653 (unsigned long long)available_ram >> MB_SHIFT); 654 } 655 } 656 657 kvm__arch_validate_cfg(kvm); 658 } 659 660 static struct kvm *kvm_cmd_run_init(int argc, const char **argv) 661 { 662 static char default_name[20]; 663 unsigned int nr_online_cpus; 664 struct kvm *kvm = kvm__new(); 665 666 if (IS_ERR(kvm)) 667 return kvm; 668 669 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); 670 kvm->cfg.custom_rootfs_name = "default"; 671 /* 672 * An architecture can allow the user to set the RAM base address to 673 * zero. Initialize the address before parsing the command line 674 * arguments, otherwise it will be impossible to distinguish between the 675 * user setting the base address to zero or letting it unset and using 676 * the default value. 677 */ 678 kvm->cfg.ram_addr = kvm__arch_default_ram_address(); 679 680 while (argc != 0) { 681 BUILD_OPTIONS(options, &kvm->cfg, kvm); 682 argc = parse_options(argc, argv, options, run_usage, 683 PARSE_OPT_STOP_AT_NON_OPTION | 684 PARSE_OPT_KEEP_DASHDASH); 685 if (argc != 0) { 686 /* Cusrom options, should have been handled elsewhere */ 687 if (strcmp(argv[0], "--") == 0) { 688 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 689 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 690 kvm_run_write_sandbox_cmd(kvm, argv+1, argc-1); 691 break; 692 } 693 } 694 695 if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) || 696 (kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) { 697 pr_err("Cannot handle parameter: %s", argv[0]); 698 usage_with_options(run_usage, options); 699 free(kvm); 700 return ERR_PTR(-EINVAL); 701 } 702 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 703 /* 704 * first unhandled parameter is treated as 705 * sandbox command 706 */ 707 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 708 kvm_run_write_sandbox_cmd(kvm, argv, argc); 709 } else { 710 /* 711 * first unhandled parameter is treated as a kernel 712 * image 713 */ 714 kvm->cfg.kernel_filename = argv[0]; 715 } 716 argv++; 717 argc--; 718 } 719 720 } 721 722 kvm_run_validate_cfg(kvm); 723 724 if (!kvm->cfg.kernel_filename && !kvm->cfg.firmware_filename) { 725 kvm->cfg.kernel_filename = find_kernel(); 726 727 if (!kvm->cfg.kernel_filename) { 728 kernel_usage_with_options(); 729 return ERR_PTR(-EINVAL); 730 } 731 } 732 733 if (kvm->cfg.kernel_filename) { 734 kvm->cfg.vmlinux_filename = find_vmlinux(); 735 kvm->vmlinux = kvm->cfg.vmlinux_filename; 736 } 737 738 if (kvm->cfg.nrcpus == 0) 739 kvm->cfg.nrcpus = nr_online_cpus; 740 741 if (!kvm->cfg.ram_size) 742 kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus); 743 744 if (!kvm->cfg.dev) 745 kvm->cfg.dev = DEFAULT_KVM_DEV; 746 747 if (!kvm->cfg.console) 748 kvm->cfg.console = DEFAULT_CONSOLE; 749 750 if (!strncmp(kvm->cfg.console, "virtio", 6)) 751 kvm->cfg.active_console = CONSOLE_VIRTIO; 752 else if (!strncmp(kvm->cfg.console, "serial", 6)) 753 kvm->cfg.active_console = CONSOLE_8250; 754 else if (!strncmp(kvm->cfg.console, "hv", 2)) 755 kvm->cfg.active_console = CONSOLE_HV; 756 else 757 pr_warning("No console!"); 758 759 if (!kvm->cfg.host_ip) 760 kvm->cfg.host_ip = DEFAULT_HOST_ADDR; 761 762 if (!kvm->cfg.guest_ip) 763 kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR; 764 765 if (!kvm->cfg.guest_mac) 766 kvm->cfg.guest_mac = DEFAULT_GUEST_MAC; 767 768 if (!kvm->cfg.host_mac) 769 kvm->cfg.host_mac = DEFAULT_HOST_MAC; 770 771 if (!kvm->cfg.script) 772 kvm->cfg.script = DEFAULT_SCRIPT; 773 774 if (!kvm->cfg.network) 775 kvm->cfg.network = DEFAULT_NETWORK; 776 777 if (!kvm->cfg.guest_name) { 778 if (kvm->cfg.custom_rootfs) { 779 kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name; 780 } else { 781 sprintf(default_name, "guest-%u", getpid()); 782 kvm->cfg.guest_name = default_name; 783 } 784 } 785 786 if (!kvm->cfg.nodefaults && 787 !kvm->cfg.using_rootfs && 788 !kvm->cfg.disk_image[0].filename && 789 !kvm->cfg.initrd_filename) { 790 char tmp[PATH_MAX]; 791 792 kvm_setup_create_new(kvm->cfg.custom_rootfs_name); 793 kvm_setup_resolv(kvm->cfg.custom_rootfs_name); 794 795 snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default"); 796 if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) 797 die("Unable to initialize virtio 9p"); 798 if (virtio_9p__register(kvm, "/", "hostfs") < 0) 799 die("Unable to initialize virtio 9p"); 800 kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; 801 } 802 803 if (kvm->cfg.custom_rootfs) { 804 kvm_run_set_sandbox(kvm); 805 if (kvm_setup_guest_init(kvm->cfg.custom_rootfs_name)) 806 die("Failed to setup init for guest."); 807 } 808 809 if (kvm->cfg.nodefaults) 810 kvm->cfg.real_cmdline = kvm->cfg.kernel_cmdline; 811 else 812 kvm_run_set_real_cmdline(kvm); 813 814 if (kvm->cfg.kernel_filename) { 815 pr_info("# %s run -k %s -m %Lu -c %d --name %s", KVM_BINARY_NAME, 816 kvm->cfg.kernel_filename, 817 (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, 818 kvm->cfg.nrcpus, kvm->cfg.guest_name); 819 } else if (kvm->cfg.firmware_filename) { 820 pr_info("# %s run --firmware %s -m %Lu -c %d --name %s", KVM_BINARY_NAME, 821 kvm->cfg.firmware_filename, 822 (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, 823 kvm->cfg.nrcpus, kvm->cfg.guest_name); 824 } 825 826 if (init_list__init(kvm) < 0) 827 die ("Initialisation failed"); 828 829 return kvm; 830 } 831 832 static int kvm_cmd_run_work(struct kvm *kvm) 833 { 834 int i; 835 836 for (i = 0; i < kvm->nrcpus; i++) { 837 if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0) 838 die("unable to create KVM VCPU thread"); 839 } 840 841 /* Only VCPU #0 is going to exit by itself when shutting down */ 842 if (pthread_join(kvm->cpus[0]->thread, NULL) != 0) 843 die("unable to join with vcpu 0"); 844 845 return kvm_cpu__exit(kvm); 846 } 847 848 static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret) 849 { 850 compat__print_all_messages(); 851 852 init_list__exit(kvm); 853 854 if (guest_ret == 0) 855 pr_info("KVM session ended normally."); 856 } 857 858 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 859 { 860 int ret = -EFAULT; 861 struct kvm *kvm; 862 863 kvm = kvm_cmd_run_init(argc, argv); 864 if (IS_ERR(kvm)) 865 return PTR_ERR(kvm); 866 867 ret = kvm_cmd_run_work(kvm); 868 kvm_cmd_run_exit(kvm, ret); 869 870 return ret; 871 } 872