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