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