xref: /qemu/dump/win_dump.c (revision a8d2532645cf5ce4f75981f81dfe363efc35d05c)
12da91b54SViktor Prutyanov /*
22da91b54SViktor Prutyanov  * Windows crashdump
32da91b54SViktor Prutyanov  *
42da91b54SViktor Prutyanov  * Copyright (c) 2018 Virtuozzo International GmbH
52da91b54SViktor Prutyanov  *
62da91b54SViktor Prutyanov  * This work is licensed under the terms of the GNU GPL, version 2 or later.
72da91b54SViktor Prutyanov  * See the COPYING file in the top-level directory.
82da91b54SViktor Prutyanov  *
92da91b54SViktor Prutyanov  */
102da91b54SViktor Prutyanov 
112da91b54SViktor Prutyanov #include "qemu/osdep.h"
12*a8d25326SMarkus Armbruster #include "qemu-common.h"
132da91b54SViktor Prutyanov #include "qemu/cutils.h"
142da91b54SViktor Prutyanov #include "elf.h"
152da91b54SViktor Prutyanov #include "cpu.h"
162da91b54SViktor Prutyanov #include "exec/hwaddr.h"
172da91b54SViktor Prutyanov #include "monitor/monitor.h"
182da91b54SViktor Prutyanov #include "sysemu/kvm.h"
192da91b54SViktor Prutyanov #include "sysemu/dump.h"
202da91b54SViktor Prutyanov #include "sysemu/sysemu.h"
212da91b54SViktor Prutyanov #include "sysemu/memory_mapping.h"
222da91b54SViktor Prutyanov #include "sysemu/cpus.h"
232da91b54SViktor Prutyanov #include "qapi/error.h"
242da91b54SViktor Prutyanov #include "qapi/qmp/qerror.h"
252da91b54SViktor Prutyanov #include "qemu/error-report.h"
262da91b54SViktor Prutyanov #include "hw/misc/vmcoreinfo.h"
272da91b54SViktor Prutyanov #include "win_dump.h"
282da91b54SViktor Prutyanov 
292da91b54SViktor Prutyanov static size_t write_run(WinDumpPhyMemRun64 *run, int fd, Error **errp)
302da91b54SViktor Prutyanov {
312da91b54SViktor Prutyanov     void *buf;
322da91b54SViktor Prutyanov     uint64_t addr = run->BasePage << TARGET_PAGE_BITS;
332da91b54SViktor Prutyanov     uint64_t size = run->PageCount << TARGET_PAGE_BITS;
347184de64SViktor Prutyanov     uint64_t len, l;
357184de64SViktor Prutyanov     size_t total = 0;
367184de64SViktor Prutyanov 
377184de64SViktor Prutyanov     while (size) {
387184de64SViktor Prutyanov         len = size;
392da91b54SViktor Prutyanov 
402da91b54SViktor Prutyanov         buf = cpu_physical_memory_map(addr, &len, false);
412da91b54SViktor Prutyanov         if (!buf) {
427184de64SViktor Prutyanov             error_setg(errp, "win-dump: failed to map physical range"
437184de64SViktor Prutyanov                              " 0x%016" PRIx64 "-0x%016" PRIx64, addr, addr + size - 1);
442da91b54SViktor Prutyanov             return 0;
452da91b54SViktor Prutyanov         }
462da91b54SViktor Prutyanov 
477184de64SViktor Prutyanov         l = qemu_write_full(fd, buf, len);
482da91b54SViktor Prutyanov         cpu_physical_memory_unmap(buf, addr, false, len);
497184de64SViktor Prutyanov         if (l != len) {
507184de64SViktor Prutyanov             error_setg(errp, QERR_IO_ERROR);
517184de64SViktor Prutyanov             return 0;
527184de64SViktor Prutyanov         }
532da91b54SViktor Prutyanov 
547184de64SViktor Prutyanov         addr += l;
557184de64SViktor Prutyanov         size -= l;
567184de64SViktor Prutyanov         total += l;
577184de64SViktor Prutyanov     }
587184de64SViktor Prutyanov 
597184de64SViktor Prutyanov     return total;
602da91b54SViktor Prutyanov }
612da91b54SViktor Prutyanov 
622da91b54SViktor Prutyanov static void write_runs(DumpState *s, WinDumpHeader64 *h, Error **errp)
632da91b54SViktor Prutyanov {
642da91b54SViktor Prutyanov     WinDumpPhyMemDesc64 *desc = &h->PhysicalMemoryBlock;
652da91b54SViktor Prutyanov     WinDumpPhyMemRun64 *run = desc->Run;
662da91b54SViktor Prutyanov     Error *local_err = NULL;
672da91b54SViktor Prutyanov     int i;
682da91b54SViktor Prutyanov 
692da91b54SViktor Prutyanov     for (i = 0; i < desc->NumberOfRuns; i++) {
702da91b54SViktor Prutyanov         s->written_size += write_run(run + i, s->fd, &local_err);
712da91b54SViktor Prutyanov         if (local_err) {
722da91b54SViktor Prutyanov             error_propagate(errp, local_err);
732da91b54SViktor Prutyanov             return;
742da91b54SViktor Prutyanov         }
752da91b54SViktor Prutyanov     }
762da91b54SViktor Prutyanov }
772da91b54SViktor Prutyanov 
782da91b54SViktor Prutyanov static void patch_mm_pfn_database(WinDumpHeader64 *h, Error **errp)
792da91b54SViktor Prutyanov {
802da91b54SViktor Prutyanov     if (cpu_memory_rw_debug(first_cpu,
812da91b54SViktor Prutyanov             h->KdDebuggerDataBlock + KDBG_MM_PFN_DATABASE_OFFSET64,
822da91b54SViktor Prutyanov             (uint8_t *)&h->PfnDatabase, sizeof(h->PfnDatabase), 0)) {
832da91b54SViktor Prutyanov         error_setg(errp, "win-dump: failed to read MmPfnDatabase");
842da91b54SViktor Prutyanov         return;
852da91b54SViktor Prutyanov     }
862da91b54SViktor Prutyanov }
872da91b54SViktor Prutyanov 
882da91b54SViktor Prutyanov static void patch_bugcheck_data(WinDumpHeader64 *h, Error **errp)
892da91b54SViktor Prutyanov {
902da91b54SViktor Prutyanov     uint64_t KiBugcheckData;
912da91b54SViktor Prutyanov 
922da91b54SViktor Prutyanov     if (cpu_memory_rw_debug(first_cpu,
932da91b54SViktor Prutyanov             h->KdDebuggerDataBlock + KDBG_KI_BUGCHECK_DATA_OFFSET64,
942da91b54SViktor Prutyanov             (uint8_t *)&KiBugcheckData, sizeof(KiBugcheckData), 0)) {
952da91b54SViktor Prutyanov         error_setg(errp, "win-dump: failed to read KiBugcheckData");
962da91b54SViktor Prutyanov         return;
972da91b54SViktor Prutyanov     }
982da91b54SViktor Prutyanov 
992da91b54SViktor Prutyanov     if (cpu_memory_rw_debug(first_cpu,
1002da91b54SViktor Prutyanov             KiBugcheckData,
1012da91b54SViktor Prutyanov             h->BugcheckData, sizeof(h->BugcheckData), 0)) {
1022da91b54SViktor Prutyanov         error_setg(errp, "win-dump: failed to read bugcheck data");
1032da91b54SViktor Prutyanov         return;
1042da91b54SViktor Prutyanov     }
1052ad9b50fSViktor Prutyanov 
1062ad9b50fSViktor Prutyanov     /*
1072ad9b50fSViktor Prutyanov      * If BugcheckCode wasn't saved, we consider guest OS as alive.
1082ad9b50fSViktor Prutyanov      */
1092ad9b50fSViktor Prutyanov 
1102ad9b50fSViktor Prutyanov     if (!h->BugcheckCode) {
1112ad9b50fSViktor Prutyanov         h->BugcheckCode = LIVE_SYSTEM_DUMP;
1122ad9b50fSViktor Prutyanov     }
1132da91b54SViktor Prutyanov }
1142da91b54SViktor Prutyanov 
1152da91b54SViktor Prutyanov /*
1162da91b54SViktor Prutyanov  * This routine tries to correct mistakes in crashdump header.
1172da91b54SViktor Prutyanov  */
1182da91b54SViktor Prutyanov static void patch_header(WinDumpHeader64 *h)
1192da91b54SViktor Prutyanov {
1202da91b54SViktor Prutyanov     Error *local_err = NULL;
1212da91b54SViktor Prutyanov 
1222da91b54SViktor Prutyanov     h->RequiredDumpSpace = sizeof(WinDumpHeader64) +
1232da91b54SViktor Prutyanov             (h->PhysicalMemoryBlock.NumberOfPages << TARGET_PAGE_BITS);
1242da91b54SViktor Prutyanov     h->PhysicalMemoryBlock.unused = 0;
1252da91b54SViktor Prutyanov     h->unused1 = 0;
1262da91b54SViktor Prutyanov 
1272da91b54SViktor Prutyanov     patch_mm_pfn_database(h, &local_err);
1282da91b54SViktor Prutyanov     if (local_err) {
1292da91b54SViktor Prutyanov         warn_report_err(local_err);
1302da91b54SViktor Prutyanov         local_err = NULL;
1312da91b54SViktor Prutyanov     }
1322da91b54SViktor Prutyanov     patch_bugcheck_data(h, &local_err);
1332da91b54SViktor Prutyanov     if (local_err) {
1342da91b54SViktor Prutyanov         warn_report_err(local_err);
1352da91b54SViktor Prutyanov     }
1362da91b54SViktor Prutyanov }
1372da91b54SViktor Prutyanov 
1382da91b54SViktor Prutyanov static void check_header(WinDumpHeader64 *h, Error **errp)
1392da91b54SViktor Prutyanov {
1402da91b54SViktor Prutyanov     const char Signature[] = "PAGE";
1412da91b54SViktor Prutyanov     const char ValidDump[] = "DU64";
1422da91b54SViktor Prutyanov 
1432da91b54SViktor Prutyanov     if (memcmp(h->Signature, Signature, sizeof(h->Signature))) {
1442da91b54SViktor Prutyanov         error_setg(errp, "win-dump: invalid header, expected '%.4s',"
1452da91b54SViktor Prutyanov                          " got '%.4s'", Signature, h->Signature);
1462da91b54SViktor Prutyanov         return;
1472da91b54SViktor Prutyanov     }
1482da91b54SViktor Prutyanov 
1492da91b54SViktor Prutyanov     if (memcmp(h->ValidDump, ValidDump, sizeof(h->ValidDump))) {
1502da91b54SViktor Prutyanov         error_setg(errp, "win-dump: invalid header, expected '%.4s',"
1512da91b54SViktor Prutyanov                          " got '%.4s'", ValidDump, h->ValidDump);
1522da91b54SViktor Prutyanov         return;
1532da91b54SViktor Prutyanov     }
1542da91b54SViktor Prutyanov }
1552da91b54SViktor Prutyanov 
1562da91b54SViktor Prutyanov static void check_kdbg(WinDumpHeader64 *h, Error **errp)
1572da91b54SViktor Prutyanov {
1582da91b54SViktor Prutyanov     const char OwnerTag[] = "KDBG";
1592da91b54SViktor Prutyanov     char read_OwnerTag[4];
1602ababfccSViktor Prutyanov     uint64_t KdDebuggerDataBlock = h->KdDebuggerDataBlock;
1612ababfccSViktor Prutyanov     bool try_fallback = true;
1622da91b54SViktor Prutyanov 
1632ababfccSViktor Prutyanov try_again:
1642da91b54SViktor Prutyanov     if (cpu_memory_rw_debug(first_cpu,
1652ababfccSViktor Prutyanov             KdDebuggerDataBlock + KDBG_OWNER_TAG_OFFSET64,
1662da91b54SViktor Prutyanov             (uint8_t *)&read_OwnerTag, sizeof(read_OwnerTag), 0)) {
1672da91b54SViktor Prutyanov         error_setg(errp, "win-dump: failed to read OwnerTag");
1682da91b54SViktor Prutyanov         return;
1692da91b54SViktor Prutyanov     }
1702da91b54SViktor Prutyanov 
1712da91b54SViktor Prutyanov     if (memcmp(read_OwnerTag, OwnerTag, sizeof(read_OwnerTag))) {
1722ababfccSViktor Prutyanov         if (try_fallback) {
1732ababfccSViktor Prutyanov             /*
1742ababfccSViktor Prutyanov              * If attempt to use original KDBG failed
1752ababfccSViktor Prutyanov              * (most likely because of its encryption),
1762ababfccSViktor Prutyanov              * we try to use KDBG obtained by guest driver.
1772ababfccSViktor Prutyanov              */
1782ababfccSViktor Prutyanov 
1792ababfccSViktor Prutyanov             KdDebuggerDataBlock = h->BugcheckParameter1;
1802ababfccSViktor Prutyanov             try_fallback = false;
1812ababfccSViktor Prutyanov             goto try_again;
1822ababfccSViktor Prutyanov         } else {
1832da91b54SViktor Prutyanov             error_setg(errp, "win-dump: invalid KDBG OwnerTag,"
1842ababfccSViktor Prutyanov                              " expected '%.4s', got '%.4s'",
1852da91b54SViktor Prutyanov                              OwnerTag, read_OwnerTag);
1862da91b54SViktor Prutyanov             return;
1872da91b54SViktor Prutyanov         }
1882da91b54SViktor Prutyanov     }
1892da91b54SViktor Prutyanov 
1902ababfccSViktor Prutyanov     h->KdDebuggerDataBlock = KdDebuggerDataBlock;
1912ababfccSViktor Prutyanov }
1922ababfccSViktor Prutyanov 
1932ad9b50fSViktor Prutyanov struct saved_context {
1942ad9b50fSViktor Prutyanov     WinContext ctx;
1952ad9b50fSViktor Prutyanov     uint64_t addr;
1962ad9b50fSViktor Prutyanov };
1972ad9b50fSViktor Prutyanov 
1982ad9b50fSViktor Prutyanov static void patch_and_save_context(WinDumpHeader64 *h,
1992ad9b50fSViktor Prutyanov                                    struct saved_context *saved_ctx,
2002ad9b50fSViktor Prutyanov                                    Error **errp)
2012ad9b50fSViktor Prutyanov {
2022ad9b50fSViktor Prutyanov     uint64_t KiProcessorBlock;
2032ad9b50fSViktor Prutyanov     uint16_t OffsetPrcbContext;
2042ad9b50fSViktor Prutyanov     CPUState *cpu;
2052ad9b50fSViktor Prutyanov     int i = 0;
2062ad9b50fSViktor Prutyanov 
2072ad9b50fSViktor Prutyanov     if (cpu_memory_rw_debug(first_cpu,
2082ad9b50fSViktor Prutyanov             h->KdDebuggerDataBlock + KDBG_KI_PROCESSOR_BLOCK_OFFSET64,
2092ad9b50fSViktor Prutyanov             (uint8_t *)&KiProcessorBlock, sizeof(KiProcessorBlock), 0)) {
2102ad9b50fSViktor Prutyanov         error_setg(errp, "win-dump: failed to read KiProcessorBlock");
2112ad9b50fSViktor Prutyanov         return;
2122ad9b50fSViktor Prutyanov     }
2132ad9b50fSViktor Prutyanov 
2142ad9b50fSViktor Prutyanov     if (cpu_memory_rw_debug(first_cpu,
2152ad9b50fSViktor Prutyanov             h->KdDebuggerDataBlock + KDBG_OFFSET_PRCB_CONTEXT_OFFSET64,
2162ad9b50fSViktor Prutyanov             (uint8_t *)&OffsetPrcbContext, sizeof(OffsetPrcbContext), 0)) {
2172ad9b50fSViktor Prutyanov         error_setg(errp, "win-dump: failed to read OffsetPrcbContext");
2182ad9b50fSViktor Prutyanov         return;
2192ad9b50fSViktor Prutyanov     }
2202ad9b50fSViktor Prutyanov 
2212ad9b50fSViktor Prutyanov     CPU_FOREACH(cpu) {
2222ad9b50fSViktor Prutyanov         X86CPU *x86_cpu = X86_CPU(cpu);
2232ad9b50fSViktor Prutyanov         CPUX86State *env = &x86_cpu->env;
2242ad9b50fSViktor Prutyanov         uint64_t Prcb;
2252ad9b50fSViktor Prutyanov         uint64_t Context;
2262ad9b50fSViktor Prutyanov         WinContext ctx;
2272ad9b50fSViktor Prutyanov 
2282ad9b50fSViktor Prutyanov         if (cpu_memory_rw_debug(first_cpu,
2292ad9b50fSViktor Prutyanov                 KiProcessorBlock + i * sizeof(uint64_t),
2302ad9b50fSViktor Prutyanov                 (uint8_t *)&Prcb, sizeof(Prcb), 0)) {
2312ad9b50fSViktor Prutyanov             error_setg(errp, "win-dump: failed to read"
2322ad9b50fSViktor Prutyanov                              " CPU #%d PRCB location", i);
2332ad9b50fSViktor Prutyanov             return;
2342ad9b50fSViktor Prutyanov         }
2352ad9b50fSViktor Prutyanov 
2362ad9b50fSViktor Prutyanov         if (cpu_memory_rw_debug(first_cpu,
2372ad9b50fSViktor Prutyanov                 Prcb + OffsetPrcbContext,
2382ad9b50fSViktor Prutyanov                 (uint8_t *)&Context, sizeof(Context), 0)) {
2392ad9b50fSViktor Prutyanov             error_setg(errp, "win-dump: failed to read"
2402ad9b50fSViktor Prutyanov                              " CPU #%d ContextFrame location", i);
2412ad9b50fSViktor Prutyanov             return;
2422ad9b50fSViktor Prutyanov         }
2432ad9b50fSViktor Prutyanov 
2442ad9b50fSViktor Prutyanov         saved_ctx[i].addr = Context;
2452ad9b50fSViktor Prutyanov 
2462ad9b50fSViktor Prutyanov         ctx = (WinContext){
2472ad9b50fSViktor Prutyanov             .ContextFlags = WIN_CTX_ALL,
2482ad9b50fSViktor Prutyanov             .MxCsr = env->mxcsr,
2492ad9b50fSViktor Prutyanov 
2502ad9b50fSViktor Prutyanov             .SegEs = env->segs[0].selector,
2512ad9b50fSViktor Prutyanov             .SegCs = env->segs[1].selector,
2522ad9b50fSViktor Prutyanov             .SegSs = env->segs[2].selector,
2532ad9b50fSViktor Prutyanov             .SegDs = env->segs[3].selector,
2542ad9b50fSViktor Prutyanov             .SegFs = env->segs[4].selector,
2552ad9b50fSViktor Prutyanov             .SegGs = env->segs[5].selector,
2562ad9b50fSViktor Prutyanov             .EFlags = cpu_compute_eflags(env),
2572ad9b50fSViktor Prutyanov 
2582ad9b50fSViktor Prutyanov             .Dr0 = env->dr[0],
2592ad9b50fSViktor Prutyanov             .Dr1 = env->dr[1],
2602ad9b50fSViktor Prutyanov             .Dr2 = env->dr[2],
2612ad9b50fSViktor Prutyanov             .Dr3 = env->dr[3],
2622ad9b50fSViktor Prutyanov             .Dr6 = env->dr[6],
2632ad9b50fSViktor Prutyanov             .Dr7 = env->dr[7],
2642ad9b50fSViktor Prutyanov 
2652ad9b50fSViktor Prutyanov             .Rax = env->regs[R_EAX],
2662ad9b50fSViktor Prutyanov             .Rbx = env->regs[R_EBX],
2672ad9b50fSViktor Prutyanov             .Rcx = env->regs[R_ECX],
2682ad9b50fSViktor Prutyanov             .Rdx = env->regs[R_EDX],
2692ad9b50fSViktor Prutyanov             .Rsp = env->regs[R_ESP],
2702ad9b50fSViktor Prutyanov             .Rbp = env->regs[R_EBP],
2712ad9b50fSViktor Prutyanov             .Rsi = env->regs[R_ESI],
2722ad9b50fSViktor Prutyanov             .Rdi = env->regs[R_EDI],
2732ad9b50fSViktor Prutyanov             .R8  = env->regs[8],
2742ad9b50fSViktor Prutyanov             .R9  = env->regs[9],
2752ad9b50fSViktor Prutyanov             .R10 = env->regs[10],
2762ad9b50fSViktor Prutyanov             .R11 = env->regs[11],
2772ad9b50fSViktor Prutyanov             .R12 = env->regs[12],
2782ad9b50fSViktor Prutyanov             .R13 = env->regs[13],
2792ad9b50fSViktor Prutyanov             .R14 = env->regs[14],
2802ad9b50fSViktor Prutyanov             .R15 = env->regs[15],
2812ad9b50fSViktor Prutyanov 
2822ad9b50fSViktor Prutyanov             .Rip = env->eip,
2832ad9b50fSViktor Prutyanov             .FltSave = {
2842ad9b50fSViktor Prutyanov                 .MxCsr = env->mxcsr,
2852ad9b50fSViktor Prutyanov             },
2862ad9b50fSViktor Prutyanov         };
2872ad9b50fSViktor Prutyanov 
2882ad9b50fSViktor Prutyanov         if (cpu_memory_rw_debug(first_cpu, Context,
2892ad9b50fSViktor Prutyanov                 (uint8_t *)&saved_ctx[i].ctx, sizeof(WinContext), 0)) {
2902ad9b50fSViktor Prutyanov             error_setg(errp, "win-dump: failed to save CPU #%d context", i);
2912ad9b50fSViktor Prutyanov             return;
2922ad9b50fSViktor Prutyanov         }
2932ad9b50fSViktor Prutyanov 
2942ad9b50fSViktor Prutyanov         if (cpu_memory_rw_debug(first_cpu, Context,
2952ad9b50fSViktor Prutyanov                 (uint8_t *)&ctx, sizeof(WinContext), 1)) {
2962ad9b50fSViktor Prutyanov             error_setg(errp, "win-dump: failed to write CPU #%d context", i);
2972ad9b50fSViktor Prutyanov             return;
2982ad9b50fSViktor Prutyanov         }
2992ad9b50fSViktor Prutyanov 
3002ad9b50fSViktor Prutyanov         i++;
3012ad9b50fSViktor Prutyanov     }
3022ad9b50fSViktor Prutyanov }
3032ad9b50fSViktor Prutyanov 
3042ad9b50fSViktor Prutyanov static void restore_context(WinDumpHeader64 *h,
3052ad9b50fSViktor Prutyanov                             struct saved_context *saved_ctx)
3062ad9b50fSViktor Prutyanov {
3072ad9b50fSViktor Prutyanov     int i;
3082ad9b50fSViktor Prutyanov     Error *err = NULL;
3092ad9b50fSViktor Prutyanov 
3102ad9b50fSViktor Prutyanov     for (i = 0; i < h->NumberProcessors; i++) {
3112ad9b50fSViktor Prutyanov         if (cpu_memory_rw_debug(first_cpu, saved_ctx[i].addr,
3122ad9b50fSViktor Prutyanov                 (uint8_t *)&saved_ctx[i].ctx, sizeof(WinContext), 1)) {
3132ad9b50fSViktor Prutyanov             error_setg(&err, "win-dump: failed to restore CPU #%d context", i);
3142ad9b50fSViktor Prutyanov             warn_report_err(err);
3152ad9b50fSViktor Prutyanov         }
3162ad9b50fSViktor Prutyanov     }
3172ad9b50fSViktor Prutyanov }
3182ad9b50fSViktor Prutyanov 
3192da91b54SViktor Prutyanov void create_win_dump(DumpState *s, Error **errp)
3202da91b54SViktor Prutyanov {
3212da91b54SViktor Prutyanov     WinDumpHeader64 *h = (WinDumpHeader64 *)(s->guest_note +
3222da91b54SViktor Prutyanov             VMCOREINFO_ELF_NOTE_HDR_SIZE);
32392d1b3d5SViktor Prutyanov     X86CPU *first_x86_cpu = X86_CPU(first_cpu);
32492d1b3d5SViktor Prutyanov     uint64_t saved_cr3 = first_x86_cpu->env.cr[3];
3252ad9b50fSViktor Prutyanov     struct saved_context *saved_ctx = NULL;
3262da91b54SViktor Prutyanov     Error *local_err = NULL;
3272da91b54SViktor Prutyanov 
3282da91b54SViktor Prutyanov     if (s->guest_note_size != sizeof(WinDumpHeader64) +
3292da91b54SViktor Prutyanov             VMCOREINFO_ELF_NOTE_HDR_SIZE) {
3302da91b54SViktor Prutyanov         error_setg(errp, "win-dump: invalid vmcoreinfo note size");
3312da91b54SViktor Prutyanov         return;
3322da91b54SViktor Prutyanov     }
3332da91b54SViktor Prutyanov 
3342da91b54SViktor Prutyanov     check_header(h, &local_err);
3352da91b54SViktor Prutyanov     if (local_err) {
3362da91b54SViktor Prutyanov         error_propagate(errp, local_err);
3372da91b54SViktor Prutyanov         return;
3382da91b54SViktor Prutyanov     }
3392da91b54SViktor Prutyanov 
34092d1b3d5SViktor Prutyanov     /*
34192d1b3d5SViktor Prutyanov      * Further access to kernel structures by virtual addresses
34292d1b3d5SViktor Prutyanov      * should be made from system context.
34392d1b3d5SViktor Prutyanov      */
34492d1b3d5SViktor Prutyanov 
34592d1b3d5SViktor Prutyanov     first_x86_cpu->env.cr[3] = h->DirectoryTableBase;
34692d1b3d5SViktor Prutyanov 
3472da91b54SViktor Prutyanov     check_kdbg(h, &local_err);
3482da91b54SViktor Prutyanov     if (local_err) {
3492da91b54SViktor Prutyanov         error_propagate(errp, local_err);
35092d1b3d5SViktor Prutyanov         goto out_cr3;
3512da91b54SViktor Prutyanov     }
3522da91b54SViktor Prutyanov 
3532da91b54SViktor Prutyanov     patch_header(h);
3542da91b54SViktor Prutyanov 
3552ad9b50fSViktor Prutyanov     saved_ctx = g_new(struct saved_context, h->NumberProcessors);
3562ad9b50fSViktor Prutyanov 
3572ad9b50fSViktor Prutyanov     /*
3582ad9b50fSViktor Prutyanov      * Always patch context because there is no way
3592ad9b50fSViktor Prutyanov      * to determine if the system-saved context is valid
3602ad9b50fSViktor Prutyanov      */
3612ad9b50fSViktor Prutyanov 
3622ad9b50fSViktor Prutyanov     patch_and_save_context(h, saved_ctx, &local_err);
3632ad9b50fSViktor Prutyanov     if (local_err) {
3642ad9b50fSViktor Prutyanov         error_propagate(errp, local_err);
3652ad9b50fSViktor Prutyanov         goto out_free;
3662ad9b50fSViktor Prutyanov     }
3672ad9b50fSViktor Prutyanov 
3682da91b54SViktor Prutyanov     s->total_size = h->RequiredDumpSpace;
3692da91b54SViktor Prutyanov 
3702da91b54SViktor Prutyanov     s->written_size = qemu_write_full(s->fd, h, sizeof(*h));
3712da91b54SViktor Prutyanov     if (s->written_size != sizeof(*h)) {
3722da91b54SViktor Prutyanov         error_setg(errp, QERR_IO_ERROR);
3732ad9b50fSViktor Prutyanov         goto out_restore;
3742da91b54SViktor Prutyanov     }
3752da91b54SViktor Prutyanov 
3762da91b54SViktor Prutyanov     write_runs(s, h, &local_err);
3772da91b54SViktor Prutyanov     if (local_err) {
3782da91b54SViktor Prutyanov         error_propagate(errp, local_err);
3792ad9b50fSViktor Prutyanov         goto out_restore;
3802da91b54SViktor Prutyanov     }
38192d1b3d5SViktor Prutyanov 
3822ad9b50fSViktor Prutyanov out_restore:
3832ad9b50fSViktor Prutyanov     restore_context(h, saved_ctx);
3842ad9b50fSViktor Prutyanov out_free:
3852ad9b50fSViktor Prutyanov     g_free(saved_ctx);
38692d1b3d5SViktor Prutyanov out_cr3:
38792d1b3d5SViktor Prutyanov     first_x86_cpu->env.cr[3] = saved_cr3;
38892d1b3d5SViktor Prutyanov 
38992d1b3d5SViktor Prutyanov     return;
3902da91b54SViktor Prutyanov }
391