673ef30afa13ea779883c556e39ebd2a547be4a7
[reactos.git] / reactos / ntoskrnl / mm / ARM3 / sysldr.c
1 /*
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
8 */
9
10 /* INCLUDES *******************************************************************/
11
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 #line 16 "ARMĀ³::LOADER"
17 #define MODULE_INVOLVED_IN_ARM3
18 #include "../ARM3/miarm.h"
19
20 /* GCC's incompetence strikes again */
21 __inline
22 VOID
23 sprintf_nt(IN PCHAR Buffer,
24 IN PCHAR Format,
25 IN ...)
26 {
27 va_list ap;
28 va_start(ap, Format);
29 vsprintf(Buffer, Format, ap);
30 va_end(ap);
31 }
32
33 /* GLOBALS ********************************************************************/
34
35 LIST_ENTRY PsLoadedModuleList;
36 LIST_ENTRY MmLoadedUserImageList;
37 KSPIN_LOCK PsLoadedModuleSpinLock;
38 ERESOURCE PsLoadedModuleResource;
39 ULONG_PTR PsNtosImageBase;
40 KMUTANT MmSystemLoadLock;
41
42 PFN_NUMBER MmTotalSystemDriverPages;
43
44 PVOID MmUnloadedDrivers;
45 PVOID MmLastUnloadedDrivers;
46
47 BOOLEAN MmMakeLowMemory;
48 BOOLEAN MmEnforceWriteProtection = TRUE;
49
50 PMMPTE MiKernelResourceStartPte, MiKernelResourceEndPte;
51 ULONG_PTR ExPoolCodeStart, ExPoolCodeEnd, MmPoolCodeStart, MmPoolCodeEnd;
52 ULONG_PTR MmPteCodeStart, MmPteCodeEnd;
53
54 /* FUNCTIONS ******************************************************************/
55
56 PVOID
57 NTAPI
58 MiCacheImageSymbols(IN PVOID BaseAddress)
59 {
60 ULONG DebugSize;
61 PVOID DebugDirectory = NULL;
62 PAGED_CODE();
63
64 /* Make sure it's safe to access the image */
65 _SEH2_TRY
66 {
67 /* Get the debug directory */
68 DebugDirectory = RtlImageDirectoryEntryToData(BaseAddress,
69 TRUE,
70 IMAGE_DIRECTORY_ENTRY_DEBUG,
71 &DebugSize);
72 }
73 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
74 {
75 /* Nothing */
76 }
77 _SEH2_END;
78
79 /* Return the directory */
80 return DebugDirectory;
81 }
82
83 NTSTATUS
84 NTAPI
85 MiLoadImageSection(IN OUT PVOID *SectionPtr,
86 OUT PVOID *ImageBase,
87 IN PUNICODE_STRING FileName,
88 IN BOOLEAN SessionLoad,
89 IN PLDR_DATA_TABLE_ENTRY LdrEntry)
90 {
91 PROS_SECTION_OBJECT Section = *SectionPtr;
92 NTSTATUS Status;
93 PEPROCESS Process;
94 PVOID Base = NULL;
95 SIZE_T ViewSize = 0;
96 KAPC_STATE ApcState;
97 LARGE_INTEGER SectionOffset = {{0, 0}};
98 BOOLEAN LoadSymbols = FALSE;
99 PFN_NUMBER PteCount;
100 PMMPTE PointerPte, LastPte;
101 PVOID DriverBase;
102 MMPTE TempPte;
103 PAGED_CODE();
104
105 /* Detect session load */
106 if (SessionLoad)
107 {
108 /* Fail */
109 DPRINT1("Session loading not yet supported!\n");
110 while (TRUE);
111 }
112
113 /* Not session load, shouldn't have an entry */
114 ASSERT(LdrEntry == NULL);
115
116 /* Attach to the system process */
117 KeStackAttachProcess(&PsInitialSystemProcess->Pcb, &ApcState);
118
119 /* Check if we need to load symbols */
120 if (NtGlobalFlag & FLG_ENABLE_KDEBUG_SYMBOL_LOAD)
121 {
122 /* Yes we do */
123 LoadSymbols = TRUE;
124 NtGlobalFlag &= ~FLG_ENABLE_KDEBUG_SYMBOL_LOAD;
125 }
126
127 /* Map the driver */
128 Process = PsGetCurrentProcess();
129 Status = MmMapViewOfSection(Section,
130 Process,
131 &Base,
132 0,
133 0,
134 &SectionOffset,
135 &ViewSize,
136 ViewUnmap,
137 0,
138 PAGE_EXECUTE);
139
140 /* Re-enable the flag */
141 if (LoadSymbols) NtGlobalFlag |= FLG_ENABLE_KDEBUG_SYMBOL_LOAD;
142
143 /* Check if we failed with distinguished status code */
144 if (Status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH)
145 {
146 /* Change it to something more generic */
147 Status = STATUS_INVALID_IMAGE_FORMAT;
148 }
149
150 /* Now check if we failed */
151 if (!NT_SUCCESS(Status))
152 {
153 /* Detach and return */
154 KeUnstackDetachProcess(&ApcState);
155 return Status;
156 }
157
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;
162
163 /* New driver base */
164 LastPte = PointerPte + PteCount;
165 DriverBase = MiPteToAddress(PointerPte);
166
167 /* The driver is here */
168 *ImageBase = DriverBase;
169 DPRINT1("Loading: %wZ at %p with %lx pages\n", FileName, DriverBase, PteCount);
170
171 /* Loop the new driver PTEs */
172 TempPte = ValidKernelPte;
173 while (PointerPte < LastPte)
174 {
175 /* Allocate a page */
176 MI_SET_USAGE(MI_USAGE_DRIVER_PAGE);
177 #if MI_TRACE_PFNS
178 PWCHAR pos = NULL;
179 ULONG len = 0;
180 if (FileName->Buffer)
181 {
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);
185 }
186 #endif
187 TempPte.u.Hard.PageFrameNumber = MiAllocatePfn(PointerPte, MM_EXECUTE);
188
189 /* Write it */
190 MI_WRITE_VALID_PTE(PointerPte, TempPte);
191
192 /* Move on */
193 PointerPte++;
194 }
195
196 /* Copy the image */
197 RtlCopyMemory(DriverBase, Base, PteCount << PAGE_SHIFT);
198
199 /* Now unmap the view */
200 Status = MmUnmapViewOfSection(Process, Base);
201 ASSERT(NT_SUCCESS(Status));
202
203 /* Detach and return status */
204 KeUnstackDetachProcess(&ApcState);
205 return Status;
206 }
207
208 PVOID
209 NTAPI
210 MiLocateExportName(IN PVOID DllBase,
211 IN PCHAR ExportName)
212 {
213 PULONG NameTable;
214 PUSHORT OrdinalTable;
215 PIMAGE_EXPORT_DIRECTORY ExportDirectory;
216 LONG Low = 0, Mid = 0, High, Ret;
217 USHORT Ordinal;
218 PVOID Function;
219 ULONG ExportSize;
220 PULONG ExportTable;
221 PAGED_CODE();
222
223 /* Get the export directory */
224 ExportDirectory = RtlImageDirectoryEntryToData(DllBase,
225 TRUE,
226 IMAGE_DIRECTORY_ENTRY_EXPORT,
227 &ExportSize);
228 if (!ExportDirectory) return NULL;
229
230 /* Setup name tables */
231 NameTable = (PULONG)((ULONG_PTR)DllBase +
232 ExportDirectory->AddressOfNames);
233 OrdinalTable = (PUSHORT)((ULONG_PTR)DllBase +
234 ExportDirectory->AddressOfNameOrdinals);
235
236 /* Do a binary search */
237 High = ExportDirectory->NumberOfNames - 1;
238 while (High >= Low)
239 {
240 /* Get new middle value */
241 Mid = (Low + High) >> 1;
242
243 /* Compare name */
244 Ret = strcmp(ExportName, (PCHAR)DllBase + NameTable[Mid]);
245 if (Ret < 0)
246 {
247 /* Update high */
248 High = Mid - 1;
249 }
250 else if (Ret > 0)
251 {
252 /* Update low */
253 Low = Mid + 1;
254 }
255 else
256 {
257 /* We got it */
258 break;
259 }
260 }
261
262 /* Check if we couldn't find it */
263 if (High < Low) return NULL;
264
265 /* Otherwise, this is the ordinal */
266 Ordinal = OrdinalTable[Mid];
267
268 /* Resolve the address and write it */
269 ExportTable = (PULONG)((ULONG_PTR)DllBase +
270 ExportDirectory->AddressOfFunctions);
271 Function = (PVOID)((ULONG_PTR)DllBase + ExportTable[Ordinal]);
272
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)))
276 {
277 /* It is, fail */
278 return NULL;
279 }
280
281 /* We found it */
282 return Function;
283 }
284
285 NTSTATUS
286 NTAPI
287 MmCallDllInitialize(IN PLDR_DATA_TABLE_ENTRY LdrEntry,
288 IN PLIST_ENTRY ListHead)
289 {
290 UNICODE_STRING ServicesKeyName = RTL_CONSTANT_STRING(
291 L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\");
292 PMM_DLL_INITIALIZE DllInit;
293 UNICODE_STRING RegPath, ImportName;
294 NTSTATUS Status;
295
296 /* Try to see if the image exports a DllInitialize routine */
297 DllInit = (PMM_DLL_INITIALIZE)MiLocateExportName(LdrEntry->DllBase,
298 "DllInitialize");
299 if (!DllInit) return STATUS_SUCCESS;
300
301 /* Do a temporary copy of BaseDllName called ImportName
302 * because we'll alter the length of the string
303 */
304 ImportName.Length = LdrEntry->BaseDllName.Length;
305 ImportName.MaximumLength = LdrEntry->BaseDllName.MaximumLength;
306 ImportName.Buffer = LdrEntry->BaseDllName.Buffer;
307
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,
313 TAG_LDR_WSTR);
314
315 /* Check if this allocation was unsuccessful */
316 if (!RegPath.Buffer) return STATUS_INSUFFICIENT_RESOURCES;
317
318 /* Build and append the service name itself */
319 RegPath.Length = ServicesKeyName.Length;
320 RtlCopyMemory(RegPath.Buffer,
321 ServicesKeyName.Buffer,
322 ServicesKeyName.Length);
323
324 /* Check if there is a dot in the filename */
325 if (wcschr(ImportName.Buffer, L'.'))
326 {
327 /* Remove the extension */
328 ImportName.Length = (wcschr(ImportName.Buffer, L'.') -
329 ImportName.Buffer) * sizeof(WCHAR);
330 }
331
332 /* Append service name (the basename without extension) */
333 RtlAppendUnicodeStringToString(&RegPath, &ImportName);
334
335 /* Now call the DllInit func */
336 DPRINT("Calling DllInit(%wZ)\n", &RegPath);
337 Status = DllInit(&RegPath);
338
339 /* Clean up */
340 ExFreePool(RegPath.Buffer);
341
342 /* Return status value which DllInitialize returned */
343 return Status;
344 }
345
346 BOOLEAN
347 NTAPI
348 MiCallDllUnloadAndUnloadDll(IN PLDR_DATA_TABLE_ENTRY LdrEntry)
349 {
350 NTSTATUS Status;
351 PMM_DLL_UNLOAD Func;
352 PAGED_CODE();
353
354 /* Get the unload routine */
355 Func = (PMM_DLL_UNLOAD)MiLocateExportName(LdrEntry->DllBase, "DllUnload");
356 if (!Func) return FALSE;
357
358 /* Call it and check for success */
359 Status = Func();
360 if (!NT_SUCCESS(Status)) return FALSE;
361
362 /* Lie about the load count so we can unload the image */
363 ASSERT(LdrEntry->LoadCount == 0);
364 LdrEntry->LoadCount = 1;
365
366 /* Unload it and return true */
367 MmUnloadSystemImage(LdrEntry);
368 return TRUE;
369 }
370
371 NTSTATUS
372 NTAPI
373 MiDereferenceImports(IN PLOAD_IMPORTS ImportList)
374 {
375 SIZE_T i;
376 LOAD_IMPORTS SingleEntry;
377 PLDR_DATA_TABLE_ENTRY LdrEntry;
378 PVOID CurrentImports;
379 PAGED_CODE();
380
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))
385 {
386 /* Then there's nothing to do */
387 return STATUS_SUCCESS;
388 }
389
390 /* Check for single-entry */
391 if ((ULONG_PTR)ImportList & MM_SYSLDR_SINGLE_ENTRY)
392 {
393 /* Set it up */
394 SingleEntry.Count = 1;
395 SingleEntry.Entry[0] = (PVOID)((ULONG_PTR)ImportList &~ MM_SYSLDR_SINGLE_ENTRY);
396
397 /* Use this as the import list */
398 ImportList = &SingleEntry;
399 }
400
401 /* Loop the import list */
402 for (i = 0; (i < ImportList->Count) && (ImportList->Entry[i]); i++)
403 {
404 /* Get the entry */
405 LdrEntry = ImportList->Entry[i];
406 DPRINT1("%wZ <%wZ>\n", &LdrEntry->FullDllName, &LdrEntry->BaseDllName);
407
408 /* Skip boot loaded images */
409 if (LdrEntry->LoadedImports == MM_SYSLDR_BOOT_LOADED) continue;
410
411 /* Dereference the entry */
412 ASSERT(LdrEntry->LoadCount >= 1);
413 if (!--LdrEntry->LoadCount)
414 {
415 /* Save the import data in case unload fails */
416 CurrentImports = LdrEntry->LoadedImports;
417
418 /* This is the last entry */
419 LdrEntry->LoadedImports = MM_SYSLDR_NO_IMPORTS;
420 if (MiCallDllUnloadAndUnloadDll(LdrEntry))
421 {
422 /* Unloading worked, parse this DLL's imports too */
423 MiDereferenceImports(CurrentImports);
424
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))
429 {
430 /* Free them */
431 ExFreePool(CurrentImports);
432 }
433 }
434 else
435 {
436 /* Unload failed, restore imports */
437 LdrEntry->LoadedImports = CurrentImports;
438 }
439 }
440 }
441
442 /* Done */
443 return STATUS_SUCCESS;
444 }
445
446 VOID
447 NTAPI
448 MiClearImports(IN PLDR_DATA_TABLE_ENTRY LdrEntry)
449 {
450 PAGED_CODE();
451
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))
456 {
457 /* Nothing to do */
458 return;
459 }
460
461 /* Otherwise, free the import list */
462 ExFreePool(LdrEntry->LoadedImports);
463 LdrEntry->LoadedImports = MM_SYSLDR_BOOT_LOADED;
464 }
465
466 PVOID
467 NTAPI
468 MiFindExportedRoutineByName(IN PVOID DllBase,
469 IN PANSI_STRING ExportName)
470 {
471 PULONG NameTable;
472 PUSHORT OrdinalTable;
473 PIMAGE_EXPORT_DIRECTORY ExportDirectory;
474 LONG Low = 0, Mid = 0, High, Ret;
475 USHORT Ordinal;
476 PVOID Function;
477 ULONG ExportSize;
478 PULONG ExportTable;
479 PAGED_CODE();
480
481 /* Get the export directory */
482 ExportDirectory = RtlImageDirectoryEntryToData(DllBase,
483 TRUE,
484 IMAGE_DIRECTORY_ENTRY_EXPORT,
485 &ExportSize);
486 if (!ExportDirectory) return NULL;
487
488 /* Setup name tables */
489 NameTable = (PULONG)((ULONG_PTR)DllBase +
490 ExportDirectory->AddressOfNames);
491 OrdinalTable = (PUSHORT)((ULONG_PTR)DllBase +
492 ExportDirectory->AddressOfNameOrdinals);
493
494 /* Do a binary search */
495 High = ExportDirectory->NumberOfNames - 1;
496 while (High >= Low)
497 {
498 /* Get new middle value */
499 Mid = (Low + High) >> 1;
500
501 /* Compare name */
502 Ret = strcmp(ExportName->Buffer, (PCHAR)DllBase + NameTable[Mid]);
503 if (Ret < 0)
504 {
505 /* Update high */
506 High = Mid - 1;
507 }
508 else if (Ret > 0)
509 {
510 /* Update low */
511 Low = Mid + 1;
512 }
513 else
514 {
515 /* We got it */
516 break;
517 }
518 }
519
520 /* Check if we couldn't find it */
521 if (High < Low) return NULL;
522
523 /* Otherwise, this is the ordinal */
524 Ordinal = OrdinalTable[Mid];
525
526 /* Validate the ordinal */
527 if (Ordinal >= ExportDirectory->NumberOfFunctions) return NULL;
528
529 /* Resolve the address and write it */
530 ExportTable = (PULONG)((ULONG_PTR)DllBase +
531 ExportDirectory->AddressOfFunctions);
532 Function = (PVOID)((ULONG_PTR)DllBase + ExportTable[Ordinal]);
533
534 /* We found it! */
535 ASSERT(!(Function > (PVOID)ExportDirectory) &&
536 (Function < (PVOID)((ULONG_PTR)ExportDirectory + ExportSize)));
537 return Function;
538 }
539
540 VOID
541 NTAPI
542 MiProcessLoaderEntry(IN PLDR_DATA_TABLE_ENTRY LdrEntry,
543 IN BOOLEAN Insert)
544 {
545 KIRQL OldIrql;
546
547 /* Acquire module list lock */
548 KeEnterCriticalRegion();
549 ExAcquireResourceExclusiveLite(&PsLoadedModuleResource, TRUE);
550
551 /* Acquire the spinlock too as we will insert or remove the entry */
552 OldIrql = KeAcquireSpinLockRaiseToSynch(&PsLoadedModuleSpinLock);
553
554 /* Insert or remove from the list */
555 Insert ? InsertTailList(&PsLoadedModuleList, &LdrEntry->InLoadOrderLinks) :
556 RemoveEntryList(&LdrEntry->InLoadOrderLinks);
557
558 /* Release locks */
559 KeReleaseSpinLock(&PsLoadedModuleSpinLock, OldIrql);
560 ExReleaseResourceLite(&PsLoadedModuleResource);
561 KeLeaveCriticalRegion();
562 }
563
564 VOID
565 NTAPI
566 INIT_FUNCTION
567 MiUpdateThunks(IN PLOADER_PARAMETER_BLOCK LoaderBlock,
568 IN PVOID OldBase,
569 IN PVOID NewBase,
570 IN ULONG Size)
571 {
572 ULONG_PTR OldBaseTop, Delta;
573 PLDR_DATA_TABLE_ENTRY LdrEntry;
574 PLIST_ENTRY NextEntry;
575 ULONG ImportSize;
576 //
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.
585 //
586 #ifdef _WORKING_LINKER_
587 ULONG i;
588 #endif
589 PULONG_PTR ImageThunk;
590 PIMAGE_IMPORT_DESCRIPTOR ImportDescriptor;
591
592 /* Calculate the top and delta */
593 OldBaseTop = (ULONG_PTR)OldBase + Size - 1;
594 Delta = (ULONG_PTR)NewBase - (ULONG_PTR)OldBase;
595
596 /* Loop the loader block */
597 for (NextEntry = LoaderBlock->LoadOrderListHead.Flink;
598 NextEntry != &LoaderBlock->LoadOrderListHead;
599 NextEntry = NextEntry->Flink)
600 {
601 /* Get the loader entry */
602 LdrEntry = CONTAINING_RECORD(NextEntry,
603 LDR_DATA_TABLE_ENTRY,
604 InLoadOrderLinks);
605 #ifdef _WORKING_LINKER_
606 /* Get the IAT */
607 ImageThunk = RtlImageDirectoryEntryToData(LdrEntry->DllBase,
608 TRUE,
609 IMAGE_DIRECTORY_ENTRY_IAT,
610 &ImportSize);
611 if (!ImageThunk) continue;
612
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++)
616 {
617 /* Check if it's within this module */
618 if ((*ImageThunk >= (ULONG_PTR)OldBase) && (*ImageThunk <= OldBaseTop))
619 {
620 /* Relocate it */
621 DPRINT("[Mm0]: Updating IAT at: %p. Old Entry: %p. New Entry: %p.\n",
622 ImageThunk, *ImageThunk, *ImageThunk + Delta);
623 *ImageThunk += Delta;
624 }
625 }
626 #else
627 /* Get the import table */
628 ImportDescriptor = RtlImageDirectoryEntryToData(LdrEntry->DllBase,
629 TRUE,
630 IMAGE_DIRECTORY_ENTRY_IMPORT,
631 &ImportSize);
632 if (!ImportDescriptor) continue;
633
634 /* Make sure we have an IAT */
635 DPRINT("[Mm0]: Updating thunks in: %wZ\n", &LdrEntry->BaseDllName);
636 while ((ImportDescriptor->Name) &&
637 (ImportDescriptor->OriginalFirstThunk))
638 {
639 /* Get the image thunk */
640 ImageThunk = (PVOID)((ULONG_PTR)LdrEntry->DllBase +
641 ImportDescriptor->FirstThunk);
642 while (*ImageThunk)
643 {
644 /* Check if it's within this module */
645 if ((*ImageThunk >= (ULONG_PTR)OldBase) && (*ImageThunk <= OldBaseTop))
646 {
647 /* Relocate it */
648 DPRINT("[Mm0]: Updating IAT at: %p. Old Entry: %p. New Entry: %p.\n",
649 ImageThunk, *ImageThunk, *ImageThunk + Delta);
650 *ImageThunk += Delta;
651 }
652
653 /* Go to the next thunk */
654 ImageThunk++;
655 }
656
657 /* Go to the next import */
658 ImportDescriptor++;
659 }
660 #endif
661 }
662 }
663
664 NTSTATUS
665 NTAPI
666 MiSnapThunk(IN PVOID DllBase,
667 IN PVOID ImageBase,
668 IN PIMAGE_THUNK_DATA Name,
669 IN PIMAGE_THUNK_DATA Address,
670 IN PIMAGE_EXPORT_DIRECTORY ExportDirectory,
671 IN ULONG ExportSize,
672 IN BOOLEAN SnapForwarder,
673 OUT PCHAR *MissingApi)
674 {
675 BOOLEAN IsOrdinal;
676 USHORT Ordinal;
677 PULONG NameTable;
678 PUSHORT OrdinalTable;
679 PIMAGE_IMPORT_BY_NAME NameImport;
680 USHORT Hint;
681 ULONG Low = 0, Mid = 0, High;
682 LONG Ret;
683 NTSTATUS Status;
684 PCHAR MissingForwarder;
685 CHAR NameBuffer[MAXIMUM_FILENAME_LENGTH];
686 PULONG ExportTable;
687 ANSI_STRING DllName;
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;
694 ULONG ForwardLength;
695 IMAGE_THUNK_DATA ForwardThunk;
696 PAGED_CODE();
697
698 /* Check if this is an ordinal */
699 IsOrdinal = IMAGE_SNAP_BY_ORDINAL(Name->u1.Ordinal);
700 if ((IsOrdinal) && !(SnapForwarder))
701 {
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;
706 }
707 else
708 {
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;
712
713 /* Copy the procedure name */
714 strncpy(*MissingApi,
715 (PCHAR)&NameImport->Name[0],
716 MAXIMUM_FILENAME_LENGTH - 1);
717
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);
724
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])))
729 {
730 /* We have a match, get the ordinal number from here */
731 Ordinal = OrdinalTable[Hint];
732 }
733 else
734 {
735 /* Do a binary search */
736 High = ExportDirectory->NumberOfNames - 1;
737 while (High >= Low)
738 {
739 /* Get new middle value */
740 Mid = (Low + High) >> 1;
741
742 /* Compare name */
743 Ret = strcmp((PCHAR)NameImport->Name, (PCHAR)DllBase + NameTable[Mid]);
744 if (Ret < 0)
745 {
746 /* Update high */
747 High = Mid - 1;
748 }
749 else if (Ret > 0)
750 {
751 /* Update low */
752 Low = Mid + 1;
753 }
754 else
755 {
756 /* We got it */
757 break;
758 }
759 }
760
761 /* Check if we couldn't find it */
762 if (High < Low)
763 {
764 DPRINT1("Warning: Driver failed to load, %s not found\n", NameImport->Name);
765 return STATUS_DRIVER_ENTRYPOINT_NOT_FOUND;
766 }
767
768 /* Otherwise, this is the ordinal */
769 Ordinal = OrdinalTable[Mid];
770 }
771 }
772
773 /* Check if the ordinal is invalid */
774 if (Ordinal >= ExportDirectory->NumberOfFunctions)
775 {
776 /* Fail */
777 Status = STATUS_DRIVER_ORDINAL_NOT_FOUND;
778 }
779 else
780 {
781 /* In case the forwarder is missing */
782 MissingForwarder = NameBuffer;
783
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];
788
789 /* Assume success from now on */
790 Status = STATUS_SUCCESS;
791
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)))
795 {
796 /* Now assume failure in case the forwarder doesn't exist */
797 Status = STATUS_DRIVER_ENTRYPOINT_NOT_FOUND;
798
799 /* Build the forwarder name */
800 DllName.Buffer = (PCHAR)Address->u1.Function;
801 DllName.Length = strchr(DllName.Buffer, '.') -
802 DllName.Buffer +
803 sizeof(ANSI_NULL);
804 DllName.MaximumLength = DllName.Length;
805
806 /* Convert it */
807 if (!NT_SUCCESS(RtlAnsiStringToUnicodeString(&ForwarderName,
808 &DllName,
809 TRUE)))
810 {
811 /* We failed, just return an error */
812 return Status;
813 }
814
815 /* Loop the module list */
816 NextEntry = PsLoadedModuleList.Flink;
817 while (NextEntry != &PsLoadedModuleList)
818 {
819 /* Get the loader entry */
820 LdrEntry = CONTAINING_RECORD(NextEntry,
821 LDR_DATA_TABLE_ENTRY,
822 InLoadOrderLinks);
823
824 /* Check if it matches */
825 if (RtlPrefixString((PSTRING)&ForwarderName,
826 (PSTRING)&LdrEntry->BaseDllName,
827 TRUE))
828 {
829 /* Get the forwarder export directory */
830 ForwardExportDirectory =
831 RtlImageDirectoryEntryToData(LdrEntry->DllBase,
832 TRUE,
833 IMAGE_DIRECTORY_ENTRY_EXPORT,
834 &ForwardExportSize);
835 if (!ForwardExportDirectory) break;
836
837 /* Allocate a name entry */
838 ForwardLength = strlen(DllName.Buffer + DllName.Length) +
839 sizeof(ANSI_NULL);
840 ForwardName = ExAllocatePoolWithTag(PagedPool,
841 sizeof(*ForwardName) +
842 ForwardLength,
843 TAG_LDR_WSTR);
844 if (!ForwardName) break;
845
846 /* Copy the data */
847 RtlCopyMemory(&ForwardName->Name[0],
848 DllName.Buffer + DllName.Length,
849 ForwardLength);
850 ForwardName->Hint = 0;
851
852 /* Set the new address */
853 ForwardThunk.u1.AddressOfData = (ULONG_PTR)ForwardName;
854
855 /* Snap the forwarder */
856 Status = MiSnapThunk(LdrEntry->DllBase,
857 ImageBase,
858 &ForwardThunk,
859 &ForwardThunk,
860 ForwardExportDirectory,
861 ForwardExportSize,
862 TRUE,
863 &MissingForwarder);
864
865 /* Free the forwarder name and set the thunk */
866 ExFreePoolWithTag(ForwardName, TAG_LDR_WSTR);
867 Address->u1 = ForwardThunk.u1;
868 break;
869 }
870
871 /* Go to the next entry */
872 NextEntry = NextEntry->Flink;
873 }
874
875 /* Free the name */
876 RtlFreeUnicodeString(&ForwarderName);
877 }
878 }
879
880 /* Return status */
881 return Status;
882 }
883
884 NTSTATUS
885 NTAPI
886 MmUnloadSystemImage(IN PVOID ImageHandle)
887 {
888 PLDR_DATA_TABLE_ENTRY LdrEntry = ImageHandle;
889 PVOID BaseAddress = LdrEntry->DllBase;
890 NTSTATUS Status;
891 STRING TempName;
892 BOOLEAN HadEntry = FALSE;
893
894 /* Acquire the loader lock */
895 KeEnterCriticalRegion();
896 KeWaitForSingleObject(&MmSystemLoadLock,
897 WrVirtualMemory,
898 KernelMode,
899 FALSE,
900 NULL);
901
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;
904
905 /* We should still be alive */
906 ASSERT(LdrEntry->LoadCount != 0);
907 LdrEntry->LoadCount--;
908
909 /* Check if we're still loaded */
910 if (LdrEntry->LoadCount) goto Done;
911
912 /* We should cleanup... are symbols loaded */
913 if (LdrEntry->Flags & LDRP_DEBUG_SYMBOLS_LOADED)
914 {
915 /* Create the ANSI name */
916 Status = RtlUnicodeStringToAnsiString(&TempName,
917 &LdrEntry->BaseDllName,
918 TRUE);
919 if (NT_SUCCESS(Status))
920 {
921 /* Unload the symbols */
922 DbgUnLoadImageSymbols(&TempName,
923 BaseAddress,
924 (ULONG_PTR)ZwCurrentProcess());
925 RtlFreeAnsiString(&TempName);
926 }
927 }
928
929 /* FIXME: Free the driver */
930 DPRINT1("Leaking driver: %wZ\n", &LdrEntry->BaseDllName);
931 //MmFreeSection(LdrEntry->DllBase);
932
933 /* Check if we're linked in */
934 if (LdrEntry->InLoadOrderLinks.Flink)
935 {
936 /* Remove us */
937 MiProcessLoaderEntry(LdrEntry, FALSE);
938 HadEntry = TRUE;
939 }
940
941 /* Dereference and clear the imports */
942 MiDereferenceImports(LdrEntry->LoadedImports);
943 MiClearImports(LdrEntry);
944
945 /* Check if the entry needs to go away */
946 if (HadEntry)
947 {
948 /* Check if it had a name */
949 if (LdrEntry->FullDllName.Buffer)
950 {
951 /* Free it */
952 ExFreePool(LdrEntry->FullDllName.Buffer);
953 }
954
955 /* Check if we had a section */
956 if (LdrEntry->SectionPointer)
957 {
958 /* Dereference it */
959 ObDereferenceObject(LdrEntry->SectionPointer);
960 }
961
962 /* Free the entry */
963 ExFreePool(LdrEntry);
964 }
965
966 /* Release the system lock and return */
967 Done:
968 KeReleaseMutant(&MmSystemLoadLock, 1, FALSE, FALSE);
969 KeLeaveCriticalRegion();
970 return STATUS_SUCCESS;
971 }
972
973 NTSTATUS
974 NTAPI
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)
981 {
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;
994 NTSTATUS Status;
995 PIMAGE_THUNK_DATA OrigThunk, FirstThunk;
996 PAGED_CODE();
997 DPRINT("%s - ImageBase: %p. ImageFileDirectory: %wZ\n",
998 __FUNCTION__, ImageBase, ImageFileDirectory);
999
1000 /* Assume no imports */
1001 *LoadImports = MM_SYSLDR_NO_IMPORTS;
1002
1003 /* Get the import descriptor */
1004 ImportDescriptor = RtlImageDirectoryEntryToData(ImageBase,
1005 TRUE,
1006 IMAGE_DIRECTORY_ENTRY_IMPORT,
1007 &ImportSize);
1008 if (!ImportDescriptor) return STATUS_SUCCESS;
1009
1010 /* Loop all imports to count them */
1011 for (CurrentImport = ImportDescriptor;
1012 (CurrentImport->Name) && (CurrentImport->OriginalFirstThunk);
1013 CurrentImport++)
1014 {
1015 /* One more */
1016 ImportCount++;
1017 }
1018
1019 /* Make sure we have non-zero imports */
1020 if (ImportCount)
1021 {
1022 /* Calculate and allocate the list we'll need */
1023 LoadedImportsSize = ImportCount * sizeof(PVOID) + sizeof(SIZE_T);
1024 LoadedImports = ExAllocatePoolWithTag(PagedPool,
1025 LoadedImportsSize,
1026 'TDmM');
1027 if (LoadedImports)
1028 {
1029 /* Zero it */
1030 RtlZeroMemory(LoadedImports, LoadedImportsSize);
1031 LoadedImports->Count = ImportCount;
1032 }
1033 }
1034 else
1035 {
1036 /* No table */
1037 LoadedImports = NULL;
1038 }
1039
1040 /* Reset the import count and loop descriptors again */
1041 ImportCount = GdiLink = NormalLink = 0;
1042 while ((ImportDescriptor->Name) && (ImportDescriptor->OriginalFirstThunk))
1043 {
1044 /* Get the name */
1045 ImportName = (PCHAR)((ULONG_PTR)ImageBase + ImportDescriptor->Name);
1046
1047 /* Check if this is a GDI driver */
1048 GdiLink = GdiLink |
1049 !(_strnicmp(ImportName, "win32k", sizeof("win32k") - 1));
1050
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)));
1057
1058 /* Check if this is a valid GDI driver */
1059 if ((GdiLink) && (NormalLink))
1060 {
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;
1065 }
1066
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)))
1074 {
1075 /* This is not kernel code */
1076 MiDereferenceImports(LoadedImports);
1077 if (LoadedImports) ExFreePool(LoadedImports);
1078 return STATUS_PROCEDURE_NOT_FOUND;
1079 }
1080
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)))
1085 {
1086 /* Don't reference this */
1087 ReferenceNeeded = FALSE;
1088 }
1089 else
1090 {
1091 /* Reference these modules */
1092 ReferenceNeeded = TRUE;
1093 }
1094
1095 /* Now setup a unicode string for the import */
1096 RtlInitAnsiString(&TempString, ImportName);
1097 Status = RtlAnsiStringToUnicodeString(&NameString, &TempString, TRUE);
1098 if (!NT_SUCCESS(Status))
1099 {
1100 /* Failed */
1101 MiDereferenceImports(LoadedImports);
1102 if (LoadedImports) ExFreePoolWithTag(LoadedImports, TAG_LDR_WSTR);
1103 return Status;
1104 }
1105
1106 /* We don't support name prefixes yet */
1107 if (NamePrefix) DPRINT1("Name Prefix not yet supported!\n");
1108
1109 /* Remember that we haven't loaded the import at this point */
1110 CheckDllState:
1111 Loaded = FALSE;
1112 ImportBase = NULL;
1113
1114 /* Loop the driver list */
1115 NextEntry = PsLoadedModuleList.Flink;
1116 while (NextEntry != &PsLoadedModuleList)
1117 {
1118 /* Get the loader entry and compare the name */
1119 LdrEntry = CONTAINING_RECORD(NextEntry,
1120 LDR_DATA_TABLE_ENTRY,
1121 InLoadOrderLinks);
1122 if (RtlEqualUnicodeString(&NameString,
1123 &LdrEntry->BaseDllName,
1124 TRUE))
1125 {
1126 /* Get the base address */
1127 ImportBase = LdrEntry->DllBase;
1128
1129 /* Check if we haven't loaded yet, and we need references */
1130 if (!(Loaded) && (ReferenceNeeded))
1131 {
1132 /* Make sure we're not already loading */
1133 if (!(LdrEntry->Flags & LDRP_LOAD_IN_PROGRESS))
1134 {
1135 /* Increase the load count */
1136 LdrEntry->LoadCount++;
1137 }
1138 }
1139
1140 /* Done, break out */
1141 break;
1142 }
1143
1144 /* Go to the next entry */
1145 NextEntry = NextEntry->Flink;
1146 }
1147
1148 /* Check if we haven't loaded the import yet */
1149 if (!ImportBase)
1150 {
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,
1157 'TDmM');
1158 if (DllName.Buffer)
1159 {
1160 /* Setup the base length and copy it */
1161 DllName.Length = ImageFileDirectory->Length;
1162 RtlCopyMemory(DllName.Buffer,
1163 ImageFileDirectory->Buffer,
1164 ImageFileDirectory->Length);
1165
1166 /* Now add the import name and null-terminate it */
1167 RtlAppendUnicodeStringToString(&DllName,
1168 &NameString);
1169 DllName.Buffer[DllName.Length / sizeof(WCHAR)] = UNICODE_NULL;
1170
1171 /* Load the image */
1172 Status = MmLoadSystemImage(&DllName,
1173 NamePrefix,
1174 NULL,
1175 FALSE,
1176 (PVOID)&DllEntry,
1177 &DllBase);
1178 if (NT_SUCCESS(Status))
1179 {
1180 /* We can free the DLL Name */
1181 ExFreePool(DllName.Buffer);
1182 }
1183 else
1184 {
1185 /* Fill out the information for the error */
1186 *MissingDriver = DllName.Buffer;
1187 *(PULONG)MissingDriver |= 1;
1188 *MissingApi = NULL;
1189 }
1190 }
1191 else
1192 {
1193 /* We're out of resources */
1194 Status = STATUS_INSUFFICIENT_RESOURCES;
1195 }
1196
1197 /* Check if we're OK until now */
1198 if (NT_SUCCESS(Status))
1199 {
1200 /* We're now loaded */
1201 Loaded = TRUE;
1202
1203 /* Sanity check */
1204 ASSERT(DllBase = DllEntry->DllBase);
1205
1206 /* Call the initialization routines */
1207 Status = MmCallDllInitialize(DllEntry, &PsLoadedModuleList);
1208 if (!NT_SUCCESS(Status))
1209 {
1210 /* We failed, unload the image */
1211 MmUnloadSystemImage(DllEntry);
1212 while (TRUE);
1213 Loaded = FALSE;
1214 }
1215 }
1216
1217 /* Check if we failed by here */
1218 if (!NT_SUCCESS(Status))
1219 {
1220 /* Cleanup and return */
1221 RtlFreeUnicodeString(&NameString);
1222 MiDereferenceImports(LoadedImports);
1223 if (LoadedImports) ExFreePoolWithTag(LoadedImports, TAG_LDR_WSTR);
1224 return Status;
1225 }
1226
1227 /* Loop again to make sure that everything is OK */
1228 goto CheckDllState;
1229 }
1230
1231 /* Check if we're support to reference this import */
1232 if ((ReferenceNeeded) && (LoadedImports))
1233 {
1234 /* Make sure we're not already loading */
1235 if (!(LdrEntry->Flags & LDRP_LOAD_IN_PROGRESS))
1236 {
1237 /* Add the entry */
1238 LoadedImports->Entry[ImportCount] = LdrEntry;
1239 ImportCount++;
1240 }
1241 }
1242
1243 /* Free the import name */
1244 RtlFreeUnicodeString(&NameString);
1245
1246 /* Set the missing driver name and get the export directory */
1247 *MissingDriver = LdrEntry->BaseDllName.Buffer;
1248 ExportDirectory = RtlImageDirectoryEntryToData(ImportBase,
1249 TRUE,
1250 IMAGE_DIRECTORY_ENTRY_EXPORT,
1251 &ExportSize);
1252 if (!ExportDirectory)
1253 {
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;
1259 }
1260
1261 /* Make sure we have an IAT */
1262 if (ImportDescriptor->OriginalFirstThunk)
1263 {
1264 /* Get the first thunks */
1265 OrigThunk = (PVOID)((ULONG_PTR)ImageBase +
1266 ImportDescriptor->OriginalFirstThunk);
1267 FirstThunk = (PVOID)((ULONG_PTR)ImageBase +
1268 ImportDescriptor->FirstThunk);
1269
1270 /* Loop the IAT */
1271 while (OrigThunk->u1.AddressOfData)
1272 {
1273 /* Snap thunk */
1274 Status = MiSnapThunk(ImportBase,
1275 ImageBase,
1276 OrigThunk++,
1277 FirstThunk++,
1278 ExportDirectory,
1279 ExportSize,
1280 FALSE,
1281 MissingApi);
1282 if (!NT_SUCCESS(Status))
1283 {
1284 /* Cleanup and return */
1285 MiDereferenceImports(LoadedImports);
1286 if (LoadedImports) ExFreePoolWithTag(LoadedImports, TAG_LDR_WSTR);
1287 return Status;
1288 }
1289
1290 /* Reset the buffer */
1291 *MissingApi = MissingApiBuffer;
1292 }
1293 }
1294
1295 /* Go to the next import */
1296 ImportDescriptor++;
1297 }
1298
1299 /* Check if we have an import list */
1300 if (LoadedImports)
1301 {
1302 /* Reset the count again, and loop entries */
1303 ImportCount = 0;
1304 for (i = 0; i < LoadedImports->Count; i++)
1305 {
1306 if (LoadedImports->Entry[i])
1307 {
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);
1311 ImportCount++;
1312 }
1313 }
1314
1315 /* Check if we had no imports */
1316 if (!ImportCount)
1317 {
1318 /* Free the list and set it to no imports */
1319 ExFreePoolWithTag(LoadedImports, TAG_LDR_WSTR);
1320 LoadedImports = MM_SYSLDR_NO_IMPORTS;
1321 }
1322 else if (ImportCount == 1)
1323 {
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;
1327 }
1328 else if (ImportCount != LoadedImports->Count)
1329 {
1330 /* Allocate a new list */
1331 LoadedImportsSize = ImportCount * sizeof(PVOID) + sizeof(SIZE_T);
1332 NewImports = ExAllocatePoolWithTag(PagedPool,
1333 LoadedImportsSize,
1334 'TDmM');
1335 if (NewImports)
1336 {
1337 /* Set count */
1338 NewImports->Count = 0;
1339
1340 /* Loop all the imports */
1341 for (i = 0; i < LoadedImports->Count; i++)
1342 {
1343 /* Make sure it's valid */
1344 if (LoadedImports->Entry[i])
1345 {
1346 /* Copy it */
1347 NewImports->Entry[NewImports->Count] = LoadedImports->Entry[i];
1348 NewImports->Count++;
1349 }
1350 }
1351
1352 /* Free the old copy */
1353 ExFreePoolWithTag(LoadedImports, TAG_LDR_WSTR);
1354 LoadedImports = NewImports;
1355 }
1356 }
1357
1358 /* Return the list */
1359 *LoadImports = LoadedImports;
1360 }
1361
1362 /* Return success */
1363 return STATUS_SUCCESS;
1364 }
1365
1366 VOID
1367 NTAPI
1368 INIT_FUNCTION
1369 MiReloadBootLoadedDrivers(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
1370 {
1371 PLIST_ENTRY NextEntry;
1372 ULONG i = 0;
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;
1379 NTSTATUS Status;
1380 PMMPTE PointerPte, StartPte, LastPte;
1381 PFN_NUMBER PteCount;
1382 PMMPFN Pfn1;
1383 MMPTE TempPte, OldPte;
1384
1385 /* Loop driver list */
1386 for (NextEntry = LoaderBlock->LoadOrderListHead.Flink;
1387 NextEntry != &LoaderBlock->LoadOrderListHead;
1388 NextEntry = NextEntry->Flink)
1389 {
1390 /* Get the loader entry and NT header */
1391 LdrEntry = CONTAINING_RECORD(NextEntry,
1392 LDR_DATA_TABLE_ENTRY,
1393 InLoadOrderLinks);
1394 NtHeader = RtlImageNtHeader(LdrEntry->DllBase);
1395
1396 /* Debug info */
1397 DPRINT("[Mm0]: Driver at: %p ending at: %p for module: %wZ\n",
1398 LdrEntry->DllBase,
1399 (ULONG_PTR)LdrEntry->DllBase + LdrEntry->SizeOfImage,
1400 &LdrEntry->FullDllName);
1401
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;
1406
1407 #if MI_TRACE_PFNS
1408 /* Loop the PTEs */
1409 while (PointerPte < LastPte)
1410 {
1411 ULONG len;
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);
1416 PointerPte++;
1417 }
1418 #endif
1419 /* Skip kernel and HAL */
1420 /* ROS HACK: Skip BOOTVID/KDCOM too */
1421 i++;
1422 if (i <= 4) continue;
1423
1424 /* Skip non-drivers */
1425 if (!NtHeader) continue;
1426
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;
1432
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)
1437 {
1438 /* We don't really have relocations */
1439 ValidRelocs = FALSE;
1440 }
1441 else
1442 {
1443 /* Make sure the size is valid */
1444 if ((DataDirectory->VirtualAddress + DataDirectory->Size) >
1445 LdrEntry->SizeOfImage)
1446 {
1447 /* They're not, skip */
1448 continue;
1449 }
1450
1451 /* We have relocations */
1452 ValidRelocs = TRUE;
1453 }
1454
1455 /* Remember the original address */
1456 DllBase = LdrEntry->DllBase;
1457
1458 /* Loop the PTEs */
1459 PointerPte = StartPte;
1460 while (PointerPte < LastPte)
1461 {
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;
1467
1468 /* Next */
1469 PointerPte++;
1470 }
1471
1472 /* Now reserve system PTEs for the image */
1473 PointerPte = MiReserveSystemPtes(PteCount, SystemPteSpace);
1474 if (!PointerPte)
1475 {
1476 /* Shouldn't happen */
1477 DPRINT1("[Mm0]: Couldn't allocate driver section!\n");
1478 while (TRUE);
1479 }
1480
1481 /* This is the new virtual address for the module */
1482 LastPte = PointerPte + PteCount;
1483 NewImageAddress = MiPteToAddress(PointerPte);
1484
1485 /* Sanity check */
1486 DPRINT("[Mm0]: Copying from: %p to: %p\n", DllBase, NewImageAddress);
1487 ASSERT(ExpInitializationPhase == 0);
1488
1489 /* Loop the new driver PTEs */
1490 TempPte = ValidKernelPte;
1491 while (PointerPte < LastPte)
1492 {
1493 /* Copy the old data */
1494 OldPte = *StartPte;
1495 ASSERT(OldPte.u.Hard.Valid == 1);
1496
1497 /* Set page number from the loader's memory */
1498 TempPte.u.Hard.PageFrameNumber = OldPte.u.Hard.PageFrameNumber;
1499
1500 /* Write it */
1501 MI_WRITE_VALID_PTE(PointerPte, TempPte);
1502
1503 /* Move on */
1504 PointerPte++;
1505 StartPte++;
1506 }
1507
1508 /* Update position */
1509 PointerPte -= PteCount;
1510
1511 /* Sanity check */
1512 ASSERT(*(PULONG)NewImageAddress == *(PULONG)DllBase);
1513
1514 /* Set the image base to the address where the loader put it */
1515 NtHeader->OptionalHeader.ImageBase = (ULONG_PTR)DllBase;
1516
1517 /* Check if we had relocations */
1518 if (ValidRelocs)
1519 {
1520 /* Relocate the image */
1521 Status = LdrRelocateImageWithBias(NewImageAddress,
1522 0,
1523 "SYSLDR",
1524 STATUS_SUCCESS,
1525 STATUS_CONFLICTING_ADDRESSES,
1526 STATUS_INVALID_IMAGE_FORMAT);
1527 if (!NT_SUCCESS(Status))
1528 {
1529 /* This shouldn't happen */
1530 DPRINT1("Relocations failed!\n");
1531 while (TRUE);
1532 }
1533 }
1534
1535 /* Update the loader entry */
1536 LdrEntry->DllBase = NewImageAddress;
1537
1538 /* Update the thunks */
1539 DPRINT("[Mm0]: Updating thunks to: %wZ\n", &LdrEntry->BaseDllName);
1540 MiUpdateThunks(LoaderBlock,
1541 DllBase,
1542 NewImageAddress,
1543 LdrEntry->SizeOfImage);
1544
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;
1550
1551 /* FIXME: We'll need to fixup the PFN linkage when switching to ARM3 */
1552 }
1553 }
1554
1555 NTSTATUS
1556 NTAPI
1557 INIT_FUNCTION
1558 MiBuildImportsForBootDrivers(VOID)
1559 {
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;
1571
1572 /* Initialize variables */
1573 KernelEntry = HalEntry = LastEntry = NULL;
1574
1575 /* Loop the loaded module list... we are early enough that no lock is needed */
1576 NextEntry = PsLoadedModuleList.Flink;
1577 while (NextEntry != &PsLoadedModuleList)
1578 {
1579 /* Get the entry */
1580 LdrEntry = CONTAINING_RECORD(NextEntry,
1581 LDR_DATA_TABLE_ENTRY,
1582 InLoadOrderLinks);
1583
1584 /* Check if it's the kernel or HAL */
1585 if (RtlEqualUnicodeString(&KernelName, &LdrEntry->BaseDllName, TRUE))
1586 {
1587 /* Found it */
1588 KernelEntry = LdrEntry;
1589 }
1590 else if (RtlEqualUnicodeString(&HalName, &LdrEntry->BaseDllName, TRUE))
1591 {
1592 /* Found it */
1593 HalEntry = LdrEntry;
1594 }
1595
1596 /* Check if this is a driver DLL */
1597 if (LdrEntry->Flags & LDRP_DRIVER_DEPENDENT_DLL)
1598 {
1599 /* Check if this is the HAL or kernel */
1600 if ((LdrEntry == HalEntry) || (LdrEntry == KernelEntry))
1601 {
1602 /* Add a reference */
1603 LdrEntry->LoadCount = 1;
1604 }
1605 else
1606 {
1607 /* No referencing needed */
1608 LdrEntry->LoadCount = 0;
1609 }
1610 }
1611 else
1612 {
1613 /* No referencing needed */
1614 LdrEntry->LoadCount = 0;
1615 }
1616
1617 /* Remember this came from the loader */
1618 LdrEntry->LoadedImports = MM_SYSLDR_BOOT_LOADED;
1619
1620 /* Keep looping */
1621 NextEntry = NextEntry->Flink;
1622 Modules++;
1623 }
1624
1625 /* We must have at least found the kernel and HAL */
1626 if (!(HalEntry) || (!KernelEntry)) return STATUS_NOT_FOUND;
1627
1628 /* Allocate the list */
1629 EntryArray = ExAllocatePoolWithTag(PagedPool, Modules * sizeof(PVOID), 'TDmM');
1630 if (!EntryArray) return STATUS_INSUFFICIENT_RESOURCES;
1631
1632 /* Loop the loaded module list again */
1633 NextEntry = PsLoadedModuleList.Flink;
1634 while (NextEntry != &PsLoadedModuleList)
1635 {
1636 /* Get the entry */
1637 LdrEntry = CONTAINING_RECORD(NextEntry,
1638 LDR_DATA_TABLE_ENTRY,
1639 InLoadOrderLinks);
1640 #ifdef _WORKING_LOADER_
1641 /* Get its imports */
1642 ImageThunk = RtlImageDirectoryEntryToData(LdrEntry->DllBase,
1643 TRUE,
1644 IMAGE_DIRECTORY_ENTRY_IAT,
1645 &ImportSize);
1646 if (!ImageThunk)
1647 #else
1648 /* Get its imports */
1649 ImportDescriptor = RtlImageDirectoryEntryToData(LdrEntry->DllBase,
1650 TRUE,
1651 IMAGE_DIRECTORY_ENTRY_IMPORT,
1652 &ImportSize);
1653 if (!ImportDescriptor)
1654 #endif
1655 {
1656 /* None present */
1657 LdrEntry->LoadedImports = MM_SYSLDR_NO_IMPORTS;
1658 NextEntry = NextEntry->Flink;
1659 continue;
1660 }
1661
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);
1666
1667 /* Scan the thunks */
1668 for (i = 0, DllBase = 0, DllEnd = 0; i < ImportSize; i++, ImageThunk++)
1669 #else
1670 i = DllBase = DllEnd = 0;
1671 while ((ImportDescriptor->Name) &&
1672 (ImportDescriptor->OriginalFirstThunk))
1673 {
1674 /* Get the image thunk */
1675 ImageThunk = (PVOID)((ULONG_PTR)LdrEntry->DllBase +
1676 ImportDescriptor->FirstThunk);
1677 while (*ImageThunk)
1678 #endif
1679 {
1680 /* Do we already have an address? */
1681 if (DllBase)
1682 {
1683 /* Is the thunk in the same address? */
1684 if ((*ImageThunk >= DllBase) && (*ImageThunk < DllEnd))
1685 {
1686 /* Skip it, we already have a reference for it */
1687 ASSERT(EntryArray[j]);
1688 ImageThunk++;
1689 continue;
1690 }
1691 }
1692
1693 /* Loop the loaded module list to locate this address owner */
1694 j = 0;
1695 NextEntry2 = PsLoadedModuleList.Flink;
1696 while (NextEntry2 != &PsLoadedModuleList)
1697 {
1698 /* Get the entry */
1699 LdrEntry2 = CONTAINING_RECORD(NextEntry2,
1700 LDR_DATA_TABLE_ENTRY,
1701 InLoadOrderLinks);
1702
1703 /* Get the address range for this module */
1704 DllBase = (ULONG_PTR)LdrEntry2->DllBase;
1705 DllEnd = DllBase + LdrEntry2->SizeOfImage;
1706
1707 /* Check if this IAT entry matches it */
1708 if ((*ImageThunk >= DllBase) && (*ImageThunk < DllEnd))
1709 {
1710 /* Save it */
1711 //DPRINT1("Found imported dll: %wZ\n", &LdrEntry2->BaseDllName);
1712 EntryArray[j] = LdrEntry2;
1713 break;
1714 }
1715
1716 /* Keep searching */
1717 NextEntry2 = NextEntry2->Flink;
1718 j++;
1719 }
1720
1721 /* Do we have a thunk outside the range? */
1722 if ((*ImageThunk < DllBase) || (*ImageThunk >= DllEnd))
1723 {
1724 /* Could be 0... */
1725 if (*ImageThunk)
1726 {
1727 /* Should not be happening */
1728 DPRINT1("Broken IAT entry for %p at %p (%lx)\n",
1729 LdrEntry, ImageThunk, *ImageThunk);
1730 ASSERT(FALSE);
1731 }
1732
1733 /* Reset if we hit this */
1734 DllBase = 0;
1735 }
1736 #ifndef _WORKING_LOADER_
1737 ImageThunk++;
1738 }
1739
1740 i++;
1741 ImportDescriptor++;
1742 #endif
1743 }
1744
1745 /* Now scan how many imports we really have */
1746 for (i = 0, ImportSize = 0; i < Modules; i++)
1747 {
1748 /* Skip HAL and kernel */
1749 if ((EntryArray[i]) &&
1750 (EntryArray[i] != HalEntry) &&
1751 (EntryArray[i] != KernelEntry))
1752 {
1753 /* A valid reference */
1754 LastEntry = EntryArray[i];
1755 ImportSize++;
1756 }
1757 }
1758
1759 /* Do we have any imports after all? */
1760 if (!ImportSize)
1761 {
1762 /* No */
1763 LdrEntry->LoadedImports = MM_SYSLDR_NO_IMPORTS;
1764 }
1765 else if (ImportSize == 1)
1766 {
1767 /* A single entry import */
1768 LdrEntry->LoadedImports = (PVOID)((ULONG_PTR)LastEntry | MM_SYSLDR_SINGLE_ENTRY);
1769 LastEntry->LoadCount++;
1770 }
1771 else
1772 {
1773 /* We need an import table */
1774 LoadedImportsSize = ImportSize * sizeof(PVOID) + sizeof(SIZE_T);
1775 LoadedImports = ExAllocatePoolWithTag(PagedPool,
1776 LoadedImportsSize,
1777 'TDmM');
1778 ASSERT(LoadedImports);
1779
1780 /* Save the count */
1781 LoadedImports->Count = ImportSize;
1782
1783 /* Now copy all imports */
1784 for (i = 0, j = 0; i < Modules; i++)
1785 {
1786 /* Skip HAL and kernel */
1787 if ((EntryArray[i]) &&
1788 (EntryArray[i] != HalEntry) &&
1789 (EntryArray[i] != KernelEntry))
1790 {
1791 /* A valid reference */
1792 //DPRINT1("Found valid entry: %p\n", EntryArray[i]);
1793 LoadedImports->Entry[j] = EntryArray[i];
1794 EntryArray[i]->LoadCount++;
1795 j++;
1796 }
1797 }
1798
1799 /* Should had as many entries as we expected */
1800 ASSERT(j == ImportSize);
1801 LdrEntry->LoadedImports = LoadedImports;
1802 }
1803
1804 /* Next */
1805 NextEntry = NextEntry->Flink;
1806 }
1807
1808 /* Free the initial array */
1809 ExFreePool(EntryArray);
1810
1811 /* FIXME: Might not need to keep the HAL/Kernel imports around */
1812
1813 /* Kernel and HAL are loaded at boot */
1814 KernelEntry->LoadedImports = MM_SYSLDR_BOOT_LOADED;
1815 HalEntry->LoadedImports = MM_SYSLDR_BOOT_LOADED;
1816
1817 /* All worked well */
1818 return STATUS_SUCCESS;
1819 }
1820
1821 VOID
1822 NTAPI
1823 INIT_FUNCTION
1824 MiLocateKernelSections(IN PLDR_DATA_TABLE_ENTRY LdrEntry)
1825 {
1826 ULONG_PTR DllBase;
1827 PIMAGE_NT_HEADERS NtHeaders;
1828 PIMAGE_SECTION_HEADER SectionHeader;
1829 ULONG Sections, Size;
1830
1831 /* Get the kernel section header */
1832 DllBase = (ULONG_PTR)LdrEntry->DllBase;
1833 NtHeaders = RtlImageNtHeader((PVOID)DllBase);
1834 SectionHeader = IMAGE_FIRST_SECTION(NtHeaders);
1835
1836 /* Loop all the sections */
1837 Sections = NtHeaders->FileHeader.NumberOfSections;
1838 while (Sections)
1839 {
1840 /* Grab the size of the section */
1841 Size = max(SectionHeader->SizeOfRawData, SectionHeader->Misc.VirtualSize);
1842
1843 /* Check for .RSRC section */
1844 if (*(PULONG)SectionHeader->Name == 'rsr.')
1845 {
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);
1851 }
1852 else if (*(PULONG)SectionHeader->Name == 'LOOP')
1853 {
1854 /* POOLCODE vs. POOLMI */
1855 if (*(PULONG)&SectionHeader->Name[4] == 'EDOC')
1856 {
1857 /* Found Ex* Pool code */
1858 ExPoolCodeStart = DllBase + SectionHeader->VirtualAddress;
1859 ExPoolCodeEnd = ExPoolCodeStart + Size;
1860 }
1861 else if (*(PUSHORT)&SectionHeader->Name[4] == 'MI')
1862 {
1863 /* Found Mm* Pool code */
1864 MmPoolCodeStart = DllBase + SectionHeader->VirtualAddress;
1865 MmPoolCodeEnd = ExPoolCodeStart + Size;
1866 }
1867 }
1868 else if ((*(PULONG)SectionHeader->Name == 'YSIM') &&
1869 (*(PULONG)&SectionHeader->Name[4] == 'ETPS'))
1870 {
1871 /* Found MISYSPTE (Mm System PTE code)*/
1872 MmPteCodeStart = DllBase + SectionHeader->VirtualAddress;
1873 MmPteCodeEnd = ExPoolCodeStart + Size;
1874 }
1875
1876 /* Keep going */
1877 Sections--;
1878 SectionHeader++;
1879 }
1880 }
1881
1882 BOOLEAN
1883 NTAPI
1884 INIT_FUNCTION
1885 MiInitializeLoadedModuleList(IN PLOADER_PARAMETER_BLOCK LoaderBlock)
1886 {
1887 PLDR_DATA_TABLE_ENTRY LdrEntry, NewEntry;
1888 PLIST_ENTRY ListHead, NextEntry;
1889 ULONG EntrySize;
1890
1891 /* Setup the loaded module list and locks */
1892 ExInitializeResourceLite(&PsLoadedModuleResource);
1893 KeInitializeSpinLock(&PsLoadedModuleSpinLock);
1894 InitializeListHead(&PsLoadedModuleList);
1895
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,
1901 InLoadOrderLinks);
1902 PsNtosImageBase = (ULONG_PTR)LdrEntry->DllBase;
1903
1904 /* Locate resource section, pool code, and system pte code */
1905 MiLocateKernelSections(LdrEntry);
1906
1907 /* Loop the loader block */
1908 while (NextEntry != ListHead)
1909 {
1910 /* Get the loader entry */
1911 LdrEntry = CONTAINING_RECORD(NextEntry,
1912 LDR_DATA_TABLE_ENTRY,
1913 InLoadOrderLinks);
1914
1915 /* FIXME: ROS HACK. Make sure this is a driver */
1916 if (!RtlImageNtHeader(LdrEntry->DllBase))
1917 {
1918 /* Skip this entry */
1919 NextEntry = NextEntry->Flink;
1920 continue;
1921 }
1922
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;
1929
1930 /* Copy the entry over */
1931 *NewEntry = *LdrEntry;
1932
1933 /* Allocate the name */
1934 NewEntry->FullDllName.Buffer =
1935 ExAllocatePoolWithTag(PagedPool,
1936 LdrEntry->FullDllName.MaximumLength +
1937 sizeof(UNICODE_NULL),
1938 TAG_LDR_WSTR);
1939 if (!NewEntry->FullDllName.Buffer) return FALSE;
1940
1941 /* Set the base name */
1942 NewEntry->BaseDllName.Buffer = (PVOID)(NewEntry + 1);
1943
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);
1951
1952 /* Null-terminate the base name */
1953 NewEntry->BaseDllName.Buffer[NewEntry->BaseDllName.Length /
1954 sizeof(WCHAR)] = UNICODE_NULL;
1955
1956 /* Insert the entry into the list */
1957 InsertTailList(&PsLoadedModuleList, &NewEntry->InLoadOrderLinks);
1958 NextEntry = NextEntry->Flink;
1959 }
1960
1961 /* Build the import lists for the boot drivers */
1962 MiBuildImportsForBootDrivers();
1963
1964 /* We're done */
1965 return TRUE;
1966 }
1967
1968 LOGICAL
1969 NTAPI
1970 MiUseLargeDriverPage(IN ULONG NumberOfPtes,
1971 IN OUT PVOID *ImageBaseAddress,
1972 IN PUNICODE_STRING BaseImageName,
1973 IN BOOLEAN BootDriver)
1974 {
1975 PLIST_ENTRY NextEntry;
1976 BOOLEAN DriverFound = FALSE;
1977 PMI_LARGE_PAGE_DRIVER_ENTRY LargePageDriverEntry;
1978 ASSERT(KeGetCurrentIrql () <= APC_LEVEL);
1979 ASSERT(*ImageBaseAddress >= MmSystemRangeStart);
1980
1981 #ifdef _X86_
1982 if (!(KeFeatureBits & KF_LARGE_PAGE)) return FALSE;
1983 if (!(__readcr4() & CR4_PSE)) return FALSE;
1984 #endif
1985
1986 /* Make sure there's enough system PTEs for a large page driver */
1987 if (MmTotalFreeSystemPtes[SystemPteSpace] < (16 * (PDE_MAPPED_VA >> PAGE_SHIFT)))
1988 {
1989 return FALSE;
1990 }
1991
1992 /* This happens if the registry key had a "*" (wildcard) in it */
1993 if (MiLargePageAllDrivers == 0)
1994 {
1995 /* It didn't, so scan the list */
1996 NextEntry = MiLargePageDriverList.Flink;
1997 while (NextEntry != &MiLargePageDriverList)
1998 {
1999 /* Check if the driver name matches */
2000 LargePageDriverEntry = CONTAINING_RECORD(NextEntry,
2001 MI_LARGE_PAGE_DRIVER_ENTRY,
2002 Links);
2003 if (RtlEqualUnicodeString(BaseImageName,
2004 &LargePageDriverEntry->BaseName,
2005 TRUE))
2006 {
2007 /* Enable large pages for this driver */
2008 DriverFound = TRUE;
2009 break;
2010 }
2011
2012 /* Keep trying */
2013 NextEntry = NextEntry->Flink;
2014 }
2015
2016 /* If we didn't find the driver, it doesn't need large pages */
2017 if (DriverFound == FALSE) return FALSE;
2018 }
2019
2020 /* Nothing to do yet */
2021 DPRINT1("Large pages not supported!\n");
2022 return FALSE;
2023 }
2024
2025 ULONG
2026 NTAPI
2027 MiComputeDriverProtection(IN BOOLEAN SessionSpace,
2028 IN ULONG SectionProtection)
2029 {
2030 ULONG Protection = MM_ZERO_ACCESS;
2031
2032 /* Check if the caller gave anything */
2033 if (SectionProtection)
2034 {
2035 /* Always turn on execute access */
2036 SectionProtection |= IMAGE_SCN_MEM_EXECUTE;
2037
2038 /* Check if the registry setting is on or not */
2039 if (!MmEnforceWriteProtection)
2040 {
2041 /* Turn on write access too */
2042 SectionProtection |= (IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_EXECUTE);
2043 }
2044 }
2045
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;
2049
2050 /* Check for write access */
2051 if (SectionProtection & IMAGE_SCN_MEM_WRITE)
2052 {
2053 /* Session space is not supported */
2054 if (SessionSpace)
2055 {
2056 DPRINT1("Session drivers not supported\n");
2057 ASSERT(SessionSpace == FALSE);
2058 }
2059 else
2060 {
2061 /* Convert to internal PTE flag */
2062 Protection = (Protection & MM_EXECUTE) ? MM_EXECUTE_READWRITE : MM_READWRITE;
2063 }
2064 }
2065
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;
2068
2069 /* Return the computed PTE protection */
2070 return Protection;
2071 }
2072
2073 VOID
2074 NTAPI
2075 MiSetSystemCodeProtection(IN PMMPTE FirstPte,
2076 IN PMMPTE LastPte,
2077 IN ULONG ProtectionMask)
2078 {
2079 /* I'm afraid to introduce regressions at the moment... */
2080 return;
2081 }
2082
2083 VOID
2084 NTAPI
2085 MiWriteProtectSystemImage(IN PVOID ImageBase)
2086 {
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;
2095 PAGED_CODE();
2096
2097 /* No need to write protect physical memory-backed drivers (large pages) */
2098 if (MI_IS_PHYSICAL_ADDRESS(ImageBase)) return;
2099
2100 /* Get the image headers */
2101 NtHeaders = RtlImageNtHeader(ImageBase);
2102 if (!NtHeaders) return;
2103
2104 /* Check if this is a session driver or not */
2105 if (!MI_IS_SESSION_ADDRESS(ImageBase))
2106 {
2107 /* Don't touch NT4 drivers */
2108 if (NtHeaders->OptionalHeader.MajorOperatingSystemVersion < 5) return;
2109 if (NtHeaders->OptionalHeader.MajorImageVersion < 5) return;
2110 }
2111 else
2112 {
2113 /* Not supported */
2114 DPRINT1("Session drivers not supported\n");
2115 ASSERT(FALSE);
2116 }
2117
2118 /* These are the only protection masks we care about */
2119 ProtectionMask = IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
2120
2121 /* Calculate the number of pages this driver is occupying */
2122 DriverPages = BYTES_TO_PAGES(NtHeaders->OptionalHeader.SizeOfImage);
2123
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);
2128
2129 /* Loop all the sections */
2130 CurrentAddress = (ULONG_PTR)ImageBase;
2131 while (Sections)
2132 {
2133 /* Get the section size */
2134 Size = max(Section->SizeOfRawData, Section->Misc.VirtualSize);
2135
2136 /* Get its virtual address */
2137 BaseAddress = (ULONG_PTR)ImageBase + Section->VirtualAddress;
2138 if (BaseAddress < CurrentAddress)
2139 {
2140 /* Windows doesn't like these */
2141 DPRINT1("Badly linked image!\n");
2142 return;
2143 }
2144
2145 /* Remember the current address */
2146 CurrentAddress = BaseAddress + Size - 1;
2147
2148 /* Next */
2149 Sections--;
2150 Section++;
2151 }
2152
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);
2157
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;
2161
2162 /* Set the PTE points for the image, and loop its sections */
2163 StartPte = MiAddressToPte(ImageBase);
2164 LastPte = StartPte + DriverPages;
2165 while (Sections)
2166 {
2167 /* Get the section size */
2168 Size = max(Section->SizeOfRawData, Section->Misc.VirtualSize);
2169
2170 /* Get its virtual address and PTE */
2171 BaseAddress = (ULONG_PTR)ImageBase + Section->VirtualAddress;
2172 PointerPte = MiAddressToPte(BaseAddress);
2173
2174 /* Check if we were already protecting a run, and found a new run */
2175 if ((ComboPte) && (PointerPte > ComboPte))
2176 {
2177 /* Compute protection */
2178 CombinedMask = MiComputeDriverProtection(FALSE, CombinedProtection);
2179
2180 /* Set it */
2181 MiSetSystemCodeProtection(ComboPte, ComboPte, CombinedMask);
2182
2183 /* Check for overlap */
2184 if (ComboPte == StartPte) StartPte++;
2185
2186 /* One done, reset variables */
2187 ComboPte = NULL;
2188 CombinedProtection = 0;
2189 }
2190
2191 /* Break out when needed */
2192 if (PointerPte >= LastPte) break;
2193
2194 /* Get the requested protection from the image header */
2195 SectionProtection = Section->Characteristics & ProtectionMask;
2196 if (SectionProtection == CurrentProtection)
2197 {
2198 /* Same protection, so merge the request */
2199 CurrentAddress = BaseAddress + Size - 1;
2200
2201 /* Next */
2202 Sections--;
2203 Section++;
2204 continue;
2205 }
2206
2207 /* This is now a new section, so close up the old one */
2208 CurrentPte = MiAddressToPte(CurrentAddress);
2209
2210 /* Check for overlap */
2211 if (CurrentPte == PointerPte)
2212 {
2213 /* Skip the last PTE, since it overlaps with us */
2214 CurrentPte--;
2215
2216 /* And set the PTE we will merge with */
2217 ASSERT((ComboPte == NULL) || (ComboPte == PointerPte));
2218 ComboPte = PointerPte;
2219
2220 /* Get the most flexible protection by merging both */
2221 CombinedMask |= (SectionProtection | CurrentProtection);
2222 }
2223
2224 /* Loop any PTEs left */
2225 if (CurrentPte >= StartPte)
2226 {
2227 /* Sanity check */
2228 ASSERT(StartPte < LastPte);
2229
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);
2233
2234 /* Compute the protection and set it */
2235 CurrentMask = MiComputeDriverProtection(FALSE, CurrentProtection);
2236 MiSetSystemCodeProtection(StartPte, CurrentPte, CurrentMask);
2237 }
2238
2239 /* Set new state */
2240 StartPte = PointerPte;
2241 CurrentAddress = BaseAddress + Size - 1;
2242 CurrentProtection = SectionProtection;
2243
2244 /* Next */
2245 Sections--;
2246 Section++;
2247 }
2248
2249 /* Is there a leftover section to merge? */
2250 if (ComboPte)
2251 {
2252 /* Compute and set the protection */
2253 CombinedMask = MiComputeDriverProtection(FALSE, CombinedProtection);
2254 MiSetSystemCodeProtection(ComboPte, ComboPte, CombinedMask);
2255
2256 /* Handle overlap */
2257 if (ComboPte == StartPte) StartPte++;
2258 }
2259
2260 /* Finally, handle the last section */
2261 CurrentPte = MiAddressToPte(CurrentAddress);
2262 if ((StartPte < LastPte) && (CurrentPte >= StartPte))
2263 {
2264 /* Handle overlap */
2265 if (CurrentPte >= LastPte) CurrentPte = LastPte - 1;
2266 ASSERT(CurrentPte >= StartPte);
2267
2268 /* Compute and set the protection */
2269 CurrentMask = MiComputeDriverProtection(FALSE, CurrentProtection);
2270 MiSetSystemCodeProtection(StartPte, CurrentPte, CurrentMask);
2271 }
2272 }
2273
2274 VOID
2275 NTAPI
2276 MiSetPagingOfDriver(IN PMMPTE PointerPte,
2277 IN PMMPTE LastPte)
2278 {
2279 PVOID ImageBase;
2280 PETHREAD CurrentThread = PsGetCurrentThread();
2281 PFN_NUMBER PageCount = 0, PageFrameIndex;
2282 PMMPFN Pfn1;
2283 PAGED_CODE();
2284
2285 /* Get the driver's base address */
2286 ImageBase = MiPteToAddress(PointerPte);
2287 ASSERT(MI_IS_SESSION_IMAGE_ADDRESS(ImageBase) == FALSE);
2288
2289 /* If this is a large page, it's stuck in physical memory */
2290 if (MI_IS_PHYSICAL_ADDRESS(ImageBase)) return;
2291
2292 /* Lock the working set */
2293 MiLockWorkingSet(CurrentThread, &MmSystemCacheWs);
2294
2295 /* Loop the PTEs */
2296 while (PointerPte <= LastPte)
2297 {
2298 /* Check for valid PTE */
2299 if (PointerPte->u.Hard.Valid == 1)
2300 {
2301 PageFrameIndex = PFN_FROM_PTE(PointerPte);
2302 Pfn1 = MiGetPfnEntry(PageFrameIndex);
2303 ASSERT(Pfn1->u2.ShareCount == 1);
2304
2305 /* No working sets in ReactOS yet */
2306 PageCount++;
2307 }
2308
2309 ImageBase = (PVOID)((ULONG_PTR)ImageBase + PAGE_SIZE);
2310 PointerPte++;
2311 }
2312
2313 /* Release the working set */
2314 MiUnlockWorkingSet(CurrentThread, &MmSystemCacheWs);
2315
2316 /* Do we have any driver pages? */
2317 if (PageCount)
2318 {
2319 /* Update counters */
2320 InterlockedExchangeAdd((PLONG)&MmTotalSystemDriverPages, PageCount);
2321 }
2322 }
2323
2324 VOID
2325 NTAPI
2326 MiEnablePagingOfDriver(IN PLDR_DATA_TABLE_ENTRY LdrEntry)
2327 {
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;
2334
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;
2339
2340 /* Get the sections and their alignment */
2341 Sections = NtHeaders->FileHeader.NumberOfSections;
2342 Alignment = NtHeaders->OptionalHeader.SectionAlignment - 1;
2343
2344 /* Loop each section */
2345 Section = IMAGE_FIRST_SECTION(NtHeaders);
2346 while (Sections)
2347 {
2348 /* Find PAGE or .edata */
2349 if ((*(PULONG)Section->Name == 'EGAP') ||
2350 (*(PULONG)Section->Name == 'ade.'))
2351 {
2352 /* Had we already done some work? */
2353 if (!PointerPte)
2354 {
2355 /* Nope, setup the first PTE address */
2356 PointerPte = MiAddressToPte(ROUND_TO_PAGES(ImageBase +
2357 Section->
2358 VirtualAddress));
2359 }
2360
2361 /* Compute the size */
2362 Size = max(Section->SizeOfRawData, Section->Misc.VirtualSize);
2363
2364 /* Find the last PTE that maps this section */
2365 LastPte = MiAddressToPte(ImageBase +
2366 Section->VirtualAddress +
2367 Alignment +
2368 Size -
2369 PAGE_SIZE);
2370 }
2371 else
2372 {
2373 /* Had we found a section before? */
2374 if (PointerPte)
2375 {
2376 /* Mark it as pageable */
2377 MiSetPagingOfDriver(PointerPte, LastPte);
2378 PointerPte = NULL;
2379 }
2380 }
2381
2382 /* Keep searching */
2383 Sections--;
2384 Section++;
2385 }
2386
2387 /* Handle the straggler */
2388 if (PointerPte) MiSetPagingOfDriver(PointerPte, LastPte);
2389 }
2390
2391 BOOLEAN
2392 NTAPI
2393 MmVerifyImageIsOkForMpUse(IN PVOID BaseAddress)
2394 {
2395 PIMAGE_NT_HEADERS NtHeader;
2396 PAGED_CODE();
2397
2398 /* Get NT Headers */
2399 NtHeader = RtlImageNtHeader(BaseAddress);
2400 if (NtHeader)
2401 {
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))
2405 {
2406 /* Fail */
2407 return FALSE;
2408 }
2409 }
2410
2411 /* Otherwise, it's safe */
2412 return TRUE;
2413 }
2414
2415 NTSTATUS
2416 NTAPI
2417 MmCheckSystemImage(IN HANDLE ImageHandle,
2418 IN BOOLEAN PurgeSection)
2419 {
2420 NTSTATUS Status;
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;
2429 PAGED_CODE();
2430
2431 /* Setup the object attributes */
2432 InitializeObjectAttributes(&ObjectAttributes,
2433 NULL,
2434 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
2435 NULL,
2436 NULL);
2437
2438 /* Create a section for the DLL */
2439 Status = ZwCreateSection(&SectionHandle,
2440 SECTION_MAP_EXECUTE,
2441 &ObjectAttributes,
2442 NULL,
2443 PAGE_EXECUTE,
2444 SEC_IMAGE,
2445 ImageHandle);
2446 if (!NT_SUCCESS(Status)) return Status;
2447
2448 /* Make sure we're in the system process */
2449 KeStackAttachProcess(&PsInitialSystemProcess->Pcb, &ApcState);
2450
2451 /* Map it */
2452 Status = ZwMapViewOfSection(SectionHandle,
2453 NtCurrentProcess(),
2454 &ViewBase,
2455 0,
2456 0,
2457 NULL,
2458 &ViewSize,
2459 ViewShare,
2460 0,
2461 PAGE_EXECUTE);
2462 if (!NT_SUCCESS(Status))
2463 {
2464 /* We failed, close the handle and return */
2465 KeUnstackDetachProcess(&ApcState);
2466 ZwClose(SectionHandle);
2467 return Status;
2468 }
2469
2470 /* Now query image information */
2471 Status = ZwQueryInformationFile(ImageHandle,
2472 &IoStatusBlock,
2473 &FileStandardInfo,
2474 sizeof(FileStandardInfo),
2475 FileStandardInformation);
2476 if (NT_SUCCESS(Status))
2477 {
2478 /* First, verify the checksum */
2479 if (!LdrVerifyMappedImageMatchesChecksum(ViewBase,
2480 ViewSize,
2481 FileStandardInfo.
2482 EndOfFile.LowPart))
2483 {
2484 /* Set checksum failure */
2485 Status = STATUS_IMAGE_CHECKSUM_MISMATCH;
2486 goto Fail;
2487 }
2488
2489 /* Make sure it's a real image */
2490 NtHeaders = RtlImageNtHeader(ViewBase);
2491 if (!NtHeaders)
2492 {
2493 /* Set checksum failure */
2494 Status = STATUS_IMAGE_CHECKSUM_MISMATCH;
2495 goto Fail;
2496 }
2497
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))
2501 {
2502 /* Set protection failure */
2503 Status = STATUS_INVALID_IMAGE_PROTECT;
2504 goto Fail;
2505 }
2506
2507 /* Check that it's a valid SMP image if we have more then one CPU */
2508 if (!MmVerifyImageIsOkForMpUse(ViewBase))
2509 {
2510 /* Otherwise it's not the right image */
2511 Status = STATUS_IMAGE_MP_UP_MISMATCH;
2512 }
2513 }
2514
2515 /* Unmap the section, close the handle, and return status */
2516 Fail:
2517 ZwUnmapViewOfSection(NtCurrentProcess(), ViewBase);
2518 KeUnstackDetachProcess(&ApcState);
2519 ZwClose(SectionHandle);
2520 return Status;
2521 }
2522
2523 NTSTATUS
2524 NTAPI
2525 MmLoadSystemImage(IN PUNICODE_STRING FileName,
2526 IN PUNICODE_STRING NamePrefix OPTIONAL,
2527 IN PUNICODE_STRING LoadedName OPTIONAL,
2528 IN ULONG Flags,
2529 OUT PVOID *ModuleObject,
2530 OUT PVOID *ImageBaseAddress)
2531 {
2532 PVOID ModuleLoadBase = NULL;
2533 NTSTATUS Status;
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;
2550 STRING AnsiTemp;
2551 PAGED_CODE();
2552
2553 /* Detect session-load */
2554 if (Flags)
2555 {
2556 /* Sanity checks */
2557 ASSERT(NamePrefix == NULL);
2558 ASSERT(LoadedName == NULL);
2559
2560 /* Make sure the process is in session too */
2561 if (!PsGetCurrentProcess()->ProcessInSession) return STATUS_NO_MEMORY;
2562 }
2563
2564 /* Allocate a buffer we'll use for names */
2565 Buffer = ExAllocatePoolWithTag(NonPagedPool, MAX_PATH, 'nLmM');
2566 if (!Buffer) return STATUS_INSUFFICIENT_RESOURCES;
2567
2568 /* Check for a separator */
2569 if (FileName->Buffer[0] == OBJ_NAME_PATH_SEPARATOR)
2570 {
2571 PWCHAR p;
2572 ULONG BaseLength;
2573
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--;
2577
2578 /* Get the length */
2579 BaseLength = (ULONG)(&FileName->Buffer[FileName->Length / sizeof(WCHAR)] - p);
2580 BaseLength *= sizeof(WCHAR);
2581
2582 /* Setup the string */
2583 BaseName.Length = (USHORT)BaseLength;
2584 BaseName.Buffer = p;
2585 }
2586 else
2587 {
2588 /* Otherwise, we already have a base name */
2589 BaseName.Length = FileName->Length;
2590 BaseName.Buffer = FileName->Buffer;
2591 }
2592
2593 /* Setup the maximum length */
2594 BaseName.MaximumLength = BaseName.Length;
2595
2596 /* Now compute the base directory */
2597 BaseDirectory = *FileName;
2598 BaseDirectory.Length -= BaseName.Length;
2599 BaseDirectory.MaximumLength = BaseDirectory.Length;
2600
2601 /* And the prefix, which for now is just the name itself */
2602 PrefixName = *FileName;
2603
2604 /* Check if we have a prefix */
2605 if (NamePrefix) DPRINT1("Prefixed images are not yet supported!\n");
2606
2607 /* Check if we already have a name, use it instead */
2608 if (LoadedName) BaseName = *LoadedName;
2609
2610 /* Check for loader snap debugging */
2611 if (NtGlobalFlag & FLG_SHOW_LDR_SNAPS)
2612 {
2613 /* Print out standard string */
2614 DPRINT1("MM:SYSLDR Loading %wZ (%wZ) %s\n",
2615 &PrefixName, &BaseName, Flags ? "in session space" : "");
2616 }
2617
2618 /* Acquire the load lock */
2619 LoaderScan:
2620 ASSERT(LockOwned == FALSE);
2621 LockOwned = TRUE;
2622 KeEnterCriticalRegion();
2623 KeWaitForSingleObject(&MmSystemLoadLock,
2624 WrVirtualMemory,
2625 KernelMode,
2626 FALSE,
2627 NULL);
2628
2629 /* Scan the module list */
2630 NextEntry = PsLoadedModuleList.Flink;
2631 while (NextEntry != &PsLoadedModuleList)
2632 {
2633 /* Get the entry and compare the names */
2634 LdrEntry = CONTAINING_RECORD(NextEntry,
2635 LDR_DATA_TABLE_ENTRY,
2636 InLoadOrderLinks);
2637 if (RtlEqualUnicodeString(&PrefixName, &LdrEntry->FullDllName, TRUE))
2638 {
2639 /* Found it, break out */
2640 break;
2641 }
2642
2643 /* Keep scanning */
2644 NextEntry = NextEntry->Flink;
2645 }
2646
2647 /* Check if we found the image */
2648 if (NextEntry != &PsLoadedModuleList)
2649 {
2650 /* Check if we had already mapped a section */
2651 if (Section)
2652 {
2653 /* Dereference and clear */
2654 ObDereferenceObject(Section);
2655 Section = NULL;
2656 }
2657
2658 /* Check if this was supposed to be a session load */
2659 if (!Flags)
2660 {
2661 /* It wasn't, so just return the data */
2662 *ModuleObject = LdrEntry;
2663 *ImageBaseAddress = LdrEntry->DllBase;
2664 Status = STATUS_IMAGE_ALREADY_LOADED;
2665 }
2666 else
2667 {
2668 /* We don't support session loading yet */
2669 DPRINT1("Unsupported Session-Load!\n");
2670 while (TRUE);
2671 }
2672
2673 /* Do cleanup */
2674 goto Quickie;
2675 }
2676 else if (!Section)
2677 {
2678 /* It wasn't loaded, and we didn't have a previous attempt */
2679 KeReleaseMutant(&MmSystemLoadLock, 1, FALSE, FALSE);
2680 KeLeaveCriticalRegion();
2681 LockOwned = FALSE;
2682
2683 /* Check if KD is enabled */
2684 if ((KdDebuggerEnabled) && !(KdDebuggerNotPresent))
2685 {
2686 /* FIXME: Attempt to get image from KD */
2687 }
2688
2689 /* We don't have a valid entry */
2690 LdrEntry = NULL;
2691
2692 /* Setup image attributes */
2693 InitializeObjectAttributes(&ObjectAttributes,
2694 FileName,
2695 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
2696 NULL,
2697 NULL);
2698
2699 /* Open the image */
2700 Status = ZwOpenFile(&FileHandle,
2701 FILE_EXECUTE,
2702 &ObjectAttributes,
2703 &IoStatusBlock,
2704 FILE_SHARE_READ | FILE_SHARE_DELETE,
2705 0);
2706 if (!NT_SUCCESS(Status)) goto Quickie;
2707
2708 /* Validate it */
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))
2713 {
2714 /* Fail loading */
2715 goto Quickie;
2716 }
2717
2718 /* Check if this is a session-load */
2719 if (Flags)
2720 {
2721 /* Then we only need read and execute */
2722 DesiredAccess = SECTION_MAP_READ | SECTION_MAP_EXECUTE;
2723 }
2724 else
2725 {
2726 /* Otherwise, we can allow write access */
2727 DesiredAccess = SECTION_ALL_ACCESS;
2728 }
2729
2730 /* Initialize the attributes for the section */
2731 InitializeObjectAttributes(&ObjectAttributes,
2732 NULL,
2733 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
2734 NULL,
2735 NULL);
2736
2737 /* Create the section */
2738 Status = ZwCreateSection(&SectionHandle,
2739 DesiredAccess,
2740 &ObjectAttributes,
2741 NULL,
2742 PAGE_EXECUTE,
2743 SEC_IMAGE,
2744 FileHandle);
2745 if (!NT_SUCCESS(Status)) goto Quickie;
2746
2747 /* Now get the section pointer */
2748 Status = ObReferenceObjectByHandle(SectionHandle,
2749 SECTION_MAP_EXECUTE,
2750 MmSectionObjectType,
2751 KernelMode,
2752 &Section,
2753 NULL);
2754 ZwClose(SectionHandle);
2755 if (!NT_SUCCESS(Status)) goto Quickie;
2756
2757 /* Check if this was supposed to be a session-load */
2758 if (Flags)
2759 {
2760 /* We don't support session loading yet */
2761 DPRINT1("Unsupported Session-Load!\n");
2762 while (TRUE);
2763 }
2764
2765 /* Check the loader list again, we should end up in the path below */
2766 goto LoaderScan;
2767 }
2768 else
2769 {
2770 /* We don't have a valid entry */
2771 LdrEntry = NULL;
2772 }
2773
2774 /* Load the image */
2775 Status = MiLoadImageSection(&Section,
2776 &ModuleLoadBase,
2777 FileName,
2778 FALSE,
2779 NULL);
2780 ASSERT(Status != STATUS_ALREADY_COMMITTED);
2781
2782 /* Get the size of the driver */
2783 DriverSize = ((PROS_SECTION_OBJECT)Section)->ImageSection->ImageSize;
2784
2785 /* Make sure we're not being loaded into session space */
2786 if (!Flags)
2787 {
2788 /* Check for success */
2789 if (NT_SUCCESS(Status))
2790 {
2791 /* Support large pages for drivers */
2792 MiUseLargeDriverPage(DriverSize / PAGE_SIZE,
2793 &ModuleLoadBase,
2794 &BaseName,
2795 TRUE);
2796 }
2797
2798 /* Dereference the section */
2799 ObDereferenceObject(Section);
2800 Section = NULL;
2801 }
2802
2803 /* Check for failure of the load earlier */
2804 if (!NT_SUCCESS(Status)) goto Quickie;
2805
2806 /* Relocate the driver */
2807 Status = LdrRelocateImageWithBias(ModuleLoadBase,
2808 0,
2809 "SYSLDR",
2810 STATUS_SUCCESS,
2811 STATUS_CONFLICTING_ADDRESSES,
2812 STATUS_INVALID_IMAGE_FORMAT);
2813 if (!NT_SUCCESS(Status)) goto Quickie;
2814
2815
2816 /* Get the NT Header */
2817 NtHeader = RtlImageNtHeader(ModuleLoadBase);
2818
2819 /* Calculate the size we'll need for the entry and allocate it */
2820 EntrySize = sizeof(LDR_DATA_TABLE_ENTRY) +
2821 BaseName.Length +
2822 sizeof(UNICODE_NULL);
2823
2824 /* Allocate the entry */
2825 LdrEntry = ExAllocatePoolWithTag(NonPagedPool, EntrySize, TAG_MODULE_OBJECT);
2826 if (!LdrEntry)
2827 {
2828 /* Fail */
2829 Status = STATUS_INSUFFICIENT_RESOURCES;
2830 goto Quickie;
2831 }
2832
2833 /* Setup the entry */
2834 LdrEntry->Flags = LDRP_LOAD_IN_PROGRESS;
2835 LdrEntry->LoadCount = 1;
2836 LdrEntry->LoadedImports = LoadedImports;
2837 LdrEntry->PatchInformation = NULL;
2838
2839 /* Check the version */
2840 if ((NtHeader->OptionalHeader.MajorOperatingSystemVersion >= 5) &&
2841 (NtHeader->OptionalHeader.MajorImageVersion >= 5))
2842 {
2843 /* Mark this image as a native image */
2844 LdrEntry->Flags |= LDRP_ENTRY_NATIVE;
2845 }
2846
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;
2854
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;
2859
2860 /* Copy and null-terminate it */
2861 RtlCopyMemory(LdrEntry->BaseDllName.Buffer,
2862 BaseName.Buffer,
2863 BaseName.Length);
2864 LdrEntry->BaseDllName.Buffer[BaseName.Length / sizeof(WCHAR)] = UNICODE_NULL;
2865
2866 /* Now allocate the full name */
2867 LdrEntry->FullDllName.Buffer = ExAllocatePoolWithTag(PagedPool,
2868 PrefixName.Length +
2869 sizeof(UNICODE_NULL),
2870 TAG_LDR_WSTR);
2871 if (!LdrEntry->FullDllName.Buffer)
2872 {
2873 /* Don't fail, just set it to zero */
2874 LdrEntry->FullDllName.Length = 0;
2875 LdrEntry->FullDllName.MaximumLength = 0;
2876 }
2877 else
2878 {
2879 /* Set it up */
2880 LdrEntry->FullDllName.Length = PrefixName.Length;
2881 LdrEntry->FullDllName.MaximumLength = PrefixName.Length;
2882
2883 /* Copy and null-terminate */
2884 RtlCopyMemory(LdrEntry->FullDllName.Buffer,
2885 PrefixName.Buffer,
2886 PrefixName.Length);
2887 LdrEntry->FullDllName.Buffer[PrefixName.Length / sizeof(WCHAR)] = UNICODE_NULL;
2888 }
2889
2890 /* Add the entry */
2891 MiProcessLoaderEntry(LdrEntry, TRUE);
2892
2893 /* Resolve imports */
2894 MissingApiName = Buffer;
2895 Status = MiResolveImageReferences(ModuleLoadBase,
2896 &BaseDirectory,
2897 NULL,
2898 &MissingApiName,
2899 &MissingDriverName,
2900 &LoadedImports);
2901 if (!NT_SUCCESS(Status))
2902 {
2903 /* Fail */
2904 MiProcessLoaderEntry(LdrEntry, FALSE);
2905
2906 /* Check if we need to free the name */
2907 if (LdrEntry->FullDllName.Buffer)
2908 {
2909 /* Free it */
2910 ExFreePool(LdrEntry->FullDllName.Buffer);
2911 }
2912
2913 /* Free the entry itself */
2914 ExFreePoolWithTag(LdrEntry, TAG_MODULE_OBJECT);
2915 LdrEntry = NULL;
2916 goto Quickie;
2917 }
2918
2919 /* Update the loader entry */
2920 LdrEntry->Flags |= (LDRP_SYSTEM_MAPPED |
2921 LDRP_ENTRY_PROCESSED |
2922 LDRP_MM_LOADED);
2923 LdrEntry->Flags &= ~LDRP_LOAD_IN_PROGRESS;
2924 LdrEntry->LoadedImports = LoadedImports;
2925
2926 /* FIXME: Call driver verifier's loader function */
2927
2928 /* Write-protect the system image */
2929 MiWriteProtectSystemImage(LdrEntry->DllBase);
2930
2931 /* Check if notifications are enabled */
2932 if (PsImageNotifyEnabled)
2933 {
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;
2941
2942 /* Send the notification */
2943 PspRunLoadImageNotifyRoutines(FileName, NULL, &ImageInfo);
2944 }
2945
2946 #if defined(KDBG) || defined(_WINKD_)
2947 /* MiCacheImageSymbols doesn't detect rossym */
2948 if (TRUE)
2949 #else
2950 /* Check if there's symbols */
2951 if (MiCacheImageSymbols(LdrEntry->DllBase))
2952 #endif
2953 {
2954 /* Check if the system root is present */
2955 if ((PrefixName.Length > (11 * sizeof(WCHAR))) &&
2956 !(_wcsnicmp(PrefixName.Buffer, L"\\SystemRoot", 11)))
2957 {
2958 /* Add the system root */
2959 UnicodeTemp = PrefixName;
2960 UnicodeTemp.Buffer += 11;
2961 UnicodeTemp.Length -= (11 * sizeof(WCHAR));
2962 sprintf_nt(Buffer,
2963 "%ws%wZ",
2964 &SharedUserData->NtSystemRoot[2],
2965 &UnicodeTemp);
2966 }
2967 else
2968 {
2969 /* Build the name */
2970 sprintf_nt(Buffer, "%wZ", &BaseName);
2971 }
2972
2973 /* Setup the ansi string */
2974 RtlInitString(&AnsiTemp, Buffer);
2975
2976 /* Notify the debugger */
2977 DbgLoadImageSymbols(&AnsiTemp,
2978 LdrEntry->DllBase,
2979 (ULONG_PTR)ZwCurrentProcess());
2980 LdrEntry->Flags |= LDRP_DEBUG_SYMBOLS_LOADED;
2981 }
2982
2983 /* Page the driver */
2984 ASSERT(Section == NULL);
2985 MiEnablePagingOfDriver(LdrEntry);
2986
2987 /* Return pointers */
2988 *ModuleObject = LdrEntry;
2989 *ImageBaseAddress = LdrEntry->DllBase;
2990
2991 Quickie:
2992 /* Check if we have the lock acquired */
2993 if (LockOwned)
2994 {
2995 /* Release the lock */
2996 KeReleaseMutant(&MmSystemLoadLock, 1, FALSE, FALSE);
2997 KeLeaveCriticalRegion();
2998 LockOwned = FALSE;
2999 }
3000
3001 /* If we have a file handle, close it */
3002 if (FileHandle) ZwClose(FileHandle);
3003
3004 /* Check if we had a prefix */
3005 if (NamePrefix) ExFreePool(PrefixName.Buffer);
3006
3007 /* Free the name buffer and return status */
3008 ExFreePoolWithTag(Buffer, TAG_LDR_WSTR);
3009 return Status;
3010 }
3011
3012 PLDR_DATA_TABLE_ENTRY
3013 NTAPI
3014 MiLookupDataTableEntry(IN PVOID Address)
3015 {
3016 PLDR_DATA_TABLE_ENTRY LdrEntry, FoundEntry = NULL;
3017 PLIST_ENTRY NextEntry;
3018 PAGED_CODE();
3019
3020 /* Loop entries */
3021 NextEntry = PsLoadedModuleList.Flink;
3022 do
3023 {
3024 /* Get the loader entry */
3025 LdrEntry = CONTAINING_RECORD(NextEntry,
3026 LDR_DATA_TABLE_ENTRY,
3027 InLoadOrderLinks);
3028
3029 /* Check if the address matches */
3030 if ((Address >= LdrEntry->DllBase) &&
3031 (Address < (PVOID)((ULONG_PTR)LdrEntry->DllBase +
3032 LdrEntry->SizeOfImage)))
3033 {
3034 /* Found a match */
3035 FoundEntry = LdrEntry;
3036 break;
3037 }
3038
3039 /* Move on */
3040 NextEntry = NextEntry->Flink;
3041 } while(NextEntry != &PsLoadedModuleList);
3042
3043 /* Return the entry */
3044 return FoundEntry;
3045 }
3046
3047 /* PUBLIC FUNCTIONS ***********************************************************/
3048
3049 /*
3050 * @implemented
3051 */
3052 PVOID
3053 NTAPI
3054 MmPageEntireDriver(IN PVOID AddressWithinSection)
3055 {
3056 PMMPTE StartPte, EndPte;
3057 PLDR_DATA_TABLE_ENTRY LdrEntry;
3058 PAGED_CODE();
3059
3060 /* Get the loader entry */
3061 LdrEntry = MiLookupDataTableEntry(AddressWithinSection);
3062 if (!LdrEntry) return NULL;
3063
3064 /* Check if paging of kernel mode is disabled or if the driver is mapped as an image */
3065 if ((MmDisablePagingExecutive) || (LdrEntry->SectionPointer))
3066 {
3067 /* Don't do anything, just return the base address */
3068 return LdrEntry->DllBase;
3069 }
3070
3071 /* Wait for active DPCs to finish before we page out the driver */
3072 KeFlushQueuedDpcs();
3073
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);
3077
3078 /* Enable paging for the PTE range */
3079 ASSERT(MI_IS_SESSION_IMAGE_ADDRESS(AddressWithinSection) == FALSE);
3080 MiSetPagingOfDriver(StartPte, EndPte);
3081
3082 /* Return the base address */
3083 return LdrEntry->DllBase;
3084 }
3085
3086 /*
3087 * @unimplemented
3088 */
3089 VOID
3090 NTAPI
3091 MmResetDriverPaging(IN PVOID AddressWithinSection)
3092 {
3093 UNIMPLEMENTED;
3094 }
3095
3096 /*
3097 * @implemented
3098 */
3099 PVOID
3100 NTAPI
3101 MmGetSystemRoutineAddress(IN PUNICODE_STRING SystemRoutineName)
3102 {
3103 PVOID ProcAddress = NULL;
3104 ANSI_STRING AnsiRoutineName;
3105 NTSTATUS Status;
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");
3111 ULONG Modules = 0;
3112
3113 /* Convert routine to ansi name */
3114 Status = RtlUnicodeStringToAnsiString(&AnsiRoutineName,
3115 SystemRoutineName,
3116 TRUE);
3117 if (!NT_SUCCESS(Status)) return NULL;
3118
3119 /* Lock the list */
3120 KeEnterCriticalRegion();
3121 ExAcquireResourceSharedLite(&PsLoadedModuleResource, TRUE);
3122
3123 /* Loop the loaded module list */
3124 NextEntry = PsLoadedModuleList.Flink;
3125 while (NextEntry != &PsLoadedModuleList)
3126 {
3127 /* Get the entry */
3128 LdrEntry = CONTAINING_RECORD(NextEntry,
3129 LDR_DATA_TABLE_ENTRY,
3130 InLoadOrderLinks);
3131
3132 /* Check if it's the kernel or HAL */
3133 if (RtlEqualUnicodeString(&KernelName, &LdrEntry->BaseDllName, TRUE))
3134 {
3135 /* Found it */
3136 Found = TRUE;
3137 Modules++;
3138 }
3139 else if (RtlEqualUnicodeString(&HalName, &LdrEntry->BaseDllName, TRUE))
3140 {
3141 /* Found it */
3142 Found = TRUE;
3143 Modules++;
3144 }
3145
3146 /* Check if we found a valid binary */
3147 if (Found)
3148 {
3149 /* Find the procedure name */
3150 ProcAddress = MiFindExportedRoutineByName(LdrEntry->DllBase,
3151 &AnsiRoutineName);
3152
3153 /* Break out if we found it or if we already tried both modules */
3154 if (ProcAddress) break;
3155 if (Modules == 2) break;
3156 }
3157
3158 /* Keep looping */
3159 NextEntry = NextEntry->Flink;
3160 }
3161
3162 /* Release the lock */
3163 ExReleaseResourceLite(&PsLoadedModuleResource);
3164 KeLeaveCriticalRegion();
3165
3166 /* Free the string and return */
3167 RtlFreeAnsiString(&AnsiRoutineName);
3168 return ProcAddress;
3169 }
3170