2 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
3 * Lennox Wu <lennox.wu@sunplusct.com>
4 * Chen Liqin <liqin.chen@sunplusct.com>
5 * Copyright (C) 2012 Regents of the University of California
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see the file COPYING, or write
19 * to the Free Software Foundation, Inc.,
24 #include <linux/kernel.h>
25 #include <linux/interrupt.h>
26 #include <linux/perf_event.h>
27 #include <linux/signal.h>
28 #include <linux/uaccess.h>
30 #include <asm/pgalloc.h>
31 #include <asm/ptrace.h>
34 * This routine handles page faults. It determines the address and the
35 * problem, and then passes it off to one of the appropriate routines.
37 asmlinkage void do_page_fault(struct pt_regs *regs)
39 struct task_struct *tsk;
40 struct vm_area_struct *vma;
42 unsigned long addr, cause;
43 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
44 int code = SEGV_MAPERR;
48 addr = regs->sbadaddr;
54 * Fault-in kernel-space virtual memory on-demand.
55 * The 'reference' page table is init_mm.pgd.
57 * NOTE! We MUST NOT take any locks for this case. We may
58 * be in an interrupt or a critical region, and should
59 * only copy the information from the master page table,
62 if (unlikely((addr >= VMALLOC_START) && (addr <= VMALLOC_END)))
65 /* Enable interrupts if they were enabled in the parent context. */
66 if (likely(regs->sstatus & SR_SPIE))
70 * If we're in an interrupt, have no user context, or are running
71 * in an atomic region, then we must not take the fault.
73 if (unlikely(faulthandler_disabled() || !mm))
77 flags |= FAULT_FLAG_USER;
79 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
82 down_read(&mm->mmap_sem);
83 vma = find_vma(mm, addr);
86 if (likely(vma->vm_start <= addr))
88 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
90 if (unlikely(expand_stack(vma, addr)))
94 * Ok, we have a good vm_area for this memory access, so
101 case EXC_INST_PAGE_FAULT:
102 if (!(vma->vm_flags & VM_EXEC))
105 case EXC_LOAD_PAGE_FAULT:
106 if (!(vma->vm_flags & VM_READ))
109 case EXC_STORE_PAGE_FAULT:
110 if (!(vma->vm_flags & VM_WRITE))
112 flags |= FAULT_FLAG_WRITE;
115 panic("%s: unhandled cause %lu", __func__, cause);
119 * If for any reason at all we could not handle the fault,
120 * make sure we exit gracefully rather than endlessly redo
123 fault = handle_mm_fault(vma, addr, flags);
126 * If we need to retry but a fatal signal is pending, handle the
127 * signal first. We do not need to release the mmap_sem because it
128 * would already be released in __lock_page_or_retry in mm/filemap.c.
130 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(tsk))
133 if (unlikely(fault & VM_FAULT_ERROR)) {
134 if (fault & VM_FAULT_OOM)
136 else if (fault & VM_FAULT_SIGBUS)
142 * Major/minor page fault accounting is only done on the
143 * initial attempt. If we go through a retry, it is extremely
144 * likely that the page will be found in page cache at that point.
146 if (flags & FAULT_FLAG_ALLOW_RETRY) {
147 if (fault & VM_FAULT_MAJOR) {
149 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
153 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
156 if (fault & VM_FAULT_RETRY) {
158 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
161 flags &= ~(FAULT_FLAG_ALLOW_RETRY);
162 flags |= FAULT_FLAG_TRIED;
165 * No need to up_read(&mm->mmap_sem) as we would
166 * have already released it in __lock_page_or_retry
173 up_read(&mm->mmap_sem);
177 * Something tried to access memory that isn't in our memory map.
178 * Fix it, but check if it's kernel or user first.
181 up_read(&mm->mmap_sem);
182 /* User mode accesses just cause a SIGSEGV */
183 if (user_mode(regs)) {
184 do_trap(regs, SIGSEGV, code, addr, tsk);
189 /* Are we prepared to handle this kernel fault? */
190 if (fixup_exception(regs))
194 * Oops. The kernel tried to access some bad page. We'll have to
195 * terminate things with extreme prejudice.
198 pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n",
199 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
200 "paging request", addr);
205 * We ran out of memory, call the OOM killer, and return the userspace
206 * (which will retry the fault, or kill us if we got oom-killed).
209 up_read(&mm->mmap_sem);
210 if (!user_mode(regs))
212 pagefault_out_of_memory();
216 up_read(&mm->mmap_sem);
217 /* Kernel mode? Handle exceptions or die */
218 if (!user_mode(regs))
220 do_trap(regs, SIGBUS, BUS_ADRERR, addr, tsk);
232 /* User mode accesses just cause a SIGSEGV */
234 return do_trap(regs, SIGSEGV, code, addr, tsk);
237 * Synchronize this task's top level page-table
238 * with the 'reference' page table.
240 * Do _not_ use "tsk->active_mm->pgd" here.
241 * We might be inside an interrupt in the middle
244 index = pgd_index(addr);
245 pgd = (pgd_t *)pfn_to_virt(csr_read(CSR_SATP)) + index;
246 pgd_k = init_mm.pgd + index;
248 if (!pgd_present(*pgd_k))
250 set_pgd(pgd, *pgd_k);
252 p4d = p4d_offset(pgd, addr);
253 p4d_k = p4d_offset(pgd_k, addr);
254 if (!p4d_present(*p4d_k))
257 pud = pud_offset(p4d, addr);
258 pud_k = pud_offset(p4d_k, addr);
259 if (!pud_present(*pud_k))
263 * Since the vmalloc area is global, it is unnecessary
264 * to copy individual PTEs
266 pmd = pmd_offset(pud, addr);
267 pmd_k = pmd_offset(pud_k, addr);
268 if (!pmd_present(*pmd_k))
270 set_pmd(pmd, *pmd_k);
273 * Make sure the actual PTE exists as well to
274 * catch kernel vmalloc-area accesses to non-mapped
275 * addresses. If we don't do this, this will just
276 * silently loop forever.
278 pte_k = pte_offset_kernel(pmd_k, addr);
279 if (!pte_present(*pte_k))