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