2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/mdlsup.c
5 * PURPOSE: ARM Memory Manager Memory Descriptor List (MDL) Management
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 #define MODULE_INVOLVED_IN_ARM3
16 #include "../ARM3/miarm.h"
18 /* GLOBALS ********************************************************************/
21 BOOLEAN MmTrackLockedPages
;
22 SIZE_T MmSystemLockPagesCount
;
24 /* PUBLIC FUNCTIONS ***********************************************************/
31 MmCreateMdl(IN PMDL Mdl
,
38 // Check if we don't have an MDL built
43 // Calculate the size we'll need and allocate the MDL
45 Size
= MmSizeOfMdl(Base
, Length
);
46 Mdl
= ExAllocatePoolWithTag(NonPagedPool
, Size
, TAG_MDL
);
47 if (!Mdl
) return NULL
;
53 MmInitializeMdl(Mdl
, Base
, Length
);
62 MmSizeOfMdl(IN PVOID Base
,
66 // Return the MDL size
69 (ADDRESS_AND_SIZE_TO_SPAN_PAGES(Base
, Length
) * sizeof(PFN_NUMBER
));
77 MmBuildMdlForNonPagedPool(IN PMDL Mdl
)
79 PPFN_NUMBER MdlPages
, EndPage
;
80 PFN_NUMBER Pfn
, PageCount
;
87 ASSERT(Mdl
->ByteCount
!= 0);
88 ASSERT((Mdl
->MdlFlags
& (MDL_PAGES_LOCKED
|
89 MDL_MAPPED_TO_SYSTEM_VA
|
90 MDL_SOURCE_IS_NONPAGED_POOL
|
94 // We know the MDL isn't associated to a process now
99 // Get page and VA information
101 MdlPages
= (PPFN_NUMBER
)(Mdl
+ 1);
105 // Set the system address and now get the page count
107 Mdl
->MappedSystemVa
= (PVOID
)((ULONG_PTR
)Base
+ Mdl
->ByteOffset
);
108 PageCount
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(Mdl
->MappedSystemVa
,
110 ASSERT(PageCount
!= 0);
111 EndPage
= MdlPages
+ PageCount
;
116 PointerPte
= MiAddressToPte(Base
);
122 Pfn
= PFN_FROM_PTE(PointerPte
++);
124 } while (MdlPages
< EndPage
);
127 // Set the nonpaged pool flag
129 Mdl
->MdlFlags
|= MDL_SOURCE_IS_NONPAGED_POOL
;
132 // Check if this is an I/O mapping
134 if (!MiGetPfnEntry(Pfn
)) Mdl
->MdlFlags
|= MDL_IO_SPACE
;
142 MmAllocatePagesForMdl(IN PHYSICAL_ADDRESS LowAddress
,
143 IN PHYSICAL_ADDRESS HighAddress
,
144 IN PHYSICAL_ADDRESS SkipBytes
,
145 IN SIZE_T TotalBytes
)
148 // Call the internal routine
150 return MiAllocatePagesForMdl(LowAddress
,
163 MmAllocatePagesForMdlEx(IN PHYSICAL_ADDRESS LowAddress
,
164 IN PHYSICAL_ADDRESS HighAddress
,
165 IN PHYSICAL_ADDRESS SkipBytes
,
166 IN SIZE_T TotalBytes
,
167 IN MEMORY_CACHING_TYPE CacheType
,
170 MI_PFN_CACHE_ATTRIBUTE CacheAttribute
;
173 // Check for invalid cache type
175 if (CacheType
> MmWriteCombined
)
178 // Normalize to default
180 CacheAttribute
= MiNotMapped
;
185 // Conver to internal caching attribute
187 CacheAttribute
= MiPlatformCacheAttributes
[FALSE
][CacheType
];
191 // Only these flags are allowed
193 if (Flags
& ~(MM_DONT_ZERO_ALLOCATION
| MM_ALLOCATE_FROM_LOCAL_NODE_ONLY
))
202 // Call the internal routine
204 return MiAllocatePagesForMdl(LowAddress
,
217 MmFreePagesFromMdl(IN PMDL Mdl
)
224 DPRINT("Freeing MDL: %p\n", Mdl
);
229 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
230 ASSERT((Mdl
->MdlFlags
& MDL_IO_SPACE
) == 0);
231 ASSERT(((ULONG_PTR
)Mdl
->StartVa
& (PAGE_SIZE
- 1)) == 0);
234 // Get address and page information
236 Base
= (PVOID
)((ULONG_PTR
)Mdl
->StartVa
+ Mdl
->ByteOffset
);
237 NumberOfPages
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(Base
, Mdl
->ByteCount
);
242 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
245 // Loop all the MDL pages
247 Pages
= (PPFN_NUMBER
)(Mdl
+ 1);
251 // Reached the last page
253 if (*Pages
== LIST_HEAD
) break;
256 // Get the page entry
258 Pfn1
= MiGetPfnEntry(*Pages
);
260 ASSERT(Pfn1
->u2
.ShareCount
== 1);
261 ASSERT(MI_IS_PFN_DELETED(Pfn1
) == TRUE
);
262 if (Pfn1
->u4
.PteFrame
!= 0x1FFEDCB)
264 /* Corrupted PFN entry or invalid free */
265 KeBugCheckEx(MEMORY_MANAGEMENT
, 0x1236, (ULONG_PTR
)Mdl
, (ULONG_PTR
)Pages
, *Pages
);
271 Pfn1
->u3
.e1
.StartOfAllocation
= 0;
272 Pfn1
->u3
.e1
.EndOfAllocation
= 0;
273 Pfn1
->u2
.ShareCount
= 0;
278 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
279 if (Pfn1
->u3
.e2
.ReferenceCount
!= 1)
281 /* Just take off one reference */
282 InterlockedDecrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
286 /* We'll be nuking the whole page */
287 MiDecrementReferenceCount(Pfn1
, *Pages
);
291 // Clear this page and move on
293 *Pages
++ = LIST_HEAD
;
294 } while (--NumberOfPages
!= 0);
299 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
302 // Remove the pages locked flag
304 Mdl
->MdlFlags
&= ~MDL_PAGES_LOCKED
;
312 MmMapLockedPagesSpecifyCache(IN PMDL Mdl
,
313 IN KPROCESSOR_MODE AccessMode
,
314 IN MEMORY_CACHING_TYPE CacheType
,
315 IN PVOID BaseAddress
,
316 IN ULONG BugCheckOnFailure
,
317 IN MM_PAGE_PRIORITY Priority
)
320 PPFN_NUMBER MdlPages
, LastPage
;
321 PFN_NUMBER PageCount
;
323 MI_PFN_CACHE_ATTRIBUTE CacheAttribute
;
330 ASSERT(Mdl
->ByteCount
!= 0);
335 Base
= (PVOID
)((ULONG_PTR
)Mdl
->StartVa
+ Mdl
->ByteOffset
);
338 // Handle kernel case first
340 if (AccessMode
== KernelMode
)
343 // Get the list of pages and count
345 MdlPages
= (PPFN_NUMBER
)(Mdl
+ 1);
346 PageCount
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(Base
, Mdl
->ByteCount
);
347 LastPage
= MdlPages
+ PageCount
;
352 ASSERT((Mdl
->MdlFlags
& (MDL_MAPPED_TO_SYSTEM_VA
|
353 MDL_SOURCE_IS_NONPAGED_POOL
|
354 MDL_PARTIAL_HAS_BEEN_MAPPED
)) == 0);
355 ASSERT((Mdl
->MdlFlags
& (MDL_PAGES_LOCKED
| MDL_PARTIAL
)) != 0);
358 // Get the correct cache type
360 IsIoMapping
= (Mdl
->MdlFlags
& MDL_IO_SPACE
) != 0;
361 CacheAttribute
= MiPlatformCacheAttributes
[IsIoMapping
][CacheType
];
366 PointerPte
= MiReserveSystemPtes(PageCount
, SystemPteSpace
);
370 // If it can fail, return NULL
372 if (Mdl
->MdlFlags
& MDL_MAPPING_CAN_FAIL
) return NULL
;
375 // Should we bugcheck?
377 if (!BugCheckOnFailure
) return NULL
;
380 // Yes, crash the system
382 KeBugCheckEx(NO_MORE_SYSTEM_PTES
, 0, PageCount
, 0, 0);
386 // Get the mapped address
388 Base
= (PVOID
)((ULONG_PTR
)MiPteToAddress(PointerPte
) + Mdl
->ByteOffset
);
393 TempPte
= ValidKernelPte
;
394 switch (CacheAttribute
)
401 MI_PAGE_DISABLE_CACHE(&TempPte
);
402 MI_PAGE_WRITE_THROUGH(&TempPte
);
405 case MiWriteCombined
:
408 // Enable write combining
410 MI_PAGE_DISABLE_CACHE(&TempPte
);
411 MI_PAGE_WRITE_COMBINED(&TempPte
);
429 if (*MdlPages
== LIST_HEAD
) break;
434 TempPte
.u
.Hard
.PageFrameNumber
= *MdlPages
;
435 MI_WRITE_VALID_PTE(PointerPte
++, TempPte
);
436 } while (++MdlPages
< LastPage
);
441 ASSERT((Mdl
->MdlFlags
& MDL_MAPPED_TO_SYSTEM_VA
) == 0);
442 Mdl
->MappedSystemVa
= Base
;
443 Mdl
->MdlFlags
|= MDL_MAPPED_TO_SYSTEM_VA
;
446 // Check if it was partial
448 if (Mdl
->MdlFlags
& MDL_PARTIAL
)
451 // Write the appropriate flag here too
453 Mdl
->MdlFlags
|= MDL_PARTIAL_HAS_BEEN_MAPPED
;
457 // Return the mapped address
471 MmMapLockedPages(IN PMDL Mdl
,
472 IN KPROCESSOR_MODE AccessMode
)
475 // Call the extended version
477 return MmMapLockedPagesSpecifyCache(Mdl
,
490 MmUnmapLockedPages(IN PVOID BaseAddress
,
494 PFN_NUMBER PageCount
;
495 PPFN_NUMBER MdlPages
;
501 ASSERT(Mdl
->ByteCount
!= 0);
504 // Check if this is a kernel request
506 if (BaseAddress
> MM_HIGHEST_USER_ADDRESS
)
509 // Get base and count information
511 Base
= (PVOID
)((ULONG_PTR
)Mdl
->StartVa
+ Mdl
->ByteOffset
);
512 PageCount
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(Base
, Mdl
->ByteCount
);
517 ASSERT((Mdl
->MdlFlags
& MDL_PARENT_MAPPED_SYSTEM_VA
) == 0);
518 ASSERT(PageCount
!= 0);
519 ASSERT(Mdl
->MdlFlags
& MDL_MAPPED_TO_SYSTEM_VA
);
524 PointerPte
= MiAddressToPte(BaseAddress
);
527 // This should be a resident system PTE
529 ASSERT(PointerPte
>= MmSystemPtesStart
[SystemPteSpace
]);
530 ASSERT(PointerPte
<= MmSystemPtesEnd
[SystemPteSpace
]);
531 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
534 // Check if the caller wants us to free advanced pages
536 if (Mdl
->MdlFlags
& MDL_FREE_EXTRA_PTES
)
539 // Get the MDL page array
541 MdlPages
= (PPFN_NUMBER
)(Mdl
+ 1);
542 MdlPages
+= PageCount
;
547 PageCount
+= *MdlPages
;
548 PointerPte
-= *MdlPages
;
549 ASSERT(PointerPte
>= MmSystemPtesStart
[SystemPteSpace
]);
550 ASSERT(PointerPte
<= MmSystemPtesEnd
[SystemPteSpace
]);
553 // Get the new base address
555 BaseAddress
= (PVOID
)((ULONG_PTR
)BaseAddress
-
556 ((*MdlPages
) << PAGE_SHIFT
));
562 Mdl
->MdlFlags
&= ~(MDL_MAPPED_TO_SYSTEM_VA
|
563 MDL_PARTIAL_HAS_BEEN_MAPPED
|
564 MDL_FREE_EXTRA_PTES
);
567 // Release the system PTEs
569 MiReleaseSystemPtes(PointerPte
, PageCount
, SystemPteSpace
);
582 MmProbeAndLockPages(IN PMDL Mdl
,
583 IN KPROCESSOR_MODE AccessMode
,
584 IN LOCK_OPERATION Operation
)
586 PPFN_NUMBER MdlPages
;
587 PVOID Base
, Address
, LastAddress
, StartAddress
;
588 ULONG LockPages
, TotalPages
;
589 NTSTATUS Status
= STATUS_SUCCESS
;
590 PEPROCESS CurrentProcess
;
591 NTSTATUS ProbeStatus
;
592 PMMPTE PointerPte
, LastPte
;
594 PFN_NUMBER PageFrameIndex
;
597 USHORT OldRefCount
, RefCount
;
599 DPRINT("Probing MDL: %p\n", Mdl
);
604 ASSERT(Mdl
->ByteCount
!= 0);
605 ASSERT(((ULONG
)Mdl
->ByteOffset
& ~(PAGE_SIZE
- 1)) == 0);
606 ASSERT(((ULONG_PTR
)Mdl
->StartVa
& (PAGE_SIZE
- 1)) == 0);
607 ASSERT((Mdl
->MdlFlags
& (MDL_PAGES_LOCKED
|
608 MDL_MAPPED_TO_SYSTEM_VA
|
609 MDL_SOURCE_IS_NONPAGED_POOL
|
611 MDL_IO_SPACE
)) == 0);
614 // Get page and base information
616 MdlPages
= (PPFN_NUMBER
)(Mdl
+ 1);
617 Base
= (PVOID
)Mdl
->StartVa
;
620 // Get the addresses and how many pages we span (and need to lock)
622 Address
= (PVOID
)((ULONG_PTR
)Base
+ Mdl
->ByteOffset
);
623 LastAddress
= (PVOID
)((ULONG_PTR
)Address
+ Mdl
->ByteCount
);
624 LockPages
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(Address
, Mdl
->ByteCount
);
625 ASSERT(LockPages
!= 0);
627 /* Block invalid access */
628 if ((AccessMode
!= KernelMode
) &&
629 ((LastAddress
> (PVOID
)MM_USER_PROBE_ADDRESS
) || (Address
>= LastAddress
)))
631 /* Caller should be in SEH, raise the error */
632 *MdlPages
= LIST_HEAD
;
633 ExRaiseStatus(STATUS_ACCESS_VIOLATION
);
639 if (Address
<= MM_HIGHEST_USER_ADDRESS
)
644 CurrentProcess
= PsGetCurrentProcess();
651 CurrentProcess
= NULL
;
655 // Save the number of pages we'll have to lock, and the start address
657 TotalPages
= LockPages
;
658 StartAddress
= Address
;
660 /* Large pages not supported */
661 ASSERT(!MI_IS_PHYSICAL_ADDRESS(Address
));
666 ProbeStatus
= STATUS_SUCCESS
;
677 *MdlPages
= LIST_HEAD
;
682 *(volatile CHAR
*)Address
;
685 // Check if this is write access (only probe for user-mode)
687 if ((Operation
!= IoReadAccess
) &&
688 (Address
<= MM_HIGHEST_USER_ADDRESS
))
691 // Probe for write too
693 ProbeForWriteChar(Address
);
699 Address
= PAGE_ALIGN((ULONG_PTR
)Address
+ PAGE_SIZE
);
706 } while (Address
< LastAddress
);
709 // Reset back to the original page
711 ASSERT(LockPages
== 0);
712 MdlPages
= (PPFN_NUMBER
)(Mdl
+ 1);
714 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
719 ProbeStatus
= _SEH2_GetExceptionCode();
724 // So how did that go?
726 if (ProbeStatus
!= STATUS_SUCCESS
)
731 DPRINT1("MDL PROBE FAILED!\n");
733 ExRaiseStatus(ProbeStatus
);
737 // Get the PTE and PDE
739 PointerPte
= MiAddressToPte(StartAddress
);
740 PointerPde
= MiAddressToPde(StartAddress
);
741 #if (_MI_PAGING_LEVELS >= 3)
742 DPRINT1("PAE/x64 Not Implemented\n");
749 ASSERT(MdlPages
== (PPFN_NUMBER
)(Mdl
+ 1));
752 // Check what kind of operation this is
754 if (Operation
!= IoReadAccess
)
757 // Set the write flag
759 Mdl
->MdlFlags
|= MDL_WRITE_OPERATION
;
764 // Remove the write flag
766 Mdl
->MdlFlags
&= ~(MDL_WRITE_OPERATION
);
770 // Mark the MDL as locked *now*
772 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
775 // Check if this came from kernel mode
777 if (Base
>= MM_HIGHEST_USER_ADDRESS
)
780 // We should not have a process
782 ASSERT(CurrentProcess
== NULL
);
786 // In kernel mode, we don't need to check for write access
788 Operation
= IoReadAccess
;
794 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
801 ASSERT(TotalPages
!= 0);
802 ASSERT(CurrentProcess
== PsGetCurrentProcess());
805 // Track locked pages
807 InterlockedExchangeAddSizeT(&CurrentProcess
->NumberOfLockedPages
,
813 Mdl
->Process
= CurrentProcess
;
815 /* Lock the process working set */
816 MiLockProcessWorkingSet(CurrentProcess
, PsGetCurrentThread());
824 LastPte
= MiAddressToPte((PVOID
)((ULONG_PTR
)LastAddress
- 1));
832 // Assume failure and check for non-mapped pages
834 *MdlPages
= LIST_HEAD
;
835 #if (_MI_PAGING_LEVELS >= 3)
836 /* Should be checking the PPE and PXE */
839 while ((PointerPde
->u
.Hard
.Valid
== 0) ||
840 (PointerPte
->u
.Hard
.Valid
== 0))
843 // What kind of lock where we using?
850 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
854 /* Release process working set */
855 MiUnlockProcessWorkingSet(CurrentProcess
, PsGetCurrentThread());
861 Address
= MiPteToAddress(PointerPte
);
862 Status
= MmAccessFault(FALSE
, Address
, KernelMode
, NULL
);
863 if (!NT_SUCCESS(Status
))
868 DPRINT1("Access fault failed\n");
873 // Waht lock should we use?
880 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
884 /* Lock the process working set */
885 MiLockProcessWorkingSet(CurrentProcess
, PsGetCurrentThread());
890 // Check if this was a write or modify
892 if (Operation
!= IoReadAccess
)
895 // Check if the PTE is not writable
897 if (MI_IS_PAGE_WRITEABLE(PointerPte
) == FALSE
)
900 // Check if it's copy on write
902 if (MI_IS_PAGE_COPY_ON_WRITE(PointerPte
))
905 // Get the base address and allow a change for user-mode
907 Address
= MiPteToAddress(PointerPte
);
908 if (Address
<= MM_HIGHEST_USER_ADDRESS
)
911 // What kind of lock where we using?
918 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
922 /* Release process working set */
923 MiUnlockProcessWorkingSet(CurrentProcess
, PsGetCurrentThread());
929 Status
= MmAccessFault(TRUE
, Address
, KernelMode
, NULL
);
930 if (!NT_SUCCESS(Status
))
935 DPRINT1("Access fault failed\n");
940 // Re-acquire the lock
947 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
951 /* Lock the process working set */
952 MiLockProcessWorkingSet(CurrentProcess
, PsGetCurrentThread());
963 // Fail, since we won't allow this
965 Status
= STATUS_ACCESS_VIOLATION
;
966 goto CleanupWithLock
;
973 PageFrameIndex
= PFN_FROM_PTE(PointerPte
);
974 Pfn1
= MiGetPfnEntry(PageFrameIndex
);
977 /* Either this is for kernel-mode, or the working set is held */
978 ASSERT((CurrentProcess
== NULL
) || (UsePfnLock
== FALSE
));
980 /* No Physical VADs supported yet */
981 if (CurrentProcess
) ASSERT(CurrentProcess
->PhysicalVadRoot
== NULL
);
983 /* This address should already exist and be fully valid */
984 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
985 if (MI_IS_ROS_PFN(Pfn1
))
987 /* ReactOS Mm doesn't track share count */
988 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
992 /* On ARM3 pages, we should see a valid share count */
993 ASSERT((Pfn1
->u2
.ShareCount
!= 0) && (Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
));
995 /* We don't support mapping a prototype page yet */
996 ASSERT((Pfn1
->u3
.e1
.PrototypePte
== 0) && (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0));
999 /* More locked pages! */
1000 InterlockedExchangeAddSizeT(&MmSystemLockPagesCount
, 1);
1002 /* Loop trying to update the reference count */
1005 /* Get the current reference count, make sure it's valid */
1006 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1007 ASSERT(OldRefCount
!= 0);
1008 ASSERT(OldRefCount
< 2500);
1010 /* Bump it up by one */
1011 RefCount
= InterlockedCompareExchange16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
,
1014 ASSERT(RefCount
!= 0);
1015 } while (OldRefCount
!= RefCount
);
1017 /* Was this the first lock attempt? */
1018 if (OldRefCount
!= 1)
1020 /* Someone else came through */
1021 InterlockedExchangeAddSizeT(&MmSystemLockPagesCount
, -1);
1027 // For I/O addresses, just remember this
1029 Mdl
->MdlFlags
|= MDL_IO_SPACE
;
1033 // Write the page and move on
1035 *MdlPages
++ = PageFrameIndex
;
1038 /* Check if we're on a PDE boundary */
1039 if (!((ULONG_PTR
)PointerPte
& (PD_SIZE
- 1))) PointerPde
++;
1040 } while (PointerPte
<= LastPte
);
1043 // What kind of lock where we using?
1050 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1054 /* Release process working set */
1055 MiUnlockProcessWorkingSet(CurrentProcess
, PsGetCurrentThread());
1061 ASSERT((Mdl
->MdlFlags
& MDL_DESCRIBES_AWE
) == 0);
1066 // This is the failure path
1068 ASSERT(!NT_SUCCESS(Status
));
1071 // What kind of lock where we using?
1078 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1082 /* Release process working set */
1083 MiUnlockProcessWorkingSet(CurrentProcess
, PsGetCurrentThread());
1087 // Pages must be locked so MmUnlock can work
1089 ASSERT(Mdl
->MdlFlags
& MDL_PAGES_LOCKED
);
1095 ExRaiseStatus(Status
);
1103 MmUnlockPages(IN PMDL Mdl
)
1105 PPFN_NUMBER MdlPages
, LastPage
;
1108 ULONG Flags
, PageCount
;
1110 USHORT RefCount
, OldRefCount
;
1112 DPRINT("Unlocking MDL: %p\n", Mdl
);
1117 ASSERT((Mdl
->MdlFlags
& MDL_PAGES_LOCKED
) != 0);
1118 ASSERT((Mdl
->MdlFlags
& MDL_SOURCE_IS_NONPAGED_POOL
) == 0);
1119 ASSERT((Mdl
->MdlFlags
& MDL_PARTIAL
) == 0);
1120 ASSERT(Mdl
->ByteCount
!= 0);
1123 // Get the process associated and capture the flags which are volatile
1125 Process
= Mdl
->Process
;
1126 Flags
= Mdl
->MdlFlags
;
1129 // Automagically undo any calls to MmGetSystemAddressForMdl's for this MDL
1131 if (Mdl
->MdlFlags
& MDL_MAPPED_TO_SYSTEM_VA
)
1134 // Unmap the pages from system space
1136 MmUnmapLockedPages(Mdl
->MappedSystemVa
, Mdl
);
1140 // Get the page count
1142 MdlPages
= (PPFN_NUMBER
)(Mdl
+ 1);
1143 Base
= (PVOID
)((ULONG_PTR
)Mdl
->StartVa
+ Mdl
->ByteOffset
);
1144 PageCount
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(Base
, Mdl
->ByteCount
);
1145 ASSERT(PageCount
!= 0);
1148 // We don't support AWE
1150 if (Flags
& MDL_DESCRIBES_AWE
) ASSERT(FALSE
);
1153 // Check if the buffer is mapped I/O space
1155 if (Flags
& MDL_IO_SPACE
)
1160 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1165 LastPage
= MdlPages
+ PageCount
;
1169 // Last page, break out
1171 if (*MdlPages
== LIST_HEAD
) break;
1174 // Check if this page is in the PFN database
1176 Pfn1
= MiGetPfnEntry(*MdlPages
);
1179 /* Get the current entry and reference count */
1180 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1181 ASSERT(OldRefCount
!= 0);
1183 /* Is this already the last dereference */
1184 if (OldRefCount
== 1)
1186 /* It should be on a free list waiting for us */
1187 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 1);
1188 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1189 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1191 /* Not supported yet */
1192 ASSERT(((Pfn1
->u3
.e1
.PrototypePte
== 0) &&
1193 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0)));
1196 InterlockedExchangeAddSizeT(&MmSystemLockPagesCount
, -1);
1198 /* Do the last dereference, we're done here */
1199 MiDecrementReferenceCount(Pfn1
, *MdlPages
);
1203 /* Loop decrementing one reference */
1206 /* Make sure it's still valid */
1207 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1208 ASSERT(OldRefCount
!= 0);
1210 /* Take off one reference */
1211 RefCount
= InterlockedCompareExchange16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
,
1214 ASSERT(RefCount
!= 0);
1215 } while (OldRefCount
!= RefCount
);
1216 ASSERT(RefCount
> 1);
1218 /* Are there only lock references left? */
1221 /* And does the page still have users? */
1222 if (Pfn1
->u2
.ShareCount
>= 1)
1224 /* Then it should still be valid */
1225 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1227 /* Not supported yet */
1228 ASSERT(((Pfn1
->u3
.e1
.PrototypePte
== 0) &&
1229 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0)));
1231 /* But there is one less "locked" page though */
1232 InterlockedExchangeAddSizeT(&MmSystemLockPagesCount
, -1);
1237 } while (++MdlPages
< LastPage
);
1242 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1245 // Check if we have a process
1250 // Handle the accounting of locked pages
1252 ASSERT(Process
->NumberOfLockedPages
> 0);
1253 InterlockedExchangeAddSizeT(&Process
->NumberOfLockedPages
,
1260 Mdl
->MdlFlags
&= ~MDL_IO_SPACE
;
1261 Mdl
->MdlFlags
&= ~MDL_PAGES_LOCKED
;
1266 // Check if we have a process
1271 // Handle the accounting of locked pages
1273 ASSERT(Process
->NumberOfLockedPages
> 0);
1274 InterlockedExchangeAddSizeT(&Process
->NumberOfLockedPages
,
1281 LastPage
= MdlPages
+ PageCount
;
1285 // Last page reached
1287 if (*MdlPages
== LIST_HEAD
)
1290 // Were there no pages at all?
1292 if (MdlPages
== (PPFN_NUMBER
)(Mdl
+ 1))
1295 // We're already done
1297 Mdl
->MdlFlags
&= ~MDL_PAGES_LOCKED
;
1302 // Otherwise, stop here
1304 LastPage
= MdlPages
;
1308 /* Save the PFN entry instead for the secondary loop */
1309 *MdlPages
= (PFN_NUMBER
)MiGetPfnEntry(*MdlPages
);
1310 ASSERT((*MdlPages
) != 0);
1311 } while (++MdlPages
< LastPage
);
1316 MdlPages
= (PPFN_NUMBER
)(Mdl
+ 1);
1319 // Now grab the PFN lock for the actual unlock and dereference
1321 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1324 /* Get the current entry and reference count */
1325 Pfn1
= (PMMPFN
)(*MdlPages
);
1326 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1327 ASSERT(OldRefCount
!= 0);
1329 /* Is this already the last dereference */
1330 if (OldRefCount
== 1)
1332 /* It should be on a free list waiting for us */
1333 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
== 1);
1334 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1335 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1337 /* Not supported yet */
1338 ASSERT(((Pfn1
->u3
.e1
.PrototypePte
== 0) &&
1339 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0)));
1342 InterlockedExchangeAddSizeT(&MmSystemLockPagesCount
, -1);
1344 /* Do the last dereference, we're done here */
1345 MiDecrementReferenceCount(Pfn1
, MiGetPfnEntryIndex(Pfn1
));
1349 /* Loop decrementing one reference */
1352 /* Make sure it's still valid */
1353 OldRefCount
= Pfn1
->u3
.e2
.ReferenceCount
;
1354 ASSERT(OldRefCount
!= 0);
1356 /* Take off one reference */
1357 RefCount
= InterlockedCompareExchange16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
,
1360 ASSERT(RefCount
!= 0);
1361 } while (OldRefCount
!= RefCount
);
1362 ASSERT(RefCount
> 1);
1364 /* Are there only lock references left? */
1367 /* And does the page still have users? */
1368 if (Pfn1
->u2
.ShareCount
>= 1)
1370 /* Then it should still be valid */
1371 ASSERT(Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
);
1373 /* Not supported yet */
1374 ASSERT(((Pfn1
->u3
.e1
.PrototypePte
== 0) &&
1375 (Pfn1
->OriginalPte
.u
.Soft
.Prototype
== 0)));
1377 /* But there is one less "locked" page though */
1378 InterlockedExchangeAddSizeT(&MmSystemLockPagesCount
, -1);
1382 } while (++MdlPages
< LastPage
);
1387 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1392 Mdl
->MdlFlags
&= ~MDL_PAGES_LOCKED
;
1400 MmAdvanceMdl(IN PMDL Mdl
,
1401 IN ULONG NumberOfBytes
)
1404 return STATUS_NOT_IMPLEMENTED
;
1412 MmMapLockedPagesWithReservedMapping(IN PVOID MappingAddress
,
1414 IN PMDL MemoryDescriptorList
,
1415 IN MEMORY_CACHING_TYPE CacheType
)
1426 MmUnmapReservedMapping(IN PVOID BaseAddress
,
1428 IN PMDL MemoryDescriptorList
)
1438 MmPrefetchPages(IN ULONG NumberOfLists
,
1439 IN PREAD_LIST
*ReadLists
)
1442 return STATUS_NOT_IMPLEMENTED
;
1450 MmProtectMdlSystemAddress(IN PMDL MemoryDescriptorList
,
1451 IN ULONG NewProtect
)
1454 return STATUS_NOT_IMPLEMENTED
;
1462 MmProbeAndLockProcessPages(IN OUT PMDL MemoryDescriptorList
,
1463 IN PEPROCESS Process
,
1464 IN KPROCESSOR_MODE AccessMode
,
1465 IN LOCK_OPERATION Operation
)
1476 MmProbeAndLockSelectedPages(IN OUT PMDL MemoryDescriptorList
,
1477 IN LARGE_INTEGER PageList
[],
1478 IN KPROCESSOR_MODE AccessMode
,
1479 IN LOCK_OPERATION Operation
)
1489 MmMapMemoryDumpMdl(IN PMDL Mdl
)