fault.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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)
  251. {
  252. unsigned int ec = ESR_ELx_EC(esr);
  253. unsigned int fsc_type = esr & ESR_ELx_FSC_TYPE;
  254. return (ec == ESR_ELx_EC_DABT_CUR && fsc_type == ESR_ELx_FSC_PERM) ||
  255. (ec == ESR_ELx_EC_IABT_CUR && fsc_type == ESR_ELx_FSC_PERM);
  256. }
  257. static bool is_el0_instruction_abort(unsigned int esr)
  258. {
  259. return ESR_ELx_EC(esr) == ESR_ELx_EC_IABT_LOW;
  260. }
  261. static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
  262. struct pt_regs *regs)
  263. {
  264. struct task_struct *tsk;
  265. struct mm_struct *mm;
  266. int fault, sig, code;
  267. unsigned long vm_flags = VM_READ | VM_WRITE;
  268. unsigned int mm_flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  269. if (notify_page_fault(regs, esr))
  270. return 0;
  271. tsk = current;
  272. mm = tsk->mm;
  273. /*
  274. * If we're in an interrupt or have no user context, we must not take
  275. * the fault.
  276. */
  277. if (faulthandler_disabled() || !mm)
  278. goto no_context;
  279. if (user_mode(regs))
  280. mm_flags |= FAULT_FLAG_USER;
  281. if (is_el0_instruction_abort(esr)) {
  282. vm_flags = VM_EXEC;
  283. } else if ((esr & ESR_ELx_WNR) && !(esr & ESR_ELx_CM)) {
  284. vm_flags = VM_WRITE;
  285. mm_flags |= FAULT_FLAG_WRITE;
  286. }
  287. if (is_permission_fault(esr) && (addr < TASK_SIZE)) {
  288. /* regs->orig_addr_limit may be 0 if we entered from EL0 */
  289. if (regs->orig_addr_limit == KERNEL_DS)
  290. die("Accessing user space memory with fs=KERNEL_DS", regs, esr);
  291. if (is_el1_instruction_abort(esr))
  292. die("Attempting to execute userspace memory", regs, esr);
  293. if (!search_exception_tables(regs->pc))
  294. die("Accessing user space memory outside uaccess.h routines", regs, esr);
  295. }
  296. /*
  297. * As per x86, we may deadlock here. However, since the kernel only
  298. * validly references user space from well defined areas of the code,
  299. * we can bug out early if this is from code which shouldn't.
  300. */
  301. if (!down_read_trylock(&mm->mmap_sem)) {
  302. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  303. goto no_context;
  304. retry:
  305. down_read(&mm->mmap_sem);
  306. } else {
  307. /*
  308. * The above down_read_trylock() might have succeeded in which
  309. * case, we'll have missed the might_sleep() from down_read().
  310. */
  311. might_sleep();
  312. #ifdef CONFIG_DEBUG_VM
  313. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  314. goto no_context;
  315. #endif
  316. }
  317. fault = __do_page_fault(mm, addr, mm_flags, vm_flags, tsk);
  318. /*
  319. * If we need to retry but a fatal signal is pending, handle the
  320. * signal first. We do not need to release the mmap_sem because it
  321. * would already be released in __lock_page_or_retry in mm/filemap.c.
  322. */
  323. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)) {
  324. if (!user_mode(regs))
  325. goto no_context;
  326. return 0;
  327. }
  328. /*
  329. * Major/minor page fault accounting is only done on the initial
  330. * attempt. If we go through a retry, it is extremely likely that the
  331. * page will be found in page cache at that point.
  332. */
  333. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  334. if (mm_flags & FAULT_FLAG_ALLOW_RETRY) {
  335. if (fault & VM_FAULT_MAJOR) {
  336. tsk->maj_flt++;
  337. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
  338. addr);
  339. } else {
  340. tsk->min_flt++;
  341. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
  342. addr);
  343. }
  344. if (fault & VM_FAULT_RETRY) {
  345. /*
  346. * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
  347. * starvation.
  348. */
  349. mm_flags &= ~FAULT_FLAG_ALLOW_RETRY;
  350. mm_flags |= FAULT_FLAG_TRIED;
  351. goto retry;
  352. }
  353. }
  354. up_read(&mm->mmap_sem);
  355. /*
  356. * Handle the "normal" case first - VM_FAULT_MAJOR
  357. */
  358. if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
  359. VM_FAULT_BADACCESS))))
  360. return 0;
  361. /*
  362. * If we are in kernel mode at this point, we have no context to
  363. * handle this fault with.
  364. */
  365. if (!user_mode(regs))
  366. goto no_context;
  367. if (fault & VM_FAULT_OOM) {
  368. /*
  369. * We ran out of memory, call the OOM killer, and return to
  370. * userspace (which will retry the fault, or kill us if we got
  371. * oom-killed).
  372. */
  373. pagefault_out_of_memory();
  374. return 0;
  375. }
  376. if (fault & VM_FAULT_SIGBUS) {
  377. /*
  378. * We had some memory, but were unable to successfully fix up
  379. * this page fault.
  380. */
  381. sig = SIGBUS;
  382. code = BUS_ADRERR;
  383. } else {
  384. /*
  385. * Something tried to access memory that isn't in our memory
  386. * map.
  387. */
  388. sig = SIGSEGV;
  389. code = fault == VM_FAULT_BADACCESS ?
  390. SEGV_ACCERR : SEGV_MAPERR;
  391. }
  392. __do_user_fault(tsk, addr, esr, sig, code, regs);
  393. return 0;
  394. no_context:
  395. __do_kernel_fault(mm, addr, esr, regs);
  396. return 0;
  397. }
  398. /*
  399. * First Level Translation Fault Handler
  400. *
  401. * We enter here because the first level page table doesn't contain a valid
  402. * entry for the address.
  403. *
  404. * If the address is in kernel space (>= TASK_SIZE), then we are probably
  405. * faulting in the vmalloc() area.
  406. *
  407. * If the init_task's first level page tables contains the relevant entry, we
  408. * copy the it to this task. If not, we send the process a signal, fixup the
  409. * exception, or oops the kernel.
  410. *
  411. * NOTE! We MUST NOT take any locks for this case. We may be in an interrupt
  412. * or a critical region, and should only copy the information from the master
  413. * page table, nothing more.
  414. */
  415. static int __kprobes do_translation_fault(unsigned long addr,
  416. unsigned int esr,
  417. struct pt_regs *regs)
  418. {
  419. if (addr < TASK_SIZE)
  420. return do_page_fault(addr, esr, regs);
  421. do_bad_area(addr, esr, regs);
  422. return 0;
  423. }
  424. static int do_alignment_fault(unsigned long addr, unsigned int esr,
  425. struct pt_regs *regs)
  426. {
  427. do_bad_area(addr, esr, regs);
  428. return 0;
  429. }
  430. /*
  431. * This abort handler always returns "fault".
  432. */
  433. static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  434. {
  435. return 1;
  436. }
  437. static const struct fault_info fault_info[] = {
  438. { do_bad, SIGBUS, 0, "ttbr address size fault" },
  439. { do_bad, SIGBUS, 0, "level 1 address size fault" },
  440. { do_bad, SIGBUS, 0, "level 2 address size fault" },
  441. { do_bad, SIGBUS, 0, "level 3 address size fault" },
  442. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 0 translation fault" },
  443. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
  444. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
  445. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
  446. { do_bad, SIGBUS, 0, "unknown 8" },
  447. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
  448. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
  449. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
  450. { do_bad, SIGBUS, 0, "unknown 12" },
  451. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
  452. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
  453. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
  454. { do_bad, SIGBUS, 0, "synchronous external abort" },
  455. { do_bad, SIGBUS, 0, "unknown 17" },
  456. { do_bad, SIGBUS, 0, "unknown 18" },
  457. { do_bad, SIGBUS, 0, "unknown 19" },
  458. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  459. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  460. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  461. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  462. { do_bad, SIGBUS, 0, "synchronous parity error" },
  463. { do_bad, SIGBUS, 0, "unknown 25" },
  464. { do_bad, SIGBUS, 0, "unknown 26" },
  465. { do_bad, SIGBUS, 0, "unknown 27" },
  466. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  467. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  468. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  469. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk)" },
  470. { do_bad, SIGBUS, 0, "unknown 32" },
  471. { do_alignment_fault, SIGBUS, BUS_ADRALN, "alignment fault" },
  472. { do_bad, SIGBUS, 0, "unknown 34" },
  473. { do_bad, SIGBUS, 0, "unknown 35" },
  474. { do_bad, SIGBUS, 0, "unknown 36" },
  475. { do_bad, SIGBUS, 0, "unknown 37" },
  476. { do_bad, SIGBUS, 0, "unknown 38" },
  477. { do_bad, SIGBUS, 0, "unknown 39" },
  478. { do_bad, SIGBUS, 0, "unknown 40" },
  479. { do_bad, SIGBUS, 0, "unknown 41" },
  480. { do_bad, SIGBUS, 0, "unknown 42" },
  481. { do_bad, SIGBUS, 0, "unknown 43" },
  482. { do_bad, SIGBUS, 0, "unknown 44" },
  483. { do_bad, SIGBUS, 0, "unknown 45" },
  484. { do_bad, SIGBUS, 0, "unknown 46" },
  485. { do_bad, SIGBUS, 0, "unknown 47" },
  486. { do_bad, SIGBUS, 0, "TLB conflict abort" },
  487. { do_bad, SIGBUS, 0, "unknown 49" },
  488. { do_bad, SIGBUS, 0, "unknown 50" },
  489. { do_bad, SIGBUS, 0, "unknown 51" },
  490. { do_bad, SIGBUS, 0, "implementation fault (lockdown abort)" },
  491. { do_bad, SIGBUS, 0, "implementation fault (unsupported exclusive)" },
  492. { do_bad, SIGBUS, 0, "unknown 54" },
  493. { do_bad, SIGBUS, 0, "unknown 55" },
  494. { do_bad, SIGBUS, 0, "unknown 56" },
  495. { do_bad, SIGBUS, 0, "unknown 57" },
  496. { do_bad, SIGBUS, 0, "unknown 58" },
  497. { do_bad, SIGBUS, 0, "unknown 59" },
  498. { do_bad, SIGBUS, 0, "unknown 60" },
  499. { do_bad, SIGBUS, 0, "section domain fault" },
  500. { do_bad, SIGBUS, 0, "page domain fault" },
  501. { do_bad, SIGBUS, 0, "unknown 63" },
  502. };
  503. /*
  504. * Dispatch a data abort to the relevant handler.
  505. */
  506. asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
  507. struct pt_regs *regs)
  508. {
  509. const struct fault_info *inf = esr_to_fault_info(esr);
  510. struct siginfo info;
  511. if (!inf->fn(addr, esr, regs))
  512. return;
  513. pr_alert("Unhandled fault: %s (0x%08x) at 0x%016lx\n",
  514. inf->name, esr, addr);
  515. info.si_signo = inf->sig;
  516. info.si_errno = 0;
  517. info.si_code = inf->code;
  518. info.si_addr = (void __user *)addr;
  519. arm64_notify_die("", regs, &info, esr);
  520. }
  521. asmlinkage void __exception do_el0_irq_bp_hardening(void)
  522. {
  523. /* PC has already been checked in entry.S */
  524. arm64_apply_bp_hardening();
  525. }
  526. asmlinkage void __exception do_el0_ia_bp_hardening(unsigned long addr,
  527. unsigned int esr,
  528. struct pt_regs *regs)
  529. {
  530. /*
  531. * We've taken an instruction abort from userspace and not yet
  532. * re-enabled IRQs. If the address is a kernel address, apply
  533. * BP hardening prior to enabling IRQs and pre-emption.
  534. */
  535. if (addr > TASK_SIZE)
  536. arm64_apply_bp_hardening();
  537. local_irq_enable();
  538. do_mem_abort(addr, esr, regs);
  539. }
  540. /*
  541. * Handle stack alignment exceptions.
  542. */
  543. asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
  544. unsigned int esr,
  545. struct pt_regs *regs)
  546. {
  547. struct siginfo info;
  548. struct task_struct *tsk = current;
  549. if (user_mode(regs)) {
  550. if (instruction_pointer(regs) > TASK_SIZE)
  551. arm64_apply_bp_hardening();
  552. local_irq_enable();
  553. }
  554. if (show_unhandled_signals && unhandled_signal(tsk, SIGBUS))
  555. pr_info_ratelimited("%s[%d]: %s exception: pc=%p sp=%p\n",
  556. tsk->comm, task_pid_nr(tsk),
  557. esr_get_class_string(esr), (void *)regs->pc,
  558. (void *)regs->sp);
  559. info.si_signo = SIGBUS;
  560. info.si_errno = 0;
  561. info.si_code = BUS_ADRALN;
  562. info.si_addr = (void __user *)addr;
  563. arm64_notify_die("Oops - SP/PC alignment exception", regs, &info, esr);
  564. }
  565. int __init early_brk64(unsigned long addr, unsigned int esr,
  566. struct pt_regs *regs);
  567. /*
  568. * __refdata because early_brk64 is __init, but the reference to it is
  569. * clobbered at arch_initcall time.
  570. * See traps.c and debug-monitors.c:debug_traps_init().
  571. */
  572. static struct fault_info __refdata debug_fault_info[] = {
  573. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
  574. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
  575. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
  576. { do_bad, SIGBUS, 0, "unknown 3" },
  577. { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
  578. { do_bad, SIGTRAP, 0, "aarch32 vector catch" },
  579. { early_brk64, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
  580. { do_bad, SIGBUS, 0, "unknown 7" },
  581. };
  582. void __init hook_debug_fault_code(int nr,
  583. int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  584. int sig, int code, const char *name)
  585. {
  586. BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
  587. debug_fault_info[nr].fn = fn;
  588. debug_fault_info[nr].sig = sig;
  589. debug_fault_info[nr].code = code;
  590. debug_fault_info[nr].name = name;
  591. }
  592. asmlinkage int __exception do_debug_exception(unsigned long addr,
  593. unsigned int esr,
  594. struct pt_regs *regs)
  595. {
  596. const struct fault_info *inf = debug_fault_info + DBG_ESR_EVT(esr);
  597. struct siginfo info;
  598. int rv;
  599. /*
  600. * Tell lockdep we disabled irqs in entry.S. Do nothing if they were
  601. * already disabled to preserve the last enabled/disabled addresses.
  602. */
  603. if (interrupts_enabled(regs))
  604. trace_hardirqs_off();
  605. if (user_mode(regs) && instruction_pointer(regs) > TASK_SIZE)
  606. arm64_apply_bp_hardening();
  607. if (!inf->fn(addr, esr, regs)) {
  608. rv = 1;
  609. } else {
  610. pr_alert("Unhandled debug exception: %s (0x%08x) at 0x%016lx\n",
  611. inf->name, esr, addr);
  612. info.si_signo = inf->sig;
  613. info.si_errno = 0;
  614. info.si_code = inf->code;
  615. info.si_addr = (void __user *)addr;
  616. arm64_notify_die("", regs, &info, 0);
  617. rv = 0;
  618. }
  619. if (interrupts_enabled(regs))
  620. trace_hardirqs_on();
  621. return rv;
  622. }
  623. NOKPROBE_SYMBOL(do_debug_exception);
  624. #ifdef CONFIG_ARM64_PAN
  625. int cpu_enable_pan(void *__unused)
  626. {
  627. /*
  628. * We modify PSTATE. This won't work from irq context as the PSTATE
  629. * is discarded once we return from the exception.
  630. */
  631. WARN_ON_ONCE(in_interrupt());
  632. config_sctlr_el1(SCTLR_EL1_SPAN, 0);
  633. asm(SET_PSTATE_PAN(1));
  634. return 0;
  635. }
  636. #endif /* CONFIG_ARM64_PAN */
  637. #ifdef CONFIG_ARM64_UAO
  638. /*
  639. * Kernel threads have fs=KERNEL_DS by default, and don't need to call
  640. * set_fs(), devtmpfs in particular relies on this behaviour.
  641. * We need to enable the feature at runtime (instead of adding it to
  642. * PSR_MODE_EL1h) as the feature may not be implemented by the cpu.
  643. */
  644. int cpu_enable_uao(void *__unused)
  645. {
  646. asm(SET_PSTATE_UAO(1));
  647. return 0;
  648. }
  649. #endif /* CONFIG_ARM64_UAO */