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