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/smp_lock.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/kprobes.h>
24 #include <asm/pgtable.h>
25 #include <asm/openprom.h>
26 #include <asm/oplib.h>
27 #include <asm/uaccess.h>
30 #include <asm/sections.h>
31 #include <asm/kdebug.h>
32 #include <asm/mmu_context.h>
35 * To debug kernel to catch accesses to certain virtual/physical addresses.
36 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
37 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
38 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
39 * watched. This is only useful on a single cpu machine for now. After the watchpoint
40 * is detected, the process causing it will be killed, thus preventing an infinite loop.
42 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
44 unsigned long lsubits;
46 __asm__ __volatile__("ldxa [%%g0] %1, %0"
48 : "i" (ASI_LSU_CONTROL));
49 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
50 LSU_CONTROL_PR | LSU_CONTROL_VR |
51 LSU_CONTROL_PW | LSU_CONTROL_VW);
53 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
56 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
59 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
61 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
63 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
64 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
67 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
71 static void __kprobes unhandled_fault(unsigned long address,
72 struct task_struct *tsk,
75 if ((unsigned long) address < PAGE_SIZE) {
76 printk(KERN_ALERT "Unable to handle kernel NULL "
77 "pointer dereference\n");
79 printk(KERN_ALERT "Unable to handle kernel paging request "
80 "at virtual address %016lx\n", (unsigned long)address);
82 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
84 CTX_HWBITS(tsk->mm->context) :
85 CTX_HWBITS(tsk->active_mm->context)));
86 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
87 (tsk->mm ? (unsigned long) tsk->mm->pgd :
88 (unsigned long) tsk->active_mm->pgd));
89 if (notify_die(DIE_GPF, "general protection fault", regs,
90 0, 0, SIGSEGV) == NOTIFY_STOP)
92 die_if_kernel("Oops", regs);
95 static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
99 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
101 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
102 __asm__("mov %%sp, %0" : "=r" (ksp));
103 show_stack(current, ksp);
104 unhandled_fault(regs->tpc, current, regs);
108 * We now make sure that mmap_sem is held in all paths that call
109 * this. Additionally, to prevent kswapd from ripping ptes from
110 * under us, raise interrupts around the time that we look at the
111 * pte, kswapd will have to wait to get his smp ipi response from
112 * us. vmtruncate likewise. This saves us having to get pte lock.
114 static unsigned int get_user_insn(unsigned long tpc)
116 pgd_t *pgdp = pgd_offset(current->mm, tpc);
122 unsigned long pstate;
126 pudp = pud_offset(pgdp, tpc);
129 pmdp = pmd_offset(pudp, tpc);
133 /* This disables preemption for us as well. */
134 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
135 __asm__ __volatile__("wrpr %0, %1, %%pstate"
136 : : "r" (pstate), "i" (PSTATE_IE));
137 ptep = pte_offset_map(pmdp, tpc);
139 if (!pte_present(pte))
142 pa = (pte_pfn(pte) << PAGE_SHIFT);
143 pa += (tpc & ~PAGE_MASK);
145 /* Use phys bypass so we don't pollute dtlb/dcache. */
146 __asm__ __volatile__("lduwa [%1] %2, %0"
148 : "r" (pa), "i" (ASI_PHYS_USE_EC));
152 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
157 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
159 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
160 unsigned int insn, int fault_code)
167 if (fault_code & FAULT_CODE_ITLB)
168 info.si_addr = (void __user *) regs->tpc;
170 info.si_addr = (void __user *)
171 compute_effective_address(regs, insn, 0);
173 force_sig_info(sig, &info, current);
176 extern int handle_ldf_stq(u32, struct pt_regs *);
177 extern int handle_ld_nf(u32, struct pt_regs *);
179 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
182 if (!regs->tpc || (regs->tpc & 0x3))
184 if (regs->tstate & TSTATE_PRIV) {
185 insn = *(unsigned int *) regs->tpc;
187 insn = get_user_insn(regs->tpc);
193 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
194 unsigned int insn, unsigned long address)
196 unsigned char asi = ASI_P;
198 if ((!insn) && (regs->tstate & TSTATE_PRIV))
201 /* If user insn could be read (thus insn is zero), that
202 * is fine. We will just gun down the process with a signal
206 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
207 (insn & 0xc0800000) == 0xc0800000) {
209 asi = (regs->tstate >> 24);
212 if ((asi & 0xf2) == 0x82) {
213 if (insn & 0x1000000) {
214 handle_ldf_stq(insn, regs);
216 /* This was a non-faulting load. Just clear the
217 * destination register(s) and continue with the next
220 handle_ld_nf(insn, regs);
226 /* Is this in ex_table? */
227 if (regs->tstate & TSTATE_PRIV) {
228 const struct exception_table_entry *entry;
230 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
232 asi = (regs->tstate >> 24);
237 /* Look in asi.h: All _S asis have LS bit set */
239 (entry = search_exception_tables(regs->tpc))) {
240 regs->tpc = entry->fixup;
241 regs->tnpc = regs->tpc + 4;
245 /* The si_code was set to make clear whether
246 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
248 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
253 unhandled_fault (address, current, regs);
256 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
258 struct mm_struct *mm = current->mm;
259 struct vm_area_struct *vma;
260 unsigned int insn = 0;
261 int si_code, fault_code;
262 unsigned long address, mm_rss;
264 fault_code = get_thread_fault_code();
266 if (notify_die(DIE_PAGE_FAULT, "page_fault", regs,
267 fault_code, 0, SIGSEGV) == NOTIFY_STOP)
270 si_code = SEGV_MAPERR;
271 address = current_thread_info()->fault_address;
273 if ((fault_code & FAULT_CODE_ITLB) &&
274 (fault_code & FAULT_CODE_DTLB))
277 if (regs->tstate & TSTATE_PRIV) {
278 unsigned long tpc = regs->tpc;
280 /* Sanity check the PC. */
281 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
282 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
283 /* Valid, no problems... */
285 bad_kernel_pc(regs, address);
291 * If we're in an interrupt or have no user
292 * context, we must not take the fault..
294 if (in_atomic() || !mm)
297 if (test_thread_flag(TIF_32BIT)) {
298 if (!(regs->tstate & TSTATE_PRIV))
299 regs->tpc &= 0xffffffff;
300 address &= 0xffffffff;
303 if (!down_read_trylock(&mm->mmap_sem)) {
304 if ((regs->tstate & TSTATE_PRIV) &&
305 !search_exception_tables(regs->tpc)) {
306 insn = get_fault_insn(regs, insn);
307 goto handle_kernel_fault;
309 down_read(&mm->mmap_sem);
312 vma = find_vma(mm, address);
316 /* Pure DTLB misses do not tell us whether the fault causing
317 * load/store/atomic was a write or not, it only says that there
318 * was no match. So in such a case we (carefully) read the
319 * instruction to try and figure this out. It's an optimization
320 * so it's ok if we can't do this.
322 * Special hack, window spill/fill knows the exact fault type.
325 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
326 (vma->vm_flags & VM_WRITE) != 0) {
327 insn = get_fault_insn(regs, 0);
330 if ((insn & 0xc0200000) == 0xc0200000 &&
331 (insn & 0x1780000) != 0x1680000) {
332 /* Don't bother updating thread struct value,
333 * because update_mmu_cache only cares which tlb
334 * the access came from.
336 fault_code |= FAULT_CODE_WRITE;
341 if (vma->vm_start <= address)
343 if (!(vma->vm_flags & VM_GROWSDOWN))
345 if (!(fault_code & FAULT_CODE_WRITE)) {
346 /* Non-faulting loads shouldn't expand stack. */
347 insn = get_fault_insn(regs, insn);
348 if ((insn & 0xc0800000) == 0xc0800000) {
352 asi = (regs->tstate >> 24);
355 if ((asi & 0xf2) == 0x82)
359 if (expand_stack(vma, address))
362 * Ok, we have a good vm_area for this memory access, so
366 si_code = SEGV_ACCERR;
368 /* If we took a ITLB miss on a non-executable page, catch
371 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
372 BUG_ON(address != regs->tpc);
373 BUG_ON(regs->tstate & TSTATE_PRIV);
377 if (fault_code & FAULT_CODE_WRITE) {
378 if (!(vma->vm_flags & VM_WRITE))
381 /* Spitfire has an icache which does not snoop
382 * processor stores. Later processors do...
384 if (tlb_type == spitfire &&
385 (vma->vm_flags & VM_EXEC) != 0 &&
386 vma->vm_file != NULL)
387 set_thread_fault_code(fault_code |
388 FAULT_CODE_BLKCOMMIT);
390 /* Allow reads even for write-only mappings */
391 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
395 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
402 case VM_FAULT_SIGBUS:
410 up_read(&mm->mmap_sem);
412 mm_rss = get_mm_rss(mm);
413 #ifdef CONFIG_HUGETLB_PAGE
414 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
416 if (unlikely(mm_rss >=
417 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
418 tsb_grow(mm, MM_TSB_BASE, mm_rss);
419 #ifdef CONFIG_HUGETLB_PAGE
420 mm_rss = mm->context.huge_pte_count;
421 if (unlikely(mm_rss >=
422 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
423 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
428 * Something tried to access memory that isn't in our memory map..
429 * Fix it, but check if it's kernel or user first..
432 insn = get_fault_insn(regs, insn);
433 up_read(&mm->mmap_sem);
436 do_kernel_fault(regs, si_code, fault_code, insn, address);
440 * We ran out of memory, or some other thing happened to us that made
441 * us unable to handle the page fault gracefully.
444 insn = get_fault_insn(regs, insn);
445 up_read(&mm->mmap_sem);
446 printk("VM: killing process %s\n", current->comm);
447 if (!(regs->tstate & TSTATE_PRIV))
449 goto handle_kernel_fault;
452 insn = get_fault_insn(regs, 0);
453 goto handle_kernel_fault;
456 insn = get_fault_insn(regs, insn);
457 up_read(&mm->mmap_sem);
460 * Send a sigbus, regardless of whether we were in kernel
463 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
465 /* Kernel mode? Handle exceptions or die */
466 if (regs->tstate & TSTATE_PRIV)
467 goto handle_kernel_fault;