5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7 * Derived from "arch/i386/mm/fault.c"
8 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
10 * Modified by Cort Dougan and Paul Mackerras.
12 * Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
20 #include <linux/config.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
27 #include <linux/mman.h>
29 #include <linux/interrupt.h>
30 #include <linux/smp_lock.h>
31 #include <linux/module.h>
34 #include <asm/pgtable.h>
36 #include <asm/mmu_context.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
40 #include <asm/ppcdebug.h>
42 #ifdef CONFIG_DEBUG_KERNEL
43 int debugger_kernel_faults = 1;
46 void bad_page_fault(struct pt_regs *, unsigned long, int);
49 * For 600- and 800-family processors, the error_code parameter is DSISR
50 * for a data fault, SRR1 for an instruction fault.
52 void do_page_fault(struct pt_regs *regs, unsigned long address,
53 unsigned long error_code)
55 struct vm_area_struct * vma;
56 struct mm_struct *mm = current->mm;
58 unsigned long code = SEGV_MAPERR;
59 unsigned long is_write = error_code & 0x02000000;
62 * Fortunately the bit assignments in SRR1 for an instruction
63 * fault and DSISR for a data fault are mostly the same for the
64 * bits we are interested in. But there are some bits which
65 * indicate errors in DSISR but can validly be set in SRR1.
67 if (regs->trap == 0x400)
68 error_code &= 0x48200000;
69 else if (regs->trap != 0x300) /* ensure error_code is 0 on SLB miss */
72 #ifdef CONFIG_DEBUG_KERNEL
73 if (debugger_fault_handler && (regs->trap == 0x300 ||
74 regs->trap == 0x380)) {
75 debugger_fault_handler(regs);
80 /* On a kernel SLB miss we can only check for a valid exception entry */
81 if (!user_mode(regs) && (regs->trap == 0x380)) {
82 bad_page_fault(regs, address, SIGSEGV);
86 #ifdef CONFIG_DEBUG_KERNEL
87 if (error_code & 0x00400000) {
89 if (debugger_dabr_match(regs))
94 if (in_atomic() || mm == NULL) {
95 bad_page_fault(regs, address, SIGSEGV);
98 down_read(&mm->mmap_sem);
99 vma = find_vma(mm, address);
103 if (vma->vm_start <= address) {
106 if (!(vma->vm_flags & VM_GROWSDOWN))
108 if (expand_stack(vma, address))
116 if (!(vma->vm_flags & VM_WRITE))
120 /* protection fault */
121 if (error_code & 0x08000000)
123 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
129 * If for any reason at all we couldn't handle the fault,
130 * make sure we exit gracefully rather than endlessly redo
133 switch (handle_mm_fault(mm, vma, address, is_write)) {
141 case VM_FAULT_SIGBUS:
149 up_read(&mm->mmap_sem);
153 up_read(&mm->mmap_sem);
155 /* User mode accesses cause a SIGSEGV */
156 if (user_mode(regs)) {
157 info.si_signo = SIGSEGV;
160 info.si_addr = (void *) address;
162 ifppcdebug(PPCDBG_SIGNALXMON)
163 PPCDBG_ENTER_DEBUGGER_REGS(regs);
166 force_sig_info(SIGSEGV, &info, current);
170 bad_page_fault(regs, address, SIGSEGV);
174 * We ran out of memory, or some other thing happened to us that made
175 * us unable to handle the page fault gracefully.
178 up_read(&mm->mmap_sem);
179 if (current->pid == 1) {
181 down_read(&mm->mmap_sem);
184 printk("VM: killing process %s\n", current->comm);
187 bad_page_fault(regs, address, SIGKILL);
191 up_read(&mm->mmap_sem);
192 info.si_signo = SIGBUS;
194 info.si_code = BUS_ADRERR;
195 info.si_addr = (void *)address;
196 force_sig_info (SIGBUS, &info, current);
197 if (!user_mode(regs))
198 bad_page_fault(regs, address, SIGBUS);
202 * bad_page_fault is called when we have a bad access from the kernel.
203 * It is called from do_page_fault above and from some of the procedures
207 bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
209 extern void die(const char *, struct pt_regs *, long);
210 const struct exception_table_entry *entry;
212 /* Are we prepared to handle this fault? */
213 if ((entry = search_exception_tables(regs->nip)) != NULL) {
214 regs->nip = entry->fixup;
218 /* kernel has accessed a bad area */
219 #ifdef CONFIG_DEBUG_KERNEL
220 if (debugger_kernel_faults)
223 die("Kernel access of bad area", regs, sig);