···11+/*22+ * arch/microblaze/mm/fault.c33+ *44+ * Copyright (C) 2007 Xilinx, Inc. All rights reserved.55+ *66+ * Derived from "arch/ppc/mm/fault.c"77+ * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)88+ *99+ * Derived from "arch/i386/mm/fault.c"1010+ * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds1111+ *1212+ * Modified by Cort Dougan and Paul Mackerras.1313+ *1414+ * This file is subject to the terms and conditions of the GNU General1515+ * Public License. See the file COPYING in the main directory of this1616+ * archive for more details.1717+ *1818+ */1919+2020+#include <linux/module.h>2121+#include <linux/signal.h>2222+#include <linux/sched.h>2323+#include <linux/kernel.h>2424+#include <linux/errno.h>2525+#include <linux/string.h>2626+#include <linux/types.h>2727+#include <linux/ptrace.h>2828+#include <linux/mman.h>2929+#include <linux/mm.h>3030+#include <linux/interrupt.h>3131+3232+#include <asm/page.h>3333+#include <asm/pgtable.h>3434+#include <asm/mmu.h>3535+#include <asm/mmu_context.h>3636+#include <asm/system.h>3737+#include <linux/uaccess.h>3838+#include <asm/exceptions.h>3939+4040+#if defined(CONFIG_KGDB)4141+int debugger_kernel_faults = 1;4242+#endif4343+4444+static unsigned long pte_misses; /* updated by do_page_fault() */4545+static unsigned long pte_errors; /* updated by do_page_fault() */4646+4747+/*4848+ * Check whether the instruction at regs->pc is a store using4949+ * an update addressing form which will update r1.5050+ */5151+static int store_updates_sp(struct pt_regs *regs)5252+{5353+ unsigned int inst;5454+5555+ if (get_user(inst, (unsigned int *)regs->pc))5656+ return 0;5757+ /* check for 1 in the rD field */5858+ if (((inst >> 21) & 0x1f) != 1)5959+ return 0;6060+ /* check for store opcodes */6161+ if ((inst & 0xd0000000) == 0xd0000000)6262+ return 1;6363+ return 0;6464+}6565+6666+6767+/*6868+ * bad_page_fault is called when we have a bad access from the kernel.6969+ * It is called from do_page_fault above and from some of the procedures7070+ * in traps.c.7171+ */7272+static void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)7373+{7474+ const struct exception_table_entry *fixup;7575+/* MS: no context */7676+ /* Are we prepared to handle this fault? */7777+ fixup = search_exception_tables(regs->pc);7878+ if (fixup) {7979+ regs->pc = fixup->fixup;8080+ return;8181+ }8282+8383+ /* kernel has accessed a bad area */8484+#if defined(CONFIG_KGDB)8585+ if (debugger_kernel_faults)8686+ debugger(regs);8787+#endif8888+ die("kernel access of bad area", regs, sig);8989+}9090+9191+/*9292+ * The error_code parameter is ESR for a data fault,9393+ * 0 for an instruction fault.9494+ */9595+void do_page_fault(struct pt_regs *regs, unsigned long address,9696+ unsigned long error_code)9797+{9898+ struct vm_area_struct *vma;9999+ struct mm_struct *mm = current->mm;100100+ siginfo_t info;101101+ int code = SEGV_MAPERR;102102+ int is_write = error_code & ESR_S;103103+ int fault;104104+105105+ regs->ear = address;106106+ regs->esr = error_code;107107+108108+ /* On a kernel SLB miss we can only check for a valid exception entry */109109+ if (kernel_mode(regs) && (address >= TASK_SIZE)) {110110+ printk(KERN_WARNING "kernel task_size exceed");111111+ _exception(SIGSEGV, regs, code, address);112112+ }113113+114114+ /* for instr TLB miss and instr storage exception ESR_S is undefined */115115+ if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)116116+ is_write = 0;117117+118118+#if defined(CONFIG_KGDB)119119+ if (debugger_fault_handler && regs->trap == 0x300) {120120+ debugger_fault_handler(regs);121121+ return;122122+ }123123+#endif /* CONFIG_KGDB */124124+125125+ if (in_atomic() || mm == NULL) {126126+ /* FIXME */127127+ if (kernel_mode(regs)) {128128+ printk(KERN_EMERG129129+ "Page fault in kernel mode - Oooou!!! pid %d\n",130130+ current->pid);131131+ _exception(SIGSEGV, regs, code, address);132132+ return;133133+ }134134+ /* in_atomic() in user mode is really bad,135135+ as is current->mm == NULL. */136136+ printk(KERN_EMERG "Page fault in user mode with "137137+ "in_atomic(), mm = %p\n", mm);138138+ printk(KERN_EMERG "r15 = %lx MSR = %lx\n",139139+ regs->r15, regs->msr);140140+ die("Weird page fault", regs, SIGSEGV);141141+ }142142+143143+ /* When running in the kernel we expect faults to occur only to144144+ * addresses in user space. All other faults represent errors in the145145+ * kernel and should generate an OOPS. Unfortunately, in the case of an146146+ * erroneous fault occurring in a code path which already holds mmap_sem147147+ * we will deadlock attempting to validate the fault against the148148+ * address space. Luckily the kernel only validly references user149149+ * space from well defined areas of code, which are listed in the150150+ * exceptions table.151151+ *152152+ * As the vast majority of faults will be valid we will only perform153153+ * the source reference check when there is a possibility of a deadlock.154154+ * Attempt to lock the address space, if we cannot we then validate the155155+ * source. If this is invalid we can skip the address space check,156156+ * thus avoiding the deadlock.157157+ */158158+ if (!down_read_trylock(&mm->mmap_sem)) {159159+ if (kernel_mode(regs) && !search_exception_tables(regs->pc))160160+ goto bad_area_nosemaphore;161161+162162+ down_read(&mm->mmap_sem);163163+ }164164+165165+ vma = find_vma(mm, address);166166+ if (!vma)167167+ goto bad_area;168168+169169+ if (vma->vm_start <= address)170170+ goto good_area;171171+172172+ if (!(vma->vm_flags & VM_GROWSDOWN))173173+ goto bad_area;174174+175175+ if (!is_write)176176+ goto bad_area;177177+178178+ /*179179+ * N.B. The ABI allows programs to access up to180180+ * a few hundred bytes below the stack pointer (TBD).181181+ * The kernel signal delivery code writes up to about 1.5kB182182+ * below the stack pointer (r1) before decrementing it.183183+ * The exec code can write slightly over 640kB to the stack184184+ * before setting the user r1. Thus we allow the stack to185185+ * expand to 1MB without further checks.186186+ */187187+ if (address + 0x100000 < vma->vm_end) {188188+189189+ /* get user regs even if this fault is in kernel mode */190190+ struct pt_regs *uregs = current->thread.regs;191191+ if (uregs == NULL)192192+ goto bad_area;193193+194194+ /*195195+ * A user-mode access to an address a long way below196196+ * the stack pointer is only valid if the instruction197197+ * is one which would update the stack pointer to the198198+ * address accessed if the instruction completed,199199+ * i.e. either stwu rs,n(r1) or stwux rs,r1,rb200200+ * (or the byte, halfword, float or double forms).201201+ *202202+ * If we don't check this then any write to the area203203+ * between the last mapped region and the stack will204204+ * expand the stack rather than segfaulting.205205+ */206206+ if (address + 2048 < uregs->r1207207+ && (kernel_mode(regs) || !store_updates_sp(regs)))208208+ goto bad_area;209209+ }210210+ if (expand_stack(vma, address))211211+ goto bad_area;212212+213213+good_area:214214+ code = SEGV_ACCERR;215215+216216+ /* a write */217217+ if (is_write) {218218+ if (!(vma->vm_flags & VM_WRITE))219219+ goto bad_area;220220+ /* a read */221221+ } else {222222+ /* protection fault */223223+ if (error_code & 0x08000000)224224+ goto bad_area;225225+ if (!(vma->vm_flags & (VM_READ | VM_EXEC)))226226+ goto bad_area;227227+ }228228+229229+ /*230230+ * If for any reason at all we couldn't handle the fault,231231+ * make sure we exit gracefully rather than endlessly redo232232+ * the fault.233233+ */234234+survive:235235+ fault = handle_mm_fault(mm, vma, address, is_write);236236+ if (unlikely(fault & VM_FAULT_ERROR)) {237237+ if (fault & VM_FAULT_OOM)238238+ goto out_of_memory;239239+ else if (fault & VM_FAULT_SIGBUS)240240+ goto do_sigbus;241241+ BUG();242242+ }243243+ if (fault & VM_FAULT_MAJOR)244244+ current->maj_flt++;245245+ else246246+ current->min_flt++;247247+ up_read(&mm->mmap_sem);248248+ /*249249+ * keep track of tlb+htab misses that are good addrs but250250+ * just need pte's created via handle_mm_fault()251251+ * -- Cort252252+ */253253+ pte_misses++;254254+ return;255255+256256+bad_area:257257+ up_read(&mm->mmap_sem);258258+259259+bad_area_nosemaphore:260260+ pte_errors++;261261+262262+ /* User mode accesses cause a SIGSEGV */263263+ if (user_mode(regs)) {264264+ _exception(SIGSEGV, regs, code, address);265265+/* info.si_signo = SIGSEGV;266266+ info.si_errno = 0;267267+ info.si_code = code;268268+ info.si_addr = (void *) address;269269+ force_sig_info(SIGSEGV, &info, current);*/270270+ return;271271+ }272272+273273+ bad_page_fault(regs, address, SIGSEGV);274274+ return;275275+276276+/*277277+ * We ran out of memory, or some other thing happened to us that made278278+ * us unable to handle the page fault gracefully.279279+ */280280+out_of_memory:281281+ if (current->pid == 1) {282282+ yield();283283+ down_read(&mm->mmap_sem);284284+ goto survive;285285+ }286286+ up_read(&mm->mmap_sem);287287+ printk(KERN_WARNING "VM: killing process %s\n", current->comm);288288+ if (user_mode(regs))289289+ do_exit(SIGKILL);290290+ bad_page_fault(regs, address, SIGKILL);291291+ return;292292+293293+do_sigbus:294294+ up_read(&mm->mmap_sem);295295+ if (user_mode(regs)) {296296+ info.si_signo = SIGBUS;297297+ info.si_errno = 0;298298+ info.si_code = BUS_ADRERR;299299+ info.si_addr = (void __user *)address;300300+ force_sig_info(SIGBUS, &info, current);301301+ return;302302+ }303303+ bad_page_fault(regs, address, SIGBUS);304304+}