fault.c 18 KB

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