xref: /kvmtool/builtin-run.c (revision c7b7a542cdcd8ebe55cc3e7b49cabba15725c2ca)
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 
img_name_parser(const struct option * opt,const char * arg,int unset)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 
kvm_run_set_wrapper_sandbox(void)86 void kvm_run_set_wrapper_sandbox(void)
87 {
88 	kvm_run_wrapper = KVM_RUN_SANDBOX;
89 }
90 
parse_mem_unit(char ** next)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 
parse_mem_option(const char * nptr,char ** next)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 
mem_parser(const struct option * opt,const char * arg,int unset)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 
loglevel_parser(const struct option * opt,const char * arg,int unset)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. Pass mode=none to disable"\
256 		     " all network devices",				\
257 		     netdev_parser, NULL, kvm),				\
258 	OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel"	\
259 			" DHCP in rootfs mode"),			\
260 									\
261 	OPT_GROUP("VFIO options:"),					\
262 	OPT_CALLBACK('\0', "vfio-pci", NULL, "[domain:]bus:dev.fn",	\
263 		     "Assign a PCI device to the virtual machine",	\
264 		     vfio_device_parser, kvm),				\
265 									\
266 	OPT_GROUP("Debug options:"),					\
267 	OPT_CALLBACK_NOOPT('\0', "debug", kvm, NULL,			\
268 			"Enable debug messages (deprecated, use "	\
269 			"--loglevel=debug instead)",			\
270 			loglevel_parser, NULL),				\
271 	OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step,	\
272 			"Enable single stepping"),			\
273 	OPT_BOOLEAN('\0', "debug-ioport", &(cfg)->ioport_debug,		\
274 			"Enable ioport debugging"),			\
275 	OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug,		\
276 			"Enable MMIO debugging"),			\
277 	OPT_INTEGER('\0', "debug-iodelay", &(cfg)->debug_iodelay,	\
278 			"Delay IO by millisecond"),			\
279 									\
280 	OPT_ARCH(RUN, cfg)						\
281 	OPT_END()							\
282 	};
283 
kvm_cpu_thread(void * arg)284 static void *kvm_cpu_thread(void *arg)
285 {
286 	char name[16];
287 
288 	current_kvm_cpu = arg;
289 
290 	sprintf(name, "kvm-vcpu-%lu", current_kvm_cpu->cpu_id);
291 	kvm__set_thread_name(name);
292 
293 	if (kvm_cpu__start(current_kvm_cpu))
294 		goto panic_kvm;
295 
296 	return (void *) (intptr_t) 0;
297 
298 panic_kvm:
299 	pr_err("KVM exit reason: %u (\"%s\")",
300 		current_kvm_cpu->kvm_run->exit_reason,
301 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
302 
303 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) {
304 		pr_err("KVM exit code: %llu",
305 			(unsigned long long)current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
306 	}
307 
308 	kvm_cpu__set_debug_fd(STDOUT_FILENO);
309 	kvm_cpu__show_registers(current_kvm_cpu);
310 	kvm_cpu__show_code(current_kvm_cpu);
311 	kvm_cpu__show_page_tables(current_kvm_cpu);
312 
313 	return (void *) (intptr_t) 1;
314 }
315 
316 static char kernel[PATH_MAX];
317 
318 static const char *host_kernels[] = {
319 	"/boot/vmlinuz",
320 	"/boot/bzImage",
321 	NULL
322 };
323 
324 static const char *default_kernels[] = {
325 	"./bzImage",
326 	"arch/" BUILD_ARCH "/boot/bzImage",
327 	"../../arch/" BUILD_ARCH "/boot/bzImage",
328 	NULL
329 };
330 
331 static const char *default_vmlinux[] = {
332 	"vmlinux",
333 	"../../../vmlinux",
334 	"../../vmlinux",
335 	NULL
336 };
337 
kernel_usage_with_options(void)338 static void kernel_usage_with_options(void)
339 {
340 	const char **k;
341 	struct utsname uts;
342 
343 	pr_err("Could not find default kernel image in:");
344 	k = &default_kernels[0];
345 	while (*k) {
346 		pr_err("\t%s", *k);
347 		k++;
348 	}
349 
350 	if (uname(&uts) < 0)
351 		return;
352 
353 	k = &host_kernels[0];
354 	while (*k) {
355 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
356 			return;
357 		pr_err("\t%s", kernel);
358 		k++;
359 	}
360 	pr_info("Please see '%s run --help' for more options.",
361 		KVM_BINARY_NAME);
362 }
363 
host_page_size(void)364 static long host_page_size(void)
365 {
366 	long page_size = sysconf(_SC_PAGE_SIZE);
367 
368 	if (page_size < 0) {
369 		pr_warning("sysconf(_SC_PAGE_SIZE) failed");
370 		return 0;
371 	}
372 
373 	return page_size;
374 }
375 
host_ram_nrpages(void)376 static long host_ram_nrpages(void)
377 {
378 	long nr_pages = sysconf(_SC_PHYS_PAGES);
379 
380 	if (nr_pages < 0) {
381 		pr_warning("sysconf(_SC_PHYS_PAGES) failed");
382 		return 0;
383 	}
384 
385 	return nr_pages;
386 }
387 
host_ram_size(void)388 static u64 host_ram_size(void)
389 {
390 	long page_size = host_page_size();
391 	long nr_pages = host_ram_nrpages();
392 
393 	return (u64)nr_pages * page_size;
394 }
395 
396 /*
397  * If user didn't specify how much memory it wants to allocate for the guest,
398  * avoid filling the whole host RAM.
399  */
400 #define RAM_SIZE_RATIO		0.8
401 
get_ram_size(int nr_cpus)402 static u64 get_ram_size(int nr_cpus)
403 {
404 	long nr_pages_available = host_ram_nrpages() * RAM_SIZE_RATIO;
405 	u64 ram_size = (u64)SZ_64M * (nr_cpus + 3);
406 	u64 available = MIN_RAM_SIZE;
407 
408 	if (nr_pages_available)
409 		available = nr_pages_available * host_page_size();
410 
411 	if (ram_size > available)
412 		ram_size = available;
413 
414 	return ram_size;
415 }
416 
find_kernel(void)417 static const char *find_kernel(void)
418 {
419 	const char **k;
420 	struct stat st;
421 	struct utsname uts;
422 
423 	k = &default_kernels[0];
424 	while (*k) {
425 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
426 			k++;
427 			continue;
428 		}
429 		strlcpy(kernel, *k, PATH_MAX);
430 		return kernel;
431 	}
432 
433 	if (uname(&uts) < 0)
434 		return NULL;
435 
436 	k = &host_kernels[0];
437 	while (*k) {
438 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
439 			return NULL;
440 
441 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
442 			k++;
443 			continue;
444 		}
445 		return kernel;
446 
447 	}
448 	return NULL;
449 }
450 
find_vmlinux(void)451 static const char *find_vmlinux(void)
452 {
453 	const char **vmlinux;
454 
455 	vmlinux = &default_vmlinux[0];
456 	while (*vmlinux) {
457 		struct stat st;
458 
459 		if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
460 			vmlinux++;
461 			continue;
462 		}
463 		return *vmlinux;
464 	}
465 	return NULL;
466 }
467 
kvm_run_help(void)468 void kvm_run_help(void)
469 {
470 	struct kvm *kvm = NULL;
471 
472 	BUILD_OPTIONS(options, &kvm->cfg, kvm);
473 	usage_with_options(run_usage, options);
474 }
475 
kvm_run_set_sandbox(struct kvm * kvm)476 static int kvm_run_set_sandbox(struct kvm *kvm)
477 {
478 	const char *guestfs_name = kvm->cfg.custom_rootfs_name;
479 	char path[PATH_MAX], script[PATH_MAX], *tmp;
480 
481 	snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name);
482 
483 	remove(path);
484 
485 	if (kvm->cfg.sandbox == NULL)
486 		return 0;
487 
488 	tmp = realpath(kvm->cfg.sandbox, NULL);
489 	if (tmp == NULL)
490 		return -ENOMEM;
491 
492 	snprintf(script, PATH_MAX, "/host/%s", tmp);
493 	free(tmp);
494 
495 	return symlink(script, path);
496 }
497 
kvm_write_sandbox_cmd_exactly(int fd,const char * arg)498 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg)
499 {
500 	const char *single_quote;
501 
502 	if (!*arg) { /* zero length string */
503 		if (write(fd, "''", 2) <= 0)
504 			die("Failed writing sandbox script");
505 		return;
506 	}
507 
508 	while (*arg) {
509 		single_quote = strchrnul(arg, '\'');
510 
511 		/* write non-single-quote string as #('string') */
512 		if (arg != single_quote) {
513 			if (write(fd, "'", 1) <= 0 ||
514 			    write(fd, arg, single_quote - arg) <= 0 ||
515 			    write(fd, "'", 1) <= 0)
516 				die("Failed writing sandbox script");
517 		}
518 
519 		/* write single quote as #("'") */
520 		if (*single_quote) {
521 			if (write(fd, "\"'\"", 3) <= 0)
522 				die("Failed writing sandbox script");
523 		} else
524 			break;
525 
526 		arg = single_quote + 1;
527 	}
528 }
529 
resolve_program(const char * src,char * dst,size_t len)530 static void resolve_program(const char *src, char *dst, size_t len)
531 {
532 	struct stat st;
533 	int err;
534 
535 	err = stat(src, &st);
536 
537 	if (!err && S_ISREG(st.st_mode)) {
538 		char resolved_path[PATH_MAX];
539 
540 		if (!realpath(src, resolved_path))
541 			die("Unable to resolve program %s: %s\n", src, strerror(errno));
542 
543 		if (snprintf(dst, len, "/host%s", resolved_path) >= (int)len)
544 			die("Pathname too long: %s -> %s\n", src, resolved_path);
545 
546 	} else
547 		strlcpy(dst, src, len);
548 }
549 
kvm_run_write_sandbox_cmd(struct kvm * kvm,const char ** argv,int argc)550 static void kvm_run_write_sandbox_cmd(struct kvm *kvm, const char **argv, int argc)
551 {
552 	const char script_hdr[] = "#! /bin/bash\n\n";
553 	char program[PATH_MAX];
554 	int fd;
555 
556 	remove(kvm->cfg.sandbox);
557 
558 	fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777);
559 	if (fd < 0)
560 		die("Failed creating sandbox script");
561 
562 	if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0)
563 		die("Failed writing sandbox script");
564 
565 	resolve_program(argv[0], program, PATH_MAX);
566 	kvm_write_sandbox_cmd_exactly(fd, program);
567 
568 	argv++;
569 	argc--;
570 
571 	while (argc) {
572 		if (write(fd, " ", 1) <= 0)
573 			die("Failed writing sandbox script");
574 
575 		kvm_write_sandbox_cmd_exactly(fd, argv[0]);
576 		argv++;
577 		argc--;
578 	}
579 	if (write(fd, "\n", 1) <= 0)
580 		die("Failed writing sandbox script");
581 
582 	close(fd);
583 }
584 
kvm_run_set_real_cmdline(struct kvm * kvm)585 static void kvm_run_set_real_cmdline(struct kvm *kvm)
586 {
587 	static char real_cmdline[2048];
588 	bool video;
589 
590 	video = kvm->cfg.vnc || kvm->cfg.sdl || kvm->cfg.gtk;
591 
592 	memset(real_cmdline, 0, sizeof(real_cmdline));
593 	kvm__arch_set_cmdline(real_cmdline, video);
594 
595 	if (video) {
596 		strcat(real_cmdline, " console=tty0");
597 	} else {
598 		switch (kvm->cfg.active_console) {
599 		case CONSOLE_HV:
600 			/* Fallthrough */
601 		case CONSOLE_VIRTIO:
602 			strcat(real_cmdline, " console=hvc0");
603 			break;
604 		case CONSOLE_8250:
605 			strcat(real_cmdline, " console=ttyS0");
606 			break;
607 		}
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 #ifdef CONFIG_GUEST_PRE_INIT
614 			strcat(real_cmdline, " init=/virt/pre_init");
615 #else
616 			strcat(real_cmdline, " init=/virt/init");
617 #endif
618 			if (!kvm->cfg.no_dhcp)
619 				strcat(real_cmdline, "  ip=dhcp");
620 		}
621 	} else if (!kvm->cfg.kernel_cmdline || !strstr(kvm->cfg.kernel_cmdline, "root=")) {
622 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
623 	}
624 
625 	if (kvm->cfg.kernel_cmdline) {
626 		strcat(real_cmdline, " ");
627 		strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline));
628 	}
629 
630 	kvm->cfg.real_cmdline = real_cmdline;
631 }
632 
kvm_run_validate_cfg(struct kvm * kvm)633 static void kvm_run_validate_cfg(struct kvm *kvm)
634 {
635 	u64 available_ram;
636 
637 	if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename)
638 		die("Only one of --kernel or --firmware can be specified");
639 
640 	if ((kvm->cfg.vnc && (kvm->cfg.sdl || kvm->cfg.gtk)) ||
641 	    (kvm->cfg.sdl && kvm->cfg.gtk))
642 		die("Only one of --vnc, --sdl or --gtk can be specified");
643 
644 	if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename)
645 		pr_warning("Ignoring initrd file when loading a firmware image");
646 
647 	if (kvm->cfg.ram_size) {
648 		available_ram = host_ram_size();
649 		if (available_ram && kvm->cfg.ram_size > available_ram) {
650 			pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB",
651 				(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
652 				(unsigned long long)available_ram >> MB_SHIFT);
653 		}
654 	}
655 
656 	kvm__arch_validate_cfg(kvm);
657 }
658 
kvm_cmd_run_init(int argc,const char ** argv)659 static struct kvm *kvm_cmd_run_init(int argc, const char **argv)
660 {
661 	static char default_name[20];
662 	unsigned int nr_online_cpus;
663 	struct kvm *kvm = kvm__new();
664 
665 	if (IS_ERR(kvm))
666 		return kvm;
667 
668 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
669 	kvm->cfg.custom_rootfs_name = "default";
670 	/*
671 	 * An architecture can allow the user to set the RAM base address to
672 	 * zero. Initialize the address before parsing the command line
673 	 * arguments, otherwise it will be impossible to distinguish between the
674 	 * user setting the base address to zero or letting it unset and using
675 	 * the default value.
676 	 */
677 	kvm->cfg.ram_addr = kvm__arch_default_ram_address();
678 
679 	while (argc != 0) {
680 		BUILD_OPTIONS(options, &kvm->cfg, kvm);
681 		argc = parse_options(argc, argv, options, run_usage,
682 				PARSE_OPT_STOP_AT_NON_OPTION |
683 				PARSE_OPT_KEEP_DASHDASH);
684 		if (argc != 0) {
685 			/* Cusrom options, should have been handled elsewhere */
686 			if (strcmp(argv[0], "--") == 0) {
687 				if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
688 					kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
689 					kvm_run_write_sandbox_cmd(kvm, argv+1, argc-1);
690 					break;
691 				}
692 			}
693 
694 			if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) ||
695 				(kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) {
696 				pr_err("Cannot handle parameter: %s", argv[0]);
697 				usage_with_options(run_usage, options);
698 				free(kvm);
699 				return ERR_PTR(-EINVAL);
700 			}
701 			if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
702 				/*
703 				 * first unhandled parameter is treated as
704 				 * sandbox command
705 				 */
706 				kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
707 				kvm_run_write_sandbox_cmd(kvm, argv, argc);
708 			} else {
709 				/*
710 				 * first unhandled parameter is treated as a kernel
711 				 * image
712 				 */
713 				kvm->cfg.kernel_filename = argv[0];
714 			}
715 			argv++;
716 			argc--;
717 		}
718 
719 	}
720 
721 	kvm_run_validate_cfg(kvm);
722 
723 	if (!kvm->cfg.kernel_filename && !kvm->cfg.firmware_filename) {
724 		kvm->cfg.kernel_filename = find_kernel();
725 
726 		if (!kvm->cfg.kernel_filename) {
727 			kernel_usage_with_options();
728 			return ERR_PTR(-EINVAL);
729 		}
730 	}
731 
732 	if (kvm->cfg.kernel_filename) {
733 		kvm->cfg.vmlinux_filename = find_vmlinux();
734 		kvm->vmlinux = kvm->cfg.vmlinux_filename;
735 	}
736 
737 	if (kvm->cfg.nrcpus == 0)
738 		kvm->cfg.nrcpus = nr_online_cpus;
739 
740 	if (!kvm->cfg.ram_size)
741 		kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus);
742 
743 	if (!kvm->cfg.dev)
744 		kvm->cfg.dev = DEFAULT_KVM_DEV;
745 
746 	if (!kvm->cfg.console)
747 		kvm->cfg.console = DEFAULT_CONSOLE;
748 
749 	if (!strncmp(kvm->cfg.console, "virtio", 6))
750 		kvm->cfg.active_console  = CONSOLE_VIRTIO;
751 	else if (!strncmp(kvm->cfg.console, "serial", 6))
752 		kvm->cfg.active_console  = CONSOLE_8250;
753 	else if (!strncmp(kvm->cfg.console, "hv", 2))
754 		kvm->cfg.active_console = CONSOLE_HV;
755 	else
756 		pr_warning("No console!");
757 
758 	if (!kvm->cfg.host_ip)
759 		kvm->cfg.host_ip = DEFAULT_HOST_ADDR;
760 
761 	if (!kvm->cfg.guest_ip)
762 		kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR;
763 
764 	if (!kvm->cfg.guest_mac)
765 		kvm->cfg.guest_mac = DEFAULT_GUEST_MAC;
766 
767 	if (!kvm->cfg.host_mac)
768 		kvm->cfg.host_mac = DEFAULT_HOST_MAC;
769 
770 	if (!kvm->cfg.script)
771 		kvm->cfg.script = DEFAULT_SCRIPT;
772 
773 	if (!kvm->cfg.network)
774                 kvm->cfg.network = DEFAULT_NETWORK;
775 
776 	if (!kvm->cfg.guest_name) {
777 		if (kvm->cfg.custom_rootfs) {
778 			kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name;
779 		} else {
780 			sprintf(default_name, "guest-%u", getpid());
781 			kvm->cfg.guest_name = default_name;
782 		}
783 	}
784 
785 	if (!kvm->cfg.nodefaults &&
786 	    !kvm->cfg.using_rootfs &&
787 	    !kvm->cfg.disk_image[0].filename &&
788 	    !kvm->cfg.initrd_filename) {
789 		char tmp[PATH_MAX];
790 
791 		kvm_setup_create_new(kvm->cfg.custom_rootfs_name);
792 		kvm_setup_resolv(kvm->cfg.custom_rootfs_name);
793 
794 		snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default");
795 		if (virtio_9p__register(kvm, tmp, "/dev/root") < 0)
796 			die("Unable to initialize virtio 9p");
797 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
798 			die("Unable to initialize virtio 9p");
799 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
800 	}
801 
802 	if (kvm->cfg.custom_rootfs) {
803 		kvm_run_set_sandbox(kvm);
804 		if (kvm_setup_guest_init(kvm->cfg.custom_rootfs_name))
805 			die("Failed to setup init for guest.");
806 	}
807 
808 	if (kvm->cfg.nodefaults)
809 		kvm->cfg.real_cmdline = kvm->cfg.kernel_cmdline;
810 	else
811 		kvm_run_set_real_cmdline(kvm);
812 
813 	if (kvm->cfg.kernel_filename) {
814 		pr_info("# %s run -k %s -m %Lu -c %d --name %s", KVM_BINARY_NAME,
815 			kvm->cfg.kernel_filename,
816 			(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
817 			kvm->cfg.nrcpus, kvm->cfg.guest_name);
818 	} else if (kvm->cfg.firmware_filename) {
819 		pr_info("# %s run --firmware %s -m %Lu -c %d --name %s", KVM_BINARY_NAME,
820 			kvm->cfg.firmware_filename,
821 			(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
822 			kvm->cfg.nrcpus, kvm->cfg.guest_name);
823 	}
824 
825 	if (init_list__init(kvm) < 0)
826 		die ("Initialisation failed");
827 
828 	return kvm;
829 }
830 
kvm_cmd_run_work(struct kvm * kvm)831 static int kvm_cmd_run_work(struct kvm *kvm)
832 {
833 	int i;
834 
835 	for (i = 0; i < kvm->nrcpus; i++) {
836 		if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0)
837 			die("unable to create KVM VCPU thread");
838 	}
839 
840 	/* Only VCPU #0 is going to exit by itself when shutting down */
841 	if (pthread_join(kvm->cpus[0]->thread, NULL) != 0)
842 		die("unable to join with vcpu 0");
843 
844 	return kvm_cpu__exit(kvm);
845 }
846 
kvm_cmd_run_exit(struct kvm * kvm,int guest_ret)847 static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret)
848 {
849 	compat__print_all_messages();
850 
851 	init_list__exit(kvm);
852 
853 	if (guest_ret == 0)
854 		pr_info("KVM session ended normally.");
855 }
856 
kvm_cmd_run(int argc,const char ** argv,const char * prefix)857 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
858 {
859 	int ret = -EFAULT;
860 	struct kvm *kvm;
861 
862 	kvm = kvm_cmd_run_init(argc, argv);
863 	if (IS_ERR(kvm))
864 		return PTR_ERR(kvm);
865 
866 	ret = kvm_cmd_run_work(kvm);
867 	kvm_cmd_run_exit(kvm, ret);
868 
869 	return ret;
870 }
871