fault.c 12 KB

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