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