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