vserver 1.9.5.x5
[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         pud_t *pudp;
179         pmd_t *pmdp;
180         pte_t *ptep, pte;
181         unsigned long pa;
182         u32 insn = 0;
183         unsigned long pstate;
184
185         if (pgd_none(*pgdp))
186                 goto outret;
187         pudp = pud_offset(pgdp, tpc);
188         if (pud_none(*pudp))
189                 goto outret;
190         pmdp = pmd_offset(pudp, tpc);
191         if (pmd_none(*pmdp))
192                 goto outret;
193
194         /* This disables preemption for us as well. */
195         __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
196         __asm__ __volatile__("wrpr %0, %1, %%pstate"
197                                 : : "r" (pstate), "i" (PSTATE_IE));
198         ptep = pte_offset_map(pmdp, tpc);
199         pte = *ptep;
200         if (!pte_present(pte))
201                 goto out;
202
203         pa  = (pte_val(pte) & _PAGE_PADDR);
204         pa += (tpc & ~PAGE_MASK);
205
206         /* Use phys bypass so we don't pollute dtlb/dcache. */
207         __asm__ __volatile__("lduwa [%1] %2, %0"
208                              : "=r" (insn)
209                              : "r" (pa), "i" (ASI_PHYS_USE_EC));
210
211 out:
212         pte_unmap(ptep);
213         __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
214 outret:
215         return insn;
216 }
217
218 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
219
220 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
221                              unsigned int insn, int fault_code)
222 {
223         siginfo_t info;
224
225         info.si_code = code;
226         info.si_signo = sig;
227         info.si_errno = 0;
228         if (fault_code & FAULT_CODE_ITLB)
229                 info.si_addr = (void __user *) regs->tpc;
230         else
231                 info.si_addr = (void __user *)
232                         compute_effective_address(regs, insn, 0);
233         info.si_trapno = 0;
234         force_sig_info(sig, &info, current);
235 }
236
237 extern int handle_ldf_stq(u32, struct pt_regs *);
238 extern int handle_ld_nf(u32, struct pt_regs *);
239
240 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
241 {
242         if (!insn) {
243                 if (!regs->tpc || (regs->tpc & 0x3))
244                         return 0;
245                 if (regs->tstate & TSTATE_PRIV) {
246                         insn = *(unsigned int *) regs->tpc;
247                 } else {
248                         insn = get_user_insn(regs->tpc);
249                 }
250         }
251         return insn;
252 }
253
254 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
255                             unsigned int insn, unsigned long address)
256 {
257         unsigned long g2;
258         unsigned char asi = ASI_P;
259  
260         if ((!insn) && (regs->tstate & TSTATE_PRIV))
261                 goto cannot_handle;
262
263         /* If user insn could be read (thus insn is zero), that
264          * is fine.  We will just gun down the process with a signal
265          * in that case.
266          */
267
268         if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
269             (insn & 0xc0800000) == 0xc0800000) {
270                 if (insn & 0x2000)
271                         asi = (regs->tstate >> 24);
272                 else
273                         asi = (insn >> 5);
274                 if ((asi & 0xf2) == 0x82) {
275                         if (insn & 0x1000000) {
276                                 handle_ldf_stq(insn, regs);
277                         } else {
278                                 /* This was a non-faulting load. Just clear the
279                                  * destination register(s) and continue with the next
280                                  * instruction. -jj
281                                  */
282                                 handle_ld_nf(insn, regs);
283                         }
284                         return;
285                 }
286         }
287                 
288         g2 = regs->u_regs[UREG_G2];
289
290         /* Is this in ex_table? */
291         if (regs->tstate & TSTATE_PRIV) {
292                 unsigned long fixup;
293
294                 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
295                         if (insn & 0x2000)
296                                 asi = (regs->tstate >> 24);
297                         else
298                                 asi = (insn >> 5);
299                 }
300         
301                 /* Look in asi.h: All _S asis have LS bit set */
302                 if ((asi & 0x1) &&
303                     (fixup = search_extables_range(regs->tpc, &g2))) {
304                         regs->tpc = fixup;
305                         regs->tnpc = regs->tpc + 4;
306                         regs->u_regs[UREG_G2] = g2;
307                         return;
308                 }
309         } else {
310                 /* The si_code was set to make clear whether
311                  * this was a SEGV_MAPERR or SEGV_ACCERR fault.
312                  */
313                 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
314                 return;
315         }
316
317 cannot_handle:
318         unhandled_fault (address, current, regs);
319 }
320
321 asmlinkage void do_sparc64_fault(struct pt_regs *regs)
322 {
323         struct mm_struct *mm = current->mm;
324         struct vm_area_struct *vma;
325         unsigned int insn = 0;
326         int si_code, fault_code;
327         unsigned long address;
328
329         fault_code = get_thread_fault_code();
330
331         if (notify_die(DIE_PAGE_FAULT, "page_fault", regs,
332                        fault_code, 0, SIGSEGV) == NOTIFY_STOP)
333                 return;
334
335         si_code = SEGV_MAPERR;
336         address = current_thread_info()->fault_address;
337
338         if ((fault_code & FAULT_CODE_ITLB) &&
339             (fault_code & FAULT_CODE_DTLB))
340                 BUG();
341
342         if (regs->tstate & TSTATE_PRIV) {
343                 unsigned long tpc = regs->tpc;
344
345                 /* Sanity check the PC. */
346                 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
347                     (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
348                         /* Valid, no problems... */
349                 } else {
350                         bad_kernel_pc(regs);
351                         return;
352                 }
353         }
354
355         /*
356          * If we're in an interrupt or have no user
357          * context, we must not take the fault..
358          */
359         if (in_atomic() || !mm)
360                 goto intr_or_no_mm;
361
362         if (test_thread_flag(TIF_32BIT)) {
363                 if (!(regs->tstate & TSTATE_PRIV))
364                         regs->tpc &= 0xffffffff;
365                 address &= 0xffffffff;
366         }
367
368         if (!down_read_trylock(&mm->mmap_sem)) {
369                 if ((regs->tstate & TSTATE_PRIV) &&
370                     !search_exception_tables(regs->tpc)) {
371                         insn = get_fault_insn(regs, insn);
372                         goto handle_kernel_fault;
373                 }
374                 down_read(&mm->mmap_sem);
375         }
376
377         vma = find_vma(mm, address);
378         if (!vma)
379                 goto bad_area;
380
381         /* Pure DTLB misses do not tell us whether the fault causing
382          * load/store/atomic was a write or not, it only says that there
383          * was no match.  So in such a case we (carefully) read the
384          * instruction to try and figure this out.  It's an optimization
385          * so it's ok if we can't do this.
386          *
387          * Special hack, window spill/fill knows the exact fault type.
388          */
389         if (((fault_code &
390               (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
391             (vma->vm_flags & VM_WRITE) != 0) {
392                 insn = get_fault_insn(regs, 0);
393                 if (!insn)
394                         goto continue_fault;
395                 if ((insn & 0xc0200000) == 0xc0200000 &&
396                     (insn & 0x1780000) != 0x1680000) {
397                         /* Don't bother updating thread struct value,
398                          * because update_mmu_cache only cares which tlb
399                          * the access came from.
400                          */
401                         fault_code |= FAULT_CODE_WRITE;
402                 }
403         }
404 continue_fault:
405
406         if (vma->vm_start <= address)
407                 goto good_area;
408         if (!(vma->vm_flags & VM_GROWSDOWN))
409                 goto bad_area;
410         if (!(fault_code & FAULT_CODE_WRITE)) {
411                 /* Non-faulting loads shouldn't expand stack. */
412                 insn = get_fault_insn(regs, insn);
413                 if ((insn & 0xc0800000) == 0xc0800000) {
414                         unsigned char asi;
415
416                         if (insn & 0x2000)
417                                 asi = (regs->tstate >> 24);
418                         else
419                                 asi = (insn >> 5);
420                         if ((asi & 0xf2) == 0x82)
421                                 goto bad_area;
422                 }
423         }
424         if (expand_stack(vma, address))
425                 goto bad_area;
426         /*
427          * Ok, we have a good vm_area for this memory access, so
428          * we can handle it..
429          */
430 good_area:
431         si_code = SEGV_ACCERR;
432
433         /* If we took a ITLB miss on a non-executable page, catch
434          * that here.
435          */
436         if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
437                 BUG_ON(address != regs->tpc);
438                 BUG_ON(regs->tstate & TSTATE_PRIV);
439                 goto bad_area;
440         }
441
442         if (fault_code & FAULT_CODE_WRITE) {
443                 if (!(vma->vm_flags & VM_WRITE))
444                         goto bad_area;
445
446                 /* Spitfire has an icache which does not snoop
447                  * processor stores.  Later processors do...
448                  */
449                 if (tlb_type == spitfire &&
450                     (vma->vm_flags & VM_EXEC) != 0 &&
451                     vma->vm_file != NULL)
452                         set_thread_fault_code(fault_code |
453                                               FAULT_CODE_BLKCOMMIT);
454         } else {
455                 /* Allow reads even for write-only mappings */
456                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
457                         goto bad_area;
458         }
459
460         switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
461         case VM_FAULT_MINOR:
462                 current->min_flt++;
463                 break;
464         case VM_FAULT_MAJOR:
465                 current->maj_flt++;
466                 break;
467         case VM_FAULT_SIGBUS:
468                 goto do_sigbus;
469         case VM_FAULT_OOM:
470                 goto out_of_memory;
471         default:
472                 BUG();
473         }
474
475         up_read(&mm->mmap_sem);
476         goto fault_done;
477
478         /*
479          * Something tried to access memory that isn't in our memory map..
480          * Fix it, but check if it's kernel or user first..
481          */
482 bad_area:
483         insn = get_fault_insn(regs, insn);
484         up_read(&mm->mmap_sem);
485
486 handle_kernel_fault:
487         do_kernel_fault(regs, si_code, fault_code, insn, address);
488
489         goto fault_done;
490
491 /*
492  * We ran out of memory, or some other thing happened to us that made
493  * us unable to handle the page fault gracefully.
494  */
495 out_of_memory:
496         insn = get_fault_insn(regs, insn);
497         up_read(&mm->mmap_sem);
498         printk("VM: killing process %s\n", current->comm);
499         if (!(regs->tstate & TSTATE_PRIV))
500                 do_exit(SIGKILL);
501         goto handle_kernel_fault;
502
503 intr_or_no_mm:
504         insn = get_fault_insn(regs, 0);
505         goto handle_kernel_fault;
506
507 do_sigbus:
508         insn = get_fault_insn(regs, insn);
509         up_read(&mm->mmap_sem);
510
511         /*
512          * Send a sigbus, regardless of whether we were in kernel
513          * or user mode.
514          */
515         do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
516
517         /* Kernel mode? Handle exceptions or die */
518         if (regs->tstate & TSTATE_PRIV)
519                 goto handle_kernel_fault;
520
521 fault_done:
522         /* These values are no longer needed, clear them. */
523         set_thread_fault_code(0);
524         current_thread_info()->fault_address = 0;
525 }