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