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);
23 extern void __kernel_heap_start;
26 intr_routine_page_fault(const isr_param* param)
28 uintptr_t ptr = cpu_rcr2();
34 if (!vmm_lookup(ptr, &mapping)) {
38 if (!SEL_RPL(param->cs)) {
40 if (do_kernel(&mapping)) {
46 struct mm_region* hit_region = region_get(__current, ptr);
53 x86_pte_t* pte = PTE_MOUNTED(PD_REFERENCED, ptr >> 12);
54 if (*pte & PG_PRESENT) {
55 if ((hit_region->attr & REGION_PERM_MASK) ==
56 (REGION_RSHARED | REGION_READ)) {
57 // normal page fault, do COW
60 (uintptr_t)vmm_dup_page(__current->pid, PG_ENTRY_ADDR(*pte));
61 pmm_free_page(__current->pid, *pte & ~0xFFF);
62 *pte = (*pte & 0xFFF) | pa | PG_WRITE;
65 // impossible cases or accessing privileged page
73 uintptr_t loc = *pte & ~0xfff;
74 // a writable page, not present, pte attr is not null
75 // and no indication of cached page -> a new page need to be alloc
76 if ((hit_region->attr & REGION_WRITE) && (*pte & 0xfff) && !loc) {
78 uintptr_t pa = pmm_alloc_page(__current->pid, 0);
79 *pte = *pte | pa | PG_PRESENT;
82 // page not present, bring it from disk or somewhere else
83 __print_panic_msg("WIP page fault route", param);
88 kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p)\n",
93 terminate_proc(LXSEGFAULT);
98 do_kernel(v_mapping* mapping)
100 uintptr_t addr = mapping->va;
101 if (addr >= &__kernel_heap_start && addr < L2_BASE_VADDR) {
102 // This is kernel heap page
103 uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0);
104 *mapping->pte = (*mapping->pte & 0xfff) | pa | PG_PRESENT;
105 cpu_invplg(mapping->pte);