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