| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  linux/arch/arm/mm/fault.c | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 1995  Linus Torvalds | 
|  | 5 | *  Modifications for ARM processor (c) 1995-2004 Russell King | 
|  | 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 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> | 
|  | 12 | #include <linux/signal.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/mm.h> | 
|  | 14 | #include <linux/init.h> | 
| Nicolas Pitre | 25ce1dd | 2007-12-03 15:21:57 -0500 | [diff] [blame] | 15 | #include <linux/kprobes.h> | 
| Russell King | 33fa9b1 | 2008-09-06 11:35:55 +0100 | [diff] [blame] | 16 | #include <linux/uaccess.h> | 
| Nicolas Pitre | 252d4c2 | 2008-09-11 11:52:02 -0400 | [diff] [blame] | 17 | #include <linux/page-flags.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include <asm/system.h> | 
|  | 20 | #include <asm/pgtable.h> | 
|  | 21 | #include <asm/tlbflush.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include "fault.h" | 
|  | 24 |  | 
| Nicolas Pitre | 25ce1dd | 2007-12-03 15:21:57 -0500 | [diff] [blame] | 25 |  | 
|  | 26 | #ifdef CONFIG_KPROBES | 
|  | 27 | static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr) | 
|  | 28 | { | 
|  | 29 | int ret = 0; | 
|  | 30 |  | 
|  | 31 | if (!user_mode(regs)) { | 
|  | 32 | /* kprobe_running() needs smp_processor_id() */ | 
|  | 33 | preempt_disable(); | 
|  | 34 | if (kprobe_running() && kprobe_fault_handler(regs, fsr)) | 
|  | 35 | ret = 1; | 
|  | 36 | preempt_enable(); | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | return ret; | 
|  | 40 | } | 
|  | 41 | #else | 
|  | 42 | static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr) | 
|  | 43 | { | 
|  | 44 | return 0; | 
|  | 45 | } | 
|  | 46 | #endif | 
|  | 47 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* | 
|  | 49 | * This is useful to dump out the page tables associated with | 
|  | 50 | * 'addr' in mm 'mm'. | 
|  | 51 | */ | 
|  | 52 | void show_pte(struct mm_struct *mm, unsigned long addr) | 
|  | 53 | { | 
|  | 54 | pgd_t *pgd; | 
|  | 55 |  | 
|  | 56 | if (!mm) | 
|  | 57 | mm = &init_mm; | 
|  | 58 |  | 
|  | 59 | printk(KERN_ALERT "pgd = %p\n", mm->pgd); | 
|  | 60 | pgd = pgd_offset(mm, addr); | 
|  | 61 | printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd)); | 
|  | 62 |  | 
|  | 63 | do { | 
|  | 64 | pmd_t *pmd; | 
|  | 65 | pte_t *pte; | 
|  | 66 |  | 
|  | 67 | if (pgd_none(*pgd)) | 
|  | 68 | break; | 
|  | 69 |  | 
|  | 70 | if (pgd_bad(*pgd)) { | 
|  | 71 | printk("(bad)"); | 
|  | 72 | break; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | pmd = pmd_offset(pgd, addr); | 
| Nicolas Pitre | da46c79 | 2008-09-30 16:10:11 +0100 | [diff] [blame] | 76 | if (PTRS_PER_PMD != 1) | 
|  | 77 | printk(", *pmd=%08lx", pmd_val(*pmd)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | if (pmd_none(*pmd)) | 
|  | 80 | break; | 
|  | 81 |  | 
|  | 82 | if (pmd_bad(*pmd)) { | 
|  | 83 | printk("(bad)"); | 
|  | 84 | break; | 
|  | 85 | } | 
|  | 86 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* We must not map this if we have highmem enabled */ | 
| Nicolas Pitre | 252d4c2 | 2008-09-11 11:52:02 -0400 | [diff] [blame] | 88 | if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT))) | 
|  | 89 | break; | 
|  | 90 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | pte = pte_offset_map(pmd, addr); | 
|  | 92 | printk(", *pte=%08lx", pte_val(*pte)); | 
|  | 93 | printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE])); | 
|  | 94 | pte_unmap(pte); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } while(0); | 
|  | 96 |  | 
|  | 97 | printk("\n"); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | /* | 
|  | 101 | * Oops.  The kernel tried to access some page that wasn't present. | 
|  | 102 | */ | 
|  | 103 | static void | 
|  | 104 | __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr, | 
|  | 105 | struct pt_regs *regs) | 
|  | 106 | { | 
|  | 107 | /* | 
|  | 108 | * Are we prepared to handle this kernel fault? | 
|  | 109 | */ | 
|  | 110 | if (fixup_exception(regs)) | 
|  | 111 | return; | 
|  | 112 |  | 
|  | 113 | /* | 
|  | 114 | * No handler, we'll have to terminate things with extreme prejudice. | 
|  | 115 | */ | 
|  | 116 | bust_spinlocks(1); | 
|  | 117 | printk(KERN_ALERT | 
|  | 118 | "Unable to handle kernel %s at virtual address %08lx\n", | 
|  | 119 | (addr < PAGE_SIZE) ? "NULL pointer dereference" : | 
|  | 120 | "paging request", addr); | 
|  | 121 |  | 
|  | 122 | show_pte(mm, addr); | 
|  | 123 | die("Oops", regs, fsr); | 
|  | 124 | bust_spinlocks(0); | 
|  | 125 | do_exit(SIGKILL); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | /* | 
|  | 129 | * Something tried to access memory that isn't in our memory map.. | 
|  | 130 | * User mode accesses just cause a SIGSEGV | 
|  | 131 | */ | 
|  | 132 | static void | 
|  | 133 | __do_user_fault(struct task_struct *tsk, unsigned long addr, | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 134 | unsigned int fsr, unsigned int sig, int code, | 
|  | 135 | struct pt_regs *regs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { | 
|  | 137 | struct siginfo si; | 
|  | 138 |  | 
|  | 139 | #ifdef CONFIG_DEBUG_USER | 
|  | 140 | if (user_debug & UDBG_SEGV) { | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 141 | printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n", | 
|  | 142 | tsk->comm, sig, addr, fsr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | show_pte(tsk->mm, addr); | 
|  | 144 | show_regs(regs); | 
|  | 145 | } | 
|  | 146 | #endif | 
|  | 147 |  | 
|  | 148 | tsk->thread.address = addr; | 
|  | 149 | tsk->thread.error_code = fsr; | 
|  | 150 | tsk->thread.trap_no = 14; | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 151 | si.si_signo = sig; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | si.si_errno = 0; | 
|  | 153 | si.si_code = code; | 
|  | 154 | si.si_addr = (void __user *)addr; | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 155 | force_sig_info(sig, &si, tsk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Russell King | e5beac3 | 2006-09-27 16:13:48 +0100 | [diff] [blame] | 158 | void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { | 
| Russell King | e5beac3 | 2006-09-27 16:13:48 +0100 | [diff] [blame] | 160 | struct task_struct *tsk = current; | 
|  | 161 | struct mm_struct *mm = tsk->active_mm; | 
|  | 162 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* | 
|  | 164 | * If we are in kernel mode at this point, we | 
|  | 165 | * have no context to handle this fault with. | 
|  | 166 | */ | 
|  | 167 | if (user_mode(regs)) | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 168 | __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | else | 
|  | 170 | __do_kernel_fault(mm, addr, fsr, regs); | 
|  | 171 | } | 
|  | 172 |  | 
| Nick Piggin | 5c72fc5 | 2007-07-20 09:21:06 +0200 | [diff] [blame] | 173 | #define VM_FAULT_BADMAP		0x010000 | 
|  | 174 | #define VM_FAULT_BADACCESS	0x020000 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 |  | 
|  | 176 | static int | 
|  | 177 | __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr, | 
|  | 178 | struct task_struct *tsk) | 
|  | 179 | { | 
|  | 180 | struct vm_area_struct *vma; | 
|  | 181 | int fault, mask; | 
|  | 182 |  | 
|  | 183 | vma = find_vma(mm, addr); | 
|  | 184 | fault = VM_FAULT_BADMAP; | 
|  | 185 | if (!vma) | 
|  | 186 | goto out; | 
|  | 187 | if (vma->vm_start > addr) | 
|  | 188 | goto check_stack; | 
|  | 189 |  | 
|  | 190 | /* | 
|  | 191 | * Ok, we have a good vm_area for this | 
|  | 192 | * memory access, so we can handle it. | 
|  | 193 | */ | 
|  | 194 | good_area: | 
|  | 195 | if (fsr & (1 << 11)) /* write? */ | 
|  | 196 | mask = VM_WRITE; | 
|  | 197 | else | 
| Jason Baron | df67b3d | 2006-09-29 01:58:58 -0700 | [diff] [blame] | 198 | mask = VM_READ|VM_EXEC|VM_WRITE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 |  | 
|  | 200 | fault = VM_FAULT_BADACCESS; | 
|  | 201 | if (!(vma->vm_flags & mask)) | 
|  | 202 | goto out; | 
|  | 203 |  | 
|  | 204 | /* | 
|  | 205 | * If for any reason at all we couldn't handle | 
|  | 206 | * the fault, make sure we exit gracefully rather | 
|  | 207 | * than endlessly redo the fault. | 
|  | 208 | */ | 
|  | 209 | survive: | 
|  | 210 | fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, fsr & (1 << 11)); | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 211 | if (unlikely(fault & VM_FAULT_ERROR)) { | 
|  | 212 | if (fault & VM_FAULT_OOM) | 
|  | 213 | goto out_of_memory; | 
|  | 214 | else if (fault & VM_FAULT_SIGBUS) | 
|  | 215 | return fault; | 
|  | 216 | BUG(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 218 | if (fault & VM_FAULT_MAJOR) | 
|  | 219 | tsk->maj_flt++; | 
|  | 220 | else | 
|  | 221 | tsk->min_flt++; | 
|  | 222 | return fault; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 |  | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 224 | out_of_memory: | 
| Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 225 | if (!is_global_init(tsk)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | goto out; | 
|  | 227 |  | 
|  | 228 | /* | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 229 | * If we are out of memory for pid1, sleep for a while and retry | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | */ | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 231 | up_read(&mm->mmap_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | yield(); | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 233 | down_read(&mm->mmap_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | goto survive; | 
|  | 235 |  | 
|  | 236 | check_stack: | 
|  | 237 | if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr)) | 
|  | 238 | goto good_area; | 
|  | 239 | out: | 
|  | 240 | return fault; | 
|  | 241 | } | 
|  | 242 |  | 
| Nicolas Pitre | 785d3cd | 2007-12-03 15:27:56 -0500 | [diff] [blame] | 243 | static int __kprobes | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | 
|  | 245 | { | 
|  | 246 | struct task_struct *tsk; | 
|  | 247 | struct mm_struct *mm; | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 248 | int fault, sig, code; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 |  | 
| Nicolas Pitre | 25ce1dd | 2007-12-03 15:21:57 -0500 | [diff] [blame] | 250 | if (notify_page_fault(regs, fsr)) | 
|  | 251 | return 0; | 
|  | 252 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | tsk = current; | 
|  | 254 | mm  = tsk->mm; | 
|  | 255 |  | 
|  | 256 | /* | 
|  | 257 | * If we're in an interrupt or have no user | 
|  | 258 | * context, we must not take the fault.. | 
|  | 259 | */ | 
| Peter Zijlstra | 6edaf68 | 2006-12-06 20:32:18 -0800 | [diff] [blame] | 260 | if (in_atomic() || !mm) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | goto no_context; | 
|  | 262 |  | 
| Russell King | 840ff6a | 2005-09-20 17:52:13 +0100 | [diff] [blame] | 263 | /* | 
|  | 264 | * As per x86, we may deadlock here.  However, since the kernel only | 
|  | 265 | * validly references user space from well defined areas of the code, | 
|  | 266 | * we can bug out early if this is from code which shouldn't. | 
|  | 267 | */ | 
|  | 268 | if (!down_read_trylock(&mm->mmap_sem)) { | 
|  | 269 | if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc)) | 
|  | 270 | goto no_context; | 
|  | 271 | down_read(&mm->mmap_sem); | 
|  | 272 | } | 
|  | 273 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | fault = __do_page_fault(mm, addr, fsr, tsk); | 
|  | 275 | up_read(&mm->mmap_sem); | 
|  | 276 |  | 
|  | 277 | /* | 
| Russell King | ff2afb9 | 2005-08-04 14:17:33 +0100 | [diff] [blame] | 278 | * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | */ | 
| Nick Piggin | 5c72fc5 | 2007-07-20 09:21:06 +0200 | [diff] [blame] | 280 | if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS)))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return 0; | 
|  | 282 |  | 
|  | 283 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | * If we are in kernel mode at this point, we | 
|  | 285 | * have no context to handle this fault with. | 
|  | 286 | */ | 
|  | 287 | if (!user_mode(regs)) | 
|  | 288 | goto no_context; | 
|  | 289 |  | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 290 | if (fault & VM_FAULT_OOM) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | /* | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 292 | * We ran out of memory, or some other thing | 
|  | 293 | * happened to us that made us unable to handle | 
|  | 294 | * the page fault gracefully. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | */ | 
|  | 296 | printk("VM: killing process %s\n", tsk->comm); | 
| Will Schmidt | dcca2bd | 2007-10-16 01:24:18 -0700 | [diff] [blame] | 297 | do_group_exit(SIGKILL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | return 0; | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 299 | } | 
|  | 300 | if (fault & VM_FAULT_SIGBUS) { | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 301 | /* | 
|  | 302 | * We had some memory, but were unable to | 
|  | 303 | * successfully fix up this page fault. | 
|  | 304 | */ | 
|  | 305 | sig = SIGBUS; | 
|  | 306 | code = BUS_ADRERR; | 
| Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 307 | } else { | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 308 | /* | 
|  | 309 | * Something tried to access memory that | 
|  | 310 | * isn't in our memory map.. | 
|  | 311 | */ | 
|  | 312 | sig = SIGSEGV; | 
|  | 313 | code = fault == VM_FAULT_BADACCESS ? | 
|  | 314 | SEGV_ACCERR : SEGV_MAPERR; | 
| akpm@osdl.org | 2d137c2 | 2005-04-16 15:23:55 -0700 | [diff] [blame] | 315 | } | 
|  | 316 |  | 
|  | 317 | __do_user_fault(tsk, addr, fsr, sig, code, regs); | 
|  | 318 | return 0; | 
|  | 319 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | no_context: | 
|  | 321 | __do_kernel_fault(mm, addr, fsr, regs); | 
|  | 322 | return 0; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | /* | 
|  | 326 | * First Level Translation Fault Handler | 
|  | 327 | * | 
|  | 328 | * We enter here because the first level page table doesn't contain | 
|  | 329 | * a valid entry for the address. | 
|  | 330 | * | 
|  | 331 | * If the address is in kernel space (>= TASK_SIZE), then we are | 
|  | 332 | * probably faulting in the vmalloc() area. | 
|  | 333 | * | 
|  | 334 | * If the init_task's first level page tables contains the relevant | 
|  | 335 | * entry, we copy the it to this task.  If not, we send the process | 
|  | 336 | * a signal, fixup the exception, or oops the kernel. | 
|  | 337 | * | 
|  | 338 | * NOTE! We MUST NOT take any locks for this case. We may be in an | 
|  | 339 | * interrupt or a critical region, and should only copy the information | 
|  | 340 | * from the master page table, nothing more. | 
|  | 341 | */ | 
| Nicolas Pitre | 785d3cd | 2007-12-03 15:27:56 -0500 | [diff] [blame] | 342 | static int __kprobes | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | do_translation_fault(unsigned long addr, unsigned int fsr, | 
|  | 344 | struct pt_regs *regs) | 
|  | 345 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | unsigned int index; | 
|  | 347 | pgd_t *pgd, *pgd_k; | 
|  | 348 | pmd_t *pmd, *pmd_k; | 
|  | 349 |  | 
|  | 350 | if (addr < TASK_SIZE) | 
|  | 351 | return do_page_fault(addr, fsr, regs); | 
|  | 352 |  | 
|  | 353 | index = pgd_index(addr); | 
|  | 354 |  | 
|  | 355 | /* | 
|  | 356 | * FIXME: CP15 C1 is write only on ARMv3 architectures. | 
|  | 357 | */ | 
|  | 358 | pgd = cpu_get_pgd() + index; | 
|  | 359 | pgd_k = init_mm.pgd + index; | 
|  | 360 |  | 
|  | 361 | if (pgd_none(*pgd_k)) | 
|  | 362 | goto bad_area; | 
|  | 363 |  | 
|  | 364 | if (!pgd_present(*pgd)) | 
|  | 365 | set_pgd(pgd, *pgd_k); | 
|  | 366 |  | 
|  | 367 | pmd_k = pmd_offset(pgd_k, addr); | 
|  | 368 | pmd   = pmd_offset(pgd, addr); | 
|  | 369 |  | 
|  | 370 | if (pmd_none(*pmd_k)) | 
|  | 371 | goto bad_area; | 
|  | 372 |  | 
|  | 373 | copy_pmd(pmd, pmd_k); | 
|  | 374 | return 0; | 
|  | 375 |  | 
|  | 376 | bad_area: | 
| Russell King | e5beac3 | 2006-09-27 16:13:48 +0100 | [diff] [blame] | 377 | do_bad_area(addr, fsr, regs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | return 0; | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | /* | 
|  | 382 | * Some section permission faults need to be handled gracefully. | 
|  | 383 | * They can happen due to a __{get,put}_user during an oops. | 
|  | 384 | */ | 
|  | 385 | static int | 
|  | 386 | do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | 
|  | 387 | { | 
| Russell King | e5beac3 | 2006-09-27 16:13:48 +0100 | [diff] [blame] | 388 | do_bad_area(addr, fsr, regs); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | return 0; | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | /* | 
|  | 393 | * This abort handler always returns "fault". | 
|  | 394 | */ | 
|  | 395 | static int | 
|  | 396 | do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | 
|  | 397 | { | 
|  | 398 | return 1; | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | static struct fsr_info { | 
|  | 402 | int	(*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs); | 
|  | 403 | int	sig; | 
| Russell King | cfb0810 | 2005-06-30 11:06:49 +0100 | [diff] [blame] | 404 | int	code; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | const char *name; | 
|  | 406 | } fsr_info[] = { | 
|  | 407 | /* | 
|  | 408 | * The following are the standard ARMv3 and ARMv4 aborts.  ARMv5 | 
|  | 409 | * defines these to be "precise" aborts. | 
|  | 410 | */ | 
| Russell King | cfb0810 | 2005-06-30 11:06:49 +0100 | [diff] [blame] | 411 | { do_bad,		SIGSEGV, 0,		"vector exception"		   }, | 
|  | 412 | { do_bad,		SIGILL,	 BUS_ADRALN,	"alignment exception"		   }, | 
|  | 413 | { do_bad,		SIGKILL, 0,		"terminal exception"		   }, | 
|  | 414 | { do_bad,		SIGILL,	 BUS_ADRALN,	"alignment exception"		   }, | 
|  | 415 | { do_bad,		SIGBUS,	 0,		"external abort on linefetch"	   }, | 
|  | 416 | { do_translation_fault,	SIGSEGV, SEGV_MAPERR,	"section translation fault"	   }, | 
|  | 417 | { do_bad,		SIGBUS,	 0,		"external abort on linefetch"	   }, | 
|  | 418 | { do_page_fault,	SIGSEGV, SEGV_MAPERR,	"page translation fault"	   }, | 
|  | 419 | { do_bad,		SIGBUS,	 0,		"external abort on non-linefetch"  }, | 
|  | 420 | { do_bad,		SIGSEGV, SEGV_ACCERR,	"section domain fault"		   }, | 
|  | 421 | { do_bad,		SIGBUS,	 0,		"external abort on non-linefetch"  }, | 
|  | 422 | { do_bad,		SIGSEGV, SEGV_ACCERR,	"page domain fault"		   }, | 
|  | 423 | { do_bad,		SIGBUS,	 0,		"external abort on translation"	   }, | 
|  | 424 | { do_sect_fault,	SIGSEGV, SEGV_ACCERR,	"section permission fault"	   }, | 
|  | 425 | { do_bad,		SIGBUS,	 0,		"external abort on translation"	   }, | 
|  | 426 | { do_page_fault,	SIGSEGV, SEGV_ACCERR,	"page permission fault"		   }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | /* | 
|  | 428 | * The following are "imprecise" aborts, which are signalled by bit | 
|  | 429 | * 10 of the FSR, and may not be recoverable.  These are only | 
|  | 430 | * supported if the CPU abort handler supports bit 10. | 
|  | 431 | */ | 
| Russell King | cfb0810 | 2005-06-30 11:06:49 +0100 | [diff] [blame] | 432 | { do_bad,		SIGBUS,  0,		"unknown 16"			   }, | 
|  | 433 | { do_bad,		SIGBUS,  0,		"unknown 17"			   }, | 
|  | 434 | { do_bad,		SIGBUS,  0,		"unknown 18"			   }, | 
|  | 435 | { do_bad,		SIGBUS,  0,		"unknown 19"			   }, | 
|  | 436 | { do_bad,		SIGBUS,  0,		"lock abort"			   }, /* xscale */ | 
|  | 437 | { do_bad,		SIGBUS,  0,		"unknown 21"			   }, | 
|  | 438 | { do_bad,		SIGBUS,  BUS_OBJERR,	"imprecise external abort"	   }, /* xscale */ | 
|  | 439 | { do_bad,		SIGBUS,  0,		"unknown 23"			   }, | 
|  | 440 | { do_bad,		SIGBUS,  0,		"dcache parity error"		   }, /* xscale */ | 
|  | 441 | { do_bad,		SIGBUS,  0,		"unknown 25"			   }, | 
|  | 442 | { do_bad,		SIGBUS,  0,		"unknown 26"			   }, | 
|  | 443 | { do_bad,		SIGBUS,  0,		"unknown 27"			   }, | 
|  | 444 | { do_bad,		SIGBUS,  0,		"unknown 28"			   }, | 
|  | 445 | { do_bad,		SIGBUS,  0,		"unknown 29"			   }, | 
|  | 446 | { do_bad,		SIGBUS,  0,		"unknown 30"			   }, | 
|  | 447 | { do_bad,		SIGBUS,  0,		"unknown 31"			   } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | }; | 
|  | 449 |  | 
|  | 450 | void __init | 
|  | 451 | hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *), | 
|  | 452 | int sig, const char *name) | 
|  | 453 | { | 
|  | 454 | if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) { | 
|  | 455 | fsr_info[nr].fn   = fn; | 
|  | 456 | fsr_info[nr].sig  = sig; | 
|  | 457 | fsr_info[nr].name = name; | 
|  | 458 | } | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | /* | 
|  | 462 | * Dispatch a data abort to the relevant handler. | 
|  | 463 | */ | 
| Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 464 | asmlinkage void __exception | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | 
|  | 466 | { | 
|  | 467 | const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6); | 
| Russell King | cfb0810 | 2005-06-30 11:06:49 +0100 | [diff] [blame] | 468 | struct siginfo info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 |  | 
|  | 470 | if (!inf->fn(addr, fsr, regs)) | 
|  | 471 | return; | 
|  | 472 |  | 
|  | 473 | printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n", | 
|  | 474 | inf->name, fsr, addr); | 
| Russell King | cfb0810 | 2005-06-30 11:06:49 +0100 | [diff] [blame] | 475 |  | 
|  | 476 | info.si_signo = inf->sig; | 
|  | 477 | info.si_errno = 0; | 
|  | 478 | info.si_code  = inf->code; | 
|  | 479 | info.si_addr  = (void __user *)addr; | 
| Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 480 | arm_notify_die("", regs, &info, fsr, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } | 
|  | 482 |  | 
| Russell King | 7ab3f8d | 2007-03-02 15:01:36 +0000 | [diff] [blame] | 483 | asmlinkage void __exception | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | do_PrefetchAbort(unsigned long addr, struct pt_regs *regs) | 
|  | 485 | { | 
|  | 486 | do_translation_fault(addr, 0, regs); | 
|  | 487 | } | 
|  | 488 |  |