Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jesper Nilsson | 028c1f6 | 2010-08-04 17:23:24 +0200 | [diff] [blame] | 2 | * arch/cris/mm/fault.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Jesper Nilsson | 028c1f6 | 2010-08-04 17:23:24 +0200 | [diff] [blame] | 4 | * Copyright (C) 2000-2010 Axis Communications AB |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/mm.h> |
| 8 | #include <linux/interrupt.h> |
| 9 | #include <linux/module.h> |
Jesper Nilsson | b4e8a18 | 2010-08-04 17:42:43 +0200 | [diff] [blame] | 10 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <asm/uaccess.h> |
David Howells | b1a154d | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 12 | #include <arch/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | extern int find_fixup_code(struct pt_regs *); |
| 15 | extern void die_if_kernel(const char *, struct pt_regs *, long); |
Jesper Nilsson | 2d495eb | 2010-08-04 17:48:40 +0200 | [diff] [blame] | 16 | extern void show_registers(struct pt_regs *regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | /* debug of low-level TLB reload */ |
| 19 | #undef DEBUG |
| 20 | |
| 21 | #ifdef DEBUG |
| 22 | #define D(x) x |
| 23 | #else |
| 24 | #define D(x) |
| 25 | #endif |
| 26 | |
| 27 | /* debug of higher-level faults */ |
| 28 | #define DPG(x) |
| 29 | |
| 30 | /* current active page directory */ |
| 31 | |
Jesper Nilsson | fe87f94 | 2009-06-24 15:13:41 +0900 | [diff] [blame] | 32 | DEFINE_PER_CPU(pgd_t *, current_pgd); |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 33 | unsigned long cris_signal_return_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * Notice that the address we're given is aligned to the page the fault |
| 41 | * occurred in, since we only get the PFN in R_MMU_CAUSE not the complete |
| 42 | * address. |
| 43 | * |
| 44 | * error_code: |
Jesper Nilsson | 3e1fdc4 | 2007-11-30 13:59:57 +0100 | [diff] [blame] | 45 | * bit 0 == 0 means no page found, 1 means protection fault |
| 46 | * bit 1 == 0 means read, 1 means write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * |
| 48 | * If this routine detects a bad access, it returns 1, otherwise it |
| 49 | * returns 0. |
| 50 | */ |
| 51 | |
| 52 | asmlinkage void |
| 53 | do_page_fault(unsigned long address, struct pt_regs *regs, |
| 54 | int protection, int writeaccess) |
| 55 | { |
| 56 | struct task_struct *tsk; |
| 57 | struct mm_struct *mm; |
| 58 | struct vm_area_struct * vma; |
| 59 | siginfo_t info; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 60 | int fault; |
Kautuk Consul | 4d5914d | 2012-03-20 14:24:05 +0100 | [diff] [blame^] | 61 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE | |
| 62 | ((writeaccess & 1) ? FAULT_FLAG_WRITE : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Jesper Nilsson | 3e1fdc4 | 2007-11-30 13:59:57 +0100 | [diff] [blame] | 64 | D(printk(KERN_DEBUG |
| 65 | "Page fault for %lX on %X at %lX, prot %d write %d\n", |
| 66 | address, smp_processor_id(), instruction_pointer(regs), |
| 67 | protection, writeaccess)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | tsk = current; |
| 70 | |
| 71 | /* |
| 72 | * We fault-in kernel-space virtual memory on-demand. The |
| 73 | * 'reference' page table is init_mm.pgd. |
| 74 | * |
| 75 | * NOTE! We MUST NOT take any locks for this case. We may |
| 76 | * be in an interrupt or a critical region, and should |
| 77 | * only copy the information from the master page table, |
| 78 | * nothing more. |
| 79 | * |
| 80 | * NOTE2: This is done so that, when updating the vmalloc |
| 81 | * mappings we don't have to walk all processes pgdirs and |
| 82 | * add the high mappings all at once. Instead we do it as they |
| 83 | * are used. However vmalloc'ed page entries have the PAGE_GLOBAL |
| 84 | * bit set so sometimes the TLB can use a lingering entry. |
| 85 | * |
| 86 | * This verifies that the fault happens in kernel space |
| 87 | * and that the fault was not a protection error (error_code & 1). |
| 88 | */ |
| 89 | |
| 90 | if (address >= VMALLOC_START && |
| 91 | !protection && |
| 92 | !user_mode(regs)) |
| 93 | goto vmalloc_fault; |
| 94 | |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 95 | /* When stack execution is not allowed we store the signal |
| 96 | * trampolines in the reserved cris_signal_return_page. |
| 97 | * Handle this in the exact same way as vmalloc (we know |
| 98 | * that the mapping is there and is valid so no need to |
| 99 | * call handle_mm_fault). |
| 100 | */ |
| 101 | if (cris_signal_return_page && |
| 102 | address == cris_signal_return_page && |
| 103 | !protection && user_mode(regs)) |
| 104 | goto vmalloc_fault; |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* we can and should enable interrupts at this point */ |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 107 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | mm = tsk->mm; |
| 110 | info.si_code = SEGV_MAPERR; |
| 111 | |
| 112 | /* |
Jesper Nilsson | 028c1f6 | 2010-08-04 17:23:24 +0200 | [diff] [blame] | 113 | * If we're in an interrupt or "atomic" operation or have no |
| 114 | * user context, we must not take the fault. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | */ |
| 116 | |
Jesper Nilsson | 028c1f6 | 2010-08-04 17:23:24 +0200 | [diff] [blame] | 117 | if (in_atomic() || !mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | goto no_context; |
| 119 | |
Kautuk Consul | 4d5914d | 2012-03-20 14:24:05 +0100 | [diff] [blame^] | 120 | retry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | down_read(&mm->mmap_sem); |
| 122 | vma = find_vma(mm, address); |
| 123 | if (!vma) |
| 124 | goto bad_area; |
| 125 | if (vma->vm_start <= address) |
| 126 | goto good_area; |
| 127 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 128 | goto bad_area; |
| 129 | if (user_mode(regs)) { |
| 130 | /* |
| 131 | * accessing the stack below usp is always a bug. |
| 132 | * we get page-aligned addresses so we can only check |
| 133 | * if we're within a page from usp, but that might be |
| 134 | * enough to catch brutal errors at least. |
| 135 | */ |
| 136 | if (address + PAGE_SIZE < rdusp()) |
| 137 | goto bad_area; |
| 138 | } |
| 139 | if (expand_stack(vma, address)) |
| 140 | goto bad_area; |
| 141 | |
| 142 | /* |
| 143 | * Ok, we have a good vm_area for this memory access, so |
| 144 | * we can handle it.. |
| 145 | */ |
| 146 | |
| 147 | good_area: |
| 148 | info.si_code = SEGV_ACCERR; |
| 149 | |
| 150 | /* first do some preliminary protection checks */ |
| 151 | |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 152 | if (writeaccess == 2){ |
| 153 | if (!(vma->vm_flags & VM_EXEC)) |
| 154 | goto bad_area; |
| 155 | } else if (writeaccess == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | if (!(vma->vm_flags & VM_WRITE)) |
| 157 | goto bad_area; |
| 158 | } else { |
| 159 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) |
| 160 | goto bad_area; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * If for any reason at all we couldn't handle the fault, |
| 165 | * make sure we exit gracefully rather than endlessly redo |
| 166 | * the fault. |
| 167 | */ |
| 168 | |
Kautuk Consul | 4d5914d | 2012-03-20 14:24:05 +0100 | [diff] [blame^] | 169 | fault = handle_mm_fault(mm, vma, address, flags); |
| 170 | |
| 171 | if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) |
| 172 | return; |
| 173 | |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 174 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 175 | if (fault & VM_FAULT_OOM) |
| 176 | goto out_of_memory; |
| 177 | else if (fault & VM_FAULT_SIGBUS) |
| 178 | goto do_sigbus; |
| 179 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
Kautuk Consul | 4d5914d | 2012-03-20 14:24:05 +0100 | [diff] [blame^] | 181 | |
| 182 | if (flags & FAULT_FLAG_ALLOW_RETRY) { |
| 183 | if (fault & VM_FAULT_MAJOR) |
| 184 | tsk->maj_flt++; |
| 185 | else |
| 186 | tsk->min_flt++; |
| 187 | if (fault & VM_FAULT_RETRY) { |
| 188 | flags &= ~FAULT_FLAG_ALLOW_RETRY; |
| 189 | |
| 190 | /* |
| 191 | * No need to up_read(&mm->mmap_sem) as we would |
| 192 | * have already released it in __lock_page_or_retry |
| 193 | * in mm/filemap.c. |
| 194 | */ |
| 195 | |
| 196 | goto retry; |
| 197 | } |
| 198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | up_read(&mm->mmap_sem); |
| 201 | return; |
| 202 | |
| 203 | /* |
| 204 | * Something tried to access memory that isn't in our memory map.. |
| 205 | * Fix it, but check if it's kernel or user first.. |
| 206 | */ |
| 207 | |
| 208 | bad_area: |
| 209 | up_read(&mm->mmap_sem); |
| 210 | |
| 211 | bad_area_nosemaphore: |
| 212 | DPG(show_registers(regs)); |
| 213 | |
| 214 | /* User mode accesses just cause a SIGSEGV */ |
| 215 | |
| 216 | if (user_mode(regs)) { |
Jesper Nilsson | b4e8a18 | 2010-08-04 17:42:43 +0200 | [diff] [blame] | 217 | printk(KERN_NOTICE "%s (pid %d) segfaults for page " |
| 218 | "address %08lx at pc %08lx\n", |
| 219 | tsk->comm, tsk->pid, |
| 220 | address, instruction_pointer(regs)); |
Jesper Nilsson | 2d495eb | 2010-08-04 17:48:40 +0200 | [diff] [blame] | 221 | |
| 222 | /* With DPG on, we've already dumped registers above. */ |
| 223 | DPG(if (0)) |
| 224 | show_registers(regs); |
| 225 | |
Jesper Nilsson | b4e8a18 | 2010-08-04 17:42:43 +0200 | [diff] [blame] | 226 | #ifdef CONFIG_NO_SEGFAULT_TERMINATION |
| 227 | DECLARE_WAIT_QUEUE_HEAD(wq); |
| 228 | wait_event_interruptible(wq, 0 == 1); |
| 229 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | info.si_signo = SIGSEGV; |
| 231 | info.si_errno = 0; |
| 232 | /* info.si_code has been set above */ |
| 233 | info.si_addr = (void *)address; |
| 234 | force_sig_info(SIGSEGV, &info, tsk); |
Jesper Nilsson | b4e8a18 | 2010-08-04 17:42:43 +0200 | [diff] [blame] | 235 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
| 239 | no_context: |
| 240 | |
| 241 | /* Are we prepared to handle this kernel fault? |
| 242 | * |
Jesper Nilsson | 3e1fdc4 | 2007-11-30 13:59:57 +0100 | [diff] [blame] | 243 | * (The kernel has valid exception-points in the source |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 244 | * when it accesses user-memory. When it fails in one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | * of those points, we find it in a table and do a jump |
| 246 | * to some fixup code that loads an appropriate error |
| 247 | * code) |
| 248 | */ |
| 249 | |
| 250 | if (find_fixup_code(regs)) |
| 251 | return; |
| 252 | |
| 253 | /* |
| 254 | * Oops. The kernel tried to access some bad page. We'll have to |
| 255 | * terminate things with extreme prejudice. |
| 256 | */ |
| 257 | |
Jesper Nilsson | 3e1fdc4 | 2007-11-30 13:59:57 +0100 | [diff] [blame] | 258 | if (!oops_in_progress) { |
| 259 | oops_in_progress = 1; |
| 260 | if ((unsigned long) (address) < PAGE_SIZE) |
| 261 | printk(KERN_ALERT "Unable to handle kernel NULL " |
| 262 | "pointer dereference"); |
| 263 | else |
| 264 | printk(KERN_ALERT "Unable to handle kernel access" |
| 265 | " at virtual address %08lx\n", address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Jesper Nilsson | 3e1fdc4 | 2007-11-30 13:59:57 +0100 | [diff] [blame] | 267 | die_if_kernel("Oops", regs, (writeaccess << 1) | protection); |
| 268 | oops_in_progress = 0; |
| 269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | do_exit(SIGKILL); |
| 272 | |
| 273 | /* |
| 274 | * We ran out of memory, or some other thing happened to us that made |
| 275 | * us unable to handle the page fault gracefully. |
| 276 | */ |
| 277 | |
| 278 | out_of_memory: |
| 279 | up_read(&mm->mmap_sem); |
Jesper Nilsson | 3648bdf | 2010-07-30 18:34:16 +0200 | [diff] [blame] | 280 | if (!user_mode(regs)) |
| 281 | goto no_context; |
| 282 | pagefault_out_of_memory(); |
| 283 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | do_sigbus: |
| 286 | up_read(&mm->mmap_sem); |
| 287 | |
| 288 | /* |
| 289 | * Send a sigbus, regardless of whether we were in kernel |
| 290 | * or user mode. |
| 291 | */ |
| 292 | info.si_signo = SIGBUS; |
| 293 | info.si_errno = 0; |
| 294 | info.si_code = BUS_ADRERR; |
| 295 | info.si_addr = (void *)address; |
| 296 | force_sig_info(SIGBUS, &info, tsk); |
| 297 | |
| 298 | /* Kernel mode? Handle exceptions or die */ |
| 299 | if (!user_mode(regs)) |
| 300 | goto no_context; |
| 301 | return; |
| 302 | |
| 303 | vmalloc_fault: |
| 304 | { |
| 305 | /* |
| 306 | * Synchronize this task's top level page-table |
| 307 | * with the 'reference' page table. |
| 308 | * |
| 309 | * Use current_pgd instead of tsk->active_mm->pgd |
| 310 | * since the latter might be unavailable if this |
| 311 | * code is executed in a misfortunately run irq |
| 312 | * (like inside schedule() between switch_mm and |
| 313 | * switch_to...). |
| 314 | */ |
| 315 | |
| 316 | int offset = pgd_index(address); |
| 317 | pgd_t *pgd, *pgd_k; |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 318 | pud_t *pud, *pud_k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | pmd_t *pmd, *pmd_k; |
| 320 | pte_t *pte_k; |
| 321 | |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 322 | pgd = (pgd_t *)per_cpu(current_pgd, smp_processor_id()) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | pgd_k = init_mm.pgd + offset; |
| 324 | |
| 325 | /* Since we're two-level, we don't need to do both |
| 326 | * set_pgd and set_pmd (they do the same thing). If |
| 327 | * we go three-level at some point, do the right thing |
Jesper Nilsson | 3e1fdc4 | 2007-11-30 13:59:57 +0100 | [diff] [blame] | 328 | * with pgd_present and set_pgd here. |
| 329 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | * Also, since the vmalloc area is global, we don't |
| 331 | * need to copy individual PTE's, it is enough to |
| 332 | * copy the pgd pointer into the pte page of the |
| 333 | * root task. If that is there, we'll find our pte if |
| 334 | * it exists. |
| 335 | */ |
| 336 | |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 337 | pud = pud_offset(pgd, address); |
| 338 | pud_k = pud_offset(pgd_k, address); |
| 339 | if (!pud_present(*pud_k)) |
| 340 | goto no_context; |
| 341 | |
| 342 | pmd = pmd_offset(pud, address); |
| 343 | pmd_k = pmd_offset(pud_k, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | if (!pmd_present(*pmd_k)) |
| 346 | goto bad_area_nosemaphore; |
| 347 | |
| 348 | set_pmd(pmd, *pmd_k); |
| 349 | |
| 350 | /* Make sure the actual PTE exists as well to |
| 351 | * catch kernel vmalloc-area accesses to non-mapped |
| 352 | * addresses. If we don't do this, this will just |
| 353 | * silently loop forever. |
| 354 | */ |
| 355 | |
| 356 | pte_k = pte_offset_kernel(pmd_k, address); |
| 357 | if (!pte_present(*pte_k)) |
| 358 | goto no_context; |
| 359 | |
| 360 | return; |
| 361 | } |
| 362 | } |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 363 | |
| 364 | /* Find fixup code. */ |
| 365 | int |
| 366 | find_fixup_code(struct pt_regs *regs) |
| 367 | { |
| 368 | const struct exception_table_entry *fixup; |
Jesper Nilsson | a90993c | 2010-08-04 14:39:01 +0200 | [diff] [blame] | 369 | /* in case of delay slot fault (v32) */ |
| 370 | unsigned long ip = (instruction_pointer(regs) & ~0x1); |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 371 | |
Jesper Nilsson | a90993c | 2010-08-04 14:39:01 +0200 | [diff] [blame] | 372 | fixup = search_exception_tables(ip); |
| 373 | if (fixup != 0) { |
Mikael Starvik | 4f18cfb | 2005-07-27 11:44:39 -0700 | [diff] [blame] | 374 | /* Adjust the instruction pointer in the stackframe. */ |
| 375 | instruction_pointer(regs) = fixup->fixup; |
| 376 | arch_fixup(regs); |
| 377 | return 1; |
| 378 | } |
| 379 | |
| 380 | return 0; |
| 381 | } |