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.
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
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>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/kprobes.h>
21 #include <linux/kallsyms.h>
22 #include <linux/kdebug.h>
25 #include <asm/pgtable.h>
26 #include <asm/openprom.h>
27 #include <asm/oplib.h>
28 #include <asm/uaccess.h>
31 #include <asm/sections.h>
32 #include <asm/mmu_context.h>
35 ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain
);
37 /* Hook to register for page fault notifications */
38 int register_page_fault_notifier(struct notifier_block
*nb
)
40 return atomic_notifier_chain_register(¬ify_page_fault_chain
, nb
);
43 int unregister_page_fault_notifier(struct notifier_block
*nb
)
45 return atomic_notifier_chain_unregister(¬ify_page_fault_chain
, nb
);
48 static inline int notify_page_fault(enum die_val val
, const char *str
,
49 struct pt_regs
*regs
, long err
, int trap
, int sig
)
51 struct die_args args
= {
58 return atomic_notifier_call_chain(¬ify_page_fault_chain
, val
, &args
);
61 static inline int notify_page_fault(enum die_val val
, const char *str
,
62 struct pt_regs
*regs
, long err
, int trap
, int sig
)
69 * To debug kernel to catch accesses to certain virtual/physical addresses.
70 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
71 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
72 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
73 * watched. This is only useful on a single cpu machine for now. After the watchpoint
74 * is detected, the process causing it will be killed, thus preventing an infinite loop.
76 void set_brkpt(unsigned long addr
, unsigned char mask
, int flags
, int mode
)
78 unsigned long lsubits
;
80 __asm__
__volatile__("ldxa [%%g0] %1, %0"
82 : "i" (ASI_LSU_CONTROL
));
83 lsubits
&= ~(LSU_CONTROL_PM
| LSU_CONTROL_VM
|
84 LSU_CONTROL_PR
| LSU_CONTROL_VR
|
85 LSU_CONTROL_PW
| LSU_CONTROL_VW
);
87 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
90 : "r" (addr
), "r" (mode
? VIRT_WATCHPOINT
: PHYS_WATCHPOINT
),
93 lsubits
|= ((unsigned long)mask
<< (mode
? 25 : 33));
95 lsubits
|= (mode
? LSU_CONTROL_VR
: LSU_CONTROL_PR
);
97 lsubits
|= (mode
? LSU_CONTROL_VW
: LSU_CONTROL_PW
);
98 __asm__
__volatile__("stxa %0, [%%g0] %1\n\t"
101 : "r" (lsubits
), "i" (ASI_LSU_CONTROL
)
105 static void __kprobes
unhandled_fault(unsigned long address
,
106 struct task_struct
*tsk
,
107 struct pt_regs
*regs
)
109 if ((unsigned long) address
< PAGE_SIZE
) {
110 printk(KERN_ALERT
"Unable to handle kernel NULL "
111 "pointer dereference\n");
113 printk(KERN_ALERT
"Unable to handle kernel paging request "
114 "at virtual address %016lx\n", (unsigned long)address
);
116 printk(KERN_ALERT
"tsk->{mm,active_mm}->context = %016lx\n",
118 CTX_HWBITS(tsk
->mm
->context
) :
119 CTX_HWBITS(tsk
->active_mm
->context
)));
120 printk(KERN_ALERT
"tsk->{mm,active_mm}->pgd = %016lx\n",
121 (tsk
->mm
? (unsigned long) tsk
->mm
->pgd
:
122 (unsigned long) tsk
->active_mm
->pgd
));
123 if (notify_die(DIE_GPF
, "general protection fault", regs
,
124 0, 0, SIGSEGV
) == NOTIFY_STOP
)
126 die_if_kernel("Oops", regs
);
129 static void bad_kernel_pc(struct pt_regs
*regs
, unsigned long vaddr
)
133 printk(KERN_CRIT
"OOPS: Bogus kernel PC [%016lx] in fault handler\n",
135 printk(KERN_CRIT
"OOPS: RPC [%016lx]\n", regs
->u_regs
[15]);
136 print_symbol("RPC: <%s>\n", regs
->u_regs
[15]);
137 printk(KERN_CRIT
"OOPS: Fault was to vaddr[%lx]\n", vaddr
);
138 __asm__("mov %%sp, %0" : "=r" (ksp
));
139 show_stack(current
, ksp
);
140 unhandled_fault(regs
->tpc
, current
, regs
);
144 * We now make sure that mmap_sem is held in all paths that call
145 * this. Additionally, to prevent kswapd from ripping ptes from
146 * under us, raise interrupts around the time that we look at the
147 * pte, kswapd will have to wait to get his smp ipi response from
148 * us. vmtruncate likewise. This saves us having to get pte lock.
150 static unsigned int get_user_insn(unsigned long tpc
)
152 pgd_t
*pgdp
= pgd_offset(current
->mm
, tpc
);
158 unsigned long pstate
;
162 pudp
= pud_offset(pgdp
, tpc
);
165 pmdp
= pmd_offset(pudp
, tpc
);
169 /* This disables preemption for us as well. */
170 __asm__
__volatile__("rdpr %%pstate, %0" : "=r" (pstate
));
171 __asm__
__volatile__("wrpr %0, %1, %%pstate"
172 : : "r" (pstate
), "i" (PSTATE_IE
));
173 ptep
= pte_offset_map(pmdp
, tpc
);
175 if (!pte_present(pte
))
178 pa
= (pte_pfn(pte
) << PAGE_SHIFT
);
179 pa
+= (tpc
& ~PAGE_MASK
);
181 /* Use phys bypass so we don't pollute dtlb/dcache. */
182 __asm__
__volatile__("lduwa [%1] %2, %0"
184 : "r" (pa
), "i" (ASI_PHYS_USE_EC
));
188 __asm__
__volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate
));
193 extern unsigned long compute_effective_address(struct pt_regs
*, unsigned int, unsigned int);
195 static void do_fault_siginfo(int code
, int sig
, struct pt_regs
*regs
,
196 unsigned int insn
, int fault_code
)
203 if (fault_code
& FAULT_CODE_ITLB
)
204 info
.si_addr
= (void __user
*) regs
->tpc
;
206 info
.si_addr
= (void __user
*)
207 compute_effective_address(regs
, insn
, 0);
209 force_sig_info(sig
, &info
, current
);
212 extern int handle_ldf_stq(u32
, struct pt_regs
*);
213 extern int handle_ld_nf(u32
, struct pt_regs
*);
215 static unsigned int get_fault_insn(struct pt_regs
*regs
, unsigned int insn
)
218 if (!regs
->tpc
|| (regs
->tpc
& 0x3))
220 if (regs
->tstate
& TSTATE_PRIV
) {
221 insn
= *(unsigned int *) regs
->tpc
;
223 insn
= get_user_insn(regs
->tpc
);
229 static void do_kernel_fault(struct pt_regs
*regs
, int si_code
, int fault_code
,
230 unsigned int insn
, unsigned long address
)
232 unsigned char asi
= ASI_P
;
234 if ((!insn
) && (regs
->tstate
& TSTATE_PRIV
))
237 /* If user insn could be read (thus insn is zero), that
238 * is fine. We will just gun down the process with a signal
242 if (!(fault_code
& (FAULT_CODE_WRITE
|FAULT_CODE_ITLB
)) &&
243 (insn
& 0xc0800000) == 0xc0800000) {
245 asi
= (regs
->tstate
>> 24);
248 if ((asi
& 0xf2) == 0x82) {
249 if (insn
& 0x1000000) {
250 handle_ldf_stq(insn
, regs
);
252 /* This was a non-faulting load. Just clear the
253 * destination register(s) and continue with the next
256 handle_ld_nf(insn
, regs
);
262 /* Is this in ex_table? */
263 if (regs
->tstate
& TSTATE_PRIV
) {
264 const struct exception_table_entry
*entry
;
266 if (asi
== ASI_P
&& (insn
& 0xc0800000) == 0xc0800000) {
268 asi
= (regs
->tstate
>> 24);
273 /* Look in asi.h: All _S asis have LS bit set */
275 (entry
= search_exception_tables(regs
->tpc
))) {
276 regs
->tpc
= entry
->fixup
;
277 regs
->tnpc
= regs
->tpc
+ 4;
281 /* The si_code was set to make clear whether
282 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
284 do_fault_siginfo(si_code
, SIGSEGV
, regs
, insn
, fault_code
);
289 unhandled_fault (address
, current
, regs
);
292 asmlinkage
void __kprobes
do_sparc64_fault(struct pt_regs
*regs
)
294 struct mm_struct
*mm
= current
->mm
;
295 struct vm_area_struct
*vma
;
296 unsigned int insn
= 0;
297 int si_code
, fault_code
;
298 unsigned long address
, mm_rss
;
300 fault_code
= get_thread_fault_code();
302 if (notify_page_fault(DIE_PAGE_FAULT
, "page_fault", regs
,
303 fault_code
, 0, SIGSEGV
) == NOTIFY_STOP
)
306 si_code
= SEGV_MAPERR
;
307 address
= current_thread_info()->fault_address
;
309 if ((fault_code
& FAULT_CODE_ITLB
) &&
310 (fault_code
& FAULT_CODE_DTLB
))
313 if (regs
->tstate
& TSTATE_PRIV
) {
314 unsigned long tpc
= regs
->tpc
;
316 /* Sanity check the PC. */
317 if ((tpc
>= KERNBASE
&& tpc
< (unsigned long) _etext
) ||
318 (tpc
>= MODULES_VADDR
&& tpc
< MODULES_END
)) {
319 /* Valid, no problems... */
321 bad_kernel_pc(regs
, address
);
327 * If we're in an interrupt or have no user
328 * context, we must not take the fault..
330 if (in_atomic() || !mm
)
333 if (test_thread_flag(TIF_32BIT
)) {
334 if (!(regs
->tstate
& TSTATE_PRIV
))
335 regs
->tpc
&= 0xffffffff;
336 address
&= 0xffffffff;
339 if (!down_read_trylock(&mm
->mmap_sem
)) {
340 if ((regs
->tstate
& TSTATE_PRIV
) &&
341 !search_exception_tables(regs
->tpc
)) {
342 insn
= get_fault_insn(regs
, insn
);
343 goto handle_kernel_fault
;
345 down_read(&mm
->mmap_sem
);
348 vma
= find_vma(mm
, address
);
352 /* Pure DTLB misses do not tell us whether the fault causing
353 * load/store/atomic was a write or not, it only says that there
354 * was no match. So in such a case we (carefully) read the
355 * instruction to try and figure this out. It's an optimization
356 * so it's ok if we can't do this.
358 * Special hack, window spill/fill knows the exact fault type.
361 (FAULT_CODE_DTLB
| FAULT_CODE_WRITE
| FAULT_CODE_WINFIXUP
)) == FAULT_CODE_DTLB
) &&
362 (vma
->vm_flags
& VM_WRITE
) != 0) {
363 insn
= get_fault_insn(regs
, 0);
366 /* All loads, stores and atomics have bits 30 and 31 both set
367 * in the instruction. Bit 21 is set in all stores, but we
368 * have to avoid prefetches which also have bit 21 set.
370 if ((insn
& 0xc0200000) == 0xc0200000 &&
371 (insn
& 0x01780000) != 0x01680000) {
372 /* Don't bother updating thread struct value,
373 * because update_mmu_cache only cares which tlb
374 * the access came from.
376 fault_code
|= FAULT_CODE_WRITE
;
381 if (vma
->vm_start
<= address
)
383 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
385 if (!(fault_code
& FAULT_CODE_WRITE
)) {
386 /* Non-faulting loads shouldn't expand stack. */
387 insn
= get_fault_insn(regs
, insn
);
388 if ((insn
& 0xc0800000) == 0xc0800000) {
392 asi
= (regs
->tstate
>> 24);
395 if ((asi
& 0xf2) == 0x82)
399 if (expand_stack(vma
, address
))
402 * Ok, we have a good vm_area for this memory access, so
406 si_code
= SEGV_ACCERR
;
408 /* If we took a ITLB miss on a non-executable page, catch
411 if ((fault_code
& FAULT_CODE_ITLB
) && !(vma
->vm_flags
& VM_EXEC
)) {
412 BUG_ON(address
!= regs
->tpc
);
413 BUG_ON(regs
->tstate
& TSTATE_PRIV
);
417 if (fault_code
& FAULT_CODE_WRITE
) {
418 if (!(vma
->vm_flags
& VM_WRITE
))
421 /* Spitfire has an icache which does not snoop
422 * processor stores. Later processors do...
424 if (tlb_type
== spitfire
&&
425 (vma
->vm_flags
& VM_EXEC
) != 0 &&
426 vma
->vm_file
!= NULL
)
427 set_thread_fault_code(fault_code
|
428 FAULT_CODE_BLKCOMMIT
);
430 /* Allow reads even for write-only mappings */
431 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
435 switch (handle_mm_fault(mm
, vma
, address
, (fault_code
& FAULT_CODE_WRITE
))) {
442 case VM_FAULT_SIGBUS
:
450 up_read(&mm
->mmap_sem
);
452 mm_rss
= get_mm_rss(mm
);
453 #ifdef CONFIG_HUGETLB_PAGE
454 mm_rss
-= (mm
->context
.huge_pte_count
* (HPAGE_SIZE
/ PAGE_SIZE
));
456 if (unlikely(mm_rss
>
457 mm
->context
.tsb_block
[MM_TSB_BASE
].tsb_rss_limit
))
458 tsb_grow(mm
, MM_TSB_BASE
, mm_rss
);
459 #ifdef CONFIG_HUGETLB_PAGE
460 mm_rss
= mm
->context
.huge_pte_count
;
461 if (unlikely(mm_rss
>
462 mm
->context
.tsb_block
[MM_TSB_HUGE
].tsb_rss_limit
))
463 tsb_grow(mm
, MM_TSB_HUGE
, mm_rss
);
468 * Something tried to access memory that isn't in our memory map..
469 * Fix it, but check if it's kernel or user first..
472 insn
= get_fault_insn(regs
, insn
);
473 up_read(&mm
->mmap_sem
);
476 do_kernel_fault(regs
, si_code
, fault_code
, insn
, address
);
480 * We ran out of memory, or some other thing happened to us that made
481 * us unable to handle the page fault gracefully.
484 insn
= get_fault_insn(regs
, insn
);
485 up_read(&mm
->mmap_sem
);
486 printk("VM: killing process %s\n", current
->comm
);
487 if (!(regs
->tstate
& TSTATE_PRIV
))
489 goto handle_kernel_fault
;
492 insn
= get_fault_insn(regs
, 0);
493 goto handle_kernel_fault
;
496 insn
= get_fault_insn(regs
, insn
);
497 up_read(&mm
->mmap_sem
);
500 * Send a sigbus, regardless of whether we were in kernel
503 do_fault_siginfo(BUS_ADRERR
, SIGBUS
, regs
, insn
, fault_code
);
505 /* Kernel mode? Handle exceptions or die */
506 if (regs
->tstate
& TSTATE_PRIV
)
507 goto handle_kernel_fault
;