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 static inline int notify_page_fault(struct pt_regs
*regs
)
39 /* kprobe_running() needs smp_processor_id() */
40 if (!user_mode(regs
)) {
42 if (kprobe_running() && kprobe_fault_handler(regs
, 0))
49 static inline int notify_page_fault(struct pt_regs
*regs
)
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.
63 void set_brkpt(unsigned long addr
, unsigned char mask
, int flags
, int mode
)
65 unsigned long lsubits
;
67 __asm__
__volatile__("ldxa [%%g0] %1, %0"
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
);
74 __asm__
__volatile__("stxa %0, [%1] %2\n\t"
77 : "r" (addr
), "r" (mode
? VIRT_WATCHPOINT
: PHYS_WATCHPOINT
),
80 lsubits
|= ((unsigned long)mask
<< (mode
? 25 : 33));
82 lsubits
|= (mode
? LSU_CONTROL_VR
: LSU_CONTROL_PR
);
84 lsubits
|= (mode
? LSU_CONTROL_VW
: LSU_CONTROL_PW
);
85 __asm__
__volatile__("stxa %0, [%%g0] %1\n\t"
88 : "r" (lsubits
), "i" (ASI_LSU_CONTROL
)
92 static void __kprobes
unhandled_fault(unsigned long address
,
93 struct task_struct
*tsk
,
96 if ((unsigned long) address
< PAGE_SIZE
) {
97 printk(KERN_ALERT
"Unable to handle kernel NULL "
98 "pointer dereference\n");
100 printk(KERN_ALERT
"Unable to handle kernel paging request "
101 "at virtual address %016lx\n", (unsigned long)address
);
103 printk(KERN_ALERT
"tsk->{mm,active_mm}->context = %016lx\n",
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
));
110 die_if_kernel("Oops", regs
);
113 static void bad_kernel_pc(struct pt_regs
*regs
, unsigned long vaddr
)
115 printk(KERN_CRIT
"OOPS: Bogus kernel PC [%016lx] in fault handler\n",
117 printk(KERN_CRIT
"OOPS: RPC [%016lx]\n", regs
->u_regs
[15]);
118 print_symbol("RPC: <%s>\n", regs
->u_regs
[15]);
119 printk(KERN_CRIT
"OOPS: Fault was to vaddr[%lx]\n", vaddr
);
121 unhandled_fault(regs
->tpc
, current
, regs
);
125 * We now make sure that mmap_sem is held in all paths that call
126 * this. Additionally, to prevent kswapd from ripping ptes from
127 * under us, raise interrupts around the time that we look at the
128 * pte, kswapd will have to wait to get his smp ipi response from
129 * us. vmtruncate likewise. This saves us having to get pte lock.
131 static unsigned int get_user_insn(unsigned long tpc
)
133 pgd_t
*pgdp
= pgd_offset(current
->mm
, tpc
);
139 unsigned long pstate
;
143 pudp
= pud_offset(pgdp
, tpc
);
146 pmdp
= pmd_offset(pudp
, tpc
);
150 /* This disables preemption for us as well. */
151 __asm__
__volatile__("rdpr %%pstate, %0" : "=r" (pstate
));
152 __asm__
__volatile__("wrpr %0, %1, %%pstate"
153 : : "r" (pstate
), "i" (PSTATE_IE
));
154 ptep
= pte_offset_map(pmdp
, tpc
);
156 if (!pte_present(pte
))
159 pa
= (pte_pfn(pte
) << PAGE_SHIFT
);
160 pa
+= (tpc
& ~PAGE_MASK
);
162 /* Use phys bypass so we don't pollute dtlb/dcache. */
163 __asm__
__volatile__("lduwa [%1] %2, %0"
165 : "r" (pa
), "i" (ASI_PHYS_USE_EC
));
169 __asm__
__volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate
));
174 extern unsigned long compute_effective_address(struct pt_regs
*, unsigned int, unsigned int);
176 static void do_fault_siginfo(int code
, int sig
, struct pt_regs
*regs
,
177 unsigned int insn
, int fault_code
)
184 if (fault_code
& FAULT_CODE_ITLB
)
185 info
.si_addr
= (void __user
*) regs
->tpc
;
187 info
.si_addr
= (void __user
*)
188 compute_effective_address(regs
, insn
, 0);
190 force_sig_info(sig
, &info
, current
);
193 extern int handle_ldf_stq(u32
, struct pt_regs
*);
194 extern int handle_ld_nf(u32
, struct pt_regs
*);
196 static unsigned int get_fault_insn(struct pt_regs
*regs
, unsigned int insn
)
199 if (!regs
->tpc
|| (regs
->tpc
& 0x3))
201 if (regs
->tstate
& TSTATE_PRIV
) {
202 insn
= *(unsigned int *) regs
->tpc
;
204 insn
= get_user_insn(regs
->tpc
);
210 static void do_kernel_fault(struct pt_regs
*regs
, int si_code
, int fault_code
,
211 unsigned int insn
, unsigned long address
)
213 unsigned char asi
= ASI_P
;
215 if ((!insn
) && (regs
->tstate
& TSTATE_PRIV
))
218 /* If user insn could be read (thus insn is zero), that
219 * is fine. We will just gun down the process with a signal
223 if (!(fault_code
& (FAULT_CODE_WRITE
|FAULT_CODE_ITLB
)) &&
224 (insn
& 0xc0800000) == 0xc0800000) {
226 asi
= (regs
->tstate
>> 24);
229 if ((asi
& 0xf2) == 0x82) {
230 if (insn
& 0x1000000) {
231 handle_ldf_stq(insn
, regs
);
233 /* This was a non-faulting load. Just clear the
234 * destination register(s) and continue with the next
237 handle_ld_nf(insn
, regs
);
243 /* Is this in ex_table? */
244 if (regs
->tstate
& TSTATE_PRIV
) {
245 const struct exception_table_entry
*entry
;
247 entry
= search_exception_tables(regs
->tpc
);
249 regs
->tpc
= entry
->fixup
;
250 regs
->tnpc
= regs
->tpc
+ 4;
254 /* The si_code was set to make clear whether
255 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
257 do_fault_siginfo(si_code
, SIGSEGV
, regs
, insn
, fault_code
);
262 unhandled_fault (address
, current
, regs
);
265 asmlinkage
void __kprobes
do_sparc64_fault(struct pt_regs
*regs
)
267 struct mm_struct
*mm
= current
->mm
;
268 struct vm_area_struct
*vma
;
269 unsigned int insn
= 0;
270 int si_code
, fault_code
, fault
;
271 unsigned long address
, mm_rss
;
273 fault_code
= get_thread_fault_code();
275 if (notify_page_fault(regs
))
278 si_code
= SEGV_MAPERR
;
279 address
= current_thread_info()->fault_address
;
281 if ((fault_code
& FAULT_CODE_ITLB
) &&
282 (fault_code
& FAULT_CODE_DTLB
))
285 if (regs
->tstate
& TSTATE_PRIV
) {
286 unsigned long tpc
= regs
->tpc
;
288 /* Sanity check the PC. */
289 if ((tpc
>= KERNBASE
&& tpc
< (unsigned long) __init_end
) ||
290 (tpc
>= MODULES_VADDR
&& tpc
< MODULES_END
)) {
291 /* Valid, no problems... */
293 bad_kernel_pc(regs
, address
);
299 * If we're in an interrupt or have no user
300 * context, we must not take the fault..
302 if (in_atomic() || !mm
)
305 if (test_thread_flag(TIF_32BIT
)) {
306 if (!(regs
->tstate
& TSTATE_PRIV
))
307 regs
->tpc
&= 0xffffffff;
308 address
&= 0xffffffff;
311 if (!down_read_trylock(&mm
->mmap_sem
)) {
312 if ((regs
->tstate
& TSTATE_PRIV
) &&
313 !search_exception_tables(regs
->tpc
)) {
314 insn
= get_fault_insn(regs
, insn
);
315 goto handle_kernel_fault
;
317 down_read(&mm
->mmap_sem
);
320 vma
= find_vma(mm
, address
);
324 /* Pure DTLB misses do not tell us whether the fault causing
325 * load/store/atomic was a write or not, it only says that there
326 * was no match. So in such a case we (carefully) read the
327 * instruction to try and figure this out. It's an optimization
328 * so it's ok if we can't do this.
330 * Special hack, window spill/fill knows the exact fault type.
333 (FAULT_CODE_DTLB
| FAULT_CODE_WRITE
| FAULT_CODE_WINFIXUP
)) == FAULT_CODE_DTLB
) &&
334 (vma
->vm_flags
& VM_WRITE
) != 0) {
335 insn
= get_fault_insn(regs
, 0);
338 /* All loads, stores and atomics have bits 30 and 31 both set
339 * in the instruction. Bit 21 is set in all stores, but we
340 * have to avoid prefetches which also have bit 21 set.
342 if ((insn
& 0xc0200000) == 0xc0200000 &&
343 (insn
& 0x01780000) != 0x01680000) {
344 /* Don't bother updating thread struct value,
345 * because update_mmu_cache only cares which tlb
346 * the access came from.
348 fault_code
|= FAULT_CODE_WRITE
;
353 if (vma
->vm_start
<= address
)
355 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
357 if (!(fault_code
& FAULT_CODE_WRITE
)) {
358 /* Non-faulting loads shouldn't expand stack. */
359 insn
= get_fault_insn(regs
, insn
);
360 if ((insn
& 0xc0800000) == 0xc0800000) {
364 asi
= (regs
->tstate
>> 24);
367 if ((asi
& 0xf2) == 0x82)
371 if (expand_stack(vma
, address
))
374 * Ok, we have a good vm_area for this memory access, so
378 si_code
= SEGV_ACCERR
;
380 /* If we took a ITLB miss on a non-executable page, catch
383 if ((fault_code
& FAULT_CODE_ITLB
) && !(vma
->vm_flags
& VM_EXEC
)) {
384 BUG_ON(address
!= regs
->tpc
);
385 BUG_ON(regs
->tstate
& TSTATE_PRIV
);
389 if (fault_code
& FAULT_CODE_WRITE
) {
390 if (!(vma
->vm_flags
& VM_WRITE
))
393 /* Spitfire has an icache which does not snoop
394 * processor stores. Later processors do...
396 if (tlb_type
== spitfire
&&
397 (vma
->vm_flags
& VM_EXEC
) != 0 &&
398 vma
->vm_file
!= NULL
)
399 set_thread_fault_code(fault_code
|
400 FAULT_CODE_BLKCOMMIT
);
402 /* Allow reads even for write-only mappings */
403 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
407 fault
= handle_mm_fault(mm
, vma
, address
, (fault_code
& FAULT_CODE_WRITE
));
408 if (unlikely(fault
& VM_FAULT_ERROR
)) {
409 if (fault
& VM_FAULT_OOM
)
411 else if (fault
& VM_FAULT_SIGBUS
)
415 if (fault
& VM_FAULT_MAJOR
)
420 up_read(&mm
->mmap_sem
);
422 mm_rss
= get_mm_rss(mm
);
423 #ifdef CONFIG_HUGETLB_PAGE
424 mm_rss
-= (mm
->context
.huge_pte_count
* (HPAGE_SIZE
/ PAGE_SIZE
));
426 if (unlikely(mm_rss
>
427 mm
->context
.tsb_block
[MM_TSB_BASE
].tsb_rss_limit
))
428 tsb_grow(mm
, MM_TSB_BASE
, mm_rss
);
429 #ifdef CONFIG_HUGETLB_PAGE
430 mm_rss
= mm
->context
.huge_pte_count
;
431 if (unlikely(mm_rss
>
432 mm
->context
.tsb_block
[MM_TSB_HUGE
].tsb_rss_limit
))
433 tsb_grow(mm
, MM_TSB_HUGE
, mm_rss
);
438 * Something tried to access memory that isn't in our memory map..
439 * Fix it, but check if it's kernel or user first..
442 insn
= get_fault_insn(regs
, insn
);
443 up_read(&mm
->mmap_sem
);
446 do_kernel_fault(regs
, si_code
, fault_code
, insn
, address
);
450 * We ran out of memory, or some other thing happened to us that made
451 * us unable to handle the page fault gracefully.
454 insn
= get_fault_insn(regs
, insn
);
455 up_read(&mm
->mmap_sem
);
456 printk("VM: killing process %s\n", current
->comm
);
457 if (!(regs
->tstate
& TSTATE_PRIV
))
458 do_group_exit(SIGKILL
);
459 goto handle_kernel_fault
;
462 insn
= get_fault_insn(regs
, 0);
463 goto handle_kernel_fault
;
466 insn
= get_fault_insn(regs
, insn
);
467 up_read(&mm
->mmap_sem
);
470 * Send a sigbus, regardless of whether we were in kernel
473 do_fault_siginfo(BUS_ADRERR
, SIGBUS
, regs
, insn
, fault_code
);
475 /* Kernel mode? Handle exceptions or die */
476 if (regs
->tstate
& TSTATE_PRIV
)
477 goto handle_kernel_fault
;