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