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