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>
26 * This routine handles page faults. It determines the address,
27 * and the problem, and then passes it off to one of the appropriate
30 asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
31 unsigned long writeaccess,
32 unsigned long address)
34 struct task_struct *tsk;
36 struct vm_area_struct * vma;
42 * We don't bother with any notifier callbacks here, as they are
43 * all handled through the __do_page_fault() fast-path.
47 si_code = SEGV_MAPERR;
49 if (unlikely(address >= TASK_SIZE)) {
51 * Synchronize this task's top level page-table
52 * with the 'reference' page table.
54 * Do _not_ use "tsk" here. We might be inside
55 * an interrupt in the middle of a task switch..
57 int offset = pgd_index(address);
62 pgd = get_TTB() + offset;
63 pgd_k = swapper_pg_dir + offset;
65 if (!pgd_present(*pgd)) {
66 if (!pgd_present(*pgd_k))
67 goto bad_area_nosemaphore;
72 pud = pud_offset(pgd, address);
73 pud_k = pud_offset(pgd_k, address);
75 if (!pud_present(*pud)) {
76 if (!pud_present(*pud_k))
77 goto bad_area_nosemaphore;
82 pmd = pmd_offset(pud, address);
83 pmd_k = pmd_offset(pud_k, address);
84 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
85 goto bad_area_nosemaphore;
91 /* Only enable interrupts if they were on before the fault */
92 if ((regs->sr & SR_IMASK) != SR_IMASK) {
100 * If we're in an interrupt or have no user
101 * context, we must not take the fault..
103 if (in_atomic() || !mm)
106 down_read(&mm->mmap_sem);
108 vma = find_vma(mm, address);
111 if (vma->vm_start <= address)
113 if (!(vma->vm_flags & VM_GROWSDOWN))
115 if (expand_stack(vma, address))
118 * Ok, we have a good vm_area for this memory access, so
122 si_code = SEGV_ACCERR;
124 if (!(vma->vm_flags & VM_WRITE))
127 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
132 * If for any reason at all we couldn't handle the fault,
133 * make sure we exit gracefully rather than endlessly redo
137 fault = handle_mm_fault(mm, vma, address, writeaccess);
138 if (unlikely(fault & VM_FAULT_ERROR)) {
139 if (fault & VM_FAULT_OOM)
141 else if (fault & VM_FAULT_SIGBUS)
145 if (fault & VM_FAULT_MAJOR)
150 up_read(&mm->mmap_sem);
154 * Something tried to access memory that isn't in our memory map..
155 * Fix it, but check if it's kernel or user first..
158 up_read(&mm->mmap_sem);
160 bad_area_nosemaphore:
161 if (user_mode(regs)) {
162 info.si_signo = SIGSEGV;
164 info.si_code = si_code;
165 info.si_addr = (void *) address;
166 force_sig_info(SIGSEGV, &info, tsk);
171 /* Are we prepared to handle this kernel fault? */
172 if (fixup_exception(regs))
175 if (handle_trapped_io(regs, address))
178 * Oops. The kernel tried to access some bad page. We'll have to
179 * terminate things with extreme prejudice.
185 if (oops_may_print()) {
188 if (address < PAGE_SIZE)
189 printk(KERN_ALERT "Unable to handle kernel NULL "
190 "pointer dereference");
192 printk(KERN_ALERT "Unable to handle kernel paging "
194 printk(" at virtual address %08lx\n", address);
195 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
196 page = (unsigned long)get_TTB();
198 page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT];
199 printk(KERN_ALERT "*pde = %08lx\n", page);
200 if (page & _PAGE_PRESENT) {
202 address &= 0x003ff000;
203 page = ((__typeof__(page) *)
204 __va(page))[address >>
206 printk(KERN_ALERT "*pte = %08lx\n", page);
211 die("Oops", regs, writeaccess);
216 * We ran out of memory, or some other thing happened to us that made
217 * us unable to handle the page fault gracefully.
220 up_read(&mm->mmap_sem);
221 if (is_global_init(current)) {
223 down_read(&mm->mmap_sem);
226 printk("VM: killing process %s\n", tsk->comm);
228 do_group_exit(SIGKILL);
232 up_read(&mm->mmap_sem);
235 * Send a sigbus, regardless of whether we were in kernel
238 info.si_signo = SIGBUS;
240 info.si_code = BUS_ADRERR;
241 info.si_addr = (void *)address;
242 force_sig_info(SIGBUS, &info, tsk);
244 /* Kernel mode? Handle exceptions or die */
245 if (!user_mode(regs))
249 static inline int notify_page_fault(struct pt_regs *regs, int trap)
253 trace_mark(kernel_arch_trap_entry, "trap_id %d ip #p%ld",
254 trap >> 5, instruction_pointer(regs));
256 #ifdef CONFIG_KPROBES
257 if (!user_mode(regs)) {
259 if (kprobe_running() && kprobe_fault_handler(regs, trap))
268 #ifdef CONFIG_SH_STORE_QUEUES
270 * This is a special case for the SH-4 store queues, as pages for this
271 * space still need to be faulted in before it's possible to flush the
272 * store queue cache for writeout to the remapped region.
274 #define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000)
276 #define P3_ADDR_MAX P4SEG
280 * Called with interrupts disabled.
282 asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs,
283 unsigned long writeaccess,
284 unsigned long address)
293 if (notify_page_fault(regs, lookup_exception_vector()))
296 #ifdef CONFIG_SH_KGDB
297 if (kgdb_nofault && kgdb_bus_err_hook)
304 * We don't take page faults for P1, P2, and parts of P4, these
305 * are always mapped, whether it be due to legacy behaviour in
306 * 29-bit mode, or due to PMB configuration in 32-bit mode.
308 if (address >= P3SEG && address < P3_ADDR_MAX) {
309 pgd = pgd_offset_k(address);
311 if (unlikely(address >= TASK_SIZE || !current->mm))
314 pgd = pgd_offset(current->mm, address);
317 pud = pud_offset(pgd, address);
318 if (pud_none_or_clear_bad(pud))
320 pmd = pmd_offset(pud, address);
321 if (pmd_none_or_clear_bad(pmd))
323 pte = pte_offset_kernel(pmd, address);
325 if (unlikely(pte_none(entry) || pte_not_present(entry)))
327 if (unlikely(writeaccess && !pte_write(entry)))
331 entry = pte_mkdirty(entry);
332 entry = pte_mkyoung(entry);
334 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
336 * ITLB is not affected by "ldtlb" instruction.
337 * So, we need to flush the entry by ourselves.
339 local_flush_tlb_one(get_asid(), address & PAGE_MASK);
343 update_mmu_cache(NULL, address, entry);
347 trace_mark(kernel_arch_trap_exit, MARK_NOARGS);