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