blob: 19ef5a61a41363119d87bb0f20146eba018cc8bc [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
Pushkar Joshiabbb2662012-09-07 14:22:58 -070040#define CREATE_TRACE_POINTS
41#include <trace/events/exception.h>
42
Catalin Marinas09529f72009-07-24 12:34:55 +010043#ifdef CONFIG_MMU
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -050044
45#ifdef CONFIG_KPROBES
46static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
47{
48 int ret = 0;
49
50 if (!user_mode(regs)) {
51 /* kprobe_running() needs smp_processor_id() */
52 preempt_disable();
53 if (kprobe_running() && kprobe_fault_handler(regs, fsr))
54 ret = 1;
55 preempt_enable();
56 }
57
58 return ret;
59}
60#else
61static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
62{
63 return 0;
64}
65#endif
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * This is useful to dump out the page tables associated with
69 * 'addr' in mm 'mm'.
70 */
71void show_pte(struct mm_struct *mm, unsigned long addr)
72{
73 pgd_t *pgd;
74
75 if (!mm)
76 mm = &init_mm;
77
78 printk(KERN_ALERT "pgd = %p\n", mm->pgd);
79 pgd = pgd_offset(mm, addr);
Will Deacon29a38192011-02-15 14:31:37 +010080 printk(KERN_ALERT "[%08lx] *pgd=%08llx",
81 addr, (long long)pgd_val(*pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 do {
Russell King516295e2010-11-21 16:27:49 +000084 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 pmd_t *pmd;
86 pte_t *pte;
87
88 if (pgd_none(*pgd))
89 break;
90
91 if (pgd_bad(*pgd)) {
92 printk("(bad)");
93 break;
94 }
95
Russell King516295e2010-11-21 16:27:49 +000096 pud = pud_offset(pgd, addr);
97 if (PTRS_PER_PUD != 1)
Catalin Marinas140d5dc2011-07-13 16:32:58 +010098 printk(", *pud=%08llx", (long long)pud_val(*pud));
Russell King516295e2010-11-21 16:27:49 +000099
100 if (pud_none(*pud))
101 break;
102
103 if (pud_bad(*pud)) {
104 printk("(bad)");
105 break;
106 }
107
108 pmd = pmd_offset(pud, addr);
Nicolas Pitreda46c792008-09-30 16:10:11 +0100109 if (PTRS_PER_PMD != 1)
Will Deacon29a38192011-02-15 14:31:37 +0100110 printk(", *pmd=%08llx", (long long)pmd_val(*pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 if (pmd_none(*pmd))
113 break;
114
115 if (pmd_bad(*pmd)) {
116 printk("(bad)");
117 break;
118 }
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /* We must not map this if we have highmem enabled */
Nicolas Pitre252d4c22008-09-11 11:52:02 -0400121 if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
122 break;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 pte = pte_offset_map(pmd, addr);
Will Deacon29a38192011-02-15 14:31:37 +0100125 printk(", *pte=%08llx", (long long)pte_val(*pte));
Catalin Marinasf7b81562011-11-22 17:30:31 +0000126#ifndef CONFIG_ARM_LPAE
Will Deacon29a38192011-02-15 14:31:37 +0100127 printk(", *ppte=%08llx",
128 (long long)pte_val(pte[PTE_HWTABLE_PTRS]));
Catalin Marinasf7b81562011-11-22 17:30:31 +0000129#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 pte_unmap(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } while(0);
132
133 printk("\n");
134}
Catalin Marinas09529f72009-07-24 12:34:55 +0100135#else /* CONFIG_MMU */
136void show_pte(struct mm_struct *mm, unsigned long addr)
137{ }
138#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/*
141 * Oops. The kernel tried to access some page that wasn't present.
142 */
143static void
144__do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
145 struct pt_regs *regs)
146{
147 /*
148 * Are we prepared to handle this kernel fault?
149 */
150 if (fixup_exception(regs))
151 return;
152
153 /*
154 * No handler, we'll have to terminate things with extreme prejudice.
155 */
156 bust_spinlocks(1);
157 printk(KERN_ALERT
158 "Unable to handle kernel %s at virtual address %08lx\n",
159 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
160 "paging request", addr);
161
162 show_pte(mm, addr);
163 die("Oops", regs, fsr);
164 bust_spinlocks(0);
165 do_exit(SIGKILL);
166}
167
168/*
169 * Something tried to access memory that isn't in our memory map..
170 * User mode accesses just cause a SIGSEGV
171 */
172static void
173__do_user_fault(struct task_struct *tsk, unsigned long addr,
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700174 unsigned int fsr, unsigned int sig, int code,
175 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct siginfo si;
178
Pushkar Joshiabbb2662012-09-07 14:22:58 -0700179 trace_user_fault(tsk, addr, fsr);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#ifdef CONFIG_DEBUG_USER
Javi Merinof5274c22012-02-06 15:45:36 +0100182 if (((user_debug & UDBG_SEGV) && (sig == SIGSEGV)) ||
183 ((user_debug & UDBG_BUS) && (sig == SIGBUS))) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700184 printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
185 tsk->comm, sig, addr, fsr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 show_pte(tsk->mm, addr);
187 show_regs(regs);
188 }
189#endif
190
191 tsk->thread.address = addr;
192 tsk->thread.error_code = fsr;
193 tsk->thread.trap_no = 14;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700194 si.si_signo = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 si.si_errno = 0;
196 si.si_code = code;
197 si.si_addr = (void __user *)addr;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700198 force_sig_info(sig, &si, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Russell Kinge5beac32006-09-27 16:13:48 +0100201void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Russell Kinge5beac32006-09-27 16:13:48 +0100203 struct task_struct *tsk = current;
204 struct mm_struct *mm = tsk->active_mm;
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /*
207 * If we are in kernel mode at this point, we
208 * have no context to handle this fault with.
209 */
210 if (user_mode(regs))
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700211 __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 else
213 __do_kernel_fault(mm, addr, fsr, regs);
214}
215
Catalin Marinas09529f72009-07-24 12:34:55 +0100216#ifdef CONFIG_MMU
Nick Piggin5c72fc52007-07-20 09:21:06 +0200217#define VM_FAULT_BADMAP 0x010000
218#define VM_FAULT_BADACCESS 0x020000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Russell Kingd374bf12009-09-20 12:53:01 +0100220/*
221 * Check that the permissions on the VMA allow for the fault which occurred.
222 * If we encountered a write fault, we must have write permission, otherwise
223 * we allow any permission.
224 */
225static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
226{
227 unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
228
229 if (fsr & FSR_WRITE)
230 mask = VM_WRITE;
Russell Kingdf297bf2009-09-20 13:18:47 +0100231 if (fsr & FSR_LNX_PF)
232 mask = VM_EXEC;
Russell Kingd374bf12009-09-20 12:53:01 +0100233
234 return vma->vm_flags & mask ? false : true;
235}
236
237static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238__do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
Kautuk Consul8878a532011-11-27 17:49:50 +0100239 unsigned int flags, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 struct vm_area_struct *vma;
Russell Kingd374bf12009-09-20 12:53:01 +0100242 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 vma = find_vma(mm, addr);
245 fault = VM_FAULT_BADMAP;
Russell Kingd374bf12009-09-20 12:53:01 +0100246 if (unlikely(!vma))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto out;
Russell Kingd374bf12009-09-20 12:53:01 +0100248 if (unlikely(vma->vm_start > addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 goto check_stack;
250
251 /*
252 * Ok, we have a good vm_area for this
253 * memory access, so we can handle it.
254 */
255good_area:
Russell Kingd374bf12009-09-20 12:53:01 +0100256 if (access_error(fsr, vma)) {
257 fault = VM_FAULT_BADACCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto out;
Russell Kingd374bf12009-09-20 12:53:01 +0100259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Kautuk Consul8878a532011-11-27 17:49:50 +0100261 return handle_mm_fault(mm, vma, addr & PAGE_MASK, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263check_stack:
Russell King9b61a4d2012-05-16 15:19:20 +0100264 /* Don't allow expansion below FIRST_USER_ADDRESS */
265 if (vma->vm_flags & VM_GROWSDOWN &&
266 addr >= FIRST_USER_ADDRESS && !expand_stack(vma, addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 goto good_area;
268out:
269 return fault;
270}
271
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500272static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
274{
275 struct task_struct *tsk;
276 struct mm_struct *mm;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700277 int fault, sig, code;
Kautuk Consul8878a532011-11-27 17:49:50 +0100278 int write = fsr & FSR_WRITE;
279 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
280 (write ? FAULT_FLAG_WRITE : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Nicolas Pitre25ce1dd2007-12-03 15:21:57 -0500282 if (notify_page_fault(regs, fsr))
283 return 0;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 tsk = current;
286 mm = tsk->mm;
287
Russell King02fe2842011-06-25 11:44:06 +0100288 /* Enable interrupts if they were enabled in the parent context. */
289 if (interrupts_enabled(regs))
290 local_irq_enable();
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /*
293 * If we're in an interrupt or have no user
294 * context, we must not take the fault..
295 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800296 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto no_context;
298
Russell King840ff6a2005-09-20 17:52:13 +0100299 /*
300 * As per x86, we may deadlock here. However, since the kernel only
301 * validly references user space from well defined areas of the code,
302 * we can bug out early if this is from code which shouldn't.
303 */
304 if (!down_read_trylock(&mm->mmap_sem)) {
305 if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
306 goto no_context;
Kautuk Consul8878a532011-11-27 17:49:50 +0100307retry:
Russell King840ff6a2005-09-20 17:52:13 +0100308 down_read(&mm->mmap_sem);
Russell Kingbf456992009-09-20 12:52:19 +0100309 } else {
310 /*
311 * The above down_read_trylock() might have succeeded in
312 * which case, we'll have missed the might_sleep() from
313 * down_read()
314 */
315 might_sleep();
Imre Deak1d212712009-10-05 13:40:44 +0100316#ifdef CONFIG_DEBUG_VM
317 if (!user_mode(regs) &&
318 !search_exception_tables(regs->ARM_pc))
319 goto no_context;
320#endif
Russell King840ff6a2005-09-20 17:52:13 +0100321 }
322
Kautuk Consul8878a532011-11-27 17:49:50 +0100323 fault = __do_page_fault(mm, addr, fsr, flags, tsk);
324
325 /* If we need to retry but a fatal signal is pending, handle the
326 * signal first. We do not need to release the mmap_sem because
327 * it would already be released in __lock_page_or_retry in
328 * mm/filemap.c. */
329 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
330 return 0;
331
332 /*
333 * Major/minor page fault accounting is only done on the
334 * initial attempt. If we go through a retry, it is extremely
335 * likely that the page will be found in page cache at that point.
336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200338 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
Kautuk Consuldff2aa72012-04-02 18:19:49 +0100339 if (!(fault & VM_FAULT_ERROR) && flags & FAULT_FLAG_ALLOW_RETRY) {
Kautuk Consul8878a532011-11-27 17:49:50 +0100340 if (fault & VM_FAULT_MAJOR) {
341 tsk->maj_flt++;
342 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
343 regs, addr);
344 } else {
345 tsk->min_flt++;
346 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
347 regs, addr);
348 }
349 if (fault & VM_FAULT_RETRY) {
350 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
351 * of starvation. */
352 flags &= ~FAULT_FLAG_ALLOW_RETRY;
353 goto retry;
354 }
355 }
356
357 up_read(&mm->mmap_sem);
Jamie Iles7ada1892010-02-02 20:24:58 +0100358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
Russell Kingff2afb92005-08-04 14:17:33 +0100360 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Nick Piggin5c72fc52007-07-20 09:21:06 +0200362 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return 0;
364
Russell Kingb42c6342009-09-20 12:47:40 +0100365 if (fault & VM_FAULT_OOM) {
366 /*
367 * We ran out of memory, call the OOM killer, and return to
368 * userspace (which will retry the fault, or kill us if we
369 * got oom-killed)
370 */
371 pagefault_out_of_memory();
372 return 0;
373 }
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * If we are in kernel mode at this point, we
377 * have no context to handle this fault with.
378 */
379 if (!user_mode(regs))
380 goto no_context;
381
Nick Piggin83c54072007-07-19 01:47:05 -0700382 if (fault & VM_FAULT_SIGBUS) {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700383 /*
384 * We had some memory, but were unable to
385 * successfully fix up this page fault.
386 */
387 sig = SIGBUS;
388 code = BUS_ADRERR;
Nick Piggin83c54072007-07-19 01:47:05 -0700389 } else {
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700390 /*
391 * Something tried to access memory that
392 * isn't in our memory map..
393 */
394 sig = SIGSEGV;
395 code = fault == VM_FAULT_BADACCESS ?
396 SEGV_ACCERR : SEGV_MAPERR;
akpm@osdl.org2d137c22005-04-16 15:23:55 -0700397 }
398
399 __do_user_fault(tsk, addr, fsr, sig, code, regs);
400 return 0;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402no_context:
403 __do_kernel_fault(mm, addr, fsr, regs);
404 return 0;
405}
Catalin Marinas09529f72009-07-24 12:34:55 +0100406#else /* CONFIG_MMU */
407static int
408do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
409{
410 return 0;
411}
412#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414/*
415 * First Level Translation Fault Handler
416 *
417 * We enter here because the first level page table doesn't contain
418 * a valid entry for the address.
419 *
420 * If the address is in kernel space (>= TASK_SIZE), then we are
421 * probably faulting in the vmalloc() area.
422 *
423 * If the init_task's first level page tables contains the relevant
424 * entry, we copy the it to this task. If not, we send the process
425 * a signal, fixup the exception, or oops the kernel.
426 *
427 * NOTE! We MUST NOT take any locks for this case. We may be in an
428 * interrupt or a critical region, and should only copy the information
429 * from the master page table, nothing more.
430 */
Catalin Marinas09529f72009-07-24 12:34:55 +0100431#ifdef CONFIG_MMU
Nicolas Pitre785d3cd2007-12-03 15:27:56 -0500432static int __kprobes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433do_translation_fault(unsigned long addr, unsigned int fsr,
434 struct pt_regs *regs)
435{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned int index;
437 pgd_t *pgd, *pgd_k;
Russell King516295e2010-11-21 16:27:49 +0000438 pud_t *pud, *pud_k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 pmd_t *pmd, *pmd_k;
440
441 if (addr < TASK_SIZE)
442 return do_page_fault(addr, fsr, regs);
443
Anfei5e27fb72010-06-08 15:16:49 +0100444 if (user_mode(regs))
445 goto bad_area;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 index = pgd_index(addr);
448
449 /*
450 * FIXME: CP15 C1 is write only on ARMv3 architectures.
451 */
452 pgd = cpu_get_pgd() + index;
453 pgd_k = init_mm.pgd + index;
454
455 if (pgd_none(*pgd_k))
456 goto bad_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!pgd_present(*pgd))
458 set_pgd(pgd, *pgd_k);
459
Russell King516295e2010-11-21 16:27:49 +0000460 pud = pud_offset(pgd, addr);
461 pud_k = pud_offset(pgd_k, addr);
462
463 if (pud_none(*pud_k))
464 goto bad_area;
465 if (!pud_present(*pud))
466 set_pud(pud, *pud_k);
467
468 pmd = pmd_offset(pud, addr);
469 pmd_k = pmd_offset(pud_k, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Catalin Marinasf7b81562011-11-22 17:30:31 +0000471#ifdef CONFIG_ARM_LPAE
472 /*
473 * Only one hardware entry per PMD with LPAE.
474 */
475 index = 0;
476#else
Kirill A. Shutemov33a9c412010-07-22 13:20:22 +0100477 /*
478 * On ARM one Linux PGD entry contains two hardware entries (see page
479 * tables layout in pgtable.h). We normally guarantee that we always
480 * fill both L1 entries. But create_mapping() doesn't follow the rule.
481 * It can create inidividual L1 entries, so here we have to call
482 * pmd_none() check for the entry really corresponded to address, not
483 * for the first of pair.
484 */
485 index = (addr >> SECTION_SHIFT) & 1;
Catalin Marinasf7b81562011-11-22 17:30:31 +0000486#endif
Kirill A. Shutemov33a9c412010-07-22 13:20:22 +0100487 if (pmd_none(pmd_k[index]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto bad_area;
489
490 copy_pmd(pmd, pmd_k);
491 return 0;
492
493bad_area:
Russell Kinge5beac32006-09-27 16:13:48 +0100494 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
496}
Catalin Marinas09529f72009-07-24 12:34:55 +0100497#else /* CONFIG_MMU */
498static int
499do_translation_fault(unsigned long addr, unsigned int fsr,
500 struct pt_regs *regs)
501{
502 return 0;
503}
504#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506/*
507 * Some section permission faults need to be handled gracefully.
508 * They can happen due to a __{get,put}_user during an oops.
509 */
510static int
511do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
512{
Russell Kinge5beac32006-09-27 16:13:48 +0100513 do_bad_area(addr, fsr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
515}
516
517/*
518 * This abort handler always returns "fault".
519 */
520static int
521do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
522{
523 return 1;
524}
525
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
527#define __str(x) #x
528#define MRC(x, v1, v2, v4, v5, v6) do { \
529 unsigned int __##x; \
530 asm("mrc " __str(v1) ", " __str(v2) ", %0, " __str(v4) ", " \
531 __str(v5) ", " __str(v6) "\n" \
532 : "=r" (__##x)); \
533 pr_info("%s: %s = 0x%.8x\n", __func__, #x, __##x); \
534} while(0)
535
536#define MSM_TCSR_SPARE2 (MSM_TCSR_BASE + 0x60)
537
538#endif
539
Steve Mucklef132c6c2012-06-06 18:30:57 -0700540int
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541do_imprecise_ext(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
542{
543#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
544 MRC(ADFSR, p15, 0, c5, c1, 0);
545 MRC(DFSR, p15, 0, c5, c0, 0);
546 MRC(ACTLR, p15, 0, c1, c0, 1);
547 MRC(EFSR, p15, 7, c15, c0, 1);
548 MRC(L2SR, p15, 3, c15, c1, 0);
549 MRC(L2CR0, p15, 3, c15, c0, 1);
550 MRC(L2CPUESR, p15, 3, c15, c1, 1);
551 MRC(L2CPUCR, p15, 3, c15, c0, 2);
552 MRC(SPESR, p15, 1, c9, c7, 0);
553 MRC(SPCR, p15, 0, c9, c7, 0);
554 MRC(DMACHSR, p15, 1, c11, c0, 0);
555 MRC(DMACHESR, p15, 1, c11, c0, 1);
556 MRC(DMACHCR, p15, 0, c11, c0, 2);
557
558 /* clear out EFSR and ADFSR after fault */
559 asm volatile ("mcr p15, 7, %0, c15, c0, 1\n\t"
560 "mcr p15, 0, %0, c5, c1, 0"
561 : : "r" (0));
562#endif
563#if defined(CONFIG_ARCH_MSM_SCORPION) && !defined(CONFIG_MSM_SMP)
564 pr_info("%s: TCSR_SPARE2 = 0x%.8x\n", __func__, readl(MSM_TCSR_SPARE2));
565#endif
566 return 1;
567}
568
Catalin Marinas136848d2011-11-22 17:30:28 +0000569struct fsr_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
571 int sig;
Russell Kingcfb08102005-06-30 11:06:49 +0100572 int code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574};
575
Catalin Marinas136848d2011-11-22 17:30:28 +0000576/* FSR definition */
Catalin Marinasf7b81562011-11-22 17:30:31 +0000577#ifdef CONFIG_ARM_LPAE
578#include "fsr-3level.c"
579#else
Catalin Marinas136848d2011-11-22 17:30:28 +0000580#include "fsr-2level.c"
Catalin Marinasf7b81562011-11-22 17:30:31 +0000581#endif
Catalin Marinas136848d2011-11-22 17:30:28 +0000582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583void __init
584hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100585 int sig, int code, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Kirill A. Shutemov6338a6a2010-07-22 13:18:19 +0100587 if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
588 BUG();
589
590 fsr_info[nr].fn = fn;
591 fsr_info[nr].sig = sig;
592 fsr_info[nr].code = code;
593 fsr_info[nr].name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Stepan Moskovchenko00da0742011-07-08 14:06:44 -0700596#ifdef CONFIG_MSM_KRAIT_TBB_ABORT_HANDLER
597static int krait_tbb_fixup(unsigned int fsr, struct pt_regs *regs)
598{
599 int base_cond, cond = 0;
600 unsigned int p1, cpsr_z, cpsr_c, cpsr_n, cpsr_v;
601
602 if ((read_cpuid_id() & 0xFFFFFFFC) != 0x510F04D0)
603 return 0;
604
605 if (!thumb_mode(regs))
606 return 0;
607
608 /* If ITSTATE is 0, return quickly */
609 if ((regs->ARM_cpsr & PSR_IT_MASK) == 0)
610 return 0;
611
612 cpsr_n = (regs->ARM_cpsr & PSR_N_BIT) ? 1 : 0;
613 cpsr_z = (regs->ARM_cpsr & PSR_Z_BIT) ? 1 : 0;
614 cpsr_c = (regs->ARM_cpsr & PSR_C_BIT) ? 1 : 0;
615 cpsr_v = (regs->ARM_cpsr & PSR_V_BIT) ? 1 : 0;
616
617 p1 = (regs->ARM_cpsr & BIT(12)) ? 1 : 0;
618
619 base_cond = (regs->ARM_cpsr >> 13) & 0x07;
620
621 switch (base_cond) {
622 case 0x0: /* equal */
623 cond = cpsr_z;
624 break;
625
626 case 0x1: /* carry set */
627 cond = cpsr_c;
628 break;
629
630 case 0x2: /* minus / negative */
631 cond = cpsr_n;
632 break;
633
634 case 0x3: /* overflow */
635 cond = cpsr_v;
636 break;
637
638 case 0x4: /* unsigned higher */
639 cond = (cpsr_c == 1) && (cpsr_z == 0);
640 break;
641
642 case 0x5: /* signed greater / equal */
643 cond = (cpsr_n == cpsr_v);
644 break;
645
646 case 0x6: /* signed greater */
647 cond = (cpsr_z == 0) && (cpsr_n == cpsr_v);
648 break;
649
650 case 0x7: /* always */
651 cond = 1;
652 break;
653 };
654
655 if (cond == p1) {
656 pr_debug("Conditional abort fixup, PC=%08x, base=%d, cond=%d\n",
657 (unsigned int) regs->ARM_pc, base_cond, cond);
658 regs->ARM_pc += 2;
659 return 1;
660 }
661 return 0;
662}
663#endif
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*
666 * Dispatch a data abort to the relevant handler.
667 */
Russell King7ab3f8d2007-03-02 15:01:36 +0000668asmlinkage void __exception
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
670{
Russell Kingc88d6aa2009-09-20 12:41:58 +0100671 const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
Russell Kingcfb08102005-06-30 11:06:49 +0100672 struct siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
675 if (emulate_domain_manager_data_abort(fsr, addr))
676 return;
677#endif
678
Stepan Moskovchenko00da0742011-07-08 14:06:44 -0700679#ifdef CONFIG_MSM_KRAIT_TBB_ABORT_HANDLER
680 if (krait_tbb_fixup(fsr, regs))
681 return;
682#endif
683
Russell Kingdf297bf2009-09-20 13:18:47 +0100684 if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return;
686
687 printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
688 inf->name, fsr, addr);
Russell Kingcfb08102005-06-30 11:06:49 +0100689
690 info.si_signo = inf->sig;
691 info.si_errno = 0;
692 info.si_code = inf->code;
693 info.si_addr = (void __user *)addr;
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700694 arm_notify_die("", regs, &info, fsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Will Deacon3a4b5dc2010-09-03 10:39:59 +0100697void __init
698hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
699 int sig, int code, const char *name)
700{
701 if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
702 BUG();
703
704 ifsr_info[nr].fn = fn;
705 ifsr_info[nr].sig = sig;
706 ifsr_info[nr].code = code;
707 ifsr_info[nr].name = name;
708}
709
Russell King7ab3f8d2007-03-02 15:01:36 +0000710asmlinkage void __exception
Kirill A. Shutemov4fb28472009-09-25 13:39:47 +0100711do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100713 const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
714 struct siginfo info;
715
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716#ifdef CONFIG_EMULATE_DOMAIN_MANAGER_V7
717 if (emulate_domain_manager_prefetch_abort(ifsr, addr))
718 return;
719#endif
720
Kirill A. Shutemovd25ef8b2009-09-25 13:40:49 +0100721 if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
722 return;
723
724 printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
725 inf->name, ifsr, addr);
726
727 info.si_signo = inf->sig;
728 info.si_errno = 0;
729 info.si_code = inf->code;
730 info.si_addr = (void __user *)addr;
731 arm_notify_die("", regs, &info, ifsr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Catalin Marinasf7b81562011-11-22 17:30:31 +0000734#ifndef CONFIG_ARM_LPAE
Kirill A. Shutemov993bf4e2010-07-22 13:23:25 +0100735static int __init exceptions_init(void)
736{
737 if (cpu_architecture() >= CPU_ARCH_ARMv6) {
738 hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
739 "I-cache maintenance fault");
740 }
741
Kirill A. Shutemovb8ab5392010-07-26 11:20:41 +0100742 if (cpu_architecture() >= CPU_ARCH_ARMv7) {
743 /*
744 * TODO: Access flag faults introduced in ARMv6K.
745 * Runtime check for 'K' extension is needed
746 */
747 hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
748 "section access flag fault");
749 hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
750 "section access flag fault");
751 }
752
Kirill A. Shutemov993bf4e2010-07-22 13:23:25 +0100753 return 0;
754}
755
756arch_initcall(exceptions_init);
Catalin Marinasf7b81562011-11-22 17:30:31 +0000757#endif