xref: /kvmtool/builtin-run.c (revision 8f239c84920d53e9d3320091b55fdbd25b3cb132)
1 #include "kvm/builtin-run.h"
2 
3 #include "kvm/virtio-balloon.h"
4 #include "kvm/virtio-console.h"
5 #include "kvm/parse-options.h"
6 #include "kvm/8250-serial.h"
7 #include "kvm/framebuffer.h"
8 #include "kvm/disk-image.h"
9 #include "kvm/threadpool.h"
10 #include "kvm/virtio-blk.h"
11 #include "kvm/virtio-net.h"
12 #include "kvm/virtio-rng.h"
13 #include "kvm/ioeventfd.h"
14 #include "kvm/virtio-9p.h"
15 #include "kvm/barrier.h"
16 #include "kvm/kvm-cpu.h"
17 #include "kvm/ioport.h"
18 #include "kvm/symbol.h"
19 #include "kvm/i8042.h"
20 #include "kvm/mutex.h"
21 #include "kvm/term.h"
22 #include "kvm/util.h"
23 #include "kvm/vesa.h"
24 #include "kvm/irq.h"
25 #include "kvm/kvm.h"
26 #include "kvm/pci.h"
27 #include "kvm/rtc.h"
28 #include "kvm/sdl.h"
29 #include "kvm/vnc.h"
30 
31 #include <linux/types.h>
32 
33 #include <sys/utsname.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <termios.h>
37 #include <signal.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <ctype.h>
42 #include <stdio.h>
43 
44 #define DEFAULT_KVM_DEV		"/dev/kvm"
45 #define DEFAULT_CONSOLE		"serial"
46 #define DEFAULT_NETWORK		"user"
47 #define DEFAULT_HOST_ADDR	"192.168.33.1"
48 #define DEFAULT_GUEST_ADDR	"192.168.33.15"
49 #define DEFAULT_GUEST_MAC	"02:15:15:15:15:15"
50 #define DEFAULT_HOST_MAC	"02:01:01:01:01:01"
51 #define DEFAULT_SCRIPT		"none"
52 
53 #define MB_SHIFT		(20)
54 #define MIN_RAM_SIZE_MB		(64ULL)
55 #define MIN_RAM_SIZE_BYTE	(MIN_RAM_SIZE_MB << MB_SHIFT)
56 
57 struct kvm *kvm;
58 struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
59 __thread struct kvm_cpu *current_kvm_cpu;
60 
61 static u64 ram_size;
62 static u8  image_count;
63 static int virtio_rng;
64 static const char *kernel_cmdline;
65 static const char *kernel_filename;
66 static const char *vmlinux_filename;
67 static const char *initrd_filename;
68 static const char *image_filename[MAX_DISK_IMAGES];
69 static const char *console;
70 static const char *dev;
71 static const char *network;
72 static const char *host_ip;
73 static const char *guest_ip;
74 static const char *guest_mac;
75 static const char *host_mac;
76 static const char *script;
77 static const char *guest_name;
78 static bool single_step;
79 static bool readonly_image[MAX_DISK_IMAGES];
80 static bool vnc;
81 static bool sdl;
82 static bool balloon;
83 static bool using_rootfs;
84 extern bool ioport_debug;
85 extern int  active_console;
86 extern int  debug_iodelay;
87 
88 bool do_debug_print = false;
89 
90 static int nrcpus;
91 static int vidmode = -1;
92 
93 static const char * const run_usage[] = {
94 	"kvm run [<options>] [<kernel image>]",
95 	NULL
96 };
97 
98 static int img_name_parser(const struct option *opt, const char *arg, int unset)
99 {
100 	char *sep;
101 	struct stat st;
102 
103 	if (stat(arg, &st) == 0 &&
104 	    S_ISDIR(st.st_mode)) {
105 		char tmp[PATH_MAX];
106 
107 		if (realpath(arg, tmp) == 0 ||
108 		    virtio_9p__init(kvm, tmp, "/dev/root") < 0)
109 			die("Unable to initialize virtio 9p");
110 		using_rootfs = 1;
111 		return 0;
112 	}
113 
114 	if (image_count >= MAX_DISK_IMAGES)
115 		die("Currently only 4 images are supported");
116 
117 	image_filename[image_count] = arg;
118 	sep = strstr(arg, ",");
119 	if (sep) {
120 		if (strcmp(sep + 1, "ro") == 0)
121 			readonly_image[image_count] = 1;
122 		*sep = 0;
123 	}
124 
125 	image_count++;
126 
127 	return 0;
128 }
129 
130 static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset)
131 {
132 	char *tag_name;
133 	char tmp[PATH_MAX];
134 
135 	/*
136 	 * 9p dir can be of the form dirname,tag_name or
137 	 * just dirname. In the later case we use the
138 	 * default tag name
139 	 */
140 	tag_name = strstr(arg, ",");
141 	if (tag_name) {
142 		*tag_name = '\0';
143 		tag_name++;
144 	}
145 	if (realpath(arg, tmp)) {
146 		if (virtio_9p__init(kvm, tmp, tag_name) < 0)
147 			die("Unable to initialize virtio 9p");
148 	} else
149 		die("Failed resolving 9p path");
150 	return 0;
151 }
152 
153 
154 static const struct option options[] = {
155 	OPT_GROUP("Basic options:"),
156 	OPT_STRING('\0', "name", &guest_name, "guest name",
157 			"A name for the guest"),
158 	OPT_INTEGER('c', "cpus", &nrcpus, "Number of CPUs"),
159 	OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."),
160 	OPT_CALLBACK('d', "disk", NULL, "image or rootfs_dir", "Disk image or rootfs directory", img_name_parser),
161 	OPT_BOOLEAN('\0', "balloon", &balloon, "Enable virtio balloon"),
162 	OPT_BOOLEAN('\0', "vnc", &vnc, "Enable VNC framebuffer"),
163 	OPT_BOOLEAN('\0', "sdl", &sdl, "Enable SDL framebuffer"),
164 	OPT_INCR('\0', "rng", &virtio_rng, "Enable virtio Random Number Generator"),
165 	OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name",
166 		     "Enable virtio 9p to share files between host and guest", virtio_9p_rootdir_parser),
167 	OPT_STRING('\0', "console", &console, "serial or virtio",
168 			"Console to use"),
169 	OPT_STRING('\0', "dev", &dev, "device_file", "KVM device file"),
170 
171 	OPT_GROUP("Kernel options:"),
172 	OPT_STRING('k', "kernel", &kernel_filename, "kernel",
173 			"Kernel to boot in virtual machine"),
174 	OPT_STRING('i', "initrd", &initrd_filename, "initrd",
175 			"Initial RAM disk image"),
176 	OPT_STRING('p', "params", &kernel_cmdline, "params",
177 			"Kernel command line arguments"),
178 
179 	OPT_GROUP("Networking options:"),
180 	OPT_STRING('n', "network", &network, "user, tap, none",
181 			"Network to use"),
182 	OPT_STRING('\0', "host-ip", &host_ip, "a.b.c.d",
183 			"Assign this address to the host side networking"),
184 	OPT_STRING('\0', "guest-ip", &guest_ip, "a.b.c.d",
185 			"Assign this address to the guest side networking"),
186 	OPT_STRING('\0', "host-mac", &host_mac, "aa:bb:cc:dd:ee:ff",
187 			"Assign this address to the host side NIC"),
188 	OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff",
189 			"Assign this address to the guest side NIC"),
190 	OPT_STRING('\0', "tapscript", &script, "Script path",
191 			 "Assign a script to process created tap device"),
192 
193 	OPT_GROUP("BIOS options:"),
194 	OPT_INTEGER('\0', "vidmode", &vidmode,
195 		    "Video mode"),
196 
197 	OPT_GROUP("Debug options:"),
198 	OPT_BOOLEAN('\0', "debug", &do_debug_print,
199 			"Enable debug messages"),
200 	OPT_BOOLEAN('\0', "debug-single-step", &single_step,
201 			"Enable single stepping"),
202 	OPT_BOOLEAN('\0', "debug-ioport", &ioport_debug,
203 			"Enable ioport debugging"),
204 	OPT_INTEGER('\0', "debug-iodelay", &debug_iodelay,
205 			"Delay IO by millisecond"),
206 	OPT_END()
207 };
208 
209 /*
210  * Serialize debug printout so that the output of multiple vcpus does not
211  * get mixed up:
212  */
213 static int printout_done;
214 
215 static void handle_sigusr1(int sig)
216 {
217 	struct kvm_cpu *cpu = current_kvm_cpu;
218 
219 	if (!cpu)
220 		return;
221 
222 	printf("\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id);
223 	kvm_cpu__show_registers(cpu);
224 	kvm_cpu__show_code(cpu);
225 	kvm_cpu__show_page_tables(cpu);
226 	fflush(stdout);
227 	printout_done = 1;
228 	mb();
229 }
230 
231 /* Pause/resume the guest using SIGUSR2 */
232 static int is_paused;
233 
234 static void handle_sigusr2(int sig)
235 {
236 	if (sig == SIGKVMRESUME && is_paused)
237 		kvm__continue();
238 	else if (sig == SIGUSR2 && !is_paused)
239 		kvm__pause();
240 	else
241 		return;
242 
243 	is_paused = !is_paused;
244 	pr_info("Guest %s\n", is_paused ? "paused" : "resumed");
245 }
246 
247 static void handle_sigquit(int sig)
248 {
249 	int i;
250 
251 	for (i = 0; i < nrcpus; i++) {
252 		struct kvm_cpu *cpu = kvm_cpus[i];
253 
254 		if (!cpu)
255 			continue;
256 
257 		printout_done = 0;
258 		pthread_kill(cpu->thread, SIGUSR1);
259 		/*
260 		 * Wait for the vCPU to dump state before signalling
261 		 * the next thread. Since this is debug code it does
262 		 * not matter that we are burning CPU time a bit:
263 		 */
264 		while (!printout_done)
265 			mb();
266 	}
267 
268 	serial8250__inject_sysrq(kvm);
269 }
270 
271 static void handle_sigalrm(int sig)
272 {
273 	serial8250__inject_interrupt(kvm);
274 	virtio_console__inject_interrupt(kvm);
275 }
276 
277 static void handle_sigstop(int sig)
278 {
279 	kvm_cpu__reboot();
280 }
281 
282 static void *kvm_cpu_thread(void *arg)
283 {
284 	current_kvm_cpu		= arg;
285 
286 	if (kvm_cpu__start(current_kvm_cpu))
287 		goto panic_kvm;
288 
289 	kvm_cpu__delete(current_kvm_cpu);
290 
291 	return (void *) (intptr_t) 0;
292 
293 panic_kvm:
294 	fprintf(stderr, "KVM exit reason: %u (\"%s\")\n",
295 		current_kvm_cpu->kvm_run->exit_reason,
296 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
297 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
298 		fprintf(stderr, "KVM exit code: 0x%Lu\n",
299 			current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
300 
301 	kvm_cpu__show_registers(current_kvm_cpu);
302 	kvm_cpu__show_code(current_kvm_cpu);
303 	kvm_cpu__show_page_tables(current_kvm_cpu);
304 
305 	kvm_cpu__delete(current_kvm_cpu);
306 
307 	return (void *) (intptr_t) 1;
308 }
309 
310 static char kernel[PATH_MAX];
311 
312 static const char *host_kernels[] = {
313 	"/boot/vmlinuz",
314 	"/boot/bzImage",
315 	NULL
316 };
317 
318 static const char *default_kernels[] = {
319 	"./bzImage",
320 	"../../arch/x86/boot/bzImage",
321 	NULL
322 };
323 
324 static const char *default_vmlinux[] = {
325 	"../../../vmlinux",
326 	"../../vmlinux",
327 	NULL
328 };
329 
330 static void kernel_usage_with_options(void)
331 {
332 	const char **k;
333 	struct utsname uts;
334 
335 	fprintf(stderr, "Fatal: could not find default kernel image in:\n");
336 	k = &default_kernels[0];
337 	while (*k) {
338 		fprintf(stderr, "\t%s\n", *k);
339 		k++;
340 	}
341 
342 	if (uname(&uts) < 0)
343 		return;
344 
345 	k = &host_kernels[0];
346 	while (*k) {
347 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
348 			return;
349 		fprintf(stderr, "\t%s\n", kernel);
350 		k++;
351 	}
352 	fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n");
353 }
354 
355 static u64 host_ram_size(void)
356 {
357 	long page_size;
358 	long nr_pages;
359 
360 	nr_pages	= sysconf(_SC_PHYS_PAGES);
361 	if (nr_pages < 0) {
362 		pr_warning("sysconf(_SC_PHYS_PAGES) failed");
363 		return 0;
364 	}
365 
366 	page_size	= sysconf(_SC_PAGE_SIZE);
367 	if (page_size < 0) {
368 		pr_warning("sysconf(_SC_PAGE_SIZE) failed");
369 		return 0;
370 	}
371 
372 	return (nr_pages * page_size) >> MB_SHIFT;
373 }
374 
375 /*
376  * If user didn't specify how much memory it wants to allocate for the guest,
377  * avoid filling the whole host RAM.
378  */
379 #define RAM_SIZE_RATIO		0.8
380 
381 static u64 get_ram_size(int nr_cpus)
382 {
383 	u64 available;
384 	u64 ram_size;
385 
386 	ram_size	= 64 * (nr_cpus + 3);
387 
388 	available	= host_ram_size() * RAM_SIZE_RATIO;
389 	if (!available)
390 		available = MIN_RAM_SIZE_MB;
391 
392 	if (ram_size > available)
393 		ram_size	= available;
394 
395 	return ram_size;
396 }
397 
398 static const char *find_kernel(void)
399 {
400 	const char **k;
401 	struct stat st;
402 	struct utsname uts;
403 
404 	k = &default_kernels[0];
405 	while (*k) {
406 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
407 			k++;
408 			continue;
409 		}
410 		strncpy(kernel, *k, PATH_MAX);
411 		return kernel;
412 	}
413 
414 	if (uname(&uts) < 0)
415 		return NULL;
416 
417 	k = &host_kernels[0];
418 	while (*k) {
419 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
420 			return NULL;
421 
422 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
423 			k++;
424 			continue;
425 		}
426 		return kernel;
427 
428 	}
429 	return NULL;
430 }
431 
432 static const char *find_vmlinux(void)
433 {
434 	const char **vmlinux;
435 
436 	vmlinux = &default_vmlinux[0];
437 	while (*vmlinux) {
438 		struct stat st;
439 
440 		if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
441 			vmlinux++;
442 			continue;
443 		}
444 		return *vmlinux;
445 	}
446 	return NULL;
447 }
448 
449 void kvm_run_help(void)
450 {
451 	usage_with_options(run_usage, options);
452 }
453 
454 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
455 {
456 	struct virtio_net_parameters net_params;
457 	static char real_cmdline[2048], default_name[20];
458 	struct framebuffer *fb = NULL;
459 	unsigned int nr_online_cpus;
460 	int exit_code = 0;
461 	int max_cpus, recommended_cpus;
462 	int i;
463 	void *ret;
464 
465 	signal(SIGALRM, handle_sigalrm);
466 	signal(SIGQUIT, handle_sigquit);
467 	signal(SIGUSR1, handle_sigusr1);
468 	signal(SIGUSR2, handle_sigusr2);
469 	signal(SIGKVMSTOP, handle_sigstop);
470 	signal(SIGKVMRESUME, handle_sigusr2);
471 	/* ignore balloon signal by default if not enable balloon optiion */
472 	signal(SIGKVMADDMEM, SIG_IGN);
473 	signal(SIGKVMDELMEM, SIG_IGN);
474 
475 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
476 
477 	while (argc != 0) {
478 		argc = parse_options(argc, argv, options, run_usage,
479 				PARSE_OPT_STOP_AT_NON_OPTION);
480 		if (argc != 0) {
481 			if (kernel_filename) {
482 				fprintf(stderr, "Cannot handle parameter: "
483 						"%s\n", argv[0]);
484 				usage_with_options(run_usage, options);
485 				return EINVAL;
486 			}
487 			/* first unhandled parameter is treated as a kernel
488 			   image
489 			 */
490 			kernel_filename = argv[0];
491 			argv++;
492 			argc--;
493 		}
494 
495 	}
496 
497 	if (!kernel_filename)
498 		kernel_filename = find_kernel();
499 
500 	if (!kernel_filename) {
501 		kernel_usage_with_options();
502 		return EINVAL;
503 	}
504 
505 	vmlinux_filename = find_vmlinux();
506 
507 	if (nrcpus == 0)
508 		nrcpus = nr_online_cpus;
509 	else if (nrcpus < 1 || nrcpus > KVM_NR_CPUS)
510 		die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS);
511 
512 	if (!ram_size)
513 		ram_size	= get_ram_size(nrcpus);
514 
515 	if (ram_size < MIN_RAM_SIZE_MB)
516 		die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB);
517 
518 	if (ram_size > host_ram_size())
519 		pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size, host_ram_size());
520 
521 	ram_size <<= MB_SHIFT;
522 
523 	if (!dev)
524 		dev = DEFAULT_KVM_DEV;
525 
526 	if (!console)
527 		console = DEFAULT_CONSOLE;
528 
529 	if (!strncmp(console, "virtio", 6))
530 		active_console  = CONSOLE_VIRTIO;
531 	else
532 		active_console  = CONSOLE_8250;
533 
534 	if (!host_ip)
535 		host_ip = DEFAULT_HOST_ADDR;
536 
537 	if (!guest_ip)
538 		guest_ip = DEFAULT_GUEST_ADDR;
539 
540 	if (!guest_mac)
541 		guest_mac = DEFAULT_GUEST_MAC;
542 
543 	if (!host_mac)
544 		host_mac = DEFAULT_HOST_MAC;
545 
546 	if (!script)
547 		script = DEFAULT_SCRIPT;
548 
549 	symbol__init(vmlinux_filename);
550 
551 	term_init();
552 
553 	if (!guest_name) {
554 		sprintf(default_name, "guest-%u", getpid());
555 		guest_name = default_name;
556 	}
557 
558 	kvm = kvm__init(dev, ram_size, guest_name);
559 
560 	irq__init(kvm);
561 
562 	kvm->single_step = single_step;
563 
564 	ioeventfd__init();
565 
566 	max_cpus = kvm__max_cpus(kvm);
567 	recommended_cpus = kvm__recommended_cpus(kvm);
568 
569 	if (nrcpus > max_cpus) {
570 		printf("  # Limit the number of CPUs to %d\n", max_cpus);
571 		kvm->nrcpus	= max_cpus;
572 	} else if (nrcpus > recommended_cpus) {
573 		printf("  # Warning: The maximum recommended amount of VCPUs"
574 			" is %d\n", recommended_cpus);
575 	}
576 
577 	kvm->nrcpus = nrcpus;
578 
579 	/*
580 	 * vidmode should be either specified
581 	 * either set by default
582 	 */
583 	if (vnc || sdl) {
584 		if (vidmode == -1)
585 			vidmode = 0x312;
586 	} else
587 		vidmode = 0;
588 
589 	memset(real_cmdline, 0, sizeof(real_cmdline));
590 	strcpy(real_cmdline, "notsc noapic noacpi pci=conf1 reboot=k panic=1 i8042.direct=1 i8042.dumbkbd=1 i8042.nopnp=1");
591 	if (vnc || sdl) {
592 		strcat(real_cmdline, " video=vesafb console=tty0");
593 	} else
594 		strcat(real_cmdline, " console=ttyS0 earlyprintk=serial");
595 	strcat(real_cmdline, " ");
596 	if (kernel_cmdline)
597 		strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline));
598 
599 	if (!using_rootfs && !image_filename[0]) {
600 		if (virtio_9p__init(kvm, "/", "/dev/root") < 0)
601 			die("Unable to initialize virtio 9p");
602 
603 		using_rootfs = 1;
604 
605 		if (!strstr(real_cmdline, "init="))
606 			strlcat(real_cmdline, " init=/bin/sh ", sizeof(real_cmdline));
607 	}
608 
609 	if (!strstr(real_cmdline, "root="))
610 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
611 
612 	if (using_rootfs)
613 		strcat(real_cmdline, " root=/dev/root rootflags=rw,trans=virtio,version=9p2000.u rootfstype=9p");
614 
615 	if (image_count) {
616 		kvm->nr_disks = image_count;
617 		kvm->disks    = disk_image__open_all(image_filename, readonly_image, image_count);
618 		if (!kvm->disks)
619 			die("Unable to load all disk images.");
620 
621 		virtio_blk__init_all(kvm);
622 	}
623 
624 	printf("  # kvm run -k %s -m %Lu -c %d --name %s\n", kernel_filename, ram_size / 1024 / 1024, nrcpus, guest_name);
625 
626 	if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,
627 				real_cmdline, vidmode))
628 		die("unable to load kernel %s", kernel_filename);
629 
630 	kvm->vmlinux		= vmlinux_filename;
631 
632 	ioport__setup_legacy();
633 
634 	rtc__init();
635 
636 	serial8250__init(kvm);
637 
638 	pci__init();
639 
640 	if (active_console == CONSOLE_VIRTIO)
641 		virtio_console__init(kvm);
642 
643 	if (virtio_rng)
644 		while (virtio_rng--)
645 			virtio_rng__init(kvm);
646 
647 	if (balloon)
648 		virtio_bln__init(kvm);
649 
650 	if (!network)
651 		network = DEFAULT_NETWORK;
652 
653 	if (strncmp(network, "none", 4)) {
654 		net_params.guest_ip = guest_ip;
655 		net_params.host_ip = host_ip;
656 		net_params.kvm = kvm;
657 		net_params.script = script;
658 		sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
659 			net_params.guest_mac,
660 			net_params.guest_mac+1,
661 			net_params.guest_mac+2,
662 			net_params.guest_mac+3,
663 			net_params.guest_mac+4,
664 			net_params.guest_mac+5);
665 		sscanf(host_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
666 			net_params.host_mac,
667 			net_params.host_mac+1,
668 			net_params.host_mac+2,
669 			net_params.host_mac+3,
670 			net_params.host_mac+4,
671 			net_params.host_mac+5);
672 
673 		if (!strncmp(network, "user", 4))
674 			net_params.mode = NET_MODE_USER;
675 		else if (!strncmp(network, "tap", 3))
676 			net_params.mode = NET_MODE_TAP;
677 		else
678 			die("Unkown network mode %s, please use -network user, tap, none", network);
679 		virtio_net__init(&net_params);
680 	}
681 
682 	kvm__start_timer(kvm);
683 
684 	kvm__setup_bios(kvm);
685 
686 	for (i = 0; i < nrcpus; i++) {
687 		kvm_cpus[i] = kvm_cpu__init(kvm, i);
688 		if (!kvm_cpus[i])
689 			die("unable to initialize KVM VCPU");
690 	}
691 
692 	kvm__init_ram(kvm);
693 
694 	kbd__init(kvm);
695 
696 	if (vnc || sdl)
697 		fb = vesa__init(kvm);
698 
699 	if (vnc) {
700 		if (fb)
701 			vnc__init(fb);
702 	}
703 
704 	if (sdl) {
705 		if (fb)
706 			sdl__init(fb);
707 	}
708 
709 	fb__start();
710 
711 	thread_pool__init(nr_online_cpus);
712 	ioeventfd__start();
713 
714 	for (i = 0; i < nrcpus; i++) {
715 		if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0)
716 			die("unable to create KVM VCPU thread");
717 	}
718 
719 	/* Only VCPU #0 is going to exit by itself when shutting down */
720 	if (pthread_join(kvm_cpus[0]->thread, &ret) != 0)
721 		exit_code = 1;
722 
723 	for (i = 1; i < nrcpus; i++) {
724 		if (kvm_cpus[i]->is_running) {
725 			pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
726 			if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
727 				die("pthread_join");
728 		}
729 		if (ret != NULL)
730 			exit_code = 1;
731 	}
732 
733 	fb__stop();
734 
735 	virtio_blk__delete_all(kvm);
736 	virtio_rng__delete_all(kvm);
737 
738 	disk_image__close_all(kvm->disks, image_count);
739 	kvm__delete(kvm);
740 
741 	if (!exit_code)
742 		printf("\n  # KVM session ended normally.\n");
743 
744 	return exit_code;
745 }
746