fault.c 22 KB

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