-
Notifications
You must be signed in to change notification settings - Fork 273
/
qemu.c
334 lines (282 loc) · 7.97 KB
/
qemu.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
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
/*
* Derive kernel base from a QEMU saved VM file
*
* Copyright (C) 2009, 2010 Red Hat, Inc.
* Written by Paolo Bonzini.
*
* Portions Copyright (C) 2009 David Anderson
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include "qemu-load.h"
#include "kvmdump.h"
/*
* Some bits we need to access in the control registers and page tables.
*/
#define MSR_EFER_LMA (1 << 10)
#define PG_PRESENT_MASK (1 << 0)
#define PG_PSE_MASK (1 << 7)
#define CR0_PG_MASK (1 << 31)
#define CR4_PAE_MASK (1 << 31)
#define CR4_PSE_MASK (1 << 31)
static uint32_t
ldl (struct qemu_device_x86 *dx86, struct qemu_device_ram *dram, uint64_t addr)
{
char buf[4096];
if (dx86->a20_masked)
addr &= ~(1LL<<20);
if (!ram_read_phys_page (dram, buf, addr & ~0xfff))
return 0;
assert ((addr & 0xfff) <= 0xffc);
return *(uint32_t *)(buf + (addr & 0xfff));
}
static uint64_t
ldq (struct qemu_device_x86 *dx86, struct qemu_device_ram *dram, uint64_t addr)
{
char buf[4096];
if (dx86->a20_masked)
addr &= ~(1LL<<20);
if (!ram_read_phys_page (dram, buf, addr & ~0xfff))
return 0;
assert ((addr & 0xfff) <= 0xff8);
return *(uint64_t *)(buf + (addr & 0xfff));
}
/*
* Messy x86 TLB fault logic, walking the page tables to find the physical
* address corresponding to ADDR. Taken from QEMU.
*/
static uint64_t
get_phys_page(struct qemu_device_x86 *dx86, struct qemu_device_ram *dram,
uint64_t addr)
{
uint64_t pde_addr, pte_addr;
uint64_t pte, paddr;
uint32_t page_offset;
int page_size;
if ((dx86->cr4 & CR4_PAE_MASK) || (dx86->efer & MSR_EFER_LMA)) {
uint64_t pdpe_addr;
uint64_t pde, pdpe;
if (dx86->cr4 & CR4_PAE_MASK)
dprintf ("PAE active\n");
if (dx86->efer & MSR_EFER_LMA) {
uint64_t pml4e_addr, pml4e;
int32_t sext;
dprintf ("long mode active\n");
/* test virtual address sign extension */
sext = (int64_t) addr >> 47;
if (sext != 0 && sext != -1)
return -1;
pml4e_addr = ((dx86->cr3 & ~0xfff)
+ (((addr >> 39) & 0x1ff) << 3));
pml4e = ldq (dx86, dram, pml4e_addr);
if (!(pml4e & PG_PRESENT_MASK))
return -1;
dprintf ("PML4 page present\n");
pdpe_addr = ((pml4e & ~0xfff)
+ (((addr >> 30) & 0x1ff) << 3));
pdpe = ldq (dx86, dram, pdpe_addr);
if (!(pdpe & PG_PRESENT_MASK))
return -1;
dprintf ("PDPE page present\n");
} else {
dprintf ("long mode inactive\n");
pdpe_addr = ((dx86->cr3 & ~0x1f)
+ ((addr >> 27) & 0x18));
pdpe = ldq (dx86, dram, pdpe_addr);
if (!(pdpe & PG_PRESENT_MASK))
return -1;
dprintf ("PDPE page present\n");
}
pde_addr = (pdpe & ~0xfff) + (((addr >> 21) & 0x1ff) << 3);
pde = ldq (dx86, dram, pde_addr);
if (!(pde & PG_PRESENT_MASK))
return -1;
dprintf ("PDE page present\n");
if (pde & PG_PSE_MASK) {
/* 2 MB page */
dprintf ("2MB page\n");
page_size = 2048 * 1024;
pte = pde & ~((page_size - 1) & ~0xfff);
} else {
/* 4 KB page */
dprintf ("4 KB PAE page\n");
pte_addr = ((pde & ~0xfff)
+ (((addr >> 12) & 0x1ff) << 3));
page_size = 4096;
pte = ldq (dx86, dram, pte_addr);
if (!(pte & PG_PRESENT_MASK))
return -1;
dprintf ("PTE page present\n");
}
} else {
/* Not PAE. */
uint32_t pde;
if (!(dx86->cr0 & CR0_PG_MASK)) {
dprintf ("Paging inactive\n");
pte = addr;
page_size = 4096;
} else {
/* page directory entry */
pde_addr = ((dx86->cr3 & ~0xfff)
+ ((addr >> 20) & 0xffc));
pde = ldl (dx86, dram, pde_addr);
if (!(pde & PG_PRESENT_MASK))
return -1;
dprintf ("PDE page present\n");
if ((pde & PG_PSE_MASK) && (dx86->cr4 & CR4_PSE_MASK)) {
page_size = 4096 * 1024;
pte = pde & ~((page_size - 1) & ~0xfff);
} else {
page_size = 4096;
pte_addr = ((pde & ~0xfff)
+ ((addr >> 10) & 0xffc));
pte = ldl (dx86, dram, pte_addr);
if (!(pte & PG_PRESENT_MASK))
return -1;
dprintf ("PTE page present\n");
}
}
}
page_offset = (addr & 0xfff) & (page_size - 1);
paddr = (pte & ~0xfff) + page_offset;
return paddr;
}
/*
* I'm using the IDT base as a quick way to find the bottom of the
* kernel memory.
*/
static uint64_t
get_idt_base(struct qemu_device_list *dl)
{
struct qemu_device_x86 *dx86 = (struct qemu_device_x86 *)
device_find_instance (dl, "cpu", 0);
return dx86->idt.base;
}
static uint64_t
get_kernel_base(struct qemu_device_list *dl)
{
int i;
uint64_t kernel_base = -1;
uint64_t base_vaddr, last, mask;
struct qemu_device_x86 *dx86 = (struct qemu_device_x86 *)
device_find_instance (dl, "cpu", 0);
struct qemu_device_ram *dram = (struct qemu_device_ram *)
device_find_instance (dl, "ram", 0);
for (i = 30, last = -1; (kernel_base == -1) && (i >= 20); i--)
{
mask = ~((1LL << i) - 1);
base_vaddr = dx86->idt.base & mask;
if (base_vaddr == last)
continue;
if (base_vaddr < kvm->kvbase) {
fprintf(stderr,
"WARNING: IDT base contains: %llx\n "
"cannot determine physical base address: defaulting to 0\n\n",
(unsigned long long)base_vaddr);
return 0;
}
dprintf("get_kernel_base: %llx\n", (unsigned long long)base_vaddr);
kernel_base = get_phys_page(dx86, dram, base_vaddr);
last = base_vaddr;
}
if (kernel_base != -1) {
dprintf("kvbase: %llx vaddr used: %llx physical: %llx\n",
(unsigned long long)kvm->kvbase,
(unsigned long long)base_vaddr,
(unsigned long long)kernel_base);
/*
* Subtract the offset between the virtual address used
* and the kernel's base virtual address.
*/
kernel_base -= (base_vaddr - kvm->kvbase);
} else {
dprintf("WARNING: cannot determine physical base address:"
" defaulting to 0\n\n");
kernel_base = 0;
kvm->flags |= NO_PHYS_BASE;
}
return kernel_base;
}
#ifdef MAIN_FROM_TEST_C
int main (int argc, char **argv)
{
struct qemu_device_list *dl;
FILE *fp;
if (argc != 2) {
fprintf (stderr, "Usage: test SAVE-FILE\n");
exit (1);
}
fp = fopen(argv[1], "r");
if (!fp) {
fprintf (stderr, "Error: %s\n", strerror (errno));
exit (1);
}
#ifdef HOST_32BIT
dl = qemu_load (devices_x86_32, QEMU_FEATURE_CPU|QEMU_FEATURE_RAM, fp);
#else
dl = qemu_load (devices_x86_64, QEMU_FEATURE_CPU|QEMU_FEATURE_RAM, fp);
#endif
printf ("IDT at %llx\n", get_idt_base (dl));
printf ("Physical kernel base at %llx\n", get_kernel_base (dl));
device_list_free (dl);
fclose (fp);
exit (0);
}
#endif
/*
* crash utility adaptation
*/
#include "defs.h"
int
qemu_init(char *filename)
{
struct qemu_device_list *dl;
struct qemu_device_ram *dram;
uint64_t idt = 0;
if (CRASHDEBUG(1))
dump_qemu_header(kvm->ofp);
rewind(kvm->vmp);
if (kvm->flags & (MAPFILE|MAPFILE_APPENDED))
return TRUE;
please_wait("scanning KVM dumpfile");
if (kvm->flags & KVMHOST_32)
dl = qemu_load(devices_x86_32,
QEMU_FEATURE_CPU|QEMU_FEATURE_RAM, kvm->vmp);
else
dl = qemu_load(devices_x86_64,
QEMU_FEATURE_CPU|QEMU_FEATURE_RAM, kvm->vmp);
please_wait_done();
if (dl) {
if (machine_type("X86_64")) {
idt = get_idt_base(dl);
kvm->mapinfo.phys_base = get_kernel_base(dl);
}
dram = (struct qemu_device_ram *)
device_find_instance (dl, "ram", 0);
if (CRASHDEBUG(1)) {
if (machine_type("X86_64")) {
fprintf(kvm->ofp, "IDT: %llx\n",
(ulonglong)idt);
fprintf(kvm->ofp, "physical kernel base: %llx\n",
(ulonglong)kvm->mapinfo.phys_base);
}
fprintf(kvm->ofp, "last RAM offset: %llx\n",
(ulonglong)dram->last_ram_offset);
}
device_list_free (dl);
} else
fclose(kvm->vmp);
return dl ? TRUE : FALSE;
}