1 #include <arch/x86/interrupts.h>
2 #include <lunaix/common.h>
3 #include <lunaix/mm/mm.h>
4 #include <lunaix/mm/pmm.h>
5 #include <lunaix/mm/region.h>
6 #include <lunaix/mm/vmm.h>
7 #include <lunaix/sched.h>
8 #include <lunaix/status.h>
9 #include <lunaix/syslog.h>
12 kprintf(const char* fmt, ...)
16 __kprintf("PFAULT", fmt, args);
21 __print_panic_msg(const char* msg, const isr_param* param);
24 intr_routine_page_fault(const isr_param* param)
26 uintptr_t ptr = cpu_rcr2();
31 struct mm_region* hit_region = region_get(__current, ptr);
38 x86_pte_t* pte = PTE_MOUNTED(PD_REFERENCED, ptr >> 12);
39 if (*pte & PG_PRESENT) {
40 if ((hit_region->attr & REGION_PERM_MASK) ==
41 (REGION_RSHARED | REGION_READ)) {
42 // normal page fault, do COW
45 (uintptr_t)vmm_dup_page(__current->pid, PG_ENTRY_ADDR(*pte));
46 pmm_free_page(__current->pid, *pte & ~0xFFF);
47 *pte = (*pte & 0xFFF) | pa | PG_WRITE;
50 // impossible cases or accessing privileged page
58 uintptr_t loc = *pte & ~0xfff;
59 // a writable page, not present, pte attr is not null
60 // and no indication of cached page -> a new page need to be alloc
61 if ((hit_region->attr & REGION_WRITE) && (*pte & 0xfff) && !loc) {
63 uintptr_t pa = pmm_alloc_page(__current->pid, 0);
64 *pte = *pte | pa | PG_PRESENT;
67 // page not present, bring it from disk or somewhere else
68 __print_panic_msg("WIP page fault route", param);
73 kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p)\n",
78 terminate_proc(LXSEGFAULT);