fault.c 14 KB

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