Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1 | /* |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 2 | * PowerPC version |
| 3 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) |
| 4 | * |
| 5 | * Derived from "arch/i386/mm/fault.c" |
| 6 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| 7 | * |
| 8 | * Modified by Cort Dougan and Paul Mackerras. |
| 9 | * |
| 10 | * Modified for PPC64 by Dave Engebretsen (engebret@ibm.com) |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/config.h> |
| 19 | #include <linux/signal.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/ptrace.h> |
| 26 | #include <linux/mman.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/highmem.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/kprobes.h> |
| 32 | |
| 33 | #include <asm/page.h> |
| 34 | #include <asm/pgtable.h> |
| 35 | #include <asm/mmu.h> |
| 36 | #include <asm/mmu_context.h> |
| 37 | #include <asm/system.h> |
| 38 | #include <asm/uaccess.h> |
| 39 | #include <asm/tlbflush.h> |
| 40 | #include <asm/kdebug.h> |
| 41 | #include <asm/siginfo.h> |
| 42 | |
Anil S Keshavamurthy | 4f9e87c | 2006-06-26 00:25:27 -0700 | [diff] [blame] | 43 | #ifdef CONFIG_KPROBES |
| 44 | ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain); |
| 45 | |
| 46 | /* Hook to register for page fault notifications */ |
| 47 | int register_page_fault_notifier(struct notifier_block *nb) |
| 48 | { |
| 49 | return atomic_notifier_chain_register(¬ify_page_fault_chain, nb); |
| 50 | } |
| 51 | |
| 52 | int unregister_page_fault_notifier(struct notifier_block *nb) |
| 53 | { |
| 54 | return atomic_notifier_chain_unregister(¬ify_page_fault_chain, nb); |
| 55 | } |
| 56 | |
| 57 | static inline int notify_page_fault(enum die_val val, const char *str, |
| 58 | struct pt_regs *regs, long err, int trap, int sig) |
| 59 | { |
| 60 | struct die_args args = { |
| 61 | .regs = regs, |
| 62 | .str = str, |
| 63 | .err = err, |
| 64 | .trapnr = trap, |
| 65 | .signr = sig |
| 66 | }; |
| 67 | return atomic_notifier_call_chain(¬ify_page_fault_chain, val, &args); |
| 68 | } |
| 69 | #else |
| 70 | static inline int notify_page_fault(enum die_val val, const char *str, |
| 71 | struct pt_regs *regs, long err, int trap, int sig) |
| 72 | { |
| 73 | return NOTIFY_DONE; |
| 74 | } |
| 75 | #endif |
| 76 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 77 | /* |
| 78 | * Check whether the instruction at regs->nip is a store using |
| 79 | * an update addressing form which will update r1. |
| 80 | */ |
| 81 | static int store_updates_sp(struct pt_regs *regs) |
| 82 | { |
| 83 | unsigned int inst; |
| 84 | |
| 85 | if (get_user(inst, (unsigned int __user *)regs->nip)) |
| 86 | return 0; |
| 87 | /* check for 1 in the rA field */ |
| 88 | if (((inst >> 16) & 0x1f) != 1) |
| 89 | return 0; |
| 90 | /* check major opcode */ |
| 91 | switch (inst >> 26) { |
| 92 | case 37: /* stwu */ |
| 93 | case 39: /* stbu */ |
| 94 | case 45: /* sthu */ |
| 95 | case 53: /* stfsu */ |
| 96 | case 55: /* stfdu */ |
| 97 | return 1; |
| 98 | case 62: /* std or stdu */ |
| 99 | return (inst & 3) == 1; |
| 100 | case 31: |
| 101 | /* check minor opcode */ |
| 102 | switch ((inst >> 1) & 0x3ff) { |
| 103 | case 181: /* stdux */ |
| 104 | case 183: /* stwux */ |
| 105 | case 247: /* stbux */ |
| 106 | case 439: /* sthux */ |
| 107 | case 695: /* stfsux */ |
| 108 | case 759: /* stfdux */ |
| 109 | return 1; |
| 110 | } |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
Kumar Gala | cffb09c | 2005-10-26 09:55:41 -0500 | [diff] [blame] | 115 | #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE)) |
Anton Blanchard | bce6c5f | 2006-01-09 15:47:04 +1100 | [diff] [blame] | 116 | static void do_dabr(struct pt_regs *regs, unsigned long address, |
| 117 | unsigned long error_code) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 118 | { |
| 119 | siginfo_t info; |
| 120 | |
| 121 | if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code, |
| 122 | 11, SIGSEGV) == NOTIFY_STOP) |
| 123 | return; |
| 124 | |
| 125 | if (debugger_dabr_match(regs)) |
| 126 | return; |
| 127 | |
| 128 | /* Clear the DABR */ |
| 129 | set_dabr(0); |
| 130 | |
| 131 | /* Deliver the signal to userspace */ |
| 132 | info.si_signo = SIGTRAP; |
| 133 | info.si_errno = 0; |
| 134 | info.si_code = TRAP_HWBKPT; |
Anton Blanchard | bce6c5f | 2006-01-09 15:47:04 +1100 | [diff] [blame] | 135 | info.si_addr = (void __user *)address; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 136 | force_sig_info(SIGTRAP, &info, current); |
| 137 | } |
Kumar Gala | cffb09c | 2005-10-26 09:55:41 -0500 | [diff] [blame] | 138 | #endif /* !(CONFIG_4xx || CONFIG_BOOKE)*/ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * For 600- and 800-family processors, the error_code parameter is DSISR |
| 142 | * for a data fault, SRR1 for an instruction fault. For 400-family processors |
| 143 | * the error_code parameter is ESR for a data fault, 0 for an instruction |
| 144 | * fault. |
| 145 | * For 64-bit processors, the error_code parameter is |
| 146 | * - DSISR for a non-SLB data access fault, |
| 147 | * - SRR1 & 0x08000000 for a non-SLB instruction access fault |
| 148 | * - 0 any SLB fault. |
| 149 | * |
| 150 | * The return value is 0 if the fault was handled, or the signal |
| 151 | * number if this is a kernel fault that can't be handled here. |
| 152 | */ |
| 153 | int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address, |
| 154 | unsigned long error_code) |
| 155 | { |
| 156 | struct vm_area_struct * vma; |
| 157 | struct mm_struct *mm = current->mm; |
| 158 | siginfo_t info; |
| 159 | int code = SEGV_MAPERR; |
| 160 | int is_write = 0; |
| 161 | int trap = TRAP(regs); |
| 162 | int is_exec = trap == 0x400; |
| 163 | |
| 164 | #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE)) |
| 165 | /* |
| 166 | * Fortunately the bit assignments in SRR1 for an instruction |
| 167 | * fault and DSISR for a data fault are mostly the same for the |
| 168 | * bits we are interested in. But there are some bits which |
| 169 | * indicate errors in DSISR but can validly be set in SRR1. |
| 170 | */ |
| 171 | if (trap == 0x400) |
| 172 | error_code &= 0x48200000; |
| 173 | else |
| 174 | is_write = error_code & DSISR_ISSTORE; |
| 175 | #else |
| 176 | is_write = error_code & ESR_DST; |
| 177 | #endif /* CONFIG_4xx || CONFIG_BOOKE */ |
| 178 | |
Anil S Keshavamurthy | 4f9e87c | 2006-06-26 00:25:27 -0700 | [diff] [blame] | 179 | if (notify_page_fault(DIE_PAGE_FAULT, "page_fault", regs, error_code, |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 180 | 11, SIGSEGV) == NOTIFY_STOP) |
| 181 | return 0; |
| 182 | |
| 183 | if (trap == 0x300) { |
| 184 | if (debugger_fault_handler(regs)) |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* On a kernel SLB miss we can only check for a valid exception entry */ |
| 189 | if (!user_mode(regs) && (address >= TASK_SIZE)) |
| 190 | return SIGSEGV; |
| 191 | |
| 192 | #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE)) |
| 193 | if (error_code & DSISR_DABRMATCH) { |
| 194 | /* DABR match */ |
Anton Blanchard | bce6c5f | 2006-01-09 15:47:04 +1100 | [diff] [blame] | 195 | do_dabr(regs, address, error_code); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | #endif /* !(CONFIG_4xx || CONFIG_BOOKE)*/ |
| 199 | |
| 200 | if (in_atomic() || mm == NULL) { |
| 201 | if (!user_mode(regs)) |
| 202 | return SIGSEGV; |
| 203 | /* in_atomic() in user mode is really bad, |
| 204 | as is current->mm == NULL. */ |
| 205 | printk(KERN_EMERG "Page fault in user mode with" |
| 206 | "in_atomic() = %d mm = %p\n", in_atomic(), mm); |
| 207 | printk(KERN_EMERG "NIP = %lx MSR = %lx\n", |
| 208 | regs->nip, regs->msr); |
| 209 | die("Weird page fault", regs, SIGSEGV); |
| 210 | } |
| 211 | |
| 212 | /* When running in the kernel we expect faults to occur only to |
| 213 | * addresses in user space. All other faults represent errors in the |
Anton Blanchard | fc5266e | 2006-04-01 11:33:12 +1100 | [diff] [blame] | 214 | * kernel and should generate an OOPS. Unfortunately, in the case of an |
| 215 | * erroneous fault occurring in a code path which already holds mmap_sem |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 216 | * we will deadlock attempting to validate the fault against the |
| 217 | * address space. Luckily the kernel only validly references user |
| 218 | * space from well defined areas of code, which are listed in the |
| 219 | * exceptions table. |
| 220 | * |
| 221 | * As the vast majority of faults will be valid we will only perform |
Anton Blanchard | fc5266e | 2006-04-01 11:33:12 +1100 | [diff] [blame] | 222 | * the source reference check when there is a possibility of a deadlock. |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 223 | * Attempt to lock the address space, if we cannot we then validate the |
| 224 | * source. If this is invalid we can skip the address space check, |
| 225 | * thus avoiding the deadlock. |
| 226 | */ |
| 227 | if (!down_read_trylock(&mm->mmap_sem)) { |
| 228 | if (!user_mode(regs) && !search_exception_tables(regs->nip)) |
| 229 | goto bad_area_nosemaphore; |
| 230 | |
| 231 | down_read(&mm->mmap_sem); |
| 232 | } |
| 233 | |
| 234 | vma = find_vma(mm, address); |
| 235 | if (!vma) |
| 236 | goto bad_area; |
| 237 | if (vma->vm_start <= address) |
| 238 | goto good_area; |
| 239 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 240 | goto bad_area; |
| 241 | |
| 242 | /* |
| 243 | * N.B. The POWER/Open ABI allows programs to access up to |
| 244 | * 288 bytes below the stack pointer. |
| 245 | * The kernel signal delivery code writes up to about 1.5kB |
| 246 | * below the stack pointer (r1) before decrementing it. |
| 247 | * The exec code can write slightly over 640kB to the stack |
| 248 | * before setting the user r1. Thus we allow the stack to |
| 249 | * expand to 1MB without further checks. |
| 250 | */ |
| 251 | if (address + 0x100000 < vma->vm_end) { |
| 252 | /* get user regs even if this fault is in kernel mode */ |
| 253 | struct pt_regs *uregs = current->thread.regs; |
| 254 | if (uregs == NULL) |
| 255 | goto bad_area; |
| 256 | |
| 257 | /* |
| 258 | * A user-mode access to an address a long way below |
| 259 | * the stack pointer is only valid if the instruction |
| 260 | * is one which would update the stack pointer to the |
| 261 | * address accessed if the instruction completed, |
| 262 | * i.e. either stwu rs,n(r1) or stwux rs,r1,rb |
| 263 | * (or the byte, halfword, float or double forms). |
| 264 | * |
| 265 | * If we don't check this then any write to the area |
| 266 | * between the last mapped region and the stack will |
| 267 | * expand the stack rather than segfaulting. |
| 268 | */ |
| 269 | if (address + 2048 < uregs->gpr[1] |
| 270 | && (!user_mode(regs) || !store_updates_sp(regs))) |
| 271 | goto bad_area; |
| 272 | } |
| 273 | if (expand_stack(vma, address)) |
| 274 | goto bad_area; |
| 275 | |
| 276 | good_area: |
| 277 | code = SEGV_ACCERR; |
| 278 | #if defined(CONFIG_6xx) |
| 279 | if (error_code & 0x95700000) |
| 280 | /* an error such as lwarx to I/O controller space, |
| 281 | address matching DABR, eciwx, etc. */ |
| 282 | goto bad_area; |
| 283 | #endif /* CONFIG_6xx */ |
| 284 | #if defined(CONFIG_8xx) |
| 285 | /* The MPC8xx seems to always set 0x80000000, which is |
| 286 | * "undefined". Of those that can be set, this is the only |
| 287 | * one which seems bad. |
| 288 | */ |
| 289 | if (error_code & 0x10000000) |
| 290 | /* Guarded storage error. */ |
| 291 | goto bad_area; |
| 292 | #endif /* CONFIG_8xx */ |
| 293 | |
| 294 | if (is_exec) { |
| 295 | #ifdef CONFIG_PPC64 |
| 296 | /* protection fault */ |
| 297 | if (error_code & DSISR_PROTFAULT) |
| 298 | goto bad_area; |
| 299 | if (!(vma->vm_flags & VM_EXEC)) |
| 300 | goto bad_area; |
| 301 | #endif |
| 302 | #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE) |
| 303 | pte_t *ptep; |
Eugene Surovegin | bab70a4 | 2006-03-28 10:13:12 -0800 | [diff] [blame] | 304 | pmd_t *pmdp; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 305 | |
| 306 | /* Since 4xx/Book-E supports per-page execute permission, |
| 307 | * we lazily flush dcache to icache. */ |
| 308 | ptep = NULL; |
Eugene Surovegin | bab70a4 | 2006-03-28 10:13:12 -0800 | [diff] [blame] | 309 | if (get_pteptr(mm, address, &ptep, &pmdp)) { |
| 310 | spinlock_t *ptl = pte_lockptr(mm, pmdp); |
| 311 | spin_lock(ptl); |
| 312 | if (pte_present(*ptep)) { |
| 313 | struct page *page = pte_page(*ptep); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 314 | |
Eugene Surovegin | bab70a4 | 2006-03-28 10:13:12 -0800 | [diff] [blame] | 315 | if (!test_bit(PG_arch_1, &page->flags)) { |
| 316 | flush_dcache_icache_page(page); |
| 317 | set_bit(PG_arch_1, &page->flags); |
| 318 | } |
| 319 | pte_update(ptep, 0, _PAGE_HWEXEC); |
| 320 | _tlbie(address); |
| 321 | pte_unmap_unlock(ptep, ptl); |
| 322 | up_read(&mm->mmap_sem); |
| 323 | return 0; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 324 | } |
Eugene Surovegin | bab70a4 | 2006-03-28 10:13:12 -0800 | [diff] [blame] | 325 | pte_unmap_unlock(ptep, ptl); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 326 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 327 | #endif |
| 328 | /* a write */ |
| 329 | } else if (is_write) { |
| 330 | if (!(vma->vm_flags & VM_WRITE)) |
| 331 | goto bad_area; |
| 332 | /* a read */ |
| 333 | } else { |
| 334 | /* protection fault */ |
| 335 | if (error_code & 0x08000000) |
| 336 | goto bad_area; |
| 337 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) |
| 338 | goto bad_area; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * If for any reason at all we couldn't handle the fault, |
| 343 | * make sure we exit gracefully rather than endlessly redo |
| 344 | * the fault. |
| 345 | */ |
| 346 | survive: |
| 347 | switch (handle_mm_fault(mm, vma, address, is_write)) { |
| 348 | |
| 349 | case VM_FAULT_MINOR: |
| 350 | current->min_flt++; |
| 351 | break; |
| 352 | case VM_FAULT_MAJOR: |
| 353 | current->maj_flt++; |
| 354 | break; |
| 355 | case VM_FAULT_SIGBUS: |
| 356 | goto do_sigbus; |
| 357 | case VM_FAULT_OOM: |
| 358 | goto out_of_memory; |
| 359 | default: |
| 360 | BUG(); |
| 361 | } |
| 362 | |
| 363 | up_read(&mm->mmap_sem); |
| 364 | return 0; |
| 365 | |
| 366 | bad_area: |
| 367 | up_read(&mm->mmap_sem); |
| 368 | |
| 369 | bad_area_nosemaphore: |
| 370 | /* User mode accesses cause a SIGSEGV */ |
| 371 | if (user_mode(regs)) { |
| 372 | _exception(SIGSEGV, regs, code, address); |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | if (is_exec && (error_code & DSISR_PROTFAULT) |
| 377 | && printk_ratelimit()) |
| 378 | printk(KERN_CRIT "kernel tried to execute NX-protected" |
| 379 | " page (%lx) - exploit attempt? (uid: %d)\n", |
| 380 | address, current->uid); |
| 381 | |
| 382 | return SIGSEGV; |
| 383 | |
| 384 | /* |
| 385 | * We ran out of memory, or some other thing happened to us that made |
| 386 | * us unable to handle the page fault gracefully. |
| 387 | */ |
| 388 | out_of_memory: |
| 389 | up_read(&mm->mmap_sem); |
| 390 | if (current->pid == 1) { |
| 391 | yield(); |
| 392 | down_read(&mm->mmap_sem); |
| 393 | goto survive; |
| 394 | } |
| 395 | printk("VM: killing process %s\n", current->comm); |
| 396 | if (user_mode(regs)) |
| 397 | do_exit(SIGKILL); |
| 398 | return SIGKILL; |
| 399 | |
| 400 | do_sigbus: |
| 401 | up_read(&mm->mmap_sem); |
| 402 | if (user_mode(regs)) { |
| 403 | info.si_signo = SIGBUS; |
| 404 | info.si_errno = 0; |
| 405 | info.si_code = BUS_ADRERR; |
| 406 | info.si_addr = (void __user *)address; |
| 407 | force_sig_info(SIGBUS, &info, current); |
| 408 | return 0; |
| 409 | } |
| 410 | return SIGBUS; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * bad_page_fault is called when we have a bad access from the kernel. |
| 415 | * It is called from the DSI and ISI handlers in head.S and from some |
| 416 | * of the procedures in traps.c. |
| 417 | */ |
| 418 | void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig) |
| 419 | { |
| 420 | const struct exception_table_entry *entry; |
| 421 | |
| 422 | /* Are we prepared to handle this fault? */ |
| 423 | if ((entry = search_exception_tables(regs->nip)) != NULL) { |
| 424 | regs->nip = entry->fixup; |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | /* kernel has accessed a bad area */ |
Olof Johansson | 723925b | 2005-11-06 14:54:36 -0800 | [diff] [blame] | 429 | |
| 430 | printk(KERN_ALERT "Unable to handle kernel paging request for "); |
| 431 | switch (regs->trap) { |
| 432 | case 0x300: |
| 433 | case 0x380: |
| 434 | printk("data at address 0x%08lx\n", regs->dar); |
| 435 | break; |
| 436 | case 0x400: |
| 437 | case 0x480: |
| 438 | printk("instruction fetch\n"); |
| 439 | break; |
| 440 | default: |
| 441 | printk("unknown fault\n"); |
| 442 | } |
| 443 | printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n", |
| 444 | regs->nip); |
| 445 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 446 | die("Kernel access of bad area", regs, sig); |
| 447 | } |