4 * Copyright (C) 2000-2010 Axis Communications AB
8 #include <linux/interrupt.h>
9 #include <linux/module.h>
10 #include <linux/wait.h>
11 #include <linux/uaccess.h>
12 #include <arch/system.h>
14 extern int find_fixup_code(struct pt_regs
*);
15 extern void die_if_kernel(const char *, struct pt_regs
*, long);
16 extern void show_registers(struct pt_regs
*regs
);
18 /* debug of low-level TLB reload */
27 /* debug of higher-level faults */
30 /* current active page directory */
32 DEFINE_PER_CPU(pgd_t
*, current_pgd
);
33 unsigned long cris_signal_return_page
;
36 * This routine handles page faults. It determines the address,
37 * and the problem, and then passes it off to one of the appropriate
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
45 * bit 0 == 0 means no page found, 1 means protection fault
46 * bit 1 == 0 means read, 1 means write
48 * If this routine detects a bad access, it returns 1, otherwise it
53 do_page_fault(unsigned long address
, struct pt_regs
*regs
,
54 int protection
, int writeaccess
)
56 struct task_struct
*tsk
;
58 struct vm_area_struct
* vma
;
61 unsigned int flags
= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
;
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
));
71 * We fault-in kernel-space virtual memory on-demand. The
72 * 'reference' page table is init_mm.pgd.
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,
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.
85 * This verifies that the fault happens in kernel space
86 * and that the fault was not a protection error (error_code & 1).
89 if (address
>= VMALLOC_START
&&
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).
100 if (cris_signal_return_page
&&
101 address
== cris_signal_return_page
&&
102 !protection
&& user_mode(regs
))
105 /* we can and should enable interrupts at this point */
109 info
.si_code
= SEGV_MAPERR
;
112 * If we're in an interrupt, have pagefaults disabled or have no
113 * user context, we must not take the fault.
116 if (faulthandler_disabled() || !mm
)
120 flags
|= FAULT_FLAG_USER
;
122 down_read(&mm
->mmap_sem
);
123 vma
= find_vma(mm
, address
);
126 if (vma
->vm_start
<= address
)
128 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
130 if (user_mode(regs
)) {
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.
137 if (address
+ PAGE_SIZE
< rdusp())
140 if (expand_stack(vma
, address
))
144 * Ok, we have a good vm_area for this memory access, so
149 info
.si_code
= SEGV_ACCERR
;
151 /* first do some preliminary protection checks */
153 if (writeaccess
== 2){
154 if (!(vma
->vm_flags
& VM_EXEC
))
156 } else if (writeaccess
== 1) {
157 if (!(vma
->vm_flags
& VM_WRITE
))
159 flags
|= FAULT_FLAG_WRITE
;
161 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
166 * If for any reason at all we couldn't handle the fault,
167 * make sure we exit gracefully rather than endlessly redo
171 fault
= handle_mm_fault(vma
, address
, flags
);
173 if ((fault
& VM_FAULT_RETRY
) && fatal_signal_pending(current
))
176 if (unlikely(fault
& VM_FAULT_ERROR
)) {
177 if (fault
& VM_FAULT_OOM
)
179 else if (fault
& VM_FAULT_SIGSEGV
)
181 else if (fault
& VM_FAULT_SIGBUS
)
186 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
187 if (fault
& VM_FAULT_MAJOR
)
191 if (fault
& VM_FAULT_RETRY
) {
192 flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
193 flags
|= FAULT_FLAG_TRIED
;
196 * No need to up_read(&mm->mmap_sem) as we would
197 * have already released it in __lock_page_or_retry
205 up_read(&mm
->mmap_sem
);
209 * Something tried to access memory that isn't in our memory map..
210 * Fix it, but check if it's kernel or user first..
214 up_read(&mm
->mmap_sem
);
216 bad_area_nosemaphore
:
217 DPG(show_registers(regs
));
219 /* User mode accesses just cause a SIGSEGV */
221 if (user_mode(regs
)) {
222 #ifdef CONFIG_NO_SEGFAULT_TERMINATION
223 DECLARE_WAIT_QUEUE_HEAD(wq
);
225 printk(KERN_NOTICE
"%s (pid %d) segfaults for page "
226 "address %08lx at pc %08lx\n",
228 address
, instruction_pointer(regs
));
230 /* With DPG on, we've already dumped registers above. */
232 show_registers(regs
);
234 #ifdef CONFIG_NO_SEGFAULT_TERMINATION
235 wait_event_interruptible(wq
, 0 == 1);
237 info
.si_signo
= SIGSEGV
;
239 /* info.si_code has been set above */
240 info
.si_addr
= (void *)address
;
241 force_sig_info(SIGSEGV
, &info
, tsk
);
248 /* Are we prepared to handle this kernel fault?
250 * (The kernel has valid exception-points in the source
251 * when it accesses user-memory. When it fails in one
252 * of those points, we find it in a table and do a jump
253 * to some fixup code that loads an appropriate error
257 if (find_fixup_code(regs
))
261 * Oops. The kernel tried to access some bad page. We'll have to
262 * terminate things with extreme prejudice.
265 if (!oops_in_progress
) {
266 oops_in_progress
= 1;
267 if ((unsigned long) (address
) < PAGE_SIZE
)
268 printk(KERN_ALERT
"Unable to handle kernel NULL "
269 "pointer dereference");
271 printk(KERN_ALERT
"Unable to handle kernel access"
272 " at virtual address %08lx\n", address
);
274 die_if_kernel("Oops", regs
, (writeaccess
<< 1) | protection
);
275 oops_in_progress
= 0;
281 * We ran out of memory, or some other thing happened to us that made
282 * us unable to handle the page fault gracefully.
286 up_read(&mm
->mmap_sem
);
287 if (!user_mode(regs
))
289 pagefault_out_of_memory();
293 up_read(&mm
->mmap_sem
);
296 * Send a sigbus, regardless of whether we were in kernel
299 info
.si_signo
= SIGBUS
;
301 info
.si_code
= BUS_ADRERR
;
302 info
.si_addr
= (void *)address
;
303 force_sig_info(SIGBUS
, &info
, tsk
);
305 /* Kernel mode? Handle exceptions or die */
306 if (!user_mode(regs
))
313 * Synchronize this task's top level page-table
314 * with the 'reference' page table.
316 * Use current_pgd instead of tsk->active_mm->pgd
317 * since the latter might be unavailable if this
318 * code is executed in a misfortunately run irq
319 * (like inside schedule() between switch_mm and
323 int offset
= pgd_index(address
);
329 pgd
= (pgd_t
*)per_cpu(current_pgd
, smp_processor_id()) + offset
;
330 pgd_k
= init_mm
.pgd
+ offset
;
332 /* Since we're two-level, we don't need to do both
333 * set_pgd and set_pmd (they do the same thing). If
334 * we go three-level at some point, do the right thing
335 * with pgd_present and set_pgd here.
337 * Also, since the vmalloc area is global, we don't
338 * need to copy individual PTE's, it is enough to
339 * copy the pgd pointer into the pte page of the
340 * root task. If that is there, we'll find our pte if
344 pud
= pud_offset(pgd
, address
);
345 pud_k
= pud_offset(pgd_k
, address
);
346 if (!pud_present(*pud_k
))
349 pmd
= pmd_offset(pud
, address
);
350 pmd_k
= pmd_offset(pud_k
, address
);
352 if (!pmd_present(*pmd_k
))
353 goto bad_area_nosemaphore
;
355 set_pmd(pmd
, *pmd_k
);
357 /* Make sure the actual PTE exists as well to
358 * catch kernel vmalloc-area accesses to non-mapped
359 * addresses. If we don't do this, this will just
360 * silently loop forever.
363 pte_k
= pte_offset_kernel(pmd_k
, address
);
364 if (!pte_present(*pte_k
))
371 /* Find fixup code. */
373 find_fixup_code(struct pt_regs
*regs
)
375 const struct exception_table_entry
*fixup
;
376 /* in case of delay slot fault (v32) */
377 unsigned long ip
= (instruction_pointer(regs
) & ~0x1);
379 fixup
= search_exception_tables(ip
);
381 /* Adjust the instruction pointer in the stackframe. */
382 instruction_pointer(regs
) = fixup
->fixup
;