fault.c 13 KB

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