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