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