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