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
6 * Copyright (C) 1995 - 2000 by Ralf Baechle
7 * Copyright (C) 1999, 2000 by Silicon Graphics, Inc.
9 #include <linux/config.h>
10 #include <linux/signal.h>
11 #include <linux/sched.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/ptrace.h>
18 #include <linux/mman.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/version.h>
23 #include <linux/vt_kern.h> /* For unblank_screen() */
24 #include <linux/module.h>
26 #include <asm/branch.h>
27 #include <asm/hardirq.h>
28 #include <asm/pgalloc.h>
29 #include <asm/mmu_context.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
32 #include <asm/ptrace.h>
34 #define development_version (LINUX_VERSION_CODE & 0x100)
37 * Macro for exception fixup code to access integer registers.
39 #define dpf_reg(r) (regs->regs[r])
42 * Unlock any spinlocks which will prevent us from getting the out
44 void bust_spinlocks(int yes)
46 int loglevel_save = console_loglevel;
57 * OK, the message is on the console. Now we call printk()
58 * without oops_in_progress set so that printk will give klogd
59 * a poke. Hold onto your hats...
61 console_loglevel = 15; /* NMI oopser may have shut the console up */
63 console_loglevel = loglevel_save;
67 * This routine handles page faults. It determines the address,
68 * and the problem, and then passes it off to one of the appropriate
71 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
72 unsigned long address)
74 struct vm_area_struct * vma;
75 struct task_struct *tsk = current;
76 struct mm_struct *mm = tsk->mm;
77 const struct exception_table_entry *fixup;
81 printk("Cpu%d[%s:%d:%08lx:%ld:%08lx]\n", smp_processor_id(),
82 current->comm, current->pid, address, write, regs->cp0_epc);
86 * We fault-in kernel-space virtual memory on-demand. The
87 * 'reference' page table is init_mm.pgd.
89 * NOTE! We MUST NOT take any locks for this case. We may
90 * be in an interrupt or a critical region, and should
91 * only copy the information from the master page table,
94 if (address >= VMALLOC_START)
97 info.si_code = SEGV_MAPERR;
99 * If we're in an interrupt or have no user
100 * context, we must not take the fault..
102 if (in_atomic() || !mm)
105 down_read(&mm->mmap_sem);
106 vma = find_vma(mm, address);
109 if (vma->vm_start <= address)
111 if (!(vma->vm_flags & VM_GROWSDOWN))
113 if (expand_stack(vma, address))
116 * Ok, we have a good vm_area for this memory access, so
120 info.si_code = SEGV_ACCERR;
123 if (!(vma->vm_flags & VM_WRITE))
126 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
132 * If for any reason at all we couldn't handle the fault,
133 * make sure we exit gracefully rather than endlessly redo
136 switch (handle_mm_fault(mm, vma, address, write)) {
143 case VM_FAULT_SIGBUS:
151 up_read(&mm->mmap_sem);
155 * Something tried to access memory that isn't in our memory map..
156 * Fix it, but check if it's kernel or user first..
159 up_read(&mm->mmap_sem);
161 /* User mode accesses just cause a SIGSEGV */
162 if (user_mode(regs)) {
163 tsk->thread.cp0_badvaddr = address;
164 tsk->thread.error_code = write;
166 printk("do_page_fault() #2: sending SIGSEGV to %s for illegal %s\n"
167 "%08lx (epc == %08lx, ra == %08lx)\n",
169 write ? "write access to" : "read access from",
171 (unsigned long) regs->cp0_epc,
172 (unsigned long) regs->regs[31]);
174 info.si_signo = SIGSEGV;
176 /* info.si_code has been set above */
177 info.si_addr = (void *) address;
178 force_sig_info(SIGSEGV, &info, tsk);
183 /* Are we prepared to handle this kernel fault? */
184 fixup = search_exception_tables(exception_epc(regs));
186 unsigned long new_epc = fixup->nextinsn;
188 tsk->thread.cp0_baduaddr = address;
189 if (development_version)
190 printk(KERN_DEBUG "%s: Exception at [<%lx>] (%lx)\n",
191 tsk->comm, regs->cp0_epc, new_epc);
192 regs->cp0_epc = new_epc;
197 * Oops. The kernel tried to access some bad page. We'll have to
198 * terminate things with extreme prejudice.
203 printk(KERN_ALERT "Cpu %d Unable to handle kernel paging request at "
204 "address %016lx, epc == %016lx, ra == %016lx\n",
205 smp_processor_id(), address, regs->cp0_epc, regs->regs[31]);
209 * We ran out of memory, or some other thing happened to us that made
210 * us unable to handle the page fault gracefully.
213 up_read(&mm->mmap_sem);
216 down_read(&mm->mmap_sem);
219 printk("VM: killing process %s\n", tsk->comm);
225 up_read(&mm->mmap_sem);
228 * Send a sigbus, regardless of whether we were in kernel
231 tsk->thread.cp0_badvaddr = address;
232 info.si_signo = SIGBUS;
234 info.si_code = BUS_ADRERR;
235 info.si_addr = (void *) address;
236 force_sig_info(SIGBUS, &info, tsk);
238 /* Kernel mode? Handle exceptions or die */
239 if (!user_mode(regs))
245 panic("Pagefault for kernel virtual memory");