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