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