sparc: Use new '%pS' infrastructure to print symbols.
[linux-2.6/linux-loongson.git] / arch / sparc64 / mm / fault.c
blobea7d7ae76bc24d20f3e731507b478f032986f150
1 /*
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)
6 */
8 #include <asm/head.h>
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>
16 #include <linux/mm.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>
23 #include <asm/page.h>
24 #include <asm/pgtable.h>
25 #include <asm/openprom.h>
26 #include <asm/oplib.h>
27 #include <asm/uaccess.h>
28 #include <asm/asi.h>
29 #include <asm/lsu.h>
30 #include <asm/sections.h>
31 #include <asm/mmu_context.h>
33 #ifdef CONFIG_KPROBES
34 static inline int notify_page_fault(struct pt_regs *regs)
36 int ret = 0;
38 /* kprobe_running() needs smp_processor_id() */
39 if (!user_mode(regs)) {
40 preempt_disable();
41 if (kprobe_running() && kprobe_fault_handler(regs, 0))
42 ret = 1;
43 preempt_enable();
45 return ret;
47 #else
48 static inline int notify_page_fault(struct pt_regs *regs)
50 return 0;
52 #endif
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"
67 : "=r" (lsubits)
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"
74 "membar #Sync"
75 : /* no outputs */
76 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
77 "i" (ASI_DMMU));
79 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
80 if (flags & VM_READ)
81 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
82 if (flags & VM_WRITE)
83 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
84 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
85 "membar #Sync"
86 : /* no outputs */
87 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
88 : "memory");
91 static void __kprobes unhandled_fault(unsigned long address,
92 struct task_struct *tsk,
93 struct pt_regs *regs)
95 if ((unsigned long) address < PAGE_SIZE) {
96 printk(KERN_ALERT "Unable to handle kernel NULL "
97 "pointer dereference\n");
98 } else {
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",
103 (tsk->mm ?
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",
115 regs->tpc);
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);
119 dump_stack();
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);
133 pud_t *pudp;
134 pmd_t *pmdp;
135 pte_t *ptep, pte;
136 unsigned long pa;
137 u32 insn = 0;
138 unsigned long pstate;
140 if (pgd_none(*pgdp))
141 goto outret;
142 pudp = pud_offset(pgdp, tpc);
143 if (pud_none(*pudp))
144 goto outret;
145 pmdp = pmd_offset(pudp, tpc);
146 if (pmd_none(*pmdp))
147 goto outret;
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);
154 pte = *ptep;
155 if (!pte_present(pte))
156 goto out;
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"
163 : "=r" (insn)
164 : "r" (pa), "i" (ASI_PHYS_USE_EC));
166 out:
167 pte_unmap(ptep);
168 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
169 outret:
170 return insn;
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)
178 siginfo_t info;
180 info.si_code = code;
181 info.si_signo = sig;
182 info.si_errno = 0;
183 if (fault_code & FAULT_CODE_ITLB)
184 info.si_addr = (void __user *) regs->tpc;
185 else
186 info.si_addr = (void __user *)
187 compute_effective_address(regs, insn, 0);
188 info.si_trapno = 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)
197 if (!insn) {
198 if (!regs->tpc || (regs->tpc & 0x3))
199 return 0;
200 if (regs->tstate & TSTATE_PRIV) {
201 insn = *(unsigned int *) regs->tpc;
202 } else {
203 insn = get_user_insn(regs->tpc);
206 return insn;
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))
215 goto cannot_handle;
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
219 * in that case.
222 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
223 (insn & 0xc0800000) == 0xc0800000) {
224 if (insn & 0x2000)
225 asi = (regs->tstate >> 24);
226 else
227 asi = (insn >> 5);
228 if ((asi & 0xf2) == 0x82) {
229 if (insn & 0x1000000) {
230 handle_ldf_stq(insn, regs);
231 } else {
232 /* This was a non-faulting load. Just clear the
233 * destination register(s) and continue with the next
234 * instruction. -jj
236 handle_ld_nf(insn, regs);
238 return;
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);
247 if (entry) {
248 regs->tpc = entry->fixup;
249 regs->tnpc = regs->tpc + 4;
250 return;
252 } else {
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);
257 return;
260 cannot_handle:
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))
275 return;
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))
282 BUG();
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... */
291 } else {
292 bad_kernel_pc(regs, address);
293 return;
298 * If we're in an interrupt or have no user
299 * context, we must not take the fault..
301 if (in_atomic() || !mm)
302 goto intr_or_no_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);
320 if (!vma)
321 goto bad_area;
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.
331 if (((fault_code &
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);
335 if (!insn)
336 goto continue_fault;
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;
350 continue_fault:
352 if (vma->vm_start <= address)
353 goto good_area;
354 if (!(vma->vm_flags & VM_GROWSDOWN))
355 goto bad_area;
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) {
360 unsigned char asi;
362 if (insn & 0x2000)
363 asi = (regs->tstate >> 24);
364 else
365 asi = (insn >> 5);
366 if ((asi & 0xf2) == 0x82)
367 goto bad_area;
370 if (expand_stack(vma, address))
371 goto bad_area;
373 * Ok, we have a good vm_area for this memory access, so
374 * we can handle it..
376 good_area:
377 si_code = SEGV_ACCERR;
379 /* If we took a ITLB miss on a non-executable page, catch
380 * that here.
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);
385 goto bad_area;
388 if (fault_code & FAULT_CODE_WRITE) {
389 if (!(vma->vm_flags & VM_WRITE))
390 goto bad_area;
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);
400 } else {
401 /* Allow reads even for write-only mappings */
402 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
403 goto bad_area;
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)
409 goto out_of_memory;
410 else if (fault & VM_FAULT_SIGBUS)
411 goto do_sigbus;
412 BUG();
414 if (fault & VM_FAULT_MAJOR)
415 current->maj_flt++;
416 else
417 current->min_flt++;
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));
424 #endif
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);
433 #endif
434 return;
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..
440 bad_area:
441 insn = get_fault_insn(regs, insn);
442 up_read(&mm->mmap_sem);
444 handle_kernel_fault:
445 do_kernel_fault(regs, si_code, fault_code, insn, address);
446 return;
449 * We ran out of memory, or some other thing happened to us that made
450 * us unable to handle the page fault gracefully.
452 out_of_memory:
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;
460 intr_or_no_mm:
461 insn = get_fault_insn(regs, 0);
462 goto handle_kernel_fault;
464 do_sigbus:
465 insn = get_fault_insn(regs, insn);
466 up_read(&mm->mmap_sem);
469 * Send a sigbus, regardless of whether we were in kernel
470 * or user mode.
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;