blob: 1605967cce91c1a076a482a56cb9cf399ebc204e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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.
3 *
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
6 */
7
8#include <asm/head.h>
9
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/smp_lock.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070021#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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/kdebug.h>
David S. Miller7a1ac522006-03-16 02:02:32 -080032#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070034#ifdef CONFIG_KPROBES
35ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
36
37/* Hook to register for page fault notifications */
38int register_page_fault_notifier(struct notifier_block *nb)
39{
40 return atomic_notifier_chain_register(&notify_page_fault_chain, nb);
41}
42
43int unregister_page_fault_notifier(struct notifier_block *nb)
44{
45 return atomic_notifier_chain_unregister(&notify_page_fault_chain, nb);
46}
47
48static inline int notify_page_fault(enum die_val val, const char *str,
49 struct pt_regs *regs, long err, int trap, int sig)
50{
51 struct die_args args = {
52 .regs = regs,
53 .str = str,
54 .err = err,
55 .trapnr = trap,
56 .signr = sig
57 };
58 return atomic_notifier_call_chain(&notify_page_fault_chain, val, &args);
59}
60#else
61static inline int notify_page_fault(enum die_val val, const char *str,
62 struct pt_regs *regs, long err, int trap, int sig)
63{
64 return NOTIFY_DONE;
65}
66#endif
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * 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.
75 */
76void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
77{
78 unsigned long lsubits;
79
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);
86
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));
92
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");
103}
104
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700105static void __kprobes unhandled_fault(unsigned long address,
106 struct task_struct *tsk,
107 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
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);
115 }
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);
127}
128
David S. Millerbf941d62006-02-13 18:07:45 -0800129static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 unsigned long *ksp;
132
133 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
134 regs->tpc);
David S. Millerbf941d62006-02-13 18:07:45 -0800135 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 __asm__("mov %%sp, %0" : "=r" (ksp));
137 show_stack(current, ksp);
138 unhandled_fault(regs->tpc, current, regs);
139}
140
141/*
142 * We now make sure that mmap_sem is held in all paths that call
143 * this. Additionally, to prevent kswapd from ripping ptes from
144 * under us, raise interrupts around the time that we look at the
145 * pte, kswapd will have to wait to get his smp ipi response from
Hugh Dickinsda160542005-11-08 10:00:55 -0800146 * us. vmtruncate likewise. This saves us having to get pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
148static unsigned int get_user_insn(unsigned long tpc)
149{
150 pgd_t *pgdp = pgd_offset(current->mm, tpc);
151 pud_t *pudp;
152 pmd_t *pmdp;
153 pte_t *ptep, pte;
154 unsigned long pa;
155 u32 insn = 0;
156 unsigned long pstate;
157
158 if (pgd_none(*pgdp))
159 goto outret;
160 pudp = pud_offset(pgdp, tpc);
161 if (pud_none(*pudp))
162 goto outret;
163 pmdp = pmd_offset(pudp, tpc);
164 if (pmd_none(*pmdp))
165 goto outret;
166
167 /* This disables preemption for us as well. */
168 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
169 __asm__ __volatile__("wrpr %0, %1, %%pstate"
170 : : "r" (pstate), "i" (PSTATE_IE));
171 ptep = pte_offset_map(pmdp, tpc);
172 pte = *ptep;
173 if (!pte_present(pte))
174 goto out;
175
David S. Millerc4bce902006-02-11 21:57:54 -0800176 pa = (pte_pfn(pte) << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 pa += (tpc & ~PAGE_MASK);
178
179 /* Use phys bypass so we don't pollute dtlb/dcache. */
180 __asm__ __volatile__("lduwa [%1] %2, %0"
181 : "=r" (insn)
182 : "r" (pa), "i" (ASI_PHYS_USE_EC));
183
184out:
185 pte_unmap(ptep);
186 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
187outret:
188 return insn;
189}
190
191extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
192
193static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
194 unsigned int insn, int fault_code)
195{
196 siginfo_t info;
197
198 info.si_code = code;
199 info.si_signo = sig;
200 info.si_errno = 0;
201 if (fault_code & FAULT_CODE_ITLB)
202 info.si_addr = (void __user *) regs->tpc;
203 else
204 info.si_addr = (void __user *)
205 compute_effective_address(regs, insn, 0);
206 info.si_trapno = 0;
207 force_sig_info(sig, &info, current);
208}
209
210extern int handle_ldf_stq(u32, struct pt_regs *);
211extern int handle_ld_nf(u32, struct pt_regs *);
212
213static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
214{
215 if (!insn) {
216 if (!regs->tpc || (regs->tpc & 0x3))
217 return 0;
218 if (regs->tstate & TSTATE_PRIV) {
219 insn = *(unsigned int *) regs->tpc;
220 } else {
221 insn = get_user_insn(regs->tpc);
222 }
223 }
224 return insn;
225}
226
227static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
228 unsigned int insn, unsigned long address)
229{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 unsigned char asi = ASI_P;
231
232 if ((!insn) && (regs->tstate & TSTATE_PRIV))
233 goto cannot_handle;
234
235 /* If user insn could be read (thus insn is zero), that
236 * is fine. We will just gun down the process with a signal
237 * in that case.
238 */
239
240 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
241 (insn & 0xc0800000) == 0xc0800000) {
242 if (insn & 0x2000)
243 asi = (regs->tstate >> 24);
244 else
245 asi = (insn >> 5);
246 if ((asi & 0xf2) == 0x82) {
247 if (insn & 0x1000000) {
248 handle_ldf_stq(insn, regs);
249 } else {
250 /* This was a non-faulting load. Just clear the
251 * destination register(s) and continue with the next
252 * instruction. -jj
253 */
254 handle_ld_nf(insn, regs);
255 }
256 return;
257 }
258 }
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Is this in ex_table? */
261 if (regs->tstate & TSTATE_PRIV) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700262 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
265 if (insn & 0x2000)
266 asi = (regs->tstate >> 24);
267 else
268 asi = (insn >> 5);
269 }
270
271 /* Look in asi.h: All _S asis have LS bit set */
272 if ((asi & 0x1) &&
David S. Miller8cf14af2005-09-28 20:21:11 -0700273 (entry = search_exception_tables(regs->tpc))) {
274 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return;
277 }
278 } else {
279 /* The si_code was set to make clear whether
280 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
281 */
282 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
283 return;
284 }
285
286cannot_handle:
287 unhandled_fault (address, current, regs);
288}
289
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700290asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct mm_struct *mm = current->mm;
293 struct vm_area_struct *vma;
294 unsigned int insn = 0;
295 int si_code, fault_code;
David S. Miller7a1ac522006-03-16 02:02:32 -0800296 unsigned long address, mm_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 fault_code = get_thread_fault_code();
299
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -0700300 if (notify_page_fault(DIE_PAGE_FAULT, "page_fault", regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 fault_code, 0, SIGSEGV) == NOTIFY_STOP)
302 return;
303
304 si_code = SEGV_MAPERR;
305 address = current_thread_info()->fault_address;
306
307 if ((fault_code & FAULT_CODE_ITLB) &&
308 (fault_code & FAULT_CODE_DTLB))
309 BUG();
310
311 if (regs->tstate & TSTATE_PRIV) {
312 unsigned long tpc = regs->tpc;
313
314 /* Sanity check the PC. */
315 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
316 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
317 /* Valid, no problems... */
318 } else {
David S. Millerbf941d62006-02-13 18:07:45 -0800319 bad_kernel_pc(regs, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return;
321 }
322 }
323
324 /*
325 * If we're in an interrupt or have no user
326 * context, we must not take the fault..
327 */
328 if (in_atomic() || !mm)
329 goto intr_or_no_mm;
330
331 if (test_thread_flag(TIF_32BIT)) {
332 if (!(regs->tstate & TSTATE_PRIV))
333 regs->tpc &= 0xffffffff;
334 address &= 0xffffffff;
335 }
336
337 if (!down_read_trylock(&mm->mmap_sem)) {
338 if ((regs->tstate & TSTATE_PRIV) &&
339 !search_exception_tables(regs->tpc)) {
340 insn = get_fault_insn(regs, insn);
341 goto handle_kernel_fault;
342 }
343 down_read(&mm->mmap_sem);
344 }
345
346 vma = find_vma(mm, address);
347 if (!vma)
348 goto bad_area;
349
350 /* Pure DTLB misses do not tell us whether the fault causing
351 * load/store/atomic was a write or not, it only says that there
352 * was no match. So in such a case we (carefully) read the
353 * instruction to try and figure this out. It's an optimization
354 * so it's ok if we can't do this.
355 *
356 * Special hack, window spill/fill knows the exact fault type.
357 */
358 if (((fault_code &
359 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
360 (vma->vm_flags & VM_WRITE) != 0) {
361 insn = get_fault_insn(regs, 0);
362 if (!insn)
363 goto continue_fault;
David S. Miller73c50a22006-03-28 13:32:24 -0800364 /* All loads, stores and atomics have bits 30 and 31 both set
365 * in the instruction. Bit 21 is set in all stores, but we
366 * have to avoid prefetches which also have bit 21 set.
367 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if ((insn & 0xc0200000) == 0xc0200000 &&
David S. Miller73c50a22006-03-28 13:32:24 -0800369 (insn & 0x01780000) != 0x01680000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Don't bother updating thread struct value,
371 * because update_mmu_cache only cares which tlb
372 * the access came from.
373 */
374 fault_code |= FAULT_CODE_WRITE;
375 }
376 }
377continue_fault:
378
379 if (vma->vm_start <= address)
380 goto good_area;
381 if (!(vma->vm_flags & VM_GROWSDOWN))
382 goto bad_area;
383 if (!(fault_code & FAULT_CODE_WRITE)) {
384 /* Non-faulting loads shouldn't expand stack. */
385 insn = get_fault_insn(regs, insn);
386 if ((insn & 0xc0800000) == 0xc0800000) {
387 unsigned char asi;
388
389 if (insn & 0x2000)
390 asi = (regs->tstate >> 24);
391 else
392 asi = (insn >> 5);
393 if ((asi & 0xf2) == 0x82)
394 goto bad_area;
395 }
396 }
397 if (expand_stack(vma, address))
398 goto bad_area;
399 /*
400 * Ok, we have a good vm_area for this memory access, so
401 * we can handle it..
402 */
403good_area:
404 si_code = SEGV_ACCERR;
405
406 /* If we took a ITLB miss on a non-executable page, catch
407 * that here.
408 */
409 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
410 BUG_ON(address != regs->tpc);
411 BUG_ON(regs->tstate & TSTATE_PRIV);
412 goto bad_area;
413 }
414
415 if (fault_code & FAULT_CODE_WRITE) {
416 if (!(vma->vm_flags & VM_WRITE))
417 goto bad_area;
418
419 /* Spitfire has an icache which does not snoop
420 * processor stores. Later processors do...
421 */
422 if (tlb_type == spitfire &&
423 (vma->vm_flags & VM_EXEC) != 0 &&
424 vma->vm_file != NULL)
425 set_thread_fault_code(fault_code |
426 FAULT_CODE_BLKCOMMIT);
427 } else {
428 /* Allow reads even for write-only mappings */
429 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
430 goto bad_area;
431 }
432
433 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
434 case VM_FAULT_MINOR:
435 current->min_flt++;
436 break;
437 case VM_FAULT_MAJOR:
438 current->maj_flt++;
439 break;
440 case VM_FAULT_SIGBUS:
441 goto do_sigbus;
442 case VM_FAULT_OOM:
443 goto out_of_memory;
444 default:
445 BUG();
446 }
447
448 up_read(&mm->mmap_sem);
David S. Miller7a1ac522006-03-16 02:02:32 -0800449
450 mm_rss = get_mm_rss(mm);
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800451#ifdef CONFIG_HUGETLB_PAGE
452 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
453#endif
David S. Miller7bebd832006-03-27 01:07:55 -0800454 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800455 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
456 tsb_grow(mm, MM_TSB_BASE, mm_rss);
457#ifdef CONFIG_HUGETLB_PAGE
458 mm_rss = mm->context.huge_pte_count;
David S. Miller7bebd832006-03-27 01:07:55 -0800459 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800460 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
461 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
462#endif
David S. Millerefdc1e22005-09-28 21:06:47 -0700463 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 /*
466 * Something tried to access memory that isn't in our memory map..
467 * Fix it, but check if it's kernel or user first..
468 */
469bad_area:
470 insn = get_fault_insn(regs, insn);
471 up_read(&mm->mmap_sem);
472
473handle_kernel_fault:
474 do_kernel_fault(regs, si_code, fault_code, insn, address);
David S. Millerefdc1e22005-09-28 21:06:47 -0700475 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477/*
478 * We ran out of memory, or some other thing happened to us that made
479 * us unable to handle the page fault gracefully.
480 */
481out_of_memory:
482 insn = get_fault_insn(regs, insn);
483 up_read(&mm->mmap_sem);
484 printk("VM: killing process %s\n", current->comm);
485 if (!(regs->tstate & TSTATE_PRIV))
486 do_exit(SIGKILL);
487 goto handle_kernel_fault;
488
489intr_or_no_mm:
490 insn = get_fault_insn(regs, 0);
491 goto handle_kernel_fault;
492
493do_sigbus:
494 insn = get_fault_insn(regs, insn);
495 up_read(&mm->mmap_sem);
496
497 /*
498 * Send a sigbus, regardless of whether we were in kernel
499 * or user mode.
500 */
501 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
502
503 /* Kernel mode? Handle exceptions or die */
504 if (regs->tstate & TSTATE_PRIV)
505 goto handle_kernel_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}