xref: /kvmtool/builtin-run.c (revision 73b7d03849f6894bba8381dc8e8392b035660ae5)
1 #include <stdio.h>
2 #include <string.h>
3 #include <signal.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 #include <inttypes.h>
7 #include <stdlib.h>
8 #include <termios.h>
9 #include <sys/utsname.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 
13 /* user defined header files */
14 #include <linux/types.h>
15 #include <kvm/kvm.h>
16 #include <kvm/kvm-cpu.h>
17 #include <kvm/8250-serial.h>
18 #include <kvm/virtio-blk.h>
19 #include <kvm/virtio-net.h>
20 #include <kvm/virtio-console.h>
21 #include <kvm/disk-image.h>
22 #include <kvm/util.h>
23 #include <kvm/pci.h>
24 #include <kvm/term.h>
25 #include <kvm/ioport.h>
26 
27 /* header files for gitish interface  */
28 #include <kvm/kvm-run.h>
29 #include <kvm/parse-options.h>
30 
31 #define DEFAULT_KVM_DEV		"/dev/kvm"
32 #define DEFAULT_CONSOLE		"serial"
33 #define DEFAULT_NETWORK		"virtio"
34 #define DEFAULT_HOST_ADDR	"192.168.33.2"
35 #define DEFAULT_GUEST_MAC	"00:11:22:33:44:55"
36 #define DEFAULT_SCRIPT		"none"
37 
38 #define MB_SHIFT		(20)
39 #define MIN_RAM_SIZE_MB		(64ULL)
40 #define MIN_RAM_SIZE_BYTE	(MIN_RAM_SIZE_MB << MB_SHIFT)
41 
42 #define KVM_NR_CPUS		(255)
43 
44 static struct kvm *kvm;
45 static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
46 static __thread struct kvm_cpu *current_kvm_cpu;
47 
48 static void handle_sigquit(int sig)
49 {
50 	serial8250__inject_sysrq(kvm);
51 }
52 
53 static void handle_sigalrm(int sig)
54 {
55 	serial8250__inject_interrupt(kvm);
56 	virtio_console__inject_interrupt(kvm);
57 }
58 
59 static u64 ram_size = MIN_RAM_SIZE_MB;
60 static const char *kernel_cmdline;
61 static const char *kernel_filename;
62 static const char *initrd_filename;
63 static const char *image_filename;
64 static const char *console;
65 static const char *kvm_dev;
66 static const char *network;
67 static const char *host_ip_addr;
68 static const char *guest_mac;
69 static const char *script;
70 static bool single_step;
71 static bool readonly_image;
72 extern bool ioport_debug;
73 extern int  active_console;
74 
75 static int nrcpus = 1;
76 
77 static const char * const run_usage[] = {
78 	"kvm run [<options>] [<kernel image>]",
79 	NULL
80 };
81 
82 static const struct option options[] = {
83 	OPT_GROUP("Basic options:"),
84 	OPT_INTEGER('\0', "cpus", &nrcpus, "Number of CPUs"),
85 	OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."),
86 	OPT_STRING('i', "image", &image_filename, "image", "Disk image"),
87 	OPT_BOOLEAN('\0', "readonly", &readonly_image,
88 			"Don't write changes back to disk image"),
89 	OPT_STRING('c', "console", &console, "serial or virtio",
90 			"Console to use"),
91 
92 	OPT_GROUP("Kernel options:"),
93 	OPT_STRING('k', "kernel", &kernel_filename, "kernel",
94 			"Kernel to boot in virtual machine"),
95 	OPT_STRING('r', "initrd", &initrd_filename, "initrd",
96 			"Initial RAM disk image"),
97 	OPT_STRING('p', "params", &kernel_cmdline, "params",
98 			"Kernel command line arguments"),
99 
100 	OPT_GROUP("Networking options:"),
101 	OPT_STRING('n', "network", &network, "virtio",
102 			"Network to use"),
103 	OPT_STRING('\0', "host-ip-addr", &host_ip_addr, "a.b.c.d",
104 			"Assign this address to the host side networking"),
105 	OPT_STRING('\0', "guest-mac", &guest_mac, "aa:bb:cc:dd:ee:ff",
106 			"Assign this address to the guest side NIC"),
107 	OPT_STRING('\0', "tapscript", &script, "Script path",
108 			 "Assign a script to process created tap device"),
109 	OPT_GROUP("Debug options:"),
110 	OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"),
111 	OPT_BOOLEAN('s', "single-step", &single_step,
112 			"Enable single stepping"),
113 	OPT_BOOLEAN('g', "ioport-debug", &ioport_debug,
114 			"Enable ioport debugging"),
115 	OPT_END()
116 };
117 
118 static void *kvm_cpu_thread(void *arg)
119 {
120 	current_kvm_cpu		= arg;
121 
122 	if (kvm_cpu__start(current_kvm_cpu))
123 		goto panic_kvm;
124 
125 	kvm_cpu__delete(current_kvm_cpu);
126 
127 	return (void *) (intptr_t) 0;
128 
129 panic_kvm:
130 	fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n",
131 		current_kvm_cpu->kvm_run->exit_reason,
132 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
133 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
134 		fprintf(stderr, "KVM exit code: 0x%Lu\n",
135 			current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
136 	disk_image__close(kvm->disk_image);
137 	kvm_cpu__show_registers(current_kvm_cpu);
138 	kvm_cpu__show_code(current_kvm_cpu);
139 	kvm_cpu__show_page_tables(current_kvm_cpu);
140 
141 	kvm_cpu__delete(current_kvm_cpu);
142 
143 	return (void *) (intptr_t) 1;
144 }
145 
146 static char kernel[PATH_MAX];
147 const char *host_kernels[] = {
148 	"/boot/vmlinuz",
149 	"/boot/bzImage",
150 	NULL
151 };
152 const char *defult_kernels[] = {
153 	"./bzImage",
154 	"../../arch/x86/boot/bzImage",
155 	NULL
156 };
157 
158 static void kernel_usage_with_options(void)
159 {
160 	const char **k;
161 	struct utsname uts;
162 
163 	fprintf(stderr, "Fatal: could not find default kernel image in:\n");
164 	k = &defult_kernels[0];
165 	while (*k) {
166 		fprintf(stderr, "\t%s\n", *k);
167 		k++;
168 	}
169 
170 	if (uname(&uts) < 0)
171 		return;
172 
173 	k = &host_kernels[0];
174 	while (*k) {
175 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
176 			return;
177 		fprintf(stderr, "\t%s\n", kernel);
178 		k++;
179 	}
180 	usage_with_options(run_usage, options);
181 }
182 
183 static const char *find_kernel(void)
184 {
185 	const char **k;
186 	struct stat st;
187 	struct utsname uts;
188 
189 	k = &defult_kernels[0];
190 	while (*k) {
191 		if (stat(*k, &st) < 0 || !S_ISREG(st.st_mode)) {
192 			k++;
193 			continue;
194 		}
195 		strncpy(kernel, *k, PATH_MAX);
196 		return kernel;
197 	}
198 
199 	if (uname(&uts) < 0)
200 		return NULL;
201 
202 	k = &host_kernels[0];
203 	while (*k) {
204 		if (snprintf(kernel, PATH_MAX, "%s-%s", *k, uts.release) < 0)
205 			return NULL;
206 
207 		if (stat(kernel, &st) < 0 || !S_ISREG(st.st_mode)) {
208 			k++;
209 			continue;
210 		}
211 		return kernel;
212 
213 	}
214 	return NULL;
215 }
216 
217 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
218 {
219 	static char real_cmdline[2048];
220 	int exit_code = 0;
221 	int i;
222 	struct virtio_net_parameters net_params;
223 
224 	signal(SIGALRM, handle_sigalrm);
225 	signal(SIGQUIT, handle_sigquit);
226 
227 	while (argc != 0) {
228 		argc = parse_options(argc, argv, options, run_usage,
229 				PARSE_OPT_STOP_AT_NON_OPTION);
230 		if (argc != 0) {
231 			if (kernel_filename) {
232 				fprintf(stderr, "Cannot handle parameter: "
233 						"%s\n", argv[0]);
234 				usage_with_options(run_usage, options);
235 				return EINVAL;
236 			}
237 			/* first unhandled parameter is treated as a kernel
238 			   image
239 			 */
240 			kernel_filename = argv[0];
241 			argv++;
242 			argc--;
243 		}
244 
245 	}
246 
247 	if (!kernel_filename)
248 		kernel_filename = find_kernel();
249 
250 	if (!kernel_filename) {
251 		kernel_usage_with_options();
252 		return EINVAL;
253 	}
254 
255 	if (nrcpus < 1 || nrcpus > KVM_NR_CPUS)
256 		die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS);
257 
258 	/* FIXME: Remove as only SMP gets fully supported */
259 	if (nrcpus > 1)
260 		warning("Limiting CPUs to 1, true SMP is not yet implemented");
261 
262 	if (ram_size < MIN_RAM_SIZE_MB)
263 		die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB);
264 
265 	ram_size <<= MB_SHIFT;
266 
267 	if (!kvm_dev)
268 		kvm_dev = DEFAULT_KVM_DEV;
269 
270 	if (!console)
271 		console = DEFAULT_CONSOLE;
272 
273 	if (!strncmp(console, "virtio", 6))
274 		active_console  = CONSOLE_VIRTIO;
275 	else
276 		active_console  = CONSOLE_8250;
277 
278 	if (!host_ip_addr)
279 		host_ip_addr = DEFAULT_HOST_ADDR;
280 
281 	if (!guest_mac)
282 		guest_mac = DEFAULT_GUEST_MAC;
283 
284 	if (!script)
285 		script = DEFAULT_SCRIPT;
286 
287 	term_init();
288 
289 	kvm = kvm__init(kvm_dev, ram_size);
290 
291 	if (image_filename) {
292 		kvm->disk_image	= disk_image__open(image_filename, readonly_image);
293 		if (!kvm->disk_image)
294 			die("unable to load disk image %s", image_filename);
295 	}
296 
297 	strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 ");
298 	if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) {
299 		strlcat(real_cmdline, "root=/dev/vda rw ",
300 				sizeof(real_cmdline));
301 	}
302 
303 	if (kernel_cmdline) {
304 		strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline));
305 		real_cmdline[sizeof(real_cmdline)-1] = '\0';
306 	}
307 
308 	if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,
309 				real_cmdline))
310 		die("unable to load kernel %s", kernel_filename);
311 
312 	ioport__setup_legacy();
313 
314 	kvm__setup_bios(kvm);
315 
316 	serial8250__init(kvm);
317 
318 	pci__init();
319 
320 	virtio_blk__init(kvm);
321 
322 	virtio_console__init(kvm);
323 
324 	if (!network)
325 		network = DEFAULT_NETWORK;
326 
327 	if (!strncmp(network, "virtio", 6)) {
328 		net_params = (struct virtio_net_parameters) {
329 			.host_ip = host_ip_addr,
330 			.self = kvm,
331 			.script = script
332 		};
333 		sscanf(guest_mac,	"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
334 							net_params.guest_mac,
335 							net_params.guest_mac+1,
336 							net_params.guest_mac+2,
337 							net_params.guest_mac+3,
338 							net_params.guest_mac+4,
339 							net_params.guest_mac+5);
340 
341 		virtio_net__init(&net_params);
342 	}
343 
344 	kvm__start_timer(kvm);
345 
346 	for (i = 0; i < nrcpus; i++) {
347 		kvm_cpus[i] = kvm_cpu__init(kvm, i);
348 		if (!kvm_cpus[i])
349 			die("unable to initialize KVM VCPU");
350 
351 		if (single_step)
352 			kvm_cpu__enable_singlestep(kvm_cpus[i]);
353 
354 		if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0)
355 			die("unable to create KVM VCPU thread");
356 	}
357 
358 	for (i = 0; i < nrcpus; i++) {
359 		void *ret;
360 
361 		if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
362 			die("pthread_join");
363 
364 		if (ret != NULL)
365 			exit_code	= 1;
366 	}
367 
368 	disk_image__close(kvm->disk_image);
369 	kvm__delete(kvm);
370 
371 	if (!exit_code)
372 		printf("\n  # KVM session ended normally.\n");
373 
374 	return exit_code;
375 }
376