1 #include <stdio.h> 2 #include <string.h> 3 #include <signal.h> 4 #include <stdint.h> 5 #include <unistd.h> 6 #include <inttypes.h> 7 #include <stdlib.h> 8 #include <termios.h> 9 #include <sys/utsname.h> 10 #include <sys/types.h> 11 #include <sys/stat.h> 12 #include <ctype.h> 13 14 /* user defined header files */ 15 #include <linux/types.h> 16 #include <kvm/kvm.h> 17 #include <kvm/kvm-cpu.h> 18 #include <kvm/8250-serial.h> 19 #include <kvm/virtio-blk.h> 20 #include <kvm/virtio-net.h> 21 #include <kvm/virtio-console.h> 22 #include <kvm/virtio-rng.h> 23 #include <kvm/disk-image.h> 24 #include <kvm/util.h> 25 #include <kvm/pci.h> 26 #include <kvm/rtc.h> 27 #include <kvm/term.h> 28 #include <kvm/ioport.h> 29 #include <kvm/threadpool.h> 30 31 /* header files for gitish interface */ 32 #include <kvm/kvm-run.h> 33 #include <kvm/parse-options.h> 34 35 #define DEFAULT_KVM_DEV "/dev/kvm" 36 #define DEFAULT_CONSOLE "serial" 37 #define DEFAULT_NETWORK "virtio" 38 #define DEFAULT_HOST_ADDR "192.168.33.2" 39 #define DEFAULT_GUEST_MAC "00:11:22:33:44:55" 40 #define DEFAULT_SCRIPT "none" 41 42 #define MB_SHIFT (20) 43 #define MIN_RAM_SIZE_MB (64ULL) 44 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) 45 46 static struct kvm *kvm; 47 static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS]; 48 static __thread struct kvm_cpu *current_kvm_cpu; 49 50 static u64 ram_size = MIN_RAM_SIZE_MB; 51 static const char *kernel_cmdline; 52 static const char *kernel_filename; 53 static const char *initrd_filename; 54 static const char *image_filename; 55 static const char *console; 56 static const char *kvm_dev; 57 static const char *network; 58 static const char *host_ip_addr; 59 static const char *guest_mac; 60 static const char *script; 61 static bool single_step; 62 static bool readonly_image; 63 static bool virtio_rng; 64 extern bool ioport_debug; 65 extern int active_console; 66 67 static int nrcpus = 1; 68 69 static const char * const run_usage[] = { 70 "kvm run [<options>] [<kernel image>]", 71 NULL 72 }; 73 74 static const struct option options[] = { 75 OPT_GROUP("Basic options:"), 76 OPT_INTEGER('\0', "cpus", &nrcpus, "Number of CPUs"), 77 OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), 78 OPT_STRING('i', "image", &image_filename, "image", "Disk image"), 79 OPT_BOOLEAN('\0', "readonly", &readonly_image, 80 "Don't write changes back to disk image"), 81 OPT_STRING('c', "console", &console, "serial or virtio", 82 "Console to use"), 83 OPT_BOOLEAN('\0', "virtio-rng", &virtio_rng, 84 "Enable virtio Random Number Generator"), 85 86 OPT_GROUP("Kernel options:"), 87 OPT_STRING('k', "kernel", &kernel_filename, "kernel", 88 "Kernel to boot in virtual machine"), 89 OPT_STRING('r', "initrd", &initrd_filename, "initrd", 90 "Initial RAM disk image"), 91 OPT_STRING('p', "params", &kernel_cmdline, "params", 92 "Kernel command line arguments"), 93 94 OPT_GROUP("Networking options:"), 95 OPT_STRING('n', "network", &network, "virtio", 96 "Network to use"), 97 OPT_STRING('\0', "host-ip-addr", &host_ip_addr, "a.b.c.d", 98 "Assign this address to the host side networking"), 99 OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff", 100 "Assign this address to the guest side NIC"), 101 OPT_STRING('\0', "tapscript", &script, "Script path", 102 "Assign a script to process created tap device"), 103 OPT_GROUP("Debug options:"), 104 OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"), 105 OPT_BOOLEAN('s', "single-step", &single_step, 106 "Enable single stepping"), 107 OPT_BOOLEAN('g', "ioport-debug", &ioport_debug, 108 "Enable ioport debugging"), 109 OPT_END() 110 }; 111 112 static void handle_sigquit(int sig) 113 { 114 int i; 115 116 for (i = 0; i < nrcpus; i++) { 117 struct kvm_cpu *cpu = kvm_cpus[i]; 118 119 kvm_cpu__show_registers(cpu); 120 kvm_cpu__show_code(cpu); 121 kvm_cpu__show_page_tables(cpu); 122 } 123 124 serial8250__inject_sysrq(kvm); 125 } 126 127 static void handle_sigalrm(int sig) 128 { 129 serial8250__inject_interrupt(kvm); 130 virtio_console__inject_interrupt(kvm); 131 } 132 133 static void *kvm_cpu_thread(void *arg) 134 { 135 current_kvm_cpu = arg; 136 137 if (kvm_cpu__start(current_kvm_cpu)) 138 goto panic_kvm; 139 140 kvm_cpu__delete(current_kvm_cpu); 141 142 return (void *) (intptr_t) 0; 143 144 panic_kvm: 145 fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n", 146 current_kvm_cpu->kvm_run->exit_reason, 147 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 148 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 149 fprintf(stderr, "KVM exit code: 0x%Lu\n", 150 current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 151 152 kvm_cpu__show_registers(current_kvm_cpu); 153 kvm_cpu__show_code(current_kvm_cpu); 154 kvm_cpu__show_page_tables(current_kvm_cpu); 155 156 kvm_cpu__delete(current_kvm_cpu); 157 158 return (void *) (intptr_t) 1; 159 } 160 161 static char kernel[PATH_MAX]; 162 const char *host_kernels[] = { 163 "/boot/vmlinuz", 164 "/boot/bzImage", 165 NULL 166 }; 167 const char *default_kernels[] = { 168 "./bzImage", 169 "../../arch/x86/boot/bzImage", 170 NULL 171 }; 172 173 static void kernel_usage_with_options(void) 174 { 175 const char **k; 176 struct utsname uts; 177 178 fprintf(stderr, "Fatal: could not find default kernel image in:\n"); 179 k = &default_kernels[0]; 180 while (*k) { 181 fprintf(stderr, "\t%s\n", *k); 182 k++; 183 } 184 185 if (uname(&uts) < 0) 186 return; 187 188 k = &host_kernels[0]; 189 while (*k) { 190 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 191 return; 192 fprintf(stderr, "\t%s\n", kernel); 193 k++; 194 } 195 fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n"); 196 } 197 198 static const char *find_kernel(void) 199 { 200 const char **k; 201 struct stat st; 202 struct utsname uts; 203 204 k = &default_kernels[0]; 205 while (*k) { 206 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) { 207 k++; 208 continue; 209 } 210 strncpy(kernel, *k, PATH_MAX); 211 return kernel; 212 } 213 214 if (uname(&uts) < 0) 215 return NULL; 216 217 k = &host_kernels[0]; 218 while (*k) { 219 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 220 return NULL; 221 222 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) { 223 k++; 224 continue; 225 } 226 return kernel; 227 228 } 229 return NULL; 230 } 231 232 static int root_device(char *dev, long *part) 233 { 234 FILE *fp; 235 char *line; 236 int tmp; 237 size_t nr_read; 238 char device[PATH_MAX]; 239 char mnt_pt[PATH_MAX]; 240 char resolved_path[PATH_MAX]; 241 char *p; 242 struct stat st; 243 244 fp = fopen("/proc/mounts", "r"); 245 if (!fp) 246 return -1; 247 248 line = NULL; 249 tmp = 0; 250 while (!feof(fp)) { 251 if (getline(&line, &nr_read, fp) < 0) 252 break; 253 sscanf(line, "%s %s", device, mnt_pt); 254 if (!strncmp(device, "/dev", 4) && !strcmp(mnt_pt, "/")) { 255 tmp = 1; 256 break; 257 } 258 } 259 fclose(fp); 260 free(line); 261 262 if (!tmp) 263 return -1; 264 265 /* get the absolute path */ 266 if (!realpath(device, resolved_path)) 267 return -1; 268 269 /* find the partition number */ 270 p = resolved_path; 271 while (*p) { 272 if (isdigit(*p)) { 273 strncpy(dev, resolved_path, p - resolved_path); 274 *part = atol(p); 275 break; 276 } 277 p++; 278 } 279 280 /* verify the device path */ 281 if (stat(dev, &st) < 0) 282 return -1; 283 284 if (access(dev, R_OK) < 0) 285 return -1; 286 287 return 0; 288 } 289 290 static char *host_image(char *cmd_line, size_t size) 291 { 292 char *t; 293 char device[PATH_MAX]; 294 long part = 0; 295 296 t = malloc(PATH_MAX); 297 if (!t) 298 return NULL; 299 300 /* check for the root file system */ 301 if (root_device(device, &part) < 0) { 302 free(t); 303 return NULL; 304 } 305 strncpy(t, device, PATH_MAX); 306 if (!strstr(cmd_line, "root=")) { 307 char tmp[PATH_MAX]; 308 snprintf(tmp, sizeof(tmp), "root=/dev/vda%ld rw ", part); 309 strlcat(cmd_line, tmp, size); 310 } 311 return t; 312 } 313 314 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 315 { 316 static char real_cmdline[2048]; 317 int exit_code = 0; 318 int i; 319 struct virtio_net_parameters net_params; 320 char *hi; 321 unsigned int nr_online_cpus; 322 323 signal(SIGALRM, handle_sigalrm); 324 signal(SIGQUIT, handle_sigquit); 325 326 while (argc != 0) { 327 argc = parse_options(argc, argv, options, run_usage, 328 PARSE_OPT_STOP_AT_NON_OPTION); 329 if (argc != 0) { 330 if (kernel_filename) { 331 fprintf(stderr, "Cannot handle parameter: " 332 "%s\n", argv[0]); 333 usage_with_options(run_usage, options); 334 return EINVAL; 335 } 336 /* first unhandled parameter is treated as a kernel 337 image 338 */ 339 kernel_filename = argv[0]; 340 argv++; 341 argc--; 342 } 343 344 } 345 346 if (!kernel_filename) 347 kernel_filename = find_kernel(); 348 349 if (!kernel_filename) { 350 kernel_usage_with_options(); 351 return EINVAL; 352 } 353 354 if (nrcpus < 1 || nrcpus > KVM_NR_CPUS) 355 die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS); 356 357 /* FIXME: Remove as only SMP gets fully supported */ 358 if (nrcpus > 1) { 359 warning("Limiting CPUs to 1, true SMP is not yet implemented"); 360 nrcpus = 1; 361 } 362 363 if (ram_size < MIN_RAM_SIZE_MB) 364 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB); 365 366 ram_size <<= MB_SHIFT; 367 368 if (!kvm_dev) 369 kvm_dev = DEFAULT_KVM_DEV; 370 371 if (!console) 372 console = DEFAULT_CONSOLE; 373 374 if (!strncmp(console, "virtio", 6)) 375 active_console = CONSOLE_VIRTIO; 376 else 377 active_console = CONSOLE_8250; 378 379 if (!host_ip_addr) 380 host_ip_addr = DEFAULT_HOST_ADDR; 381 382 if (!guest_mac) 383 guest_mac = DEFAULT_GUEST_MAC; 384 385 if (!script) 386 script = DEFAULT_SCRIPT; 387 388 term_init(); 389 390 kvm = kvm__init(kvm_dev, ram_size); 391 392 kvm->nrcpus = nrcpus; 393 394 memset(real_cmdline, 0, sizeof(real_cmdline)); 395 strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); 396 if (kernel_cmdline) 397 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); 398 399 hi = NULL; 400 if (!image_filename) { 401 hi = host_image(real_cmdline, sizeof(real_cmdline)); 402 if (hi) { 403 image_filename = hi; 404 readonly_image = true; 405 } 406 } 407 408 if (!strstr(real_cmdline, "root=")) 409 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline)); 410 411 if (image_filename) { 412 kvm->disk_image = disk_image__open(image_filename, readonly_image); 413 if (!kvm->disk_image) 414 die("unable to load disk image %s", image_filename); 415 } 416 free(hi); 417 418 if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, 419 real_cmdline)) 420 die("unable to load kernel %s", kernel_filename); 421 422 ioport__setup_legacy(); 423 424 rtc__init(); 425 426 kvm__setup_bios(kvm); 427 428 serial8250__init(kvm); 429 430 pci__init(); 431 432 virtio_blk__init(kvm); 433 434 virtio_console__init(kvm); 435 436 if (virtio_rng) 437 virtio_rng__init(kvm); 438 439 if (!network) 440 network = DEFAULT_NETWORK; 441 442 if (!strncmp(network, "virtio", 6)) { 443 net_params = (struct virtio_net_parameters) { 444 .host_ip = host_ip_addr, 445 .self = kvm, 446 .script = script 447 }; 448 sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 449 net_params.guest_mac, 450 net_params.guest_mac+1, 451 net_params.guest_mac+2, 452 net_params.guest_mac+3, 453 net_params.guest_mac+4, 454 net_params.guest_mac+5); 455 456 virtio_net__init(&net_params); 457 } 458 459 kvm__start_timer(kvm); 460 461 for (i = 0; i < nrcpus; i++) { 462 kvm_cpus[i] = kvm_cpu__init(kvm, i); 463 if (!kvm_cpus[i]) 464 die("unable to initialize KVM VCPU"); 465 466 if (single_step) 467 kvm_cpu__enable_singlestep(kvm_cpus[i]); 468 } 469 470 kvm__init_ram(kvm); 471 472 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN); 473 thread_pool__init(nr_online_cpus); 474 475 for (i = 0; i < nrcpus; i++) { 476 if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0) 477 die("unable to create KVM VCPU thread"); 478 } 479 480 for (i = 0; i < nrcpus; i++) { 481 void *ret; 482 483 if (pthread_join(kvm_cpus[i]->thread, &ret) != 0) 484 die("pthread_join"); 485 486 if (ret != NULL) 487 exit_code = 1; 488 } 489 490 disk_image__close(kvm->disk_image); 491 kvm__delete(kvm); 492 493 if (!exit_code) 494 printf("\n # KVM session ended normally.\n"); 495 496 return exit_code; 497 } 498