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