Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2006 Atmel Corporation |
| 3 | * |
| 4 | * Based on linux/arch/sh/mm/fault.c: |
| 5 | * Copyright (C) 1999 Niibe Yutaka |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/pagemap.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 15 | #include <linux/kdebug.h> |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 16 | #include <linux/kprobes.h> |
| 17 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 18 | #include <asm/mmu_context.h> |
| 19 | #include <asm/sysreg.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 20 | #include <asm/tlb.h> |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 21 | #include <asm/uaccess.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 22 | |
| 23 | #ifdef CONFIG_KPROBES |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 24 | static inline int notify_page_fault(struct pt_regs *regs, int trap) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 25 | { |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 26 | int ret = 0; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 27 | |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 28 | if (!user_mode(regs)) { |
| 29 | if (kprobe_running() && kprobe_fault_handler(regs, trap)) |
| 30 | ret = 1; |
| 31 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 32 | |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 33 | return ret; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 34 | } |
| 35 | #else |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 36 | static inline int notify_page_fault(struct pt_regs *regs, int trap) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 37 | { |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 38 | return 0; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 39 | } |
| 40 | #endif |
| 41 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 42 | int exception_trace = 1; |
| 43 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 44 | /* |
| 45 | * This routine handles page faults. It determines the address and the |
| 46 | * problem, and then passes it off to one of the appropriate routines. |
| 47 | * |
| 48 | * ecr is the Exception Cause Register. Possible values are: |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 49 | * 6: Protection fault (instruction access) |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 50 | * 15: Protection fault (read access) |
| 51 | * 16: Protection fault (write access) |
| 52 | * 20: Page not found (instruction access) |
| 53 | * 24: Page not found (read access) |
| 54 | * 28: Page not found (write access) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 55 | */ |
| 56 | asmlinkage void do_page_fault(unsigned long ecr, struct pt_regs *regs) |
| 57 | { |
| 58 | struct task_struct *tsk; |
| 59 | struct mm_struct *mm; |
| 60 | struct vm_area_struct *vma; |
| 61 | const struct exception_table_entry *fixup; |
| 62 | unsigned long address; |
| 63 | unsigned long page; |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 64 | long signr; |
| 65 | int code; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 66 | int fault; |
Kautuk Consul | bf7c27e | 2012-07-30 14:39:25 -0700 | [diff] [blame] | 67 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 68 | |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 69 | if (notify_page_fault(regs, ecr)) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 70 | return; |
| 71 | |
| 72 | address = sysreg_read(TLBEAR); |
| 73 | |
| 74 | tsk = current; |
| 75 | mm = tsk->mm; |
| 76 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 77 | signr = SIGSEGV; |
| 78 | code = SEGV_MAPERR; |
| 79 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 80 | /* |
| 81 | * If we're in an interrupt or have no user context, we must |
| 82 | * not take the fault... |
| 83 | */ |
| 84 | if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM)) |
| 85 | goto no_context; |
| 86 | |
| 87 | local_irq_enable(); |
| 88 | |
Johannes Weiner | 759496b | 2013-09-12 15:13:39 -0700 | [diff] [blame] | 89 | if (user_mode(regs)) |
| 90 | flags |= FAULT_FLAG_USER; |
Kautuk Consul | bf7c27e | 2012-07-30 14:39:25 -0700 | [diff] [blame] | 91 | retry: |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 92 | down_read(&mm->mmap_sem); |
| 93 | |
| 94 | vma = find_vma(mm, address); |
| 95 | if (!vma) |
| 96 | goto bad_area; |
| 97 | if (vma->vm_start <= address) |
| 98 | goto good_area; |
| 99 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 100 | goto bad_area; |
| 101 | if (expand_stack(vma, address)) |
| 102 | goto bad_area; |
| 103 | |
| 104 | /* |
| 105 | * Ok, we have a good vm_area for this memory access, so we |
| 106 | * can handle it... |
| 107 | */ |
| 108 | good_area: |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 109 | code = SEGV_ACCERR; |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 110 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 111 | switch (ecr) { |
| 112 | case ECR_PROTECTION_X: |
| 113 | case ECR_TLB_MISS_X: |
| 114 | if (!(vma->vm_flags & VM_EXEC)) |
| 115 | goto bad_area; |
| 116 | break; |
| 117 | case ECR_PROTECTION_R: |
| 118 | case ECR_TLB_MISS_R: |
| 119 | if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))) |
| 120 | goto bad_area; |
| 121 | break; |
| 122 | case ECR_PROTECTION_W: |
| 123 | case ECR_TLB_MISS_W: |
| 124 | if (!(vma->vm_flags & VM_WRITE)) |
| 125 | goto bad_area; |
Kautuk Consul | bf7c27e | 2012-07-30 14:39:25 -0700 | [diff] [blame] | 126 | flags |= FAULT_FLAG_WRITE; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 127 | break; |
| 128 | default: |
| 129 | panic("Unhandled case %lu in do_page_fault!", ecr); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * If for any reason at all we couldn't handle the fault, make |
| 134 | * sure we exit gracefully rather than endlessly redo the |
| 135 | * fault. |
| 136 | */ |
Kautuk Consul | bf7c27e | 2012-07-30 14:39:25 -0700 | [diff] [blame] | 137 | fault = handle_mm_fault(mm, vma, address, flags); |
| 138 | |
| 139 | if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) |
| 140 | return; |
| 141 | |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 142 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 143 | if (fault & VM_FAULT_OOM) |
| 144 | goto out_of_memory; |
Linus Torvalds | 33692f2 | 2015-01-29 10:51:32 -0800 | [diff] [blame] | 145 | else if (fault & VM_FAULT_SIGSEGV) |
| 146 | goto bad_area; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 147 | else if (fault & VM_FAULT_SIGBUS) |
| 148 | goto do_sigbus; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 149 | BUG(); |
| 150 | } |
Kautuk Consul | bf7c27e | 2012-07-30 14:39:25 -0700 | [diff] [blame] | 151 | |
| 152 | if (flags & FAULT_FLAG_ALLOW_RETRY) { |
| 153 | if (fault & VM_FAULT_MAJOR) |
| 154 | tsk->maj_flt++; |
| 155 | else |
| 156 | tsk->min_flt++; |
| 157 | if (fault & VM_FAULT_RETRY) { |
| 158 | flags &= ~FAULT_FLAG_ALLOW_RETRY; |
Shaohua Li | 45cac65 | 2012-10-08 16:32:19 -0700 | [diff] [blame] | 159 | flags |= FAULT_FLAG_TRIED; |
Kautuk Consul | bf7c27e | 2012-07-30 14:39:25 -0700 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * No need to up_read(&mm->mmap_sem) as we would have |
| 163 | * already released it in __lock_page_or_retry() in |
| 164 | * mm/filemap.c. |
| 165 | */ |
| 166 | goto retry; |
| 167 | } |
| 168 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 169 | |
| 170 | up_read(&mm->mmap_sem); |
| 171 | return; |
| 172 | |
| 173 | /* |
| 174 | * Something tried to access memory that isn't in our memory |
| 175 | * map. Fix it, but check if it's kernel or user first... |
| 176 | */ |
| 177 | bad_area: |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 178 | up_read(&mm->mmap_sem); |
| 179 | |
| 180 | if (user_mode(regs)) { |
Andrea Righi | 126187f | 2007-05-23 14:14:52 -0700 | [diff] [blame] | 181 | if (exception_trace && printk_ratelimit()) |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 182 | printk("%s%s[%d]: segfault at %08lx pc %08lx " |
| 183 | "sp %08lx ecr %lu\n", |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 184 | is_global_init(tsk) ? KERN_EMERG : KERN_INFO, |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 185 | tsk->comm, tsk->pid, address, regs->pc, |
| 186 | regs->sp, ecr); |
| 187 | _exception(SIGSEGV, regs, code, address); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | |
| 191 | no_context: |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 192 | /* Are we prepared to handle this kernel fault? */ |
| 193 | fixup = search_exception_tables(regs->pc); |
| 194 | if (fixup) { |
| 195 | regs->pc = fixup->fixup; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Oops. The kernel tried to access some bad page. We'll have |
| 201 | * to terminate things with extreme prejudice. |
| 202 | */ |
| 203 | if (address < PAGE_SIZE) |
| 204 | printk(KERN_ALERT |
| 205 | "Unable to handle kernel NULL pointer dereference"); |
| 206 | else |
| 207 | printk(KERN_ALERT |
| 208 | "Unable to handle kernel paging request"); |
| 209 | printk(" at virtual address %08lx\n", address); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 210 | |
| 211 | page = sysreg_read(PTBR); |
| 212 | printk(KERN_ALERT "ptbr = %08lx", page); |
Haavard Skinnemoen | 3201982 | 2008-02-13 12:32:34 +0100 | [diff] [blame] | 213 | if (address >= TASK_SIZE) |
| 214 | page = (unsigned long)swapper_pg_dir; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 215 | if (page) { |
| 216 | page = ((unsigned long *)page)[address >> 22]; |
| 217 | printk(" pgd = %08lx", page); |
| 218 | if (page & _PAGE_PRESENT) { |
| 219 | page &= PAGE_MASK; |
| 220 | address &= 0x003ff000; |
| 221 | page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT]; |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 222 | printk(" pte = %08lx", page); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 225 | printk("\n"); |
| 226 | die("Kernel access of bad area", regs, signr); |
| 227 | return; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * We ran out of memory, or some other thing happened to us |
| 231 | * that made us unable to handle the page fault gracefully. |
| 232 | */ |
| 233 | out_of_memory: |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 234 | up_read(&mm->mmap_sem); |
Nick Piggin | 67a8a20 | 2010-08-09 17:19:02 -0700 | [diff] [blame] | 235 | if (!user_mode(regs)) |
| 236 | goto no_context; |
Johannes Weiner | 8713410 | 2013-09-12 15:13:38 -0700 | [diff] [blame] | 237 | pagefault_out_of_memory(); |
Nick Piggin | 67a8a20 | 2010-08-09 17:19:02 -0700 | [diff] [blame] | 238 | return; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 239 | |
| 240 | do_sigbus: |
| 241 | up_read(&mm->mmap_sem); |
| 242 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 243 | /* Kernel mode? Handle exceptions or die */ |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 244 | signr = SIGBUS; |
| 245 | code = BUS_ADRERR; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 246 | if (!user_mode(regs)) |
| 247 | goto no_context; |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 248 | |
| 249 | if (exception_trace) |
| 250 | printk("%s%s[%d]: bus error at %08lx pc %08lx " |
| 251 | "sp %08lx ecr %lu\n", |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 252 | is_global_init(tsk) ? KERN_EMERG : KERN_INFO, |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 253 | tsk->comm, tsk->pid, address, regs->pc, |
| 254 | regs->sp, ecr); |
| 255 | |
| 256 | _exception(SIGBUS, regs, BUS_ADRERR, address); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | asmlinkage void do_bus_error(unsigned long addr, int write_access, |
| 260 | struct pt_regs *regs) |
| 261 | { |
| 262 | printk(KERN_ALERT |
| 263 | "Bus error at physical address 0x%08lx (%s access)\n", |
| 264 | addr, write_access ? "write" : "read"); |
| 265 | printk(KERN_INFO "DTLB dump:\n"); |
| 266 | dump_dtlb(); |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 267 | die("Bus Error", regs, SIGKILL); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 268 | } |