fault.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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/extable.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/signal.h>
  20. #include <linux/sched/debug.h>
  21. #include <linux/highmem.h>
  22. #include <linux/perf_event.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/system_misc.h>
  25. #include <asm/system_info.h>
  26. #include <asm/tlbflush.h>
  27. #include "fault.h"
  28. #ifdef CONFIG_MMU
  29. #ifdef CONFIG_KPROBES
  30. static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
  31. {
  32. int ret = 0;
  33. if (!user_mode(regs)) {
  34. /* kprobe_running() needs smp_processor_id() */
  35. preempt_disable();
  36. if (kprobe_running() && kprobe_fault_handler(regs, fsr))
  37. ret = 1;
  38. preempt_enable();
  39. }
  40. return ret;
  41. }
  42. #else
  43. static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
  44. {
  45. return 0;
  46. }
  47. #endif
  48. /*
  49. * This is useful to dump out the page tables associated with
  50. * 'addr' in mm 'mm'.
  51. */
  52. void show_pte(struct mm_struct *mm, unsigned long addr)
  53. {
  54. pgd_t *pgd;
  55. if (!mm)
  56. mm = &init_mm;
  57. pr_alert("pgd = %p\n", mm->pgd);
  58. pgd = pgd_offset(mm, addr);
  59. pr_alert("[%08lx] *pgd=%08llx",
  60. addr, (long long)pgd_val(*pgd));
  61. do {
  62. pud_t *pud;
  63. pmd_t *pmd;
  64. pte_t *pte;
  65. if (pgd_none(*pgd))
  66. break;
  67. if (pgd_bad(*pgd)) {
  68. pr_cont("(bad)");
  69. break;
  70. }
  71. pud = pud_offset(pgd, addr);
  72. if (PTRS_PER_PUD != 1)
  73. pr_cont(", *pud=%08llx", (long long)pud_val(*pud));
  74. if (pud_none(*pud))
  75. break;
  76. if (pud_bad(*pud)) {
  77. pr_cont("(bad)");
  78. break;
  79. }
  80. pmd = pmd_offset(pud, addr);
  81. if (PTRS_PER_PMD != 1)
  82. pr_cont(", *pmd=%08llx", (long long)pmd_val(*pmd));
  83. if (pmd_none(*pmd))
  84. break;
  85. if (pmd_bad(*pmd)) {
  86. pr_cont("(bad)");
  87. break;
  88. }
  89. /* We must not map this if we have highmem enabled */
  90. if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
  91. break;
  92. pte = pte_offset_map(pmd, addr);
  93. pr_cont(", *pte=%08llx", (long long)pte_val(*pte));
  94. #ifndef CONFIG_ARM_LPAE
  95. pr_cont(", *ppte=%08llx",
  96. (long long)pte_val(pte[PTE_HWTABLE_PTRS]));
  97. #endif
  98. pte_unmap(pte);
  99. } while(0);
  100. pr_cont("\n");
  101. }
  102. #else /* CONFIG_MMU */
  103. void show_pte(struct mm_struct *mm, unsigned long addr)
  104. { }
  105. #endif /* CONFIG_MMU */
  106. /*
  107. * Oops. The kernel tried to access some page that wasn't present.
  108. */
  109. static void
  110. __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  111. struct pt_regs *regs)
  112. {
  113. /*
  114. * Are we prepared to handle this kernel fault?
  115. */
  116. if (fixup_exception(regs))
  117. return;
  118. /*
  119. * No handler, we'll have to terminate things with extreme prejudice.
  120. */
  121. bust_spinlocks(1);
  122. pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
  123. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  124. "paging request", addr);
  125. show_pte(mm, addr);
  126. die("Oops", regs, fsr);
  127. bust_spinlocks(0);
  128. do_exit(SIGKILL);
  129. }
  130. /*
  131. * Something tried to access memory that isn't in our memory map..
  132. * User mode accesses just cause a SIGSEGV
  133. */
  134. static void
  135. __do_user_fault(struct task_struct *tsk, unsigned long addr,
  136. unsigned int fsr, unsigned int sig, int code,
  137. struct pt_regs *regs)
  138. {
  139. struct siginfo si;
  140. if (addr > TASK_SIZE)
  141. harden_branch_predictor();
  142. clear_siginfo(&si);
  143. #ifdef CONFIG_DEBUG_USER
  144. if (((user_debug & UDBG_SEGV) && (sig == SIGSEGV)) ||
  145. ((user_debug & UDBG_BUS) && (sig == SIGBUS))) {
  146. printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
  147. tsk->comm, sig, addr, fsr);
  148. show_pte(tsk->mm, addr);
  149. show_regs(regs);
  150. }
  151. #endif
  152. tsk->thread.address = addr;
  153. tsk->thread.error_code = fsr;
  154. tsk->thread.trap_no = 14;
  155. si.si_signo = sig;
  156. si.si_errno = 0;
  157. si.si_code = code;
  158. si.si_addr = (void __user *)addr;
  159. force_sig_info(sig, &si, tsk);
  160. }
  161. void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  162. {
  163. struct task_struct *tsk = current;
  164. struct mm_struct *mm = tsk->active_mm;
  165. /*
  166. * If we are in kernel mode at this point, we
  167. * have no context to handle this fault with.
  168. */
  169. if (user_mode(regs))
  170. __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
  171. else
  172. __do_kernel_fault(mm, addr, fsr, regs);
  173. }
  174. #ifdef CONFIG_MMU
  175. #define VM_FAULT_BADMAP 0x010000
  176. #define VM_FAULT_BADACCESS 0x020000
  177. /*
  178. * Check that the permissions on the VMA allow for the fault which occurred.
  179. * If we encountered a write fault, we must have write permission, otherwise
  180. * we allow any permission.
  181. */
  182. static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
  183. {
  184. unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
  185. if (fsr & FSR_WRITE)
  186. mask = VM_WRITE;
  187. if (fsr & FSR_LNX_PF)
  188. mask = VM_EXEC;
  189. return vma->vm_flags & mask ? false : true;
  190. }
  191. static int __kprobes
  192. __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  193. unsigned int flags, struct task_struct *tsk)
  194. {
  195. struct vm_area_struct *vma;
  196. int fault;
  197. vma = find_vma(mm, addr);
  198. fault = VM_FAULT_BADMAP;
  199. if (unlikely(!vma))
  200. goto out;
  201. if (unlikely(vma->vm_start > addr))
  202. goto check_stack;
  203. /*
  204. * Ok, we have a good vm_area for this
  205. * memory access, so we can handle it.
  206. */
  207. good_area:
  208. if (access_error(fsr, vma)) {
  209. fault = VM_FAULT_BADACCESS;
  210. goto out;
  211. }
  212. return handle_mm_fault(vma, addr & PAGE_MASK, flags);
  213. check_stack:
  214. /* Don't allow expansion below FIRST_USER_ADDRESS */
  215. if (vma->vm_flags & VM_GROWSDOWN &&
  216. addr >= FIRST_USER_ADDRESS && !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. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  228. if (notify_page_fault(regs, fsr))
  229. return 0;
  230. tsk = current;
  231. mm = tsk->mm;
  232. /* Enable interrupts if they were enabled in the parent context. */
  233. if (interrupts_enabled(regs))
  234. local_irq_enable();
  235. /*
  236. * If we're in an interrupt or have no user
  237. * context, we must not take the fault..
  238. */
  239. if (faulthandler_disabled() || !mm)
  240. goto no_context;
  241. if (user_mode(regs))
  242. flags |= FAULT_FLAG_USER;
  243. if (fsr & FSR_WRITE)
  244. flags |= FAULT_FLAG_WRITE;
  245. /*
  246. * As per x86, we may deadlock here. However, since the kernel only
  247. * validly references user space from well defined areas of the code,
  248. * we can bug out early if this is from code which shouldn't.
  249. */
  250. if (!down_read_trylock(&mm->mmap_sem)) {
  251. if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
  252. goto no_context;
  253. retry:
  254. down_read(&mm->mmap_sem);
  255. } else {
  256. /*
  257. * The above down_read_trylock() might have succeeded in
  258. * which case, we'll have missed the might_sleep() from
  259. * down_read()
  260. */
  261. might_sleep();
  262. #ifdef CONFIG_DEBUG_VM
  263. if (!user_mode(regs) &&
  264. !search_exception_tables(regs->ARM_pc))
  265. goto no_context;
  266. #endif
  267. }
  268. fault = __do_page_fault(mm, addr, fsr, flags, tsk);
  269. /* If we need to retry but a fatal signal is pending, handle the
  270. * signal first. We do not need to release the mmap_sem because
  271. * it would already be released in __lock_page_or_retry in
  272. * mm/filemap.c. */
  273. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  274. if (!user_mode(regs))
  275. goto no_context;
  276. return 0;
  277. }
  278. /*
  279. * Major/minor page fault accounting is only done on the
  280. * initial attempt. If we go through a retry, it is extremely
  281. * likely that the page will be found in page cache at that point.
  282. */
  283. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  284. if (!(fault & VM_FAULT_ERROR) && flags & FAULT_FLAG_ALLOW_RETRY) {
  285. if (fault & VM_FAULT_MAJOR) {
  286. tsk->maj_flt++;
  287. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  288. regs, addr);
  289. } else {
  290. tsk->min_flt++;
  291. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  292. regs, addr);
  293. }
  294. if (fault & VM_FAULT_RETRY) {
  295. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  296. * of starvation. */
  297. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  298. flags |= FAULT_FLAG_TRIED;
  299. goto retry;
  300. }
  301. }
  302. up_read(&mm->mmap_sem);
  303. /*
  304. * Handle the "normal" case first - VM_FAULT_MAJOR
  305. */
  306. if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
  307. return 0;
  308. /*
  309. * If we are in kernel mode at this point, we
  310. * have no context to handle this fault with.
  311. */
  312. if (!user_mode(regs))
  313. goto no_context;
  314. if (fault & VM_FAULT_OOM) {
  315. /*
  316. * We ran out of memory, call the OOM killer, and return to
  317. * userspace (which will retry the fault, or kill us if we
  318. * got oom-killed)
  319. */
  320. pagefault_out_of_memory();
  321. return 0;
  322. }
  323. if (fault & VM_FAULT_SIGBUS) {
  324. /*
  325. * We had some memory, but were unable to
  326. * successfully fix up this page fault.
  327. */
  328. sig = SIGBUS;
  329. code = BUS_ADRERR;
  330. } else {
  331. /*
  332. * Something tried to access memory that
  333. * isn't in our memory map..
  334. */
  335. sig = SIGSEGV;
  336. code = fault == VM_FAULT_BADACCESS ?
  337. SEGV_ACCERR : SEGV_MAPERR;
  338. }
  339. __do_user_fault(tsk, addr, fsr, sig, code, regs);
  340. return 0;
  341. no_context:
  342. __do_kernel_fault(mm, addr, fsr, regs);
  343. return 0;
  344. }
  345. #else /* CONFIG_MMU */
  346. static int
  347. do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  348. {
  349. return 0;
  350. }
  351. #endif /* CONFIG_MMU */
  352. /*
  353. * First Level Translation Fault Handler
  354. *
  355. * We enter here because the first level page table doesn't contain
  356. * a valid entry for the address.
  357. *
  358. * If the address is in kernel space (>= TASK_SIZE), then we are
  359. * probably faulting in the vmalloc() area.
  360. *
  361. * If the init_task's first level page tables contains the relevant
  362. * entry, we copy the it to this task. If not, we send the process
  363. * a signal, fixup the exception, or oops the kernel.
  364. *
  365. * NOTE! We MUST NOT take any locks for this case. We may be in an
  366. * interrupt or a critical region, and should only copy the information
  367. * from the master page table, nothing more.
  368. */
  369. #ifdef CONFIG_MMU
  370. static int __kprobes
  371. do_translation_fault(unsigned long addr, unsigned int fsr,
  372. struct pt_regs *regs)
  373. {
  374. unsigned int index;
  375. pgd_t *pgd, *pgd_k;
  376. pud_t *pud, *pud_k;
  377. pmd_t *pmd, *pmd_k;
  378. if (addr < TASK_SIZE)
  379. return do_page_fault(addr, fsr, regs);
  380. if (user_mode(regs))
  381. goto bad_area;
  382. index = pgd_index(addr);
  383. pgd = cpu_get_pgd() + index;
  384. pgd_k = init_mm.pgd + index;
  385. if (pgd_none(*pgd_k))
  386. goto bad_area;
  387. if (!pgd_present(*pgd))
  388. set_pgd(pgd, *pgd_k);
  389. pud = pud_offset(pgd, addr);
  390. pud_k = pud_offset(pgd_k, addr);
  391. if (pud_none(*pud_k))
  392. goto bad_area;
  393. if (!pud_present(*pud))
  394. set_pud(pud, *pud_k);
  395. pmd = pmd_offset(pud, addr);
  396. pmd_k = pmd_offset(pud_k, addr);
  397. #ifdef CONFIG_ARM_LPAE
  398. /*
  399. * Only one hardware entry per PMD with LPAE.
  400. */
  401. index = 0;
  402. #else
  403. /*
  404. * On ARM one Linux PGD entry contains two hardware entries (see page
  405. * tables layout in pgtable.h). We normally guarantee that we always
  406. * fill both L1 entries. But create_mapping() doesn't follow the rule.
  407. * It can create inidividual L1 entries, so here we have to call
  408. * pmd_none() check for the entry really corresponded to address, not
  409. * for the first of pair.
  410. */
  411. index = (addr >> SECTION_SHIFT) & 1;
  412. #endif
  413. if (pmd_none(pmd_k[index]))
  414. goto bad_area;
  415. copy_pmd(pmd, pmd_k);
  416. return 0;
  417. bad_area:
  418. do_bad_area(addr, fsr, regs);
  419. return 0;
  420. }
  421. #else /* CONFIG_MMU */
  422. static int
  423. do_translation_fault(unsigned long addr, unsigned int fsr,
  424. struct pt_regs *regs)
  425. {
  426. return 0;
  427. }
  428. #endif /* CONFIG_MMU */
  429. /*
  430. * Some section permission faults need to be handled gracefully.
  431. * They can happen due to a __{get,put}_user during an oops.
  432. */
  433. #ifndef CONFIG_ARM_LPAE
  434. static int
  435. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  436. {
  437. do_bad_area(addr, fsr, regs);
  438. return 0;
  439. }
  440. #endif /* CONFIG_ARM_LPAE */
  441. /*
  442. * This abort handler always returns "fault".
  443. */
  444. static int
  445. do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  446. {
  447. return 1;
  448. }
  449. struct fsr_info {
  450. int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
  451. int sig;
  452. int code;
  453. const char *name;
  454. };
  455. /* FSR definition */
  456. #ifdef CONFIG_ARM_LPAE
  457. #include "fsr-3level.c"
  458. #else
  459. #include "fsr-2level.c"
  460. #endif
  461. void __init
  462. hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  463. int sig, int code, const char *name)
  464. {
  465. if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
  466. BUG();
  467. fsr_info[nr].fn = fn;
  468. fsr_info[nr].sig = sig;
  469. fsr_info[nr].code = code;
  470. fsr_info[nr].name = name;
  471. }
  472. /*
  473. * Dispatch a data abort to the relevant handler.
  474. */
  475. asmlinkage void
  476. do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  477. {
  478. const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
  479. struct siginfo info;
  480. if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
  481. return;
  482. pr_alert("Unhandled fault: %s (0x%03x) at 0x%08lx\n",
  483. inf->name, fsr, addr);
  484. show_pte(current->mm, addr);
  485. clear_siginfo(&info);
  486. info.si_signo = inf->sig;
  487. info.si_errno = 0;
  488. info.si_code = inf->code;
  489. info.si_addr = (void __user *)addr;
  490. arm_notify_die("", regs, &info, fsr, 0);
  491. }
  492. void __init
  493. hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  494. int sig, int code, const char *name)
  495. {
  496. if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
  497. BUG();
  498. ifsr_info[nr].fn = fn;
  499. ifsr_info[nr].sig = sig;
  500. ifsr_info[nr].code = code;
  501. ifsr_info[nr].name = name;
  502. }
  503. asmlinkage void
  504. do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
  505. {
  506. const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
  507. struct siginfo info;
  508. if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
  509. return;
  510. pr_alert("Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
  511. inf->name, ifsr, addr);
  512. clear_siginfo(&info);
  513. info.si_signo = inf->sig;
  514. info.si_errno = 0;
  515. info.si_code = inf->code;
  516. info.si_addr = (void __user *)addr;
  517. arm_notify_die("", regs, &info, ifsr, 0);
  518. }
  519. /*
  520. * Abort handler to be used only during first unmasking of asynchronous aborts
  521. * on the boot CPU. This makes sure that the machine will not die if the
  522. * firmware/bootloader left an imprecise abort pending for us to trip over.
  523. */
  524. static int __init early_abort_handler(unsigned long addr, unsigned int fsr,
  525. struct pt_regs *regs)
  526. {
  527. pr_warn("Hit pending asynchronous external abort (FSR=0x%08x) during "
  528. "first unmask, this is most likely caused by a "
  529. "firmware/bootloader bug.\n", fsr);
  530. return 0;
  531. }
  532. void __init early_abt_enable(void)
  533. {
  534. fsr_info[FSR_FS_AEA].fn = early_abort_handler;
  535. local_abt_enable();
  536. fsr_info[FSR_FS_AEA].fn = do_bad;
  537. }
  538. #ifndef CONFIG_ARM_LPAE
  539. static int __init exceptions_init(void)
  540. {
  541. if (cpu_architecture() >= CPU_ARCH_ARMv6) {
  542. hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
  543. "I-cache maintenance fault");
  544. }
  545. if (cpu_architecture() >= CPU_ARCH_ARMv7) {
  546. /*
  547. * TODO: Access flag faults introduced in ARMv6K.
  548. * Runtime check for 'K' extension is needed
  549. */
  550. hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
  551. "section access flag fault");
  552. hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
  553. "section access flag fault");
  554. }
  555. return 0;
  556. }
  557. arch_initcall(exceptions_init);
  558. #endif