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