Disintegrate asm/system.h for Microblaze
[linux-2.6.git] / arch / microblaze / mm / fault.c
blobc38a265846dec68c427398834e9833ceced31753
1 /*
2 * arch/microblaze/mm/fault.c
4 * Copyright (C) 2007 Xilinx, Inc. All rights reserved.
6 * Derived from "arch/ppc/mm/fault.c"
7 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
9 * Derived from "arch/i386/mm/fault.c"
10 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
12 * Modified by Cort Dougan and Paul Mackerras.
14 * This file is subject to the terms and conditions of the GNU General
15 * Public License. See the file COPYING in the main directory of this
16 * archive for more details.
20 #include <linux/module.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
27 #include <linux/ptrace.h>
28 #include <linux/mman.h>
29 #include <linux/mm.h>
30 #include <linux/interrupt.h>
32 #include <asm/page.h>
33 #include <asm/pgtable.h>
34 #include <asm/mmu.h>
35 #include <asm/mmu_context.h>
36 #include <linux/uaccess.h>
37 #include <asm/exceptions.h>
39 static unsigned long pte_misses; /* updated by do_page_fault() */
40 static unsigned long pte_errors; /* updated by do_page_fault() */
43 * Check whether the instruction at regs->pc is a store using
44 * an update addressing form which will update r1.
46 static int store_updates_sp(struct pt_regs *regs)
48 unsigned int inst;
50 if (get_user(inst, (unsigned int __user *)regs->pc))
51 return 0;
52 /* check for 1 in the rD field */
53 if (((inst >> 21) & 0x1f) != 1)
54 return 0;
55 /* check for store opcodes */
56 if ((inst & 0xd0000000) == 0xd0000000)
57 return 1;
58 return 0;
63 * bad_page_fault is called when we have a bad access from the kernel.
64 * It is called from do_page_fault above and from some of the procedures
65 * in traps.c.
67 void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig)
69 const struct exception_table_entry *fixup;
70 /* MS: no context */
71 /* Are we prepared to handle this fault? */
72 fixup = search_exception_tables(regs->pc);
73 if (fixup) {
74 regs->pc = fixup->fixup;
75 return;
78 /* kernel has accessed a bad area */
79 die("kernel access of bad area", regs, sig);
83 * The error_code parameter is ESR for a data fault,
84 * 0 for an instruction fault.
86 void do_page_fault(struct pt_regs *regs, unsigned long address,
87 unsigned long error_code)
89 struct vm_area_struct *vma;
90 struct mm_struct *mm = current->mm;
91 siginfo_t info;
92 int code = SEGV_MAPERR;
93 int is_write = error_code & ESR_S;
94 int fault;
96 regs->ear = address;
97 regs->esr = error_code;
99 /* On a kernel SLB miss we can only check for a valid exception entry */
100 if (unlikely(kernel_mode(regs) && (address >= TASK_SIZE))) {
101 printk(KERN_WARNING "kernel task_size exceed");
102 _exception(SIGSEGV, regs, code, address);
105 /* for instr TLB miss and instr storage exception ESR_S is undefined */
106 if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11)
107 is_write = 0;
109 if (unlikely(in_atomic() || !mm)) {
110 if (kernel_mode(regs))
111 goto bad_area_nosemaphore;
113 /* in_atomic() in user mode is really bad,
114 as is current->mm == NULL. */
115 printk(KERN_EMERG "Page fault in user mode with "
116 "in_atomic(), mm = %p\n", mm);
117 printk(KERN_EMERG "r15 = %lx MSR = %lx\n",
118 regs->r15, regs->msr);
119 die("Weird page fault", regs, SIGSEGV);
122 /* When running in the kernel we expect faults to occur only to
123 * addresses in user space. All other faults represent errors in the
124 * kernel and should generate an OOPS. Unfortunately, in the case of an
125 * erroneous fault occurring in a code path which already holds mmap_sem
126 * we will deadlock attempting to validate the fault against the
127 * address space. Luckily the kernel only validly references user
128 * space from well defined areas of code, which are listed in the
129 * exceptions table.
131 * As the vast majority of faults will be valid we will only perform
132 * the source reference check when there is a possibility of a deadlock.
133 * Attempt to lock the address space, if we cannot we then validate the
134 * source. If this is invalid we can skip the address space check,
135 * thus avoiding the deadlock.
137 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
138 if (kernel_mode(regs) && !search_exception_tables(regs->pc))
139 goto bad_area_nosemaphore;
141 down_read(&mm->mmap_sem);
144 vma = find_vma(mm, address);
145 if (unlikely(!vma))
146 goto bad_area;
148 if (vma->vm_start <= address)
149 goto good_area;
151 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
152 goto bad_area;
154 if (unlikely(!is_write))
155 goto bad_area;
158 * N.B. The ABI allows programs to access up to
159 * a few hundred bytes below the stack pointer (TBD).
160 * The kernel signal delivery code writes up to about 1.5kB
161 * below the stack pointer (r1) before decrementing it.
162 * The exec code can write slightly over 640kB to the stack
163 * before setting the user r1. Thus we allow the stack to
164 * expand to 1MB without further checks.
166 if (unlikely(address + 0x100000 < vma->vm_end)) {
168 /* get user regs even if this fault is in kernel mode */
169 struct pt_regs *uregs = current->thread.regs;
170 if (uregs == NULL)
171 goto bad_area;
174 * A user-mode access to an address a long way below
175 * the stack pointer is only valid if the instruction
176 * is one which would update the stack pointer to the
177 * address accessed if the instruction completed,
178 * i.e. either stwu rs,n(r1) or stwux rs,r1,rb
179 * (or the byte, halfword, float or double forms).
181 * If we don't check this then any write to the area
182 * between the last mapped region and the stack will
183 * expand the stack rather than segfaulting.
185 if (address + 2048 < uregs->r1
186 && (kernel_mode(regs) || !store_updates_sp(regs)))
187 goto bad_area;
189 if (expand_stack(vma, address))
190 goto bad_area;
192 good_area:
193 code = SEGV_ACCERR;
195 /* a write */
196 if (unlikely(is_write)) {
197 if (unlikely(!(vma->vm_flags & VM_WRITE)))
198 goto bad_area;
199 /* a read */
200 } else {
201 /* protection fault */
202 if (unlikely(error_code & 0x08000000))
203 goto bad_area;
204 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC))))
205 goto bad_area;
209 * If for any reason at all we couldn't handle the fault,
210 * make sure we exit gracefully rather than endlessly redo
211 * the fault.
213 fault = handle_mm_fault(mm, vma, address, is_write ? FAULT_FLAG_WRITE : 0);
214 if (unlikely(fault & VM_FAULT_ERROR)) {
215 if (fault & VM_FAULT_OOM)
216 goto out_of_memory;
217 else if (fault & VM_FAULT_SIGBUS)
218 goto do_sigbus;
219 BUG();
221 if (unlikely(fault & VM_FAULT_MAJOR))
222 current->maj_flt++;
223 else
224 current->min_flt++;
225 up_read(&mm->mmap_sem);
227 * keep track of tlb+htab misses that are good addrs but
228 * just need pte's created via handle_mm_fault()
229 * -- Cort
231 pte_misses++;
232 return;
234 bad_area:
235 up_read(&mm->mmap_sem);
237 bad_area_nosemaphore:
238 pte_errors++;
240 /* User mode accesses cause a SIGSEGV */
241 if (user_mode(regs)) {
242 _exception(SIGSEGV, regs, code, address);
243 /* info.si_signo = SIGSEGV;
244 info.si_errno = 0;
245 info.si_code = code;
246 info.si_addr = (void *) address;
247 force_sig_info(SIGSEGV, &info, current);*/
248 return;
251 bad_page_fault(regs, address, SIGSEGV);
252 return;
255 * We ran out of memory, or some other thing happened to us that made
256 * us unable to handle the page fault gracefully.
258 out_of_memory:
259 up_read(&mm->mmap_sem);
260 if (!user_mode(regs))
261 bad_page_fault(regs, address, SIGKILL);
262 else
263 pagefault_out_of_memory();
264 return;
266 do_sigbus:
267 up_read(&mm->mmap_sem);
268 if (user_mode(regs)) {
269 info.si_signo = SIGBUS;
270 info.si_errno = 0;
271 info.si_code = BUS_ADRERR;
272 info.si_addr = (void __user *)address;
273 force_sig_info(SIGBUS, &info, current);
274 return;
276 bad_page_fault(regs, address, SIGBUS);