ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sh / mm / fault.c
1 /* $Id: fault.c,v 1.14 2004/01/13 05:52:11 kkojima Exp $
2  *
3  *  linux/arch/sh/mm/fault.c
4  *  Copyright (C) 1999  Niibe Yutaka
5  *  Copyright (C) 2003  Paul Mundt
6  *
7  *  Based on linux/arch/i386/mm/fault.c:
8  *   Copyright (C) 1995  Linus Torvalds
9  */
10
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>
19 #include <linux/mm.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24
25 #include <asm/system.h>
26 #include <asm/io.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>
32 #include <asm/kgdb.h>
33
34 extern void die(const char *,struct pt_regs *,long);
35
36 /*
37  * This routine handles page faults.  It determines the address,
38  * and the problem, and then passes it off to one of the appropriate
39  * routines.
40  */
41 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
42                               unsigned long address)
43 {
44         struct task_struct *tsk;
45         struct mm_struct *mm;
46         struct vm_area_struct * vma;
47         unsigned long page;
48
49 #ifdef CONFIG_SH_KGDB
50         if (kgdb_nofault && kgdb_bus_err_hook)
51                 kgdb_bus_err_hook();
52 #endif
53
54         tsk = current;
55         mm = tsk->mm;
56
57         /*
58          * If we're in an interrupt or have no user
59          * context, we must not take the fault..
60          */
61         if (in_atomic() || !mm)
62                 goto no_context;
63
64         down_read(&mm->mmap_sem);
65
66         vma = find_vma(mm, address);
67         if (!vma)
68                 goto bad_area;
69         if (vma->vm_start <= address)
70                 goto good_area;
71         if (!(vma->vm_flags & VM_GROWSDOWN))
72                 goto bad_area;
73         if (expand_stack(vma, address))
74                 goto bad_area;
75 /*
76  * Ok, we have a good vm_area for this memory access, so
77  * we can handle it..
78  */
79 good_area:
80         if (writeaccess) {
81                 if (!(vma->vm_flags & VM_WRITE))
82                         goto bad_area;
83         } else {
84                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
85                         goto bad_area;
86         }
87
88         /*
89          * If for any reason at all we couldn't handle the fault,
90          * make sure we exit gracefully rather than endlessly redo
91          * the fault.
92          */
93 survive:
94         switch (handle_mm_fault(mm, vma, address, writeaccess)) {
95                 case VM_FAULT_MINOR:
96                         tsk->min_flt++;
97                         break;
98                 case VM_FAULT_MAJOR:
99                         tsk->maj_flt++;
100                         break;
101                 case VM_FAULT_SIGBUS:
102                         goto do_sigbus;
103                 case VM_FAULT_OOM:
104                         goto out_of_memory;
105                 default:
106                         BUG();
107         }
108
109         up_read(&mm->mmap_sem);
110         return;
111
112 /*
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..
115  */
116 bad_area:
117         up_read(&mm->mmap_sem);
118
119         if (user_mode(regs)) {
120                 tsk->thread.address = address;
121                 tsk->thread.error_code = writeaccess;
122                 force_sig(SIGSEGV, tsk);
123                 return;
124         }
125
126 no_context:
127         /* Are we prepared to handle this kernel fault?  */
128         if (fixup_exception(regs))
129                 return;
130
131 /*
132  * Oops. The kernel tried to access some bad page. We'll have to
133  * terminate things with extreme prejudice.
134  *
135  */
136         if (address < PAGE_SIZE)
137                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
138         else
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"
143                      : "=r" (page)
144                      : "m" (__m(MMU_TTB)));
145         if (page) {
146                 page = ((unsigned long *) page)[address >> 22];
147                 printk(KERN_ALERT "*pde = %08lx\n", page);
148                 if (page & _PAGE_PRESENT) {
149                         page &= PAGE_MASK;
150                         address &= 0x003ff000;
151                         page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
152                         printk(KERN_ALERT "*pte = %08lx\n", page);
153                 }
154         }
155         die("Oops", regs, writeaccess);
156         do_exit(SIGKILL);
157
158 /*
159  * We ran out of memory, or some other thing happened to us that made
160  * us unable to handle the page fault gracefully.
161  */
162 out_of_memory:
163         up_read(&mm->mmap_sem);
164         if (current->pid == 1) {
165                 yield();
166                 down_read(&mm->mmap_sem);
167                 goto survive;
168         }
169         printk("VM: killing process %s\n", tsk->comm);
170         if (user_mode(regs))
171                 do_exit(SIGKILL);
172         goto no_context;
173
174 do_sigbus:
175         up_read(&mm->mmap_sem);
176
177         /*
178          * Send a sigbus, regardless of whether we were in kernel
179          * or user mode.
180          */
181         tsk->thread.address = address;
182         tsk->thread.error_code = writeaccess;
183         tsk->thread.trap_no = 14;
184         force_sig(SIGBUS, tsk);
185
186         /* Kernel mode? Handle exceptions or die */
187         if (!user_mode(regs))
188                 goto no_context;
189 }
190
191 /*
192  * Called with interrupt disabled.
193  */
194 asmlinkage int __do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
195                                unsigned long address)
196 {
197         unsigned long addrmax = P4SEG;
198         pgd_t *dir;
199         pmd_t *pmd;
200         pte_t *pte;
201         pte_t entry;
202
203 #ifdef CONFIG_SH_KGDB
204         if (kgdb_nofault && kgdb_bus_err_hook)
205                 kgdb_bus_err_hook();
206 #endif
207
208 #ifdef CONFIG_SH_STORE_QUEUES
209         addrmax = P4SEG_STORE_QUE + 0x04000000;
210 #endif
211
212         if (address >= P3SEG && address < addrmax)
213                 dir = pgd_offset_k(address);
214         else if (address >= TASK_SIZE)
215                 return 1;
216         else if (!current->mm)
217                 return 1;
218         else
219                 dir = pgd_offset(current->mm, address);
220
221         pmd = pmd_offset(dir, address);
222         if (pmd_none(*pmd))
223                 return 1;
224         if (pmd_bad(*pmd)) {
225                 pmd_ERROR(*pmd);
226                 pmd_clear(pmd);
227                 return 1;
228         }
229         pte = pte_offset_kernel(pmd, address);
230         entry = *pte;
231         if (pte_none(entry) || pte_not_present(entry)
232             || (writeaccess && !pte_write(entry)))
233                 return 1;
234
235         if (writeaccess)
236                 entry = pte_mkdirty(entry);
237         entry = pte_mkyoung(entry);
238
239 #ifdef CONFIG_CPU_SH4
240         /*
241          * ITLB is not affected by "ldtlb" instruction.
242          * So, we need to flush the entry by ourselves.
243          */
244
245         {
246                 unsigned long flags;
247                 local_irq_save(flags);
248                 __flush_tlb_page(get_asid(), address&PAGE_MASK);
249                 local_irq_restore(flags);
250         }
251 #endif
252
253         set_pte(pte, entry);
254         update_mmu_cache(NULL, address, entry);
255
256         return 0;
257 }
258
259 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
260 {
261         if (vma->vm_mm && vma->vm_mm->context != NO_CONTEXT) {
262                 unsigned long flags;
263                 unsigned long asid;
264                 unsigned long saved_asid = MMU_NO_ASID;
265
266                 asid = vma->vm_mm->context & MMU_CONTEXT_ASID_MASK;
267                 page &= PAGE_MASK;
268
269                 local_irq_save(flags);
270                 if (vma->vm_mm != current->mm) {
271                         saved_asid = get_asid();
272                         set_asid(asid);
273                 }
274                 __flush_tlb_page(asid, page);
275                 if (saved_asid != MMU_NO_ASID)
276                         set_asid(saved_asid);
277                 local_irq_restore(flags);
278         }
279 }
280
281 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
282                      unsigned long end)
283 {
284         struct mm_struct *mm = vma->vm_mm;
285
286         if (mm->context != NO_CONTEXT) {
287                 unsigned long flags;
288                 int size;
289
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);
296                 } else {
297                         unsigned long asid = mm->context&MMU_CONTEXT_ASID_MASK;
298                         unsigned long saved_asid = MMU_NO_ASID;
299
300                         start &= PAGE_MASK;
301                         end += (PAGE_SIZE - 1);
302                         end &= PAGE_MASK;
303                         if (mm != current->mm) {
304                                 saved_asid = get_asid();
305                                 set_asid(asid);
306                         }
307                         while (start < end) {
308                                 __flush_tlb_page(asid, start);
309                                 start += PAGE_SIZE;
310                         }
311                         if (saved_asid != MMU_NO_ASID)
312                                 set_asid(saved_asid);
313                 }
314                 local_irq_restore(flags);
315         }
316 }
317
318 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
319 {
320         unsigned long flags;
321         int size;
322
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 */
326                 flush_tlb_all();
327         } else {
328                 unsigned long asid = init_mm.context&MMU_CONTEXT_ASID_MASK;
329                 unsigned long saved_asid = get_asid();
330
331                 start &= PAGE_MASK;
332                 end += (PAGE_SIZE - 1);
333                 end &= PAGE_MASK;
334                 set_asid(asid);
335                 while (start < end) {
336                         __flush_tlb_page(asid, start);
337                         start += PAGE_SIZE;
338                 }
339                 set_asid(saved_asid);
340         }
341         local_irq_restore(flags);
342 }
343
344 void flush_tlb_mm(struct mm_struct *mm)
345 {
346         /* Invalidate all TLB of this process. */
347         /* Instead of invalidating each TLB, we get new MMU context. */
348         if (mm->context != NO_CONTEXT) {
349                 unsigned long flags;
350
351                 local_irq_save(flags);
352                 mm->context = NO_CONTEXT;
353                 if (mm == current->mm)
354                         activate_context(mm);
355                 local_irq_restore(flags);
356         }
357 }
358
359 void flush_tlb_all(void)
360 {
361         unsigned long flags, status;
362
363         /*
364          * Flush all the TLB.
365          *
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.
369          */
370         local_irq_save(flags);
371         status = ctrl_inl(MMUCR);
372         status |= 0x04;         
373         ctrl_outl(status, MMUCR);
374         local_irq_restore(flags);
375 }