]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/sparc64/mm/fault.c
mm: fault feedback #2
[net-next-2.6.git] / arch / sparc64 / mm / fault.c
CommitLineData
1da177e4
LT
1/* $Id: fault.c,v 1.59 2002/02/09 19:49:31 davem Exp $
2 * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
3 *
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * 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>
1da177e4
LT
18#include <linux/init.h>
19#include <linux/interrupt.h>
05e14cb3 20#include <linux/kprobes.h>
eb398d10 21#include <linux/kallsyms.h>
1eeb66a1 22#include <linux/kdebug.h>
1da177e4
LT
23
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>
7a1ac526 32#include <asm/mmu_context.h>
1da177e4 33
d98f8f05 34#ifdef CONFIG_KPROBES
127cda1e 35static inline int notify_page_fault(struct pt_regs *regs)
d98f8f05 36{
127cda1e
DM
37 int ret = 0;
38
39 /* 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;
d98f8f05
AK
47}
48#else
127cda1e 49static inline int notify_page_fault(struct pt_regs *regs)
d98f8f05 50{
127cda1e 51 return 0;
d98f8f05
AK
52}
53#endif
54
1da177e4
LT
55/*
56 * To debug kernel to catch accesses to certain virtual/physical addresses.
57 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
58 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
59 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
60 * watched. This is only useful on a single cpu machine for now. After the watchpoint
61 * is detected, the process causing it will be killed, thus preventing an infinite loop.
62 */
63void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
64{
65 unsigned long lsubits;
66
67 __asm__ __volatile__("ldxa [%%g0] %1, %0"
68 : "=r" (lsubits)
69 : "i" (ASI_LSU_CONTROL));
70 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
71 LSU_CONTROL_PR | LSU_CONTROL_VR |
72 LSU_CONTROL_PW | LSU_CONTROL_VW);
73
74 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
75 "membar #Sync"
76 : /* no outputs */
77 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
78 "i" (ASI_DMMU));
79
80 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
81 if (flags & VM_READ)
82 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
83 if (flags & VM_WRITE)
84 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
85 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
86 "membar #Sync"
87 : /* no outputs */
88 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
89 : "memory");
90}
91
05e14cb3
PP
92static void __kprobes unhandled_fault(unsigned long address,
93 struct task_struct *tsk,
94 struct pt_regs *regs)
1da177e4
LT
95{
96 if ((unsigned long) address < PAGE_SIZE) {
97 printk(KERN_ALERT "Unable to handle kernel NULL "
98 "pointer dereference\n");
99 } else {
100 printk(KERN_ALERT "Unable to handle kernel paging request "
101 "at virtual address %016lx\n", (unsigned long)address);
102 }
103 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
104 (tsk->mm ?
105 CTX_HWBITS(tsk->mm->context) :
106 CTX_HWBITS(tsk->active_mm->context)));
107 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
108 (tsk->mm ? (unsigned long) tsk->mm->pgd :
109 (unsigned long) tsk->active_mm->pgd));
1da177e4
LT
110 die_if_kernel("Oops", regs);
111}
112
bf941d6c 113static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
1da177e4
LT
114{
115 unsigned long *ksp;
116
117 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
118 regs->tpc);
eb398d10
DM
119 printk(KERN_CRIT "OOPS: RPC [%016lx]\n", regs->u_regs[15]);
120 print_symbol("RPC: <%s>\n", regs->u_regs[15]);
bf941d6c 121 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
1da177e4
LT
122 __asm__("mov %%sp, %0" : "=r" (ksp));
123 show_stack(current, ksp);
124 unhandled_fault(regs->tpc, current, regs);
125}
126
127/*
128 * We now make sure that mmap_sem is held in all paths that call
129 * this. Additionally, to prevent kswapd from ripping ptes from
130 * under us, raise interrupts around the time that we look at the
131 * pte, kswapd will have to wait to get his smp ipi response from
da160546 132 * us. vmtruncate likewise. This saves us having to get pte lock.
1da177e4
LT
133 */
134static unsigned int get_user_insn(unsigned long tpc)
135{
136 pgd_t *pgdp = pgd_offset(current->mm, tpc);
137 pud_t *pudp;
138 pmd_t *pmdp;
139 pte_t *ptep, pte;
140 unsigned long pa;
141 u32 insn = 0;
142 unsigned long pstate;
143
144 if (pgd_none(*pgdp))
145 goto outret;
146 pudp = pud_offset(pgdp, tpc);
147 if (pud_none(*pudp))
148 goto outret;
149 pmdp = pmd_offset(pudp, tpc);
150 if (pmd_none(*pmdp))
151 goto outret;
152
153 /* This disables preemption for us as well. */
154 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
155 __asm__ __volatile__("wrpr %0, %1, %%pstate"
156 : : "r" (pstate), "i" (PSTATE_IE));
157 ptep = pte_offset_map(pmdp, tpc);
158 pte = *ptep;
159 if (!pte_present(pte))
160 goto out;
161
c4bce90e 162 pa = (pte_pfn(pte) << PAGE_SHIFT);
1da177e4
LT
163 pa += (tpc & ~PAGE_MASK);
164
165 /* Use phys bypass so we don't pollute dtlb/dcache. */
166 __asm__ __volatile__("lduwa [%1] %2, %0"
167 : "=r" (insn)
168 : "r" (pa), "i" (ASI_PHYS_USE_EC));
169
170out:
171 pte_unmap(ptep);
172 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
173outret:
174 return insn;
175}
176
177extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
178
179static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
180 unsigned int insn, int fault_code)
181{
182 siginfo_t info;
183
184 info.si_code = code;
185 info.si_signo = sig;
186 info.si_errno = 0;
187 if (fault_code & FAULT_CODE_ITLB)
188 info.si_addr = (void __user *) regs->tpc;
189 else
190 info.si_addr = (void __user *)
191 compute_effective_address(regs, insn, 0);
192 info.si_trapno = 0;
193 force_sig_info(sig, &info, current);
194}
195
196extern int handle_ldf_stq(u32, struct pt_regs *);
197extern int handle_ld_nf(u32, struct pt_regs *);
198
199static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
200{
201 if (!insn) {
202 if (!regs->tpc || (regs->tpc & 0x3))
203 return 0;
204 if (regs->tstate & TSTATE_PRIV) {
205 insn = *(unsigned int *) regs->tpc;
206 } else {
207 insn = get_user_insn(regs->tpc);
208 }
209 }
210 return insn;
211}
212
213static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
214 unsigned int insn, unsigned long address)
215{
1da177e4
LT
216 unsigned char asi = ASI_P;
217
218 if ((!insn) && (regs->tstate & TSTATE_PRIV))
219 goto cannot_handle;
220
221 /* If user insn could be read (thus insn is zero), that
222 * is fine. We will just gun down the process with a signal
223 * in that case.
224 */
225
226 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
227 (insn & 0xc0800000) == 0xc0800000) {
228 if (insn & 0x2000)
229 asi = (regs->tstate >> 24);
230 else
231 asi = (insn >> 5);
232 if ((asi & 0xf2) == 0x82) {
233 if (insn & 0x1000000) {
234 handle_ldf_stq(insn, regs);
235 } else {
236 /* This was a non-faulting load. Just clear the
237 * destination register(s) and continue with the next
238 * instruction. -jj
239 */
240 handle_ld_nf(insn, regs);
241 }
242 return;
243 }
244 }
245
1da177e4
LT
246 /* Is this in ex_table? */
247 if (regs->tstate & TSTATE_PRIV) {
8cf14af0 248 const struct exception_table_entry *entry;
1da177e4
LT
249
250 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
251 if (insn & 0x2000)
252 asi = (regs->tstate >> 24);
253 else
254 asi = (insn >> 5);
255 }
256
257 /* Look in asi.h: All _S asis have LS bit set */
258 if ((asi & 0x1) &&
8cf14af0
DM
259 (entry = search_exception_tables(regs->tpc))) {
260 regs->tpc = entry->fixup;
1da177e4 261 regs->tnpc = regs->tpc + 4;
1da177e4
LT
262 return;
263 }
264 } else {
265 /* The si_code was set to make clear whether
266 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
267 */
268 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
269 return;
270 }
271
272cannot_handle:
273 unhandled_fault (address, current, regs);
274}
275
05e14cb3 276asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
1da177e4
LT
277{
278 struct mm_struct *mm = current->mm;
279 struct vm_area_struct *vma;
280 unsigned int insn = 0;
83c54070 281 int si_code, fault_code, fault;
7a1ac526 282 unsigned long address, mm_rss;
1da177e4
LT
283
284 fault_code = get_thread_fault_code();
285
127cda1e 286 if (notify_page_fault(regs))
1da177e4
LT
287 return;
288
289 si_code = SEGV_MAPERR;
290 address = current_thread_info()->fault_address;
291
292 if ((fault_code & FAULT_CODE_ITLB) &&
293 (fault_code & FAULT_CODE_DTLB))
294 BUG();
295
296 if (regs->tstate & TSTATE_PRIV) {
297 unsigned long tpc = regs->tpc;
298
299 /* Sanity check the PC. */
300 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
301 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
302 /* Valid, no problems... */
303 } else {
bf941d6c 304 bad_kernel_pc(regs, address);
1da177e4
LT
305 return;
306 }
307 }
308
309 /*
310 * If we're in an interrupt or have no user
311 * context, we must not take the fault..
312 */
313 if (in_atomic() || !mm)
314 goto intr_or_no_mm;
315
316 if (test_thread_flag(TIF_32BIT)) {
317 if (!(regs->tstate & TSTATE_PRIV))
318 regs->tpc &= 0xffffffff;
319 address &= 0xffffffff;
320 }
321
322 if (!down_read_trylock(&mm->mmap_sem)) {
323 if ((regs->tstate & TSTATE_PRIV) &&
324 !search_exception_tables(regs->tpc)) {
325 insn = get_fault_insn(regs, insn);
326 goto handle_kernel_fault;
327 }
328 down_read(&mm->mmap_sem);
329 }
330
331 vma = find_vma(mm, address);
332 if (!vma)
333 goto bad_area;
334
335 /* Pure DTLB misses do not tell us whether the fault causing
336 * load/store/atomic was a write or not, it only says that there
337 * was no match. So in such a case we (carefully) read the
338 * instruction to try and figure this out. It's an optimization
339 * so it's ok if we can't do this.
340 *
341 * Special hack, window spill/fill knows the exact fault type.
342 */
343 if (((fault_code &
344 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
345 (vma->vm_flags & VM_WRITE) != 0) {
346 insn = get_fault_insn(regs, 0);
347 if (!insn)
348 goto continue_fault;
73c50a27
DM
349 /* All loads, stores and atomics have bits 30 and 31 both set
350 * in the instruction. Bit 21 is set in all stores, but we
351 * have to avoid prefetches which also have bit 21 set.
352 */
1da177e4 353 if ((insn & 0xc0200000) == 0xc0200000 &&
73c50a27 354 (insn & 0x01780000) != 0x01680000) {
1da177e4
LT
355 /* Don't bother updating thread struct value,
356 * because update_mmu_cache only cares which tlb
357 * the access came from.
358 */
359 fault_code |= FAULT_CODE_WRITE;
360 }
361 }
362continue_fault:
363
364 if (vma->vm_start <= address)
365 goto good_area;
366 if (!(vma->vm_flags & VM_GROWSDOWN))
367 goto bad_area;
368 if (!(fault_code & FAULT_CODE_WRITE)) {
369 /* Non-faulting loads shouldn't expand stack. */
370 insn = get_fault_insn(regs, insn);
371 if ((insn & 0xc0800000) == 0xc0800000) {
372 unsigned char asi;
373
374 if (insn & 0x2000)
375 asi = (regs->tstate >> 24);
376 else
377 asi = (insn >> 5);
378 if ((asi & 0xf2) == 0x82)
379 goto bad_area;
380 }
381 }
382 if (expand_stack(vma, address))
383 goto bad_area;
384 /*
385 * Ok, we have a good vm_area for this memory access, so
386 * we can handle it..
387 */
388good_area:
389 si_code = SEGV_ACCERR;
390
391 /* If we took a ITLB miss on a non-executable page, catch
392 * that here.
393 */
394 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
395 BUG_ON(address != regs->tpc);
396 BUG_ON(regs->tstate & TSTATE_PRIV);
397 goto bad_area;
398 }
399
400 if (fault_code & FAULT_CODE_WRITE) {
401 if (!(vma->vm_flags & VM_WRITE))
402 goto bad_area;
403
404 /* Spitfire has an icache which does not snoop
405 * processor stores. Later processors do...
406 */
407 if (tlb_type == spitfire &&
408 (vma->vm_flags & VM_EXEC) != 0 &&
409 vma->vm_file != NULL)
410 set_thread_fault_code(fault_code |
411 FAULT_CODE_BLKCOMMIT);
412 } else {
413 /* Allow reads even for write-only mappings */
414 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
415 goto bad_area;
416 }
417
83c54070
NP
418 fault = handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE));
419 if (unlikely(fault & VM_FAULT_ERROR)) {
420 if (fault & VM_FAULT_OOM)
421 goto out_of_memory;
422 else if (fault & VM_FAULT_SIGBUS)
423 goto do_sigbus;
1da177e4
LT
424 BUG();
425 }
83c54070
NP
426 if (fault & VM_FAULT_MAJOR)
427 current->maj_flt++;
428 else
429 current->min_flt++;
1da177e4
LT
430
431 up_read(&mm->mmap_sem);
7a1ac526
DM
432
433 mm_rss = get_mm_rss(mm);
dcc1e8dd
DM
434#ifdef CONFIG_HUGETLB_PAGE
435 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
436#endif
7bebd83d 437 if (unlikely(mm_rss >
dcc1e8dd
DM
438 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
439 tsb_grow(mm, MM_TSB_BASE, mm_rss);
440#ifdef CONFIG_HUGETLB_PAGE
441 mm_rss = mm->context.huge_pte_count;
7bebd83d 442 if (unlikely(mm_rss >
dcc1e8dd
DM
443 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
444 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
445#endif
efdc1e20 446 return;
1da177e4
LT
447
448 /*
449 * Something tried to access memory that isn't in our memory map..
450 * Fix it, but check if it's kernel or user first..
451 */
452bad_area:
453 insn = get_fault_insn(regs, insn);
454 up_read(&mm->mmap_sem);
455
456handle_kernel_fault:
457 do_kernel_fault(regs, si_code, fault_code, insn, address);
efdc1e20 458 return;
1da177e4
LT
459
460/*
461 * We ran out of memory, or some other thing happened to us that made
462 * us unable to handle the page fault gracefully.
463 */
464out_of_memory:
465 insn = get_fault_insn(regs, insn);
466 up_read(&mm->mmap_sem);
467 printk("VM: killing process %s\n", current->comm);
468 if (!(regs->tstate & TSTATE_PRIV))
469 do_exit(SIGKILL);
470 goto handle_kernel_fault;
471
472intr_or_no_mm:
473 insn = get_fault_insn(regs, 0);
474 goto handle_kernel_fault;
475
476do_sigbus:
477 insn = get_fault_insn(regs, insn);
478 up_read(&mm->mmap_sem);
479
480 /*
481 * Send a sigbus, regardless of whether we were in kernel
482 * or user mode.
483 */
484 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
485
486 /* Kernel mode? Handle exceptions or die */
487 if (regs->tstate & TSTATE_PRIV)
488 goto handle_kernel_fault;
1da177e4 489}