1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
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>
16 #include <linux/kprobes.h>
17 #include <linux/kfence.h>
18 #include <linux/entry-common.h>
20 #include <asm/ptrace.h>
21 #include <asm/tlbflush.h>
23 #include "../kernel/head.h"
25 static void show_pte(unsigned long addr)
32 struct mm_struct *mm = current->mm;
37 pr_alert("Current %s pgtable: %luK pagesize, %d-bit VAs, pgdp=0x%016llx\n",
38 current->comm, PAGE_SIZE / SZ_1K, VA_BITS,
39 mm == &init_mm ? (u64)__pa_symbol(mm->pgd) : virt_to_phys(mm->pgd));
41 pgdp = pgd_offset(mm, addr);
43 pr_alert("[%016lx] pgd=%016lx", addr, pgd_val(pgd));
44 if (pgd_none(pgd) || pgd_bad(pgd) || pgd_leaf(pgd))
47 p4dp = p4d_offset(pgdp, addr);
49 pr_cont(", p4d=%016lx", p4d_val(p4d));
50 if (p4d_none(p4d) || p4d_bad(p4d) || p4d_leaf(p4d))
53 pudp = pud_offset(p4dp, addr);
55 pr_cont(", pud=%016lx", pud_val(pud));
56 if (pud_none(pud) || pud_bad(pud) || pud_leaf(pud))
59 pmdp = pmd_offset(pudp, addr);
61 pr_cont(", pmd=%016lx", pmd_val(pmd));
62 if (pmd_none(pmd) || pmd_bad(pmd) || pmd_leaf(pmd))
65 ptep = pte_offset_map(pmdp, addr);
70 pr_cont(", pte=%016lx", pte_val(pte));
76 static void die_kernel_fault(const char *msg, unsigned long addr,
81 pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n", msg,
87 make_task_dead(SIGKILL);
90 static inline void no_context(struct pt_regs *regs, unsigned long addr)
94 /* Are we prepared to handle this kernel fault? */
95 if (fixup_exception(regs))
99 * Oops. The kernel tried to access some bad page. We'll have to
100 * terminate things with extreme prejudice.
102 if (addr < PAGE_SIZE)
103 msg = "NULL pointer dereference";
105 if (kfence_handle_page_fault(addr, regs->cause == EXC_STORE_PAGE_FAULT, regs))
108 msg = "paging request";
111 die_kernel_fault(msg, addr, regs);
114 static inline void mm_fault_error(struct pt_regs *regs, unsigned long addr, vm_fault_t fault)
116 if (!user_mode(regs)) {
117 no_context(regs, addr);
121 if (fault & VM_FAULT_OOM) {
123 * We ran out of memory, call the OOM killer, and return the userspace
124 * (which will retry the fault, or kill us if we got oom-killed).
126 pagefault_out_of_memory();
128 } else if (fault & (VM_FAULT_SIGBUS | VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) {
129 /* Kernel mode? Handle exceptions or die */
130 do_trap(regs, SIGBUS, BUS_ADRERR, addr);
132 } else if (fault & VM_FAULT_SIGSEGV) {
133 do_trap(regs, SIGSEGV, SEGV_MAPERR, addr);
141 bad_area_nosemaphore(struct pt_regs *regs, int code, unsigned long addr)
144 * Something tried to access memory that isn't in our memory map.
145 * Fix it, but check if it's kernel or user first.
147 /* User mode accesses just cause a SIGSEGV */
148 if (user_mode(regs)) {
149 do_trap(regs, SIGSEGV, code, addr);
153 no_context(regs, addr);
157 bad_area(struct pt_regs *regs, struct mm_struct *mm, int code,
160 mmap_read_unlock(mm);
162 bad_area_nosemaphore(regs, code, addr);
165 static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long addr)
175 /* User mode accesses just cause a SIGSEGV */
177 return do_trap(regs, SIGSEGV, code, addr);
180 * Synchronize this task's top level page-table
181 * with the 'reference' page table.
183 * Do _not_ use "tsk->active_mm->pgd" here.
184 * We might be inside an interrupt in the middle
187 index = pgd_index(addr);
188 pfn = csr_read(CSR_SATP) & SATP_PPN;
189 pgd = (pgd_t *)pfn_to_virt(pfn) + index;
190 pgd_k = init_mm.pgd + index;
192 if (!pgd_present(pgdp_get(pgd_k))) {
193 no_context(regs, addr);
196 set_pgd(pgd, pgdp_get(pgd_k));
198 p4d_k = p4d_offset(pgd_k, addr);
199 if (!p4d_present(p4dp_get(p4d_k))) {
200 no_context(regs, addr);
204 pud_k = pud_offset(p4d_k, addr);
205 if (!pud_present(pudp_get(pud_k))) {
206 no_context(regs, addr);
209 if (pud_leaf(pudp_get(pud_k)))
213 * Since the vmalloc area is global, it is unnecessary
214 * to copy individual PTEs
216 pmd_k = pmd_offset(pud_k, addr);
217 if (!pmd_present(pmdp_get(pmd_k))) {
218 no_context(regs, addr);
221 if (pmd_leaf(pmdp_get(pmd_k)))
225 * Make sure the actual PTE exists as well to
226 * catch kernel vmalloc-area accesses to non-mapped
227 * addresses. If we don't do this, this will just
228 * silently loop forever.
230 pte_k = pte_offset_kernel(pmd_k, addr);
231 if (!pte_present(ptep_get(pte_k))) {
232 no_context(regs, addr);
237 * The kernel assumes that TLBs don't cache invalid
238 * entries, but in RISC-V, SFENCE.VMA specifies an
239 * ordering constraint, not a cache flush; it is
240 * necessary even after writing invalid entries.
243 local_flush_tlb_page(addr);
246 static inline bool access_error(unsigned long cause, struct vm_area_struct *vma)
249 case EXC_INST_PAGE_FAULT:
250 if (!(vma->vm_flags & VM_EXEC)) {
254 case EXC_LOAD_PAGE_FAULT:
255 /* Write implies read */
256 if (!(vma->vm_flags & (VM_READ | VM_WRITE))) {
260 case EXC_STORE_PAGE_FAULT:
261 if (!(vma->vm_flags & VM_WRITE)) {
266 panic("%s: unhandled cause %lu", __func__, cause);
272 * This routine handles page faults. It determines the address and the
273 * problem, and then passes it off to one of the appropriate routines.
275 void handle_page_fault(struct pt_regs *regs)
277 struct task_struct *tsk;
278 struct vm_area_struct *vma;
279 struct mm_struct *mm;
280 unsigned long addr, cause;
281 unsigned int flags = FAULT_FLAG_DEFAULT;
282 int code = SEGV_MAPERR;
286 addr = regs->badaddr;
291 if (kprobe_page_fault(regs, cause))
295 * Fault-in kernel-space virtual memory on-demand.
296 * The 'reference' page table is init_mm.pgd.
298 * NOTE! We MUST NOT take any locks for this case. We may
299 * be in an interrupt or a critical region, and should
300 * only copy the information from the master page table,
303 if ((!IS_ENABLED(CONFIG_MMU) || !IS_ENABLED(CONFIG_64BIT)) &&
304 unlikely(addr >= VMALLOC_START && addr < VMALLOC_END)) {
305 vmalloc_fault(regs, code, addr);
309 /* Enable interrupts if they were enabled in the parent context. */
310 if (!regs_irqs_disabled(regs))
314 * If we're in an interrupt, have no user context, or are running
315 * in an atomic region, then we must not take the fault.
317 if (unlikely(faulthandler_disabled() || !mm)) {
318 tsk->thread.bad_cause = cause;
319 no_context(regs, addr);
324 flags |= FAULT_FLAG_USER;
326 if (!user_mode(regs) && addr < TASK_SIZE && unlikely(!(regs->status & SR_SUM))) {
327 if (fixup_exception(regs))
330 die_kernel_fault("access to user memory without uaccess routines", addr, regs);
333 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
335 if (cause == EXC_STORE_PAGE_FAULT)
336 flags |= FAULT_FLAG_WRITE;
337 else if (cause == EXC_INST_PAGE_FAULT)
338 flags |= FAULT_FLAG_INSTRUCTION;
339 if (!(flags & FAULT_FLAG_USER))
342 vma = lock_vma_under_rcu(mm, addr);
346 if (unlikely(access_error(cause, vma))) {
348 count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
349 tsk->thread.bad_cause = cause;
350 bad_area_nosemaphore(regs, SEGV_ACCERR, addr);
354 fault = handle_mm_fault(vma, addr, flags | FAULT_FLAG_VMA_LOCK, regs);
355 if (!(fault & (VM_FAULT_RETRY | VM_FAULT_COMPLETED)))
358 if (!(fault & VM_FAULT_RETRY)) {
359 count_vm_vma_lock_event(VMA_LOCK_SUCCESS);
362 count_vm_vma_lock_event(VMA_LOCK_RETRY);
363 if (fault & VM_FAULT_MAJOR)
364 flags |= FAULT_FLAG_TRIED;
366 if (fault_signal_pending(fault, regs)) {
367 if (!user_mode(regs))
368 no_context(regs, addr);
374 vma = lock_mm_and_find_vma(mm, addr, regs);
375 if (unlikely(!vma)) {
376 tsk->thread.bad_cause = cause;
377 bad_area_nosemaphore(regs, code, addr);
382 * Ok, we have a good vm_area for this memory access, so
387 if (unlikely(access_error(cause, vma))) {
388 tsk->thread.bad_cause = cause;
389 bad_area(regs, mm, code, addr);
394 * If for any reason at all we could not handle the fault,
395 * make sure we exit gracefully rather than endlessly redo
398 fault = handle_mm_fault(vma, addr, flags, regs);
401 * If we need to retry but a fatal signal is pending, handle the
402 * signal first. We do not need to release the mmap_lock because it
403 * would already be released in __lock_page_or_retry in mm/filemap.c.
405 if (fault_signal_pending(fault, regs)) {
406 if (!user_mode(regs))
407 no_context(regs, addr);
411 /* The fault is fully completed (including releasing mmap lock) */
412 if (fault & VM_FAULT_COMPLETED)
415 if (unlikely(fault & VM_FAULT_RETRY)) {
416 flags |= FAULT_FLAG_TRIED;
419 * No need to mmap_read_unlock(mm) as we would
420 * have already released it in __lock_page_or_retry
426 mmap_read_unlock(mm);
429 if (unlikely(fault & VM_FAULT_ERROR)) {
430 tsk->thread.bad_cause = cause;
431 mm_fault_error(regs, addr, fault);