1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6 * Copyright (C) 1995 - 2000 by Ralf Baechle
8 #include <linux/context_tracking.h>
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/entry-common.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/ptrace.h>
18 #include <linux/ratelimit.h>
19 #include <linux/mman.h>
21 #include <linux/smp.h>
22 #include <linux/kdebug.h>
23 #include <linux/perf_event.h>
24 #include <linux/uaccess.h>
25 #include <linux/kfence.h>
27 #include <asm/branch.h>
28 #include <asm/exception.h>
29 #include <asm/mmu_context.h>
30 #include <asm/ptrace.h>
32 int show_unhandled_signals = 1;
34 static int __kprobes spurious_fault(unsigned long write, unsigned long address)
42 if (!(address & __UA_LIMIT))
45 pgd = pgd_offset_k(address);
46 if (!pgd_present(pgdp_get(pgd)))
49 p4d = p4d_offset(pgd, address);
50 if (!p4d_present(p4dp_get(p4d)))
53 pud = pud_offset(p4d, address);
54 if (!pud_present(pudp_get(pud)))
57 pmd = pmd_offset(pud, address);
58 if (!pmd_present(pmdp_get(pmd)))
62 return write ? pmd_write(pmdp_get(pmd)) : 1;
64 pte = pte_offset_kernel(pmd, address);
65 if (!pte_present(ptep_get(pte)))
68 return write ? pte_write(ptep_get(pte)) : 1;
72 static void __kprobes no_context(struct pt_regs *regs,
73 unsigned long write, unsigned long address)
75 const int field = sizeof(unsigned long) * 2;
77 if (spurious_fault(write, address))
80 /* Are we prepared to handle this kernel fault? */
81 if (fixup_exception(regs))
84 if (kfence_handle_page_fault(address, write, regs))
88 * Oops. The kernel tried to access some bad page. We'll have to
89 * terminate things with extreme prejudice.
93 pr_alert("CPU %d Unable to handle kernel paging request at "
94 "virtual address %0*lx, era == %0*lx, ra == %0*lx\n",
95 raw_smp_processor_id(), field, address, field, regs->csr_era,
96 field, regs->regs[1]);
100 static void __kprobes do_out_of_memory(struct pt_regs *regs,
101 unsigned long write, unsigned long address)
104 * We ran out of memory, call the OOM killer, and return the userspace
105 * (which will retry the fault, or kill us if we got oom-killed).
107 if (!user_mode(regs)) {
108 no_context(regs, write, address);
111 pagefault_out_of_memory();
114 static void __kprobes do_sigbus(struct pt_regs *regs,
115 unsigned long write, unsigned long address, int si_code)
117 /* Kernel mode? Handle exceptions or die */
118 if (!user_mode(regs)) {
119 no_context(regs, write, address);
124 * Send a sigbus, regardless of whether we were in kernel
127 current->thread.csr_badvaddr = address;
128 current->thread.trap_nr = read_csr_excode();
129 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
132 static void __kprobes do_sigsegv(struct pt_regs *regs,
133 unsigned long write, unsigned long address, int si_code)
135 const int field = sizeof(unsigned long) * 2;
136 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
138 /* Kernel mode? Handle exceptions or die */
139 if (!user_mode(regs)) {
140 no_context(regs, write, address);
144 /* User mode accesses just cause a SIGSEGV */
145 current->thread.csr_badvaddr = address;
147 current->thread.error_code = 1;
149 current->thread.error_code = 2;
150 current->thread.trap_nr = read_csr_excode();
152 if (show_unhandled_signals &&
153 unhandled_signal(current, SIGSEGV) && __ratelimit(&ratelimit_state)) {
154 pr_info("do_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx\n",
156 write ? "write access to" : "read access from",
158 pr_info("era = %0*lx in", field,
159 (unsigned long) regs->csr_era);
160 print_vma_addr(KERN_CONT " ", regs->csr_era);
162 pr_info("ra = %0*lx in", field,
163 (unsigned long) regs->regs[1]);
164 print_vma_addr(KERN_CONT " ", regs->regs[1]);
167 force_sig_fault(SIGSEGV, si_code, (void __user *)address);
171 * This routine handles page faults. It determines the address,
172 * and the problem, and then passes it off to one of the appropriate
175 static void __kprobes __do_page_fault(struct pt_regs *regs,
176 unsigned long write, unsigned long address)
178 int si_code = SEGV_MAPERR;
179 unsigned int flags = FAULT_FLAG_DEFAULT;
180 struct task_struct *tsk = current;
181 struct mm_struct *mm = tsk->mm;
182 struct vm_area_struct *vma = NULL;
185 if (kprobe_page_fault(regs, current->thread.trap_nr))
189 * We fault-in kernel-space virtual memory on-demand. The
190 * 'reference' page table is init_mm.pgd.
192 * NOTE! We MUST NOT take any locks for this case. We may
193 * be in an interrupt or a critical region, and should
194 * only copy the information from the master page table,
197 if (address & __UA_LIMIT) {
198 if (!user_mode(regs))
199 no_context(regs, write, address);
201 do_sigsegv(regs, write, address, si_code);
206 * If we're in an interrupt or have no user
207 * context, we must not take the fault..
209 if (faulthandler_disabled() || !mm) {
210 do_sigsegv(regs, write, address, si_code);
215 flags |= FAULT_FLAG_USER;
217 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
219 vma = lock_mm_and_find_vma(mm, address, regs);
221 goto bad_area_nosemaphore;
225 * Something tried to access memory that isn't in our memory map..
226 * Fix it, but check if it's kernel or user first..
229 mmap_read_unlock(mm);
230 bad_area_nosemaphore:
231 do_sigsegv(regs, write, address, si_code);
235 * Ok, we have a good vm_area for this memory access, so
239 si_code = SEGV_ACCERR;
242 flags |= FAULT_FLAG_WRITE;
243 if (!(vma->vm_flags & VM_WRITE))
246 if (!(vma->vm_flags & VM_EXEC) && address == exception_era(regs))
248 if (!(vma->vm_flags & (VM_READ | VM_WRITE)) && address != exception_era(regs))
253 * If for any reason at all we couldn't handle the fault,
254 * make sure we exit gracefully rather than endlessly redo
257 fault = handle_mm_fault(vma, address, flags, regs);
259 if (fault_signal_pending(fault, regs)) {
260 if (!user_mode(regs))
261 no_context(regs, write, address);
265 /* The fault is fully completed (including releasing mmap lock) */
266 if (fault & VM_FAULT_COMPLETED)
269 if (unlikely(fault & VM_FAULT_RETRY)) {
270 flags |= FAULT_FLAG_TRIED;
273 * No need to mmap_read_unlock(mm) as we would
274 * have already released it in __lock_page_or_retry
279 if (unlikely(fault & VM_FAULT_ERROR)) {
280 mmap_read_unlock(mm);
281 if (fault & VM_FAULT_OOM) {
282 do_out_of_memory(regs, write, address);
284 } else if (fault & VM_FAULT_SIGSEGV) {
285 do_sigsegv(regs, write, address, si_code);
287 } else if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
288 do_sigbus(regs, write, address, si_code);
294 mmap_read_unlock(mm);
297 asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
298 unsigned long write, unsigned long address)
300 irqentry_state_t state = irqentry_enter(regs);
302 /* Enable interrupt if enabled in parent context */
303 if (likely(regs->csr_prmd & CSR_PRMD_PIE))
306 __do_page_fault(regs, write, address);
310 irqentry_exit(regs, state);