blob: d7561bb7c52bc26ad747d68ce34dd43e9ad85ba8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
Russell King67306da2008-12-14 18:01:44 +000014#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -050016#include <linux/kprobes.h>
Russell King33fa9b12008-09-06 11:35:55 +010017#include <linux/uaccess.h>
Nicolas Pitre252d4c22008-09-11 11:52:02 -040018#include <linux/page-flags.h>
Catalin Marinas412bb0a2009-07-24 12:37:09 +010019#include <linux/sched.h>
Russell King65cec8e2009-08-17 20:02:06 +010020#include <linux/highmem.h>
Jamie Iles7ada1892010-02-02 20:24:58 +010021#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Jamie Iles0fd86292011-10-08 11:20:42 +010023#include <asm/exception.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/system.h>
25#include <asm/pgtable.h>
26#include <asm/tlbflush.h>
Stepan Moskovchenko00da0742011-07-08 14:06:44 -070027#include <asm/cputype.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
29#include <asm/io.h>
30#include <mach/msm_iomap.h>
31#endif
32
33#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
34#include <asm/domain.h>
35#endif /* CONFIG_EMULATE_DOMAIN_MANAGER_V7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include "fault.h"
38
Russell Kingc88d6aa2009-09-20 12:41:58 +010039/*
Russell Kingdf297bf2009-09-20 13:18:47 +010040 * Fault status register encodings. We steal bit 31 for our own purposes.
Russell Kingc88d6aa2009-09-20 12:41:58 +010041 */
Russell Kingdf297bf2009-09-20 13:18:47 +010042#define FSR_LNX_PF (1 << 31)
Russell Kingc88d6aa2009-09-20 12:41:58 +010043#define FSR_WRITE (1 << 11)
44#define FSR_FS4 (1 << 10)
45#define FSR_FS3_0 (15)
46
47static inline int fsr_fs(unsigned int fsr)
48{
49 return (fsr & FSR_FS3_0) | (fsr & FSR_FS4) >> 6;
50}
51
Catalin Marinas09529f72009-07-24 12:34:55 +010052#ifdef CONFIG_MMU
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -050053
54#ifdef CONFIG_KPROBES
55static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
56{
57 int ret = 0;
58
59 if (!user_mode(regs)) {
60 /* kprobe_running() needs smp_processor_id() */
61 preempt_disable();
62 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
63 ret = 1;
64 preempt_enable();
65 }
66
67 return ret;
68}
69#else
70static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
71{
72 return 0;
73}
74#endif
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * This is useful to dump out the page tables associated with
78 * 'addr' in mm 'mm'.
79 */
80void show_pte(struct mm_struct *mm, unsigned long addr)
81{
82 pgd_t *pgd;
83
84 if (!mm)
85 mm = &init_mm;
86
87 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
88 pgd = pgd_offset(mm, addr);
Will Deacon29a38192011-02-15 14:31:37 +010089 printk(KERN_ALERT "[%08lx] *pgd=%08llx",
90 addr, (long long)pgd_val(*pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 do {
Russell King516295e2010-11-21 16:27:49 +000093 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 pmd_t *pmd;
95 pte_t *pte;
96
97 if (pgd_none(*pgd))
98 break;
99
100 if (pgd_bad(*pgd)) {
101 printk("(bad)");
102 break;
103 }
104
Russell King516295e2010-11-21 16:27:49 +0000105 pud = pud_offset(pgd, addr);
106 if (PTRS_PER_PUD != 1)
107 printk(", *pud=%08lx", pud_val(*pud));
108
109 if (pud_none(*pud))
110 break;
111
112 if (pud_bad(*pud)) {
113 printk("(bad)");
114 break;
115 }
116
117 pmd = pmd_offset(pud, addr);
Nicolas Pitreda46c792008-09-30 16:10:11 +0100118 if (PTRS_PER_PMD != 1)
Will Deacon29a38192011-02-15 14:31:37 +0100119 printk(", *pmd=%08llx", (long long)pmd_val(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (pmd_none(*pmd))
122 break;
123
124 if (pmd_bad(*pmd)) {
125 printk("(bad)");
126 break;
127 }
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /* We must not map this if we have highmem enabled */
Nicolas Pitre252d4c22008-09-11 11:52:02 -0400130 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
131 break;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 pte = pte_offset_map(pmd, addr);
Will Deacon29a38192011-02-15 14:31:37 +0100134 printk(", *pte=%08llx", (long long)pte_val(*pte));
135 printk(", *ppte=%08llx",
136 (long long)pte_val(pte[PTE_HWTABLE_PTRS]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 pte_unmap(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 } while(0);
139
140 printk("\n");
141}
Catalin Marinas09529f72009-07-24 12:34:55 +0100142#else /* CONFIG_MMU */
143void show_pte(struct mm_struct *mm, unsigned long addr)
144{ }
145#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147/*
148 * Oops. The kernel tried to access some page that wasn't present.
149 */
150static void
151__do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
152 struct pt_regs *regs)
153{
154 /*
155 * Are we prepared to handle this kernel fault?
156 */
157 if (fixup_exception(regs))
158 return;
159
160 /*
161 * No handler, we'll have to terminate things with extreme prejudice.
162 */
163 bust_spinlocks(1);
164 printk(KERN_ALERT
165 "Unable to handle kernel %s at virtual address %08lx\n",
166 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
167 "paging request", addr);
168
169 show_pte(mm, addr);
170 die("Oops", regs, fsr);
171 bust_spinlocks(0);
172 do_exit(SIGKILL);
173}
174
175/*
176 * Something tried to access memory that isn't in our memory map..
177 * User mode accesses just cause a SIGSEGV
178 */
179static void
180__do_user_fault(struct task_struct *tsk, unsigned long addr,
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700181 unsigned int fsr, unsigned int sig, int code,
182 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct siginfo si;
185
186#ifdef CONFIG_DEBUG_USER
187 if (user_debug & UDBG_SEGV) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700188 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
189 tsk->comm, sig, addr, fsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 show_pte(tsk->mm, addr);
191 show_regs(regs);
192 }
193#endif
194
195 tsk->thread.address = addr;
196 tsk->thread.error_code = fsr;
197 tsk->thread.trap_no = 14;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700198 si.si_signo = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 si.si_errno = 0;
200 si.si_code = code;
201 si.si_addr = (void __user *)addr;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700202 force_sig_info(sig, &si, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Russell Kinge5beac32006-09-27 16:13:48 +0100205void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Russell Kinge5beac32006-09-27 16:13:48 +0100207 struct task_struct *tsk = current;
208 struct mm_struct *mm = tsk->active_mm;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /*
211 * If we are in kernel mode at this point, we
212 * have no context to handle this fault with.
213 */
214 if (user_mode(regs))
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700215 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 else
217 __do_kernel_fault(mm, addr, fsr, regs);
218}
219
Catalin Marinas09529f72009-07-24 12:34:55 +0100220#ifdef CONFIG_MMU
Nick Piggin5c72fc52007-07-20 09:21:06 +0200221#define VM_FAULT_BADMAP 0x010000
222#define VM_FAULT_BADACCESS 0x020000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Russell Kingd374bf12009-09-20 12:53:01 +0100224/*
225 * Check that the permissions on the VMA allow for the fault which occurred.
226 * If we encountered a write fault, we must have write permission, otherwise
227 * we allow any permission.
228 */
229static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
230{
231 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
232
233 if (fsr & FSR_WRITE)
234 mask = VM_WRITE;
Russell Kingdf297bf2009-09-20 13:18:47 +0100235 if (fsr & FSR_LNX_PF)
236 mask = VM_EXEC;
Russell Kingd374bf12009-09-20 12:53:01 +0100237
238 return vma->vm_flags & mask ? false : true;
239}
240
241static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
243 struct task_struct *tsk)
244{
245 struct vm_area_struct *vma;
Russell Kingd374bf12009-09-20 12:53:01 +0100246 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 vma = find_vma(mm, addr);
249 fault = VM_FAULT_BADMAP;
Russell Kingd374bf12009-09-20 12:53:01 +0100250 if (unlikely(!vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 goto out;
Russell Kingd374bf12009-09-20 12:53:01 +0100252 if (unlikely(vma->vm_start > addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto check_stack;
254
255 /*
256 * Ok, we have a good vm_area for this
257 * memory access, so we can handle it.
258 */
259good_area:
Russell Kingd374bf12009-09-20 12:53:01 +0100260 if (access_error(fsr, vma)) {
261 fault = VM_FAULT_BADACCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto out;
Russell Kingd374bf12009-09-20 12:53:01 +0100263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /*
Russell Kingb42c6342009-09-20 12:47:40 +0100266 * If for any reason at all we couldn't handle the fault, make
267 * sure we exit gracefully rather than endlessly redo the fault.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
Russell Kingc88d6aa2009-09-20 12:41:58 +0100269 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0);
Russell Kingb42c6342009-09-20 12:47:40 +0100270 if (unlikely(fault & VM_FAULT_ERROR))
271 return fault;
Nick Piggin83c54072007-07-19 01:47:05 -0700272 if (fault & VM_FAULT_MAJOR)
273 tsk->maj_flt++;
274 else
275 tsk->min_flt++;
276 return fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278check_stack:
279 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
280 goto good_area;
281out:
282 return fault;
283}
284
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500285static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
287{
288 struct task_struct *tsk;
289 struct mm_struct *mm;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700290 int fault, sig, code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -0500292 if (notify_page_fault(regs, fsr))
293 return 0;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 tsk = current;
296 mm = tsk->mm;
297
298 /*
299 * If we're in an interrupt or have no user
300 * context, we must not take the fault..
301 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800302 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 goto no_context;
304
Russell King840ff6a2005-09-20 17:52:13 +0100305 /*
306 * As per x86, we may deadlock here. However, since the kernel only
307 * validly references user space from well defined areas of the code,
308 * we can bug out early if this is from code which shouldn't.
309 */
310 if (!down_read_trylock(&mm->mmap_sem)) {
311 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
312 goto no_context;
313 down_read(&mm->mmap_sem);
Russell Kingbf456992009-09-20 12:52:19 +0100314 } else {
315 /*
316 * The above down_read_trylock() might have succeeded in
317 * which case, we'll have missed the might_sleep() from
318 * down_read()
319 */
320 might_sleep();
Imre Deak1d212712009-10-05 13:40:44 +0100321#ifdef CONFIG_DEBUG_VM
322 if (!user_mode(regs) &&
323 !search_exception_tables(regs->ARM_pc))
324 goto no_context;
325#endif
Russell King840ff6a2005-09-20 17:52:13 +0100326 }
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 fault = __do_page_fault(mm, addr, fsr, tsk);
329 up_read(&mm->mmap_sem);
330
Jamie Iles7ada1892010-02-02 20:24:58 +0100331 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, addr);
332 if (fault & VM_FAULT_MAJOR)
333 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0, regs, addr);
334 else if (fault & VM_FAULT_MINOR)
335 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0, regs, addr);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /*
Russell Kingff2afb92005-08-04 14:17:33 +0100338 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Nick Piggin5c72fc52007-07-20 09:21:06 +0200340 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 0;
342
Russell Kingb42c6342009-09-20 12:47:40 +0100343 if (fault & VM_FAULT_OOM) {
344 /*
345 * We ran out of memory, call the OOM killer, and return to
346 * userspace (which will retry the fault, or kill us if we
347 * got oom-killed)
348 */
349 pagefault_out_of_memory();
350 return 0;
351 }
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * If we are in kernel mode at this point, we
355 * have no context to handle this fault with.
356 */
357 if (!user_mode(regs))
358 goto no_context;
359
Nick Piggin83c54072007-07-19 01:47:05 -0700360 if (fault & VM_FAULT_SIGBUS) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700361 /*
362 * We had some memory, but were unable to
363 * successfully fix up this page fault.
364 */
365 sig = SIGBUS;
366 code = BUS_ADRERR;
Nick Piggin83c54072007-07-19 01:47:05 -0700367 } else {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700368 /*
369 * Something tried to access memory that
370 * isn't in our memory map..
371 */
372 sig = SIGSEGV;
373 code = fault == VM_FAULT_BADACCESS ?
374 SEGV_ACCERR : SEGV_MAPERR;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700375 }
376
377 __do_user_fault(tsk, addr, fsr, sig, code, regs);
378 return 0;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380no_context:
381 __do_kernel_fault(mm, addr, fsr, regs);
382 return 0;
383}
Catalin Marinas09529f72009-07-24 12:34:55 +0100384#else /* CONFIG_MMU */
385static int
386do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
387{
388 return 0;
389}
390#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392/*
393 * First Level Translation Fault Handler
394 *
395 * We enter here because the first level page table doesn't contain
396 * a valid entry for the address.
397 *
398 * If the address is in kernel space (>= TASK_SIZE), then we are
399 * probably faulting in the vmalloc() area.
400 *
401 * If the init_task's first level page tables contains the relevant
402 * entry, we copy the it to this task. If not, we send the process
403 * a signal, fixup the exception, or oops the kernel.
404 *
405 * NOTE! We MUST NOT take any locks for this case. We may be in an
406 * interrupt or a critical region, and should only copy the information
407 * from the master page table, nothing more.
408 */
Catalin Marinas09529f72009-07-24 12:34:55 +0100409#ifdef CONFIG_MMU
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500410static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411do_translation_fault(unsigned long addr, unsigned int fsr,
412 struct pt_regs *regs)
413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 unsigned int index;
415 pgd_t *pgd, *pgd_k;
Russell King516295e2010-11-21 16:27:49 +0000416 pud_t *pud, *pud_k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 pmd_t *pmd, *pmd_k;
418
419 if (addr < TASK_SIZE)
420 return do_page_fault(addr, fsr, regs);
421
Anfei5e27fb72010-06-08 15:16:49 +0100422 if (user_mode(regs))
423 goto bad_area;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 index = pgd_index(addr);
426
427 /*
428 * FIXME: CP15 C1 is write only on ARMv3 architectures.
429 */
430 pgd = cpu_get_pgd() + index;
431 pgd_k = init_mm.pgd + index;
432
433 if (pgd_none(*pgd_k))
434 goto bad_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!pgd_present(*pgd))
436 set_pgd(pgd, *pgd_k);
437
Russell King516295e2010-11-21 16:27:49 +0000438 pud = pud_offset(pgd, addr);
439 pud_k = pud_offset(pgd_k, addr);
440
441 if (pud_none(*pud_k))
442 goto bad_area;
443 if (!pud_present(*pud))
444 set_pud(pud, *pud_k);
445
446 pmd = pmd_offset(pud, addr);
447 pmd_k = pmd_offset(pud_k, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Kirill A. Shutemov33a9c412010-07-22 13:20:22 +0100449 /*
450 * On ARM one Linux PGD entry contains two hardware entries (see page
451 * tables layout in pgtable.h). We normally guarantee that we always
452 * fill both L1 entries. But create_mapping() doesn't follow the rule.
453 * It can create inidividual L1 entries, so here we have to call
454 * pmd_none() check for the entry really corresponded to address, not
455 * for the first of pair.
456 */
457 index = (addr >> SECTION_SHIFT) & 1;
458 if (pmd_none(pmd_k[index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 goto bad_area;
460
461 copy_pmd(pmd, pmd_k);
462 return 0;
463
464bad_area:
Russell Kinge5beac32006-09-27 16:13:48 +0100465 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
467}
Catalin Marinas09529f72009-07-24 12:34:55 +0100468#else /* CONFIG_MMU */
469static int
470do_translation_fault(unsigned long addr, unsigned int fsr,
471 struct pt_regs *regs)
472{
473 return 0;
474}
475#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477/*
478 * Some section permission faults need to be handled gracefully.
479 * They can happen due to a __{get,put}_user during an oops.
480 */
481static int
482do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
483{
Russell Kinge5beac32006-09-27 16:13:48 +0100484 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486}
487
488/*
489 * This abort handler always returns "fault".
490 */
491static int
492do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
493{
494 return 1;
495}
496
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700497#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
498#define __str(x) #x
499#define MRC(x, v1, v2, v4, v5, v6) do { \
500 unsigned int __##x; \
501 asm("mrc " __str(v1) ", " __str(v2) ", %0, " __str(v4) ", " \
502 __str(v5) ", " __str(v6) "\n" \
503 : "=r" (__##x)); \
504 pr_info("%s: %s = 0x%.8x\n", __func__, #x, __##x); \
505} while(0)
506
507#define MSM_TCSR_SPARE2 (MSM_TCSR_BASE + 0x60)
508
509#endif
510
511static int
512do_imprecise_ext(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
513{
514#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
515 MRC(ADFSR, p15, 0, c5, c1, 0);
516 MRC(DFSR, p15, 0, c5, c0, 0);
517 MRC(ACTLR, p15, 0, c1, c0, 1);
518 MRC(EFSR, p15, 7, c15, c0, 1);
519 MRC(L2SR, p15, 3, c15, c1, 0);
520 MRC(L2CR0, p15, 3, c15, c0, 1);
521 MRC(L2CPUESR, p15, 3, c15, c1, 1);
522 MRC(L2CPUCR, p15, 3, c15, c0, 2);
523 MRC(SPESR, p15, 1, c9, c7, 0);
524 MRC(SPCR, p15, 0, c9, c7, 0);
525 MRC(DMACHSR, p15, 1, c11, c0, 0);
526 MRC(DMACHESR, p15, 1, c11, c0, 1);
527 MRC(DMACHCR, p15, 0, c11, c0, 2);
528
529 /* clear out EFSR and ADFSR after fault */
530 asm volatile ("mcr p15, 7, %0, c15, c0, 1\n\t"
531 "mcr p15, 0, %0, c5, c1, 0"
532 : : "r" (0));
533#endif
534#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
535 pr_info("%s: TCSR_SPARE2 = 0x%.8x\n", __func__, readl(MSM_TCSR_SPARE2));
536#endif
537 return 1;
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540static struct fsr_info {
541 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
542 int sig;
Russell Kingcfb08102005-06-30 11:06:49 +0100543 int code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 const char *name;
545} fsr_info[] = {
546 /*
547 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
548 * defines these to be "precise" aborts.
549 */
Russell Kingcfb08102005-06-30 11:06:49 +0100550 { do_bad, SIGSEGV, 0, "vector exception" },
Kirill A. Shutemov3dc91af2010-07-22 13:16:49 +0100551 { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" },
Russell Kingcfb08102005-06-30 11:06:49 +0100552 { do_bad, SIGKILL, 0, "terminal exception" },
Kirill A. Shutemov3dc91af2010-07-22 13:16:49 +0100553 { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" },
Russell Kingcfb08102005-06-30 11:06:49 +0100554 { do_bad, SIGBUS, 0, "external abort on linefetch" },
555 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
556 { do_bad, SIGBUS, 0, "external abort on linefetch" },
557 { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
558 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
559 { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
560 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
561 { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
562 { do_bad, SIGBUS, 0, "external abort on translation" },
563 { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
564 { do_bad, SIGBUS, 0, "external abort on translation" },
565 { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /*
567 * The following are "imprecise" aborts, which are signalled by bit
568 * 10 of the FSR, and may not be recoverable. These are only
569 * supported if the CPU abort handler supports bit 10.
570 */
Russell Kingcfb08102005-06-30 11:06:49 +0100571 { do_bad, SIGBUS, 0, "unknown 16" },
572 { do_bad, SIGBUS, 0, "unknown 17" },
573 { do_bad, SIGBUS, 0, "unknown 18" },
574 { do_bad, SIGBUS, 0, "unknown 19" },
575 { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */
576 { do_bad, SIGBUS, 0, "unknown 21" },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577 { do_imprecise_ext, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */
Russell Kingcfb08102005-06-30 11:06:49 +0100578 { do_bad, SIGBUS, 0, "unknown 23" },
579 { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */
580 { do_bad, SIGBUS, 0, "unknown 25" },
581 { do_bad, SIGBUS, 0, "unknown 26" },
582 { do_bad, SIGBUS, 0, "unknown 27" },
583 { do_bad, SIGBUS, 0, "unknown 28" },
584 { do_bad, SIGBUS, 0, "unknown 29" },
585 { do_bad, SIGBUS, 0, "unknown 30" },
586 { do_bad, SIGBUS, 0, "unknown 31" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587};
588
589void __init
590hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100591 int sig, int code, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100593 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
594 BUG();
595
596 fsr_info[nr].fn = fn;
597 fsr_info[nr].sig = sig;
598 fsr_info[nr].code = code;
599 fsr_info[nr].name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Stepan Moskovchenko00da0742011-07-08 14:06:44 -0700602#ifdef CONFIG_MSM_KRAIT_TBB_ABORT_HANDLER
603static int krait_tbb_fixup(unsigned int fsr, struct pt_regs *regs)
604{
605 int base_cond, cond = 0;
606 unsigned int p1, cpsr_z, cpsr_c, cpsr_n, cpsr_v;
607
608 if ((read_cpuid_id() & 0xFFFFFFFC) != 0x510F04D0)
609 return 0;
610
611 if (!thumb_mode(regs))
612 return 0;
613
614 /* If ITSTATE is 0, return quickly */
615 if ((regs->ARM_cpsr & PSR_IT_MASK) == 0)
616 return 0;
617
618 cpsr_n = (regs->ARM_cpsr & PSR_N_BIT) ? 1 : 0;
619 cpsr_z = (regs->ARM_cpsr & PSR_Z_BIT) ? 1 : 0;
620 cpsr_c = (regs->ARM_cpsr & PSR_C_BIT) ? 1 : 0;
621 cpsr_v = (regs->ARM_cpsr & PSR_V_BIT) ? 1 : 0;
622
623 p1 = (regs->ARM_cpsr & BIT(12)) ? 1 : 0;
624
625 base_cond = (regs->ARM_cpsr >> 13) & 0x07;
626
627 switch (base_cond) {
628 case 0x0: /* equal */
629 cond = cpsr_z;
630 break;
631
632 case 0x1: /* carry set */
633 cond = cpsr_c;
634 break;
635
636 case 0x2: /* minus / negative */
637 cond = cpsr_n;
638 break;
639
640 case 0x3: /* overflow */
641 cond = cpsr_v;
642 break;
643
644 case 0x4: /* unsigned higher */
645 cond = (cpsr_c == 1) && (cpsr_z == 0);
646 break;
647
648 case 0x5: /* signed greater / equal */
649 cond = (cpsr_n == cpsr_v);
650 break;
651
652 case 0x6: /* signed greater */
653 cond = (cpsr_z == 0) && (cpsr_n == cpsr_v);
654 break;
655
656 case 0x7: /* always */
657 cond = 1;
658 break;
659 };
660
661 if (cond == p1) {
662 pr_debug("Conditional abort fixup, PC=%08x, base=%d, cond=%d\n",
663 (unsigned int) regs->ARM_pc, base_cond, cond);
664 regs->ARM_pc += 2;
665 return 1;
666 }
667 return 0;
668}
669#endif
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/*
672 * Dispatch a data abort to the relevant handler.
673 */
Russell King7ab3f8d2007-03-02 15:01:36 +0000674asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
676{
Russell Kingc88d6aa2009-09-20 12:41:58 +0100677 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
Russell Kingcfb08102005-06-30 11:06:49 +0100678 struct siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
681 if (emulate_domain_manager_data_abort(fsr, addr))
682 return;
683#endif
684
Stepan Moskovchenko00da0742011-07-08 14:06:44 -0700685#ifdef CONFIG_MSM_KRAIT_TBB_ABORT_HANDLER
686 if (krait_tbb_fixup(fsr, regs))
687 return;
688#endif
689
Russell Kingdf297bf2009-09-20 13:18:47 +0100690 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return;
692
693 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
694 inf->name, fsr, addr);
Russell Kingcfb08102005-06-30 11:06:49 +0100695
696 info.si_signo = inf->sig;
697 info.si_errno = 0;
698 info.si_code = inf->code;
699 info.si_addr = (void __user *)addr;
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700700 arm_notify_die("", regs, &info, fsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100703
704static struct fsr_info ifsr_info[] = {
705 { do_bad, SIGBUS, 0, "unknown 0" },
706 { do_bad, SIGBUS, 0, "unknown 1" },
707 { do_bad, SIGBUS, 0, "debug event" },
708 { do_bad, SIGSEGV, SEGV_ACCERR, "section access flag fault" },
709 { do_bad, SIGBUS, 0, "unknown 4" },
710 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
711 { do_bad, SIGSEGV, SEGV_ACCERR, "page access flag fault" },
712 { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
713 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
714 { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
715 { do_bad, SIGBUS, 0, "unknown 10" },
716 { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
717 { do_bad, SIGBUS, 0, "external abort on translation" },
718 { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
719 { do_bad, SIGBUS, 0, "external abort on translation" },
720 { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
721 { do_bad, SIGBUS, 0, "unknown 16" },
722 { do_bad, SIGBUS, 0, "unknown 17" },
723 { do_bad, SIGBUS, 0, "unknown 18" },
724 { do_bad, SIGBUS, 0, "unknown 19" },
725 { do_bad, SIGBUS, 0, "unknown 20" },
726 { do_bad, SIGBUS, 0, "unknown 21" },
727 { do_bad, SIGBUS, 0, "unknown 22" },
728 { do_bad, SIGBUS, 0, "unknown 23" },
729 { do_bad, SIGBUS, 0, "unknown 24" },
730 { do_bad, SIGBUS, 0, "unknown 25" },
731 { do_bad, SIGBUS, 0, "unknown 26" },
732 { do_bad, SIGBUS, 0, "unknown 27" },
733 { do_bad, SIGBUS, 0, "unknown 28" },
734 { do_bad, SIGBUS, 0, "unknown 29" },
735 { do_bad, SIGBUS, 0, "unknown 30" },
736 { do_bad, SIGBUS, 0, "unknown 31" },
737};
738
Will Deacon3a4b5dc2010-09-03 10:39:59 +0100739void __init
740hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
741 int sig, int code, const char *name)
742{
743 if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
744 BUG();
745
746 ifsr_info[nr].fn = fn;
747 ifsr_info[nr].sig = sig;
748 ifsr_info[nr].code = code;
749 ifsr_info[nr].name = name;
750}
751
Russell King7ab3f8d2007-03-02 15:01:36 +0000752asmlinkage void __exception
Kirill A. Shutemov4fb28472009-09-25 13:39:47 +0100753do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100755 const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
756 struct siginfo info;
757
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
759 if (emulate_domain_manager_prefetch_abort(ifsr, addr))
760 return;
761#endif
762
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100763 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
764 return;
765
766 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
767 inf->name, ifsr, addr);
768
769 info.si_signo = inf->sig;
770 info.si_errno = 0;
771 info.si_code = inf->code;
772 info.si_addr = (void __user *)addr;
773 arm_notify_die("", regs, &info, ifsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Kirill A. Shutemov993bf4e2010-07-22 13:23:25 +0100776static int __init exceptions_init(void)
777{
778 if (cpu_architecture() >= CPU_ARCH_ARMv6) {
779 hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
780 "I-cache maintenance fault");
781 }
782
Kirill A. Shutemovb8ab5392010-07-26 11:20:41 +0100783 if (cpu_architecture() >= CPU_ARCH_ARMv7) {
784 /*
785 * TODO: Access flag faults introduced in ARMv6K.
786 * Runtime check for 'K' extension is needed
787 */
788 hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
789 "section access flag fault");
790 hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
791 "section access flag fault");
792 }
793
Kirill A. Shutemov993bf4e2010-07-22 13:23:25 +0100794 return 0;
795}
796
797arch_initcall(exceptions_init);