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