KernelMain reports more details about memory usage, LdrResolveImports catches kernel32, user32, ole32 and winmm and redirects them to win32.

This commit is contained in:
maksw committed 2026-05-02 15:25:39 +02:00
1 parent 129be91e15
commit c13dd99150
7 files changed
+116 -1190

No files matched your search

+13 -7
View File
@@ -78,9 +78,8 @@ typedef KSTATUS (*DRIVER_ENTRY_FN)(const HAL_API *);
// Phase Helper: Resolve Imports for all registered modules
// ---------------------------------------------------------------------------
static void ResolveSystemImports(BOOT_INFO* bootInfo) {
// Resolve imports for all loaded modules (drivers may import from hal.dll,
// win32.dll imports from syscall.dll, etc).
// Starting at 2 to skip the kernel and HAL base images, as per your design.
// Resolve imports for all loaded modules (drivers may import from hal.dll, win32.dll imports from syscall.dll, etc).
// Starting at 2 to skip the kernel and HAL base images.
for (uint32_t i = 2; i < bootInfo->ModuleCount; i++) {
LdrResolveImports(bootInfo->Modules[i].VirtBase);
}
@@ -112,7 +111,7 @@ static void InitializeDrivers(BOOT_INFO* bootInfo, const HAL_API* hal) {
// Identify .sys files by extension
size_t len = strlen(name);
if (len < 4 || strcmp(name + len - 4, ".sys") != 0) continue;
if (len < 4 || strcmp(name + len - 4, ".sys")) continue;
DbgPrint("KernelMain: Executing DriverEntry for %s\n", name);
@@ -235,7 +234,7 @@ static void LoadAndRegisterDll(const char* filename) {
IoCloseFile(f);
// Map the DLL into memory
// Passing 'true' for user-mode so init.exe can access its code
// Passing 'true' for user-mode
uintptr_t mappedBase = 0;
s = LdrLoadImage(mem, size, &mappedBase, true);
if (!KSUCCESS(s)) {
@@ -384,8 +383,6 @@ extern void KernelMain(BOOT_INFO* bootInfo) {
//
KeInitializeScheduler();
// hal->Screen.Clear(0x000000);
DbgPrint("KernelMain: initialisation complete. %.2f MB free.\n",
(float)MmFreeFrameCount() * PAGE_SIZE / (1024.0f * 1024.0f));
DbgPrint("KernelMain: Loading system DLLs...\n");
LoadAndRegisterDll("syscall.dll");
@@ -406,6 +403,15 @@ extern void KernelMain(BOOT_INFO* bootInfo) {
DbgPrint("KernelMain: Starting scheduler... Handing over control.\n");
_enable();
float freeMB = (float)MmFreeFrameCount() * PAGE_SIZE / (1024.0f * 1024.0f);
float kernelMB = (float)MmGetKernelPoolUsage() / (1024.0f * 1024.0f);
float userMB = (float)MmGetUserHeapUsage() / (1024.0f * 1024.0f);
DbgPrint("Memory Status:\n");
DbgPrint(" -> Free (PMM): %.2f MB\n", freeMB);
DbgPrint(" -> Kernel Pool: %.2f MB\n", kernelMB);
DbgPrint(" -> User Heap: %.2f MB\n", userMB);
while (1) {
//DbgPrint("KernelMain: Idle loop. Free memory: %.2f MB.\n",
//(float)(MmFreeFrameCount() * PAGE_SIZE) / (1024.0f * 1024.0f));
+4 -1
View File
@@ -225,13 +225,16 @@ KSTATUS LdrResolveImports(uintptr_t imageBase) {
PE_HEADER* pe = (PE_HEADER *)(imageBase + peOffset);
OPT_HEADER64* opt = &pe->OptionalHeader;
if (opt->NumberOfRvaAndSizes < 2) return STATUS_SUCCESS; // no imports
if (opt->NumberOfRvaAndSizes < 2) return STATUS_SUCCESS; // no imports
uint32_t impRva = opt->DataDirectory[1].VirtualAddress;
if (!impRva) return STATUS_SUCCESS;
IMPORT_DESC* imp = (IMPORT_DESC *)(imageBase + impRva);
while (imp->Name) {
const char* dllName = (const char *)(imageBase + imp->Name);
if (!_stricmp(dllName, "kernel32.dll") || !_stricmp(dllName, "user32.dll")
|| !_stricmp(dllName, "ole32.dll") || !_stricmp(dllName, "winmm.dll"))
dllName = "win32.dll";
uint64_t* thunk = (uint64_t *)(imageBase + imp->FirstThunk);
uint64_t* origThunk = (uint64_t *)(imageBase + (imp->OriginalFirstThunk
? imp->OriginalFirstThunk
+10
View File
@@ -443,3 +443,13 @@ void* MmReallocatePool(void* ptr, size_t newSize) { return HeapRealloc(&gPoolHea
void* MmAllocateUserMemory(size_t size) { return HeapAlloc(&gUserHeap, size); }
void MmFreeUserMemory(void* ptr, size_t size) { (void)size; HeapFree(&gUserHeap, ptr); }
void* MmReallocateUserMemory(void* ptr, size_t newSize) { return HeapRealloc(&gUserHeap, ptr, newSize); }
size_t MmGetKernelPoolUsage(void) {
// Returns bytes committed to the kernel pool
return (size_t)(gPoolHeap.Brk - gPoolHeap.Start);
}
size_t MmGetUserHeapUsage(void) {
// Returns bytes committed to the user heap
return (size_t)(gUserHeap.Brk - gUserHeap.Start);
}
+3
View File
@@ -68,3 +68,6 @@ void* MmReallocatePool(void* ptr, size_t newSize);
void* MmAllocateUserMemory(size_t size);
void MmFreeUserMemory(void* ptr, size_t size);
void* MmReallocateUserMemory(void* ptr, size_t newSize);
size_t MmGetKernelPoolUsage(void);
size_t MmGetUserHeapUsage(void);