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/kprobes.h>
24 #include <linux/perf_event.h>
25 #include <linux/uaccess.h>
27 #include <asm/branch.h>
28 #include <asm/mmu_context.h>
29 #include <asm/ptrace.h>
31 int show_unhandled_signals = 1;
33 static void __kprobes no_context(struct pt_regs *regs, unsigned long address)
35 const int field = sizeof(unsigned long) * 2;
37 /* Are we prepared to handle this kernel fault? */
38 if (fixup_exception(regs))
42 * Oops. The kernel tried to access some bad page. We'll have to
43 * terminate things with extreme prejudice.
47 pr_alert("CPU %d Unable to handle kernel paging request at "
48 "virtual address %0*lx, era == %0*lx, ra == %0*lx\n",
49 raw_smp_processor_id(), field, address, field, regs->csr_era,
50 field, regs->regs[1]);
54 static void __kprobes do_out_of_memory(struct pt_regs *regs, unsigned long address)
57 * We ran out of memory, call the OOM killer, and return the userspace
58 * (which will retry the fault, or kill us if we got oom-killed).
60 if (!user_mode(regs)) {
61 no_context(regs, address);
64 pagefault_out_of_memory();
67 static void __kprobes do_sigbus(struct pt_regs *regs,
68 unsigned long write, unsigned long address, int si_code)
70 /* Kernel mode? Handle exceptions or die */
71 if (!user_mode(regs)) {
72 no_context(regs, address);
77 * Send a sigbus, regardless of whether we were in kernel
80 current->thread.csr_badvaddr = address;
81 current->thread.trap_nr = read_csr_excode();
82 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);
85 static void __kprobes do_sigsegv(struct pt_regs *regs,
86 unsigned long write, unsigned long address, int si_code)
88 const int field = sizeof(unsigned long) * 2;
89 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
91 /* Kernel mode? Handle exceptions or die */
92 if (!user_mode(regs)) {
93 no_context(regs, address);
97 /* User mode accesses just cause a SIGSEGV */
98 current->thread.csr_badvaddr = address;
100 current->thread.error_code = 1;
102 current->thread.error_code = 2;
103 current->thread.trap_nr = read_csr_excode();
105 if (show_unhandled_signals &&
106 unhandled_signal(current, SIGSEGV) && __ratelimit(&ratelimit_state)) {
107 pr_info("do_page_fault(): sending SIGSEGV to %s for invalid %s %0*lx\n",
109 write ? "write access to" : "read access from",
111 pr_info("era = %0*lx in", field,
112 (unsigned long) regs->csr_era);
113 print_vma_addr(KERN_CONT " ", regs->csr_era);
115 pr_info("ra = %0*lx in", field,
116 (unsigned long) regs->regs[1]);
117 print_vma_addr(KERN_CONT " ", regs->regs[1]);
120 force_sig_fault(SIGSEGV, si_code, (void __user *)address);
124 * This routine handles page faults. It determines the address,
125 * and the problem, and then passes it off to one of the appropriate
128 static void __kprobes __do_page_fault(struct pt_regs *regs,
129 unsigned long write, unsigned long address)
131 int si_code = SEGV_MAPERR;
132 unsigned int flags = FAULT_FLAG_DEFAULT;
133 struct task_struct *tsk = current;
134 struct mm_struct *mm = tsk->mm;
135 struct vm_area_struct *vma = NULL;
138 if (kprobe_page_fault(regs, current->thread.trap_nr))
142 * We fault-in kernel-space virtual memory on-demand. The
143 * 'reference' page table is init_mm.pgd.
145 * NOTE! We MUST NOT take any locks for this case. We may
146 * be in an interrupt or a critical region, and should
147 * only copy the information from the master page table,
150 if (address & __UA_LIMIT) {
151 if (!user_mode(regs))
152 no_context(regs, address);
154 do_sigsegv(regs, write, address, si_code);
159 * If we're in an interrupt or have no user
160 * context, we must not take the fault..
162 if (faulthandler_disabled() || !mm) {
163 do_sigsegv(regs, write, address, si_code);
168 flags |= FAULT_FLAG_USER;
170 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
172 vma = lock_mm_and_find_vma(mm, address, regs);
174 goto bad_area_nosemaphore;
178 * Something tried to access memory that isn't in our memory map..
179 * Fix it, but check if it's kernel or user first..
182 mmap_read_unlock(mm);
183 bad_area_nosemaphore:
184 do_sigsegv(regs, write, address, si_code);
188 * Ok, we have a good vm_area for this memory access, so
192 si_code = SEGV_ACCERR;
195 flags |= FAULT_FLAG_WRITE;
196 if (!(vma->vm_flags & VM_WRITE))
199 if (!(vma->vm_flags & VM_READ) && address != exception_era(regs))
201 if (!(vma->vm_flags & VM_EXEC) && address == exception_era(regs))
206 * If for any reason at all we couldn't handle the fault,
207 * make sure we exit gracefully rather than endlessly redo
210 fault = handle_mm_fault(vma, address, flags, regs);
212 if (fault_signal_pending(fault, regs)) {
213 if (!user_mode(regs))
214 no_context(regs, address);
218 /* The fault is fully completed (including releasing mmap lock) */
219 if (fault & VM_FAULT_COMPLETED)
222 if (unlikely(fault & VM_FAULT_RETRY)) {
223 flags |= FAULT_FLAG_TRIED;
226 * No need to mmap_read_unlock(mm) as we would
227 * have already released it in __lock_page_or_retry
232 if (unlikely(fault & VM_FAULT_ERROR)) {
233 mmap_read_unlock(mm);
234 if (fault & VM_FAULT_OOM) {
235 do_out_of_memory(regs, address);
237 } else if (fault & VM_FAULT_SIGSEGV) {
238 do_sigsegv(regs, write, address, si_code);
240 } else if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
241 do_sigbus(regs, write, address, si_code);
247 mmap_read_unlock(mm);
250 asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
251 unsigned long write, unsigned long address)
253 irqentry_state_t state = irqentry_enter(regs);
255 /* Enable interrupt if enabled in parent context */
256 if (likely(regs->csr_prmd & CSR_PRMD_PIE))
259 __do_page_fault(regs, write, address);
263 irqentry_exit(regs, state);