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 struct kvm *kvm_cmd_run_init(int argc, const char **argv) 459 { 460 static char real_cmdline[2048], default_name[20]; 461 unsigned int nr_online_cpus; 462 struct kvm *kvm = kvm__new(); 463 bool video; 464 465 if (IS_ERR(kvm)) 466 return kvm; 467 468 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); 469 kvm->cfg.custom_rootfs_name = "default"; 470 471 while (argc != 0) { 472 BUILD_OPTIONS(options, &kvm->cfg, kvm); 473 argc = parse_options(argc, argv, options, run_usage, 474 PARSE_OPT_STOP_AT_NON_OPTION | 475 PARSE_OPT_KEEP_DASHDASH); 476 if (argc != 0) { 477 /* Cusrom options, should have been handled elsewhere */ 478 if (strcmp(argv[0], "--") == 0) { 479 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 480 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 481 kvm_run_write_sandbox_cmd(kvm, argv+1, argc-1); 482 break; 483 } 484 } 485 486 if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) || 487 (kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) { 488 fprintf(stderr, "Cannot handle parameter: " 489 "%s\n", argv[0]); 490 usage_with_options(run_usage, options); 491 free(kvm); 492 return ERR_PTR(-EINVAL); 493 } 494 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 495 /* 496 * first unhandled parameter is treated as 497 * sandbox command 498 */ 499 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 500 kvm_run_write_sandbox_cmd(kvm, argv, argc); 501 } else { 502 /* 503 * first unhandled parameter is treated as a kernel 504 * image 505 */ 506 kvm->cfg.kernel_filename = argv[0]; 507 } 508 argv++; 509 argc--; 510 } 511 512 } 513 514 kvm->nr_disks = kvm->cfg.image_count; 515 516 if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename) 517 die("Only one of --kernel or --firmware can be specified"); 518 519 if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename) 520 pr_warning("Ignoring initrd file when loading a firmware image"); 521 522 if (!kvm->cfg.kernel_filename && !kvm->cfg.firmware_filename) { 523 kvm->cfg.kernel_filename = find_kernel(); 524 525 if (!kvm->cfg.kernel_filename) { 526 kernel_usage_with_options(); 527 return ERR_PTR(-EINVAL); 528 } 529 } 530 531 if (kvm->cfg.kernel_filename) { 532 kvm->cfg.vmlinux_filename = find_vmlinux(); 533 kvm->vmlinux = kvm->cfg.vmlinux_filename; 534 } 535 536 if (kvm->cfg.nrcpus == 0) 537 kvm->cfg.nrcpus = nr_online_cpus; 538 539 if (!kvm->cfg.ram_size) 540 kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus); 541 542 if (kvm->cfg.ram_size > host_ram_size()) 543 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", 544 (unsigned long long)kvm->cfg.ram_size, 545 (unsigned long long)host_ram_size()); 546 547 kvm->cfg.ram_size <<= MB_SHIFT; 548 549 if (!kvm->cfg.dev) 550 kvm->cfg.dev = DEFAULT_KVM_DEV; 551 552 if (!kvm->cfg.console) 553 kvm->cfg.console = DEFAULT_CONSOLE; 554 555 video = kvm->cfg.vnc || kvm->cfg.sdl || kvm->cfg.gtk; 556 if (video) { 557 if ((kvm->cfg.vnc && (kvm->cfg.sdl || kvm->cfg.gtk)) || 558 (kvm->cfg.sdl && kvm->cfg.gtk)) 559 die("Only one of --vnc, --sdl or --gtk can be specified"); 560 } 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 memset(real_cmdline, 0, sizeof(real_cmdline)); 590 kvm__arch_set_cmdline(real_cmdline, video); 591 592 if (video) { 593 strcat(real_cmdline, " console=tty0"); 594 } else { 595 switch (kvm->cfg.active_console) { 596 case CONSOLE_HV: 597 /* Fallthrough */ 598 case CONSOLE_VIRTIO: 599 strcat(real_cmdline, " console=hvc0"); 600 break; 601 case CONSOLE_8250: 602 strcat(real_cmdline, " console=ttyS0"); 603 break; 604 } 605 } 606 607 if (!kvm->cfg.guest_name) { 608 if (kvm->cfg.custom_rootfs) { 609 kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name; 610 } else { 611 sprintf(default_name, "guest-%u", getpid()); 612 kvm->cfg.guest_name = default_name; 613 } 614 } 615 616 if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) { 617 char tmp[PATH_MAX]; 618 619 kvm_setup_create_new(kvm->cfg.custom_rootfs_name); 620 kvm_setup_resolv(kvm->cfg.custom_rootfs_name); 621 622 snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default"); 623 if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) 624 die("Unable to initialize virtio 9p"); 625 if (virtio_9p__register(kvm, "/", "hostfs") < 0) 626 die("Unable to initialize virtio 9p"); 627 kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; 628 } 629 630 if (kvm->cfg.using_rootfs) { 631 strcat(real_cmdline, " rw rootflags=trans=virtio,version=9p2000.L,cache=loose rootfstype=9p"); 632 if (kvm->cfg.custom_rootfs) { 633 kvm_run_set_sandbox(kvm); 634 635 #ifdef CONFIG_GUEST_PRE_INIT 636 strcat(real_cmdline, " init=/virt/pre_init"); 637 #else 638 strcat(real_cmdline, " init=/virt/init"); 639 #endif 640 641 if (!kvm->cfg.no_dhcp) 642 strcat(real_cmdline, " ip=dhcp"); 643 if (kvm_setup_guest_init(kvm->cfg.custom_rootfs_name)) 644 die("Failed to setup init for guest."); 645 } 646 } else if (!kvm->cfg.kernel_cmdline || !strstr(kvm->cfg.kernel_cmdline, "root=")) { 647 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); 648 } 649 650 if (kvm->cfg.kernel_cmdline) { 651 strcat(real_cmdline, " "); 652 strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline)); 653 } 654 655 kvm->cfg.real_cmdline = real_cmdline; 656 657 if (kvm->cfg.kernel_filename) { 658 printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, 659 kvm->cfg.kernel_filename, 660 (unsigned long long)kvm->cfg.ram_size / 1024 / 1024, 661 kvm->cfg.nrcpus, kvm->cfg.guest_name); 662 } else if (kvm->cfg.firmware_filename) { 663 printf(" # %s run --firmware %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, 664 kvm->cfg.firmware_filename, 665 (unsigned long long)kvm->cfg.ram_size / 1024 / 1024, 666 kvm->cfg.nrcpus, kvm->cfg.guest_name); 667 } 668 669 if (init_list__init(kvm) < 0) 670 die ("Initialisation failed"); 671 672 return kvm; 673 } 674 675 static int kvm_cmd_run_work(struct kvm *kvm) 676 { 677 int i; 678 679 for (i = 0; i < kvm->nrcpus; i++) { 680 if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0) 681 die("unable to create KVM VCPU thread"); 682 } 683 684 /* Only VCPU #0 is going to exit by itself when shutting down */ 685 if (pthread_join(kvm->cpus[0]->thread, NULL) != 0) 686 die("unable to join with vcpu 0"); 687 688 return kvm_cpu__exit(kvm); 689 } 690 691 static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret) 692 { 693 compat__print_all_messages(); 694 695 init_list__exit(kvm); 696 697 if (guest_ret == 0) 698 printf("\n # KVM session ended normally.\n"); 699 } 700 701 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 702 { 703 int ret = -EFAULT; 704 struct kvm *kvm; 705 706 kvm = kvm_cmd_run_init(argc, argv); 707 if (IS_ERR(kvm)) 708 return PTR_ERR(kvm); 709 710 ret = kvm_cmd_run_work(kvm); 711 kvm_cmd_run_exit(kvm, ret); 712 713 return ret; 714 } 715