fault.c 19 KB

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