PMMPTE PointerPte, LastPte;
PVOID DriverBase;
MMPTE TempPte;
+ KIRQL OldIrql;
+ PFN_NUMBER PageFrameIndex;
PAGED_CODE();
/* Detect session load */
*ImageBase = DriverBase;
DPRINT1("Loading: %wZ at %p with %lx pages\n", FileName, DriverBase, PteCount);
- /* Loop the new driver PTEs */
- TempPte = ValidKernelPte;
- while (PointerPte < LastPte)
- {
- /* Allocate a page */
- MI_SET_USAGE(MI_USAGE_DRIVER_PAGE);
+ /* Lock the PFN database */
+ OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
+
+ /* Some debug stuff */
+ MI_SET_USAGE(MI_USAGE_DRIVER_PAGE);
#if MI_TRACE_PFNS
+ if (FileName->Buffer)
+ {
PWCHAR pos = NULL;
ULONG len = 0;
- if (FileName->Buffer)
- {
- pos = wcsrchr(FileName->Buffer, '\\');
- len = wcslen(pos) * sizeof(WCHAR);
- if (pos) snprintf(MI_PFN_CURRENT_PROCESS_NAME, min(16, len), "%S", pos);
- }
+ pos = wcsrchr(FileName->Buffer, '\\');
+ len = wcslen(pos) * sizeof(WCHAR);
+ if (pos) snprintf(MI_PFN_CURRENT_PROCESS_NAME, min(16, len), "%S", pos);
+ }
#endif
- TempPte.u.Hard.PageFrameNumber = MiAllocatePfn(PointerPte, MM_EXECUTE);
- /* Write it */
+ /* Loop the new driver PTEs */
+ TempPte = ValidKernelPte;
+ while (PointerPte < LastPte)
+ {
+ /* Make sure the PTE is not valid for whatever reason */
+ ASSERT(PointerPte->u.Hard.Valid == 0);
+
+ /* Grab a page */
+ PageFrameIndex = MiRemoveAnyPage(MI_GET_NEXT_COLOR());
+
+ /* Initialize its PFN entry */
+ MiInitializePfn(PageFrameIndex, PointerPte, TRUE);
+
+ /* Write the PTE */
+ TempPte.u.Hard.PageFrameNumber = PageFrameIndex;
MI_WRITE_VALID_PTE(PointerPte, TempPte);
/* Move on */
PointerPte++;
}
+ /* Release the PFN lock */
+ KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
+
/* Copy the image */
RtlCopyMemory(DriverBase, Base, PteCount << PAGE_SHIFT);
/* Unload the symbols */
DbgUnLoadImageSymbols(&TempName,
BaseAddress,
- (ULONG_PTR)ZwCurrentProcess());
+ (ULONG_PTR)PsGetCurrentProcessId());
RtlFreeAnsiString(&TempName);
}
}
LdrEntry = CONTAINING_RECORD(NextEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
DllBase = (ULONG_PTR)LdrEntry->DllBase;
+ /* Only process boot loaded images. Other drivers are processed by
+ MmFreeDriverInitialization */
+ if (LdrEntry->Flags & LDRP_MM_LOADED)
+ {
+ /* Keep going */
+ NextEntry = NextEntry->Flink;
+ continue;
+ }
+
/* Get the NT header */
NtHeader = RtlImageNtHeader((PVOID)DllBase);
if (!NtHeader)
}
else
{
- /* No referencing needed */
- LdrEntry->LoadCount = 0;
+ /* Add a reference for all other modules as well */
+ LdrEntry->LoadCount = 1;
}
/* Remember this came from the loader */
PMMPFN Pfn1;
PAGED_CODE();
+ /* The page fault handler is broken and doesn't page back in! */
+ DPRINT1("WARNING: MiSetPagingOfDriver() called, but paging is broken! ignoring!\n");
+ return;
+
/* Get the driver's base address */
ImageBase = MiPteToAddress(PointerPte);
ASSERT(MI_IS_SESSION_IMAGE_ADDRESS(ImageBase) == FALSE);
/* Notify the debugger */
DbgLoadImageSymbols(&AnsiTemp,
LdrEntry->DllBase,
- (ULONG_PTR)ZwCurrentProcess());
+ (ULONG_PTR)PsGetCurrentProcessId());
LdrEntry->Flags |= LDRP_DEBUG_SYMBOLS_LOADED;
}