xref: /kvmtool/builtin-run.c (revision 8cec93dbc73e705e6fbefe395da586ae1b71e0e2)
1 #include "kvm/builtin-run.h"
2 
3 #include "kvm/builtin-setup.h"
4 #include "kvm/virtio-balloon.h"
5 #include "kvm/virtio-console.h"
6 #include "kvm/parse-options.h"
7 #include "kvm/8250-serial.h"
8 #include "kvm/framebuffer.h"
9 #include "kvm/disk-image.h"
10 #include "kvm/threadpool.h"
11 #include "kvm/virtio-scsi.h"
12 #include "kvm/virtio-blk.h"
13 #include "kvm/virtio-net.h"
14 #include "kvm/virtio-rng.h"
15 #include "kvm/ioeventfd.h"
16 #include "kvm/virtio-9p.h"
17 #include "kvm/barrier.h"
18 #include "kvm/kvm-cpu.h"
19 #include "kvm/ioport.h"
20 #include "kvm/symbol.h"
21 #include "kvm/i8042.h"
22 #include "kvm/mutex.h"
23 #include "kvm/term.h"
24 #include "kvm/util.h"
25 #include "kvm/strbuf.h"
26 #include "kvm/vesa.h"
27 #include "kvm/irq.h"
28 #include "kvm/kvm.h"
29 #include "kvm/pci.h"
30 #include "kvm/rtc.h"
31 #include "kvm/sdl.h"
32 #include "kvm/vnc.h"
33 #include "kvm/guest_compat.h"
34 #include "kvm/pci-shmem.h"
35 #include "kvm/kvm-ipc.h"
36 #include "kvm/builtin-debug.h"
37 
38 #include <linux/types.h>
39 #include <linux/err.h>
40 
41 #include <sys/utsname.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <termios.h>
45 #include <signal.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <ctype.h>
50 #include <stdio.h>
51 
52 #define MB_SHIFT		(20)
53 #define KB_SHIFT		(10)
54 #define GB_SHIFT		(30)
55 
56 struct kvm *kvm;
57 __thread struct kvm_cpu *current_kvm_cpu;
58 
59 static int  kvm_run_wrapper;
60 
61 bool do_debug_print = false;
62 
63 static int vidmode = -1;
64 
65 extern char _binary_guest_init_start;
66 extern char _binary_guest_init_size;
67 
68 static const char * const run_usage[] = {
69 	"lkvm run [<options>] [<kernel image>]",
70 	NULL
71 };
72 
73 enum {
74 	KVM_RUN_DEFAULT,
75 	KVM_RUN_SANDBOX,
76 };
77 
78 static int img_name_parser(const struct option *opt, const char *arg, int unset)
79 {
80 	char path[PATH_MAX];
81 	struct stat st;
82 	struct kvm *kvm = opt->ptr;
83 
84 	if (stat(arg, &st) == 0 &&
85 	    S_ISDIR(st.st_mode)) {
86 		char tmp[PATH_MAX];
87 
88 		if (kvm->cfg.using_rootfs)
89 			die("Please use only one rootfs directory atmost");
90 
91 		if (realpath(arg, tmp) == 0 ||
92 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
93 			die("Unable to initialize virtio 9p");
94 		kvm->cfg.using_rootfs = 1;
95 		return 0;
96 	}
97 
98 	snprintf(path, PATH_MAX, "%s%s", kvm__get_dir(), arg);
99 
100 	if (stat(path, &st) == 0 &&
101 	    S_ISDIR(st.st_mode)) {
102 		char tmp[PATH_MAX];
103 
104 		if (kvm->cfg.using_rootfs)
105 			die("Please use only one rootfs directory atmost");
106 
107 		if (realpath(path, tmp) == 0 ||
108 		    virtio_9p__register(kvm, tmp, "/dev/root") < 0)
109 			die("Unable to initialize virtio 9p");
110 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
111 			die("Unable to initialize virtio 9p");
112 		kvm_setup_resolv(arg);
113 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
114 		kvm->cfg.custom_rootfs_name = arg;
115 		return 0;
116 	}
117 
118 	return disk_img_name_parser(opt, arg, unset);
119 }
120 
121 void kvm_run_set_wrapper_sandbox(void)
122 {
123 	kvm_run_wrapper = KVM_RUN_SANDBOX;
124 }
125 
126 static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, int unset)
127 {
128 	char *tag_name;
129 	char tmp[PATH_MAX];
130 
131 	/*
132 	 * 9p dir can be of the form dirname,tag_name or
133 	 * just dirname. In the later case we use the
134 	 * default tag name
135 	 */
136 	tag_name = strstr(arg, ",");
137 	if (tag_name) {
138 		*tag_name = '\0';
139 		tag_name++;
140 	}
141 	if (realpath(arg, tmp)) {
142 		if (virtio_9p__register(kvm, tmp, tag_name) < 0)
143 			die("Unable to initialize virtio 9p");
144 	} else
145 		die("Failed resolving 9p path");
146 	return 0;
147 }
148 
149 static inline void str_to_mac(const char *str, char *mac)
150 {
151 	sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
152 		mac, mac+1, mac+2, mac+3, mac+4, mac+5);
153 }
154 static int set_net_param(struct virtio_net_params *p, const char *param,
155 				const char *val)
156 {
157 	if (strcmp(param, "guest_mac") == 0) {
158 		str_to_mac(val, p->guest_mac);
159 	} else if (strcmp(param, "mode") == 0) {
160 		if (!strncmp(val, "user", 4)) {
161 			int i;
162 
163 			for (i = 0; i < kvm->cfg.num_net_devices; i++)
164 				if (kvm->cfg.net_params[i].mode == NET_MODE_USER)
165 					die("Only one usermode network device allowed at a time");
166 			p->mode = NET_MODE_USER;
167 		} else if (!strncmp(val, "tap", 3)) {
168 			p->mode = NET_MODE_TAP;
169 		} else if (!strncmp(val, "none", 4)) {
170 			kvm->cfg.no_net = 1;
171 			return -1;
172 		} else
173 			die("Unknown network mode %s, please use user, tap or none", kvm->cfg.network);
174 	} else if (strcmp(param, "script") == 0) {
175 		p->script = strdup(val);
176 	} else if (strcmp(param, "guest_ip") == 0) {
177 		p->guest_ip = strdup(val);
178 	} else if (strcmp(param, "host_ip") == 0) {
179 		p->host_ip = strdup(val);
180 	} else if (strcmp(param, "trans") == 0) {
181 		p->trans = strdup(val);
182 	} else if (strcmp(param, "vhost") == 0) {
183 		p->vhost = atoi(val);
184 	} else if (strcmp(param, "fd") == 0) {
185 		p->fd = atoi(val);
186 	} else
187 		die("Unknown network parameter %s", param);
188 
189 	return 0;
190 }
191 
192 static int netdev_parser(const struct option *opt, const char *arg, int unset)
193 {
194 	struct virtio_net_params p;
195 	char *buf = NULL, *cmd = NULL, *cur = NULL;
196 	bool on_cmd = true;
197 
198 	if (arg) {
199 		buf = strdup(arg);
200 		if (buf == NULL)
201 			die("Failed allocating new net buffer");
202 		cur = strtok(buf, ",=");
203 	}
204 
205 	p = (struct virtio_net_params) {
206 		.guest_ip	= DEFAULT_GUEST_ADDR,
207 		.host_ip	= DEFAULT_HOST_ADDR,
208 		.script		= DEFAULT_SCRIPT,
209 		.mode		= NET_MODE_TAP,
210 	};
211 
212 	str_to_mac(DEFAULT_GUEST_MAC, p.guest_mac);
213 	p.guest_mac[5] += kvm->cfg.num_net_devices;
214 
215 	while (cur) {
216 		if (on_cmd) {
217 			cmd = cur;
218 		} else {
219 			if (set_net_param(&p, cmd, cur) < 0)
220 				goto done;
221 		}
222 		on_cmd = !on_cmd;
223 
224 		cur = strtok(NULL, ",=");
225 	};
226 
227 	kvm->cfg.num_net_devices++;
228 
229 	kvm->cfg.net_params = realloc(kvm->cfg.net_params, kvm->cfg.num_net_devices * sizeof(*kvm->cfg.net_params));
230 	if (kvm->cfg.net_params == NULL)
231 		die("Failed adding new network device");
232 
233 	kvm->cfg.net_params[kvm->cfg.num_net_devices - 1] = p;
234 
235 done:
236 	free(buf);
237 	return 0;
238 }
239 
240 #define BUILD_OPTIONS(name, cfg, kvm)					\
241 	struct option name[] = {					\
242 	OPT_GROUP("Basic options:"),					\
243 	OPT_STRING('\0', "name", &(cfg)->guest_name, "guest name",	\
244 			"A name for the guest"),			\
245 	OPT_INTEGER('c', "cpus", &(cfg)->nrcpus, "Number of CPUs"),	\
246 	OPT_U64('m', "mem", &(cfg)->ram_size, "Virtual machine memory size\
247 		in MiB."),						\
248 	OPT_CALLBACK('\0', "shmem", NULL,				\
249 		     "[pci:]<addr>:<size>[:handle=<handle>][:create]",	\
250 		     "Share host shmem with guest via pci device",	\
251 		     shmem_parser, NULL),				\
252 	OPT_CALLBACK('d', "disk", kvm, "image or rootfs_dir", "Disk 	\
253 			image or rootfs directory", img_name_parser,	\
254 			kvm),						\
255 	OPT_BOOLEAN('\0', "balloon", &(cfg)->balloon, "Enable virtio	\
256 			balloon"),					\
257 	OPT_BOOLEAN('\0', "vnc", &(cfg)->vnc, "Enable VNC framebuffer"),\
258 	OPT_BOOLEAN('\0', "sdl", &(cfg)->sdl, "Enable SDL framebuffer"),\
259 	OPT_BOOLEAN('\0', "rng", &(cfg)->virtio_rng, "Enable virtio Random\
260 			Number Generator"),				\
261 	OPT_CALLBACK('\0', "9p", NULL, "dir_to_share,tag_name",		\
262 		     "Enable virtio 9p to share files between host and	\
263 		     guest", virtio_9p_rootdir_parser, NULL),		\
264 	OPT_STRING('\0', "console", &(cfg)->console, "serial, virtio or	\
265 			hv", "Console to use"),				\
266 	OPT_STRING('\0', "dev", &(cfg)->dev, "device_file",		\
267 			"KVM device file"),				\
268 	OPT_CALLBACK('\0', "tty", NULL, "tty id",			\
269 		     "Remap guest TTY into a pty on the host",		\
270 		     tty_parser, NULL),					\
271 	OPT_STRING('\0', "sandbox", &(cfg)->sandbox, "script",		\
272 			"Run this script when booting into custom	\
273 			rootfs"),					\
274 	OPT_STRING('\0', "hugetlbfs", &(cfg)->hugetlbfs_path, "path",	\
275 			"Hugetlbfs path"),				\
276 									\
277 	OPT_GROUP("Kernel options:"),					\
278 	OPT_STRING('k', "kernel", &(cfg)->kernel_filename, "kernel",	\
279 			"Kernel to boot in virtual machine"),		\
280 	OPT_STRING('i', "initrd", &(cfg)->initrd_filename, "initrd",	\
281 			"Initial RAM disk image"),			\
282 	OPT_STRING('p', "params", &(cfg)->kernel_cmdline, "params",	\
283 			"Kernel command line arguments"),		\
284 	OPT_STRING('f', "firmware", &(cfg)->firmware_filename, "firmware",\
285 			"Firmware image to boot in virtual machine"),	\
286 									\
287 	OPT_GROUP("Networking options:"),				\
288 	OPT_CALLBACK_DEFAULT('n', "network", NULL, "network params",	\
289 		     "Create a new guest NIC",				\
290 		     netdev_parser, NULL, NULL),			\
291 	OPT_BOOLEAN('\0', "no-dhcp", &(cfg)->no_dhcp, "Disable kernel DHCP\
292 			in rootfs mode"),				\
293 									\
294 	OPT_GROUP("BIOS options:"),					\
295 	OPT_INTEGER('\0', "vidmode", &vidmode,				\
296 		    "Video mode"),					\
297 									\
298 	OPT_GROUP("Debug options:"),					\
299 	OPT_BOOLEAN('\0', "debug", &do_debug_print,			\
300 			"Enable debug messages"),			\
301 	OPT_BOOLEAN('\0', "debug-single-step", &(cfg)->single_step,	\
302 			"Enable single stepping"),			\
303 	OPT_BOOLEAN('\0', "debug-ioport", &(cfg)->ioport_debug,		\
304 			"Enable ioport debugging"),			\
305 	OPT_BOOLEAN('\0', "debug-mmio", &(cfg)->mmio_debug,		\
306 			"Enable MMIO debugging"),			\
307 	OPT_INTEGER('\0', "debug-iodelay", &(cfg)->debug_iodelay,	\
308 			"Delay IO by millisecond"),			\
309 	OPT_END()							\
310 	};
311 
312 /*
313  * Serialize debug printout so that the output of multiple vcpus does not
314  * get mixed up:
315  */
316 static int printout_done;
317 
318 static void handle_sigusr1(int sig)
319 {
320 	struct kvm_cpu *cpu = current_kvm_cpu;
321 	int fd = kvm_cpu__get_debug_fd();
322 
323 	if (!cpu || cpu->needs_nmi)
324 		return;
325 
326 	dprintf(fd, "\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id);
327 	kvm_cpu__show_registers(cpu);
328 	kvm_cpu__show_code(cpu);
329 	kvm_cpu__show_page_tables(cpu);
330 	fflush(stdout);
331 	printout_done = 1;
332 	mb();
333 }
334 
335 /* Pause/resume the guest using SIGUSR2 */
336 static int is_paused;
337 
338 static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
339 {
340 	if (WARN_ON(len))
341 		return;
342 
343 	if (type == KVM_IPC_RESUME && is_paused) {
344 		kvm->vm_state = KVM_VMSTATE_RUNNING;
345 		kvm__continue();
346 	} else if (type == KVM_IPC_PAUSE && !is_paused) {
347 		kvm->vm_state = KVM_VMSTATE_PAUSED;
348 		ioctl(kvm->vm_fd, KVM_KVMCLOCK_CTRL);
349 		kvm__pause();
350 	} else {
351 		return;
352 	}
353 
354 	is_paused = !is_paused;
355 }
356 
357 static void handle_vmstate(int fd, u32 type, u32 len, u8 *msg)
358 {
359 	int r = 0;
360 
361 	if (type == KVM_IPC_VMSTATE)
362 		r = write(fd, &kvm->vm_state, sizeof(kvm->vm_state));
363 
364 	if (r < 0)
365 		pr_warning("Failed sending VMSTATE");
366 }
367 
368 static void handle_debug(int fd, u32 type, u32 len, u8 *msg)
369 {
370 	int i;
371 	struct debug_cmd_params *params;
372 	u32 dbg_type;
373 	u32 vcpu;
374 
375 	if (WARN_ON(type != KVM_IPC_DEBUG || len != sizeof(*params)))
376 		return;
377 
378 	params = (void *)msg;
379 	dbg_type = params->dbg_type;
380 	vcpu = params->cpu;
381 
382 	if (dbg_type & KVM_DEBUG_CMD_TYPE_SYSRQ)
383 		serial8250__inject_sysrq(kvm, params->sysrq);
384 
385 	if (dbg_type & KVM_DEBUG_CMD_TYPE_NMI) {
386 		if ((int)vcpu >= kvm->nrcpus)
387 			return;
388 
389 		kvm->cpus[vcpu]->needs_nmi = 1;
390 		pthread_kill(kvm->cpus[vcpu]->thread, SIGUSR1);
391 	}
392 
393 	if (!(dbg_type & KVM_DEBUG_CMD_TYPE_DUMP))
394 		return;
395 
396 	for (i = 0; i < kvm->nrcpus; i++) {
397 		struct kvm_cpu *cpu = kvm->cpus[i];
398 
399 		if (!cpu)
400 			continue;
401 
402 		printout_done = 0;
403 
404 		kvm_cpu__set_debug_fd(fd);
405 		pthread_kill(cpu->thread, SIGUSR1);
406 		/*
407 		 * Wait for the vCPU to dump state before signalling
408 		 * the next thread. Since this is debug code it does
409 		 * not matter that we are burning CPU time a bit:
410 		 */
411 		while (!printout_done)
412 			mb();
413 	}
414 
415 	close(fd);
416 
417 	serial8250__inject_sysrq(kvm, 'p');
418 }
419 
420 static void handle_sigalrm(int sig)
421 {
422 	kvm__arch_periodic_poll(kvm);
423 }
424 
425 static void handle_stop(int fd, u32 type, u32 len, u8 *msg)
426 {
427 	if (WARN_ON(type != KVM_IPC_STOP || len))
428 		return;
429 
430 	kvm_cpu__reboot(kvm);
431 }
432 
433 static void *kvm_cpu_thread(void *arg)
434 {
435 	current_kvm_cpu		= arg;
436 
437 	if (kvm_cpu__start(current_kvm_cpu))
438 		goto panic_kvm;
439 
440 	return (void *) (intptr_t) 0;
441 
442 panic_kvm:
443 	fprintf(stderr, "KVM exit reason: %u (\"%s\")\n",
444 		current_kvm_cpu->kvm_run->exit_reason,
445 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
446 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
447 		fprintf(stderr, "KVM exit code: 0x%Lu\n",
448 			current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
449 
450 	kvm_cpu__set_debug_fd(STDOUT_FILENO);
451 	kvm_cpu__show_registers(current_kvm_cpu);
452 	kvm_cpu__show_code(current_kvm_cpu);
453 	kvm_cpu__show_page_tables(current_kvm_cpu);
454 
455 	return (void *) (intptr_t) 1;
456 }
457 
458 static char kernel[PATH_MAX];
459 
460 static const char *host_kernels[] = {
461 	"/boot/vmlinuz",
462 	"/boot/bzImage",
463 	NULL
464 };
465 
466 static const char *default_kernels[] = {
467 	"./bzImage",
468 	"arch/" BUILD_ARCH "/boot/bzImage",
469 	"../../arch/" BUILD_ARCH "/boot/bzImage",
470 	NULL
471 };
472 
473 static const char *default_vmlinux[] = {
474 	"vmlinux",
475 	"../../../vmlinux",
476 	"../../vmlinux",
477 	NULL
478 };
479 
480 static void kernel_usage_with_options(void)
481 {
482 	const char **k;
483 	struct utsname uts;
484 
485 	fprintf(stderr, "Fatal: could not find default kernel image in:\n");
486 	k = &default_kernels[0];
487 	while (*k) {
488 		fprintf(stderr, "\t%s\n", *k);
489 		k++;
490 	}
491 
492 	if (uname(&uts) < 0)
493 		return;
494 
495 	k = &host_kernels[0];
496 	while (*k) {
497 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
498 			return;
499 		fprintf(stderr, "\t%s\n", kernel);
500 		k++;
501 	}
502 	fprintf(stderr, "\nPlease see '%s run --help' for more options.\n\n",
503 		KVM_BINARY_NAME);
504 }
505 
506 static u64 host_ram_size(void)
507 {
508 	long page_size;
509 	long nr_pages;
510 
511 	nr_pages	= sysconf(_SC_PHYS_PAGES);
512 	if (nr_pages < 0) {
513 		pr_warning("sysconf(_SC_PHYS_PAGES) failed");
514 		return 0;
515 	}
516 
517 	page_size	= sysconf(_SC_PAGE_SIZE);
518 	if (page_size < 0) {
519 		pr_warning("sysconf(_SC_PAGE_SIZE) failed");
520 		return 0;
521 	}
522 
523 	return (nr_pages * page_size) >> MB_SHIFT;
524 }
525 
526 /*
527  * If user didn't specify how much memory it wants to allocate for the guest,
528  * avoid filling the whole host RAM.
529  */
530 #define RAM_SIZE_RATIO		0.8
531 
532 static u64 get_ram_size(int nr_cpus)
533 {
534 	u64 available;
535 	u64 ram_size;
536 
537 	ram_size	= 64 * (nr_cpus + 3);
538 
539 	available	= host_ram_size() * RAM_SIZE_RATIO;
540 	if (!available)
541 		available = MIN_RAM_SIZE_MB;
542 
543 	if (ram_size > available)
544 		ram_size	= available;
545 
546 	return ram_size;
547 }
548 
549 static const char *find_kernel(void)
550 {
551 	const char **k;
552 	struct stat st;
553 	struct utsname uts;
554 
555 	k = &default_kernels[0];
556 	while (*k) {
557 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
558 			k++;
559 			continue;
560 		}
561 		strncpy(kernel, *k, PATH_MAX);
562 		return kernel;
563 	}
564 
565 	if (uname(&uts) < 0)
566 		return NULL;
567 
568 	k = &host_kernels[0];
569 	while (*k) {
570 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
571 			return NULL;
572 
573 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
574 			k++;
575 			continue;
576 		}
577 		return kernel;
578 
579 	}
580 	return NULL;
581 }
582 
583 static const char *find_vmlinux(void)
584 {
585 	const char **vmlinux;
586 
587 	vmlinux = &default_vmlinux[0];
588 	while (*vmlinux) {
589 		struct stat st;
590 
591 		if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
592 			vmlinux++;
593 			continue;
594 		}
595 		return *vmlinux;
596 	}
597 	return NULL;
598 }
599 
600 void kvm_run_help(void)
601 {
602 	BUILD_OPTIONS(options, &kvm->cfg, kvm);
603 	usage_with_options(run_usage, options);
604 }
605 
606 static int kvm_setup_guest_init(void)
607 {
608 	const char *rootfs = kvm->cfg.custom_rootfs_name;
609 	char tmp[PATH_MAX];
610 	size_t size;
611 	int fd, ret;
612 	char *data;
613 
614 	/* Setup /virt/init */
615 	size = (size_t)&_binary_guest_init_size;
616 	data = (char *)&_binary_guest_init_start;
617 	snprintf(tmp, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), rootfs);
618 	remove(tmp);
619 	fd = open(tmp, O_CREAT | O_WRONLY, 0755);
620 	if (fd < 0)
621 		die("Fail to setup %s", tmp);
622 	ret = xwrite(fd, data, size);
623 	if (ret < 0)
624 		die("Fail to setup %s", tmp);
625 	close(fd);
626 
627 	return 0;
628 }
629 
630 static int kvm_run_set_sandbox(void)
631 {
632 	const char *guestfs_name = kvm->cfg.custom_rootfs_name;
633 	char path[PATH_MAX], script[PATH_MAX], *tmp;
634 
635 	snprintf(path, PATH_MAX, "%s%s/virt/sandbox.sh", kvm__get_dir(), guestfs_name);
636 
637 	remove(path);
638 
639 	if (kvm->cfg.sandbox == NULL)
640 		return 0;
641 
642 	tmp = realpath(kvm->cfg.sandbox, NULL);
643 	if (tmp == NULL)
644 		return -ENOMEM;
645 
646 	snprintf(script, PATH_MAX, "/host/%s", tmp);
647 	free(tmp);
648 
649 	return symlink(script, path);
650 }
651 
652 static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg)
653 {
654 	const char *single_quote;
655 
656 	if (!*arg) { /* zero length string */
657 		if (write(fd, "''", 2) <= 0)
658 			die("Failed writing sandbox script");
659 		return;
660 	}
661 
662 	while (*arg) {
663 		single_quote = strchrnul(arg, '\'');
664 
665 		/* write non-single-quote string as #('string') */
666 		if (arg != single_quote) {
667 			if (write(fd, "'", 1) <= 0 ||
668 			    write(fd, arg, single_quote - arg) <= 0 ||
669 			    write(fd, "'", 1) <= 0)
670 				die("Failed writing sandbox script");
671 		}
672 
673 		/* write single quote as #("'") */
674 		if (*single_quote) {
675 			if (write(fd, "\"'\"", 3) <= 0)
676 				die("Failed writing sandbox script");
677 		} else
678 			break;
679 
680 		arg = single_quote + 1;
681 	}
682 }
683 
684 static void resolve_program(const char *src, char *dst, size_t len)
685 {
686 	struct stat st;
687 	int err;
688 
689 	err = stat(src, &st);
690 
691 	if (!err && S_ISREG(st.st_mode)) {
692 		char resolved_path[PATH_MAX];
693 
694 		if (!realpath(src, resolved_path))
695 			die("Unable to resolve program %s: %s\n", src, strerror(errno));
696 
697 		snprintf(dst, len, "/host%s", resolved_path);
698 	} else
699 		strncpy(dst, src, len);
700 }
701 
702 static void kvm_run_write_sandbox_cmd(const char **argv, int argc)
703 {
704 	const char script_hdr[] = "#! /bin/bash\n\n";
705 	char program[PATH_MAX];
706 	int fd;
707 
708 	remove(kvm->cfg.sandbox);
709 
710 	fd = open(kvm->cfg.sandbox, O_RDWR | O_CREAT, 0777);
711 	if (fd < 0)
712 		die("Failed creating sandbox script");
713 
714 	if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0)
715 		die("Failed writing sandbox script");
716 
717 	resolve_program(argv[0], program, PATH_MAX);
718 	kvm_write_sandbox_cmd_exactly(fd, program);
719 
720 	argv++;
721 	argc--;
722 
723 	while (argc) {
724 		if (write(fd, " ", 1) <= 0)
725 			die("Failed writing sandbox script");
726 
727 		kvm_write_sandbox_cmd_exactly(fd, argv[0]);
728 		argv++;
729 		argc--;
730 	}
731 	if (write(fd, "\n", 1) <= 0)
732 		die("Failed writing sandbox script");
733 
734 	close(fd);
735 }
736 
737 static int kvm_cmd_run_init(int argc, const char **argv)
738 {
739 	static char real_cmdline[2048], default_name[20];
740 	struct framebuffer *fb = NULL;
741 	unsigned int nr_online_cpus;
742 	int i, r;
743 
744 	kvm = kvm__new();
745 	if (IS_ERR(kvm))
746 		return PTR_ERR(kvm);
747 
748 	signal(SIGALRM, handle_sigalrm);
749 	kvm_ipc__register_handler(KVM_IPC_DEBUG, handle_debug);
750 	signal(SIGUSR1, handle_sigusr1);
751 	kvm_ipc__register_handler(KVM_IPC_PAUSE, handle_pause);
752 	kvm_ipc__register_handler(KVM_IPC_RESUME, handle_pause);
753 	kvm_ipc__register_handler(KVM_IPC_STOP, handle_stop);
754 	kvm_ipc__register_handler(KVM_IPC_VMSTATE, handle_vmstate);
755 
756 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
757 	kvm->cfg.custom_rootfs_name = "default";
758 
759 	while (argc != 0) {
760 		BUILD_OPTIONS(options, &kvm->cfg, kvm);
761 		argc = parse_options(argc, argv, options, run_usage,
762 				PARSE_OPT_STOP_AT_NON_OPTION |
763 				PARSE_OPT_KEEP_DASHDASH);
764 		if (argc != 0) {
765 			/* Cusrom options, should have been handled elsewhere */
766 			if (strcmp(argv[0], "--") == 0) {
767 				if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
768 					kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
769 					kvm_run_write_sandbox_cmd(argv+1, argc-1);
770 					break;
771 				}
772 			}
773 
774 			if ((kvm_run_wrapper == KVM_RUN_DEFAULT && kvm->cfg.kernel_filename) ||
775 				(kvm_run_wrapper == KVM_RUN_SANDBOX && kvm->cfg.sandbox)) {
776 				fprintf(stderr, "Cannot handle parameter: "
777 						"%s\n", argv[0]);
778 				usage_with_options(run_usage, options);
779 				free(kvm);
780 				return -EINVAL;
781 			}
782 			if (kvm_run_wrapper == KVM_RUN_SANDBOX) {
783 				/*
784 				 * first unhandled parameter is treated as
785 				 * sandbox command
786 				 */
787 				kvm->cfg.sandbox = DEFAULT_SANDBOX_FILENAME;
788 				kvm_run_write_sandbox_cmd(argv, argc);
789 			} else {
790 				/*
791 				 * first unhandled parameter is treated as a kernel
792 				 * image
793 				 */
794 				kvm->cfg.kernel_filename = argv[0];
795 			}
796 			argv++;
797 			argc--;
798 		}
799 
800 	}
801 
802 	kvm->nr_disks = kvm->cfg.image_count;
803 
804 	if (!kvm->cfg.kernel_filename)
805 		kvm->cfg.kernel_filename = find_kernel();
806 
807 	if (!kvm->cfg.kernel_filename) {
808 		kernel_usage_with_options();
809 		return -EINVAL;
810 	}
811 
812 	kvm->cfg.vmlinux_filename = find_vmlinux();
813 
814 	if (kvm->cfg.nrcpus == 0)
815 		kvm->cfg.nrcpus = nr_online_cpus;
816 
817 	if (!kvm->cfg.ram_size)
818 		kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus);
819 
820 	if (kvm->cfg.ram_size < MIN_RAM_SIZE_MB)
821 		die("Not enough memory specified: %lluMB (min %lluMB)", kvm->cfg.ram_size, MIN_RAM_SIZE_MB);
822 
823 	if (kvm->cfg.ram_size > host_ram_size())
824 		pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", kvm->cfg.ram_size, host_ram_size());
825 
826 	kvm->cfg.ram_size <<= MB_SHIFT;
827 
828 	if (!kvm->cfg.dev)
829 		kvm->cfg.dev = DEFAULT_KVM_DEV;
830 
831 	if (!kvm->cfg.console)
832 		kvm->cfg.console = DEFAULT_CONSOLE;
833 
834 	if (!strncmp(kvm->cfg.console, "virtio", 6))
835 		kvm->cfg.active_console  = CONSOLE_VIRTIO;
836 	else if (!strncmp(kvm->cfg.console, "serial", 6))
837 		kvm->cfg.active_console  = CONSOLE_8250;
838 	else if (!strncmp(kvm->cfg.console, "hv", 2))
839 		kvm->cfg.active_console = CONSOLE_HV;
840 	else
841 		pr_warning("No console!");
842 
843 	if (!kvm->cfg.host_ip)
844 		kvm->cfg.host_ip = DEFAULT_HOST_ADDR;
845 
846 	if (!kvm->cfg.guest_ip)
847 		kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR;
848 
849 	if (!kvm->cfg.guest_mac)
850 		kvm->cfg.guest_mac = DEFAULT_GUEST_MAC;
851 
852 	if (!kvm->cfg.host_mac)
853 		kvm->cfg.host_mac = DEFAULT_HOST_MAC;
854 
855 	if (!kvm->cfg.script)
856 		kvm->cfg.script = DEFAULT_SCRIPT;
857 
858 	r = term_init(kvm);
859 	if (r < 0) {
860 		pr_err("term_init() failed with error %d\n", r);
861 		goto fail;
862 	}
863 
864 	if (!kvm->cfg.guest_name) {
865 		if (kvm->cfg.custom_rootfs) {
866 			kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name;
867 		} else {
868 			sprintf(default_name, "guest-%u", getpid());
869 			kvm->cfg.guest_name = default_name;
870 		}
871 	}
872 
873 	r = kvm__init(kvm);
874 	if (r)
875 		goto fail;
876 
877 	r = ioeventfd__init(kvm);
878 	if (r < 0) {
879 		pr_err("ioeventfd__init() failed with error %d\n", r);
880 		goto fail;
881 	}
882 
883 	r = kvm_cpu__init(kvm);
884 	if (r < 0) {
885 		pr_err("kvm_cpu__init() failed with error %d\n", r);
886 		goto fail;
887 	}
888 
889 	r = irq__init(kvm);
890 	if (r < 0) {
891 		pr_err("irq__init() failed with error %d\n", r);
892 		goto fail;
893 	}
894 
895 	r = pci__init(kvm);
896 	if (r < 0) {
897 		pr_err("pci__init() failed with error %d\n", r);
898 		goto fail;
899 	}
900 
901 	r = ioport__init(kvm);
902 	if (r < 0) {
903 		pr_err("ioport__init() failed with error %d\n", r);
904 		goto fail;
905 	}
906 
907 	/*
908 	 * vidmode should be either specified
909 	 * either set by default
910 	 */
911 	if (kvm->cfg.vnc || kvm->cfg.sdl) {
912 		if (vidmode == -1)
913 			vidmode = 0x312;
914 	} else {
915 		vidmode = 0;
916 	}
917 
918 	memset(real_cmdline, 0, sizeof(real_cmdline));
919 	kvm__arch_set_cmdline(real_cmdline, kvm->cfg.vnc || kvm->cfg.sdl);
920 
921 	if (strlen(real_cmdline) > 0)
922 		strcat(real_cmdline, " ");
923 
924 	if (kvm->cfg.kernel_cmdline)
925 		strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline));
926 
927 	if (!kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename && !kvm->cfg.initrd_filename) {
928 		char tmp[PATH_MAX];
929 
930 		kvm_setup_create_new(kvm->cfg.custom_rootfs_name);
931 		kvm_setup_resolv(kvm->cfg.custom_rootfs_name);
932 
933 		snprintf(tmp, PATH_MAX, "%s%s", kvm__get_dir(), "default");
934 		if (virtio_9p__register(kvm, tmp, "/dev/root") < 0)
935 			die("Unable to initialize virtio 9p");
936 		if (virtio_9p__register(kvm, "/", "hostfs") < 0)
937 			die("Unable to initialize virtio 9p");
938 		kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
939 	}
940 
941 	if (kvm->cfg.using_rootfs) {
942 		strcat(real_cmdline, " root=/dev/root rw rootflags=rw,trans=virtio,version=9p2000.L rootfstype=9p");
943 		if (kvm->cfg.custom_rootfs) {
944 			kvm_run_set_sandbox();
945 
946 			strcat(real_cmdline, " init=/virt/init");
947 
948 			if (!kvm->cfg.no_dhcp)
949 				strcat(real_cmdline, "  ip=dhcp");
950 			if (kvm_setup_guest_init())
951 				die("Failed to setup init for guest.");
952 		}
953 	} else if (!strstr(real_cmdline, "root=")) {
954 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
955 	}
956 
957 	r = disk_image__init(kvm);
958 	if (r < 0) {
959 		pr_err("disk_image__init() failed with error %d\n", r);
960 		goto fail;
961 	}
962 
963 	printf("  # %s run -k %s -m %Lu -c %d --name %s\n", KVM_BINARY_NAME,
964 		kvm->cfg.kernel_filename, kvm->cfg.ram_size / 1024 / 1024, kvm->cfg.nrcpus, kvm->cfg.guest_name);
965 
966 	if (!kvm->cfg.firmware_filename) {
967 		if (!kvm__load_kernel(kvm, kvm->cfg.kernel_filename,
968 				kvm->cfg.initrd_filename, real_cmdline, vidmode))
969 			die("unable to load kernel %s", kvm->cfg.kernel_filename);
970 
971 		kvm->vmlinux = kvm->cfg.vmlinux_filename;
972 		r = symbol_init(kvm);
973 		if (r < 0)
974 			pr_debug("symbol_init() failed with error %d\n", r);
975 	}
976 
977 	ioport__setup_arch();
978 
979 	r = rtc__init(kvm);
980 	if (r < 0) {
981 		pr_err("rtc__init() failed with error %d\n", r);
982 		goto fail;
983 	}
984 
985 	r = serial8250__init(kvm);
986 	if (r < 0) {
987 		pr_err("serial__init() failed with error %d\n", r);
988 		goto fail;
989 	}
990 
991 	r = virtio_blk__init(kvm);
992 	if (r < 0) {
993 		pr_err("virtio_blk__init() failed with error %d\n", r);
994 		goto fail;
995 	}
996 
997 	r = virtio_scsi_init(kvm);
998 	if (r < 0) {
999 		pr_err("virtio_scsi_init() failed with error %d\n", r);
1000 		goto fail;
1001 	}
1002 
1003 	r = virtio_console__init(kvm);
1004 	if (r < 0) {
1005 		pr_err("virtio_console__init() failed with error %d\n", r);
1006 		goto fail;
1007 	}
1008 
1009 	r = virtio_rng__init(kvm);
1010 	if (r < 0) {
1011 		pr_err("virtio_rng__init() failed with error %d\n", r);
1012 		goto fail;
1013 	}
1014 
1015 	r = virtio_bln__init(kvm);
1016 	if (r < 0) {
1017 		pr_err("virtio_rng__init() failed with error %d\n", r);
1018 		goto fail;
1019 	}
1020 
1021 	if (!kvm->cfg.network)
1022 		kvm->cfg.network = DEFAULT_NETWORK;
1023 
1024 	virtio_9p__init(kvm);
1025 
1026 	for (i = 0; i < kvm->cfg.num_net_devices; i++) {
1027 		kvm->cfg.net_params[i].kvm = kvm;
1028 		virtio_net__init(&kvm->cfg.net_params[i]);
1029 	}
1030 
1031 	if (kvm->cfg.num_net_devices == 0 && kvm->cfg.no_net == 0) {
1032 		struct virtio_net_params net_params;
1033 
1034 		net_params = (struct virtio_net_params) {
1035 			.guest_ip	= kvm->cfg.guest_ip,
1036 			.host_ip	= kvm->cfg.host_ip,
1037 			.kvm		= kvm,
1038 			.script		= kvm->cfg.script,
1039 			.mode		= NET_MODE_USER,
1040 		};
1041 		str_to_mac(kvm->cfg.guest_mac, net_params.guest_mac);
1042 		str_to_mac(kvm->cfg.host_mac, net_params.host_mac);
1043 
1044 		virtio_net__init(&net_params);
1045 	}
1046 
1047 	kvm__init_ram(kvm);
1048 
1049 #ifdef CONFIG_X86
1050 	kbd__init(kvm);
1051 #endif
1052 
1053 	r = pci_shmem__init(kvm);
1054 	if (r < 0) {
1055 		pr_err("pci_shmem__init() failed with error %d\n", r);
1056 		goto fail;
1057 	}
1058 
1059 	if (kvm->cfg.vnc || kvm->cfg.sdl) {
1060 		fb = vesa__init(kvm);
1061 		if (IS_ERR(fb)) {
1062 			pr_err("vesa__init() failed with error %ld\n", PTR_ERR(fb));
1063 			goto fail;
1064 		}
1065 	}
1066 
1067 	if (kvm->cfg.vnc && fb) {
1068 		r = vnc__init(fb);
1069 		if (r < 0) {
1070 			pr_err("vnc__init() failed with error %d\n", r);
1071 			goto fail;
1072 		}
1073 	}
1074 
1075 	if (kvm->cfg.sdl && fb) {
1076 		sdl__init(fb);
1077 		if (r < 0) {
1078 			pr_err("sdl__init() failed with error %d\n", r);
1079 			goto fail;
1080 		}
1081 	}
1082 
1083 	r = fb__init(kvm);
1084 	if (r < 0) {
1085 		pr_err("fb__init() failed with error %d\n", r);
1086 		goto fail;
1087 	}
1088 
1089 	/*
1090 	 * Device init all done; firmware init must
1091 	 * come after this (it may set up device trees etc.)
1092 	 */
1093 
1094 	r = kvm_timer__init(kvm);
1095 	if (r < 0) {
1096 		pr_err("kvm_timer__init() failed with error %d\n", r);
1097 		goto fail;
1098 	}
1099 
1100 	if (kvm->cfg.firmware_filename) {
1101 		if (!kvm__load_firmware(kvm, kvm->cfg.firmware_filename))
1102 			die("unable to load firmware image %s: %s", kvm->cfg.firmware_filename, strerror(errno));
1103 	} else {
1104 		kvm__arch_setup_firmware(kvm);
1105 		if (r < 0) {
1106 			pr_err("kvm__arch_setup_firmware() failed with error %d\n", r);
1107 			goto fail;
1108 		}
1109 	}
1110 
1111 	r = thread_pool__init(kvm);
1112 	if (r < 0) {
1113 		pr_err("thread_pool__init() failed with error %d\n", r);
1114 		goto fail;
1115 	}
1116 
1117 fail:
1118 	return r;
1119 }
1120 
1121 static int kvm_cmd_run_work(void)
1122 {
1123 	int i;
1124 	void *ret = NULL;
1125 
1126 	for (i = 0; i < kvm->nrcpus; i++) {
1127 		if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0)
1128 			die("unable to create KVM VCPU thread");
1129 	}
1130 
1131 	/* Only VCPU #0 is going to exit by itself when shutting down */
1132 	return pthread_join(kvm->cpus[0]->thread, &ret);
1133 }
1134 
1135 static void kvm_cmd_run_exit(int guest_ret)
1136 {
1137 	int r = 0;
1138 
1139 	compat__print_all_messages();
1140 
1141 	r = kvm_cpu__exit(kvm);
1142 	if (r < 0)
1143 		pr_warning("kvm_cpu__exit() failed with error %d\n", r);
1144 
1145 	r = symbol_exit(kvm);
1146 	if (r < 0)
1147 		pr_warning("symbol_exit() failed with error %d\n", r);
1148 
1149 	r = irq__exit(kvm);
1150 	if (r < 0)
1151 		pr_warning("irq__exit() failed with error %d\n", r);
1152 
1153 	r = kvm_timer__exit(kvm);
1154 	if (r < 0)
1155 		pr_warning("kvm_timer__exit() failed with error %d\n", r);
1156 
1157 	r = fb__exit(kvm);
1158 	if (r < 0)
1159 		pr_warning("kvm_timer__exit() failed with error %d\n", r);
1160 
1161 	r = virtio_scsi_exit(kvm);
1162 	if (r < 0)
1163 		pr_warning("virtio_scsi_exit() failed with error %d\n", r);
1164 
1165 	r = virtio_blk__exit(kvm);
1166 	if (r < 0)
1167 		pr_warning("virtio_blk__exit() failed with error %d\n", r);
1168 
1169 	r = virtio_rng__exit(kvm);
1170 	if (r < 0)
1171 		pr_warning("virtio_rng__exit() failed with error %d\n", r);
1172 
1173 	r = virtio_bln__exit(kvm);
1174 	if (r < 0)
1175 		pr_warning("virtio_bln__exit() failed with error %d\n", r);
1176 
1177 	r = virtio_console__exit(kvm);
1178 	if (r < 0)
1179 		pr_warning("virtio_console__exit() failed with error %d\n", r);
1180 
1181 	r = pci_shmem__exit(kvm);
1182 	if (r < 0)
1183 		pr_warning("pci_shmem__exit() failed with error %d\n", r);
1184 
1185 	r = disk_image__exit(kvm);
1186 	if (r < 0)
1187 		pr_warning("disk_image__exit() failed with error %d\n", r);
1188 
1189 	r = serial8250__exit(kvm);
1190 	if (r < 0)
1191 		pr_warning("serial8250__exit() failed with error %d\n", r);
1192 
1193 	r = rtc__exit(kvm);
1194 	if (r < 0)
1195 		pr_warning("rtc__exit() failed with error %d\n", r);
1196 
1197 	r = kvm__arch_free_firmware(kvm);
1198 	if (r < 0)
1199 		pr_warning("kvm__arch_free_firmware() failed with error %d\n", r);
1200 
1201 	r = ioport__exit(kvm);
1202 	if (r < 0)
1203 		pr_warning("ioport__exit() failed with error %d\n", r);
1204 
1205 	r = ioeventfd__exit(kvm);
1206 	if (r < 0)
1207 		pr_warning("ioeventfd__exit() failed with error %d\n", r);
1208 
1209 	r = pci__exit(kvm);
1210 	if (r < 0)
1211 		pr_warning("pci__exit() failed with error %d\n", r);
1212 
1213 	r = term_exit(kvm);
1214 	if (r < 0)
1215 		pr_warning("pci__exit() failed with error %d\n", r);
1216 
1217 	r = thread_pool__exit(kvm);
1218 	if (r < 0)
1219 		pr_warning("thread_pool__exit() failed with error %d\n", r);
1220 
1221 	r = kvm__exit(kvm);
1222 	if (r < 0)
1223 		pr_warning("pci__exit() failed with error %d\n", r);
1224 
1225 	if (guest_ret == 0)
1226 		printf("\n  # KVM session ended normally.\n");
1227 }
1228 
1229 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
1230 {
1231 	int r, ret = -EFAULT;
1232 
1233 	r = kvm_cmd_run_init(argc, argv);
1234 	if (r < 0)
1235 		return r;
1236 
1237 	ret = kvm_cmd_run_work();
1238 	kvm_cmd_run_exit(ret);
1239 
1240 	return ret;
1241 }
1242