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