1 /* 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/signal.h> 19 #include <linux/sched.h> 20 #include <linux/sched/task_stack.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/extable.h> 31 #include <linux/kprobes.h> 32 #include <linux/kdebug.h> 33 #include <linux/perf_event.h> 34 #include <linux/ratelimit.h> 35 #include <linux/context_tracking.h> 36 #include <linux/hugetlb.h> 37 #include <linux/uaccess.h> 38 39 #include <asm/firmware.h> 40 #include <asm/page.h> 41 #include <asm/pgtable.h> 42 #include <asm/mmu.h> 43 #include <asm/mmu_context.h> 44 #include <asm/tlbflush.h> 45 #include <asm/siginfo.h> 46 #include <asm/debug.h> 47 48 #include "icswx.h" 49 50 #ifdef CONFIG_KPROBES 51 static inline int notify_page_fault(struct pt_regs *regs) 52 { 53 int ret = 0; 54 55 /* kprobe_running() needs smp_processor_id() */ 56 if (!user_mode(regs)) { 57 preempt_disable(); 58 if (kprobe_running() && kprobe_fault_handler(regs, 11)) 59 ret = 1; 60 preempt_enable(); 61 } 62 63 return ret; 64 } 65 #else 66 static inline int notify_page_fault(struct pt_regs *regs) 67 { 68 return 0; 69 } 70 #endif 71 72 /* 73 * Check whether the instruction at regs->nip is a store using 74 * an update addressing form which will update r1. 75 */ 76 static int store_updates_sp(struct pt_regs *regs) 77 { 78 unsigned int inst; 79 80 if (get_user(inst, (unsigned int __user *)regs->nip)) 81 return 0; 82 /* check for 1 in the rA field */ 83 if (((inst >> 16) & 0x1f) != 1) 84 return 0; 85 /* check major opcode */ 86 switch (inst >> 26) { 87 case 37: /* stwu */ 88 case 39: /* stbu */ 89 case 45: /* sthu */ 90 case 53: /* stfsu */ 91 case 55: /* stfdu */ 92 return 1; 93 case 62: /* std or stdu */ 94 return (inst & 3) == 1; 95 case 31: 96 /* check minor opcode */ 97 switch ((inst >> 1) & 0x3ff) { 98 case 181: /* stdux */ 99 case 183: /* stwux */ 100 case 247: /* stbux */ 101 case 439: /* sthux */ 102 case 695: /* stfsux */ 103 case 759: /* stfdux */ 104 return 1; 105 } 106 } 107 return 0; 108 } 109 /* 110 * do_page_fault error handling helpers 111 */ 112 113 #define MM_FAULT_RETURN 0 114 #define MM_FAULT_CONTINUE -1 115 #define MM_FAULT_ERR(sig) (sig) 116 117 static int do_sigbus(struct pt_regs *regs, unsigned long address, 118 unsigned int fault) 119 { 120 siginfo_t info; 121 unsigned int lsb = 0; 122 123 if (!user_mode(regs)) 124 return MM_FAULT_ERR(SIGBUS); 125 126 current->thread.trap_nr = BUS_ADRERR; 127 info.si_signo = SIGBUS; 128 info.si_errno = 0; 129 info.si_code = BUS_ADRERR; 130 info.si_addr = (void __user *)address; 131 #ifdef CONFIG_MEMORY_FAILURE 132 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) { 133 pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n", 134 current->comm, current->pid, address); 135 info.si_code = BUS_MCEERR_AR; 136 } 137 138 if (fault & VM_FAULT_HWPOISON_LARGE) 139 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault)); 140 if (fault & VM_FAULT_HWPOISON) 141 lsb = PAGE_SHIFT; 142 #endif 143 info.si_addr_lsb = lsb; 144 force_sig_info(SIGBUS, &info, current); 145 return MM_FAULT_RETURN; 146 } 147 148 static int mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault) 149 { 150 /* 151 * Pagefault was interrupted by SIGKILL. We have no reason to 152 * continue the pagefault. 153 */ 154 if (fatal_signal_pending(current)) { 155 /* Coming from kernel, we need to deal with uaccess fixups */ 156 if (user_mode(regs)) 157 return MM_FAULT_RETURN; 158 return MM_FAULT_ERR(SIGKILL); 159 } 160 161 /* No fault: be happy */ 162 if (!(fault & VM_FAULT_ERROR)) 163 return MM_FAULT_CONTINUE; 164 165 /* Out of memory */ 166 if (fault & VM_FAULT_OOM) { 167 /* 168 * We ran out of memory, or some other thing happened to us that 169 * made us unable to handle the page fault gracefully. 170 */ 171 if (!user_mode(regs)) 172 return MM_FAULT_ERR(SIGKILL); 173 pagefault_out_of_memory(); 174 return MM_FAULT_RETURN; 175 } 176 177 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) 178 return do_sigbus(regs, addr, fault); 179 180 /* We don't understand the fault code, this is fatal */ 181 BUG(); 182 return MM_FAULT_CONTINUE; 183 } 184 185 /* 186 * For 600- and 800-family processors, the error_code parameter is DSISR 187 * for a data fault, SRR1 for an instruction fault. For 400-family processors 188 * the error_code parameter is ESR for a data fault, 0 for an instruction 189 * fault. 190 * For 64-bit processors, the error_code parameter is 191 * - DSISR for a non-SLB data access fault, 192 * - SRR1 & 0x08000000 for a non-SLB instruction access fault 193 * - 0 any SLB fault. 194 * 195 * The return value is 0 if the fault was handled, or the signal 196 * number if this is a kernel fault that can't be handled here. 197 */ 198 int do_page_fault(struct pt_regs *regs, unsigned long address, 199 unsigned long error_code) 200 { 201 enum ctx_state prev_state = exception_enter(); 202 struct vm_area_struct * vma; 203 struct mm_struct *mm = current->mm; 204 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; 205 int code = SEGV_MAPERR; 206 int is_write = 0; 207 int trap = TRAP(regs); 208 int is_exec = trap == 0x400; 209 int fault; 210 int rc = 0, store_update_sp = 0; 211 212 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE)) 213 /* 214 * Fortunately the bit assignments in SRR1 for an instruction 215 * fault and DSISR for a data fault are mostly the same for the 216 * bits we are interested in. But there are some bits which 217 * indicate errors in DSISR but can validly be set in SRR1. 218 */ 219 if (trap == 0x400) 220 error_code &= 0x48200000; 221 else 222 is_write = error_code & DSISR_ISSTORE; 223 #else 224 is_write = error_code & ESR_DST; 225 #endif /* CONFIG_4xx || CONFIG_BOOKE */ 226 227 #ifdef CONFIG_PPC_ICSWX 228 /* 229 * we need to do this early because this "data storage 230 * interrupt" does not update the DAR/DEAR so we don't want to 231 * look at it 232 */ 233 if (error_code & ICSWX_DSI_UCT) { 234 rc = acop_handle_fault(regs, address, error_code); 235 if (rc) 236 goto bail; 237 } 238 #endif /* CONFIG_PPC_ICSWX */ 239 240 if (notify_page_fault(regs)) 241 goto bail; 242 243 if (unlikely(debugger_fault_handler(regs))) 244 goto bail; 245 246 /* 247 * The kernel should never take an execute fault nor should it 248 * take a page fault to a kernel address. 249 */ 250 if (!user_mode(regs) && (is_exec || (address >= TASK_SIZE))) { 251 rc = SIGSEGV; 252 goto bail; 253 } 254 255 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE) || \ 256 defined(CONFIG_PPC_BOOK3S_64)) 257 if (error_code & DSISR_DABRMATCH) { 258 /* breakpoint match */ 259 do_break(regs, address, error_code); 260 goto bail; 261 } 262 #endif 263 264 /* We restore the interrupt state now */ 265 if (!arch_irq_disabled_regs(regs)) 266 local_irq_enable(); 267 268 if (faulthandler_disabled() || mm == NULL) { 269 if (!user_mode(regs)) { 270 rc = SIGSEGV; 271 goto bail; 272 } 273 /* faulthandler_disabled() in user mode is really bad, 274 as is current->mm == NULL. */ 275 printk(KERN_EMERG "Page fault in user mode with " 276 "faulthandler_disabled() = %d mm = %p\n", 277 faulthandler_disabled(), mm); 278 printk(KERN_EMERG "NIP = %lx MSR = %lx\n", 279 regs->nip, regs->msr); 280 die("Weird page fault", regs, SIGSEGV); 281 } 282 283 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); 284 285 /* 286 * We want to do this outside mmap_sem, because reading code around nip 287 * can result in fault, which will cause a deadlock when called with 288 * mmap_sem held 289 */ 290 if (!is_exec && user_mode(regs)) 291 store_update_sp = store_updates_sp(regs); 292 293 if (user_mode(regs)) 294 flags |= FAULT_FLAG_USER; 295 296 /* When running in the kernel we expect faults to occur only to 297 * addresses in user space. All other faults represent errors in the 298 * kernel and should generate an OOPS. Unfortunately, in the case of an 299 * erroneous fault occurring in a code path which already holds mmap_sem 300 * we will deadlock attempting to validate the fault against the 301 * address space. Luckily the kernel only validly references user 302 * space from well defined areas of code, which are listed in the 303 * exceptions table. 304 * 305 * As the vast majority of faults will be valid we will only perform 306 * the source reference check when there is a possibility of a deadlock. 307 * Attempt to lock the address space, if we cannot we then validate the 308 * source. If this is invalid we can skip the address space check, 309 * thus avoiding the deadlock. 310 */ 311 if (!down_read_trylock(&mm->mmap_sem)) { 312 if (!user_mode(regs) && !search_exception_tables(regs->nip)) 313 goto bad_area_nosemaphore; 314 315 retry: 316 down_read(&mm->mmap_sem); 317 } else { 318 /* 319 * The above down_read_trylock() might have succeeded in 320 * which case we'll have missed the might_sleep() from 321 * down_read(): 322 */ 323 might_sleep(); 324 } 325 326 vma = find_vma(mm, address); 327 if (!vma) 328 goto bad_area; 329 if (vma->vm_start <= address) 330 goto good_area; 331 if (!(vma->vm_flags & VM_GROWSDOWN)) 332 goto bad_area; 333 334 /* 335 * N.B. The POWER/Open ABI allows programs to access up to 336 * 288 bytes below the stack pointer. 337 * The kernel signal delivery code writes up to about 1.5kB 338 * below the stack pointer (r1) before decrementing it. 339 * The exec code can write slightly over 640kB to the stack 340 * before setting the user r1. Thus we allow the stack to 341 * expand to 1MB without further checks. 342 */ 343 if (address + 0x100000 < vma->vm_end) { 344 /* get user regs even if this fault is in kernel mode */ 345 struct pt_regs *uregs = current->thread.regs; 346 if (uregs == NULL) 347 goto bad_area; 348 349 /* 350 * A user-mode access to an address a long way below 351 * the stack pointer is only valid if the instruction 352 * is one which would update the stack pointer to the 353 * address accessed if the instruction completed, 354 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb 355 * (or the byte, halfword, float or double forms). 356 * 357 * If we don't check this then any write to the area 358 * between the last mapped region and the stack will 359 * expand the stack rather than segfaulting. 360 */ 361 if (address + 2048 < uregs->gpr[1] && !store_update_sp) 362 goto bad_area; 363 } 364 if (expand_stack(vma, address)) 365 goto bad_area; 366 367 good_area: 368 code = SEGV_ACCERR; 369 #if defined(CONFIG_6xx) 370 if (error_code & 0x95700000) 371 /* an error such as lwarx to I/O controller space, 372 address matching DABR, eciwx, etc. */ 373 goto bad_area; 374 #endif /* CONFIG_6xx */ 375 #if defined(CONFIG_8xx) 376 /* The MPC8xx seems to always set 0x80000000, which is 377 * "undefined". Of those that can be set, this is the only 378 * one which seems bad. 379 */ 380 if (error_code & 0x10000000) 381 /* Guarded storage error. */ 382 goto bad_area; 383 #endif /* CONFIG_8xx */ 384 385 if (is_exec) { 386 /* 387 * Allow execution from readable areas if the MMU does not 388 * provide separate controls over reading and executing. 389 * 390 * Note: That code used to not be enabled for 4xx/BookE. 391 * It is now as I/D cache coherency for these is done at 392 * set_pte_at() time and I see no reason why the test 393 * below wouldn't be valid on those processors. This -may- 394 * break programs compiled with a really old ABI though. 395 */ 396 if (!(vma->vm_flags & VM_EXEC) && 397 (cpu_has_feature(CPU_FTR_NOEXECUTE) || 398 !(vma->vm_flags & (VM_READ | VM_WRITE)))) 399 goto bad_area; 400 /* a write */ 401 } else if (is_write) { 402 if (!(vma->vm_flags & VM_WRITE)) 403 goto bad_area; 404 flags |= FAULT_FLAG_WRITE; 405 /* a read */ 406 } else { 407 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) 408 goto bad_area; 409 } 410 #ifdef CONFIG_PPC_STD_MMU 411 /* 412 * For hash translation mode, we should never get a 413 * PROTFAULT. Any update to pte to reduce access will result in us 414 * removing the hash page table entry, thus resulting in a DSISR_NOHPTE 415 * fault instead of DSISR_PROTFAULT. 416 * 417 * A pte update to relax the access will not result in a hash page table 418 * entry invalidate and hence can result in DSISR_PROTFAULT. 419 * ptep_set_access_flags() doesn't do a hpte flush. This is why we have 420 * the special !is_write in the below conditional. 421 * 422 * For platforms that doesn't supports coherent icache and do support 423 * per page noexec bit, we do setup things such that we do the 424 * sync between D/I cache via fault. But that is handled via low level 425 * hash fault code (hash_page_do_lazy_icache()) and we should not reach 426 * here in such case. 427 * 428 * For wrong access that can result in PROTFAULT, the above vma->vm_flags 429 * check should handle those and hence we should fall to the bad_area 430 * handling correctly. 431 * 432 * For embedded with per page exec support that doesn't support coherent 433 * icache we do get PROTFAULT and we handle that D/I cache sync in 434 * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON 435 * is conditional for server MMU. 436 * 437 * For radix, we can get prot fault for autonuma case, because radix 438 * page table will have them marked noaccess for user. 439 */ 440 if (!radix_enabled() && !is_write) 441 WARN_ON_ONCE(error_code & DSISR_PROTFAULT); 442 #endif /* CONFIG_PPC_STD_MMU */ 443 444 /* 445 * If for any reason at all we couldn't handle the fault, 446 * make sure we exit gracefully rather than endlessly redo 447 * the fault. 448 */ 449 fault = handle_mm_fault(vma, address, flags); 450 451 /* 452 * Handle the retry right now, the mmap_sem has been released in that 453 * case. 454 */ 455 if (unlikely(fault & VM_FAULT_RETRY)) { 456 /* We retry only once */ 457 if (flags & FAULT_FLAG_ALLOW_RETRY) { 458 /* 459 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk 460 * of starvation. 461 */ 462 flags &= ~FAULT_FLAG_ALLOW_RETRY; 463 flags |= FAULT_FLAG_TRIED; 464 if (!fatal_signal_pending(current)) 465 goto retry; 466 } 467 /* We will enter mm_fault_error() below */ 468 } else 469 up_read(¤t->mm->mmap_sem); 470 471 if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) { 472 if (fault & VM_FAULT_SIGSEGV) 473 goto bad_area_nosemaphore; 474 rc = mm_fault_error(regs, address, fault); 475 if (rc >= MM_FAULT_RETURN) 476 goto bail; 477 else 478 rc = 0; 479 } 480 481 /* 482 * Major/minor page fault accounting. 483 */ 484 if (fault & VM_FAULT_MAJOR) { 485 current->maj_flt++; 486 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 487 regs, address); 488 #ifdef CONFIG_PPC_SMLPAR 489 if (firmware_has_feature(FW_FEATURE_CMO)) { 490 u32 page_ins; 491 492 preempt_disable(); 493 page_ins = be32_to_cpu(get_lppaca()->page_ins); 494 page_ins += 1 << PAGE_FACTOR; 495 get_lppaca()->page_ins = cpu_to_be32(page_ins); 496 preempt_enable(); 497 } 498 #endif /* CONFIG_PPC_SMLPAR */ 499 } else { 500 current->min_flt++; 501 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 502 regs, address); 503 } 504 505 goto bail; 506 507 bad_area: 508 up_read(&mm->mmap_sem); 509 510 bad_area_nosemaphore: 511 /* User mode accesses cause a SIGSEGV */ 512 if (user_mode(regs)) { 513 _exception(SIGSEGV, regs, code, address); 514 goto bail; 515 } 516 517 if (is_exec && (error_code & DSISR_PROTFAULT)) 518 printk_ratelimited(KERN_CRIT "kernel tried to execute NX-protected" 519 " page (%lx) - exploit attempt? (uid: %d)\n", 520 address, from_kuid(&init_user_ns, current_uid())); 521 522 rc = SIGSEGV; 523 524 bail: 525 exception_exit(prev_state); 526 return rc; 527 } 528 NOKPROBE_SYMBOL(do_page_fault); 529 530 /* 531 * bad_page_fault is called when we have a bad access from the kernel. 532 * It is called from the DSI and ISI handlers in head.S and from some 533 * of the procedures in traps.c. 534 */ 535 void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig) 536 { 537 const struct exception_table_entry *entry; 538 539 /* Are we prepared to handle this fault? */ 540 if ((entry = search_exception_tables(regs->nip)) != NULL) { 541 regs->nip = extable_fixup(entry); 542 return; 543 } 544 545 /* kernel has accessed a bad area */ 546 547 switch (regs->trap) { 548 case 0x300: 549 case 0x380: 550 printk(KERN_ALERT "Unable to handle kernel paging request for " 551 "data at address 0x%08lx\n", regs->dar); 552 break; 553 case 0x400: 554 case 0x480: 555 printk(KERN_ALERT "Unable to handle kernel paging request for " 556 "instruction fetch\n"); 557 break; 558 case 0x600: 559 printk(KERN_ALERT "Unable to handle kernel paging request for " 560 "unaligned access at address 0x%08lx\n", regs->dar); 561 break; 562 default: 563 printk(KERN_ALERT "Unable to handle kernel paging request for " 564 "unknown fault\n"); 565 break; 566 } 567 printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n", 568 regs->nip); 569 570 if (task_stack_end_corrupted(current)) 571 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n"); 572 573 die("Kernel access of bad area", regs, sig); 574 } 575