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