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>
22 extern void die(const char *,struct pt_regs *,long);
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 if (kgdb_nofault && kgdb_bus_err_hook)
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 /* This will never happen with the folded page table. */
66 if (!pgd_present(*pgd)) {
67 if (!pgd_present(*pgd_k))
68 goto bad_area_nosemaphore;
73 pud = pud_offset(pgd, address);
74 pud_k = pud_offset(pgd_k, address);
75 if (pud_present(*pud) || !pud_present(*pud_k))
76 goto bad_area_nosemaphore;
79 pmd = pmd_offset(pud, address);
80 pmd_k = pmd_offset(pud_k, address);
81 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
82 goto bad_area_nosemaphore;
89 * If we're in an interrupt or have no user
90 * context, we must not take the fault..
92 if (in_atomic() || !mm)
95 down_read(&mm->mmap_sem);
97 vma = find_vma(mm, address);
100 if (vma->vm_start <= address)
102 if (!(vma->vm_flags & VM_GROWSDOWN))
104 if (expand_stack(vma, address))
107 * Ok, we have a good vm_area for this memory access, so
111 si_code = SEGV_ACCERR;
113 if (!(vma->vm_flags & VM_WRITE))
116 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
121 * If for any reason at all we couldn't handle the fault,
122 * make sure we exit gracefully rather than endlessly redo
126 switch (handle_mm_fault(mm, vma, address, writeaccess)) {
133 case VM_FAULT_SIGBUS:
141 up_read(&mm->mmap_sem);
145 * Something tried to access memory that isn't in our memory map..
146 * Fix it, but check if it's kernel or user first..
149 up_read(&mm->mmap_sem);
151 bad_area_nosemaphore:
152 if (user_mode(regs)) {
153 info.si_signo = SIGSEGV;
155 info.si_code = si_code;
156 info.si_addr = (void *) address;
157 force_sig_info(SIGSEGV, &info, tsk);
162 /* Are we prepared to handle this kernel fault? */
163 if (fixup_exception(regs))
167 * Oops. The kernel tried to access some bad page. We'll have to
168 * terminate things with extreme prejudice.
171 if (address < PAGE_SIZE)
172 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
174 printk(KERN_ALERT "Unable to handle kernel paging request");
175 printk(" at virtual address %08lx\n", address);
176 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
177 page = (unsigned long)get_TTB();
179 page = ((unsigned long *) page)[address >> PGDIR_SHIFT];
180 printk(KERN_ALERT "*pde = %08lx\n", page);
181 if (page & _PAGE_PRESENT) {
183 address &= 0x003ff000;
184 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
185 printk(KERN_ALERT "*pte = %08lx\n", page);
188 die("Oops", regs, writeaccess);
192 * We ran out of memory, or some other thing happened to us that made
193 * us unable to handle the page fault gracefully.
196 up_read(&mm->mmap_sem);
197 if (is_init(current)) {
199 down_read(&mm->mmap_sem);
202 printk("VM: killing process %s\n", tsk->comm);
208 up_read(&mm->mmap_sem);
211 * Send a sigbus, regardless of whether we were in kernel
214 info.si_signo = SIGBUS;
216 info.si_code = BUS_ADRERR;
217 info.si_addr = (void *)address;
218 force_sig_info(SIGBUS, &info, tsk);
220 /* Kernel mode? Handle exceptions or die */
221 if (!user_mode(regs))