1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * OpenRISC fault.c 4 * 5 * Linux architectural port borrowing liberally from similar works of 6 * others. All original copyrights apply as per the original source 7 * declaration. 8 * 9 * Modifications for the OpenRISC architecture: 10 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com> 11 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se> 12 */ 13 14 #include <linux/mm.h> 15 #include <linux/interrupt.h> 16 #include <linux/extable.h> 17 #include <linux/sched/signal.h> 18 #include <linux/perf_event.h> 19 20 #include <linux/uaccess.h> 21 #include <asm/mmu_context.h> 22 #include <asm/siginfo.h> 23 #include <asm/signal.h> 24 25 #define NUM_TLB_ENTRIES 64 26 #define TLB_OFFSET(add) (((add) >> PAGE_SHIFT) & (NUM_TLB_ENTRIES-1)) 27 28 /* __PHX__ :: - check the vmalloc_fault in do_page_fault() 29 * - also look into include/asm/mmu_context.h 30 */ 31 volatile pgd_t *current_pgd[NR_CPUS]; 32 33 extern void __noreturn die(char *, struct pt_regs *, long); 34 35 /* 36 * This routine handles page faults. It determines the address, 37 * and the problem, and then passes it off to one of the appropriate 38 * routines. 39 * 40 * If this routine detects a bad access, it returns 1, otherwise it 41 * returns 0. 42 */ 43 44 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address, 45 unsigned long vector, int write_acc) 46 { 47 struct task_struct *tsk; 48 struct mm_struct *mm; 49 struct vm_area_struct *vma; 50 int si_code; 51 vm_fault_t fault; 52 unsigned int flags = FAULT_FLAG_DEFAULT; 53 54 tsk = current; 55 56 /* 57 * We fault-in kernel-space virtual memory on-demand. The 58 * 'reference' page table is init_mm.pgd. 59 * 60 * NOTE! We MUST NOT take any locks for this case. We may 61 * be in an interrupt or a critical region, and should 62 * only copy the information from the master page table, 63 * nothing more. 64 * 65 * NOTE2: This is done so that, when updating the vmalloc 66 * mappings we don't have to walk all processes pgdirs and 67 * add the high mappings all at once. Instead we do it as they 68 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL 69 * bit set so sometimes the TLB can use a lingering entry. 70 * 71 * This verifies that the fault happens in kernel space 72 * and that the fault was not a protection error. 73 */ 74 75 if (address >= VMALLOC_START && 76 (vector != 0x300 && vector != 0x400) && 77 !user_mode(regs)) 78 goto vmalloc_fault; 79 80 /* If exceptions were enabled, we can reenable them here */ 81 if (user_mode(regs)) { 82 /* Exception was in userspace: reenable interrupts */ 83 local_irq_enable(); 84 flags |= FAULT_FLAG_USER; 85 } else { 86 /* If exception was in a syscall, then IRQ's may have 87 * been enabled or disabled. If they were enabled, 88 * reenable them. 89 */ 90 if (regs->sr && (SPR_SR_IEE | SPR_SR_TEE)) 91 local_irq_enable(); 92 } 93 94 mm = tsk->mm; 95 si_code = SEGV_MAPERR; 96 97 /* 98 * If we're in an interrupt or have no user 99 * context, we must not take the fault.. 100 */ 101 102 if (in_interrupt() || !mm) 103 goto no_context; 104 105 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 106 107 retry: 108 mmap_read_lock(mm); 109 vma = find_vma(mm, address); 110 111 if (!vma) 112 goto bad_area; 113 114 if (vma->vm_start <= address) 115 goto good_area; 116 117 if (!(vma->vm_flags & VM_GROWSDOWN)) 118 goto bad_area; 119 120 if (user_mode(regs)) { 121 /* 122 * accessing the stack below usp is always a bug. 123 * we get page-aligned addresses so we can only check 124 * if we're within a page from usp, but that might be 125 * enough to catch brutal errors at least. 126 */ 127 if (address + PAGE_SIZE < regs->sp) 128 goto bad_area; 129 } 130 vma = expand_stack(mm, address); 131 if (!vma) 132 goto bad_area_nosemaphore; 133 134 /* 135 * Ok, we have a good vm_area for this memory access, so 136 * we can handle it.. 137 */ 138 139 good_area: 140 si_code = SEGV_ACCERR; 141 142 /* first do some preliminary protection checks */ 143 144 if (write_acc) { 145 if (!(vma->vm_flags & VM_WRITE)) 146 goto bad_area; 147 flags |= FAULT_FLAG_WRITE; 148 } else { 149 /* not present */ 150 if (!(vma->vm_flags & (VM_READ | VM_EXEC))) 151 goto bad_area; 152 } 153 154 /* are we trying to execute nonexecutable area */ 155 if ((vector == 0x400) && !(vma->vm_page_prot.pgprot & _PAGE_EXEC)) 156 goto bad_area; 157 158 /* 159 * If for any reason at all we couldn't handle the fault, 160 * make sure we exit gracefully rather than endlessly redo 161 * the fault. 162 */ 163 164 fault = handle_mm_fault(vma, address, flags, regs); 165 166 if (fault_signal_pending(fault, regs)) { 167 if (!user_mode(regs)) 168 goto no_context; 169 return; 170 } 171 172 /* The fault is fully completed (including releasing mmap lock) */ 173 if (fault & VM_FAULT_COMPLETED) 174 return; 175 176 if (unlikely(fault & VM_FAULT_ERROR)) { 177 if (fault & VM_FAULT_OOM) 178 goto out_of_memory; 179 else if (fault & VM_FAULT_SIGSEGV) 180 goto bad_area; 181 else if (fault & VM_FAULT_SIGBUS) 182 goto do_sigbus; 183 BUG(); 184 } 185 186 /*RGD modeled on Cris */ 187 if (fault & VM_FAULT_RETRY) { 188 flags |= FAULT_FLAG_TRIED; 189 190 /* No need to mmap_read_unlock(mm) as we would 191 * have already released it in __lock_page_or_retry 192 * in mm/filemap.c. 193 */ 194 195 goto retry; 196 } 197 198 mmap_read_unlock(mm); 199 return; 200 201 /* 202 * Something tried to access memory that isn't in our memory map.. 203 * Fix it, but check if it's kernel or user first.. 204 */ 205 206 bad_area: 207 mmap_read_unlock(mm); 208 209 bad_area_nosemaphore: 210 211 /* User mode accesses just cause a SIGSEGV */ 212 213 if (user_mode(regs)) { 214 force_sig_fault(SIGSEGV, si_code, (void __user *)address); 215 return; 216 } 217 218 no_context: 219 220 /* Are we prepared to handle this kernel fault? 221 * 222 * (The kernel has valid exception-points in the source 223 * when it acesses user-memory. When it fails in one 224 * of those points, we find it in a table and do a jump 225 * to some fixup code that loads an appropriate error 226 * code) 227 */ 228 229 { 230 const struct exception_table_entry *entry; 231 232 if ((entry = search_exception_tables(regs->pc)) != NULL) { 233 /* Adjust the instruction pointer in the stackframe */ 234 regs->pc = entry->fixup; 235 return; 236 } 237 } 238 239 /* 240 * Oops. The kernel tried to access some bad page. We'll have to 241 * terminate things with extreme prejudice. 242 */ 243 244 if ((unsigned long)(address) < PAGE_SIZE) 245 printk(KERN_ALERT 246 "Unable to handle kernel NULL pointer dereference"); 247 else 248 printk(KERN_ALERT "Unable to handle kernel access"); 249 printk(" at virtual address 0x%08lx\n", address); 250 251 die("Oops", regs, write_acc); 252 253 /* 254 * We ran out of memory, or some other thing happened to us that made 255 * us unable to handle the page fault gracefully. 256 */ 257 258 out_of_memory: 259 mmap_read_unlock(mm); 260 if (!user_mode(regs)) 261 goto no_context; 262 pagefault_out_of_memory(); 263 return; 264 265 do_sigbus: 266 mmap_read_unlock(mm); 267 268 /* 269 * Send a sigbus, regardless of whether we were in kernel 270 * or user mode. 271 */ 272 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address); 273 274 /* Kernel mode? Handle exceptions or die */ 275 if (!user_mode(regs)) 276 goto no_context; 277 return; 278 279 vmalloc_fault: 280 { 281 /* 282 * Synchronize this task's top level page-table 283 * with the 'reference' page table. 284 * 285 * Use current_pgd instead of tsk->active_mm->pgd 286 * since the latter might be unavailable if this 287 * code is executed in a misfortunately run irq 288 * (like inside schedule() between switch_mm and 289 * switch_to...). 290 */ 291 292 int offset = pgd_index(address); 293 pgd_t *pgd, *pgd_k; 294 p4d_t *p4d, *p4d_k; 295 pud_t *pud, *pud_k; 296 pmd_t *pmd, *pmd_k; 297 pte_t *pte_k; 298 299 /* 300 phx_warn("do_page_fault(): vmalloc_fault will not work, " 301 "since current_pgd assign a proper value somewhere\n" 302 "anyhow we don't need this at the moment\n"); 303 304 phx_mmu("vmalloc_fault"); 305 */ 306 pgd = (pgd_t *)current_pgd[smp_processor_id()] + offset; 307 pgd_k = init_mm.pgd + offset; 308 309 /* Since we're two-level, we don't need to do both 310 * set_pgd and set_pmd (they do the same thing). If 311 * we go three-level at some point, do the right thing 312 * with pgd_present and set_pgd here. 313 * 314 * Also, since the vmalloc area is global, we don't 315 * need to copy individual PTE's, it is enough to 316 * copy the pgd pointer into the pte page of the 317 * root task. If that is there, we'll find our pte if 318 * it exists. 319 */ 320 321 p4d = p4d_offset(pgd, address); 322 p4d_k = p4d_offset(pgd_k, address); 323 if (!p4d_present(*p4d_k)) 324 goto no_context; 325 326 pud = pud_offset(p4d, address); 327 pud_k = pud_offset(p4d_k, address); 328 if (!pud_present(*pud_k)) 329 goto no_context; 330 331 pmd = pmd_offset(pud, address); 332 pmd_k = pmd_offset(pud_k, address); 333 334 if (!pmd_present(*pmd_k)) 335 goto bad_area_nosemaphore; 336 337 set_pmd(pmd, *pmd_k); 338 339 /* Make sure the actual PTE exists as well to 340 * catch kernel vmalloc-area accesses to non-mapped 341 * addresses. If we don't do this, this will just 342 * silently loop forever. 343 */ 344 345 pte_k = pte_offset_kernel(pmd_k, address); 346 if (!pte_present(*pte_k)) 347 goto no_context; 348 349 return; 350 } 351 } 352