1 /* Page Fault Handling for ARC (TLB Miss / ProtV) 2 * 3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 */ 9 10 #include <linux/signal.h> 11 #include <linux/interrupt.h> 12 #include <linux/sched.h> 13 #include <linux/errno.h> 14 #include <linux/ptrace.h> 15 #include <linux/uaccess.h> 16 #include <linux/kdebug.h> 17 #include <asm/pgalloc.h> 18 19 static int handle_vmalloc_fault(struct mm_struct *mm, unsigned long address) 20 { 21 /* 22 * Synchronize this task's top level page-table 23 * with the 'reference' page table. 24 */ 25 pgd_t *pgd, *pgd_k; 26 pud_t *pud, *pud_k; 27 pmd_t *pmd, *pmd_k; 28 29 pgd = pgd_offset_fast(mm, address); 30 pgd_k = pgd_offset_k(address); 31 32 if (!pgd_present(*pgd_k)) 33 goto bad_area; 34 35 pud = pud_offset(pgd, address); 36 pud_k = pud_offset(pgd_k, address); 37 if (!pud_present(*pud_k)) 38 goto bad_area; 39 40 pmd = pmd_offset(pud, address); 41 pmd_k = pmd_offset(pud_k, address); 42 if (!pmd_present(*pmd_k)) 43 goto bad_area; 44 45 set_pmd(pmd, *pmd_k); 46 47 /* XXX: create the TLB entry here */ 48 return 0; 49 50 bad_area: 51 return 1; 52 } 53 54 void do_page_fault(struct pt_regs *regs, int write, unsigned long address, 55 unsigned long cause_code) 56 { 57 struct vm_area_struct *vma = NULL; 58 struct task_struct *tsk = current; 59 struct mm_struct *mm = tsk->mm; 60 siginfo_t info; 61 int fault, ret; 62 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE | 63 (write ? FAULT_FLAG_WRITE : 0); 64 65 /* 66 * We fault-in kernel-space virtual memory on-demand. The 67 * 'reference' page table is init_mm.pgd. 68 * 69 * NOTE! We MUST NOT take any locks for this case. We may 70 * be in an interrupt or a critical region, and should 71 * only copy the information from the master page table, 72 * nothing more. 73 */ 74 if (address >= VMALLOC_START && address <= VMALLOC_END) { 75 ret = handle_vmalloc_fault(mm, address); 76 if (unlikely(ret)) 77 goto bad_area_nosemaphore; 78 else 79 return; 80 } 81 82 info.si_code = SEGV_MAPERR; 83 84 /* 85 * If we're in an interrupt or have no user 86 * context, we must not take the fault.. 87 */ 88 if (in_atomic() || !mm) 89 goto no_context; 90 91 retry: 92 down_read(&mm->mmap_sem); 93 vma = find_vma(mm, address); 94 if (!vma) 95 goto bad_area; 96 if (vma->vm_start <= address) 97 goto good_area; 98 if (!(vma->vm_flags & VM_GROWSDOWN)) 99 goto bad_area; 100 if (expand_stack(vma, address)) 101 goto bad_area; 102 103 /* 104 * Ok, we have a good vm_area for this memory access, so 105 * we can handle it.. 106 */ 107 good_area: 108 info.si_code = SEGV_ACCERR; 109 110 /* Handle protection violation, execute on heap or stack */ 111 112 if (cause_code == ((ECR_V_PROTV << 16) | ECR_C_PROTV_INST_FETCH)) 113 goto bad_area; 114 115 if (write) { 116 if (!(vma->vm_flags & VM_WRITE)) 117 goto bad_area; 118 } else { 119 if (!(vma->vm_flags & (VM_READ | VM_EXEC))) 120 goto bad_area; 121 } 122 123 survive: 124 /* 125 * If for any reason at all we couldn't handle the fault, 126 * make sure we exit gracefully rather than endlessly redo 127 * the fault. 128 */ 129 fault = handle_mm_fault(mm, vma, address, flags); 130 131 /* If Pagefault was interrupted by SIGKILL, exit page fault "early" */ 132 if (unlikely(fatal_signal_pending(current))) { 133 if ((fault & VM_FAULT_ERROR) && !(fault & VM_FAULT_RETRY)) 134 up_read(&mm->mmap_sem); 135 if (user_mode(regs)) 136 return; 137 } 138 139 if (likely(!(fault & VM_FAULT_ERROR))) { 140 if (flags & FAULT_FLAG_ALLOW_RETRY) { 141 /* To avoid updating stats twice for retry case */ 142 if (fault & VM_FAULT_MAJOR) 143 tsk->maj_flt++; 144 else 145 tsk->min_flt++; 146 147 if (fault & VM_FAULT_RETRY) { 148 flags &= ~FAULT_FLAG_ALLOW_RETRY; 149 flags |= FAULT_FLAG_TRIED; 150 goto retry; 151 } 152 } 153 154 /* Fault Handled Gracefully */ 155 up_read(&mm->mmap_sem); 156 return; 157 } 158 159 /* TBD: switch to pagefault_out_of_memory() */ 160 if (fault & VM_FAULT_OOM) 161 goto out_of_memory; 162 else if (fault & VM_FAULT_SIGBUS) 163 goto do_sigbus; 164 165 /* no man's land */ 166 BUG(); 167 168 /* 169 * Something tried to access memory that isn't in our memory map.. 170 * Fix it, but check if it's kernel or user first.. 171 */ 172 bad_area: 173 up_read(&mm->mmap_sem); 174 175 bad_area_nosemaphore: 176 /* User mode accesses just cause a SIGSEGV */ 177 if (user_mode(regs)) { 178 tsk->thread.fault_address = address; 179 tsk->thread.cause_code = cause_code; 180 info.si_signo = SIGSEGV; 181 info.si_errno = 0; 182 /* info.si_code has been set above */ 183 info.si_addr = (void __user *)address; 184 force_sig_info(SIGSEGV, &info, tsk); 185 return; 186 } 187 188 no_context: 189 /* Are we prepared to handle this kernel fault? 190 * 191 * (The kernel has valid exception-points in the source 192 * when it acesses user-memory. When it fails in one 193 * of those points, we find it in a table and do a jump 194 * to some fixup code that loads an appropriate error 195 * code) 196 */ 197 if (fixup_exception(regs)) 198 return; 199 200 die("Oops", regs, address, cause_code); 201 202 out_of_memory: 203 if (is_global_init(tsk)) { 204 yield(); 205 goto survive; 206 } 207 up_read(&mm->mmap_sem); 208 209 if (user_mode(regs)) 210 do_group_exit(SIGKILL); /* This will never return */ 211 212 goto no_context; 213 214 do_sigbus: 215 up_read(&mm->mmap_sem); 216 217 if (!user_mode(regs)) 218 goto no_context; 219 220 tsk->thread.fault_address = address; 221 tsk->thread.cause_code = cause_code; 222 info.si_signo = SIGBUS; 223 info.si_errno = 0; 224 info.si_code = BUS_ADRERR; 225 info.si_addr = (void __user *)address; 226 force_sig_info(SIGBUS, &info, tsk); 227 } 228