X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/474a5dd282586c31abfefc7953f148acdc226731..c50b9a23b03c677efa3842536c363d368542e30b:/lunaix-os/kernel/asm/x86/pfault.c diff --git a/lunaix-os/kernel/asm/x86/pfault.c b/lunaix-os/kernel/asm/x86/pfault.c index 30fd498..6498619 100644 --- a/lunaix-os/kernel/asm/x86/pfault.c +++ b/lunaix-os/kernel/asm/x86/pfault.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -40,17 +41,17 @@ intr_routine_page_fault(const isr_param* param) if (do_kernel(&mapping)) { return; } - goto segv_term; + // 如果不是,那么看看内核是不是需要用户页。 } struct mm_region* hit_region = region_get(&__current->mm.regions, ptr); if (!hit_region) { - // Into the void... + // 当你凝视深渊时…… goto segv_term; } - x86_pte_t* pte = &PTE_MOUNTED(PD_REFERENCED, ptr >> 12); + volatile x86_pte_t* pte = &PTE_MOUNTED(PD_REFERENCED, ptr >> 12); if ((*pte & PG_PRESENT)) { if ((hit_region->attr & COW_MASK) == COW_MASK) { // normal page fault, do COW @@ -65,10 +66,10 @@ intr_routine_page_fault(const isr_param* param) goto segv_term; } - if (!(*pte)) { - // Invalid location - goto segv_term; - } + // if (!(*pte)) { + // // Invalid location + // goto segv_term; + // } uintptr_t loc = *pte & ~0xfff; @@ -77,22 +78,51 @@ intr_routine_page_fault(const isr_param* param) if ((hit_region->attr & REGION_WRITE) && (*pte & 0xfff) && !loc) { cpu_invplg(pte); uintptr_t pa = pmm_alloc_page(__current->pid, 0); + if (!pa) { + goto oom; + } + *pte = *pte | pa | PG_PRESENT; goto resolved; } + // if mfile is set, then it is a mem map + if (hit_region->mfile) { + struct v_file* file = hit_region->mfile; + u32_t offset = + (ptr - hit_region->start) & (PG_SIZE - 1) + hit_region->offset; + uintptr_t pa = pmm_alloc_page(__current->pid, 0); + + if (!pa) { + goto oom; + } + + cpu_invplg(pte); + *pte = *pte | pa | PG_PRESENT; + int errno = file->ops->read_page( + file->inode, ptr & (PG_SIZE - 1), PG_SIZE, offset); + if (errno < 0) { + kprintf(KERROR "fail to read page (%d)\n", errno); + goto segv_term; + } + goto resolved; + } + // page not present, bring it from disk or somewhere else __print_panic_msg("WIP page fault route", param); while (1) ; +oom: + kprintf(KERROR "out of memory\n"); segv_term: kprintf(KERROR "(pid: %d) Segmentation fault on %p (%p:%p)\n", __current->pid, ptr, param->cs, param->eip); - terminate_proc(LXSEGFAULT); + __SIGSET(__current->sig_pending, _SIGSEGV); + schedule(); // should not reach while (1) ; @@ -106,14 +136,8 @@ int do_kernel(v_mapping* mapping) { uintptr_t addr = mapping->va; - if (addr >= KHEAP_START && addr < PROC_START) { - // This is kernel heap page - uintptr_t pa = pmm_alloc_page(KERNEL_PID, 0); - *mapping->pte = (*mapping->pte & 0xfff) | pa | PG_PRESENT; - cpu_invplg(mapping->pte); - cpu_invplg(addr); - goto done; - } + + // TODO return 0; done: