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;
114 patch_mm_pfn_database(h
, &local_err
);
116 warn_report_err(local_err
);
119 patch_bugcheck_data(h
, &local_err
);
121 warn_report_err(local_err
);
125 static void check_header(WinDumpHeader64
*h
, Error
**errp
)
127 const char Signature
[] = "PAGE";
128 const char ValidDump
[] = "DU64";
130 if (memcmp(h
->Signature
, Signature
, sizeof(h
->Signature
))) {
131 error_setg(errp
, "win-dump: invalid header, expected '%.4s',"
132 " got '%.4s'", Signature
, h
->Signature
);
136 if (memcmp(h
->ValidDump
, ValidDump
, sizeof(h
->ValidDump
))) {
137 error_setg(errp
, "win-dump: invalid header, expected '%.4s',"
138 " got '%.4s'", ValidDump
, h
->ValidDump
);
143 static void check_kdbg(WinDumpHeader64
*h
, Error
**errp
)
145 const char OwnerTag
[] = "KDBG";
146 char read_OwnerTag
[4];
147 uint64_t KdDebuggerDataBlock
= h
->KdDebuggerDataBlock
;
148 bool try_fallback
= true;
151 if (cpu_memory_rw_debug(first_cpu
,
152 KdDebuggerDataBlock
+ KDBG_OWNER_TAG_OFFSET64
,
153 (uint8_t *)&read_OwnerTag
, sizeof(read_OwnerTag
), 0)) {
154 error_setg(errp
, "win-dump: failed to read OwnerTag");
158 if (memcmp(read_OwnerTag
, OwnerTag
, sizeof(read_OwnerTag
))) {
161 * If attempt to use original KDBG failed
162 * (most likely because of its encryption),
163 * we try to use KDBG obtained by guest driver.
166 KdDebuggerDataBlock
= h
->BugcheckParameter1
;
167 try_fallback
= false;
170 error_setg(errp
, "win-dump: invalid KDBG OwnerTag,"
171 " expected '%.4s', got '%.4s'",
172 OwnerTag
, read_OwnerTag
);
177 h
->KdDebuggerDataBlock
= KdDebuggerDataBlock
;
180 void create_win_dump(DumpState
*s
, Error
**errp
)
182 WinDumpHeader64
*h
= (WinDumpHeader64
*)(s
->guest_note
+
183 VMCOREINFO_ELF_NOTE_HDR_SIZE
);
184 X86CPU
*first_x86_cpu
= X86_CPU(first_cpu
);
185 uint64_t saved_cr3
= first_x86_cpu
->env
.cr
[3];
186 Error
*local_err
= NULL
;
188 if (s
->guest_note_size
!= sizeof(WinDumpHeader64
) +
189 VMCOREINFO_ELF_NOTE_HDR_SIZE
) {
190 error_setg(errp
, "win-dump: invalid vmcoreinfo note size");
194 check_header(h
, &local_err
);
196 error_propagate(errp
, local_err
);
201 * Further access to kernel structures by virtual addresses
202 * should be made from system context.
205 first_x86_cpu
->env
.cr
[3] = h
->DirectoryTableBase
;
207 check_kdbg(h
, &local_err
);
209 error_propagate(errp
, local_err
);
215 s
->total_size
= h
->RequiredDumpSpace
;
217 s
->written_size
= qemu_write_full(s
->fd
, h
, sizeof(*h
));
218 if (s
->written_size
!= sizeof(*h
)) {
219 error_setg(errp
, QERR_IO_ERROR
);
223 write_runs(s
, h
, &local_err
);
225 error_propagate(errp
, local_err
);
230 first_x86_cpu
->env
.cr
[3] = saved_cr3
;