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