fault.c 13 KB

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