]>
Commit | Line | Data |
---|---|---|
56372b0b G |
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 | */ | |
0f91f3f6 | 12 | #include <linux/extable.h> |
56372b0b G |
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> | |
3f07c014 | 20 | #include <linux/sched/signal.h> |
56372b0b G |
21 | #include <linux/io.h> |
22 | ||
56372b0b G |
23 | #include <asm/pgtable.h> |
24 | #include <asm/tlbflush.h> | |
25 | ||
26 | /* | |
27 | * Fault status register encodings. We steal bit 31 for our own purposes. | |
28 | */ | |
29 | #define FSR_LNX_PF (1 << 31) | |
30 | ||
31 | static inline int fsr_fs(unsigned int fsr) | |
32 | { | |
33 | /* xyabcde will be abcde+xy */ | |
34 | return (fsr & 31) + ((fsr & (3 << 5)) >> 5); | |
35 | } | |
36 | ||
37 | /* | |
38 | * This is useful to dump out the page tables associated with | |
39 | * 'addr' in mm 'mm'. | |
40 | */ | |
41 | void show_pte(struct mm_struct *mm, unsigned long addr) | |
42 | { | |
43 | pgd_t *pgd; | |
44 | ||
45 | if (!mm) | |
46 | mm = &init_mm; | |
47 | ||
48 | printk(KERN_ALERT "pgd = %p\n", mm->pgd); | |
49 | pgd = pgd_offset(mm, addr); | |
50 | printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd)); | |
51 | ||
52 | do { | |
53 | pmd_t *pmd; | |
54 | pte_t *pte; | |
55 | ||
56 | if (pgd_none(*pgd)) | |
57 | break; | |
58 | ||
59 | if (pgd_bad(*pgd)) { | |
60 | printk("(bad)"); | |
61 | break; | |
62 | } | |
63 | ||
64 | pmd = pmd_offset((pud_t *) pgd, addr); | |
65 | if (PTRS_PER_PMD != 1) | |
66 | printk(", *pmd=%08lx", pmd_val(*pmd)); | |
67 | ||
68 | if (pmd_none(*pmd)) | |
69 | break; | |
70 | ||
71 | if (pmd_bad(*pmd)) { | |
72 | printk("(bad)"); | |
73 | break; | |
74 | } | |
75 | ||
76 | /* We must not map this if we have highmem enabled */ | |
77 | if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT))) | |
78 | break; | |
79 | ||
80 | pte = pte_offset_map(pmd, addr); | |
81 | printk(", *pte=%08lx", pte_val(*pte)); | |
82 | pte_unmap(pte); | |
83 | } while (0); | |
84 | ||
85 | printk("\n"); | |
86 | } | |
87 | ||
88 | /* | |
89 | * Oops. The kernel tried to access some page that wasn't present. | |
90 | */ | |
91 | static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr, | |
92 | unsigned int fsr, struct pt_regs *regs) | |
93 | { | |
94 | /* | |
95 | * Are we prepared to handle this kernel fault? | |
96 | */ | |
97 | if (fixup_exception(regs)) | |
98 | return; | |
99 | ||
100 | /* | |
101 | * No handler, we'll have to terminate things with extreme prejudice. | |
102 | */ | |
103 | bust_spinlocks(1); | |
104 | printk(KERN_ALERT | |
105 | "Unable to handle kernel %s at virtual address %08lx\n", | |
106 | (addr < PAGE_SIZE) ? "NULL pointer dereference" : | |
107 | "paging request", addr); | |
108 | ||
109 | show_pte(mm, addr); | |
110 | die("Oops", regs, fsr); | |
111 | bust_spinlocks(0); | |
112 | do_exit(SIGKILL); | |
113 | } | |
114 | ||
115 | /* | |
116 | * Something tried to access memory that isn't in our memory map.. | |
117 | * User mode accesses just cause a SIGSEGV | |
118 | */ | |
119 | static void __do_user_fault(struct task_struct *tsk, unsigned long addr, | |
120 | unsigned int fsr, unsigned int sig, int code, | |
121 | struct pt_regs *regs) | |
122 | { | |
123 | struct siginfo si; | |
124 | ||
125 | tsk->thread.address = addr; | |
126 | tsk->thread.error_code = fsr; | |
127 | tsk->thread.trap_no = 14; | |
3eb0f519 | 128 | clear_siginfo(&si); |
56372b0b G |
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 | ||
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 | */ | |
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 | ||
171 | static int __do_pf(struct mm_struct *mm, unsigned long addr, unsigned int fsr, | |
f3f09d5a | 172 | unsigned int flags, struct task_struct *tsk) |
56372b0b G |
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 | */ | |
dcddffd4 | 198 | fault = handle_mm_fault(vma, addr & PAGE_MASK, flags); |
56372b0b G |
199 | return fault; |
200 | ||
201 | check_stack: | |
202 | if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr)) | |
203 | goto good_area; | |
204 | out: | |
205 | return fault; | |
206 | } | |
207 | ||
208 | static int do_pf(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | |
209 | { | |
210 | struct task_struct *tsk; | |
211 | struct mm_struct *mm; | |
212 | int fault, sig, code; | |
759496ba | 213 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; |
56372b0b G |
214 | |
215 | tsk = current; | |
216 | mm = tsk->mm; | |
217 | ||
218 | /* | |
219 | * If we're in an interrupt or have no user | |
220 | * context, we must not take the fault.. | |
221 | */ | |
70ffdb93 | 222 | if (faulthandler_disabled() || !mm) |
56372b0b G |
223 | goto no_context; |
224 | ||
759496ba JW |
225 | if (user_mode(regs)) |
226 | flags |= FAULT_FLAG_USER; | |
227 | if (!(fsr ^ 0x12)) | |
228 | flags |= FAULT_FLAG_WRITE; | |
229 | ||
56372b0b G |
230 | /* |
231 | * As per x86, we may deadlock here. However, since the kernel only | |
232 | * validly references user space from well defined areas of the code, | |
233 | * we can bug out early if this is from code which shouldn't. | |
234 | */ | |
235 | if (!down_read_trylock(&mm->mmap_sem)) { | |
236 | if (!user_mode(regs) | |
237 | && !search_exception_tables(regs->UCreg_pc)) | |
238 | goto no_context; | |
f3f09d5a | 239 | retry: |
56372b0b G |
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 | ||
f3f09d5a KC |
255 | fault = __do_pf(mm, addr, fsr, flags, tsk); |
256 | ||
257 | /* If we need to retry but a fatal signal is pending, handle the | |
258 | * signal first. We do not need to release the mmap_sem because | |
259 | * it would already be released in __lock_page_or_retry in | |
260 | * mm/filemap.c. */ | |
261 | if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) | |
262 | return 0; | |
263 | ||
264 | if (!(fault & VM_FAULT_ERROR) && (flags & FAULT_FLAG_ALLOW_RETRY)) { | |
265 | if (fault & VM_FAULT_MAJOR) | |
266 | tsk->maj_flt++; | |
267 | else | |
268 | tsk->min_flt++; | |
269 | if (fault & VM_FAULT_RETRY) { | |
270 | /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk | |
271 | * of starvation. */ | |
272 | flags &= ~FAULT_FLAG_ALLOW_RETRY; | |
273 | goto retry; | |
274 | } | |
275 | } | |
276 | ||
56372b0b G |
277 | up_read(&mm->mmap_sem); |
278 | ||
279 | /* | |
0e8fb931 | 280 | * Handle the "normal" case first - VM_FAULT_MAJOR |
56372b0b G |
281 | */ |
282 | if (likely(!(fault & | |
283 | (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS)))) | |
284 | return 0; | |
285 | ||
87134102 JW |
286 | /* |
287 | * If we are in kernel mode at this point, we | |
288 | * have no context to handle this fault with. | |
289 | */ | |
290 | if (!user_mode(regs)) | |
291 | goto no_context; | |
292 | ||
56372b0b G |
293 | if (fault & VM_FAULT_OOM) { |
294 | /* | |
295 | * We ran out of memory, call the OOM killer, and return to | |
296 | * userspace (which will retry the fault, or kill us if we | |
297 | * got oom-killed) | |
298 | */ | |
299 | pagefault_out_of_memory(); | |
300 | return 0; | |
301 | } | |
302 | ||
56372b0b G |
303 | if (fault & VM_FAULT_SIGBUS) { |
304 | /* | |
305 | * We had some memory, but were unable to | |
306 | * successfully fix up this page fault. | |
307 | */ | |
308 | sig = SIGBUS; | |
309 | code = BUS_ADRERR; | |
310 | } else { | |
311 | /* | |
312 | * Something tried to access memory that | |
313 | * isn't in our memory map.. | |
314 | */ | |
315 | sig = SIGSEGV; | |
316 | code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR; | |
317 | } | |
318 | ||
319 | __do_user_fault(tsk, addr, fsr, sig, code, regs); | |
320 | return 0; | |
321 | ||
322 | no_context: | |
323 | __do_kernel_fault(mm, addr, fsr, regs); | |
324 | return 0; | |
325 | } | |
326 | ||
327 | /* | |
328 | * First Level Translation Fault Handler | |
329 | * | |
330 | * We enter here because the first level page table doesn't contain | |
331 | * a valid entry for the address. | |
332 | * | |
333 | * If the address is in kernel space (>= TASK_SIZE), then we are | |
334 | * probably faulting in the vmalloc() area. | |
335 | * | |
336 | * If the init_task's first level page tables contains the relevant | |
337 | * entry, we copy the it to this task. If not, we send the process | |
338 | * a signal, fixup the exception, or oops the kernel. | |
339 | * | |
340 | * NOTE! We MUST NOT take any locks for this case. We may be in an | |
341 | * interrupt or a critical region, and should only copy the information | |
342 | * from the master page table, nothing more. | |
343 | */ | |
344 | static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | |
345 | { | |
346 | unsigned int index; | |
347 | pgd_t *pgd, *pgd_k; | |
348 | pmd_t *pmd, *pmd_k; | |
349 | ||
350 | if (addr < TASK_SIZE) | |
351 | return do_pf(addr, fsr, regs); | |
352 | ||
353 | if (user_mode(regs)) | |
354 | goto bad_area; | |
355 | ||
356 | index = pgd_index(addr); | |
357 | ||
358 | pgd = cpu_get_pgd() + index; | |
359 | pgd_k = init_mm.pgd + index; | |
360 | ||
361 | if (pgd_none(*pgd_k)) | |
362 | goto bad_area; | |
363 | ||
364 | pmd_k = pmd_offset((pud_t *) pgd_k, addr); | |
365 | pmd = pmd_offset((pud_t *) pgd, addr); | |
366 | ||
367 | if (pmd_none(*pmd_k)) | |
368 | goto bad_area; | |
369 | ||
370 | set_pmd(pmd, *pmd_k); | |
371 | flush_pmd_entry(pmd); | |
372 | return 0; | |
373 | ||
374 | bad_area: | |
375 | do_bad_area(addr, fsr, regs); | |
376 | return 0; | |
377 | } | |
378 | ||
379 | /* | |
380 | * This abort handler always returns "fault". | |
381 | */ | |
382 | static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | |
383 | { | |
384 | return 1; | |
385 | } | |
386 | ||
387 | static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs) | |
388 | { | |
389 | unsigned int res1, res2; | |
390 | ||
391 | printk("dabt exception but no error!\n"); | |
392 | ||
393 | __asm__ __volatile__( | |
394 | "mff %0,f0\n" | |
395 | "mff %1,f1\n" | |
396 | : "=r"(res1), "=r"(res2) | |
397 | : | |
398 | : "memory"); | |
399 | ||
400 | printk(KERN_EMERG "r0 :%08x r1 :%08x\n", res1, res2); | |
401 | panic("shut up\n"); | |
402 | return 0; | |
403 | } | |
404 | ||
405 | static struct fsr_info { | |
406 | int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs); | |
407 | int sig; | |
408 | int code; | |
409 | const char *name; | |
410 | } fsr_info[] = { | |
411 | /* | |
412 | * The following are the standard Unicore-I and UniCore-II aborts. | |
413 | */ | |
414 | { do_good, SIGBUS, 0, "no error" }, | |
415 | { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" }, | |
416 | { do_bad, SIGBUS, BUS_OBJERR, "external exception" }, | |
417 | { do_bad, SIGBUS, 0, "burst operation" }, | |
418 | { do_bad, SIGBUS, 0, "unknown 00100" }, | |
419 | { do_ifault, SIGSEGV, SEGV_MAPERR, "2nd level pt non-exist"}, | |
420 | { do_bad, SIGBUS, 0, "2nd lvl large pt non-exist" }, | |
421 | { do_bad, SIGBUS, 0, "invalid pte" }, | |
422 | { do_pf, SIGSEGV, SEGV_MAPERR, "page miss" }, | |
423 | { do_bad, SIGBUS, 0, "middle page miss" }, | |
424 | { do_bad, SIGBUS, 0, "large page miss" }, | |
425 | { do_pf, SIGSEGV, SEGV_MAPERR, "super page (section) miss" }, | |
426 | { do_bad, SIGBUS, 0, "unknown 01100" }, | |
427 | { do_bad, SIGBUS, 0, "unknown 01101" }, | |
428 | { do_bad, SIGBUS, 0, "unknown 01110" }, | |
429 | { do_bad, SIGBUS, 0, "unknown 01111" }, | |
430 | { do_bad, SIGBUS, 0, "addr: up 3G or IO" }, | |
431 | { do_pf, SIGSEGV, SEGV_ACCERR, "read unreadable addr" }, | |
432 | { do_pf, SIGSEGV, SEGV_ACCERR, "write unwriteable addr"}, | |
433 | { do_pf, SIGSEGV, SEGV_ACCERR, "exec unexecutable addr"}, | |
434 | { do_bad, SIGBUS, 0, "unknown 10100" }, | |
435 | { do_bad, SIGBUS, 0, "unknown 10101" }, | |
436 | { do_bad, SIGBUS, 0, "unknown 10110" }, | |
437 | { do_bad, SIGBUS, 0, "unknown 10111" }, | |
438 | { do_bad, SIGBUS, 0, "unknown 11000" }, | |
439 | { do_bad, SIGBUS, 0, "unknown 11001" }, | |
440 | { do_bad, SIGBUS, 0, "unknown 11010" }, | |
441 | { do_bad, SIGBUS, 0, "unknown 11011" }, | |
442 | { do_bad, SIGBUS, 0, "unknown 11100" }, | |
443 | { do_bad, SIGBUS, 0, "unknown 11101" }, | |
444 | { do_bad, SIGBUS, 0, "unknown 11110" }, | |
445 | { do_bad, SIGBUS, 0, "unknown 11111" } | |
446 | }; | |
447 | ||
448 | void __init hook_fault_code(int nr, | |
449 | int (*fn) (unsigned long, unsigned int, struct pt_regs *), | |
450 | int sig, int code, const char *name) | |
451 | { | |
452 | if (nr < 0 || nr >= ARRAY_SIZE(fsr_info)) | |
453 | BUG(); | |
454 | ||
455 | fsr_info[nr].fn = fn; | |
456 | fsr_info[nr].sig = sig; | |
457 | fsr_info[nr].code = code; | |
458 | fsr_info[nr].name = name; | |
459 | } | |
460 | ||
461 | /* | |
462 | * Dispatch a data abort to the relevant handler. | |
463 | */ | |
464 | asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr, | |
465 | struct pt_regs *regs) | |
466 | { | |
467 | const struct fsr_info *inf = fsr_info + fsr_fs(fsr); | |
468 | struct siginfo info; | |
469 | ||
470 | if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs)) | |
471 | return; | |
472 | ||
473 | printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n", | |
474 | inf->name, fsr, addr); | |
475 | ||
3eb0f519 | 476 | clear_siginfo(&info); |
56372b0b G |
477 | info.si_signo = inf->sig; |
478 | info.si_errno = 0; | |
479 | info.si_code = inf->code; | |
480 | info.si_addr = (void __user *)addr; | |
481 | uc32_notify_die("", regs, &info, fsr, 0); | |
482 | } | |
483 | ||
484 | asmlinkage void do_PrefetchAbort(unsigned long addr, | |
485 | unsigned int ifsr, struct pt_regs *regs) | |
486 | { | |
487 | const struct fsr_info *inf = fsr_info + fsr_fs(ifsr); | |
488 | struct siginfo info; | |
489 | ||
490 | if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs)) | |
491 | return; | |
492 | ||
493 | printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n", | |
494 | inf->name, ifsr, addr); | |
495 | ||
3eb0f519 | 496 | clear_siginfo(&info); |
56372b0b G |
497 | info.si_signo = inf->sig; |
498 | info.si_errno = 0; | |
499 | info.si_code = inf->code; | |
500 | info.si_addr = (void __user *)addr; | |
501 | uc32_notify_die("", regs, &info, ifsr, 0); | |
502 | } |