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