[NTOS:MM]
[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 PMMPDE 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 MMPDE PageDirectory;
483 #else
484 PMMPDE 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 extern KSPIN_LOCK MmExpansionLock;
638 extern PETHREAD MiExpansionLockOwner;
639
640 FORCEINLINE
641 BOOLEAN
642 MiIsMemoryTypeFree(TYPE_OF_MEMORY MemoryType)
643 {
644 return ((MemoryType == LoaderFree) ||
645 (MemoryType == LoaderLoadedProgram) ||
646 (MemoryType == LoaderFirmwareTemporary) ||
647 (MemoryType == LoaderOsloaderStack));
648 }
649
650 FORCEINLINE
651 BOOLEAN
652 MiIsMemoryTypeInvisible(TYPE_OF_MEMORY MemoryType)
653 {
654 return ((MemoryType == LoaderFirmwarePermanent) ||
655 (MemoryType == LoaderSpecialMemory) ||
656 (MemoryType == LoaderHALCachedMemory) ||
657 (MemoryType == LoaderBBTMemory));
658 }
659
660 #ifdef _M_AMD64
661 FORCEINLINE
662 BOOLEAN
663 MiIsUserPxe(PVOID Address)
664 {
665 return ((ULONG_PTR)Address >> 7) == 0x1FFFFEDF6FB7DA0ULL;
666 }
667
668 FORCEINLINE
669 BOOLEAN
670 MiIsUserPpe(PVOID Address)
671 {
672 return ((ULONG_PTR)Address >> 16) == 0xFFFFF6FB7DA0ULL;
673 }
674
675 FORCEINLINE
676 BOOLEAN
677 MiIsUserPde(PVOID Address)
678 {
679 return ((ULONG_PTR)Address >> 25) == 0x7FFFFB7DA0ULL;
680 }
681
682 FORCEINLINE
683 BOOLEAN
684 MiIsUserPte(PVOID Address)
685 {
686 return ((ULONG_PTR)Address >> 34) == 0x3FFFFDA0ULL;
687 }
688 #else
689 FORCEINLINE
690 BOOLEAN
691 MiIsUserPde(PVOID Address)
692 {
693 return ((Address >= (PVOID)MiAddressToPde(NULL)) &&
694 (Address <= (PVOID)MiHighestUserPde));
695 }
696
697 FORCEINLINE
698 BOOLEAN
699 MiIsUserPte(PVOID Address)
700 {
701 return (Address <= (PVOID)MiHighestUserPte);
702 }
703 #endif
704
705 //
706 // Figures out the hardware bits for a PTE
707 //
708 FORCEINLINE
709 ULONG_PTR
710 MiDetermineUserGlobalPteMask(IN PVOID PointerPte)
711 {
712 MMPTE TempPte;
713
714 /* Start fresh */
715 TempPte.u.Long = 0;
716
717 /* Make it valid and accessed */
718 TempPte.u.Hard.Valid = TRUE;
719 MI_MAKE_ACCESSED_PAGE(&TempPte);
720
721 /* Is this for user-mode? */
722 if (
723 #if (_MI_PAGING_LEVELS == 4)
724 MiIsUserPxe(PointerPte) ||
725 #endif
726 #if (_MI_PAGING_LEVELS >= 3)
727 MiIsUserPpe(PointerPte) ||
728 #endif
729 MiIsUserPde(PointerPte) ||
730 MiIsUserPte(PointerPte))
731 {
732 /* Set the owner bit */
733 MI_MAKE_OWNER_PAGE(&TempPte);
734 }
735
736 /* FIXME: We should also set the global bit */
737
738 /* Return the protection */
739 return TempPte.u.Long;
740 }
741
742 //
743 // Creates a valid kernel PTE with the given protection
744 //
745 FORCEINLINE
746 VOID
747 MI_MAKE_HARDWARE_PTE_KERNEL(IN PMMPTE NewPte,
748 IN PMMPTE MappingPte,
749 IN ULONG_PTR ProtectionMask,
750 IN PFN_NUMBER PageFrameNumber)
751 {
752 /* Only valid for kernel, non-session PTEs */
753 ASSERT(MappingPte > MiHighestUserPte);
754 ASSERT(!MI_IS_SESSION_PTE(MappingPte));
755 ASSERT((MappingPte < (PMMPTE)PDE_BASE) || (MappingPte > (PMMPTE)PDE_TOP));
756
757 /* Start fresh */
758 *NewPte = ValidKernelPte;
759
760 /* Set the protection and page */
761 NewPte->u.Hard.PageFrameNumber = PageFrameNumber;
762 NewPte->u.Long |= MmProtectToPteMask[ProtectionMask];
763 }
764
765 //
766 // Creates a valid PTE with the given protection
767 //
768 FORCEINLINE
769 VOID
770 MI_MAKE_HARDWARE_PTE(IN PMMPTE NewPte,
771 IN PMMPTE MappingPte,
772 IN ULONG_PTR ProtectionMask,
773 IN PFN_NUMBER PageFrameNumber)
774 {
775 /* Set the protection and page */
776 NewPte->u.Long = MiDetermineUserGlobalPteMask(MappingPte);
777 NewPte->u.Long |= MmProtectToPteMask[ProtectionMask];
778 NewPte->u.Hard.PageFrameNumber = PageFrameNumber;
779 }
780
781 //
782 // Creates a valid user PTE with the given protection
783 //
784 FORCEINLINE
785 VOID
786 MI_MAKE_HARDWARE_PTE_USER(IN PMMPTE NewPte,
787 IN PMMPTE MappingPte,
788 IN ULONG_PTR ProtectionMask,
789 IN PFN_NUMBER PageFrameNumber)
790 {
791 /* Only valid for kernel, non-session PTEs */
792 ASSERT(MappingPte <= MiHighestUserPte);
793
794 /* Start fresh */
795 NewPte->u.Long = 0;
796
797 /* Set the protection and page */
798 NewPte->u.Hard.Valid = TRUE;
799 NewPte->u.Hard.Owner = TRUE;
800 NewPte->u.Hard.PageFrameNumber = PageFrameNumber;
801 NewPte->u.Long |= MmProtectToPteMask[ProtectionMask];
802 }
803
804 #ifndef _M_AMD64
805 //
806 // Builds a Prototype PTE for the address of the PTE
807 //
808 FORCEINLINE
809 VOID
810 MI_MAKE_PROTOTYPE_PTE(IN PMMPTE NewPte,
811 IN PMMPTE PointerPte)
812 {
813 ULONG_PTR Offset;
814
815 /* Mark this as a prototype */
816 NewPte->u.Long = 0;
817 NewPte->u.Proto.Prototype = 1;
818
819 /*
820 * Prototype PTEs are only valid in paged pool by design, this little trick
821 * lets us only use 30 bits for the adress of the PTE, as long as the area
822 * stays 1024MB At most.
823 */
824 Offset = (ULONG_PTR)PointerPte - (ULONG_PTR)MmPagedPoolStart;
825
826 /*
827 * 7 bits go in the "low" (but we assume the bottom 2 are zero)
828 * and the other 21 bits go in the "high"
829 */
830 NewPte->u.Proto.ProtoAddressLow = (Offset & 0x1FC) >> 2;
831 NewPte->u.Proto.ProtoAddressHigh = (Offset & 0x3FFFFE00) >> 9;
832 }
833
834 //
835 // Builds a Subsection PTE for the address of the Segment
836 //
837 FORCEINLINE
838 VOID
839 MI_MAKE_SUBSECTION_PTE(IN PMMPTE NewPte,
840 IN PVOID Segment)
841 {
842 ULONG_PTR Offset;
843
844 /* Mark this as a prototype */
845 NewPte->u.Long = 0;
846 NewPte->u.Subsect.Prototype = 1;
847
848 /*
849 * Segments are only valid either in nonpaged pool. We store the 20 bit
850 * difference either from the top or bottom of nonpaged pool, giving a
851 * maximum of 128MB to each delta, meaning nonpaged pool cannot exceed
852 * 256MB.
853 */
854 if ((ULONG_PTR)Segment < ((ULONG_PTR)MmSubsectionBase + (128 * _1MB)))
855 {
856 Offset = (ULONG_PTR)Segment - (ULONG_PTR)MmSubsectionBase;
857 NewPte->u.Subsect.WhichPool = PagedPool;
858 }
859 else
860 {
861 Offset = (ULONG_PTR)MmNonPagedPoolEnd - (ULONG_PTR)Segment;
862 NewPte->u.Subsect.WhichPool = NonPagedPool;
863 }
864
865 /*
866 * 4 bits go in the "low" (but we assume the bottom 3 are zero)
867 * and the other 20 bits go in the "high"
868 */
869 NewPte->u.Subsect.SubsectionAddressLow = (Offset & 0x78) >> 3;
870 NewPte->u.Subsect.SubsectionAddressHigh = (Offset & 0xFFFFF80) >> 7;
871 }
872
873 FORCEINLINE
874 BOOLEAN
875 MI_IS_MAPPED_PTE(PMMPTE PointerPte)
876 {
877 /// \todo Make this reasonable code, this is UGLY!
878 return ((PointerPte->u.Long & 0xFFFFFC01) != 0);
879 }
880
881 #endif
882
883 //
884 // Returns if the page is physically resident (ie: a large page)
885 // FIXFIX: CISC/x86 only?
886 //
887 FORCEINLINE
888 BOOLEAN
889 MI_IS_PHYSICAL_ADDRESS(IN PVOID Address)
890 {
891 PMMPDE PointerPde;
892
893 /* Large pages are never paged out, always physically resident */
894 PointerPde = MiAddressToPde(Address);
895 return ((PointerPde->u.Hard.LargePage) && (PointerPde->u.Hard.Valid));
896 }
897
898 //
899 // Writes a valid PTE
900 //
901 FORCEINLINE
902 VOID
903 MI_WRITE_VALID_PTE(IN PMMPTE PointerPte,
904 IN MMPTE TempPte)
905 {
906 /* Write the valid PTE */
907 ASSERT(PointerPte->u.Hard.Valid == 0);
908 ASSERT(TempPte.u.Hard.Valid == 1);
909 *PointerPte = TempPte;
910 }
911
912 //
913 // Updates a valid PTE
914 //
915 FORCEINLINE
916 VOID
917 MI_UPDATE_VALID_PTE(IN PMMPTE PointerPte,
918 IN MMPTE TempPte)
919 {
920 /* Write the valid PTE */
921 ASSERT(PointerPte->u.Hard.Valid == 1);
922 ASSERT(TempPte.u.Hard.Valid == 1);
923 ASSERT(PointerPte->u.Hard.PageFrameNumber == TempPte.u.Hard.PageFrameNumber);
924 *PointerPte = TempPte;
925 }
926
927 //
928 // Writes an invalid PTE
929 //
930 FORCEINLINE
931 VOID
932 MI_WRITE_INVALID_PTE(IN PMMPTE PointerPte,
933 IN MMPTE InvalidPte)
934 {
935 /* Write the invalid PTE */
936 ASSERT(InvalidPte.u.Hard.Valid == 0);
937 ASSERT(InvalidPte.u.Long != 0);
938 *PointerPte = InvalidPte;
939 }
940
941 //
942 // Erase the PTE completely
943 //
944 FORCEINLINE
945 VOID
946 MI_ERASE_PTE(IN PMMPTE PointerPte)
947 {
948 /* Zero out the PTE */
949 ASSERT(PointerPte->u.Long != 0);
950 PointerPte->u.Long = 0;
951 }
952
953 //
954 // Writes a valid PDE
955 //
956 FORCEINLINE
957 VOID
958 MI_WRITE_VALID_PDE(IN PMMPDE PointerPde,
959 IN MMPDE TempPde)
960 {
961 /* Write the valid PDE */
962 ASSERT(PointerPde->u.Hard.Valid == 0);
963 ASSERT(TempPde.u.Hard.Valid == 1);
964 *PointerPde = TempPde;
965 }
966
967 //
968 // Writes an invalid PDE
969 //
970 FORCEINLINE
971 VOID
972 MI_WRITE_INVALID_PDE(IN PMMPDE PointerPde,
973 IN MMPDE InvalidPde)
974 {
975 /* Write the invalid PDE */
976 ASSERT(InvalidPde.u.Hard.Valid == 0);
977 ASSERT(InvalidPde.u.Long != 0);
978 *PointerPde = InvalidPde;
979 }
980
981 //
982 // Checks if the thread already owns a working set
983 //
984 FORCEINLINE
985 BOOLEAN
986 MM_ANY_WS_LOCK_HELD(IN PETHREAD Thread)
987 {
988 /* If any of these are held, return TRUE */
989 return ((Thread->OwnsProcessWorkingSetExclusive) ||
990 (Thread->OwnsProcessWorkingSetShared) ||
991 (Thread->OwnsSystemWorkingSetExclusive) ||
992 (Thread->OwnsSystemWorkingSetShared) ||
993 (Thread->OwnsSessionWorkingSetExclusive) ||
994 (Thread->OwnsSessionWorkingSetShared));
995 }
996
997 //
998 // Checks if the process owns the working set lock
999 //
1000 FORCEINLINE
1001 BOOLEAN
1002 MI_WS_OWNER(IN PEPROCESS Process)
1003 {
1004 /* Check if this process is the owner, and that the thread owns the WS */
1005 if (PsGetCurrentThread()->OwnsProcessWorkingSetExclusive == 0)
1006 {
1007 DPRINT("Thread: %p is not an owner\n", PsGetCurrentThread());
1008 }
1009 if (KeGetCurrentThread()->ApcState.Process != &Process->Pcb)
1010 {
1011 DPRINT("Current thread %p is attached to another process %p\n", PsGetCurrentThread(), Process);
1012 }
1013 return ((KeGetCurrentThread()->ApcState.Process == &Process->Pcb) &&
1014 ((PsGetCurrentThread()->OwnsProcessWorkingSetExclusive) ||
1015 (PsGetCurrentThread()->OwnsProcessWorkingSetShared)));
1016 }
1017
1018 //
1019 // New ARM3<->RosMM PAGE Architecture
1020 //
1021 FORCEINLINE
1022 BOOLEAN
1023 MiIsRosSectionObject(IN PVOID Section)
1024 {
1025 PROS_SECTION_OBJECT RosSection = Section;
1026 if ((RosSection->Type == 'SC') && (RosSection->Size == 'TN')) return TRUE;
1027 return FALSE;
1028 }
1029
1030 #define MI_IS_ROS_PFN(x) ((x)->u4.AweAllocation == TRUE)
1031
1032 VOID
1033 NTAPI
1034 MiDecrementReferenceCount(
1035 IN PMMPFN Pfn1,
1036 IN PFN_NUMBER PageFrameIndex
1037 );
1038
1039 FORCEINLINE
1040 BOOLEAN
1041 MI_IS_WS_UNSAFE(IN PEPROCESS Process)
1042 {
1043 return (Process->Vm.Flags.AcquiredUnsafe == TRUE);
1044 }
1045
1046 //
1047 // Locks the working set for the given process
1048 //
1049 FORCEINLINE
1050 VOID
1051 MiLockProcessWorkingSet(IN PEPROCESS Process,
1052 IN PETHREAD Thread)
1053 {
1054 /* Shouldn't already be owning the process working set */
1055 ASSERT(Thread->OwnsProcessWorkingSetShared == FALSE);
1056 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1057
1058 /* Block APCs, make sure that still nothing is already held */
1059 KeEnterGuardedRegion();
1060 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread));
1061
1062 /* Lock the working set */
1063 ExAcquirePushLockExclusive(&Process->Vm.WorkingSetMutex);
1064
1065 /* Now claim that we own the lock */
1066 ASSERT(!MI_IS_WS_UNSAFE(Process));
1067 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1068 Thread->OwnsProcessWorkingSetExclusive = TRUE;
1069 }
1070
1071 FORCEINLINE
1072 VOID
1073 MiLockProcessWorkingSetShared(IN PEPROCESS Process,
1074 IN PETHREAD Thread)
1075 {
1076 /* Shouldn't already be owning the process working set */
1077 ASSERT(Thread->OwnsProcessWorkingSetShared == FALSE);
1078 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1079
1080 /* Block APCs, make sure that still nothing is already held */
1081 KeEnterGuardedRegion();
1082 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread));
1083
1084 /* Lock the working set */
1085 ExAcquirePushLockShared(&Process->Vm.WorkingSetMutex);
1086
1087 /* Now claim that we own the lock */
1088 ASSERT(!MI_IS_WS_UNSAFE(Process));
1089 ASSERT(Thread->OwnsProcessWorkingSetShared == FALSE);
1090 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1091 Thread->OwnsProcessWorkingSetShared = TRUE;
1092 }
1093
1094 FORCEINLINE
1095 VOID
1096 MiLockProcessWorkingSetUnsafe(IN PEPROCESS Process,
1097 IN PETHREAD Thread)
1098 {
1099 /* Shouldn't already be owning the process working set */
1100 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1101
1102 /* APCs must be blocked, make sure that still nothing is already held */
1103 ASSERT(KeAreAllApcsDisabled() == TRUE);
1104 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread));
1105
1106 /* Lock the working set */
1107 ExAcquirePushLockExclusive(&Process->Vm.WorkingSetMutex);
1108
1109 /* Now claim that we own the lock */
1110 ASSERT(!MI_IS_WS_UNSAFE(Process));
1111 Process->Vm.Flags.AcquiredUnsafe = 1;
1112 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1113 Thread->OwnsProcessWorkingSetExclusive = TRUE;
1114 }
1115
1116 //
1117 // Unlocks the working set for the given process
1118 //
1119 FORCEINLINE
1120 VOID
1121 MiUnlockProcessWorkingSet(IN PEPROCESS Process,
1122 IN PETHREAD Thread)
1123 {
1124 /* Make sure we are the owner of a safe acquisition */
1125 ASSERT(MI_WS_OWNER(Process));
1126 ASSERT(!MI_IS_WS_UNSAFE(Process));
1127
1128 /* The thread doesn't own it anymore */
1129 ASSERT(Thread->OwnsProcessWorkingSetExclusive == TRUE);
1130 Thread->OwnsProcessWorkingSetExclusive = FALSE;
1131
1132 /* Release the lock and re-enable APCs */
1133 ExReleasePushLockExclusive(&Process->Vm.WorkingSetMutex);
1134 KeLeaveGuardedRegion();
1135 }
1136
1137 //
1138 // Unlocks the working set for the given process
1139 //
1140 FORCEINLINE
1141 VOID
1142 MiUnlockProcessWorkingSetShared(IN PEPROCESS Process,
1143 IN PETHREAD Thread)
1144 {
1145 /* Make sure we are the owner of a safe acquisition (because shared) */
1146 ASSERT(MI_WS_OWNER(Process));
1147 ASSERT(!MI_IS_WS_UNSAFE(Process));
1148
1149 /* Ensure we are in a shared acquisition */
1150 ASSERT(Thread->OwnsProcessWorkingSetShared == TRUE);
1151 ASSERT(Thread->OwnsProcessWorkingSetExclusive == FALSE);
1152
1153 /* Don't claim the lock anylonger */
1154 Thread->OwnsProcessWorkingSetShared = FALSE;
1155
1156 /* Release the lock and re-enable APCs */
1157 ExReleasePushLockShared(&Process->Vm.WorkingSetMutex);
1158 KeLeaveGuardedRegion();
1159 }
1160
1161 //
1162 // Unlocks the working set for the given process
1163 //
1164 FORCEINLINE
1165 VOID
1166 MiUnlockProcessWorkingSetUnsafe(IN PEPROCESS Process,
1167 IN PETHREAD Thread)
1168 {
1169 /* Make sure we are the owner of an unsafe acquisition */
1170 ASSERT(KeGetCurrentIrql() <= APC_LEVEL);
1171 ASSERT(KeAreAllApcsDisabled() == TRUE);
1172 ASSERT(MI_WS_OWNER(Process));
1173 ASSERT(MI_IS_WS_UNSAFE(Process));
1174
1175 /* No longer unsafe */
1176 Process->Vm.Flags.AcquiredUnsafe = 0;
1177
1178 /* The thread doesn't own it anymore */
1179 ASSERT(Thread->OwnsProcessWorkingSetExclusive == TRUE);
1180 Thread->OwnsProcessWorkingSetExclusive = FALSE;
1181
1182 /* Release the lock but don't touch APC state */
1183 ExReleasePushLockExclusive(&Process->Vm.WorkingSetMutex);
1184 ASSERT(KeGetCurrentIrql() <= APC_LEVEL);
1185 }
1186
1187 //
1188 // Locks the working set
1189 //
1190 FORCEINLINE
1191 VOID
1192 MiLockWorkingSet(IN PETHREAD Thread,
1193 IN PMMSUPPORT WorkingSet)
1194 {
1195 /* Block APCs */
1196 KeEnterGuardedRegion();
1197
1198 /* Working set should be in global memory */
1199 ASSERT(MI_IS_SESSION_ADDRESS((PVOID)WorkingSet) == FALSE);
1200
1201 /* Thread shouldn't already be owning something */
1202 ASSERT(!MM_ANY_WS_LOCK_HELD(Thread));
1203
1204 /* Lock this working set */
1205 ExAcquirePushLockExclusive(&WorkingSet->WorkingSetMutex);
1206
1207 /* Which working set is this? */
1208 if (WorkingSet == &MmSystemCacheWs)
1209 {
1210 /* Own the system working set */
1211 ASSERT((Thread->OwnsSystemWorkingSetExclusive == FALSE) &&
1212 (Thread->OwnsSystemWorkingSetShared == FALSE));
1213 Thread->OwnsSystemWorkingSetExclusive = TRUE;
1214 }
1215 else if (WorkingSet->Flags.SessionSpace)
1216 {
1217 /* Own the session working set */
1218 ASSERT((Thread->OwnsSessionWorkingSetExclusive == FALSE) &&
1219 (Thread->OwnsSessionWorkingSetShared == FALSE));
1220 Thread->OwnsSessionWorkingSetExclusive = TRUE;
1221 }
1222 else
1223 {
1224 /* Own the process working set */
1225 ASSERT((Thread->OwnsProcessWorkingSetExclusive == FALSE) &&
1226 (Thread->OwnsProcessWorkingSetShared == FALSE));
1227 Thread->OwnsProcessWorkingSetExclusive = TRUE;
1228 }
1229 }
1230
1231 //
1232 // Unlocks the working set
1233 //
1234 FORCEINLINE
1235 VOID
1236 MiUnlockWorkingSet(IN PETHREAD Thread,
1237 IN PMMSUPPORT WorkingSet)
1238 {
1239 /* Working set should be in global memory */
1240 ASSERT(MI_IS_SESSION_ADDRESS((PVOID)WorkingSet) == FALSE);
1241
1242 /* Which working set is this? */
1243 if (WorkingSet == &MmSystemCacheWs)
1244 {
1245 /* Release the system working set */
1246 ASSERT((Thread->OwnsSystemWorkingSetExclusive == TRUE) ||
1247 (Thread->OwnsSystemWorkingSetShared == TRUE));
1248 Thread->OwnsSystemWorkingSetExclusive = FALSE;
1249 }
1250 else if (WorkingSet->Flags.SessionSpace)
1251 {
1252 /* Release the session working set */
1253 ASSERT((Thread->OwnsSessionWorkingSetExclusive == TRUE) ||
1254 (Thread->OwnsSessionWorkingSetShared == TRUE));
1255 Thread->OwnsSessionWorkingSetExclusive = 0;
1256 }
1257 else
1258 {
1259 /* Release the process working set */
1260 ASSERT((Thread->OwnsProcessWorkingSetExclusive) ||
1261 (Thread->OwnsProcessWorkingSetShared));
1262 Thread->OwnsProcessWorkingSetExclusive = FALSE;
1263 }
1264
1265 /* Release the working set lock */
1266 ExReleasePushLockExclusive(&WorkingSet->WorkingSetMutex);
1267
1268 /* Unblock APCs */
1269 KeLeaveGuardedRegion();
1270 }
1271
1272 FORCEINLINE
1273 VOID
1274 MiUnlockProcessWorkingSetForFault(IN PEPROCESS Process,
1275 IN PETHREAD Thread,
1276 OUT PBOOLEAN Safe,
1277 OUT PBOOLEAN Shared)
1278 {
1279 ASSERT(MI_WS_OWNER(Process));
1280
1281 /* Check if the current owner is unsafe */
1282 if (MI_IS_WS_UNSAFE(Process))
1283 {
1284 /* Release unsafely */
1285 MiUnlockProcessWorkingSetUnsafe(Process, Thread);
1286 *Safe = FALSE;
1287 *Shared = FALSE;
1288 }
1289 else if (Thread->OwnsProcessWorkingSetExclusive == 1)
1290 {
1291 /* Owner is safe and exclusive, release normally */
1292 MiUnlockProcessWorkingSet(Process, Thread);
1293 *Safe = TRUE;
1294 *Shared = FALSE;
1295 }
1296 else
1297 {
1298 /* Owner is shared (implies safe), release normally */
1299 MiUnlockProcessWorkingSetShared(Process, Thread);
1300 *Safe = TRUE;
1301 *Shared = TRUE;
1302 }
1303 }
1304
1305 FORCEINLINE
1306 VOID
1307 MiLockProcessWorkingSetForFault(IN PEPROCESS Process,
1308 IN PETHREAD Thread,
1309 IN BOOLEAN Safe,
1310 IN BOOLEAN Shared)
1311 {
1312 /* Check if this was a safe lock or not */
1313 if (Safe)
1314 {
1315 if (Shared)
1316 {
1317 /* Reacquire safely & shared */
1318 MiLockProcessWorkingSetShared(Process, Thread);
1319 }
1320 else
1321 {
1322 /* Reacquire safely */
1323 MiLockProcessWorkingSet(Process, Thread);
1324 }
1325 }
1326 else
1327 {
1328 /* Unsafe lock cannot be shared */
1329 ASSERT(Shared == FALSE);
1330 /* Reacquire unsafely */
1331 MiLockProcessWorkingSetUnsafe(Process, Thread);
1332 }
1333 }
1334
1335 FORCEINLINE
1336 KIRQL
1337 MiAcquireExpansionLock(VOID)
1338 {
1339 KIRQL OldIrql;
1340
1341 ASSERT(KeGetCurrentIrql() <= APC_LEVEL);
1342 KeAcquireSpinLock(&MmExpansionLock, &OldIrql);
1343 ASSERT(MiExpansionLockOwner == NULL);
1344 MiExpansionLockOwner = PsGetCurrentThread();
1345 return OldIrql;
1346 }
1347
1348 FORCEINLINE
1349 VOID
1350 MiReleaseExpansionLock(KIRQL OldIrql)
1351 {
1352 ASSERT(MiExpansionLockOwner == PsGetCurrentThread());
1353 MiExpansionLockOwner = NULL;
1354 KeReleaseSpinLock(&MmExpansionLock, OldIrql);
1355 ASSERT(KeGetCurrentIrql() <= APC_LEVEL);
1356 }
1357
1358 //
1359 // Returns the ProtoPTE inside a VAD for the given VPN
1360 //
1361 FORCEINLINE
1362 PMMPTE
1363 MI_GET_PROTOTYPE_PTE_FOR_VPN(IN PMMVAD Vad,
1364 IN ULONG_PTR Vpn)
1365 {
1366 PMMPTE ProtoPte;
1367
1368 /* Find the offset within the VAD's prototype PTEs */
1369 ProtoPte = Vad->FirstPrototypePte + (Vpn - Vad->StartingVpn);
1370 ASSERT(ProtoPte <= Vad->LastContiguousPte);
1371 return ProtoPte;
1372 }
1373
1374 //
1375 // Returns the PFN Database entry for the given page number
1376 // Warning: This is not necessarily a valid PFN database entry!
1377 //
1378 FORCEINLINE
1379 PMMPFN
1380 MI_PFN_ELEMENT(IN PFN_NUMBER Pfn)
1381 {
1382 /* Get the entry */
1383 return &MmPfnDatabase[Pfn];
1384 };
1385
1386 //
1387 // Drops a locked page without dereferencing it
1388 //
1389 FORCEINLINE
1390 VOID
1391 MiDropLockCount(IN PMMPFN Pfn1)
1392 {
1393 /* This page shouldn't be locked, but it should be valid */
1394 ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
1395 ASSERT(Pfn1->u2.ShareCount == 0);
1396
1397 /* Is this the last reference to the page */
1398 if (Pfn1->u3.e2.ReferenceCount == 1)
1399 {
1400 /* It better not be valid */
1401 ASSERT(Pfn1->u3.e1.PageLocation != ActiveAndValid);
1402
1403 /* Is it a prototype PTE? */
1404 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1405 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1406 {
1407 /* FIXME: We should return commit */
1408 DPRINT1("Not returning commit for prototype PTE\n");
1409 }
1410
1411 /* Update the counter */
1412 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1413 }
1414 }
1415
1416 //
1417 // Drops a locked page and dereferences it
1418 //
1419 FORCEINLINE
1420 VOID
1421 MiDereferencePfnAndDropLockCount(IN PMMPFN Pfn1)
1422 {
1423 USHORT RefCount, OldRefCount;
1424 PFN_NUMBER PageFrameIndex;
1425
1426 /* Loop while we decrement the page successfully */
1427 do
1428 {
1429 /* There should be at least one reference */
1430 OldRefCount = Pfn1->u3.e2.ReferenceCount;
1431 ASSERT(OldRefCount != 0);
1432
1433 /* Are we the last one */
1434 if (OldRefCount == 1)
1435 {
1436 /* The page shoudln't be shared not active at this point */
1437 ASSERT(Pfn1->u3.e2.ReferenceCount == 1);
1438 ASSERT(Pfn1->u3.e1.PageLocation != ActiveAndValid);
1439 ASSERT(Pfn1->u2.ShareCount == 0);
1440
1441 /* Is it a prototype PTE? */
1442 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1443 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1444 {
1445 /* FIXME: We should return commit */
1446 DPRINT1("Not returning commit for prototype PTE\n");
1447 }
1448
1449 /* Update the counter, and drop a reference the long way */
1450 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1451 PageFrameIndex = MiGetPfnEntryIndex(Pfn1);
1452 MiDecrementReferenceCount(Pfn1, PageFrameIndex);
1453 return;
1454 }
1455
1456 /* Drop a reference the short way, and that's it */
1457 RefCount = InterlockedCompareExchange16((PSHORT)&Pfn1->u3.e2.ReferenceCount,
1458 OldRefCount - 1,
1459 OldRefCount);
1460 ASSERT(RefCount != 0);
1461 } while (OldRefCount != RefCount);
1462
1463 /* If we got here, there should be more than one reference */
1464 ASSERT(RefCount > 1);
1465 if (RefCount == 2)
1466 {
1467 /* Is it still being shared? */
1468 if (Pfn1->u2.ShareCount >= 1)
1469 {
1470 /* Then it should be valid */
1471 ASSERT(Pfn1->u3.e1.PageLocation == ActiveAndValid);
1472
1473 /* Is it a prototype PTE? */
1474 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1475 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1476 {
1477 /* We don't handle ethis */
1478 ASSERT(FALSE);
1479 }
1480
1481 /* Update the counter */
1482 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1483 }
1484 }
1485 }
1486
1487 //
1488 // References a locked page and updates the counter
1489 // Used in MmProbeAndLockPages to handle different edge cases
1490 //
1491 FORCEINLINE
1492 VOID
1493 MiReferenceProbedPageAndBumpLockCount(IN PMMPFN Pfn1)
1494 {
1495 USHORT RefCount, OldRefCount;
1496
1497 /* Sanity check */
1498 ASSERT(Pfn1->u3.e2.ReferenceCount != 0);
1499
1500 /* Does ARM3 own the page? */
1501 if (MI_IS_ROS_PFN(Pfn1))
1502 {
1503 /* ReactOS Mm doesn't track share count */
1504 ASSERT(Pfn1->u3.e1.PageLocation == ActiveAndValid);
1505 }
1506 else
1507 {
1508 /* On ARM3 pages, we should see a valid share count */
1509 ASSERT((Pfn1->u2.ShareCount != 0) && (Pfn1->u3.e1.PageLocation == ActiveAndValid));
1510
1511 /* Is it a prototype PTE? */
1512 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1513 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1514 {
1515 /* FIXME: We should charge commit */
1516 DPRINT1("Not charging commit for prototype PTE\n");
1517 }
1518 }
1519
1520 /* More locked pages! */
1521 InterlockedIncrementSizeT(&MmSystemLockPagesCount);
1522
1523 /* Loop trying to update the reference count */
1524 do
1525 {
1526 /* Get the current reference count, make sure it's valid */
1527 OldRefCount = Pfn1->u3.e2.ReferenceCount;
1528 ASSERT(OldRefCount != 0);
1529 ASSERT(OldRefCount < 2500);
1530
1531 /* Bump it up by one */
1532 RefCount = InterlockedCompareExchange16((PSHORT)&Pfn1->u3.e2.ReferenceCount,
1533 OldRefCount + 1,
1534 OldRefCount);
1535 ASSERT(RefCount != 0);
1536 } while (OldRefCount != RefCount);
1537
1538 /* Was this the first lock attempt? If not, undo our bump */
1539 if (OldRefCount != 1) InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1540 }
1541
1542 //
1543 // References a locked page and updates the counter
1544 // Used in all other cases except MmProbeAndLockPages
1545 //
1546 FORCEINLINE
1547 VOID
1548 MiReferenceUsedPageAndBumpLockCount(IN PMMPFN Pfn1)
1549 {
1550 USHORT NewRefCount;
1551
1552 /* Is it a prototype PTE? */
1553 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1554 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1555 {
1556 /* FIXME: We should charge commit */
1557 DPRINT1("Not charging commit for prototype PTE\n");
1558 }
1559
1560 /* More locked pages! */
1561 InterlockedIncrementSizeT(&MmSystemLockPagesCount);
1562
1563 /* Update the reference count */
1564 NewRefCount = InterlockedIncrement16((PSHORT)&Pfn1->u3.e2.ReferenceCount);
1565 if (NewRefCount == 2)
1566 {
1567 /* Is it locked or shared? */
1568 if (Pfn1->u2.ShareCount)
1569 {
1570 /* It's shared, so make sure it's active */
1571 ASSERT(Pfn1->u3.e1.PageLocation == ActiveAndValid);
1572 }
1573 else
1574 {
1575 /* It's locked, so we shouldn't lock again */
1576 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1577 }
1578 }
1579 else
1580 {
1581 /* Someone had already locked the page, so undo our bump */
1582 ASSERT(NewRefCount < 2500);
1583 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1584 }
1585 }
1586
1587 //
1588 // References a locked page and updates the counter
1589 // Used in all other cases except MmProbeAndLockPages
1590 //
1591 FORCEINLINE
1592 VOID
1593 MiReferenceUnusedPageAndBumpLockCount(IN PMMPFN Pfn1)
1594 {
1595 USHORT NewRefCount;
1596
1597 /* Make sure the page isn't used yet */
1598 ASSERT(Pfn1->u2.ShareCount == 0);
1599 ASSERT(Pfn1->u3.e1.PageLocation != ActiveAndValid);
1600
1601 /* Is it a prototype PTE? */
1602 if ((Pfn1->u3.e1.PrototypePte == 1) &&
1603 (Pfn1->OriginalPte.u.Soft.Prototype == 1))
1604 {
1605 /* FIXME: We should charge commit */
1606 DPRINT1("Not charging commit for prototype PTE\n");
1607 }
1608
1609 /* More locked pages! */
1610 InterlockedIncrementSizeT(&MmSystemLockPagesCount);
1611
1612 /* Update the reference count */
1613 NewRefCount = InterlockedIncrement16((PSHORT)&Pfn1->u3.e2.ReferenceCount);
1614 if (NewRefCount != 1)
1615 {
1616 /* Someone had already locked the page, so undo our bump */
1617 ASSERT(NewRefCount < 2500);
1618 InterlockedDecrementSizeT(&MmSystemLockPagesCount);
1619 }
1620 }
1621
1622 FORCEINLINE
1623 VOID
1624 MiIncrementPageTableReferences(IN PVOID Address)
1625 {
1626 PUSHORT RefCount;
1627
1628 RefCount = &MmWorkingSetList->UsedPageTableEntries[MiGetPdeOffset(Address)];
1629
1630 *RefCount += 1;
1631 ASSERT(*RefCount <= PTE_PER_PAGE);
1632 }
1633
1634 FORCEINLINE
1635 VOID
1636 MiDecrementPageTableReferences(IN PVOID Address)
1637 {
1638 PUSHORT RefCount;
1639
1640 RefCount = &MmWorkingSetList->UsedPageTableEntries[MiGetPdeOffset(Address)];
1641
1642 *RefCount -= 1;
1643 ASSERT(*RefCount < PTE_PER_PAGE);
1644 }
1645
1646 FORCEINLINE
1647 USHORT
1648 MiQueryPageTableReferences(IN PVOID Address)
1649 {
1650 PUSHORT RefCount;
1651
1652 RefCount = &MmWorkingSetList->UsedPageTableEntries[MiGetPdeOffset(Address)];
1653
1654 return *RefCount;
1655 }
1656
1657 BOOLEAN
1658 NTAPI
1659 MmArmInitSystem(
1660 IN ULONG Phase,
1661 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1662 );
1663
1664 VOID
1665 NTAPI
1666 MiInitializeSessionSpaceLayout();
1667
1668 NTSTATUS
1669 NTAPI
1670 MiInitMachineDependent(
1671 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1672 );
1673
1674 VOID
1675 NTAPI
1676 MiComputeColorInformation(
1677 VOID
1678 );
1679
1680 VOID
1681 NTAPI
1682 MiMapPfnDatabase(
1683 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1684 );
1685
1686 VOID
1687 NTAPI
1688 MiInitializeColorTables(
1689 VOID
1690 );
1691
1692 VOID
1693 NTAPI
1694 MiInitializePfnDatabase(
1695 IN PLOADER_PARAMETER_BLOCK LoaderBlock
1696 );
1697
1698 VOID
1699 NTAPI
1700 MiInitializeSessionWsSupport(
1701 VOID
1702 );
1703
1704 VOID
1705 NTAPI
1706 MiInitializeSessionIds(
1707 VOID
1708 );
1709
1710 BOOLEAN
1711 NTAPI
1712 MiInitializeMemoryEvents(
1713 VOID
1714 );
1715
1716 PFN_NUMBER
1717 NTAPI
1718 MxGetNextPage(
1719 IN PFN_NUMBER PageCount
1720 );
1721
1722 PPHYSICAL_MEMORY_DESCRIPTOR
1723 NTAPI
1724 MmInitializeMemoryLimits(
1725 IN PLOADER_PARAMETER_BLOCK LoaderBlock,
1726 IN PBOOLEAN IncludeType
1727 );
1728
1729 PFN_NUMBER
1730 NTAPI
1731 MiPagesInLoaderBlock(
1732 IN PLOADER_PARAMETER_BLOCK LoaderBlock,
1733 IN PBOOLEAN IncludeType
1734 );
1735
1736 VOID
1737 FASTCALL
1738 MiSyncARM3WithROS(
1739 IN PVOID AddressStart,
1740 IN PVOID AddressEnd
1741 );
1742
1743 NTSTATUS
1744 NTAPI
1745 MiRosProtectVirtualMemory(
1746 IN PEPROCESS Process,
1747 IN OUT PVOID *BaseAddress,
1748 IN OUT PSIZE_T NumberOfBytesToProtect,
1749 IN ULONG NewAccessProtection,
1750 OUT PULONG OldAccessProtection OPTIONAL
1751 );
1752
1753 NTSTATUS
1754 NTAPI
1755 MmArmAccessFault(
1756 IN BOOLEAN StoreInstruction,
1757 IN PVOID Address,
1758 IN KPROCESSOR_MODE Mode,
1759 IN PVOID TrapInformation
1760 );
1761
1762 NTSTATUS
1763 FASTCALL
1764 MiCheckPdeForPagedPool(
1765 IN PVOID Address
1766 );
1767
1768 VOID
1769 NTAPI
1770 MiInitializeNonPagedPool(
1771 VOID
1772 );
1773
1774 VOID
1775 NTAPI
1776 MiInitializeNonPagedPoolThresholds(
1777 VOID
1778 );
1779
1780 VOID
1781 NTAPI
1782 MiInitializePoolEvents(
1783 VOID
1784 );
1785
1786 VOID //
1787 NTAPI //
1788 InitializePool( //
1789 IN POOL_TYPE PoolType,// FIXFIX: This should go in ex.h after the pool merge
1790 IN ULONG Threshold //
1791 ); //
1792
1793 // FIXFIX: THIS ONE TOO
1794 VOID
1795 NTAPI
1796 INIT_FUNCTION
1797 ExInitializePoolDescriptor(
1798 IN PPOOL_DESCRIPTOR PoolDescriptor,
1799 IN POOL_TYPE PoolType,
1800 IN ULONG PoolIndex,
1801 IN ULONG Threshold,
1802 IN PVOID PoolLock
1803 );
1804
1805 NTSTATUS
1806 NTAPI
1807 MiInitializeSessionPool(
1808 VOID
1809 );
1810
1811 VOID
1812 NTAPI
1813 MiInitializeSystemPtes(
1814 IN PMMPTE StartingPte,
1815 IN ULONG NumberOfPtes,
1816 IN MMSYSTEM_PTE_POOL_TYPE PoolType
1817 );
1818
1819 PMMPTE
1820 NTAPI
1821 MiReserveSystemPtes(
1822 IN ULONG NumberOfPtes,
1823 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1824 );
1825
1826 VOID
1827 NTAPI
1828 MiReleaseSystemPtes(
1829 IN PMMPTE StartingPte,
1830 IN ULONG NumberOfPtes,
1831 IN MMSYSTEM_PTE_POOL_TYPE SystemPtePoolType
1832 );
1833
1834
1835 PFN_NUMBER
1836 NTAPI
1837 MiFindContiguousPages(
1838 IN PFN_NUMBER LowestPfn,
1839 IN PFN_NUMBER HighestPfn,
1840 IN PFN_NUMBER BoundaryPfn,
1841 IN PFN_NUMBER SizeInPages,
1842 IN MEMORY_CACHING_TYPE CacheType
1843 );
1844
1845 PVOID
1846 NTAPI
1847 MiCheckForContiguousMemory(
1848 IN PVOID BaseAddress,
1849 IN PFN_NUMBER BaseAddressPages,
1850 IN PFN_NUMBER SizeInPages,
1851 IN PFN_NUMBER LowestPfn,
1852 IN PFN_NUMBER HighestPfn,
1853 IN PFN_NUMBER BoundaryPfn,
1854 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute
1855 );
1856
1857 PMDL
1858 NTAPI
1859 MiAllocatePagesForMdl(
1860 IN PHYSICAL_ADDRESS LowAddress,
1861 IN PHYSICAL_ADDRESS HighAddress,
1862 IN PHYSICAL_ADDRESS SkipBytes,
1863 IN SIZE_T TotalBytes,
1864 IN MI_PFN_CACHE_ATTRIBUTE CacheAttribute,
1865 IN ULONG Flags
1866 );
1867
1868 PVOID
1869 NTAPI
1870 MiMapLockedPagesInUserSpace(
1871 IN PMDL Mdl,
1872 IN PVOID BaseVa,
1873 IN MEMORY_CACHING_TYPE CacheType,
1874 IN PVOID BaseAddress
1875 );
1876
1877 VOID
1878 NTAPI
1879 MiUnmapLockedPagesInUserSpace(
1880 IN PVOID BaseAddress,
1881 IN PMDL Mdl
1882 );
1883
1884 VOID
1885 NTAPI
1886 MiInsertPageInList(
1887 IN PMMPFNLIST ListHead,
1888 IN PFN_NUMBER PageFrameIndex
1889 );
1890
1891 VOID
1892 NTAPI
1893 MiUnlinkFreeOrZeroedPage(
1894 IN PMMPFN Entry
1895 );
1896
1897 VOID
1898 NTAPI
1899 MiUnlinkPageFromList(
1900 IN PMMPFN Pfn
1901 );
1902
1903 VOID
1904 NTAPI
1905 MiInitializePfn(
1906 IN PFN_NUMBER PageFrameIndex,
1907 IN PMMPTE PointerPte,
1908 IN BOOLEAN Modified
1909 );
1910
1911 NTSTATUS
1912 NTAPI
1913 MiInitializeAndChargePfn(
1914 OUT PPFN_NUMBER PageFrameIndex,
1915 IN PMMPDE PointerPde,
1916 IN PFN_NUMBER ContainingPageFrame,
1917 IN BOOLEAN SessionAllocation
1918 );
1919
1920 VOID
1921 NTAPI
1922 MiInitializePfnAndMakePteValid(
1923 IN PFN_NUMBER PageFrameIndex,
1924 IN PMMPTE PointerPte,
1925 IN MMPTE TempPte
1926 );
1927
1928 VOID
1929 NTAPI
1930 MiInitializePfnForOtherProcess(
1931 IN PFN_NUMBER PageFrameIndex,
1932 IN PVOID PteAddress,
1933 IN PFN_NUMBER PteFrame
1934 );
1935
1936 VOID
1937 NTAPI
1938 MiDecrementShareCount(
1939 IN PMMPFN Pfn1,
1940 IN PFN_NUMBER PageFrameIndex
1941 );
1942
1943 PFN_NUMBER
1944 NTAPI
1945 MiRemoveAnyPage(
1946 IN ULONG Color
1947 );
1948
1949 PFN_NUMBER
1950 NTAPI
1951 MiRemoveZeroPage(
1952 IN ULONG Color
1953 );
1954
1955 VOID
1956 NTAPI
1957 MiZeroPhysicalPage(
1958 IN PFN_NUMBER PageFrameIndex
1959 );
1960
1961 VOID
1962 NTAPI
1963 MiInsertPageInFreeList(
1964 IN PFN_NUMBER PageFrameIndex
1965 );
1966
1967 PFN_COUNT
1968 NTAPI
1969 MiDeleteSystemPageableVm(
1970 IN PMMPTE PointerPte,
1971 IN PFN_NUMBER PageCount,
1972 IN ULONG Flags,
1973 OUT PPFN_NUMBER ValidPages
1974 );
1975
1976 ULONG
1977 NTAPI
1978 MiGetPageProtection(
1979 IN PMMPTE PointerPte
1980 );
1981
1982 PLDR_DATA_TABLE_ENTRY
1983 NTAPI
1984 MiLookupDataTableEntry(
1985 IN PVOID Address
1986 );
1987
1988 VOID
1989 NTAPI
1990 MiInitializeDriverLargePageList(
1991 VOID
1992 );
1993
1994 VOID
1995 NTAPI
1996 MiInitializeLargePageSupport(
1997 VOID
1998 );
1999
2000 VOID
2001 NTAPI
2002 MiSyncCachedRanges(
2003 VOID
2004 );
2005
2006 BOOLEAN
2007 NTAPI
2008 MiIsPfnInUse(
2009 IN PMMPFN Pfn1
2010 );
2011
2012 PMMVAD
2013 NTAPI
2014 MiLocateAddress(
2015 IN PVOID VirtualAddress
2016 );
2017
2018 TABLE_SEARCH_RESULT
2019 NTAPI
2020 MiCheckForConflictingNode(
2021 IN ULONG_PTR StartVpn,
2022 IN ULONG_PTR EndVpn,
2023 IN PMM_AVL_TABLE Table,
2024 OUT PMMADDRESS_NODE *NodeOrParent
2025 );
2026
2027 TABLE_SEARCH_RESULT
2028 NTAPI
2029 MiFindEmptyAddressRangeDownTree(
2030 IN SIZE_T Length,
2031 IN ULONG_PTR BoundaryAddress,
2032 IN ULONG_PTR Alignment,
2033 IN PMM_AVL_TABLE Table,
2034 OUT PULONG_PTR Base,
2035 OUT PMMADDRESS_NODE *Parent
2036 );
2037
2038 NTSTATUS
2039 NTAPI
2040 MiFindEmptyAddressRangeDownBasedTree(
2041 IN SIZE_T Length,
2042 IN ULONG_PTR BoundaryAddress,
2043 IN ULONG_PTR Alignment,
2044 IN PMM_AVL_TABLE Table,
2045 OUT PULONG_PTR Base
2046 );
2047
2048 TABLE_SEARCH_RESULT
2049 NTAPI
2050 MiFindEmptyAddressRangeInTree(
2051 IN SIZE_T Length,
2052 IN ULONG_PTR Alignment,
2053 IN PMM_AVL_TABLE Table,
2054 OUT PMMADDRESS_NODE *PreviousVad,
2055 OUT PULONG_PTR Base
2056 );
2057
2058 NTSTATUS
2059 NTAPI
2060 MiCheckSecuredVad(
2061 IN PMMVAD Vad,
2062 IN PVOID Base,
2063 IN SIZE_T Size,
2064 IN ULONG ProtectionMask
2065 );
2066
2067 NTSTATUS
2068 NTAPI
2069 MiInsertVadEx(
2070 _Inout_ PMMVAD Vad,
2071 _In_ ULONG_PTR *BaseAddress,
2072 _In_ SIZE_T ViewSize,
2073 _In_ ULONG_PTR HighestAddress,
2074 _In_ ULONG_PTR Alignment,
2075 _In_ ULONG AllocationType);
2076
2077 VOID
2078 NTAPI
2079 MiInsertBasedSection(
2080 IN PSECTION Section
2081 );
2082
2083 NTSTATUS
2084 NTAPI
2085 MiUnmapViewOfSection(
2086 IN PEPROCESS Process,
2087 IN PVOID BaseAddress,
2088 IN ULONG Flags
2089 );
2090
2091 NTSTATUS
2092 NTAPI
2093 MiRosUnmapViewOfSection(
2094 IN PEPROCESS Process,
2095 IN PVOID BaseAddress,
2096 IN ULONG Flags
2097 );
2098
2099 VOID
2100 NTAPI
2101 MiInsertNode(
2102 IN PMM_AVL_TABLE Table,
2103 IN PMMADDRESS_NODE NewNode,
2104 PMMADDRESS_NODE Parent,
2105 TABLE_SEARCH_RESULT Result
2106 );
2107
2108 VOID
2109 NTAPI
2110 MiRemoveNode(
2111 IN PMMADDRESS_NODE Node,
2112 IN PMM_AVL_TABLE Table
2113 );
2114
2115 PMMADDRESS_NODE
2116 NTAPI
2117 MiGetPreviousNode(
2118 IN PMMADDRESS_NODE Node
2119 );
2120
2121 PMMADDRESS_NODE
2122 NTAPI
2123 MiGetNextNode(
2124 IN PMMADDRESS_NODE Node
2125 );
2126
2127 BOOLEAN
2128 NTAPI
2129 MiInitializeSystemSpaceMap(
2130 IN PMMSESSION InputSession OPTIONAL
2131 );
2132
2133 VOID
2134 NTAPI
2135 MiSessionRemoveProcess(
2136 VOID
2137 );
2138
2139 VOID
2140 NTAPI
2141 MiReleaseProcessReferenceToSessionDataPage(
2142 IN PMM_SESSION_SPACE SessionGlobal
2143 );
2144
2145 VOID
2146 NTAPI
2147 MiSessionAddProcess(
2148 IN PEPROCESS NewProcess
2149 );
2150
2151 NTSTATUS
2152 NTAPI
2153 MiSessionCommitPageTables(
2154 IN PVOID StartVa,
2155 IN PVOID EndVa
2156 );
2157
2158 ULONG
2159 NTAPI
2160 MiMakeProtectionMask(
2161 IN ULONG Protect
2162 );
2163
2164 VOID
2165 NTAPI
2166 MiDeleteVirtualAddresses(
2167 IN ULONG_PTR Va,
2168 IN ULONG_PTR EndingAddress,
2169 IN PMMVAD Vad
2170 );
2171
2172 ULONG
2173 NTAPI
2174 MiMakeSystemAddressValid(
2175 IN PVOID PageTableVirtualAddress,
2176 IN PEPROCESS CurrentProcess
2177 );
2178
2179 ULONG
2180 NTAPI
2181 MiMakeSystemAddressValidPfn(
2182 IN PVOID VirtualAddress,
2183 IN KIRQL OldIrql
2184 );
2185
2186 VOID
2187 NTAPI
2188 MiRemoveMappedView(
2189 IN PEPROCESS CurrentProcess,
2190 IN PMMVAD Vad
2191 );
2192
2193 PSUBSECTION
2194 NTAPI
2195 MiLocateSubsection(
2196 IN PMMVAD Vad,
2197 IN ULONG_PTR Vpn
2198 );
2199
2200 VOID
2201 NTAPI
2202 MiDeleteARM3Section(
2203 PVOID ObjectBody
2204 );
2205
2206 NTSTATUS
2207 NTAPI
2208 MiQueryMemorySectionName(
2209 IN HANDLE ProcessHandle,
2210 IN PVOID BaseAddress,
2211 OUT PVOID MemoryInformation,
2212 IN SIZE_T MemoryInformationLength,
2213 OUT PSIZE_T ReturnLength
2214 );
2215
2216 NTSTATUS
2217 NTAPI
2218 MiRosUnmapViewInSystemSpace(
2219 IN PVOID MappedBase
2220 );
2221
2222 POOL_TYPE
2223 NTAPI
2224 MmDeterminePoolType(
2225 IN PVOID PoolAddress
2226 );
2227
2228 VOID
2229 NTAPI
2230 MiMakePdeExistAndMakeValid(
2231 IN PMMPDE PointerPde,
2232 IN PEPROCESS TargetProcess,
2233 IN KIRQL OldIrql
2234 );
2235
2236 //
2237 // MiRemoveZeroPage will use inline code to zero out the page manually if only
2238 // free pages are available. In some scenarios, we don't/can't run that piece of
2239 // code and would rather only have a real zero page. If we can't have a zero page,
2240 // then we'd like to have our own code to grab a free page and zero it out, by
2241 // using MiRemoveAnyPage. This macro implements this.
2242 //
2243 FORCEINLINE
2244 PFN_NUMBER
2245 MiRemoveZeroPageSafe(IN ULONG Color)
2246 {
2247 if (MmFreePagesByColor[ZeroedPageList][Color].Flink != LIST_HEAD) return MiRemoveZeroPage(Color);
2248 return 0;
2249 }
2250
2251 /* EOF */