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/perf_event.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/kdebug.h>
26 #include <asm/system.h>
28 #include <asm/pgtable.h>
29 #include <asm/memreg.h>
30 #include <asm/openprom.h>
31 #include <asm/oplib.h>
33 #include <asm/traps.h>
34 #include <asm/uaccess.h>
36 extern int prom_node_root;
38 /* At boot time we determine these two values necessary for setting
39 * up the segment maps and page table entries (pte's).
42 int num_segmaps, num_contexts;
45 /* various Virtual Address Cache parameters we find at boot time... */
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;
51 /* Return how much physical memory we have. */
52 unsigned long probe_memory(void)
54 unsigned long total = 0;
57 for (i = 0; sp_banks[i].num_bytes; i++)
58 total += sp_banks[i].num_bytes;
63 extern void sun4c_complete_all_stores(void);
65 /* Whee, a level 15 NMI interrupt memory error. Let's have fun... */
66 asmlinkage void sparc_lvl15_nmi(struct pt_regs *regs, unsigned long serr,
67 unsigned long svaddr, unsigned long aerr,
70 sun4c_complete_all_stores();
71 printk("FAULT: NMI received\n");
72 printk("SREGS: Synchronous Error %08lx\n", serr);
73 printk(" Synchronous Vaddr %08lx\n", svaddr);
74 printk(" Asynchronous Error %08lx\n", aerr);
75 printk(" Asynchronous Vaddr %08lx\n", avaddr);
77 printk(" Memory Parity Error %08lx\n", *sun4c_memerr_reg);
78 printk("REGISTER DUMP:\n");
83 static void unhandled_fault(unsigned long, struct task_struct *,
84 struct pt_regs *) __attribute__ ((noreturn));
86 static void unhandled_fault(unsigned long address, struct task_struct *tsk,
89 if((unsigned long) address < PAGE_SIZE) {
91 "Unable to handle kernel NULL pointer dereference\n");
93 printk(KERN_ALERT "Unable to handle kernel paging request "
94 "at virtual address %08lx\n", address);
96 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
97 (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
98 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
99 (tsk->mm ? (unsigned long) tsk->mm->pgd :
100 (unsigned long) tsk->active_mm->pgd));
101 die_if_kernel("Oops", regs);
104 asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc,
105 unsigned long address)
112 i = search_extables_range(ret_pc, &g2);
115 /* load & store will be handled by fixup */
119 /* store will be handled by fixup, load will bump out */
120 /* for _to_ macros */
121 insn = *((unsigned int *) pc);
122 if ((insn >> 21) & 1)
127 /* load will be handled by fixup, store will bump out */
128 /* for _from_ macros */
129 insn = *((unsigned int *) pc);
130 if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
138 memset(®s, 0, sizeof (regs));
141 __asm__ __volatile__(
145 "nop\n" : "=r" (regs.psr));
146 unhandled_fault(address, current, ®s);
152 extern unsigned long safe_compute_effective_address(struct pt_regs *,
155 static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
162 if (regs->psr & PSR_PS) {
163 insn = *(unsigned int *) regs->pc;
165 __get_user(insn, (unsigned int *) regs->pc);
168 return safe_compute_effective_address(regs, insn);
171 asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
172 unsigned long address)
174 struct vm_area_struct *vma;
175 struct task_struct *tsk = current;
176 struct mm_struct *mm = tsk->mm;
180 int from_user = !(regs->psr & PSR_PS);
187 * We fault-in kernel-space virtual memory on-demand. The
188 * 'reference' page table is init_mm.pgd.
190 * NOTE! We MUST NOT take any locks for this case. We may
191 * be in an interrupt or a critical region, and should
192 * only copy the information from the master page table,
195 if (!ARCH_SUN4C && address >= TASK_SIZE)
198 info.si_code = SEGV_MAPERR;
201 * If we're in an interrupt or have no user
202 * context, we must not take the fault..
204 if (in_atomic() || !mm)
207 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
209 down_read(&mm->mmap_sem);
212 * The kernel referencing a bad kernel pointer can lock up
213 * a sun4c machine completely, so we must attempt recovery.
215 if(!from_user && address >= PAGE_OFFSET)
218 vma = find_vma(mm, address);
221 if(vma->vm_start <= address)
223 if(!(vma->vm_flags & VM_GROWSDOWN))
225 if(expand_stack(vma, address))
228 * Ok, we have a good vm_area for this memory access, so
232 info.si_code = SEGV_ACCERR;
234 if(!(vma->vm_flags & VM_WRITE))
237 /* Allow reads even for write-only mappings */
238 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
243 * If for any reason at all we couldn't handle the fault,
244 * make sure we exit gracefully rather than endlessly redo
247 fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
248 if (unlikely(fault & VM_FAULT_ERROR)) {
249 if (fault & VM_FAULT_OOM)
251 else if (fault & VM_FAULT_SIGBUS)
255 if (fault & VM_FAULT_MAJOR) {
257 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
261 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
264 up_read(&mm->mmap_sem);
268 * Something tried to access memory that isn't in our memory map..
269 * Fix it, but check if it's kernel or user first..
272 up_read(&mm->mmap_sem);
274 bad_area_nosemaphore:
275 /* User mode accesses just cause a SIGSEGV */
278 printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
279 tsk->comm, tsk->pid, address, regs->pc);
281 info.si_signo = SIGSEGV;
283 /* info.si_code set above to make clear whether
284 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
285 info.si_addr = (void __user *)compute_si_addr(regs, text_fault);
287 force_sig_info (SIGSEGV, &info, tsk);
291 /* Is this in ex_table? */
293 g2 = regs->u_regs[UREG_G2];
295 fixup = search_extables_range(regs->pc, &g2);
296 if (fixup > 10) { /* Values below are reserved for other things */
297 extern const unsigned __memset_start[];
298 extern const unsigned __memset_end[];
299 extern const unsigned __csum_partial_copy_start[];
300 extern const unsigned __csum_partial_copy_end[];
302 #ifdef DEBUG_EXCEPTIONS
303 printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
304 printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
305 regs->pc, fixup, g2);
307 if ((regs->pc >= (unsigned long)__memset_start &&
308 regs->pc < (unsigned long)__memset_end) ||
309 (regs->pc >= (unsigned long)__csum_partial_copy_start &&
310 regs->pc < (unsigned long)__csum_partial_copy_end)) {
311 regs->u_regs[UREG_I4] = address;
312 regs->u_regs[UREG_I5] = regs->pc;
314 regs->u_regs[UREG_G2] = g2;
316 regs->npc = regs->pc + 4;
321 unhandled_fault (address, tsk, regs);
325 * We ran out of memory, or some other thing happened to us that made
326 * us unable to handle the page fault gracefully.
329 up_read(&mm->mmap_sem);
331 pagefault_out_of_memory();
337 up_read(&mm->mmap_sem);
338 info.si_signo = SIGBUS;
340 info.si_code = BUS_ADRERR;
341 info.si_addr = (void __user *) compute_si_addr(regs, text_fault);
343 force_sig_info (SIGBUS, &info, tsk);
350 * Synchronize this task's top level page-table
351 * with the 'reference' page table.
353 int offset = pgd_index(address);
357 pgd = tsk->active_mm->pgd + offset;
358 pgd_k = init_mm.pgd + offset;
360 if (!pgd_present(*pgd)) {
361 if (!pgd_present(*pgd_k))
362 goto bad_area_nosemaphore;
363 pgd_val(*pgd) = pgd_val(*pgd_k);
367 pmd = pmd_offset(pgd, address);
368 pmd_k = pmd_offset(pgd_k, address);
370 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
371 goto bad_area_nosemaphore;
377 asmlinkage void do_sun4c_fault(struct pt_regs *regs, int text_fault, int write,
378 unsigned long address)
380 extern void sun4c_update_mmu_cache(struct vm_area_struct *,
381 unsigned long,pte_t *);
382 extern pte_t *sun4c_pte_offset_kernel(pmd_t *,unsigned long);
383 struct task_struct *tsk = current;
384 struct mm_struct *mm = tsk->mm;
391 !(regs->psr & PSR_PS)) {
392 unsigned int insn, __user *ip;
394 ip = (unsigned int __user *)regs->pc;
395 if (!get_user(insn, ip)) {
396 if ((insn & 0xc1680000) == 0xc0680000)
402 /* We are oopsing. */
403 do_sparc_fault(regs, text_fault, write, address);
404 BUG(); /* P3 Oops already, you bitch */
407 pgdp = pgd_offset(mm, address);
408 ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, address);
410 if (pgd_val(*pgdp)) {
412 if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
413 == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
416 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
417 _SUN4C_PAGE_MODIFIED |
421 local_irq_save(flags);
422 if (sun4c_get_segmap(address) != invalid_segment) {
423 sun4c_put_pte(address, pte_val(*ptep));
424 local_irq_restore(flags);
427 local_irq_restore(flags);
430 if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
431 == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
434 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
437 local_irq_save(flags);
438 if (sun4c_get_segmap(address) != invalid_segment) {
439 sun4c_put_pte(address, pte_val(*ptep));
440 local_irq_restore(flags);
443 local_irq_restore(flags);
448 /* This conditional is 'interesting'. */
449 if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
450 && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
451 /* Note: It is safe to not grab the MMAP semaphore here because
452 * we know that update_mmu_cache() will not sleep for
453 * any reason (at least not in the current implementation)
454 * and therefore there is no danger of another thread getting
455 * on the CPU and doing a shrink_mmap() on this vma.
457 sun4c_update_mmu_cache (find_vma(current->mm, address), address,
460 do_sparc_fault(regs, text_fault, write, address);
463 /* This always deals with user addresses. */
464 static void force_user_fault(unsigned long address, int write)
466 struct vm_area_struct *vma;
467 struct task_struct *tsk = current;
468 struct mm_struct *mm = tsk->mm;
471 info.si_code = SEGV_MAPERR;
474 printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
475 tsk->pid, write, address);
477 down_read(&mm->mmap_sem);
478 vma = find_vma(mm, address);
481 if(vma->vm_start <= address)
483 if(!(vma->vm_flags & VM_GROWSDOWN))
485 if(expand_stack(vma, address))
488 info.si_code = SEGV_ACCERR;
490 if(!(vma->vm_flags & VM_WRITE))
493 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
496 switch (handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0)) {
497 case VM_FAULT_SIGBUS:
501 up_read(&mm->mmap_sem);
504 up_read(&mm->mmap_sem);
506 printk("Window whee %s [%d]: segfaults at %08lx\n",
507 tsk->comm, tsk->pid, address);
509 info.si_signo = SIGSEGV;
511 /* info.si_code set above to make clear whether
512 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
513 info.si_addr = (void __user *) address;
515 force_sig_info (SIGSEGV, &info, tsk);
519 up_read(&mm->mmap_sem);
520 info.si_signo = SIGBUS;
522 info.si_code = BUS_ADRERR;
523 info.si_addr = (void __user *) address;
525 force_sig_info (SIGBUS, &info, tsk);
528 void window_overflow_fault(void)
532 sp = current_thread_info()->rwbuf_stkptrs[0];
533 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
534 force_user_fault(sp + 0x38, 1);
535 force_user_fault(sp, 1);
538 void window_underflow_fault(unsigned long sp)
540 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
541 force_user_fault(sp + 0x38, 0);
542 force_user_fault(sp, 0);
545 void window_ret_fault(struct pt_regs *regs)
549 sp = regs->u_regs[UREG_FP];
550 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
551 force_user_fault(sp + 0x38, 0);
552 force_user_fault(sp, 0);