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