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