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