fault.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2009 Wind River Systems Inc
  3. * Implemented by [email protected] and [email protected]
  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. return;
  113. /* The fault is fully completed (including releasing mmap lock) */
  114. if (fault & VM_FAULT_COMPLETED)
  115. return;
  116. if (unlikely(fault & VM_FAULT_ERROR)) {
  117. if (fault & VM_FAULT_OOM)
  118. goto out_of_memory;
  119. else if (fault & VM_FAULT_SIGSEGV)
  120. goto bad_area;
  121. else if (fault & VM_FAULT_SIGBUS)
  122. goto do_sigbus;
  123. BUG();
  124. }
  125. if (fault & VM_FAULT_RETRY) {
  126. flags |= FAULT_FLAG_TRIED;
  127. /*
  128. * No need to mmap_read_unlock(mm) as we would
  129. * have already released it in __lock_page_or_retry
  130. * in mm/filemap.c.
  131. */
  132. goto retry;
  133. }
  134. mmap_read_unlock(mm);
  135. return;
  136. /*
  137. * Something tried to access memory that isn't in our memory map..
  138. * Fix it, but check if it's kernel or user first..
  139. */
  140. bad_area:
  141. mmap_read_unlock(mm);
  142. bad_area_nosemaphore:
  143. /* User mode accesses just cause a SIGSEGV */
  144. if (user_mode(regs)) {
  145. if (unhandled_signal(current, SIGSEGV) && printk_ratelimit()) {
  146. pr_info("%s: unhandled page fault (%d) at 0x%08lx, "
  147. "cause %ld\n", current->comm, SIGSEGV, address, cause);
  148. show_regs(regs);
  149. }
  150. _exception(SIGSEGV, regs, code, address);
  151. return;
  152. }
  153. no_context:
  154. /* Are we prepared to handle this kernel fault? */
  155. if (fixup_exception(regs))
  156. return;
  157. /*
  158. * Oops. The kernel tried to access some bad page. We'll have to
  159. * terminate things with extreme prejudice.
  160. */
  161. bust_spinlocks(1);
  162. pr_alert("Unable to handle kernel %s at virtual address %08lx",
  163. address < PAGE_SIZE ? "NULL pointer dereference" :
  164. "paging request", address);
  165. pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra,
  166. cause);
  167. panic("Oops");
  168. return;
  169. /*
  170. * We ran out of memory, or some other thing happened to us that made
  171. * us unable to handle the page fault gracefully.
  172. */
  173. out_of_memory:
  174. mmap_read_unlock(mm);
  175. if (!user_mode(regs))
  176. goto no_context;
  177. pagefault_out_of_memory();
  178. return;
  179. do_sigbus:
  180. mmap_read_unlock(mm);
  181. /* Kernel mode? Handle exceptions or die */
  182. if (!user_mode(regs))
  183. goto no_context;
  184. _exception(SIGBUS, regs, BUS_ADRERR, address);
  185. return;
  186. vmalloc_fault:
  187. {
  188. /*
  189. * Synchronize this task's top level page-table
  190. * with the 'reference' page table.
  191. *
  192. * Do _not_ use "tsk" here. We might be inside
  193. * an interrupt in the middle of a task switch..
  194. */
  195. int offset = pgd_index(address);
  196. pgd_t *pgd, *pgd_k;
  197. p4d_t *p4d, *p4d_k;
  198. pud_t *pud, *pud_k;
  199. pmd_t *pmd, *pmd_k;
  200. pte_t *pte_k;
  201. pgd = pgd_current + offset;
  202. pgd_k = init_mm.pgd + offset;
  203. if (!pgd_present(*pgd_k))
  204. goto no_context;
  205. set_pgd(pgd, *pgd_k);
  206. p4d = p4d_offset(pgd, address);
  207. p4d_k = p4d_offset(pgd_k, address);
  208. if (!p4d_present(*p4d_k))
  209. goto no_context;
  210. pud = pud_offset(p4d, address);
  211. pud_k = pud_offset(p4d_k, address);
  212. if (!pud_present(*pud_k))
  213. goto no_context;
  214. pmd = pmd_offset(pud, address);
  215. pmd_k = pmd_offset(pud_k, address);
  216. if (!pmd_present(*pmd_k))
  217. goto no_context;
  218. set_pmd(pmd, *pmd_k);
  219. pte_k = pte_offset_kernel(pmd_k, address);
  220. if (!pte_present(*pte_k))
  221. goto no_context;
  222. flush_tlb_kernel_page(address);
  223. return;
  224. }
  225. }