xref: /kvmtool/builtin-run.c (revision cac9e8fd0152dbcccd2ba7edeef4fda79778b445)
1 #include "kvm/builtin-run.h"
2 
3 #include "kvm/builtin-setup.h"
4 #include "kvm/virtio-balloon.h"
5 #include "kvm/virtio-console.h"
6 #include "kvm/parse-options.h"
7 #include "kvm/8250-serial.h"
8 #include "kvm/framebuffer.h"
9 #include "kvm/disk-image.h"
10 #include "kvm/threadpool.h"
11 #include "kvm/virtio-scsi.h"
12 #include "kvm/virtio-blk.h"
13 #include "kvm/virtio-net.h"
14 #include "kvm/virtio-rng.h"
15 #include "kvm/ioeventfd.h"
16 #include "kvm/virtio-9p.h"
17 #include "kvm/barrier.h"
18 #include "kvm/kvm-cpu.h"
19 #include "kvm/ioport.h"
20 #include "kvm/symbol.h"
21 #include "kvm/i8042.h"
22 #include "kvm/mutex.h"
23 #include "kvm/term.h"
24 #include "kvm/util.h"
25 #include "kvm/strbuf.h"
26 #include "kvm/vesa.h"
27 #include "kvm/irq.h"
28 #include "kvm/kvm.h"
29 #include "kvm/pci.h"
30 #include "kvm/rtc.h"
31 #include "kvm/sdl.h"
32 #include "kvm/vnc.h"
33 #include "kvm/guest_compat.h"
34 #include "kvm/pci-shmem.h"
35 #include "kvm/kvm-ipc.h"
36 #include "kvm/builtin-debug.h"
37 
38 #include <linux/types.h>
39 #include <linux/err.h>
40 
41 #include <sys/utsname.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <termios.h>
45 #include <signal.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <ctype.h>
50 #include <stdio.h>
51 
52 #define MB_SHIFT		(20)
53 #define KB_SHIFT		(10)
54 #define GB_SHIFT		(30)
55 
56 struct kvm *kvm;
57 __thread struct kvm_cpu *current_kvm_cpu;
58 
59 static int  kvm_run_wrapper;
60 
61 bool do_debug_print = false;
62 
63 extern char _binary_guest_init_start;
64 extern char _binary_guest_init_size;
65 
66 static const char * const run_usage[] = {
67 	"lkvm run [<options>] [<kernel image>]",
68 	NULL
69 };
70 
71 enum {
72 	KVM_RUN_DEFAULT,
73 	KVM_RUN_SANDBOX,
74 };
75 
76 static int img_name_parser(const struct option *opt, const char *arg, int unset)
77 {
78 	char path[PATH_MAX];
79 	struct stat st;
80 
81 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
82 
83 	if ((stat(arg, &st) == 0 && S_ISDIR(st.st_mode)) ||
84 	   (stat(path, &st) == 0 && S_ISDIR(st.st_mode)))
85 		return virtio_9p_img_name_parser(opt, arg, unset);
86 	return disk_img_name_parser(opt, arg, unset);
87 }
88 
89 void kvm_run_set_wrapper_sandbox(void)
90 {
91 	kvm_run_wrapper = KVM_RUN_SANDBOX;
92 }
93 
94 #define BUILD_OPTIONS(name, cfg, kvm)					\
95 	struct option name[] = {					\
96 	OPT_GROUP("Basic options:"),					\
97 	OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name",	\
98 			"A name for the guest"),			\
99 	OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"),	\
100 	OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory size\
101 		in MiB."),						\
102 	OPT_CALLBACK('\0', "shmem", NULL,				\
103 		     "[pci:]<addr>:<size>[:handle=<handle>][:create]",	\
104 		     "Share host shmem with guest via pci device",	\
105 		     shmem_parser, NULL),				\
106 	OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk 	\
107 			image or rootfs directory", img_name_parser,	\
108 			kvm),						\
109 	OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio	\
110 			balloon"),					\
111 	OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\
112 	OPT_BOOLEAN('\0', "sdl", &(cfg)->sdl, "Enable SDL framebuffer"),\
113 	OPT_BOOLEAN('\0', "rng", &(cfg)->virtio_rng, "Enable virtio Random\
114 			Number Generator"),				\
115 	OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name",		\
116 		     "Enable virtio 9p to share files between host and	\
117 		     guest", virtio_9p_rootdir_parser, kvm),		\
118 	OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or	\
119 			hv", "Console to use"),				\
120 	OPT_STRING('\0', "dev", &(cfg)->dev, "device_file",		\
121 			"KVM device file"),				\
122 	OPT_CALLBACK('\0', "tty", NULL, "tty id",			\
123 		     "Remap guest TTY into a pty on the host",		\
124 		     tty_parser, NULL),					\
125 	OPT_STRING('\0', "sandbox", &(cfg)->sandbox, "script",		\
126 			"Run this script when booting into custom	\
127 			rootfs"),					\
128 	OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path",	\
129 			"Hugetlbfs path"),				\
130 									\
131 	OPT_GROUP("Kernel options:"),					\
132 	OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel",	\
133 			"Kernel to boot in virtual machine"),		\
134 	OPT_STRING('i', "initrd", &(cfg)->initrd_filename, "initrd",	\
135 			"Initial RAM disk image"),			\
136 	OPT_STRING('p', "params", &(cfg)->kernel_cmdline, "params",	\
137 			"Kernel command line arguments"),		\
138 	OPT_STRING('f', "firmware", &(cfg)->firmware_filename, "firmware",\
139 			"Firmware image to boot in virtual machine"),	\
140 									\
141 	OPT_GROUP("Networking options:"),				\
142 	OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params",	\
143 		     "Create a new guest NIC",				\
144 		     netdev_parser, NULL, kvm),				\
145 	OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel DHCP\
146 			in rootfs mode"),				\
147 									\
148 	OPT_GROUP("BIOS options:"),					\
149 	OPT_INTEGER('\0', "vidmode", &(cfg)->vidmode,			\
150 		    "Video mode"),					\
151 									\
152 	OPT_GROUP("Debug options:"),					\
153 	OPT_BOOLEAN('\0', "debug", &do_debug_print,			\
154 			"Enable debug messages"),			\
155 	OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step,	\
156 			"Enable single stepping"),			\
157 	OPT_BOOLEAN('\0', "debug-ioport", &(cfg)->ioport_debug,		\
158 			"Enable ioport debugging"),			\
159 	OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug,		\
160 			"Enable MMIO debugging"),			\
161 	OPT_INTEGER('\0', "debug-iodelay", &(cfg)->debug_iodelay,	\
162 			"Delay IO by millisecond"),			\
163 	OPT_END()							\
164 	};
165 
166 static void handle_sigalrm(int sig)
167 {
168 	kvm__arch_periodic_poll(kvm);
169 }
170 
171 static void *kvm_cpu_thread(void *arg)
172 {
173 	current_kvm_cpu		= arg;
174 
175 	if (kvm_cpu__start(current_kvm_cpu))
176 		goto panic_kvm;
177 
178 	return (void *) (intptr_t) 0;
179 
180 panic_kvm:
181 	fprintf(stderr, "KVM exit reason: %u (\"%s\")\n",
182 		current_kvm_cpu->kvm_run->exit_reason,
183 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
184 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
185 		fprintf(stderr, "KVM exit code: 0x%Lu\n",
186 			current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
187 
188 	kvm_cpu__set_debug_fd(STDOUT_FILENO);
189 	kvm_cpu__show_registers(current_kvm_cpu);
190 	kvm_cpu__show_code(current_kvm_cpu);
191 	kvm_cpu__show_page_tables(current_kvm_cpu);
192 
193 	return (void *) (intptr_t) 1;
194 }
195 
196 static char kernel[PATH_MAX];
197 
198 static const char *host_kernels[] = {
199 	"/boot/vmlinuz",
200 	"/boot/bzImage",
201 	NULL
202 };
203 
204 static const char *default_kernels[] = {
205 	"./bzImage",
206 	"arch/" BUILD_ARCH "/boot/bzImage",
207 	"../../arch/" BUILD_ARCH "/boot/bzImage",
208 	NULL
209 };
210 
211 static const char *default_vmlinux[] = {
212 	"vmlinux",
213 	"../../../vmlinux",
214 	"../../vmlinux",
215 	NULL
216 };
217 
218 static void kernel_usage_with_options(void)
219 {
220 	const char **k;
221 	struct utsname uts;
222 
223 	fprintf(stderr, "Fatal: could not find default kernel image in:\n");
224 	k = &default_kernels[0];
225 	while (*k) {
226 		fprintf(stderr, "\t%s\n", *k);
227 		k++;
228 	}
229 
230 	if (uname(&uts) < 0)
231 		return;
232 
233 	k = &host_kernels[0];
234 	while (*k) {
235 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
236 			return;
237 		fprintf(stderr, "\t%s\n", kernel);
238 		k++;
239 	}
240 	fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n",
241 		KVM_BINARY_NAME);
242 }
243 
244 static u64 host_ram_size(void)
245 {
246 	long page_size;
247 	long nr_pages;
248 
249 	nr_pages	= sysconf(_SC_PHYS_PAGES);
250 	if (nr_pages < 0) {
251 		pr_warning("sysconf(_SC_PHYS_PAGES) failed");
252 		return 0;
253 	}
254 
255 	page_size	= sysconf(_SC_PAGE_SIZE);
256 	if (page_size < 0) {
257 		pr_warning("sysconf(_SC_PAGE_SIZE) failed");
258 		return 0;
259 	}
260 
261 	return (nr_pages * page_size) >> MB_SHIFT;
262 }
263 
264 /*
265  * If user didn't specify how much memory it wants to allocate for the guest,
266  * avoid filling the whole host RAM.
267  */
268 #define RAM_SIZE_RATIO		0.8
269 
270 static u64 get_ram_size(int nr_cpus)
271 {
272 	u64 available;
273 	u64 ram_size;
274 
275 	ram_size	= 64 * (nr_cpus + 3);
276 
277 	available	= host_ram_size() * RAM_SIZE_RATIO;
278 	if (!available)
279 		available = MIN_RAM_SIZE_MB;
280 
281 	if (ram_size > available)
282 		ram_size	= available;
283 
284 	return ram_size;
285 }
286 
287 static const char *find_kernel(void)
288 {
289 	const char **k;
290 	struct stat st;
291 	struct utsname uts;
292 
293 	k = &default_kernels[0];
294 	while (*k) {
295 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
296 			k++;
297 			continue;
298 		}
299 		strncpy(kernel, *k, PATH_MAX);
300 		return kernel;
301 	}
302 
303 	if (uname(&uts) < 0)
304 		return NULL;
305 
306 	k = &host_kernels[0];
307 	while (*k) {
308 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
309 			return NULL;
310 
311 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
312 			k++;
313 			continue;
314 		}
315 		return kernel;
316 
317 	}
318 	return NULL;
319 }
320 
321 static const char *find_vmlinux(void)
322 {
323 	const char **vmlinux;
324 
325 	vmlinux = &default_vmlinux[0];
326 	while (*vmlinux) {
327 		struct stat st;
328 
329 		if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
330 			vmlinux++;
331 			continue;
332 		}
333 		return *vmlinux;
334 	}
335 	return NULL;
336 }
337 
338 void kvm_run_help(void)
339 {
340 	BUILD_OPTIONS(options, &kvm->cfg, kvm);
341 	usage_with_options(run_usage, options);
342 }
343 
344 static int kvm_setup_guest_init(void)
345 {
346 	const char *rootfs = kvm->cfg.custom_rootfs_name;
347 	char tmp[PATH_MAX];
348 	size_t size;
349 	int fd, ret;
350 	char *data;
351 
352 	/* Setup /virt/init */
353 	size = (size_t)&_binary_guest_init_size;
354 	data = (char *)&_binary_guest_init_start;
355 	snprintf(tmp, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), rootfs);
356 	remove(tmp);
357 	fd = open(tmp, O_CREAT | O_WRONLY, 0755);
358 	if (fd < 0)
359 		die("Fail to setup %s", tmp);
360 	ret = xwrite(fd, data, size);
361 	if (ret < 0)
362 		die("Fail to setup %s", tmp);
363 	close(fd);
364 
365 	return 0;
366 }
367 
368 static int kvm_run_set_sandbox(void)
369 {
370 	const char *guestfs_name = kvm->cfg.custom_rootfs_name;
371 	char path[PATH_MAX], script[PATH_MAX], *tmp;
372 
373 	snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name);
374 
375 	remove(path);
376 
377 	if (kvm->cfg.sandbox == NULL)
378 		return 0;
379 
380 	tmp = realpath(kvm->cfg.sandbox, NULL);
381 	if (tmp == NULL)
382 		return -ENOMEM;
383 
384 	snprintf(script, PATH_MAX, "/host/%s", tmp);
385 	free(tmp);
386 
387 	return symlink(script, path);
388 }
389 
390 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg)
391 {
392 	const char *single_quote;
393 
394 	if (!*arg) { /* zero length string */
395 		if (write(fd, "''", 2) <= 0)
396 			die("Failed writing sandbox script");
397 		return;
398 	}
399 
400 	while (*arg) {
401 		single_quote = strchrnul(arg, '\'');
402 
403 		/* write non-single-quote string as #('string') */
404 		if (arg != single_quote) {
405 			if (write(fd, "'", 1) <= 0 ||
406 			    write(fd, arg, single_quote - arg) <= 0 ||
407 			    write(fd, "'", 1) <= 0)
408 				die("Failed writing sandbox script");
409 		}
410 
411 		/* write single quote as #("'") */
412 		if (*single_quote) {
413 			if (write(fd, "\"'\"", 3) <= 0)
414 				die("Failed writing sandbox script");
415 		} else
416 			break;
417 
418 		arg = single_quote + 1;
419 	}
420 }
421 
422 static void resolve_program(const char *src, char *dst, size_t len)
423 {
424 	struct stat st;
425 	int err;
426 
427 	err = stat(src, &st);
428 
429 	if (!err && S_ISREG(st.st_mode)) {
430 		char resolved_path[PATH_MAX];
431 
432 		if (!realpath(src, resolved_path))
433 			die("Unable to resolve program %s: %s\n", src, strerror(errno));
434 
435 		snprintf(dst, len, "/host%s", resolved_path);
436 	} else
437 		strncpy(dst, src, len);
438 }
439 
440 static void kvm_run_write_sandbox_cmd(const char **argv, int argc)
441 {
442 	const char script_hdr[] = "#! /bin/bash\n\n";
443 	char program[PATH_MAX];
444 	int fd;
445 
446 	remove(kvm->cfg.sandbox);
447 
448 	fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777);
449 	if (fd < 0)
450 		die("Failed creating sandbox script");
451 
452 	if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0)
453 		die("Failed writing sandbox script");
454 
455 	resolve_program(argv[0], program, PATH_MAX);
456 	kvm_write_sandbox_cmd_exactly(fd, program);
457 
458 	argv++;
459 	argc--;
460 
461 	while (argc) {
462 		if (write(fd, " ", 1) <= 0)
463 			die("Failed writing sandbox script");
464 
465 		kvm_write_sandbox_cmd_exactly(fd, argv[0]);
466 		argv++;
467 		argc--;
468 	}
469 	if (write(fd, "\n", 1) <= 0)
470 		die("Failed writing sandbox script");
471 
472 	close(fd);
473 }
474 
475 static int kvm_cmd_run_init(int argc, const char **argv)
476 {
477 	static char real_cmdline[2048], default_name[20];
478 	unsigned int nr_online_cpus;
479 	int r;
480 
481 	kvm = kvm__new();
482 	if (IS_ERR(kvm))
483 		return PTR_ERR(kvm);
484 
485 	signal(SIGALRM, handle_sigalrm);
486 
487 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
488 	kvm->cfg.custom_rootfs_name = "default";
489 
490 	while (argc != 0) {
491 		BUILD_OPTIONS(options, &kvm->cfg, kvm);
492 		argc = parse_options(argc, argv, options, run_usage,
493 				PARSE_OPT_STOP_AT_NON_OPTION |
494 				PARSE_OPT_KEEP_DASHDASH);
495 		if (argc != 0) {
496 			/* Cusrom options, should have been handled elsewhere */
497 			if (strcmp(argv[0], "--") == 0) {
498 				if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
499 					kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
500 					kvm_run_write_sandbox_cmd(argv+1, argc-1);
501 					break;
502 				}
503 			}
504 
505 			if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) ||
506 				(kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) {
507 				fprintf(stderr, "Cannot handle parameter: "
508 						"%s\n", argv[0]);
509 				usage_with_options(run_usage, options);
510 				free(kvm);
511 				return -EINVAL;
512 			}
513 			if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
514 				/*
515 				 * first unhandled parameter is treated as
516 				 * sandbox command
517 				 */
518 				kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
519 				kvm_run_write_sandbox_cmd(argv, argc);
520 			} else {
521 				/*
522 				 * first unhandled parameter is treated as a kernel
523 				 * image
524 				 */
525 				kvm->cfg.kernel_filename = argv[0];
526 			}
527 			argv++;
528 			argc--;
529 		}
530 
531 	}
532 
533 	kvm->nr_disks = kvm->cfg.image_count;
534 
535 	if (!kvm->cfg.kernel_filename)
536 		kvm->cfg.kernel_filename = find_kernel();
537 
538 	if (!kvm->cfg.kernel_filename) {
539 		kernel_usage_with_options();
540 		return -EINVAL;
541 	}
542 
543 	kvm->cfg.vmlinux_filename = find_vmlinux();
544 	kvm->vmlinux = kvm->cfg.vmlinux_filename;
545 
546 	if (kvm->cfg.nrcpus == 0)
547 		kvm->cfg.nrcpus = nr_online_cpus;
548 
549 	if (!kvm->cfg.ram_size)
550 		kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus);
551 
552 	if (kvm->cfg.ram_size < MIN_RAM_SIZE_MB)
553 		die("Not enough memory specified: %lluMB (min %lluMB)", kvm->cfg.ram_size, MIN_RAM_SIZE_MB);
554 
555 	if (kvm->cfg.ram_size > host_ram_size())
556 		pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", kvm->cfg.ram_size, host_ram_size());
557 
558 	kvm->cfg.ram_size <<= MB_SHIFT;
559 
560 	if (!kvm->cfg.dev)
561 		kvm->cfg.dev = DEFAULT_KVM_DEV;
562 
563 	if (!kvm->cfg.console)
564 		kvm->cfg.console = DEFAULT_CONSOLE;
565 
566 	if (!strncmp(kvm->cfg.console, "virtio", 6))
567 		kvm->cfg.active_console  = CONSOLE_VIRTIO;
568 	else if (!strncmp(kvm->cfg.console, "serial", 6))
569 		kvm->cfg.active_console  = CONSOLE_8250;
570 	else if (!strncmp(kvm->cfg.console, "hv", 2))
571 		kvm->cfg.active_console = CONSOLE_HV;
572 	else
573 		pr_warning("No console!");
574 
575 	if (!kvm->cfg.host_ip)
576 		kvm->cfg.host_ip = DEFAULT_HOST_ADDR;
577 
578 	if (!kvm->cfg.guest_ip)
579 		kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR;
580 
581 	if (!kvm->cfg.guest_mac)
582 		kvm->cfg.guest_mac = DEFAULT_GUEST_MAC;
583 
584 	if (!kvm->cfg.host_mac)
585 		kvm->cfg.host_mac = DEFAULT_HOST_MAC;
586 
587 	if (!kvm->cfg.script)
588 		kvm->cfg.script = DEFAULT_SCRIPT;
589 
590 	if (!kvm->cfg.vnc && !kvm->cfg.sdl)
591 		kvm->cfg.vidmode = -1;
592 
593 	if (!kvm->cfg.network)
594                 kvm->cfg.network = DEFAULT_NETWORK;
595 
596 	memset(real_cmdline, 0, sizeof(real_cmdline));
597 	kvm__arch_set_cmdline(real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl);
598 
599 	if (strlen(real_cmdline) > 0)
600 		strcat(real_cmdline, " ");
601 
602 	if (kvm->cfg.kernel_cmdline)
603 		strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline));
604 
605 	if (!kvm->cfg.guest_name) {
606 		if (kvm->cfg.custom_rootfs) {
607 			kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name;
608 		} else {
609 			sprintf(default_name, "guest-%u", getpid());
610 			kvm->cfg.guest_name = default_name;
611 		}
612 	}
613 
614 	if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) {
615 		char tmp[PATH_MAX];
616 
617 		kvm_setup_create_new(kvm->cfg.custom_rootfs_name);
618 		kvm_setup_resolv(kvm->cfg.custom_rootfs_name);
619 
620 		snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default");
621 		if (virtio_9p__register(kvm, tmp, "/dev/root") < 0)
622 			die("Unable to initialize virtio 9p");
623 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
624 			die("Unable to initialize virtio 9p");
625 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
626 	}
627 
628 	if (kvm->cfg.using_rootfs) {
629 		strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
630 		if (kvm->cfg.custom_rootfs) {
631 			kvm_run_set_sandbox();
632 
633 			strcat(real_cmdline, " init=/virt/init");
634 
635 			if (!kvm->cfg.no_dhcp)
636 				strcat(real_cmdline, "  ip=dhcp");
637 			if (kvm_setup_guest_init())
638 				die("Failed to setup init for guest.");
639 		}
640 	} else if (!strstr(real_cmdline, "root=")) {
641 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
642 	}
643 
644 	kvm->cfg.real_cmdline = real_cmdline;
645 
646 	printf("  # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
647 		kvm->cfg.kernel_filename, kvm->cfg.ram_size / 1024 / 1024, kvm->cfg.nrcpus, kvm->cfg.guest_name);
648 
649 	r = kvm__init(kvm);
650 	if (r)
651 		goto fail;
652 
653 	r = term_init(kvm);
654 	if (r < 0) {
655 		pr_err("term_init() failed with error %d\n", r);
656 		goto fail;
657 	}
658 
659 
660 	r = ioeventfd__init(kvm);
661 	if (r < 0) {
662 		pr_err("ioeventfd__init() failed with error %d\n", r);
663 		goto fail;
664 	}
665 
666 	r = kvm_cpu__init(kvm);
667 	if (r < 0) {
668 		pr_err("kvm_cpu__init() failed with error %d\n", r);
669 		goto fail;
670 	}
671 
672 	r = irq__init(kvm);
673 	if (r < 0) {
674 		pr_err("irq__init() failed with error %d\n", r);
675 		goto fail;
676 	}
677 
678 	r = pci__init(kvm);
679 	if (r < 0) {
680 		pr_err("pci__init() failed with error %d\n", r);
681 		goto fail;
682 	}
683 
684 	r = ioport__init(kvm);
685 	if (r < 0) {
686 		pr_err("ioport__init() failed with error %d\n", r);
687 		goto fail;
688 	}
689 
690 	r = disk_image__init(kvm);
691 	if (r < 0) {
692 		pr_err("disk_image__init() failed with error %d\n", r);
693 		goto fail;
694 	}
695 
696 	r = symbol_init(kvm);
697 	if (r < 0)
698 		pr_debug("symbol_init() failed with error %d\n", r);
699 
700 	r = rtc__init(kvm);
701 	if (r < 0) {
702 		pr_err("rtc__init() failed with error %d\n", r);
703 		goto fail;
704 	}
705 
706 	r = serial8250__init(kvm);
707 	if (r < 0) {
708 		pr_err("serial__init() failed with error %d\n", r);
709 		goto fail;
710 	}
711 
712 	r = virtio_blk__init(kvm);
713 	if (r < 0) {
714 		pr_err("virtio_blk__init() failed with error %d\n", r);
715 		goto fail;
716 	}
717 
718 	r = virtio_scsi_init(kvm);
719 	if (r < 0) {
720 		pr_err("virtio_scsi_init() failed with error %d\n", r);
721 		goto fail;
722 	}
723 
724 	r = virtio_console__init(kvm);
725 	if (r < 0) {
726 		pr_err("virtio_console__init() failed with error %d\n", r);
727 		goto fail;
728 	}
729 
730 	r = virtio_rng__init(kvm);
731 	if (r < 0) {
732 		pr_err("virtio_rng__init() failed with error %d\n", r);
733 		goto fail;
734 	}
735 
736 	r = virtio_bln__init(kvm);
737 	if (r < 0) {
738 		pr_err("virtio_rng__init() failed with error %d\n", r);
739 		goto fail;
740 	}
741 
742 	r = virtio_9p__init(kvm);
743 	if (r < 0) {
744 		pr_err("virtio_9p__init() failed with error %d\n", r);
745 		goto fail;
746 	}
747 
748 	r = virtio_net__init(kvm);
749 	if (r < 0) {
750 		pr_err("virtio_net__init() failed with error %d\n", r);
751 		goto fail;
752 	}
753 
754 	r = kbd__init(kvm);
755 	if (r < 0) {
756 		pr_err("kbd__init() failed with error %d\n", r);
757 		goto fail;
758 	}
759 
760 	r = pci_shmem__init(kvm);
761 	if (r < 0) {
762 		pr_err("pci_shmem__init() failed with error %d\n", r);
763 		goto fail;
764 	}
765 
766 	r = vnc__init(kvm);
767 	if (r < 0) {
768 		pr_err("vnc__init() failed with error %d\n", r);
769 		goto fail;
770 	}
771 
772 	r = sdl__init(kvm);
773 	if (r < 0) {
774 		pr_err("sdl__init() failed with error %d\n", r);
775 		goto fail;
776 	}
777 
778 	r = fb__init(kvm);
779 	if (r < 0) {
780 		pr_err("fb__init() failed with error %d\n", r);
781 		goto fail;
782 	}
783 
784 	/*
785 	 * Device init all done; firmware init must
786 	 * come after this (it may set up device trees etc.)
787 	 */
788 
789 	r = kvm_timer__init(kvm);
790 	if (r < 0) {
791 		pr_err("kvm_timer__init() failed with error %d\n", r);
792 		goto fail;
793 	}
794 
795 	r = thread_pool__init(kvm);
796 	if (r < 0) {
797 		pr_err("thread_pool__init() failed with error %d\n", r);
798 		goto fail;
799 	}
800 
801 fail:
802 	return r;
803 }
804 
805 static int kvm_cmd_run_work(void)
806 {
807 	int i;
808 	void *ret = NULL;
809 
810 	for (i = 0; i < kvm->nrcpus; i++) {
811 		if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0)
812 			die("unable to create KVM VCPU thread");
813 	}
814 
815 	/* Only VCPU #0 is going to exit by itself when shutting down */
816 	return pthread_join(kvm->cpus[0]->thread, &ret);
817 }
818 
819 static void kvm_cmd_run_exit(int guest_ret)
820 {
821 	int r = 0;
822 
823 	compat__print_all_messages();
824 
825 	r = kvm_cpu__exit(kvm);
826 	if (r < 0)
827 		pr_warning("kvm_cpu__exit() failed with error %d\n", r);
828 
829 	r = symbol_exit(kvm);
830 	if (r < 0)
831 		pr_warning("symbol_exit() failed with error %d\n", r);
832 
833 	r = irq__exit(kvm);
834 	if (r < 0)
835 		pr_warning("irq__exit() failed with error %d\n", r);
836 
837 	r = kvm_timer__exit(kvm);
838 	if (r < 0)
839 		pr_warning("kvm_timer__exit() failed with error %d\n", r);
840 
841 	r = fb__exit(kvm);
842 	if (r < 0)
843 		pr_warning("kvm_timer__exit() failed with error %d\n", r);
844 
845 	r = virtio_net__exit(kvm);
846 	if (r < 0)
847 		pr_warning("virtio_net__exit() failed with error %d\n", r);
848 
849 	r = virtio_scsi_exit(kvm);
850 	if (r < 0)
851 		pr_warning("virtio_scsi_exit() failed with error %d\n", r);
852 
853 	r = virtio_blk__exit(kvm);
854 	if (r < 0)
855 		pr_warning("virtio_blk__exit() failed with error %d\n", r);
856 
857 	r = virtio_rng__exit(kvm);
858 	if (r < 0)
859 		pr_warning("virtio_rng__exit() failed with error %d\n", r);
860 
861 	r = virtio_bln__exit(kvm);
862 	if (r < 0)
863 		pr_warning("virtio_bln__exit() failed with error %d\n", r);
864 
865 	r = virtio_console__exit(kvm);
866 	if (r < 0)
867 		pr_warning("virtio_console__exit() failed with error %d\n", r);
868 
869 	r = pci_shmem__exit(kvm);
870 	if (r < 0)
871 		pr_warning("pci_shmem__exit() failed with error %d\n", r);
872 
873 	r = disk_image__exit(kvm);
874 	if (r < 0)
875 		pr_warning("disk_image__exit() failed with error %d\n", r);
876 
877 	r = serial8250__exit(kvm);
878 	if (r < 0)
879 		pr_warning("serial8250__exit() failed with error %d\n", r);
880 
881 	r = rtc__exit(kvm);
882 	if (r < 0)
883 		pr_warning("rtc__exit() failed with error %d\n", r);
884 
885 	r = kvm__arch_free_firmware(kvm);
886 	if (r < 0)
887 		pr_warning("kvm__arch_free_firmware() failed with error %d\n", r);
888 
889 	r = ioport__exit(kvm);
890 	if (r < 0)
891 		pr_warning("ioport__exit() failed with error %d\n", r);
892 
893 	r = ioeventfd__exit(kvm);
894 	if (r < 0)
895 		pr_warning("ioeventfd__exit() failed with error %d\n", r);
896 
897 	r = pci__exit(kvm);
898 	if (r < 0)
899 		pr_warning("pci__exit() failed with error %d\n", r);
900 
901 	r = term_exit(kvm);
902 	if (r < 0)
903 		pr_warning("pci__exit() failed with error %d\n", r);
904 
905 	r = thread_pool__exit(kvm);
906 	if (r < 0)
907 		pr_warning("thread_pool__exit() failed with error %d\n", r);
908 
909 	r = kvm__exit(kvm);
910 	if (r < 0)
911 		pr_warning("pci__exit() failed with error %d\n", r);
912 
913 	if (guest_ret == 0)
914 		printf("\n  # KVM session ended normally.\n");
915 }
916 
917 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
918 {
919 	int r, ret = -EFAULT;
920 
921 	r = kvm_cmd_run_init(argc, argv);
922 	if (r < 0)
923 		return r;
924 
925 	ret = kvm_cmd_run_work();
926 	kvm_cmd_run_exit(ret);
927 
928 	return ret;
929 }
930