Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * arch/microblaze/mm/fault.c |
| 3 | * |
| 4 | * Copyright (C) 2007 Xilinx, Inc. All rights reserved. |
| 5 | * |
| 6 | * Derived from "arch/ppc/mm/fault.c" |
| 7 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) |
| 8 | * |
| 9 | * Derived from "arch/i386/mm/fault.c" |
| 10 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| 11 | * |
| 12 | * Modified by Cort Dougan and Paul Mackerras. |
| 13 | * |
| 14 | * This file is subject to the terms and conditions of the GNU General |
| 15 | * Public License. See the file COPYING in the main directory of this |
| 16 | * archive for more details. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/signal.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/ptrace.h> |
| 28 | #include <linux/mman.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | |
| 32 | #include <asm/page.h> |
| 33 | #include <asm/pgtable.h> |
| 34 | #include <asm/mmu.h> |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 35 | #include <linux/mmu_context.h> |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 36 | #include <linux/uaccess.h> |
| 37 | #include <asm/exceptions.h> |
| 38 | |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 39 | static unsigned long pte_misses; /* updated by do_page_fault() */ |
| 40 | static unsigned long pte_errors; /* updated by do_page_fault() */ |
| 41 | |
| 42 | /* |
| 43 | * Check whether the instruction at regs->pc is a store using |
| 44 | * an update addressing form which will update r1. |
| 45 | */ |
| 46 | static int store_updates_sp(struct pt_regs *regs) |
| 47 | { |
| 48 | unsigned int inst; |
| 49 | |
Michal Simek | 5213a9c | 2011-02-07 12:24:39 +0100 | [diff] [blame] | 50 | if (get_user(inst, (unsigned int __user *)regs->pc)) |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 51 | return 0; |
| 52 | /* check for 1 in the rD field */ |
| 53 | if (((inst >> 21) & 0x1f) != 1) |
| 54 | return 0; |
| 55 | /* check for store opcodes */ |
| 56 | if ((inst & 0xd0000000) == 0xd0000000) |
| 57 | return 1; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /* |
| 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 procedures |
| 65 | * in traps.c. |
| 66 | */ |
Michal Simek | 3863dbc | 2009-07-21 12:48:01 +0200 | [diff] [blame] | 67 | void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig) |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 68 | { |
| 69 | const struct exception_table_entry *fixup; |
| 70 | /* MS: no context */ |
| 71 | /* Are we prepared to handle this fault? */ |
| 72 | fixup = search_exception_tables(regs->pc); |
| 73 | if (fixup) { |
| 74 | regs->pc = fixup->fixup; |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | /* kernel has accessed a bad area */ |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 79 | die("kernel access of bad area", regs, sig); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * The error_code parameter is ESR for a data fault, |
| 84 | * 0 for an instruction fault. |
| 85 | */ |
| 86 | void do_page_fault(struct pt_regs *regs, unsigned long address, |
| 87 | unsigned long error_code) |
| 88 | { |
| 89 | struct vm_area_struct *vma; |
| 90 | struct mm_struct *mm = current->mm; |
| 91 | siginfo_t info; |
| 92 | int code = SEGV_MAPERR; |
| 93 | int is_write = error_code & ESR_S; |
| 94 | int fault; |
Johannes Weiner | 759496b | 2013-09-12 15:13:39 -0700 | [diff] [blame] | 95 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 96 | |
| 97 | regs->ear = address; |
| 98 | regs->esr = error_code; |
| 99 | |
| 100 | /* On a kernel SLB miss we can only check for a valid exception entry */ |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 101 | if (unlikely(kernel_mode(regs) && (address >= TASK_SIZE))) { |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 102 | pr_warn("kernel task_size exceed"); |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 103 | _exception(SIGSEGV, regs, code, address); |
| 104 | } |
| 105 | |
| 106 | /* for instr TLB miss and instr storage exception ESR_S is undefined */ |
| 107 | if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11) |
| 108 | is_write = 0; |
| 109 | |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 110 | if (unlikely(in_atomic() || !mm)) { |
Michal Simek | f10eca6 | 2009-07-16 16:00:49 +0200 | [diff] [blame] | 111 | if (kernel_mode(regs)) |
| 112 | goto bad_area_nosemaphore; |
| 113 | |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 114 | /* in_atomic() in user mode is really bad, |
| 115 | as is current->mm == NULL. */ |
Michal Simek | 6bd55f0 | 2012-12-27 10:40:38 +0100 | [diff] [blame] | 116 | pr_emerg("Page fault in user mode with in_atomic(), mm = %p\n", |
| 117 | mm); |
| 118 | pr_emerg("r15 = %lx MSR = %lx\n", |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 119 | regs->r15, regs->msr); |
| 120 | die("Weird page fault", regs, SIGSEGV); |
| 121 | } |
| 122 | |
Johannes Weiner | 759496b | 2013-09-12 15:13:39 -0700 | [diff] [blame] | 123 | if (user_mode(regs)) |
| 124 | flags |= FAULT_FLAG_USER; |
| 125 | |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 126 | /* When running in the kernel we expect faults to occur only to |
| 127 | * addresses in user space. All other faults represent errors in the |
| 128 | * kernel and should generate an OOPS. Unfortunately, in the case of an |
| 129 | * erroneous fault occurring in a code path which already holds mmap_sem |
| 130 | * we will deadlock attempting to validate the fault against the |
| 131 | * address space. Luckily the kernel only validly references user |
| 132 | * space from well defined areas of code, which are listed in the |
| 133 | * exceptions table. |
| 134 | * |
| 135 | * As the vast majority of faults will be valid we will only perform |
| 136 | * the source reference check when there is a possibility of a deadlock. |
| 137 | * Attempt to lock the address space, if we cannot we then validate the |
| 138 | * source. If this is invalid we can skip the address space check, |
| 139 | * thus avoiding the deadlock. |
| 140 | */ |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 141 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 142 | if (kernel_mode(regs) && !search_exception_tables(regs->pc)) |
| 143 | goto bad_area_nosemaphore; |
| 144 | |
Kautuk Consul | f397c07 | 2012-03-20 09:21:40 -0400 | [diff] [blame] | 145 | retry: |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 146 | down_read(&mm->mmap_sem); |
| 147 | } |
| 148 | |
| 149 | vma = find_vma(mm, address); |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 150 | if (unlikely(!vma)) |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 151 | goto bad_area; |
| 152 | |
| 153 | if (vma->vm_start <= address) |
| 154 | goto good_area; |
| 155 | |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 156 | if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 157 | goto bad_area; |
| 158 | |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 159 | if (unlikely(!is_write)) |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 160 | goto bad_area; |
| 161 | |
| 162 | /* |
| 163 | * N.B. The ABI allows programs to access up to |
| 164 | * a few hundred bytes below the stack pointer (TBD). |
| 165 | * The kernel signal delivery code writes up to about 1.5kB |
| 166 | * below the stack pointer (r1) before decrementing it. |
| 167 | * The exec code can write slightly over 640kB to the stack |
| 168 | * before setting the user r1. Thus we allow the stack to |
| 169 | * expand to 1MB without further checks. |
| 170 | */ |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 171 | if (unlikely(address + 0x100000 < vma->vm_end)) { |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 172 | |
| 173 | /* get user regs even if this fault is in kernel mode */ |
| 174 | struct pt_regs *uregs = current->thread.regs; |
| 175 | if (uregs == NULL) |
| 176 | goto bad_area; |
| 177 | |
| 178 | /* |
| 179 | * A user-mode access to an address a long way below |
| 180 | * the stack pointer is only valid if the instruction |
| 181 | * is one which would update the stack pointer to the |
| 182 | * address accessed if the instruction completed, |
| 183 | * i.e. either stwu rs,n(r1) or stwux rs,r1,rb |
| 184 | * (or the byte, halfword, float or double forms). |
| 185 | * |
| 186 | * If we don't check this then any write to the area |
| 187 | * between the last mapped region and the stack will |
| 188 | * expand the stack rather than segfaulting. |
| 189 | */ |
| 190 | if (address + 2048 < uregs->r1 |
| 191 | && (kernel_mode(regs) || !store_updates_sp(regs))) |
| 192 | goto bad_area; |
| 193 | } |
| 194 | if (expand_stack(vma, address)) |
| 195 | goto bad_area; |
| 196 | |
| 197 | good_area: |
| 198 | code = SEGV_ACCERR; |
| 199 | |
| 200 | /* a write */ |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 201 | if (unlikely(is_write)) { |
| 202 | if (unlikely(!(vma->vm_flags & VM_WRITE))) |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 203 | goto bad_area; |
Johannes Weiner | 759496b | 2013-09-12 15:13:39 -0700 | [diff] [blame] | 204 | flags |= FAULT_FLAG_WRITE; |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 205 | /* a read */ |
| 206 | } else { |
| 207 | /* protection fault */ |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 208 | if (unlikely(error_code & 0x08000000)) |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 209 | goto bad_area; |
Michal Simek | 78ebfa8 | 2010-03-23 15:37:02 +0100 | [diff] [blame] | 210 | if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC)))) |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 211 | goto bad_area; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * If for any reason at all we couldn't handle the fault, |
| 216 | * make sure we exit gracefully rather than endlessly redo |
| 217 | * the fault. |
| 218 | */ |
Kautuk Consul | f397c07 | 2012-03-20 09:21:40 -0400 | [diff] [blame] | 219 | fault = handle_mm_fault(mm, vma, address, flags); |
| 220 | |
| 221 | if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) |
| 222 | return; |
| 223 | |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 224 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 225 | if (fault & VM_FAULT_OOM) |
| 226 | goto out_of_memory; |
| 227 | else if (fault & VM_FAULT_SIGBUS) |
| 228 | goto do_sigbus; |
| 229 | BUG(); |
| 230 | } |
Kautuk Consul | f397c07 | 2012-03-20 09:21:40 -0400 | [diff] [blame] | 231 | |
| 232 | if (flags & FAULT_FLAG_ALLOW_RETRY) { |
| 233 | if (unlikely(fault & VM_FAULT_MAJOR)) |
| 234 | current->maj_flt++; |
| 235 | else |
| 236 | current->min_flt++; |
| 237 | if (fault & VM_FAULT_RETRY) { |
| 238 | flags &= ~FAULT_FLAG_ALLOW_RETRY; |
Shaohua Li | 45cac65 | 2012-10-08 16:32:19 -0700 | [diff] [blame] | 239 | flags |= FAULT_FLAG_TRIED; |
Kautuk Consul | f397c07 | 2012-03-20 09:21:40 -0400 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * No need to up_read(&mm->mmap_sem) as we would |
| 243 | * have already released it in __lock_page_or_retry |
| 244 | * in mm/filemap.c. |
| 245 | */ |
| 246 | |
| 247 | goto retry; |
| 248 | } |
| 249 | } |
| 250 | |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 251 | up_read(&mm->mmap_sem); |
Kautuk Consul | f397c07 | 2012-03-20 09:21:40 -0400 | [diff] [blame] | 252 | |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 253 | /* |
| 254 | * keep track of tlb+htab misses that are good addrs but |
| 255 | * just need pte's created via handle_mm_fault() |
| 256 | * -- Cort |
| 257 | */ |
| 258 | pte_misses++; |
| 259 | return; |
| 260 | |
| 261 | bad_area: |
| 262 | up_read(&mm->mmap_sem); |
| 263 | |
| 264 | bad_area_nosemaphore: |
| 265 | pte_errors++; |
| 266 | |
| 267 | /* User mode accesses cause a SIGSEGV */ |
| 268 | if (user_mode(regs)) { |
| 269 | _exception(SIGSEGV, regs, code, address); |
| 270 | /* info.si_signo = SIGSEGV; |
| 271 | info.si_errno = 0; |
| 272 | info.si_code = code; |
| 273 | info.si_addr = (void *) address; |
| 274 | force_sig_info(SIGSEGV, &info, current);*/ |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | bad_page_fault(regs, address, SIGSEGV); |
| 279 | return; |
| 280 | |
| 281 | /* |
| 282 | * We ran out of memory, or some other thing happened to us that made |
| 283 | * us unable to handle the page fault gracefully. |
| 284 | */ |
| 285 | out_of_memory: |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 286 | up_read(&mm->mmap_sem); |
Nick Piggin | 21e1c93 | 2010-04-23 02:06:21 +1000 | [diff] [blame] | 287 | if (!user_mode(regs)) |
| 288 | bad_page_fault(regs, address, SIGKILL); |
| 289 | else |
| 290 | pagefault_out_of_memory(); |
Michal Simek | 5de9612 | 2009-05-26 16:30:13 +0200 | [diff] [blame] | 291 | return; |
| 292 | |
| 293 | do_sigbus: |
| 294 | up_read(&mm->mmap_sem); |
| 295 | if (user_mode(regs)) { |
| 296 | info.si_signo = SIGBUS; |
| 297 | info.si_errno = 0; |
| 298 | info.si_code = BUS_ADRERR; |
| 299 | info.si_addr = (void __user *)address; |
| 300 | force_sig_info(SIGBUS, &info, current); |
| 301 | return; |
| 302 | } |
| 303 | bad_page_fault(regs, address, SIGBUS); |
| 304 | } |