[NTOSKRNL/FREELDR/NDK]
[reactos.git] / reactos / ntoskrnl / mm / ARM3 / miarm.h
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/miarm.h
5 * PURPOSE: ARM Memory Manager Header
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 */
8
9 #define MI_LOWEST_VAD_ADDRESS (PVOID)MM_LOWEST_USER_ADDRESS
10
11 /* Make the code cleaner with some definitions for size multiples */
12 #define _1KB (1024u)
13 #define _1MB (1024 * _1KB)
14 #define _1GB (1024 * _1MB)
15
16 /* Everyone loves 64K */
17 #define _64K (64 * _1KB)
18
19 /* Area mapped by a PDE */
20 #define PDE_MAPPED_VA (PTE_COUNT * PAGE_SIZE)
21
22 /* Size of a page table */
23 #define PT_SIZE (PTE_COUNT * sizeof(MMPTE))
24
25 /* Size of a page directory */
26 #define PD_SIZE (PDE_COUNT * sizeof(MMPDE))
27
28 /* Stop using these! */
29 #define PD_COUNT PPE_PER_PAGE
30 #define PDE_COUNT PDE_PER_PAGE
31 #define PTE_COUNT PTE_PER_PAGE
32
33 /* Size of all page directories for a process */
34 #define SYSTEM_PD_SIZE (PD_COUNT * PD_SIZE)
35 #ifdef _M_IX86
36 C_ASSERT(SYSTEM_PD_SIZE == PAGE_SIZE);
37 #endif
38
39 //
40 // Protection Bits part of the internal memory manager Protection Mask, from:
41 // http://reactos.org/wiki/Techwiki:Memory_management_in_the_Windows_XP_kernel
42 // https://www.reactos.org/wiki/Techwiki:Memory_Protection_constants
43 // and public assertions.
44 //
45 #define MM_ZERO_ACCESS 0
46 #define MM_READONLY 1
47 #define MM_EXECUTE 2
48 #define MM_EXECUTE_READ 3
49 #define MM_READWRITE 4
50 #define MM_WRITECOPY 5
51 #define MM_EXECUTE_READWRITE 6
52 #define MM_EXECUTE_WRITECOPY 7
53 #define MM_PROTECT_ACCESS 7
54
55 //
56 // These are flags on top of the actual protection mask
57 //
58 #define MM_NOCACHE 0x08
59 #define MM_GUARDPAGE 0x10
60 #define MM_WRITECOMBINE 0x18
61 #define MM_PROTECT_SPECIAL 0x18
62
63 //
64 // These are special cases
65 //
66 #define MM_DECOMMIT (MM_ZERO_ACCESS | MM_GUARDPAGE)
67 #define MM_NOACCESS (MM_ZERO_ACCESS | MM_WRITECOMBINE)
68 #define MM_OUTSWAPPED_KSTACK (MM_EXECUTE_WRITECOPY | MM_WRITECOMBINE)
69 #define MM_INVALID_PROTECTION 0xFFFFFFFF
70
71 //
72 // Specific PTE Definitions that map to the Memory Manager's Protection Mask Bits
73 // The Memory Manager's definition define the attributes that must be preserved
74 // and these PTE definitions describe the attributes in the hardware sense. This
75 // helps deal with hardware differences between the actual boolean expression of
76 // the argument.
77 //
78 // For example, in the logical attributes, we want to express read-only as a flag
79 // but on x86, it is writability that must be set. On the other hand, on x86, just
80 // like in the kernel, it is disabling the caches that requires a special flag,
81 // while on certain architectures such as ARM, it is enabling the cache which
82 // requires a flag.
83 //
84 #if defined(_M_IX86) || defined(_M_AMD64)
85 //
86 // Access Flags
87 //
88 #define PTE_READONLY 0 // Doesn't exist on x86
89 #define PTE_EXECUTE 0 // Not worrying about NX yet
90 #define PTE_EXECUTE_READ 0 // Not worrying about NX yet
91 #define PTE_READWRITE 0x2
92 #define PTE_WRITECOPY 0x200
93 #define PTE_EXECUTE_READWRITE 0x2 // Not worrying about NX yet
94 #define PTE_EXECUTE_WRITECOPY 0x200
95 #define PTE_PROTOTYPE 0x400
96
97 //
98 // State Flags
99 //
100 #define PTE_VALID 0x1
101 #define PTE_ACCESSED 0x20
102 #define PTE_DIRTY 0x40
103
104 //
105 // Cache flags
106 //
107 #define PTE_ENABLE_CACHE 0
108 #define PTE_DISABLE_CACHE 0x10
109 #define PTE_WRITECOMBINED_CACHE 0x10
110 #elif defined(_M_ARM)
111 #define PTE_READONLY 0x200
112 #define PTE_EXECUTE 0 // Not worrying about NX yet
113 #define PTE_EXECUTE_READ 0 // Not worrying about NX yet
114 #define PTE_READWRITE 0 // Doesn't exist on ARM
115 #define PTE_WRITECOPY 0 // Doesn't exist on ARM
116 #define PTE_EXECUTE_READWRITE 0 // Not worrying about NX yet
117 #define PTE_EXECUTE_WRITECOPY 0 // Not worrying about NX yet
118 #define PTE_PROTOTYPE 0x400 // Using the Shared bit
119 //
120 // Cache flags
121 //
122 #define PTE_ENABLE_CACHE 0
123 #define PTE_DISABLE_CACHE 0x10
124 #define PTE_WRITECOMBINED_CACHE 0x10
125 #else
126 #error Define these please!
127 #endif
128
129 extern const ULONG_PTR MmProtectToPteMask[32];
130 extern const ULONG MmProtectToValue[32];
131
132 //
133 // Assertions for session images, addresses, and PTEs
134 //
135 #define MI_IS_SESSION_IMAGE_ADDRESS(Address) \
136 (((Address) >= MiSessionImageStart) && ((Address) < MiSessionImageEnd))
137
138 #define MI_IS_SESSION_ADDRESS(Address) \
139 (((Address) >= MmSessionBase) && ((Address) < MiSessionSpaceEnd))
140
141 #define MI_IS_SESSION_PTE(Pte) \
142 ((((PMMPTE)Pte) >= MiSessionBasePte) && (((PMMPTE)Pte) < MiSessionLastPte))
143
144 #define MI_IS_PAGE_TABLE_ADDRESS(Address) \
145 (((PVOID)(Address) >= (PVOID)PTE_BASE) && ((PVOID)(Address) <= (PVOID)PTE_TOP))
146
147 #define MI_IS_SYSTEM_PAGE_TABLE_ADDRESS(Address) \
148 (((Address) >= (PVOID)MiAddressToPte(MmSystemRangeStart)) && ((Address) <= (PVOID)PTE_TOP))
149
150 #define MI_IS_PAGE_TABLE_OR_HYPER_ADDRESS(Address) \
151 (((PVOID)(Address) >= (PVOID)PTE_BASE) && ((PVOID)(Address) <= (PVOID)MmHyperSpaceEnd))
152
153 //
154 // Creates a software PTE with the given protection
155 //
156 #define MI_MAKE_SOFTWARE_PTE(p, x) ((p)->u.Long = (x << MM_PTE_SOFTWARE_PROTECTION_BITS))
157
158 //
159 // Marks a PTE as deleted
160 //
161 #define MI_SET_PFN_DELETED(x) ((x)->PteAddress = (PMMPTE)((ULONG_PTR)(x)->PteAddress | 1))
162 #define MI_IS_PFN_DELETED(x) ((ULONG_PTR)((x)->PteAddress) & 1)
163
164 //
165 // Special values for LoadedImports
166 //
167 #ifdef _WIN64
168 #define MM_SYSLDR_NO_IMPORTS (PVOID)0xFFFFFFFFFFFFFFFEULL
169 #define MM_SYSLDR_BOOT_LOADED (PVOID)0xFFFFFFFFFFFFFFFFULL
170 #else
171 #define MM_SYSLDR_NO_IMPORTS (PVOID)0xFFFFFFFE
172 #define MM_SYSLDR_BOOT_LOADED (PVOID)0xFFFFFFFF
173 #endif
174 #define MM_SYSLDR_SINGLE_ENTRY 0x1
175
176 //
177 // Number of initial session IDs
178 //
179 #define MI_INITIAL_SESSION_IDS 64
180
181 #if defined(_M_IX86) || defined(_M_ARM)
182 //
183 // PFN List Sentinel
184 //
185 #define LIST_HEAD 0xFFFFFFFF
186
187 //
188 // Because GCC cannot automatically downcast 0xFFFFFFFF to lesser-width bits,
189 // we need a manual definition suited to the number of bits in the PteFrame.
190 // This is used as a LIST_HEAD for the colored list
191 //
192 #define COLORED_LIST_HEAD ((1 << 25) - 1) // 0x1FFFFFF
193 #elif defined(_M_AMD64)
194 #define LIST_HEAD 0xFFFFFFFFFFFFFFFFLL
195 #define COLORED_LIST_HEAD ((1ULL << 57) - 1) // 0x1FFFFFFFFFFFFFFLL
196 #else
197 #error Define these please!
198 #endif
199
200 //
201 // Special IRQL value (found in assertions)
202 //
203 #define MM_NOIRQL (KIRQL)0xFFFFFFFF
204
205 //
206 // Returns the color of a page
207 //
208 #define MI_GET_PAGE_COLOR(x) ((x) & MmSecondaryColorMask)
209 #define MI_GET_NEXT_COLOR() (MI_GET_PAGE_COLOR(++MmSystemPageColor))
210 #define MI_GET_NEXT_PROCESS_COLOR(x) (MI_GET_PAGE_COLOR(++(x)->NextPageColor))
211
212 //
213 // Prototype PTEs that don't yet have a pagefile association
214 //
215 #ifdef _WIN64
216 #define MI_PTE_LOOKUP_NEEDED 0xffffffffULL
217 #else
218 #define MI_PTE_LOOKUP_NEEDED 0xFFFFF
219 #endif
220
221 //
222 // Number of session data and tag pages
223 //
224 #define MI_SESSION_DATA_PAGES_MAXIMUM (MM_ALLOCATION_GRANULARITY / PAGE_SIZE)
225 #define MI_SESSION_TAG_PAGES_MAXIMUM (MM_ALLOCATION_GRANULARITY / PAGE_SIZE)
226
227 //
228 // Used by MiCheckSecuredVad
229 //
230 #define MM_READ_WRITE_ALLOWED 11
231 #define MM_READ_ONLY_ALLOWED 10
232 #define MM_NO_ACCESS_ALLOWED 01
233 #define MM_DELETE_CHECK 85
234
235 //
236 // System views are binned into 64K chunks
237 //
238 #define MI_SYSTEM_VIEW_BUCKET_SIZE _64K
239
240 //
241 // FIXFIX: These should go in ex.h after the pool merge
242 //
243 #ifdef _WIN64
244 #define POOL_BLOCK_SIZE 16
245 #else
246 #define POOL_BLOCK_SIZE 8
247 #endif
248 #define POOL_LISTS_PER_PAGE (PAGE_SIZE / POOL_BLOCK_SIZE)
249 #define BASE_POOL_TYPE_MASK 1
250 #define POOL_MAX_ALLOC (PAGE_SIZE - (sizeof(POOL_HEADER) + POOL_BLOCK_SIZE))
251
252 //
253 // Pool debugging/analysis/tracing flags
254 //
255 #define POOL_FLAG_CHECK_TIMERS 0x1
256 #define POOL_FLAG_CHECK_WORKERS 0x2
257 #define POOL_FLAG_CHECK_RESOURCES 0x4
258 #define POOL_FLAG_VERIFIER 0x8
259 #define POOL_FLAG_CHECK_DEADLOCK 0x10
260 #define POOL_FLAG_SPECIAL_POOL 0x20
261 #define POOL_FLAG_DBGPRINT_ON_FAILURE 0x40
262 #define POOL_FLAG_CRASH_ON_FAILURE 0x80
263
264 //
265 // BAD_POOL_HEADER codes during pool bugcheck
266 //
267 #define POOL_CORRUPTED_LIST 3
268 #define POOL_SIZE_OR_INDEX_MISMATCH 5
269 #define POOL_ENTRIES_NOT_ALIGNED_PREVIOUS 6
270 #define POOL_HEADER_NOT_ALIGNED 7
271 #define POOL_HEADER_IS_ZERO 8
272 #define POOL_ENTRIES_NOT_ALIGNED_NEXT 9
273 #define POOL_ENTRY_NOT_FOUND 10
274
275 //
276 // BAD_POOL_CALLER codes during pool bugcheck
277 //
278 #define POOL_ENTRY_CORRUPTED 1
279 #define POOL_ENTRY_ALREADY_FREE 6
280 #define POOL_ENTRY_NOT_ALLOCATED 7
281 #define POOL_ALLOC_IRQL_INVALID 8
282 #define POOL_FREE_IRQL_INVALID 9
283 #define POOL_BILLED_PROCESS_INVALID 13
284 #define POOL_HEADER_SIZE_INVALID 32
285
286 typedef struct _POOL_DESCRIPTOR
287 {
288 POOL_TYPE PoolType;
289 ULONG PoolIndex;
290 ULONG RunningAllocs;
291 ULONG RunningDeAllocs;
292 ULONG TotalPages;
293 ULONG TotalBigPages;
294 ULONG Threshold;
295 PVOID LockAddress;
296 PVOID PendingFrees;
297 LONG PendingFreeDepth;
298 SIZE_T TotalBytes;
299 SIZE_T Spare0;
300 LIST_ENTRY ListHeads[POOL_LISTS_PER_PAGE];
301 } POOL_DESCRIPTOR, *PPOOL_DESCRIPTOR;
302
303 typedef struct _POOL_HEADER
304 {
305 union
306 {
307 struct
308 {
309 #ifdef _WIN64
310 USHORT PreviousSize:8;
311 USHORT PoolIndex:8;
312 USHORT BlockSize:8;
313 USHORT PoolType:8;
314 #else
315 USHORT PreviousSize:9;
316 USHORT PoolIndex:7;
317 USHORT BlockSize:9;
318 USHORT PoolType:7;
319 #endif
320 };
321 ULONG Ulong1;
322 };
323 #ifdef _WIN64
324 ULONG PoolTag;
325 #endif
326 union
327 {
328 #ifdef _WIN64
329 PEPROCESS ProcessBilled;
330 #else
331 ULONG PoolTag;
332 #endif
333 struct
334 {
335 USHORT AllocatorBackTraceIndex;
336 USHORT PoolTagHash;
337 };
338 };
339 } POOL_HEADER, *PPOOL_HEADER;
340
341 C_ASSERT(sizeof(POOL_HEADER) == POOL_BLOCK_SIZE);
342 C_ASSERT(POOL_BLOCK_SIZE == sizeof(LIST_ENTRY));
343
344 typedef struct _POOL_TRACKER_TABLE
345 {
346 ULONG Key;
347 LONG NonPagedAllocs;
348 LONG NonPagedFrees;
349 SIZE_T NonPagedBytes;
350 LONG PagedAllocs;
351 LONG PagedFrees;
352 SIZE_T PagedBytes;
353 } POOL_TRACKER_TABLE, *PPOOL_TRACKER_TABLE;
354
355 typedef struct _POOL_TRACKER_BIG_PAGES
356 {
357 PVOID Va;
358 ULONG Key;
359 ULONG NumberOfPages;
360 PVOID QuotaObject;
361 } POOL_TRACKER_BIG_PAGES, *PPOOL_TRACKER_BIG_PAGES;
362
363 extern ULONG ExpNumberOfPagedPools;
364 extern POOL_DESCRIPTOR NonPagedPoolDescriptor;
365 extern PPOOL_DESCRIPTOR ExpPagedPoolDescriptor[16 + 1];
366 extern PPOOL_TRACKER_TABLE PoolTrackTable;
367
368 //
369 // END FIXFIX
370 //
371
372 typedef struct _MI_LARGE_PAGE_DRIVER_ENTRY
373 {
374 LIST_ENTRY Links;
375 UNICODE_STRING BaseName;
376 } MI_LARGE_PAGE_DRIVER_ENTRY, *PMI_LARGE_PAGE_DRIVER_ENTRY;
377
378 typedef enum _MMSYSTEM_PTE_POOL_TYPE
379 {
380 SystemPteSpace,
381 NonPagedPoolExpansion,
382 MaximumPtePoolTypes
383 } MMSYSTEM_PTE_POOL_TYPE;
384
385 typedef enum _MI_PFN_CACHE_ATTRIBUTE
386 {
387 MiNonCached,
388 MiCached,
389 MiWriteCombined,
390 MiNotMapped
391 } MI_PFN_CACHE_ATTRIBUTE, *PMI_PFN_CACHE_ATTRIBUTE;
392
393 typedef struct _PHYSICAL_MEMORY_RUN
394 {
395 PFN_NUMBER BasePage;
396 PFN_NUMBER PageCount;
397 } PHYSICAL_MEMORY_RUN, *PPHYSICAL_MEMORY_RUN;
398
399 typedef struct _PHYSICAL_MEMORY_DESCRIPTOR
400 {
401 ULONG NumberOfRuns;
402 PFN_NUMBER NumberOfPages;
403 PHYSICAL_MEMORY_RUN Run[1];
404 } PHYSICAL_MEMORY_DESCRIPTOR, *PPHYSICAL_MEMORY_DESCRIPTOR;
405
406 typedef struct _MMCOLOR_TABLES
407 {
408 PFN_NUMBER Flink;
409 PVOID Blink;
410 PFN_NUMBER Count;
411 } MMCOLOR_TABLES, *PMMCOLOR_TABLES;
412
413 typedef struct _MI_LARGE_PAGE_RANGES
414 {
415 PFN_NUMBER StartFrame;
416 PFN_NUMBER LastFrame;
417 } MI_LARGE_PAGE_RANGES, *PMI_LARGE_PAGE_RANGES;
418
419 typedef struct _MMVIEW
420 {
421 ULONG_PTR Entry;
422 PCONTROL_AREA ControlArea;
423 } MMVIEW, *PMMVIEW;
424
425 typedef struct _MMSESSION
426 {
427 KGUARDED_MUTEX SystemSpaceViewLock;
428 PKGUARDED_MUTEX SystemSpaceViewLockPointer;
429 PCHAR SystemSpaceViewStart;
430 PMMVIEW SystemSpaceViewTable;
431 ULONG SystemSpaceHashSize;
432 ULONG SystemSpaceHashEntries;
433 ULONG SystemSpaceHashKey;
434 ULONG BitmapFailures;
435 PRTL_BITMAP SystemSpaceBitMap;
436 } MMSESSION, *PMMSESSION;
437
438 typedef struct _MM_SESSION_SPACE_FLAGS
439 {
440 ULONG Initialized:1;
441 ULONG DeletePending:1;
442 ULONG Filler:30;
443 } MM_SESSION_SPACE_FLAGS;
444
445 typedef struct _MM_SESSION_SPACE
446 {
447 struct _MM_SESSION_SPACE *GlobalVirtualAddress;
448 LONG ReferenceCount;
449 union
450 {
451 ULONG LongFlags;
452 MM_SESSION_SPACE_FLAGS Flags;
453 } u;
454 ULONG SessionId;
455 LIST_ENTRY ProcessList;
456 LARGE_INTEGER LastProcessSwappedOutTime;
457 PFN_NUMBER SessionPageDirectoryIndex;
458 SIZE_T NonPageablePages;
459 SIZE_T CommittedPages;
460 PVOID PagedPoolStart;
461 PVOID PagedPoolEnd;
462 PMMPTE PagedPoolBasePde;
463 ULONG Color;
464 LONG ResidentProcessCount;
465 ULONG SessionPoolAllocationFailures[4];
466 LIST_ENTRY ImageList;
467 LCID LocaleId;
468 ULONG AttachCount;
469 KEVENT AttachEvent;
470 PEPROCESS LastProcess;
471 LONG ProcessReferenceToSession;
472 LIST_ENTRY WsListEntry;
473 GENERAL_LOOKASIDE Lookaside[SESSION_POOL_LOOKASIDES];
474 MMSESSION Session;
475 KGUARDED_MUTEX PagedPoolMutex;
476 MM_PAGED_POOL_INFO PagedPoolInfo;
477 MMSUPPORT Vm;
478 PMMWSLE Wsle;
479 PDRIVER_UNLOAD Win32KDriverUnload;
480 POOL_DESCRIPTOR PagedPool;
481 #if defined (_M_AMD64)
482 MMPTE PageDirectory;
483 #else
484 PMMPTE PageTables;
485 #endif
486 #if defined (_M_AMD64)
487 PMMPTE SpecialPoolFirstPte;
488 PMMPTE SpecialPoolLastPte;
489 PMMPTE NextPdeForSpecialPoolExpansion;
490 PMMPTE LastPdeForSpecialPoolExpansion;
491 PFN_NUMBER SpecialPagesInUse;
492 #endif
493 LONG ImageLoadingCount;
494 } MM_SESSION_SPACE, *PMM_SESSION_SPACE;
495
496 extern PMM_SESSION_SPACE MmSessionSpace;
497 extern MMPTE HyperTemplatePte;
498 extern MMPDE ValidKernelPde;
499 extern MMPTE ValidKernelPte;
500 extern MMPDE ValidKernelPdeLocal;
501 extern MMPTE ValidKernelPteLocal;
502 extern MMPDE DemandZeroPde;
503 extern MMPTE DemandZeroPte;
504 extern MMPTE PrototypePte;
505 extern MMPTE MmDecommittedPte;
506 extern BOOLEAN MmLargeSystemCache;
507 extern BOOLEAN MmZeroPageFile;
508 extern BOOLEAN MmProtectFreedNonPagedPool;
509 extern BOOLEAN MmTrackLockedPages;
510 extern BOOLEAN MmTrackPtes;
511 extern BOOLEAN MmDynamicPfn;
512 extern BOOLEAN MmMirroring;
513 extern BOOLEAN MmMakeLowMemory;
514 extern BOOLEAN MmEnforceWriteProtection;
515 extern SIZE_T MmAllocationFragment;
516 extern ULONG MmConsumedPoolPercentage;
517 extern ULONG MmVerifyDriverBufferType;
518 extern ULONG MmVerifyDriverLevel;
519 extern WCHAR MmVerifyDriverBuffer[512];
520 extern WCHAR MmLargePageDriverBuffer[512];
521 extern LIST_ENTRY MiLargePageDriverList;
522 extern BOOLEAN MiLargePageAllDrivers;
523 extern ULONG MmVerifyDriverBufferLength;
524 extern ULONG MmLargePageDriverBufferLength;
525 extern SIZE_T MmSizeOfNonPagedPoolInBytes;
526 extern SIZE_T MmMaximumNonPagedPoolInBytes;
527 extern PFN_NUMBER MmMaximumNonPagedPoolInPages;
528 extern PFN_NUMBER MmSizeOfPagedPoolInPages;
529 extern PVOID MmNonPagedSystemStart;
530 extern SIZE_T MiNonPagedSystemSize;
531 extern PVOID MmNonPagedPoolStart;
532 extern PVOID MmNonPagedPoolExpansionStart;
533 extern PVOID MmNonPagedPoolEnd;
534 extern SIZE_T MmSizeOfPagedPoolInBytes;
535 extern PVOID MmPagedPoolStart;
536 extern PVOID MmPagedPoolEnd;
537 extern PVOID MmSessionBase;
538 extern SIZE_T MmSessionSize;
539 extern PMMPTE MmFirstReservedMappingPte, MmLastReservedMappingPte;
540 extern PMMPTE MiFirstReservedZeroingPte;
541 extern MI_PFN_CACHE_ATTRIBUTE MiPlatformCacheAttributes[2][MmMaximumCacheType];
542 extern PPHYSICAL_MEMORY_DESCRIPTOR MmPhysicalMemoryBlock;
543 extern SIZE_T MmBootImageSize;
544 extern PMMPTE MmSystemPtesStart[MaximumPtePoolTypes];
545 extern PMMPTE MmSystemPtesEnd[MaximumPtePoolTypes];
546 extern PMEMORY_ALLOCATION_DESCRIPTOR MxFreeDescriptor;
547 extern MEMORY_ALLOCATION_DESCRIPTOR MxOldFreeDescriptor;
548 extern ULONG_PTR MxPfnAllocation;
549 extern MM_PAGED_POOL_INFO MmPagedPoolInfo;
550 extern RTL_BITMAP MiPfnBitMap;
551 extern KGUARDED_MUTEX MmPagedPoolMutex;
552 extern KGUARDED_MUTEX MmSectionCommitMutex;
553 extern PVOID MmPagedPoolStart;
554 extern PVOID MmPagedPoolEnd;
555 extern PVOID MmNonPagedSystemStart;
556 extern PVOID MiSystemViewStart;
557 extern SIZE_T MmSystemViewSize;
558 extern PVOID MmSessionBase;
559 extern PVOID MiSessionSpaceEnd;
560 extern PMMPTE MiSessionImagePteStart;
561 extern PMMPTE MiSessionImagePteEnd;
562 extern PMMPTE MiSessionBasePte;
563 extern PMMPTE MiSessionLastPte;
564 extern SIZE_T MmSizeOfPagedPoolInBytes;
565 extern PMMPDE MmSystemPagePtes;
566 extern PVOID MmSystemCacheStart;
567 extern PVOID MmSystemCacheEnd;
568 extern MMSUPPORT MmSystemCacheWs;
569 extern SIZE_T MmAllocatedNonPagedPool;
570 extern ULONG MmSpecialPoolTag;
571 extern PVOID MmHyperSpaceEnd;
572 extern PMMWSL MmSystemCacheWorkingSetList;
573 extern SIZE_T MmMinimumNonPagedPoolSize;
574 extern ULONG MmMinAdditionNonPagedPoolPerMb;
575 extern SIZE_T MmDefaultMaximumNonPagedPool;
576 extern ULONG MmMaxAdditionNonPagedPoolPerMb;
577 extern ULONG MmSecondaryColors;
578 extern ULONG MmSecondaryColorMask;
579 extern ULONG MmNumberOfSystemPtes;
580 extern ULONG MmMaximumNonPagedPoolPercent;
581 extern ULONG MmLargeStackSize;
582 extern PMMCOLOR_TABLES MmFreePagesByColor[FreePageList + 1];
583 extern MMPFNLIST MmStandbyPageListByPriority[8];
584 extern ULONG MmProductType;
585 extern MM_SYSTEMSIZE MmSystemSize;
586 extern PKEVENT MiLowMemoryEvent;
587 extern PKEVENT MiHighMemoryEvent;
588 extern PKEVENT MiLowPagedPoolEvent;
589 extern PKEVENT MiHighPagedPoolEvent;
590 extern PKEVENT MiLowNonPagedPoolEvent;
591 extern PKEVENT MiHighNonPagedPoolEvent;
592 extern PFN_NUMBER MmLowMemoryThreshold;
593 extern PFN_NUMBER MmHighMemoryThreshold;
594 extern PFN_NUMBER MiLowPagedPoolThreshold;
595 extern PFN_NUMBER MiHighPagedPoolThreshold;
596 extern PFN_NUMBER MiLowNonPagedPoolThreshold;
597 extern PFN_NUMBER MiHighNonPagedPoolThreshold;
598 extern PFN_NUMBER MmMinimumFreePages;
599 extern PFN_NUMBER MmPlentyFreePages;
600 extern SIZE_T MmMinimumStackCommitInBytes;
601 extern PFN_COUNT MiExpansionPoolPagesInitialCharge;
602 extern PFN_NUMBER MmResidentAvailablePages;
603 extern PFN_NUMBER MmResidentAvailableAtInit;
604 extern ULONG MmTotalFreeSystemPtes[MaximumPtePoolTypes];
605 extern PFN_NUMBER MmTotalSystemDriverPages;
606 extern ULONG MmCritsectTimeoutSeconds;
607 extern PVOID MiSessionImageStart;
608 extern PVOID MiSessionImageEnd;
609 extern PMMPTE MiHighestUserPte;
610 extern PMMPDE MiHighestUserPde;
611 extern PFN_NUMBER MmSystemPageDirectory[PD_COUNT];
612 extern PMMPTE MmSharedUserDataPte;
613 extern LIST_ENTRY MmProcessList;
614 extern BOOLEAN MmZeroingPageThreadActive;
615 extern KEVENT MmZeroingPageEvent;
616 extern ULONG MmSystemPageColor;
617 extern ULONG MmProcessColorSeed;
618 extern PMMWSL MmWorkingSetList;
619 extern PFN_NUMBER MiNumberOfFreePages;
620 extern SIZE_T MmSessionViewSize;
621 extern SIZE_T MmSessionPoolSize;
622 extern SIZE_T MmSessionImageSize;
623 extern PVOID MiSystemViewStart;
624 extern PVOID MiSessionPoolEnd; // 0xBE000000
625 extern PVOID MiSessionPoolStart; // 0xBD000000
626 extern PVOID MiSessionViewStart; // 0xBE000000
627 extern PVOID MiSessionSpaceWs;
628 extern ULONG MmMaximumDeadKernelStacks;
629 extern SLIST_HEADER MmDeadStackSListHead;
630 extern MM_AVL_TABLE MmSectionBasedRoot;
631 extern KGUARDED_MUTEX MmSectionBasedMutex;
632 extern PVOID MmHighSectionBase;
633 extern SIZE_T MmSystemLockPagesCount;
634 extern ULONG_PTR MmSubsectionBase;
635 extern LARGE_INTEGER MmCriticalSectionTimeout;
636 extern LIST_ENTRY MmWorkingSetExpansionHead;
637
638 FORCEINLINE
639 BOOLEAN
640 MiIsMemoryTypeFree(TYPE_OF_MEMORY MemoryType)
641 {
642 return ((MemoryType == LoaderFree) ||
643 (MemoryType == LoaderLoadedProgram) ||
644 (MemoryType == LoaderFirmwareTemporary) ||
645 (MemoryType == LoaderOsloaderStack));
646 }
647
648 FORCEINLINE
649 BOOLEAN
650 MiIsMemoryTypeInvisible(TYPE_OF_MEMORY MemoryType)
651 {
652 return ((MemoryType == LoaderFirmwarePermanent) ||
653 (MemoryType == LoaderSpecialMemory) ||
654 (MemoryType == LoaderHALCachedMemory) ||
655 (MemoryType == LoaderBBTMemory));
656 }
657
658 #ifdef _M_AMD64
659 FORCEINLINE
660 BOOLEAN
661 MiIsUserPxe(PVOID Address)
662 {
663 return ((ULONG_PTR)Address >> 7) == 0x1FFFFEDF6FB7DA0ULL;
664 }
665
666 FORCEINLINE
667 BOOLEAN
668 MiIsUserPpe(PVOID Address)
669 {
670 return ((ULONG_PTR)Address >> 16) == 0xFFFFF6FB7DA0ULL;
671 }
672
673 FORCEINLINE
674 BOOLEAN
675 MiIsUserPde(PVOID Address)
676 {
677 return ((ULONG_PTR)Address >> 25) == 0x7FFFFB7DA0ULL;
678 }
679
680 FORCEINLINE
681 BOOLEAN
682 MiIsUserPte(PVOID Address)
683 {
684 return ((ULONG_PTR)Address >> 34) == 0x3FFFFDA0ULL;
685 }
686 #else
687 FORCEINLINE
688 BOOLEAN
689 MiIsUserPde(PVOID Address)
690 {
691 return ((Address >= (PVOID)MiAddressToPde(NULL)) &&
692 (Address <= (PVOID)MiHighestUserPde));
693 }
694
695 FORCEINLINE
696 BOOLEAN
697 MiIsUserPte(PVOID Address)
698 {
699 return (Address <= (PVOID)MiHighestUserPte);
700 }
701 #endif
702
703 //
704 // Figures out the hardware bits for a PTE
705 //
706 FORCEINLINE
707 ULONG_PTR
708 MiDetermineUserGlobalPteMask(IN PVOID PointerPte)
709 {
710 MMPTE TempPte;
711
712 /* Start fresh */
713 TempPte.u.Long = 0;
714
715 /* Make it valid and accessed */
716 TempPte.u.Hard.Valid = TRUE;
717 MI_MAKE_ACCESSED_PAGE(&TempPte);
718
719 /* Is this for user-mode? */
720 if (
721 #if (_MI_PAGING_LEVELS == 4)
722 MiIsUserPxe(PointerPte) ||
723 #endif
724 #if (_MI_PAGING_LEVELS >= 3)
725 MiIsUserPpe(PointerPte) ||
726 #endif
727 MiIsUserPde(PointerPte) ||
728 MiIsUserPte(PointerPte))
729 {
730 /* Set the owner bit */
731 MI_MAKE_OWNER_PAGE(&TempPte);
732 }
733
734 /* FIXME: We should also set the global bit */
735
736 /* Return the protection */
737 return TempPte.u.Long;
738 }
739
740 //
741 // Creates a valid kernel PTE with the given protection
742 //
743 FORCEINLINE
744 VOID
745 MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte,
746 IN PMMPTE MappingPte,
747 IN ULONG_PTR ProtectionMask,
748 IN PFN_NUMBER PageFrameNumber)
749 {
750 /* Only valid for kernel, non-session PTEs */
751 ASSERT(MappingPte > MiHighestUserPte);
752 ASSERT(!MI_IS_SESSION_PTE(MappingPte));
753 ASSERT((MappingPte < (PMMPTE)PDE_BASE) || (MappingPte > (PMMPTE)PDE_TOP));
754
755 /* Start fresh */
756 *NewPte = ValidKernelPte;
757
758 /* Set the protection and page */
759 NewPte->u.Hard.PageFrameNumber = PageFrameNumber;
760 NewPte->u.Long |= MmProtectToPteMask[ProtectionMask];
761 }
762
763 //
764 // Creates a valid PTE with the given protection
765 //
766 FORCEINLINE
767 VOID
768 MI_MAKE_HARDWARE_PTE(IN PMMPTE NewPte,
769 IN PMMPTE MappingPte,
770 IN ULONG_PTR ProtectionMask,
771 IN PFN_NUMBER PageFrameNumber)
772 {
773 /* Set the protection and page */
774 NewPte->u.Long = MiDetermineUserGlobalPteMask(MappingPte);
775 NewPte->u.Long |= MmProtectToPteMask[ProtectionMask];
776 NewPte->u.Hard.PageFrameNumber = PageFrameNumber;
777 }
778
779 //
780 // Creates a valid user PTE with the given protection
781 //
782 FORCEINLINE
783 VOID
784 MI_MAKE_HARDWARE_PTE_USER(IN PMMPTE NewPte,
785 IN PMMPTE MappingPte,
786 IN ULONG_PTR ProtectionMask,
787 IN PFN_NUMBER PageFrameNumber)
788 {
789 /* Only valid for kernel, non-session PTEs */
790 ASSERT(MappingPte <= MiHighestUserPte);
791
792 /* Start fresh */
793 NewPte->u.Long = 0;
794
795 /* Set the protection and page */
796 NewPte->u.Hard.Valid = TRUE;
797 NewPte->u.Hard.Owner = TRUE;
798 NewPte->u.Hard.PageFrameNumber = PageFrameNumber;
799 NewPte->u.Long |= MmProtectToPteMask[ProtectionMask];
800 }
801
802 #ifndef _M_AMD64
803 //
804 // Builds a Prototype PTE for the address of the PTE
805 //
806 FORCEINLINE
807 VOID
808 MI_MAKE_PROTOTYPE_PTE(IN PMMPTE NewPte,
809 IN PMMPTE PointerPte)
810 {
811 ULONG_PTR Offset;
812
813 /* Mark this as a prototype */
814 NewPte->u.Long = 0;
815 NewPte->u.Proto.Prototype = 1;
816
817 /*
818 * Prototype PTEs are only valid in paged pool by design, this little trick
819 * lets us only use 30 bits for the adress of the PTE, as long as the area
820 * stays 1024MB At most.
821 */
822 Offset = (ULONG_PTR)PointerPte - (ULONG_PTR)MmPagedPoolStart;
823
824 /*
825 * 7 bits go in the "low" (but we assume the bottom 2 are zero)
826 * and the other 21 bits go in the "high"
827 */
828 NewPte->u.Proto.ProtoAddressLow = (Offset & 0x1FC) >> 2;
829 NewPte->u.Proto.ProtoAddressHigh = (Offset & 0x3FFFFE00) >> 9;
830 }
831
832 //
833 // Builds a Subsection PTE for the address of the Segment
834 //
835 FORCEINLINE
836 VOID
837 MI_MAKE_SUBSECTION_PTE(IN PMMPTE NewPte,
838 IN PVOID Segment)
839 {
840 ULONG_PTR Offset;
841
842 /* Mark this as a prototype */
843 NewPte->u.Long = 0;
844 NewPte->u.Subsect.Prototype = 1;
845
846 /*
847 * Segments are only valid either in nonpaged pool. We store the 20 bit
848 * difference either from the top or bottom of nonpaged pool, giving a
849 * maximum of 128MB to each delta, meaning nonpaged pool cannot exceed
850 * 256MB.
851 */
852 if ((ULONG_PTR)Segment < ((ULONG_PTR)MmSubsectionBase + (128 * _1MB)))
853 {
854 Offset = (ULONG_PTR)Segment - (ULONG_PTR)MmSubsectionBase;
855 NewPte->u.Subsect.WhichPool = PagedPool;
856 }
857 else
858 {
859 Offset = (ULONG_PTR)MmNonPagedPoolEnd - (ULONG_PTR)Segment;
860 NewPte->u.Subsect.WhichPool = NonPagedPool;
861 }
862
863 /*
864 * 4 bits go in the "low" (but we assume the bottom 3 are zero)
865 * and the other 20 bits go in the "high"
866 */
867 NewPte->u.Subsect.SubsectionAddressLow = (Offset & 0x78) >> 3;
868 NewPte->u.Subsect.SubsectionAddressHigh = (Offset & 0xFFFFF80) >> 7;
869 }
870
871 FORCEINLINE
872 BOOLEAN
873 MI_IS_MAPPED_PTE(PMMPTE PointerPte)
874 {
875 /// \todo Make this reasonable code, this is UGLY!
876 return ((PointerPte->u.Long & 0xFFFFFC01) != 0);
877 }
878
879 #endif
880
881 //
882 // Returns if the page is physically resident (ie: a large page)
883 // FIXFIX: CISC/x86 only?
884 //
885 FORCEINLINE
886 BOOLEAN
887 MI_IS_PHYSICAL_ADDRESS(IN PVOID Address)
888 {
889 PMMPDE PointerPde;
890
891 /* Large pages are never paged out, always physically resident */
892 PointerPde = MiAddressToPde(Address);
893 return ((PointerPde->u.Hard.LargePage) && (PointerPde->u.Hard.Valid));
894 }
895
896 //
897 // Writes a valid PTE
898 //
899 FORCEINLINE
900 VOID
901 MI_WRITE_VALID_PTE(IN PMMPTE PointerPte,
902 IN MMPTE TempPte)
903 {
904 /* Write the valid PTE */
905 ASSERT(PointerPte->u.Hard.Valid == 0);
906 ASSERT(TempPte.u.Hard.Valid == 1);
907 *PointerPte = TempPte;
908 }
909
910 //
911 // Updates a valid PTE
912 //
913 FORCEINLINE
914 VOID
915 MI_UPDATE_VALID_PTE(IN PMMPTE PointerPte,
916 IN MMPTE TempPte)
917 {
918 /* Write the valid PTE */
919 ASSERT(PointerPte->u.Hard.Valid == 1);
920 ASSERT(TempPte.u.Hard.Valid == 1);
921 ASSERT(PointerPte->u.Hard.PageFrameNumber == TempPte.u.Hard.PageFrameNumber);
922 *PointerPte = TempPte;
923 }
924
925 //
926 // Writes an invalid PTE
927 //
928 FORCEINLINE
929 VOID
930 MI_WRITE_INVALID_PTE(IN PMMPTE PointerPte,
931 IN MMPTE InvalidPte)
932 {
933 /* Write the invalid PTE */
934 ASSERT(InvalidPte.u.Hard.Valid == 0);
935 ASSERT(InvalidPte.u.Long != 0);
936 *PointerPte = InvalidPte;
937 }
938
939 //
940 // Erase the PTE completely
941 //
942 FORCEINLINE
943 VOID
944 MI_ERASE_PTE(IN PMMPTE PointerPte)
945 {
946 /* Zero out the PTE */
947 ASSERT(PointerPte->u.Long != 0);
948 PointerPte->u.Long = 0;
949 }
950
951 //
952 // Writes a valid PDE
953 //
954 FORCEINLINE
955 VOID
956 MI_WRITE_VALID_PDE(IN PMMPDE PointerPde,
957 IN MMPDE TempPde)
958 {
959 /* Write the valid PDE */
960 ASSERT(PointerPde->u.Hard.Valid == 0);
961 ASSERT(TempPde.u.Hard.Valid == 1);
962 *PointerPde = TempPde;
963 }
964
965 //
966 // Writes an invalid PDE
967 //
968 FORCEINLINE
969 VOID
970 MI_WRITE_INVALID_PDE(IN PMMPDE PointerPde,
971 IN MMPDE InvalidPde)
972 {
973 /* Write the invalid PDE */
974 ASSERT(InvalidPde.u.Hard.Valid == 0);
975 ASSERT(InvalidPde.u.Long != 0);
976 *PointerPde = InvalidPde;
977 }
978
979 //
980 // Checks if the thread already owns a working set
981 //
982 FORCEINLINE
983 BOOLEAN
984 MM_ANY_WS_LOCK_HELD(IN PETHREAD Thread)
985 {
986 /* If any of these are held, return TRUE */
987 return ((Thread->OwnsProcessWorkingSetExclusive) ||
988 (Thread->OwnsProcessWorkingSetShared) ||
989 (Thread->OwnsSystemWorkingSetExclusive) ||
990 (Thread->OwnsSystemWorkingSetShared) ||
991 (Thread->OwnsSessionWorkingSetExclusive) ||
992 (Thread->OwnsSessionWorkingSetShared));
993 }
994
995 //
996 // Checks if the process owns the working set lock
997 //
998 FORCEINLINE
999 BOOLEAN
1000 MI_WS_OWNER(IN PEPROCESS Process)
1001 {
1002 /* Check if this process is the owner, and that the thread owns the WS */
1003 if (PsGetCurrentThread()->OwnsProcessWorkingSetExclusive == 0)
1004 {
1005 DPRINT("Thread: %p is not an owner\n", PsGetCurrentThread());
1006 }
1007 if (KeGetCurrentThread()->ApcState.Process != &Process->Pcb)
1008 {
1009 DPRINT("Current thread %p is attached to another process %p\n", PsGetCurrentThread(), Process);
1010 }
1011 return ((KeGetCurrentThread()->ApcState.Process == &Process->Pcb) &&
1012 ((PsGetCurrentThread()->OwnsProcessWorkingSetExclusive) ||
1013 (PsGetCurrentThread()->OwnsProcessWorkingSetShared)));
1014 }
1015
1016 //
1017 // New ARM3<->RosMM PAGE Architecture
1018 //
1019 FORCEINLINE
1020 BOOLEAN
1021 MiIsRosSectionObject(IN PVOID Section)
1022 {
1023 PROS_SECTION_OBJECT RosSection = Section;
1024 if ((RosSection->Type == 'SC') && (RosSection->Size == 'TN')) return TRUE;
1025 return FALSE;
1026 }
1027
1028 #define MI_IS_ROS_PFN(x) ((x)->u4.AweAllocation == TRUE)
1029
1030 VOID
1031 NTAPI
1032 MiDecrementReferenceCount(
1033 IN PMMPFN Pfn1,
1034 IN PFN_NUMBER PageFrameIndex
1035 );
1036
1037 FORCEINLINE
1038 BOOLEAN
1039 MI_IS_WS_UNSAFE(IN PEPROCESS Process)
1040 {
1041 return (Process->Vm.Flags.AcquiredUnsafe == TRUE);
1042 }
1043
1044 //
1045 // Locks the working set for the given process
1046 //
1047 FORCEINLINE
1048 VOID
1049 MiLockProcessWorkingSet(IN PEPROCESS Process,
1050 IN PETHREAD Thread)
1051 {
1052 /* Shouldn't already be owning the process working set */
1053 ASSERT(Thread->OwnsProcessWorkingSetShared == FALSE);
1054 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1055
1056 /* Block APCs, make sure that still nothing is already held */
1057 KeEnterGuardedRegion();
1058 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread));
1059
1060 /* Lock the working set */
1061 ExAcquirePushLockExclusive(&Process->Vm.WorkingSetMutex);
1062
1063 /* Now claim that we own the lock */
1064 ASSERT(!MI_IS_WS_UNSAFE(Process));
1065 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1066 Thread->OwnsProcessWorkingSetExclusive = TRUE;
1067 }
1068
1069 FORCEINLINE
1070 VOID
1071 MiLockProcessWorkingSetShared(IN PEPROCESS Process,
1072 IN PETHREAD Thread)
1073 {
1074 /* Shouldn't already be owning the process working set */
1075 ASSERT(Thread->OwnsProcessWorkingSetShared == FALSE);
1076 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1077
1078 /* Block APCs, make sure that still nothing is already held */
1079 KeEnterGuardedRegion();
1080 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread));
1081
1082 /* Lock the working set */
1083 ExAcquirePushLockShared(&Process->Vm.WorkingSetMutex);
1084
1085 /* Now claim that we own the lock */
1086 ASSERT(!MI_IS_WS_UNSAFE(Process));
1087 ASSERT(Thread->OwnsProcessWorkingSetShared == FALSE);
1088 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1089 Thread->OwnsProcessWorkingSetShared = TRUE;
1090 }
1091
1092 FORCEINLINE
1093 VOID
1094 MiLockProcessWorkingSetUnsafe(IN PEPROCESS Process,
1095 IN PETHREAD Thread)
1096 {
1097 /* Shouldn't already be owning the process working set */
1098 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1099
1100 /* APCs must be blocked, make sure that still nothing is already held */
1101 ASSERT(KeAreAllApcsDisabled() == TRUE);
1102 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread));
1103
1104 /* Lock the working set */
1105 ExAcquirePushLockExclusive(&Process->Vm.WorkingSetMutex);
1106
1107 /* Now claim that we own the lock */
1108 ASSERT(!MI_IS_WS_UNSAFE(Process));
1109 Process->Vm.Flags.AcquiredUnsafe = 1;
1110 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1111 Thread->OwnsProcessWorkingSetExclusive = TRUE;
1112 }
1113
1114 //
1115 // Unlocks the working set for the given process
1116 //
1117 FORCEINLINE
1118 VOID
1119 MiUnlockProcessWorkingSet(IN PEPROCESS Process,
1120 IN PETHREAD Thread)
1121 {
1122 /* Make sure we are the owner of a safe acquisition */
1123 ASSERT(MI_WS_OWNER(Process));
1124 ASSERT(!MI_IS_WS_UNSAFE(Process));
1125
1126 /* The thread doesn't own it anymore */
1127 ASSERT(Thread->OwnsProcessWorkingSetExclusive == TRUE);
1128 Thread->OwnsProcessWorkingSetExclusive = FALSE;
1129
1130 /* Release the lock and re-enable APCs */
1131 ExReleasePushLockExclusive(&Process->Vm.WorkingSetMutex);
1132 KeLeaveGuardedRegion();
1133 }
1134
1135 //
1136 // Unlocks the working set for the given process
1137 //
1138 FORCEINLINE
1139 VOID
1140 MiUnlockProcessWorkingSetShared(IN PEPROCESS Process,
1141 IN PETHREAD Thread)
1142 {
1143 /* Make sure we are the owner of a safe acquisition (because shared) */
1144 ASSERT(MI_WS_OWNER(Process));
1145 ASSERT(!MI_IS_WS_UNSAFE(Process));
1146
1147 /* Ensure we are in a shared acquisition */
1148 ASSERT(Thread->OwnsProcessWorkingSetShared == TRUE);
1149 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1150
1151 /* Don't claim the lock anylonger */
1152 Thread->OwnsProcessWorkingSetShared = FALSE;
1153
1154 /* Release the lock and re-enable APCs */
1155 ExReleasePushLockShared(&Process->Vm.WorkingSetMutex);
1156 KeLeaveGuardedRegion();
1157 }
1158
1159 //
1160 // Unlocks the working set for the given process
1161 //
1162 FORCEINLINE
1163 VOID
1164 MiUnlockProcessWorkingSetUnsafe(IN PEPROCESS Process,
1165 IN PETHREAD Thread)
1166 {
1167 /* Make sure we are the owner of an unsafe acquisition */
1168 ASSERT(KeGetCurrentIrql() <= APC_LEVEL);
1169 ASSERT(KeAreAllApcsDisabled() == TRUE);
1170 ASSERT(MI_WS_OWNER(Process));
1171 ASSERT(MI_IS_WS_UNSAFE(Process));
1172
1173 /* No longer unsafe */
1174 Process->Vm.Flags.AcquiredUnsafe = 0;
1175
1176 /* The thread doesn't own it anymore */
1177 ASSERT(Thread->OwnsProcessWorkingSetExclusive == TRUE);
1178 Thread->OwnsProcessWorkingSetExclusive = FALSE;
1179
1180 /* Release the lock but don't touch APC state */
1181 ExReleasePushLockExclusive(&Process->Vm.WorkingSetMutex);
1182 ASSERT(KeGetCurrentIrql() <= APC_LEVEL);
1183 }
1184
1185 //
1186 // Locks the working set
1187 //
1188 FORCEINLINE
1189 VOID
1190 MiLockWorkingSet(IN PETHREAD Thread,
1191 IN PMMSUPPORT WorkingSet)
1192 {
1193 /* Block APCs */
1194 KeEnterGuardedRegion();
1195
1196 /* Working set should be in global memory */
1197 ASSERT(MI_IS_SESSION_ADDRESS((PVOID)WorkingSet) == FALSE);
1198
1199 /* Thread shouldn't already be owning something */
1200 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread));
1201
1202 /* Lock this working set */
1203 ExAcquirePushLockExclusive(&WorkingSet->WorkingSetMutex);
1204
1205 /* Which working set is this? */
1206 if (WorkingSet == &MmSystemCacheWs)
1207 {
1208 /* Own the system working set */
1209 ASSERT((Thread->OwnsSystemWorkingSetExclusive == FALSE) &&
1210 (Thread->OwnsSystemWorkingSetShared == FALSE));
1211 Thread->OwnsSystemWorkingSetExclusive = TRUE;
1212 }
1213 else if (WorkingSet->Flags.SessionSpace)
1214 {
1215 /* Own the session working set */
1216 ASSERT((Thread->OwnsSessionWorkingSetExclusive == FALSE) &&
1217 (Thread->OwnsSessionWorkingSetShared == FALSE));
1218 Thread->OwnsSessionWorkingSetExclusive = TRUE;
1219 }
1220 else
1221 {
1222 /* Own the process working set */
1223 ASSERT((Thread->OwnsProcessWorkingSetExclusive == FALSE) &&
1224 (Thread->OwnsProcessWorkingSetShared == FALSE));
1225 Thread->OwnsProcessWorkingSetExclusive = TRUE;
1226 }
1227 }
1228
1229 //
1230 // Unlocks the working set
1231 //
1232 FORCEINLINE
1233 VOID
1234 MiUnlockWorkingSet(IN PETHREAD Thread,
1235 IN PMMSUPPORT WorkingSet)
1236 {
1237 /* Working set should be in global memory */
1238 ASSERT(MI_IS_SESSION_ADDRESS((PVOID)WorkingSet) == FALSE);
1239
1240 /* Which working set is this? */
1241 if (WorkingSet == &MmSystemCacheWs)
1242 {
1243 /* Release the system working set */
1244 ASSERT((Thread->OwnsSystemWorkingSetExclusive == TRUE) ||
1245 (Thread->OwnsSystemWorkingSetShared == TRUE));
1246 Thread->OwnsSystemWorkingSetExclusive = FALSE;
1247 }
1248 else if (WorkingSet->Flags.SessionSpace)
1249 {
1250 /* Release the session working set */
1251 ASSERT((Thread->OwnsSessionWorkingSetExclusive == TRUE) ||
1252 (Thread->OwnsSessionWorkingSetShared == TRUE));
1253 Thread->OwnsSessionWorkingSetExclusive = 0;
1254 }
1255 else
1256 {
1257 /* Release the process working set */
1258 ASSERT((Thread->OwnsProcessWorkingSetExclusive) ||
1259 (Thread->OwnsProcessWorkingSetShared));
1260 Thread->OwnsProcessWorkingSetExclusive = FALSE;
1261 }
1262
1263 /* Release the working set lock */
1264 ExReleasePushLockExclusive(&WorkingSet->WorkingSetMutex);
1265
1266 /* Unblock APCs */
1267 KeLeaveGuardedRegion();
1268 }
1269
1270 FORCEINLINE
1271 VOID
1272 MiUnlockProcessWorkingSetForFault(IN PEPROCESS Process,
1273 IN PETHREAD Thread,
1274 OUT PBOOLEAN Safe,
1275 OUT PBOOLEAN Shared)
1276 {
1277 ASSERT(MI_WS_OWNER(Process));
1278
1279 /* Check if the current owner is unsafe */
1280 if (MI_IS_WS_UNSAFE(Process))
1281 {
1282 /* Release unsafely */
1283 MiUnlockProcessWorkingSetUnsafe(Process, Thread);
1284 *Safe = FALSE;
1285 *Shared = FALSE;
1286 }
1287 else if (Thread->OwnsProcessWorkingSetExclusive == 1)
1288 {
1289 /* Owner is safe and exclusive, release normally */
1290 MiUnlockProcessWorkingSet(Process, Thread);
1291 *Safe = TRUE;
1292 *Shared = FALSE;
1293 }
1294 else
1295 {
1296 /* Owner is shared (implies safe), release normally */
1297 MiUnlockProcessWorkingSetShared(Process, Thread);
1298 *Safe = TRUE;
1299 *Shared = TRUE;
1300 }
1301 }
1302
1303 FORCEINLINE
1304 VOID
1305 MiLockProcessWorkingSetForFault(IN PEPROCESS Process,
1306 IN PETHREAD Thread,
1307 IN BOOLEAN Safe,
1308 IN BOOLEAN Shared)
1309 {
1310 /* Check if this was a safe lock or not */
1311 if (Safe)
1312 {
1313 if (Shared)
1314 {
1315 /* Reacquire safely & shared */
1316 MiLockProcessWorkingSetShared(Process, Thread);
1317 }
1318 else
1319 {
1320 /* Reacquire safely */
1321 MiLockProcessWorkingSet(Process, Thread);
1322 }
1323 }
1324 else
1325 {
1326 /* Unsafe lock cannot be shared */
1327 ASSERT(Shared == FALSE);
1328 /* Reacquire unsafely */
1329 MiLockProcessWorkingSetUnsafe(Process, Thread);
1330 }
1331 }
1332
1333 //
1334 // Returns the ProtoPTE inside a VAD for the given VPN
1335 //
1336 FORCEINLINE
1337 PMMPTE
1338 MI_GET_PROTOTYPE_PTE_FOR_VPN(IN PMMVAD Vad,
1339 IN ULONG_PTR Vpn)
1340 {
1341 PMMPTE ProtoPte;
1342
1343 /* Find the offset within the VAD's prototype PTEs */
1344 ProtoPte = Vad->FirstPrototypePte + (Vpn - Vad->StartingVpn);
1345 ASSERT(ProtoPte <= Vad->LastContiguousPte);
1346 return ProtoPte;
1347 }
1348
1349 //
1350 // Returns the PFN Database entry for the given page number
1351 // Warning: This is not necessarily a valid PFN database entry!
1352 //
1353 FORCEINLINE
1354 PMMPFN
1355 MI_PFN_ELEMENT(IN PFN_NUMBER Pfn)
1356 {
1357 /* Get the entry */
1358 return &MmPfnDatabase[Pfn];
1359 };
1360
1361 //
1362 // Drops a locked page without dereferencing it
1363 //
1364 FORCEINLINE
1365 VOID
1366 MiDropLockCount(IN PMMPFN Pfn1)
1367 {
1368 /* This page shouldn't be locked, but it should be valid */
1369 ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
1370 ASSERT(Pfn1->u2.ShareCount == 0);
1371
1372 /* Is this the last reference to the page */
1373 if (Pfn1->u3.e2.ReferenceCount == 1)
1374 {
1375 /* It better not be valid */
1376 ASSERT(Pfn1->u3.e1.PageLocation != ActiveAndValid);
1377
1378 /* Is it a prototype PTE? */
1379 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1380 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1381 {
1382 /* FIXME: We should return commit */
1383 DPRINT1("Not returning commit for prototype PTE\n");
1384 }
1385
1386 /* Update the counter */
1387 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1388 }
1389 }
1390
1391 //
1392 // Drops a locked page and dereferences it
1393 //
1394 FORCEINLINE
1395 VOID
1396 MiDereferencePfnAndDropLockCount(IN PMMPFN Pfn1)
1397 {
1398 USHORT RefCount, OldRefCount;
1399 PFN_NUMBER PageFrameIndex;
1400
1401 /* Loop while we decrement the page successfully */
1402 do
1403 {
1404 /* There should be at least one reference */
1405 OldRefCount = Pfn1->u3.e2.ReferenceCount;
1406 ASSERT(OldRefCount != 0);
1407
1408 /* Are we the last one */
1409 if (OldRefCount == 1)
1410 {
1411 /* The page shoudln't be shared not active at this point */
1412 ASSERT(Pfn1->u3.e2.ReferenceCount == 1);
1413 ASSERT(Pfn1->u3.e1.PageLocation != ActiveAndValid);
1414 ASSERT(Pfn1->u2.ShareCount == 0);
1415
1416 /* Is it a prototype PTE? */
1417 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1418 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1419 {
1420 /* FIXME: We should return commit */
1421 DPRINT1("Not returning commit for prototype PTE\n");
1422 }
1423
1424 /* Update the counter, and drop a reference the long way */
1425 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1426 PageFrameIndex = MiGetPfnEntryIndex(Pfn1);
1427 MiDecrementReferenceCount(Pfn1, PageFrameIndex);
1428 return;
1429 }
1430
1431 /* Drop a reference the short way, and that's it */
1432 RefCount = InterlockedCompareExchange16((PSHORT)&Pfn1->u3.e2.ReferenceCount,
1433 OldRefCount - 1,
1434 OldRefCount);
1435 ASSERT(RefCount != 0);
1436 } while (OldRefCount != RefCount);
1437
1438 /* If we got here, there should be more than one reference */
1439 ASSERT(RefCount > 1);
1440 if (RefCount == 2)
1441 {
1442 /* Is it still being shared? */
1443 if (Pfn1->u2.ShareCount >= 1)
1444 {
1445 /* Then it should be valid */
1446 ASSERT(Pfn1->u3.e1.PageLocation == ActiveAndValid);
1447
1448 /* Is it a prototype PTE? */
1449 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1450 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1451 {
1452 /* We don't handle ethis */
1453 ASSERT(FALSE);
1454 }
1455
1456 /* Update the counter */
1457 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1458 }
1459 }
1460 }
1461
1462 //
1463 // References a locked page and updates the counter
1464 // Used in MmProbeAndLockPages to handle different edge cases
1465 //
1466 FORCEINLINE
1467 VOID
1468 MiReferenceProbedPageAndBumpLockCount(IN PMMPFN Pfn1)
1469 {
1470 USHORT RefCount, OldRefCount;
1471
1472 /* Sanity check */
1473 ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
1474
1475 /* Does ARM3 own the page? */
1476 if (MI_IS_ROS_PFN(Pfn1))
1477 {
1478 /* ReactOS Mm doesn't track share count */
1479 ASSERT(Pfn1->u3.e1.PageLocation == ActiveAndValid);
1480 }
1481 else
1482 {
1483 /* On ARM3 pages, we should see a valid share count */
1484 ASSERT((Pfn1->u2.ShareCount != 0) && (Pfn1->u3.e1.PageLocation == ActiveAndValid));
1485
1486 /* Is it a prototype PTE? */
1487 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1488 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1489 {
1490 /* FIXME: We should charge commit */
1491 DPRINT1("Not charging commit for prototype PTE\n");
1492 }
1493 }
1494
1495 /* More locked pages! */
1496 InterlockedIncrementSizeT(&MmSystemLockPagesCount);
1497
1498 /* Loop trying to update the reference count */
1499 do
1500 {
1501 /* Get the current reference count, make sure it's valid */
1502 OldRefCount = Pfn1->u3.e2.ReferenceCount;
1503 ASSERT(OldRefCount != 0);
1504 ASSERT(OldRefCount < 2500);
1505
1506 /* Bump it up by one */
1507 RefCount = InterlockedCompareExchange16((PSHORT)&Pfn1->u3.e2.ReferenceCount,
1508 OldRefCount + 1,
1509 OldRefCount);
1510 ASSERT(RefCount != 0);
1511 } while (OldRefCount != RefCount);
1512
1513 /* Was this the first lock attempt? If not, undo our bump */
1514 if (OldRefCount != 1) InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1515 }
1516
1517 //
1518 // References a locked page and updates the counter
1519 // Used in all other cases except MmProbeAndLockPages
1520 //
1521 FORCEINLINE
1522 VOID
1523 MiReferenceUsedPageAndBumpLockCount(IN PMMPFN Pfn1)
1524 {
1525 USHORT NewRefCount;
1526
1527 /* Is it a prototype PTE? */
1528 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1529 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1530 {
1531 /* FIXME: We should charge commit */
1532 DPRINT1("Not charging commit for prototype PTE\n");
1533 }
1534
1535 /* More locked pages! */
1536 InterlockedIncrementSizeT(&MmSystemLockPagesCount);
1537
1538 /* Update the reference count */
1539 NewRefCount = InterlockedIncrement16((PSHORT)&Pfn1->u3.e2.ReferenceCount);
1540 if (NewRefCount == 2)
1541 {
1542 /* Is it locked or shared? */
1543 if (Pfn1->u2.ShareCount)
1544 {
1545 /* It's shared, so make sure it's active */
1546 ASSERT(Pfn1->u3.e1.PageLocation == ActiveAndValid);
1547 }
1548 else
1549 {
1550 /* It's locked, so we shouldn't lock again */
1551 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1552 }
1553 }
1554 else
1555 {
1556 /* Someone had already locked the page, so undo our bump */
1557 ASSERT(NewRefCount < 2500);
1558 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1559 }
1560 }
1561
1562 //
1563 // References a locked page and updates the counter
1564 // Used in all other cases except MmProbeAndLockPages
1565 //
1566 FORCEINLINE
1567 VOID
1568 MiReferenceUnusedPageAndBumpLockCount(IN PMMPFN Pfn1)
1569 {
1570 USHORT NewRefCount;
1571
1572 /* Make sure the page isn't used yet */
1573 ASSERT(Pfn1->u2.ShareCount == 0);
1574 ASSERT(Pfn1->u3.e1.PageLocation != ActiveAndValid);
1575
1576 /* Is it a prototype PTE? */
1577 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1578 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1579 {
1580 /* FIXME: We should charge commit */
1581 DPRINT1("Not charging commit for prototype PTE\n");
1582 }
1583
1584 /* More locked pages! */
1585 InterlockedIncrementSizeT(&MmSystemLockPagesCount);
1586
1587 /* Update the reference count */
1588 NewRefCount = InterlockedIncrement16((PSHORT)&Pfn1->u3.e2.ReferenceCount);
1589 if (NewRefCount != 1)
1590 {
1591 /* Someone had already locked the page, so undo our bump */
1592 ASSERT(NewRefCount < 2500);
1593 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1594 }
1595 }
1596
1597 FORCEINLINE
1598 VOID
1599 MiIncrementPageTableReferences(IN PVOID Address)
1600 {
1601 PUSHORT RefCount;
1602
1603 RefCount = &MmWorkingSetList->UsedPageTableEntries[MiGetPdeOffset(Address)];
1604
1605 *RefCount += 1;
1606 ASSERT(*RefCount <= PTE_PER_PAGE);
1607 }
1608
1609 FORCEINLINE
1610 VOID
1611 MiDecrementPageTableReferences(IN PVOID Address)
1612 {
1613 PUSHORT RefCount;
1614
1615 RefCount = &MmWorkingSetList->UsedPageTableEntries[MiGetPdeOffset(Address)];
1616
1617 *RefCount -= 1;
1618 ASSERT(*RefCount < PTE_PER_PAGE);
1619 }
1620
1621 FORCEINLINE
1622 USHORT
1623 MiQueryPageTableReferences(IN PVOID Address)
1624 {
1625 PUSHORT RefCount;
1626
1627 RefCount = &MmWorkingSetList->UsedPageTableEntries[MiGetPdeOffset(Address)];
1628
1629 return *RefCount;
1630 }
1631
1632 BOOLEAN
1633 NTAPI
1634 MmArmInitSystem(
1635 IN ULONG Phase,
1636 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1637 );
1638
1639 VOID
1640 NTAPI
1641 MiInitializeSessionSpaceLayout();
1642
1643 NTSTATUS
1644 NTAPI
1645 MiInitMachineDependent(
1646 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1647 );
1648
1649 VOID
1650 NTAPI
1651 MiComputeColorInformation(
1652 VOID
1653 );
1654
1655 VOID
1656 NTAPI
1657 MiMapPfnDatabase(
1658 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1659 );
1660
1661 VOID
1662 NTAPI
1663 MiInitializeColorTables(
1664 VOID
1665 );
1666
1667 VOID
1668 NTAPI
1669 MiInitializePfnDatabase(
1670 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1671 );
1672
1673 VOID
1674 NTAPI
1675 MiInitializeSessionWsSupport(
1676 VOID
1677 );
1678
1679 VOID
1680 NTAPI
1681 MiInitializeSessionIds(
1682 VOID
1683 );
1684
1685 BOOLEAN
1686 NTAPI
1687 MiInitializeMemoryEvents(
1688 VOID
1689 );
1690
1691 PFN_NUMBER
1692 NTAPI
1693 MxGetNextPage(
1694 IN PFN_NUMBER PageCount
1695 );
1696
1697 PPHYSICAL_MEMORY_DESCRIPTOR
1698 NTAPI
1699 MmInitializeMemoryLimits(
1700 IN PLOADER_PARAMETER_BLOCK LoaderBlock,
1701 IN PBOOLEAN IncludeType
1702 );
1703
1704 PFN_NUMBER
1705 NTAPI
1706 MiPagesInLoaderBlock(
1707 IN PLOADER_PARAMETER_BLOCK LoaderBlock,
1708 IN PBOOLEAN IncludeType
1709 );
1710
1711 VOID
1712 FASTCALL
1713 MiSyncARM3WithROS(
1714 IN PVOID AddressStart,
1715 IN PVOID AddressEnd
1716 );
1717
1718 NTSTATUS
1719 NTAPI
1720 MiRosProtectVirtualMemory(
1721 IN PEPROCESS Process,
1722 IN OUT PVOID *BaseAddress,
1723 IN OUT PSIZE_T NumberOfBytesToProtect,
1724 IN ULONG NewAccessProtection,
1725 OUT PULONG OldAccessProtection OPTIONAL
1726 );
1727
1728 NTSTATUS
1729 NTAPI
1730 MmArmAccessFault(
1731 IN BOOLEAN StoreInstruction,
1732 IN PVOID Address,
1733 IN KPROCESSOR_MODE Mode,
1734 IN PVOID TrapInformation
1735 );
1736
1737 NTSTATUS
1738 FASTCALL
1739 MiCheckPdeForPagedPool(
1740 IN PVOID Address
1741 );
1742
1743 VOID
1744 NTAPI
1745 MiInitializeNonPagedPool(
1746 VOID
1747 );
1748
1749 VOID
1750 NTAPI
1751 MiInitializeNonPagedPoolThresholds(
1752 VOID
1753 );
1754
1755 VOID
1756 NTAPI
1757 MiInitializePoolEvents(
1758 VOID
1759 );
1760
1761 VOID //
1762 NTAPI //
1763 InitializePool( //
1764 IN POOL_TYPE PoolType,// FIXFIX: This should go in ex.h after the pool merge
1765 IN ULONG Threshold //
1766 ); //
1767
1768 // FIXFIX: THIS ONE TOO
1769 VOID
1770 NTAPI
1771 INIT_FUNCTION
1772 ExInitializePoolDescriptor(
1773 IN PPOOL_DESCRIPTOR PoolDescriptor,
1774 IN POOL_TYPE PoolType,
1775 IN ULONG PoolIndex,
1776 IN ULONG Threshold,
1777 IN PVOID PoolLock
1778 );
1779
1780 NTSTATUS
1781 NTAPI
1782 MiInitializeSessionPool(
1783 VOID
1784 );
1785
1786 VOID
1787 NTAPI
1788 MiInitializeSystemPtes(
1789 IN PMMPTE StartingPte,
1790 IN ULONG NumberOfPtes,
1791 IN MMSYSTEM_PTE_POOL_TYPE PoolType
1792 );
1793
1794 PMMPTE
1795 NTAPI
1796 MiReserveSystemPtes(
1797 IN ULONG NumberOfPtes,
1798 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1799 );
1800
1801 VOID
1802 NTAPI
1803 MiReleaseSystemPtes(
1804 IN PMMPTE StartingPte,
1805 IN ULONG NumberOfPtes,
1806 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1807 );
1808
1809
1810 PFN_NUMBER
1811 NTAPI
1812 MiFindContiguousPages(
1813 IN PFN_NUMBER LowestPfn,
1814 IN PFN_NUMBER HighestPfn,
1815 IN PFN_NUMBER BoundaryPfn,
1816 IN PFN_NUMBER SizeInPages,
1817 IN MEMORY_CACHING_TYPE CacheType
1818 );
1819
1820 PVOID
1821 NTAPI
1822 MiCheckForContiguousMemory(
1823 IN PVOID BaseAddress,
1824 IN PFN_NUMBER BaseAddressPages,
1825 IN PFN_NUMBER SizeInPages,
1826 IN PFN_NUMBER LowestPfn,
1827 IN PFN_NUMBER HighestPfn,
1828 IN PFN_NUMBER BoundaryPfn,
1829 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
1830 );
1831
1832 PMDL
1833 NTAPI
1834 MiAllocatePagesForMdl(
1835 IN PHYSICAL_ADDRESS LowAddress,
1836 IN PHYSICAL_ADDRESS HighAddress,
1837 IN PHYSICAL_ADDRESS SkipBytes,
1838 IN SIZE_T TotalBytes,
1839 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute,
1840 IN ULONG Flags
1841 );
1842
1843 PVOID
1844 NTAPI
1845 MiMapLockedPagesInUserSpace(
1846 IN PMDL Mdl,
1847 IN PVOID BaseVa,
1848 IN MEMORY_CACHING_TYPE CacheType,
1849 IN PVOID BaseAddress
1850 );
1851
1852 VOID
1853 NTAPI
1854 MiUnmapLockedPagesInUserSpace(
1855 IN PVOID BaseAddress,
1856 IN PMDL Mdl
1857 );
1858
1859 VOID
1860 NTAPI
1861 MiInsertPageInList(
1862 IN PMMPFNLIST ListHead,
1863 IN PFN_NUMBER PageFrameIndex
1864 );
1865
1866 VOID
1867 NTAPI
1868 MiUnlinkFreeOrZeroedPage(
1869 IN PMMPFN Entry
1870 );
1871
1872 VOID
1873 NTAPI
1874 MiUnlinkPageFromList(
1875 IN PMMPFN Pfn
1876 );
1877
1878 VOID
1879 NTAPI
1880 MiInitializePfn(
1881 IN PFN_NUMBER PageFrameIndex,
1882 IN PMMPTE PointerPte,
1883 IN BOOLEAN Modified
1884 );
1885
1886 NTSTATUS
1887 NTAPI
1888 MiInitializeAndChargePfn(
1889 OUT PPFN_NUMBER PageFrameIndex,
1890 IN PMMPTE PointerPde,
1891 IN PFN_NUMBER ContainingPageFrame,
1892 IN BOOLEAN SessionAllocation
1893 );
1894
1895 VOID
1896 NTAPI
1897 MiInitializePfnAndMakePteValid(
1898 IN PFN_NUMBER PageFrameIndex,
1899 IN PMMPTE PointerPte,
1900 IN MMPTE TempPte
1901 );
1902
1903 VOID
1904 NTAPI
1905 MiInitializePfnForOtherProcess(
1906 IN PFN_NUMBER PageFrameIndex,
1907 IN PMMPTE PointerPte,
1908 IN PFN_NUMBER PteFrame
1909 );
1910
1911 VOID
1912 NTAPI
1913 MiDecrementShareCount(
1914 IN PMMPFN Pfn1,
1915 IN PFN_NUMBER PageFrameIndex
1916 );
1917
1918 PFN_NUMBER
1919 NTAPI
1920 MiRemoveAnyPage(
1921 IN ULONG Color
1922 );
1923
1924 PFN_NUMBER
1925 NTAPI
1926 MiRemoveZeroPage(
1927 IN ULONG Color
1928 );
1929
1930 VOID
1931 NTAPI
1932 MiZeroPhysicalPage(
1933 IN PFN_NUMBER PageFrameIndex
1934 );
1935
1936 VOID
1937 NTAPI
1938 MiInsertPageInFreeList(
1939 IN PFN_NUMBER PageFrameIndex
1940 );
1941
1942 PFN_COUNT
1943 NTAPI
1944 MiDeleteSystemPageableVm(
1945 IN PMMPTE PointerPte,
1946 IN PFN_NUMBER PageCount,
1947 IN ULONG Flags,
1948 OUT PPFN_NUMBER ValidPages
1949 );
1950
1951 ULONG
1952 NTAPI
1953 MiGetPageProtection(
1954 IN PMMPTE PointerPte
1955 );
1956
1957 PLDR_DATA_TABLE_ENTRY
1958 NTAPI
1959 MiLookupDataTableEntry(
1960 IN PVOID Address
1961 );
1962
1963 VOID
1964 NTAPI
1965 MiInitializeDriverLargePageList(
1966 VOID
1967 );
1968
1969 VOID
1970 NTAPI
1971 MiInitializeLargePageSupport(
1972 VOID
1973 );
1974
1975 VOID
1976 NTAPI
1977 MiSyncCachedRanges(
1978 VOID
1979 );
1980
1981 BOOLEAN
1982 NTAPI
1983 MiIsPfnInUse(
1984 IN PMMPFN Pfn1
1985 );
1986
1987 PMMVAD
1988 NTAPI
1989 MiLocateAddress(
1990 IN PVOID VirtualAddress
1991 );
1992
1993 TABLE_SEARCH_RESULT
1994 NTAPI
1995 MiCheckForConflictingNode(
1996 IN ULONG_PTR StartVpn,
1997 IN ULONG_PTR EndVpn,
1998 IN PMM_AVL_TABLE Table,
1999 OUT PMMADDRESS_NODE *NodeOrParent
2000 );
2001
2002 TABLE_SEARCH_RESULT
2003 NTAPI
2004 MiFindEmptyAddressRangeDownTree(
2005 IN SIZE_T Length,
2006 IN ULONG_PTR BoundaryAddress,
2007 IN ULONG_PTR Alignment,
2008 IN PMM_AVL_TABLE Table,
2009 OUT PULONG_PTR Base,
2010 OUT PMMADDRESS_NODE *Parent
2011 );
2012
2013 NTSTATUS
2014 NTAPI
2015 MiFindEmptyAddressRangeDownBasedTree(
2016 IN SIZE_T Length,
2017 IN ULONG_PTR BoundaryAddress,
2018 IN ULONG_PTR Alignment,
2019 IN PMM_AVL_TABLE Table,
2020 OUT PULONG_PTR Base
2021 );
2022
2023 TABLE_SEARCH_RESULT
2024 NTAPI
2025 MiFindEmptyAddressRangeInTree(
2026 IN SIZE_T Length,
2027 IN ULONG_PTR Alignment,
2028 IN PMM_AVL_TABLE Table,
2029 OUT PMMADDRESS_NODE *PreviousVad,
2030 OUT PULONG_PTR Base
2031 );
2032
2033 NTSTATUS
2034 NTAPI
2035 MiCheckSecuredVad(
2036 IN PMMVAD Vad,
2037 IN PVOID Base,
2038 IN SIZE_T Size,
2039 IN ULONG ProtectionMask
2040 );
2041
2042 VOID
2043 NTAPI
2044 MiInsertVad(
2045 IN PMMVAD Vad,
2046 IN PEPROCESS Process
2047 );
2048
2049 NTSTATUS
2050 NTAPI
2051 MiInsertVadEx(
2052 _Inout_ PMMVAD Vad,
2053 _In_ ULONG_PTR *BaseAddress,
2054 _In_ SIZE_T ViewSize,
2055 _In_ ULONG_PTR HighestAddress,
2056 _In_ ULONG_PTR Alignment,
2057 _In_ ULONG AllocationType);
2058
2059 VOID
2060 NTAPI
2061 MiInsertBasedSection(
2062 IN PSECTION Section
2063 );
2064
2065 NTSTATUS
2066 NTAPI
2067 MiUnmapViewOfSection(
2068 IN PEPROCESS Process,
2069 IN PVOID BaseAddress,
2070 IN ULONG Flags
2071 );
2072
2073 NTSTATUS
2074 NTAPI
2075 MiRosUnmapViewOfSection(
2076 IN PEPROCESS Process,
2077 IN PVOID BaseAddress,
2078 IN ULONG Flags
2079 );
2080
2081 VOID
2082 NTAPI
2083 MiInsertNode(
2084 IN PMM_AVL_TABLE Table,
2085 IN PMMADDRESS_NODE NewNode,
2086 PMMADDRESS_NODE Parent,
2087 TABLE_SEARCH_RESULT Result
2088 );
2089
2090 VOID
2091 NTAPI
2092 MiRemoveNode(
2093 IN PMMADDRESS_NODE Node,
2094 IN PMM_AVL_TABLE Table
2095 );
2096
2097 PMMADDRESS_NODE
2098 NTAPI
2099 MiGetPreviousNode(
2100 IN PMMADDRESS_NODE Node
2101 );
2102
2103 PMMADDRESS_NODE
2104 NTAPI
2105 MiGetNextNode(
2106 IN PMMADDRESS_NODE Node
2107 );
2108
2109 BOOLEAN
2110 NTAPI
2111 MiInitializeSystemSpaceMap(
2112 IN PMMSESSION InputSession OPTIONAL
2113 );
2114
2115 VOID
2116 NTAPI
2117 MiSessionRemoveProcess(
2118 VOID
2119 );
2120
2121 VOID
2122 NTAPI
2123 MiReleaseProcessReferenceToSessionDataPage(
2124 IN PMM_SESSION_SPACE SessionGlobal
2125 );
2126
2127 VOID
2128 NTAPI
2129 MiSessionAddProcess(
2130 IN PEPROCESS NewProcess
2131 );
2132
2133 NTSTATUS
2134 NTAPI
2135 MiSessionCommitPageTables(
2136 IN PVOID StartVa,
2137 IN PVOID EndVa
2138 );
2139
2140 ULONG
2141 NTAPI
2142 MiMakeProtectionMask(
2143 IN ULONG Protect
2144 );
2145
2146 VOID
2147 NTAPI
2148 MiDeleteVirtualAddresses(
2149 IN ULONG_PTR Va,
2150 IN ULONG_PTR EndingAddress,
2151 IN PMMVAD Vad
2152 );
2153
2154 ULONG
2155 NTAPI
2156 MiMakeSystemAddressValid(
2157 IN PVOID PageTableVirtualAddress,
2158 IN PEPROCESS CurrentProcess
2159 );
2160
2161 ULONG
2162 NTAPI
2163 MiMakeSystemAddressValidPfn(
2164 IN PVOID VirtualAddress,
2165 IN KIRQL OldIrql
2166 );
2167
2168 VOID
2169 NTAPI
2170 MiRemoveMappedView(
2171 IN PEPROCESS CurrentProcess,
2172 IN PMMVAD Vad
2173 );
2174
2175 PSUBSECTION
2176 NTAPI
2177 MiLocateSubsection(
2178 IN PMMVAD Vad,
2179 IN ULONG_PTR Vpn
2180 );
2181
2182 VOID
2183 NTAPI
2184 MiDeleteARM3Section(
2185 PVOID ObjectBody
2186 );
2187
2188 NTSTATUS
2189 NTAPI
2190 MiQueryMemorySectionName(
2191 IN HANDLE ProcessHandle,
2192 IN PVOID BaseAddress,
2193 OUT PVOID MemoryInformation,
2194 IN SIZE_T MemoryInformationLength,
2195 OUT PSIZE_T ReturnLength
2196 );
2197
2198 NTSTATUS
2199 NTAPI
2200 MiRosUnmapViewInSystemSpace(
2201 IN PVOID MappedBase
2202 );
2203
2204 POOL_TYPE
2205 NTAPI
2206 MmDeterminePoolType(
2207 IN PVOID PoolAddress
2208 );
2209
2210 VOID
2211 NTAPI
2212 MiMakePdeExistAndMakeValid(
2213 IN PMMPTE PointerPde,
2214 IN PEPROCESS TargetProcess,
2215 IN KIRQL OldIrql
2216 );
2217
2218 //
2219 // MiRemoveZeroPage will use inline code to zero out the page manually if only
2220 // free pages are available. In some scenarios, we don't/can't run that piece of
2221 // code and would rather only have a real zero page. If we can't have a zero page,
2222 // then we'd like to have our own code to grab a free page and zero it out, by
2223 // using MiRemoveAnyPage. This macro implements this.
2224 //
2225 FORCEINLINE
2226 PFN_NUMBER
2227 MiRemoveZeroPageSafe(IN ULONG Color)
2228 {
2229 if (MmFreePagesByColor[ZeroedPageList][Color].Flink != LIST_HEAD) return MiRemoveZeroPage(Color);
2230 return 0;
2231 }
2232
2233 /* EOF */