1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
4 * Lennox Wu <lennox.wu@sunplusct.com>
5 * Chen Liqin <liqin.chen@sunplusct.com>
6 * Copyright (C) 2012 Regents of the University of California
11 #include <linux/kernel.h>
12 #include <linux/interrupt.h>
13 #include <linux/perf_event.h>
14 #include <linux/signal.h>
15 #include <linux/uaccess.h>
17 #include <asm/pgalloc.h>
18 #include <asm/ptrace.h>
21 * This routine handles page faults. It determines the address and the
22 * problem, and then passes it off to one of the appropriate routines.
24 asmlinkage void do_page_fault(struct pt_regs *regs)
26 struct task_struct *tsk;
27 struct vm_area_struct *vma;
29 unsigned long addr, cause;
30 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
31 int code = SEGV_MAPERR;
35 addr = regs->sbadaddr;
41 * Fault-in kernel-space virtual memory on-demand.
42 * The 'reference' page table is init_mm.pgd.
44 * NOTE! We MUST NOT take any locks for this case. We may
45 * be in an interrupt or a critical region, and should
46 * only copy the information from the master page table,
49 if (unlikely((addr >= VMALLOC_START) && (addr <= VMALLOC_END)))
52 /* Enable interrupts if they were enabled in the parent context. */
53 if (likely(regs->sstatus & SR_SPIE))
57 * If we're in an interrupt, have no user context, or are running
58 * in an atomic region, then we must not take the fault.
60 if (unlikely(faulthandler_disabled() || !mm))
64 flags |= FAULT_FLAG_USER;
66 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
69 down_read(&mm->mmap_sem);
70 vma = find_vma(mm, addr);
73 if (likely(vma->vm_start <= addr))
75 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
77 if (unlikely(expand_stack(vma, addr)))
81 * Ok, we have a good vm_area for this memory access, so
88 case EXC_INST_PAGE_FAULT:
89 if (!(vma->vm_flags & VM_EXEC))
92 case EXC_LOAD_PAGE_FAULT:
93 if (!(vma->vm_flags & VM_READ))
96 case EXC_STORE_PAGE_FAULT:
97 if (!(vma->vm_flags & VM_WRITE))
99 flags |= FAULT_FLAG_WRITE;
102 panic("%s: unhandled cause %lu", __func__, cause);
106 * If for any reason at all we could not handle the fault,
107 * make sure we exit gracefully rather than endlessly redo
110 fault = handle_mm_fault(vma, addr, flags);
113 * If we need to retry but a fatal signal is pending, handle the
114 * signal first. We do not need to release the mmap_sem because it
115 * would already be released in __lock_page_or_retry in mm/filemap.c.
117 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(tsk))
120 if (unlikely(fault & VM_FAULT_ERROR)) {
121 if (fault & VM_FAULT_OOM)
123 else if (fault & VM_FAULT_SIGBUS)
129 * Major/minor page fault accounting is only done on the
130 * initial attempt. If we go through a retry, it is extremely
131 * likely that the page will be found in page cache at that point.
133 if (flags & FAULT_FLAG_ALLOW_RETRY) {
134 if (fault & VM_FAULT_MAJOR) {
136 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
140 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
143 if (fault & VM_FAULT_RETRY) {
145 * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
148 flags &= ~(FAULT_FLAG_ALLOW_RETRY);
149 flags |= FAULT_FLAG_TRIED;
152 * No need to up_read(&mm->mmap_sem) as we would
153 * have already released it in __lock_page_or_retry
160 up_read(&mm->mmap_sem);
164 * Something tried to access memory that isn't in our memory map.
165 * Fix it, but check if it's kernel or user first.
168 up_read(&mm->mmap_sem);
169 /* User mode accesses just cause a SIGSEGV */
170 if (user_mode(regs)) {
171 do_trap(regs, SIGSEGV, code, addr, tsk);
176 /* Are we prepared to handle this kernel fault? */
177 if (fixup_exception(regs))
181 * Oops. The kernel tried to access some bad page. We'll have to
182 * terminate things with extreme prejudice.
185 pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n",
186 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
187 "paging request", addr);
192 * We ran out of memory, call the OOM killer, and return the userspace
193 * (which will retry the fault, or kill us if we got oom-killed).
196 up_read(&mm->mmap_sem);
197 if (!user_mode(regs))
199 pagefault_out_of_memory();
203 up_read(&mm->mmap_sem);
204 /* Kernel mode? Handle exceptions or die */
205 if (!user_mode(regs))
207 do_trap(regs, SIGBUS, BUS_ADRERR, addr, tsk);
219 /* User mode accesses just cause a SIGSEGV */
221 return do_trap(regs, SIGSEGV, code, addr, tsk);
224 * Synchronize this task's top level page-table
225 * with the 'reference' page table.
227 * Do _not_ use "tsk->active_mm->pgd" here.
228 * We might be inside an interrupt in the middle
231 index = pgd_index(addr);
232 pgd = (pgd_t *)pfn_to_virt(csr_read(CSR_SATP)) + index;
233 pgd_k = init_mm.pgd + index;
235 if (!pgd_present(*pgd_k))
237 set_pgd(pgd, *pgd_k);
239 p4d = p4d_offset(pgd, addr);
240 p4d_k = p4d_offset(pgd_k, addr);
241 if (!p4d_present(*p4d_k))
244 pud = pud_offset(p4d, addr);
245 pud_k = pud_offset(p4d_k, addr);
246 if (!pud_present(*pud_k))
250 * Since the vmalloc area is global, it is unnecessary
251 * to copy individual PTEs
253 pmd = pmd_offset(pud, addr);
254 pmd_k = pmd_offset(pud_k, addr);
255 if (!pmd_present(*pmd_k))
257 set_pmd(pmd, *pmd_k);
260 * Make sure the actual PTE exists as well to
261 * catch kernel vmalloc-area accesses to non-mapped
262 * addresses. If we don't do this, this will just
263 * silently loop forever.
265 pte_k = pte_offset_kernel(pmd_k, addr);
266 if (!pte_present(*pte_k))