fault.c 21 KB

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