2 * linux/arch/cris/mm/fault.c
4 * Copyright (C) 2000-2006 Axis Communications AB
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <asm/uaccess.h>
15 extern int find_fixup_code(struct pt_regs
*);
16 extern void die_if_kernel(const char *, struct pt_regs
*, long);
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
;
63 "Page fault for %lX on %X at %lX, prot %d write %d\n",
64 address
, smp_processor_id(), instruction_pointer(regs
),
65 protection
, writeaccess
));
70 * We fault-in kernel-space virtual memory on-demand. The
71 * 'reference' page table is init_mm.pgd.
73 * NOTE! We MUST NOT take any locks for this case. We may
74 * be in an interrupt or a critical region, and should
75 * only copy the information from the master page table,
78 * NOTE2: This is done so that, when updating the vmalloc
79 * mappings we don't have to walk all processes pgdirs and
80 * add the high mappings all at once. Instead we do it as they
81 * are used. However vmalloc'ed page entries have the PAGE_GLOBAL
82 * bit set so sometimes the TLB can use a lingering entry.
84 * This verifies that the fault happens in kernel space
85 * and that the fault was not a protection error (error_code & 1).
88 if (address
>= VMALLOC_START
&&
93 /* When stack execution is not allowed we store the signal
94 * trampolines in the reserved cris_signal_return_page.
95 * Handle this in the exact same way as vmalloc (we know
96 * that the mapping is there and is valid so no need to
97 * call handle_mm_fault).
99 if (cris_signal_return_page
&&
100 address
== cris_signal_return_page
&&
101 !protection
&& user_mode(regs
))
104 /* we can and should enable interrupts at this point */
108 info
.si_code
= SEGV_MAPERR
;
111 * If we're in an interrupt or have no user
112 * context, we must not take the fault..
115 if (in_interrupt() || !mm
)
118 down_read(&mm
->mmap_sem
);
119 vma
= find_vma(mm
, address
);
122 if (vma
->vm_start
<= address
)
124 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
126 if (user_mode(regs
)) {
128 * accessing the stack below usp is always a bug.
129 * we get page-aligned addresses so we can only check
130 * if we're within a page from usp, but that might be
131 * enough to catch brutal errors at least.
133 if (address
+ PAGE_SIZE
< rdusp())
136 if (expand_stack(vma
, address
))
140 * Ok, we have a good vm_area for this memory access, so
145 info
.si_code
= SEGV_ACCERR
;
147 /* first do some preliminary protection checks */
149 if (writeaccess
== 2){
150 if (!(vma
->vm_flags
& VM_EXEC
))
152 } else if (writeaccess
== 1) {
153 if (!(vma
->vm_flags
& VM_WRITE
))
156 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
161 * If for any reason at all we couldn't handle the fault,
162 * make sure we exit gracefully rather than endlessly redo
166 fault
= handle_mm_fault(mm
, vma
, address
, (writeaccess
& 1) ? FAULT_FLAG_WRITE
: 0);
167 if (unlikely(fault
& VM_FAULT_ERROR
)) {
168 if (fault
& VM_FAULT_OOM
)
170 else if (fault
& VM_FAULT_SIGBUS
)
174 if (fault
& VM_FAULT_MAJOR
)
179 up_read(&mm
->mmap_sem
);
183 * Something tried to access memory that isn't in our memory map..
184 * Fix it, but check if it's kernel or user first..
188 up_read(&mm
->mmap_sem
);
190 bad_area_nosemaphore
:
191 DPG(show_registers(regs
));
193 /* User mode accesses just cause a SIGSEGV */
195 if (user_mode(regs
)) {
196 info
.si_signo
= SIGSEGV
;
198 /* info.si_code has been set above */
199 info
.si_addr
= (void *)address
;
200 force_sig_info(SIGSEGV
, &info
, tsk
);
201 printk(KERN_NOTICE
"%s (pid %d) segfaults for page "
202 "address %08lx at pc %08lx\n",
203 tsk
->comm
, tsk
->pid
, address
, instruction_pointer(regs
));
209 /* Are we prepared to handle this kernel fault?
211 * (The kernel has valid exception-points in the source
212 * when it accesses user-memory. When it fails in one
213 * of those points, we find it in a table and do a jump
214 * to some fixup code that loads an appropriate error
218 if (find_fixup_code(regs
))
222 * Oops. The kernel tried to access some bad page. We'll have to
223 * terminate things with extreme prejudice.
226 if (!oops_in_progress
) {
227 oops_in_progress
= 1;
228 if ((unsigned long) (address
) < PAGE_SIZE
)
229 printk(KERN_ALERT
"Unable to handle kernel NULL "
230 "pointer dereference");
232 printk(KERN_ALERT
"Unable to handle kernel access"
233 " at virtual address %08lx\n", address
);
235 die_if_kernel("Oops", regs
, (writeaccess
<< 1) | protection
);
236 oops_in_progress
= 0;
242 * We ran out of memory, or some other thing happened to us that made
243 * us unable to handle the page fault gracefully.
247 up_read(&mm
->mmap_sem
);
248 printk("VM: killing process %s\n", tsk
->comm
);
254 up_read(&mm
->mmap_sem
);
257 * Send a sigbus, regardless of whether we were in kernel
260 info
.si_signo
= SIGBUS
;
262 info
.si_code
= BUS_ADRERR
;
263 info
.si_addr
= (void *)address
;
264 force_sig_info(SIGBUS
, &info
, tsk
);
266 /* Kernel mode? Handle exceptions or die */
267 if (!user_mode(regs
))
274 * Synchronize this task's top level page-table
275 * with the 'reference' page table.
277 * Use current_pgd instead of tsk->active_mm->pgd
278 * since the latter might be unavailable if this
279 * code is executed in a misfortunately run irq
280 * (like inside schedule() between switch_mm and
284 int offset
= pgd_index(address
);
290 pgd
= (pgd_t
*)per_cpu(current_pgd
, smp_processor_id()) + offset
;
291 pgd_k
= init_mm
.pgd
+ offset
;
293 /* Since we're two-level, we don't need to do both
294 * set_pgd and set_pmd (they do the same thing). If
295 * we go three-level at some point, do the right thing
296 * with pgd_present and set_pgd here.
298 * Also, since the vmalloc area is global, we don't
299 * need to copy individual PTE's, it is enough to
300 * copy the pgd pointer into the pte page of the
301 * root task. If that is there, we'll find our pte if
305 pud
= pud_offset(pgd
, address
);
306 pud_k
= pud_offset(pgd_k
, address
);
307 if (!pud_present(*pud_k
))
310 pmd
= pmd_offset(pud
, address
);
311 pmd_k
= pmd_offset(pud_k
, address
);
313 if (!pmd_present(*pmd_k
))
314 goto bad_area_nosemaphore
;
316 set_pmd(pmd
, *pmd_k
);
318 /* Make sure the actual PTE exists as well to
319 * catch kernel vmalloc-area accesses to non-mapped
320 * addresses. If we don't do this, this will just
321 * silently loop forever.
324 pte_k
= pte_offset_kernel(pmd_k
, address
);
325 if (!pte_present(*pte_k
))
332 /* Find fixup code. */
334 find_fixup_code(struct pt_regs
*regs
)
336 const struct exception_table_entry
*fixup
;
338 if ((fixup
= search_exception_tables(instruction_pointer(regs
))) != 0) {
339 /* Adjust the instruction pointer in the stackframe. */
340 instruction_pointer(regs
) = fixup
->fixup
;