5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7 * Derived from "arch/i386/mm/fault.c"
8 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
10 * Modified by Cort Dougan and Paul Mackerras.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 #include <linux/config.h>
19 #include <linux/signal.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/string.h>
24 #include <linux/types.h>
25 #include <linux/ptrace.h>
26 #include <linux/mman.h>
28 #include <linux/interrupt.h>
31 #include <asm/pgtable.h>
33 #include <asm/mmu_context.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
37 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
38 extern void (*debugger
)(struct pt_regs
*);
39 extern void (*debugger_fault_handler
)(struct pt_regs
*);
40 extern int (*debugger_dabr_match
)(struct pt_regs
*);
41 int debugger_kernel_faults
= 1;
44 unsigned long htab_reloads
= 0; /* updated by head.S:hash_page() */
45 unsigned long htab_evicts
= 0; /* updated by head.S:hash_page() */
46 unsigned long pte_misses
= 0; /* updated by do_page_fault() */
47 unsigned long pte_errors
= 0; /* updated by do_page_fault() */
48 unsigned int probingmem
= 0;
50 extern void die_if_kernel(char *, struct pt_regs
*, long);
51 void bad_page_fault(struct pt_regs
*, unsigned long);
52 void do_page_fault(struct pt_regs
*, unsigned long, unsigned long);
55 * For 600- and 800-family processors, the error_code parameter is DSISR
56 * for a data fault, SRR1 for an instruction fault. For 400-family processors
57 * the error_code parameter is ESR for a data fault, 0 for an instruction
60 void do_page_fault(struct pt_regs
*regs
, unsigned long address
,
61 unsigned long error_code
)
63 struct vm_area_struct
* vma
;
64 struct mm_struct
*mm
= current
->mm
;
66 int code
= SEGV_MAPERR
;
67 #if defined(CONFIG_4xx)
68 int is_write
= error_code
& ESR_DST
;
73 * Fortunately the bit assignments in SRR1 for an instruction
74 * fault and DSISR for a data fault are mostly the same for the
75 * bits we are interested in. But there are some bits which
76 * indicate errors in DSISR but can validly be set in SRR1.
78 if (regs
->trap
== 0x400)
79 error_code
&= 0x48200000;
81 is_write
= error_code
& 0x02000000;
82 #endif /* CONFIG_4xx */
84 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
85 if (debugger_fault_handler
&& regs
->trap
== 0x300) {
86 debugger_fault_handler(regs
);
89 #if !defined(CONFIG_4xx)
90 if (error_code
& 0x00400000) {
92 if (debugger_dabr_match(regs
))
95 #endif /* !CONFIG_4xx */
96 #endif /* CONFIG_XMON || CONFIG_KGDB */
98 if (in_interrupt() || mm
== NULL
) {
99 bad_page_fault(regs
, address
);
103 vma
= find_vma(mm
, address
);
106 if (vma
->vm_start
<= address
)
108 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
110 if (expand_stack(vma
, address
))
115 #if defined(CONFIG_6xx)
116 if (error_code
& 0x95700000)
117 /* an error such as lwarx to I/O controller space,
118 address matching DABR, eciwx, etc. */
120 #endif /* CONFIG_6xx */
121 #if defined(CONFIG_8xx)
122 /* The MPC8xx seems to always set 0x80000000, which is
123 * "undefined". Of those that can be set, this is the only
124 * one which seems bad.
126 if (error_code
& 0x10000000)
127 /* Guarded storage error. */
129 #endif /* CONFIG_8xx */
133 if (!(vma
->vm_flags
& VM_WRITE
))
137 /* protection fault */
138 if (error_code
& 0x08000000)
140 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
145 * If for any reason at all we couldn't handle the fault,
146 * make sure we exit gracefully rather than endlessly redo
149 switch (handle_mm_fault(mm
, vma
, address
, is_write
)) {
164 * keep track of tlb+htab misses that are good addrs but
165 * just need pte's created via handle_mm_fault()
175 /* User mode accesses cause a SIGSEGV */
176 if (user_mode(regs
)) {
177 info
.si_signo
= SIGSEGV
;
180 info
.si_addr
= (void *) address
;
181 force_sig_info(SIGSEGV
, &info
, current
);
185 bad_page_fault(regs
, address
);
189 * We ran out of memory, or some other thing happened to us that made
190 * us unable to handle the page fault gracefully.
194 printk("VM: killing process %s\n", current
->comm
);
197 bad_page_fault(regs
, address
);
202 info
.si_signo
= SIGBUS
;
204 info
.si_code
= BUS_ADRERR
;
205 info
.si_addr
= (void *)address
;
206 force_sig_info (SIGBUS
, &info
, current
);
207 if (!user_mode(regs
))
208 bad_page_fault(regs
, address
);
212 * bad_page_fault is called when we have a bad access from the kernel.
213 * It is called from do_page_fault above and from some of the procedures
217 bad_page_fault(struct pt_regs
*regs
, unsigned long address
)
221 /* Are we prepared to handle this fault? */
222 if ((fixup
= search_exception_table(regs
->nip
)) != 0) {
227 /* kernel has accessed a bad area */
229 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
230 if (debugger_kernel_faults
)
233 print_backtrace( (unsigned long *)regs
->gpr
[1] );
234 panic("kernel access of bad area pc %lx lr %lx address %lX tsk %s/%d",
235 regs
->nip
,regs
->link
,address
,current
->comm
,current
->pid
);
240 /* The pgtable.h claims some functions generically exist, but I
241 * can't find them......
243 pte_t
*va_to_pte(unsigned long address
)
248 struct mm_struct
*mm
;
250 if (address
< TASK_SIZE
)
255 dir
= pgd_offset(mm
, address
& PAGE_MASK
);
257 pmd
= pmd_offset(dir
, address
& PAGE_MASK
);
258 if (pmd
&& pmd_present(*pmd
)) {
259 pte
= pte_offset(pmd
, address
& PAGE_MASK
);
260 if (pte
&& pte_present(*pte
)) {
274 unsigned long va_to_phys(unsigned long address
)
278 pte
= va_to_pte(address
);
280 return(((unsigned long)(pte_val(*pte
)) & PAGE_MASK
) | (address
& ~(PAGE_MASK
-1)));
285 print_8xx_pte(struct mm_struct
*mm
, unsigned long addr
)
291 printk(" pte @ 0x%8lx: ", addr
);
292 pgd
= pgd_offset(mm
, addr
& PAGE_MASK
);
294 pmd
= pmd_offset(pgd
, addr
& PAGE_MASK
);
295 if (pmd
&& pmd_present(*pmd
)) {
296 pte
= pte_offset(pmd
, addr
& PAGE_MASK
);
298 printk(" (0x%08lx)->(0x%08lx)->0x%08lx\n",
299 (long)pgd
, (long)pte
, (long)pte_val(*pte
));
300 #define pp ((long)pte_val(*pte))
301 printk(" RPN: %05lx PP: %lx SPS: %lx SH: %lx "
305 (pp
>>3)&1, /* small */
306 (pp
>>2)&1, /* shared */
307 (pp
>>1)&1, /* cache inhibit */
326 get_8xx_pte(struct mm_struct
*mm
, unsigned long addr
)
333 pgd
= pgd_offset(mm
, addr
& PAGE_MASK
);
335 pmd
= pmd_offset(pgd
, addr
& PAGE_MASK
);
336 if (pmd
&& pmd_present(*pmd
)) {
337 pte
= pte_offset(pmd
, addr
& PAGE_MASK
);
339 retval
= (int)pte_val(*pte
);
345 #endif /* CONFIG_8xx */
349 * Misc debugging functions. Please leave them here. -- Cort
351 void print_pte(struct _PTE p
)
354 "%08x %08x vsid: %06x h: %01x api: %02x rpn: %05x rcwimg: %d%d%d%d%d%d pp: %02x\n",
355 *((unsigned long *)(&p
)), *((long *)&p
+1),
356 p
.vsid
, p
.h
, p
.api
, p
.rpn
,
357 p
.r
,p
.c
,p
.w
,p
.i
,p
.m
,p
.g
,p
.pp
);
361 * Search the hw hash table for a mapping to the given physical
364 unsigned long htab_phys_to_va(unsigned long address
)
366 extern PTE
*Hash
, *Hash_end
;
369 for ( ptr
= Hash
; ptr
< Hash_end
; ptr
++ )
371 if ( ptr
->rpn
== (address
>>12) )
372 printk("phys %08lX -> va ???\n",