blob: b4aac424b2e99fa515a2d130ec81d04d567a5f83 [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>
20
21#include <asm/branch.h>
22#include <asm/mmu_context.h>
23#include <asm/system.h>
24#include <asm/uaccess.h>
25#include <asm/ptrace.h>
Thiemo Seufer16033d62005-02-19 13:56:04 +000026#include <asm/highmem.h> /* For VMALLOC_END */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * This routine handles page faults. It determines the address,
30 * and the problem, and then passes it off to one of the appropriate
31 * routines.
32 */
33asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
34 unsigned long address)
35{
36 struct vm_area_struct * vma = NULL;
37 struct task_struct *tsk = current;
38 struct mm_struct *mm = tsk->mm;
39 const int field = sizeof(unsigned long) * 2;
40 siginfo_t info;
Nick Piggin83c54072007-07-19 01:47:05 -070041 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#if 0
Ralf Baechled6f70362007-03-29 22:30:01 +010044 printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 current->comm, current->pid, field, address, write,
46 field, regs->cp0_epc);
47#endif
48
49 info.si_code = SEGV_MAPERR;
50
51 /*
52 * We fault-in kernel-space virtual memory on-demand. The
53 * 'reference' page table is init_mm.pgd.
54 *
55 * NOTE! We MUST NOT take any locks for this case. We may
56 * be in an interrupt or a critical region, and should
57 * only copy the information from the master page table,
58 * nothing more.
59 */
David Daney2ca2ebf2009-09-02 15:47:34 -070060#ifdef CONFIG_64BIT
61# define VMALLOC_FAULT_TARGET no_context
62#else
63# define VMALLOC_FAULT_TARGET vmalloc_fault
64#endif
65
Thiemo Seufer16033d62005-02-19 13:56:04 +000066 if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
David Daney2ca2ebf2009-09-02 15:47:34 -070067 goto VMALLOC_FAULT_TARGET;
Atsushi Nemoto656be922006-10-26 00:08:31 +090068#ifdef MODULE_START
69 if (unlikely(address >= MODULE_START && address < MODULE_END))
David Daney2ca2ebf2009-09-02 15:47:34 -070070 goto VMALLOC_FAULT_TARGET;
Atsushi Nemoto656be922006-10-26 00:08:31 +090071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /*
74 * If we're in an interrupt or have no user
75 * context, we must not take the fault..
76 */
77 if (in_atomic() || !mm)
78 goto bad_area_nosemaphore;
79
80 down_read(&mm->mmap_sem);
81 vma = find_vma(mm, address);
82 if (!vma)
83 goto bad_area;
84 if (vma->vm_start <= address)
85 goto good_area;
86 if (!(vma->vm_flags & VM_GROWSDOWN))
87 goto bad_area;
88 if (expand_stack(vma, address))
89 goto bad_area;
90/*
91 * Ok, we have a good vm_area for this memory access, so
92 * we can handle it..
93 */
94good_area:
95 info.si_code = SEGV_ACCERR;
96
97 if (write) {
98 if (!(vma->vm_flags & VM_WRITE))
99 goto bad_area;
100 } else {
David Daney6dd93442010-02-10 15:12:47 -0800101 if (kernel_uses_smartmips_rixi) {
102 if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
103#if 0
104 pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
105 raw_smp_processor_id(),
106 current->comm, current->pid,
107 field, address, write,
108 field, regs->cp0_epc);
109#endif
110 goto bad_area;
111 }
112 if (!(vma->vm_flags & VM_READ)) {
113#if 0
114 pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
115 raw_smp_processor_id(),
116 current->comm, current->pid,
117 field, address, write,
118 field, regs->cp0_epc);
119#endif
120 goto bad_area;
121 }
122 } else {
123 if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
124 goto bad_area;
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /*
129 * If for any reason at all we couldn't handle the fault,
130 * make sure we exit gracefully rather than endlessly redo
131 * the fault.
132 */
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700133 fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -0700134 if (unlikely(fault & VM_FAULT_ERROR)) {
135 if (fault & VM_FAULT_OOM)
136 goto out_of_memory;
137 else if (fault & VM_FAULT_SIGBUS)
138 goto do_sigbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 BUG();
140 }
Nick Piggin83c54072007-07-19 01:47:05 -0700141 if (fault & VM_FAULT_MAJOR)
142 tsk->maj_flt++;
143 else
144 tsk->min_flt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 up_read(&mm->mmap_sem);
147 return;
148
149/*
150 * Something tried to access memory that isn't in our memory map..
151 * Fix it, but check if it's kernel or user first..
152 */
153bad_area:
154 up_read(&mm->mmap_sem);
155
156bad_area_nosemaphore:
157 /* User mode accesses just cause a SIGSEGV */
158 if (user_mode(regs)) {
159 tsk->thread.cp0_badvaddr = address;
160 tsk->thread.error_code = write;
161#if 0
162 printk("do_page_fault() #2: sending SIGSEGV to %s for "
163 "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
164 tsk->comm,
165 write ? "write access to" : "read access from",
166 field, address,
167 field, (unsigned long) regs->cp0_epc,
168 field, (unsigned long) regs->regs[31]);
169#endif
170 info.si_signo = SIGSEGV;
171 info.si_errno = 0;
172 /* info.si_code has been set above */
Ralf Baechlefe00f942005-03-01 19:22:29 +0000173 info.si_addr = (void __user *) address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 force_sig_info(SIGSEGV, &info, tsk);
175 return;
176 }
177
178no_context:
179 /* Are we prepared to handle this kernel fault? */
180 if (fixup_exception(regs)) {
181 current->thread.cp0_baduaddr = address;
182 return;
183 }
184
185 /*
186 * Oops. The kernel tried to access some bad page. We'll have to
187 * terminate things with extreme prejudice.
188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 bust_spinlocks(1);
190
191 printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
192 "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
Ralf Baechled6f70362007-03-29 22:30:01 +0100193 raw_smp_processor_id(), field, address, field, regs->cp0_epc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 field, regs->regs[31]);
195 die("Oops", regs);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197out_of_memory:
Ralf Baechlec7c1e382009-01-12 00:09:13 +0000198 /*
199 * We ran out of memory, call the OOM killer, and return the userspace
200 * (which will retry the fault, or kill us if we got oom-killed).
201 */
Akinobu Mitaa887b4d2009-07-04 01:33:09 +0900202 up_read(&mm->mmap_sem);
Ralf Baechlec7c1e382009-01-12 00:09:13 +0000203 pagefault_out_of_memory();
204 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206do_sigbus:
207 up_read(&mm->mmap_sem);
208
209 /* Kernel mode? Handle exceptions or die */
210 if (!user_mode(regs))
211 goto no_context;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100212 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /*
214 * Send a sigbus, regardless of whether we were in kernel
215 * or user mode.
216 */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100217#if 0
218 printk("do_page_fault() #3: sending SIGBUS to %s for "
219 "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
220 tsk->comm,
221 write ? "write access to" : "read access from",
222 field, address,
223 field, (unsigned long) regs->cp0_epc,
224 field, (unsigned long) regs->regs[31]);
225#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 tsk->thread.cp0_badvaddr = address;
227 info.si_signo = SIGBUS;
228 info.si_errno = 0;
229 info.si_code = BUS_ADRERR;
Ralf Baechlefe00f942005-03-01 19:22:29 +0000230 info.si_addr = (void __user *) address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 force_sig_info(SIGBUS, &info, tsk);
232
233 return;
David Daney2ca2ebf2009-09-02 15:47:34 -0700234#ifndef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235vmalloc_fault:
236 {
237 /*
238 * Synchronize this task's top level page-table
239 * with the 'reference' page table.
240 *
241 * Do _not_ use "tsk" here. We might be inside
242 * an interrupt in the middle of a task switch..
243 */
244 int offset = __pgd_offset(address);
245 pgd_t *pgd, *pgd_k;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000246 pud_t *pud, *pud_k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 pmd_t *pmd, *pmd_k;
248 pte_t *pte_k;
249
Ralf Baechled6f70362007-03-29 22:30:01 +0100250 pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 pgd_k = init_mm.pgd + offset;
252
253 if (!pgd_present(*pgd_k))
254 goto no_context;
255 set_pgd(pgd, *pgd_k);
256
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000257 pud = pud_offset(pgd, address);
258 pud_k = pud_offset(pgd_k, address);
259 if (!pud_present(*pud_k))
260 goto no_context;
261
262 pmd = pmd_offset(pud, address);
263 pmd_k = pmd_offset(pud_k, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!pmd_present(*pmd_k))
265 goto no_context;
266 set_pmd(pmd, *pmd_k);
267
268 pte_k = pte_offset_kernel(pmd_k, address);
269 if (!pte_present(*pte_k))
270 goto no_context;
271 return;
272 }
David Daney2ca2ebf2009-09-02 15:47:34 -0700273#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}