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");
102 * If BugcheckCode wasn't saved, we consider guest OS as alive.
105 if (!h
->BugcheckCode
) {
106 h
->BugcheckCode
= LIVE_SYSTEM_DUMP
;
111 * This routine tries to correct mistakes in crashdump header.
113 static void patch_header(WinDumpHeader64
*h
)
115 Error
*local_err
= NULL
;
117 h
->RequiredDumpSpace
= sizeof(WinDumpHeader64
) +
118 (h
->PhysicalMemoryBlock
.NumberOfPages
<< TARGET_PAGE_BITS
);
119 h
->PhysicalMemoryBlock
.unused
= 0;
122 patch_mm_pfn_database(h
, &local_err
);
124 warn_report_err(local_err
);
127 patch_bugcheck_data(h
, &local_err
);
129 warn_report_err(local_err
);
133 static void check_header(WinDumpHeader64
*h
, Error
**errp
)
135 const char Signature
[] = "PAGE";
136 const char ValidDump
[] = "DU64";
138 if (memcmp(h
->Signature
, Signature
, sizeof(h
->Signature
))) {
139 error_setg(errp
, "win-dump: invalid header, expected '%.4s',"
140 " got '%.4s'", Signature
, h
->Signature
);
144 if (memcmp(h
->ValidDump
, ValidDump
, sizeof(h
->ValidDump
))) {
145 error_setg(errp
, "win-dump: invalid header, expected '%.4s',"
146 " got '%.4s'", ValidDump
, h
->ValidDump
);
151 static void check_kdbg(WinDumpHeader64
*h
, Error
**errp
)
153 const char OwnerTag
[] = "KDBG";
154 char read_OwnerTag
[4];
155 uint64_t KdDebuggerDataBlock
= h
->KdDebuggerDataBlock
;
156 bool try_fallback
= true;
159 if (cpu_memory_rw_debug(first_cpu
,
160 KdDebuggerDataBlock
+ KDBG_OWNER_TAG_OFFSET64
,
161 (uint8_t *)&read_OwnerTag
, sizeof(read_OwnerTag
), 0)) {
162 error_setg(errp
, "win-dump: failed to read OwnerTag");
166 if (memcmp(read_OwnerTag
, OwnerTag
, sizeof(read_OwnerTag
))) {
169 * If attempt to use original KDBG failed
170 * (most likely because of its encryption),
171 * we try to use KDBG obtained by guest driver.
174 KdDebuggerDataBlock
= h
->BugcheckParameter1
;
175 try_fallback
= false;
178 error_setg(errp
, "win-dump: invalid KDBG OwnerTag,"
179 " expected '%.4s', got '%.4s'",
180 OwnerTag
, read_OwnerTag
);
185 h
->KdDebuggerDataBlock
= KdDebuggerDataBlock
;
188 struct saved_context
{
193 static void patch_and_save_context(WinDumpHeader64
*h
,
194 struct saved_context
*saved_ctx
,
197 uint64_t KiProcessorBlock
;
198 uint16_t OffsetPrcbContext
;
202 if (cpu_memory_rw_debug(first_cpu
,
203 h
->KdDebuggerDataBlock
+ KDBG_KI_PROCESSOR_BLOCK_OFFSET64
,
204 (uint8_t *)&KiProcessorBlock
, sizeof(KiProcessorBlock
), 0)) {
205 error_setg(errp
, "win-dump: failed to read KiProcessorBlock");
209 if (cpu_memory_rw_debug(first_cpu
,
210 h
->KdDebuggerDataBlock
+ KDBG_OFFSET_PRCB_CONTEXT_OFFSET64
,
211 (uint8_t *)&OffsetPrcbContext
, sizeof(OffsetPrcbContext
), 0)) {
212 error_setg(errp
, "win-dump: failed to read OffsetPrcbContext");
217 X86CPU
*x86_cpu
= X86_CPU(cpu
);
218 CPUX86State
*env
= &x86_cpu
->env
;
223 if (cpu_memory_rw_debug(first_cpu
,
224 KiProcessorBlock
+ i
* sizeof(uint64_t),
225 (uint8_t *)&Prcb
, sizeof(Prcb
), 0)) {
226 error_setg(errp
, "win-dump: failed to read"
227 " CPU #%d PRCB location", i
);
231 if (cpu_memory_rw_debug(first_cpu
,
232 Prcb
+ OffsetPrcbContext
,
233 (uint8_t *)&Context
, sizeof(Context
), 0)) {
234 error_setg(errp
, "win-dump: failed to read"
235 " CPU #%d ContextFrame location", i
);
239 saved_ctx
[i
].addr
= Context
;
242 .ContextFlags
= WIN_CTX_ALL
,
245 .SegEs
= env
->segs
[0].selector
,
246 .SegCs
= env
->segs
[1].selector
,
247 .SegSs
= env
->segs
[2].selector
,
248 .SegDs
= env
->segs
[3].selector
,
249 .SegFs
= env
->segs
[4].selector
,
250 .SegGs
= env
->segs
[5].selector
,
251 .EFlags
= cpu_compute_eflags(env
),
260 .Rax
= env
->regs
[R_EAX
],
261 .Rbx
= env
->regs
[R_EBX
],
262 .Rcx
= env
->regs
[R_ECX
],
263 .Rdx
= env
->regs
[R_EDX
],
264 .Rsp
= env
->regs
[R_ESP
],
265 .Rbp
= env
->regs
[R_EBP
],
266 .Rsi
= env
->regs
[R_ESI
],
267 .Rdi
= env
->regs
[R_EDI
],
270 .R10
= env
->regs
[10],
271 .R11
= env
->regs
[11],
272 .R12
= env
->regs
[12],
273 .R13
= env
->regs
[13],
274 .R14
= env
->regs
[14],
275 .R15
= env
->regs
[15],
283 if (cpu_memory_rw_debug(first_cpu
, Context
,
284 (uint8_t *)&saved_ctx
[i
].ctx
, sizeof(WinContext
), 0)) {
285 error_setg(errp
, "win-dump: failed to save CPU #%d context", i
);
289 if (cpu_memory_rw_debug(first_cpu
, Context
,
290 (uint8_t *)&ctx
, sizeof(WinContext
), 1)) {
291 error_setg(errp
, "win-dump: failed to write CPU #%d context", i
);
299 static void restore_context(WinDumpHeader64
*h
,
300 struct saved_context
*saved_ctx
)
305 for (i
= 0; i
< h
->NumberProcessors
; i
++) {
306 if (cpu_memory_rw_debug(first_cpu
, saved_ctx
[i
].addr
,
307 (uint8_t *)&saved_ctx
[i
].ctx
, sizeof(WinContext
), 1)) {
308 error_setg(&err
, "win-dump: failed to restore CPU #%d context", i
);
309 warn_report_err(err
);
314 void create_win_dump(DumpState
*s
, Error
**errp
)
316 WinDumpHeader64
*h
= (WinDumpHeader64
*)(s
->guest_note
+
317 VMCOREINFO_ELF_NOTE_HDR_SIZE
);
318 X86CPU
*first_x86_cpu
= X86_CPU(first_cpu
);
319 uint64_t saved_cr3
= first_x86_cpu
->env
.cr
[3];
320 struct saved_context
*saved_ctx
= NULL
;
321 Error
*local_err
= NULL
;
323 if (s
->guest_note_size
!= sizeof(WinDumpHeader64
) +
324 VMCOREINFO_ELF_NOTE_HDR_SIZE
) {
325 error_setg(errp
, "win-dump: invalid vmcoreinfo note size");
329 check_header(h
, &local_err
);
331 error_propagate(errp
, local_err
);
336 * Further access to kernel structures by virtual addresses
337 * should be made from system context.
340 first_x86_cpu
->env
.cr
[3] = h
->DirectoryTableBase
;
342 check_kdbg(h
, &local_err
);
344 error_propagate(errp
, local_err
);
350 saved_ctx
= g_new(struct saved_context
, h
->NumberProcessors
);
353 * Always patch context because there is no way
354 * to determine if the system-saved context is valid
357 patch_and_save_context(h
, saved_ctx
, &local_err
);
359 error_propagate(errp
, local_err
);
363 s
->total_size
= h
->RequiredDumpSpace
;
365 s
->written_size
= qemu_write_full(s
->fd
, h
, sizeof(*h
));
366 if (s
->written_size
!= sizeof(*h
)) {
367 error_setg(errp
, QERR_IO_ERROR
);
371 write_runs(s
, h
, &local_err
);
373 error_propagate(errp
, local_err
);
378 restore_context(h
, saved_ctx
);
382 first_x86_cpu
->env
.cr
[3] = saved_cr3
;