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