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