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/mmu_context.h>
34 #include <asm/registers.h> /* required by inline asm statements */
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;
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;
57 static unsigned long long page_not_present
= 0ULL;
61 extern void die(const char *,struct pt_regs
*,long);
63 #define PFLAG(val,flag) (( (val) & (flag) ) ? #flag : "" )
64 #define PPROT(flag) PFLAG(pgprot_val(prot),flag)
66 static inline void print_prots(pgprot_t prot
)
68 printk("prot is 0x%08lx\n",pgprot_val(prot
));
70 printk("%s %s %s %s %s\n",PPROT(_PAGE_SHARED
),PPROT(_PAGE_READ
),
71 PPROT(_PAGE_EXECUTE
),PPROT(_PAGE_WRITE
),PPROT(_PAGE_USER
));
74 static inline void print_vma(struct vm_area_struct
*vma
)
76 printk("vma start 0x%08lx\n", vma
->vm_start
);
77 printk("vma end 0x%08lx\n", vma
->vm_end
);
79 print_prots(vma
->vm_page_prot
);
80 printk("vm_flags 0x%08lx\n", vma
->vm_flags
);
83 static inline void print_task(struct task_struct
*tsk
)
85 printk("Task pid %d\n", tsk
->pid
);
88 static pte_t
*lookup_pte(struct mm_struct
*mm
, unsigned long address
)
95 dir
= pgd_offset(mm
, address
);
100 pmd
= pmd_offset(dir
, address
);
101 if (pmd_none(*pmd
)) {
105 pte
= pte_offset_kernel(pmd
, address
);
108 if (pte_none(entry
)) {
111 if (!pte_present(entry
)) {
119 * This routine handles page faults. It determines the address,
120 * and the problem, and then passes it off to one of the appropriate
123 asmlinkage
void do_page_fault(struct pt_regs
*regs
, unsigned long writeaccess
,
124 unsigned long textaccess
, unsigned long address
)
126 struct task_struct
*tsk
;
127 struct mm_struct
*mm
;
128 struct vm_area_struct
* vma
;
129 const struct exception_table_entry
*fixup
;
132 #if defined(CONFIG_SH64_PROC_TLB)
133 ++calls_to_do_slow_page_fault
;
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.
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.
150 /* Not an IO address, so reenable interrupts */
154 * If we're in an interrupt or have no user
155 * context, we must not take the fault..
157 if (in_interrupt() || !mm
)
160 /* TLB misses upon some cache flushes get done under cli() */
161 down_read(&mm
->mmap_sem
);
163 vma
= find_vma(mm
, address
);
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
);
175 if (vma
->vm_start
<= address
) {
179 if (!(vma
->vm_flags
& VM_GROWSDOWN
)) {
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
);
191 if (expand_stack(vma
, address
)) {
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
);
202 * Ok, we have a good vm_area for this memory access, so
207 if (!(vma
->vm_flags
& VM_EXEC
))
211 if (!(vma
->vm_flags
& VM_WRITE
))
214 if (!(vma
->vm_flags
& VM_READ
))
220 * If for any reason at all we couldn't handle the fault,
221 * make sure we exit gracefully rather than endlessly redo
225 switch (handle_mm_fault(mm
, vma
, address
, writeaccess
)) {
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
);
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?) */
249 __do_tlb_refill(address
, textaccess
, pte
);
253 up_read(&mm
->mmap_sem
);
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..
262 printk("fault:bad area\n");
264 up_read(&mm
->mmap_sem
);
266 if (user_mode(regs
)) {
270 /* This is really to help debug faults when starting
271 * usermode, so only need a few */
273 printk("user mode bad_area address=%08lx pid=%d (%s) pc=%08lx\n",
274 address
, current
->pid
, current
->comm
,
275 (unsigned long) regs
->pc
);
281 panic("INIT had user mode bad_area\n");
283 tsk
->thread
.address
= address
;
284 tsk
->thread
.error_code
= writeaccess
;
285 info
.si_signo
= SIGSEGV
;
287 info
.si_addr
= (void *) address
;
288 force_sig_info(SIGSEGV
, &info
, tsk
);
294 printk("fault:No context\n");
296 /* Are we prepared to handle this kernel fault? */
297 fixup
= search_exception_tables(regs
->pc
);
299 regs
->pc
= fixup
->fixup
;
304 * Oops. The kernel tried to access some bad page. We'll have to
305 * terminate things with extreme prejudice.
308 if (address
< PAGE_SIZE
)
309 printk(KERN_ALERT
"Unable to handle kernel NULL pointer dereference");
311 printk(KERN_ALERT
"Unable to handle kernel paging request");
312 printk(" at virtual address %08lx\n", address
);
313 printk(KERN_ALERT
"pc = %08Lx%08Lx\n", regs
->pc
>> 32, regs
->pc
& 0xffffffff);
314 die("Oops", regs
, writeaccess
);
318 * We ran out of memory, or some other thing happened to us that made
319 * us unable to handle the page fault gracefully.
322 if (current
->pid
== 1) {
323 panic("INIT out of memory\n");
327 printk("fault:Out of memory\n");
328 up_read(&mm
->mmap_sem
);
329 if (current
->pid
== 1) {
331 down_read(&mm
->mmap_sem
);
334 printk("VM: killing process %s\n", tsk
->comm
);
340 printk("fault:Do sigbus\n");
341 up_read(&mm
->mmap_sem
);
344 * Send a sigbus, regardless of whether we were in kernel
347 tsk
->thread
.address
= address
;
348 tsk
->thread
.error_code
= writeaccess
;
349 tsk
->thread
.trap_no
= 14;
350 force_sig(SIGBUS
, tsk
);
352 /* Kernel mode? Handle exceptions or die */
353 if (!user_mode(regs
))
358 void flush_tlb_all(void);
360 void update_mmu_cache(struct vm_area_struct
* vma
,
361 unsigned long address
, pte_t pte
)
363 #if defined(CONFIG_SH64_PROC_TLB)
364 ++calls_to_update_mmu_cache
;
368 * This appears to get called once for every pte entry that gets
369 * established => I don't think it's efficient to try refilling the
370 * TLBs with the pages - some may not get accessed even. Also, for
371 * executable pages, it is impossible to determine reliably here which
372 * TLB they should be mapped into (or both even).
374 * So, just do nothing here and handle faults on demand. In the
375 * TLBMISS handling case, the refill is now done anyway after the pte
376 * has been fixed up, so that deals with most useful cases.
380 static void __flush_tlb_page(struct vm_area_struct
*vma
, unsigned long page
)
382 unsigned long long match
, pteh
=0, lpage
;
384 struct mm_struct
*mm
;
388 if (mm
->context
== NO_CONTEXT
)
392 * Sign-extend based on neff.
394 lpage
= (page
& NEFF_SIGN
) ? (page
| NEFF_MASK
) : page
;
395 match
= ((mm
->context
& MMU_CONTEXT_ASID_MASK
) << PTEH_ASID_SHIFT
) | PTEH_VALID
;
398 /* Do ITLB : don't bother for pages in non-exectutable VMAs */
399 if (vma
->vm_flags
& VM_EXEC
) {
400 for_each_itlb_entry(tlb
) {
401 asm volatile ("getcfg %1, 0, %0"
406 __flush_tlb_slot(tlb
);
413 /* Do DTLB : any page could potentially be in here. */
414 for_each_dtlb_entry(tlb
) {
415 asm volatile ("getcfg %1, 0, %0"
420 __flush_tlb_slot(tlb
);
427 void flush_tlb_page(struct vm_area_struct
*vma
, unsigned long page
)
431 #if defined(CONFIG_SH64_PROC_TLB)
432 ++calls_to_flush_tlb_page
;
437 local_irq_save(flags
);
438 __flush_tlb_page(vma
, page
);
439 local_irq_restore(flags
);
443 void flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
,
447 unsigned long long match
, pteh
=0, pteh_epn
, pteh_low
;
449 struct mm_struct
*mm
;
453 #if defined(CONFIG_SH64_PROC_TLB)
454 ++calls_to_flush_tlb_range
;
457 unsigned long size
= (end
- 1) - start
;
458 size
>>= 12; /* divide by PAGE_SIZE */
459 size
++; /* end=start+4096 => 1 page */
461 case 1 : flush_tlb_range_1
++; break;
462 case 2 : flush_tlb_range_2
++; break;
463 case 3 ... 4 : flush_tlb_range_3_4
++; break;
464 case 5 ... 7 : flush_tlb_range_5_7
++; break;
465 case 8 ... 11 : flush_tlb_range_8_11
++; break;
466 case 12 ... 15 : flush_tlb_range_12_15
++; break;
467 default : flush_tlb_range_16_up
++; break;
472 if (mm
->context
== NO_CONTEXT
)
475 local_irq_save(flags
);
480 match
= ((mm
->context
& MMU_CONTEXT_ASID_MASK
) << PTEH_ASID_SHIFT
) | PTEH_VALID
;
483 for_each_itlb_entry(tlb
) {
484 asm volatile ("getcfg %1, 0, %0"
488 pteh_epn
= pteh
& PAGE_MASK
;
489 pteh_low
= pteh
& ~PAGE_MASK
;
491 if (pteh_low
== match
&& pteh_epn
>= start
&& pteh_epn
<= end
)
492 __flush_tlb_slot(tlb
);
496 for_each_dtlb_entry(tlb
) {
497 asm volatile ("getcfg %1, 0, %0"
501 pteh_epn
= pteh
& PAGE_MASK
;
502 pteh_low
= pteh
& ~PAGE_MASK
;
504 if (pteh_low
== match
&& pteh_epn
>= start
&& pteh_epn
<= end
)
505 __flush_tlb_slot(tlb
);
508 local_irq_restore(flags
);
511 void flush_tlb_mm(struct mm_struct
*mm
)
515 #if defined(CONFIG_SH64_PROC_TLB)
516 ++calls_to_flush_tlb_mm
;
519 if (mm
->context
== NO_CONTEXT
)
522 local_irq_save(flags
);
524 mm
->context
=NO_CONTEXT
;
526 activate_context(mm
);
528 local_irq_restore(flags
);
532 void flush_tlb_all(void)
534 /* Invalidate all, including shared pages, excluding fixed TLBs */
536 unsigned long flags
, tlb
;
538 #if defined(CONFIG_SH64_PROC_TLB)
539 ++calls_to_flush_tlb_all
;
542 local_irq_save(flags
);
544 /* Flush each ITLB entry */
545 for_each_itlb_entry(tlb
) {
546 __flush_tlb_slot(tlb
);
549 /* Flush each DTLB entry */
550 for_each_dtlb_entry(tlb
) {
551 __flush_tlb_slot(tlb
);
554 local_irq_restore(flags
);
557 void flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
559 /* FIXME: Optimize this later.. */
563 #if defined(CONFIG_SH64_PROC_TLB)
564 /* Procfs interface to read the performance information */
567 tlb_proc_info(char *buf
, char **start
, off_t fpos
, int length
, int *eof
, void *data
)
570 len
+= sprintf(buf
+len
, "do_fast_page_fault called %12lld times\n", calls_to_do_fast_page_fault
);
571 len
+= sprintf(buf
+len
, "do_slow_page_fault called %12lld times\n", calls_to_do_slow_page_fault
);
572 len
+= sprintf(buf
+len
, "update_mmu_cache called %12lld times\n", calls_to_update_mmu_cache
);
573 len
+= sprintf(buf
+len
, "flush_tlb_page called %12lld times\n", calls_to_flush_tlb_page
);
574 len
+= sprintf(buf
+len
, "flush_tlb_range called %12lld times\n", calls_to_flush_tlb_range
);
575 len
+= sprintf(buf
+len
, "flush_tlb_mm called %12lld times\n", calls_to_flush_tlb_mm
);
576 len
+= sprintf(buf
+len
, "flush_tlb_all called %12lld times\n", calls_to_flush_tlb_all
);
577 len
+= sprintf(buf
+len
, "flush_tlb_range_sizes\n"
585 flush_tlb_range_1
, flush_tlb_range_2
, flush_tlb_range_3_4
,
586 flush_tlb_range_5_7
, flush_tlb_range_8_11
, flush_tlb_range_12_15
,
587 flush_tlb_range_16_up
);
588 len
+= sprintf(buf
+len
, "page not present %12lld times\n", page_not_present
);
593 static int __init
register_proc_tlb(void)
595 create_proc_read_entry("tlb", 0, NULL
, tlb_proc_info
, NULL
);
599 __initcall(register_proc_tlb
);