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