llc2: Remove dead code for state machine
[linux/fpc-iii.git] / arch / avr32 / mm / fault.c
blobb92e60958617eb25ff90481989f494c3625f5a98
1 /*
2 * Copyright (C) 2004-2006 Atmel Corporation
4 * Based on linux/arch/sh/mm/fault.c:
5 * Copyright (C) 1999 Niibe Yutaka
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/pagemap.h>
15 #include <linux/kdebug.h>
16 #include <linux/kprobes.h>
18 #include <asm/mmu_context.h>
19 #include <asm/sysreg.h>
20 #include <asm/tlb.h>
21 #include <asm/uaccess.h>
23 #ifdef CONFIG_KPROBES
24 static inline int notify_page_fault(struct pt_regs *regs, int trap)
26 int ret = 0;
28 if (!user_mode(regs)) {
29 if (kprobe_running() && kprobe_fault_handler(regs, trap))
30 ret = 1;
33 return ret;
35 #else
36 static inline int notify_page_fault(struct pt_regs *regs, int trap)
38 return 0;
40 #endif
42 int exception_trace = 1;
45 * This routine handles page faults. It determines the address and the
46 * problem, and then passes it off to one of the appropriate routines.
48 * ecr is the Exception Cause Register. Possible values are:
49 * 6: Protection fault (instruction access)
50 * 15: Protection fault (read access)
51 * 16: Protection fault (write access)
52 * 20: Page not found (instruction access)
53 * 24: Page not found (read access)
54 * 28: Page not found (write access)
56 asmlinkage void do_page_fault(unsigned long ecr, struct pt_regs *regs)
58 struct task_struct *tsk;
59 struct mm_struct *mm;
60 struct vm_area_struct *vma;
61 const struct exception_table_entry *fixup;
62 unsigned long address;
63 unsigned long page;
64 long signr;
65 int code;
66 int fault;
67 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
69 if (notify_page_fault(regs, ecr))
70 return;
72 address = sysreg_read(TLBEAR);
74 tsk = current;
75 mm = tsk->mm;
77 signr = SIGSEGV;
78 code = SEGV_MAPERR;
81 * If we're in an interrupt or have no user context, we must
82 * not take the fault...
84 if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM))
85 goto no_context;
87 local_irq_enable();
89 retry:
90 down_read(&mm->mmap_sem);
92 vma = find_vma(mm, address);
93 if (!vma)
94 goto bad_area;
95 if (vma->vm_start <= address)
96 goto good_area;
97 if (!(vma->vm_flags & VM_GROWSDOWN))
98 goto bad_area;
99 if (expand_stack(vma, address))
100 goto bad_area;
103 * Ok, we have a good vm_area for this memory access, so we
104 * can handle it...
106 good_area:
107 code = SEGV_ACCERR;
109 switch (ecr) {
110 case ECR_PROTECTION_X:
111 case ECR_TLB_MISS_X:
112 if (!(vma->vm_flags & VM_EXEC))
113 goto bad_area;
114 break;
115 case ECR_PROTECTION_R:
116 case ECR_TLB_MISS_R:
117 if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
118 goto bad_area;
119 break;
120 case ECR_PROTECTION_W:
121 case ECR_TLB_MISS_W:
122 if (!(vma->vm_flags & VM_WRITE))
123 goto bad_area;
124 flags |= FAULT_FLAG_WRITE;
125 break;
126 default:
127 panic("Unhandled case %lu in do_page_fault!", ecr);
131 * If for any reason at all we couldn't handle the fault, make
132 * sure we exit gracefully rather than endlessly redo the
133 * fault.
135 fault = handle_mm_fault(mm, vma, address, flags);
137 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
138 return;
140 if (unlikely(fault & VM_FAULT_ERROR)) {
141 if (fault & VM_FAULT_OOM)
142 goto out_of_memory;
143 else if (fault & VM_FAULT_SIGBUS)
144 goto do_sigbus;
145 BUG();
148 if (flags & FAULT_FLAG_ALLOW_RETRY) {
149 if (fault & VM_FAULT_MAJOR)
150 tsk->maj_flt++;
151 else
152 tsk->min_flt++;
153 if (fault & VM_FAULT_RETRY) {
154 flags &= ~FAULT_FLAG_ALLOW_RETRY;
157 * No need to up_read(&mm->mmap_sem) as we would have
158 * already released it in __lock_page_or_retry() in
159 * mm/filemap.c.
161 goto retry;
165 up_read(&mm->mmap_sem);
166 return;
169 * Something tried to access memory that isn't in our memory
170 * map. Fix it, but check if it's kernel or user first...
172 bad_area:
173 up_read(&mm->mmap_sem);
175 if (user_mode(regs)) {
176 if (exception_trace && printk_ratelimit())
177 printk("%s%s[%d]: segfault at %08lx pc %08lx "
178 "sp %08lx ecr %lu\n",
179 is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
180 tsk->comm, tsk->pid, address, regs->pc,
181 regs->sp, ecr);
182 _exception(SIGSEGV, regs, code, address);
183 return;
186 no_context:
187 /* Are we prepared to handle this kernel fault? */
188 fixup = search_exception_tables(regs->pc);
189 if (fixup) {
190 regs->pc = fixup->fixup;
191 return;
195 * Oops. The kernel tried to access some bad page. We'll have
196 * to terminate things with extreme prejudice.
198 if (address < PAGE_SIZE)
199 printk(KERN_ALERT
200 "Unable to handle kernel NULL pointer dereference");
201 else
202 printk(KERN_ALERT
203 "Unable to handle kernel paging request");
204 printk(" at virtual address %08lx\n", address);
206 page = sysreg_read(PTBR);
207 printk(KERN_ALERT "ptbr = %08lx", page);
208 if (address >= TASK_SIZE)
209 page = (unsigned long)swapper_pg_dir;
210 if (page) {
211 page = ((unsigned long *)page)[address >> 22];
212 printk(" pgd = %08lx", page);
213 if (page & _PAGE_PRESENT) {
214 page &= PAGE_MASK;
215 address &= 0x003ff000;
216 page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT];
217 printk(" pte = %08lx", page);
220 printk("\n");
221 die("Kernel access of bad area", regs, signr);
222 return;
225 * We ran out of memory, or some other thing happened to us
226 * that made us unable to handle the page fault gracefully.
228 out_of_memory:
229 up_read(&mm->mmap_sem);
230 pagefault_out_of_memory();
231 if (!user_mode(regs))
232 goto no_context;
233 return;
235 do_sigbus:
236 up_read(&mm->mmap_sem);
238 /* Kernel mode? Handle exceptions or die */
239 signr = SIGBUS;
240 code = BUS_ADRERR;
241 if (!user_mode(regs))
242 goto no_context;
244 if (exception_trace)
245 printk("%s%s[%d]: bus error at %08lx pc %08lx "
246 "sp %08lx ecr %lu\n",
247 is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
248 tsk->comm, tsk->pid, address, regs->pc,
249 regs->sp, ecr);
251 _exception(SIGBUS, regs, BUS_ADRERR, address);
254 asmlinkage void do_bus_error(unsigned long addr, int write_access,
255 struct pt_regs *regs)
257 printk(KERN_ALERT
258 "Bus error at physical address 0x%08lx (%s access)\n",
259 addr, write_access ? "write" : "read");
260 printk(KERN_INFO "DTLB dump:\n");
261 dump_dtlb();
262 die("Bus Error", regs, SIGKILL);