Merge branch 'reset-seq' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik/libat...
[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/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/kprobes.h>
21 #include <linux/kallsyms.h>
22 #include <linux/kdebug.h>
23
24 #include <asm/page.h>
25 #include <asm/pgtable.h>
26 #include <asm/openprom.h>
27 #include <asm/oplib.h>
28 #include <asm/uaccess.h>
29 #include <asm/asi.h>
30 #include <asm/lsu.h>
31 #include <asm/sections.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: RPC [%016lx]\n", regs->u_regs[15]);
136         print_symbol("RPC: <%s>\n", regs->u_regs[15]);
137         printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
138         __asm__("mov %%sp, %0" : "=r" (ksp));
139         show_stack(current, ksp);
140         unhandled_fault(regs->tpc, current, regs);
141 }
142
143 /*
144  * We now make sure that mmap_sem is held in all paths that call 
145  * this. Additionally, to prevent kswapd from ripping ptes from
146  * under us, raise interrupts around the time that we look at the
147  * pte, kswapd will have to wait to get his smp ipi response from
148  * us. vmtruncate likewise. This saves us having to get pte lock.
149  */
150 static unsigned int get_user_insn(unsigned long tpc)
151 {
152         pgd_t *pgdp = pgd_offset(current->mm, tpc);
153         pud_t *pudp;
154         pmd_t *pmdp;
155         pte_t *ptep, pte;
156         unsigned long pa;
157         u32 insn = 0;
158         unsigned long pstate;
159
160         if (pgd_none(*pgdp))
161                 goto outret;
162         pudp = pud_offset(pgdp, tpc);
163         if (pud_none(*pudp))
164                 goto outret;
165         pmdp = pmd_offset(pudp, tpc);
166         if (pmd_none(*pmdp))
167                 goto outret;
168
169         /* This disables preemption for us as well. */
170         __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
171         __asm__ __volatile__("wrpr %0, %1, %%pstate"
172                                 : : "r" (pstate), "i" (PSTATE_IE));
173         ptep = pte_offset_map(pmdp, tpc);
174         pte = *ptep;
175         if (!pte_present(pte))
176                 goto out;
177
178         pa  = (pte_pfn(pte) << PAGE_SHIFT);
179         pa += (tpc & ~PAGE_MASK);
180
181         /* Use phys bypass so we don't pollute dtlb/dcache. */
182         __asm__ __volatile__("lduwa [%1] %2, %0"
183                              : "=r" (insn)
184                              : "r" (pa), "i" (ASI_PHYS_USE_EC));
185
186 out:
187         pte_unmap(ptep);
188         __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
189 outret:
190         return insn;
191 }
192
193 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
194
195 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
196                              unsigned int insn, int fault_code)
197 {
198         siginfo_t info;
199
200         info.si_code = code;
201         info.si_signo = sig;
202         info.si_errno = 0;
203         if (fault_code & FAULT_CODE_ITLB)
204                 info.si_addr = (void __user *) regs->tpc;
205         else
206                 info.si_addr = (void __user *)
207                         compute_effective_address(regs, insn, 0);
208         info.si_trapno = 0;
209         force_sig_info(sig, &info, current);
210 }
211
212 extern int handle_ldf_stq(u32, struct pt_regs *);
213 extern int handle_ld_nf(u32, struct pt_regs *);
214
215 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
216 {
217         if (!insn) {
218                 if (!regs->tpc || (regs->tpc & 0x3))
219                         return 0;
220                 if (regs->tstate & TSTATE_PRIV) {
221                         insn = *(unsigned int *) regs->tpc;
222                 } else {
223                         insn = get_user_insn(regs->tpc);
224                 }
225         }
226         return insn;
227 }
228
229 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
230                             unsigned int insn, unsigned long address)
231 {
232         unsigned char asi = ASI_P;
233  
234         if ((!insn) && (regs->tstate & TSTATE_PRIV))
235                 goto cannot_handle;
236
237         /* If user insn could be read (thus insn is zero), that
238          * is fine.  We will just gun down the process with a signal
239          * in that case.
240          */
241
242         if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
243             (insn & 0xc0800000) == 0xc0800000) {
244                 if (insn & 0x2000)
245                         asi = (regs->tstate >> 24);
246                 else
247                         asi = (insn >> 5);
248                 if ((asi & 0xf2) == 0x82) {
249                         if (insn & 0x1000000) {
250                                 handle_ldf_stq(insn, regs);
251                         } else {
252                                 /* This was a non-faulting load. Just clear the
253                                  * destination register(s) and continue with the next
254                                  * instruction. -jj
255                                  */
256                                 handle_ld_nf(insn, regs);
257                         }
258                         return;
259                 }
260         }
261                 
262         /* Is this in ex_table? */
263         if (regs->tstate & TSTATE_PRIV) {
264                 const struct exception_table_entry *entry;
265
266                 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
267                         if (insn & 0x2000)
268                                 asi = (regs->tstate >> 24);
269                         else
270                                 asi = (insn >> 5);
271                 }
272         
273                 /* Look in asi.h: All _S asis have LS bit set */
274                 if ((asi & 0x1) &&
275                     (entry = search_exception_tables(regs->tpc))) {
276                         regs->tpc = entry->fixup;
277                         regs->tnpc = regs->tpc + 4;
278                         return;
279                 }
280         } else {
281                 /* The si_code was set to make clear whether
282                  * this was a SEGV_MAPERR or SEGV_ACCERR fault.
283                  */
284                 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
285                 return;
286         }
287
288 cannot_handle:
289         unhandled_fault (address, current, regs);
290 }
291
292 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
293 {
294         struct mm_struct *mm = current->mm;
295         struct vm_area_struct *vma;
296         unsigned int insn = 0;
297         int si_code, fault_code;
298         unsigned long address, mm_rss;
299
300         fault_code = get_thread_fault_code();
301
302         if (notify_page_fault(DIE_PAGE_FAULT, "page_fault", regs,
303                        fault_code, 0, SIGSEGV) == NOTIFY_STOP)
304                 return;
305
306         si_code = SEGV_MAPERR;
307         address = current_thread_info()->fault_address;
308
309         if ((fault_code & FAULT_CODE_ITLB) &&
310             (fault_code & FAULT_CODE_DTLB))
311                 BUG();
312
313         if (regs->tstate & TSTATE_PRIV) {
314                 unsigned long tpc = regs->tpc;
315
316                 /* Sanity check the PC. */
317                 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
318                     (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
319                         /* Valid, no problems... */
320                 } else {
321                         bad_kernel_pc(regs, address);
322                         return;
323                 }
324         }
325
326         /*
327          * If we're in an interrupt or have no user
328          * context, we must not take the fault..
329          */
330         if (in_atomic() || !mm)
331                 goto intr_or_no_mm;
332
333         if (test_thread_flag(TIF_32BIT)) {
334                 if (!(regs->tstate & TSTATE_PRIV))
335                         regs->tpc &= 0xffffffff;
336                 address &= 0xffffffff;
337         }
338
339         if (!down_read_trylock(&mm->mmap_sem)) {
340                 if ((regs->tstate & TSTATE_PRIV) &&
341                     !search_exception_tables(regs->tpc)) {
342                         insn = get_fault_insn(regs, insn);
343                         goto handle_kernel_fault;
344                 }
345                 down_read(&mm->mmap_sem);
346         }
347
348         vma = find_vma(mm, address);
349         if (!vma)
350                 goto bad_area;
351
352         /* Pure DTLB misses do not tell us whether the fault causing
353          * load/store/atomic was a write or not, it only says that there
354          * was no match.  So in such a case we (carefully) read the
355          * instruction to try and figure this out.  It's an optimization
356          * so it's ok if we can't do this.
357          *
358          * Special hack, window spill/fill knows the exact fault type.
359          */
360         if (((fault_code &
361               (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
362             (vma->vm_flags & VM_WRITE) != 0) {
363                 insn = get_fault_insn(regs, 0);
364                 if (!insn)
365                         goto continue_fault;
366                 /* All loads, stores and atomics have bits 30 and 31 both set
367                  * in the instruction.  Bit 21 is set in all stores, but we
368                  * have to avoid prefetches which also have bit 21 set.
369                  */
370                 if ((insn & 0xc0200000) == 0xc0200000 &&
371                     (insn & 0x01780000) != 0x01680000) {
372                         /* Don't bother updating thread struct value,
373                          * because update_mmu_cache only cares which tlb
374                          * the access came from.
375                          */
376                         fault_code |= FAULT_CODE_WRITE;
377                 }
378         }
379 continue_fault:
380
381         if (vma->vm_start <= address)
382                 goto good_area;
383         if (!(vma->vm_flags & VM_GROWSDOWN))
384                 goto bad_area;
385         if (!(fault_code & FAULT_CODE_WRITE)) {
386                 /* Non-faulting loads shouldn't expand stack. */
387                 insn = get_fault_insn(regs, insn);
388                 if ((insn & 0xc0800000) == 0xc0800000) {
389                         unsigned char asi;
390
391                         if (insn & 0x2000)
392                                 asi = (regs->tstate >> 24);
393                         else
394                                 asi = (insn >> 5);
395                         if ((asi & 0xf2) == 0x82)
396                                 goto bad_area;
397                 }
398         }
399         if (expand_stack(vma, address))
400                 goto bad_area;
401         /*
402          * Ok, we have a good vm_area for this memory access, so
403          * we can handle it..
404          */
405 good_area:
406         si_code = SEGV_ACCERR;
407
408         /* If we took a ITLB miss on a non-executable page, catch
409          * that here.
410          */
411         if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
412                 BUG_ON(address != regs->tpc);
413                 BUG_ON(regs->tstate & TSTATE_PRIV);
414                 goto bad_area;
415         }
416
417         if (fault_code & FAULT_CODE_WRITE) {
418                 if (!(vma->vm_flags & VM_WRITE))
419                         goto bad_area;
420
421                 /* Spitfire has an icache which does not snoop
422                  * processor stores.  Later processors do...
423                  */
424                 if (tlb_type == spitfire &&
425                     (vma->vm_flags & VM_EXEC) != 0 &&
426                     vma->vm_file != NULL)
427                         set_thread_fault_code(fault_code |
428                                               FAULT_CODE_BLKCOMMIT);
429         } else {
430                 /* Allow reads even for write-only mappings */
431                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
432                         goto bad_area;
433         }
434
435         switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
436         case VM_FAULT_MINOR:
437                 current->min_flt++;
438                 break;
439         case VM_FAULT_MAJOR:
440                 current->maj_flt++;
441                 break;
442         case VM_FAULT_SIGBUS:
443                 goto do_sigbus;
444         case VM_FAULT_OOM:
445                 goto out_of_memory;
446         default:
447                 BUG();
448         }
449
450         up_read(&mm->mmap_sem);
451
452         mm_rss = get_mm_rss(mm);
453 #ifdef CONFIG_HUGETLB_PAGE
454         mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
455 #endif
456         if (unlikely(mm_rss >
457                      mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
458                 tsb_grow(mm, MM_TSB_BASE, mm_rss);
459 #ifdef CONFIG_HUGETLB_PAGE
460         mm_rss = mm->context.huge_pte_count;
461         if (unlikely(mm_rss >
462                      mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
463                 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
464 #endif
465         return;
466
467         /*
468          * Something tried to access memory that isn't in our memory map..
469          * Fix it, but check if it's kernel or user first..
470          */
471 bad_area:
472         insn = get_fault_insn(regs, insn);
473         up_read(&mm->mmap_sem);
474
475 handle_kernel_fault:
476         do_kernel_fault(regs, si_code, fault_code, insn, address);
477         return;
478
479 /*
480  * We ran out of memory, or some other thing happened to us that made
481  * us unable to handle the page fault gracefully.
482  */
483 out_of_memory:
484         insn = get_fault_insn(regs, insn);
485         up_read(&mm->mmap_sem);
486         printk("VM: killing process %s\n", current->comm);
487         if (!(regs->tstate & TSTATE_PRIV))
488                 do_exit(SIGKILL);
489         goto handle_kernel_fault;
490
491 intr_or_no_mm:
492         insn = get_fault_insn(regs, 0);
493         goto handle_kernel_fault;
494
495 do_sigbus:
496         insn = get_fault_insn(regs, insn);
497         up_read(&mm->mmap_sem);
498
499         /*
500          * Send a sigbus, regardless of whether we were in kernel
501          * or user mode.
502          */
503         do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
504
505         /* Kernel mode? Handle exceptions or die */
506         if (regs->tstate & TSTATE_PRIV)
507                 goto handle_kernel_fault;
508 }