xref: /reactos/ntoskrnl/cache/section/newmm.h (revision 9393fc32)
1 #pragma once
2 
3 #include <internal/arch/mm.h>
4 
5 /* TYPES *********************************************************************/
6 #define MM_SEGMENT_FINALIZE (0x40000000)
7 
8 
9 #define MIN(x,y) (((x)<(y))?(x):(y))
10 #define MAX(x,y) (((x)>(y))?(x):(y))
11 
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))
17 
18 #define SEC_CACHE                           (0x20000000)
19 
20 /* We store 8 bits of location with a page association */
21 #define ENTRIES_PER_ELEMENT 256
22 
23 extern KEVENT MmWaitPageEvent;
24 
25 typedef struct _CACHE_SECTION_PAGE_TABLE
26 {
27     LARGE_INTEGER FileOffset;
28     PMM_SECTION_SEGMENT Segment;
29     ULONG Refcount;
30     ULONG_PTR PageEntries[ENTRIES_PER_ELEMENT];
31 } CACHE_SECTION_PAGE_TABLE, *PCACHE_SECTION_PAGE_TABLE;
32 
33 struct _MM_REQUIRED_RESOURCES;
34 
35 typedef NTSTATUS (NTAPI * AcquireResource)(
36      PMMSUPPORT AddressSpace,
37      struct _MEMORY_AREA *MemoryArea,
38      struct _MM_REQUIRED_RESOURCES *Required);
39 
40 typedef NTSTATUS (NTAPI * NotPresentFaultHandler)(
41     PMMSUPPORT AddressSpace,
42     struct _MEMORY_AREA *MemoryArea,
43     PVOID Address,
44     BOOLEAN Locked,
45     struct _MM_REQUIRED_RESOURCES *Required);
46 
47 typedef NTSTATUS (NTAPI * FaultHandler)(
48     PMMSUPPORT AddressSpace,
49     struct _MEMORY_AREA *MemoryArea,
50     PVOID Address,
51     struct _MM_REQUIRED_RESOURCES *Required);
52 
53 typedef struct _MM_REQUIRED_RESOURCES
54 {
55     ULONG Consumer;
56     ULONG Amount;
57     ULONG Offset;
58     ULONG State;
59     PVOID Context;
60     LARGE_INTEGER FileOffset;
61     AcquireResource DoAcquisition;
62     PFN_NUMBER Page[2];
63     PVOID Buffer[2];
64     SWAPENTRY SwapEntry;
65     const char *File;
66     int Line;
67 } MM_REQUIRED_RESOURCES, *PMM_REQUIRED_RESOURCES;
68 
69 NTSTATUS
70 NTAPI
71 MmCreateCacheSection(PSECTION *SectionObject,
72                      ACCESS_MASK DesiredAccess,
73                      POBJECT_ATTRIBUTES ObjectAttributes,
74                      PLARGE_INTEGER UMaximumSize,
75                      ULONG SectionPageProtection,
76                      ULONG AllocationAttributes,
77                      PFILE_OBJECT FileObject);
78 
79 PFN_NUMBER
80 NTAPI
81 MmWithdrawSectionPage(PMM_SECTION_SEGMENT Segment,
82                       PLARGE_INTEGER FileOffset,
83                       BOOLEAN *Dirty);
84 
85 NTSTATUS
86 NTAPI
87 MmFinalizeSectionPageOut(PMM_SECTION_SEGMENT Segment,
88                          PLARGE_INTEGER FileOffset,
89                          PFN_NUMBER Page,
90                          BOOLEAN Dirty);
91 
92 /* sptab.c *******************************************************************/
93 
94 VOID
95 NTAPI
96 MiInitializeSectionPageTable(PMM_SECTION_SEGMENT Segment);
97 
98 typedef VOID (NTAPI *FREE_SECTION_PAGE_FUN)(
99     PMM_SECTION_SEGMENT Segment,
100     PLARGE_INTEGER Offset);
101 
102 VOID
103 NTAPI
104 MmFreePageTablesSectionSegment(PMM_SECTION_SEGMENT Segment,
105                                FREE_SECTION_PAGE_FUN FreePage);
106 
107 NTSTATUS
108 NTAPI
109 MmSetSectionAssociation(PFN_NUMBER Page,
110                         PMM_SECTION_SEGMENT Segment,
111                         PLARGE_INTEGER Offset);
112 
113 VOID
114 NTAPI
115 MmDeleteSectionAssociation(PFN_NUMBER Page);
116 
117 NTSTATUS
118 NTAPI
119 MmpPageOutPhysicalAddress(PFN_NUMBER Page);
120 
121 /* io.c **********************************************************************/
122 
123 NTSTATUS
124 MmspWaitForFileLock(PFILE_OBJECT File);
125 
126 NTSTATUS
127 NTAPI
128 MiSimpleRead(PFILE_OBJECT FileObject,
129              PLARGE_INTEGER FileOffset,
130              PVOID Buffer,
131              ULONG Length,
132              BOOLEAN Paging,
133              PIO_STATUS_BLOCK ReadStatus);
134 
135 NTSTATUS
136 NTAPI
137 _MiSimpleWrite(PFILE_OBJECT FileObject,
138                PLARGE_INTEGER FileOffset,
139                PVOID Buffer,
140                ULONG Length,
141                PIO_STATUS_BLOCK ReadStatus,
142                const char *file,
143                int line);
144 
145 #define MiSimpleWrite(F,O,B,L,R) _MiSimpleWrite(F,O,B,L,R,__FILE__,__LINE__)
146 
147 NTSTATUS
148 NTAPI
149 _MiWriteBackPage(PFILE_OBJECT FileObject,
150                  PLARGE_INTEGER Offset,
151                  ULONG Length,
152                  PFN_NUMBER Page,
153                  const char *File,
154                  int Line);
155 
156 #define MiWriteBackPage(F,O,L,P) _MiWriteBackPage(F,O,L,P,__FILE__,__LINE__)
157 
158 /* section.c *****************************************************************/
159 
160 NTSTATUS
161 NTAPI
162 MmAccessFaultCacheSection(KPROCESSOR_MODE Mode,
163                           ULONG_PTR Address,
164                           BOOLEAN FromMdl);
165 
166 NTSTATUS
167 NTAPI
168 MiReadFilePage(PMMSUPPORT AddressSpace,
169                PMEMORY_AREA MemoryArea,
170                PMM_REQUIRED_RESOURCES RequiredResources);
171 
172 NTSTATUS
173 NTAPI
174 MiGetOnePage(PMMSUPPORT AddressSpace,
175              PMEMORY_AREA MemoryArea,
176              PMM_REQUIRED_RESOURCES RequiredResources);
177 
178 NTSTATUS
179 NTAPI
180 MiSwapInPage(PMMSUPPORT AddressSpace,
181              PMEMORY_AREA MemoryArea,
182              PMM_REQUIRED_RESOURCES RequiredResources);
183 
184 NTSTATUS
185 NTAPI
186 MiWriteSwapPage(PMMSUPPORT AddressSpace,
187                 PMEMORY_AREA MemoryArea,
188                 PMM_REQUIRED_RESOURCES Resources);
189 
190 NTSTATUS
191 NTAPI
192 MiWriteFilePage(PMMSUPPORT AddressSpace,
193                 PMEMORY_AREA MemoryArea,
194                 PMM_REQUIRED_RESOURCES Resources);
195 
196 VOID
197 NTAPI
198 MiFreeSegmentPage(PMM_SECTION_SEGMENT Segment,
199                   PLARGE_INTEGER FileOffset);
200 
201 _Success_(1)
202 _When_(return==STATUS_MORE_PROCESSING_REQUIRED, _At_(Required->DoAcquisition, _Post_notnull_))
203 NTSTATUS
204 NTAPI
205 MiCowCacheSectionPage (
206     _In_ PMMSUPPORT AddressSpace,
207     _In_ PMEMORY_AREA MemoryArea,
208     _In_ PVOID Address,
209     _In_ BOOLEAN Locked,
210     _Inout_ PMM_REQUIRED_RESOURCES Required);
211 
212 VOID
213 MmPageOutDeleteMapping(PVOID Context,
214                        PEPROCESS Process,
215                        PVOID Address);
216 
217 VOID
218 MmFreeCacheSectionPage(PVOID Context,
219                        MEMORY_AREA* MemoryArea,
220                        PVOID Address,
221                        PFN_NUMBER Page,
222                        SWAPENTRY SwapEntry,
223                        BOOLEAN Dirty);
224 
225 NTSTATUS
226 NTAPI
227 _MiFlushMappedSection(PVOID BaseAddress,
228                       PLARGE_INTEGER BaseOffset,
229                       PLARGE_INTEGER FileSize,
230                       BOOLEAN Dirty,
231                       const char *File,
232                       int Line);
233 
234 #define MiFlushMappedSection(A,O,S,D) _MiFlushMappedSection(A,O,S,D,__FILE__,__LINE__)
235 
236 VOID
237 NTAPI
238 MmFinalizeSegment(PMM_SECTION_SEGMENT Segment);
239 
240 VOID
241 NTAPI
242 MmFreeSectionSegments(PFILE_OBJECT FileObject);
243 
244 NTSTATUS
245 NTAPI
246 MmMapCacheViewInSystemSpaceAtOffset(IN PMM_SECTION_SEGMENT Segment,
247                                     OUT PVOID* MappedBase,
248                                     IN PLARGE_INTEGER ViewOffset,
249                                     IN OUT PULONG ViewSize);
250 
251 NTSTATUS
252 NTAPI
253 _MiMapViewOfSegment(PMMSUPPORT AddressSpace,
254                     PMM_SECTION_SEGMENT Segment,
255                     PVOID* BaseAddress,
256                     SIZE_T ViewSize,
257                     ULONG Protect,
258                     PLARGE_INTEGER ViewOffset,
259                     ULONG AllocationType,
260                     const char *file,
261                     int line);
262 
263 #define MiMapViewOfSegment(AddressSpace,Segment,BaseAddress,ViewSize,Protect,ViewOffset,AllocationType) \
264     _MiMapViewOfSegment(AddressSpace,Segment,BaseAddress,ViewSize,Protect,ViewOffset,AllocationType,__FILE__,__LINE__)
265 
266 NTSTATUS
267 NTAPI
268 MmUnmapViewOfCacheSegment(PMMSUPPORT AddressSpace,
269                           PVOID BaseAddress);
270 
271 NTSTATUS
272 NTAPI
273 MmUnmapCacheViewInSystemSpace(PVOID Address);
274 
275 _Success_(1)
276 _When_(return==STATUS_MORE_PROCESSING_REQUIRED, _At_(Required->DoAcquisition, _Post_notnull_))
277 NTSTATUS
278 NTAPI
279 MmNotPresentFaultCachePage (
280     _In_ PMMSUPPORT AddressSpace,
281     _In_ MEMORY_AREA* MemoryArea,
282     _In_ PVOID Address,
283     _In_ BOOLEAN Locked,
284     _Inout_ PMM_REQUIRED_RESOURCES Required);
285 
286 NTSTATUS
287 NTAPI
288 MmPageOutPageFileView(PMMSUPPORT AddressSpace,
289                       PMEMORY_AREA MemoryArea,
290                       PVOID Address,
291                       PMM_REQUIRED_RESOURCES Required);
292 
293 FORCEINLINE
294 BOOLEAN
295 _MmTryToLockAddressSpace(IN PMMSUPPORT AddressSpace,
296                          const char *file,
297                          int line)
298 {
299     BOOLEAN Result = KeTryToAcquireGuardedMutex(&CONTAINING_RECORD(AddressSpace, EPROCESS, Vm)->AddressCreationLock);
300     //DbgPrint("(%s:%d) Try Lock Address Space %x -> %s\n", file, line, AddressSpace, Result ? "true" : "false");
301     return Result;
302 }
303 
304 #define MmTryToLockAddressSpace(x) _MmTryToLockAddressSpace(x,__FILE__,__LINE__)
305 
306 NTSTATUS
307 NTAPI
308 MiWidenSegment(PMMSUPPORT AddressSpace,
309                PMEMORY_AREA MemoryArea,
310                PMM_REQUIRED_RESOURCES RequiredResources);
311 
312 NTSTATUS
313 NTAPI
314 MiSwapInSectionPage(PMMSUPPORT AddressSpace,
315                     PMEMORY_AREA MemoryArea,
316                     PMM_REQUIRED_RESOURCES RequiredResources);
317 
318 NTSTATUS
319 NTAPI
320 MmExtendCacheSection(PSECTION Section,
321                      PLARGE_INTEGER NewSize,
322                      BOOLEAN ExtendFile);
323 
324 NTSTATUS
325 NTAPI
326 _MiFlushMappedSection(PVOID BaseAddress,
327                       PLARGE_INTEGER BaseOffset,
328                       PLARGE_INTEGER FileSize,
329                       BOOLEAN Dirty,
330                       const char *File,
331                       int Line);
332 
333 #define MiFlushMappedSection(A,O,S,D) _MiFlushMappedSection(A,O,S,D,__FILE__,__LINE__)
334 
335 PVOID
336 NTAPI
337 MmGetSegmentRmap(PFN_NUMBER Page,
338                  PULONG RawOffset);
339 
340 NTSTATUS
341 NTAPI
342 MmNotPresentFaultCacheSection(KPROCESSOR_MODE Mode,
343                               ULONG_PTR Address,
344                               BOOLEAN FromMdl);
345 
346 ULONG
347 NTAPI
348 MiCacheEvictPages(PMM_SECTION_SEGMENT Segment,
349                   ULONG Target);
350 
351 NTSTATUS
352 MiRosTrimCache(ULONG Target,
353                ULONG Priority,
354                PULONG NrFreed);
355