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/marker.h>
19 #include <asm/io_trapped.h>
20 #include <asm/system.h>
21 #include <asm/mmu_context.h>
22 #include <asm/tlbflush.h>
25 * This routine handles page faults. It determines the address,
26 * and the problem, and then passes it off to one of the appropriate
29 asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
30 unsigned long writeaccess,
31 unsigned long address)
33 struct task_struct *tsk;
35 struct vm_area_struct * vma;
41 * We don't bother with any notifier callbacks here, as they are
42 * all handled through the __do_page_fault() fast-path.
46 si_code = SEGV_MAPERR;
48 if (unlikely(address >= TASK_SIZE)) {
50 * Synchronize this task's top level page-table
51 * with the 'reference' page table.
53 * Do _not_ use "tsk" here. We might be inside
54 * an interrupt in the middle of a task switch..
56 int offset = pgd_index(address);
61 pgd = get_TTB() + offset;
62 pgd_k = swapper_pg_dir + offset;
64 if (!pgd_present(*pgd)) {
65 if (!pgd_present(*pgd_k))
66 goto bad_area_nosemaphore;
71 pud = pud_offset(pgd, address);
72 pud_k = pud_offset(pgd_k, address);
74 if (!pud_present(*pud)) {
75 if (!pud_present(*pud_k))
76 goto bad_area_nosemaphore;
81 pmd = pmd_offset(pud, address);
82 pmd_k = pmd_offset(pud_k, address);
83 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
84 goto bad_area_nosemaphore;
90 /* Only enable interrupts if they were on before the fault */
91 if ((regs->sr & SR_IMASK) != SR_IMASK) {
99 * If we're in an interrupt or have no user
100 * context, we must not take the fault..
102 if (in_atomic() || !mm)
105 down_read(&mm->mmap_sem);
107 vma = find_vma(mm, address);
110 if (vma->vm_start <= address)
112 if (!(vma->vm_flags & VM_GROWSDOWN))
114 if (expand_stack(vma, address))
117 * Ok, we have a good vm_area for this memory access, so
121 si_code = SEGV_ACCERR;
123 if (!(vma->vm_flags & VM_WRITE))
126 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
131 * If for any reason at all we couldn't handle the fault,
132 * make sure we exit gracefully rather than endlessly redo
136 fault = handle_mm_fault(mm, vma, address, writeaccess);
137 if (unlikely(fault & VM_FAULT_ERROR)) {
138 if (fault & VM_FAULT_OOM)
140 else if (fault & VM_FAULT_SIGBUS)
144 if (fault & VM_FAULT_MAJOR)
149 up_read(&mm->mmap_sem);
153 * Something tried to access memory that isn't in our memory map..
154 * Fix it, but check if it's kernel or user first..
157 up_read(&mm->mmap_sem);
159 bad_area_nosemaphore:
160 if (user_mode(regs)) {
161 info.si_signo = SIGSEGV;
163 info.si_code = si_code;
164 info.si_addr = (void *) address;
165 force_sig_info(SIGSEGV, &info, tsk);
170 /* Are we prepared to handle this kernel fault? */
171 if (fixup_exception(regs))
174 if (handle_trapped_io(regs, address))
177 * Oops. The kernel tried to access some bad page. We'll have to
178 * terminate things with extreme prejudice.
184 if (oops_may_print()) {
187 if (address < PAGE_SIZE)
188 printk(KERN_ALERT "Unable to handle kernel NULL "
189 "pointer dereference");
191 printk(KERN_ALERT "Unable to handle kernel paging "
193 printk(" at virtual address %08lx\n", address);
194 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
195 page = (unsigned long)get_TTB();
197 page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT];
198 printk(KERN_ALERT "*pde = %08lx\n", page);
199 if (page & _PAGE_PRESENT) {
201 address &= 0x003ff000;
202 page = ((__typeof__(page) *)
203 __va(page))[address >>
205 printk(KERN_ALERT "*pte = %08lx\n", page);
210 die("Oops", regs, writeaccess);
215 * We ran out of memory, or some other thing happened to us that made
216 * us unable to handle the page fault gracefully.
219 up_read(&mm->mmap_sem);
220 if (is_global_init(current)) {
222 down_read(&mm->mmap_sem);
225 printk("VM: killing process %s\n", tsk->comm);
227 do_group_exit(SIGKILL);
231 up_read(&mm->mmap_sem);
234 * Send a sigbus, regardless of whether we were in kernel
237 info.si_signo = SIGBUS;
239 info.si_code = BUS_ADRERR;
240 info.si_addr = (void *)address;
241 force_sig_info(SIGBUS, &info, tsk);
243 /* Kernel mode? Handle exceptions or die */
244 if (!user_mode(regs))
248 static inline int notify_page_fault(struct pt_regs *regs, int trap)
252 trace_mark(kernel_arch_trap_entry, "trap_id %d ip #p%ld",
253 trap >> 5, instruction_pointer(regs));
255 #ifdef CONFIG_KPROBES
256 if (!user_mode(regs)) {
258 if (kprobe_running() && kprobe_fault_handler(regs, trap))
268 * Called with interrupts disabled.
270 asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs,
271 unsigned long writeaccess,
272 unsigned long address)
281 if (notify_page_fault(regs, lookup_exception_vector()))
287 * We don't take page faults for P1, P2, and parts of P4, these
288 * are always mapped, whether it be due to legacy behaviour in
289 * 29-bit mode, or due to PMB configuration in 32-bit mode.
291 if (address >= P3SEG && address < P3_ADDR_MAX) {
292 pgd = pgd_offset_k(address);
294 if (unlikely(address >= TASK_SIZE || !current->mm))
297 pgd = pgd_offset(current->mm, address);
300 pud = pud_offset(pgd, address);
301 if (pud_none_or_clear_bad(pud))
303 pmd = pmd_offset(pud, address);
304 if (pmd_none_or_clear_bad(pmd))
306 pte = pte_offset_kernel(pmd, address);
308 if (unlikely(pte_none(entry) || pte_not_present(entry)))
310 if (unlikely(writeaccess && !pte_write(entry)))
314 entry = pte_mkdirty(entry);
315 entry = pte_mkyoung(entry);
317 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
319 * ITLB is not affected by "ldtlb" instruction.
320 * So, we need to flush the entry by ourselves.
322 local_flush_tlb_one(get_asid(), address & PAGE_MASK);
326 update_mmu_cache(NULL, address, entry);
330 trace_mark(kernel_arch_trap_exit, MARK_NOARGS);