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