15de96121SMichal Simek /*
25de96121SMichal Simek * arch/microblaze/mm/fault.c
35de96121SMichal Simek *
45de96121SMichal Simek * Copyright (C) 2007 Xilinx, Inc. All rights reserved.
55de96121SMichal Simek *
65de96121SMichal Simek * Derived from "arch/ppc/mm/fault.c"
75de96121SMichal Simek * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
85de96121SMichal Simek *
95de96121SMichal Simek * Derived from "arch/i386/mm/fault.c"
105de96121SMichal Simek * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
115de96121SMichal Simek *
125de96121SMichal Simek * Modified by Cort Dougan and Paul Mackerras.
135de96121SMichal Simek *
145de96121SMichal Simek * This file is subject to the terms and conditions of the GNU General
155de96121SMichal Simek * Public License. See the file COPYING in the main directory of this
165de96121SMichal Simek * archive for more details.
175de96121SMichal Simek *
185de96121SMichal Simek */
195de96121SMichal Simek
20a75f3108SPaul Gortmaker #include <linux/extable.h>
215de96121SMichal Simek #include <linux/signal.h>
225de96121SMichal Simek #include <linux/sched.h>
235de96121SMichal Simek #include <linux/kernel.h>
245de96121SMichal Simek #include <linux/errno.h>
255de96121SMichal Simek #include <linux/string.h>
265de96121SMichal Simek #include <linux/types.h>
275de96121SMichal Simek #include <linux/ptrace.h>
285de96121SMichal Simek #include <linux/mman.h>
295de96121SMichal Simek #include <linux/mm.h>
305de96121SMichal Simek #include <linux/interrupt.h>
31aeb6aefcSPeter Xu #include <linux/perf_event.h>
325de96121SMichal Simek
335de96121SMichal Simek #include <asm/page.h>
345de96121SMichal Simek #include <asm/mmu.h>
356bd55f0bSMichal Simek #include <linux/mmu_context.h>
365de96121SMichal Simek #include <linux/uaccess.h>
375de96121SMichal Simek #include <asm/exceptions.h>
385de96121SMichal Simek
395de96121SMichal Simek static unsigned long pte_misses; /* updated by do_page_fault() */
405de96121SMichal Simek static unsigned long pte_errors; /* updated by do_page_fault() */
415de96121SMichal Simek
425de96121SMichal Simek /*
435de96121SMichal Simek * Check whether the instruction at regs->pc is a store using
445de96121SMichal Simek * an update addressing form which will update r1.
455de96121SMichal Simek */
store_updates_sp(struct pt_regs * regs)465de96121SMichal Simek static int store_updates_sp(struct pt_regs *regs)
475de96121SMichal Simek {
485de96121SMichal Simek unsigned int inst;
495de96121SMichal Simek
505213a9c3SMichal Simek if (get_user(inst, (unsigned int __user *)regs->pc))
515de96121SMichal Simek return 0;
525de96121SMichal Simek /* check for 1 in the rD field */
535de96121SMichal Simek if (((inst >> 21) & 0x1f) != 1)
545de96121SMichal Simek return 0;
555de96121SMichal Simek /* check for store opcodes */
565de96121SMichal Simek if ((inst & 0xd0000000) == 0xd0000000)
575de96121SMichal Simek return 1;
585de96121SMichal Simek return 0;
595de96121SMichal Simek }
605de96121SMichal Simek
615de96121SMichal Simek
625de96121SMichal Simek /*
635de96121SMichal Simek * bad_page_fault is called when we have a bad access from the kernel.
645de96121SMichal Simek * It is called from do_page_fault above and from some of the procedures
655de96121SMichal Simek * in traps.c.
665de96121SMichal Simek */
bad_page_fault(struct pt_regs * regs,unsigned long address,int sig)673863dbceSMichal Simek void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
685de96121SMichal Simek {
695de96121SMichal Simek const struct exception_table_entry *fixup;
705de96121SMichal Simek /* MS: no context */
715de96121SMichal Simek /* Are we prepared to handle this fault? */
725de96121SMichal Simek fixup = search_exception_tables(regs->pc);
735de96121SMichal Simek if (fixup) {
745de96121SMichal Simek regs->pc = fixup->fixup;
755de96121SMichal Simek return;
765de96121SMichal Simek }
775de96121SMichal Simek
785de96121SMichal Simek /* kernel has accessed a bad area */
795de96121SMichal Simek die("kernel access of bad area", regs, sig);
805de96121SMichal Simek }
815de96121SMichal Simek
825de96121SMichal Simek /*
835de96121SMichal Simek * The error_code parameter is ESR for a data fault,
845de96121SMichal Simek * 0 for an instruction fault.
855de96121SMichal Simek */
do_page_fault(struct pt_regs * regs,unsigned long address,unsigned long error_code)865de96121SMichal Simek void do_page_fault(struct pt_regs *regs, unsigned long address,
875de96121SMichal Simek unsigned long error_code)
885de96121SMichal Simek {
895de96121SMichal Simek struct vm_area_struct *vma;
905de96121SMichal Simek struct mm_struct *mm = current->mm;
915de96121SMichal Simek int code = SEGV_MAPERR;
925de96121SMichal Simek int is_write = error_code & ESR_S;
9350a7ca3cSSouptick Joarder vm_fault_t fault;
94dde16072SPeter Xu unsigned int flags = FAULT_FLAG_DEFAULT;
955de96121SMichal Simek
965de96121SMichal Simek regs->ear = address;
975de96121SMichal Simek regs->esr = error_code;
985de96121SMichal Simek
995de96121SMichal Simek /* On a kernel SLB miss we can only check for a valid exception entry */
10078ebfa88SMichal Simek if (unlikely(kernel_mode(regs) && (address >= TASK_SIZE))) {
1016bd55f0bSMichal Simek pr_warn("kernel task_size exceed");
1025de96121SMichal Simek _exception(SIGSEGV, regs, code, address);
1035de96121SMichal Simek }
1045de96121SMichal Simek
1055de96121SMichal Simek /* for instr TLB miss and instr storage exception ESR_S is undefined */
1065de96121SMichal Simek if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
1075de96121SMichal Simek is_write = 0;
1085de96121SMichal Simek
10970ffdb93SDavid Hildenbrand if (unlikely(faulthandler_disabled() || !mm)) {
110f10eca6eSMichal Simek if (kernel_mode(regs))
111f10eca6eSMichal Simek goto bad_area_nosemaphore;
112f10eca6eSMichal Simek
11370ffdb93SDavid Hildenbrand /* faulthandler_disabled() in user mode is really bad,
1145de96121SMichal Simek as is current->mm == NULL. */
11570ffdb93SDavid Hildenbrand pr_emerg("Page fault in user mode with faulthandler_disabled(), mm = %p\n",
1166bd55f0bSMichal Simek mm);
1176bd55f0bSMichal Simek pr_emerg("r15 = %lx MSR = %lx\n",
1185de96121SMichal Simek regs->r15, regs->msr);
1195de96121SMichal Simek die("Weird page fault", regs, SIGSEGV);
1205de96121SMichal Simek }
1215de96121SMichal Simek
122759496baSJohannes Weiner if (user_mode(regs))
123759496baSJohannes Weiner flags |= FAULT_FLAG_USER;
124759496baSJohannes Weiner
125aeb6aefcSPeter Xu perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
126aeb6aefcSPeter Xu
1275de96121SMichal Simek /* When running in the kernel we expect faults to occur only to
1285de96121SMichal Simek * addresses in user space. All other faults represent errors in the
1295de96121SMichal Simek * kernel and should generate an OOPS. Unfortunately, in the case of an
130c1e8d7c6SMichel Lespinasse * erroneous fault occurring in a code path which already holds mmap_lock
1315de96121SMichal Simek * we will deadlock attempting to validate the fault against the
1325de96121SMichal Simek * address space. Luckily the kernel only validly references user
1335de96121SMichal Simek * space from well defined areas of code, which are listed in the
1345de96121SMichal Simek * exceptions table.
1355de96121SMichal Simek *
1365de96121SMichal Simek * As the vast majority of faults will be valid we will only perform
1375de96121SMichal Simek * the source reference check when there is a possibility of a deadlock.
1385de96121SMichal Simek * Attempt to lock the address space, if we cannot we then validate the
1395de96121SMichal Simek * source. If this is invalid we can skip the address space check,
1405de96121SMichal Simek * thus avoiding the deadlock.
1415de96121SMichal Simek */
142d8ed45c5SMichel Lespinasse if (unlikely(!mmap_read_trylock(mm))) {
1435de96121SMichal Simek if (kernel_mode(regs) && !search_exception_tables(regs->pc))
1445de96121SMichal Simek goto bad_area_nosemaphore;
1455de96121SMichal Simek
146f397c077SKautuk Consul retry:
147d8ed45c5SMichel Lespinasse mmap_read_lock(mm);
1485de96121SMichal Simek }
1495de96121SMichal Simek
1505de96121SMichal Simek vma = find_vma(mm, address);
15178ebfa88SMichal Simek if (unlikely(!vma))
1525de96121SMichal Simek goto bad_area;
1535de96121SMichal Simek
1545de96121SMichal Simek if (vma->vm_start <= address)
1555de96121SMichal Simek goto good_area;
1565de96121SMichal Simek
15778ebfa88SMichal Simek if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
1585de96121SMichal Simek goto bad_area;
1595de96121SMichal Simek
16078ebfa88SMichal Simek if (unlikely(!is_write))
1615de96121SMichal Simek goto bad_area;
1625de96121SMichal Simek
1635de96121SMichal Simek /*
1645de96121SMichal Simek * N.B. The ABI allows programs to access up to
1655de96121SMichal Simek * a few hundred bytes below the stack pointer (TBD).
1665de96121SMichal Simek * The kernel signal delivery code writes up to about 1.5kB
1675de96121SMichal Simek * below the stack pointer (r1) before decrementing it.
1685de96121SMichal Simek * The exec code can write slightly over 640kB to the stack
1695de96121SMichal Simek * before setting the user r1. Thus we allow the stack to
1705de96121SMichal Simek * expand to 1MB without further checks.
1715de96121SMichal Simek */
17278ebfa88SMichal Simek if (unlikely(address + 0x100000 < vma->vm_end)) {
1735de96121SMichal Simek
1745de96121SMichal Simek /* get user regs even if this fault is in kernel mode */
1755de96121SMichal Simek struct pt_regs *uregs = current->thread.regs;
1765de96121SMichal Simek if (uregs == NULL)
1775de96121SMichal Simek goto bad_area;
1785de96121SMichal Simek
1795de96121SMichal Simek /*
1805de96121SMichal Simek * A user-mode access to an address a long way below
1815de96121SMichal Simek * the stack pointer is only valid if the instruction
1825de96121SMichal Simek * is one which would update the stack pointer to the
1835de96121SMichal Simek * address accessed if the instruction completed,
1845de96121SMichal Simek * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
1855de96121SMichal Simek * (or the byte, halfword, float or double forms).
1865de96121SMichal Simek *
1875de96121SMichal Simek * If we don't check this then any write to the area
1885de96121SMichal Simek * between the last mapped region and the stack will
1895de96121SMichal Simek * expand the stack rather than segfaulting.
1905de96121SMichal Simek */
1915de96121SMichal Simek if (address + 2048 < uregs->r1
1925de96121SMichal Simek && (kernel_mode(regs) || !store_updates_sp(regs)))
1935de96121SMichal Simek goto bad_area;
1945de96121SMichal Simek }
195*8d7071afSLinus Torvalds vma = expand_stack(mm, address);
196*8d7071afSLinus Torvalds if (!vma)
197*8d7071afSLinus Torvalds goto bad_area_nosemaphore;
1985de96121SMichal Simek
1995de96121SMichal Simek good_area:
2005de96121SMichal Simek code = SEGV_ACCERR;
2015de96121SMichal Simek
2025de96121SMichal Simek /* a write */
20378ebfa88SMichal Simek if (unlikely(is_write)) {
20478ebfa88SMichal Simek if (unlikely(!(vma->vm_flags & VM_WRITE)))
2055de96121SMichal Simek goto bad_area;
206759496baSJohannes Weiner flags |= FAULT_FLAG_WRITE;
2075de96121SMichal Simek /* a read */
2085de96121SMichal Simek } else {
2095de96121SMichal Simek /* protection fault */
21078ebfa88SMichal Simek if (unlikely(error_code & 0x08000000))
2115de96121SMichal Simek goto bad_area;
21278ebfa88SMichal Simek if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC))))
2135de96121SMichal Simek goto bad_area;
2145de96121SMichal Simek }
2155de96121SMichal Simek
2165de96121SMichal Simek /*
2175de96121SMichal Simek * If for any reason at all we couldn't handle the fault,
2185de96121SMichal Simek * make sure we exit gracefully rather than endlessly redo
2195de96121SMichal Simek * the fault.
2205de96121SMichal Simek */
221aeb6aefcSPeter Xu fault = handle_mm_fault(vma, address, flags, regs);
222f397c077SKautuk Consul
223a1179ac7SAl Viro if (fault_signal_pending(fault, regs)) {
224a1179ac7SAl Viro if (!user_mode(regs))
225a1179ac7SAl Viro bad_page_fault(regs, address, SIGBUS);
226f397c077SKautuk Consul return;
227a1179ac7SAl Viro }
228f397c077SKautuk Consul
229d9272525SPeter Xu /* The fault is fully completed (including releasing mmap lock) */
230d9272525SPeter Xu if (fault & VM_FAULT_COMPLETED)
231d9272525SPeter Xu return;
232d9272525SPeter Xu
2335de96121SMichal Simek if (unlikely(fault & VM_FAULT_ERROR)) {
2345de96121SMichal Simek if (fault & VM_FAULT_OOM)
2355de96121SMichal Simek goto out_of_memory;
23633692f27SLinus Torvalds else if (fault & VM_FAULT_SIGSEGV)
23733692f27SLinus Torvalds goto bad_area;
2385de96121SMichal Simek else if (fault & VM_FAULT_SIGBUS)
2395de96121SMichal Simek goto do_sigbus;
2405de96121SMichal Simek BUG();
2415de96121SMichal Simek }
242f397c077SKautuk Consul
243f397c077SKautuk Consul if (fault & VM_FAULT_RETRY) {
24445cac65bSShaohua Li flags |= FAULT_FLAG_TRIED;
245f397c077SKautuk Consul
246f397c077SKautuk Consul /*
2473e4e28c5SMichel Lespinasse * No need to mmap_read_unlock(mm) as we would
248f397c077SKautuk Consul * have already released it in __lock_page_or_retry
249f397c077SKautuk Consul * in mm/filemap.c.
250f397c077SKautuk Consul */
251f397c077SKautuk Consul
252f397c077SKautuk Consul goto retry;
253f397c077SKautuk Consul }
254f397c077SKautuk Consul
255d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
256f397c077SKautuk Consul
2575de96121SMichal Simek /*
2585de96121SMichal Simek * keep track of tlb+htab misses that are good addrs but
2595de96121SMichal Simek * just need pte's created via handle_mm_fault()
2605de96121SMichal Simek * -- Cort
2615de96121SMichal Simek */
2625de96121SMichal Simek pte_misses++;
2635de96121SMichal Simek return;
2645de96121SMichal Simek
2655de96121SMichal Simek bad_area:
266d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
2675de96121SMichal Simek
2685de96121SMichal Simek bad_area_nosemaphore:
2695de96121SMichal Simek pte_errors++;
2705de96121SMichal Simek
2715de96121SMichal Simek /* User mode accesses cause a SIGSEGV */
2725de96121SMichal Simek if (user_mode(regs)) {
2735de96121SMichal Simek _exception(SIGSEGV, regs, code, address);
2745de96121SMichal Simek return;
2755de96121SMichal Simek }
2765de96121SMichal Simek
2775de96121SMichal Simek bad_page_fault(regs, address, SIGSEGV);
2785de96121SMichal Simek return;
2795de96121SMichal Simek
2805de96121SMichal Simek /*
2815de96121SMichal Simek * We ran out of memory, or some other thing happened to us that made
2825de96121SMichal Simek * us unable to handle the page fault gracefully.
2835de96121SMichal Simek */
2845de96121SMichal Simek out_of_memory:
285d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
28621e1c936SNick Piggin if (!user_mode(regs))
2875de96121SMichal Simek bad_page_fault(regs, address, SIGKILL);
28821e1c936SNick Piggin else
28921e1c936SNick Piggin pagefault_out_of_memory();
2905de96121SMichal Simek return;
2915de96121SMichal Simek
2925de96121SMichal Simek do_sigbus:
293d8ed45c5SMichel Lespinasse mmap_read_unlock(mm);
2945de96121SMichal Simek if (user_mode(regs)) {
2952e1661d2SEric W. Biederman force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
2965de96121SMichal Simek return;
2975de96121SMichal Simek }
2985de96121SMichal Simek bad_page_fault(regs, address, SIGBUS);
2995de96121SMichal Simek }
300