blob: ce75b8882efbe520ed8f90d2ba7b672872b043d9 [file] [log] [blame]
Paul Mundt26ff6c12006-09-27 15:13:36 +09001/*
2 * Page fault handler for SH with an MMU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 1999 Niibe Yutaka
Paul Mundt037c10a2008-09-08 12:22:47 +09005 * Copyright (C) 2003 - 2008 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Based on linux/arch/i386/mm/fault.c:
8 * Copyright (C) 1995 Linus Torvalds
Paul Mundt26ff6c12006-09-27 15:13:36 +09009 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
Paul Mundt0f08f332006-09-27 17:03:56 +090016#include <linux/hardirq.h>
17#include <linux/kprobes.h>
Paul Mundt7433ab7702009-06-25 02:30:10 +090018#include <linux/perf_counter.h>
Magnus Damme7cc9a72008-02-07 20:18:21 +090019#include <asm/io_trapped.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/mmu_context.h>
Paul Mundtdb2e1fa2007-02-14 14:13:10 +090022#include <asm/tlbflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Paul Mundt7433ab7702009-06-25 02:30:10 +090024static inline int notify_page_fault(struct pt_regs *regs, int trap)
25{
26 int ret = 0;
27
Paul Mundtc63c3102009-07-05 02:50:10 +090028 if (kprobes_built_in() && !user_mode(regs)) {
Paul Mundt7433ab7702009-06-25 02:30:10 +090029 preempt_disable();
30 if (kprobe_running() && kprobe_fault_handler(regs, trap))
31 ret = 1;
32 preempt_enable();
33 }
Paul Mundt7433ab7702009-06-25 02:30:10 +090034
35 return ret;
36}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * This routine handles page faults. It determines the address,
40 * and the problem, and then passes it off to one of the appropriate
41 * routines.
42 */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090043asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
44 unsigned long writeaccess,
45 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 struct task_struct *tsk;
48 struct mm_struct *mm;
49 struct vm_area_struct * vma;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090050 int si_code;
Nick Piggin83c54072007-07-19 01:47:05 -070051 int fault;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090052 siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Paul Mundt8f2baee2008-09-21 12:11:25 +090054 /*
55 * We don't bother with any notifier callbacks here, as they are
56 * all handled through the __do_page_fault() fast-path.
57 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 tsk = current;
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +090060 si_code = SEGV_MAPERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Stuart Menefy99a596f2006-11-21 15:38:05 +090062 if (unlikely(address >= TASK_SIZE)) {
63 /*
64 * Synchronize this task's top level page-table
65 * with the 'reference' page table.
66 *
67 * Do _not_ use "tsk" here. We might be inside
68 * an interrupt in the middle of a task switch..
69 */
70 int offset = pgd_index(address);
71 pgd_t *pgd, *pgd_k;
72 pud_t *pud, *pud_k;
73 pmd_t *pmd, *pmd_k;
74
75 pgd = get_TTB() + offset;
76 pgd_k = swapper_pg_dir + offset;
77
Stuart Menefy99a596f2006-11-21 15:38:05 +090078 if (!pgd_present(*pgd)) {
79 if (!pgd_present(*pgd_k))
80 goto bad_area_nosemaphore;
81 set_pgd(pgd, *pgd_k);
82 return;
83 }
84
85 pud = pud_offset(pgd, address);
86 pud_k = pud_offset(pgd_k, address);
Stuart Menefy96e14e52008-09-05 16:17:15 +090087
88 if (!pud_present(*pud)) {
89 if (!pud_present(*pud_k))
90 goto bad_area_nosemaphore;
91 set_pud(pud, *pud_k);
92 return;
93 }
Stuart Menefy99a596f2006-11-21 15:38:05 +090094
95 pmd = pmd_offset(pud, address);
96 pmd_k = pmd_offset(pud_k, address);
97 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
98 goto bad_area_nosemaphore;
99 set_pmd(pmd, *pmd_k);
100
101 return;
102 }
103
Stuart Menefyf2fb4e42008-07-02 17:51:23 +0900104 mm = tsk->mm;
105
Paul Mundt7433ab7702009-06-25 02:30:10 +0900106 if (unlikely(notify_page_fault(regs, lookup_exception_vector())))
107 return;
108
109 /* Only enable interrupts if they were on before the fault */
110 if ((regs->sr & SR_IMASK) != SR_IMASK)
111 local_irq_enable();
112
113 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * If we're in an interrupt or have no user
117 * context, we must not take the fault..
118 */
119 if (in_atomic() || !mm)
120 goto no_context;
121
122 down_read(&mm->mmap_sem);
123
124 vma = find_vma(mm, address);
125 if (!vma)
126 goto bad_area;
127 if (vma->vm_start <= address)
128 goto good_area;
129 if (!(vma->vm_flags & VM_GROWSDOWN))
130 goto bad_area;
131 if (expand_stack(vma, address))
132 goto bad_area;
133/*
134 * Ok, we have a good vm_area for this memory access, so
135 * we can handle it..
136 */
137good_area:
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900138 si_code = SEGV_ACCERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (writeaccess) {
140 if (!(vma->vm_flags & VM_WRITE))
141 goto bad_area;
142 } else {
Jason Barondf67b3d2006-09-29 01:58:58 -0700143 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 goto bad_area;
145 }
146
147 /*
148 * If for any reason at all we couldn't handle the fault,
149 * make sure we exit gracefully rather than endlessly redo
150 * the fault.
151 */
152survive:
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700153 fault = handle_mm_fault(mm, vma, address, writeaccess ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -0700154 if (unlikely(fault & VM_FAULT_ERROR)) {
155 if (fault & VM_FAULT_OOM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto out_of_memory;
Nick Piggin83c54072007-07-19 01:47:05 -0700157 else if (fault & VM_FAULT_SIGBUS)
158 goto do_sigbus;
159 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Paul Mundt7433ab7702009-06-25 02:30:10 +0900161 if (fault & VM_FAULT_MAJOR) {
Nick Piggin83c54072007-07-19 01:47:05 -0700162 tsk->maj_flt++;
Paul Mundt7433ab7702009-06-25 02:30:10 +0900163 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
164 regs, address);
165 } else {
Nick Piggin83c54072007-07-19 01:47:05 -0700166 tsk->min_flt++;
Paul Mundt7433ab7702009-06-25 02:30:10 +0900167 perf_swcounter_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
168 regs, address);
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 up_read(&mm->mmap_sem);
172 return;
173
174/*
175 * Something tried to access memory that isn't in our memory map..
176 * Fix it, but check if it's kernel or user first..
177 */
178bad_area:
179 up_read(&mm->mmap_sem);
180
Stuart Menefy99a596f2006-11-21 15:38:05 +0900181bad_area_nosemaphore:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (user_mode(regs)) {
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900183 info.si_signo = SIGSEGV;
184 info.si_errno = 0;
185 info.si_code = si_code;
186 info.si_addr = (void *) address;
187 force_sig_info(SIGSEGV, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return;
189 }
190
191no_context:
192 /* Are we prepared to handle this kernel fault? */
193 if (fixup_exception(regs))
194 return;
195
Magnus Damme7cc9a72008-02-07 20:18:21 +0900196 if (handle_trapped_io(regs, address))
197 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
199 * Oops. The kernel tried to access some bad page. We'll have to
200 * terminate things with extreme prejudice.
201 *
202 */
Paul Mundt0630e452007-06-18 19:02:47 +0900203
204 bust_spinlocks(1);
205
206 if (oops_may_print()) {
Paul Mundtb62ad832008-01-10 14:07:03 +0900207 unsigned long page;
Paul Mundt0630e452007-06-18 19:02:47 +0900208
209 if (address < PAGE_SIZE)
210 printk(KERN_ALERT "Unable to handle kernel NULL "
211 "pointer dereference");
212 else
213 printk(KERN_ALERT "Unable to handle kernel paging "
214 "request");
215 printk(" at virtual address %08lx\n", address);
216 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
217 page = (unsigned long)get_TTB();
218 if (page) {
Paul Mundt06f862c2007-08-01 16:39:51 +0900219 page = ((__typeof__(page) *)page)[address >> PGDIR_SHIFT];
Paul Mundt0630e452007-06-18 19:02:47 +0900220 printk(KERN_ALERT "*pde = %08lx\n", page);
221 if (page & _PAGE_PRESENT) {
222 page &= PAGE_MASK;
223 address &= 0x003ff000;
224 page = ((__typeof__(page) *)
225 __va(page))[address >>
226 PAGE_SHIFT];
227 printk(KERN_ALERT "*pte = %08lx\n", page);
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 }
Paul Mundt0630e452007-06-18 19:02:47 +0900231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 die("Oops", regs, writeaccess);
Paul Mundt0630e452007-06-18 19:02:47 +0900233 bust_spinlocks(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 do_exit(SIGKILL);
235
236/*
237 * We ran out of memory, or some other thing happened to us that made
238 * us unable to handle the page fault gracefully.
239 */
240out_of_memory:
241 up_read(&mm->mmap_sem);
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700242 if (is_global_init(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 yield();
244 down_read(&mm->mmap_sem);
245 goto survive;
246 }
247 printk("VM: killing process %s\n", tsk->comm);
248 if (user_mode(regs))
Will Schmidtdcca2bd2007-10-16 01:24:18 -0700249 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 goto no_context;
251
252do_sigbus:
253 up_read(&mm->mmap_sem);
254
255 /*
256 * Send a sigbus, regardless of whether we were in kernel
257 * or user mode.
258 */
Stuart Menefyb5a1bcb2006-11-21 13:34:04 +0900259 info.si_signo = SIGBUS;
260 info.si_errno = 0;
261 info.si_code = BUS_ADRERR;
262 info.si_addr = (void *)address;
263 force_sig_info(SIGBUS, &info, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* Kernel mode? Handle exceptions or die */
266 if (!user_mode(regs))
267 goto no_context;
268}
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900269
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900270/*
271 * Called with interrupts disabled.
272 */
273asmlinkage int __kprobes __do_page_fault(struct pt_regs *regs,
274 unsigned long writeaccess,
275 unsigned long address)
276{
277 pgd_t *pgd;
278 pud_t *pud;
279 pmd_t *pmd;
280 pte_t *pte;
281 pte_t entry;
Paul Mundt7433ab7702009-06-25 02:30:10 +0900282 int ret = 1;
Paul Mundt3d586952008-09-21 13:56:39 +0900283
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900284 /*
285 * We don't take page faults for P1, P2, and parts of P4, these
286 * are always mapped, whether it be due to legacy behaviour in
287 * 29-bit mode, or due to PMB configuration in 32-bit mode.
288 */
289 if (address >= P3SEG && address < P3_ADDR_MAX) {
290 pgd = pgd_offset_k(address);
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900291 } else {
Paul Mundt0f1a3942007-11-19 13:05:18 +0900292 if (unlikely(address >= TASK_SIZE || !current->mm))
Paul Mundt3d586952008-09-21 13:56:39 +0900293 goto out;
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900294
Paul Mundt0f1a3942007-11-19 13:05:18 +0900295 pgd = pgd_offset(current->mm, address);
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900296 }
297
298 pud = pud_offset(pgd, address);
299 if (pud_none_or_clear_bad(pud))
Paul Mundt3d586952008-09-21 13:56:39 +0900300 goto out;
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900301 pmd = pmd_offset(pud, address);
302 if (pmd_none_or_clear_bad(pmd))
Paul Mundt3d586952008-09-21 13:56:39 +0900303 goto out;
Paul Mundt0f1a3942007-11-19 13:05:18 +0900304 pte = pte_offset_kernel(pmd, address);
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900305 entry = *pte;
306 if (unlikely(pte_none(entry) || pte_not_present(entry)))
Paul Mundt3d586952008-09-21 13:56:39 +0900307 goto out;
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900308 if (unlikely(writeaccess && !pte_write(entry)))
Paul Mundt3d586952008-09-21 13:56:39 +0900309 goto out;
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900310
311 if (writeaccess)
312 entry = pte_mkdirty(entry);
313 entry = pte_mkyoung(entry);
314
Hideo Saitoa602cc02008-02-14 14:45:08 +0900315#if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
316 /*
317 * ITLB is not affected by "ldtlb" instruction.
318 * So, we need to flush the entry by ourselves.
319 */
320 local_flush_tlb_one(get_asid(), address & PAGE_MASK);
321#endif
322
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900323 set_pte(pte, entry);
324 update_mmu_cache(NULL, address, entry);
Paul Mundt0f1a3942007-11-19 13:05:18 +0900325
Paul Mundt3d586952008-09-21 13:56:39 +0900326 ret = 0;
327out:
Paul Mundt3d586952008-09-21 13:56:39 +0900328 return ret;
Paul Mundtdb2e1fa2007-02-14 14:13:10 +0900329}