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