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;
202 #ifdef CONFIG_SH_KGDB
203 if (kgdb_nofault && kgdb_bus_err_hook)
207 #ifdef CONFIG_SH_STORE_QUEUES
208 addrmax = P4SEG_STORE_QUE + 0x04000000;
211 if (address >= P3SEG && address < addrmax)
212 dir = pgd_offset_k(address);
213 else if (address >= TASK_SIZE)
215 else if (!current->mm)
218 dir = pgd_offset(current->mm, address);
220 pmd = pmd_offset(dir, address);
228 pte = pte_offset_kernel(pmd, address);
230 if (pte_none(entry) || pte_not_present(entry)
231 || (writeaccess && !pte_write(entry)))
235 entry = pte_mkdirty(entry);
236 entry = pte_mkyoung(entry);
238 #ifdef CONFIG_CPU_SH4
240 * ITLB is not affected by "ldtlb" instruction.
241 * So, we need to flush the entry by ourselves.
246 local_irq_save(flags);
247 __flush_tlb_page(get_asid(), address&PAGE_MASK);
248 local_irq_restore(flags);
253 update_mmu_cache(NULL, address, entry);
258 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
260 if (vma->vm_mm && vma->vm_mm->context != NO_CONTEXT) {
263 unsigned long saved_asid = MMU_NO_ASID;
265 asid = vma->vm_mm->context & MMU_CONTEXT_ASID_MASK;
268 local_irq_save(flags);
269 if (vma->vm_mm != current->mm) {
270 saved_asid = get_asid();
273 __flush_tlb_page(asid, page);
274 if (saved_asid != MMU_NO_ASID)
275 set_asid(saved_asid);
276 local_irq_restore(flags);
280 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
283 struct mm_struct *mm = vma->vm_mm;
285 if (mm->context != NO_CONTEXT) {
289 local_irq_save(flags);
290 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
291 if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
292 mm->context = NO_CONTEXT;
293 if (mm == current->mm)
294 activate_context(mm);
296 unsigned long asid = mm->context&MMU_CONTEXT_ASID_MASK;
297 unsigned long saved_asid = MMU_NO_ASID;
300 end += (PAGE_SIZE - 1);
302 if (mm != current->mm) {
303 saved_asid = get_asid();
306 while (start < end) {
307 __flush_tlb_page(asid, start);
310 if (saved_asid != MMU_NO_ASID)
311 set_asid(saved_asid);
313 local_irq_restore(flags);
317 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
322 local_irq_save(flags);
323 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
324 if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
327 unsigned long asid = init_mm.context&MMU_CONTEXT_ASID_MASK;
328 unsigned long saved_asid = get_asid();
331 end += (PAGE_SIZE - 1);
334 while (start < end) {
335 __flush_tlb_page(asid, start);
338 set_asid(saved_asid);
340 local_irq_restore(flags);
343 void flush_tlb_mm(struct mm_struct *mm)
345 /* Invalidate all TLB of this process. */
346 /* Instead of invalidating each TLB, we get new MMU context. */
347 if (mm->context != NO_CONTEXT) {
350 local_irq_save(flags);
351 mm->context = NO_CONTEXT;
352 if (mm == current->mm)
353 activate_context(mm);
354 local_irq_restore(flags);
358 void flush_tlb_all(void)
360 unsigned long flags, status;
365 * Write to the MMU control register's bit:
366 * TF-bit for SH-3, TI-bit for SH-4.
367 * It's same position, bit #2.
369 local_irq_save(flags);
370 status = ctrl_inl(MMUCR);
372 ctrl_outl(status, MMUCR);
373 local_irq_restore(flags);