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