blob: 52238983527d605914853fd5415ea39617944ffe [file] [log] [blame]
Chen Liqin6bc9a392009-06-12 22:01:00 +08001/*
2 * arch/score/mm/fault.c
3 *
4 * Score Processor version.
5 *
6 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
7 * Lennox Wu <lennox.wu@sunplusct.com>
8 * Chen Liqin <liqin.chen@sunplusct.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see the file COPYING, or write
22 * to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <linux/errno.h>
27#include <linux/interrupt.h>
28#include <linux/kernel.h>
29#include <linux/mm.h>
30#include <linux/mman.h>
31#include <linux/module.h>
32#include <linux/signal.h>
33#include <linux/sched.h>
34#include <linux/string.h>
35#include <linux/types.h>
36#include <linux/ptrace.h>
37
38/*
39 * This routine handles page faults. It determines the address,
40 * and the problem, and then passes it off to one of the appropriate
41 * routines.
42 */
43asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
44 unsigned long address)
45{
46 struct vm_area_struct *vma = NULL;
47 struct task_struct *tsk = current;
48 struct mm_struct *mm = tsk->mm;
49 const int field = sizeof(unsigned long) * 2;
Johannes Weiner759496b2013-09-12 15:13:39 -070050 unsigned long flags = 0;
Chen Liqin6bc9a392009-06-12 22:01:00 +080051 siginfo_t info;
52 int fault;
53
54 info.si_code = SEGV_MAPERR;
55
56 /*
57 * We fault-in kernel-space virtual memory on-demand. The
58 * 'reference' page table is init_mm.pgd.
59 *
60 * NOTE! We MUST NOT take any locks for this case. We may
61 * be in an interrupt or a critical region, and should
62 * only copy the information from the master page table,
63 * nothing more.
64 */
65 if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
66 goto vmalloc_fault;
67#ifdef MODULE_START
68 if (unlikely(address >= MODULE_START && address < MODULE_END))
69 goto vmalloc_fault;
70#endif
71
72 /*
73 * If we're in an interrupt or have no user
74 * context, we must not take the fault..
75 */
76 if (in_atomic() || !mm)
77 goto bad_area_nosemaphore;
78
Johannes Weiner759496b2013-09-12 15:13:39 -070079 if (user_mode(regs))
80 flags |= FAULT_FLAG_USER;
81
Chen Liqin6bc9a392009-06-12 22:01:00 +080082 down_read(&mm->mmap_sem);
83 vma = find_vma(mm, address);
84 if (!vma)
85 goto bad_area;
86 if (vma->vm_start <= address)
87 goto good_area;
88 if (!(vma->vm_flags & VM_GROWSDOWN))
89 goto bad_area;
90 if (expand_stack(vma, address))
91 goto bad_area;
92 /*
93 * Ok, we have a good vm_area for this memory access, so
94 * we can handle it..
95 */
96good_area:
97 info.si_code = SEGV_ACCERR;
98
99 if (write) {
100 if (!(vma->vm_flags & VM_WRITE))
101 goto bad_area;
Johannes Weiner759496b2013-09-12 15:13:39 -0700102 flags |= FAULT_FLAG_WRITE;
Chen Liqin6bc9a392009-06-12 22:01:00 +0800103 } else {
104 if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
105 goto bad_area;
106 }
107
Chen Liqin6bc9a392009-06-12 22:01:00 +0800108 /*
109 * If for any reason at all we couldn't handle the fault,
110 * make sure we exit gracefully rather than endlessly redo
111 * the fault.
112 */
Johannes Weiner759496b2013-09-12 15:13:39 -0700113 fault = handle_mm_fault(mm, vma, address, flags);
Chen Liqin6bc9a392009-06-12 22:01:00 +0800114 if (unlikely(fault & VM_FAULT_ERROR)) {
115 if (fault & VM_FAULT_OOM)
116 goto out_of_memory;
117 else if (fault & VM_FAULT_SIGBUS)
118 goto do_sigbus;
119 BUG();
120 }
121 if (fault & VM_FAULT_MAJOR)
122 tsk->maj_flt++;
123 else
124 tsk->min_flt++;
125
126 up_read(&mm->mmap_sem);
127 return;
128
129 /*
130 * Something tried to access memory that isn't in our memory map..
131 * Fix it, but check if it's kernel or user first..
132 */
133bad_area:
134 up_read(&mm->mmap_sem);
135
136bad_area_nosemaphore:
137 /* User mode accesses just cause a SIGSEGV */
138 if (user_mode(regs)) {
139 tsk->thread.cp0_badvaddr = address;
140 tsk->thread.error_code = write;
141 info.si_signo = SIGSEGV;
142 info.si_errno = 0;
143 /* info.si_code has been set above */
144 info.si_addr = (void __user *) address;
145 force_sig_info(SIGSEGV, &info, tsk);
146 return;
147 }
148
149no_context:
150 /* Are we prepared to handle this kernel fault? */
151 if (fixup_exception(regs)) {
152 current->thread.cp0_baduaddr = address;
153 return;
154 }
155
156 /*
157 * Oops. The kernel tried to access some bad page. We'll have to
158 * terminate things with extreme prejudice.
159 */
160 bust_spinlocks(1);
161
162 printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
163 "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
164 0, field, address, field, regs->cp0_epc,
165 field, regs->regs[3]);
166 die("Oops", regs);
167
168 /*
169 * We ran out of memory, or some other thing happened to us that made
170 * us unable to handle the page fault gracefully.
171 */
172out_of_memory:
173 up_read(&mm->mmap_sem);
Johannes Weiner609838c2013-07-08 15:59:50 -0700174 if (!user_mode(regs))
175 goto no_context;
176 pagefault_out_of_memory();
177 return;
Chen Liqin6bc9a392009-06-12 22:01:00 +0800178
179do_sigbus:
180 up_read(&mm->mmap_sem);
181 /* Kernel mode? Handle exceptions or die */
182 if (!user_mode(regs))
183 goto no_context;
184 else
185 /*
186 * Send a sigbus, regardless of whether we were in kernel
187 * or user mode.
188 */
189 tsk->thread.cp0_badvaddr = address;
190 info.si_signo = SIGBUS;
191 info.si_errno = 0;
192 info.si_code = BUS_ADRERR;
193 info.si_addr = (void __user *) address;
194 force_sig_info(SIGBUS, &info, tsk);
195 return;
196vmalloc_fault:
197 {
198 /*
199 * Synchronize this task's top level page-table
200 * with the 'reference' page table.
201 *
202 * Do _not_ use "tsk" here. We might be inside
203 * an interrupt in the middle of a task switch..
204 */
205 int offset = __pgd_offset(address);
206 pgd_t *pgd, *pgd_k;
207 pud_t *pud, *pud_k;
208 pmd_t *pmd, *pmd_k;
209 pte_t *pte_k;
210
211 pgd = (pgd_t *) pgd_current + offset;
212 pgd_k = init_mm.pgd + offset;
213
214 if (!pgd_present(*pgd_k))
215 goto no_context;
216 set_pgd(pgd, *pgd_k);
217
218 pud = pud_offset(pgd, address);
219 pud_k = pud_offset(pgd_k, address);
220 if (!pud_present(*pud_k))
221 goto no_context;
222
223 pmd = pmd_offset(pud, address);
224 pmd_k = pmd_offset(pud_k, address);
225 if (!pmd_present(*pmd_k))
226 goto no_context;
227 set_pmd(pmd, *pmd_k);
228
229 pte_k = pte_offset_kernel(pmd_k, address);
230 if (!pte_present(*pte_k))
231 goto no_context;
232 return;
233 }
234}