fault.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * linux/arch/arm/mm/fault.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Modifications for ARM processor (c) 1995-2004 Russell King
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/signal.h>
  13. #include <linux/mm.h>
  14. #include <linux/hardirq.h>
  15. #include <linux/init.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/page-flags.h>
  19. #include <linux/sched.h>
  20. #include <linux/highmem.h>
  21. #include <asm/system.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/tlbflush.h>
  24. #include "fault.h"
  25. /*
  26. * Fault status register encodings
  27. */
  28. #define FSR_WRITE (1 << 11)
  29. #define FSR_FS4 (1 << 10)
  30. #define FSR_FS3_0 (15)
  31. static inline int fsr_fs(unsigned int fsr)
  32. {
  33. return (fsr & FSR_FS3_0) | (fsr & FSR_FS4) >> 6;
  34. }
  35. #ifdef CONFIG_MMU
  36. #ifdef CONFIG_KPROBES
  37. static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
  38. {
  39. int ret = 0;
  40. if (!user_mode(regs)) {
  41. /* kprobe_running() needs smp_processor_id() */
  42. preempt_disable();
  43. if (kprobe_running() && kprobe_fault_handler(regs, fsr))
  44. ret = 1;
  45. preempt_enable();
  46. }
  47. return ret;
  48. }
  49. #else
  50. static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
  51. {
  52. return 0;
  53. }
  54. #endif
  55. /*
  56. * This is useful to dump out the page tables associated with
  57. * 'addr' in mm 'mm'.
  58. */
  59. void show_pte(struct mm_struct *mm, unsigned long addr)
  60. {
  61. pgd_t *pgd;
  62. if (!mm)
  63. mm = &init_mm;
  64. printk(KERN_ALERT "pgd = %p\n", mm->pgd);
  65. pgd = pgd_offset(mm, addr);
  66. printk(KERN_ALERT "[%08lx] *pgd=%08lx", addr, pgd_val(*pgd));
  67. do {
  68. pmd_t *pmd;
  69. pte_t *pte;
  70. if (pgd_none(*pgd))
  71. break;
  72. if (pgd_bad(*pgd)) {
  73. printk("(bad)");
  74. break;
  75. }
  76. pmd = pmd_offset(pgd, addr);
  77. if (PTRS_PER_PMD != 1)
  78. printk(", *pmd=%08lx", pmd_val(*pmd));
  79. if (pmd_none(*pmd))
  80. break;
  81. if (pmd_bad(*pmd)) {
  82. printk("(bad)");
  83. break;
  84. }
  85. /* We must not map this if we have highmem enabled */
  86. if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
  87. break;
  88. pte = pte_offset_map(pmd, addr);
  89. printk(", *pte=%08lx", pte_val(*pte));
  90. printk(", *ppte=%08lx", pte_val(pte[-PTRS_PER_PTE]));
  91. pte_unmap(pte);
  92. } while(0);
  93. printk("\n");
  94. }
  95. #else /* CONFIG_MMU */
  96. void show_pte(struct mm_struct *mm, unsigned long addr)
  97. { }
  98. #endif /* CONFIG_MMU */
  99. /*
  100. * Oops. The kernel tried to access some page that wasn't present.
  101. */
  102. static void
  103. __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  104. struct pt_regs *regs)
  105. {
  106. /*
  107. * Are we prepared to handle this kernel fault?
  108. */
  109. if (fixup_exception(regs))
  110. return;
  111. /*
  112. * No handler, we'll have to terminate things with extreme prejudice.
  113. */
  114. bust_spinlocks(1);
  115. printk(KERN_ALERT
  116. "Unable to handle kernel %s at virtual address %08lx\n",
  117. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  118. "paging request", addr);
  119. show_pte(mm, addr);
  120. die("Oops", regs, fsr);
  121. bust_spinlocks(0);
  122. do_exit(SIGKILL);
  123. }
  124. /*
  125. * Something tried to access memory that isn't in our memory map..
  126. * User mode accesses just cause a SIGSEGV
  127. */
  128. static void
  129. __do_user_fault(struct task_struct *tsk, unsigned long addr,
  130. unsigned int fsr, unsigned int sig, int code,
  131. struct pt_regs *regs)
  132. {
  133. struct siginfo si;
  134. #ifdef CONFIG_DEBUG_USER
  135. if (user_debug & UDBG_SEGV) {
  136. printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
  137. tsk->comm, sig, addr, fsr);
  138. show_pte(tsk->mm, addr);
  139. show_regs(regs);
  140. }
  141. #endif
  142. tsk->thread.address = addr;
  143. tsk->thread.error_code = fsr;
  144. tsk->thread.trap_no = 14;
  145. si.si_signo = sig;
  146. si.si_errno = 0;
  147. si.si_code = code;
  148. si.si_addr = (void __user *)addr;
  149. force_sig_info(sig, &si, tsk);
  150. }
  151. void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  152. {
  153. struct task_struct *tsk = current;
  154. struct mm_struct *mm = tsk->active_mm;
  155. /*
  156. * If we are in kernel mode at this point, we
  157. * have no context to handle this fault with.
  158. */
  159. if (user_mode(regs))
  160. __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
  161. else
  162. __do_kernel_fault(mm, addr, fsr, regs);
  163. }
  164. #ifdef CONFIG_MMU
  165. #define VM_FAULT_BADMAP 0x010000
  166. #define VM_FAULT_BADACCESS 0x020000
  167. static int
  168. __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  169. struct task_struct *tsk)
  170. {
  171. struct vm_area_struct *vma;
  172. int fault, mask;
  173. vma = find_vma(mm, addr);
  174. fault = VM_FAULT_BADMAP;
  175. if (!vma)
  176. goto out;
  177. if (vma->vm_start > addr)
  178. goto check_stack;
  179. /*
  180. * Ok, we have a good vm_area for this
  181. * memory access, so we can handle it.
  182. */
  183. good_area:
  184. if (fsr & FSR_WRITE)
  185. mask = VM_WRITE;
  186. else
  187. mask = VM_READ|VM_EXEC|VM_WRITE;
  188. fault = VM_FAULT_BADACCESS;
  189. if (!(vma->vm_flags & mask))
  190. goto out;
  191. /*
  192. * If for any reason at all we couldn't handle the fault, make
  193. * sure we exit gracefully rather than endlessly redo the fault.
  194. */
  195. fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, (fsr & FSR_WRITE) ? FAULT_FLAG_WRITE : 0);
  196. if (unlikely(fault & VM_FAULT_ERROR))
  197. return fault;
  198. if (fault & VM_FAULT_MAJOR)
  199. tsk->maj_flt++;
  200. else
  201. tsk->min_flt++;
  202. return fault;
  203. check_stack:
  204. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  205. goto good_area;
  206. out:
  207. return fault;
  208. }
  209. static int __kprobes
  210. do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  211. {
  212. struct task_struct *tsk;
  213. struct mm_struct *mm;
  214. int fault, sig, code;
  215. if (notify_page_fault(regs, fsr))
  216. return 0;
  217. tsk = current;
  218. mm = tsk->mm;
  219. /*
  220. * If we're in an interrupt or have no user
  221. * context, we must not take the fault..
  222. */
  223. if (in_atomic() || !mm)
  224. goto no_context;
  225. /*
  226. * As per x86, we may deadlock here. However, since the kernel only
  227. * validly references user space from well defined areas of the code,
  228. * we can bug out early if this is from code which shouldn't.
  229. */
  230. if (!down_read_trylock(&mm->mmap_sem)) {
  231. if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
  232. goto no_context;
  233. down_read(&mm->mmap_sem);
  234. } else {
  235. /*
  236. * The above down_read_trylock() might have succeeded in
  237. * which case, we'll have missed the might_sleep() from
  238. * down_read()
  239. */
  240. might_sleep();
  241. }
  242. fault = __do_page_fault(mm, addr, fsr, tsk);
  243. up_read(&mm->mmap_sem);
  244. /*
  245. * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
  246. */
  247. if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
  248. return 0;
  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. /*
  259. * If we are in kernel mode at this point, we
  260. * have no context to handle this fault with.
  261. */
  262. if (!user_mode(regs))
  263. goto no_context;
  264. if (fault & VM_FAULT_SIGBUS) {
  265. /*
  266. * We had some memory, but were unable to
  267. * successfully fix up this page fault.
  268. */
  269. sig = SIGBUS;
  270. code = BUS_ADRERR;
  271. } else {
  272. /*
  273. * Something tried to access memory that
  274. * isn't in our memory map..
  275. */
  276. sig = SIGSEGV;
  277. code = fault == VM_FAULT_BADACCESS ?
  278. SEGV_ACCERR : SEGV_MAPERR;
  279. }
  280. __do_user_fault(tsk, addr, fsr, sig, code, regs);
  281. return 0;
  282. no_context:
  283. __do_kernel_fault(mm, addr, fsr, regs);
  284. return 0;
  285. }
  286. #else /* CONFIG_MMU */
  287. static int
  288. do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  289. {
  290. return 0;
  291. }
  292. #endif /* CONFIG_MMU */
  293. /*
  294. * First Level Translation Fault Handler
  295. *
  296. * We enter here because the first level page table doesn't contain
  297. * a valid entry for the address.
  298. *
  299. * If the address is in kernel space (>= TASK_SIZE), then we are
  300. * probably faulting in the vmalloc() area.
  301. *
  302. * If the init_task's first level page tables contains the relevant
  303. * entry, we copy the it to this task. If not, we send the process
  304. * a signal, fixup the exception, or oops the kernel.
  305. *
  306. * NOTE! We MUST NOT take any locks for this case. We may be in an
  307. * interrupt or a critical region, and should only copy the information
  308. * from the master page table, nothing more.
  309. */
  310. #ifdef CONFIG_MMU
  311. static int __kprobes
  312. do_translation_fault(unsigned long addr, unsigned int fsr,
  313. struct pt_regs *regs)
  314. {
  315. unsigned int index;
  316. pgd_t *pgd, *pgd_k;
  317. pmd_t *pmd, *pmd_k;
  318. if (addr < TASK_SIZE)
  319. return do_page_fault(addr, fsr, regs);
  320. index = pgd_index(addr);
  321. /*
  322. * FIXME: CP15 C1 is write only on ARMv3 architectures.
  323. */
  324. pgd = cpu_get_pgd() + index;
  325. pgd_k = init_mm.pgd + index;
  326. if (pgd_none(*pgd_k))
  327. goto bad_area;
  328. if (!pgd_present(*pgd))
  329. set_pgd(pgd, *pgd_k);
  330. pmd_k = pmd_offset(pgd_k, addr);
  331. pmd = pmd_offset(pgd, addr);
  332. if (pmd_none(*pmd_k))
  333. goto bad_area;
  334. copy_pmd(pmd, pmd_k);
  335. return 0;
  336. bad_area:
  337. do_bad_area(addr, fsr, regs);
  338. return 0;
  339. }
  340. #else /* CONFIG_MMU */
  341. static int
  342. do_translation_fault(unsigned long addr, unsigned int fsr,
  343. struct pt_regs *regs)
  344. {
  345. return 0;
  346. }
  347. #endif /* CONFIG_MMU */
  348. /*
  349. * Some section permission faults need to be handled gracefully.
  350. * They can happen due to a __{get,put}_user during an oops.
  351. */
  352. static int
  353. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  354. {
  355. do_bad_area(addr, fsr, regs);
  356. return 0;
  357. }
  358. /*
  359. * This abort handler always returns "fault".
  360. */
  361. static int
  362. do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  363. {
  364. return 1;
  365. }
  366. static struct fsr_info {
  367. int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
  368. int sig;
  369. int code;
  370. const char *name;
  371. } fsr_info[] = {
  372. /*
  373. * The following are the standard ARMv3 and ARMv4 aborts. ARMv5
  374. * defines these to be "precise" aborts.
  375. */
  376. { do_bad, SIGSEGV, 0, "vector exception" },
  377. { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
  378. { do_bad, SIGKILL, 0, "terminal exception" },
  379. { do_bad, SIGILL, BUS_ADRALN, "alignment exception" },
  380. { do_bad, SIGBUS, 0, "external abort on linefetch" },
  381. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "section translation fault" },
  382. { do_bad, SIGBUS, 0, "external abort on linefetch" },
  383. { do_page_fault, SIGSEGV, SEGV_MAPERR, "page translation fault" },
  384. { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
  385. { do_bad, SIGSEGV, SEGV_ACCERR, "section domain fault" },
  386. { do_bad, SIGBUS, 0, "external abort on non-linefetch" },
  387. { do_bad, SIGSEGV, SEGV_ACCERR, "page domain fault" },
  388. { do_bad, SIGBUS, 0, "external abort on translation" },
  389. { do_sect_fault, SIGSEGV, SEGV_ACCERR, "section permission fault" },
  390. { do_bad, SIGBUS, 0, "external abort on translation" },
  391. { do_page_fault, SIGSEGV, SEGV_ACCERR, "page permission fault" },
  392. /*
  393. * The following are "imprecise" aborts, which are signalled by bit
  394. * 10 of the FSR, and may not be recoverable. These are only
  395. * supported if the CPU abort handler supports bit 10.
  396. */
  397. { do_bad, SIGBUS, 0, "unknown 16" },
  398. { do_bad, SIGBUS, 0, "unknown 17" },
  399. { do_bad, SIGBUS, 0, "unknown 18" },
  400. { do_bad, SIGBUS, 0, "unknown 19" },
  401. { do_bad, SIGBUS, 0, "lock abort" }, /* xscale */
  402. { do_bad, SIGBUS, 0, "unknown 21" },
  403. { do_bad, SIGBUS, BUS_OBJERR, "imprecise external abort" }, /* xscale */
  404. { do_bad, SIGBUS, 0, "unknown 23" },
  405. { do_bad, SIGBUS, 0, "dcache parity error" }, /* xscale */
  406. { do_bad, SIGBUS, 0, "unknown 25" },
  407. { do_bad, SIGBUS, 0, "unknown 26" },
  408. { do_bad, SIGBUS, 0, "unknown 27" },
  409. { do_bad, SIGBUS, 0, "unknown 28" },
  410. { do_bad, SIGBUS, 0, "unknown 29" },
  411. { do_bad, SIGBUS, 0, "unknown 30" },
  412. { do_bad, SIGBUS, 0, "unknown 31" }
  413. };
  414. void __init
  415. hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  416. int sig, const char *name)
  417. {
  418. if (nr >= 0 && nr < ARRAY_SIZE(fsr_info)) {
  419. fsr_info[nr].fn = fn;
  420. fsr_info[nr].sig = sig;
  421. fsr_info[nr].name = name;
  422. }
  423. }
  424. /*
  425. * Dispatch a data abort to the relevant handler.
  426. */
  427. asmlinkage void __exception
  428. do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  429. {
  430. const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
  431. struct siginfo info;
  432. if (!inf->fn(addr, fsr, regs))
  433. return;
  434. printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
  435. inf->name, fsr, addr);
  436. info.si_signo = inf->sig;
  437. info.si_errno = 0;
  438. info.si_code = inf->code;
  439. info.si_addr = (void __user *)addr;
  440. arm_notify_die("", regs, &info, fsr, 0);
  441. }
  442. asmlinkage void __exception
  443. do_PrefetchAbort(unsigned long addr, struct pt_regs *regs)
  444. {
  445. do_translation_fault(addr, 0, regs);
  446. }