1 #include <stdio.h> 2 #include <string.h> 3 #include <signal.h> 4 #include <unistd.h> 5 #include <stdlib.h> 6 #include <termios.h> 7 #include <sys/utsname.h> 8 #include <sys/types.h> 9 #include <sys/stat.h> 10 #include <ctype.h> 11 12 /* user defined header files */ 13 #include <linux/types.h> 14 #include <kvm/kvm.h> 15 #include <kvm/kvm-cpu.h> 16 #include <kvm/8250-serial.h> 17 #include <kvm/virtio-blk.h> 18 #include <kvm/virtio-net.h> 19 #include <kvm/virtio-console.h> 20 #include <kvm/virtio-rng.h> 21 #include <kvm/virtio-balloon.h> 22 #include <kvm/disk-image.h> 23 #include <kvm/util.h> 24 #include <kvm/pci.h> 25 #include <kvm/rtc.h> 26 #include <kvm/term.h> 27 #include <kvm/ioport.h> 28 #include <kvm/threadpool.h> 29 #include <kvm/barrier.h> 30 #include <kvm/symbol.h> 31 #include <kvm/virtio-9p.h> 32 #include <kvm/vesa.h> 33 #include <kvm/ioeventfd.h> 34 #include <kvm/i8042.h> 35 #include <kvm/vnc.h> 36 #include <kvm/sdl.h> 37 #include <kvm/framebuffer.h> 38 #include <kvm/irq.h> 39 40 /* header files for gitish interface */ 41 #include <kvm/builtin-run.h> 42 #include <kvm/parse-options.h> 43 #include <kvm/mutex.h> 44 45 #define DEFAULT_KVM_DEV "/dev/kvm" 46 #define DEFAULT_CONSOLE "serial" 47 #define DEFAULT_NETWORK "user" 48 #define DEFAULT_HOST_ADDR "192.168.33.1" 49 #define DEFAULT_GUEST_ADDR "192.168.33.15" 50 #define DEFAULT_GUEST_MAC "02:15:15:15:15:15" 51 #define DEFAULT_HOST_MAC "02:01:01:01:01:01" 52 #define DEFAULT_SCRIPT "none" 53 54 #define MB_SHIFT (20) 55 #define MIN_RAM_SIZE_MB (64ULL) 56 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) 57 58 struct kvm *kvm; 59 struct kvm_cpu *kvm_cpus[KVM_NR_CPUS]; 60 __thread struct kvm_cpu *current_kvm_cpu; 61 62 static u64 ram_size; 63 static u8 image_count; 64 static int virtio_rng; 65 static const char *kernel_cmdline; 66 static const char *kernel_filename; 67 static const char *vmlinux_filename; 68 static const char *initrd_filename; 69 static const char *image_filename[MAX_DISK_IMAGES]; 70 static const char *console; 71 static const char *dev; 72 static const char *network; 73 static const char *host_ip; 74 static const char *guest_ip; 75 static const char *guest_mac; 76 static const char *host_mac; 77 static const char *script; 78 static const char *guest_name; 79 static bool single_step; 80 static bool readonly_image[MAX_DISK_IMAGES]; 81 static bool vnc; 82 static bool sdl; 83 static bool balloon; 84 static bool using_rootfs; 85 extern bool ioport_debug; 86 extern int active_console; 87 extern int debug_iodelay; 88 89 bool do_debug_print = false; 90 91 static int nrcpus; 92 static int vidmode = -1; 93 94 static const char * const run_usage[] = { 95 "kvm run [<options>] [<kernel image>]", 96 NULL 97 }; 98 99 static int img_name_parser(const struct option *opt, const char *arg, int unset) 100 { 101 char *sep; 102 struct stat st; 103 104 if (stat(arg, &st) == 0 && 105 S_ISDIR(st.st_mode)) { 106 char tmp[PATH_MAX]; 107 108 if (realpath(arg, tmp) == 0 || 109 virtio_9p__init(kvm, tmp, "/dev/root") < 0) 110 die("Unable to initialize virtio 9p"); 111 using_rootfs = 1; 112 return 0; 113 } 114 115 if (image_count >= MAX_DISK_IMAGES) 116 die("Currently only 4 images are supported"); 117 118 image_filename[image_count] = arg; 119 sep = strstr(arg, ","); 120 if (sep) { 121 if (strcmp(sep + 1, "ro") == 0) 122 readonly_image[image_count] = 1; 123 *sep = 0; 124 } 125 126 image_count++; 127 128 return 0; 129 } 130 131 static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset) 132 { 133 char *tag_name; 134 char tmp[PATH_MAX]; 135 136 /* 137 * 9p dir can be of the form dirname,tag_name or 138 * just dirname. In the later case we use the 139 * default tag name 140 */ 141 tag_name = strstr(arg, ","); 142 if (tag_name) { 143 *tag_name = '\0'; 144 tag_name++; 145 } 146 if (realpath(arg, tmp)) { 147 if (virtio_9p__init(kvm, tmp, tag_name) < 0) 148 die("Unable to initialize virtio 9p"); 149 } else 150 die("Failed resolving 9p path"); 151 return 0; 152 } 153 154 155 static const struct option options[] = { 156 OPT_GROUP("Basic options:"), 157 OPT_STRING('\0', "name", &guest_name, "guest name", 158 "A name for the guest"), 159 OPT_INTEGER('c', "cpus", &nrcpus, "Number of CPUs"), 160 OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), 161 OPT_CALLBACK('d', "disk", NULL, "image or rootfs_dir", "Disk image or rootfs directory", img_name_parser), 162 OPT_BOOLEAN('\0', "balloon", &balloon, "Enable virtio balloon"), 163 OPT_BOOLEAN('\0', "vnc", &vnc, "Enable VNC framebuffer"), 164 OPT_BOOLEAN('\0', "sdl", &sdl, "Enable SDL framebuffer"), 165 OPT_INCR('\0', "rng", &virtio_rng, "Enable virtio Random Number Generator"), 166 OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name", 167 "Enable virtio 9p to share files between host and guest", virtio_9p_rootdir_parser), 168 OPT_STRING('\0', "console", &console, "serial or virtio", 169 "Console to use"), 170 OPT_STRING('\0', "dev", &dev, "device_file", "KVM device file"), 171 172 OPT_GROUP("Kernel options:"), 173 OPT_STRING('k', "kernel", &kernel_filename, "kernel", 174 "Kernel to boot in virtual machine"), 175 OPT_STRING('i', "initrd", &initrd_filename, "initrd", 176 "Initial RAM disk image"), 177 OPT_STRING('p', "params", &kernel_cmdline, "params", 178 "Kernel command line arguments"), 179 180 OPT_GROUP("Networking options:"), 181 OPT_STRING('n', "network", &network, "user, tap, none", 182 "Network to use"), 183 OPT_STRING('\0', "host-ip", &host_ip, "a.b.c.d", 184 "Assign this address to the host side networking"), 185 OPT_STRING('\0', "guest-ip", &guest_ip, "a.b.c.d", 186 "Assign this address to the guest side networking"), 187 OPT_STRING('\0', "host-mac", &host_mac, "aa:bb:cc:dd:ee:ff", 188 "Assign this address to the host side NIC"), 189 OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff", 190 "Assign this address to the guest side NIC"), 191 OPT_STRING('\0', "tapscript", &script, "Script path", 192 "Assign a script to process created tap device"), 193 194 OPT_GROUP("BIOS options:"), 195 OPT_INTEGER('\0', "vidmode", &vidmode, 196 "Video mode"), 197 198 OPT_GROUP("Debug options:"), 199 OPT_BOOLEAN('\0', "debug", &do_debug_print, 200 "Enable debug messages"), 201 OPT_BOOLEAN('\0', "debug-single-step", &single_step, 202 "Enable single stepping"), 203 OPT_BOOLEAN('\0', "debug-ioport", &ioport_debug, 204 "Enable ioport debugging"), 205 OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay, 206 "Delay IO by millisecond"), 207 OPT_END() 208 }; 209 210 /* 211 * Serialize debug printout so that the output of multiple vcpus does not 212 * get mixed up: 213 */ 214 static int printout_done; 215 216 static void handle_sigusr1(int sig) 217 { 218 struct kvm_cpu *cpu = current_kvm_cpu; 219 220 if (!cpu) 221 return; 222 223 printf("\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id); 224 kvm_cpu__show_registers(cpu); 225 kvm_cpu__show_code(cpu); 226 kvm_cpu__show_page_tables(cpu); 227 fflush(stdout); 228 printout_done = 1; 229 mb(); 230 } 231 232 /* Pause/resume the guest using SIGUSR2 */ 233 static int is_paused; 234 235 static void handle_sigusr2(int sig) 236 { 237 if (sig == SIGKVMRESUME && is_paused) 238 kvm__continue(); 239 else if (sig == SIGUSR2 && !is_paused) 240 kvm__pause(); 241 else 242 return; 243 244 is_paused = !is_paused; 245 pr_info("Guest %s\n", is_paused ? "paused" : "resumed"); 246 } 247 248 static void handle_sigquit(int sig) 249 { 250 int i; 251 252 for (i = 0; i < nrcpus; i++) { 253 struct kvm_cpu *cpu = kvm_cpus[i]; 254 255 if (!cpu) 256 continue; 257 258 printout_done = 0; 259 pthread_kill(cpu->thread, SIGUSR1); 260 /* 261 * Wait for the vCPU to dump state before signalling 262 * the next thread. Since this is debug code it does 263 * not matter that we are burning CPU time a bit: 264 */ 265 while (!printout_done) 266 mb(); 267 } 268 269 serial8250__inject_sysrq(kvm); 270 } 271 272 static void handle_sigalrm(int sig) 273 { 274 serial8250__inject_interrupt(kvm); 275 virtio_console__inject_interrupt(kvm); 276 } 277 278 static void handle_sigstop(int sig) 279 { 280 kvm_cpu__reboot(); 281 } 282 283 static void *kvm_cpu_thread(void *arg) 284 { 285 current_kvm_cpu = arg; 286 287 if (kvm_cpu__start(current_kvm_cpu)) 288 goto panic_kvm; 289 290 kvm_cpu__delete(current_kvm_cpu); 291 292 return (void *) (intptr_t) 0; 293 294 panic_kvm: 295 fprintf(stderr, "KVM exit reason: %u (\"%s\")\n", 296 current_kvm_cpu->kvm_run->exit_reason, 297 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 298 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 299 fprintf(stderr, "KVM exit code: 0x%Lu\n", 300 current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 301 302 kvm_cpu__show_registers(current_kvm_cpu); 303 kvm_cpu__show_code(current_kvm_cpu); 304 kvm_cpu__show_page_tables(current_kvm_cpu); 305 306 kvm_cpu__delete(current_kvm_cpu); 307 308 return (void *) (intptr_t) 1; 309 } 310 311 static char kernel[PATH_MAX]; 312 313 static const char *host_kernels[] = { 314 "/boot/vmlinuz", 315 "/boot/bzImage", 316 NULL 317 }; 318 319 static const char *default_kernels[] = { 320 "./bzImage", 321 "../../arch/x86/boot/bzImage", 322 NULL 323 }; 324 325 static const char *default_vmlinux[] = { 326 "../../../vmlinux", 327 "../../vmlinux", 328 NULL 329 }; 330 331 static void kernel_usage_with_options(void) 332 { 333 const char **k; 334 struct utsname uts; 335 336 fprintf(stderr, "Fatal: could not find default kernel image in:\n"); 337 k = &default_kernels[0]; 338 while (*k) { 339 fprintf(stderr, "\t%s\n", *k); 340 k++; 341 } 342 343 if (uname(&uts) < 0) 344 return; 345 346 k = &host_kernels[0]; 347 while (*k) { 348 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 349 return; 350 fprintf(stderr, "\t%s\n", kernel); 351 k++; 352 } 353 fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n"); 354 } 355 356 static u64 host_ram_size(void) 357 { 358 long page_size; 359 long nr_pages; 360 361 nr_pages = sysconf(_SC_PHYS_PAGES); 362 if (nr_pages < 0) { 363 pr_warning("sysconf(_SC_PHYS_PAGES) failed"); 364 return 0; 365 } 366 367 page_size = sysconf(_SC_PAGE_SIZE); 368 if (page_size < 0) { 369 pr_warning("sysconf(_SC_PAGE_SIZE) failed"); 370 return 0; 371 } 372 373 return (nr_pages * page_size) >> MB_SHIFT; 374 } 375 376 /* 377 * If user didn't specify how much memory it wants to allocate for the guest, 378 * avoid filling the whole host RAM. 379 */ 380 #define RAM_SIZE_RATIO 0.8 381 382 static u64 get_ram_size(int nr_cpus) 383 { 384 u64 available; 385 u64 ram_size; 386 387 ram_size = 64 * (nr_cpus + 3); 388 389 available = host_ram_size() * RAM_SIZE_RATIO; 390 if (!available) 391 available = MIN_RAM_SIZE_MB; 392 393 if (ram_size > available) 394 ram_size = available; 395 396 return ram_size; 397 } 398 399 static const char *find_kernel(void) 400 { 401 const char **k; 402 struct stat st; 403 struct utsname uts; 404 405 k = &default_kernels[0]; 406 while (*k) { 407 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) { 408 k++; 409 continue; 410 } 411 strncpy(kernel, *k, PATH_MAX); 412 return kernel; 413 } 414 415 if (uname(&uts) < 0) 416 return NULL; 417 418 k = &host_kernels[0]; 419 while (*k) { 420 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 421 return NULL; 422 423 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) { 424 k++; 425 continue; 426 } 427 return kernel; 428 429 } 430 return NULL; 431 } 432 433 static const char *find_vmlinux(void) 434 { 435 const char **vmlinux; 436 437 vmlinux = &default_vmlinux[0]; 438 while (*vmlinux) { 439 struct stat st; 440 441 if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) { 442 vmlinux++; 443 continue; 444 } 445 return *vmlinux; 446 } 447 return NULL; 448 } 449 450 static int root_device(char *dev, long *part) 451 { 452 struct stat st; 453 454 if (stat("/", &st) < 0) 455 return -1; 456 457 *part = minor(st.st_dev); 458 459 sprintf(dev, "/dev/block/%u:0", major(st.st_dev)); 460 if (access(dev, R_OK) < 0) 461 return -1; 462 463 return 0; 464 } 465 466 static char *host_image(char *cmd_line, size_t size) 467 { 468 char *t; 469 char device[PATH_MAX]; 470 long part = 0; 471 472 t = malloc(PATH_MAX); 473 if (!t) 474 return NULL; 475 476 /* check for the root file system */ 477 if (root_device(device, &part) < 0) { 478 free(t); 479 return NULL; 480 } 481 strncpy(t, device, PATH_MAX); 482 if (!strstr(cmd_line, "root=")) { 483 char tmp[PATH_MAX]; 484 snprintf(tmp, sizeof(tmp), "root=/dev/vda%ld rw ", part); 485 strlcat(cmd_line, tmp, size); 486 } 487 return t; 488 } 489 490 void kvm_run_help(void) 491 { 492 usage_with_options(run_usage, options); 493 } 494 495 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 496 { 497 struct virtio_net_parameters net_params; 498 static char real_cmdline[2048], default_name[20]; 499 struct framebuffer *fb = NULL; 500 unsigned int nr_online_cpus; 501 int exit_code = 0; 502 int max_cpus, recommended_cpus; 503 char *hi; 504 int i; 505 void *ret; 506 507 signal(SIGALRM, handle_sigalrm); 508 signal(SIGQUIT, handle_sigquit); 509 signal(SIGUSR1, handle_sigusr1); 510 signal(SIGUSR2, handle_sigusr2); 511 signal(SIGKVMSTOP, handle_sigstop); 512 signal(SIGKVMRESUME, handle_sigusr2); 513 514 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); 515 516 while (argc != 0) { 517 argc = parse_options(argc, argv, options, run_usage, 518 PARSE_OPT_STOP_AT_NON_OPTION); 519 if (argc != 0) { 520 if (kernel_filename) { 521 fprintf(stderr, "Cannot handle parameter: " 522 "%s\n", argv[0]); 523 usage_with_options(run_usage, options); 524 return EINVAL; 525 } 526 /* first unhandled parameter is treated as a kernel 527 image 528 */ 529 kernel_filename = argv[0]; 530 argv++; 531 argc--; 532 } 533 534 } 535 536 if (!kernel_filename) 537 kernel_filename = find_kernel(); 538 539 if (!kernel_filename) { 540 kernel_usage_with_options(); 541 return EINVAL; 542 } 543 544 vmlinux_filename = find_vmlinux(); 545 546 if (nrcpus == 0) 547 nrcpus = nr_online_cpus; 548 else if (nrcpus < 1 || nrcpus > KVM_NR_CPUS) 549 die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS); 550 551 if (!ram_size) 552 ram_size = get_ram_size(nrcpus); 553 554 if (ram_size < MIN_RAM_SIZE_MB) 555 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB); 556 557 if (ram_size > host_ram_size()) 558 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size, host_ram_size()); 559 560 ram_size <<= MB_SHIFT; 561 562 if (!dev) 563 dev = DEFAULT_KVM_DEV; 564 565 if (!console) 566 console = DEFAULT_CONSOLE; 567 568 if (!strncmp(console, "virtio", 6)) 569 active_console = CONSOLE_VIRTIO; 570 else 571 active_console = CONSOLE_8250; 572 573 if (!host_ip) 574 host_ip = DEFAULT_HOST_ADDR; 575 576 if (!guest_ip) 577 guest_ip = DEFAULT_GUEST_ADDR; 578 579 if (!guest_mac) 580 guest_mac = DEFAULT_GUEST_MAC; 581 582 if (!host_mac) 583 host_mac = DEFAULT_HOST_MAC; 584 585 if (!script) 586 script = DEFAULT_SCRIPT; 587 588 symbol__init(vmlinux_filename); 589 590 term_init(); 591 592 if (!guest_name) { 593 sprintf(default_name, "guest-%u", getpid()); 594 guest_name = default_name; 595 } 596 597 kvm = kvm__init(dev, ram_size, guest_name); 598 599 irq__init(kvm); 600 601 kvm->single_step = single_step; 602 603 ioeventfd__init(); 604 605 max_cpus = kvm__max_cpus(kvm); 606 recommended_cpus = kvm__recommended_cpus(kvm); 607 608 if (nrcpus > max_cpus) { 609 printf(" # Limit the number of CPUs to %d\n", max_cpus); 610 kvm->nrcpus = max_cpus; 611 } else if (nrcpus > recommended_cpus) { 612 printf(" # Warning: The maximum recommended amount of VCPUs" 613 " is %d\n", recommended_cpus); 614 } 615 616 kvm->nrcpus = nrcpus; 617 618 /* 619 * vidmode should be either specified 620 * either set by default 621 */ 622 if (vnc || sdl) { 623 if (vidmode == -1) 624 vidmode = 0x312; 625 } else 626 vidmode = 0; 627 628 memset(real_cmdline, 0, sizeof(real_cmdline)); 629 strcpy(real_cmdline, "notsc noapic noacpi pci=conf1 reboot=k panic=1"); 630 if (vnc || sdl) { 631 strcat(real_cmdline, " video=vesafb console=tty0"); 632 } else 633 strcat(real_cmdline, " console=ttyS0 earlyprintk=serial"); 634 strcat(real_cmdline, " "); 635 if (kernel_cmdline) 636 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); 637 638 hi = NULL; 639 if (!using_rootfs && !image_filename[0]) { 640 hi = host_image(real_cmdline, sizeof(real_cmdline)); 641 if (hi) { 642 image_filename[0] = hi; 643 readonly_image[0] = true; 644 image_count++; 645 } 646 } 647 648 if (!strstr(real_cmdline, "root=")) 649 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); 650 651 if (using_rootfs) 652 strcat(real_cmdline, " root=/dev/root rootflags=rw,trans=virtio,version=9p2000.u rootfstype=9p"); 653 654 if (image_count) { 655 kvm->nr_disks = image_count; 656 kvm->disks = disk_image__open_all(image_filename, readonly_image, image_count); 657 if (!kvm->disks) 658 die("Unable to load all disk images."); 659 660 virtio_blk__init_all(kvm); 661 } 662 663 free(hi); 664 665 printf(" # kvm run -k %s -m %Lu -c %d --name %s\n", kernel_filename, ram_size / 1024 / 1024, nrcpus, guest_name); 666 667 if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, 668 real_cmdline, vidmode)) 669 die("unable to load kernel %s", kernel_filename); 670 671 kvm->vmlinux = vmlinux_filename; 672 673 ioport__setup_legacy(); 674 675 rtc__init(); 676 677 serial8250__init(kvm); 678 679 pci__init(); 680 681 if (active_console == CONSOLE_VIRTIO) 682 virtio_console__init(kvm); 683 684 if (virtio_rng) 685 while (virtio_rng--) 686 virtio_rng__init(kvm); 687 688 if (balloon) 689 virtio_bln__init(kvm); 690 691 if (!network) 692 network = DEFAULT_NETWORK; 693 694 if (strncmp(network, "none", 4)) { 695 net_params.guest_ip = guest_ip; 696 net_params.host_ip = host_ip; 697 net_params.kvm = kvm; 698 net_params.script = script; 699 sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 700 net_params.guest_mac, 701 net_params.guest_mac+1, 702 net_params.guest_mac+2, 703 net_params.guest_mac+3, 704 net_params.guest_mac+4, 705 net_params.guest_mac+5); 706 sscanf(host_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 707 net_params.host_mac, 708 net_params.host_mac+1, 709 net_params.host_mac+2, 710 net_params.host_mac+3, 711 net_params.host_mac+4, 712 net_params.host_mac+5); 713 714 if (!strncmp(network, "user", 4)) 715 net_params.mode = NET_MODE_USER; 716 else if (!strncmp(network, "tap", 3)) 717 net_params.mode = NET_MODE_TAP; 718 else 719 die("Unkown network mode %s, please use -network user, tap, none", network); 720 virtio_net__init(&net_params); 721 } 722 723 kvm__start_timer(kvm); 724 725 kvm__setup_bios(kvm); 726 727 for (i = 0; i < nrcpus; i++) { 728 kvm_cpus[i] = kvm_cpu__init(kvm, i); 729 if (!kvm_cpus[i]) 730 die("unable to initialize KVM VCPU"); 731 } 732 733 kvm__init_ram(kvm); 734 735 kbd__init(kvm); 736 737 if (vnc || sdl) 738 fb = vesa__init(kvm); 739 740 if (vnc) { 741 if (fb) 742 vnc__init(fb); 743 } 744 745 if (sdl) { 746 if (fb) 747 sdl__init(fb); 748 } 749 750 fb__start(); 751 752 thread_pool__init(nr_online_cpus); 753 ioeventfd__start(); 754 755 for (i = 0; i < nrcpus; i++) { 756 if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0) 757 die("unable to create KVM VCPU thread"); 758 } 759 760 /* Only VCPU #0 is going to exit by itself when shutting down */ 761 if (pthread_join(kvm_cpus[0]->thread, &ret) != 0) 762 exit_code = 1; 763 764 for (i = 1; i < nrcpus; i++) { 765 pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT); 766 if (pthread_join(kvm_cpus[i]->thread, &ret) != 0) 767 die("pthread_join"); 768 769 if (ret != NULL) 770 exit_code = 1; 771 } 772 773 fb__stop(); 774 775 virtio_blk__delete_all(kvm); 776 virtio_rng__delete_all(kvm); 777 778 disk_image__close_all(kvm->disks, image_count); 779 kvm__delete(kvm); 780 781 if (!exit_code) 782 printf("\n # KVM session ended normally.\n"); 783 784 return exit_code; 785 } 786