2 * This file is subject to the terms and conditions of the GNU General Public
\r
3 * License. See the file "COPYING" in the main directory of this archive
\r
6 * Copyright (C) 1995 - 2000 by Ralf Baechle
\r
8 #include <linux/signal.h>
\r
9 #include <linux/sched.h>
\r
10 #include <linux/interrupt.h>
\r
11 #include <linux/kernel.h>
\r
12 #include <linux/errno.h>
\r
13 #include <linux/string.h>
\r
14 #include <linux/types.h>
\r
15 #include <linux/ptrace.h>
\r
16 #include <linux/mman.h>
\r
17 #include <linux/mm.h>
\r
18 #include <linux/smp.h>
\r
19 #include <linux/smp_lock.h>
\r
20 #include <linux/vt_kern.h> /* For unblank_screen() */
\r
21 #include <linux/module.h>
\r
23 #include <asm/branch.h>
\r
24 #include <asm/hardirq.h>
\r
25 #include <asm/mmu_context.h>
\r
26 #include <asm/system.h>
\r
27 #include <asm/uaccess.h>
\r
28 #include <asm/ptrace.h>
\r
31 * This routine handles page faults. It determines the address,
\r
32 * and the problem, and then passes it off to one of the appropriate
\r
35 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
\r
36 unsigned long address)
\r
38 struct vm_area_struct * vma = NULL;
\r
39 struct task_struct *tsk = current;
\r
40 struct mm_struct *mm = tsk->mm;
\r
41 const int field = sizeof(unsigned long) * 2;
\r
45 printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", smp_processor_id(),
\r
46 current->comm, current->pid, field, address, write,
\r
47 field, regs->cp0_epc);
\r
50 info.si_code = SEGV_MAPERR;
\r
53 * We fault-in kernel-space virtual memory on-demand. The
\r
54 * 'reference' page table is init_mm.pgd.
\r
56 * NOTE! We MUST NOT take any locks for this case. We may
\r
57 * be in an interrupt or a critical region, and should
\r
58 * only copy the information from the master page table,
\r
61 if (unlikely(address >= VMALLOC_START))
\r
65 * If we're in an interrupt or have no user
\r
66 * context, we must not take the fault..
\r
68 if (in_atomic() || !mm)
\r
69 goto bad_area_nosemaphore;
\r
71 down_read(&mm->mmap_sem);
\r
72 vma = find_vma(mm, address);
\r
75 if (vma->vm_start <= address)
\r
77 if (!(vma->vm_flags & VM_GROWSDOWN))
\r
79 if (expand_stack(vma, address))
\r
82 * Ok, we have a good vm_area for this memory access, so
\r
83 * we can handle it..
\r
86 info.si_code = SEGV_ACCERR;
\r
89 if (!(vma->vm_flags & VM_WRITE))
\r
92 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
\r
98 * If for any reason at all we couldn't handle the fault,
\r
99 * make sure we exit gracefully rather than endlessly redo
\r
102 switch (handle_mm_fault(mm, vma, address, write)) {
\r
103 case VM_FAULT_MINOR:
\r
106 case VM_FAULT_MAJOR:
\r
109 case VM_FAULT_SIGBUS:
\r
112 goto out_of_memory;
\r
117 up_read(&mm->mmap_sem);
\r
121 * Something tried to access memory that isn't in our memory map..
\r
122 * Fix it, but check if it's kernel or user first..
\r
125 up_read(&mm->mmap_sem);
\r
127 bad_area_nosemaphore:
\r
128 /* User mode accesses just cause a SIGSEGV */
\r
129 if (user_mode(regs)) {
\r
130 tsk->thread.cp0_badvaddr = address;
\r
131 tsk->thread.error_code = write;
\r
133 printk("do_page_fault() #2: sending SIGSEGV to %s for "
\r
134 "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
\r
136 write ? "write access to" : "read access from",
\r
138 field, (unsigned long) regs->cp0_epc,
\r
139 field, (unsigned long) regs->regs[31]);
\r
141 info.si_signo = SIGSEGV;
\r
143 /* info.si_code has been set above */
\r
144 info.si_addr = (void *) address;
\r
145 force_sig_info(SIGSEGV, &info, tsk);
\r
150 /* Are we prepared to handle this kernel fault? */
\r
151 if (fixup_exception(regs)) {
\r
152 current->thread.cp0_baduaddr = address;
\r
157 * Oops. The kernel tried to access some bad page. We'll have to
\r
158 * terminate things with extreme prejudice.
\r
163 printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
\r
164 "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
\r
165 smp_processor_id(), field, address, field, regs->cp0_epc,
\r
166 field, regs->regs[31]);
\r
170 * We ran out of memory, or some other thing happened to us that made
\r
171 * us unable to handle the page fault gracefully.
\r
174 up_read(&mm->mmap_sem);
\r
175 if (tsk->pid == 1) {
\r
177 down_read(&mm->mmap_sem);
\r
180 printk("VM: killing process %s\n", tsk->comm);
\r
181 if (user_mode(regs))
\r
186 up_read(&mm->mmap_sem);
\r
188 /* Kernel mode? Handle exceptions or die */
\r
189 if (!user_mode(regs))
\r
193 * Send a sigbus, regardless of whether we were in kernel
\r
196 tsk->thread.cp0_badvaddr = address;
\r
197 info.si_signo = SIGBUS;
\r
199 info.si_code = BUS_ADRERR;
\r
200 info.si_addr = (void *) address;
\r
201 force_sig_info(SIGBUS, &info, tsk);
\r
208 * Synchronize this task's top level page-table
\r
209 * with the 'reference' page table.
\r
211 * Do _not_ use "tsk" here. We might be inside
\r
212 * an interrupt in the middle of a task switch..
\r
214 int offset = __pgd_offset(address);
\r
215 pgd_t *pgd, *pgd_k;
\r
216 pmd_t *pmd, *pmd_k;
\r
219 pgd = (pgd_t *) pgd_current[smp_processor_id()] + offset;
\r
220 pgd_k = init_mm.pgd + offset;
\r
222 if (!pgd_present(*pgd_k))
\r
224 set_pgd(pgd, *pgd_k);
\r
226 pmd = pmd_offset(pgd, address);
\r
227 pmd_k = pmd_offset(pgd_k, address);
\r
228 if (!pmd_present(*pmd_k))
\r
230 set_pmd(pmd, *pmd_k);
\r
232 pte_k = pte_offset_kernel(pmd_k, address);
\r
233 if (!pte_present(*pte_k))
\r