2 * Copyright (C) 2009 Wind River Systems Inc
3 * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
5 * based on arch/mips/mm/fault.c which is:
7 * Copyright (C) 1995-2000 Ralf Baechle
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/types.h>
21 #include <linux/ptrace.h>
22 #include <linux/mman.h>
24 #include <linux/module.h>
25 #include <linux/uaccess.h>
26 #include <linux/ptrace.h>
28 #include <asm/mmu_context.h>
29 #include <asm/traps.h>
31 #define EXC_SUPERV_INSN_ACCESS 9 /* Supervisor only instruction address */
32 #define EXC_SUPERV_DATA_ACCESS 11 /* Supervisor only data address */
33 #define EXC_X_PROTECTION_FAULT 13 /* TLB permission violation (x) */
34 #define EXC_R_PROTECTION_FAULT 14 /* TLB permission violation (r) */
35 #define EXC_W_PROTECTION_FAULT 15 /* TLB permission violation (w) */
38 * This routine handles page faults. It determines the address,
39 * and the problem, and then passes it off to one of the appropriate
42 asmlinkage
void do_page_fault(struct pt_regs
*regs
, unsigned long cause
,
43 unsigned long address
)
45 struct vm_area_struct
*vma
= NULL
;
46 struct task_struct
*tsk
= current
;
47 struct mm_struct
*mm
= tsk
->mm
;
48 int code
= SEGV_MAPERR
;
50 unsigned int flags
= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
;
54 /* Restart the instruction */
58 * We fault-in kernel-space virtual memory on-demand. The
59 * 'reference' page table is init_mm.pgd.
61 * NOTE! We MUST NOT take any locks for this case. We may
62 * be in an interrupt or a critical region, and should
63 * only copy the information from the master page table,
66 if (unlikely(address
>= VMALLOC_START
&& address
<= VMALLOC_END
)) {
68 goto bad_area_nosemaphore
;
73 if (unlikely(address
>= TASK_SIZE
))
74 goto bad_area_nosemaphore
;
77 * If we're in an interrupt or have no user
78 * context, we must not take the fault..
80 if (faulthandler_disabled() || !mm
)
81 goto bad_area_nosemaphore
;
84 flags
|= FAULT_FLAG_USER
;
86 if (!down_read_trylock(&mm
->mmap_sem
)) {
87 if (!user_mode(regs
) && !search_exception_tables(regs
->ea
))
88 goto bad_area_nosemaphore
;
90 down_read(&mm
->mmap_sem
);
93 vma
= find_vma(mm
, address
);
96 if (vma
->vm_start
<= address
)
98 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
100 if (expand_stack(vma
, address
))
103 * Ok, we have a good vm_area for this memory access, so
110 case EXC_SUPERV_INSN_ACCESS
:
112 case EXC_SUPERV_DATA_ACCESS
:
114 case EXC_X_PROTECTION_FAULT
:
115 if (!(vma
->vm_flags
& VM_EXEC
))
118 case EXC_R_PROTECTION_FAULT
:
119 if (!(vma
->vm_flags
& VM_READ
))
122 case EXC_W_PROTECTION_FAULT
:
123 if (!(vma
->vm_flags
& VM_WRITE
))
125 flags
= FAULT_FLAG_WRITE
;
130 * If for any reason at all we couldn't handle the fault,
131 * make sure we exit gracefully rather than endlessly redo
134 fault
= handle_mm_fault(mm
, vma
, address
, flags
);
136 if ((fault
& VM_FAULT_RETRY
) && fatal_signal_pending(current
))
139 if (unlikely(fault
& VM_FAULT_ERROR
)) {
140 if (fault
& VM_FAULT_OOM
)
142 else if (fault
& VM_FAULT_SIGSEGV
)
144 else if (fault
& VM_FAULT_SIGBUS
)
150 * Major/minor page fault accounting is only done on the
151 * initial attempt. If we go through a retry, it is extremely
152 * likely that the page will be found in page cache at that point.
154 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
155 if (fault
& VM_FAULT_MAJOR
)
159 if (fault
& VM_FAULT_RETRY
) {
160 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
162 flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
163 flags
|= FAULT_FLAG_TRIED
;
166 * No need to up_read(&mm->mmap_sem) as we would
167 * have already released it in __lock_page_or_retry
175 up_read(&mm
->mmap_sem
);
179 * Something tried to access memory that isn't in our memory map..
180 * Fix it, but check if it's kernel or user first..
183 up_read(&mm
->mmap_sem
);
185 bad_area_nosemaphore
:
186 /* User mode accesses just cause a SIGSEGV */
187 if (user_mode(regs
)) {
188 if (unhandled_signal(current
, SIGSEGV
) && printk_ratelimit()) {
189 pr_info("%s: unhandled page fault (%d) at 0x%08lx, "
190 "cause %ld\n", current
->comm
, SIGSEGV
, address
, cause
);
193 _exception(SIGSEGV
, regs
, code
, address
);
198 /* Are we prepared to handle this kernel fault? */
199 if (fixup_exception(regs
))
203 * Oops. The kernel tried to access some bad page. We'll have to
204 * terminate things with extreme prejudice.
208 pr_alert("Unable to handle kernel %s at virtual address %08lx",
209 address
< PAGE_SIZE
? "NULL pointer dereference" :
210 "paging request", address
);
211 pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs
->ea
, regs
->ra
,
217 * We ran out of memory, or some other thing happened to us that made
218 * us unable to handle the page fault gracefully.
221 up_read(&mm
->mmap_sem
);
222 if (!user_mode(regs
))
224 pagefault_out_of_memory();
228 up_read(&mm
->mmap_sem
);
230 /* Kernel mode? Handle exceptions or die */
231 if (!user_mode(regs
))
234 _exception(SIGBUS
, regs
, BUS_ADRERR
, address
);
240 * Synchronize this task's top level page-table
241 * with the 'reference' page table.
243 * Do _not_ use "tsk" here. We might be inside
244 * an interrupt in the middle of a task switch..
246 int offset
= pgd_index(address
);
252 pgd
= pgd_current
+ offset
;
253 pgd_k
= init_mm
.pgd
+ offset
;
255 if (!pgd_present(*pgd_k
))
257 set_pgd(pgd
, *pgd_k
);
259 pud
= pud_offset(pgd
, address
);
260 pud_k
= pud_offset(pgd_k
, address
);
261 if (!pud_present(*pud_k
))
263 pmd
= pmd_offset(pud
, address
);
264 pmd_k
= pmd_offset(pud_k
, address
);
265 if (!pmd_present(*pmd_k
))
267 set_pmd(pmd
, *pmd_k
);
269 pte_k
= pte_offset_kernel(pmd_k
, address
);
270 if (!pte_present(*pte_k
))
273 flush_tlb_one(address
);