]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/metag/mm/fault.c
Merge tag 'metag-v3.9-rc1-v4' of git://git.kernel.org/pub/scm/linux/kernel/git/jhogan...
[karo-tx-linux.git] / arch / metag / mm / fault.c
1 /*
2  *  Meta page fault handling.
3  *
4  *  Copyright (C) 2005-2012 Imagination Technologies Ltd.
5  */
6
7 #include <linux/mman.h>
8 #include <linux/mm.h>
9 #include <linux/kernel.h>
10 #include <linux/ptrace.h>
11 #include <linux/interrupt.h>
12 #include <linux/uaccess.h>
13
14 #include <asm/tlbflush.h>
15 #include <asm/mmu.h>
16 #include <asm/traps.h>
17
18 /* Clear any pending catch buffer state. */
19 static void clear_cbuf_entry(struct pt_regs *regs, unsigned long addr,
20                              unsigned int trapno)
21 {
22         PTBICTXEXTCB0 cbuf = regs->extcb0;
23
24         switch (trapno) {
25                 /* Instruction fetch faults leave no catch buffer state. */
26         case TBIXXF_SIGNUM_IGF:
27         case TBIXXF_SIGNUM_IPF:
28                 return;
29         default:
30                 if (cbuf[0].CBAddr == addr) {
31                         cbuf[0].CBAddr = 0;
32                         cbuf[0].CBFlags &= ~TXCATCH0_FAULT_BITS;
33
34                         /* And, as this is the ONLY catch entry, we
35                          * need to clear the cbuf bit from the context!
36                          */
37                         regs->ctx.SaveMask &= ~(TBICTX_CBUF_BIT |
38                                                 TBICTX_XCBF_BIT);
39
40                         return;
41                 }
42                 pr_err("Failed to clear cbuf entry!\n");
43         }
44 }
45
46 int show_unhandled_signals = 1;
47
48 int do_page_fault(struct pt_regs *regs, unsigned long address,
49                   unsigned int write_access, unsigned int trapno)
50 {
51         struct task_struct *tsk;
52         struct mm_struct *mm;
53         struct vm_area_struct *vma, *prev_vma;
54         siginfo_t info;
55         int fault;
56         unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
57                                 (write_access ? FAULT_FLAG_WRITE : 0);
58
59         tsk = current;
60
61         if ((address >= VMALLOC_START) && (address < VMALLOC_END)) {
62                 /*
63                  * Synchronize this task's top level page-table
64                  * with the 'reference' page table.
65                  *
66                  * Do _not_ use "tsk" here. We might be inside
67                  * an interrupt in the middle of a task switch..
68                  */
69                 int offset = pgd_index(address);
70                 pgd_t *pgd, *pgd_k;
71                 pud_t *pud, *pud_k;
72                 pmd_t *pmd, *pmd_k;
73                 pte_t *pte_k;
74
75                 pgd = ((pgd_t *)mmu_get_base()) + offset;
76                 pgd_k = swapper_pg_dir + offset;
77
78                 /* This will never happen with the folded page table. */
79                 if (!pgd_present(*pgd)) {
80                         if (!pgd_present(*pgd_k))
81                                 goto bad_area_nosemaphore;
82                         set_pgd(pgd, *pgd_k);
83                         return 0;
84                 }
85
86                 pud = pud_offset(pgd, address);
87                 pud_k = pud_offset(pgd_k, address);
88                 if (!pud_present(*pud_k))
89                         goto bad_area_nosemaphore;
90                 set_pud(pud, *pud_k);
91
92                 pmd = pmd_offset(pud, address);
93                 pmd_k = pmd_offset(pud_k, address);
94                 if (!pmd_present(*pmd_k))
95                         goto bad_area_nosemaphore;
96                 set_pmd(pmd, *pmd_k);
97
98                 pte_k = pte_offset_kernel(pmd_k, address);
99                 if (!pte_present(*pte_k))
100                         goto bad_area_nosemaphore;
101
102                 /* May only be needed on Chorus2 */
103                 flush_tlb_all();
104                 return 0;
105         }
106
107         mm = tsk->mm;
108
109         if (in_atomic() || !mm)
110                 goto no_context;
111
112 retry:
113         down_read(&mm->mmap_sem);
114
115         vma = find_vma_prev(mm, address, &prev_vma);
116
117         if (!vma || address < vma->vm_start)
118                 goto check_expansion;
119
120 good_area:
121         if (write_access) {
122                 if (!(vma->vm_flags & VM_WRITE))
123                         goto bad_area;
124         } else {
125                 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
126                         goto bad_area;
127         }
128
129         /*
130          * If for any reason at all we couldn't handle the fault,
131          * make sure we exit gracefully rather than endlessly redo
132          * the fault.
133          */
134         fault = handle_mm_fault(mm, vma, address, flags);
135
136         if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
137                 return 0;
138
139         if (unlikely(fault & VM_FAULT_ERROR)) {
140                 if (fault & VM_FAULT_OOM)
141                         goto out_of_memory;
142                 else if (fault & VM_FAULT_SIGBUS)
143                         goto do_sigbus;
144                 BUG();
145         }
146         if (flags & FAULT_FLAG_ALLOW_RETRY) {
147                 if (fault & VM_FAULT_MAJOR)
148                         tsk->maj_flt++;
149                 else
150                         tsk->min_flt++;
151                 if (fault & VM_FAULT_RETRY) {
152                         flags &= ~FAULT_FLAG_ALLOW_RETRY;
153                         flags |= FAULT_FLAG_TRIED;
154
155                         /*
156                          * No need to up_read(&mm->mmap_sem) as we would
157                          * have already released it in __lock_page_or_retry
158                          * in mm/filemap.c.
159                          */
160
161                         goto retry;
162                 }
163         }
164
165         up_read(&mm->mmap_sem);
166         return 0;
167
168 check_expansion:
169         vma = prev_vma;
170         if (vma && (expand_stack(vma, address) == 0))
171                 goto good_area;
172
173 bad_area:
174         up_read(&mm->mmap_sem);
175
176 bad_area_nosemaphore:
177         if (user_mode(regs)) {
178                 info.si_signo = SIGSEGV;
179                 info.si_errno = 0;
180                 info.si_code = SEGV_MAPERR;
181                 info.si_addr = (__force void __user *)address;
182                 info.si_trapno = trapno;
183
184                 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
185                     printk_ratelimit()) {
186                         pr_info("%s%s[%d]: segfault at %lx pc %08x sp %08x write %d trap %#x (%s)",
187                                task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
188                                tsk->comm, task_pid_nr(tsk), address,
189                                regs->ctx.CurrPC, regs->ctx.AX[0].U0,
190                                write_access, trapno, trap_name(trapno));
191                         print_vma_addr(" in ", regs->ctx.CurrPC);
192                         print_vma_addr(" rtp in ", regs->ctx.DX[4].U1);
193                         printk("\n");
194                         show_regs(regs);
195                 }
196                 force_sig_info(SIGSEGV, &info, tsk);
197                 return 1;
198         }
199         goto no_context;
200
201 do_sigbus:
202         up_read(&mm->mmap_sem);
203
204         /*
205          * Send a sigbus, regardless of whether we were in kernel
206          * or user mode.
207          */
208         info.si_signo = SIGBUS;
209         info.si_errno = 0;
210         info.si_code = BUS_ADRERR;
211         info.si_addr = (__force void __user *)address;
212         info.si_trapno = trapno;
213         force_sig_info(SIGBUS, &info, tsk);
214
215         /* Kernel mode? Handle exceptions or die */
216         if (!user_mode(regs))
217                 goto no_context;
218
219         return 1;
220
221         /*
222          * We ran out of memory, or some other thing happened to us that made
223          * us unable to handle the page fault gracefully.
224          */
225 out_of_memory:
226         up_read(&mm->mmap_sem);
227         if (user_mode(regs))
228                 do_group_exit(SIGKILL);
229
230 no_context:
231         /* Are we prepared to handle this kernel fault?  */
232         if (fixup_exception(regs)) {
233                 clear_cbuf_entry(regs, address, trapno);
234                 return 1;
235         }
236
237         die("Oops", regs, (write_access << 15) | trapno, address);
238         do_exit(SIGKILL);
239 }