3 #include <internal/arch/mm.h>
5 /* TYPES *********************************************************************/
6 #define MM_SEGMENT_FINALIZE (0x40000000)
9 #define MIN(x,y) (((x)<(y))?(x):(y))
10 #define MAX(x,y) (((x)>(y))?(x):(y))
12 /* Determine what's needed to make paged pool fit in this category.
13 * it seems that something more is required to satisfy arm3. */
14 #define BALANCER_CAN_EVICT(Consumer) \
15 (((Consumer) == MC_USER) || \
16 ((Consumer) == MC_CACHE))
18 #define SEC_CACHE (0x20000000)
20 #define MiWaitForPageEvent(Process,Address) do { \
21 DPRINT("MiWaitForPageEvent %p:%p #\n", Process, Address); \
22 KeWaitForSingleObject(&MmWaitPageEvent, 0, KernelMode, FALSE, NULL); \
25 #define MiSetPageEvent(Process,Address) do { \
26 DPRINT("MiSetPageEvent %p:%p #\n",Process, (PVOID)(Address)); \
27 KeSetEvent(&MmWaitPageEvent, IO_NO_INCREMENT, FALSE); \
30 /* We store 8 bits of location with a page association */
31 #define ENTRIES_PER_ELEMENT 256
33 extern KEVENT MmWaitPageEvent
;
35 typedef struct _CACHE_SECTION_PAGE_TABLE
37 LARGE_INTEGER FileOffset
;
38 PMM_SECTION_SEGMENT Segment
;
40 ULONG_PTR PageEntries
[ENTRIES_PER_ELEMENT
];
41 } CACHE_SECTION_PAGE_TABLE
, *PCACHE_SECTION_PAGE_TABLE
;
43 struct _MM_REQUIRED_RESOURCES
;
45 typedef NTSTATUS (NTAPI
* AcquireResource
)(
46 PMMSUPPORT AddressSpace
,
47 struct _MEMORY_AREA
*MemoryArea
,
48 struct _MM_REQUIRED_RESOURCES
*Required
);
50 typedef NTSTATUS (NTAPI
* NotPresentFaultHandler
)(
51 PMMSUPPORT AddressSpace
,
52 struct _MEMORY_AREA
*MemoryArea
,
55 struct _MM_REQUIRED_RESOURCES
*Required
);
57 typedef NTSTATUS (NTAPI
* FaultHandler
)(
58 PMMSUPPORT AddressSpace
,
59 struct _MEMORY_AREA
*MemoryArea
,
61 struct _MM_REQUIRED_RESOURCES
*Required
);
63 typedef struct _MM_REQUIRED_RESOURCES
70 LARGE_INTEGER FileOffset
;
71 AcquireResource DoAcquisition
;
77 } MM_REQUIRED_RESOURCES
, *PMM_REQUIRED_RESOURCES
;
81 MmCreateCacheSection(PSECTION
*SectionObject
,
82 ACCESS_MASK DesiredAccess
,
83 POBJECT_ATTRIBUTES ObjectAttributes
,
84 PLARGE_INTEGER UMaximumSize
,
85 ULONG SectionPageProtection
,
86 ULONG AllocationAttributes
,
87 PFILE_OBJECT FileObject
);
91 MmWithdrawSectionPage(PMM_SECTION_SEGMENT Segment
,
92 PLARGE_INTEGER FileOffset
,
97 MmFinalizeSectionPageOut(PMM_SECTION_SEGMENT Segment
,
98 PLARGE_INTEGER FileOffset
,
102 /* sptab.c *******************************************************************/
106 MiInitializeSectionPageTable(PMM_SECTION_SEGMENT Segment
);
108 typedef VOID (NTAPI
*FREE_SECTION_PAGE_FUN
)(
109 PMM_SECTION_SEGMENT Segment
,
110 PLARGE_INTEGER Offset
);
114 MmFreePageTablesSectionSegment(PMM_SECTION_SEGMENT Segment
,
115 FREE_SECTION_PAGE_FUN FreePage
);
119 MmSetSectionAssociation(PFN_NUMBER Page
,
120 PMM_SECTION_SEGMENT Segment
,
121 PLARGE_INTEGER Offset
);
125 MmDeleteSectionAssociation(PFN_NUMBER Page
);
129 MmpPageOutPhysicalAddress(PFN_NUMBER Page
);
131 /* io.c **********************************************************************/
134 MmspWaitForFileLock(PFILE_OBJECT File
);
138 MiSimpleRead(PFILE_OBJECT FileObject
,
139 PLARGE_INTEGER FileOffset
,
143 PIO_STATUS_BLOCK ReadStatus
);
147 _MiSimpleWrite(PFILE_OBJECT FileObject
,
148 PLARGE_INTEGER FileOffset
,
151 PIO_STATUS_BLOCK ReadStatus
,
155 #define MiSimpleWrite(F,O,B,L,R) _MiSimpleWrite(F,O,B,L,R,__FILE__,__LINE__)
159 _MiWriteBackPage(PFILE_OBJECT FileObject
,
160 PLARGE_INTEGER Offset
,
166 #define MiWriteBackPage(F,O,L,P) _MiWriteBackPage(F,O,L,P,__FILE__,__LINE__)
168 /* section.c *****************************************************************/
172 MmAccessFaultCacheSection(KPROCESSOR_MODE Mode
,
178 MiReadFilePage(PMMSUPPORT AddressSpace
,
179 PMEMORY_AREA MemoryArea
,
180 PMM_REQUIRED_RESOURCES RequiredResources
);
184 MiGetOnePage(PMMSUPPORT AddressSpace
,
185 PMEMORY_AREA MemoryArea
,
186 PMM_REQUIRED_RESOURCES RequiredResources
);
190 MiSwapInPage(PMMSUPPORT AddressSpace
,
191 PMEMORY_AREA MemoryArea
,
192 PMM_REQUIRED_RESOURCES RequiredResources
);
196 MiWriteSwapPage(PMMSUPPORT AddressSpace
,
197 PMEMORY_AREA MemoryArea
,
198 PMM_REQUIRED_RESOURCES Resources
);
202 MiWriteFilePage(PMMSUPPORT AddressSpace
,
203 PMEMORY_AREA MemoryArea
,
204 PMM_REQUIRED_RESOURCES Resources
);
208 MiFreeSegmentPage(PMM_SECTION_SEGMENT Segment
,
209 PLARGE_INTEGER FileOffset
);
212 _When_(return==STATUS_MORE_PROCESSING_REQUIRED
, _At_(Required
->DoAcquisition
, _Post_notnull_
))
215 MiCowCacheSectionPage (
216 _In_ PMMSUPPORT AddressSpace
,
217 _In_ PMEMORY_AREA MemoryArea
,
220 _Inout_ PMM_REQUIRED_RESOURCES Required
);
223 MmPageOutDeleteMapping(PVOID Context
,
228 MmFreeCacheSectionPage(PVOID Context
,
229 MEMORY_AREA
* MemoryArea
,
237 _MiFlushMappedSection(PVOID BaseAddress
,
238 PLARGE_INTEGER BaseOffset
,
239 PLARGE_INTEGER FileSize
,
244 #define MiFlushMappedSection(A,O,S,D) _MiFlushMappedSection(A,O,S,D,__FILE__,__LINE__)
248 MmFinalizeSegment(PMM_SECTION_SEGMENT Segment
);
252 MmFreeSectionSegments(PFILE_OBJECT FileObject
);
256 MmMapCacheViewInSystemSpaceAtOffset(IN PMM_SECTION_SEGMENT Segment
,
257 OUT PVOID
* MappedBase
,
258 IN PLARGE_INTEGER ViewOffset
,
259 IN OUT PULONG ViewSize
);
263 _MiMapViewOfSegment(PMMSUPPORT AddressSpace
,
264 PMM_SECTION_SEGMENT Segment
,
268 PLARGE_INTEGER ViewOffset
,
269 ULONG AllocationType
,
273 #define MiMapViewOfSegment(AddressSpace,Segment,BaseAddress,ViewSize,Protect,ViewOffset,AllocationType) \
274 _MiMapViewOfSegment(AddressSpace,Segment,BaseAddress,ViewSize,Protect,ViewOffset,AllocationType,__FILE__,__LINE__)
278 MmUnmapViewOfCacheSegment(PMMSUPPORT AddressSpace
,
283 MmUnmapCacheViewInSystemSpace(PVOID Address
);
286 _When_(return==STATUS_MORE_PROCESSING_REQUIRED
, _At_(Required
->DoAcquisition
, _Post_notnull_
))
289 MmNotPresentFaultCachePage (
290 _In_ PMMSUPPORT AddressSpace
,
291 _In_ MEMORY_AREA
* MemoryArea
,
294 _Inout_ PMM_REQUIRED_RESOURCES Required
);
298 MmPageOutPageFileView(PMMSUPPORT AddressSpace
,
299 PMEMORY_AREA MemoryArea
,
301 PMM_REQUIRED_RESOURCES Required
);
305 _MmTryToLockAddressSpace(IN PMMSUPPORT AddressSpace
,
309 BOOLEAN Result
= KeTryToAcquireGuardedMutex(&CONTAINING_RECORD(AddressSpace
, EPROCESS
, Vm
)->AddressCreationLock
);
310 //DbgPrint("(%s:%d) Try Lock Address Space %x -> %s\n", file, line, AddressSpace, Result ? "true" : "false");
314 #define MmTryToLockAddressSpace(x) _MmTryToLockAddressSpace(x,__FILE__,__LINE__)
318 MiWidenSegment(PMMSUPPORT AddressSpace
,
319 PMEMORY_AREA MemoryArea
,
320 PMM_REQUIRED_RESOURCES RequiredResources
);
324 MiSwapInSectionPage(PMMSUPPORT AddressSpace
,
325 PMEMORY_AREA MemoryArea
,
326 PMM_REQUIRED_RESOURCES RequiredResources
);
330 MmExtendCacheSection(PSECTION Section
,
331 PLARGE_INTEGER NewSize
,
336 _MiFlushMappedSection(PVOID BaseAddress
,
337 PLARGE_INTEGER BaseOffset
,
338 PLARGE_INTEGER FileSize
,
343 #define MiFlushMappedSection(A,O,S,D) _MiFlushMappedSection(A,O,S,D,__FILE__,__LINE__)
347 MmGetSegmentRmap(PFN_NUMBER Page
,
352 MmNotPresentFaultCacheSection(KPROCESSOR_MODE Mode
,
358 MiCacheEvictPages(PMM_SECTION_SEGMENT Segment
,
362 MiRosTrimCache(ULONG Target
,