2 * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
4 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
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/kdebug.h>
24 #include <asm/pgtable.h>
25 #include <asm/openprom.h>
26 #include <asm/oplib.h>
27 #include <asm/uaccess.h>
30 #include <asm/sections.h>
31 #include <asm/mmu_context.h>
34 static inline int notify_page_fault(struct pt_regs
*regs
)
38 /* kprobe_running() needs smp_processor_id() */
39 if (!user_mode(regs
)) {
41 if (kprobe_running() && kprobe_fault_handler(regs
, 0))
48 static inline int notify_page_fault(struct pt_regs
*regs
)
54 static void __kprobes
unhandled_fault(unsigned long address
,
55 struct task_struct
*tsk
,
58 if ((unsigned long) address
< PAGE_SIZE
) {
59 printk(KERN_ALERT
"Unable to handle kernel NULL "
60 "pointer dereference\n");
62 printk(KERN_ALERT
"Unable to handle kernel paging request "
63 "at virtual address %016lx\n", (unsigned long)address
);
65 printk(KERN_ALERT
"tsk->{mm,active_mm}->context = %016lx\n",
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
));
72 die_if_kernel("Oops", regs
);
75 static void bad_kernel_pc(struct pt_regs
*regs
, unsigned long vaddr
)
77 printk(KERN_CRIT
"OOPS: Bogus kernel PC [%016lx] in fault handler\n",
79 printk(KERN_CRIT
"OOPS: RPC [%016lx]\n", regs
->u_regs
[15]);
80 printk("OOPS: RPC <%pS>\n", (void *) regs
->u_regs
[15]);
81 printk(KERN_CRIT
"OOPS: Fault was to vaddr[%lx]\n", vaddr
);
83 unhandled_fault(regs
->tpc
, current
, regs
);
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
91 * us. vmtruncate likewise. This saves us having to get pte lock.
93 static unsigned int get_user_insn(unsigned long tpc
)
95 pgd_t
*pgdp
= pgd_offset(current
->mm
, tpc
);
101 unsigned long pstate
;
105 pudp
= pud_offset(pgdp
, tpc
);
108 pmdp
= pmd_offset(pudp
, tpc
);
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
);
118 if (!pte_present(pte
))
121 pa
= (pte_pfn(pte
) << PAGE_SHIFT
);
122 pa
+= (tpc
& ~PAGE_MASK
);
124 /* Use phys bypass so we don't pollute dtlb/dcache. */
125 __asm__
__volatile__("lduwa [%1] %2, %0"
127 : "r" (pa
), "i" (ASI_PHYS_USE_EC
));
131 __asm__
__volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate
));
136 extern unsigned long compute_effective_address(struct pt_regs
*, unsigned int, unsigned int);
138 static void do_fault_siginfo(int code
, int sig
, struct pt_regs
*regs
,
139 unsigned int insn
, int fault_code
)
146 if (fault_code
& FAULT_CODE_ITLB
)
147 info
.si_addr
= (void __user
*) regs
->tpc
;
149 info
.si_addr
= (void __user
*)
150 compute_effective_address(regs
, insn
, 0);
152 force_sig_info(sig
, &info
, current
);
155 extern int handle_ldf_stq(u32
, struct pt_regs
*);
156 extern int handle_ld_nf(u32
, struct pt_regs
*);
158 static unsigned int get_fault_insn(struct pt_regs
*regs
, unsigned int insn
)
161 if (!regs
->tpc
|| (regs
->tpc
& 0x3))
163 if (regs
->tstate
& TSTATE_PRIV
) {
164 insn
= *(unsigned int *) regs
->tpc
;
166 insn
= get_user_insn(regs
->tpc
);
172 static void do_kernel_fault(struct pt_regs
*regs
, int si_code
, int fault_code
,
173 unsigned int insn
, unsigned long address
)
175 unsigned char asi
= ASI_P
;
177 if ((!insn
) && (regs
->tstate
& TSTATE_PRIV
))
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
185 if (!(fault_code
& (FAULT_CODE_WRITE
|FAULT_CODE_ITLB
)) &&
186 (insn
& 0xc0800000) == 0xc0800000) {
188 asi
= (regs
->tstate
>> 24);
191 if ((asi
& 0xf2) == 0x82) {
192 if (insn
& 0x1000000) {
193 handle_ldf_stq(insn
, regs
);
195 /* This was a non-faulting load. Just clear the
196 * destination register(s) and continue with the next
199 handle_ld_nf(insn
, regs
);
205 /* Is this in ex_table? */
206 if (regs
->tstate
& TSTATE_PRIV
) {
207 const struct exception_table_entry
*entry
;
209 entry
= search_exception_tables(regs
->tpc
);
211 regs
->tpc
= entry
->fixup
;
212 regs
->tnpc
= regs
->tpc
+ 4;
216 /* The si_code was set to make clear whether
217 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
219 do_fault_siginfo(si_code
, SIGSEGV
, regs
, insn
, fault_code
);
224 unhandled_fault (address
, current
, regs
);
227 asmlinkage
void __kprobes
do_sparc64_fault(struct pt_regs
*regs
)
229 struct mm_struct
*mm
= current
->mm
;
230 struct vm_area_struct
*vma
;
231 unsigned int insn
= 0;
232 int si_code
, fault_code
, fault
;
233 unsigned long address
, mm_rss
;
235 fault_code
= get_thread_fault_code();
237 if (notify_page_fault(regs
))
240 si_code
= SEGV_MAPERR
;
241 address
= current_thread_info()->fault_address
;
243 if ((fault_code
& FAULT_CODE_ITLB
) &&
244 (fault_code
& FAULT_CODE_DTLB
))
247 if (regs
->tstate
& TSTATE_PRIV
) {
248 unsigned long tpc
= regs
->tpc
;
250 /* Sanity check the PC. */
251 if ((tpc
>= KERNBASE
&& tpc
< (unsigned long) __init_end
) ||
252 (tpc
>= MODULES_VADDR
&& tpc
< MODULES_END
)) {
253 /* Valid, no problems... */
255 bad_kernel_pc(regs
, address
);
261 * If we're in an interrupt or have no user
262 * context, we must not take the fault..
264 if (in_atomic() || !mm
)
267 if (test_thread_flag(TIF_32BIT
)) {
268 if (!(regs
->tstate
& TSTATE_PRIV
))
269 regs
->tpc
&= 0xffffffff;
270 address
&= 0xffffffff;
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
;
279 down_read(&mm
->mmap_sem
);
282 vma
= find_vma(mm
, address
);
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.
292 * Special hack, window spill/fill knows the exact fault type.
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);
300 /* 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.
304 if ((insn
& 0xc0200000) == 0xc0200000 &&
305 (insn
& 0x01780000) != 0x01680000) {
306 /* Don't bother updating thread struct value,
307 * because update_mmu_cache only cares which tlb
308 * the access came from.
310 fault_code
|= FAULT_CODE_WRITE
;
315 if (vma
->vm_start
<= address
)
317 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
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) {
326 asi
= (regs
->tstate
>> 24);
329 if ((asi
& 0xf2) == 0x82)
333 if (expand_stack(vma
, address
))
336 * Ok, we have a good vm_area for this memory access, so
340 si_code
= SEGV_ACCERR
;
342 /* If we took a ITLB miss on a non-executable page, catch
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
);
351 if (fault_code
& FAULT_CODE_WRITE
) {
352 if (!(vma
->vm_flags
& VM_WRITE
))
355 /* Spitfire has an icache which does not snoop
356 * processor stores. Later processors do...
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
);
364 /* Allow reads even for write-only mappings */
365 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
369 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
)
373 else if (fault
& VM_FAULT_SIGBUS
)
377 if (fault
& VM_FAULT_MAJOR
)
382 up_read(&mm
->mmap_sem
);
384 mm_rss
= get_mm_rss(mm
);
385 #ifdef CONFIG_HUGETLB_PAGE
386 mm_rss
-= (mm
->context
.huge_pte_count
* (HPAGE_SIZE
/ PAGE_SIZE
));
388 if (unlikely(mm_rss
>
389 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
;
393 if (unlikely(mm_rss
>
394 mm
->context
.tsb_block
[MM_TSB_HUGE
].tsb_rss_limit
))
395 tsb_grow(mm
, MM_TSB_HUGE
, mm_rss
);
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..
404 insn
= get_fault_insn(regs
, insn
);
405 up_read(&mm
->mmap_sem
);
408 do_kernel_fault(regs
, si_code
, fault_code
, insn
, address
);
412 * We ran out of memory, or some other thing happened to us that made
413 * us unable to handle the page fault gracefully.
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
))
420 do_group_exit(SIGKILL
);
421 goto handle_kernel_fault
;
424 insn
= get_fault_insn(regs
, 0);
425 goto handle_kernel_fault
;
428 insn
= get_fault_insn(regs
, insn
);
429 up_read(&mm
->mmap_sem
);
432 * Send a sigbus, regardless of whether we were in kernel
435 do_fault_siginfo(BUS_ADRERR
, SIGBUS
, regs
, insn
, fault_code
);
437 /* Kernel mode? Handle exceptions or die */
438 if (regs
->tstate
& TSTATE_PRIV
)
439 goto handle_kernel_fault
;