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