blob: 7475c2e162930acc7d81ac22087b0e94ef8b7907 [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
23#include <asm/system.h>
24#include <asm/pgtable.h>
25#include <asm/tlbflush.h>
Stepan Moskovchenko00da0742011-07-08 14:06:44 -070026#include <asm/cputype.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
28#include <asm/io.h>
29#include <mach/msm_iomap.h>
30#endif
31
32#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
33#include <asm/domain.h>
34#endif /* CONFIG_EMULATE_DOMAIN_MANAGER_V7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include "fault.h"
37
Russell Kingc88d6aa2009-09-20 12:41:58 +010038/*
Russell Kingdf297bf2009-09-20 13:18:47 +010039 * Fault status register encodings. We steal bit 31 for our own purposes.
Russell Kingc88d6aa2009-09-20 12:41:58 +010040 */
Russell Kingdf297bf2009-09-20 13:18:47 +010041#define FSR_LNX_PF (1 << 31)
Russell Kingc88d6aa2009-09-20 12:41:58 +010042#define FSR_WRITE (1 << 11)
43#define FSR_FS4 (1 << 10)
44#define FSR_FS3_0 (15)
45
46static inline int fsr_fs(unsigned int fsr)
47{
48 return (fsr & FSR_FS3_0) | (fsr & FSR_FS4) >> 6;
49}
50
Catalin Marinas09529f72009-07-24 12:34:55 +010051#ifdef CONFIG_MMU
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -050052
53#ifdef CONFIG_KPROBES
54static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
55{
56 int ret = 0;
57
58 if (!user_mode(regs)) {
59 /* kprobe_running() needs smp_processor_id() */
60 preempt_disable();
61 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
62 ret = 1;
63 preempt_enable();
64 }
65
66 return ret;
67}
68#else
69static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
70{
71 return 0;
72}
73#endif
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
76 * This is useful to dump out the page tables associated with
77 * 'addr' in mm 'mm'.
78 */
79void show_pte(struct mm_struct *mm, unsigned long addr)
80{
81 pgd_t *pgd;
82
83 if (!mm)
84 mm = &init_mm;
85
86 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
87 pgd = pgd_offset(mm, addr);
Will Deacon29a38192011-02-15 14:31:37 +010088 printk(KERN_ALERT "[%08lx] *pgd=%08llx",
89 addr, (long long)pgd_val(*pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 do {
Russell King516295e2010-11-21 16:27:49 +000092 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pmd_t *pmd;
94 pte_t *pte;
95
96 if (pgd_none(*pgd))
97 break;
98
99 if (pgd_bad(*pgd)) {
100 printk("(bad)");
101 break;
102 }
103
Russell King516295e2010-11-21 16:27:49 +0000104 pud = pud_offset(pgd, addr);
105 if (PTRS_PER_PUD != 1)
106 printk(", *pud=%08lx", pud_val(*pud));
107
108 if (pud_none(*pud))
109 break;
110
111 if (pud_bad(*pud)) {
112 printk("(bad)");
113 break;
114 }
115
116 pmd = pmd_offset(pud, addr);
Nicolas Pitreda46c792008-09-30 16:10:11 +0100117 if (PTRS_PER_PMD != 1)
Will Deacon29a38192011-02-15 14:31:37 +0100118 printk(", *pmd=%08llx", (long long)pmd_val(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (pmd_none(*pmd))
121 break;
122
123 if (pmd_bad(*pmd)) {
124 printk("(bad)");
125 break;
126 }
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /* We must not map this if we have highmem enabled */
Nicolas Pitre252d4c22008-09-11 11:52:02 -0400129 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
130 break;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 pte = pte_offset_map(pmd, addr);
Will Deacon29a38192011-02-15 14:31:37 +0100133 printk(", *pte=%08llx", (long long)pte_val(*pte));
134 printk(", *ppte=%08llx",
135 (long long)pte_val(pte[PTE_HWTABLE_PTRS]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 pte_unmap(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 } while(0);
138
139 printk("\n");
140}
Catalin Marinas09529f72009-07-24 12:34:55 +0100141#else /* CONFIG_MMU */
142void show_pte(struct mm_struct *mm, unsigned long addr)
143{ }
144#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/*
147 * Oops. The kernel tried to access some page that wasn't present.
148 */
149static void
150__do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
151 struct pt_regs *regs)
152{
153 /*
154 * Are we prepared to handle this kernel fault?
155 */
156 if (fixup_exception(regs))
157 return;
158
159 /*
160 * No handler, we'll have to terminate things with extreme prejudice.
161 */
162 bust_spinlocks(1);
163 printk(KERN_ALERT
164 "Unable to handle kernel %s at virtual address %08lx\n",
165 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
166 "paging request", addr);
167
168 show_pte(mm, addr);
169 die("Oops", regs, fsr);
170 bust_spinlocks(0);
171 do_exit(SIGKILL);
172}
173
174/*
175 * Something tried to access memory that isn't in our memory map..
176 * User mode accesses just cause a SIGSEGV
177 */
178static void
179__do_user_fault(struct task_struct *tsk, unsigned long addr,
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700180 unsigned int fsr, unsigned int sig, int code,
181 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct siginfo si;
184
185#ifdef CONFIG_DEBUG_USER
186 if (user_debug & UDBG_SEGV) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700187 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
188 tsk->comm, sig, addr, fsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 show_pte(tsk->mm, addr);
190 show_regs(regs);
191 }
192#endif
193
194 tsk->thread.address = addr;
195 tsk->thread.error_code = fsr;
196 tsk->thread.trap_no = 14;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700197 si.si_signo = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 si.si_errno = 0;
199 si.si_code = code;
200 si.si_addr = (void __user *)addr;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700201 force_sig_info(sig, &si, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Russell Kinge5beac32006-09-27 16:13:48 +0100204void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Russell Kinge5beac32006-09-27 16:13:48 +0100206 struct task_struct *tsk = current;
207 struct mm_struct *mm = tsk->active_mm;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /*
210 * If we are in kernel mode at this point, we
211 * have no context to handle this fault with.
212 */
213 if (user_mode(regs))
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700214 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 else
216 __do_kernel_fault(mm, addr, fsr, regs);
217}
218
Catalin Marinas09529f72009-07-24 12:34:55 +0100219#ifdef CONFIG_MMU
Nick Piggin5c72fc52007-07-20 09:21:06 +0200220#define VM_FAULT_BADMAP 0x010000
221#define VM_FAULT_BADACCESS 0x020000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Russell Kingd374bf12009-09-20 12:53:01 +0100223/*
224 * Check that the permissions on the VMA allow for the fault which occurred.
225 * If we encountered a write fault, we must have write permission, otherwise
226 * we allow any permission.
227 */
228static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
229{
230 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
231
232 if (fsr & FSR_WRITE)
233 mask = VM_WRITE;
Russell Kingdf297bf2009-09-20 13:18:47 +0100234 if (fsr & FSR_LNX_PF)
235 mask = VM_EXEC;
Russell Kingd374bf12009-09-20 12:53:01 +0100236
237 return vma->vm_flags & mask ? false : true;
238}
239
240static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
242 struct task_struct *tsk)
243{
244 struct vm_area_struct *vma;
Russell Kingd374bf12009-09-20 12:53:01 +0100245 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 vma = find_vma(mm, addr);
248 fault = VM_FAULT_BADMAP;
Russell Kingd374bf12009-09-20 12:53:01 +0100249 if (unlikely(!vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto out;
Russell Kingd374bf12009-09-20 12:53:01 +0100251 if (unlikely(vma->vm_start > addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto check_stack;
253
254 /*
255 * Ok, we have a good vm_area for this
256 * memory access, so we can handle it.
257 */
258good_area:
Russell Kingd374bf12009-09-20 12:53:01 +0100259 if (access_error(fsr, vma)) {
260 fault = VM_FAULT_BADACCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto out;
Russell Kingd374bf12009-09-20 12:53:01 +0100262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
Russell Kingb42c6342009-09-20 12:47:40 +0100265 * If for any reason at all we couldn't handle the fault, make
266 * sure we exit gracefully rather than endlessly redo the fault.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
Russell Kingc88d6aa2009-09-20 12:41:58 +0100268 fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0);
Russell Kingb42c6342009-09-20 12:47:40 +0100269 if (unlikely(fault & VM_FAULT_ERROR))
270 return fault;
Nick Piggin83c54072007-07-19 01:47:05 -0700271 if (fault & VM_FAULT_MAJOR)
272 tsk->maj_flt++;
273 else
274 tsk->min_flt++;
275 return fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277check_stack:
278 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
279 goto good_area;
280out:
281 return fault;
282}
283
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500284static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
286{
287 struct task_struct *tsk;
288 struct mm_struct *mm;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700289 int fault, sig, code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -0500291 if (notify_page_fault(regs, fsr))
292 return 0;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 tsk = current;
295 mm = tsk->mm;
296
297 /*
298 * If we're in an interrupt or have no user
299 * context, we must not take the fault..
300 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800301 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 goto no_context;
303
Russell King840ff6a2005-09-20 17:52:13 +0100304 /*
305 * As per x86, we may deadlock here. However, since the kernel only
306 * validly references user space from well defined areas of the code,
307 * we can bug out early if this is from code which shouldn't.
308 */
309 if (!down_read_trylock(&mm->mmap_sem)) {
310 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
311 goto no_context;
312 down_read(&mm->mmap_sem);
Russell Kingbf456992009-09-20 12:52:19 +0100313 } else {
314 /*
315 * The above down_read_trylock() might have succeeded in
316 * which case, we'll have missed the might_sleep() from
317 * down_read()
318 */
319 might_sleep();
Imre Deak1d212712009-10-05 13:40:44 +0100320#ifdef CONFIG_DEBUG_VM
321 if (!user_mode(regs) &&
322 !search_exception_tables(regs->ARM_pc))
323 goto no_context;
324#endif
Russell King840ff6a2005-09-20 17:52:13 +0100325 }
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 fault = __do_page_fault(mm, addr, fsr, tsk);
328 up_read(&mm->mmap_sem);
329
Jamie Iles7ada1892010-02-02 20:24:58 +0100330 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, addr);
331 if (fault & VM_FAULT_MAJOR)
332 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0, regs, addr);
333 else if (fault & VM_FAULT_MINOR)
334 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0, regs, addr);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
Russell Kingff2afb92005-08-04 14:17:33 +0100337 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Nick Piggin5c72fc52007-07-20 09:21:06 +0200339 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341
Russell Kingb42c6342009-09-20 12:47:40 +0100342 if (fault & VM_FAULT_OOM) {
343 /*
344 * We ran out of memory, call the OOM killer, and return to
345 * userspace (which will retry the fault, or kill us if we
346 * got oom-killed)
347 */
348 pagefault_out_of_memory();
349 return 0;
350 }
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * If we are in kernel mode at this point, we
354 * have no context to handle this fault with.
355 */
356 if (!user_mode(regs))
357 goto no_context;
358
Nick Piggin83c54072007-07-19 01:47:05 -0700359 if (fault & VM_FAULT_SIGBUS) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700360 /*
361 * We had some memory, but were unable to
362 * successfully fix up this page fault.
363 */
364 sig = SIGBUS;
365 code = BUS_ADRERR;
Nick Piggin83c54072007-07-19 01:47:05 -0700366 } else {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700367 /*
368 * Something tried to access memory that
369 * isn't in our memory map..
370 */
371 sig = SIGSEGV;
372 code = fault == VM_FAULT_BADACCESS ?
373 SEGV_ACCERR : SEGV_MAPERR;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700374 }
375
376 __do_user_fault(tsk, addr, fsr, sig, code, regs);
377 return 0;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379no_context:
380 __do_kernel_fault(mm, addr, fsr, regs);
381 return 0;
382}
Catalin Marinas09529f72009-07-24 12:34:55 +0100383#else /* CONFIG_MMU */
384static int
385do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
386{
387 return 0;
388}
389#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391/*
392 * First Level Translation Fault Handler
393 *
394 * We enter here because the first level page table doesn't contain
395 * a valid entry for the address.
396 *
397 * If the address is in kernel space (>= TASK_SIZE), then we are
398 * probably faulting in the vmalloc() area.
399 *
400 * If the init_task's first level page tables contains the relevant
401 * entry, we copy the it to this task. If not, we send the process
402 * a signal, fixup the exception, or oops the kernel.
403 *
404 * NOTE! We MUST NOT take any locks for this case. We may be in an
405 * interrupt or a critical region, and should only copy the information
406 * from the master page table, nothing more.
407 */
Catalin Marinas09529f72009-07-24 12:34:55 +0100408#ifdef CONFIG_MMU
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500409static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410do_translation_fault(unsigned long addr, unsigned int fsr,
411 struct pt_regs *regs)
412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 unsigned int index;
414 pgd_t *pgd, *pgd_k;
Russell King516295e2010-11-21 16:27:49 +0000415 pud_t *pud, *pud_k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 pmd_t *pmd, *pmd_k;
417
418 if (addr < TASK_SIZE)
419 return do_page_fault(addr, fsr, regs);
420
Anfei5e27fb72010-06-08 15:16:49 +0100421 if (user_mode(regs))
422 goto bad_area;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 index = pgd_index(addr);
425
426 /*
427 * FIXME: CP15 C1 is write only on ARMv3 architectures.
428 */
429 pgd = cpu_get_pgd() + index;
430 pgd_k = init_mm.pgd + index;
431
432 if (pgd_none(*pgd_k))
433 goto bad_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (!pgd_present(*pgd))
435 set_pgd(pgd, *pgd_k);
436
Russell King516295e2010-11-21 16:27:49 +0000437 pud = pud_offset(pgd, addr);
438 pud_k = pud_offset(pgd_k, addr);
439
440 if (pud_none(*pud_k))
441 goto bad_area;
442 if (!pud_present(*pud))
443 set_pud(pud, *pud_k);
444
445 pmd = pmd_offset(pud, addr);
446 pmd_k = pmd_offset(pud_k, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Kirill A. Shutemov33a9c412010-07-22 13:20:22 +0100448 /*
449 * On ARM one Linux PGD entry contains two hardware entries (see page
450 * tables layout in pgtable.h). We normally guarantee that we always
451 * fill both L1 entries. But create_mapping() doesn't follow the rule.
452 * It can create inidividual L1 entries, so here we have to call
453 * pmd_none() check for the entry really corresponded to address, not
454 * for the first of pair.
455 */
456 index = (addr >> SECTION_SHIFT) & 1;
457 if (pmd_none(pmd_k[index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 goto bad_area;
459
460 copy_pmd(pmd, pmd_k);
461 return 0;
462
463bad_area:
Russell Kinge5beac32006-09-27 16:13:48 +0100464 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466}
Catalin Marinas09529f72009-07-24 12:34:55 +0100467#else /* CONFIG_MMU */
468static int
469do_translation_fault(unsigned long addr, unsigned int fsr,
470 struct pt_regs *regs)
471{
472 return 0;
473}
474#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476/*
477 * Some section permission faults need to be handled gracefully.
478 * They can happen due to a __{get,put}_user during an oops.
479 */
480static int
481do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
482{
Russell Kinge5beac32006-09-27 16:13:48 +0100483 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485}
486
487/*
488 * This abort handler always returns "fault".
489 */
490static int
491do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
492{
493 return 1;
494}
495
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
497#define __str(x) #x
498#define MRC(x, v1, v2, v4, v5, v6) do { \
499 unsigned int __##x; \
500 asm("mrc " __str(v1) ", " __str(v2) ", %0, " __str(v4) ", " \
501 __str(v5) ", " __str(v6) "\n" \
502 : "=r" (__##x)); \
503 pr_info("%s: %s = 0x%.8x\n", __func__, #x, __##x); \
504} while(0)
505
506#define MSM_TCSR_SPARE2 (MSM_TCSR_BASE + 0x60)
507
508#endif
509
510static int
511do_imprecise_ext(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
512{
513#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
514 MRC(ADFSR, p15, 0, c5, c1, 0);
515 MRC(DFSR, p15, 0, c5, c0, 0);
516 MRC(ACTLR, p15, 0, c1, c0, 1);
517 MRC(EFSR, p15, 7, c15, c0, 1);
518 MRC(L2SR, p15, 3, c15, c1, 0);
519 MRC(L2CR0, p15, 3, c15, c0, 1);
520 MRC(L2CPUESR, p15, 3, c15, c1, 1);
521 MRC(L2CPUCR, p15, 3, c15, c0, 2);
522 MRC(SPESR, p15, 1, c9, c7, 0);
523 MRC(SPCR, p15, 0, c9, c7, 0);
524 MRC(DMACHSR, p15, 1, c11, c0, 0);
525 MRC(DMACHESR, p15, 1, c11, c0, 1);
526 MRC(DMACHCR, p15, 0, c11, c0, 2);
527
528 /* clear out EFSR and ADFSR after fault */
529 asm volatile ("mcr p15, 7, %0, c15, c0, 1\n\t"
530 "mcr p15, 0, %0, c5, c1, 0"
531 : : "r" (0));
532#endif
533#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
534 pr_info("%s: TCSR_SPARE2 = 0x%.8x\n", __func__, readl(MSM_TCSR_SPARE2));
535#endif
536 return 1;
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static struct fsr_info {
540 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
541 int sig;
Russell Kingcfb08102005-06-30 11:06:49 +0100542 int code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 const char *name;
544} fsr_info[] = {
545 /*
546 * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
547 * defines these to be "precise" aborts.
548 */
Russell Kingcfb08102005-06-30 11:06:49 +0100549 { do_bad, SIGSEGV, 0, "vector exception" },
Kirill A. Shutemov3dc91af2010-07-22 13:16:49 +0100550 { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" },
Russell Kingcfb08102005-06-30 11:06:49 +0100551 { do_bad, SIGKILL, 0, "terminal exception" },
Kirill A. Shutemov3dc91af2010-07-22 13:16:49 +0100552 { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" },
Russell Kingcfb08102005-06-30 11:06:49 +0100553 { do_bad, SIGBUS, 0, "external abort on linefetch" },
554 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
555 { do_bad, SIGBUS, 0, "external abort on linefetch" },
556 { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
557 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
558 { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
559 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
560 { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
561 { do_bad, SIGBUS, 0, "external abort on translation" },
562 { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
563 { do_bad, SIGBUS, 0, "external abort on translation" },
564 { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * The following are "imprecise" aborts, which are signalled by bit
567 * 10 of the FSR, and may not be recoverable. These are only
568 * supported if the CPU abort handler supports bit 10.
569 */
Russell Kingcfb08102005-06-30 11:06:49 +0100570 { do_bad, SIGBUS, 0, "unknown 16" },
571 { do_bad, SIGBUS, 0, "unknown 17" },
572 { do_bad, SIGBUS, 0, "unknown 18" },
573 { do_bad, SIGBUS, 0, "unknown 19" },
574 { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */
575 { do_bad, SIGBUS, 0, "unknown 21" },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700576 { do_imprecise_ext, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */
Russell Kingcfb08102005-06-30 11:06:49 +0100577 { do_bad, SIGBUS, 0, "unknown 23" },
578 { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */
579 { do_bad, SIGBUS, 0, "unknown 25" },
580 { do_bad, SIGBUS, 0, "unknown 26" },
581 { do_bad, SIGBUS, 0, "unknown 27" },
582 { do_bad, SIGBUS, 0, "unknown 28" },
583 { do_bad, SIGBUS, 0, "unknown 29" },
584 { do_bad, SIGBUS, 0, "unknown 30" },
585 { do_bad, SIGBUS, 0, "unknown 31" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586};
587
588void __init
589hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100590 int sig, int code, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100592 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
593 BUG();
594
595 fsr_info[nr].fn = fn;
596 fsr_info[nr].sig = sig;
597 fsr_info[nr].code = code;
598 fsr_info[nr].name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Stepan Moskovchenko00da0742011-07-08 14:06:44 -0700601#ifdef CONFIG_MSM_KRAIT_TBB_ABORT_HANDLER
602static int krait_tbb_fixup(unsigned int fsr, struct pt_regs *regs)
603{
604 int base_cond, cond = 0;
605 unsigned int p1, cpsr_z, cpsr_c, cpsr_n, cpsr_v;
606
607 if ((read_cpuid_id() & 0xFFFFFFFC) != 0x510F04D0)
608 return 0;
609
610 if (!thumb_mode(regs))
611 return 0;
612
613 /* If ITSTATE is 0, return quickly */
614 if ((regs->ARM_cpsr & PSR_IT_MASK) == 0)
615 return 0;
616
617 cpsr_n = (regs->ARM_cpsr & PSR_N_BIT) ? 1 : 0;
618 cpsr_z = (regs->ARM_cpsr & PSR_Z_BIT) ? 1 : 0;
619 cpsr_c = (regs->ARM_cpsr & PSR_C_BIT) ? 1 : 0;
620 cpsr_v = (regs->ARM_cpsr & PSR_V_BIT) ? 1 : 0;
621
622 p1 = (regs->ARM_cpsr & BIT(12)) ? 1 : 0;
623
624 base_cond = (regs->ARM_cpsr >> 13) & 0x07;
625
626 switch (base_cond) {
627 case 0x0: /* equal */
628 cond = cpsr_z;
629 break;
630
631 case 0x1: /* carry set */
632 cond = cpsr_c;
633 break;
634
635 case 0x2: /* minus / negative */
636 cond = cpsr_n;
637 break;
638
639 case 0x3: /* overflow */
640 cond = cpsr_v;
641 break;
642
643 case 0x4: /* unsigned higher */
644 cond = (cpsr_c == 1) && (cpsr_z == 0);
645 break;
646
647 case 0x5: /* signed greater / equal */
648 cond = (cpsr_n == cpsr_v);
649 break;
650
651 case 0x6: /* signed greater */
652 cond = (cpsr_z == 0) && (cpsr_n == cpsr_v);
653 break;
654
655 case 0x7: /* always */
656 cond = 1;
657 break;
658 };
659
660 if (cond == p1) {
661 pr_debug("Conditional abort fixup, PC=%08x, base=%d, cond=%d\n",
662 (unsigned int) regs->ARM_pc, base_cond, cond);
663 regs->ARM_pc += 2;
664 return 1;
665 }
666 return 0;
667}
668#endif
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/*
671 * Dispatch a data abort to the relevant handler.
672 */
Russell King7ab3f8d2007-03-02 15:01:36 +0000673asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
675{
Russell Kingc88d6aa2009-09-20 12:41:58 +0100676 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
Russell Kingcfb08102005-06-30 11:06:49 +0100677 struct siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700679#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
680 if (emulate_domain_manager_data_abort(fsr, addr))
681 return;
682#endif
683
Stepan Moskovchenko00da0742011-07-08 14:06:44 -0700684#ifdef CONFIG_MSM_KRAIT_TBB_ABORT_HANDLER
685 if (krait_tbb_fixup(fsr, regs))
686 return;
687#endif
688
Russell Kingdf297bf2009-09-20 13:18:47 +0100689 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return;
691
692 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
693 inf->name, fsr, addr);
Russell Kingcfb08102005-06-30 11:06:49 +0100694
695 info.si_signo = inf->sig;
696 info.si_errno = 0;
697 info.si_code = inf->code;
698 info.si_addr = (void __user *)addr;
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700699 arm_notify_die("", regs, &info, fsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100702
703static struct fsr_info ifsr_info[] = {
704 { do_bad, SIGBUS, 0, "unknown 0" },
705 { do_bad, SIGBUS, 0, "unknown 1" },
706 { do_bad, SIGBUS, 0, "debug event" },
707 { do_bad, SIGSEGV, SEGV_ACCERR, "section access flag fault" },
708 { do_bad, SIGBUS, 0, "unknown 4" },
709 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
710 { do_bad, SIGSEGV, SEGV_ACCERR, "page access flag fault" },
711 { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
712 { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
713 { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
714 { do_bad, SIGBUS, 0, "unknown 10" },
715 { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
716 { do_bad, SIGBUS, 0, "external abort on translation" },
717 { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
718 { do_bad, SIGBUS, 0, "external abort on translation" },
719 { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
720 { do_bad, SIGBUS, 0, "unknown 16" },
721 { do_bad, SIGBUS, 0, "unknown 17" },
722 { do_bad, SIGBUS, 0, "unknown 18" },
723 { do_bad, SIGBUS, 0, "unknown 19" },
724 { do_bad, SIGBUS, 0, "unknown 20" },
725 { do_bad, SIGBUS, 0, "unknown 21" },
726 { do_bad, SIGBUS, 0, "unknown 22" },
727 { do_bad, SIGBUS, 0, "unknown 23" },
728 { do_bad, SIGBUS, 0, "unknown 24" },
729 { do_bad, SIGBUS, 0, "unknown 25" },
730 { do_bad, SIGBUS, 0, "unknown 26" },
731 { do_bad, SIGBUS, 0, "unknown 27" },
732 { do_bad, SIGBUS, 0, "unknown 28" },
733 { do_bad, SIGBUS, 0, "unknown 29" },
734 { do_bad, SIGBUS, 0, "unknown 30" },
735 { do_bad, SIGBUS, 0, "unknown 31" },
736};
737
Will Deacon3a4b5dc2010-09-03 10:39:59 +0100738void __init
739hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
740 int sig, int code, const char *name)
741{
742 if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
743 BUG();
744
745 ifsr_info[nr].fn = fn;
746 ifsr_info[nr].sig = sig;
747 ifsr_info[nr].code = code;
748 ifsr_info[nr].name = name;
749}
750
Russell King7ab3f8d2007-03-02 15:01:36 +0000751asmlinkage void __exception
Kirill A. Shutemov4fb28472009-09-25 13:39:47 +0100752do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100754 const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
755 struct siginfo info;
756
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700757#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
758 if (emulate_domain_manager_prefetch_abort(ifsr, addr))
759 return;
760#endif
761
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100762 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
763 return;
764
765 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
766 inf->name, ifsr, addr);
767
768 info.si_signo = inf->sig;
769 info.si_errno = 0;
770 info.si_code = inf->code;
771 info.si_addr = (void __user *)addr;
772 arm_notify_die("", regs, &info, ifsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Kirill A. Shutemov993bf4e2010-07-22 13:23:25 +0100775static int __init exceptions_init(void)
776{
777 if (cpu_architecture() >= CPU_ARCH_ARMv6) {
778 hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
779 "I-cache maintenance fault");
780 }
781
Kirill A. Shutemovb8ab5392010-07-26 11:20:41 +0100782 if (cpu_architecture() >= CPU_ARCH_ARMv7) {
783 /*
784 * TODO: Access flag faults introduced in ARMv6K.
785 * Runtime check for 'K' extension is needed
786 */
787 hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
788 "section access flag fault");
789 hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
790 "section access flag fault");
791 }
792
Kirill A. Shutemov993bf4e2010-07-22 13:23:25 +0100793 return 0;
794}
795
796arch_initcall(exceptions_init);