1 /*
2  * linux/arch/unicore32/mm/fault.c
3  *
4  * Code specific to PKUnity SoC and UniCore ISA
5  *
6  * Copyright (C) 2001-2010 GUAN Xue-tao
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 #include <linux/module.h>
13 #include <linux/signal.h>
14 #include <linux/mm.h>
15 #include <linux/hardirq.h>
16 #include <linux/init.h>
17 #include <linux/kprobes.h>
18 #include <linux/uaccess.h>
19 #include <linux/page-flags.h>
20 #include <linux/sched.h>
21 #include <linux/io.h>
22 
23 #include <asm/system.h>
24 #include <asm/pgtable.h>
25 #include <asm/tlbflush.h>
26 
27 /*
28  * Fault status register encodings.  We steal bit 31 for our own purposes.
29  */
30 #define FSR_LNX_PF		(1 << 31)
31 
fsr_fs(unsigned int fsr)32 static inline int fsr_fs(unsigned int fsr)
33 {
34 	/* xyabcde will be abcde+xy */
35 	return (fsr & 31) + ((fsr & (3 << 5)) >> 5);
36 }
37 
38 /*
39  * This is useful to dump out the page tables associated with
40  * 'addr' in mm 'mm'.
41  */
show_pte(struct mm_struct * mm,unsigned long addr)42 void show_pte(struct mm_struct *mm, unsigned long addr)
43 {
44 	pgd_t *pgd;
45 
46 	if (!mm)
47 		mm = &init_mm;
48 
49 	printk(KERN_ALERT "pgd = %p\n", mm->pgd);
50 	pgd = pgd_offset(mm, addr);
51 	printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
52 
53 	do {
54 		pmd_t *pmd;
55 		pte_t *pte;
56 
57 		if (pgd_none(*pgd))
58 			break;
59 
60 		if (pgd_bad(*pgd)) {
61 			printk("(bad)");
62 			break;
63 		}
64 
65 		pmd = pmd_offset((pud_t *) pgd, addr);
66 		if (PTRS_PER_PMD != 1)
67 			printk(", *pmd=%08lx", pmd_val(*pmd));
68 
69 		if (pmd_none(*pmd))
70 			break;
71 
72 		if (pmd_bad(*pmd)) {
73 			printk("(bad)");
74 			break;
75 		}
76 
77 		/* We must not map this if we have highmem enabled */
78 		if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
79 			break;
80 
81 		pte = pte_offset_map(pmd, addr);
82 		printk(", *pte=%08lx", pte_val(*pte));
83 		pte_unmap(pte);
84 	} while (0);
85 
86 	printk("\n");
87 }
88 
89 /*
90  * Oops.  The kernel tried to access some page that wasn't present.
91  */
__do_kernel_fault(struct mm_struct * mm,unsigned long addr,unsigned int fsr,struct pt_regs * regs)92 static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
93 		unsigned int fsr, struct pt_regs *regs)
94 {
95 	/*
96 	 * Are we prepared to handle this kernel fault?
97 	 */
98 	if (fixup_exception(regs))
99 		return;
100 
101 	/*
102 	 * No handler, we'll have to terminate things with extreme prejudice.
103 	 */
104 	bust_spinlocks(1);
105 	printk(KERN_ALERT
106 	       "Unable to handle kernel %s at virtual address %08lx\n",
107 	       (addr < PAGE_SIZE) ? "NULL pointer dereference" :
108 	       "paging request", addr);
109 
110 	show_pte(mm, addr);
111 	die("Oops", regs, fsr);
112 	bust_spinlocks(0);
113 	do_exit(SIGKILL);
114 }
115 
116 /*
117  * Something tried to access memory that isn't in our memory map..
118  * User mode accesses just cause a SIGSEGV
119  */
__do_user_fault(struct task_struct * tsk,unsigned long addr,unsigned int fsr,unsigned int sig,int code,struct pt_regs * regs)120 static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
121 		unsigned int fsr, unsigned int sig, int code,
122 		struct pt_regs *regs)
123 {
124 	struct siginfo si;
125 
126 	tsk->thread.address = addr;
127 	tsk->thread.error_code = fsr;
128 	tsk->thread.trap_no = 14;
129 	si.si_signo = sig;
130 	si.si_errno = 0;
131 	si.si_code = code;
132 	si.si_addr = (void __user *)addr;
133 	force_sig_info(sig, &si, tsk);
134 }
135 
do_bad_area(unsigned long addr,unsigned int fsr,struct pt_regs * regs)136 void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
137 {
138 	struct task_struct *tsk = current;
139 	struct mm_struct *mm = tsk->active_mm;
140 
141 	/*
142 	 * If we are in kernel mode at this point, we
143 	 * have no context to handle this fault with.
144 	 */
145 	if (user_mode(regs))
146 		__do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
147 	else
148 		__do_kernel_fault(mm, addr, fsr, regs);
149 }
150 
151 #define VM_FAULT_BADMAP		0x010000
152 #define VM_FAULT_BADACCESS	0x020000
153 
154 /*
155  * Check that the permissions on the VMA allow for the fault which occurred.
156  * If we encountered a write fault, we must have write permission, otherwise
157  * we allow any permission.
158  */
access_error(unsigned int fsr,struct vm_area_struct * vma)159 static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
160 {
161 	unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
162 
163 	if (!(fsr ^ 0x12))	/* write? */
164 		mask = VM_WRITE;
165 	if (fsr & FSR_LNX_PF)
166 		mask = VM_EXEC;
167 
168 	return vma->vm_flags & mask ? false : true;
169 }
170 
__do_pf(struct mm_struct * mm,unsigned long addr,unsigned int fsr,struct task_struct * tsk)171 static int __do_pf(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
172 		struct task_struct *tsk)
173 {
174 	struct vm_area_struct *vma;
175 	int fault;
176 
177 	vma = find_vma(mm, addr);
178 	fault = VM_FAULT_BADMAP;
179 	if (unlikely(!vma))
180 		goto out;
181 	if (unlikely(vma->vm_start > addr))
182 		goto check_stack;
183 
184 	/*
185 	 * Ok, we have a good vm_area for this
186 	 * memory access, so we can handle it.
187 	 */
188 good_area:
189 	if (access_error(fsr, vma)) {
190 		fault = VM_FAULT_BADACCESS;
191 		goto out;
192 	}
193 
194 	/*
195 	 * If for any reason at all we couldn't handle the fault, make
196 	 * sure we exit gracefully rather than endlessly redo the fault.
197 	 */
198 	fault = handle_mm_fault(mm, vma, addr & PAGE_MASK,
199 			    (!(fsr ^ 0x12)) ? FAULT_FLAG_WRITE : 0);
200 	if (unlikely(fault & VM_FAULT_ERROR))
201 		return fault;
202 	if (fault & VM_FAULT_MAJOR)
203 		tsk->maj_flt++;
204 	else
205 		tsk->min_flt++;
206 	return fault;
207 
208 check_stack:
209 	if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
210 		goto good_area;
211 out:
212 	return fault;
213 }
214 
do_pf(unsigned long addr,unsigned int fsr,struct pt_regs * regs)215 static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
216 {
217 	struct task_struct *tsk;
218 	struct mm_struct *mm;
219 	int fault, sig, code;
220 
221 	tsk = current;
222 	mm = tsk->mm;
223 
224 	/*
225 	 * If we're in an interrupt or have no user
226 	 * context, we must not take the fault..
227 	 */
228 	if (in_atomic() || !mm)
229 		goto no_context;
230 
231 	/*
232 	 * As per x86, we may deadlock here.  However, since the kernel only
233 	 * validly references user space from well defined areas of the code,
234 	 * we can bug out early if this is from code which shouldn't.
235 	 */
236 	if (!down_read_trylock(&mm->mmap_sem)) {
237 		if (!user_mode(regs)
238 		    && !search_exception_tables(regs->UCreg_pc))
239 			goto no_context;
240 		down_read(&mm->mmap_sem);
241 	} else {
242 		/*
243 		 * The above down_read_trylock() might have succeeded in
244 		 * which case, we'll have missed the might_sleep() from
245 		 * down_read()
246 		 */
247 		might_sleep();
248 #ifdef CONFIG_DEBUG_VM
249 		if (!user_mode(regs) &&
250 		    !search_exception_tables(regs->UCreg_pc))
251 			goto no_context;
252 #endif
253 	}
254 
255 	fault = __do_pf(mm, addr, fsr, tsk);
256 	up_read(&mm->mmap_sem);
257 
258 	/*
259 	 * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
260 	 */
261 	if (likely(!(fault &
262 	       (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
263 		return 0;
264 
265 	if (fault & VM_FAULT_OOM) {
266 		/*
267 		 * We ran out of memory, call the OOM killer, and return to
268 		 * userspace (which will retry the fault, or kill us if we
269 		 * got oom-killed)
270 		 */
271 		pagefault_out_of_memory();
272 		return 0;
273 	}
274 
275 	/*
276 	 * If we are in kernel mode at this point, we
277 	 * have no context to handle this fault with.
278 	 */
279 	if (!user_mode(regs))
280 		goto no_context;
281 
282 	if (fault & VM_FAULT_SIGBUS) {
283 		/*
284 		 * We had some memory, but were unable to
285 		 * successfully fix up this page fault.
286 		 */
287 		sig = SIGBUS;
288 		code = BUS_ADRERR;
289 	} else {
290 		/*
291 		 * Something tried to access memory that
292 		 * isn't in our memory map..
293 		 */
294 		sig = SIGSEGV;
295 		code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR;
296 	}
297 
298 	__do_user_fault(tsk, addr, fsr, sig, code, regs);
299 	return 0;
300 
301 no_context:
302 	__do_kernel_fault(mm, addr, fsr, regs);
303 	return 0;
304 }
305 
306 /*
307  * First Level Translation Fault Handler
308  *
309  * We enter here because the first level page table doesn't contain
310  * a valid entry for the address.
311  *
312  * If the address is in kernel space (>= TASK_SIZE), then we are
313  * probably faulting in the vmalloc() area.
314  *
315  * If the init_task's first level page tables contains the relevant
316  * entry, we copy the it to this task.  If not, we send the process
317  * a signal, fixup the exception, or oops the kernel.
318  *
319  * NOTE! We MUST NOT take any locks for this case. We may be in an
320  * interrupt or a critical region, and should only copy the information
321  * from the master page table, nothing more.
322  */
do_ifault(unsigned long addr,unsigned int fsr,struct pt_regs * regs)323 static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
324 {
325 	unsigned int index;
326 	pgd_t *pgd, *pgd_k;
327 	pmd_t *pmd, *pmd_k;
328 
329 	if (addr < TASK_SIZE)
330 		return do_pf(addr, fsr, regs);
331 
332 	if (user_mode(regs))
333 		goto bad_area;
334 
335 	index = pgd_index(addr);
336 
337 	pgd = cpu_get_pgd() + index;
338 	pgd_k = init_mm.pgd + index;
339 
340 	if (pgd_none(*pgd_k))
341 		goto bad_area;
342 
343 	pmd_k = pmd_offset((pud_t *) pgd_k, addr);
344 	pmd = pmd_offset((pud_t *) pgd, addr);
345 
346 	if (pmd_none(*pmd_k))
347 		goto bad_area;
348 
349 	set_pmd(pmd, *pmd_k);
350 	flush_pmd_entry(pmd);
351 	return 0;
352 
353 bad_area:
354 	do_bad_area(addr, fsr, regs);
355 	return 0;
356 }
357 
358 /*
359  * This abort handler always returns "fault".
360  */
do_bad(unsigned long addr,unsigned int fsr,struct pt_regs * regs)361 static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
362 {
363 	return 1;
364 }
365 
do_good(unsigned long addr,unsigned int fsr,struct pt_regs * regs)366 static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
367 {
368 	unsigned int res1, res2;
369 
370 	printk("dabt exception but no error!\n");
371 
372 	__asm__ __volatile__(
373 			"mff %0,f0\n"
374 			"mff %1,f1\n"
375 			: "=r"(res1), "=r"(res2)
376 			:
377 			: "memory");
378 
379 	printk(KERN_EMERG "r0 :%08x  r1 :%08x\n", res1, res2);
380 	panic("shut up\n");
381 	return 0;
382 }
383 
384 static struct fsr_info {
385 	int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs);
386 	int sig;
387 	int code;
388 	const char *name;
389 } fsr_info[] = {
390 	/*
391 	 * The following are the standard Unicore-I and UniCore-II aborts.
392 	 */
393 	{ do_good,	SIGBUS,  0,		"no error"		},
394 	{ do_bad,	SIGBUS,  BUS_ADRALN,	"alignment exception"	},
395 	{ do_bad,	SIGBUS,  BUS_OBJERR,	"external exception"	},
396 	{ do_bad,	SIGBUS,  0,		"burst operation"	},
397 	{ do_bad,	SIGBUS,  0,		"unknown 00100"		},
398 	{ do_ifault,	SIGSEGV, SEGV_MAPERR,	"2nd level pt non-exist"},
399 	{ do_bad,	SIGBUS,  0,		"2nd lvl large pt non-exist" },
400 	{ do_bad,	SIGBUS,  0,		"invalid pte"		},
401 	{ do_pf,	SIGSEGV, SEGV_MAPERR,	"page miss"		},
402 	{ do_bad,	SIGBUS,  0,		"middle page miss"	},
403 	{ do_bad,	SIGBUS,	 0,		"large page miss"	},
404 	{ do_pf,	SIGSEGV, SEGV_MAPERR,	"super page (section) miss" },
405 	{ do_bad,	SIGBUS,  0,		"unknown 01100"		},
406 	{ do_bad,	SIGBUS,  0,		"unknown 01101"		},
407 	{ do_bad,	SIGBUS,  0,		"unknown 01110"		},
408 	{ do_bad,	SIGBUS,  0,		"unknown 01111"		},
409 	{ do_bad,	SIGBUS,  0,		"addr: up 3G or IO"	},
410 	{ do_pf,	SIGSEGV, SEGV_ACCERR,	"read unreadable addr"	},
411 	{ do_pf,	SIGSEGV, SEGV_ACCERR,	"write unwriteable addr"},
412 	{ do_pf,	SIGSEGV, SEGV_ACCERR,	"exec unexecutable addr"},
413 	{ do_bad,	SIGBUS,  0,		"unknown 10100"		},
414 	{ do_bad,	SIGBUS,  0,		"unknown 10101"		},
415 	{ do_bad,	SIGBUS,  0,		"unknown 10110"		},
416 	{ do_bad,	SIGBUS,  0,		"unknown 10111"		},
417 	{ do_bad,	SIGBUS,  0,		"unknown 11000"		},
418 	{ do_bad,	SIGBUS,  0,		"unknown 11001"		},
419 	{ do_bad,	SIGBUS,  0,		"unknown 11010"		},
420 	{ do_bad,	SIGBUS,  0,		"unknown 11011"		},
421 	{ do_bad,	SIGBUS,  0,		"unknown 11100"		},
422 	{ do_bad,	SIGBUS,  0,		"unknown 11101"		},
423 	{ do_bad,	SIGBUS,  0,		"unknown 11110"		},
424 	{ do_bad,	SIGBUS,  0,		"unknown 11111"		}
425 };
426 
hook_fault_code(int nr,int (* fn)(unsigned long,unsigned int,struct pt_regs *),int sig,int code,const char * name)427 void __init hook_fault_code(int nr,
428 		int (*fn) (unsigned long, unsigned int, struct pt_regs *),
429 		int sig, int code, const char *name)
430 {
431 	if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
432 		BUG();
433 
434 	fsr_info[nr].fn   = fn;
435 	fsr_info[nr].sig  = sig;
436 	fsr_info[nr].code = code;
437 	fsr_info[nr].name = name;
438 }
439 
440 /*
441  * Dispatch a data abort to the relevant handler.
442  */
do_DataAbort(unsigned long addr,unsigned int fsr,struct pt_regs * regs)443 asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr,
444 			struct pt_regs *regs)
445 {
446 	const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
447 	struct siginfo info;
448 
449 	if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
450 		return;
451 
452 	printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
453 	       inf->name, fsr, addr);
454 
455 	info.si_signo = inf->sig;
456 	info.si_errno = 0;
457 	info.si_code = inf->code;
458 	info.si_addr = (void __user *)addr;
459 	uc32_notify_die("", regs, &info, fsr, 0);
460 }
461 
do_PrefetchAbort(unsigned long addr,unsigned int ifsr,struct pt_regs * regs)462 asmlinkage void do_PrefetchAbort(unsigned long addr,
463 			unsigned int ifsr, struct pt_regs *regs)
464 {
465 	const struct fsr_info *inf = fsr_info + fsr_fs(ifsr);
466 	struct siginfo info;
467 
468 	if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
469 		return;
470 
471 	printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
472 	       inf->name, ifsr, addr);
473 
474 	info.si_signo = inf->sig;
475 	info.si_errno = 0;
476 	info.si_code = inf->code;
477 	info.si_addr = (void __user *)addr;
478 	uc32_notify_die("", regs, &info, ifsr, 0);
479 }
480