fault.c 12 KB

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