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-common.h"
13 #include "qemu/cutils.h"
16 #include "exec/hwaddr.h"
17 #include "monitor/monitor.h"
18 #include "sysemu/kvm.h"
19 #include "sysemu/dump.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
;
39 buf
= cpu_physical_memory_map(addr
, &len
, false);
41 error_setg(errp
, "win-dump: failed to map physical range"
42 " 0x%016" PRIx64
"-0x%016" PRIx64
, addr
, addr
+ size
- 1);
46 l
= qemu_write_full(fd
, buf
, len
);
47 cpu_physical_memory_unmap(buf
, addr
, false, len
);
49 error_setg(errp
, QERR_IO_ERROR
);
61 static void write_runs(DumpState
*s
, WinDumpHeader64
*h
, Error
**errp
)
63 WinDumpPhyMemDesc64
*desc
= &h
->PhysicalMemoryBlock
;
64 WinDumpPhyMemRun64
*run
= desc
->Run
;
65 Error
*local_err
= NULL
;
68 for (i
= 0; i
< desc
->NumberOfRuns
; i
++) {
69 s
->written_size
+= write_run(run
+ i
, s
->fd
, &local_err
);
71 error_propagate(errp
, local_err
);
77 static void patch_mm_pfn_database(WinDumpHeader64
*h
, Error
**errp
)
79 if (cpu_memory_rw_debug(first_cpu
,
80 h
->KdDebuggerDataBlock
+ KDBG_MM_PFN_DATABASE_OFFSET64
,
81 (uint8_t *)&h
->PfnDatabase
, sizeof(h
->PfnDatabase
), 0)) {
82 error_setg(errp
, "win-dump: failed to read MmPfnDatabase");
87 static void patch_bugcheck_data(WinDumpHeader64
*h
, Error
**errp
)
89 uint64_t KiBugcheckData
;
91 if (cpu_memory_rw_debug(first_cpu
,
92 h
->KdDebuggerDataBlock
+ KDBG_KI_BUGCHECK_DATA_OFFSET64
,
93 (uint8_t *)&KiBugcheckData
, sizeof(KiBugcheckData
), 0)) {
94 error_setg(errp
, "win-dump: failed to read KiBugcheckData");
98 if (cpu_memory_rw_debug(first_cpu
,
100 h
->BugcheckData
, sizeof(h
->BugcheckData
), 0)) {
101 error_setg(errp
, "win-dump: failed to read bugcheck data");
106 * If BugcheckCode wasn't saved, we consider guest OS as alive.
109 if (!h
->BugcheckCode
) {
110 h
->BugcheckCode
= LIVE_SYSTEM_DUMP
;
115 * This routine tries to correct mistakes in crashdump header.
117 static void patch_header(WinDumpHeader64
*h
)
119 Error
*local_err
= NULL
;
121 h
->RequiredDumpSpace
= sizeof(WinDumpHeader64
) +
122 (h
->PhysicalMemoryBlock
.NumberOfPages
<< TARGET_PAGE_BITS
);
123 h
->PhysicalMemoryBlock
.unused
= 0;
126 patch_mm_pfn_database(h
, &local_err
);
128 warn_report_err(local_err
);
131 patch_bugcheck_data(h
, &local_err
);
133 warn_report_err(local_err
);
137 static void check_header(WinDumpHeader64
*h
, Error
**errp
)
139 const char Signature
[] = "PAGE";
140 const char ValidDump
[] = "DU64";
142 if (memcmp(h
->Signature
, Signature
, sizeof(h
->Signature
))) {
143 error_setg(errp
, "win-dump: invalid header, expected '%.4s',"
144 " got '%.4s'", Signature
, h
->Signature
);
148 if (memcmp(h
->ValidDump
, ValidDump
, sizeof(h
->ValidDump
))) {
149 error_setg(errp
, "win-dump: invalid header, expected '%.4s',"
150 " got '%.4s'", ValidDump
, h
->ValidDump
);
155 static void check_kdbg(WinDumpHeader64
*h
, Error
**errp
)
157 const char OwnerTag
[] = "KDBG";
158 char read_OwnerTag
[4];
159 uint64_t KdDebuggerDataBlock
= h
->KdDebuggerDataBlock
;
160 bool try_fallback
= true;
163 if (cpu_memory_rw_debug(first_cpu
,
164 KdDebuggerDataBlock
+ KDBG_OWNER_TAG_OFFSET64
,
165 (uint8_t *)&read_OwnerTag
, sizeof(read_OwnerTag
), 0)) {
166 error_setg(errp
, "win-dump: failed to read OwnerTag");
170 if (memcmp(read_OwnerTag
, OwnerTag
, sizeof(read_OwnerTag
))) {
173 * If attempt to use original KDBG failed
174 * (most likely because of its encryption),
175 * we try to use KDBG obtained by guest driver.
178 KdDebuggerDataBlock
= h
->BugcheckParameter1
;
179 try_fallback
= false;
182 error_setg(errp
, "win-dump: invalid KDBG OwnerTag,"
183 " expected '%.4s', got '%.4s'",
184 OwnerTag
, read_OwnerTag
);
189 h
->KdDebuggerDataBlock
= KdDebuggerDataBlock
;
192 struct saved_context
{
197 static void patch_and_save_context(WinDumpHeader64
*h
,
198 struct saved_context
*saved_ctx
,
201 uint64_t KiProcessorBlock
;
202 uint16_t OffsetPrcbContext
;
206 if (cpu_memory_rw_debug(first_cpu
,
207 h
->KdDebuggerDataBlock
+ KDBG_KI_PROCESSOR_BLOCK_OFFSET64
,
208 (uint8_t *)&KiProcessorBlock
, sizeof(KiProcessorBlock
), 0)) {
209 error_setg(errp
, "win-dump: failed to read KiProcessorBlock");
213 if (cpu_memory_rw_debug(first_cpu
,
214 h
->KdDebuggerDataBlock
+ KDBG_OFFSET_PRCB_CONTEXT_OFFSET64
,
215 (uint8_t *)&OffsetPrcbContext
, sizeof(OffsetPrcbContext
), 0)) {
216 error_setg(errp
, "win-dump: failed to read OffsetPrcbContext");
221 X86CPU
*x86_cpu
= X86_CPU(cpu
);
222 CPUX86State
*env
= &x86_cpu
->env
;
227 if (cpu_memory_rw_debug(first_cpu
,
228 KiProcessorBlock
+ i
* sizeof(uint64_t),
229 (uint8_t *)&Prcb
, sizeof(Prcb
), 0)) {
230 error_setg(errp
, "win-dump: failed to read"
231 " CPU #%d PRCB location", i
);
235 if (cpu_memory_rw_debug(first_cpu
,
236 Prcb
+ OffsetPrcbContext
,
237 (uint8_t *)&Context
, sizeof(Context
), 0)) {
238 error_setg(errp
, "win-dump: failed to read"
239 " CPU #%d ContextFrame location", i
);
243 saved_ctx
[i
].addr
= Context
;
246 .ContextFlags
= WIN_CTX_ALL
,
249 .SegEs
= env
->segs
[0].selector
,
250 .SegCs
= env
->segs
[1].selector
,
251 .SegSs
= env
->segs
[2].selector
,
252 .SegDs
= env
->segs
[3].selector
,
253 .SegFs
= env
->segs
[4].selector
,
254 .SegGs
= env
->segs
[5].selector
,
255 .EFlags
= cpu_compute_eflags(env
),
264 .Rax
= env
->regs
[R_EAX
],
265 .Rbx
= env
->regs
[R_EBX
],
266 .Rcx
= env
->regs
[R_ECX
],
267 .Rdx
= env
->regs
[R_EDX
],
268 .Rsp
= env
->regs
[R_ESP
],
269 .Rbp
= env
->regs
[R_EBP
],
270 .Rsi
= env
->regs
[R_ESI
],
271 .Rdi
= env
->regs
[R_EDI
],
274 .R10
= env
->regs
[10],
275 .R11
= env
->regs
[11],
276 .R12
= env
->regs
[12],
277 .R13
= env
->regs
[13],
278 .R14
= env
->regs
[14],
279 .R15
= env
->regs
[15],
287 if (cpu_memory_rw_debug(first_cpu
, Context
,
288 (uint8_t *)&saved_ctx
[i
].ctx
, sizeof(WinContext
), 0)) {
289 error_setg(errp
, "win-dump: failed to save CPU #%d context", i
);
293 if (cpu_memory_rw_debug(first_cpu
, Context
,
294 (uint8_t *)&ctx
, sizeof(WinContext
), 1)) {
295 error_setg(errp
, "win-dump: failed to write CPU #%d context", i
);
303 static void restore_context(WinDumpHeader64
*h
,
304 struct saved_context
*saved_ctx
)
309 for (i
= 0; i
< h
->NumberProcessors
; i
++) {
310 if (cpu_memory_rw_debug(first_cpu
, saved_ctx
[i
].addr
,
311 (uint8_t *)&saved_ctx
[i
].ctx
, sizeof(WinContext
), 1)) {
312 error_setg(&err
, "win-dump: failed to restore CPU #%d context", i
);
313 warn_report_err(err
);
318 void create_win_dump(DumpState
*s
, Error
**errp
)
320 WinDumpHeader64
*h
= (WinDumpHeader64
*)(s
->guest_note
+
321 VMCOREINFO_ELF_NOTE_HDR_SIZE
);
322 X86CPU
*first_x86_cpu
= X86_CPU(first_cpu
);
323 uint64_t saved_cr3
= first_x86_cpu
->env
.cr
[3];
324 struct saved_context
*saved_ctx
= NULL
;
325 Error
*local_err
= NULL
;
327 if (s
->guest_note_size
!= sizeof(WinDumpHeader64
) +
328 VMCOREINFO_ELF_NOTE_HDR_SIZE
) {
329 error_setg(errp
, "win-dump: invalid vmcoreinfo note size");
333 check_header(h
, &local_err
);
335 error_propagate(errp
, local_err
);
340 * Further access to kernel structures by virtual addresses
341 * should be made from system context.
344 first_x86_cpu
->env
.cr
[3] = h
->DirectoryTableBase
;
346 check_kdbg(h
, &local_err
);
348 error_propagate(errp
, local_err
);
354 saved_ctx
= g_new(struct saved_context
, h
->NumberProcessors
);
357 * Always patch context because there is no way
358 * to determine if the system-saved context is valid
361 patch_and_save_context(h
, saved_ctx
, &local_err
);
363 error_propagate(errp
, local_err
);
367 s
->total_size
= h
->RequiredDumpSpace
;
369 s
->written_size
= qemu_write_full(s
->fd
, h
, sizeof(*h
));
370 if (s
->written_size
!= sizeof(*h
)) {
371 error_setg(errp
, QERR_IO_ERROR
);
375 write_runs(s
, h
, &local_err
);
377 error_propagate(errp
, local_err
);
382 restore_context(h
, saved_ctx
);
386 first_x86_cpu
->env
.cr
[3] = saved_cr3
;