blob: 0214a43b9911b00e552d99c3396b07f9f12f6481 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1995 - 2000 by Ralf Baechle
7 */
8#include <linux/signal.h>
9#include <linux/sched.h>
10#include <linux/interrupt.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/string.h>
14#include <linux/types.h>
15#include <linux/ptrace.h>
16#include <linux/mman.h>
17#include <linux/mm.h>
18#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
David Daneyc1bf2072010-08-03 11:22:20 -070020#include <linux/kprobes.h>
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +080021#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/branch.h>
24#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
26#include <asm/ptrace.h>
Thiemo Seufer16033d62005-02-19 13:56:04 +000027#include <asm/highmem.h> /* For VMALLOC_END */
David Daneyc1bf2072010-08-03 11:22:20 -070028#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/*
31 * This routine handles page faults. It determines the address,
32 * and the problem, and then passes it off to one of the appropriate
33 * routines.
34 */
David Daneyc1bf2072010-08-03 11:22:20 -070035asmlinkage void __kprobes do_page_fault(struct pt_regs *regs, unsigned long write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 unsigned long address)
37{
38 struct vm_area_struct * vma = NULL;
39 struct task_struct *tsk = current;
40 struct mm_struct *mm = tsk->mm;
41 const int field = sizeof(unsigned long) * 2;
42 siginfo_t info;
Nick Piggin83c54072007-07-19 01:47:05 -070043 int fault;
Johannes Weinere2ec2c22013-09-12 15:13:39 -070044 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#if 0
Ralf Baechled6f70362007-03-29 22:30:01 +010047 printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 current->comm, current->pid, field, address, write,
49 field, regs->cp0_epc);
50#endif
51
David Daneyc1bf2072010-08-03 11:22:20 -070052#ifdef CONFIG_KPROBES
53 /*
Ralf Baechle70342282013-01-22 12:59:30 +010054 * This is to notify the fault handler of the kprobes. The
David Daneyc1bf2072010-08-03 11:22:20 -070055 * exception code is redundant as it is also carried in REGS,
56 * but we pass it anyhow.
57 */
58 if (notify_die(DIE_PAGE_FAULT, "page fault", regs, -1,
59 (regs->cp0_cause >> 2) & 0x1f, SIGSEGV) == NOTIFY_STOP)
60 return;
61#endif
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 info.si_code = SEGV_MAPERR;
64
65 /*
66 * We fault-in kernel-space virtual memory on-demand. The
67 * 'reference' page table is init_mm.pgd.
68 *
69 * NOTE! We MUST NOT take any locks for this case. We may
70 * be in an interrupt or a critical region, and should
71 * only copy the information from the master page table,
72 * nothing more.
73 */
David Daney2ca2ebf2009-09-02 15:47:34 -070074#ifdef CONFIG_64BIT
75# define VMALLOC_FAULT_TARGET no_context
76#else
77# define VMALLOC_FAULT_TARGET vmalloc_fault
78#endif
79
Thiemo Seufer16033d62005-02-19 13:56:04 +000080 if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
David Daney2ca2ebf2009-09-02 15:47:34 -070081 goto VMALLOC_FAULT_TARGET;
Atsushi Nemoto656be922006-10-26 00:08:31 +090082#ifdef MODULE_START
83 if (unlikely(address >= MODULE_START && address < MODULE_END))
David Daney2ca2ebf2009-09-02 15:47:34 -070084 goto VMALLOC_FAULT_TARGET;
Atsushi Nemoto656be922006-10-26 00:08:31 +090085#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /*
88 * If we're in an interrupt or have no user
89 * context, we must not take the fault..
90 */
91 if (in_atomic() || !mm)
92 goto bad_area_nosemaphore;
93
Johannes Weinere2ec2c22013-09-12 15:13:39 -070094 if (user_mode(regs))
95 flags |= FAULT_FLAG_USER;
Kautuk Consul43ca4952011-12-23 16:52:42 +053096retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 down_read(&mm->mmap_sem);
98 vma = find_vma(mm, address);
99 if (!vma)
100 goto bad_area;
101 if (vma->vm_start <= address)
102 goto good_area;
103 if (!(vma->vm_flags & VM_GROWSDOWN))
104 goto bad_area;
105 if (expand_stack(vma, address))
106 goto bad_area;
107/*
108 * Ok, we have a good vm_area for this memory access, so
109 * we can handle it..
110 */
111good_area:
112 info.si_code = SEGV_ACCERR;
113
114 if (write) {
115 if (!(vma->vm_flags & VM_WRITE))
116 goto bad_area;
Johannes Weinere2ec2c22013-09-12 15:13:39 -0700117 flags |= FAULT_FLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 } else {
Steven J. Hill05857c62012-09-13 16:51:46 -0500119 if (cpu_has_rixi) {
David Daney6dd93442010-02-10 15:12:47 -0800120 if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
121#if 0
122 pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
123 raw_smp_processor_id(),
124 current->comm, current->pid,
125 field, address, write,
126 field, regs->cp0_epc);
127#endif
128 goto bad_area;
129 }
130 if (!(vma->vm_flags & VM_READ)) {
131#if 0
132 pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
133 raw_smp_processor_id(),
134 current->comm, current->pid,
135 field, address, write,
136 field, regs->cp0_epc);
137#endif
138 goto bad_area;
139 }
140 } else {
141 if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
142 goto bad_area;
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /*
147 * If for any reason at all we couldn't handle the fault,
148 * make sure we exit gracefully rather than endlessly redo
149 * the fault.
150 */
Kautuk Consul43ca4952011-12-23 16:52:42 +0530151 fault = handle_mm_fault(mm, vma, address, flags);
152
153 if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
154 return;
155
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200156 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
Nick Piggin83c54072007-07-19 01:47:05 -0700157 if (unlikely(fault & VM_FAULT_ERROR)) {
158 if (fault & VM_FAULT_OOM)
159 goto out_of_memory;
160 else if (fault & VM_FAULT_SIGBUS)
161 goto do_sigbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 BUG();
163 }
Kautuk Consul43ca4952011-12-23 16:52:42 +0530164 if (flags & FAULT_FLAG_ALLOW_RETRY) {
165 if (fault & VM_FAULT_MAJOR) {
166 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
167 regs, address);
168 tsk->maj_flt++;
169 } else {
170 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
171 regs, address);
172 tsk->min_flt++;
173 }
174 if (fault & VM_FAULT_RETRY) {
175 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Shaohua Li45cac652012-10-08 16:32:19 -0700176 flags |= FAULT_FLAG_TRIED;
Kautuk Consul43ca4952011-12-23 16:52:42 +0530177
178 /*
179 * No need to up_read(&mm->mmap_sem) as we would
180 * have already released it in __lock_page_or_retry
181 * in mm/filemap.c.
182 */
183
184 goto retry;
185 }
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +0800186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 up_read(&mm->mmap_sem);
189 return;
190
191/*
192 * Something tried to access memory that isn't in our memory map..
193 * Fix it, but check if it's kernel or user first..
194 */
195bad_area:
196 up_read(&mm->mmap_sem);
197
198bad_area_nosemaphore:
199 /* User mode accesses just cause a SIGSEGV */
200 if (user_mode(regs)) {
201 tsk->thread.cp0_badvaddr = address;
202 tsk->thread.error_code = write;
203#if 0
204 printk("do_page_fault() #2: sending SIGSEGV to %s for "
205 "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
206 tsk->comm,
207 write ? "write access to" : "read access from",
208 field, address,
209 field, (unsigned long) regs->cp0_epc,
210 field, (unsigned long) regs->regs[31]);
211#endif
212 info.si_signo = SIGSEGV;
213 info.si_errno = 0;
214 /* info.si_code has been set above */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000215 info.si_addr = (void __user *) address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 force_sig_info(SIGSEGV, &info, tsk);
217 return;
218 }
219
220no_context:
Ralf Baechle70342282013-01-22 12:59:30 +0100221 /* Are we prepared to handle this kernel fault? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (fixup_exception(regs)) {
223 current->thread.cp0_baduaddr = address;
224 return;
225 }
226
227 /*
228 * Oops. The kernel tried to access some bad page. We'll have to
229 * terminate things with extreme prejudice.
230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 bust_spinlocks(1);
232
233 printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
234 "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
Ralf Baechled6f70362007-03-29 22:30:01 +0100235 raw_smp_processor_id(), field, address, field, regs->cp0_epc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 field, regs->regs[31]);
237 die("Oops", regs);
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239out_of_memory:
Ralf Baechlec7c1e382009-01-12 00:09:13 +0000240 /*
241 * We ran out of memory, call the OOM killer, and return the userspace
242 * (which will retry the fault, or kill us if we got oom-killed).
243 */
Akinobu Mitaa887b4d2009-07-04 01:33:09 +0900244 up_read(&mm->mmap_sem);
Johannes Weiner086c6cc2013-09-12 15:13:38 -0700245 if (!user_mode(regs))
246 goto no_context;
Ralf Baechlec7c1e382009-01-12 00:09:13 +0000247 pagefault_out_of_memory();
248 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250do_sigbus:
251 up_read(&mm->mmap_sem);
252
253 /* Kernel mode? Handle exceptions or die */
254 if (!user_mode(regs))
255 goto no_context;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100256 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /*
258 * Send a sigbus, regardless of whether we were in kernel
259 * or user mode.
260 */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100261#if 0
262 printk("do_page_fault() #3: sending SIGBUS to %s for "
263 "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
264 tsk->comm,
265 write ? "write access to" : "read access from",
266 field, address,
267 field, (unsigned long) regs->cp0_epc,
268 field, (unsigned long) regs->regs[31]);
269#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 tsk->thread.cp0_badvaddr = address;
271 info.si_signo = SIGBUS;
272 info.si_errno = 0;
273 info.si_code = BUS_ADRERR;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000274 info.si_addr = (void __user *) address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 force_sig_info(SIGBUS, &info, tsk);
276
277 return;
David Daney2ca2ebf2009-09-02 15:47:34 -0700278#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279vmalloc_fault:
280 {
281 /*
282 * Synchronize this task's top level page-table
283 * with the 'reference' page table.
284 *
285 * Do _not_ use "tsk" here. We might be inside
286 * an interrupt in the middle of a task switch..
287 */
288 int offset = __pgd_offset(address);
289 pgd_t *pgd, *pgd_k;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000290 pud_t *pud, *pud_k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 pmd_t *pmd, *pmd_k;
292 pte_t *pte_k;
293
Ralf Baechled6f70362007-03-29 22:30:01 +0100294 pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 pgd_k = init_mm.pgd + offset;
296
297 if (!pgd_present(*pgd_k))
298 goto no_context;
299 set_pgd(pgd, *pgd_k);
300
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000301 pud = pud_offset(pgd, address);
302 pud_k = pud_offset(pgd_k, address);
303 if (!pud_present(*pud_k))
304 goto no_context;
305
306 pmd = pmd_offset(pud, address);
307 pmd_k = pmd_offset(pud_k, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!pmd_present(*pmd_k))
309 goto no_context;
310 set_pmd(pmd, *pmd_k);
311
312 pte_k = pte_offset_kernel(pmd_k, address);
313 if (!pte_present(*pte_k))
314 goto no_context;
315 return;
316 }
David Daney2ca2ebf2009-09-02 15:47:34 -0700317#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}