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