-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin-run.c
More file actions
372 lines (295 loc) · 9.83 KB
/
builtin-run.c
File metadata and controls
372 lines (295 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "kvm/builtin-run.h"
#include "kvm/8250-serial.h"
#include "kvm/barrier.h"
#include "kvm/builtin-debug.h"
#include "kvm/disk-image.h"
#include "kvm/framebuffer.h"
#include "kvm/guest_compat.h"
#include "kvm/i8042.h"
#include "kvm/ioeventfd.h"
#include "kvm/ioport.h"
#include "kvm/irq.h"
#include "kvm/kvm-cpu.h"
#include "kvm/kvm-ipc.h"
#include "kvm/kvm.h"
#include "kvm/mutex.h"
#include "kvm/pci.h"
#include "kvm/rtc.h"
#include "kvm/sdl.h"
#include "kvm/strbuf.h"
#include "kvm/symbol.h"
#include "kvm/term.h"
#include "kvm/threadpool.h"
#include "kvm/util.h"
#include "kvm/vesa.h"
#include "kvm/virtio-9p.h"
#include "kvm/virtio-balloon.h"
#include "kvm/virtio-blk.h"
#include "kvm/virtio-console.h"
#include "kvm/virtio-net.h"
#include "kvm/virtio-rng.h"
#include "kvm/virtio-scsi.h"
#include "kvm/vnc.h"
#include <ctype.h>
#include <linux/err.h>
#include <linux/sizes.h>
#include <linux/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
#define KB_SHIFT (10)
#define MB_SHIFT (20)
#define GB_SHIFT (30)
#define TB_SHIFT (40)
#define PB_SHIFT (50)
__thread struct kvm_cpu *current_kvm_cpu;
int loglevel = LOGLEVEL_INFO;
static void *kvm_cpu_thread(void *arg) {
char name[16];
current_kvm_cpu = arg;
sprintf(name, "kvm-vcpu-%lu", current_kvm_cpu->cpu_id);
kvm__set_thread_name(name);
if (kvm_cpu__start(current_kvm_cpu))
goto panic_kvm;
return ERR_PTR(0);
panic_kvm:
pr_err("KVM exit reason: %u (\"%s\")", current_kvm_cpu->kvm_run->exit_reason,
kvm_exit_reasons[current_kvm_cpu->kvm_run->exit_reason]);
if (current_kvm_cpu->kvm_run->exit_reason == KVM_EXIT_UNKNOWN) {
pr_err("KVM exit code: %llu", (unsigned long long)current_kvm_cpu->kvm_run->hw.hardware_exit_reason);
}
kvm_cpu__set_debug_fd(STDOUT_FILENO);
kvm_cpu__show_registers(current_kvm_cpu);
kvm_cpu__show_code(current_kvm_cpu);
kvm_cpu__show_page_tables(current_kvm_cpu);
return ERR_PTR(1);
}
static char kernel[PATH_MAX];
static long host_page_size(void) {
long page_size = sysconf(_SC_PAGE_SIZE);
if (page_size < 0) {
pr_warning("sysconf(_SC_PAGE_SIZE) failed");
return 0;
}
return page_size;
}
static long host_ram_nrpages(void) {
long nr_pages = sysconf(_SC_PHYS_PAGES);
if (nr_pages < 0) {
pr_warning("sysconf(_SC_PHYS_PAGES) failed");
return 0;
}
return nr_pages;
}
static u64 host_ram_size(void) {
long page_size = host_page_size();
long nr_pages = host_ram_nrpages();
return (u64)nr_pages * page_size;
}
/*
* If user didn't specify how much memory it wants to allocate for the guest,
* avoid filling the whole host RAM.
*/
#define RAM_SIZE_RATIO 0.8
static u64 get_ram_size(int nr_cpus) {
long nr_pages_available = host_ram_nrpages() * RAM_SIZE_RATIO;
u64 ram_size = (u64)SZ_64M * (nr_cpus + 3);
u64 available = MIN_RAM_SIZE;
if (nr_pages_available) {
available = nr_pages_available * host_page_size();
}
if (ram_size > available) {
ram_size = available;
}
return ram_size;
}
static void kvm_run_set_real_cmdline(struct kvm *kvm) {
static char real_cmdline[2048];
bool video;
video = kvm->cfg.vnc || kvm->cfg.sdl || kvm->cfg.gtk;
memset(real_cmdline, 0, sizeof(real_cmdline));
kvm__arch_set_cmdline(real_cmdline, video);
if (video) {
strcat(real_cmdline, " console=tty0");
} else {
switch (kvm->cfg.active_console) {
case CONSOLE_HV:
/* Fallthrough */
case CONSOLE_VIRTIO:
strcat(real_cmdline, " console=hvc0");
break;
case CONSOLE_8250:
strcat(real_cmdline, " console=ttyS0");
break;
}
}
if (kvm->cfg.using_rootfs) {
strcat(real_cmdline, " rw rootflags=trans=virtio,version=9p2000.L,cache=loose rootfstype=9p");
if (kvm->cfg.custom_rootfs) {
strcat(real_cmdline, " init=/virt/init");
}
} else if (!kvm->cfg.kernel_cmdline || !strstr(kvm->cfg.kernel_cmdline, "root=")) {
strlcat(real_cmdline, " root=/dev/vda rw ", sizeof(real_cmdline));
}
if (kvm->cfg.kernel_cmdline) {
strcat(real_cmdline, " ");
strlcat(real_cmdline, kvm->cfg.kernel_cmdline, sizeof(real_cmdline));
}
kvm->cfg.real_cmdline = real_cmdline;
}
static void kvm_run_validate_cfg(struct kvm *kvm) {
u64 available_ram;
if (kvm->cfg.kernel_filename && kvm->cfg.firmware_filename) {
die("Only one of --kernel or --firmware can be specified");
}
if ((kvm->cfg.vnc && (kvm->cfg.sdl || kvm->cfg.gtk)) || (kvm->cfg.sdl && kvm->cfg.gtk)) {
die("Only one of --vnc, --sdl or --gtk can be specified");
}
if (kvm->cfg.firmware_filename && kvm->cfg.initrd_filename) {
pr_warning("Ignoring initrd file when loading a firmware image");
}
if (kvm->cfg.ram_size) {
available_ram = host_ram_size();
if (available_ram && kvm->cfg.ram_size > available_ram) {
pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB",
(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT,
(unsigned long long)available_ram >> MB_SHIFT);
}
}
kvm__arch_validate_cfg(kvm);
}
extern char* g_get_rootfs_path();
extern u64 g_get_ram_size();
extern int g_get_nrcpus();
static struct kvm *kvm_cmd_run_init(const char *kernel_filename) {
static char default_name[20];
unsigned int nr_online_cpus;
struct kvm *kvm = kvm__new();
if (IS_ERR(kvm)) {
return kvm;
}
nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
kvm->cfg.custom_rootfs_name = "default";
/*
* An architecture can allow the user to set the RAM base address to
* zero. Initialize the address before parsing the command line
* arguments, otherwise it will be impossible to distinguish between the
* user setting the base address to zero or letting it unset and using
* the default value.
*/
kvm->cfg.ram_addr = kvm__arch_default_ram_address();
kvm->cfg.kernel_filename = kernel_filename;
kvm->cfg.console = "virtio";
kvm_run_validate_cfg(kvm);
kvm->cfg.nrcpus = g_get_nrcpus();
if (kvm->cfg.nrcpus == 0) {
kvm->cfg.nrcpus = nr_online_cpus;
}
kvm->cfg.ram_size = g_get_ram_size();
if (!kvm->cfg.ram_size) {
kvm->cfg.ram_size = get_ram_size(kvm->cfg.nrcpus);
}
if (!kvm->cfg.dev) {
kvm->cfg.dev = DEFAULT_KVM_DEV;
}
if (!kvm->cfg.console) {
kvm->cfg.console = DEFAULT_CONSOLE;
}
if (!strncmp(kvm->cfg.console, "virtio", 6)) {
kvm->cfg.active_console = CONSOLE_VIRTIO;
} else if (!strncmp(kvm->cfg.console, "serial", 6)) {
kvm->cfg.active_console = CONSOLE_8250;
} else if (!strncmp(kvm->cfg.console, "hv", 2)) {
kvm->cfg.active_console = CONSOLE_HV;
} else {
pr_warning("No console!");
}
if (!kvm->cfg.host_ip) {
kvm->cfg.host_ip = DEFAULT_HOST_ADDR;
}
if (!kvm->cfg.guest_ip) {
kvm->cfg.guest_ip = DEFAULT_GUEST_ADDR;
}
if (!kvm->cfg.guest_mac) {
kvm->cfg.guest_mac = DEFAULT_GUEST_MAC;
}
if (!kvm->cfg.host_mac) {
kvm->cfg.host_mac = DEFAULT_HOST_MAC;
}
if (!kvm->cfg.script) {
kvm->cfg.script = DEFAULT_SCRIPT;
}
if (!kvm->cfg.network) {
kvm->cfg.network = DEFAULT_NETWORK;
}
if (!kvm->cfg.guest_name) {
if (kvm->cfg.custom_rootfs) {
kvm->cfg.guest_name = kvm->cfg.custom_rootfs_name;
} else {
sprintf(default_name, "guest-%u", getpid());
kvm->cfg.guest_name = default_name;
}
}
if (!kvm->cfg.nodefaults && !kvm->cfg.using_rootfs && !kvm->cfg.disk_image[0].filename &&
!kvm->cfg.initrd_filename) {
char tmp[PATH_MAX];
snprintf(tmp, PATH_MAX, g_get_rootfs_path());
if (virtio_9p__register(kvm, tmp, "/dev/root") < 0) {
die("Unable to initialize virtio 9p");
}
kvm->cfg.using_rootfs = kvm->cfg.custom_rootfs = 1;
}
if (kvm->cfg.nodefaults) {
kvm->cfg.real_cmdline = kvm->cfg.kernel_cmdline;
} else {
kvm_run_set_real_cmdline(kvm);
}
if (kvm->cfg.kernel_filename) {
pr_info("# %s run -k %s -m %Lu -c %d --name %s", KVM_BINARY_NAME, kvm->cfg.kernel_filename,
(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, kvm->cfg.nrcpus, kvm->cfg.guest_name);
} else if (kvm->cfg.firmware_filename) {
pr_info("# %s run --firmware %s -m %Lu -c %d --name %s", KVM_BINARY_NAME, kvm->cfg.firmware_filename,
(unsigned long long)kvm->cfg.ram_size >> MB_SHIFT, kvm->cfg.nrcpus, kvm->cfg.guest_name);
}
if (init_list__init(kvm) < 0) {
die("Initialisation failed");
}
return kvm;
}
static int kvm_cmd_run_work(struct kvm *kvm) {
void *vcpu0_ret;
int i;
for (i = 0; i < kvm->nrcpus; i++) {
if (pthread_create(&kvm->cpus[i]->thread, NULL, kvm_cpu_thread, kvm->cpus[i]) != 0) {
die("unable to create KVM VCPU thread");
}
}
/* Only VCPU #0 is going to exit by itself when shutting down */
if (pthread_join(kvm->cpus[0]->thread, &vcpu0_ret) != 0) {
die("unable to join with vcpu 0");
}
return kvm_cpu__exit(kvm, PTR_ERR(vcpu0_ret));
}
static void kvm_cmd_run_exit(struct kvm *kvm, int guest_ret) {
compat__print_all_messages();
init_list__exit(kvm);
if (guest_ret == 0) {
pr_info("KVM session ended normally.");
}
}
int kvm_cmd_run(const char *kernel_filename) {
int ret = -EFAULT;
struct kvm *kvm;
kvm = kvm_cmd_run_init(kernel_filename);
if (IS_ERR(kvm)) {
return PTR_ERR(kvm);
}
ret = kvm_cmd_run_work(kvm);
kvm_cmd_run_exit(kvm, ret);
return ret;
}