blob: 89c6763d5e7e678b0623ef9ecd1d464c3a256ebb [file] [log] [blame]
Catalin Marinas1d18c472012-03-05 11:49:27 +00001/*
2 * Based on arch/arm/mm/fault.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 * Copyright (C) 1995-2004 Russell King
6 * Copyright (C) 2012 ARM Ltd.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/module.h>
22#include <linux/signal.h>
23#include <linux/mm.h>
24#include <linux/hardirq.h>
25#include <linux/init.h>
26#include <linux/kprobes.h>
27#include <linux/uaccess.h>
28#include <linux/page-flags.h>
29#include <linux/sched.h>
30#include <linux/highmem.h>
31#include <linux/perf_event.h>
32
33#include <asm/exception.h>
34#include <asm/debug-monitors.h>
Catalin Marinas91413002014-04-06 23:04:12 +010035#include <asm/esr.h>
Catalin Marinas1d18c472012-03-05 11:49:27 +000036#include <asm/system_misc.h>
37#include <asm/pgtable.h>
38#include <asm/tlbflush.h>
39
Catalin Marinas3495386b2012-10-24 16:34:02 +010040static const char *fault_name(unsigned int esr);
41
Catalin Marinas1d18c472012-03-05 11:49:27 +000042/*
43 * Dump out the page tables associated with 'addr' in mm 'mm'.
44 */
45void show_pte(struct mm_struct *mm, unsigned long addr)
46{
47 pgd_t *pgd;
48
49 if (!mm)
50 mm = &init_mm;
51
52 pr_alert("pgd = %p\n", mm->pgd);
53 pgd = pgd_offset(mm, addr);
54 pr_alert("[%08lx] *pgd=%016llx", addr, pgd_val(*pgd));
55
56 do {
57 pud_t *pud;
58 pmd_t *pmd;
59 pte_t *pte;
60
Steve Capper4339e3f32013-04-19 15:49:31 +010061 if (pgd_none(*pgd) || pgd_bad(*pgd))
Catalin Marinas1d18c472012-03-05 11:49:27 +000062 break;
63
64 pud = pud_offset(pgd, addr);
Steve Capper4339e3f32013-04-19 15:49:31 +010065 if (pud_none(*pud) || pud_bad(*pud))
Catalin Marinas1d18c472012-03-05 11:49:27 +000066 break;
67
68 pmd = pmd_offset(pud, addr);
69 printk(", *pmd=%016llx", pmd_val(*pmd));
Steve Capper4339e3f32013-04-19 15:49:31 +010070 if (pmd_none(*pmd) || pmd_bad(*pmd))
Catalin Marinas1d18c472012-03-05 11:49:27 +000071 break;
72
73 pte = pte_offset_map(pmd, addr);
74 printk(", *pte=%016llx", pte_val(*pte));
75 pte_unmap(pte);
76 } while(0);
77
78 printk("\n");
79}
80
81/*
82 * The kernel tried to access some page that wasn't present.
83 */
84static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
85 unsigned int esr, struct pt_regs *regs)
86{
87 /*
88 * Are we prepared to handle this kernel fault?
89 */
90 if (fixup_exception(regs))
91 return;
92
93 /*
94 * No handler, we'll have to terminate things with extreme prejudice.
95 */
96 bust_spinlocks(1);
97 pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
98 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
99 "paging request", addr);
100
101 show_pte(mm, addr);
102 die("Oops", regs, esr);
103 bust_spinlocks(0);
104 do_exit(SIGKILL);
105}
106
107/*
108 * Something tried to access memory that isn't in our memory map. User mode
109 * accesses just cause a SIGSEGV
110 */
111static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
112 unsigned int esr, unsigned int sig, int code,
113 struct pt_regs *regs)
114{
115 struct siginfo si;
116
Catalin Marinas953dbbe2013-05-21 12:16:56 +0100117 if (show_unhandled_signals && unhandled_signal(tsk, sig) &&
118 printk_ratelimit()) {
Catalin Marinas3495386b2012-10-24 16:34:02 +0100119 pr_info("%s[%d]: unhandled %s (%d) at 0x%08lx, esr 0x%03x\n",
120 tsk->comm, task_pid_nr(tsk), fault_name(esr), sig,
121 addr, esr);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000122 show_pte(tsk->mm, addr);
123 show_regs(regs);
124 }
125
126 tsk->thread.fault_address = addr;
Catalin Marinas91413002014-04-06 23:04:12 +0100127 tsk->thread.fault_code = esr;
Catalin Marinas1d18c472012-03-05 11:49:27 +0000128 si.si_signo = sig;
129 si.si_errno = 0;
130 si.si_code = code;
131 si.si_addr = (void __user *)addr;
132 force_sig_info(sig, &si, tsk);
133}
134
Catalin Marinas59f67e12013-09-16 15:18:28 +0100135static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
Catalin Marinas1d18c472012-03-05 11:49:27 +0000136{
137 struct task_struct *tsk = current;
138 struct mm_struct *mm = tsk->active_mm;
139
140 /*
141 * If we are in kernel mode at this point, we have no context to
142 * handle this fault with.
143 */
144 if (user_mode(regs))
145 __do_user_fault(tsk, addr, esr, SIGSEGV, SEGV_MAPERR, regs);
146 else
147 __do_kernel_fault(mm, addr, esr, regs);
148}
149
150#define VM_FAULT_BADMAP 0x010000
151#define VM_FAULT_BADACCESS 0x020000
152
Catalin Marinas1d18c472012-03-05 11:49:27 +0000153#define ESR_LNX_EXEC (1 << 24)
154
Catalin Marinas1d18c472012-03-05 11:49:27 +0000155static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
Will Deacondb6f4102013-07-19 15:37:12 +0100156 unsigned int mm_flags, unsigned long vm_flags,
Catalin Marinas1d18c472012-03-05 11:49:27 +0000157 struct task_struct *tsk)
158{
159 struct vm_area_struct *vma;
160 int fault;
161
162 vma = find_vma(mm, addr);
163 fault = VM_FAULT_BADMAP;
164 if (unlikely(!vma))
165 goto out;
166 if (unlikely(vma->vm_start > addr))
167 goto check_stack;
168
169 /*
170 * Ok, we have a good vm_area for this memory access, so we can handle
171 * it.
172 */
173good_area:
Will Deacondb6f4102013-07-19 15:37:12 +0100174 /*
175 * Check that the permissions on the VMA allow for the fault which
Catalin Marinasbc07c2c2014-04-03 16:17:32 +0100176 * occurred.
Will Deacondb6f4102013-07-19 15:37:12 +0100177 */
178 if (!(vma->vm_flags & vm_flags)) {
Catalin Marinas1d18c472012-03-05 11:49:27 +0000179 fault = VM_FAULT_BADACCESS;
180 goto out;
181 }
182
Will Deacondb6f4102013-07-19 15:37:12 +0100183 return handle_mm_fault(mm, vma, addr & PAGE_MASK, mm_flags);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000184
185check_stack:
186 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
187 goto good_area;
188out:
189 return fault;
190}
191
192static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
193 struct pt_regs *regs)
194{
195 struct task_struct *tsk;
196 struct mm_struct *mm;
197 int fault, sig, code;
Catalin Marinasbc07c2c2014-04-03 16:17:32 +0100198 unsigned long vm_flags = VM_READ | VM_WRITE;
Will Deacondb6f4102013-07-19 15:37:12 +0100199 unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
200
Catalin Marinas1d18c472012-03-05 11:49:27 +0000201 tsk = current;
202 mm = tsk->mm;
203
204 /* Enable interrupts if they were enabled in the parent context. */
205 if (interrupts_enabled(regs))
206 local_irq_enable();
207
208 /*
209 * If we're in an interrupt or have no user context, we must not take
210 * the fault.
211 */
212 if (in_atomic() || !mm)
213 goto no_context;
214
Johannes Weiner759496b2013-09-12 15:13:39 -0700215 if (user_mode(regs))
216 mm_flags |= FAULT_FLAG_USER;
217
218 if (esr & ESR_LNX_EXEC) {
219 vm_flags = VM_EXEC;
Catalin Marinas91413002014-04-06 23:04:12 +0100220 } else if ((esr & ESR_EL1_WRITE) && !(esr & ESR_EL1_CM)) {
Johannes Weiner759496b2013-09-12 15:13:39 -0700221 vm_flags = VM_WRITE;
222 mm_flags |= FAULT_FLAG_WRITE;
223 }
224
Catalin Marinas1d18c472012-03-05 11:49:27 +0000225 /*
226 * As per x86, we may deadlock here. However, since the kernel only
227 * validly references user space from well defined areas of the code,
228 * we can bug out early if this is from code which shouldn't.
229 */
230 if (!down_read_trylock(&mm->mmap_sem)) {
231 if (!user_mode(regs) && !search_exception_tables(regs->pc))
232 goto no_context;
233retry:
234 down_read(&mm->mmap_sem);
235 } else {
236 /*
237 * The above down_read_trylock() might have succeeded in which
238 * case, we'll have missed the might_sleep() from down_read().
239 */
240 might_sleep();
241#ifdef CONFIG_DEBUG_VM
242 if (!user_mode(regs) && !search_exception_tables(regs->pc))
243 goto no_context;
244#endif
245 }
246
Will Deacondb6f4102013-07-19 15:37:12 +0100247 fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000248
249 /*
250 * If we need to retry but a fatal signal is pending, handle the
251 * signal first. We do not need to release the mmap_sem because it
252 * would already be released in __lock_page_or_retry in mm/filemap.c.
253 */
254 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
255 return 0;
256
257 /*
258 * Major/minor page fault accounting is only done on the initial
259 * attempt. If we go through a retry, it is extremely likely that the
260 * page will be found in page cache at that point.
261 */
262
263 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
Will Deacondb6f4102013-07-19 15:37:12 +0100264 if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
Catalin Marinas1d18c472012-03-05 11:49:27 +0000265 if (fault & VM_FAULT_MAJOR) {
266 tsk->maj_flt++;
267 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
268 addr);
269 } else {
270 tsk->min_flt++;
271 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
272 addr);
273 }
274 if (fault & VM_FAULT_RETRY) {
275 /*
276 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
277 * starvation.
278 */
Will Deacondb6f4102013-07-19 15:37:12 +0100279 mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
Catalin Marinas1d18c472012-03-05 11:49:27 +0000280 goto retry;
281 }
282 }
283
284 up_read(&mm->mmap_sem);
285
286 /*
287 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
288 */
289 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
290 VM_FAULT_BADACCESS))))
291 return 0;
292
Johannes Weiner87134102013-09-12 15:13:38 -0700293 /*
294 * If we are in kernel mode at this point, we have no context to
295 * handle this fault with.
296 */
297 if (!user_mode(regs))
298 goto no_context;
299
Catalin Marinas1d18c472012-03-05 11:49:27 +0000300 if (fault & VM_FAULT_OOM) {
301 /*
302 * We ran out of memory, call the OOM killer, and return to
303 * userspace (which will retry the fault, or kill us if we got
304 * oom-killed).
305 */
306 pagefault_out_of_memory();
307 return 0;
308 }
309
Catalin Marinas1d18c472012-03-05 11:49:27 +0000310 if (fault & VM_FAULT_SIGBUS) {
311 /*
312 * We had some memory, but were unable to successfully fix up
313 * this page fault.
314 */
315 sig = SIGBUS;
316 code = BUS_ADRERR;
317 } else {
318 /*
319 * Something tried to access memory that isn't in our memory
320 * map.
321 */
322 sig = SIGSEGV;
323 code = fault == VM_FAULT_BADACCESS ?
324 SEGV_ACCERR : SEGV_MAPERR;
325 }
326
327 __do_user_fault(tsk, addr, esr, sig, code, regs);
328 return 0;
329
330no_context:
331 __do_kernel_fault(mm, addr, esr, regs);
332 return 0;
333}
334
335/*
336 * First Level Translation Fault Handler
337 *
338 * We enter here because the first level page table doesn't contain a valid
339 * entry for the address.
340 *
341 * If the address is in kernel space (>= TASK_SIZE), then we are probably
342 * faulting in the vmalloc() area.
343 *
344 * If the init_task's first level page tables contains the relevant entry, we
345 * copy the it to this task. If not, we send the process a signal, fixup the
346 * exception, or oops the kernel.
347 *
348 * NOTE! We MUST NOT take any locks for this case. We may be in an interrupt
349 * or a critical region, and should only copy the information from the master
350 * page table, nothing more.
351 */
352static int __kprobes do_translation_fault(unsigned long addr,
353 unsigned int esr,
354 struct pt_regs *regs)
355{
356 if (addr < TASK_SIZE)
357 return do_page_fault(addr, esr, regs);
358
359 do_bad_area(addr, esr, regs);
360 return 0;
361}
362
363/*
Catalin Marinas1d18c472012-03-05 11:49:27 +0000364 * This abort handler always returns "fault".
365 */
366static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
367{
368 return 1;
369}
370
371static struct fault_info {
372 int (*fn)(unsigned long addr, unsigned int esr, struct pt_regs *regs);
373 int sig;
374 int code;
375 const char *name;
376} fault_info[] = {
377 { do_bad, SIGBUS, 0, "ttbr address size fault" },
378 { do_bad, SIGBUS, 0, "level 1 address size fault" },
379 { do_bad, SIGBUS, 0, "level 2 address size fault" },
380 { do_bad, SIGBUS, 0, "level 3 address size fault" },
381 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "input address range fault" },
382 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
383 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
384 { do_page_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
385 { do_bad, SIGBUS, 0, "reserved access flag fault" },
Steve Capper084bd292013-04-10 13:48:00 +0100386 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
387 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
Catalin Marinas1d18c472012-03-05 11:49:27 +0000388 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
389 { do_bad, SIGBUS, 0, "reserved permission fault" },
Steve Capper084bd292013-04-10 13:48:00 +0100390 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
391 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
Catalin Marinas1d18c472012-03-05 11:49:27 +0000392 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
393 { do_bad, SIGBUS, 0, "synchronous external abort" },
394 { do_bad, SIGBUS, 0, "asynchronous external abort" },
395 { do_bad, SIGBUS, 0, "unknown 18" },
396 { do_bad, SIGBUS, 0, "unknown 19" },
397 { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
398 { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
399 { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
400 { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
401 { do_bad, SIGBUS, 0, "synchronous parity error" },
402 { do_bad, SIGBUS, 0, "asynchronous parity error" },
403 { do_bad, SIGBUS, 0, "unknown 26" },
404 { do_bad, SIGBUS, 0, "unknown 27" },
405 { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk" },
406 { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk" },
407 { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk" },
408 { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk" },
409 { do_bad, SIGBUS, 0, "unknown 32" },
410 { do_bad, SIGBUS, BUS_ADRALN, "alignment fault" },
411 { do_bad, SIGBUS, 0, "debug event" },
412 { do_bad, SIGBUS, 0, "unknown 35" },
413 { do_bad, SIGBUS, 0, "unknown 36" },
414 { do_bad, SIGBUS, 0, "unknown 37" },
415 { do_bad, SIGBUS, 0, "unknown 38" },
416 { do_bad, SIGBUS, 0, "unknown 39" },
417 { do_bad, SIGBUS, 0, "unknown 40" },
418 { do_bad, SIGBUS, 0, "unknown 41" },
419 { do_bad, SIGBUS, 0, "unknown 42" },
420 { do_bad, SIGBUS, 0, "unknown 43" },
421 { do_bad, SIGBUS, 0, "unknown 44" },
422 { do_bad, SIGBUS, 0, "unknown 45" },
423 { do_bad, SIGBUS, 0, "unknown 46" },
424 { do_bad, SIGBUS, 0, "unknown 47" },
425 { do_bad, SIGBUS, 0, "unknown 48" },
426 { do_bad, SIGBUS, 0, "unknown 49" },
427 { do_bad, SIGBUS, 0, "unknown 50" },
428 { do_bad, SIGBUS, 0, "unknown 51" },
429 { do_bad, SIGBUS, 0, "implementation fault (lockdown abort)" },
430 { do_bad, SIGBUS, 0, "unknown 53" },
431 { do_bad, SIGBUS, 0, "unknown 54" },
432 { do_bad, SIGBUS, 0, "unknown 55" },
433 { do_bad, SIGBUS, 0, "unknown 56" },
434 { do_bad, SIGBUS, 0, "unknown 57" },
435 { do_bad, SIGBUS, 0, "implementation fault (coprocessor abort)" },
436 { do_bad, SIGBUS, 0, "unknown 59" },
437 { do_bad, SIGBUS, 0, "unknown 60" },
438 { do_bad, SIGBUS, 0, "unknown 61" },
439 { do_bad, SIGBUS, 0, "unknown 62" },
440 { do_bad, SIGBUS, 0, "unknown 63" },
441};
442
Catalin Marinas3495386b2012-10-24 16:34:02 +0100443static const char *fault_name(unsigned int esr)
444{
445 const struct fault_info *inf = fault_info + (esr & 63);
446 return inf->name;
447}
448
Catalin Marinas1d18c472012-03-05 11:49:27 +0000449/*
450 * Dispatch a data abort to the relevant handler.
451 */
452asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
453 struct pt_regs *regs)
454{
455 const struct fault_info *inf = fault_info + (esr & 63);
456 struct siginfo info;
457
458 if (!inf->fn(addr, esr, regs))
459 return;
460
461 pr_alert("Unhandled fault: %s (0x%08x) at 0x%016lx\n",
462 inf->name, esr, addr);
463
464 info.si_signo = inf->sig;
465 info.si_errno = 0;
466 info.si_code = inf->code;
467 info.si_addr = (void __user *)addr;
468 arm64_notify_die("", regs, &info, esr);
469}
470
471/*
472 * Handle stack alignment exceptions.
473 */
474asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
475 unsigned int esr,
476 struct pt_regs *regs)
477{
478 struct siginfo info;
479
480 info.si_signo = SIGBUS;
481 info.si_errno = 0;
482 info.si_code = BUS_ADRALN;
483 info.si_addr = (void __user *)addr;
484 arm64_notify_die("", regs, &info, esr);
485}
486
487static struct fault_info debug_fault_info[] = {
488 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
489 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
490 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
491 { do_bad, SIGBUS, 0, "unknown 3" },
492 { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
493 { do_bad, SIGTRAP, 0, "aarch32 vector catch" },
494 { do_bad, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
495 { do_bad, SIGBUS, 0, "unknown 7" },
496};
497
498void __init hook_debug_fault_code(int nr,
499 int (*fn)(unsigned long, unsigned int, struct pt_regs *),
500 int sig, int code, const char *name)
501{
502 BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
503
504 debug_fault_info[nr].fn = fn;
505 debug_fault_info[nr].sig = sig;
506 debug_fault_info[nr].code = code;
507 debug_fault_info[nr].name = name;
508}
509
510asmlinkage int __exception do_debug_exception(unsigned long addr,
511 unsigned int esr,
512 struct pt_regs *regs)
513{
514 const struct fault_info *inf = debug_fault_info + DBG_ESR_EVT(esr);
515 struct siginfo info;
516
517 if (!inf->fn(addr, esr, regs))
518 return 1;
519
520 pr_alert("Unhandled debug exception: %s (0x%08x) at 0x%016lx\n",
521 inf->name, esr, addr);
522
523 info.si_signo = inf->sig;
524 info.si_errno = 0;
525 info.si_code = inf->code;
526 info.si_addr = (void __user *)addr;
Catalin Marinas91413002014-04-06 23:04:12 +0100527 arm64_notify_die("", regs, &info, 0);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000528
529 return 0;
530}