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