2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/sysldr.c
5 * PURPOSE: Contains the Kernel Loader (SYSLDR) for loading PE files.
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * ReactOS Portable Systems Group
10 /* INCLUDES *******************************************************************/
16 #line 16 "ARMĀ³::LOADER"
17 #define MODULE_INVOLVED_IN_ARM3
18 #include "../ARM3/miarm.h"
20 /* GCC's incompetence strikes again */
23 sprintf_nt(IN PCHAR Buffer
,
29 vsprintf(Buffer
, Format
, ap
);
33 /* GLOBALS ********************************************************************/
35 LIST_ENTRY PsLoadedModuleList
;
36 LIST_ENTRY MmLoadedUserImageList
;
37 KSPIN_LOCK PsLoadedModuleSpinLock
;
38 ERESOURCE PsLoadedModuleResource
;
39 ULONG_PTR PsNtosImageBase
;
40 KMUTANT MmSystemLoadLock
;
42 PFN_NUMBER MmTotalSystemDriverPages
;
44 PVOID MmUnloadedDrivers
;
45 PVOID MmLastUnloadedDrivers
;
47 BOOLEAN MmMakeLowMemory
;
48 BOOLEAN MmEnforceWriteProtection
= TRUE
;
50 PMMPTE MiKernelResourceStartPte
, MiKernelResourceEndPte
;
51 ULONG_PTR ExPoolCodeStart
, ExPoolCodeEnd
, MmPoolCodeStart
, MmPoolCodeEnd
;
52 ULONG_PTR MmPteCodeStart
, MmPteCodeEnd
;
54 /* FUNCTIONS ******************************************************************/
58 MiCacheImageSymbols(IN PVOID BaseAddress
)
61 PVOID DebugDirectory
= NULL
;
64 /* Make sure it's safe to access the image */
67 /* Get the debug directory */
68 DebugDirectory
= RtlImageDirectoryEntryToData(BaseAddress
,
70 IMAGE_DIRECTORY_ENTRY_DEBUG
,
73 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
79 /* Return the directory */
80 return DebugDirectory
;
85 MiLoadImageSection(IN OUT PVOID
*SectionPtr
,
87 IN PUNICODE_STRING FileName
,
88 IN BOOLEAN SessionLoad
,
89 IN PLDR_DATA_TABLE_ENTRY LdrEntry
)
91 PROS_SECTION_OBJECT Section
= *SectionPtr
;
97 LARGE_INTEGER SectionOffset
= {{0, 0}};
98 BOOLEAN LoadSymbols
= FALSE
;
100 PMMPTE PointerPte
, LastPte
;
105 /* Detect session load */
109 DPRINT1("Session loading not yet supported!\n");
113 /* Not session load, shouldn't have an entry */
114 ASSERT(LdrEntry
== NULL
);
116 /* Attach to the system process */
117 KeStackAttachProcess(&PsInitialSystemProcess
->Pcb
, &ApcState
);
119 /* Check if we need to load symbols */
120 if (NtGlobalFlag
& FLG_ENABLE_KDEBUG_SYMBOL_LOAD
)
124 NtGlobalFlag
&= ~FLG_ENABLE_KDEBUG_SYMBOL_LOAD
;
128 Process
= PsGetCurrentProcess();
129 Status
= MmMapViewOfSection(Section
,
140 /* Re-enable the flag */
141 if (LoadSymbols
) NtGlobalFlag
|= FLG_ENABLE_KDEBUG_SYMBOL_LOAD
;
143 /* Check if we failed with distinguished status code */
144 if (Status
== STATUS_IMAGE_MACHINE_TYPE_MISMATCH
)
146 /* Change it to something more generic */
147 Status
= STATUS_INVALID_IMAGE_FORMAT
;
150 /* Now check if we failed */
151 if (!NT_SUCCESS(Status
))
153 /* Detach and return */
154 KeUnstackDetachProcess(&ApcState
);
158 /* Reserve system PTEs needed */
159 PteCount
= ROUND_TO_PAGES(Section
->ImageSection
->ImageSize
) >> PAGE_SHIFT
;
160 PointerPte
= MiReserveSystemPtes(PteCount
, SystemPteSpace
);
161 if (!PointerPte
) return STATUS_INSUFFICIENT_RESOURCES
;
163 /* New driver base */
164 LastPte
= PointerPte
+ PteCount
;
165 DriverBase
= MiPteToAddress(PointerPte
);
167 /* The driver is here */
168 *ImageBase
= DriverBase
;
169 DPRINT1("Loading: %wZ at %p with %lx pages\n", FileName
, DriverBase
, PteCount
);
171 /* Loop the new driver PTEs */
172 TempPte
= ValidKernelPte
;
173 while (PointerPte
< LastPte
)
175 /* Allocate a page */
176 MI_SET_USAGE(MI_USAGE_DRIVER_PAGE
);
180 if (FileName
->Buffer
)
182 pos
= wcsrchr(FileName
->Buffer
, '\\');
183 len
= wcslen(pos
) * sizeof(WCHAR
);
184 if (pos
) snprintf(MI_PFN_CURRENT_PROCESS_NAME
, min(16, len
), "%S", pos
);
187 TempPte
.u
.Hard
.PageFrameNumber
= MiAllocatePfn(PointerPte
, MM_EXECUTE
);
190 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
197 RtlCopyMemory(DriverBase
, Base
, PteCount
<< PAGE_SHIFT
);
199 /* Now unmap the view */
200 Status
= MmUnmapViewOfSection(Process
, Base
);
201 ASSERT(NT_SUCCESS(Status
));
203 /* Detach and return status */
204 KeUnstackDetachProcess(&ApcState
);
210 MiLocateExportName(IN PVOID DllBase
,
214 PUSHORT OrdinalTable
;
215 PIMAGE_EXPORT_DIRECTORY ExportDirectory
;
216 LONG Low
= 0, Mid
= 0, High
, Ret
;
223 /* Get the export directory */
224 ExportDirectory
= RtlImageDirectoryEntryToData(DllBase
,
226 IMAGE_DIRECTORY_ENTRY_EXPORT
,
228 if (!ExportDirectory
) return NULL
;
230 /* Setup name tables */
231 NameTable
= (PULONG
)((ULONG_PTR
)DllBase
+
232 ExportDirectory
->AddressOfNames
);
233 OrdinalTable
= (PUSHORT
)((ULONG_PTR
)DllBase
+
234 ExportDirectory
->AddressOfNameOrdinals
);
236 /* Do a binary search */
237 High
= ExportDirectory
->NumberOfNames
- 1;
240 /* Get new middle value */
241 Mid
= (Low
+ High
) >> 1;
244 Ret
= strcmp(ExportName
, (PCHAR
)DllBase
+ NameTable
[Mid
]);
262 /* Check if we couldn't find it */
263 if (High
< Low
) return NULL
;
265 /* Otherwise, this is the ordinal */
266 Ordinal
= OrdinalTable
[Mid
];
268 /* Resolve the address and write it */
269 ExportTable
= (PULONG
)((ULONG_PTR
)DllBase
+
270 ExportDirectory
->AddressOfFunctions
);
271 Function
= (PVOID
)((ULONG_PTR
)DllBase
+ ExportTable
[Ordinal
]);
273 /* Check if the function is actually a forwarder */
274 if (((ULONG_PTR
)Function
> (ULONG_PTR
)ExportDirectory
) &&
275 ((ULONG_PTR
)Function
< ((ULONG_PTR
)ExportDirectory
+ ExportSize
)))
287 MmCallDllInitialize(IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
288 IN PLIST_ENTRY ListHead
)
290 UNICODE_STRING ServicesKeyName
= RTL_CONSTANT_STRING(
291 L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
292 PMM_DLL_INITIALIZE DllInit
;
293 UNICODE_STRING RegPath
, ImportName
;
296 /* Try to see if the image exports a DllInitialize routine */
297 DllInit
= (PMM_DLL_INITIALIZE
)MiLocateExportName(LdrEntry
->DllBase
,
299 if (!DllInit
) return STATUS_SUCCESS
;
301 /* Do a temporary copy of BaseDllName called ImportName
302 * because we'll alter the length of the string
304 ImportName
.Length
= LdrEntry
->BaseDllName
.Length
;
305 ImportName
.MaximumLength
= LdrEntry
->BaseDllName
.MaximumLength
;
306 ImportName
.Buffer
= LdrEntry
->BaseDllName
.Buffer
;
308 /* Obtain the path to this dll's service in the registry */
309 RegPath
.MaximumLength
= ServicesKeyName
.Length
+
310 ImportName
.Length
+ sizeof(UNICODE_NULL
);
311 RegPath
.Buffer
= ExAllocatePoolWithTag(NonPagedPool
,
312 RegPath
.MaximumLength
,
315 /* Check if this allocation was unsuccessful */
316 if (!RegPath
.Buffer
) return STATUS_INSUFFICIENT_RESOURCES
;
318 /* Build and append the service name itself */
319 RegPath
.Length
= ServicesKeyName
.Length
;
320 RtlCopyMemory(RegPath
.Buffer
,
321 ServicesKeyName
.Buffer
,
322 ServicesKeyName
.Length
);
324 /* Check if there is a dot in the filename */
325 if (wcschr(ImportName
.Buffer
, L
'.'))
327 /* Remove the extension */
328 ImportName
.Length
= (wcschr(ImportName
.Buffer
, L
'.') -
329 ImportName
.Buffer
) * sizeof(WCHAR
);
332 /* Append service name (the basename without extension) */
333 RtlAppendUnicodeStringToString(&RegPath
, &ImportName
);
335 /* Now call the DllInit func */
336 DPRINT("Calling DllInit(%wZ)\n", &RegPath
);
337 Status
= DllInit(&RegPath
);
340 ExFreePool(RegPath
.Buffer
);
342 /* Return status value which DllInitialize returned */
348 MiCallDllUnloadAndUnloadDll(IN PLDR_DATA_TABLE_ENTRY LdrEntry
)
354 /* Get the unload routine */
355 Func
= (PMM_DLL_UNLOAD
)MiLocateExportName(LdrEntry
->DllBase
, "DllUnload");
356 if (!Func
) return FALSE
;
358 /* Call it and check for success */
360 if (!NT_SUCCESS(Status
)) return FALSE
;
362 /* Lie about the load count so we can unload the image */
363 ASSERT(LdrEntry
->LoadCount
== 0);
364 LdrEntry
->LoadCount
= 1;
366 /* Unload it and return true */
367 MmUnloadSystemImage(LdrEntry
);
373 MiDereferenceImports(IN PLOAD_IMPORTS ImportList
)
376 LOAD_IMPORTS SingleEntry
;
377 PLDR_DATA_TABLE_ENTRY LdrEntry
;
378 PVOID CurrentImports
;
381 /* Check if there's no imports or if we're a boot driver */
382 if ((ImportList
== MM_SYSLDR_NO_IMPORTS
) ||
383 (ImportList
== MM_SYSLDR_BOOT_LOADED
) ||
384 (ImportList
->Count
== 0))
386 /* Then there's nothing to do */
387 return STATUS_SUCCESS
;
390 /* Check for single-entry */
391 if ((ULONG_PTR
)ImportList
& MM_SYSLDR_SINGLE_ENTRY
)
394 SingleEntry
.Count
= 1;
395 SingleEntry
.Entry
[0] = (PVOID
)((ULONG_PTR
)ImportList
&~ MM_SYSLDR_SINGLE_ENTRY
);
397 /* Use this as the import list */
398 ImportList
= &SingleEntry
;
401 /* Loop the import list */
402 for (i
= 0; (i
< ImportList
->Count
) && (ImportList
->Entry
[i
]); i
++)
405 LdrEntry
= ImportList
->Entry
[i
];
406 DPRINT1("%wZ <%wZ>\n", &LdrEntry
->FullDllName
, &LdrEntry
->BaseDllName
);
408 /* Skip boot loaded images */
409 if (LdrEntry
->LoadedImports
== MM_SYSLDR_BOOT_LOADED
) continue;
411 /* Dereference the entry */
412 ASSERT(LdrEntry
->LoadCount
>= 1);
413 if (!--LdrEntry
->LoadCount
)
415 /* Save the import data in case unload fails */
416 CurrentImports
= LdrEntry
->LoadedImports
;
418 /* This is the last entry */
419 LdrEntry
->LoadedImports
= MM_SYSLDR_NO_IMPORTS
;
420 if (MiCallDllUnloadAndUnloadDll(LdrEntry
))
422 /* Unloading worked, parse this DLL's imports too */
423 MiDereferenceImports(CurrentImports
);
425 /* Check if we had valid imports */
426 if ((CurrentImports
!= MM_SYSLDR_BOOT_LOADED
) ||
427 (CurrentImports
!= MM_SYSLDR_NO_IMPORTS
) ||
428 !((ULONG_PTR
)LdrEntry
->LoadedImports
& MM_SYSLDR_SINGLE_ENTRY
))
431 ExFreePool(CurrentImports
);
436 /* Unload failed, restore imports */
437 LdrEntry
->LoadedImports
= CurrentImports
;
443 return STATUS_SUCCESS
;
448 MiClearImports(IN PLDR_DATA_TABLE_ENTRY LdrEntry
)
452 /* Check if there's no imports or we're a boot driver or only one entry */
453 if ((LdrEntry
->LoadedImports
== MM_SYSLDR_BOOT_LOADED
) ||
454 (LdrEntry
->LoadedImports
== MM_SYSLDR_NO_IMPORTS
) ||
455 ((ULONG_PTR
)LdrEntry
->LoadedImports
& MM_SYSLDR_SINGLE_ENTRY
))
461 /* Otherwise, free the import list */
462 ExFreePool(LdrEntry
->LoadedImports
);
463 LdrEntry
->LoadedImports
= MM_SYSLDR_BOOT_LOADED
;
468 MiFindExportedRoutineByName(IN PVOID DllBase
,
469 IN PANSI_STRING ExportName
)
472 PUSHORT OrdinalTable
;
473 PIMAGE_EXPORT_DIRECTORY ExportDirectory
;
474 LONG Low
= 0, Mid
= 0, High
, Ret
;
481 /* Get the export directory */
482 ExportDirectory
= RtlImageDirectoryEntryToData(DllBase
,
484 IMAGE_DIRECTORY_ENTRY_EXPORT
,
486 if (!ExportDirectory
) return NULL
;
488 /* Setup name tables */
489 NameTable
= (PULONG
)((ULONG_PTR
)DllBase
+
490 ExportDirectory
->AddressOfNames
);
491 OrdinalTable
= (PUSHORT
)((ULONG_PTR
)DllBase
+
492 ExportDirectory
->AddressOfNameOrdinals
);
494 /* Do a binary search */
495 High
= ExportDirectory
->NumberOfNames
- 1;
498 /* Get new middle value */
499 Mid
= (Low
+ High
) >> 1;
502 Ret
= strcmp(ExportName
->Buffer
, (PCHAR
)DllBase
+ NameTable
[Mid
]);
520 /* Check if we couldn't find it */
521 if (High
< Low
) return NULL
;
523 /* Otherwise, this is the ordinal */
524 Ordinal
= OrdinalTable
[Mid
];
526 /* Validate the ordinal */
527 if (Ordinal
>= ExportDirectory
->NumberOfFunctions
) return NULL
;
529 /* Resolve the address and write it */
530 ExportTable
= (PULONG
)((ULONG_PTR
)DllBase
+
531 ExportDirectory
->AddressOfFunctions
);
532 Function
= (PVOID
)((ULONG_PTR
)DllBase
+ ExportTable
[Ordinal
]);
535 ASSERT(!(Function
> (PVOID
)ExportDirectory
) &&
536 (Function
< (PVOID
)((ULONG_PTR
)ExportDirectory
+ ExportSize
)));
542 MiProcessLoaderEntry(IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
547 /* Acquire module list lock */
548 KeEnterCriticalRegion();
549 ExAcquireResourceExclusiveLite(&PsLoadedModuleResource
, TRUE
);
551 /* Acquire the spinlock too as we will insert or remove the entry */
552 OldIrql
= KeAcquireSpinLockRaiseToSynch(&PsLoadedModuleSpinLock
);
554 /* Insert or remove from the list */
555 Insert
? InsertTailList(&PsLoadedModuleList
, &LdrEntry
->InLoadOrderLinks
) :
556 RemoveEntryList(&LdrEntry
->InLoadOrderLinks
);
559 KeReleaseSpinLock(&PsLoadedModuleSpinLock
, OldIrql
);
560 ExReleaseResourceLite(&PsLoadedModuleResource
);
561 KeLeaveCriticalRegion();
567 MiUpdateThunks(IN PLOADER_PARAMETER_BLOCK LoaderBlock
,
572 ULONG_PTR OldBaseTop
, Delta
;
573 PLDR_DATA_TABLE_ENTRY LdrEntry
;
574 PLIST_ENTRY NextEntry
;
577 // FIXME: MINGW-W64 must fix LD to generate drivers that Windows can load,
578 // since a real version of Windows would fail at this point, but they seem
579 // busy implementing features such as "HotPatch" support in GCC 4.6 instead,
580 // a feature which isn't even used by Windows. Priorities, priorities...
581 // Please note that Microsoft WDK EULA and license prohibits using
582 // the information contained within it for the generation of "non-Windows"
583 // drivers, which is precisely what LD will generate, since an LD driver
584 // will not load on Windows.
586 #ifdef _WORKING_LINKER_
589 PULONG_PTR ImageThunk
;
590 PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor
;
592 /* Calculate the top and delta */
593 OldBaseTop
= (ULONG_PTR
)OldBase
+ Size
- 1;
594 Delta
= (ULONG_PTR
)NewBase
- (ULONG_PTR
)OldBase
;
596 /* Loop the loader block */
597 for (NextEntry
= LoaderBlock
->LoadOrderListHead
.Flink
;
598 NextEntry
!= &LoaderBlock
->LoadOrderListHead
;
599 NextEntry
= NextEntry
->Flink
)
601 /* Get the loader entry */
602 LdrEntry
= CONTAINING_RECORD(NextEntry
,
603 LDR_DATA_TABLE_ENTRY
,
605 #ifdef _WORKING_LINKER_
607 ImageThunk
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
609 IMAGE_DIRECTORY_ENTRY_IAT
,
611 if (!ImageThunk
) continue;
613 /* Make sure we have an IAT */
614 DPRINT("[Mm0]: Updating thunks in: %wZ\n", &LdrEntry
->BaseDllName
);
615 for (i
= 0; i
< ImportSize
; i
++, ImageThunk
++)
617 /* Check if it's within this module */
618 if ((*ImageThunk
>= (ULONG_PTR
)OldBase
) && (*ImageThunk
<= OldBaseTop
))
621 DPRINT("[Mm0]: Updating IAT at: %p. Old Entry: %p. New Entry: %p.\n",
622 ImageThunk
, *ImageThunk
, *ImageThunk
+ Delta
);
623 *ImageThunk
+= Delta
;
627 /* Get the import table */
628 ImportDescriptor
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
630 IMAGE_DIRECTORY_ENTRY_IMPORT
,
632 if (!ImportDescriptor
) continue;
634 /* Make sure we have an IAT */
635 DPRINT("[Mm0]: Updating thunks in: %wZ\n", &LdrEntry
->BaseDllName
);
636 while ((ImportDescriptor
->Name
) &&
637 (ImportDescriptor
->OriginalFirstThunk
))
639 /* Get the image thunk */
640 ImageThunk
= (PVOID
)((ULONG_PTR
)LdrEntry
->DllBase
+
641 ImportDescriptor
->FirstThunk
);
644 /* Check if it's within this module */
645 if ((*ImageThunk
>= (ULONG_PTR
)OldBase
) && (*ImageThunk
<= OldBaseTop
))
648 DPRINT("[Mm0]: Updating IAT at: %p. Old Entry: %p. New Entry: %p.\n",
649 ImageThunk
, *ImageThunk
, *ImageThunk
+ Delta
);
650 *ImageThunk
+= Delta
;
653 /* Go to the next thunk */
657 /* Go to the next import */
666 MiSnapThunk(IN PVOID DllBase
,
668 IN PIMAGE_THUNK_DATA Name
,
669 IN PIMAGE_THUNK_DATA Address
,
670 IN PIMAGE_EXPORT_DIRECTORY ExportDirectory
,
672 IN BOOLEAN SnapForwarder
,
673 OUT PCHAR
*MissingApi
)
678 PUSHORT OrdinalTable
;
679 PIMAGE_IMPORT_BY_NAME NameImport
;
681 ULONG Low
= 0, Mid
= 0, High
;
684 PCHAR MissingForwarder
;
685 CHAR NameBuffer
[MAXIMUM_FILENAME_LENGTH
];
688 UNICODE_STRING ForwarderName
;
689 PLIST_ENTRY NextEntry
;
690 PLDR_DATA_TABLE_ENTRY LdrEntry
;
691 ULONG ForwardExportSize
;
692 PIMAGE_EXPORT_DIRECTORY ForwardExportDirectory
;
693 PIMAGE_IMPORT_BY_NAME ForwardName
;
695 IMAGE_THUNK_DATA ForwardThunk
;
698 /* Check if this is an ordinal */
699 IsOrdinal
= IMAGE_SNAP_BY_ORDINAL(Name
->u1
.Ordinal
);
700 if ((IsOrdinal
) && !(SnapForwarder
))
702 /* Get the ordinal number and set it as missing */
703 Ordinal
= (USHORT
)(IMAGE_ORDINAL(Name
->u1
.Ordinal
) -
704 ExportDirectory
->Base
);
705 *MissingApi
= (PCHAR
)(ULONG_PTR
)Ordinal
;
709 /* Get the VA if we don't have to snap */
710 if (!SnapForwarder
) Name
->u1
.AddressOfData
+= (ULONG_PTR
)ImageBase
;
711 NameImport
= (PIMAGE_IMPORT_BY_NAME
)Name
->u1
.AddressOfData
;
713 /* Copy the procedure name */
715 (PCHAR
)&NameImport
->Name
[0],
716 MAXIMUM_FILENAME_LENGTH
- 1);
718 /* Setup name tables */
719 DPRINT("Import name: %s\n", NameImport
->Name
);
720 NameTable
= (PULONG
)((ULONG_PTR
)DllBase
+
721 ExportDirectory
->AddressOfNames
);
722 OrdinalTable
= (PUSHORT
)((ULONG_PTR
)DllBase
+
723 ExportDirectory
->AddressOfNameOrdinals
);
725 /* Get the hint and check if it's valid */
726 Hint
= NameImport
->Hint
;
727 if ((Hint
< ExportDirectory
->NumberOfNames
) &&
728 !(strcmp((PCHAR
)NameImport
->Name
, (PCHAR
)DllBase
+ NameTable
[Hint
])))
730 /* We have a match, get the ordinal number from here */
731 Ordinal
= OrdinalTable
[Hint
];
735 /* Do a binary search */
736 High
= ExportDirectory
->NumberOfNames
- 1;
739 /* Get new middle value */
740 Mid
= (Low
+ High
) >> 1;
743 Ret
= strcmp((PCHAR
)NameImport
->Name
, (PCHAR
)DllBase
+ NameTable
[Mid
]);
761 /* Check if we couldn't find it */
764 DPRINT1("Warning: Driver failed to load, %s not found\n", NameImport
->Name
);
765 return STATUS_DRIVER_ENTRYPOINT_NOT_FOUND
;
768 /* Otherwise, this is the ordinal */
769 Ordinal
= OrdinalTable
[Mid
];
773 /* Check if the ordinal is invalid */
774 if (Ordinal
>= ExportDirectory
->NumberOfFunctions
)
777 Status
= STATUS_DRIVER_ORDINAL_NOT_FOUND
;
781 /* In case the forwarder is missing */
782 MissingForwarder
= NameBuffer
;
784 /* Resolve the address and write it */
785 ExportTable
= (PULONG
)((ULONG_PTR
)DllBase
+
786 ExportDirectory
->AddressOfFunctions
);
787 Address
->u1
.Function
= (ULONG_PTR
)DllBase
+ ExportTable
[Ordinal
];
789 /* Assume success from now on */
790 Status
= STATUS_SUCCESS
;
792 /* Check if the function is actually a forwarder */
793 if ((Address
->u1
.Function
> (ULONG_PTR
)ExportDirectory
) &&
794 (Address
->u1
.Function
< ((ULONG_PTR
)ExportDirectory
+ ExportSize
)))
796 /* Now assume failure in case the forwarder doesn't exist */
797 Status
= STATUS_DRIVER_ENTRYPOINT_NOT_FOUND
;
799 /* Build the forwarder name */
800 DllName
.Buffer
= (PCHAR
)Address
->u1
.Function
;
801 DllName
.Length
= strchr(DllName
.Buffer
, '.') -
804 DllName
.MaximumLength
= DllName
.Length
;
807 if (!NT_SUCCESS(RtlAnsiStringToUnicodeString(&ForwarderName
,
811 /* We failed, just return an error */
815 /* Loop the module list */
816 NextEntry
= PsLoadedModuleList
.Flink
;
817 while (NextEntry
!= &PsLoadedModuleList
)
819 /* Get the loader entry */
820 LdrEntry
= CONTAINING_RECORD(NextEntry
,
821 LDR_DATA_TABLE_ENTRY
,
824 /* Check if it matches */
825 if (RtlPrefixString((PSTRING
)&ForwarderName
,
826 (PSTRING
)&LdrEntry
->BaseDllName
,
829 /* Get the forwarder export directory */
830 ForwardExportDirectory
=
831 RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
833 IMAGE_DIRECTORY_ENTRY_EXPORT
,
835 if (!ForwardExportDirectory
) break;
837 /* Allocate a name entry */
838 ForwardLength
= strlen(DllName
.Buffer
+ DllName
.Length
) +
840 ForwardName
= ExAllocatePoolWithTag(PagedPool
,
841 sizeof(*ForwardName
) +
844 if (!ForwardName
) break;
847 RtlCopyMemory(&ForwardName
->Name
[0],
848 DllName
.Buffer
+ DllName
.Length
,
850 ForwardName
->Hint
= 0;
852 /* Set the new address */
853 ForwardThunk
.u1
.AddressOfData
= (ULONG_PTR
)ForwardName
;
855 /* Snap the forwarder */
856 Status
= MiSnapThunk(LdrEntry
->DllBase
,
860 ForwardExportDirectory
,
865 /* Free the forwarder name and set the thunk */
866 ExFreePoolWithTag(ForwardName
, TAG_LDR_WSTR
);
867 Address
->u1
= ForwardThunk
.u1
;
871 /* Go to the next entry */
872 NextEntry
= NextEntry
->Flink
;
876 RtlFreeUnicodeString(&ForwarderName
);
886 MmUnloadSystemImage(IN PVOID ImageHandle
)
888 PLDR_DATA_TABLE_ENTRY LdrEntry
= ImageHandle
;
889 PVOID BaseAddress
= LdrEntry
->DllBase
;
892 BOOLEAN HadEntry
= FALSE
;
894 /* Acquire the loader lock */
895 KeEnterCriticalRegion();
896 KeWaitForSingleObject(&MmSystemLoadLock
,
902 /* Check if this driver was loaded at boot and didn't get imports parsed */
903 if (LdrEntry
->LoadedImports
== MM_SYSLDR_BOOT_LOADED
) goto Done
;
905 /* We should still be alive */
906 ASSERT(LdrEntry
->LoadCount
!= 0);
907 LdrEntry
->LoadCount
--;
909 /* Check if we're still loaded */
910 if (LdrEntry
->LoadCount
) goto Done
;
912 /* We should cleanup... are symbols loaded */
913 if (LdrEntry
->Flags
& LDRP_DEBUG_SYMBOLS_LOADED
)
915 /* Create the ANSI name */
916 Status
= RtlUnicodeStringToAnsiString(&TempName
,
917 &LdrEntry
->BaseDllName
,
919 if (NT_SUCCESS(Status
))
921 /* Unload the symbols */
922 DbgUnLoadImageSymbols(&TempName
,
924 (ULONG_PTR
)ZwCurrentProcess());
925 RtlFreeAnsiString(&TempName
);
929 /* FIXME: Free the driver */
930 DPRINT1("Leaking driver: %wZ\n", &LdrEntry
->BaseDllName
);
931 //MmFreeSection(LdrEntry->DllBase);
933 /* Check if we're linked in */
934 if (LdrEntry
->InLoadOrderLinks
.Flink
)
937 MiProcessLoaderEntry(LdrEntry
, FALSE
);
941 /* Dereference and clear the imports */
942 MiDereferenceImports(LdrEntry
->LoadedImports
);
943 MiClearImports(LdrEntry
);
945 /* Check if the entry needs to go away */
948 /* Check if it had a name */
949 if (LdrEntry
->FullDllName
.Buffer
)
952 ExFreePool(LdrEntry
->FullDllName
.Buffer
);
955 /* Check if we had a section */
956 if (LdrEntry
->SectionPointer
)
959 ObDereferenceObject(LdrEntry
->SectionPointer
);
963 ExFreePool(LdrEntry
);
966 /* Release the system lock and return */
968 KeReleaseMutant(&MmSystemLoadLock
, 1, FALSE
, FALSE
);
969 KeLeaveCriticalRegion();
970 return STATUS_SUCCESS
;
975 MiResolveImageReferences(IN PVOID ImageBase
,
976 IN PUNICODE_STRING ImageFileDirectory
,
977 IN PUNICODE_STRING NamePrefix OPTIONAL
,
978 OUT PCHAR
*MissingApi
,
979 OUT PWCHAR
*MissingDriver
,
980 OUT PLOAD_IMPORTS
*LoadImports
)
982 PCHAR MissingApiBuffer
= *MissingApi
, ImportName
;
983 PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor
, CurrentImport
;
984 ULONG ImportSize
, ImportCount
= 0, LoadedImportsSize
, ExportSize
;
985 PLOAD_IMPORTS LoadedImports
, NewImports
;
986 ULONG GdiLink
, NormalLink
, i
;
987 BOOLEAN ReferenceNeeded
, Loaded
;
988 ANSI_STRING TempString
;
989 UNICODE_STRING NameString
, DllName
;
990 PLDR_DATA_TABLE_ENTRY LdrEntry
= NULL
, DllEntry
, ImportEntry
= NULL
;
991 PVOID ImportBase
, DllBase
;
992 PLIST_ENTRY NextEntry
;
993 PIMAGE_EXPORT_DIRECTORY ExportDirectory
;
995 PIMAGE_THUNK_DATA OrigThunk
, FirstThunk
;
997 DPRINT("%s - ImageBase: %p. ImageFileDirectory: %wZ\n",
998 __FUNCTION__
, ImageBase
, ImageFileDirectory
);
1000 /* Assume no imports */
1001 *LoadImports
= MM_SYSLDR_NO_IMPORTS
;
1003 /* Get the import descriptor */
1004 ImportDescriptor
= RtlImageDirectoryEntryToData(ImageBase
,
1006 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1008 if (!ImportDescriptor
) return STATUS_SUCCESS
;
1010 /* Loop all imports to count them */
1011 for (CurrentImport
= ImportDescriptor
;
1012 (CurrentImport
->Name
) && (CurrentImport
->OriginalFirstThunk
);
1019 /* Make sure we have non-zero imports */
1022 /* Calculate and allocate the list we'll need */
1023 LoadedImportsSize
= ImportCount
* sizeof(PVOID
) + sizeof(SIZE_T
);
1024 LoadedImports
= ExAllocatePoolWithTag(PagedPool
,
1030 RtlZeroMemory(LoadedImports
, LoadedImportsSize
);
1031 LoadedImports
->Count
= ImportCount
;
1037 LoadedImports
= NULL
;
1040 /* Reset the import count and loop descriptors again */
1041 ImportCount
= GdiLink
= NormalLink
= 0;
1042 while ((ImportDescriptor
->Name
) && (ImportDescriptor
->OriginalFirstThunk
))
1045 ImportName
= (PCHAR
)((ULONG_PTR
)ImageBase
+ ImportDescriptor
->Name
);
1047 /* Check if this is a GDI driver */
1049 !(_strnicmp(ImportName
, "win32k", sizeof("win32k") - 1));
1051 /* We can also allow dxapi (for Windows compat, allow IRT and coverage )*/
1052 NormalLink
= NormalLink
|
1053 ((_strnicmp(ImportName
, "win32k", sizeof("win32k") - 1)) &&
1054 (_strnicmp(ImportName
, "dxapi", sizeof("dxapi") - 1)) &&
1055 (_strnicmp(ImportName
, "coverage", sizeof("coverage") - 1)) &&
1056 (_strnicmp(ImportName
, "irt", sizeof("irt") - 1)));
1058 /* Check if this is a valid GDI driver */
1059 if ((GdiLink
) && (NormalLink
))
1061 /* It's not, it's importing stuff it shouldn't be! */
1062 MiDereferenceImports(LoadedImports
);
1063 if (LoadedImports
) ExFreePool(LoadedImports
);
1064 return STATUS_PROCEDURE_NOT_FOUND
;
1067 /* Check for user-mode printer or video card drivers, which don't belong */
1068 if (!(_strnicmp(ImportName
, "ntdll", sizeof("ntdll") - 1)) ||
1069 !(_strnicmp(ImportName
, "winsrv", sizeof("winsrv") - 1)) ||
1070 !(_strnicmp(ImportName
, "advapi32", sizeof("advapi32") - 1)) ||
1071 !(_strnicmp(ImportName
, "kernel32", sizeof("kernel32") - 1)) ||
1072 !(_strnicmp(ImportName
, "user32", sizeof("user32") - 1)) ||
1073 !(_strnicmp(ImportName
, "gdi32", sizeof("gdi32") - 1)))
1075 /* This is not kernel code */
1076 MiDereferenceImports(LoadedImports
);
1077 if (LoadedImports
) ExFreePool(LoadedImports
);
1078 return STATUS_PROCEDURE_NOT_FOUND
;
1081 /* Check if this is a "core" import, which doesn't get referenced */
1082 if (!(_strnicmp(ImportName
, "ntoskrnl", sizeof("ntoskrnl") - 1)) ||
1083 !(_strnicmp(ImportName
, "win32k", sizeof("win32k") - 1)) ||
1084 !(_strnicmp(ImportName
, "hal", sizeof("hal") - 1)))
1086 /* Don't reference this */
1087 ReferenceNeeded
= FALSE
;
1091 /* Reference these modules */
1092 ReferenceNeeded
= TRUE
;
1095 /* Now setup a unicode string for the import */
1096 RtlInitAnsiString(&TempString
, ImportName
);
1097 Status
= RtlAnsiStringToUnicodeString(&NameString
, &TempString
, TRUE
);
1098 if (!NT_SUCCESS(Status
))
1101 MiDereferenceImports(LoadedImports
);
1102 if (LoadedImports
) ExFreePoolWithTag(LoadedImports
, TAG_LDR_WSTR
);
1106 /* We don't support name prefixes yet */
1107 if (NamePrefix
) DPRINT1("Name Prefix not yet supported!\n");
1109 /* Remember that we haven't loaded the import at this point */
1114 /* Loop the driver list */
1115 NextEntry
= PsLoadedModuleList
.Flink
;
1116 while (NextEntry
!= &PsLoadedModuleList
)
1118 /* Get the loader entry and compare the name */
1119 LdrEntry
= CONTAINING_RECORD(NextEntry
,
1120 LDR_DATA_TABLE_ENTRY
,
1122 if (RtlEqualUnicodeString(&NameString
,
1123 &LdrEntry
->BaseDllName
,
1126 /* Get the base address */
1127 ImportBase
= LdrEntry
->DllBase
;
1129 /* Check if we haven't loaded yet, and we need references */
1130 if (!(Loaded
) && (ReferenceNeeded
))
1132 /* Make sure we're not already loading */
1133 if (!(LdrEntry
->Flags
& LDRP_LOAD_IN_PROGRESS
))
1135 /* Increase the load count */
1136 LdrEntry
->LoadCount
++;
1140 /* Done, break out */
1144 /* Go to the next entry */
1145 NextEntry
= NextEntry
->Flink
;
1148 /* Check if we haven't loaded the import yet */
1151 /* Setup the import DLL name */
1152 DllName
.MaximumLength
= NameString
.Length
+
1153 ImageFileDirectory
->Length
+
1154 sizeof(UNICODE_NULL
);
1155 DllName
.Buffer
= ExAllocatePoolWithTag(NonPagedPool
,
1156 DllName
.MaximumLength
,
1160 /* Setup the base length and copy it */
1161 DllName
.Length
= ImageFileDirectory
->Length
;
1162 RtlCopyMemory(DllName
.Buffer
,
1163 ImageFileDirectory
->Buffer
,
1164 ImageFileDirectory
->Length
);
1166 /* Now add the import name and null-terminate it */
1167 RtlAppendUnicodeStringToString(&DllName
,
1169 DllName
.Buffer
[DllName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1171 /* Load the image */
1172 Status
= MmLoadSystemImage(&DllName
,
1178 if (NT_SUCCESS(Status
))
1180 /* We can free the DLL Name */
1181 ExFreePool(DllName
.Buffer
);
1185 /* Fill out the information for the error */
1186 *MissingDriver
= DllName
.Buffer
;
1187 *(PULONG
)MissingDriver
|= 1;
1193 /* We're out of resources */
1194 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1197 /* Check if we're OK until now */
1198 if (NT_SUCCESS(Status
))
1200 /* We're now loaded */
1204 ASSERT(DllBase
= DllEntry
->DllBase
);
1206 /* Call the initialization routines */
1207 Status
= MmCallDllInitialize(DllEntry
, &PsLoadedModuleList
);
1208 if (!NT_SUCCESS(Status
))
1210 /* We failed, unload the image */
1211 MmUnloadSystemImage(DllEntry
);
1217 /* Check if we failed by here */
1218 if (!NT_SUCCESS(Status
))
1220 /* Cleanup and return */
1221 RtlFreeUnicodeString(&NameString
);
1222 MiDereferenceImports(LoadedImports
);
1223 if (LoadedImports
) ExFreePoolWithTag(LoadedImports
, TAG_LDR_WSTR
);
1227 /* Loop again to make sure that everything is OK */
1231 /* Check if we're support to reference this import */
1232 if ((ReferenceNeeded
) && (LoadedImports
))
1234 /* Make sure we're not already loading */
1235 if (!(LdrEntry
->Flags
& LDRP_LOAD_IN_PROGRESS
))
1238 LoadedImports
->Entry
[ImportCount
] = LdrEntry
;
1243 /* Free the import name */
1244 RtlFreeUnicodeString(&NameString
);
1246 /* Set the missing driver name and get the export directory */
1247 *MissingDriver
= LdrEntry
->BaseDllName
.Buffer
;
1248 ExportDirectory
= RtlImageDirectoryEntryToData(ImportBase
,
1250 IMAGE_DIRECTORY_ENTRY_EXPORT
,
1252 if (!ExportDirectory
)
1254 /* Cleanup and return */
1255 MiDereferenceImports(LoadedImports
);
1256 if (LoadedImports
) ExFreePoolWithTag(LoadedImports
, TAG_LDR_WSTR
);
1257 DPRINT1("Warning: Driver failed to load, %S not found\n", *MissingDriver
);
1258 return STATUS_DRIVER_ENTRYPOINT_NOT_FOUND
;
1261 /* Make sure we have an IAT */
1262 if (ImportDescriptor
->OriginalFirstThunk
)
1264 /* Get the first thunks */
1265 OrigThunk
= (PVOID
)((ULONG_PTR
)ImageBase
+
1266 ImportDescriptor
->OriginalFirstThunk
);
1267 FirstThunk
= (PVOID
)((ULONG_PTR
)ImageBase
+
1268 ImportDescriptor
->FirstThunk
);
1271 while (OrigThunk
->u1
.AddressOfData
)
1274 Status
= MiSnapThunk(ImportBase
,
1282 if (!NT_SUCCESS(Status
))
1284 /* Cleanup and return */
1285 MiDereferenceImports(LoadedImports
);
1286 if (LoadedImports
) ExFreePoolWithTag(LoadedImports
, TAG_LDR_WSTR
);
1290 /* Reset the buffer */
1291 *MissingApi
= MissingApiBuffer
;
1295 /* Go to the next import */
1299 /* Check if we have an import list */
1302 /* Reset the count again, and loop entries */
1304 for (i
= 0; i
< LoadedImports
->Count
; i
++)
1306 if (LoadedImports
->Entry
[i
])
1308 /* Got an entry, OR it with 1 in case it's the single entry */
1309 ImportEntry
= (PVOID
)((ULONG_PTR
)LoadedImports
->Entry
[i
] |
1310 MM_SYSLDR_SINGLE_ENTRY
);
1315 /* Check if we had no imports */
1318 /* Free the list and set it to no imports */
1319 ExFreePoolWithTag(LoadedImports
, TAG_LDR_WSTR
);
1320 LoadedImports
= MM_SYSLDR_NO_IMPORTS
;
1322 else if (ImportCount
== 1)
1324 /* Just one entry, we can free the table and only use our entry */
1325 ExFreePoolWithTag(LoadedImports
, TAG_LDR_WSTR
);
1326 LoadedImports
= (PLOAD_IMPORTS
)ImportEntry
;
1328 else if (ImportCount
!= LoadedImports
->Count
)
1330 /* Allocate a new list */
1331 LoadedImportsSize
= ImportCount
* sizeof(PVOID
) + sizeof(SIZE_T
);
1332 NewImports
= ExAllocatePoolWithTag(PagedPool
,
1338 NewImports
->Count
= 0;
1340 /* Loop all the imports */
1341 for (i
= 0; i
< LoadedImports
->Count
; i
++)
1343 /* Make sure it's valid */
1344 if (LoadedImports
->Entry
[i
])
1347 NewImports
->Entry
[NewImports
->Count
] = LoadedImports
->Entry
[i
];
1348 NewImports
->Count
++;
1352 /* Free the old copy */
1353 ExFreePoolWithTag(LoadedImports
, TAG_LDR_WSTR
);
1354 LoadedImports
= NewImports
;
1358 /* Return the list */
1359 *LoadImports
= LoadedImports
;
1362 /* Return success */
1363 return STATUS_SUCCESS
;
1369 MiReloadBootLoadedDrivers(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
1371 PLIST_ENTRY NextEntry
;
1373 PIMAGE_NT_HEADERS NtHeader
;
1374 PLDR_DATA_TABLE_ENTRY LdrEntry
;
1375 PIMAGE_FILE_HEADER FileHeader
;
1376 BOOLEAN ValidRelocs
;
1377 PIMAGE_DATA_DIRECTORY DataDirectory
;
1378 PVOID DllBase
, NewImageAddress
;
1380 PMMPTE PointerPte
, StartPte
, LastPte
;
1381 PFN_NUMBER PteCount
;
1383 MMPTE TempPte
, OldPte
;
1385 /* Loop driver list */
1386 for (NextEntry
= LoaderBlock
->LoadOrderListHead
.Flink
;
1387 NextEntry
!= &LoaderBlock
->LoadOrderListHead
;
1388 NextEntry
= NextEntry
->Flink
)
1390 /* Get the loader entry and NT header */
1391 LdrEntry
= CONTAINING_RECORD(NextEntry
,
1392 LDR_DATA_TABLE_ENTRY
,
1394 NtHeader
= RtlImageNtHeader(LdrEntry
->DllBase
);
1397 DPRINT("[Mm0]: Driver at: %p ending at: %p for module: %wZ\n",
1399 (ULONG_PTR
)LdrEntry
->DllBase
+ LdrEntry
->SizeOfImage
,
1400 &LdrEntry
->FullDllName
);
1402 /* Get the first PTE and the number of PTEs we'll need */
1403 PointerPte
= StartPte
= MiAddressToPte(LdrEntry
->DllBase
);
1404 PteCount
= ROUND_TO_PAGES(LdrEntry
->SizeOfImage
) >> PAGE_SHIFT
;
1405 LastPte
= StartPte
+ PteCount
;
1409 while (PointerPte
< LastPte
)
1412 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
1413 Pfn1
= MiGetPfnEntry(PFN_FROM_PTE(PointerPte
));
1414 len
= wcslen(LdrEntry
->BaseDllName
.Buffer
) * sizeof(WCHAR
);
1415 snprintf(Pfn1
->ProcessName
, min(16, len
), "%S", LdrEntry
->BaseDllName
.Buffer
);
1419 /* Skip kernel and HAL */
1420 /* ROS HACK: Skip BOOTVID/KDCOM too */
1422 if (i
<= 4) continue;
1424 /* Skip non-drivers */
1425 if (!NtHeader
) continue;
1427 /* Get the file header and make sure we can relocate */
1428 FileHeader
= &NtHeader
->FileHeader
;
1429 if (FileHeader
->Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
) continue;
1430 if (NtHeader
->OptionalHeader
.NumberOfRvaAndSizes
<
1431 IMAGE_DIRECTORY_ENTRY_BASERELOC
) continue;
1433 /* Everything made sense until now, check the relocation section too */
1434 DataDirectory
= &NtHeader
->OptionalHeader
.
1435 DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1436 if (!DataDirectory
->VirtualAddress
)
1438 /* We don't really have relocations */
1439 ValidRelocs
= FALSE
;
1443 /* Make sure the size is valid */
1444 if ((DataDirectory
->VirtualAddress
+ DataDirectory
->Size
) >
1445 LdrEntry
->SizeOfImage
)
1447 /* They're not, skip */
1451 /* We have relocations */
1455 /* Remember the original address */
1456 DllBase
= LdrEntry
->DllBase
;
1459 PointerPte
= StartPte
;
1460 while (PointerPte
< LastPte
)
1462 /* Mark the page modified in the PFN database */
1463 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
1464 Pfn1
= MiGetPfnEntry(PFN_FROM_PTE(PointerPte
));
1465 ASSERT(Pfn1
->u3
.e1
.Rom
== 0);
1466 Pfn1
->u3
.e1
.Modified
= TRUE
;
1472 /* Now reserve system PTEs for the image */
1473 PointerPte
= MiReserveSystemPtes(PteCount
, SystemPteSpace
);
1476 /* Shouldn't happen */
1477 DPRINT1("[Mm0]: Couldn't allocate driver section!\n");
1481 /* This is the new virtual address for the module */
1482 LastPte
= PointerPte
+ PteCount
;
1483 NewImageAddress
= MiPteToAddress(PointerPte
);
1486 DPRINT("[Mm0]: Copying from: %p to: %p\n", DllBase
, NewImageAddress
);
1487 ASSERT(ExpInitializationPhase
== 0);
1489 /* Loop the new driver PTEs */
1490 TempPte
= ValidKernelPte
;
1491 while (PointerPte
< LastPte
)
1493 /* Copy the old data */
1495 ASSERT(OldPte
.u
.Hard
.Valid
== 1);
1497 /* Set page number from the loader's memory */
1498 TempPte
.u
.Hard
.PageFrameNumber
= OldPte
.u
.Hard
.PageFrameNumber
;
1501 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
1508 /* Update position */
1509 PointerPte
-= PteCount
;
1512 ASSERT(*(PULONG
)NewImageAddress
== *(PULONG
)DllBase
);
1514 /* Set the image base to the address where the loader put it */
1515 NtHeader
->OptionalHeader
.ImageBase
= (ULONG_PTR
)DllBase
;
1517 /* Check if we had relocations */
1520 /* Relocate the image */
1521 Status
= LdrRelocateImageWithBias(NewImageAddress
,
1525 STATUS_CONFLICTING_ADDRESSES
,
1526 STATUS_INVALID_IMAGE_FORMAT
);
1527 if (!NT_SUCCESS(Status
))
1529 /* This shouldn't happen */
1530 DPRINT1("Relocations failed!\n");
1535 /* Update the loader entry */
1536 LdrEntry
->DllBase
= NewImageAddress
;
1538 /* Update the thunks */
1539 DPRINT("[Mm0]: Updating thunks to: %wZ\n", &LdrEntry
->BaseDllName
);
1540 MiUpdateThunks(LoaderBlock
,
1543 LdrEntry
->SizeOfImage
);
1545 /* Update the loader entry */
1546 LdrEntry
->Flags
|= LDRP_SYSTEM_MAPPED
;
1547 LdrEntry
->EntryPoint
= (PVOID
)((ULONG_PTR
)NewImageAddress
+
1548 NtHeader
->OptionalHeader
.AddressOfEntryPoint
);
1549 LdrEntry
->SizeOfImage
= PteCount
<< PAGE_SHIFT
;
1551 /* FIXME: We'll need to fixup the PFN linkage when switching to ARM3 */
1558 MiBuildImportsForBootDrivers(VOID
)
1560 PLIST_ENTRY NextEntry
, NextEntry2
;
1561 PLDR_DATA_TABLE_ENTRY LdrEntry
, KernelEntry
, HalEntry
, LdrEntry2
, LastEntry
;
1562 PLDR_DATA_TABLE_ENTRY
* EntryArray
;
1563 UNICODE_STRING KernelName
= RTL_CONSTANT_STRING(L
"ntoskrnl.exe");
1564 UNICODE_STRING HalName
= RTL_CONSTANT_STRING(L
"hal.dll");
1565 PLOAD_IMPORTS LoadedImports
;
1566 ULONG LoadedImportsSize
, ImportSize
;
1567 PULONG_PTR ImageThunk
;
1568 ULONG_PTR DllBase
, DllEnd
;
1569 ULONG Modules
= 0, i
, j
= 0;
1570 PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor
;
1572 /* Initialize variables */
1573 KernelEntry
= HalEntry
= LastEntry
= NULL
;
1575 /* Loop the loaded module list... we are early enough that no lock is needed */
1576 NextEntry
= PsLoadedModuleList
.Flink
;
1577 while (NextEntry
!= &PsLoadedModuleList
)
1580 LdrEntry
= CONTAINING_RECORD(NextEntry
,
1581 LDR_DATA_TABLE_ENTRY
,
1584 /* Check if it's the kernel or HAL */
1585 if (RtlEqualUnicodeString(&KernelName
, &LdrEntry
->BaseDllName
, TRUE
))
1588 KernelEntry
= LdrEntry
;
1590 else if (RtlEqualUnicodeString(&HalName
, &LdrEntry
->BaseDllName
, TRUE
))
1593 HalEntry
= LdrEntry
;
1596 /* Check if this is a driver DLL */
1597 if (LdrEntry
->Flags
& LDRP_DRIVER_DEPENDENT_DLL
)
1599 /* Check if this is the HAL or kernel */
1600 if ((LdrEntry
== HalEntry
) || (LdrEntry
== KernelEntry
))
1602 /* Add a reference */
1603 LdrEntry
->LoadCount
= 1;
1607 /* No referencing needed */
1608 LdrEntry
->LoadCount
= 0;
1613 /* No referencing needed */
1614 LdrEntry
->LoadCount
= 0;
1617 /* Remember this came from the loader */
1618 LdrEntry
->LoadedImports
= MM_SYSLDR_BOOT_LOADED
;
1621 NextEntry
= NextEntry
->Flink
;
1625 /* We must have at least found the kernel and HAL */
1626 if (!(HalEntry
) || (!KernelEntry
)) return STATUS_NOT_FOUND
;
1628 /* Allocate the list */
1629 EntryArray
= ExAllocatePoolWithTag(PagedPool
, Modules
* sizeof(PVOID
), 'TDmM');
1630 if (!EntryArray
) return STATUS_INSUFFICIENT_RESOURCES
;
1632 /* Loop the loaded module list again */
1633 NextEntry
= PsLoadedModuleList
.Flink
;
1634 while (NextEntry
!= &PsLoadedModuleList
)
1637 LdrEntry
= CONTAINING_RECORD(NextEntry
,
1638 LDR_DATA_TABLE_ENTRY
,
1640 #ifdef _WORKING_LOADER_
1641 /* Get its imports */
1642 ImageThunk
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
1644 IMAGE_DIRECTORY_ENTRY_IAT
,
1648 /* Get its imports */
1649 ImportDescriptor
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
1651 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1653 if (!ImportDescriptor
)
1657 LdrEntry
->LoadedImports
= MM_SYSLDR_NO_IMPORTS
;
1658 NextEntry
= NextEntry
->Flink
;
1662 /* Clear the list and count the number of IAT thunks */
1663 RtlZeroMemory(EntryArray
, Modules
* sizeof(PVOID
));
1664 #ifdef _WORKING_LOADER_
1665 ImportSize
/= sizeof(ULONG_PTR
);
1667 /* Scan the thunks */
1668 for (i
= 0, DllBase
= 0, DllEnd
= 0; i
< ImportSize
; i
++, ImageThunk
++)
1670 i
= DllBase
= DllEnd
= 0;
1671 while ((ImportDescriptor
->Name
) &&
1672 (ImportDescriptor
->OriginalFirstThunk
))
1674 /* Get the image thunk */
1675 ImageThunk
= (PVOID
)((ULONG_PTR
)LdrEntry
->DllBase
+
1676 ImportDescriptor
->FirstThunk
);
1680 /* Do we already have an address? */
1683 /* Is the thunk in the same address? */
1684 if ((*ImageThunk
>= DllBase
) && (*ImageThunk
< DllEnd
))
1686 /* Skip it, we already have a reference for it */
1687 ASSERT(EntryArray
[j
]);
1693 /* Loop the loaded module list to locate this address owner */
1695 NextEntry2
= PsLoadedModuleList
.Flink
;
1696 while (NextEntry2
!= &PsLoadedModuleList
)
1699 LdrEntry2
= CONTAINING_RECORD(NextEntry2
,
1700 LDR_DATA_TABLE_ENTRY
,
1703 /* Get the address range for this module */
1704 DllBase
= (ULONG_PTR
)LdrEntry2
->DllBase
;
1705 DllEnd
= DllBase
+ LdrEntry2
->SizeOfImage
;
1707 /* Check if this IAT entry matches it */
1708 if ((*ImageThunk
>= DllBase
) && (*ImageThunk
< DllEnd
))
1711 //DPRINT1("Found imported dll: %wZ\n", &LdrEntry2->BaseDllName);
1712 EntryArray
[j
] = LdrEntry2
;
1716 /* Keep searching */
1717 NextEntry2
= NextEntry2
->Flink
;
1721 /* Do we have a thunk outside the range? */
1722 if ((*ImageThunk
< DllBase
) || (*ImageThunk
>= DllEnd
))
1727 /* Should not be happening */
1728 DPRINT1("Broken IAT entry for %p at %p (%lx)\n",
1729 LdrEntry
, ImageThunk
, *ImageThunk
);
1733 /* Reset if we hit this */
1736 #ifndef _WORKING_LOADER_
1745 /* Now scan how many imports we really have */
1746 for (i
= 0, ImportSize
= 0; i
< Modules
; i
++)
1748 /* Skip HAL and kernel */
1749 if ((EntryArray
[i
]) &&
1750 (EntryArray
[i
] != HalEntry
) &&
1751 (EntryArray
[i
] != KernelEntry
))
1753 /* A valid reference */
1754 LastEntry
= EntryArray
[i
];
1759 /* Do we have any imports after all? */
1763 LdrEntry
->LoadedImports
= MM_SYSLDR_NO_IMPORTS
;
1765 else if (ImportSize
== 1)
1767 /* A single entry import */
1768 LdrEntry
->LoadedImports
= (PVOID
)((ULONG_PTR
)LastEntry
| MM_SYSLDR_SINGLE_ENTRY
);
1769 LastEntry
->LoadCount
++;
1773 /* We need an import table */
1774 LoadedImportsSize
= ImportSize
* sizeof(PVOID
) + sizeof(SIZE_T
);
1775 LoadedImports
= ExAllocatePoolWithTag(PagedPool
,
1778 ASSERT(LoadedImports
);
1780 /* Save the count */
1781 LoadedImports
->Count
= ImportSize
;
1783 /* Now copy all imports */
1784 for (i
= 0, j
= 0; i
< Modules
; i
++)
1786 /* Skip HAL and kernel */
1787 if ((EntryArray
[i
]) &&
1788 (EntryArray
[i
] != HalEntry
) &&
1789 (EntryArray
[i
] != KernelEntry
))
1791 /* A valid reference */
1792 //DPRINT1("Found valid entry: %p\n", EntryArray[i]);
1793 LoadedImports
->Entry
[j
] = EntryArray
[i
];
1794 EntryArray
[i
]->LoadCount
++;
1799 /* Should had as many entries as we expected */
1800 ASSERT(j
== ImportSize
);
1801 LdrEntry
->LoadedImports
= LoadedImports
;
1805 NextEntry
= NextEntry
->Flink
;
1808 /* Free the initial array */
1809 ExFreePool(EntryArray
);
1811 /* FIXME: Might not need to keep the HAL/Kernel imports around */
1813 /* Kernel and HAL are loaded at boot */
1814 KernelEntry
->LoadedImports
= MM_SYSLDR_BOOT_LOADED
;
1815 HalEntry
->LoadedImports
= MM_SYSLDR_BOOT_LOADED
;
1817 /* All worked well */
1818 return STATUS_SUCCESS
;
1824 MiLocateKernelSections(IN PLDR_DATA_TABLE_ENTRY LdrEntry
)
1827 PIMAGE_NT_HEADERS NtHeaders
;
1828 PIMAGE_SECTION_HEADER SectionHeader
;
1829 ULONG Sections
, Size
;
1831 /* Get the kernel section header */
1832 DllBase
= (ULONG_PTR
)LdrEntry
->DllBase
;
1833 NtHeaders
= RtlImageNtHeader((PVOID
)DllBase
);
1834 SectionHeader
= IMAGE_FIRST_SECTION(NtHeaders
);
1836 /* Loop all the sections */
1837 Sections
= NtHeaders
->FileHeader
.NumberOfSections
;
1840 /* Grab the size of the section */
1841 Size
= max(SectionHeader
->SizeOfRawData
, SectionHeader
->Misc
.VirtualSize
);
1843 /* Check for .RSRC section */
1844 if (*(PULONG
)SectionHeader
->Name
== 'rsr.')
1846 /* Remember the PTEs so we can modify them later */
1847 MiKernelResourceStartPte
= MiAddressToPte(DllBase
+
1848 SectionHeader
->VirtualAddress
);
1849 MiKernelResourceEndPte
= MiKernelResourceStartPte
+
1850 BYTES_TO_PAGES(SectionHeader
->VirtualAddress
+ Size
);
1852 else if (*(PULONG
)SectionHeader
->Name
== 'LOOP')
1854 /* POOLCODE vs. POOLMI */
1855 if (*(PULONG
)&SectionHeader
->Name
[4] == 'EDOC')
1857 /* Found Ex* Pool code */
1858 ExPoolCodeStart
= DllBase
+ SectionHeader
->VirtualAddress
;
1859 ExPoolCodeEnd
= ExPoolCodeStart
+ Size
;
1861 else if (*(PUSHORT
)&SectionHeader
->Name
[4] == 'MI')
1863 /* Found Mm* Pool code */
1864 MmPoolCodeStart
= DllBase
+ SectionHeader
->VirtualAddress
;
1865 MmPoolCodeEnd
= ExPoolCodeStart
+ Size
;
1868 else if ((*(PULONG
)SectionHeader
->Name
== 'YSIM') &&
1869 (*(PULONG
)&SectionHeader
->Name
[4] == 'ETPS'))
1871 /* Found MISYSPTE (Mm System PTE code)*/
1872 MmPteCodeStart
= DllBase
+ SectionHeader
->VirtualAddress
;
1873 MmPteCodeEnd
= ExPoolCodeStart
+ Size
;
1885 MiInitializeLoadedModuleList(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
1887 PLDR_DATA_TABLE_ENTRY LdrEntry
, NewEntry
;
1888 PLIST_ENTRY ListHead
, NextEntry
;
1891 /* Setup the loaded module list and locks */
1892 ExInitializeResourceLite(&PsLoadedModuleResource
);
1893 KeInitializeSpinLock(&PsLoadedModuleSpinLock
);
1894 InitializeListHead(&PsLoadedModuleList
);
1896 /* Get loop variables and the kernel entry */
1897 ListHead
= &LoaderBlock
->LoadOrderListHead
;
1898 NextEntry
= ListHead
->Flink
;
1899 LdrEntry
= CONTAINING_RECORD(NextEntry
,
1900 LDR_DATA_TABLE_ENTRY
,
1902 PsNtosImageBase
= (ULONG_PTR
)LdrEntry
->DllBase
;
1904 /* Locate resource section, pool code, and system pte code */
1905 MiLocateKernelSections(LdrEntry
);
1907 /* Loop the loader block */
1908 while (NextEntry
!= ListHead
)
1910 /* Get the loader entry */
1911 LdrEntry
= CONTAINING_RECORD(NextEntry
,
1912 LDR_DATA_TABLE_ENTRY
,
1915 /* FIXME: ROS HACK. Make sure this is a driver */
1916 if (!RtlImageNtHeader(LdrEntry
->DllBase
))
1918 /* Skip this entry */
1919 NextEntry
= NextEntry
->Flink
;
1923 /* Calculate the size we'll need and allocate a copy */
1924 EntrySize
= sizeof(LDR_DATA_TABLE_ENTRY
) +
1925 LdrEntry
->BaseDllName
.MaximumLength
+
1926 sizeof(UNICODE_NULL
);
1927 NewEntry
= ExAllocatePoolWithTag(NonPagedPool
, EntrySize
, TAG_LDR_WSTR
);
1928 if (!NewEntry
) return FALSE
;
1930 /* Copy the entry over */
1931 *NewEntry
= *LdrEntry
;
1933 /* Allocate the name */
1934 NewEntry
->FullDllName
.Buffer
=
1935 ExAllocatePoolWithTag(PagedPool
,
1936 LdrEntry
->FullDllName
.MaximumLength
+
1937 sizeof(UNICODE_NULL
),
1939 if (!NewEntry
->FullDllName
.Buffer
) return FALSE
;
1941 /* Set the base name */
1942 NewEntry
->BaseDllName
.Buffer
= (PVOID
)(NewEntry
+ 1);
1944 /* Copy the full and base name */
1945 RtlCopyMemory(NewEntry
->FullDllName
.Buffer
,
1946 LdrEntry
->FullDllName
.Buffer
,
1947 LdrEntry
->FullDllName
.MaximumLength
);
1948 RtlCopyMemory(NewEntry
->BaseDllName
.Buffer
,
1949 LdrEntry
->BaseDllName
.Buffer
,
1950 LdrEntry
->BaseDllName
.MaximumLength
);
1952 /* Null-terminate the base name */
1953 NewEntry
->BaseDllName
.Buffer
[NewEntry
->BaseDllName
.Length
/
1954 sizeof(WCHAR
)] = UNICODE_NULL
;
1956 /* Insert the entry into the list */
1957 InsertTailList(&PsLoadedModuleList
, &NewEntry
->InLoadOrderLinks
);
1958 NextEntry
= NextEntry
->Flink
;
1961 /* Build the import lists for the boot drivers */
1962 MiBuildImportsForBootDrivers();
1970 MiUseLargeDriverPage(IN ULONG NumberOfPtes
,
1971 IN OUT PVOID
*ImageBaseAddress
,
1972 IN PUNICODE_STRING BaseImageName
,
1973 IN BOOLEAN BootDriver
)
1975 PLIST_ENTRY NextEntry
;
1976 BOOLEAN DriverFound
= FALSE
;
1977 PMI_LARGE_PAGE_DRIVER_ENTRY LargePageDriverEntry
;
1978 ASSERT(KeGetCurrentIrql () <= APC_LEVEL
);
1979 ASSERT(*ImageBaseAddress
>= MmSystemRangeStart
);
1982 if (!(KeFeatureBits
& KF_LARGE_PAGE
)) return FALSE
;
1983 if (!(__readcr4() & CR4_PSE
)) return FALSE
;
1986 /* Make sure there's enough system PTEs for a large page driver */
1987 if (MmTotalFreeSystemPtes
[SystemPteSpace
] < (16 * (PDE_MAPPED_VA
>> PAGE_SHIFT
)))
1992 /* This happens if the registry key had a "*" (wildcard) in it */
1993 if (MiLargePageAllDrivers
== 0)
1995 /* It didn't, so scan the list */
1996 NextEntry
= MiLargePageDriverList
.Flink
;
1997 while (NextEntry
!= &MiLargePageDriverList
)
1999 /* Check if the driver name matches */
2000 LargePageDriverEntry
= CONTAINING_RECORD(NextEntry
,
2001 MI_LARGE_PAGE_DRIVER_ENTRY
,
2003 if (RtlEqualUnicodeString(BaseImageName
,
2004 &LargePageDriverEntry
->BaseName
,
2007 /* Enable large pages for this driver */
2013 NextEntry
= NextEntry
->Flink
;
2016 /* If we didn't find the driver, it doesn't need large pages */
2017 if (DriverFound
== FALSE
) return FALSE
;
2020 /* Nothing to do yet */
2021 DPRINT1("Large pages not supported!\n");
2027 MiComputeDriverProtection(IN BOOLEAN SessionSpace
,
2028 IN ULONG SectionProtection
)
2030 ULONG Protection
= MM_ZERO_ACCESS
;
2032 /* Check if the caller gave anything */
2033 if (SectionProtection
)
2035 /* Always turn on execute access */
2036 SectionProtection
|= IMAGE_SCN_MEM_EXECUTE
;
2038 /* Check if the registry setting is on or not */
2039 if (!MmEnforceWriteProtection
)
2041 /* Turn on write access too */
2042 SectionProtection
|= (IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_EXECUTE
);
2046 /* Convert to internal PTE flags */
2047 if (SectionProtection
& IMAGE_SCN_MEM_EXECUTE
) Protection
|= MM_EXECUTE
;
2048 if (SectionProtection
& IMAGE_SCN_MEM_READ
) Protection
|= MM_READONLY
;
2050 /* Check for write access */
2051 if (SectionProtection
& IMAGE_SCN_MEM_WRITE
)
2053 /* Session space is not supported */
2056 DPRINT1("Session drivers not supported\n");
2057 ASSERT(SessionSpace
== FALSE
);
2061 /* Convert to internal PTE flag */
2062 Protection
= (Protection
& MM_EXECUTE
) ? MM_EXECUTE_READWRITE
: MM_READWRITE
;
2066 /* If there's no access at all by now, convert to internal no access flag */
2067 if (Protection
== MM_ZERO_ACCESS
) Protection
= MM_NOACCESS
;
2069 /* Return the computed PTE protection */
2075 MiSetSystemCodeProtection(IN PMMPTE FirstPte
,
2077 IN ULONG ProtectionMask
)
2079 /* I'm afraid to introduce regressions at the moment... */
2085 MiWriteProtectSystemImage(IN PVOID ImageBase
)
2087 PIMAGE_NT_HEADERS NtHeaders
;
2088 PIMAGE_SECTION_HEADER Section
;
2089 PFN_NUMBER DriverPages
;
2090 ULONG CurrentProtection
, SectionProtection
, CombinedProtection
, ProtectionMask
;
2091 ULONG Sections
, Size
;
2092 ULONG_PTR BaseAddress
, CurrentAddress
;
2093 PMMPTE PointerPte
, StartPte
, LastPte
, CurrentPte
, ComboPte
= NULL
;
2094 ULONG CurrentMask
, CombinedMask
= 0;
2097 /* No need to write protect physical memory-backed drivers (large pages) */
2098 if (MI_IS_PHYSICAL_ADDRESS(ImageBase
)) return;
2100 /* Get the image headers */
2101 NtHeaders
= RtlImageNtHeader(ImageBase
);
2102 if (!NtHeaders
) return;
2104 /* Check if this is a session driver or not */
2105 if (!MI_IS_SESSION_ADDRESS(ImageBase
))
2107 /* Don't touch NT4 drivers */
2108 if (NtHeaders
->OptionalHeader
.MajorOperatingSystemVersion
< 5) return;
2109 if (NtHeaders
->OptionalHeader
.MajorImageVersion
< 5) return;
2114 DPRINT1("Session drivers not supported\n");
2118 /* These are the only protection masks we care about */
2119 ProtectionMask
= IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_READ
| IMAGE_SCN_MEM_EXECUTE
;
2121 /* Calculate the number of pages this driver is occupying */
2122 DriverPages
= BYTES_TO_PAGES(NtHeaders
->OptionalHeader
.SizeOfImage
);
2124 /* Get the number of sections and the first section header */
2125 Sections
= NtHeaders
->FileHeader
.NumberOfSections
;
2126 ASSERT(Sections
!= 0);
2127 Section
= IMAGE_FIRST_SECTION(NtHeaders
);
2129 /* Loop all the sections */
2130 CurrentAddress
= (ULONG_PTR
)ImageBase
;
2133 /* Get the section size */
2134 Size
= max(Section
->SizeOfRawData
, Section
->Misc
.VirtualSize
);
2136 /* Get its virtual address */
2137 BaseAddress
= (ULONG_PTR
)ImageBase
+ Section
->VirtualAddress
;
2138 if (BaseAddress
< CurrentAddress
)
2140 /* Windows doesn't like these */
2141 DPRINT1("Badly linked image!\n");
2145 /* Remember the current address */
2146 CurrentAddress
= BaseAddress
+ Size
- 1;
2153 /* Get the number of sections and the first section header */
2154 Sections
= NtHeaders
->FileHeader
.NumberOfSections
;
2155 ASSERT(Sections
!= 0);
2156 Section
= IMAGE_FIRST_SECTION(NtHeaders
);
2158 /* Set the address at the end to initialize the loop */
2159 CurrentAddress
= (ULONG_PTR
)Section
+ Sections
- 1;
2160 CurrentProtection
= IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_READ
;
2162 /* Set the PTE points for the image, and loop its sections */
2163 StartPte
= MiAddressToPte(ImageBase
);
2164 LastPte
= StartPte
+ DriverPages
;
2167 /* Get the section size */
2168 Size
= max(Section
->SizeOfRawData
, Section
->Misc
.VirtualSize
);
2170 /* Get its virtual address and PTE */
2171 BaseAddress
= (ULONG_PTR
)ImageBase
+ Section
->VirtualAddress
;
2172 PointerPte
= MiAddressToPte(BaseAddress
);
2174 /* Check if we were already protecting a run, and found a new run */
2175 if ((ComboPte
) && (PointerPte
> ComboPte
))
2177 /* Compute protection */
2178 CombinedMask
= MiComputeDriverProtection(FALSE
, CombinedProtection
);
2181 MiSetSystemCodeProtection(ComboPte
, ComboPte
, CombinedMask
);
2183 /* Check for overlap */
2184 if (ComboPte
== StartPte
) StartPte
++;
2186 /* One done, reset variables */
2188 CombinedProtection
= 0;
2191 /* Break out when needed */
2192 if (PointerPte
>= LastPte
) break;
2194 /* Get the requested protection from the image header */
2195 SectionProtection
= Section
->Characteristics
& ProtectionMask
;
2196 if (SectionProtection
== CurrentProtection
)
2198 /* Same protection, so merge the request */
2199 CurrentAddress
= BaseAddress
+ Size
- 1;
2207 /* This is now a new section, so close up the old one */
2208 CurrentPte
= MiAddressToPte(CurrentAddress
);
2210 /* Check for overlap */
2211 if (CurrentPte
== PointerPte
)
2213 /* Skip the last PTE, since it overlaps with us */
2216 /* And set the PTE we will merge with */
2217 ASSERT((ComboPte
== NULL
) || (ComboPte
== PointerPte
));
2218 ComboPte
= PointerPte
;
2220 /* Get the most flexible protection by merging both */
2221 CombinedMask
|= (SectionProtection
| CurrentProtection
);
2224 /* Loop any PTEs left */
2225 if (CurrentPte
>= StartPte
)
2228 ASSERT(StartPte
< LastPte
);
2230 /* Make sure we don't overflow past the last PTE in the driver */
2231 if (CurrentPte
>= LastPte
) CurrentPte
= LastPte
- 1;
2232 ASSERT(CurrentPte
>= StartPte
);
2234 /* Compute the protection and set it */
2235 CurrentMask
= MiComputeDriverProtection(FALSE
, CurrentProtection
);
2236 MiSetSystemCodeProtection(StartPte
, CurrentPte
, CurrentMask
);
2240 StartPte
= PointerPte
;
2241 CurrentAddress
= BaseAddress
+ Size
- 1;
2242 CurrentProtection
= SectionProtection
;
2249 /* Is there a leftover section to merge? */
2252 /* Compute and set the protection */
2253 CombinedMask
= MiComputeDriverProtection(FALSE
, CombinedProtection
);
2254 MiSetSystemCodeProtection(ComboPte
, ComboPte
, CombinedMask
);
2256 /* Handle overlap */
2257 if (ComboPte
== StartPte
) StartPte
++;
2260 /* Finally, handle the last section */
2261 CurrentPte
= MiAddressToPte(CurrentAddress
);
2262 if ((StartPte
< LastPte
) && (CurrentPte
>= StartPte
))
2264 /* Handle overlap */
2265 if (CurrentPte
>= LastPte
) CurrentPte
= LastPte
- 1;
2266 ASSERT(CurrentPte
>= StartPte
);
2268 /* Compute and set the protection */
2269 CurrentMask
= MiComputeDriverProtection(FALSE
, CurrentProtection
);
2270 MiSetSystemCodeProtection(StartPte
, CurrentPte
, CurrentMask
);
2276 MiSetPagingOfDriver(IN PMMPTE PointerPte
,
2280 PETHREAD CurrentThread
= PsGetCurrentThread();
2281 PFN_NUMBER PageCount
= 0, PageFrameIndex
;
2285 /* Get the driver's base address */
2286 ImageBase
= MiPteToAddress(PointerPte
);
2287 ASSERT(MI_IS_SESSION_IMAGE_ADDRESS(ImageBase
) == FALSE
);
2289 /* If this is a large page, it's stuck in physical memory */
2290 if (MI_IS_PHYSICAL_ADDRESS(ImageBase
)) return;
2292 /* Lock the working set */
2293 MiLockWorkingSet(CurrentThread
, &MmSystemCacheWs
);
2296 while (PointerPte
<= LastPte
)
2298 /* Check for valid PTE */
2299 if (PointerPte
->u
.Hard
.Valid
== 1)
2301 PageFrameIndex
= PFN_FROM_PTE(PointerPte
);
2302 Pfn1
= MiGetPfnEntry(PageFrameIndex
);
2303 ASSERT(Pfn1
->u2
.ShareCount
== 1);
2305 /* No working sets in ReactOS yet */
2309 ImageBase
= (PVOID
)((ULONG_PTR
)ImageBase
+ PAGE_SIZE
);
2313 /* Release the working set */
2314 MiUnlockWorkingSet(CurrentThread
, &MmSystemCacheWs
);
2316 /* Do we have any driver pages? */
2319 /* Update counters */
2320 InterlockedExchangeAdd((PLONG
)&MmTotalSystemDriverPages
, PageCount
);
2326 MiEnablePagingOfDriver(IN PLDR_DATA_TABLE_ENTRY LdrEntry
)
2328 ULONG_PTR ImageBase
;
2329 PIMAGE_NT_HEADERS NtHeaders
;
2330 ULONG Sections
, Alignment
, Size
;
2331 PIMAGE_SECTION_HEADER Section
;
2332 PMMPTE PointerPte
= NULL
, LastPte
= NULL
;
2333 if (MmDisablePagingExecutive
) return;
2335 /* Get the driver base address and its NT header */
2336 ImageBase
= (ULONG_PTR
)LdrEntry
->DllBase
;
2337 NtHeaders
= RtlImageNtHeader((PVOID
)ImageBase
);
2338 if (!NtHeaders
) return;
2340 /* Get the sections and their alignment */
2341 Sections
= NtHeaders
->FileHeader
.NumberOfSections
;
2342 Alignment
= NtHeaders
->OptionalHeader
.SectionAlignment
- 1;
2344 /* Loop each section */
2345 Section
= IMAGE_FIRST_SECTION(NtHeaders
);
2348 /* Find PAGE or .edata */
2349 if ((*(PULONG
)Section
->Name
== 'EGAP') ||
2350 (*(PULONG
)Section
->Name
== 'ade.'))
2352 /* Had we already done some work? */
2355 /* Nope, setup the first PTE address */
2356 PointerPte
= MiAddressToPte(ROUND_TO_PAGES(ImageBase
+
2361 /* Compute the size */
2362 Size
= max(Section
->SizeOfRawData
, Section
->Misc
.VirtualSize
);
2364 /* Find the last PTE that maps this section */
2365 LastPte
= MiAddressToPte(ImageBase
+
2366 Section
->VirtualAddress
+
2373 /* Had we found a section before? */
2376 /* Mark it as pageable */
2377 MiSetPagingOfDriver(PointerPte
, LastPte
);
2382 /* Keep searching */
2387 /* Handle the straggler */
2388 if (PointerPte
) MiSetPagingOfDriver(PointerPte
, LastPte
);
2393 MmVerifyImageIsOkForMpUse(IN PVOID BaseAddress
)
2395 PIMAGE_NT_HEADERS NtHeader
;
2398 /* Get NT Headers */
2399 NtHeader
= RtlImageNtHeader(BaseAddress
);
2402 /* Check if this image is only safe for UP while we have 2+ CPUs */
2403 if ((KeNumberProcessors
> 1) &&
2404 (NtHeader
->FileHeader
.Characteristics
& IMAGE_FILE_UP_SYSTEM_ONLY
))
2411 /* Otherwise, it's safe */
2417 MmCheckSystemImage(IN HANDLE ImageHandle
,
2418 IN BOOLEAN PurgeSection
)
2421 HANDLE SectionHandle
;
2422 PVOID ViewBase
= NULL
;
2423 SIZE_T ViewSize
= 0;
2424 IO_STATUS_BLOCK IoStatusBlock
;
2425 FILE_STANDARD_INFORMATION FileStandardInfo
;
2426 KAPC_STATE ApcState
;
2427 PIMAGE_NT_HEADERS NtHeaders
;
2428 OBJECT_ATTRIBUTES ObjectAttributes
;
2431 /* Setup the object attributes */
2432 InitializeObjectAttributes(&ObjectAttributes
,
2434 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2438 /* Create a section for the DLL */
2439 Status
= ZwCreateSection(&SectionHandle
,
2440 SECTION_MAP_EXECUTE
,
2446 if (!NT_SUCCESS(Status
)) return Status
;
2448 /* Make sure we're in the system process */
2449 KeStackAttachProcess(&PsInitialSystemProcess
->Pcb
, &ApcState
);
2452 Status
= ZwMapViewOfSection(SectionHandle
,
2462 if (!NT_SUCCESS(Status
))
2464 /* We failed, close the handle and return */
2465 KeUnstackDetachProcess(&ApcState
);
2466 ZwClose(SectionHandle
);
2470 /* Now query image information */
2471 Status
= ZwQueryInformationFile(ImageHandle
,
2474 sizeof(FileStandardInfo
),
2475 FileStandardInformation
);
2476 if (NT_SUCCESS(Status
))
2478 /* First, verify the checksum */
2479 if (!LdrVerifyMappedImageMatchesChecksum(ViewBase
,
2484 /* Set checksum failure */
2485 Status
= STATUS_IMAGE_CHECKSUM_MISMATCH
;
2489 /* Make sure it's a real image */
2490 NtHeaders
= RtlImageNtHeader(ViewBase
);
2493 /* Set checksum failure */
2494 Status
= STATUS_IMAGE_CHECKSUM_MISMATCH
;
2498 /* Make sure it's for the correct architecture */
2499 if ((NtHeaders
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_NATIVE
) ||
2500 (NtHeaders
->OptionalHeader
.Magic
!= IMAGE_NT_OPTIONAL_HDR_MAGIC
))
2502 /* Set protection failure */
2503 Status
= STATUS_INVALID_IMAGE_PROTECT
;
2507 /* Check that it's a valid SMP image if we have more then one CPU */
2508 if (!MmVerifyImageIsOkForMpUse(ViewBase
))
2510 /* Otherwise it's not the right image */
2511 Status
= STATUS_IMAGE_MP_UP_MISMATCH
;
2515 /* Unmap the section, close the handle, and return status */
2517 ZwUnmapViewOfSection(NtCurrentProcess(), ViewBase
);
2518 KeUnstackDetachProcess(&ApcState
);
2519 ZwClose(SectionHandle
);
2525 MmLoadSystemImage(IN PUNICODE_STRING FileName
,
2526 IN PUNICODE_STRING NamePrefix OPTIONAL
,
2527 IN PUNICODE_STRING LoadedName OPTIONAL
,
2529 OUT PVOID
*ModuleObject
,
2530 OUT PVOID
*ImageBaseAddress
)
2532 PVOID ModuleLoadBase
= NULL
;
2534 HANDLE FileHandle
= NULL
;
2535 OBJECT_ATTRIBUTES ObjectAttributes
;
2536 IO_STATUS_BLOCK IoStatusBlock
;
2537 PIMAGE_NT_HEADERS NtHeader
;
2538 UNICODE_STRING BaseName
, BaseDirectory
, PrefixName
, UnicodeTemp
;
2539 PLDR_DATA_TABLE_ENTRY LdrEntry
= NULL
;
2540 ULONG EntrySize
, DriverSize
;
2541 PLOAD_IMPORTS LoadedImports
= MM_SYSLDR_NO_IMPORTS
;
2542 PCHAR MissingApiName
, Buffer
;
2543 PWCHAR MissingDriverName
;
2544 HANDLE SectionHandle
;
2545 ACCESS_MASK DesiredAccess
;
2546 PVOID Section
= NULL
;
2547 BOOLEAN LockOwned
= FALSE
;
2548 PLIST_ENTRY NextEntry
;
2549 IMAGE_INFO ImageInfo
;
2553 /* Detect session-load */
2557 ASSERT(NamePrefix
== NULL
);
2558 ASSERT(LoadedName
== NULL
);
2560 /* Make sure the process is in session too */
2561 if (!PsGetCurrentProcess()->ProcessInSession
) return STATUS_NO_MEMORY
;
2564 /* Allocate a buffer we'll use for names */
2565 Buffer
= ExAllocatePoolWithTag(NonPagedPool
, MAX_PATH
, 'nLmM');
2566 if (!Buffer
) return STATUS_INSUFFICIENT_RESOURCES
;
2568 /* Check for a separator */
2569 if (FileName
->Buffer
[0] == OBJ_NAME_PATH_SEPARATOR
)
2574 /* Loop the path until we get to the base name */
2575 p
= &FileName
->Buffer
[FileName
->Length
/ sizeof(WCHAR
)];
2576 while (*(p
- 1) != OBJ_NAME_PATH_SEPARATOR
) p
--;
2578 /* Get the length */
2579 BaseLength
= (ULONG
)(&FileName
->Buffer
[FileName
->Length
/ sizeof(WCHAR
)] - p
);
2580 BaseLength
*= sizeof(WCHAR
);
2582 /* Setup the string */
2583 BaseName
.Length
= (USHORT
)BaseLength
;
2584 BaseName
.Buffer
= p
;
2588 /* Otherwise, we already have a base name */
2589 BaseName
.Length
= FileName
->Length
;
2590 BaseName
.Buffer
= FileName
->Buffer
;
2593 /* Setup the maximum length */
2594 BaseName
.MaximumLength
= BaseName
.Length
;
2596 /* Now compute the base directory */
2597 BaseDirectory
= *FileName
;
2598 BaseDirectory
.Length
-= BaseName
.Length
;
2599 BaseDirectory
.MaximumLength
= BaseDirectory
.Length
;
2601 /* And the prefix, which for now is just the name itself */
2602 PrefixName
= *FileName
;
2604 /* Check if we have a prefix */
2605 if (NamePrefix
) DPRINT1("Prefixed images are not yet supported!\n");
2607 /* Check if we already have a name, use it instead */
2608 if (LoadedName
) BaseName
= *LoadedName
;
2610 /* Check for loader snap debugging */
2611 if (NtGlobalFlag
& FLG_SHOW_LDR_SNAPS
)
2613 /* Print out standard string */
2614 DPRINT1("MM:SYSLDR Loading %wZ (%wZ) %s\n",
2615 &PrefixName
, &BaseName
, Flags
? "in session space" : "");
2618 /* Acquire the load lock */
2620 ASSERT(LockOwned
== FALSE
);
2622 KeEnterCriticalRegion();
2623 KeWaitForSingleObject(&MmSystemLoadLock
,
2629 /* Scan the module list */
2630 NextEntry
= PsLoadedModuleList
.Flink
;
2631 while (NextEntry
!= &PsLoadedModuleList
)
2633 /* Get the entry and compare the names */
2634 LdrEntry
= CONTAINING_RECORD(NextEntry
,
2635 LDR_DATA_TABLE_ENTRY
,
2637 if (RtlEqualUnicodeString(&PrefixName
, &LdrEntry
->FullDllName
, TRUE
))
2639 /* Found it, break out */
2644 NextEntry
= NextEntry
->Flink
;
2647 /* Check if we found the image */
2648 if (NextEntry
!= &PsLoadedModuleList
)
2650 /* Check if we had already mapped a section */
2653 /* Dereference and clear */
2654 ObDereferenceObject(Section
);
2658 /* Check if this was supposed to be a session load */
2661 /* It wasn't, so just return the data */
2662 *ModuleObject
= LdrEntry
;
2663 *ImageBaseAddress
= LdrEntry
->DllBase
;
2664 Status
= STATUS_IMAGE_ALREADY_LOADED
;
2668 /* We don't support session loading yet */
2669 DPRINT1("Unsupported Session-Load!\n");
2678 /* It wasn't loaded, and we didn't have a previous attempt */
2679 KeReleaseMutant(&MmSystemLoadLock
, 1, FALSE
, FALSE
);
2680 KeLeaveCriticalRegion();
2683 /* Check if KD is enabled */
2684 if ((KdDebuggerEnabled
) && !(KdDebuggerNotPresent
))
2686 /* FIXME: Attempt to get image from KD */
2689 /* We don't have a valid entry */
2692 /* Setup image attributes */
2693 InitializeObjectAttributes(&ObjectAttributes
,
2695 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2699 /* Open the image */
2700 Status
= ZwOpenFile(&FileHandle
,
2704 FILE_SHARE_READ
| FILE_SHARE_DELETE
,
2706 if (!NT_SUCCESS(Status
)) goto Quickie
;
2709 Status
= MmCheckSystemImage(FileHandle
, FALSE
);
2710 if ((Status
== STATUS_IMAGE_CHECKSUM_MISMATCH
) ||
2711 (Status
== STATUS_IMAGE_MP_UP_MISMATCH
) ||
2712 (Status
== STATUS_INVALID_IMAGE_PROTECT
))
2718 /* Check if this is a session-load */
2721 /* Then we only need read and execute */
2722 DesiredAccess
= SECTION_MAP_READ
| SECTION_MAP_EXECUTE
;
2726 /* Otherwise, we can allow write access */
2727 DesiredAccess
= SECTION_ALL_ACCESS
;
2730 /* Initialize the attributes for the section */
2731 InitializeObjectAttributes(&ObjectAttributes
,
2733 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2737 /* Create the section */
2738 Status
= ZwCreateSection(&SectionHandle
,
2745 if (!NT_SUCCESS(Status
)) goto Quickie
;
2747 /* Now get the section pointer */
2748 Status
= ObReferenceObjectByHandle(SectionHandle
,
2749 SECTION_MAP_EXECUTE
,
2750 MmSectionObjectType
,
2754 ZwClose(SectionHandle
);
2755 if (!NT_SUCCESS(Status
)) goto Quickie
;
2757 /* Check if this was supposed to be a session-load */
2760 /* We don't support session loading yet */
2761 DPRINT1("Unsupported Session-Load!\n");
2765 /* Check the loader list again, we should end up in the path below */
2770 /* We don't have a valid entry */
2774 /* Load the image */
2775 Status
= MiLoadImageSection(&Section
,
2780 ASSERT(Status
!= STATUS_ALREADY_COMMITTED
);
2782 /* Get the size of the driver */
2783 DriverSize
= ((PROS_SECTION_OBJECT
)Section
)->ImageSection
->ImageSize
;
2785 /* Make sure we're not being loaded into session space */
2788 /* Check for success */
2789 if (NT_SUCCESS(Status
))
2791 /* Support large pages for drivers */
2792 MiUseLargeDriverPage(DriverSize
/ PAGE_SIZE
,
2798 /* Dereference the section */
2799 ObDereferenceObject(Section
);
2803 /* Check for failure of the load earlier */
2804 if (!NT_SUCCESS(Status
)) goto Quickie
;
2806 /* Relocate the driver */
2807 Status
= LdrRelocateImageWithBias(ModuleLoadBase
,
2811 STATUS_CONFLICTING_ADDRESSES
,
2812 STATUS_INVALID_IMAGE_FORMAT
);
2813 if (!NT_SUCCESS(Status
)) goto Quickie
;
2816 /* Get the NT Header */
2817 NtHeader
= RtlImageNtHeader(ModuleLoadBase
);
2819 /* Calculate the size we'll need for the entry and allocate it */
2820 EntrySize
= sizeof(LDR_DATA_TABLE_ENTRY
) +
2822 sizeof(UNICODE_NULL
);
2824 /* Allocate the entry */
2825 LdrEntry
= ExAllocatePoolWithTag(NonPagedPool
, EntrySize
, TAG_MODULE_OBJECT
);
2829 Status
= STATUS_INSUFFICIENT_RESOURCES
;
2833 /* Setup the entry */
2834 LdrEntry
->Flags
= LDRP_LOAD_IN_PROGRESS
;
2835 LdrEntry
->LoadCount
= 1;
2836 LdrEntry
->LoadedImports
= LoadedImports
;
2837 LdrEntry
->PatchInformation
= NULL
;
2839 /* Check the version */
2840 if ((NtHeader
->OptionalHeader
.MajorOperatingSystemVersion
>= 5) &&
2841 (NtHeader
->OptionalHeader
.MajorImageVersion
>= 5))
2843 /* Mark this image as a native image */
2844 LdrEntry
->Flags
|= LDRP_ENTRY_NATIVE
;
2847 /* Setup the rest of the entry */
2848 LdrEntry
->DllBase
= ModuleLoadBase
;
2849 LdrEntry
->EntryPoint
= (PVOID
)((ULONG_PTR
)ModuleLoadBase
+
2850 NtHeader
->OptionalHeader
.AddressOfEntryPoint
);
2851 LdrEntry
->SizeOfImage
= DriverSize
;
2852 LdrEntry
->CheckSum
= NtHeader
->OptionalHeader
.CheckSum
;
2853 LdrEntry
->SectionPointer
= Section
;
2855 /* Now write the DLL name */
2856 LdrEntry
->BaseDllName
.Buffer
= (PVOID
)(LdrEntry
+ 1);
2857 LdrEntry
->BaseDllName
.Length
= BaseName
.Length
;
2858 LdrEntry
->BaseDllName
.MaximumLength
= BaseName
.Length
;
2860 /* Copy and null-terminate it */
2861 RtlCopyMemory(LdrEntry
->BaseDllName
.Buffer
,
2864 LdrEntry
->BaseDllName
.Buffer
[BaseName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2866 /* Now allocate the full name */
2867 LdrEntry
->FullDllName
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
2869 sizeof(UNICODE_NULL
),
2871 if (!LdrEntry
->FullDllName
.Buffer
)
2873 /* Don't fail, just set it to zero */
2874 LdrEntry
->FullDllName
.Length
= 0;
2875 LdrEntry
->FullDllName
.MaximumLength
= 0;
2880 LdrEntry
->FullDllName
.Length
= PrefixName
.Length
;
2881 LdrEntry
->FullDllName
.MaximumLength
= PrefixName
.Length
;
2883 /* Copy and null-terminate */
2884 RtlCopyMemory(LdrEntry
->FullDllName
.Buffer
,
2887 LdrEntry
->FullDllName
.Buffer
[PrefixName
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2891 MiProcessLoaderEntry(LdrEntry
, TRUE
);
2893 /* Resolve imports */
2894 MissingApiName
= Buffer
;
2895 Status
= MiResolveImageReferences(ModuleLoadBase
,
2901 if (!NT_SUCCESS(Status
))
2904 MiProcessLoaderEntry(LdrEntry
, FALSE
);
2906 /* Check if we need to free the name */
2907 if (LdrEntry
->FullDllName
.Buffer
)
2910 ExFreePool(LdrEntry
->FullDllName
.Buffer
);
2913 /* Free the entry itself */
2914 ExFreePoolWithTag(LdrEntry
, TAG_MODULE_OBJECT
);
2919 /* Update the loader entry */
2920 LdrEntry
->Flags
|= (LDRP_SYSTEM_MAPPED
|
2921 LDRP_ENTRY_PROCESSED
|
2923 LdrEntry
->Flags
&= ~LDRP_LOAD_IN_PROGRESS
;
2924 LdrEntry
->LoadedImports
= LoadedImports
;
2926 /* FIXME: Call driver verifier's loader function */
2928 /* Write-protect the system image */
2929 MiWriteProtectSystemImage(LdrEntry
->DllBase
);
2931 /* Check if notifications are enabled */
2932 if (PsImageNotifyEnabled
)
2934 /* Fill out the notification data */
2935 ImageInfo
.Properties
= 0;
2936 ImageInfo
.ImageAddressingMode
= IMAGE_ADDRESSING_MODE_32BIT
;
2937 ImageInfo
.SystemModeImage
= TRUE
;
2938 ImageInfo
.ImageSize
= LdrEntry
->SizeOfImage
;
2939 ImageInfo
.ImageBase
= LdrEntry
->DllBase
;
2940 ImageInfo
.ImageSectionNumber
= ImageInfo
.ImageSelector
= 0;
2942 /* Send the notification */
2943 PspRunLoadImageNotifyRoutines(FileName
, NULL
, &ImageInfo
);
2946 #if defined(KDBG) || defined(_WINKD_)
2947 /* MiCacheImageSymbols doesn't detect rossym */
2950 /* Check if there's symbols */
2951 if (MiCacheImageSymbols(LdrEntry
->DllBase
))
2954 /* Check if the system root is present */
2955 if ((PrefixName
.Length
> (11 * sizeof(WCHAR
))) &&
2956 !(_wcsnicmp(PrefixName
.Buffer
, L
"\\SystemRoot", 11)))
2958 /* Add the system root */
2959 UnicodeTemp
= PrefixName
;
2960 UnicodeTemp
.Buffer
+= 11;
2961 UnicodeTemp
.Length
-= (11 * sizeof(WCHAR
));
2964 &SharedUserData
->NtSystemRoot
[2],
2969 /* Build the name */
2970 sprintf_nt(Buffer
, "%wZ", &BaseName
);
2973 /* Setup the ansi string */
2974 RtlInitString(&AnsiTemp
, Buffer
);
2976 /* Notify the debugger */
2977 DbgLoadImageSymbols(&AnsiTemp
,
2979 (ULONG_PTR
)ZwCurrentProcess());
2980 LdrEntry
->Flags
|= LDRP_DEBUG_SYMBOLS_LOADED
;
2983 /* Page the driver */
2984 ASSERT(Section
== NULL
);
2985 MiEnablePagingOfDriver(LdrEntry
);
2987 /* Return pointers */
2988 *ModuleObject
= LdrEntry
;
2989 *ImageBaseAddress
= LdrEntry
->DllBase
;
2992 /* Check if we have the lock acquired */
2995 /* Release the lock */
2996 KeReleaseMutant(&MmSystemLoadLock
, 1, FALSE
, FALSE
);
2997 KeLeaveCriticalRegion();
3001 /* If we have a file handle, close it */
3002 if (FileHandle
) ZwClose(FileHandle
);
3004 /* Check if we had a prefix */
3005 if (NamePrefix
) ExFreePool(PrefixName
.Buffer
);
3007 /* Free the name buffer and return status */
3008 ExFreePoolWithTag(Buffer
, TAG_LDR_WSTR
);
3012 PLDR_DATA_TABLE_ENTRY
3014 MiLookupDataTableEntry(IN PVOID Address
)
3016 PLDR_DATA_TABLE_ENTRY LdrEntry
, FoundEntry
= NULL
;
3017 PLIST_ENTRY NextEntry
;
3021 NextEntry
= PsLoadedModuleList
.Flink
;
3024 /* Get the loader entry */
3025 LdrEntry
= CONTAINING_RECORD(NextEntry
,
3026 LDR_DATA_TABLE_ENTRY
,
3029 /* Check if the address matches */
3030 if ((Address
>= LdrEntry
->DllBase
) &&
3031 (Address
< (PVOID
)((ULONG_PTR
)LdrEntry
->DllBase
+
3032 LdrEntry
->SizeOfImage
)))
3035 FoundEntry
= LdrEntry
;
3040 NextEntry
= NextEntry
->Flink
;
3041 } while(NextEntry
!= &PsLoadedModuleList
);
3043 /* Return the entry */
3047 /* PUBLIC FUNCTIONS ***********************************************************/
3054 MmPageEntireDriver(IN PVOID AddressWithinSection
)
3056 PMMPTE StartPte
, EndPte
;
3057 PLDR_DATA_TABLE_ENTRY LdrEntry
;
3060 /* Get the loader entry */
3061 LdrEntry
= MiLookupDataTableEntry(AddressWithinSection
);
3062 if (!LdrEntry
) return NULL
;
3064 /* Check if paging of kernel mode is disabled or if the driver is mapped as an image */
3065 if ((MmDisablePagingExecutive
) || (LdrEntry
->SectionPointer
))
3067 /* Don't do anything, just return the base address */
3068 return LdrEntry
->DllBase
;
3071 /* Wait for active DPCs to finish before we page out the driver */
3072 KeFlushQueuedDpcs();
3074 /* Get the PTE range for the whole driver image */
3075 StartPte
= MiAddressToPte((ULONG_PTR
)LdrEntry
->DllBase
);
3076 EndPte
= MiAddressToPte((ULONG_PTR
)LdrEntry
->DllBase
+ LdrEntry
->SizeOfImage
);
3078 /* Enable paging for the PTE range */
3079 ASSERT(MI_IS_SESSION_IMAGE_ADDRESS(AddressWithinSection
) == FALSE
);
3080 MiSetPagingOfDriver(StartPte
, EndPte
);
3082 /* Return the base address */
3083 return LdrEntry
->DllBase
;
3091 MmResetDriverPaging(IN PVOID AddressWithinSection
)
3101 MmGetSystemRoutineAddress(IN PUNICODE_STRING SystemRoutineName
)
3103 PVOID ProcAddress
= NULL
;
3104 ANSI_STRING AnsiRoutineName
;
3106 PLIST_ENTRY NextEntry
;
3107 PLDR_DATA_TABLE_ENTRY LdrEntry
;
3108 BOOLEAN Found
= FALSE
;
3109 UNICODE_STRING KernelName
= RTL_CONSTANT_STRING(L
"ntoskrnl.exe");
3110 UNICODE_STRING HalName
= RTL_CONSTANT_STRING(L
"hal.dll");
3113 /* Convert routine to ansi name */
3114 Status
= RtlUnicodeStringToAnsiString(&AnsiRoutineName
,
3117 if (!NT_SUCCESS(Status
)) return NULL
;
3120 KeEnterCriticalRegion();
3121 ExAcquireResourceSharedLite(&PsLoadedModuleResource
, TRUE
);
3123 /* Loop the loaded module list */
3124 NextEntry
= PsLoadedModuleList
.Flink
;
3125 while (NextEntry
!= &PsLoadedModuleList
)
3128 LdrEntry
= CONTAINING_RECORD(NextEntry
,
3129 LDR_DATA_TABLE_ENTRY
,
3132 /* Check if it's the kernel or HAL */
3133 if (RtlEqualUnicodeString(&KernelName
, &LdrEntry
->BaseDllName
, TRUE
))
3139 else if (RtlEqualUnicodeString(&HalName
, &LdrEntry
->BaseDllName
, TRUE
))
3146 /* Check if we found a valid binary */
3149 /* Find the procedure name */
3150 ProcAddress
= MiFindExportedRoutineByName(LdrEntry
->DllBase
,
3153 /* Break out if we found it or if we already tried both modules */
3154 if (ProcAddress
) break;
3155 if (Modules
== 2) break;
3159 NextEntry
= NextEntry
->Flink
;
3162 /* Release the lock */
3163 ExReleaseResourceLite(&PsLoadedModuleResource
);
3164 KeLeaveCriticalRegion();
3166 /* Free the string and return */
3167 RtlFreeAnsiString(&AnsiRoutineName
);