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