Revert "sh: Ensure fixmap and store queue space can co-exist."
[deliverable/linux.git] / arch / sh / mm / fault.c
CommitLineData
26ff6c11
PM
1/*
2 * Page fault handler for SH with an MMU.
1da177e4 3 *
1da177e4 4 * Copyright (C) 1999 Niibe Yutaka
dbdb4e9f 5 * Copyright (C) 2003 - 2012 Paul Mundt
1da177e4
LT
6 *
7 * Based on linux/arch/i386/mm/fault.c:
8 * Copyright (C) 1995 Linus Torvalds
26ff6c11
PM
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.
1da177e4 13 */
1da177e4 14#include <linux/kernel.h>
1da177e4 15#include <linux/mm.h>
0f08f338
PM
16#include <linux/hardirq.h>
17#include <linux/kprobes.h>
cdd6c482 18#include <linux/perf_event.h>
dbdb4e9f 19#include <linux/kdebug.h>
e7cc9a73 20#include <asm/io_trapped.h>
1da177e4 21#include <asm/mmu_context.h>
db2e1fa3 22#include <asm/tlbflush.h>
e839ca52 23#include <asm/traps.h>
1da177e4 24
7433ab77
PM
25static inline int notify_page_fault(struct pt_regs *regs, int trap)
26{
27 int ret = 0;
28
c63c3105 29 if (kprobes_built_in() && !user_mode(regs)) {
7433ab77
PM
30 preempt_disable();
31 if (kprobe_running() && kprobe_fault_handler(regs, trap))
32 ret = 1;
33 preempt_enable();
34 }
7433ab77
PM
35
36 return ret;
37}
38
dbdb4e9f
PM
39static void
40force_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
45c0e0e2
SM
53/*
54 * This is useful to dump out the page tables associated with
55 * 'addr' in mm 'mm'.
56 */
57static 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,
28080329 69 (u32)(sizeof(*pgd) * 2), (u64)pgd_val(*pgd));
45c0e0e2
SM
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)
28080329 86 printk(", *pud=%0*Lx", (u32)(sizeof(*pud) * 2),
45c0e0e2
SM
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)
28080329 99 printk(", *pmd=%0*Lx", (u32)(sizeof(*pmd) * 2),
45c0e0e2
SM
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);
28080329
PM
115 printk(", *pte=%0*Lx", (u32)(sizeof(*pte) * 2),
116 (u64)pte_val(*pte));
45c0e0e2
SM
117 } while (0);
118
119 printk("\n");
120}
121
0f60bb25
PM
122static 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
5d9b4b19
MF
140 if (!pud_present(*pud))
141 set_pud(pud, *pud_k);
142
0f60bb25
PM
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);
05dd2cd3
MF
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 */
0f60bb25 156 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
05dd2cd3
MF
157 return NULL;
158 }
0f60bb25
PM
159
160 return pmd_k;
161}
162
163/*
164 * Handle a fault on the vmalloc or module mapping area
165 */
166static noinline int vmalloc_fault(unsigned long address)
167{
168 pgd_t *pgd_k;
169 pmd_t *pmd_k;
170 pte_t *pte_k;
171
c3e0af98
PM
172 /* Make sure we are in vmalloc/module/P3 area: */
173 if (!(address >= P3SEG && address < P3_ADDR_MAX))
0f60bb25
PM
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();
05dd2cd3 184 pmd_k = vmalloc_sync_one(pgd_k, address);
0f60bb25
PM
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
dbdb4e9f
PM
195static void
196show_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
214static noinline void
5a1dc78a 215no_context(struct pt_regs *regs, unsigned long error_code,
dbdb4e9f
PM
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
5a1dc78a 233 die("Oops", regs, error_code);
dbdb4e9f
PM
234 bust_spinlocks(0);
235 do_exit(SIGKILL);
236}
237
238static void
5a1dc78a 239__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
dbdb4e9f
PM
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
5a1dc78a 256 no_context(regs, error_code, address);
dbdb4e9f
PM
257}
258
259static noinline void
5a1dc78a 260bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
dbdb4e9f
PM
261 unsigned long address)
262{
5a1dc78a 263 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
dbdb4e9f
PM
264}
265
266static void
5a1dc78a 267__bad_area(struct pt_regs *regs, unsigned long error_code,
dbdb4e9f
PM
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
5a1dc78a 278 __bad_area_nosemaphore(regs, error_code, address, si_code);
dbdb4e9f
PM
279}
280
281static noinline void
5a1dc78a 282bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
dbdb4e9f 283{
5a1dc78a 284 __bad_area(regs, error_code, address, SEGV_MAPERR);
dbdb4e9f
PM
285}
286
287static noinline void
5a1dc78a 288bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
dbdb4e9f
PM
289 unsigned long address)
290{
5a1dc78a 291 __bad_area(regs, error_code, address, SEGV_ACCERR);
dbdb4e9f
PM
292}
293
294static 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(&current->mm->mmap_sem);
301
302 pagefault_out_of_memory();
303}
304
305static void
5a1dc78a 306do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
dbdb4e9f
PM
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))
5a1dc78a 315 no_context(regs, error_code, address);
dbdb4e9f
PM
316
317 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
318}
319
320static noinline int
5a1dc78a 321mm_fault_error(struct pt_regs *regs, unsigned long error_code,
dbdb4e9f
PM
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(&current->mm->mmap_sem);
331 if (!user_mode(regs))
5a1dc78a 332 no_context(regs, error_code, address);
dbdb4e9f
PM
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(&current->mm->mmap_sem);
5a1dc78a 343 no_context(regs, error_code, address);
dbdb4e9f
PM
344 return 1;
345 }
346
347 out_of_memory();
348 } else {
349 if (fault & VM_FAULT_SIGBUS)
5a1dc78a 350 do_sigbus(regs, error_code, address);
dbdb4e9f
PM
351 else
352 BUG();
353 }
354
355 return 1;
356}
357
28080329 358static inline int access_error(int error_code, struct vm_area_struct *vma)
dbdb4e9f 359{
28080329 360 if (error_code & FAULT_CODE_WRITE) {
dbdb4e9f
PM
361 /* write, present and write, not present: */
362 if (unlikely(!(vma->vm_flags & VM_WRITE)))
363 return 1;
364 return 0;
365 }
366
28080329
PM
367 /* ITLB miss on NX page */
368 if (unlikely((error_code & FAULT_CODE_ITLB) &&
369 !(vma->vm_flags & VM_EXEC)))
370 return 1;
371
dbdb4e9f
PM
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
0f60bb25
PM
379static int fault_in_kernel_space(unsigned long address)
380{
381 return address >= TASK_SIZE;
382}
383
1da177e4
LT
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 */
b5a1bcbe 389asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
5a1dc78a 390 unsigned long error_code,
b5a1bcbe 391 unsigned long address)
1da177e4 392{
0f60bb25 393 unsigned long vec;
1da177e4
LT
394 struct task_struct *tsk;
395 struct mm_struct *mm;
396 struct vm_area_struct * vma;
83c54070 397 int fault;
5a1dc78a 398 int write = error_code & FAULT_CODE_WRITE;
11fd9824 399 unsigned int flags = (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
5a1dc78a 400 (write ? FAULT_FLAG_WRITE : 0));
1da177e4 401
1da177e4 402 tsk = current;
0f60bb25 403 mm = tsk->mm;
0f60bb25 404 vec = lookup_exception_vector();
1da177e4 405
0f60bb25
PM
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)
99a596f9 417 return;
0f60bb25 418 if (notify_page_fault(regs, vec))
96e14e54 419 return;
99a596f9 420
5a1dc78a 421 bad_area_nosemaphore(regs, error_code, address);
dbdb4e9f 422 return;
99a596f9
SM
423 }
424
0f60bb25 425 if (unlikely(notify_page_fault(regs, vec)))
7433ab77
PM
426 return;
427
f2fb4e4f 428 /* Only enable interrupts if they were on before the fault */
7433ab77 429 if ((regs->sr & SR_IMASK) != SR_IMASK)
f2fb4e4f 430 local_irq_enable();
f2fb4e4f 431
a8b0ca17 432 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
f2fb4e4f 433
1da177e4 434 /*
0f60bb25
PM
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:
1da177e4 437 */
dbdb4e9f 438 if (unlikely(in_atomic() || !mm)) {
5a1dc78a 439 bad_area_nosemaphore(regs, error_code, address);
dbdb4e9f
PM
440 return;
441 }
1da177e4 442
11fd9824 443retry:
1da177e4
LT
444 down_read(&mm->mmap_sem);
445
446 vma = find_vma(mm, address);
dbdb4e9f 447 if (unlikely(!vma)) {
5a1dc78a 448 bad_area(regs, error_code, address);
dbdb4e9f
PM
449 return;
450 }
451 if (likely(vma->vm_start <= address))
1da177e4 452 goto good_area;
dbdb4e9f 453 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
5a1dc78a 454 bad_area(regs, error_code, address);
dbdb4e9f
PM
455 return;
456 }
457 if (unlikely(expand_stack(vma, address))) {
5a1dc78a 458 bad_area(regs, error_code, address);
dbdb4e9f
PM
459 return;
460 }
0f60bb25
PM
461
462 /*
463 * Ok, we have a good vm_area for this memory access, so
464 * we can handle it..
465 */
1da177e4 466good_area:
5a1dc78a
PM
467 if (unlikely(access_error(error_code, vma))) {
468 bad_area_access_error(regs, error_code, address);
dbdb4e9f 469 return;
1da177e4
LT
470 }
471
5a1dc78a
PM
472 set_thread_fault_code(error_code);
473
1da177e4
LT
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 */
11fd9824
KC
479 fault = handle_mm_fault(mm, vma, address, flags);
480
dbdb4e9f 481 if (unlikely(fault & (VM_FAULT_RETRY | VM_FAULT_ERROR)))
5a1dc78a 482 if (mm_fault_error(regs, error_code, address, fault))
dbdb4e9f 483 return;
11fd9824
KC
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 }
7433ab77 505 }
1da177e4
LT
506
507 up_read(&mm->mmap_sem);
1da177e4 508}
This page took 0.633093 seconds and 5 git commands to generate.