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