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/pci-shmem.h" 35 #include "kvm/kvm-ipc.h" 36 #include "kvm/builtin-debug.h" 37 38 #include <linux/types.h> 39 #include <linux/err.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 MB_SHIFT (20) 53 #define KB_SHIFT (10) 54 #define GB_SHIFT (30) 55 56 struct kvm *kvm; 57 __thread struct kvm_cpu *current_kvm_cpu; 58 59 static int kvm_run_wrapper; 60 61 bool do_debug_print = false; 62 63 static int vidmode = -1; 64 65 extern char _binary_guest_init_start; 66 extern char _binary_guest_init_size; 67 68 static const char * const run_usage[] = { 69 "lkvm run [<options>] [<kernel image>]", 70 NULL 71 }; 72 73 enum { 74 KVM_RUN_DEFAULT, 75 KVM_RUN_SANDBOX, 76 }; 77 78 static int img_name_parser(const struct option *opt, const char *arg, int unset) 79 { 80 char path[PATH_MAX]; 81 struct stat st; 82 struct kvm *kvm = opt->ptr; 83 84 if (stat(arg, &st) == 0 && 85 S_ISDIR(st.st_mode)) { 86 char tmp[PATH_MAX]; 87 88 if (kvm->cfg.using_rootfs) 89 die("Please use only one rootfs directory atmost"); 90 91 if (realpath(arg, tmp) == 0 || 92 virtio_9p__register(kvm, tmp, "/dev/root") < 0) 93 die("Unable to initialize virtio 9p"); 94 kvm->cfg.using_rootfs = 1; 95 return 0; 96 } 97 98 snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg); 99 100 if (stat(path, &st) == 0 && 101 S_ISDIR(st.st_mode)) { 102 char tmp[PATH_MAX]; 103 104 if (kvm->cfg.using_rootfs) 105 die("Please use only one rootfs directory atmost"); 106 107 if (realpath(path, tmp) == 0 || 108 virtio_9p__register(kvm, tmp, "/dev/root") < 0) 109 die("Unable to initialize virtio 9p"); 110 if (virtio_9p__register(kvm, "/", "hostfs") < 0) 111 die("Unable to initialize virtio 9p"); 112 kvm_setup_resolv(arg); 113 kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; 114 kvm->cfg.custom_rootfs_name = arg; 115 return 0; 116 } 117 118 return disk_img_name_parser(opt, arg, unset); 119 } 120 121 void kvm_run_set_wrapper_sandbox(void) 122 { 123 kvm_run_wrapper = KVM_RUN_SANDBOX; 124 } 125 126 static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset) 127 { 128 char *tag_name; 129 char tmp[PATH_MAX]; 130 131 /* 132 * 9p dir can be of the form dirname,tag_name or 133 * just dirname. In the later case we use the 134 * default tag name 135 */ 136 tag_name = strstr(arg, ","); 137 if (tag_name) { 138 *tag_name = '\0'; 139 tag_name++; 140 } 141 if (realpath(arg, tmp)) { 142 if (virtio_9p__register(kvm, tmp, tag_name) < 0) 143 die("Unable to initialize virtio 9p"); 144 } else 145 die("Failed resolving 9p path"); 146 return 0; 147 } 148 149 #define BUILD_OPTIONS(name, cfg, kvm) \ 150 struct option name[] = { \ 151 OPT_GROUP("Basic options:"), \ 152 OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name", \ 153 "A name for the guest"), \ 154 OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"), \ 155 OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory size\ 156 in MiB."), \ 157 OPT_CALLBACK('\0', "shmem", NULL, \ 158 "[pci:]<addr>:<size>[:handle=<handle>][:create]", \ 159 "Share host shmem with guest via pci device", \ 160 shmem_parser, NULL), \ 161 OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk \ 162 image or rootfs directory", img_name_parser, \ 163 kvm), \ 164 OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio \ 165 balloon"), \ 166 OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\ 167 OPT_BOOLEAN('\0', "sdl", &(cfg)->sdl, "Enable SDL framebuffer"),\ 168 OPT_BOOLEAN('\0', "rng", &(cfg)->virtio_rng, "Enable virtio Random\ 169 Number Generator"), \ 170 OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name", \ 171 "Enable virtio 9p to share files between host and \ 172 guest", virtio_9p_rootdir_parser, NULL), \ 173 OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or \ 174 hv", "Console to use"), \ 175 OPT_STRING('\0', "dev", &(cfg)->dev, "device_file", \ 176 "KVM device file"), \ 177 OPT_CALLBACK('\0', "tty", NULL, "tty id", \ 178 "Remap guest TTY into a pty on the host", \ 179 tty_parser, NULL), \ 180 OPT_STRING('\0', "sandbox", &(cfg)->sandbox, "script", \ 181 "Run this script when booting into custom \ 182 rootfs"), \ 183 OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path", \ 184 "Hugetlbfs path"), \ 185 \ 186 OPT_GROUP("Kernel options:"), \ 187 OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel", \ 188 "Kernel to boot in virtual machine"), \ 189 OPT_STRING('i', "initrd", &(cfg)->initrd_filename, "initrd", \ 190 "Initial RAM disk image"), \ 191 OPT_STRING('p', "params", &(cfg)->kernel_cmdline, "params", \ 192 "Kernel command line arguments"), \ 193 OPT_STRING('f', "firmware", &(cfg)->firmware_filename, "firmware",\ 194 "Firmware image to boot in virtual machine"), \ 195 \ 196 OPT_GROUP("Networking options:"), \ 197 OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params", \ 198 "Create a new guest NIC", \ 199 netdev_parser, NULL, kvm), \ 200 OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel DHCP\ 201 in rootfs mode"), \ 202 \ 203 OPT_GROUP("BIOS options:"), \ 204 OPT_INTEGER('\0', "vidmode", &vidmode, \ 205 "Video mode"), \ 206 \ 207 OPT_GROUP("Debug options:"), \ 208 OPT_BOOLEAN('\0', "debug", &do_debug_print, \ 209 "Enable debug messages"), \ 210 OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step, \ 211 "Enable single stepping"), \ 212 OPT_BOOLEAN('\0', "debug-ioport", &(cfg)->ioport_debug, \ 213 "Enable ioport debugging"), \ 214 OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug, \ 215 "Enable MMIO debugging"), \ 216 OPT_INTEGER('\0', "debug-iodelay", &(cfg)->debug_iodelay, \ 217 "Delay IO by millisecond"), \ 218 OPT_END() \ 219 }; 220 221 /* 222 * Serialize debug printout so that the output of multiple vcpus does not 223 * get mixed up: 224 */ 225 static int printout_done; 226 227 static void handle_sigusr1(int sig) 228 { 229 struct kvm_cpu *cpu = current_kvm_cpu; 230 int fd = kvm_cpu__get_debug_fd(); 231 232 if (!cpu || cpu->needs_nmi) 233 return; 234 235 dprintf(fd, "\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id); 236 kvm_cpu__show_registers(cpu); 237 kvm_cpu__show_code(cpu); 238 kvm_cpu__show_page_tables(cpu); 239 fflush(stdout); 240 printout_done = 1; 241 mb(); 242 } 243 244 /* Pause/resume the guest using SIGUSR2 */ 245 static int is_paused; 246 247 static void handle_pause(int fd, u32 type, u32 len, u8 *msg) 248 { 249 if (WARN_ON(len)) 250 return; 251 252 if (type == KVM_IPC_RESUME && is_paused) { 253 kvm->vm_state = KVM_VMSTATE_RUNNING; 254 kvm__continue(); 255 } else if (type == KVM_IPC_PAUSE && !is_paused) { 256 kvm->vm_state = KVM_VMSTATE_PAUSED; 257 ioctl(kvm->vm_fd, KVM_KVMCLOCK_CTRL); 258 kvm__pause(); 259 } else { 260 return; 261 } 262 263 is_paused = !is_paused; 264 } 265 266 static void handle_vmstate(int fd, u32 type, u32 len, u8 *msg) 267 { 268 int r = 0; 269 270 if (type == KVM_IPC_VMSTATE) 271 r = write(fd, &kvm->vm_state, sizeof(kvm->vm_state)); 272 273 if (r < 0) 274 pr_warning("Failed sending VMSTATE"); 275 } 276 277 static void handle_debug(int fd, u32 type, u32 len, u8 *msg) 278 { 279 int i; 280 struct debug_cmd_params *params; 281 u32 dbg_type; 282 u32 vcpu; 283 284 if (WARN_ON(type != KVM_IPC_DEBUG || len != sizeof(*params))) 285 return; 286 287 params = (void *)msg; 288 dbg_type = params->dbg_type; 289 vcpu = params->cpu; 290 291 if (dbg_type & KVM_DEBUG_CMD_TYPE_SYSRQ) 292 serial8250__inject_sysrq(kvm, params->sysrq); 293 294 if (dbg_type & KVM_DEBUG_CMD_TYPE_NMI) { 295 if ((int)vcpu >= kvm->nrcpus) 296 return; 297 298 kvm->cpus[vcpu]->needs_nmi = 1; 299 pthread_kill(kvm->cpus[vcpu]->thread, SIGUSR1); 300 } 301 302 if (!(dbg_type & KVM_DEBUG_CMD_TYPE_DUMP)) 303 return; 304 305 for (i = 0; i < kvm->nrcpus; i++) { 306 struct kvm_cpu *cpu = kvm->cpus[i]; 307 308 if (!cpu) 309 continue; 310 311 printout_done = 0; 312 313 kvm_cpu__set_debug_fd(fd); 314 pthread_kill(cpu->thread, SIGUSR1); 315 /* 316 * Wait for the vCPU to dump state before signalling 317 * the next thread. Since this is debug code it does 318 * not matter that we are burning CPU time a bit: 319 */ 320 while (!printout_done) 321 mb(); 322 } 323 324 close(fd); 325 326 serial8250__inject_sysrq(kvm, 'p'); 327 } 328 329 static void handle_sigalrm(int sig) 330 { 331 kvm__arch_periodic_poll(kvm); 332 } 333 334 static void handle_stop(int fd, u32 type, u32 len, u8 *msg) 335 { 336 if (WARN_ON(type != KVM_IPC_STOP || len)) 337 return; 338 339 kvm_cpu__reboot(kvm); 340 } 341 342 static void *kvm_cpu_thread(void *arg) 343 { 344 current_kvm_cpu = arg; 345 346 if (kvm_cpu__start(current_kvm_cpu)) 347 goto panic_kvm; 348 349 return (void *) (intptr_t) 0; 350 351 panic_kvm: 352 fprintf(stderr, "KVM exit reason: %u (\"%s\")\n", 353 current_kvm_cpu->kvm_run->exit_reason, 354 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 355 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 356 fprintf(stderr, "KVM exit code: 0x%Lu\n", 357 current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 358 359 kvm_cpu__set_debug_fd(STDOUT_FILENO); 360 kvm_cpu__show_registers(current_kvm_cpu); 361 kvm_cpu__show_code(current_kvm_cpu); 362 kvm_cpu__show_page_tables(current_kvm_cpu); 363 364 return (void *) (intptr_t) 1; 365 } 366 367 static char kernel[PATH_MAX]; 368 369 static const char *host_kernels[] = { 370 "/boot/vmlinuz", 371 "/boot/bzImage", 372 NULL 373 }; 374 375 static const char *default_kernels[] = { 376 "./bzImage", 377 "arch/" BUILD_ARCH "/boot/bzImage", 378 "../../arch/" BUILD_ARCH "/boot/bzImage", 379 NULL 380 }; 381 382 static const char *default_vmlinux[] = { 383 "vmlinux", 384 "../../../vmlinux", 385 "../../vmlinux", 386 NULL 387 }; 388 389 static void kernel_usage_with_options(void) 390 { 391 const char **k; 392 struct utsname uts; 393 394 fprintf(stderr, "Fatal: could not find default kernel image in:\n"); 395 k = &default_kernels[0]; 396 while (*k) { 397 fprintf(stderr, "\t%s\n", *k); 398 k++; 399 } 400 401 if (uname(&uts) < 0) 402 return; 403 404 k = &host_kernels[0]; 405 while (*k) { 406 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 407 return; 408 fprintf(stderr, "\t%s\n", kernel); 409 k++; 410 } 411 fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n", 412 KVM_BINARY_NAME); 413 } 414 415 static u64 host_ram_size(void) 416 { 417 long page_size; 418 long nr_pages; 419 420 nr_pages = sysconf(_SC_PHYS_PAGES); 421 if (nr_pages < 0) { 422 pr_warning("sysconf(_SC_PHYS_PAGES) failed"); 423 return 0; 424 } 425 426 page_size = sysconf(_SC_PAGE_SIZE); 427 if (page_size < 0) { 428 pr_warning("sysconf(_SC_PAGE_SIZE) failed"); 429 return 0; 430 } 431 432 return (nr_pages * page_size) >> MB_SHIFT; 433 } 434 435 /* 436 * If user didn't specify how much memory it wants to allocate for the guest, 437 * avoid filling the whole host RAM. 438 */ 439 #define RAM_SIZE_RATIO 0.8 440 441 static u64 get_ram_size(int nr_cpus) 442 { 443 u64 available; 444 u64 ram_size; 445 446 ram_size = 64 * (nr_cpus + 3); 447 448 available = host_ram_size() * RAM_SIZE_RATIO; 449 if (!available) 450 available = MIN_RAM_SIZE_MB; 451 452 if (ram_size > available) 453 ram_size = available; 454 455 return ram_size; 456 } 457 458 static const char *find_kernel(void) 459 { 460 const char **k; 461 struct stat st; 462 struct utsname uts; 463 464 k = &default_kernels[0]; 465 while (*k) { 466 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) { 467 k++; 468 continue; 469 } 470 strncpy(kernel, *k, PATH_MAX); 471 return kernel; 472 } 473 474 if (uname(&uts) < 0) 475 return NULL; 476 477 k = &host_kernels[0]; 478 while (*k) { 479 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 480 return NULL; 481 482 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) { 483 k++; 484 continue; 485 } 486 return kernel; 487 488 } 489 return NULL; 490 } 491 492 static const char *find_vmlinux(void) 493 { 494 const char **vmlinux; 495 496 vmlinux = &default_vmlinux[0]; 497 while (*vmlinux) { 498 struct stat st; 499 500 if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) { 501 vmlinux++; 502 continue; 503 } 504 return *vmlinux; 505 } 506 return NULL; 507 } 508 509 void kvm_run_help(void) 510 { 511 BUILD_OPTIONS(options, &kvm->cfg, kvm); 512 usage_with_options(run_usage, options); 513 } 514 515 static int kvm_setup_guest_init(void) 516 { 517 const char *rootfs = kvm->cfg.custom_rootfs_name; 518 char tmp[PATH_MAX]; 519 size_t size; 520 int fd, ret; 521 char *data; 522 523 /* Setup /virt/init */ 524 size = (size_t)&_binary_guest_init_size; 525 data = (char *)&_binary_guest_init_start; 526 snprintf(tmp, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), rootfs); 527 remove(tmp); 528 fd = open(tmp, O_CREAT | O_WRONLY, 0755); 529 if (fd < 0) 530 die("Fail to setup %s", tmp); 531 ret = xwrite(fd, data, size); 532 if (ret < 0) 533 die("Fail to setup %s", tmp); 534 close(fd); 535 536 return 0; 537 } 538 539 static int kvm_run_set_sandbox(void) 540 { 541 const char *guestfs_name = kvm->cfg.custom_rootfs_name; 542 char path[PATH_MAX], script[PATH_MAX], *tmp; 543 544 snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name); 545 546 remove(path); 547 548 if (kvm->cfg.sandbox == NULL) 549 return 0; 550 551 tmp = realpath(kvm->cfg.sandbox, NULL); 552 if (tmp == NULL) 553 return -ENOMEM; 554 555 snprintf(script, PATH_MAX, "/host/%s", tmp); 556 free(tmp); 557 558 return symlink(script, path); 559 } 560 561 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg) 562 { 563 const char *single_quote; 564 565 if (!*arg) { /* zero length string */ 566 if (write(fd, "''", 2) <= 0) 567 die("Failed writing sandbox script"); 568 return; 569 } 570 571 while (*arg) { 572 single_quote = strchrnul(arg, '\''); 573 574 /* write non-single-quote string as #('string') */ 575 if (arg != single_quote) { 576 if (write(fd, "'", 1) <= 0 || 577 write(fd, arg, single_quote - arg) <= 0 || 578 write(fd, "'", 1) <= 0) 579 die("Failed writing sandbox script"); 580 } 581 582 /* write single quote as #("'") */ 583 if (*single_quote) { 584 if (write(fd, "\"'\"", 3) <= 0) 585 die("Failed writing sandbox script"); 586 } else 587 break; 588 589 arg = single_quote + 1; 590 } 591 } 592 593 static void resolve_program(const char *src, char *dst, size_t len) 594 { 595 struct stat st; 596 int err; 597 598 err = stat(src, &st); 599 600 if (!err && S_ISREG(st.st_mode)) { 601 char resolved_path[PATH_MAX]; 602 603 if (!realpath(src, resolved_path)) 604 die("Unable to resolve program %s: %s\n", src, strerror(errno)); 605 606 snprintf(dst, len, "/host%s", resolved_path); 607 } else 608 strncpy(dst, src, len); 609 } 610 611 static void kvm_run_write_sandbox_cmd(const char **argv, int argc) 612 { 613 const char script_hdr[] = "#! /bin/bash\n\n"; 614 char program[PATH_MAX]; 615 int fd; 616 617 remove(kvm->cfg.sandbox); 618 619 fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777); 620 if (fd < 0) 621 die("Failed creating sandbox script"); 622 623 if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0) 624 die("Failed writing sandbox script"); 625 626 resolve_program(argv[0], program, PATH_MAX); 627 kvm_write_sandbox_cmd_exactly(fd, program); 628 629 argv++; 630 argc--; 631 632 while (argc) { 633 if (write(fd, " ", 1) <= 0) 634 die("Failed writing sandbox script"); 635 636 kvm_write_sandbox_cmd_exactly(fd, argv[0]); 637 argv++; 638 argc--; 639 } 640 if (write(fd, "\n", 1) <= 0) 641 die("Failed writing sandbox script"); 642 643 close(fd); 644 } 645 646 static int kvm_cmd_run_init(int argc, const char **argv) 647 { 648 static char real_cmdline[2048], default_name[20]; 649 struct framebuffer *fb = NULL; 650 unsigned int nr_online_cpus; 651 int r; 652 653 kvm = kvm__new(); 654 if (IS_ERR(kvm)) 655 return PTR_ERR(kvm); 656 657 signal(SIGALRM, handle_sigalrm); 658 kvm_ipc__register_handler(KVM_IPC_DEBUG, handle_debug); 659 signal(SIGUSR1, handle_sigusr1); 660 kvm_ipc__register_handler(KVM_IPC_PAUSE, handle_pause); 661 kvm_ipc__register_handler(KVM_IPC_RESUME, handle_pause); 662 kvm_ipc__register_handler(KVM_IPC_STOP, handle_stop); 663 kvm_ipc__register_handler(KVM_IPC_VMSTATE, handle_vmstate); 664 665 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); 666 kvm->cfg.custom_rootfs_name = "default"; 667 668 while (argc != 0) { 669 BUILD_OPTIONS(options, &kvm->cfg, kvm); 670 argc = parse_options(argc, argv, options, run_usage, 671 PARSE_OPT_STOP_AT_NON_OPTION | 672 PARSE_OPT_KEEP_DASHDASH); 673 if (argc != 0) { 674 /* Cusrom options, should have been handled elsewhere */ 675 if (strcmp(argv[0], "--") == 0) { 676 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 677 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 678 kvm_run_write_sandbox_cmd(argv+1, argc-1); 679 break; 680 } 681 } 682 683 if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) || 684 (kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) { 685 fprintf(stderr, "Cannot handle parameter: " 686 "%s\n", argv[0]); 687 usage_with_options(run_usage, options); 688 free(kvm); 689 return -EINVAL; 690 } 691 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 692 /* 693 * first unhandled parameter is treated as 694 * sandbox command 695 */ 696 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 697 kvm_run_write_sandbox_cmd(argv, argc); 698 } else { 699 /* 700 * first unhandled parameter is treated as a kernel 701 * image 702 */ 703 kvm->cfg.kernel_filename = argv[0]; 704 } 705 argv++; 706 argc--; 707 } 708 709 } 710 711 kvm->nr_disks = kvm->cfg.image_count; 712 713 if (!kvm->cfg.kernel_filename) 714 kvm->cfg.kernel_filename = find_kernel(); 715 716 if (!kvm->cfg.kernel_filename) { 717 kernel_usage_with_options(); 718 return -EINVAL; 719 } 720 721 kvm->cfg.vmlinux_filename = find_vmlinux(); 722 723 if (kvm->cfg.nrcpus == 0) 724 kvm->cfg.nrcpus = nr_online_cpus; 725 726 if (!kvm->cfg.ram_size) 727 kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus); 728 729 if (kvm->cfg.ram_size < MIN_RAM_SIZE_MB) 730 die("Not enough memory specified: %lluMB (min %lluMB)", kvm->cfg.ram_size, MIN_RAM_SIZE_MB); 731 732 if (kvm->cfg.ram_size > host_ram_size()) 733 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", kvm->cfg.ram_size, host_ram_size()); 734 735 kvm->cfg.ram_size <<= MB_SHIFT; 736 737 if (!kvm->cfg.dev) 738 kvm->cfg.dev = DEFAULT_KVM_DEV; 739 740 if (!kvm->cfg.console) 741 kvm->cfg.console = DEFAULT_CONSOLE; 742 743 if (!strncmp(kvm->cfg.console, "virtio", 6)) 744 kvm->cfg.active_console = CONSOLE_VIRTIO; 745 else if (!strncmp(kvm->cfg.console, "serial", 6)) 746 kvm->cfg.active_console = CONSOLE_8250; 747 else if (!strncmp(kvm->cfg.console, "hv", 2)) 748 kvm->cfg.active_console = CONSOLE_HV; 749 else 750 pr_warning("No console!"); 751 752 if (!kvm->cfg.host_ip) 753 kvm->cfg.host_ip = DEFAULT_HOST_ADDR; 754 755 if (!kvm->cfg.guest_ip) 756 kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR; 757 758 if (!kvm->cfg.guest_mac) 759 kvm->cfg.guest_mac = DEFAULT_GUEST_MAC; 760 761 if (!kvm->cfg.host_mac) 762 kvm->cfg.host_mac = DEFAULT_HOST_MAC; 763 764 if (!kvm->cfg.script) 765 kvm->cfg.script = DEFAULT_SCRIPT; 766 767 r = term_init(kvm); 768 if (r < 0) { 769 pr_err("term_init() failed with error %d\n", r); 770 goto fail; 771 } 772 773 if (!kvm->cfg.guest_name) { 774 if (kvm->cfg.custom_rootfs) { 775 kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name; 776 } else { 777 sprintf(default_name, "guest-%u", getpid()); 778 kvm->cfg.guest_name = default_name; 779 } 780 } 781 782 r = kvm__init(kvm); 783 if (r) 784 goto fail; 785 786 r = ioeventfd__init(kvm); 787 if (r < 0) { 788 pr_err("ioeventfd__init() failed with error %d\n", r); 789 goto fail; 790 } 791 792 r = kvm_cpu__init(kvm); 793 if (r < 0) { 794 pr_err("kvm_cpu__init() failed with error %d\n", r); 795 goto fail; 796 } 797 798 r = irq__init(kvm); 799 if (r < 0) { 800 pr_err("irq__init() failed with error %d\n", r); 801 goto fail; 802 } 803 804 r = pci__init(kvm); 805 if (r < 0) { 806 pr_err("pci__init() failed with error %d\n", r); 807 goto fail; 808 } 809 810 r = ioport__init(kvm); 811 if (r < 0) { 812 pr_err("ioport__init() failed with error %d\n", r); 813 goto fail; 814 } 815 816 /* 817 * vidmode should be either specified 818 * either set by default 819 */ 820 if (kvm->cfg.vnc || kvm->cfg.sdl) { 821 if (vidmode == -1) 822 vidmode = 0x312; 823 } else { 824 vidmode = 0; 825 } 826 827 memset(real_cmdline, 0, sizeof(real_cmdline)); 828 kvm__arch_set_cmdline(real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl); 829 830 if (strlen(real_cmdline) > 0) 831 strcat(real_cmdline, " "); 832 833 if (kvm->cfg.kernel_cmdline) 834 strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline)); 835 836 if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) { 837 char tmp[PATH_MAX]; 838 839 kvm_setup_create_new(kvm->cfg.custom_rootfs_name); 840 kvm_setup_resolv(kvm->cfg.custom_rootfs_name); 841 842 snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default"); 843 if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) 844 die("Unable to initialize virtio 9p"); 845 if (virtio_9p__register(kvm, "/", "hostfs") < 0) 846 die("Unable to initialize virtio 9p"); 847 kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; 848 } 849 850 if (kvm->cfg.using_rootfs) { 851 strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p"); 852 if (kvm->cfg.custom_rootfs) { 853 kvm_run_set_sandbox(); 854 855 strcat(real_cmdline, " init=/virt/init"); 856 857 if (!kvm->cfg.no_dhcp) 858 strcat(real_cmdline, " ip=dhcp"); 859 if (kvm_setup_guest_init()) 860 die("Failed to setup init for guest."); 861 } 862 } else if (!strstr(real_cmdline, "root=")) { 863 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); 864 } 865 866 r = disk_image__init(kvm); 867 if (r < 0) { 868 pr_err("disk_image__init() failed with error %d\n", r); 869 goto fail; 870 } 871 872 printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, 873 kvm->cfg.kernel_filename, kvm->cfg.ram_size / 1024 / 1024, kvm->cfg.nrcpus, kvm->cfg.guest_name); 874 875 if (!kvm->cfg.firmware_filename) { 876 if (!kvm__load_kernel(kvm, kvm->cfg.kernel_filename, 877 kvm->cfg.initrd_filename, real_cmdline, vidmode)) 878 die("unable to load kernel %s", kvm->cfg.kernel_filename); 879 880 kvm->vmlinux = kvm->cfg.vmlinux_filename; 881 r = symbol_init(kvm); 882 if (r < 0) 883 pr_debug("symbol_init() failed with error %d\n", r); 884 } 885 886 ioport__setup_arch(); 887 888 r = rtc__init(kvm); 889 if (r < 0) { 890 pr_err("rtc__init() failed with error %d\n", r); 891 goto fail; 892 } 893 894 r = serial8250__init(kvm); 895 if (r < 0) { 896 pr_err("serial__init() failed with error %d\n", r); 897 goto fail; 898 } 899 900 r = virtio_blk__init(kvm); 901 if (r < 0) { 902 pr_err("virtio_blk__init() failed with error %d\n", r); 903 goto fail; 904 } 905 906 r = virtio_scsi_init(kvm); 907 if (r < 0) { 908 pr_err("virtio_scsi_init() failed with error %d\n", r); 909 goto fail; 910 } 911 912 r = virtio_console__init(kvm); 913 if (r < 0) { 914 pr_err("virtio_console__init() failed with error %d\n", r); 915 goto fail; 916 } 917 918 r = virtio_rng__init(kvm); 919 if (r < 0) { 920 pr_err("virtio_rng__init() failed with error %d\n", r); 921 goto fail; 922 } 923 924 r = virtio_bln__init(kvm); 925 if (r < 0) { 926 pr_err("virtio_rng__init() failed with error %d\n", r); 927 goto fail; 928 } 929 930 if (!kvm->cfg.network) 931 kvm->cfg.network = DEFAULT_NETWORK; 932 933 virtio_9p__init(kvm); 934 935 r = virtio_net__init(kvm); 936 if (r < 0) { 937 pr_err("virtio_net__init() failed with error %d\n", r); 938 goto fail; 939 } 940 941 kvm__init_ram(kvm); 942 943 #ifdef CONFIG_X86 944 kbd__init(kvm); 945 #endif 946 947 r = pci_shmem__init(kvm); 948 if (r < 0) { 949 pr_err("pci_shmem__init() failed with error %d\n", r); 950 goto fail; 951 } 952 953 if (kvm->cfg.vnc || kvm->cfg.sdl) { 954 fb = vesa__init(kvm); 955 if (IS_ERR(fb)) { 956 pr_err("vesa__init() failed with error %ld\n", PTR_ERR(fb)); 957 goto fail; 958 } 959 } 960 961 if (kvm->cfg.vnc && fb) { 962 r = vnc__init(fb); 963 if (r < 0) { 964 pr_err("vnc__init() failed with error %d\n", r); 965 goto fail; 966 } 967 } 968 969 if (kvm->cfg.sdl && fb) { 970 sdl__init(fb); 971 if (r < 0) { 972 pr_err("sdl__init() failed with error %d\n", r); 973 goto fail; 974 } 975 } 976 977 r = fb__init(kvm); 978 if (r < 0) { 979 pr_err("fb__init() failed with error %d\n", r); 980 goto fail; 981 } 982 983 /* 984 * Device init all done; firmware init must 985 * come after this (it may set up device trees etc.) 986 */ 987 988 r = kvm_timer__init(kvm); 989 if (r < 0) { 990 pr_err("kvm_timer__init() failed with error %d\n", r); 991 goto fail; 992 } 993 994 if (kvm->cfg.firmware_filename) { 995 if (!kvm__load_firmware(kvm, kvm->cfg.firmware_filename)) 996 die("unable to load firmware image %s: %s", kvm->cfg.firmware_filename, strerror(errno)); 997 } else { 998 kvm__arch_setup_firmware(kvm); 999 if (r < 0) { 1000 pr_err("kvm__arch_setup_firmware() failed with error %d\n", r); 1001 goto fail; 1002 } 1003 } 1004 1005 r = thread_pool__init(kvm); 1006 if (r < 0) { 1007 pr_err("thread_pool__init() failed with error %d\n", r); 1008 goto fail; 1009 } 1010 1011 fail: 1012 return r; 1013 } 1014 1015 static int kvm_cmd_run_work(void) 1016 { 1017 int i; 1018 void *ret = NULL; 1019 1020 for (i = 0; i < kvm->nrcpus; i++) { 1021 if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0) 1022 die("unable to create KVM VCPU thread"); 1023 } 1024 1025 /* Only VCPU #0 is going to exit by itself when shutting down */ 1026 return pthread_join(kvm->cpus[0]->thread, &ret); 1027 } 1028 1029 static void kvm_cmd_run_exit(int guest_ret) 1030 { 1031 int r = 0; 1032 1033 compat__print_all_messages(); 1034 1035 r = kvm_cpu__exit(kvm); 1036 if (r < 0) 1037 pr_warning("kvm_cpu__exit() failed with error %d\n", r); 1038 1039 r = symbol_exit(kvm); 1040 if (r < 0) 1041 pr_warning("symbol_exit() failed with error %d\n", r); 1042 1043 r = irq__exit(kvm); 1044 if (r < 0) 1045 pr_warning("irq__exit() failed with error %d\n", r); 1046 1047 r = kvm_timer__exit(kvm); 1048 if (r < 0) 1049 pr_warning("kvm_timer__exit() failed with error %d\n", r); 1050 1051 r = fb__exit(kvm); 1052 if (r < 0) 1053 pr_warning("kvm_timer__exit() failed with error %d\n", r); 1054 1055 r = virtio_net__exit(kvm); 1056 if (r < 0) 1057 pr_warning("virtio_net__exit() failed with error %d\n", r); 1058 1059 r = virtio_scsi_exit(kvm); 1060 if (r < 0) 1061 pr_warning("virtio_scsi_exit() failed with error %d\n", r); 1062 1063 r = virtio_blk__exit(kvm); 1064 if (r < 0) 1065 pr_warning("virtio_blk__exit() failed with error %d\n", r); 1066 1067 r = virtio_rng__exit(kvm); 1068 if (r < 0) 1069 pr_warning("virtio_rng__exit() failed with error %d\n", r); 1070 1071 r = virtio_bln__exit(kvm); 1072 if (r < 0) 1073 pr_warning("virtio_bln__exit() failed with error %d\n", r); 1074 1075 r = virtio_console__exit(kvm); 1076 if (r < 0) 1077 pr_warning("virtio_console__exit() failed with error %d\n", r); 1078 1079 r = pci_shmem__exit(kvm); 1080 if (r < 0) 1081 pr_warning("pci_shmem__exit() failed with error %d\n", r); 1082 1083 r = disk_image__exit(kvm); 1084 if (r < 0) 1085 pr_warning("disk_image__exit() failed with error %d\n", r); 1086 1087 r = serial8250__exit(kvm); 1088 if (r < 0) 1089 pr_warning("serial8250__exit() failed with error %d\n", r); 1090 1091 r = rtc__exit(kvm); 1092 if (r < 0) 1093 pr_warning("rtc__exit() failed with error %d\n", r); 1094 1095 r = kvm__arch_free_firmware(kvm); 1096 if (r < 0) 1097 pr_warning("kvm__arch_free_firmware() failed with error %d\n", r); 1098 1099 r = ioport__exit(kvm); 1100 if (r < 0) 1101 pr_warning("ioport__exit() failed with error %d\n", r); 1102 1103 r = ioeventfd__exit(kvm); 1104 if (r < 0) 1105 pr_warning("ioeventfd__exit() failed with error %d\n", r); 1106 1107 r = pci__exit(kvm); 1108 if (r < 0) 1109 pr_warning("pci__exit() failed with error %d\n", r); 1110 1111 r = term_exit(kvm); 1112 if (r < 0) 1113 pr_warning("pci__exit() failed with error %d\n", r); 1114 1115 r = thread_pool__exit(kvm); 1116 if (r < 0) 1117 pr_warning("thread_pool__exit() failed with error %d\n", r); 1118 1119 r = kvm__exit(kvm); 1120 if (r < 0) 1121 pr_warning("pci__exit() failed with error %d\n", r); 1122 1123 if (guest_ret == 0) 1124 printf("\n # KVM session ended normally.\n"); 1125 } 1126 1127 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 1128 { 1129 int r, ret = -EFAULT; 1130 1131 r = kvm_cmd_run_init(argc, argv); 1132 if (r < 0) 1133 return r; 1134 1135 ret = kvm_cmd_run_work(); 1136 kvm_cmd_run_exit(ret); 1137 1138 return ret; 1139 } 1140