fault.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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.h>
  29. #include <linux/highmem.h>
  30. #include <linux/perf_event.h>
  31. #include <linux/preempt.h>
  32. #include <asm/bug.h>
  33. #include <asm/cpufeature.h>
  34. #include <asm/exception.h>
  35. #include <asm/debug-monitors.h>
  36. #include <asm/esr.h>
  37. #include <asm/sysreg.h>
  38. #include <asm/system_misc.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/tlbflush.h>
  41. struct fault_info {
  42. int (*fn)(unsigned long addr, unsigned int esr,
  43. struct pt_regs *regs);
  44. int sig;
  45. int code;
  46. const char *name;
  47. };
  48. static const struct fault_info fault_info[];
  49. static inline const struct fault_info *esr_to_fault_info(unsigned int esr)
  50. {
  51. return fault_info + (esr & 63);
  52. }
  53. #ifdef CONFIG_KPROBES
  54. static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
  55. {
  56. int ret = 0;
  57. /* kprobe_running() needs smp_processor_id() */
  58. if (!user_mode(regs)) {
  59. preempt_disable();
  60. if (kprobe_running() && kprobe_fault_handler(regs, esr))
  61. ret = 1;
  62. preempt_enable();
  63. }
  64. return ret;
  65. }
  66. #else
  67. static inline int notify_page_fault(struct pt_regs *regs, unsigned int esr)
  68. {
  69. return 0;
  70. }
  71. #endif
  72. /*
  73. * Dump out the page tables associated with 'addr' in mm 'mm'.
  74. */
  75. void show_pte(struct mm_struct *mm, unsigned long addr)
  76. {
  77. pgd_t *pgd;
  78. if (!mm)
  79. mm = &init_mm;
  80. pr_alert("pgd = %p\n", mm->pgd);
  81. pgd = pgd_offset(mm, addr);
  82. pr_alert("[%08lx] *pgd=%016llx", addr, pgd_val(*pgd));
  83. do {
  84. pud_t *pud;
  85. pmd_t *pmd;
  86. pte_t *pte;
  87. if (pgd_none(*pgd) || pgd_bad(*pgd))
  88. break;
  89. pud = pud_offset(pgd, addr);
  90. pr_cont(", *pud=%016llx", pud_val(*pud));
  91. if (pud_none(*pud) || pud_bad(*pud))
  92. break;
  93. pmd = pmd_offset(pud, addr);
  94. pr_cont(", *pmd=%016llx", pmd_val(*pmd));
  95. if (pmd_none(*pmd) || pmd_bad(*pmd))
  96. break;
  97. pte = pte_offset_map(pmd, addr);
  98. pr_cont(", *pte=%016llx", pte_val(*pte));
  99. pte_unmap(pte);
  100. } while(0);
  101. pr_cont("\n");
  102. }
  103. #ifdef CONFIG_ARM64_HW_AFDBM
  104. /*
  105. * This function sets the access flags (dirty, accessed), as well as write
  106. * permission, and only to a more permissive setting.
  107. *
  108. * It needs to cope with hardware update of the accessed/dirty state by other
  109. * agents in the system and can safely skip the __sync_icache_dcache() call as,
  110. * like set_pte_at(), the PTE is never changed from no-exec to exec here.
  111. *
  112. * Returns whether or not the PTE actually changed.
  113. */
  114. int ptep_set_access_flags(struct vm_area_struct *vma,
  115. unsigned long address, pte_t *ptep,
  116. pte_t entry, int dirty)
  117. {
  118. pteval_t old_pteval;
  119. unsigned int tmp;
  120. if (pte_same(*ptep, entry))
  121. return 0;
  122. /* only preserve the access flags and write permission */
  123. pte_val(entry) &= PTE_AF | PTE_WRITE | PTE_DIRTY;
  124. /*
  125. * PTE_RDONLY is cleared by default in the asm below, so set it in
  126. * back if necessary (read-only or clean PTE).
  127. */
  128. if (!pte_write(entry) || !pte_sw_dirty(entry))
  129. pte_val(entry) |= PTE_RDONLY;
  130. /*
  131. * Setting the flags must be done atomically to avoid racing with the
  132. * hardware update of the access/dirty state.
  133. */
  134. asm volatile("// ptep_set_access_flags\n"
  135. " prfm pstl1strm, %2\n"
  136. "1: ldxr %0, %2\n"
  137. " and %0, %0, %3 // clear PTE_RDONLY\n"
  138. " orr %0, %0, %4 // set flags\n"
  139. " stxr %w1, %0, %2\n"
  140. " cbnz %w1, 1b\n"
  141. : "=&r" (old_pteval), "=&r" (tmp), "+Q" (pte_val(*ptep))
  142. : "L" (~PTE_RDONLY), "r" (pte_val(entry)));
  143. flush_tlb_fix_spurious_fault(vma, address);
  144. return 1;
  145. }
  146. #endif
  147. static bool is_el1_instruction_abort(unsigned int esr)
  148. {
  149. return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_CUR;
  150. }
  151. /*
  152. * The kernel tried to access some page that wasn't present.
  153. */
  154. static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
  155. unsigned int esr, struct pt_regs *regs)
  156. {
  157. /*
  158. * Are we prepared to handle this kernel fault?
  159. * We are almost certainly not prepared to handle instruction faults.
  160. */
  161. if (!is_el1_instruction_abort(esr) && fixup_exception(regs))
  162. return;
  163. /*
  164. * No handler, we'll have to terminate things with extreme prejudice.
  165. */
  166. bust_spinlocks(1);
  167. pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
  168. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  169. "paging request", addr);
  170. show_pte(mm, addr);
  171. die("Oops", regs, esr);
  172. bust_spinlocks(0);
  173. do_exit(SIGKILL);
  174. }
  175. /*
  176. * Something tried to access memory that isn't in our memory map. User mode
  177. * accesses just cause a SIGSEGV
  178. */
  179. static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
  180. unsigned int esr, unsigned int sig, int code,
  181. struct pt_regs *regs)
  182. {
  183. struct siginfo si;
  184. const struct fault_info *inf;
  185. if (unhandled_signal(tsk, sig) && show_unhandled_signals_ratelimited()) {
  186. inf = esr_to_fault_info(esr);
  187. pr_info("%s[%d]: unhandled %s (%d) at 0x%08lx, esr 0x%03x\n",
  188. tsk->comm, task_pid_nr(tsk), inf->name, sig,
  189. addr, esr);
  190. show_pte(tsk->mm, addr);
  191. show_regs(regs);
  192. }
  193. tsk->thread.fault_address = addr;
  194. tsk->thread.fault_code = esr;
  195. si.si_signo = sig;
  196. si.si_errno = 0;
  197. si.si_code = code;
  198. si.si_addr = (void __user *)addr;
  199. force_sig_info(sig, &si, tsk);
  200. }
  201. static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  202. {
  203. struct task_struct *tsk = current;
  204. struct mm_struct *mm = tsk->active_mm;
  205. const struct fault_info *inf;
  206. /*
  207. * If we are in kernel mode at this point, we have no context to
  208. * handle this fault with.
  209. */
  210. if (user_mode(regs)) {
  211. inf = esr_to_fault_info(esr);
  212. __do_user_fault(tsk, addr, esr, inf->sig, inf->code, regs);
  213. } else
  214. __do_kernel_fault(mm, addr, esr, regs);
  215. }
  216. #define VM_FAULT_BADMAP 0x010000
  217. #define VM_FAULT_BADACCESS 0x020000
  218. static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
  219. unsigned int mm_flags, unsigned long vm_flags,
  220. struct task_struct *tsk)
  221. {
  222. struct vm_area_struct *vma;
  223. int fault;
  224. vma = find_vma(mm, addr);
  225. fault = VM_FAULT_BADMAP;
  226. if (unlikely(!vma))
  227. goto out;
  228. if (unlikely(vma->vm_start > addr))
  229. goto check_stack;
  230. /*
  231. * Ok, we have a good vm_area for this memory access, so we can handle
  232. * it.
  233. */
  234. good_area:
  235. /*
  236. * Check that the permissions on the VMA allow for the fault which
  237. * occurred.
  238. */
  239. if (!(vma->vm_flags & vm_flags)) {
  240. fault = VM_FAULT_BADACCESS;
  241. goto out;
  242. }
  243. return handle_mm_fault(vma, addr & PAGE_MASK, mm_flags);
  244. check_stack:
  245. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  246. goto good_area;
  247. out:
  248. return fault;
  249. }
  250. static inline bool is_permission_fault(unsigned int esr, struct pt_regs *regs)
  251. {
  252. unsigned int ec = ESR_ELx_EC(esr);
  253. unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;
  254. if (ec != ESR_ELx_EC_DABT_CUR && ec != ESR_ELx_EC_IABT_CUR)
  255. return false;
  256. if (system_uses_ttbr0_pan())
  257. return fsc_type == ESR_ELx_FSC_FAULT &&
  258. (regs->pstate & PSR_PAN_BIT);
  259. else
  260. return fsc_type == ESR_ELx_FSC_PERM;
  261. }
  262. static bool is_el0_instruction_abort(unsigned int esr)
  263. {
  264. return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW;
  265. }
  266. static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
  267. struct pt_regs *regs)
  268. {
  269. struct task_struct *tsk;
  270. struct mm_struct *mm;
  271. int fault, sig, code;
  272. unsigned long vm_flags = VM_READ | VM_WRITE;
  273. unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  274. if (notify_page_fault(regs, esr))
  275. return 0;
  276. tsk = current;
  277. mm = tsk->mm;
  278. /*
  279. * If we're in an interrupt or have no user context, we must not take
  280. * the fault.
  281. */
  282. if (faulthandler_disabled() || !mm)
  283. goto no_context;
  284. if (user_mode(regs))
  285. mm_flags |= FAULT_FLAG_USER;
  286. if (is_el0_instruction_abort(esr)) {
  287. vm_flags = VM_EXEC;
  288. } else if ((esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM)) {
  289. vm_flags = VM_WRITE;
  290. mm_flags |= FAULT_FLAG_WRITE;
  291. }
  292. if (addr < USER_DS && is_permission_fault(esr, regs)) {
  293. /* regs->orig_addr_limit may be 0 if we entered from EL0 */
  294. if (regs->orig_addr_limit == KERNEL_DS)
  295. die("Accessing user space memory with fs=KERNEL_DS", regs, esr);
  296. if (is_el1_instruction_abort(esr))
  297. die("Attempting to execute userspace memory", regs, esr);
  298. if (!search_exception_tables(regs->pc))
  299. die("Accessing user space memory outside uaccess.h routines", regs, esr);
  300. }
  301. /*
  302. * As per x86, we may deadlock here. However, since the kernel only
  303. * validly references user space from well defined areas of the code,
  304. * we can bug out early if this is from code which shouldn't.
  305. */
  306. if (!down_read_trylock(&mm->mmap_sem)) {
  307. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  308. goto no_context;
  309. retry:
  310. down_read(&mm->mmap_sem);
  311. } else {
  312. /*
  313. * The above down_read_trylock() might have succeeded in which
  314. * case, we'll have missed the might_sleep() from down_read().
  315. */
  316. might_sleep();
  317. #ifdef CONFIG_DEBUG_VM
  318. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  319. goto no_context;
  320. #endif
  321. }
  322. fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk);
  323. /*
  324. * If we need to retry but a fatal signal is pending, handle the
  325. * signal first. We do not need to release the mmap_sem because it
  326. * would already be released in __lock_page_or_retry in mm/filemap.c.
  327. */
  328. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  329. if (!user_mode(regs))
  330. goto no_context;
  331. return 0;
  332. }
  333. /*
  334. * Major/minor page fault accounting is only done on the initial
  335. * attempt. If we go through a retry, it is extremely likely that the
  336. * page will be found in page cache at that point.
  337. */
  338. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  339. if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
  340. if (fault & VM_FAULT_MAJOR) {
  341. tsk->maj_flt++;
  342. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
  343. addr);
  344. } else {
  345. tsk->min_flt++;
  346. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
  347. addr);
  348. }
  349. if (fault & VM_FAULT_RETRY) {
  350. /*
  351. * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
  352. * starvation.
  353. */
  354. mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
  355. mm_flags |= FAULT_FLAG_TRIED;
  356. goto retry;
  357. }
  358. }
  359. up_read(&mm->mmap_sem);
  360. /*
  361. * Handle the "normal" case first - VM_FAULT_MAJOR
  362. */
  363. if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
  364. VM_FAULT_BADACCESS))))
  365. return 0;
  366. /*
  367. * If we are in kernel mode at this point, we have no context to
  368. * handle this fault with.
  369. */
  370. if (!user_mode(regs))
  371. goto no_context;
  372. if (fault & VM_FAULT_OOM) {
  373. /*
  374. * We ran out of memory, call the OOM killer, and return to
  375. * userspace (which will retry the fault, or kill us if we got
  376. * oom-killed).
  377. */
  378. pagefault_out_of_memory();
  379. return 0;
  380. }
  381. if (fault & VM_FAULT_SIGBUS) {
  382. /*
  383. * We had some memory, but were unable to successfully fix up
  384. * this page fault.
  385. */
  386. sig = SIGBUS;
  387. code = BUS_ADRERR;
  388. } else {
  389. /*
  390. * Something tried to access memory that isn't in our memory
  391. * map.
  392. */
  393. sig = SIGSEGV;
  394. code = fault == VM_FAULT_BADACCESS ?
  395. SEGV_ACCERR : SEGV_MAPERR;
  396. }
  397. __do_user_fault(tsk, addr, esr, sig, code, regs);
  398. return 0;
  399. no_context:
  400. __do_kernel_fault(mm, addr, esr, regs);
  401. return 0;
  402. }
  403. /*
  404. * First Level Translation Fault Handler
  405. *
  406. * We enter here because the first level page table doesn't contain a valid
  407. * entry for the address.
  408. *
  409. * If the address is in kernel space (>= TASK_SIZE), then we are probably
  410. * faulting in the vmalloc() area.
  411. *
  412. * If the init_task's first level page tables contains the relevant entry, we
  413. * copy the it to this task. If not, we send the process a signal, fixup the
  414. * exception, or oops the kernel.
  415. *
  416. * NOTE! We MUST NOT take any locks for this case. We may be in an interrupt
  417. * or a critical region, and should only copy the information from the master
  418. * page table, nothing more.
  419. */
  420. static int __kprobes do_translation_fault(unsigned long addr,
  421. unsigned int esr,
  422. struct pt_regs *regs)
  423. {
  424. if (addr < TASK_SIZE)
  425. return do_page_fault(addr, esr, regs);
  426. do_bad_area(addr, esr, regs);
  427. return 0;
  428. }
  429. static int do_alignment_fault(unsigned long addr, unsigned int esr,
  430. struct pt_regs *regs)
  431. {
  432. do_bad_area(addr, esr, regs);
  433. return 0;
  434. }
  435. /*
  436. * This abort handler always returns "fault".
  437. */
  438. static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  439. {
  440. return 1;
  441. }
  442. static const struct fault_info fault_info[] = {
  443. { do_bad, SIGBUS, 0, "ttbr address size fault" },
  444. { do_bad, SIGBUS, 0, "level 1 address size fault" },
  445. { do_bad, SIGBUS, 0, "level 2 address size fault" },
  446. { do_bad, SIGBUS, 0, "level 3 address size fault" },
  447. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" },
  448. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
  449. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
  450. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
  451. { do_bad, SIGBUS, 0, "unknown 8" },
  452. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
  453. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
  454. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
  455. { do_bad, SIGBUS, 0, "unknown 12" },
  456. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
  457. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
  458. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
  459. { do_bad, SIGBUS, 0, "synchronous external abort" },
  460. { do_bad, SIGBUS, 0, "unknown 17" },
  461. { do_bad, SIGBUS, 0, "unknown 18" },
  462. { do_bad, SIGBUS, 0, "unknown 19" },
  463. { do_bad, SIGBUS, 0, "synchronous external abort (translation table walk)" },
  464. { do_bad, SIGBUS, 0, "synchronous external abort (translation table walk)" },
  465. { do_bad, SIGBUS, 0, "synchronous external abort (translation table walk)" },
  466. { do_bad, SIGBUS, 0, "synchronous external abort (translation table walk)" },
  467. { do_bad, SIGBUS, 0, "synchronous parity error" },
  468. { do_bad, SIGBUS, 0, "unknown 25" },
  469. { do_bad, SIGBUS, 0, "unknown 26" },
  470. { do_bad, SIGBUS, 0, "unknown 27" },
  471. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  472. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  473. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  474. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  475. { do_bad, SIGBUS, 0, "unknown 32" },
  476. { do_alignment_fault, SIGBUS, BUS_ADRALN, "alignment fault" },
  477. { do_bad, SIGBUS, 0, "unknown 34" },
  478. { do_bad, SIGBUS, 0, "unknown 35" },
  479. { do_bad, SIGBUS, 0, "unknown 36" },
  480. { do_bad, SIGBUS, 0, "unknown 37" },
  481. { do_bad, SIGBUS, 0, "unknown 38" },
  482. { do_bad, SIGBUS, 0, "unknown 39" },
  483. { do_bad, SIGBUS, 0, "unknown 40" },
  484. { do_bad, SIGBUS, 0, "unknown 41" },
  485. { do_bad, SIGBUS, 0, "unknown 42" },
  486. { do_bad, SIGBUS, 0, "unknown 43" },
  487. { do_bad, SIGBUS, 0, "unknown 44" },
  488. { do_bad, SIGBUS, 0, "unknown 45" },
  489. { do_bad, SIGBUS, 0, "unknown 46" },
  490. { do_bad, SIGBUS, 0, "unknown 47" },
  491. { do_bad, SIGBUS, 0, "TLB conflict abort" },
  492. { do_bad, SIGBUS, 0, "unknown 49" },
  493. { do_bad, SIGBUS, 0, "unknown 50" },
  494. { do_bad, SIGBUS, 0, "unknown 51" },
  495. { do_bad, SIGBUS, 0, "implementation fault (lockdown abort)" },
  496. { do_bad, SIGBUS, 0, "implementation fault (unsupported exclusive)" },
  497. { do_bad, SIGBUS, 0, "unknown 54" },
  498. { do_bad, SIGBUS, 0, "unknown 55" },
  499. { do_bad, SIGBUS, 0, "unknown 56" },
  500. { do_bad, SIGBUS, 0, "unknown 57" },
  501. { do_bad, SIGBUS, 0, "unknown 58" },
  502. { do_bad, SIGBUS, 0, "unknown 59" },
  503. { do_bad, SIGBUS, 0, "unknown 60" },
  504. { do_bad, SIGBUS, 0, "section domain fault" },
  505. { do_bad, SIGBUS, 0, "page domain fault" },
  506. { do_bad, SIGBUS, 0, "unknown 63" },
  507. };
  508. /*
  509. * Dispatch a data abort to the relevant handler.
  510. */
  511. asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
  512. struct pt_regs *regs)
  513. {
  514. const struct fault_info *inf = esr_to_fault_info(esr);
  515. struct siginfo info;
  516. if (!inf->fn(addr, esr, regs))
  517. return;
  518. pr_alert("Unhandled fault: %s (0x%08x) at 0x%016lx\n",
  519. inf->name, esr, addr);
  520. info.si_signo = inf->sig;
  521. info.si_errno = 0;
  522. info.si_code = inf->code;
  523. info.si_addr = (void __user *)addr;
  524. arm64_notify_die("", regs, &info, esr);
  525. }
  526. /*
  527. * Handle stack alignment exceptions.
  528. */
  529. asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
  530. unsigned int esr,
  531. struct pt_regs *regs)
  532. {
  533. struct siginfo info;
  534. struct task_struct *tsk = current;
  535. if (show_unhandled_signals && unhandled_signal(tsk, SIGBUS))
  536. pr_info_ratelimited("%s[%d]: %s exception: pc=%p sp=%p\n",
  537. tsk->comm, task_pid_nr(tsk),
  538. esr_get_class_string(esr), (void *)regs->pc,
  539. (void *)regs->sp);
  540. info.si_signo = SIGBUS;
  541. info.si_errno = 0;
  542. info.si_code = BUS_ADRALN;
  543. info.si_addr = (void __user *)addr;
  544. arm64_notify_die("Oops - SP/PC alignment exception", regs, &info, esr);
  545. }
  546. int __init early_brk64(unsigned long addr, unsigned int esr,
  547. struct pt_regs *regs);
  548. /*
  549. * __refdata because early_brk64 is __init, but the reference to it is
  550. * clobbered at arch_initcall time.
  551. * See traps.c and debug-monitors.c:debug_traps_init().
  552. */
  553. static struct fault_info __refdata debug_fault_info[] = {
  554. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
  555. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
  556. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
  557. { do_bad, SIGBUS, 0, "unknown 3" },
  558. { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
  559. { do_bad, SIGTRAP, 0, "aarch32 vector catch" },
  560. { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
  561. { do_bad, SIGBUS, 0, "unknown 7" },
  562. };
  563. void __init hook_debug_fault_code(int nr,
  564. int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  565. int sig, int code, const char *name)
  566. {
  567. BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
  568. debug_fault_info[nr].fn = fn;
  569. debug_fault_info[nr].sig = sig;
  570. debug_fault_info[nr].code = code;
  571. debug_fault_info[nr].name = name;
  572. }
  573. asmlinkage int __exception do_debug_exception(unsigned long addr,
  574. unsigned int esr,
  575. struct pt_regs *regs)
  576. {
  577. const struct fault_info *inf = debug_fault_info + DBG_ESR_EVT(esr);
  578. struct siginfo info;
  579. int rv;
  580. /*
  581. * Tell lockdep we disabled irqs in entry.S. Do nothing if they were
  582. * already disabled to preserve the last enabled/disabled addresses.
  583. */
  584. if (interrupts_enabled(regs))
  585. trace_hardirqs_off();
  586. if (!inf->fn(addr, esr, regs)) {
  587. rv = 1;
  588. } else {
  589. pr_alert("Unhandled debug exception: %s (0x%08x) at 0x%016lx\n",
  590. inf->name, esr, addr);
  591. info.si_signo = inf->sig;
  592. info.si_errno = 0;
  593. info.si_code = inf->code;
  594. info.si_addr = (void __user *)addr;
  595. arm64_notify_die("", regs, &info, 0);
  596. rv = 0;
  597. }
  598. if (interrupts_enabled(regs))
  599. trace_hardirqs_on();
  600. return rv;
  601. }
  602. NOKPROBE_SYMBOL(do_debug_exception);
  603. #ifdef CONFIG_ARM64_PAN
  604. int cpu_enable_pan(void *__unused)
  605. {
  606. /*
  607. * We modify PSTATE. This won't work from irq context as the PSTATE
  608. * is discarded once we return from the exception.
  609. */
  610. WARN_ON_ONCE(in_interrupt());
  611. config_sctlr_el1(SCTLR_EL1_SPAN, 0);
  612. asm(SET_PSTATE_PAN(1));
  613. return 0;
  614. }
  615. #endif /* CONFIG_ARM64_PAN */
  616. #ifdef CONFIG_ARM64_UAO
  617. /*
  618. * Kernel threads have fs=KERNEL_DS by default, and don't need to call
  619. * set_fs(), devtmpfs in particular relies on this behaviour.
  620. * We need to enable the feature at runtime (instead of adding it to
  621. * PSR_MODE_EL1h) as the feature may not be implemented by the cpu.
  622. */
  623. int cpu_enable_uao(void *__unused)
  624. {
  625. asm(SET_PSTATE_UAO(1));
  626. return 0;
  627. }
  628. #endif /* CONFIG_ARM64_UAO */