xref: /kvmtool/builtin-run.c (revision aba1efa5fb47776d4212c7626e5013331f550712)
1 #include <stdio.h>
2 #include <string.h>
3 #include <signal.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <termios.h>
7 #include <sys/utsname.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <ctype.h>
11 
12 /* user defined header files */
13 #include <linux/types.h>
14 #include <kvm/kvm.h>
15 #include <kvm/kvm-cpu.h>
16 #include <kvm/8250-serial.h>
17 #include <kvm/virtio-blk.h>
18 #include <kvm/virtio-net.h>
19 #include <kvm/virtio-console.h>
20 #include <kvm/virtio-rng.h>
21 #include <kvm/disk-image.h>
22 #include <kvm/util.h>
23 #include <kvm/pci.h>
24 #include <kvm/rtc.h>
25 #include <kvm/term.h>
26 #include <kvm/ioport.h>
27 #include <kvm/threadpool.h>
28 #include <kvm/barrier.h>
29 #include <kvm/symbol.h>
30 #include <kvm/virtio-9p.h>
31 #include <kvm/vesa.h>
32 #include <kvm/ioeventfd.h>
33 #include <kvm/i8042.h>
34 #include <kvm/vnc.h>
35 #include <kvm/framebuffer.h>
36 
37 /* header files for gitish interface  */
38 #include <kvm/kvm-run.h>
39 #include <kvm/parse-options.h>
40 #include <kvm/mutex.h>
41 
42 #define DEFAULT_KVM_DEV		"/dev/kvm"
43 #define DEFAULT_CONSOLE		"serial"
44 #define DEFAULT_NETWORK		"virtio"
45 #define DEFAULT_HOST_ADDR	"192.168.33.2"
46 #define DEFAULT_GUEST_MAC	"00:11:22:33:44:55"
47 #define DEFAULT_SCRIPT		"none"
48 
49 #define MB_SHIFT		(20)
50 #define MIN_RAM_SIZE_MB		(64ULL)
51 #define MIN_RAM_SIZE_BYTE	(MIN_RAM_SIZE_MB << MB_SHIFT)
52 
53 struct kvm *kvm;
54 struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
55 __thread struct kvm_cpu *current_kvm_cpu;
56 
57 static u64 ram_size;
58 static u8  image_count;
59 static int virtio_rng;
60 static const char *kernel_cmdline;
61 static const char *kernel_filename;
62 static const char *vmlinux_filename;
63 static const char *initrd_filename;
64 static const char *image_filename[MAX_DISK_IMAGES];
65 static const char *console;
66 static const char *kvm_dev;
67 static const char *network;
68 static const char *host_ip_addr;
69 static const char *guest_mac;
70 static const char *script;
71 static const char *virtio_9p_dir;
72 static bool single_step;
73 static bool readonly_image[MAX_DISK_IMAGES];
74 static bool vnc;
75 extern bool ioport_debug;
76 extern int  active_console;
77 
78 bool do_debug_print = false;
79 
80 static int nrcpus;
81 
82 static const char * const run_usage[] = {
83 	"kvm run [<options>] [<kernel image>]",
84 	NULL
85 };
86 
87 static int img_name_parser(const struct option *opt, const char *arg, int unset)
88 {
89 	char *sep;
90 
91 	if (image_count >= MAX_DISK_IMAGES)
92 		die("Currently only 4 images are supported");
93 
94 	image_filename[image_count] = arg;
95 	sep = strstr(arg, ",");
96 	if (sep) {
97 		if (strcmp(sep + 1, "ro") == 0)
98 			readonly_image[image_count] = 1;
99 		*sep = 0;
100 	}
101 
102 	image_count++;
103 
104 	return 0;
105 }
106 
107 static const struct option options[] = {
108 	OPT_GROUP("Basic options:"),
109 	OPT_INTEGER('c', "cpus", &nrcpus, "Number of CPUs"),
110 	OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."),
111 	OPT_CALLBACK('d', "disk", NULL, "image", "Disk image", img_name_parser),
112 	OPT_STRING('\0', "console", &console, "serial or virtio",
113 			"Console to use"),
114 	OPT_INCR('\0', "rng", &virtio_rng,
115 			"Enable virtio Random Number Generator"),
116 	OPT_STRING('\0', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"),
117 	OPT_STRING('\0', "virtio-9p", &virtio_9p_dir, "root dir",
118 			"Enable 9p over virtio"),
119 	OPT_BOOLEAN('\0', "vnc", &vnc, "Enable VNC framebuffer"),
120 
121 	OPT_GROUP("Kernel options:"),
122 	OPT_STRING('k', "kernel", &kernel_filename, "kernel",
123 			"Kernel to boot in virtual machine"),
124 	OPT_STRING('i', "initrd", &initrd_filename, "initrd",
125 			"Initial RAM disk image"),
126 	OPT_STRING('p', "params", &kernel_cmdline, "params",
127 			"Kernel command line arguments"),
128 
129 	OPT_GROUP("Networking options:"),
130 	OPT_STRING('n', "network", &network, "virtio",
131 			"Network to use"),
132 	OPT_STRING('\0', "host-ip-addr", &host_ip_addr, "a.b.c.d",
133 			"Assign this address to the host side networking"),
134 	OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff",
135 			"Assign this address to the guest side NIC"),
136 	OPT_STRING('\0', "tapscript", &script, "Script path",
137 			 "Assign a script to process created tap device"),
138 
139 	OPT_GROUP("Debug options:"),
140 	OPT_BOOLEAN('\0', "debug", &do_debug_print,
141 			"Enable debug messages"),
142 	OPT_BOOLEAN('\0', "debug-single-step", &single_step,
143 			"Enable single stepping"),
144 	OPT_BOOLEAN('\0', "debug-ioport-debug", &ioport_debug,
145 			"Enable ioport debugging"),
146 	OPT_END()
147 };
148 
149 /*
150  * Serialize debug printout so that the output of multiple vcpus does not
151  * get mixed up:
152  */
153 static int printout_done;
154 
155 static void handle_sigusr1(int sig)
156 {
157 	struct kvm_cpu *cpu = current_kvm_cpu;
158 
159 	if (!cpu)
160 		return;
161 
162 	printf("\n #\n # vCPU #%ld's dump:\n #\n", cpu->cpu_id);
163 	kvm_cpu__show_registers(cpu);
164 	kvm_cpu__show_code(cpu);
165 	kvm_cpu__show_page_tables(cpu);
166 	fflush(stdout);
167 	printout_done = 1;
168 	mb();
169 }
170 
171 /* Pause/resume the guest using SIGUSR2 */
172 static int is_paused;
173 
174 static void handle_sigusr2(int sig)
175 {
176 	if (is_paused)
177 		kvm__continue();
178 	else
179 		kvm__pause();
180 
181 	is_paused = !is_paused;
182 	pr_info("Guest %s\n", is_paused ? "paused" : "resumed");
183 }
184 
185 static void handle_sigquit(int sig)
186 {
187 	int i;
188 
189 	for (i = 0; i < nrcpus; i++) {
190 		struct kvm_cpu *cpu = kvm_cpus[i];
191 
192 		if (!cpu)
193 			continue;
194 
195 		printout_done = 0;
196 		pthread_kill(cpu->thread, SIGUSR1);
197 		/*
198 		 * Wait for the vCPU to dump state before signalling
199 		 * the next thread. Since this is debug code it does
200 		 * not matter that we are burning CPU time a bit:
201 		 */
202 		while (!printout_done)
203 			mb();
204 	}
205 
206 	serial8250__inject_sysrq(kvm);
207 }
208 
209 static void handle_sigalrm(int sig)
210 {
211 	serial8250__inject_interrupt(kvm);
212 	virtio_console__inject_interrupt(kvm);
213 }
214 
215 static void *kvm_cpu_thread(void *arg)
216 {
217 	current_kvm_cpu		= arg;
218 
219 	if (kvm_cpu__start(current_kvm_cpu))
220 		goto panic_kvm;
221 
222 	kvm_cpu__delete(current_kvm_cpu);
223 
224 	return (void *) (intptr_t) 0;
225 
226 panic_kvm:
227 	fprintf(stderr, "KVM exit reason: %u (\"%s\")\n",
228 		current_kvm_cpu->kvm_run->exit_reason,
229 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
230 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
231 		fprintf(stderr, "KVM exit code: 0x%Lu\n",
232 			current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
233 
234 	kvm_cpu__show_registers(current_kvm_cpu);
235 	kvm_cpu__show_code(current_kvm_cpu);
236 	kvm_cpu__show_page_tables(current_kvm_cpu);
237 
238 	kvm_cpu__delete(current_kvm_cpu);
239 
240 	return (void *) (intptr_t) 1;
241 }
242 
243 static char kernel[PATH_MAX];
244 
245 static const char *host_kernels[] = {
246 	"/boot/vmlinuz",
247 	"/boot/bzImage",
248 	NULL
249 };
250 
251 static const char *default_kernels[] = {
252 	"./bzImage",
253 	"../../arch/x86/boot/bzImage",
254 	NULL
255 };
256 
257 static const char *default_vmlinux[] = {
258 	"../../../vmlinux",
259 	"../../vmlinux",
260 	NULL
261 };
262 
263 static void kernel_usage_with_options(void)
264 {
265 	const char **k;
266 	struct utsname uts;
267 
268 	fprintf(stderr, "Fatal: could not find default kernel image in:\n");
269 	k = &default_kernels[0];
270 	while (*k) {
271 		fprintf(stderr, "\t%s\n", *k);
272 		k++;
273 	}
274 
275 	if (uname(&uts) < 0)
276 		return;
277 
278 	k = &host_kernels[0];
279 	while (*k) {
280 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
281 			return;
282 		fprintf(stderr, "\t%s\n", kernel);
283 		k++;
284 	}
285 	fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n");
286 }
287 
288 static u64 host_ram_size(void)
289 {
290 	long page_size;
291 	long nr_pages;
292 
293 	nr_pages	= sysconf(_SC_PHYS_PAGES);
294 	if (nr_pages < 0) {
295 		pr_warning("sysconf(_SC_PHYS_PAGES) failed");
296 		return 0;
297 	}
298 
299 	page_size	= sysconf(_SC_PAGE_SIZE);
300 	if (page_size < 0) {
301 		pr_warning("sysconf(_SC_PAGE_SIZE) failed");
302 		return 0;
303 	}
304 
305 	return (nr_pages * page_size) >> MB_SHIFT;
306 }
307 
308 /*
309  * If user didn't specify how much memory it wants to allocate for the guest,
310  * avoid filling the whole host RAM.
311  */
312 #define RAM_SIZE_RATIO		0.8
313 
314 static u64 get_ram_size(int nr_cpus)
315 {
316 	long available;
317 	long ram_size;
318 
319 	ram_size	= 64 * (nr_cpus + 3);
320 
321 	available	= host_ram_size() * RAM_SIZE_RATIO;
322 	if (!available)
323 		available = MIN_RAM_SIZE_MB;
324 
325 	if (ram_size > available)
326 		ram_size	= available;
327 
328 	return ram_size;
329 }
330 
331 static const char *find_kernel(void)
332 {
333 	const char **k;
334 	struct stat st;
335 	struct utsname uts;
336 
337 	k = &default_kernels[0];
338 	while (*k) {
339 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
340 			k++;
341 			continue;
342 		}
343 		strncpy(kernel, *k, PATH_MAX);
344 		return kernel;
345 	}
346 
347 	if (uname(&uts) < 0)
348 		return NULL;
349 
350 	k = &host_kernels[0];
351 	while (*k) {
352 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
353 			return NULL;
354 
355 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
356 			k++;
357 			continue;
358 		}
359 		return kernel;
360 
361 	}
362 	return NULL;
363 }
364 
365 static const char *find_vmlinux(void)
366 {
367 	const char **vmlinux;
368 
369 	vmlinux = &default_vmlinux[0];
370 	while (*vmlinux) {
371 		struct stat st;
372 
373 		if (stat(*vmlinux, &st) < 0 || !S_ISREG(st.st_mode)) {
374 			vmlinux++;
375 			continue;
376 		}
377 		return *vmlinux;
378 	}
379 	return NULL;
380 }
381 
382 static int root_device(char *dev, long *part)
383 {
384 	struct stat st;
385 
386 	if (stat("/", &st) < 0)
387 		return -1;
388 
389 	*part = minor(st.st_dev);
390 
391 	sprintf(dev, "/dev/block/%u:0", major(st.st_dev));
392 	if (access(dev, R_OK) < 0)
393 		return -1;
394 
395 	return 0;
396 }
397 
398 static char *host_image(char *cmd_line, size_t size)
399 {
400 	char *t;
401 	char device[PATH_MAX];
402 	long part = 0;
403 
404 	t = malloc(PATH_MAX);
405 	if (!t)
406 		return NULL;
407 
408 	/* check for the root file system */
409 	if (root_device(device, &part) < 0) {
410 		free(t);
411 		return NULL;
412 	}
413 	strncpy(t, device, PATH_MAX);
414 	if (!strstr(cmd_line, "root=")) {
415 		char tmp[PATH_MAX];
416 		snprintf(tmp, sizeof(tmp), "root=/dev/vda%ld rw ", part);
417 		strlcat(cmd_line, tmp, size);
418 	}
419 	return t;
420 }
421 
422 void kvm_run_help(void)
423 {
424 	usage_with_options(run_usage, options);
425 }
426 
427 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
428 {
429 	struct virtio_net_parameters net_params;
430 	static char real_cmdline[2048];
431 	struct framebuffer *fb = NULL;
432 	unsigned int nr_online_cpus;
433 	int exit_code = 0;
434 	u16 vidmode = 0;
435 	int max_cpus;
436 	char *hi;
437 	int i;
438 	void *ret;
439 
440 	signal(SIGALRM, handle_sigalrm);
441 	signal(SIGQUIT, handle_sigquit);
442 	signal(SIGUSR1, handle_sigusr1);
443 	signal(SIGUSR2, handle_sigusr2);
444 
445 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
446 
447 	while (argc != 0) {
448 		argc = parse_options(argc, argv, options, run_usage,
449 				PARSE_OPT_STOP_AT_NON_OPTION);
450 		if (argc != 0) {
451 			if (kernel_filename) {
452 				fprintf(stderr, "Cannot handle parameter: "
453 						"%s\n", argv[0]);
454 				usage_with_options(run_usage, options);
455 				return EINVAL;
456 			}
457 			/* first unhandled parameter is treated as a kernel
458 			   image
459 			 */
460 			kernel_filename = argv[0];
461 			argv++;
462 			argc--;
463 		}
464 
465 	}
466 
467 	if (!kernel_filename)
468 		kernel_filename = find_kernel();
469 
470 	if (!kernel_filename) {
471 		kernel_usage_with_options();
472 		return EINVAL;
473 	}
474 
475 	vmlinux_filename = find_vmlinux();
476 
477 	if (nrcpus == 0)
478 		nrcpus = nr_online_cpus;
479 	else if (nrcpus < 1 || nrcpus > KVM_NR_CPUS)
480 		die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS);
481 
482 	if (!ram_size)
483 		ram_size	= get_ram_size(nrcpus);
484 
485 	if (ram_size < MIN_RAM_SIZE_MB)
486 		die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB);
487 
488 	if (ram_size > host_ram_size())
489 		pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size, host_ram_size());
490 
491 	ram_size <<= MB_SHIFT;
492 
493 	if (!kvm_dev)
494 		kvm_dev = DEFAULT_KVM_DEV;
495 
496 	if (!console)
497 		console = DEFAULT_CONSOLE;
498 
499 	if (!strncmp(console, "virtio", 6))
500 		active_console  = CONSOLE_VIRTIO;
501 	else
502 		active_console  = CONSOLE_8250;
503 
504 	if (!host_ip_addr)
505 		host_ip_addr = DEFAULT_HOST_ADDR;
506 
507 	if (!guest_mac)
508 		guest_mac = DEFAULT_GUEST_MAC;
509 
510 	if (!script)
511 		script = DEFAULT_SCRIPT;
512 
513 	if (virtio_9p_dir) {
514 		char tmp[PATH_MAX];
515 
516 		if (realpath(virtio_9p_dir, tmp))
517 			virtio_9p__init(kvm, tmp);
518 		else
519 			die("Failed resolving 9p path");
520 	}
521 
522 	symbol__init(vmlinux_filename);
523 
524 	term_init();
525 
526 	kvm = kvm__init(kvm_dev, ram_size);
527 
528 	ioeventfd__init();
529 
530 	max_cpus = kvm__max_cpus(kvm);
531 
532 	if (nrcpus > max_cpus) {
533 		printf("  # Limit the number of CPUs to %d\n", max_cpus);
534 		kvm->nrcpus	= max_cpus;
535 	}
536 
537 	kvm->nrcpus = nrcpus;
538 
539 	memset(real_cmdline, 0, sizeof(real_cmdline));
540 	strcpy(real_cmdline, "notsc noapic noacpi pci=conf1");
541 	if (vnc) {
542 		strcat(real_cmdline, " video=vesafb console=tty0");
543 		vidmode = 0x312;
544 	} else {
545 		strcat(real_cmdline, " console=ttyS0 earlyprintk=serial");
546 	}
547 	strcat(real_cmdline, " ");
548 	if (kernel_cmdline)
549 		strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline));
550 
551 	hi = NULL;
552 	if (!image_filename[0]) {
553 		hi = host_image(real_cmdline, sizeof(real_cmdline));
554 		if (hi) {
555 			image_filename[0] = hi;
556 			readonly_image[0] = true;
557 			image_count++;
558 		}
559 	}
560 
561 	if (!strstr(real_cmdline, "root="))
562 		strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
563 
564 	if (image_count) {
565 		kvm->nr_disks = image_count;
566 		kvm->disks    = disk_image__open_all(image_filename, readonly_image, image_count);
567 		if (!kvm->disks)
568 			die("Unable to load all disk images.");
569 
570 		virtio_blk__init_all(kvm);
571 	}
572 
573 	free(hi);
574 
575 	printf("  # kvm run -k %s -m %Lu -c %d\n", kernel_filename, ram_size / 1024 / 1024, nrcpus);
576 
577 	if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,
578 				real_cmdline, vidmode))
579 		die("unable to load kernel %s", kernel_filename);
580 
581 	kvm->vmlinux		= vmlinux_filename;
582 
583 	ioport__setup_legacy();
584 
585 	rtc__init();
586 
587 	serial8250__init(kvm);
588 
589 	pci__init();
590 
591 	if (active_console == CONSOLE_VIRTIO)
592 		virtio_console__init(kvm);
593 
594 	if (virtio_rng)
595 		while (virtio_rng--)
596 			virtio_rng__init(kvm);
597 
598 	if (!network)
599 		network = DEFAULT_NETWORK;
600 
601 	if (!strncmp(network, "virtio", 6)) {
602 		net_params = (struct virtio_net_parameters) {
603 			.host_ip = host_ip_addr,
604 			.kvm = kvm,
605 			.script = script
606 		};
607 		sscanf(guest_mac,	"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
608 							net_params.guest_mac,
609 							net_params.guest_mac+1,
610 							net_params.guest_mac+2,
611 							net_params.guest_mac+3,
612 							net_params.guest_mac+4,
613 							net_params.guest_mac+5);
614 
615 		virtio_net__init(&net_params);
616 	}
617 
618 	kvm__start_timer(kvm);
619 
620 	kvm__setup_bios(kvm);
621 
622 	for (i = 0; i < nrcpus; i++) {
623 		kvm_cpus[i] = kvm_cpu__init(kvm, i);
624 		if (!kvm_cpus[i])
625 			die("unable to initialize KVM VCPU");
626 
627 		if (single_step)
628 			kvm_cpu__enable_singlestep(kvm_cpus[i]);
629 	}
630 
631 	kvm__init_ram(kvm);
632 
633 	if (vnc) {
634 		kbd__init(kvm);
635 		fb = vesa__init(kvm);
636 	}
637 
638 	if (fb)
639 		vnc__init(fb);
640 
641 	fb__start();
642 
643 	thread_pool__init(nr_online_cpus);
644 	ioeventfd__start();
645 
646 	for (i = 0; i < nrcpus; i++) {
647 		if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0)
648 			die("unable to create KVM VCPU thread");
649 	}
650 
651 	/* Only VCPU #0 is going to exit by itself when shutting down */
652 	if (pthread_join(kvm_cpus[0]->thread, &ret) != 0)
653 		exit_code = 1;
654 
655 	for (i = 1; i < nrcpus; i++) {
656 		pthread_kill(kvm_cpus[i]->thread, SIGKVMEXIT);
657 		if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
658 			die("pthread_join");
659 
660 		if (ret != NULL)
661 			exit_code = 1;
662 	}
663 
664 	fb__stop();
665 
666 	virtio_blk__delete_all(kvm);
667 	virtio_rng__delete_all(kvm);
668 
669 	disk_image__close_all(kvm->disks, image_count);
670 	kvm__delete(kvm);
671 
672 	if (!exit_code)
673 		printf("\n  # KVM session ended normally.\n");
674 
675 	return exit_code;
676 }
677