3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 * Derived from "arch/i386/mm/fault.c"
6 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Modified by Cort Dougan and Paul Mackerras.
10 * Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
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.
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>
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>
39 #include <asm/firmware.h>
41 #include <asm/pgtable.h>
43 #include <asm/mmu_context.h>
44 #include <asm/tlbflush.h>
45 #include <asm/siginfo.h>
46 #include <asm/debug.h>
50 static inline bool notify_page_fault(struct pt_regs *regs)
55 /* kprobe_running() needs smp_processor_id() */
56 if (!user_mode(regs)) {
58 if (kprobe_running() && kprobe_fault_handler(regs, 11))
62 #endif /* CONFIG_KPROBES */
64 if (unlikely(debugger_fault_handler(regs)))
71 * Check whether the instruction at regs->nip is a store using
72 * an update addressing form which will update r1.
74 static int store_updates_sp(struct pt_regs *regs)
78 if (get_user(inst, (unsigned int __user *)regs->nip))
80 /* check for 1 in the rA field */
81 if (((inst >> 16) & 0x1f) != 1)
83 /* check major opcode */
91 case 62: /* std or stdu */
92 return (inst & 3) == 1;
94 /* check minor opcode */
95 switch ((inst >> 1) & 0x3ff) {
100 case 695: /* stfsux */
101 case 759: /* stfdux */
108 * do_page_fault error handling helpers
112 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long address, int si_code)
115 * If we are in kernel mode, bail out with a SEGV, this will
116 * be caught by the assembly which will restore the non-volatile
117 * registers before calling bad_page_fault()
119 if (!user_mode(regs))
122 _exception(SIGSEGV, regs, si_code, address);
127 static noinline int bad_area_nosemaphore(struct pt_regs *regs, unsigned long address)
129 return __bad_area_nosemaphore(regs, address, SEGV_MAPERR);
132 static int __bad_area(struct pt_regs *regs, unsigned long address, int si_code)
134 struct mm_struct *mm = current->mm;
137 * Something tried to access memory that isn't in our memory map..
138 * Fix it, but check if it's kernel or user first..
140 up_read(&mm->mmap_sem);
142 return __bad_area_nosemaphore(regs, address, si_code);
145 static noinline int bad_area(struct pt_regs *regs, unsigned long address)
147 return __bad_area(regs, address, SEGV_MAPERR);
150 static int do_sigbus(struct pt_regs *regs, unsigned long address,
154 unsigned int lsb = 0;
156 if (!user_mode(regs))
159 current->thread.trap_nr = BUS_ADRERR;
160 info.si_signo = SIGBUS;
162 info.si_code = BUS_ADRERR;
163 info.si_addr = (void __user *)address;
164 #ifdef CONFIG_MEMORY_FAILURE
165 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
166 pr_err("MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
167 current->comm, current->pid, address);
168 info.si_code = BUS_MCEERR_AR;
171 if (fault & VM_FAULT_HWPOISON_LARGE)
172 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
173 if (fault & VM_FAULT_HWPOISON)
176 info.si_addr_lsb = lsb;
177 force_sig_info(SIGBUS, &info, current);
181 static int mm_fault_error(struct pt_regs *regs, unsigned long addr, int fault)
184 * Kernel page fault interrupted by SIGKILL. We have no reason to
185 * continue processing.
187 if (fatal_signal_pending(current) && !user_mode(regs))
191 if (fault & VM_FAULT_OOM) {
193 * We ran out of memory, or some other thing happened to us that
194 * made us unable to handle the page fault gracefully.
196 if (!user_mode(regs))
198 pagefault_out_of_memory();
200 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
201 VM_FAULT_HWPOISON_LARGE))
202 return do_sigbus(regs, addr, fault);
203 else if (fault & VM_FAULT_SIGSEGV)
204 return bad_area_nosemaphore(regs, addr);
211 /* Is this a bad kernel fault ? */
212 static bool bad_kernel_fault(bool is_exec, unsigned long error_code,
213 unsigned long address)
215 if (is_exec && (error_code & (DSISR_NOEXEC_OR_G | DSISR_KEYFAULT))) {
216 printk_ratelimited(KERN_CRIT "kernel tried to execute"
217 " exec-protected page (%lx) -"
218 "exploit attempt? (uid: %d)\n",
219 address, from_kuid(&init_user_ns,
222 return is_exec || (address >= TASK_SIZE);
225 #ifdef CONFIG_PPC_SMLPAR
226 static inline void cmo_account_page_fault(void)
228 if (firmware_has_feature(FW_FEATURE_CMO)) {
232 page_ins = be32_to_cpu(get_lppaca()->page_ins);
233 page_ins += 1 << PAGE_FACTOR;
234 get_lppaca()->page_ins = cpu_to_be32(page_ins);
239 static inline void cmo_account_page_fault(void) { }
240 #endif /* CONFIG_PPC_SMLPAR */
242 #ifdef CONFIG_PPC_STD_MMU
243 static void sanity_check_fault(bool is_write, unsigned long error_code)
246 * For hash translation mode, we should never get a
247 * PROTFAULT. Any update to pte to reduce access will result in us
248 * removing the hash page table entry, thus resulting in a DSISR_NOHPTE
249 * fault instead of DSISR_PROTFAULT.
251 * A pte update to relax the access will not result in a hash page table
252 * entry invalidate and hence can result in DSISR_PROTFAULT.
253 * ptep_set_access_flags() doesn't do a hpte flush. This is why we have
254 * the special !is_write in the below conditional.
256 * For platforms that doesn't supports coherent icache and do support
257 * per page noexec bit, we do setup things such that we do the
258 * sync between D/I cache via fault. But that is handled via low level
259 * hash fault code (hash_page_do_lazy_icache()) and we should not reach
262 * For wrong access that can result in PROTFAULT, the above vma->vm_flags
263 * check should handle those and hence we should fall to the bad_area
264 * handling correctly.
266 * For embedded with per page exec support that doesn't support coherent
267 * icache we do get PROTFAULT and we handle that D/I cache sync in
268 * set_pte_at while taking the noexec/prot fault. Hence this is WARN_ON
269 * is conditional for server MMU.
271 * For radix, we can get prot fault for autonuma case, because radix
272 * page table will have them marked noaccess for user.
274 if (!radix_enabled() && !is_write)
275 WARN_ON_ONCE(error_code & DSISR_PROTFAULT);
278 static void sanity_check_fault(bool is_write, unsigned long error_code) { }
279 #endif /* CONFIG_PPC_STD_MMU */
282 * Define the correct "is_write" bit in error_code based
283 * on the processor family
285 #if (defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
286 #define page_fault_is_write(__err) ((__err) & ESR_DST)
287 #define page_fault_is_bad(__err) (0)
289 #define page_fault_is_write(__err) ((__err) & DSISR_ISSTORE)
290 #if defined(CONFIG_8xx)
291 #define page_fault_is_bad(__err) ((__err) & 0x10000000)
292 #elif defined(CONFIG_PPC64)
293 #define page_fault_is_bad(__err) ((__err) & DSISR_BAD_FAULT_64S)
295 #define page_fault_is_bad(__err) ((__err) & DSISR_BAD_FAULT_32S)
300 * For 600- and 800-family processors, the error_code parameter is DSISR
301 * for a data fault, SRR1 for an instruction fault. For 400-family processors
302 * the error_code parameter is ESR for a data fault, 0 for an instruction
304 * For 64-bit processors, the error_code parameter is
305 * - DSISR for a non-SLB data access fault,
306 * - SRR1 & 0x08000000 for a non-SLB instruction access fault
309 * The return value is 0 if the fault was handled, or the signal
310 * number if this is a kernel fault that can't be handled here.
312 static int __do_page_fault(struct pt_regs *regs, unsigned long address,
313 unsigned long error_code)
315 struct vm_area_struct * vma;
316 struct mm_struct *mm = current->mm;
317 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
318 int is_exec = TRAP(regs) == 0x400;
319 int is_user = user_mode(regs);
320 int is_write = page_fault_is_write(error_code);
322 int store_update_sp = 0;
324 #ifdef CONFIG_PPC_ICSWX
326 * we need to do this early because this "data storage
327 * interrupt" does not update the DAR/DEAR so we don't want to
330 if (error_code & ICSWX_DSI_UCT) {
331 int rc = acop_handle_fault(regs, address, error_code);
335 #endif /* CONFIG_PPC_ICSWX */
337 if (notify_page_fault(regs))
340 if (unlikely(page_fault_is_bad(error_code))) {
342 _exception(SIGBUS, regs, BUS_OBJERR, address);
348 /* Additional sanity check(s) */
349 sanity_check_fault(is_write, error_code);
352 * The kernel should never take an execute fault nor should it
353 * take a page fault to a kernel address.
355 if (unlikely(!is_user && bad_kernel_fault(is_exec, error_code, address)))
359 * If we're in an interrupt, have no user context or are running
360 * in a region with pagefaults disabled then we must not take the fault
362 if (unlikely(faulthandler_disabled() || !mm)) {
364 printk_ratelimited(KERN_ERR "Page fault in user mode"
365 " with faulthandler_disabled()=%d"
367 faulthandler_disabled(), mm);
368 return bad_area_nosemaphore(regs, address);
371 /* We restore the interrupt state now */
372 if (!arch_irq_disabled_regs(regs))
375 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
378 * We want to do this outside mmap_sem, because reading code around nip
379 * can result in fault, which will cause a deadlock when called with
382 if (is_write && is_user)
383 store_update_sp = store_updates_sp(regs);
386 flags |= FAULT_FLAG_USER;
388 /* When running in the kernel we expect faults to occur only to
389 * addresses in user space. All other faults represent errors in the
390 * kernel and should generate an OOPS. Unfortunately, in the case of an
391 * erroneous fault occurring in a code path which already holds mmap_sem
392 * we will deadlock attempting to validate the fault against the
393 * address space. Luckily the kernel only validly references user
394 * space from well defined areas of code, which are listed in the
397 * As the vast majority of faults will be valid we will only perform
398 * the source reference check when there is a possibility of a deadlock.
399 * Attempt to lock the address space, if we cannot we then validate the
400 * source. If this is invalid we can skip the address space check,
401 * thus avoiding the deadlock.
403 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
404 if (!is_user && !search_exception_tables(regs->nip))
405 return bad_area_nosemaphore(regs, address);
408 down_read(&mm->mmap_sem);
411 * The above down_read_trylock() might have succeeded in
412 * which case we'll have missed the might_sleep() from
418 vma = find_vma(mm, address);
420 return bad_area(regs, address);
421 if (likely(vma->vm_start <= address))
423 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
424 return bad_area(regs, address);
427 * N.B. The POWER/Open ABI allows programs to access up to
428 * 288 bytes below the stack pointer.
429 * The kernel signal delivery code writes up to about 1.5kB
430 * below the stack pointer (r1) before decrementing it.
431 * The exec code can write slightly over 640kB to the stack
432 * before setting the user r1. Thus we allow the stack to
433 * expand to 1MB without further checks.
435 if (address + 0x100000 < vma->vm_end) {
436 /* get user regs even if this fault is in kernel mode */
437 struct pt_regs *uregs = current->thread.regs;
439 return bad_area(regs, address);
442 * A user-mode access to an address a long way below
443 * the stack pointer is only valid if the instruction
444 * is one which would update the stack pointer to the
445 * address accessed if the instruction completed,
446 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
447 * (or the byte, halfword, float or double forms).
449 * If we don't check this then any write to the area
450 * between the last mapped region and the stack will
451 * expand the stack rather than segfaulting.
453 if (address + 2048 < uregs->gpr[1] && !store_update_sp)
454 return bad_area(regs, address);
456 if (unlikely(expand_stack(vma, address)))
457 return bad_area(regs, address);
462 * Allow execution from readable areas if the MMU does not
463 * provide separate controls over reading and executing.
465 * Note: That code used to not be enabled for 4xx/BookE.
466 * It is now as I/D cache coherency for these is done at
467 * set_pte_at() time and I see no reason why the test
468 * below wouldn't be valid on those processors. This -may-
469 * break programs compiled with a really old ABI though.
471 if (unlikely(!(vma->vm_flags & VM_EXEC) &&
472 (cpu_has_feature(CPU_FTR_NOEXECUTE) ||
473 !(vma->vm_flags & (VM_READ | VM_WRITE)))))
474 return bad_area(regs, address);
476 } else if (is_write) {
477 if (unlikely(!(vma->vm_flags & VM_WRITE)))
478 return bad_area(regs, address);
479 flags |= FAULT_FLAG_WRITE;
482 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
483 return bad_area(regs, address);
487 * If for any reason at all we couldn't handle the fault,
488 * make sure we exit gracefully rather than endlessly redo
491 fault = handle_mm_fault(vma, address, flags);
494 * Handle the retry right now, the mmap_sem has been released in that
497 if (unlikely(fault & VM_FAULT_RETRY)) {
498 /* We retry only once */
499 if (flags & FAULT_FLAG_ALLOW_RETRY) {
501 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
504 flags &= ~FAULT_FLAG_ALLOW_RETRY;
505 flags |= FAULT_FLAG_TRIED;
506 if (!fatal_signal_pending(current))
511 * User mode? Just return to handle the fatal exception otherwise
512 * return to bad_page_fault
514 return is_user ? 0 : SIGBUS;
517 up_read(¤t->mm->mmap_sem);
519 if (unlikely(fault & VM_FAULT_ERROR))
520 return mm_fault_error(regs, address, fault);
523 * Major/minor page fault accounting.
525 if (fault & VM_FAULT_MAJOR) {
527 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, address);
528 cmo_account_page_fault();
531 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, address);
535 NOKPROBE_SYMBOL(__do_page_fault);
537 int do_page_fault(struct pt_regs *regs, unsigned long address,
538 unsigned long error_code)
540 enum ctx_state prev_state = exception_enter();
541 int rc = __do_page_fault(regs, address, error_code);
542 exception_exit(prev_state);
545 NOKPROBE_SYMBOL(do_page_fault);
548 * bad_page_fault is called when we have a bad access from the kernel.
549 * It is called from the DSI and ISI handlers in head.S and from some
550 * of the procedures in traps.c.
552 void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
554 const struct exception_table_entry *entry;
556 /* Are we prepared to handle this fault? */
557 if ((entry = search_exception_tables(regs->nip)) != NULL) {
558 regs->nip = extable_fixup(entry);
562 /* kernel has accessed a bad area */
564 switch (regs->trap) {
567 printk(KERN_ALERT "Unable to handle kernel paging request for "
568 "data at address 0x%08lx\n", regs->dar);
572 printk(KERN_ALERT "Unable to handle kernel paging request for "
573 "instruction fetch\n");
576 printk(KERN_ALERT "Unable to handle kernel paging request for "
577 "unaligned access at address 0x%08lx\n", regs->dar);
580 printk(KERN_ALERT "Unable to handle kernel paging request for "
584 printk(KERN_ALERT "Faulting instruction address: 0x%08lx\n",
587 if (task_stack_end_corrupted(current))
588 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
590 die("Kernel access of bad area", regs, sig);