1/* 2 * This file is subject to the terms and conditions of the GNU General Public 3 * License. See the file "COPYING" in the main directory of this archive 4 * for more details. 5 * 6 * Copyright (C) 1995 - 2000 by Ralf Baechle 7 */ 8#include <linux/signal.h> 9#include <linux/sched.h> 10#include <linux/interrupt.h> 11#include <linux/kernel.h> 12#include <linux/errno.h> 13#include <linux/string.h> 14#include <linux/types.h> 15#include <linux/ptrace.h> 16#include <linux/mman.h> 17#include <linux/mm.h> 18#include <linux/smp.h> 19#include <linux/vt_kern.h> /* For unblank_screen() */ 20#include <linux/module.h> 21 22#include <asm/branch.h> 23#include <asm/mmu_context.h> 24#include <asm/system.h> 25#include <asm/uaccess.h> 26#include <asm/ptrace.h> 27#include <asm/highmem.h> /* For VMALLOC_END */ 28 29/* 30 * This routine handles page faults. It determines the address, 31 * and the problem, and then passes it off to one of the appropriate 32 * routines. 33 */ 34asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write, 35 unsigned long address) 36{ 37 struct vm_area_struct * vma = NULL; 38 struct task_struct *tsk = current; 39 struct mm_struct *mm = tsk->mm; 40 const int field = sizeof(unsigned long) * 2; 41 siginfo_t info; 42 43 44 info.si_code = SEGV_MAPERR; 45 46 /* 47 * We fault-in kernel-space virtual memory on-demand. The 48 * 'reference' page table is init_mm.pgd. 49 * 50 * NOTE! We MUST NOT take any locks for this case. We may 51 * be in an interrupt or a critical region, and should 52 * only copy the information from the master page table, 53 * nothing more. 54 */ 55 if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END)) 56 goto vmalloc_fault; 57#ifdef MODULE_START 58 if (unlikely(address >= MODULE_START && address < MODULE_END)) 59 goto vmalloc_fault; 60#endif 61 62 /* 63 * If we're in an interrupt or have no user 64 * context, we must not take the fault.. 65 */ 66 if (in_atomic() || !mm) 67 goto bad_area_nosemaphore; 68 69 down_read(&mm->mmap_sem); 70 vma = find_vma(mm, address); 71 if (!vma) 72 goto bad_area; 73 if (vma->vm_start <= address) 74 goto good_area; 75 if (!(vma->vm_flags & VM_GROWSDOWN)) 76 goto bad_area; 77 if (expand_stack(vma, address)) 78 goto bad_area; 79/* 80 * Ok, we have a good vm_area for this memory access, so 81 * we can handle it.. 82 */ 83good_area: 84 info.si_code = SEGV_ACCERR; 85 86 if (write) { 87 if (!(vma->vm_flags & VM_WRITE)) 88 goto bad_area; 89 } else { 90 if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))) 91 goto bad_area; 92 } 93 94survive: 95 /* 96 * If for any reason at all we couldn't handle the fault, 97 * make sure we exit gracefully rather than endlessly redo 98 * the fault. 99 */ 100 switch (handle_mm_fault(mm, vma, address, write)) { 101 case VM_FAULT_MINOR: 102 tsk->min_flt++; 103 break; 104 case VM_FAULT_MAJOR: 105 tsk->maj_flt++; 106 break; 107 case VM_FAULT_SIGBUS: 108 goto do_sigbus; 109 case VM_FAULT_OOM: 110 goto out_of_memory; 111 default: 112 BUG(); 113 } 114 115 up_read(&mm->mmap_sem); 116 return; 117 118/* 119 * Something tried to access memory that isn't in our memory map.. 120 * Fix it, but check if it's kernel or user first.. 121 */ 122bad_area: 123 up_read(&mm->mmap_sem); 124 125bad_area_nosemaphore: 126 /* User mode accesses just cause a SIGSEGV */ 127 if (user_mode(regs)) { 128 tsk->thread.cp0_badvaddr = address; 129 tsk->thread.error_code = write; 130 info.si_signo = SIGSEGV; 131 info.si_errno = 0; 132 /* info.si_code has been set above */ 133 info.si_addr = (void __user *) address; 134 force_sig_info(SIGSEGV, &info, tsk); 135 return; 136 } 137 138no_context: 139 /* Are we prepared to handle this kernel fault? */ 140 if (fixup_exception(regs)) { 141 current->thread.cp0_baduaddr = address; 142 return; 143 } 144 145 /* 146 * Oops. The kernel tried to access some bad page. We'll have to 147 * terminate things with extreme prejudice. 148 */ 149 bust_spinlocks(1); 150 151 printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at " 152 "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n", 153 raw_smp_processor_id(), field, address, field, regs->cp0_epc, 154 field, regs->regs[31]); 155 die("Oops", regs); 156 157/* 158 * We ran out of memory, or some other thing happened to us that made 159 * us unable to handle the page fault gracefully. 160 */ 161out_of_memory: 162 up_read(&mm->mmap_sem); 163 if (is_init(tsk)) { 164 yield(); 165 down_read(&mm->mmap_sem); 166 goto survive; 167 } 168 printk("VM: killing process %s\n", tsk->comm); 169 if (user_mode(regs)) 170 do_exit(SIGKILL); 171 goto no_context; 172 173do_sigbus: 174 up_read(&mm->mmap_sem); 175 176 /* Kernel mode? Handle exceptions or die */ 177 if (!user_mode(regs)) 178 goto no_context; 179 else 180 /* 181 * Send a sigbus, regardless of whether we were in kernel 182 * or user mode. 183 */ 184 tsk->thread.cp0_badvaddr = address; 185 info.si_signo = SIGBUS; 186 info.si_errno = 0; 187 info.si_code = BUS_ADRERR; 188 info.si_addr = (void __user *) address; 189 force_sig_info(SIGBUS, &info, tsk); 190 191 return; 192vmalloc_fault: 193 { 194 /* 195 * Synchronize this task's top level page-table 196 * with the 'reference' page table. 197 * 198 * Do _not_ use "tsk" here. We might be inside 199 * an interrupt in the middle of a task switch.. 200 */ 201 int offset = __pgd_offset(address); 202 pgd_t *pgd, *pgd_k; 203 pud_t *pud, *pud_k; 204 pmd_t *pmd, *pmd_k; 205 pte_t *pte_k; 206 207 pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset; 208 pgd_k = init_mm.pgd + offset; 209 210 if (!pgd_present(*pgd_k)) 211 goto no_context; 212 set_pgd(pgd, *pgd_k); 213 214 pud = pud_offset(pgd, address); 215 pud_k = pud_offset(pgd_k, address); 216 if (!pud_present(*pud_k)) 217 goto no_context; 218 219 pmd = pmd_offset(pud, address); 220 pmd_k = pmd_offset(pud_k, address); 221 if (!pmd_present(*pmd_k)) 222 goto no_context; 223 set_pmd(pmd, *pmd_k); 224 225 pte_k = pte_offset_kernel(pmd_k, address); 226 if (!pte_present(*pte_k)) 227 goto no_context; 228 return; 229 } 230} 231