fault.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 = 0;
  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 (in_atomic() || !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. down_read(&mm->mmap_sem);
  79. }
  80. vma = find_vma(mm, address);
  81. if (!vma)
  82. goto bad_area;
  83. if (vma->vm_start <= address)
  84. goto good_area;
  85. if (!(vma->vm_flags & VM_GROWSDOWN))
  86. goto bad_area;
  87. if (expand_stack(vma, address))
  88. goto bad_area;
  89. /*
  90. * Ok, we have a good vm_area for this memory access, so
  91. * we can handle it..
  92. */
  93. good_area:
  94. code = SEGV_ACCERR;
  95. switch (cause) {
  96. case EXC_SUPERV_INSN_ACCESS:
  97. goto bad_area;
  98. case EXC_SUPERV_DATA_ACCESS:
  99. goto bad_area;
  100. case EXC_X_PROTECTION_FAULT:
  101. if (!(vma->vm_flags & VM_EXEC))
  102. goto bad_area;
  103. break;
  104. case EXC_R_PROTECTION_FAULT:
  105. if (!(vma->vm_flags & VM_READ))
  106. goto bad_area;
  107. break;
  108. case EXC_W_PROTECTION_FAULT:
  109. if (!(vma->vm_flags & VM_WRITE))
  110. goto bad_area;
  111. flags = FAULT_FLAG_WRITE;
  112. break;
  113. }
  114. survive:
  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 (unlikely(fault & VM_FAULT_ERROR)) {
  122. if (fault & VM_FAULT_OOM)
  123. goto out_of_memory;
  124. else if (fault & VM_FAULT_SIGBUS)
  125. goto do_sigbus;
  126. BUG();
  127. }
  128. if (fault & VM_FAULT_MAJOR)
  129. tsk->maj_flt++;
  130. else
  131. tsk->min_flt++;
  132. up_read(&mm->mmap_sem);
  133. return;
  134. /*
  135. * Something tried to access memory that isn't in our memory map..
  136. * Fix it, but check if it's kernel or user first..
  137. */
  138. bad_area:
  139. up_read(&mm->mmap_sem);
  140. bad_area_nosemaphore:
  141. /* User mode accesses just cause a SIGSEGV */
  142. if (user_mode(regs)) {
  143. pr_alert("%s: unhandled page fault (%d) at 0x%08lx, "
  144. "cause %ld\n", current->comm, SIGSEGV, address, cause);
  145. show_regs(regs);
  146. _exception(SIGSEGV, regs, code, address);
  147. return;
  148. }
  149. no_context:
  150. /* Are we prepared to handle this kernel fault? */
  151. if (fixup_exception(regs))
  152. return;
  153. /*
  154. * Oops. The kernel tried to access some bad page. We'll have to
  155. * terminate things with extreme prejudice.
  156. */
  157. bust_spinlocks(1);
  158. pr_alert("Unable to handle kernel %s at virtual address %08lx",
  159. address < PAGE_SIZE ? "NULL pointer dereference" :
  160. "paging request", address);
  161. pr_alert("ea = %08lx, ra = %08lx, cause = %ld\n", regs->ea, regs->ra,
  162. cause);
  163. panic("Oops");
  164. return;
  165. /*
  166. * We ran out of memory, or some other thing happened to us that made
  167. * us unable to handle the page fault gracefully.
  168. */
  169. out_of_memory:
  170. up_read(&mm->mmap_sem);
  171. if (is_global_init(tsk)) {
  172. yield();
  173. down_read(&mm->mmap_sem);
  174. goto survive;
  175. }
  176. if (!user_mode(regs))
  177. goto no_context;
  178. pagefault_out_of_memory();
  179. return;
  180. do_sigbus:
  181. up_read(&mm->mmap_sem);
  182. /* Kernel mode? Handle exceptions or die */
  183. if (!user_mode(regs))
  184. goto no_context;
  185. _exception(SIGBUS, regs, BUS_ADRERR, address);
  186. return;
  187. vmalloc_fault:
  188. {
  189. /*
  190. * Synchronize this task's top level page-table
  191. * with the 'reference' page table.
  192. *
  193. * Do _not_ use "tsk" here. We might be inside
  194. * an interrupt in the middle of a task switch..
  195. */
  196. int offset = pgd_index(address);
  197. pgd_t *pgd, *pgd_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. pud = pud_offset(pgd, address);
  207. pud_k = pud_offset(pgd_k, address);
  208. if (!pud_present(*pud_k))
  209. goto no_context;
  210. pmd = pmd_offset(pud, address);
  211. pmd_k = pmd_offset(pud_k, address);
  212. if (!pmd_present(*pmd_k))
  213. goto no_context;
  214. set_pmd(pmd, *pmd_k);
  215. pte_k = pte_offset_kernel(pmd_k, address);
  216. if (!pte_present(*pte_k))
  217. goto no_context;
  218. flush_tlb_one(address);
  219. return;
  220. }
  221. }