fault.c 20 KB

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