Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Page fault handler for SH with an MMU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright (C) 1999 Niibe Yutaka |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 5 | * Copyright (C) 2003 - 2012 Paul Mundt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Based on linux/arch/i386/mm/fault.c: |
| 8 | * Copyright (C) 1995 Linus Torvalds |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 9 | * |
| 10 | * This file is subject to the terms and conditions of the GNU General Public |
| 11 | * License. See the file "COPYING" in the main directory of this archive |
| 12 | * for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mm.h> |
Paul Mundt | 0f08f33 | 2006-09-27 17:03:56 +0900 | [diff] [blame] | 16 | #include <linux/hardirq.h> |
| 17 | #include <linux/kprobes.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 18 | #include <linux/perf_event.h> |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 19 | #include <linux/kdebug.h> |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 20 | #include <asm/io_trapped.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/mmu_context.h> |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 22 | #include <asm/tlbflush.h> |
David Howells | e839ca5 | 2012-03-28 18:30:03 +0100 | [diff] [blame] | 23 | #include <asm/traps.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Paul Mundt | 7433ab770 | 2009-06-25 02:30:10 +0900 | [diff] [blame] | 25 | static inline int notify_page_fault(struct pt_regs *regs, int trap) |
| 26 | { |
| 27 | int ret = 0; |
| 28 | |
Paul Mundt | c63c310 | 2009-07-05 02:50:10 +0900 | [diff] [blame] | 29 | if (kprobes_built_in() && !user_mode(regs)) { |
Paul Mundt | 7433ab770 | 2009-06-25 02:30:10 +0900 | [diff] [blame] | 30 | preempt_disable(); |
| 31 | if (kprobe_running() && kprobe_fault_handler(regs, trap)) |
| 32 | ret = 1; |
| 33 | preempt_enable(); |
| 34 | } |
Paul Mundt | 7433ab770 | 2009-06-25 02:30:10 +0900 | [diff] [blame] | 35 | |
| 36 | return ret; |
| 37 | } |
| 38 | |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 39 | static void |
| 40 | force_sig_info_fault(int si_signo, int si_code, unsigned long address, |
| 41 | struct task_struct *tsk) |
| 42 | { |
| 43 | siginfo_t info; |
| 44 | |
| 45 | info.si_signo = si_signo; |
| 46 | info.si_errno = 0; |
| 47 | info.si_code = si_code; |
| 48 | info.si_addr = (void __user *)address; |
| 49 | |
| 50 | force_sig_info(si_signo, &info, tsk); |
| 51 | } |
| 52 | |
Stuart Menefy | 45c0e0e | 2012-04-19 17:25:03 +0900 | [diff] [blame] | 53 | /* |
| 54 | * This is useful to dump out the page tables associated with |
| 55 | * 'addr' in mm 'mm'. |
| 56 | */ |
| 57 | static void show_pte(struct mm_struct *mm, unsigned long addr) |
| 58 | { |
| 59 | pgd_t *pgd; |
| 60 | |
| 61 | if (mm) |
| 62 | pgd = mm->pgd; |
| 63 | else |
| 64 | pgd = get_TTB(); |
| 65 | |
| 66 | printk(KERN_ALERT "pgd = %p\n", pgd); |
| 67 | pgd += pgd_index(addr); |
| 68 | printk(KERN_ALERT "[%08lx] *pgd=%0*Lx", addr, |
Paul Mundt | 2808032 | 2012-05-14 15:33:28 +0900 | [diff] [blame] | 69 | (u32)(sizeof(*pgd) * 2), (u64)pgd_val(*pgd)); |
Stuart Menefy | 45c0e0e | 2012-04-19 17:25:03 +0900 | [diff] [blame] | 70 | |
| 71 | do { |
| 72 | pud_t *pud; |
| 73 | pmd_t *pmd; |
| 74 | pte_t *pte; |
| 75 | |
| 76 | if (pgd_none(*pgd)) |
| 77 | break; |
| 78 | |
| 79 | if (pgd_bad(*pgd)) { |
| 80 | printk("(bad)"); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | pud = pud_offset(pgd, addr); |
| 85 | if (PTRS_PER_PUD != 1) |
Paul Mundt | 2808032 | 2012-05-14 15:33:28 +0900 | [diff] [blame] | 86 | printk(", *pud=%0*Lx", (u32)(sizeof(*pud) * 2), |
Stuart Menefy | 45c0e0e | 2012-04-19 17:25:03 +0900 | [diff] [blame] | 87 | (u64)pud_val(*pud)); |
| 88 | |
| 89 | if (pud_none(*pud)) |
| 90 | break; |
| 91 | |
| 92 | if (pud_bad(*pud)) { |
| 93 | printk("(bad)"); |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | pmd = pmd_offset(pud, addr); |
| 98 | if (PTRS_PER_PMD != 1) |
Paul Mundt | 2808032 | 2012-05-14 15:33:28 +0900 | [diff] [blame] | 99 | printk(", *pmd=%0*Lx", (u32)(sizeof(*pmd) * 2), |
Stuart Menefy | 45c0e0e | 2012-04-19 17:25:03 +0900 | [diff] [blame] | 100 | (u64)pmd_val(*pmd)); |
| 101 | |
| 102 | if (pmd_none(*pmd)) |
| 103 | break; |
| 104 | |
| 105 | if (pmd_bad(*pmd)) { |
| 106 | printk("(bad)"); |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | /* We must not map this if we have highmem enabled */ |
| 111 | if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT))) |
| 112 | break; |
| 113 | |
| 114 | pte = pte_offset_kernel(pmd, addr); |
Paul Mundt | 2808032 | 2012-05-14 15:33:28 +0900 | [diff] [blame] | 115 | printk(", *pte=%0*Lx", (u32)(sizeof(*pte) * 2), |
| 116 | (u64)pte_val(*pte)); |
Stuart Menefy | 45c0e0e | 2012-04-19 17:25:03 +0900 | [diff] [blame] | 117 | } while (0); |
| 118 | |
| 119 | printk("\n"); |
| 120 | } |
| 121 | |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 122 | static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) |
| 123 | { |
| 124 | unsigned index = pgd_index(address); |
| 125 | pgd_t *pgd_k; |
| 126 | pud_t *pud, *pud_k; |
| 127 | pmd_t *pmd, *pmd_k; |
| 128 | |
| 129 | pgd += index; |
| 130 | pgd_k = init_mm.pgd + index; |
| 131 | |
| 132 | if (!pgd_present(*pgd_k)) |
| 133 | return NULL; |
| 134 | |
| 135 | pud = pud_offset(pgd, address); |
| 136 | pud_k = pud_offset(pgd_k, address); |
| 137 | if (!pud_present(*pud_k)) |
| 138 | return NULL; |
| 139 | |
Matt Fleming | 5d9b4b1 | 2009-12-13 14:38:50 +0000 | [diff] [blame] | 140 | if (!pud_present(*pud)) |
| 141 | set_pud(pud, *pud_k); |
| 142 | |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 143 | pmd = pmd_offset(pud, address); |
| 144 | pmd_k = pmd_offset(pud_k, address); |
| 145 | if (!pmd_present(*pmd_k)) |
| 146 | return NULL; |
| 147 | |
| 148 | if (!pmd_present(*pmd)) |
| 149 | set_pmd(pmd, *pmd_k); |
Matt Fleming | 05dd2cd | 2009-07-13 11:38:04 +0000 | [diff] [blame] | 150 | else { |
| 151 | /* |
| 152 | * The page tables are fully synchronised so there must |
| 153 | * be another reason for the fault. Return NULL here to |
| 154 | * signal that we have not taken care of the fault. |
| 155 | */ |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 156 | BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k)); |
Matt Fleming | 05dd2cd | 2009-07-13 11:38:04 +0000 | [diff] [blame] | 157 | return NULL; |
| 158 | } |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 159 | |
| 160 | return pmd_k; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Handle a fault on the vmalloc or module mapping area |
| 165 | */ |
| 166 | static noinline int vmalloc_fault(unsigned long address) |
| 167 | { |
| 168 | pgd_t *pgd_k; |
| 169 | pmd_t *pmd_k; |
| 170 | pte_t *pte_k; |
| 171 | |
Paul Mundt | c3e0af9 | 2012-05-18 19:30:05 +0900 | [diff] [blame^] | 172 | /* Make sure we are in vmalloc/module/P3 area: */ |
| 173 | if (!(address >= P3SEG && address < P3_ADDR_MAX)) |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 174 | return -1; |
| 175 | |
| 176 | /* |
| 177 | * Synchronize this task's top level page-table |
| 178 | * with the 'reference' page table. |
| 179 | * |
| 180 | * Do _not_ use "current" here. We might be inside |
| 181 | * an interrupt in the middle of a task switch.. |
| 182 | */ |
| 183 | pgd_k = get_TTB(); |
Matt Fleming | 05dd2cd | 2009-07-13 11:38:04 +0000 | [diff] [blame] | 184 | pmd_k = vmalloc_sync_one(pgd_k, address); |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 185 | if (!pmd_k) |
| 186 | return -1; |
| 187 | |
| 188 | pte_k = pte_offset_kernel(pmd_k, address); |
| 189 | if (!pte_present(*pte_k)) |
| 190 | return -1; |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 195 | static void |
| 196 | show_fault_oops(struct pt_regs *regs, unsigned long address) |
| 197 | { |
| 198 | if (!oops_may_print()) |
| 199 | return; |
| 200 | |
| 201 | printk(KERN_ALERT "BUG: unable to handle kernel "); |
| 202 | if (address < PAGE_SIZE) |
| 203 | printk(KERN_CONT "NULL pointer dereference"); |
| 204 | else |
| 205 | printk(KERN_CONT "paging request"); |
| 206 | |
| 207 | printk(KERN_CONT " at %08lx\n", address); |
| 208 | printk(KERN_ALERT "PC:"); |
| 209 | printk_address(regs->pc, 1); |
| 210 | |
| 211 | show_pte(NULL, address); |
| 212 | } |
| 213 | |
| 214 | static noinline void |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 215 | no_context(struct pt_regs *regs, unsigned long error_code, |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 216 | unsigned long address) |
| 217 | { |
| 218 | /* Are we prepared to handle this kernel fault? */ |
| 219 | if (fixup_exception(regs)) |
| 220 | return; |
| 221 | |
| 222 | if (handle_trapped_io(regs, address)) |
| 223 | return; |
| 224 | |
| 225 | /* |
| 226 | * Oops. The kernel tried to access some bad page. We'll have to |
| 227 | * terminate things with extreme prejudice. |
| 228 | */ |
| 229 | bust_spinlocks(1); |
| 230 | |
| 231 | show_fault_oops(regs, address); |
| 232 | |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 233 | die("Oops", regs, error_code); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 234 | bust_spinlocks(0); |
| 235 | do_exit(SIGKILL); |
| 236 | } |
| 237 | |
| 238 | static void |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 239 | __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 240 | unsigned long address, int si_code) |
| 241 | { |
| 242 | struct task_struct *tsk = current; |
| 243 | |
| 244 | /* User mode accesses just cause a SIGSEGV */ |
| 245 | if (user_mode(regs)) { |
| 246 | /* |
| 247 | * It's possible to have interrupts off here: |
| 248 | */ |
| 249 | local_irq_enable(); |
| 250 | |
| 251 | force_sig_info_fault(SIGSEGV, si_code, address, tsk); |
| 252 | |
| 253 | return; |
| 254 | } |
| 255 | |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 256 | no_context(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static noinline void |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 260 | bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 261 | unsigned long address) |
| 262 | { |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 263 | __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 267 | __bad_area(struct pt_regs *regs, unsigned long error_code, |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 268 | unsigned long address, int si_code) |
| 269 | { |
| 270 | struct mm_struct *mm = current->mm; |
| 271 | |
| 272 | /* |
| 273 | * Something tried to access memory that isn't in our memory map.. |
| 274 | * Fix it, but check if it's kernel or user first.. |
| 275 | */ |
| 276 | up_read(&mm->mmap_sem); |
| 277 | |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 278 | __bad_area_nosemaphore(regs, error_code, address, si_code); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static noinline void |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 282 | bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address) |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 283 | { |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 284 | __bad_area(regs, error_code, address, SEGV_MAPERR); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static noinline void |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 288 | bad_area_access_error(struct pt_regs *regs, unsigned long error_code, |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 289 | unsigned long address) |
| 290 | { |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 291 | __bad_area(regs, error_code, address, SEGV_ACCERR); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static void out_of_memory(void) |
| 295 | { |
| 296 | /* |
| 297 | * We ran out of memory, call the OOM killer, and return the userspace |
| 298 | * (which will retry the fault, or kill us if we got oom-killed): |
| 299 | */ |
| 300 | up_read(¤t->mm->mmap_sem); |
| 301 | |
| 302 | pagefault_out_of_memory(); |
| 303 | } |
| 304 | |
| 305 | static void |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 306 | do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address) |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 307 | { |
| 308 | struct task_struct *tsk = current; |
| 309 | struct mm_struct *mm = tsk->mm; |
| 310 | |
| 311 | up_read(&mm->mmap_sem); |
| 312 | |
| 313 | /* Kernel mode? Handle exceptions or die: */ |
| 314 | if (!user_mode(regs)) |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 315 | no_context(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 316 | |
| 317 | force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk); |
| 318 | } |
| 319 | |
| 320 | static noinline int |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 321 | mm_fault_error(struct pt_regs *regs, unsigned long error_code, |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 322 | unsigned long address, unsigned int fault) |
| 323 | { |
| 324 | /* |
| 325 | * Pagefault was interrupted by SIGKILL. We have no reason to |
| 326 | * continue pagefault. |
| 327 | */ |
| 328 | if (fatal_signal_pending(current)) { |
| 329 | if (!(fault & VM_FAULT_RETRY)) |
| 330 | up_read(¤t->mm->mmap_sem); |
| 331 | if (!user_mode(regs)) |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 332 | no_context(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 333 | return 1; |
| 334 | } |
| 335 | |
| 336 | if (!(fault & VM_FAULT_ERROR)) |
| 337 | return 0; |
| 338 | |
| 339 | if (fault & VM_FAULT_OOM) { |
| 340 | /* Kernel mode? Handle exceptions or die: */ |
| 341 | if (!user_mode(regs)) { |
| 342 | up_read(¤t->mm->mmap_sem); |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 343 | no_context(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 344 | return 1; |
| 345 | } |
| 346 | |
| 347 | out_of_memory(); |
| 348 | } else { |
| 349 | if (fault & VM_FAULT_SIGBUS) |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 350 | do_sigbus(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 351 | else |
| 352 | BUG(); |
| 353 | } |
| 354 | |
| 355 | return 1; |
| 356 | } |
| 357 | |
Paul Mundt | 2808032 | 2012-05-14 15:33:28 +0900 | [diff] [blame] | 358 | static inline int access_error(int error_code, struct vm_area_struct *vma) |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 359 | { |
Paul Mundt | 2808032 | 2012-05-14 15:33:28 +0900 | [diff] [blame] | 360 | if (error_code & FAULT_CODE_WRITE) { |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 361 | /* write, present and write, not present: */ |
| 362 | if (unlikely(!(vma->vm_flags & VM_WRITE))) |
| 363 | return 1; |
| 364 | return 0; |
| 365 | } |
| 366 | |
Paul Mundt | 2808032 | 2012-05-14 15:33:28 +0900 | [diff] [blame] | 367 | /* ITLB miss on NX page */ |
| 368 | if (unlikely((error_code & FAULT_CODE_ITLB) && |
| 369 | !(vma->vm_flags & VM_EXEC))) |
| 370 | return 1; |
| 371 | |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 372 | /* read, not present: */ |
| 373 | if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))) |
| 374 | return 1; |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 379 | static int fault_in_kernel_space(unsigned long address) |
| 380 | { |
| 381 | return address >= TASK_SIZE; |
| 382 | } |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /* |
| 385 | * This routine handles page faults. It determines the address, |
| 386 | * and the problem, and then passes it off to one of the appropriate |
| 387 | * routines. |
| 388 | */ |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 389 | asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 390 | unsigned long error_code, |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 391 | unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 393 | unsigned long vec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | struct task_struct *tsk; |
| 395 | struct mm_struct *mm; |
| 396 | struct vm_area_struct * vma; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 397 | int fault; |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 398 | int write = error_code & FAULT_CODE_WRITE; |
Kautuk Consul | 11fd982 | 2012-03-31 08:06:11 -0400 | [diff] [blame] | 399 | unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE | |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 400 | (write ? FAULT_FLAG_WRITE : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | tsk = current; |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 403 | mm = tsk->mm; |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 404 | vec = lookup_exception_vector(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 406 | /* |
| 407 | * We fault-in kernel-space virtual memory on-demand. The |
| 408 | * 'reference' page table is init_mm.pgd. |
| 409 | * |
| 410 | * NOTE! We MUST NOT take any locks for this case. We may |
| 411 | * be in an interrupt or a critical region, and should |
| 412 | * only copy the information from the master page table, |
| 413 | * nothing more. |
| 414 | */ |
| 415 | if (unlikely(fault_in_kernel_space(address))) { |
| 416 | if (vmalloc_fault(address) >= 0) |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 417 | return; |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 418 | if (notify_page_fault(regs, vec)) |
Stuart Menefy | 96e14e5 | 2008-09-05 16:17:15 +0900 | [diff] [blame] | 419 | return; |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 420 | |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 421 | bad_area_nosemaphore(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 422 | return; |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 423 | } |
| 424 | |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 425 | if (unlikely(notify_page_fault(regs, vec))) |
Paul Mundt | 7433ab770 | 2009-06-25 02:30:10 +0900 | [diff] [blame] | 426 | return; |
| 427 | |
| 428 | /* Only enable interrupts if they were on before the fault */ |
| 429 | if ((regs->sr & SR_IMASK) != SR_IMASK) |
| 430 | local_irq_enable(); |
| 431 | |
Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 432 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); |
Paul Mundt | 7433ab770 | 2009-06-25 02:30:10 +0900 | [diff] [blame] | 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | /* |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 435 | * If we're in an interrupt, have no user context or are running |
| 436 | * in an atomic region then we must not take the fault: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | */ |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 438 | if (unlikely(in_atomic() || !mm)) { |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 439 | bad_area_nosemaphore(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 440 | return; |
| 441 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Kautuk Consul | 11fd982 | 2012-03-31 08:06:11 -0400 | [diff] [blame] | 443 | retry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | down_read(&mm->mmap_sem); |
| 445 | |
| 446 | vma = find_vma(mm, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 447 | if (unlikely(!vma)) { |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 448 | bad_area(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 449 | return; |
| 450 | } |
| 451 | if (likely(vma->vm_start <= address)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | goto good_area; |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 453 | if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 454 | bad_area(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | if (unlikely(expand_stack(vma, address))) { |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 458 | bad_area(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 459 | return; |
| 460 | } |
Paul Mundt | 0f60bb2 | 2009-07-05 03:18:47 +0900 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * Ok, we have a good vm_area for this memory access, so |
| 464 | * we can handle it.. |
| 465 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | good_area: |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 467 | if (unlikely(access_error(error_code, vma))) { |
| 468 | bad_area_access_error(regs, error_code, address); |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 469 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 472 | set_thread_fault_code(error_code); |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | /* |
| 475 | * If for any reason at all we couldn't handle the fault, |
| 476 | * make sure we exit gracefully rather than endlessly redo |
| 477 | * the fault. |
| 478 | */ |
Kautuk Consul | 11fd982 | 2012-03-31 08:06:11 -0400 | [diff] [blame] | 479 | fault = handle_mm_fault(mm, vma, address, flags); |
| 480 | |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 481 | if (unlikely(fault & (VM_FAULT_RETRY | VM_FAULT_ERROR))) |
Paul Mundt | 5a1dc78 | 2012-05-14 14:57:28 +0900 | [diff] [blame] | 482 | if (mm_fault_error(regs, error_code, address, fault)) |
Paul Mundt | dbdb4e9 | 2012-05-14 10:27:34 +0900 | [diff] [blame] | 483 | return; |
Kautuk Consul | 11fd982 | 2012-03-31 08:06:11 -0400 | [diff] [blame] | 484 | |
| 485 | if (flags & FAULT_FLAG_ALLOW_RETRY) { |
| 486 | if (fault & VM_FAULT_MAJOR) { |
| 487 | tsk->maj_flt++; |
| 488 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, |
| 489 | regs, address); |
| 490 | } else { |
| 491 | tsk->min_flt++; |
| 492 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, |
| 493 | regs, address); |
| 494 | } |
| 495 | if (fault & VM_FAULT_RETRY) { |
| 496 | flags &= ~FAULT_FLAG_ALLOW_RETRY; |
| 497 | |
| 498 | /* |
| 499 | * No need to up_read(&mm->mmap_sem) as we would |
| 500 | * have already released it in __lock_page_or_retry |
| 501 | * in mm/filemap.c. |
| 502 | */ |
| 503 | goto retry; |
| 504 | } |
Paul Mundt | 7433ab770 | 2009-06-25 02:30:10 +0900 | [diff] [blame] | 505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | up_read(&mm->mmap_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | } |