This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / sh64 / mm / fault.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * arch/sh64/mm/fault.c
7  *
8  * Copyright (C) 2000, 2001  Paolo Alberelli
9  * Copyright (C) 2003  Richard Curnow (/proc/tlb, bug fixes)
10  * Copyright (C) 2003  Paul Mundt
11  *
12  */
13
14 #include <linux/signal.h>
15 #include <linux/rwsem.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/types.h>
21 #include <linux/ptrace.h>
22 #include <linux/mman.h>
23 #include <linux/mm.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/interrupt.h>
27
28 #include <asm/system.h>
29 #include <asm/io.h>
30 #include <asm/tlb.h>
31 #include <asm/uaccess.h>
32 #include <asm/pgalloc.h>
33 #include <asm/hardirq.h>
34 #include <asm/mmu_context.h>
35 #include <asm/registers.h>              /* required by inline asm statements */
36
37 #if defined(CONFIG_SH64_PROC_TLB)
38 #include <linux/init.h>
39 #include <linux/proc_fs.h>
40 /* Count numbers of tlb refills in each region */
41 static unsigned long long calls_to_update_mmu_cache = 0ULL;
42 static unsigned long long calls_to_flush_tlb_page   = 0ULL;
43 static unsigned long long calls_to_flush_tlb_range  = 0ULL;
44 static unsigned long long calls_to_flush_tlb_mm     = 0ULL;
45 static unsigned long long calls_to_flush_tlb_all    = 0ULL;
46 unsigned long long calls_to_do_slow_page_fault = 0ULL;
47 unsigned long long calls_to_do_fast_page_fault = 0ULL;
48
49 /* Count size of ranges for flush_tlb_range */
50 static unsigned long long flush_tlb_range_1         = 0ULL;
51 static unsigned long long flush_tlb_range_2         = 0ULL;
52 static unsigned long long flush_tlb_range_3_4       = 0ULL;
53 static unsigned long long flush_tlb_range_5_7       = 0ULL;
54 static unsigned long long flush_tlb_range_8_11      = 0ULL;
55 static unsigned long long flush_tlb_range_12_15     = 0ULL;
56 static unsigned long long flush_tlb_range_16_up     = 0ULL;
57
58 static unsigned long long page_not_present          = 0ULL;
59
60 #endif
61
62 extern void die(const char *,struct pt_regs *,long);
63
64 #define PFLAG(val,flag)   (( (val) & (flag) ) ? #flag : "" )
65 #define PPROT(flag) PFLAG(pgprot_val(prot),flag)
66
67 static inline void print_prots(pgprot_t prot)
68 {
69         printk("prot is 0x%08lx\n",pgprot_val(prot));
70
71         printk("%s %s %s %s %s\n",PPROT(_PAGE_SHARED),PPROT(_PAGE_READ),
72                PPROT(_PAGE_EXECUTE),PPROT(_PAGE_WRITE),PPROT(_PAGE_USER));
73 }
74
75 static inline void print_vma(struct vm_area_struct *vma)
76 {
77         printk("vma start 0x%08lx\n", vma->vm_start);
78         printk("vma end   0x%08lx\n", vma->vm_end);
79
80         print_prots(vma->vm_page_prot);
81         printk("vm_flags 0x%08lx\n", vma->vm_flags);
82 }
83
84 static inline void print_task(struct task_struct *tsk)
85 {
86         printk("Task pid %d\n", tsk->pid);
87 }
88
89 static pte_t *lookup_pte(struct mm_struct *mm, unsigned long address)
90 {
91         pgd_t *dir;
92         pmd_t *pmd;
93         pte_t *pte;
94         pte_t entry;
95
96         dir = pgd_offset(mm, address);
97         if (pgd_none(*dir)) {
98                 return NULL;
99         }
100
101         pmd = pmd_offset(dir, address);
102         if (pmd_none(*pmd)) {
103                 return NULL;
104         }
105
106         pte = pte_offset_kernel(pmd, address);
107         entry = *pte;
108
109         if (pte_none(entry)) {
110                 return NULL;
111         }
112         if (!pte_present(entry)) {
113                 return NULL;
114         }
115
116         return pte;
117 }
118
119 /*
120  * This routine handles page faults.  It determines the address,
121  * and the problem, and then passes it off to one of the appropriate
122  * routines.
123  */
124 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
125                               unsigned long textaccess, unsigned long address)
126 {
127         struct task_struct *tsk;
128         struct mm_struct *mm;
129         struct vm_area_struct * vma;
130         const struct exception_table_entry *fixup;
131         pte_t *pte;
132
133 #if defined(CONFIG_SH64_PROC_TLB)
134         ++calls_to_do_slow_page_fault;
135 #endif
136
137         /* SIM
138          * Note this is now called with interrupts still disabled
139          * This is to cope with being called for a missing IO port
140          * address with interupts disabled. This should be fixed as
141          * soon as we have a better 'fast path' miss handler.
142          *
143          * Plus take care how you try and debug this stuff.
144          * For example, writing debug data to a port which you
145          * have just faulted on is not going to work.
146          */
147
148         tsk = current;
149         mm = tsk->mm;
150
151         /* Not an IO address, so reenable interrupts */
152         sti();
153
154         /*
155          * If we're in an interrupt or have no user
156          * context, we must not take the fault..
157          */
158         if (in_interrupt() || !mm)
159                 goto no_context;
160
161         /* TLB misses upon some cache flushes get done under cli() */
162         down_read(&mm->mmap_sem);
163
164         vma = find_vma(mm, address);
165
166         if (!vma) {
167 #ifdef DEBUG_FAULT
168                 print_task(tsk);
169                 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
170                        __FUNCTION__,__LINE__,
171                        address,regs->pc,textaccess,writeaccess);
172                 show_regs(regs);
173 #endif
174                 goto bad_area;
175         }
176         if (vma->vm_start <= address) {
177                 goto good_area;
178         }
179
180         if (!(vma->vm_flags & VM_GROWSDOWN)) {
181 #ifdef DEBUG_FAULT
182                 print_task(tsk);
183                 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
184                        __FUNCTION__,__LINE__,
185                        address,regs->pc,textaccess,writeaccess);
186                 show_regs(regs);
187
188                 print_vma(vma);
189 #endif
190                 goto bad_area;
191         }
192         if (expand_stack(vma, address)) {
193 #ifdef DEBUG_FAULT
194                 print_task(tsk);
195                 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
196                        __FUNCTION__,__LINE__,
197                        address,regs->pc,textaccess,writeaccess);
198                 show_regs(regs);
199 #endif
200                 goto bad_area;
201         }
202 /*
203  * Ok, we have a good vm_area for this memory access, so
204  * we can handle it..
205  */
206 good_area:
207         if (writeaccess) {
208                 if (!(vma->vm_flags & VM_WRITE))
209                         goto bad_area;
210         } else {
211                 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
212                         goto bad_area;
213         }
214
215         if (textaccess) {
216                 if (!(vma->vm_flags & VM_EXEC))
217                         goto bad_area;
218         }
219
220         /*
221          * If for any reason at all we couldn't handle the fault,
222          * make sure we exit gracefully rather than endlessly redo
223          * the fault.
224          */
225 survive:
226         switch (handle_mm_fault(mm, vma, address, writeaccess)) {
227         case 1:
228                 tsk->min_flt++;
229                 break;
230         case 2:
231                 tsk->maj_flt++;
232                 break;
233         case 0:
234                 goto do_sigbus;
235         default:
236                 goto out_of_memory;
237         }
238         /* If we get here, the page fault has been handled.  Do the TLB refill
239            now from the newly-setup PTE, to avoid having to fault again right
240            away on the same instruction. */
241         pte = lookup_pte (mm, address);
242         if (!pte) {
243                 /* From empirical evidence, we can get here, due to
244                    !pte_present(pte).  (e.g. if a swap-in occurs, and the page
245                    is swapped back out again before the process that wanted it
246                    gets rescheduled?) */
247                 goto no_pte;
248         }
249
250         __do_tlb_refill(address, textaccess, pte);
251
252 no_pte:
253
254         up_read(&mm->mmap_sem);
255         return;
256
257 /*
258  * Something tried to access memory that isn't in our memory map..
259  * Fix it, but check if it's kernel or user first..
260  */
261 bad_area:
262 #ifdef DEBUG_FAULT
263         printk("fault:bad area\n");
264 #endif
265         up_read(&mm->mmap_sem);
266
267         if (user_mode(regs)) {
268                 printk("user mode bad_area address=%08lx pid=%d (%s) pc=%08lx opcode=%08lx\n",
269                         address, current->pid, current->comm,
270                         (unsigned long) regs->pc,
271                         *(unsigned long *)(u32)(regs->pc & ~3));
272                 show_regs(regs);
273                 if (tsk->pid == 1) {
274                         panic("INIT had user mode bad_area\n");
275                 }
276                 tsk->thread.address = address;
277                 tsk->thread.error_code = writeaccess;
278                 force_sig(SIGSEGV, tsk);
279                 return;
280         }
281
282 no_context:
283 #ifdef DEBUG_FAULT
284         printk("fault:No context\n");
285 #endif
286         /* Are we prepared to handle this kernel fault?  */
287         fixup = search_exception_tables(regs->pc);
288         if (fixup) {
289                 regs->pc = fixup->fixup;
290                 return;
291         }
292
293 /*
294  * Oops. The kernel tried to access some bad page. We'll have to
295  * terminate things with extreme prejudice.
296  *
297  */
298         if (address < PAGE_SIZE)
299                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
300         else
301                 printk(KERN_ALERT "Unable to handle kernel paging request");
302         printk(" at virtual address %08lx\n", address);
303         printk(KERN_ALERT "pc = %08Lx%08Lx\n", regs->pc >> 32, regs->pc & 0xffffffff);
304         die("Oops", regs, writeaccess);
305         do_exit(SIGKILL);
306
307 /*
308  * We ran out of memory, or some other thing happened to us that made
309  * us unable to handle the page fault gracefully.
310  */
311 out_of_memory:
312         if (current->pid == 1) {
313                 panic("INIT out of memory\n");
314                 yield();
315                 goto survive;
316         }
317         printk("fault:Out of memory\n");
318         up_read(&mm->mmap_sem);
319         if (current->pid == 1) {
320                 yield();
321                 down_read(&mm->mmap_sem);
322                 goto survive;
323         }
324         printk("VM: killing process %s\n", tsk->comm);
325         if (user_mode(regs))
326                 do_exit(SIGKILL);
327         goto no_context;
328
329 do_sigbus:
330         printk("fault:Do sigbus\n");
331         up_read(&mm->mmap_sem);
332
333         /*
334          * Send a sigbus, regardless of whether we were in kernel
335          * or user mode.
336          */
337         tsk->thread.address = address;
338         tsk->thread.error_code = writeaccess;
339         tsk->thread.trap_no = 14;
340         force_sig(SIGBUS, tsk);
341
342         /* Kernel mode? Handle exceptions or die */
343         if (!user_mode(regs))
344                 goto no_context;
345 }
346
347
348 void flush_tlb_all(void);
349
350 void update_mmu_cache(struct vm_area_struct * vma,
351                         unsigned long address, pte_t pte)
352 {
353 #if defined(CONFIG_SH64_PROC_TLB)
354         ++calls_to_update_mmu_cache;
355 #endif
356
357         /*
358          * This appears to get called once for every pte entry that gets
359          * established => I don't think it's efficient to try refilling the
360          * TLBs with the pages - some may not get accessed even.  Also, for
361          * executable pages, it is impossible to determine reliably here which
362          * TLB they should be mapped into (or both even).
363          *
364          * So, just do nothing here and handle faults on demand.  In the
365          * TLBMISS handling case, the refill is now done anyway after the pte
366          * has been fixed up, so that deals with most useful cases.
367          */
368 }
369
370 static void __flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
371 {
372         unsigned long long match, pteh=0, lpage;
373         unsigned long tlb;
374         struct mm_struct *mm;
375
376         mm = vma->vm_mm;
377
378         if (mm->context == NO_CONTEXT)
379                 return;
380
381         /*
382          * Sign-extend based on neff.
383          */
384         lpage = (page & NEFF_SIGN) ? (page | NEFF_MASK) : page;
385         match = ((mm->context & MMU_CONTEXT_ASID_MASK) << PTEH_ASID_SHIFT) | PTEH_VALID;
386         match |= lpage;
387
388         /* Do ITLB : don't bother for pages in non-exectutable VMAs */
389         if (vma->vm_flags & VM_EXEC) {
390                 for_each_itlb_entry(tlb) {
391                         asm volatile ("getcfg   %1, 0, %0"
392                                       : "=r" (pteh)
393                                       : "r" (tlb) );
394
395                         if (pteh == match) {
396                                 __flush_tlb_slot(tlb);
397                                 break;
398                         }
399
400                 }
401         }
402
403         /* Do DTLB : any page could potentially be in here. */
404         for_each_dtlb_entry(tlb) {
405                 asm volatile ("getcfg   %1, 0, %0"
406                               : "=r" (pteh)
407                               : "r" (tlb) );
408
409                 if (pteh == match) {
410                         __flush_tlb_slot(tlb);
411                         break;
412                 }
413
414         }
415 }
416
417 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
418 {
419         unsigned long flags;
420
421 #if defined(CONFIG_SH64_PROC_TLB)
422         ++calls_to_flush_tlb_page;
423 #endif
424
425         if (vma->vm_mm) {
426                 page &= PAGE_MASK;
427                 local_irq_save(flags);
428                 __flush_tlb_page(vma, page);
429                 local_irq_restore(flags);
430         }
431 }
432
433 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
434                      unsigned long end)
435 {
436         unsigned long flags;
437         unsigned long long match, pteh=0, pteh_epn, pteh_low;
438         unsigned long tlb;
439         struct mm_struct *mm;
440
441         mm = vma->vm_mm;
442
443 #if defined(CONFIG_SH64_PROC_TLB)
444         ++calls_to_flush_tlb_range;
445
446         {
447                 unsigned long size = (end - 1) - start;
448                 size >>= 12; /* divide by PAGE_SIZE */
449                 size++; /* end=start+4096 => 1 page */
450                 switch (size) {
451                   case  1        : flush_tlb_range_1++;     break;
452                   case  2        : flush_tlb_range_2++;     break;
453                   case  3 ...  4 : flush_tlb_range_3_4++;   break;
454                   case  5 ...  7 : flush_tlb_range_5_7++;   break;
455                   case  8 ... 11 : flush_tlb_range_8_11++;  break;
456                   case 12 ... 15 : flush_tlb_range_12_15++; break;
457                   default        : flush_tlb_range_16_up++; break;
458                 }
459         }
460 #endif
461
462         if (mm->context == NO_CONTEXT)
463                 return;
464
465         local_irq_save(flags);
466
467         start &= PAGE_MASK;
468         end &= PAGE_MASK;
469
470         match = ((mm->context & MMU_CONTEXT_ASID_MASK) << PTEH_ASID_SHIFT) | PTEH_VALID;
471
472         /* Flush ITLB */
473         for_each_itlb_entry(tlb) {
474                 asm volatile ("getcfg   %1, 0, %0"
475                               : "=r" (pteh)
476                               : "r" (tlb) );
477
478                 pteh_epn = pteh & PAGE_MASK;
479                 pteh_low = pteh & ~PAGE_MASK;
480
481                 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
482                         __flush_tlb_slot(tlb);
483         }
484
485         /* Flush DTLB */
486         for_each_dtlb_entry(tlb) {
487                 asm volatile ("getcfg   %1, 0, %0"
488                               : "=r" (pteh)
489                               : "r" (tlb) );
490
491                 pteh_epn = pteh & PAGE_MASK;
492                 pteh_low = pteh & ~PAGE_MASK;
493
494                 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
495                         __flush_tlb_slot(tlb);
496         }
497
498         local_irq_restore(flags);
499 }
500
501 void flush_tlb_mm(struct mm_struct *mm)
502 {
503         unsigned long flags;
504
505 #if defined(CONFIG_SH64_PROC_TLB)
506         ++calls_to_flush_tlb_mm;
507 #endif
508
509         if (mm->context == NO_CONTEXT)
510                 return;
511
512         local_irq_save(flags);
513
514         mm->context=NO_CONTEXT;
515         if(mm==current->mm)
516                 activate_context(mm);
517
518         local_irq_restore(flags);
519
520 }
521
522 void flush_tlb_all(void)
523 {
524         /* Invalidate all, including shared pages, excluding fixed TLBs */
525
526         unsigned long flags, tlb;
527
528 #if defined(CONFIG_SH64_PROC_TLB)
529         ++calls_to_flush_tlb_all;
530 #endif
531
532         local_irq_save(flags);
533
534         /* Flush each ITLB entry */
535         for_each_itlb_entry(tlb) {
536                 __flush_tlb_slot(tlb);
537         }
538
539         /* Flush each DTLB entry */
540         for_each_dtlb_entry(tlb) {
541                 __flush_tlb_slot(tlb);
542         }
543
544         local_irq_restore(flags);
545 }
546
547 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
548 {
549         /* FIXME: Optimize this later.. */
550         flush_tlb_all();
551 }
552
553 #if defined(CONFIG_SH64_PROC_TLB)
554 /* Procfs interface to read the performance information */
555
556 static int
557 tlb_proc_info(char *buf, char **start, off_t fpos, int length, int *eof, void *data)
558 {
559   int len=0;
560   len += sprintf(buf+len, "do_fast_page_fault   called %12lld times\n", calls_to_do_fast_page_fault);
561   len += sprintf(buf+len, "do_slow_page_fault   called %12lld times\n", calls_to_do_slow_page_fault);
562   len += sprintf(buf+len, "update_mmu_cache     called %12lld times\n", calls_to_update_mmu_cache);
563   len += sprintf(buf+len, "flush_tlb_page       called %12lld times\n", calls_to_flush_tlb_page);
564   len += sprintf(buf+len, "flush_tlb_range      called %12lld times\n", calls_to_flush_tlb_range);
565   len += sprintf(buf+len, "flush_tlb_mm         called %12lld times\n", calls_to_flush_tlb_mm);
566   len += sprintf(buf+len, "flush_tlb_all        called %12lld times\n", calls_to_flush_tlb_all);
567   len += sprintf(buf+len, "flush_tlb_range_sizes\n"
568                           " 1      : %12lld\n"
569                           " 2      : %12lld\n"
570                           " 3 -  4 : %12lld\n"
571                           " 5 -  7 : %12lld\n"
572                           " 8 - 11 : %12lld\n"
573                           "12 - 15 : %12lld\n"
574                           "16+     : %12lld\n",
575                           flush_tlb_range_1, flush_tlb_range_2, flush_tlb_range_3_4,
576                           flush_tlb_range_5_7, flush_tlb_range_8_11, flush_tlb_range_12_15,
577                           flush_tlb_range_16_up);
578   len += sprintf(buf+len, "page not present           %12lld times\n", page_not_present);
579   *eof = 1;
580   return len;
581 }
582
583 static int __init register_proc_tlb(void)
584 {
585   create_proc_read_entry("tlb", 0, NULL, tlb_proc_info, NULL);
586   return 0;
587 }
588
589 __initcall(register_proc_tlb);
590
591 #endif