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 handle_sigalrm(int sig, siginfo_t *si, void *uc) 169 { 170 struct kvm *kvm = si->si_value.sival_ptr; 171 172 kvm__arch_periodic_poll(kvm); 173 } 174 175 static void *kvm_cpu_thread(void *arg) 176 { 177 char name[16]; 178 179 current_kvm_cpu = arg; 180 181 sprintf(name, "kvm-vcpu-%lu", current_kvm_cpu->cpu_id); 182 kvm__set_thread_name(name); 183 184 if (kvm_cpu__start(current_kvm_cpu)) 185 goto panic_kvm; 186 187 return (void *) (intptr_t) 0; 188 189 panic_kvm: 190 fprintf(stderr, "KVM exit reason: %u (\"%s\")\n", 191 current_kvm_cpu->kvm_run->exit_reason, 192 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 193 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 194 fprintf(stderr, "KVM exit code: 0x%Lu\n", 195 current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 196 197 kvm_cpu__set_debug_fd(STDOUT_FILENO); 198 kvm_cpu__show_registers(current_kvm_cpu); 199 kvm_cpu__show_code(current_kvm_cpu); 200 kvm_cpu__show_page_tables(current_kvm_cpu); 201 202 return (void *) (intptr_t) 1; 203 } 204 205 static char kernel[PATH_MAX]; 206 207 static const char *host_kernels[] = { 208 "/boot/vmlinuz", 209 "/boot/bzImage", 210 NULL 211 }; 212 213 static const char *default_kernels[] = { 214 "./bzImage", 215 "arch/" BUILD_ARCH "/boot/bzImage", 216 "../../arch/" BUILD_ARCH "/boot/bzImage", 217 NULL 218 }; 219 220 static const char *default_vmlinux[] = { 221 "vmlinux", 222 "../../../vmlinux", 223 "../../vmlinux", 224 NULL 225 }; 226 227 static void kernel_usage_with_options(void) 228 { 229 const char **k; 230 struct utsname uts; 231 232 fprintf(stderr, "Fatal: could not find default kernel image in:\n"); 233 k = &default_kernels[0]; 234 while (*k) { 235 fprintf(stderr, "\t%s\n", *k); 236 k++; 237 } 238 239 if (uname(&uts) < 0) 240 return; 241 242 k = &host_kernels[0]; 243 while (*k) { 244 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 245 return; 246 fprintf(stderr, "\t%s\n", kernel); 247 k++; 248 } 249 fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n", 250 KVM_BINARY_NAME); 251 } 252 253 static u64 host_ram_size(void) 254 { 255 long page_size; 256 long nr_pages; 257 258 nr_pages = sysconf(_SC_PHYS_PAGES); 259 if (nr_pages < 0) { 260 pr_warning("sysconf(_SC_PHYS_PAGES) failed"); 261 return 0; 262 } 263 264 page_size = sysconf(_SC_PAGE_SIZE); 265 if (page_size < 0) { 266 pr_warning("sysconf(_SC_PAGE_SIZE) failed"); 267 return 0; 268 } 269 270 return (nr_pages * page_size) >> MB_SHIFT; 271 } 272 273 /* 274 * If user didn't specify how much memory it wants to allocate for the guest, 275 * avoid filling the whole host RAM. 276 */ 277 #define RAM_SIZE_RATIO 0.8 278 279 static u64 get_ram_size(int nr_cpus) 280 { 281 u64 available; 282 u64 ram_size; 283 284 ram_size = 64 * (nr_cpus + 3); 285 286 available = host_ram_size() * RAM_SIZE_RATIO; 287 if (!available) 288 available = MIN_RAM_SIZE_MB; 289 290 if (ram_size > available) 291 ram_size = available; 292 293 return ram_size; 294 } 295 296 static const char *find_kernel(void) 297 { 298 const char **k; 299 struct stat st; 300 struct utsname uts; 301 302 k = &default_kernels[0]; 303 while (*k) { 304 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) { 305 k++; 306 continue; 307 } 308 strncpy(kernel, *k, PATH_MAX); 309 return kernel; 310 } 311 312 if (uname(&uts) < 0) 313 return NULL; 314 315 k = &host_kernels[0]; 316 while (*k) { 317 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 318 return NULL; 319 320 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) { 321 k++; 322 continue; 323 } 324 return kernel; 325 326 } 327 return NULL; 328 } 329 330 static const char *find_vmlinux(void) 331 { 332 const char **vmlinux; 333 334 vmlinux = &default_vmlinux[0]; 335 while (*vmlinux) { 336 struct stat st; 337 338 if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) { 339 vmlinux++; 340 continue; 341 } 342 return *vmlinux; 343 } 344 return NULL; 345 } 346 347 void kvm_run_help(void) 348 { 349 struct kvm *kvm = NULL; 350 351 BUILD_OPTIONS(options, &kvm->cfg, kvm); 352 usage_with_options(run_usage, options); 353 } 354 355 static int kvm_setup_guest_init(struct kvm *kvm) 356 { 357 const char *rootfs = kvm->cfg.custom_rootfs_name; 358 char tmp[PATH_MAX]; 359 size_t size; 360 int fd, ret; 361 char *data; 362 363 /* Setup /virt/init */ 364 size = (size_t)&_binary_guest_init_size; 365 data = (char *)&_binary_guest_init_start; 366 snprintf(tmp, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), rootfs); 367 remove(tmp); 368 fd = open(tmp, O_CREAT | O_WRONLY, 0755); 369 if (fd < 0) 370 die("Fail to setup %s", tmp); 371 ret = xwrite(fd, data, size); 372 if (ret < 0) 373 die("Fail to setup %s", tmp); 374 close(fd); 375 376 return 0; 377 } 378 379 static int kvm_run_set_sandbox(struct kvm *kvm) 380 { 381 const char *guestfs_name = kvm->cfg.custom_rootfs_name; 382 char path[PATH_MAX], script[PATH_MAX], *tmp; 383 384 snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name); 385 386 remove(path); 387 388 if (kvm->cfg.sandbox == NULL) 389 return 0; 390 391 tmp = realpath(kvm->cfg.sandbox, NULL); 392 if (tmp == NULL) 393 return -ENOMEM; 394 395 snprintf(script, PATH_MAX, "/host/%s", tmp); 396 free(tmp); 397 398 return symlink(script, path); 399 } 400 401 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg) 402 { 403 const char *single_quote; 404 405 if (!*arg) { /* zero length string */ 406 if (write(fd, "''", 2) <= 0) 407 die("Failed writing sandbox script"); 408 return; 409 } 410 411 while (*arg) { 412 single_quote = strchrnul(arg, '\''); 413 414 /* write non-single-quote string as #('string') */ 415 if (arg != single_quote) { 416 if (write(fd, "'", 1) <= 0 || 417 write(fd, arg, single_quote - arg) <= 0 || 418 write(fd, "'", 1) <= 0) 419 die("Failed writing sandbox script"); 420 } 421 422 /* write single quote as #("'") */ 423 if (*single_quote) { 424 if (write(fd, "\"'\"", 3) <= 0) 425 die("Failed writing sandbox script"); 426 } else 427 break; 428 429 arg = single_quote + 1; 430 } 431 } 432 433 static void resolve_program(const char *src, char *dst, size_t len) 434 { 435 struct stat st; 436 int err; 437 438 err = stat(src, &st); 439 440 if (!err && S_ISREG(st.st_mode)) { 441 char resolved_path[PATH_MAX]; 442 443 if (!realpath(src, resolved_path)) 444 die("Unable to resolve program %s: %s\n", src, strerror(errno)); 445 446 snprintf(dst, len, "/host%s", resolved_path); 447 } else 448 strncpy(dst, src, len); 449 } 450 451 static void kvm_run_write_sandbox_cmd(struct kvm *kvm, const char **argv, int argc) 452 { 453 const char script_hdr[] = "#! /bin/bash\n\n"; 454 char program[PATH_MAX]; 455 int fd; 456 457 remove(kvm->cfg.sandbox); 458 459 fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777); 460 if (fd < 0) 461 die("Failed creating sandbox script"); 462 463 if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0) 464 die("Failed writing sandbox script"); 465 466 resolve_program(argv[0], program, PATH_MAX); 467 kvm_write_sandbox_cmd_exactly(fd, program); 468 469 argv++; 470 argc--; 471 472 while (argc) { 473 if (write(fd, " ", 1) <= 0) 474 die("Failed writing sandbox script"); 475 476 kvm_write_sandbox_cmd_exactly(fd, argv[0]); 477 argv++; 478 argc--; 479 } 480 if (write(fd, "\n", 1) <= 0) 481 die("Failed writing sandbox script"); 482 483 close(fd); 484 } 485 486 static struct kvm *kvm_cmd_run_init(int argc, const char **argv) 487 { 488 static char real_cmdline[2048], default_name[20]; 489 unsigned int nr_online_cpus; 490 struct sigaction sa; 491 struct kvm *kvm = kvm__new(); 492 493 if (IS_ERR(kvm)) 494 return kvm; 495 496 sa.sa_flags = SA_SIGINFO; 497 sa.sa_sigaction = handle_sigalrm; 498 sigemptyset(&sa.sa_mask); 499 sigaction(SIGALRM, &sa, NULL); 500 501 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); 502 kvm->cfg.custom_rootfs_name = "default"; 503 504 while (argc != 0) { 505 BUILD_OPTIONS(options, &kvm->cfg, kvm); 506 argc = parse_options(argc, argv, options, run_usage, 507 PARSE_OPT_STOP_AT_NON_OPTION | 508 PARSE_OPT_KEEP_DASHDASH); 509 if (argc != 0) { 510 /* Cusrom options, should have been handled elsewhere */ 511 if (strcmp(argv[0], "--") == 0) { 512 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 513 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 514 kvm_run_write_sandbox_cmd(kvm, argv+1, argc-1); 515 break; 516 } 517 } 518 519 if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) || 520 (kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) { 521 fprintf(stderr, "Cannot handle parameter: " 522 "%s\n", argv[0]); 523 usage_with_options(run_usage, options); 524 free(kvm); 525 return ERR_PTR(-EINVAL); 526 } 527 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 528 /* 529 * first unhandled parameter is treated as 530 * sandbox command 531 */ 532 kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME; 533 kvm_run_write_sandbox_cmd(kvm, argv, argc); 534 } else { 535 /* 536 * first unhandled parameter is treated as a kernel 537 * image 538 */ 539 kvm->cfg.kernel_filename = argv[0]; 540 } 541 argv++; 542 argc--; 543 } 544 545 } 546 547 kvm->nr_disks = kvm->cfg.image_count; 548 549 if (!kvm->cfg.kernel_filename) 550 kvm->cfg.kernel_filename = find_kernel(); 551 552 if (!kvm->cfg.kernel_filename) { 553 kernel_usage_with_options(); 554 return ERR_PTR(-EINVAL); 555 } 556 557 kvm->cfg.vmlinux_filename = find_vmlinux(); 558 kvm->vmlinux = kvm->cfg.vmlinux_filename; 559 560 if (kvm->cfg.nrcpus == 0) 561 kvm->cfg.nrcpus = nr_online_cpus; 562 563 if (!kvm->cfg.ram_size) 564 kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus); 565 566 if (kvm->cfg.ram_size > host_ram_size()) 567 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", kvm->cfg.ram_size, host_ram_size()); 568 569 kvm->cfg.ram_size <<= MB_SHIFT; 570 571 if (!kvm->cfg.dev) 572 kvm->cfg.dev = DEFAULT_KVM_DEV; 573 574 if (!kvm->cfg.console) 575 kvm->cfg.console = DEFAULT_CONSOLE; 576 577 if (!strncmp(kvm->cfg.console, "virtio", 6)) 578 kvm->cfg.active_console = CONSOLE_VIRTIO; 579 else if (!strncmp(kvm->cfg.console, "serial", 6)) 580 kvm->cfg.active_console = CONSOLE_8250; 581 else if (!strncmp(kvm->cfg.console, "hv", 2)) 582 kvm->cfg.active_console = CONSOLE_HV; 583 else 584 pr_warning("No console!"); 585 586 if (!kvm->cfg.host_ip) 587 kvm->cfg.host_ip = DEFAULT_HOST_ADDR; 588 589 if (!kvm->cfg.guest_ip) 590 kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR; 591 592 if (!kvm->cfg.guest_mac) 593 kvm->cfg.guest_mac = DEFAULT_GUEST_MAC; 594 595 if (!kvm->cfg.host_mac) 596 kvm->cfg.host_mac = DEFAULT_HOST_MAC; 597 598 if (!kvm->cfg.script) 599 kvm->cfg.script = DEFAULT_SCRIPT; 600 601 if (!kvm->cfg.network) 602 kvm->cfg.network = DEFAULT_NETWORK; 603 604 memset(real_cmdline, 0, sizeof(real_cmdline)); 605 kvm__arch_set_cmdline(real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl || kvm->cfg.gtk); 606 607 if (strlen(real_cmdline) > 0) 608 strcat(real_cmdline, " "); 609 610 if (kvm->cfg.kernel_cmdline) 611 strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline)); 612 613 if (!kvm->cfg.guest_name) { 614 if (kvm->cfg.custom_rootfs) { 615 kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name; 616 } else { 617 sprintf(default_name, "guest-%u", getpid()); 618 kvm->cfg.guest_name = default_name; 619 } 620 } 621 622 if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) { 623 char tmp[PATH_MAX]; 624 625 kvm_setup_create_new(kvm->cfg.custom_rootfs_name); 626 kvm_setup_resolv(kvm->cfg.custom_rootfs_name); 627 628 snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default"); 629 if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) 630 die("Unable to initialize virtio 9p"); 631 if (virtio_9p__register(kvm, "/", "hostfs") < 0) 632 die("Unable to initialize virtio 9p"); 633 kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1; 634 } 635 636 if (kvm->cfg.using_rootfs) { 637 strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p"); 638 if (kvm->cfg.custom_rootfs) { 639 kvm_run_set_sandbox(kvm); 640 641 strcat(real_cmdline, " init=/virt/init"); 642 643 if (!kvm->cfg.no_dhcp) 644 strcat(real_cmdline, " ip=dhcp"); 645 if (kvm_setup_guest_init(kvm)) 646 die("Failed to setup init for guest."); 647 } 648 } else if (!strstr(real_cmdline, "root=")) { 649 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); 650 } 651 652 kvm->cfg.real_cmdline = real_cmdline; 653 654 printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, 655 kvm->cfg.kernel_filename, kvm->cfg.ram_size / 1024 / 1024, kvm->cfg.nrcpus, kvm->cfg.guest_name); 656 657 if (init_list__init(kvm) < 0) 658 die ("Initialisation failed"); 659 660 return kvm; 661 } 662 663 static int kvm_cmd_run_work(struct kvm *kvm) 664 { 665 int i; 666 void *ret = NULL; 667 668 for (i = 0; i < kvm->nrcpus; i++) { 669 if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0) 670 die("unable to create KVM VCPU thread"); 671 } 672 673 /* Only VCPU #0 is going to exit by itself when shutting down */ 674 return pthread_join(kvm->cpus[0]->thread, &ret); 675 } 676 677 static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret) 678 { 679 compat__print_all_messages(); 680 681 init_list__exit(kvm); 682 683 if (guest_ret == 0) 684 printf("\n # KVM session ended normally.\n"); 685 } 686 687 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 688 { 689 int ret = -EFAULT; 690 struct kvm *kvm; 691 692 kvm = kvm_cmd_run_init(argc, argv); 693 if (IS_ERR(kvm)) 694 return PTR_ERR(kvm); 695 696 ret = kvm_cmd_run_work(kvm); 697 kvm_cmd_run_exit(kvm, ret); 698 699 return ret; 700 } 701