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"
31 #include "kvm/pci-shmem.h"
33 #include <linux/types.h>
35 #include <sys/utsname.h>
36 #include <sys/types.h>
46 #define DEFAULT_KVM_DEV "/dev/kvm"
47 #define DEFAULT_CONSOLE "serial"
48 #define DEFAULT_NETWORK "user"
49 #define DEFAULT_HOST_ADDR "192.168.33.1"
50 #define DEFAULT_GUEST_ADDR "192.168.33.15"
51 #define DEFAULT_GUEST_MAC "02:15:15:15:15:15"
52 #define DEFAULT_HOST_MAC "02:01:01:01:01:01"
53 #define DEFAULT_SCRIPT "none"
58 #define MIN_RAM_SIZE_MB (64ULL)
59 #define MIN_RAM_SIZE_BYTE (MIN_RAM_SIZE_MB << MB_SHIFT)
62 struct kvm_cpu
*kvm_cpus
[KVM_NR_CPUS
];
63 __thread
struct kvm_cpu
*current_kvm_cpu
;
66 static u8 image_count
;
67 static bool virtio_rng
;
68 static const char *kernel_cmdline
;
69 static const char *kernel_filename
;
70 static const char *vmlinux_filename
;
71 static const char *initrd_filename
;
72 static const char *image_filename
[MAX_DISK_IMAGES
];
73 static const char *console
;
74 static const char *dev
;
75 static const char *network
;
76 static const char *host_ip
;
77 static const char *guest_ip
;
78 static const char *guest_mac
;
79 static const char *host_mac
;
80 static const char *script
;
81 static const char *guest_name
;
82 static bool single_step
;
83 static bool readonly_image
[MAX_DISK_IMAGES
];
87 static bool using_rootfs
;
88 extern bool ioport_debug
;
89 extern int active_console
;
90 extern int debug_iodelay
;
92 bool do_debug_print
= false;
95 static int vidmode
= -1;
97 static const char * const run_usage
[] = {
98 "kvm run [<options>] [<kernel image>]",
102 static int img_name_parser(const struct option
*opt
, const char *arg
, int unset
)
107 if (stat(arg
, &st
) == 0 &&
108 S_ISDIR(st
.st_mode
)) {
111 if (realpath(arg
, tmp
) == 0 ||
112 virtio_9p__register(kvm
, tmp
, "/dev/root") < 0)
113 die("Unable to initialize virtio 9p");
118 if (image_count
>= MAX_DISK_IMAGES
)
119 die("Currently only 4 images are supported");
121 image_filename
[image_count
] = arg
;
122 sep
= strstr(arg
, ",");
124 if (strcmp(sep
+ 1, "ro") == 0)
125 readonly_image
[image_count
] = 1;
134 static int virtio_9p_rootdir_parser(const struct option
*opt
, const char *arg
, int unset
)
140 * 9p dir can be of the form dirname,tag_name or
141 * just dirname. In the later case we use the
144 tag_name
= strstr(arg
, ",");
149 if (realpath(arg
, tmp
)) {
150 if (virtio_9p__register(kvm
, tmp
, tag_name
) < 0)
151 die("Unable to initialize virtio 9p");
153 die("Failed resolving 9p path");
157 static int shmem_parser(const struct option
*opt
, const char *arg
, int unset
)
159 const uint64_t default_size
= SHMEM_DEFAULT_SIZE
;
160 const uint64_t default_phys_addr
= SHMEM_DEFAULT_ADDR
;
161 const char *default_handle
= SHMEM_DEFAULT_HANDLE
;
162 struct shmem_info
*si
= malloc(sizeof(struct shmem_info
));
163 enum { PCI
, UNK
} addr_type
= PCI
;
173 const int skip_pci
= strlen("pci:");
175 pr_info("shmem_parser(%p,%s,%d)", opt
, arg
, unset
);
176 /* parse out optional addr family */
177 if (strcasestr(p
, "pci:")) {
180 } else if (strcasestr(p
, "mem:")) {
181 die("I can't add to E820 map yet.\n");
183 /* parse out physical addr */
185 if (strcasestr(p
, "0x"))
187 phys_addr
= strtoll(p
, &next
, base
);
188 if (next
== p
&& phys_addr
== 0) {
189 pr_info("shmem: no physical addr specified, using default.");
190 phys_addr
= default_phys_addr
;
192 if (*next
!= ':' && *next
!= '\0')
193 die("shmem: unexpected chars after phys addr.\n");
200 if (strcasestr(p
, "0x"))
202 size
= strtoll(p
, &next
, base
);
203 if (next
== p
&& size
== 0) {
204 pr_info("shmem: no size specified, using default.");
207 /* look for [KMGkmg][Bb]* uses base 2. */
209 if (strspn(next
, "KMGkmg")) { /* might have a prefix */
210 if (*(next
+ 1) == 'B' || *(next
+ 1) == 'b')
215 size
= size
<< KB_SHIFT
;
219 size
= size
<< MB_SHIFT
;
223 size
= size
<< GB_SHIFT
;
226 die("shmem: bug in detecting size prefix.");
231 if (*next
!= ':' && *next
!= '\0') {
232 die("shmem: unexpected chars after phys size. <%c><%c>\n",
239 /* parse out optional shmem handle */
240 const int skip_handle
= strlen("handle=");
241 next
= strcasestr(p
, "handle=");
244 die("unexpected chars before handle\n");
246 next
= strchrnul(p
, ':');
248 handle
= malloc(next
- p
+ 1);
249 strncpy(handle
, p
, next
- p
);
250 handle
[next
- p
] = '\0'; /* just in case. */
257 /* parse optional create flag to see if we should create shm seg. */
258 if (*p
&& strcasestr(p
, "create")) {
260 p
+= strlen("create");
263 die("shmem: unexpected trailing chars\n");
264 if (handle
== NULL
) {
265 handle
= malloc(strlen(default_handle
) + 1);
266 strcpy(handle
, default_handle
);
269 pr_info("shmem: phys_addr = %lx", phys_addr
);
270 pr_info("shmem: size = %lx", size
);
271 pr_info("shmem: handle = %s", handle
);
272 pr_info("shmem: create = %d", create
);
275 si
->phys_addr
= phys_addr
;
279 pci_shmem__register_mem(si
); /* ownership of si, etc. passed on. */
283 static const struct option options
[] = {
284 OPT_GROUP("Basic options:"),
285 OPT_STRING('\0', "name", &guest_name
, "guest name",
286 "A name for the guest"),
287 OPT_INTEGER('c', "cpus", &nrcpus
, "Number of CPUs"),
288 OPT_U64('m', "mem", &ram_size
, "Virtual machine memory size in MiB."),
289 OPT_CALLBACK('\0', "shmem", NULL
,
290 "[pci:]<addr>:<size>[:handle=<handle>][:create]",
291 "Share host shmem with guest via pci device",
293 OPT_CALLBACK('d', "disk", NULL
, "image or rootfs_dir", "Disk image or rootfs directory", img_name_parser
),
294 OPT_BOOLEAN('\0', "balloon", &balloon
, "Enable virtio balloon"),
295 OPT_BOOLEAN('\0', "vnc", &vnc
, "Enable VNC framebuffer"),
296 OPT_BOOLEAN('\0', "sdl", &sdl
, "Enable SDL framebuffer"),
297 OPT_BOOLEAN('\0', "rng", &virtio_rng
, "Enable virtio Random Number Generator"),
298 OPT_CALLBACK('\0', "9p", NULL
, "dir_to_share,tag_name",
299 "Enable virtio 9p to share files between host and guest", virtio_9p_rootdir_parser
),
300 OPT_STRING('\0', "console", &console
, "serial or virtio",
302 OPT_STRING('\0', "dev", &dev
, "device_file", "KVM device file"),
304 OPT_GROUP("Kernel options:"),
305 OPT_STRING('k', "kernel", &kernel_filename
, "kernel",
306 "Kernel to boot in virtual machine"),
307 OPT_STRING('i', "initrd", &initrd_filename
, "initrd",
308 "Initial RAM disk image"),
309 OPT_STRING('p', "params", &kernel_cmdline
, "params",
310 "Kernel command line arguments"),
312 OPT_GROUP("Networking options:"),
313 OPT_STRING('n', "network", &network
, "user, tap, none",
315 OPT_STRING('\0', "host-ip", &host_ip
, "a.b.c.d",
316 "Assign this address to the host side networking"),
317 OPT_STRING('\0', "guest-ip", &guest_ip
, "a.b.c.d",
318 "Assign this address to the guest side networking"),
319 OPT_STRING('\0', "host-mac", &host_mac
, "aa:bb:cc:dd:ee:ff",
320 "Assign this address to the host side NIC"),
321 OPT_STRING('\0', "guest-mac", &guest_mac
, "aa:bb:cc:dd:ee:ff",
322 "Assign this address to the guest side NIC"),
323 OPT_STRING('\0', "tapscript", &script
, "Script path",
324 "Assign a script to process created tap device"),
326 OPT_GROUP("BIOS options:"),
327 OPT_INTEGER('\0', "vidmode", &vidmode
,
330 OPT_GROUP("Debug options:"),
331 OPT_BOOLEAN('\0', "debug", &do_debug_print
,
332 "Enable debug messages"),
333 OPT_BOOLEAN('\0', "debug-single-step", &single_step
,
334 "Enable single stepping"),
335 OPT_BOOLEAN('\0', "debug-ioport", &ioport_debug
,
336 "Enable ioport debugging"),
337 OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay
,
338 "Delay IO by millisecond"),
343 * Serialize debug printout so that the output of multiple vcpus does not
346 static int printout_done
;
348 static void handle_sigusr1(int sig
)
350 struct kvm_cpu
*cpu
= current_kvm_cpu
;
355 printf("\n #\n # vCPU #%ld's dump:\n #\n", cpu
->cpu_id
);
356 kvm_cpu__show_registers(cpu
);
357 kvm_cpu__show_code(cpu
);
358 kvm_cpu__show_page_tables(cpu
);
364 /* Pause/resume the guest using SIGUSR2 */
365 static int is_paused
;
367 static void handle_sigusr2(int sig
)
369 if (sig
== SIGKVMRESUME
&& is_paused
)
371 else if (sig
== SIGUSR2
&& !is_paused
)
376 is_paused
= !is_paused
;
377 pr_info("Guest %s\n", is_paused
? "paused" : "resumed");
380 static void handle_sigquit(int sig
)
384 for (i
= 0; i
< nrcpus
; i
++) {
385 struct kvm_cpu
*cpu
= kvm_cpus
[i
];
391 pthread_kill(cpu
->thread
, SIGUSR1
);
393 * Wait for the vCPU to dump state before signalling
394 * the next thread. Since this is debug code it does
395 * not matter that we are burning CPU time a bit:
397 while (!printout_done
)
401 serial8250__inject_sysrq(kvm
);
404 static void handle_sigalrm(int sig
)
406 serial8250__inject_interrupt(kvm
);
407 virtio_console__inject_interrupt(kvm
);
410 static void handle_sigstop(int sig
)
415 static void *kvm_cpu_thread(void *arg
)
417 current_kvm_cpu
= arg
;
419 if (kvm_cpu__start(current_kvm_cpu
))
422 kvm_cpu__delete(current_kvm_cpu
);
424 return (void *) (intptr_t) 0;
427 fprintf(stderr
, "KVM exit reason: %u (\"%s\")\n",
428 current_kvm_cpu
->kvm_run
->exit_reason
,
429 kvm_exit_reasons
[current_kvm_cpu
->kvm_run
->exit_reason
]);
430 if (current_kvm_cpu
->kvm_run
->exit_reason
== KVM_EXIT_UNKNOWN
)
431 fprintf(stderr
, "KVM exit code: 0x%Lu\n",
432 current_kvm_cpu
->kvm_run
->hw
.hardware_exit_reason
);
434 kvm_cpu__show_registers(current_kvm_cpu
);
435 kvm_cpu__show_code(current_kvm_cpu
);
436 kvm_cpu__show_page_tables(current_kvm_cpu
);
438 kvm_cpu__delete(current_kvm_cpu
);
440 return (void *) (intptr_t) 1;
443 static char kernel
[PATH_MAX
];
445 static const char *host_kernels
[] = {
451 static const char *default_kernels
[] = {
453 "../../arch/x86/boot/bzImage",
457 static const char *default_vmlinux
[] = {
463 static void kernel_usage_with_options(void)
468 fprintf(stderr
, "Fatal: could not find default kernel image in:\n");
469 k
= &default_kernels
[0];
471 fprintf(stderr
, "\t%s\n", *k
);
478 k
= &host_kernels
[0];
480 if (snprintf(kernel
, PATH_MAX
, "%s-%s", *k
, uts
.release
) < 0)
482 fprintf(stderr
, "\t%s\n", kernel
);
485 fprintf(stderr
, "\nPlease see 'kvm run --help' for more options.\n\n");
488 static u64
host_ram_size(void)
493 nr_pages
= sysconf(_SC_PHYS_PAGES
);
495 pr_warning("sysconf(_SC_PHYS_PAGES) failed");
499 page_size
= sysconf(_SC_PAGE_SIZE
);
501 pr_warning("sysconf(_SC_PAGE_SIZE) failed");
505 return (nr_pages
* page_size
) >> MB_SHIFT
;
509 * If user didn't specify how much memory it wants to allocate for the guest,
510 * avoid filling the whole host RAM.
512 #define RAM_SIZE_RATIO 0.8
514 static u64
get_ram_size(int nr_cpus
)
519 ram_size
= 64 * (nr_cpus
+ 3);
521 available
= host_ram_size() * RAM_SIZE_RATIO
;
523 available
= MIN_RAM_SIZE_MB
;
525 if (ram_size
> available
)
526 ram_size
= available
;
531 static const char *find_kernel(void)
537 k
= &default_kernels
[0];
539 if (stat(*k
, &st
) < 0 || !S_ISREG(st
.st_mode
)) {
543 strncpy(kernel
, *k
, PATH_MAX
);
550 k
= &host_kernels
[0];
552 if (snprintf(kernel
, PATH_MAX
, "%s-%s", *k
, uts
.release
) < 0)
555 if (stat(kernel
, &st
) < 0 || !S_ISREG(st
.st_mode
)) {
565 static const char *find_vmlinux(void)
567 const char **vmlinux
;
569 vmlinux
= &default_vmlinux
[0];
573 if (stat(*vmlinux
, &st
) < 0 || !S_ISREG(st
.st_mode
)) {
582 void kvm_run_help(void)
584 usage_with_options(run_usage
, options
);
587 int kvm_cmd_run(int argc
, const char **argv
, const char *prefix
)
589 struct virtio_net_parameters net_params
;
590 static char real_cmdline
[2048], default_name
[20];
591 struct framebuffer
*fb
= NULL
;
592 unsigned int nr_online_cpus
;
594 int max_cpus
, recommended_cpus
;
598 signal(SIGALRM
, handle_sigalrm
);
599 signal(SIGQUIT
, handle_sigquit
);
600 signal(SIGUSR1
, handle_sigusr1
);
601 signal(SIGUSR2
, handle_sigusr2
);
602 signal(SIGKVMSTOP
, handle_sigstop
);
603 signal(SIGKVMRESUME
, handle_sigusr2
);
604 /* ignore balloon signal by default if not enable balloon optiion */
605 signal(SIGKVMADDMEM
, SIG_IGN
);
606 signal(SIGKVMDELMEM
, SIG_IGN
);
608 nr_online_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
611 argc
= parse_options(argc
, argv
, options
, run_usage
,
612 PARSE_OPT_STOP_AT_NON_OPTION
);
614 if (kernel_filename
) {
615 fprintf(stderr
, "Cannot handle parameter: "
617 usage_with_options(run_usage
, options
);
620 /* first unhandled parameter is treated as a kernel
623 kernel_filename
= argv
[0];
630 if (!kernel_filename
)
631 kernel_filename
= find_kernel();
633 if (!kernel_filename
) {
634 kernel_usage_with_options();
638 vmlinux_filename
= find_vmlinux();
641 nrcpus
= nr_online_cpus
;
642 else if (nrcpus
< 1 || nrcpus
> KVM_NR_CPUS
)
643 die("Number of CPUs %d is out of [1;%d] range", nrcpus
, KVM_NR_CPUS
);
646 ram_size
= get_ram_size(nrcpus
);
648 if (ram_size
< MIN_RAM_SIZE_MB
)
649 die("Not enough memory specified: %lluMB (min %lluMB)", ram_size
, MIN_RAM_SIZE_MB
);
651 if (ram_size
> host_ram_size())
652 pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size
, host_ram_size());
654 ram_size
<<= MB_SHIFT
;
657 dev
= DEFAULT_KVM_DEV
;
660 console
= DEFAULT_CONSOLE
;
662 if (!strncmp(console
, "virtio", 6))
663 active_console
= CONSOLE_VIRTIO
;
665 active_console
= CONSOLE_8250
;
668 host_ip
= DEFAULT_HOST_ADDR
;
671 guest_ip
= DEFAULT_GUEST_ADDR
;
674 guest_mac
= DEFAULT_GUEST_MAC
;
677 host_mac
= DEFAULT_HOST_MAC
;
680 script
= DEFAULT_SCRIPT
;
682 symbol__init(vmlinux_filename
);
687 sprintf(default_name
, "guest-%u", getpid());
688 guest_name
= default_name
;
691 kvm
= kvm__init(dev
, ram_size
, guest_name
);
695 kvm
->single_step
= single_step
;
699 max_cpus
= kvm__max_cpus(kvm
);
700 recommended_cpus
= kvm__recommended_cpus(kvm
);
702 if (nrcpus
> max_cpus
) {
703 printf(" # Limit the number of CPUs to %d\n", max_cpus
);
704 kvm
->nrcpus
= max_cpus
;
705 } else if (nrcpus
> recommended_cpus
) {
706 printf(" # Warning: The maximum recommended amount of VCPUs"
707 " is %d\n", recommended_cpus
);
710 kvm
->nrcpus
= nrcpus
;
713 * vidmode should be either specified
714 * either set by default
722 memset(real_cmdline
, 0, sizeof(real_cmdline
));
723 strcpy(real_cmdline
, "notsc noapic noacpi pci=conf1 reboot=k panic=1 i8042.direct=1 i8042.dumbkbd=1 i8042.nopnp=1");
725 strcat(real_cmdline
, " video=vesafb console=tty0");
727 strcat(real_cmdline
, " console=ttyS0 earlyprintk=serial");
728 strcat(real_cmdline
, " ");
730 strlcat(real_cmdline
, kernel_cmdline
, sizeof(real_cmdline
));
732 if (!using_rootfs
&& !image_filename
[0]) {
733 if (virtio_9p__register(kvm
, "/", "/dev/root") < 0)
734 die("Unable to initialize virtio 9p");
738 if (!strstr(real_cmdline
, "init="))
739 strlcat(real_cmdline
, " init=/bin/sh ", sizeof(real_cmdline
));
742 if (!strstr(real_cmdline
, "root="))
743 strlcat(real_cmdline
, " root=/dev/vda rw ", sizeof(real_cmdline
));
746 strcat(real_cmdline
, " root=/dev/root rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
749 kvm
->nr_disks
= image_count
;
750 kvm
->disks
= disk_image__open_all(image_filename
, readonly_image
, image_count
);
752 die("Unable to load all disk images.");
754 virtio_blk__init_all(kvm
);
757 printf(" # kvm run -k %s -m %Lu -c %d --name %s\n", kernel_filename
, ram_size
/ 1024 / 1024, nrcpus
, guest_name
);
759 if (!kvm__load_kernel(kvm
, kernel_filename
, initrd_filename
,
760 real_cmdline
, vidmode
))
761 die("unable to load kernel %s", kernel_filename
);
763 kvm
->vmlinux
= vmlinux_filename
;
765 ioport__setup_legacy();
769 serial8250__init(kvm
);
773 if (active_console
== CONSOLE_VIRTIO
)
774 virtio_console__init(kvm
);
777 virtio_rng__init(kvm
);
780 virtio_bln__init(kvm
);
783 network
= DEFAULT_NETWORK
;
785 virtio_9p__init(kvm
);
787 if (strncmp(network
, "none", 4)) {
788 net_params
.guest_ip
= guest_ip
;
789 net_params
.host_ip
= host_ip
;
790 net_params
.kvm
= kvm
;
791 net_params
.script
= script
;
792 sscanf(guest_mac
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
793 net_params
.guest_mac
,
794 net_params
.guest_mac
+1,
795 net_params
.guest_mac
+2,
796 net_params
.guest_mac
+3,
797 net_params
.guest_mac
+4,
798 net_params
.guest_mac
+5);
799 sscanf(host_mac
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
801 net_params
.host_mac
+1,
802 net_params
.host_mac
+2,
803 net_params
.host_mac
+3,
804 net_params
.host_mac
+4,
805 net_params
.host_mac
+5);
807 if (!strncmp(network
, "user", 4))
808 net_params
.mode
= NET_MODE_USER
;
809 else if (!strncmp(network
, "tap", 3))
810 net_params
.mode
= NET_MODE_TAP
;
812 die("Unkown network mode %s, please use -network user, tap, none", network
);
813 virtio_net__init(&net_params
);
816 kvm__start_timer(kvm
);
818 kvm__setup_bios(kvm
);
820 for (i
= 0; i
< nrcpus
; i
++) {
821 kvm_cpus
[i
] = kvm_cpu__init(kvm
, i
);
823 die("unable to initialize KVM VCPU");
830 pci_shmem__init(kvm
);
833 fb
= vesa__init(kvm
);
847 thread_pool__init(nr_online_cpus
);
850 for (i
= 0; i
< nrcpus
; i
++) {
851 if (pthread_create(&kvm_cpus
[i
]->thread
, NULL
, kvm_cpu_thread
, kvm_cpus
[i
]) != 0)
852 die("unable to create KVM VCPU thread");
855 /* Only VCPU #0 is going to exit by itself when shutting down */
856 if (pthread_join(kvm_cpus
[0]->thread
, &ret
) != 0)
859 for (i
= 1; i
< nrcpus
; i
++) {
860 if (kvm_cpus
[i
]->is_running
) {
861 pthread_kill(kvm_cpus
[i
]->thread
, SIGKVMEXIT
);
862 if (pthread_join(kvm_cpus
[i
]->thread
, &ret
) != 0)
869 compat__print_all_messages();
873 virtio_blk__delete_all(kvm
);
874 virtio_rng__delete_all(kvm
);
876 disk_image__close_all(kvm
->disks
, image_count
);
880 printf("\n # KVM session ended normally.\n");