7 #include <sys/utsname.h>
12 /* user defined header files */
13 #include <linux/types.h>
15 #include <kvm/kvm-cpu.h>
16 #include <kvm/8250-serial.h>
17 #include <kvm/virtio-blk.h>
18 #include <kvm/virtio-net.h>
19 #include <kvm/virtio-console.h>
20 #include <kvm/virtio-rng.h>
21 #include <kvm/disk-image.h>
26 #include <kvm/ioport.h>
27 #include <kvm/threadpool.h>
29 /* header files for gitish interface */
30 #include <kvm/kvm-run.h>
31 #include <kvm/parse-options.h>
33 #define DEFAULT_KVM_DEV "/dev/kvm"
34 #define DEFAULT_CONSOLE "serial"
35 #define DEFAULT_NETWORK "virtio"
36 #define DEFAULT_HOST_ADDR "192.168.33.2"
37 #define DEFAULT_GUEST_MAC "00:11:22:33:44:55"
38 #define DEFAULT_SCRIPT "none"
41 #define MIN_RAM_SIZE_MB (64ULL)
42 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT)
43 #define MAX_DISK_IMAGES 4
45 static struct kvm *kvm;
46 static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
47 static __thread struct kvm_cpu *current_kvm_cpu;
50 static u8 image_count;
51 static const char *kernel_cmdline;
52 static const char *kernel_filename;
53 static const char *initrd_filename;
54 static const char *image_filename[MAX_DISK_IMAGES];
55 static const char *console;
56 static const char *kvm_dev;
57 static const char *network;
58 static const char *host_ip_addr;
59 static const char *guest_mac;
60 static const char *script;
61 static bool single_step;
62 static bool readonly_image[MAX_DISK_IMAGES];
63 static bool virtio_rng;
64 extern bool ioport_debug;
65 extern int active_console;
67 static int nrcpus = 1;
69 static const char * const run_usage[] = {
70 "kvm run [<options>] [<kernel image>]",
74 static int img_name_parser(const struct option *opt, const char *arg, int unset)
78 if (image_count >= MAX_DISK_IMAGES)
79 die("Currently only 4 images are supported");
81 image_filename[image_count] = arg;
82 sep = strstr(arg, ",");
84 if (strcmp(sep + 1, "ro") == 0)
85 readonly_image[image_count] = 1;
94 static const struct option options[] = {
95 OPT_GROUP("Basic options:"),
96 OPT_INTEGER('\0', "cpus", &nrcpus, "Number of CPUs"),
97 OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."),
98 OPT_CALLBACK('i', "image", NULL, "image", "Disk image", img_name_parser),
99 OPT_STRING('c', "console", &console, "serial or virtio",
101 OPT_BOOLEAN('\0', "virtio-rng", &virtio_rng,
102 "Enable virtio Random Number Generator"),
104 OPT_GROUP("Kernel options:"),
105 OPT_STRING('k', "kernel", &kernel_filename, "kernel",
106 "Kernel to boot in virtual machine"),
107 OPT_STRING('r', "initrd", &initrd_filename, "initrd",
108 "Initial RAM disk image"),
109 OPT_STRING('p', "params", &kernel_cmdline, "params",
110 "Kernel command line arguments"),
112 OPT_GROUP("Networking options:"),
113 OPT_STRING('n', "network", &network, "virtio",
115 OPT_STRING('\0', "host-ip-addr", &host_ip_addr, "a.b.c.d",
116 "Assign this address to the host side networking"),
117 OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff",
118 "Assign this address to the guest side NIC"),
119 OPT_STRING('\0', "tapscript", &script, "Script path",
120 "Assign a script to process created tap device"),
121 OPT_GROUP("Debug options:"),
122 OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"),
123 OPT_BOOLEAN('s', "single-step", &single_step,
124 "Enable single stepping"),
125 OPT_BOOLEAN('g', "ioport-debug", &ioport_debug,
126 "Enable ioport debugging"),
130 static void handle_sigusr1(int sig)
132 struct kvm_cpu *cpu = current_kvm_cpu;
137 kvm_cpu__show_registers(cpu);
138 kvm_cpu__show_code(cpu);
139 kvm_cpu__show_page_tables(cpu);
142 static void handle_sigquit(int sig)
146 for (i = 0; i < nrcpus; i++) {
147 struct kvm_cpu *cpu = kvm_cpus[i];
152 pthread_kill(cpu->thread, SIGUSR1);
155 serial8250__inject_sysrq(kvm);
158 static void handle_sigalrm(int sig)
160 serial8250__inject_interrupt(kvm);
161 virtio_console__inject_interrupt(kvm);
164 static void *kvm_cpu_thread(void *arg)
166 current_kvm_cpu = arg;
168 if (kvm_cpu__start(current_kvm_cpu))
171 kvm_cpu__delete(current_kvm_cpu);
173 return (void *) (intptr_t) 0;
176 fprintf(stderr, "KVM exit reason: %u (\"%s\")\n",
177 current_kvm_cpu->kvm_run->exit_reason,
178 kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
179 if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
180 fprintf(stderr, "KVM exit code: 0x%Lu\n",
181 current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
183 kvm_cpu__show_registers(current_kvm_cpu);
184 kvm_cpu__show_code(current_kvm_cpu);
185 kvm_cpu__show_page_tables(current_kvm_cpu);
187 kvm_cpu__delete(current_kvm_cpu);
189 return (void *) (intptr_t) 1;
192 static char kernel[PATH_MAX];
193 const char *host_kernels[] = {
198 const char *default_kernels[] = {
200 "../../arch/x86/boot/bzImage",
204 static void kernel_usage_with_options(void)
209 fprintf(stderr, "Fatal: could not find default kernel image in:\n");
210 k = &default_kernels[0];
212 fprintf(stderr, "\t%s\n", *k);
219 k = &host_kernels[0];
221 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
223 fprintf(stderr, "\t%s\n", kernel);
226 fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n");
229 static u64 host_ram_size(void)
234 nr_pages = sysconf(_SC_PHYS_PAGES);
236 page_size = sysconf(_SC_PAGE_SIZE);
238 return (nr_pages * page_size) >> MB_SHIFT;
242 * If user didn't specify how much memory it wants to allocate for the guest,
243 * avoid filling the whole host RAM.
245 #define RAM_SIZE_RATIO 0.8
247 static u64 get_ram_size(int nr_cpus)
252 ram_size = 64 * (nr_cpus + 3);
254 available = host_ram_size() * RAM_SIZE_RATIO;
256 if (ram_size > available)
257 ram_size = available;
262 static const char *find_kernel(void)
268 k = &default_kernels[0];
270 if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
274 strncpy(kernel, *k, PATH_MAX);
281 k = &host_kernels[0];
283 if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
286 if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
296 static int root_device(char *dev, long *part)
300 if (stat("/", &st) < 0)
303 *part = minor(st.st_dev);
305 sprintf(dev, "/dev/block/%u:0", major(st.st_dev));
306 if (access(dev, R_OK) < 0)
312 static char *host_image(char *cmd_line, size_t size)
315 char device[PATH_MAX];
318 t = malloc(PATH_MAX);
322 /* check for the root file system */
323 if (root_device(device, &part) < 0) {
327 strncpy(t, device, PATH_MAX);
328 if (!strstr(cmd_line, "root=")) {
330 snprintf(tmp, sizeof(tmp), "root=/dev/vda%ld rw ", part);
331 strlcat(cmd_line, tmp, size);
336 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
338 static char real_cmdline[2048];
339 unsigned int nr_online_cpus;
343 struct virtio_net_parameters net_params;
346 signal(SIGALRM, handle_sigalrm);
347 signal(SIGQUIT, handle_sigquit);
348 signal(SIGUSR1, handle_sigusr1);
351 argc = parse_options(argc, argv, options, run_usage,
352 PARSE_OPT_STOP_AT_NON_OPTION);
354 if (kernel_filename) {
355 fprintf(stderr, "Cannot handle parameter: "
357 usage_with_options(run_usage, options);
360 /* first unhandled parameter is treated as a kernel
363 kernel_filename = argv[0];
370 if (!kernel_filename)
371 kernel_filename = find_kernel();
373 if (!kernel_filename) {
374 kernel_usage_with_options();
378 if (nrcpus < 1 || nrcpus > KVM_NR_CPUS)
379 die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS);
382 ram_size = get_ram_size(nrcpus);
384 if (ram_size < MIN_RAM_SIZE_MB)
385 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB);
387 if (ram_size > host_ram_size())
388 warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size, host_ram_size());
390 ram_size <<= MB_SHIFT;
393 kvm_dev = DEFAULT_KVM_DEV;
396 console = DEFAULT_CONSOLE;
398 if (!strncmp(console, "virtio", 6))
399 active_console = CONSOLE_VIRTIO;
401 active_console = CONSOLE_8250;
404 host_ip_addr = DEFAULT_HOST_ADDR;
407 guest_mac = DEFAULT_GUEST_MAC;
410 script = DEFAULT_SCRIPT;
414 kvm = kvm__init(kvm_dev, ram_size);
416 max_cpus = kvm__max_cpus(kvm);
418 if (nrcpus > max_cpus) {
419 printf(" # Limit the number of CPUs to %d\n", max_cpus);
420 kvm->nrcpus = max_cpus;
423 kvm->nrcpus = nrcpus;
425 memset(real_cmdline, 0, sizeof(real_cmdline));
426 strcpy(real_cmdline, "notsc noapic noacpi pci=conf1 console=ttyS0 earlyprintk=serial");
428 strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline));
431 if (!image_filename[0]) {
432 hi = host_image(real_cmdline, sizeof(real_cmdline));
434 image_filename[0] = hi;
435 readonly_image[0] = true;
440 if (!strstr(real_cmdline, "root="))
441 strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
443 for (i = 0; i < image_count; i++) {
444 if (image_filename[i]) {
445 struct disk_image *disk = disk_image__open(image_filename[i], readonly_image[i]);
447 die("unable to load disk image %s", image_filename[i]);
449 virtio_blk__init(kvm, disk);
454 if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,
456 die("unable to load kernel %s", kernel_filename);
458 ioport__setup_legacy();
462 serial8250__init(kvm);
466 if (active_console == CONSOLE_VIRTIO)
467 virtio_console__init(kvm);
470 virtio_rng__init(kvm);
473 network = DEFAULT_NETWORK;
475 if (!strncmp(network, "virtio", 6)) {
476 net_params = (struct virtio_net_parameters) {
477 .host_ip = host_ip_addr,
481 sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
482 net_params.guest_mac,
483 net_params.guest_mac+1,
484 net_params.guest_mac+2,
485 net_params.guest_mac+3,
486 net_params.guest_mac+4,
487 net_params.guest_mac+5);
489 virtio_net__init(&net_params);
492 kvm__start_timer(kvm);
494 kvm__setup_bios(kvm);
496 for (i = 0; i < nrcpus; i++) {
497 kvm_cpus[i] = kvm_cpu__init(kvm, i);
499 die("unable to initialize KVM VCPU");
502 kvm_cpu__enable_singlestep(kvm_cpus[i]);
507 nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
508 thread_pool__init(nr_online_cpus);
510 for (i = 0; i < nrcpus; i++) {
511 if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0)
512 die("unable to create KVM VCPU thread");
515 for (i = 0; i < nrcpus; i++) {
518 if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
528 printf("\n # KVM session ended normally.\n");