2 * PROJECT: ReactOS Kernel
3 * LICENSE: BSD - See COPYING.ARM in the top level directory
4 * FILE: ntoskrnl/mm/ARM3/pagfault.c
5 * PURPOSE: ARM Memory Manager Page Fault Handling
6 * PROGRAMMERS: ReactOS Portable Systems Group
9 /* INCLUDES *******************************************************************/
15 #define MODULE_INVOLVED_IN_ARM3
16 #include <mm/ARM3/miarm.h>
18 /* GLOBALS ********************************************************************/
20 #define HYDRA_PROCESS (PEPROCESS)1
22 BOOLEAN UserPdeFault
= FALSE
;
25 /* PRIVATE FUNCTIONS **********************************************************/
29 MiCheckForUserStackOverflow(IN PVOID Address
,
30 IN PVOID TrapInformation
)
32 PETHREAD CurrentThread
= PsGetCurrentThread();
33 PTEB Teb
= CurrentThread
->Tcb
.Teb
;
34 PVOID StackBase
, DeallocationStack
, NextStackAddress
;
38 /* Do we own the address space lock? */
39 if (CurrentThread
->AddressSpaceOwner
== 1)
41 /* This isn't valid */
42 DPRINT1("Process owns address space lock\n");
43 ASSERT(KeAreAllApcsDisabled() == TRUE
);
44 return STATUS_GUARD_PAGE_VIOLATION
;
47 /* Are we attached? */
48 if (KeIsAttachedProcess())
50 /* This isn't valid */
51 DPRINT1("Process is attached\n");
52 return STATUS_GUARD_PAGE_VIOLATION
;
55 /* Read the current settings */
56 StackBase
= Teb
->NtTib
.StackBase
;
57 DeallocationStack
= Teb
->DeallocationStack
;
58 GuranteedSize
= Teb
->GuaranteedStackBytes
;
59 DPRINT("Handling guard page fault with Stacks Addresses 0x%p and 0x%p, guarantee: %lx\n",
60 StackBase
, DeallocationStack
, GuranteedSize
);
62 /* Guarantees make this code harder, for now, assume there aren't any */
63 ASSERT(GuranteedSize
== 0);
65 /* So allocate only the minimum guard page size */
66 GuranteedSize
= PAGE_SIZE
;
68 /* Does this faulting stack address actually exist in the stack? */
69 if ((Address
>= StackBase
) || (Address
< DeallocationStack
))
72 DPRINT1("Faulting address outside of stack bounds. Address=%p, StackBase=%p, DeallocationStack=%p\n",
73 Address
, StackBase
, DeallocationStack
);
74 return STATUS_GUARD_PAGE_VIOLATION
;
77 /* This is where the stack will start now */
78 NextStackAddress
= (PVOID
)((ULONG_PTR
)PAGE_ALIGN(Address
) - GuranteedSize
);
80 /* Do we have at least one page between here and the end of the stack? */
81 if (((ULONG_PTR
)NextStackAddress
- PAGE_SIZE
) <= (ULONG_PTR
)DeallocationStack
)
83 /* We don't -- Windows would try to make this guard page valid now */
84 DPRINT1("Close to our death...\n");
85 return STATUS_STACK_OVERFLOW
;
88 /* Don't handle this flag yet */
89 ASSERT((PsGetCurrentProcess()->Peb
->NtGlobalFlag
& FLG_DISABLE_STACK_EXTENSION
) == 0);
91 /* Update the stack limit */
92 Teb
->NtTib
.StackLimit
= (PVOID
)((ULONG_PTR
)NextStackAddress
+ GuranteedSize
);
94 /* Now move the guard page to the next page */
95 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
100 PAGE_READWRITE
| PAGE_GUARD
);
101 if ((NT_SUCCESS(Status
) || (Status
== STATUS_ALREADY_COMMITTED
)))
104 DPRINT("Guard page handled successfully for %p\n", Address
);
105 return STATUS_PAGE_FAULT_GUARD_PAGE
;
108 /* Fail, we couldn't move the guard page */
109 DPRINT1("Guard page failure: %lx\n", Status
);
111 return STATUS_STACK_OVERFLOW
;
117 _In_ ULONG ProtectionMask
,
119 _In_ BOOLEAN Execute
)
121 #define _BYTE_MASK(Bit0, Bit1, Bit2, Bit3, Bit4, Bit5, Bit6, Bit7) \
122 (Bit0) | ((Bit1) << 1) | ((Bit2) << 2) | ((Bit3) << 3) | \
123 ((Bit4) << 4) | ((Bit5) << 5) | ((Bit6) << 6) | ((Bit7) << 7)
124 static const UCHAR AccessAllowedMask
[2][2] =
126 { // Protect 0 1 2 3 4 5 6 7
127 _BYTE_MASK(0, 1, 1, 1, 1, 1, 1, 1), // READ
128 _BYTE_MASK(0, 0, 1, 1, 0, 0, 1, 1), // EXECUTE READ
131 _BYTE_MASK(0, 0, 0, 0, 1, 1, 1, 1), // WRITE
132 _BYTE_MASK(0, 0, 0, 0, 0, 0, 1, 1), // EXECUTE WRITE
136 /* We want only the lower access bits */
137 ProtectionMask
&= MM_PROTECT_ACCESS
;
139 /* Look it up in the table */
140 return (AccessAllowedMask
[Write
!= 0][Execute
!= 0] >> ProtectionMask
) & 1;
145 MiAccessCheck(IN PMMPTE PointerPte
,
146 IN BOOLEAN StoreInstruction
,
147 IN KPROCESSOR_MODE PreviousMode
,
148 IN ULONG_PTR ProtectionMask
,
154 /* Check for invalid user-mode access */
155 if ((PreviousMode
== UserMode
) && (PointerPte
> MiHighestUserPte
))
157 return STATUS_ACCESS_VIOLATION
;
160 /* Capture the PTE -- is it valid? */
161 TempPte
= *PointerPte
;
162 if (TempPte
.u
.Hard
.Valid
)
164 /* Was someone trying to write to it? */
165 if (StoreInstruction
)
168 if (MI_IS_PAGE_WRITEABLE(&TempPte
) ||
169 MI_IS_PAGE_COPY_ON_WRITE(&TempPte
))
171 /* Then there's nothing to worry about */
172 return STATUS_SUCCESS
;
175 /* Oops! This isn't allowed */
176 return STATUS_ACCESS_VIOLATION
;
179 /* Someone was trying to read from a valid PTE, that's fine too */
180 return STATUS_SUCCESS
;
183 /* Check if the protection on the page allows what is being attempted */
184 if (!MiIsAccessAllowed(ProtectionMask
, StoreInstruction
, FALSE
))
186 return STATUS_ACCESS_VIOLATION
;
189 /* Check if this is a guard page */
190 if ((ProtectionMask
& MM_PROTECT_SPECIAL
) == MM_GUARDPAGE
)
192 ASSERT(ProtectionMask
!= MM_DECOMMIT
);
194 /* Attached processes can't expand their stack */
195 if (KeIsAttachedProcess()) return STATUS_ACCESS_VIOLATION
;
197 /* No support for prototype PTEs yet */
198 ASSERT(TempPte
.u
.Soft
.Prototype
== 0);
200 /* Remove the guard page bit, and return a guard page violation */
201 TempPte
.u
.Soft
.Protection
= ProtectionMask
& ~MM_GUARDPAGE
;
202 ASSERT(TempPte
.u
.Long
!= 0);
203 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
204 return STATUS_GUARD_PAGE_VIOLATION
;
208 return STATUS_SUCCESS
;
213 MiCheckVirtualAddress(IN PVOID VirtualAddress
,
214 OUT PULONG ProtectCode
,
215 OUT PMMVAD
*ProtoVad
)
220 /* No prototype/section support for now */
223 /* User or kernel fault? */
224 if (VirtualAddress
<= MM_HIGHEST_USER_ADDRESS
)
226 /* Special case for shared data */
227 if (PAGE_ALIGN(VirtualAddress
) == (PVOID
)MM_SHARED_USER_DATA_VA
)
229 /* It's a read-only page */
230 *ProtectCode
= MM_READONLY
;
231 return MmSharedUserDataPte
;
234 /* Find the VAD, it might not exist if the address is bogus */
235 Vad
= MiLocateAddress(VirtualAddress
);
238 /* Bogus virtual address */
239 *ProtectCode
= MM_NOACCESS
;
243 /* ReactOS does not handle physical memory VADs yet */
244 ASSERT(Vad
->u
.VadFlags
.VadType
!= VadDevicePhysicalMemory
);
246 /* Check if it's a section, or just an allocation */
247 if (Vad
->u
.VadFlags
.PrivateMemory
)
249 /* ReactOS does not handle AWE VADs yet */
250 ASSERT(Vad
->u
.VadFlags
.VadType
!= VadAwe
);
252 /* This must be a TEB/PEB VAD */
253 if (Vad
->u
.VadFlags
.MemCommit
)
255 /* It's committed, so return the VAD protection */
256 *ProtectCode
= (ULONG
)Vad
->u
.VadFlags
.Protection
;
260 /* It has not yet been committed, so return no access */
261 *ProtectCode
= MM_NOACCESS
;
264 /* In both cases, return no PTE */
269 /* ReactOS does not supoprt these VADs yet */
270 ASSERT(Vad
->u
.VadFlags
.VadType
!= VadImageMap
);
271 ASSERT(Vad
->u2
.VadFlags2
.ExtendableFile
== 0);
273 /* Return the proto VAD */
276 /* Get the prototype PTE for this page */
277 PointerPte
= (((ULONG_PTR
)VirtualAddress
>> PAGE_SHIFT
) - Vad
->StartingVpn
) + Vad
->FirstPrototypePte
;
278 ASSERT(PointerPte
!= NULL
);
279 ASSERT(PointerPte
<= Vad
->LastContiguousPte
);
281 /* Return the Prototype PTE and the protection for the page mapping */
282 *ProtectCode
= (ULONG
)Vad
->u
.VadFlags
.Protection
;
286 else if (MI_IS_PAGE_TABLE_ADDRESS(VirtualAddress
))
288 /* This should never happen, as these addresses are handled by the double-maping */
289 if (((PMMPTE
)VirtualAddress
>= MiAddressToPte(MmPagedPoolStart
)) &&
290 ((PMMPTE
)VirtualAddress
<= MmPagedPoolInfo
.LastPteForPagedPool
))
292 /* Fail such access */
293 *ProtectCode
= MM_NOACCESS
;
297 /* Return full access rights */
298 *ProtectCode
= MM_READWRITE
;
301 else if (MI_IS_SESSION_ADDRESS(VirtualAddress
))
303 /* ReactOS does not have an image list yet, so bail out to failure case */
304 ASSERT(IsListEmpty(&MmSessionSpace
->ImageList
));
307 /* Default case -- failure */
308 *ProtectCode
= MM_NOACCESS
;
312 #if (_MI_PAGING_LEVELS == 2)
315 MiSynchronizeSystemPde(PMMPDE PointerPde
)
320 /* Get the Index from the PDE */
321 Index
= ((ULONG_PTR
)PointerPde
& (SYSTEM_PD_SIZE
- 1)) / sizeof(MMPTE
);
323 /* Copy the PDE from the double-mapped system page directory */
324 SystemPde
= MmSystemPagePtes
[Index
];
325 *PointerPde
= SystemPde
;
327 /* Make sure we re-read the PDE and PTE */
328 KeMemoryBarrierWithoutFence();
330 /* Return, if we had success */
331 return (BOOLEAN
)SystemPde
.u
.Hard
.Valid
;
336 MiCheckPdeForSessionSpace(IN PVOID Address
)
340 PVOID SessionAddress
;
343 /* Is this a session PTE? */
344 if (MI_IS_SESSION_PTE(Address
))
346 /* Make sure the PDE for session space is valid */
347 PointerPde
= MiAddressToPde(MmSessionSpace
);
348 if (!PointerPde
->u
.Hard
.Valid
)
350 /* This means there's no valid session, bail out */
351 DbgPrint("MiCheckPdeForSessionSpace: No current session for PTE %p\n",
354 return STATUS_ACCESS_VIOLATION
;
357 /* Now get the session-specific page table for this address */
358 SessionAddress
= MiPteToAddress(Address
);
359 PointerPde
= MiAddressToPte(Address
);
360 if (PointerPde
->u
.Hard
.Valid
) return STATUS_WAIT_1
;
362 /* It's not valid, so find it in the page table array */
363 Index
= ((ULONG_PTR
)SessionAddress
- (ULONG_PTR
)MmSessionBase
) >> 22;
364 TempPde
.u
.Long
= MmSessionSpace
->PageTables
[Index
].u
.Long
;
365 if (TempPde
.u
.Hard
.Valid
)
367 /* The copy is valid, so swap it in */
368 InterlockedExchange((PLONG
)PointerPde
, TempPde
.u
.Long
);
369 return STATUS_WAIT_1
;
372 /* We don't seem to have allocated a page table for this address yet? */
373 DbgPrint("MiCheckPdeForSessionSpace: No Session PDE for PTE %p, %p\n",
374 PointerPde
->u
.Long
, SessionAddress
);
376 return STATUS_ACCESS_VIOLATION
;
379 /* Is the address also a session address? If not, we're done */
380 if (!MI_IS_SESSION_ADDRESS(Address
)) return STATUS_SUCCESS
;
382 /* It is, so again get the PDE for session space */
383 PointerPde
= MiAddressToPde(MmSessionSpace
);
384 if (!PointerPde
->u
.Hard
.Valid
)
386 /* This means there's no valid session, bail out */
387 DbgPrint("MiCheckPdeForSessionSpace: No current session for VA %p\n",
390 return STATUS_ACCESS_VIOLATION
;
393 /* Now get the PDE for the address itself */
394 PointerPde
= MiAddressToPde(Address
);
395 if (!PointerPde
->u
.Hard
.Valid
)
397 /* Do the swap, we should be good to go */
398 Index
= ((ULONG_PTR
)Address
- (ULONG_PTR
)MmSessionBase
) >> 22;
399 PointerPde
->u
.Long
= MmSessionSpace
->PageTables
[Index
].u
.Long
;
400 if (PointerPde
->u
.Hard
.Valid
) return STATUS_WAIT_1
;
402 /* We had not allocated a page table for this session address yet, fail! */
403 DbgPrint("MiCheckPdeForSessionSpace: No Session PDE for VA %p, %p\n",
404 PointerPde
->u
.Long
, Address
);
406 return STATUS_ACCESS_VIOLATION
;
409 /* It's valid, so there's nothing to do */
410 return STATUS_SUCCESS
;
415 MiCheckPdeForPagedPool(IN PVOID Address
)
418 NTSTATUS Status
= STATUS_SUCCESS
;
420 /* Check session PDE */
421 if (MI_IS_SESSION_ADDRESS(Address
)) return MiCheckPdeForSessionSpace(Address
);
422 if (MI_IS_SESSION_PTE(Address
)) return MiCheckPdeForSessionSpace(Address
);
425 // Check if this is a fault while trying to access the page table itself
427 if (MI_IS_SYSTEM_PAGE_TABLE_ADDRESS(Address
))
430 // Send a hint to the page fault handler that this is only a valid fault
431 // if we already detected this was access within the page table range
433 PointerPde
= (PMMPDE
)MiAddressToPte(Address
);
434 Status
= STATUS_WAIT_1
;
436 else if (Address
< MmSystemRangeStart
)
439 // This is totally illegal
441 return STATUS_ACCESS_VIOLATION
;
446 // Get the PDE for the address
448 PointerPde
= MiAddressToPde(Address
);
452 // Check if it's not valid
454 if (PointerPde
->u
.Hard
.Valid
== 0)
457 // Copy it from our double-mapped system page directory
459 InterlockedExchangePte(PointerPde
,
460 MmSystemPagePtes
[((ULONG_PTR
)PointerPde
& (SYSTEM_PD_SIZE
- 1)) / sizeof(MMPTE
)].u
.Long
);
471 MiCheckPdeForPagedPool(IN PVOID Address
)
473 return STATUS_ACCESS_VIOLATION
;
479 MiZeroPfn(IN PFN_NUMBER PageFrameNumber
)
486 /* Get the PFN for this page */
487 Pfn1
= MiGetPfnEntry(PageFrameNumber
);
490 /* Grab a system PTE we can use to zero the page */
491 ZeroPte
= MiReserveSystemPtes(1, SystemPteSpace
);
494 /* Initialize the PTE for it */
495 TempPte
= ValidKernelPte
;
496 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameNumber
;
499 if (Pfn1
->u3
.e1
.CacheAttribute
== MiWriteCombined
)
501 /* Write combining, no caching */
502 MI_PAGE_DISABLE_CACHE(&TempPte
);
503 MI_PAGE_WRITE_COMBINED(&TempPte
);
505 else if (Pfn1
->u3
.e1
.CacheAttribute
== MiNonCached
)
507 /* Write through, no caching */
508 MI_PAGE_DISABLE_CACHE(&TempPte
);
509 MI_PAGE_WRITE_THROUGH(&TempPte
);
512 /* Make the system PTE valid with our PFN */
513 MI_WRITE_VALID_PTE(ZeroPte
, TempPte
);
515 /* Get the address it maps to, and zero it out */
516 ZeroAddress
= MiPteToAddress(ZeroPte
);
517 KeZeroPages(ZeroAddress
, PAGE_SIZE
);
519 /* Now get rid of it */
520 MiReleaseSystemPtes(ZeroPte
, 1, SystemPteSpace
);
526 _In_ PFN_NUMBER DestPage
,
527 _In_ PFN_NUMBER SrcPage
)
531 PMMPFN DestPfn
, SrcPfn
;
533 const VOID
* SrcAddress
;
536 DestPfn
= MiGetPfnEntry(DestPage
);
538 SrcPfn
= MiGetPfnEntry(SrcPage
);
541 /* Grab 2 system PTEs */
542 SysPtes
= MiReserveSystemPtes(2, SystemPteSpace
);
545 /* Initialize the destination PTE */
546 TempPte
= ValidKernelPte
;
547 TempPte
.u
.Hard
.PageFrameNumber
= DestPage
;
550 if (DestPfn
->u3
.e1
.CacheAttribute
== MiWriteCombined
)
552 /* Write combining, no caching */
553 MI_PAGE_DISABLE_CACHE(&TempPte
);
554 MI_PAGE_WRITE_COMBINED(&TempPte
);
556 else if (DestPfn
->u3
.e1
.CacheAttribute
== MiNonCached
)
558 /* Write through, no caching */
559 MI_PAGE_DISABLE_CACHE(&TempPte
);
560 MI_PAGE_WRITE_THROUGH(&TempPte
);
563 /* Make the system PTE valid with our PFN */
564 MI_WRITE_VALID_PTE(&SysPtes
[0], TempPte
);
566 /* Initialize the source PTE */
567 TempPte
= ValidKernelPte
;
568 TempPte
.u
.Hard
.PageFrameNumber
= SrcPage
;
571 if (SrcPfn
->u3
.e1
.CacheAttribute
== MiNonCached
)
573 MI_PAGE_DISABLE_CACHE(&TempPte
);
576 /* Make the system PTE valid with our PFN */
577 MI_WRITE_VALID_PTE(&SysPtes
[1], TempPte
);
579 /* Get the addresses and perform the copy */
580 DestAddress
= MiPteToAddress(&SysPtes
[0]);
581 SrcAddress
= MiPteToAddress(&SysPtes
[1]);
582 RtlCopyMemory(DestAddress
, SrcAddress
, PAGE_SIZE
);
584 /* Now get rid of it */
585 MiReleaseSystemPtes(SysPtes
, 2, SystemPteSpace
);
590 MiResolveDemandZeroFault(IN PVOID Address
,
591 IN PMMPTE PointerPte
,
592 IN PEPROCESS Process
,
595 PFN_NUMBER PageFrameNumber
= 0;
597 BOOLEAN NeedZero
= FALSE
, HaveLock
= FALSE
;
600 DPRINT("ARM3 Demand Zero Page Fault Handler for address: %p in process: %p\n",
604 /* Must currently only be called by paging path */
605 if ((Process
> HYDRA_PROCESS
) && (OldIrql
== MM_NOIRQL
))
608 ASSERT(MI_IS_PAGE_TABLE_ADDRESS(PointerPte
));
611 ASSERT(Process
->ForkInProgress
== NULL
);
613 /* Get process color */
614 Color
= MI_GET_NEXT_PROCESS_COLOR(Process
);
615 ASSERT(Color
!= 0xFFFFFFFF);
617 /* We'll need a zero page */
622 /* Check if we need a zero page */
623 NeedZero
= (OldIrql
!= MM_NOIRQL
);
625 /* Session-backed image views must be zeroed */
626 if ((Process
== HYDRA_PROCESS
) &&
627 ((MI_IS_SESSION_IMAGE_ADDRESS(Address
)) ||
628 ((Address
>= MiSessionViewStart
) && (Address
< MiSessionSpaceWs
))))
633 /* Hardcode unknown color */
637 /* Check if the PFN database should be acquired */
638 if (OldIrql
== MM_NOIRQL
)
640 /* Acquire it and remember we should release it after */
641 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
645 /* We either manually locked the PFN DB, or already came with it locked */
646 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
647 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
649 /* Assert we have enough pages */
650 ASSERT(MmAvailablePages
>= 32);
653 if (UserPdeFault
) MI_SET_USAGE(MI_USAGE_PAGE_TABLE
);
654 if (!UserPdeFault
) MI_SET_USAGE(MI_USAGE_DEMAND_ZERO
);
656 if (Process
== HYDRA_PROCESS
) MI_SET_PROCESS2("Hydra");
657 else if (Process
) MI_SET_PROCESS2(Process
->ImageFileName
);
658 else MI_SET_PROCESS2("Kernel Demand 0");
660 /* Do we need a zero page? */
661 if (Color
!= 0xFFFFFFFF)
663 /* Try to get one, if we couldn't grab a free page and zero it */
664 PageFrameNumber
= MiRemoveZeroPageSafe(Color
);
665 if (!PageFrameNumber
)
667 /* We'll need a free page and zero it manually */
668 PageFrameNumber
= MiRemoveAnyPage(Color
);
674 /* Get a color, and see if we should grab a zero or non-zero page */
675 Color
= MI_GET_NEXT_COLOR();
678 /* Process or system doesn't want a zero page, grab anything */
679 PageFrameNumber
= MiRemoveAnyPage(Color
);
683 /* System wants a zero page, obtain one */
684 PageFrameNumber
= MiRemoveZeroPage(Color
);
689 MiInitializePfn(PageFrameNumber
, PointerPte
, TRUE
);
691 /* Do we have the lock? */
695 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
697 /* Update performance counters */
698 if (Process
> HYDRA_PROCESS
) Process
->NumberOfPrivatePages
++;
701 /* Increment demand zero faults */
702 InterlockedIncrement(&KeGetCurrentPrcb()->MmDemandZeroCount
);
704 /* Zero the page if need be */
705 if (NeedZero
) MiZeroPfn(PageFrameNumber
);
707 /* Fault on user PDE, or fault on user PTE? */
708 if (PointerPte
<= MiHighestUserPte
)
710 /* User fault, build a user PTE */
711 MI_MAKE_HARDWARE_PTE_USER(&TempPte
,
713 PointerPte
->u
.Soft
.Protection
,
718 /* This is a user-mode PDE, create a kernel PTE for it */
719 MI_MAKE_HARDWARE_PTE(&TempPte
,
721 PointerPte
->u
.Soft
.Protection
,
725 /* Set it dirty if it's a writable page */
726 if (MI_IS_PAGE_WRITEABLE(&TempPte
)) MI_MAKE_DIRTY_PAGE(&TempPte
);
729 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
731 /* Did we manually acquire the lock */
734 /* Get the PFN entry */
735 Pfn1
= MI_PFN_ELEMENT(PageFrameNumber
);
737 /* Windows does these sanity checks */
738 ASSERT(Pfn1
->u1
.Event
== 0);
739 ASSERT(Pfn1
->u3
.e1
.PrototypePte
== 0);
745 DPRINT("Demand zero page has now been paged in\n");
746 return STATUS_PAGE_FAULT_DEMAND_ZERO
;
751 MiCompleteProtoPteFault(IN BOOLEAN StoreInstruction
,
753 IN PMMPTE PointerPte
,
754 IN PMMPTE PointerProtoPte
,
756 IN PMMPFN
* LockedProtoPfn
)
759 PMMPTE OriginalPte
, PageTablePte
;
760 ULONG_PTR Protection
;
761 PFN_NUMBER PageFrameIndex
;
763 BOOLEAN OriginalProtection
, DirtyPage
;
765 /* Must be called with an valid prototype PTE, with the PFN lock held */
766 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
767 ASSERT(PointerProtoPte
->u
.Hard
.Valid
== 1);
770 PageFrameIndex
= PFN_FROM_PTE(PointerProtoPte
);
772 /* Get the PFN entry and set it as a prototype PTE */
773 Pfn1
= MiGetPfnEntry(PageFrameIndex
);
774 Pfn1
->u3
.e1
.PrototypePte
= 1;
776 /* Increment the share count for the page table */
777 PageTablePte
= MiAddressToPte(PointerPte
);
778 Pfn2
= MiGetPfnEntry(PageTablePte
->u
.Hard
.PageFrameNumber
);
779 Pfn2
->u2
.ShareCount
++;
781 /* Check where we should be getting the protection information from */
782 if (PointerPte
->u
.Soft
.PageFileHigh
== MI_PTE_LOOKUP_NEEDED
)
784 /* Get the protection from the PTE, there's no real Proto PTE data */
785 Protection
= PointerPte
->u
.Soft
.Protection
;
787 /* Remember that we did not use the proto protection */
788 OriginalProtection
= FALSE
;
792 /* Get the protection from the original PTE link */
793 OriginalPte
= &Pfn1
->OriginalPte
;
794 Protection
= OriginalPte
->u
.Soft
.Protection
;
796 /* Remember that we used the original protection */
797 OriginalProtection
= TRUE
;
799 /* Check if this was a write on a read only proto */
800 if ((StoreInstruction
) && !(Protection
& MM_READWRITE
))
803 StoreInstruction
= 0;
807 /* Check if this was a write on a non-COW page */
809 if ((StoreInstruction
) && ((Protection
& MM_WRITECOPY
) != MM_WRITECOPY
))
811 /* Then the page should be marked dirty */
815 ASSERT(Pfn1
->OriginalPte
.u
.Soft
.Prototype
!= 0);
818 /* Did we get a locked incoming PFN? */
821 /* Drop a reference */
822 ASSERT((*LockedProtoPfn
)->u3
.e2
.ReferenceCount
>= 1);
823 MiDereferencePfnAndDropLockCount(*LockedProtoPfn
);
824 *LockedProtoPfn
= NULL
;
827 /* Release the PFN lock */
828 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
830 /* Remove special/caching bits */
831 Protection
&= ~MM_PROTECT_SPECIAL
;
834 if (Pfn1
->u3
.e1
.CacheAttribute
== MiWriteCombined
)
836 /* Write combining, no caching */
837 MI_PAGE_DISABLE_CACHE(&TempPte
);
838 MI_PAGE_WRITE_COMBINED(&TempPte
);
840 else if (Pfn1
->u3
.e1
.CacheAttribute
== MiNonCached
)
842 /* Write through, no caching */
843 MI_PAGE_DISABLE_CACHE(&TempPte
);
844 MI_PAGE_WRITE_THROUGH(&TempPte
);
847 /* Check if this is a kernel or user address */
848 if (Address
< MmSystemRangeStart
)
850 /* Build the user PTE */
851 MI_MAKE_HARDWARE_PTE_USER(&TempPte
, PointerPte
, Protection
, PageFrameIndex
);
855 /* Build the kernel PTE */
856 MI_MAKE_HARDWARE_PTE(&TempPte
, PointerPte
, Protection
, PageFrameIndex
);
859 /* Set the dirty flag if needed */
860 if (DirtyPage
) MI_MAKE_DIRTY_PAGE(&TempPte
);
863 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
865 /* Reset the protection if needed */
866 if (OriginalProtection
) Protection
= MM_ZERO_ACCESS
;
869 ASSERT(PointerPte
== MiAddressToPte(Address
));
870 return STATUS_SUCCESS
;
875 MiResolvePageFileFault(_In_ BOOLEAN StoreInstruction
,
876 _In_ PVOID FaultingAddress
,
877 _In_ PMMPTE PointerPte
,
878 _In_ PEPROCESS CurrentProcess
,
879 _Inout_ KIRQL
*OldIrql
)
884 MMPTE TempPte
= *PointerPte
;
886 ULONG PageFileIndex
= TempPte
.u
.Soft
.PageFileLow
;
887 ULONG_PTR PageFileOffset
= TempPte
.u
.Soft
.PageFileHigh
;
888 ULONG Protection
= TempPte
.u
.Soft
.Protection
;
890 /* Things we don't support yet */
891 ASSERT(CurrentProcess
> HYDRA_PROCESS
);
892 ASSERT(*OldIrql
!= MM_NOIRQL
);
894 /* We must hold the PFN lock */
895 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
897 /* Some sanity checks */
898 ASSERT(TempPte
.u
.Hard
.Valid
== 0);
899 ASSERT(TempPte
.u
.Soft
.PageFileHigh
!= 0);
900 ASSERT(TempPte
.u
.Soft
.PageFileHigh
!= MI_PTE_LOOKUP_NEEDED
);
902 /* Get any page, it will be overwritten */
903 Color
= MI_GET_NEXT_PROCESS_COLOR(CurrentProcess
);
904 Page
= MiRemoveAnyPage(Color
);
906 /* Initialize this PFN */
907 MiInitializePfn(Page
, PointerPte
, StoreInstruction
);
909 /* Sets the PFN as being in IO operation */
910 Pfn1
= MI_PFN_ELEMENT(Page
);
911 ASSERT(Pfn1
->u1
.Event
== NULL
);
912 ASSERT(Pfn1
->u3
.e1
.ReadInProgress
== 0);
913 ASSERT(Pfn1
->u3
.e1
.WriteInProgress
== 0);
914 Pfn1
->u3
.e1
.ReadInProgress
= 1;
916 /* We must write the PTE now as the PFN lock will be released while performing the IO operation */
917 MI_MAKE_TRANSITION_PTE(&TempPte
, Page
, Protection
);
919 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
921 /* Release the PFN lock while we proceed */
922 KeReleaseQueuedSpinLock(LockQueuePfnLock
, *OldIrql
);
924 /* Do the paging IO */
925 Status
= MiReadPageFile(Page
, PageFileIndex
, PageFileOffset
);
927 /* Lock the PFN database again */
928 *OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
930 /* Nobody should have changed that while we were not looking */
931 ASSERT(Pfn1
->u3
.e1
.ReadInProgress
== 1);
932 ASSERT(Pfn1
->u3
.e1
.WriteInProgress
== 0);
934 if (!NT_SUCCESS(Status
))
938 Pfn1
->u4
.InPageError
= 1;
939 Pfn1
->u1
.ReadStatus
= Status
;
942 /* And the PTE can finally be valid */
943 MI_MAKE_HARDWARE_PTE(&TempPte
, PointerPte
, Protection
, Page
);
944 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
946 Pfn1
->u3
.e1
.ReadInProgress
= 0;
947 /* Did someone start to wait on us while we proceeded ? */
950 /* Tell them we're done */
951 KeSetEvent(Pfn1
->u1
.Event
, IO_NO_INCREMENT
, FALSE
);
959 MiResolveTransitionFault(IN BOOLEAN StoreInstruction
,
960 IN PVOID FaultingAddress
,
961 IN PMMPTE PointerPte
,
962 IN PEPROCESS CurrentProcess
,
964 OUT PKEVENT
**InPageBlock
)
966 PFN_NUMBER PageFrameIndex
;
969 PMMPTE PointerToPteForProtoPage
;
970 DPRINT("Transition fault on 0x%p with PTE 0x%p in process %s\n",
971 FaultingAddress
, PointerPte
, CurrentProcess
->ImageFileName
);
973 /* Windowss does this check */
974 ASSERT(*InPageBlock
== NULL
);
976 /* ARM3 doesn't support this path */
977 ASSERT(OldIrql
!= MM_NOIRQL
);
979 /* Capture the PTE and make sure it's in transition format */
980 TempPte
= *PointerPte
;
981 ASSERT((TempPte
.u
.Soft
.Valid
== 0) &&
982 (TempPte
.u
.Soft
.Prototype
== 0) &&
983 (TempPte
.u
.Soft
.Transition
== 1));
985 /* Get the PFN and the PFN entry */
986 PageFrameIndex
= TempPte
.u
.Trans
.PageFrameNumber
;
987 DPRINT("Transition PFN: %lx\n", PageFrameIndex
);
988 Pfn1
= MiGetPfnEntry(PageFrameIndex
);
990 /* One more transition fault! */
991 InterlockedIncrement(&KeGetCurrentPrcb()->MmTransitionCount
);
993 /* This is from ARM3 -- Windows normally handles this here */
994 ASSERT(Pfn1
->u4
.InPageError
== 0);
996 /* See if we should wait before terminating the fault */
997 if ((Pfn1
->u3
.e1
.ReadInProgress
== 1)
998 || ((Pfn1
->u3
.e1
.WriteInProgress
== 1) && StoreInstruction
))
1000 DPRINT1("The page is currently in a page transition !\n");
1001 *InPageBlock
= &Pfn1
->u1
.Event
;
1002 if (PointerPte
== Pfn1
->PteAddress
)
1004 DPRINT1("And this if for this particular PTE.\n");
1005 /* The PTE will be made valid by the thread serving the fault */
1006 return STATUS_SUCCESS
; // FIXME: Maybe something more descriptive
1010 /* Windows checks there's some free pages and this isn't an in-page error */
1011 ASSERT(MmAvailablePages
> 0);
1012 ASSERT(Pfn1
->u4
.InPageError
== 0);
1014 /* ReactOS checks for this */
1015 ASSERT(MmAvailablePages
> 32);
1017 /* Was this a transition page in the valid list, or free/zero list? */
1018 if (Pfn1
->u3
.e1
.PageLocation
== ActiveAndValid
)
1020 /* All Windows does here is a bunch of sanity checks */
1021 DPRINT("Transition in active list\n");
1022 ASSERT((Pfn1
->PteAddress
>= MiAddressToPte(MmPagedPoolStart
)) &&
1023 (Pfn1
->PteAddress
<= MiAddressToPte(MmPagedPoolEnd
)));
1024 ASSERT(Pfn1
->u2
.ShareCount
!= 0);
1025 ASSERT(Pfn1
->u3
.e2
.ReferenceCount
!= 0);
1029 /* Otherwise, the page is removed from its list */
1030 DPRINT("Transition page in free/zero list\n");
1031 MiUnlinkPageFromList(Pfn1
);
1032 MiReferenceUnusedPageAndBumpLockCount(Pfn1
);
1035 /* At this point, there should no longer be any in-page errors */
1036 ASSERT(Pfn1
->u4
.InPageError
== 0);
1038 /* Check if this was a PFN with no more share references */
1039 if (Pfn1
->u2
.ShareCount
== 0) MiDropLockCount(Pfn1
);
1041 /* Bump the share count and make the page valid */
1042 Pfn1
->u2
.ShareCount
++;
1043 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
1045 /* Prototype PTEs are in paged pool, which itself might be in transition */
1046 if (FaultingAddress
>= MmSystemRangeStart
)
1048 /* Check if this is a paged pool PTE in transition state */
1049 PointerToPteForProtoPage
= MiAddressToPte(PointerPte
);
1050 TempPte
= *PointerToPteForProtoPage
;
1051 if ((TempPte
.u
.Hard
.Valid
== 0) && (TempPte
.u
.Soft
.Transition
== 1))
1053 /* This isn't yet supported */
1054 DPRINT1("Double transition fault not yet supported\n");
1059 /* Build the final PTE */
1060 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
1061 ASSERT(PointerPte
->u
.Trans
.Prototype
== 0);
1062 ASSERT(PointerPte
->u
.Trans
.Transition
== 1);
1063 TempPte
.u
.Long
= (PointerPte
->u
.Long
& ~0xFFF) |
1064 (MmProtectToPteMask
[PointerPte
->u
.Trans
.Protection
]) |
1065 MiDetermineUserGlobalPteMask(PointerPte
);
1067 /* Is the PTE writeable? */
1068 if ((Pfn1
->u3
.e1
.Modified
) &&
1069 MI_IS_PAGE_WRITEABLE(&TempPte
) &&
1070 !MI_IS_PAGE_COPY_ON_WRITE(&TempPte
))
1073 MI_MAKE_DIRTY_PAGE(&TempPte
);
1078 MI_MAKE_CLEAN_PAGE(&TempPte
);
1081 /* Write the valid PTE */
1082 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
1084 /* Return success */
1085 return STATUS_PAGE_FAULT_TRANSITION
;
1090 MiResolveProtoPteFault(IN BOOLEAN StoreInstruction
,
1092 IN PMMPTE PointerPte
,
1093 IN PMMPTE PointerProtoPte
,
1094 IN OUT PMMPFN
*OutPfn
,
1095 OUT PVOID
*PageFileData
,
1096 OUT PMMPTE PteValue
,
1097 IN PEPROCESS Process
,
1099 IN PVOID TrapInformation
)
1101 MMPTE TempPte
, PteContents
;
1103 PFN_NUMBER PageFrameIndex
;
1105 PKEVENT
* InPageBlock
= NULL
;
1108 /* Must be called with an invalid, prototype PTE, with the PFN lock held */
1109 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1110 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
1111 ASSERT(PointerPte
->u
.Soft
.Prototype
== 1);
1113 /* Read the prototype PTE and check if it's valid */
1114 TempPte
= *PointerProtoPte
;
1115 if (TempPte
.u
.Hard
.Valid
== 1)
1117 /* One more user of this mapped page */
1118 PageFrameIndex
= PFN_FROM_PTE(&TempPte
);
1119 Pfn1
= MiGetPfnEntry(PageFrameIndex
);
1120 Pfn1
->u2
.ShareCount
++;
1122 /* Call it a transition */
1123 InterlockedIncrement(&KeGetCurrentPrcb()->MmTransitionCount
);
1125 /* Complete the prototype PTE fault -- this will release the PFN lock */
1126 return MiCompleteProtoPteFault(StoreInstruction
,
1134 /* Make sure there's some protection mask */
1135 if (TempPte
.u
.Long
== 0)
1137 /* Release the lock */
1138 DPRINT1("Access on reserved section?\n");
1139 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1140 return STATUS_ACCESS_VIOLATION
;
1143 /* There is no such thing as a decommitted prototype PTE */
1144 ASSERT(TempPte
.u
.Long
!= MmDecommittedPte
.u
.Long
);
1146 /* Check for access rights on the PTE proper */
1147 PteContents
= *PointerPte
;
1148 if (PteContents
.u
.Soft
.PageFileHigh
!= MI_PTE_LOOKUP_NEEDED
)
1150 if (!PteContents
.u
.Proto
.ReadOnly
)
1152 Protection
= TempPte
.u
.Soft
.Protection
;
1156 Protection
= MM_READONLY
;
1158 /* Check for page acess in software */
1159 Status
= MiAccessCheck(PointerProtoPte
,
1162 TempPte
.u
.Soft
.Protection
,
1165 ASSERT(Status
== STATUS_SUCCESS
);
1169 Protection
= PteContents
.u
.Soft
.Protection
;
1172 /* Check for writing copy on write page */
1173 if (((Protection
& MM_WRITECOPY
) == MM_WRITECOPY
) && StoreInstruction
)
1175 PFN_NUMBER PageFrameIndex
, ProtoPageFrameIndex
;
1178 /* Resolve the proto fault as if it was a read operation */
1179 Status
= MiResolveProtoPteFault(FALSE
,
1190 if (!NT_SUCCESS(Status
))
1195 /* Lock again the PFN lock, MiResolveProtoPteFault unlocked it */
1196 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1198 /* And re-read the proto PTE */
1199 TempPte
= *PointerProtoPte
;
1200 ASSERT(TempPte
.u
.Hard
.Valid
== 1);
1201 ProtoPageFrameIndex
= PFN_FROM_PTE(&TempPte
);
1203 /* Get a new page for the private copy */
1204 if (Process
> HYDRA_PROCESS
)
1205 Color
= MI_GET_NEXT_PROCESS_COLOR(Process
);
1207 Color
= MI_GET_NEXT_COLOR();
1209 PageFrameIndex
= MiRemoveAnyPage(Color
);
1211 /* Perform the copy */
1212 MiCopyPfn(PageFrameIndex
, ProtoPageFrameIndex
);
1214 /* This will drop everything MiResolveProtoPteFault referenced */
1215 MiDeletePte(PointerPte
, Address
, Process
, PointerProtoPte
);
1217 /* Because now we use this */
1218 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
1219 MiInitializePfn(PageFrameIndex
, PointerPte
, TRUE
);
1221 /* Fix the protection */
1222 Protection
&= ~MM_WRITECOPY
;
1223 Protection
|= MM_READWRITE
;
1224 if (Address
< MmSystemRangeStart
)
1226 /* Build the user PTE */
1227 MI_MAKE_HARDWARE_PTE_USER(&PteContents
, PointerPte
, Protection
, PageFrameIndex
);
1231 /* Build the kernel PTE */
1232 MI_MAKE_HARDWARE_PTE(&PteContents
, PointerPte
, Protection
, PageFrameIndex
);
1235 /* And finally, write the valid PTE */
1236 MI_WRITE_VALID_PTE(PointerPte
, PteContents
);
1238 /* The caller expects us to release the PFN lock */
1239 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1243 /* Check for clone PTEs */
1244 if (PointerPte
<= MiHighestUserPte
) ASSERT(Process
->CloneRoot
== NULL
);
1246 /* We don't support mapped files yet */
1247 ASSERT(TempPte
.u
.Soft
.Prototype
== 0);
1249 /* We might however have transition PTEs */
1250 if (TempPte
.u
.Soft
.Transition
== 1)
1252 /* Resolve the transition fault */
1253 ASSERT(OldIrql
!= MM_NOIRQL
);
1254 Status
= MiResolveTransitionFault(StoreInstruction
,
1260 ASSERT(NT_SUCCESS(Status
));
1264 /* We also don't support paged out pages */
1265 ASSERT(TempPte
.u
.Soft
.PageFileHigh
== 0);
1267 /* Resolve the demand zero fault */
1268 Status
= MiResolveDemandZeroFault(Address
,
1272 ASSERT(NT_SUCCESS(Status
));
1275 /* Complete the prototype PTE fault -- this will release the PFN lock */
1276 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
1277 return MiCompleteProtoPteFault(StoreInstruction
,
1287 MiDispatchFault(IN BOOLEAN StoreInstruction
,
1289 IN PMMPTE PointerPte
,
1290 IN PMMPTE PointerProtoPte
,
1291 IN BOOLEAN Recursive
,
1292 IN PEPROCESS Process
,
1293 IN PVOID TrapInformation
,
1297 KIRQL OldIrql
, LockIrql
;
1299 PMMPTE SuperProtoPte
;
1300 PMMPFN Pfn1
, OutPfn
= NULL
;
1301 PFN_NUMBER PageFrameIndex
;
1302 PFN_COUNT PteCount
, ProcessedPtes
;
1303 DPRINT("ARM3 Page Fault Dispatcher for address: %p in process: %p\n",
1307 /* Make sure the addresses are ok */
1308 ASSERT(PointerPte
== MiAddressToPte(Address
));
1311 // Make sure APCs are off and we're not at dispatch
1313 OldIrql
= KeGetCurrentIrql();
1314 ASSERT(OldIrql
<= APC_LEVEL
);
1315 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1318 // Grab a copy of the PTE
1320 TempPte
= *PointerPte
;
1322 /* Do we have a prototype PTE? */
1323 if (PointerProtoPte
)
1325 /* This should never happen */
1326 ASSERT(!MI_IS_PHYSICAL_ADDRESS(PointerProtoPte
));
1328 /* Check if this is a kernel-mode address */
1329 SuperProtoPte
= MiAddressToPte(PointerProtoPte
);
1330 if (Address
>= MmSystemRangeStart
)
1332 /* Lock the PFN database */
1333 LockIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1335 /* Has the PTE been made valid yet? */
1336 if (!SuperProtoPte
->u
.Hard
.Valid
)
1340 else if (PointerPte
->u
.Hard
.Valid
== 1)
1345 /* Resolve the fault -- this will release the PFN lock */
1346 Status
= MiResolveProtoPteFault(StoreInstruction
,
1356 ASSERT(Status
== STATUS_SUCCESS
);
1358 /* Complete this as a transition fault */
1359 ASSERT(OldIrql
== KeGetCurrentIrql());
1360 ASSERT(OldIrql
<= APC_LEVEL
);
1361 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1366 /* We only handle the lookup path */
1367 ASSERT(PointerPte
->u
.Soft
.PageFileHigh
== MI_PTE_LOOKUP_NEEDED
);
1369 /* Is there a non-image VAD? */
1371 (Vad
->u
.VadFlags
.VadType
!= VadImageMap
) &&
1372 !(Vad
->u2
.VadFlags2
.ExtendableFile
))
1374 /* One day, ReactOS will cluster faults */
1375 ASSERT(Address
<= MM_HIGHEST_USER_ADDRESS
);
1376 DPRINT("Should cluster fault, but won't\n");
1379 /* Only one PTE to handle for now */
1383 /* Lock the PFN database */
1384 LockIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1386 /* We only handle the valid path */
1387 ASSERT(SuperProtoPte
->u
.Hard
.Valid
== 1);
1389 /* Capture the PTE */
1390 TempPte
= *PointerProtoPte
;
1392 /* Loop to handle future case of clustered faults */
1395 /* For our current usage, this should be true */
1396 if (TempPte
.u
.Hard
.Valid
== 1)
1398 /* Bump the share count on the PTE */
1399 PageFrameIndex
= PFN_FROM_PTE(&TempPte
);
1400 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
1401 Pfn1
->u2
.ShareCount
++;
1403 else if ((TempPte
.u
.Soft
.Prototype
== 0) &&
1404 (TempPte
.u
.Soft
.Transition
== 1))
1406 /* This is a standby page, bring it back from the cache */
1407 PageFrameIndex
= TempPte
.u
.Trans
.PageFrameNumber
;
1408 DPRINT("oooh, shiny, a soft fault! 0x%lx\n", PageFrameIndex
);
1409 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
1410 ASSERT(Pfn1
->u3
.e1
.PageLocation
!= ActiveAndValid
);
1412 /* Should not yet happen in ReactOS */
1413 ASSERT(Pfn1
->u3
.e1
.ReadInProgress
== 0);
1414 ASSERT(Pfn1
->u4
.InPageError
== 0);
1417 MiUnlinkPageFromList(Pfn1
);
1419 /* Bump its reference count */
1420 ASSERT(Pfn1
->u2
.ShareCount
== 0);
1421 InterlockedIncrement16((PSHORT
)&Pfn1
->u3
.e2
.ReferenceCount
);
1422 Pfn1
->u2
.ShareCount
++;
1424 /* Make it valid again */
1425 /* This looks like another macro.... */
1426 Pfn1
->u3
.e1
.PageLocation
= ActiveAndValid
;
1427 ASSERT(PointerProtoPte
->u
.Hard
.Valid
== 0);
1428 ASSERT(PointerProtoPte
->u
.Trans
.Prototype
== 0);
1429 ASSERT(PointerProtoPte
->u
.Trans
.Transition
== 1);
1430 TempPte
.u
.Long
= (PointerProtoPte
->u
.Long
& ~0xFFF) |
1431 MmProtectToPteMask
[PointerProtoPte
->u
.Trans
.Protection
];
1432 TempPte
.u
.Hard
.Valid
= 1;
1433 MI_MAKE_ACCESSED_PAGE(&TempPte
);
1435 /* Is the PTE writeable? */
1436 if ((Pfn1
->u3
.e1
.Modified
) &&
1437 MI_IS_PAGE_WRITEABLE(&TempPte
) &&
1438 !MI_IS_PAGE_COPY_ON_WRITE(&TempPte
))
1441 MI_MAKE_DIRTY_PAGE(&TempPte
);
1446 MI_MAKE_CLEAN_PAGE(&TempPte
);
1449 /* Write the valid PTE */
1450 MI_WRITE_VALID_PTE(PointerProtoPte
, TempPte
);
1451 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
1455 /* Page is invalid, get out of the loop */
1459 /* One more done, was it the last? */
1460 if (++ProcessedPtes
== PteCount
)
1462 /* Complete the fault */
1463 MiCompleteProtoPteFault(StoreInstruction
,
1470 /* THIS RELEASES THE PFN LOCK! */
1474 /* No clustered faults yet */
1478 /* Did we resolve the fault? */
1481 /* Bump the transition count */
1482 InterlockedExchangeAddSizeT(&KeGetCurrentPrcb()->MmTransitionCount
, ProcessedPtes
);
1485 /* Loop all the processing we did */
1486 ASSERT(ProcessedPtes
== 0);
1488 /* Complete this as a transition fault */
1489 ASSERT(OldIrql
== KeGetCurrentIrql());
1490 ASSERT(OldIrql
<= APC_LEVEL
);
1491 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1492 return STATUS_PAGE_FAULT_TRANSITION
;
1495 /* We did not -- PFN lock is still held, prepare to resolve prototype PTE fault */
1496 OutPfn
= MI_PFN_ELEMENT(SuperProtoPte
->u
.Hard
.PageFrameNumber
);
1497 MiReferenceUsedPageAndBumpLockCount(OutPfn
);
1498 ASSERT(OutPfn
->u3
.e2
.ReferenceCount
> 1);
1499 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
1501 /* Resolve the fault -- this will release the PFN lock */
1502 Status
= MiResolveProtoPteFault(StoreInstruction
,
1512 //ASSERT(Status != STATUS_ISSUE_PAGING_IO);
1513 //ASSERT(Status != STATUS_REFAULT);
1514 //ASSERT(Status != STATUS_PTE_CHANGED);
1516 /* Did the routine clean out the PFN or should we? */
1519 /* We had a locked PFN, so acquire the PFN lock to dereference it */
1520 ASSERT(PointerProtoPte
!= NULL
);
1521 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1523 /* Dereference the locked PFN */
1524 MiDereferencePfnAndDropLockCount(OutPfn
);
1525 ASSERT(OutPfn
->u3
.e2
.ReferenceCount
>= 1);
1527 /* And now release the lock */
1528 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1531 /* Complete this as a transition fault */
1532 ASSERT(OldIrql
== KeGetCurrentIrql());
1533 ASSERT(OldIrql
<= APC_LEVEL
);
1534 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1539 /* Is this a transition PTE */
1540 if (TempPte
.u
.Soft
.Transition
)
1542 PKEVENT
* InPageBlock
= NULL
;
1543 PKEVENT PreviousPageEvent
;
1544 KEVENT CurrentPageEvent
;
1546 /* Lock the PFN database */
1547 LockIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1550 Status
= MiResolveTransitionFault(StoreInstruction
, Address
, PointerPte
, Process
, LockIrql
, &InPageBlock
);
1552 ASSERT(NT_SUCCESS(Status
));
1554 if (InPageBlock
!= NULL
)
1556 /* Another thread is reading or writing this page. Put us into the waiting queue. */
1557 KeInitializeEvent(&CurrentPageEvent
, NotificationEvent
, FALSE
);
1558 PreviousPageEvent
= *InPageBlock
;
1559 *InPageBlock
= &CurrentPageEvent
;
1562 /* And now release the lock and leave*/
1563 KeReleaseQueuedSpinLock(LockQueuePfnLock
, LockIrql
);
1565 if (InPageBlock
!= NULL
)
1567 KeWaitForSingleObject(&CurrentPageEvent
, WrPageIn
, KernelMode
, FALSE
, NULL
);
1569 /* Let's the chain go on */
1570 if (PreviousPageEvent
)
1572 KeSetEvent(PreviousPageEvent
, IO_NO_INCREMENT
, FALSE
);
1576 ASSERT(OldIrql
== KeGetCurrentIrql());
1577 ASSERT(OldIrql
<= APC_LEVEL
);
1578 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1582 /* Should we page the data back in ? */
1583 if (TempPte
.u
.Soft
.PageFileHigh
!= 0)
1585 /* Lock the PFN database */
1586 LockIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1589 Status
= MiResolvePageFileFault(StoreInstruction
, Address
, PointerPte
, Process
, &LockIrql
);
1591 /* And now release the lock and leave*/
1592 KeReleaseQueuedSpinLock(LockQueuePfnLock
, LockIrql
);
1594 ASSERT(OldIrql
== KeGetCurrentIrql());
1595 ASSERT(OldIrql
<= APC_LEVEL
);
1596 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1601 // The PTE must be invalid but not completely empty. It must also not be a
1602 // prototype a transition or a paged-out PTE as those scenarii should've been handled above.
1603 // These are all Windows checks
1605 ASSERT(TempPte
.u
.Hard
.Valid
== 0);
1606 ASSERT(TempPte
.u
.Soft
.Prototype
== 0);
1607 ASSERT(TempPte
.u
.Soft
.Transition
== 0);
1608 ASSERT(TempPte
.u
.Soft
.PageFileHigh
== 0);
1609 ASSERT(TempPte
.u
.Long
!= 0);
1612 // If we got this far, the PTE can only be a demand zero PTE, which is what
1613 // we want. Go handle it!
1615 Status
= MiResolveDemandZeroFault(Address
,
1619 ASSERT(KeAreAllApcsDisabled() == TRUE
);
1620 if (NT_SUCCESS(Status
))
1623 // Make sure we're returning in a sane state and pass the status down
1625 ASSERT(OldIrql
== KeGetCurrentIrql());
1626 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
1631 // Generate an access fault
1633 return STATUS_ACCESS_VIOLATION
;
1638 MmArmAccessFault(IN BOOLEAN StoreInstruction
,
1640 IN KPROCESSOR_MODE Mode
,
1641 IN PVOID TrapInformation
)
1643 KIRQL OldIrql
= KeGetCurrentIrql(), LockIrql
;
1644 PMMPTE ProtoPte
= NULL
;
1645 PMMPTE PointerPte
= MiAddressToPte(Address
);
1646 PMMPDE PointerPde
= MiAddressToPde(Address
);
1647 #if (_MI_PAGING_LEVELS >= 3)
1648 PMMPDE PointerPpe
= MiAddressToPpe(Address
);
1649 #if (_MI_PAGING_LEVELS == 4)
1650 PMMPDE PointerPxe
= MiAddressToPxe(Address
);
1654 PETHREAD CurrentThread
;
1655 PEPROCESS CurrentProcess
;
1657 PMMSUPPORT WorkingSet
;
1658 ULONG ProtectionCode
;
1660 PFN_NUMBER PageFrameIndex
;
1662 BOOLEAN IsSessionAddress
;
1664 DPRINT("ARM3 FAULT AT: %p\n", Address
);
1666 /* Check for page fault on high IRQL */
1667 if (OldIrql
> APC_LEVEL
)
1669 #if (_MI_PAGING_LEVELS < 3)
1670 /* Could be a page table for paged pool, which we'll allow */
1671 if (MI_IS_SYSTEM_PAGE_TABLE_ADDRESS(Address
)) MiSynchronizeSystemPde((PMMPDE
)PointerPte
);
1672 MiCheckPdeForPagedPool(Address
);
1674 /* Check if any of the top-level pages are invalid */
1676 #if (_MI_PAGING_LEVELS == 4)
1677 (PointerPxe
->u
.Hard
.Valid
== 0) ||
1679 #if (_MI_PAGING_LEVELS >= 3)
1680 (PointerPpe
->u
.Hard
.Valid
== 0) ||
1682 (PointerPde
->u
.Hard
.Valid
== 0) ||
1683 (PointerPte
->u
.Hard
.Valid
== 0))
1685 /* This fault is not valid, print out some debugging help */
1686 DbgPrint("MM:***PAGE FAULT AT IRQL > 1 Va %p, IRQL %lx\n",
1689 if (TrapInformation
)
1691 PKTRAP_FRAME TrapFrame
= TrapInformation
;
1693 DbgPrint("MM:***EIP %p, EFL %p\n", TrapFrame
->Eip
, TrapFrame
->EFlags
);
1694 DbgPrint("MM:***EAX %p, ECX %p EDX %p\n", TrapFrame
->Eax
, TrapFrame
->Ecx
, TrapFrame
->Edx
);
1695 DbgPrint("MM:***EBX %p, ESI %p EDI %p\n", TrapFrame
->Ebx
, TrapFrame
->Esi
, TrapFrame
->Edi
);
1696 #elif defined(_M_AMD64)
1697 DbgPrint("MM:***RIP %p, EFL %p\n", TrapFrame
->Rip
, TrapFrame
->EFlags
);
1698 DbgPrint("MM:***RAX %p, RCX %p RDX %p\n", TrapFrame
->Rax
, TrapFrame
->Rcx
, TrapFrame
->Rdx
);
1699 DbgPrint("MM:***RBX %p, RSI %p RDI %p\n", TrapFrame
->Rbx
, TrapFrame
->Rsi
, TrapFrame
->Rdi
);
1700 #elif defined(_M_ARM)
1701 DbgPrint("MM:***PC %p\n", TrapFrame
->Pc
);
1702 DbgPrint("MM:***R0 %p, R1 %p R2 %p, R3 %p\n", TrapFrame
->R0
, TrapFrame
->R1
, TrapFrame
->R2
, TrapFrame
->R3
);
1703 DbgPrint("MM:***R11 %p, R12 %p SP %p, LR %p\n", TrapFrame
->R11
, TrapFrame
->R12
, TrapFrame
->Sp
, TrapFrame
->Lr
);
1707 /* Tell the trap handler to fail */
1708 return STATUS_IN_PAGE_ERROR
| 0x10000000;
1711 /* Not yet implemented in ReactOS */
1712 ASSERT(MI_IS_PAGE_LARGE(PointerPde
) == FALSE
);
1713 ASSERT(((StoreInstruction
) && MI_IS_PAGE_COPY_ON_WRITE(PointerPte
)) == FALSE
);
1715 /* Check if this was a write */
1716 if (StoreInstruction
)
1718 /* Was it to a read-only page? */
1719 Pfn1
= MI_PFN_ELEMENT(PointerPte
->u
.Hard
.PageFrameNumber
);
1720 if (!(PointerPte
->u
.Long
& PTE_READWRITE
) &&
1721 !(Pfn1
->OriginalPte
.u
.Soft
.Protection
& MM_READWRITE
))
1723 /* Crash with distinguished bugcheck code */
1724 KeBugCheckEx(ATTEMPTED_WRITE_TO_READONLY_MEMORY
,
1727 (ULONG_PTR
)TrapInformation
,
1732 /* Nothing is actually wrong */
1733 DPRINT1("Fault at IRQL %u is ok (%p)\n", OldIrql
, Address
);
1734 return STATUS_SUCCESS
;
1737 /* Check for kernel fault address */
1738 if (Address
>= MmSystemRangeStart
)
1740 /* Bail out, if the fault came from user mode */
1741 if (Mode
== UserMode
) return STATUS_ACCESS_VIOLATION
;
1743 #if (_MI_PAGING_LEVELS == 2)
1744 if (MI_IS_SYSTEM_PAGE_TABLE_ADDRESS(Address
)) MiSynchronizeSystemPde((PMMPDE
)PointerPte
);
1745 MiCheckPdeForPagedPool(Address
);
1748 /* Check if the higher page table entries are invalid */
1750 #if (_MI_PAGING_LEVELS == 4)
1751 /* AMD64 system, check if PXE is invalid */
1752 (PointerPxe
->u
.Hard
.Valid
== 0) ||
1754 #if (_MI_PAGING_LEVELS >= 3)
1755 /* PAE/AMD64 system, check if PPE is invalid */
1756 (PointerPpe
->u
.Hard
.Valid
== 0) ||
1758 /* Always check if the PDE is valid */
1759 (PointerPde
->u
.Hard
.Valid
== 0))
1761 /* PXE/PPE/PDE (still) not valid, kill the system */
1762 KeBugCheckEx(PAGE_FAULT_IN_NONPAGED_AREA
,
1765 (ULONG_PTR
)TrapInformation
,
1769 /* Not handling session faults yet */
1770 IsSessionAddress
= MI_IS_SESSION_ADDRESS(Address
);
1772 /* The PDE is valid, so read the PTE */
1773 TempPte
= *PointerPte
;
1774 if (TempPte
.u
.Hard
.Valid
== 1)
1776 /* Check if this was system space or session space */
1777 if (!IsSessionAddress
)
1779 /* Check if the PTE is still valid under PFN lock */
1780 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
1781 TempPte
= *PointerPte
;
1782 if (TempPte
.u
.Hard
.Valid
)
1784 /* Check if this was a write */
1785 if (StoreInstruction
)
1787 /* Was it to a read-only page? */
1788 Pfn1
= MI_PFN_ELEMENT(PointerPte
->u
.Hard
.PageFrameNumber
);
1789 if (!(PointerPte
->u
.Long
& PTE_READWRITE
) &&
1790 !(Pfn1
->OriginalPte
.u
.Soft
.Protection
& MM_READWRITE
))
1792 /* Crash with distinguished bugcheck code */
1793 KeBugCheckEx(ATTEMPTED_WRITE_TO_READONLY_MEMORY
,
1796 (ULONG_PTR
)TrapInformation
,
1802 /* Release PFN lock and return all good */
1803 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
1804 return STATUS_SUCCESS
;
1807 #if (_MI_PAGING_LEVELS == 2)
1808 /* Check if this was a session PTE that needs to remap the session PDE */
1809 if (MI_IS_SESSION_PTE(Address
))
1811 /* Do the remapping */
1812 Status
= MiCheckPdeForSessionSpace(Address
);
1813 if (!NT_SUCCESS(Status
))
1815 /* It failed, this address is invalid */
1816 KeBugCheckEx(PAGE_FAULT_IN_NONPAGED_AREA
,
1819 (ULONG_PTR
)TrapInformation
,
1825 _WARN("Session space stuff is not implemented yet!")
1829 /* Check for a fault on the page table or hyperspace */
1830 if (MI_IS_PAGE_TABLE_OR_HYPER_ADDRESS(Address
))
1832 #if (_MI_PAGING_LEVELS < 3)
1833 /* Windows does this check but I don't understand why -- it's done above! */
1834 ASSERT(MiCheckPdeForPagedPool(Address
) != STATUS_WAIT_1
);
1836 /* Handle this as a user mode fault */
1840 /* Get the current thread */
1841 CurrentThread
= PsGetCurrentThread();
1843 /* What kind of address is this */
1844 if (!IsSessionAddress
)
1846 /* Use the system working set */
1847 WorkingSet
= &MmSystemCacheWs
;
1848 CurrentProcess
= NULL
;
1850 /* Make sure we don't have a recursive working set lock */
1851 if ((CurrentThread
->OwnsProcessWorkingSetExclusive
) ||
1852 (CurrentThread
->OwnsProcessWorkingSetShared
) ||
1853 (CurrentThread
->OwnsSystemWorkingSetExclusive
) ||
1854 (CurrentThread
->OwnsSystemWorkingSetShared
) ||
1855 (CurrentThread
->OwnsSessionWorkingSetExclusive
) ||
1856 (CurrentThread
->OwnsSessionWorkingSetShared
))
1859 return STATUS_IN_PAGE_ERROR
| 0x10000000;
1864 /* Use the session process and working set */
1865 CurrentProcess
= HYDRA_PROCESS
;
1866 WorkingSet
= &MmSessionSpace
->GlobalVirtualAddress
->Vm
;
1868 /* Make sure we don't have a recursive working set lock */
1869 if ((CurrentThread
->OwnsSessionWorkingSetExclusive
) ||
1870 (CurrentThread
->OwnsSessionWorkingSetShared
))
1873 return STATUS_IN_PAGE_ERROR
| 0x10000000;
1877 /* Acquire the working set lock */
1878 KeRaiseIrql(APC_LEVEL
, &LockIrql
);
1879 MiLockWorkingSet(CurrentThread
, WorkingSet
);
1881 /* Re-read PTE now that we own the lock */
1882 TempPte
= *PointerPte
;
1883 if (TempPte
.u
.Hard
.Valid
== 1)
1885 /* Check if this was a write */
1886 if (StoreInstruction
)
1888 /* Was it to a read-only page that is not copy on write? */
1889 Pfn1
= MI_PFN_ELEMENT(PointerPte
->u
.Hard
.PageFrameNumber
);
1890 if (!(TempPte
.u
.Long
& PTE_READWRITE
) &&
1891 !(Pfn1
->OriginalPte
.u
.Soft
.Protection
& MM_READWRITE
) &&
1892 !MI_IS_PAGE_COPY_ON_WRITE(&TempPte
))
1894 /* Case not yet handled */
1895 ASSERT(!IsSessionAddress
);
1897 /* Crash with distinguished bugcheck code */
1898 KeBugCheckEx(ATTEMPTED_WRITE_TO_READONLY_MEMORY
,
1901 (ULONG_PTR
)TrapInformation
,
1906 /* Check for read-only write in session space */
1907 if ((IsSessionAddress
) &&
1908 (StoreInstruction
) &&
1909 !MI_IS_PAGE_WRITEABLE(&TempPte
))
1912 ASSERT(MI_IS_SESSION_IMAGE_ADDRESS(Address
));
1915 if (!MI_IS_PAGE_COPY_ON_WRITE(&TempPte
))
1917 /* Then this is not allowed */
1918 KeBugCheckEx(ATTEMPTED_WRITE_TO_READONLY_MEMORY
,
1920 (ULONG_PTR
)TempPte
.u
.Long
,
1921 (ULONG_PTR
)TrapInformation
,
1925 /* Otherwise, handle COW */
1929 /* Release the working set */
1930 MiUnlockWorkingSet(CurrentThread
, WorkingSet
);
1931 KeLowerIrql(LockIrql
);
1933 /* Otherwise, the PDE was probably invalid, and all is good now */
1934 return STATUS_SUCCESS
;
1937 /* Check one kind of prototype PTE */
1938 if (TempPte
.u
.Soft
.Prototype
)
1940 /* Make sure protected pool is on, and that this is a pool address */
1941 if ((MmProtectFreedNonPagedPool
) &&
1942 (((Address
>= MmNonPagedPoolStart
) &&
1943 (Address
< (PVOID
)((ULONG_PTR
)MmNonPagedPoolStart
+
1944 MmSizeOfNonPagedPoolInBytes
))) ||
1945 ((Address
>= MmNonPagedPoolExpansionStart
) &&
1946 (Address
< MmNonPagedPoolEnd
))))
1948 /* Bad boy, bad boy, whatcha gonna do, whatcha gonna do when ARM3 comes for you! */
1949 KeBugCheckEx(DRIVER_CAUGHT_MODIFYING_FREED_POOL
,
1956 /* Get the prototype PTE! */
1957 ProtoPte
= MiProtoPteToPte(&TempPte
);
1959 /* Do we need to locate the prototype PTE in session space? */
1960 if ((IsSessionAddress
) &&
1961 (TempPte
.u
.Soft
.PageFileHigh
== MI_PTE_LOOKUP_NEEDED
))
1963 /* Yep, go find it as well as the VAD for it */
1964 ProtoPte
= MiCheckVirtualAddress(Address
,
1967 ASSERT(ProtoPte
!= NULL
);
1972 /* We don't implement transition PTEs */
1973 ASSERT(TempPte
.u
.Soft
.Transition
== 0);
1975 /* Check for no-access PTE */
1976 if (TempPte
.u
.Soft
.Protection
== MM_NOACCESS
)
1978 /* Bugcheck the system! */
1979 KeBugCheckEx(PAGE_FAULT_IN_NONPAGED_AREA
,
1982 (ULONG_PTR
)TrapInformation
,
1986 /* Check for no protecton at all */
1987 if (TempPte
.u
.Soft
.Protection
== MM_ZERO_ACCESS
)
1989 /* Bugcheck the system! */
1990 KeBugCheckEx(PAGE_FAULT_IN_NONPAGED_AREA
,
1993 (ULONG_PTR
)TrapInformation
,
1998 /* Check for demand page */
1999 if ((StoreInstruction
) &&
2001 !(IsSessionAddress
) &&
2002 !(TempPte
.u
.Hard
.Valid
))
2004 /* Get the protection code */
2005 ASSERT(TempPte
.u
.Soft
.Transition
== 0);
2006 if (!(TempPte
.u
.Soft
.Protection
& MM_READWRITE
))
2008 /* Bugcheck the system! */
2009 KeBugCheckEx(ATTEMPTED_WRITE_TO_READONLY_MEMORY
,
2012 (ULONG_PTR
)TrapInformation
,
2017 /* Now do the real fault handling */
2018 Status
= MiDispatchFault(StoreInstruction
,
2027 /* Release the working set */
2028 ASSERT(KeAreAllApcsDisabled() == TRUE
);
2029 MiUnlockWorkingSet(CurrentThread
, WorkingSet
);
2030 KeLowerIrql(LockIrql
);
2033 DPRINT("Fault resolved with status: %lx\n", Status
);
2037 /* This is a user fault */
2039 CurrentThread
= PsGetCurrentThread();
2040 CurrentProcess
= (PEPROCESS
)CurrentThread
->Tcb
.ApcState
.Process
;
2042 /* Lock the working set */
2043 MiLockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2045 #if (_MI_PAGING_LEVELS == 4)
2046 // Note to Timo: You should call MiCheckVirtualAddress and also check if it's zero pte
2047 // also this is missing the page count increment
2048 /* Check if the PXE is valid */
2049 if (PointerPxe
->u
.Hard
.Valid
== 0)
2051 /* Right now, we only handle scenarios where the PXE is totally empty */
2052 ASSERT(PointerPxe
->u
.Long
== 0);
2054 /* Resolve a demand zero fault */
2055 Status
= MiResolveDemandZeroFault(PointerPpe
,
2060 /* We should come back with a valid PXE */
2061 ASSERT(PointerPxe
->u
.Hard
.Valid
== 1);
2065 #if (_MI_PAGING_LEVELS >= 3)
2066 // Note to Timo: You should call MiCheckVirtualAddress and also check if it's zero pte
2067 // also this is missing the page count increment
2068 /* Check if the PPE is valid */
2069 if (PointerPpe
->u
.Hard
.Valid
== 0)
2071 /* Right now, we only handle scenarios where the PPE is totally empty */
2072 ASSERT(PointerPpe
->u
.Long
== 0);
2074 /* Resolve a demand zero fault */
2075 Status
= MiResolveDemandZeroFault(PointerPde
,
2080 /* We should come back with a valid PPE */
2081 ASSERT(PointerPpe
->u
.Hard
.Valid
== 1);
2085 /* Check if the PDE is valid */
2086 if (PointerPde
->u
.Hard
.Valid
== 0)
2088 /* Right now, we only handle scenarios where the PDE is totally empty */
2089 ASSERT(PointerPde
->u
.Long
== 0);
2091 /* And go dispatch the fault on the PDE. This should handle the demand-zero */
2093 UserPdeFault
= TRUE
;
2095 MiCheckVirtualAddress(Address
, &ProtectionCode
, &Vad
);
2096 if (ProtectionCode
== MM_NOACCESS
)
2098 #if (_MI_PAGING_LEVELS == 2)
2099 /* Could be a page table for paged pool */
2100 MiCheckPdeForPagedPool(Address
);
2102 /* Has the code above changed anything -- is this now a valid PTE? */
2103 Status
= (PointerPde
->u
.Hard
.Valid
== 1) ? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;
2105 /* Either this was a bogus VA or we've fixed up a paged pool PDE */
2106 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2110 /* Write a demand-zero PDE */
2111 MI_WRITE_INVALID_PDE(PointerPde
, DemandZeroPde
);
2113 /* Dispatch the fault */
2114 Status
= MiDispatchFault(TRUE
,
2119 PsGetCurrentProcess(),
2123 UserPdeFault
= FALSE
;
2125 /* We should come back with APCs enabled, and with a valid PDE */
2126 ASSERT(KeAreAllApcsDisabled() == TRUE
);
2127 ASSERT(PointerPde
->u
.Hard
.Valid
== 1);
2131 /* Not yet implemented in ReactOS */
2132 ASSERT(MI_IS_PAGE_LARGE(PointerPde
) == FALSE
);
2135 /* Now capture the PTE. */
2136 TempPte
= *PointerPte
;
2138 /* Check if the PTE is valid */
2139 if (TempPte
.u
.Hard
.Valid
)
2141 /* Check if this is a write on a readonly PTE */
2142 if (StoreInstruction
)
2144 /* Is this a copy on write PTE? */
2145 if (MI_IS_PAGE_COPY_ON_WRITE(&TempPte
))
2147 PFN_NUMBER PageFrameIndex
, OldPageFrameIndex
;
2150 LockIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
2152 ASSERT(MmAvailablePages
> 0);
2154 /* Allocate a new page and copy it */
2155 PageFrameIndex
= MiRemoveAnyPage(MI_GET_NEXT_PROCESS_COLOR(CurrentProcess
));
2156 OldPageFrameIndex
= PFN_FROM_PTE(&TempPte
);
2158 MiCopyPfn(PageFrameIndex
, OldPageFrameIndex
);
2160 /* Dereference whatever this PTE is referencing */
2161 Pfn1
= MI_PFN_ELEMENT(OldPageFrameIndex
);
2162 ASSERT(Pfn1
->u3
.e1
.PrototypePte
== 1);
2163 ASSERT(!MI_IS_PFN_DELETED(Pfn1
));
2164 ProtoPte
= Pfn1
->PteAddress
;
2165 MiDeletePte(PointerPte
, Address
, CurrentProcess
, ProtoPte
);
2167 /* And make a new shiny one with our page */
2168 MiInitializePfn(PageFrameIndex
, PointerPte
, TRUE
);
2169 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameIndex
;
2170 TempPte
.u
.Hard
.Write
= 1;
2171 TempPte
.u
.Hard
.CopyOnWrite
= 0;
2173 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
2175 KeReleaseQueuedSpinLock(LockQueuePfnLock
, LockIrql
);
2177 /* Return the status */
2178 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2179 return STATUS_PAGE_FAULT_COPY_ON_WRITE
;
2182 /* Is this a read-only PTE? */
2183 if (!MI_IS_PAGE_WRITEABLE(&TempPte
))
2185 /* Return the status */
2186 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2187 return STATUS_ACCESS_VIOLATION
;
2191 /* FIXME: Execution is ignored for now, since we don't have no-execute pages yet */
2193 /* The fault has already been resolved by a different thread */
2194 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2195 return STATUS_SUCCESS
;
2198 /* Quick check for demand-zero */
2199 if (TempPte
.u
.Long
== (MM_READWRITE
<< MM_PTE_SOFTWARE_PROTECTION_BITS
))
2201 /* Resolve the fault */
2202 MiResolveDemandZeroFault(Address
,
2207 /* Return the status */
2208 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2209 return STATUS_PAGE_FAULT_DEMAND_ZERO
;
2212 /* Check for zero PTE */
2213 if (TempPte
.u
.Long
== 0)
2215 /* Check if this address range belongs to a valid allocation (VAD) */
2216 ProtoPte
= MiCheckVirtualAddress(Address
, &ProtectionCode
, &Vad
);
2217 if (ProtectionCode
== MM_NOACCESS
)
2219 #if (_MI_PAGING_LEVELS == 2)
2220 /* Could be a page table for paged pool */
2221 MiCheckPdeForPagedPool(Address
);
2223 /* Has the code above changed anything -- is this now a valid PTE? */
2224 Status
= (PointerPte
->u
.Hard
.Valid
== 1) ? STATUS_SUCCESS
: STATUS_ACCESS_VIOLATION
;
2226 /* Either this was a bogus VA or we've fixed up a paged pool PDE */
2227 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2232 * Check if this is a real user-mode address or actually a kernel-mode
2233 * page table for a user mode address
2235 if (Address
<= MM_HIGHEST_USER_ADDRESS
)
2237 /* Add an additional page table reference */
2238 MiIncrementPageTableReferences(Address
);
2241 /* Is this a guard page? */
2242 if ((ProtectionCode
& MM_PROTECT_SPECIAL
) == MM_GUARDPAGE
)
2244 /* The VAD protection cannot be MM_DECOMMIT! */
2245 ASSERT(ProtectionCode
!= MM_DECOMMIT
);
2247 /* Remove the bit */
2248 TempPte
.u
.Soft
.Protection
= ProtectionCode
& ~MM_GUARDPAGE
;
2249 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
2252 ASSERT(ProtoPte
== NULL
);
2253 ASSERT(CurrentThread
->ApcNeeded
== 0);
2255 /* Drop the working set lock */
2256 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2257 ASSERT(KeGetCurrentIrql() == OldIrql
);
2259 /* Handle stack expansion */
2260 return MiCheckForUserStackOverflow(Address
, TrapInformation
);
2263 /* Did we get a prototype PTE back? */
2266 /* Is this PTE actually part of the PDE-PTE self-mapping directory? */
2267 if (PointerPde
== MiAddressToPde(PTE_BASE
))
2269 /* Then it's really a demand-zero PDE (on behalf of user-mode) */
2271 _WARN("This is probably completely broken!");
2272 MI_WRITE_INVALID_PDE((PMMPDE
)PointerPte
, DemandZeroPde
);
2274 MI_WRITE_INVALID_PTE(PointerPte
, DemandZeroPde
);
2279 /* No, create a new PTE. First, write the protection */
2280 TempPte
.u
.Soft
.Protection
= ProtectionCode
;
2281 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
2284 /* Lock the PFN database since we're going to grab a page */
2285 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
2287 /* Make sure we have enough pages */
2288 ASSERT(MmAvailablePages
>= 32);
2290 /* Try to get a zero page */
2291 MI_SET_USAGE(MI_USAGE_PEB_TEB
);
2292 MI_SET_PROCESS2(CurrentProcess
->ImageFileName
);
2293 Color
= MI_GET_NEXT_PROCESS_COLOR(CurrentProcess
);
2294 PageFrameIndex
= MiRemoveZeroPageSafe(Color
);
2295 if (!PageFrameIndex
)
2297 /* Grab a page out of there. Later we should grab a colored zero page */
2298 PageFrameIndex
= MiRemoveAnyPage(Color
);
2299 ASSERT(PageFrameIndex
);
2301 /* Release the lock since we need to do some zeroing */
2302 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
2304 /* Zero out the page, since it's for user-mode */
2305 MiZeroPfn(PageFrameIndex
);
2307 /* Grab the lock again so we can initialize the PFN entry */
2308 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
2311 /* Initialize the PFN entry now */
2312 MiInitializePfn(PageFrameIndex
, PointerPte
, 1);
2314 /* And we're done with the lock */
2315 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
2317 /* Increment the count of pages in the process */
2318 CurrentProcess
->NumberOfPrivatePages
++;
2320 /* One more demand-zero fault */
2321 InterlockedIncrement(&KeGetCurrentPrcb()->MmDemandZeroCount
);
2323 /* Fault on user PDE, or fault on user PTE? */
2324 if (PointerPte
<= MiHighestUserPte
)
2326 /* User fault, build a user PTE */
2327 MI_MAKE_HARDWARE_PTE_USER(&TempPte
,
2329 PointerPte
->u
.Soft
.Protection
,
2334 /* This is a user-mode PDE, create a kernel PTE for it */
2335 MI_MAKE_HARDWARE_PTE(&TempPte
,
2337 PointerPte
->u
.Soft
.Protection
,
2341 /* Write the dirty bit for writeable pages */
2342 if (MI_IS_PAGE_WRITEABLE(&TempPte
)) MI_MAKE_DIRTY_PAGE(&TempPte
);
2344 /* And now write down the PTE, making the address valid */
2345 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
2346 Pfn1
= MI_PFN_ELEMENT(PageFrameIndex
);
2347 ASSERT(Pfn1
->u1
.Event
== NULL
);
2350 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
2351 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2352 return STATUS_PAGE_FAULT_DEMAND_ZERO
;
2355 /* We should have a valid protection here */
2356 ASSERT(ProtectionCode
!= 0x100);
2358 /* Write the prototype PTE */
2359 TempPte
= PrototypePte
;
2360 TempPte
.u
.Soft
.Protection
= ProtectionCode
;
2361 ASSERT(TempPte
.u
.Long
!= 0);
2362 MI_WRITE_INVALID_PTE(PointerPte
, TempPte
);
2366 /* Get the protection code and check if this is a proto PTE */
2367 ProtectionCode
= (ULONG
)TempPte
.u
.Soft
.Protection
;
2368 if (TempPte
.u
.Soft
.Prototype
)
2370 /* Do we need to go find the real PTE? */
2371 if (TempPte
.u
.Soft
.PageFileHigh
== MI_PTE_LOOKUP_NEEDED
)
2373 /* Get the prototype pte and VAD for it */
2374 ProtoPte
= MiCheckVirtualAddress(Address
,
2379 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
2380 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2381 return STATUS_ACCESS_VIOLATION
;
2386 /* Get the prototype PTE! */
2387 ProtoPte
= MiProtoPteToPte(&TempPte
);
2389 /* Is it read-only */
2390 if (TempPte
.u
.Proto
.ReadOnly
)
2392 /* Set read-only code */
2393 ProtectionCode
= MM_READONLY
;
2397 /* Set unknown protection */
2398 ProtectionCode
= 0x100;
2399 ASSERT(CurrentProcess
->CloneRoot
!= NULL
);
2405 /* Do we have a valid protection code? */
2406 if (ProtectionCode
!= 0x100)
2408 /* Run a software access check first, including to detect guard pages */
2409 Status
= MiAccessCheck(PointerPte
,
2415 if (Status
!= STATUS_SUCCESS
)
2418 ASSERT(CurrentThread
->ApcNeeded
== 0);
2420 /* Drop the working set lock */
2421 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2422 ASSERT(KeGetCurrentIrql() == OldIrql
);
2424 /* Did we hit a guard page? */
2425 if (Status
== STATUS_GUARD_PAGE_VIOLATION
)
2427 /* Handle stack expansion */
2428 return MiCheckForUserStackOverflow(Address
, TrapInformation
);
2431 /* Otherwise, fail back to the caller directly */
2436 /* Dispatch the fault */
2437 Status
= MiDispatchFault(StoreInstruction
,
2446 /* Return the status */
2447 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
2448 MiUnlockProcessWorkingSet(CurrentProcess
, CurrentThread
);
2454 MmGetExecuteOptions(IN PULONG ExecuteOptions
)
2456 PKPROCESS CurrentProcess
= &PsGetCurrentProcess()->Pcb
;
2457 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
);
2459 *ExecuteOptions
= 0;
2461 if (CurrentProcess
->Flags
.ExecuteDisable
)
2463 *ExecuteOptions
|= MEM_EXECUTE_OPTION_DISABLE
;
2466 if (CurrentProcess
->Flags
.ExecuteEnable
)
2468 *ExecuteOptions
|= MEM_EXECUTE_OPTION_ENABLE
;
2471 if (CurrentProcess
->Flags
.DisableThunkEmulation
)
2473 *ExecuteOptions
|= MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
;
2476 if (CurrentProcess
->Flags
.Permanent
)
2478 *ExecuteOptions
|= MEM_EXECUTE_OPTION_PERMANENT
;
2481 if (CurrentProcess
->Flags
.ExecuteDispatchEnable
)
2483 *ExecuteOptions
|= MEM_EXECUTE_OPTION_EXECUTE_DISPATCH_ENABLE
;
2486 if (CurrentProcess
->Flags
.ImageDispatchEnable
)
2488 *ExecuteOptions
|= MEM_EXECUTE_OPTION_IMAGE_DISPATCH_ENABLE
;
2491 return STATUS_SUCCESS
;
2496 MmSetExecuteOptions(IN ULONG ExecuteOptions
)
2498 PKPROCESS CurrentProcess
= &PsGetCurrentProcess()->Pcb
;
2499 KLOCK_QUEUE_HANDLE ProcessLock
;
2500 NTSTATUS Status
= STATUS_ACCESS_DENIED
;
2501 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
);
2503 /* Only accept valid flags */
2504 if (ExecuteOptions
& ~MEM_EXECUTE_OPTION_VALID_FLAGS
)
2507 DPRINT1("Invalid no-execute options\n");
2508 return STATUS_INVALID_PARAMETER
;
2511 /* Change the NX state in the process lock */
2512 KiAcquireProcessLock(CurrentProcess
, &ProcessLock
);
2514 /* Don't change anything if the permanent flag was set */
2515 if (!CurrentProcess
->Flags
.Permanent
)
2517 /* Start by assuming it's not disabled */
2518 CurrentProcess
->Flags
.ExecuteDisable
= FALSE
;
2520 /* Now process each flag and turn the equivalent bit on */
2521 if (ExecuteOptions
& MEM_EXECUTE_OPTION_DISABLE
)
2523 CurrentProcess
->Flags
.ExecuteDisable
= TRUE
;
2525 if (ExecuteOptions
& MEM_EXECUTE_OPTION_ENABLE
)
2527 CurrentProcess
->Flags
.ExecuteEnable
= TRUE
;
2529 if (ExecuteOptions
& MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION
)
2531 CurrentProcess
->Flags
.DisableThunkEmulation
= TRUE
;
2533 if (ExecuteOptions
& MEM_EXECUTE_OPTION_PERMANENT
)
2535 CurrentProcess
->Flags
.Permanent
= TRUE
;
2537 if (ExecuteOptions
& MEM_EXECUTE_OPTION_EXECUTE_DISPATCH_ENABLE
)
2539 CurrentProcess
->Flags
.ExecuteDispatchEnable
= TRUE
;
2541 if (ExecuteOptions
& MEM_EXECUTE_OPTION_IMAGE_DISPATCH_ENABLE
)
2543 CurrentProcess
->Flags
.ImageDispatchEnable
= TRUE
;
2546 /* These are turned on by default if no-execution is also eanbled */
2547 if (CurrentProcess
->Flags
.ExecuteEnable
)
2549 CurrentProcess
->Flags
.ExecuteDispatchEnable
= TRUE
;
2550 CurrentProcess
->Flags
.ImageDispatchEnable
= TRUE
;
2554 Status
= STATUS_SUCCESS
;
2557 /* Release the lock and return status */
2558 KiReleaseProcessLock(&ProcessLock
);