blob: 97904f83f83f9c7ad0a1bc819c98ebba065cbff4 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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/kasan.h>
43#include <asm/sysreg.h>
44#include <asm/system_misc.h>
45#include <asm/pgtable.h>
46#include <asm/tlbflush.h>
47#include <asm/traps.h>
48
49#include <acpi/ghes.h>
50
51struct fault_info {
52 int (*fn)(unsigned long addr, unsigned int esr,
53 struct pt_regs *regs);
54 int sig;
55 int code;
56 const char *name;
57};
58
59static struct fault_info fault_info[];
60
61static inline const struct fault_info *esr_to_fault_info(unsigned int esr)
62{
63 return fault_info + (esr & 63);
64}
65
66#ifdef CONFIG_KPROBES
67static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
68{
69 int ret = 0;
70
71 /* kprobe_running() needs smp_processor_id() */
72 if (!user_mode(regs)) {
73 preempt_disable();
74 if (kprobe_running() && kprobe_fault_handler(regs, esr))
75 ret = 1;
76 preempt_enable();
77 }
78
79 return ret;
80}
81#else
82static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
83{
84 return 0;
85}
86#endif
87
88static void data_abort_decode(unsigned int esr)
89{
90 pr_alert("Data abort info:\n");
91
92 if (esr & ESR_ELx_ISV) {
93 pr_alert(" Access size = %u byte(s)\n",
94 1U << ((esr & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT));
95 pr_alert(" SSE = %lu, SRT = %lu\n",
96 (esr & ESR_ELx_SSE) >> ESR_ELx_SSE_SHIFT,
97 (esr & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT);
98 pr_alert(" SF = %lu, AR = %lu\n",
99 (esr & ESR_ELx_SF) >> ESR_ELx_SF_SHIFT,
100 (esr & ESR_ELx_AR) >> ESR_ELx_AR_SHIFT);
101 } else {
102 pr_alert(" ISV = 0, ISS = 0x%08lx\n", esr & ESR_ELx_ISS_MASK);
103 }
104
105 pr_alert(" CM = %lu, WnR = %lu\n",
106 (esr & ESR_ELx_CM) >> ESR_ELx_CM_SHIFT,
107 (esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT);
108}
109
110static void mem_abort_decode(unsigned int esr)
111{
112 pr_alert("Mem abort info:\n");
113
114 pr_alert(" ESR = 0x%08x\n", esr);
115 pr_alert(" Exception class = %s, IL = %u bits\n",
116 esr_get_class_string(esr),
117 (esr & ESR_ELx_IL) ? 32 : 16);
118 pr_alert(" SET = %lu, FnV = %lu\n",
119 (esr & ESR_ELx_SET_MASK) >> ESR_ELx_SET_SHIFT,
120 (esr & ESR_ELx_FnV) >> ESR_ELx_FnV_SHIFT);
121 pr_alert(" EA = %lu, S1PTW = %lu\n",
122 (esr & ESR_ELx_EA) >> ESR_ELx_EA_SHIFT,
123 (esr & ESR_ELx_S1PTW) >> ESR_ELx_S1PTW_SHIFT);
124
125 if (esr_is_data_abort(esr))
126 data_abort_decode(esr);
127}
128
129static inline bool is_ttbr0_addr(unsigned long addr)
130{
131 /* entry assembly clears tags for TTBR0 addrs */
132 return addr < TASK_SIZE;
133}
134
135static inline bool is_ttbr1_addr(unsigned long addr)
136{
137 /* TTBR1 addresses may have a tag if KASAN_SW_TAGS is in use */
138 return arch_kasan_reset_tag(addr) >= VA_START;
139}
140
141/*
142 * Dump out the page tables associated with 'addr' in the currently active mm.
143 */
144void show_pte(unsigned long addr)
145{
146 struct mm_struct *mm;
147 pgd_t *pgdp;
148 pgd_t pgd;
149
150 if (is_ttbr0_addr(addr)) {
151 /* TTBR0 */
152 mm = current->active_mm;
153 if (mm == &init_mm) {
154 pr_alert("[%016lx] user address but active_mm is swapper\n",
155 addr);
156 return;
157 }
158 } else if (is_ttbr1_addr(addr)) {
159 /* TTBR1 */
160 mm = &init_mm;
161 } else {
162 pr_alert("[%016lx] address between user and kernel address ranges\n",
163 addr);
164 return;
165 }
166
167 pr_alert("%s pgtable: %luk pages, %u-bit VAs, pgdp = %p\n",
168 mm == &init_mm ? "swapper" : "user", PAGE_SIZE / SZ_1K,
169 VA_BITS, mm->pgd);
170 pgdp = pgd_offset(mm, addr);
171 pgd = READ_ONCE(*pgdp);
172 pr_alert("[%016lx] pgd=%016llx", addr, pgd_val(pgd));
173
174 do {
175 pud_t *pudp, pud;
176 pmd_t *pmdp, pmd;
177 pte_t *ptep, pte;
178
179 if (pgd_none(pgd) || pgd_bad(pgd))
180 break;
181
182 pudp = pud_offset(pgdp, addr);
183 pud = READ_ONCE(*pudp);
184 pr_cont(", pud=%016llx", pud_val(pud));
185 if (pud_none(pud) || pud_bad(pud))
186 break;
187
188 pmdp = pmd_offset(pudp, addr);
189 pmd = READ_ONCE(*pmdp);
190 pr_cont(", pmd=%016llx", pmd_val(pmd));
191 if (pmd_none(pmd) || pmd_bad(pmd))
192 break;
193
194 ptep = pte_offset_map(pmdp, addr);
195 pte = READ_ONCE(*ptep);
196 pr_cont(", pte=%016llx", pte_val(pte));
197 pte_unmap(ptep);
198 } while(0);
199
200 pr_cont("\n");
201}
202
203/*
204 * This function sets the access flags (dirty, accessed), as well as write
205 * permission, and only to a more permissive setting.
206 *
207 * It needs to cope with hardware update of the accessed/dirty state by other
208 * agents in the system and can safely skip the __sync_icache_dcache() call as,
209 * like set_pte_at(), the PTE is never changed from no-exec to exec here.
210 *
211 * Returns whether or not the PTE actually changed.
212 */
213int ptep_set_access_flags(struct vm_area_struct *vma,
214 unsigned long address, pte_t *ptep,
215 pte_t entry, int dirty)
216{
217 pteval_t old_pteval, pteval;
218 pte_t pte = READ_ONCE(*ptep);
219
220 if (pte_same(pte, entry))
221 return 0;
222
223 /* only preserve the access flags and write permission */
224 pte_val(entry) &= PTE_RDONLY | PTE_AF | PTE_WRITE | PTE_DIRTY;
225
226 /*
227 * Setting the flags must be done atomically to avoid racing with the
228 * hardware update of the access/dirty state. The PTE_RDONLY bit must
229 * be set to the most permissive (lowest value) of *ptep and entry
230 * (calculated as: a & b == ~(~a | ~b)).
231 */
232 pte_val(entry) ^= PTE_RDONLY;
233 pteval = pte_val(pte);
234 do {
235 old_pteval = pteval;
236 pteval ^= PTE_RDONLY;
237 pteval |= pte_val(entry);
238 pteval ^= PTE_RDONLY;
239 pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval);
240 } while (pteval != old_pteval);
241
242 flush_tlb_fix_spurious_fault(vma, address);
243 return 1;
244}
245
246static bool is_el1_instruction_abort(unsigned int esr)
247{
248 return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_CUR;
249}
250
251static inline bool is_el1_permission_fault(unsigned int esr,
252 struct pt_regs *regs,
253 unsigned long addr)
254{
255 unsigned int ec = ESR_ELx_EC(esr);
256 unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;
257
258 if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
259 return false;
260
261 if (fsc_type == ESR_ELx_FSC_PERM)
262 return true;
263
264 if (is_ttbr0_addr(addr) && system_uses_ttbr0_pan())
265 return fsc_type == ESR_ELx_FSC_FAULT &&
266 (regs->pstate & PSR_PAN_BIT);
267
268 return false;
269}
270
271static void die_kernel_fault(const char *msg, unsigned long addr,
272 unsigned int esr, struct pt_regs *regs)
273{
274 bust_spinlocks(1);
275
276 pr_alert("Unable to handle kernel %s at virtual address %016lx\n", msg,
277 addr);
278
279 mem_abort_decode(esr);
280
281 show_pte(addr);
282 die("Oops", regs, esr);
283 bust_spinlocks(0);
284 do_exit(SIGKILL);
285}
286
287static void __do_kernel_fault(unsigned long addr, unsigned int esr,
288 struct pt_regs *regs)
289{
290 const char *msg;
291
292 /*
293 * Are we prepared to handle this kernel fault?
294 * We are almost certainly not prepared to handle instruction faults.
295 */
296 if (!is_el1_instruction_abort(esr) && fixup_exception(regs))
297 return;
298
299 if (is_el1_permission_fault(esr, regs, addr)) {
300 if (esr & ESR_ELx_WNR)
301 msg = "write to read-only memory";
302 else
303 msg = "read from unreadable memory";
304 } else if (addr < PAGE_SIZE) {
305 msg = "NULL pointer dereference";
306 } else {
307 msg = "paging request";
308 }
309
310 die_kernel_fault(msg, addr, esr, regs);
311}
312
313static void __do_user_fault(struct siginfo *info, unsigned int esr)
314{
315 current->thread.fault_address = (unsigned long)info->si_addr;
316
317 /*
318 * If the faulting address is in the kernel, we must sanitize the ESR.
319 * From userspace's point of view, kernel-only mappings don't exist
320 * at all, so we report them as level 0 translation faults.
321 * (This is not quite the way that "no mapping there at all" behaves:
322 * an alignment fault not caused by the memory type would take
323 * precedence over translation fault for a real access to empty
324 * space. Unfortunately we can't easily distinguish "alignment fault
325 * not caused by memory type" from "alignment fault caused by memory
326 * type", so we ignore this wrinkle and just return the translation
327 * fault.)
328 */
329 if (!is_ttbr0_addr(current->thread.fault_address)) {
330 switch (ESR_ELx_EC(esr)) {
331 case ESR_ELx_EC_DABT_LOW:
332 /*
333 * These bits provide only information about the
334 * faulting instruction, which userspace knows already.
335 * We explicitly clear bits which are architecturally
336 * RES0 in case they are given meanings in future.
337 * We always report the ESR as if the fault was taken
338 * to EL1 and so ISV and the bits in ISS[23:14] are
339 * clear. (In fact it always will be a fault to EL1.)
340 */
341 esr &= ESR_ELx_EC_MASK | ESR_ELx_IL |
342 ESR_ELx_CM | ESR_ELx_WNR;
343 esr |= ESR_ELx_FSC_FAULT;
344 break;
345 case ESR_ELx_EC_IABT_LOW:
346 /*
347 * Claim a level 0 translation fault.
348 * All other bits are architecturally RES0 for faults
349 * reported with that DFSC value, so we clear them.
350 */
351 esr &= ESR_ELx_EC_MASK | ESR_ELx_IL;
352 esr |= ESR_ELx_FSC_FAULT;
353 break;
354 default:
355 /*
356 * This should never happen (entry.S only brings us
357 * into this code for insn and data aborts from a lower
358 * exception level). Fail safe by not providing an ESR
359 * context record at all.
360 */
361 WARN(1, "ESR 0x%x is not DABT or IABT from EL0\n", esr);
362 esr = 0;
363 break;
364 }
365 }
366
367 current->thread.fault_code = esr;
368 arm64_force_sig_info(info, esr_to_fault_info(esr)->name, current);
369}
370
371static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
372{
373 /*
374 * If we are in kernel mode at this point, we have no context to
375 * handle this fault with.
376 */
377 if (user_mode(regs)) {
378 const struct fault_info *inf = esr_to_fault_info(esr);
379 struct siginfo si;
380
381 clear_siginfo(&si);
382 si.si_signo = inf->sig;
383 si.si_code = inf->code;
384 si.si_addr = (void __user *)addr;
385
386 __do_user_fault(&si, esr);
387 } else {
388 __do_kernel_fault(addr, esr, regs);
389 }
390}
391
392#define VM_FAULT_BADMAP 0x010000
393#define VM_FAULT_BADACCESS 0x020000
394
395static vm_fault_t __do_page_fault(struct mm_struct *mm, unsigned long addr,
396 unsigned int mm_flags, unsigned long vm_flags,
397 struct task_struct *tsk)
398{
399 struct vm_area_struct *vma;
400 vm_fault_t fault;
401
402 vma = find_vma(mm, addr);
403 fault = VM_FAULT_BADMAP;
404 if (unlikely(!vma))
405 goto out;
406 if (unlikely(vma->vm_start > addr))
407 goto check_stack;
408
409 /*
410 * Ok, we have a good vm_area for this memory access, so we can handle
411 * it.
412 */
413good_area:
414 /*
415 * Check that the permissions on the VMA allow for the fault which
416 * occurred.
417 */
418 if (!(vma->vm_flags & vm_flags)) {
419 fault = VM_FAULT_BADACCESS;
420 goto out;
421 }
422
423 return handle_mm_fault(vma, addr & PAGE_MASK, mm_flags);
424
425check_stack:
426 if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
427 goto good_area;
428out:
429 return fault;
430}
431
432static bool is_el0_instruction_abort(unsigned int esr)
433{
434 return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW;
435}
436
437static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
438 struct pt_regs *regs)
439{
440 struct task_struct *tsk;
441 struct mm_struct *mm;
442 struct siginfo si;
443 vm_fault_t fault, major = 0;
444 unsigned long vm_flags = VM_READ | VM_WRITE | VM_EXEC;
445 unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
446
447 if (notify_page_fault(regs, esr))
448 return 0;
449
450 tsk = current;
451 mm = tsk->mm;
452
453 /*
454 * If we're in an interrupt or have no user context, we must not take
455 * the fault.
456 */
457 if (faulthandler_disabled() || !mm)
458 goto no_context;
459
460 if (user_mode(regs))
461 mm_flags |= FAULT_FLAG_USER;
462
463 if (is_el0_instruction_abort(esr)) {
464 vm_flags = VM_EXEC;
465 } else if ((esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM)) {
466 vm_flags = VM_WRITE;
467 mm_flags |= FAULT_FLAG_WRITE;
468 }
469
470 if (is_ttbr0_addr(addr) && is_el1_permission_fault(esr, regs, addr)) {
471 /* regs->orig_addr_limit may be 0 if we entered from EL0 */
472 if (regs->orig_addr_limit == KERNEL_DS)
473 die_kernel_fault("access to user memory with fs=KERNEL_DS",
474 addr, esr, regs);
475
476 if (is_el1_instruction_abort(esr))
477 die_kernel_fault("execution of user memory",
478 addr, esr, regs);
479
480 if (!search_exception_tables(regs->pc))
481 die_kernel_fault("access to user memory outside uaccess routines",
482 addr, esr, regs);
483 }
484
485 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
486
487 /*
488 * As per x86, we may deadlock here. However, since the kernel only
489 * validly references user space from well defined areas of the code,
490 * we can bug out early if this is from code which shouldn't.
491 */
492 if (!down_read_trylock(&mm->mmap_sem)) {
493 if (!user_mode(regs) && !search_exception_tables(regs->pc))
494 goto no_context;
495retry:
496 down_read(&mm->mmap_sem);
497 } else {
498 /*
499 * The above down_read_trylock() might have succeeded in which
500 * case, we'll have missed the might_sleep() from down_read().
501 */
502 might_sleep();
503#ifdef CONFIG_DEBUG_VM
504 if (!user_mode(regs) && !search_exception_tables(regs->pc))
505 goto no_context;
506#endif
507 }
508
509 fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk);
510 major |= fault & VM_FAULT_MAJOR;
511
512 if (fault & VM_FAULT_RETRY) {
513 /*
514 * If we need to retry but a fatal signal is pending,
515 * handle the signal first. We do not need to release
516 * the mmap_sem because it would already be released
517 * in __lock_page_or_retry in mm/filemap.c.
518 */
519 if (fatal_signal_pending(current)) {
520 if (!user_mode(regs))
521 goto no_context;
522 return 0;
523 }
524
525 /*
526 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
527 * starvation.
528 */
529 if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
530 mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
531 mm_flags |= FAULT_FLAG_TRIED;
532 goto retry;
533 }
534 }
535 up_read(&mm->mmap_sem);
536
537 /*
538 * Handle the "normal" (no error) case first.
539 */
540 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
541 VM_FAULT_BADACCESS)))) {
542 /*
543 * Major/minor page fault accounting is only done
544 * once. If we go through a retry, it is extremely
545 * likely that the page will be found in page cache at
546 * that point.
547 */
548 if (major) {
549 tsk->maj_flt++;
550 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
551 addr);
552 } else {
553 tsk->min_flt++;
554 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
555 addr);
556 }
557
558 return 0;
559 }
560
561 /*
562 * If we are in kernel mode at this point, we have no context to
563 * handle this fault with.
564 */
565 if (!user_mode(regs))
566 goto no_context;
567
568 if (fault & VM_FAULT_OOM) {
569 /*
570 * We ran out of memory, call the OOM killer, and return to
571 * userspace (which will retry the fault, or kill us if we got
572 * oom-killed).
573 */
574 pagefault_out_of_memory();
575 return 0;
576 }
577
578 clear_siginfo(&si);
579 si.si_addr = (void __user *)addr;
580
581 if (fault & VM_FAULT_SIGBUS) {
582 /*
583 * We had some memory, but were unable to successfully fix up
584 * this page fault.
585 */
586 si.si_signo = SIGBUS;
587 si.si_code = BUS_ADRERR;
588 } else if (fault & VM_FAULT_HWPOISON_LARGE) {
589 unsigned int hindex = VM_FAULT_GET_HINDEX(fault);
590
591 si.si_signo = SIGBUS;
592 si.si_code = BUS_MCEERR_AR;
593 si.si_addr_lsb = hstate_index_to_shift(hindex);
594 } else if (fault & VM_FAULT_HWPOISON) {
595 si.si_signo = SIGBUS;
596 si.si_code = BUS_MCEERR_AR;
597 si.si_addr_lsb = PAGE_SHIFT;
598 } else {
599 /*
600 * Something tried to access memory that isn't in our memory
601 * map.
602 */
603 si.si_signo = SIGSEGV;
604 si.si_code = fault == VM_FAULT_BADACCESS ?
605 SEGV_ACCERR : SEGV_MAPERR;
606 }
607
608 __do_user_fault(&si, esr);
609 return 0;
610
611no_context:
612 __do_kernel_fault(addr, esr, regs);
613 return 0;
614}
615
616static int __kprobes do_translation_fault(unsigned long addr,
617 unsigned int esr,
618 struct pt_regs *regs)
619{
620 if (is_ttbr0_addr(addr))
621 return do_page_fault(addr, esr, regs);
622
623 do_bad_area(addr, esr, regs);
624 return 0;
625}
626
627static int do_alignment_fault(unsigned long addr, unsigned int esr,
628 struct pt_regs *regs)
629{
630 do_bad_area(addr, esr, regs);
631 return 0;
632}
633
634static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
635{
636 return 1; /* "fault" */
637}
638
639static int do_sea(unsigned long addr, unsigned int esr, struct pt_regs *regs)
640{
641 struct siginfo info;
642 const struct fault_info *inf;
643
644 inf = esr_to_fault_info(esr);
645
646 /*
647 * Synchronous aborts may interrupt code which had interrupts masked.
648 * Before calling out into the wider kernel tell the interested
649 * subsystems.
650 */
651 if (IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
652 if (interrupts_enabled(regs))
653 nmi_enter();
654
655 ghes_notify_sea();
656
657 if (interrupts_enabled(regs))
658 nmi_exit();
659 }
660
661 clear_siginfo(&info);
662 info.si_signo = inf->sig;
663 info.si_errno = 0;
664 info.si_code = inf->code;
665 if (esr & ESR_ELx_FnV)
666 info.si_addr = NULL;
667 else
668 info.si_addr = (void __user *)addr;
669 arm64_notify_die(inf->name, regs, &info, esr);
670
671 return 0;
672}
673
674static struct fault_info fault_info[] = {
675 { do_bad, SIGKILL, SI_KERNEL, "ttbr address size fault" },
676 { do_bad, SIGKILL, SI_KERNEL, "level 1 address size fault" },
677 { do_bad, SIGKILL, SI_KERNEL, "level 2 address size fault" },
678 { do_bad, SIGKILL, SI_KERNEL, "level 3 address size fault" },
679 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" },
680 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
681 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
682 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
683 { do_bad, SIGKILL, SI_KERNEL, "unknown 8" },
684 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
685 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
686 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
687 { do_bad, SIGKILL, SI_KERNEL, "unknown 12" },
688 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
689 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
690 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
691 { do_sea, SIGBUS, BUS_OBJERR, "synchronous external abort" },
692 { do_bad, SIGKILL, SI_KERNEL, "unknown 17" },
693 { do_bad, SIGKILL, SI_KERNEL, "unknown 18" },
694 { do_bad, SIGKILL, SI_KERNEL, "unknown 19" },
695 { do_sea, SIGKILL, SI_KERNEL, "level 0 (translation table walk)" },
696 { do_sea, SIGKILL, SI_KERNEL, "level 1 (translation table walk)" },
697 { do_sea, SIGKILL, SI_KERNEL, "level 2 (translation table walk)" },
698 { do_sea, SIGKILL, SI_KERNEL, "level 3 (translation table walk)" },
699 { do_sea, SIGBUS, BUS_OBJERR, "synchronous parity or ECC error" }, // Reserved when RAS is implemented
700 { do_bad, SIGKILL, SI_KERNEL, "unknown 25" },
701 { do_bad, SIGKILL, SI_KERNEL, "unknown 26" },
702 { do_bad, SIGKILL, SI_KERNEL, "unknown 27" },
703 { do_sea, SIGKILL, SI_KERNEL, "level 0 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
704 { do_sea, SIGKILL, SI_KERNEL, "level 1 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
705 { do_sea, SIGKILL, SI_KERNEL, "level 2 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
706 { do_sea, SIGKILL, SI_KERNEL, "level 3 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
707 { do_bad, SIGKILL, SI_KERNEL, "unknown 32" },
708 { do_alignment_fault, SIGBUS, BUS_ADRALN, "alignment fault" },
709 { do_bad, SIGKILL, SI_KERNEL, "unknown 34" },
710 { do_bad, SIGKILL, SI_KERNEL, "unknown 35" },
711 { do_bad, SIGKILL, SI_KERNEL, "unknown 36" },
712 { do_bad, SIGKILL, SI_KERNEL, "unknown 37" },
713 { do_bad, SIGKILL, SI_KERNEL, "unknown 38" },
714 { do_bad, SIGKILL, SI_KERNEL, "unknown 39" },
715 { do_bad, SIGKILL, SI_KERNEL, "unknown 40" },
716 { do_bad, SIGKILL, SI_KERNEL, "unknown 41" },
717 { do_bad, SIGKILL, SI_KERNEL, "unknown 42" },
718 { do_bad, SIGKILL, SI_KERNEL, "unknown 43" },
719 { do_bad, SIGKILL, SI_KERNEL, "unknown 44" },
720 { do_bad, SIGKILL, SI_KERNEL, "unknown 45" },
721 { do_bad, SIGKILL, SI_KERNEL, "unknown 46" },
722 { do_bad, SIGKILL, SI_KERNEL, "unknown 47" },
723 { do_bad, SIGKILL, SI_KERNEL, "TLB conflict abort" },
724 { do_bad, SIGKILL, SI_KERNEL, "Unsupported atomic hardware update fault" },
725 { do_bad, SIGKILL, SI_KERNEL, "unknown 50" },
726 { do_bad, SIGKILL, SI_KERNEL, "unknown 51" },
727 { do_bad, SIGKILL, SI_KERNEL, "implementation fault (lockdown abort)" },
728 { do_bad, SIGBUS, BUS_OBJERR, "implementation fault (unsupported exclusive)" },
729 { do_bad, SIGKILL, SI_KERNEL, "unknown 54" },
730 { do_bad, SIGKILL, SI_KERNEL, "unknown 55" },
731 { do_bad, SIGKILL, SI_KERNEL, "unknown 56" },
732 { do_bad, SIGKILL, SI_KERNEL, "unknown 57" },
733 { do_bad, SIGKILL, SI_KERNEL, "unknown 58" },
734 { do_bad, SIGKILL, SI_KERNEL, "unknown 59" },
735 { do_bad, SIGKILL, SI_KERNEL, "unknown 60" },
736 { do_bad, SIGKILL, SI_KERNEL, "section domain fault" },
737 { do_bad, SIGKILL, SI_KERNEL, "page domain fault" },
738 { do_bad, SIGKILL, SI_KERNEL, "unknown 63" },
739};
740
741int handle_guest_sea(phys_addr_t addr, unsigned int esr)
742{
743 return ghes_notify_sea();
744}
745
746asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
747 struct pt_regs *regs)
748{
749 const struct fault_info *inf = esr_to_fault_info(esr);
750 struct siginfo info;
751
752 if (!inf->fn(addr, esr, regs))
753 return;
754
755 if (!user_mode(regs)) {
756 pr_alert("Unhandled fault at 0x%016lx\n", addr);
757 mem_abort_decode(esr);
758 show_pte(addr);
759 }
760
761 clear_siginfo(&info);
762 info.si_signo = inf->sig;
763 info.si_errno = 0;
764 info.si_code = inf->code;
765 info.si_addr = (void __user *)addr;
766 arm64_notify_die(inf->name, regs, &info, esr);
767}
768
769asmlinkage void __exception do_el0_irq_bp_hardening(void)
770{
771 /* PC has already been checked in entry.S */
772 arm64_apply_bp_hardening();
773}
774
775asmlinkage void __exception do_el0_ia_bp_hardening(unsigned long addr,
776 unsigned int esr,
777 struct pt_regs *regs)
778{
779 /*
780 * We've taken an instruction abort from userspace and not yet
781 * re-enabled IRQs. If the address is a kernel address, apply
782 * BP hardening prior to enabling IRQs and pre-emption.
783 */
784 if (!is_ttbr0_addr(addr))
785 arm64_apply_bp_hardening();
786
787 local_irq_enable();
788 do_mem_abort(addr, esr, regs);
789}
790
791
792asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
793 unsigned int esr,
794 struct pt_regs *regs)
795{
796 struct siginfo info;
797
798 if (user_mode(regs)) {
799 if (!is_ttbr0_addr(instruction_pointer(regs)))
800 arm64_apply_bp_hardening();
801 local_irq_enable();
802 }
803
804 clear_siginfo(&info);
805 info.si_signo = SIGBUS;
806 info.si_errno = 0;
807 info.si_code = BUS_ADRALN;
808 info.si_addr = (void __user *)addr;
809 arm64_notify_die("SP/PC alignment exception", regs, &info, esr);
810}
811
812int __init early_brk64(unsigned long addr, unsigned int esr,
813 struct pt_regs *regs);
814
815/*
816 * __refdata because early_brk64 is __init, but the reference to it is
817 * clobbered at arch_initcall time.
818 * See traps.c and debug-monitors.c:debug_traps_init().
819 */
820static struct fault_info __refdata debug_fault_info[] = {
821 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
822 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
823 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
824 { do_bad, SIGKILL, SI_KERNEL, "unknown 3" },
825 { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
826 { do_bad, SIGKILL, SI_KERNEL, "aarch32 vector catch" },
827 { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
828 { do_bad, SIGKILL, SI_KERNEL, "unknown 7" },
829};
830
831void __init hook_debug_fault_code(int nr,
832 int (*fn)(unsigned long, unsigned int, struct pt_regs *),
833 int sig, int code, const char *name)
834{
835 BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
836
837 debug_fault_info[nr].fn = fn;
838 debug_fault_info[nr].sig = sig;
839 debug_fault_info[nr].code = code;
840 debug_fault_info[nr].name = name;
841}
842
843void hook_fault_code(int nr,
844 int (*fn)(unsigned long, unsigned int, struct pt_regs *),
845 int sig, int code, const char *name)
846{
847 WARN_ON(nr < 0 || nr >= ARRAY_SIZE(fault_info));
848
849 fault_info[nr].fn = fn;
850 fault_info[nr].sig = sig;
851 fault_info[nr].code = code;
852 fault_info[nr].name = name;
853}
854
855#ifdef CONFIG_ARM64_ERRATUM_1463225
856DECLARE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
857
858static int __exception
859cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
860{
861 if (user_mode(regs))
862 return 0;
863
864 if (!__this_cpu_read(__in_cortex_a76_erratum_1463225_wa))
865 return 0;
866
867 /*
868 * We've taken a dummy step exception from the kernel to ensure
869 * that interrupts are re-enabled on the syscall path. Return back
870 * to cortex_a76_erratum_1463225_svc_handler() with debug exceptions
871 * masked so that we can safely restore the mdscr and get on with
872 * handling the syscall.
873 */
874 regs->pstate |= PSR_D_BIT;
875 return 1;
876}
877#else
878static int __exception
879cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
880{
881 return 0;
882}
883#endif /* CONFIG_ARM64_ERRATUM_1463225 */
884
885asmlinkage int __exception do_debug_exception(unsigned long addr_if_watchpoint,
886 unsigned int esr,
887 struct pt_regs *regs)
888{
889 const struct fault_info *inf = debug_fault_info + DBG_ESR_EVT(esr);
890 unsigned long pc = instruction_pointer(regs);
891 int rv;
892
893 if (cortex_a76_erratum_1463225_debug_handler(regs))
894 return 0;
895
896 /*
897 * Tell lockdep we disabled irqs in entry.S. Do nothing if they were
898 * already disabled to preserve the last enabled/disabled addresses.
899 */
900 if (interrupts_enabled(regs))
901 trace_hardirqs_off();
902
903 if (user_mode(regs) && !is_ttbr0_addr(pc))
904 arm64_apply_bp_hardening();
905
906 if (!inf->fn(addr_if_watchpoint, esr, regs)) {
907 rv = 1;
908 } else {
909 struct siginfo info;
910
911 clear_siginfo(&info);
912 info.si_signo = inf->sig;
913 info.si_errno = 0;
914 info.si_code = inf->code;
915 info.si_addr = (void __user *)pc;
916 arm64_notify_die(inf->name, regs, &info, esr);
917 rv = 0;
918 }
919
920 if (interrupts_enabled(regs))
921 trace_hardirqs_on();
922
923 return rv;
924}
925NOKPROBE_SYMBOL(do_debug_exception);
926
927#ifdef CONFIG_ARM64_PAN
928void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused)
929{
930 /*
931 * We modify PSTATE. This won't work from irq context as the PSTATE
932 * is discarded once we return from the exception.
933 */
934 WARN_ON_ONCE(in_interrupt());
935
936 sysreg_clear_set(sctlr_el1, SCTLR_EL1_SPAN, 0);
937 asm(SET_PSTATE_PAN(1));
938}
939#endif /* CONFIG_ARM64_PAN */