xref: /OK3568_Linux_fs/kernel/arch/nios2/mm/fault.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2009 Wind River Systems Inc
3*4882a593Smuzhiyun  *   Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * based on arch/mips/mm/fault.c which is:
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 1995-2000 Ralf Baechle
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This file is subject to the terms and conditions of the GNU General Public
10*4882a593Smuzhiyun  * License.  See the file "COPYING" in the main directory of this archive
11*4882a593Smuzhiyun  * for more details.
12*4882a593Smuzhiyun  */
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/signal.h>
15*4882a593Smuzhiyun #include <linux/sched.h>
16*4882a593Smuzhiyun #include <linux/sched/debug.h>
17*4882a593Smuzhiyun #include <linux/interrupt.h>
18*4882a593Smuzhiyun #include <linux/kernel.h>
19*4882a593Smuzhiyun #include <linux/errno.h>
20*4882a593Smuzhiyun #include <linux/string.h>
21*4882a593Smuzhiyun #include <linux/types.h>
22*4882a593Smuzhiyun #include <linux/ptrace.h>
23*4882a593Smuzhiyun #include <linux/mman.h>
24*4882a593Smuzhiyun #include <linux/mm.h>
25*4882a593Smuzhiyun #include <linux/extable.h>
26*4882a593Smuzhiyun #include <linux/uaccess.h>
27*4882a593Smuzhiyun #include <linux/perf_event.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include <asm/mmu_context.h>
30*4882a593Smuzhiyun #include <asm/traps.h>
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define EXC_SUPERV_INSN_ACCESS	9  /* Supervisor only instruction address */
33*4882a593Smuzhiyun #define EXC_SUPERV_DATA_ACCESS	11 /* Supervisor only data address */
34*4882a593Smuzhiyun #define EXC_X_PROTECTION_FAULT	13 /* TLB permission violation (x) */
35*4882a593Smuzhiyun #define EXC_R_PROTECTION_FAULT	14 /* TLB permission violation (r) */
36*4882a593Smuzhiyun #define EXC_W_PROTECTION_FAULT	15 /* TLB permission violation (w) */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun  * This routine handles page faults.  It determines the address,
40*4882a593Smuzhiyun  * and the problem, and then passes it off to one of the appropriate
41*4882a593Smuzhiyun  * routines.
42*4882a593Smuzhiyun  */
do_page_fault(struct pt_regs * regs,unsigned long cause,unsigned long address)43*4882a593Smuzhiyun asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,
44*4882a593Smuzhiyun 				unsigned long address)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	struct vm_area_struct *vma = NULL;
47*4882a593Smuzhiyun 	struct task_struct *tsk = current;
48*4882a593Smuzhiyun 	struct mm_struct *mm = tsk->mm;
49*4882a593Smuzhiyun 	int code = SEGV_MAPERR;
50*4882a593Smuzhiyun 	vm_fault_t fault;
51*4882a593Smuzhiyun 	unsigned int flags = FAULT_FLAG_DEFAULT;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	cause >>= 2;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	/* Restart the instruction */
56*4882a593Smuzhiyun 	regs->ea -= 4;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	/*
59*4882a593Smuzhiyun 	 * We fault-in kernel-space virtual memory on-demand. The
60*4882a593Smuzhiyun 	 * 'reference' page table is init_mm.pgd.
61*4882a593Smuzhiyun 	 *
62*4882a593Smuzhiyun 	 * NOTE! We MUST NOT take any locks for this case. We may
63*4882a593Smuzhiyun 	 * be in an interrupt or a critical region, and should
64*4882a593Smuzhiyun 	 * only copy the information from the master page table,
65*4882a593Smuzhiyun 	 * nothing more.
66*4882a593Smuzhiyun 	 */
67*4882a593Smuzhiyun 	if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END)) {
68*4882a593Smuzhiyun 		if (user_mode(regs))
69*4882a593Smuzhiyun 			goto bad_area_nosemaphore;
70*4882a593Smuzhiyun 		else
71*4882a593Smuzhiyun 			goto vmalloc_fault;
72*4882a593Smuzhiyun 	}
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (unlikely(address >= TASK_SIZE))
75*4882a593Smuzhiyun 		goto bad_area_nosemaphore;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	/*
78*4882a593Smuzhiyun 	 * If we're in an interrupt or have no user
79*4882a593Smuzhiyun 	 * context, we must not take the fault..
80*4882a593Smuzhiyun 	 */
81*4882a593Smuzhiyun 	if (faulthandler_disabled() || !mm)
82*4882a593Smuzhiyun 		goto bad_area_nosemaphore;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	if (user_mode(regs))
85*4882a593Smuzhiyun 		flags |= FAULT_FLAG_USER;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	if (!mmap_read_trylock(mm)) {
90*4882a593Smuzhiyun 		if (!user_mode(regs) && !search_exception_tables(regs->ea))
91*4882a593Smuzhiyun 			goto bad_area_nosemaphore;
92*4882a593Smuzhiyun retry:
93*4882a593Smuzhiyun 		mmap_read_lock(mm);
94*4882a593Smuzhiyun 	}
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	vma = find_vma(mm, address);
97*4882a593Smuzhiyun 	if (!vma)
98*4882a593Smuzhiyun 		goto bad_area;
99*4882a593Smuzhiyun 	if (vma->vm_start <= address)
100*4882a593Smuzhiyun 		goto good_area;
101*4882a593Smuzhiyun 	if (!(vma->vm_flags & VM_GROWSDOWN))
102*4882a593Smuzhiyun 		goto bad_area;
103*4882a593Smuzhiyun 	if (expand_stack(vma, address))
104*4882a593Smuzhiyun 		goto bad_area;
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun  * Ok, we have a good vm_area for this memory access, so
107*4882a593Smuzhiyun  * we can handle it..
108*4882a593Smuzhiyun  */
109*4882a593Smuzhiyun good_area:
110*4882a593Smuzhiyun 	code = SEGV_ACCERR;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	switch (cause) {
113*4882a593Smuzhiyun 	case EXC_SUPERV_INSN_ACCESS:
114*4882a593Smuzhiyun 		goto bad_area;
115*4882a593Smuzhiyun 	case EXC_SUPERV_DATA_ACCESS:
116*4882a593Smuzhiyun 		goto bad_area;
117*4882a593Smuzhiyun 	case EXC_X_PROTECTION_FAULT:
118*4882a593Smuzhiyun 		if (!(vma->vm_flags & VM_EXEC))
119*4882a593Smuzhiyun 			goto bad_area;
120*4882a593Smuzhiyun 		break;
121*4882a593Smuzhiyun 	case EXC_R_PROTECTION_FAULT:
122*4882a593Smuzhiyun 		if (!(vma->vm_flags & VM_READ))
123*4882a593Smuzhiyun 			goto bad_area;
124*4882a593Smuzhiyun 		break;
125*4882a593Smuzhiyun 	case EXC_W_PROTECTION_FAULT:
126*4882a593Smuzhiyun 		if (!(vma->vm_flags & VM_WRITE))
127*4882a593Smuzhiyun 			goto bad_area;
128*4882a593Smuzhiyun 		flags = FAULT_FLAG_WRITE;
129*4882a593Smuzhiyun 		break;
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/*
133*4882a593Smuzhiyun 	 * If for any reason at all we couldn't handle the fault,
134*4882a593Smuzhiyun 	 * make sure we exit gracefully rather than endlessly redo
135*4882a593Smuzhiyun 	 * the fault.
136*4882a593Smuzhiyun 	 */
137*4882a593Smuzhiyun 	fault = handle_mm_fault(vma, address, flags, regs);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	if (fault_signal_pending(fault, regs))
140*4882a593Smuzhiyun 		return;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	if (unlikely(fault & VM_FAULT_ERROR)) {
143*4882a593Smuzhiyun 		if (fault & VM_FAULT_OOM)
144*4882a593Smuzhiyun 			goto out_of_memory;
145*4882a593Smuzhiyun 		else if (fault & VM_FAULT_SIGSEGV)
146*4882a593Smuzhiyun 			goto bad_area;
147*4882a593Smuzhiyun 		else if (fault & VM_FAULT_SIGBUS)
148*4882a593Smuzhiyun 			goto do_sigbus;
149*4882a593Smuzhiyun 		BUG();
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (flags & FAULT_FLAG_ALLOW_RETRY) {
153*4882a593Smuzhiyun 		if (fault & VM_FAULT_RETRY) {
154*4882a593Smuzhiyun 			flags |= FAULT_FLAG_TRIED;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 			/*
157*4882a593Smuzhiyun 			 * No need to mmap_read_unlock(mm) as we would
158*4882a593Smuzhiyun 			 * have already released it in __lock_page_or_retry
159*4882a593Smuzhiyun 			 * in mm/filemap.c.
160*4882a593Smuzhiyun 			 */
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 			goto retry;
163*4882a593Smuzhiyun 		}
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	mmap_read_unlock(mm);
167*4882a593Smuzhiyun 	return;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun  * Something tried to access memory that isn't in our memory map..
171*4882a593Smuzhiyun  * Fix it, but check if it's kernel or user first..
172*4882a593Smuzhiyun  */
173*4882a593Smuzhiyun bad_area:
174*4882a593Smuzhiyun 	mmap_read_unlock(mm);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun bad_area_nosemaphore:
177*4882a593Smuzhiyun 	/* User mode accesses just cause a SIGSEGV */
178*4882a593Smuzhiyun 	if (user_mode(regs)) {
179*4882a593Smuzhiyun 		if (unhandled_signal(current, SIGSEGV) && printk_ratelimit()) {
180*4882a593Smuzhiyun 			pr_info("%s: unhandled page fault (%d) at 0x%08lx, "
181*4882a593Smuzhiyun 				"cause %ld\n", current->comm, SIGSEGV, address, cause);
182*4882a593Smuzhiyun 			show_regs(regs);
183*4882a593Smuzhiyun 		}
184*4882a593Smuzhiyun 		_exception(SIGSEGV, regs, code, address);
185*4882a593Smuzhiyun 		return;
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun no_context:
189*4882a593Smuzhiyun 	/* Are we prepared to handle this kernel fault? */
190*4882a593Smuzhiyun 	if (fixup_exception(regs))
191*4882a593Smuzhiyun 		return;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/*
194*4882a593Smuzhiyun 	 * Oops. The kernel tried to access some bad page. We'll have to
195*4882a593Smuzhiyun 	 * terminate things with extreme prejudice.
196*4882a593Smuzhiyun 	 */
197*4882a593Smuzhiyun 	bust_spinlocks(1);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	pr_alert("Unable to handle kernel %s at virtual address %08lx",
200*4882a593Smuzhiyun 		address < PAGE_SIZE ? "NULL pointer dereference" :
201*4882a593Smuzhiyun 		"paging request", address);
202*4882a593Smuzhiyun 	pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra,
203*4882a593Smuzhiyun 		cause);
204*4882a593Smuzhiyun 	panic("Oops");
205*4882a593Smuzhiyun 	return;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun /*
208*4882a593Smuzhiyun  * We ran out of memory, or some other thing happened to us that made
209*4882a593Smuzhiyun  * us unable to handle the page fault gracefully.
210*4882a593Smuzhiyun  */
211*4882a593Smuzhiyun out_of_memory:
212*4882a593Smuzhiyun 	mmap_read_unlock(mm);
213*4882a593Smuzhiyun 	if (!user_mode(regs))
214*4882a593Smuzhiyun 		goto no_context;
215*4882a593Smuzhiyun 	pagefault_out_of_memory();
216*4882a593Smuzhiyun 	return;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun do_sigbus:
219*4882a593Smuzhiyun 	mmap_read_unlock(mm);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/* Kernel mode? Handle exceptions or die */
222*4882a593Smuzhiyun 	if (!user_mode(regs))
223*4882a593Smuzhiyun 		goto no_context;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	_exception(SIGBUS, regs, BUS_ADRERR, address);
226*4882a593Smuzhiyun 	return;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun vmalloc_fault:
229*4882a593Smuzhiyun 	{
230*4882a593Smuzhiyun 		/*
231*4882a593Smuzhiyun 		 * Synchronize this task's top level page-table
232*4882a593Smuzhiyun 		 * with the 'reference' page table.
233*4882a593Smuzhiyun 		 *
234*4882a593Smuzhiyun 		 * Do _not_ use "tsk" here. We might be inside
235*4882a593Smuzhiyun 		 * an interrupt in the middle of a task switch..
236*4882a593Smuzhiyun 		 */
237*4882a593Smuzhiyun 		int offset = pgd_index(address);
238*4882a593Smuzhiyun 		pgd_t *pgd, *pgd_k;
239*4882a593Smuzhiyun 		p4d_t *p4d, *p4d_k;
240*4882a593Smuzhiyun 		pud_t *pud, *pud_k;
241*4882a593Smuzhiyun 		pmd_t *pmd, *pmd_k;
242*4882a593Smuzhiyun 		pte_t *pte_k;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 		pgd = pgd_current + offset;
245*4882a593Smuzhiyun 		pgd_k = init_mm.pgd + offset;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 		if (!pgd_present(*pgd_k))
248*4882a593Smuzhiyun 			goto no_context;
249*4882a593Smuzhiyun 		set_pgd(pgd, *pgd_k);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 		p4d = p4d_offset(pgd, address);
252*4882a593Smuzhiyun 		p4d_k = p4d_offset(pgd_k, address);
253*4882a593Smuzhiyun 		if (!p4d_present(*p4d_k))
254*4882a593Smuzhiyun 			goto no_context;
255*4882a593Smuzhiyun 		pud = pud_offset(p4d, address);
256*4882a593Smuzhiyun 		pud_k = pud_offset(p4d_k, address);
257*4882a593Smuzhiyun 		if (!pud_present(*pud_k))
258*4882a593Smuzhiyun 			goto no_context;
259*4882a593Smuzhiyun 		pmd = pmd_offset(pud, address);
260*4882a593Smuzhiyun 		pmd_k = pmd_offset(pud_k, address);
261*4882a593Smuzhiyun 		if (!pmd_present(*pmd_k))
262*4882a593Smuzhiyun 			goto no_context;
263*4882a593Smuzhiyun 		set_pmd(pmd, *pmd_k);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 		pte_k = pte_offset_kernel(pmd_k, address);
266*4882a593Smuzhiyun 		if (!pte_present(*pte_k))
267*4882a593Smuzhiyun 			goto no_context;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 		flush_tlb_kernel_page(address);
270*4882a593Smuzhiyun 		return;
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun }
273