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