xref: /kvmtool/builtin-run.c (revision 5d1a249ce504d81a87c548dcf110cc224a1c09d3) !
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 	strcpy(real_cmdline, "notsc nolapic noacpi pci=conf1 console=ttyS0 ");
147 	if (!kernel_cmdline || !strstr(kernel_cmdline, "root=")) {
148 		strlcat(real_cmdline, "root=/dev/vda rw ",
149 				sizeof(real_cmdline));
150 	}
151 
152 	if (kernel_cmdline) {
153 		strlcat(real_cmdline, kernel_cmdline, sizeof(real_cmdline));
154 		real_cmdline[sizeof(real_cmdline)-1] = '\0';
155 	}
156 
157 	if (!kvm__load_kernel(kvm, kernel_filename, initrd_filename,
158 				real_cmdline))
159 		die("unable to load kernel %s", kernel_filename);
160 
161 	kvm__setup_bios(kvm);
162 
163 	serial8250__init(kvm);
164 
165 	pci__init();
166 
167 	virtio_blk__init(kvm);
168 
169 	virtio_console__init(kvm);
170 
171 	kvm__start_timer(kvm);
172 
173 	if (single_step)
174 		kvm_cpu__enable_singlestep(cpu);
175 
176 	if (kvm_cpu__start(cpu))
177 		goto panic_kvm;
178 
179 	disk_image__close(kvm->disk_image);
180 	kvm__delete(kvm);
181 
182 	printf("\n  # KVM session ended normally.\n");
183 
184 	return 0;
185 
186 panic_kvm:
187 	fprintf(stderr, "KVM exit reason: %" PRIu32 " (\"%s\")\n",
188 		cpu->kvm_run->exit_reason,
189 		kvm_exit_reasons[cpu->kvm_run->exit_reason]);
190 	if (cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN)
191 		fprintf(stderr, "KVM exit code: 0x%" PRIu64 "\n",
192 			cpu->kvm_run->hw.hardware_exit_reason);
193 	disk_image__close(kvm->disk_image);
194 	kvm_cpu__show_registers(cpu);
195 	kvm_cpu__show_code(cpu);
196 	kvm_cpu__show_page_tables(cpu);
197 	kvm_cpu__delete(cpu);
198 	kvm__delete(kvm);
199 
200 	return 1;
201 }
202