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