fault.c 12 KB

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