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