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>
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/mmu_context.h>
34 static inline int notify_page_fault(struct pt_regs *regs)
38 /* kprobe_running() needs smp_processor_id() */
39 if (!user_mode(regs)) {
41 if (kprobe_running() && kprobe_fault_handler(regs, 0))
48 static inline int notify_page_fault(struct pt_regs *regs)
55 * To debug kernel to catch accesses to certain virtual/physical addresses.
56 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
57 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
58 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
59 * watched. This is only useful on a single cpu machine for now. After the watchpoint
60 * is detected, the process causing it will be killed, thus preventing an infinite loop.
62 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
64 unsigned long lsubits;
66 __asm__ __volatile__("ldxa [%%g0] %1, %0"
68 : "i" (ASI_LSU_CONTROL));
69 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
70 LSU_CONTROL_PR | LSU_CONTROL_VR |
71 LSU_CONTROL_PW | LSU_CONTROL_VW);
73 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
76 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
79 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
81 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
83 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
84 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
87 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
91 static void __kprobes unhandled_fault(unsigned long address,
92 struct task_struct *tsk,
95 if ((unsigned long) address < PAGE_SIZE) {
96 printk(KERN_ALERT "Unable to handle kernel NULL "
97 "pointer dereference\n");
99 printk(KERN_ALERT "Unable to handle kernel paging request "
100 "at virtual address %016lx\n", (unsigned long)address);
102 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
104 CTX_HWBITS(tsk->mm->context) :
105 CTX_HWBITS(tsk->active_mm->context)));
106 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
107 (tsk->mm ? (unsigned long) tsk->mm->pgd :
108 (unsigned long) tsk->active_mm->pgd));
109 die_if_kernel("Oops", regs);
112 static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
114 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
116 printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
117 printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
118 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
120 unhandled_fault(regs->tpc, current, regs);
124 * We now make sure that mmap_sem is held in all paths that call
125 * this. Additionally, to prevent kswapd from ripping ptes from
126 * under us, raise interrupts around the time that we look at the
127 * pte, kswapd will have to wait to get his smp ipi response from
128 * us. vmtruncate likewise. This saves us having to get pte lock.
130 static unsigned int get_user_insn(unsigned long tpc)
132 pgd_t *pgdp = pgd_offset(current->mm, tpc);
138 unsigned long pstate;
142 pudp = pud_offset(pgdp, tpc);
145 pmdp = pmd_offset(pudp, tpc);
149 /* This disables preemption for us as well. */
150 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
151 __asm__ __volatile__("wrpr %0, %1, %%pstate"
152 : : "r" (pstate), "i" (PSTATE_IE));
153 ptep = pte_offset_map(pmdp, tpc);
155 if (!pte_present(pte))
158 pa = (pte_pfn(pte) << PAGE_SHIFT);
159 pa += (tpc & ~PAGE_MASK);
161 /* Use phys bypass so we don't pollute dtlb/dcache. */
162 __asm__ __volatile__("lduwa [%1] %2, %0"
164 : "r" (pa), "i" (ASI_PHYS_USE_EC));
168 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
173 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
175 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
176 unsigned int insn, int fault_code)
183 if (fault_code & FAULT_CODE_ITLB)
184 info.si_addr = (void __user *) regs->tpc;
186 info.si_addr = (void __user *)
187 compute_effective_address(regs, insn, 0);
189 force_sig_info(sig, &info, current);
192 extern int handle_ldf_stq(u32, struct pt_regs *);
193 extern int handle_ld_nf(u32, struct pt_regs *);
195 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
198 if (!regs->tpc || (regs->tpc & 0x3))
200 if (regs->tstate & TSTATE_PRIV) {
201 insn = *(unsigned int *) regs->tpc;
203 insn = get_user_insn(regs->tpc);
209 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
210 unsigned int insn, unsigned long address)
212 unsigned char asi = ASI_P;
214 if ((!insn) && (regs->tstate & TSTATE_PRIV))
217 /* If user insn could be read (thus insn is zero), that
218 * is fine. We will just gun down the process with a signal
222 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
223 (insn & 0xc0800000) == 0xc0800000) {
225 asi = (regs->tstate >> 24);
228 if ((asi & 0xf2) == 0x82) {
229 if (insn & 0x1000000) {
230 handle_ldf_stq(insn, regs);
232 /* This was a non-faulting load. Just clear the
233 * destination register(s) and continue with the next
236 handle_ld_nf(insn, regs);
242 /* Is this in ex_table? */
243 if (regs->tstate & TSTATE_PRIV) {
244 const struct exception_table_entry *entry;
246 entry = search_exception_tables(regs->tpc);
248 regs->tpc = entry->fixup;
249 regs->tnpc = regs->tpc + 4;
253 /* The si_code was set to make clear whether
254 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
256 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
261 unhandled_fault (address, current, regs);
264 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
266 struct mm_struct *mm = current->mm;
267 struct vm_area_struct *vma;
268 unsigned int insn = 0;
269 int si_code, fault_code, fault;
270 unsigned long address, mm_rss;
272 fault_code = get_thread_fault_code();
274 if (notify_page_fault(regs))
277 si_code = SEGV_MAPERR;
278 address = current_thread_info()->fault_address;
280 if ((fault_code & FAULT_CODE_ITLB) &&
281 (fault_code & FAULT_CODE_DTLB))
284 if (regs->tstate & TSTATE_PRIV) {
285 unsigned long tpc = regs->tpc;
287 /* Sanity check the PC. */
288 if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
289 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
290 /* Valid, no problems... */
292 bad_kernel_pc(regs, address);
298 * If we're in an interrupt or have no user
299 * context, we must not take the fault..
301 if (in_atomic() || !mm)
304 if (test_thread_flag(TIF_32BIT)) {
305 if (!(regs->tstate & TSTATE_PRIV))
306 regs->tpc &= 0xffffffff;
307 address &= 0xffffffff;
310 if (!down_read_trylock(&mm->mmap_sem)) {
311 if ((regs->tstate & TSTATE_PRIV) &&
312 !search_exception_tables(regs->tpc)) {
313 insn = get_fault_insn(regs, insn);
314 goto handle_kernel_fault;
316 down_read(&mm->mmap_sem);
319 vma = find_vma(mm, address);
323 /* Pure DTLB misses do not tell us whether the fault causing
324 * load/store/atomic was a write or not, it only says that there
325 * was no match. So in such a case we (carefully) read the
326 * instruction to try and figure this out. It's an optimization
327 * so it's ok if we can't do this.
329 * Special hack, window spill/fill knows the exact fault type.
332 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
333 (vma->vm_flags & VM_WRITE) != 0) {
334 insn = get_fault_insn(regs, 0);
337 /* All loads, stores and atomics have bits 30 and 31 both set
338 * in the instruction. Bit 21 is set in all stores, but we
339 * have to avoid prefetches which also have bit 21 set.
341 if ((insn & 0xc0200000) == 0xc0200000 &&
342 (insn & 0x01780000) != 0x01680000) {
343 /* Don't bother updating thread struct value,
344 * because update_mmu_cache only cares which tlb
345 * the access came from.
347 fault_code |= FAULT_CODE_WRITE;
352 if (vma->vm_start <= address)
354 if (!(vma->vm_flags & VM_GROWSDOWN))
356 if (!(fault_code & FAULT_CODE_WRITE)) {
357 /* Non-faulting loads shouldn't expand stack. */
358 insn = get_fault_insn(regs, insn);
359 if ((insn & 0xc0800000) == 0xc0800000) {
363 asi = (regs->tstate >> 24);
366 if ((asi & 0xf2) == 0x82)
370 if (expand_stack(vma, address))
373 * Ok, we have a good vm_area for this memory access, so
377 si_code = SEGV_ACCERR;
379 /* If we took a ITLB miss on a non-executable page, catch
382 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
383 BUG_ON(address != regs->tpc);
384 BUG_ON(regs->tstate & TSTATE_PRIV);
388 if (fault_code & FAULT_CODE_WRITE) {
389 if (!(vma->vm_flags & VM_WRITE))
392 /* Spitfire has an icache which does not snoop
393 * processor stores. Later processors do...
395 if (tlb_type == spitfire &&
396 (vma->vm_flags & VM_EXEC) != 0 &&
397 vma->vm_file != NULL)
398 set_thread_fault_code(fault_code |
399 FAULT_CODE_BLKCOMMIT);
401 /* Allow reads even for write-only mappings */
402 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
406 fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE));
407 if (unlikely(fault & VM_FAULT_ERROR)) {
408 if (fault & VM_FAULT_OOM)
410 else if (fault & VM_FAULT_SIGBUS)
414 if (fault & VM_FAULT_MAJOR)
419 up_read(&mm->mmap_sem);
421 mm_rss = get_mm_rss(mm);
422 #ifdef CONFIG_HUGETLB_PAGE
423 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
425 if (unlikely(mm_rss >
426 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
427 tsb_grow(mm, MM_TSB_BASE, mm_rss);
428 #ifdef CONFIG_HUGETLB_PAGE
429 mm_rss = mm->context.huge_pte_count;
430 if (unlikely(mm_rss >
431 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
432 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
437 * Something tried to access memory that isn't in our memory map..
438 * Fix it, but check if it's kernel or user first..
441 insn = get_fault_insn(regs, insn);
442 up_read(&mm->mmap_sem);
445 do_kernel_fault(regs, si_code, fault_code, insn, address);
449 * We ran out of memory, or some other thing happened to us that made
450 * us unable to handle the page fault gracefully.
453 insn = get_fault_insn(regs, insn);
454 up_read(&mm->mmap_sem);
455 printk("VM: killing process %s\n", current->comm);
456 if (!(regs->tstate & TSTATE_PRIV))
457 do_group_exit(SIGKILL);
458 goto handle_kernel_fault;
461 insn = get_fault_insn(regs, 0);
462 goto handle_kernel_fault;
465 insn = get_fault_insn(regs, insn);
466 up_read(&mm->mmap_sem);
469 * Send a sigbus, regardless of whether we were in kernel
472 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
474 /* Kernel mode? Handle exceptions or die */
475 if (regs->tstate & TSTATE_PRIV)
476 goto handle_kernel_fault;