1 /* $Id: fault.c,v 1.59 2002/02/09 19:49:31 davem Exp $
2 * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
10 #include <linux/string.h>
11 #include <linux/types.h>
12 #include <linux/sched.h>
13 #include <linux/ptrace.h>
14 #include <linux/mman.h>
15 #include <linux/signal.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/kprobes.h>
21 #include <linux/kallsyms.h>
22 #include <linux/kdebug.h>
25 #include <asm/pgtable.h>
26 #include <asm/openprom.h>
27 #include <asm/oplib.h>
28 #include <asm/uaccess.h>
31 #include <asm/sections.h>
32 #include <asm/mmu_context.h>
35 static inline int notify_page_fault(struct pt_regs *regs)
39 /* kprobe_running() needs smp_processor_id() */
40 if (!user_mode(regs)) {
42 if (kprobe_running() && kprobe_fault_handler(regs, 0))
49 static inline int notify_page_fault(struct pt_regs *regs)
56 * To debug kernel to catch accesses to certain virtual/physical addresses.
57 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
58 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
59 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
60 * watched. This is only useful on a single cpu machine for now. After the watchpoint
61 * is detected, the process causing it will be killed, thus preventing an infinite loop.
63 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
65 unsigned long lsubits;
67 __asm__ __volatile__("ldxa [%%g0] %1, %0"
69 : "i" (ASI_LSU_CONTROL));
70 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
71 LSU_CONTROL_PR | LSU_CONTROL_VR |
72 LSU_CONTROL_PW | LSU_CONTROL_VW);
74 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
77 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
80 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
82 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
84 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
85 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
88 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
92 static void __kprobes unhandled_fault(unsigned long address,
93 struct task_struct *tsk,
96 if ((unsigned long) address < PAGE_SIZE) {
97 printk(KERN_ALERT "Unable to handle kernel NULL "
98 "pointer dereference\n");
100 printk(KERN_ALERT "Unable to handle kernel paging request "
101 "at virtual address %016lx\n", (unsigned long)address);
103 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
105 CTX_HWBITS(tsk->mm->context) :
106 CTX_HWBITS(tsk->active_mm->context)));
107 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
108 (tsk->mm ? (unsigned long) tsk->mm->pgd :
109 (unsigned long) tsk->active_mm->pgd));
110 die_if_kernel("Oops", regs);
113 static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
117 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
119 printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
120 print_symbol("RPC: <%s>\n", regs->u_regs[15]);
121 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
122 __asm__("mov %%sp, %0" : "=r" (ksp));
123 show_stack(current, ksp);
124 unhandled_fault(regs->tpc, current, regs);
128 * We now make sure that mmap_sem is held in all paths that call
129 * this. Additionally, to prevent kswapd from ripping ptes from
130 * under us, raise interrupts around the time that we look at the
131 * pte, kswapd will have to wait to get his smp ipi response from
132 * us. vmtruncate likewise. This saves us having to get pte lock.
134 static unsigned int get_user_insn(unsigned long tpc)
136 pgd_t *pgdp = pgd_offset(current->mm, tpc);
142 unsigned long pstate;
146 pudp = pud_offset(pgdp, tpc);
149 pmdp = pmd_offset(pudp, tpc);
153 /* This disables preemption for us as well. */
154 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
155 __asm__ __volatile__("wrpr %0, %1, %%pstate"
156 : : "r" (pstate), "i" (PSTATE_IE));
157 ptep = pte_offset_map(pmdp, tpc);
159 if (!pte_present(pte))
162 pa = (pte_pfn(pte) << PAGE_SHIFT);
163 pa += (tpc & ~PAGE_MASK);
165 /* Use phys bypass so we don't pollute dtlb/dcache. */
166 __asm__ __volatile__("lduwa [%1] %2, %0"
168 : "r" (pa), "i" (ASI_PHYS_USE_EC));
172 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
177 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
179 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
180 unsigned int insn, int fault_code)
187 if (fault_code & FAULT_CODE_ITLB)
188 info.si_addr = (void __user *) regs->tpc;
190 info.si_addr = (void __user *)
191 compute_effective_address(regs, insn, 0);
193 force_sig_info(sig, &info, current);
196 extern int handle_ldf_stq(u32, struct pt_regs *);
197 extern int handle_ld_nf(u32, struct pt_regs *);
199 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
202 if (!regs->tpc || (regs->tpc & 0x3))
204 if (regs->tstate & TSTATE_PRIV) {
205 insn = *(unsigned int *) regs->tpc;
207 insn = get_user_insn(regs->tpc);
213 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
214 unsigned int insn, unsigned long address)
216 unsigned char asi = ASI_P;
218 if ((!insn) && (regs->tstate & TSTATE_PRIV))
221 /* If user insn could be read (thus insn is zero), that
222 * is fine. We will just gun down the process with a signal
226 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
227 (insn & 0xc0800000) == 0xc0800000) {
229 asi = (regs->tstate >> 24);
232 if ((asi & 0xf2) == 0x82) {
233 if (insn & 0x1000000) {
234 handle_ldf_stq(insn, regs);
236 /* This was a non-faulting load. Just clear the
237 * destination register(s) and continue with the next
240 handle_ld_nf(insn, regs);
246 /* Is this in ex_table? */
247 if (regs->tstate & TSTATE_PRIV) {
248 const struct exception_table_entry *entry;
250 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
252 asi = (regs->tstate >> 24);
257 /* Look in asi.h: All _S asis have LS bit set */
259 (entry = search_exception_tables(regs->tpc))) {
260 regs->tpc = entry->fixup;
261 regs->tnpc = regs->tpc + 4;
265 /* The si_code was set to make clear whether
266 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
268 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
273 unhandled_fault (address, current, regs);
276 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
278 struct mm_struct *mm = current->mm;
279 struct vm_area_struct *vma;
280 unsigned int insn = 0;
281 int si_code, fault_code, fault;
282 unsigned long address, mm_rss;
284 fault_code = get_thread_fault_code();
286 if (notify_page_fault(regs))
289 si_code = SEGV_MAPERR;
290 address = current_thread_info()->fault_address;
292 if ((fault_code & FAULT_CODE_ITLB) &&
293 (fault_code & FAULT_CODE_DTLB))
296 if (regs->tstate & TSTATE_PRIV) {
297 unsigned long tpc = regs->tpc;
299 /* Sanity check the PC. */
300 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
301 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
302 /* Valid, no problems... */
304 bad_kernel_pc(regs, address);
310 * If we're in an interrupt or have no user
311 * context, we must not take the fault..
313 if (in_atomic() || !mm)
316 if (test_thread_flag(TIF_32BIT)) {
317 if (!(regs->tstate & TSTATE_PRIV))
318 regs->tpc &= 0xffffffff;
319 address &= 0xffffffff;
322 if (!down_read_trylock(&mm->mmap_sem)) {
323 if ((regs->tstate & TSTATE_PRIV) &&
324 !search_exception_tables(regs->tpc)) {
325 insn = get_fault_insn(regs, insn);
326 goto handle_kernel_fault;
328 down_read(&mm->mmap_sem);
331 vma = find_vma(mm, address);
335 /* Pure DTLB misses do not tell us whether the fault causing
336 * load/store/atomic was a write or not, it only says that there
337 * was no match. So in such a case we (carefully) read the
338 * instruction to try and figure this out. It's an optimization
339 * so it's ok if we can't do this.
341 * Special hack, window spill/fill knows the exact fault type.
344 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
345 (vma->vm_flags & VM_WRITE) != 0) {
346 insn = get_fault_insn(regs, 0);
349 /* All loads, stores and atomics have bits 30 and 31 both set
350 * in the instruction. Bit 21 is set in all stores, but we
351 * have to avoid prefetches which also have bit 21 set.
353 if ((insn & 0xc0200000) == 0xc0200000 &&
354 (insn & 0x01780000) != 0x01680000) {
355 /* Don't bother updating thread struct value,
356 * because update_mmu_cache only cares which tlb
357 * the access came from.
359 fault_code |= FAULT_CODE_WRITE;
364 if (vma->vm_start <= address)
366 if (!(vma->vm_flags & VM_GROWSDOWN))
368 if (!(fault_code & FAULT_CODE_WRITE)) {
369 /* Non-faulting loads shouldn't expand stack. */
370 insn = get_fault_insn(regs, insn);
371 if ((insn & 0xc0800000) == 0xc0800000) {
375 asi = (regs->tstate >> 24);
378 if ((asi & 0xf2) == 0x82)
382 if (expand_stack(vma, address))
385 * Ok, we have a good vm_area for this memory access, so
389 si_code = SEGV_ACCERR;
391 /* If we took a ITLB miss on a non-executable page, catch
394 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
395 BUG_ON(address != regs->tpc);
396 BUG_ON(regs->tstate & TSTATE_PRIV);
400 if (fault_code & FAULT_CODE_WRITE) {
401 if (!(vma->vm_flags & VM_WRITE))
404 /* Spitfire has an icache which does not snoop
405 * processor stores. Later processors do...
407 if (tlb_type == spitfire &&
408 (vma->vm_flags & VM_EXEC) != 0 &&
409 vma->vm_file != NULL)
410 set_thread_fault_code(fault_code |
411 FAULT_CODE_BLKCOMMIT);
413 /* Allow reads even for write-only mappings */
414 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
418 fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE));
419 if (unlikely(fault & VM_FAULT_ERROR)) {
420 if (fault & VM_FAULT_OOM)
422 else if (fault & VM_FAULT_SIGBUS)
426 if (fault & VM_FAULT_MAJOR)
431 up_read(&mm->mmap_sem);
433 mm_rss = get_mm_rss(mm);
434 #ifdef CONFIG_HUGETLB_PAGE
435 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
437 if (unlikely(mm_rss >
438 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
439 tsb_grow(mm, MM_TSB_BASE, mm_rss);
440 #ifdef CONFIG_HUGETLB_PAGE
441 mm_rss = mm->context.huge_pte_count;
442 if (unlikely(mm_rss >
443 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
444 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
449 * Something tried to access memory that isn't in our memory map..
450 * Fix it, but check if it's kernel or user first..
453 insn = get_fault_insn(regs, insn);
454 up_read(&mm->mmap_sem);
457 do_kernel_fault(regs, si_code, fault_code, insn, address);
461 * We ran out of memory, or some other thing happened to us that made
462 * us unable to handle the page fault gracefully.
465 insn = get_fault_insn(regs, insn);
466 up_read(&mm->mmap_sem);
467 printk("VM: killing process %s\n", current->comm);
468 if (!(regs->tstate & TSTATE_PRIV))
470 goto handle_kernel_fault;
473 insn = get_fault_insn(regs, 0);
474 goto handle_kernel_fault;
477 insn = get_fault_insn(regs, insn);
478 up_read(&mm->mmap_sem);
481 * Send a sigbus, regardless of whether we were in kernel
484 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
486 /* Kernel mode? Handle exceptions or die */
487 if (regs->tstate & TSTATE_PRIV)
488 goto handle_kernel_fault;