blob: 11c5a18f10ed9655c5fbc7acc6e5202b28b05e93 [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 Mundt163b2f02009-06-25 02:49:03 +09006 * Copyright (C) 2003 - 2009 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;
98 pte_t *pte;
Nick Piggin83c54072007-07-19 01:47:05 -070099 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* SIM
102 * Note this is now called with interrupts still disabled
103 * This is to cope with being called for a missing IO port
Simon Arlott0a354772007-05-14 08:25:48 +0900104 * address with interrupts disabled. This should be fixed as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * soon as we have a better 'fast path' miss handler.
106 *
107 * Plus take care how you try and debug this stuff.
108 * For example, writing debug data to a port which you
109 * have just faulted on is not going to work.
110 */
111
112 tsk = current;
113 mm = tsk->mm;
114
115 /* Not an IO address, so reenable interrupts */
116 local_irq_enable();
117
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200118 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
Paul Mundt163b2f02009-06-25 02:49:03 +0900119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /*
121 * If we're in an interrupt or have no user
122 * context, we must not take the fault..
123 */
Peter Zijlstra6edaf682006-12-06 20:32:18 -0800124 if (in_atomic() || !mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 goto no_context;
126
127 /* TLB misses upon some cache flushes get done under cli() */
128 down_read(&mm->mmap_sem);
129
130 vma = find_vma(mm, address);
131
132 if (!vma) {
133#ifdef DEBUG_FAULT
134 print_task(tsk);
135 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
Harvey Harrison866e6b92008-03-04 15:23:47 -0800136 __func__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 address,regs->pc,textaccess,writeaccess);
138 show_regs(regs);
139#endif
140 goto bad_area;
141 }
142 if (vma->vm_start <= address) {
143 goto good_area;
144 }
145
146 if (!(vma->vm_flags & VM_GROWSDOWN)) {
147#ifdef DEBUG_FAULT
148 print_task(tsk);
149 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
Harvey Harrison866e6b92008-03-04 15:23:47 -0800150 __func__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 address,regs->pc,textaccess,writeaccess);
152 show_regs(regs);
153
154 print_vma(vma);
155#endif
156 goto bad_area;
157 }
158 if (expand_stack(vma, address)) {
159#ifdef DEBUG_FAULT
160 print_task(tsk);
161 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
Harvey Harrison866e6b92008-03-04 15:23:47 -0800162 __func__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 address,regs->pc,textaccess,writeaccess);
164 show_regs(regs);
165#endif
166 goto bad_area;
167 }
168/*
169 * Ok, we have a good vm_area for this memory access, so
170 * we can handle it..
171 */
172good_area:
173 if (textaccess) {
174 if (!(vma->vm_flags & VM_EXEC))
175 goto bad_area;
176 } else {
177 if (writeaccess) {
178 if (!(vma->vm_flags & VM_WRITE))
179 goto bad_area;
180 } else {
181 if (!(vma->vm_flags & VM_READ))
182 goto bad_area;
183 }
184 }
185
186 /*
187 * If for any reason at all we couldn't handle the fault,
188 * make sure we exit gracefully rather than endlessly redo
189 * the fault.
190 */
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700191 fault = handle_mm_fault(mm, vma, address, writeaccess ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -0700192 if (unlikely(fault & VM_FAULT_ERROR)) {
193 if (fault & VM_FAULT_OOM)
194 goto out_of_memory;
195 else if (fault & VM_FAULT_SIGBUS)
196 goto do_sigbus;
197 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Paul Mundt163b2f02009-06-25 02:49:03 +0900199
200 if (fault & VM_FAULT_MAJOR) {
Nick Piggin83c54072007-07-19 01:47:05 -0700201 tsk->maj_flt++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200202 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
Paul Mundt163b2f02009-06-25 02:49:03 +0900203 regs, address);
204 } else {
Nick Piggin83c54072007-07-19 01:47:05 -0700205 tsk->min_flt++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200206 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
Paul Mundt163b2f02009-06-25 02:49:03 +0900207 regs, address);
208 }
Nick Piggin83c54072007-07-19 01:47:05 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* If we get here, the page fault has been handled. Do the TLB refill
211 now from the newly-setup PTE, to avoid having to fault again right
212 away on the same instruction. */
213 pte = lookup_pte (mm, address);
214 if (!pte) {
215 /* From empirical evidence, we can get here, due to
216 !pte_present(pte). (e.g. if a swap-in occurs, and the page
217 is swapped back out again before the process that wanted it
218 gets rescheduled?) */
219 goto no_pte;
220 }
221
222 __do_tlb_refill(address, textaccess, pte);
223
224no_pte:
225
226 up_read(&mm->mmap_sem);
227 return;
228
229/*
230 * Something tried to access memory that isn't in our memory map..
231 * Fix it, but check if it's kernel or user first..
232 */
233bad_area:
234#ifdef DEBUG_FAULT
235 printk("fault:bad area\n");
236#endif
237 up_read(&mm->mmap_sem);
238
239 if (user_mode(regs)) {
240 static int count=0;
241 siginfo_t info;
242 if (count < 4) {
243 /* This is really to help debug faults when starting
244 * usermode, so only need a few */
245 count++;
246 printk("user mode bad_area address=%08lx pid=%d (%s) pc=%08lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700247 address, task_pid_nr(current), current->comm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 (unsigned long) regs->pc);
249#if 0
250 show_regs(regs);
251#endif
252 }
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700253 if (is_global_init(tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 panic("INIT had user mode bad_area\n");
255 }
256 tsk->thread.address = address;
257 tsk->thread.error_code = writeaccess;
258 info.si_signo = SIGSEGV;
259 info.si_errno = 0;
260 info.si_addr = (void *) address;
261 force_sig_info(SIGSEGV, &info, tsk);
262 return;
263 }
264
265no_context:
266#ifdef DEBUG_FAULT
267 printk("fault:No context\n");
268#endif
269 /* Are we prepared to handle this kernel fault? */
270 fixup = search_exception_tables(regs->pc);
271 if (fixup) {
272 regs->pc = fixup->fixup;
273 return;
274 }
275
276/*
277 * Oops. The kernel tried to access some bad page. We'll have to
278 * terminate things with extreme prejudice.
279 *
280 */
281 if (address < PAGE_SIZE)
282 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
283 else
284 printk(KERN_ALERT "Unable to handle kernel paging request");
285 printk(" at virtual address %08lx\n", address);
286 printk(KERN_ALERT "pc = %08Lx%08Lx\n", regs->pc >> 32, regs->pc & 0xffffffff);
287 die("Oops", regs, writeaccess);
288 do_exit(SIGKILL);
289
290/*
291 * We ran out of memory, or some other thing happened to us that made
292 * us unable to handle the page fault gracefully.
293 */
294out_of_memory:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 up_read(&mm->mmap_sem);
Nick Piggin6b6b18e2010-04-22 16:06:26 +0000296 if (!user_mode(regs))
297 goto no_context;
298 pagefault_out_of_memory();
299 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301do_sigbus:
302 printk("fault:Do sigbus\n");
303 up_read(&mm->mmap_sem);
304
305 /*
306 * Send a sigbus, regardless of whether we were in kernel
307 * or user mode.
308 */
309 tsk->thread.address = address;
310 tsk->thread.error_code = writeaccess;
311 tsk->thread.trap_no = 14;
312 force_sig(SIGBUS, tsk);
313
314 /* Kernel mode? Handle exceptions or die */
315 if (!user_mode(regs))
316 goto no_context;
317}
318
Paul Mundt3eeffb32007-11-19 18:57:03 +0900319void local_flush_tlb_one(unsigned long asid, unsigned long page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 unsigned long long match, pteh=0, lpage;
322 unsigned long tlb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /*
325 * Sign-extend based on neff.
326 */
Paul Mundtc7914832009-08-04 17:14:39 +0900327 lpage = neff_sign_extend(page);
Paul Mundt3eeffb32007-11-19 18:57:03 +0900328 match = (asid << PTEH_ASID_SHIFT) | PTEH_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 match |= lpage;
330
Paul Mundt3eeffb32007-11-19 18:57:03 +0900331 for_each_itlb_entry(tlb) {
332 asm volatile ("getcfg %1, 0, %0"
333 : "=r" (pteh)
334 : "r" (tlb) );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Paul Mundt3eeffb32007-11-19 18:57:03 +0900336 if (pteh == match) {
337 __flush_tlb_slot(tlb);
338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 for_each_dtlb_entry(tlb) {
343 asm volatile ("getcfg %1, 0, %0"
344 : "=r" (pteh)
345 : "r" (tlb) );
346
347 if (pteh == match) {
348 __flush_tlb_slot(tlb);
349 break;
350 }
351
352 }
353}
354
Paul Mundt3eeffb32007-11-19 18:57:03 +0900355void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 unsigned long flags;
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (vma->vm_mm) {
360 page &= PAGE_MASK;
361 local_irq_save(flags);
Paul Mundt3eeffb32007-11-19 18:57:03 +0900362 local_flush_tlb_one(get_asid(), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 local_irq_restore(flags);
364 }
365}
366
Paul Mundt3eeffb32007-11-19 18:57:03 +0900367void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
368 unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 unsigned long flags;
371 unsigned long long match, pteh=0, pteh_epn, pteh_low;
372 unsigned long tlb;
Paul Mundt3eeffb32007-11-19 18:57:03 +0900373 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct mm_struct *mm;
375
376 mm = vma->vm_mm;
Paul Mundt3eeffb32007-11-19 18:57:03 +0900377 if (cpu_context(cpu, mm) == NO_CONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return;
379
380 local_irq_save(flags);
381
382 start &= PAGE_MASK;
383 end &= PAGE_MASK;
384
Paul Mundt3eeffb32007-11-19 18:57:03 +0900385 match = (cpu_asid(cpu, mm) << PTEH_ASID_SHIFT) | PTEH_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* Flush ITLB */
388 for_each_itlb_entry(tlb) {
389 asm volatile ("getcfg %1, 0, %0"
390 : "=r" (pteh)
391 : "r" (tlb) );
392
393 pteh_epn = pteh & PAGE_MASK;
394 pteh_low = pteh & ~PAGE_MASK;
395
396 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
397 __flush_tlb_slot(tlb);
398 }
399
400 /* Flush DTLB */
401 for_each_dtlb_entry(tlb) {
402 asm volatile ("getcfg %1, 0, %0"
403 : "=r" (pteh)
404 : "r" (tlb) );
405
406 pteh_epn = pteh & PAGE_MASK;
407 pteh_low = pteh & ~PAGE_MASK;
408
409 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
410 __flush_tlb_slot(tlb);
411 }
412
413 local_irq_restore(flags);
414}
415
Paul Mundt3eeffb32007-11-19 18:57:03 +0900416void local_flush_tlb_mm(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 unsigned long flags;
Paul Mundt3eeffb32007-11-19 18:57:03 +0900419 unsigned int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Paul Mundt3eeffb32007-11-19 18:57:03 +0900421 if (cpu_context(cpu, mm) == NO_CONTEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return;
423
424 local_irq_save(flags);
425
Paul Mundt3eeffb32007-11-19 18:57:03 +0900426 cpu_context(cpu, mm) = NO_CONTEXT;
427 if (mm == current->mm)
428 activate_context(mm, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Paul Mundt3eeffb32007-11-19 18:57:03 +0900433void local_flush_tlb_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 /* Invalidate all, including shared pages, excluding fixed TLBs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned long flags, tlb;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 local_irq_save(flags);
439
440 /* Flush each ITLB entry */
Paul Mundt3eeffb32007-11-19 18:57:03 +0900441 for_each_itlb_entry(tlb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 __flush_tlb_slot(tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* Flush each DTLB entry */
Paul Mundt3eeffb32007-11-19 18:57:03 +0900445 for_each_dtlb_entry(tlb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 __flush_tlb_slot(tlb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 local_irq_restore(flags);
449}
450
Paul Mundt3eeffb32007-11-19 18:57:03 +0900451void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 /* FIXME: Optimize this later.. */
454 flush_tlb_all();
455}
Paul Mundt9cef7492009-07-29 00:12:17 +0900456
Paul Mundt59615ec2010-07-02 15:44:09 +0900457void __flush_tlb_global(void)
458{
459 flush_tlb_all();
460}
461
Paul Mundt9cef7492009-07-29 00:12:17 +0900462void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
463{
464}