Import 2.3.12pre2
[davej-history.git] / arch / mips / mm / fault.c
blob2389c0b86ba8ddd713f8cd978ecb60f9a703eda6
1 /* $Id: fault.c,v 1.9 1999/01/04 16:03:53 ralf Exp $
3 * This file is subject to the terms and conditions of the GNU General Public
4 * License. See the file "COPYING" in the main directory of this archive
5 * for more details.
7 * Copyright (C) 1995, 1996, 1997, 1998 by Ralf Baechle
8 */
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/types.h>
16 #include <linux/ptrace.h>
17 #include <linux/mman.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/version.h>
23 #include <asm/hardirq.h>
24 #include <asm/pgtable.h>
25 #include <asm/mmu_context.h>
26 #include <asm/softirq.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
30 #define development_version (LINUX_VERSION_CODE & 0x100)
32 extern void die(char *, struct pt_regs *, unsigned long write);
34 unsigned long asid_cache;
37 * Macro for exception fixup code to access integer registers.
39 #define dpf_reg(r) (regs->regs[r])
42 * This routine handles page faults. It determines the address,
43 * and the problem, and then passes it off to one of the appropriate
44 * routines.
46 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
47 unsigned long address)
49 struct vm_area_struct * vma;
50 struct task_struct *tsk = current;
51 struct mm_struct *mm = tsk->mm;
52 unsigned long fixup;
55 * If we're in an interrupt or have no user
56 * context, we must not take the fault..
58 if (in_interrupt() || !mm)
59 goto no_context;
60 #if 0
61 printk("[%s:%d:%08lx:%ld:%08lx]\n", current->comm, current->pid,
62 address, write, regs->cp0_epc);
63 #endif
64 down(&mm->mmap_sem);
65 vma = find_vma(mm, address);
66 if (!vma)
67 goto bad_area;
68 if (vma->vm_start <= address)
69 goto good_area;
70 if (!(vma->vm_flags & VM_GROWSDOWN))
71 goto bad_area;
72 if (expand_stack(vma, address))
73 goto bad_area;
75 * Ok, we have a good vm_area for this memory access, so
76 * we can handle it..
78 good_area:
79 if (write) {
80 if (!(vma->vm_flags & VM_WRITE))
81 goto bad_area;
82 } else {
83 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
84 goto bad_area;
88 * If for any reason at all we couldn't handle the fault,
89 * make sure we exit gracefully rather than endlessly redo
90 * the fault.
93 int fault = handle_mm_fault(tsk, vma, address, write);
94 if (fault < 0)
95 goto out_of_memory;
96 if (!fault)
97 goto do_sigbus;
99 up(&mm->mmap_sem);
100 return;
103 * Something tried to access memory that isn't in our memory map..
104 * Fix it, but check if it's kernel or user first..
106 bad_area:
107 up(&mm->mmap_sem);
109 if (user_mode(regs)) {
110 tsk->tss.cp0_badvaddr = address;
111 tsk->tss.error_code = write;
112 #if 0
113 printk("do_page_fault() #2: sending SIGSEGV to %s for illegal %s\n"
114 "%08lx (epc == %08lx, ra == %08lx)\n",
115 tsk->comm,
116 write ? "write access to" : "read access from",
117 address,
118 (unsigned long) regs->cp0_epc,
119 (unsigned long) regs->regs[31]);
120 #endif
121 force_sig(SIGSEGV, tsk);
122 return;
125 no_context:
126 /* Are we prepared to handle this kernel fault? */
127 fixup = search_exception_table(regs->cp0_epc);
128 if (fixup) {
129 long new_epc;
131 tsk->tss.cp0_baduaddr = address;
132 new_epc = fixup_exception(dpf_reg, fixup, regs->cp0_epc);
133 if (development_version)
134 printk(KERN_DEBUG "%s: Exception at [<%lx>] (%lx)\n",
135 tsk->comm, regs->cp0_epc, new_epc);
136 regs->cp0_epc = new_epc;
137 return;
141 * Oops. The kernel tried to access some bad page. We'll have to
142 * terminate things with extreme prejudice.
144 printk(KERN_ALERT "Unable to handle kernel paging request at virtual "
145 "address %08lx, epc == %08lx, ra == %08lx\n",
146 address, regs->cp0_epc, regs->regs[31]);
147 die("Oops", regs, write);
148 do_exit(SIGKILL);
151 * We ran out of memory, or some other thing happened to us that made
152 * us unable to handle the page fault gracefully.
154 out_of_memory:
155 up(&mm->mmap_sem);
156 printk("VM: killing process %s\n", tsk->comm);
157 if (user_mode(regs))
158 do_exit(SIGKILL);
159 goto no_context;
161 do_sigbus:
162 up(&mm->mmap_sem);
165 * Send a sigbus, regardless of whether we were in kernel
166 * or user mode.
167 * XXX Store details about fault for siginfo handling into tss.
169 force_sig(SIGBUS, tsk);
171 /* Kernel mode? Handle exceptions or die */
172 if (!user_mode(regs))
173 goto no_context;