fault.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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/exception.h>
  32. #include <asm/debug-monitors.h>
  33. #include <asm/system_misc.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/tlbflush.h>
  36. static const char *fault_name(unsigned int esr);
  37. /*
  38. * Dump out the page tables associated with 'addr' in mm 'mm'.
  39. */
  40. void show_pte(struct mm_struct *mm, unsigned long addr)
  41. {
  42. pgd_t *pgd;
  43. if (!mm)
  44. mm = &init_mm;
  45. pr_alert("pgd = %p\n", mm->pgd);
  46. pgd = pgd_offset(mm, addr);
  47. pr_alert("[%08lx] *pgd=%016llx", addr, pgd_val(*pgd));
  48. do {
  49. pud_t *pud;
  50. pmd_t *pmd;
  51. pte_t *pte;
  52. if (pgd_none(*pgd) || pgd_bad(*pgd))
  53. break;
  54. pud = pud_offset(pgd, addr);
  55. if (pud_none(*pud) || pud_bad(*pud))
  56. break;
  57. pmd = pmd_offset(pud, addr);
  58. printk(", *pmd=%016llx", pmd_val(*pmd));
  59. if (pmd_none(*pmd) || pmd_bad(*pmd))
  60. break;
  61. pte = pte_offset_map(pmd, addr);
  62. printk(", *pte=%016llx", pte_val(*pte));
  63. pte_unmap(pte);
  64. } while(0);
  65. printk("\n");
  66. }
  67. /*
  68. * The kernel tried to access some page that wasn't present.
  69. */
  70. static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
  71. unsigned int esr, struct pt_regs *regs)
  72. {
  73. /*
  74. * Are we prepared to handle this kernel fault?
  75. */
  76. if (fixup_exception(regs))
  77. return;
  78. /*
  79. * No handler, we'll have to terminate things with extreme prejudice.
  80. */
  81. bust_spinlocks(1);
  82. pr_alert("Unable to handle kernel %s at virtual address %08lx\n",
  83. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  84. "paging request", addr);
  85. show_pte(mm, addr);
  86. die("Oops", regs, esr);
  87. bust_spinlocks(0);
  88. do_exit(SIGKILL);
  89. }
  90. /*
  91. * Something tried to access memory that isn't in our memory map. User mode
  92. * accesses just cause a SIGSEGV
  93. */
  94. static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
  95. unsigned int esr, unsigned int sig, int code,
  96. struct pt_regs *regs)
  97. {
  98. struct siginfo si;
  99. if (show_unhandled_signals && unhandled_signal(tsk, sig) &&
  100. printk_ratelimit()) {
  101. pr_info("%s[%d]: unhandled %s (%d) at 0x%08lx, esr 0x%03x\n",
  102. tsk->comm, task_pid_nr(tsk), fault_name(esr), sig,
  103. addr, esr);
  104. show_pte(tsk->mm, addr);
  105. show_regs(regs);
  106. }
  107. tsk->thread.fault_address = addr;
  108. si.si_signo = sig;
  109. si.si_errno = 0;
  110. si.si_code = code;
  111. si.si_addr = (void __user *)addr;
  112. force_sig_info(sig, &si, tsk);
  113. }
  114. void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  115. {
  116. struct task_struct *tsk = current;
  117. struct mm_struct *mm = tsk->active_mm;
  118. /*
  119. * If we are in kernel mode at this point, we have no context to
  120. * handle this fault with.
  121. */
  122. if (user_mode(regs))
  123. __do_user_fault(tsk, addr, esr, SIGSEGV, SEGV_MAPERR, regs);
  124. else
  125. __do_kernel_fault(mm, addr, esr, regs);
  126. }
  127. #define VM_FAULT_BADMAP 0x010000
  128. #define VM_FAULT_BADACCESS 0x020000
  129. #define ESR_WRITE (1 << 6)
  130. #define ESR_CM (1 << 8)
  131. #define ESR_LNX_EXEC (1 << 24)
  132. /*
  133. * Check that the permissions on the VMA allow for the fault which occurred.
  134. * If we encountered a write fault, we must have write permission, otherwise
  135. * we allow any permission.
  136. */
  137. static inline bool access_error(unsigned int esr, struct vm_area_struct *vma)
  138. {
  139. unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
  140. if (esr & ESR_WRITE)
  141. mask = VM_WRITE;
  142. if (esr & ESR_LNX_EXEC)
  143. mask = VM_EXEC;
  144. return vma->vm_flags & mask ? false : true;
  145. }
  146. static int __do_page_fault(struct mm_struct *mm, unsigned long addr,
  147. unsigned int esr, unsigned int flags,
  148. struct task_struct *tsk)
  149. {
  150. struct vm_area_struct *vma;
  151. int fault;
  152. vma = find_vma(mm, addr);
  153. fault = VM_FAULT_BADMAP;
  154. if (unlikely(!vma))
  155. goto out;
  156. if (unlikely(vma->vm_start > addr))
  157. goto check_stack;
  158. /*
  159. * Ok, we have a good vm_area for this memory access, so we can handle
  160. * it.
  161. */
  162. good_area:
  163. if (access_error(esr, vma)) {
  164. fault = VM_FAULT_BADACCESS;
  165. goto out;
  166. }
  167. return handle_mm_fault(mm, vma, addr & PAGE_MASK, flags);
  168. check_stack:
  169. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  170. goto good_area;
  171. out:
  172. return fault;
  173. }
  174. static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
  175. struct pt_regs *regs)
  176. {
  177. struct task_struct *tsk;
  178. struct mm_struct *mm;
  179. int fault, sig, code;
  180. bool write = (esr & ESR_WRITE) && !(esr & ESR_CM);
  181. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  182. (write ? FAULT_FLAG_WRITE : 0);
  183. tsk = current;
  184. mm = tsk->mm;
  185. /* Enable interrupts if they were enabled in the parent context. */
  186. if (interrupts_enabled(regs))
  187. local_irq_enable();
  188. /*
  189. * If we're in an interrupt or have no user context, we must not take
  190. * the fault.
  191. */
  192. if (in_atomic() || !mm)
  193. goto no_context;
  194. /*
  195. * As per x86, we may deadlock here. However, since the kernel only
  196. * validly references user space from well defined areas of the code,
  197. * we can bug out early if this is from code which shouldn't.
  198. */
  199. if (!down_read_trylock(&mm->mmap_sem)) {
  200. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  201. goto no_context;
  202. retry:
  203. down_read(&mm->mmap_sem);
  204. } else {
  205. /*
  206. * The above down_read_trylock() might have succeeded in which
  207. * case, we'll have missed the might_sleep() from down_read().
  208. */
  209. might_sleep();
  210. #ifdef CONFIG_DEBUG_VM
  211. if (!user_mode(regs) && !search_exception_tables(regs->pc))
  212. goto no_context;
  213. #endif
  214. }
  215. fault = __do_page_fault(mm, addr, esr, flags, tsk);
  216. /*
  217. * If we need to retry but a fatal signal is pending, handle the
  218. * signal first. We do not need to release the mmap_sem because it
  219. * would already be released in __lock_page_or_retry in mm/filemap.c.
  220. */
  221. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  222. return 0;
  223. /*
  224. * Major/minor page fault accounting is only done on the initial
  225. * attempt. If we go through a retry, it is extremely likely that the
  226. * page will be found in page cache at that point.
  227. */
  228. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  229. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  230. if (fault & VM_FAULT_MAJOR) {
  231. tsk->maj_flt++;
  232. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs,
  233. addr);
  234. } else {
  235. tsk->min_flt++;
  236. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs,
  237. addr);
  238. }
  239. if (fault & VM_FAULT_RETRY) {
  240. /*
  241. * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk of
  242. * starvation.
  243. */
  244. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  245. goto retry;
  246. }
  247. }
  248. up_read(&mm->mmap_sem);
  249. /*
  250. * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
  251. */
  252. if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP |
  253. VM_FAULT_BADACCESS))))
  254. return 0;
  255. if (fault & VM_FAULT_OOM) {
  256. /*
  257. * We ran out of memory, call the OOM killer, and return to
  258. * userspace (which will retry the fault, or kill us if we got
  259. * oom-killed).
  260. */
  261. pagefault_out_of_memory();
  262. return 0;
  263. }
  264. /*
  265. * If we are in kernel mode at this point, we have no context to
  266. * handle this fault with.
  267. */
  268. if (!user_mode(regs))
  269. goto no_context;
  270. if (fault & VM_FAULT_SIGBUS) {
  271. /*
  272. * We had some memory, but were unable to successfully fix up
  273. * this page fault.
  274. */
  275. sig = SIGBUS;
  276. code = BUS_ADRERR;
  277. } else {
  278. /*
  279. * Something tried to access memory that isn't in our memory
  280. * map.
  281. */
  282. sig = SIGSEGV;
  283. code = fault == VM_FAULT_BADACCESS ?
  284. SEGV_ACCERR : SEGV_MAPERR;
  285. }
  286. __do_user_fault(tsk, addr, esr, sig, code, regs);
  287. return 0;
  288. no_context:
  289. __do_kernel_fault(mm, addr, esr, regs);
  290. return 0;
  291. }
  292. /*
  293. * First Level Translation Fault Handler
  294. *
  295. * We enter here because the first level page table doesn't contain a valid
  296. * entry for the address.
  297. *
  298. * If the address is in kernel space (>= TASK_SIZE), then we are probably
  299. * faulting in the vmalloc() area.
  300. *
  301. * If the init_task's first level page tables contains the relevant entry, we
  302. * copy the it to this task. If not, we send the process a signal, fixup the
  303. * exception, or oops the kernel.
  304. *
  305. * NOTE! We MUST NOT take any locks for this case. We may be in an interrupt
  306. * or a critical region, and should only copy the information from the master
  307. * page table, nothing more.
  308. */
  309. static int __kprobes do_translation_fault(unsigned long addr,
  310. unsigned int esr,
  311. struct pt_regs *regs)
  312. {
  313. if (addr < TASK_SIZE)
  314. return do_page_fault(addr, esr, regs);
  315. do_bad_area(addr, esr, regs);
  316. return 0;
  317. }
  318. /*
  319. * Some section permission faults need to be handled gracefully. They can
  320. * happen due to a __{get,put}_user during an oops.
  321. */
  322. static int do_sect_fault(unsigned long addr, unsigned int esr,
  323. struct pt_regs *regs)
  324. {
  325. do_bad_area(addr, esr, regs);
  326. return 0;
  327. }
  328. /*
  329. * This abort handler always returns "fault".
  330. */
  331. static int do_bad(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  332. {
  333. return 1;
  334. }
  335. static struct fault_info {
  336. int (*fn)(unsigned long addr, unsigned int esr, struct pt_regs *regs);
  337. int sig;
  338. int code;
  339. const char *name;
  340. } fault_info[] = {
  341. { do_bad, SIGBUS, 0, "ttbr address size fault" },
  342. { do_bad, SIGBUS, 0, "level 1 address size fault" },
  343. { do_bad, SIGBUS, 0, "level 2 address size fault" },
  344. { do_bad, SIGBUS, 0, "level 3 address size fault" },
  345. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "input address range fault" },
  346. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 1 translation fault" },
  347. { do_translation_fault, SIGSEGV, SEGV_MAPERR, "level 2 translation fault" },
  348. { do_page_fault, SIGSEGV, SEGV_MAPERR, "level 3 translation fault" },
  349. { do_bad, SIGBUS, 0, "reserved access flag fault" },
  350. { do_bad, SIGSEGV, SEGV_ACCERR, "level 1 access flag fault" },
  351. { do_bad, SIGSEGV, SEGV_ACCERR, "level 2 access flag fault" },
  352. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 access flag fault" },
  353. { do_bad, SIGBUS, 0, "reserved permission fault" },
  354. { do_bad, SIGSEGV, SEGV_ACCERR, "level 1 permission fault" },
  355. { do_sect_fault, SIGSEGV, SEGV_ACCERR, "level 2 permission fault" },
  356. { do_page_fault, SIGSEGV, SEGV_ACCERR, "level 3 permission fault" },
  357. { do_bad, SIGBUS, 0, "synchronous external abort" },
  358. { do_bad, SIGBUS, 0, "asynchronous external abort" },
  359. { do_bad, SIGBUS, 0, "unknown 18" },
  360. { do_bad, SIGBUS, 0, "unknown 19" },
  361. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  362. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  363. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  364. { do_bad, SIGBUS, 0, "synchronous abort (translation table walk)" },
  365. { do_bad, SIGBUS, 0, "synchronous parity error" },
  366. { do_bad, SIGBUS, 0, "asynchronous parity error" },
  367. { do_bad, SIGBUS, 0, "unknown 26" },
  368. { do_bad, SIGBUS, 0, "unknown 27" },
  369. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk" },
  370. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk" },
  371. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk" },
  372. { do_bad, SIGBUS, 0, "synchronous parity error (translation table walk" },
  373. { do_bad, SIGBUS, 0, "unknown 32" },
  374. { do_bad, SIGBUS, BUS_ADRALN, "alignment fault" },
  375. { do_bad, SIGBUS, 0, "debug event" },
  376. { do_bad, SIGBUS, 0, "unknown 35" },
  377. { do_bad, SIGBUS, 0, "unknown 36" },
  378. { do_bad, SIGBUS, 0, "unknown 37" },
  379. { do_bad, SIGBUS, 0, "unknown 38" },
  380. { do_bad, SIGBUS, 0, "unknown 39" },
  381. { do_bad, SIGBUS, 0, "unknown 40" },
  382. { do_bad, SIGBUS, 0, "unknown 41" },
  383. { do_bad, SIGBUS, 0, "unknown 42" },
  384. { do_bad, SIGBUS, 0, "unknown 43" },
  385. { do_bad, SIGBUS, 0, "unknown 44" },
  386. { do_bad, SIGBUS, 0, "unknown 45" },
  387. { do_bad, SIGBUS, 0, "unknown 46" },
  388. { do_bad, SIGBUS, 0, "unknown 47" },
  389. { do_bad, SIGBUS, 0, "unknown 48" },
  390. { do_bad, SIGBUS, 0, "unknown 49" },
  391. { do_bad, SIGBUS, 0, "unknown 50" },
  392. { do_bad, SIGBUS, 0, "unknown 51" },
  393. { do_bad, SIGBUS, 0, "implementation fault (lockdown abort)" },
  394. { do_bad, SIGBUS, 0, "unknown 53" },
  395. { do_bad, SIGBUS, 0, "unknown 54" },
  396. { do_bad, SIGBUS, 0, "unknown 55" },
  397. { do_bad, SIGBUS, 0, "unknown 56" },
  398. { do_bad, SIGBUS, 0, "unknown 57" },
  399. { do_bad, SIGBUS, 0, "implementation fault (coprocessor abort)" },
  400. { do_bad, SIGBUS, 0, "unknown 59" },
  401. { do_bad, SIGBUS, 0, "unknown 60" },
  402. { do_bad, SIGBUS, 0, "unknown 61" },
  403. { do_bad, SIGBUS, 0, "unknown 62" },
  404. { do_bad, SIGBUS, 0, "unknown 63" },
  405. };
  406. static const char *fault_name(unsigned int esr)
  407. {
  408. const struct fault_info *inf = fault_info + (esr & 63);
  409. return inf->name;
  410. }
  411. /*
  412. * Dispatch a data abort to the relevant handler.
  413. */
  414. asmlinkage void __exception do_mem_abort(unsigned long addr, unsigned int esr,
  415. struct pt_regs *regs)
  416. {
  417. const struct fault_info *inf = fault_info + (esr & 63);
  418. struct siginfo info;
  419. if (!inf->fn(addr, esr, regs))
  420. return;
  421. pr_alert("Unhandled fault: %s (0x%08x) at 0x%016lx\n",
  422. inf->name, esr, addr);
  423. info.si_signo = inf->sig;
  424. info.si_errno = 0;
  425. info.si_code = inf->code;
  426. info.si_addr = (void __user *)addr;
  427. arm64_notify_die("", regs, &info, esr);
  428. }
  429. /*
  430. * Handle stack alignment exceptions.
  431. */
  432. asmlinkage void __exception do_sp_pc_abort(unsigned long addr,
  433. unsigned int esr,
  434. struct pt_regs *regs)
  435. {
  436. struct siginfo info;
  437. info.si_signo = SIGBUS;
  438. info.si_errno = 0;
  439. info.si_code = BUS_ADRALN;
  440. info.si_addr = (void __user *)addr;
  441. arm64_notify_die("", regs, &info, esr);
  442. }
  443. static struct fault_info debug_fault_info[] = {
  444. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware breakpoint" },
  445. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware single-step" },
  446. { do_bad, SIGTRAP, TRAP_HWBKPT, "hardware watchpoint" },
  447. { do_bad, SIGBUS, 0, "unknown 3" },
  448. { do_bad, SIGTRAP, TRAP_BRKPT, "aarch32 BKPT" },
  449. { do_bad, SIGTRAP, 0, "aarch32 vector catch" },
  450. { do_bad, SIGTRAP, TRAP_BRKPT, "aarch64 BRK" },
  451. { do_bad, SIGBUS, 0, "unknown 7" },
  452. };
  453. void __init hook_debug_fault_code(int nr,
  454. int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  455. int sig, int code, const char *name)
  456. {
  457. BUG_ON(nr < 0 || nr >= ARRAY_SIZE(debug_fault_info));
  458. debug_fault_info[nr].fn = fn;
  459. debug_fault_info[nr].sig = sig;
  460. debug_fault_info[nr].code = code;
  461. debug_fault_info[nr].name = name;
  462. }
  463. asmlinkage int __exception do_debug_exception(unsigned long addr,
  464. unsigned int esr,
  465. struct pt_regs *regs)
  466. {
  467. const struct fault_info *inf = debug_fault_info + DBG_ESR_EVT(esr);
  468. struct siginfo info;
  469. if (!inf->fn(addr, esr, regs))
  470. return 1;
  471. pr_alert("Unhandled debug exception: %s (0x%08x) at 0x%016lx\n",
  472. inf->name, esr, addr);
  473. info.si_signo = inf->sig;
  474. info.si_errno = 0;
  475. info.si_code = inf->code;
  476. info.si_addr = (void __user *)addr;
  477. arm64_notify_die("", regs, &info, esr);
  478. return 0;
  479. }