Skip to content

Commit 1a02d33

Browse files
committed
[NTOS:KD64] Fix usage of the debugging banner code, based on when KdInitSystem() is called (reactos#7540)
- The debugging banner helpers *CANNOT* be in the INIT section, because it is possible for KdInitSystem() to enable the debugger **MUCH LATER** after boot time. (Reverts part of commit f239ca0 (r72922).) This can happen in two situations: * When the debugger is in CRASHDEBUG mode, i.e. initialized at boot time but not immediately enabled, and a BSOD happens later that enables the debugger with a `KdInitSystem(0, NULL)` call. * When the debugger was possibly manually disabled with a KdDisableDebugger() call, then later re-enabled with a KdEnableDebugger() call. - In the same cases as described above, the KeLoaderBlock is freed after boot time. Thus, KdpGetMemorySizeInMBs() cannot use it and enumerate the MemoryDescriptors to evaluate the number of physical memory pages available on the system. Instead, we can use what the memory manager has already computed, since the latter is already initialized by now. These two fixes avoid (invisible) crashes when (re-)enabling the debugger at non-boot run time.
1 parent 76d35dd commit 1a02d33

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

ntoskrnl/kd64/kdinit.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,27 @@
2323
*
2424
* Strongly inspired by:
2525
* mm\ARM3\mminit.c : MiScanMemoryDescriptors(...)
26-
*
27-
* See also: kd\kdio.c
2826
*/
29-
static CODE_SEG("INIT")
27+
static
3028
SIZE_T
31-
KdpGetMemorySizeInMBs(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
29+
KdpGetMemorySizeInMBs(
30+
_In_opt_ PLOADER_PARAMETER_BLOCK LoaderBlock)
3231
{
3332
PLIST_ENTRY ListEntry;
3433
PMEMORY_ALLOCATION_DESCRIPTOR Descriptor;
3534
SIZE_T NumberOfPhysicalPages = 0;
3635

36+
/*
37+
* If no loader block is present (e.g. the debugger is initialized only
38+
* much later after boot), just use the already-initialized Mm-computed
39+
* number of physical pages. Otherwise do the evaluation ourselves.
40+
*/
41+
if (!LoaderBlock)
42+
{
43+
NumberOfPhysicalPages = MmNumberOfPhysicalPages;
44+
goto ReturnSize;
45+
}
46+
3747
/* Loop the memory descriptors */
3848
for (ListEntry = LoaderBlock->MemoryDescriptorListHead.Flink;
3949
ListEntry != &LoaderBlock->MemoryDescriptorListHead;
@@ -62,12 +72,12 @@ KdpGetMemorySizeInMBs(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
6272
}
6373
}
6474

75+
ReturnSize:
6576
/* Round size up. Assumed to better match actual physical RAM size */
6677
return ALIGN_UP_BY(NumberOfPhysicalPages * PAGE_SIZE, 1024 * 1024) / (1024 * 1024);
6778
}
6879

69-
/* See also: kd\kdio.c */
70-
static CODE_SEG("INIT")
80+
static
7181
VOID
7282
KdpPrintBanner(IN SIZE_T MemSizeMBs)
7383
{

0 commit comments

Comments
 (0)