2 * arch/sh/mm/tlb-flush_64.c
4 * Copyright (C) 2000, 2001 Paolo Alberelli
5 * Copyright (C) 2003 Richard Curnow (/proc/tlb, bug fixes)
6 * Copyright (C) 2003 - 2009 Paul Mundt
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
12 #include <linux/signal.h>
13 #include <linux/rwsem.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/ptrace.h>
20 #include <linux/mman.h>
22 #include <linux/smp.h>
23 #include <linux/perf_event.h>
24 #include <linux/interrupt.h>
25 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgalloc.h>
30 #include <asm/mmu_context.h>
32 extern void die(const char *,struct pt_regs
*,long);
34 #define PFLAG(val,flag) (( (val) & (flag) ) ? #flag : "" )
35 #define PPROT(flag) PFLAG(pgprot_val(prot),flag)
37 static inline void print_prots(pgprot_t prot
)
39 printk("prot is 0x%016llx\n",pgprot_val(prot
));
41 printk("%s %s %s %s %s\n",PPROT(_PAGE_SHARED
),PPROT(_PAGE_READ
),
42 PPROT(_PAGE_EXECUTE
),PPROT(_PAGE_WRITE
),PPROT(_PAGE_USER
));
45 static inline void print_vma(struct vm_area_struct
*vma
)
47 printk("vma start 0x%08lx\n", vma
->vm_start
);
48 printk("vma end 0x%08lx\n", vma
->vm_end
);
50 print_prots(vma
->vm_page_prot
);
51 printk("vm_flags 0x%08lx\n", vma
->vm_flags
);
54 static inline void print_task(struct task_struct
*tsk
)
56 printk("Task pid %d\n", task_pid_nr(tsk
));
59 static pte_t
*lookup_pte(struct mm_struct
*mm
, unsigned long address
)
67 dir
= pgd_offset(mm
, address
);
71 pud
= pud_offset(dir
, address
);
75 pmd
= pmd_offset(pud
, address
);
79 pte
= pte_offset_kernel(pmd
, address
);
81 if (pte_none(entry
) || !pte_present(entry
))
88 * This routine handles page faults. It determines the address,
89 * and the problem, and then passes it off to one of the appropriate
92 asmlinkage
void do_page_fault(struct pt_regs
*regs
, unsigned long writeaccess
,
93 unsigned long textaccess
, unsigned long address
)
95 struct task_struct
*tsk
;
97 struct vm_area_struct
* vma
;
98 const struct exception_table_entry
*fixup
;
103 * Note this is now called with interrupts still disabled
104 * This is to cope with being called for a missing IO port
105 * address with interrupts disabled. This should be fixed as
106 * soon as we have a better 'fast path' miss handler.
108 * Plus take care how you try and debug this stuff.
109 * For example, writing debug data to a port which you
110 * have just faulted on is not going to work.
116 /* Not an IO address, so reenable interrupts */
119 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS
, 1, 0, regs
, address
);
122 * If we're in an interrupt or have no user
123 * context, we must not take the fault..
125 if (in_atomic() || !mm
)
128 /* TLB misses upon some cache flushes get done under cli() */
129 down_read(&mm
->mmap_sem
);
131 vma
= find_vma(mm
, address
);
136 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
138 address
,regs
->pc
,textaccess
,writeaccess
);
143 if (vma
->vm_start
<= address
) {
147 if (!(vma
->vm_flags
& VM_GROWSDOWN
)) {
150 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
152 address
,regs
->pc
,textaccess
,writeaccess
);
159 if (expand_stack(vma
, address
)) {
162 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
164 address
,regs
->pc
,textaccess
,writeaccess
);
170 * Ok, we have a good vm_area for this memory access, so
175 if (!(vma
->vm_flags
& VM_EXEC
))
179 if (!(vma
->vm_flags
& VM_WRITE
))
182 if (!(vma
->vm_flags
& VM_READ
))
188 * If for any reason at all we couldn't handle the fault,
189 * make sure we exit gracefully rather than endlessly redo
192 fault
= handle_mm_fault(mm
, vma
, address
, writeaccess
? FAULT_FLAG_WRITE
: 0);
193 if (unlikely(fault
& VM_FAULT_ERROR
)) {
194 if (fault
& VM_FAULT_OOM
)
196 else if (fault
& VM_FAULT_SIGBUS
)
201 if (fault
& VM_FAULT_MAJOR
) {
203 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ
, 1, 0,
207 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN
, 1, 0,
211 /* If we get here, the page fault has been handled. Do the TLB refill
212 now from the newly-setup PTE, to avoid having to fault again right
213 away on the same instruction. */
214 pte
= lookup_pte (mm
, address
);
216 /* From empirical evidence, we can get here, due to
217 !pte_present(pte). (e.g. if a swap-in occurs, and the page
218 is swapped back out again before the process that wanted it
219 gets rescheduled?) */
223 __do_tlb_refill(address
, textaccess
, pte
);
227 up_read(&mm
->mmap_sem
);
231 * Something tried to access memory that isn't in our memory map..
232 * Fix it, but check if it's kernel or user first..
236 printk("fault:bad area\n");
238 up_read(&mm
->mmap_sem
);
240 if (user_mode(regs
)) {
244 /* This is really to help debug faults when starting
245 * usermode, so only need a few */
247 printk("user mode bad_area address=%08lx pid=%d (%s) pc=%08lx\n",
248 address
, task_pid_nr(current
), current
->comm
,
249 (unsigned long) regs
->pc
);
254 if (is_global_init(tsk
)) {
255 panic("INIT had user mode bad_area\n");
257 tsk
->thread
.address
= address
;
258 tsk
->thread
.error_code
= writeaccess
;
259 info
.si_signo
= SIGSEGV
;
261 info
.si_addr
= (void *) address
;
262 force_sig_info(SIGSEGV
, &info
, tsk
);
268 printk("fault:No context\n");
270 /* Are we prepared to handle this kernel fault? */
271 fixup
= search_exception_tables(regs
->pc
);
273 regs
->pc
= fixup
->fixup
;
278 * Oops. The kernel tried to access some bad page. We'll have to
279 * terminate things with extreme prejudice.
282 if (address
< PAGE_SIZE
)
283 printk(KERN_ALERT
"Unable to handle kernel NULL pointer dereference");
285 printk(KERN_ALERT
"Unable to handle kernel paging request");
286 printk(" at virtual address %08lx\n", address
);
287 printk(KERN_ALERT
"pc = %08Lx%08Lx\n", regs
->pc
>> 32, regs
->pc
& 0xffffffff);
288 die("Oops", regs
, writeaccess
);
292 * We ran out of memory, or some other thing happened to us that made
293 * us unable to handle the page fault gracefully.
296 up_read(&mm
->mmap_sem
);
297 if (!user_mode(regs
))
299 pagefault_out_of_memory();
303 printk("fault:Do sigbus\n");
304 up_read(&mm
->mmap_sem
);
307 * Send a sigbus, regardless of whether we were in kernel
310 tsk
->thread
.address
= address
;
311 tsk
->thread
.error_code
= writeaccess
;
312 tsk
->thread
.trap_no
= 14;
313 force_sig(SIGBUS
, tsk
);
315 /* Kernel mode? Handle exceptions or die */
316 if (!user_mode(regs
))
320 void local_flush_tlb_one(unsigned long asid
, unsigned long page
)
322 unsigned long long match
, pteh
=0, lpage
;
326 * Sign-extend based on neff.
328 lpage
= neff_sign_extend(page
);
329 match
= (asid
<< PTEH_ASID_SHIFT
) | PTEH_VALID
;
332 for_each_itlb_entry(tlb
) {
333 asm volatile ("getcfg %1, 0, %0"
338 __flush_tlb_slot(tlb
);
343 for_each_dtlb_entry(tlb
) {
344 asm volatile ("getcfg %1, 0, %0"
349 __flush_tlb_slot(tlb
);
356 void local_flush_tlb_page(struct vm_area_struct
*vma
, unsigned long page
)
362 local_irq_save(flags
);
363 local_flush_tlb_one(get_asid(), page
);
364 local_irq_restore(flags
);
368 void local_flush_tlb_range(struct vm_area_struct
*vma
, unsigned long start
,
372 unsigned long long match
, pteh
=0, pteh_epn
, pteh_low
;
374 unsigned int cpu
= smp_processor_id();
375 struct mm_struct
*mm
;
378 if (cpu_context(cpu
, mm
) == NO_CONTEXT
)
381 local_irq_save(flags
);
386 match
= (cpu_asid(cpu
, mm
) << PTEH_ASID_SHIFT
) | PTEH_VALID
;
389 for_each_itlb_entry(tlb
) {
390 asm volatile ("getcfg %1, 0, %0"
394 pteh_epn
= pteh
& PAGE_MASK
;
395 pteh_low
= pteh
& ~PAGE_MASK
;
397 if (pteh_low
== match
&& pteh_epn
>= start
&& pteh_epn
<= end
)
398 __flush_tlb_slot(tlb
);
402 for_each_dtlb_entry(tlb
) {
403 asm volatile ("getcfg %1, 0, %0"
407 pteh_epn
= pteh
& PAGE_MASK
;
408 pteh_low
= pteh
& ~PAGE_MASK
;
410 if (pteh_low
== match
&& pteh_epn
>= start
&& pteh_epn
<= end
)
411 __flush_tlb_slot(tlb
);
414 local_irq_restore(flags
);
417 void local_flush_tlb_mm(struct mm_struct
*mm
)
420 unsigned int cpu
= smp_processor_id();
422 if (cpu_context(cpu
, mm
) == NO_CONTEXT
)
425 local_irq_save(flags
);
427 cpu_context(cpu
, mm
) = NO_CONTEXT
;
428 if (mm
== current
->mm
)
429 activate_context(mm
, cpu
);
431 local_irq_restore(flags
);
434 void local_flush_tlb_all(void)
436 /* Invalidate all, including shared pages, excluding fixed TLBs */
437 unsigned long flags
, tlb
;
439 local_irq_save(flags
);
441 /* Flush each ITLB entry */
442 for_each_itlb_entry(tlb
)
443 __flush_tlb_slot(tlb
);
445 /* Flush each DTLB entry */
446 for_each_dtlb_entry(tlb
)
447 __flush_tlb_slot(tlb
);
449 local_irq_restore(flags
);
452 void local_flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
454 /* FIXME: Optimize this later.. */
458 void __flush_tlb_global(void)
463 void __update_tlb(struct vm_area_struct
*vma
, unsigned long address
, pte_t pte
)