2 * fault.c: Page fault handlers for the Sparc.
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
11 #include <linux/string.h>
12 #include <linux/types.h>
13 #include <linux/sched.h>
14 #include <linux/ptrace.h>
15 #include <linux/mman.h>
16 #include <linux/threads.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
20 #include <linux/smp.h>
21 #include <linux/perf_event.h>
22 #include <linux/interrupt.h>
23 #include <linux/kdebug.h>
26 #include <asm/pgtable.h>
27 #include <asm/openprom.h>
28 #include <asm/oplib.h>
30 #include <asm/traps.h>
31 #include <asm/uaccess.h>
33 extern int prom_node_root
;
35 int show_unhandled_signals
= 1;
37 /* At boot time we determine these two values necessary for setting
38 * up the segment maps and page table entries (pte's).
41 int num_segmaps
, num_contexts
;
44 /* various Virtual Address Cache parameters we find at boot time... */
46 int vac_size
, vac_linesize
, vac_do_hw_vac_flushes
;
47 int vac_entries_per_context
, vac_entries_per_segment
;
48 int vac_entries_per_page
;
50 /* Return how much physical memory we have. */
51 unsigned long probe_memory(void)
53 unsigned long total
= 0;
56 for (i
= 0; sp_banks
[i
].num_bytes
; i
++)
57 total
+= sp_banks
[i
].num_bytes
;
62 /* Whee, a level 15 NMI interrupt memory error. Let's have fun... */
63 asmlinkage
void sparc_lvl15_nmi(struct pt_regs
*regs
, unsigned long serr
,
64 unsigned long svaddr
, unsigned long aerr
,
67 printk("FAULT: NMI received\n");
68 printk("SREGS: Synchronous Error %08lx\n", serr
);
69 printk(" Synchronous Vaddr %08lx\n", svaddr
);
70 printk(" Asynchronous Error %08lx\n", aerr
);
71 printk(" Asynchronous Vaddr %08lx\n", avaddr
);
72 printk("REGISTER DUMP:\n");
77 static void unhandled_fault(unsigned long, struct task_struct
*,
78 struct pt_regs
*) __attribute__ ((noreturn
));
80 static void unhandled_fault(unsigned long address
, struct task_struct
*tsk
,
83 if((unsigned long) address
< PAGE_SIZE
) {
85 "Unable to handle kernel NULL pointer dereference\n");
87 printk(KERN_ALERT
"Unable to handle kernel paging request "
88 "at virtual address %08lx\n", address
);
90 printk(KERN_ALERT
"tsk->{mm,active_mm}->context = %08lx\n",
91 (tsk
->mm
? tsk
->mm
->context
: tsk
->active_mm
->context
));
92 printk(KERN_ALERT
"tsk->{mm,active_mm}->pgd = %08lx\n",
93 (tsk
->mm
? (unsigned long) tsk
->mm
->pgd
:
94 (unsigned long) tsk
->active_mm
->pgd
));
95 die_if_kernel("Oops", regs
);
98 asmlinkage
int lookup_fault(unsigned long pc
, unsigned long ret_pc
,
99 unsigned long address
)
106 i
= search_extables_range(ret_pc
, &g2
);
109 /* load & store will be handled by fixup */
113 /* store will be handled by fixup, load will bump out */
114 /* for _to_ macros */
115 insn
= *((unsigned int *) pc
);
116 if ((insn
>> 21) & 1)
121 /* load will be handled by fixup, store will bump out */
122 /* for _from_ macros */
123 insn
= *((unsigned int *) pc
);
124 if (!((insn
>> 21) & 1) || ((insn
>>19)&0x3f) == 15)
132 memset(®s
, 0, sizeof (regs
));
135 __asm__
__volatile__(
139 "nop\n" : "=r" (regs
.psr
));
140 unhandled_fault(address
, current
, ®s
);
147 show_signal_msg(struct pt_regs
*regs
, int sig
, int code
,
148 unsigned long address
, struct task_struct
*tsk
)
150 if (!unhandled_signal(tsk
, sig
))
153 if (!printk_ratelimit())
156 printk("%s%s[%d]: segfault at %lx ip %p (rpc %p) sp %p error %x",
157 task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
,
158 tsk
->comm
, task_pid_nr(tsk
), address
,
159 (void *)regs
->pc
, (void *)regs
->u_regs
[UREG_I7
],
160 (void *)regs
->u_regs
[UREG_FP
], code
);
162 print_vma_addr(KERN_CONT
" in ", regs
->pc
);
164 printk(KERN_CONT
"\n");
167 static void __do_fault_siginfo(int code
, int sig
, struct pt_regs
*regs
,
175 info
.si_addr
= (void __user
*) addr
;
178 if (unlikely(show_unhandled_signals
))
179 show_signal_msg(regs
, sig
, info
.si_code
,
182 force_sig_info (sig
, &info
, current
);
185 extern unsigned long safe_compute_effective_address(struct pt_regs
*,
188 static unsigned long compute_si_addr(struct pt_regs
*regs
, int text_fault
)
195 if (regs
->psr
& PSR_PS
) {
196 insn
= *(unsigned int *) regs
->pc
;
198 __get_user(insn
, (unsigned int *) regs
->pc
);
201 return safe_compute_effective_address(regs
, insn
);
204 static noinline
void do_fault_siginfo(int code
, int sig
, struct pt_regs
*regs
,
207 unsigned long addr
= compute_si_addr(regs
, text_fault
);
209 __do_fault_siginfo(code
, sig
, regs
, addr
);
212 asmlinkage
void do_sparc_fault(struct pt_regs
*regs
, int text_fault
, int write
,
213 unsigned long address
)
215 struct vm_area_struct
*vma
;
216 struct task_struct
*tsk
= current
;
217 struct mm_struct
*mm
= tsk
->mm
;
220 int from_user
= !(regs
->psr
& PSR_PS
);
222 unsigned int flags
= (FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
|
223 (write
? FAULT_FLAG_WRITE
: 0));
229 * We fault-in kernel-space virtual memory on-demand. The
230 * 'reference' page table is init_mm.pgd.
232 * NOTE! We MUST NOT take any locks for this case. We may
233 * be in an interrupt or a critical region, and should
234 * only copy the information from the master page table,
238 if (!ARCH_SUN4C
&& address
>= TASK_SIZE
)
242 * If we're in an interrupt or have no user
243 * context, we must not take the fault..
245 if (in_atomic() || !mm
)
248 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, regs
, address
);
251 down_read(&mm
->mmap_sem
);
254 * The kernel referencing a bad kernel pointer can lock up
255 * a sun4c machine completely, so we must attempt recovery.
257 if(!from_user
&& address
>= PAGE_OFFSET
)
260 vma
= find_vma(mm
, address
);
263 if(vma
->vm_start
<= address
)
265 if(!(vma
->vm_flags
& VM_GROWSDOWN
))
267 if(expand_stack(vma
, address
))
270 * Ok, we have a good vm_area for this memory access, so
276 if(!(vma
->vm_flags
& VM_WRITE
))
279 /* Allow reads even for write-only mappings */
280 if(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
285 * If for any reason at all we couldn't handle the fault,
286 * make sure we exit gracefully rather than endlessly redo
289 fault
= handle_mm_fault(mm
, vma
, address
, flags
);
291 if ((fault
& VM_FAULT_RETRY
) && fatal_signal_pending(current
))
294 if (unlikely(fault
& VM_FAULT_ERROR
)) {
295 if (fault
& VM_FAULT_OOM
)
297 else if (fault
& VM_FAULT_SIGBUS
)
302 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
303 if (fault
& VM_FAULT_MAJOR
) {
305 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
,
309 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
,
312 if (fault
& VM_FAULT_RETRY
) {
313 flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
315 /* No need to up_read(&mm->mmap_sem) as we would
316 * have already released it in __lock_page_or_retry
324 up_read(&mm
->mmap_sem
);
328 * Something tried to access memory that isn't in our memory map..
329 * Fix it, but check if it's kernel or user first..
332 up_read(&mm
->mmap_sem
);
334 bad_area_nosemaphore
:
335 /* User mode accesses just cause a SIGSEGV */
337 do_fault_siginfo(code
, SIGSEGV
, regs
, text_fault
);
341 /* Is this in ex_table? */
343 g2
= regs
->u_regs
[UREG_G2
];
345 fixup
= search_extables_range(regs
->pc
, &g2
);
346 if (fixup
> 10) { /* Values below are reserved for other things */
347 extern const unsigned __memset_start
[];
348 extern const unsigned __memset_end
[];
349 extern const unsigned __csum_partial_copy_start
[];
350 extern const unsigned __csum_partial_copy_end
[];
352 #ifdef DEBUG_EXCEPTIONS
353 printk("Exception: PC<%08lx> faddr<%08lx>\n", regs
->pc
, address
);
354 printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
355 regs
->pc
, fixup
, g2
);
357 if ((regs
->pc
>= (unsigned long)__memset_start
&&
358 regs
->pc
< (unsigned long)__memset_end
) ||
359 (regs
->pc
>= (unsigned long)__csum_partial_copy_start
&&
360 regs
->pc
< (unsigned long)__csum_partial_copy_end
)) {
361 regs
->u_regs
[UREG_I4
] = address
;
362 regs
->u_regs
[UREG_I5
] = regs
->pc
;
364 regs
->u_regs
[UREG_G2
] = g2
;
366 regs
->npc
= regs
->pc
+ 4;
371 unhandled_fault (address
, tsk
, regs
);
375 * We ran out of memory, or some other thing happened to us that made
376 * us unable to handle the page fault gracefully.
379 up_read(&mm
->mmap_sem
);
381 pagefault_out_of_memory();
387 up_read(&mm
->mmap_sem
);
388 do_fault_siginfo(BUS_ADRERR
, SIGBUS
, regs
, text_fault
);
395 * Synchronize this task's top level page-table
396 * with the 'reference' page table.
398 int offset
= pgd_index(address
);
402 pgd
= tsk
->active_mm
->pgd
+ offset
;
403 pgd_k
= init_mm
.pgd
+ offset
;
405 if (!pgd_present(*pgd
)) {
406 if (!pgd_present(*pgd_k
))
407 goto bad_area_nosemaphore
;
408 pgd_val(*pgd
) = pgd_val(*pgd_k
);
412 pmd
= pmd_offset(pgd
, address
);
413 pmd_k
= pmd_offset(pgd_k
, address
);
415 if (pmd_present(*pmd
) || !pmd_present(*pmd_k
))
416 goto bad_area_nosemaphore
;
422 /* This always deals with user addresses. */
423 static void force_user_fault(unsigned long address
, int write
)
425 struct vm_area_struct
*vma
;
426 struct task_struct
*tsk
= current
;
427 struct mm_struct
*mm
= tsk
->mm
;
432 down_read(&mm
->mmap_sem
);
433 vma
= find_vma(mm
, address
);
436 if(vma
->vm_start
<= address
)
438 if(!(vma
->vm_flags
& VM_GROWSDOWN
))
440 if(expand_stack(vma
, address
))
445 if(!(vma
->vm_flags
& VM_WRITE
))
448 if(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
451 switch (handle_mm_fault(mm
, vma
, address
, write
? FAULT_FLAG_WRITE
: 0)) {
452 case VM_FAULT_SIGBUS
:
456 up_read(&mm
->mmap_sem
);
459 up_read(&mm
->mmap_sem
);
460 __do_fault_siginfo(code
, SIGSEGV
, tsk
->thread
.kregs
, address
);
464 up_read(&mm
->mmap_sem
);
465 __do_fault_siginfo(BUS_ADRERR
, SIGBUS
, tsk
->thread
.kregs
, address
);
468 static void check_stack_aligned(unsigned long sp
)
471 force_sig(SIGILL
, current
);
474 void window_overflow_fault(void)
478 sp
= current_thread_info()->rwbuf_stkptrs
[0];
479 if(((sp
+ 0x38) & PAGE_MASK
) != (sp
& PAGE_MASK
))
480 force_user_fault(sp
+ 0x38, 1);
481 force_user_fault(sp
, 1);
483 check_stack_aligned(sp
);
486 void window_underflow_fault(unsigned long sp
)
488 if(((sp
+ 0x38) & PAGE_MASK
) != (sp
& PAGE_MASK
))
489 force_user_fault(sp
+ 0x38, 0);
490 force_user_fault(sp
, 0);
492 check_stack_aligned(sp
);
495 void window_ret_fault(struct pt_regs
*regs
)
499 sp
= regs
->u_regs
[UREG_FP
];
500 if(((sp
+ 0x38) & PAGE_MASK
) != (sp
& PAGE_MASK
))
501 force_user_fault(sp
+ 0x38, 0);
502 force_user_fault(sp
, 0);
504 check_stack_aligned(sp
);