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/mmu_context.h>
30 #include <asm/cacheflush.h>
33 extern void die(const char *,struct pt_regs *,long);
36 * This routine handles page faults. It determines the address,
37 * and the problem, and then passes it off to one of the appropriate
40 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
41 unsigned long address)
43 struct task_struct *tsk;
45 struct vm_area_struct * vma;
49 if (kgdb_nofault && kgdb_bus_err_hook)
57 * If we're in an interrupt or have no user
58 * context, we must not take the fault..
60 if (in_atomic() || !mm)
63 down_read(&mm->mmap_sem);
65 vma = find_vma(mm, address);
68 if (vma->vm_start <= address)
70 if (!(vma->vm_flags & VM_GROWSDOWN))
72 if (expand_stack(vma, address))
75 * Ok, we have a good vm_area for this memory access, so
80 if (!(vma->vm_flags & VM_WRITE))
83 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
88 * If for any reason at all we couldn't handle the fault,
89 * make sure we exit gracefully rather than endlessly redo
93 switch (handle_mm_fault(mm, vma, address, writeaccess)) {
100 case VM_FAULT_SIGBUS:
108 up_read(&mm->mmap_sem);
112 * Something tried to access memory that isn't in our memory map..
113 * Fix it, but check if it's kernel or user first..
116 up_read(&mm->mmap_sem);
118 if (user_mode(regs)) {
119 tsk->thread.address = address;
120 tsk->thread.error_code = writeaccess;
121 force_sig(SIGSEGV, tsk);
126 /* Are we prepared to handle this kernel fault? */
127 if (fixup_exception(regs))
131 * Oops. The kernel tried to access some bad page. We'll have to
132 * terminate things with extreme prejudice.
135 if (address < PAGE_SIZE)
136 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
138 printk(KERN_ALERT "Unable to handle kernel paging request");
139 printk(" at virtual address %08lx\n", address);
140 printk(KERN_ALERT "pc = %08lx\n", regs->pc);
141 asm volatile("mov.l %1, %0"
143 : "m" (__m(MMU_TTB)));
145 page = ((unsigned long *) page)[address >> 22];
146 printk(KERN_ALERT "*pde = %08lx\n", page);
147 if (page & _PAGE_PRESENT) {
149 address &= 0x003ff000;
150 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
151 printk(KERN_ALERT "*pte = %08lx\n", page);
154 die("Oops", regs, writeaccess);
158 * We ran out of memory, or some other thing happened to us that made
159 * us unable to handle the page fault gracefully.
162 up_read(&mm->mmap_sem);
163 if (current->pid == 1) {
165 down_read(&mm->mmap_sem);
168 printk("VM: killing process %s\n", tsk->comm);
174 up_read(&mm->mmap_sem);
177 * Send a sigbus, regardless of whether we were in kernel
180 tsk->thread.address = address;
181 tsk->thread.error_code = writeaccess;
182 tsk->thread.trap_no = 14;
183 force_sig(SIGBUS, tsk);
185 /* Kernel mode? Handle exceptions or die */
186 if (!user_mode(regs))
191 * Called with interrupt disabled.
193 asmlinkage int __do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
194 unsigned long address)
196 unsigned long addrmax = P4SEG;
201 struct mm_struct *mm;
205 #ifdef CONFIG_SH_KGDB
206 if (kgdb_nofault && kgdb_bus_err_hook)
210 #ifdef CONFIG_SH_STORE_QUEUES
211 addrmax = P4SEG_STORE_QUE + 0x04000000;
214 if (address >= P3SEG && address < addrmax) {
215 pgd = pgd_offset_k(address);
217 } else if (address >= TASK_SIZE)
219 else if (!(mm = current->mm))
222 pgd = pgd_offset(mm, address);
224 pmd = pmd_offset(pgd, address);
225 if (pmd_none_or_clear_bad(pmd))
228 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
230 pte = pte_offset_kernel(pmd, address);
233 if (pte_none(entry) || pte_not_present(entry)
234 || (writeaccess && !pte_write(entry)))
238 entry = pte_mkdirty(entry);
239 entry = pte_mkyoung(entry);
241 #ifdef CONFIG_CPU_SH4
243 * ITLB is not affected by "ldtlb" instruction.
244 * So, we need to flush the entry by ourselves.
249 local_irq_save(flags);
250 __flush_tlb_page(get_asid(), address&PAGE_MASK);
251 local_irq_restore(flags);
256 update_mmu_cache(NULL, address, entry);
260 pte_unmap_unlock(pte, ptl);
264 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
266 if (vma->vm_mm && vma->vm_mm->context != NO_CONTEXT) {
269 unsigned long saved_asid = MMU_NO_ASID;
271 asid = vma->vm_mm->context & MMU_CONTEXT_ASID_MASK;
274 local_irq_save(flags);
275 if (vma->vm_mm != current->mm) {
276 saved_asid = get_asid();
279 __flush_tlb_page(asid, page);
280 if (saved_asid != MMU_NO_ASID)
281 set_asid(saved_asid);
282 local_irq_restore(flags);
286 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
289 struct mm_struct *mm = vma->vm_mm;
291 if (mm->context != NO_CONTEXT) {
295 local_irq_save(flags);
296 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
297 if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
298 mm->context = NO_CONTEXT;
299 if (mm == current->mm)
300 activate_context(mm);
302 unsigned long asid = mm->context&MMU_CONTEXT_ASID_MASK;
303 unsigned long saved_asid = MMU_NO_ASID;
306 end += (PAGE_SIZE - 1);
308 if (mm != current->mm) {
309 saved_asid = get_asid();
312 while (start < end) {
313 __flush_tlb_page(asid, start);
316 if (saved_asid != MMU_NO_ASID)
317 set_asid(saved_asid);
319 local_irq_restore(flags);
323 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
328 local_irq_save(flags);
329 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
330 if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
333 unsigned long asid = init_mm.context&MMU_CONTEXT_ASID_MASK;
334 unsigned long saved_asid = get_asid();
337 end += (PAGE_SIZE - 1);
340 while (start < end) {
341 __flush_tlb_page(asid, start);
344 set_asid(saved_asid);
346 local_irq_restore(flags);
349 void flush_tlb_mm(struct mm_struct *mm)
351 /* Invalidate all TLB of this process. */
352 /* Instead of invalidating each TLB, we get new MMU context. */
353 if (mm->context != NO_CONTEXT) {
356 local_irq_save(flags);
357 mm->context = NO_CONTEXT;
358 if (mm == current->mm)
359 activate_context(mm);
360 local_irq_restore(flags);
364 void flush_tlb_all(void)
366 unsigned long flags, status;
371 * Write to the MMU control register's bit:
372 * TF-bit for SH-3, TI-bit for SH-4.
373 * It's same position, bit #2.
375 local_irq_save(flags);
376 status = ctrl_inl(MMUCR);
378 ctrl_outl(status, MMUCR);
379 local_irq_restore(flags);