2 * Copyright (C) 2009 Wind River Systems Inc
5 * based on arch/mips/mm/fault.c which is:
7 * Copyright (C) 1995-2000 Ralf Baechle
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/sched/debug.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/types.h>
22 #include <linux/ptrace.h>
23 #include <linux/mman.h>
25 #include <linux/extable.h>
26 #include <linux/uaccess.h>
27 #include <linux/ptrace.h>
29 #include <asm/mmu_context.h>
30 #include <asm/traps.h>
32 #define EXC_SUPERV_INSN_ACCESS 9 /* Supervisor only instruction address */
33 #define EXC_SUPERV_DATA_ACCESS 11 /* Supervisor only data address */
34 #define EXC_X_PROTECTION_FAULT 13 /* TLB permission violation (x) */
35 #define EXC_R_PROTECTION_FAULT 14 /* TLB permission violation (r) */
36 #define EXC_W_PROTECTION_FAULT 15 /* TLB permission violation (w) */
39 * This routine handles page faults. It determines the address,
40 * and the problem, and then passes it off to one of the appropriate
43 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,
44 unsigned long address)
46 struct vm_area_struct *vma = NULL;
47 struct task_struct *tsk = current;
48 struct mm_struct *mm = tsk->mm;
49 int code = SEGV_MAPERR;
51 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
55 /* Restart the instruction */
59 * We fault-in kernel-space virtual memory on-demand. The
60 * 'reference' page table is init_mm.pgd.
62 * NOTE! We MUST NOT take any locks for this case. We may
63 * be in an interrupt or a critical region, and should
64 * only copy the information from the master page table,
67 if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END)) {
69 goto bad_area_nosemaphore;
74 if (unlikely(address >= TASK_SIZE))
75 goto bad_area_nosemaphore;
78 * If we're in an interrupt or have no user
79 * context, we must not take the fault..
81 if (faulthandler_disabled() || !mm)
82 goto bad_area_nosemaphore;
85 flags |= FAULT_FLAG_USER;
87 if (!down_read_trylock(&mm->mmap_sem)) {
88 if (!user_mode(regs) && !search_exception_tables(regs->ea))
89 goto bad_area_nosemaphore;
91 down_read(&mm->mmap_sem);
94 vma = find_vma(mm, address);
97 if (vma->vm_start <= address)
99 if (!(vma->vm_flags & VM_GROWSDOWN))
101 if (expand_stack(vma, address))
104 * Ok, we have a good vm_area for this memory access, so
111 case EXC_SUPERV_INSN_ACCESS:
113 case EXC_SUPERV_DATA_ACCESS:
115 case EXC_X_PROTECTION_FAULT:
116 if (!(vma->vm_flags & VM_EXEC))
119 case EXC_R_PROTECTION_FAULT:
120 if (!(vma->vm_flags & VM_READ))
123 case EXC_W_PROTECTION_FAULT:
124 if (!(vma->vm_flags & VM_WRITE))
126 flags = FAULT_FLAG_WRITE;
131 * If for any reason at all we couldn't handle the fault,
132 * make sure we exit gracefully rather than endlessly redo
135 fault = handle_mm_fault(vma, address, flags);
137 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
140 if (unlikely(fault & VM_FAULT_ERROR)) {
141 if (fault & VM_FAULT_OOM)
143 else if (fault & VM_FAULT_SIGSEGV)
145 else if (fault & VM_FAULT_SIGBUS)
151 * Major/minor page fault accounting is only done on the
152 * initial attempt. If we go through a retry, it is extremely
153 * likely that the page will be found in page cache at that point.
155 if (flags & FAULT_FLAG_ALLOW_RETRY) {
156 if (fault & VM_FAULT_MAJOR)
160 if (fault & VM_FAULT_RETRY) {
161 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
163 flags &= ~FAULT_FLAG_ALLOW_RETRY;
164 flags |= FAULT_FLAG_TRIED;
167 * No need to up_read(&mm->mmap_sem) as we would
168 * have already released it in __lock_page_or_retry
176 up_read(&mm->mmap_sem);
180 * Something tried to access memory that isn't in our memory map..
181 * Fix it, but check if it's kernel or user first..
184 up_read(&mm->mmap_sem);
186 bad_area_nosemaphore:
187 /* User mode accesses just cause a SIGSEGV */
188 if (user_mode(regs)) {
189 if (unhandled_signal(current, SIGSEGV) && printk_ratelimit()) {
190 pr_info("%s: unhandled page fault (%d) at 0x%08lx, "
191 "cause %ld\n", current->comm, SIGSEGV, address, cause);
194 _exception(SIGSEGV, regs, code, address);
199 /* Are we prepared to handle this kernel fault? */
200 if (fixup_exception(regs))
204 * Oops. The kernel tried to access some bad page. We'll have to
205 * terminate things with extreme prejudice.
209 pr_alert("Unable to handle kernel %s at virtual address %08lx",
210 address < PAGE_SIZE ? "NULL pointer dereference" :
211 "paging request", address);
212 pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra,
218 * We ran out of memory, or some other thing happened to us that made
219 * us unable to handle the page fault gracefully.
222 up_read(&mm->mmap_sem);
223 if (!user_mode(regs))
225 pagefault_out_of_memory();
229 up_read(&mm->mmap_sem);
231 /* Kernel mode? Handle exceptions or die */
232 if (!user_mode(regs))
235 _exception(SIGBUS, regs, BUS_ADRERR, address);
241 * Synchronize this task's top level page-table
242 * with the 'reference' page table.
244 * Do _not_ use "tsk" here. We might be inside
245 * an interrupt in the middle of a task switch..
247 int offset = pgd_index(address);
253 pgd = pgd_current + offset;
254 pgd_k = init_mm.pgd + offset;
256 if (!pgd_present(*pgd_k))
258 set_pgd(pgd, *pgd_k);
260 pud = pud_offset(pgd, address);
261 pud_k = pud_offset(pgd_k, address);
262 if (!pud_present(*pud_k))
264 pmd = pmd_offset(pud, address);
265 pmd_k = pmd_offset(pud_k, address);
266 if (!pmd_present(*pmd_k))
268 set_pmd(pmd, *pmd_k);
270 pte_k = pte_offset_kernel(pmd_k, address);
271 if (!pte_present(*pte_k))
274 flush_tlb_one(address);