xref: /kvmtool/builtin-run.c (revision 9b46ebc561d3cc3bb54a350e4eed94d24562f347)
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/kvm-ipc.h"
35 #include "kvm/builtin-debug.h"
36 
37 #include <linux/types.h>
38 #include <linux/err.h>
39 #include <linux/sizes.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 KB_SHIFT		(10)
53 #define MB_SHIFT		(20)
54 #define GB_SHIFT		(30)
55 #define TB_SHIFT		(40)
56 #define PB_SHIFT		(50)
57 
58 __thread struct kvm_cpu *current_kvm_cpu;
59 
60 static int  kvm_run_wrapper;
61 
62 bool do_debug_print = false;
63 
64 static const char * const run_usage[] = {
65 	"lkvm run [<options>] [<kernel image>]",
66 	NULL
67 };
68 
69 enum {
70 	KVM_RUN_DEFAULT,
71 	KVM_RUN_SANDBOX,
72 };
73 
74 static int img_name_parser(const struct option *opt, const char *arg, int unset)
75 {
76 	char path[PATH_MAX];
77 	struct stat st;
78 
79 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
80 
81 	if ((stat(arg, &st) == 0 && S_ISDIR(st.st_mode)) ||
82 	   (stat(path, &st) == 0 && S_ISDIR(st.st_mode)))
83 		return virtio_9p_img_name_parser(opt, arg, unset);
84 	return disk_img_name_parser(opt, arg, unset);
85 }
86 
87 void kvm_run_set_wrapper_sandbox(void)
88 {
89 	kvm_run_wrapper = KVM_RUN_SANDBOX;
90 }
91 
92 static int parse_mem_unit(char **next)
93 {
94 	switch (**next) {
95 	case 'B': case 'b': (*next)++; return 0;
96 	case 'K': case 'k': (*next)++; return KB_SHIFT;
97 	case 'M': case 'm': (*next)++; return MB_SHIFT;
98 	case 'G': case 'g': (*next)++; return GB_SHIFT;
99 	case 'T': case 't': (*next)++; return TB_SHIFT;
100 	case 'P': case 'p': (*next)++; return PB_SHIFT;
101 	}
102 
103 	return MB_SHIFT;
104 }
105 
106 static u64 parse_mem_option(const char *nptr, char **next)
107 {
108 	u64 shift;
109 	u64 val;
110 
111 	errno = 0;
112 	val = strtoull(nptr, next, 10);
113 	if (errno == ERANGE)
114 		die("Memory too large: %s", nptr);
115 	if (*next == nptr)
116 		die("Invalid memory specifier: %s", nptr);
117 
118 	shift = parse_mem_unit(next);
119 	if ((val << shift) < val)
120 		die("Memory too large: %s", nptr);
121 
122 	return val << shift;
123 }
124 
125 static int mem_parser(const struct option *opt, const char *arg, int unset)
126 {
127 	struct kvm *kvm = opt->ptr;
128 	char *next, *nptr;
129 
130 	kvm->cfg.ram_size = parse_mem_option(arg, &next);
131 	if (kvm->cfg.ram_size == 0)
132 		die("Invalid RAM size: %s", arg);
133 
134 	if (kvm__arch_has_cfg_ram_address() && *next == '@') {
135 		next++;
136 		if (*next == '\0')
137 			die("Missing memory address: %s", arg);
138 
139 		nptr = next;
140 		kvm->cfg.ram_addr = parse_mem_option(nptr, &next);
141 	}
142 
143 	if (*next != '\0')
144 		die("Invalid memory specifier: %s", arg);
145 
146 	return 0;
147 }
148 
149 #ifndef OPT_ARCH_RUN
150 #define OPT_ARCH_RUN(...)
151 #endif
152 
153 #ifdef ARCH_HAS_CFG_RAM_ADDRESS
154 #define MEM_OPT_HELP_SHORT	"size[BKMGTP][@addr[BKMGTP]]"
155 #define MEM_OPT_HELP_LONG						\
156 	"Virtual machine memory size and optional base address, both"	\
157 	" measured by default in megabytes (M)"
158 #else
159 #define MEM_OPT_HELP_SHORT	"size[BKMGTP]"
160 #define MEM_OPT_HELP_LONG						\
161 	"Virtual machine memory size, by default measured in"		\
162 	" in megabytes (M)"
163 #endif
164 
165 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
166 #define VIRTIO_TRANS_OPT_HELP_SHORT    "[pci|pci-legacy|mmio|mmio-legacy]"
167 #else
168 #define VIRTIO_TRANS_OPT_HELP_SHORT    "[pci|pci-legacy]"
169 #endif
170 
171 #define BUILD_OPTIONS(name, cfg, kvm)					\
172 	struct option name[] = {					\
173 	OPT_GROUP("Basic options:"),					\
174 	OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name",	\
175 			"A name for the guest"),			\
176 	OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"),	\
177 	OPT_CALLBACK('m', "mem", NULL, MEM_OPT_HELP_SHORT,		\
178 		     MEM_OPT_HELP_LONG, mem_parser, kvm),		\
179 	OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk "	\
180 			" image or rootfs directory", img_name_parser,	\
181 			kvm),						\
182 	OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio"	\
183 			" balloon"),					\
184 	OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\
185 	OPT_BOOLEAN('\0', "gtk", &(cfg)->gtk, "Enable GTK framebuffer"),\
186 	OPT_BOOLEAN('\0', "sdl", &(cfg)->sdl, "Enable SDL framebuffer"),\
187 	OPT_BOOLEAN('\0', "rng", &(cfg)->virtio_rng, "Enable virtio"	\
188 			" Random Number Generator"),			\
189 	OPT_BOOLEAN('\0', "nodefaults", &(cfg)->nodefaults, "Disable"   \
190 			" implicit configuration that cannot be"	\
191 			" disabled otherwise"),				\
192 	OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name",		\
193 		     "Enable virtio 9p to share files between host and"	\
194 		     " guest", virtio_9p_rootdir_parser, kvm),		\
195 	OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or"\
196 			" hv", "Console to use"),			\
197 	OPT_U64('\0', "vsock", &(cfg)->vsock_cid,			\
198 			"Guest virtio socket CID"),			\
199 	OPT_STRING('\0', "dev", &(cfg)->dev, "device_file",		\
200 			"KVM device file"),				\
201 	OPT_CALLBACK('\0', "tty", NULL, "tty id",			\
202 		     "Remap guest TTY into a pty on the host",		\
203 		     tty_parser, NULL),					\
204 	OPT_STRING('\0', "sandbox", &(cfg)->sandbox, "script",		\
205 			"Run this script when booting into custom"	\
206 			" rootfs"),					\
207 	OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path",	\
208 			"Hugetlbfs path"),				\
209 	OPT_CALLBACK_NOOPT('\0', "virtio-legacy",			\
210 			   &(cfg)->virtio_transport, '\0',		\
211 			   "Use legacy virtio transport (Deprecated:"	\
212 			   " Use --virtio-transport option instead)",	\
213 			   virtio_transport_parser, NULL),		\
214 	OPT_CALLBACK('\0', "virtio-transport", &(cfg)->virtio_transport,\
215 		     VIRTIO_TRANS_OPT_HELP_SHORT,		        \
216 		     "Type of virtio transport",			\
217 		     virtio_transport_parser, NULL),			\
218 									\
219 	OPT_GROUP("Kernel options:"),					\
220 	OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel",	\
221 			"Kernel to boot in virtual machine"),		\
222 	OPT_STRING('i', "initrd", &(cfg)->initrd_filename, "initrd",	\
223 			"Initial RAM disk image"),			\
224 	OPT_STRING('p', "params", &(cfg)->kernel_cmdline, "params",	\
225 			"Kernel command line arguments"),		\
226 	OPT_STRING('f', "firmware", &(cfg)->firmware_filename, "firmware",\
227 			"Firmware image to boot in virtual machine"),	\
228 	OPT_STRING('F', "flash", &(cfg)->flash_filename, "flash",\
229 			"Flash image to present to virtual machine"),	\
230 									\
231 	OPT_GROUP("Networking options:"),				\
232 	OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params",	\
233 		     "Create a new guest NIC",				\
234 		     netdev_parser, NULL, kvm),				\
235 	OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel"	\
236 			" DHCP in rootfs mode"),			\
237 									\
238 	OPT_GROUP("VFIO options:"),					\
239 	OPT_CALLBACK('\0', "vfio-pci", NULL, "[domain:]bus:dev.fn",	\
240 		     "Assign a PCI device to the virtual machine",	\
241 		     vfio_device_parser, kvm),				\
242 									\
243 	OPT_GROUP("Debug options:"),					\
244 	OPT_BOOLEAN('\0', "debug", &do_debug_print,			\
245 			"Enable debug messages"),			\
246 	OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step,	\
247 			"Enable single stepping"),			\
248 	OPT_BOOLEAN('\0', "debug-ioport", &(cfg)->ioport_debug,		\
249 			"Enable ioport debugging"),			\
250 	OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug,		\
251 			"Enable MMIO debugging"),			\
252 	OPT_INTEGER('\0', "debug-iodelay", &(cfg)->debug_iodelay,	\
253 			"Delay IO by millisecond"),			\
254 									\
255 	OPT_ARCH(RUN, cfg)						\
256 	OPT_END()							\
257 	};
258 
259 static void *kvm_cpu_thread(void *arg)
260 {
261 	char name[16];
262 
263 	current_kvm_cpu = arg;
264 
265 	sprintf(name, "kvm-vcpu-%lu", current_kvm_cpu->cpu_id);
266 	kvm__set_thread_name(name);
267 
268 	if (kvm_cpu__start(current_kvm_cpu))
269 		goto panic_kvm;
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%llu\n",
279 			(unsigned long long)current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
280 
281 	kvm_cpu__set_debug_fd(STDOUT_FILENO);
282 	kvm_cpu__show_registers(current_kvm_cpu);
283 	kvm_cpu__show_code(current_kvm_cpu);
284 	kvm_cpu__show_page_tables(current_kvm_cpu);
285 
286 	return (void *) (intptr_t) 1;
287 }
288 
289 static char kernel[PATH_MAX];
290 
291 static const char *host_kernels[] = {
292 	"/boot/vmlinuz",
293 	"/boot/bzImage",
294 	NULL
295 };
296 
297 static const char *default_kernels[] = {
298 	"./bzImage",
299 	"arch/" BUILD_ARCH "/boot/bzImage",
300 	"../../arch/" BUILD_ARCH "/boot/bzImage",
301 	NULL
302 };
303 
304 static const char *default_vmlinux[] = {
305 	"vmlinux",
306 	"../../../vmlinux",
307 	"../../vmlinux",
308 	NULL
309 };
310 
311 static void kernel_usage_with_options(void)
312 {
313 	const char **k;
314 	struct utsname uts;
315 
316 	fprintf(stderr, "Fatal: could not find default kernel image in:\n");
317 	k = &default_kernels[0];
318 	while (*k) {
319 		fprintf(stderr, "\t%s\n", *k);
320 		k++;
321 	}
322 
323 	if (uname(&uts) < 0)
324 		return;
325 
326 	k = &host_kernels[0];
327 	while (*k) {
328 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
329 			return;
330 		fprintf(stderr, "\t%s\n", kernel);
331 		k++;
332 	}
333 	fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n",
334 		KVM_BINARY_NAME);
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 (u64)nr_pages * page_size;
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	= (u64)SZ_64M * (nr_cpus + 3);
369 
370 	available	= host_ram_size() * RAM_SIZE_RATIO;
371 	if (!available)
372 		available = MIN_RAM_SIZE;
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 		strlcpy(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 void kvm_run_help(void)
432 {
433 	struct kvm *kvm = NULL;
434 
435 	BUILD_OPTIONS(options, &kvm->cfg, kvm);
436 	usage_with_options(run_usage, options);
437 }
438 
439 static int kvm_run_set_sandbox(struct kvm *kvm)
440 {
441 	const char *guestfs_name = kvm->cfg.custom_rootfs_name;
442 	char path[PATH_MAX], script[PATH_MAX], *tmp;
443 
444 	snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name);
445 
446 	remove(path);
447 
448 	if (kvm->cfg.sandbox == NULL)
449 		return 0;
450 
451 	tmp = realpath(kvm->cfg.sandbox, NULL);
452 	if (tmp == NULL)
453 		return -ENOMEM;
454 
455 	snprintf(script, PATH_MAX, "/host/%s", tmp);
456 	free(tmp);
457 
458 	return symlink(script, path);
459 }
460 
461 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg)
462 {
463 	const char *single_quote;
464 
465 	if (!*arg) { /* zero length string */
466 		if (write(fd, "''", 2) <= 0)
467 			die("Failed writing sandbox script");
468 		return;
469 	}
470 
471 	while (*arg) {
472 		single_quote = strchrnul(arg, '\'');
473 
474 		/* write non-single-quote string as #('string') */
475 		if (arg != single_quote) {
476 			if (write(fd, "'", 1) <= 0 ||
477 			    write(fd, arg, single_quote - arg) <= 0 ||
478 			    write(fd, "'", 1) <= 0)
479 				die("Failed writing sandbox script");
480 		}
481 
482 		/* write single quote as #("'") */
483 		if (*single_quote) {
484 			if (write(fd, "\"'\"", 3) <= 0)
485 				die("Failed writing sandbox script");
486 		} else
487 			break;
488 
489 		arg = single_quote + 1;
490 	}
491 }
492 
493 static void resolve_program(const char *src, char *dst, size_t len)
494 {
495 	struct stat st;
496 	int err;
497 
498 	err = stat(src, &st);
499 
500 	if (!err && S_ISREG(st.st_mode)) {
501 		char resolved_path[PATH_MAX];
502 
503 		if (!realpath(src, resolved_path))
504 			die("Unable to resolve program %s: %s\n", src, strerror(errno));
505 
506 		if (snprintf(dst, len, "/host%s", resolved_path) >= (int)len)
507 			die("Pathname too long: %s -> %s\n", src, resolved_path);
508 
509 	} else
510 		strlcpy(dst, src, len);
511 }
512 
513 static void kvm_run_write_sandbox_cmd(struct kvm *kvm, const char **argv, int argc)
514 {
515 	const char script_hdr[] = "#! /bin/bash\n\n";
516 	char program[PATH_MAX];
517 	int fd;
518 
519 	remove(kvm->cfg.sandbox);
520 
521 	fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777);
522 	if (fd < 0)
523 		die("Failed creating sandbox script");
524 
525 	if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0)
526 		die("Failed writing sandbox script");
527 
528 	resolve_program(argv[0], program, PATH_MAX);
529 	kvm_write_sandbox_cmd_exactly(fd, program);
530 
531 	argv++;
532 	argc--;
533 
534 	while (argc) {
535 		if (write(fd, " ", 1) <= 0)
536 			die("Failed writing sandbox script");
537 
538 		kvm_write_sandbox_cmd_exactly(fd, argv[0]);
539 		argv++;
540 		argc--;
541 	}
542 	if (write(fd, "\n", 1) <= 0)
543 		die("Failed writing sandbox script");
544 
545 	close(fd);
546 }
547 
548 static void kvm_run_set_real_cmdline(struct kvm *kvm)
549 {
550 	static char real_cmdline[2048];
551 	bool video;
552 
553 	video = kvm->cfg.vnc || kvm->cfg.sdl || kvm->cfg.gtk;
554 
555 	memset(real_cmdline, 0, sizeof(real_cmdline));
556 	kvm__arch_set_cmdline(real_cmdline, video);
557 
558 	if (video) {
559 		strcat(real_cmdline, " console=tty0");
560 	} else {
561 		switch (kvm->cfg.active_console) {
562 		case CONSOLE_HV:
563 			/* Fallthrough */
564 		case CONSOLE_VIRTIO:
565 			strcat(real_cmdline, " console=hvc0");
566 			break;
567 		case CONSOLE_8250:
568 			strcat(real_cmdline, " console=ttyS0");
569 			break;
570 		}
571 	}
572 
573 	if (kvm->cfg.using_rootfs) {
574 		strcat(real_cmdline, " rw rootflags=trans=virtio,version=9p2000.L,cache=loose rootfstype=9p");
575 		if (kvm->cfg.custom_rootfs) {
576 #ifdef CONFIG_GUEST_PRE_INIT
577 			strcat(real_cmdline, " init=/virt/pre_init");
578 #else
579 			strcat(real_cmdline, " init=/virt/init");
580 #endif
581 			if (!kvm->cfg.no_dhcp)
582 				strcat(real_cmdline, "  ip=dhcp");
583 		}
584 	} else if (!kvm->cfg.kernel_cmdline || !strstr(kvm->cfg.kernel_cmdline, "root=")) {
585 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
586 	}
587 
588 	if (kvm->cfg.kernel_cmdline) {
589 		strcat(real_cmdline, " ");
590 		strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline));
591 	}
592 
593 	kvm->cfg.real_cmdline = real_cmdline;
594 }
595 
596 static void kvm_run_validate_cfg(struct kvm *kvm)
597 {
598 	u64 available_ram;
599 
600 	if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename)
601 		die("Only one of --kernel or --firmware can be specified");
602 
603 	if ((kvm->cfg.vnc && (kvm->cfg.sdl || kvm->cfg.gtk)) ||
604 	    (kvm->cfg.sdl && kvm->cfg.gtk))
605 		die("Only one of --vnc, --sdl or --gtk can be specified");
606 
607 	if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename)
608 		pr_warning("Ignoring initrd file when loading a firmware image");
609 
610 	if (kvm->cfg.ram_size) {
611 		available_ram = host_ram_size();
612 		if (available_ram && kvm->cfg.ram_size > available_ram) {
613 			pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB",
614 				(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
615 				(unsigned long long)available_ram >> MB_SHIFT);
616 		}
617 	}
618 
619 	kvm__arch_validate_cfg(kvm);
620 }
621 
622 static struct kvm *kvm_cmd_run_init(int argc, const char **argv)
623 {
624 	static char default_name[20];
625 	unsigned int nr_online_cpus;
626 	struct kvm *kvm = kvm__new();
627 
628 	if (IS_ERR(kvm))
629 		return kvm;
630 
631 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
632 	kvm->cfg.custom_rootfs_name = "default";
633 	/*
634 	 * An architecture can allow the user to set the RAM base address to
635 	 * zero. Initialize the address before parsing the command line
636 	 * arguments, otherwise it will be impossible to distinguish between the
637 	 * user setting the base address to zero or letting it unset and using
638 	 * the default value.
639 	 */
640 	kvm->cfg.ram_addr = kvm__arch_default_ram_address();
641 
642 	while (argc != 0) {
643 		BUILD_OPTIONS(options, &kvm->cfg, kvm);
644 		argc = parse_options(argc, argv, options, run_usage,
645 				PARSE_OPT_STOP_AT_NON_OPTION |
646 				PARSE_OPT_KEEP_DASHDASH);
647 		if (argc != 0) {
648 			/* Cusrom options, should have been handled elsewhere */
649 			if (strcmp(argv[0], "--") == 0) {
650 				if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
651 					kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
652 					kvm_run_write_sandbox_cmd(kvm, argv+1, argc-1);
653 					break;
654 				}
655 			}
656 
657 			if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) ||
658 				(kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) {
659 				fprintf(stderr, "Cannot handle parameter: "
660 						"%s\n", argv[0]);
661 				usage_with_options(run_usage, options);
662 				free(kvm);
663 				return ERR_PTR(-EINVAL);
664 			}
665 			if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
666 				/*
667 				 * first unhandled parameter is treated as
668 				 * sandbox command
669 				 */
670 				kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
671 				kvm_run_write_sandbox_cmd(kvm, argv, argc);
672 			} else {
673 				/*
674 				 * first unhandled parameter is treated as a kernel
675 				 * image
676 				 */
677 				kvm->cfg.kernel_filename = argv[0];
678 			}
679 			argv++;
680 			argc--;
681 		}
682 
683 	}
684 
685 	kvm_run_validate_cfg(kvm);
686 
687 	if (!kvm->cfg.kernel_filename && !kvm->cfg.firmware_filename) {
688 		kvm->cfg.kernel_filename = find_kernel();
689 
690 		if (!kvm->cfg.kernel_filename) {
691 			kernel_usage_with_options();
692 			return ERR_PTR(-EINVAL);
693 		}
694 	}
695 
696 	if (kvm->cfg.kernel_filename) {
697 		kvm->cfg.vmlinux_filename = find_vmlinux();
698 		kvm->vmlinux = kvm->cfg.vmlinux_filename;
699 	}
700 
701 	if (kvm->cfg.nrcpus == 0)
702 		kvm->cfg.nrcpus = nr_online_cpus;
703 
704 	if (!kvm->cfg.ram_size)
705 		kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus);
706 
707 	if (!kvm->cfg.dev)
708 		kvm->cfg.dev = DEFAULT_KVM_DEV;
709 
710 	if (!kvm->cfg.console)
711 		kvm->cfg.console = DEFAULT_CONSOLE;
712 
713 	if (!strncmp(kvm->cfg.console, "virtio", 6))
714 		kvm->cfg.active_console  = CONSOLE_VIRTIO;
715 	else if (!strncmp(kvm->cfg.console, "serial", 6))
716 		kvm->cfg.active_console  = CONSOLE_8250;
717 	else if (!strncmp(kvm->cfg.console, "hv", 2))
718 		kvm->cfg.active_console = CONSOLE_HV;
719 	else
720 		pr_warning("No console!");
721 
722 	if (!kvm->cfg.host_ip)
723 		kvm->cfg.host_ip = DEFAULT_HOST_ADDR;
724 
725 	if (!kvm->cfg.guest_ip)
726 		kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR;
727 
728 	if (!kvm->cfg.guest_mac)
729 		kvm->cfg.guest_mac = DEFAULT_GUEST_MAC;
730 
731 	if (!kvm->cfg.host_mac)
732 		kvm->cfg.host_mac = DEFAULT_HOST_MAC;
733 
734 	if (!kvm->cfg.script)
735 		kvm->cfg.script = DEFAULT_SCRIPT;
736 
737 	if (!kvm->cfg.network)
738                 kvm->cfg.network = DEFAULT_NETWORK;
739 
740 	if (!kvm->cfg.guest_name) {
741 		if (kvm->cfg.custom_rootfs) {
742 			kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name;
743 		} else {
744 			sprintf(default_name, "guest-%u", getpid());
745 			kvm->cfg.guest_name = default_name;
746 		}
747 	}
748 
749 	if (!kvm->cfg.nodefaults &&
750 	    !kvm->cfg.using_rootfs &&
751 	    !kvm->cfg.disk_image[0].filename &&
752 	    !kvm->cfg.initrd_filename) {
753 		char tmp[PATH_MAX];
754 
755 		kvm_setup_create_new(kvm->cfg.custom_rootfs_name);
756 		kvm_setup_resolv(kvm->cfg.custom_rootfs_name);
757 
758 		snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default");
759 		if (virtio_9p__register(kvm, tmp, "/dev/root") < 0)
760 			die("Unable to initialize virtio 9p");
761 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
762 			die("Unable to initialize virtio 9p");
763 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
764 	}
765 
766 	if (kvm->cfg.custom_rootfs) {
767 		kvm_run_set_sandbox(kvm);
768 		if (kvm_setup_guest_init(kvm->cfg.custom_rootfs_name))
769 			die("Failed to setup init for guest.");
770 	}
771 
772 	if (kvm->cfg.nodefaults)
773 		kvm->cfg.real_cmdline = kvm->cfg.kernel_cmdline;
774 	else
775 		kvm_run_set_real_cmdline(kvm);
776 
777 	if (kvm->cfg.kernel_filename) {
778 		printf("  # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
779 		       kvm->cfg.kernel_filename,
780 		       (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
781 		       kvm->cfg.nrcpus, kvm->cfg.guest_name);
782 	} else if (kvm->cfg.firmware_filename) {
783 		printf("  # %s run --firmware %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
784 		       kvm->cfg.firmware_filename,
785 		       (unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
786 		       kvm->cfg.nrcpus, kvm->cfg.guest_name);
787 	}
788 
789 	if (init_list__init(kvm) < 0)
790 		die ("Initialisation failed");
791 
792 	return kvm;
793 }
794 
795 static int kvm_cmd_run_work(struct kvm *kvm)
796 {
797 	int i;
798 
799 	for (i = 0; i < kvm->nrcpus; i++) {
800 		if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0)
801 			die("unable to create KVM VCPU thread");
802 	}
803 
804 	/* Only VCPU #0 is going to exit by itself when shutting down */
805 	if (pthread_join(kvm->cpus[0]->thread, NULL) != 0)
806 		die("unable to join with vcpu 0");
807 
808 	return kvm_cpu__exit(kvm);
809 }
810 
811 static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret)
812 {
813 	compat__print_all_messages();
814 
815 	init_list__exit(kvm);
816 
817 	if (guest_ret == 0)
818 		printf("\n  # KVM session ended normally.\n");
819 }
820 
821 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
822 {
823 	int ret = -EFAULT;
824 	struct kvm *kvm;
825 
826 	kvm = kvm_cmd_run_init(argc, argv);
827 	if (IS_ERR(kvm))
828 		return PTR_ERR(kvm);
829 
830 	ret = kvm_cmd_run_work(kvm);
831 	kvm_cmd_run_exit(kvm, ret);
832 
833 	return ret;
834 }
835