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