fault.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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_el1_permission_fault(unsigned int esr,
  208. struct pt_regs *regs,
  209. unsigned long addr)
  210. {
  211. unsigned int ec = ESR_ELx_EC(esr);
  212. unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;
  213. if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
  214. return false;
  215. if (fsc_type == ESR_ELx_FSC_PERM)
  216. return true;
  217. if (addr < TASK_SIZE && system_uses_ttbr0_pan())
  218. return fsc_type == ESR_ELx_FSC_FAULT &&
  219. (regs->pstate & PSR_PAN_BIT);
  220. return false;
  221. }
  222. static void die_kernel_fault(const char *msg, unsigned long addr,
  223. unsigned int esr, struct pt_regs *regs)
  224. {
  225. bust_spinlocks(1);
  226. pr_alert("Unable to handle kernel %s at virtual address %016lx\n", msg,
  227. addr);
  228. mem_abort_decode(esr);
  229. show_pte(addr);
  230. die("Oops", regs, esr);
  231. bust_spinlocks(0);
  232. do_exit(SIGKILL);
  233. }
  234. static void __do_kernel_fault(unsigned long addr, unsigned int esr,
  235. struct pt_regs *regs)
  236. {
  237. const char *msg;
  238. /*
  239. * Are we prepared to handle this kernel fault?
  240. * We are almost certainly not prepared to handle instruction faults.
  241. */
  242. if (!is_el1_instruction_abort(esr) && fixup_exception(regs))
  243. return;
  244. if (is_el1_permission_fault(esr, regs, addr)) {
  245. if (esr & ESR_ELx_WNR)
  246. msg = "write to read-only memory";
  247. else
  248. msg = "read from unreadable memory";
  249. } else if (addr < PAGE_SIZE) {
  250. msg = "NULL pointer dereference";
  251. } else {
  252. msg = "paging request";
  253. }
  254. die_kernel_fault(msg, addr, esr, regs);
  255. }
  256. static void __do_user_fault(struct siginfo *info, unsigned int esr)
  257. {
  258. current->thread.fault_address = (unsigned long)info->si_addr;
  259. /*
  260. * If the faulting address is in the kernel, we must sanitize the ESR.
  261. * From userspace's point of view, kernel-only mappings don't exist
  262. * at all, so we report them as level 0 translation faults.
  263. * (This is not quite the way that "no mapping there at all" behaves:
  264. * an alignment fault not caused by the memory type would take
  265. * precedence over translation fault for a real access to empty
  266. * space. Unfortunately we can't easily distinguish "alignment fault
  267. * not caused by memory type" from "alignment fault caused by memory
  268. * type", so we ignore this wrinkle and just return the translation
  269. * fault.)
  270. */
  271. if (current->thread.fault_address >= TASK_SIZE) {
  272. switch (ESR_ELx_EC(esr)) {
  273. case ESR_ELx_EC_DABT_LOW:
  274. /*
  275. * These bits provide only information about the
  276. * faulting instruction, which userspace knows already.
  277. * We explicitly clear bits which are architecturally
  278. * RES0 in case they are given meanings in future.
  279. * We always report the ESR as if the fault was taken
  280. * to EL1 and so ISV and the bits in ISS[23:14] are
  281. * clear. (In fact it always will be a fault to EL1.)
  282. */
  283. esr &= ESR_ELx_EC_MASK | ESR_ELx_IL |
  284. ESR_ELx_CM | ESR_ELx_WNR;
  285. esr |= ESR_ELx_FSC_FAULT;
  286. break;
  287. case ESR_ELx_EC_IABT_LOW:
  288. /*
  289. * Claim a level 0 translation fault.
  290. * All other bits are architecturally RES0 for faults
  291. * reported with that DFSC value, so we clear them.
  292. */
  293. esr &= ESR_ELx_EC_MASK | ESR_ELx_IL;
  294. esr |= ESR_ELx_FSC_FAULT;
  295. break;
  296. default:
  297. /*
  298. * This should never happen (entry.S only brings us
  299. * into this code for insn and data aborts from a lower
  300. * exception level). Fail safe by not providing an ESR
  301. * context record at all.
  302. */
  303. WARN(1, "ESR 0x%x is not DABT or IABT from EL0\n", esr);
  304. esr = 0;
  305. break;
  306. }
  307. }
  308. current->thread.fault_code = esr;
  309. arm64_force_sig_info(info, esr_to_fault_info(esr)->name, current);
  310. }
  311. static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  312. {
  313. /*
  314. * If we are in kernel mode at this point, we have no context to
  315. * handle this fault with.
  316. */
  317. if (user_mode(regs)) {
  318. const struct fault_info *inf = esr_to_fault_info(esr);
  319. struct siginfo si;
  320. clear_siginfo(&si);
  321. si.si_signo = inf->sig;
  322. si.si_code = inf->code;
  323. si.si_addr = (void __user *)addr;
  324. __do_user_fault(&si, esr);
  325. } else {
  326. __do_kernel_fault(addr, esr, regs);
  327. }
  328. }
  329. #define VM_FAULT_BADMAP 0x010000
  330. #define VM_FAULT_BADACCESS 0x020000
  331. static vm_fault_t __do_page_fault(struct mm_struct *mm, unsigned long addr,
  332. unsigned int mm_flags, unsigned long vm_flags,
  333. struct task_struct *tsk)
  334. {
  335. struct vm_area_struct *vma;
  336. vm_fault_t fault;
  337. vma = find_vma(mm, addr);
  338. fault = VM_FAULT_BADMAP;
  339. if (unlikely(!vma))
  340. goto out;
  341. if (unlikely(vma->vm_start > addr))
  342. goto check_stack;
  343. /*
  344. * Ok, we have a good vm_area for this memory access, so we can handle
  345. * it.
  346. */
  347. good_area:
  348. /*
  349. * Check that the permissions on the VMA allow for the fault which
  350. * occurred.
  351. */
  352. if (!(vma->vm_flags & vm_flags)) {
  353. fault = VM_FAULT_BADACCESS;
  354. goto out;
  355. }
  356. return handle_mm_fault(vma, addr & PAGE_MASK, mm_flags);
  357. check_stack:
  358. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  359. goto good_area;
  360. out:
  361. return fault;
  362. }
  363. static bool is_el0_instruction_abort(unsigned int esr)
  364. {
  365. return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW;
  366. }
  367. static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
  368. struct pt_regs *regs)
  369. {
  370. struct task_struct *tsk;
  371. struct mm_struct *mm;
  372. struct siginfo si;
  373. vm_fault_t fault, major = 0;
  374. unsigned long vm_flags = VM_READ | VM_WRITE | VM_EXEC;
  375. unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  376. if (notify_page_fault(regs, esr))
  377. return 0;
  378. tsk = current;
  379. mm = tsk->mm;
  380. /*
  381. * If we're in an interrupt or have no user context, we must not take
  382. * the fault.
  383. */
  384. if (faulthandler_disabled() || !mm)
  385. goto no_context;
  386. if (user_mode(regs))
  387. mm_flags |= FAULT_FLAG_USER;
  388. if (is_el0_instruction_abort(esr)) {
  389. vm_flags = VM_EXEC;
  390. } else if ((esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM)) {
  391. vm_flags = VM_WRITE;
  392. mm_flags |= FAULT_FLAG_WRITE;
  393. }
  394. if (addr < TASK_SIZE && is_el1_permission_fault(esr, regs, addr)) {
  395. /* regs->orig_addr_limit may be 0 if we entered from EL0 */
  396. if (regs->orig_addr_limit == KERNEL_DS)
  397. die_kernel_fault("access to user memory with fs=KERNEL_DS",
  398. addr, esr, regs);
  399. if (is_el1_instruction_abort(esr))
  400. die_kernel_fault("execution of user memory",
  401. addr, esr, regs);
  402. if (!search_exception_tables(regs->pc))
  403. die_kernel_fault("access to user memory outside uaccess routines",
  404. addr, esr, regs);
  405. }
  406. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  407. /*
  408. * As per x86, we may deadlock here. However, since the kernel only
  409. * validly references user space from well defined areas of the code,
  410. * we can bug out early if this is from code which shouldn't.
  411. */
  412. if (!down_read_trylock(&mm->mmap_sem)) {
  413. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  414. goto no_context;
  415. retry:
  416. down_read(&mm->mmap_sem);
  417. } else {
  418. /*
  419. * The above down_read_trylock() might have succeeded in which
  420. * case, we'll have missed the might_sleep() from down_read().
  421. */
  422. might_sleep();
  423. #ifdef CONFIG_DEBUG_VM
  424. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  425. goto no_context;
  426. #endif
  427. }
  428. fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk);
  429. major |= fault & VM_FAULT_MAJOR;
  430. if (fault & VM_FAULT_RETRY) {
  431. /*
  432. * If we need to retry but a fatal signal is pending,
  433. * handle the signal first. We do not need to release
  434. * the mmap_sem because it would already be released
  435. * in __lock_page_or_retry in mm/filemap.c.
  436. */
  437. if (fatal_signal_pending(current)) {
  438. if (!user_mode(regs))
  439. goto no_context;
  440. return 0;
  441. }
  442. /*
  443. * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
  444. * starvation.
  445. */
  446. if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
  447. mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
  448. mm_flags |= FAULT_FLAG_TRIED;
  449. goto retry;
  450. }
  451. }
  452. up_read(&mm->mmap_sem);
  453. /*
  454. * Handle the "normal" (no error) case first.
  455. */
  456. if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
  457. VM_FAULT_BADACCESS)))) {
  458. /*
  459. * Major/minor page fault accounting is only done
  460. * once. If we go through a retry, it is extremely
  461. * likely that the page will be found in page cache at
  462. * that point.
  463. */
  464. if (major) {
  465. tsk->maj_flt++;
  466. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
  467. addr);
  468. } else {
  469. tsk->min_flt++;
  470. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
  471. addr);
  472. }
  473. return 0;
  474. }
  475. /*
  476. * If we are in kernel mode at this point, we have no context to
  477. * handle this fault with.
  478. */
  479. if (!user_mode(regs))
  480. goto no_context;
  481. if (fault & VM_FAULT_OOM) {
  482. /*
  483. * We ran out of memory, call the OOM killer, and return to
  484. * userspace (which will retry the fault, or kill us if we got
  485. * oom-killed).
  486. */
  487. pagefault_out_of_memory();
  488. return 0;
  489. }
  490. clear_siginfo(&si);
  491. si.si_addr = (void __user *)addr;
  492. if (fault & VM_FAULT_SIGBUS) {
  493. /*
  494. * We had some memory, but were unable to successfully fix up
  495. * this page fault.
  496. */
  497. si.si_signo = SIGBUS;
  498. si.si_code = BUS_ADRERR;
  499. } else if (fault & VM_FAULT_HWPOISON_LARGE) {
  500. unsigned int hindex = VM_FAULT_GET_HINDEX(fault);
  501. si.si_signo = SIGBUS;
  502. si.si_code = BUS_MCEERR_AR;
  503. si.si_addr_lsb = hstate_index_to_shift(hindex);
  504. } else if (fault & VM_FAULT_HWPOISON) {
  505. si.si_signo = SIGBUS;
  506. si.si_code = BUS_MCEERR_AR;
  507. si.si_addr_lsb = PAGE_SHIFT;
  508. } else {
  509. /*
  510. * Something tried to access memory that isn't in our memory
  511. * map.
  512. */
  513. si.si_signo = SIGSEGV;
  514. si.si_code = fault == VM_FAULT_BADACCESS ?
  515. SEGV_ACCERR : SEGV_MAPERR;
  516. }
  517. __do_user_fault(&si, esr);
  518. return 0;
  519. no_context:
  520. __do_kernel_fault(addr, esr, regs);
  521. return 0;
  522. }
  523. static int __kprobes do_translation_fault(unsigned long addr,
  524. unsigned int esr,
  525. struct pt_regs *regs)
  526. {
  527. if (addr < TASK_SIZE)
  528. return do_page_fault(addr, esr, regs);
  529. do_bad_area(addr, esr, regs);
  530. return 0;
  531. }
  532. static int do_alignment_fault(unsigned long addr, unsigned int esr,
  533. struct pt_regs *regs)
  534. {
  535. do_bad_area(addr, esr, regs);
  536. return 0;
  537. }
  538. static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  539. {
  540. return 1; /* "fault" */
  541. }
  542. static int do_sea(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  543. {
  544. struct siginfo info;
  545. const struct fault_info *inf;
  546. inf = esr_to_fault_info(esr);
  547. /*
  548. * Synchronous aborts may interrupt code which had interrupts masked.
  549. * Before calling out into the wider kernel tell the interested
  550. * subsystems.
  551. */
  552. if (IS_ENABLED(CONFIG_ACPI_APEI_SEA)) {
  553. if (interrupts_enabled(regs))
  554. nmi_enter();
  555. ghes_notify_sea();
  556. if (interrupts_enabled(regs))
  557. nmi_exit();
  558. }
  559. clear_siginfo(&info);
  560. info.si_signo = inf->sig;
  561. info.si_errno = 0;
  562. info.si_code = inf->code;
  563. if (esr & ESR_ELx_FnV)
  564. info.si_addr = NULL;
  565. else
  566. info.si_addr = (void __user *)addr;
  567. arm64_notify_die(inf->name, regs, &info, esr);
  568. return 0;
  569. }
  570. static const struct fault_info fault_info[] = {
  571. { do_bad, SIGKILL, SI_KERNEL, "ttbr address size fault" },
  572. { do_bad, SIGKILL, SI_KERNEL, "level 1 address size fault" },
  573. { do_bad, SIGKILL, SI_KERNEL, "level 2 address size fault" },
  574. { do_bad, SIGKILL, SI_KERNEL, "level 3 address size fault" },
  575. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" },
  576. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
  577. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
  578. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
  579. { do_bad, SIGKILL, SI_KERNEL, "unknown 8" },
  580. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
  581. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
  582. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
  583. { do_bad, SIGKILL, SI_KERNEL, "unknown 12" },
  584. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
  585. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
  586. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
  587. { do_sea, SIGBUS, BUS_OBJERR, "synchronous external abort" },
  588. { do_bad, SIGKILL, SI_KERNEL, "unknown 17" },
  589. { do_bad, SIGKILL, SI_KERNEL, "unknown 18" },
  590. { do_bad, SIGKILL, SI_KERNEL, "unknown 19" },
  591. { do_sea, SIGKILL, SI_KERNEL, "level 0 (translation table walk)" },
  592. { do_sea, SIGKILL, SI_KERNEL, "level 1 (translation table walk)" },
  593. { do_sea, SIGKILL, SI_KERNEL, "level 2 (translation table walk)" },
  594. { do_sea, SIGKILL, SI_KERNEL, "level 3 (translation table walk)" },
  595. { do_sea, SIGBUS, BUS_OBJERR, "synchronous parity or ECC error" }, // Reserved when RAS is implemented
  596. { do_bad, SIGKILL, SI_KERNEL, "unknown 25" },
  597. { do_bad, SIGKILL, SI_KERNEL, "unknown 26" },
  598. { do_bad, SIGKILL, SI_KERNEL, "unknown 27" },
  599. { do_sea, SIGKILL, SI_KERNEL, "level 0 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
  600. { do_sea, SIGKILL, SI_KERNEL, "level 1 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
  601. { do_sea, SIGKILL, SI_KERNEL, "level 2 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
  602. { do_sea, SIGKILL, SI_KERNEL, "level 3 synchronous parity error (translation table walk)" }, // Reserved when RAS is implemented
  603. { do_bad, SIGKILL, SI_KERNEL, "unknown 32" },
  604. { do_alignment_fault, SIGBUS, BUS_ADRALN, "alignment fault" },
  605. { do_bad, SIGKILL, SI_KERNEL, "unknown 34" },
  606. { do_bad, SIGKILL, SI_KERNEL, "unknown 35" },
  607. { do_bad, SIGKILL, SI_KERNEL, "unknown 36" },
  608. { do_bad, SIGKILL, SI_KERNEL, "unknown 37" },
  609. { do_bad, SIGKILL, SI_KERNEL, "unknown 38" },
  610. { do_bad, SIGKILL, SI_KERNEL, "unknown 39" },
  611. { do_bad, SIGKILL, SI_KERNEL, "unknown 40" },
  612. { do_bad, SIGKILL, SI_KERNEL, "unknown 41" },
  613. { do_bad, SIGKILL, SI_KERNEL, "unknown 42" },
  614. { do_bad, SIGKILL, SI_KERNEL, "unknown 43" },
  615. { do_bad, SIGKILL, SI_KERNEL, "unknown 44" },
  616. { do_bad, SIGKILL, SI_KERNEL, "unknown 45" },
  617. { do_bad, SIGKILL, SI_KERNEL, "unknown 46" },
  618. { do_bad, SIGKILL, SI_KERNEL, "unknown 47" },
  619. { do_bad, SIGKILL, SI_KERNEL, "TLB conflict abort" },
  620. { do_bad, SIGKILL, SI_KERNEL, "Unsupported atomic hardware update fault" },
  621. { do_bad, SIGKILL, SI_KERNEL, "unknown 50" },
  622. { do_bad, SIGKILL, SI_KERNEL, "unknown 51" },
  623. { do_bad, SIGKILL, SI_KERNEL, "implementation fault (lockdown abort)" },
  624. { do_bad, SIGBUS, BUS_OBJERR, "implementation fault (unsupported exclusive)" },
  625. { do_bad, SIGKILL, SI_KERNEL, "unknown 54" },
  626. { do_bad, SIGKILL, SI_KERNEL, "unknown 55" },
  627. { do_bad, SIGKILL, SI_KERNEL, "unknown 56" },
  628. { do_bad, SIGKILL, SI_KERNEL, "unknown 57" },
  629. { do_bad, SIGKILL, SI_KERNEL, "unknown 58" },
  630. { do_bad, SIGKILL, SI_KERNEL, "unknown 59" },
  631. { do_bad, SIGKILL, SI_KERNEL, "unknown 60" },
  632. { do_bad, SIGKILL, SI_KERNEL, "section domain fault" },
  633. { do_bad, SIGKILL, SI_KERNEL, "page domain fault" },
  634. { do_bad, SIGKILL, SI_KERNEL, "unknown 63" },
  635. };
  636. int handle_guest_sea(phys_addr_t addr, unsigned int esr)
  637. {
  638. return ghes_notify_sea();
  639. }
  640. asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
  641. struct pt_regs *regs)
  642. {
  643. const struct fault_info *inf = esr_to_fault_info(esr);
  644. struct siginfo info;
  645. if (!inf->fn(addr, esr, regs))
  646. return;
  647. if (!user_mode(regs)) {
  648. pr_alert("Unhandled fault at 0x%016lx\n", addr);
  649. mem_abort_decode(esr);
  650. show_pte(addr);
  651. }
  652. clear_siginfo(&info);
  653. info.si_signo = inf->sig;
  654. info.si_errno = 0;
  655. info.si_code = inf->code;
  656. info.si_addr = (void __user *)addr;
  657. arm64_notify_die(inf->name, regs, &info, esr);
  658. }
  659. asmlinkage void __exception do_el0_irq_bp_hardening(void)
  660. {
  661. /* PC has already been checked in entry.S */
  662. arm64_apply_bp_hardening();
  663. }
  664. asmlinkage void __exception do_el0_ia_bp_hardening(unsigned long addr,
  665. unsigned int esr,
  666. struct pt_regs *regs)
  667. {
  668. /*
  669. * We've taken an instruction abort from userspace and not yet
  670. * re-enabled IRQs. If the address is a kernel address, apply
  671. * BP hardening prior to enabling IRQs and pre-emption.
  672. */
  673. if (addr > TASK_SIZE)
  674. arm64_apply_bp_hardening();
  675. local_irq_enable();
  676. do_mem_abort(addr, esr, regs);
  677. }
  678. asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
  679. unsigned int esr,
  680. struct pt_regs *regs)
  681. {
  682. struct siginfo info;
  683. if (user_mode(regs)) {
  684. if (instruction_pointer(regs) > TASK_SIZE)
  685. arm64_apply_bp_hardening();
  686. local_irq_enable();
  687. }
  688. clear_siginfo(&info);
  689. info.si_signo = SIGBUS;
  690. info.si_errno = 0;
  691. info.si_code = BUS_ADRALN;
  692. info.si_addr = (void __user *)addr;
  693. arm64_notify_die("SP/PC alignment exception", regs, &info, esr);
  694. }
  695. int __init early_brk64(unsigned long addr, unsigned int esr,
  696. struct pt_regs *regs);
  697. /*
  698. * __refdata because early_brk64 is __init, but the reference to it is
  699. * clobbered at arch_initcall time.
  700. * See traps.c and debug-monitors.c:debug_traps_init().
  701. */
  702. static struct fault_info __refdata debug_fault_info[] = {
  703. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
  704. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
  705. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
  706. { do_bad, SIGKILL, SI_KERNEL, "unknown 3" },
  707. { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
  708. { do_bad, SIGKILL, SI_KERNEL, "aarch32 vector catch" },
  709. { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
  710. { do_bad, SIGKILL, SI_KERNEL, "unknown 7" },
  711. };
  712. void __init hook_debug_fault_code(int nr,
  713. int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  714. int sig, int code, const char *name)
  715. {
  716. BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
  717. debug_fault_info[nr].fn = fn;
  718. debug_fault_info[nr].sig = sig;
  719. debug_fault_info[nr].code = code;
  720. debug_fault_info[nr].name = name;
  721. }
  722. #ifdef CONFIG_ARM64_ERRATUM_1463225
  723. DECLARE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
  724. static int __exception
  725. cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
  726. {
  727. if (user_mode(regs))
  728. return 0;
  729. if (!__this_cpu_read(__in_cortex_a76_erratum_1463225_wa))
  730. return 0;
  731. /*
  732. * We've taken a dummy step exception from the kernel to ensure
  733. * that interrupts are re-enabled on the syscall path. Return back
  734. * to cortex_a76_erratum_1463225_svc_handler() with debug exceptions
  735. * masked so that we can safely restore the mdscr and get on with
  736. * handling the syscall.
  737. */
  738. regs->pstate |= PSR_D_BIT;
  739. return 1;
  740. }
  741. #else
  742. static int __exception
  743. cortex_a76_erratum_1463225_debug_handler(struct pt_regs *regs)
  744. {
  745. return 0;
  746. }
  747. #endif /* CONFIG_ARM64_ERRATUM_1463225 */
  748. asmlinkage int __exception do_debug_exception(unsigned long addr_if_watchpoint,
  749. unsigned int esr,
  750. struct pt_regs *regs)
  751. {
  752. const struct fault_info *inf = debug_fault_info + DBG_ESR_EVT(esr);
  753. unsigned long pc = instruction_pointer(regs);
  754. int rv;
  755. if (cortex_a76_erratum_1463225_debug_handler(regs))
  756. return 0;
  757. /*
  758. * Tell lockdep we disabled irqs in entry.S. Do nothing if they were
  759. * already disabled to preserve the last enabled/disabled addresses.
  760. */
  761. if (interrupts_enabled(regs))
  762. trace_hardirqs_off();
  763. if (user_mode(regs) && pc > TASK_SIZE)
  764. arm64_apply_bp_hardening();
  765. if (!inf->fn(addr_if_watchpoint, esr, regs)) {
  766. rv = 1;
  767. } else {
  768. struct siginfo info;
  769. clear_siginfo(&info);
  770. info.si_signo = inf->sig;
  771. info.si_errno = 0;
  772. info.si_code = inf->code;
  773. info.si_addr = (void __user *)pc;
  774. arm64_notify_die(inf->name, regs, &info, esr);
  775. rv = 0;
  776. }
  777. if (interrupts_enabled(regs))
  778. trace_hardirqs_on();
  779. return rv;
  780. }
  781. NOKPROBE_SYMBOL(do_debug_exception);
  782. #ifdef CONFIG_ARM64_PAN
  783. void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused)
  784. {
  785. /*
  786. * We modify PSTATE. This won't work from irq context as the PSTATE
  787. * is discarded once we return from the exception.
  788. */
  789. WARN_ON_ONCE(in_interrupt());
  790. sysreg_clear_set(sctlr_el1, SCTLR_EL1_SPAN, 0);
  791. asm(SET_PSTATE_PAN(1));
  792. }
  793. #endif /* CONFIG_ARM64_PAN */