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 "qemu/cutils.h"
15 #include "exec/hwaddr.h"
16 #include "monitor/monitor.h"
17 #include "sysemu/kvm.h"
18 #include "sysemu/dump.h"
19 #include "sysemu/sysemu.h"
20 #include "sysemu/memory_mapping.h"
21 #include "sysemu/cpus.h"
22 #include "qapi/error.h"
23 #include "qapi/qmp/qerror.h"
24 #include "qemu/error-report.h"
25 #include "hw/misc/vmcoreinfo.h"
28 static size_t write_run(WinDumpPhyMemRun64
*run
, int fd
, Error
**errp
)
31 uint64_t addr
= run
->BasePage
<< TARGET_PAGE_BITS
;
32 uint64_t size
= run
->PageCount
<< TARGET_PAGE_BITS
;
35 buf
= cpu_physical_memory_map(addr
, &len
, false);
37 error_setg(errp
, "win-dump: failed to map run");
41 error_setg(errp
, "win-dump: failed to map entire run");
46 len
= qemu_write_full(fd
, buf
, len
);
48 error_setg(errp
, QERR_IO_ERROR
);
52 cpu_physical_memory_unmap(buf
, addr
, false, len
);
57 static void write_runs(DumpState
*s
, WinDumpHeader64
*h
, Error
**errp
)
59 WinDumpPhyMemDesc64
*desc
= &h
->PhysicalMemoryBlock
;
60 WinDumpPhyMemRun64
*run
= desc
->Run
;
61 Error
*local_err
= NULL
;
64 for (i
= 0; i
< desc
->NumberOfRuns
; i
++) {
65 s
->written_size
+= write_run(run
+ i
, s
->fd
, &local_err
);
67 error_propagate(errp
, local_err
);
73 static void patch_mm_pfn_database(WinDumpHeader64
*h
, Error
**errp
)
75 if (cpu_memory_rw_debug(first_cpu
,
76 h
->KdDebuggerDataBlock
+ KDBG_MM_PFN_DATABASE_OFFSET64
,
77 (uint8_t *)&h
->PfnDatabase
, sizeof(h
->PfnDatabase
), 0)) {
78 error_setg(errp
, "win-dump: failed to read MmPfnDatabase");
83 static void patch_bugcheck_data(WinDumpHeader64
*h
, Error
**errp
)
85 uint64_t KiBugcheckData
;
87 if (cpu_memory_rw_debug(first_cpu
,
88 h
->KdDebuggerDataBlock
+ KDBG_KI_BUGCHECK_DATA_OFFSET64
,
89 (uint8_t *)&KiBugcheckData
, sizeof(KiBugcheckData
), 0)) {
90 error_setg(errp
, "win-dump: failed to read KiBugcheckData");
94 if (cpu_memory_rw_debug(first_cpu
,
96 h
->BugcheckData
, sizeof(h
->BugcheckData
), 0)) {
97 error_setg(errp
, "win-dump: failed to read bugcheck data");
103 * This routine tries to correct mistakes in crashdump header.
105 static void patch_header(WinDumpHeader64
*h
)
107 Error
*local_err
= NULL
;
109 h
->RequiredDumpSpace
= sizeof(WinDumpHeader64
) +
110 (h
->PhysicalMemoryBlock
.NumberOfPages
<< TARGET_PAGE_BITS
);
111 h
->PhysicalMemoryBlock
.unused
= 0;
115 * We assume h->DirectoryBase and current CR3 are the same when we access
116 * memory by virtual address. In other words, we suppose current context
117 * is system context. It is definetely true in case of BSOD.
120 patch_mm_pfn_database(h
, &local_err
);
122 warn_report_err(local_err
);
125 patch_bugcheck_data(h
, &local_err
);
127 warn_report_err(local_err
);
131 static void check_header(WinDumpHeader64
*h
, Error
**errp
)
133 const char Signature
[] = "PAGE";
134 const char ValidDump
[] = "DU64";
136 if (memcmp(h
->Signature
, Signature
, sizeof(h
->Signature
))) {
137 error_setg(errp
, "win-dump: invalid header, expected '%.4s',"
138 " got '%.4s'", Signature
, h
->Signature
);
142 if (memcmp(h
->ValidDump
, ValidDump
, sizeof(h
->ValidDump
))) {
143 error_setg(errp
, "win-dump: invalid header, expected '%.4s',"
144 " got '%.4s'", ValidDump
, h
->ValidDump
);
149 static void check_kdbg(WinDumpHeader64
*h
, Error
**errp
)
151 const char OwnerTag
[] = "KDBG";
152 char read_OwnerTag
[4];
154 if (cpu_memory_rw_debug(first_cpu
,
155 h
->KdDebuggerDataBlock
+ KDBG_OWNER_TAG_OFFSET64
,
156 (uint8_t *)&read_OwnerTag
, sizeof(read_OwnerTag
), 0)) {
157 error_setg(errp
, "win-dump: failed to read OwnerTag");
161 if (memcmp(read_OwnerTag
, OwnerTag
, sizeof(read_OwnerTag
))) {
162 error_setg(errp
, "win-dump: invalid KDBG OwnerTag,"
163 " expected '%.4s', got '%.4s',"
164 " KdDebuggerDataBlock seems to be encrypted",
165 OwnerTag
, read_OwnerTag
);
170 void create_win_dump(DumpState
*s
, Error
**errp
)
172 WinDumpHeader64
*h
= (WinDumpHeader64
*)(s
->guest_note
+
173 VMCOREINFO_ELF_NOTE_HDR_SIZE
);
174 Error
*local_err
= NULL
;
176 if (s
->guest_note_size
!= sizeof(WinDumpHeader64
) +
177 VMCOREINFO_ELF_NOTE_HDR_SIZE
) {
178 error_setg(errp
, "win-dump: invalid vmcoreinfo note size");
182 check_header(h
, &local_err
);
184 error_propagate(errp
, local_err
);
188 check_kdbg(h
, &local_err
);
190 error_propagate(errp
, local_err
);
196 s
->total_size
= h
->RequiredDumpSpace
;
198 s
->written_size
= qemu_write_full(s
->fd
, h
, sizeof(*h
));
199 if (s
->written_size
!= sizeof(*h
)) {
200 error_setg(errp
, QERR_IO_ERROR
);
204 write_runs(s
, h
, &local_err
);
206 error_propagate(errp
, local_err
);