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