David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 1 | /* MN10300 MMU Fault handler |
| 2 | * |
| 3 | * Copyright (C) 2007 Matsushita Electric Industrial Co., Ltd. |
| 4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 5 | * Modified by David Howells (dhowells@redhat.com) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public Licence |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the Licence, or (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/signal.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/ptrace.h> |
| 20 | #include <linux/mman.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/smp.h> |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/vt_kern.h> /* For unblank_screen() */ |
| 26 | |
| 27 | #include <asm/system.h> |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <asm/pgalloc.h> |
| 30 | #include <asm/hardirq.h> |
| 31 | #include <asm/gdb-stub.h> |
| 32 | #include <asm/cpu-regs.h> |
| 33 | |
| 34 | /* |
| 35 | * Unlock any spinlocks which will prevent us from getting the |
| 36 | * message out |
| 37 | */ |
| 38 | void bust_spinlocks(int yes) |
| 39 | { |
| 40 | if (yes) { |
| 41 | oops_in_progress = 1; |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 42 | } else { |
| 43 | int loglevel_save = console_loglevel; |
| 44 | #ifdef CONFIG_VT |
| 45 | unblank_screen(); |
| 46 | #endif |
| 47 | oops_in_progress = 0; |
| 48 | /* |
| 49 | * OK, the message is on the console. Now we call printk() |
| 50 | * without oops_in_progress set so that printk will give klogd |
| 51 | * a poke. Hold onto your hats... |
| 52 | */ |
| 53 | console_loglevel = 15; /* NMI oopser may have shut the console |
| 54 | * up */ |
| 55 | printk(" "); |
| 56 | console_loglevel = loglevel_save; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void do_BUG(const char *file, int line) |
| 61 | { |
| 62 | bust_spinlocks(1); |
| 63 | printk(KERN_EMERG "------------[ cut here ]------------\n"); |
| 64 | printk(KERN_EMERG "kernel BUG at %s:%d!\n", file, line); |
| 65 | } |
| 66 | |
| 67 | #if 0 |
| 68 | static void print_pagetable_entries(pgd_t *pgdir, unsigned long address) |
| 69 | { |
| 70 | pgd_t *pgd; |
| 71 | pmd_t *pmd; |
| 72 | pte_t *pte; |
| 73 | |
| 74 | pgd = pgdir + __pgd_offset(address); |
| 75 | printk(KERN_DEBUG "pgd entry %p: %016Lx\n", |
| 76 | pgd, (long long) pgd_val(*pgd)); |
| 77 | |
| 78 | if (!pgd_present(*pgd)) { |
| 79 | printk(KERN_DEBUG "... pgd not present!\n"); |
| 80 | return; |
| 81 | } |
| 82 | pmd = pmd_offset(pgd, address); |
| 83 | printk(KERN_DEBUG "pmd entry %p: %016Lx\n", |
| 84 | pmd, (long long)pmd_val(*pmd)); |
| 85 | |
| 86 | if (!pmd_present(*pmd)) { |
| 87 | printk(KERN_DEBUG "... pmd not present!\n"); |
| 88 | return; |
| 89 | } |
| 90 | pte = pte_offset(pmd, address); |
| 91 | printk(KERN_DEBUG "pte entry %p: %016Lx\n", |
| 92 | pte, (long long) pte_val(*pte)); |
| 93 | |
| 94 | if (!pte_present(*pte)) |
| 95 | printk(KERN_DEBUG "... pte not present!\n"); |
| 96 | } |
| 97 | #endif |
| 98 | |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 99 | /* |
| 100 | * This routine handles page faults. It determines the address, |
| 101 | * and the problem, and then passes it off to one of the appropriate |
| 102 | * routines. |
| 103 | * |
| 104 | * fault_code: |
| 105 | * - LSW: either MMUFCR_IFC or MMUFCR_DFC as appropriate |
| 106 | * - MSW: 0 if data access, 1 if instruction access |
| 107 | * - bit 0: TLB miss flag |
| 108 | * - bit 1: initial write |
| 109 | * - bit 2: page invalid |
| 110 | * - bit 3: protection violation |
| 111 | * - bit 4: accessor (0=user 1=kernel) |
| 112 | * - bit 5: 0=read 1=write |
| 113 | * - bit 6-8: page protection spec |
| 114 | * - bit 9: illegal address |
| 115 | * - bit 16: 0=data 1=ins |
| 116 | * |
| 117 | */ |
| 118 | asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long fault_code, |
| 119 | unsigned long address) |
| 120 | { |
| 121 | struct vm_area_struct *vma; |
| 122 | struct task_struct *tsk; |
| 123 | struct mm_struct *mm; |
| 124 | unsigned long page; |
| 125 | siginfo_t info; |
| 126 | int write, fault; |
| 127 | |
| 128 | #ifdef CONFIG_GDBSTUB |
| 129 | /* handle GDB stub causing a fault */ |
| 130 | if (gdbstub_busy) { |
| 131 | gdbstub_exception(regs, TBR & TBR_INT_CODE); |
| 132 | return; |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #if 0 |
| 137 | printk(KERN_DEBUG "--- do_page_fault(%p,%s:%04lx,%08lx)\n", |
| 138 | regs, |
| 139 | fault_code & 0x10000 ? "ins" : "data", |
| 140 | fault_code & 0xffff, address); |
| 141 | #endif |
| 142 | |
| 143 | tsk = current; |
| 144 | |
| 145 | /* |
| 146 | * We fault-in kernel-space virtual memory on-demand. The |
| 147 | * 'reference' page table is init_mm.pgd. |
| 148 | * |
| 149 | * NOTE! We MUST NOT take any locks for this case. We may |
| 150 | * be in an interrupt or a critical region, and should |
| 151 | * only copy the information from the master page table, |
| 152 | * nothing more. |
| 153 | * |
| 154 | * This verifies that the fault happens in kernel space |
| 155 | * and that the fault was a page not present (invalid) error |
| 156 | */ |
| 157 | if (address >= VMALLOC_START && address < VMALLOC_END && |
| 158 | (fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR && |
| 159 | (fault_code & MMUFCR_xFC_PGINVAL) == MMUFCR_xFC_PGINVAL |
| 160 | ) |
| 161 | goto vmalloc_fault; |
| 162 | |
| 163 | mm = tsk->mm; |
| 164 | info.si_code = SEGV_MAPERR; |
| 165 | |
| 166 | /* |
| 167 | * If we're in an interrupt or have no user |
| 168 | * context, we must not take the fault.. |
| 169 | */ |
David Howells | d1c6d2e | 2008-09-11 17:18:56 +0100 | [diff] [blame] | 170 | if (in_atomic() || !mm) |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 171 | goto no_context; |
| 172 | |
| 173 | down_read(&mm->mmap_sem); |
| 174 | |
| 175 | vma = find_vma(mm, address); |
| 176 | if (!vma) |
| 177 | goto bad_area; |
| 178 | if (vma->vm_start <= address) |
| 179 | goto good_area; |
| 180 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 181 | goto bad_area; |
| 182 | |
| 183 | if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) { |
| 184 | /* accessing the stack below the stack pointer is always a |
| 185 | * bug */ |
| 186 | if ((address & PAGE_MASK) + 2 * PAGE_SIZE < regs->sp) { |
| 187 | #if 0 |
| 188 | printk(KERN_WARNING |
| 189 | "[%d] ### Access below stack @%lx (sp=%lx)\n", |
| 190 | current->pid, address, regs->sp); |
| 191 | printk(KERN_WARNING |
| 192 | "vma [%08x - %08x]\n", |
| 193 | vma->vm_start, vma->vm_end); |
| 194 | show_registers(regs); |
| 195 | printk(KERN_WARNING |
| 196 | "[%d] ### Code: [%08lx]" |
| 197 | " %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 198 | current->pid, |
| 199 | regs->pc, |
| 200 | ((u8 *) regs->pc)[0], |
| 201 | ((u8 *) regs->pc)[1], |
| 202 | ((u8 *) regs->pc)[2], |
| 203 | ((u8 *) regs->pc)[3], |
| 204 | ((u8 *) regs->pc)[4], |
| 205 | ((u8 *) regs->pc)[5], |
| 206 | ((u8 *) regs->pc)[6], |
| 207 | ((u8 *) regs->pc)[7] |
| 208 | ); |
| 209 | #endif |
| 210 | goto bad_area; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (expand_stack(vma, address)) |
| 215 | goto bad_area; |
| 216 | |
| 217 | /* |
| 218 | * Ok, we have a good vm_area for this memory access, so |
| 219 | * we can handle it.. |
| 220 | */ |
| 221 | good_area: |
| 222 | info.si_code = SEGV_ACCERR; |
| 223 | write = 0; |
| 224 | switch (fault_code & (MMUFCR_xFC_PGINVAL|MMUFCR_xFC_TYPE)) { |
| 225 | default: /* 3: write, present */ |
| 226 | case MMUFCR_xFC_TYPE_WRITE: |
| 227 | #ifdef TEST_VERIFY_AREA |
| 228 | if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR) |
| 229 | printk(KERN_DEBUG "WP fault at %08lx\n", regs->pc); |
| 230 | #endif |
| 231 | /* write to absent page */ |
| 232 | case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_WRITE: |
| 233 | if (!(vma->vm_flags & VM_WRITE)) |
| 234 | goto bad_area; |
| 235 | write++; |
| 236 | break; |
| 237 | |
| 238 | /* read from protected page */ |
| 239 | case MMUFCR_xFC_TYPE_READ: |
| 240 | goto bad_area; |
| 241 | |
| 242 | /* read from absent page present */ |
| 243 | case MMUFCR_xFC_PGINVAL | MMUFCR_xFC_TYPE_READ: |
| 244 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) |
| 245 | goto bad_area; |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * If for any reason at all we couldn't handle the fault, |
| 251 | * make sure we exit gracefully rather than endlessly redo |
| 252 | * the fault. |
| 253 | */ |
Linus Torvalds | d06063c | 2009-04-10 09:01:23 -0700 | [diff] [blame] | 254 | fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 255 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 256 | if (fault & VM_FAULT_OOM) |
| 257 | goto out_of_memory; |
| 258 | else if (fault & VM_FAULT_SIGBUS) |
| 259 | goto do_sigbus; |
| 260 | BUG(); |
| 261 | } |
| 262 | if (fault & VM_FAULT_MAJOR) |
| 263 | current->maj_flt++; |
| 264 | else |
| 265 | current->min_flt++; |
| 266 | |
| 267 | up_read(&mm->mmap_sem); |
| 268 | return; |
| 269 | |
| 270 | /* |
| 271 | * Something tried to access memory that isn't in our memory map.. |
| 272 | * Fix it, but check if it's kernel or user first.. |
| 273 | */ |
| 274 | bad_area: |
| 275 | up_read(&mm->mmap_sem); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 276 | |
| 277 | /* User mode accesses just cause a SIGSEGV */ |
| 278 | if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) { |
| 279 | info.si_signo = SIGSEGV; |
| 280 | info.si_errno = 0; |
| 281 | /* info.si_code has been set above */ |
| 282 | info.si_addr = (void *)address; |
| 283 | force_sig_info(SIGSEGV, &info, tsk); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | no_context: |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 288 | /* Are we prepared to handle this kernel fault? */ |
| 289 | if (fixup_exception(regs)) |
| 290 | return; |
| 291 | |
| 292 | /* |
| 293 | * Oops. The kernel tried to access some bad page. We'll have to |
| 294 | * terminate things with extreme prejudice. |
| 295 | */ |
| 296 | |
| 297 | bust_spinlocks(1); |
| 298 | |
| 299 | if (address < PAGE_SIZE) |
| 300 | printk(KERN_ALERT |
| 301 | "Unable to handle kernel NULL pointer dereference"); |
| 302 | else |
| 303 | printk(KERN_ALERT |
| 304 | "Unable to handle kernel paging request"); |
| 305 | printk(" at virtual address %08lx\n", address); |
| 306 | printk(" printing pc:\n"); |
| 307 | printk(KERN_ALERT "%08lx\n", regs->pc); |
| 308 | |
| 309 | #ifdef CONFIG_GDBSTUB |
| 310 | gdbstub_intercept( |
| 311 | regs, fault_code & 0x00010000 ? EXCEP_IAERROR : EXCEP_DAERROR); |
| 312 | #endif |
| 313 | |
| 314 | page = PTBR; |
| 315 | page = ((unsigned long *) __va(page))[address >> 22]; |
| 316 | printk(KERN_ALERT "*pde = %08lx\n", page); |
| 317 | if (page & 1) { |
| 318 | page &= PAGE_MASK; |
| 319 | address &= 0x003ff000; |
| 320 | page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT]; |
| 321 | printk(KERN_ALERT "*pte = %08lx\n", page); |
| 322 | } |
| 323 | |
| 324 | die("Oops", regs, fault_code); |
| 325 | do_exit(SIGKILL); |
| 326 | |
| 327 | /* |
| 328 | * We ran out of memory, or some other thing happened to us that made |
| 329 | * us unable to handle the page fault gracefully. |
| 330 | */ |
| 331 | out_of_memory: |
| 332 | up_read(&mm->mmap_sem); |
Akira Takeuchi | 368dd5a | 2010-10-27 17:28:55 +0100 | [diff] [blame] | 333 | printk(KERN_ALERT "VM: killing process %s\n", tsk->comm); |
| 334 | if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_USR) |
| 335 | do_exit(SIGKILL); |
| 336 | goto no_context; |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 337 | |
| 338 | do_sigbus: |
| 339 | up_read(&mm->mmap_sem); |
David Howells | b920de1 | 2008-02-08 04:19:31 -0800 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | * Send a sigbus, regardless of whether we were in kernel |
| 343 | * or user mode. |
| 344 | */ |
| 345 | info.si_signo = SIGBUS; |
| 346 | info.si_errno = 0; |
| 347 | info.si_code = BUS_ADRERR; |
| 348 | info.si_addr = (void *)address; |
| 349 | force_sig_info(SIGBUS, &info, tsk); |
| 350 | |
| 351 | /* Kernel mode? Handle exceptions or die */ |
| 352 | if ((fault_code & MMUFCR_xFC_ACCESS) == MMUFCR_xFC_ACCESS_SR) |
| 353 | goto no_context; |
| 354 | return; |
| 355 | |
| 356 | vmalloc_fault: |
| 357 | { |
| 358 | /* |
| 359 | * Synchronize this task's top level page-table |
| 360 | * with the 'reference' page table. |
| 361 | * |
| 362 | * Do _not_ use "tsk" here. We might be inside |
| 363 | * an interrupt in the middle of a task switch.. |
| 364 | */ |
| 365 | int index = pgd_index(address); |
| 366 | pgd_t *pgd, *pgd_k; |
| 367 | pud_t *pud, *pud_k; |
| 368 | pmd_t *pmd, *pmd_k; |
| 369 | pte_t *pte_k; |
| 370 | |
| 371 | pgd_k = init_mm.pgd + index; |
| 372 | |
| 373 | if (!pgd_present(*pgd_k)) |
| 374 | goto no_context; |
| 375 | |
| 376 | pud_k = pud_offset(pgd_k, address); |
| 377 | if (!pud_present(*pud_k)) |
| 378 | goto no_context; |
| 379 | |
| 380 | pmd_k = pmd_offset(pud_k, address); |
| 381 | if (!pmd_present(*pmd_k)) |
| 382 | goto no_context; |
| 383 | |
| 384 | pgd = (pgd_t *) PTBR + index; |
| 385 | pud = pud_offset(pgd, address); |
| 386 | pmd = pmd_offset(pud, address); |
| 387 | set_pmd(pmd, *pmd_k); |
| 388 | |
| 389 | pte_k = pte_offset_kernel(pmd_k, address); |
| 390 | if (!pte_present(*pte_k)) |
| 391 | goto no_context; |
| 392 | return; |
| 393 | } |
| 394 | } |