1 /* $Id: fault.c,v 1.14 2004/01/13 05:52:11 kkojima Exp $
3 * linux/arch/sh/mm/fault.c
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
11 #include <linux/signal.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/ptrace.h>
18 #include <linux/mman.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
25 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgalloc.h>
29 #include <asm/hardirq.h>
30 #include <asm/mmu_context.h>
31 #include <asm/cacheflush.h>
34 extern void die(const char *,struct pt_regs *,long);
37 * This routine handles page faults. It determines the address,
38 * and the problem, and then passes it off to one of the appropriate
41 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
42 unsigned long address)
44 struct task_struct *tsk;
46 struct vm_area_struct * vma;
50 if (kgdb_nofault && kgdb_bus_err_hook)
58 * If we're in an interrupt or have no user
59 * context, we must not take the fault..
61 if (in_atomic() || !mm)
64 down_read(&mm->mmap_sem);
66 vma = find_vma(mm, address);
69 if (vma->vm_start <= address)
71 if (!(vma->vm_flags & VM_GROWSDOWN))
73 if (expand_stack(vma, address))
76 * Ok, we have a good vm_area for this memory access, so
81 if (!(vma->vm_flags & VM_WRITE))
84 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
89 * If for any reason at all we couldn't handle the fault,
90 * make sure we exit gracefully rather than endlessly redo
94 switch (handle_mm_fault(mm, vma, address, writeaccess)) {
101 case VM_FAULT_SIGBUS:
109 up_read(&mm->mmap_sem);
113 * Something tried to access memory that isn't in our memory map..
114 * Fix it, but check if it's kernel or user first..
117 up_read(&mm->mmap_sem);
119 if (user_mode(regs)) {
120 tsk->thread.address = address;
121 tsk->thread.error_code = writeaccess;
122 force_sig(SIGSEGV, tsk);
127 /* Are we prepared to handle this kernel fault? */
128 if (fixup_exception(regs))
132 * Oops. The kernel tried to access some bad page. We'll have to
133 * terminate things with extreme prejudice.
136 if (address < PAGE_SIZE)
137 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
139 printk(KERN_ALERT "Unable to handle kernel paging request");
140 printk(" at virtual address %08lx\n", address);
141 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
142 asm volatile("mov.l %1, %0"
144 : "m" (__m(MMU_TTB)));
146 page = ((unsigned long *) page)[address >> 22];
147 printk(KERN_ALERT "*pde = %08lx\n", page);
148 if (page & _PAGE_PRESENT) {
150 address &= 0x003ff000;
151 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
152 printk(KERN_ALERT "*pte = %08lx\n", page);
155 die("Oops", regs, writeaccess);
159 * We ran out of memory, or some other thing happened to us that made
160 * us unable to handle the page fault gracefully.
163 up_read(&mm->mmap_sem);
164 if (current->pid == 1) {
166 down_read(&mm->mmap_sem);
169 printk("VM: killing process %s\n", tsk->comm);
175 up_read(&mm->mmap_sem);
178 * Send a sigbus, regardless of whether we were in kernel
181 tsk->thread.address = address;
182 tsk->thread.error_code = writeaccess;
183 tsk->thread.trap_no = 14;
184 force_sig(SIGBUS, tsk);
186 /* Kernel mode? Handle exceptions or die */
187 if (!user_mode(regs))
192 * Called with interrupt disabled.
194 asmlinkage int __do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
195 unsigned long address)
197 unsigned long addrmax = P4SEG;
203 #ifdef CONFIG_SH_KGDB
204 if (kgdb_nofault && kgdb_bus_err_hook)
208 #ifdef CONFIG_SH_STORE_QUEUES
209 addrmax = P4SEG_STORE_QUE + 0x04000000;
212 if (address >= P3SEG && address < addrmax)
213 dir = pgd_offset_k(address);
214 else if (address >= TASK_SIZE)
216 else if (!current->mm)
219 dir = pgd_offset(current->mm, address);
221 pmd = pmd_offset(dir, address);
229 pte = pte_offset_kernel(pmd, address);
231 if (pte_none(entry) || pte_not_present(entry)
232 || (writeaccess && !pte_write(entry)))
236 entry = pte_mkdirty(entry);
237 entry = pte_mkyoung(entry);
239 #ifdef CONFIG_CPU_SH4
241 * ITLB is not affected by "ldtlb" instruction.
242 * So, we need to flush the entry by ourselves.
247 local_irq_save(flags);
248 __flush_tlb_page(get_asid(), address&PAGE_MASK);
249 local_irq_restore(flags);
254 update_mmu_cache(NULL, address, entry);
259 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
261 if (vma->vm_mm && vma->vm_mm->context != NO_CONTEXT) {
264 unsigned long saved_asid = MMU_NO_ASID;
266 asid = vma->vm_mm->context & MMU_CONTEXT_ASID_MASK;
269 local_irq_save(flags);
270 if (vma->vm_mm != current->mm) {
271 saved_asid = get_asid();
274 __flush_tlb_page(asid, page);
275 if (saved_asid != MMU_NO_ASID)
276 set_asid(saved_asid);
277 local_irq_restore(flags);
281 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
284 struct mm_struct *mm = vma->vm_mm;
286 if (mm->context != NO_CONTEXT) {
290 local_irq_save(flags);
291 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
292 if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
293 mm->context = NO_CONTEXT;
294 if (mm == current->mm)
295 activate_context(mm);
297 unsigned long asid = mm->context&MMU_CONTEXT_ASID_MASK;
298 unsigned long saved_asid = MMU_NO_ASID;
301 end += (PAGE_SIZE - 1);
303 if (mm != current->mm) {
304 saved_asid = get_asid();
307 while (start < end) {
308 __flush_tlb_page(asid, start);
311 if (saved_asid != MMU_NO_ASID)
312 set_asid(saved_asid);
314 local_irq_restore(flags);
318 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
323 local_irq_save(flags);
324 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
325 if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
328 unsigned long asid = init_mm.context&MMU_CONTEXT_ASID_MASK;
329 unsigned long saved_asid = get_asid();
332 end += (PAGE_SIZE - 1);
335 while (start < end) {
336 __flush_tlb_page(asid, start);
339 set_asid(saved_asid);
341 local_irq_restore(flags);
344 void flush_tlb_mm(struct mm_struct *mm)
346 /* Invalidate all TLB of this process. */
347 /* Instead of invalidating each TLB, we get new MMU context. */
348 if (mm->context != NO_CONTEXT) {
351 local_irq_save(flags);
352 mm->context = NO_CONTEXT;
353 if (mm == current->mm)
354 activate_context(mm);
355 local_irq_restore(flags);
359 void flush_tlb_all(void)
361 unsigned long flags, status;
366 * Write to the MMU control register's bit:
367 * TF-bit for SH-3, TI-bit for SH-4.
368 * It's same position, bit #2.
370 local_irq_save(flags);
371 status = ctrl_inl(MMUCR);
373 ctrl_outl(status, MMUCR);
374 local_irq_restore(flags);