blob: 6081936bf03b84579ce488b7488b8bc7c6e395ac [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. Miller4ed5d5e2009-12-10 18:08:29 -080034static inline __kprobes 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. Miller4ed5d5e2009-12-10 18:08:29 -080069static void __kprobes 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
David S. Miller4ed5d5e2009-12-10 18:08:29 -0800166static void __kprobes do_kernel_fault(struct pt_regs *regs, int si_code,
167 int fault_code, unsigned int insn,
168 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned char asi = ASI_P;
171
172 if ((!insn) && (regs->tstate & TSTATE_PRIV))
173 goto cannot_handle;
174
175 /* If user insn could be read (thus insn is zero), that
176 * is fine. We will just gun down the process with a signal
177 * in that case.
178 */
179
180 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
181 (insn & 0xc0800000) == 0xc0800000) {
182 if (insn & 0x2000)
183 asi = (regs->tstate >> 24);
184 else
185 asi = (insn >> 5);
186 if ((asi & 0xf2) == 0x82) {
187 if (insn & 0x1000000) {
188 handle_ldf_stq(insn, regs);
189 } else {
190 /* This was a non-faulting load. Just clear the
191 * destination register(s) and continue with the next
192 * instruction. -jj
193 */
194 handle_ld_nf(insn, regs);
195 }
196 return;
197 }
198 }
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* Is this in ex_table? */
201 if (regs->tstate & TSTATE_PRIV) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700202 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
David S. Miller622eaec2008-02-26 17:30:02 -0800204 entry = search_exception_tables(regs->tpc);
205 if (entry) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700206 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return;
209 }
210 } else {
211 /* The si_code was set to make clear whether
212 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
213 */
214 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
215 return;
216 }
217
218cannot_handle:
219 unhandled_fault (address, current, regs);
220}
221
David S. Miller4ed5d5e2009-12-10 18:08:29 -0800222static void noinline __kprobes bogus_32bit_fault_tpc(struct pt_regs *regs)
David S. Miller9b026052009-02-03 16:28:23 -0800223{
224 static int times;
225
226 if (times++ < 10)
227 printk(KERN_ERR "FAULT[%s:%d]: 32-bit process reports "
228 "64-bit TPC [%lx]\n",
229 current->comm, current->pid,
230 regs->tpc);
231 show_regs(regs);
232}
233
David S. Miller4ed5d5e2009-12-10 18:08:29 -0800234static void noinline __kprobes bogus_32bit_fault_address(struct pt_regs *regs,
235 unsigned long addr)
David S. Miller9b026052009-02-03 16:28:23 -0800236{
237 static int times;
238
239 if (times++ < 10)
240 printk(KERN_ERR "FAULT[%s:%d]: 32-bit process "
241 "reports 64-bit fault address [%lx]\n",
242 current->comm, current->pid, addr);
243 show_regs(regs);
244}
245
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700246asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct mm_struct *mm = current->mm;
249 struct vm_area_struct *vma;
250 unsigned int insn = 0;
Nick Piggin83c54072007-07-19 01:47:05 -0700251 int si_code, fault_code, fault;
David S. Miller7a1ac522006-03-16 02:02:32 -0800252 unsigned long address, mm_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 fault_code = get_thread_fault_code();
255
David S. Miller127cda12007-05-08 18:25:14 -0700256 if (notify_page_fault(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return;
258
259 si_code = SEGV_MAPERR;
260 address = current_thread_info()->fault_address;
261
262 if ((fault_code & FAULT_CODE_ITLB) &&
263 (fault_code & FAULT_CODE_DTLB))
264 BUG();
265
David S. Millereeabac72009-02-02 22:08:15 -0800266 if (test_thread_flag(TIF_32BIT)) {
David S. Miller9b026052009-02-03 16:28:23 -0800267 if (!(regs->tstate & TSTATE_PRIV)) {
268 if (unlikely((regs->tpc >> 32) != 0)) {
269 bogus_32bit_fault_tpc(regs);
270 goto intr_or_no_mm;
271 }
272 }
273 if (unlikely((address >> 32) != 0)) {
274 bogus_32bit_fault_address(regs, address);
275 goto intr_or_no_mm;
276 }
David S. Millereeabac72009-02-02 22:08:15 -0800277 }
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (regs->tstate & TSTATE_PRIV) {
David S. Miller9b026052009-02-03 16:28:23 -0800280 unsigned long tpc = regs->tpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* Sanity check the PC. */
David S. Millerbe717162008-02-28 20:38:15 -0800283 if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
285 /* Valid, no problems... */
286 } else {
David S. Millerbf941d62006-02-13 18:07:45 -0800287 bad_kernel_pc(regs, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return;
289 }
290 }
291
292 /*
293 * If we're in an interrupt or have no user
294 * context, we must not take the fault..
295 */
296 if (in_atomic() || !mm)
297 goto intr_or_no_mm;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!down_read_trylock(&mm->mmap_sem)) {
300 if ((regs->tstate & TSTATE_PRIV) &&
301 !search_exception_tables(regs->tpc)) {
302 insn = get_fault_insn(regs, insn);
303 goto handle_kernel_fault;
304 }
305 down_read(&mm->mmap_sem);
306 }
307
308 vma = find_vma(mm, address);
309 if (!vma)
310 goto bad_area;
311
312 /* Pure DTLB misses do not tell us whether the fault causing
313 * load/store/atomic was a write or not, it only says that there
314 * was no match. So in such a case we (carefully) read the
315 * instruction to try and figure this out. It's an optimization
316 * so it's ok if we can't do this.
317 *
318 * Special hack, window spill/fill knows the exact fault type.
319 */
320 if (((fault_code &
321 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
322 (vma->vm_flags & VM_WRITE) != 0) {
323 insn = get_fault_insn(regs, 0);
324 if (!insn)
325 goto continue_fault;
David S. Miller73c50a22006-03-28 13:32:24 -0800326 /* All loads, stores and atomics have bits 30 and 31 both set
327 * in the instruction. Bit 21 is set in all stores, but we
328 * have to avoid prefetches which also have bit 21 set.
329 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if ((insn & 0xc0200000) == 0xc0200000 &&
David S. Miller73c50a22006-03-28 13:32:24 -0800331 (insn & 0x01780000) != 0x01680000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* Don't bother updating thread struct value,
333 * because update_mmu_cache only cares which tlb
334 * the access came from.
335 */
336 fault_code |= FAULT_CODE_WRITE;
337 }
338 }
339continue_fault:
340
341 if (vma->vm_start <= address)
342 goto good_area;
343 if (!(vma->vm_flags & VM_GROWSDOWN))
344 goto bad_area;
345 if (!(fault_code & FAULT_CODE_WRITE)) {
346 /* Non-faulting loads shouldn't expand stack. */
347 insn = get_fault_insn(regs, insn);
348 if ((insn & 0xc0800000) == 0xc0800000) {
349 unsigned char asi;
350
351 if (insn & 0x2000)
352 asi = (regs->tstate >> 24);
353 else
354 asi = (insn >> 5);
355 if ((asi & 0xf2) == 0x82)
356 goto bad_area;
357 }
358 }
359 if (expand_stack(vma, address))
360 goto bad_area;
361 /*
362 * Ok, we have a good vm_area for this memory access, so
363 * we can handle it..
364 */
365good_area:
366 si_code = SEGV_ACCERR;
367
368 /* If we took a ITLB miss on a non-executable page, catch
369 * that here.
370 */
371 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
372 BUG_ON(address != regs->tpc);
373 BUG_ON(regs->tstate & TSTATE_PRIV);
374 goto bad_area;
375 }
376
377 if (fault_code & FAULT_CODE_WRITE) {
378 if (!(vma->vm_flags & VM_WRITE))
379 goto bad_area;
380
381 /* Spitfire has an icache which does not snoop
382 * processor stores. Later processors do...
383 */
384 if (tlb_type == spitfire &&
385 (vma->vm_flags & VM_EXEC) != 0 &&
386 vma->vm_file != NULL)
387 set_thread_fault_code(fault_code |
388 FAULT_CODE_BLKCOMMIT);
389 } else {
390 /* Allow reads even for write-only mappings */
391 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
392 goto bad_area;
393 }
394
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700395 fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE) ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -0700396 if (unlikely(fault & VM_FAULT_ERROR)) {
397 if (fault & VM_FAULT_OOM)
398 goto out_of_memory;
399 else if (fault & VM_FAULT_SIGBUS)
400 goto do_sigbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 BUG();
402 }
Nick Piggin83c54072007-07-19 01:47:05 -0700403 if (fault & VM_FAULT_MAJOR)
404 current->maj_flt++;
405 else
406 current->min_flt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 up_read(&mm->mmap_sem);
David S. Miller7a1ac522006-03-16 02:02:32 -0800409
410 mm_rss = get_mm_rss(mm);
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800411#ifdef CONFIG_HUGETLB_PAGE
412 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
413#endif
David S. Miller7bebd832006-03-27 01:07:55 -0800414 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800415 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
416 tsb_grow(mm, MM_TSB_BASE, mm_rss);
417#ifdef CONFIG_HUGETLB_PAGE
418 mm_rss = mm->context.huge_pte_count;
David S. Miller7bebd832006-03-27 01:07:55 -0800419 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800420 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
421 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
422#endif
David S. Millerefdc1e22005-09-28 21:06:47 -0700423 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /*
426 * Something tried to access memory that isn't in our memory map..
427 * Fix it, but check if it's kernel or user first..
428 */
429bad_area:
430 insn = get_fault_insn(regs, insn);
431 up_read(&mm->mmap_sem);
432
433handle_kernel_fault:
434 do_kernel_fault(regs, si_code, fault_code, insn, address);
David S. Millerefdc1e22005-09-28 21:06:47 -0700435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437/*
438 * We ran out of memory, or some other thing happened to us that made
439 * us unable to handle the page fault gracefully.
440 */
441out_of_memory:
442 insn = get_fault_insn(regs, insn);
443 up_read(&mm->mmap_sem);
David S. Millera923c282009-08-02 19:17:15 -0700444 if (!(regs->tstate & TSTATE_PRIV)) {
445 pagefault_out_of_memory();
446 return;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto handle_kernel_fault;
449
450intr_or_no_mm:
451 insn = get_fault_insn(regs, 0);
452 goto handle_kernel_fault;
453
454do_sigbus:
455 insn = get_fault_insn(regs, insn);
456 up_read(&mm->mmap_sem);
457
458 /*
459 * Send a sigbus, regardless of whether we were in kernel
460 * or user mode.
461 */
462 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
463
464 /* Kernel mode? Handle exceptions or die */
465 if (regs->tstate & TSTATE_PRIV)
466 goto handle_kernel_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}