sh: kprobes: Hook up kprobe_fault_handler() in the page fault path.
[linux-2.6/kvm.git] / arch / sh / mm / fault_32.c
blob659811c179e6a09290d90ac3c8bf12a2563bf9b7
1 /*
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
12 * for more details.
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/hardirq.h>
17 #include <linux/kprobes.h>
18 #include <asm/io_trapped.h>
19 #include <asm/system.h>
20 #include <asm/mmu_context.h>
21 #include <asm/tlbflush.h>
22 #include <asm/kgdb.h>
24 #ifdef CONFIG_KPROBES
25 static inline int notify_page_fault(struct pt_regs *regs, int trap)
27 int ret = 0;
29 if (!user_mode(regs)) {
30 preempt_disable();
31 if (kprobe_running() && kprobe_fault_handler(regs, trap))
32 ret = 1;
33 preempt_enable();
36 return ret;
38 #else
39 static inline int notify_page_fault(struct pt_regs *regs, int trap)
41 return 0;
43 #endif
46 * This routine handles page faults. It determines the address,
47 * and the problem, and then passes it off to one of the appropriate
48 * routines.
50 asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
51 unsigned long writeaccess,
52 unsigned long address)
54 struct task_struct *tsk;
55 struct mm_struct *mm;
56 struct vm_area_struct * vma;
57 int si_code;
58 int fault;
59 siginfo_t info;
61 if (notify_page_fault(regs, writeaccess))
62 return;
64 #ifdef CONFIG_SH_KGDB
65 if (kgdb_nofault && kgdb_bus_err_hook)
66 kgdb_bus_err_hook();
67 #endif
69 tsk = current;
70 si_code = SEGV_MAPERR;
72 if (unlikely(address >= TASK_SIZE)) {
74 * Synchronize this task's top level page-table
75 * with the 'reference' page table.
77 * Do _not_ use "tsk" here. We might be inside
78 * an interrupt in the middle of a task switch..
80 int offset = pgd_index(address);
81 pgd_t *pgd, *pgd_k;
82 pud_t *pud, *pud_k;
83 pmd_t *pmd, *pmd_k;
85 pgd = get_TTB() + offset;
86 pgd_k = swapper_pg_dir + offset;
88 if (!pgd_present(*pgd)) {
89 if (!pgd_present(*pgd_k))
90 goto bad_area_nosemaphore;
91 set_pgd(pgd, *pgd_k);
92 return;
95 pud = pud_offset(pgd, address);
96 pud_k = pud_offset(pgd_k, address);
98 if (!pud_present(*pud)) {
99 if (!pud_present(*pud_k))
100 goto bad_area_nosemaphore;
101 set_pud(pud, *pud_k);
102 return;
105 pmd = pmd_offset(pud, address);
106 pmd_k = pmd_offset(pud_k, address);
107 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
108 goto bad_area_nosemaphore;
109 set_pmd(pmd, *pmd_k);
111 return;
114 /* Only enable interrupts if they were on before the fault */
115 if ((regs->sr & SR_IMASK) != SR_IMASK) {
116 trace_hardirqs_on();
117 local_irq_enable();
120 mm = tsk->mm;
123 * If we're in an interrupt or have no user
124 * context, we must not take the fault..
126 if (in_atomic() || !mm)
127 goto no_context;
129 down_read(&mm->mmap_sem);
131 vma = find_vma(mm, address);
132 if (!vma)
133 goto bad_area;
134 if (vma->vm_start <= address)
135 goto good_area;
136 if (!(vma->vm_flags & VM_GROWSDOWN))
137 goto bad_area;
138 if (expand_stack(vma, address))
139 goto bad_area;
141 * Ok, we have a good vm_area for this memory access, so
142 * we can handle it..
144 good_area:
145 si_code = SEGV_ACCERR;
146 if (writeaccess) {
147 if (!(vma->vm_flags & VM_WRITE))
148 goto bad_area;
149 } else {
150 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
151 goto bad_area;
155 * If for any reason at all we couldn't handle the fault,
156 * make sure we exit gracefully rather than endlessly redo
157 * the fault.
159 survive:
160 fault = handle_mm_fault(mm, vma, address, writeaccess);
161 if (unlikely(fault & VM_FAULT_ERROR)) {
162 if (fault & VM_FAULT_OOM)
163 goto out_of_memory;
164 else if (fault & VM_FAULT_SIGBUS)
165 goto do_sigbus;
166 BUG();
168 if (fault & VM_FAULT_MAJOR)
169 tsk->maj_flt++;
170 else
171 tsk->min_flt++;
173 up_read(&mm->mmap_sem);
174 return;
177 * Something tried to access memory that isn't in our memory map..
178 * Fix it, but check if it's kernel or user first..
180 bad_area:
181 up_read(&mm->mmap_sem);
183 bad_area_nosemaphore:
184 if (user_mode(regs)) {
185 info.si_signo = SIGSEGV;
186 info.si_errno = 0;
187 info.si_code = si_code;
188 info.si_addr = (void *) address;
189 force_sig_info(SIGSEGV, &info, tsk);
190 return;
193 no_context:
194 /* Are we prepared to handle this kernel fault? */
195 if (fixup_exception(regs))
196 return;
198 if (handle_trapped_io(regs, address))
199 return;
201 * Oops. The kernel tried to access some bad page. We'll have to
202 * terminate things with extreme prejudice.
206 bust_spinlocks(1);
208 if (oops_may_print()) {
209 unsigned long page;
211 if (address < PAGE_SIZE)
212 printk(KERN_ALERT "Unable to handle kernel NULL "
213 "pointer dereference");
214 else
215 printk(KERN_ALERT "Unable to handle kernel paging "
216 "request");
217 printk(" at virtual address %08lx\n", address);
218 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
219 page = (unsigned long)get_TTB();
220 if (page) {
221 page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT];
222 printk(KERN_ALERT "*pde = %08lx\n", page);
223 if (page & _PAGE_PRESENT) {
224 page &= PAGE_MASK;
225 address &= 0x003ff000;
226 page = ((__typeof__(page) *)
227 __va(page))[address >>
228 PAGE_SHIFT];
229 printk(KERN_ALERT "*pte = %08lx\n", page);
234 die("Oops", regs, writeaccess);
235 bust_spinlocks(0);
236 do_exit(SIGKILL);
239 * We ran out of memory, or some other thing happened to us that made
240 * us unable to handle the page fault gracefully.
242 out_of_memory:
243 up_read(&mm->mmap_sem);
244 if (is_global_init(current)) {
245 yield();
246 down_read(&mm->mmap_sem);
247 goto survive;
249 printk("VM: killing process %s\n", tsk->comm);
250 if (user_mode(regs))
251 do_group_exit(SIGKILL);
252 goto no_context;
254 do_sigbus:
255 up_read(&mm->mmap_sem);
258 * Send a sigbus, regardless of whether we were in kernel
259 * or user mode.
261 info.si_signo = SIGBUS;
262 info.si_errno = 0;
263 info.si_code = BUS_ADRERR;
264 info.si_addr = (void *)address;
265 force_sig_info(SIGBUS, &info, tsk);
267 /* Kernel mode? Handle exceptions or die */
268 if (!user_mode(regs))
269 goto no_context;
272 #ifdef CONFIG_SH_STORE_QUEUES
274 * This is a special case for the SH-4 store queues, as pages for this
275 * space still need to be faulted in before it's possible to flush the
276 * store queue cache for writeout to the remapped region.
278 #define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000)
279 #else
280 #define P3_ADDR_MAX P4SEG
281 #endif
284 * Called with interrupts disabled.
286 asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs,
287 unsigned long writeaccess,
288 unsigned long address)
290 pgd_t *pgd;
291 pud_t *pud;
292 pmd_t *pmd;
293 pte_t *pte;
294 pte_t entry;
296 if (notify_page_fault(regs, writeaccess))
297 return 0;
299 #ifdef CONFIG_SH_KGDB
300 if (kgdb_nofault && kgdb_bus_err_hook)
301 kgdb_bus_err_hook();
302 #endif
305 * We don't take page faults for P1, P2, and parts of P4, these
306 * are always mapped, whether it be due to legacy behaviour in
307 * 29-bit mode, or due to PMB configuration in 32-bit mode.
309 if (address >= P3SEG && address < P3_ADDR_MAX) {
310 pgd = pgd_offset_k(address);
311 } else {
312 if (unlikely(address >= TASK_SIZE || !current->mm))
313 return 1;
315 pgd = pgd_offset(current->mm, address);
318 pud = pud_offset(pgd, address);
319 if (pud_none_or_clear_bad(pud))
320 return 1;
321 pmd = pmd_offset(pud, address);
322 if (pmd_none_or_clear_bad(pmd))
323 return 1;
325 pte = pte_offset_kernel(pmd, address);
326 entry = *pte;
327 if (unlikely(pte_none(entry) || pte_not_present(entry)))
328 return 1;
329 if (unlikely(writeaccess && !pte_write(entry)))
330 return 1;
332 if (writeaccess)
333 entry = pte_mkdirty(entry);
334 entry = pte_mkyoung(entry);
336 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
338 * ITLB is not affected by "ldtlb" instruction.
339 * So, we need to flush the entry by ourselves.
341 local_flush_tlb_one(get_asid(), address & PAGE_MASK);
342 #endif
344 set_pte(pte, entry);
345 update_mmu_cache(NULL, address, entry);
347 return 0;