blob: ed03b339884b5d5440af2eb63523a83bc7d032f7 [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 Iles5a567d72011-10-08 11:20:42 +010023#include <asm/exception.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/pgtable.h>
David Howells9f97da72012-03-28 18:30:01 +010025#include <asm/system_misc.h>
26#include <asm/system_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/tlbflush.h>
Stepan Moskovchenko00da0742011-07-08 14:06:44 -070028#include <asm/cputype.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
30#include <asm/io.h>
31#include <mach/msm_iomap.h>
32#endif
33
34#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
35#include <asm/domain.h>
36#endif /* CONFIG_EMULATE_DOMAIN_MANAGER_V7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include "fault.h"
39
Catalin Marinas09529f72009-07-24 12:34:55 +010040#ifdef CONFIG_MMU
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -050041
42#ifdef CONFIG_KPROBES
43static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
44{
45 int ret = 0;
46
47 if (!user_mode(regs)) {
48 /* kprobe_running() needs smp_processor_id() */
49 preempt_disable();
50 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
51 ret = 1;
52 preempt_enable();
53 }
54
55 return ret;
56}
57#else
58static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
59{
60 return 0;
61}
62#endif
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * This is useful to dump out the page tables associated with
66 * 'addr' in mm 'mm'.
67 */
68void show_pte(struct mm_struct *mm, unsigned long addr)
69{
70 pgd_t *pgd;
71
72 if (!mm)
73 mm = &init_mm;
74
75 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
76 pgd = pgd_offset(mm, addr);
Will Deacon29a38192011-02-15 14:31:37 +010077 printk(KERN_ALERT "[%08lx] *pgd=%08llx",
78 addr, (long long)pgd_val(*pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 do {
Russell King516295e2010-11-21 16:27:49 +000081 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 pmd_t *pmd;
83 pte_t *pte;
84
85 if (pgd_none(*pgd))
86 break;
87
88 if (pgd_bad(*pgd)) {
89 printk("(bad)");
90 break;
91 }
92
Russell King516295e2010-11-21 16:27:49 +000093 pud = pud_offset(pgd, addr);
94 if (PTRS_PER_PUD != 1)
Catalin Marinas140d5dc2011-07-13 16:32:58 +010095 printk(", *pud=%08llx", (long long)pud_val(*pud));
Russell King516295e2010-11-21 16:27:49 +000096
97 if (pud_none(*pud))
98 break;
99
100 if (pud_bad(*pud)) {
101 printk("(bad)");
102 break;
103 }
104
105 pmd = pmd_offset(pud, addr);
Nicolas Pitreda46c792008-09-30 16:10:11 +0100106 if (PTRS_PER_PMD != 1)
Will Deacon29a38192011-02-15 14:31:37 +0100107 printk(", *pmd=%08llx", (long long)pmd_val(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if (pmd_none(*pmd))
110 break;
111
112 if (pmd_bad(*pmd)) {
113 printk("(bad)");
114 break;
115 }
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* We must not map this if we have highmem enabled */
Nicolas Pitre252d4c22008-09-11 11:52:02 -0400118 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
119 break;
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 pte = pte_offset_map(pmd, addr);
Will Deacon29a38192011-02-15 14:31:37 +0100122 printk(", *pte=%08llx", (long long)pte_val(*pte));
Catalin Marinasf7b81562011-11-22 17:30:31 +0000123#ifndef CONFIG_ARM_LPAE
Will Deacon29a38192011-02-15 14:31:37 +0100124 printk(", *ppte=%08llx",
125 (long long)pte_val(pte[PTE_HWTABLE_PTRS]));
Catalin Marinasf7b81562011-11-22 17:30:31 +0000126#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 pte_unmap(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 } while(0);
129
130 printk("\n");
131}
Catalin Marinas09529f72009-07-24 12:34:55 +0100132#else /* CONFIG_MMU */
133void show_pte(struct mm_struct *mm, unsigned long addr)
134{ }
135#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/*
138 * Oops. The kernel tried to access some page that wasn't present.
139 */
140static void
141__do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
142 struct pt_regs *regs)
143{
144 /*
145 * Are we prepared to handle this kernel fault?
146 */
147 if (fixup_exception(regs))
148 return;
149
150 /*
151 * No handler, we'll have to terminate things with extreme prejudice.
152 */
153 bust_spinlocks(1);
154 printk(KERN_ALERT
155 "Unable to handle kernel %s at virtual address %08lx\n",
156 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
157 "paging request", addr);
158
159 show_pte(mm, addr);
160 die("Oops", regs, fsr);
161 bust_spinlocks(0);
162 do_exit(SIGKILL);
163}
164
165/*
166 * Something tried to access memory that isn't in our memory map..
167 * User mode accesses just cause a SIGSEGV
168 */
169static void
170__do_user_fault(struct task_struct *tsk, unsigned long addr,
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700171 unsigned int fsr, unsigned int sig, int code,
172 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct siginfo si;
175
176#ifdef CONFIG_DEBUG_USER
Javi Merinof5274c22012-02-06 15:45:36 +0100177 if (((user_debug & UDBG_SEGV) && (sig == SIGSEGV)) ||
178 ((user_debug & UDBG_BUS) && (sig == SIGBUS))) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700179 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
180 tsk->comm, sig, addr, fsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 show_pte(tsk->mm, addr);
182 show_regs(regs);
183 }
184#endif
185
186 tsk->thread.address = addr;
187 tsk->thread.error_code = fsr;
188 tsk->thread.trap_no = 14;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700189 si.si_signo = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 si.si_errno = 0;
191 si.si_code = code;
192 si.si_addr = (void __user *)addr;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700193 force_sig_info(sig, &si, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Russell Kinge5beac32006-09-27 16:13:48 +0100196void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Russell Kinge5beac32006-09-27 16:13:48 +0100198 struct task_struct *tsk = current;
199 struct mm_struct *mm = tsk->active_mm;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /*
202 * If we are in kernel mode at this point, we
203 * have no context to handle this fault with.
204 */
205 if (user_mode(regs))
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700206 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 else
208 __do_kernel_fault(mm, addr, fsr, regs);
209}
210
Catalin Marinas09529f72009-07-24 12:34:55 +0100211#ifdef CONFIG_MMU
Nick Piggin5c72fc52007-07-20 09:21:06 +0200212#define VM_FAULT_BADMAP 0x010000
213#define VM_FAULT_BADACCESS 0x020000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Russell Kingd374bf12009-09-20 12:53:01 +0100215/*
216 * Check that the permissions on the VMA allow for the fault which occurred.
217 * If we encountered a write fault, we must have write permission, otherwise
218 * we allow any permission.
219 */
220static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
221{
222 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
223
224 if (fsr & FSR_WRITE)
225 mask = VM_WRITE;
Russell Kingdf297bf2009-09-20 13:18:47 +0100226 if (fsr & FSR_LNX_PF)
227 mask = VM_EXEC;
Russell Kingd374bf12009-09-20 12:53:01 +0100228
229 return vma->vm_flags & mask ? false : true;
230}
231
232static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
Kautuk Consul8878a532011-11-27 17:49:50 +0100234 unsigned int flags, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 struct vm_area_struct *vma;
Russell Kingd374bf12009-09-20 12:53:01 +0100237 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 vma = find_vma(mm, addr);
240 fault = VM_FAULT_BADMAP;
Russell Kingd374bf12009-09-20 12:53:01 +0100241 if (unlikely(!vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto out;
Russell Kingd374bf12009-09-20 12:53:01 +0100243 if (unlikely(vma->vm_start > addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto check_stack;
245
246 /*
247 * Ok, we have a good vm_area for this
248 * memory access, so we can handle it.
249 */
250good_area:
Russell Kingd374bf12009-09-20 12:53:01 +0100251 if (access_error(fsr, vma)) {
252 fault = VM_FAULT_BADACCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 goto out;
Russell Kingd374bf12009-09-20 12:53:01 +0100254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Kautuk Consul8878a532011-11-27 17:49:50 +0100256 return handle_mm_fault(mm, vma, addr & PAGE_MASK, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258check_stack:
Russell King9b61a4d2012-05-16 15:19:20 +0100259 /* Don't allow expansion below FIRST_USER_ADDRESS */
260 if (vma->vm_flags & VM_GROWSDOWN &&
261 addr >= FIRST_USER_ADDRESS && !expand_stack(vma, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto good_area;
263out:
264 return fault;
265}
266
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500267static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
269{
270 struct task_struct *tsk;
271 struct mm_struct *mm;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700272 int fault, sig, code;
Kautuk Consul8878a532011-11-27 17:49:50 +0100273 int write = fsr & FSR_WRITE;
274 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
275 (write ? FAULT_FLAG_WRITE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -0500277 if (notify_page_fault(regs, fsr))
278 return 0;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 tsk = current;
281 mm = tsk->mm;
282
Russell King02fe2842011-06-25 11:44:06 +0100283 /* Enable interrupts if they were enabled in the parent context. */
284 if (interrupts_enabled(regs))
285 local_irq_enable();
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /*
288 * If we're in an interrupt or have no user
289 * context, we must not take the fault..
290 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800291 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 goto no_context;
293
Russell King840ff6a2005-09-20 17:52:13 +0100294 /*
295 * As per x86, we may deadlock here. However, since the kernel only
296 * validly references user space from well defined areas of the code,
297 * we can bug out early if this is from code which shouldn't.
298 */
299 if (!down_read_trylock(&mm->mmap_sem)) {
300 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
301 goto no_context;
Kautuk Consul8878a532011-11-27 17:49:50 +0100302retry:
Russell King840ff6a2005-09-20 17:52:13 +0100303 down_read(&mm->mmap_sem);
Russell Kingbf456992009-09-20 12:52:19 +0100304 } else {
305 /*
306 * The above down_read_trylock() might have succeeded in
307 * which case, we'll have missed the might_sleep() from
308 * down_read()
309 */
310 might_sleep();
Imre Deak1d212712009-10-05 13:40:44 +0100311#ifdef CONFIG_DEBUG_VM
312 if (!user_mode(regs) &&
313 !search_exception_tables(regs->ARM_pc))
314 goto no_context;
315#endif
Russell King840ff6a2005-09-20 17:52:13 +0100316 }
317
Kautuk Consul8878a532011-11-27 17:49:50 +0100318 fault = __do_page_fault(mm, addr, fsr, flags, tsk);
319
320 /* If we need to retry but a fatal signal is pending, handle the
321 * signal first. We do not need to release the mmap_sem because
322 * it would already be released in __lock_page_or_retry in
323 * mm/filemap.c. */
324 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
325 return 0;
326
327 /*
328 * Major/minor page fault accounting is only done on the
329 * initial attempt. If we go through a retry, it is extremely
330 * likely that the page will be found in page cache at that point.
331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200333 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
Kautuk Consuldff2aa72012-04-02 18:19:49 +0100334 if (!(fault & VM_FAULT_ERROR) && flags & FAULT_FLAG_ALLOW_RETRY) {
Kautuk Consul8878a532011-11-27 17:49:50 +0100335 if (fault & VM_FAULT_MAJOR) {
336 tsk->maj_flt++;
337 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
338 regs, addr);
339 } else {
340 tsk->min_flt++;
341 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
342 regs, addr);
343 }
344 if (fault & VM_FAULT_RETRY) {
345 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
346 * of starvation. */
347 flags &= ~FAULT_FLAG_ALLOW_RETRY;
348 goto retry;
349 }
350 }
351
352 up_read(&mm->mmap_sem);
Jamie Iles7ada1892010-02-02 20:24:58 +0100353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /*
Russell Kingff2afb92005-08-04 14:17:33 +0100355 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Nick Piggin5c72fc52007-07-20 09:21:06 +0200357 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 0;
359
Russell Kingb42c6342009-09-20 12:47:40 +0100360 if (fault & VM_FAULT_OOM) {
361 /*
362 * We ran out of memory, call the OOM killer, and return to
363 * userspace (which will retry the fault, or kill us if we
364 * got oom-killed)
365 */
366 pagefault_out_of_memory();
367 return 0;
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 * If we are in kernel mode at this point, we
372 * have no context to handle this fault with.
373 */
374 if (!user_mode(regs))
375 goto no_context;
376
Nick Piggin83c54072007-07-19 01:47:05 -0700377 if (fault & VM_FAULT_SIGBUS) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700378 /*
379 * We had some memory, but were unable to
380 * successfully fix up this page fault.
381 */
382 sig = SIGBUS;
383 code = BUS_ADRERR;
Nick Piggin83c54072007-07-19 01:47:05 -0700384 } else {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700385 /*
386 * Something tried to access memory that
387 * isn't in our memory map..
388 */
389 sig = SIGSEGV;
390 code = fault == VM_FAULT_BADACCESS ?
391 SEGV_ACCERR : SEGV_MAPERR;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700392 }
393
394 __do_user_fault(tsk, addr, fsr, sig, code, regs);
395 return 0;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397no_context:
398 __do_kernel_fault(mm, addr, fsr, regs);
399 return 0;
400}
Catalin Marinas09529f72009-07-24 12:34:55 +0100401#else /* CONFIG_MMU */
402static int
403do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
404{
405 return 0;
406}
407#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409/*
410 * First Level Translation Fault Handler
411 *
412 * We enter here because the first level page table doesn't contain
413 * a valid entry for the address.
414 *
415 * If the address is in kernel space (>= TASK_SIZE), then we are
416 * probably faulting in the vmalloc() area.
417 *
418 * If the init_task's first level page tables contains the relevant
419 * entry, we copy the it to this task. If not, we send the process
420 * a signal, fixup the exception, or oops the kernel.
421 *
422 * NOTE! We MUST NOT take any locks for this case. We may be in an
423 * interrupt or a critical region, and should only copy the information
424 * from the master page table, nothing more.
425 */
Catalin Marinas09529f72009-07-24 12:34:55 +0100426#ifdef CONFIG_MMU
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500427static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428do_translation_fault(unsigned long addr, unsigned int fsr,
429 struct pt_regs *regs)
430{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned int index;
432 pgd_t *pgd, *pgd_k;
Russell King516295e2010-11-21 16:27:49 +0000433 pud_t *pud, *pud_k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 pmd_t *pmd, *pmd_k;
435
436 if (addr < TASK_SIZE)
437 return do_page_fault(addr, fsr, regs);
438
Anfei5e27fb72010-06-08 15:16:49 +0100439 if (user_mode(regs))
440 goto bad_area;
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 index = pgd_index(addr);
443
444 /*
445 * FIXME: CP15 C1 is write only on ARMv3 architectures.
446 */
447 pgd = cpu_get_pgd() + index;
448 pgd_k = init_mm.pgd + index;
449
450 if (pgd_none(*pgd_k))
451 goto bad_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!pgd_present(*pgd))
453 set_pgd(pgd, *pgd_k);
454
Russell King516295e2010-11-21 16:27:49 +0000455 pud = pud_offset(pgd, addr);
456 pud_k = pud_offset(pgd_k, addr);
457
458 if (pud_none(*pud_k))
459 goto bad_area;
460 if (!pud_present(*pud))
461 set_pud(pud, *pud_k);
462
463 pmd = pmd_offset(pud, addr);
464 pmd_k = pmd_offset(pud_k, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Catalin Marinasf7b81562011-11-22 17:30:31 +0000466#ifdef CONFIG_ARM_LPAE
467 /*
468 * Only one hardware entry per PMD with LPAE.
469 */
470 index = 0;
471#else
Kirill A. Shutemov33a9c412010-07-22 13:20:22 +0100472 /*
473 * On ARM one Linux PGD entry contains two hardware entries (see page
474 * tables layout in pgtable.h). We normally guarantee that we always
475 * fill both L1 entries. But create_mapping() doesn't follow the rule.
476 * It can create inidividual L1 entries, so here we have to call
477 * pmd_none() check for the entry really corresponded to address, not
478 * for the first of pair.
479 */
480 index = (addr >> SECTION_SHIFT) & 1;
Catalin Marinasf7b81562011-11-22 17:30:31 +0000481#endif
Kirill A. Shutemov33a9c412010-07-22 13:20:22 +0100482 if (pmd_none(pmd_k[index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto bad_area;
484
485 copy_pmd(pmd, pmd_k);
486 return 0;
487
488bad_area:
Russell Kinge5beac32006-09-27 16:13:48 +0100489 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return 0;
491}
Catalin Marinas09529f72009-07-24 12:34:55 +0100492#else /* CONFIG_MMU */
493static int
494do_translation_fault(unsigned long addr, unsigned int fsr,
495 struct pt_regs *regs)
496{
497 return 0;
498}
499#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501/*
502 * Some section permission faults need to be handled gracefully.
503 * They can happen due to a __{get,put}_user during an oops.
504 */
505static int
506do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
507{
Russell Kinge5beac32006-09-27 16:13:48 +0100508 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 0;
510}
511
512/*
513 * This abort handler always returns "fault".
514 */
515static int
516do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
517{
518 return 1;
519}
520
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
522#define __str(x) #x
523#define MRC(x, v1, v2, v4, v5, v6) do { \
524 unsigned int __##x; \
525 asm("mrc " __str(v1) ", " __str(v2) ", %0, " __str(v4) ", " \
526 __str(v5) ", " __str(v6) "\n" \
527 : "=r" (__##x)); \
528 pr_info("%s: %s = 0x%.8x\n", __func__, #x, __##x); \
529} while(0)
530
531#define MSM_TCSR_SPARE2 (MSM_TCSR_BASE + 0x60)
532
533#endif
534
Steve Mucklef132c6c2012-06-06 18:30:57 -0700535int
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536do_imprecise_ext(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
537{
538#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
539 MRC(ADFSR, p15, 0, c5, c1, 0);
540 MRC(DFSR, p15, 0, c5, c0, 0);
541 MRC(ACTLR, p15, 0, c1, c0, 1);
542 MRC(EFSR, p15, 7, c15, c0, 1);
543 MRC(L2SR, p15, 3, c15, c1, 0);
544 MRC(L2CR0, p15, 3, c15, c0, 1);
545 MRC(L2CPUESR, p15, 3, c15, c1, 1);
546 MRC(L2CPUCR, p15, 3, c15, c0, 2);
547 MRC(SPESR, p15, 1, c9, c7, 0);
548 MRC(SPCR, p15, 0, c9, c7, 0);
549 MRC(DMACHSR, p15, 1, c11, c0, 0);
550 MRC(DMACHESR, p15, 1, c11, c0, 1);
551 MRC(DMACHCR, p15, 0, c11, c0, 2);
552
553 /* clear out EFSR and ADFSR after fault */
554 asm volatile ("mcr p15, 7, %0, c15, c0, 1\n\t"
555 "mcr p15, 0, %0, c5, c1, 0"
556 : : "r" (0));
557#endif
558#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
559 pr_info("%s: TCSR_SPARE2 = 0x%.8x\n", __func__, readl(MSM_TCSR_SPARE2));
560#endif
561 return 1;
562}
563
Catalin Marinas136848d2011-11-22 17:30:28 +0000564struct fsr_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
566 int sig;
Russell Kingcfb08102005-06-30 11:06:49 +0100567 int code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569};
570
Catalin Marinas136848d2011-11-22 17:30:28 +0000571/* FSR definition */
Catalin Marinasf7b81562011-11-22 17:30:31 +0000572#ifdef CONFIG_ARM_LPAE
573#include "fsr-3level.c"
574#else
Catalin Marinas136848d2011-11-22 17:30:28 +0000575#include "fsr-2level.c"
Catalin Marinasf7b81562011-11-22 17:30:31 +0000576#endif
Catalin Marinas136848d2011-11-22 17:30:28 +0000577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578void __init
579hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100580 int sig, int code, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100582 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
583 BUG();
584
585 fsr_info[nr].fn = fn;
586 fsr_info[nr].sig = sig;
587 fsr_info[nr].code = code;
588 fsr_info[nr].name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Stepan Moskovchenko00da0742011-07-08 14:06:44 -0700591#ifdef CONFIG_MSM_KRAIT_TBB_ABORT_HANDLER
592static int krait_tbb_fixup(unsigned int fsr, struct pt_regs *regs)
593{
594 int base_cond, cond = 0;
595 unsigned int p1, cpsr_z, cpsr_c, cpsr_n, cpsr_v;
596
597 if ((read_cpuid_id() & 0xFFFFFFFC) != 0x510F04D0)
598 return 0;
599
600 if (!thumb_mode(regs))
601 return 0;
602
603 /* If ITSTATE is 0, return quickly */
604 if ((regs->ARM_cpsr & PSR_IT_MASK) == 0)
605 return 0;
606
607 cpsr_n = (regs->ARM_cpsr & PSR_N_BIT) ? 1 : 0;
608 cpsr_z = (regs->ARM_cpsr & PSR_Z_BIT) ? 1 : 0;
609 cpsr_c = (regs->ARM_cpsr & PSR_C_BIT) ? 1 : 0;
610 cpsr_v = (regs->ARM_cpsr & PSR_V_BIT) ? 1 : 0;
611
612 p1 = (regs->ARM_cpsr & BIT(12)) ? 1 : 0;
613
614 base_cond = (regs->ARM_cpsr >> 13) & 0x07;
615
616 switch (base_cond) {
617 case 0x0: /* equal */
618 cond = cpsr_z;
619 break;
620
621 case 0x1: /* carry set */
622 cond = cpsr_c;
623 break;
624
625 case 0x2: /* minus / negative */
626 cond = cpsr_n;
627 break;
628
629 case 0x3: /* overflow */
630 cond = cpsr_v;
631 break;
632
633 case 0x4: /* unsigned higher */
634 cond = (cpsr_c == 1) && (cpsr_z == 0);
635 break;
636
637 case 0x5: /* signed greater / equal */
638 cond = (cpsr_n == cpsr_v);
639 break;
640
641 case 0x6: /* signed greater */
642 cond = (cpsr_z == 0) && (cpsr_n == cpsr_v);
643 break;
644
645 case 0x7: /* always */
646 cond = 1;
647 break;
648 };
649
650 if (cond == p1) {
651 pr_debug("Conditional abort fixup, PC=%08x, base=%d, cond=%d\n",
652 (unsigned int) regs->ARM_pc, base_cond, cond);
653 regs->ARM_pc += 2;
654 return 1;
655 }
656 return 0;
657}
658#endif
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/*
661 * Dispatch a data abort to the relevant handler.
662 */
Russell King7ab3f8d2007-03-02 15:01:36 +0000663asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
665{
Russell Kingc88d6aa2009-09-20 12:41:58 +0100666 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
Russell Kingcfb08102005-06-30 11:06:49 +0100667 struct siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
670 if (emulate_domain_manager_data_abort(fsr, addr))
671 return;
672#endif
673
Stepan Moskovchenko00da0742011-07-08 14:06:44 -0700674#ifdef CONFIG_MSM_KRAIT_TBB_ABORT_HANDLER
675 if (krait_tbb_fixup(fsr, regs))
676 return;
677#endif
678
Russell Kingdf297bf2009-09-20 13:18:47 +0100679 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return;
681
682 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
683 inf->name, fsr, addr);
Russell Kingcfb08102005-06-30 11:06:49 +0100684
685 info.si_signo = inf->sig;
686 info.si_errno = 0;
687 info.si_code = inf->code;
688 info.si_addr = (void __user *)addr;
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700689 arm_notify_die("", regs, &info, fsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Will Deacon3a4b5dc2010-09-03 10:39:59 +0100692void __init
693hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
694 int sig, int code, const char *name)
695{
696 if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
697 BUG();
698
699 ifsr_info[nr].fn = fn;
700 ifsr_info[nr].sig = sig;
701 ifsr_info[nr].code = code;
702 ifsr_info[nr].name = name;
703}
704
Russell King7ab3f8d2007-03-02 15:01:36 +0000705asmlinkage void __exception
Kirill A. Shutemov4fb28472009-09-25 13:39:47 +0100706do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100708 const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
709 struct siginfo info;
710
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
712 if (emulate_domain_manager_prefetch_abort(ifsr, addr))
713 return;
714#endif
715
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100716 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
717 return;
718
719 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
720 inf->name, ifsr, addr);
721
722 info.si_signo = inf->sig;
723 info.si_errno = 0;
724 info.si_code = inf->code;
725 info.si_addr = (void __user *)addr;
726 arm_notify_die("", regs, &info, ifsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Catalin Marinasf7b81562011-11-22 17:30:31 +0000729#ifndef CONFIG_ARM_LPAE
Kirill A. Shutemov993bf4e2010-07-22 13:23:25 +0100730static int __init exceptions_init(void)
731{
732 if (cpu_architecture() >= CPU_ARCH_ARMv6) {
733 hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
734 "I-cache maintenance fault");
735 }
736
Kirill A. Shutemovb8ab5392010-07-26 11:20:41 +0100737 if (cpu_architecture() >= CPU_ARCH_ARMv7) {
738 /*
739 * TODO: Access flag faults introduced in ARMv6K.
740 * Runtime check for 'K' extension is needed
741 */
742 hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
743 "section access flag fault");
744 hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
745 "section access flag fault");
746 }
747
Kirill A. Shutemov993bf4e2010-07-22 13:23:25 +0100748 return 0;
749}
750
751arch_initcall(exceptions_init);
Catalin Marinasf7b81562011-11-22 17:30:31 +0000752#endif