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 static struct kvm *kvm; 33 static struct kvm_cpu *cpu; 34 35 static void handle_sigint(int sig) 36 { 37 exit(1); 38 } 39 40 static void handle_sigquit(int sig) 41 { 42 kvm_cpu__show_registers(cpu); 43 kvm_cpu__show_code(cpu); 44 kvm_cpu__show_page_tables(cpu); 45 46 kvm_cpu__delete(cpu); 47 kvm__delete(kvm); 48 49 exit(1); 50 } 51 52 static u64 ram_size = MIN_RAM_SIZE_MB; 53 static const char *kernel_cmdline; 54 static const char *kernel_filename; 55 static const char *initrd_filename; 56 static const char *image_filename; 57 static const char *kvm_dev; 58 static bool single_step; 59 static bool virtio_console; 60 extern bool ioport_debug; 61 extern int active_console; 62 63 static const char * const run_usage[] = { 64 "kvm run [<options>] <kernel image>", 65 NULL 66 }; 67 68 static const struct option options[] = { 69 OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), 70 OPT_STRING('p', "params", &kernel_cmdline, "params", 71 "Kernel command line arguments"), 72 OPT_STRING('r', "initrd", &initrd_filename, "initrd", 73 "Initial RAM disk image"), 74 OPT_STRING('k', "kernel", &kernel_filename, "kernel", 75 "Kernel to boot in virtual machine"), 76 OPT_STRING('i', "image", &image_filename, "image", "Disk image"), 77 OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"), 78 OPT_BOOLEAN('s', "single-step", &single_step, 79 "Enable single stepping"), 80 OPT_BOOLEAN('g', "ioport-debug", &ioport_debug, 81 "Enable ioport debugging"), 82 OPT_BOOLEAN('c', "enable-virtio-console", &virtio_console, 83 "Enable the virtual IO console"), 84 OPT_END() 85 }; 86 87 int kvm_cmd_run(int argc, const char **argv, const char *prefix) 88 { 89 static char real_cmdline[2048]; 90 91 signal(SIGQUIT, handle_sigquit); 92 signal(SIGINT, handle_sigint); 93 94 if (!argv || !*argv) { 95 /* no argument specified */ 96 usage_with_options(run_usage, options); 97 return EINVAL; 98 } 99 100 while (argc != 0) { 101 argc = parse_options(argc, argv, options, run_usage, 102 PARSE_OPT_STOP_AT_NON_OPTION); 103 if (argc != 0) { 104 if (kernel_filename) { 105 fprintf(stderr, "Cannot handle parameter: " 106 "%s\n", argv[0]); 107 usage_with_options(run_usage, options); 108 return EINVAL; 109 } 110 /* first unhandled parameter is treated as a kernel 111 image 112 */ 113 kernel_filename = argv[0]; 114 argv++; 115 argc--; 116 } 117 118 } 119 120 if (ram_size < MIN_RAM_SIZE_MB) 121 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, 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 cpu = kvm_cpu__init(kvm); 136 if (!cpu) 137 die("unable to initialize KVM VCPU"); 138 139 if (image_filename) { 140 kvm->disk_image = disk_image__open(image_filename); 141 if (!kvm->disk_image) 142 die("unable to load disk image %s", image_filename); 143 } 144 145 strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 "); 146 if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) { 147 strlcat(real_cmdline, "root=/dev/vda rw ", 148 sizeof(real_cmdline)); 149 } 150 151 if (kernel_cmdline) { 152 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline)); 153 real_cmdline[sizeof(real_cmdline)-1] = '\0'; 154 } 155 156 if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename, 157 real_cmdline)) 158 die("unable to load kernel %s", kernel_filename); 159 160 kvm__setup_bios(kvm); 161 162 serial8250__init(kvm); 163 164 pci__init(); 165 166 virtio_blk__init(kvm); 167 168 virtio_console__init(kvm); 169 170 kvm__start_timer(kvm); 171 172 if (single_step) 173 kvm_cpu__enable_singlestep(cpu); 174 175 if (kvm_cpu__start(cpu)) 176 goto panic_kvm; 177 178 disk_image__close(kvm->disk_image); 179 kvm__delete(kvm); 180 181 printf("\n # KVM session ended normally.\n"); 182 183 return 0; 184 185 panic_kvm: 186 fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n", 187 cpu->kvm_run->exit_reason, 188 kvm_exit_reasons[cpu->kvm_run->exit_reason]); 189 if (cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) 190 fprintf(stderr, "KVM exit code: 0x%Lu\n", 191 cpu->kvm_run->hw.hardware_exit_reason); 192 disk_image__close(kvm->disk_image); 193 kvm_cpu__show_registers(cpu); 194 kvm_cpu__show_code(cpu); 195 kvm_cpu__show_page_tables(cpu); 196 kvm_cpu__delete(cpu); 197 kvm__delete(kvm); 198 199 return 1; 200 } 201