commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / arch / ppc64 / mm / fault.c
1 /*
2  *  arch/ppc/mm/fault.c
3  *
4  *  PowerPC version 
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  *  Derived from "arch/i386/mm/fault.c"
8  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
9  *
10  *  Modified by Cort Dougan and Paul Mackerras.
11  *
12  *  Modified for PPC64 by Dave Engebretsen (engebret@ibm.com)
13  *
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.
18  */
19
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>
28 #include <linux/mm.h>
29 #include <linux/interrupt.h>
30 #include <linux/smp_lock.h>
31 #include <linux/module.h>
32
33 #include <asm/page.h>
34 #include <asm/pgtable.h>
35 #include <asm/mmu.h>
36 #include <asm/mmu_context.h>
37 #include <asm/system.h>
38 #include <asm/uaccess.h>
39
40 #include <asm/ppcdebug.h>
41
42 #ifdef CONFIG_DEBUG_KERNEL
43 int debugger_kernel_faults = 1;
44 #endif
45
46 void bad_page_fault(struct pt_regs *, unsigned long, int);
47
48 /*
49  * For 600- and 800-family processors, the error_code parameter is DSISR
50  * for a data fault, SRR1 for an instruction fault.
51  */
52 void do_page_fault(struct pt_regs *regs, unsigned long address,
53                    unsigned long error_code)
54 {
55         struct vm_area_struct * vma;
56         struct mm_struct *mm = current->mm;
57         siginfo_t info;
58         unsigned long code = SEGV_MAPERR;
59         unsigned long is_write = error_code & 0x02000000;
60
61         /*
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.
66          */
67         if (regs->trap == 0x400)
68                 error_code &= 0x48200000;
69         else if (regs->trap != 0x300) /* ensure error_code is 0 on SLB miss */
70                 error_code = 0;
71
72 #ifdef CONFIG_DEBUG_KERNEL
73         if (debugger_fault_handler && (regs->trap == 0x300 ||
74                                        regs->trap == 0x380)) {
75                 debugger_fault_handler(regs);
76                 return;
77         }
78 #endif
79
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);
83                 return;
84         }
85
86 #ifdef CONFIG_DEBUG_KERNEL
87         if (error_code & 0x00400000) {
88                 /* DABR match */
89                 if (debugger_dabr_match(regs))
90                         return;
91         }
92 #endif
93
94         if (in_atomic() || mm == NULL) {
95                 bad_page_fault(regs, address, SIGSEGV);
96                 return;
97         }
98         down_read(&mm->mmap_sem);
99         vma = find_vma(mm, address);
100         if (!vma)
101                 goto bad_area;
102
103         if (vma->vm_start <= address) {
104                 goto good_area;
105         }
106         if (!(vma->vm_flags & VM_GROWSDOWN))
107                 goto bad_area;
108         if (expand_stack(vma, address))
109                 goto bad_area;
110
111 good_area:
112         code = SEGV_ACCERR;
113
114         /* a write */
115         if (is_write) {
116                 if (!(vma->vm_flags & VM_WRITE))
117                         goto bad_area;
118         /* a read */
119         } else {
120                 /* protection fault */
121                 if (error_code & 0x08000000)
122                         goto bad_area;
123                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
124                         goto bad_area;
125         }
126
127  survive:
128         /*
129          * If for any reason at all we couldn't handle the fault,
130          * make sure we exit gracefully rather than endlessly redo
131          * the fault.
132          */
133         switch (handle_mm_fault(mm, vma, address, is_write)) {
134
135         case VM_FAULT_MINOR:
136                 current->min_flt++;
137                 break;
138         case VM_FAULT_MAJOR:
139                 current->maj_flt++;
140                 break;
141         case VM_FAULT_SIGBUS:
142                 goto do_sigbus;
143         case VM_FAULT_OOM:
144                 goto out_of_memory;
145         default:
146                 BUG();
147         }
148
149         up_read(&mm->mmap_sem);
150         return;
151
152 bad_area:
153         up_read(&mm->mmap_sem);
154
155         /* User mode accesses cause a SIGSEGV */
156         if (user_mode(regs)) {
157                 info.si_signo = SIGSEGV;
158                 info.si_errno = 0;
159                 info.si_code = code;
160                 info.si_addr = (void *) address;
161 #ifdef CONFIG_XMON
162                 ifppcdebug(PPCDBG_SIGNALXMON)
163                         PPCDBG_ENTER_DEBUGGER_REGS(regs);
164 #endif
165
166                 force_sig_info(SIGSEGV, &info, current);
167                 return;
168         }
169
170         bad_page_fault(regs, address, SIGSEGV);
171         return;
172
173 /*
174  * We ran out of memory, or some other thing happened to us that made
175  * us unable to handle the page fault gracefully.
176  */
177 out_of_memory:
178         up_read(&mm->mmap_sem);
179         if (current->pid == 1) {
180                 yield();
181                 down_read(&mm->mmap_sem);
182                 goto survive;
183         }
184         printk("VM: killing process %s\n", current->comm);
185         if (user_mode(regs))
186                 do_exit(SIGKILL);
187         bad_page_fault(regs, address, SIGKILL);
188         return;
189
190 do_sigbus:
191         up_read(&mm->mmap_sem);
192         info.si_signo = SIGBUS;
193         info.si_errno = 0;
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);
199 }
200
201 /*
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
204  * in traps.c.
205  */
206 void
207 bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
208 {
209         extern void die(const char *, struct pt_regs *, long);
210         const struct exception_table_entry *entry;
211
212         /* Are we prepared to handle this fault?  */
213         if ((entry = search_exception_tables(regs->nip)) != NULL) {
214                 regs->nip = entry->fixup;
215                 return;
216         }
217
218         /* kernel has accessed a bad area */
219 #ifdef CONFIG_DEBUG_KERNEL
220         if (debugger_kernel_faults)
221                 debugger(regs);
222 #endif
223         die("Kernel access of bad area", regs, sig);
224 }