Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[pandora-kernel.git] / arch / sparc64 / mm / fault.c
1 /* $Id: fault.c,v 1.59 2002/02/09 19:49:31 davem Exp $
2  * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
3  *
4  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5  * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
6  */
7
8 #include <asm/head.h>
9
10 #include <linux/string.h>
11 #include <linux/types.h>
12 #include <linux/sched.h>
13 #include <linux/ptrace.h>
14 #include <linux/mman.h>
15 #include <linux/signal.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/smp_lock.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/kprobes.h>
22
23 #include <asm/page.h>
24 #include <asm/pgtable.h>
25 #include <asm/openprom.h>
26 #include <asm/oplib.h>
27 #include <asm/uaccess.h>
28 #include <asm/asi.h>
29 #include <asm/lsu.h>
30 #include <asm/sections.h>
31 #include <asm/kdebug.h>
32 #include <asm/mmu_context.h>
33
34 #ifdef CONFIG_KPROBES
35 ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
36
37 /* Hook to register for page fault notifications */
38 int register_page_fault_notifier(struct notifier_block *nb)
39 {
40         return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
41 }
42
43 int unregister_page_fault_notifier(struct notifier_block *nb)
44 {
45         return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
46 }
47
48 static inline int notify_page_fault(enum die_val val, const char *str,
49                         struct pt_regs *regs, long err, int trap, int sig)
50 {
51         struct die_args args = {
52                 .regs = regs,
53                 .str = str,
54                 .err = err,
55                 .trapnr = trap,
56                 .signr = sig
57         };
58         return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
59 }
60 #else
61 static inline int notify_page_fault(enum die_val val, const char *str,
62                         struct pt_regs *regs, long err, int trap, int sig)
63 {
64         return NOTIFY_DONE;
65 }
66 #endif
67
68 /*
69  * To debug kernel to catch accesses to certain virtual/physical addresses.
70  * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
71  * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
72  * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
73  * watched. This is only useful on a single cpu machine for now. After the watchpoint
74  * is detected, the process causing it will be killed, thus preventing an infinite loop.
75  */
76 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
77 {
78         unsigned long lsubits;
79
80         __asm__ __volatile__("ldxa [%%g0] %1, %0"
81                              : "=r" (lsubits)
82                              : "i" (ASI_LSU_CONTROL));
83         lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
84                      LSU_CONTROL_PR | LSU_CONTROL_VR |
85                      LSU_CONTROL_PW | LSU_CONTROL_VW);
86
87         __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
88                              "membar    #Sync"
89                              : /* no outputs */
90                              : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
91                                "i" (ASI_DMMU));
92
93         lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
94         if (flags & VM_READ)
95                 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
96         if (flags & VM_WRITE)
97                 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
98         __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
99                              "membar #Sync"
100                              : /* no outputs */
101                              : "r" (lsubits), "i" (ASI_LSU_CONTROL)
102                              : "memory");
103 }
104
105 static void __kprobes unhandled_fault(unsigned long address,
106                                       struct task_struct *tsk,
107                                       struct pt_regs *regs)
108 {
109         if ((unsigned long) address < PAGE_SIZE) {
110                 printk(KERN_ALERT "Unable to handle kernel NULL "
111                        "pointer dereference\n");
112         } else {
113                 printk(KERN_ALERT "Unable to handle kernel paging request "
114                        "at virtual address %016lx\n", (unsigned long)address);
115         }
116         printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
117                (tsk->mm ?
118                 CTX_HWBITS(tsk->mm->context) :
119                 CTX_HWBITS(tsk->active_mm->context)));
120         printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
121                (tsk->mm ? (unsigned long) tsk->mm->pgd :
122                           (unsigned long) tsk->active_mm->pgd));
123         if (notify_die(DIE_GPF, "general protection fault", regs,
124                        0, 0, SIGSEGV) == NOTIFY_STOP)
125                 return;
126         die_if_kernel("Oops", regs);
127 }
128
129 static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
130 {
131         unsigned long *ksp;
132
133         printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
134                regs->tpc);
135         printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
136         __asm__("mov %%sp, %0" : "=r" (ksp));
137         show_stack(current, ksp);
138         unhandled_fault(regs->tpc, current, regs);
139 }
140
141 /*
142  * We now make sure that mmap_sem is held in all paths that call 
143  * this. Additionally, to prevent kswapd from ripping ptes from
144  * under us, raise interrupts around the time that we look at the
145  * pte, kswapd will have to wait to get his smp ipi response from
146  * us. vmtruncate likewise. This saves us having to get pte lock.
147  */
148 static unsigned int get_user_insn(unsigned long tpc)
149 {
150         pgd_t *pgdp = pgd_offset(current->mm, tpc);
151         pud_t *pudp;
152         pmd_t *pmdp;
153         pte_t *ptep, pte;
154         unsigned long pa;
155         u32 insn = 0;
156         unsigned long pstate;
157
158         if (pgd_none(*pgdp))
159                 goto outret;
160         pudp = pud_offset(pgdp, tpc);
161         if (pud_none(*pudp))
162                 goto outret;
163         pmdp = pmd_offset(pudp, tpc);
164         if (pmd_none(*pmdp))
165                 goto outret;
166
167         /* This disables preemption for us as well. */
168         __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
169         __asm__ __volatile__("wrpr %0, %1, %%pstate"
170                                 : : "r" (pstate), "i" (PSTATE_IE));
171         ptep = pte_offset_map(pmdp, tpc);
172         pte = *ptep;
173         if (!pte_present(pte))
174                 goto out;
175
176         pa  = (pte_pfn(pte) << PAGE_SHIFT);
177         pa += (tpc & ~PAGE_MASK);
178
179         /* Use phys bypass so we don't pollute dtlb/dcache. */
180         __asm__ __volatile__("lduwa [%1] %2, %0"
181                              : "=r" (insn)
182                              : "r" (pa), "i" (ASI_PHYS_USE_EC));
183
184 out:
185         pte_unmap(ptep);
186         __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
187 outret:
188         return insn;
189 }
190
191 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
192
193 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
194                              unsigned int insn, int fault_code)
195 {
196         siginfo_t info;
197
198         info.si_code = code;
199         info.si_signo = sig;
200         info.si_errno = 0;
201         if (fault_code & FAULT_CODE_ITLB)
202                 info.si_addr = (void __user *) regs->tpc;
203         else
204                 info.si_addr = (void __user *)
205                         compute_effective_address(regs, insn, 0);
206         info.si_trapno = 0;
207         force_sig_info(sig, &info, current);
208 }
209
210 extern int handle_ldf_stq(u32, struct pt_regs *);
211 extern int handle_ld_nf(u32, struct pt_regs *);
212
213 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
214 {
215         if (!insn) {
216                 if (!regs->tpc || (regs->tpc & 0x3))
217                         return 0;
218                 if (regs->tstate & TSTATE_PRIV) {
219                         insn = *(unsigned int *) regs->tpc;
220                 } else {
221                         insn = get_user_insn(regs->tpc);
222                 }
223         }
224         return insn;
225 }
226
227 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
228                             unsigned int insn, unsigned long address)
229 {
230         unsigned char asi = ASI_P;
231  
232         if ((!insn) && (regs->tstate & TSTATE_PRIV))
233                 goto cannot_handle;
234
235         /* If user insn could be read (thus insn is zero), that
236          * is fine.  We will just gun down the process with a signal
237          * in that case.
238          */
239
240         if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
241             (insn & 0xc0800000) == 0xc0800000) {
242                 if (insn & 0x2000)
243                         asi = (regs->tstate >> 24);
244                 else
245                         asi = (insn >> 5);
246                 if ((asi & 0xf2) == 0x82) {
247                         if (insn & 0x1000000) {
248                                 handle_ldf_stq(insn, regs);
249                         } else {
250                                 /* This was a non-faulting load. Just clear the
251                                  * destination register(s) and continue with the next
252                                  * instruction. -jj
253                                  */
254                                 handle_ld_nf(insn, regs);
255                         }
256                         return;
257                 }
258         }
259                 
260         /* Is this in ex_table? */
261         if (regs->tstate & TSTATE_PRIV) {
262                 const struct exception_table_entry *entry;
263
264                 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
265                         if (insn & 0x2000)
266                                 asi = (regs->tstate >> 24);
267                         else
268                                 asi = (insn >> 5);
269                 }
270         
271                 /* Look in asi.h: All _S asis have LS bit set */
272                 if ((asi & 0x1) &&
273                     (entry = search_exception_tables(regs->tpc))) {
274                         regs->tpc = entry->fixup;
275                         regs->tnpc = regs->tpc + 4;
276                         return;
277                 }
278         } else {
279                 /* The si_code was set to make clear whether
280                  * this was a SEGV_MAPERR or SEGV_ACCERR fault.
281                  */
282                 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
283                 return;
284         }
285
286 cannot_handle:
287         unhandled_fault (address, current, regs);
288 }
289
290 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
291 {
292         struct mm_struct *mm = current->mm;
293         struct vm_area_struct *vma;
294         unsigned int insn = 0;
295         int si_code, fault_code;
296         unsigned long address, mm_rss;
297
298         fault_code = get_thread_fault_code();
299
300         if (notify_page_fault(DIE_PAGE_FAULT, "page_fault", regs,
301                        fault_code, 0, SIGSEGV) == NOTIFY_STOP)
302                 return;
303
304         si_code = SEGV_MAPERR;
305         address = current_thread_info()->fault_address;
306
307         if ((fault_code & FAULT_CODE_ITLB) &&
308             (fault_code & FAULT_CODE_DTLB))
309                 BUG();
310
311         if (regs->tstate & TSTATE_PRIV) {
312                 unsigned long tpc = regs->tpc;
313
314                 /* Sanity check the PC. */
315                 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
316                     (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
317                         /* Valid, no problems... */
318                 } else {
319                         bad_kernel_pc(regs, address);
320                         return;
321                 }
322         }
323
324         /*
325          * If we're in an interrupt or have no user
326          * context, we must not take the fault..
327          */
328         if (in_atomic() || !mm)
329                 goto intr_or_no_mm;
330
331         if (test_thread_flag(TIF_32BIT)) {
332                 if (!(regs->tstate & TSTATE_PRIV))
333                         regs->tpc &= 0xffffffff;
334                 address &= 0xffffffff;
335         }
336
337         if (!down_read_trylock(&mm->mmap_sem)) {
338                 if ((regs->tstate & TSTATE_PRIV) &&
339                     !search_exception_tables(regs->tpc)) {
340                         insn = get_fault_insn(regs, insn);
341                         goto handle_kernel_fault;
342                 }
343                 down_read(&mm->mmap_sem);
344         }
345
346         vma = find_vma(mm, address);
347         if (!vma)
348                 goto bad_area;
349
350         /* Pure DTLB misses do not tell us whether the fault causing
351          * load/store/atomic was a write or not, it only says that there
352          * was no match.  So in such a case we (carefully) read the
353          * instruction to try and figure this out.  It's an optimization
354          * so it's ok if we can't do this.
355          *
356          * Special hack, window spill/fill knows the exact fault type.
357          */
358         if (((fault_code &
359               (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
360             (vma->vm_flags & VM_WRITE) != 0) {
361                 insn = get_fault_insn(regs, 0);
362                 if (!insn)
363                         goto continue_fault;
364                 /* All loads, stores and atomics have bits 30 and 31 both set
365                  * in the instruction.  Bit 21 is set in all stores, but we
366                  * have to avoid prefetches which also have bit 21 set.
367                  */
368                 if ((insn & 0xc0200000) == 0xc0200000 &&
369                     (insn & 0x01780000) != 0x01680000) {
370                         /* Don't bother updating thread struct value,
371                          * because update_mmu_cache only cares which tlb
372                          * the access came from.
373                          */
374                         fault_code |= FAULT_CODE_WRITE;
375                 }
376         }
377 continue_fault:
378
379         if (vma->vm_start <= address)
380                 goto good_area;
381         if (!(vma->vm_flags & VM_GROWSDOWN))
382                 goto bad_area;
383         if (!(fault_code & FAULT_CODE_WRITE)) {
384                 /* Non-faulting loads shouldn't expand stack. */
385                 insn = get_fault_insn(regs, insn);
386                 if ((insn & 0xc0800000) == 0xc0800000) {
387                         unsigned char asi;
388
389                         if (insn & 0x2000)
390                                 asi = (regs->tstate >> 24);
391                         else
392                                 asi = (insn >> 5);
393                         if ((asi & 0xf2) == 0x82)
394                                 goto bad_area;
395                 }
396         }
397         if (expand_stack(vma, address))
398                 goto bad_area;
399         /*
400          * Ok, we have a good vm_area for this memory access, so
401          * we can handle it..
402          */
403 good_area:
404         si_code = SEGV_ACCERR;
405
406         /* If we took a ITLB miss on a non-executable page, catch
407          * that here.
408          */
409         if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
410                 BUG_ON(address != regs->tpc);
411                 BUG_ON(regs->tstate & TSTATE_PRIV);
412                 goto bad_area;
413         }
414
415         if (fault_code & FAULT_CODE_WRITE) {
416                 if (!(vma->vm_flags & VM_WRITE))
417                         goto bad_area;
418
419                 /* Spitfire has an icache which does not snoop
420                  * processor stores.  Later processors do...
421                  */
422                 if (tlb_type == spitfire &&
423                     (vma->vm_flags & VM_EXEC) != 0 &&
424                     vma->vm_file != NULL)
425                         set_thread_fault_code(fault_code |
426                                               FAULT_CODE_BLKCOMMIT);
427         } else {
428                 /* Allow reads even for write-only mappings */
429                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
430                         goto bad_area;
431         }
432
433         switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
434         case VM_FAULT_MINOR:
435                 current->min_flt++;
436                 break;
437         case VM_FAULT_MAJOR:
438                 current->maj_flt++;
439                 break;
440         case VM_FAULT_SIGBUS:
441                 goto do_sigbus;
442         case VM_FAULT_OOM:
443                 goto out_of_memory;
444         default:
445                 BUG();
446         }
447
448         up_read(&mm->mmap_sem);
449
450         mm_rss = get_mm_rss(mm);
451 #ifdef CONFIG_HUGETLB_PAGE
452         mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
453 #endif
454         if (unlikely(mm_rss >
455                      mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
456                 tsb_grow(mm, MM_TSB_BASE, mm_rss);
457 #ifdef CONFIG_HUGETLB_PAGE
458         mm_rss = mm->context.huge_pte_count;
459         if (unlikely(mm_rss >
460                      mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
461                 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
462 #endif
463         return;
464
465         /*
466          * Something tried to access memory that isn't in our memory map..
467          * Fix it, but check if it's kernel or user first..
468          */
469 bad_area:
470         insn = get_fault_insn(regs, insn);
471         up_read(&mm->mmap_sem);
472
473 handle_kernel_fault:
474         do_kernel_fault(regs, si_code, fault_code, insn, address);
475         return;
476
477 /*
478  * We ran out of memory, or some other thing happened to us that made
479  * us unable to handle the page fault gracefully.
480  */
481 out_of_memory:
482         insn = get_fault_insn(regs, insn);
483         up_read(&mm->mmap_sem);
484         printk("VM: killing process %s\n", current->comm);
485         if (!(regs->tstate & TSTATE_PRIV))
486                 do_exit(SIGKILL);
487         goto handle_kernel_fault;
488
489 intr_or_no_mm:
490         insn = get_fault_insn(regs, 0);
491         goto handle_kernel_fault;
492
493 do_sigbus:
494         insn = get_fault_insn(regs, insn);
495         up_read(&mm->mmap_sem);
496
497         /*
498          * Send a sigbus, regardless of whether we were in kernel
499          * or user mode.
500          */
501         do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
502
503         /* Kernel mode? Handle exceptions or die */
504         if (regs->tstate & TSTATE_PRIV)
505                 goto handle_kernel_fault;
506 }