xref: /kvmtool/builtin-run.c (revision e830cce7780a0d008fd00f6e5daba6dcc5e297d4)
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 struct kvm_cpu **kvm_cpus;
58 __thread struct kvm_cpu *current_kvm_cpu;
59 
60 static int  kvm_run_wrapper;
61 extern int  active_console;
62 extern int  debug_iodelay;
63 
64 bool do_debug_print = false;
65 
66 static int nrcpus;
67 static int vidmode = -1;
68 
69 extern char _binary_guest_init_start;
70 extern char _binary_guest_init_size;
71 
72 static const char * const run_usage[] = {
73 	"lkvm run [<options>] [<kernel image>]",
74 	NULL
75 };
76 
77 enum {
78 	KVM_RUN_DEFAULT,
79 	KVM_RUN_SANDBOX,
80 };
81 
82 void kvm_run_set_wrapper_sandbox(void)
83 {
84 	kvm_run_wrapper = KVM_RUN_SANDBOX;
85 }
86 
87 static int img_name_parser(const struct option *opt, const char *arg, int unset)
88 {
89 	char path[PATH_MAX];
90 	const char *cur;
91 	struct stat st;
92 	char *sep;
93 
94 	if (stat(arg, &st) == 0 &&
95 	    S_ISDIR(st.st_mode)) {
96 		char tmp[PATH_MAX];
97 
98 		if (kvm->cfg.using_rootfs)
99 			die("Please use only one rootfs directory atmost");
100 
101 		if (realpath(arg, tmp) == 0 ||
102 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
103 			die("Unable to initialize virtio 9p");
104 		kvm->cfg.using_rootfs = 1;
105 		return 0;
106 	}
107 
108 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
109 
110 	if (stat(path, &st) == 0 &&
111 	    S_ISDIR(st.st_mode)) {
112 		char tmp[PATH_MAX];
113 
114 		if (kvm->cfg.using_rootfs)
115 			die("Please use only one rootfs directory atmost");
116 
117 		if (realpath(path, tmp) == 0 ||
118 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
119 			die("Unable to initialize virtio 9p");
120 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
121 			die("Unable to initialize virtio 9p");
122 		kvm_setup_resolv(arg);
123 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
124 		kvm->cfg.custom_rootfs_name = arg;
125 		return 0;
126 	}
127 
128 	if (kvm->cfg.image_count >= MAX_DISK_IMAGES)
129 		die("Currently only 4 images are supported");
130 
131 	kvm->cfg.disk_image[kvm->cfg.image_count].filename = arg;
132 	cur = arg;
133 
134 	if (strncmp(arg, "scsi:", 5) == 0) {
135 		sep = strstr(arg, ":");
136 		if (sep)
137 			kvm->cfg.disk_image[kvm->cfg.image_count].wwpn = sep + 1;
138 		sep = strstr(sep + 1, ":");
139 		if (sep) {
140 			*sep = 0;
141 			kvm->cfg.disk_image[kvm->cfg.image_count].tpgt = sep + 1;
142 		}
143 		cur = sep + 1;
144 	}
145 
146 	do {
147 		sep = strstr(cur, ",");
148 		if (sep) {
149 			if (strncmp(sep + 1, "ro", 2) == 0)
150 				kvm->cfg.disk_image[kvm->cfg.image_count].readonly = true;
151 			else if (strncmp(sep + 1, "direct", 6) == 0)
152 				kvm->cfg.disk_image[kvm->cfg.image_count].direct = true;
153 			*sep = 0;
154 			cur = sep + 1;
155 		}
156 	} while (sep);
157 
158 	kvm->cfg.image_count++;
159 
160 	return 0;
161 }
162 
163 static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset)
164 {
165 	char *tag_name;
166 	char tmp[PATH_MAX];
167 
168 	/*
169 	 * 9p dir can be of the form dirname,tag_name or
170 	 * just dirname. In the later case we use the
171 	 * default tag name
172 	 */
173 	tag_name = strstr(arg, ",");
174 	if (tag_name) {
175 		*tag_name = '\0';
176 		tag_name++;
177 	}
178 	if (realpath(arg, tmp)) {
179 		if (virtio_9p__register(kvm, tmp, tag_name) < 0)
180 			die("Unable to initialize virtio 9p");
181 	} else
182 		die("Failed resolving 9p path");
183 	return 0;
184 }
185 
186 static int tty_parser(const struct option *opt, const char *arg, int unset)
187 {
188 	int tty = atoi(arg);
189 
190 	term_set_tty(tty);
191 
192 	return 0;
193 }
194 
195 static inline void str_to_mac(const char *str, char *mac)
196 {
197 	sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
198 		mac, mac+1, mac+2, mac+3, mac+4, mac+5);
199 }
200 static int set_net_param(struct virtio_net_params *p, const char *param,
201 				const char *val)
202 {
203 	if (strcmp(param, "guest_mac") == 0) {
204 		str_to_mac(val, p->guest_mac);
205 	} else if (strcmp(param, "mode") == 0) {
206 		if (!strncmp(val, "user", 4)) {
207 			int i;
208 
209 			for (i = 0; i < kvm->cfg.num_net_devices; i++)
210 				if (kvm->cfg.net_params[i].mode == NET_MODE_USER)
211 					die("Only one usermode network device allowed at a time");
212 			p->mode = NET_MODE_USER;
213 		} else if (!strncmp(val, "tap", 3)) {
214 			p->mode = NET_MODE_TAP;
215 		} else if (!strncmp(val, "none", 4)) {
216 			kvm->cfg.no_net = 1;
217 			return -1;
218 		} else
219 			die("Unknown network mode %s, please use user, tap or none", kvm->cfg.network);
220 	} else if (strcmp(param, "script") == 0) {
221 		p->script = strdup(val);
222 	} else if (strcmp(param, "guest_ip") == 0) {
223 		p->guest_ip = strdup(val);
224 	} else if (strcmp(param, "host_ip") == 0) {
225 		p->host_ip = strdup(val);
226 	} else if (strcmp(param, "trans") == 0) {
227 		p->trans = strdup(val);
228 	} else if (strcmp(param, "vhost") == 0) {
229 		p->vhost = atoi(val);
230 	} else if (strcmp(param, "fd") == 0) {
231 		p->fd = atoi(val);
232 	} else
233 		die("Unknown network parameter %s", param);
234 
235 	return 0;
236 }
237 
238 static int netdev_parser(const struct option *opt, const char *arg, int unset)
239 {
240 	struct virtio_net_params p;
241 	char *buf = NULL, *cmd = NULL, *cur = NULL;
242 	bool on_cmd = true;
243 
244 	if (arg) {
245 		buf = strdup(arg);
246 		if (buf == NULL)
247 			die("Failed allocating new net buffer");
248 		cur = strtok(buf, ",=");
249 	}
250 
251 	p = (struct virtio_net_params) {
252 		.guest_ip	= DEFAULT_GUEST_ADDR,
253 		.host_ip	= DEFAULT_HOST_ADDR,
254 		.script		= DEFAULT_SCRIPT,
255 		.mode		= NET_MODE_TAP,
256 	};
257 
258 	str_to_mac(DEFAULT_GUEST_MAC, p.guest_mac);
259 	p.guest_mac[5] += kvm->cfg.num_net_devices;
260 
261 	while (cur) {
262 		if (on_cmd) {
263 			cmd = cur;
264 		} else {
265 			if (set_net_param(&p, cmd, cur) < 0)
266 				goto done;
267 		}
268 		on_cmd = !on_cmd;
269 
270 		cur = strtok(NULL, ",=");
271 	};
272 
273 	kvm->cfg.num_net_devices++;
274 
275 	kvm->cfg.net_params = realloc(kvm->cfg.net_params, kvm->cfg.num_net_devices * sizeof(*kvm->cfg.net_params));
276 	if (kvm->cfg.net_params == NULL)
277 		die("Failed adding new network device");
278 
279 	kvm->cfg.net_params[kvm->cfg.num_net_devices - 1] = p;
280 
281 done:
282 	free(buf);
283 	return 0;
284 }
285 
286 static int shmem_parser(const struct option *opt, const char *arg, int unset)
287 {
288 	const u64 default_size = SHMEM_DEFAULT_SIZE;
289 	const u64 default_phys_addr = SHMEM_DEFAULT_ADDR;
290 	const char *default_handle = SHMEM_DEFAULT_HANDLE;
291 	struct shmem_info *si = malloc(sizeof(struct shmem_info));
292 	u64 phys_addr;
293 	u64 size;
294 	char *handle = NULL;
295 	int create = 0;
296 	const char *p = arg;
297 	char *next;
298 	int base = 10;
299 	int verbose = 0;
300 
301 	const int skip_pci = strlen("pci:");
302 	if (verbose)
303 		pr_info("shmem_parser(%p,%s,%d)", opt, arg, unset);
304 	/* parse out optional addr family */
305 	if (strcasestr(p, "pci:")) {
306 		p += skip_pci;
307 	} else if (strcasestr(p, "mem:")) {
308 		die("I can't add to E820 map yet.\n");
309 	}
310 	/* parse out physical addr */
311 	base = 10;
312 	if (strcasestr(p, "0x"))
313 		base = 16;
314 	phys_addr = strtoll(p, &next, base);
315 	if (next == p && phys_addr == 0) {
316 		pr_info("shmem: no physical addr specified, using default.");
317 		phys_addr = default_phys_addr;
318 	}
319 	if (*next != ':' && *next != '\0')
320 		die("shmem: unexpected chars after phys addr.\n");
321 	if (*next == '\0')
322 		p = next;
323 	else
324 		p = next + 1;
325 	/* parse out size */
326 	base = 10;
327 	if (strcasestr(p, "0x"))
328 		base = 16;
329 	size = strtoll(p, &next, base);
330 	if (next == p && size == 0) {
331 		pr_info("shmem: no size specified, using default.");
332 		size = default_size;
333 	}
334 	/* look for [KMGkmg][Bb]*  uses base 2. */
335 	int skip_B = 0;
336 	if (strspn(next, "KMGkmg")) {	/* might have a prefix */
337 		if (*(next + 1) == 'B' || *(next + 1) == 'b')
338 			skip_B = 1;
339 		switch (*next) {
340 		case 'K':
341 		case 'k':
342 			size = size << KB_SHIFT;
343 			break;
344 		case 'M':
345 		case 'm':
346 			size = size << MB_SHIFT;
347 			break;
348 		case 'G':
349 		case 'g':
350 			size = size << GB_SHIFT;
351 			break;
352 		default:
353 			die("shmem: bug in detecting size prefix.");
354 			break;
355 		}
356 		next += 1 + skip_B;
357 	}
358 	if (*next != ':' && *next != '\0') {
359 		die("shmem: unexpected chars after phys size. <%c><%c>\n",
360 		    *next, *p);
361 	}
362 	if (*next == '\0')
363 		p = next;
364 	else
365 		p = next + 1;
366 	/* parse out optional shmem handle */
367 	const int skip_handle = strlen("handle=");
368 	next = strcasestr(p, "handle=");
369 	if (*p && next) {
370 		if (p != next)
371 			die("unexpected chars before handle\n");
372 		p += skip_handle;
373 		next = strchrnul(p, ':');
374 		if (next - p) {
375 			handle = malloc(next - p + 1);
376 			strncpy(handle, p, next - p);
377 			handle[next - p] = '\0';	/* just in case. */
378 		}
379 		if (*next == '\0')
380 			p = next;
381 		else
382 			p = next + 1;
383 	}
384 	/* parse optional create flag to see if we should create shm seg. */
385 	if (*p && strcasestr(p, "create")) {
386 		create = 1;
387 		p += strlen("create");
388 	}
389 	if (*p != '\0')
390 		die("shmem: unexpected trailing chars\n");
391 	if (handle == NULL) {
392 		handle = malloc(strlen(default_handle) + 1);
393 		strcpy(handle, default_handle);
394 	}
395 	if (verbose) {
396 		pr_info("shmem: phys_addr = %llx", phys_addr);
397 		pr_info("shmem: size      = %llx", size);
398 		pr_info("shmem: handle    = %s", handle);
399 		pr_info("shmem: create    = %d", create);
400 	}
401 
402 	si->phys_addr = phys_addr;
403 	si->size = size;
404 	si->handle = handle;
405 	si->create = create;
406 	pci_shmem__register_mem(si);	/* ownership of si, etc. passed on. */
407 	return 0;
408 }
409 
410 #define BUILD_OPTIONS(name, cfg)					\
411 	struct option name[] = {					\
412 	OPT_GROUP("Basic options:"),					\
413 	OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name",	\
414 			"A name for the guest"),			\
415 	OPT_INTEGER('c', "cpus", &nrcpus, "Number of CPUs"),		\
416 	OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory size\
417 		in MiB."),						\
418 	OPT_CALLBACK('\0', "shmem", NULL,				\
419 		     "[pci:]<addr>:<size>[:handle=<handle>][:create]",	\
420 		     "Share host shmem with guest via pci device",	\
421 		     shmem_parser),					\
422 	OPT_CALLBACK('d', "disk", NULL, "image or rootfs_dir", "Disk	\
423 			image or rootfs directory", img_name_parser),	\
424 	OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio	\
425 			balloon"),					\
426 	OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\
427 	OPT_BOOLEAN('\0', "sdl", &(cfg)->sdl, "Enable SDL framebuffer"),\
428 	OPT_BOOLEAN('\0', "rng", &(cfg)->virtio_rng, "Enable virtio Random\
429 			Number Generator"),				\
430 	OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name",		\
431 		     "Enable virtio 9p to share files between host and	\
432 		     guest", virtio_9p_rootdir_parser),			\
433 	OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or	\
434 			hv", "Console to use"),				\
435 	OPT_STRING('\0', "dev", &(cfg)->dev, "device_file",		\
436 			"KVM device file"),				\
437 	OPT_CALLBACK('\0', "tty", NULL, "tty id",			\
438 		     "Remap guest TTY into a pty on the host",		\
439 		     tty_parser),					\
440 	OPT_STRING('\0', "sandbox", &(cfg)->sandbox, "script",		\
441 			"Run this script when booting into custom	\
442 			rootfs"),					\
443 	OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path",	\
444 			"Hugetlbfs path"),				\
445 									\
446 	OPT_GROUP("Kernel options:"),					\
447 	OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel",	\
448 			"Kernel to boot in virtual machine"),		\
449 	OPT_STRING('i', "initrd", &(cfg)->initrd_filename, "initrd",	\
450 			"Initial RAM disk image"),			\
451 	OPT_STRING('p', "params", &(cfg)->kernel_cmdline, "params",	\
452 			"Kernel command line arguments"),		\
453 	OPT_STRING('f', "firmware", &(cfg)->firmware_filename, "firmware",\
454 			"Firmware image to boot in virtual machine"),	\
455 									\
456 	OPT_GROUP("Networking options:"),				\
457 	OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params",	\
458 		     "Create a new guest NIC",				\
459 		     netdev_parser, NULL),				\
460 	OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel DHCP\
461 			in rootfs mode"),				\
462 									\
463 	OPT_GROUP("BIOS options:"),					\
464 	OPT_INTEGER('\0', "vidmode", &vidmode,				\
465 		    "Video mode"),					\
466 									\
467 	OPT_GROUP("Debug options:"),					\
468 	OPT_BOOLEAN('\0', "debug", &do_debug_print,			\
469 			"Enable debug messages"),			\
470 	OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step,	\
471 			"Enable single stepping"),			\
472 	OPT_BOOLEAN('\0', "debug-ioport", &(cfg)->ioport_debug,		\
473 			"Enable ioport debugging"),			\
474 	OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug,		\
475 			"Enable MMIO debugging"),			\
476 	OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay,		\
477 			"Delay IO by millisecond"),			\
478 	OPT_END()							\
479 	};
480 
481 /*
482  * Serialize debug printout so that the output of multiple vcpus does not
483  * get mixed up:
484  */
485 static int printout_done;
486 
487 static void handle_sigusr1(int sig)
488 {
489 	struct kvm_cpu *cpu = current_kvm_cpu;
490 	int fd = kvm_cpu__get_debug_fd();
491 
492 	if (!cpu || cpu->needs_nmi)
493 		return;
494 
495 	dprintf(fd, "\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id);
496 	kvm_cpu__show_registers(cpu);
497 	kvm_cpu__show_code(cpu);
498 	kvm_cpu__show_page_tables(cpu);
499 	fflush(stdout);
500 	printout_done = 1;
501 	mb();
502 }
503 
504 /* Pause/resume the guest using SIGUSR2 */
505 static int is_paused;
506 
507 static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
508 {
509 	if (WARN_ON(len))
510 		return;
511 
512 	if (type == KVM_IPC_RESUME && is_paused) {
513 		kvm->vm_state = KVM_VMSTATE_RUNNING;
514 		kvm__continue();
515 	} else if (type == KVM_IPC_PAUSE && !is_paused) {
516 		kvm->vm_state = KVM_VMSTATE_PAUSED;
517 		ioctl(kvm->vm_fd, KVM_KVMCLOCK_CTRL);
518 		kvm__pause();
519 	} else {
520 		return;
521 	}
522 
523 	is_paused = !is_paused;
524 }
525 
526 static void handle_vmstate(int fd, u32 type, u32 len, u8 *msg)
527 {
528 	int r = 0;
529 
530 	if (type == KVM_IPC_VMSTATE)
531 		r = write(fd, &kvm->vm_state, sizeof(kvm->vm_state));
532 
533 	if (r < 0)
534 		pr_warning("Failed sending VMSTATE");
535 }
536 
537 static void handle_debug(int fd, u32 type, u32 len, u8 *msg)
538 {
539 	int i;
540 	struct debug_cmd_params *params;
541 	u32 dbg_type;
542 	u32 vcpu;
543 
544 	if (WARN_ON(type != KVM_IPC_DEBUG || len != sizeof(*params)))
545 		return;
546 
547 	params = (void *)msg;
548 	dbg_type = params->dbg_type;
549 	vcpu = params->cpu;
550 
551 	if (dbg_type & KVM_DEBUG_CMD_TYPE_SYSRQ)
552 		serial8250__inject_sysrq(kvm, params->sysrq);
553 
554 	if (dbg_type & KVM_DEBUG_CMD_TYPE_NMI) {
555 		if ((int)vcpu >= kvm->nrcpus)
556 			return;
557 
558 		kvm_cpus[vcpu]->needs_nmi = 1;
559 		pthread_kill(kvm_cpus[vcpu]->thread, SIGUSR1);
560 	}
561 
562 	if (!(dbg_type & KVM_DEBUG_CMD_TYPE_DUMP))
563 		return;
564 
565 	for (i = 0; i < nrcpus; i++) {
566 		struct kvm_cpu *cpu = kvm_cpus[i];
567 
568 		if (!cpu)
569 			continue;
570 
571 		printout_done = 0;
572 
573 		kvm_cpu__set_debug_fd(fd);
574 		pthread_kill(cpu->thread, SIGUSR1);
575 		/*
576 		 * Wait for the vCPU to dump state before signalling
577 		 * the next thread. Since this is debug code it does
578 		 * not matter that we are burning CPU time a bit:
579 		 */
580 		while (!printout_done)
581 			mb();
582 	}
583 
584 	close(fd);
585 
586 	serial8250__inject_sysrq(kvm, 'p');
587 }
588 
589 static void handle_sigalrm(int sig)
590 {
591 	kvm__arch_periodic_poll(kvm);
592 }
593 
594 static void handle_stop(int fd, u32 type, u32 len, u8 *msg)
595 {
596 	if (WARN_ON(type != KVM_IPC_STOP || len))
597 		return;
598 
599 	kvm_cpu__reboot();
600 }
601 
602 static void *kvm_cpu_thread(void *arg)
603 {
604 	current_kvm_cpu		= arg;
605 
606 	if (kvm_cpu__start(current_kvm_cpu))
607 		goto panic_kvm;
608 
609 	return (void *) (intptr_t) 0;
610 
611 panic_kvm:
612 	fprintf(stderr, "KVM exit reason: %u (\"%s\")\n",
613 		current_kvm_cpu->kvm_run->exit_reason,
614 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
615 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
616 		fprintf(stderr, "KVM exit code: 0x%Lu\n",
617 			current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
618 
619 	kvm_cpu__set_debug_fd(STDOUT_FILENO);
620 	kvm_cpu__show_registers(current_kvm_cpu);
621 	kvm_cpu__show_code(current_kvm_cpu);
622 	kvm_cpu__show_page_tables(current_kvm_cpu);
623 
624 	return (void *) (intptr_t) 1;
625 }
626 
627 static char kernel[PATH_MAX];
628 
629 static const char *host_kernels[] = {
630 	"/boot/vmlinuz",
631 	"/boot/bzImage",
632 	NULL
633 };
634 
635 static const char *default_kernels[] = {
636 	"./bzImage",
637 	"arch/" BUILD_ARCH "/boot/bzImage",
638 	"../../arch/" BUILD_ARCH "/boot/bzImage",
639 	NULL
640 };
641 
642 static const char *default_vmlinux[] = {
643 	"vmlinux",
644 	"../../../vmlinux",
645 	"../../vmlinux",
646 	NULL
647 };
648 
649 static void kernel_usage_with_options(void)
650 {
651 	const char **k;
652 	struct utsname uts;
653 
654 	fprintf(stderr, "Fatal: could not find default kernel image in:\n");
655 	k = &default_kernels[0];
656 	while (*k) {
657 		fprintf(stderr, "\t%s\n", *k);
658 		k++;
659 	}
660 
661 	if (uname(&uts) < 0)
662 		return;
663 
664 	k = &host_kernels[0];
665 	while (*k) {
666 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
667 			return;
668 		fprintf(stderr, "\t%s\n", kernel);
669 		k++;
670 	}
671 	fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n",
672 		KVM_BINARY_NAME);
673 }
674 
675 static u64 host_ram_size(void)
676 {
677 	long page_size;
678 	long nr_pages;
679 
680 	nr_pages	= sysconf(_SC_PHYS_PAGES);
681 	if (nr_pages < 0) {
682 		pr_warning("sysconf(_SC_PHYS_PAGES) failed");
683 		return 0;
684 	}
685 
686 	page_size	= sysconf(_SC_PAGE_SIZE);
687 	if (page_size < 0) {
688 		pr_warning("sysconf(_SC_PAGE_SIZE) failed");
689 		return 0;
690 	}
691 
692 	return (nr_pages * page_size) >> MB_SHIFT;
693 }
694 
695 /*
696  * If user didn't specify how much memory it wants to allocate for the guest,
697  * avoid filling the whole host RAM.
698  */
699 #define RAM_SIZE_RATIO		0.8
700 
701 static u64 get_ram_size(int nr_cpus)
702 {
703 	u64 available;
704 	u64 ram_size;
705 
706 	ram_size	= 64 * (nr_cpus + 3);
707 
708 	available	= host_ram_size() * RAM_SIZE_RATIO;
709 	if (!available)
710 		available = MIN_RAM_SIZE_MB;
711 
712 	if (ram_size > available)
713 		ram_size	= available;
714 
715 	return ram_size;
716 }
717 
718 static const char *find_kernel(void)
719 {
720 	const char **k;
721 	struct stat st;
722 	struct utsname uts;
723 
724 	k = &default_kernels[0];
725 	while (*k) {
726 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
727 			k++;
728 			continue;
729 		}
730 		strncpy(kernel, *k, PATH_MAX);
731 		return kernel;
732 	}
733 
734 	if (uname(&uts) < 0)
735 		return NULL;
736 
737 	k = &host_kernels[0];
738 	while (*k) {
739 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
740 			return NULL;
741 
742 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
743 			k++;
744 			continue;
745 		}
746 		return kernel;
747 
748 	}
749 	return NULL;
750 }
751 
752 static const char *find_vmlinux(void)
753 {
754 	const char **vmlinux;
755 
756 	vmlinux = &default_vmlinux[0];
757 	while (*vmlinux) {
758 		struct stat st;
759 
760 		if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
761 			vmlinux++;
762 			continue;
763 		}
764 		return *vmlinux;
765 	}
766 	return NULL;
767 }
768 
769 void kvm_run_help(void)
770 {
771 	BUILD_OPTIONS(options, &kvm->cfg);
772 	usage_with_options(run_usage, options);
773 }
774 
775 static int kvm_setup_guest_init(void)
776 {
777 	const char *rootfs = kvm->cfg.custom_rootfs_name;
778 	char tmp[PATH_MAX];
779 	size_t size;
780 	int fd, ret;
781 	char *data;
782 
783 	/* Setup /virt/init */
784 	size = (size_t)&_binary_guest_init_size;
785 	data = (char *)&_binary_guest_init_start;
786 	snprintf(tmp, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), rootfs);
787 	remove(tmp);
788 	fd = open(tmp, O_CREAT | O_WRONLY, 0755);
789 	if (fd < 0)
790 		die("Fail to setup %s", tmp);
791 	ret = xwrite(fd, data, size);
792 	if (ret < 0)
793 		die("Fail to setup %s", tmp);
794 	close(fd);
795 
796 	return 0;
797 }
798 
799 static int kvm_run_set_sandbox(void)
800 {
801 	const char *guestfs_name = kvm->cfg.custom_rootfs_name;
802 	char path[PATH_MAX], script[PATH_MAX], *tmp;
803 
804 	snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name);
805 
806 	remove(path);
807 
808 	if (kvm->cfg.sandbox == NULL)
809 		return 0;
810 
811 	tmp = realpath(kvm->cfg.sandbox, NULL);
812 	if (tmp == NULL)
813 		return -ENOMEM;
814 
815 	snprintf(script, PATH_MAX, "/host/%s", tmp);
816 	free(tmp);
817 
818 	return symlink(script, path);
819 }
820 
821 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg)
822 {
823 	const char *single_quote;
824 
825 	if (!*arg) { /* zero length string */
826 		if (write(fd, "''", 2) <= 0)
827 			die("Failed writing sandbox script");
828 		return;
829 	}
830 
831 	while (*arg) {
832 		single_quote = strchrnul(arg, '\'');
833 
834 		/* write non-single-quote string as #('string') */
835 		if (arg != single_quote) {
836 			if (write(fd, "'", 1) <= 0 ||
837 			    write(fd, arg, single_quote - arg) <= 0 ||
838 			    write(fd, "'", 1) <= 0)
839 				die("Failed writing sandbox script");
840 		}
841 
842 		/* write single quote as #("'") */
843 		if (*single_quote) {
844 			if (write(fd, "\"'\"", 3) <= 0)
845 				die("Failed writing sandbox script");
846 		} else
847 			break;
848 
849 		arg = single_quote + 1;
850 	}
851 }
852 
853 static void resolve_program(const char *src, char *dst, size_t len)
854 {
855 	struct stat st;
856 	int err;
857 
858 	err = stat(src, &st);
859 
860 	if (!err && S_ISREG(st.st_mode)) {
861 		char resolved_path[PATH_MAX];
862 
863 		if (!realpath(src, resolved_path))
864 			die("Unable to resolve program %s: %s\n", src, strerror(errno));
865 
866 		snprintf(dst, len, "/host%s", resolved_path);
867 	} else
868 		strncpy(dst, src, len);
869 }
870 
871 static void kvm_run_write_sandbox_cmd(const char **argv, int argc)
872 {
873 	const char script_hdr[] = "#! /bin/bash\n\n";
874 	char program[PATH_MAX];
875 	int fd;
876 
877 	remove(kvm->cfg.sandbox);
878 
879 	fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777);
880 	if (fd < 0)
881 		die("Failed creating sandbox script");
882 
883 	if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0)
884 		die("Failed writing sandbox script");
885 
886 	resolve_program(argv[0], program, PATH_MAX);
887 	kvm_write_sandbox_cmd_exactly(fd, program);
888 
889 	argv++;
890 	argc--;
891 
892 	while (argc) {
893 		if (write(fd, " ", 1) <= 0)
894 			die("Failed writing sandbox script");
895 
896 		kvm_write_sandbox_cmd_exactly(fd, argv[0]);
897 		argv++;
898 		argc--;
899 	}
900 	if (write(fd, "\n", 1) <= 0)
901 		die("Failed writing sandbox script");
902 
903 	close(fd);
904 }
905 
906 static int kvm_cmd_run_init(int argc, const char **argv)
907 {
908 	static char real_cmdline[2048], default_name[20];
909 	struct framebuffer *fb = NULL;
910 	unsigned int nr_online_cpus;
911 	int max_cpus, recommended_cpus;
912 	int i, r;
913 
914 	kvm = kvm__new();
915 	if (IS_ERR(kvm))
916 		return PTR_ERR(kvm);
917 
918 	signal(SIGALRM, handle_sigalrm);
919 	kvm_ipc__register_handler(KVM_IPC_DEBUG, handle_debug);
920 	signal(SIGUSR1, handle_sigusr1);
921 	kvm_ipc__register_handler(KVM_IPC_PAUSE, handle_pause);
922 	kvm_ipc__register_handler(KVM_IPC_RESUME, handle_pause);
923 	kvm_ipc__register_handler(KVM_IPC_STOP, handle_stop);
924 	kvm_ipc__register_handler(KVM_IPC_VMSTATE, handle_vmstate);
925 
926 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
927 	kvm->cfg.custom_rootfs_name = "default";
928 
929 	while (argc != 0) {
930 		BUILD_OPTIONS(options, &kvm->cfg);
931 		argc = parse_options(argc, argv, options, run_usage,
932 				PARSE_OPT_STOP_AT_NON_OPTION |
933 				PARSE_OPT_KEEP_DASHDASH);
934 		if (argc != 0) {
935 			/* Cusrom options, should have been handled elsewhere */
936 			if (strcmp(argv[0], "--") == 0) {
937 				if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
938 					kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
939 					kvm_run_write_sandbox_cmd(argv+1, argc-1);
940 					break;
941 				}
942 			}
943 
944 			if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) ||
945 				(kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) {
946 				fprintf(stderr, "Cannot handle parameter: "
947 						"%s\n", argv[0]);
948 				usage_with_options(run_usage, options);
949 				free(kvm);
950 				return -EINVAL;
951 			}
952 			if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
953 				/*
954 				 * first unhandled parameter is treated as
955 				 * sandbox command
956 				 */
957 				kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
958 				kvm_run_write_sandbox_cmd(argv, argc);
959 			} else {
960 				/*
961 				 * first unhandled parameter is treated as a kernel
962 				 * image
963 				 */
964 				kvm->cfg.kernel_filename = argv[0];
965 			}
966 			argv++;
967 			argc--;
968 		}
969 
970 	}
971 
972 	if (!kvm->cfg.kernel_filename)
973 		kvm->cfg.kernel_filename = find_kernel();
974 
975 	if (!kvm->cfg.kernel_filename) {
976 		kernel_usage_with_options();
977 		return -EINVAL;
978 	}
979 
980 	kvm->cfg.vmlinux_filename = find_vmlinux();
981 
982 	if (nrcpus == 0)
983 		nrcpus = nr_online_cpus;
984 
985 	if (!kvm->cfg.ram_size)
986 		kvm->cfg.ram_size = get_ram_size(nrcpus);
987 
988 	if (kvm->cfg.ram_size < MIN_RAM_SIZE_MB)
989 		die("Not enough memory specified: %lluMB (min %lluMB)", kvm->cfg.ram_size, MIN_RAM_SIZE_MB);
990 
991 	if (kvm->cfg.ram_size > host_ram_size())
992 		pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", kvm->cfg.ram_size, host_ram_size());
993 
994 	kvm->cfg.ram_size <<= MB_SHIFT;
995 
996 	if (!kvm->cfg.dev)
997 		kvm->cfg.dev = DEFAULT_KVM_DEV;
998 
999 	if (!kvm->cfg.console)
1000 		kvm->cfg.console = DEFAULT_CONSOLE;
1001 
1002 	if (!strncmp(kvm->cfg.console, "virtio", 6))
1003 		active_console  = CONSOLE_VIRTIO;
1004 	else if (!strncmp(kvm->cfg.console, "serial", 6))
1005 		active_console  = CONSOLE_8250;
1006 	else if (!strncmp(kvm->cfg.console, "hv", 2))
1007 		active_console = CONSOLE_HV;
1008 	else
1009 		pr_warning("No console!");
1010 
1011 	if (!kvm->cfg.host_ip)
1012 		kvm->cfg.host_ip = DEFAULT_HOST_ADDR;
1013 
1014 	if (!kvm->cfg.guest_ip)
1015 		kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR;
1016 
1017 	if (!kvm->cfg.guest_mac)
1018 		kvm->cfg.guest_mac = DEFAULT_GUEST_MAC;
1019 
1020 	if (!kvm->cfg.host_mac)
1021 		kvm->cfg.host_mac = DEFAULT_HOST_MAC;
1022 
1023 	if (!kvm->cfg.script)
1024 		kvm->cfg.script = DEFAULT_SCRIPT;
1025 
1026 	term_init();
1027 
1028 	if (!kvm->cfg.guest_name) {
1029 		if (kvm->cfg.custom_rootfs) {
1030 			kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name;
1031 		} else {
1032 			sprintf(default_name, "guest-%u", getpid());
1033 			kvm->cfg.guest_name = default_name;
1034 		}
1035 	}
1036 
1037 	r = kvm__init(kvm);
1038 	if (r)
1039 		goto fail;
1040 
1041 	r = ioeventfd__init(kvm);
1042 	if (r < 0) {
1043 		pr_err("ioeventfd__init() failed with error %d\n", r);
1044 		goto fail;
1045 	}
1046 
1047 	max_cpus = kvm__max_cpus(kvm);
1048 	recommended_cpus = kvm__recommended_cpus(kvm);
1049 
1050 	if (nrcpus > max_cpus) {
1051 		printf("  # Limit the number of CPUs to %d\n", max_cpus);
1052 		nrcpus = max_cpus;
1053 	} else if (nrcpus > recommended_cpus) {
1054 		printf("  # Warning: The maximum recommended amount of VCPUs"
1055 			" is %d\n", recommended_cpus);
1056 	}
1057 
1058 	kvm->nrcpus = nrcpus;
1059 
1060 	/* Alloc one pointer too many, so array ends up 0-terminated */
1061 	kvm_cpus = calloc(nrcpus + 1, sizeof(void *));
1062 	if (!kvm_cpus)
1063 		die("Couldn't allocate array for %d CPUs", nrcpus);
1064 
1065 	r = irq__init(kvm);
1066 	if (r < 0) {
1067 		pr_err("irq__init() failed with error %d\n", r);
1068 		goto fail;
1069 	}
1070 
1071 	r = pci__init(kvm);
1072 	if (r < 0) {
1073 		pr_err("pci__init() failed with error %d\n", r);
1074 		goto fail;
1075 	}
1076 
1077 	r = ioport__init(kvm);
1078 	if (r < 0) {
1079 		pr_err("ioport__init() failed with error %d\n", r);
1080 		goto fail;
1081 	}
1082 
1083 	/*
1084 	 * vidmode should be either specified
1085 	 * either set by default
1086 	 */
1087 	if (kvm->cfg.vnc || kvm->cfg.sdl) {
1088 		if (vidmode == -1)
1089 			vidmode = 0x312;
1090 	} else {
1091 		vidmode = 0;
1092 	}
1093 
1094 	memset(real_cmdline, 0, sizeof(real_cmdline));
1095 	kvm__arch_set_cmdline(real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl);
1096 
1097 	if (strlen(real_cmdline) > 0)
1098 		strcat(real_cmdline, " ");
1099 
1100 	if (kvm->cfg.kernel_cmdline)
1101 		strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline));
1102 
1103 	if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) {
1104 		char tmp[PATH_MAX];
1105 
1106 		kvm_setup_create_new(kvm->cfg.custom_rootfs_name);
1107 		kvm_setup_resolv(kvm->cfg.custom_rootfs_name);
1108 
1109 		snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default");
1110 		if (virtio_9p__register(kvm, tmp, "/dev/root") < 0)
1111 			die("Unable to initialize virtio 9p");
1112 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
1113 			die("Unable to initialize virtio 9p");
1114 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
1115 	}
1116 
1117 	if (kvm->cfg.using_rootfs) {
1118 		strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
1119 		if (kvm->cfg.custom_rootfs) {
1120 			kvm_run_set_sandbox();
1121 
1122 			strcat(real_cmdline, " init=/virt/init");
1123 
1124 			if (!kvm->cfg.no_dhcp)
1125 				strcat(real_cmdline, "  ip=dhcp");
1126 			if (kvm_setup_guest_init())
1127 				die("Failed to setup init for guest.");
1128 		}
1129 	} else if (!strstr(real_cmdline, "root=")) {
1130 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
1131 	}
1132 
1133 	if (kvm->cfg.image_count) {
1134 		kvm->nr_disks = kvm->cfg.image_count;
1135 		kvm->disks = disk_image__open_all((struct disk_image_params *)&kvm->cfg.disk_image, kvm->cfg.image_count);
1136 		if (IS_ERR(kvm->disks)) {
1137 			r = PTR_ERR(kvm->disks);
1138 			pr_err("disk_image__open_all() failed with error %ld\n",
1139 					PTR_ERR(kvm->disks));
1140 			goto fail;
1141 		}
1142 	}
1143 
1144 	printf("  # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
1145 		kvm->cfg.kernel_filename, kvm->cfg.ram_size / 1024 / 1024, nrcpus, kvm->cfg.guest_name);
1146 
1147 	if (!kvm->cfg.firmware_filename) {
1148 		if (!kvm__load_kernel(kvm, kvm->cfg.kernel_filename,
1149 				kvm->cfg.initrd_filename, real_cmdline, vidmode))
1150 			die("unable to load kernel %s", kvm->cfg.kernel_filename);
1151 
1152 		kvm->vmlinux = kvm->cfg.vmlinux_filename;
1153 		r = symbol_init(kvm);
1154 		if (r < 0)
1155 			pr_debug("symbol_init() failed with error %d\n", r);
1156 	}
1157 
1158 	ioport__setup_arch();
1159 
1160 	r = rtc__init(kvm);
1161 	if (r < 0) {
1162 		pr_err("rtc__init() failed with error %d\n", r);
1163 		goto fail;
1164 	}
1165 
1166 	r = serial8250__init(kvm);
1167 	if (r < 0) {
1168 		pr_err("serial__init() failed with error %d\n", r);
1169 		goto fail;
1170 	}
1171 
1172 	r = virtio_blk__init(kvm);
1173 	if (r < 0) {
1174 		pr_err("virtio_blk__init() failed with error %d\n", r);
1175 		goto fail;
1176 	}
1177 
1178 	r = virtio_scsi_init(kvm);
1179 	if (r < 0) {
1180 		pr_err("virtio_scsi_init() failed with error %d\n", r);
1181 		goto fail;
1182 	}
1183 
1184 
1185 	if (active_console == CONSOLE_VIRTIO)
1186 		virtio_console__init(kvm);
1187 
1188 	if (kvm->cfg.virtio_rng)
1189 		virtio_rng__init(kvm);
1190 
1191 	if (kvm->cfg.balloon)
1192 		virtio_bln__init(kvm);
1193 
1194 	if (!kvm->cfg.network)
1195 		kvm->cfg.network = DEFAULT_NETWORK;
1196 
1197 	virtio_9p__init(kvm);
1198 
1199 	for (i = 0; i < kvm->cfg.num_net_devices; i++) {
1200 		kvm->cfg.net_params[i].kvm = kvm;
1201 		virtio_net__init(&kvm->cfg.net_params[i]);
1202 	}
1203 
1204 	if (kvm->cfg.num_net_devices == 0 && kvm->cfg.no_net == 0) {
1205 		struct virtio_net_params net_params;
1206 
1207 		net_params = (struct virtio_net_params) {
1208 			.guest_ip	= kvm->cfg.guest_ip,
1209 			.host_ip	= kvm->cfg.host_ip,
1210 			.kvm		= kvm,
1211 			.script		= kvm->cfg.script,
1212 			.mode		= NET_MODE_USER,
1213 		};
1214 		str_to_mac(kvm->cfg.guest_mac, net_params.guest_mac);
1215 		str_to_mac(kvm->cfg.host_mac, net_params.host_mac);
1216 
1217 		virtio_net__init(&net_params);
1218 	}
1219 
1220 	kvm__init_ram(kvm);
1221 
1222 #ifdef CONFIG_X86
1223 	kbd__init(kvm);
1224 #endif
1225 
1226 	pci_shmem__init(kvm);
1227 
1228 	if (kvm->cfg.vnc || kvm->cfg.sdl) {
1229 		fb = vesa__init(kvm);
1230 		if (IS_ERR(fb)) {
1231 			pr_err("vesa__init() failed with error %ld\n", PTR_ERR(fb));
1232 			goto fail;
1233 		}
1234 	}
1235 
1236 	if (kvm->cfg.vnc && fb) {
1237 		r = vnc__init(fb);
1238 		if (r < 0) {
1239 			pr_err("vnc__init() failed with error %d\n", r);
1240 			goto fail;
1241 		}
1242 	}
1243 
1244 	if (kvm->cfg.sdl && fb) {
1245 		sdl__init(fb);
1246 		if (r < 0) {
1247 			pr_err("sdl__init() failed with error %d\n", r);
1248 			goto fail;
1249 		}
1250 	}
1251 
1252 	r = fb__start();
1253 	if (r < 0) {
1254 		pr_err("fb__init() failed with error %d\n", r);
1255 		goto fail;
1256 	}
1257 
1258 	/* Device init all done; firmware init must
1259 	 * come after this (it may set up device trees etc.)
1260 	 */
1261 
1262 	kvm__start_timer(kvm);
1263 
1264 	if (kvm->cfg.firmware_filename) {
1265 		if (!kvm__load_firmware(kvm, kvm->cfg.firmware_filename))
1266 			die("unable to load firmware image %s: %s", kvm->cfg.firmware_filename, strerror(errno));
1267 	} else {
1268 		kvm__arch_setup_firmware(kvm);
1269 		if (r < 0) {
1270 			pr_err("kvm__arch_setup_firmware() failed with error %d\n", r);
1271 			goto fail;
1272 		}
1273 	}
1274 
1275 	for (i = 0; i < nrcpus; i++) {
1276 		kvm_cpus[i] = kvm_cpu__init(kvm, i);
1277 		if (!kvm_cpus[i])
1278 			die("unable to initialize KVM VCPU");
1279 	}
1280 
1281 	thread_pool__init(nr_online_cpus);
1282 fail:
1283 	return r;
1284 }
1285 
1286 static int kvm_cmd_run_work(void)
1287 {
1288 	int i, r = -1;
1289 	void *ret = NULL;
1290 
1291 	for (i = 0; i < nrcpus; i++) {
1292 		if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0)
1293 			die("unable to create KVM VCPU thread");
1294 	}
1295 
1296 	/* Only VCPU #0 is going to exit by itself when shutting down */
1297 	if (pthread_join(kvm_cpus[0]->thread, &ret) != 0)
1298 		r = 0;
1299 
1300 	kvm_cpu__delete(kvm_cpus[0]);
1301 	kvm_cpus[0] = NULL;
1302 
1303 	for (i = 1; i < nrcpus; i++) {
1304 		if (kvm_cpus[i]->is_running) {
1305 			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
1306 			if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
1307 				die("pthread_join");
1308 			kvm_cpu__delete(kvm_cpus[i]);
1309 		}
1310 		if (ret == NULL)
1311 			r = 0;
1312 	}
1313 
1314 	return r;
1315 }
1316 
1317 static void kvm_cmd_run_exit(int guest_ret)
1318 {
1319 	int r = 0;
1320 
1321 	compat__print_all_messages();
1322 
1323 	r = symbol_exit(kvm);
1324 	if (r < 0)
1325 		pr_warning("symbol_exit() failed with error %d\n", r);
1326 
1327 	r = irq__exit(kvm);
1328 	if (r < 0)
1329 		pr_warning("irq__exit() failed with error %d\n", r);
1330 
1331 	fb__stop();
1332 
1333 	r = virtio_scsi_exit(kvm);
1334 	if (r < 0)
1335 		pr_warning("virtio_scsi_exit() failed with error %d\n", r);
1336 
1337 	r = virtio_blk__exit(kvm);
1338 	if (r < 0)
1339 		pr_warning("virtio_blk__exit() failed with error %d\n", r);
1340 
1341 	r = virtio_rng__exit(kvm);
1342 	if (r < 0)
1343 		pr_warning("virtio_rng__exit() failed with error %d\n", r);
1344 
1345 	r = disk_image__close_all(kvm->disks, kvm->cfg.image_count);
1346 	if (r < 0)
1347 		pr_warning("disk_image__close_all() failed with error %d\n", r);
1348 
1349 	r = serial8250__exit(kvm);
1350 	if (r < 0)
1351 		pr_warning("serial8250__exit() failed with error %d\n", r);
1352 
1353 	r = rtc__exit(kvm);
1354 	if (r < 0)
1355 		pr_warning("rtc__exit() failed with error %d\n", r);
1356 
1357 	r = kvm__arch_free_firmware(kvm);
1358 	if (r < 0)
1359 		pr_warning("kvm__arch_free_firmware() failed with error %d\n", r);
1360 
1361 	r = ioport__exit(kvm);
1362 	if (r < 0)
1363 		pr_warning("ioport__exit() failed with error %d\n", r);
1364 
1365 	r = ioeventfd__exit(kvm);
1366 	if (r < 0)
1367 		pr_warning("ioeventfd__exit() failed with error %d\n", r);
1368 
1369 	r = pci__exit(kvm);
1370 	if (r < 0)
1371 		pr_warning("pci__exit() failed with error %d\n", r);
1372 
1373 	r = kvm__exit(kvm);
1374 	if (r < 0)
1375 		pr_warning("pci__exit() failed with error %d\n", r);
1376 
1377 	free(kvm_cpus);
1378 
1379 	if (guest_ret == 0)
1380 		printf("\n  # KVM session ended normally.\n");
1381 }
1382 
1383 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
1384 {
1385 	int r, ret = -EFAULT;
1386 
1387 	r = kvm_cmd_run_init(argc, argv);
1388 	if (r < 0)
1389 		return r;
1390 
1391 	ret = kvm_cmd_run_work();
1392 	kvm_cmd_run_exit(ret);
1393 
1394 	return ret;
1395 }
1396