2 * Copyright (C) 2004-2006 Atmel Corporation
4 * Based on linux/arch/sh/mm/fault.c:
5 * Copyright (C) 1999 Niibe Yutaka
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/pagemap.h>
15 #include <linux/kdebug.h>
16 #include <linux/kprobes.h>
17 #include <linux/uaccess.h>
19 #include <asm/mmu_context.h>
20 #include <asm/sysreg.h>
24 static inline int notify_page_fault(struct pt_regs
*regs
, int trap
)
28 if (!user_mode(regs
)) {
29 if (kprobe_running() && kprobe_fault_handler(regs
, trap
))
36 static inline int notify_page_fault(struct pt_regs
*regs
, int trap
)
42 int exception_trace
= 1;
45 * This routine handles page faults. It determines the address and the
46 * problem, and then passes it off to one of the appropriate routines.
48 * ecr is the Exception Cause Register. Possible values are:
49 * 6: Protection fault (instruction access)
50 * 15: Protection fault (read access)
51 * 16: Protection fault (write access)
52 * 20: Page not found (instruction access)
53 * 24: Page not found (read access)
54 * 28: Page not found (write access)
56 asmlinkage
void do_page_fault(unsigned long ecr
, struct pt_regs
*regs
)
58 struct task_struct
*tsk
;
60 struct vm_area_struct
*vma
;
61 const struct exception_table_entry
*fixup
;
62 unsigned long address
;
67 unsigned int flags
= FAULT_FLAG_ALLOW_RETRY
| FAULT_FLAG_KILLABLE
;
69 if (notify_page_fault(regs
, ecr
))
72 address
= sysreg_read(TLBEAR
);
81 * If we're in an interrupt or have no user context, we must
82 * not take the fault...
84 if (faulthandler_disabled() || !mm
|| regs
->sr
& SYSREG_BIT(GM
))
90 flags
|= FAULT_FLAG_USER
;
92 down_read(&mm
->mmap_sem
);
94 vma
= find_vma(mm
, address
);
97 if (vma
->vm_start
<= address
)
99 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
101 if (expand_stack(vma
, address
))
105 * Ok, we have a good vm_area for this memory access, so we
112 case ECR_PROTECTION_X
:
114 if (!(vma
->vm_flags
& VM_EXEC
))
117 case ECR_PROTECTION_R
:
119 if (!(vma
->vm_flags
& (VM_READ
| VM_WRITE
| VM_EXEC
)))
122 case ECR_PROTECTION_W
:
124 if (!(vma
->vm_flags
& VM_WRITE
))
126 flags
|= FAULT_FLAG_WRITE
;
129 panic("Unhandled case %lu in do_page_fault!", ecr
);
133 * If for any reason at all we couldn't handle the fault, make
134 * sure we exit gracefully rather than endlessly redo the
137 fault
= handle_mm_fault(mm
, vma
, address
, flags
);
139 if ((fault
& VM_FAULT_RETRY
) && fatal_signal_pending(current
))
142 if (unlikely(fault
& VM_FAULT_ERROR
)) {
143 if (fault
& VM_FAULT_OOM
)
145 else if (fault
& VM_FAULT_SIGSEGV
)
147 else if (fault
& VM_FAULT_SIGBUS
)
152 if (flags
& FAULT_FLAG_ALLOW_RETRY
) {
153 if (fault
& VM_FAULT_MAJOR
)
157 if (fault
& VM_FAULT_RETRY
) {
158 flags
&= ~FAULT_FLAG_ALLOW_RETRY
;
159 flags
|= FAULT_FLAG_TRIED
;
162 * No need to up_read(&mm->mmap_sem) as we would have
163 * already released it in __lock_page_or_retry() in
170 up_read(&mm
->mmap_sem
);
174 * Something tried to access memory that isn't in our memory
175 * map. Fix it, but check if it's kernel or user first...
178 up_read(&mm
->mmap_sem
);
180 if (user_mode(regs
)) {
181 if (exception_trace
&& printk_ratelimit())
182 printk("%s%s[%d]: segfault at %08lx pc %08lx "
183 "sp %08lx ecr %lu\n",
184 is_global_init(tsk
) ? KERN_EMERG
: KERN_INFO
,
185 tsk
->comm
, tsk
->pid
, address
, regs
->pc
,
187 _exception(SIGSEGV
, regs
, code
, address
);
192 /* Are we prepared to handle this kernel fault? */
193 fixup
= search_exception_tables(regs
->pc
);
195 regs
->pc
= fixup
->fixup
;
200 * Oops. The kernel tried to access some bad page. We'll have
201 * to terminate things with extreme prejudice.
203 if (address
< PAGE_SIZE
)
205 "Unable to handle kernel NULL pointer dereference");
208 "Unable to handle kernel paging request");
209 printk(" at virtual address %08lx\n", address
);
211 page
= sysreg_read(PTBR
);
212 printk(KERN_ALERT
"ptbr = %08lx", page
);
213 if (address
>= TASK_SIZE
)
214 page
= (unsigned long)swapper_pg_dir
;
216 page
= ((unsigned long *)page
)[address
>> 22];
217 printk(" pgd = %08lx", page
);
218 if (page
& _PAGE_PRESENT
) {
220 address
&= 0x003ff000;
221 page
= ((unsigned long *)__va(page
))[address
>> PAGE_SHIFT
];
222 printk(" pte = %08lx", page
);
226 die("Kernel access of bad area", regs
, signr
);
230 * We ran out of memory, or some other thing happened to us
231 * that made us unable to handle the page fault gracefully.
234 up_read(&mm
->mmap_sem
);
235 if (!user_mode(regs
))
237 pagefault_out_of_memory();
241 up_read(&mm
->mmap_sem
);
243 /* Kernel mode? Handle exceptions or die */
246 if (!user_mode(regs
))
250 printk("%s%s[%d]: bus error at %08lx pc %08lx "
251 "sp %08lx ecr %lu\n",
252 is_global_init(tsk
) ? KERN_EMERG
: KERN_INFO
,
253 tsk
->comm
, tsk
->pid
, address
, regs
->pc
,
256 _exception(SIGBUS
, regs
, BUS_ADRERR
, address
);
259 asmlinkage
void do_bus_error(unsigned long addr
, int write_access
,
260 struct pt_regs
*regs
)
263 "Bus error at physical address 0x%08lx (%s access)\n",
264 addr
, write_access
? "write" : "read");
265 printk(KERN_INFO
"DTLB dump:\n");
267 die("Bus Error", regs
, SIGKILL
);