blob: 1a786abdada3d19abdf2a01949e81513b957db7b [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
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
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070055static void __kprobes unhandled_fault(unsigned long address,
56 struct task_struct *tsk,
57 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 if ((unsigned long) address < PAGE_SIZE) {
60 printk(KERN_ALERT "Unable to handle kernel NULL "
61 "pointer dereference\n");
62 } else {
63 printk(KERN_ALERT "Unable to handle kernel paging request "
64 "at virtual address %016lx\n", (unsigned long)address);
65 }
66 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
67 (tsk->mm ?
68 CTX_HWBITS(tsk->mm->context) :
69 CTX_HWBITS(tsk->active_mm->context)));
70 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
71 (tsk->mm ? (unsigned long) tsk->mm->pgd :
72 (unsigned long) tsk->active_mm->pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 die_if_kernel("Oops", regs);
74}
75
David S. Millerbf941d62006-02-13 18:07:45 -080076static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
79 regs->tpc);
David S. Millereb398d12006-07-22 01:12:09 -070080 printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
David S. Miller4fe3ebe2008-07-17 22:11:32 -070081 printk("OOPS: RPC <%pS>\n", (void *) regs->u_regs[15]);
David S. Millerbf941d62006-02-13 18:07:45 -080082 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
David S. Millerc1f193a2007-07-30 00:17:12 -070083 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unhandled_fault(regs->tpc, current, regs);
85}
86
87/*
88 * We now make sure that mmap_sem is held in all paths that call
89 * this. Additionally, to prevent kswapd from ripping ptes from
90 * under us, raise interrupts around the time that we look at the
91 * pte, kswapd will have to wait to get his smp ipi response from
Hugh Dickinsda160542005-11-08 10:00:55 -080092 * us. vmtruncate likewise. This saves us having to get pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
94static unsigned int get_user_insn(unsigned long tpc)
95{
96 pgd_t *pgdp = pgd_offset(current->mm, tpc);
97 pud_t *pudp;
98 pmd_t *pmdp;
99 pte_t *ptep, pte;
100 unsigned long pa;
101 u32 insn = 0;
102 unsigned long pstate;
103
104 if (pgd_none(*pgdp))
105 goto outret;
106 pudp = pud_offset(pgdp, tpc);
107 if (pud_none(*pudp))
108 goto outret;
109 pmdp = pmd_offset(pudp, tpc);
110 if (pmd_none(*pmdp))
111 goto outret;
112
113 /* This disables preemption for us as well. */
114 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
115 __asm__ __volatile__("wrpr %0, %1, %%pstate"
116 : : "r" (pstate), "i" (PSTATE_IE));
117 ptep = pte_offset_map(pmdp, tpc);
118 pte = *ptep;
119 if (!pte_present(pte))
120 goto out;
121
David S. Millerc4bce902006-02-11 21:57:54 -0800122 pa = (pte_pfn(pte) << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 pa += (tpc & ~PAGE_MASK);
124
125 /* Use phys bypass so we don't pollute dtlb/dcache. */
126 __asm__ __volatile__("lduwa [%1] %2, %0"
127 : "=r" (insn)
128 : "r" (pa), "i" (ASI_PHYS_USE_EC));
129
130out:
131 pte_unmap(ptep);
132 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
133outret:
134 return insn;
135}
136
137extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
138
139static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
140 unsigned int insn, int fault_code)
141{
142 siginfo_t info;
143
144 info.si_code = code;
145 info.si_signo = sig;
146 info.si_errno = 0;
147 if (fault_code & FAULT_CODE_ITLB)
148 info.si_addr = (void __user *) regs->tpc;
149 else
150 info.si_addr = (void __user *)
151 compute_effective_address(regs, insn, 0);
152 info.si_trapno = 0;
153 force_sig_info(sig, &info, current);
154}
155
156extern int handle_ldf_stq(u32, struct pt_regs *);
157extern int handle_ld_nf(u32, struct pt_regs *);
158
159static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
160{
161 if (!insn) {
162 if (!regs->tpc || (regs->tpc & 0x3))
163 return 0;
164 if (regs->tstate & TSTATE_PRIV) {
165 insn = *(unsigned int *) regs->tpc;
166 } else {
167 insn = get_user_insn(regs->tpc);
168 }
169 }
170 return insn;
171}
172
173static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
174 unsigned int insn, unsigned long address)
175{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 unsigned char asi = ASI_P;
177
178 if ((!insn) && (regs->tstate & TSTATE_PRIV))
179 goto cannot_handle;
180
181 /* If user insn could be read (thus insn is zero), that
182 * is fine. We will just gun down the process with a signal
183 * in that case.
184 */
185
186 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
187 (insn & 0xc0800000) == 0xc0800000) {
188 if (insn & 0x2000)
189 asi = (regs->tstate >> 24);
190 else
191 asi = (insn >> 5);
192 if ((asi & 0xf2) == 0x82) {
193 if (insn & 0x1000000) {
194 handle_ldf_stq(insn, regs);
195 } else {
196 /* This was a non-faulting load. Just clear the
197 * destination register(s) and continue with the next
198 * instruction. -jj
199 */
200 handle_ld_nf(insn, regs);
201 }
202 return;
203 }
204 }
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* Is this in ex_table? */
207 if (regs->tstate & TSTATE_PRIV) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700208 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
David S. Miller622eaec2008-02-26 17:30:02 -0800210 entry = search_exception_tables(regs->tpc);
211 if (entry) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700212 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return;
215 }
216 } else {
217 /* The si_code was set to make clear whether
218 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
219 */
220 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
221 return;
222 }
223
224cannot_handle:
225 unhandled_fault (address, current, regs);
226}
227
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700228asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct mm_struct *mm = current->mm;
231 struct vm_area_struct *vma;
232 unsigned int insn = 0;
Nick Piggin83c54072007-07-19 01:47:05 -0700233 int si_code, fault_code, fault;
David S. Miller7a1ac522006-03-16 02:02:32 -0800234 unsigned long address, mm_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 fault_code = get_thread_fault_code();
237
David S. Miller127cda12007-05-08 18:25:14 -0700238 if (notify_page_fault(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return;
240
241 si_code = SEGV_MAPERR;
242 address = current_thread_info()->fault_address;
243
244 if ((fault_code & FAULT_CODE_ITLB) &&
245 (fault_code & FAULT_CODE_DTLB))
246 BUG();
247
David S. Millereeabac72009-02-02 22:08:15 -0800248 if (test_thread_flag(TIF_32BIT)) {
249 if (!(regs->tstate & TSTATE_PRIV))
250 regs->tpc &= 0xffffffff;
251 address &= 0xffffffff;
252 }
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (regs->tstate & TSTATE_PRIV) {
David S. Millereeabac72009-02-02 22:08:15 -0800255 unsigned long eaddr, tpc = regs->tpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* Sanity check the PC. */
David S. Millerbe717162008-02-28 20:38:15 -0800258 if ((tpc >= KERNBASE && tpc < (unsigned long) __init_end) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
260 /* Valid, no problems... */
261 } else {
David S. Millerbf941d62006-02-13 18:07:45 -0800262 bad_kernel_pc(regs, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return;
264 }
David S. Millereeabac72009-02-02 22:08:15 -0800265
266 insn = get_fault_insn(regs, insn);
267 eaddr = compute_effective_address(regs, insn, 0);
268 if (WARN_ON_ONCE((eaddr & PAGE_MASK) != (address & PAGE_MASK))){
269 printk(KERN_ERR "FAULT: Mismatch kernel fault "
270 "address: addr[%lx] eaddr[%lx] TPC[%lx]\n",
271 address, eaddr, tpc);
272 show_regs(regs);
273 goto handle_kernel_fault;
274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
277 /*
278 * If we're in an interrupt or have no user
279 * context, we must not take the fault..
280 */
281 if (in_atomic() || !mm)
282 goto intr_or_no_mm;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!down_read_trylock(&mm->mmap_sem)) {
285 if ((regs->tstate & TSTATE_PRIV) &&
286 !search_exception_tables(regs->tpc)) {
287 insn = get_fault_insn(regs, insn);
288 goto handle_kernel_fault;
289 }
290 down_read(&mm->mmap_sem);
291 }
292
293 vma = find_vma(mm, address);
294 if (!vma)
295 goto bad_area;
296
297 /* Pure DTLB misses do not tell us whether the fault causing
298 * load/store/atomic was a write or not, it only says that there
299 * was no match. So in such a case we (carefully) read the
300 * instruction to try and figure this out. It's an optimization
301 * so it's ok if we can't do this.
302 *
303 * Special hack, window spill/fill knows the exact fault type.
304 */
305 if (((fault_code &
306 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
307 (vma->vm_flags & VM_WRITE) != 0) {
308 insn = get_fault_insn(regs, 0);
309 if (!insn)
310 goto continue_fault;
David S. Miller73c50a22006-03-28 13:32:24 -0800311 /* All loads, stores and atomics have bits 30 and 31 both set
312 * in the instruction. Bit 21 is set in all stores, but we
313 * have to avoid prefetches which also have bit 21 set.
314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if ((insn & 0xc0200000) == 0xc0200000 &&
David S. Miller73c50a22006-03-28 13:32:24 -0800316 (insn & 0x01780000) != 0x01680000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* Don't bother updating thread struct value,
318 * because update_mmu_cache only cares which tlb
319 * the access came from.
320 */
321 fault_code |= FAULT_CODE_WRITE;
322 }
323 }
324continue_fault:
325
326 if (vma->vm_start <= address)
327 goto good_area;
328 if (!(vma->vm_flags & VM_GROWSDOWN))
329 goto bad_area;
330 if (!(fault_code & FAULT_CODE_WRITE)) {
331 /* Non-faulting loads shouldn't expand stack. */
332 insn = get_fault_insn(regs, insn);
333 if ((insn & 0xc0800000) == 0xc0800000) {
334 unsigned char asi;
335
336 if (insn & 0x2000)
337 asi = (regs->tstate >> 24);
338 else
339 asi = (insn >> 5);
340 if ((asi & 0xf2) == 0x82)
341 goto bad_area;
342 }
343 }
344 if (expand_stack(vma, address))
345 goto bad_area;
346 /*
347 * Ok, we have a good vm_area for this memory access, so
348 * we can handle it..
349 */
350good_area:
351 si_code = SEGV_ACCERR;
352
353 /* If we took a ITLB miss on a non-executable page, catch
354 * that here.
355 */
356 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
357 BUG_ON(address != regs->tpc);
358 BUG_ON(regs->tstate & TSTATE_PRIV);
359 goto bad_area;
360 }
361
362 if (fault_code & FAULT_CODE_WRITE) {
363 if (!(vma->vm_flags & VM_WRITE))
364 goto bad_area;
365
366 /* Spitfire has an icache which does not snoop
367 * processor stores. Later processors do...
368 */
369 if (tlb_type == spitfire &&
370 (vma->vm_flags & VM_EXEC) != 0 &&
371 vma->vm_file != NULL)
372 set_thread_fault_code(fault_code |
373 FAULT_CODE_BLKCOMMIT);
374 } else {
375 /* Allow reads even for write-only mappings */
376 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
377 goto bad_area;
378 }
379
Nick Piggin83c54072007-07-19 01:47:05 -0700380 fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE));
381 if (unlikely(fault & VM_FAULT_ERROR)) {
382 if (fault & VM_FAULT_OOM)
383 goto out_of_memory;
384 else if (fault & VM_FAULT_SIGBUS)
385 goto do_sigbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 BUG();
387 }
Nick Piggin83c54072007-07-19 01:47:05 -0700388 if (fault & VM_FAULT_MAJOR)
389 current->maj_flt++;
390 else
391 current->min_flt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 up_read(&mm->mmap_sem);
David S. Miller7a1ac522006-03-16 02:02:32 -0800394
395 mm_rss = get_mm_rss(mm);
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800396#ifdef CONFIG_HUGETLB_PAGE
397 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
398#endif
David S. Miller7bebd832006-03-27 01:07:55 -0800399 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800400 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
401 tsb_grow(mm, MM_TSB_BASE, mm_rss);
402#ifdef CONFIG_HUGETLB_PAGE
403 mm_rss = mm->context.huge_pte_count;
David S. Miller7bebd832006-03-27 01:07:55 -0800404 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800405 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
406 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
407#endif
David S. Millerefdc1e22005-09-28 21:06:47 -0700408 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /*
411 * Something tried to access memory that isn't in our memory map..
412 * Fix it, but check if it's kernel or user first..
413 */
414bad_area:
415 insn = get_fault_insn(regs, insn);
416 up_read(&mm->mmap_sem);
417
418handle_kernel_fault:
419 do_kernel_fault(regs, si_code, fault_code, insn, address);
David S. Millerefdc1e22005-09-28 21:06:47 -0700420 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422/*
423 * We ran out of memory, or some other thing happened to us that made
424 * us unable to handle the page fault gracefully.
425 */
426out_of_memory:
427 insn = get_fault_insn(regs, insn);
428 up_read(&mm->mmap_sem);
429 printk("VM: killing process %s\n", current->comm);
430 if (!(regs->tstate & TSTATE_PRIV))
Will Schmidtdcca2bd2007-10-16 01:24:18 -0700431 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto handle_kernel_fault;
433
434intr_or_no_mm:
435 insn = get_fault_insn(regs, 0);
436 goto handle_kernel_fault;
437
438do_sigbus:
439 insn = get_fault_insn(regs, insn);
440 up_read(&mm->mmap_sem);
441
442 /*
443 * Send a sigbus, regardless of whether we were in kernel
444 * or user mode.
445 */
446 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
447
448 /* Kernel mode? Handle exceptions or die */
449 if (regs->tstate & TSTATE_PRIV)
450 goto handle_kernel_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}