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 /* Return how much physical memory we have. */
51 unsigned long probe_memory(void)
53 unsigned long total = 0;
56 for (i = 0; sp_banks[i].num_bytes; i++)
57 total += sp_banks[i].num_bytes;
62 extern void sun4c_complete_all_stores(void);
64 /* Whee, a level 15 NMI interrupt memory error. Let's have fun... */
65 asmlinkage void sparc_lvl15_nmi(struct pt_regs *regs, unsigned long serr,
66 unsigned long svaddr, unsigned long aerr,
69 sun4c_complete_all_stores();
70 printk("FAULT: NMI received\n");
71 printk("SREGS: Synchronous Error %08lx\n", serr);
72 printk(" Synchronous Vaddr %08lx\n", svaddr);
73 printk(" Asynchronous Error %08lx\n", aerr);
74 printk(" Asynchronous Vaddr %08lx\n", avaddr);
76 printk(" Memory Parity Error %08lx\n", *sun4c_memerr_reg);
77 printk("REGISTER DUMP:\n");
82 static void unhandled_fault(unsigned long, struct task_struct *,
83 struct pt_regs *) __attribute__ ((noreturn));
85 static void unhandled_fault(unsigned long address, struct task_struct *tsk,
88 if((unsigned long) address < PAGE_SIZE) {
90 "Unable to handle kernel NULL pointer dereference\n");
92 printk(KERN_ALERT "Unable to handle kernel paging request "
93 "at virtual address %08lx\n", address);
95 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
96 (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
97 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
98 (tsk->mm ? (unsigned long) tsk->mm->pgd :
99 (unsigned long) tsk->active_mm->pgd));
100 die_if_kernel("Oops", regs);
103 asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc,
104 unsigned long address)
111 i = search_extables_range(ret_pc, &g2);
114 /* load & store will be handled by fixup */
118 /* store will be handled by fixup, load will bump out */
119 /* for _to_ macros */
120 insn = *((unsigned int *) pc);
121 if ((insn >> 21) & 1)
126 /* load will be handled by fixup, store will bump out */
127 /* for _from_ macros */
128 insn = *((unsigned int *) pc);
129 if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
137 memset(®s, 0, sizeof (regs));
140 __asm__ __volatile__(
144 "nop\n" : "=r" (regs.psr));
145 unhandled_fault(address, current, ®s);
151 extern unsigned long safe_compute_effective_address(struct pt_regs *,
154 static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
161 if (regs->psr & PSR_PS) {
162 insn = *(unsigned int *) regs->pc;
164 __get_user(insn, (unsigned int *) regs->pc);
167 return safe_compute_effective_address(regs, insn);
170 asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
171 unsigned long address)
173 struct vm_area_struct *vma;
174 struct task_struct *tsk = current;
175 struct mm_struct *mm = tsk->mm;
179 int from_user = !(regs->psr & PSR_PS);
186 * We fault-in kernel-space virtual memory on-demand. The
187 * 'reference' page table is init_mm.pgd.
189 * NOTE! We MUST NOT take any locks for this case. We may
190 * be in an interrupt or a critical region, and should
191 * only copy the information from the master page table,
194 if (!ARCH_SUN4C && address >= TASK_SIZE)
197 info.si_code = SEGV_MAPERR;
200 * If we're in an interrupt or have no user
201 * context, we must not take the fault..
203 if (in_atomic() || !mm)
206 down_read(&mm->mmap_sem);
209 * The kernel referencing a bad kernel pointer can lock up
210 * a sun4c machine completely, so we must attempt recovery.
212 if(!from_user && address >= PAGE_OFFSET)
215 vma = find_vma(mm, address);
218 if(vma->vm_start <= address)
220 if(!(vma->vm_flags & VM_GROWSDOWN))
222 if(expand_stack(vma, address))
225 * Ok, we have a good vm_area for this memory access, so
229 info.si_code = SEGV_ACCERR;
231 if(!(vma->vm_flags & VM_WRITE))
234 /* Allow reads even for write-only mappings */
235 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
240 * If for any reason at all we couldn't handle the fault,
241 * make sure we exit gracefully rather than endlessly redo
244 fault = handle_mm_fault(mm, vma, address, write);
245 if (unlikely(fault & VM_FAULT_ERROR)) {
246 if (fault & VM_FAULT_OOM)
248 else if (fault & VM_FAULT_SIGBUS)
252 if (fault & VM_FAULT_MAJOR)
256 up_read(&mm->mmap_sem);
260 * Something tried to access memory that isn't in our memory map..
261 * Fix it, but check if it's kernel or user first..
264 up_read(&mm->mmap_sem);
266 bad_area_nosemaphore:
267 /* User mode accesses just cause a SIGSEGV */
270 printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
271 tsk->comm, tsk->pid, address, regs->pc);
273 info.si_signo = SIGSEGV;
275 /* info.si_code set above to make clear whether
276 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
277 info.si_addr = (void __user *)compute_si_addr(regs, text_fault);
279 force_sig_info (SIGSEGV, &info, tsk);
283 /* Is this in ex_table? */
285 g2 = regs->u_regs[UREG_G2];
287 fixup = search_extables_range(regs->pc, &g2);
288 if (fixup > 10) { /* Values below are reserved for other things */
289 extern const unsigned __memset_start[];
290 extern const unsigned __memset_end[];
291 extern const unsigned __csum_partial_copy_start[];
292 extern const unsigned __csum_partial_copy_end[];
294 #ifdef DEBUG_EXCEPTIONS
295 printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
296 printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
297 regs->pc, fixup, g2);
299 if ((regs->pc >= (unsigned long)__memset_start &&
300 regs->pc < (unsigned long)__memset_end) ||
301 (regs->pc >= (unsigned long)__csum_partial_copy_start &&
302 regs->pc < (unsigned long)__csum_partial_copy_end)) {
303 regs->u_regs[UREG_I4] = address;
304 regs->u_regs[UREG_I5] = regs->pc;
306 regs->u_regs[UREG_G2] = g2;
308 regs->npc = regs->pc + 4;
313 unhandled_fault (address, tsk, regs);
317 * We ran out of memory, or some other thing happened to us that made
318 * us unable to handle the page fault gracefully.
321 up_read(&mm->mmap_sem);
322 printk("VM: killing process %s\n", tsk->comm);
324 do_group_exit(SIGKILL);
328 up_read(&mm->mmap_sem);
329 info.si_signo = SIGBUS;
331 info.si_code = BUS_ADRERR;
332 info.si_addr = (void __user *) compute_si_addr(regs, text_fault);
334 force_sig_info (SIGBUS, &info, tsk);
341 * Synchronize this task's top level page-table
342 * with the 'reference' page table.
344 int offset = pgd_index(address);
348 pgd = tsk->active_mm->pgd + offset;
349 pgd_k = init_mm.pgd + offset;
351 if (!pgd_present(*pgd)) {
352 if (!pgd_present(*pgd_k))
353 goto bad_area_nosemaphore;
354 pgd_val(*pgd) = pgd_val(*pgd_k);
358 pmd = pmd_offset(pgd, address);
359 pmd_k = pmd_offset(pgd_k, address);
361 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
362 goto bad_area_nosemaphore;
368 asmlinkage void do_sun4c_fault(struct pt_regs *regs, int text_fault, int write,
369 unsigned long address)
371 extern void sun4c_update_mmu_cache(struct vm_area_struct *,
372 unsigned long,pte_t);
373 extern pte_t *sun4c_pte_offset_kernel(pmd_t *,unsigned long);
374 struct task_struct *tsk = current;
375 struct mm_struct *mm = tsk->mm;
382 !(regs->psr & PSR_PS)) {
383 unsigned int insn, __user *ip;
385 ip = (unsigned int __user *)regs->pc;
386 if (!get_user(insn, ip)) {
387 if ((insn & 0xc1680000) == 0xc0680000)
393 /* We are oopsing. */
394 do_sparc_fault(regs, text_fault, write, address);
395 BUG(); /* P3 Oops already, you bitch */
398 pgdp = pgd_offset(mm, address);
399 ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, address);
401 if (pgd_val(*pgdp)) {
403 if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
404 == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
407 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
408 _SUN4C_PAGE_MODIFIED |
412 local_irq_save(flags);
413 if (sun4c_get_segmap(address) != invalid_segment) {
414 sun4c_put_pte(address, pte_val(*ptep));
415 local_irq_restore(flags);
418 local_irq_restore(flags);
421 if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
422 == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
425 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
428 local_irq_save(flags);
429 if (sun4c_get_segmap(address) != invalid_segment) {
430 sun4c_put_pte(address, pte_val(*ptep));
431 local_irq_restore(flags);
434 local_irq_restore(flags);
439 /* This conditional is 'interesting'. */
440 if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
441 && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
442 /* Note: It is safe to not grab the MMAP semaphore here because
443 * we know that update_mmu_cache() will not sleep for
444 * any reason (at least not in the current implementation)
445 * and therefore there is no danger of another thread getting
446 * on the CPU and doing a shrink_mmap() on this vma.
448 sun4c_update_mmu_cache (find_vma(current->mm, address), address,
451 do_sparc_fault(regs, text_fault, write, address);
454 /* This always deals with user addresses. */
455 static void force_user_fault(unsigned long address, int write)
457 struct vm_area_struct *vma;
458 struct task_struct *tsk = current;
459 struct mm_struct *mm = tsk->mm;
462 info.si_code = SEGV_MAPERR;
465 printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
466 tsk->pid, write, address);
468 down_read(&mm->mmap_sem);
469 vma = find_vma(mm, address);
472 if(vma->vm_start <= address)
474 if(!(vma->vm_flags & VM_GROWSDOWN))
476 if(expand_stack(vma, address))
479 info.si_code = SEGV_ACCERR;
481 if(!(vma->vm_flags & VM_WRITE))
484 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
487 switch (handle_mm_fault(mm, vma, address, write)) {
488 case VM_FAULT_SIGBUS:
492 up_read(&mm->mmap_sem);
495 up_read(&mm->mmap_sem);
497 printk("Window whee %s [%d]: segfaults at %08lx\n",
498 tsk->comm, tsk->pid, address);
500 info.si_signo = SIGSEGV;
502 /* info.si_code set above to make clear whether
503 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
504 info.si_addr = (void __user *) address;
506 force_sig_info (SIGSEGV, &info, tsk);
510 up_read(&mm->mmap_sem);
511 info.si_signo = SIGBUS;
513 info.si_code = BUS_ADRERR;
514 info.si_addr = (void __user *) address;
516 force_sig_info (SIGBUS, &info, tsk);
519 void window_overflow_fault(void)
523 sp = current_thread_info()->rwbuf_stkptrs[0];
524 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
525 force_user_fault(sp + 0x38, 1);
526 force_user_fault(sp, 1);
529 void window_underflow_fault(unsigned long sp)
531 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
532 force_user_fault(sp + 0x38, 0);
533 force_user_fault(sp, 0);
536 void window_ret_fault(struct pt_regs *regs)
540 sp = regs->u_regs[UREG_FP];
541 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
542 force_user_fault(sp + 0x38, 0);
543 force_user_fault(sp, 0);