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