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 10 /* user defined header files */ 11 #include <linux/types.h> 12 #include <kvm/kvm.h> 13 #include <kvm/kvm-cpu.h> 14 #include <kvm/8250-serial.h> 15 #include <kvm/virtio-blk.h> 16 #include <kvm/virtio-console.h> 17 #include <kvm/disk-image.h> 18 #include <kvm/util.h> 19 #include <kvm/pci.h> 20 #include <kvm/term.h> 21 22 /* header files for gitish interface */ 23 #include <kvm/kvm-run.h> 24 #include <kvm/parse-options.h> 25 26 #define DEFAULT_KVM_DEV "/dev/kvm" 27 28 #define MB_SHIFT (20) 29 #define MIN_RAM_SIZE_MB (64ULL) 30 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT) 31 32 #define KVM_NR_CPUS (255) 33 34 static struct kvm *kvm; 35 static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS]; 36 static __thread struct kvm_cpu *current_kvm_cpu; 37 38 static void handle_sigint(int sig) 39 { 40 exit(1); 41 } 42 43 static void handle_sigquit(int sig) 44 { 45 serial8250__inject_sysrq(kvm); 46 } 47 48 static void handle_sigalrm(int sig) 49 { 50 serial8250__inject_interrupt(kvm); 51 virtio_console__inject_interrupt(kvm); 52 } 53 54 static u64 ram_size = MIN_RAM_SIZE_MB; 55 static const char *kernel_cmdline; 56 static const char *kernel_filename; 57 static const char *initrd_filename; 58 static const char *image_filename; 59 static const char *kvm_dev; 60 static bool single_step; 61 static bool virtio_console; 62 extern bool ioport_debug; 63 extern int active_console; 64 65 static int nrcpus = 1; 66 67 static const char * const run_usage[] = { 68 "kvm run [<options>] <kernel image>", 69 NULL 70 }; 71 72 static const struct option options[] = { 73 OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), 74 OPT_STRING('p', "params", &kernel_cmdline, "params", 75 "Kernel command line arguments"), 76 OPT_STRING('r', "initrd", &initrd_filename, "initrd", 77 "Initial RAM disk image"), 78 OPT_STRING('k', "kernel", &kernel_filename, "kernel", 79 "Kernel to boot in virtual machine"), 80 OPT_STRING('i', "image", &image_filename, "image", "Disk image"), 81 OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"), 82 OPT_BOOLEAN('s', "single-step", &single_step, 83 "Enable single stepping"), 84 OPT_BOOLEAN('g', "ioport-debug", &ioport_debug, 85 "Enable ioport debugging"), 86 OPT_BOOLEAN('c', "enable-virtio-console", &virtio_console, 87 "Enable the virtual IO console"), 88 OPT_INTEGER('\0', "cpus", &nrcpus, "Number of CPUs"), 89 OPT_END() 90 }; 91 92 static void *kvm_cpu_thread(void *arg) 93 { 94 current_kvm_cpu = arg; 95 96 if (kvm_cpu__start(current_kvm_cpu)) 97 goto panic_kvm; 98 99 kvm_cpu__delete(current_kvm_cpu); 100 101 return (void *) (intptr_t) 0; 102 103 panic_kvm: 104 fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n", 105 current_kvm_cpu->kvm_run->exit_reason, 106 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]); 107 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 108 fprintf(stderr, "KVM exit code: 0x%Lu\n", 109 current_kvm_cpu->kvm_run->hw.hardware_exit_reason); 110 disk_image__close(kvm->disk_image); 111 kvm_cpu__show_registers(current_kvm_cpu); 112 kvm_cpu__show_code(current_kvm_cpu); 113 kvm_cpu__show_page_tables(current_kvm_cpu); 114 115 kvm_cpu__delete(current_kvm_cpu); 116 117 return (void *) (intptr_t) 1; 118 } 119 120 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 121 { 122 static char real_cmdline[2048]; 123 int exit_code = 0; 124 int i; 125 126 signal(SIGALRM, handle_sigalrm); 127 signal(SIGQUIT, handle_sigquit); 128 signal(SIGINT, handle_sigint); 129 130 if (!argv || !*argv) { 131 /* no argument specified */ 132 usage_with_options(run_usage, options); 133 return EINVAL; 134 } 135 136 while (argc != 0) { 137 argc = parse_options(argc, argv, options, run_usage, 138 PARSE_OPT_STOP_AT_NON_OPTION); 139 if (argc != 0) { 140 if (kernel_filename) { 141 fprintf(stderr, "Cannot handle parameter: " 142 "%s\n", argv[0]); 143 usage_with_options(run_usage, options); 144 return EINVAL; 145 } 146 /* first unhandled parameter is treated as a kernel 147 image 148 */ 149 kernel_filename = argv[0]; 150 argv++; 151 argc--; 152 } 153 154 } 155 156 if (nrcpus < 1 || nrcpus > KVM_NR_CPUS) 157 die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS); 158 159 /* FIXME: Remove as only SMP gets fully supported */ 160 if (nrcpus > 1) 161 warning("Limiting CPUs to 1, true SMP is not yet implemented"); 162 163 if (ram_size < MIN_RAM_SIZE_MB) 164 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB); 165 166 ram_size <<= MB_SHIFT; 167 168 if (!kvm_dev) 169 kvm_dev = DEFAULT_KVM_DEV; 170 171 if (virtio_console == true) 172 active_console = CONSOLE_VIRTIO; 173 174 term_init(); 175 176 kvm = kvm__init(kvm_dev, ram_size); 177 178 if (image_filename) { 179 kvm->disk_image = disk_image__open(image_filename); 180 if (!kvm->disk_image) 181 die("unable to load disk image %s", image_filename); 182 } 183 184 strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); 185 if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) { 186 strlcat(real_cmdline, "root=/dev/vda rw ", 187 sizeof(real_cmdline)); 188 } 189 190 if (kernel_cmdline) { 191 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); 192 real_cmdline[sizeof(real_cmdline)-1] = '\0'; 193 } 194 195 if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, 196 real_cmdline)) 197 die("unable to load kernel %s", kernel_filename); 198 199 kvm__setup_bios(kvm); 200 201 serial8250__init(kvm); 202 203 pci__init(); 204 205 virtio_blk__init(kvm); 206 207 virtio_console__init(kvm); 208 209 kvm__start_timer(kvm); 210 211 for (i = 0; i < nrcpus; i++) { 212 kvm_cpus[i] = kvm_cpu__init(kvm, i); 213 if (!kvm_cpus[i]) 214 die("unable to initialize KVM VCPU"); 215 216 if (single_step) 217 kvm_cpu__enable_singlestep(kvm_cpus[i]); 218 219 if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0) 220 die("unable to create KVM VCPU thread"); 221 } 222 223 for (i = 0; i < nrcpus; i++) { 224 void *ret; 225 226 if (pthread_join(kvm_cpus[i]->thread, &ret) != 0) 227 die("pthread_join"); 228 229 if (ret != NULL) 230 exit_code = 1; 231 } 232 233 disk_image__close(kvm->disk_image); 234 kvm__delete(kvm); 235 236 if (!exit_code) 237 printf("\n # KVM session ended normally.\n"); 238 239 return exit_code; 240 } 241