fault.c 24 KB

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