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/strbuf.h" 25 #include "kvm/vesa.h" 26 #include "kvm/irq.h" 27 #include "kvm/kvm.h" 28 #include "kvm/pci.h" 29 #include "kvm/rtc.h" 30 #include "kvm/sdl.h" 31 #include "kvm/vnc.h" 32 #include "kvm/guest_compat.h" 33 #include "kvm/pci-shmem.h" 34 #include "kvm/kvm-ipc.h" 35 #include "kvm/builtin-debug.h" 36 37 #include <linux/types.h> 38 #include <linux/err.h> 39 40 #include <sys/utsname.h> 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <termios.h> 44 #include <signal.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <ctype.h> 49 #include <stdio.h> 50 51 #define DEFAULT_KVM_DEV "/dev/kvm" 52 #define DEFAULT_CONSOLE "serial" 53 #define DEFAULT_NETWORK "user" 54 #define DEFAULT_HOST_ADDR "192.168.33.1" 55 #define DEFAULT_GUEST_ADDR "192.168.33.15" 56 #define DEFAULT_GUEST_MAC "02:15:15:15:15:15" 57 #define DEFAULT_HOST_MAC "02:01:01:01:01:01" 58 #define DEFAULT_SCRIPT "none" 59 const char *DEFAULT_SANDBOX_FILENAME = "guest/sandbox.sh"; 60 61 #define MB_SHIFT (20) 62 #define KB_SHIFT (10) 63 #define GB_SHIFT (30) 64 #define MIN_RAM_SIZE_MB (64ULL) 65 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) 66 67 struct kvm *kvm; 68 struct kvm_cpu **kvm_cpus; 69 __thread struct kvm_cpu *current_kvm_cpu; 70 71 static struct disk_image_params disk_image[MAX_DISK_IMAGES]; 72 static u64 ram_size; 73 static u8 image_count; 74 static u8 num_net_devices; 75 static bool virtio_rng; 76 static const char *kernel_cmdline; 77 static const char *kernel_filename; 78 static const char *vmlinux_filename; 79 static const char *initrd_filename; 80 static const char *firmware_filename; 81 static const char *console; 82 static const char *dev; 83 static const char *network; 84 static const char *host_ip; 85 static const char *guest_ip; 86 static const char *guest_mac; 87 static const char *host_mac; 88 static const char *script; 89 static const char *guest_name; 90 static const char *sandbox; 91 static const char *hugetlbfs_path; 92 static const char *custom_rootfs_name = "default"; 93 static struct virtio_net_params *net_params; 94 static bool single_step; 95 static bool vnc; 96 static bool sdl; 97 static bool balloon; 98 static bool using_rootfs; 99 static bool custom_rootfs; 100 static bool no_net; 101 static bool no_dhcp; 102 extern bool ioport_debug; 103 extern bool mmio_debug; 104 static int kvm_run_wrapper; 105 extern int active_console; 106 extern int debug_iodelay; 107 108 bool do_debug_print = false; 109 110 static int nrcpus; 111 static int vidmode = -1; 112 113 static const char * const run_usage[] = { 114 "lkvm run [<options>] [<kernel image>]", 115 NULL 116 }; 117 118 enum { 119 KVM_RUN_DEFAULT, 120 KVM_RUN_SANDBOX, 121 }; 122 123 void kvm_run_set_wrapper_sandbox(void) 124 { 125 kvm_run_wrapper = KVM_RUN_SANDBOX; 126 } 127 128 static int img_name_parser(const struct option *opt, const char *arg, int unset) 129 { 130 char *sep; 131 struct stat st; 132 char path[PATH_MAX]; 133 134 if (stat(arg, &st) == 0 && 135 S_ISDIR(st.st_mode)) { 136 char tmp[PATH_MAX]; 137 138 if (using_rootfs) 139 die("Please use only one rootfs directory atmost"); 140 141 if (realpath(arg, tmp) == 0 || 142 virtio_9p__register(kvm, tmp, "/dev/root") < 0) 143 die("Unable to initialize virtio 9p"); 144 using_rootfs = 1; 145 return 0; 146 } 147 148 snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg); 149 150 if (stat(path, &st) == 0 && 151 S_ISDIR(st.st_mode)) { 152 char tmp[PATH_MAX]; 153 154 if (using_rootfs) 155 die("Please use only one rootfs directory atmost"); 156 157 if (realpath(path, tmp) == 0 || 158 virtio_9p__register(kvm, tmp, "/dev/root") < 0) 159 die("Unable to initialize virtio 9p"); 160 if (virtio_9p__register(kvm, "/", "hostfs") < 0) 161 die("Unable to initialize virtio 9p"); 162 kvm_setup_resolv(arg); 163 using_rootfs = custom_rootfs = 1; 164 custom_rootfs_name = arg; 165 return 0; 166 } 167 168 if (image_count >= MAX_DISK_IMAGES) 169 die("Currently only 4 images are supported"); 170 171 disk_image[image_count].filename = arg; 172 sep = strstr(arg, ","); 173 if (sep) { 174 if (strcmp(sep + 1, "ro") == 0) 175 disk_image[image_count].readonly = true; 176 *sep = 0; 177 } 178 179 image_count++; 180 181 return 0; 182 } 183 184 static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset) 185 { 186 char *tag_name; 187 char tmp[PATH_MAX]; 188 189 /* 190 * 9p dir can be of the form dirname,tag_name or 191 * just dirname. In the later case we use the 192 * default tag name 193 */ 194 tag_name = strstr(arg, ","); 195 if (tag_name) { 196 *tag_name = '\0'; 197 tag_name++; 198 } 199 if (realpath(arg, tmp)) { 200 if (virtio_9p__register(kvm, tmp, tag_name) < 0) 201 die("Unable to initialize virtio 9p"); 202 } else 203 die("Failed resolving 9p path"); 204 return 0; 205 } 206 207 static int tty_parser(const struct option *opt, const char *arg, int unset) 208 { 209 int tty = atoi(arg); 210 211 term_set_tty(tty); 212 213 return 0; 214 } 215 216 static inline void str_to_mac(const char *str, char *mac) 217 { 218 sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 219 mac, mac+1, mac+2, mac+3, mac+4, mac+5); 220 } 221 static int set_net_param(struct virtio_net_params *p, const char *param, 222 const char *val) 223 { 224 if (strcmp(param, "guest_mac") == 0) { 225 str_to_mac(val, p->guest_mac); 226 } else if (strcmp(param, "mode") == 0) { 227 if (!strncmp(val, "user", 4)) { 228 int i; 229 230 for (i = 0; i < num_net_devices; i++) 231 if (net_params[i].mode == NET_MODE_USER) 232 die("Only one usermode network device allowed at a time"); 233 p->mode = NET_MODE_USER; 234 } else if (!strncmp(val, "tap", 3)) { 235 p->mode = NET_MODE_TAP; 236 } else if (!strncmp(val, "none", 4)) { 237 no_net = 1; 238 return -1; 239 } else 240 die("Unkown network mode %s, please use user, tap or none", network); 241 } else if (strcmp(param, "script") == 0) { 242 p->script = strdup(val); 243 } else if (strcmp(param, "guest_ip") == 0) { 244 p->guest_ip = strdup(val); 245 } else if (strcmp(param, "host_ip") == 0) { 246 p->host_ip = strdup(val); 247 } else if (strcmp(param, "trans") == 0) { 248 p->trans = strdup(val); 249 } else if (strcmp(param, "vhost") == 0) { 250 p->vhost = atoi(val); 251 } else if (strcmp(param, "fd") == 0) { 252 p->fd = atoi(val); 253 } 254 255 return 0; 256 } 257 258 static int netdev_parser(const struct option *opt, const char *arg, int unset) 259 { 260 struct virtio_net_params p; 261 char *buf = NULL, *cmd = NULL, *cur = NULL; 262 bool on_cmd = true; 263 264 if (arg) { 265 buf = strdup(arg); 266 if (buf == NULL) 267 die("Failed allocating new net buffer"); 268 cur = strtok(buf, ",="); 269 } 270 271 p = (struct virtio_net_params) { 272 .guest_ip = DEFAULT_GUEST_ADDR, 273 .host_ip = DEFAULT_HOST_ADDR, 274 .script = DEFAULT_SCRIPT, 275 .mode = NET_MODE_TAP, 276 }; 277 278 str_to_mac(DEFAULT_GUEST_MAC, p.guest_mac); 279 p.guest_mac[5] += num_net_devices; 280 281 while (cur) { 282 if (on_cmd) { 283 cmd = cur; 284 } else { 285 if (set_net_param(&p, cmd, cur) < 0) 286 goto done; 287 } 288 on_cmd = !on_cmd; 289 290 cur = strtok(NULL, ",="); 291 }; 292 293 num_net_devices++; 294 295 net_params = realloc(net_params, num_net_devices * sizeof(*net_params)); 296 if (net_params == NULL) 297 die("Failed adding new network device"); 298 299 net_params[num_net_devices - 1] = p; 300 301 done: 302 free(buf); 303 return 0; 304 } 305 306 static int shmem_parser(const struct option *opt, const char *arg, int unset) 307 { 308 const u64 default_size = SHMEM_DEFAULT_SIZE; 309 const u64 default_phys_addr = SHMEM_DEFAULT_ADDR; 310 const char *default_handle = SHMEM_DEFAULT_HANDLE; 311 struct shmem_info *si = malloc(sizeof(struct shmem_info)); 312 u64 phys_addr; 313 u64 size; 314 char *handle = NULL; 315 int create = 0; 316 const char *p = arg; 317 char *next; 318 int base = 10; 319 int verbose = 0; 320 321 const int skip_pci = strlen("pci:"); 322 if (verbose) 323 pr_info("shmem_parser(%p,%s,%d)", opt, arg, unset); 324 /* parse out optional addr family */ 325 if (strcasestr(p, "pci:")) { 326 p += skip_pci; 327 } else if (strcasestr(p, "mem:")) { 328 die("I can't add to E820 map yet.\n"); 329 } 330 /* parse out physical addr */ 331 base = 10; 332 if (strcasestr(p, "0x")) 333 base = 16; 334 phys_addr = strtoll(p, &next, base); 335 if (next == p && phys_addr == 0) { 336 pr_info("shmem: no physical addr specified, using default."); 337 phys_addr = default_phys_addr; 338 } 339 if (*next != ':' && *next != '\0') 340 die("shmem: unexpected chars after phys addr.\n"); 341 if (*next == '\0') 342 p = next; 343 else 344 p = next + 1; 345 /* parse out size */ 346 base = 10; 347 if (strcasestr(p, "0x")) 348 base = 16; 349 size = strtoll(p, &next, base); 350 if (next == p && size == 0) { 351 pr_info("shmem: no size specified, using default."); 352 size = default_size; 353 } 354 /* look for [KMGkmg][Bb]* uses base 2. */ 355 int skip_B = 0; 356 if (strspn(next, "KMGkmg")) { /* might have a prefix */ 357 if (*(next + 1) == 'B' || *(next + 1) == 'b') 358 skip_B = 1; 359 switch (*next) { 360 case 'K': 361 case 'k': 362 size = size << KB_SHIFT; 363 break; 364 case 'M': 365 case 'm': 366 size = size << MB_SHIFT; 367 break; 368 case 'G': 369 case 'g': 370 size = size << GB_SHIFT; 371 break; 372 default: 373 die("shmem: bug in detecting size prefix."); 374 break; 375 } 376 next += 1 + skip_B; 377 } 378 if (*next != ':' && *next != '\0') { 379 die("shmem: unexpected chars after phys size. <%c><%c>\n", 380 *next, *p); 381 } 382 if (*next == '\0') 383 p = next; 384 else 385 p = next + 1; 386 /* parse out optional shmem handle */ 387 const int skip_handle = strlen("handle="); 388 next = strcasestr(p, "handle="); 389 if (*p && next) { 390 if (p != next) 391 die("unexpected chars before handle\n"); 392 p += skip_handle; 393 next = strchrnul(p, ':'); 394 if (next - p) { 395 handle = malloc(next - p + 1); 396 strncpy(handle, p, next - p); 397 handle[next - p] = '\0'; /* just in case. */ 398 } 399 if (*next == '\0') 400 p = next; 401 else 402 p = next + 1; 403 } 404 /* parse optional create flag to see if we should create shm seg. */ 405 if (*p && strcasestr(p, "create")) { 406 create = 1; 407 p += strlen("create"); 408 } 409 if (*p != '\0') 410 die("shmem: unexpected trailing chars\n"); 411 if (handle == NULL) { 412 handle = malloc(strlen(default_handle) + 1); 413 strcpy(handle, default_handle); 414 } 415 if (verbose) { 416 pr_info("shmem: phys_addr = %llx", phys_addr); 417 pr_info("shmem: size = %llx", size); 418 pr_info("shmem: handle = %s", handle); 419 pr_info("shmem: create = %d", create); 420 } 421 422 si->phys_addr = phys_addr; 423 si->size = size; 424 si->handle = handle; 425 si->create = create; 426 pci_shmem__register_mem(si); /* ownership of si, etc. passed on. */ 427 return 0; 428 } 429 430 static const struct option options[] = { 431 OPT_GROUP("Basic options:"), 432 OPT_STRING('\0', "name", &guest_name, "guest name", 433 "A name for the guest"), 434 OPT_INTEGER('c', "cpus", &nrcpus, "Number of CPUs"), 435 OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), 436 OPT_CALLBACK('\0', "shmem", NULL, 437 "[pci:]<addr>:<size>[:handle=<handle>][:create]", 438 "Share host shmem with guest via pci device", 439 shmem_parser), 440 OPT_CALLBACK('d', "disk", NULL, "image or rootfs_dir", "Disk image or rootfs directory", img_name_parser), 441 OPT_BOOLEAN('\0', "balloon", &balloon, "Enable virtio balloon"), 442 OPT_BOOLEAN('\0', "vnc", &vnc, "Enable VNC framebuffer"), 443 OPT_BOOLEAN('\0', "sdl", &sdl, "Enable SDL framebuffer"), 444 OPT_BOOLEAN('\0', "rng", &virtio_rng, "Enable virtio Random Number Generator"), 445 OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name", 446 "Enable virtio 9p to share files between host and guest", virtio_9p_rootdir_parser), 447 OPT_STRING('\0', "console", &console, "serial, virtio or hv", 448 "Console to use"), 449 OPT_STRING('\0', "dev", &dev, "device_file", "KVM device file"), 450 OPT_CALLBACK('\0', "tty", NULL, "tty id", 451 "Remap guest TTY into a pty on the host", 452 tty_parser), 453 OPT_STRING('\0', "sandbox", &sandbox, "script", 454 "Run this script when booting into custom rootfs"), 455 OPT_STRING('\0', "hugetlbfs", &hugetlbfs_path, "path", "Hugetlbfs path"), 456 457 OPT_GROUP("Kernel options:"), 458 OPT_STRING('k', "kernel", &kernel_filename, "kernel", 459 "Kernel to boot in virtual machine"), 460 OPT_STRING('i', "initrd", &initrd_filename, "initrd", 461 "Initial RAM disk image"), 462 OPT_STRING('p', "params", &kernel_cmdline, "params", 463 "Kernel command line arguments"), 464 OPT_STRING('f', "firmware", &firmware_filename, "firmware", 465 "Firmware image to boot in virtual machine"), 466 467 OPT_GROUP("Networking options:"), 468 OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params", 469 "Create a new guest NIC", 470 netdev_parser, NULL), 471 OPT_BOOLEAN('\0', "no-dhcp", &no_dhcp, "Disable kernel DHCP in rootfs mode"), 472 473 OPT_GROUP("BIOS options:"), 474 OPT_INTEGER('\0', "vidmode", &vidmode, 475 "Video mode"), 476 477 OPT_GROUP("Debug options:"), 478 OPT_BOOLEAN('\0', "debug", &do_debug_print, 479 "Enable debug messages"), 480 OPT_BOOLEAN('\0', "debug-single-step", &single_step, 481 "Enable single stepping"), 482 OPT_BOOLEAN('\0', "debug-ioport", &ioport_debug, 483 "Enable ioport debugging"), 484 OPT_BOOLEAN('\0', "debug-mmio", &mmio_debug, 485 "Enable MMIO debugging"), 486 OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay, 487 "Delay IO by millisecond"), 488 OPT_END() 489 }; 490 491 /* 492 * Serialize debug printout so that the output of multiple vcpus does not 493 * get mixed up: 494 */ 495 static int printout_done; 496 497 static void handle_sigusr1(int sig) 498 { 499 struct kvm_cpu *cpu = current_kvm_cpu; 500 int fd = kvm_cpu__get_debug_fd(); 501 502 if (!cpu || cpu->needs_nmi) 503 return; 504 505 dprintf(fd, "\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id); 506 kvm_cpu__show_registers(cpu); 507 kvm_cpu__show_code(cpu); 508 kvm_cpu__show_page_tables(cpu); 509 fflush(stdout); 510 printout_done = 1; 511 mb(); 512 } 513 514 /* Pause/resume the guest using SIGUSR2 */ 515 static int is_paused; 516 517 static void handle_pause(int fd, u32 type, u32 len, u8 *msg) 518 { 519 if (WARN_ON(len)) 520 return; 521 522 if (type == KVM_IPC_RESUME && is_paused) { 523 kvm->vm_state = KVM_VMSTATE_RUNNING; 524 kvm__continue(); 525 } else if (type == KVM_IPC_PAUSE && !is_paused) { 526 kvm->vm_state = KVM_VMSTATE_PAUSED; 527 kvm__pause(); 528 } else { 529 return; 530 } 531 532 is_paused = !is_paused; 533 } 534 535 static void handle_vmstate(int fd, u32 type, u32 len, u8 *msg) 536 { 537 int r = 0; 538 539 if (type == KVM_IPC_VMSTATE) 540 r = write(fd, &kvm->vm_state, sizeof(kvm->vm_state)); 541 542 if (r < 0) 543 pr_warning("Failed sending VMSTATE"); 544 } 545 546 static void handle_debug(int fd, u32 type, u32 len, u8 *msg) 547 { 548 int i; 549 struct debug_cmd_params *params; 550 u32 dbg_type; 551 u32 vcpu; 552 553 if (WARN_ON(type != KVM_IPC_DEBUG || len != sizeof(*params))) 554 return; 555 556 params = (void *)msg; 557 dbg_type = params->dbg_type; 558 vcpu = params->cpu; 559 560 if (dbg_type & KVM_DEBUG_CMD_TYPE_NMI) { 561 if ((int)vcpu >= kvm->nrcpus) 562 return; 563 564 kvm_cpus[vcpu]->needs_nmi = 1; 565 pthread_kill(kvm_cpus[vcpu]->thread, SIGUSR1); 566 } 567 568 if (!(dbg_type & KVM_DEBUG_CMD_TYPE_DUMP)) 569 return; 570 571 for (i = 0; i < nrcpus; i++) { 572 struct kvm_cpu *cpu = kvm_cpus[i]; 573 574 if (!cpu) 575 continue; 576 577 printout_done = 0; 578 579 kvm_cpu__set_debug_fd(fd); 580 pthread_kill(cpu->thread, SIGUSR1); 581 /* 582 * Wait for the vCPU to dump state before signalling 583 * the next thread. Since this is debug code it does 584 * not matter that we are burning CPU time a bit: 585 */ 586 while (!printout_done) 587 mb(); 588 } 589 590 close(fd); 591 592 serial8250__inject_sysrq(kvm); 593 } 594 595 static void handle_sigalrm(int sig) 596 { 597 kvm__arch_periodic_poll(kvm); 598 } 599 600 static void handle_stop(int fd, u32 type, u32 len, u8 *msg) 601 { 602 if (WARN_ON(type != KVM_IPC_STOP || len)) 603 return; 604 605 kvm_cpu__reboot(); 606 } 607 608 static void *kvm_cpu_thread(void *arg) 609 { 610 current_kvm_cpu = arg; 611 612 if (kvm_cpu__start(current_kvm_cpu)) 613 goto panic_kvm; 614 615 return (void *) (intptr_t) 0; 616 617 panic_kvm: 618 fprintf(stderr, "KVM exit reason: %u (\"%s\")\n", 619 current_kvm_cpu->kvm_run->exit_reason, 620 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 621 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 622 fprintf(stderr, "KVM exit code: 0x%Lu\n", 623 current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 624 625 kvm_cpu__set_debug_fd(STDOUT_FILENO); 626 kvm_cpu__show_registers(current_kvm_cpu); 627 kvm_cpu__show_code(current_kvm_cpu); 628 kvm_cpu__show_page_tables(current_kvm_cpu); 629 630 return (void *) (intptr_t) 1; 631 } 632 633 static char kernel[PATH_MAX]; 634 635 static const char *host_kernels[] = { 636 "/boot/vmlinuz", 637 "/boot/bzImage", 638 NULL 639 }; 640 641 static const char *default_kernels[] = { 642 "./bzImage", 643 "arch/" BUILD_ARCH "/boot/bzImage", 644 "../../arch/" BUILD_ARCH "/boot/bzImage", 645 NULL 646 }; 647 648 static const char *default_vmlinux[] = { 649 "vmlinux", 650 "../../../vmlinux", 651 "../../vmlinux", 652 NULL 653 }; 654 655 static void kernel_usage_with_options(void) 656 { 657 const char **k; 658 struct utsname uts; 659 660 fprintf(stderr, "Fatal: could not find default kernel image in:\n"); 661 k = &default_kernels[0]; 662 while (*k) { 663 fprintf(stderr, "\t%s\n", *k); 664 k++; 665 } 666 667 if (uname(&uts) < 0) 668 return; 669 670 k = &host_kernels[0]; 671 while (*k) { 672 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 673 return; 674 fprintf(stderr, "\t%s\n", kernel); 675 k++; 676 } 677 fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n", 678 KVM_BINARY_NAME); 679 } 680 681 static u64 host_ram_size(void) 682 { 683 long page_size; 684 long nr_pages; 685 686 nr_pages = sysconf(_SC_PHYS_PAGES); 687 if (nr_pages < 0) { 688 pr_warning("sysconf(_SC_PHYS_PAGES) failed"); 689 return 0; 690 } 691 692 page_size = sysconf(_SC_PAGE_SIZE); 693 if (page_size < 0) { 694 pr_warning("sysconf(_SC_PAGE_SIZE) failed"); 695 return 0; 696 } 697 698 return (nr_pages * page_size) >> MB_SHIFT; 699 } 700 701 /* 702 * If user didn't specify how much memory it wants to allocate for the guest, 703 * avoid filling the whole host RAM. 704 */ 705 #define RAM_SIZE_RATIO 0.8 706 707 static u64 get_ram_size(int nr_cpus) 708 { 709 u64 available; 710 u64 ram_size; 711 712 ram_size = 64 * (nr_cpus + 3); 713 714 available = host_ram_size() * RAM_SIZE_RATIO; 715 if (!available) 716 available = MIN_RAM_SIZE_MB; 717 718 if (ram_size > available) 719 ram_size = available; 720 721 return ram_size; 722 } 723 724 static const char *find_kernel(void) 725 { 726 const char **k; 727 struct stat st; 728 struct utsname uts; 729 730 k = &default_kernels[0]; 731 while (*k) { 732 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) { 733 k++; 734 continue; 735 } 736 strncpy(kernel, *k, PATH_MAX); 737 return kernel; 738 } 739 740 if (uname(&uts) < 0) 741 return NULL; 742 743 k = &host_kernels[0]; 744 while (*k) { 745 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 746 return NULL; 747 748 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) { 749 k++; 750 continue; 751 } 752 return kernel; 753 754 } 755 return NULL; 756 } 757 758 static const char *find_vmlinux(void) 759 { 760 const char **vmlinux; 761 762 vmlinux = &default_vmlinux[0]; 763 while (*vmlinux) { 764 struct stat st; 765 766 if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) { 767 vmlinux++; 768 continue; 769 } 770 return *vmlinux; 771 } 772 return NULL; 773 } 774 775 void kvm_run_help(void) 776 { 777 usage_with_options(run_usage, options); 778 } 779 780 static int kvm_custom_stage2(void) 781 { 782 char tmp[PATH_MAX], dst[PATH_MAX], *src; 783 const char *rootfs = custom_rootfs_name; 784 int r; 785 786 src = realpath("guest/init_stage2", NULL); 787 if (src == NULL) 788 return -ENOMEM; 789 790 snprintf(tmp, PATH_MAX, "%s%s/virt/init_stage2", kvm__get_dir(), rootfs); 791 remove(tmp); 792 793 snprintf(dst, PATH_MAX, "/host/%s", src); 794 r = symlink(dst, tmp); 795 free(src); 796 797 return r; 798 } 799 800 static int kvm_run_set_sandbox(void) 801 { 802 const char *guestfs_name = custom_rootfs_name; 803 char path[PATH_MAX], script[PATH_MAX], *tmp; 804 805 snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name); 806 807 remove(path); 808 809 if (sandbox == NULL) 810 return 0; 811 812 tmp = realpath(sandbox, NULL); 813 if (tmp == NULL) 814 return -ENOMEM; 815 816 snprintf(script, PATH_MAX, "/host/%s", tmp); 817 free(tmp); 818 819 return symlink(script, path); 820 } 821 822 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg) 823 { 824 const char *single_quote; 825 826 if (!*arg) { /* zero length string */ 827 if (write(fd, "''", 2) <= 0) 828 die("Failed writing sandbox script"); 829 return; 830 } 831 832 while (*arg) { 833 single_quote = strchrnul(arg, '\''); 834 835 /* write non-single-quote string as #('string') */ 836 if (arg != single_quote) { 837 if (write(fd, "'", 1) <= 0 || 838 write(fd, arg, single_quote - arg) <= 0 || 839 write(fd, "'", 1) <= 0) 840 die("Failed writing sandbox script"); 841 } 842 843 /* write single quote as #("'") */ 844 if (*single_quote) { 845 if (write(fd, "\"'\"", 3) <= 0) 846 die("Failed writing sandbox script"); 847 } else 848 break; 849 850 arg = single_quote + 1; 851 } 852 } 853 854 static void resolve_program(const char *src, char *dst, size_t len) 855 { 856 struct stat st; 857 int err; 858 859 err = stat(src, &st); 860 861 if (!err && S_ISREG(st.st_mode)) { 862 char resolved_path[PATH_MAX]; 863 864 if (!realpath(src, resolved_path)) 865 die("Unable to resolve program %s: %s\n", src, strerror(errno)); 866 867 snprintf(dst, len, "/host%s", resolved_path); 868 } else 869 strncpy(dst, src, len); 870 } 871 872 static void kvm_run_write_sandbox_cmd(const char **argv, int argc) 873 { 874 const char script_hdr[] = "#! /bin/bash\n\n"; 875 char program[PATH_MAX]; 876 int fd; 877 878 remove(sandbox); 879 880 fd = open(sandbox, O_RDWR | O_CREAT, 0777); 881 if (fd < 0) 882 die("Failed creating sandbox script"); 883 884 if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0) 885 die("Failed writing sandbox script"); 886 887 resolve_program(argv[0], program, PATH_MAX); 888 kvm_write_sandbox_cmd_exactly(fd, program); 889 890 argv++; 891 argc--; 892 893 while (argc) { 894 if (write(fd, " ", 1) <= 0) 895 die("Failed writing sandbox script"); 896 897 kvm_write_sandbox_cmd_exactly(fd, argv[0]); 898 argv++; 899 argc--; 900 } 901 if (write(fd, "\n", 1) <= 0) 902 die("Failed writing sandbox script"); 903 904 close(fd); 905 } 906 907 static int kvm_cmd_run_init(int argc, const char **argv) 908 { 909 static char real_cmdline[2048], default_name[20]; 910 struct framebuffer *fb = NULL; 911 unsigned int nr_online_cpus; 912 int max_cpus, recommended_cpus; 913 int i, r; 914 915 signal(SIGALRM, handle_sigalrm); 916 kvm_ipc__register_handler(KVM_IPC_DEBUG, handle_debug); 917 signal(SIGUSR1, handle_sigusr1); 918 kvm_ipc__register_handler(KVM_IPC_PAUSE, handle_pause); 919 kvm_ipc__register_handler(KVM_IPC_RESUME, handle_pause); 920 kvm_ipc__register_handler(KVM_IPC_STOP, handle_stop); 921 kvm_ipc__register_handler(KVM_IPC_VMSTATE, handle_vmstate); 922 923 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); 924 925 while (argc != 0) { 926 argc = parse_options(argc, argv, options, run_usage, 927 PARSE_OPT_STOP_AT_NON_OPTION | 928 PARSE_OPT_KEEP_DASHDASH); 929 if (argc != 0) { 930 /* Cusrom options, should have been handled elsewhere */ 931 if (strcmp(argv[0], "--") == 0) { 932 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 933 sandbox = DEFAULT_SANDBOX_FILENAME; 934 kvm_run_write_sandbox_cmd(argv+1, argc-1); 935 break; 936 } 937 } 938 939 if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kernel_filename) || 940 (kvm_run_wrapper == KVM_RUN_SANDBOX && sandbox)) { 941 fprintf(stderr, "Cannot handle parameter: " 942 "%s\n", argv[0]); 943 usage_with_options(run_usage, options); 944 return EINVAL; 945 } 946 if (kvm_run_wrapper == KVM_RUN_SANDBOX) { 947 /* 948 * first unhandled parameter is treated as 949 * sandbox command 950 */ 951 sandbox = DEFAULT_SANDBOX_FILENAME; 952 kvm_run_write_sandbox_cmd(argv, argc); 953 } else { 954 /* 955 * first unhandled parameter is treated as a kernel 956 * image 957 */ 958 kernel_filename = argv[0]; 959 } 960 argv++; 961 argc--; 962 } 963 964 } 965 966 if (!kernel_filename) 967 kernel_filename = find_kernel(); 968 969 if (!kernel_filename) { 970 kernel_usage_with_options(); 971 return EINVAL; 972 } 973 974 vmlinux_filename = find_vmlinux(); 975 976 if (nrcpus == 0) 977 nrcpus = nr_online_cpus; 978 979 if (!ram_size) 980 ram_size = get_ram_size(nrcpus); 981 982 if (ram_size < MIN_RAM_SIZE_MB) 983 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB); 984 985 if (ram_size > host_ram_size()) 986 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size, host_ram_size()); 987 988 ram_size <<= MB_SHIFT; 989 990 if (!dev) 991 dev = DEFAULT_KVM_DEV; 992 993 if (!console) 994 console = DEFAULT_CONSOLE; 995 996 if (!strncmp(console, "virtio", 6)) 997 active_console = CONSOLE_VIRTIO; 998 else if (!strncmp(console, "serial", 6)) 999 active_console = CONSOLE_8250; 1000 else if (!strncmp(console, "hv", 2)) 1001 active_console = CONSOLE_HV; 1002 else 1003 pr_warning("No console!"); 1004 1005 if (!host_ip) 1006 host_ip = DEFAULT_HOST_ADDR; 1007 1008 if (!guest_ip) 1009 guest_ip = DEFAULT_GUEST_ADDR; 1010 1011 if (!guest_mac) 1012 guest_mac = DEFAULT_GUEST_MAC; 1013 1014 if (!host_mac) 1015 host_mac = DEFAULT_HOST_MAC; 1016 1017 if (!script) 1018 script = DEFAULT_SCRIPT; 1019 1020 term_init(); 1021 1022 if (!guest_name) { 1023 if (custom_rootfs) { 1024 guest_name = custom_rootfs_name; 1025 } else { 1026 sprintf(default_name, "guest-%u", getpid()); 1027 guest_name = default_name; 1028 } 1029 } 1030 1031 kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name); 1032 if (IS_ERR(kvm)) { 1033 r = PTR_ERR(kvm); 1034 goto fail; 1035 } 1036 1037 kvm->single_step = single_step; 1038 1039 r = ioeventfd__init(kvm); 1040 if (r < 0) { 1041 pr_err("ioeventfd__init() failed with error %d\n", r); 1042 goto fail; 1043 } 1044 1045 max_cpus = kvm__max_cpus(kvm); 1046 recommended_cpus = kvm__recommended_cpus(kvm); 1047 1048 if (nrcpus > max_cpus) { 1049 printf(" # Limit the number of CPUs to %d\n", max_cpus); 1050 nrcpus = max_cpus; 1051 } else if (nrcpus > recommended_cpus) { 1052 printf(" # Warning: The maximum recommended amount of VCPUs" 1053 " is %d\n", recommended_cpus); 1054 } 1055 1056 kvm->nrcpus = nrcpus; 1057 1058 /* Alloc one pointer too many, so array ends up 0-terminated */ 1059 kvm_cpus = calloc(nrcpus + 1, sizeof(void *)); 1060 if (!kvm_cpus) 1061 die("Couldn't allocate array for %d CPUs", nrcpus); 1062 1063 r = irq__init(kvm); 1064 if (r < 0) { 1065 pr_err("irq__init() failed with error %d\n", r); 1066 goto fail; 1067 } 1068 1069 r = pci__init(kvm); 1070 if (r < 0) { 1071 pr_err("pci__init() failed with error %d\n", r); 1072 goto fail; 1073 } 1074 1075 r = ioport__init(kvm); 1076 if (r < 0) { 1077 pr_err("ioport__init() failed with error %d\n", r); 1078 goto fail; 1079 } 1080 1081 /* 1082 * vidmode should be either specified 1083 * either set by default 1084 */ 1085 if (vnc || sdl) { 1086 if (vidmode == -1) 1087 vidmode = 0x312; 1088 } else { 1089 vidmode = 0; 1090 } 1091 1092 memset(real_cmdline, 0, sizeof(real_cmdline)); 1093 kvm__arch_set_cmdline(real_cmdline, vnc || sdl); 1094 1095 if (strlen(real_cmdline) > 0) 1096 strcat(real_cmdline, " "); 1097 1098 if (kernel_cmdline) 1099 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); 1100 1101 if (!using_rootfs && !disk_image[0].filename && !initrd_filename) { 1102 char tmp[PATH_MAX]; 1103 1104 kvm_setup_create_new(custom_rootfs_name); 1105 kvm_setup_resolv(custom_rootfs_name); 1106 1107 snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default"); 1108 if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) 1109 die("Unable to initialize virtio 9p"); 1110 if (virtio_9p__register(kvm, "/", "hostfs") < 0) 1111 die("Unable to initialize virtio 9p"); 1112 using_rootfs = custom_rootfs = 1; 1113 } 1114 1115 if (using_rootfs) { 1116 strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p"); 1117 if (custom_rootfs) { 1118 kvm_run_set_sandbox(); 1119 1120 strcat(real_cmdline, " init=/virt/init"); 1121 1122 if (!no_dhcp) 1123 strcat(real_cmdline, " ip=dhcp"); 1124 if (kvm_custom_stage2()) 1125 die("Failed linking stage 2 of init."); 1126 } 1127 } else if (!strstr(real_cmdline, "root=")) { 1128 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); 1129 } 1130 1131 if (image_count) { 1132 kvm->nr_disks = image_count; 1133 kvm->disks = disk_image__open_all((struct disk_image_params *)&disk_image, image_count); 1134 if (IS_ERR(kvm->disks)) { 1135 r = PTR_ERR(kvm->disks); 1136 pr_err("disk_image__open_all() failed with error %ld\n", 1137 PTR_ERR(kvm->disks)); 1138 goto fail; 1139 } 1140 } 1141 1142 printf(" # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME, 1143 kernel_filename, ram_size / 1024 / 1024, nrcpus, guest_name); 1144 1145 if (!firmware_filename) { 1146 if (!kvm__load_kernel(kvm, kernel_filename, 1147 initrd_filename, real_cmdline, vidmode)) 1148 die("unable to load kernel %s", kernel_filename); 1149 1150 kvm->vmlinux = vmlinux_filename; 1151 r = symbol_init(kvm); 1152 if (r < 0) 1153 pr_debug("symbol_init() failed with error %d\n", r); 1154 } 1155 1156 ioport__setup_arch(); 1157 1158 r = rtc__init(kvm); 1159 if (r < 0) { 1160 pr_err("rtc__init() failed with error %d\n", r); 1161 goto fail; 1162 } 1163 1164 r = serial8250__init(kvm); 1165 if (r < 0) { 1166 pr_err("serial__init() failed with error %d\n", r); 1167 goto fail; 1168 } 1169 1170 r = virtio_blk__init(kvm); 1171 if (r < 0) { 1172 pr_err("virtio_blk__init() failed with error %d\n", r); 1173 goto fail; 1174 } 1175 1176 if (active_console == CONSOLE_VIRTIO) 1177 virtio_console__init(kvm); 1178 1179 if (virtio_rng) 1180 virtio_rng__init(kvm); 1181 1182 if (balloon) 1183 virtio_bln__init(kvm); 1184 1185 if (!network) 1186 network = DEFAULT_NETWORK; 1187 1188 virtio_9p__init(kvm); 1189 1190 for (i = 0; i < num_net_devices; i++) { 1191 net_params[i].kvm = kvm; 1192 virtio_net__init(&net_params[i]); 1193 } 1194 1195 if (num_net_devices == 0 && no_net == 0) { 1196 struct virtio_net_params net_params; 1197 1198 net_params = (struct virtio_net_params) { 1199 .guest_ip = guest_ip, 1200 .host_ip = host_ip, 1201 .kvm = kvm, 1202 .script = script, 1203 .mode = NET_MODE_USER, 1204 }; 1205 str_to_mac(guest_mac, net_params.guest_mac); 1206 str_to_mac(host_mac, net_params.host_mac); 1207 1208 virtio_net__init(&net_params); 1209 } 1210 1211 kvm__init_ram(kvm); 1212 1213 #ifdef CONFIG_X86 1214 kbd__init(kvm); 1215 #endif 1216 1217 pci_shmem__init(kvm); 1218 1219 if (vnc || sdl) { 1220 fb = vesa__init(kvm); 1221 if (IS_ERR(fb)) { 1222 pr_err("vesa__init() failed with error %ld\n", PTR_ERR(fb)); 1223 goto fail; 1224 } 1225 } 1226 1227 if (vnc && fb) { 1228 r = vnc__init(fb); 1229 if (r < 0) { 1230 pr_err("vnc__init() failed with error %d\n", r); 1231 goto fail; 1232 } 1233 } 1234 1235 if (sdl && fb) { 1236 sdl__init(fb); 1237 if (r < 0) { 1238 pr_err("sdl__init() failed with error %d\n", r); 1239 goto fail; 1240 } 1241 } 1242 1243 r = fb__start(); 1244 if (r < 0) { 1245 pr_err("fb__init() failed with error %d\n", r); 1246 goto fail; 1247 } 1248 1249 /* Device init all done; firmware init must 1250 * come after this (it may set up device trees etc.) 1251 */ 1252 1253 kvm__start_timer(kvm); 1254 1255 if (firmware_filename) { 1256 if (!kvm__load_firmware(kvm, firmware_filename)) 1257 die("unable to load firmware image %s: %s", firmware_filename, strerror(errno)); 1258 } else { 1259 kvm__arch_setup_firmware(kvm); 1260 if (r < 0) { 1261 pr_err("kvm__arch_setup_firmware() failed with error %d\n", r); 1262 goto fail; 1263 } 1264 } 1265 1266 for (i = 0; i < nrcpus; i++) { 1267 kvm_cpus[i] = kvm_cpu__init(kvm, i); 1268 if (!kvm_cpus[i]) 1269 die("unable to initialize KVM VCPU"); 1270 } 1271 1272 thread_pool__init(nr_online_cpus); 1273 fail: 1274 return r; 1275 } 1276 1277 static int kvm_cmd_run_work(void) 1278 { 1279 int i, r = -1; 1280 void *ret = NULL; 1281 1282 for (i = 0; i < nrcpus; i++) { 1283 if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0) 1284 die("unable to create KVM VCPU thread"); 1285 } 1286 1287 /* Only VCPU #0 is going to exit by itself when shutting down */ 1288 if (pthread_join(kvm_cpus[0]->thread, &ret) != 0) 1289 r = 0; 1290 1291 kvm_cpu__delete(kvm_cpus[0]); 1292 kvm_cpus[0] = NULL; 1293 1294 for (i = 1; i < nrcpus; i++) { 1295 if (kvm_cpus[i]->is_running) { 1296 pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT); 1297 if (pthread_join(kvm_cpus[i]->thread, &ret) != 0) 1298 die("pthread_join"); 1299 kvm_cpu__delete(kvm_cpus[i]); 1300 } 1301 if (ret == NULL) 1302 r = 0; 1303 } 1304 1305 return r; 1306 } 1307 1308 static void kvm_cmd_run_exit(int guest_ret) 1309 { 1310 int r = 0; 1311 1312 compat__print_all_messages(); 1313 1314 r = symbol_exit(kvm); 1315 if (r < 0) 1316 pr_warning("symbol_exit() failed with error %d\n", r); 1317 1318 r = irq__exit(kvm); 1319 if (r < 0) 1320 pr_warning("irq__exit() failed with error %d\n", r); 1321 1322 fb__stop(); 1323 1324 r = virtio_blk__exit(kvm); 1325 if (r < 0) 1326 pr_warning("virtio_blk__exit() failed with error %d\n", r); 1327 1328 r = virtio_rng__exit(kvm); 1329 if (r < 0) 1330 pr_warning("virtio_rng__exit() failed with error %d\n", r); 1331 1332 r = disk_image__close_all(kvm->disks, image_count); 1333 if (r < 0) 1334 pr_warning("disk_image__close_all() failed with error %d\n", r); 1335 1336 r = serial8250__exit(kvm); 1337 if (r < 0) 1338 pr_warning("serial8250__exit() failed with error %d\n", r); 1339 1340 r = rtc__exit(kvm); 1341 if (r < 0) 1342 pr_warning("rtc__exit() failed with error %d\n", r); 1343 1344 r = kvm__arch_free_firmware(kvm); 1345 if (r < 0) 1346 pr_warning("kvm__arch_free_firmware() failed with error %d\n", r); 1347 1348 r = ioport__exit(kvm); 1349 if (r < 0) 1350 pr_warning("ioport__exit() failed with error %d\n", r); 1351 1352 r = ioeventfd__exit(kvm); 1353 if (r < 0) 1354 pr_warning("ioeventfd__exit() failed with error %d\n", r); 1355 1356 r = pci__exit(kvm); 1357 if (r < 0) 1358 pr_warning("pci__exit() failed with error %d\n", r); 1359 1360 r = kvm__exit(kvm); 1361 if (r < 0) 1362 pr_warning("pci__exit() failed with error %d\n", r); 1363 1364 free(kvm_cpus); 1365 1366 if (guest_ret == 0) 1367 printf("\n # KVM session ended normally.\n"); 1368 } 1369 1370 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 1371 { 1372 int r, ret = -EFAULT; 1373 1374 r = kvm_cmd_run_init(argc, argv); 1375 if (r < 0) 1376 return r; 1377 1378 ret = kvm_cmd_run_work(); 1379 kvm_cmd_run_exit(ret); 1380 1381 return ret; 1382 } 1383