fault.c 6.7 KB

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