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