blob: 0aa50b200af809a863eb0705bcaedba0329ab65a [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 *
David S. Miller4fe3ebe2008-07-17 22:11:32 -07004 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070021#include <linux/kdebug.h>
David S. Millereeabac72009-02-02 22:08:15 -080022#include <linux/percpu.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
David S. Miller127cda12007-05-08 18:25:14 -070034static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070035{
David S. Miller127cda12007-05-08 18:25:14 -070036 int ret = 0;
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070037
David S. Miller127cda12007-05-08 18:25:14 -070038 /* kprobe_running() needs smp_processor_id() */
David S. Miller135d0822009-12-10 18:02:19 -080039 if (kprobes_built_in() && !user_mode(regs)) {
David S. Miller127cda12007-05-08 18:25:14 -070040 preempt_disable();
41 if (kprobe_running() && kprobe_fault_handler(regs, 0))
42 ret = 1;
43 preempt_enable();
44 }
45 return ret;
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070046}
Anil S Keshavamurthyd98f8f02006-06-26 00:25:27 -070047
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070048static void __kprobes unhandled_fault(unsigned long address,
49 struct task_struct *tsk,
50 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 if ((unsigned long) address < PAGE_SIZE) {
53 printk(KERN_ALERT "Unable to handle kernel NULL "
54 "pointer dereference\n");
55 } else {
56 printk(KERN_ALERT "Unable to handle kernel paging request "
57 "at virtual address %016lx\n", (unsigned long)address);
58 }
59 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
60 (tsk->mm ?
61 CTX_HWBITS(tsk->mm->context) :
62 CTX_HWBITS(tsk->active_mm->context)));
63 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
64 (tsk->mm ? (unsigned long) tsk->mm->pgd :
65 (unsigned long) tsk->active_mm->pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 die_if_kernel("Oops", regs);
67}
68
David S. Millerbf941d62006-02-13 18:07:45 -080069static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
72 regs->tpc);
David S. Millereb398d12006-07-22 01:12:09 -070073 printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
David S. Miller4fe3ebe2008-07-17 22:11:32 -070074 printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
David S. Millerbf941d62006-02-13 18:07:45 -080075 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
David S. Millerc1f193a2007-07-30 00:17:12 -070076 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unhandled_fault(regs->tpc, current, regs);
78}
79
80/*
81 * We now make sure that mmap_sem is held in all paths that call
82 * this. Additionally, to prevent kswapd from ripping ptes from
83 * under us, raise interrupts around the time that we look at the
84 * pte, kswapd will have to wait to get his smp ipi response from
Hugh Dickinsda160542005-11-08 10:00:55 -080085 * us. vmtruncate likewise. This saves us having to get pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87static unsigned int get_user_insn(unsigned long tpc)
88{
89 pgd_t *pgdp = pgd_offset(current->mm, tpc);
90 pud_t *pudp;
91 pmd_t *pmdp;
92 pte_t *ptep, pte;
93 unsigned long pa;
94 u32 insn = 0;
95 unsigned long pstate;
96
97 if (pgd_none(*pgdp))
98 goto outret;
99 pudp = pud_offset(pgdp, tpc);
100 if (pud_none(*pudp))
101 goto outret;
102 pmdp = pmd_offset(pudp, tpc);
103 if (pmd_none(*pmdp))
104 goto outret;
105
106 /* This disables preemption for us as well. */
107 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
108 __asm__ __volatile__("wrpr %0, %1, %%pstate"
109 : : "r" (pstate), "i" (PSTATE_IE));
110 ptep = pte_offset_map(pmdp, tpc);
111 pte = *ptep;
112 if (!pte_present(pte))
113 goto out;
114
David S. Millerc4bce902006-02-11 21:57:54 -0800115 pa = (pte_pfn(pte) << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 pa += (tpc & ~PAGE_MASK);
117
118 /* Use phys bypass so we don't pollute dtlb/dcache. */
119 __asm__ __volatile__("lduwa [%1] %2, %0"
120 : "=r" (insn)
121 : "r" (pa), "i" (ASI_PHYS_USE_EC));
122
123out:
124 pte_unmap(ptep);
125 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
126outret:
127 return insn;
128}
129
130extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
131
132static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
133 unsigned int insn, int fault_code)
134{
135 siginfo_t info;
136
137 info.si_code = code;
138 info.si_signo = sig;
139 info.si_errno = 0;
140 if (fault_code & FAULT_CODE_ITLB)
141 info.si_addr = (void __user *) regs->tpc;
142 else
143 info.si_addr = (void __user *)
144 compute_effective_address(regs, insn, 0);
145 info.si_trapno = 0;
146 force_sig_info(sig, &info, current);
147}
148
149extern int handle_ldf_stq(u32, struct pt_regs *);
150extern int handle_ld_nf(u32, struct pt_regs *);
151
152static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
153{
154 if (!insn) {
155 if (!regs->tpc || (regs->tpc & 0x3))
156 return 0;
157 if (regs->tstate & TSTATE_PRIV) {
158 insn = *(unsigned int *) regs->tpc;
159 } else {
160 insn = get_user_insn(regs->tpc);
161 }
162 }
163 return insn;
164}
165
166static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
167 unsigned int insn, unsigned long address)
168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned char asi = ASI_P;
170
171 if ((!insn) && (regs->tstate & TSTATE_PRIV))
172 goto cannot_handle;
173
174 /* If user insn could be read (thus insn is zero), that
175 * is fine. We will just gun down the process with a signal
176 * in that case.
177 */
178
179 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
180 (insn & 0xc0800000) == 0xc0800000) {
181 if (insn & 0x2000)
182 asi = (regs->tstate >> 24);
183 else
184 asi = (insn >> 5);
185 if ((asi & 0xf2) == 0x82) {
186 if (insn & 0x1000000) {
187 handle_ldf_stq(insn, regs);
188 } else {
189 /* This was a non-faulting load. Just clear the
190 * destination register(s) and continue with the next
191 * instruction. -jj
192 */
193 handle_ld_nf(insn, regs);
194 }
195 return;
196 }
197 }
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Is this in ex_table? */
200 if (regs->tstate & TSTATE_PRIV) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700201 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
David S. Miller622eaec2008-02-26 17:30:02 -0800203 entry = search_exception_tables(regs->tpc);
204 if (entry) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700205 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return;
208 }
209 } else {
210 /* The si_code was set to make clear whether
211 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
212 */
213 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
214 return;
215 }
216
217cannot_handle:
218 unhandled_fault (address, current, regs);
219}
220
David S. Miller9b026052009-02-03 16:28:23 -0800221static void noinline bogus_32bit_fault_tpc(struct pt_regs *regs)
222{
223 static int times;
224
225 if (times++ < 10)
226 printk(KERN_ERR "FAULT[%s:%d]: 32-bit process reports "
227 "64-bit TPC [%lx]\n",
228 current->comm, current->pid,
229 regs->tpc);
230 show_regs(regs);
231}
232
233static void noinline bogus_32bit_fault_address(struct pt_regs *regs,
234 unsigned long addr)
235{
236 static int times;
237
238 if (times++ < 10)
239 printk(KERN_ERR "FAULT[%s:%d]: 32-bit process "
240 "reports 64-bit fault address [%lx]\n",
241 current->comm, current->pid, addr);
242 show_regs(regs);
243}
244
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700245asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct mm_struct *mm = current->mm;
248 struct vm_area_struct *vma;
249 unsigned int insn = 0;
Nick Piggin83c54072007-07-19 01:47:05 -0700250 int si_code, fault_code, fault;
David S. Miller7a1ac522006-03-16 02:02:32 -0800251 unsigned long address, mm_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 fault_code = get_thread_fault_code();
254
David S. Miller127cda12007-05-08 18:25:14 -0700255 if (notify_page_fault(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return;
257
258 si_code = SEGV_MAPERR;
259 address = current_thread_info()->fault_address;
260
261 if ((fault_code & FAULT_CODE_ITLB) &&
262 (fault_code & FAULT_CODE_DTLB))
263 BUG();
264
David S. Millereeabac72009-02-02 22:08:15 -0800265 if (test_thread_flag(TIF_32BIT)) {
David S. Miller9b026052009-02-03 16:28:23 -0800266 if (!(regs->tstate & TSTATE_PRIV)) {
267 if (unlikely((regs->tpc >> 32) != 0)) {
268 bogus_32bit_fault_tpc(regs);
269 goto intr_or_no_mm;
270 }
271 }
272 if (unlikely((address >> 32) != 0)) {
273 bogus_32bit_fault_address(regs, address);
274 goto intr_or_no_mm;
275 }
David S. Millereeabac72009-02-02 22:08:15 -0800276 }
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (regs->tstate & TSTATE_PRIV) {
David S. Miller9b026052009-02-03 16:28:23 -0800279 unsigned long tpc = regs->tpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* Sanity check the PC. */
David S. Millerbe717162008-02-28 20:38:15 -0800282 if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
284 /* Valid, no problems... */
285 } else {
David S. Millerbf941d62006-02-13 18:07:45 -0800286 bad_kernel_pc(regs, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return;
288 }
289 }
290
291 /*
292 * If we're in an interrupt or have no user
293 * context, we must not take the fault..
294 */
295 if (in_atomic() || !mm)
296 goto intr_or_no_mm;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!down_read_trylock(&mm->mmap_sem)) {
299 if ((regs->tstate & TSTATE_PRIV) &&
300 !search_exception_tables(regs->tpc)) {
301 insn = get_fault_insn(regs, insn);
302 goto handle_kernel_fault;
303 }
304 down_read(&mm->mmap_sem);
305 }
306
307 vma = find_vma(mm, address);
308 if (!vma)
309 goto bad_area;
310
311 /* Pure DTLB misses do not tell us whether the fault causing
312 * load/store/atomic was a write or not, it only says that there
313 * was no match. So in such a case we (carefully) read the
314 * instruction to try and figure this out. It's an optimization
315 * so it's ok if we can't do this.
316 *
317 * Special hack, window spill/fill knows the exact fault type.
318 */
319 if (((fault_code &
320 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
321 (vma->vm_flags & VM_WRITE) != 0) {
322 insn = get_fault_insn(regs, 0);
323 if (!insn)
324 goto continue_fault;
David S. Miller73c50a22006-03-28 13:32:24 -0800325 /* All loads, stores and atomics have bits 30 and 31 both set
326 * in the instruction. Bit 21 is set in all stores, but we
327 * have to avoid prefetches which also have bit 21 set.
328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if ((insn & 0xc0200000) == 0xc0200000 &&
David S. Miller73c50a22006-03-28 13:32:24 -0800330 (insn & 0x01780000) != 0x01680000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /* Don't bother updating thread struct value,
332 * because update_mmu_cache only cares which tlb
333 * the access came from.
334 */
335 fault_code |= FAULT_CODE_WRITE;
336 }
337 }
338continue_fault:
339
340 if (vma->vm_start <= address)
341 goto good_area;
342 if (!(vma->vm_flags & VM_GROWSDOWN))
343 goto bad_area;
344 if (!(fault_code & FAULT_CODE_WRITE)) {
345 /* Non-faulting loads shouldn't expand stack. */
346 insn = get_fault_insn(regs, insn);
347 if ((insn & 0xc0800000) == 0xc0800000) {
348 unsigned char asi;
349
350 if (insn & 0x2000)
351 asi = (regs->tstate >> 24);
352 else
353 asi = (insn >> 5);
354 if ((asi & 0xf2) == 0x82)
355 goto bad_area;
356 }
357 }
358 if (expand_stack(vma, address))
359 goto bad_area;
360 /*
361 * Ok, we have a good vm_area for this memory access, so
362 * we can handle it..
363 */
364good_area:
365 si_code = SEGV_ACCERR;
366
367 /* If we took a ITLB miss on a non-executable page, catch
368 * that here.
369 */
370 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
371 BUG_ON(address != regs->tpc);
372 BUG_ON(regs->tstate & TSTATE_PRIV);
373 goto bad_area;
374 }
375
376 if (fault_code & FAULT_CODE_WRITE) {
377 if (!(vma->vm_flags & VM_WRITE))
378 goto bad_area;
379
380 /* Spitfire has an icache which does not snoop
381 * processor stores. Later processors do...
382 */
383 if (tlb_type == spitfire &&
384 (vma->vm_flags & VM_EXEC) != 0 &&
385 vma->vm_file != NULL)
386 set_thread_fault_code(fault_code |
387 FAULT_CODE_BLKCOMMIT);
388 } else {
389 /* Allow reads even for write-only mappings */
390 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
391 goto bad_area;
392 }
393
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700394 fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE) ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -0700395 if (unlikely(fault & VM_FAULT_ERROR)) {
396 if (fault & VM_FAULT_OOM)
397 goto out_of_memory;
398 else if (fault & VM_FAULT_SIGBUS)
399 goto do_sigbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 BUG();
401 }
Nick Piggin83c54072007-07-19 01:47:05 -0700402 if (fault & VM_FAULT_MAJOR)
403 current->maj_flt++;
404 else
405 current->min_flt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 up_read(&mm->mmap_sem);
David S. Miller7a1ac522006-03-16 02:02:32 -0800408
409 mm_rss = get_mm_rss(mm);
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800410#ifdef CONFIG_HUGETLB_PAGE
411 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
412#endif
David S. Miller7bebd832006-03-27 01:07:55 -0800413 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800414 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
415 tsb_grow(mm, MM_TSB_BASE, mm_rss);
416#ifdef CONFIG_HUGETLB_PAGE
417 mm_rss = mm->context.huge_pte_count;
David S. Miller7bebd832006-03-27 01:07:55 -0800418 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800419 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
420 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
421#endif
David S. Millerefdc1e22005-09-28 21:06:47 -0700422 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /*
425 * Something tried to access memory that isn't in our memory map..
426 * Fix it, but check if it's kernel or user first..
427 */
428bad_area:
429 insn = get_fault_insn(regs, insn);
430 up_read(&mm->mmap_sem);
431
432handle_kernel_fault:
433 do_kernel_fault(regs, si_code, fault_code, insn, address);
David S. Millerefdc1e22005-09-28 21:06:47 -0700434 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436/*
437 * We ran out of memory, or some other thing happened to us that made
438 * us unable to handle the page fault gracefully.
439 */
440out_of_memory:
441 insn = get_fault_insn(regs, insn);
442 up_read(&mm->mmap_sem);
David S. Millera923c282009-08-02 19:17:15 -0700443 if (!(regs->tstate & TSTATE_PRIV)) {
444 pagefault_out_of_memory();
445 return;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 goto handle_kernel_fault;
448
449intr_or_no_mm:
450 insn = get_fault_insn(regs, 0);
451 goto handle_kernel_fault;
452
453do_sigbus:
454 insn = get_fault_insn(regs, insn);
455 up_read(&mm->mmap_sem);
456
457 /*
458 * Send a sigbus, regardless of whether we were in kernel
459 * or user mode.
460 */
461 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
462
463 /* Kernel mode? Handle exceptions or die */
464 if (regs->tstate & TSTATE_PRIV)
465 goto handle_kernel_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}