fault.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * linux/arch/arm/mm/fault.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. * Modifications for ARM processor (c) 1995-2004 Russell King
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/signal.h>
  13. #include <linux/mm.h>
  14. #include <linux/hardirq.h>
  15. #include <linux/init.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/page-flags.h>
  19. #include <linux/sched.h>
  20. #include <linux/highmem.h>
  21. #include <linux/perf_event.h>
  22. #include <asm/exception.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/system_misc.h>
  25. #include <asm/system_info.h>
  26. #include <asm/tlbflush.h>
  27. #include "fault.h"
  28. #ifdef CONFIG_MMU
  29. #ifdef CONFIG_KPROBES
  30. static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
  31. {
  32. int ret = 0;
  33. if (!user_mode(regs)) {
  34. /* kprobe_running() needs smp_processor_id() */
  35. preempt_disable();
  36. if (kprobe_running() && kprobe_fault_handler(regs, fsr))
  37. ret = 1;
  38. preempt_enable();
  39. }
  40. return ret;
  41. }
  42. #else
  43. static inline int notify_page_fault(struct pt_regs *regs, unsigned int fsr)
  44. {
  45. return 0;
  46. }
  47. #endif
  48. /*
  49. * This is useful to dump out the page tables associated with
  50. * 'addr' in mm 'mm'.
  51. */
  52. void show_pte(struct mm_struct *mm, unsigned long addr)
  53. {
  54. pgd_t *pgd;
  55. if (!mm)
  56. mm = &init_mm;
  57. printk(KERN_ALERT "pgd = %p\n", mm->pgd);
  58. pgd = pgd_offset(mm, addr);
  59. printk(KERN_ALERT "[%08lx] *pgd=%08llx",
  60. addr, (long long)pgd_val(*pgd));
  61. do {
  62. pud_t *pud;
  63. pmd_t *pmd;
  64. pte_t *pte;
  65. if (pgd_none(*pgd))
  66. break;
  67. if (pgd_bad(*pgd)) {
  68. printk("(bad)");
  69. break;
  70. }
  71. pud = pud_offset(pgd, addr);
  72. if (PTRS_PER_PUD != 1)
  73. printk(", *pud=%08llx", (long long)pud_val(*pud));
  74. if (pud_none(*pud))
  75. break;
  76. if (pud_bad(*pud)) {
  77. printk("(bad)");
  78. break;
  79. }
  80. pmd = pmd_offset(pud, addr);
  81. if (PTRS_PER_PMD != 1)
  82. printk(", *pmd=%08llx", (long long)pmd_val(*pmd));
  83. if (pmd_none(*pmd))
  84. break;
  85. if (pmd_bad(*pmd)) {
  86. printk("(bad)");
  87. break;
  88. }
  89. /* We must not map this if we have highmem enabled */
  90. if (PageHighMem(pfn_to_page(pmd_val(*pmd) >> PAGE_SHIFT)))
  91. break;
  92. pte = pte_offset_map(pmd, addr);
  93. printk(", *pte=%08llx", (long long)pte_val(*pte));
  94. #ifndef CONFIG_ARM_LPAE
  95. printk(", *ppte=%08llx",
  96. (long long)pte_val(pte[PTE_HWTABLE_PTRS]));
  97. #endif
  98. pte_unmap(pte);
  99. } while(0);
  100. printk("\n");
  101. }
  102. #else /* CONFIG_MMU */
  103. void show_pte(struct mm_struct *mm, unsigned long addr)
  104. { }
  105. #endif /* CONFIG_MMU */
  106. /*
  107. * Oops. The kernel tried to access some page that wasn't present.
  108. */
  109. static void
  110. __do_kernel_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  111. struct pt_regs *regs)
  112. {
  113. /*
  114. * Are we prepared to handle this kernel fault?
  115. */
  116. if (fixup_exception(regs))
  117. return;
  118. /*
  119. * No handler, we'll have to terminate things with extreme prejudice.
  120. */
  121. bust_spinlocks(1);
  122. printk(KERN_ALERT
  123. "Unable to handle kernel %s at virtual address %08lx\n",
  124. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  125. "paging request", addr);
  126. show_pte(mm, addr);
  127. die("Oops", regs, fsr);
  128. bust_spinlocks(0);
  129. do_exit(SIGKILL);
  130. }
  131. /*
  132. * Something tried to access memory that isn't in our memory map..
  133. * User mode accesses just cause a SIGSEGV
  134. */
  135. static void
  136. __do_user_fault(struct task_struct *tsk, unsigned long addr,
  137. unsigned int fsr, unsigned int sig, int code,
  138. struct pt_regs *regs)
  139. {
  140. struct siginfo si;
  141. #ifdef CONFIG_DEBUG_USER
  142. if (((user_debug & UDBG_SEGV) && (sig == SIGSEGV)) ||
  143. ((user_debug & UDBG_BUS) && (sig == SIGBUS))) {
  144. printk(KERN_DEBUG "%s: unhandled page fault (%d) at 0x%08lx, code 0x%03x\n",
  145. tsk->comm, sig, addr, fsr);
  146. show_pte(tsk->mm, addr);
  147. show_regs(regs);
  148. }
  149. #endif
  150. tsk->thread.address = addr;
  151. tsk->thread.error_code = fsr;
  152. tsk->thread.trap_no = 14;
  153. si.si_signo = sig;
  154. si.si_errno = 0;
  155. si.si_code = code;
  156. si.si_addr = (void __user *)addr;
  157. force_sig_info(sig, &si, tsk);
  158. }
  159. void do_bad_area(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  160. {
  161. struct task_struct *tsk = current;
  162. struct mm_struct *mm = tsk->active_mm;
  163. /*
  164. * If we are in kernel mode at this point, we
  165. * have no context to handle this fault with.
  166. */
  167. if (user_mode(regs))
  168. __do_user_fault(tsk, addr, fsr, SIGSEGV, SEGV_MAPERR, regs);
  169. else
  170. __do_kernel_fault(mm, addr, fsr, regs);
  171. }
  172. #ifdef CONFIG_MMU
  173. #define VM_FAULT_BADMAP 0x010000
  174. #define VM_FAULT_BADACCESS 0x020000
  175. /*
  176. * Check that the permissions on the VMA allow for the fault which occurred.
  177. * If we encountered a write fault, we must have write permission, otherwise
  178. * we allow any permission.
  179. */
  180. static inline bool access_error(unsigned int fsr, struct vm_area_struct *vma)
  181. {
  182. unsigned int mask = VM_READ | VM_WRITE | VM_EXEC;
  183. if (fsr & FSR_WRITE)
  184. mask = VM_WRITE;
  185. if (fsr & FSR_LNX_PF)
  186. mask = VM_EXEC;
  187. return vma->vm_flags & mask ? false : true;
  188. }
  189. static int __kprobes
  190. __do_page_fault(struct mm_struct *mm, unsigned long addr, unsigned int fsr,
  191. unsigned int flags, struct task_struct *tsk)
  192. {
  193. struct vm_area_struct *vma;
  194. int fault;
  195. vma = find_vma(mm, addr);
  196. fault = VM_FAULT_BADMAP;
  197. if (unlikely(!vma))
  198. goto out;
  199. if (unlikely(vma->vm_start > addr))
  200. goto check_stack;
  201. /*
  202. * Ok, we have a good vm_area for this
  203. * memory access, so we can handle it.
  204. */
  205. good_area:
  206. if (access_error(fsr, vma)) {
  207. fault = VM_FAULT_BADACCESS;
  208. goto out;
  209. }
  210. return handle_mm_fault(mm, vma, addr & PAGE_MASK, flags);
  211. check_stack:
  212. /* Don't allow expansion below FIRST_USER_ADDRESS */
  213. if (vma->vm_flags & VM_GROWSDOWN &&
  214. addr >= FIRST_USER_ADDRESS && !expand_stack(vma, addr))
  215. goto good_area;
  216. out:
  217. return fault;
  218. }
  219. static int __kprobes
  220. do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  221. {
  222. struct task_struct *tsk;
  223. struct mm_struct *mm;
  224. int fault, sig, code;
  225. int write = fsr & FSR_WRITE;
  226. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
  227. (write ? FAULT_FLAG_WRITE : 0);
  228. if (notify_page_fault(regs, fsr))
  229. return 0;
  230. tsk = current;
  231. mm = tsk->mm;
  232. /* Enable interrupts if they were enabled in the parent context. */
  233. if (interrupts_enabled(regs))
  234. local_irq_enable();
  235. /*
  236. * If we're in an interrupt or have no user
  237. * context, we must not take the fault..
  238. */
  239. if (in_atomic() || !mm)
  240. goto no_context;
  241. /*
  242. * As per x86, we may deadlock here. However, since the kernel only
  243. * validly references user space from well defined areas of the code,
  244. * we can bug out early if this is from code which shouldn't.
  245. */
  246. if (!down_read_trylock(&mm->mmap_sem)) {
  247. if (!user_mode(regs) && !search_exception_tables(regs->ARM_pc))
  248. goto no_context;
  249. retry:
  250. down_read(&mm->mmap_sem);
  251. } else {
  252. /*
  253. * The above down_read_trylock() might have succeeded in
  254. * which case, we'll have missed the might_sleep() from
  255. * down_read()
  256. */
  257. might_sleep();
  258. #ifdef CONFIG_DEBUG_VM
  259. if (!user_mode(regs) &&
  260. !search_exception_tables(regs->ARM_pc))
  261. goto no_context;
  262. #endif
  263. }
  264. fault = __do_page_fault(mm, addr, fsr, flags, tsk);
  265. /* If we need to retry but a fatal signal is pending, handle the
  266. * signal first. We do not need to release the mmap_sem because
  267. * it would already be released in __lock_page_or_retry in
  268. * mm/filemap.c. */
  269. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  270. return 0;
  271. /*
  272. * Major/minor page fault accounting is only done on the
  273. * initial attempt. If we go through a retry, it is extremely
  274. * likely that the page will be found in page cache at that point.
  275. */
  276. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  277. if (!(fault & VM_FAULT_ERROR) && flags & FAULT_FLAG_ALLOW_RETRY) {
  278. if (fault & VM_FAULT_MAJOR) {
  279. tsk->maj_flt++;
  280. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  281. regs, addr);
  282. } else {
  283. tsk->min_flt++;
  284. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  285. regs, addr);
  286. }
  287. if (fault & VM_FAULT_RETRY) {
  288. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  289. * of starvation. */
  290. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  291. goto retry;
  292. }
  293. }
  294. up_read(&mm->mmap_sem);
  295. /*
  296. * Handle the "normal" case first - VM_FAULT_MAJOR / VM_FAULT_MINOR
  297. */
  298. if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
  299. return 0;
  300. if (fault & VM_FAULT_OOM) {
  301. /*
  302. * We ran out of memory, call the OOM killer, and return to
  303. * userspace (which will retry the fault, or kill us if we
  304. * got oom-killed)
  305. */
  306. pagefault_out_of_memory();
  307. return 0;
  308. }
  309. /*
  310. * If we are in kernel mode at this point, we
  311. * have no context to handle this fault with.
  312. */
  313. if (!user_mode(regs))
  314. goto no_context;
  315. if (fault & VM_FAULT_SIGBUS) {
  316. /*
  317. * We had some memory, but were unable to
  318. * successfully fix up this page fault.
  319. */
  320. sig = SIGBUS;
  321. code = BUS_ADRERR;
  322. } else {
  323. /*
  324. * Something tried to access memory that
  325. * isn't in our memory map..
  326. */
  327. sig = SIGSEGV;
  328. code = fault == VM_FAULT_BADACCESS ?
  329. SEGV_ACCERR : SEGV_MAPERR;
  330. }
  331. __do_user_fault(tsk, addr, fsr, sig, code, regs);
  332. return 0;
  333. no_context:
  334. __do_kernel_fault(mm, addr, fsr, regs);
  335. return 0;
  336. }
  337. #else /* CONFIG_MMU */
  338. static int
  339. do_page_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  340. {
  341. return 0;
  342. }
  343. #endif /* CONFIG_MMU */
  344. /*
  345. * First Level Translation Fault Handler
  346. *
  347. * We enter here because the first level page table doesn't contain
  348. * a valid entry for the address.
  349. *
  350. * If the address is in kernel space (>= TASK_SIZE), then we are
  351. * probably faulting in the vmalloc() area.
  352. *
  353. * If the init_task's first level page tables contains the relevant
  354. * entry, we copy the it to this task. If not, we send the process
  355. * a signal, fixup the exception, or oops the kernel.
  356. *
  357. * NOTE! We MUST NOT take any locks for this case. We may be in an
  358. * interrupt or a critical region, and should only copy the information
  359. * from the master page table, nothing more.
  360. */
  361. #ifdef CONFIG_MMU
  362. static int __kprobes
  363. do_translation_fault(unsigned long addr, unsigned int fsr,
  364. struct pt_regs *regs)
  365. {
  366. unsigned int index;
  367. pgd_t *pgd, *pgd_k;
  368. pud_t *pud, *pud_k;
  369. pmd_t *pmd, *pmd_k;
  370. if (addr < TASK_SIZE)
  371. return do_page_fault(addr, fsr, regs);
  372. if (user_mode(regs))
  373. goto bad_area;
  374. index = pgd_index(addr);
  375. pgd = cpu_get_pgd() + index;
  376. pgd_k = init_mm.pgd + index;
  377. if (pgd_none(*pgd_k))
  378. goto bad_area;
  379. if (!pgd_present(*pgd))
  380. set_pgd(pgd, *pgd_k);
  381. pud = pud_offset(pgd, addr);
  382. pud_k = pud_offset(pgd_k, addr);
  383. if (pud_none(*pud_k))
  384. goto bad_area;
  385. if (!pud_present(*pud))
  386. set_pud(pud, *pud_k);
  387. pmd = pmd_offset(pud, addr);
  388. pmd_k = pmd_offset(pud_k, addr);
  389. #ifdef CONFIG_ARM_LPAE
  390. /*
  391. * Only one hardware entry per PMD with LPAE.
  392. */
  393. index = 0;
  394. #else
  395. /*
  396. * On ARM one Linux PGD entry contains two hardware entries (see page
  397. * tables layout in pgtable.h). We normally guarantee that we always
  398. * fill both L1 entries. But create_mapping() doesn't follow the rule.
  399. * It can create inidividual L1 entries, so here we have to call
  400. * pmd_none() check for the entry really corresponded to address, not
  401. * for the first of pair.
  402. */
  403. index = (addr >> SECTION_SHIFT) & 1;
  404. #endif
  405. if (pmd_none(pmd_k[index]))
  406. goto bad_area;
  407. copy_pmd(pmd, pmd_k);
  408. return 0;
  409. bad_area:
  410. do_bad_area(addr, fsr, regs);
  411. return 0;
  412. }
  413. #else /* CONFIG_MMU */
  414. static int
  415. do_translation_fault(unsigned long addr, unsigned int fsr,
  416. struct pt_regs *regs)
  417. {
  418. return 0;
  419. }
  420. #endif /* CONFIG_MMU */
  421. /*
  422. * Some section permission faults need to be handled gracefully.
  423. * They can happen due to a __{get,put}_user during an oops.
  424. */
  425. static int
  426. do_sect_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  427. {
  428. do_bad_area(addr, fsr, regs);
  429. return 0;
  430. }
  431. /*
  432. * This abort handler always returns "fault".
  433. */
  434. static int
  435. do_bad(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  436. {
  437. return 1;
  438. }
  439. struct fsr_info {
  440. int (*fn)(unsigned long addr, unsigned int fsr, struct pt_regs *regs);
  441. int sig;
  442. int code;
  443. const char *name;
  444. };
  445. /* FSR definition */
  446. #ifdef CONFIG_ARM_LPAE
  447. #include "fsr-3level.c"
  448. #else
  449. #include "fsr-2level.c"
  450. #endif
  451. void __init
  452. hook_fault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  453. int sig, int code, const char *name)
  454. {
  455. if (nr < 0 || nr >= ARRAY_SIZE(fsr_info))
  456. BUG();
  457. fsr_info[nr].fn = fn;
  458. fsr_info[nr].sig = sig;
  459. fsr_info[nr].code = code;
  460. fsr_info[nr].name = name;
  461. }
  462. /*
  463. * Dispatch a data abort to the relevant handler.
  464. */
  465. asmlinkage void __exception
  466. do_DataAbort(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  467. {
  468. const struct fsr_info *inf = fsr_info + fsr_fs(fsr);
  469. struct siginfo info;
  470. if (!inf->fn(addr, fsr & ~FSR_LNX_PF, regs))
  471. return;
  472. printk(KERN_ALERT "Unhandled fault: %s (0x%03x) at 0x%08lx\n",
  473. inf->name, fsr, 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. arm_notify_die("", regs, &info, fsr, 0);
  479. }
  480. void __init
  481. hook_ifault_code(int nr, int (*fn)(unsigned long, unsigned int, struct pt_regs *),
  482. int sig, int code, const char *name)
  483. {
  484. if (nr < 0 || nr >= ARRAY_SIZE(ifsr_info))
  485. BUG();
  486. ifsr_info[nr].fn = fn;
  487. ifsr_info[nr].sig = sig;
  488. ifsr_info[nr].code = code;
  489. ifsr_info[nr].name = name;
  490. }
  491. asmlinkage void __exception
  492. do_PrefetchAbort(unsigned long addr, unsigned int ifsr, struct pt_regs *regs)
  493. {
  494. const struct fsr_info *inf = ifsr_info + fsr_fs(ifsr);
  495. struct siginfo info;
  496. if (!inf->fn(addr, ifsr | FSR_LNX_PF, regs))
  497. return;
  498. printk(KERN_ALERT "Unhandled prefetch abort: %s (0x%03x) at 0x%08lx\n",
  499. inf->name, ifsr, addr);
  500. info.si_signo = inf->sig;
  501. info.si_errno = 0;
  502. info.si_code = inf->code;
  503. info.si_addr = (void __user *)addr;
  504. arm_notify_die("", regs, &info, ifsr, 0);
  505. }
  506. #ifndef CONFIG_ARM_LPAE
  507. static int __init exceptions_init(void)
  508. {
  509. if (cpu_architecture() >= CPU_ARCH_ARMv6) {
  510. hook_fault_code(4, do_translation_fault, SIGSEGV, SEGV_MAPERR,
  511. "I-cache maintenance fault");
  512. }
  513. if (cpu_architecture() >= CPU_ARCH_ARMv7) {
  514. /*
  515. * TODO: Access flag faults introduced in ARMv6K.
  516. * Runtime check for 'K' extension is needed
  517. */
  518. hook_fault_code(3, do_bad, SIGSEGV, SEGV_MAPERR,
  519. "section access flag fault");
  520. hook_fault_code(6, do_bad, SIGSEGV, SEGV_MAPERR,
  521. "section access flag fault");
  522. }
  523. return 0;
  524. }
  525. arch_initcall(exceptions_init);
  526. #endif