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
8 * Copyright (C) 2000, 2001 Paolo Alberelli
9 * Copyright (C) 2003 Richard Curnow (/proc/tlb, bug fixes)
10 * Copyright (C) 2003 Paul Mundt
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>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/interrupt.h>
28 #include <asm/system.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 */
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;
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;
58 static unsigned long long page_not_present = 0ULL;
62 extern void die(const char *,struct pt_regs *,long);
64 #define PFLAG(val,flag) (( (val) & (flag) ) ? #flag : "" )
65 #define PPROT(flag) PFLAG(pgprot_val(prot),flag)
67 static inline void print_prots(pgprot_t prot)
69 printk("prot is 0x%08lx\n",pgprot_val(prot));
71 printk("%s %s %s %s %s\n",PPROT(_PAGE_SHARED),PPROT(_PAGE_READ),
72 PPROT(_PAGE_EXECUTE),PPROT(_PAGE_WRITE),PPROT(_PAGE_USER));
75 static inline void print_vma(struct vm_area_struct *vma)
77 printk("vma start 0x%08lx\n", vma->vm_start);
78 printk("vma end 0x%08lx\n", vma->vm_end);
80 print_prots(vma->vm_page_prot);
81 printk("vm_flags 0x%08lx\n", vma->vm_flags);
84 static inline void print_task(struct task_struct *tsk)
86 printk("Task pid %d\n", tsk->pid);
89 static pte_t *lookup_pte(struct mm_struct *mm, unsigned long address)
96 dir = pgd_offset(mm, address);
101 pmd = pmd_offset(dir, address);
102 if (pmd_none(*pmd)) {
106 pte = pte_offset_kernel(pmd, address);
109 if (pte_none(entry)) {
112 if (!pte_present(entry)) {
120 * This routine handles page faults. It determines the address,
121 * and the problem, and then passes it off to one of the appropriate
124 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
125 unsigned long textaccess, unsigned long address)
127 struct task_struct *tsk;
128 struct mm_struct *mm;
129 struct vm_area_struct * vma;
130 const struct exception_table_entry *fixup;
133 #if defined(CONFIG_SH64_PROC_TLB)
134 ++calls_to_do_slow_page_fault;
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.
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.
151 /* Not an IO address, so reenable interrupts */
155 * If we're in an interrupt or have no user
156 * context, we must not take the fault..
158 if (in_interrupt() || !mm)
161 /* TLB misses upon some cache flushes get done under cli() */
162 down_read(&mm->mmap_sem);
164 vma = find_vma(mm, address);
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);
176 if (vma->vm_start <= address) {
180 if (!(vma->vm_flags & VM_GROWSDOWN)) {
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);
192 if (expand_stack(vma, address)) {
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);
203 * Ok, we have a good vm_area for this memory access, so
208 if (!(vma->vm_flags & VM_WRITE))
211 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
216 if (!(vma->vm_flags & VM_EXEC))
221 * If for any reason at all we couldn't handle the fault,
222 * make sure we exit gracefully rather than endlessly redo
226 switch (handle_mm_fault(mm, vma, address, writeaccess)) {
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);
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?) */
250 __do_tlb_refill(address, textaccess, pte);
254 up_read(&mm->mmap_sem);
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..
263 printk("fault:bad area\n");
265 up_read(&mm->mmap_sem);
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));
274 panic("INIT had user mode bad_area\n");
276 tsk->thread.address = address;
277 tsk->thread.error_code = writeaccess;
278 force_sig(SIGSEGV, tsk);
284 printk("fault:No context\n");
286 /* Are we prepared to handle this kernel fault? */
287 fixup = search_exception_tables(regs->pc);
289 regs->pc = fixup->fixup;
294 * Oops. The kernel tried to access some bad page. We'll have to
295 * terminate things with extreme prejudice.
298 if (address < PAGE_SIZE)
299 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
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);
308 * We ran out of memory, or some other thing happened to us that made
309 * us unable to handle the page fault gracefully.
312 if (current->pid == 1) {
313 panic("INIT out of memory\n");
317 printk("fault:Out of memory\n");
318 up_read(&mm->mmap_sem);
319 if (current->pid == 1) {
321 down_read(&mm->mmap_sem);
324 printk("VM: killing process %s\n", tsk->comm);
330 printk("fault:Do sigbus\n");
331 up_read(&mm->mmap_sem);
334 * Send a sigbus, regardless of whether we were in kernel
337 tsk->thread.address = address;
338 tsk->thread.error_code = writeaccess;
339 tsk->thread.trap_no = 14;
340 force_sig(SIGBUS, tsk);
342 /* Kernel mode? Handle exceptions or die */
343 if (!user_mode(regs))
348 void flush_tlb_all(void);
350 void update_mmu_cache(struct vm_area_struct * vma,
351 unsigned long address, pte_t pte)
353 #if defined(CONFIG_SH64_PROC_TLB)
354 ++calls_to_update_mmu_cache;
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).
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.
370 static void __flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
372 unsigned long long match, pteh=0, lpage;
374 struct mm_struct *mm;
378 if (mm->context == NO_CONTEXT)
382 * Sign-extend based on neff.
384 lpage = (page & NEFF_SIGN) ? (page | NEFF_MASK) : page;
385 match = ((mm->context & MMU_CONTEXT_ASID_MASK) << PTEH_ASID_SHIFT) | PTEH_VALID;
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"
396 __flush_tlb_slot(tlb);
403 /* Do DTLB : any page could potentially be in here. */
404 for_each_dtlb_entry(tlb) {
405 asm volatile ("getcfg %1, 0, %0"
410 __flush_tlb_slot(tlb);
417 void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
421 #if defined(CONFIG_SH64_PROC_TLB)
422 ++calls_to_flush_tlb_page;
427 local_irq_save(flags);
428 __flush_tlb_page(vma, page);
429 local_irq_restore(flags);
433 void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
437 unsigned long long match, pteh=0, pteh_epn, pteh_low;
439 struct mm_struct *mm;
443 #if defined(CONFIG_SH64_PROC_TLB)
444 ++calls_to_flush_tlb_range;
447 unsigned long size = (end - 1) - start;
448 size >>= 12; /* divide by PAGE_SIZE */
449 size++; /* end=start+4096 => 1 page */
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;
462 if (mm->context == NO_CONTEXT)
465 local_irq_save(flags);
470 match = ((mm->context & MMU_CONTEXT_ASID_MASK) << PTEH_ASID_SHIFT) | PTEH_VALID;
473 for_each_itlb_entry(tlb) {
474 asm volatile ("getcfg %1, 0, %0"
478 pteh_epn = pteh & PAGE_MASK;
479 pteh_low = pteh & ~PAGE_MASK;
481 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
482 __flush_tlb_slot(tlb);
486 for_each_dtlb_entry(tlb) {
487 asm volatile ("getcfg %1, 0, %0"
491 pteh_epn = pteh & PAGE_MASK;
492 pteh_low = pteh & ~PAGE_MASK;
494 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
495 __flush_tlb_slot(tlb);
498 local_irq_restore(flags);
501 void flush_tlb_mm(struct mm_struct *mm)
505 #if defined(CONFIG_SH64_PROC_TLB)
506 ++calls_to_flush_tlb_mm;
509 if (mm->context == NO_CONTEXT)
512 local_irq_save(flags);
514 mm->context=NO_CONTEXT;
516 activate_context(mm);
518 local_irq_restore(flags);
522 void flush_tlb_all(void)
524 /* Invalidate all, including shared pages, excluding fixed TLBs */
526 unsigned long flags, tlb;
528 #if defined(CONFIG_SH64_PROC_TLB)
529 ++calls_to_flush_tlb_all;
532 local_irq_save(flags);
534 /* Flush each ITLB entry */
535 for_each_itlb_entry(tlb) {
536 __flush_tlb_slot(tlb);
539 /* Flush each DTLB entry */
540 for_each_dtlb_entry(tlb) {
541 __flush_tlb_slot(tlb);
544 local_irq_restore(flags);
547 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
549 /* FIXME: Optimize this later.. */
553 #if defined(CONFIG_SH64_PROC_TLB)
554 /* Procfs interface to read the performance information */
557 tlb_proc_info(char *buf, char **start, off_t fpos, int length, int *eof, void *data)
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"
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);
583 static int __init register_proc_tlb(void)
585 create_proc_read_entry("tlb", 0, NULL, tlb_proc_info, NULL);
589 __initcall(register_proc_tlb);