···11+/*22+ * Copyright (C) 2010, Tobias Klauser <tklauser@distanz.ch>33+ * Copyright (C) 2009, Wind River Systems Inc44+ * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com55+ *66+ * This file is subject to the terms and conditions of the GNU General Public77+ * License. See the file "COPYING" in the main directory of this archive88+ * for more details.99+ */1010+1111+#include <linux/module.h>1212+#include <linux/uaccess.h>1313+1414+int fixup_exception(struct pt_regs *regs)1515+{1616+ const struct exception_table_entry *fixup;1717+1818+ fixup = search_exception_tables(regs->ea);1919+ if (fixup) {2020+ regs->ea = fixup->fixup;2121+ return 1;2222+ }2323+2424+ return 0;2525+}
+251
arch/nios2/mm/fault.c
···11+/*22+ * Copyright (C) 2009 Wind River Systems Inc33+ * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com44+ *55+ * based on arch/mips/mm/fault.c which is:66+ *77+ * Copyright (C) 1995-2000 Ralf Baechle88+ *99+ * This file is subject to the terms and conditions of the GNU General Public1010+ * License. See the file "COPYING" in the main directory of this archive1111+ * for more details.1212+ */1313+1414+#include <linux/signal.h>1515+#include <linux/sched.h>1616+#include <linux/interrupt.h>1717+#include <linux/kernel.h>1818+#include <linux/errno.h>1919+#include <linux/string.h>2020+#include <linux/types.h>2121+#include <linux/ptrace.h>2222+#include <linux/mman.h>2323+#include <linux/mm.h>2424+#include <linux/module.h>2525+#include <linux/uaccess.h>2626+#include <linux/ptrace.h>2727+2828+#include <asm/mmu_context.h>2929+#include <asm/traps.h>3030+3131+#define EXC_SUPERV_INSN_ACCESS 9 /* Supervisor only instruction address */3232+#define EXC_SUPERV_DATA_ACCESS 11 /* Supervisor only data address */3333+#define EXC_X_PROTECTION_FAULT 13 /* TLB permission violation (x) */3434+#define EXC_R_PROTECTION_FAULT 14 /* TLB permission violation (r) */3535+#define EXC_W_PROTECTION_FAULT 15 /* TLB permission violation (w) */3636+3737+/*3838+ * This routine handles page faults. It determines the address,3939+ * and the problem, and then passes it off to one of the appropriate4040+ * routines.4141+ */4242+asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,4343+ unsigned long address)4444+{4545+ struct vm_area_struct *vma = NULL;4646+ struct task_struct *tsk = current;4747+ struct mm_struct *mm = tsk->mm;4848+ int code = SEGV_MAPERR;4949+ int fault;5050+ unsigned int flags = 0;5151+5252+ cause >>= 2;5353+5454+ /* Restart the instruction */5555+ regs->ea -= 4;5656+5757+ /*5858+ * We fault-in kernel-space virtual memory on-demand. The5959+ * 'reference' page table is init_mm.pgd.6060+ *6161+ * NOTE! We MUST NOT take any locks for this case. We may6262+ * be in an interrupt or a critical region, and should6363+ * only copy the information from the master page table,6464+ * nothing more.6565+ */6666+ if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END)) {6767+ if (user_mode(regs))6868+ goto bad_area_nosemaphore;6969+ else7070+ goto vmalloc_fault;7171+ }7272+7373+ if (unlikely(address >= TASK_SIZE))7474+ goto bad_area_nosemaphore;7575+7676+ /*7777+ * If we're in an interrupt or have no user7878+ * context, we must not take the fault..7979+ */8080+ if (in_atomic() || !mm)8181+ goto bad_area_nosemaphore;8282+8383+ if (user_mode(regs))8484+ flags |= FAULT_FLAG_USER;8585+8686+ if (!down_read_trylock(&mm->mmap_sem)) {8787+ if (!user_mode(regs) && !search_exception_tables(regs->ea))8888+ goto bad_area_nosemaphore;8989+ down_read(&mm->mmap_sem);9090+ }9191+9292+ vma = find_vma(mm, address);9393+ if (!vma)9494+ goto bad_area;9595+ if (vma->vm_start <= address)9696+ goto good_area;9797+ if (!(vma->vm_flags & VM_GROWSDOWN))9898+ goto bad_area;9999+ if (expand_stack(vma, address))100100+ goto bad_area;101101+/*102102+ * Ok, we have a good vm_area for this memory access, so103103+ * we can handle it..104104+ */105105+good_area:106106+ code = SEGV_ACCERR;107107+108108+ switch (cause) {109109+ case EXC_SUPERV_INSN_ACCESS:110110+ goto bad_area;111111+ case EXC_SUPERV_DATA_ACCESS:112112+ goto bad_area;113113+ case EXC_X_PROTECTION_FAULT:114114+ if (!(vma->vm_flags & VM_EXEC))115115+ goto bad_area;116116+ break;117117+ case EXC_R_PROTECTION_FAULT:118118+ if (!(vma->vm_flags & VM_READ))119119+ goto bad_area;120120+ break;121121+ case EXC_W_PROTECTION_FAULT:122122+ if (!(vma->vm_flags & VM_WRITE))123123+ goto bad_area;124124+ flags = FAULT_FLAG_WRITE;125125+ break;126126+ }127127+128128+survive:129129+ /*130130+ * If for any reason at all we couldn't handle the fault,131131+ * make sure we exit gracefully rather than endlessly redo132132+ * the fault.133133+ */134134+ fault = handle_mm_fault(mm, vma, address, flags);135135+ if (unlikely(fault & VM_FAULT_ERROR)) {136136+ if (fault & VM_FAULT_OOM)137137+ goto out_of_memory;138138+ else if (fault & VM_FAULT_SIGBUS)139139+ goto do_sigbus;140140+ BUG();141141+ }142142+ if (fault & VM_FAULT_MAJOR)143143+ tsk->maj_flt++;144144+ else145145+ tsk->min_flt++;146146+147147+ up_read(&mm->mmap_sem);148148+ return;149149+150150+/*151151+ * Something tried to access memory that isn't in our memory map..152152+ * Fix it, but check if it's kernel or user first..153153+ */154154+bad_area:155155+ up_read(&mm->mmap_sem);156156+157157+bad_area_nosemaphore:158158+ /* User mode accesses just cause a SIGSEGV */159159+ if (user_mode(regs)) {160160+ pr_alert("%s: unhandled page fault (%d) at 0x%08lx, "161161+ "cause %ld\n", current->comm, SIGSEGV, address, cause);162162+ show_regs(regs);163163+ _exception(SIGSEGV, regs, code, address);164164+ return;165165+ }166166+167167+no_context:168168+ /* Are we prepared to handle this kernel fault? */169169+ if (fixup_exception(regs))170170+ return;171171+172172+ /*173173+ * Oops. The kernel tried to access some bad page. We'll have to174174+ * terminate things with extreme prejudice.175175+ */176176+ bust_spinlocks(1);177177+178178+ pr_alert("Unable to handle kernel %s at virtual address %08lx",179179+ address < PAGE_SIZE ? "NULL pointer dereference" :180180+ "paging request", address);181181+ pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra,182182+ cause);183183+ panic("Oops");184184+ return;185185+186186+/*187187+ * We ran out of memory, or some other thing happened to us that made188188+ * us unable to handle the page fault gracefully.189189+ */190190+out_of_memory:191191+ up_read(&mm->mmap_sem);192192+ if (is_global_init(tsk)) {193193+ yield();194194+ down_read(&mm->mmap_sem);195195+ goto survive;196196+ }197197+ if (!user_mode(regs))198198+ goto no_context;199199+ pagefault_out_of_memory();200200+ return;201201+202202+do_sigbus:203203+ up_read(&mm->mmap_sem);204204+205205+ /* Kernel mode? Handle exceptions or die */206206+ if (!user_mode(regs))207207+ goto no_context;208208+209209+ _exception(SIGBUS, regs, BUS_ADRERR, address);210210+ return;211211+212212+vmalloc_fault:213213+ {214214+ /*215215+ * Synchronize this task's top level page-table216216+ * with the 'reference' page table.217217+ *218218+ * Do _not_ use "tsk" here. We might be inside219219+ * an interrupt in the middle of a task switch..220220+ */221221+ int offset = pgd_index(address);222222+ pgd_t *pgd, *pgd_k;223223+ pud_t *pud, *pud_k;224224+ pmd_t *pmd, *pmd_k;225225+ pte_t *pte_k;226226+227227+ pgd = pgd_current + offset;228228+ pgd_k = init_mm.pgd + offset;229229+230230+ if (!pgd_present(*pgd_k))231231+ goto no_context;232232+ set_pgd(pgd, *pgd_k);233233+234234+ pud = pud_offset(pgd, address);235235+ pud_k = pud_offset(pgd_k, address);236236+ if (!pud_present(*pud_k))237237+ goto no_context;238238+ pmd = pmd_offset(pud, address);239239+ pmd_k = pmd_offset(pud_k, address);240240+ if (!pmd_present(*pmd_k))241241+ goto no_context;242242+ set_pmd(pmd, *pmd_k);243243+244244+ pte_k = pte_offset_kernel(pmd_k, address);245245+ if (!pte_present(*pte_k))246246+ goto no_context;247247+248248+ flush_tlb_one(address);249249+ return;250250+ }251251+}