1 /* $Id: fault.c,v 1.122 2001/11/17 07:19:26 davem Exp $
2 * fault.c: Page fault handlers for the Sparc.
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)
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>
20 #include <linux/smp.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/kdebug.h>
25 #include <asm/system.h>
27 #include <asm/pgtable.h>
28 #include <asm/memreg.h>
29 #include <asm/openprom.h>
30 #include <asm/oplib.h>
32 #include <asm/traps.h>
33 #include <asm/uaccess.h>
35 extern int prom_node_root;
37 /* At boot time we determine these two values necessary for setting
38 * up the segment maps and page table entries (pte's).
41 int num_segmaps, num_contexts;
44 /* various Virtual Address Cache parameters we find at boot time... */
46 int vac_size, vac_linesize, vac_do_hw_vac_flushes;
47 int vac_entries_per_context, vac_entries_per_segment;
48 int vac_entries_per_page;
50 /* Nice, simple, prom library does all the sweating for us. ;) */
51 int prom_probe_memory (void)
53 register struct linux_mlist_v0 *mlist;
54 register unsigned long bytes, base_paddr, tally;
58 mlist= *prom_meminfo()->v0_available;
59 bytes = tally = mlist->num_bytes;
60 base_paddr = (unsigned long) mlist->start_adr;
62 sp_banks[0].base_addr = base_paddr;
63 sp_banks[0].num_bytes = bytes;
65 while (mlist->theres_more != (void *) 0){
67 mlist = mlist->theres_more;
68 bytes = mlist->num_bytes;
70 if (i > SPARC_PHYS_BANKS-1) {
71 printk ("The machine has more banks than "
72 "this kernel can support\n"
73 "Increase the SPARC_PHYS_BANKS "
74 "setting (currently %d)\n",
76 i = SPARC_PHYS_BANKS-1;
80 sp_banks[i].base_addr = (unsigned long) mlist->start_adr;
81 sp_banks[i].num_bytes = mlist->num_bytes;
85 sp_banks[i].base_addr = 0xdeadbeef;
86 sp_banks[i].num_bytes = 0;
88 /* Now mask all bank sizes on a page boundary, it is all we can
91 for(i=0; sp_banks[i].num_bytes != 0; i++)
92 sp_banks[i].num_bytes &= PAGE_MASK;
97 /* Traverse the memory lists in the prom to see how much physical we
105 total = prom_probe_memory();
107 /* Oh man, much nicer, keep the dirt in promlib. */
111 extern void sun4c_complete_all_stores(void);
113 /* Whee, a level 15 NMI interrupt memory error. Let's have fun... */
114 asmlinkage void sparc_lvl15_nmi(struct pt_regs *regs, unsigned long serr,
115 unsigned long svaddr, unsigned long aerr,
116 unsigned long avaddr)
118 sun4c_complete_all_stores();
119 printk("FAULT: NMI received\n");
120 printk("SREGS: Synchronous Error %08lx\n", serr);
121 printk(" Synchronous Vaddr %08lx\n", svaddr);
122 printk(" Asynchronous Error %08lx\n", aerr);
123 printk(" Asynchronous Vaddr %08lx\n", avaddr);
124 if (sun4c_memerr_reg)
125 printk(" Memory Parity Error %08lx\n", *sun4c_memerr_reg);
126 printk("REGISTER DUMP:\n");
131 static void unhandled_fault(unsigned long, struct task_struct *,
132 struct pt_regs *) __attribute__ ((noreturn));
134 static void unhandled_fault(unsigned long address, struct task_struct *tsk,
135 struct pt_regs *regs)
137 if((unsigned long) address < PAGE_SIZE) {
139 "Unable to handle kernel NULL pointer dereference\n");
141 printk(KERN_ALERT "Unable to handle kernel paging request "
142 "at virtual address %08lx\n", address);
144 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
145 (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
146 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
147 (tsk->mm ? (unsigned long) tsk->mm->pgd :
148 (unsigned long) tsk->active_mm->pgd));
149 die_if_kernel("Oops", regs);
152 asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc,
153 unsigned long address)
160 i = search_extables_range(ret_pc, &g2);
163 /* load & store will be handled by fixup */
167 /* store will be handled by fixup, load will bump out */
168 /* for _to_ macros */
169 insn = *((unsigned int *) pc);
170 if ((insn >> 21) & 1)
175 /* load will be handled by fixup, store will bump out */
176 /* for _from_ macros */
177 insn = *((unsigned int *) pc);
178 if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
186 memset(®s, 0, sizeof (regs));
189 __asm__ __volatile__(
193 "nop\n" : "=r" (regs.psr));
194 unhandled_fault(address, current, ®s);
200 extern unsigned long safe_compute_effective_address(struct pt_regs *,
203 static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
210 if (regs->psr & PSR_PS) {
211 insn = *(unsigned int *) regs->pc;
213 __get_user(insn, (unsigned int *) regs->pc);
216 return safe_compute_effective_address(regs, insn);
219 asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
220 unsigned long address)
222 struct vm_area_struct *vma;
223 struct task_struct *tsk = current;
224 struct mm_struct *mm = tsk->mm;
228 int from_user = !(regs->psr & PSR_PS);
235 * We fault-in kernel-space virtual memory on-demand. The
236 * 'reference' page table is init_mm.pgd.
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,
243 if (!ARCH_SUN4C_SUN4 && address >= TASK_SIZE)
246 info.si_code = SEGV_MAPERR;
249 * If we're in an interrupt or have no user
250 * context, we must not take the fault..
252 if (in_atomic() || !mm)
255 down_read(&mm->mmap_sem);
258 * The kernel referencing a bad kernel pointer can lock up
259 * a sun4c machine completely, so we must attempt recovery.
261 if(!from_user && address >= PAGE_OFFSET)
264 vma = find_vma(mm, address);
267 if(vma->vm_start <= address)
269 if(!(vma->vm_flags & VM_GROWSDOWN))
271 if(expand_stack(vma, address))
274 * Ok, we have a good vm_area for this memory access, so
278 info.si_code = SEGV_ACCERR;
280 if(!(vma->vm_flags & VM_WRITE))
283 /* Allow reads even for write-only mappings */
284 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
289 * If for any reason at all we couldn't handle the fault,
290 * make sure we exit gracefully rather than endlessly redo
293 fault = handle_mm_fault(mm, vma, address, write);
294 if (unlikely(fault & VM_FAULT_ERROR)) {
295 if (fault & VM_FAULT_OOM)
297 else if (fault & VM_FAULT_SIGBUS)
301 if (fault & VM_FAULT_MAJOR)
305 up_read(&mm->mmap_sem);
309 * Something tried to access memory that isn't in our memory map..
310 * Fix it, but check if it's kernel or user first..
313 up_read(&mm->mmap_sem);
315 bad_area_nosemaphore:
316 /* User mode accesses just cause a SIGSEGV */
319 printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
320 tsk->comm, tsk->pid, address, regs->pc);
322 info.si_signo = SIGSEGV;
324 /* info.si_code set above to make clear whether
325 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
326 info.si_addr = (void __user *)compute_si_addr(regs, text_fault);
328 force_sig_info (SIGSEGV, &info, tsk);
332 /* Is this in ex_table? */
334 g2 = regs->u_regs[UREG_G2];
335 if (!from_user && (fixup = search_extables_range(regs->pc, &g2))) {
336 if (fixup > 10) { /* Values below are reserved for other things */
337 extern const unsigned __memset_start[];
338 extern const unsigned __memset_end[];
339 extern const unsigned __csum_partial_copy_start[];
340 extern const unsigned __csum_partial_copy_end[];
342 #ifdef DEBUG_EXCEPTIONS
343 printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
344 printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
345 regs->pc, fixup, g2);
347 if ((regs->pc >= (unsigned long)__memset_start &&
348 regs->pc < (unsigned long)__memset_end) ||
349 (regs->pc >= (unsigned long)__csum_partial_copy_start &&
350 regs->pc < (unsigned long)__csum_partial_copy_end)) {
351 regs->u_regs[UREG_I4] = address;
352 regs->u_regs[UREG_I5] = regs->pc;
354 regs->u_regs[UREG_G2] = g2;
356 regs->npc = regs->pc + 4;
361 unhandled_fault (address, tsk, regs);
365 * We ran out of memory, or some other thing happened to us that made
366 * us unable to handle the page fault gracefully.
369 up_read(&mm->mmap_sem);
370 printk("VM: killing process %s\n", tsk->comm);
372 do_group_exit(SIGKILL);
376 up_read(&mm->mmap_sem);
377 info.si_signo = SIGBUS;
379 info.si_code = BUS_ADRERR;
380 info.si_addr = (void __user *) compute_si_addr(regs, text_fault);
382 force_sig_info (SIGBUS, &info, tsk);
389 * Synchronize this task's top level page-table
390 * with the 'reference' page table.
392 int offset = pgd_index(address);
396 pgd = tsk->active_mm->pgd + offset;
397 pgd_k = init_mm.pgd + offset;
399 if (!pgd_present(*pgd)) {
400 if (!pgd_present(*pgd_k))
401 goto bad_area_nosemaphore;
402 pgd_val(*pgd) = pgd_val(*pgd_k);
406 pmd = pmd_offset(pgd, address);
407 pmd_k = pmd_offset(pgd_k, address);
409 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
410 goto bad_area_nosemaphore;
416 asmlinkage void do_sun4c_fault(struct pt_regs *regs, int text_fault, int write,
417 unsigned long address)
419 extern void sun4c_update_mmu_cache(struct vm_area_struct *,
420 unsigned long,pte_t);
421 extern pte_t *sun4c_pte_offset_kernel(pmd_t *,unsigned long);
422 struct task_struct *tsk = current;
423 struct mm_struct *mm = tsk->mm;
430 !(regs->psr & PSR_PS)) {
431 unsigned int insn, __user *ip;
433 ip = (unsigned int __user *)regs->pc;
434 if (!get_user(insn, ip)) {
435 if ((insn & 0xc1680000) == 0xc0680000)
441 /* We are oopsing. */
442 do_sparc_fault(regs, text_fault, write, address);
443 BUG(); /* P3 Oops already, you bitch */
446 pgdp = pgd_offset(mm, address);
447 ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, address);
449 if (pgd_val(*pgdp)) {
451 if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
452 == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
455 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
456 _SUN4C_PAGE_MODIFIED |
460 local_irq_save(flags);
461 if (sun4c_get_segmap(address) != invalid_segment) {
462 sun4c_put_pte(address, pte_val(*ptep));
463 local_irq_restore(flags);
466 local_irq_restore(flags);
469 if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
470 == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
473 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
476 local_irq_save(flags);
477 if (sun4c_get_segmap(address) != invalid_segment) {
478 sun4c_put_pte(address, pte_val(*ptep));
479 local_irq_restore(flags);
482 local_irq_restore(flags);
487 /* This conditional is 'interesting'. */
488 if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
489 && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
490 /* Note: It is safe to not grab the MMAP semaphore here because
491 * we know that update_mmu_cache() will not sleep for
492 * any reason (at least not in the current implementation)
493 * and therefore there is no danger of another thread getting
494 * on the CPU and doing a shrink_mmap() on this vma.
496 sun4c_update_mmu_cache (find_vma(current->mm, address), address,
499 do_sparc_fault(regs, text_fault, write, address);
502 /* This always deals with user addresses. */
503 inline void force_user_fault(unsigned long address, int write)
505 struct vm_area_struct *vma;
506 struct task_struct *tsk = current;
507 struct mm_struct *mm = tsk->mm;
510 info.si_code = SEGV_MAPERR;
513 printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
514 tsk->pid, write, address);
516 down_read(&mm->mmap_sem);
517 vma = find_vma(mm, address);
520 if(vma->vm_start <= address)
522 if(!(vma->vm_flags & VM_GROWSDOWN))
524 if(expand_stack(vma, address))
527 info.si_code = SEGV_ACCERR;
529 if(!(vma->vm_flags & VM_WRITE))
532 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
535 switch (handle_mm_fault(mm, vma, address, write)) {
536 case VM_FAULT_SIGBUS:
540 up_read(&mm->mmap_sem);
543 up_read(&mm->mmap_sem);
545 printk("Window whee %s [%d]: segfaults at %08lx\n",
546 tsk->comm, tsk->pid, address);
548 info.si_signo = SIGSEGV;
550 /* info.si_code set above to make clear whether
551 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
552 info.si_addr = (void __user *) address;
554 force_sig_info (SIGSEGV, &info, tsk);
558 up_read(&mm->mmap_sem);
559 info.si_signo = SIGBUS;
561 info.si_code = BUS_ADRERR;
562 info.si_addr = (void __user *) address;
564 force_sig_info (SIGBUS, &info, tsk);
567 void window_overflow_fault(void)
571 sp = current_thread_info()->rwbuf_stkptrs[0];
572 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
573 force_user_fault(sp + 0x38, 1);
574 force_user_fault(sp, 1);
577 void window_underflow_fault(unsigned long sp)
579 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
580 force_user_fault(sp + 0x38, 0);
581 force_user_fault(sp, 0);
584 void window_ret_fault(struct pt_regs *regs)
588 sp = regs->u_regs[UREG_FP];
589 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
590 force_user_fault(sp + 0x38, 0);
591 force_user_fault(sp, 0);