1 | /* $Id: DBGPlugInWinNt.cpp 73491 2018-08-03 14:51:55Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGPlugInWindows - Debugger and Guest OS Digger Plugin For Windows NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DBGF /// @todo add new log group.
|
---|
23 | #include "DBGPlugIns.h"
|
---|
24 | #include <VBox/vmm/dbgf.h>
|
---|
25 | #include <VBox/vmm/cpumctx.h>
|
---|
26 | #include <VBox/vmm/mm.h>
|
---|
27 | #include <VBox/err.h>
|
---|
28 | #include <VBox/param.h>
|
---|
29 | #include <iprt/ctype.h>
|
---|
30 | #include <iprt/ldr.h>
|
---|
31 | #include <iprt/mem.h>
|
---|
32 | #include <iprt/path.h>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/formats/pecoff.h>
|
---|
36 | #include <iprt/formats/mz.h>
|
---|
37 | #include <iprt/nt/nt-structures.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Structures and Typedefs *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 |
|
---|
44 | /** @name Internal WinNT structures
|
---|
45 | * @{ */
|
---|
46 | /**
|
---|
47 | * PsLoadedModuleList entry for 32-bit NT aka LDR_DATA_TABLE_ENTRY.
|
---|
48 | * Tested with XP.
|
---|
49 | */
|
---|
50 | typedef struct NTMTE32
|
---|
51 | {
|
---|
52 | struct
|
---|
53 | {
|
---|
54 | uint32_t Flink;
|
---|
55 | uint32_t Blink;
|
---|
56 | } InLoadOrderLinks,
|
---|
57 | InMemoryOrderModuleList,
|
---|
58 | InInitializationOrderModuleList;
|
---|
59 | uint32_t DllBase;
|
---|
60 | uint32_t EntryPoint;
|
---|
61 | /** @note This field is not a size in NT 3.1. It's NULL for images loaded by the
|
---|
62 | * boot loader, for other images it looks like some kind of pointer. */
|
---|
63 | uint32_t SizeOfImage;
|
---|
64 | struct
|
---|
65 | {
|
---|
66 | uint16_t Length;
|
---|
67 | uint16_t MaximumLength;
|
---|
68 | uint32_t Buffer;
|
---|
69 | } FullDllName,
|
---|
70 | BaseDllName;
|
---|
71 | uint32_t Flags;
|
---|
72 | uint16_t LoadCount;
|
---|
73 | uint16_t TlsIndex;
|
---|
74 | /* ... there is more ... */
|
---|
75 | } NTMTE32;
|
---|
76 | typedef NTMTE32 *PNTMTE32;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * PsLoadedModuleList entry for 64-bit NT aka LDR_DATA_TABLE_ENTRY.
|
---|
80 | */
|
---|
81 | typedef struct NTMTE64
|
---|
82 | {
|
---|
83 | struct
|
---|
84 | {
|
---|
85 | uint64_t Flink;
|
---|
86 | uint64_t Blink;
|
---|
87 | } InLoadOrderLinks, /**< 0x00 */
|
---|
88 | InMemoryOrderModuleList, /**< 0x10 */
|
---|
89 | InInitializationOrderModuleList; /**< 0x20 */
|
---|
90 | uint64_t DllBase; /**< 0x30 */
|
---|
91 | uint64_t EntryPoint; /**< 0x38 */
|
---|
92 | uint32_t SizeOfImage; /**< 0x40 */
|
---|
93 | uint32_t Alignment; /**< 0x44 */
|
---|
94 | struct
|
---|
95 | {
|
---|
96 | uint16_t Length; /**< 0x48,0x58 */
|
---|
97 | uint16_t MaximumLength; /**< 0x4a,0x5a */
|
---|
98 | uint32_t Alignment; /**< 0x4c,0x5c */
|
---|
99 | uint64_t Buffer; /**< 0x50,0x60 */
|
---|
100 | } FullDllName, /**< 0x48 */
|
---|
101 | BaseDllName; /**< 0x58 */
|
---|
102 | uint32_t Flags; /**< 0x68 */
|
---|
103 | uint16_t LoadCount; /**< 0x6c */
|
---|
104 | uint16_t TlsIndex; /**< 0x6e */
|
---|
105 | /* ... there is more ... */
|
---|
106 | } NTMTE64;
|
---|
107 | typedef NTMTE64 *PNTMTE64;
|
---|
108 |
|
---|
109 | /** MTE union. */
|
---|
110 | typedef union NTMTE
|
---|
111 | {
|
---|
112 | NTMTE32 vX_32;
|
---|
113 | NTMTE64 vX_64;
|
---|
114 | } NTMTE;
|
---|
115 | typedef NTMTE *PNTMTE;
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * The essential bits of the KUSER_SHARED_DATA structure.
|
---|
120 | */
|
---|
121 | typedef struct NTKUSERSHAREDDATA
|
---|
122 | {
|
---|
123 | uint32_t TickCountLowDeprecated;
|
---|
124 | uint32_t TickCountMultiplier;
|
---|
125 | struct
|
---|
126 | {
|
---|
127 | uint32_t LowPart;
|
---|
128 | int32_t High1Time;
|
---|
129 | int32_t High2Time;
|
---|
130 |
|
---|
131 | } InterruptTime,
|
---|
132 | SystemTime,
|
---|
133 | TimeZoneBias;
|
---|
134 | uint16_t ImageNumberLow;
|
---|
135 | uint16_t ImageNumberHigh;
|
---|
136 | RTUTF16 NtSystemRoot[260];
|
---|
137 | uint32_t MaxStackTraceDepth;
|
---|
138 | uint32_t CryptoExponent;
|
---|
139 | uint32_t TimeZoneId;
|
---|
140 | uint32_t LargePageMinimum;
|
---|
141 | uint32_t Reserved2[7];
|
---|
142 | uint32_t NtProductType;
|
---|
143 | uint8_t ProductTypeIsValid;
|
---|
144 | uint8_t abPadding[3];
|
---|
145 | uint32_t NtMajorVersion;
|
---|
146 | uint32_t NtMinorVersion;
|
---|
147 | /* uint8_t ProcessorFeatures[64];
|
---|
148 | ...
|
---|
149 | */
|
---|
150 | } NTKUSERSHAREDDATA;
|
---|
151 | typedef NTKUSERSHAREDDATA *PNTKUSERSHAREDDATA;
|
---|
152 |
|
---|
153 | /** KI_USER_SHARED_DATA for i386 */
|
---|
154 | #define NTKUSERSHAREDDATA_WINNT32 UINT32_C(0xffdf0000)
|
---|
155 | /** KI_USER_SHARED_DATA for AMD64 */
|
---|
156 | #define NTKUSERSHAREDDATA_WINNT64 UINT64_C(0xfffff78000000000)
|
---|
157 |
|
---|
158 | /** NTKUSERSHAREDDATA::NtProductType */
|
---|
159 | typedef enum NTPRODUCTTYPE
|
---|
160 | {
|
---|
161 | kNtProductType_Invalid = 0,
|
---|
162 | kNtProductType_WinNt = 1,
|
---|
163 | kNtProductType_LanManNt,
|
---|
164 | kNtProductType_Server
|
---|
165 | } NTPRODUCTTYPE;
|
---|
166 |
|
---|
167 |
|
---|
168 | /** NT image header union. */
|
---|
169 | typedef union NTHDRSU
|
---|
170 | {
|
---|
171 | IMAGE_NT_HEADERS32 vX_32;
|
---|
172 | IMAGE_NT_HEADERS64 vX_64;
|
---|
173 | } NTHDRS;
|
---|
174 | /** Pointer to NT image header union. */
|
---|
175 | typedef NTHDRS *PNTHDRS;
|
---|
176 | /** Pointer to const NT image header union. */
|
---|
177 | typedef NTHDRS const *PCNTHDRS;
|
---|
178 |
|
---|
179 | /** @} */
|
---|
180 |
|
---|
181 |
|
---|
182 |
|
---|
183 | typedef enum DBGDIGGERWINNTVER
|
---|
184 | {
|
---|
185 | DBGDIGGERWINNTVER_UNKNOWN,
|
---|
186 | DBGDIGGERWINNTVER_3_1,
|
---|
187 | DBGDIGGERWINNTVER_3_5,
|
---|
188 | DBGDIGGERWINNTVER_4_0,
|
---|
189 | DBGDIGGERWINNTVER_5_0,
|
---|
190 | DBGDIGGERWINNTVER_5_1,
|
---|
191 | DBGDIGGERWINNTVER_6_0
|
---|
192 | } DBGDIGGERWINNTVER;
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * WinNT guest OS digger instance data.
|
---|
196 | */
|
---|
197 | typedef struct DBGDIGGERWINNT
|
---|
198 | {
|
---|
199 | /** Whether the information is valid or not.
|
---|
200 | * (For fending off illegal interface method calls.) */
|
---|
201 | bool fValid;
|
---|
202 | /** 32-bit (true) or 64-bit (false) */
|
---|
203 | bool f32Bit;
|
---|
204 | /** Set if NT 3.1 was detected.
|
---|
205 | * This implies both Misc.VirtualSize and NTMTE32::SizeOfImage are zero. */
|
---|
206 | bool fNt31;
|
---|
207 |
|
---|
208 | /** The NT version. */
|
---|
209 | DBGDIGGERWINNTVER enmVer;
|
---|
210 | /** NTKUSERSHAREDDATA::NtProductType */
|
---|
211 | NTPRODUCTTYPE NtProductType;
|
---|
212 | /** NTKUSERSHAREDDATA::NtMajorVersion */
|
---|
213 | uint32_t NtMajorVersion;
|
---|
214 | /** NTKUSERSHAREDDATA::NtMinorVersion */
|
---|
215 | uint32_t NtMinorVersion;
|
---|
216 |
|
---|
217 | /** The address of the ntoskrnl.exe image. */
|
---|
218 | DBGFADDRESS KernelAddr;
|
---|
219 | /** The address of the ntoskrnl.exe module table entry. */
|
---|
220 | DBGFADDRESS KernelMteAddr;
|
---|
221 | /** The address of PsLoadedModuleList. */
|
---|
222 | DBGFADDRESS PsLoadedModuleListAddr;
|
---|
223 | } DBGDIGGERWINNT;
|
---|
224 | /** Pointer to the linux guest OS digger instance data. */
|
---|
225 | typedef DBGDIGGERWINNT *PDBGDIGGERWINNT;
|
---|
226 |
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * The WinNT digger's loader reader instance data.
|
---|
230 | */
|
---|
231 | typedef struct DBGDIGGERWINNTRDR
|
---|
232 | {
|
---|
233 | /** The VM handle (referenced). */
|
---|
234 | PUVM pUVM;
|
---|
235 | /** The image base. */
|
---|
236 | DBGFADDRESS ImageAddr;
|
---|
237 | /** The image size. */
|
---|
238 | uint32_t cbImage;
|
---|
239 | /** The file offset of the SizeOfImage field in the optional header if it
|
---|
240 | * needs patching, otherwise set to UINT32_MAX. */
|
---|
241 | uint32_t offSizeOfImage;
|
---|
242 | /** The correct image size. */
|
---|
243 | uint32_t cbCorrectImageSize;
|
---|
244 | /** Number of entries in the aMappings table. */
|
---|
245 | uint32_t cMappings;
|
---|
246 | /** Mapping hint. */
|
---|
247 | uint32_t iHint;
|
---|
248 | /** Mapping file offset to memory offsets, ordered by file offset. */
|
---|
249 | struct
|
---|
250 | {
|
---|
251 | /** The file offset. */
|
---|
252 | uint32_t offFile;
|
---|
253 | /** The size of this mapping. */
|
---|
254 | uint32_t cbMem;
|
---|
255 | /** The offset to the memory from the start of the image. */
|
---|
256 | uint32_t offMem;
|
---|
257 | } aMappings[1];
|
---|
258 | } DBGDIGGERWINNTRDR;
|
---|
259 | /** Pointer a WinNT loader reader instance data. */
|
---|
260 | typedef DBGDIGGERWINNTRDR *PDBGDIGGERWINNTRDR;
|
---|
261 |
|
---|
262 |
|
---|
263 | /*********************************************************************************************************************************
|
---|
264 | * Defined Constants And Macros *
|
---|
265 | *********************************************************************************************************************************/
|
---|
266 | /** Validates a 32-bit Windows NT kernel address */
|
---|
267 | #define WINNT32_VALID_ADDRESS(Addr) ((Addr) > UINT32_C(0x80000000) && (Addr) < UINT32_C(0xfffff000))
|
---|
268 | /** Validates a 64-bit Windows NT kernel address */
|
---|
269 | #define WINNT64_VALID_ADDRESS(Addr) ((Addr) > UINT64_C(0xffff800000000000) && (Addr) < UINT64_C(0xfffffffffffff000))
|
---|
270 | /** Validates a kernel address. */
|
---|
271 | #define WINNT_VALID_ADDRESS(pThis, Addr) ((pThis)->f32Bit ? WINNT32_VALID_ADDRESS(Addr) : WINNT64_VALID_ADDRESS(Addr))
|
---|
272 | /** Versioned and bitness wrapper. */
|
---|
273 | #define WINNT_UNION(pThis, pUnion, Member) ((pThis)->f32Bit ? (pUnion)->vX_32. Member : (pUnion)->vX_64. Member )
|
---|
274 |
|
---|
275 | /** The length (in chars) of the kernel file name (no path). */
|
---|
276 | #define WINNT_KERNEL_BASE_NAME_LEN 12
|
---|
277 |
|
---|
278 | /** WindowsNT on little endian ASCII systems. */
|
---|
279 | #define DIG_WINNT_MOD_TAG UINT64_C(0x54696e646f774e54)
|
---|
280 |
|
---|
281 |
|
---|
282 | /*********************************************************************************************************************************
|
---|
283 | * Internal Functions *
|
---|
284 | *********************************************************************************************************************************/
|
---|
285 | static DECLCALLBACK(int) dbgDiggerWinNtInit(PUVM pUVM, void *pvData);
|
---|
286 |
|
---|
287 |
|
---|
288 | /*********************************************************************************************************************************
|
---|
289 | * Global Variables *
|
---|
290 | *********************************************************************************************************************************/
|
---|
291 | /** Kernel names. */
|
---|
292 | static const RTUTF16 g_wszKernelNames[][WINNT_KERNEL_BASE_NAME_LEN + 1] =
|
---|
293 | {
|
---|
294 | { 'n', 't', 'o', 's', 'k', 'r', 'n', 'l', '.', 'e', 'x', 'e' }
|
---|
295 | };
|
---|
296 |
|
---|
297 |
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Process a PE image found in guest memory.
|
---|
301 | *
|
---|
302 | * @param pThis The instance data.
|
---|
303 | * @param pUVM The user mode VM handle.
|
---|
304 | * @param pszName The module name.
|
---|
305 | * @param pszFilename The image filename.
|
---|
306 | * @param pImageAddr The image address.
|
---|
307 | * @param cbImage The size of the image.
|
---|
308 | */
|
---|
309 | static void dbgDiggerWinNtProcessImage(PDBGDIGGERWINNT pThis, PUVM pUVM, const char *pszName, const char *pszFilename,
|
---|
310 | PCDBGFADDRESS pImageAddr, uint32_t cbImage)
|
---|
311 | {
|
---|
312 | LogFlow(("DigWinNt: %RGp %#x %s\n", pImageAddr->FlatPtr, cbImage, pszName));
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * Do some basic validation first.
|
---|
316 | */
|
---|
317 | if ( (cbImage < sizeof(IMAGE_NT_HEADERS64) && !pThis->fNt31)
|
---|
318 | || cbImage >= _1M * 256)
|
---|
319 | {
|
---|
320 | Log(("DigWinNt: %s: Bad image size: %#x\n", pszName, cbImage));
|
---|
321 | return;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * Use the common in-memory module reader to create a debug module.
|
---|
326 | */
|
---|
327 | RTERRINFOSTATIC ErrInfo;
|
---|
328 | RTDBGMOD hDbgMod = NIL_RTDBGMOD;
|
---|
329 | int rc = DBGFR3ModInMem(pUVM, pImageAddr, pThis->fNt31 ? DBGFMODINMEM_F_PE_NT31 : 0, pszName, pszFilename,
|
---|
330 | pThis->f32Bit ? RTLDRARCH_X86_32 : RTLDRARCH_AMD64, cbImage,
|
---|
331 | &hDbgMod, RTErrInfoInitStatic(&ErrInfo));
|
---|
332 | if (RT_SUCCESS(rc))
|
---|
333 | {
|
---|
334 | /*
|
---|
335 | * Tag the module.
|
---|
336 | */
|
---|
337 | rc = RTDbgModSetTag(hDbgMod, DIG_WINNT_MOD_TAG);
|
---|
338 | AssertRC(rc);
|
---|
339 |
|
---|
340 | /*
|
---|
341 | * Link the module.
|
---|
342 | */
|
---|
343 | RTDBGAS hAs = DBGFR3AsResolveAndRetain(pUVM, DBGF_AS_KERNEL);
|
---|
344 | if (hAs != NIL_RTDBGAS)
|
---|
345 | rc = RTDbgAsModuleLink(hAs, hDbgMod, pImageAddr->FlatPtr, RTDBGASLINK_FLAGS_REPLACE /*fFlags*/);
|
---|
346 | else
|
---|
347 | rc = VERR_INTERNAL_ERROR;
|
---|
348 | RTDbgModRelease(hDbgMod);
|
---|
349 | RTDbgAsRelease(hAs);
|
---|
350 | }
|
---|
351 | else if (RTErrInfoIsSet(&ErrInfo.Core))
|
---|
352 | Log(("DigWinNt: %s: DBGFR3ModInMem failed: %Rrc - %s\n", pszName, rc, ErrInfo.Core.pszMsg));
|
---|
353 | else
|
---|
354 | Log(("DigWinNt: %s: DBGFR3ModInMem failed: %Rrc\n", pszName, rc));
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * Adjust the module name into something that's compatible with the debugger.
|
---|
360 | *
|
---|
361 | * @param pUVM The user mode VM handle.
|
---|
362 | * @param ppszName Pointer to the image name pointer.
|
---|
363 | * @param pImageAddr The image load address.
|
---|
364 | */
|
---|
365 | static const char *dbgDiggerWintNtFilenameToModuleName(const char *pszFilename, char *pszName, size_t cbName)
|
---|
366 | {
|
---|
367 | /* Skip to the filename part of the filename. :-) */
|
---|
368 | pszFilename = RTPathFilenameEx(pszFilename, RTPATH_STR_F_STYLE_DOS);
|
---|
369 |
|
---|
370 | /* We try use 'nt' for the kernel. */
|
---|
371 | if ( RTStrICmpAscii(pszFilename, "ntoskrnl.exe") == 0
|
---|
372 | || RTStrICmpAscii(pszFilename, "ntkrnlmp.exe") == 0)
|
---|
373 | return "nt";
|
---|
374 |
|
---|
375 |
|
---|
376 | /* Drop the extension if .dll or .sys. */
|
---|
377 | size_t cchFilename = strlen(pszFilename);
|
---|
378 | if ( cchFilename > 4
|
---|
379 | && pszFilename[cchFilename - 4] == '.')
|
---|
380 | {
|
---|
381 | if ( RTStrICmpAscii(&pszFilename[cchFilename - 4], ".sys") == 0
|
---|
382 | || RTStrICmpAscii(&pszFilename[cchFilename - 4], ".dll") == 0)
|
---|
383 | cchFilename -= 4;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* Copy and do replacements. */
|
---|
387 | if (cchFilename >= cbName)
|
---|
388 | cchFilename = cbName - 1;
|
---|
389 | size_t off;
|
---|
390 | for (off = 0; off < cchFilename; off++)
|
---|
391 | {
|
---|
392 | char ch = pszFilename[off];
|
---|
393 | if (!RT_C_IS_ALNUM(ch))
|
---|
394 | ch = '_';
|
---|
395 | pszName[off] = ch;
|
---|
396 | }
|
---|
397 | pszName[off] = '\0';
|
---|
398 | return pszName;
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * @copydoc DBGFOSREG::pfnStackUnwindAssist
|
---|
404 | */
|
---|
405 | static DECLCALLBACK(int) dbgDiggerWinNtStackUnwindAssist(PUVM pUVM, void *pvData, VMCPUID idCpu, PDBGFSTACKFRAME pFrame,
|
---|
406 | PRTDBGUNWINDSTATE pState, PCCPUMCTX pInitialCtx, RTDBGAS hAs,
|
---|
407 | uint64_t *puScratch)
|
---|
408 | {
|
---|
409 | Assert(pInitialCtx);
|
---|
410 |
|
---|
411 | /*
|
---|
412 | * We want to locate trap frames here. The trap frame structure contains
|
---|
413 | * the 64-bit IRET frame, so given unwind information it's easy to identify
|
---|
414 | * using the return type and frame address.
|
---|
415 | */
|
---|
416 | if (pFrame->fFlags & DBGFSTACKFRAME_FLAGS_64BIT)
|
---|
417 | {
|
---|
418 | /*
|
---|
419 | * Is this a trap frame? If so, try read the trap frame.
|
---|
420 | */
|
---|
421 | if ( pFrame->enmReturnType == RTDBGRETURNTYPE_IRET64
|
---|
422 | && !(pFrame->AddrFrame.FlatPtr & 0x7)
|
---|
423 | && WINNT64_VALID_ADDRESS(pFrame->AddrFrame.FlatPtr) )
|
---|
424 | {
|
---|
425 | KTRAP_FRAME_AMD64 TrapFrame;
|
---|
426 | RT_ZERO(TrapFrame);
|
---|
427 | uint64_t const uTrapFrameAddr = pFrame->AddrFrame.FlatPtr
|
---|
428 | - RT_UOFFSETOF(KTRAP_FRAME_AMD64, ErrCdOrXcptFrameOrS);
|
---|
429 | int rc = pState->pfnReadStack(pState, uTrapFrameAddr, sizeof(TrapFrame), &TrapFrame);
|
---|
430 | if (RT_SUCCESS(rc))
|
---|
431 | {
|
---|
432 | /* Valid? Not too much else we can check here (EFlags isn't
|
---|
433 | reliable in manually construct frames). */
|
---|
434 | if (TrapFrame.ExceptionActive <= 2)
|
---|
435 | {
|
---|
436 | pFrame->fFlags |= DBGFSTACKFRAME_FLAGS_TRAP_FRAME;
|
---|
437 |
|
---|
438 | /*
|
---|
439 | * Add sure 'register' information from the frame to the frame.
|
---|
440 | *
|
---|
441 | * To avoid code duplication, we do this in two steps in a loop.
|
---|
442 | * The first iteration only figures out how many registers we're
|
---|
443 | * going to save and allocates room for them. The second iteration
|
---|
444 | * does the actual adding.
|
---|
445 | */
|
---|
446 | uint32_t cRegs = pFrame->cSureRegs;
|
---|
447 | PDBGFREGVALEX paSureRegs = NULL;
|
---|
448 | #define ADD_REG_NAMED(a_Type, a_ValMemb, a_Value, a_pszName) do { \
|
---|
449 | if (paSureRegs) \
|
---|
450 | { \
|
---|
451 | paSureRegs[iReg].pszName = a_pszName;\
|
---|
452 | paSureRegs[iReg].enmReg = DBGFREG_END; \
|
---|
453 | paSureRegs[iReg].enmType = a_Type; \
|
---|
454 | paSureRegs[iReg].Value.a_ValMemb = (a_Value); \
|
---|
455 | } \
|
---|
456 | iReg++; \
|
---|
457 | } while (0)
|
---|
458 | #define MAYBE_ADD_GREG(a_Value, a_enmReg, a_idxReg) do { \
|
---|
459 | if (!(pState->u.x86.Loaded.s.fRegs & RT_BIT(a_idxReg))) \
|
---|
460 | { \
|
---|
461 | if (paSureRegs) \
|
---|
462 | { \
|
---|
463 | pState->u.x86.Loaded.s.fRegs |= RT_BIT(a_idxReg); \
|
---|
464 | pState->u.x86.auRegs[a_idxReg] = (a_Value); \
|
---|
465 | paSureRegs[iReg].Value.u64 = (a_Value); \
|
---|
466 | paSureRegs[iReg].enmReg = a_enmReg; \
|
---|
467 | paSureRegs[iReg].enmType = DBGFREGVALTYPE_U64; \
|
---|
468 | paSureRegs[iReg].pszName = NULL; \
|
---|
469 | } \
|
---|
470 | iReg++; \
|
---|
471 | } \
|
---|
472 | } while (0)
|
---|
473 | for (unsigned iLoop = 0; iLoop < 2; iLoop++)
|
---|
474 | {
|
---|
475 | uint32_t iReg = pFrame->cSureRegs;
|
---|
476 | ADD_REG_NAMED(DBGFREGVALTYPE_U64, u64, uTrapFrameAddr, "TrapFrame");
|
---|
477 | ADD_REG_NAMED(DBGFREGVALTYPE_U8, u8, TrapFrame.ExceptionActive, "ExceptionActive");
|
---|
478 | if (TrapFrame.ExceptionActive == 0)
|
---|
479 | {
|
---|
480 | ADD_REG_NAMED(DBGFREGVALTYPE_U8, u8, TrapFrame.PreviousIrql, "PrevIrql");
|
---|
481 | ADD_REG_NAMED(DBGFREGVALTYPE_U8, u8, (uint8_t)TrapFrame.ErrCdOrXcptFrameOrS, "IntNo");
|
---|
482 | }
|
---|
483 | else if ( TrapFrame.ExceptionActive == 1
|
---|
484 | && TrapFrame.FaultIndicator == ((TrapFrame.ErrCdOrXcptFrameOrS >> 1) & 0x9))
|
---|
485 | ADD_REG_NAMED(DBGFREGVALTYPE_U64, u64, TrapFrame.FaultAddrOrCtxRecOrTS, "cr2-probably");
|
---|
486 | if (TrapFrame.SegCs & X86_SEL_RPL)
|
---|
487 | ADD_REG_NAMED(DBGFREGVALTYPE_U8, u8, 1, "UserMode");
|
---|
488 | else
|
---|
489 | ADD_REG_NAMED(DBGFREGVALTYPE_U8, u8, 1, "KernelMode");
|
---|
490 | if (TrapFrame.ExceptionActive <= 1)
|
---|
491 | {
|
---|
492 | MAYBE_ADD_GREG(TrapFrame.Rax, DBGFREG_RAX, X86_GREG_xAX);
|
---|
493 | MAYBE_ADD_GREG(TrapFrame.Rcx, DBGFREG_RCX, X86_GREG_xCX);
|
---|
494 | MAYBE_ADD_GREG(TrapFrame.Rdx, DBGFREG_RDX, X86_GREG_xDX);
|
---|
495 | MAYBE_ADD_GREG(TrapFrame.R8, DBGFREG_R8, X86_GREG_x8);
|
---|
496 | MAYBE_ADD_GREG(TrapFrame.R9, DBGFREG_R9, X86_GREG_x9);
|
---|
497 | MAYBE_ADD_GREG(TrapFrame.R10, DBGFREG_R10, X86_GREG_x10);
|
---|
498 | MAYBE_ADD_GREG(TrapFrame.R11, DBGFREG_R11, X86_GREG_x11);
|
---|
499 | }
|
---|
500 | else if (TrapFrame.ExceptionActive == 2)
|
---|
501 | {
|
---|
502 | MAYBE_ADD_GREG(TrapFrame.Rbx, DBGFREG_RBX, X86_GREG_xBX);
|
---|
503 | MAYBE_ADD_GREG(TrapFrame.Rsi, DBGFREG_RSI, X86_GREG_xSI);
|
---|
504 | MAYBE_ADD_GREG(TrapFrame.Rdi, DBGFREG_RDI, X86_GREG_xDI);
|
---|
505 | }
|
---|
506 | // MAYBE_ADD_GREG(TrapFrame.Rbp, DBGFREG_RBP, X86_GREG_xBP); - KiInterrupt[Sub]Dispatch* may leave this invalid.
|
---|
507 |
|
---|
508 | /* Done? */
|
---|
509 | if (iLoop > 0)
|
---|
510 | {
|
---|
511 | Assert(cRegs == iReg);
|
---|
512 | break;
|
---|
513 | }
|
---|
514 |
|
---|
515 | /* Resize the array, zeroing the extension. */
|
---|
516 | if (pFrame->cSureRegs)
|
---|
517 | paSureRegs = (PDBGFREGVALEX)MMR3HeapRealloc(pFrame->paSureRegs, iReg * sizeof(paSureRegs[0]));
|
---|
518 | else
|
---|
519 | paSureRegs = (PDBGFREGVALEX)MMR3HeapAllocU(pUVM, MM_TAG_DBGF_STACK, iReg * sizeof(paSureRegs[0]));
|
---|
520 | AssertReturn(paSureRegs, VERR_NO_MEMORY);
|
---|
521 |
|
---|
522 | pFrame->paSureRegs = paSureRegs;
|
---|
523 | RT_BZERO(&paSureRegs[pFrame->cSureRegs], (iReg - pFrame->cSureRegs) * sizeof(paSureRegs[0]));
|
---|
524 | cRegs = iReg;
|
---|
525 | }
|
---|
526 | #undef ADD_REG_NAMED
|
---|
527 | #undef MAYBE_ADD_GREG
|
---|
528 |
|
---|
529 | /* Commit the register update. */
|
---|
530 | pFrame->cSureRegs = cRegs;
|
---|
531 | }
|
---|
532 | }
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | RT_NOREF(pUVM, pvData, idCpu, hAs, pInitialCtx, puScratch);
|
---|
537 | return VINF_SUCCESS;
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | /**
|
---|
542 | * @copydoc DBGFOSREG::pfnQueryInterface
|
---|
543 | */
|
---|
544 | static DECLCALLBACK(void *) dbgDiggerWinNtQueryInterface(PUVM pUVM, void *pvData, DBGFOSINTERFACE enmIf)
|
---|
545 | {
|
---|
546 | RT_NOREF3(pUVM, pvData, enmIf);
|
---|
547 | return NULL;
|
---|
548 | }
|
---|
549 |
|
---|
550 |
|
---|
551 | /**
|
---|
552 | * @copydoc DBGFOSREG::pfnQueryVersion
|
---|
553 | */
|
---|
554 | static DECLCALLBACK(int) dbgDiggerWinNtQueryVersion(PUVM pUVM, void *pvData, char *pszVersion, size_t cchVersion)
|
---|
555 | {
|
---|
556 | RT_NOREF1(pUVM);
|
---|
557 | PDBGDIGGERWINNT pThis = (PDBGDIGGERWINNT)pvData;
|
---|
558 | Assert(pThis->fValid);
|
---|
559 | const char *pszNtProductType;
|
---|
560 | switch (pThis->NtProductType)
|
---|
561 | {
|
---|
562 | case kNtProductType_WinNt: pszNtProductType = "-WinNT"; break;
|
---|
563 | case kNtProductType_LanManNt: pszNtProductType = "-LanManNT"; break;
|
---|
564 | case kNtProductType_Server: pszNtProductType = "-Server"; break;
|
---|
565 | default: pszNtProductType = ""; break;
|
---|
566 | }
|
---|
567 | RTStrPrintf(pszVersion, cchVersion, "%u.%u-%s%s", pThis->NtMajorVersion, pThis->NtMinorVersion,
|
---|
568 | pThis->f32Bit ? "x86" : "AMD64", pszNtProductType);
|
---|
569 | return VINF_SUCCESS;
|
---|
570 | }
|
---|
571 |
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * @copydoc DBGFOSREG::pfnTerm
|
---|
575 | */
|
---|
576 | static DECLCALLBACK(void) dbgDiggerWinNtTerm(PUVM pUVM, void *pvData)
|
---|
577 | {
|
---|
578 | RT_NOREF1(pUVM);
|
---|
579 | PDBGDIGGERWINNT pThis = (PDBGDIGGERWINNT)pvData;
|
---|
580 | Assert(pThis->fValid);
|
---|
581 |
|
---|
582 | /*
|
---|
583 | * As long as we're using our private LDR reader implementation,
|
---|
584 | * we must unlink and ditch the modules we created.
|
---|
585 | */
|
---|
586 | RTDBGAS hDbgAs = DBGFR3AsResolveAndRetain(pUVM, DBGF_AS_KERNEL);
|
---|
587 | if (hDbgAs != NIL_RTDBGAS)
|
---|
588 | {
|
---|
589 | uint32_t iMod = RTDbgAsModuleCount(hDbgAs);
|
---|
590 | while (iMod-- > 0)
|
---|
591 | {
|
---|
592 | RTDBGMOD hMod = RTDbgAsModuleByIndex(hDbgAs, iMod);
|
---|
593 | if (hMod != NIL_RTDBGMOD)
|
---|
594 | {
|
---|
595 | if (RTDbgModGetTag(hMod) == DIG_WINNT_MOD_TAG)
|
---|
596 | {
|
---|
597 | int rc = RTDbgAsModuleUnlink(hDbgAs, hMod);
|
---|
598 | AssertRC(rc);
|
---|
599 | }
|
---|
600 | RTDbgModRelease(hMod);
|
---|
601 | }
|
---|
602 | }
|
---|
603 | RTDbgAsRelease(hDbgAs);
|
---|
604 | }
|
---|
605 |
|
---|
606 | pThis->fValid = false;
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | /**
|
---|
611 | * @copydoc DBGFOSREG::pfnRefresh
|
---|
612 | */
|
---|
613 | static DECLCALLBACK(int) dbgDiggerWinNtRefresh(PUVM pUVM, void *pvData)
|
---|
614 | {
|
---|
615 | PDBGDIGGERWINNT pThis = (PDBGDIGGERWINNT)pvData;
|
---|
616 | NOREF(pThis);
|
---|
617 | Assert(pThis->fValid);
|
---|
618 |
|
---|
619 | /*
|
---|
620 | * For now we'll flush and reload everything.
|
---|
621 | */
|
---|
622 | dbgDiggerWinNtTerm(pUVM, pvData);
|
---|
623 |
|
---|
624 | return dbgDiggerWinNtInit(pUVM, pvData);
|
---|
625 | }
|
---|
626 |
|
---|
627 |
|
---|
628 | /**
|
---|
629 | * @copydoc DBGFOSREG::pfnInit
|
---|
630 | */
|
---|
631 | static DECLCALLBACK(int) dbgDiggerWinNtInit(PUVM pUVM, void *pvData)
|
---|
632 | {
|
---|
633 | PDBGDIGGERWINNT pThis = (PDBGDIGGERWINNT)pvData;
|
---|
634 | Assert(!pThis->fValid);
|
---|
635 |
|
---|
636 | union
|
---|
637 | {
|
---|
638 | uint8_t au8[0x2000];
|
---|
639 | RTUTF16 wsz[0x2000/2];
|
---|
640 | NTKUSERSHAREDDATA UserSharedData;
|
---|
641 | } u;
|
---|
642 | DBGFADDRESS Addr;
|
---|
643 | int rc;
|
---|
644 |
|
---|
645 | /*
|
---|
646 | * Figure the NT version.
|
---|
647 | */
|
---|
648 | DBGFR3AddrFromFlat(pUVM, &Addr, pThis->f32Bit ? NTKUSERSHAREDDATA_WINNT32 : NTKUSERSHAREDDATA_WINNT64);
|
---|
649 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &Addr, &u, PAGE_SIZE);
|
---|
650 | if (RT_SUCCESS(rc))
|
---|
651 | {
|
---|
652 | pThis->NtProductType = u.UserSharedData.ProductTypeIsValid && u.UserSharedData.NtProductType <= kNtProductType_Server
|
---|
653 | ? (NTPRODUCTTYPE)u.UserSharedData.NtProductType
|
---|
654 | : kNtProductType_Invalid;
|
---|
655 | pThis->NtMajorVersion = u.UserSharedData.NtMajorVersion;
|
---|
656 | pThis->NtMinorVersion = u.UserSharedData.NtMinorVersion;
|
---|
657 | }
|
---|
658 | else if (pThis->fNt31)
|
---|
659 | {
|
---|
660 | pThis->NtProductType = kNtProductType_WinNt;
|
---|
661 | pThis->NtMajorVersion = 3;
|
---|
662 | pThis->NtMinorVersion = 1;
|
---|
663 | }
|
---|
664 | else
|
---|
665 | {
|
---|
666 | Log(("DigWinNt: Error reading KUSER_SHARED_DATA: %Rrc\n", rc));
|
---|
667 | return rc;
|
---|
668 | }
|
---|
669 |
|
---|
670 | /*
|
---|
671 | * Dig out the module chain.
|
---|
672 | */
|
---|
673 | DBGFADDRESS AddrPrev = pThis->PsLoadedModuleListAddr;
|
---|
674 | Addr = pThis->KernelMteAddr;
|
---|
675 | do
|
---|
676 | {
|
---|
677 | /* Read the validate the MTE. */
|
---|
678 | NTMTE Mte;
|
---|
679 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &Addr, &Mte, pThis->f32Bit ? sizeof(Mte.vX_32) : sizeof(Mte.vX_64));
|
---|
680 | if (RT_FAILURE(rc))
|
---|
681 | break;
|
---|
682 | if (WINNT_UNION(pThis, &Mte, InLoadOrderLinks.Blink) != AddrPrev.FlatPtr)
|
---|
683 | {
|
---|
684 | Log(("DigWinNt: Bad Mte At %RGv - backpointer\n", Addr.FlatPtr));
|
---|
685 | break;
|
---|
686 | }
|
---|
687 | if (!WINNT_VALID_ADDRESS(pThis, WINNT_UNION(pThis, &Mte, InLoadOrderLinks.Flink)) )
|
---|
688 | {
|
---|
689 | Log(("DigWinNt: Bad Mte at %RGv - forward pointer\n", Addr.FlatPtr));
|
---|
690 | break;
|
---|
691 | }
|
---|
692 | if (!WINNT_VALID_ADDRESS(pThis, WINNT_UNION(pThis, &Mte, BaseDllName.Buffer)))
|
---|
693 | {
|
---|
694 | Log(("DigWinNt: Bad Mte at %RGv - BaseDllName=%llx\n", Addr.FlatPtr, WINNT_UNION(pThis, &Mte, BaseDllName.Buffer)));
|
---|
695 | break;
|
---|
696 | }
|
---|
697 | if (!WINNT_VALID_ADDRESS(pThis, WINNT_UNION(pThis, &Mte, FullDllName.Buffer)))
|
---|
698 | {
|
---|
699 | Log(("DigWinNt: Bad Mte at %RGv - FullDllName=%llx\n", Addr.FlatPtr, WINNT_UNION(pThis, &Mte, FullDllName.Buffer)));
|
---|
700 | break;
|
---|
701 | }
|
---|
702 | if (!WINNT_VALID_ADDRESS(pThis, WINNT_UNION(pThis, &Mte, DllBase)))
|
---|
703 | {
|
---|
704 | Log(("DigWinNt: Bad Mte at %RGv - DllBase=%llx\n", Addr.FlatPtr, WINNT_UNION(pThis, &Mte, DllBase) ));
|
---|
705 | break;
|
---|
706 | }
|
---|
707 |
|
---|
708 | uint32_t const cbImageMte = !pThis->fNt31 ? WINNT_UNION(pThis, &Mte, SizeOfImage) : 0;
|
---|
709 | if ( !pThis->fNt31
|
---|
710 | && ( cbImageMte > _256M
|
---|
711 | || WINNT_UNION(pThis, &Mte, EntryPoint) - WINNT_UNION(pThis, &Mte, DllBase) > cbImageMte) )
|
---|
712 | {
|
---|
713 | Log(("DigWinNt: Bad Mte at %RGv - EntryPoint=%llx SizeOfImage=%x DllBase=%llx\n",
|
---|
714 | Addr.FlatPtr, WINNT_UNION(pThis, &Mte, EntryPoint), cbImageMte, WINNT_UNION(pThis, &Mte, DllBase)));
|
---|
715 | break;
|
---|
716 | }
|
---|
717 |
|
---|
718 | /* Read the full name. */
|
---|
719 | DBGFADDRESS AddrName;
|
---|
720 | DBGFR3AddrFromFlat(pUVM, &AddrName, WINNT_UNION(pThis, &Mte, FullDllName.Buffer));
|
---|
721 | uint16_t cbName = WINNT_UNION(pThis, &Mte, FullDllName.Length);
|
---|
722 | if (cbName < sizeof(u))
|
---|
723 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &AddrName, &u, cbName);
|
---|
724 | else
|
---|
725 | rc = VERR_OUT_OF_RANGE;
|
---|
726 | if (RT_FAILURE(rc))
|
---|
727 | {
|
---|
728 | DBGFR3AddrFromFlat(pUVM, &AddrName, WINNT_UNION(pThis, &Mte, BaseDllName.Buffer));
|
---|
729 | cbName = WINNT_UNION(pThis, &Mte, BaseDllName.Length);
|
---|
730 | if (cbName < sizeof(u))
|
---|
731 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &AddrName, &u, cbName);
|
---|
732 | else
|
---|
733 | rc = VERR_OUT_OF_RANGE;
|
---|
734 | }
|
---|
735 | if (RT_SUCCESS(rc))
|
---|
736 | {
|
---|
737 | u.wsz[cbName / 2] = '\0';
|
---|
738 |
|
---|
739 | char *pszFilename;
|
---|
740 | rc = RTUtf16ToUtf8(u.wsz, &pszFilename);
|
---|
741 | if (RT_SUCCESS(rc))
|
---|
742 | {
|
---|
743 | char szModName[128];
|
---|
744 | const char *pszModName = dbgDiggerWintNtFilenameToModuleName(pszFilename, szModName, sizeof(szModName));
|
---|
745 |
|
---|
746 | /* Read the start of the PE image and pass it along to a worker. */
|
---|
747 | DBGFADDRESS ImageAddr;
|
---|
748 | DBGFR3AddrFromFlat(pUVM, &ImageAddr, WINNT_UNION(pThis, &Mte, DllBase));
|
---|
749 | dbgDiggerWinNtProcessImage(pThis, pUVM, pszModName, pszFilename, &ImageAddr, cbImageMte);
|
---|
750 | RTStrFree(pszFilename);
|
---|
751 | }
|
---|
752 | }
|
---|
753 |
|
---|
754 | /* next */
|
---|
755 | AddrPrev = Addr;
|
---|
756 | DBGFR3AddrFromFlat(pUVM, &Addr, WINNT_UNION(pThis, &Mte, InLoadOrderLinks.Flink));
|
---|
757 | } while ( Addr.FlatPtr != pThis->KernelMteAddr.FlatPtr
|
---|
758 | && Addr.FlatPtr != pThis->PsLoadedModuleListAddr.FlatPtr);
|
---|
759 |
|
---|
760 | pThis->fValid = true;
|
---|
761 | return VINF_SUCCESS;
|
---|
762 | }
|
---|
763 |
|
---|
764 |
|
---|
765 | /**
|
---|
766 | * @copydoc DBGFOSREG::pfnProbe
|
---|
767 | */
|
---|
768 | static DECLCALLBACK(bool) dbgDiggerWinNtProbe(PUVM pUVM, void *pvData)
|
---|
769 | {
|
---|
770 | PDBGDIGGERWINNT pThis = (PDBGDIGGERWINNT)pvData;
|
---|
771 | DBGFADDRESS Addr;
|
---|
772 | union
|
---|
773 | {
|
---|
774 | uint8_t au8[8192];
|
---|
775 | uint16_t au16[8192/2];
|
---|
776 | uint32_t au32[8192/4];
|
---|
777 | IMAGE_DOS_HEADER MzHdr;
|
---|
778 | RTUTF16 wsz[8192/2];
|
---|
779 | X86DESC64GATE a32Gates[X86_XCPT_PF + 1];
|
---|
780 | X86DESC64GATE a64Gates[X86_XCPT_PF + 1];
|
---|
781 | } u;
|
---|
782 |
|
---|
783 | union
|
---|
784 | {
|
---|
785 | NTMTE32 v32;
|
---|
786 | NTMTE64 v64;
|
---|
787 | } uMte, uMte2, uMte3;
|
---|
788 |
|
---|
789 | /*
|
---|
790 | * NT only runs in protected or long mode.
|
---|
791 | */
|
---|
792 | CPUMMODE const enmMode = DBGFR3CpuGetMode(pUVM, 0 /*idCpu*/);
|
---|
793 | if (enmMode != CPUMMODE_PROTECTED && enmMode != CPUMMODE_LONG)
|
---|
794 | return false;
|
---|
795 | bool const f64Bit = enmMode == CPUMMODE_LONG;
|
---|
796 | uint64_t const uStart = f64Bit ? UINT64_C(0xffff080000000000) : UINT32_C(0x80001000);
|
---|
797 | uint64_t const uEnd = f64Bit ? UINT64_C(0xffffffffffff0000) : UINT32_C(0xffff0000);
|
---|
798 |
|
---|
799 | /*
|
---|
800 | * To approximately locate the kernel we examine the IDTR handlers.
|
---|
801 | *
|
---|
802 | * The exception/trap/fault handlers are all in NT kernel image, we pick
|
---|
803 | * KiPageFault here.
|
---|
804 | */
|
---|
805 | uint64_t uIdtrBase = 0;
|
---|
806 | uint16_t uIdtrLimit = 0;
|
---|
807 | int rc = DBGFR3RegCpuQueryXdtr(pUVM, 0, DBGFREG_IDTR, &uIdtrBase, &uIdtrLimit);
|
---|
808 | AssertRCReturn(rc, false);
|
---|
809 |
|
---|
810 | const uint16_t cbMinIdtr = (X86_XCPT_PF + 1) * (f64Bit ? sizeof(X86DESC64GATE) : sizeof(X86DESCGATE));
|
---|
811 | if (uIdtrLimit < cbMinIdtr)
|
---|
812 | return false;
|
---|
813 |
|
---|
814 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, DBGFR3AddrFromFlat(pUVM, &Addr, uIdtrBase), &u, cbMinIdtr);
|
---|
815 | if (RT_FAILURE(rc))
|
---|
816 | return false;
|
---|
817 |
|
---|
818 | uint64_t uKrnlStart = uStart;
|
---|
819 | uint64_t uKrnlEnd = uEnd;
|
---|
820 | if (f64Bit)
|
---|
821 | {
|
---|
822 | uint64_t uHandler = u.a64Gates[X86_XCPT_PF].u16OffsetLow
|
---|
823 | | ((uint32_t)u.a64Gates[X86_XCPT_PF].u16OffsetHigh << 16)
|
---|
824 | | ((uint64_t)u.a64Gates[X86_XCPT_PF].u32OffsetTop << 32);
|
---|
825 | if (uHandler < uStart || uHandler > uEnd)
|
---|
826 | return false;
|
---|
827 | uKrnlStart = (uHandler & ~(uint64_t)_4M) - _512M;
|
---|
828 | uKrnlEnd = (uHandler + (uint64_t)_4M) & ~(uint64_t)_4M;
|
---|
829 | }
|
---|
830 | else
|
---|
831 | {
|
---|
832 | uint32_t uHandler = u.a32Gates[X86_XCPT_PF].u16OffsetLow
|
---|
833 | | ((uint32_t)u.a64Gates[X86_XCPT_PF].u16OffsetHigh << 16);
|
---|
834 | if (uHandler < uStart || uHandler > uEnd)
|
---|
835 | return false;
|
---|
836 | uKrnlStart = (uHandler & ~(uint64_t)_4M) - _64M;
|
---|
837 | uKrnlEnd = (uHandler + (uint64_t)_4M) & ~(uint64_t)_4M;
|
---|
838 | }
|
---|
839 |
|
---|
840 | /*
|
---|
841 | * Look for the PAGELK section name that seems to be a part of all kernels.
|
---|
842 | * Then try find the module table entry for it. Since it's the first entry
|
---|
843 | * in the PsLoadedModuleList we can easily validate the list head and report
|
---|
844 | * success.
|
---|
845 | *
|
---|
846 | * Note! We ASSUME the section name is 8 byte aligned.
|
---|
847 | */
|
---|
848 | DBGFADDRESS KernelAddr;
|
---|
849 | for (DBGFR3AddrFromFlat(pUVM, &KernelAddr, uKrnlStart);
|
---|
850 | KernelAddr.FlatPtr < uKrnlEnd;
|
---|
851 | KernelAddr.FlatPtr += PAGE_SIZE)
|
---|
852 | {
|
---|
853 | bool fNt31 = false;
|
---|
854 | DBGFADDRESS const RetryAddress = KernelAddr;
|
---|
855 | rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, &KernelAddr, uEnd - KernelAddr.FlatPtr,
|
---|
856 | 8, "PAGELK\0", sizeof("PAGELK\0"), &KernelAddr);
|
---|
857 | if ( rc == VERR_DBGF_MEM_NOT_FOUND
|
---|
858 | && enmMode != CPUMMODE_LONG)
|
---|
859 | {
|
---|
860 | /* NT3.1 didn't have a PAGELK section, so look for _TEXT instead. The
|
---|
861 | following VirtualSize is zero, so check for that too. */
|
---|
862 | rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, &RetryAddress, uEnd - RetryAddress.FlatPtr,
|
---|
863 | 8, "_TEXT\0\0\0\0\0\0", sizeof("_TEXT\0\0\0\0\0\0"), &KernelAddr);
|
---|
864 | fNt31 = true;
|
---|
865 | }
|
---|
866 | if (RT_FAILURE(rc))
|
---|
867 | break;
|
---|
868 | DBGFR3AddrSub(&KernelAddr, KernelAddr.FlatPtr & PAGE_OFFSET_MASK);
|
---|
869 |
|
---|
870 | /* MZ + PE header. */
|
---|
871 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, &KernelAddr, &u, sizeof(u));
|
---|
872 | if ( RT_SUCCESS(rc)
|
---|
873 | && u.MzHdr.e_magic == IMAGE_DOS_SIGNATURE
|
---|
874 | && !(u.MzHdr.e_lfanew & 0x7)
|
---|
875 | && u.MzHdr.e_lfanew >= 0x080
|
---|
876 | && u.MzHdr.e_lfanew <= 0x400) /* W8 is at 0x288*/
|
---|
877 | {
|
---|
878 | if (enmMode != CPUMMODE_LONG)
|
---|
879 | {
|
---|
880 | IMAGE_NT_HEADERS32 const *pHdrs = (IMAGE_NT_HEADERS32 const *)&u.au8[u.MzHdr.e_lfanew];
|
---|
881 | if ( pHdrs->Signature == IMAGE_NT_SIGNATURE
|
---|
882 | && pHdrs->FileHeader.Machine == IMAGE_FILE_MACHINE_I386
|
---|
883 | && pHdrs->FileHeader.SizeOfOptionalHeader == sizeof(pHdrs->OptionalHeader)
|
---|
884 | && pHdrs->FileHeader.NumberOfSections >= 10 /* the kernel has lots */
|
---|
885 | && (pHdrs->FileHeader.Characteristics & (IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL)) == IMAGE_FILE_EXECUTABLE_IMAGE
|
---|
886 | && pHdrs->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC
|
---|
887 | && pHdrs->OptionalHeader.NumberOfRvaAndSizes == IMAGE_NUMBEROF_DIRECTORY_ENTRIES
|
---|
888 | )
|
---|
889 | {
|
---|
890 | /* Find the MTE. */
|
---|
891 | RT_ZERO(uMte);
|
---|
892 | uMte.v32.DllBase = KernelAddr.FlatPtr;
|
---|
893 | uMte.v32.EntryPoint = KernelAddr.FlatPtr + pHdrs->OptionalHeader.AddressOfEntryPoint;
|
---|
894 | uMte.v32.SizeOfImage = !fNt31 ? pHdrs->OptionalHeader.SizeOfImage : 0; /* NT 3.1 didn't set the size. */
|
---|
895 | DBGFADDRESS HitAddr;
|
---|
896 | rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, &KernelAddr, uEnd - KernelAddr.FlatPtr,
|
---|
897 | 4 /*align*/, &uMte.v32.DllBase, 3 * sizeof(uint32_t), &HitAddr);
|
---|
898 | while (RT_SUCCESS(rc))
|
---|
899 | {
|
---|
900 | /* check the name. */
|
---|
901 | DBGFADDRESS MteAddr = HitAddr;
|
---|
902 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, DBGFR3AddrSub(&MteAddr, RT_OFFSETOF(NTMTE32, DllBase)),
|
---|
903 | &uMte2.v32, sizeof(uMte2.v32));
|
---|
904 | if ( RT_SUCCESS(rc)
|
---|
905 | && uMte2.v32.DllBase == uMte.v32.DllBase
|
---|
906 | && uMte2.v32.EntryPoint == uMte.v32.EntryPoint
|
---|
907 | && uMte2.v32.SizeOfImage == uMte.v32.SizeOfImage
|
---|
908 | && WINNT32_VALID_ADDRESS(uMte2.v32.InLoadOrderLinks.Flink)
|
---|
909 | && WINNT32_VALID_ADDRESS(uMte2.v32.BaseDllName.Buffer)
|
---|
910 | && WINNT32_VALID_ADDRESS(uMte2.v32.FullDllName.Buffer)
|
---|
911 | && uMte2.v32.BaseDllName.Length <= 128
|
---|
912 | && uMte2.v32.FullDllName.Length <= 260
|
---|
913 | )
|
---|
914 | {
|
---|
915 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, DBGFR3AddrFromFlat(pUVM, &Addr, uMte2.v32.BaseDllName.Buffer),
|
---|
916 | u.wsz, uMte2.v32.BaseDllName.Length);
|
---|
917 | u.wsz[uMte2.v32.BaseDllName.Length / 2] = '\0';
|
---|
918 | if ( RT_SUCCESS(rc)
|
---|
919 | && ( !RTUtf16ICmp(u.wsz, g_wszKernelNames[0])
|
---|
920 | /* || !RTUtf16ICmp(u.wsz, g_wszKernelNames[1]) */
|
---|
921 | )
|
---|
922 | )
|
---|
923 | {
|
---|
924 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/,
|
---|
925 | DBGFR3AddrFromFlat(pUVM, &Addr, uMte2.v32.InLoadOrderLinks.Blink),
|
---|
926 | &uMte3.v32, RT_SIZEOFMEMB(NTMTE32, InLoadOrderLinks));
|
---|
927 | if ( RT_SUCCESS(rc)
|
---|
928 | && uMte3.v32.InLoadOrderLinks.Flink == MteAddr.FlatPtr
|
---|
929 | && WINNT32_VALID_ADDRESS(uMte3.v32.InLoadOrderLinks.Blink) )
|
---|
930 | {
|
---|
931 | Log(("DigWinNt: MteAddr=%RGv KernelAddr=%RGv SizeOfImage=%x &PsLoadedModuleList=%RGv (32-bit)\n",
|
---|
932 | MteAddr.FlatPtr, KernelAddr.FlatPtr, uMte2.v32.SizeOfImage, Addr.FlatPtr));
|
---|
933 | pThis->KernelAddr = KernelAddr;
|
---|
934 | pThis->KernelMteAddr = MteAddr;
|
---|
935 | pThis->PsLoadedModuleListAddr = Addr;
|
---|
936 | pThis->f32Bit = true;
|
---|
937 | pThis->fNt31 = fNt31;
|
---|
938 | return true;
|
---|
939 | }
|
---|
940 | }
|
---|
941 | else if (RT_SUCCESS(rc))
|
---|
942 | {
|
---|
943 | Log2(("DigWinNt: Wrong module: MteAddr=%RGv ImageAddr=%RGv SizeOfImage=%#x '%ls'\n",
|
---|
944 | MteAddr.FlatPtr, KernelAddr.FlatPtr, uMte2.v32.SizeOfImage, u.wsz));
|
---|
945 | break; /* Not NT kernel */
|
---|
946 | }
|
---|
947 | }
|
---|
948 |
|
---|
949 | /* next */
|
---|
950 | DBGFR3AddrAdd(&HitAddr, 4);
|
---|
951 | if (HitAddr.FlatPtr < uEnd)
|
---|
952 | rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, &HitAddr, uEnd - HitAddr.FlatPtr,
|
---|
953 | 4 /*align*/, &uMte.v32.DllBase, 3 * sizeof(uint32_t), &HitAddr);
|
---|
954 | else
|
---|
955 | rc = VERR_DBGF_MEM_NOT_FOUND;
|
---|
956 | }
|
---|
957 | }
|
---|
958 | }
|
---|
959 | else
|
---|
960 | {
|
---|
961 | IMAGE_NT_HEADERS64 const *pHdrs = (IMAGE_NT_HEADERS64 const *)&u.au8[u.MzHdr.e_lfanew];
|
---|
962 | if ( pHdrs->Signature == IMAGE_NT_SIGNATURE
|
---|
963 | && pHdrs->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64
|
---|
964 | && pHdrs->FileHeader.SizeOfOptionalHeader == sizeof(pHdrs->OptionalHeader)
|
---|
965 | && pHdrs->FileHeader.NumberOfSections >= 10 /* the kernel has lots */
|
---|
966 | && (pHdrs->FileHeader.Characteristics & (IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL))
|
---|
967 | == IMAGE_FILE_EXECUTABLE_IMAGE
|
---|
968 | && pHdrs->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC
|
---|
969 | && pHdrs->OptionalHeader.NumberOfRvaAndSizes == IMAGE_NUMBEROF_DIRECTORY_ENTRIES
|
---|
970 | )
|
---|
971 | {
|
---|
972 | /* Find the MTE. */
|
---|
973 | RT_ZERO(uMte.v64);
|
---|
974 | uMte.v64.DllBase = KernelAddr.FlatPtr;
|
---|
975 | uMte.v64.EntryPoint = KernelAddr.FlatPtr + pHdrs->OptionalHeader.AddressOfEntryPoint;
|
---|
976 | uMte.v64.SizeOfImage = pHdrs->OptionalHeader.SizeOfImage;
|
---|
977 | DBGFADDRESS ScanAddr;
|
---|
978 | DBGFADDRESS HitAddr;
|
---|
979 | rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, DBGFR3AddrFromFlat(pUVM, &ScanAddr, uStart),
|
---|
980 | uEnd - uStart, 8 /*align*/, &uMte.v64.DllBase, 5 * sizeof(uint32_t), &HitAddr);
|
---|
981 | while (RT_SUCCESS(rc))
|
---|
982 | {
|
---|
983 | /* Read the start of the MTE and check some basic members. */
|
---|
984 | DBGFADDRESS MteAddr = HitAddr;
|
---|
985 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, DBGFR3AddrSub(&MteAddr, RT_OFFSETOF(NTMTE64, DllBase)),
|
---|
986 | &uMte2.v64, sizeof(uMte2.v64));
|
---|
987 | if ( RT_SUCCESS(rc)
|
---|
988 | && uMte2.v64.DllBase == uMte.v64.DllBase
|
---|
989 | && uMte2.v64.EntryPoint == uMte.v64.EntryPoint
|
---|
990 | && uMte2.v64.SizeOfImage == uMte.v64.SizeOfImage
|
---|
991 | && WINNT64_VALID_ADDRESS(uMte2.v64.InLoadOrderLinks.Flink)
|
---|
992 | && WINNT64_VALID_ADDRESS(uMte2.v64.BaseDllName.Buffer)
|
---|
993 | && WINNT64_VALID_ADDRESS(uMte2.v64.FullDllName.Buffer)
|
---|
994 | && uMte2.v64.BaseDllName.Length <= 128
|
---|
995 | && uMte2.v64.FullDllName.Length <= 260
|
---|
996 | )
|
---|
997 | {
|
---|
998 | /* Try read the base name and compare with known NT kernel names. */
|
---|
999 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/, DBGFR3AddrFromFlat(pUVM, &Addr, uMte2.v64.BaseDllName.Buffer),
|
---|
1000 | u.wsz, uMte2.v64.BaseDllName.Length);
|
---|
1001 | u.wsz[uMte2.v64.BaseDllName.Length / 2] = '\0';
|
---|
1002 | if ( RT_SUCCESS(rc)
|
---|
1003 | && ( !RTUtf16ICmp(u.wsz, g_wszKernelNames[0])
|
---|
1004 | /* || !RTUtf16ICmp(u.wsz, g_wszKernelNames[1]) */
|
---|
1005 | )
|
---|
1006 | )
|
---|
1007 | {
|
---|
1008 | /* Read the link entry of the previous entry in the list and check that its
|
---|
1009 | forward pointer points at the MTE we've found. */
|
---|
1010 | rc = DBGFR3MemRead(pUVM, 0 /*idCpu*/,
|
---|
1011 | DBGFR3AddrFromFlat(pUVM, &Addr, uMte2.v64.InLoadOrderLinks.Blink),
|
---|
1012 | &uMte3.v64, RT_SIZEOFMEMB(NTMTE64, InLoadOrderLinks));
|
---|
1013 | if ( RT_SUCCESS(rc)
|
---|
1014 | && uMte3.v64.InLoadOrderLinks.Flink == MteAddr.FlatPtr
|
---|
1015 | && WINNT64_VALID_ADDRESS(uMte3.v64.InLoadOrderLinks.Blink) )
|
---|
1016 | {
|
---|
1017 | Log(("DigWinNt: MteAddr=%RGv KernelAddr=%RGv SizeOfImage=%x &PsLoadedModuleList=%RGv (32-bit)\n",
|
---|
1018 | MteAddr.FlatPtr, KernelAddr.FlatPtr, uMte2.v64.SizeOfImage, Addr.FlatPtr));
|
---|
1019 | pThis->KernelAddr = KernelAddr;
|
---|
1020 | pThis->KernelMteAddr = MteAddr;
|
---|
1021 | pThis->PsLoadedModuleListAddr = Addr;
|
---|
1022 | pThis->f32Bit = false;
|
---|
1023 | pThis->fNt31 = false;
|
---|
1024 | return true;
|
---|
1025 | }
|
---|
1026 | }
|
---|
1027 | else if (RT_SUCCESS(rc))
|
---|
1028 | {
|
---|
1029 | Log2(("DigWinNt: Wrong module: MteAddr=%RGv ImageAddr=%RGv SizeOfImage=%#x '%ls'\n",
|
---|
1030 | MteAddr.FlatPtr, KernelAddr.FlatPtr, uMte2.v64.SizeOfImage, u.wsz));
|
---|
1031 | break; /* Not NT kernel */
|
---|
1032 | }
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | /* next */
|
---|
1036 | DBGFR3AddrAdd(&HitAddr, 8);
|
---|
1037 | if (HitAddr.FlatPtr < uEnd)
|
---|
1038 | rc = DBGFR3MemScan(pUVM, 0 /*idCpu*/, &HitAddr, uEnd - HitAddr.FlatPtr,
|
---|
1039 | 8 /*align*/, &uMte.v64.DllBase, 3 * sizeof(uint32_t), &HitAddr);
|
---|
1040 | else
|
---|
1041 | rc = VERR_DBGF_MEM_NOT_FOUND;
|
---|
1042 | }
|
---|
1043 | }
|
---|
1044 | }
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 | return false;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 |
|
---|
1051 | /**
|
---|
1052 | * @copydoc DBGFOSREG::pfnDestruct
|
---|
1053 | */
|
---|
1054 | static DECLCALLBACK(void) dbgDiggerWinNtDestruct(PUVM pUVM, void *pvData)
|
---|
1055 | {
|
---|
1056 | RT_NOREF2(pUVM, pvData);
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 |
|
---|
1060 | /**
|
---|
1061 | * @copydoc DBGFOSREG::pfnConstruct
|
---|
1062 | */
|
---|
1063 | static DECLCALLBACK(int) dbgDiggerWinNtConstruct(PUVM pUVM, void *pvData)
|
---|
1064 | {
|
---|
1065 | RT_NOREF1(pUVM);
|
---|
1066 | PDBGDIGGERWINNT pThis = (PDBGDIGGERWINNT)pvData;
|
---|
1067 | pThis->fValid = false;
|
---|
1068 | pThis->f32Bit = false;
|
---|
1069 | pThis->enmVer = DBGDIGGERWINNTVER_UNKNOWN;
|
---|
1070 | return VINF_SUCCESS;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 |
|
---|
1074 | const DBGFOSREG g_DBGDiggerWinNt =
|
---|
1075 | {
|
---|
1076 | /* .u32Magic = */ DBGFOSREG_MAGIC,
|
---|
1077 | /* .fFlags = */ 0,
|
---|
1078 | /* .cbData = */ sizeof(DBGDIGGERWINNT),
|
---|
1079 | /* .szName = */ "WinNT",
|
---|
1080 | /* .pfnConstruct = */ dbgDiggerWinNtConstruct,
|
---|
1081 | /* .pfnDestruct = */ dbgDiggerWinNtDestruct,
|
---|
1082 | /* .pfnProbe = */ dbgDiggerWinNtProbe,
|
---|
1083 | /* .pfnInit = */ dbgDiggerWinNtInit,
|
---|
1084 | /* .pfnRefresh = */ dbgDiggerWinNtRefresh,
|
---|
1085 | /* .pfnTerm = */ dbgDiggerWinNtTerm,
|
---|
1086 | /* .pfnQueryVersion = */ dbgDiggerWinNtQueryVersion,
|
---|
1087 | /* .pfnQueryInterface = */ dbgDiggerWinNtQueryInterface,
|
---|
1088 | /* .pfnStackUnwindAssist = */ dbgDiggerWinNtStackUnwindAssist,
|
---|
1089 | /* .u32EndMagic = */ DBGFOSREG_MAGIC
|
---|
1090 | };
|
---|
1091 |
|
---|