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