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