Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 2 | /* |
| 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 Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
James Morse | d44f1b8 | 2019-01-29 18:48:50 +0000 | [diff] [blame] | 10 | #include <linux/acpi.h> |
Paul Gortmaker | 0edfa83 | 2016-09-19 17:38:55 -0400 | [diff] [blame] | 11 | #include <linux/extable.h> |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 12 | #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 Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 19 | #include <linux/sched/signal.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 20 | #include <linux/sched/debug.h> |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 21 | #include <linux/highmem.h> |
| 22 | #include <linux/perf_event.h> |
James Morse | 7209c86 | 2016-10-18 11:27:47 +0100 | [diff] [blame] | 23 | #include <linux/preempt.h> |
Jonathan (Zhixiong) Zhang | e7c600f | 2017-06-08 18:25:27 +0100 | [diff] [blame] | 24 | #include <linux/hugetlb.h> |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 25 | |
James Morse | d44f1b8 | 2019-01-29 18:48:50 +0000 | [diff] [blame] | 26 | #include <asm/acpi.h> |
James Morse | 7209c86 | 2016-10-18 11:27:47 +0100 | [diff] [blame] | 27 | #include <asm/bug.h> |
Catalin Marinas | 3bbf715 | 2017-06-26 14:27:36 +0100 | [diff] [blame] | 28 | #include <asm/cmpxchg.h> |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 29 | #include <asm/cpufeature.h> |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 30 | #include <asm/exception.h> |
Julien Thierry | 9a0c032 | 2018-08-28 16:51:15 +0100 | [diff] [blame] | 31 | #include <asm/daifflags.h> |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 32 | #include <asm/debug-monitors.h> |
Catalin Marinas | 9141300 | 2014-04-06 23:04:12 +0100 | [diff] [blame] | 33 | #include <asm/esr.h> |
Andrey Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 34 | #include <asm/kasan.h> |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 35 | #include <asm/sysreg.h> |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 36 | #include <asm/system_misc.h> |
| 37 | #include <asm/pgtable.h> |
| 38 | #include <asm/tlbflush.h> |
Will Deacon | 92ff067 | 2018-02-20 14:53:22 +0000 | [diff] [blame] | 39 | #include <asm/traps.h> |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 40 | |
Victor Kamensky | 09a6adf | 2017-04-03 22:51:01 -0700 | [diff] [blame] | 41 | struct 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 | |
| 49 | static const struct fault_info fault_info[]; |
Anshuman Khandual | 359048f | 2018-09-22 21:09:54 +0530 | [diff] [blame] | 50 | static struct fault_info debug_fault_info[]; |
Victor Kamensky | 09a6adf | 2017-04-03 22:51:01 -0700 | [diff] [blame] | 51 | |
| 52 | static inline const struct fault_info *esr_to_fault_info(unsigned int esr) |
| 53 | { |
Anshuman Khandual | 00bbd5d | 2018-09-22 21:09:52 +0530 | [diff] [blame] | 54 | return fault_info + (esr & ESR_ELx_FSC); |
Victor Kamensky | 09a6adf | 2017-04-03 22:51:01 -0700 | [diff] [blame] | 55 | } |
Catalin Marinas | 3495386b | 2012-10-24 16:34:02 +0100 | [diff] [blame] | 56 | |
Anshuman Khandual | 359048f | 2018-09-22 21:09:54 +0530 | [diff] [blame] | 57 | static 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 Thierry | 1f9b893 | 2017-08-04 09:31:42 +0100 | [diff] [blame] | 62 | static 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 Rutland | 0a6de8b | 2017-10-02 12:42:00 +0100 | [diff] [blame] | 76 | pr_alert(" ISV = 0, ISS = 0x%08lx\n", esr & ESR_ELx_ISS_MASK); |
Julien Thierry | 1f9b893 | 2017-08-04 09:31:42 +0100 | [diff] [blame] | 77 | } |
| 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 Thierry | 1f9b893 | 2017-08-04 09:31:42 +0100 | [diff] [blame] | 84 | static void mem_abort_decode(unsigned int esr) |
| 85 | { |
| 86 | pr_alert("Mem abort info:\n"); |
| 87 | |
Mark Rutland | 42dbf54 | 2017-10-19 11:19:55 +0100 | [diff] [blame] | 88 | pr_alert(" ESR = 0x%08x\n", esr); |
Julien Thierry | 1f9b893 | 2017-08-04 09:31:42 +0100 | [diff] [blame] | 89 | 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 Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 103 | static inline bool is_ttbr0_addr(unsigned long addr) |
| 104 | { |
| 105 | /* entry assembly clears tags for TTBR0 addrs */ |
| 106 | return addr < TASK_SIZE; |
| 107 | } |
| 108 | |
| 109 | static 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 Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 115 | /* |
Kristina Martsenko | 67ce16e | 2017-06-09 16:35:52 +0100 | [diff] [blame] | 116 | * Dump out the page tables associated with 'addr' in the currently active mm. |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 117 | */ |
Will Deacon | 7048a59 | 2019-04-03 13:36:54 +0100 | [diff] [blame] | 118 | static void show_pte(unsigned long addr) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 119 | { |
Kristina Martsenko | 67ce16e | 2017-06-09 16:35:52 +0100 | [diff] [blame] | 120 | struct mm_struct *mm; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 121 | pgd_t *pgdp; |
| 122 | pgd_t pgd; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 123 | |
Andrey Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 124 | if (is_ttbr0_addr(addr)) { |
Kristina Martsenko | 67ce16e | 2017-06-09 16:35:52 +0100 | [diff] [blame] | 125 | /* 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 Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 132 | } else if (is_ttbr1_addr(addr)) { |
Kristina Martsenko | 67ce16e | 2017-06-09 16:35:52 +0100 | [diff] [blame] | 133 | /* TTBR1 */ |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 134 | mm = &init_mm; |
Kristina Martsenko | 67ce16e | 2017-06-09 16:35:52 +0100 | [diff] [blame] | 135 | } else { |
| 136 | pr_alert("[%016lx] address between user and kernel address ranges\n", |
| 137 | addr); |
| 138 | return; |
| 139 | } |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 140 | |
Will Deacon | 48caebf | 2019-05-14 12:25:28 +0100 | [diff] [blame] | 141 | pr_alert("%s pgtable: %luk pages, %u-bit VAs, pgdp=%016lx\n", |
Will Deacon | 1eb34b6 | 2017-05-15 15:23:58 +0100 | [diff] [blame] | 142 | mm == &init_mm ? "swapper" : "user", PAGE_SIZE / SZ_1K, |
Will Deacon | 48caebf | 2019-05-14 12:25:28 +0100 | [diff] [blame] | 143 | mm == &init_mm ? VA_BITS : (int)vabits_user, |
| 144 | (unsigned long)virt_to_phys(mm->pgd)); |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 145 | pgdp = pgd_offset(mm, addr); |
| 146 | pgd = READ_ONCE(*pgdp); |
| 147 | pr_alert("[%016lx] pgd=%016llx", addr, pgd_val(pgd)); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 148 | |
| 149 | do { |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 150 | pud_t *pudp, pud; |
| 151 | pmd_t *pmdp, pmd; |
| 152 | pte_t *ptep, pte; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 153 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 154 | if (pgd_none(pgd) || pgd_bad(pgd)) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 155 | break; |
| 156 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 157 | 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 Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 161 | break; |
| 162 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 163 | 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 Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 167 | break; |
| 168 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 169 | ptep = pte_offset_map(pmdp, addr); |
| 170 | pte = READ_ONCE(*ptep); |
| 171 | pr_cont(", pte=%016llx", pte_val(pte)); |
| 172 | pte_unmap(ptep); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 173 | } while(0); |
| 174 | |
Mark Rutland | 6ef4fb3 | 2017-01-03 14:27:26 +0000 | [diff] [blame] | 175 | pr_cont("\n"); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Catalin Marinas | 66dbd6e | 2016-04-13 16:01:22 +0100 | [diff] [blame] | 178 | /* |
| 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 | */ |
| 188 | int ptep_set_access_flags(struct vm_area_struct *vma, |
| 189 | unsigned long address, pte_t *ptep, |
| 190 | pte_t entry, int dirty) |
| 191 | { |
Catalin Marinas | 3bbf715 | 2017-06-26 14:27:36 +0100 | [diff] [blame] | 192 | pteval_t old_pteval, pteval; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 193 | pte_t pte = READ_ONCE(*ptep); |
Catalin Marinas | 66dbd6e | 2016-04-13 16:01:22 +0100 | [diff] [blame] | 194 | |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 195 | if (pte_same(pte, entry)) |
Catalin Marinas | 66dbd6e | 2016-04-13 16:01:22 +0100 | [diff] [blame] | 196 | return 0; |
| 197 | |
| 198 | /* only preserve the access flags and write permission */ |
Catalin Marinas | 73e86cb | 2017-07-04 19:04:18 +0100 | [diff] [blame] | 199 | pte_val(entry) &= PTE_RDONLY | PTE_AF | PTE_WRITE | PTE_DIRTY; |
Catalin Marinas | 66dbd6e | 2016-04-13 16:01:22 +0100 | [diff] [blame] | 200 | |
| 201 | /* |
| 202 | * Setting the flags must be done atomically to avoid racing with the |
Catalin Marinas | 6d33274 | 2017-07-25 14:53:03 +0100 | [diff] [blame] | 203 | * 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 Marinas | 66dbd6e | 2016-04-13 16:01:22 +0100 | [diff] [blame] | 206 | */ |
Catalin Marinas | 6d33274 | 2017-07-25 14:53:03 +0100 | [diff] [blame] | 207 | pte_val(entry) ^= PTE_RDONLY; |
Will Deacon | 20a004e | 2018-02-15 11:14:56 +0000 | [diff] [blame] | 208 | pteval = pte_val(pte); |
Catalin Marinas | 3bbf715 | 2017-06-26 14:27:36 +0100 | [diff] [blame] | 209 | 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 Marinas | 66dbd6e | 2016-04-13 16:01:22 +0100 | [diff] [blame] | 216 | |
| 217 | flush_tlb_fix_spurious_fault(vma, address); |
| 218 | return 1; |
| 219 | } |
Catalin Marinas | 66dbd6e | 2016-04-13 16:01:22 +0100 | [diff] [blame] | 220 | |
Laura Abbott | 9adeb8e | 2016-08-09 18:25:26 -0700 | [diff] [blame] | 221 | static bool is_el1_instruction_abort(unsigned int esr) |
| 222 | { |
| 223 | return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_CUR; |
| 224 | } |
| 225 | |
Anshuman Khandual | dbfe382 | 2018-09-22 21:09:53 +0530 | [diff] [blame] | 226 | static inline bool is_el1_permission_fault(unsigned long addr, unsigned int esr, |
| 227 | struct pt_regs *regs) |
Stephen Boyd | b824b93 | 2017-04-05 12:18:31 -0700 | [diff] [blame] | 228 | { |
| 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 Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 238 | if (is_ttbr0_addr(addr) && system_uses_ttbr0_pan()) |
Stephen Boyd | b824b93 | 2017-04-05 12:18:31 -0700 | [diff] [blame] | 239 | return fsc_type == ESR_ELx_FSC_FAULT && |
| 240 | (regs->pstate & PSR_PAN_BIT); |
| 241 | |
| 242 | return false; |
| 243 | } |
| 244 | |
Mark Rutland | c870f14 | 2018-05-21 14:14:51 +0100 | [diff] [blame] | 245 | static 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 Martsenko | 67ce16e | 2017-06-09 16:35:52 +0100 | [diff] [blame] | 261 | static void __do_kernel_fault(unsigned long addr, unsigned int esr, |
| 262 | struct pt_regs *regs) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 263 | { |
Stephen Boyd | b824b93 | 2017-04-05 12:18:31 -0700 | [diff] [blame] | 264 | const char *msg; |
| 265 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 266 | /* |
| 267 | * Are we prepared to handle this kernel fault? |
Laura Abbott | 9adeb8e | 2016-08-09 18:25:26 -0700 | [diff] [blame] | 268 | * We are almost certainly not prepared to handle instruction faults. |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 269 | */ |
Laura Abbott | 9adeb8e | 2016-08-09 18:25:26 -0700 | [diff] [blame] | 270 | if (!is_el1_instruction_abort(esr) && fixup_exception(regs)) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 271 | return; |
| 272 | |
Anshuman Khandual | dbfe382 | 2018-09-22 21:09:53 +0530 | [diff] [blame] | 273 | if (is_el1_permission_fault(addr, esr, regs)) { |
Stephen Boyd | b824b93 | 2017-04-05 12:18:31 -0700 | [diff] [blame] | 274 | 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 Rutland | c870f14 | 2018-05-21 14:14:51 +0100 | [diff] [blame] | 284 | die_kernel_fault(msg, addr, esr, regs); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Eric W. Biederman | f29ad20 | 2018-09-22 09:37:55 +0200 | [diff] [blame] | 287 | static void set_thread_esr(unsigned long address, unsigned int esr) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 288 | { |
Eric W. Biederman | f29ad20 | 2018-09-22 09:37:55 +0200 | [diff] [blame] | 289 | current->thread.fault_address = address; |
Peter Maydell | cc19846 | 2018-05-22 17:11:20 +0100 | [diff] [blame] | 290 | |
| 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 Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 303 | if (!is_ttbr0_addr(current->thread.fault_address)) { |
Peter Maydell | cc19846 | 2018-05-22 17:11:20 +0100 | [diff] [blame] | 304 | 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 Deacon | 92ff067 | 2018-02-20 14:53:22 +0000 | [diff] [blame] | 341 | current->thread.fault_code = esr; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Catalin Marinas | 59f67e1 | 2013-09-16 15:18:28 +0100 | [diff] [blame] | 344 | static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 345 | { |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 346 | /* |
| 347 | * If we are in kernel mode at this point, we have no context to |
| 348 | * handle this fault with. |
| 349 | */ |
Victor Kamensky | 09a6adf | 2017-04-03 22:51:01 -0700 | [diff] [blame] | 350 | if (user_mode(regs)) { |
Will Deacon | 92ff067 | 2018-02-20 14:53:22 +0000 | [diff] [blame] | 351 | const struct fault_info *inf = esr_to_fault_info(esr); |
Eric W. Biederman | 3eb0f51 | 2018-04-17 15:26:37 -0500 | [diff] [blame] | 352 | |
Eric W. Biederman | effb093 | 2018-09-22 10:05:41 +0200 | [diff] [blame] | 353 | set_thread_esr(addr, esr); |
Eric W. Biederman | feca355 | 2018-09-22 10:26:57 +0200 | [diff] [blame] | 354 | arm64_force_sig_fault(inf->sig, inf->code, (void __user *)addr, |
| 355 | inf->name); |
Will Deacon | 92ff067 | 2018-02-20 14:53:22 +0000 | [diff] [blame] | 356 | } else { |
Kristina Martsenko | 67ce16e | 2017-06-09 16:35:52 +0100 | [diff] [blame] | 357 | __do_kernel_fault(addr, esr, regs); |
Will Deacon | 92ff067 | 2018-02-20 14:53:22 +0000 | [diff] [blame] | 358 | } |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | #define VM_FAULT_BADMAP 0x010000 |
| 362 | #define VM_FAULT_BADACCESS 0x020000 |
| 363 | |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 364 | static vm_fault_t __do_page_fault(struct mm_struct *mm, unsigned long addr, |
Anshuman Khandual | 6168103 | 2019-06-03 12:11:23 +0530 | [diff] [blame] | 365 | unsigned int mm_flags, unsigned long vm_flags) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 366 | { |
Anshuman Khandual | 4745224 | 2019-06-07 14:43:06 +0530 | [diff] [blame] | 367 | struct vm_area_struct *vma = find_vma(mm, addr); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 368 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 369 | if (unlikely(!vma)) |
Anshuman Khandual | 4745224 | 2019-06-07 14:43:06 +0530 | [diff] [blame] | 370 | return VM_FAULT_BADMAP; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * Ok, we have a good vm_area for this memory access, so we can handle |
| 374 | * it. |
| 375 | */ |
Anshuman Khandual | 4745224 | 2019-06-07 14:43:06 +0530 | [diff] [blame] | 376 | 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 Deacon | db6f410 | 2013-07-19 15:37:12 +0100 | [diff] [blame] | 383 | /* |
| 384 | * Check that the permissions on the VMA allow for the fault which |
Catalin Marinas | cab15ce | 2016-08-11 18:44:50 +0100 | [diff] [blame] | 385 | * occurred. |
Will Deacon | db6f410 | 2013-07-19 15:37:12 +0100 | [diff] [blame] | 386 | */ |
Anshuman Khandual | 4745224 | 2019-06-07 14:43:06 +0530 | [diff] [blame] | 387 | if (!(vma->vm_flags & vm_flags)) |
| 388 | return VM_FAULT_BADACCESS; |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 389 | return handle_mm_fault(vma, addr & PAGE_MASK, mm_flags); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Mark Rutland | 541ec87 | 2016-05-31 12:33:03 +0100 | [diff] [blame] | 392 | static bool is_el0_instruction_abort(unsigned int esr) |
| 393 | { |
| 394 | return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW; |
| 395 | } |
| 396 | |
Anshuman Khandual | c49bd02 | 2019-06-07 14:43:05 +0530 | [diff] [blame] | 397 | /* |
| 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 | */ |
| 401 | static bool is_write_abort(unsigned int esr) |
| 402 | { |
| 403 | return (esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM); |
| 404 | } |
| 405 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 406 | static int __kprobes do_page_fault(unsigned long addr, unsigned int esr, |
| 407 | struct pt_regs *regs) |
| 408 | { |
Eric W. Biederman | 2d2837f | 2018-09-22 10:16:42 +0200 | [diff] [blame] | 409 | const struct fault_info *inf; |
Anshuman Khandual | 6168103 | 2019-06-03 12:11:23 +0530 | [diff] [blame] | 410 | struct mm_struct *mm = current->mm; |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 411 | vm_fault_t fault, major = 0; |
Catalin Marinas | cab15ce | 2016-08-11 18:44:50 +0100 | [diff] [blame] | 412 | unsigned long vm_flags = VM_READ | VM_WRITE; |
Will Deacon | db6f410 | 2013-07-19 15:37:12 +0100 | [diff] [blame] | 413 | unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; |
| 414 | |
Anshuman Khandual | b98cca4 | 2019-07-16 16:28:00 -0700 | [diff] [blame^] | 415 | if (kprobe_page_fault(regs, esr)) |
Sandeepa Prabhu | 2dd0e8d | 2016-07-08 12:35:48 -0400 | [diff] [blame] | 416 | return 0; |
| 417 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 418 | /* |
| 419 | * If we're in an interrupt or have no user context, we must not take |
| 420 | * the fault. |
| 421 | */ |
David Hildenbrand | 70ffdb9 | 2015-05-11 17:52:11 +0200 | [diff] [blame] | 422 | if (faulthandler_disabled() || !mm) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 423 | goto no_context; |
| 424 | |
Johannes Weiner | 759496b | 2013-09-12 15:13:39 -0700 | [diff] [blame] | 425 | if (user_mode(regs)) |
| 426 | mm_flags |= FAULT_FLAG_USER; |
| 427 | |
Mark Rutland | 541ec87 | 2016-05-31 12:33:03 +0100 | [diff] [blame] | 428 | if (is_el0_instruction_abort(esr)) { |
Johannes Weiner | 759496b | 2013-09-12 15:13:39 -0700 | [diff] [blame] | 429 | vm_flags = VM_EXEC; |
Anshuman Khandual | 01de177 | 2019-05-05 09:45:12 +0530 | [diff] [blame] | 430 | mm_flags |= FAULT_FLAG_INSTRUCTION; |
Anshuman Khandual | c49bd02 | 2019-06-07 14:43:05 +0530 | [diff] [blame] | 431 | } else if (is_write_abort(esr)) { |
Johannes Weiner | 759496b | 2013-09-12 15:13:39 -0700 | [diff] [blame] | 432 | vm_flags = VM_WRITE; |
| 433 | mm_flags |= FAULT_FLAG_WRITE; |
| 434 | } |
| 435 | |
Andrey Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 436 | if (is_ttbr0_addr(addr) && is_el1_permission_fault(addr, esr, regs)) { |
James Morse | e19a6ee | 2016-06-20 18:28:01 +0100 | [diff] [blame] | 437 | /* regs->orig_addr_limit may be 0 if we entered from EL0 */ |
| 438 | if (regs->orig_addr_limit == KERNEL_DS) |
Mark Rutland | c870f14 | 2018-05-21 14:14:51 +0100 | [diff] [blame] | 439 | die_kernel_fault("access to user memory with fs=KERNEL_DS", |
| 440 | addr, esr, regs); |
James Morse | 7054419 | 2016-02-05 14:58:50 +0000 | [diff] [blame] | 441 | |
Laura Abbott | 9adeb8e | 2016-08-09 18:25:26 -0700 | [diff] [blame] | 442 | if (is_el1_instruction_abort(esr)) |
Mark Rutland | c870f14 | 2018-05-21 14:14:51 +0100 | [diff] [blame] | 443 | die_kernel_fault("execution of user memory", |
| 444 | addr, esr, regs); |
Laura Abbott | 9adeb8e | 2016-08-09 18:25:26 -0700 | [diff] [blame] | 445 | |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame] | 446 | if (!search_exception_tables(regs->pc)) |
Mark Rutland | c870f14 | 2018-05-21 14:14:51 +0100 | [diff] [blame] | 447 | die_kernel_fault("access to user memory outside uaccess routines", |
| 448 | addr, esr, regs); |
James Morse | 57f4959 | 2016-02-05 14:58:48 +0000 | [diff] [blame] | 449 | } |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 450 | |
Punit Agrawal | 0e3a902 | 2017-06-08 18:25:28 +0100 | [diff] [blame] | 451 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr); |
| 452 | |
James Morse | 338d4f4 | 2015-07-22 19:05:54 +0100 | [diff] [blame] | 453 | /* |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 454 | * 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; |
| 461 | retry: |
| 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 Khandual | a050931 | 2019-06-03 12:11:22 +0530 | [diff] [blame] | 470 | if (!user_mode(regs) && !search_exception_tables(regs->pc)) { |
| 471 | up_read(&mm->mmap_sem); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 472 | goto no_context; |
Anshuman Khandual | a050931 | 2019-06-03 12:11:22 +0530 | [diff] [blame] | 473 | } |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 474 | #endif |
| 475 | } |
| 476 | |
Anshuman Khandual | 6168103 | 2019-06-03 12:11:23 +0530 | [diff] [blame] | 477 | fault = __do_page_fault(mm, addr, mm_flags, vm_flags); |
Punit Agrawal | 0e3a902 | 2017-06-08 18:25:28 +0100 | [diff] [blame] | 478 | 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 Rutland | 289d07a | 2017-07-11 15:19:22 +0100 | [diff] [blame] | 487 | if (fatal_signal_pending(current)) { |
| 488 | if (!user_mode(regs)) |
| 489 | goto no_context; |
Punit Agrawal | 0e3a902 | 2017-06-08 18:25:28 +0100 | [diff] [blame] | 490 | return 0; |
Mark Rutland | 289d07a | 2017-07-11 15:19:22 +0100 | [diff] [blame] | 491 | } |
Punit Agrawal | 0e3a902 | 2017-06-08 18:25:28 +0100 | [diff] [blame] | 492 | |
| 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 Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 504 | |
| 505 | /* |
Punit Agrawal | 0e3a902 | 2017-06-08 18:25:28 +0100 | [diff] [blame] | 506 | * Handle the "normal" (no error) case first. |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 507 | */ |
Punit Agrawal | 0e3a902 | 2017-06-08 18:25:28 +0100 | [diff] [blame] | 508 | 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 Khandual | 6168103 | 2019-06-03 12:11:23 +0530 | [diff] [blame] | 517 | current->maj_flt++; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 518 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, |
| 519 | addr); |
| 520 | } else { |
Anshuman Khandual | 6168103 | 2019-06-03 12:11:23 +0530 | [diff] [blame] | 521 | current->min_flt++; |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 522 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, |
| 523 | addr); |
| 524 | } |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 525 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 526 | return 0; |
Punit Agrawal | 0e3a902 | 2017-06-08 18:25:28 +0100 | [diff] [blame] | 527 | } |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 528 | |
Johannes Weiner | 8713410 | 2013-09-12 15:13:38 -0700 | [diff] [blame] | 529 | /* |
| 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 Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 536 | 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. Biederman | 2d2837f | 2018-09-22 10:16:42 +0200 | [diff] [blame] | 546 | inf = esr_to_fault_info(esr); |
Eric W. Biederman | 559d8d9 | 2018-09-22 10:18:42 +0200 | [diff] [blame] | 547 | set_thread_esr(addr, esr); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 548 | 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. Biederman | feca355 | 2018-09-22 10:26:57 +0200 | [diff] [blame] | 553 | arm64_force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)addr, |
| 554 | inf->name); |
Eric W. Biederman | 9ea3a97 | 2018-09-22 09:46:39 +0200 | [diff] [blame] | 555 | } else if (fault & (VM_FAULT_HWPOISON_LARGE | VM_FAULT_HWPOISON)) { |
| 556 | unsigned int lsb; |
Will Deacon | 92ff067 | 2018-02-20 14:53:22 +0000 | [diff] [blame] | 557 | |
Eric W. Biederman | 9ea3a97 | 2018-09-22 09:46:39 +0200 | [diff] [blame] | 558 | lsb = PAGE_SHIFT; |
| 559 | if (fault & VM_FAULT_HWPOISON_LARGE) |
| 560 | lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); |
Will Deacon | 92ff067 | 2018-02-20 14:53:22 +0000 | [diff] [blame] | 561 | |
Eric W. Biederman | b4d5557 | 2018-09-22 10:37:15 +0200 | [diff] [blame] | 562 | arm64_force_sig_mceerr(BUS_MCEERR_AR, (void __user *)addr, lsb, |
| 563 | inf->name); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 564 | } else { |
| 565 | /* |
| 566 | * Something tried to access memory that isn't in our memory |
| 567 | * map. |
| 568 | */ |
Eric W. Biederman | feca355 | 2018-09-22 10:26:57 +0200 | [diff] [blame] | 569 | arm64_force_sig_fault(SIGSEGV, |
| 570 | fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR, |
| 571 | (void __user *)addr, |
| 572 | inf->name); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 575 | return 0; |
| 576 | |
| 577 | no_context: |
Kristina Martsenko | 67ce16e | 2017-06-09 16:35:52 +0100 | [diff] [blame] | 578 | __do_kernel_fault(addr, esr, regs); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 582 | static int __kprobes do_translation_fault(unsigned long addr, |
| 583 | unsigned int esr, |
| 584 | struct pt_regs *regs) |
| 585 | { |
Andrey Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 586 | if (is_ttbr0_addr(addr)) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 587 | return do_page_fault(addr, esr, regs); |
| 588 | |
| 589 | do_bad_area(addr, esr, regs); |
| 590 | return 0; |
| 591 | } |
| 592 | |
EunTaik Lee | 52d7523 | 2016-02-16 04:44:35 +0000 | [diff] [blame] | 593 | static 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 Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 600 | static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs) |
| 601 | { |
Will Deacon | f67d5c4 | 2017-09-22 11:01:26 +0100 | [diff] [blame] | 602 | return 1; /* "fault" */ |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Tyler Baicar | 32015c2 | 2017-06-21 12:17:08 -0600 | [diff] [blame] | 605 | static int do_sea(unsigned long addr, unsigned int esr, struct pt_regs *regs) |
| 606 | { |
Tyler Baicar | 32015c2 | 2017-06-21 12:17:08 -0600 | [diff] [blame] | 607 | const struct fault_info *inf; |
Eric W. Biederman | 6fa998e | 2018-09-21 17:24:40 +0200 | [diff] [blame] | 608 | void __user *siaddr; |
Tyler Baicar | 32015c2 | 2017-06-21 12:17:08 -0600 | [diff] [blame] | 609 | |
| 610 | inf = esr_to_fault_info(esr); |
Tyler Baicar | 32015c2 | 2017-06-21 12:17:08 -0600 | [diff] [blame] | 611 | |
Tyler Baicar | 7edda08 | 2017-06-21 12:17:09 -0600 | [diff] [blame] | 612 | /* |
James Morse | d44f1b8 | 2019-01-29 18:48:50 +0000 | [diff] [blame] | 613 | * Return value ignored as we rely on signal merging. |
| 614 | * Future patches will make this more robust. |
Tyler Baicar | 7edda08 | 2017-06-21 12:17:09 -0600 | [diff] [blame] | 615 | */ |
James Morse | d44f1b8 | 2019-01-29 18:48:50 +0000 | [diff] [blame] | 616 | apei_claim_sea(regs); |
Tyler Baicar | 7edda08 | 2017-06-21 12:17:09 -0600 | [diff] [blame] | 617 | |
Tyler Baicar | 32015c2 | 2017-06-21 12:17:08 -0600 | [diff] [blame] | 618 | if (esr & ESR_ELx_FnV) |
Eric W. Biederman | 6fa998e | 2018-09-21 17:24:40 +0200 | [diff] [blame] | 619 | siaddr = NULL; |
Tyler Baicar | 32015c2 | 2017-06-21 12:17:08 -0600 | [diff] [blame] | 620 | else |
Eric W. Biederman | 6fa998e | 2018-09-21 17:24:40 +0200 | [diff] [blame] | 621 | siaddr = (void __user *)addr; |
| 622 | arm64_notify_die(inf->name, regs, inf->sig, inf->code, siaddr, esr); |
Tyler Baicar | 32015c2 | 2017-06-21 12:17:08 -0600 | [diff] [blame] | 623 | |
Dongjiu Geng | faa75e1 | 2017-12-13 18:36:47 +0800 | [diff] [blame] | 624 | return 0; |
Tyler Baicar | 32015c2 | 2017-06-21 12:17:08 -0600 | [diff] [blame] | 625 | } |
| 626 | |
Victor Kamensky | 09a6adf | 2017-04-03 22:51:01 -0700 | [diff] [blame] | 627 | static const struct fault_info fault_info[] = { |
Dave Martin | af40ff6 | 2018-03-08 17:41:05 +0000 | [diff] [blame] | 628 | { 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 Deacon | 7f73f7a | 2014-11-21 14:22:22 +0000 | [diff] [blame] | 632 | { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" }, |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 633 | { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" }, |
| 634 | { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" }, |
Will Deacon | 760bfb4 | 2017-09-29 12:27:41 +0100 | [diff] [blame] | 635 | { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" }, |
Dave Martin | af40ff6 | 2018-03-08 17:41:05 +0000 | [diff] [blame] | 636 | { do_bad, SIGKILL, SI_KERNEL, "unknown 8" }, |
Steve Capper | 084bd29 | 2013-04-10 13:48:00 +0100 | [diff] [blame] | 637 | { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" }, |
| 638 | { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" }, |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 639 | { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" }, |
Dave Martin | af40ff6 | 2018-03-08 17:41:05 +0000 | [diff] [blame] | 640 | { do_bad, SIGKILL, SI_KERNEL, "unknown 12" }, |
Steve Capper | 084bd29 | 2013-04-10 13:48:00 +0100 | [diff] [blame] | 641 | { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" }, |
| 642 | { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" }, |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 643 | { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" }, |
Dave Martin | af40ff6 | 2018-03-08 17:41:05 +0000 | [diff] [blame] | 644 | { 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 Lee | 52d7523 | 2016-02-16 04:44:35 +0000 | [diff] [blame] | 661 | { do_alignment_fault, SIGBUS, BUS_ADRALN, "alignment fault" }, |
Dave Martin | af40ff6 | 2018-03-08 17:41:05 +0000 | [diff] [blame] | 662 | { 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 Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 692 | }; |
| 693 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 694 | asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr, |
| 695 | struct pt_regs *regs) |
| 696 | { |
Victor Kamensky | 09a6adf | 2017-04-03 22:51:01 -0700 | [diff] [blame] | 697 | const struct fault_info *inf = esr_to_fault_info(esr); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 698 | |
| 699 | if (!inf->fn(addr, esr, regs)) |
| 700 | return; |
| 701 | |
Will Deacon | 1049c30 | 2018-02-20 14:41:02 +0000 | [diff] [blame] | 702 | if (!user_mode(regs)) { |
| 703 | pr_alert("Unhandled fault at 0x%016lx\n", addr); |
| 704 | mem_abort_decode(esr); |
Will Deacon | 80b6eb0 | 2017-10-31 15:56:11 +0000 | [diff] [blame] | 705 | show_pte(addr); |
Will Deacon | 1049c30 | 2018-02-20 14:41:02 +0000 | [diff] [blame] | 706 | } |
Mark Rutland | 42dbf54 | 2017-10-19 11:19:55 +0100 | [diff] [blame] | 707 | |
Eric W. Biederman | 6fa998e | 2018-09-21 17:24:40 +0200 | [diff] [blame] | 708 | arm64_notify_die(inf->name, regs, |
| 709 | inf->sig, inf->code, (void __user *)addr, esr); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Will Deacon | 30d88c0 | 2018-02-02 17:31:40 +0000 | [diff] [blame] | 712 | asmlinkage 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 Deacon | 0f15adb | 2018-01-03 11:17:58 +0000 | [diff] [blame] | 718 | asmlinkage 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 Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 727 | if (!is_ttbr0_addr(addr)) |
Will Deacon | 0f15adb | 2018-01-03 11:17:58 +0000 | [diff] [blame] | 728 | arm64_apply_bp_hardening(); |
| 729 | |
Julien Thierry | 9a0c032 | 2018-08-28 16:51:15 +0100 | [diff] [blame] | 730 | local_daif_restore(DAIF_PROCCTX); |
Will Deacon | 0f15adb | 2018-01-03 11:17:58 +0000 | [diff] [blame] | 731 | do_mem_abort(addr, esr, regs); |
| 732 | } |
| 733 | |
| 734 | |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 735 | asmlinkage void __exception do_sp_pc_abort(unsigned long addr, |
| 736 | unsigned int esr, |
| 737 | struct pt_regs *regs) |
| 738 | { |
Will Deacon | 5dfc6ed | 2018-02-02 17:31:39 +0000 | [diff] [blame] | 739 | if (user_mode(regs)) { |
Andrey Konovalov | 356607f | 2018-12-28 00:30:27 -0800 | [diff] [blame] | 740 | if (!is_ttbr0_addr(instruction_pointer(regs))) |
Will Deacon | 5dfc6ed | 2018-02-02 17:31:39 +0000 | [diff] [blame] | 741 | arm64_apply_bp_hardening(); |
Julien Thierry | 9a0c032 | 2018-08-28 16:51:15 +0100 | [diff] [blame] | 742 | local_daif_restore(DAIF_PROCCTX); |
Will Deacon | 5dfc6ed | 2018-02-02 17:31:39 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Eric W. Biederman | 6fa998e | 2018-09-21 17:24:40 +0200 | [diff] [blame] | 745 | arm64_notify_die("SP/PC alignment exception", regs, |
| 746 | SIGBUS, BUS_ADRALN, (void __user *)addr, esr); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Dave P Martin | 9fb7410 | 2015-07-24 16:37:48 +0100 | [diff] [blame] | 749 | int __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 | */ |
| 757 | static struct fault_info __refdata debug_fault_info[] = { |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 758 | { 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 Martin | af40ff6 | 2018-03-08 17:41:05 +0000 | [diff] [blame] | 761 | { do_bad, SIGKILL, SI_KERNEL, "unknown 3" }, |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 762 | { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" }, |
Dave Martin | af40ff6 | 2018-03-08 17:41:05 +0000 | [diff] [blame] | 763 | { do_bad, SIGKILL, SI_KERNEL, "aarch32 vector catch" }, |
Dave P Martin | 9fb7410 | 2015-07-24 16:37:48 +0100 | [diff] [blame] | 764 | { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" }, |
Dave Martin | af40ff6 | 2018-03-08 17:41:05 +0000 | [diff] [blame] | 765 | { do_bad, SIGKILL, SI_KERNEL, "unknown 7" }, |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 766 | }; |
| 767 | |
| 768 | void __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 Deacon | 969f5ea | 2019-04-29 13:03:57 +0100 | [diff] [blame] | 780 | #ifdef CONFIG_ARM64_ERRATUM_1463225 |
| 781 | DECLARE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa); |
| 782 | |
| 783 | static int __exception |
| 784 | cortex_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 |
| 803 | static int __exception |
| 804 | cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs) |
| 805 | { |
| 806 | return 0; |
| 807 | } |
| 808 | #endif /* CONFIG_ARM64_ERRATUM_1463225 */ |
| 809 | |
Will Deacon | 52c6d14 | 2019-02-25 12:06:43 +0000 | [diff] [blame] | 810 | asmlinkage void __exception do_debug_exception(unsigned long addr_if_watchpoint, |
| 811 | unsigned int esr, |
| 812 | struct pt_regs *regs) |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 813 | { |
Anshuman Khandual | 359048f | 2018-09-22 21:09:54 +0530 | [diff] [blame] | 814 | const struct fault_info *inf = esr_to_debug_fault_info(esr); |
Will Deacon | b9a4b9d | 2019-03-01 13:28:00 +0000 | [diff] [blame] | 815 | unsigned long pc = instruction_pointer(regs); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 816 | |
Will Deacon | 969f5ea | 2019-04-29 13:03:57 +0100 | [diff] [blame] | 817 | if (cortex_a76_erratum_1463225_debug_handler(regs)) |
| 818 | return; |
| 819 | |
James Morse | 6afedcd | 2016-04-13 13:40:00 +0100 | [diff] [blame] | 820 | /* |
| 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 Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 826 | |
Will Deacon | b9a4b9d | 2019-03-01 13:28:00 +0000 | [diff] [blame] | 827 | if (user_mode(regs) && !is_ttbr0_addr(pc)) |
Will Deacon | 5dfc6ed | 2018-02-02 17:31:39 +0000 | [diff] [blame] | 828 | arm64_apply_bp_hardening(); |
| 829 | |
Will Deacon | 52c6d14 | 2019-02-25 12:06:43 +0000 | [diff] [blame] | 830 | if (inf->fn(addr_if_watchpoint, esr, regs)) { |
Eric W. Biederman | 6fa998e | 2018-09-21 17:24:40 +0200 | [diff] [blame] | 831 | arm64_notify_die(inf->name, regs, |
Will Deacon | b9a4b9d | 2019-03-01 13:28:00 +0000 | [diff] [blame] | 832 | inf->sig, inf->code, (void __user *)pc, esr); |
James Morse | 6afedcd | 2016-04-13 13:40:00 +0100 | [diff] [blame] | 833 | } |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 834 | |
James Morse | 6afedcd | 2016-04-13 13:40:00 +0100 | [diff] [blame] | 835 | if (interrupts_enabled(regs)) |
| 836 | trace_hardirqs_on(); |
Catalin Marinas | 1d18c47 | 2012-03-05 11:49:27 +0000 | [diff] [blame] | 837 | } |
Sandeepa Prabhu | 2dd0e8d | 2016-07-08 12:35:48 -0400 | [diff] [blame] | 838 | NOKPROBE_SYMBOL(do_debug_exception); |