fault.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Based on arch/arm/mm/fault.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Copyright (C) 1995-2004 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/signal.h>
  22. #include <linux/mm.h>
  23. #include <linux/hardirq.h>
  24. #include <linux/init.h>
  25. #include <linux/kprobes.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/page-flags.h>
  28. #include <linux/sched.h>
  29. #include <linux/highmem.h>
  30. #include <linux/perf_event.h>
  31. #include <asm/cpufeature.h>
  32. #include <asm/exception.h>
  33. #include <asm/debug-monitors.h>
  34. #include <asm/esr.h>
  35. #include <asm/sysreg.h>
  36. #include <asm/system_misc.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/tlbflush.h>
  39. static const char *fault_name(unsigned int esr);
  40. /*
  41. * Dump out the page tables associated with 'addr' in mm 'mm'.
  42. */
  43. void show_pte(struct mm_struct *mm, unsigned long addr)
  44. {
  45. pgd_t *pgd;
  46. if (!mm)
  47. mm = &init_mm;
  48. pr_alert("pgd = %p\n", mm->pgd);
  49. pgd = pgd_offset(mm, addr);
  50. pr_alert("[%08lx] *pgd=%016llx", addr, pgd_val(*pgd));
  51. do {
  52. pud_t *pud;
  53. pmd_t *pmd;
  54. pte_t *pte;
  55. if (pgd_none(*pgd) || pgd_bad(*pgd))
  56. break;
  57. pud = pud_offset(pgd, addr);
  58. printk(", *pud=%016llx", pud_val(*pud));
  59. if (pud_none(*pud) || pud_bad(*pud))
  60. break;
  61. pmd = pmd_offset(pud, addr);
  62. printk(", *pmd=%016llx", pmd_val(*pmd));
  63. if (pmd_none(*pmd) || pmd_bad(*pmd))
  64. break;
  65. pte = pte_offset_map(pmd, addr);
  66. printk(", *pte=%016llx", pte_val(*pte));
  67. pte_unmap(pte);
  68. } while(0);
  69. printk("\n");
  70. }
  71. /*
  72. * The kernel tried to access some page that wasn't present.
  73. */
  74. static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
  75. unsigned int esr, struct pt_regs *regs)
  76. {
  77. /*
  78. * Are we prepared to handle this kernel fault?
  79. */
  80. if (fixup_exception(regs))
  81. return;
  82. /*
  83. * No handler, we'll have to terminate things with extreme prejudice.
  84. */
  85. bust_spinlocks(1);
  86. pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
  87. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  88. "paging request", addr);
  89. show_pte(mm, addr);
  90. die("Oops", regs, esr);
  91. bust_spinlocks(0);
  92. do_exit(SIGKILL);
  93. }
  94. /*
  95. * Something tried to access memory that isn't in our memory map. User mode
  96. * accesses just cause a SIGSEGV
  97. */
  98. static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
  99. unsigned int esr, unsigned int sig, int code,
  100. struct pt_regs *regs)
  101. {
  102. struct siginfo si;
  103. if (unhandled_signal(tsk, sig) && show_unhandled_signals_ratelimited()) {
  104. pr_info("%s[%d]: unhandled %s (%d) at 0x%08lx, esr 0x%03x\n",
  105. tsk->comm, task_pid_nr(tsk), fault_name(esr), sig,
  106. addr, esr);
  107. show_pte(tsk->mm, addr);
  108. show_regs(regs);
  109. }
  110. tsk->thread.fault_address = addr;
  111. tsk->thread.fault_code = esr;
  112. si.si_signo = sig;
  113. si.si_errno = 0;
  114. si.si_code = code;
  115. si.si_addr = (void __user *)addr;
  116. force_sig_info(sig, &si, tsk);
  117. }
  118. static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  119. {
  120. struct task_struct *tsk = current;
  121. struct mm_struct *mm = tsk->active_mm;
  122. /*
  123. * If we are in kernel mode at this point, we have no context to
  124. * handle this fault with.
  125. */
  126. if (user_mode(regs))
  127. __do_user_fault(tsk, addr, esr, SIGSEGV, SEGV_MAPERR, regs);
  128. else
  129. __do_kernel_fault(mm, addr, esr, regs);
  130. }
  131. #define VM_FAULT_BADMAP 0x010000
  132. #define VM_FAULT_BADACCESS 0x020000
  133. #define ESR_LNX_EXEC (1 << 24)
  134. static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
  135. unsigned int mm_flags, unsigned long vm_flags,
  136. struct task_struct *tsk)
  137. {
  138. struct vm_area_struct *vma;
  139. int fault;
  140. vma = find_vma(mm, addr);
  141. fault = VM_FAULT_BADMAP;
  142. if (unlikely(!vma))
  143. goto out;
  144. if (unlikely(vma->vm_start > addr))
  145. goto check_stack;
  146. /*
  147. * Ok, we have a good vm_area for this memory access, so we can handle
  148. * it.
  149. */
  150. good_area:
  151. /*
  152. * Check that the permissions on the VMA allow for the fault which
  153. * occurred. If we encountered a write or exec fault, we must have
  154. * appropriate permissions, otherwise we allow any permission.
  155. */
  156. if (!(vma->vm_flags & vm_flags)) {
  157. fault = VM_FAULT_BADACCESS;
  158. goto out;
  159. }
  160. return handle_mm_fault(mm, vma, addr & PAGE_MASK, mm_flags);
  161. check_stack:
  162. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  163. goto good_area;
  164. out:
  165. return fault;
  166. }
  167. static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
  168. struct pt_regs *regs)
  169. {
  170. struct task_struct *tsk;
  171. struct mm_struct *mm;
  172. int fault, sig, code;
  173. unsigned long vm_flags = VM_READ | VM_WRITE | VM_EXEC;
  174. unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  175. tsk = current;
  176. mm = tsk->mm;
  177. /* Enable interrupts if they were enabled in the parent context. */
  178. if (interrupts_enabled(regs))
  179. local_irq_enable();
  180. /*
  181. * If we're in an interrupt or have no user context, we must not take
  182. * the fault.
  183. */
  184. if (faulthandler_disabled() || !mm)
  185. goto no_context;
  186. if (user_mode(regs))
  187. mm_flags |= FAULT_FLAG_USER;
  188. if (esr & ESR_LNX_EXEC) {
  189. vm_flags = VM_EXEC;
  190. } else if ((esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM)) {
  191. vm_flags = VM_WRITE;
  192. mm_flags |= FAULT_FLAG_WRITE;
  193. }
  194. /*
  195. * PAN bit set implies the fault happened in kernel space, but not
  196. * in the arch's user access functions.
  197. */
  198. if (IS_ENABLED(CONFIG_ARM64_PAN) && (regs->pstate & PSR_PAN_BIT))
  199. goto no_context;
  200. /*
  201. * As per x86, we may deadlock here. However, since the kernel only
  202. * validly references user space from well defined areas of the code,
  203. * we can bug out early if this is from code which shouldn't.
  204. */
  205. if (!down_read_trylock(&mm->mmap_sem)) {
  206. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  207. goto no_context;
  208. retry:
  209. down_read(&mm->mmap_sem);
  210. } else {
  211. /*
  212. * The above down_read_trylock() might have succeeded in which
  213. * case, we'll have missed the might_sleep() from down_read().
  214. */
  215. might_sleep();
  216. #ifdef CONFIG_DEBUG_VM
  217. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  218. goto no_context;
  219. #endif
  220. }
  221. fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk);
  222. /*
  223. * If we need to retry but a fatal signal is pending, handle the
  224. * signal first. We do not need to release the mmap_sem because it
  225. * would already be released in __lock_page_or_retry in mm/filemap.c.
  226. */
  227. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  228. return 0;
  229. /*
  230. * Major/minor page fault accounting is only done on the initial
  231. * attempt. If we go through a retry, it is extremely likely that the
  232. * page will be found in page cache at that point.
  233. */
  234. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  235. if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
  236. if (fault & VM_FAULT_MAJOR) {
  237. tsk->maj_flt++;
  238. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
  239. addr);
  240. } else {
  241. tsk->min_flt++;
  242. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
  243. addr);
  244. }
  245. if (fault & VM_FAULT_RETRY) {
  246. /*
  247. * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
  248. * starvation.
  249. */
  250. mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
  251. mm_flags |= FAULT_FLAG_TRIED;
  252. goto retry;
  253. }
  254. }
  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 |
  260. VM_FAULT_BADACCESS))))
  261. return 0;
  262. /*
  263. * If we are in kernel mode at this point, we have no context to
  264. * handle this fault with.
  265. */
  266. if (!user_mode(regs))
  267. goto no_context;
  268. if (fault & VM_FAULT_OOM) {
  269. /*
  270. * We ran out of memory, call the OOM killer, and return to
  271. * userspace (which will retry the fault, or kill us if we got
  272. * oom-killed).
  273. */
  274. pagefault_out_of_memory();
  275. return 0;
  276. }
  277. if (fault & VM_FAULT_SIGBUS) {
  278. /*
  279. * We had some memory, but were unable to successfully fix up
  280. * this page fault.
  281. */
  282. sig = SIGBUS;
  283. code = BUS_ADRERR;
  284. } else {
  285. /*
  286. * Something tried to access memory that isn't in our memory
  287. * map.
  288. */
  289. sig = SIGSEGV;
  290. code = fault == VM_FAULT_BADACCESS ?
  291. SEGV_ACCERR : SEGV_MAPERR;
  292. }
  293. __do_user_fault(tsk, addr, esr, sig, code, regs);
  294. return 0;
  295. no_context:
  296. __do_kernel_fault(mm, addr, esr, regs);
  297. return 0;
  298. }
  299. /*
  300. * First Level Translation Fault Handler
  301. *
  302. * We enter here because the first level page table doesn't contain a valid
  303. * entry for the address.
  304. *
  305. * If the address is in kernel space (>= TASK_SIZE), then we are probably
  306. * faulting in the vmalloc() area.
  307. *
  308. * If the init_task's first level page tables contains the relevant entry, we
  309. * copy the it to this task. If not, we send the process a signal, fixup the
  310. * exception, or oops the kernel.
  311. *
  312. * NOTE! We MUST NOT take any locks for this case. We may be in an interrupt
  313. * or a critical region, and should only copy the information from the master
  314. * page table, nothing more.
  315. */
  316. static int __kprobes do_translation_fault(unsigned long addr,
  317. unsigned int esr,
  318. struct pt_regs *regs)
  319. {
  320. if (addr < TASK_SIZE)
  321. return do_page_fault(addr, esr, regs);
  322. do_bad_area(addr, esr, regs);
  323. return 0;
  324. }
  325. /*
  326. * This abort handler always returns "fault".
  327. */
  328. static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  329. {
  330. return 1;
  331. }
  332. static struct fault_info {
  333. int (*fn)(unsigned long addr, unsigned int esr, struct pt_regs *regs);
  334. int sig;
  335. int code;
  336. const char *name;
  337. } fault_info[] = {
  338. { do_bad, SIGBUS, 0, "ttbr address size fault" },
  339. { do_bad, SIGBUS, 0, "level 1 address size fault" },
  340. { do_bad, SIGBUS, 0, "level 2 address size fault" },
  341. { do_bad, SIGBUS, 0, "level 3 address size fault" },
  342. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" },
  343. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
  344. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
  345. { do_page_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
  346. { do_bad, SIGBUS, 0, "unknown 8" },
  347. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
  348. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
  349. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
  350. { do_bad, SIGBUS, 0, "unknown 12" },
  351. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
  352. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
  353. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
  354. { do_bad, SIGBUS, 0, "synchronous external abort" },
  355. { do_bad, SIGBUS, 0, "unknown 17" },
  356. { do_bad, SIGBUS, 0, "unknown 18" },
  357. { do_bad, SIGBUS, 0, "unknown 19" },
  358. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  359. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  360. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  361. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  362. { do_bad, SIGBUS, 0, "synchronous parity error" },
  363. { do_bad, SIGBUS, 0, "unknown 25" },
  364. { do_bad, SIGBUS, 0, "unknown 26" },
  365. { do_bad, SIGBUS, 0, "unknown 27" },
  366. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  367. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  368. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  369. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  370. { do_bad, SIGBUS, 0, "unknown 32" },
  371. { do_bad, SIGBUS, BUS_ADRALN, "alignment fault" },
  372. { do_bad, SIGBUS, 0, "unknown 34" },
  373. { do_bad, SIGBUS, 0, "unknown 35" },
  374. { do_bad, SIGBUS, 0, "unknown 36" },
  375. { do_bad, SIGBUS, 0, "unknown 37" },
  376. { do_bad, SIGBUS, 0, "unknown 38" },
  377. { do_bad, SIGBUS, 0, "unknown 39" },
  378. { do_bad, SIGBUS, 0, "unknown 40" },
  379. { do_bad, SIGBUS, 0, "unknown 41" },
  380. { do_bad, SIGBUS, 0, "unknown 42" },
  381. { do_bad, SIGBUS, 0, "unknown 43" },
  382. { do_bad, SIGBUS, 0, "unknown 44" },
  383. { do_bad, SIGBUS, 0, "unknown 45" },
  384. { do_bad, SIGBUS, 0, "unknown 46" },
  385. { do_bad, SIGBUS, 0, "unknown 47" },
  386. { do_bad, SIGBUS, 0, "TLB conflict abort" },
  387. { do_bad, SIGBUS, 0, "unknown 49" },
  388. { do_bad, SIGBUS, 0, "unknown 50" },
  389. { do_bad, SIGBUS, 0, "unknown 51" },
  390. { do_bad, SIGBUS, 0, "implementation fault (lockdown abort)" },
  391. { do_bad, SIGBUS, 0, "implementation fault (unsupported exclusive)" },
  392. { do_bad, SIGBUS, 0, "unknown 54" },
  393. { do_bad, SIGBUS, 0, "unknown 55" },
  394. { do_bad, SIGBUS, 0, "unknown 56" },
  395. { do_bad, SIGBUS, 0, "unknown 57" },
  396. { do_bad, SIGBUS, 0, "unknown 58" },
  397. { do_bad, SIGBUS, 0, "unknown 59" },
  398. { do_bad, SIGBUS, 0, "unknown 60" },
  399. { do_bad, SIGBUS, 0, "section domain fault" },
  400. { do_bad, SIGBUS, 0, "page domain fault" },
  401. { do_bad, SIGBUS, 0, "unknown 63" },
  402. };
  403. static const char *fault_name(unsigned int esr)
  404. {
  405. const struct fault_info *inf = fault_info + (esr & 63);
  406. return inf->name;
  407. }
  408. /*
  409. * Dispatch a data abort to the relevant handler.
  410. */
  411. asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
  412. struct pt_regs *regs)
  413. {
  414. const struct fault_info *inf = fault_info + (esr & 63);
  415. struct siginfo info;
  416. if (!inf->fn(addr, esr, regs))
  417. return;
  418. pr_alert("Unhandled fault: %s (0x%08x) at 0x%016lx\n",
  419. inf->name, esr, addr);
  420. info.si_signo = inf->sig;
  421. info.si_errno = 0;
  422. info.si_code = inf->code;
  423. info.si_addr = (void __user *)addr;
  424. arm64_notify_die("", regs, &info, esr);
  425. }
  426. /*
  427. * Handle stack alignment exceptions.
  428. */
  429. asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
  430. unsigned int esr,
  431. struct pt_regs *regs)
  432. {
  433. struct siginfo info;
  434. struct task_struct *tsk = current;
  435. if (show_unhandled_signals && unhandled_signal(tsk, SIGBUS))
  436. pr_info_ratelimited("%s[%d]: %s exception: pc=%p sp=%p\n",
  437. tsk->comm, task_pid_nr(tsk),
  438. esr_get_class_string(esr), (void *)regs->pc,
  439. (void *)regs->sp);
  440. info.si_signo = SIGBUS;
  441. info.si_errno = 0;
  442. info.si_code = BUS_ADRALN;
  443. info.si_addr = (void __user *)addr;
  444. arm64_notify_die("Oops - SP/PC alignment exception", regs, &info, esr);
  445. }
  446. int __init early_brk64(unsigned long addr, unsigned int esr,
  447. struct pt_regs *regs);
  448. /*
  449. * __refdata because early_brk64 is __init, but the reference to it is
  450. * clobbered at arch_initcall time.
  451. * See traps.c and debug-monitors.c:debug_traps_init().
  452. */
  453. static struct fault_info __refdata debug_fault_info[] = {
  454. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
  455. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
  456. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
  457. { do_bad, SIGBUS, 0, "unknown 3" },
  458. { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
  459. { do_bad, SIGTRAP, 0, "aarch32 vector catch" },
  460. { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
  461. { do_bad, SIGBUS, 0, "unknown 7" },
  462. };
  463. void __init hook_debug_fault_code(int nr,
  464. int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  465. int sig, int code, const char *name)
  466. {
  467. BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
  468. debug_fault_info[nr].fn = fn;
  469. debug_fault_info[nr].sig = sig;
  470. debug_fault_info[nr].code = code;
  471. debug_fault_info[nr].name = name;
  472. }
  473. asmlinkage int __exception do_debug_exception(unsigned long addr,
  474. unsigned int esr,
  475. struct pt_regs *regs)
  476. {
  477. const struct fault_info *inf = debug_fault_info + DBG_ESR_EVT(esr);
  478. struct siginfo info;
  479. if (!inf->fn(addr, esr, regs))
  480. return 1;
  481. pr_alert("Unhandled debug exception: %s (0x%08x) at 0x%016lx\n",
  482. inf->name, esr, addr);
  483. info.si_signo = inf->sig;
  484. info.si_errno = 0;
  485. info.si_code = inf->code;
  486. info.si_addr = (void __user *)addr;
  487. arm64_notify_die("", regs, &info, 0);
  488. return 0;
  489. }
  490. #ifdef CONFIG_ARM64_PAN
  491. void cpu_enable_pan(void *__unused)
  492. {
  493. config_sctlr_el1(SCTLR_EL1_SPAN, 0);
  494. }
  495. #endif /* CONFIG_ARM64_PAN */