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