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