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