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