fault.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/signal.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 <linux/mm_types.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/mmu.h>
  20. /*
  21. * kernel virtual address is required to implement vmalloc/pkmap/fixmap
  22. * Refer to asm/processor.h for System Memory Map
  23. *
  24. * It simply copies the PMD entry (pointer to 2nd level page table or hugepage)
  25. * from swapper pgdir to task pgdir. The 2nd level table/page is thus shared
  26. */
  27. noinline static int handle_kernel_vaddr_fault(unsigned long address)
  28. {
  29. /*
  30. * Synchronize this task's top level page-table
  31. * with the 'reference' page table.
  32. */
  33. pgd_t *pgd, *pgd_k;
  34. pud_t *pud, *pud_k;
  35. pmd_t *pmd, *pmd_k;
  36. pgd = pgd_offset_fast(current->active_mm, address);
  37. pgd_k = pgd_offset_k(address);
  38. if (!pgd_present(*pgd_k))
  39. goto bad_area;
  40. pud = pud_offset(pgd, address);
  41. pud_k = pud_offset(pgd_k, address);
  42. if (!pud_present(*pud_k))
  43. goto bad_area;
  44. pmd = pmd_offset(pud, address);
  45. pmd_k = pmd_offset(pud_k, address);
  46. if (!pmd_present(*pmd_k))
  47. goto bad_area;
  48. set_pmd(pmd, *pmd_k);
  49. /* XXX: create the TLB entry here */
  50. return 0;
  51. bad_area:
  52. return 1;
  53. }
  54. void do_page_fault(unsigned long address, struct pt_regs *regs)
  55. {
  56. struct vm_area_struct *vma = NULL;
  57. struct task_struct *tsk = current;
  58. struct mm_struct *mm = tsk->mm;
  59. int si_code = 0;
  60. int ret;
  61. vm_fault_t fault;
  62. int write = regs->ecr_cause & ECR_C_PROTV_STORE; /* ST/EX */
  63. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  64. /*
  65. * We fault-in kernel-space virtual memory on-demand. The
  66. * 'reference' page table is init_mm.pgd.
  67. *
  68. * NOTE! We MUST NOT take any locks for this case. We may
  69. * be in an interrupt or a critical region, and should
  70. * only copy the information from the master page table,
  71. * nothing more.
  72. */
  73. if (address >= VMALLOC_START) {
  74. ret = handle_kernel_vaddr_fault(address);
  75. if (unlikely(ret))
  76. goto bad_area_nosemaphore;
  77. else
  78. return;
  79. }
  80. si_code = SEGV_MAPERR;
  81. /*
  82. * If we're in an interrupt or have no user
  83. * context, we must not take the fault..
  84. */
  85. if (faulthandler_disabled() || !mm)
  86. goto no_context;
  87. if (user_mode(regs))
  88. flags |= FAULT_FLAG_USER;
  89. retry:
  90. down_read(&mm->mmap_sem);
  91. vma = find_vma(mm, address);
  92. if (!vma)
  93. goto bad_area;
  94. if (vma->vm_start <= address)
  95. goto good_area;
  96. if (!(vma->vm_flags & VM_GROWSDOWN))
  97. goto bad_area;
  98. if (expand_stack(vma, address))
  99. goto bad_area;
  100. /*
  101. * Ok, we have a good vm_area for this memory access, so
  102. * we can handle it..
  103. */
  104. good_area:
  105. si_code = SEGV_ACCERR;
  106. /* Handle protection violation, execute on heap or stack */
  107. if ((regs->ecr_vec == ECR_V_PROTV) &&
  108. (regs->ecr_cause == ECR_C_PROTV_INST_FETCH))
  109. goto bad_area;
  110. if (write) {
  111. if (!(vma->vm_flags & VM_WRITE))
  112. goto bad_area;
  113. flags |= FAULT_FLAG_WRITE;
  114. } else {
  115. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  116. goto bad_area;
  117. }
  118. /*
  119. * If for any reason at all we couldn't handle the fault,
  120. * make sure we exit gracefully rather than endlessly redo
  121. * the fault.
  122. */
  123. fault = handle_mm_fault(vma, address, flags);
  124. /* If Pagefault was interrupted by SIGKILL, exit page fault "early" */
  125. if (unlikely(fatal_signal_pending(current))) {
  126. if ((fault & VM_FAULT_ERROR) && !(fault & VM_FAULT_RETRY))
  127. up_read(&mm->mmap_sem);
  128. if (user_mode(regs))
  129. return;
  130. }
  131. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  132. if (likely(!(fault & VM_FAULT_ERROR))) {
  133. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  134. /* To avoid updating stats twice for retry case */
  135. if (fault & VM_FAULT_MAJOR) {
  136. tsk->maj_flt++;
  137. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  138. regs, address);
  139. } else {
  140. tsk->min_flt++;
  141. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  142. regs, address);
  143. }
  144. if (fault & VM_FAULT_RETRY) {
  145. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  146. flags |= FAULT_FLAG_TRIED;
  147. goto retry;
  148. }
  149. }
  150. /* Fault Handled Gracefully */
  151. up_read(&mm->mmap_sem);
  152. return;
  153. }
  154. if (fault & VM_FAULT_OOM)
  155. goto out_of_memory;
  156. else if (fault & VM_FAULT_SIGSEGV)
  157. goto bad_area;
  158. else if (fault & VM_FAULT_SIGBUS)
  159. goto do_sigbus;
  160. /* no man's land */
  161. BUG();
  162. /*
  163. * Something tried to access memory that isn't in our memory map..
  164. * Fix it, but check if it's kernel or user first..
  165. */
  166. bad_area:
  167. up_read(&mm->mmap_sem);
  168. bad_area_nosemaphore:
  169. /* User mode accesses just cause a SIGSEGV */
  170. if (user_mode(regs)) {
  171. tsk->thread.fault_address = address;
  172. force_sig_fault(SIGSEGV, si_code, (void __user *)address, tsk);
  173. return;
  174. }
  175. no_context:
  176. /* Are we prepared to handle this kernel fault?
  177. *
  178. * (The kernel has valid exception-points in the source
  179. * when it accesses user-memory. When it fails in one
  180. * of those points, we find it in a table and do a jump
  181. * to some fixup code that loads an appropriate error
  182. * code)
  183. */
  184. if (fixup_exception(regs))
  185. return;
  186. die("Oops", regs, address);
  187. out_of_memory:
  188. up_read(&mm->mmap_sem);
  189. if (user_mode(regs)) {
  190. pagefault_out_of_memory();
  191. return;
  192. }
  193. goto no_context;
  194. do_sigbus:
  195. up_read(&mm->mmap_sem);
  196. if (!user_mode(regs))
  197. goto no_context;
  198. tsk->thread.fault_address = address;
  199. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, tsk);
  200. }