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
;
31 // Check if this is a fault while trying to access the page table itself
33 if ((Address
>= (PVOID
)MiAddressToPte(MmSystemRangeStart
)) &&
34 (Address
< (PVOID
)PTE_TOP
))
37 // Send a hint to the page fault handler that this is only a valid fault
38 // if we already detected this was access within the page table range
40 PointerPde
= MiAddressToPte(Address
);
41 Status
= STATUS_WAIT_1
;
43 else if (Address
< MmSystemRangeStart
)
46 // This is totally illegal
48 return STATUS_ACCESS_VIOLATION
;
53 // Get the PDE for the address
55 PointerPde
= MiAddressToPde(Address
);
59 // Check if it's not valid
61 if (PointerPde
->u
.Hard
.Valid
== 0)
64 // Copy it from our double-mapped system page directory
66 InterlockedExchangePte(PointerPde
,
67 MmSystemPagePtes
[((ULONG_PTR
)PointerPde
&
69 sizeof(MMPTE
)].u
.Long
);
80 MiResolveDemandZeroFault(IN PVOID Address
,
85 PFN_NUMBER PageFrameNumber
;
87 DPRINT("ARM3 Demand Zero Page Fault Handler for address: %p in process: %p\n",
92 // Lock the PFN database
94 OldIrql
= KeAcquireQueuedSpinLock(LockQueuePfnLock
);
95 ASSERT(PointerPte
->u
.Hard
.Valid
== 0);
100 PageFrameNumber
= MmAllocPage(MC_PPOOL
, 0);
101 DPRINT("New pool page: %lx\n", PageFrameNumber
);
106 KeReleaseQueuedSpinLock(LockQueuePfnLock
, OldIrql
);
109 // Increment demand zero faults
111 InterlockedIncrement(&KeGetCurrentPrcb()->MmDemandZeroCount
);
116 TempPte
= HyperTemplatePte
;
117 TempPte
.u
.Hard
.PageFrameNumber
= PageFrameNumber
;
118 *PointerPte
= TempPte
;
119 ASSERT(PointerPte
->u
.Hard
.Valid
== 1);
124 DPRINT("Paged pool page has now been paged in\n");
125 return STATUS_PAGE_FAULT_DEMAND_ZERO
;
130 MiDispatchFault(IN BOOLEAN StoreInstruction
,
132 IN PMMPTE PointerPte
,
133 IN PMMPTE PrototypePte
,
134 IN BOOLEAN Recursive
,
135 IN PEPROCESS Process
,
136 IN PVOID TrapInformation
,
142 DPRINT("ARM3 Page Fault Dispatcher for address: %p in process: %p\n",
147 // Make sure APCs are off and we're not at dispatch
149 OldIrql
= KeGetCurrentIrql ();
150 ASSERT(OldIrql
<= APC_LEVEL
);
151 ASSERT(KeAreAllApcsDisabled () == TRUE
);
154 // Grab a copy of the PTE
156 TempPte
= *PointerPte
;
159 // The PTE must be invalid, but not totally blank
161 ASSERT(TempPte
.u
.Hard
.Valid
== 0);
162 ASSERT(TempPte
.u
.Long
!= 0);
165 // No prototype, transition or page file software PTEs in ARM3 yet
167 ASSERT(TempPte
.u
.Soft
.Prototype
== 0);
168 ASSERT(TempPte
.u
.Soft
.Transition
== 0);
169 ASSERT(TempPte
.u
.Soft
.PageFileHigh
== 0);
172 // If we got this far, the PTE can only be a demand zero PTE, which is what
173 // we want. Go handle it!
175 Status
= MiResolveDemandZeroFault(Address
,
179 if (NT_SUCCESS(Status
))
182 // Make sure we're returning in a sane state and pass the status down
184 ASSERT(OldIrql
== KeGetCurrentIrql ());
185 ASSERT(KeGetCurrentIrql() <= APC_LEVEL
);
190 // Generate an access fault
192 return STATUS_ACCESS_VIOLATION
;
197 MmArmAccessFault(IN BOOLEAN StoreInstruction
,
199 IN KPROCESSOR_MODE Mode
,
200 IN PVOID TrapInformation
)
202 KIRQL OldIrql
= KeGetCurrentIrql(), LockIrql
;
205 PETHREAD CurrentThread
;
207 DPRINT("ARM3 FAULT AT: %p\n", Address
);
210 // Get the PTE and PDE
212 PointerPte
= MiAddressToPte(Address
);
215 // Check for dispatch-level snafu
217 if (OldIrql
> APC_LEVEL
)
220 // There are some special cases where this is okay, but not in ARM3 yet
222 DbgPrint("MM:***PAGE FAULT AT IRQL > 1 Va %p, IRQL %lx\n",
225 ASSERT(OldIrql
<= APC_LEVEL
);
229 // Check for kernel fault
231 if (Address
>= MmSystemRangeStart
)
234 // What are you even DOING here?
236 if (Mode
== UserMode
) return STATUS_ACCESS_VIOLATION
;
241 if (!MiIsPdeForAddressValid(Address
))
246 DPRINT("Invalid PDE\n");
249 // Handle mapping in "Special" PDE directoreis
251 MiCheckPdeForPagedPool(Address
);
254 // Now we SHOULD be good
256 if (!MiIsPdeForAddressValid(Address
))
259 // FIXFIX: Do the S-LIST hack
265 KeBugCheckEx(PAGE_FAULT_IN_NONPAGED_AREA
,
268 (ULONG_PTR
)TrapInformation
,
274 // The PDE is valid, so read the PTE
276 TempPte
= *PointerPte
;
277 if (TempPte
.u
.Hard
.Valid
== 1)
280 // Only two things can go wrong here:
281 // Executing NX page (we couldn't care less)
282 // Writing to a read-only page (the stuff ARM3 works with is write,
283 // so again, moot point).
285 if (StoreInstruction
)
287 DPRINT1("Should NEVER happen on ARM3!!!\n");
288 return STATUS_ACCESS_VIOLATION
;
292 // Otherwise, the PDE was probably invalid, and all is good now
294 return STATUS_SUCCESS
;
298 // Check for a fault on the page table or hyperspace itself
300 if ((Address
>= (PVOID
)PTE_BASE
) && (Address
<= MmHyperSpaceEnd
))
303 // This might happen...not sure yet
305 DPRINT1("FAULT ON PAGE TABLES!\n");
308 // Map in the page table
310 if (MiCheckPdeForPagedPool(Address
) == STATUS_WAIT_1
)
312 DPRINT1("PAGE TABLES FAULTED IN!\n");
313 return STATUS_SUCCESS
;
317 // Otherwise the page table doesn't actually exist
319 DPRINT1("FAILING\n");
320 return STATUS_ACCESS_VIOLATION
;
324 // Now we must raise to APC_LEVEL and mark the thread as owner
325 // We don't actually implement a working set pushlock, so this is only
326 // for internal consistency (and blocking APCs)
328 KeRaiseIrql(APC_LEVEL
, &LockIrql
);
329 CurrentThread
= PsGetCurrentThread();
330 KeEnterGuardedRegion();
331 ASSERT((CurrentThread
->OwnsSystemWorkingSetExclusive
== 0) &&
332 (CurrentThread
->OwnsSystemWorkingSetShared
== 0));
333 CurrentThread
->OwnsSystemWorkingSetExclusive
= 1;
336 // Re-read PTE now that the IRQL has been raised
338 TempPte
= *PointerPte
;
339 if (TempPte
.u
.Hard
.Valid
== 1)
342 // Only two things can go wrong here:
343 // Executing NX page (we couldn't care less)
344 // Writing to a read-only page (the stuff ARM3 works with is write,
345 // so again, moot point.
347 if (StoreInstruction
)
349 DPRINT1("Should NEVER happen on ARM3!!!\n");
350 return STATUS_ACCESS_VIOLATION
;
354 // Otherwise, the PDE was probably invalid, and all is good now
356 return STATUS_SUCCESS
;
360 // We don't implement prototype PTEs
362 ASSERT(TempPte
.u
.Soft
.Prototype
== 0);
365 // We don't implement transition PTEs
367 ASSERT(TempPte
.u
.Soft
.Transition
== 0);
370 // Now do the real fault handling
372 Status
= MiDispatchFault(StoreInstruction
,
384 ASSERT(KeAreAllApcsDisabled() == TRUE
);
385 CurrentThread
->OwnsSystemWorkingSetExclusive
= 0;
386 KeLeaveGuardedRegion();
387 KeLowerIrql(LockIrql
);
392 DPRINT("Fault resolved with status: %lx\n", Status
);
399 DPRINT1("WARNING: USER MODE FAULT IN ARM3???\n");
400 return STATUS_ACCESS_VIOLATION
;