fault.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2009 Wind River Systems Inc
  3. * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
  4. *
  5. * based on arch/mips/mm/fault.c which is:
  6. *
  7. * Copyright (C) 1995-2000 Ralf Baechle
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <linux/signal.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/debug.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/string.h>
  20. #include <linux/types.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/mman.h>
  23. #include <linux/mm.h>
  24. #include <linux/extable.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/ptrace.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/traps.h>
  29. #define EXC_SUPERV_INSN_ACCESS 9 /* Supervisor only instruction address */
  30. #define EXC_SUPERV_DATA_ACCESS 11 /* Supervisor only data address */
  31. #define EXC_X_PROTECTION_FAULT 13 /* TLB permission violation (x) */
  32. #define EXC_R_PROTECTION_FAULT 14 /* TLB permission violation (r) */
  33. #define EXC_W_PROTECTION_FAULT 15 /* TLB permission violation (w) */
  34. /*
  35. * This routine handles page faults. It determines the address,
  36. * and the problem, and then passes it off to one of the appropriate
  37. * routines.
  38. */
  39. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,
  40. unsigned long address)
  41. {
  42. struct vm_area_struct *vma = NULL;
  43. struct task_struct *tsk = current;
  44. struct mm_struct *mm = tsk->mm;
  45. int code = SEGV_MAPERR;
  46. int fault;
  47. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  48. cause >>= 2;
  49. /* Restart the instruction */
  50. regs->ea -= 4;
  51. /*
  52. * We fault-in kernel-space virtual memory on-demand. The
  53. * 'reference' page table is init_mm.pgd.
  54. *
  55. * NOTE! We MUST NOT take any locks for this case. We may
  56. * be in an interrupt or a critical region, and should
  57. * only copy the information from the master page table,
  58. * nothing more.
  59. */
  60. if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END)) {
  61. if (user_mode(regs))
  62. goto bad_area_nosemaphore;
  63. else
  64. goto vmalloc_fault;
  65. }
  66. if (unlikely(address >= TASK_SIZE))
  67. goto bad_area_nosemaphore;
  68. /*
  69. * If we're in an interrupt or have no user
  70. * context, we must not take the fault..
  71. */
  72. if (faulthandler_disabled() || !mm)
  73. goto bad_area_nosemaphore;
  74. if (user_mode(regs))
  75. flags |= FAULT_FLAG_USER;
  76. if (!down_read_trylock(&mm->mmap_sem)) {
  77. if (!user_mode(regs) && !search_exception_tables(regs->ea))
  78. goto bad_area_nosemaphore;
  79. retry:
  80. down_read(&mm->mmap_sem);
  81. }
  82. vma = find_vma(mm, address);
  83. if (!vma)
  84. goto bad_area;
  85. if (vma->vm_start <= address)
  86. goto good_area;
  87. if (!(vma->vm_flags & VM_GROWSDOWN))
  88. goto bad_area;
  89. if (expand_stack(vma, address))
  90. goto bad_area;
  91. /*
  92. * Ok, we have a good vm_area for this memory access, so
  93. * we can handle it..
  94. */
  95. good_area:
  96. code = SEGV_ACCERR;
  97. switch (cause) {
  98. case EXC_SUPERV_INSN_ACCESS:
  99. goto bad_area;
  100. case EXC_SUPERV_DATA_ACCESS:
  101. goto bad_area;
  102. case EXC_X_PROTECTION_FAULT:
  103. if (!(vma->vm_flags & VM_EXEC))
  104. goto bad_area;
  105. break;
  106. case EXC_R_PROTECTION_FAULT:
  107. if (!(vma->vm_flags & VM_READ))
  108. goto bad_area;
  109. break;
  110. case EXC_W_PROTECTION_FAULT:
  111. if (!(vma->vm_flags & VM_WRITE))
  112. goto bad_area;
  113. flags = FAULT_FLAG_WRITE;
  114. break;
  115. }
  116. /*
  117. * If for any reason at all we couldn't handle the fault,
  118. * make sure we exit gracefully rather than endlessly redo
  119. * the fault.
  120. */
  121. fault = handle_mm_fault(vma, address, flags);
  122. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  123. return;
  124. if (unlikely(fault & VM_FAULT_ERROR)) {
  125. if (fault & VM_FAULT_OOM)
  126. goto out_of_memory;
  127. else if (fault & VM_FAULT_SIGSEGV)
  128. goto bad_area;
  129. else if (fault & VM_FAULT_SIGBUS)
  130. goto do_sigbus;
  131. BUG();
  132. }
  133. /*
  134. * Major/minor page fault accounting is only done on the
  135. * initial attempt. If we go through a retry, it is extremely
  136. * likely that the page will be found in page cache at that point.
  137. */
  138. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  139. if (fault & VM_FAULT_MAJOR)
  140. current->maj_flt++;
  141. else
  142. current->min_flt++;
  143. if (fault & VM_FAULT_RETRY) {
  144. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  145. * of starvation. */
  146. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  147. flags |= FAULT_FLAG_TRIED;
  148. /*
  149. * No need to up_read(&mm->mmap_sem) as we would
  150. * have already released it in __lock_page_or_retry
  151. * in mm/filemap.c.
  152. */
  153. goto retry;
  154. }
  155. }
  156. up_read(&mm->mmap_sem);
  157. return;
  158. /*
  159. * Something tried to access memory that isn't in our memory map..
  160. * Fix it, but check if it's kernel or user first..
  161. */
  162. bad_area:
  163. up_read(&mm->mmap_sem);
  164. bad_area_nosemaphore:
  165. /* User mode accesses just cause a SIGSEGV */
  166. if (user_mode(regs)) {
  167. if (unhandled_signal(current, SIGSEGV) && printk_ratelimit()) {
  168. pr_info("%s: unhandled page fault (%d) at 0x%08lx, "
  169. "cause %ld\n", current->comm, SIGSEGV, address, cause);
  170. show_regs(regs);
  171. }
  172. _exception(SIGSEGV, regs, code, address);
  173. return;
  174. }
  175. no_context:
  176. /* Are we prepared to handle this kernel fault? */
  177. if (fixup_exception(regs))
  178. return;
  179. /*
  180. * Oops. The kernel tried to access some bad page. We'll have to
  181. * terminate things with extreme prejudice.
  182. */
  183. bust_spinlocks(1);
  184. pr_alert("Unable to handle kernel %s at virtual address %08lx",
  185. address < PAGE_SIZE ? "NULL pointer dereference" :
  186. "paging request", address);
  187. pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra,
  188. cause);
  189. panic("Oops");
  190. return;
  191. /*
  192. * We ran out of memory, or some other thing happened to us that made
  193. * us unable to handle the page fault gracefully.
  194. */
  195. out_of_memory:
  196. up_read(&mm->mmap_sem);
  197. if (!user_mode(regs))
  198. goto no_context;
  199. pagefault_out_of_memory();
  200. return;
  201. do_sigbus:
  202. up_read(&mm->mmap_sem);
  203. /* Kernel mode? Handle exceptions or die */
  204. if (!user_mode(regs))
  205. goto no_context;
  206. _exception(SIGBUS, regs, BUS_ADRERR, address);
  207. return;
  208. vmalloc_fault:
  209. {
  210. /*
  211. * Synchronize this task's top level page-table
  212. * with the 'reference' page table.
  213. *
  214. * Do _not_ use "tsk" here. We might be inside
  215. * an interrupt in the middle of a task switch..
  216. */
  217. int offset = pgd_index(address);
  218. pgd_t *pgd, *pgd_k;
  219. pud_t *pud, *pud_k;
  220. pmd_t *pmd, *pmd_k;
  221. pte_t *pte_k;
  222. pgd = pgd_current + offset;
  223. pgd_k = init_mm.pgd + offset;
  224. if (!pgd_present(*pgd_k))
  225. goto no_context;
  226. set_pgd(pgd, *pgd_k);
  227. pud = pud_offset(pgd, address);
  228. pud_k = pud_offset(pgd_k, address);
  229. if (!pud_present(*pud_k))
  230. goto no_context;
  231. pmd = pmd_offset(pud, address);
  232. pmd_k = pmd_offset(pud_k, address);
  233. if (!pmd_present(*pmd_k))
  234. goto no_context;
  235. set_pmd(pmd, *pmd_k);
  236. pte_k = pte_offset_kernel(pmd_k, address);
  237. if (!pte_present(*pte_k))
  238. goto no_context;
  239. flush_tlb_one(address);
  240. return;
  241. }
  242. }