/*1* arch/microblaze/mm/fault.c2*3* Copyright (C) 2007 Xilinx, Inc. All rights reserved.4*5* Derived from "arch/ppc/mm/fault.c"6* Copyright (C) 1995-1996 Gary Thomas ([email protected])7*8* Derived from "arch/i386/mm/fault.c"9* Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds10*11* Modified by Cort Dougan and Paul Mackerras.12*13* This file is subject to the terms and conditions of the GNU General14* Public License. See the file COPYING in the main directory of this15* archive for more details.16*17*/1819#include <linux/module.h>20#include <linux/signal.h>21#include <linux/sched.h>22#include <linux/kernel.h>23#include <linux/errno.h>24#include <linux/string.h>25#include <linux/types.h>26#include <linux/ptrace.h>27#include <linux/mman.h>28#include <linux/mm.h>29#include <linux/interrupt.h>3031#include <asm/page.h>32#include <asm/pgtable.h>33#include <asm/mmu.h>34#include <asm/mmu_context.h>35#include <asm/system.h>36#include <linux/uaccess.h>37#include <asm/exceptions.h>3839static unsigned long pte_misses; /* updated by do_page_fault() */40static unsigned long pte_errors; /* updated by do_page_fault() */4142/*43* Check whether the instruction at regs->pc is a store using44* an update addressing form which will update r1.45*/46static int store_updates_sp(struct pt_regs *regs)47{48unsigned int inst;4950if (get_user(inst, (unsigned int __user *)regs->pc))51return 0;52/* check for 1 in the rD field */53if (((inst >> 21) & 0x1f) != 1)54return 0;55/* check for store opcodes */56if ((inst & 0xd0000000) == 0xd0000000)57return 1;58return 0;59}606162/*63* bad_page_fault is called when we have a bad access from the kernel.64* It is called from do_page_fault above and from some of the procedures65* in traps.c.66*/67void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)68{69const struct exception_table_entry *fixup;70/* MS: no context */71/* Are we prepared to handle this fault? */72fixup = search_exception_tables(regs->pc);73if (fixup) {74regs->pc = fixup->fixup;75return;76}7778/* kernel has accessed a bad area */79die("kernel access of bad area", regs, sig);80}8182/*83* The error_code parameter is ESR for a data fault,84* 0 for an instruction fault.85*/86void do_page_fault(struct pt_regs *regs, unsigned long address,87unsigned long error_code)88{89struct vm_area_struct *vma;90struct mm_struct *mm = current->mm;91siginfo_t info;92int code = SEGV_MAPERR;93int is_write = error_code & ESR_S;94int fault;9596regs->ear = address;97regs->esr = error_code;9899/* On a kernel SLB miss we can only check for a valid exception entry */100if (unlikely(kernel_mode(regs) && (address >= TASK_SIZE))) {101printk(KERN_WARNING "kernel task_size exceed");102_exception(SIGSEGV, regs, code, address);103}104105/* for instr TLB miss and instr storage exception ESR_S is undefined */106if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)107is_write = 0;108109if (unlikely(in_atomic() || !mm)) {110if (kernel_mode(regs))111goto bad_area_nosemaphore;112113/* in_atomic() in user mode is really bad,114as is current->mm == NULL. */115printk(KERN_EMERG "Page fault in user mode with "116"in_atomic(), mm = %p\n", mm);117printk(KERN_EMERG "r15 = %lx MSR = %lx\n",118regs->r15, regs->msr);119die("Weird page fault", regs, SIGSEGV);120}121122/* When running in the kernel we expect faults to occur only to123* addresses in user space. All other faults represent errors in the124* kernel and should generate an OOPS. Unfortunately, in the case of an125* erroneous fault occurring in a code path which already holds mmap_sem126* we will deadlock attempting to validate the fault against the127* address space. Luckily the kernel only validly references user128* space from well defined areas of code, which are listed in the129* exceptions table.130*131* As the vast majority of faults will be valid we will only perform132* the source reference check when there is a possibility of a deadlock.133* Attempt to lock the address space, if we cannot we then validate the134* source. If this is invalid we can skip the address space check,135* thus avoiding the deadlock.136*/137if (unlikely(!down_read_trylock(&mm->mmap_sem))) {138if (kernel_mode(regs) && !search_exception_tables(regs->pc))139goto bad_area_nosemaphore;140141down_read(&mm->mmap_sem);142}143144vma = find_vma(mm, address);145if (unlikely(!vma))146goto bad_area;147148if (vma->vm_start <= address)149goto good_area;150151if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))152goto bad_area;153154if (unlikely(!is_write))155goto bad_area;156157/*158* N.B. The ABI allows programs to access up to159* a few hundred bytes below the stack pointer (TBD).160* The kernel signal delivery code writes up to about 1.5kB161* below the stack pointer (r1) before decrementing it.162* The exec code can write slightly over 640kB to the stack163* before setting the user r1. Thus we allow the stack to164* expand to 1MB without further checks.165*/166if (unlikely(address + 0x100000 < vma->vm_end)) {167168/* get user regs even if this fault is in kernel mode */169struct pt_regs *uregs = current->thread.regs;170if (uregs == NULL)171goto bad_area;172173/*174* A user-mode access to an address a long way below175* the stack pointer is only valid if the instruction176* is one which would update the stack pointer to the177* address accessed if the instruction completed,178* i.e. either stwu rs,n(r1) or stwux rs,r1,rb179* (or the byte, halfword, float or double forms).180*181* If we don't check this then any write to the area182* between the last mapped region and the stack will183* expand the stack rather than segfaulting.184*/185if (address + 2048 < uregs->r1186&& (kernel_mode(regs) || !store_updates_sp(regs)))187goto bad_area;188}189if (expand_stack(vma, address))190goto bad_area;191192good_area:193code = SEGV_ACCERR;194195/* a write */196if (unlikely(is_write)) {197if (unlikely(!(vma->vm_flags & VM_WRITE)))198goto bad_area;199/* a read */200} else {201/* protection fault */202if (unlikely(error_code & 0x08000000))203goto bad_area;204if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC))))205goto bad_area;206}207208/*209* If for any reason at all we couldn't handle the fault,210* make sure we exit gracefully rather than endlessly redo211* the fault.212*/213fault = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);214if (unlikely(fault & VM_FAULT_ERROR)) {215if (fault & VM_FAULT_OOM)216goto out_of_memory;217else if (fault & VM_FAULT_SIGBUS)218goto do_sigbus;219BUG();220}221if (unlikely(fault & VM_FAULT_MAJOR))222current->maj_flt++;223else224current->min_flt++;225up_read(&mm->mmap_sem);226/*227* keep track of tlb+htab misses that are good addrs but228* just need pte's created via handle_mm_fault()229* -- Cort230*/231pte_misses++;232return;233234bad_area:235up_read(&mm->mmap_sem);236237bad_area_nosemaphore:238pte_errors++;239240/* User mode accesses cause a SIGSEGV */241if (user_mode(regs)) {242_exception(SIGSEGV, regs, code, address);243/* info.si_signo = SIGSEGV;244info.si_errno = 0;245info.si_code = code;246info.si_addr = (void *) address;247force_sig_info(SIGSEGV, &info, current);*/248return;249}250251bad_page_fault(regs, address, SIGSEGV);252return;253254/*255* We ran out of memory, or some other thing happened to us that made256* us unable to handle the page fault gracefully.257*/258out_of_memory:259up_read(&mm->mmap_sem);260if (!user_mode(regs))261bad_page_fault(regs, address, SIGKILL);262else263pagefault_out_of_memory();264return;265266do_sigbus:267up_read(&mm->mmap_sem);268if (user_mode(regs)) {269info.si_signo = SIGBUS;270info.si_errno = 0;271info.si_code = BUS_ADRERR;272info.si_addr = (void __user *)address;273force_sig_info(SIGBUS, &info, current);274return;275}276bad_page_fault(regs, address, SIGBUS);277}278279280