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