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