]> err.no Git - linux-2.6/blob - arch/sparc64/mm/fault.c
[SPARC64]: Log faulting vaddr when bogus kernel PC detected.
[linux-2.6] / 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
33 /*
34  * To debug kernel to catch accesses to certain virtual/physical addresses.
35  * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
36  * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
37  * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
38  * watched. This is only useful on a single cpu machine for now. After the watchpoint
39  * is detected, the process causing it will be killed, thus preventing an infinite loop.
40  */
41 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
42 {
43         unsigned long lsubits;
44
45         __asm__ __volatile__("ldxa [%%g0] %1, %0"
46                              : "=r" (lsubits)
47                              : "i" (ASI_LSU_CONTROL));
48         lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
49                      LSU_CONTROL_PR | LSU_CONTROL_VR |
50                      LSU_CONTROL_PW | LSU_CONTROL_VW);
51
52         __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
53                              "membar    #Sync"
54                              : /* no outputs */
55                              : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
56                                "i" (ASI_DMMU));
57
58         lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
59         if (flags & VM_READ)
60                 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
61         if (flags & VM_WRITE)
62                 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
63         __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
64                              "membar #Sync"
65                              : /* no outputs */
66                              : "r" (lsubits), "i" (ASI_LSU_CONTROL)
67                              : "memory");
68 }
69
70 static void __kprobes unhandled_fault(unsigned long address,
71                                       struct task_struct *tsk,
72                                       struct pt_regs *regs)
73 {
74         if ((unsigned long) address < PAGE_SIZE) {
75                 printk(KERN_ALERT "Unable to handle kernel NULL "
76                        "pointer dereference\n");
77         } else {
78                 printk(KERN_ALERT "Unable to handle kernel paging request "
79                        "at virtual address %016lx\n", (unsigned long)address);
80         }
81         printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
82                (tsk->mm ?
83                 CTX_HWBITS(tsk->mm->context) :
84                 CTX_HWBITS(tsk->active_mm->context)));
85         printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
86                (tsk->mm ? (unsigned long) tsk->mm->pgd :
87                           (unsigned long) tsk->active_mm->pgd));
88         if (notify_die(DIE_GPF, "general protection fault", regs,
89                        0, 0, SIGSEGV) == NOTIFY_STOP)
90                 return;
91         die_if_kernel("Oops", regs);
92 }
93
94 static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
95 {
96         unsigned long *ksp;
97
98         printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
99                regs->tpc);
100         printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
101         __asm__("mov %%sp, %0" : "=r" (ksp));
102         show_stack(current, ksp);
103         unhandled_fault(regs->tpc, current, regs);
104 }
105
106 /*
107  * We now make sure that mmap_sem is held in all paths that call 
108  * this. Additionally, to prevent kswapd from ripping ptes from
109  * under us, raise interrupts around the time that we look at the
110  * pte, kswapd will have to wait to get his smp ipi response from
111  * us. vmtruncate likewise. This saves us having to get pte lock.
112  */
113 static unsigned int get_user_insn(unsigned long tpc)
114 {
115         pgd_t *pgdp = pgd_offset(current->mm, tpc);
116         pud_t *pudp;
117         pmd_t *pmdp;
118         pte_t *ptep, pte;
119         unsigned long pa;
120         u32 insn = 0;
121         unsigned long pstate;
122
123         if (pgd_none(*pgdp))
124                 goto outret;
125         pudp = pud_offset(pgdp, tpc);
126         if (pud_none(*pudp))
127                 goto outret;
128         pmdp = pmd_offset(pudp, tpc);
129         if (pmd_none(*pmdp))
130                 goto outret;
131
132         /* This disables preemption for us as well. */
133         __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
134         __asm__ __volatile__("wrpr %0, %1, %%pstate"
135                                 : : "r" (pstate), "i" (PSTATE_IE));
136         ptep = pte_offset_map(pmdp, tpc);
137         pte = *ptep;
138         if (!pte_present(pte))
139                 goto out;
140
141         pa  = (pte_pfn(pte) << PAGE_SHIFT);
142         pa += (tpc & ~PAGE_MASK);
143
144         /* Use phys bypass so we don't pollute dtlb/dcache. */
145         __asm__ __volatile__("lduwa [%1] %2, %0"
146                              : "=r" (insn)
147                              : "r" (pa), "i" (ASI_PHYS_USE_EC));
148
149 out:
150         pte_unmap(ptep);
151         __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
152 outret:
153         return insn;
154 }
155
156 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
157
158 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
159                              unsigned int insn, int fault_code)
160 {
161         siginfo_t info;
162
163         info.si_code = code;
164         info.si_signo = sig;
165         info.si_errno = 0;
166         if (fault_code & FAULT_CODE_ITLB)
167                 info.si_addr = (void __user *) regs->tpc;
168         else
169                 info.si_addr = (void __user *)
170                         compute_effective_address(regs, insn, 0);
171         info.si_trapno = 0;
172         force_sig_info(sig, &info, current);
173 }
174
175 extern int handle_ldf_stq(u32, struct pt_regs *);
176 extern int handle_ld_nf(u32, struct pt_regs *);
177
178 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
179 {
180         if (!insn) {
181                 if (!regs->tpc || (regs->tpc & 0x3))
182                         return 0;
183                 if (regs->tstate & TSTATE_PRIV) {
184                         insn = *(unsigned int *) regs->tpc;
185                 } else {
186                         insn = get_user_insn(regs->tpc);
187                 }
188         }
189         return insn;
190 }
191
192 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
193                             unsigned int insn, unsigned long address)
194 {
195         unsigned char asi = ASI_P;
196  
197         if ((!insn) && (regs->tstate & TSTATE_PRIV))
198                 goto cannot_handle;
199
200         /* If user insn could be read (thus insn is zero), that
201          * is fine.  We will just gun down the process with a signal
202          * in that case.
203          */
204
205         if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
206             (insn & 0xc0800000) == 0xc0800000) {
207                 if (insn & 0x2000)
208                         asi = (regs->tstate >> 24);
209                 else
210                         asi = (insn >> 5);
211                 if ((asi & 0xf2) == 0x82) {
212                         if (insn & 0x1000000) {
213                                 handle_ldf_stq(insn, regs);
214                         } else {
215                                 /* This was a non-faulting load. Just clear the
216                                  * destination register(s) and continue with the next
217                                  * instruction. -jj
218                                  */
219                                 handle_ld_nf(insn, regs);
220                         }
221                         return;
222                 }
223         }
224                 
225         /* Is this in ex_table? */
226         if (regs->tstate & TSTATE_PRIV) {
227                 const struct exception_table_entry *entry;
228
229                 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
230                         if (insn & 0x2000)
231                                 asi = (regs->tstate >> 24);
232                         else
233                                 asi = (insn >> 5);
234                 }
235         
236                 /* Look in asi.h: All _S asis have LS bit set */
237                 if ((asi & 0x1) &&
238                     (entry = search_exception_tables(regs->tpc))) {
239                         regs->tpc = entry->fixup;
240                         regs->tnpc = regs->tpc + 4;
241                         return;
242                 }
243         } else {
244                 /* The si_code was set to make clear whether
245                  * this was a SEGV_MAPERR or SEGV_ACCERR fault.
246                  */
247                 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
248                 return;
249         }
250
251 cannot_handle:
252         unhandled_fault (address, current, regs);
253 }
254
255 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
256 {
257         struct mm_struct *mm = current->mm;
258         struct vm_area_struct *vma;
259         unsigned int insn = 0;
260         int si_code, fault_code;
261         unsigned long address;
262
263         fault_code = get_thread_fault_code();
264
265         if (notify_die(DIE_PAGE_FAULT, "page_fault", regs,
266                        fault_code, 0, SIGSEGV) == NOTIFY_STOP)
267                 return;
268
269         si_code = SEGV_MAPERR;
270         address = current_thread_info()->fault_address;
271
272         if ((fault_code & FAULT_CODE_ITLB) &&
273             (fault_code & FAULT_CODE_DTLB))
274                 BUG();
275
276         if (regs->tstate & TSTATE_PRIV) {
277                 unsigned long tpc = regs->tpc;
278
279                 /* Sanity check the PC. */
280                 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
281                     (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
282                         /* Valid, no problems... */
283                 } else {
284                         bad_kernel_pc(regs, address);
285                         return;
286                 }
287         }
288
289         /*
290          * If we're in an interrupt or have no user
291          * context, we must not take the fault..
292          */
293         if (in_atomic() || !mm)
294                 goto intr_or_no_mm;
295
296         if (test_thread_flag(TIF_32BIT)) {
297                 if (!(regs->tstate & TSTATE_PRIV))
298                         regs->tpc &= 0xffffffff;
299                 address &= 0xffffffff;
300         }
301
302         if (!down_read_trylock(&mm->mmap_sem)) {
303                 if ((regs->tstate & TSTATE_PRIV) &&
304                     !search_exception_tables(regs->tpc)) {
305                         insn = get_fault_insn(regs, insn);
306                         goto handle_kernel_fault;
307                 }
308                 down_read(&mm->mmap_sem);
309         }
310
311         vma = find_vma(mm, address);
312         if (!vma)
313                 goto bad_area;
314
315         /* Pure DTLB misses do not tell us whether the fault causing
316          * load/store/atomic was a write or not, it only says that there
317          * was no match.  So in such a case we (carefully) read the
318          * instruction to try and figure this out.  It's an optimization
319          * so it's ok if we can't do this.
320          *
321          * Special hack, window spill/fill knows the exact fault type.
322          */
323         if (((fault_code &
324               (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
325             (vma->vm_flags & VM_WRITE) != 0) {
326                 insn = get_fault_insn(regs, 0);
327                 if (!insn)
328                         goto continue_fault;
329                 if ((insn & 0xc0200000) == 0xc0200000 &&
330                     (insn & 0x1780000) != 0x1680000) {
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         switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
395         case VM_FAULT_MINOR:
396                 current->min_flt++;
397                 break;
398         case VM_FAULT_MAJOR:
399                 current->maj_flt++;
400                 break;
401         case VM_FAULT_SIGBUS:
402                 goto do_sigbus;
403         case VM_FAULT_OOM:
404                 goto out_of_memory;
405         default:
406                 BUG();
407         }
408
409         up_read(&mm->mmap_sem);
410         return;
411
412         /*
413          * Something tried to access memory that isn't in our memory map..
414          * Fix it, but check if it's kernel or user first..
415          */
416 bad_area:
417         insn = get_fault_insn(regs, insn);
418         up_read(&mm->mmap_sem);
419
420 handle_kernel_fault:
421         do_kernel_fault(regs, si_code, fault_code, insn, address);
422         return;
423
424 /*
425  * We ran out of memory, or some other thing happened to us that made
426  * us unable to handle the page fault gracefully.
427  */
428 out_of_memory:
429         insn = get_fault_insn(regs, insn);
430         up_read(&mm->mmap_sem);
431         printk("VM: killing process %s\n", current->comm);
432         if (!(regs->tstate & TSTATE_PRIV))
433                 do_exit(SIGKILL);
434         goto handle_kernel_fault;
435
436 intr_or_no_mm:
437         insn = get_fault_insn(regs, 0);
438         goto handle_kernel_fault;
439
440 do_sigbus:
441         insn = get_fault_insn(regs, insn);
442         up_read(&mm->mmap_sem);
443
444         /*
445          * Send a sigbus, regardless of whether we were in kernel
446          * or user mode.
447          */
448         do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
449
450         /* Kernel mode? Handle exceptions or die */
451         if (regs->tstate & TSTATE_PRIV)
452                 goto handle_kernel_fault;
453 }