summaryrefslogtreecommitdiffstats
path: root/arch/x86_64/pagefault.c
blob: 5bba20b61351e2b56900fbbe086c1ca9d6f92ec8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "arch/x86_64/idt.h"
#include "arch/processor.h"
#include "memory.h"
#include "print.h"
#include "jove.h"

#include <stdbool.h>

int_state_t*
_pagefault_handler(int_state_t *state)
{
    extern uint64_t __isr_err;

    uintptr_t fault_addr = 0;
    __asm__ volatile("movq %%cr2, %0": "=r"(fault_addr));

    if(fault_addr < USERLAND_MEMORY_LIMIT) {
        // Check if there is an entry in the exception return table.
        processor_t *proc = processor_current();
        if(proc->ert_i > 0) {
            state->ip = (uintptr_t)_exrtab_pop();
            return state;
        }
    }

    bool present = __isr_err & 1;
    bool write = __isr_err & 2;
    bool user = __isr_err & 4;

    klogf("Page fault at %016X\n", fault_addr);
    klogf("%s %s from a %s address\n", 
            user ? "user" : "kernel",
            write ? "wrote" : "read",
            present ? "present" : "non-present");

    int_state_print(state);
    kpanic("Unhandled page fault\n");
    return state;
}