Commit | Line | Data |
---|---|---|
5de96121 MS |
1 | /* |
2 | * arch/microblaze/mm/fault.c | |
3 | * | |
4 | * Copyright (C) 2007 Xilinx, Inc. All rights reserved. | |
5 | * | |
6 | * Derived from "arch/ppc/mm/fault.c" | |
7 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) | |
8 | * | |
9 | * Derived from "arch/i386/mm/fault.c" | |
10 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | |
11 | * | |
12 | * Modified by Cort Dougan and Paul Mackerras. | |
13 | * | |
14 | * This file is subject to the terms and conditions of the GNU General | |
15 | * Public License. See the file COPYING in the main directory of this | |
16 | * archive for more details. | |
17 | * | |
18 | */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/signal.h> | |
22 | #include <linux/sched.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/string.h> | |
26 | #include <linux/types.h> | |
27 | #include <linux/ptrace.h> | |
28 | #include <linux/mman.h> | |
29 | #include <linux/mm.h> | |
30 | #include <linux/interrupt.h> | |
31 | ||
32 | #include <asm/page.h> | |
33 | #include <asm/pgtable.h> | |
34 | #include <asm/mmu.h> | |
35 | #include <asm/mmu_context.h> | |
5de96121 MS |
36 | #include <linux/uaccess.h> |
37 | #include <asm/exceptions.h> | |
38 | ||
5de96121 MS |
39 | static unsigned long pte_misses; /* updated by do_page_fault() */ |
40 | static unsigned long pte_errors; /* updated by do_page_fault() */ | |
41 | ||
42 | /* | |
43 | * Check whether the instruction at regs->pc is a store using | |
44 | * an update addressing form which will update r1. | |
45 | */ | |
46 | static int store_updates_sp(struct pt_regs *regs) | |
47 | { | |
48 | unsigned int inst; | |
49 | ||
5213a9c3 | 50 | if (get_user(inst, (unsigned int __user *)regs->pc)) |
5de96121 MS |
51 | return 0; |
52 | /* check for 1 in the rD field */ | |
53 | if (((inst >> 21) & 0x1f) != 1) | |
54 | return 0; | |
55 | /* check for store opcodes */ | |
56 | if ((inst & 0xd0000000) == 0xd0000000) | |
57 | return 1; | |
58 | return 0; | |
59 | } | |
60 | ||
61 | ||
62 | /* | |
63 | * bad_page_fault is called when we have a bad access from the kernel. | |
64 | * It is called from do_page_fault above and from some of the procedures | |
65 | * in traps.c. | |
66 | */ | |
3863dbce | 67 | void bad_page_fault(struct pt_regs *regs, unsigned long address, int sig) |
5de96121 MS |
68 | { |
69 | const struct exception_table_entry *fixup; | |
70 | /* MS: no context */ | |
71 | /* Are we prepared to handle this fault? */ | |
72 | fixup = search_exception_tables(regs->pc); | |
73 | if (fixup) { | |
74 | regs->pc = fixup->fixup; | |
75 | return; | |
76 | } | |
77 | ||
78 | /* kernel has accessed a bad area */ | |
5de96121 MS |
79 | die("kernel access of bad area", regs, sig); |
80 | } | |
81 | ||
82 | /* | |
83 | * The error_code parameter is ESR for a data fault, | |
84 | * 0 for an instruction fault. | |
85 | */ | |
86 | void do_page_fault(struct pt_regs *regs, unsigned long address, | |
87 | unsigned long error_code) | |
88 | { | |
89 | struct vm_area_struct *vma; | |
90 | struct mm_struct *mm = current->mm; | |
91 | siginfo_t info; | |
92 | int code = SEGV_MAPERR; | |
93 | int is_write = error_code & ESR_S; | |
94 | int fault; | |
f397c077 KC |
95 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE | |
96 | (is_write ? FAULT_FLAG_WRITE : 0); | |
5de96121 MS |
97 | |
98 | regs->ear = address; | |
99 | regs->esr = error_code; | |
100 | ||
101 | /* On a kernel SLB miss we can only check for a valid exception entry */ | |
78ebfa88 | 102 | if (unlikely(kernel_mode(regs) && (address >= TASK_SIZE))) { |
5de96121 MS |
103 | printk(KERN_WARNING "kernel task_size exceed"); |
104 | _exception(SIGSEGV, regs, code, address); | |
105 | } | |
106 | ||
107 | /* for instr TLB miss and instr storage exception ESR_S is undefined */ | |
108 | if ((error_code & 0x13) == 0x13 || (error_code & 0x11) == 0x11) | |
109 | is_write = 0; | |
110 | ||
78ebfa88 | 111 | if (unlikely(in_atomic() || !mm)) { |
f10eca6e MS |
112 | if (kernel_mode(regs)) |
113 | goto bad_area_nosemaphore; | |
114 | ||
5de96121 MS |
115 | /* in_atomic() in user mode is really bad, |
116 | as is current->mm == NULL. */ | |
117 | printk(KERN_EMERG "Page fault in user mode with " | |
118 | "in_atomic(), mm = %p\n", mm); | |
119 | printk(KERN_EMERG "r15 = %lx MSR = %lx\n", | |
120 | regs->r15, regs->msr); | |
121 | die("Weird page fault", regs, SIGSEGV); | |
122 | } | |
123 | ||
124 | /* When running in the kernel we expect faults to occur only to | |
125 | * addresses in user space. All other faults represent errors in the | |
126 | * kernel and should generate an OOPS. Unfortunately, in the case of an | |
127 | * erroneous fault occurring in a code path which already holds mmap_sem | |
128 | * we will deadlock attempting to validate the fault against the | |
129 | * address space. Luckily the kernel only validly references user | |
130 | * space from well defined areas of code, which are listed in the | |
131 | * exceptions table. | |
132 | * | |
133 | * As the vast majority of faults will be valid we will only perform | |
134 | * the source reference check when there is a possibility of a deadlock. | |
135 | * Attempt to lock the address space, if we cannot we then validate the | |
136 | * source. If this is invalid we can skip the address space check, | |
137 | * thus avoiding the deadlock. | |
138 | */ | |
78ebfa88 | 139 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { |
5de96121 MS |
140 | if (kernel_mode(regs) && !search_exception_tables(regs->pc)) |
141 | goto bad_area_nosemaphore; | |
142 | ||
f397c077 | 143 | retry: |
5de96121 MS |
144 | down_read(&mm->mmap_sem); |
145 | } | |
146 | ||
147 | vma = find_vma(mm, address); | |
78ebfa88 | 148 | if (unlikely(!vma)) |
5de96121 MS |
149 | goto bad_area; |
150 | ||
151 | if (vma->vm_start <= address) | |
152 | goto good_area; | |
153 | ||
78ebfa88 | 154 | if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) |
5de96121 MS |
155 | goto bad_area; |
156 | ||
78ebfa88 | 157 | if (unlikely(!is_write)) |
5de96121 MS |
158 | goto bad_area; |
159 | ||
160 | /* | |
161 | * N.B. The ABI allows programs to access up to | |
162 | * a few hundred bytes below the stack pointer (TBD). | |
163 | * The kernel signal delivery code writes up to about 1.5kB | |
164 | * below the stack pointer (r1) before decrementing it. | |
165 | * The exec code can write slightly over 640kB to the stack | |
166 | * before setting the user r1. Thus we allow the stack to | |
167 | * expand to 1MB without further checks. | |
168 | */ | |
78ebfa88 | 169 | if (unlikely(address + 0x100000 < vma->vm_end)) { |
5de96121 MS |
170 | |
171 | /* get user regs even if this fault is in kernel mode */ | |
172 | struct pt_regs *uregs = current->thread.regs; | |
173 | if (uregs == NULL) | |
174 | goto bad_area; | |
175 | ||
176 | /* | |
177 | * A user-mode access to an address a long way below | |
178 | * the stack pointer is only valid if the instruction | |
179 | * is one which would update the stack pointer to the | |
180 | * address accessed if the instruction completed, | |
181 | * i.e. either stwu rs,n(r1) or stwux rs,r1,rb | |
182 | * (or the byte, halfword, float or double forms). | |
183 | * | |
184 | * If we don't check this then any write to the area | |
185 | * between the last mapped region and the stack will | |
186 | * expand the stack rather than segfaulting. | |
187 | */ | |
188 | if (address + 2048 < uregs->r1 | |
189 | && (kernel_mode(regs) || !store_updates_sp(regs))) | |
190 | goto bad_area; | |
191 | } | |
192 | if (expand_stack(vma, address)) | |
193 | goto bad_area; | |
194 | ||
195 | good_area: | |
196 | code = SEGV_ACCERR; | |
197 | ||
198 | /* a write */ | |
78ebfa88 MS |
199 | if (unlikely(is_write)) { |
200 | if (unlikely(!(vma->vm_flags & VM_WRITE))) | |
5de96121 MS |
201 | goto bad_area; |
202 | /* a read */ | |
203 | } else { | |
204 | /* protection fault */ | |
78ebfa88 | 205 | if (unlikely(error_code & 0x08000000)) |
5de96121 | 206 | goto bad_area; |
78ebfa88 | 207 | if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC)))) |
5de96121 MS |
208 | goto bad_area; |
209 | } | |
210 | ||
211 | /* | |
212 | * If for any reason at all we couldn't handle the fault, | |
213 | * make sure we exit gracefully rather than endlessly redo | |
214 | * the fault. | |
215 | */ | |
f397c077 KC |
216 | fault = handle_mm_fault(mm, vma, address, flags); |
217 | ||
218 | if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) | |
219 | return; | |
220 | ||
5de96121 MS |
221 | if (unlikely(fault & VM_FAULT_ERROR)) { |
222 | if (fault & VM_FAULT_OOM) | |
223 | goto out_of_memory; | |
224 | else if (fault & VM_FAULT_SIGBUS) | |
225 | goto do_sigbus; | |
226 | BUG(); | |
227 | } | |
f397c077 KC |
228 | |
229 | if (flags & FAULT_FLAG_ALLOW_RETRY) { | |
230 | if (unlikely(fault & VM_FAULT_MAJOR)) | |
231 | current->maj_flt++; | |
232 | else | |
233 | current->min_flt++; | |
234 | if (fault & VM_FAULT_RETRY) { | |
235 | flags &= ~FAULT_FLAG_ALLOW_RETRY; | |
236 | ||
237 | /* | |
238 | * No need to up_read(&mm->mmap_sem) as we would | |
239 | * have already released it in __lock_page_or_retry | |
240 | * in mm/filemap.c. | |
241 | */ | |
242 | ||
243 | goto retry; | |
244 | } | |
245 | } | |
246 | ||
5de96121 | 247 | up_read(&mm->mmap_sem); |
f397c077 | 248 | |
5de96121 MS |
249 | /* |
250 | * keep track of tlb+htab misses that are good addrs but | |
251 | * just need pte's created via handle_mm_fault() | |
252 | * -- Cort | |
253 | */ | |
254 | pte_misses++; | |
255 | return; | |
256 | ||
257 | bad_area: | |
258 | up_read(&mm->mmap_sem); | |
259 | ||
260 | bad_area_nosemaphore: | |
261 | pte_errors++; | |
262 | ||
263 | /* User mode accesses cause a SIGSEGV */ | |
264 | if (user_mode(regs)) { | |
265 | _exception(SIGSEGV, regs, code, address); | |
266 | /* info.si_signo = SIGSEGV; | |
267 | info.si_errno = 0; | |
268 | info.si_code = code; | |
269 | info.si_addr = (void *) address; | |
270 | force_sig_info(SIGSEGV, &info, current);*/ | |
271 | return; | |
272 | } | |
273 | ||
274 | bad_page_fault(regs, address, SIGSEGV); | |
275 | return; | |
276 | ||
277 | /* | |
278 | * We ran out of memory, or some other thing happened to us that made | |
279 | * us unable to handle the page fault gracefully. | |
280 | */ | |
281 | out_of_memory: | |
5de96121 | 282 | up_read(&mm->mmap_sem); |
21e1c936 NP |
283 | if (!user_mode(regs)) |
284 | bad_page_fault(regs, address, SIGKILL); | |
285 | else | |
286 | pagefault_out_of_memory(); | |
5de96121 MS |
287 | return; |
288 | ||
289 | do_sigbus: | |
290 | up_read(&mm->mmap_sem); | |
291 | if (user_mode(regs)) { | |
292 | info.si_signo = SIGBUS; | |
293 | info.si_errno = 0; | |
294 | info.si_code = BUS_ADRERR; | |
295 | info.si_addr = (void __user *)address; | |
296 | force_sig_info(SIGBUS, &info, current); | |
297 | return; | |
298 | } | |
299 | bad_page_fault(regs, address, SIGBUS); | |
300 | } |