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