xref: /kvmtool/builtin-run.c (revision b6bae725decc42a4b5bc76058afb4c9d6abe4645)
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 long host_page_size(void)
364 {
365 	long page_size = sysconf(_SC_PAGE_SIZE);
366 
367 	if (page_size < 0) {
368 		pr_warning("sysconf(_SC_PAGE_SIZE) failed");
369 		return 0;
370 	}
371 
372 	return page_size;
373 }
374 
375 static u64 host_ram_size(void)
376 {
377 	long page_size = host_page_size();
378 	long nr_pages;
379 
380 	nr_pages	= sysconf(_SC_PHYS_PAGES);
381 	if (nr_pages < 0) {
382 		pr_warning("sysconf(_SC_PHYS_PAGES) failed");
383 		return 0;
384 	}
385 
386 	return (u64)nr_pages * page_size;
387 }
388 
389 /*
390  * If user didn't specify how much memory it wants to allocate for the guest,
391  * avoid filling the whole host RAM.
392  */
393 #define RAM_SIZE_RATIO		0.8
394 
395 static u64 get_ram_size(int nr_cpus)
396 {
397 	u64 available;
398 	u64 ram_size;
399 
400 	ram_size	= (u64)SZ_64M * (nr_cpus + 3);
401 
402 	available	= host_ram_size() * RAM_SIZE_RATIO;
403 	if (!available)
404 		available = MIN_RAM_SIZE;
405 
406 	if (ram_size > available)
407 		ram_size	= available;
408 
409 	return ram_size;
410 }
411 
412 static const char *find_kernel(void)
413 {
414 	const char **k;
415 	struct stat st;
416 	struct utsname uts;
417 
418 	k = &default_kernels[0];
419 	while (*k) {
420 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
421 			k++;
422 			continue;
423 		}
424 		strlcpy(kernel, *k, PATH_MAX);
425 		return kernel;
426 	}
427 
428 	if (uname(&uts) < 0)
429 		return NULL;
430 
431 	k = &host_kernels[0];
432 	while (*k) {
433 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
434 			return NULL;
435 
436 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
437 			k++;
438 			continue;
439 		}
440 		return kernel;
441 
442 	}
443 	return NULL;
444 }
445 
446 static const char *find_vmlinux(void)
447 {
448 	const char **vmlinux;
449 
450 	vmlinux = &default_vmlinux[0];
451 	while (*vmlinux) {
452 		struct stat st;
453 
454 		if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
455 			vmlinux++;
456 			continue;
457 		}
458 		return *vmlinux;
459 	}
460 	return NULL;
461 }
462 
463 void kvm_run_help(void)
464 {
465 	struct kvm *kvm = NULL;
466 
467 	BUILD_OPTIONS(options, &kvm->cfg, kvm);
468 	usage_with_options(run_usage, options);
469 }
470 
471 static int kvm_run_set_sandbox(struct kvm *kvm)
472 {
473 	const char *guestfs_name = kvm->cfg.custom_rootfs_name;
474 	char path[PATH_MAX], script[PATH_MAX], *tmp;
475 
476 	snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name);
477 
478 	remove(path);
479 
480 	if (kvm->cfg.sandbox == NULL)
481 		return 0;
482 
483 	tmp = realpath(kvm->cfg.sandbox, NULL);
484 	if (tmp == NULL)
485 		return -ENOMEM;
486 
487 	snprintf(script, PATH_MAX, "/host/%s", tmp);
488 	free(tmp);
489 
490 	return symlink(script, path);
491 }
492 
493 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg)
494 {
495 	const char *single_quote;
496 
497 	if (!*arg) { /* zero length string */
498 		if (write(fd, "''", 2) <= 0)
499 			die("Failed writing sandbox script");
500 		return;
501 	}
502 
503 	while (*arg) {
504 		single_quote = strchrnul(arg, '\'');
505 
506 		/* write non-single-quote string as #('string') */
507 		if (arg != single_quote) {
508 			if (write(fd, "'", 1) <= 0 ||
509 			    write(fd, arg, single_quote - arg) <= 0 ||
510 			    write(fd, "'", 1) <= 0)
511 				die("Failed writing sandbox script");
512 		}
513 
514 		/* write single quote as #("'") */
515 		if (*single_quote) {
516 			if (write(fd, "\"'\"", 3) <= 0)
517 				die("Failed writing sandbox script");
518 		} else
519 			break;
520 
521 		arg = single_quote + 1;
522 	}
523 }
524 
525 static void resolve_program(const char *src, char *dst, size_t len)
526 {
527 	struct stat st;
528 	int err;
529 
530 	err = stat(src, &st);
531 
532 	if (!err && S_ISREG(st.st_mode)) {
533 		char resolved_path[PATH_MAX];
534 
535 		if (!realpath(src, resolved_path))
536 			die("Unable to resolve program %s: %s\n", src, strerror(errno));
537 
538 		if (snprintf(dst, len, "/host%s", resolved_path) >= (int)len)
539 			die("Pathname too long: %s -> %s\n", src, resolved_path);
540 
541 	} else
542 		strlcpy(dst, src, len);
543 }
544 
545 static void kvm_run_write_sandbox_cmd(struct kvm *kvm, const char **argv, int argc)
546 {
547 	const char script_hdr[] = "#! /bin/bash\n\n";
548 	char program[PATH_MAX];
549 	int fd;
550 
551 	remove(kvm->cfg.sandbox);
552 
553 	fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777);
554 	if (fd < 0)
555 		die("Failed creating sandbox script");
556 
557 	if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0)
558 		die("Failed writing sandbox script");
559 
560 	resolve_program(argv[0], program, PATH_MAX);
561 	kvm_write_sandbox_cmd_exactly(fd, program);
562 
563 	argv++;
564 	argc--;
565 
566 	while (argc) {
567 		if (write(fd, " ", 1) <= 0)
568 			die("Failed writing sandbox script");
569 
570 		kvm_write_sandbox_cmd_exactly(fd, argv[0]);
571 		argv++;
572 		argc--;
573 	}
574 	if (write(fd, "\n", 1) <= 0)
575 		die("Failed writing sandbox script");
576 
577 	close(fd);
578 }
579 
580 static void kvm_run_set_real_cmdline(struct kvm *kvm)
581 {
582 	static char real_cmdline[2048];
583 	bool video;
584 
585 	video = kvm->cfg.vnc || kvm->cfg.sdl || kvm->cfg.gtk;
586 
587 	memset(real_cmdline, 0, sizeof(real_cmdline));
588 	kvm__arch_set_cmdline(real_cmdline, video);
589 
590 	if (video) {
591 		strcat(real_cmdline, " console=tty0");
592 	} else {
593 		switch (kvm->cfg.active_console) {
594 		case CONSOLE_HV:
595 			/* Fallthrough */
596 		case CONSOLE_VIRTIO:
597 			strcat(real_cmdline, " console=hvc0");
598 			break;
599 		case CONSOLE_8250:
600 			strcat(real_cmdline, " console=ttyS0");
601 			break;
602 		}
603 	}
604 
605 	if (kvm->cfg.using_rootfs) {
606 		strcat(real_cmdline, " rw rootflags=trans=virtio,version=9p2000.L,cache=loose rootfstype=9p");
607 		if (kvm->cfg.custom_rootfs) {
608 #ifdef CONFIG_GUEST_PRE_INIT
609 			strcat(real_cmdline, " init=/virt/pre_init");
610 #else
611 			strcat(real_cmdline, " init=/virt/init");
612 #endif
613 			if (!kvm->cfg.no_dhcp)
614 				strcat(real_cmdline, "  ip=dhcp");
615 		}
616 	} else if (!kvm->cfg.kernel_cmdline || !strstr(kvm->cfg.kernel_cmdline, "root=")) {
617 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
618 	}
619 
620 	if (kvm->cfg.kernel_cmdline) {
621 		strcat(real_cmdline, " ");
622 		strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline));
623 	}
624 
625 	kvm->cfg.real_cmdline = real_cmdline;
626 }
627 
628 static void kvm_run_validate_cfg(struct kvm *kvm)
629 {
630 	u64 available_ram;
631 
632 	if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename)
633 		die("Only one of --kernel or --firmware can be specified");
634 
635 	if ((kvm->cfg.vnc && (kvm->cfg.sdl || kvm->cfg.gtk)) ||
636 	    (kvm->cfg.sdl && kvm->cfg.gtk))
637 		die("Only one of --vnc, --sdl or --gtk can be specified");
638 
639 	if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename)
640 		pr_warning("Ignoring initrd file when loading a firmware image");
641 
642 	if (kvm->cfg.ram_size) {
643 		available_ram = host_ram_size();
644 		if (available_ram && kvm->cfg.ram_size > available_ram) {
645 			pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB",
646 				(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
647 				(unsigned long long)available_ram >> MB_SHIFT);
648 		}
649 	}
650 
651 	kvm__arch_validate_cfg(kvm);
652 }
653 
654 static struct kvm *kvm_cmd_run_init(int argc, const char **argv)
655 {
656 	static char default_name[20];
657 	unsigned int nr_online_cpus;
658 	struct kvm *kvm = kvm__new();
659 
660 	if (IS_ERR(kvm))
661 		return kvm;
662 
663 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
664 	kvm->cfg.custom_rootfs_name = "default";
665 	/*
666 	 * An architecture can allow the user to set the RAM base address to
667 	 * zero. Initialize the address before parsing the command line
668 	 * arguments, otherwise it will be impossible to distinguish between the
669 	 * user setting the base address to zero or letting it unset and using
670 	 * the default value.
671 	 */
672 	kvm->cfg.ram_addr = kvm__arch_default_ram_address();
673 
674 	while (argc != 0) {
675 		BUILD_OPTIONS(options, &kvm->cfg, kvm);
676 		argc = parse_options(argc, argv, options, run_usage,
677 				PARSE_OPT_STOP_AT_NON_OPTION |
678 				PARSE_OPT_KEEP_DASHDASH);
679 		if (argc != 0) {
680 			/* Cusrom options, should have been handled elsewhere */
681 			if (strcmp(argv[0], "--") == 0) {
682 				if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
683 					kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
684 					kvm_run_write_sandbox_cmd(kvm, argv+1, argc-1);
685 					break;
686 				}
687 			}
688 
689 			if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) ||
690 				(kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) {
691 				pr_err("Cannot handle parameter: %s", argv[0]);
692 				usage_with_options(run_usage, options);
693 				free(kvm);
694 				return ERR_PTR(-EINVAL);
695 			}
696 			if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
697 				/*
698 				 * first unhandled parameter is treated as
699 				 * sandbox command
700 				 */
701 				kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
702 				kvm_run_write_sandbox_cmd(kvm, argv, argc);
703 			} else {
704 				/*
705 				 * first unhandled parameter is treated as a kernel
706 				 * image
707 				 */
708 				kvm->cfg.kernel_filename = argv[0];
709 			}
710 			argv++;
711 			argc--;
712 		}
713 
714 	}
715 
716 	kvm_run_validate_cfg(kvm);
717 
718 	if (!kvm->cfg.kernel_filename && !kvm->cfg.firmware_filename) {
719 		kvm->cfg.kernel_filename = find_kernel();
720 
721 		if (!kvm->cfg.kernel_filename) {
722 			kernel_usage_with_options();
723 			return ERR_PTR(-EINVAL);
724 		}
725 	}
726 
727 	if (kvm->cfg.kernel_filename) {
728 		kvm->cfg.vmlinux_filename = find_vmlinux();
729 		kvm->vmlinux = kvm->cfg.vmlinux_filename;
730 	}
731 
732 	if (kvm->cfg.nrcpus == 0)
733 		kvm->cfg.nrcpus = nr_online_cpus;
734 
735 	if (!kvm->cfg.ram_size)
736 		kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus);
737 
738 	if (!kvm->cfg.dev)
739 		kvm->cfg.dev = DEFAULT_KVM_DEV;
740 
741 	if (!kvm->cfg.console)
742 		kvm->cfg.console = DEFAULT_CONSOLE;
743 
744 	if (!strncmp(kvm->cfg.console, "virtio", 6))
745 		kvm->cfg.active_console  = CONSOLE_VIRTIO;
746 	else if (!strncmp(kvm->cfg.console, "serial", 6))
747 		kvm->cfg.active_console  = CONSOLE_8250;
748 	else if (!strncmp(kvm->cfg.console, "hv", 2))
749 		kvm->cfg.active_console = CONSOLE_HV;
750 	else
751 		pr_warning("No console!");
752 
753 	if (!kvm->cfg.host_ip)
754 		kvm->cfg.host_ip = DEFAULT_HOST_ADDR;
755 
756 	if (!kvm->cfg.guest_ip)
757 		kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR;
758 
759 	if (!kvm->cfg.guest_mac)
760 		kvm->cfg.guest_mac = DEFAULT_GUEST_MAC;
761 
762 	if (!kvm->cfg.host_mac)
763 		kvm->cfg.host_mac = DEFAULT_HOST_MAC;
764 
765 	if (!kvm->cfg.script)
766 		kvm->cfg.script = DEFAULT_SCRIPT;
767 
768 	if (!kvm->cfg.network)
769                 kvm->cfg.network = DEFAULT_NETWORK;
770 
771 	if (!kvm->cfg.guest_name) {
772 		if (kvm->cfg.custom_rootfs) {
773 			kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name;
774 		} else {
775 			sprintf(default_name, "guest-%u", getpid());
776 			kvm->cfg.guest_name = default_name;
777 		}
778 	}
779 
780 	if (!kvm->cfg.nodefaults &&
781 	    !kvm->cfg.using_rootfs &&
782 	    !kvm->cfg.disk_image[0].filename &&
783 	    !kvm->cfg.initrd_filename) {
784 		char tmp[PATH_MAX];
785 
786 		kvm_setup_create_new(kvm->cfg.custom_rootfs_name);
787 		kvm_setup_resolv(kvm->cfg.custom_rootfs_name);
788 
789 		snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default");
790 		if (virtio_9p__register(kvm, tmp, "/dev/root") < 0)
791 			die("Unable to initialize virtio 9p");
792 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
793 			die("Unable to initialize virtio 9p");
794 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
795 	}
796 
797 	if (kvm->cfg.custom_rootfs) {
798 		kvm_run_set_sandbox(kvm);
799 		if (kvm_setup_guest_init(kvm->cfg.custom_rootfs_name))
800 			die("Failed to setup init for guest.");
801 	}
802 
803 	if (kvm->cfg.nodefaults)
804 		kvm->cfg.real_cmdline = kvm->cfg.kernel_cmdline;
805 	else
806 		kvm_run_set_real_cmdline(kvm);
807 
808 	if (kvm->cfg.kernel_filename) {
809 		pr_info("# %s run -k %s -m %Lu -c %d --name %s", KVM_BINARY_NAME,
810 			kvm->cfg.kernel_filename,
811 			(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
812 			kvm->cfg.nrcpus, kvm->cfg.guest_name);
813 	} else if (kvm->cfg.firmware_filename) {
814 		pr_info("# %s run --firmware %s -m %Lu -c %d --name %s", KVM_BINARY_NAME,
815 			kvm->cfg.firmware_filename,
816 			(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
817 			kvm->cfg.nrcpus, kvm->cfg.guest_name);
818 	}
819 
820 	if (init_list__init(kvm) < 0)
821 		die ("Initialisation failed");
822 
823 	return kvm;
824 }
825 
826 static int kvm_cmd_run_work(struct kvm *kvm)
827 {
828 	int i;
829 
830 	for (i = 0; i < kvm->nrcpus; i++) {
831 		if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0)
832 			die("unable to create KVM VCPU thread");
833 	}
834 
835 	/* Only VCPU #0 is going to exit by itself when shutting down */
836 	if (pthread_join(kvm->cpus[0]->thread, NULL) != 0)
837 		die("unable to join with vcpu 0");
838 
839 	return kvm_cpu__exit(kvm);
840 }
841 
842 static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret)
843 {
844 	compat__print_all_messages();
845 
846 	init_list__exit(kvm);
847 
848 	if (guest_ret == 0)
849 		pr_info("KVM session ended normally.");
850 }
851 
852 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
853 {
854 	int ret = -EFAULT;
855 	struct kvm *kvm;
856 
857 	kvm = kvm_cmd_run_init(argc, argv);
858 	if (IS_ERR(kvm))
859 		return PTR_ERR(kvm);
860 
861 	ret = kvm_cmd_run_work(kvm);
862 	kvm_cmd_run_exit(kvm, ret);
863 
864 	return ret;
865 }
866