13fa2d384SViktor Prutyanov /* 23fa2d384SViktor Prutyanov * Copyright (c) 2018 Virtuozzo International GmbH 33fa2d384SViktor Prutyanov * 43fa2d384SViktor Prutyanov * This work is licensed under the terms of the GNU GPL, version 2 or later. 53fa2d384SViktor Prutyanov * 63fa2d384SViktor Prutyanov */ 73fa2d384SViktor Prutyanov 83fa2d384SViktor Prutyanov #include "qemu/osdep.h" 9*f024f503SAkihiko Odaki #include "qemu/bitops.h" 10bbfff196SMarkus Armbruster 113fa2d384SViktor Prutyanov #include "err.h" 123fa2d384SViktor Prutyanov #include "addrspace.h" 133fa2d384SViktor Prutyanov #include "pe.h" 143fa2d384SViktor Prutyanov #include "pdb.h" 153fa2d384SViktor Prutyanov #include "kdbg.h" 163fa2d384SViktor Prutyanov #include "download.h" 173fa2d384SViktor Prutyanov #include "qemu/win_dump_defs.h" 183fa2d384SViktor Prutyanov 193fa2d384SViktor Prutyanov #define SYM_URL_BASE "https://msdl.microsoft.com/download/symbols/" 203fa2d384SViktor Prutyanov #define PDB_NAME "ntkrnlmp.pdb" 21d399d6b1SViktor Prutyanov #define PE_NAME "ntoskrnl.exe" 223fa2d384SViktor Prutyanov 233fa2d384SViktor Prutyanov #define INITIAL_MXCSR 0x1f80 249b7dcd8fSViktor Prutyanov #define MAX_NUMBER_OF_RUNS 42 253fa2d384SViktor Prutyanov 263fa2d384SViktor Prutyanov typedef struct idt_desc { 273fa2d384SViktor Prutyanov uint16_t offset1; /* offset bits 0..15 */ 283fa2d384SViktor Prutyanov uint16_t selector; 293fa2d384SViktor Prutyanov uint8_t ist; 303fa2d384SViktor Prutyanov uint8_t type_attr; 313fa2d384SViktor Prutyanov uint16_t offset2; /* offset bits 16..31 */ 323fa2d384SViktor Prutyanov uint32_t offset3; /* offset bits 32..63 */ 333fa2d384SViktor Prutyanov uint32_t rsrvd; 343fa2d384SViktor Prutyanov } __attribute__ ((packed)) idt_desc_t; 353fa2d384SViktor Prutyanov 363fa2d384SViktor Prutyanov static uint64_t idt_desc_addr(idt_desc_t desc) 373fa2d384SViktor Prutyanov { 383fa2d384SViktor Prutyanov return (uint64_t)desc.offset1 | ((uint64_t)desc.offset2 << 16) | 393fa2d384SViktor Prutyanov ((uint64_t)desc.offset3 << 32); 403fa2d384SViktor Prutyanov } 413fa2d384SViktor Prutyanov 423fa2d384SViktor Prutyanov static const uint64_t SharedUserData = 0xfffff78000000000; 433fa2d384SViktor Prutyanov 443fa2d384SViktor Prutyanov #define KUSD_OFFSET_SUITE_MASK 0x2d0 453fa2d384SViktor Prutyanov #define KUSD_OFFSET_PRODUCT_TYPE 0x264 463fa2d384SViktor Prutyanov 473fa2d384SViktor Prutyanov #define SYM_RESOLVE(base, r, s) ((s = pdb_resolve(base, r, #s)),\ 486ec6e988SViktor Prutyanov s ? printf(#s" = 0x%016"PRIx64"\n", s) :\ 496ec6e988SViktor Prutyanov eprintf("Failed to resolve "#s"\n"), s) 503fa2d384SViktor Prutyanov 513fa2d384SViktor Prutyanov /* 523fa2d384SViktor Prutyanov * Decoding algorithm can be found in Volatility project 533fa2d384SViktor Prutyanov */ 543fa2d384SViktor Prutyanov static void kdbg_decode(uint64_t *dst, uint64_t *src, size_t size, 553fa2d384SViktor Prutyanov uint64_t kwn, uint64_t kwa, uint64_t kdbe) 563fa2d384SViktor Prutyanov { 573fa2d384SViktor Prutyanov size_t i; 583fa2d384SViktor Prutyanov assert(size % sizeof(uint64_t) == 0); 593fa2d384SViktor Prutyanov for (i = 0; i < size / sizeof(uint64_t); i++) { 603fa2d384SViktor Prutyanov uint64_t block; 613fa2d384SViktor Prutyanov 623fa2d384SViktor Prutyanov block = src[i]; 63*f024f503SAkihiko Odaki block = rol64(block ^ kwn, kwn); 643fa2d384SViktor Prutyanov block = __builtin_bswap64(block ^ kdbe) ^ kwa; 653fa2d384SViktor Prutyanov dst[i] = block; 663fa2d384SViktor Prutyanov } 673fa2d384SViktor Prutyanov } 683fa2d384SViktor Prutyanov 693fa2d384SViktor Prutyanov static KDDEBUGGER_DATA64 *get_kdbg(uint64_t KernBase, struct pdb_reader *pdb, 703fa2d384SViktor Prutyanov struct va_space *vs, uint64_t KdDebuggerDataBlock) 713fa2d384SViktor Prutyanov { 723fa2d384SViktor Prutyanov const char OwnerTag[4] = "KDBG"; 733fa2d384SViktor Prutyanov KDDEBUGGER_DATA64 *kdbg = NULL; 743fa2d384SViktor Prutyanov DBGKD_DEBUG_DATA_HEADER64 kdbg_hdr; 753fa2d384SViktor Prutyanov bool decode = false; 763fa2d384SViktor Prutyanov uint64_t kwn, kwa, KdpDataBlockEncoded; 773fa2d384SViktor Prutyanov 78a15f9749SAkihiko Odaki if (!va_space_rw(vs, 793fa2d384SViktor Prutyanov KdDebuggerDataBlock + offsetof(KDDEBUGGER_DATA64, Header), 803fa2d384SViktor Prutyanov &kdbg_hdr, sizeof(kdbg_hdr), 0)) { 813fa2d384SViktor Prutyanov eprintf("Failed to extract KDBG header\n"); 823fa2d384SViktor Prutyanov return NULL; 833fa2d384SViktor Prutyanov } 843fa2d384SViktor Prutyanov 853fa2d384SViktor Prutyanov if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) { 863fa2d384SViktor Prutyanov uint64_t KiWaitNever, KiWaitAlways; 873fa2d384SViktor Prutyanov 883fa2d384SViktor Prutyanov decode = true; 893fa2d384SViktor Prutyanov 903fa2d384SViktor Prutyanov if (!SYM_RESOLVE(KernBase, pdb, KiWaitNever) || 913fa2d384SViktor Prutyanov !SYM_RESOLVE(KernBase, pdb, KiWaitAlways) || 923fa2d384SViktor Prutyanov !SYM_RESOLVE(KernBase, pdb, KdpDataBlockEncoded)) { 933fa2d384SViktor Prutyanov return NULL; 943fa2d384SViktor Prutyanov } 953fa2d384SViktor Prutyanov 96a15f9749SAkihiko Odaki if (!va_space_rw(vs, KiWaitNever, &kwn, sizeof(kwn), 0) || 97a15f9749SAkihiko Odaki !va_space_rw(vs, KiWaitAlways, &kwa, sizeof(kwa), 0)) { 983fa2d384SViktor Prutyanov return NULL; 993fa2d384SViktor Prutyanov } 1003fa2d384SViktor Prutyanov 1016ec6e988SViktor Prutyanov printf("[KiWaitNever] = 0x%016"PRIx64"\n", kwn); 1026ec6e988SViktor Prutyanov printf("[KiWaitAlways] = 0x%016"PRIx64"\n", kwa); 1033fa2d384SViktor Prutyanov 1043fa2d384SViktor Prutyanov /* 1053fa2d384SViktor Prutyanov * If KDBG header can be decoded, KDBG size is available 1063fa2d384SViktor Prutyanov * and entire KDBG can be decoded. 1073fa2d384SViktor Prutyanov */ 1083fa2d384SViktor Prutyanov printf("Decoding KDBG header...\n"); 1093fa2d384SViktor Prutyanov kdbg_decode((uint64_t *)&kdbg_hdr, (uint64_t *)&kdbg_hdr, 1103fa2d384SViktor Prutyanov sizeof(kdbg_hdr), kwn, kwa, KdpDataBlockEncoded); 1113fa2d384SViktor Prutyanov 1123fa2d384SViktor Prutyanov printf("Owner tag is \'%.4s\'\n", (char *)&kdbg_hdr.OwnerTag); 1133fa2d384SViktor Prutyanov if (memcmp(&kdbg_hdr.OwnerTag, OwnerTag, sizeof(OwnerTag))) { 1143fa2d384SViktor Prutyanov eprintf("Failed to decode KDBG header\n"); 1153fa2d384SViktor Prutyanov return NULL; 1163fa2d384SViktor Prutyanov } 1173fa2d384SViktor Prutyanov } 1183fa2d384SViktor Prutyanov 1192a052b4eSSuraj Shirvankar kdbg = g_malloc(kdbg_hdr.Size); 1203fa2d384SViktor Prutyanov 121a15f9749SAkihiko Odaki if (!va_space_rw(vs, KdDebuggerDataBlock, kdbg, kdbg_hdr.Size, 0)) { 1223fa2d384SViktor Prutyanov eprintf("Failed to extract entire KDBG\n"); 1232a052b4eSSuraj Shirvankar g_free(kdbg); 1243fa2d384SViktor Prutyanov return NULL; 1253fa2d384SViktor Prutyanov } 1263fa2d384SViktor Prutyanov 1273fa2d384SViktor Prutyanov if (!decode) { 1283fa2d384SViktor Prutyanov return kdbg; 1293fa2d384SViktor Prutyanov } 1303fa2d384SViktor Prutyanov 1313fa2d384SViktor Prutyanov printf("Decoding KdDebuggerDataBlock...\n"); 1323fa2d384SViktor Prutyanov kdbg_decode((uint64_t *)kdbg, (uint64_t *)kdbg, kdbg_hdr.Size, 1333fa2d384SViktor Prutyanov kwn, kwa, KdpDataBlockEncoded); 1343fa2d384SViktor Prutyanov 1353fa2d384SViktor Prutyanov va_space_rw(vs, KdDebuggerDataBlock, kdbg, kdbg_hdr.Size, 1); 1363fa2d384SViktor Prutyanov 1373fa2d384SViktor Prutyanov return kdbg; 1383fa2d384SViktor Prutyanov } 1393fa2d384SViktor Prutyanov 140a64b4e17SViktor Prutyanov static void win_context_init_from_qemu_cpu_state(WinContext64 *ctx, 1413fa2d384SViktor Prutyanov QEMUCPUState *s) 1423fa2d384SViktor Prutyanov { 143a64b4e17SViktor Prutyanov WinContext64 win_ctx = (WinContext64){ 1443fa2d384SViktor Prutyanov .ContextFlags = WIN_CTX_X64 | WIN_CTX_INT | WIN_CTX_SEG | WIN_CTX_CTL, 1453fa2d384SViktor Prutyanov .MxCsr = INITIAL_MXCSR, 1463fa2d384SViktor Prutyanov 1473fa2d384SViktor Prutyanov .SegCs = s->cs.selector, 1483fa2d384SViktor Prutyanov .SegSs = s->ss.selector, 1493fa2d384SViktor Prutyanov .SegDs = s->ds.selector, 1503fa2d384SViktor Prutyanov .SegEs = s->es.selector, 1513fa2d384SViktor Prutyanov .SegFs = s->fs.selector, 1523fa2d384SViktor Prutyanov .SegGs = s->gs.selector, 1533fa2d384SViktor Prutyanov .EFlags = (uint32_t)s->rflags, 1543fa2d384SViktor Prutyanov 1553fa2d384SViktor Prutyanov .Rax = s->rax, 1563fa2d384SViktor Prutyanov .Rbx = s->rbx, 1573fa2d384SViktor Prutyanov .Rcx = s->rcx, 1583fa2d384SViktor Prutyanov .Rdx = s->rdx, 1593fa2d384SViktor Prutyanov .Rsp = s->rsp, 1603fa2d384SViktor Prutyanov .Rbp = s->rbp, 1613fa2d384SViktor Prutyanov .Rsi = s->rsi, 1623fa2d384SViktor Prutyanov .Rdi = s->rdi, 1633fa2d384SViktor Prutyanov .R8 = s->r8, 1643fa2d384SViktor Prutyanov .R9 = s->r9, 1653fa2d384SViktor Prutyanov .R10 = s->r10, 1663fa2d384SViktor Prutyanov .R11 = s->r11, 1673fa2d384SViktor Prutyanov .R12 = s->r12, 1683fa2d384SViktor Prutyanov .R13 = s->r13, 1693fa2d384SViktor Prutyanov .R14 = s->r14, 1703fa2d384SViktor Prutyanov .R15 = s->r15, 1713fa2d384SViktor Prutyanov 1723fa2d384SViktor Prutyanov .Rip = s->rip, 1733fa2d384SViktor Prutyanov .FltSave = { 1743fa2d384SViktor Prutyanov .MxCsr = INITIAL_MXCSR, 1753fa2d384SViktor Prutyanov }, 1763fa2d384SViktor Prutyanov }; 1773fa2d384SViktor Prutyanov 1783fa2d384SViktor Prutyanov *ctx = win_ctx; 1793fa2d384SViktor Prutyanov } 1803fa2d384SViktor Prutyanov 1813fa2d384SViktor Prutyanov /* 1823fa2d384SViktor Prutyanov * Finds paging-structure hierarchy base, 1833fa2d384SViktor Prutyanov * if previously set doesn't give access to kernel structures 1843fa2d384SViktor Prutyanov */ 185fbc3d7d2SAkihiko Odaki static bool fix_dtb(struct va_space *vs, QEMU_Elf *qe) 1863fa2d384SViktor Prutyanov { 1873fa2d384SViktor Prutyanov /* 1883fa2d384SViktor Prutyanov * Firstly, test previously set DTB. 1893fa2d384SViktor Prutyanov */ 1903fa2d384SViktor Prutyanov if (va_space_resolve(vs, SharedUserData)) { 191fbc3d7d2SAkihiko Odaki return true; 1923fa2d384SViktor Prutyanov } 1933fa2d384SViktor Prutyanov 1943fa2d384SViktor Prutyanov /* 1953fa2d384SViktor Prutyanov * Secondly, find CPU which run system task. 1963fa2d384SViktor Prutyanov */ 1973fa2d384SViktor Prutyanov size_t i; 1983fa2d384SViktor Prutyanov for (i = 0; i < qe->state_nr; i++) { 1993fa2d384SViktor Prutyanov QEMUCPUState *s = qe->state[i]; 2003fa2d384SViktor Prutyanov 2013fa2d384SViktor Prutyanov if (is_system(s)) { 2023fa2d384SViktor Prutyanov va_space_set_dtb(vs, s->cr[3]); 2036ec6e988SViktor Prutyanov printf("DTB 0x%016"PRIx64" has been found from CPU #%zu" 2043fa2d384SViktor Prutyanov " as system task CR3\n", vs->dtb, i); 205fbc3d7d2SAkihiko Odaki return va_space_resolve(vs, SharedUserData); 2063fa2d384SViktor Prutyanov } 2073fa2d384SViktor Prutyanov } 2083fa2d384SViktor Prutyanov 2093fa2d384SViktor Prutyanov /* 2103fa2d384SViktor Prutyanov * Thirdly, use KERNEL_GS_BASE from CPU #0 as PRCB address and 2113fa2d384SViktor Prutyanov * CR3 as [Prcb+0x7000] 2123fa2d384SViktor Prutyanov */ 2133fa2d384SViktor Prutyanov if (qe->has_kernel_gs_base) { 2143fa2d384SViktor Prutyanov QEMUCPUState *s = qe->state[0]; 2153fa2d384SViktor Prutyanov uint64_t Prcb = s->kernel_gs_base; 2163fa2d384SViktor Prutyanov uint64_t *cr3 = va_space_resolve(vs, Prcb + 0x7000); 2173fa2d384SViktor Prutyanov 2183fa2d384SViktor Prutyanov if (!cr3) { 219fbc3d7d2SAkihiko Odaki return false; 2203fa2d384SViktor Prutyanov } 2213fa2d384SViktor Prutyanov 2223fa2d384SViktor Prutyanov va_space_set_dtb(vs, *cr3); 2236ec6e988SViktor Prutyanov printf("DirectoryTableBase = 0x%016"PRIx64" has been found from CPU #0" 2243fa2d384SViktor Prutyanov " as interrupt handling CR3\n", vs->dtb); 225fbc3d7d2SAkihiko Odaki return va_space_resolve(vs, SharedUserData); 2263fa2d384SViktor Prutyanov } 2273fa2d384SViktor Prutyanov 228fbc3d7d2SAkihiko Odaki return true; 2293fa2d384SViktor Prutyanov } 2303fa2d384SViktor Prutyanov 2319b7dcd8fSViktor Prutyanov static void try_merge_runs(struct pa_space *ps, 2329b7dcd8fSViktor Prutyanov WinDumpPhyMemDesc64 *PhysicalMemoryBlock) 2339b7dcd8fSViktor Prutyanov { 2349b7dcd8fSViktor Prutyanov unsigned int merge_cnt = 0, run_idx = 0; 2359b7dcd8fSViktor Prutyanov 2369b7dcd8fSViktor Prutyanov PhysicalMemoryBlock->NumberOfRuns = 0; 2379b7dcd8fSViktor Prutyanov 2389b7dcd8fSViktor Prutyanov for (size_t idx = 0; idx < ps->block_nr; idx++) { 2399b7dcd8fSViktor Prutyanov struct pa_block *blk = ps->block + idx; 2409b7dcd8fSViktor Prutyanov struct pa_block *next = blk + 1; 2419b7dcd8fSViktor Prutyanov 2429b7dcd8fSViktor Prutyanov PhysicalMemoryBlock->NumberOfPages += blk->size / ELF2DMP_PAGE_SIZE; 2439b7dcd8fSViktor Prutyanov 2449b7dcd8fSViktor Prutyanov if (idx + 1 != ps->block_nr && blk->paddr + blk->size == next->paddr) { 2459b7dcd8fSViktor Prutyanov printf("Block #%zu 0x%"PRIx64"+:0x%"PRIx64" and %u previous will be" 2469b7dcd8fSViktor Prutyanov " merged\n", idx, blk->paddr, blk->size, merge_cnt); 2479b7dcd8fSViktor Prutyanov merge_cnt++; 2489b7dcd8fSViktor Prutyanov } else { 2499b7dcd8fSViktor Prutyanov struct pa_block *first_merged = blk - merge_cnt; 2509b7dcd8fSViktor Prutyanov 2519b7dcd8fSViktor Prutyanov printf("Block #%zu 0x%"PRIx64"+:0x%"PRIx64" and %u previous will be" 2529b7dcd8fSViktor Prutyanov " merged to 0x%"PRIx64"+:0x%"PRIx64" (run #%u)\n", 2539b7dcd8fSViktor Prutyanov idx, blk->paddr, blk->size, merge_cnt, first_merged->paddr, 2549b7dcd8fSViktor Prutyanov blk->paddr + blk->size - first_merged->paddr, run_idx); 2559b7dcd8fSViktor Prutyanov PhysicalMemoryBlock->Run[run_idx] = (WinDumpPhyMemRun64) { 2569b7dcd8fSViktor Prutyanov .BasePage = first_merged->paddr / ELF2DMP_PAGE_SIZE, 2579b7dcd8fSViktor Prutyanov .PageCount = (blk->paddr + blk->size - first_merged->paddr) / 2589b7dcd8fSViktor Prutyanov ELF2DMP_PAGE_SIZE, 2599b7dcd8fSViktor Prutyanov }; 2609b7dcd8fSViktor Prutyanov PhysicalMemoryBlock->NumberOfRuns++; 2619b7dcd8fSViktor Prutyanov run_idx++; 2629b7dcd8fSViktor Prutyanov merge_cnt = 0; 2639b7dcd8fSViktor Prutyanov } 2649b7dcd8fSViktor Prutyanov } 2659b7dcd8fSViktor Prutyanov } 2669b7dcd8fSViktor Prutyanov 267fbc3d7d2SAkihiko Odaki static bool fill_header(WinDumpHeader64 *hdr, struct pa_space *ps, 2683fa2d384SViktor Prutyanov struct va_space *vs, uint64_t KdDebuggerDataBlock, 269fbc3d7d2SAkihiko Odaki KDDEBUGGER_DATA64 *kdbg, uint64_t KdVersionBlock, 270fbc3d7d2SAkihiko Odaki int nr_cpus) 2713fa2d384SViktor Prutyanov { 2723fa2d384SViktor Prutyanov uint32_t *suite_mask = va_space_resolve(vs, SharedUserData + 2733fa2d384SViktor Prutyanov KUSD_OFFSET_SUITE_MASK); 2743fa2d384SViktor Prutyanov int32_t *product_type = va_space_resolve(vs, SharedUserData + 2753fa2d384SViktor Prutyanov KUSD_OFFSET_PRODUCT_TYPE); 2763fa2d384SViktor Prutyanov DBGKD_GET_VERSION64 kvb; 2773fa2d384SViktor Prutyanov WinDumpHeader64 h; 2783fa2d384SViktor Prutyanov 2792d0fc797SJiaxun Yang QEMU_BUILD_BUG_ON(KUSD_OFFSET_SUITE_MASK >= ELF2DMP_PAGE_SIZE); 2802d0fc797SJiaxun Yang QEMU_BUILD_BUG_ON(KUSD_OFFSET_PRODUCT_TYPE >= ELF2DMP_PAGE_SIZE); 2813fa2d384SViktor Prutyanov 2823fa2d384SViktor Prutyanov if (!suite_mask || !product_type) { 283fbc3d7d2SAkihiko Odaki return false; 2843fa2d384SViktor Prutyanov } 2853fa2d384SViktor Prutyanov 286a15f9749SAkihiko Odaki if (!va_space_rw(vs, KdVersionBlock, &kvb, sizeof(kvb), 0)) { 2873fa2d384SViktor Prutyanov eprintf("Failed to extract KdVersionBlock\n"); 288fbc3d7d2SAkihiko Odaki return false; 2893fa2d384SViktor Prutyanov } 2903fa2d384SViktor Prutyanov 2913fa2d384SViktor Prutyanov h = (WinDumpHeader64) { 2923fa2d384SViktor Prutyanov .Signature = "PAGE", 2933fa2d384SViktor Prutyanov .ValidDump = "DU64", 2943fa2d384SViktor Prutyanov .MajorVersion = kvb.MajorVersion, 2953fa2d384SViktor Prutyanov .MinorVersion = kvb.MinorVersion, 2963fa2d384SViktor Prutyanov .DirectoryTableBase = vs->dtb, 2973fa2d384SViktor Prutyanov .PfnDatabase = kdbg->MmPfnDatabase, 2983fa2d384SViktor Prutyanov .PsLoadedModuleList = kdbg->PsLoadedModuleList, 2993fa2d384SViktor Prutyanov .PsActiveProcessHead = kdbg->PsActiveProcessHead, 3003fa2d384SViktor Prutyanov .MachineImageType = kvb.MachineType, 3013fa2d384SViktor Prutyanov .NumberProcessors = nr_cpus, 3023fa2d384SViktor Prutyanov .BugcheckCode = LIVE_SYSTEM_DUMP, 3033fa2d384SViktor Prutyanov .KdDebuggerDataBlock = KdDebuggerDataBlock, 3043fa2d384SViktor Prutyanov .DumpType = 1, 3053fa2d384SViktor Prutyanov .Comment = "Hello from elf2dmp!", 3063fa2d384SViktor Prutyanov .SuiteMask = *suite_mask, 3073fa2d384SViktor Prutyanov .ProductType = *product_type, 3083fa2d384SViktor Prutyanov .SecondaryDataState = kvb.KdSecondaryVersion, 3093fa2d384SViktor Prutyanov .PhysicalMemoryBlock = (WinDumpPhyMemDesc64) { 3103fa2d384SViktor Prutyanov .NumberOfRuns = ps->block_nr, 3113fa2d384SViktor Prutyanov }, 3123fa2d384SViktor Prutyanov .RequiredDumpSpace = sizeof(h), 3133fa2d384SViktor Prutyanov }; 3143fa2d384SViktor Prutyanov 3159b7dcd8fSViktor Prutyanov if (h.PhysicalMemoryBlock.NumberOfRuns <= MAX_NUMBER_OF_RUNS) { 3169b7dcd8fSViktor Prutyanov for (size_t idx = 0; idx < ps->block_nr; idx++) { 31705adc48eSViktor Prutyanov h.PhysicalMemoryBlock.NumberOfPages += 3189b7dcd8fSViktor Prutyanov ps->block[idx].size / ELF2DMP_PAGE_SIZE; 3199b7dcd8fSViktor Prutyanov h.PhysicalMemoryBlock.Run[idx] = (WinDumpPhyMemRun64) { 3209b7dcd8fSViktor Prutyanov .BasePage = ps->block[idx].paddr / ELF2DMP_PAGE_SIZE, 3219b7dcd8fSViktor Prutyanov .PageCount = ps->block[idx].size / ELF2DMP_PAGE_SIZE, 3223fa2d384SViktor Prutyanov }; 3233fa2d384SViktor Prutyanov } 3249b7dcd8fSViktor Prutyanov } else { 3259b7dcd8fSViktor Prutyanov try_merge_runs(ps, &h.PhysicalMemoryBlock); 3269b7dcd8fSViktor Prutyanov } 3273fa2d384SViktor Prutyanov 32805adc48eSViktor Prutyanov h.RequiredDumpSpace += 32905adc48eSViktor Prutyanov h.PhysicalMemoryBlock.NumberOfPages << ELF2DMP_PAGE_BITS; 3303fa2d384SViktor Prutyanov 3313fa2d384SViktor Prutyanov *hdr = h; 3323fa2d384SViktor Prutyanov 333fbc3d7d2SAkihiko Odaki return true; 3343fa2d384SViktor Prutyanov } 3353fa2d384SViktor Prutyanov 33687157ef3SAkihiko Odaki /* 33787157ef3SAkihiko Odaki * fill_context() continues even if it fails to fill contexts of some CPUs. 33887157ef3SAkihiko Odaki * A dump may still contain valuable information even if it lacks contexts of 33987157ef3SAkihiko Odaki * some CPUs due to dump corruption or a failure before starting CPUs. 34087157ef3SAkihiko Odaki */ 34187157ef3SAkihiko Odaki static void fill_context(KDDEBUGGER_DATA64 *kdbg, 3423fa2d384SViktor Prutyanov struct va_space *vs, QEMU_Elf *qe) 3433fa2d384SViktor Prutyanov { 3443fa2d384SViktor Prutyanov int i; 34505adc48eSViktor Prutyanov 3463fa2d384SViktor Prutyanov for (i = 0; i < qe->state_nr; i++) { 3473fa2d384SViktor Prutyanov uint64_t Prcb; 3483fa2d384SViktor Prutyanov uint64_t Context; 349a64b4e17SViktor Prutyanov WinContext64 ctx; 3503fa2d384SViktor Prutyanov QEMUCPUState *s = qe->state[i]; 3513fa2d384SViktor Prutyanov 352a15f9749SAkihiko Odaki if (!va_space_rw(vs, kdbg->KiProcessorBlock + sizeof(Prcb) * i, 3533fa2d384SViktor Prutyanov &Prcb, sizeof(Prcb), 0)) { 3543fa2d384SViktor Prutyanov eprintf("Failed to read CPU #%d PRCB location\n", i); 35587157ef3SAkihiko Odaki continue; 3563fa2d384SViktor Prutyanov } 3573fa2d384SViktor Prutyanov 358548b8edcSAkihiko Odaki if (!Prcb) { 359548b8edcSAkihiko Odaki eprintf("Context for CPU #%d is missing\n", i); 360548b8edcSAkihiko Odaki continue; 361548b8edcSAkihiko Odaki } 362548b8edcSAkihiko Odaki 363a15f9749SAkihiko Odaki if (!va_space_rw(vs, Prcb + kdbg->OffsetPrcbContext, 3643fa2d384SViktor Prutyanov &Context, sizeof(Context), 0)) { 3653fa2d384SViktor Prutyanov eprintf("Failed to read CPU #%d ContextFrame location\n", i); 36687157ef3SAkihiko Odaki continue; 3673fa2d384SViktor Prutyanov } 3683fa2d384SViktor Prutyanov 3693fa2d384SViktor Prutyanov printf("Filling context for CPU #%d...\n", i); 3703fa2d384SViktor Prutyanov win_context_init_from_qemu_cpu_state(&ctx, s); 3713fa2d384SViktor Prutyanov 372a15f9749SAkihiko Odaki if (!va_space_rw(vs, Context, &ctx, sizeof(ctx), 1)) { 3733fa2d384SViktor Prutyanov eprintf("Failed to fill CPU #%d context\n", i); 37487157ef3SAkihiko Odaki continue; 3753fa2d384SViktor Prutyanov } 3763fa2d384SViktor Prutyanov } 3773fa2d384SViktor Prutyanov } 3783fa2d384SViktor Prutyanov 379fbc3d7d2SAkihiko Odaki static bool pe_get_data_dir_entry(uint64_t base, void *start_addr, int idx, 38006ac60b7SViktor Prutyanov void *entry, size_t size, struct va_space *vs) 38106ac60b7SViktor Prutyanov { 38206ac60b7SViktor Prutyanov const char e_magic[2] = "MZ"; 38306ac60b7SViktor Prutyanov const char Signature[4] = "PE\0\0"; 38406ac60b7SViktor Prutyanov IMAGE_DOS_HEADER *dos_hdr = start_addr; 38506ac60b7SViktor Prutyanov IMAGE_NT_HEADERS64 nt_hdrs; 38606ac60b7SViktor Prutyanov IMAGE_FILE_HEADER *file_hdr = &nt_hdrs.FileHeader; 38706ac60b7SViktor Prutyanov IMAGE_OPTIONAL_HEADER64 *opt_hdr = &nt_hdrs.OptionalHeader; 38806ac60b7SViktor Prutyanov IMAGE_DATA_DIRECTORY *data_dir = nt_hdrs.OptionalHeader.DataDirectory; 38906ac60b7SViktor Prutyanov 39006ac60b7SViktor Prutyanov QEMU_BUILD_BUG_ON(sizeof(*dos_hdr) >= ELF2DMP_PAGE_SIZE); 39106ac60b7SViktor Prutyanov 39206ac60b7SViktor Prutyanov if (memcmp(&dos_hdr->e_magic, e_magic, sizeof(e_magic))) { 393fbc3d7d2SAkihiko Odaki return false; 39406ac60b7SViktor Prutyanov } 39506ac60b7SViktor Prutyanov 396a15f9749SAkihiko Odaki if (!va_space_rw(vs, base + dos_hdr->e_lfanew, 39706ac60b7SViktor Prutyanov &nt_hdrs, sizeof(nt_hdrs), 0)) { 398fbc3d7d2SAkihiko Odaki return false; 39906ac60b7SViktor Prutyanov } 40006ac60b7SViktor Prutyanov 40106ac60b7SViktor Prutyanov if (memcmp(&nt_hdrs.Signature, Signature, sizeof(Signature)) || 40206ac60b7SViktor Prutyanov file_hdr->Machine != 0x8664 || opt_hdr->Magic != 0x020b) { 403fbc3d7d2SAkihiko Odaki return false; 40406ac60b7SViktor Prutyanov } 40506ac60b7SViktor Prutyanov 406a15f9749SAkihiko Odaki if (!va_space_rw(vs, base + data_dir[idx].VirtualAddress, entry, size, 0)) { 407fbc3d7d2SAkihiko Odaki return false; 40806ac60b7SViktor Prutyanov } 40906ac60b7SViktor Prutyanov 41006ac60b7SViktor Prutyanov printf("Data directory entry #%d: RVA = 0x%08"PRIx32"\n", idx, 41106ac60b7SViktor Prutyanov (uint32_t)data_dir[idx].VirtualAddress); 41206ac60b7SViktor Prutyanov 413fbc3d7d2SAkihiko Odaki return true; 41406ac60b7SViktor Prutyanov } 41506ac60b7SViktor Prutyanov 416fbc3d7d2SAkihiko Odaki static bool write_dump(struct pa_space *ps, 4173fa2d384SViktor Prutyanov WinDumpHeader64 *hdr, const char *name) 4183fa2d384SViktor Prutyanov { 4193fa2d384SViktor Prutyanov FILE *dmp_file = fopen(name, "wb"); 4203fa2d384SViktor Prutyanov size_t i; 4213fa2d384SViktor Prutyanov 4223fa2d384SViktor Prutyanov if (!dmp_file) { 4233fa2d384SViktor Prutyanov eprintf("Failed to open output file \'%s\'\n", name); 424fbc3d7d2SAkihiko Odaki return false; 4253fa2d384SViktor Prutyanov } 4263fa2d384SViktor Prutyanov 4273fa2d384SViktor Prutyanov printf("Writing header to file...\n"); 4283fa2d384SViktor Prutyanov 4293fa2d384SViktor Prutyanov if (fwrite(hdr, sizeof(*hdr), 1, dmp_file) != 1) { 4303fa2d384SViktor Prutyanov eprintf("Failed to write dump header\n"); 4313fa2d384SViktor Prutyanov fclose(dmp_file); 432fbc3d7d2SAkihiko Odaki return false; 4333fa2d384SViktor Prutyanov } 4343fa2d384SViktor Prutyanov 4353fa2d384SViktor Prutyanov for (i = 0; i < ps->block_nr; i++) { 4363fa2d384SViktor Prutyanov struct pa_block *b = &ps->block[i]; 4373fa2d384SViktor Prutyanov 438d5c27a53SViktor Prutyanov printf("Writing block #%zu/%zu of %"PRIu64" bytes to file...\n", i, 439d5c27a53SViktor Prutyanov ps->block_nr, b->size); 4403fa2d384SViktor Prutyanov if (fwrite(b->addr, b->size, 1, dmp_file) != 1) { 441d5c27a53SViktor Prutyanov eprintf("Failed to write block\n"); 4423fa2d384SViktor Prutyanov fclose(dmp_file); 443fbc3d7d2SAkihiko Odaki return false; 4443fa2d384SViktor Prutyanov } 4453fa2d384SViktor Prutyanov } 4463fa2d384SViktor Prutyanov 447fbc3d7d2SAkihiko Odaki return !fclose(dmp_file); 4483fa2d384SViktor Prutyanov } 4493fa2d384SViktor Prutyanov 4503c407ec6SViktor Prutyanov static bool pe_check_pdb_name(uint64_t base, void *start_addr, 4513c407ec6SViktor Prutyanov struct va_space *vs, OMFSignatureRSDS *rsds) 4523fa2d384SViktor Prutyanov { 4533fa2d384SViktor Prutyanov const char sign_rsds[4] = "RSDS"; 4543fa2d384SViktor Prutyanov IMAGE_DEBUG_DIRECTORY debug_dir; 4553c407ec6SViktor Prutyanov char pdb_name[sizeof(PDB_NAME)]; 4563fa2d384SViktor Prutyanov 457fbc3d7d2SAkihiko Odaki if (!pe_get_data_dir_entry(base, start_addr, IMAGE_FILE_DEBUG_DIRECTORY, 45806ac60b7SViktor Prutyanov &debug_dir, sizeof(debug_dir), vs)) { 45906ac60b7SViktor Prutyanov eprintf("Failed to get Debug Directory\n"); 4603c407ec6SViktor Prutyanov return false; 4613fa2d384SViktor Prutyanov } 4623fa2d384SViktor Prutyanov 4633fa2d384SViktor Prutyanov if (debug_dir.Type != IMAGE_DEBUG_TYPE_CODEVIEW) { 4643c407ec6SViktor Prutyanov eprintf("Debug Directory type is not CodeView\n"); 4653c407ec6SViktor Prutyanov return false; 4663fa2d384SViktor Prutyanov } 4673fa2d384SViktor Prutyanov 468a15f9749SAkihiko Odaki if (!va_space_rw(vs, base + debug_dir.AddressOfRawData, 4693c407ec6SViktor Prutyanov rsds, sizeof(*rsds), 0)) { 4703c407ec6SViktor Prutyanov eprintf("Failed to resolve OMFSignatureRSDS\n"); 4713c407ec6SViktor Prutyanov return false; 4723fa2d384SViktor Prutyanov } 4733fa2d384SViktor Prutyanov 4743c407ec6SViktor Prutyanov if (memcmp(&rsds->Signature, sign_rsds, sizeof(sign_rsds))) { 4758b01683eSViktor Prutyanov eprintf("CodeView signature is \'%.4s\', \'%.4s\' expected\n", 4763c407ec6SViktor Prutyanov rsds->Signature, sign_rsds); 4773c407ec6SViktor Prutyanov return false; 4783fa2d384SViktor Prutyanov } 4793fa2d384SViktor Prutyanov 4803c407ec6SViktor Prutyanov if (debug_dir.SizeOfData - sizeof(*rsds) != sizeof(PDB_NAME)) { 4813c407ec6SViktor Prutyanov eprintf("PDB name size doesn't match\n"); 4823c407ec6SViktor Prutyanov return false; 4833fa2d384SViktor Prutyanov } 4843fa2d384SViktor Prutyanov 485a15f9749SAkihiko Odaki if (!va_space_rw(vs, base + debug_dir.AddressOfRawData + 486a15f9749SAkihiko Odaki offsetof(OMFSignatureRSDS, name), 487a15f9749SAkihiko Odaki pdb_name, sizeof(PDB_NAME), 0)) { 4883c407ec6SViktor Prutyanov eprintf("Failed to resolve PDB name\n"); 4893c407ec6SViktor Prutyanov return false; 4903fa2d384SViktor Prutyanov } 4913fa2d384SViktor Prutyanov 4923fa2d384SViktor Prutyanov printf("PDB name is \'%s\', \'%s\' expected\n", pdb_name, PDB_NAME); 4933fa2d384SViktor Prutyanov 4943c407ec6SViktor Prutyanov return !strcmp(pdb_name, PDB_NAME); 4953fa2d384SViktor Prutyanov } 4963fa2d384SViktor Prutyanov 4973c407ec6SViktor Prutyanov static void pe_get_pdb_symstore_hash(OMFSignatureRSDS *rsds, char *hash) 4983c407ec6SViktor Prutyanov { 4993c407ec6SViktor Prutyanov sprintf(hash, "%.08x%.04x%.04x%.02x%.02x", rsds->guid.a, rsds->guid.b, 5003c407ec6SViktor Prutyanov rsds->guid.c, rsds->guid.d[0], rsds->guid.d[1]); 5013fa2d384SViktor Prutyanov hash += 20; 5023c407ec6SViktor Prutyanov for (unsigned int i = 0; i < 6; i++, hash += 2) { 5033c407ec6SViktor Prutyanov sprintf(hash, "%.02x", rsds->guid.e[i]); 5043fa2d384SViktor Prutyanov } 5053fa2d384SViktor Prutyanov 5063c407ec6SViktor Prutyanov sprintf(hash, "%.01x", rsds->age); 5073fa2d384SViktor Prutyanov } 5083fa2d384SViktor Prutyanov 5093fa2d384SViktor Prutyanov int main(int argc, char *argv[]) 5103fa2d384SViktor Prutyanov { 511a4e58de1SAkihiko Odaki int err = 1; 5123fa2d384SViktor Prutyanov QEMU_Elf qemu_elf; 5133fa2d384SViktor Prutyanov struct pa_space ps; 5143fa2d384SViktor Prutyanov struct va_space vs; 5153fa2d384SViktor Prutyanov QEMUCPUState *state; 5163fa2d384SViktor Prutyanov idt_desc_t first_idt_desc; 5173fa2d384SViktor Prutyanov uint64_t KernBase; 5183fa2d384SViktor Prutyanov void *nt_start_addr = NULL; 5193fa2d384SViktor Prutyanov WinDumpHeader64 header; 5203fa2d384SViktor Prutyanov char pdb_hash[34]; 5213fa2d384SViktor Prutyanov char pdb_url[] = SYM_URL_BASE PDB_NAME 5223fa2d384SViktor Prutyanov "/0123456789ABCDEF0123456789ABCDEFx/" PDB_NAME; 5233fa2d384SViktor Prutyanov struct pdb_reader pdb; 5243fa2d384SViktor Prutyanov uint64_t KdDebuggerDataBlock; 5253fa2d384SViktor Prutyanov KDDEBUGGER_DATA64 *kdbg; 5263fa2d384SViktor Prutyanov uint64_t KdVersionBlock; 527d399d6b1SViktor Prutyanov bool kernel_found = false; 5283c407ec6SViktor Prutyanov OMFSignatureRSDS rsds; 5293fa2d384SViktor Prutyanov 5303fa2d384SViktor Prutyanov if (argc != 3) { 5313fa2d384SViktor Prutyanov eprintf("usage:\n\t%s elf_file dmp_file\n", argv[0]); 5323fa2d384SViktor Prutyanov return 1; 5333fa2d384SViktor Prutyanov } 5343fa2d384SViktor Prutyanov 53549760ccfSAkihiko Odaki if (!QEMU_Elf_init(&qemu_elf, argv[1])) { 5363fa2d384SViktor Prutyanov eprintf("Failed to initialize QEMU ELF dump\n"); 5373fa2d384SViktor Prutyanov return 1; 5383fa2d384SViktor Prutyanov } 5393fa2d384SViktor Prutyanov 540262a0ff8SAkihiko Odaki pa_space_create(&ps, &qemu_elf); 5413fa2d384SViktor Prutyanov 5423fa2d384SViktor Prutyanov state = qemu_elf.state[0]; 5436ec6e988SViktor Prutyanov printf("CPU #0 CR3 is 0x%016"PRIx64"\n", state->cr[3]); 5443fa2d384SViktor Prutyanov 5453fa2d384SViktor Prutyanov va_space_create(&vs, &ps, state->cr[3]); 546fbc3d7d2SAkihiko Odaki if (!fix_dtb(&vs, &qemu_elf)) { 5473fa2d384SViktor Prutyanov eprintf("Failed to find paging base\n"); 5482aa205f7SAkihiko Odaki goto out_ps; 5493fa2d384SViktor Prutyanov } 5503fa2d384SViktor Prutyanov 5516ec6e988SViktor Prutyanov printf("CPU #0 IDT is at 0x%016"PRIx64"\n", state->idt.base); 5523fa2d384SViktor Prutyanov 553a15f9749SAkihiko Odaki if (!va_space_rw(&vs, state->idt.base, 5543fa2d384SViktor Prutyanov &first_idt_desc, sizeof(first_idt_desc), 0)) { 5553fa2d384SViktor Prutyanov eprintf("Failed to get CPU #0 IDT[0]\n"); 5563fa2d384SViktor Prutyanov goto out_ps; 5573fa2d384SViktor Prutyanov } 5586ec6e988SViktor Prutyanov printf("CPU #0 IDT[0] -> 0x%016"PRIx64"\n", idt_desc_addr(first_idt_desc)); 5593fa2d384SViktor Prutyanov 5602d0fc797SJiaxun Yang KernBase = idt_desc_addr(first_idt_desc) & ~(ELF2DMP_PAGE_SIZE - 1); 5616ec6e988SViktor Prutyanov printf("Searching kernel downwards from 0x%016"PRIx64"...\n", KernBase); 5623fa2d384SViktor Prutyanov 5632d0fc797SJiaxun Yang for (; KernBase >= 0xfffff78000000000; KernBase -= ELF2DMP_PAGE_SIZE) { 5643fa2d384SViktor Prutyanov nt_start_addr = va_space_resolve(&vs, KernBase); 5653fa2d384SViktor Prutyanov if (!nt_start_addr) { 5663fa2d384SViktor Prutyanov continue; 5673fa2d384SViktor Prutyanov } 5683fa2d384SViktor Prutyanov 5693fa2d384SViktor Prutyanov if (*(uint16_t *)nt_start_addr == 0x5a4d) { /* MZ */ 5703c407ec6SViktor Prutyanov printf("Checking candidate KernBase = 0x%016"PRIx64"\n", KernBase); 5713c407ec6SViktor Prutyanov if (pe_check_pdb_name(KernBase, nt_start_addr, &vs, &rsds)) { 572d399d6b1SViktor Prutyanov kernel_found = true; 5733fa2d384SViktor Prutyanov break; 5743fa2d384SViktor Prutyanov } 5753fa2d384SViktor Prutyanov } 576d399d6b1SViktor Prutyanov } 5773fa2d384SViktor Prutyanov 578d399d6b1SViktor Prutyanov if (!kernel_found) { 57906164cc4SViktor Prutyanov eprintf("Failed to find NT kernel image\n"); 58006164cc4SViktor Prutyanov goto out_ps; 58106164cc4SViktor Prutyanov } 58206164cc4SViktor Prutyanov 5836ec6e988SViktor Prutyanov printf("KernBase = 0x%016"PRIx64", signature is \'%.2s\'\n", KernBase, 5843fa2d384SViktor Prutyanov (char *)nt_start_addr); 5853fa2d384SViktor Prutyanov 5863c407ec6SViktor Prutyanov pe_get_pdb_symstore_hash(&rsds, pdb_hash); 5873fa2d384SViktor Prutyanov 5883fa2d384SViktor Prutyanov sprintf(pdb_url, "%s%s/%s/%s", SYM_URL_BASE, PDB_NAME, pdb_hash, PDB_NAME); 5893fa2d384SViktor Prutyanov printf("PDB URL is %s\n", pdb_url); 5903fa2d384SViktor Prutyanov 5911b806c36SAkihiko Odaki if (!download_url(PDB_NAME, pdb_url)) { 5923fa2d384SViktor Prutyanov eprintf("Failed to download PDB file\n"); 5933fa2d384SViktor Prutyanov goto out_ps; 5943fa2d384SViktor Prutyanov } 5953fa2d384SViktor Prutyanov 596b1250455SAkihiko Odaki if (!pdb_init_from_file(PDB_NAME, &pdb)) { 5973fa2d384SViktor Prutyanov eprintf("Failed to initialize PDB reader\n"); 5983fa2d384SViktor Prutyanov goto out_pdb_file; 5993fa2d384SViktor Prutyanov } 6003fa2d384SViktor Prutyanov 6013fa2d384SViktor Prutyanov if (!SYM_RESOLVE(KernBase, &pdb, KdDebuggerDataBlock) || 6023fa2d384SViktor Prutyanov !SYM_RESOLVE(KernBase, &pdb, KdVersionBlock)) { 6033fa2d384SViktor Prutyanov goto out_pdb; 6043fa2d384SViktor Prutyanov } 6053fa2d384SViktor Prutyanov 6063fa2d384SViktor Prutyanov kdbg = get_kdbg(KernBase, &pdb, &vs, KdDebuggerDataBlock); 6073fa2d384SViktor Prutyanov if (!kdbg) { 6083fa2d384SViktor Prutyanov goto out_pdb; 6093fa2d384SViktor Prutyanov } 6103fa2d384SViktor Prutyanov 611fbc3d7d2SAkihiko Odaki if (!fill_header(&header, &ps, &vs, KdDebuggerDataBlock, kdbg, 6123fa2d384SViktor Prutyanov KdVersionBlock, qemu_elf.state_nr)) { 613885538fdSAlexChen goto out_kdbg; 6143fa2d384SViktor Prutyanov } 6153fa2d384SViktor Prutyanov 61687157ef3SAkihiko Odaki fill_context(kdbg, &vs, &qemu_elf); 6173fa2d384SViktor Prutyanov 618fbc3d7d2SAkihiko Odaki if (!write_dump(&ps, &header, argv[2])) { 6193fa2d384SViktor Prutyanov eprintf("Failed to save dump\n"); 6203fa2d384SViktor Prutyanov goto out_kdbg; 6213fa2d384SViktor Prutyanov } 6223fa2d384SViktor Prutyanov 623a4e58de1SAkihiko Odaki err = 0; 624a4e58de1SAkihiko Odaki 6253fa2d384SViktor Prutyanov out_kdbg: 6262a052b4eSSuraj Shirvankar g_free(kdbg); 6273fa2d384SViktor Prutyanov out_pdb: 6283fa2d384SViktor Prutyanov pdb_exit(&pdb); 6293fa2d384SViktor Prutyanov out_pdb_file: 6303fa2d384SViktor Prutyanov unlink(PDB_NAME); 6313fa2d384SViktor Prutyanov out_ps: 6323fa2d384SViktor Prutyanov pa_space_destroy(&ps); 6333fa2d384SViktor Prutyanov QEMU_Elf_exit(&qemu_elf); 6343fa2d384SViktor Prutyanov 6353fa2d384SViktor Prutyanov return err; 6363fa2d384SViktor Prutyanov } 637