Auto-update from upstream
[linux-2.6] / arch / sparc / mm / fault.c
1 /* $Id: fault.c,v 1.122 2001/11/17 07:19:26 davem Exp $
2  * fault.c:  Page fault handlers for the Sparc.
3  *
4  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
6  * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7  */
8
9 #include <asm/head.h>
10
11 #include <linux/string.h>
12 #include <linux/types.h>
13 #include <linux/sched.h>
14 #include <linux/ptrace.h>
15 #include <linux/mman.h>
16 #include <linux/threads.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/mm.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24
25 #include <asm/system.h>
26 #include <asm/page.h>
27 #include <asm/pgtable.h>
28 #include <asm/memreg.h>
29 #include <asm/openprom.h>
30 #include <asm/oplib.h>
31 #include <asm/smp.h>
32 #include <asm/traps.h>
33 #include <asm/kdebug.h>
34 #include <asm/uaccess.h>
35
36 #define ELEMENTS(arr) (sizeof (arr)/sizeof (arr[0]))
37
38 extern int prom_node_root;
39
40 /* At boot time we determine these two values necessary for setting
41  * up the segment maps and page table entries (pte's).
42  */
43
44 int num_segmaps, num_contexts;
45 int invalid_segment;
46
47 /* various Virtual Address Cache parameters we find at boot time... */
48
49 int vac_size, vac_linesize, vac_do_hw_vac_flushes;
50 int vac_entries_per_context, vac_entries_per_segment;
51 int vac_entries_per_page;
52
53 /* Nice, simple, prom library does all the sweating for us. ;) */
54 int prom_probe_memory (void)
55 {
56         register struct linux_mlist_v0 *mlist;
57         register unsigned long bytes, base_paddr, tally;
58         register int i;
59
60         i = 0;
61         mlist= *prom_meminfo()->v0_available;
62         bytes = tally = mlist->num_bytes;
63         base_paddr = (unsigned long) mlist->start_adr;
64   
65         sp_banks[0].base_addr = base_paddr;
66         sp_banks[0].num_bytes = bytes;
67
68         while (mlist->theres_more != (void *) 0){
69                 i++;
70                 mlist = mlist->theres_more;
71                 bytes = mlist->num_bytes;
72                 tally += bytes;
73                 if (i > SPARC_PHYS_BANKS-1) {
74                         printk ("The machine has more banks than "
75                                 "this kernel can support\n"
76                                 "Increase the SPARC_PHYS_BANKS "
77                                 "setting (currently %d)\n",
78                                 SPARC_PHYS_BANKS);
79                         i = SPARC_PHYS_BANKS-1;
80                         break;
81                 }
82     
83                 sp_banks[i].base_addr = (unsigned long) mlist->start_adr;
84                 sp_banks[i].num_bytes = mlist->num_bytes;
85         }
86
87         i++;
88         sp_banks[i].base_addr = 0xdeadbeef;
89         sp_banks[i].num_bytes = 0;
90
91         /* Now mask all bank sizes on a page boundary, it is all we can
92          * use anyways.
93          */
94         for(i=0; sp_banks[i].num_bytes != 0; i++)
95                 sp_banks[i].num_bytes &= PAGE_MASK;
96
97         return tally;
98 }
99
100 /* Traverse the memory lists in the prom to see how much physical we
101  * have.
102  */
103 unsigned long
104 probe_memory(void)
105 {
106         int total;
107
108         total = prom_probe_memory();
109
110         /* Oh man, much nicer, keep the dirt in promlib. */
111         return total;
112 }
113
114 extern void sun4c_complete_all_stores(void);
115
116 /* Whee, a level 15 NMI interrupt memory error.  Let's have fun... */
117 asmlinkage void sparc_lvl15_nmi(struct pt_regs *regs, unsigned long serr,
118                                 unsigned long svaddr, unsigned long aerr,
119                                 unsigned long avaddr)
120 {
121         sun4c_complete_all_stores();
122         printk("FAULT: NMI received\n");
123         printk("SREGS: Synchronous Error %08lx\n", serr);
124         printk("       Synchronous Vaddr %08lx\n", svaddr);
125         printk("      Asynchronous Error %08lx\n", aerr);
126         printk("      Asynchronous Vaddr %08lx\n", avaddr);
127         if (sun4c_memerr_reg)
128                 printk("     Memory Parity Error %08lx\n", *sun4c_memerr_reg);
129         printk("REGISTER DUMP:\n");
130         show_regs(regs);
131         prom_halt();
132 }
133
134 static void unhandled_fault(unsigned long, struct task_struct *,
135                 struct pt_regs *) __attribute__ ((noreturn));
136
137 static void unhandled_fault(unsigned long address, struct task_struct *tsk,
138                      struct pt_regs *regs)
139 {
140         if((unsigned long) address < PAGE_SIZE) {
141                 printk(KERN_ALERT
142                     "Unable to handle kernel NULL pointer dereference\n");
143         } else {
144                 printk(KERN_ALERT "Unable to handle kernel paging request "
145                        "at virtual address %08lx\n", address);
146         }
147         printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
148                 (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
149         printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
150                 (tsk->mm ? (unsigned long) tsk->mm->pgd :
151                         (unsigned long) tsk->active_mm->pgd));
152         die_if_kernel("Oops", regs);
153 }
154
155 asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc, 
156                             unsigned long address)
157 {
158         struct pt_regs regs;
159         unsigned long g2;
160         unsigned int insn;
161         int i;
162         
163         i = search_extables_range(ret_pc, &g2);
164         switch (i) {
165         case 3:
166                 /* load & store will be handled by fixup */
167                 return 3;
168
169         case 1:
170                 /* store will be handled by fixup, load will bump out */
171                 /* for _to_ macros */
172                 insn = *((unsigned int *) pc);
173                 if ((insn >> 21) & 1)
174                         return 1;
175                 break;
176
177         case 2:
178                 /* load will be handled by fixup, store will bump out */
179                 /* for _from_ macros */
180                 insn = *((unsigned int *) pc);
181                 if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
182                         return 2; 
183                 break; 
184
185         default:
186                 break;
187         };
188
189         memset(&regs, 0, sizeof (regs));
190         regs.pc = pc;
191         regs.npc = pc + 4;
192         __asm__ __volatile__(
193                 "rd %%psr, %0\n\t"
194                 "nop\n\t"
195                 "nop\n\t"
196                 "nop\n" : "=r" (regs.psr));
197         unhandled_fault(address, current, &regs);
198
199         /* Not reached */
200         return 0;
201 }
202
203 extern unsigned long safe_compute_effective_address(struct pt_regs *,
204                                                     unsigned int);
205
206 static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
207 {
208         unsigned int insn;
209
210         if (text_fault)
211                 return regs->pc;
212
213         if (regs->psr & PSR_PS) {
214                 insn = *(unsigned int *) regs->pc;
215         } else {
216                 __get_user(insn, (unsigned int *) regs->pc);
217         }
218
219         return safe_compute_effective_address(regs, insn);
220 }
221
222 asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
223                                unsigned long address)
224 {
225         struct vm_area_struct *vma;
226         struct task_struct *tsk = current;
227         struct mm_struct *mm = tsk->mm;
228         unsigned int fixup;
229         unsigned long g2;
230         siginfo_t info;
231         int from_user = !(regs->psr & PSR_PS);
232
233         if(text_fault)
234                 address = regs->pc;
235
236         /*
237          * We fault-in kernel-space virtual memory on-demand. The
238          * 'reference' page table is init_mm.pgd.
239          *
240          * NOTE! We MUST NOT take any locks for this case. We may
241          * be in an interrupt or a critical region, and should
242          * only copy the information from the master page table,
243          * nothing more.
244          */
245         if (!ARCH_SUN4C_SUN4 && address >= TASK_SIZE)
246                 goto vmalloc_fault;
247
248         info.si_code = SEGV_MAPERR;
249
250         /*
251          * If we're in an interrupt or have no user
252          * context, we must not take the fault..
253          */
254         if (in_atomic() || !mm)
255                 goto no_context;
256
257         down_read(&mm->mmap_sem);
258
259         /*
260          * The kernel referencing a bad kernel pointer can lock up
261          * a sun4c machine completely, so we must attempt recovery.
262          */
263         if(!from_user && address >= PAGE_OFFSET)
264                 goto bad_area;
265
266         vma = find_vma(mm, address);
267         if(!vma)
268                 goto bad_area;
269         if(vma->vm_start <= address)
270                 goto good_area;
271         if(!(vma->vm_flags & VM_GROWSDOWN))
272                 goto bad_area;
273         if(expand_stack(vma, address))
274                 goto bad_area;
275         /*
276          * Ok, we have a good vm_area for this memory access, so
277          * we can handle it..
278          */
279 good_area:
280         info.si_code = SEGV_ACCERR;
281         if(write) {
282                 if(!(vma->vm_flags & VM_WRITE))
283                         goto bad_area;
284         } else {
285                 /* Allow reads even for write-only mappings */
286                 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
287                         goto bad_area;
288         }
289
290         /*
291          * If for any reason at all we couldn't handle the fault,
292          * make sure we exit gracefully rather than endlessly redo
293          * the fault.
294          */
295         switch (handle_mm_fault(mm, vma, address, write)) {
296         case VM_FAULT_SIGBUS:
297                 goto do_sigbus;
298         case VM_FAULT_OOM:
299                 goto out_of_memory;
300         case VM_FAULT_MAJOR:
301                 current->maj_flt++;
302                 break;
303         case VM_FAULT_MINOR:
304         default:
305                 current->min_flt++;
306                 break;
307         }
308         up_read(&mm->mmap_sem);
309         return;
310
311         /*
312          * Something tried to access memory that isn't in our memory map..
313          * Fix it, but check if it's kernel or user first..
314          */
315 bad_area:
316         up_read(&mm->mmap_sem);
317
318 bad_area_nosemaphore:
319         /* User mode accesses just cause a SIGSEGV */
320         if(from_user) {
321 #if 0
322                 printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
323                        tsk->comm, tsk->pid, address, regs->pc);
324 #endif
325                 info.si_signo = SIGSEGV;
326                 info.si_errno = 0;
327                 /* info.si_code set above to make clear whether
328                    this was a SEGV_MAPERR or SEGV_ACCERR fault.  */
329                 info.si_addr = (void __user *)compute_si_addr(regs, text_fault);
330                 info.si_trapno = 0;
331                 force_sig_info (SIGSEGV, &info, tsk);
332                 return;
333         }
334
335         /* Is this in ex_table? */
336 no_context:
337         g2 = regs->u_regs[UREG_G2];
338         if (!from_user && (fixup = search_extables_range(regs->pc, &g2))) {
339                 if (fixup > 10) { /* Values below are reserved for other things */
340                         extern const unsigned __memset_start[];
341                         extern const unsigned __memset_end[];
342                         extern const unsigned __csum_partial_copy_start[];
343                         extern const unsigned __csum_partial_copy_end[];
344
345 #ifdef DEBUG_EXCEPTIONS
346                         printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
347                         printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
348                                 regs->pc, fixup, g2);
349 #endif
350                         if ((regs->pc >= (unsigned long)__memset_start &&
351                              regs->pc < (unsigned long)__memset_end) ||
352                             (regs->pc >= (unsigned long)__csum_partial_copy_start &&
353                              regs->pc < (unsigned long)__csum_partial_copy_end)) {
354                                 regs->u_regs[UREG_I4] = address;
355                                 regs->u_regs[UREG_I5] = regs->pc;
356                         }
357                         regs->u_regs[UREG_G2] = g2;
358                         regs->pc = fixup;
359                         regs->npc = regs->pc + 4;
360                         return;
361                 }
362         }
363         
364         unhandled_fault (address, tsk, regs);
365         do_exit(SIGKILL);
366
367 /*
368  * We ran out of memory, or some other thing happened to us that made
369  * us unable to handle the page fault gracefully.
370  */
371 out_of_memory:
372         up_read(&mm->mmap_sem);
373         printk("VM: killing process %s\n", tsk->comm);
374         if (from_user)
375                 do_exit(SIGKILL);
376         goto no_context;
377
378 do_sigbus:
379         up_read(&mm->mmap_sem);
380         info.si_signo = SIGBUS;
381         info.si_errno = 0;
382         info.si_code = BUS_ADRERR;
383         info.si_addr = (void __user *) compute_si_addr(regs, text_fault);
384         info.si_trapno = 0;
385         force_sig_info (SIGBUS, &info, tsk);
386         if (!from_user)
387                 goto no_context;
388
389 vmalloc_fault:
390         {
391                 /*
392                  * Synchronize this task's top level page-table
393                  * with the 'reference' page table.
394                  */
395                 int offset = pgd_index(address);
396                 pgd_t *pgd, *pgd_k;
397                 pmd_t *pmd, *pmd_k;
398
399                 pgd = tsk->active_mm->pgd + offset;
400                 pgd_k = init_mm.pgd + offset;
401
402                 if (!pgd_present(*pgd)) {
403                         if (!pgd_present(*pgd_k))
404                                 goto bad_area_nosemaphore;
405                         pgd_val(*pgd) = pgd_val(*pgd_k);
406                         return;
407                 }
408
409                 pmd = pmd_offset(pgd, address);
410                 pmd_k = pmd_offset(pgd_k, address);
411
412                 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
413                         goto bad_area_nosemaphore;
414                 *pmd = *pmd_k;
415                 return;
416         }
417 }
418
419 asmlinkage void do_sun4c_fault(struct pt_regs *regs, int text_fault, int write,
420                                unsigned long address)
421 {
422         extern void sun4c_update_mmu_cache(struct vm_area_struct *,
423                                            unsigned long,pte_t);
424         extern pte_t *sun4c_pte_offset_kernel(pmd_t *,unsigned long);
425         struct task_struct *tsk = current;
426         struct mm_struct *mm = tsk->mm;
427         pgd_t *pgdp;
428         pte_t *ptep;
429
430         if (text_fault) {
431                 address = regs->pc;
432         } else if (!write &&
433                    !(regs->psr & PSR_PS)) {
434                 unsigned int insn, __user *ip;
435
436                 ip = (unsigned int __user *)regs->pc;
437                 if (!get_user(insn, ip)) {
438                         if ((insn & 0xc1680000) == 0xc0680000)
439                                 write = 1;
440                 }
441         }
442
443         if (!mm) {
444                 /* We are oopsing. */
445                 do_sparc_fault(regs, text_fault, write, address);
446                 BUG();  /* P3 Oops already, you bitch */
447         }
448
449         pgdp = pgd_offset(mm, address);
450         ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, address);
451
452         if (pgd_val(*pgdp)) {
453             if (write) {
454                 if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
455                                    == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
456                         unsigned long flags;
457
458                         *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
459                                       _SUN4C_PAGE_MODIFIED |
460                                       _SUN4C_PAGE_VALID |
461                                       _SUN4C_PAGE_DIRTY);
462
463                         local_irq_save(flags);
464                         if (sun4c_get_segmap(address) != invalid_segment) {
465                                 sun4c_put_pte(address, pte_val(*ptep));
466                                 local_irq_restore(flags);
467                                 return;
468                         }
469                         local_irq_restore(flags);
470                 }
471             } else {
472                 if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
473                                    == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
474                         unsigned long flags;
475
476                         *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
477                                       _SUN4C_PAGE_VALID);
478
479                         local_irq_save(flags);
480                         if (sun4c_get_segmap(address) != invalid_segment) {
481                                 sun4c_put_pte(address, pte_val(*ptep));
482                                 local_irq_restore(flags);
483                                 return;
484                         }
485                         local_irq_restore(flags);
486                 }
487             }
488         }
489
490         /* This conditional is 'interesting'. */
491         if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
492             && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
493                 /* Note: It is safe to not grab the MMAP semaphore here because
494                  *       we know that update_mmu_cache() will not sleep for
495                  *       any reason (at least not in the current implementation)
496                  *       and therefore there is no danger of another thread getting
497                  *       on the CPU and doing a shrink_mmap() on this vma.
498                  */
499                 sun4c_update_mmu_cache (find_vma(current->mm, address), address,
500                                         *ptep);
501         else
502                 do_sparc_fault(regs, text_fault, write, address);
503 }
504
505 /* This always deals with user addresses. */
506 inline void force_user_fault(unsigned long address, int write)
507 {
508         struct vm_area_struct *vma;
509         struct task_struct *tsk = current;
510         struct mm_struct *mm = tsk->mm;
511         siginfo_t info;
512
513         info.si_code = SEGV_MAPERR;
514
515 #if 0
516         printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
517                tsk->pid, write, address);
518 #endif
519         down_read(&mm->mmap_sem);
520         vma = find_vma(mm, address);
521         if(!vma)
522                 goto bad_area;
523         if(vma->vm_start <= address)
524                 goto good_area;
525         if(!(vma->vm_flags & VM_GROWSDOWN))
526                 goto bad_area;
527         if(expand_stack(vma, address))
528                 goto bad_area;
529 good_area:
530         info.si_code = SEGV_ACCERR;
531         if(write) {
532                 if(!(vma->vm_flags & VM_WRITE))
533                         goto bad_area;
534         } else {
535                 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
536                         goto bad_area;
537         }
538         switch (handle_mm_fault(mm, vma, address, write)) {
539         case VM_FAULT_SIGBUS:
540         case VM_FAULT_OOM:
541                 goto do_sigbus;
542         }
543         up_read(&mm->mmap_sem);
544         return;
545 bad_area:
546         up_read(&mm->mmap_sem);
547 #if 0
548         printk("Window whee %s [%d]: segfaults at %08lx\n",
549                tsk->comm, tsk->pid, address);
550 #endif
551         info.si_signo = SIGSEGV;
552         info.si_errno = 0;
553         /* info.si_code set above to make clear whether
554            this was a SEGV_MAPERR or SEGV_ACCERR fault.  */
555         info.si_addr = (void __user *) address;
556         info.si_trapno = 0;
557         force_sig_info (SIGSEGV, &info, tsk);
558         return;
559
560 do_sigbus:
561         up_read(&mm->mmap_sem);
562         info.si_signo = SIGBUS;
563         info.si_errno = 0;
564         info.si_code = BUS_ADRERR;
565         info.si_addr = (void __user *) address;
566         info.si_trapno = 0;
567         force_sig_info (SIGBUS, &info, tsk);
568 }
569
570 void window_overflow_fault(void)
571 {
572         unsigned long sp;
573
574         sp = current_thread_info()->rwbuf_stkptrs[0];
575         if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
576                 force_user_fault(sp + 0x38, 1);
577         force_user_fault(sp, 1);
578 }
579
580 void window_underflow_fault(unsigned long sp)
581 {
582         if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
583                 force_user_fault(sp + 0x38, 0);
584         force_user_fault(sp, 0);
585 }
586
587 void window_ret_fault(struct pt_regs *regs)
588 {
589         unsigned long sp;
590
591         sp = regs->u_regs[UREG_FP];
592         if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
593                 force_user_fault(sp + 0x38, 0);
594         force_user_fault(sp, 0);
595 }