Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Wind River Systems Inc |
| 3 | * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com |
| 4 | * |
| 5 | * based on arch/mips/mm/fault.c which is: |
| 6 | * |
| 7 | * Copyright (C) 1995-2000 Ralf Baechle |
| 8 | * |
| 9 | * This file is subject to the terms and conditions of the GNU General Public |
| 10 | * License. See the file "COPYING" in the main directory of this archive |
| 11 | * for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/signal.h> |
| 15 | #include <linux/sched.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 16 | #include <linux/sched/debug.h> |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/ptrace.h> |
| 23 | #include <linux/mman.h> |
| 24 | #include <linux/mm.h> |
Paul Gortmaker | 7e227e8 | 2017-01-10 09:51:08 -0500 | [diff] [blame] | 25 | #include <linux/extable.h> |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Peter Xu | 4487dcf | 2020-08-11 18:38:22 -0700 | [diff] [blame] | 27 | #include <linux/perf_event.h> |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 28 | |
| 29 | #include <asm/mmu_context.h> |
| 30 | #include <asm/traps.h> |
| 31 | |
| 32 | #define EXC_SUPERV_INSN_ACCESS 9 /* Supervisor only instruction address */ |
| 33 | #define EXC_SUPERV_DATA_ACCESS 11 /* Supervisor only data address */ |
| 34 | #define EXC_X_PROTECTION_FAULT 13 /* TLB permission violation (x) */ |
| 35 | #define EXC_R_PROTECTION_FAULT 14 /* TLB permission violation (r) */ |
| 36 | #define EXC_W_PROTECTION_FAULT 15 /* TLB permission violation (w) */ |
| 37 | |
| 38 | /* |
| 39 | * This routine handles page faults. It determines the address, |
| 40 | * and the problem, and then passes it off to one of the appropriate |
| 41 | * routines. |
| 42 | */ |
| 43 | asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause, |
| 44 | unsigned long address) |
| 45 | { |
| 46 | struct vm_area_struct *vma = NULL; |
| 47 | struct task_struct *tsk = current; |
| 48 | struct mm_struct *mm = tsk->mm; |
| 49 | int code = SEGV_MAPERR; |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 50 | vm_fault_t fault; |
Peter Xu | dde1607 | 2020-04-01 21:08:37 -0700 | [diff] [blame] | 51 | unsigned int flags = FAULT_FLAG_DEFAULT; |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 52 | |
| 53 | cause >>= 2; |
| 54 | |
| 55 | /* Restart the instruction */ |
| 56 | regs->ea -= 4; |
| 57 | |
| 58 | /* |
| 59 | * We fault-in kernel-space virtual memory on-demand. The |
| 60 | * 'reference' page table is init_mm.pgd. |
| 61 | * |
| 62 | * NOTE! We MUST NOT take any locks for this case. We may |
| 63 | * be in an interrupt or a critical region, and should |
| 64 | * only copy the information from the master page table, |
| 65 | * nothing more. |
| 66 | */ |
| 67 | if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END)) { |
| 68 | if (user_mode(regs)) |
| 69 | goto bad_area_nosemaphore; |
| 70 | else |
| 71 | goto vmalloc_fault; |
| 72 | } |
| 73 | |
| 74 | if (unlikely(address >= TASK_SIZE)) |
| 75 | goto bad_area_nosemaphore; |
| 76 | |
| 77 | /* |
| 78 | * If we're in an interrupt or have no user |
| 79 | * context, we must not take the fault.. |
| 80 | */ |
David Hildenbrand | 70ffdb9 | 2015-05-11 17:52:11 +0200 | [diff] [blame] | 81 | if (faulthandler_disabled() || !mm) |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 82 | goto bad_area_nosemaphore; |
| 83 | |
| 84 | if (user_mode(regs)) |
| 85 | flags |= FAULT_FLAG_USER; |
| 86 | |
Peter Xu | 4487dcf | 2020-08-11 18:38:22 -0700 | [diff] [blame] | 87 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); |
| 88 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 89 | if (!mmap_read_trylock(mm)) { |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 90 | if (!user_mode(regs) && !search_exception_tables(regs->ea)) |
| 91 | goto bad_area_nosemaphore; |
Ley Foon Tan | 96f3a5c | 2015-02-09 18:11:29 +0800 | [diff] [blame] | 92 | retry: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 93 | mmap_read_lock(mm); |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | vma = find_vma(mm, address); |
| 97 | if (!vma) |
| 98 | goto bad_area; |
| 99 | if (vma->vm_start <= address) |
| 100 | goto good_area; |
| 101 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 102 | goto bad_area; |
| 103 | if (expand_stack(vma, address)) |
| 104 | goto bad_area; |
| 105 | /* |
| 106 | * Ok, we have a good vm_area for this memory access, so |
| 107 | * we can handle it.. |
| 108 | */ |
| 109 | good_area: |
| 110 | code = SEGV_ACCERR; |
| 111 | |
| 112 | switch (cause) { |
| 113 | case EXC_SUPERV_INSN_ACCESS: |
| 114 | goto bad_area; |
| 115 | case EXC_SUPERV_DATA_ACCESS: |
| 116 | goto bad_area; |
| 117 | case EXC_X_PROTECTION_FAULT: |
| 118 | if (!(vma->vm_flags & VM_EXEC)) |
| 119 | goto bad_area; |
| 120 | break; |
| 121 | case EXC_R_PROTECTION_FAULT: |
| 122 | if (!(vma->vm_flags & VM_READ)) |
| 123 | goto bad_area; |
| 124 | break; |
| 125 | case EXC_W_PROTECTION_FAULT: |
| 126 | if (!(vma->vm_flags & VM_WRITE)) |
| 127 | goto bad_area; |
| 128 | flags = FAULT_FLAG_WRITE; |
| 129 | break; |
| 130 | } |
| 131 | |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 132 | /* |
| 133 | * If for any reason at all we couldn't handle the fault, |
| 134 | * make sure we exit gracefully rather than endlessly redo |
| 135 | * the fault. |
| 136 | */ |
Peter Xu | 4487dcf | 2020-08-11 18:38:22 -0700 | [diff] [blame] | 137 | fault = handle_mm_fault(vma, address, flags, regs); |
Ley Foon Tan | 96f3a5c | 2015-02-09 18:11:29 +0800 | [diff] [blame] | 138 | |
Peter Xu | 4ef8732 | 2020-04-01 21:08:06 -0700 | [diff] [blame] | 139 | if (fault_signal_pending(fault, regs)) |
Ley Foon Tan | 96f3a5c | 2015-02-09 18:11:29 +0800 | [diff] [blame] | 140 | return; |
| 141 | |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 142 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 143 | if (fault & VM_FAULT_OOM) |
| 144 | goto out_of_memory; |
Linus Torvalds | 33692f2 | 2015-01-29 10:51:32 -0800 | [diff] [blame] | 145 | else if (fault & VM_FAULT_SIGSEGV) |
| 146 | goto bad_area; |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 147 | else if (fault & VM_FAULT_SIGBUS) |
| 148 | goto do_sigbus; |
| 149 | BUG(); |
| 150 | } |
Ley Foon Tan | 96f3a5c | 2015-02-09 18:11:29 +0800 | [diff] [blame] | 151 | |
Ley Foon Tan | 96f3a5c | 2015-02-09 18:11:29 +0800 | [diff] [blame] | 152 | if (flags & FAULT_FLAG_ALLOW_RETRY) { |
Ley Foon Tan | 96f3a5c | 2015-02-09 18:11:29 +0800 | [diff] [blame] | 153 | if (fault & VM_FAULT_RETRY) { |
Ley Foon Tan | 96f3a5c | 2015-02-09 18:11:29 +0800 | [diff] [blame] | 154 | flags |= FAULT_FLAG_TRIED; |
| 155 | |
| 156 | /* |
Michel Lespinasse | 3e4e28c | 2020-06-08 21:33:51 -0700 | [diff] [blame] | 157 | * No need to mmap_read_unlock(mm) as we would |
Ley Foon Tan | 96f3a5c | 2015-02-09 18:11:29 +0800 | [diff] [blame] | 158 | * have already released it in __lock_page_or_retry |
| 159 | * in mm/filemap.c. |
| 160 | */ |
| 161 | |
| 162 | goto retry; |
| 163 | } |
| 164 | } |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 165 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 166 | mmap_read_unlock(mm); |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 167 | return; |
| 168 | |
| 169 | /* |
| 170 | * Something tried to access memory that isn't in our memory map.. |
| 171 | * Fix it, but check if it's kernel or user first.. |
| 172 | */ |
| 173 | bad_area: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 174 | mmap_read_unlock(mm); |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 175 | |
| 176 | bad_area_nosemaphore: |
| 177 | /* User mode accesses just cause a SIGSEGV */ |
| 178 | if (user_mode(regs)) { |
Chung-Ling Tang | a3248d6 | 2015-02-09 09:40:50 +0800 | [diff] [blame] | 179 | if (unhandled_signal(current, SIGSEGV) && printk_ratelimit()) { |
| 180 | pr_info("%s: unhandled page fault (%d) at 0x%08lx, " |
| 181 | "cause %ld\n", current->comm, SIGSEGV, address, cause); |
| 182 | show_regs(regs); |
| 183 | } |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 184 | _exception(SIGSEGV, regs, code, address); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | no_context: |
| 189 | /* Are we prepared to handle this kernel fault? */ |
| 190 | if (fixup_exception(regs)) |
| 191 | return; |
| 192 | |
| 193 | /* |
| 194 | * Oops. The kernel tried to access some bad page. We'll have to |
| 195 | * terminate things with extreme prejudice. |
| 196 | */ |
| 197 | bust_spinlocks(1); |
| 198 | |
| 199 | pr_alert("Unable to handle kernel %s at virtual address %08lx", |
| 200 | address < PAGE_SIZE ? "NULL pointer dereference" : |
| 201 | "paging request", address); |
| 202 | pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra, |
| 203 | cause); |
| 204 | panic("Oops"); |
| 205 | return; |
| 206 | |
| 207 | /* |
| 208 | * We ran out of memory, or some other thing happened to us that made |
| 209 | * us unable to handle the page fault gracefully. |
| 210 | */ |
| 211 | out_of_memory: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 212 | mmap_read_unlock(mm); |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 213 | if (!user_mode(regs)) |
| 214 | goto no_context; |
| 215 | pagefault_out_of_memory(); |
| 216 | return; |
| 217 | |
| 218 | do_sigbus: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 219 | mmap_read_unlock(mm); |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 220 | |
| 221 | /* Kernel mode? Handle exceptions or die */ |
| 222 | if (!user_mode(regs)) |
| 223 | goto no_context; |
| 224 | |
| 225 | _exception(SIGBUS, regs, BUS_ADRERR, address); |
| 226 | return; |
| 227 | |
| 228 | vmalloc_fault: |
| 229 | { |
| 230 | /* |
| 231 | * Synchronize this task's top level page-table |
| 232 | * with the 'reference' page table. |
| 233 | * |
| 234 | * Do _not_ use "tsk" here. We might be inside |
| 235 | * an interrupt in the middle of a task switch.. |
| 236 | */ |
| 237 | int offset = pgd_index(address); |
| 238 | pgd_t *pgd, *pgd_k; |
Mike Rapoport | 9f4e703 | 2020-06-04 16:46:35 -0700 | [diff] [blame] | 239 | p4d_t *p4d, *p4d_k; |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 240 | pud_t *pud, *pud_k; |
| 241 | pmd_t *pmd, *pmd_k; |
| 242 | pte_t *pte_k; |
| 243 | |
| 244 | pgd = pgd_current + offset; |
| 245 | pgd_k = init_mm.pgd + offset; |
| 246 | |
| 247 | if (!pgd_present(*pgd_k)) |
| 248 | goto no_context; |
| 249 | set_pgd(pgd, *pgd_k); |
| 250 | |
Mike Rapoport | 9f4e703 | 2020-06-04 16:46:35 -0700 | [diff] [blame] | 251 | p4d = p4d_offset(pgd, address); |
| 252 | p4d_k = p4d_offset(pgd_k, address); |
| 253 | if (!p4d_present(*p4d_k)) |
| 254 | goto no_context; |
| 255 | pud = pud_offset(p4d, address); |
| 256 | pud_k = pud_offset(p4d_k, address); |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 257 | if (!pud_present(*pud_k)) |
| 258 | goto no_context; |
| 259 | pmd = pmd_offset(pud, address); |
| 260 | pmd_k = pmd_offset(pud_k, address); |
| 261 | if (!pmd_present(*pmd_k)) |
| 262 | goto no_context; |
| 263 | set_pmd(pmd, *pmd_k); |
| 264 | |
| 265 | pte_k = pte_offset_kernel(pmd_k, address); |
| 266 | if (!pte_present(*pte_k)) |
| 267 | goto no_context; |
| 268 | |
Nicholas Piggin | 195568a | 2018-11-05 10:00:15 +0800 | [diff] [blame] | 269 | flush_tlb_kernel_page(address); |
Ley Foon Tan | 862674d | 2014-11-06 15:19:44 +0800 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | } |