2 * Windows crashdump (target specific implementations)
4 * Copyright (c) 2018 Virtuozzo International GmbH
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.
11 #include "qemu/osdep.h"
12 #include "sysemu/dump.h"
13 #include "qapi/error.h"
14 #include "qemu/error-report.h"
15 #include "exec/cpu-defs.h"
16 #include "hw/core/cpu.h"
17 #include "qemu/win_dump_defs.h"
21 #if defined(TARGET_X86_64)
23 bool win_dump_available(Error
**errp
)
28 static size_t win_dump_ptr_size(bool x64
)
30 return x64
? sizeof(uint64_t) : sizeof(uint32_t);
33 #define _WIN_DUMP_FIELD(f) (x64 ? h->x64.f : h->x32.f)
34 #define WIN_DUMP_FIELD(field) _WIN_DUMP_FIELD(field)
36 #define _WIN_DUMP_FIELD_PTR(f) (x64 ? (void *)&h->x64.f : (void *)&h->x32.f)
37 #define WIN_DUMP_FIELD_PTR(field) _WIN_DUMP_FIELD_PTR(field)
39 #define _WIN_DUMP_FIELD_SIZE(f) (x64 ? sizeof(h->x64.f) : sizeof(h->x32.f))
40 #define WIN_DUMP_FIELD_SIZE(field) _WIN_DUMP_FIELD_SIZE(field)
42 static size_t win_dump_ctx_size(bool x64
)
44 return x64
? sizeof(WinContext64
) : sizeof(WinContext32
);
47 static size_t write_run(uint64_t base_page
, uint64_t page_count
,
51 uint64_t addr
= base_page
<< TARGET_PAGE_BITS
;
52 uint64_t size
= page_count
<< TARGET_PAGE_BITS
;
60 buf
= cpu_physical_memory_map(addr
, &len
, false);
62 error_setg(errp
, "win-dump: failed to map physical range"
63 " 0x%016" PRIx64
"-0x%016" PRIx64
, addr
, addr
+ size
- 1);
67 l
= qemu_write_full(fd
, buf
, len
);
69 cpu_physical_memory_unmap(buf
, addr
, false, len
);
71 error_setg_errno(errp
, eno
, "win-dump: failed to save memory");
83 static void write_runs(DumpState
*s
, WinDumpHeader
*h
, bool x64
, Error
**errp
)
85 uint64_t BasePage
, PageCount
;
86 Error
*local_err
= NULL
;
89 for (i
= 0; i
< WIN_DUMP_FIELD(PhysicalMemoryBlock
.NumberOfRuns
); i
++) {
90 BasePage
= WIN_DUMP_FIELD(PhysicalMemoryBlock
.Run
[i
].BasePage
);
91 PageCount
= WIN_DUMP_FIELD(PhysicalMemoryBlock
.Run
[i
].PageCount
);
92 s
->written_size
+= write_run(BasePage
, PageCount
, s
->fd
, &local_err
);
94 error_propagate(errp
, local_err
);
100 static int cpu_read_ptr(bool x64
, CPUState
*cpu
, uint64_t addr
, uint64_t *ptr
)
106 ret
= cpu_memory_rw_debug(cpu
, addr
, x64
? (void *)&ptr64
: (void *)&ptr32
,
107 win_dump_ptr_size(x64
), 0);
109 *ptr
= x64
? ptr64
: ptr32
;
114 static void patch_mm_pfn_database(WinDumpHeader
*h
, bool x64
, Error
**errp
)
116 if (cpu_memory_rw_debug(first_cpu
,
117 WIN_DUMP_FIELD(KdDebuggerDataBlock
) + KDBG_MM_PFN_DATABASE_OFFSET
,
118 WIN_DUMP_FIELD_PTR(PfnDatabase
),
119 WIN_DUMP_FIELD_SIZE(PfnDatabase
), 0)) {
120 error_setg(errp
, "win-dump: failed to read MmPfnDatabase");
125 static void patch_bugcheck_data(WinDumpHeader
*h
, bool x64
, Error
**errp
)
127 uint64_t KiBugcheckData
;
129 if (cpu_read_ptr(x64
, first_cpu
,
130 WIN_DUMP_FIELD(KdDebuggerDataBlock
) + KDBG_KI_BUGCHECK_DATA_OFFSET
,
132 error_setg(errp
, "win-dump: failed to read KiBugcheckData");
136 if (cpu_memory_rw_debug(first_cpu
, KiBugcheckData
,
137 WIN_DUMP_FIELD(BugcheckData
),
138 WIN_DUMP_FIELD_SIZE(BugcheckData
), 0)) {
139 error_setg(errp
, "win-dump: failed to read bugcheck data");
144 * If BugcheckCode wasn't saved, we consider guest OS as alive.
147 if (!WIN_DUMP_FIELD(BugcheckCode
)) {
148 *(uint32_t *)WIN_DUMP_FIELD_PTR(BugcheckCode
) = LIVE_SYSTEM_DUMP
;
153 * This routine tries to correct mistakes in crashdump header.
155 static void patch_header(WinDumpHeader
*h
, bool x64
)
157 Error
*local_err
= NULL
;
160 h
->x64
.RequiredDumpSpace
= sizeof(WinDumpHeader64
) +
161 (h
->x64
.PhysicalMemoryBlock
.NumberOfPages
<< TARGET_PAGE_BITS
);
162 h
->x64
.PhysicalMemoryBlock
.unused
= 0;
165 h
->x32
.RequiredDumpSpace
= sizeof(WinDumpHeader32
) +
166 (h
->x32
.PhysicalMemoryBlock
.NumberOfPages
<< TARGET_PAGE_BITS
);
169 patch_mm_pfn_database(h
, x64
, &local_err
);
171 warn_report_err(local_err
);
174 patch_bugcheck_data(h
, x64
, &local_err
);
176 warn_report_err(local_err
);
180 static bool check_header(WinDumpHeader
*h
, bool *x64
, Error
**errp
)
182 const char Signature
[] = "PAGE";
184 if (memcmp(h
->Signature
, Signature
, sizeof(h
->Signature
))) {
185 error_setg(errp
, "win-dump: invalid header, expected '%.4s',"
186 " got '%.4s'", Signature
, h
->Signature
);
190 if (!memcmp(h
->ValidDump
, "DUMP", sizeof(h
->ValidDump
))) {
192 } else if (!memcmp(h
->ValidDump
, "DU64", sizeof(h
->ValidDump
))) {
195 error_setg(errp
, "win-dump: invalid header, expected 'DUMP' or 'DU64',"
196 " got '%.4s'", h
->ValidDump
);
203 static void check_kdbg(WinDumpHeader
*h
, bool x64
, Error
**errp
)
205 const char OwnerTag
[] = "KDBG";
206 char read_OwnerTag
[4];
207 uint64_t KdDebuggerDataBlock
= WIN_DUMP_FIELD(KdDebuggerDataBlock
);
208 bool try_fallback
= true;
211 if (cpu_memory_rw_debug(first_cpu
,
212 KdDebuggerDataBlock
+ KDBG_OWNER_TAG_OFFSET
,
213 (uint8_t *)&read_OwnerTag
, sizeof(read_OwnerTag
), 0)) {
214 error_setg(errp
, "win-dump: failed to read OwnerTag");
218 if (memcmp(read_OwnerTag
, OwnerTag
, sizeof(read_OwnerTag
))) {
221 * If attempt to use original KDBG failed
222 * (most likely because of its encryption),
223 * we try to use KDBG obtained by guest driver.
226 KdDebuggerDataBlock
= WIN_DUMP_FIELD(BugcheckParameter1
);
227 try_fallback
= false;
230 error_setg(errp
, "win-dump: invalid KDBG OwnerTag,"
231 " expected '%.4s', got '%.4s'",
232 OwnerTag
, read_OwnerTag
);
238 h
->x64
.KdDebuggerDataBlock
= KdDebuggerDataBlock
;
240 h
->x32
.KdDebuggerDataBlock
= KdDebuggerDataBlock
;
244 struct saved_context
{
249 static void patch_and_save_context(WinDumpHeader
*h
, bool x64
,
250 struct saved_context
*saved_ctx
,
253 uint64_t KdDebuggerDataBlock
= WIN_DUMP_FIELD(KdDebuggerDataBlock
);
254 uint64_t KiProcessorBlock
;
255 uint16_t OffsetPrcbContext
;
259 if (cpu_read_ptr(x64
, first_cpu
,
260 KdDebuggerDataBlock
+ KDBG_KI_PROCESSOR_BLOCK_OFFSET
,
261 &KiProcessorBlock
)) {
262 error_setg(errp
, "win-dump: failed to read KiProcessorBlock");
266 if (cpu_memory_rw_debug(first_cpu
,
267 KdDebuggerDataBlock
+ KDBG_OFFSET_PRCB_CONTEXT_OFFSET
,
268 (uint8_t *)&OffsetPrcbContext
, sizeof(OffsetPrcbContext
), 0)) {
269 error_setg(errp
, "win-dump: failed to read OffsetPrcbContext");
274 X86CPU
*x86_cpu
= X86_CPU(cpu
);
275 CPUX86State
*env
= &x86_cpu
->env
;
280 if (i
>= WIN_DUMP_FIELD(NumberProcessors
)) {
281 warn_report("win-dump: number of QEMU CPUs is bigger than"
282 " NumberProcessors (%u) in guest Windows",
283 WIN_DUMP_FIELD(NumberProcessors
));
287 if (cpu_read_ptr(x64
, first_cpu
,
288 KiProcessorBlock
+ i
* win_dump_ptr_size(x64
),
290 error_setg(errp
, "win-dump: failed to read"
291 " CPU #%d PRCB location", i
);
295 if (cpu_read_ptr(x64
, first_cpu
,
296 Prcb
+ OffsetPrcbContext
,
298 error_setg(errp
, "win-dump: failed to read"
299 " CPU #%d ContextFrame location", i
);
303 saved_ctx
[i
].addr
= Context
;
306 ctx
.x64
= (WinContext64
){
307 .ContextFlags
= WIN_CTX64_ALL
,
310 .SegEs
= env
->segs
[0].selector
,
311 .SegCs
= env
->segs
[1].selector
,
312 .SegSs
= env
->segs
[2].selector
,
313 .SegDs
= env
->segs
[3].selector
,
314 .SegFs
= env
->segs
[4].selector
,
315 .SegGs
= env
->segs
[5].selector
,
316 .EFlags
= cpu_compute_eflags(env
),
325 .Rax
= env
->regs
[R_EAX
],
326 .Rbx
= env
->regs
[R_EBX
],
327 .Rcx
= env
->regs
[R_ECX
],
328 .Rdx
= env
->regs
[R_EDX
],
329 .Rsp
= env
->regs
[R_ESP
],
330 .Rbp
= env
->regs
[R_EBP
],
331 .Rsi
= env
->regs
[R_ESI
],
332 .Rdi
= env
->regs
[R_EDI
],
335 .R10
= env
->regs
[10],
336 .R11
= env
->regs
[11],
337 .R12
= env
->regs
[12],
338 .R13
= env
->regs
[13],
339 .R14
= env
->regs
[14],
340 .R15
= env
->regs
[15],
348 ctx
.x32
= (WinContext32
){
349 .ContextFlags
= WIN_CTX32_FULL
| WIN_CTX_DBG
,
351 .SegEs
= env
->segs
[0].selector
,
352 .SegCs
= env
->segs
[1].selector
,
353 .SegSs
= env
->segs
[2].selector
,
354 .SegDs
= env
->segs
[3].selector
,
355 .SegFs
= env
->segs
[4].selector
,
356 .SegGs
= env
->segs
[5].selector
,
357 .EFlags
= cpu_compute_eflags(env
),
366 .Eax
= env
->regs
[R_EAX
],
367 .Ebx
= env
->regs
[R_EBX
],
368 .Ecx
= env
->regs
[R_ECX
],
369 .Edx
= env
->regs
[R_EDX
],
370 .Esp
= env
->regs
[R_ESP
],
371 .Ebp
= env
->regs
[R_EBP
],
372 .Esi
= env
->regs
[R_ESI
],
373 .Edi
= env
->regs
[R_EDI
],
379 if (cpu_memory_rw_debug(first_cpu
, Context
,
380 &saved_ctx
[i
].ctx
, win_dump_ctx_size(x64
), 0)) {
381 error_setg(errp
, "win-dump: failed to save CPU #%d context", i
);
385 if (cpu_memory_rw_debug(first_cpu
, Context
,
386 &ctx
, win_dump_ctx_size(x64
), 1)) {
387 error_setg(errp
, "win-dump: failed to write CPU #%d context", i
);
395 static void restore_context(WinDumpHeader
*h
, bool x64
,
396 struct saved_context
*saved_ctx
)
400 for (i
= 0; i
< WIN_DUMP_FIELD(NumberProcessors
); i
++) {
401 if (cpu_memory_rw_debug(first_cpu
, saved_ctx
[i
].addr
,
402 &saved_ctx
[i
].ctx
, win_dump_ctx_size(x64
), 1)) {
403 warn_report("win-dump: failed to restore CPU #%d context", i
);
408 void create_win_dump(DumpState
*s
, Error
**errp
)
410 WinDumpHeader
*h
= (void *)(s
->guest_note
+ VMCOREINFO_ELF_NOTE_HDR_SIZE
);
411 X86CPU
*first_x86_cpu
= X86_CPU(first_cpu
);
412 uint64_t saved_cr3
= first_x86_cpu
->env
.cr
[3];
413 struct saved_context
*saved_ctx
= NULL
;
414 Error
*local_err
= NULL
;
418 if (s
->guest_note_size
!= VMCOREINFO_WIN_DUMP_NOTE_SIZE32
&&
419 s
->guest_note_size
!= VMCOREINFO_WIN_DUMP_NOTE_SIZE64
) {
420 error_setg(errp
, "win-dump: invalid vmcoreinfo note size");
424 if (!check_header(h
, &x64
, &local_err
)) {
425 error_propagate(errp
, local_err
);
429 hdr_size
= x64
? sizeof(WinDumpHeader64
) : sizeof(WinDumpHeader32
);
432 * Further access to kernel structures by virtual addresses
433 * should be made from system context.
436 first_x86_cpu
->env
.cr
[3] = WIN_DUMP_FIELD(DirectoryTableBase
);
438 check_kdbg(h
, x64
, &local_err
);
440 error_propagate(errp
, local_err
);
444 patch_header(h
, x64
);
446 saved_ctx
= g_new(struct saved_context
, WIN_DUMP_FIELD(NumberProcessors
));
449 * Always patch context because there is no way
450 * to determine if the system-saved context is valid
453 patch_and_save_context(h
, x64
, saved_ctx
, &local_err
);
455 error_propagate(errp
, local_err
);
459 s
->total_size
= WIN_DUMP_FIELD(RequiredDumpSpace
);
461 s
->written_size
= qemu_write_full(s
->fd
, h
, hdr_size
);
462 if (s
->written_size
!= hdr_size
) {
463 error_setg_errno(errp
, errno
, "win-dump: failed to write header");
467 write_runs(s
, h
, x64
, &local_err
);
469 error_propagate(errp
, local_err
);
474 restore_context(h
, x64
, saved_ctx
);
478 first_x86_cpu
->env
.cr
[3] = saved_cr3
;
483 #else /* !TARGET_X86_64 */
485 bool win_dump_available(Error
**errp
)
487 error_setg(errp
, "Windows dump is only available for x86-64");
492 void create_win_dump(DumpState
*s
, Error
**errp
)
494 win_dump_available(errp
);