]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/arm64/mm/fault.c
arm64: Convert pte handling from inline asm to using (cmp)xchg
[mirror_ubuntu-artful-kernel.git] / arch / arm64 / mm / fault.c
1 /*
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/extable.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/signal.h>
30 #include <linux/sched/debug.h>
31 #include <linux/highmem.h>
32 #include <linux/perf_event.h>
33 #include <linux/preempt.h>
34 #include <linux/hugetlb.h>
35
36 #include <asm/bug.h>
37 #include <asm/cmpxchg.h>
38 #include <asm/cpufeature.h>
39 #include <asm/exception.h>
40 #include <asm/debug-monitors.h>
41 #include <asm/esr.h>
42 #include <asm/sysreg.h>
43 #include <asm/system_misc.h>
44 #include <asm/pgtable.h>
45 #include <asm/tlbflush.h>
46
47 #include <acpi/ghes.h>
48
49 struct fault_info {
50 int (*fn)(unsigned long addr, unsigned int esr,
51 struct pt_regs *regs);
52 int sig;
53 int code;
54 const char *name;
55 };
56
57 static const struct fault_info fault_info[];
58
59 static inline const struct fault_info *esr_to_fault_info(unsigned int esr)
60 {
61 return fault_info + (esr & 63);
62 }
63
64 #ifdef CONFIG_KPROBES
65 static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
66 {
67 int ret = 0;
68
69 /* kprobe_running() needs smp_processor_id() */
70 if (!user_mode(regs)) {
71 preempt_disable();
72 if (kprobe_running() && kprobe_fault_handler(regs, esr))
73 ret = 1;
74 preempt_enable();
75 }
76
77 return ret;
78 }
79 #else
80 static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
81 {
82 return 0;
83 }
84 #endif
85
86 /*
87 * Dump out the page tables associated with 'addr' in the currently active mm.
88 */
89 void show_pte(unsigned long addr)
90 {
91 struct mm_struct *mm;
92 pgd_t *pgd;
93
94 if (addr < TASK_SIZE) {
95 /* TTBR0 */
96 mm = current->active_mm;
97 if (mm == &init_mm) {
98 pr_alert("[%016lx] user address but active_mm is swapper\n",
99 addr);
100 return;
101 }
102 } else if (addr >= VA_START) {
103 /* TTBR1 */
104 mm = &init_mm;
105 } else {
106 pr_alert("[%016lx] address between user and kernel address ranges\n",
107 addr);
108 return;
109 }
110
111 pr_alert("%s pgtable: %luk pages, %u-bit VAs, pgd = %p\n",
112 mm == &init_mm ? "swapper" : "user", PAGE_SIZE / SZ_1K,
113 VA_BITS, mm->pgd);
114 pgd = pgd_offset(mm, addr);
115 pr_alert("[%016lx] *pgd=%016llx", addr, pgd_val(*pgd));
116
117 do {
118 pud_t *pud;
119 pmd_t *pmd;
120 pte_t *pte;
121
122 if (pgd_none(*pgd) || pgd_bad(*pgd))
123 break;
124
125 pud = pud_offset(pgd, addr);
126 pr_cont(", *pud=%016llx", pud_val(*pud));
127 if (pud_none(*pud) || pud_bad(*pud))
128 break;
129
130 pmd = pmd_offset(pud, addr);
131 pr_cont(", *pmd=%016llx", pmd_val(*pmd));
132 if (pmd_none(*pmd) || pmd_bad(*pmd))
133 break;
134
135 pte = pte_offset_map(pmd, addr);
136 pr_cont(", *pte=%016llx", pte_val(*pte));
137 pte_unmap(pte);
138 } while(0);
139
140 pr_cont("\n");
141 }
142
143 #ifdef CONFIG_ARM64_HW_AFDBM
144 /*
145 * This function sets the access flags (dirty, accessed), as well as write
146 * permission, and only to a more permissive setting.
147 *
148 * It needs to cope with hardware update of the accessed/dirty state by other
149 * agents in the system and can safely skip the __sync_icache_dcache() call as,
150 * like set_pte_at(), the PTE is never changed from no-exec to exec here.
151 *
152 * Returns whether or not the PTE actually changed.
153 */
154 int ptep_set_access_flags(struct vm_area_struct *vma,
155 unsigned long address, pte_t *ptep,
156 pte_t entry, int dirty)
157 {
158 pteval_t old_pteval, pteval;
159
160 if (pte_same(*ptep, entry))
161 return 0;
162
163 /* only preserve the access flags and write permission */
164 pte_val(entry) &= PTE_AF | PTE_WRITE | PTE_DIRTY;
165
166 /* set PTE_RDONLY if actual read-only or clean PTE */
167 if (!pte_write(entry) || !pte_sw_dirty(entry))
168 entry = pte_set_rdonly(entry);
169
170 /*
171 * Setting the flags must be done atomically to avoid racing with the
172 * hardware update of the access/dirty state. The PTE_RDONLY bit must
173 * be set to the most permissive (lowest value) of *ptep and entry
174 * (calculated as: a & b == ~(~a | ~b)).
175 */
176 pte_val(entry) ^= PTE_RDONLY;
177 pteval = READ_ONCE(pte_val(*ptep));
178 do {
179 old_pteval = pteval;
180 pteval ^= PTE_RDONLY;
181 pteval |= pte_val(entry);
182 pteval ^= PTE_RDONLY;
183 pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval);
184 } while (pteval != old_pteval);
185
186 flush_tlb_fix_spurious_fault(vma, address);
187 return 1;
188 }
189 #endif
190
191 static bool is_el1_instruction_abort(unsigned int esr)
192 {
193 return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_CUR;
194 }
195
196 static inline bool is_permission_fault(unsigned int esr, struct pt_regs *regs,
197 unsigned long addr)
198 {
199 unsigned int ec = ESR_ELx_EC(esr);
200 unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;
201
202 if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
203 return false;
204
205 if (fsc_type == ESR_ELx_FSC_PERM)
206 return true;
207
208 if (addr < USER_DS && system_uses_ttbr0_pan())
209 return fsc_type == ESR_ELx_FSC_FAULT &&
210 (regs->pstate & PSR_PAN_BIT);
211
212 return false;
213 }
214
215 /*
216 * The kernel tried to access some page that wasn't present.
217 */
218 static void __do_kernel_fault(unsigned long addr, unsigned int esr,
219 struct pt_regs *regs)
220 {
221 const char *msg;
222
223 /*
224 * Are we prepared to handle this kernel fault?
225 * We are almost certainly not prepared to handle instruction faults.
226 */
227 if (!is_el1_instruction_abort(esr) && fixup_exception(regs))
228 return;
229
230 /*
231 * No handler, we'll have to terminate things with extreme prejudice.
232 */
233 bust_spinlocks(1);
234
235 if (is_permission_fault(esr, regs, addr)) {
236 if (esr & ESR_ELx_WNR)
237 msg = "write to read-only memory";
238 else
239 msg = "read from unreadable memory";
240 } else if (addr < PAGE_SIZE) {
241 msg = "NULL pointer dereference";
242 } else {
243 msg = "paging request";
244 }
245
246 pr_alert("Unable to handle kernel %s at virtual address %08lx\n", msg,
247 addr);
248
249 show_pte(addr);
250 die("Oops", regs, esr);
251 bust_spinlocks(0);
252 do_exit(SIGKILL);
253 }
254
255 /*
256 * Something tried to access memory that isn't in our memory map. User mode
257 * accesses just cause a SIGSEGV
258 */
259 static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
260 unsigned int esr, unsigned int sig, int code,
261 struct pt_regs *regs, int fault)
262 {
263 struct siginfo si;
264 const struct fault_info *inf;
265 unsigned int lsb = 0;
266
267 if (unhandled_signal(tsk, sig) && show_unhandled_signals_ratelimited()) {
268 inf = esr_to_fault_info(esr);
269 pr_info("%s[%d]: unhandled %s (%d) at 0x%08lx, esr 0x%03x",
270 tsk->comm, task_pid_nr(tsk), inf->name, sig,
271 addr, esr);
272 print_vma_addr(KERN_CONT ", in ", regs->pc);
273 pr_cont("\n");
274 __show_regs(regs);
275 }
276
277 tsk->thread.fault_address = addr;
278 tsk->thread.fault_code = esr;
279 si.si_signo = sig;
280 si.si_errno = 0;
281 si.si_code = code;
282 si.si_addr = (void __user *)addr;
283 /*
284 * Either small page or large page may be poisoned.
285 * In other words, VM_FAULT_HWPOISON_LARGE and
286 * VM_FAULT_HWPOISON are mutually exclusive.
287 */
288 if (fault & VM_FAULT_HWPOISON_LARGE)
289 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
290 else if (fault & VM_FAULT_HWPOISON)
291 lsb = PAGE_SHIFT;
292 si.si_addr_lsb = lsb;
293
294 force_sig_info(sig, &si, tsk);
295 }
296
297 static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
298 {
299 struct task_struct *tsk = current;
300 const struct fault_info *inf;
301
302 /*
303 * If we are in kernel mode at this point, we have no context to
304 * handle this fault with.
305 */
306 if (user_mode(regs)) {
307 inf = esr_to_fault_info(esr);
308 __do_user_fault(tsk, addr, esr, inf->sig, inf->code, regs, 0);
309 } else
310 __do_kernel_fault(addr, esr, regs);
311 }
312
313 #define VM_FAULT_BADMAP 0x010000
314 #define VM_FAULT_BADACCESS 0x020000
315
316 static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
317 unsigned int mm_flags, unsigned long vm_flags,
318 struct task_struct *tsk)
319 {
320 struct vm_area_struct *vma;
321 int fault;
322
323 vma = find_vma(mm, addr);
324 fault = VM_FAULT_BADMAP;
325 if (unlikely(!vma))
326 goto out;
327 if (unlikely(vma->vm_start > addr))
328 goto check_stack;
329
330 /*
331 * Ok, we have a good vm_area for this memory access, so we can handle
332 * it.
333 */
334 good_area:
335 /*
336 * Check that the permissions on the VMA allow for the fault which
337 * occurred.
338 */
339 if (!(vma->vm_flags & vm_flags)) {
340 fault = VM_FAULT_BADACCESS;
341 goto out;
342 }
343
344 return handle_mm_fault(vma, addr & PAGE_MASK, mm_flags);
345
346 check_stack:
347 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
348 goto good_area;
349 out:
350 return fault;
351 }
352
353 static bool is_el0_instruction_abort(unsigned int esr)
354 {
355 return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW;
356 }
357
358 static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
359 struct pt_regs *regs)
360 {
361 struct task_struct *tsk;
362 struct mm_struct *mm;
363 int fault, sig, code, major = 0;
364 unsigned long vm_flags = VM_READ | VM_WRITE;
365 unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
366
367 if (notify_page_fault(regs, esr))
368 return 0;
369
370 tsk = current;
371 mm = tsk->mm;
372
373 /*
374 * If we're in an interrupt or have no user context, we must not take
375 * the fault.
376 */
377 if (faulthandler_disabled() || !mm)
378 goto no_context;
379
380 if (user_mode(regs))
381 mm_flags |= FAULT_FLAG_USER;
382
383 if (is_el0_instruction_abort(esr)) {
384 vm_flags = VM_EXEC;
385 } else if ((esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM)) {
386 vm_flags = VM_WRITE;
387 mm_flags |= FAULT_FLAG_WRITE;
388 }
389
390 if (addr < USER_DS && is_permission_fault(esr, regs, addr)) {
391 /* regs->orig_addr_limit may be 0 if we entered from EL0 */
392 if (regs->orig_addr_limit == KERNEL_DS)
393 die("Accessing user space memory with fs=KERNEL_DS", regs, esr);
394
395 if (is_el1_instruction_abort(esr))
396 die("Attempting to execute userspace memory", regs, esr);
397
398 if (!search_exception_tables(regs->pc))
399 die("Accessing user space memory outside uaccess.h routines", regs, esr);
400 }
401
402 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
403
404 /*
405 * As per x86, we may deadlock here. However, since the kernel only
406 * validly references user space from well defined areas of the code,
407 * we can bug out early if this is from code which shouldn't.
408 */
409 if (!down_read_trylock(&mm->mmap_sem)) {
410 if (!user_mode(regs) && !search_exception_tables(regs->pc))
411 goto no_context;
412 retry:
413 down_read(&mm->mmap_sem);
414 } else {
415 /*
416 * The above down_read_trylock() might have succeeded in which
417 * case, we'll have missed the might_sleep() from down_read().
418 */
419 might_sleep();
420 #ifdef CONFIG_DEBUG_VM
421 if (!user_mode(regs) && !search_exception_tables(regs->pc))
422 goto no_context;
423 #endif
424 }
425
426 fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk);
427 major |= fault & VM_FAULT_MAJOR;
428
429 if (fault & VM_FAULT_RETRY) {
430 /*
431 * If we need to retry but a fatal signal is pending,
432 * handle the signal first. We do not need to release
433 * the mmap_sem because it would already be released
434 * in __lock_page_or_retry in mm/filemap.c.
435 */
436 if (fatal_signal_pending(current)) {
437 if (!user_mode(regs))
438 goto no_context;
439 return 0;
440 }
441
442 /*
443 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
444 * starvation.
445 */
446 if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
447 mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
448 mm_flags |= FAULT_FLAG_TRIED;
449 goto retry;
450 }
451 }
452 up_read(&mm->mmap_sem);
453
454 /*
455 * Handle the "normal" (no error) case first.
456 */
457 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
458 VM_FAULT_BADACCESS)))) {
459 /*
460 * Major/minor page fault accounting is only done
461 * once. If we go through a retry, it is extremely
462 * likely that the page will be found in page cache at
463 * that point.
464 */
465 if (major) {
466 tsk->maj_flt++;
467 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
468 addr);
469 } else {
470 tsk->min_flt++;
471 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
472 addr);
473 }
474
475 return 0;
476 }
477
478 /*
479 * If we are in kernel mode at this point, we have no context to
480 * handle this fault with.
481 */
482 if (!user_mode(regs))
483 goto no_context;
484
485 if (fault & VM_FAULT_OOM) {
486 /*
487 * We ran out of memory, call the OOM killer, and return to
488 * userspace (which will retry the fault, or kill us if we got
489 * oom-killed).
490 */
491 pagefault_out_of_memory();
492 return 0;
493 }
494
495 if (fault & VM_FAULT_SIGBUS) {
496 /*
497 * We had some memory, but were unable to successfully fix up
498 * this page fault.
499 */
500 sig = SIGBUS;
501 code = BUS_ADRERR;
502 } else if (fault & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) {
503 sig = SIGBUS;
504 code = BUS_MCEERR_AR;
505 } else {
506 /*
507 * Something tried to access memory that isn't in our memory
508 * map.
509 */
510 sig = SIGSEGV;
511 code = fault == VM_FAULT_BADACCESS ?
512 SEGV_ACCERR : SEGV_MAPERR;
513 }
514
515 __do_user_fault(tsk, addr, esr, sig, code, regs, fault);
516 return 0;
517
518 no_context:
519 __do_kernel_fault(addr, esr, regs);
520 return 0;
521 }
522
523 /*
524 * First Level Translation Fault Handler
525 *
526 * We enter here because the first level page table doesn't contain a valid
527 * entry for the address.
528 *
529 * If the address is in kernel space (>= TASK_SIZE), then we are probably
530 * faulting in the vmalloc() area.
531 *
532 * If the init_task's first level page tables contains the relevant entry, we
533 * copy the it to this task. If not, we send the process a signal, fixup the
534 * exception, or oops the kernel.
535 *
536 * NOTE! We MUST NOT take any locks for this case. We may be in an interrupt
537 * or a critical region, and should only copy the information from the master
538 * page table, nothing more.
539 */
540 static int __kprobes do_translation_fault(unsigned long addr,
541 unsigned int esr,
542 struct pt_regs *regs)
543 {
544 if (addr < TASK_SIZE)
545 return do_page_fault(addr, esr, regs);
546
547 do_bad_area(addr, esr, regs);
548 return 0;
549 }
550
551 static int do_alignment_fault(unsigned long addr, unsigned int esr,
552 struct pt_regs *regs)
553 {
554 do_bad_area(addr, esr, regs);
555 return 0;
556 }
557
558 /*
559 * This abort handler always returns "fault".
560 */
561 static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
562 {
563 return 1;
564 }
565
566 /*
567 * This abort handler deals with Synchronous External Abort.
568 * It calls notifiers, and then returns "fault".
569 */
570 static int do_sea(unsigned long addr, unsigned int esr, struct pt_regs *regs)
571 {
572 struct siginfo info;
573 const struct fault_info *inf;
574 int ret = 0;
575
576 inf = esr_to_fault_info(esr);
577 pr_err("Synchronous External Abort: %s (0x%08x) at 0x%016lx\n",
578 inf->name, esr, addr);
579
580 /*
581 * Synchronous aborts may interrupt code which had interrupts masked.
582 * Before calling out into the wider kernel tell the interested
583 * subsystems.
584 */
585 if (IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
586 if (interrupts_enabled(regs))
587 nmi_enter();
588
589 ret = ghes_notify_sea();
590
591 if (interrupts_enabled(regs))
592 nmi_exit();
593 }
594
595 info.si_signo = SIGBUS;
596 info.si_errno = 0;
597 info.si_code = 0;
598 if (esr & ESR_ELx_FnV)
599 info.si_addr = NULL;
600 else
601 info.si_addr = (void __user *)addr;
602 arm64_notify_die("", regs, &info, esr);
603
604 return ret;
605 }
606
607 static const struct fault_info fault_info[] = {
608 { do_bad, SIGBUS, 0, "ttbr address size fault" },
609 { do_bad, SIGBUS, 0, "level 1 address size fault" },
610 { do_bad, SIGBUS, 0, "level 2 address size fault" },
611 { do_bad, SIGBUS, 0, "level 3 address size fault" },
612 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" },
613 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
614 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
615 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
616 { do_bad, SIGBUS, 0, "unknown 8" },
617 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
618 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
619 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
620 { do_bad, SIGBUS, 0, "unknown 12" },
621 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
622 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
623 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
624 { do_sea, SIGBUS, 0, "synchronous external abort" },
625 { do_bad, SIGBUS, 0, "unknown 17" },
626 { do_bad, SIGBUS, 0, "unknown 18" },
627 { do_bad, SIGBUS, 0, "unknown 19" },
628 { do_sea, SIGBUS, 0, "level 0 (translation table walk)" },
629 { do_sea, SIGBUS, 0, "level 1 (translation table walk)" },
630 { do_sea, SIGBUS, 0, "level 2 (translation table walk)" },
631 { do_sea, SIGBUS, 0, "level 3 (translation table walk)" },
632 { do_sea, SIGBUS, 0, "synchronous parity or ECC error" },
633 { do_bad, SIGBUS, 0, "unknown 25" },
634 { do_bad, SIGBUS, 0, "unknown 26" },
635 { do_bad, SIGBUS, 0, "unknown 27" },
636 { do_sea, SIGBUS, 0, "level 0 synchronous parity error (translation table walk)" },
637 { do_sea, SIGBUS, 0, "level 1 synchronous parity error (translation table walk)" },
638 { do_sea, SIGBUS, 0, "level 2 synchronous parity error (translation table walk)" },
639 { do_sea, SIGBUS, 0, "level 3 synchronous parity error (translation table walk)" },
640 { do_bad, SIGBUS, 0, "unknown 32" },
641 { do_alignment_fault, SIGBUS, BUS_ADRALN, "alignment fault" },
642 { do_bad, SIGBUS, 0, "unknown 34" },
643 { do_bad, SIGBUS, 0, "unknown 35" },
644 { do_bad, SIGBUS, 0, "unknown 36" },
645 { do_bad, SIGBUS, 0, "unknown 37" },
646 { do_bad, SIGBUS, 0, "unknown 38" },
647 { do_bad, SIGBUS, 0, "unknown 39" },
648 { do_bad, SIGBUS, 0, "unknown 40" },
649 { do_bad, SIGBUS, 0, "unknown 41" },
650 { do_bad, SIGBUS, 0, "unknown 42" },
651 { do_bad, SIGBUS, 0, "unknown 43" },
652 { do_bad, SIGBUS, 0, "unknown 44" },
653 { do_bad, SIGBUS, 0, "unknown 45" },
654 { do_bad, SIGBUS, 0, "unknown 46" },
655 { do_bad, SIGBUS, 0, "unknown 47" },
656 { do_bad, SIGBUS, 0, "TLB conflict abort" },
657 { do_bad, SIGBUS, 0, "unknown 49" },
658 { do_bad, SIGBUS, 0, "unknown 50" },
659 { do_bad, SIGBUS, 0, "unknown 51" },
660 { do_bad, SIGBUS, 0, "implementation fault (lockdown abort)" },
661 { do_bad, SIGBUS, 0, "implementation fault (unsupported exclusive)" },
662 { do_bad, SIGBUS, 0, "unknown 54" },
663 { do_bad, SIGBUS, 0, "unknown 55" },
664 { do_bad, SIGBUS, 0, "unknown 56" },
665 { do_bad, SIGBUS, 0, "unknown 57" },
666 { do_bad, SIGBUS, 0, "unknown 58" },
667 { do_bad, SIGBUS, 0, "unknown 59" },
668 { do_bad, SIGBUS, 0, "unknown 60" },
669 { do_bad, SIGBUS, 0, "section domain fault" },
670 { do_bad, SIGBUS, 0, "page domain fault" },
671 { do_bad, SIGBUS, 0, "unknown 63" },
672 };
673
674 /*
675 * Handle Synchronous External Aborts that occur in a guest kernel.
676 *
677 * The return value will be zero if the SEA was successfully handled
678 * and non-zero if there was an error processing the error or there was
679 * no error to process.
680 */
681 int handle_guest_sea(phys_addr_t addr, unsigned int esr)
682 {
683 int ret = -ENOENT;
684
685 if (IS_ENABLED(CONFIG_ACPI_APEI_SEA))
686 ret = ghes_notify_sea();
687
688 return ret;
689 }
690
691 /*
692 * Dispatch a data abort to the relevant handler.
693 */
694 asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
695 struct pt_regs *regs)
696 {
697 const struct fault_info *inf = esr_to_fault_info(esr);
698 struct siginfo info;
699
700 if (!inf->fn(addr, esr, regs))
701 return;
702
703 pr_alert("Unhandled fault: %s (0x%08x) at 0x%016lx\n",
704 inf->name, esr, addr);
705
706 info.si_signo = inf->sig;
707 info.si_errno = 0;
708 info.si_code = inf->code;
709 info.si_addr = (void __user *)addr;
710 arm64_notify_die("", regs, &info, esr);
711 }
712
713 /*
714 * Handle stack alignment exceptions.
715 */
716 asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
717 unsigned int esr,
718 struct pt_regs *regs)
719 {
720 struct siginfo info;
721 struct task_struct *tsk = current;
722
723 if (show_unhandled_signals && unhandled_signal(tsk, SIGBUS))
724 pr_info_ratelimited("%s[%d]: %s exception: pc=%p sp=%p\n",
725 tsk->comm, task_pid_nr(tsk),
726 esr_get_class_string(esr), (void *)regs->pc,
727 (void *)regs->sp);
728
729 info.si_signo = SIGBUS;
730 info.si_errno = 0;
731 info.si_code = BUS_ADRALN;
732 info.si_addr = (void __user *)addr;
733 arm64_notify_die("Oops - SP/PC alignment exception", regs, &info, esr);
734 }
735
736 int __init early_brk64(unsigned long addr, unsigned int esr,
737 struct pt_regs *regs);
738
739 /*
740 * __refdata because early_brk64 is __init, but the reference to it is
741 * clobbered at arch_initcall time.
742 * See traps.c and debug-monitors.c:debug_traps_init().
743 */
744 static struct fault_info __refdata debug_fault_info[] = {
745 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
746 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
747 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
748 { do_bad, SIGBUS, 0, "unknown 3" },
749 { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
750 { do_bad, SIGTRAP, 0, "aarch32 vector catch" },
751 { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
752 { do_bad, SIGBUS, 0, "unknown 7" },
753 };
754
755 void __init hook_debug_fault_code(int nr,
756 int (*fn)(unsigned long, unsigned int, struct pt_regs *),
757 int sig, int code, const char *name)
758 {
759 BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
760
761 debug_fault_info[nr].fn = fn;
762 debug_fault_info[nr].sig = sig;
763 debug_fault_info[nr].code = code;
764 debug_fault_info[nr].name = name;
765 }
766
767 asmlinkage int __exception do_debug_exception(unsigned long addr,
768 unsigned int esr,
769 struct pt_regs *regs)
770 {
771 const struct fault_info *inf = debug_fault_info + DBG_ESR_EVT(esr);
772 struct siginfo info;
773 int rv;
774
775 /*
776 * Tell lockdep we disabled irqs in entry.S. Do nothing if they were
777 * already disabled to preserve the last enabled/disabled addresses.
778 */
779 if (interrupts_enabled(regs))
780 trace_hardirqs_off();
781
782 if (!inf->fn(addr, esr, regs)) {
783 rv = 1;
784 } else {
785 pr_alert("Unhandled debug exception: %s (0x%08x) at 0x%016lx\n",
786 inf->name, esr, addr);
787
788 info.si_signo = inf->sig;
789 info.si_errno = 0;
790 info.si_code = inf->code;
791 info.si_addr = (void __user *)addr;
792 arm64_notify_die("", regs, &info, 0);
793 rv = 0;
794 }
795
796 if (interrupts_enabled(regs))
797 trace_hardirqs_on();
798
799 return rv;
800 }
801 NOKPROBE_SYMBOL(do_debug_exception);
802
803 #ifdef CONFIG_ARM64_PAN
804 int cpu_enable_pan(void *__unused)
805 {
806 /*
807 * We modify PSTATE. This won't work from irq context as the PSTATE
808 * is discarded once we return from the exception.
809 */
810 WARN_ON_ONCE(in_interrupt());
811
812 config_sctlr_el1(SCTLR_EL1_SPAN, 0);
813 asm(SET_PSTATE_PAN(1));
814 return 0;
815 }
816 #endif /* CONFIG_ARM64_PAN */