Merge branch 'upstream' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik/netdev-2.6
[linux-2.6] / 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 #include <linux/kprobes.h>
33
34 #include <asm/page.h>
35 #include <asm/pgtable.h>
36 #include <asm/mmu.h>
37 #include <asm/mmu_context.h>
38 #include <asm/system.h>
39 #include <asm/uaccess.h>
40 #include <asm/kdebug.h>
41
42 /*
43  * Check whether the instruction at regs->nip is a store using
44  * an update addressing form which will update r1.
45  */
46 static int store_updates_sp(struct pt_regs *regs)
47 {
48         unsigned int inst;
49
50         if (get_user(inst, (unsigned int __user *)regs->nip))
51                 return 0;
52         /* check for 1 in the rA field */
53         if (((inst >> 16) & 0x1f) != 1)
54                 return 0;
55         /* check major opcode */
56         switch (inst >> 26) {
57         case 37:        /* stwu */
58         case 39:        /* stbu */
59         case 45:        /* sthu */
60         case 53:        /* stfsu */
61         case 55:        /* stfdu */
62                 return 1;
63         case 62:        /* std or stdu */
64                 return (inst & 3) == 1;
65         case 31:
66                 /* check minor opcode */
67                 switch ((inst >> 1) & 0x3ff) {
68                 case 181:       /* stdux */
69                 case 183:       /* stwux */
70                 case 247:       /* stbux */
71                 case 439:       /* sthux */
72                 case 695:       /* stfsux */
73                 case 759:       /* stfdux */
74                         return 1;
75                 }
76         }
77         return 0;
78 }
79
80 /*
81  * The error_code parameter is
82  *  - DSISR for a non-SLB data access fault,
83  *  - SRR1 & 0x08000000 for a non-SLB instruction access fault
84  *  - 0 any SLB fault.
85  * The return value is 0 if the fault was handled, or the signal
86  * number if this is a kernel fault that can't be handled here.
87  */
88 int __kprobes do_page_fault(struct pt_regs *regs, unsigned long address,
89                             unsigned long error_code)
90 {
91         struct vm_area_struct * vma;
92         struct mm_struct *mm = current->mm;
93         siginfo_t info;
94         unsigned long code = SEGV_MAPERR;
95         unsigned long is_write = error_code & DSISR_ISSTORE;
96         unsigned long trap = TRAP(regs);
97         unsigned long is_exec = trap == 0x400;
98
99         BUG_ON((trap == 0x380) || (trap == 0x480));
100
101         if (notify_die(DIE_PAGE_FAULT, "page_fault", regs, error_code,
102                                 11, SIGSEGV) == NOTIFY_STOP)
103                 return 0;
104
105         if (trap == 0x300) {
106                 if (debugger_fault_handler(regs))
107                         return 0;
108         }
109
110         /* On a kernel SLB miss we can only check for a valid exception entry */
111         if (!user_mode(regs) && (address >= TASK_SIZE))
112                 return SIGSEGV;
113
114         if (error_code & DSISR_DABRMATCH) {
115                 if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
116                                         11, SIGSEGV) == NOTIFY_STOP)
117                         return 0;
118                 if (debugger_dabr_match(regs))
119                         return 0;
120         }
121
122         if (in_atomic() || mm == NULL) {
123                 if (!user_mode(regs))
124                         return SIGSEGV;
125                 /* in_atomic() in user mode is really bad,
126                    as is current->mm == NULL. */
127                 printk(KERN_EMERG "Page fault in user mode with"
128                        "in_atomic() = %d mm = %p\n", in_atomic(), mm);
129                 printk(KERN_EMERG "NIP = %lx  MSR = %lx\n",
130                        regs->nip, regs->msr);
131                 die("Weird page fault", regs, SIGSEGV);
132         }
133
134         /* When running in the kernel we expect faults to occur only to
135          * addresses in user space.  All other faults represent errors in the
136          * kernel and should generate an OOPS.  Unfortunatly, in the case of an
137          * erroneous fault occuring in a code path which already holds mmap_sem
138          * we will deadlock attempting to validate the fault against the
139          * address space.  Luckily the kernel only validly references user
140          * space from well defined areas of code, which are listed in the
141          * exceptions table.
142          *
143          * As the vast majority of faults will be valid we will only perform
144          * the source reference check when there is a possibilty of a deadlock.
145          * Attempt to lock the address space, if we cannot we then validate the
146          * source.  If this is invalid we can skip the address space check,
147          * thus avoiding the deadlock.
148          */
149         if (!down_read_trylock(&mm->mmap_sem)) {
150                 if (!user_mode(regs) && !search_exception_tables(regs->nip))
151                         goto bad_area_nosemaphore;
152
153                 down_read(&mm->mmap_sem);
154         }
155
156         vma = find_vma(mm, address);
157         if (!vma)
158                 goto bad_area;
159
160         if (vma->vm_start <= address) {
161                 goto good_area;
162         }
163         if (!(vma->vm_flags & VM_GROWSDOWN))
164                 goto bad_area;
165
166         /*
167          * N.B. The POWER/Open ABI allows programs to access up to
168          * 288 bytes below the stack pointer.
169          * The kernel signal delivery code writes up to about 1.5kB
170          * below the stack pointer (r1) before decrementing it.
171          * The exec code can write slightly over 640kB to the stack
172          * before setting the user r1.  Thus we allow the stack to
173          * expand to 1MB without further checks.
174          */
175         if (address + 0x100000 < vma->vm_end) {
176                 /* get user regs even if this fault is in kernel mode */
177                 struct pt_regs *uregs = current->thread.regs;
178                 if (uregs == NULL)
179                         goto bad_area;
180
181                 /*
182                  * A user-mode access to an address a long way below
183                  * the stack pointer is only valid if the instruction
184                  * is one which would update the stack pointer to the
185                  * address accessed if the instruction completed,
186                  * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
187                  * (or the byte, halfword, float or double forms).
188                  *
189                  * If we don't check this then any write to the area
190                  * between the last mapped region and the stack will
191                  * expand the stack rather than segfaulting.
192                  */
193                 if (address + 2048 < uregs->gpr[1]
194                     && (!user_mode(regs) || !store_updates_sp(regs)))
195                         goto bad_area;
196         }
197
198         if (expand_stack(vma, address))
199                 goto bad_area;
200
201 good_area:
202         code = SEGV_ACCERR;
203
204         if (is_exec) {
205                 /* protection fault */
206                 if (error_code & DSISR_PROTFAULT)
207                         goto bad_area;
208                 if (!(vma->vm_flags & VM_EXEC))
209                         goto bad_area;
210         /* a write */
211         } else if (is_write) {
212                 if (!(vma->vm_flags & VM_WRITE))
213                         goto bad_area;
214         /* a read */
215         } else {
216                 if (!(vma->vm_flags & VM_READ))
217                         goto bad_area;
218         }
219
220  survive:
221         /*
222          * If for any reason at all we couldn't handle the fault,
223          * make sure we exit gracefully rather than endlessly redo
224          * the fault.
225          */
226         switch (handle_mm_fault(mm, vma, address, is_write)) {
227
228         case VM_FAULT_MINOR:
229                 current->min_flt++;
230                 break;
231         case VM_FAULT_MAJOR:
232                 current->maj_flt++;
233                 break;
234         case VM_FAULT_SIGBUS:
235                 goto do_sigbus;
236         case VM_FAULT_OOM:
237                 goto out_of_memory;
238         default:
239                 BUG();
240         }
241
242         up_read(&mm->mmap_sem);
243         return 0;
244
245 bad_area:
246         up_read(&mm->mmap_sem);
247
248 bad_area_nosemaphore:
249         /* User mode accesses cause a SIGSEGV */
250         if (user_mode(regs)) {
251                 info.si_signo = SIGSEGV;
252                 info.si_errno = 0;
253                 info.si_code = code;
254                 info.si_addr = (void __user *) address;
255                 force_sig_info(SIGSEGV, &info, current);
256                 return 0;
257         }
258
259         if (trap == 0x400 && (error_code & DSISR_PROTFAULT)
260             && printk_ratelimit())
261                 printk(KERN_CRIT "kernel tried to execute NX-protected"
262                        " page (%lx) - exploit attempt? (uid: %d)\n",
263                        address, current->uid);
264
265         return SIGSEGV;
266
267 /*
268  * We ran out of memory, or some other thing happened to us that made
269  * us unable to handle the page fault gracefully.
270  */
271 out_of_memory:
272         up_read(&mm->mmap_sem);
273         if (current->pid == 1) {
274                 yield();
275                 down_read(&mm->mmap_sem);
276                 goto survive;
277         }
278         printk("VM: killing process %s\n", current->comm);
279         if (user_mode(regs))
280                 do_exit(SIGKILL);
281         return SIGKILL;
282
283 do_sigbus:
284         up_read(&mm->mmap_sem);
285         if (user_mode(regs)) {
286                 info.si_signo = SIGBUS;
287                 info.si_errno = 0;
288                 info.si_code = BUS_ADRERR;
289                 info.si_addr = (void __user *)address;
290                 force_sig_info(SIGBUS, &info, current);
291                 return 0;
292         }
293         return SIGBUS;
294 }
295
296 /*
297  * bad_page_fault is called when we have a bad access from the kernel.
298  * It is called from do_page_fault above and from some of the procedures
299  * in traps.c.
300  */
301 void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
302 {
303         const struct exception_table_entry *entry;
304
305         /* Are we prepared to handle this fault?  */
306         if ((entry = search_exception_tables(regs->nip)) != NULL) {
307                 regs->nip = entry->fixup;
308                 return;
309         }
310
311         /* kernel has accessed a bad area */
312         die("Kernel access of bad area", regs, sig);
313 }