The Pedigree Project 0.1
armv7/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
17}
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.faultAddress;
26 const uintptr_t page = address & ~(PAGE_SIZE - 1);
27 const bool fetch = state.vector == 3;
28 const bool write = !fetch && (state.faultStatus & (1U << 11));
29 const size_t status = (state.faultStatus & 15) | ((state.faultStatus >> 6) & 16);
30 const bool present = status == 0xd || status == 0xf;
31 VirtualAddressSpace& space = Processor::information().getVirtualAddressSpace();
32 if (present && write &&
33 space.handleCopyOnWriteFault(reinterpret_cast<void*>(page), !state.kernelMode())) {
34 if (collectMetrics) {
35 Metrics::increment(Metrics::PageFaultCopyOnWrite);
36 }
37 return;
38 }
39 if (dispatchHandlers(state, address, write, present)) {
40 if (collectMetrics) {
41 Metrics::increment(Metrics::PageFaultHandled);
42 }
43 return;
44 }
45
46 Thread* thread = Processor::information().getCurrentThread();
47 if (thread && !state.kernelMode()) {
48 Process* process = thread->getParent();
49 if (process && process->getSubsystem()) {
50 const uintptr_t errorCode = (present ? 1 : 0) | (write ? 2 : 0) | 4 | (fetch ? 16 : 0);
51 thread->deferSubsystemException(static_cast<size_t>(Subsystem::PageFault), address,
52 errorCode);
53 if (collectMetrics) {
54 Metrics::increment(Metrics::PageFaultDeferred);
55 }
56 return;
57 }
58 }
59 ERROR("ARMv7: page fault at " << Hex << address << " PC " << state.pc << " FSR "
60 << state.faultStatus);
61 panic("ARMv7: 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