fault.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 vm_fault_t __do_pf(struct mm_struct *mm, unsigned long addr,
  145. unsigned int fsr, unsigned int flags, struct task_struct *tsk)
  146. {
  147. struct vm_area_struct *vma;
  148. vm_fault_t 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 sig, code;
  181. vm_fault_t fault;
  182. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  183. tsk = current;
  184. mm = tsk->mm;
  185. /*
  186. * If we're in an interrupt or have no user
  187. * context, we must not take the fault..
  188. */
  189. if (faulthandler_disabled() || !mm)
  190. goto no_context;
  191. if (user_mode(regs))
  192. flags |= FAULT_FLAG_USER;
  193. if (!(fsr ^ 0x12))
  194. flags |= FAULT_FLAG_WRITE;
  195. /*
  196. * As per x86, we may deadlock here. However, since the kernel only
  197. * validly references user space from well defined areas of the code,
  198. * we can bug out early if this is from code which shouldn't.
  199. */
  200. if (!down_read_trylock(&mm->mmap_sem)) {
  201. if (!user_mode(regs)
  202. && !search_exception_tables(regs->UCreg_pc))
  203. goto no_context;
  204. retry:
  205. down_read(&mm->mmap_sem);
  206. } else {
  207. /*
  208. * The above down_read_trylock() might have succeeded in
  209. * which case, we'll have missed the might_sleep() from
  210. * down_read()
  211. */
  212. might_sleep();
  213. #ifdef CONFIG_DEBUG_VM
  214. if (!user_mode(regs) &&
  215. !search_exception_tables(regs->UCreg_pc))
  216. goto no_context;
  217. #endif
  218. }
  219. fault = __do_pf(mm, addr, fsr, flags, tsk);
  220. /* If we need to retry but a fatal signal is pending, handle the
  221. * signal first. We do not need to release the mmap_sem because
  222. * it would already be released in __lock_page_or_retry in
  223. * mm/filemap.c. */
  224. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  225. return 0;
  226. if (!(fault & VM_FAULT_ERROR) && (flags & FAULT_FLAG_ALLOW_RETRY)) {
  227. if (fault & VM_FAULT_MAJOR)
  228. tsk->maj_flt++;
  229. else
  230. tsk->min_flt++;
  231. if (fault & VM_FAULT_RETRY) {
  232. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  233. * of starvation. */
  234. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  235. goto retry;
  236. }
  237. }
  238. up_read(&mm->mmap_sem);
  239. /*
  240. * Handle the "normal" case first - VM_FAULT_MAJOR
  241. */
  242. if (likely(!(fault &
  243. (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
  244. return 0;
  245. /*
  246. * If we are in kernel mode at this point, we
  247. * have no context to handle this fault with.
  248. */
  249. if (!user_mode(regs))
  250. goto no_context;
  251. if (fault & VM_FAULT_OOM) {
  252. /*
  253. * We ran out of memory, call the OOM killer, and return to
  254. * userspace (which will retry the fault, or kill us if we
  255. * got oom-killed)
  256. */
  257. pagefault_out_of_memory();
  258. return 0;
  259. }
  260. if (fault & VM_FAULT_SIGBUS) {
  261. /*
  262. * We had some memory, but were unable to
  263. * successfully fix up this page fault.
  264. */
  265. sig = SIGBUS;
  266. code = BUS_ADRERR;
  267. } else {
  268. /*
  269. * Something tried to access memory that
  270. * isn't in our memory map..
  271. */
  272. sig = SIGSEGV;
  273. code = fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR;
  274. }
  275. __do_user_fault(tsk, addr, fsr, sig, code, regs);
  276. return 0;
  277. no_context:
  278. __do_kernel_fault(mm, addr, fsr, regs);
  279. return 0;
  280. }
  281. /*
  282. * First Level Translation Fault Handler
  283. *
  284. * We enter here because the first level page table doesn't contain
  285. * a valid entry for the address.
  286. *
  287. * If the address is in kernel space (>= TASK_SIZE), then we are
  288. * probably faulting in the vmalloc() area.
  289. *
  290. * If the init_task's first level page tables contains the relevant
  291. * entry, we copy the it to this task. If not, we send the process
  292. * a signal, fixup the exception, or oops the kernel.
  293. *
  294. * NOTE! We MUST NOT take any locks for this case. We may be in an
  295. * interrupt or a critical region, and should only copy the information
  296. * from the master page table, nothing more.
  297. */
  298. static int do_ifault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  299. {
  300. unsigned int index;
  301. pgd_t *pgd, *pgd_k;
  302. pmd_t *pmd, *pmd_k;
  303. if (addr < TASK_SIZE)
  304. return do_pf(addr, fsr, regs);
  305. if (user_mode(regs))
  306. goto bad_area;
  307. index = pgd_index(addr);
  308. pgd = cpu_get_pgd() + index;
  309. pgd_k = init_mm.pgd + index;
  310. if (pgd_none(*pgd_k))
  311. goto bad_area;
  312. pmd_k = pmd_offset((pud_t *) pgd_k, addr);
  313. pmd = pmd_offset((pud_t *) pgd, addr);
  314. if (pmd_none(*pmd_k))
  315. goto bad_area;
  316. set_pmd(pmd, *pmd_k);
  317. flush_pmd_entry(pmd);
  318. return 0;
  319. bad_area:
  320. do_bad_area(addr, fsr, regs);
  321. return 0;
  322. }
  323. /*
  324. * This abort handler always returns "fault".
  325. */
  326. static int do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  327. {
  328. return 1;
  329. }
  330. static int do_good(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  331. {
  332. unsigned int res1, res2;
  333. printk("dabt exception but no error!\n");
  334. __asm__ __volatile__(
  335. "mff %0,f0\n"
  336. "mff %1,f1\n"
  337. : "=r"(res1), "=r"(res2)
  338. :
  339. : "memory");
  340. printk(KERN_EMERG "r0 :%08x r1 :%08x\n", res1, res2);
  341. panic("shut up\n");
  342. return 0;
  343. }
  344. static struct fsr_info {
  345. int (*fn) (unsigned long addr, unsigned int fsr, struct pt_regs *regs);
  346. int sig;
  347. int code;
  348. const char *name;
  349. } fsr_info[] = {
  350. /*
  351. * The following are the standard Unicore-I and UniCore-II aborts.
  352. */
  353. { do_good, SIGBUS, 0, "no error" },
  354. { do_bad, SIGBUS, BUS_ADRALN, "alignment exception" },
  355. { do_bad, SIGBUS, BUS_OBJERR, "external exception" },
  356. { do_bad, SIGBUS, 0, "burst operation" },
  357. { do_bad, SIGBUS, 0, "unknown 00100" },
  358. { do_ifault, SIGSEGV, SEGV_MAPERR, "2nd level pt non-exist"},
  359. { do_bad, SIGBUS, 0, "2nd lvl large pt non-exist" },
  360. { do_bad, SIGBUS, 0, "invalid pte" },
  361. { do_pf, SIGSEGV, SEGV_MAPERR, "page miss" },
  362. { do_bad, SIGBUS, 0, "middle page miss" },
  363. { do_bad, SIGBUS, 0, "large page miss" },
  364. { do_pf, SIGSEGV, SEGV_MAPERR, "super page (section) miss" },
  365. { do_bad, SIGBUS, 0, "unknown 01100" },
  366. { do_bad, SIGBUS, 0, "unknown 01101" },
  367. { do_bad, SIGBUS, 0, "unknown 01110" },
  368. { do_bad, SIGBUS, 0, "unknown 01111" },
  369. { do_bad, SIGBUS, 0, "addr: up 3G or IO" },
  370. { do_pf, SIGSEGV, SEGV_ACCERR, "read unreadable addr" },
  371. { do_pf, SIGSEGV, SEGV_ACCERR, "write unwriteable addr"},
  372. { do_pf, SIGSEGV, SEGV_ACCERR, "exec unexecutable addr"},
  373. { do_bad, SIGBUS, 0, "unknown 10100" },
  374. { do_bad, SIGBUS, 0, "unknown 10101" },
  375. { do_bad, SIGBUS, 0, "unknown 10110" },
  376. { do_bad, SIGBUS, 0, "unknown 10111" },
  377. { do_bad, SIGBUS, 0, "unknown 11000" },
  378. { do_bad, SIGBUS, 0, "unknown 11001" },
  379. { do_bad, SIGBUS, 0, "unknown 11010" },
  380. { do_bad, SIGBUS, 0, "unknown 11011" },
  381. { do_bad, SIGBUS, 0, "unknown 11100" },
  382. { do_bad, SIGBUS, 0, "unknown 11101" },
  383. { do_bad, SIGBUS, 0, "unknown 11110" },
  384. { do_bad, SIGBUS, 0, "unknown 11111" }
  385. };
  386. void __init hook_fault_code(int nr,
  387. int (*fn) (unsigned long, unsigned int, struct pt_regs *),
  388. int sig, int code, const char *name)
  389. {
  390. if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
  391. BUG();
  392. fsr_info[nr].fn = fn;
  393. fsr_info[nr].sig = sig;
  394. fsr_info[nr].code = code;
  395. fsr_info[nr].name = name;
  396. }
  397. /*
  398. * Dispatch a data abort to the relevant handler.
  399. */
  400. asmlinkage void do_DataAbort(unsigned long addr, unsigned int fsr,
  401. struct pt_regs *regs)
  402. {
  403. const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
  404. struct siginfo info;
  405. if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
  406. return;
  407. printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
  408. inf->name, fsr, addr);
  409. clear_siginfo(&info);
  410. info.si_signo = inf->sig;
  411. info.si_errno = 0;
  412. info.si_code = inf->code;
  413. info.si_addr = (void __user *)addr;
  414. uc32_notify_die("", regs, &info, fsr, 0);
  415. }
  416. asmlinkage void do_PrefetchAbort(unsigned long addr,
  417. unsigned int ifsr, struct pt_regs *regs)
  418. {
  419. const struct fsr_info *inf = fsr_info + fsr_fs(ifsr);
  420. struct siginfo info;
  421. if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
  422. return;
  423. printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
  424. inf->name, ifsr, addr);
  425. clear_siginfo(&info);
  426. info.si_signo = inf->sig;
  427. info.si_errno = 0;
  428. info.si_code = inf->code;
  429. info.si_addr = (void __user *)addr;
  430. uc32_notify_die("", regs, &info, ifsr, 0);
  431. }