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