2 * Page fault handler for SH with an MMU.
4 * Copyright (C) 1999 Niibe Yutaka
5 * Copyright (C) 2003 - 2007 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>
24 * This routine handles page faults. It determines the address,
25 * and the problem, and then passes it off to one of the appropriate
28 asmlinkage
void __kprobes
do_page_fault(struct pt_regs
*regs
,
29 unsigned long writeaccess
,
30 unsigned long address
)
32 struct task_struct
*tsk
;
34 struct vm_area_struct
* vma
;
43 if (kgdb_nofault
&& kgdb_bus_err_hook
)
49 si_code
= SEGV_MAPERR
;
51 if (unlikely(address
>= TASK_SIZE
)) {
53 * Synchronize this task's top level page-table
54 * with the 'reference' page table.
56 * Do _not_ use "tsk" here. We might be inside
57 * an interrupt in the middle of a task switch..
59 int offset
= pgd_index(address
);
64 pgd
= get_TTB() + offset
;
65 pgd_k
= swapper_pg_dir
+ offset
;
67 /* This will never happen with the folded page table. */
68 if (!pgd_present(*pgd
)) {
69 if (!pgd_present(*pgd_k
))
70 goto bad_area_nosemaphore
;
75 pud
= pud_offset(pgd
, address
);
76 pud_k
= pud_offset(pgd_k
, address
);
77 if (pud_present(*pud
) || !pud_present(*pud_k
))
78 goto bad_area_nosemaphore
;
81 pmd
= pmd_offset(pud
, address
);
82 pmd_k
= pmd_offset(pud_k
, address
);
83 if (pmd_present(*pmd
) || !pmd_present(*pmd_k
))
84 goto bad_area_nosemaphore
;
91 * If we're in an interrupt or have no user
92 * context, we must not take the fault..
94 if (in_atomic() || !mm
)
97 down_read(&mm
->mmap_sem
);
99 vma
= find_vma(mm
, address
);
102 if (vma
->vm_start
<= address
)
104 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
106 if (expand_stack(vma
, address
))
109 * Ok, we have a good vm_area for this memory access, so
113 si_code
= SEGV_ACCERR
;
115 if (!(vma
->vm_flags
& VM_WRITE
))
118 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)))
123 * If for any reason at all we couldn't handle the fault,
124 * make sure we exit gracefully rather than endlessly redo
128 fault
= handle_mm_fault(mm
, vma
, address
, writeaccess
);
129 if (unlikely(fault
& VM_FAULT_ERROR
)) {
130 if (fault
& VM_FAULT_OOM
)
132 else if (fault
& VM_FAULT_SIGBUS
)
136 if (fault
& VM_FAULT_MAJOR
)
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.
174 if (oops_may_print()) {
175 __typeof__(pte_val(__pte(0))) page
;
177 if (address
< PAGE_SIZE
)
178 printk(KERN_ALERT
"Unable to handle kernel NULL "
179 "pointer dereference");
181 printk(KERN_ALERT
"Unable to handle kernel paging "
183 printk(" at virtual address %08lx\n", address
);
184 printk(KERN_ALERT
"pc = %08lx\n", regs
->pc
);
185 page
= (unsigned long)get_TTB();
187 page
= ((__typeof__(page
) *)page
)[address
>> PGDIR_SHIFT
];
188 printk(KERN_ALERT
"*pde = %08lx\n", page
);
189 if (page
& _PAGE_PRESENT
) {
191 address
&= 0x003ff000;
192 page
= ((__typeof__(page
) *)
193 __va(page
))[address
>>
195 printk(KERN_ALERT
"*pte = %08lx\n", page
);
200 die("Oops", regs
, writeaccess
);
205 * We ran out of memory, or some other thing happened to us that made
206 * us unable to handle the page fault gracefully.
209 up_read(&mm
->mmap_sem
);
210 if (is_init(current
)) {
212 down_read(&mm
->mmap_sem
);
215 printk("VM: killing process %s\n", tsk
->comm
);
221 up_read(&mm
->mmap_sem
);
224 * Send a sigbus, regardless of whether we were in kernel
227 info
.si_signo
= SIGBUS
;
229 info
.si_code
= BUS_ADRERR
;
230 info
.si_addr
= (void *)address
;
231 force_sig_info(SIGBUS
, &info
, tsk
);
233 /* Kernel mode? Handle exceptions or die */
234 if (!user_mode(regs
))
238 #ifdef CONFIG_SH_STORE_QUEUES
240 * This is a special case for the SH-4 store queues, as pages for this
241 * space still need to be faulted in before it's possible to flush the
242 * store queue cache for writeout to the remapped region.
244 #define P3_ADDR_MAX (P4SEG_STORE_QUE + 0x04000000)
246 #define P3_ADDR_MAX P4SEG
250 * Called with interrupts disabled.
252 asmlinkage
int __kprobes
__do_page_fault(struct pt_regs
*regs
,
253 unsigned long writeaccess
,
254 unsigned long address
)
261 struct mm_struct
*mm
= current
->mm
;
262 spinlock_t
*ptl
= NULL
;
265 #ifdef CONFIG_SH_KGDB
266 if (kgdb_nofault
&& kgdb_bus_err_hook
)
271 * We don't take page faults for P1, P2, and parts of P4, these
272 * are always mapped, whether it be due to legacy behaviour in
273 * 29-bit mode, or due to PMB configuration in 32-bit mode.
275 if (address
>= P3SEG
&& address
< P3_ADDR_MAX
) {
276 pgd
= pgd_offset_k(address
);
279 if (unlikely(address
>= TASK_SIZE
|| !mm
))
282 pgd
= pgd_offset(mm
, address
);
285 pud
= pud_offset(pgd
, address
);
286 if (pud_none_or_clear_bad(pud
))
288 pmd
= pmd_offset(pud
, address
);
289 if (pmd_none_or_clear_bad(pmd
))
293 pte
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
295 pte
= pte_offset_kernel(pmd
, address
);
298 if (unlikely(pte_none(entry
) || pte_not_present(entry
)))
300 if (unlikely(writeaccess
&& !pte_write(entry
)))
304 entry
= pte_mkdirty(entry
);
305 entry
= pte_mkyoung(entry
);
307 #ifdef CONFIG_CPU_SH4
309 * ITLB is not affected by "ldtlb" instruction.
310 * So, we need to flush the entry by ourselves.
312 local_flush_tlb_one(get_asid(), address
& PAGE_MASK
);
316 update_mmu_cache(NULL
, address
, entry
);
320 pte_unmap_unlock(pte
, ptl
);