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