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