3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 * Derived from "arch/i386/mm/fault.c"
6 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Modified by Cort Dougan and Paul Mackerras.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/signal.h>
17 #include <linux/sched.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
21 #include <linux/types.h>
22 #include <linux/ptrace.h>
23 #include <linux/mman.h>
25 #include <linux/interrupt.h>
26 #include <linux/highmem.h>
27 #include <linux/module.h>
30 #include <asm/pgtable.h>
32 #include <asm/mmu_context.h>
33 #include <asm/system.h>
34 #include <asm/uaccess.h>
35 #include <asm/tlbflush.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; /* updated by hashtable.S:hash_page() */
45 unsigned long htab_evicts; /* updated by hashtable.S:hash_page() */
46 unsigned long htab_preloads; /* updated by hashtable.S:add_hash_page() */
47 unsigned long pte_misses; /* updated by do_page_fault() */
48 unsigned long pte_errors; /* updated by do_page_fault() */
49 unsigned int probingmem;
52 * Check whether the instruction at regs->nip is a store using
53 * an update addressing form which will update r1.
55 static int store_updates_sp(struct pt_regs *regs)
59 if (get_user(inst, (unsigned int __user *)regs->nip))
61 /* check for 1 in the rA field */
62 if (((inst >> 16) & 0x1f) != 1)
64 /* check major opcode */
73 /* check minor opcode */
74 switch ((inst >> 1) & 0x3ff) {
78 case 695: /* stfsux */
79 case 759: /* stfdux */
87 * For 600- and 800-family processors, the error_code parameter is DSISR
88 * for a data fault, SRR1 for an instruction fault. For 400-family processors
89 * the error_code parameter is ESR for a data fault, 0 for an instruction
92 int do_page_fault(struct pt_regs *regs, unsigned long address,
93 unsigned long error_code)
95 struct vm_area_struct * vma;
96 struct mm_struct *mm = current->mm;
98 int code = SEGV_MAPERR;
100 #if defined(CONFIG_4xx) || defined (CONFIG_BOOKE)
101 int is_write = error_code & ESR_DST;
106 * Fortunately the bit assignments in SRR1 for an instruction
107 * fault and DSISR for a data fault are mostly the same for the
108 * bits we are interested in. But there are some bits which
109 * indicate errors in DSISR but can validly be set in SRR1.
111 if (TRAP(regs) == 0x400)
112 error_code &= 0x48200000;
114 is_write = error_code & 0x02000000;
115 #endif /* CONFIG_4xx || CONFIG_BOOKE */
117 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
118 if (debugger_fault_handler && TRAP(regs) == 0x300) {
119 debugger_fault_handler(regs);
122 #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
123 if (error_code & 0x00400000) {
125 if (debugger_dabr_match(regs))
128 #endif /* !(CONFIG_4xx || CONFIG_BOOKE)*/
129 #endif /* CONFIG_XMON || CONFIG_KGDB */
131 if (in_atomic() || mm == NULL)
134 down_read(&mm->mmap_sem);
135 vma = find_vma(mm, address);
138 if (vma->vm_start <= address)
140 if (!(vma->vm_flags & VM_GROWSDOWN))
146 * N.B. The rs6000/xcoff ABI allows programs to access up to
147 * a few hundred bytes below the stack pointer.
148 * The kernel signal delivery code writes up to about 1.5kB
149 * below the stack pointer (r1) before decrementing it.
150 * The exec code can write slightly over 640kB to the stack
151 * before setting the user r1. Thus we allow the stack to
152 * expand to 1MB without further checks.
154 if (address + 0x100000 < vma->vm_end) {
155 /* get user regs even if this fault is in kernel mode */
156 struct pt_regs *uregs = current->thread.regs;
161 * A user-mode access to an address a long way below
162 * the stack pointer is only valid if the instruction
163 * is one which would update the stack pointer to the
164 * address accessed if the instruction completed,
165 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
166 * (or the byte, halfword, float or double forms).
168 * If we don't check this then any write to the area
169 * between the last mapped region and the stack will
170 * expand the stack rather than segfaulting.
172 if (address + 2048 < uregs->gpr[1]
173 && (!user_mode(regs) || !store_updates_sp(regs)))
176 if (expand_stack(vma, address))
181 #if defined(CONFIG_6xx)
182 if (error_code & 0x95700000)
183 /* an error such as lwarx to I/O controller space,
184 address matching DABR, eciwx, etc. */
186 #endif /* CONFIG_6xx */
187 #if defined(CONFIG_8xx)
188 /* The MPC8xx seems to always set 0x80000000, which is
189 * "undefined". Of those that can be set, this is the only
190 * one which seems bad.
192 if (error_code & 0x10000000)
193 /* Guarded storage error. */
195 #endif /* CONFIG_8xx */
199 if (!(vma->vm_flags & VM_WRITE))
201 #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
202 /* an exec - 4xx/Book-E allows for per-page execute permission */
203 } else if (TRAP(regs) == 0x400) {
208 /* It would be nice to actually enforce the VM execute
209 permission on CPUs which can do so, but far too
210 much stuff in userspace doesn't get the permissions
211 right, so we let any page be executed for now. */
212 if (! (vma->vm_flags & VM_EXEC))
216 /* Since 4xx/Book-E supports per-page execute permission,
217 * we lazily flush dcache to icache. */
219 if (get_pteptr(mm, address, &ptep, &pmdp)) {
220 spinlock_t *ptl = pte_lockptr(mm, pmdp);
222 if (pte_present(*ptep)) {
223 struct page *page = pte_page(*ptep);
225 if (!test_bit(PG_arch_1, &page->flags)) {
226 flush_dcache_icache_page(page);
227 set_bit(PG_arch_1, &page->flags);
229 pte_update(ptep, 0, _PAGE_HWEXEC);
231 pte_unmap_unlock(ptep, ptl);
232 up_read(&mm->mmap_sem);
235 pte_unmap_unlock(ptep, ptl);
240 /* protection fault */
241 if (error_code & 0x08000000)
243 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
248 * If for any reason at all we couldn't handle the fault,
249 * make sure we exit gracefully rather than endlessly redo
253 fault = handle_mm_fault(mm, vma, address, is_write);
254 if (unlikely(fault & VM_FAULT_ERROR)) {
255 if (fault & VM_FAULT_OOM)
257 else if (fault & VM_FAULT_SIGBUS)
261 if (fault & VM_FAULT_MAJOR)
266 up_read(&mm->mmap_sem);
268 * keep track of tlb+htab misses that are good addrs but
269 * just need pte's created via handle_mm_fault()
276 up_read(&mm->mmap_sem);
279 /* User mode accesses cause a SIGSEGV */
280 if (user_mode(regs)) {
281 _exception(SIGSEGV, regs, code, address);
288 * We ran out of memory, or some other thing happened to us that made
289 * us unable to handle the page fault gracefully.
292 up_read(&mm->mmap_sem);
293 if (is_init(current)) {
295 down_read(&mm->mmap_sem);
298 printk("VM: killing process %s\n", current->comm);
300 do_group_exit(SIGKILL);
304 up_read(&mm->mmap_sem);
305 info.si_signo = SIGBUS;
307 info.si_code = BUS_ADRERR;
308 info.si_addr = (void __user *)address;
309 force_sig_info (SIGBUS, &info, current);
310 if (!user_mode(regs))
316 * bad_page_fault is called when we have a bad access from the kernel.
317 * It is called from the DSI and ISI handlers in head.S and from some
318 * of the procedures in traps.c.
321 bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
323 const struct exception_table_entry *entry;
325 /* Are we prepared to handle this fault? */
326 if ((entry = search_exception_tables(regs->nip)) != NULL) {
327 regs->nip = entry->fixup;
331 /* kernel has accessed a bad area */
332 #if defined(CONFIG_XMON) || defined(CONFIG_KGDB)
333 if (debugger_kernel_faults)
336 die("kernel access of bad area", regs, sig);
341 /* The pgtable.h claims some functions generically exist, but I
342 * can't find them......
344 pte_t *va_to_pte(unsigned long address)
350 if (address < TASK_SIZE)
353 dir = pgd_offset(&init_mm, address);
355 pmd = pmd_offset(dir, address & PAGE_MASK);
356 if (pmd && pmd_present(*pmd)) {
357 pte = pte_offset_kernel(pmd, address & PAGE_MASK);
358 if (pte && pte_present(*pte))
365 unsigned long va_to_phys(unsigned long address)
369 pte = va_to_pte(address);
371 return(((unsigned long)(pte_val(*pte)) & PAGE_MASK) | (address & ~(PAGE_MASK)));
376 print_8xx_pte(struct mm_struct *mm, unsigned long addr)
382 printk(" pte @ 0x%8lx: ", addr);
383 pgd = pgd_offset(mm, addr & PAGE_MASK);
385 pmd = pmd_offset(pgd, addr & PAGE_MASK);
386 if (pmd && pmd_present(*pmd)) {
387 pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
389 printk(" (0x%08lx)->(0x%08lx)->0x%08lx\n",
390 (long)pgd, (long)pte, (long)pte_val(*pte));
391 #define pp ((long)pte_val(*pte))
392 printk(" RPN: %05lx PP: %lx SPS: %lx SH: %lx "
396 (pp>>3)&1, /* small */
397 (pp>>2)&1, /* shared */
398 (pp>>1)&1, /* cache inhibit */
417 get_8xx_pte(struct mm_struct *mm, unsigned long addr)
424 pgd = pgd_offset(mm, addr & PAGE_MASK);
426 pmd = pmd_offset(pgd, addr & PAGE_MASK);
427 if (pmd && pmd_present(*pmd)) {
428 pte = pte_offset_kernel(pmd, addr & PAGE_MASK);
430 retval = (int)pte_val(*pte);
436 #endif /* CONFIG_8xx */