blob: 70b3c271aa9fc42c2ab24aa6adb91b872bc9847a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mundt3eeffb32007-11-19 18:57:03 +09002 * arch/sh/mm/tlb-flush_64.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2000, 2001 Paolo Alberelli
5 * Copyright (C) 2003 Richard Curnow (/proc/tlb, bug fixes)
Paul Mundta1e20302012-04-11 12:44:50 +09006 * Copyright (C) 2003 - 2012 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Paul Mundt3eeffb32007-11-19 18:57:03 +09008 * 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
10 * for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
21#include <linux/mm.h>
22#include <linux/smp.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020023#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
26#include <asm/tlb.h>
27#include <asm/uaccess.h>
28#include <asm/pgalloc.h>
29#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31extern void die(const char *,struct pt_regs *,long);
32
33#define PFLAG(val,flag) (( (val) & (flag) ) ? #flag : "" )
34#define PPROT(flag) PFLAG(pgprot_val(prot),flag)
35
36static inline void print_prots(pgprot_t prot)
37{
Matt Fleming24ef7fc2009-11-19 21:11:05 +000038 printk("prot is 0x%016llx\n",pgprot_val(prot));
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 printk("%s %s %s %s %s\n",PPROT(_PAGE_SHARED),PPROT(_PAGE_READ),
41 PPROT(_PAGE_EXECUTE),PPROT(_PAGE_WRITE),PPROT(_PAGE_USER));
42}
43
44static inline void print_vma(struct vm_area_struct *vma)
45{
46 printk("vma start 0x%08lx\n", vma->vm_start);
47 printk("vma end 0x%08lx\n", vma->vm_end);
48
49 print_prots(vma->vm_page_prot);
50 printk("vm_flags 0x%08lx\n", vma->vm_flags);
51}
52
53static inline void print_task(struct task_struct *tsk)
54{
Alexey Dobriyan19c58702007-10-18 23:40:41 -070055 printk("Task pid %d\n", task_pid_nr(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58static pte_t *lookup_pte(struct mm_struct *mm, unsigned long address)
59{
60 pgd_t *dir;
Paul Mundt3eeffb32007-11-19 18:57:03 +090061 pud_t *pud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 pmd_t *pmd;
63 pte_t *pte;
64 pte_t entry;
65
66 dir = pgd_offset(mm, address);
Paul Mundt3eeffb32007-11-19 18:57:03 +090067 if (pgd_none(*dir))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Paul Mundt3eeffb32007-11-19 18:57:03 +090070 pud = pud_offset(dir, address);
71 if (pud_none(*pud))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return NULL;
Paul Mundt3eeffb32007-11-19 18:57:03 +090073
74 pmd = pmd_offset(pud, address);
75 if (pmd_none(*pmd))
76 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 pte = pte_offset_kernel(pmd, address);
79 entry = *pte;
Paul Mundt3eeffb32007-11-19 18:57:03 +090080 if (pte_none(entry) || !pte_present(entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 return pte;
84}
85
86/*
87 * This routine handles page faults. It determines the address,
88 * and the problem, and then passes it off to one of the appropriate
89 * routines.
90 */
91asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
92 unsigned long textaccess, unsigned long address)
93{
94 struct task_struct *tsk;
95 struct mm_struct *mm;
96 struct vm_area_struct * vma;
97 const struct exception_table_entry *fixup;
Paul Mundta1e20302012-04-11 12:44:50 +090098 unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
99 (writeaccess ? FAULT_FLAG_WRITE : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 pte_t *pte;
Nick Piggin83c54072007-07-19 01:47:05 -0700101 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* SIM
104 * Note this is now called with interrupts still disabled
105 * This is to cope with being called for a missing IO port
Simon Arlott0a354772007-05-14 08:25:48 +0900106 * address with interrupts disabled. This should be fixed as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * soon as we have a better 'fast path' miss handler.
108 *
109 * Plus take care how you try and debug this stuff.
110 * For example, writing debug data to a port which you
111 * have just faulted on is not going to work.
112 */
113
114 tsk = current;
115 mm = tsk->mm;
116
117 /* Not an IO address, so reenable interrupts */
118 local_irq_enable();
119
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200120 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
Paul Mundt163b2f02009-06-25 02:49:03 +0900121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /*
123 * If we're in an interrupt or have no user
124 * context, we must not take the fault..
125 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800126 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 goto no_context;
128
Paul Mundta1e20302012-04-11 12:44:50 +0900129retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* TLB misses upon some cache flushes get done under cli() */
131 down_read(&mm->mmap_sem);
132
133 vma = find_vma(mm, address);
134
135 if (!vma) {
136#ifdef DEBUG_FAULT
137 print_task(tsk);
138 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
Harvey Harrison866e6b92008-03-04 15:23:47 -0800139 __func__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 address,regs->pc,textaccess,writeaccess);
141 show_regs(regs);
142#endif
143 goto bad_area;
144 }
145 if (vma->vm_start <= address) {
146 goto good_area;
147 }
148
149 if (!(vma->vm_flags & VM_GROWSDOWN)) {
150#ifdef DEBUG_FAULT
151 print_task(tsk);
152 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
Harvey Harrison866e6b92008-03-04 15:23:47 -0800153 __func__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 address,regs->pc,textaccess,writeaccess);
155 show_regs(regs);
156
157 print_vma(vma);
158#endif
159 goto bad_area;
160 }
161 if (expand_stack(vma, address)) {
162#ifdef DEBUG_FAULT
163 print_task(tsk);
164 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
Harvey Harrison866e6b92008-03-04 15:23:47 -0800165 __func__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 address,regs->pc,textaccess,writeaccess);
167 show_regs(regs);
168#endif
169 goto bad_area;
170 }
171/*
172 * Ok, we have a good vm_area for this memory access, so
173 * we can handle it..
174 */
175good_area:
176 if (textaccess) {
177 if (!(vma->vm_flags & VM_EXEC))
178 goto bad_area;
179 } else {
180 if (writeaccess) {
181 if (!(vma->vm_flags & VM_WRITE))
182 goto bad_area;
183 } else {
184 if (!(vma->vm_flags & VM_READ))
185 goto bad_area;
186 }
187 }
188
189 /*
190 * If for any reason at all we couldn't handle the fault,
191 * make sure we exit gracefully rather than endlessly redo
192 * the fault.
193 */
Paul Mundta1e20302012-04-11 12:44:50 +0900194 fault = handle_mm_fault(mm, vma, address, flags);
195
196 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
197 return;
198
Nick Piggin83c54072007-07-19 01:47:05 -0700199 if (unlikely(fault & VM_FAULT_ERROR)) {
200 if (fault & VM_FAULT_OOM)
201 goto out_of_memory;
202 else if (fault & VM_FAULT_SIGBUS)
203 goto do_sigbus;
204 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Paul Mundt163b2f02009-06-25 02:49:03 +0900206
Paul Mundta1e20302012-04-11 12:44:50 +0900207 if (flags & FAULT_FLAG_ALLOW_RETRY) {
208 if (fault & VM_FAULT_MAJOR) {
209 tsk->maj_flt++;
210 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
211 regs, address);
212 } else {
213 tsk->min_flt++;
214 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
215 regs, address);
216 }
217
218 if (fault & VM_FAULT_RETRY) {
219 flags &= ~FAULT_FLAG_ALLOW_RETRY;
220
221 /*
222 * No need to up_read(&mm->mmap_sem) as we would
223 * have already released it in __lock_page_or_retry
224 * in mm/filemap.c.
225 */
226 goto retry;
227 }
Paul Mundt163b2f02009-06-25 02:49:03 +0900228 }
Nick Piggin83c54072007-07-19 01:47:05 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* If we get here, the page fault has been handled. Do the TLB refill
231 now from the newly-setup PTE, to avoid having to fault again right
232 away on the same instruction. */
233 pte = lookup_pte (mm, address);
234 if (!pte) {
235 /* From empirical evidence, we can get here, due to
236 !pte_present(pte). (e.g. if a swap-in occurs, and the page
237 is swapped back out again before the process that wanted it
238 gets rescheduled?) */
239 goto no_pte;
240 }
241
242 __do_tlb_refill(address, textaccess, pte);
243
244no_pte:
245
246 up_read(&mm->mmap_sem);
247 return;
248
249/*
250 * Something tried to access memory that isn't in our memory map..
251 * Fix it, but check if it's kernel or user first..
252 */
253bad_area:
254#ifdef DEBUG_FAULT
255 printk("fault:bad area\n");
256#endif
257 up_read(&mm->mmap_sem);
258
259 if (user_mode(regs)) {
260 static int count=0;
261 siginfo_t info;
262 if (count < 4) {
263 /* This is really to help debug faults when starting
264 * usermode, so only need a few */
265 count++;
266 printk("user mode bad_area address=%08lx pid=%d (%s) pc=%08lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700267 address, task_pid_nr(current), current->comm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 (unsigned long) regs->pc);
269#if 0
270 show_regs(regs);
271#endif
272 }
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700273 if (is_global_init(tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 panic("INIT had user mode bad_area\n");
275 }
276 tsk->thread.address = address;
277 tsk->thread.error_code = writeaccess;
278 info.si_signo = SIGSEGV;
279 info.si_errno = 0;
280 info.si_addr = (void *) address;
281 force_sig_info(SIGSEGV, &info, tsk);
282 return;
283 }
284
285no_context:
286#ifdef DEBUG_FAULT
287 printk("fault:No context\n");
288#endif
289 /* Are we prepared to handle this kernel fault? */
290 fixup = search_exception_tables(regs->pc);
291 if (fixup) {
292 regs->pc = fixup->fixup;
293 return;
294 }
295
296/*
297 * Oops. The kernel tried to access some bad page. We'll have to
298 * terminate things with extreme prejudice.
299 *
300 */
301 if (address < PAGE_SIZE)
302 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
303 else
304 printk(KERN_ALERT "Unable to handle kernel paging request");
305 printk(" at virtual address %08lx\n", address);
306 printk(KERN_ALERT "pc = %08Lx%08Lx\n", regs->pc >> 32, regs->pc & 0xffffffff);
307 die("Oops", regs, writeaccess);
308 do_exit(SIGKILL);
309
310/*
311 * We ran out of memory, or some other thing happened to us that made
312 * us unable to handle the page fault gracefully.
313 */
314out_of_memory:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 up_read(&mm->mmap_sem);
Nick Piggin6b6b18e2010-04-22 16:06:26 +0000316 if (!user_mode(regs))
317 goto no_context;
318 pagefault_out_of_memory();
319 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321do_sigbus:
322 printk("fault:Do sigbus\n");
323 up_read(&mm->mmap_sem);
324
325 /*
326 * Send a sigbus, regardless of whether we were in kernel
327 * or user mode.
328 */
329 tsk->thread.address = address;
330 tsk->thread.error_code = writeaccess;
331 tsk->thread.trap_no = 14;
332 force_sig(SIGBUS, tsk);
333
334 /* Kernel mode? Handle exceptions or die */
335 if (!user_mode(regs))
336 goto no_context;
337}
338
Paul Mundt3eeffb32007-11-19 18:57:03 +0900339void local_flush_tlb_one(unsigned long asid, unsigned long page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 unsigned long long match, pteh=0, lpage;
342 unsigned long tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
345 * Sign-extend based on neff.
346 */
Paul Mundtc7914832009-08-04 17:14:39 +0900347 lpage = neff_sign_extend(page);
Paul Mundt3eeffb32007-11-19 18:57:03 +0900348 match = (asid << PTEH_ASID_SHIFT) | PTEH_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 match |= lpage;
350
Paul Mundt3eeffb32007-11-19 18:57:03 +0900351 for_each_itlb_entry(tlb) {
352 asm volatile ("getcfg %1, 0, %0"
353 : "=r" (pteh)
354 : "r" (tlb) );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Paul Mundt3eeffb32007-11-19 18:57:03 +0900356 if (pteh == match) {
357 __flush_tlb_slot(tlb);
358 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 }
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 for_each_dtlb_entry(tlb) {
363 asm volatile ("getcfg %1, 0, %0"
364 : "=r" (pteh)
365 : "r" (tlb) );
366
367 if (pteh == match) {
368 __flush_tlb_slot(tlb);
369 break;
370 }
371
372 }
373}
374
Paul Mundt3eeffb32007-11-19 18:57:03 +0900375void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 unsigned long flags;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (vma->vm_mm) {
380 page &= PAGE_MASK;
381 local_irq_save(flags);
Paul Mundt3eeffb32007-11-19 18:57:03 +0900382 local_flush_tlb_one(get_asid(), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 local_irq_restore(flags);
384 }
385}
386
Paul Mundt3eeffb32007-11-19 18:57:03 +0900387void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
388 unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 unsigned long flags;
391 unsigned long long match, pteh=0, pteh_epn, pteh_low;
392 unsigned long tlb;
Paul Mundt3eeffb32007-11-19 18:57:03 +0900393 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct mm_struct *mm;
395
396 mm = vma->vm_mm;
Paul Mundt3eeffb32007-11-19 18:57:03 +0900397 if (cpu_context(cpu, mm) == NO_CONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return;
399
400 local_irq_save(flags);
401
402 start &= PAGE_MASK;
403 end &= PAGE_MASK;
404
Paul Mundt3eeffb32007-11-19 18:57:03 +0900405 match = (cpu_asid(cpu, mm) << PTEH_ASID_SHIFT) | PTEH_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* Flush ITLB */
408 for_each_itlb_entry(tlb) {
409 asm volatile ("getcfg %1, 0, %0"
410 : "=r" (pteh)
411 : "r" (tlb) );
412
413 pteh_epn = pteh & PAGE_MASK;
414 pteh_low = pteh & ~PAGE_MASK;
415
416 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
417 __flush_tlb_slot(tlb);
418 }
419
420 /* Flush DTLB */
421 for_each_dtlb_entry(tlb) {
422 asm volatile ("getcfg %1, 0, %0"
423 : "=r" (pteh)
424 : "r" (tlb) );
425
426 pteh_epn = pteh & PAGE_MASK;
427 pteh_low = pteh & ~PAGE_MASK;
428
429 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
430 __flush_tlb_slot(tlb);
431 }
432
433 local_irq_restore(flags);
434}
435
Paul Mundt3eeffb32007-11-19 18:57:03 +0900436void local_flush_tlb_mm(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 unsigned long flags;
Paul Mundt3eeffb32007-11-19 18:57:03 +0900439 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Paul Mundt3eeffb32007-11-19 18:57:03 +0900441 if (cpu_context(cpu, mm) == NO_CONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return;
443
444 local_irq_save(flags);
445
Paul Mundt3eeffb32007-11-19 18:57:03 +0900446 cpu_context(cpu, mm) = NO_CONTEXT;
447 if (mm == current->mm)
448 activate_context(mm, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Paul Mundt3eeffb32007-11-19 18:57:03 +0900453void local_flush_tlb_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 /* Invalidate all, including shared pages, excluding fixed TLBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 unsigned long flags, tlb;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 local_irq_save(flags);
459
460 /* Flush each ITLB entry */
Paul Mundt3eeffb32007-11-19 18:57:03 +0900461 for_each_itlb_entry(tlb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 __flush_tlb_slot(tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /* Flush each DTLB entry */
Paul Mundt3eeffb32007-11-19 18:57:03 +0900465 for_each_dtlb_entry(tlb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 __flush_tlb_slot(tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 local_irq_restore(flags);
469}
470
Paul Mundt3eeffb32007-11-19 18:57:03 +0900471void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 /* FIXME: Optimize this later.. */
474 flush_tlb_all();
475}
Paul Mundt9cef7492009-07-29 00:12:17 +0900476
Paul Mundt59615ec2010-07-02 15:44:09 +0900477void __flush_tlb_global(void)
478{
479 flush_tlb_all();
480}
481
Paul Mundt9cef7492009-07-29 00:12:17 +0900482void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
483{
484}