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