Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Page fault handler for SH with an MMU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Copyright (C) 1999 Niibe Yutaka |
Paul Mundt | 037c10a | 2008-09-08 12:22:47 +0900 | [diff] [blame] | 5 | * Copyright (C) 2003 - 2008 Paul Mundt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Based on linux/arch/i386/mm/fault.c: |
| 8 | * Copyright (C) 1995 Linus Torvalds |
Paul Mundt | 26ff6c1 | 2006-09-27 15:13:36 +0900 | [diff] [blame] | 9 | * |
| 10 | * This file is subject to the terms and conditions of the GNU General Public |
| 11 | * License. See the file "COPYING" in the main directory of this archive |
| 12 | * for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mm.h> |
Paul Mundt | 0f08f33 | 2006-09-27 17:03:56 +0900 | [diff] [blame] | 16 | #include <linux/hardirq.h> |
| 17 | #include <linux/kprobes.h> |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 18 | #include <asm/io_trapped.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/mmu_context.h> |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 21 | #include <asm/tlbflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/kgdb.h> |
| 23 | |
Paul Mundt | 037c10a | 2008-09-08 12:22:47 +0900 | [diff] [blame] | 24 | static inline int notify_page_fault(struct pt_regs *regs, int trap) |
| 25 | { |
| 26 | int ret = 0; |
| 27 | |
Paul Mundt | 887f1ae | 2008-09-21 12:06:43 +0900 | [diff] [blame^] | 28 | #ifdef CONFIG_KPROBES |
Paul Mundt | 037c10a | 2008-09-08 12:22:47 +0900 | [diff] [blame] | 29 | if (!user_mode(regs)) { |
| 30 | preempt_disable(); |
| 31 | if (kprobe_running() && kprobe_fault_handler(regs, trap)) |
| 32 | ret = 1; |
| 33 | preempt_enable(); |
| 34 | } |
Paul Mundt | 887f1ae | 2008-09-21 12:06:43 +0900 | [diff] [blame^] | 35 | #endif |
Paul Mundt | 037c10a | 2008-09-08 12:22:47 +0900 | [diff] [blame] | 36 | |
| 37 | return ret; |
| 38 | } |
Paul Mundt | 037c10a | 2008-09-08 12:22:47 +0900 | [diff] [blame] | 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* |
| 41 | * This routine handles page faults. It determines the address, |
| 42 | * and the problem, and then passes it off to one of the appropriate |
| 43 | * routines. |
| 44 | */ |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 45 | asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, |
| 46 | unsigned long writeaccess, |
| 47 | unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | struct task_struct *tsk; |
| 50 | struct mm_struct *mm; |
| 51 | struct vm_area_struct * vma; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 52 | int si_code; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 53 | int fault; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 54 | siginfo_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Paul Mundt | 887f1ae | 2008-09-21 12:06:43 +0900 | [diff] [blame^] | 56 | if (notify_page_fault(regs, lookup_exception_vector())) |
Paul Mundt | 037c10a | 2008-09-08 12:22:47 +0900 | [diff] [blame] | 57 | return; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #ifdef CONFIG_SH_KGDB |
| 60 | if (kgdb_nofault && kgdb_bus_err_hook) |
| 61 | kgdb_bus_err_hook(); |
| 62 | #endif |
| 63 | |
| 64 | tsk = current; |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 65 | si_code = SEGV_MAPERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 67 | if (unlikely(address >= TASK_SIZE)) { |
| 68 | /* |
| 69 | * Synchronize this task's top level page-table |
| 70 | * with the 'reference' page table. |
| 71 | * |
| 72 | * Do _not_ use "tsk" here. We might be inside |
| 73 | * an interrupt in the middle of a task switch.. |
| 74 | */ |
| 75 | int offset = pgd_index(address); |
| 76 | pgd_t *pgd, *pgd_k; |
| 77 | pud_t *pud, *pud_k; |
| 78 | pmd_t *pmd, *pmd_k; |
| 79 | |
| 80 | pgd = get_TTB() + offset; |
| 81 | pgd_k = swapper_pg_dir + offset; |
| 82 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 83 | if (!pgd_present(*pgd)) { |
| 84 | if (!pgd_present(*pgd_k)) |
| 85 | goto bad_area_nosemaphore; |
| 86 | set_pgd(pgd, *pgd_k); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | pud = pud_offset(pgd, address); |
| 91 | pud_k = pud_offset(pgd_k, address); |
Stuart Menefy | 96e14e5 | 2008-09-05 16:17:15 +0900 | [diff] [blame] | 92 | |
| 93 | if (!pud_present(*pud)) { |
| 94 | if (!pud_present(*pud_k)) |
| 95 | goto bad_area_nosemaphore; |
| 96 | set_pud(pud, *pud_k); |
| 97 | return; |
| 98 | } |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 99 | |
| 100 | pmd = pmd_offset(pud, address); |
| 101 | pmd_k = pmd_offset(pud_k, address); |
| 102 | if (pmd_present(*pmd) || !pmd_present(*pmd_k)) |
| 103 | goto bad_area_nosemaphore; |
| 104 | set_pmd(pmd, *pmd_k); |
| 105 | |
| 106 | return; |
| 107 | } |
| 108 | |
Stuart Menefy | f2fb4e4 | 2008-07-02 17:51:23 +0900 | [diff] [blame] | 109 | /* Only enable interrupts if they were on before the fault */ |
| 110 | if ((regs->sr & SR_IMASK) != SR_IMASK) { |
| 111 | trace_hardirqs_on(); |
| 112 | local_irq_enable(); |
| 113 | } |
| 114 | |
| 115 | mm = tsk->mm; |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* |
| 118 | * If we're in an interrupt or have no user |
| 119 | * context, we must not take the fault.. |
| 120 | */ |
| 121 | if (in_atomic() || !mm) |
| 122 | goto no_context; |
| 123 | |
| 124 | down_read(&mm->mmap_sem); |
| 125 | |
| 126 | vma = find_vma(mm, address); |
| 127 | if (!vma) |
| 128 | goto bad_area; |
| 129 | if (vma->vm_start <= address) |
| 130 | goto good_area; |
| 131 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 132 | goto bad_area; |
| 133 | if (expand_stack(vma, address)) |
| 134 | goto bad_area; |
| 135 | /* |
| 136 | * Ok, we have a good vm_area for this memory access, so |
| 137 | * we can handle it.. |
| 138 | */ |
| 139 | good_area: |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 140 | si_code = SEGV_ACCERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | if (writeaccess) { |
| 142 | if (!(vma->vm_flags & VM_WRITE)) |
| 143 | goto bad_area; |
| 144 | } else { |
Jason Baron | df67b3d | 2006-09-29 01:58:58 -0700 | [diff] [blame] | 145 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | goto bad_area; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * If for any reason at all we couldn't handle the fault, |
| 151 | * make sure we exit gracefully rather than endlessly redo |
| 152 | * the fault. |
| 153 | */ |
| 154 | survive: |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 155 | fault = handle_mm_fault(mm, vma, address, writeaccess); |
| 156 | if (unlikely(fault & VM_FAULT_ERROR)) { |
| 157 | if (fault & VM_FAULT_OOM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | goto out_of_memory; |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 159 | else if (fault & VM_FAULT_SIGBUS) |
| 160 | goto do_sigbus; |
| 161 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 163 | if (fault & VM_FAULT_MAJOR) |
| 164 | tsk->maj_flt++; |
| 165 | else |
| 166 | tsk->min_flt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | up_read(&mm->mmap_sem); |
| 169 | return; |
| 170 | |
| 171 | /* |
| 172 | * Something tried to access memory that isn't in our memory map.. |
| 173 | * Fix it, but check if it's kernel or user first.. |
| 174 | */ |
| 175 | bad_area: |
| 176 | up_read(&mm->mmap_sem); |
| 177 | |
Stuart Menefy | 99a596f | 2006-11-21 15:38:05 +0900 | [diff] [blame] | 178 | bad_area_nosemaphore: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | if (user_mode(regs)) { |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 180 | info.si_signo = SIGSEGV; |
| 181 | info.si_errno = 0; |
| 182 | info.si_code = si_code; |
| 183 | info.si_addr = (void *) address; |
| 184 | force_sig_info(SIGSEGV, &info, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | no_context: |
| 189 | /* Are we prepared to handle this kernel fault? */ |
| 190 | if (fixup_exception(regs)) |
| 191 | return; |
| 192 | |
Magnus Damm | e7cc9a7 | 2008-02-07 20:18:21 +0900 | [diff] [blame] | 193 | if (handle_trapped_io(regs, address)) |
| 194 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | /* |
| 196 | * Oops. The kernel tried to access some bad page. We'll have to |
| 197 | * terminate things with extreme prejudice. |
| 198 | * |
| 199 | */ |
Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 200 | |
| 201 | bust_spinlocks(1); |
| 202 | |
| 203 | if (oops_may_print()) { |
Paul Mundt | b62ad83 | 2008-01-10 14:07:03 +0900 | [diff] [blame] | 204 | unsigned long page; |
Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 205 | |
| 206 | if (address < PAGE_SIZE) |
| 207 | printk(KERN_ALERT "Unable to handle kernel NULL " |
| 208 | "pointer dereference"); |
| 209 | else |
| 210 | printk(KERN_ALERT "Unable to handle kernel paging " |
| 211 | "request"); |
| 212 | printk(" at virtual address %08lx\n", address); |
| 213 | printk(KERN_ALERT "pc = %08lx\n", regs->pc); |
| 214 | page = (unsigned long)get_TTB(); |
| 215 | if (page) { |
Paul Mundt | 06f862c | 2007-08-01 16:39:51 +0900 | [diff] [blame] | 216 | page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT]; |
Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 217 | printk(KERN_ALERT "*pde = %08lx\n", page); |
| 218 | if (page & _PAGE_PRESENT) { |
| 219 | page &= PAGE_MASK; |
| 220 | address &= 0x003ff000; |
| 221 | page = ((__typeof__(page) *) |
| 222 | __va(page))[address >> |
| 223 | PAGE_SHIFT]; |
| 224 | printk(KERN_ALERT "*pte = %08lx\n", page); |
| 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | die("Oops", regs, writeaccess); |
Paul Mundt | 0630e45 | 2007-06-18 19:02:47 +0900 | [diff] [blame] | 230 | bust_spinlocks(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | do_exit(SIGKILL); |
| 232 | |
| 233 | /* |
| 234 | * We ran out of memory, or some other thing happened to us that made |
| 235 | * us unable to handle the page fault gracefully. |
| 236 | */ |
| 237 | out_of_memory: |
| 238 | up_read(&mm->mmap_sem); |
Serge E. Hallyn | b460cbc | 2007-10-18 23:39:52 -0700 | [diff] [blame] | 239 | if (is_global_init(current)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | yield(); |
| 241 | down_read(&mm->mmap_sem); |
| 242 | goto survive; |
| 243 | } |
| 244 | printk("VM: killing process %s\n", tsk->comm); |
| 245 | if (user_mode(regs)) |
Will Schmidt | dcca2bd | 2007-10-16 01:24:18 -0700 | [diff] [blame] | 246 | do_group_exit(SIGKILL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | goto no_context; |
| 248 | |
| 249 | do_sigbus: |
| 250 | up_read(&mm->mmap_sem); |
| 251 | |
| 252 | /* |
| 253 | * Send a sigbus, regardless of whether we were in kernel |
| 254 | * or user mode. |
| 255 | */ |
Stuart Menefy | b5a1bcb | 2006-11-21 13:34:04 +0900 | [diff] [blame] | 256 | info.si_signo = SIGBUS; |
| 257 | info.si_errno = 0; |
| 258 | info.si_code = BUS_ADRERR; |
| 259 | info.si_addr = (void *)address; |
| 260 | force_sig_info(SIGBUS, &info, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | /* Kernel mode? Handle exceptions or die */ |
| 263 | if (!user_mode(regs)) |
| 264 | goto no_context; |
| 265 | } |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 266 | |
| 267 | #ifdef CONFIG_SH_STORE_QUEUES |
| 268 | /* |
| 269 | * This is a special case for the SH-4 store queues, as pages for this |
| 270 | * space still need to be faulted in before it's possible to flush the |
| 271 | * store queue cache for writeout to the remapped region. |
| 272 | */ |
| 273 | #define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000) |
| 274 | #else |
| 275 | #define P3_ADDR_MAX P4SEG |
| 276 | #endif |
| 277 | |
| 278 | /* |
| 279 | * Called with interrupts disabled. |
| 280 | */ |
| 281 | asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs, |
| 282 | unsigned long writeaccess, |
| 283 | unsigned long address) |
| 284 | { |
| 285 | pgd_t *pgd; |
| 286 | pud_t *pud; |
| 287 | pmd_t *pmd; |
| 288 | pte_t *pte; |
| 289 | pte_t entry; |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 290 | |
Paul Mundt | 887f1ae | 2008-09-21 12:06:43 +0900 | [diff] [blame^] | 291 | if (notify_page_fault(regs, lookup_exception_vector())) |
Paul Mundt | 037c10a | 2008-09-08 12:22:47 +0900 | [diff] [blame] | 292 | return 0; |
| 293 | |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 294 | #ifdef CONFIG_SH_KGDB |
| 295 | if (kgdb_nofault && kgdb_bus_err_hook) |
| 296 | kgdb_bus_err_hook(); |
| 297 | #endif |
| 298 | |
| 299 | /* |
| 300 | * We don't take page faults for P1, P2, and parts of P4, these |
| 301 | * are always mapped, whether it be due to legacy behaviour in |
| 302 | * 29-bit mode, or due to PMB configuration in 32-bit mode. |
| 303 | */ |
| 304 | if (address >= P3SEG && address < P3_ADDR_MAX) { |
| 305 | pgd = pgd_offset_k(address); |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 306 | } else { |
Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 307 | if (unlikely(address >= TASK_SIZE || !current->mm)) |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 308 | return 1; |
| 309 | |
Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 310 | pgd = pgd_offset(current->mm, address); |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | pud = pud_offset(pgd, address); |
| 314 | if (pud_none_or_clear_bad(pud)) |
| 315 | return 1; |
| 316 | pmd = pmd_offset(pud, address); |
| 317 | if (pmd_none_or_clear_bad(pmd)) |
| 318 | return 1; |
| 319 | |
Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 320 | pte = pte_offset_kernel(pmd, address); |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 321 | entry = *pte; |
| 322 | if (unlikely(pte_none(entry) || pte_not_present(entry))) |
Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 323 | return 1; |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 324 | if (unlikely(writeaccess && !pte_write(entry))) |
Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 325 | return 1; |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 326 | |
| 327 | if (writeaccess) |
| 328 | entry = pte_mkdirty(entry); |
| 329 | entry = pte_mkyoung(entry); |
| 330 | |
Hideo Saito | a602cc0 | 2008-02-14 14:45:08 +0900 | [diff] [blame] | 331 | #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP) |
| 332 | /* |
| 333 | * ITLB is not affected by "ldtlb" instruction. |
| 334 | * So, we need to flush the entry by ourselves. |
| 335 | */ |
| 336 | local_flush_tlb_one(get_asid(), address & PAGE_MASK); |
| 337 | #endif |
| 338 | |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 339 | set_pte(pte, entry); |
| 340 | update_mmu_cache(NULL, address, entry); |
Paul Mundt | 0f1a394 | 2007-11-19 13:05:18 +0900 | [diff] [blame] | 341 | |
| 342 | return 0; |
Paul Mundt | db2e1fa | 2007-02-14 14:13:10 +0900 | [diff] [blame] | 343 | } |