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 = MIN_RAM_SIZE_MB; 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; 56 static bool virtio_console; 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 < MIN_RAM_SIZE_MB) { 118 die("Not enough memory specified: %luMB (min %luMB)", ram_size, 119 MIN_RAM_SIZE_MB); 120 } 121 ram_size <<= MB_SHIFT; 122 123 if (!kvm_dev) 124 kvm_dev = DEFAULT_KVM_DEV; 125 126 if (virtio_console == true) 127 active_console = CONSOLE_VIRTIO; 128 129 term_init(); 130 131 kvm = kvm__init(kvm_dev, ram_size); 132 133 if (image_filename) { 134 kvm->disk_image = disk_image__open(image_filename); 135 if (!kvm->disk_image) 136 die("unable to load disk image %s", image_filename); 137 } 138 139 kvm__setup_cpuid(kvm); 140 141 strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); 142 if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) { 143 strlcat(real_cmdline, "root=/dev/vda rw ", 144 sizeof(real_cmdline)); 145 } 146 147 if (kernel_cmdline) { 148 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); 149 real_cmdline[sizeof(real_cmdline)-1] = '\0'; 150 } 151 152 if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, 153 real_cmdline)) 154 die("unable to load kernel %s", kernel_filename); 155 156 kvm__reset_vcpu(kvm); 157 158 kvm__setup_bios(kvm); 159 160 if (single_step) 161 kvm__enable_singlestep(kvm); 162 163 serial8250__init(kvm); 164 165 pci__init(); 166 167 virtio_blk__init(kvm); 168 169 virtio_console__init(kvm); 170 171 kvm__start_timer(kvm); 172 173 for (;;) { 174 kvm__run(kvm); 175 176 switch (kvm->kvm_run->exit_reason) { 177 case KVM_EXIT_DEBUG: 178 kvm__show_registers(kvm); 179 kvm__show_code(kvm); 180 break; 181 case KVM_EXIT_IO: { 182 bool ret; 183 184 ret = kvm__emulate_io(kvm, 185 kvm->kvm_run->io.port, 186 (uint8_t *)kvm->kvm_run + 187 kvm->kvm_run->io.data_offset, 188 kvm->kvm_run->io.direction, 189 kvm->kvm_run->io.size, 190 kvm->kvm_run->io.count); 191 192 if (!ret) 193 goto panic_kvm; 194 break; 195 } 196 case KVM_EXIT_MMIO: { 197 bool ret; 198 199 ret = kvm__emulate_mmio(kvm, 200 kvm->kvm_run->mmio.phys_addr, 201 kvm->kvm_run->mmio.data, 202 kvm->kvm_run->mmio.len, 203 kvm->kvm_run->mmio.is_write); 204 205 if (!ret) 206 goto panic_kvm; 207 break; 208 } 209 case KVM_EXIT_INTR: { 210 serial8250__inject_interrupt(kvm); 211 virtio_console__inject_interrupt(kvm); 212 break; 213 } 214 case KVM_EXIT_SHUTDOWN: 215 goto exit_kvm; 216 default: 217 goto panic_kvm; 218 } 219 } 220 exit_kvm: 221 disk_image__close(kvm->disk_image); 222 kvm__delete(kvm); 223 224 printf("\n # KVM session ended normally.\n"); 225 226 return 0; 227 228 panic_kvm: 229 fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n", 230 kvm->kvm_run->exit_reason, 231 kvm_exit_reasons[kvm->kvm_run->exit_reason]); 232 if (kvm->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 233 fprintf(stderr, "KVM exit code: 0x%" PRIu64 "\n", 234 kvm->kvm_run->hw.hardware_exit_reason); 235 disk_image__close(kvm->disk_image); 236 kvm__show_registers(kvm); 237 kvm__show_code(kvm); 238 kvm__show_page_tables(kvm); 239 kvm__delete(kvm); 240 241 return 1; 242 } 243