blob: 236f4d228d2bc962908af77a37b012ce9d1e8bc5 [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
19#include <linux/interrupt.h>
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070020#include <linux/kprobes.h>
David S. Millereb398d12006-07-22 01:12:09 -070021#include <linux/kallsyms.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070022#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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>
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
David S. Miller127cda12007-05-08 18:25:14 -070035static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070036{
David S. Miller127cda12007-05-08 18:25:14 -070037 int ret = 0;
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070038
David S. Miller127cda12007-05-08 18:25:14 -070039 /* kprobe_running() needs smp_processor_id() */
40 if (!user_mode(regs)) {
41 preempt_disable();
42 if (kprobe_running() && kprobe_fault_handler(regs, 0))
43 ret = 1;
44 preempt_enable();
45 }
46 return ret;
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070047}
48#else
David S. Miller127cda12007-05-08 18:25:14 -070049static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070050{
David S. Miller127cda12007-05-08 18:25:14 -070051 return 0;
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070052}
53#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * To debug kernel to catch accesses to certain virtual/physical addresses.
57 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
58 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
59 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
60 * watched. This is only useful on a single cpu machine for now. After the watchpoint
61 * is detected, the process causing it will be killed, thus preventing an infinite loop.
62 */
63void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
64{
65 unsigned long lsubits;
66
67 __asm__ __volatile__("ldxa [%%g0] %1, %0"
68 : "=r" (lsubits)
69 : "i" (ASI_LSU_CONTROL));
70 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
71 LSU_CONTROL_PR | LSU_CONTROL_VR |
72 LSU_CONTROL_PW | LSU_CONTROL_VW);
73
74 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
75 "membar #Sync"
76 : /* no outputs */
77 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
78 "i" (ASI_DMMU));
79
80 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
81 if (flags & VM_READ)
82 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
83 if (flags & VM_WRITE)
84 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
85 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
86 "membar #Sync"
87 : /* no outputs */
88 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
89 : "memory");
90}
91
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070092static void __kprobes unhandled_fault(unsigned long address,
93 struct task_struct *tsk,
94 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 if ((unsigned long) address < PAGE_SIZE) {
97 printk(KERN_ALERT "Unable to handle kernel NULL "
98 "pointer dereference\n");
99 } else {
100 printk(KERN_ALERT "Unable to handle kernel paging request "
101 "at virtual address %016lx\n", (unsigned long)address);
102 }
103 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
104 (tsk->mm ?
105 CTX_HWBITS(tsk->mm->context) :
106 CTX_HWBITS(tsk->active_mm->context)));
107 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
108 (tsk->mm ? (unsigned long) tsk->mm->pgd :
109 (unsigned long) tsk->active_mm->pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 die_if_kernel("Oops", regs);
111}
112
David S. Millerbf941d62006-02-13 18:07:45 -0800113static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
116 regs->tpc);
David S. Millereb398d12006-07-22 01:12:09 -0700117 printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
118 print_symbol("RPC: <%s>\n", regs->u_regs[15]);
David S. Millerbf941d62006-02-13 18:07:45 -0800119 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
David S. Millerc1f193a2007-07-30 00:17:12 -0700120 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unhandled_fault(regs->tpc, current, regs);
122}
123
124/*
125 * We now make sure that mmap_sem is held in all paths that call
126 * this. Additionally, to prevent kswapd from ripping ptes from
127 * under us, raise interrupts around the time that we look at the
128 * pte, kswapd will have to wait to get his smp ipi response from
Hugh Dickinsda160542005-11-08 10:00:55 -0800129 * us. vmtruncate likewise. This saves us having to get pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131static unsigned int get_user_insn(unsigned long tpc)
132{
133 pgd_t *pgdp = pgd_offset(current->mm, tpc);
134 pud_t *pudp;
135 pmd_t *pmdp;
136 pte_t *ptep, pte;
137 unsigned long pa;
138 u32 insn = 0;
139 unsigned long pstate;
140
141 if (pgd_none(*pgdp))
142 goto outret;
143 pudp = pud_offset(pgdp, tpc);
144 if (pud_none(*pudp))
145 goto outret;
146 pmdp = pmd_offset(pudp, tpc);
147 if (pmd_none(*pmdp))
148 goto outret;
149
150 /* This disables preemption for us as well. */
151 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
152 __asm__ __volatile__("wrpr %0, %1, %%pstate"
153 : : "r" (pstate), "i" (PSTATE_IE));
154 ptep = pte_offset_map(pmdp, tpc);
155 pte = *ptep;
156 if (!pte_present(pte))
157 goto out;
158
David S. Millerc4bce902006-02-11 21:57:54 -0800159 pa = (pte_pfn(pte) << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 pa += (tpc & ~PAGE_MASK);
161
162 /* Use phys bypass so we don't pollute dtlb/dcache. */
163 __asm__ __volatile__("lduwa [%1] %2, %0"
164 : "=r" (insn)
165 : "r" (pa), "i" (ASI_PHYS_USE_EC));
166
167out:
168 pte_unmap(ptep);
169 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
170outret:
171 return insn;
172}
173
174extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
175
176static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
177 unsigned int insn, int fault_code)
178{
179 siginfo_t info;
180
181 info.si_code = code;
182 info.si_signo = sig;
183 info.si_errno = 0;
184 if (fault_code & FAULT_CODE_ITLB)
185 info.si_addr = (void __user *) regs->tpc;
186 else
187 info.si_addr = (void __user *)
188 compute_effective_address(regs, insn, 0);
189 info.si_trapno = 0;
190 force_sig_info(sig, &info, current);
191}
192
193extern int handle_ldf_stq(u32, struct pt_regs *);
194extern int handle_ld_nf(u32, struct pt_regs *);
195
196static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
197{
198 if (!insn) {
199 if (!regs->tpc || (regs->tpc & 0x3))
200 return 0;
201 if (regs->tstate & TSTATE_PRIV) {
202 insn = *(unsigned int *) regs->tpc;
203 } else {
204 insn = get_user_insn(regs->tpc);
205 }
206 }
207 return insn;
208}
209
210static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
211 unsigned int insn, unsigned long address)
212{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 unsigned char asi = ASI_P;
214
215 if ((!insn) && (regs->tstate & TSTATE_PRIV))
216 goto cannot_handle;
217
218 /* If user insn could be read (thus insn is zero), that
219 * is fine. We will just gun down the process with a signal
220 * in that case.
221 */
222
223 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
224 (insn & 0xc0800000) == 0xc0800000) {
225 if (insn & 0x2000)
226 asi = (regs->tstate >> 24);
227 else
228 asi = (insn >> 5);
229 if ((asi & 0xf2) == 0x82) {
230 if (insn & 0x1000000) {
231 handle_ldf_stq(insn, regs);
232 } else {
233 /* This was a non-faulting load. Just clear the
234 * destination register(s) and continue with the next
235 * instruction. -jj
236 */
237 handle_ld_nf(insn, regs);
238 }
239 return;
240 }
241 }
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* Is this in ex_table? */
244 if (regs->tstate & TSTATE_PRIV) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700245 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David S. Miller622eaec2008-02-26 17:30:02 -0800247 entry = search_exception_tables(regs->tpc);
248 if (entry) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700249 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return;
252 }
253 } else {
254 /* The si_code was set to make clear whether
255 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
256 */
257 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
258 return;
259 }
260
261cannot_handle:
262 unhandled_fault (address, current, regs);
263}
264
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700265asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct mm_struct *mm = current->mm;
268 struct vm_area_struct *vma;
269 unsigned int insn = 0;
Nick Piggin83c54072007-07-19 01:47:05 -0700270 int si_code, fault_code, fault;
David S. Miller7a1ac522006-03-16 02:02:32 -0800271 unsigned long address, mm_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 fault_code = get_thread_fault_code();
274
David S. Miller127cda12007-05-08 18:25:14 -0700275 if (notify_page_fault(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return;
277
278 si_code = SEGV_MAPERR;
279 address = current_thread_info()->fault_address;
280
281 if ((fault_code & FAULT_CODE_ITLB) &&
282 (fault_code & FAULT_CODE_DTLB))
283 BUG();
284
285 if (regs->tstate & TSTATE_PRIV) {
286 unsigned long tpc = regs->tpc;
287
288 /* Sanity check the PC. */
David S. Millerbe717162008-02-28 20:38:15 -0800289 if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
291 /* Valid, no problems... */
292 } else {
David S. Millerbf941d62006-02-13 18:07:45 -0800293 bad_kernel_pc(regs, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return;
295 }
296 }
297
298 /*
299 * If we're in an interrupt or have no user
300 * context, we must not take the fault..
301 */
302 if (in_atomic() || !mm)
303 goto intr_or_no_mm;
304
305 if (test_thread_flag(TIF_32BIT)) {
306 if (!(regs->tstate & TSTATE_PRIV))
307 regs->tpc &= 0xffffffff;
308 address &= 0xffffffff;
309 }
310
311 if (!down_read_trylock(&mm->mmap_sem)) {
312 if ((regs->tstate & TSTATE_PRIV) &&
313 !search_exception_tables(regs->tpc)) {
314 insn = get_fault_insn(regs, insn);
315 goto handle_kernel_fault;
316 }
317 down_read(&mm->mmap_sem);
318 }
319
320 vma = find_vma(mm, address);
321 if (!vma)
322 goto bad_area;
323
324 /* Pure DTLB misses do not tell us whether the fault causing
325 * load/store/atomic was a write or not, it only says that there
326 * was no match. So in such a case we (carefully) read the
327 * instruction to try and figure this out. It's an optimization
328 * so it's ok if we can't do this.
329 *
330 * Special hack, window spill/fill knows the exact fault type.
331 */
332 if (((fault_code &
333 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
334 (vma->vm_flags & VM_WRITE) != 0) {
335 insn = get_fault_insn(regs, 0);
336 if (!insn)
337 goto continue_fault;
David S. Miller73c50a22006-03-28 13:32:24 -0800338 /* All loads, stores and atomics have bits 30 and 31 both set
339 * in the instruction. Bit 21 is set in all stores, but we
340 * have to avoid prefetches which also have bit 21 set.
341 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if ((insn & 0xc0200000) == 0xc0200000 &&
David S. Miller73c50a22006-03-28 13:32:24 -0800343 (insn & 0x01780000) != 0x01680000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* Don't bother updating thread struct value,
345 * because update_mmu_cache only cares which tlb
346 * the access came from.
347 */
348 fault_code |= FAULT_CODE_WRITE;
349 }
350 }
351continue_fault:
352
353 if (vma->vm_start <= address)
354 goto good_area;
355 if (!(vma->vm_flags & VM_GROWSDOWN))
356 goto bad_area;
357 if (!(fault_code & FAULT_CODE_WRITE)) {
358 /* Non-faulting loads shouldn't expand stack. */
359 insn = get_fault_insn(regs, insn);
360 if ((insn & 0xc0800000) == 0xc0800000) {
361 unsigned char asi;
362
363 if (insn & 0x2000)
364 asi = (regs->tstate >> 24);
365 else
366 asi = (insn >> 5);
367 if ((asi & 0xf2) == 0x82)
368 goto bad_area;
369 }
370 }
371 if (expand_stack(vma, address))
372 goto bad_area;
373 /*
374 * Ok, we have a good vm_area for this memory access, so
375 * we can handle it..
376 */
377good_area:
378 si_code = SEGV_ACCERR;
379
380 /* If we took a ITLB miss on a non-executable page, catch
381 * that here.
382 */
383 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
384 BUG_ON(address != regs->tpc);
385 BUG_ON(regs->tstate & TSTATE_PRIV);
386 goto bad_area;
387 }
388
389 if (fault_code & FAULT_CODE_WRITE) {
390 if (!(vma->vm_flags & VM_WRITE))
391 goto bad_area;
392
393 /* Spitfire has an icache which does not snoop
394 * processor stores. Later processors do...
395 */
396 if (tlb_type == spitfire &&
397 (vma->vm_flags & VM_EXEC) != 0 &&
398 vma->vm_file != NULL)
399 set_thread_fault_code(fault_code |
400 FAULT_CODE_BLKCOMMIT);
401 } else {
402 /* Allow reads even for write-only mappings */
403 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
404 goto bad_area;
405 }
406
Nick Piggin83c54072007-07-19 01:47:05 -0700407 fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE));
408 if (unlikely(fault & VM_FAULT_ERROR)) {
409 if (fault & VM_FAULT_OOM)
410 goto out_of_memory;
411 else if (fault & VM_FAULT_SIGBUS)
412 goto do_sigbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 BUG();
414 }
Nick Piggin83c54072007-07-19 01:47:05 -0700415 if (fault & VM_FAULT_MAJOR)
416 current->maj_flt++;
417 else
418 current->min_flt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 up_read(&mm->mmap_sem);
David S. Miller7a1ac522006-03-16 02:02:32 -0800421
422 mm_rss = get_mm_rss(mm);
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800423#ifdef CONFIG_HUGETLB_PAGE
424 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
425#endif
David S. Miller7bebd832006-03-27 01:07:55 -0800426 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800427 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
428 tsb_grow(mm, MM_TSB_BASE, mm_rss);
429#ifdef CONFIG_HUGETLB_PAGE
430 mm_rss = mm->context.huge_pte_count;
David S. Miller7bebd832006-03-27 01:07:55 -0800431 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800432 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
433 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
434#endif
David S. Millerefdc1e22005-09-28 21:06:47 -0700435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /*
438 * Something tried to access memory that isn't in our memory map..
439 * Fix it, but check if it's kernel or user first..
440 */
441bad_area:
442 insn = get_fault_insn(regs, insn);
443 up_read(&mm->mmap_sem);
444
445handle_kernel_fault:
446 do_kernel_fault(regs, si_code, fault_code, insn, address);
David S. Millerefdc1e22005-09-28 21:06:47 -0700447 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449/*
450 * We ran out of memory, or some other thing happened to us that made
451 * us unable to handle the page fault gracefully.
452 */
453out_of_memory:
454 insn = get_fault_insn(regs, insn);
455 up_read(&mm->mmap_sem);
456 printk("VM: killing process %s\n", current->comm);
457 if (!(regs->tstate & TSTATE_PRIV))
Will Schmidtdcca2bd2007-10-16 01:24:18 -0700458 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 goto handle_kernel_fault;
460
461intr_or_no_mm:
462 insn = get_fault_insn(regs, 0);
463 goto handle_kernel_fault;
464
465do_sigbus:
466 insn = get_fault_insn(regs, insn);
467 up_read(&mm->mmap_sem);
468
469 /*
470 * Send a sigbus, regardless of whether we were in kernel
471 * or user mode.
472 */
473 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
474
475 /* Kernel mode? Handle exceptions or die */
476 if (regs->tstate & TSTATE_PRIV)
477 goto handle_kernel_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}