fault.c 23 KB

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