Commit | Line | Data |
---|---|---|
f5df8e26 JH |
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; | |
759496ba | 56 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; |
f5df8e26 JH |
57 | |
58 | tsk = current; | |
59 | ||
60 | if ((address >= VMALLOC_START) && (address < VMALLOC_END)) { | |
61 | /* | |
62 | * Synchronize this task's top level page-table | |
63 | * with the 'reference' page table. | |
64 | * | |
65 | * Do _not_ use "tsk" here. We might be inside | |
66 | * an interrupt in the middle of a task switch.. | |
67 | */ | |
68 | int offset = pgd_index(address); | |
69 | pgd_t *pgd, *pgd_k; | |
70 | pud_t *pud, *pud_k; | |
71 | pmd_t *pmd, *pmd_k; | |
72 | pte_t *pte_k; | |
73 | ||
74 | pgd = ((pgd_t *)mmu_get_base()) + offset; | |
75 | pgd_k = swapper_pg_dir + offset; | |
76 | ||
77 | /* This will never happen with the folded page table. */ | |
78 | if (!pgd_present(*pgd)) { | |
79 | if (!pgd_present(*pgd_k)) | |
80 | goto bad_area_nosemaphore; | |
81 | set_pgd(pgd, *pgd_k); | |
82 | return 0; | |
83 | } | |
84 | ||
85 | pud = pud_offset(pgd, address); | |
86 | pud_k = pud_offset(pgd_k, address); | |
87 | if (!pud_present(*pud_k)) | |
88 | goto bad_area_nosemaphore; | |
89 | set_pud(pud, *pud_k); | |
90 | ||
91 | pmd = pmd_offset(pud, address); | |
92 | pmd_k = pmd_offset(pud_k, address); | |
93 | if (!pmd_present(*pmd_k)) | |
94 | goto bad_area_nosemaphore; | |
95 | set_pmd(pmd, *pmd_k); | |
96 | ||
97 | pte_k = pte_offset_kernel(pmd_k, address); | |
98 | if (!pte_present(*pte_k)) | |
99 | goto bad_area_nosemaphore; | |
100 | ||
101 | /* May only be needed on Chorus2 */ | |
102 | flush_tlb_all(); | |
103 | return 0; | |
104 | } | |
105 | ||
106 | mm = tsk->mm; | |
107 | ||
70ffdb93 | 108 | if (faulthandler_disabled() || !mm) |
f5df8e26 JH |
109 | goto no_context; |
110 | ||
759496ba JW |
111 | if (user_mode(regs)) |
112 | flags |= FAULT_FLAG_USER; | |
f5df8e26 JH |
113 | retry: |
114 | down_read(&mm->mmap_sem); | |
115 | ||
116 | vma = find_vma_prev(mm, address, &prev_vma); | |
117 | ||
118 | if (!vma || address < vma->vm_start) | |
119 | goto check_expansion; | |
120 | ||
121 | good_area: | |
122 | if (write_access) { | |
123 | if (!(vma->vm_flags & VM_WRITE)) | |
124 | goto bad_area; | |
759496ba | 125 | flags |= FAULT_FLAG_WRITE; |
f5df8e26 JH |
126 | } else { |
127 | if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) | |
128 | goto bad_area; | |
129 | } | |
130 | ||
131 | /* | |
132 | * If for any reason at all we couldn't handle the fault, | |
133 | * make sure we exit gracefully rather than endlessly redo | |
134 | * the fault. | |
135 | */ | |
136 | fault = handle_mm_fault(mm, vma, address, flags); | |
137 | ||
138 | if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) | |
139 | return 0; | |
140 | ||
141 | if (unlikely(fault & VM_FAULT_ERROR)) { | |
142 | if (fault & VM_FAULT_OOM) | |
143 | goto out_of_memory; | |
33692f27 LT |
144 | else if (fault & VM_FAULT_SIGSEGV) |
145 | goto bad_area; | |
f5df8e26 JH |
146 | else if (fault & VM_FAULT_SIGBUS) |
147 | goto do_sigbus; | |
148 | BUG(); | |
149 | } | |
150 | if (flags & FAULT_FLAG_ALLOW_RETRY) { | |
151 | if (fault & VM_FAULT_MAJOR) | |
152 | tsk->maj_flt++; | |
153 | else | |
154 | tsk->min_flt++; | |
155 | if (fault & VM_FAULT_RETRY) { | |
156 | flags &= ~FAULT_FLAG_ALLOW_RETRY; | |
157 | flags |= FAULT_FLAG_TRIED; | |
158 | ||
159 | /* | |
160 | * No need to up_read(&mm->mmap_sem) as we would | |
161 | * have already released it in __lock_page_or_retry | |
162 | * in mm/filemap.c. | |
163 | */ | |
164 | ||
165 | goto retry; | |
166 | } | |
167 | } | |
168 | ||
169 | up_read(&mm->mmap_sem); | |
170 | return 0; | |
171 | ||
172 | check_expansion: | |
173 | vma = prev_vma; | |
174 | if (vma && (expand_stack(vma, address) == 0)) | |
175 | goto good_area; | |
176 | ||
177 | bad_area: | |
178 | up_read(&mm->mmap_sem); | |
179 | ||
180 | bad_area_nosemaphore: | |
181 | if (user_mode(regs)) { | |
182 | info.si_signo = SIGSEGV; | |
183 | info.si_errno = 0; | |
184 | info.si_code = SEGV_MAPERR; | |
185 | info.si_addr = (__force void __user *)address; | |
186 | info.si_trapno = trapno; | |
187 | ||
188 | if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) && | |
189 | printk_ratelimit()) { | |
190 | pr_info("%s%s[%d]: segfault at %lx pc %08x sp %08x write %d trap %#x (%s)", | |
191 | task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG, | |
192 | tsk->comm, task_pid_nr(tsk), address, | |
193 | regs->ctx.CurrPC, regs->ctx.AX[0].U0, | |
194 | write_access, trapno, trap_name(trapno)); | |
195 | print_vma_addr(" in ", regs->ctx.CurrPC); | |
196 | print_vma_addr(" rtp in ", regs->ctx.DX[4].U1); | |
197 | printk("\n"); | |
198 | show_regs(regs); | |
199 | } | |
200 | force_sig_info(SIGSEGV, &info, tsk); | |
201 | return 1; | |
202 | } | |
203 | goto no_context; | |
204 | ||
205 | do_sigbus: | |
206 | up_read(&mm->mmap_sem); | |
207 | ||
208 | /* | |
209 | * Send a sigbus, regardless of whether we were in kernel | |
210 | * or user mode. | |
211 | */ | |
212 | info.si_signo = SIGBUS; | |
213 | info.si_errno = 0; | |
214 | info.si_code = BUS_ADRERR; | |
215 | info.si_addr = (__force void __user *)address; | |
216 | info.si_trapno = trapno; | |
217 | force_sig_info(SIGBUS, &info, tsk); | |
218 | ||
219 | /* Kernel mode? Handle exceptions or die */ | |
220 | if (!user_mode(regs)) | |
221 | goto no_context; | |
222 | ||
223 | return 1; | |
224 | ||
225 | /* | |
226 | * We ran out of memory, or some other thing happened to us that made | |
227 | * us unable to handle the page fault gracefully. | |
228 | */ | |
229 | out_of_memory: | |
230 | up_read(&mm->mmap_sem); | |
609838cf JW |
231 | if (user_mode(regs)) { |
232 | pagefault_out_of_memory(); | |
233 | return 1; | |
234 | } | |
f5df8e26 JH |
235 | |
236 | no_context: | |
237 | /* Are we prepared to handle this kernel fault? */ | |
238 | if (fixup_exception(regs)) { | |
239 | clear_cbuf_entry(regs, address, trapno); | |
240 | return 1; | |
241 | } | |
242 | ||
243 | die("Oops", regs, (write_access << 15) | trapno, address); | |
244 | do_exit(SIGKILL); | |
245 | } |