Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / arch / ppc / mm / fault.c
blobb6da2cdfcabd4b9416572a74fabb33ee303b350c
1 /*
2 * arch/ppc/mm/fault.c
4 * PowerPC version
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>
27 #include <linux/mm.h>
28 #include <linux/interrupt.h>
30 #include <asm/page.h>
31 #include <asm/pgtable.h>
32 #include <asm/mmu.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;
42 #endif
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
58 * fault.
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;
65 siginfo_t info;
66 int code = SEGV_MAPERR;
67 #if defined(CONFIG_4xx)
68 int is_write = error_code & ESR_DST;
69 #else
70 int is_write = 0;
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;
80 else
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);
87 return;
89 #if !defined(CONFIG_4xx)
90 if (error_code & 0x00400000) {
91 /* DABR match */
92 if (debugger_dabr_match(regs))
93 return;
95 #endif /* !CONFIG_4xx */
96 #endif /* CONFIG_XMON || CONFIG_KGDB */
98 if (in_interrupt() || mm == NULL) {
99 bad_page_fault(regs, address);
100 return;
102 down(&mm->mmap_sem);
103 vma = find_vma(mm, address);
104 if (!vma)
105 goto bad_area;
106 if (vma->vm_start <= address)
107 goto good_area;
108 if (!(vma->vm_flags & VM_GROWSDOWN))
109 goto bad_area;
110 if (expand_stack(vma, address))
111 goto bad_area;
113 good_area:
114 code = SEGV_ACCERR;
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. */
119 goto bad_area;
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. */
128 goto bad_area;
129 #endif /* CONFIG_8xx */
131 /* a write */
132 if (is_write) {
133 if (!(vma->vm_flags & VM_WRITE))
134 goto bad_area;
135 /* a read */
136 } else {
137 /* protection fault */
138 if (error_code & 0x08000000)
139 goto bad_area;
140 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
141 goto bad_area;
145 * If for any reason at all we couldn't handle the fault,
146 * make sure we exit gracefully rather than endlessly redo
147 * the fault.
149 switch (handle_mm_fault(mm, vma, address, is_write)) {
150 case 1:
151 current->min_flt++;
152 break;
153 case 2:
154 current->maj_flt++;
155 break;
156 case 0:
157 goto do_sigbus;
158 default:
159 goto out_of_memory;
162 up(&mm->mmap_sem);
164 * keep track of tlb+htab misses that are good addrs but
165 * just need pte's created via handle_mm_fault()
166 * -- Cort
168 pte_misses++;
169 return;
171 bad_area:
172 up(&mm->mmap_sem);
173 pte_errors++;
175 /* User mode accesses cause a SIGSEGV */
176 if (user_mode(regs)) {
177 info.si_signo = SIGSEGV;
178 info.si_errno = 0;
179 info.si_code = code;
180 info.si_addr = (void *) address;
181 force_sig_info(SIGSEGV, &info, current);
182 return;
185 bad_page_fault(regs, address);
186 return;
189 * We ran out of memory, or some other thing happened to us that made
190 * us unable to handle the page fault gracefully.
192 out_of_memory:
193 up(&mm->mmap_sem);
194 printk("VM: killing process %s\n", current->comm);
195 if (user_mode(regs))
196 do_exit(SIGKILL);
197 bad_page_fault(regs, address);
198 return;
200 do_sigbus:
201 up(&mm->mmap_sem);
202 info.si_signo = SIGBUS;
203 info.si_errno = 0;
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
214 * in traps.c.
216 void
217 bad_page_fault(struct pt_regs *regs, unsigned long address)
219 unsigned long fixup;
221 /* Are we prepared to handle this fault? */
222 if ((fixup = search_exception_table(regs->nip)) != 0) {
223 regs->nip = fixup;
224 return;
227 /* kernel has accessed a bad area */
228 show_regs(regs);
229 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
230 if (debugger_kernel_faults)
231 debugger(regs);
232 #endif
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);
238 #ifdef CONFIG_8xx
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)
245 pgd_t *dir;
246 pmd_t *pmd;
247 pte_t *pte;
248 struct mm_struct *mm;
250 if (address < TASK_SIZE)
251 mm = current->mm;
252 else
253 mm = &init_mm;
255 dir = pgd_offset(mm, address & PAGE_MASK);
256 if (dir) {
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)) {
261 return(pte);
264 else {
265 return (0);
268 else {
269 return (0);
271 return (0);
274 unsigned long va_to_phys(unsigned long address)
276 pte_t *pte;
278 pte = va_to_pte(address);
279 if (pte)
280 return(((unsigned long)(pte_val(*pte)) & PAGE_MASK) | (address & ~(PAGE_MASK-1)));
281 return (0);
284 void
285 print_8xx_pte(struct mm_struct *mm, unsigned long addr)
287 pgd_t * pgd;
288 pmd_t * pmd;
289 pte_t * pte;
291 printk(" pte @ 0x%8lx: ", addr);
292 pgd = pgd_offset(mm, addr & PAGE_MASK);
293 if (pgd) {
294 pmd = pmd_offset(pgd, addr & PAGE_MASK);
295 if (pmd && pmd_present(*pmd)) {
296 pte = pte_offset(pmd, addr & PAGE_MASK);
297 if (pte) {
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 "
302 "CI: %lx v: %lx\n",
303 pp>>12, /* rpn */
304 (pp>>10)&3, /* pp */
305 (pp>>3)&1, /* small */
306 (pp>>2)&1, /* shared */
307 (pp>>1)&1, /* cache inhibit */
308 pp&1 /* valid */
310 #undef pp
312 else {
313 printk("no pte\n");
316 else {
317 printk("no pmd\n");
320 else {
321 printk("no pgd\n");
326 get_8xx_pte(struct mm_struct *mm, unsigned long addr)
328 pgd_t * pgd;
329 pmd_t * pmd;
330 pte_t * pte;
331 int retval = 0;
333 pgd = pgd_offset(mm, addr & PAGE_MASK);
334 if (pgd) {
335 pmd = pmd_offset(pgd, addr & PAGE_MASK);
336 if (pmd && pmd_present(*pmd)) {
337 pte = pte_offset(pmd, addr & PAGE_MASK);
338 if (pte) {
339 retval = (int)pte_val(*pte);
343 return(retval);
345 #endif /* CONFIG_8xx */
347 #if 0
349 * Misc debugging functions. Please leave them here. -- Cort
351 void print_pte(struct _PTE p)
353 printk(
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
362 * address. -- Cort
364 unsigned long htab_phys_to_va(unsigned long address)
366 extern PTE *Hash, *Hash_end;
367 PTE *ptr;
369 for ( ptr = Hash ; ptr < Hash_end ; ptr++ )
371 if ( ptr->rpn == (address>>12) )
372 printk("phys %08lX -> va ???\n",
373 address);
376 #endif