fault.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Page Fault Handling for ARC (TLB Miss / ProtV)
  2. *
  3. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/signal.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/kdebug.h>
  16. #include <linux/perf_event.h>
  17. #include <asm/pgalloc.h>
  18. #include <asm/mmu.h>
  19. static int handle_vmalloc_fault(unsigned long address)
  20. {
  21. /*
  22. * Synchronize this task's top level page-table
  23. * with the 'reference' page table.
  24. */
  25. pgd_t *pgd, *pgd_k;
  26. pud_t *pud, *pud_k;
  27. pmd_t *pmd, *pmd_k;
  28. pgd = pgd_offset_fast(current->active_mm, address);
  29. pgd_k = pgd_offset_k(address);
  30. if (!pgd_present(*pgd_k))
  31. goto bad_area;
  32. pud = pud_offset(pgd, address);
  33. pud_k = pud_offset(pgd_k, address);
  34. if (!pud_present(*pud_k))
  35. goto bad_area;
  36. pmd = pmd_offset(pud, address);
  37. pmd_k = pmd_offset(pud_k, address);
  38. if (!pmd_present(*pmd_k))
  39. goto bad_area;
  40. set_pmd(pmd, *pmd_k);
  41. /* XXX: create the TLB entry here */
  42. return 0;
  43. bad_area:
  44. return 1;
  45. }
  46. void do_page_fault(unsigned long address, struct pt_regs *regs)
  47. {
  48. struct vm_area_struct *vma = NULL;
  49. struct task_struct *tsk = current;
  50. struct mm_struct *mm = tsk->mm;
  51. siginfo_t info;
  52. int fault, ret;
  53. int write = regs->ecr_cause & ECR_C_PROTV_STORE; /* ST/EX */
  54. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  55. /*
  56. * We fault-in kernel-space virtual memory on-demand. The
  57. * 'reference' page table is init_mm.pgd.
  58. *
  59. * NOTE! We MUST NOT take any locks for this case. We may
  60. * be in an interrupt or a critical region, and should
  61. * only copy the information from the master page table,
  62. * nothing more.
  63. */
  64. if (address >= VMALLOC_START && address <= VMALLOC_END) {
  65. ret = handle_vmalloc_fault(address);
  66. if (unlikely(ret))
  67. goto bad_area_nosemaphore;
  68. else
  69. return;
  70. }
  71. info.si_code = SEGV_MAPERR;
  72. /*
  73. * If we're in an interrupt or have no user
  74. * context, we must not take the fault..
  75. */
  76. if (faulthandler_disabled() || !mm)
  77. goto no_context;
  78. if (user_mode(regs))
  79. flags |= FAULT_FLAG_USER;
  80. retry:
  81. down_read(&mm->mmap_sem);
  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. info.si_code = SEGV_ACCERR;
  97. /* Handle protection violation, execute on heap or stack */
  98. if ((regs->ecr_vec == ECR_V_PROTV) &&
  99. (regs->ecr_cause == ECR_C_PROTV_INST_FETCH))
  100. goto bad_area;
  101. if (write) {
  102. if (!(vma->vm_flags & VM_WRITE))
  103. goto bad_area;
  104. flags |= FAULT_FLAG_WRITE;
  105. } else {
  106. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  107. goto bad_area;
  108. }
  109. /*
  110. * If for any reason at all we couldn't handle the fault,
  111. * make sure we exit gracefully rather than endlessly redo
  112. * the fault.
  113. */
  114. fault = handle_mm_fault(mm, vma, address, flags);
  115. /* If Pagefault was interrupted by SIGKILL, exit page fault "early" */
  116. if (unlikely(fatal_signal_pending(current))) {
  117. if ((fault & VM_FAULT_ERROR) && !(fault & VM_FAULT_RETRY))
  118. up_read(&mm->mmap_sem);
  119. if (user_mode(regs))
  120. return;
  121. }
  122. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  123. if (likely(!(fault & VM_FAULT_ERROR))) {
  124. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  125. /* To avoid updating stats twice for retry case */
  126. if (fault & VM_FAULT_MAJOR) {
  127. tsk->maj_flt++;
  128. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  129. regs, address);
  130. } else {
  131. tsk->min_flt++;
  132. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  133. regs, address);
  134. }
  135. if (fault & VM_FAULT_RETRY) {
  136. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  137. flags |= FAULT_FLAG_TRIED;
  138. goto retry;
  139. }
  140. }
  141. /* Fault Handled Gracefully */
  142. up_read(&mm->mmap_sem);
  143. return;
  144. }
  145. if (fault & VM_FAULT_OOM)
  146. goto out_of_memory;
  147. else if (fault & VM_FAULT_SIGSEGV)
  148. goto bad_area;
  149. else if (fault & VM_FAULT_SIGBUS)
  150. goto do_sigbus;
  151. /* no man's land */
  152. BUG();
  153. /*
  154. * Something tried to access memory that isn't in our memory map..
  155. * Fix it, but check if it's kernel or user first..
  156. */
  157. bad_area:
  158. up_read(&mm->mmap_sem);
  159. bad_area_nosemaphore:
  160. /* User mode accesses just cause a SIGSEGV */
  161. if (user_mode(regs)) {
  162. tsk->thread.fault_address = address;
  163. info.si_signo = SIGSEGV;
  164. info.si_errno = 0;
  165. /* info.si_code has been set above */
  166. info.si_addr = (void __user *)address;
  167. force_sig_info(SIGSEGV, &info, tsk);
  168. return;
  169. }
  170. no_context:
  171. /* Are we prepared to handle this kernel fault?
  172. *
  173. * (The kernel has valid exception-points in the source
  174. * when it acesses user-memory. When it fails in one
  175. * of those points, we find it in a table and do a jump
  176. * to some fixup code that loads an appropriate error
  177. * code)
  178. */
  179. if (fixup_exception(regs))
  180. return;
  181. die("Oops", regs, address);
  182. out_of_memory:
  183. up_read(&mm->mmap_sem);
  184. if (user_mode(regs)) {
  185. pagefault_out_of_memory();
  186. return;
  187. }
  188. goto no_context;
  189. do_sigbus:
  190. up_read(&mm->mmap_sem);
  191. if (!user_mode(regs))
  192. goto no_context;
  193. tsk->thread.fault_address = address;
  194. info.si_signo = SIGBUS;
  195. info.si_errno = 0;
  196. info.si_code = BUS_ADRERR;
  197. info.si_addr = (void __user *)address;
  198. force_sig_info(SIGBUS, &info, tsk);
  199. }