fault.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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/extable.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/signal.h>
  29. #include <linux/sched/debug.h>
  30. #include <linux/highmem.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/preempt.h>
  33. #include <linux/hugetlb.h>
  34. #include <asm/bug.h>
  35. #include <asm/cmpxchg.h>
  36. #include <asm/cpufeature.h>
  37. #include <asm/exception.h>
  38. #include <asm/daifflags.h>
  39. #include <asm/debug-monitors.h>
  40. #include <asm/esr.h>
  41. #include <asm/sysreg.h>
  42. #include <asm/system_misc.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/tlbflush.h>
  45. #include <asm/traps.h>
  46. #include <acpi/ghes.h>
  47. struct fault_info {
  48. int (*fn)(unsigned long addr, unsigned int esr,
  49. struct pt_regs *regs);
  50. int sig;
  51. int code;
  52. const char *name;
  53. };
  54. static const struct fault_info fault_info[];
  55. static struct fault_info debug_fault_info[];
  56. static inline const struct fault_info *esr_to_fault_info(unsigned int esr)
  57. {
  58. return fault_info + (esr & ESR_ELx_FSC);
  59. }
  60. static inline const struct fault_info *esr_to_debug_fault_info(unsigned int esr)
  61. {
  62. return debug_fault_info + DBG_ESR_EVT(esr);
  63. }
  64. #ifdef CONFIG_KPROBES
  65. static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
  66. {
  67. int ret = 0;
  68. /* kprobe_running() needs smp_processor_id() */
  69. if (!user_mode(regs)) {
  70. preempt_disable();
  71. if (kprobe_running() && kprobe_fault_handler(regs, esr))
  72. ret = 1;
  73. preempt_enable();
  74. }
  75. return ret;
  76. }
  77. #else
  78. static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
  79. {
  80. return 0;
  81. }
  82. #endif
  83. static void data_abort_decode(unsigned int esr)
  84. {
  85. pr_alert("Data abort info:\n");
  86. if (esr & ESR_ELx_ISV) {
  87. pr_alert(" Access size = %u byte(s)\n",
  88. 1U << ((esr & ESR_ELx_SAS) >> ESR_ELx_SAS_SHIFT));
  89. pr_alert(" SSE = %lu, SRT = %lu\n",
  90. (esr & ESR_ELx_SSE) >> ESR_ELx_SSE_SHIFT,
  91. (esr & ESR_ELx_SRT_MASK) >> ESR_ELx_SRT_SHIFT);
  92. pr_alert(" SF = %lu, AR = %lu\n",
  93. (esr & ESR_ELx_SF) >> ESR_ELx_SF_SHIFT,
  94. (esr & ESR_ELx_AR) >> ESR_ELx_AR_SHIFT);
  95. } else {
  96. pr_alert(" ISV = 0, ISS = 0x%08lx\n", esr & ESR_ELx_ISS_MASK);
  97. }
  98. pr_alert(" CM = %lu, WnR = %lu\n",
  99. (esr & ESR_ELx_CM) >> ESR_ELx_CM_SHIFT,
  100. (esr & ESR_ELx_WNR) >> ESR_ELx_WNR_SHIFT);
  101. }
  102. static void mem_abort_decode(unsigned int esr)
  103. {
  104. pr_alert("Mem abort info:\n");
  105. pr_alert(" ESR = 0x%08x\n", esr);
  106. pr_alert(" Exception class = %s, IL = %u bits\n",
  107. esr_get_class_string(esr),
  108. (esr & ESR_ELx_IL) ? 32 : 16);
  109. pr_alert(" SET = %lu, FnV = %lu\n",
  110. (esr & ESR_ELx_SET_MASK) >> ESR_ELx_SET_SHIFT,
  111. (esr & ESR_ELx_FnV) >> ESR_ELx_FnV_SHIFT);
  112. pr_alert(" EA = %lu, S1PTW = %lu\n",
  113. (esr & ESR_ELx_EA) >> ESR_ELx_EA_SHIFT,
  114. (esr & ESR_ELx_S1PTW) >> ESR_ELx_S1PTW_SHIFT);
  115. if (esr_is_data_abort(esr))
  116. data_abort_decode(esr);
  117. }
  118. /*
  119. * Dump out the page tables associated with 'addr' in the currently active mm.
  120. */
  121. void show_pte(unsigned long addr)
  122. {
  123. struct mm_struct *mm;
  124. pgd_t *pgdp;
  125. pgd_t pgd;
  126. if (addr < TASK_SIZE) {
  127. /* TTBR0 */
  128. mm = current->active_mm;
  129. if (mm == &init_mm) {
  130. pr_alert("[%016lx] user address but active_mm is swapper\n",
  131. addr);
  132. return;
  133. }
  134. } else if (addr >= VA_START) {
  135. /* TTBR1 */
  136. mm = &init_mm;
  137. } else {
  138. pr_alert("[%016lx] address between user and kernel address ranges\n",
  139. addr);
  140. return;
  141. }
  142. pr_alert("%s pgtable: %luk pages, %u-bit VAs, pgdp = %p\n",
  143. mm == &init_mm ? "swapper" : "user", PAGE_SIZE / SZ_1K,
  144. VA_BITS, mm->pgd);
  145. pgdp = pgd_offset(mm, addr);
  146. pgd = READ_ONCE(*pgdp);
  147. pr_alert("[%016lx] pgd=%016llx", addr, pgd_val(pgd));
  148. do {
  149. pud_t *pudp, pud;
  150. pmd_t *pmdp, pmd;
  151. pte_t *ptep, pte;
  152. if (pgd_none(pgd) || pgd_bad(pgd))
  153. break;
  154. pudp = pud_offset(pgdp, addr);
  155. pud = READ_ONCE(*pudp);
  156. pr_cont(", pud=%016llx", pud_val(pud));
  157. if (pud_none(pud) || pud_bad(pud))
  158. break;
  159. pmdp = pmd_offset(pudp, addr);
  160. pmd = READ_ONCE(*pmdp);
  161. pr_cont(", pmd=%016llx", pmd_val(pmd));
  162. if (pmd_none(pmd) || pmd_bad(pmd))
  163. break;
  164. ptep = pte_offset_map(pmdp, addr);
  165. pte = READ_ONCE(*ptep);
  166. pr_cont(", pte=%016llx", pte_val(pte));
  167. pte_unmap(ptep);
  168. } while(0);
  169. pr_cont("\n");
  170. }
  171. /*
  172. * This function sets the access flags (dirty, accessed), as well as write
  173. * permission, and only to a more permissive setting.
  174. *
  175. * It needs to cope with hardware update of the accessed/dirty state by other
  176. * agents in the system and can safely skip the __sync_icache_dcache() call as,
  177. * like set_pte_at(), the PTE is never changed from no-exec to exec here.
  178. *
  179. * Returns whether or not the PTE actually changed.
  180. */
  181. int ptep_set_access_flags(struct vm_area_struct *vma,
  182. unsigned long address, pte_t *ptep,
  183. pte_t entry, int dirty)
  184. {
  185. pteval_t old_pteval, pteval;
  186. pte_t pte = READ_ONCE(*ptep);
  187. if (pte_same(pte, entry))
  188. return 0;
  189. /* only preserve the access flags and write permission */
  190. pte_val(entry) &= PTE_RDONLY | PTE_AF | PTE_WRITE | PTE_DIRTY;
  191. /*
  192. * Setting the flags must be done atomically to avoid racing with the
  193. * hardware update of the access/dirty state. The PTE_RDONLY bit must
  194. * be set to the most permissive (lowest value) of *ptep and entry
  195. * (calculated as: a & b == ~(~a | ~b)).
  196. */
  197. pte_val(entry) ^= PTE_RDONLY;
  198. pteval = pte_val(pte);
  199. do {
  200. old_pteval = pteval;
  201. pteval ^= PTE_RDONLY;
  202. pteval |= pte_val(entry);
  203. pteval ^= PTE_RDONLY;
  204. pteval = cmpxchg_relaxed(&pte_val(*ptep), old_pteval, pteval);
  205. } while (pteval != old_pteval);
  206. flush_tlb_fix_spurious_fault(vma, address);
  207. return 1;
  208. }
  209. static bool is_el1_instruction_abort(unsigned int esr)
  210. {
  211. return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_CUR;
  212. }
  213. static inline bool is_el1_permission_fault(unsigned long addr, unsigned int esr,
  214. struct pt_regs *regs)
  215. {
  216. unsigned int ec = ESR_ELx_EC(esr);
  217. unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;
  218. if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
  219. return false;
  220. if (fsc_type == ESR_ELx_FSC_PERM)
  221. return true;
  222. if (addr < TASK_SIZE && system_uses_ttbr0_pan())
  223. return fsc_type == ESR_ELx_FSC_FAULT &&
  224. (regs->pstate & PSR_PAN_BIT);
  225. return false;
  226. }
  227. static void die_kernel_fault(const char *msg, unsigned long addr,
  228. unsigned int esr, struct pt_regs *regs)
  229. {
  230. bust_spinlocks(1);
  231. pr_alert("Unable to handle kernel %s at virtual address %016lx\n", msg,
  232. addr);
  233. mem_abort_decode(esr);
  234. show_pte(addr);
  235. die("Oops", regs, esr);
  236. bust_spinlocks(0);
  237. do_exit(SIGKILL);
  238. }
  239. static void __do_kernel_fault(unsigned long addr, unsigned int esr,
  240. struct pt_regs *regs)
  241. {
  242. const char *msg;
  243. /*
  244. * Are we prepared to handle this kernel fault?
  245. * We are almost certainly not prepared to handle instruction faults.
  246. */
  247. if (!is_el1_instruction_abort(esr) && fixup_exception(regs))
  248. return;
  249. if (is_el1_permission_fault(addr, esr, regs)) {
  250. if (esr & ESR_ELx_WNR)
  251. msg = "write to read-only memory";
  252. else
  253. msg = "read from unreadable memory";
  254. } else if (addr < PAGE_SIZE) {
  255. msg = "NULL pointer dereference";
  256. } else {
  257. msg = "paging request";
  258. }
  259. die_kernel_fault(msg, addr, esr, regs);
  260. }
  261. static void set_thread_esr(unsigned long address, unsigned int esr)
  262. {
  263. current->thread.fault_address = address;
  264. /*
  265. * If the faulting address is in the kernel, we must sanitize the ESR.
  266. * From userspace's point of view, kernel-only mappings don't exist
  267. * at all, so we report them as level 0 translation faults.
  268. * (This is not quite the way that "no mapping there at all" behaves:
  269. * an alignment fault not caused by the memory type would take
  270. * precedence over translation fault for a real access to empty
  271. * space. Unfortunately we can't easily distinguish "alignment fault
  272. * not caused by memory type" from "alignment fault caused by memory
  273. * type", so we ignore this wrinkle and just return the translation
  274. * fault.)
  275. */
  276. if (current->thread.fault_address >= TASK_SIZE) {
  277. switch (ESR_ELx_EC(esr)) {
  278. case ESR_ELx_EC_DABT_LOW:
  279. /*
  280. * These bits provide only information about the
  281. * faulting instruction, which userspace knows already.
  282. * We explicitly clear bits which are architecturally
  283. * RES0 in case they are given meanings in future.
  284. * We always report the ESR as if the fault was taken
  285. * to EL1 and so ISV and the bits in ISS[23:14] are
  286. * clear. (In fact it always will be a fault to EL1.)
  287. */
  288. esr &= ESR_ELx_EC_MASK | ESR_ELx_IL |
  289. ESR_ELx_CM | ESR_ELx_WNR;
  290. esr |= ESR_ELx_FSC_FAULT;
  291. break;
  292. case ESR_ELx_EC_IABT_LOW:
  293. /*
  294. * Claim a level 0 translation fault.
  295. * All other bits are architecturally RES0 for faults
  296. * reported with that DFSC value, so we clear them.
  297. */
  298. esr &= ESR_ELx_EC_MASK | ESR_ELx_IL;
  299. esr |= ESR_ELx_FSC_FAULT;
  300. break;
  301. default:
  302. /*
  303. * This should never happen (entry.S only brings us
  304. * into this code for insn and data aborts from a lower
  305. * exception level). Fail safe by not providing an ESR
  306. * context record at all.
  307. */
  308. WARN(1, "ESR 0x%x is not DABT or IABT from EL0\n", esr);
  309. esr = 0;
  310. break;
  311. }
  312. }
  313. current->thread.fault_code = esr;
  314. }
  315. static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  316. {
  317. /*
  318. * If we are in kernel mode at this point, we have no context to
  319. * handle this fault with.
  320. */
  321. if (user_mode(regs)) {
  322. const struct fault_info *inf = esr_to_fault_info(esr);
  323. set_thread_esr(addr, esr);
  324. arm64_force_sig_fault(inf->sig, inf->code, (void __user *)addr,
  325. inf->name);
  326. } else {
  327. __do_kernel_fault(addr, esr, regs);
  328. }
  329. }
  330. #define VM_FAULT_BADMAP 0x010000
  331. #define VM_FAULT_BADACCESS 0x020000
  332. static vm_fault_t __do_page_fault(struct mm_struct *mm, unsigned long addr,
  333. unsigned int mm_flags, unsigned long vm_flags,
  334. struct task_struct *tsk)
  335. {
  336. struct vm_area_struct *vma;
  337. vm_fault_t fault;
  338. vma = find_vma(mm, addr);
  339. fault = VM_FAULT_BADMAP;
  340. if (unlikely(!vma))
  341. goto out;
  342. if (unlikely(vma->vm_start > addr))
  343. goto check_stack;
  344. /*
  345. * Ok, we have a good vm_area for this memory access, so we can handle
  346. * it.
  347. */
  348. good_area:
  349. /*
  350. * Check that the permissions on the VMA allow for the fault which
  351. * occurred.
  352. */
  353. if (!(vma->vm_flags & vm_flags)) {
  354. fault = VM_FAULT_BADACCESS;
  355. goto out;
  356. }
  357. return handle_mm_fault(vma, addr & PAGE_MASK, mm_flags);
  358. check_stack:
  359. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  360. goto good_area;
  361. out:
  362. return fault;
  363. }
  364. static bool is_el0_instruction_abort(unsigned int esr)
  365. {
  366. return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW;
  367. }
  368. static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
  369. struct pt_regs *regs)
  370. {
  371. const struct fault_info *inf;
  372. struct task_struct *tsk;
  373. struct mm_struct *mm;
  374. vm_fault_t fault, major = 0;
  375. unsigned long vm_flags = VM_READ | VM_WRITE;
  376. unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  377. if (notify_page_fault(regs, esr))
  378. return 0;
  379. tsk = current;
  380. mm = tsk->mm;
  381. /*
  382. * If we're in an interrupt or have no user context, we must not take
  383. * the fault.
  384. */
  385. if (faulthandler_disabled() || !mm)
  386. goto no_context;
  387. if (user_mode(regs))
  388. mm_flags |= FAULT_FLAG_USER;
  389. if (is_el0_instruction_abort(esr)) {
  390. vm_flags = VM_EXEC;
  391. } else if ((esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM)) {
  392. vm_flags = VM_WRITE;
  393. mm_flags |= FAULT_FLAG_WRITE;
  394. }
  395. if (addr < TASK_SIZE && is_el1_permission_fault(addr, esr, regs)) {
  396. /* regs->orig_addr_limit may be 0 if we entered from EL0 */
  397. if (regs->orig_addr_limit == KERNEL_DS)
  398. die_kernel_fault("access to user memory with fs=KERNEL_DS",
  399. addr, esr, regs);
  400. if (is_el1_instruction_abort(esr))
  401. die_kernel_fault("execution of user memory",
  402. addr, esr, regs);
  403. if (!search_exception_tables(regs->pc))
  404. die_kernel_fault("access to user memory outside uaccess routines",
  405. addr, esr, regs);
  406. }
  407. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  408. /*
  409. * As per x86, we may deadlock here. However, since the kernel only
  410. * validly references user space from well defined areas of the code,
  411. * we can bug out early if this is from code which shouldn't.
  412. */
  413. if (!down_read_trylock(&mm->mmap_sem)) {
  414. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  415. goto no_context;
  416. retry:
  417. down_read(&mm->mmap_sem);
  418. } else {
  419. /*
  420. * The above down_read_trylock() might have succeeded in which
  421. * case, we'll have missed the might_sleep() from down_read().
  422. */
  423. might_sleep();
  424. #ifdef CONFIG_DEBUG_VM
  425. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  426. goto no_context;
  427. #endif
  428. }
  429. fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk);
  430. major |= fault & VM_FAULT_MAJOR;
  431. if (fault & VM_FAULT_RETRY) {
  432. /*
  433. * If we need to retry but a fatal signal is pending,
  434. * handle the signal first. We do not need to release
  435. * the mmap_sem because it would already be released
  436. * in __lock_page_or_retry in mm/filemap.c.
  437. */
  438. if (fatal_signal_pending(current)) {
  439. if (!user_mode(regs))
  440. goto no_context;
  441. return 0;
  442. }
  443. /*
  444. * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
  445. * starvation.
  446. */
  447. if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
  448. mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
  449. mm_flags |= FAULT_FLAG_TRIED;
  450. goto retry;
  451. }
  452. }
  453. up_read(&mm->mmap_sem);
  454. /*
  455. * Handle the "normal" (no error) case first.
  456. */
  457. if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
  458. VM_FAULT_BADACCESS)))) {
  459. /*
  460. * Major/minor page fault accounting is only done
  461. * once. If we go through a retry, it is extremely
  462. * likely that the page will be found in page cache at
  463. * that point.
  464. */
  465. if (major) {
  466. tsk->maj_flt++;
  467. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
  468. addr);
  469. } else {
  470. tsk->min_flt++;
  471. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
  472. addr);
  473. }
  474. return 0;
  475. }
  476. /*
  477. * If we are in kernel mode at this point, we have no context to
  478. * handle this fault with.
  479. */
  480. if (!user_mode(regs))
  481. goto no_context;
  482. if (fault & VM_FAULT_OOM) {
  483. /*
  484. * We ran out of memory, call the OOM killer, and return to
  485. * userspace (which will retry the fault, or kill us if we got
  486. * oom-killed).
  487. */
  488. pagefault_out_of_memory();
  489. return 0;
  490. }
  491. inf = esr_to_fault_info(esr);
  492. set_thread_esr(addr, esr);
  493. if (fault & VM_FAULT_SIGBUS) {
  494. /*
  495. * We had some memory, but were unable to successfully fix up
  496. * this page fault.
  497. */
  498. arm64_force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)addr,
  499. inf->name);
  500. } else if (fault & (VM_FAULT_HWPOISON_LARGE | VM_FAULT_HWPOISON)) {
  501. unsigned int lsb;
  502. lsb = PAGE_SHIFT;
  503. if (fault & VM_FAULT_HWPOISON_LARGE)
  504. lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
  505. arm64_force_sig_mceerr(BUS_MCEERR_AR, (void __user *)addr, lsb,
  506. inf->name);
  507. } else {
  508. /*
  509. * Something tried to access memory that isn't in our memory
  510. * map.
  511. */
  512. arm64_force_sig_fault(SIGSEGV,
  513. fault == VM_FAULT_BADACCESS ? SEGV_ACCERR : SEGV_MAPERR,
  514. (void __user *)addr,
  515. inf->name);
  516. }
  517. return 0;
  518. no_context:
  519. __do_kernel_fault(addr, esr, regs);
  520. return 0;
  521. }
  522. static int __kprobes do_translation_fault(unsigned long addr,
  523. unsigned int esr,
  524. struct pt_regs *regs)
  525. {
  526. if (addr < TASK_SIZE)
  527. return do_page_fault(addr, esr, regs);
  528. do_bad_area(addr, esr, regs);
  529. return 0;
  530. }
  531. static int do_alignment_fault(unsigned long addr, unsigned int esr,
  532. struct pt_regs *regs)
  533. {
  534. do_bad_area(addr, esr, regs);
  535. return 0;
  536. }
  537. static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  538. {
  539. return 1; /* "fault" */
  540. }
  541. static int do_sea(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  542. {
  543. const struct fault_info *inf;
  544. void __user *siaddr;
  545. inf = esr_to_fault_info(esr);
  546. /*
  547. * Synchronous aborts may interrupt code which had interrupts masked.
  548. * Before calling out into the wider kernel tell the interested
  549. * subsystems.
  550. */
  551. if (IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
  552. if (interrupts_enabled(regs))
  553. nmi_enter();
  554. ghes_notify_sea();
  555. if (interrupts_enabled(regs))
  556. nmi_exit();
  557. }
  558. if (esr & ESR_ELx_FnV)
  559. siaddr = NULL;
  560. else
  561. siaddr = (void __user *)addr;
  562. arm64_notify_die(inf->name, regs, inf->sig, inf->code, siaddr, esr);
  563. return 0;
  564. }
  565. static const struct fault_info fault_info[] = {
  566. { do_bad, SIGKILL, SI_KERNEL, "ttbr address size fault" },
  567. { do_bad, SIGKILL, SI_KERNEL, "level 1 address size fault" },
  568. { do_bad, SIGKILL, SI_KERNEL, "level 2 address size fault" },
  569. { do_bad, SIGKILL, SI_KERNEL, "level 3 address size fault" },
  570. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" },
  571. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
  572. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
  573. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
  574. { do_bad, SIGKILL, SI_KERNEL, "unknown 8" },
  575. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
  576. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
  577. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
  578. { do_bad, SIGKILL, SI_KERNEL, "unknown 12" },
  579. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
  580. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
  581. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
  582. { do_sea, SIGBUS, BUS_OBJERR, "synchronous external abort" },
  583. { do_bad, SIGKILL, SI_KERNEL, "unknown 17" },
  584. { do_bad, SIGKILL, SI_KERNEL, "unknown 18" },
  585. { do_bad, SIGKILL, SI_KERNEL, "unknown 19" },
  586. { do_sea, SIGKILL, SI_KERNEL, "level 0 (translation table walk)" },
  587. { do_sea, SIGKILL, SI_KERNEL, "level 1 (translation table walk)" },
  588. { do_sea, SIGKILL, SI_KERNEL, "level 2 (translation table walk)" },
  589. { do_sea, SIGKILL, SI_KERNEL, "level 3 (translation table walk)" },
  590. { do_sea, SIGBUS, BUS_OBJERR, "synchronous parity or ECC error" }, // Reserved when RAS is implemented
  591. { do_bad, SIGKILL, SI_KERNEL, "unknown 25" },
  592. { do_bad, SIGKILL, SI_KERNEL, "unknown 26" },
  593. { do_bad, SIGKILL, SI_KERNEL, "unknown 27" },
  594. { do_sea, SIGKILL, SI_KERNEL, "level 0 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
  595. { do_sea, SIGKILL, SI_KERNEL, "level 1 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
  596. { do_sea, SIGKILL, SI_KERNEL, "level 2 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
  597. { do_sea, SIGKILL, SI_KERNEL, "level 3 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
  598. { do_bad, SIGKILL, SI_KERNEL, "unknown 32" },
  599. { do_alignment_fault, SIGBUS, BUS_ADRALN, "alignment fault" },
  600. { do_bad, SIGKILL, SI_KERNEL, "unknown 34" },
  601. { do_bad, SIGKILL, SI_KERNEL, "unknown 35" },
  602. { do_bad, SIGKILL, SI_KERNEL, "unknown 36" },
  603. { do_bad, SIGKILL, SI_KERNEL, "unknown 37" },
  604. { do_bad, SIGKILL, SI_KERNEL, "unknown 38" },
  605. { do_bad, SIGKILL, SI_KERNEL, "unknown 39" },
  606. { do_bad, SIGKILL, SI_KERNEL, "unknown 40" },
  607. { do_bad, SIGKILL, SI_KERNEL, "unknown 41" },
  608. { do_bad, SIGKILL, SI_KERNEL, "unknown 42" },
  609. { do_bad, SIGKILL, SI_KERNEL, "unknown 43" },
  610. { do_bad, SIGKILL, SI_KERNEL, "unknown 44" },
  611. { do_bad, SIGKILL, SI_KERNEL, "unknown 45" },
  612. { do_bad, SIGKILL, SI_KERNEL, "unknown 46" },
  613. { do_bad, SIGKILL, SI_KERNEL, "unknown 47" },
  614. { do_bad, SIGKILL, SI_KERNEL, "TLB conflict abort" },
  615. { do_bad, SIGKILL, SI_KERNEL, "Unsupported atomic hardware update fault" },
  616. { do_bad, SIGKILL, SI_KERNEL, "unknown 50" },
  617. { do_bad, SIGKILL, SI_KERNEL, "unknown 51" },
  618. { do_bad, SIGKILL, SI_KERNEL, "implementation fault (lockdown abort)" },
  619. { do_bad, SIGBUS, BUS_OBJERR, "implementation fault (unsupported exclusive)" },
  620. { do_bad, SIGKILL, SI_KERNEL, "unknown 54" },
  621. { do_bad, SIGKILL, SI_KERNEL, "unknown 55" },
  622. { do_bad, SIGKILL, SI_KERNEL, "unknown 56" },
  623. { do_bad, SIGKILL, SI_KERNEL, "unknown 57" },
  624. { do_bad, SIGKILL, SI_KERNEL, "unknown 58" },
  625. { do_bad, SIGKILL, SI_KERNEL, "unknown 59" },
  626. { do_bad, SIGKILL, SI_KERNEL, "unknown 60" },
  627. { do_bad, SIGKILL, SI_KERNEL, "section domain fault" },
  628. { do_bad, SIGKILL, SI_KERNEL, "page domain fault" },
  629. { do_bad, SIGKILL, SI_KERNEL, "unknown 63" },
  630. };
  631. int handle_guest_sea(phys_addr_t addr, unsigned int esr)
  632. {
  633. return ghes_notify_sea();
  634. }
  635. asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
  636. struct pt_regs *regs)
  637. {
  638. const struct fault_info *inf = esr_to_fault_info(esr);
  639. if (!inf->fn(addr, esr, regs))
  640. return;
  641. if (!user_mode(regs)) {
  642. pr_alert("Unhandled fault at 0x%016lx\n", addr);
  643. mem_abort_decode(esr);
  644. show_pte(addr);
  645. }
  646. arm64_notify_die(inf->name, regs,
  647. inf->sig, inf->code, (void __user *)addr, esr);
  648. }
  649. asmlinkage void __exception do_el0_irq_bp_hardening(void)
  650. {
  651. /* PC has already been checked in entry.S */
  652. arm64_apply_bp_hardening();
  653. }
  654. asmlinkage void __exception do_el0_ia_bp_hardening(unsigned long addr,
  655. unsigned int esr,
  656. struct pt_regs *regs)
  657. {
  658. /*
  659. * We've taken an instruction abort from userspace and not yet
  660. * re-enabled IRQs. If the address is a kernel address, apply
  661. * BP hardening prior to enabling IRQs and pre-emption.
  662. */
  663. if (addr > TASK_SIZE)
  664. arm64_apply_bp_hardening();
  665. local_daif_restore(DAIF_PROCCTX);
  666. do_mem_abort(addr, esr, regs);
  667. }
  668. asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
  669. unsigned int esr,
  670. struct pt_regs *regs)
  671. {
  672. if (user_mode(regs)) {
  673. if (instruction_pointer(regs) > TASK_SIZE)
  674. arm64_apply_bp_hardening();
  675. local_daif_restore(DAIF_PROCCTX);
  676. }
  677. arm64_notify_die("SP/PC alignment exception", regs,
  678. SIGBUS, BUS_ADRALN, (void __user *)addr, esr);
  679. }
  680. int __init early_brk64(unsigned long addr, unsigned int esr,
  681. struct pt_regs *regs);
  682. /*
  683. * __refdata because early_brk64 is __init, but the reference to it is
  684. * clobbered at arch_initcall time.
  685. * See traps.c and debug-monitors.c:debug_traps_init().
  686. */
  687. static struct fault_info __refdata debug_fault_info[] = {
  688. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
  689. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
  690. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
  691. { do_bad, SIGKILL, SI_KERNEL, "unknown 3" },
  692. { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
  693. { do_bad, SIGKILL, SI_KERNEL, "aarch32 vector catch" },
  694. { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
  695. { do_bad, SIGKILL, SI_KERNEL, "unknown 7" },
  696. };
  697. void __init hook_debug_fault_code(int nr,
  698. int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  699. int sig, int code, const char *name)
  700. {
  701. BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
  702. debug_fault_info[nr].fn = fn;
  703. debug_fault_info[nr].sig = sig;
  704. debug_fault_info[nr].code = code;
  705. debug_fault_info[nr].name = name;
  706. }
  707. asmlinkage int __exception do_debug_exception(unsigned long addr,
  708. unsigned int esr,
  709. struct pt_regs *regs)
  710. {
  711. const struct fault_info *inf = esr_to_debug_fault_info(esr);
  712. int rv;
  713. /*
  714. * Tell lockdep we disabled irqs in entry.S. Do nothing if they were
  715. * already disabled to preserve the last enabled/disabled addresses.
  716. */
  717. if (interrupts_enabled(regs))
  718. trace_hardirqs_off();
  719. if (user_mode(regs) && instruction_pointer(regs) > TASK_SIZE)
  720. arm64_apply_bp_hardening();
  721. if (!inf->fn(addr, esr, regs)) {
  722. rv = 1;
  723. } else {
  724. arm64_notify_die(inf->name, regs,
  725. inf->sig, inf->code, (void __user *)addr, esr);
  726. rv = 0;
  727. }
  728. if (interrupts_enabled(regs))
  729. trace_hardirqs_on();
  730. return rv;
  731. }
  732. NOKPROBE_SYMBOL(do_debug_exception);