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 13 /* user defined header files */ 14 #include <linux/types.h> 15 #include <kvm/kvm.h> 16 #include <kvm/kvm-cpu.h> 17 #include <kvm/8250-serial.h> 18 #include <kvm/virtio-blk.h> 19 #include <kvm/virtio-net.h> 20 #include <kvm/virtio-console.h> 21 #include <kvm/disk-image.h> 22 #include <kvm/util.h> 23 #include <kvm/pci.h> 24 #include <kvm/term.h> 25 #include <kvm/ioport.h> 26 27 /* header files for gitish interface */ 28 #include <kvm/kvm-run.h> 29 #include <kvm/parse-options.h> 30 31 #define DEFAULT_KVM_DEV "/dev/kvm" 32 #define DEFAULT_CONSOLE "serial" 33 #define DEFAULT_NETWORK "virtio" 34 #define DEFAULT_HOST_ADDR "192.168.33.2" 35 #define DEFAULT_GUEST_MAC "00:11:22:33:44:55" 36 37 #define MB_SHIFT (20) 38 #define MIN_RAM_SIZE_MB (64ULL) 39 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) 40 41 #define KVM_NR_CPUS (255) 42 43 static struct kvm *kvm; 44 static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS]; 45 static __thread struct kvm_cpu *current_kvm_cpu; 46 47 static void handle_sigquit(int sig) 48 { 49 serial8250__inject_sysrq(kvm); 50 } 51 52 static void handle_sigalrm(int sig) 53 { 54 serial8250__inject_interrupt(kvm); 55 virtio_console__inject_interrupt(kvm); 56 } 57 58 static u64 ram_size = MIN_RAM_SIZE_MB; 59 static const char *kernel_cmdline; 60 static const char *kernel_filename; 61 static const char *initrd_filename; 62 static const char *image_filename; 63 static const char *console; 64 static const char *kvm_dev; 65 static const char *network; 66 static const char *host_ip_addr; 67 static const char *guest_mac; 68 static bool single_step; 69 static bool readonly_image; 70 extern bool ioport_debug; 71 extern int active_console; 72 73 static int nrcpus = 1; 74 75 static const char * const run_usage[] = { 76 "kvm run [<options>] [<kernel image>]", 77 NULL 78 }; 79 80 static const struct option options[] = { 81 OPT_GROUP("Basic options:"), 82 OPT_INTEGER('\0', "cpus", &nrcpus, "Number of CPUs"), 83 OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), 84 OPT_STRING('i', "image", &image_filename, "image", "Disk image"), 85 OPT_BOOLEAN('\0', "readonly", &readonly_image, 86 "Don't write changes back to disk image"), 87 OPT_STRING('c', "console", &console, "serial or virtio", 88 "Console to use"), 89 90 OPT_GROUP("Kernel options:"), 91 OPT_STRING('k', "kernel", &kernel_filename, "kernel", 92 "Kernel to boot in virtual machine"), 93 OPT_STRING('r', "initrd", &initrd_filename, "initrd", 94 "Initial RAM disk image"), 95 OPT_STRING('p', "params", &kernel_cmdline, "params", 96 "Kernel command line arguments"), 97 98 OPT_GROUP("Networking options:"), 99 OPT_STRING('n', "network", &network, "virtio", 100 "Network to use"), 101 OPT_STRING('\0', "host-ip-addr", &host_ip_addr, "a.b.c.d", 102 "Assign this address to the host side networking"), 103 OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff", 104 "Assign this address to the guest side NIC"), 105 OPT_GROUP("Debug options:"), 106 OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"), 107 OPT_BOOLEAN('s', "single-step", &single_step, 108 "Enable single stepping"), 109 OPT_BOOLEAN('g', "ioport-debug", &ioport_debug, 110 "Enable ioport debugging"), 111 OPT_END() 112 }; 113 114 static void *kvm_cpu_thread(void *arg) 115 { 116 current_kvm_cpu = arg; 117 118 if (kvm_cpu__start(current_kvm_cpu)) 119 goto panic_kvm; 120 121 kvm_cpu__delete(current_kvm_cpu); 122 123 return (void *) (intptr_t) 0; 124 125 panic_kvm: 126 fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n", 127 current_kvm_cpu->kvm_run->exit_reason, 128 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 129 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 130 fprintf(stderr, "KVM exit code: 0x%Lu\n", 131 current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 132 disk_image__close(kvm->disk_image); 133 kvm_cpu__show_registers(current_kvm_cpu); 134 kvm_cpu__show_code(current_kvm_cpu); 135 kvm_cpu__show_page_tables(current_kvm_cpu); 136 137 kvm_cpu__delete(current_kvm_cpu); 138 139 return (void *) (intptr_t) 1; 140 } 141 142 static char kernel[PATH_MAX]; 143 const char *host_kernels[] = { 144 "/boot/vmlinuz", 145 "/boot/bzImage", 146 NULL 147 }; 148 const char *defult_kernels[] = { 149 "./bzImage", 150 "../../arch/x86/boot/bzImage", 151 NULL 152 }; 153 154 static void kernel_usage_with_options(void) 155 { 156 const char **k; 157 struct utsname uts; 158 159 fprintf(stderr, "Fatal: could not find default kernel image in:\n"); 160 k = &defult_kernels[0]; 161 while (*k) { 162 fprintf(stderr, "\t%s\n", *k); 163 k++; 164 } 165 166 if (uname(&uts) < 0) 167 return; 168 169 k = &host_kernels[0]; 170 while (*k) { 171 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 172 return; 173 fprintf(stderr, "\t%s\n", kernel); 174 k++; 175 } 176 usage_with_options(run_usage, options); 177 } 178 179 static const char *find_kernel(void) 180 { 181 const char **k; 182 struct stat st; 183 struct utsname uts; 184 185 k = &defult_kernels[0]; 186 while (*k) { 187 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) { 188 k++; 189 continue; 190 } 191 strncpy(kernel, *k, PATH_MAX); 192 return kernel; 193 } 194 195 if (uname(&uts) < 0) 196 return NULL; 197 198 k = &host_kernels[0]; 199 while (*k) { 200 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0) 201 return NULL; 202 203 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) { 204 k++; 205 continue; 206 } 207 return kernel; 208 209 } 210 return NULL; 211 } 212 213 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 214 { 215 static char real_cmdline[2048]; 216 int exit_code = 0; 217 int i; 218 struct virtio_net_parameters net_params; 219 220 signal(SIGALRM, handle_sigalrm); 221 signal(SIGQUIT, handle_sigquit); 222 223 while (argc != 0) { 224 argc = parse_options(argc, argv, options, run_usage, 225 PARSE_OPT_STOP_AT_NON_OPTION); 226 if (argc != 0) { 227 if (kernel_filename) { 228 fprintf(stderr, "Cannot handle parameter: " 229 "%s\n", argv[0]); 230 usage_with_options(run_usage, options); 231 return EINVAL; 232 } 233 /* first unhandled parameter is treated as a kernel 234 image 235 */ 236 kernel_filename = argv[0]; 237 argv++; 238 argc--; 239 } 240 241 } 242 243 if (!kernel_filename) 244 kernel_filename = find_kernel(); 245 246 if (!kernel_filename) { 247 kernel_usage_with_options(); 248 return EINVAL; 249 } 250 251 if (nrcpus < 1 || nrcpus > KVM_NR_CPUS) 252 die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS); 253 254 /* FIXME: Remove as only SMP gets fully supported */ 255 if (nrcpus > 1) 256 warning("Limiting CPUs to 1, true SMP is not yet implemented"); 257 258 if (ram_size < MIN_RAM_SIZE_MB) 259 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB); 260 261 ram_size <<= MB_SHIFT; 262 263 if (!kvm_dev) 264 kvm_dev = DEFAULT_KVM_DEV; 265 266 if (!console) 267 console = DEFAULT_CONSOLE; 268 269 if (!strncmp(console, "virtio", 6)) 270 active_console = CONSOLE_VIRTIO; 271 else 272 active_console = CONSOLE_8250; 273 274 if (!host_ip_addr) 275 host_ip_addr = DEFAULT_HOST_ADDR; 276 277 if (!guest_mac) 278 guest_mac = DEFAULT_GUEST_MAC; 279 280 term_init(); 281 282 kvm = kvm__init(kvm_dev, ram_size); 283 284 if (image_filename) { 285 kvm->disk_image = disk_image__open(image_filename, readonly_image); 286 if (!kvm->disk_image) 287 die("unable to load disk image %s", image_filename); 288 } 289 290 strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); 291 if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) { 292 strlcat(real_cmdline, "root=/dev/vda rw ", 293 sizeof(real_cmdline)); 294 } 295 296 if (kernel_cmdline) { 297 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); 298 real_cmdline[sizeof(real_cmdline)-1] = '\0'; 299 } 300 301 if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, 302 real_cmdline)) 303 die("unable to load kernel %s", kernel_filename); 304 305 ioport__setup_legacy(); 306 307 kvm__setup_bios(kvm); 308 309 serial8250__init(kvm); 310 311 pci__init(); 312 313 virtio_blk__init(kvm); 314 315 virtio_console__init(kvm); 316 317 if (!network) 318 network = DEFAULT_NETWORK; 319 320 if (!strncmp(network, "virtio", 6)) { 321 net_params = (struct virtio_net_parameters) { 322 .host_ip = host_ip_addr, 323 .self = kvm 324 }; 325 sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 326 net_params.guest_mac, 327 net_params.guest_mac+1, 328 net_params.guest_mac+2, 329 net_params.guest_mac+3, 330 net_params.guest_mac+4, 331 net_params.guest_mac+5); 332 333 virtio_net__init(&net_params); 334 } 335 336 kvm__start_timer(kvm); 337 338 for (i = 0; i < nrcpus; i++) { 339 kvm_cpus[i] = kvm_cpu__init(kvm, i); 340 if (!kvm_cpus[i]) 341 die("unable to initialize KVM VCPU"); 342 343 if (single_step) 344 kvm_cpu__enable_singlestep(kvm_cpus[i]); 345 346 if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0) 347 die("unable to create KVM VCPU thread"); 348 } 349 350 for (i = 0; i < nrcpus; i++) { 351 void *ret; 352 353 if (pthread_join(kvm_cpus[i]->thread, &ret) != 0) 354 die("pthread_join"); 355 356 if (ret != NULL) 357 exit_code = 1; 358 } 359 360 disk_image__close(kvm->disk_image); 361 kvm__delete(kvm); 362 363 if (!exit_code) 364 printf("\n # KVM session ended normally.\n"); 365 366 return exit_code; 367 } 368