fault.c revision 94bce453c78996cc4373d5da6cfabe07fcc6d9f9
1/*
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;
50	siginfo_t info;
51	int fault;
52
53	info.si_code = SEGV_MAPERR;
54
55	/*
56	* We fault-in kernel-space virtual memory on-demand. The
57	* 'reference' page table is init_mm.pgd.
58	*
59	* NOTE! We MUST NOT take any locks for this case. We may
60	* be in an interrupt or a critical region, and should
61	* only copy the information from the master page table,
62	* nothing more.
63	*/
64	if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
65		goto vmalloc_fault;
66#ifdef MODULE_START
67	if (unlikely(address >= MODULE_START && address < MODULE_END))
68		goto vmalloc_fault;
69#endif
70
71	/*
72	* If we're in an interrupt or have no user
73	* context, we must not take the fault..
74	*/
75	if (in_atomic() || !mm)
76		goto bad_area_nosemaphore;
77
78	down_read(&mm->mmap_sem);
79	vma = find_vma(mm, address);
80	if (!vma)
81		goto bad_area;
82	if (vma->vm_start <= address)
83		goto good_area;
84	if (!(vma->vm_flags & VM_GROWSDOWN))
85		goto bad_area;
86	if (expand_stack(vma, address))
87		goto bad_area;
88	/*
89	* Ok, we have a good vm_area for this memory access, so
90	* we can handle it..
91	 */
92good_area:
93	info.si_code = SEGV_ACCERR;
94
95	if (write) {
96		if (!(vma->vm_flags & VM_WRITE))
97			goto bad_area;
98	} else {
99		if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
100			goto bad_area;
101	}
102
103	/*
104	* If for any reason at all we couldn't handle the fault,
105	* make sure we exit gracefully rather than endlessly redo
106	* the fault.
107	*/
108	fault = handle_mm_fault(mm, vma, address, write);
109	if (unlikely(fault & VM_FAULT_ERROR)) {
110		if (fault & VM_FAULT_OOM)
111			goto out_of_memory;
112		else if (fault & VM_FAULT_SIGBUS)
113			goto do_sigbus;
114		BUG();
115	}
116	if (fault & VM_FAULT_MAJOR)
117		tsk->maj_flt++;
118	else
119		tsk->min_flt++;
120
121	up_read(&mm->mmap_sem);
122	return;
123
124	/*
125	* Something tried to access memory that isn't in our memory map..
126	* Fix it, but check if it's kernel or user first..
127	 */
128bad_area:
129	up_read(&mm->mmap_sem);
130
131bad_area_nosemaphore:
132	/* User mode accesses just cause a SIGSEGV */
133	if (user_mode(regs)) {
134		tsk->thread.cp0_badvaddr = address;
135		tsk->thread.error_code = write;
136		info.si_signo = SIGSEGV;
137		info.si_errno = 0;
138		/* info.si_code has been set above */
139		info.si_addr = (void __user *) address;
140		force_sig_info(SIGSEGV, &info, tsk);
141		return;
142	}
143
144no_context:
145	/* Are we prepared to handle this kernel fault? */
146	if (fixup_exception(regs)) {
147		current->thread.cp0_baduaddr = address;
148		return;
149	}
150
151	/*
152	* Oops. The kernel tried to access some bad page. We'll have to
153	* terminate things with extreme prejudice.
154	*/
155	bust_spinlocks(1);
156
157	printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
158			"virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
159			0, field, address, field, regs->cp0_epc,
160			field, regs->regs[3]);
161	die("Oops", regs);
162
163	/*
164	* We ran out of memory, or some other thing happened to us that made
165	* us unable to handle the page fault gracefully.
166	*/
167out_of_memory:
168	up_read(&mm->mmap_sem);
169	if (!user_mode(regs))
170		goto no_context;
171	pagefault_out_of_memory();
172	return;
173
174do_sigbus:
175	up_read(&mm->mmap_sem);
176	/* Kernel mode? Handle exceptions or die */
177	if (!user_mode(regs))
178		goto no_context;
179	else
180	/*
181	* Send a sigbus, regardless of whether we were in kernel
182	* or user mode.
183	*/
184	tsk->thread.cp0_badvaddr = address;
185	info.si_signo = SIGBUS;
186	info.si_errno = 0;
187	info.si_code = BUS_ADRERR;
188	info.si_addr = (void __user *) address;
189	force_sig_info(SIGBUS, &info, tsk);
190	return;
191vmalloc_fault:
192	{
193		/*
194		* Synchronize this task's top level page-table
195		* with the 'reference' page table.
196		*
197		* Do _not_ use "tsk" here. We might be inside
198		* an interrupt in the middle of a task switch..
199		*/
200		int offset = __pgd_offset(address);
201		pgd_t *pgd, *pgd_k;
202		pud_t *pud, *pud_k;
203		pmd_t *pmd, *pmd_k;
204		pte_t *pte_k;
205
206		pgd = (pgd_t *) pgd_current + offset;
207		pgd_k = init_mm.pgd + offset;
208
209		if (!pgd_present(*pgd_k))
210			goto no_context;
211		set_pgd(pgd, *pgd_k);
212
213		pud = pud_offset(pgd, address);
214		pud_k = pud_offset(pgd_k, address);
215		if (!pud_present(*pud_k))
216			goto no_context;
217
218		pmd = pmd_offset(pud, address);
219		pmd_k = pmd_offset(pud_k, address);
220		if (!pmd_present(*pmd_k))
221			goto no_context;
222		set_pmd(pmd, *pmd_k);
223
224		pte_k = pte_offset_kernel(pmd_k, address);
225		if (!pte_present(*pte_k))
226			goto no_context;
227		return;
228	}
229}
230