/*1* arch/cris/mm/fault.c2*3* Copyright (C) 2000-2010 Axis Communications AB4*/56#include <linux/mm.h>7#include <linux/interrupt.h>8#include <linux/module.h>9#include <linux/wait.h>10#include <asm/uaccess.h>1112extern int find_fixup_code(struct pt_regs *);13extern void die_if_kernel(const char *, struct pt_regs *, long);14extern void show_registers(struct pt_regs *regs);1516/* debug of low-level TLB reload */17#undef DEBUG1819#ifdef DEBUG20#define D(x) x21#else22#define D(x)23#endif2425/* debug of higher-level faults */26#define DPG(x)2728/* current active page directory */2930DEFINE_PER_CPU(pgd_t *, current_pgd);31unsigned long cris_signal_return_page;3233/*34* This routine handles page faults. It determines the address,35* and the problem, and then passes it off to one of the appropriate36* routines.37*38* Notice that the address we're given is aligned to the page the fault39* occurred in, since we only get the PFN in R_MMU_CAUSE not the complete40* address.41*42* error_code:43* bit 0 == 0 means no page found, 1 means protection fault44* bit 1 == 0 means read, 1 means write45*46* If this routine detects a bad access, it returns 1, otherwise it47* returns 0.48*/4950asmlinkage void51do_page_fault(unsigned long address, struct pt_regs *regs,52int protection, int writeaccess)53{54struct task_struct *tsk;55struct mm_struct *mm;56struct vm_area_struct * vma;57siginfo_t info;58int fault;5960D(printk(KERN_DEBUG61"Page fault for %lX on %X at %lX, prot %d write %d\n",62address, smp_processor_id(), instruction_pointer(regs),63protection, writeaccess));6465tsk = current;6667/*68* We fault-in kernel-space virtual memory on-demand. The69* 'reference' page table is init_mm.pgd.70*71* NOTE! We MUST NOT take any locks for this case. We may72* be in an interrupt or a critical region, and should73* only copy the information from the master page table,74* nothing more.75*76* NOTE2: This is done so that, when updating the vmalloc77* mappings we don't have to walk all processes pgdirs and78* add the high mappings all at once. Instead we do it as they79* are used. However vmalloc'ed page entries have the PAGE_GLOBAL80* bit set so sometimes the TLB can use a lingering entry.81*82* This verifies that the fault happens in kernel space83* and that the fault was not a protection error (error_code & 1).84*/8586if (address >= VMALLOC_START &&87!protection &&88!user_mode(regs))89goto vmalloc_fault;9091/* When stack execution is not allowed we store the signal92* trampolines in the reserved cris_signal_return_page.93* Handle this in the exact same way as vmalloc (we know94* that the mapping is there and is valid so no need to95* call handle_mm_fault).96*/97if (cris_signal_return_page &&98address == cris_signal_return_page &&99!protection && user_mode(regs))100goto vmalloc_fault;101102/* we can and should enable interrupts at this point */103local_irq_enable();104105mm = tsk->mm;106info.si_code = SEGV_MAPERR;107108/*109* If we're in an interrupt or "atomic" operation or have no110* user context, we must not take the fault.111*/112113if (in_atomic() || !mm)114goto no_context;115116down_read(&mm->mmap_sem);117vma = find_vma(mm, address);118if (!vma)119goto bad_area;120if (vma->vm_start <= address)121goto good_area;122if (!(vma->vm_flags & VM_GROWSDOWN))123goto bad_area;124if (user_mode(regs)) {125/*126* accessing the stack below usp is always a bug.127* we get page-aligned addresses so we can only check128* if we're within a page from usp, but that might be129* enough to catch brutal errors at least.130*/131if (address + PAGE_SIZE < rdusp())132goto bad_area;133}134if (expand_stack(vma, address))135goto bad_area;136137/*138* Ok, we have a good vm_area for this memory access, so139* we can handle it..140*/141142good_area:143info.si_code = SEGV_ACCERR;144145/* first do some preliminary protection checks */146147if (writeaccess == 2){148if (!(vma->vm_flags & VM_EXEC))149goto bad_area;150} else if (writeaccess == 1) {151if (!(vma->vm_flags & VM_WRITE))152goto bad_area;153} else {154if (!(vma->vm_flags & (VM_READ | VM_EXEC)))155goto bad_area;156}157158/*159* If for any reason at all we couldn't handle the fault,160* make sure we exit gracefully rather than endlessly redo161* the fault.162*/163164fault = handle_mm_fault(mm, vma, address, (writeaccess & 1) ? FAULT_FLAG_WRITE : 0);165if (unlikely(fault & VM_FAULT_ERROR)) {166if (fault & VM_FAULT_OOM)167goto out_of_memory;168else if (fault & VM_FAULT_SIGBUS)169goto do_sigbus;170BUG();171}172if (fault & VM_FAULT_MAJOR)173tsk->maj_flt++;174else175tsk->min_flt++;176177up_read(&mm->mmap_sem);178return;179180/*181* Something tried to access memory that isn't in our memory map..182* Fix it, but check if it's kernel or user first..183*/184185bad_area:186up_read(&mm->mmap_sem);187188bad_area_nosemaphore:189DPG(show_registers(regs));190191/* User mode accesses just cause a SIGSEGV */192193if (user_mode(regs)) {194printk(KERN_NOTICE "%s (pid %d) segfaults for page "195"address %08lx at pc %08lx\n",196tsk->comm, tsk->pid,197address, instruction_pointer(regs));198199/* With DPG on, we've already dumped registers above. */200DPG(if (0))201show_registers(regs);202203#ifdef CONFIG_NO_SEGFAULT_TERMINATION204DECLARE_WAIT_QUEUE_HEAD(wq);205wait_event_interruptible(wq, 0 == 1);206#else207info.si_signo = SIGSEGV;208info.si_errno = 0;209/* info.si_code has been set above */210info.si_addr = (void *)address;211force_sig_info(SIGSEGV, &info, tsk);212#endif213return;214}215216no_context:217218/* Are we prepared to handle this kernel fault?219*220* (The kernel has valid exception-points in the source221* when it accesses user-memory. When it fails in one222* of those points, we find it in a table and do a jump223* to some fixup code that loads an appropriate error224* code)225*/226227if (find_fixup_code(regs))228return;229230/*231* Oops. The kernel tried to access some bad page. We'll have to232* terminate things with extreme prejudice.233*/234235if (!oops_in_progress) {236oops_in_progress = 1;237if ((unsigned long) (address) < PAGE_SIZE)238printk(KERN_ALERT "Unable to handle kernel NULL "239"pointer dereference");240else241printk(KERN_ALERT "Unable to handle kernel access"242" at virtual address %08lx\n", address);243244die_if_kernel("Oops", regs, (writeaccess << 1) | protection);245oops_in_progress = 0;246}247248do_exit(SIGKILL);249250/*251* We ran out of memory, or some other thing happened to us that made252* us unable to handle the page fault gracefully.253*/254255out_of_memory:256up_read(&mm->mmap_sem);257if (!user_mode(regs))258goto no_context;259pagefault_out_of_memory();260return;261262do_sigbus:263up_read(&mm->mmap_sem);264265/*266* Send a sigbus, regardless of whether we were in kernel267* or user mode.268*/269info.si_signo = SIGBUS;270info.si_errno = 0;271info.si_code = BUS_ADRERR;272info.si_addr = (void *)address;273force_sig_info(SIGBUS, &info, tsk);274275/* Kernel mode? Handle exceptions or die */276if (!user_mode(regs))277goto no_context;278return;279280vmalloc_fault:281{282/*283* Synchronize this task's top level page-table284* with the 'reference' page table.285*286* Use current_pgd instead of tsk->active_mm->pgd287* since the latter might be unavailable if this288* code is executed in a misfortunately run irq289* (like inside schedule() between switch_mm and290* switch_to...).291*/292293int offset = pgd_index(address);294pgd_t *pgd, *pgd_k;295pud_t *pud, *pud_k;296pmd_t *pmd, *pmd_k;297pte_t *pte_k;298299pgd = (pgd_t *)per_cpu(current_pgd, smp_processor_id()) + offset;300pgd_k = init_mm.pgd + offset;301302/* Since we're two-level, we don't need to do both303* set_pgd and set_pmd (they do the same thing). If304* we go three-level at some point, do the right thing305* with pgd_present and set_pgd here.306*307* Also, since the vmalloc area is global, we don't308* need to copy individual PTE's, it is enough to309* copy the pgd pointer into the pte page of the310* root task. If that is there, we'll find our pte if311* it exists.312*/313314pud = pud_offset(pgd, address);315pud_k = pud_offset(pgd_k, address);316if (!pud_present(*pud_k))317goto no_context;318319pmd = pmd_offset(pud, address);320pmd_k = pmd_offset(pud_k, address);321322if (!pmd_present(*pmd_k))323goto bad_area_nosemaphore;324325set_pmd(pmd, *pmd_k);326327/* Make sure the actual PTE exists as well to328* catch kernel vmalloc-area accesses to non-mapped329* addresses. If we don't do this, this will just330* silently loop forever.331*/332333pte_k = pte_offset_kernel(pmd_k, address);334if (!pte_present(*pte_k))335goto no_context;336337return;338}339}340341/* Find fixup code. */342int343find_fixup_code(struct pt_regs *regs)344{345const struct exception_table_entry *fixup;346/* in case of delay slot fault (v32) */347unsigned long ip = (instruction_pointer(regs) & ~0x1);348349fixup = search_exception_tables(ip);350if (fixup != 0) {351/* Adjust the instruction pointer in the stackframe. */352instruction_pointer(regs) = fixup->fixup;353arch_fixup(regs);354return 1;355}356357return 0;358}359360361