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