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