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