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