2 * Page fault handler for SH with an MMU.
4 * Copyright (C) 1999 Niibe Yutaka
5 * Copyright (C) 2003 - 2009 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/perf_event.h>
19 #include <asm/io_trapped.h>
20 #include <asm/system.h>
21 #include <asm/mmu_context.h>
22 #include <asm/tlbflush.h>
24 static inline int notify_page_fault(struct pt_regs
*regs
, int trap
)
28 if (kprobes_built_in() && !user_mode(regs
)) {
30 if (kprobe_running() && kprobe_fault_handler(regs
, trap
))
38 static inline pmd_t
*vmalloc_sync_one(pgd_t
*pgd
, unsigned long address
)
40 unsigned index
= pgd_index(address
);
46 pgd_k
= init_mm
.pgd
+ index
;
48 if (!pgd_present(*pgd_k
))
51 pud
= pud_offset(pgd
, address
);
52 pud_k
= pud_offset(pgd_k
, address
);
53 if (!pud_present(*pud_k
))
56 pmd
= pmd_offset(pud
, address
);
57 pmd_k
= pmd_offset(pud_k
, address
);
58 if (!pmd_present(*pmd_k
))
61 if (!pmd_present(*pmd
))
65 * The page tables are fully synchronised so there must
66 * be another reason for the fault. Return NULL here to
67 * signal that we have not taken care of the fault.
69 BUG_ON(pmd_page(*pmd
) != pmd_page(*pmd_k
));
77 * Handle a fault on the vmalloc or module mapping area
79 static noinline
int vmalloc_fault(unsigned long address
)
85 /* Make sure we are in vmalloc/module/P3 area: */
86 if (!(address
>= VMALLOC_START
&& address
< P3_ADDR_MAX
))
90 * Synchronize this task's top level page-table
91 * with the 'reference' page table.
93 * Do _not_ use "current" here. We might be inside
94 * an interrupt in the middle of a task switch..
97 pmd_k
= vmalloc_sync_one(pgd_k
, address
);
101 pte_k
= pte_offset_kernel(pmd_k
, address
);
102 if (!pte_present(*pte_k
))
108 static int fault_in_kernel_space(unsigned long address
)
110 return address
>= TASK_SIZE
;
114 * This routine handles page faults. It determines the address,
115 * and the problem, and then passes it off to one of the appropriate
118 asmlinkage
void __kprobes
do_page_fault(struct pt_regs
*regs
,
119 unsigned long writeaccess
,
120 unsigned long address
)
123 struct task_struct
*tsk
;
124 struct mm_struct
*mm
;
125 struct vm_area_struct
* vma
;
132 si_code
= SEGV_MAPERR
;
133 vec
= lookup_exception_vector();
136 * We fault-in kernel-space virtual memory on-demand. The
137 * 'reference' page table is init_mm.pgd.
139 * NOTE! We MUST NOT take any locks for this case. We may
140 * be in an interrupt or a critical region, and should
141 * only copy the information from the master page table,
144 if (unlikely(fault_in_kernel_space(address
))) {
145 if (vmalloc_fault(address
) >= 0)
147 if (notify_page_fault(regs
, vec
))
150 goto bad_area_nosemaphore
;
153 if (unlikely(notify_page_fault(regs
, vec
)))
156 /* Only enable interrupts if they were on before the fault */
157 if ((regs
->sr
& SR_IMASK
) != SR_IMASK
)
160 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, 0, regs
, address
);
163 * If we're in an interrupt, have no user context or are running
164 * in an atomic region then we must not take the fault:
166 if (in_atomic() || !mm
)
169 down_read(&mm
->mmap_sem
);
171 vma
= find_vma(mm
, address
);
174 if (vma
->vm_start
<= address
)
176 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
178 if (expand_stack(vma
, address
))
182 * Ok, we have a good vm_area for this memory access, so
186 si_code
= SEGV_ACCERR
;
188 if (!(vma
->vm_flags
& VM_WRITE
))
191 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)))
196 * If for any reason at all we couldn't handle the fault,
197 * make sure we exit gracefully rather than endlessly redo
201 fault
= handle_mm_fault(mm
, vma
, address
, writeaccess
? FAULT_FLAG_WRITE
: 0);
202 if (unlikely(fault
& VM_FAULT_ERROR
)) {
203 if (fault
& VM_FAULT_OOM
)
205 else if (fault
& VM_FAULT_SIGBUS
)
209 if (fault
& VM_FAULT_MAJOR
) {
211 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
, 1, 0,
215 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
, 1, 0,
219 up_read(&mm
->mmap_sem
);
223 * Something tried to access memory that isn't in our memory map..
224 * Fix it, but check if it's kernel or user first..
227 up_read(&mm
->mmap_sem
);
229 bad_area_nosemaphore
:
230 if (user_mode(regs
)) {
231 info
.si_signo
= SIGSEGV
;
233 info
.si_code
= si_code
;
234 info
.si_addr
= (void *) address
;
235 force_sig_info(SIGSEGV
, &info
, tsk
);
240 /* Are we prepared to handle this kernel fault? */
241 if (fixup_exception(regs
))
244 if (handle_trapped_io(regs
, address
))
247 * Oops. The kernel tried to access some bad page. We'll have to
248 * terminate things with extreme prejudice.
254 if (oops_may_print()) {
257 if (address
< PAGE_SIZE
)
258 printk(KERN_ALERT
"Unable to handle kernel NULL "
259 "pointer dereference");
261 printk(KERN_ALERT
"Unable to handle kernel paging "
263 printk(" at virtual address %08lx\n", address
);
264 printk(KERN_ALERT
"pc = %08lx\n", regs
->pc
);
265 page
= (unsigned long)get_TTB();
267 page
= ((__typeof__(page
) *)page
)[address
>> PGDIR_SHIFT
];
268 printk(KERN_ALERT
"*pde = %08lx\n", page
);
269 if (page
& _PAGE_PRESENT
) {
271 address
&= 0x003ff000;
272 page
= ((__typeof__(page
) *)
273 __va(page
))[address
>>
275 printk(KERN_ALERT
"*pte = %08lx\n", page
);
280 die("Oops", regs
, writeaccess
);
285 * We ran out of memory, or some other thing happened to us that made
286 * us unable to handle the page fault gracefully.
289 up_read(&mm
->mmap_sem
);
290 if (is_global_init(current
)) {
292 down_read(&mm
->mmap_sem
);
295 printk("VM: killing process %s\n", tsk
->comm
);
297 do_group_exit(SIGKILL
);
301 up_read(&mm
->mmap_sem
);
304 * Send a sigbus, regardless of whether we were in kernel
307 info
.si_signo
= SIGBUS
;
309 info
.si_code
= BUS_ADRERR
;
310 info
.si_addr
= (void *)address
;
311 force_sig_info(SIGBUS
, &info
, tsk
);
313 /* Kernel mode? Handle exceptions or die */
314 if (!user_mode(regs
))
319 * Called with interrupts disabled.
321 asmlinkage
int __kprobes
322 handle_tlbmiss(struct pt_regs
*regs
, unsigned long writeaccess
,
323 unsigned long address
)
332 * We don't take page faults for P1, P2, and parts of P4, these
333 * are always mapped, whether it be due to legacy behaviour in
334 * 29-bit mode, or due to PMB configuration in 32-bit mode.
336 if (address
>= P3SEG
&& address
< P3_ADDR_MAX
) {
337 pgd
= pgd_offset_k(address
);
339 if (unlikely(address
>= TASK_SIZE
|| !current
->mm
))
342 pgd
= pgd_offset(current
->mm
, address
);
345 pud
= pud_offset(pgd
, address
);
346 if (pud_none_or_clear_bad(pud
))
348 pmd
= pmd_offset(pud
, address
);
349 if (pmd_none_or_clear_bad(pmd
))
351 pte
= pte_offset_kernel(pmd
, address
);
353 if (unlikely(pte_none(entry
) || pte_not_present(entry
)))
355 if (unlikely(writeaccess
&& !pte_write(entry
)))
359 entry
= pte_mkdirty(entry
);
360 entry
= pte_mkyoung(entry
);
364 #if defined(CONFIG_CPU_SH4) && !defined(CONFIG_SMP)
366 * SH-4 does not set MMUCR.RC to the corresponding TLB entry in
367 * the case of an initial page write exception, so we need to
368 * flush it in order to avoid potential TLB entry duplication.
370 if (writeaccess
== 2)
371 local_flush_tlb_one(get_asid(), address
& PAGE_MASK
);
374 update_mmu_cache(NULL
, address
, entry
);