]> git.proxmox.com Git - mirror_qemu.git/blame - dump/win_dump.c
dump: Replace TARGET_PAGE_SIZE -> qemu_target_page_size()
[mirror_qemu.git] / dump / win_dump.c
CommitLineData
2da91b54
VP
1/*
2 * Windows crashdump
3 *
4 * Copyright (c) 2018 Virtuozzo International GmbH
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 *
9 */
10
11#include "qemu/osdep.h"
12#include "qemu/cutils.h"
13#include "elf.h"
2da91b54
VP
14#include "exec/hwaddr.h"
15#include "monitor/monitor.h"
16#include "sysemu/kvm.h"
17#include "sysemu/dump.h"
2da91b54
VP
18#include "sysemu/memory_mapping.h"
19#include "sysemu/cpus.h"
20#include "qapi/error.h"
21#include "qapi/qmp/qerror.h"
22#include "qemu/error-report.h"
23#include "hw/misc/vmcoreinfo.h"
24#include "win_dump.h"
25
f5daa829
VP
26static size_t win_dump_ptr_size(bool x64)
27{
28 return x64 ? sizeof(uint64_t) : sizeof(uint32_t);
29}
fb21efe9 30
f5daa829 31#define _WIN_DUMP_FIELD(f) (x64 ? h->x64.f : h->x32.f)
fb21efe9
VP
32#define WIN_DUMP_FIELD(field) _WIN_DUMP_FIELD(field)
33
f5daa829 34#define _WIN_DUMP_FIELD_PTR(f) (x64 ? (void *)&h->x64.f : (void *)&h->x32.f)
fb21efe9
VP
35#define WIN_DUMP_FIELD_PTR(field) _WIN_DUMP_FIELD_PTR(field)
36
f5daa829 37#define _WIN_DUMP_FIELD_SIZE(f) (x64 ? sizeof(h->x64.f) : sizeof(h->x32.f))
fb21efe9
VP
38#define WIN_DUMP_FIELD_SIZE(field) _WIN_DUMP_FIELD_SIZE(field)
39
f5daa829
VP
40static size_t win_dump_ctx_size(bool x64)
41{
42 return x64 ? sizeof(WinContext64) : sizeof(WinContext32);
43}
fb21efe9
VP
44
45static size_t write_run(uint64_t base_page, uint64_t page_count,
46 int fd, Error **errp)
2da91b54
VP
47{
48 void *buf;
fb21efe9
VP
49 uint64_t addr = base_page << TARGET_PAGE_BITS;
50 uint64_t size = page_count << TARGET_PAGE_BITS;
7184de64
VP
51 uint64_t len, l;
52 size_t total = 0;
2da91b54 53
7184de64
VP
54 while (size) {
55 len = size;
2da91b54 56
7184de64
VP
57 buf = cpu_physical_memory_map(addr, &len, false);
58 if (!buf) {
59 error_setg(errp, "win-dump: failed to map physical range"
60 " 0x%016" PRIx64 "-0x%016" PRIx64, addr, addr + size - 1);
61 return 0;
62 }
2da91b54 63
7184de64
VP
64 l = qemu_write_full(fd, buf, len);
65 cpu_physical_memory_unmap(buf, addr, false, len);
66 if (l != len) {
67 error_setg(errp, QERR_IO_ERROR);
68 return 0;
69 }
70
71 addr += l;
72 size -= l;
73 total += l;
74 }
2da91b54 75
7184de64 76 return total;
2da91b54
VP
77}
78
f5daa829 79static void write_runs(DumpState *s, WinDumpHeader *h, bool x64, Error **errp)
2da91b54 80{
fb21efe9 81 uint64_t BasePage, PageCount;
2da91b54
VP
82 Error *local_err = NULL;
83 int i;
84
fb21efe9
VP
85 for (i = 0; i < WIN_DUMP_FIELD(PhysicalMemoryBlock.NumberOfRuns); i++) {
86 BasePage = WIN_DUMP_FIELD(PhysicalMemoryBlock.Run[i].BasePage);
87 PageCount = WIN_DUMP_FIELD(PhysicalMemoryBlock.Run[i].PageCount);
88 s->written_size += write_run(BasePage, PageCount, s->fd, &local_err);
2da91b54
VP
89 if (local_err) {
90 error_propagate(errp, local_err);
91 return;
92 }
93 }
94}
95
f5daa829 96static int cpu_read_ptr(bool x64, CPUState *cpu, uint64_t addr, uint64_t *ptr)
fb21efe9
VP
97{
98 int ret;
f5daa829 99 uint32_t ptr32;
fb21efe9
VP
100 uint64_t ptr64;
101
f5daa829
VP
102 ret = cpu_memory_rw_debug(cpu, addr, x64 ? (void *)&ptr64 : (void *)&ptr32,
103 win_dump_ptr_size(x64), 0);
fb21efe9 104
f5daa829 105 *ptr = x64 ? ptr64 : ptr32;
fb21efe9
VP
106
107 return ret;
108}
109
f5daa829 110static void patch_mm_pfn_database(WinDumpHeader *h, bool x64, Error **errp)
2da91b54
VP
111{
112 if (cpu_memory_rw_debug(first_cpu,
f5daa829 113 WIN_DUMP_FIELD(KdDebuggerDataBlock) + KDBG_MM_PFN_DATABASE_OFFSET,
fb21efe9
VP
114 WIN_DUMP_FIELD_PTR(PfnDatabase),
115 WIN_DUMP_FIELD_SIZE(PfnDatabase), 0)) {
2da91b54
VP
116 error_setg(errp, "win-dump: failed to read MmPfnDatabase");
117 return;
118 }
119}
120
f5daa829 121static void patch_bugcheck_data(WinDumpHeader *h, bool x64, Error **errp)
2da91b54
VP
122{
123 uint64_t KiBugcheckData;
124
f5daa829
VP
125 if (cpu_read_ptr(x64, first_cpu,
126 WIN_DUMP_FIELD(KdDebuggerDataBlock) + KDBG_KI_BUGCHECK_DATA_OFFSET,
fb21efe9 127 &KiBugcheckData)) {
2da91b54
VP
128 error_setg(errp, "win-dump: failed to read KiBugcheckData");
129 return;
130 }
131
fb21efe9
VP
132 if (cpu_memory_rw_debug(first_cpu, KiBugcheckData,
133 WIN_DUMP_FIELD(BugcheckData),
134 WIN_DUMP_FIELD_SIZE(BugcheckData), 0)) {
2da91b54
VP
135 error_setg(errp, "win-dump: failed to read bugcheck data");
136 return;
137 }
2ad9b50f
VP
138
139 /*
140 * If BugcheckCode wasn't saved, we consider guest OS as alive.
141 */
142
fb21efe9
VP
143 if (!WIN_DUMP_FIELD(BugcheckCode)) {
144 *(uint32_t *)WIN_DUMP_FIELD_PTR(BugcheckCode) = LIVE_SYSTEM_DUMP;
2ad9b50f 145 }
2da91b54
VP
146}
147
148/*
149 * This routine tries to correct mistakes in crashdump header.
150 */
f5daa829 151static void patch_header(WinDumpHeader *h, bool x64)
2da91b54
VP
152{
153 Error *local_err = NULL;
154
f5daa829
VP
155 if (x64) {
156 h->x64.RequiredDumpSpace = sizeof(WinDumpHeader64) +
157 (h->x64.PhysicalMemoryBlock.NumberOfPages << TARGET_PAGE_BITS);
158 h->x64.PhysicalMemoryBlock.unused = 0;
159 h->x64.unused1 = 0;
160 } else {
161 h->x32.RequiredDumpSpace = sizeof(WinDumpHeader32) +
162 (h->x32.PhysicalMemoryBlock.NumberOfPages << TARGET_PAGE_BITS);
163 }
2da91b54 164
f5daa829 165 patch_mm_pfn_database(h, x64, &local_err);
2da91b54
VP
166 if (local_err) {
167 warn_report_err(local_err);
168 local_err = NULL;
169 }
f5daa829 170 patch_bugcheck_data(h, x64, &local_err);
2da91b54
VP
171 if (local_err) {
172 warn_report_err(local_err);
173 }
174}
175
f5daa829 176static bool check_header(WinDumpHeader *h, bool *x64, Error **errp)
2da91b54
VP
177{
178 const char Signature[] = "PAGE";
2da91b54
VP
179
180 if (memcmp(h->Signature, Signature, sizeof(h->Signature))) {
181 error_setg(errp, "win-dump: invalid header, expected '%.4s',"
182 " got '%.4s'", Signature, h->Signature);
f5daa829 183 return false;
2da91b54
VP
184 }
185
f5daa829
VP
186 if (!memcmp(h->ValidDump, "DUMP", sizeof(h->ValidDump))) {
187 *x64 = false;
188 } else if (!memcmp(h->ValidDump, "DU64", sizeof(h->ValidDump))) {
189 *x64 = true;
190 } else {
191 error_setg(errp, "win-dump: invalid header, expected 'DUMP' or 'DU64',"
192 " got '%.4s'", h->ValidDump);
193 return false;
2da91b54 194 }
f5daa829
VP
195
196 return true;
2da91b54
VP
197}
198
f5daa829 199static void check_kdbg(WinDumpHeader *h, bool x64, Error **errp)
2da91b54
VP
200{
201 const char OwnerTag[] = "KDBG";
202 char read_OwnerTag[4];
fb21efe9 203 uint64_t KdDebuggerDataBlock = WIN_DUMP_FIELD(KdDebuggerDataBlock);
2ababfcc 204 bool try_fallback = true;
2da91b54 205
2ababfcc 206try_again:
2da91b54 207 if (cpu_memory_rw_debug(first_cpu,
f5daa829 208 KdDebuggerDataBlock + KDBG_OWNER_TAG_OFFSET,
2da91b54
VP
209 (uint8_t *)&read_OwnerTag, sizeof(read_OwnerTag), 0)) {
210 error_setg(errp, "win-dump: failed to read OwnerTag");
211 return;
212 }
213
214 if (memcmp(read_OwnerTag, OwnerTag, sizeof(read_OwnerTag))) {
2ababfcc
VP
215 if (try_fallback) {
216 /*
217 * If attempt to use original KDBG failed
218 * (most likely because of its encryption),
219 * we try to use KDBG obtained by guest driver.
220 */
221
fb21efe9 222 KdDebuggerDataBlock = WIN_DUMP_FIELD(BugcheckParameter1);
2ababfcc
VP
223 try_fallback = false;
224 goto try_again;
225 } else {
226 error_setg(errp, "win-dump: invalid KDBG OwnerTag,"
227 " expected '%.4s', got '%.4s'",
228 OwnerTag, read_OwnerTag);
229 return;
230 }
2da91b54 231 }
2ababfcc 232
f5daa829
VP
233 if (x64) {
234 h->x64.KdDebuggerDataBlock = KdDebuggerDataBlock;
235 } else {
236 h->x32.KdDebuggerDataBlock = KdDebuggerDataBlock;
237 }
2da91b54
VP
238}
239
2ad9b50f 240struct saved_context {
f5daa829 241 WinContext ctx;
2ad9b50f
VP
242 uint64_t addr;
243};
244
f5daa829 245static void patch_and_save_context(WinDumpHeader *h, bool x64,
2ad9b50f
VP
246 struct saved_context *saved_ctx,
247 Error **errp)
248{
fb21efe9 249 uint64_t KdDebuggerDataBlock = WIN_DUMP_FIELD(KdDebuggerDataBlock);
2ad9b50f
VP
250 uint64_t KiProcessorBlock;
251 uint16_t OffsetPrcbContext;
252 CPUState *cpu;
253 int i = 0;
254
f5daa829
VP
255 if (cpu_read_ptr(x64, first_cpu,
256 KdDebuggerDataBlock + KDBG_KI_PROCESSOR_BLOCK_OFFSET,
fb21efe9 257 &KiProcessorBlock)) {
2ad9b50f
VP
258 error_setg(errp, "win-dump: failed to read KiProcessorBlock");
259 return;
260 }
261
262 if (cpu_memory_rw_debug(first_cpu,
f5daa829 263 KdDebuggerDataBlock + KDBG_OFFSET_PRCB_CONTEXT_OFFSET,
2ad9b50f
VP
264 (uint8_t *)&OffsetPrcbContext, sizeof(OffsetPrcbContext), 0)) {
265 error_setg(errp, "win-dump: failed to read OffsetPrcbContext");
266 return;
267 }
268
269 CPU_FOREACH(cpu) {
270 X86CPU *x86_cpu = X86_CPU(cpu);
271 CPUX86State *env = &x86_cpu->env;
272 uint64_t Prcb;
273 uint64_t Context;
f5daa829 274 WinContext ctx;
2ad9b50f 275
e38c24cb
VP
276 if (i >= WIN_DUMP_FIELD(NumberProcessors)) {
277 warn_report("win-dump: number of QEMU CPUs is bigger than"
278 " NumberProcessors (%u) in guest Windows",
279 WIN_DUMP_FIELD(NumberProcessors));
280 return;
281 }
282
f5daa829
VP
283 if (cpu_read_ptr(x64, first_cpu,
284 KiProcessorBlock + i * win_dump_ptr_size(x64),
fb21efe9 285 &Prcb)) {
2ad9b50f
VP
286 error_setg(errp, "win-dump: failed to read"
287 " CPU #%d PRCB location", i);
288 return;
289 }
290
f5daa829 291 if (cpu_read_ptr(x64, first_cpu,
2ad9b50f 292 Prcb + OffsetPrcbContext,
fb21efe9 293 &Context)) {
2ad9b50f
VP
294 error_setg(errp, "win-dump: failed to read"
295 " CPU #%d ContextFrame location", i);
296 return;
297 }
298
299 saved_ctx[i].addr = Context;
300
f5daa829
VP
301 if (x64) {
302 ctx.x64 = (WinContext64){
303 .ContextFlags = WIN_CTX64_ALL,
2ad9b50f 304 .MxCsr = env->mxcsr,
f5daa829
VP
305
306 .SegEs = env->segs[0].selector,
307 .SegCs = env->segs[1].selector,
308 .SegSs = env->segs[2].selector,
309 .SegDs = env->segs[3].selector,
310 .SegFs = env->segs[4].selector,
311 .SegGs = env->segs[5].selector,
312 .EFlags = cpu_compute_eflags(env),
313
314 .Dr0 = env->dr[0],
315 .Dr1 = env->dr[1],
316 .Dr2 = env->dr[2],
317 .Dr3 = env->dr[3],
318 .Dr6 = env->dr[6],
319 .Dr7 = env->dr[7],
320
321 .Rax = env->regs[R_EAX],
322 .Rbx = env->regs[R_EBX],
323 .Rcx = env->regs[R_ECX],
324 .Rdx = env->regs[R_EDX],
325 .Rsp = env->regs[R_ESP],
326 .Rbp = env->regs[R_EBP],
327 .Rsi = env->regs[R_ESI],
328 .Rdi = env->regs[R_EDI],
329 .R8 = env->regs[8],
330 .R9 = env->regs[9],
331 .R10 = env->regs[10],
332 .R11 = env->regs[11],
333 .R12 = env->regs[12],
334 .R13 = env->regs[13],
335 .R14 = env->regs[14],
336 .R15 = env->regs[15],
337
338 .Rip = env->eip,
339 .FltSave = {
340 .MxCsr = env->mxcsr,
341 },
342 };
343 } else {
344 ctx.x32 = (WinContext32){
345 .ContextFlags = WIN_CTX32_FULL | WIN_CTX_DBG,
346
347 .SegEs = env->segs[0].selector,
348 .SegCs = env->segs[1].selector,
349 .SegSs = env->segs[2].selector,
350 .SegDs = env->segs[3].selector,
351 .SegFs = env->segs[4].selector,
352 .SegGs = env->segs[5].selector,
353 .EFlags = cpu_compute_eflags(env),
354
355 .Dr0 = env->dr[0],
356 .Dr1 = env->dr[1],
357 .Dr2 = env->dr[2],
358 .Dr3 = env->dr[3],
359 .Dr6 = env->dr[6],
360 .Dr7 = env->dr[7],
361
362 .Eax = env->regs[R_EAX],
363 .Ebx = env->regs[R_EBX],
364 .Ecx = env->regs[R_ECX],
365 .Edx = env->regs[R_EDX],
366 .Esp = env->regs[R_ESP],
367 .Ebp = env->regs[R_EBP],
368 .Esi = env->regs[R_ESI],
369 .Edi = env->regs[R_EDI],
370
371 .Eip = env->eip,
372 };
373 }
2ad9b50f
VP
374
375 if (cpu_memory_rw_debug(first_cpu, Context,
f5daa829 376 &saved_ctx[i].ctx, win_dump_ctx_size(x64), 0)) {
2ad9b50f
VP
377 error_setg(errp, "win-dump: failed to save CPU #%d context", i);
378 return;
379 }
380
381 if (cpu_memory_rw_debug(first_cpu, Context,
f5daa829 382 &ctx, win_dump_ctx_size(x64), 1)) {
2ad9b50f
VP
383 error_setg(errp, "win-dump: failed to write CPU #%d context", i);
384 return;
385 }
386
387 i++;
388 }
389}
390
f5daa829 391static void restore_context(WinDumpHeader *h, bool x64,
2ad9b50f
VP
392 struct saved_context *saved_ctx)
393{
394 int i;
2ad9b50f 395
fb21efe9 396 for (i = 0; i < WIN_DUMP_FIELD(NumberProcessors); i++) {
2ad9b50f 397 if (cpu_memory_rw_debug(first_cpu, saved_ctx[i].addr,
f5daa829 398 &saved_ctx[i].ctx, win_dump_ctx_size(x64), 1)) {
b0e70950 399 warn_report("win-dump: failed to restore CPU #%d context", i);
2ad9b50f
VP
400 }
401 }
402}
403
2da91b54
VP
404void create_win_dump(DumpState *s, Error **errp)
405{
f5daa829 406 WinDumpHeader *h = (void *)(s->guest_note + VMCOREINFO_ELF_NOTE_HDR_SIZE);
92d1b3d5
VP
407 X86CPU *first_x86_cpu = X86_CPU(first_cpu);
408 uint64_t saved_cr3 = first_x86_cpu->env.cr[3];
2ad9b50f 409 struct saved_context *saved_ctx = NULL;
2da91b54 410 Error *local_err = NULL;
f5daa829
VP
411 bool x64 = true;
412 size_t hdr_size;
2da91b54 413
f5daa829
VP
414 if (s->guest_note_size != VMCOREINFO_WIN_DUMP_NOTE_SIZE32 &&
415 s->guest_note_size != VMCOREINFO_WIN_DUMP_NOTE_SIZE64) {
2da91b54
VP
416 error_setg(errp, "win-dump: invalid vmcoreinfo note size");
417 return;
418 }
419
f5daa829 420 if (!check_header(h, &x64, &local_err)) {
2da91b54
VP
421 error_propagate(errp, local_err);
422 return;
423 }
424
f5daa829
VP
425 hdr_size = x64 ? sizeof(WinDumpHeader64) : sizeof(WinDumpHeader32);
426
92d1b3d5
VP
427 /*
428 * Further access to kernel structures by virtual addresses
429 * should be made from system context.
430 */
431
fb21efe9 432 first_x86_cpu->env.cr[3] = WIN_DUMP_FIELD(DirectoryTableBase);
92d1b3d5 433
f5daa829 434 check_kdbg(h, x64, &local_err);
2da91b54
VP
435 if (local_err) {
436 error_propagate(errp, local_err);
92d1b3d5 437 goto out_cr3;
2da91b54
VP
438 }
439
f5daa829 440 patch_header(h, x64);
2da91b54 441
fb21efe9 442 saved_ctx = g_new(struct saved_context, WIN_DUMP_FIELD(NumberProcessors));
2ad9b50f
VP
443
444 /*
445 * Always patch context because there is no way
446 * to determine if the system-saved context is valid
447 */
448
f5daa829 449 patch_and_save_context(h, x64, saved_ctx, &local_err);
2ad9b50f
VP
450 if (local_err) {
451 error_propagate(errp, local_err);
452 goto out_free;
453 }
454
fb21efe9 455 s->total_size = WIN_DUMP_FIELD(RequiredDumpSpace);
2da91b54 456
f5daa829
VP
457 s->written_size = qemu_write_full(s->fd, h, hdr_size);
458 if (s->written_size != hdr_size) {
2da91b54 459 error_setg(errp, QERR_IO_ERROR);
2ad9b50f 460 goto out_restore;
2da91b54
VP
461 }
462
f5daa829 463 write_runs(s, h, x64, &local_err);
2da91b54
VP
464 if (local_err) {
465 error_propagate(errp, local_err);
2ad9b50f 466 goto out_restore;
2da91b54 467 }
92d1b3d5 468
2ad9b50f 469out_restore:
f5daa829 470 restore_context(h, x64, saved_ctx);
2ad9b50f
VP
471out_free:
472 g_free(saved_ctx);
92d1b3d5
VP
473out_cr3:
474 first_x86_cpu->env.cr[3] = saved_cr3;
475
476 return;
2da91b54 477}