blob: 9568c116ac7fc629994790a3d06125d5e2f44ca9 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Catalin Marinas1d18c472012-03-05 11:49:27 +00002/*
3 * Based on arch/arm/mm/fault.c
4 *
5 * Copyright (C) 1995 Linus Torvalds
6 * Copyright (C) 1995-2004 Russell King
7 * Copyright (C) 2012 ARM Ltd.
Catalin Marinas1d18c472012-03-05 11:49:27 +00008 */
9
James Morsed44f1b82019-01-29 18:48:50 +000010#include <linux/acpi.h>
Paul Gortmaker0edfa832016-09-19 17:38:55 -040011#include <linux/extable.h>
Catalin Marinas1d18c472012-03-05 11:49:27 +000012#include <linux/signal.h>
13#include <linux/mm.h>
14#include <linux/hardirq.h>
15#include <linux/init.h>
16#include <linux/kprobes.h>
17#include <linux/uaccess.h>
18#include <linux/page-flags.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010019#include <linux/sched/signal.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010020#include <linux/sched/debug.h>
Catalin Marinas1d18c472012-03-05 11:49:27 +000021#include <linux/highmem.h>
22#include <linux/perf_event.h>
James Morse7209c862016-10-18 11:27:47 +010023#include <linux/preempt.h>
Jonathan (Zhixiong) Zhange7c600f2017-06-08 18:25:27 +010024#include <linux/hugetlb.h>
Catalin Marinas1d18c472012-03-05 11:49:27 +000025
James Morsed44f1b82019-01-29 18:48:50 +000026#include <asm/acpi.h>
James Morse7209c862016-10-18 11:27:47 +010027#include <asm/bug.h>
Catalin Marinas3bbf7152017-06-26 14:27:36 +010028#include <asm/cmpxchg.h>
James Morse338d4f42015-07-22 19:05:54 +010029#include <asm/cpufeature.h>
Catalin Marinas1d18c472012-03-05 11:49:27 +000030#include <asm/exception.h>
Julien Thierry9a0c0322018-08-28 16:51:15 +010031#include <asm/daifflags.h>
Catalin Marinas1d18c472012-03-05 11:49:27 +000032#include <asm/debug-monitors.h>
Catalin Marinas91413002014-04-06 23:04:12 +010033#include <asm/esr.h>
Andrey Konovalov356607f2018-12-28 00:30:27 -080034#include <asm/kasan.h>
James Morse338d4f42015-07-22 19:05:54 +010035#include <asm/sysreg.h>
Catalin Marinas1d18c472012-03-05 11:49:27 +000036#include <asm/system_misc.h>
37#include <asm/pgtable.h>
38#include <asm/tlbflush.h>
Will Deacon92ff0672018-02-20 14:53:22 +000039#include <asm/traps.h>
Catalin Marinas1d18c472012-03-05 11:49:27 +000040
Victor Kamensky09a6adf2017-04-03 22:51:01 -070041struct fault_info {
42 int (*fn)(unsigned long addr, unsigned int esr,
43 struct pt_regs *regs);
44 int sig;
45 int code;
46 const char *name;
47};
48
49static const struct fault_info fault_info[];
Anshuman Khandual359048f2018-09-22 21:09:54 +053050static struct fault_info debug_fault_info[];
Victor Kamensky09a6adf2017-04-03 22:51:01 -070051
52static inline const struct fault_info *esr_to_fault_info(unsigned int esr)
53{
Anshuman Khandual00bbd5d2018-09-22 21:09:52 +053054 return fault_info + (esr & ESR_ELx_FSC);
Victor Kamensky09a6adf2017-04-03 22:51:01 -070055}
Catalin Marinas3495386b2012-10-24 16:34:02 +010056
Anshuman Khandual359048f2018-09-22 21:09:54 +053057static inline const struct fault_info *esr_to_debug_fault_info(unsigned int esr)
58{
59 return debug_fault_info + DBG_ESR_EVT(esr);
60}
61
Julien Thierry1f9b8932017-08-04 09:31:42 +010062static void data_abort_decode(unsigned int esr)
63{
64 pr_alert("Data abort info:\n");
65
66 if (esr & ESR_ELx_ISV) {
67 pr_alert(" Access size = %u byte(s)\n",
68 1U << ((esr & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT));
69 pr_alert(" SSE = %lu, SRT = %lu\n",
70 (esr & ESR_ELx_SSE) >> ESR_ELx_SSE_SHIFT,
71 (esr & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT);
72 pr_alert(" SF = %lu, AR = %lu\n",
73 (esr & ESR_ELx_SF) >> ESR_ELx_SF_SHIFT,
74 (esr & ESR_ELx_AR) >> ESR_ELx_AR_SHIFT);
75 } else {
Mark Rutland0a6de8b2017-10-02 12:42:00 +010076 pr_alert(" ISV = 0, ISS = 0x%08lx\n", esr & ESR_ELx_ISS_MASK);
Julien Thierry1f9b8932017-08-04 09:31:42 +010077 }
78
79 pr_alert(" CM = %lu, WnR = %lu\n",
80 (esr & ESR_ELx_CM) >> ESR_ELx_CM_SHIFT,
81 (esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT);
82}
83
Julien Thierry1f9b8932017-08-04 09:31:42 +010084static void mem_abort_decode(unsigned int esr)
85{
86 pr_alert("Mem abort info:\n");
87
Mark Rutland42dbf542017-10-19 11:19:55 +010088 pr_alert(" ESR = 0x%08x\n", esr);
Julien Thierry1f9b8932017-08-04 09:31:42 +010089 pr_alert(" Exception class = %s, IL = %u bits\n",
90 esr_get_class_string(esr),
91 (esr & ESR_ELx_IL) ? 32 : 16);
92 pr_alert(" SET = %lu, FnV = %lu\n",
93 (esr & ESR_ELx_SET_MASK) >> ESR_ELx_SET_SHIFT,
94 (esr & ESR_ELx_FnV) >> ESR_ELx_FnV_SHIFT);
95 pr_alert(" EA = %lu, S1PTW = %lu\n",
96 (esr & ESR_ELx_EA) >> ESR_ELx_EA_SHIFT,
97 (esr & ESR_ELx_S1PTW) >> ESR_ELx_S1PTW_SHIFT);
98
99 if (esr_is_data_abort(esr))
100 data_abort_decode(esr);
101}
102
Andrey Konovalov356607f2018-12-28 00:30:27 -0800103static inline bool is_ttbr0_addr(unsigned long addr)
104{
105 /* entry assembly clears tags for TTBR0 addrs */
106 return addr < TASK_SIZE;
107}
108
109static inline bool is_ttbr1_addr(unsigned long addr)
110{
111 /* TTBR1 addresses may have a tag if KASAN_SW_TAGS is in use */
112 return arch_kasan_reset_tag(addr) >= VA_START;
113}
114
Catalin Marinas1d18c472012-03-05 11:49:27 +0000115/*
Kristina Martsenko67ce16e2017-06-09 16:35:52 +0100116 * Dump out the page tables associated with 'addr' in the currently active mm.
Catalin Marinas1d18c472012-03-05 11:49:27 +0000117 */
Will Deacon7048a592019-04-03 13:36:54 +0100118static void show_pte(unsigned long addr)
Catalin Marinas1d18c472012-03-05 11:49:27 +0000119{
Kristina Martsenko67ce16e2017-06-09 16:35:52 +0100120 struct mm_struct *mm;
Will Deacon20a004e2018-02-15 11:14:56 +0000121 pgd_t *pgdp;
122 pgd_t pgd;
Catalin Marinas1d18c472012-03-05 11:49:27 +0000123
Andrey Konovalov356607f2018-12-28 00:30:27 -0800124 if (is_ttbr0_addr(addr)) {
Kristina Martsenko67ce16e2017-06-09 16:35:52 +0100125 /* TTBR0 */
126 mm = current->active_mm;
127 if (mm == &init_mm) {
128 pr_alert("[%016lx] user address but active_mm is swapper\n",
129 addr);
130 return;
131 }
Andrey Konovalov356607f2018-12-28 00:30:27 -0800132 } else if (is_ttbr1_addr(addr)) {
Kristina Martsenko67ce16e2017-06-09 16:35:52 +0100133 /* TTBR1 */
Catalin Marinas1d18c472012-03-05 11:49:27 +0000134 mm = &init_mm;
Kristina Martsenko67ce16e2017-06-09 16:35:52 +0100135 } else {
136 pr_alert("[%016lx] address between user and kernel address ranges\n",
137 addr);
138 return;
139 }
Catalin Marinas1d18c472012-03-05 11:49:27 +0000140
Will Deacon48caebf2019-05-14 12:25:28 +0100141 pr_alert("%s pgtable: %luk pages, %u-bit VAs, pgdp=%016lx\n",
Will Deacon1eb34b62017-05-15 15:23:58 +0100142 mm == &init_mm ? "swapper" : "user", PAGE_SIZE / SZ_1K,
Will Deacon48caebf2019-05-14 12:25:28 +0100143 mm == &init_mm ? VA_BITS : (int)vabits_user,
144 (unsigned long)virt_to_phys(mm->pgd));
Will Deacon20a004e2018-02-15 11:14:56 +0000145 pgdp = pgd_offset(mm, addr);
146 pgd = READ_ONCE(*pgdp);
147 pr_alert("[%016lx] pgd=%016llx", addr, pgd_val(pgd));
Catalin Marinas1d18c472012-03-05 11:49:27 +0000148
149 do {
Will Deacon20a004e2018-02-15 11:14:56 +0000150 pud_t *pudp, pud;
151 pmd_t *pmdp, pmd;
152 pte_t *ptep, pte;
Catalin Marinas1d18c472012-03-05 11:49:27 +0000153
Will Deacon20a004e2018-02-15 11:14:56 +0000154 if (pgd_none(pgd) || pgd_bad(pgd))
Catalin Marinas1d18c472012-03-05 11:49:27 +0000155 break;
156
Will Deacon20a004e2018-02-15 11:14:56 +0000157 pudp = pud_offset(pgdp, addr);
158 pud = READ_ONCE(*pudp);
159 pr_cont(", pud=%016llx", pud_val(pud));
160 if (pud_none(pud) || pud_bad(pud))
Catalin Marinas1d18c472012-03-05 11:49:27 +0000161 break;
162
Will Deacon20a004e2018-02-15 11:14:56 +0000163 pmdp = pmd_offset(pudp, addr);
164 pmd = READ_ONCE(*pmdp);
165 pr_cont(", pmd=%016llx", pmd_val(pmd));
166 if (pmd_none(pmd) || pmd_bad(pmd))
Catalin Marinas1d18c472012-03-05 11:49:27 +0000167 break;
168
Will Deacon20a004e2018-02-15 11:14:56 +0000169 ptep = pte_offset_map(pmdp, addr);
170 pte = READ_ONCE(*ptep);
171 pr_cont(", pte=%016llx", pte_val(pte));
172 pte_unmap(ptep);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000173 } while(0);
174
Mark Rutland6ef4fb32017-01-03 14:27:26 +0000175 pr_cont("\n");
Catalin Marinas1d18c472012-03-05 11:49:27 +0000176}
177
Catalin Marinas66dbd6e2016-04-13 16:01:22 +0100178/*
179 * This function sets the access flags (dirty, accessed), as well as write
180 * permission, and only to a more permissive setting.
181 *
182 * It needs to cope with hardware update of the accessed/dirty state by other
183 * agents in the system and can safely skip the __sync_icache_dcache() call as,
184 * like set_pte_at(), the PTE is never changed from no-exec to exec here.
185 *
186 * Returns whether or not the PTE actually changed.
187 */
188int ptep_set_access_flags(struct vm_area_struct *vma,
189 unsigned long address, pte_t *ptep,
190 pte_t entry, int dirty)
191{
Catalin Marinas3bbf7152017-06-26 14:27:36 +0100192 pteval_t old_pteval, pteval;
Will Deacon20a004e2018-02-15 11:14:56 +0000193 pte_t pte = READ_ONCE(*ptep);
Catalin Marinas66dbd6e2016-04-13 16:01:22 +0100194
Will Deacon20a004e2018-02-15 11:14:56 +0000195 if (pte_same(pte, entry))
Catalin Marinas66dbd6e2016-04-13 16:01:22 +0100196 return 0;
197
198 /* only preserve the access flags and write permission */
Catalin Marinas73e86cb2017-07-04 19:04:18 +0100199 pte_val(entry) &= PTE_RDONLY | PTE_AF | PTE_WRITE | PTE_DIRTY;
Catalin Marinas66dbd6e2016-04-13 16:01:22 +0100200
201 /*
202 * Setting the flags must be done atomically to avoid racing with the
Catalin Marinas6d332742017-07-25 14:53:03 +0100203 * hardware update of the access/dirty state. The PTE_RDONLY bit must
204 * be set to the most permissive (lowest value) of *ptep and entry
205 * (calculated as: a & b == ~(~a | ~b)).
Catalin Marinas66dbd6e2016-04-13 16:01:22 +0100206 */
Catalin Marinas6d332742017-07-25 14:53:03 +0100207 pte_val(entry) ^= PTE_RDONLY;
Will Deacon20a004e2018-02-15 11:14:56 +0000208 pteval = pte_val(pte);
Catalin Marinas3bbf7152017-06-26 14:27:36 +0100209 do {
210 old_pteval = pteval;
211 pteval ^= PTE_RDONLY;
212 pteval |= pte_val(entry);
213 pteval ^= PTE_RDONLY;
214 pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval);
215 } while (pteval != old_pteval);
Catalin Marinas66dbd6e2016-04-13 16:01:22 +0100216
217 flush_tlb_fix_spurious_fault(vma, address);
218 return 1;
219}
Catalin Marinas66dbd6e2016-04-13 16:01:22 +0100220
Laura Abbott9adeb8e2016-08-09 18:25:26 -0700221static bool is_el1_instruction_abort(unsigned int esr)
222{
223 return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_CUR;
224}
225
Anshuman Khandualdbfe3822018-09-22 21:09:53 +0530226static inline bool is_el1_permission_fault(unsigned long addr, unsigned int esr,
227 struct pt_regs *regs)
Stephen Boydb824b932017-04-05 12:18:31 -0700228{
229 unsigned int ec = ESR_ELx_EC(esr);
230 unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;
231
232 if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
233 return false;
234
235 if (fsc_type == ESR_ELx_FSC_PERM)
236 return true;
237
Andrey Konovalov356607f2018-12-28 00:30:27 -0800238 if (is_ttbr0_addr(addr) && system_uses_ttbr0_pan())
Stephen Boydb824b932017-04-05 12:18:31 -0700239 return fsc_type == ESR_ELx_FSC_FAULT &&
240 (regs->pstate & PSR_PAN_BIT);
241
242 return false;
243}
244
Mark Rutlandc870f142018-05-21 14:14:51 +0100245static void die_kernel_fault(const char *msg, unsigned long addr,
246 unsigned int esr, struct pt_regs *regs)
247{
248 bust_spinlocks(1);
249
250 pr_alert("Unable to handle kernel %s at virtual address %016lx\n", msg,
251 addr);
252
253 mem_abort_decode(esr);
254
255 show_pte(addr);
256 die("Oops", regs, esr);
257 bust_spinlocks(0);
258 do_exit(SIGKILL);
259}
260
Kristina Martsenko67ce16e2017-06-09 16:35:52 +0100261static void __do_kernel_fault(unsigned long addr, unsigned int esr,
262 struct pt_regs *regs)
Catalin Marinas1d18c472012-03-05 11:49:27 +0000263{
Stephen Boydb824b932017-04-05 12:18:31 -0700264 const char *msg;
265
Catalin Marinas1d18c472012-03-05 11:49:27 +0000266 /*
267 * Are we prepared to handle this kernel fault?
Laura Abbott9adeb8e2016-08-09 18:25:26 -0700268 * We are almost certainly not prepared to handle instruction faults.
Catalin Marinas1d18c472012-03-05 11:49:27 +0000269 */
Laura Abbott9adeb8e2016-08-09 18:25:26 -0700270 if (!is_el1_instruction_abort(esr) && fixup_exception(regs))
Catalin Marinas1d18c472012-03-05 11:49:27 +0000271 return;
272
Anshuman Khandualdbfe3822018-09-22 21:09:53 +0530273 if (is_el1_permission_fault(addr, esr, regs)) {
Stephen Boydb824b932017-04-05 12:18:31 -0700274 if (esr & ESR_ELx_WNR)
275 msg = "write to read-only memory";
276 else
277 msg = "read from unreadable memory";
278 } else if (addr < PAGE_SIZE) {
279 msg = "NULL pointer dereference";
280 } else {
281 msg = "paging request";
282 }
283
Mark Rutlandc870f142018-05-21 14:14:51 +0100284 die_kernel_fault(msg, addr, esr, regs);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000285}
286
Eric W. Biedermanf29ad202018-09-22 09:37:55 +0200287static void set_thread_esr(unsigned long address, unsigned int esr)
Catalin Marinas1d18c472012-03-05 11:49:27 +0000288{
Eric W. Biedermanf29ad202018-09-22 09:37:55 +0200289 current->thread.fault_address = address;
Peter Maydellcc198462018-05-22 17:11:20 +0100290
291 /*
292 * If the faulting address is in the kernel, we must sanitize the ESR.
293 * From userspace's point of view, kernel-only mappings don't exist
294 * at all, so we report them as level 0 translation faults.
295 * (This is not quite the way that "no mapping there at all" behaves:
296 * an alignment fault not caused by the memory type would take
297 * precedence over translation fault for a real access to empty
298 * space. Unfortunately we can't easily distinguish "alignment fault
299 * not caused by memory type" from "alignment fault caused by memory
300 * type", so we ignore this wrinkle and just return the translation
301 * fault.)
302 */
Andrey Konovalov356607f2018-12-28 00:30:27 -0800303 if (!is_ttbr0_addr(current->thread.fault_address)) {
Peter Maydellcc198462018-05-22 17:11:20 +0100304 switch (ESR_ELx_EC(esr)) {
305 case ESR_ELx_EC_DABT_LOW:
306 /*
307 * These bits provide only information about the
308 * faulting instruction, which userspace knows already.
309 * We explicitly clear bits which are architecturally
310 * RES0 in case they are given meanings in future.
311 * We always report the ESR as if the fault was taken
312 * to EL1 and so ISV and the bits in ISS[23:14] are
313 * clear. (In fact it always will be a fault to EL1.)
314 */
315 esr &= ESR_ELx_EC_MASK | ESR_ELx_IL |
316 ESR_ELx_CM | ESR_ELx_WNR;
317 esr |= ESR_ELx_FSC_FAULT;
318 break;
319 case ESR_ELx_EC_IABT_LOW:
320 /*
321 * Claim a level 0 translation fault.
322 * All other bits are architecturally RES0 for faults
323 * reported with that DFSC value, so we clear them.
324 */
325 esr &= ESR_ELx_EC_MASK | ESR_ELx_IL;
326 esr |= ESR_ELx_FSC_FAULT;
327 break;
328 default:
329 /*
330 * This should never happen (entry.S only brings us
331 * into this code for insn and data aborts from a lower
332 * exception level). Fail safe by not providing an ESR
333 * context record at all.
334 */
335 WARN(1, "ESR 0x%x is not DABT or IABT from EL0\n", esr);
336 esr = 0;
337 break;
338 }
339 }
340
Will Deacon92ff0672018-02-20 14:53:22 +0000341 current->thread.fault_code = esr;
Catalin Marinas1d18c472012-03-05 11:49:27 +0000342}
343
Catalin Marinas59f67e12013-09-16 15:18:28 +0100344static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
Catalin Marinas1d18c472012-03-05 11:49:27 +0000345{
Catalin Marinas1d18c472012-03-05 11:49:27 +0000346 /*
347 * If we are in kernel mode at this point, we have no context to
348 * handle this fault with.
349 */
Victor Kamensky09a6adf2017-04-03 22:51:01 -0700350 if (user_mode(regs)) {
Will Deacon92ff0672018-02-20 14:53:22 +0000351 const struct fault_info *inf = esr_to_fault_info(esr);
Eric W. Biederman3eb0f512018-04-17 15:26:37 -0500352
Eric W. Biedermaneffb0932018-09-22 10:05:41 +0200353 set_thread_esr(addr, esr);
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200354 arm64_force_sig_fault(inf->sig, inf->code, (void __user *)addr,
355 inf->name);
Will Deacon92ff0672018-02-20 14:53:22 +0000356 } else {
Kristina Martsenko67ce16e2017-06-09 16:35:52 +0100357 __do_kernel_fault(addr, esr, regs);
Will Deacon92ff0672018-02-20 14:53:22 +0000358 }
Catalin Marinas1d18c472012-03-05 11:49:27 +0000359}
360
361#define VM_FAULT_BADMAP 0x010000
362#define VM_FAULT_BADACCESS 0x020000
363
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700364static vm_fault_t __do_page_fault(struct mm_struct *mm, unsigned long addr,
Anshuman Khandual61681032019-06-03 12:11:23 +0530365 unsigned int mm_flags, unsigned long vm_flags)
Catalin Marinas1d18c472012-03-05 11:49:27 +0000366{
Anshuman Khandual47452242019-06-07 14:43:06 +0530367 struct vm_area_struct *vma = find_vma(mm, addr);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000368
Catalin Marinas1d18c472012-03-05 11:49:27 +0000369 if (unlikely(!vma))
Anshuman Khandual47452242019-06-07 14:43:06 +0530370 return VM_FAULT_BADMAP;
Catalin Marinas1d18c472012-03-05 11:49:27 +0000371
372 /*
373 * Ok, we have a good vm_area for this memory access, so we can handle
374 * it.
375 */
Anshuman Khandual47452242019-06-07 14:43:06 +0530376 if (unlikely(vma->vm_start > addr)) {
377 if (!(vma->vm_flags & VM_GROWSDOWN))
378 return VM_FAULT_BADMAP;
379 if (expand_stack(vma, addr))
380 return VM_FAULT_BADMAP;
381 }
382
Will Deacondb6f4102013-07-19 15:37:12 +0100383 /*
384 * Check that the permissions on the VMA allow for the fault which
Catalin Marinascab15ce2016-08-11 18:44:50 +0100385 * occurred.
Will Deacondb6f4102013-07-19 15:37:12 +0100386 */
Anshuman Khandual47452242019-06-07 14:43:06 +0530387 if (!(vma->vm_flags & vm_flags))
388 return VM_FAULT_BADACCESS;
Kirill A. Shutemovdcddffd2016-07-26 15:25:18 -0700389 return handle_mm_fault(vma, addr & PAGE_MASK, mm_flags);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000390}
391
Mark Rutland541ec872016-05-31 12:33:03 +0100392static bool is_el0_instruction_abort(unsigned int esr)
393{
394 return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW;
395}
396
Anshuman Khandualc49bd022019-06-07 14:43:05 +0530397/*
398 * Note: not valid for EL1 DC IVAC, but we never use that such that it
399 * should fault. EL0 cannot issue DC IVAC (undef).
400 */
401static bool is_write_abort(unsigned int esr)
402{
403 return (esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM);
404}
405
Catalin Marinas1d18c472012-03-05 11:49:27 +0000406static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
407 struct pt_regs *regs)
408{
Eric W. Biederman2d2837f2018-09-22 10:16:42 +0200409 const struct fault_info *inf;
Anshuman Khandual61681032019-06-03 12:11:23 +0530410 struct mm_struct *mm = current->mm;
Souptick Joarder50a7ca32018-08-17 15:44:47 -0700411 vm_fault_t fault, major = 0;
Catalin Marinascab15ce2016-08-11 18:44:50 +0100412 unsigned long vm_flags = VM_READ | VM_WRITE;
Will Deacondb6f4102013-07-19 15:37:12 +0100413 unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
414
Anshuman Khandualb98cca42019-07-16 16:28:00 -0700415 if (kprobe_page_fault(regs, esr))
Sandeepa Prabhu2dd0e8d2016-07-08 12:35:48 -0400416 return 0;
417
Catalin Marinas1d18c472012-03-05 11:49:27 +0000418 /*
419 * If we're in an interrupt or have no user context, we must not take
420 * the fault.
421 */
David Hildenbrand70ffdb92015-05-11 17:52:11 +0200422 if (faulthandler_disabled() || !mm)
Catalin Marinas1d18c472012-03-05 11:49:27 +0000423 goto no_context;
424
Johannes Weiner759496b2013-09-12 15:13:39 -0700425 if (user_mode(regs))
426 mm_flags |= FAULT_FLAG_USER;
427
Mark Rutland541ec872016-05-31 12:33:03 +0100428 if (is_el0_instruction_abort(esr)) {
Johannes Weiner759496b2013-09-12 15:13:39 -0700429 vm_flags = VM_EXEC;
Anshuman Khandual01de1772019-05-05 09:45:12 +0530430 mm_flags |= FAULT_FLAG_INSTRUCTION;
Anshuman Khandualc49bd022019-06-07 14:43:05 +0530431 } else if (is_write_abort(esr)) {
Johannes Weiner759496b2013-09-12 15:13:39 -0700432 vm_flags = VM_WRITE;
433 mm_flags |= FAULT_FLAG_WRITE;
434 }
435
Andrey Konovalov356607f2018-12-28 00:30:27 -0800436 if (is_ttbr0_addr(addr) && is_el1_permission_fault(addr, esr, regs)) {
James Morsee19a6ee2016-06-20 18:28:01 +0100437 /* regs->orig_addr_limit may be 0 if we entered from EL0 */
438 if (regs->orig_addr_limit == KERNEL_DS)
Mark Rutlandc870f142018-05-21 14:14:51 +0100439 die_kernel_fault("access to user memory with fs=KERNEL_DS",
440 addr, esr, regs);
James Morse70544192016-02-05 14:58:50 +0000441
Laura Abbott9adeb8e2016-08-09 18:25:26 -0700442 if (is_el1_instruction_abort(esr))
Mark Rutlandc870f142018-05-21 14:14:51 +0100443 die_kernel_fault("execution of user memory",
444 addr, esr, regs);
Laura Abbott9adeb8e2016-08-09 18:25:26 -0700445
James Morse57f49592016-02-05 14:58:48 +0000446 if (!search_exception_tables(regs->pc))
Mark Rutlandc870f142018-05-21 14:14:51 +0100447 die_kernel_fault("access to user memory outside uaccess routines",
448 addr, esr, regs);
James Morse57f49592016-02-05 14:58:48 +0000449 }
James Morse338d4f42015-07-22 19:05:54 +0100450
Punit Agrawal0e3a9022017-06-08 18:25:28 +0100451 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
452
James Morse338d4f42015-07-22 19:05:54 +0100453 /*
Catalin Marinas1d18c472012-03-05 11:49:27 +0000454 * As per x86, we may deadlock here. However, since the kernel only
455 * validly references user space from well defined areas of the code,
456 * we can bug out early if this is from code which shouldn't.
457 */
458 if (!down_read_trylock(&mm->mmap_sem)) {
459 if (!user_mode(regs) && !search_exception_tables(regs->pc))
460 goto no_context;
461retry:
462 down_read(&mm->mmap_sem);
463 } else {
464 /*
465 * The above down_read_trylock() might have succeeded in which
466 * case, we'll have missed the might_sleep() from down_read().
467 */
468 might_sleep();
469#ifdef CONFIG_DEBUG_VM
Anshuman Khanduala0509312019-06-03 12:11:22 +0530470 if (!user_mode(regs) && !search_exception_tables(regs->pc)) {
471 up_read(&mm->mmap_sem);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000472 goto no_context;
Anshuman Khanduala0509312019-06-03 12:11:22 +0530473 }
Catalin Marinas1d18c472012-03-05 11:49:27 +0000474#endif
475 }
476
Anshuman Khandual61681032019-06-03 12:11:23 +0530477 fault = __do_page_fault(mm, addr, mm_flags, vm_flags);
Punit Agrawal0e3a9022017-06-08 18:25:28 +0100478 major |= fault & VM_FAULT_MAJOR;
479
480 if (fault & VM_FAULT_RETRY) {
481 /*
482 * If we need to retry but a fatal signal is pending,
483 * handle the signal first. We do not need to release
484 * the mmap_sem because it would already be released
485 * in __lock_page_or_retry in mm/filemap.c.
486 */
Mark Rutland289d07a2017-07-11 15:19:22 +0100487 if (fatal_signal_pending(current)) {
488 if (!user_mode(regs))
489 goto no_context;
Punit Agrawal0e3a9022017-06-08 18:25:28 +0100490 return 0;
Mark Rutland289d07a2017-07-11 15:19:22 +0100491 }
Punit Agrawal0e3a9022017-06-08 18:25:28 +0100492
493 /*
494 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
495 * starvation.
496 */
497 if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
498 mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
499 mm_flags |= FAULT_FLAG_TRIED;
500 goto retry;
501 }
502 }
503 up_read(&mm->mmap_sem);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000504
505 /*
Punit Agrawal0e3a9022017-06-08 18:25:28 +0100506 * Handle the "normal" (no error) case first.
Catalin Marinas1d18c472012-03-05 11:49:27 +0000507 */
Punit Agrawal0e3a9022017-06-08 18:25:28 +0100508 if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
509 VM_FAULT_BADACCESS)))) {
510 /*
511 * Major/minor page fault accounting is only done
512 * once. If we go through a retry, it is extremely
513 * likely that the page will be found in page cache at
514 * that point.
515 */
516 if (major) {
Anshuman Khandual61681032019-06-03 12:11:23 +0530517 current->maj_flt++;
Catalin Marinas1d18c472012-03-05 11:49:27 +0000518 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
519 addr);
520 } else {
Anshuman Khandual61681032019-06-03 12:11:23 +0530521 current->min_flt++;
Catalin Marinas1d18c472012-03-05 11:49:27 +0000522 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
523 addr);
524 }
Catalin Marinas1d18c472012-03-05 11:49:27 +0000525
Catalin Marinas1d18c472012-03-05 11:49:27 +0000526 return 0;
Punit Agrawal0e3a9022017-06-08 18:25:28 +0100527 }
Catalin Marinas1d18c472012-03-05 11:49:27 +0000528
Johannes Weiner87134102013-09-12 15:13:38 -0700529 /*
530 * If we are in kernel mode at this point, we have no context to
531 * handle this fault with.
532 */
533 if (!user_mode(regs))
534 goto no_context;
535
Catalin Marinas1d18c472012-03-05 11:49:27 +0000536 if (fault & VM_FAULT_OOM) {
537 /*
538 * We ran out of memory, call the OOM killer, and return to
539 * userspace (which will retry the fault, or kill us if we got
540 * oom-killed).
541 */
542 pagefault_out_of_memory();
543 return 0;
544 }
545
Eric W. Biederman2d2837f2018-09-22 10:16:42 +0200546 inf = esr_to_fault_info(esr);
Eric W. Biederman559d8d92018-09-22 10:18:42 +0200547 set_thread_esr(addr, esr);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000548 if (fault & VM_FAULT_SIGBUS) {
549 /*
550 * We had some memory, but were unable to successfully fix up
551 * this page fault.
552 */
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200553 arm64_force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)addr,
554 inf->name);
Eric W. Biederman9ea3a972018-09-22 09:46:39 +0200555 } else if (fault & (VM_FAULT_HWPOISON_LARGE | VM_FAULT_HWPOISON)) {
556 unsigned int lsb;
Will Deacon92ff0672018-02-20 14:53:22 +0000557
Eric W. Biederman9ea3a972018-09-22 09:46:39 +0200558 lsb = PAGE_SHIFT;
559 if (fault & VM_FAULT_HWPOISON_LARGE)
560 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
Will Deacon92ff0672018-02-20 14:53:22 +0000561
Eric W. Biedermanb4d55572018-09-22 10:37:15 +0200562 arm64_force_sig_mceerr(BUS_MCEERR_AR, (void __user *)addr, lsb,
563 inf->name);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000564 } else {
565 /*
566 * Something tried to access memory that isn't in our memory
567 * map.
568 */
Eric W. Biedermanfeca3552018-09-22 10:26:57 +0200569 arm64_force_sig_fault(SIGSEGV,
570 fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR,
571 (void __user *)addr,
572 inf->name);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000573 }
574
Catalin Marinas1d18c472012-03-05 11:49:27 +0000575 return 0;
576
577no_context:
Kristina Martsenko67ce16e2017-06-09 16:35:52 +0100578 __do_kernel_fault(addr, esr, regs);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000579 return 0;
580}
581
Catalin Marinas1d18c472012-03-05 11:49:27 +0000582static int __kprobes do_translation_fault(unsigned long addr,
583 unsigned int esr,
584 struct pt_regs *regs)
585{
Andrey Konovalov356607f2018-12-28 00:30:27 -0800586 if (is_ttbr0_addr(addr))
Catalin Marinas1d18c472012-03-05 11:49:27 +0000587 return do_page_fault(addr, esr, regs);
588
589 do_bad_area(addr, esr, regs);
590 return 0;
591}
592
EunTaik Lee52d75232016-02-16 04:44:35 +0000593static 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
Catalin Marinas1d18c472012-03-05 11:49:27 +0000600static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
601{
Will Deaconf67d5c42017-09-22 11:01:26 +0100602 return 1; /* "fault" */
Catalin Marinas1d18c472012-03-05 11:49:27 +0000603}
604
Tyler Baicar32015c22017-06-21 12:17:08 -0600605static int do_sea(unsigned long addr, unsigned int esr, struct pt_regs *regs)
606{
Tyler Baicar32015c22017-06-21 12:17:08 -0600607 const struct fault_info *inf;
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200608 void __user *siaddr;
Tyler Baicar32015c22017-06-21 12:17:08 -0600609
610 inf = esr_to_fault_info(esr);
Tyler Baicar32015c22017-06-21 12:17:08 -0600611
Tyler Baicar7edda082017-06-21 12:17:09 -0600612 /*
James Morsed44f1b82019-01-29 18:48:50 +0000613 * Return value ignored as we rely on signal merging.
614 * Future patches will make this more robust.
Tyler Baicar7edda082017-06-21 12:17:09 -0600615 */
James Morsed44f1b82019-01-29 18:48:50 +0000616 apei_claim_sea(regs);
Tyler Baicar7edda082017-06-21 12:17:09 -0600617
Tyler Baicar32015c22017-06-21 12:17:08 -0600618 if (esr & ESR_ELx_FnV)
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200619 siaddr = NULL;
Tyler Baicar32015c22017-06-21 12:17:08 -0600620 else
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200621 siaddr = (void __user *)addr;
622 arm64_notify_die(inf->name, regs, inf->sig, inf->code, siaddr, esr);
Tyler Baicar32015c22017-06-21 12:17:08 -0600623
Dongjiu Gengfaa75e12017-12-13 18:36:47 +0800624 return 0;
Tyler Baicar32015c22017-06-21 12:17:08 -0600625}
626
Victor Kamensky09a6adf2017-04-03 22:51:01 -0700627static const struct fault_info fault_info[] = {
Dave Martinaf40ff62018-03-08 17:41:05 +0000628 { do_bad, SIGKILL, SI_KERNEL, "ttbr address size fault" },
629 { do_bad, SIGKILL, SI_KERNEL, "level 1 address size fault" },
630 { do_bad, SIGKILL, SI_KERNEL, "level 2 address size fault" },
631 { do_bad, SIGKILL, SI_KERNEL, "level 3 address size fault" },
Will Deacon7f73f7a2014-11-21 14:22:22 +0000632 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" },
Catalin Marinas1d18c472012-03-05 11:49:27 +0000633 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
634 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
Will Deacon760bfb42017-09-29 12:27:41 +0100635 { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
Dave Martinaf40ff62018-03-08 17:41:05 +0000636 { do_bad, SIGKILL, SI_KERNEL, "unknown 8" },
Steve Capper084bd292013-04-10 13:48:00 +0100637 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
638 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
Catalin Marinas1d18c472012-03-05 11:49:27 +0000639 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
Dave Martinaf40ff62018-03-08 17:41:05 +0000640 { do_bad, SIGKILL, SI_KERNEL, "unknown 12" },
Steve Capper084bd292013-04-10 13:48:00 +0100641 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
642 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
Catalin Marinas1d18c472012-03-05 11:49:27 +0000643 { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
Dave Martinaf40ff62018-03-08 17:41:05 +0000644 { do_sea, SIGBUS, BUS_OBJERR, "synchronous external abort" },
645 { do_bad, SIGKILL, SI_KERNEL, "unknown 17" },
646 { do_bad, SIGKILL, SI_KERNEL, "unknown 18" },
647 { do_bad, SIGKILL, SI_KERNEL, "unknown 19" },
648 { do_sea, SIGKILL, SI_KERNEL, "level 0 (translation table walk)" },
649 { do_sea, SIGKILL, SI_KERNEL, "level 1 (translation table walk)" },
650 { do_sea, SIGKILL, SI_KERNEL, "level 2 (translation table walk)" },
651 { do_sea, SIGKILL, SI_KERNEL, "level 3 (translation table walk)" },
652 { do_sea, SIGBUS, BUS_OBJERR, "synchronous parity or ECC error" }, // Reserved when RAS is implemented
653 { do_bad, SIGKILL, SI_KERNEL, "unknown 25" },
654 { do_bad, SIGKILL, SI_KERNEL, "unknown 26" },
655 { do_bad, SIGKILL, SI_KERNEL, "unknown 27" },
656 { do_sea, SIGKILL, SI_KERNEL, "level 0 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
657 { do_sea, SIGKILL, SI_KERNEL, "level 1 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
658 { do_sea, SIGKILL, SI_KERNEL, "level 2 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
659 { do_sea, SIGKILL, SI_KERNEL, "level 3 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
660 { do_bad, SIGKILL, SI_KERNEL, "unknown 32" },
EunTaik Lee52d75232016-02-16 04:44:35 +0000661 { do_alignment_fault, SIGBUS, BUS_ADRALN, "alignment fault" },
Dave Martinaf40ff62018-03-08 17:41:05 +0000662 { do_bad, SIGKILL, SI_KERNEL, "unknown 34" },
663 { do_bad, SIGKILL, SI_KERNEL, "unknown 35" },
664 { do_bad, SIGKILL, SI_KERNEL, "unknown 36" },
665 { do_bad, SIGKILL, SI_KERNEL, "unknown 37" },
666 { do_bad, SIGKILL, SI_KERNEL, "unknown 38" },
667 { do_bad, SIGKILL, SI_KERNEL, "unknown 39" },
668 { do_bad, SIGKILL, SI_KERNEL, "unknown 40" },
669 { do_bad, SIGKILL, SI_KERNEL, "unknown 41" },
670 { do_bad, SIGKILL, SI_KERNEL, "unknown 42" },
671 { do_bad, SIGKILL, SI_KERNEL, "unknown 43" },
672 { do_bad, SIGKILL, SI_KERNEL, "unknown 44" },
673 { do_bad, SIGKILL, SI_KERNEL, "unknown 45" },
674 { do_bad, SIGKILL, SI_KERNEL, "unknown 46" },
675 { do_bad, SIGKILL, SI_KERNEL, "unknown 47" },
676 { do_bad, SIGKILL, SI_KERNEL, "TLB conflict abort" },
677 { do_bad, SIGKILL, SI_KERNEL, "Unsupported atomic hardware update fault" },
678 { do_bad, SIGKILL, SI_KERNEL, "unknown 50" },
679 { do_bad, SIGKILL, SI_KERNEL, "unknown 51" },
680 { do_bad, SIGKILL, SI_KERNEL, "implementation fault (lockdown abort)" },
681 { do_bad, SIGBUS, BUS_OBJERR, "implementation fault (unsupported exclusive)" },
682 { do_bad, SIGKILL, SI_KERNEL, "unknown 54" },
683 { do_bad, SIGKILL, SI_KERNEL, "unknown 55" },
684 { do_bad, SIGKILL, SI_KERNEL, "unknown 56" },
685 { do_bad, SIGKILL, SI_KERNEL, "unknown 57" },
686 { do_bad, SIGKILL, SI_KERNEL, "unknown 58" },
687 { do_bad, SIGKILL, SI_KERNEL, "unknown 59" },
688 { do_bad, SIGKILL, SI_KERNEL, "unknown 60" },
689 { do_bad, SIGKILL, SI_KERNEL, "section domain fault" },
690 { do_bad, SIGKILL, SI_KERNEL, "page domain fault" },
691 { do_bad, SIGKILL, SI_KERNEL, "unknown 63" },
Catalin Marinas1d18c472012-03-05 11:49:27 +0000692};
693
Catalin Marinas1d18c472012-03-05 11:49:27 +0000694asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
695 struct pt_regs *regs)
696{
Victor Kamensky09a6adf2017-04-03 22:51:01 -0700697 const struct fault_info *inf = esr_to_fault_info(esr);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000698
699 if (!inf->fn(addr, esr, regs))
700 return;
701
Will Deacon1049c302018-02-20 14:41:02 +0000702 if (!user_mode(regs)) {
703 pr_alert("Unhandled fault at 0x%016lx\n", addr);
704 mem_abort_decode(esr);
Will Deacon80b6eb02017-10-31 15:56:11 +0000705 show_pte(addr);
Will Deacon1049c302018-02-20 14:41:02 +0000706 }
Mark Rutland42dbf542017-10-19 11:19:55 +0100707
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200708 arm64_notify_die(inf->name, regs,
709 inf->sig, inf->code, (void __user *)addr, esr);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000710}
711
Will Deacon30d88c02018-02-02 17:31:40 +0000712asmlinkage void __exception do_el0_irq_bp_hardening(void)
713{
714 /* PC has already been checked in entry.S */
715 arm64_apply_bp_hardening();
716}
717
Will Deacon0f15adb2018-01-03 11:17:58 +0000718asmlinkage void __exception do_el0_ia_bp_hardening(unsigned long addr,
719 unsigned int esr,
720 struct pt_regs *regs)
721{
722 /*
723 * We've taken an instruction abort from userspace and not yet
724 * re-enabled IRQs. If the address is a kernel address, apply
725 * BP hardening prior to enabling IRQs and pre-emption.
726 */
Andrey Konovalov356607f2018-12-28 00:30:27 -0800727 if (!is_ttbr0_addr(addr))
Will Deacon0f15adb2018-01-03 11:17:58 +0000728 arm64_apply_bp_hardening();
729
Julien Thierry9a0c0322018-08-28 16:51:15 +0100730 local_daif_restore(DAIF_PROCCTX);
Will Deacon0f15adb2018-01-03 11:17:58 +0000731 do_mem_abort(addr, esr, regs);
732}
733
734
Catalin Marinas1d18c472012-03-05 11:49:27 +0000735asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
736 unsigned int esr,
737 struct pt_regs *regs)
738{
Will Deacon5dfc6ed2018-02-02 17:31:39 +0000739 if (user_mode(regs)) {
Andrey Konovalov356607f2018-12-28 00:30:27 -0800740 if (!is_ttbr0_addr(instruction_pointer(regs)))
Will Deacon5dfc6ed2018-02-02 17:31:39 +0000741 arm64_apply_bp_hardening();
Julien Thierry9a0c0322018-08-28 16:51:15 +0100742 local_daif_restore(DAIF_PROCCTX);
Will Deacon5dfc6ed2018-02-02 17:31:39 +0000743 }
744
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200745 arm64_notify_die("SP/PC alignment exception", regs,
746 SIGBUS, BUS_ADRALN, (void __user *)addr, esr);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000747}
748
Dave P Martin9fb74102015-07-24 16:37:48 +0100749int __init early_brk64(unsigned long addr, unsigned int esr,
750 struct pt_regs *regs);
751
752/*
753 * __refdata because early_brk64 is __init, but the reference to it is
754 * clobbered at arch_initcall time.
755 * See traps.c and debug-monitors.c:debug_traps_init().
756 */
757static struct fault_info __refdata debug_fault_info[] = {
Catalin Marinas1d18c472012-03-05 11:49:27 +0000758 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
759 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
760 { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
Dave Martinaf40ff62018-03-08 17:41:05 +0000761 { do_bad, SIGKILL, SI_KERNEL, "unknown 3" },
Catalin Marinas1d18c472012-03-05 11:49:27 +0000762 { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
Dave Martinaf40ff62018-03-08 17:41:05 +0000763 { do_bad, SIGKILL, SI_KERNEL, "aarch32 vector catch" },
Dave P Martin9fb74102015-07-24 16:37:48 +0100764 { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
Dave Martinaf40ff62018-03-08 17:41:05 +0000765 { do_bad, SIGKILL, SI_KERNEL, "unknown 7" },
Catalin Marinas1d18c472012-03-05 11:49:27 +0000766};
767
768void __init hook_debug_fault_code(int nr,
769 int (*fn)(unsigned long, unsigned int, struct pt_regs *),
770 int sig, int code, const char *name)
771{
772 BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
773
774 debug_fault_info[nr].fn = fn;
775 debug_fault_info[nr].sig = sig;
776 debug_fault_info[nr].code = code;
777 debug_fault_info[nr].name = name;
778}
779
Will Deacon969f5ea2019-04-29 13:03:57 +0100780#ifdef CONFIG_ARM64_ERRATUM_1463225
781DECLARE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
782
783static int __exception
784cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
785{
786 if (user_mode(regs))
787 return 0;
788
789 if (!__this_cpu_read(__in_cortex_a76_erratum_1463225_wa))
790 return 0;
791
792 /*
793 * We've taken a dummy step exception from the kernel to ensure
794 * that interrupts are re-enabled on the syscall path. Return back
795 * to cortex_a76_erratum_1463225_svc_handler() with debug exceptions
796 * masked so that we can safely restore the mdscr and get on with
797 * handling the syscall.
798 */
799 regs->pstate |= PSR_D_BIT;
800 return 1;
801}
802#else
803static int __exception
804cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
805{
806 return 0;
807}
808#endif /* CONFIG_ARM64_ERRATUM_1463225 */
809
Will Deacon52c6d142019-02-25 12:06:43 +0000810asmlinkage void __exception do_debug_exception(unsigned long addr_if_watchpoint,
811 unsigned int esr,
812 struct pt_regs *regs)
Catalin Marinas1d18c472012-03-05 11:49:27 +0000813{
Anshuman Khandual359048f2018-09-22 21:09:54 +0530814 const struct fault_info *inf = esr_to_debug_fault_info(esr);
Will Deaconb9a4b9d2019-03-01 13:28:00 +0000815 unsigned long pc = instruction_pointer(regs);
Catalin Marinas1d18c472012-03-05 11:49:27 +0000816
Will Deacon969f5ea2019-04-29 13:03:57 +0100817 if (cortex_a76_erratum_1463225_debug_handler(regs))
818 return;
819
James Morse6afedcd2016-04-13 13:40:00 +0100820 /*
821 * Tell lockdep we disabled irqs in entry.S. Do nothing if they were
822 * already disabled to preserve the last enabled/disabled addresses.
823 */
824 if (interrupts_enabled(regs))
825 trace_hardirqs_off();
Catalin Marinas1d18c472012-03-05 11:49:27 +0000826
Will Deaconb9a4b9d2019-03-01 13:28:00 +0000827 if (user_mode(regs) && !is_ttbr0_addr(pc))
Will Deacon5dfc6ed2018-02-02 17:31:39 +0000828 arm64_apply_bp_hardening();
829
Will Deacon52c6d142019-02-25 12:06:43 +0000830 if (inf->fn(addr_if_watchpoint, esr, regs)) {
Eric W. Biederman6fa998e2018-09-21 17:24:40 +0200831 arm64_notify_die(inf->name, regs,
Will Deaconb9a4b9d2019-03-01 13:28:00 +0000832 inf->sig, inf->code, (void __user *)pc, esr);
James Morse6afedcd2016-04-13 13:40:00 +0100833 }
Catalin Marinas1d18c472012-03-05 11:49:27 +0000834
James Morse6afedcd2016-04-13 13:40:00 +0100835 if (interrupts_enabled(regs))
836 trace_hardirqs_on();
Catalin Marinas1d18c472012-03-05 11:49:27 +0000837}
Sandeepa Prabhu2dd0e8d2016-07-08 12:35:48 -0400838NOKPROBE_SYMBOL(do_debug_exception);