fault.c 15 KB

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