arch: mm: pass userspace fault flag to generic fault handler
[linux-2.6-block.git] / arch / cris / mm / fault.c
CommitLineData
1da177e4 1/*
028c1f68 2 * arch/cris/mm/fault.c
1da177e4 3 *
028c1f68 4 * Copyright (C) 2000-2010 Axis Communications AB
1da177e4
LT
5 */
6
7#include <linux/mm.h>
8#include <linux/interrupt.h>
9#include <linux/module.h>
b4e8a181 10#include <linux/wait.h>
1da177e4 11#include <asm/uaccess.h>
b1a154db 12#include <arch/system.h>
1da177e4
LT
13
14extern int find_fixup_code(struct pt_regs *);
15extern void die_if_kernel(const char *, struct pt_regs *, long);
2d495ebc 16extern void show_registers(struct pt_regs *regs);
1da177e4
LT
17
18/* debug of low-level TLB reload */
19#undef DEBUG
20
21#ifdef DEBUG
22#define D(x) x
23#else
24#define D(x)
25#endif
26
27/* debug of higher-level faults */
28#define DPG(x)
29
30/* current active page directory */
31
fe87f94f 32DEFINE_PER_CPU(pgd_t *, current_pgd);
4f18cfbf 33unsigned long cris_signal_return_page;
1da177e4
LT
34
35/*
36 * This routine handles page faults. It determines the address,
37 * and the problem, and then passes it off to one of the appropriate
38 * routines.
39 *
40 * Notice that the address we're given is aligned to the page the fault
41 * occurred in, since we only get the PFN in R_MMU_CAUSE not the complete
42 * address.
43 *
44 * error_code:
3e1fdc4e
JN
45 * bit 0 == 0 means no page found, 1 means protection fault
46 * bit 1 == 0 means read, 1 means write
1da177e4
LT
47 *
48 * If this routine detects a bad access, it returns 1, otherwise it
49 * returns 0.
50 */
51
52asmlinkage void
53do_page_fault(unsigned long address, struct pt_regs *regs,
54 int protection, int writeaccess)
55{
56 struct task_struct *tsk;
57 struct mm_struct *mm;
58 struct vm_area_struct * vma;
59 siginfo_t info;
83c54070 60 int fault;
759496ba 61 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
1da177e4 62
3e1fdc4e
JN
63 D(printk(KERN_DEBUG
64 "Page fault for %lX on %X at %lX, prot %d write %d\n",
65 address, smp_processor_id(), instruction_pointer(regs),
66 protection, writeaccess));
1da177e4
LT
67
68 tsk = current;
69
70 /*
71 * We fault-in kernel-space virtual memory on-demand. The
72 * 'reference' page table is init_mm.pgd.
73 *
74 * NOTE! We MUST NOT take any locks for this case. We may
75 * be in an interrupt or a critical region, and should
76 * only copy the information from the master page table,
77 * nothing more.
78 *
79 * NOTE2: This is done so that, when updating the vmalloc
80 * mappings we don't have to walk all processes pgdirs and
81 * add the high mappings all at once. Instead we do it as they
82 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
83 * bit set so sometimes the TLB can use a lingering entry.
84 *
85 * This verifies that the fault happens in kernel space
86 * and that the fault was not a protection error (error_code & 1).
87 */
88
89 if (address >= VMALLOC_START &&
90 !protection &&
91 !user_mode(regs))
92 goto vmalloc_fault;
93
4f18cfbf
MS
94 /* When stack execution is not allowed we store the signal
95 * trampolines in the reserved cris_signal_return_page.
96 * Handle this in the exact same way as vmalloc (we know
97 * that the mapping is there and is valid so no need to
98 * call handle_mm_fault).
99 */
100 if (cris_signal_return_page &&
101 address == cris_signal_return_page &&
102 !protection && user_mode(regs))
103 goto vmalloc_fault;
104
1da177e4 105 /* we can and should enable interrupts at this point */
4f18cfbf 106 local_irq_enable();
1da177e4
LT
107
108 mm = tsk->mm;
109 info.si_code = SEGV_MAPERR;
110
111 /*
028c1f68
JN
112 * If we're in an interrupt or "atomic" operation or have no
113 * user context, we must not take the fault.
1da177e4
LT
114 */
115
028c1f68 116 if (in_atomic() || !mm)
1da177e4
LT
117 goto no_context;
118
759496ba
JW
119 if (user_mode(regs))
120 flags |= FAULT_FLAG_USER;
4d5914d6 121retry:
1da177e4
LT
122 down_read(&mm->mmap_sem);
123 vma = find_vma(mm, address);
124 if (!vma)
125 goto bad_area;
126 if (vma->vm_start <= address)
127 goto good_area;
128 if (!(vma->vm_flags & VM_GROWSDOWN))
129 goto bad_area;
130 if (user_mode(regs)) {
131 /*
132 * accessing the stack below usp is always a bug.
133 * we get page-aligned addresses so we can only check
134 * if we're within a page from usp, but that might be
135 * enough to catch brutal errors at least.
136 */
137 if (address + PAGE_SIZE < rdusp())
138 goto bad_area;
139 }
140 if (expand_stack(vma, address))
141 goto bad_area;
142
143 /*
144 * Ok, we have a good vm_area for this memory access, so
145 * we can handle it..
146 */
147
148 good_area:
149 info.si_code = SEGV_ACCERR;
150
151 /* first do some preliminary protection checks */
152
4f18cfbf
MS
153 if (writeaccess == 2){
154 if (!(vma->vm_flags & VM_EXEC))
155 goto bad_area;
156 } else if (writeaccess == 1) {
1da177e4
LT
157 if (!(vma->vm_flags & VM_WRITE))
158 goto bad_area;
759496ba 159 flags |= FAULT_FLAG_WRITE;
1da177e4
LT
160 } else {
161 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
162 goto bad_area;
163 }
164
165 /*
166 * If for any reason at all we couldn't handle the fault,
167 * make sure we exit gracefully rather than endlessly redo
168 * the fault.
169 */
170
4d5914d6
KC
171 fault = handle_mm_fault(mm, vma, address, flags);
172
173 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
174 return;
175
83c54070
NP
176 if (unlikely(fault & VM_FAULT_ERROR)) {
177 if (fault & VM_FAULT_OOM)
178 goto out_of_memory;
179 else if (fault & VM_FAULT_SIGBUS)
180 goto do_sigbus;
181 BUG();
1da177e4 182 }
4d5914d6
KC
183
184 if (flags & FAULT_FLAG_ALLOW_RETRY) {
185 if (fault & VM_FAULT_MAJOR)
186 tsk->maj_flt++;
187 else
188 tsk->min_flt++;
189 if (fault & VM_FAULT_RETRY) {
190 flags &= ~FAULT_FLAG_ALLOW_RETRY;
45cac65b 191 flags |= FAULT_FLAG_TRIED;
4d5914d6
KC
192
193 /*
194 * No need to up_read(&mm->mmap_sem) as we would
195 * have already released it in __lock_page_or_retry
196 * in mm/filemap.c.
197 */
198
199 goto retry;
200 }
201 }
1da177e4
LT
202
203 up_read(&mm->mmap_sem);
204 return;
205
206 /*
207 * Something tried to access memory that isn't in our memory map..
208 * Fix it, but check if it's kernel or user first..
209 */
210
211 bad_area:
212 up_read(&mm->mmap_sem);
213
214 bad_area_nosemaphore:
215 DPG(show_registers(regs));
216
217 /* User mode accesses just cause a SIGSEGV */
218
219 if (user_mode(regs)) {
b4e8a181
JN
220 printk(KERN_NOTICE "%s (pid %d) segfaults for page "
221 "address %08lx at pc %08lx\n",
222 tsk->comm, tsk->pid,
223 address, instruction_pointer(regs));
2d495ebc
JN
224
225 /* With DPG on, we've already dumped registers above. */
226 DPG(if (0))
227 show_registers(regs);
228
b4e8a181
JN
229#ifdef CONFIG_NO_SEGFAULT_TERMINATION
230 DECLARE_WAIT_QUEUE_HEAD(wq);
231 wait_event_interruptible(wq, 0 == 1);
232#else
1da177e4
LT
233 info.si_signo = SIGSEGV;
234 info.si_errno = 0;
235 /* info.si_code has been set above */
236 info.si_addr = (void *)address;
237 force_sig_info(SIGSEGV, &info, tsk);
b4e8a181 238#endif
1da177e4
LT
239 return;
240 }
241
242 no_context:
243
244 /* Are we prepared to handle this kernel fault?
245 *
3e1fdc4e 246 * (The kernel has valid exception-points in the source
af901ca1 247 * when it accesses user-memory. When it fails in one
1da177e4
LT
248 * of those points, we find it in a table and do a jump
249 * to some fixup code that loads an appropriate error
250 * code)
251 */
252
253 if (find_fixup_code(regs))
254 return;
255
256 /*
257 * Oops. The kernel tried to access some bad page. We'll have to
258 * terminate things with extreme prejudice.
259 */
260
3e1fdc4e
JN
261 if (!oops_in_progress) {
262 oops_in_progress = 1;
263 if ((unsigned long) (address) < PAGE_SIZE)
264 printk(KERN_ALERT "Unable to handle kernel NULL "
265 "pointer dereference");
266 else
267 printk(KERN_ALERT "Unable to handle kernel access"
268 " at virtual address %08lx\n", address);
269
270 die_if_kernel("Oops", regs, (writeaccess << 1) | protection);
271 oops_in_progress = 0;
272 }
1da177e4
LT
273
274 do_exit(SIGKILL);
275
276 /*
277 * We ran out of memory, or some other thing happened to us that made
278 * us unable to handle the page fault gracefully.
279 */
280
281 out_of_memory:
282 up_read(&mm->mmap_sem);
3648bdf7
JN
283 if (!user_mode(regs))
284 goto no_context;
285 pagefault_out_of_memory();
286 return;
1da177e4
LT
287
288 do_sigbus:
289 up_read(&mm->mmap_sem);
290
291 /*
292 * Send a sigbus, regardless of whether we were in kernel
293 * or user mode.
294 */
295 info.si_signo = SIGBUS;
296 info.si_errno = 0;
297 info.si_code = BUS_ADRERR;
298 info.si_addr = (void *)address;
299 force_sig_info(SIGBUS, &info, tsk);
300
301 /* Kernel mode? Handle exceptions or die */
302 if (!user_mode(regs))
303 goto no_context;
304 return;
305
306vmalloc_fault:
307 {
308 /*
309 * Synchronize this task's top level page-table
310 * with the 'reference' page table.
311 *
312 * Use current_pgd instead of tsk->active_mm->pgd
313 * since the latter might be unavailable if this
314 * code is executed in a misfortunately run irq
315 * (like inside schedule() between switch_mm and
316 * switch_to...).
317 */
318
319 int offset = pgd_index(address);
320 pgd_t *pgd, *pgd_k;
4f18cfbf 321 pud_t *pud, *pud_k;
1da177e4
LT
322 pmd_t *pmd, *pmd_k;
323 pte_t *pte_k;
324
4f18cfbf 325 pgd = (pgd_t *)per_cpu(current_pgd, smp_processor_id()) + offset;
1da177e4
LT
326 pgd_k = init_mm.pgd + offset;
327
328 /* Since we're two-level, we don't need to do both
329 * set_pgd and set_pmd (they do the same thing). If
330 * we go three-level at some point, do the right thing
3e1fdc4e
JN
331 * with pgd_present and set_pgd here.
332 *
1da177e4
LT
333 * Also, since the vmalloc area is global, we don't
334 * need to copy individual PTE's, it is enough to
335 * copy the pgd pointer into the pte page of the
336 * root task. If that is there, we'll find our pte if
337 * it exists.
338 */
339
4f18cfbf
MS
340 pud = pud_offset(pgd, address);
341 pud_k = pud_offset(pgd_k, address);
342 if (!pud_present(*pud_k))
343 goto no_context;
344
345 pmd = pmd_offset(pud, address);
346 pmd_k = pmd_offset(pud_k, address);
1da177e4
LT
347
348 if (!pmd_present(*pmd_k))
349 goto bad_area_nosemaphore;
350
351 set_pmd(pmd, *pmd_k);
352
353 /* Make sure the actual PTE exists as well to
354 * catch kernel vmalloc-area accesses to non-mapped
355 * addresses. If we don't do this, this will just
356 * silently loop forever.
357 */
358
359 pte_k = pte_offset_kernel(pmd_k, address);
360 if (!pte_present(*pte_k))
361 goto no_context;
362
363 return;
364 }
365}
4f18cfbf
MS
366
367/* Find fixup code. */
368int
369find_fixup_code(struct pt_regs *regs)
370{
371 const struct exception_table_entry *fixup;
a90993c6
JN
372 /* in case of delay slot fault (v32) */
373 unsigned long ip = (instruction_pointer(regs) & ~0x1);
4f18cfbf 374
a90993c6
JN
375 fixup = search_exception_tables(ip);
376 if (fixup != 0) {
4f18cfbf
MS
377 /* Adjust the instruction pointer in the stackframe. */
378 instruction_pointer(regs) = fixup->fixup;
379 arch_fixup(regs);
380 return 1;
381 }
382
383 return 0;
384}