1 #include "kvm/builtin-run.h"
3 #include "kvm/virtio-balloon.h"
4 #include "kvm/virtio-console.h"
5 #include "kvm/parse-options.h"
6 #include "kvm/8250-serial.h"
7 #include "kvm/framebuffer.h"
8 #include "kvm/disk-image.h"
9 #include "kvm/threadpool.h"
10 #include "kvm/virtio-blk.h"
11 #include "kvm/virtio-net.h"
12 #include "kvm/virtio-rng.h"
13 #include "kvm/ioeventfd.h"
14 #include "kvm/virtio-9p.h"
15 #include "kvm/barrier.h"
16 #include "kvm/kvm-cpu.h"
17 #include "kvm/ioport.h"
18 #include "kvm/symbol.h"
19 #include "kvm/i8042.h"
20 #include "kvm/mutex.h"
30 #include "kvm/guest_compat.h"
32 #include <linux/types.h>
34 #include <sys/utsname.h>
35 #include <sys/types.h>
45 #define DEFAULT_KVM_DEV "/dev/kvm"
46 #define DEFAULT_CONSOLE "serial"
47 #define DEFAULT_NETWORK "user"
48 #define DEFAULT_HOST_ADDR "192.168.33.1"
49 #define DEFAULT_GUEST_ADDR "192.168.33.15"
50 #define DEFAULT_GUEST_MAC "02:15:15:15:15:15"
51 #define DEFAULT_HOST_MAC "02:01:01:01:01:01"
52 #define DEFAULT_SCRIPT "none"
55 #define MIN_RAM_SIZE_MB (64ULL)
56 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT)
59 struct kvm_cpu
*kvm_cpus
[KVM_NR_CPUS
];
60 __thread
struct kvm_cpu
*current_kvm_cpu
;
63 static u8 image_count
;
64 static bool virtio_rng
;
65 static const char *kernel_cmdline
;
66 static const char *kernel_filename
;
67 static const char *vmlinux_filename
;
68 static const char *initrd_filename
;
69 static const char *image_filename
[MAX_DISK_IMAGES
];
70 static const char *console
;
71 static const char *dev
;
72 static const char *network
;
73 static const char *host_ip
;
74 static const char *guest_ip
;
75 static const char *guest_mac
;
76 static const char *host_mac
;
77 static const char *script
;
78 static const char *guest_name
;
79 static bool single_step
;
80 static bool readonly_image
[MAX_DISK_IMAGES
];
84 static bool using_rootfs
;
85 extern bool ioport_debug
;
86 extern int active_console
;
87 extern int debug_iodelay
;
89 bool do_debug_print
= false;
92 static int vidmode
= -1;
94 static const char * const run_usage
[] = {
95 "kvm run [<options>] [<kernel image>]",
99 static int img_name_parser(const struct option
*opt
, const char *arg
, int unset
)
104 if (stat(arg
, &st
) == 0 &&
105 S_ISDIR(st
.st_mode
)) {
108 if (realpath(arg
, tmp
) == 0 ||
109 virtio_9p__register(kvm
, tmp
, "/dev/root") < 0)
110 die("Unable to initialize virtio 9p");
115 if (image_count
>= MAX_DISK_IMAGES
)
116 die("Currently only 4 images are supported");
118 image_filename
[image_count
] = arg
;
119 sep
= strstr(arg
, ",");
121 if (strcmp(sep
+ 1, "ro") == 0)
122 readonly_image
[image_count
] = 1;
131 static int virtio_9p_rootdir_parser(const struct option
*opt
, const char *arg
, int unset
)
137 * 9p dir can be of the form dirname,tag_name or
138 * just dirname. In the later case we use the
141 tag_name
= strstr(arg
, ",");
146 if (realpath(arg
, tmp
)) {
147 if (virtio_9p__register(kvm
, tmp
, tag_name
) < 0)
148 die("Unable to initialize virtio 9p");
150 die("Failed resolving 9p path");
155 static const struct option options
[] = {
156 OPT_GROUP("Basic options:"),
157 OPT_STRING('\0', "name", &guest_name
, "guest name",
158 "A name for the guest"),
159 OPT_INTEGER('c', "cpus", &nrcpus
, "Number of CPUs"),
160 OPT_U64('m', "mem", &ram_size
, "Virtual machine memory size in MiB."),
161 OPT_CALLBACK('d', "disk", NULL
, "image or rootfs_dir", "Disk image or rootfs directory", img_name_parser
),
162 OPT_BOOLEAN('\0', "balloon", &balloon
, "Enable virtio balloon"),
163 OPT_BOOLEAN('\0', "vnc", &vnc
, "Enable VNC framebuffer"),
164 OPT_BOOLEAN('\0', "sdl", &sdl
, "Enable SDL framebuffer"),
165 OPT_BOOLEAN('\0', "rng", &virtio_rng
, "Enable virtio Random Number Generator"),
166 OPT_CALLBACK('\0', "9p", NULL
, "dir_to_share,tag_name",
167 "Enable virtio 9p to share files between host and guest", virtio_9p_rootdir_parser
),
168 OPT_STRING('\0', "console", &console
, "serial or virtio",
170 OPT_STRING('\0', "dev", &dev
, "device_file", "KVM device file"),
172 OPT_GROUP("Kernel options:"),
173 OPT_STRING('k', "kernel", &kernel_filename
, "kernel",
174 "Kernel to boot in virtual machine"),
175 OPT_STRING('i', "initrd", &initrd_filename
, "initrd",
176 "Initial RAM disk image"),
177 OPT_STRING('p', "params", &kernel_cmdline
, "params",
178 "Kernel command line arguments"),
180 OPT_GROUP("Networking options:"),
181 OPT_STRING('n', "network", &network
, "user, tap, none",
183 OPT_STRING('\0', "host-ip", &host_ip
, "a.b.c.d",
184 "Assign this address to the host side networking"),
185 OPT_STRING('\0', "guest-ip", &guest_ip
, "a.b.c.d",
186 "Assign this address to the guest side networking"),
187 OPT_STRING('\0', "host-mac", &host_mac
, "aa:bb:cc:dd:ee:ff",
188 "Assign this address to the host side NIC"),
189 OPT_STRING('\0', "guest-mac", &guest_mac
, "aa:bb:cc:dd:ee:ff",
190 "Assign this address to the guest side NIC"),
191 OPT_STRING('\0', "tapscript", &script
, "Script path",
192 "Assign a script to process created tap device"),
194 OPT_GROUP("BIOS options:"),
195 OPT_INTEGER('\0', "vidmode", &vidmode
,
198 OPT_GROUP("Debug options:"),
199 OPT_BOOLEAN('\0', "debug", &do_debug_print
,
200 "Enable debug messages"),
201 OPT_BOOLEAN('\0', "debug-single-step", &single_step
,
202 "Enable single stepping"),
203 OPT_BOOLEAN('\0', "debug-ioport", &ioport_debug
,
204 "Enable ioport debugging"),
205 OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay
,
206 "Delay IO by millisecond"),
211 * Serialize debug printout so that the output of multiple vcpus does not
214 static int printout_done
;
216 static void handle_sigusr1(int sig
)
218 struct kvm_cpu
*cpu
= current_kvm_cpu
;
223 printf("\n #\n # vCPU #%ld's dump:\n #\n", cpu
->cpu_id
);
224 kvm_cpu__show_registers(cpu
);
225 kvm_cpu__show_code(cpu
);
226 kvm_cpu__show_page_tables(cpu
);
232 /* Pause/resume the guest using SIGUSR2 */
233 static int is_paused
;
235 static void handle_sigusr2(int sig
)
237 if (sig
== SIGKVMRESUME
&& is_paused
)
239 else if (sig
== SIGUSR2
&& !is_paused
)
244 is_paused
= !is_paused
;
245 pr_info("Guest %s\n", is_paused
? "paused" : "resumed");
248 static void handle_sigquit(int sig
)
252 for (i
= 0; i
< nrcpus
; i
++) {
253 struct kvm_cpu
*cpu
= kvm_cpus
[i
];
259 pthread_kill(cpu
->thread
, SIGUSR1
);
261 * Wait for the vCPU to dump state before signalling
262 * the next thread. Since this is debug code it does
263 * not matter that we are burning CPU time a bit:
265 while (!printout_done
)
269 serial8250__inject_sysrq(kvm
);
272 static void handle_sigalrm(int sig
)
274 serial8250__inject_interrupt(kvm
);
275 virtio_console__inject_interrupt(kvm
);
278 static void handle_sigstop(int sig
)
283 static void *kvm_cpu_thread(void *arg
)
285 current_kvm_cpu
= arg
;
287 if (kvm_cpu__start(current_kvm_cpu
))
290 kvm_cpu__delete(current_kvm_cpu
);
292 return (void *) (intptr_t) 0;
295 fprintf(stderr
, "KVM exit reason: %u (\"%s\")\n",
296 current_kvm_cpu
->kvm_run
->exit_reason
,
297 kvm_exit_reasons
[current_kvm_cpu
->kvm_run
->exit_reason
]);
298 if (current_kvm_cpu
->kvm_run
->exit_reason
== KVM_EXIT_UNKNOWN
)
299 fprintf(stderr
, "KVM exit code: 0x%Lu\n",
300 current_kvm_cpu
->kvm_run
->hw
.hardware_exit_reason
);
302 kvm_cpu__show_registers(current_kvm_cpu
);
303 kvm_cpu__show_code(current_kvm_cpu
);
304 kvm_cpu__show_page_tables(current_kvm_cpu
);
306 kvm_cpu__delete(current_kvm_cpu
);
308 return (void *) (intptr_t) 1;
311 static char kernel
[PATH_MAX
];
313 static const char *host_kernels
[] = {
319 static const char *default_kernels
[] = {
321 "../../arch/x86/boot/bzImage",
325 static const char *default_vmlinux
[] = {
331 static void kernel_usage_with_options(void)
336 fprintf(stderr
, "Fatal: could not find default kernel image in:\n");
337 k
= &default_kernels
[0];
339 fprintf(stderr
, "\t%s\n", *k
);
346 k
= &host_kernels
[0];
348 if (snprintf(kernel
, PATH_MAX
, "%s-%s", *k
, uts
.release
) < 0)
350 fprintf(stderr
, "\t%s\n", kernel
);
353 fprintf(stderr
, "\nPlease see 'kvm run --help' for more options.\n\n");
356 static u64
host_ram_size(void)
361 nr_pages
= sysconf(_SC_PHYS_PAGES
);
363 pr_warning("sysconf(_SC_PHYS_PAGES) failed");
367 page_size
= sysconf(_SC_PAGE_SIZE
);
369 pr_warning("sysconf(_SC_PAGE_SIZE) failed");
373 return (nr_pages
* page_size
) >> MB_SHIFT
;
377 * If user didn't specify how much memory it wants to allocate for the guest,
378 * avoid filling the whole host RAM.
380 #define RAM_SIZE_RATIO 0.8
382 static u64
get_ram_size(int nr_cpus
)
387 ram_size
= 64 * (nr_cpus
+ 3);
389 available
= host_ram_size() * RAM_SIZE_RATIO
;
391 available
= MIN_RAM_SIZE_MB
;
393 if (ram_size
> available
)
394 ram_size
= available
;
399 static const char *find_kernel(void)
405 k
= &default_kernels
[0];
407 if (stat(*k
, &st
) < 0 || !S_ISREG(st
.st_mode
)) {
411 strncpy(kernel
, *k
, PATH_MAX
);
418 k
= &host_kernels
[0];
420 if (snprintf(kernel
, PATH_MAX
, "%s-%s", *k
, uts
.release
) < 0)
423 if (stat(kernel
, &st
) < 0 || !S_ISREG(st
.st_mode
)) {
433 static const char *find_vmlinux(void)
435 const char **vmlinux
;
437 vmlinux
= &default_vmlinux
[0];
441 if (stat(*vmlinux
, &st
) < 0 || !S_ISREG(st
.st_mode
)) {
450 void kvm_run_help(void)
452 usage_with_options(run_usage
, options
);
455 int kvm_cmd_run(int argc
, const char **argv
, const char *prefix
)
457 struct virtio_net_parameters net_params
;
458 static char real_cmdline
[2048], default_name
[20];
459 struct framebuffer
*fb
= NULL
;
460 unsigned int nr_online_cpus
;
462 int max_cpus
, recommended_cpus
;
466 signal(SIGALRM
, handle_sigalrm
);
467 signal(SIGQUIT
, handle_sigquit
);
468 signal(SIGUSR1
, handle_sigusr1
);
469 signal(SIGUSR2
, handle_sigusr2
);
470 signal(SIGKVMSTOP
, handle_sigstop
);
471 signal(SIGKVMRESUME
, handle_sigusr2
);
472 /* ignore balloon signal by default if not enable balloon optiion */
473 signal(SIGKVMADDMEM
, SIG_IGN
);
474 signal(SIGKVMDELMEM
, SIG_IGN
);
476 nr_online_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
479 argc
= parse_options(argc
, argv
, options
, run_usage
,
480 PARSE_OPT_STOP_AT_NON_OPTION
);
482 if (kernel_filename
) {
483 fprintf(stderr
, "Cannot handle parameter: "
485 usage_with_options(run_usage
, options
);
488 /* first unhandled parameter is treated as a kernel
491 kernel_filename
= argv
[0];
498 if (!kernel_filename
)
499 kernel_filename
= find_kernel();
501 if (!kernel_filename
) {
502 kernel_usage_with_options();
506 vmlinux_filename
= find_vmlinux();
509 nrcpus
= nr_online_cpus
;
510 else if (nrcpus
< 1 || nrcpus
> KVM_NR_CPUS
)
511 die("Number of CPUs %d is out of [1;%d] range", nrcpus
, KVM_NR_CPUS
);
514 ram_size
= get_ram_size(nrcpus
);
516 if (ram_size
< MIN_RAM_SIZE_MB
)
517 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size
, MIN_RAM_SIZE_MB
);
519 if (ram_size
> host_ram_size())
520 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size
, host_ram_size());
522 ram_size
<<= MB_SHIFT
;
525 dev
= DEFAULT_KVM_DEV
;
528 console
= DEFAULT_CONSOLE
;
530 if (!strncmp(console
, "virtio", 6))
531 active_console
= CONSOLE_VIRTIO
;
533 active_console
= CONSOLE_8250
;
536 host_ip
= DEFAULT_HOST_ADDR
;
539 guest_ip
= DEFAULT_GUEST_ADDR
;
542 guest_mac
= DEFAULT_GUEST_MAC
;
545 host_mac
= DEFAULT_HOST_MAC
;
548 script
= DEFAULT_SCRIPT
;
550 symbol__init(vmlinux_filename
);
555 sprintf(default_name
, "guest-%u", getpid());
556 guest_name
= default_name
;
559 kvm
= kvm__init(dev
, ram_size
, guest_name
);
563 kvm
->single_step
= single_step
;
567 max_cpus
= kvm__max_cpus(kvm
);
568 recommended_cpus
= kvm__recommended_cpus(kvm
);
570 if (nrcpus
> max_cpus
) {
571 printf(" # Limit the number of CPUs to %d\n", max_cpus
);
572 kvm
->nrcpus
= max_cpus
;
573 } else if (nrcpus
> recommended_cpus
) {
574 printf(" # Warning: The maximum recommended amount of VCPUs"
575 " is %d\n", recommended_cpus
);
578 kvm
->nrcpus
= nrcpus
;
581 * vidmode should be either specified
582 * either set by default
590 memset(real_cmdline
, 0, sizeof(real_cmdline
));
591 strcpy(real_cmdline
, "notsc noapic noacpi pci=conf1 reboot=k panic=1 i8042.direct=1 i8042.dumbkbd=1 i8042.nopnp=1");
593 strcat(real_cmdline
, " video=vesafb console=tty0");
595 strcat(real_cmdline
, " console=ttyS0 earlyprintk=serial");
596 strcat(real_cmdline
, " ");
598 strlcat(real_cmdline
, kernel_cmdline
, sizeof(real_cmdline
));
600 if (!using_rootfs
&& !image_filename
[0]) {
601 if (virtio_9p__register(kvm
, "/", "/dev/root") < 0)
602 die("Unable to initialize virtio 9p");
606 if (!strstr(real_cmdline
, "init="))
607 strlcat(real_cmdline
, " init=/bin/sh ", sizeof(real_cmdline
));
610 if (!strstr(real_cmdline
, "root="))
611 strlcat(real_cmdline
, " root=/dev/vda rw ", sizeof(real_cmdline
));
614 strcat(real_cmdline
, " root=/dev/root rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
617 kvm
->nr_disks
= image_count
;
618 kvm
->disks
= disk_image__open_all(image_filename
, readonly_image
, image_count
);
620 die("Unable to load all disk images.");
622 virtio_blk__init_all(kvm
);
625 printf(" # kvm run -k %s -m %Lu -c %d --name %s\n", kernel_filename
, ram_size
/ 1024 / 1024, nrcpus
, guest_name
);
627 if (!kvm__load_kernel(kvm
, kernel_filename
, initrd_filename
,
628 real_cmdline
, vidmode
))
629 die("unable to load kernel %s", kernel_filename
);
631 kvm
->vmlinux
= vmlinux_filename
;
633 ioport__setup_legacy();
637 serial8250__init(kvm
);
641 if (active_console
== CONSOLE_VIRTIO
)
642 virtio_console__init(kvm
);
645 virtio_rng__init(kvm
);
648 virtio_bln__init(kvm
);
651 network
= DEFAULT_NETWORK
;
653 virtio_9p__init(kvm
);
655 if (strncmp(network
, "none", 4)) {
656 net_params
.guest_ip
= guest_ip
;
657 net_params
.host_ip
= host_ip
;
658 net_params
.kvm
= kvm
;
659 net_params
.script
= script
;
660 sscanf(guest_mac
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
661 net_params
.guest_mac
,
662 net_params
.guest_mac
+1,
663 net_params
.guest_mac
+2,
664 net_params
.guest_mac
+3,
665 net_params
.guest_mac
+4,
666 net_params
.guest_mac
+5);
667 sscanf(host_mac
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
669 net_params
.host_mac
+1,
670 net_params
.host_mac
+2,
671 net_params
.host_mac
+3,
672 net_params
.host_mac
+4,
673 net_params
.host_mac
+5);
675 if (!strncmp(network
, "user", 4))
676 net_params
.mode
= NET_MODE_USER
;
677 else if (!strncmp(network
, "tap", 3))
678 net_params
.mode
= NET_MODE_TAP
;
680 die("Unkown network mode %s, please use -network user, tap, none", network
);
681 virtio_net__init(&net_params
);
684 kvm__start_timer(kvm
);
686 kvm__setup_bios(kvm
);
688 for (i
= 0; i
< nrcpus
; i
++) {
689 kvm_cpus
[i
] = kvm_cpu__init(kvm
, i
);
691 die("unable to initialize KVM VCPU");
699 fb
= vesa__init(kvm
);
713 thread_pool__init(nr_online_cpus
);
716 for (i
= 0; i
< nrcpus
; i
++) {
717 if (pthread_create(&kvm_cpus
[i
]->thread
, NULL
, kvm_cpu_thread
, kvm_cpus
[i
]) != 0)
718 die("unable to create KVM VCPU thread");
721 /* Only VCPU #0 is going to exit by itself when shutting down */
722 if (pthread_join(kvm_cpus
[0]->thread
, &ret
) != 0)
725 for (i
= 1; i
< nrcpus
; i
++) {
726 if (kvm_cpus
[i
]->is_running
) {
727 pthread_kill(kvm_cpus
[i
]->thread
, SIGKVMEXIT
);
728 if (pthread_join(kvm_cpus
[i
]->thread
, &ret
) != 0)
735 compat__print_all_messages();
739 virtio_blk__delete_all(kvm
);
740 virtio_rng__delete_all(kvm
);
742 disk_image__close_all(kvm
->disks
, image_count
);
746 printf("\n # KVM session ended normally.\n");