fault.c 14 KB

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