header cleaning: don't include smp_lock.h when not used
[linux-2.6/kmemtrace.git] / arch / sparc64 / mm / fault.c
blobc32e309f778832e572a215f6f6adc7aedc6831aa
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)
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/kallsyms.h>
22 #include <linux/kdebug.h>
24 #include <asm/page.h>
25 #include <asm/pgtable.h>
26 #include <asm/openprom.h>
27 #include <asm/oplib.h>
28 #include <asm/uaccess.h>
29 #include <asm/asi.h>
30 #include <asm/lsu.h>
31 #include <asm/sections.h>
32 #include <asm/mmu_context.h>
34 #ifdef CONFIG_KPROBES
35 ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
37 /* Hook to register for page fault notifications */
38 int register_page_fault_notifier(struct notifier_block *nb)
40 return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
43 int unregister_page_fault_notifier(struct notifier_block *nb)
45 return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
48 static inline int notify_page_fault(enum die_val val, const char *str,
49 struct pt_regs *regs, long err, int trap, int sig)
51 struct die_args args = {
52 .regs = regs,
53 .str = str,
54 .err = err,
55 .trapnr = trap,
56 .signr = sig
58 return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
60 #else
61 static inline int notify_page_fault(enum die_val val, const char *str,
62 struct pt_regs *regs, long err, int trap, int sig)
64 return NOTIFY_DONE;
66 #endif
69 * To debug kernel to catch accesses to certain virtual/physical addresses.
70 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
71 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
72 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
73 * watched. This is only useful on a single cpu machine for now. After the watchpoint
74 * is detected, the process causing it will be killed, thus preventing an infinite loop.
76 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
78 unsigned long lsubits;
80 __asm__ __volatile__("ldxa [%%g0] %1, %0"
81 : "=r" (lsubits)
82 : "i" (ASI_LSU_CONTROL));
83 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
84 LSU_CONTROL_PR | LSU_CONTROL_VR |
85 LSU_CONTROL_PW | LSU_CONTROL_VW);
87 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
88 "membar #Sync"
89 : /* no outputs */
90 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
91 "i" (ASI_DMMU));
93 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
94 if (flags & VM_READ)
95 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
96 if (flags & VM_WRITE)
97 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
98 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
99 "membar #Sync"
100 : /* no outputs */
101 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
102 : "memory");
105 static void __kprobes unhandled_fault(unsigned long address,
106 struct task_struct *tsk,
107 struct pt_regs *regs)
109 if ((unsigned long) address < PAGE_SIZE) {
110 printk(KERN_ALERT "Unable to handle kernel NULL "
111 "pointer dereference\n");
112 } else {
113 printk(KERN_ALERT "Unable to handle kernel paging request "
114 "at virtual address %016lx\n", (unsigned long)address);
116 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
117 (tsk->mm ?
118 CTX_HWBITS(tsk->mm->context) :
119 CTX_HWBITS(tsk->active_mm->context)));
120 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
121 (tsk->mm ? (unsigned long) tsk->mm->pgd :
122 (unsigned long) tsk->active_mm->pgd));
123 if (notify_die(DIE_GPF, "general protection fault", regs,
124 0, 0, SIGSEGV) == NOTIFY_STOP)
125 return;
126 die_if_kernel("Oops", regs);
129 static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
131 unsigned long *ksp;
133 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
134 regs->tpc);
135 printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
136 print_symbol("RPC: <%s>\n", regs->u_regs[15]);
137 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
138 __asm__("mov %%sp, %0" : "=r" (ksp));
139 show_stack(current, ksp);
140 unhandled_fault(regs->tpc, current, regs);
144 * We now make sure that mmap_sem is held in all paths that call
145 * this. Additionally, to prevent kswapd from ripping ptes from
146 * under us, raise interrupts around the time that we look at the
147 * pte, kswapd will have to wait to get his smp ipi response from
148 * us. vmtruncate likewise. This saves us having to get pte lock.
150 static unsigned int get_user_insn(unsigned long tpc)
152 pgd_t *pgdp = pgd_offset(current->mm, tpc);
153 pud_t *pudp;
154 pmd_t *pmdp;
155 pte_t *ptep, pte;
156 unsigned long pa;
157 u32 insn = 0;
158 unsigned long pstate;
160 if (pgd_none(*pgdp))
161 goto outret;
162 pudp = pud_offset(pgdp, tpc);
163 if (pud_none(*pudp))
164 goto outret;
165 pmdp = pmd_offset(pudp, tpc);
166 if (pmd_none(*pmdp))
167 goto outret;
169 /* This disables preemption for us as well. */
170 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
171 __asm__ __volatile__("wrpr %0, %1, %%pstate"
172 : : "r" (pstate), "i" (PSTATE_IE));
173 ptep = pte_offset_map(pmdp, tpc);
174 pte = *ptep;
175 if (!pte_present(pte))
176 goto out;
178 pa = (pte_pfn(pte) << PAGE_SHIFT);
179 pa += (tpc & ~PAGE_MASK);
181 /* Use phys bypass so we don't pollute dtlb/dcache. */
182 __asm__ __volatile__("lduwa [%1] %2, %0"
183 : "=r" (insn)
184 : "r" (pa), "i" (ASI_PHYS_USE_EC));
186 out:
187 pte_unmap(ptep);
188 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
189 outret:
190 return insn;
193 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
195 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
196 unsigned int insn, int fault_code)
198 siginfo_t info;
200 info.si_code = code;
201 info.si_signo = sig;
202 info.si_errno = 0;
203 if (fault_code & FAULT_CODE_ITLB)
204 info.si_addr = (void __user *) regs->tpc;
205 else
206 info.si_addr = (void __user *)
207 compute_effective_address(regs, insn, 0);
208 info.si_trapno = 0;
209 force_sig_info(sig, &info, current);
212 extern int handle_ldf_stq(u32, struct pt_regs *);
213 extern int handle_ld_nf(u32, struct pt_regs *);
215 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
217 if (!insn) {
218 if (!regs->tpc || (regs->tpc & 0x3))
219 return 0;
220 if (regs->tstate & TSTATE_PRIV) {
221 insn = *(unsigned int *) regs->tpc;
222 } else {
223 insn = get_user_insn(regs->tpc);
226 return insn;
229 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
230 unsigned int insn, unsigned long address)
232 unsigned char asi = ASI_P;
234 if ((!insn) && (regs->tstate & TSTATE_PRIV))
235 goto cannot_handle;
237 /* If user insn could be read (thus insn is zero), that
238 * is fine. We will just gun down the process with a signal
239 * in that case.
242 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
243 (insn & 0xc0800000) == 0xc0800000) {
244 if (insn & 0x2000)
245 asi = (regs->tstate >> 24);
246 else
247 asi = (insn >> 5);
248 if ((asi & 0xf2) == 0x82) {
249 if (insn & 0x1000000) {
250 handle_ldf_stq(insn, regs);
251 } else {
252 /* This was a non-faulting load. Just clear the
253 * destination register(s) and continue with the next
254 * instruction. -jj
256 handle_ld_nf(insn, regs);
258 return;
262 /* Is this in ex_table? */
263 if (regs->tstate & TSTATE_PRIV) {
264 const struct exception_table_entry *entry;
266 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
267 if (insn & 0x2000)
268 asi = (regs->tstate >> 24);
269 else
270 asi = (insn >> 5);
273 /* Look in asi.h: All _S asis have LS bit set */
274 if ((asi & 0x1) &&
275 (entry = search_exception_tables(regs->tpc))) {
276 regs->tpc = entry->fixup;
277 regs->tnpc = regs->tpc + 4;
278 return;
280 } else {
281 /* The si_code was set to make clear whether
282 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
284 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
285 return;
288 cannot_handle:
289 unhandled_fault (address, current, regs);
292 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
294 struct mm_struct *mm = current->mm;
295 struct vm_area_struct *vma;
296 unsigned int insn = 0;
297 int si_code, fault_code;
298 unsigned long address, mm_rss;
300 fault_code = get_thread_fault_code();
302 if (notify_page_fault(DIE_PAGE_FAULT, "page_fault", regs,
303 fault_code, 0, SIGSEGV) == NOTIFY_STOP)
304 return;
306 si_code = SEGV_MAPERR;
307 address = current_thread_info()->fault_address;
309 if ((fault_code & FAULT_CODE_ITLB) &&
310 (fault_code & FAULT_CODE_DTLB))
311 BUG();
313 if (regs->tstate & TSTATE_PRIV) {
314 unsigned long tpc = regs->tpc;
316 /* Sanity check the PC. */
317 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
318 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
319 /* Valid, no problems... */
320 } else {
321 bad_kernel_pc(regs, address);
322 return;
327 * If we're in an interrupt or have no user
328 * context, we must not take the fault..
330 if (in_atomic() || !mm)
331 goto intr_or_no_mm;
333 if (test_thread_flag(TIF_32BIT)) {
334 if (!(regs->tstate & TSTATE_PRIV))
335 regs->tpc &= 0xffffffff;
336 address &= 0xffffffff;
339 if (!down_read_trylock(&mm->mmap_sem)) {
340 if ((regs->tstate & TSTATE_PRIV) &&
341 !search_exception_tables(regs->tpc)) {
342 insn = get_fault_insn(regs, insn);
343 goto handle_kernel_fault;
345 down_read(&mm->mmap_sem);
348 vma = find_vma(mm, address);
349 if (!vma)
350 goto bad_area;
352 /* Pure DTLB misses do not tell us whether the fault causing
353 * load/store/atomic was a write or not, it only says that there
354 * was no match. So in such a case we (carefully) read the
355 * instruction to try and figure this out. It's an optimization
356 * so it's ok if we can't do this.
358 * Special hack, window spill/fill knows the exact fault type.
360 if (((fault_code &
361 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
362 (vma->vm_flags & VM_WRITE) != 0) {
363 insn = get_fault_insn(regs, 0);
364 if (!insn)
365 goto continue_fault;
366 /* All loads, stores and atomics have bits 30 and 31 both set
367 * in the instruction. Bit 21 is set in all stores, but we
368 * have to avoid prefetches which also have bit 21 set.
370 if ((insn & 0xc0200000) == 0xc0200000 &&
371 (insn & 0x01780000) != 0x01680000) {
372 /* Don't bother updating thread struct value,
373 * because update_mmu_cache only cares which tlb
374 * the access came from.
376 fault_code |= FAULT_CODE_WRITE;
379 continue_fault:
381 if (vma->vm_start <= address)
382 goto good_area;
383 if (!(vma->vm_flags & VM_GROWSDOWN))
384 goto bad_area;
385 if (!(fault_code & FAULT_CODE_WRITE)) {
386 /* Non-faulting loads shouldn't expand stack. */
387 insn = get_fault_insn(regs, insn);
388 if ((insn & 0xc0800000) == 0xc0800000) {
389 unsigned char asi;
391 if (insn & 0x2000)
392 asi = (regs->tstate >> 24);
393 else
394 asi = (insn >> 5);
395 if ((asi & 0xf2) == 0x82)
396 goto bad_area;
399 if (expand_stack(vma, address))
400 goto bad_area;
402 * Ok, we have a good vm_area for this memory access, so
403 * we can handle it..
405 good_area:
406 si_code = SEGV_ACCERR;
408 /* If we took a ITLB miss on a non-executable page, catch
409 * that here.
411 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
412 BUG_ON(address != regs->tpc);
413 BUG_ON(regs->tstate & TSTATE_PRIV);
414 goto bad_area;
417 if (fault_code & FAULT_CODE_WRITE) {
418 if (!(vma->vm_flags & VM_WRITE))
419 goto bad_area;
421 /* Spitfire has an icache which does not snoop
422 * processor stores. Later processors do...
424 if (tlb_type == spitfire &&
425 (vma->vm_flags & VM_EXEC) != 0 &&
426 vma->vm_file != NULL)
427 set_thread_fault_code(fault_code |
428 FAULT_CODE_BLKCOMMIT);
429 } else {
430 /* Allow reads even for write-only mappings */
431 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
432 goto bad_area;
435 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
436 case VM_FAULT_MINOR:
437 current->min_flt++;
438 break;
439 case VM_FAULT_MAJOR:
440 current->maj_flt++;
441 break;
442 case VM_FAULT_SIGBUS:
443 goto do_sigbus;
444 case VM_FAULT_OOM:
445 goto out_of_memory;
446 default:
447 BUG();
450 up_read(&mm->mmap_sem);
452 mm_rss = get_mm_rss(mm);
453 #ifdef CONFIG_HUGETLB_PAGE
454 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
455 #endif
456 if (unlikely(mm_rss >
457 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
458 tsb_grow(mm, MM_TSB_BASE, mm_rss);
459 #ifdef CONFIG_HUGETLB_PAGE
460 mm_rss = mm->context.huge_pte_count;
461 if (unlikely(mm_rss >
462 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
463 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
464 #endif
465 return;
468 * Something tried to access memory that isn't in our memory map..
469 * Fix it, but check if it's kernel or user first..
471 bad_area:
472 insn = get_fault_insn(regs, insn);
473 up_read(&mm->mmap_sem);
475 handle_kernel_fault:
476 do_kernel_fault(regs, si_code, fault_code, insn, address);
477 return;
480 * We ran out of memory, or some other thing happened to us that made
481 * us unable to handle the page fault gracefully.
483 out_of_memory:
484 insn = get_fault_insn(regs, insn);
485 up_read(&mm->mmap_sem);
486 printk("VM: killing process %s\n", current->comm);
487 if (!(regs->tstate & TSTATE_PRIV))
488 do_exit(SIGKILL);
489 goto handle_kernel_fault;
491 intr_or_no_mm:
492 insn = get_fault_insn(regs, 0);
493 goto handle_kernel_fault;
495 do_sigbus:
496 insn = get_fault_insn(regs, insn);
497 up_read(&mm->mmap_sem);
500 * Send a sigbus, regardless of whether we were in kernel
501 * or user mode.
503 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
505 /* Kernel mode? Handle exceptions or die */
506 if (regs->tstate & TSTATE_PRIV)
507 goto handle_kernel_fault;