2 * Page fault handler for SH with an MMU.
4 * Copyright (C) 1999 Niibe Yutaka
5 * Copyright (C) 2003 - 2008 Paul Mundt
7 * Based on linux/arch/i386/mm/fault.c:
8 * Copyright (C) 1995 Linus Torvalds
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file "COPYING" in the main directory of this archive
14 #include <linux/kernel.h>
16 #include <linux/hardirq.h>
17 #include <linux/kprobes.h>
18 #include <linux/perf_counter.h>
19 #include <asm/io_trapped.h>
20 #include <asm/system.h>
21 #include <asm/mmu_context.h>
22 #include <asm/tlbflush.h>
24 static inline int notify_page_fault(struct pt_regs
*regs
, int trap
)
28 if (kprobes_built_in() && !user_mode(regs
)) {
30 if (kprobe_running() && kprobe_fault_handler(regs
, trap
))
39 * This routine handles page faults. It determines the address,
40 * and the problem, and then passes it off to one of the appropriate
43 asmlinkage
void __kprobes
do_page_fault(struct pt_regs
*regs
,
44 unsigned long writeaccess
,
45 unsigned long address
)
47 struct task_struct
*tsk
;
49 struct vm_area_struct
* vma
;
55 * We don't bother with any notifier callbacks here, as they are
56 * all handled through the __do_page_fault() fast-path.
60 si_code
= SEGV_MAPERR
;
62 if (unlikely(address
>= TASK_SIZE
)) {
64 * Synchronize this task's top level page-table
65 * with the 'reference' page table.
67 * Do _not_ use "tsk" here. We might be inside
68 * an interrupt in the middle of a task switch..
70 int offset
= pgd_index(address
);
75 pgd
= get_TTB() + offset
;
76 pgd_k
= swapper_pg_dir
+ offset
;
78 if (!pgd_present(*pgd
)) {
79 if (!pgd_present(*pgd_k
))
80 goto bad_area_nosemaphore
;
85 pud
= pud_offset(pgd
, address
);
86 pud_k
= pud_offset(pgd_k
, address
);
88 if (!pud_present(*pud
)) {
89 if (!pud_present(*pud_k
))
90 goto bad_area_nosemaphore
;
95 pmd
= pmd_offset(pud
, address
);
96 pmd_k
= pmd_offset(pud_k
, address
);
97 if (pmd_present(*pmd
) || !pmd_present(*pmd_k
))
98 goto bad_area_nosemaphore
;
106 if (unlikely(notify_page_fault(regs
, lookup_exception_vector())))
109 /* Only enable interrupts if they were on before the fault */
110 if ((regs
->sr
& SR_IMASK
) != SR_IMASK
)
113 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, 0, regs
, address
);
116 * If we're in an interrupt or have no user
117 * context, we must not take the fault..
119 if (in_atomic() || !mm
)
122 down_read(&mm
->mmap_sem
);
124 vma
= find_vma(mm
, address
);
127 if (vma
->vm_start
<= address
)
129 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
131 if (expand_stack(vma
, address
))
134 * Ok, we have a good vm_area for this memory access, so
138 si_code
= SEGV_ACCERR
;
140 if (!(vma
->vm_flags
& VM_WRITE
))
143 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)))
148 * If for any reason at all we couldn't handle the fault,
149 * make sure we exit gracefully rather than endlessly redo
153 fault
= handle_mm_fault(mm
, vma
, address
, writeaccess
? FAULT_FLAG_WRITE
: 0);
154 if (unlikely(fault
& VM_FAULT_ERROR
)) {
155 if (fault
& VM_FAULT_OOM
)
157 else if (fault
& VM_FAULT_SIGBUS
)
161 if (fault
& VM_FAULT_MAJOR
) {
163 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
, 1, 0,
167 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
, 1, 0,
171 up_read(&mm
->mmap_sem
);
175 * Something tried to access memory that isn't in our memory map..
176 * Fix it, but check if it's kernel or user first..
179 up_read(&mm
->mmap_sem
);
181 bad_area_nosemaphore
:
182 if (user_mode(regs
)) {
183 info
.si_signo
= SIGSEGV
;
185 info
.si_code
= si_code
;
186 info
.si_addr
= (void *) address
;
187 force_sig_info(SIGSEGV
, &info
, tsk
);
192 /* Are we prepared to handle this kernel fault? */
193 if (fixup_exception(regs
))
196 if (handle_trapped_io(regs
, address
))
199 * Oops. The kernel tried to access some bad page. We'll have to
200 * terminate things with extreme prejudice.
206 if (oops_may_print()) {
209 if (address
< PAGE_SIZE
)
210 printk(KERN_ALERT
"Unable to handle kernel NULL "
211 "pointer dereference");
213 printk(KERN_ALERT
"Unable to handle kernel paging "
215 printk(" at virtual address %08lx\n", address
);
216 printk(KERN_ALERT
"pc = %08lx\n", regs
->pc
);
217 page
= (unsigned long)get_TTB();
219 page
= ((__typeof__(page
) *)page
)[address
>> PGDIR_SHIFT
];
220 printk(KERN_ALERT
"*pde = %08lx\n", page
);
221 if (page
& _PAGE_PRESENT
) {
223 address
&= 0x003ff000;
224 page
= ((__typeof__(page
) *)
225 __va(page
))[address
>>
227 printk(KERN_ALERT
"*pte = %08lx\n", page
);
232 die("Oops", regs
, writeaccess
);
237 * We ran out of memory, or some other thing happened to us that made
238 * us unable to handle the page fault gracefully.
241 up_read(&mm
->mmap_sem
);
242 if (is_global_init(current
)) {
244 down_read(&mm
->mmap_sem
);
247 printk("VM: killing process %s\n", tsk
->comm
);
249 do_group_exit(SIGKILL
);
253 up_read(&mm
->mmap_sem
);
256 * Send a sigbus, regardless of whether we were in kernel
259 info
.si_signo
= SIGBUS
;
261 info
.si_code
= BUS_ADRERR
;
262 info
.si_addr
= (void *)address
;
263 force_sig_info(SIGBUS
, &info
, tsk
);
265 /* Kernel mode? Handle exceptions or die */
266 if (!user_mode(regs
))
271 * Called with interrupts disabled.
273 asmlinkage
int __kprobes
__do_page_fault(struct pt_regs
*regs
,
274 unsigned long writeaccess
,
275 unsigned long address
)
285 * We don't take page faults for P1, P2, and parts of P4, these
286 * are always mapped, whether it be due to legacy behaviour in
287 * 29-bit mode, or due to PMB configuration in 32-bit mode.
289 if (address
>= P3SEG
&& address
< P3_ADDR_MAX
) {
290 pgd
= pgd_offset_k(address
);
292 if (unlikely(address
>= TASK_SIZE
|| !current
->mm
))
295 pgd
= pgd_offset(current
->mm
, address
);
298 pud
= pud_offset(pgd
, address
);
299 if (pud_none_or_clear_bad(pud
))
301 pmd
= pmd_offset(pud
, address
);
302 if (pmd_none_or_clear_bad(pmd
))
304 pte
= pte_offset_kernel(pmd
, address
);
306 if (unlikely(pte_none(entry
) || pte_not_present(entry
)))
308 if (unlikely(writeaccess
&& !pte_write(entry
)))
312 entry
= pte_mkdirty(entry
);
313 entry
= pte_mkyoung(entry
);
315 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
317 * ITLB is not affected by "ldtlb" instruction.
318 * So, we need to flush the entry by ourselves.
320 local_flush_tlb_one(get_asid(), address
& PAGE_MASK
);
324 update_mmu_cache(NULL
, address
, entry
);