Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2006 Atmel Corporation |
| 3 | * |
| 4 | * Based on linux/arch/sh/mm/fault.c: |
| 5 | * Copyright (C) 1999 Niibe Yutaka |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/pagemap.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 15 | #include <linux/kdebug.h> |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 16 | #include <linux/kprobes.h> |
| 17 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 18 | #include <asm/mmu_context.h> |
| 19 | #include <asm/sysreg.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 20 | #include <asm/tlb.h> |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 21 | #include <asm/uaccess.h> |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 22 | |
| 23 | #ifdef CONFIG_KPROBES |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 24 | static inline int notify_page_fault(struct pt_regs *regs, int trap) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 25 | { |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 26 | int ret = 0; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 27 | |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 28 | if (!user_mode(regs)) { |
| 29 | if (kprobe_running() && kprobe_fault_handler(regs, trap)) |
| 30 | ret = 1; |
| 31 | } |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 32 | |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 33 | return ret; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 34 | } |
| 35 | #else |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 36 | static inline int notify_page_fault(struct pt_regs *regs, int trap) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 37 | { |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 38 | return 0; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 39 | } |
| 40 | #endif |
| 41 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 42 | int exception_trace = 1; |
| 43 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 44 | /* |
| 45 | * This routine handles page faults. It determines the address and the |
| 46 | * problem, and then passes it off to one of the appropriate routines. |
| 47 | * |
| 48 | * ecr is the Exception Cause Register. Possible values are: |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 49 | * 6: Protection fault (instruction access) |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 50 | * 15: Protection fault (read access) |
| 51 | * 16: Protection fault (write access) |
| 52 | * 20: Page not found (instruction access) |
| 53 | * 24: Page not found (read access) |
| 54 | * 28: Page not found (write access) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 55 | */ |
| 56 | asmlinkage void do_page_fault(unsigned long ecr, struct pt_regs *regs) |
| 57 | { |
| 58 | struct task_struct *tsk; |
| 59 | struct mm_struct *mm; |
| 60 | struct vm_area_struct *vma; |
| 61 | const struct exception_table_entry *fixup; |
| 62 | unsigned long address; |
| 63 | unsigned long page; |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 64 | int writeaccess; |
| 65 | long signr; |
| 66 | int code; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 67 | int fault; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 68 | |
Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 69 | if (notify_page_fault(regs, ecr)) |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 70 | return; |
| 71 | |
| 72 | address = sysreg_read(TLBEAR); |
| 73 | |
| 74 | tsk = current; |
| 75 | mm = tsk->mm; |
| 76 | |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 77 | signr = SIGSEGV; |
| 78 | code = SEGV_MAPERR; |
| 79 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 80 | /* |
| 81 | * If we're in an interrupt or have no user context, we must |
| 82 | * not take the fault... |
| 83 | */ |
| 84 | if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM)) |
| 85 | goto no_context; |
| 86 | |
| 87 | local_irq_enable(); |
| 88 | |
| 89 | down_read(&mm->mmap_sem); |
| 90 | |
| 91 | vma = find_vma(mm, address); |
| 92 | if (!vma) |
| 93 | goto bad_area; |
| 94 | if (vma->vm_start <= address) |
| 95 | goto good_area; |
| 96 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 97 | goto bad_area; |
| 98 | if (expand_stack(vma, address)) |
| 99 | goto bad_area; |
| 100 | |
| 101 | /* |
| 102 | * Ok, we have a good vm_area for this memory access, so we |
| 103 | * can handle it... |
| 104 | */ |
| 105 | good_area: |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 106 | code = SEGV_ACCERR; |
| 107 | writeaccess = 0; |
| 108 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 109 | switch (ecr) { |
| 110 | case ECR_PROTECTION_X: |
| 111 | case ECR_TLB_MISS_X: |
| 112 | if (!(vma->vm_flags & VM_EXEC)) |
| 113 | goto bad_area; |
| 114 | break; |
| 115 | case ECR_PROTECTION_R: |
| 116 | case ECR_TLB_MISS_R: |
| 117 | if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))) |
| 118 | goto bad_area; |
| 119 | break; |
| 120 | case ECR_PROTECTION_W: |
| 121 | case ECR_TLB_MISS_W: |
| 122 | if (!(vma->vm_flags & VM_WRITE)) |
| 123 | goto bad_area; |
| 124 | writeaccess = 1; |
| 125 | break; |
| 126 | default: |
| 127 | panic("Unhandled case %lu in do_page_fault!", ecr); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * If for any reason at all we couldn't handle the fault, make |
| 132 | * sure we exit gracefully rather than endlessly redo the |
| 133 | * fault. |
| 134 | */ |
| 135 | survive: |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 136 | fault = handle_mm_fault(mm, vma, address, writeaccess); |
| 137 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 138 | if (fault & VM_FAULT_OOM) |
| 139 | goto out_of_memory; |
| 140 | else if (fault & VM_FAULT_SIGBUS) |
| 141 | goto do_sigbus; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 142 | BUG(); |
| 143 | } |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 144 | if (fault & VM_FAULT_MAJOR) |
| 145 | tsk->maj_flt++; |
| 146 | else |
| 147 | tsk->min_flt++; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 148 | |
| 149 | up_read(&mm->mmap_sem); |
| 150 | return; |
| 151 | |
| 152 | /* |
| 153 | * Something tried to access memory that isn't in our memory |
| 154 | * map. Fix it, but check if it's kernel or user first... |
| 155 | */ |
| 156 | bad_area: |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 157 | up_read(&mm->mmap_sem); |
| 158 | |
| 159 | if (user_mode(regs)) { |
Andrea Righi | 126187f | 2007-05-23 14:14:52 -0700 | [diff] [blame] | 160 | if (exception_trace && printk_ratelimit()) |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 161 | printk("%s%s[%d]: segfault at %08lx pc %08lx " |
| 162 | "sp %08lx ecr %lu\n", |
| 163 | is_init(tsk) ? KERN_EMERG : KERN_INFO, |
| 164 | tsk->comm, tsk->pid, address, regs->pc, |
| 165 | regs->sp, ecr); |
| 166 | _exception(SIGSEGV, regs, code, address); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | |
| 170 | no_context: |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 171 | /* Are we prepared to handle this kernel fault? */ |
| 172 | fixup = search_exception_tables(regs->pc); |
| 173 | if (fixup) { |
| 174 | regs->pc = fixup->fixup; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Oops. The kernel tried to access some bad page. We'll have |
| 180 | * to terminate things with extreme prejudice. |
| 181 | */ |
| 182 | if (address < PAGE_SIZE) |
| 183 | printk(KERN_ALERT |
| 184 | "Unable to handle kernel NULL pointer dereference"); |
| 185 | else |
| 186 | printk(KERN_ALERT |
| 187 | "Unable to handle kernel paging request"); |
| 188 | printk(" at virtual address %08lx\n", address); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 189 | |
| 190 | page = sysreg_read(PTBR); |
| 191 | printk(KERN_ALERT "ptbr = %08lx", page); |
| 192 | if (page) { |
| 193 | page = ((unsigned long *)page)[address >> 22]; |
| 194 | printk(" pgd = %08lx", page); |
| 195 | if (page & _PAGE_PRESENT) { |
| 196 | page &= PAGE_MASK; |
| 197 | address &= 0x003ff000; |
| 198 | page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT]; |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 199 | printk(" pte = %08lx", page); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 202 | printk("\n"); |
| 203 | die("Kernel access of bad area", regs, signr); |
| 204 | return; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * We ran out of memory, or some other thing happened to us |
| 208 | * that made us unable to handle the page fault gracefully. |
| 209 | */ |
| 210 | out_of_memory: |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 211 | up_read(&mm->mmap_sem); |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 212 | if (is_init(current)) { |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 213 | yield(); |
| 214 | down_read(&mm->mmap_sem); |
| 215 | goto survive; |
| 216 | } |
| 217 | printk("VM: Killing process %s\n", tsk->comm); |
| 218 | if (user_mode(regs)) |
| 219 | do_exit(SIGKILL); |
| 220 | goto no_context; |
| 221 | |
| 222 | do_sigbus: |
| 223 | up_read(&mm->mmap_sem); |
| 224 | |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 225 | /* Kernel mode? Handle exceptions or die */ |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 226 | signr = SIGBUS; |
| 227 | code = BUS_ADRERR; |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 228 | if (!user_mode(regs)) |
| 229 | goto no_context; |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 230 | |
| 231 | if (exception_trace) |
| 232 | printk("%s%s[%d]: bus error at %08lx pc %08lx " |
| 233 | "sp %08lx ecr %lu\n", |
| 234 | is_init(tsk) ? KERN_EMERG : KERN_INFO, |
| 235 | tsk->comm, tsk->pid, address, regs->pc, |
| 236 | regs->sp, ecr); |
| 237 | |
| 238 | _exception(SIGBUS, regs, BUS_ADRERR, address); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | asmlinkage void do_bus_error(unsigned long addr, int write_access, |
| 242 | struct pt_regs *regs) |
| 243 | { |
| 244 | printk(KERN_ALERT |
| 245 | "Bus error at physical address 0x%08lx (%s access)\n", |
| 246 | addr, write_access ? "write" : "read"); |
| 247 | printk(KERN_INFO "DTLB dump:\n"); |
| 248 | dump_dtlb(); |
Haavard Skinnemoen | 623b035 | 2007-03-13 17:59:11 +0100 | [diff] [blame] | 249 | die("Bus Error", regs, SIGKILL); |
Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
| 253 | * This functionality is currently not possible to implement because |
| 254 | * we're using segmentation to ensure a fixed mapping of the kernel |
| 255 | * virtual address space. |
| 256 | * |
| 257 | * It would be possible to implement this, but it would require us to |
| 258 | * disable segmentation at startup and load the kernel mappings into |
| 259 | * the TLB like any other pages. There will be lots of trickery to |
| 260 | * avoid recursive invocation of the TLB miss handler, though... |
| 261 | */ |
| 262 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 263 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 264 | { |
| 265 | |
| 266 | } |
| 267 | EXPORT_SYMBOL(kernel_map_pages); |
| 268 | #endif |