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