patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / arm / mm / fault.c
1 /*
2  *  linux/arch/arm/mm/fault.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Modifications for ARM processor (c) 1995-2004 Russell King
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/signal.h>
14 #include <linux/ptrace.h>
15 #include <linux/mm.h>
16 #include <linux/init.h>
17
18 #include <asm/system.h>
19 #include <asm/pgtable.h>
20 #include <asm/tlbflush.h>
21 #include <asm/uaccess.h>
22
23 #include "fault.h"
24
25 /*
26  * This is useful to dump out the page tables associated with
27  * 'addr' in mm 'mm'.
28  */
29 void show_pte(struct mm_struct *mm, unsigned long addr)
30 {
31         pgd_t *pgd;
32
33         if (!mm)
34                 mm = &init_mm;
35
36         printk(KERN_ALERT "pgd = %p\n", mm->pgd);
37         pgd = pgd_offset(mm, addr);
38         printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
39
40         do {
41                 pmd_t *pmd;
42                 pte_t *pte;
43
44                 if (pgd_none(*pgd))
45                         break;
46
47                 if (pgd_bad(*pgd)) {
48                         printk("(bad)");
49                         break;
50                 }
51
52                 pmd = pmd_offset(pgd, addr);
53 #if PTRS_PER_PMD != 1
54                 printk(", *pmd=%08lx", pmd_val(*pmd));
55 #endif
56
57                 if (pmd_none(*pmd))
58                         break;
59
60                 if (pmd_bad(*pmd)) {
61                         printk("(bad)");
62                         break;
63                 }
64
65 #ifndef CONFIG_HIGHMEM
66                 /* We must not map this if we have highmem enabled */
67                 pte = pte_offset_map(pmd, addr);
68                 printk(", *pte=%08lx", pte_val(*pte));
69 #ifdef CONFIG_CPU_32
70                 printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
71 #endif
72                 pte_unmap(pte);
73 #endif
74         } while(0);
75
76         printk("\n");
77 }
78
79 /*
80  * Oops.  The kernel tried to access some page that wasn't present.
81  */
82 static void
83 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
84                   struct pt_regs *regs)
85 {
86         /*
87          * Are we prepared to handle this kernel fault?
88          */
89         if (fixup_exception(regs))
90                 return;
91
92         /*
93          * No handler, we'll have to terminate things with extreme prejudice.
94          */
95         bust_spinlocks(1);
96         printk(KERN_ALERT
97                 "Unable to handle kernel %s at virtual address %08lx\n",
98                 (addr < PAGE_SIZE) ? "NULL pointer dereference" :
99                 "paging request", addr);
100
101         show_pte(mm, addr);
102         die("Oops", regs, fsr);
103         bust_spinlocks(0);
104         do_exit(SIGKILL);
105 }
106
107 /*
108  * Something tried to access memory that isn't in our memory map..
109  * User mode accesses just cause a SIGSEGV
110  */
111 static void
112 __do_user_fault(struct task_struct *tsk, unsigned long addr,
113                 unsigned int fsr, int code, struct pt_regs *regs)
114 {
115         struct siginfo si;
116
117 #ifdef CONFIG_DEBUG_USER
118         if (user_debug & UDBG_SEGV) {
119                 printk(KERN_DEBUG "%s: unhandled page fault at 0x%08lx, code 0x%03x\n",
120                        tsk->comm, addr, fsr);
121                 show_pte(tsk->mm, addr);
122                 show_regs(regs);
123         }
124 #endif
125
126         tsk->thread.address = addr;
127         tsk->thread.error_code = fsr;
128         tsk->thread.trap_no = 14;
129         si.si_signo = SIGSEGV;
130         si.si_errno = 0;
131         si.si_code = code;
132         si.si_addr = (void *)addr;
133         force_sig_info(SIGSEGV, &si, tsk);
134 }
135
136 void
137 do_bad_area(struct task_struct *tsk, struct mm_struct *mm, unsigned long addr,
138             unsigned int fsr, struct pt_regs *regs)
139 {
140         /*
141          * If we are in kernel mode at this point, we
142          * have no context to handle this fault with.
143          */
144         if (user_mode(regs))
145                 __do_user_fault(tsk, addr, fsr, SEGV_MAPERR, regs);
146         else
147                 __do_kernel_fault(mm, addr, fsr, regs);
148 }
149
150 #define VM_FAULT_BADMAP         (-20)
151 #define VM_FAULT_BADACCESS      (-21)
152
153 static int
154 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
155                 struct task_struct *tsk)
156 {
157         struct vm_area_struct *vma;
158         int fault, mask;
159
160         vma = find_vma(mm, addr);
161         fault = VM_FAULT_BADMAP;
162         if (!vma)
163                 goto out;
164         if (vma->vm_start > addr)
165                 goto check_stack;
166
167         /*
168          * Ok, we have a good vm_area for this
169          * memory access, so we can handle it.
170          */
171 good_area:
172         if (fsr & (1 << 11)) /* write? */
173                 mask = VM_WRITE;
174         else
175                 mask = VM_READ|VM_EXEC;
176
177         fault = VM_FAULT_BADACCESS;
178         if (!(vma->vm_flags & mask))
179                 goto out;
180
181         /*
182          * If for any reason at all we couldn't handle
183          * the fault, make sure we exit gracefully rather
184          * than endlessly redo the fault.
185          */
186 survive:
187         fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, fsr & (1 << 11));
188
189         /*
190          * Handle the "normal" cases first - successful and sigbus
191          */
192         switch (fault) {
193         case VM_FAULT_MAJOR:
194                 tsk->maj_flt++;
195                 return fault;
196         case VM_FAULT_MINOR:
197                 tsk->min_flt++;
198         case VM_FAULT_SIGBUS:
199                 return fault;
200         }
201
202         if (tsk->pid != 1)
203                 goto out;
204
205         /*
206          * If we are out of memory for pid1,
207          * sleep for a while and retry
208          */
209         yield();
210         goto survive;
211
212 check_stack:
213         if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
214                 goto good_area;
215 out:
216         return fault;
217 }
218
219 static int
220 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
221 {
222         struct task_struct *tsk;
223         struct mm_struct *mm;
224         int fault;
225
226         tsk = current;
227         mm  = tsk->mm;
228
229         /*
230          * If we're in an interrupt or have no user
231          * context, we must not take the fault..
232          */
233         if (in_interrupt() || !mm)
234                 goto no_context;
235
236         down_read(&mm->mmap_sem);
237         fault = __do_page_fault(mm, addr, fsr, tsk);
238         up_read(&mm->mmap_sem);
239
240         /*
241          * Handle the "normal" case first
242          */
243         if (fault > 0)
244                 return 0;
245
246         /*
247          * We had some memory, but were unable to
248          * successfully fix up this page fault.
249          */
250         if (fault == 0)
251                 goto do_sigbus;
252
253         /*
254          * If we are in kernel mode at this point, we
255          * have no context to handle this fault with.
256          */
257         if (!user_mode(regs))
258                 goto no_context;
259
260         if (fault == VM_FAULT_OOM) {
261                 /*
262                  * We ran out of memory, or some other thing happened to
263                  * us that made us unable to handle the page fault gracefully.
264                  */
265                 printk("VM: killing process %s\n", tsk->comm);
266                 do_exit(SIGKILL);
267         } else
268                 __do_user_fault(tsk, addr, fsr, fault == VM_FAULT_BADACCESS ?
269                                 SEGV_ACCERR : SEGV_MAPERR, regs);
270         return 0;
271
272
273 /*
274  * We ran out of memory, or some other thing happened to us that made
275  * us unable to handle the page fault gracefully.
276  */
277 do_sigbus:
278         /*
279          * Send a sigbus, regardless of whether we were in kernel
280          * or user mode.
281          */
282         tsk->thread.address = addr;
283         tsk->thread.error_code = fsr;
284         tsk->thread.trap_no = 14;
285         force_sig(SIGBUS, tsk);
286 #ifdef CONFIG_DEBUG_USER
287         if (user_debug & UDBG_BUS) {
288                 printk(KERN_DEBUG "%s: sigbus at 0x%08lx, pc=0x%08lx\n",
289                         current->comm, addr, instruction_pointer(regs));
290         }
291 #endif
292
293         /* Kernel mode? Handle exceptions or die */
294         if (user_mode(regs))
295                 return 0;
296
297 no_context:
298         __do_kernel_fault(mm, addr, fsr, regs);
299         return 0;
300 }
301
302 /*
303  * First Level Translation Fault Handler
304  *
305  * We enter here because the first level page table doesn't contain
306  * a valid entry for the address.
307  *
308  * If the address is in kernel space (>= TASK_SIZE), then we are
309  * probably faulting in the vmalloc() area.
310  *
311  * If the init_task's first level page tables contains the relevant
312  * entry, we copy the it to this task.  If not, we send the process
313  * a signal, fixup the exception, or oops the kernel.
314  *
315  * NOTE! We MUST NOT take any locks for this case. We may be in an
316  * interrupt or a critical region, and should only copy the information
317  * from the master page table, nothing more.
318  */
319 static int
320 do_translation_fault(unsigned long addr, unsigned int fsr,
321                      struct pt_regs *regs)
322 {
323         struct task_struct *tsk;
324         unsigned int index;
325         pgd_t *pgd, *pgd_k;
326         pmd_t *pmd, *pmd_k;
327
328         if (addr < TASK_SIZE)
329                 return do_page_fault(addr, fsr, regs);
330
331         index = pgd_index(addr);
332
333         /*
334          * FIXME: CP15 C1 is write only on ARMv3 architectures.
335          */
336         pgd = cpu_get_pgd() + index;
337         pgd_k = init_mm.pgd + index;
338
339         if (pgd_none(*pgd_k))
340                 goto bad_area;
341
342         if (!pgd_present(*pgd))
343                 set_pgd(pgd, *pgd_k);
344
345         pmd_k = pmd_offset(pgd_k, addr);
346         pmd   = pmd_offset(pgd, addr);
347
348         if (pmd_none(*pmd_k))
349                 goto bad_area;
350
351         copy_pmd(pmd, pmd_k);
352         return 0;
353
354 bad_area:
355         tsk = current;
356
357         do_bad_area(tsk, tsk->active_mm, addr, fsr, regs);
358         return 0;
359 }
360
361 /*
362  * Some section permission faults need to be handled gracefully.
363  * They can happen due to a __{get,put}_user during an oops.
364  */
365 static int
366 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
367 {
368         struct task_struct *tsk = current;
369         do_bad_area(tsk, tsk->active_mm, addr, fsr, regs);
370         return 0;
371 }
372
373 /*
374  * This abort handler always returns "fault".
375  */
376 static int
377 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
378 {
379         return 1;
380 }
381
382 static struct fsr_info {
383         int     (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
384         int     sig;
385         const char *name;
386 } fsr_info[] = {
387         /*
388          * The following are the standard ARMv3 and ARMv4 aborts.  ARMv5
389          * defines these to be "precise" aborts.
390          */
391         { do_bad,               SIGSEGV, "vector exception"                },
392         { do_bad,               SIGILL,  "alignment exception"             },
393         { do_bad,               SIGKILL, "terminal exception"              },
394         { do_bad,               SIGILL,  "alignment exception"             },
395         { do_bad,               SIGBUS,  "external abort on linefetch"     },
396         { do_translation_fault, SIGSEGV, "section translation fault"       },
397         { do_bad,               SIGBUS,  "external abort on linefetch"     },
398         { do_page_fault,        SIGSEGV, "page translation fault"          },
399         { do_bad,               SIGBUS,  "external abort on non-linefetch" },
400         { do_bad,               SIGSEGV, "section domain fault"            },
401         { do_bad,               SIGBUS,  "external abort on non-linefetch" },
402         { do_bad,               SIGSEGV, "page domain fault"               },
403         { do_bad,               SIGBUS,  "external abort on translation"   },
404         { do_sect_fault,        SIGSEGV, "section permission fault"        },
405         { do_bad,               SIGBUS,  "external abort on translation"   },
406         { do_page_fault,        SIGSEGV, "page permission fault"           },
407         /*
408          * The following are "imprecise" aborts, which are signalled by bit
409          * 10 of the FSR, and may not be recoverable.  These are only
410          * supported if the CPU abort handler supports bit 10.
411          */
412         { do_bad,               SIGBUS,  "unknown 16"                      },
413         { do_bad,               SIGBUS,  "unknown 17"                      },
414         { do_bad,               SIGBUS,  "unknown 18"                      },
415         { do_bad,               SIGBUS,  "unknown 19"                      },
416         { do_bad,               SIGBUS,  "lock abort"                      }, /* xscale */
417         { do_bad,               SIGBUS,  "unknown 21"                      },
418         { do_bad,               SIGBUS,  "imprecise external abort"        }, /* xscale */
419         { do_bad,               SIGBUS,  "unknown 23"                      },
420         { do_bad,               SIGBUS,  "dcache parity error"             }, /* xscale */
421         { do_bad,               SIGBUS,  "unknown 25"                      },
422         { do_bad,               SIGBUS,  "unknown 26"                      },
423         { do_bad,               SIGBUS,  "unknown 27"                      },
424         { do_bad,               SIGBUS,  "unknown 28"                      },
425         { do_bad,               SIGBUS,  "unknown 29"                      },
426         { do_bad,               SIGBUS,  "unknown 30"                      },
427         { do_bad,               SIGBUS,  "unknown 31"                      }
428 };
429
430 void __init
431 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
432                 int sig, const char *name)
433 {
434         if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
435                 fsr_info[nr].fn   = fn;
436                 fsr_info[nr].sig  = sig;
437                 fsr_info[nr].name = name;
438         }
439 }
440
441 /*
442  * Dispatch a data abort to the relevant handler.
443  */
444 asmlinkage void
445 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
446 {
447         const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
448
449         if (!inf->fn(addr, fsr, regs))
450                 return;
451
452         printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
453                 inf->name, fsr, addr);
454         force_sig(inf->sig, current);
455         show_pte(current->mm, addr);
456         die_if_kernel("Oops", regs, 0);
457 }
458
459 asmlinkage void
460 do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
461 {
462         do_translation_fault(addr, 0, regs);
463 }
464