fault.c 20 KB

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