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