The Pedigree Project 0.1
arm64/PageFaultHandler.cc
1#include "pedigree/kernel/Log.h"
2#include "pedigree/kernel/Metrics.h"
3#include "pedigree/kernel/Subsystem.h"
4#include "pedigree/kernel/panic.h"
5#include "pedigree/kernel/process/Process.h"
6#include "pedigree/kernel/process/Thread.h"
7#include "pedigree/kernel/processor/InterruptManager.h"
8#include "pedigree/kernel/processor/PageFaultHandler.h"
9#include "pedigree/kernel/processor/Processor.h"
10#include "pedigree/kernel/processor/VirtualAddressSpace.h"
11#include "pedigree/kernel/processor/state.h"
12
14
18
19void PageFaultHandler::interrupt(size_t, InterruptState& state) {
20 const bool collectMetrics = Processor::m_Initialised == 2;
21 if (collectMetrics) {
22 Metrics::increment(Metrics::PageFault);
23 }
24
25 const uintptr_t address = state.far;
26 const uintptr_t page = address & ~(PAGE_SIZE - 1);
27 const size_t faultStatus = state.esr & 0x3f;
28 const size_t exceptionClass = (state.esr >> 26) & 0x3f;
29 const bool fetch = exceptionClass == 0x20 || exceptionClass == 0x21;
30 const bool write = !fetch && (state.esr & (1U << 6));
31 const bool present = (faultStatus & 0x3c) == 0x0c;
32 VirtualAddressSpace& space = Processor::information().getVirtualAddressSpace();
33 if (present && write &&
34 space.handleCopyOnWriteFault(reinterpret_cast<void*>(page), !state.kernelMode())) {
35 if (collectMetrics) {
36 Metrics::increment(Metrics::PageFaultCopyOnWrite);
37 }
38 return;
39 }
40 if (dispatchHandlers(state, address, write, present)) {
41 if (collectMetrics) {
42 Metrics::increment(Metrics::PageFaultHandled);
43 }
44 return;
45 }
46
47 Thread* thread = Processor::information().getCurrentThread();
48 if (thread && !state.kernelMode()) {
49 Process* process = thread->getParent();
50 if (process && process->getSubsystem()) {
51 const uintptr_t errorCode = (present ? 1 : 0) | (write ? 2 : 0) | 4 | (fetch ? 16 : 0);
52 thread->deferSubsystemException(static_cast<size_t>(Subsystem::PageFault), address,
53 errorCode);
54 if (collectMetrics) {
55 Metrics::increment(Metrics::PageFaultDeferred);
56 }
57 return;
58 }
59 }
60 ERROR("ARM64: page fault at " << Hex << address << " PC " << state.pc << " ESR " << state.esr);
61 panic("ARM64: unrecoverable page fault");
62}
static EXPORTED_PUBLIC InterruptManager & instance()
virtual bool registerInterruptHandler(size_t nInterruptNumber, InterruptHandler *pHandler)=0
static EXPORTED_PUBLIC PageFaultHandler m_Instance
virtual void interrupt(size_t interruptNumber, InterruptState &state)
static ProcessorInformation & information()
static size_t m_Initialised
Definition Processor.h:483
bool deferSubsystemException(size_t type, uintptr_t faultAddress, uintptr_t errorCode)
Definition Thread.cc:3627
Process * getParent() const
Definition Thread.h:340
virtual bool handleCopyOnWriteFault(void *virtualAddress, bool userMode)=0
void EXPORTED_PUBLIC panic(const char *msg) NORETURN
Definition panic.cc:118
@ Hex
Definition Log.h:124