xref: /kvmtool/builtin-run.c (revision 5e3af62de401f9e016185aba30c4d36018b63d86)
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-console.h>
20 #include <kvm/disk-image.h>
21 #include <kvm/util.h>
22 #include <kvm/pci.h>
23 #include <kvm/term.h>
24 #include <kvm/ioport.h>
25 
26 /* header files for gitish interface  */
27 #include <kvm/kvm-run.h>
28 #include <kvm/parse-options.h>
29 
30 #define DEFAULT_KVM_DEV		"/dev/kvm"
31 #define DEFAULT_CONSOLE		"serial"
32 
33 #define MB_SHIFT		(20)
34 #define MIN_RAM_SIZE_MB		(64ULL)
35 #define MIN_RAM_SIZE_BYTE	(MIN_RAM_SIZE_MB << MB_SHIFT)
36 
37 #define KVM_NR_CPUS		(255)
38 
39 static struct kvm *kvm;
40 static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
41 static __thread struct kvm_cpu *current_kvm_cpu;
42 
43 static void handle_sigint(int sig)
44 {
45 	exit(1);
46 }
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 bool single_step;
67 static bool readonly_image;
68 extern bool ioport_debug;
69 extern int  active_console;
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 const struct option options[] = {
79 	OPT_GROUP("Basic options:"),
80 	OPT_INTEGER('\0', "cpus", &nrcpus, "Number of CPUs"),
81 	OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."),
82 	OPT_STRING('i', "image", &image_filename, "image", "Disk image"),
83 	OPT_BOOLEAN('\0', "readonly", &readonly_image,
84 			"Don't write changes back to disk image"),
85 	OPT_STRING('c', "console", &console, "serial or virtio",
86 			"Console to use"),
87 
88 	OPT_GROUP("Kernel options:"),
89 	OPT_STRING('k', "kernel", &kernel_filename, "kernel",
90 			"Kernel to boot in virtual machine"),
91 	OPT_STRING('r', "initrd", &initrd_filename, "initrd",
92 			"Initial RAM disk image"),
93 	OPT_STRING('p', "params", &kernel_cmdline, "params",
94 			"Kernel command line arguments"),
95 
96 	OPT_GROUP("Debug options:"),
97 	OPT_STRING('d', "kvm-dev", &kvm_dev, "kvm-dev", "KVM device file"),
98 	OPT_BOOLEAN('s', "single-step", &single_step,
99 			"Enable single stepping"),
100 	OPT_BOOLEAN('g', "ioport-debug", &ioport_debug,
101 			"Enable ioport debugging"),
102 	OPT_END()
103 };
104 
105 static void *kvm_cpu_thread(void *arg)
106 {
107 	current_kvm_cpu		= arg;
108 
109 	if (kvm_cpu__start(current_kvm_cpu))
110 		goto panic_kvm;
111 
112 	kvm_cpu__delete(current_kvm_cpu);
113 
114 	return (void *) (intptr_t) 0;
115 
116 panic_kvm:
117 	fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n",
118 		current_kvm_cpu->kvm_run->exit_reason,
119 		kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
120 	if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
121 		fprintf(stderr, "KVM exit code: 0x%Lu\n",
122 			current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
123 	disk_image__close(kvm->disk_image);
124 	kvm_cpu__show_registers(current_kvm_cpu);
125 	kvm_cpu__show_code(current_kvm_cpu);
126 	kvm_cpu__show_page_tables(current_kvm_cpu);
127 
128 	kvm_cpu__delete(current_kvm_cpu);
129 
130 	return (void *) (intptr_t) 1;
131 }
132 
133 static char host_kernel[PATH_MAX];
134 
135 static const char *find_host_kernel(void)
136 {
137 	struct utsname uts;
138 	struct stat st;
139 
140 	if (uname(&uts) < 0)
141 		return NULL;
142 
143 	if (snprintf(host_kernel, PATH_MAX, "/boot/vmlinuz-%s", uts.release) < 0)
144 		return NULL;
145 
146 	if (stat(host_kernel, &st) < 0)
147 		return NULL;
148 
149 	if (!S_ISREG(st.st_mode))
150 		return NULL;
151 
152 	return host_kernel;
153 }
154 
155 int kvm_cmd_run(int argc, const char **argv, const char *prefix)
156 {
157 	static char real_cmdline[2048];
158 	int exit_code = 0;
159 	int i;
160 
161 	signal(SIGALRM, handle_sigalrm);
162 	signal(SIGQUIT, handle_sigquit);
163 	signal(SIGINT, handle_sigint);
164 
165 	while (argc != 0) {
166 		argc = parse_options(argc, argv, options, run_usage,
167 				PARSE_OPT_STOP_AT_NON_OPTION);
168 		if (argc != 0) {
169 			if (kernel_filename) {
170 				fprintf(stderr, "Cannot handle parameter: "
171 						"%s\n", argv[0]);
172 				usage_with_options(run_usage, options);
173 				return EINVAL;
174 			}
175 			/* first unhandled parameter is treated as a kernel
176 			   image
177 			 */
178 			kernel_filename = argv[0];
179 			argv++;
180 			argc--;
181 		}
182 
183 	}
184 
185 	if (!kernel_filename)
186 		kernel_filename = find_host_kernel();
187 
188 	if (!kernel_filename) {
189 		usage_with_options(run_usage, options);
190 		return EINVAL;
191 	}
192 
193 	if (nrcpus < 1 || nrcpus > KVM_NR_CPUS)
194 		die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS);
195 
196 	/* FIXME: Remove as only SMP gets fully supported */
197 	if (nrcpus > 1)
198 		warning("Limiting CPUs to 1, true SMP is not yet implemented");
199 
200 	if (ram_size < MIN_RAM_SIZE_MB)
201 		die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB);
202 
203 	ram_size <<= MB_SHIFT;
204 
205 	if (!kvm_dev)
206 		kvm_dev = DEFAULT_KVM_DEV;
207 
208 	if (!console)
209 		console = DEFAULT_CONSOLE;
210 
211 	if (!strncmp(console, "virtio", 6))
212 		active_console  = CONSOLE_VIRTIO;
213 	else
214 		active_console  = CONSOLE_8250;
215 
216 	term_init();
217 
218 	kvm = kvm__init(kvm_dev, ram_size);
219 
220 	if (image_filename) {
221 		kvm->disk_image	= disk_image__open(image_filename, readonly_image);
222 		if (!kvm->disk_image)
223 			die("unable to load disk image %s", image_filename);
224 	}
225 
226 	strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 ");
227 	if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) {
228 		strlcat(real_cmdline, "root=/dev/vda rw ",
229 				sizeof(real_cmdline));
230 	}
231 
232 	if (kernel_cmdline) {
233 		strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline));
234 		real_cmdline[sizeof(real_cmdline)-1] = '\0';
235 	}
236 
237 	if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,
238 				real_cmdline))
239 		die("unable to load kernel %s", kernel_filename);
240 
241 	ioport__setup_legacy();
242 
243 	kvm__setup_bios(kvm);
244 
245 	serial8250__init(kvm);
246 
247 	pci__init();
248 
249 	virtio_blk__init(kvm);
250 
251 	virtio_console__init(kvm);
252 
253 	kvm__start_timer(kvm);
254 
255 	for (i = 0; i < nrcpus; i++) {
256 		kvm_cpus[i] = kvm_cpu__init(kvm, i);
257 		if (!kvm_cpus[i])
258 			die("unable to initialize KVM VCPU");
259 
260 		if (single_step)
261 			kvm_cpu__enable_singlestep(kvm_cpus[i]);
262 
263 		if (pthread_create(&kvm_cpus[i]->thread, NULL, kvm_cpu_thread, kvm_cpus[i]) != 0)
264 			die("unable to create KVM VCPU thread");
265 	}
266 
267 	for (i = 0; i < nrcpus; i++) {
268 		void *ret;
269 
270 		if (pthread_join(kvm_cpus[i]->thread, &ret) != 0)
271 			die("pthread_join");
272 
273 		if (ret != NULL)
274 			exit_code	= 1;
275 	}
276 
277 	disk_image__close(kvm->disk_image);
278 	kvm__delete(kvm);
279 
280 	if (!exit_code)
281 		printf("\n  # KVM session ended normally.\n");
282 
283 	return exit_code;
284 }
285