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