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 #line 15 "ARMĀ³::PAGFAULT"
16 #define MODULE_INVOLVED_IN_ARM3
17 #include "../ARM3/miarm.h"
19 /* GLOBALS ********************************************************************/
21 /* PRIVATE FUNCTIONS **********************************************************/
25 MiCheckPdeForPagedPool(IN PVOID Address
)
28 NTSTATUS Status
= STATUS_SUCCESS
;
30 /* No session support in ReactOS yet */
31 ASSERT(MI_IS_SESSION_ADDRESS(Address
) == FALSE
);
32 ASSERT(MI_IS_SESSION_PTE(Address
) == FALSE
);
35 // Check if this is a fault while trying to access the page table itself
37 if ((Address
>= (PVOID
)MiAddressToPte(MmSystemRangeStart
)) &&
38 (Address
< (PVOID
)PTE_TOP
))
41 // Send a hint to the page fault handler that this is only a valid fault
42 // if we already detected this was access within the page table range
44 PointerPde
= (PMMPDE
)MiAddressToPte(Address
);
45 Status
= STATUS_WAIT_1
;
47 else if (Address
< MmSystemRangeStart
)
50 // This is totally illegal
52 return STATUS_ACCESS_VIOLATION
;
57 // Get the PDE for the address
59 PointerPde
= MiAddressToPde(Address
);
63 // Check if it's not valid
65 if (PointerPde
->u
.Hard
.Valid
== 0)
68 /* This seems to be making the assumption that one PDE is one page long */
69 C_ASSERT(PAGE_SIZE
== (PD_COUNT
* (sizeof(MMPTE
) * PDE_COUNT
)));
73 // Copy it from our double-mapped system page directory
75 InterlockedExchangePte(PointerPde
,
76 MmSystemPagePtes
[((ULONG_PTR
)PointerPde
&
78 sizeof(MMPTE
)].u
.Long
);
89 MiResolveDemandZeroFault(IN PVOID Address
,
94 PFN_NUMBER PageFrameNumber
;
96 DPRINT("ARM3 Demand Zero Page Fault Handler for address: %p in process: %p\n",
100 /* Must currently only be called by paging path, for system addresses only */
101 ASSERT(OldIrql
== MM_NOIRQL
);
102 ASSERT(Process
== NULL
);
105 // Lock the PFN database
107 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
108 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
111 PageFrameNumber
= MiRemoveAnyPage(0);
112 DPRINT("New pool page: %lx\n", PageFrameNumber
);
115 MiInitializePfn(PageFrameNumber
, PointerPte
, TRUE
);
120 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
123 // Increment demand zero faults
125 InterlockedIncrement(&KeGetCurrentPrcb()->MmDemandZeroCount
);
127 /* Shouldn't see faults for user PTEs yet */
128 ASSERT(PointerPte
> MiHighestUserPte
);
131 MI_MAKE_HARDWARE_PTE(&TempPte
, PointerPte
, PointerPte
->u
.Soft
.Protection
, PageFrameNumber
);
132 MI_WRITE_VALID_PTE(PointerPte
, TempPte
);
137 DPRINT("Paged pool page has now been paged in\n");
138 return STATUS_PAGE_FAULT_DEMAND_ZERO
;
143 MiDispatchFault(IN BOOLEAN StoreInstruction
,
145 IN PMMPTE PointerPte
,
146 IN PMMPTE PrototypePte
,
147 IN BOOLEAN Recursive
,
148 IN PEPROCESS Process
,
149 IN PVOID TrapInformation
,
155 DPRINT("ARM3 Page Fault Dispatcher for address: %p in process: %p\n",
160 // Make sure APCs are off and we're not at dispatch
162 OldIrql
= KeGetCurrentIrql ();
163 ASSERT(OldIrql
<= APC_LEVEL
);
164 ASSERT(KeAreAllApcsDisabled () == TRUE
);
167 // Grab a copy of the PTE
169 TempPte
= *PointerPte
;
172 ASSERT(PrototypePte
== NULL
);
175 // The PTE must be invalid, but not totally blank
177 ASSERT(TempPte
.u
.Hard
.Valid
== 0);
178 ASSERT(TempPte
.u
.Long
!= 0);
181 // No prototype, transition or page file software PTEs in ARM3 yet
183 ASSERT(TempPte
.u
.Soft
.Prototype
== 0);
184 ASSERT(TempPte
.u
.Soft
.Transition
== 0);
185 ASSERT(TempPte
.u
.Soft
.PageFileHigh
== 0);
188 // If we got this far, the PTE can only be a demand zero PTE, which is what
189 // we want. Go handle it!
191 Status
= MiResolveDemandZeroFault(Address
,
195 ASSERT(KeAreAllApcsDisabled () == TRUE
);
196 if (NT_SUCCESS(Status
))
199 // Make sure we're returning in a sane state and pass the status down
201 ASSERT(OldIrql
== KeGetCurrentIrql ());
202 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
207 // Generate an access fault
209 return STATUS_ACCESS_VIOLATION
;
214 MmArmAccessFault(IN BOOLEAN StoreInstruction
,
216 IN KPROCESSOR_MODE Mode
,
217 IN PVOID TrapInformation
)
219 KIRQL OldIrql
= KeGetCurrentIrql(), LockIrql
;
223 PETHREAD CurrentThread
;
225 DPRINT("ARM3 FAULT AT: %p\n", Address
);
228 // Get the PTE and PDE
230 PointerPte
= MiAddressToPte(Address
);
231 PointerPde
= MiAddressToPde(Address
);
234 // Check for dispatch-level snafu
236 if (OldIrql
> APC_LEVEL
)
239 // There are some special cases where this is okay, but not in ARM3 yet
241 DbgPrint("MM:***PAGE FAULT AT IRQL > 1 Va %p, IRQL %lx\n",
244 ASSERT(OldIrql
<= APC_LEVEL
);
248 // Check for kernel fault
250 if (Address
>= MmSystemRangeStart
)
253 // What are you even DOING here?
255 if (Mode
== UserMode
) return STATUS_ACCESS_VIOLATION
;
260 if (!PointerPde
->u
.Hard
.Valid
== 0)
265 DPRINT("Invalid PDE\n");
268 // Handle mapping in "Special" PDE directoreis
270 MiCheckPdeForPagedPool(Address
);
273 // Now we SHOULD be good
275 if (PointerPde
->u
.Hard
.Valid
== 0)
278 // FIXFIX: Do the S-LIST hack
284 KeBugCheckEx(PAGE_FAULT_IN_NONPAGED_AREA
,
287 (ULONG_PTR
)TrapInformation
,
293 // The PDE is valid, so read the PTE
295 TempPte
= *PointerPte
;
296 if (TempPte
.u
.Hard
.Valid
== 1)
299 // Only two things can go wrong here:
300 // Executing NX page (we couldn't care less)
301 // Writing to a read-only page (the stuff ARM3 works with is write,
302 // so again, moot point).
304 if (StoreInstruction
)
306 DPRINT1("Should NEVER happen on ARM3!!!\n");
307 return STATUS_ACCESS_VIOLATION
;
311 // Otherwise, the PDE was probably invalid, and all is good now
313 return STATUS_SUCCESS
;
317 // Check for a fault on the page table or hyperspace itself
319 if ((Address
>= (PVOID
)PTE_BASE
) && (Address
<= MmHyperSpaceEnd
))
322 // This might happen...not sure yet
324 DPRINT1("FAULT ON PAGE TABLES: %p %lx %lx!\n", Address
, *PointerPte
, *PointerPde
);
327 // Map in the page table
329 if (MiCheckPdeForPagedPool(Address
) == STATUS_WAIT_1
)
331 DPRINT1("PAGE TABLES FAULTED IN!\n");
332 return STATUS_SUCCESS
;
336 // Otherwise the page table doesn't actually exist
338 DPRINT1("FAILING\n");
339 return STATUS_ACCESS_VIOLATION
;
343 // Now we must raise to APC_LEVEL and mark the thread as owner
344 // We don't actually implement a working set pushlock, so this is only
345 // for internal consistency (and blocking APCs)
347 KeRaiseIrql(APC_LEVEL
, &LockIrql
);
348 CurrentThread
= PsGetCurrentThread();
349 KeEnterGuardedRegion();
350 ASSERT((CurrentThread
->OwnsSystemWorkingSetExclusive
== 0) &&
351 (CurrentThread
->OwnsSystemWorkingSetShared
== 0));
352 CurrentThread
->OwnsSystemWorkingSetExclusive
= 1;
355 // Re-read PTE now that the IRQL has been raised
357 TempPte
= *PointerPte
;
358 if (TempPte
.u
.Hard
.Valid
== 1)
361 // Only two things can go wrong here:
362 // Executing NX page (we couldn't care less)
363 // Writing to a read-only page (the stuff ARM3 works with is write,
364 // so again, moot point.
366 if (StoreInstruction
)
368 DPRINT1("Should NEVER happen on ARM3!!!\n");
369 return STATUS_ACCESS_VIOLATION
;
373 // Otherwise, the PDE was probably invalid, and all is good now
375 return STATUS_SUCCESS
;
379 // We don't implement prototype PTEs
381 ASSERT(TempPte
.u
.Soft
.Prototype
== 0);
384 // We don't implement transition PTEs
386 ASSERT(TempPte
.u
.Soft
.Transition
== 0);
389 // Now do the real fault handling
391 Status
= MiDispatchFault(StoreInstruction
,
403 ASSERT(KeAreAllApcsDisabled() == TRUE
);
404 CurrentThread
->OwnsSystemWorkingSetExclusive
= 0;
405 KeLeaveGuardedRegion();
406 KeLowerIrql(LockIrql
);
411 DPRINT("Fault resolved with status: %lx\n", Status
);
418 DPRINT1("WARNING: USER MODE FAULT IN ARM3???\n");
419 return STATUS_ACCESS_VIOLATION
;