blob: fd91350fbb8e249d747a8e10fa709359db547c7a [file] [log] [blame]
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03001/*
2 * Windows crashdump
3 *
4 * Copyright (c) 2018 Virtuozzo International GmbH
5 *
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.
8 *
9 */
10
11#include "qemu/osdep.h"
12#include "qemu/cutils.h"
13#include "elf.h"
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030014#include "exec/hwaddr.h"
15#include "monitor/monitor.h"
16#include "sysemu/kvm.h"
17#include "sysemu/dump.h"
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030018#include "sysemu/memory_mapping.h"
19#include "sysemu/cpus.h"
20#include "qapi/error.h"
21#include "qapi/qmp/qerror.h"
22#include "qemu/error-report.h"
23#include "hw/misc/vmcoreinfo.h"
24#include "win_dump.h"
25
Viktor Prutyanovf5daa822022-04-06 20:15:58 +030026static size_t win_dump_ptr_size(bool x64)
27{
28 return x64 ? sizeof(uint64_t) : sizeof(uint32_t);
29}
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +030030
Viktor Prutyanovf5daa822022-04-06 20:15:58 +030031#define _WIN_DUMP_FIELD(f) (x64 ? h->x64.f : h->x32.f)
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +030032#define WIN_DUMP_FIELD(field) _WIN_DUMP_FIELD(field)
33
Viktor Prutyanovf5daa822022-04-06 20:15:58 +030034#define _WIN_DUMP_FIELD_PTR(f) (x64 ? (void *)&h->x64.f : (void *)&h->x32.f)
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +030035#define WIN_DUMP_FIELD_PTR(field) _WIN_DUMP_FIELD_PTR(field)
36
Viktor Prutyanovf5daa822022-04-06 20:15:58 +030037#define _WIN_DUMP_FIELD_SIZE(f) (x64 ? sizeof(h->x64.f) : sizeof(h->x32.f))
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +030038#define WIN_DUMP_FIELD_SIZE(field) _WIN_DUMP_FIELD_SIZE(field)
39
Viktor Prutyanovf5daa822022-04-06 20:15:58 +030040static size_t win_dump_ctx_size(bool x64)
41{
42 return x64 ? sizeof(WinContext64) : sizeof(WinContext32);
43}
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +030044
45static size_t write_run(uint64_t base_page, uint64_t page_count,
46 int fd, Error **errp)
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030047{
48 void *buf;
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +030049 uint64_t addr = base_page << TARGET_PAGE_BITS;
50 uint64_t size = page_count << TARGET_PAGE_BITS;
Viktor Prutyanov7184de62018-08-29 21:30:56 +030051 uint64_t len, l;
52 size_t total = 0;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030053
Viktor Prutyanov7184de62018-08-29 21:30:56 +030054 while (size) {
55 len = size;
56
57 buf = cpu_physical_memory_map(addr, &len, false);
58 if (!buf) {
59 error_setg(errp, "win-dump: failed to map physical range"
60 " 0x%016" PRIx64 "-0x%016" PRIx64, addr, addr + size - 1);
61 return 0;
62 }
63
64 l = qemu_write_full(fd, buf, len);
65 cpu_physical_memory_unmap(buf, addr, false, len);
66 if (l != len) {
67 error_setg(errp, QERR_IO_ERROR);
68 return 0;
69 }
70
71 addr += l;
72 size -= l;
73 total += l;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030074 }
75
Viktor Prutyanov7184de62018-08-29 21:30:56 +030076 return total;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030077}
78
Viktor Prutyanovf5daa822022-04-06 20:15:58 +030079static void write_runs(DumpState *s, WinDumpHeader *h, bool x64, Error **errp)
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030080{
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +030081 uint64_t BasePage, PageCount;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030082 Error *local_err = NULL;
83 int i;
84
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +030085 for (i = 0; i < WIN_DUMP_FIELD(PhysicalMemoryBlock.NumberOfRuns); i++) {
86 BasePage = WIN_DUMP_FIELD(PhysicalMemoryBlock.Run[i].BasePage);
87 PageCount = WIN_DUMP_FIELD(PhysicalMemoryBlock.Run[i].PageCount);
88 s->written_size += write_run(BasePage, PageCount, s->fd, &local_err);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030089 if (local_err) {
90 error_propagate(errp, local_err);
91 return;
92 }
93 }
94}
95
Viktor Prutyanovf5daa822022-04-06 20:15:58 +030096static int cpu_read_ptr(bool x64, CPUState *cpu, uint64_t addr, uint64_t *ptr)
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +030097{
98 int ret;
Viktor Prutyanovf5daa822022-04-06 20:15:58 +030099 uint32_t ptr32;
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300100 uint64_t ptr64;
101
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300102 ret = cpu_memory_rw_debug(cpu, addr, x64 ? (void *)&ptr64 : (void *)&ptr32,
103 win_dump_ptr_size(x64), 0);
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300104
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300105 *ptr = x64 ? ptr64 : ptr32;
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300106
107 return ret;
108}
109
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300110static void patch_mm_pfn_database(WinDumpHeader *h, bool x64, Error **errp)
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300111{
112 if (cpu_memory_rw_debug(first_cpu,
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300113 WIN_DUMP_FIELD(KdDebuggerDataBlock) + KDBG_MM_PFN_DATABASE_OFFSET,
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300114 WIN_DUMP_FIELD_PTR(PfnDatabase),
115 WIN_DUMP_FIELD_SIZE(PfnDatabase), 0)) {
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300116 error_setg(errp, "win-dump: failed to read MmPfnDatabase");
117 return;
118 }
119}
120
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300121static void patch_bugcheck_data(WinDumpHeader *h, bool x64, Error **errp)
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300122{
123 uint64_t KiBugcheckData;
124
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300125 if (cpu_read_ptr(x64, first_cpu,
126 WIN_DUMP_FIELD(KdDebuggerDataBlock) + KDBG_KI_BUGCHECK_DATA_OFFSET,
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300127 &KiBugcheckData)) {
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300128 error_setg(errp, "win-dump: failed to read KiBugcheckData");
129 return;
130 }
131
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300132 if (cpu_memory_rw_debug(first_cpu, KiBugcheckData,
133 WIN_DUMP_FIELD(BugcheckData),
134 WIN_DUMP_FIELD_SIZE(BugcheckData), 0)) {
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300135 error_setg(errp, "win-dump: failed to read bugcheck data");
136 return;
137 }
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300138
139 /*
140 * If BugcheckCode wasn't saved, we consider guest OS as alive.
141 */
142
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300143 if (!WIN_DUMP_FIELD(BugcheckCode)) {
144 *(uint32_t *)WIN_DUMP_FIELD_PTR(BugcheckCode) = LIVE_SYSTEM_DUMP;
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300145 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300146}
147
148/*
149 * This routine tries to correct mistakes in crashdump header.
150 */
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300151static void patch_header(WinDumpHeader *h, bool x64)
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300152{
153 Error *local_err = NULL;
154
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300155 if (x64) {
156 h->x64.RequiredDumpSpace = sizeof(WinDumpHeader64) +
157 (h->x64.PhysicalMemoryBlock.NumberOfPages << TARGET_PAGE_BITS);
158 h->x64.PhysicalMemoryBlock.unused = 0;
159 h->x64.unused1 = 0;
160 } else {
161 h->x32.RequiredDumpSpace = sizeof(WinDumpHeader32) +
162 (h->x32.PhysicalMemoryBlock.NumberOfPages << TARGET_PAGE_BITS);
163 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300164
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300165 patch_mm_pfn_database(h, x64, &local_err);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300166 if (local_err) {
167 warn_report_err(local_err);
168 local_err = NULL;
169 }
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300170 patch_bugcheck_data(h, x64, &local_err);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300171 if (local_err) {
172 warn_report_err(local_err);
173 }
174}
175
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300176static bool check_header(WinDumpHeader *h, bool *x64, Error **errp)
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300177{
178 const char Signature[] = "PAGE";
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300179
180 if (memcmp(h->Signature, Signature, sizeof(h->Signature))) {
181 error_setg(errp, "win-dump: invalid header, expected '%.4s',"
182 " got '%.4s'", Signature, h->Signature);
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300183 return false;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300184 }
185
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300186 if (!memcmp(h->ValidDump, "DUMP", sizeof(h->ValidDump))) {
187 *x64 = false;
188 } else if (!memcmp(h->ValidDump, "DU64", sizeof(h->ValidDump))) {
189 *x64 = true;
190 } else {
191 error_setg(errp, "win-dump: invalid header, expected 'DUMP' or 'DU64',"
192 " got '%.4s'", h->ValidDump);
193 return false;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300194 }
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300195
196 return true;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300197}
198
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300199static void check_kdbg(WinDumpHeader *h, bool x64, Error **errp)
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300200{
201 const char OwnerTag[] = "KDBG";
202 char read_OwnerTag[4];
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300203 uint64_t KdDebuggerDataBlock = WIN_DUMP_FIELD(KdDebuggerDataBlock);
Viktor Prutyanov2ababfc2018-05-17 19:23:41 +0300204 bool try_fallback = true;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300205
Viktor Prutyanov2ababfc2018-05-17 19:23:41 +0300206try_again:
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300207 if (cpu_memory_rw_debug(first_cpu,
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300208 KdDebuggerDataBlock + KDBG_OWNER_TAG_OFFSET,
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300209 (uint8_t *)&read_OwnerTag, sizeof(read_OwnerTag), 0)) {
210 error_setg(errp, "win-dump: failed to read OwnerTag");
211 return;
212 }
213
214 if (memcmp(read_OwnerTag, OwnerTag, sizeof(read_OwnerTag))) {
Viktor Prutyanov2ababfc2018-05-17 19:23:41 +0300215 if (try_fallback) {
216 /*
217 * If attempt to use original KDBG failed
218 * (most likely because of its encryption),
219 * we try to use KDBG obtained by guest driver.
220 */
221
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300222 KdDebuggerDataBlock = WIN_DUMP_FIELD(BugcheckParameter1);
Viktor Prutyanov2ababfc2018-05-17 19:23:41 +0300223 try_fallback = false;
224 goto try_again;
225 } else {
226 error_setg(errp, "win-dump: invalid KDBG OwnerTag,"
227 " expected '%.4s', got '%.4s'",
228 OwnerTag, read_OwnerTag);
229 return;
230 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300231 }
Viktor Prutyanov2ababfc2018-05-17 19:23:41 +0300232
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300233 if (x64) {
234 h->x64.KdDebuggerDataBlock = KdDebuggerDataBlock;
235 } else {
236 h->x32.KdDebuggerDataBlock = KdDebuggerDataBlock;
237 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300238}
239
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300240struct saved_context {
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300241 WinContext ctx;
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300242 uint64_t addr;
243};
244
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300245static void patch_and_save_context(WinDumpHeader *h, bool x64,
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300246 struct saved_context *saved_ctx,
247 Error **errp)
248{
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300249 uint64_t KdDebuggerDataBlock = WIN_DUMP_FIELD(KdDebuggerDataBlock);
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300250 uint64_t KiProcessorBlock;
251 uint16_t OffsetPrcbContext;
252 CPUState *cpu;
253 int i = 0;
254
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300255 if (cpu_read_ptr(x64, first_cpu,
256 KdDebuggerDataBlock + KDBG_KI_PROCESSOR_BLOCK_OFFSET,
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300257 &KiProcessorBlock)) {
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300258 error_setg(errp, "win-dump: failed to read KiProcessorBlock");
259 return;
260 }
261
262 if (cpu_memory_rw_debug(first_cpu,
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300263 KdDebuggerDataBlock + KDBG_OFFSET_PRCB_CONTEXT_OFFSET,
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300264 (uint8_t *)&OffsetPrcbContext, sizeof(OffsetPrcbContext), 0)) {
265 error_setg(errp, "win-dump: failed to read OffsetPrcbContext");
266 return;
267 }
268
269 CPU_FOREACH(cpu) {
270 X86CPU *x86_cpu = X86_CPU(cpu);
271 CPUX86State *env = &x86_cpu->env;
272 uint64_t Prcb;
273 uint64_t Context;
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300274 WinContext ctx;
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300275
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300276 if (cpu_read_ptr(x64, first_cpu,
277 KiProcessorBlock + i * win_dump_ptr_size(x64),
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300278 &Prcb)) {
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300279 error_setg(errp, "win-dump: failed to read"
280 " CPU #%d PRCB location", i);
281 return;
282 }
283
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300284 if (cpu_read_ptr(x64, first_cpu,
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300285 Prcb + OffsetPrcbContext,
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300286 &Context)) {
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300287 error_setg(errp, "win-dump: failed to read"
288 " CPU #%d ContextFrame location", i);
289 return;
290 }
291
292 saved_ctx[i].addr = Context;
293
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300294 if (x64) {
295 ctx.x64 = (WinContext64){
296 .ContextFlags = WIN_CTX64_ALL,
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300297 .MxCsr = env->mxcsr,
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300298
299 .SegEs = env->segs[0].selector,
300 .SegCs = env->segs[1].selector,
301 .SegSs = env->segs[2].selector,
302 .SegDs = env->segs[3].selector,
303 .SegFs = env->segs[4].selector,
304 .SegGs = env->segs[5].selector,
305 .EFlags = cpu_compute_eflags(env),
306
307 .Dr0 = env->dr[0],
308 .Dr1 = env->dr[1],
309 .Dr2 = env->dr[2],
310 .Dr3 = env->dr[3],
311 .Dr6 = env->dr[6],
312 .Dr7 = env->dr[7],
313
314 .Rax = env->regs[R_EAX],
315 .Rbx = env->regs[R_EBX],
316 .Rcx = env->regs[R_ECX],
317 .Rdx = env->regs[R_EDX],
318 .Rsp = env->regs[R_ESP],
319 .Rbp = env->regs[R_EBP],
320 .Rsi = env->regs[R_ESI],
321 .Rdi = env->regs[R_EDI],
322 .R8 = env->regs[8],
323 .R9 = env->regs[9],
324 .R10 = env->regs[10],
325 .R11 = env->regs[11],
326 .R12 = env->regs[12],
327 .R13 = env->regs[13],
328 .R14 = env->regs[14],
329 .R15 = env->regs[15],
330
331 .Rip = env->eip,
332 .FltSave = {
333 .MxCsr = env->mxcsr,
334 },
335 };
336 } else {
337 ctx.x32 = (WinContext32){
338 .ContextFlags = WIN_CTX32_FULL | WIN_CTX_DBG,
339
340 .SegEs = env->segs[0].selector,
341 .SegCs = env->segs[1].selector,
342 .SegSs = env->segs[2].selector,
343 .SegDs = env->segs[3].selector,
344 .SegFs = env->segs[4].selector,
345 .SegGs = env->segs[5].selector,
346 .EFlags = cpu_compute_eflags(env),
347
348 .Dr0 = env->dr[0],
349 .Dr1 = env->dr[1],
350 .Dr2 = env->dr[2],
351 .Dr3 = env->dr[3],
352 .Dr6 = env->dr[6],
353 .Dr7 = env->dr[7],
354
355 .Eax = env->regs[R_EAX],
356 .Ebx = env->regs[R_EBX],
357 .Ecx = env->regs[R_ECX],
358 .Edx = env->regs[R_EDX],
359 .Esp = env->regs[R_ESP],
360 .Ebp = env->regs[R_EBP],
361 .Esi = env->regs[R_ESI],
362 .Edi = env->regs[R_EDI],
363
364 .Eip = env->eip,
365 };
366 }
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300367
368 if (cpu_memory_rw_debug(first_cpu, Context,
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300369 &saved_ctx[i].ctx, win_dump_ctx_size(x64), 0)) {
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300370 error_setg(errp, "win-dump: failed to save CPU #%d context", i);
371 return;
372 }
373
374 if (cpu_memory_rw_debug(first_cpu, Context,
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300375 &ctx, win_dump_ctx_size(x64), 1)) {
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300376 error_setg(errp, "win-dump: failed to write CPU #%d context", i);
377 return;
378 }
379
380 i++;
381 }
382}
383
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300384static void restore_context(WinDumpHeader *h, bool x64,
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300385 struct saved_context *saved_ctx)
386{
387 int i;
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300388
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300389 for (i = 0; i < WIN_DUMP_FIELD(NumberProcessors); i++) {
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300390 if (cpu_memory_rw_debug(first_cpu, saved_ctx[i].addr,
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300391 &saved_ctx[i].ctx, win_dump_ctx_size(x64), 1)) {
Vladimir Sementsov-Ogievskiyb0e70952020-03-24 18:36:27 +0300392 warn_report("win-dump: failed to restore CPU #%d context", i);
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300393 }
394 }
395}
396
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300397void create_win_dump(DumpState *s, Error **errp)
398{
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300399 WinDumpHeader *h = (void *)(s->guest_note + VMCOREINFO_ELF_NOTE_HDR_SIZE);
Viktor Prutyanov92d1b3d2018-05-17 19:23:40 +0300400 X86CPU *first_x86_cpu = X86_CPU(first_cpu);
401 uint64_t saved_cr3 = first_x86_cpu->env.cr[3];
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300402 struct saved_context *saved_ctx = NULL;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300403 Error *local_err = NULL;
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300404 bool x64 = true;
405 size_t hdr_size;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300406
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300407 if (s->guest_note_size != VMCOREINFO_WIN_DUMP_NOTE_SIZE32 &&
408 s->guest_note_size != VMCOREINFO_WIN_DUMP_NOTE_SIZE64) {
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300409 error_setg(errp, "win-dump: invalid vmcoreinfo note size");
410 return;
411 }
412
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300413 if (!check_header(h, &x64, &local_err)) {
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300414 error_propagate(errp, local_err);
415 return;
416 }
417
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300418 hdr_size = x64 ? sizeof(WinDumpHeader64) : sizeof(WinDumpHeader32);
419
Viktor Prutyanov92d1b3d2018-05-17 19:23:40 +0300420 /*
421 * Further access to kernel structures by virtual addresses
422 * should be made from system context.
423 */
424
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300425 first_x86_cpu->env.cr[3] = WIN_DUMP_FIELD(DirectoryTableBase);
Viktor Prutyanov92d1b3d2018-05-17 19:23:40 +0300426
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300427 check_kdbg(h, x64, &local_err);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300428 if (local_err) {
429 error_propagate(errp, local_err);
Viktor Prutyanov92d1b3d2018-05-17 19:23:40 +0300430 goto out_cr3;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300431 }
432
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300433 patch_header(h, x64);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300434
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300435 saved_ctx = g_new(struct saved_context, WIN_DUMP_FIELD(NumberProcessors));
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300436
437 /*
438 * Always patch context because there is no way
439 * to determine if the system-saved context is valid
440 */
441
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300442 patch_and_save_context(h, x64, saved_ctx, &local_err);
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300443 if (local_err) {
444 error_propagate(errp, local_err);
445 goto out_free;
446 }
447
Viktor Prutyanovfb21efe2022-04-06 20:15:56 +0300448 s->total_size = WIN_DUMP_FIELD(RequiredDumpSpace);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300449
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300450 s->written_size = qemu_write_full(s->fd, h, hdr_size);
451 if (s->written_size != hdr_size) {
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300452 error_setg(errp, QERR_IO_ERROR);
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300453 goto out_restore;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300454 }
455
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300456 write_runs(s, h, x64, &local_err);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300457 if (local_err) {
458 error_propagate(errp, local_err);
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300459 goto out_restore;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300460 }
Viktor Prutyanov92d1b3d2018-05-17 19:23:40 +0300461
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300462out_restore:
Viktor Prutyanovf5daa822022-04-06 20:15:58 +0300463 restore_context(h, x64, saved_ctx);
Viktor Prutyanov2ad9b502018-05-17 19:23:42 +0300464out_free:
465 g_free(saved_ctx);
Viktor Prutyanov92d1b3d2018-05-17 19:23:40 +0300466out_cr3:
467 first_x86_cpu->env.cr[3] = saved_cr3;
468
469 return;
Viktor Prutyanov2da91b52018-05-17 19:23:39 +0300470}