xref: /kvmtool/builtin-run.c (revision 5857730ceee5980e482807afc922e758cbeaa921)
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 MB_SHIFT		(20)
53 #define KB_SHIFT		(10)
54 #define GB_SHIFT		(30)
55 
56 __thread struct kvm_cpu *current_kvm_cpu;
57 
58 static int  kvm_run_wrapper;
59 
60 bool do_debug_print = false;
61 
62 static const char * const run_usage[] = {
63 	"lkvm run [<options>] [<kernel image>]",
64 	NULL
65 };
66 
67 enum {
68 	KVM_RUN_DEFAULT,
69 	KVM_RUN_SANDBOX,
70 };
71 
72 static int img_name_parser(const struct option *opt, const char *arg, int unset)
73 {
74 	char path[PATH_MAX];
75 	struct stat st;
76 
77 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
78 
79 	if ((stat(arg, &st) == 0 && S_ISDIR(st.st_mode)) ||
80 	   (stat(path, &st) == 0 && S_ISDIR(st.st_mode)))
81 		return virtio_9p_img_name_parser(opt, arg, unset);
82 	return disk_img_name_parser(opt, arg, unset);
83 }
84 
85 void kvm_run_set_wrapper_sandbox(void)
86 {
87 	kvm_run_wrapper = KVM_RUN_SANDBOX;
88 }
89 
90 #ifndef OPT_ARCH_RUN
91 #define OPT_ARCH_RUN(...)
92 #endif
93 
94 #define BUILD_OPTIONS(name, cfg, kvm)					\
95 	struct option name[] = {					\
96 	OPT_GROUP("Basic options:"),					\
97 	OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name",	\
98 			"A name for the guest"),			\
99 	OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"),	\
100 	OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory"	\
101 		" size in MiB."),					\
102 	OPT_CALLBACK('\0', "shmem", NULL,				\
103 		     "[pci:]<addr>:<size>[:handle=<handle>][:create]",	\
104 		     "Share host shmem with guest via pci device",	\
105 		     shmem_parser, NULL),				\
106 	OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk "	\
107 			" image or rootfs directory", img_name_parser,	\
108 			kvm),						\
109 	OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio"	\
110 			" balloon"),					\
111 	OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\
112 	OPT_BOOLEAN('\0', "gtk", &(cfg)->gtk, "Enable GTK framebuffer"),\
113 	OPT_BOOLEAN('\0', "sdl", &(cfg)->sdl, "Enable SDL framebuffer"),\
114 	OPT_BOOLEAN('\0', "rng", &(cfg)->virtio_rng, "Enable virtio"	\
115 			" Random Number Generator"),			\
116 	OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name",		\
117 		     "Enable virtio 9p to share files between host and"	\
118 		     " guest", virtio_9p_rootdir_parser, kvm),		\
119 	OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or"\
120 			" hv", "Console to use"),			\
121 	OPT_STRING('\0', "dev", &(cfg)->dev, "device_file",		\
122 			"KVM device file"),				\
123 	OPT_CALLBACK('\0', "tty", NULL, "tty id",			\
124 		     "Remap guest TTY into a pty on the host",		\
125 		     tty_parser, NULL),					\
126 	OPT_STRING('\0', "sandbox", &(cfg)->sandbox, "script",		\
127 			"Run this script when booting into custom"	\
128 			" rootfs"),					\
129 	OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path",	\
130 			"Hugetlbfs path"),				\
131 									\
132 	OPT_GROUP("Kernel options:"),					\
133 	OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel",	\
134 			"Kernel to boot in virtual machine"),		\
135 	OPT_STRING('i', "initrd", &(cfg)->initrd_filename, "initrd",	\
136 			"Initial RAM disk image"),			\
137 	OPT_STRING('p', "params", &(cfg)->kernel_cmdline, "params",	\
138 			"Kernel command line arguments"),		\
139 	OPT_STRING('f', "firmware", &(cfg)->firmware_filename, "firmware",\
140 			"Firmware image to boot in virtual machine"),	\
141 									\
142 	OPT_GROUP("Networking options:"),				\
143 	OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params",	\
144 		     "Create a new guest NIC",				\
145 		     netdev_parser, NULL, kvm),				\
146 	OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel"	\
147 			" DHCP in rootfs mode"),			\
148 									\
149 	OPT_GROUP("Debug options:"),					\
150 	OPT_BOOLEAN('\0', "debug", &do_debug_print,			\
151 			"Enable debug messages"),			\
152 	OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step,	\
153 			"Enable single stepping"),			\
154 	OPT_BOOLEAN('\0', "debug-ioport", &(cfg)->ioport_debug,		\
155 			"Enable ioport debugging"),			\
156 	OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug,		\
157 			"Enable MMIO debugging"),			\
158 	OPT_INTEGER('\0', "debug-iodelay", &(cfg)->debug_iodelay,	\
159 			"Delay IO by millisecond"),			\
160 									\
161 	OPT_ARCH(RUN, cfg)						\
162 	OPT_END()							\
163 	};
164 
165 static void *kvm_cpu_thread(void *arg)
166 {
167 	char name[16];
168 
169 	current_kvm_cpu = arg;
170 
171 	sprintf(name, "kvm-vcpu-%lu", current_kvm_cpu->cpu_id);
172 	kvm__set_thread_name(name);
173 
174 	if (kvm_cpu__start(current_kvm_cpu))
175 		goto panic_kvm;
176 
177 	return (void *) (intptr_t) 0;
178 
179 panic_kvm:
180 	fprintf(stderr, "KVM exit reason: %u (\"%s\")\n",
181 		current_kvm_cpu->kvm_run->exit_reason,
182 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
183 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
184 		fprintf(stderr, "KVM exit code: 0x%llu\n",
185 			(unsigned long long)current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
186 
187 	kvm_cpu__set_debug_fd(STDOUT_FILENO);
188 	kvm_cpu__show_registers(current_kvm_cpu);
189 	kvm_cpu__show_code(current_kvm_cpu);
190 	kvm_cpu__show_page_tables(current_kvm_cpu);
191 
192 	return (void *) (intptr_t) 1;
193 }
194 
195 static char kernel[PATH_MAX];
196 
197 static const char *host_kernels[] = {
198 	"/boot/vmlinuz",
199 	"/boot/bzImage",
200 	NULL
201 };
202 
203 static const char *default_kernels[] = {
204 	"./bzImage",
205 	"arch/" BUILD_ARCH "/boot/bzImage",
206 	"../../arch/" BUILD_ARCH "/boot/bzImage",
207 	NULL
208 };
209 
210 static const char *default_vmlinux[] = {
211 	"vmlinux",
212 	"../../../vmlinux",
213 	"../../vmlinux",
214 	NULL
215 };
216 
217 static void kernel_usage_with_options(void)
218 {
219 	const char **k;
220 	struct utsname uts;
221 
222 	fprintf(stderr, "Fatal: could not find default kernel image in:\n");
223 	k = &default_kernels[0];
224 	while (*k) {
225 		fprintf(stderr, "\t%s\n", *k);
226 		k++;
227 	}
228 
229 	if (uname(&uts) < 0)
230 		return;
231 
232 	k = &host_kernels[0];
233 	while (*k) {
234 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
235 			return;
236 		fprintf(stderr, "\t%s\n", kernel);
237 		k++;
238 	}
239 	fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n",
240 		KVM_BINARY_NAME);
241 }
242 
243 static u64 host_ram_size(void)
244 {
245 	long page_size;
246 	long nr_pages;
247 
248 	nr_pages	= sysconf(_SC_PHYS_PAGES);
249 	if (nr_pages < 0) {
250 		pr_warning("sysconf(_SC_PHYS_PAGES) failed");
251 		return 0;
252 	}
253 
254 	page_size	= sysconf(_SC_PAGE_SIZE);
255 	if (page_size < 0) {
256 		pr_warning("sysconf(_SC_PAGE_SIZE) failed");
257 		return 0;
258 	}
259 
260 	return (nr_pages * page_size) >> MB_SHIFT;
261 }
262 
263 /*
264  * If user didn't specify how much memory it wants to allocate for the guest,
265  * avoid filling the whole host RAM.
266  */
267 #define RAM_SIZE_RATIO		0.8
268 
269 static u64 get_ram_size(int nr_cpus)
270 {
271 	u64 available;
272 	u64 ram_size;
273 
274 	ram_size	= 64 * (nr_cpus + 3);
275 
276 	available	= host_ram_size() * RAM_SIZE_RATIO;
277 	if (!available)
278 		available = MIN_RAM_SIZE_MB;
279 
280 	if (ram_size > available)
281 		ram_size	= available;
282 
283 	return ram_size;
284 }
285 
286 static const char *find_kernel(void)
287 {
288 	const char **k;
289 	struct stat st;
290 	struct utsname uts;
291 
292 	k = &default_kernels[0];
293 	while (*k) {
294 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
295 			k++;
296 			continue;
297 		}
298 		strncpy(kernel, *k, PATH_MAX);
299 		return kernel;
300 	}
301 
302 	if (uname(&uts) < 0)
303 		return NULL;
304 
305 	k = &host_kernels[0];
306 	while (*k) {
307 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
308 			return NULL;
309 
310 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
311 			k++;
312 			continue;
313 		}
314 		return kernel;
315 
316 	}
317 	return NULL;
318 }
319 
320 static const char *find_vmlinux(void)
321 {
322 	const char **vmlinux;
323 
324 	vmlinux = &default_vmlinux[0];
325 	while (*vmlinux) {
326 		struct stat st;
327 
328 		if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
329 			vmlinux++;
330 			continue;
331 		}
332 		return *vmlinux;
333 	}
334 	return NULL;
335 }
336 
337 void kvm_run_help(void)
338 {
339 	struct kvm *kvm = NULL;
340 
341 	BUILD_OPTIONS(options, &kvm->cfg, kvm);
342 	usage_with_options(run_usage, options);
343 }
344 
345 static int kvm_run_set_sandbox(struct kvm *kvm)
346 {
347 	const char *guestfs_name = kvm->cfg.custom_rootfs_name;
348 	char path[PATH_MAX], script[PATH_MAX], *tmp;
349 
350 	snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name);
351 
352 	remove(path);
353 
354 	if (kvm->cfg.sandbox == NULL)
355 		return 0;
356 
357 	tmp = realpath(kvm->cfg.sandbox, NULL);
358 	if (tmp == NULL)
359 		return -ENOMEM;
360 
361 	snprintf(script, PATH_MAX, "/host/%s", tmp);
362 	free(tmp);
363 
364 	return symlink(script, path);
365 }
366 
367 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg)
368 {
369 	const char *single_quote;
370 
371 	if (!*arg) { /* zero length string */
372 		if (write(fd, "''", 2) <= 0)
373 			die("Failed writing sandbox script");
374 		return;
375 	}
376 
377 	while (*arg) {
378 		single_quote = strchrnul(arg, '\'');
379 
380 		/* write non-single-quote string as #('string') */
381 		if (arg != single_quote) {
382 			if (write(fd, "'", 1) <= 0 ||
383 			    write(fd, arg, single_quote - arg) <= 0 ||
384 			    write(fd, "'", 1) <= 0)
385 				die("Failed writing sandbox script");
386 		}
387 
388 		/* write single quote as #("'") */
389 		if (*single_quote) {
390 			if (write(fd, "\"'\"", 3) <= 0)
391 				die("Failed writing sandbox script");
392 		} else
393 			break;
394 
395 		arg = single_quote + 1;
396 	}
397 }
398 
399 static void resolve_program(const char *src, char *dst, size_t len)
400 {
401 	struct stat st;
402 	int err;
403 
404 	err = stat(src, &st);
405 
406 	if (!err && S_ISREG(st.st_mode)) {
407 		char resolved_path[PATH_MAX];
408 
409 		if (!realpath(src, resolved_path))
410 			die("Unable to resolve program %s: %s\n", src, strerror(errno));
411 
412 		snprintf(dst, len, "/host%s", resolved_path);
413 	} else
414 		strncpy(dst, src, len);
415 }
416 
417 static void kvm_run_write_sandbox_cmd(struct kvm *kvm, const char **argv, int argc)
418 {
419 	const char script_hdr[] = "#! /bin/bash\n\n";
420 	char program[PATH_MAX];
421 	int fd;
422 
423 	remove(kvm->cfg.sandbox);
424 
425 	fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777);
426 	if (fd < 0)
427 		die("Failed creating sandbox script");
428 
429 	if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0)
430 		die("Failed writing sandbox script");
431 
432 	resolve_program(argv[0], program, PATH_MAX);
433 	kvm_write_sandbox_cmd_exactly(fd, program);
434 
435 	argv++;
436 	argc--;
437 
438 	while (argc) {
439 		if (write(fd, " ", 1) <= 0)
440 			die("Failed writing sandbox script");
441 
442 		kvm_write_sandbox_cmd_exactly(fd, argv[0]);
443 		argv++;
444 		argc--;
445 	}
446 	if (write(fd, "\n", 1) <= 0)
447 		die("Failed writing sandbox script");
448 
449 	close(fd);
450 }
451 
452 static struct kvm *kvm_cmd_run_init(int argc, const char **argv)
453 {
454 	static char real_cmdline[2048], default_name[20];
455 	unsigned int nr_online_cpus;
456 	struct kvm *kvm = kvm__new();
457 	bool video;
458 
459 	if (IS_ERR(kvm))
460 		return kvm;
461 
462 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
463 	kvm->cfg.custom_rootfs_name = "default";
464 
465 	while (argc != 0) {
466 		BUILD_OPTIONS(options, &kvm->cfg, kvm);
467 		argc = parse_options(argc, argv, options, run_usage,
468 				PARSE_OPT_STOP_AT_NON_OPTION |
469 				PARSE_OPT_KEEP_DASHDASH);
470 		if (argc != 0) {
471 			/* Cusrom options, should have been handled elsewhere */
472 			if (strcmp(argv[0], "--") == 0) {
473 				if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
474 					kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
475 					kvm_run_write_sandbox_cmd(kvm, argv+1, argc-1);
476 					break;
477 				}
478 			}
479 
480 			if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) ||
481 				(kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) {
482 				fprintf(stderr, "Cannot handle parameter: "
483 						"%s\n", argv[0]);
484 				usage_with_options(run_usage, options);
485 				free(kvm);
486 				return ERR_PTR(-EINVAL);
487 			}
488 			if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
489 				/*
490 				 * first unhandled parameter is treated as
491 				 * sandbox command
492 				 */
493 				kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
494 				kvm_run_write_sandbox_cmd(kvm, argv, argc);
495 			} else {
496 				/*
497 				 * first unhandled parameter is treated as a kernel
498 				 * image
499 				 */
500 				kvm->cfg.kernel_filename = argv[0];
501 			}
502 			argv++;
503 			argc--;
504 		}
505 
506 	}
507 
508 	kvm->nr_disks = kvm->cfg.image_count;
509 
510 	if (!kvm->cfg.kernel_filename)
511 		kvm->cfg.kernel_filename = find_kernel();
512 
513 	if (!kvm->cfg.kernel_filename) {
514 		kernel_usage_with_options();
515 		return ERR_PTR(-EINVAL);
516 	}
517 
518 	kvm->cfg.vmlinux_filename = find_vmlinux();
519 	kvm->vmlinux = kvm->cfg.vmlinux_filename;
520 
521 	if (kvm->cfg.nrcpus == 0)
522 		kvm->cfg.nrcpus = nr_online_cpus;
523 
524 	if (!kvm->cfg.ram_size)
525 		kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus);
526 
527 	if (kvm->cfg.ram_size > host_ram_size())
528 		pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB",
529 			(unsigned long long)kvm->cfg.ram_size,
530 			(unsigned long long)host_ram_size());
531 
532 	kvm->cfg.ram_size <<= MB_SHIFT;
533 
534 	if (!kvm->cfg.dev)
535 		kvm->cfg.dev = DEFAULT_KVM_DEV;
536 
537 	if (!kvm->cfg.console)
538 		kvm->cfg.console = DEFAULT_CONSOLE;
539 
540 	video = kvm->cfg.vnc || kvm->cfg.sdl || kvm->cfg.gtk;
541 
542 	if (!strncmp(kvm->cfg.console, "virtio", 6))
543 		kvm->cfg.active_console  = CONSOLE_VIRTIO;
544 	else if (!strncmp(kvm->cfg.console, "serial", 6))
545 		kvm->cfg.active_console  = CONSOLE_8250;
546 	else if (!strncmp(kvm->cfg.console, "hv", 2))
547 		kvm->cfg.active_console = CONSOLE_HV;
548 	else
549 		pr_warning("No console!");
550 
551 	if (!kvm->cfg.host_ip)
552 		kvm->cfg.host_ip = DEFAULT_HOST_ADDR;
553 
554 	if (!kvm->cfg.guest_ip)
555 		kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR;
556 
557 	if (!kvm->cfg.guest_mac)
558 		kvm->cfg.guest_mac = DEFAULT_GUEST_MAC;
559 
560 	if (!kvm->cfg.host_mac)
561 		kvm->cfg.host_mac = DEFAULT_HOST_MAC;
562 
563 	if (!kvm->cfg.script)
564 		kvm->cfg.script = DEFAULT_SCRIPT;
565 
566 	if (!kvm->cfg.network)
567                 kvm->cfg.network = DEFAULT_NETWORK;
568 
569 	memset(real_cmdline, 0, sizeof(real_cmdline));
570 	kvm__arch_set_cmdline(real_cmdline, video);
571 
572 	if (video) {
573 		strcat(real_cmdline, "console=tty0");
574 	} else {
575 		switch (kvm->cfg.active_console) {
576 		case CONSOLE_HV:
577 			/* Fallthrough */
578 		case CONSOLE_VIRTIO:
579 			strcat(real_cmdline, "console=hvc0");
580 			break;
581 		case CONSOLE_8250:
582 			strcat(real_cmdline, "console=ttyS0");
583 			break;
584 		}
585 	}
586 
587 	if (!kvm->cfg.guest_name) {
588 		if (kvm->cfg.custom_rootfs) {
589 			kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name;
590 		} else {
591 			sprintf(default_name, "guest-%u", getpid());
592 			kvm->cfg.guest_name = default_name;
593 		}
594 	}
595 
596 	if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) {
597 		char tmp[PATH_MAX];
598 
599 		kvm_setup_create_new(kvm->cfg.custom_rootfs_name);
600 		kvm_setup_resolv(kvm->cfg.custom_rootfs_name);
601 
602 		snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default");
603 		if (virtio_9p__register(kvm, tmp, "/dev/root") < 0)
604 			die("Unable to initialize virtio 9p");
605 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
606 			die("Unable to initialize virtio 9p");
607 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
608 	}
609 
610 	if (kvm->cfg.using_rootfs) {
611 		strcat(real_cmdline, " rw rootflags=trans=virtio,version=9p2000.L,cache=loose rootfstype=9p");
612 		if (kvm->cfg.custom_rootfs) {
613 			kvm_run_set_sandbox(kvm);
614 
615 #ifdef CONFIG_GUEST_PRE_INIT
616 			strcat(real_cmdline, " init=/virt/pre_init");
617 #else
618 			strcat(real_cmdline, " init=/virt/init");
619 #endif
620 
621 			if (!kvm->cfg.no_dhcp)
622 				strcat(real_cmdline, "  ip=dhcp");
623 			if (kvm_setup_guest_init(kvm->cfg.custom_rootfs_name))
624 				die("Failed to setup init for guest.");
625 		}
626 	} else if (!kvm->cfg.kernel_cmdline || !strstr(kvm->cfg.kernel_cmdline, "root=")) {
627 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
628 	}
629 
630 	if (kvm->cfg.kernel_cmdline) {
631 		strcat(real_cmdline, " ");
632 		strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline));
633 	}
634 
635 	kvm->cfg.real_cmdline = real_cmdline;
636 
637 	printf("  # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
638 		kvm->cfg.kernel_filename,
639 		(unsigned long long)kvm->cfg.ram_size / 1024 / 1024,
640 		kvm->cfg.nrcpus, kvm->cfg.guest_name);
641 
642 	if (init_list__init(kvm) < 0)
643 		die ("Initialisation failed");
644 
645 	return kvm;
646 }
647 
648 static int kvm_cmd_run_work(struct kvm *kvm)
649 {
650 	int i;
651 
652 	for (i = 0; i < kvm->nrcpus; i++) {
653 		if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0)
654 			die("unable to create KVM VCPU thread");
655 	}
656 
657 	/* Only VCPU #0 is going to exit by itself when shutting down */
658 	if (pthread_join(kvm->cpus[0]->thread, NULL) != 0)
659 		die("unable to join with vcpu 0");
660 
661 	return kvm_cpu__exit(kvm);
662 }
663 
664 static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret)
665 {
666 	compat__print_all_messages();
667 
668 	init_list__exit(kvm);
669 
670 	if (guest_ret == 0)
671 		printf("\n  # KVM session ended normally.\n");
672 }
673 
674 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
675 {
676 	int ret = -EFAULT;
677 	struct kvm *kvm;
678 
679 	kvm = kvm_cmd_run_init(argc, argv);
680 	if (IS_ERR(kvm))
681 		return PTR_ERR(kvm);
682 
683 	ret = kvm_cmd_run_work(kvm);
684 	kvm_cmd_run_exit(kvm, ret);
685 
686 	return ret;
687 }
688