fault.c 12 KB

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