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 "none" 34 #define DEFAULT_HOST_ADDR "192.168.33.2" 35 36 #define MB_SHIFT (20) 37 #define MIN_RAM_SIZE_MB (64ULL) 38 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) 39 40 #define KVM_NR_CPUS (255) 41 42 static struct kvm *kvm; 43 static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS]; 44 static __thread struct kvm_cpu *current_kvm_cpu; 45 46 static void handle_sigint(int sig) 47 { 48 exit(1); 49 } 50 51 static void handle_sigquit(int sig) 52 { 53 serial8250__inject_sysrq(kvm); 54 } 55 56 static void handle_sigalrm(int sig) 57 { 58 serial8250__inject_interrupt(kvm); 59 virtio_console__inject_interrupt(kvm); 60 } 61 62 static u64 ram_size = MIN_RAM_SIZE_MB; 63 static const char *kernel_cmdline; 64 static const char *kernel_filename; 65 static const char *initrd_filename; 66 static const char *image_filename; 67 static const char *console; 68 static const char *kvm_dev; 69 static const char *network; 70 static const char *host_ip_addr; 71 static bool single_step; 72 static bool readonly_image; 73 extern bool ioport_debug; 74 extern int active_console; 75 76 static int nrcpus = 1; 77 78 static const char * const run_usage[] = { 79 "kvm run [<options>] [<kernel image>]", 80 NULL 81 }; 82 83 static const struct option options[] = { 84 OPT_GROUP("Basic options:"), 85 OPT_INTEGER('\0', "cpus", &nrcpus, "Number of CPUs"), 86 OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), 87 OPT_STRING('i', "image", &image_filename, "image", "Disk image"), 88 OPT_BOOLEAN('\0', "readonly", &readonly_image, 89 "Don't write changes back to disk image"), 90 OPT_STRING('c', "console", &console, "serial or virtio", 91 "Console to use"), 92 93 OPT_GROUP("Kernel options:"), 94 OPT_STRING('k', "kernel", &kernel_filename, "kernel", 95 "Kernel to boot in virtual machine"), 96 OPT_STRING('r', "initrd", &initrd_filename, "initrd", 97 "Initial RAM disk image"), 98 OPT_STRING('p', "params", &kernel_cmdline, "params", 99 "Kernel command line arguments"), 100 101 OPT_GROUP("Networking options:"), 102 OPT_STRING('n', "network", &network, "virtio", 103 "Network to use"), 104 OPT_STRING('\0', "host-ip-addr", &host_ip_addr, "a.b.c.d", 105 "Assign this address to the host side networking"), 106 107 OPT_GROUP("Debug options:"), 108 OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"), 109 OPT_BOOLEAN('s', "single-step", &single_step, 110 "Enable single stepping"), 111 OPT_BOOLEAN('g', "ioport-debug", &ioport_debug, 112 "Enable ioport debugging"), 113 OPT_END() 114 }; 115 116 static void *kvm_cpu_thread(void *arg) 117 { 118 current_kvm_cpu = arg; 119 120 if (kvm_cpu__start(current_kvm_cpu)) 121 goto panic_kvm; 122 123 kvm_cpu__delete(current_kvm_cpu); 124 125 return (void *) (intptr_t) 0; 126 127 panic_kvm: 128 fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n", 129 current_kvm_cpu->kvm_run->exit_reason, 130 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 131 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 132 fprintf(stderr, "KVM exit code: 0x%Lu\n", 133 current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 134 disk_image__close(kvm->disk_image); 135 kvm_cpu__show_registers(current_kvm_cpu); 136 kvm_cpu__show_code(current_kvm_cpu); 137 kvm_cpu__show_page_tables(current_kvm_cpu); 138 139 kvm_cpu__delete(current_kvm_cpu); 140 141 return (void *) (intptr_t) 1; 142 } 143 144 static char host_kernel[PATH_MAX]; 145 146 static const char *find_host_kernel(void) 147 { 148 struct utsname uts; 149 struct stat st; 150 151 if (uname(&uts) < 0) 152 return NULL; 153 154 if (snprintf(host_kernel, PATH_MAX, "/boot/vmlinuz-%s", uts.release) < 0) 155 return NULL; 156 157 if (stat(host_kernel, &st) < 0) 158 return NULL; 159 160 if (!S_ISREG(st.st_mode)) 161 return NULL; 162 163 return host_kernel; 164 } 165 166 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 167 { 168 static char real_cmdline[2048]; 169 int exit_code = 0; 170 int i; 171 172 signal(SIGALRM, handle_sigalrm); 173 signal(SIGQUIT, handle_sigquit); 174 signal(SIGINT, handle_sigint); 175 176 while (argc != 0) { 177 argc = parse_options(argc, argv, options, run_usage, 178 PARSE_OPT_STOP_AT_NON_OPTION); 179 if (argc != 0) { 180 if (kernel_filename) { 181 fprintf(stderr, "Cannot handle parameter: " 182 "%s\n", argv[0]); 183 usage_with_options(run_usage, options); 184 return EINVAL; 185 } 186 /* first unhandled parameter is treated as a kernel 187 image 188 */ 189 kernel_filename = argv[0]; 190 argv++; 191 argc--; 192 } 193 194 } 195 196 if (!kernel_filename) 197 kernel_filename = find_host_kernel(); 198 199 if (!kernel_filename) { 200 usage_with_options(run_usage, options); 201 return EINVAL; 202 } 203 204 if (nrcpus < 1 || nrcpus > KVM_NR_CPUS) 205 die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS); 206 207 /* FIXME: Remove as only SMP gets fully supported */ 208 if (nrcpus > 1) 209 warning("Limiting CPUs to 1, true SMP is not yet implemented"); 210 211 if (ram_size < MIN_RAM_SIZE_MB) 212 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB); 213 214 ram_size <<= MB_SHIFT; 215 216 if (!kvm_dev) 217 kvm_dev = DEFAULT_KVM_DEV; 218 219 if (!console) 220 console = DEFAULT_CONSOLE; 221 222 if (!strncmp(console, "virtio", 6)) 223 active_console = CONSOLE_VIRTIO; 224 else 225 active_console = CONSOLE_8250; 226 227 if (!host_ip_addr) 228 host_ip_addr = DEFAULT_HOST_ADDR; 229 230 term_init(); 231 232 kvm = kvm__init(kvm_dev, ram_size); 233 234 if (image_filename) { 235 kvm->disk_image = disk_image__open(image_filename, readonly_image); 236 if (!kvm->disk_image) 237 die("unable to load disk image %s", image_filename); 238 } 239 240 strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); 241 if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) { 242 strlcat(real_cmdline, "root=/dev/vda rw ", 243 sizeof(real_cmdline)); 244 } 245 246 if (kernel_cmdline) { 247 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); 248 real_cmdline[sizeof(real_cmdline)-1] = '\0'; 249 } 250 251 if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, 252 real_cmdline)) 253 die("unable to load kernel %s", kernel_filename); 254 255 ioport__setup_legacy(); 256 257 kvm__setup_bios(kvm); 258 259 serial8250__init(kvm); 260 261 pci__init(); 262 263 virtio_blk__init(kvm); 264 265 virtio_console__init(kvm); 266 267 if (!network) 268 network = DEFAULT_NETWORK; 269 270 if (!strncmp(network, "virtio", 6)) 271 virtio_net__init(kvm, host_ip_addr); 272 273 kvm__start_timer(kvm); 274 275 for (i = 0; i < nrcpus; i++) { 276 kvm_cpus[i] = kvm_cpu__init(kvm, i); 277 if (!kvm_cpus[i]) 278 die("unable to initialize KVM VCPU"); 279 280 if (single_step) 281 kvm_cpu__enable_singlestep(kvm_cpus[i]); 282 283 if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0) 284 die("unable to create KVM VCPU thread"); 285 } 286 287 for (i = 0; i < nrcpus; i++) { 288 void *ret; 289 290 if (pthread_join(kvm_cpus[i]->thread, &ret) != 0) 291 die("pthread_join"); 292 293 if (ret != NULL) 294 exit_code = 1; 295 } 296 297 disk_image__close(kvm->disk_image); 298 kvm__delete(kvm); 299 300 if (!exit_code) 301 printf("\n # KVM session ended normally.\n"); 302 303 return exit_code; 304 } 305