fault.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/perf_event.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. vm_fault_t fault;
  47. unsigned int flags = FAULT_FLAG_DEFAULT;
  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. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  77. retry:
  78. vma = lock_mm_and_find_vma(mm, address, regs);
  79. if (!vma)
  80. goto bad_area_nosemaphore;
  81. /*
  82. * Ok, we have a good vm_area for this memory access, so
  83. * we can handle it..
  84. */
  85. code = SEGV_ACCERR;
  86. switch (cause) {
  87. case EXC_SUPERV_INSN_ACCESS:
  88. goto bad_area;
  89. case EXC_SUPERV_DATA_ACCESS:
  90. goto bad_area;
  91. case EXC_X_PROTECTION_FAULT:
  92. if (!(vma->vm_flags & VM_EXEC))
  93. goto bad_area;
  94. break;
  95. case EXC_R_PROTECTION_FAULT:
  96. if (!(vma->vm_flags & VM_READ))
  97. goto bad_area;
  98. break;
  99. case EXC_W_PROTECTION_FAULT:
  100. if (!(vma->vm_flags & VM_WRITE))
  101. goto bad_area;
  102. flags = FAULT_FLAG_WRITE;
  103. break;
  104. }
  105. /*
  106. * If for any reason at all we couldn't handle the fault,
  107. * make sure we exit gracefully rather than endlessly redo
  108. * the fault.
  109. */
  110. fault = handle_mm_fault(vma, address, flags, regs);
  111. if (fault_signal_pending(fault, regs)) {
  112. if (!user_mode(regs))
  113. goto no_context;
  114. return;
  115. }
  116. /* The fault is fully completed (including releasing mmap lock) */
  117. if (fault & VM_FAULT_COMPLETED)
  118. return;
  119. if (unlikely(fault & VM_FAULT_ERROR)) {
  120. if (fault & VM_FAULT_OOM)
  121. goto out_of_memory;
  122. else if (fault & VM_FAULT_SIGSEGV)
  123. goto bad_area;
  124. else if (fault & VM_FAULT_SIGBUS)
  125. goto do_sigbus;
  126. BUG();
  127. }
  128. if (fault & VM_FAULT_RETRY) {
  129. flags |= FAULT_FLAG_TRIED;
  130. /*
  131. * No need to mmap_read_unlock(mm) as we would
  132. * have already released it in __lock_page_or_retry
  133. * in mm/filemap.c.
  134. */
  135. goto retry;
  136. }
  137. mmap_read_unlock(mm);
  138. return;
  139. /*
  140. * Something tried to access memory that isn't in our memory map..
  141. * Fix it, but check if it's kernel or user first..
  142. */
  143. bad_area:
  144. mmap_read_unlock(mm);
  145. bad_area_nosemaphore:
  146. /* User mode accesses just cause a SIGSEGV */
  147. if (user_mode(regs)) {
  148. if (unhandled_signal(current, SIGSEGV) && printk_ratelimit()) {
  149. pr_info("%s: unhandled page fault (%d) at 0x%08lx, "
  150. "cause %ld\n", current->comm, SIGSEGV, address, cause);
  151. show_regs(regs);
  152. }
  153. _exception(SIGSEGV, regs, code, address);
  154. return;
  155. }
  156. no_context:
  157. /* Are we prepared to handle this kernel fault? */
  158. if (fixup_exception(regs))
  159. return;
  160. /*
  161. * Oops. The kernel tried to access some bad page. We'll have to
  162. * terminate things with extreme prejudice.
  163. */
  164. bust_spinlocks(1);
  165. pr_alert("Unable to handle kernel %s at virtual address %08lx",
  166. address < PAGE_SIZE ? "NULL pointer dereference" :
  167. "paging request", address);
  168. pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra,
  169. cause);
  170. panic("Oops");
  171. return;
  172. /*
  173. * We ran out of memory, or some other thing happened to us that made
  174. * us unable to handle the page fault gracefully.
  175. */
  176. out_of_memory:
  177. mmap_read_unlock(mm);
  178. if (!user_mode(regs))
  179. goto no_context;
  180. pagefault_out_of_memory();
  181. return;
  182. do_sigbus:
  183. mmap_read_unlock(mm);
  184. /* Kernel mode? Handle exceptions or die */
  185. if (!user_mode(regs))
  186. goto no_context;
  187. _exception(SIGBUS, regs, BUS_ADRERR, address);
  188. return;
  189. vmalloc_fault:
  190. {
  191. /*
  192. * Synchronize this task's top level page-table
  193. * with the 'reference' page table.
  194. *
  195. * Do _not_ use "tsk" here. We might be inside
  196. * an interrupt in the middle of a task switch..
  197. */
  198. int offset = pgd_index(address);
  199. pgd_t *pgd, *pgd_k;
  200. p4d_t *p4d, *p4d_k;
  201. pud_t *pud, *pud_k;
  202. pmd_t *pmd, *pmd_k;
  203. pte_t *pte_k;
  204. pgd = pgd_current + offset;
  205. pgd_k = init_mm.pgd + offset;
  206. if (!pgd_present(*pgd_k))
  207. goto no_context;
  208. set_pgd(pgd, *pgd_k);
  209. p4d = p4d_offset(pgd, address);
  210. p4d_k = p4d_offset(pgd_k, address);
  211. if (!p4d_present(*p4d_k))
  212. goto no_context;
  213. pud = pud_offset(p4d, address);
  214. pud_k = pud_offset(p4d_k, address);
  215. if (!pud_present(*pud_k))
  216. goto no_context;
  217. pmd = pmd_offset(pud, address);
  218. pmd_k = pmd_offset(pud_k, address);
  219. if (!pmd_present(*pmd_k))
  220. goto no_context;
  221. set_pmd(pmd, *pmd_k);
  222. pte_k = pte_offset_kernel(pmd_k, address);
  223. if (!pte_present(*pte_k))
  224. goto no_context;
  225. flush_tlb_kernel_page(address);
  226. return;
  227. }
  228. }