xref: /openbmc/linux/arch/arm/mm/fault.c (revision 1da177e4)
1 /*
2  *  linux/arch/arm/mm/fault.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Modifications for ARM processor (c) 1995-2004 Russell King
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/signal.h>
14 #include <linux/ptrace.h>
15 #include <linux/mm.h>
16 #include <linux/init.h>
17 
18 #include <asm/system.h>
19 #include <asm/pgtable.h>
20 #include <asm/tlbflush.h>
21 #include <asm/uaccess.h>
22 
23 #include "fault.h"
24 
25 /*
26  * This is useful to dump out the page tables associated with
27  * 'addr' in mm 'mm'.
28  */
29 void show_pte(struct mm_struct *mm, unsigned long addr)
30 {
31 	pgd_t *pgd;
32 
33 	if (!mm)
34 		mm = &init_mm;
35 
36 	printk(KERN_ALERT "pgd = %p\n", mm->pgd);
37 	pgd = pgd_offset(mm, addr);
38 	printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
39 
40 	do {
41 		pmd_t *pmd;
42 		pte_t *pte;
43 
44 		if (pgd_none(*pgd))
45 			break;
46 
47 		if (pgd_bad(*pgd)) {
48 			printk("(bad)");
49 			break;
50 		}
51 
52 		pmd = pmd_offset(pgd, addr);
53 #if PTRS_PER_PMD != 1
54 		printk(", *pmd=%08lx", pmd_val(*pmd));
55 #endif
56 
57 		if (pmd_none(*pmd))
58 			break;
59 
60 		if (pmd_bad(*pmd)) {
61 			printk("(bad)");
62 			break;
63 		}
64 
65 #ifndef CONFIG_HIGHMEM
66 		/* We must not map this if we have highmem enabled */
67 		pte = pte_offset_map(pmd, addr);
68 		printk(", *pte=%08lx", pte_val(*pte));
69 		printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
70 		pte_unmap(pte);
71 #endif
72 	} while(0);
73 
74 	printk("\n");
75 }
76 
77 /*
78  * Oops.  The kernel tried to access some page that wasn't present.
79  */
80 static void
81 __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
82 		  struct pt_regs *regs)
83 {
84 	/*
85 	 * Are we prepared to handle this kernel fault?
86 	 */
87 	if (fixup_exception(regs))
88 		return;
89 
90 	/*
91 	 * No handler, we'll have to terminate things with extreme prejudice.
92 	 */
93 	bust_spinlocks(1);
94 	printk(KERN_ALERT
95 		"Unable to handle kernel %s at virtual address %08lx\n",
96 		(addr < PAGE_SIZE) ? "NULL pointer dereference" :
97 		"paging request", addr);
98 
99 	show_pte(mm, addr);
100 	die("Oops", regs, fsr);
101 	bust_spinlocks(0);
102 	do_exit(SIGKILL);
103 }
104 
105 /*
106  * Something tried to access memory that isn't in our memory map..
107  * User mode accesses just cause a SIGSEGV
108  */
109 static void
110 __do_user_fault(struct task_struct *tsk, unsigned long addr,
111 		unsigned int fsr, int code, struct pt_regs *regs)
112 {
113 	struct siginfo si;
114 
115 #ifdef CONFIG_DEBUG_USER
116 	if (user_debug & UDBG_SEGV) {
117 		printk(KERN_DEBUG "%s: unhandled page fault at 0x%08lx, code 0x%03x\n",
118 		       tsk->comm, addr, fsr);
119 		show_pte(tsk->mm, addr);
120 		show_regs(regs);
121 	}
122 #endif
123 
124 	tsk->thread.address = addr;
125 	tsk->thread.error_code = fsr;
126 	tsk->thread.trap_no = 14;
127 	si.si_signo = SIGSEGV;
128 	si.si_errno = 0;
129 	si.si_code = code;
130 	si.si_addr = (void __user *)addr;
131 	force_sig_info(SIGSEGV, &si, tsk);
132 }
133 
134 void
135 do_bad_area(struct task_struct *tsk, struct mm_struct *mm, unsigned long addr,
136 	    unsigned int fsr, struct pt_regs *regs)
137 {
138 	/*
139 	 * If we are in kernel mode at this point, we
140 	 * have no context to handle this fault with.
141 	 */
142 	if (user_mode(regs))
143 		__do_user_fault(tsk, addr, fsr, SEGV_MAPERR, regs);
144 	else
145 		__do_kernel_fault(mm, addr, fsr, regs);
146 }
147 
148 #define VM_FAULT_BADMAP		(-20)
149 #define VM_FAULT_BADACCESS	(-21)
150 
151 static int
152 __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
153 		struct task_struct *tsk)
154 {
155 	struct vm_area_struct *vma;
156 	int fault, mask;
157 
158 	vma = find_vma(mm, addr);
159 	fault = VM_FAULT_BADMAP;
160 	if (!vma)
161 		goto out;
162 	if (vma->vm_start > addr)
163 		goto check_stack;
164 
165 	/*
166 	 * Ok, we have a good vm_area for this
167 	 * memory access, so we can handle it.
168 	 */
169 good_area:
170 	if (fsr & (1 << 11)) /* write? */
171 		mask = VM_WRITE;
172 	else
173 		mask = VM_READ|VM_EXEC;
174 
175 	fault = VM_FAULT_BADACCESS;
176 	if (!(vma->vm_flags & mask))
177 		goto out;
178 
179 	/*
180 	 * If for any reason at all we couldn't handle
181 	 * the fault, make sure we exit gracefully rather
182 	 * than endlessly redo the fault.
183 	 */
184 survive:
185 	fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, fsr & (1 << 11));
186 
187 	/*
188 	 * Handle the "normal" cases first - successful and sigbus
189 	 */
190 	switch (fault) {
191 	case VM_FAULT_MAJOR:
192 		tsk->maj_flt++;
193 		return fault;
194 	case VM_FAULT_MINOR:
195 		tsk->min_flt++;
196 	case VM_FAULT_SIGBUS:
197 		return fault;
198 	}
199 
200 	if (tsk->pid != 1)
201 		goto out;
202 
203 	/*
204 	 * If we are out of memory for pid1,
205 	 * sleep for a while and retry
206 	 */
207 	yield();
208 	goto survive;
209 
210 check_stack:
211 	if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
212 		goto good_area;
213 out:
214 	return fault;
215 }
216 
217 static int
218 do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
219 {
220 	struct task_struct *tsk;
221 	struct mm_struct *mm;
222 	int fault;
223 
224 	tsk = current;
225 	mm  = tsk->mm;
226 
227 	/*
228 	 * If we're in an interrupt or have no user
229 	 * context, we must not take the fault..
230 	 */
231 	if (in_interrupt() || !mm)
232 		goto no_context;
233 
234 	down_read(&mm->mmap_sem);
235 	fault = __do_page_fault(mm, addr, fsr, tsk);
236 	up_read(&mm->mmap_sem);
237 
238 	/*
239 	 * Handle the "normal" case first
240 	 */
241 	if (fault > 0)
242 		return 0;
243 
244 	/*
245 	 * We had some memory, but were unable to
246 	 * successfully fix up this page fault.
247 	 */
248 	if (fault == 0)
249 		goto do_sigbus;
250 
251 	/*
252 	 * If we are in kernel mode at this point, we
253 	 * have no context to handle this fault with.
254 	 */
255 	if (!user_mode(regs))
256 		goto no_context;
257 
258 	if (fault == VM_FAULT_OOM) {
259 		/*
260 		 * We ran out of memory, or some other thing happened to
261 		 * us that made us unable to handle the page fault gracefully.
262 		 */
263 		printk("VM: killing process %s\n", tsk->comm);
264 		do_exit(SIGKILL);
265 	} else
266 		__do_user_fault(tsk, addr, fsr, fault == VM_FAULT_BADACCESS ?
267 				SEGV_ACCERR : SEGV_MAPERR, regs);
268 	return 0;
269 
270 
271 /*
272  * We ran out of memory, or some other thing happened to us that made
273  * us unable to handle the page fault gracefully.
274  */
275 do_sigbus:
276 	/*
277 	 * Send a sigbus, regardless of whether we were in kernel
278 	 * or user mode.
279 	 */
280 	tsk->thread.address = addr;
281 	tsk->thread.error_code = fsr;
282 	tsk->thread.trap_no = 14;
283 	force_sig(SIGBUS, tsk);
284 #ifdef CONFIG_DEBUG_USER
285 	if (user_debug & UDBG_BUS) {
286 		printk(KERN_DEBUG "%s: sigbus at 0x%08lx, pc=0x%08lx\n",
287 			current->comm, addr, instruction_pointer(regs));
288 	}
289 #endif
290 
291 	/* Kernel mode? Handle exceptions or die */
292 	if (user_mode(regs))
293 		return 0;
294 
295 no_context:
296 	__do_kernel_fault(mm, addr, fsr, regs);
297 	return 0;
298 }
299 
300 /*
301  * First Level Translation Fault Handler
302  *
303  * We enter here because the first level page table doesn't contain
304  * a valid entry for the address.
305  *
306  * If the address is in kernel space (>= TASK_SIZE), then we are
307  * probably faulting in the vmalloc() area.
308  *
309  * If the init_task's first level page tables contains the relevant
310  * entry, we copy the it to this task.  If not, we send the process
311  * a signal, fixup the exception, or oops the kernel.
312  *
313  * NOTE! We MUST NOT take any locks for this case. We may be in an
314  * interrupt or a critical region, and should only copy the information
315  * from the master page table, nothing more.
316  */
317 static int
318 do_translation_fault(unsigned long addr, unsigned int fsr,
319 		     struct pt_regs *regs)
320 {
321 	struct task_struct *tsk;
322 	unsigned int index;
323 	pgd_t *pgd, *pgd_k;
324 	pmd_t *pmd, *pmd_k;
325 
326 	if (addr < TASK_SIZE)
327 		return do_page_fault(addr, fsr, regs);
328 
329 	index = pgd_index(addr);
330 
331 	/*
332 	 * FIXME: CP15 C1 is write only on ARMv3 architectures.
333 	 */
334 	pgd = cpu_get_pgd() + index;
335 	pgd_k = init_mm.pgd + index;
336 
337 	if (pgd_none(*pgd_k))
338 		goto bad_area;
339 
340 	if (!pgd_present(*pgd))
341 		set_pgd(pgd, *pgd_k);
342 
343 	pmd_k = pmd_offset(pgd_k, addr);
344 	pmd   = pmd_offset(pgd, addr);
345 
346 	if (pmd_none(*pmd_k))
347 		goto bad_area;
348 
349 	copy_pmd(pmd, pmd_k);
350 	return 0;
351 
352 bad_area:
353 	tsk = current;
354 
355 	do_bad_area(tsk, tsk->active_mm, addr, fsr, regs);
356 	return 0;
357 }
358 
359 /*
360  * Some section permission faults need to be handled gracefully.
361  * They can happen due to a __{get,put}_user during an oops.
362  */
363 static int
364 do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
365 {
366 	struct task_struct *tsk = current;
367 	do_bad_area(tsk, tsk->active_mm, addr, fsr, regs);
368 	return 0;
369 }
370 
371 /*
372  * This abort handler always returns "fault".
373  */
374 static int
375 do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
376 {
377 	return 1;
378 }
379 
380 static struct fsr_info {
381 	int	(*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
382 	int	sig;
383 	const char *name;
384 } fsr_info[] = {
385 	/*
386 	 * The following are the standard ARMv3 and ARMv4 aborts.  ARMv5
387 	 * defines these to be "precise" aborts.
388 	 */
389 	{ do_bad,		SIGSEGV, "vector exception"		   },
390 	{ do_bad,		SIGILL,	 "alignment exception"		   },
391 	{ do_bad,		SIGKILL, "terminal exception"		   },
392 	{ do_bad,		SIGILL,	 "alignment exception"		   },
393 	{ do_bad,		SIGBUS,	 "external abort on linefetch"	   },
394 	{ do_translation_fault,	SIGSEGV, "section translation fault"	   },
395 	{ do_bad,		SIGBUS,	 "external abort on linefetch"	   },
396 	{ do_page_fault,	SIGSEGV, "page translation fault"	   },
397 	{ do_bad,		SIGBUS,	 "external abort on non-linefetch" },
398 	{ do_bad,		SIGSEGV, "section domain fault"		   },
399 	{ do_bad,		SIGBUS,	 "external abort on non-linefetch" },
400 	{ do_bad,		SIGSEGV, "page domain fault"		   },
401 	{ do_bad,		SIGBUS,	 "external abort on translation"   },
402 	{ do_sect_fault,	SIGSEGV, "section permission fault"	   },
403 	{ do_bad,		SIGBUS,	 "external abort on translation"   },
404 	{ do_page_fault,	SIGSEGV, "page permission fault"	   },
405 	/*
406 	 * The following are "imprecise" aborts, which are signalled by bit
407 	 * 10 of the FSR, and may not be recoverable.  These are only
408 	 * supported if the CPU abort handler supports bit 10.
409 	 */
410 	{ do_bad,		SIGBUS,  "unknown 16"			   },
411 	{ do_bad,		SIGBUS,  "unknown 17"			   },
412 	{ do_bad,		SIGBUS,  "unknown 18"			   },
413 	{ do_bad,		SIGBUS,  "unknown 19"			   },
414 	{ do_bad,		SIGBUS,  "lock abort"			   }, /* xscale */
415 	{ do_bad,		SIGBUS,  "unknown 21"			   },
416 	{ do_bad,		SIGBUS,  "imprecise external abort"	   }, /* xscale */
417 	{ do_bad,		SIGBUS,  "unknown 23"			   },
418 	{ do_bad,		SIGBUS,  "dcache parity error"		   }, /* xscale */
419 	{ do_bad,		SIGBUS,  "unknown 25"			   },
420 	{ do_bad,		SIGBUS,  "unknown 26"			   },
421 	{ do_bad,		SIGBUS,  "unknown 27"			   },
422 	{ do_bad,		SIGBUS,  "unknown 28"			   },
423 	{ do_bad,		SIGBUS,  "unknown 29"			   },
424 	{ do_bad,		SIGBUS,  "unknown 30"			   },
425 	{ do_bad,		SIGBUS,  "unknown 31"			   }
426 };
427 
428 void __init
429 hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
430 		int sig, const char *name)
431 {
432 	if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
433 		fsr_info[nr].fn   = fn;
434 		fsr_info[nr].sig  = sig;
435 		fsr_info[nr].name = name;
436 	}
437 }
438 
439 /*
440  * Dispatch a data abort to the relevant handler.
441  */
442 asmlinkage void
443 do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
444 {
445 	const struct fsr_info *inf = fsr_info + (fsr & 15) + ((fsr & (1 << 10)) >> 6);
446 
447 	if (!inf->fn(addr, fsr, regs))
448 		return;
449 
450 	printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
451 		inf->name, fsr, addr);
452 	force_sig(inf->sig, current);
453 	show_pte(current->mm, addr);
454 	die_if_kernel("Oops", regs, 0);
455 }
456 
457 asmlinkage void
458 do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
459 {
460 	do_translation_fault(addr, 0, regs);
461 }
462 
463