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