vm: add VM_FAULT_SIGSEGV handling support
[pandora-kernel.git] / arch / sh / mm / tlbflush_64.c
1 /*
2  * arch/sh/mm/tlb-flush_64.c
3  *
4  * Copyright (C) 2000, 2001  Paolo Alberelli
5  * Copyright (C) 2003  Richard Curnow (/proc/tlb, bug fixes)
6  * Copyright (C) 2003 - 2009 Paul Mundt
7  *
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
10  * for more details.
11  */
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>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/perf_event.h>
24 #include <linux/interrupt.h>
25 #include <asm/system.h>
26 #include <asm/io.h>
27 #include <asm/tlb.h>
28 #include <asm/uaccess.h>
29 #include <asm/pgalloc.h>
30 #include <asm/mmu_context.h>
31
32 extern void die(const char *,struct pt_regs *,long);
33
34 #define PFLAG(val,flag)   (( (val) & (flag) ) ? #flag : "" )
35 #define PPROT(flag) PFLAG(pgprot_val(prot),flag)
36
37 static inline void print_prots(pgprot_t prot)
38 {
39         printk("prot is 0x%016llx\n",pgprot_val(prot));
40
41         printk("%s %s %s %s %s\n",PPROT(_PAGE_SHARED),PPROT(_PAGE_READ),
42                PPROT(_PAGE_EXECUTE),PPROT(_PAGE_WRITE),PPROT(_PAGE_USER));
43 }
44
45 static inline void print_vma(struct vm_area_struct *vma)
46 {
47         printk("vma start 0x%08lx\n", vma->vm_start);
48         printk("vma end   0x%08lx\n", vma->vm_end);
49
50         print_prots(vma->vm_page_prot);
51         printk("vm_flags 0x%08lx\n", vma->vm_flags);
52 }
53
54 static inline void print_task(struct task_struct *tsk)
55 {
56         printk("Task pid %d\n", task_pid_nr(tsk));
57 }
58
59 static pte_t *lookup_pte(struct mm_struct *mm, unsigned long address)
60 {
61         pgd_t *dir;
62         pud_t *pud;
63         pmd_t *pmd;
64         pte_t *pte;
65         pte_t entry;
66
67         dir = pgd_offset(mm, address);
68         if (pgd_none(*dir))
69                 return NULL;
70
71         pud = pud_offset(dir, address);
72         if (pud_none(*pud))
73                 return NULL;
74
75         pmd = pmd_offset(pud, address);
76         if (pmd_none(*pmd))
77                 return NULL;
78
79         pte = pte_offset_kernel(pmd, address);
80         entry = *pte;
81         if (pte_none(entry) || !pte_present(entry))
82                 return NULL;
83
84         return pte;
85 }
86
87 /*
88  * This routine handles page faults.  It determines the address,
89  * and the problem, and then passes it off to one of the appropriate
90  * routines.
91  */
92 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
93                               unsigned long textaccess, unsigned long address)
94 {
95         struct task_struct *tsk;
96         struct mm_struct *mm;
97         struct vm_area_struct * vma;
98         const struct exception_table_entry *fixup;
99         pte_t *pte;
100         int fault;
101
102         /* SIM
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.
107          *
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.
111          */
112
113         tsk = current;
114         mm = tsk->mm;
115
116         /* Not an IO address, so reenable interrupts */
117         local_irq_enable();
118
119         perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
120
121         /*
122          * If we're in an interrupt or have no user
123          * context, we must not take the fault..
124          */
125         if (in_atomic() || !mm)
126                 goto no_context;
127
128         /* TLB misses upon some cache flushes get done under cli() */
129         down_read(&mm->mmap_sem);
130
131         vma = find_vma(mm, address);
132
133         if (!vma) {
134 #ifdef DEBUG_FAULT
135                 print_task(tsk);
136                 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
137                        __func__, __LINE__,
138                        address,regs->pc,textaccess,writeaccess);
139                 show_regs(regs);
140 #endif
141                 goto bad_area;
142         }
143         if (vma->vm_start <= address) {
144                 goto good_area;
145         }
146
147         if (!(vma->vm_flags & VM_GROWSDOWN)) {
148 #ifdef DEBUG_FAULT
149                 print_task(tsk);
150                 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
151                        __func__, __LINE__,
152                        address,regs->pc,textaccess,writeaccess);
153                 show_regs(regs);
154
155                 print_vma(vma);
156 #endif
157                 goto bad_area;
158         }
159         if (expand_stack(vma, address)) {
160 #ifdef DEBUG_FAULT
161                 print_task(tsk);
162                 printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
163                        __func__, __LINE__,
164                        address,regs->pc,textaccess,writeaccess);
165                 show_regs(regs);
166 #endif
167                 goto bad_area;
168         }
169 /*
170  * Ok, we have a good vm_area for this memory access, so
171  * we can handle it..
172  */
173 good_area:
174         if (textaccess) {
175                 if (!(vma->vm_flags & VM_EXEC))
176                         goto bad_area;
177         } else {
178                 if (writeaccess) {
179                         if (!(vma->vm_flags & VM_WRITE))
180                                 goto bad_area;
181                 } else {
182                         if (!(vma->vm_flags & VM_READ))
183                                 goto bad_area;
184                 }
185         }
186
187         /*
188          * If for any reason at all we couldn't handle the fault,
189          * make sure we exit gracefully rather than endlessly redo
190          * the fault.
191          */
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)
195                         goto out_of_memory;
196                 else if (fault & VM_FAULT_SIGBUS)
197                         goto do_sigbus;
198                 else if (fault & VM_FAULT_SIGSEGV)
199                         goto bad_area;
200                 BUG();
201         }
202
203         if (fault & VM_FAULT_MAJOR) {
204                 tsk->maj_flt++;
205                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
206                                      regs, address);
207         } else {
208                 tsk->min_flt++;
209                 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
210                                      regs, address);
211         }
212
213         /* If we get here, the page fault has been handled.  Do the TLB refill
214            now from the newly-setup PTE, to avoid having to fault again right
215            away on the same instruction. */
216         pte = lookup_pte (mm, address);
217         if (!pte) {
218                 /* From empirical evidence, we can get here, due to
219                    !pte_present(pte).  (e.g. if a swap-in occurs, and the page
220                    is swapped back out again before the process that wanted it
221                    gets rescheduled?) */
222                 goto no_pte;
223         }
224
225         __do_tlb_refill(address, textaccess, pte);
226
227 no_pte:
228
229         up_read(&mm->mmap_sem);
230         return;
231
232 /*
233  * Something tried to access memory that isn't in our memory map..
234  * Fix it, but check if it's kernel or user first..
235  */
236 bad_area:
237 #ifdef DEBUG_FAULT
238         printk("fault:bad area\n");
239 #endif
240         up_read(&mm->mmap_sem);
241
242         if (user_mode(regs)) {
243                 static int count=0;
244                 siginfo_t info;
245                 if (count < 4) {
246                         /* This is really to help debug faults when starting
247                          * usermode, so only need a few */
248                         count++;
249                         printk("user mode bad_area address=%08lx pid=%d (%s) pc=%08lx\n",
250                                 address, task_pid_nr(current), current->comm,
251                                 (unsigned long) regs->pc);
252 #if 0
253                         show_regs(regs);
254 #endif
255                 }
256                 if (is_global_init(tsk)) {
257                         panic("INIT had user mode bad_area\n");
258                 }
259                 tsk->thread.address = address;
260                 tsk->thread.error_code = writeaccess;
261                 info.si_signo = SIGSEGV;
262                 info.si_errno = 0;
263                 info.si_addr = (void *) address;
264                 force_sig_info(SIGSEGV, &info, tsk);
265                 return;
266         }
267
268 no_context:
269 #ifdef DEBUG_FAULT
270         printk("fault:No context\n");
271 #endif
272         /* Are we prepared to handle this kernel fault?  */
273         fixup = search_exception_tables(regs->pc);
274         if (fixup) {
275                 regs->pc = fixup->fixup;
276                 return;
277         }
278
279 /*
280  * Oops. The kernel tried to access some bad page. We'll have to
281  * terminate things with extreme prejudice.
282  *
283  */
284         if (address < PAGE_SIZE)
285                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
286         else
287                 printk(KERN_ALERT "Unable to handle kernel paging request");
288         printk(" at virtual address %08lx\n", address);
289         printk(KERN_ALERT "pc = %08Lx%08Lx\n", regs->pc >> 32, regs->pc & 0xffffffff);
290         die("Oops", regs, writeaccess);
291         do_exit(SIGKILL);
292
293 /*
294  * We ran out of memory, or some other thing happened to us that made
295  * us unable to handle the page fault gracefully.
296  */
297 out_of_memory:
298         up_read(&mm->mmap_sem);
299         if (!user_mode(regs))
300                 goto no_context;
301         pagefault_out_of_memory();
302         return;
303
304 do_sigbus:
305         printk("fault:Do sigbus\n");
306         up_read(&mm->mmap_sem);
307
308         /*
309          * Send a sigbus, regardless of whether we were in kernel
310          * or user mode.
311          */
312         tsk->thread.address = address;
313         tsk->thread.error_code = writeaccess;
314         tsk->thread.trap_no = 14;
315         force_sig(SIGBUS, tsk);
316
317         /* Kernel mode? Handle exceptions or die */
318         if (!user_mode(regs))
319                 goto no_context;
320 }
321
322 void local_flush_tlb_one(unsigned long asid, unsigned long page)
323 {
324         unsigned long long match, pteh=0, lpage;
325         unsigned long tlb;
326
327         /*
328          * Sign-extend based on neff.
329          */
330         lpage = neff_sign_extend(page);
331         match = (asid << PTEH_ASID_SHIFT) | PTEH_VALID;
332         match |= lpage;
333
334         for_each_itlb_entry(tlb) {
335                 asm volatile ("getcfg   %1, 0, %0"
336                               : "=r" (pteh)
337                               : "r" (tlb) );
338
339                 if (pteh == match) {
340                         __flush_tlb_slot(tlb);
341                         break;
342                 }
343         }
344
345         for_each_dtlb_entry(tlb) {
346                 asm volatile ("getcfg   %1, 0, %0"
347                               : "=r" (pteh)
348                               : "r" (tlb) );
349
350                 if (pteh == match) {
351                         __flush_tlb_slot(tlb);
352                         break;
353                 }
354
355         }
356 }
357
358 void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
359 {
360         unsigned long flags;
361
362         if (vma->vm_mm) {
363                 page &= PAGE_MASK;
364                 local_irq_save(flags);
365                 local_flush_tlb_one(get_asid(), page);
366                 local_irq_restore(flags);
367         }
368 }
369
370 void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
371                            unsigned long end)
372 {
373         unsigned long flags;
374         unsigned long long match, pteh=0, pteh_epn, pteh_low;
375         unsigned long tlb;
376         unsigned int cpu = smp_processor_id();
377         struct mm_struct *mm;
378
379         mm = vma->vm_mm;
380         if (cpu_context(cpu, mm) == NO_CONTEXT)
381                 return;
382
383         local_irq_save(flags);
384
385         start &= PAGE_MASK;
386         end &= PAGE_MASK;
387
388         match = (cpu_asid(cpu, mm) << PTEH_ASID_SHIFT) | PTEH_VALID;
389
390         /* Flush ITLB */
391         for_each_itlb_entry(tlb) {
392                 asm volatile ("getcfg   %1, 0, %0"
393                               : "=r" (pteh)
394                               : "r" (tlb) );
395
396                 pteh_epn = pteh & PAGE_MASK;
397                 pteh_low = pteh & ~PAGE_MASK;
398
399                 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
400                         __flush_tlb_slot(tlb);
401         }
402
403         /* Flush DTLB */
404         for_each_dtlb_entry(tlb) {
405                 asm volatile ("getcfg   %1, 0, %0"
406                               : "=r" (pteh)
407                               : "r" (tlb) );
408
409                 pteh_epn = pteh & PAGE_MASK;
410                 pteh_low = pteh & ~PAGE_MASK;
411
412                 if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
413                         __flush_tlb_slot(tlb);
414         }
415
416         local_irq_restore(flags);
417 }
418
419 void local_flush_tlb_mm(struct mm_struct *mm)
420 {
421         unsigned long flags;
422         unsigned int cpu = smp_processor_id();
423
424         if (cpu_context(cpu, mm) == NO_CONTEXT)
425                 return;
426
427         local_irq_save(flags);
428
429         cpu_context(cpu, mm) = NO_CONTEXT;
430         if (mm == current->mm)
431                 activate_context(mm, cpu);
432
433         local_irq_restore(flags);
434 }
435
436 void local_flush_tlb_all(void)
437 {
438         /* Invalidate all, including shared pages, excluding fixed TLBs */
439         unsigned long flags, tlb;
440
441         local_irq_save(flags);
442
443         /* Flush each ITLB entry */
444         for_each_itlb_entry(tlb)
445                 __flush_tlb_slot(tlb);
446
447         /* Flush each DTLB entry */
448         for_each_dtlb_entry(tlb)
449                 __flush_tlb_slot(tlb);
450
451         local_irq_restore(flags);
452 }
453
454 void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
455 {
456         /* FIXME: Optimize this later.. */
457         flush_tlb_all();
458 }
459
460 void __flush_tlb_global(void)
461 {
462         flush_tlb_all();
463 }
464
465 void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
466 {
467 }