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