1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd. 3 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> 14 #include <linux/mm.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 21 #include <asm/hardirq.h> 22 #include <asm/mmu_context.h> 23 #include <asm/traps.h> 24 #include <asm/page.h> 25 26 int fixup_exception(struct pt_regs *regs) 27 { 28 const struct exception_table_entry *fixup; 29 30 fixup = search_exception_tables(instruction_pointer(regs)); 31 if (fixup) { 32 regs->pc = fixup->nextinsn; 33 34 return 1; 35 } 36 37 return 0; 38 } 39 40 /* 41 * This routine handles page faults. It determines the address, 42 * and the problem, and then passes it off to one of the appropriate 43 * routines. 44 */ 45 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write, 46 unsigned long mmu_meh) 47 { 48 struct vm_area_struct *vma = NULL; 49 struct task_struct *tsk = current; 50 struct mm_struct *mm = tsk->mm; 51 int si_code; 52 int fault; 53 unsigned long address = mmu_meh & PAGE_MASK; 54 55 si_code = SEGV_MAPERR; 56 57 #ifndef CONFIG_CPU_HAS_TLBI 58 /* 59 * We fault-in kernel-space virtual memory on-demand. The 60 * 'reference' page table is init_mm.pgd. 61 * 62 * NOTE! We MUST NOT take any locks for this case. We may 63 * be in an interrupt or a critical region, and should 64 * only copy the information from the master page table, 65 * nothing more. 66 */ 67 if (unlikely(address >= VMALLOC_START) && 68 unlikely(address <= VMALLOC_END)) { 69 /* 70 * Synchronize this task's top level page-table 71 * with the 'reference' page table. 72 * 73 * Do _not_ use "tsk" here. We might be inside 74 * an interrupt in the middle of a task switch.. 75 */ 76 int offset = __pgd_offset(address); 77 pgd_t *pgd, *pgd_k; 78 pud_t *pud, *pud_k; 79 pmd_t *pmd, *pmd_k; 80 pte_t *pte_k; 81 82 unsigned long pgd_base; 83 84 pgd_base = tlb_get_pgd(); 85 pgd = (pgd_t *)pgd_base + offset; 86 pgd_k = init_mm.pgd + offset; 87 88 if (!pgd_present(*pgd_k)) 89 goto no_context; 90 set_pgd(pgd, *pgd_k); 91 92 pud = (pud_t *)pgd; 93 pud_k = (pud_t *)pgd_k; 94 if (!pud_present(*pud_k)) 95 goto no_context; 96 97 pmd = pmd_offset(pud, address); 98 pmd_k = pmd_offset(pud_k, address); 99 if (!pmd_present(*pmd_k)) 100 goto no_context; 101 set_pmd(pmd, *pmd_k); 102 103 pte_k = pte_offset_kernel(pmd_k, address); 104 if (!pte_present(*pte_k)) 105 goto no_context; 106 return; 107 } 108 #endif 109 /* 110 * If we're in an interrupt or have no user 111 * context, we must not take the fault.. 112 */ 113 if (in_atomic() || !mm) 114 goto bad_area_nosemaphore; 115 116 down_read(&mm->mmap_sem); 117 vma = find_vma(mm, address); 118 if (!vma) 119 goto bad_area; 120 if (vma->vm_start <= address) 121 goto good_area; 122 if (!(vma->vm_flags & VM_GROWSDOWN)) 123 goto bad_area; 124 if (expand_stack(vma, address)) 125 goto bad_area; 126 /* 127 * Ok, we have a good vm_area for this memory access, so 128 * we can handle it.. 129 */ 130 good_area: 131 si_code = SEGV_ACCERR; 132 133 if (write) { 134 if (!(vma->vm_flags & VM_WRITE)) 135 goto bad_area; 136 } else { 137 if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))) 138 goto bad_area; 139 } 140 141 /* 142 * If for any reason at all we couldn't handle the fault, 143 * make sure we exit gracefully rather than endlessly redo 144 * the fault. 145 */ 146 fault = handle_mm_fault(vma, address, write ? FAULT_FLAG_WRITE : 0); 147 if (unlikely(fault & VM_FAULT_ERROR)) { 148 if (fault & VM_FAULT_OOM) 149 goto out_of_memory; 150 else if (fault & VM_FAULT_SIGBUS) 151 goto do_sigbus; 152 else if (fault & VM_FAULT_SIGSEGV) 153 goto bad_area; 154 BUG(); 155 } 156 if (fault & VM_FAULT_MAJOR) 157 tsk->maj_flt++; 158 else 159 tsk->min_flt++; 160 161 up_read(&mm->mmap_sem); 162 return; 163 164 /* 165 * Something tried to access memory that isn't in our memory map.. 166 * Fix it, but check if it's kernel or user first.. 167 */ 168 bad_area: 169 up_read(&mm->mmap_sem); 170 171 bad_area_nosemaphore: 172 /* User mode accesses just cause a SIGSEGV */ 173 if (user_mode(regs)) { 174 force_sig_fault(SIGSEGV, si_code, (void __user *)address, current); 175 return; 176 } 177 178 no_context: 179 /* Are we prepared to handle this kernel fault? */ 180 if (fixup_exception(regs)) 181 return; 182 183 /* 184 * Oops. The kernel tried to access some bad page. We'll have to 185 * terminate things with extreme prejudice. 186 */ 187 bust_spinlocks(1); 188 pr_alert("Unable to handle kernel paging request at virtual " 189 "address 0x%08lx, pc: 0x%08lx\n", address, regs->pc); 190 die_if_kernel("Oops", regs, write); 191 192 out_of_memory: 193 /* 194 * We ran out of memory, call the OOM killer, and return the userspace 195 * (which will retry the fault, or kill us if we got oom-killed). 196 */ 197 pagefault_out_of_memory(); 198 return; 199 200 do_sigbus: 201 up_read(&mm->mmap_sem); 202 203 /* Kernel mode? Handle exceptions or die */ 204 if (!user_mode(regs)) 205 goto no_context; 206 207 force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, current); 208 } 209