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