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>
22 #include <asm/hardirq.h>
23 #include <asm/mmu_context.h>
24 #include <asm/traps.h>
27 int fixup_exception(struct pt_regs
*regs
)
29 const struct exception_table_entry
*fixup
;
31 fixup
= search_exception_tables(instruction_pointer(regs
));
33 regs
->pc
= fixup
->nextinsn
;
42 * This routine handles page faults. It determines the address,
43 * and the problem, and then passes it off to one of the appropriate
46 asmlinkage
void do_page_fault(struct pt_regs
*regs
, unsigned long write
,
47 unsigned long mmu_meh
)
49 struct vm_area_struct
*vma
= NULL
;
50 struct task_struct
*tsk
= current
;
51 struct mm_struct
*mm
= tsk
->mm
;
54 unsigned long address
= mmu_meh
& PAGE_MASK
;
56 si_code
= SEGV_MAPERR
;
58 #ifndef CONFIG_CPU_HAS_TLBI
60 * We fault-in kernel-space virtual memory on-demand. The
61 * 'reference' page table is init_mm.pgd.
63 * NOTE! We MUST NOT take any locks for this case. We may
64 * be in an interrupt or a critical region, and should
65 * only copy the information from the master page table,
68 if (unlikely(address
>= VMALLOC_START
) &&
69 unlikely(address
<= VMALLOC_END
)) {
71 * Synchronize this task's top level page-table
72 * with the 'reference' page table.
74 * Do _not_ use "tsk" here. We might be inside
75 * an interrupt in the middle of a task switch..
77 int offset
= __pgd_offset(address
);
83 unsigned long pgd_base
;
85 pgd_base
= (unsigned long)__va(get_pgd());
86 pgd
= (pgd_t
*)pgd_base
+ offset
;
87 pgd_k
= init_mm
.pgd
+ offset
;
89 if (!pgd_present(*pgd_k
))
94 pud_k
= (pud_t
*)pgd_k
;
95 if (!pud_present(*pud_k
))
98 pmd
= pmd_offset(pud
, address
);
99 pmd_k
= pmd_offset(pud_k
, address
);
100 if (!pmd_present(*pmd_k
))
102 set_pmd(pmd
, *pmd_k
);
104 pte_k
= pte_offset_kernel(pmd_k
, address
);
105 if (!pte_present(*pte_k
))
111 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, regs
, address
);
113 * If we're in an interrupt or have no user
114 * context, we must not take the fault..
116 if (in_atomic() || !mm
)
117 goto bad_area_nosemaphore
;
119 down_read(&mm
->mmap_sem
);
120 vma
= find_vma(mm
, address
);
123 if (vma
->vm_start
<= address
)
125 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
127 if (expand_stack(vma
, address
))
130 * Ok, we have a good vm_area for this memory access, so
134 si_code
= SEGV_ACCERR
;
137 if (!(vma
->vm_flags
& VM_WRITE
))
140 if (!(vma
->vm_flags
& (VM_READ
| VM_WRITE
| VM_EXEC
)))
145 * If for any reason at all we couldn't handle the fault,
146 * make sure we exit gracefully rather than endlessly redo
149 fault
= handle_mm_fault(vma
, address
, write
? FAULT_FLAG_WRITE
: 0);
150 if (unlikely(fault
& VM_FAULT_ERROR
)) {
151 if (fault
& VM_FAULT_OOM
)
153 else if (fault
& VM_FAULT_SIGBUS
)
155 else if (fault
& VM_FAULT_SIGSEGV
)
159 if (fault
& VM_FAULT_MAJOR
) {
161 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
, 1, regs
,
165 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
, 1, regs
,
169 up_read(&mm
->mmap_sem
);
173 * Something tried to access memory that isn't in our memory map..
174 * Fix it, but check if it's kernel or user first..
177 up_read(&mm
->mmap_sem
);
179 bad_area_nosemaphore
:
180 /* User mode accesses just cause a SIGSEGV */
181 if (user_mode(regs
)) {
182 force_sig_fault(SIGSEGV
, si_code
, (void __user
*)address
);
187 /* Are we prepared to handle this kernel fault? */
188 if (fixup_exception(regs
))
192 * Oops. The kernel tried to access some bad page. We'll have to
193 * terminate things with extreme prejudice.
196 pr_alert("Unable to handle kernel paging request at virtual "
197 "address 0x%08lx, pc: 0x%08lx\n", address
, regs
->pc
);
198 die_if_kernel("Oops", regs
, write
);
202 * We ran out of memory, call the OOM killer, and return the userspace
203 * (which will retry the fault, or kill us if we got oom-killed).
205 pagefault_out_of_memory();
209 up_read(&mm
->mmap_sem
);
211 /* Kernel mode? Handle exceptions or die */
212 if (!user_mode(regs
))
215 force_sig_fault(SIGBUS
, BUS_ADRERR
, (void __user
*)address
);