2 * Page fault handler for SH with an MMU.
4 * Copyright (C) 1999 Niibe Yutaka
5 * Copyright (C) 2003 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 <asm/system.h>
19 #include <asm/mmu_context.h>
20 #include <asm/tlbflush.h>
23 extern void die(const char *,struct pt_regs *,long);
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;
45 if (kgdb_nofault && kgdb_bus_err_hook)
51 si_code = SEGV_MAPERR;
53 if (unlikely(address >= TASK_SIZE)) {
55 * Synchronize this task's top level page-table
56 * with the 'reference' page table.
58 * Do _not_ use "tsk" here. We might be inside
59 * an interrupt in the middle of a task switch..
61 int offset = pgd_index(address);
66 pgd = get_TTB() + offset;
67 pgd_k = swapper_pg_dir + offset;
69 /* This will never happen with the folded page table. */
70 if (!pgd_present(*pgd)) {
71 if (!pgd_present(*pgd_k))
72 goto bad_area_nosemaphore;
77 pud = pud_offset(pgd, address);
78 pud_k = pud_offset(pgd_k, address);
79 if (pud_present(*pud) || !pud_present(*pud_k))
80 goto bad_area_nosemaphore;
83 pmd = pmd_offset(pud, address);
84 pmd_k = pmd_offset(pud_k, address);
85 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
86 goto bad_area_nosemaphore;
93 * If we're in an interrupt or have no user
94 * context, we must not take the fault..
96 if (in_atomic() || !mm)
99 down_read(&mm->mmap_sem);
101 vma = find_vma(mm, address);
104 if (vma->vm_start <= address)
106 if (!(vma->vm_flags & VM_GROWSDOWN))
108 if (expand_stack(vma, address))
111 * Ok, we have a good vm_area for this memory access, so
115 si_code = SEGV_ACCERR;
117 if (!(vma->vm_flags & VM_WRITE))
120 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
125 * If for any reason at all we couldn't handle the fault,
126 * make sure we exit gracefully rather than endlessly redo
130 switch (handle_mm_fault(mm, vma, address, writeaccess)) {
137 case VM_FAULT_SIGBUS:
145 up_read(&mm->mmap_sem);
149 * Something tried to access memory that isn't in our memory map..
150 * Fix it, but check if it's kernel or user first..
153 up_read(&mm->mmap_sem);
155 bad_area_nosemaphore:
156 if (user_mode(regs)) {
157 info.si_signo = SIGSEGV;
159 info.si_code = si_code;
160 info.si_addr = (void *) address;
161 force_sig_info(SIGSEGV, &info, tsk);
166 /* Are we prepared to handle this kernel fault? */
167 if (fixup_exception(regs))
171 * Oops. The kernel tried to access some bad page. We'll have to
172 * terminate things with extreme prejudice.
175 if (address < PAGE_SIZE)
176 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
178 printk(KERN_ALERT "Unable to handle kernel paging request");
179 printk(" at virtual address %08lx\n", address);
180 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
181 page = (unsigned long)get_TTB();
183 page = ((unsigned long *) page)[address >> PGDIR_SHIFT];
184 printk(KERN_ALERT "*pde = %08lx\n", page);
185 if (page & _PAGE_PRESENT) {
187 address &= 0x003ff000;
188 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
189 printk(KERN_ALERT "*pte = %08lx\n", page);
192 die("Oops", regs, writeaccess);
196 * We ran out of memory, or some other thing happened to us that made
197 * us unable to handle the page fault gracefully.
200 up_read(&mm->mmap_sem);
201 if (is_init(current)) {
203 down_read(&mm->mmap_sem);
206 printk("VM: killing process %s\n", tsk->comm);
212 up_read(&mm->mmap_sem);
215 * Send a sigbus, regardless of whether we were in kernel
218 info.si_signo = SIGBUS;
220 info.si_code = BUS_ADRERR;
221 info.si_addr = (void *)address;
222 force_sig_info(SIGBUS, &info, tsk);
224 /* Kernel mode? Handle exceptions or die */
225 if (!user_mode(regs))
229 #ifdef CONFIG_SH_STORE_QUEUES
231 * This is a special case for the SH-4 store queues, as pages for this
232 * space still need to be faulted in before it's possible to flush the
233 * store queue cache for writeout to the remapped region.
235 #define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000)
237 #define P3_ADDR_MAX P4SEG
241 * Called with interrupts disabled.
243 asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs,
244 unsigned long writeaccess,
245 unsigned long address)
252 struct mm_struct *mm = current->mm;
256 #ifdef CONFIG_SH_KGDB
257 if (kgdb_nofault && kgdb_bus_err_hook)
262 * We don't take page faults for P1, P2, and parts of P4, these
263 * are always mapped, whether it be due to legacy behaviour in
264 * 29-bit mode, or due to PMB configuration in 32-bit mode.
266 if (address >= P3SEG && address < P3_ADDR_MAX) {
267 pgd = pgd_offset_k(address);
270 if (unlikely(address >= TASK_SIZE || !mm))
273 pgd = pgd_offset(mm, address);
276 pud = pud_offset(pgd, address);
277 if (pud_none_or_clear_bad(pud))
279 pmd = pmd_offset(pud, address);
280 if (pmd_none_or_clear_bad(pmd))
284 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
286 pte = pte_offset_kernel(pmd, address);
289 if (unlikely(pte_none(entry) || pte_not_present(entry)))
291 if (unlikely(writeaccess && !pte_write(entry)))
295 entry = pte_mkdirty(entry);
296 entry = pte_mkyoung(entry);
298 #ifdef CONFIG_CPU_SH4
300 * ITLB is not affected by "ldtlb" instruction.
301 * So, we need to flush the entry by ourselves.
303 local_flush_tlb_one(get_asid(), address & PAGE_MASK);
307 update_mmu_cache(NULL, address, entry);
311 pte_unmap_unlock(pte, ptl);