2 * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
4 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
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/kdebug.h>
22 #include <linux/percpu.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>
34 static inline int notify_page_fault(struct pt_regs *regs)
38 /* kprobe_running() needs smp_processor_id() */
39 if (kprobes_built_in() && !user_mode(regs)) {
41 if (kprobe_running() && kprobe_fault_handler(regs, 0))
48 static void __kprobes unhandled_fault(unsigned long address,
49 struct task_struct *tsk,
52 if ((unsigned long) address < PAGE_SIZE) {
53 printk(KERN_ALERT "Unable to handle kernel NULL "
54 "pointer dereference\n");
56 printk(KERN_ALERT "Unable to handle kernel paging request "
57 "at virtual address %016lx\n", (unsigned long)address);
59 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
61 CTX_HWBITS(tsk->mm->context) :
62 CTX_HWBITS(tsk->active_mm->context)));
63 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
64 (tsk->mm ? (unsigned long) tsk->mm->pgd :
65 (unsigned long) tsk->active_mm->pgd));
66 die_if_kernel("Oops", regs);
69 static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
71 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
73 printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
74 printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
75 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
77 unhandled_fault(regs->tpc, current, regs);
81 * We now make sure that mmap_sem is held in all paths that call
82 * this. Additionally, to prevent kswapd from ripping ptes from
83 * under us, raise interrupts around the time that we look at the
84 * pte, kswapd will have to wait to get his smp ipi response from
85 * us. vmtruncate likewise. This saves us having to get pte lock.
87 static unsigned int get_user_insn(unsigned long tpc)
89 pgd_t *pgdp = pgd_offset(current->mm, tpc);
99 pudp = pud_offset(pgdp, tpc);
102 pmdp = pmd_offset(pudp, tpc);
106 /* This disables preemption for us as well. */
107 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
108 __asm__ __volatile__("wrpr %0, %1, %%pstate"
109 : : "r" (pstate), "i" (PSTATE_IE));
110 ptep = pte_offset_map(pmdp, tpc);
112 if (!pte_present(pte))
115 pa = (pte_pfn(pte) << PAGE_SHIFT);
116 pa += (tpc & ~PAGE_MASK);
118 /* Use phys bypass so we don't pollute dtlb/dcache. */
119 __asm__ __volatile__("lduwa [%1] %2, %0"
121 : "r" (pa), "i" (ASI_PHYS_USE_EC));
125 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
130 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
132 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
133 unsigned int insn, int fault_code)
140 if (fault_code & FAULT_CODE_ITLB)
141 info.si_addr = (void __user *) regs->tpc;
143 info.si_addr = (void __user *)
144 compute_effective_address(regs, insn, 0);
146 force_sig_info(sig, &info, current);
149 extern int handle_ldf_stq(u32, struct pt_regs *);
150 extern int handle_ld_nf(u32, struct pt_regs *);
152 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
155 if (!regs->tpc || (regs->tpc & 0x3))
157 if (regs->tstate & TSTATE_PRIV) {
158 insn = *(unsigned int *) regs->tpc;
160 insn = get_user_insn(regs->tpc);
166 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
167 unsigned int insn, unsigned long address)
169 unsigned char asi = ASI_P;
171 if ((!insn) && (regs->tstate & TSTATE_PRIV))
174 /* If user insn could be read (thus insn is zero), that
175 * is fine. We will just gun down the process with a signal
179 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
180 (insn & 0xc0800000) == 0xc0800000) {
182 asi = (regs->tstate >> 24);
185 if ((asi & 0xf2) == 0x82) {
186 if (insn & 0x1000000) {
187 handle_ldf_stq(insn, regs);
189 /* This was a non-faulting load. Just clear the
190 * destination register(s) and continue with the next
193 handle_ld_nf(insn, regs);
199 /* Is this in ex_table? */
200 if (regs->tstate & TSTATE_PRIV) {
201 const struct exception_table_entry *entry;
203 entry = search_exception_tables(regs->tpc);
205 regs->tpc = entry->fixup;
206 regs->tnpc = regs->tpc + 4;
210 /* The si_code was set to make clear whether
211 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
213 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
218 unhandled_fault (address, current, regs);
221 static void noinline bogus_32bit_fault_tpc(struct pt_regs *regs)
226 printk(KERN_ERR "FAULT[%s:%d]: 32-bit process reports "
227 "64-bit TPC [%lx]\n",
228 current->comm, current->pid,
233 static void noinline bogus_32bit_fault_address(struct pt_regs *regs,
239 printk(KERN_ERR "FAULT[%s:%d]: 32-bit process "
240 "reports 64-bit fault address [%lx]\n",
241 current->comm, current->pid, addr);
245 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
247 struct mm_struct *mm = current->mm;
248 struct vm_area_struct *vma;
249 unsigned int insn = 0;
250 int si_code, fault_code, fault;
251 unsigned long address, mm_rss;
253 fault_code = get_thread_fault_code();
255 if (notify_page_fault(regs))
258 si_code = SEGV_MAPERR;
259 address = current_thread_info()->fault_address;
261 if ((fault_code & FAULT_CODE_ITLB) &&
262 (fault_code & FAULT_CODE_DTLB))
265 if (test_thread_flag(TIF_32BIT)) {
266 if (!(regs->tstate & TSTATE_PRIV)) {
267 if (unlikely((regs->tpc >> 32) != 0)) {
268 bogus_32bit_fault_tpc(regs);
272 if (unlikely((address >> 32) != 0)) {
273 bogus_32bit_fault_address(regs, address);
278 if (regs->tstate & TSTATE_PRIV) {
279 unsigned long tpc = regs->tpc;
281 /* Sanity check the PC. */
282 if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
283 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
284 /* Valid, no problems... */
286 bad_kernel_pc(regs, address);
292 * If we're in an interrupt or have no user
293 * context, we must not take the fault..
295 if (in_atomic() || !mm)
298 if (!down_read_trylock(&mm->mmap_sem)) {
299 if ((regs->tstate & TSTATE_PRIV) &&
300 !search_exception_tables(regs->tpc)) {
301 insn = get_fault_insn(regs, insn);
302 goto handle_kernel_fault;
304 down_read(&mm->mmap_sem);
307 vma = find_vma(mm, address);
311 /* Pure DTLB misses do not tell us whether the fault causing
312 * load/store/atomic was a write or not, it only says that there
313 * was no match. So in such a case we (carefully) read the
314 * instruction to try and figure this out. It's an optimization
315 * so it's ok if we can't do this.
317 * Special hack, window spill/fill knows the exact fault type.
320 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
321 (vma->vm_flags & VM_WRITE) != 0) {
322 insn = get_fault_insn(regs, 0);
325 /* All loads, stores and atomics have bits 30 and 31 both set
326 * in the instruction. Bit 21 is set in all stores, but we
327 * have to avoid prefetches which also have bit 21 set.
329 if ((insn & 0xc0200000) == 0xc0200000 &&
330 (insn & 0x01780000) != 0x01680000) {
331 /* Don't bother updating thread struct value,
332 * because update_mmu_cache only cares which tlb
333 * the access came from.
335 fault_code |= FAULT_CODE_WRITE;
340 if (vma->vm_start <= address)
342 if (!(vma->vm_flags & VM_GROWSDOWN))
344 if (!(fault_code & FAULT_CODE_WRITE)) {
345 /* Non-faulting loads shouldn't expand stack. */
346 insn = get_fault_insn(regs, insn);
347 if ((insn & 0xc0800000) == 0xc0800000) {
351 asi = (regs->tstate >> 24);
354 if ((asi & 0xf2) == 0x82)
358 if (expand_stack(vma, address))
361 * Ok, we have a good vm_area for this memory access, so
365 si_code = SEGV_ACCERR;
367 /* If we took a ITLB miss on a non-executable page, catch
370 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
371 BUG_ON(address != regs->tpc);
372 BUG_ON(regs->tstate & TSTATE_PRIV);
376 if (fault_code & FAULT_CODE_WRITE) {
377 if (!(vma->vm_flags & VM_WRITE))
380 /* Spitfire has an icache which does not snoop
381 * processor stores. Later processors do...
383 if (tlb_type == spitfire &&
384 (vma->vm_flags & VM_EXEC) != 0 &&
385 vma->vm_file != NULL)
386 set_thread_fault_code(fault_code |
387 FAULT_CODE_BLKCOMMIT);
389 /* Allow reads even for write-only mappings */
390 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
394 fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE) ? FAULT_FLAG_WRITE : 0);
395 if (unlikely(fault & VM_FAULT_ERROR)) {
396 if (fault & VM_FAULT_OOM)
398 else if (fault & VM_FAULT_SIGBUS)
402 if (fault & VM_FAULT_MAJOR)
407 up_read(&mm->mmap_sem);
409 mm_rss = get_mm_rss(mm);
410 #ifdef CONFIG_HUGETLB_PAGE
411 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
413 if (unlikely(mm_rss >
414 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
415 tsb_grow(mm, MM_TSB_BASE, mm_rss);
416 #ifdef CONFIG_HUGETLB_PAGE
417 mm_rss = mm->context.huge_pte_count;
418 if (unlikely(mm_rss >
419 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
420 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
425 * Something tried to access memory that isn't in our memory map..
426 * Fix it, but check if it's kernel or user first..
429 insn = get_fault_insn(regs, insn);
430 up_read(&mm->mmap_sem);
433 do_kernel_fault(regs, si_code, fault_code, insn, address);
437 * We ran out of memory, or some other thing happened to us that made
438 * us unable to handle the page fault gracefully.
441 insn = get_fault_insn(regs, insn);
442 up_read(&mm->mmap_sem);
443 if (!(regs->tstate & TSTATE_PRIV)) {
444 pagefault_out_of_memory();
447 goto handle_kernel_fault;
450 insn = get_fault_insn(regs, 0);
451 goto handle_kernel_fault;
454 insn = get_fault_insn(regs, insn);
455 up_read(&mm->mmap_sem);
458 * Send a sigbus, regardless of whether we were in kernel
461 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
463 /* Kernel mode? Handle exceptions or die */
464 if (regs->tstate & TSTATE_PRIV)
465 goto handle_kernel_fault;