diff options
Diffstat (limited to 'arch/sh/mm/fault.c')
-rw-r--r-- | arch/sh/mm/fault.c | 508 |
1 files changed, 508 insertions, 0 deletions
diff --git a/arch/sh/mm/fault.c b/arch/sh/mm/fault.c new file mode 100644 index 00000000000..16799f920f9 --- /dev/null +++ b/arch/sh/mm/fault.c | |||
@@ -0,0 +1,508 @@ | |||
1 | /* | ||
2 | * Page fault handler for SH with an MMU. | ||
3 | * | ||
4 | * Copyright (C) 1999 Niibe Yutaka | ||
5 | * Copyright (C) 2003 - 2012 Paul Mundt | ||
6 | * | ||
7 | * Based on linux/arch/i386/mm/fault.c: | ||
8 | * Copyright (C) 1995 Linus Torvalds | ||
9 | * | ||
10 | * This file is subject to the terms and conditions of the GNU General Public | ||
11 | * License. See the file "COPYING" in the main directory of this archive | ||
12 | * for more details. | ||
13 | */ | ||
14 | #include <linux/kernel.h> | ||
15 | #include <linux/mm.h> | ||
16 | #include <linux/hardirq.h> | ||
17 | #include <linux/kprobes.h> | ||
18 | #include <linux/perf_event.h> | ||
19 | #include <linux/kdebug.h> | ||
20 | #include <asm/io_trapped.h> | ||
21 | #include <asm/mmu_context.h> | ||
22 | #include <asm/tlbflush.h> | ||
23 | #include <asm/traps.h> | ||
24 | |||
25 | static inline int notify_page_fault(struct pt_regs *regs, int trap) | ||
26 | { | ||
27 | int ret = 0; | ||
28 | |||
29 | if (kprobes_built_in() && !user_mode(regs)) { | ||
30 | preempt_disable(); | ||
31 | if (kprobe_running() && kprobe_fault_handler(regs, trap)) | ||
32 | ret = 1; | ||
33 | preempt_enable(); | ||
34 | } | ||
35 | |||
36 | return ret; | ||
37 | } | ||
38 | |||
39 | static void | ||
40 | force_sig_info_fault(int si_signo, int si_code, unsigned long address, | ||
41 | struct task_struct *tsk) | ||
42 | { | ||
43 | siginfo_t info; | ||
44 | |||
45 | info.si_signo = si_signo; | ||
46 | info.si_errno = 0; | ||
47 | info.si_code = si_code; | ||
48 | info.si_addr = (void __user *)address; | ||
49 | |||
50 | force_sig_info(si_signo, &info, tsk); | ||
51 | } | ||
52 | |||
53 | /* | ||
54 | * This is useful to dump out the page tables associated with | ||
55 | * 'addr' in mm 'mm'. | ||
56 | */ | ||
57 | static void show_pte(struct mm_struct *mm, unsigned long addr) | ||
58 | { | ||
59 | pgd_t *pgd; | ||
60 | |||
61 | if (mm) | ||
62 | pgd = mm->pgd; | ||
63 | else | ||
64 | pgd = get_TTB(); | ||
65 | |||
66 | printk(KERN_ALERT "pgd = %p\n", pgd); | ||
67 | pgd += pgd_index(addr); | ||
68 | printk(KERN_ALERT "[%08lx] *pgd=%0*Lx", addr, | ||
69 | (u32)(sizeof(*pgd) * 2), (u64)pgd_val(*pgd)); | ||
70 | |||
71 | do { | ||
72 | pud_t *pud; | ||
73 | pmd_t *pmd; | ||
74 | pte_t *pte; | ||
75 | |||
76 | if (pgd_none(*pgd)) | ||
77 | break; | ||
78 | |||
79 | if (pgd_bad(*pgd)) { | ||
80 | printk("(bad)"); | ||
81 | break; | ||
82 | } | ||
83 | |||
84 | pud = pud_offset(pgd, addr); | ||
85 | if (PTRS_PER_PUD != 1) | ||
86 | printk(", *pud=%0*Lx", (u32)(sizeof(*pud) * 2), | ||
87 | (u64)pud_val(*pud)); | ||
88 | |||
89 | if (pud_none(*pud)) | ||
90 | break; | ||
91 | |||
92 | if (pud_bad(*pud)) { | ||
93 | printk("(bad)"); | ||
94 | break; | ||
95 | } | ||
96 | |||
97 | pmd = pmd_offset(pud, addr); | ||
98 | if (PTRS_PER_PMD != 1) | ||
99 | printk(", *pmd=%0*Lx", (u32)(sizeof(*pmd) * 2), | ||
100 | (u64)pmd_val(*pmd)); | ||
101 | |||
102 | if (pmd_none(*pmd)) | ||
103 | break; | ||
104 | |||
105 | if (pmd_bad(*pmd)) { | ||
106 | printk("(bad)"); | ||
107 | break; | ||
108 | } | ||
109 | |||
110 | /* We must not map this if we have highmem enabled */ | ||
111 | if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT))) | ||
112 | break; | ||
113 | |||
114 | pte = pte_offset_kernel(pmd, addr); | ||
115 | printk(", *pte=%0*Lx", (u32)(sizeof(*pte) * 2), | ||
116 | (u64)pte_val(*pte)); | ||
117 | } while (0); | ||
118 | |||
119 | printk("\n"); | ||
120 | } | ||
121 | |||
122 | static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) | ||
123 | { | ||
124 | unsigned index = pgd_index(address); | ||
125 | pgd_t *pgd_k; | ||
126 | pud_t *pud, *pud_k; | ||
127 | pmd_t *pmd, *pmd_k; | ||
128 | |||
129 | pgd += index; | ||
130 | pgd_k = init_mm.pgd + index; | ||
131 | |||
132 | if (!pgd_present(*pgd_k)) | ||
133 | return NULL; | ||
134 | |||
135 | pud = pud_offset(pgd, address); | ||
136 | pud_k = pud_offset(pgd_k, address); | ||
137 | if (!pud_present(*pud_k)) | ||
138 | return NULL; | ||
139 | |||
140 | if (!pud_present(*pud)) | ||
141 | set_pud(pud, *pud_k); | ||
142 | |||
143 | pmd = pmd_offset(pud, address); | ||
144 | pmd_k = pmd_offset(pud_k, address); | ||
145 | if (!pmd_present(*pmd_k)) | ||
146 | return NULL; | ||
147 | |||
148 | if (!pmd_present(*pmd)) | ||
149 | set_pmd(pmd, *pmd_k); | ||
150 | else { | ||
151 | /* | ||
152 | * The page tables are fully synchronised so there must | ||
153 | * be another reason for the fault. Return NULL here to | ||
154 | * signal that we have not taken care of the fault. | ||
155 | */ | ||
156 | BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k)); | ||
157 | return NULL; | ||
158 | } | ||
159 | |||
160 | return pmd_k; | ||
161 | } | ||
162 | |||
163 | /* | ||
164 | * Handle a fault on the vmalloc or module mapping area | ||
165 | */ | ||
166 | static noinline int vmalloc_fault(unsigned long address) | ||
167 | { | ||
168 | pgd_t *pgd_k; | ||
169 | pmd_t *pmd_k; | ||
170 | pte_t *pte_k; | ||
171 | |||
172 | /* Make sure we are in vmalloc/module area: */ | ||
173 | if (!is_vmalloc_addr((void *)address)) | ||
174 | return -1; | ||
175 | |||
176 | /* | ||
177 | * Synchronize this task's top level page-table | ||
178 | * with the 'reference' page table. | ||
179 | * | ||
180 | * Do _not_ use "current" here. We might be inside | ||
181 | * an interrupt in the middle of a task switch.. | ||
182 | */ | ||
183 | pgd_k = get_TTB(); | ||
184 | pmd_k = vmalloc_sync_one(pgd_k, address); | ||
185 | if (!pmd_k) | ||
186 | return -1; | ||
187 | |||
188 | pte_k = pte_offset_kernel(pmd_k, address); | ||
189 | if (!pte_present(*pte_k)) | ||
190 | return -1; | ||
191 | |||
192 | return 0; | ||
193 | } | ||
194 | |||
195 | static void | ||
196 | show_fault_oops(struct pt_regs *regs, unsigned long address) | ||
197 | { | ||
198 | if (!oops_may_print()) | ||
199 | return; | ||
200 | |||
201 | printk(KERN_ALERT "BUG: unable to handle kernel "); | ||
202 | if (address < PAGE_SIZE) | ||
203 | printk(KERN_CONT "NULL pointer dereference"); | ||
204 | else | ||
205 | printk(KERN_CONT "paging request"); | ||
206 | |||
207 | printk(KERN_CONT " at %08lx\n", address); | ||
208 | printk(KERN_ALERT "PC:"); | ||
209 | printk_address(regs->pc, 1); | ||
210 | |||
211 | show_pte(NULL, address); | ||
212 | } | ||
213 | |||
214 | static noinline void | ||
215 | no_context(struct pt_regs *regs, unsigned long error_code, | ||
216 | unsigned long address) | ||
217 | { | ||
218 | /* Are we prepared to handle this kernel fault? */ | ||
219 | if (fixup_exception(regs)) | ||
220 | return; | ||
221 | |||
222 | if (handle_trapped_io(regs, address)) | ||
223 | return; | ||
224 | |||
225 | /* | ||
226 | * Oops. The kernel tried to access some bad page. We'll have to | ||
227 | * terminate things with extreme prejudice. | ||
228 | */ | ||
229 | bust_spinlocks(1); | ||
230 | |||
231 | show_fault_oops(regs, address); | ||
232 | |||
233 | die("Oops", regs, error_code); | ||
234 | bust_spinlocks(0); | ||
235 | do_exit(SIGKILL); | ||
236 | } | ||
237 | |||
238 | static void | ||
239 | __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, | ||
240 | unsigned long address, int si_code) | ||
241 | { | ||
242 | struct task_struct *tsk = current; | ||
243 | |||
244 | /* User mode accesses just cause a SIGSEGV */ | ||
245 | if (user_mode(regs)) { | ||
246 | /* | ||
247 | * It's possible to have interrupts off here: | ||
248 | */ | ||
249 | local_irq_enable(); | ||
250 | |||
251 | force_sig_info_fault(SIGSEGV, si_code, address, tsk); | ||
252 | |||
253 | return; | ||
254 | } | ||
255 | |||
256 | no_context(regs, error_code, address); | ||
257 | } | ||
258 | |||
259 | static noinline void | ||
260 | bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, | ||
261 | unsigned long address) | ||
262 | { | ||
263 | __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR); | ||
264 | } | ||
265 | |||
266 | static void | ||
267 | __bad_area(struct pt_regs *regs, unsigned long error_code, | ||
268 | unsigned long address, int si_code) | ||
269 | { | ||
270 | struct mm_struct *mm = current->mm; | ||
271 | |||
272 | /* | ||
273 | * Something tried to access memory that isn't in our memory map.. | ||
274 | * Fix it, but check if it's kernel or user first.. | ||
275 | */ | ||
276 | up_read(&mm->mmap_sem); | ||
277 | |||
278 | __bad_area_nosemaphore(regs, error_code, address, si_code); | ||
279 | } | ||
280 | |||
281 | static noinline void | ||
282 | bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address) | ||
283 | { | ||
284 | __bad_area(regs, error_code, address, SEGV_MAPERR); | ||
285 | } | ||
286 | |||
287 | static noinline void | ||
288 | bad_area_access_error(struct pt_regs *regs, unsigned long error_code, | ||
289 | unsigned long address) | ||
290 | { | ||
291 | __bad_area(regs, error_code, address, SEGV_ACCERR); | ||
292 | } | ||
293 | |||
294 | static void out_of_memory(void) | ||
295 | { | ||
296 | /* | ||
297 | * We ran out of memory, call the OOM killer, and return the userspace | ||
298 | * (which will retry the fault, or kill us if we got oom-killed): | ||
299 | */ | ||
300 | up_read(¤t->mm->mmap_sem); | ||
301 | |||
302 | pagefault_out_of_memory(); | ||
303 | } | ||
304 | |||
305 | static void | ||
306 | do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address) | ||
307 | { | ||
308 | struct task_struct *tsk = current; | ||
309 | struct mm_struct *mm = tsk->mm; | ||
310 | |||
311 | up_read(&mm->mmap_sem); | ||
312 | |||
313 | /* Kernel mode? Handle exceptions or die: */ | ||
314 | if (!user_mode(regs)) | ||
315 | no_context(regs, error_code, address); | ||
316 | |||
317 | force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk); | ||
318 | } | ||
319 | |||
320 | static noinline int | ||
321 | mm_fault_error(struct pt_regs *regs, unsigned long error_code, | ||
322 | unsigned long address, unsigned int fault) | ||
323 | { | ||
324 | /* | ||
325 | * Pagefault was interrupted by SIGKILL. We have no reason to | ||
326 | * continue pagefault. | ||
327 | */ | ||
328 | if (fatal_signal_pending(current)) { | ||
329 | if (!(fault & VM_FAULT_RETRY)) | ||
330 | up_read(¤t->mm->mmap_sem); | ||
331 | if (!user_mode(regs)) | ||
332 | no_context(regs, error_code, address); | ||
333 | return 1; | ||
334 | } | ||
335 | |||
336 | if (!(fault & VM_FAULT_ERROR)) | ||
337 | return 0; | ||
338 | |||
339 | if (fault & VM_FAULT_OOM) { | ||
340 | /* Kernel mode? Handle exceptions or die: */ | ||
341 | if (!user_mode(regs)) { | ||
342 | up_read(¤t->mm->mmap_sem); | ||
343 | no_context(regs, error_code, address); | ||
344 | return 1; | ||
345 | } | ||
346 | |||
347 | out_of_memory(); | ||
348 | } else { | ||
349 | if (fault & VM_FAULT_SIGBUS) | ||
350 | do_sigbus(regs, error_code, address); | ||
351 | else | ||
352 | BUG(); | ||
353 | } | ||
354 | |||
355 | return 1; | ||
356 | } | ||
357 | |||
358 | static inline int access_error(int error_code, struct vm_area_struct *vma) | ||
359 | { | ||
360 | if (error_code & FAULT_CODE_WRITE) { | ||
361 | /* write, present and write, not present: */ | ||
362 | if (unlikely(!(vma->vm_flags & VM_WRITE))) | ||
363 | return 1; | ||
364 | return 0; | ||
365 | } | ||
366 | |||
367 | /* ITLB miss on NX page */ | ||
368 | if (unlikely((error_code & FAULT_CODE_ITLB) && | ||
369 | !(vma->vm_flags & VM_EXEC))) | ||
370 | return 1; | ||
371 | |||
372 | /* read, not present: */ | ||
373 | if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))) | ||
374 | return 1; | ||
375 | |||
376 | return 0; | ||
377 | } | ||
378 | |||
379 | static int fault_in_kernel_space(unsigned long address) | ||
380 | { | ||
381 | return address >= TASK_SIZE; | ||
382 | } | ||
383 | |||
384 | /* | ||
385 | * This routine handles page faults. It determines the address, | ||
386 | * and the problem, and then passes it off to one of the appropriate | ||
387 | * routines. | ||
388 | */ | ||
389 | asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, | ||
390 | unsigned long error_code, | ||
391 | unsigned long address) | ||
392 | { | ||
393 | unsigned long vec; | ||
394 | struct task_struct *tsk; | ||
395 | struct mm_struct *mm; | ||
396 | struct vm_area_struct * vma; | ||
397 | int fault; | ||
398 | int write = error_code & FAULT_CODE_WRITE; | ||
399 | unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE | | ||
400 | (write ? FAULT_FLAG_WRITE : 0)); | ||
401 | |||
402 | tsk = current; | ||
403 | mm = tsk->mm; | ||
404 | vec = lookup_exception_vector(); | ||
405 | |||
406 | /* | ||
407 | * We fault-in kernel-space virtual memory on-demand. The | ||
408 | * 'reference' page table is init_mm.pgd. | ||
409 | * | ||
410 | * NOTE! We MUST NOT take any locks for this case. We may | ||
411 | * be in an interrupt or a critical region, and should | ||
412 | * only copy the information from the master page table, | ||
413 | * nothing more. | ||
414 | */ | ||
415 | if (unlikely(fault_in_kernel_space(address))) { | ||
416 | if (vmalloc_fault(address) >= 0) | ||
417 | return; | ||
418 | if (notify_page_fault(regs, vec)) | ||
419 | return; | ||
420 | |||
421 | bad_area_nosemaphore(regs, error_code, address); | ||
422 | return; | ||
423 | } | ||
424 | |||
425 | if (unlikely(notify_page_fault(regs, vec))) | ||
426 | return; | ||
427 | |||
428 | /* Only enable interrupts if they were on before the fault */ | ||
429 | if ((regs->sr & SR_IMASK) != SR_IMASK) | ||
430 | local_irq_enable(); | ||
431 | |||
432 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); | ||
433 | |||
434 | /* | ||
435 | * If we're in an interrupt, have no user context or are running | ||
436 | * in an atomic region then we must not take the fault: | ||
437 | */ | ||
438 | if (unlikely(in_atomic() || !mm)) { | ||
439 | bad_area_nosemaphore(regs, error_code, address); | ||
440 | return; | ||
441 | } | ||
442 | |||
443 | retry: | ||
444 | down_read(&mm->mmap_sem); | ||
445 | |||
446 | vma = find_vma(mm, address); | ||
447 | if (unlikely(!vma)) { | ||
448 | bad_area(regs, error_code, address); | ||
449 | return; | ||
450 | } | ||
451 | if (likely(vma->vm_start <= address)) | ||
452 | goto good_area; | ||
453 | if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { | ||
454 | bad_area(regs, error_code, address); | ||
455 | return; | ||
456 | } | ||
457 | if (unlikely(expand_stack(vma, address))) { | ||
458 | bad_area(regs, error_code, address); | ||
459 | return; | ||
460 | } | ||
461 | |||
462 | /* | ||
463 | * Ok, we have a good vm_area for this memory access, so | ||
464 | * we can handle it.. | ||
465 | */ | ||
466 | good_area: | ||
467 | if (unlikely(access_error(error_code, vma))) { | ||
468 | bad_area_access_error(regs, error_code, address); | ||
469 | return; | ||
470 | } | ||
471 | |||
472 | set_thread_fault_code(error_code); | ||
473 | |||
474 | /* | ||
475 | * If for any reason at all we couldn't handle the fault, | ||
476 | * make sure we exit gracefully rather than endlessly redo | ||
477 | * the fault. | ||
478 | */ | ||
479 | fault = handle_mm_fault(mm, vma, address, flags); | ||
480 | |||
481 | if (unlikely(fault & (VM_FAULT_RETRY | VM_FAULT_ERROR))) | ||
482 | if (mm_fault_error(regs, error_code, address, fault)) | ||
483 | return; | ||
484 | |||
485 | if (flags & FAULT_FLAG_ALLOW_RETRY) { | ||
486 | if (fault & VM_FAULT_MAJOR) { | ||
487 | tsk->maj_flt++; | ||
488 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, | ||
489 | regs, address); | ||
490 | } else { | ||
491 | tsk->min_flt++; | ||
492 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, | ||
493 | regs, address); | ||
494 | } | ||
495 | if (fault & VM_FAULT_RETRY) { | ||
496 | flags &= ~FAULT_FLAG_ALLOW_RETRY; | ||
497 | |||
498 | /* | ||
499 | * No need to up_read(&mm->mmap_sem) as we would | ||
500 | * have already released it in __lock_page_or_retry | ||
501 | * in mm/filemap.c. | ||
502 | */ | ||
503 | goto retry; | ||
504 | } | ||
505 | } | ||
506 | |||
507 | up_read(&mm->mmap_sem); | ||
508 | } | ||