1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
4 #include <linux/signal.h>
5 #include <linux/module.h>
6 #include <linux/sched.h>
7 #include <linux/interrupt.h>
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/string.h>
11 #include <linux/types.h>
12 #include <linux/ptrace.h>
13 #include <linux/mman.h>
15 #include <linux/smp.h>
16 #include <linux/version.h>
17 #include <linux/vt_kern.h>
18 #include <linux/extable.h>
19 #include <linux/uaccess.h>
20 #include <linux/perf_event.h>
21 #include <linux/kprobes.h>
23 #include <asm/hardirq.h>
24 #include <asm/mmu_context.h>
25 #include <asm/traps.h>
28 int fixup_exception(struct pt_regs *regs)
30 const struct exception_table_entry *fixup;
32 fixup = search_exception_tables(instruction_pointer(regs));
34 regs->pc = fixup->nextinsn;
42 static inline bool is_write(struct pt_regs *regs)
44 switch (trap_no(regs)) {
54 #ifdef CONFIG_CPU_HAS_LDSTEX
55 static inline void csky_cmpxchg_fixup(struct pt_regs *regs)
60 extern unsigned long csky_cmpxchg_ldw;
61 extern unsigned long csky_cmpxchg_stw;
62 static inline void csky_cmpxchg_fixup(struct pt_regs *regs)
64 if (trap_no(regs) != VEC_TLBMODIFIED)
67 if (instruction_pointer(regs) == csky_cmpxchg_stw)
68 instruction_pointer_set(regs, csky_cmpxchg_ldw);
74 * This routine handles page faults. It determines the address,
75 * and the problem, and then passes it off to one of the appropriate
78 asmlinkage void do_page_fault(struct pt_regs *regs)
80 struct vm_area_struct *vma = NULL;
81 struct task_struct *tsk = current;
82 struct mm_struct *mm = tsk->mm;
83 unsigned int flags = FAULT_FLAG_DEFAULT;
86 unsigned long address = read_mmu_entryhi() & PAGE_MASK;
88 csky_cmpxchg_fixup(regs);
90 if (kprobe_page_fault(regs, tsk->thread.trap_no))
93 si_code = SEGV_MAPERR;
96 * We fault-in kernel-space virtual memory on-demand. The
97 * 'reference' page table is init_mm.pgd.
99 * NOTE! We MUST NOT take any locks for this case. We may
100 * be in an interrupt or a critical region, and should
101 * only copy the information from the master page table,
104 if (unlikely(address >= VMALLOC_START) &&
105 unlikely(address <= VMALLOC_END)) {
107 * Synchronize this task's top level page-table
108 * with the 'reference' page table.
110 * Do _not_ use "tsk" here. We might be inside
111 * an interrupt in the middle of a task switch..
113 int offset = pgd_index(address);
119 pgd = get_pgd() + offset;
120 pgd_k = init_mm.pgd + offset;
122 if (!pgd_present(*pgd_k))
124 set_pgd(pgd, *pgd_k);
127 pud_k = (pud_t *)pgd_k;
128 if (!pud_present(*pud_k))
131 pmd = pmd_offset(pud, address);
132 pmd_k = pmd_offset(pud_k, address);
133 if (!pmd_present(*pmd_k))
135 set_pmd(pmd, *pmd_k);
137 pte_k = pte_offset_kernel(pmd_k, address);
138 if (!pte_present(*pte_k))
141 flush_tlb_one(address);
147 * If we're in an interrupt or have no user
148 * context, we must not take the fault..
150 if (unlikely(faulthandler_disabled() || !mm))
151 goto bad_area_nosemaphore;
154 flags |= FAULT_FLAG_USER;
157 flags |= FAULT_FLAG_WRITE;
159 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
162 vma = find_vma(mm, address);
165 if (vma->vm_start <= address)
167 if (!(vma->vm_flags & VM_GROWSDOWN))
169 if (expand_stack(vma, address))
172 * Ok, we have a good vm_area for this memory access, so
176 si_code = SEGV_ACCERR;
178 if (is_write(regs)) {
179 if (!(vma->vm_flags & VM_WRITE))
182 if (unlikely(!vma_is_accessible(vma)))
187 * If for any reason at all we couldn't handle the fault,
188 * make sure we exit gracefully rather than endlessly redo
191 fault = handle_mm_fault(vma, address, flags, regs);
192 if (unlikely(fault & VM_FAULT_ERROR)) {
193 if (fault & VM_FAULT_OOM)
195 else if (fault & VM_FAULT_SIGBUS)
197 else if (fault & VM_FAULT_SIGSEGV)
202 if (unlikely((fault & VM_FAULT_RETRY) && (flags & FAULT_FLAG_ALLOW_RETRY))) {
203 flags |= FAULT_FLAG_TRIED;
206 * No need to mmap_read_unlock(mm) as we would
207 * have already released it in __lock_page_or_retry
213 mmap_read_unlock(mm);
217 * Something tried to access memory that isn't in our memory map..
218 * Fix it, but check if it's kernel or user first..
221 mmap_read_unlock(mm);
223 bad_area_nosemaphore:
224 /* User mode accesses just cause a SIGSEGV */
225 if (user_mode(regs)) {
226 tsk->thread.trap_no = trap_no(regs);
227 force_sig_fault(SIGSEGV, si_code, (void __user *)address);
232 tsk->thread.trap_no = trap_no(regs);
234 /* Are we prepared to handle this kernel fault? */
235 if (fixup_exception(regs))
239 * Oops. The kernel tried to access some bad page. We'll have to
240 * terminate things with extreme prejudice.
243 pr_alert("Unable to handle kernel paging request at virtual "
244 "address 0x%08lx, pc: 0x%08lx\n", address, regs->pc);
248 tsk->thread.trap_no = trap_no(regs);
251 * We ran out of memory, call the OOM killer, and return the userspace
252 * (which will retry the fault, or kill us if we got oom-killed).
254 pagefault_out_of_memory();
258 tsk->thread.trap_no = trap_no(regs);
260 mmap_read_unlock(mm);
262 /* Kernel mode? Handle exceptions or die */
263 if (!user_mode(regs))
266 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address);