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