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