blob: b2f2d2d668491905dbbc37c385449852a9e7fdde [file] [log] [blame]
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -07001/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
4 * Based on linux/arch/sh/mm/fault.c:
5 * Copyright (C) 1999 Niibe Yutaka
6 *
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.
10 */
11
12#include <linux/mm.h>
13#include <linux/module.h>
14#include <linux/pagemap.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070015#include <linux/kdebug.h>
Christoph Hellwig9caebec2007-05-12 17:56:11 +020016#include <linux/kprobes.h>
17
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070018#include <asm/mmu_context.h>
19#include <asm/sysreg.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070020#include <asm/tlb.h>
Haavard Skinnemoen623b0352007-03-13 17:59:11 +010021#include <asm/uaccess.h>
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070022
23#ifdef CONFIG_KPROBES
Christoph Hellwig9caebec2007-05-12 17:56:11 +020024static inline int notify_page_fault(struct pt_regs *regs, int trap)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070025{
Christoph Hellwig9caebec2007-05-12 17:56:11 +020026 int ret = 0;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070027
Christoph Hellwig9caebec2007-05-12 17:56:11 +020028 if (!user_mode(regs)) {
29 if (kprobe_running() && kprobe_fault_handler(regs, trap))
30 ret = 1;
31 }
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070032
Christoph Hellwig9caebec2007-05-12 17:56:11 +020033 return ret;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070034}
35#else
Christoph Hellwig9caebec2007-05-12 17:56:11 +020036static inline int notify_page_fault(struct pt_regs *regs, int trap)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070037{
Christoph Hellwig9caebec2007-05-12 17:56:11 +020038 return 0;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070039}
40#endif
41
Haavard Skinnemoen623b0352007-03-13 17:59:11 +010042int exception_trace = 1;
43
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070044/*
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.
47 *
48 * ecr is the Exception Cause Register. Possible values are:
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070049 * 6: Protection fault (instruction access)
Haavard Skinnemoen623b0352007-03-13 17:59:11 +010050 * 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)
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070055 */
56asmlinkage void do_page_fault(unsigned long ecr, struct pt_regs *regs)
57{
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;
Haavard Skinnemoen623b0352007-03-13 17:59:11 +010064 long signr;
65 int code;
Nick Piggin83c54072007-07-19 01:47:05 -070066 int fault;
Kautuk Consulbf7c27e2012-07-30 14:39:25 -070067 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070068
Christoph Hellwig9caebec2007-05-12 17:56:11 +020069 if (notify_page_fault(regs, ecr))
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070070 return;
71
72 address = sysreg_read(TLBEAR);
73
74 tsk = current;
75 mm = tsk->mm;
76
Haavard Skinnemoen623b0352007-03-13 17:59:11 +010077 signr = SIGSEGV;
78 code = SEGV_MAPERR;
79
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070080 /*
81 * If we're in an interrupt or have no user context, we must
82 * not take the fault...
83 */
84 if (in_atomic() || !mm || regs->sr & SYSREG_BIT(GM))
85 goto no_context;
86
87 local_irq_enable();
88
Kautuk Consulbf7c27e2012-07-30 14:39:25 -070089retry:
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -070090 down_read(&mm->mmap_sem);
91
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;
101
102 /*
103 * Ok, we have a good vm_area for this memory access, so we
104 * can handle it...
105 */
106good_area:
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100107 code = SEGV_ACCERR;
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100108
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700109 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;
Kautuk Consulbf7c27e2012-07-30 14:39:25 -0700124 flags |= FAULT_FLAG_WRITE;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700125 break;
126 default:
127 panic("Unhandled case %lu in do_page_fault!", ecr);
128 }
129
130 /*
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.
134 */
Kautuk Consulbf7c27e2012-07-30 14:39:25 -0700135 fault = handle_mm_fault(mm, vma, address, flags);
136
137 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
138 return;
139
Nick Piggin83c54072007-07-19 01:47:05 -0700140 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;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700145 BUG();
146 }
Kautuk Consulbf7c27e2012-07-30 14:39:25 -0700147
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;
Shaohua Li45cac652012-10-08 16:32:19 -0700155 flags |= FAULT_FLAG_TRIED;
Kautuk Consulbf7c27e2012-07-30 14:39:25 -0700156
157 /*
158 * No need to up_read(&mm->mmap_sem) as we would have
159 * already released it in __lock_page_or_retry() in
160 * mm/filemap.c.
161 */
162 goto retry;
163 }
164 }
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700165
166 up_read(&mm->mmap_sem);
167 return;
168
169 /*
170 * Something tried to access memory that isn't in our memory
171 * map. Fix it, but check if it's kernel or user first...
172 */
173bad_area:
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700174 up_read(&mm->mmap_sem);
175
176 if (user_mode(regs)) {
Andrea Righi126187f2007-05-23 14:14:52 -0700177 if (exception_trace && printk_ratelimit())
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100178 printk("%s%s[%d]: segfault at %08lx pc %08lx "
179 "sp %08lx ecr %lu\n",
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700180 is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100181 tsk->comm, tsk->pid, address, regs->pc,
182 regs->sp, ecr);
183 _exception(SIGSEGV, regs, code, address);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700184 return;
185 }
186
187no_context:
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700188 /* Are we prepared to handle this kernel fault? */
189 fixup = search_exception_tables(regs->pc);
190 if (fixup) {
191 regs->pc = fixup->fixup;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700192 return;
193 }
194
195 /*
196 * Oops. The kernel tried to access some bad page. We'll have
197 * to terminate things with extreme prejudice.
198 */
199 if (address < PAGE_SIZE)
200 printk(KERN_ALERT
201 "Unable to handle kernel NULL pointer dereference");
202 else
203 printk(KERN_ALERT
204 "Unable to handle kernel paging request");
205 printk(" at virtual address %08lx\n", address);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700206
207 page = sysreg_read(PTBR);
208 printk(KERN_ALERT "ptbr = %08lx", page);
Haavard Skinnemoen32019822008-02-13 12:32:34 +0100209 if (address >= TASK_SIZE)
210 page = (unsigned long)swapper_pg_dir;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700211 if (page) {
212 page = ((unsigned long *)page)[address >> 22];
213 printk(" pgd = %08lx", page);
214 if (page & _PAGE_PRESENT) {
215 page &= PAGE_MASK;
216 address &= 0x003ff000;
217 page = ((unsigned long *)__va(page))[address >> PAGE_SHIFT];
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100218 printk(" pte = %08lx", page);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700219 }
220 }
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100221 printk("\n");
222 die("Kernel access of bad area", regs, signr);
223 return;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700224
225 /*
226 * We ran out of memory, or some other thing happened to us
227 * that made us unable to handle the page fault gracefully.
228 */
229out_of_memory:
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700230 up_read(&mm->mmap_sem);
Nick Piggin67a8a202010-08-09 17:19:02 -0700231 pagefault_out_of_memory();
232 if (!user_mode(regs))
233 goto no_context;
234 return;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700235
236do_sigbus:
237 up_read(&mm->mmap_sem);
238
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700239 /* Kernel mode? Handle exceptions or die */
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100240 signr = SIGBUS;
241 code = BUS_ADRERR;
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700242 if (!user_mode(regs))
243 goto no_context;
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100244
245 if (exception_trace)
246 printk("%s%s[%d]: bus error at %08lx pc %08lx "
247 "sp %08lx ecr %lu\n",
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700248 is_global_init(tsk) ? KERN_EMERG : KERN_INFO,
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100249 tsk->comm, tsk->pid, address, regs->pc,
250 regs->sp, ecr);
251
252 _exception(SIGBUS, regs, BUS_ADRERR, address);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700253}
254
255asmlinkage void do_bus_error(unsigned long addr, int write_access,
256 struct pt_regs *regs)
257{
258 printk(KERN_ALERT
259 "Bus error at physical address 0x%08lx (%s access)\n",
260 addr, write_access ? "write" : "read");
261 printk(KERN_INFO "DTLB dump:\n");
262 dump_dtlb();
Haavard Skinnemoen623b0352007-03-13 17:59:11 +0100263 die("Bus Error", regs, SIGKILL);
Haavard Skinnemoen5f97f7f2006-09-25 23:32:13 -0700264}