fault.c 15 KB

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