-
Notifications
You must be signed in to change notification settings - Fork 18
/
kexec.c
269 lines (223 loc) · 7.67 KB
/
kexec.c
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
/*-
* Copyright (c) 2015 xhyve developers
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY ???, INC ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <xhyve/vmm/vmm_api.h>
#include <xhyve/firmware/kexec.h>
#ifndef ALIGNUP
#define ALIGNUP(x, a) (((x - 1) & ~(a - 1)) + a)
#endif
#define BASE_GDT 0x2000ull
#define BASE_ZEROPAGE 0x3000ull
#define BASE_CMDLINE 0x4000ull
#define BASE_KERNEL 0x100000ull
#define HDRS 0x53726448 /* SrdH */
static struct {
uintptr_t base;
size_t size;
} memory, kernel, ramdisk;
static struct {
char *kernel;
char *initrd;
char *cmdline;
} config;
static int
kexec_load_kernel(char *path, char *cmdline) {
uint64_t kernel_offset, kernel_size, kernel_init_size, kernel_start, mem_k;
size_t sz, cmdline_len;
volatile struct zero_page *zp;
FILE *f;
if ((memory.size < (BASE_ZEROPAGE + sizeof(struct zero_page))) ||
((BASE_ZEROPAGE + sizeof(struct zero_page)) > BASE_CMDLINE))
{
return -1;
}
zp = ((struct zero_page *) (memory.base + ((off_t) BASE_ZEROPAGE)));
memset(((void *) ((uintptr_t) zp)), 0, sizeof(struct zero_page));
if (!(f = fopen(path, "r"))) {
return -1;
}
fseek(f, 0L, SEEK_END);
sz = (size_t) ftell(f);
if (sz < (0x01f1 + sizeof(struct setup_header))) {
fclose(f);
return -1;
}
fseek(f, 0x01f1, SEEK_SET);
if (!fread(((void *) ((uintptr_t) &zp->setup_header)), 1,
sizeof(zp->setup_header), f))
{
fclose(f);
return -1;
}
if ((zp->setup_header.setup_sects == 0) || /* way way too old */
(zp->setup_header.boot_flag != 0xaa55) || /* no boot magic */
(zp->setup_header.header != HDRS) || /* way too old */
(zp->setup_header.version < 0x020a) || /* too old */
(!(zp->setup_header.loadflags & 1)) || /* no bzImage */
(sz < (((zp->setup_header.setup_sects + 1) * 512) +
(zp->setup_header.syssize * 16)))) /* too small */
{
/* we can't boot this kernel */
fclose(f);
return -1;
}
kernel_offset = ((zp->setup_header.setup_sects + 1) * 512);
kernel_size = (sz - kernel_offset);
kernel_init_size = ALIGNUP(zp->setup_header.init_size, 0x1000ull);
kernel_start = (zp->setup_header.relocatable_kernel) ?
ALIGNUP(BASE_KERNEL, zp->setup_header.kernel_alignment) :
zp->setup_header.pref_address;
if ((kernel_start < BASE_KERNEL) ||
(kernel_size > kernel_init_size) || /* XXX: always true? */
((kernel_start + kernel_init_size) > memory.size)) /* oom */
{
fclose(f);
return -1;
}
/* copy kernel */
fseek(f, ((long) kernel_offset), SEEK_SET);
if (!fread(((void *) (memory.base + kernel_start)), 1, kernel_size, f)) {
fclose(f);
return -1;
}
fclose(f);
/* copy cmdline */
cmdline_len = strlen(cmdline);
if (((cmdline_len + 1)> zp->setup_header.cmdline_size) ||
((BASE_CMDLINE + (cmdline_len + 1)) > kernel_start))
{
return -1;
}
memcpy(((void *) (memory.base + BASE_CMDLINE)), cmdline, cmdline_len);
memset(((void *) (memory.base + BASE_CMDLINE + cmdline_len)), '\0', 1);
zp->setup_header.cmd_line_ptr = ((uint32_t) BASE_CMDLINE);
zp->ext_cmd_line_ptr = ((uint32_t) (BASE_CMDLINE >> 32));
zp->setup_header.hardware_subarch = 0; /* PC */
zp->setup_header.type_of_loader = 0xd; /* kexec */
mem_k = (memory.size - 0x100000) >> 10; /* assume memory base is at 0 */
zp->alt_mem_k = (mem_k > 0xffffffff) ? 0xffffffff : ((uint32_t) mem_k);
zp->e820_map[0].addr = 0x0000000000000000;
zp->e820_map[0].size = 0x000000000009fc00;
zp->e820_map[0].type = 1;
zp->e820_map[1].addr = 0x0000000000100000;
zp->e820_map[1].size = (memory.size - 0x0000000000100000);
zp->e820_map[1].type = 1;
if (xh_vm_get_highmem_size() == 0) {
zp->e820_entries = 2;
} else {
zp->e820_map[2].addr = 0x0000000100000000;
zp->e820_map[2].size = xh_vm_get_highmem_size();
zp->e820_map[2].type = 1;
zp->e820_entries = 3;
}
kernel.base = kernel_start;
kernel.size = kernel_init_size;
return 0;
}
static int
kexec_load_ramdisk(char *path) {
uint64_t ramdisk_start;
volatile struct zero_page *zp;
size_t sz;
FILE *f;
zp = ((struct zero_page *) (memory.base + BASE_ZEROPAGE));
if (!(f = fopen(path, "r"))) {;
return -1;
}
fseek(f, 0L, SEEK_END);
sz = (size_t) ftell(f);
fseek(f, 0, SEEK_SET);
ramdisk_start = ALIGNUP((kernel.base + kernel.size), 0x1000ull);
if ((ramdisk_start + sz) > memory.size) {
/* not enough memory */
fclose(f);
return -1;
}
/* copy ramdisk */
if (!fread(((void *) (memory.base + ramdisk_start)), 1, sz, f)) {
fclose(f);
return -1;
}
fclose(f);
zp->setup_header.ramdisk_image = ((uint32_t) ramdisk_start);
zp->ext_ramdisk_image = ((uint32_t) (ramdisk_start >> 32));
zp->setup_header.ramdisk_size = ((uint32_t) sz);
zp->ext_ramdisk_size = ((uint32_t) (sz >> 32));
ramdisk.base = ramdisk_start;
ramdisk.size = sz;
return 0;
}
void
kexec_init(char *kernel_path, char *initrd_path, char *cmdline) {
config.kernel = kernel_path;
config.initrd = initrd_path;
config.cmdline = cmdline;
}
uint64_t
kexec(void)
{
uint64_t *gdt_entry;
void *gpa_map;
gpa_map = xh_vm_map_gpa(0, xh_vm_get_lowmem_size());
memory.base = (uintptr_t) gpa_map;
memory.size = xh_vm_get_lowmem_size();
if (kexec_load_kernel(config.kernel,
config.cmdline ? config.cmdline : "auto"))
{
fprintf(stderr, "kexec: failed to load kernel %s\n", config.kernel);
abort();
}
if (config.initrd && kexec_load_ramdisk(config.initrd)) {
fprintf(stderr, "kexec: failed to load initrd %s\n", config.initrd);
abort();
}
gdt_entry = ((uint64_t *) (memory.base + BASE_GDT));
gdt_entry[0] = 0x0000000000000000; /* null */
gdt_entry[1] = 0x0000000000000000; /* null */
gdt_entry[2] = 0x00cf9a000000ffff; /* code */
gdt_entry[3] = 0x00cf92000000ffff; /* data */
xh_vcpu_reset(0);
xh_vm_set_desc(0, VM_REG_GUEST_GDTR, BASE_GDT, 0x1f, 0);
xh_vm_set_desc(0, VM_REG_GUEST_CS, 0, 0xffffffff, 0xc09b);
xh_vm_set_desc(0, VM_REG_GUEST_DS, 0, 0xffffffff, 0xc093);
xh_vm_set_desc(0, VM_REG_GUEST_ES, 0, 0xffffffff, 0xc093);
xh_vm_set_desc(0, VM_REG_GUEST_SS, 0, 0xffffffff, 0xc093);
xh_vm_set_register(0, VM_REG_GUEST_CS, 0x10);
xh_vm_set_register(0, VM_REG_GUEST_DS, 0x18);
xh_vm_set_register(0, VM_REG_GUEST_ES, 0x18);
xh_vm_set_register(0, VM_REG_GUEST_SS, 0x18);
xh_vm_set_register(0, VM_REG_GUEST_CR0, 0x21); /* enable protected mode */
xh_vm_set_register(0, VM_REG_GUEST_RBP, 0);
xh_vm_set_register(0, VM_REG_GUEST_RDI, 0);
xh_vm_set_register(0, VM_REG_GUEST_RBX, 0);
xh_vm_set_register(0, VM_REG_GUEST_RFLAGS, 0x2);
xh_vm_set_register(0, VM_REG_GUEST_RSI, BASE_ZEROPAGE);
xh_vm_set_register(0, VM_REG_GUEST_RIP, kernel.base);
return kernel.base;
}