fault.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 = SEGV_MAPERR;
  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 && !user_mode(regs)) {
  74. ret = handle_kernel_vaddr_fault(address);
  75. if (unlikely(ret))
  76. goto no_context;
  77. else
  78. return;
  79. }
  80. /*
  81. * If we're in an interrupt or have no user
  82. * context, we must not take the fault..
  83. */
  84. if (faulthandler_disabled() || !mm)
  85. goto no_context;
  86. if (user_mode(regs))
  87. flags |= FAULT_FLAG_USER;
  88. retry:
  89. down_read(&mm->mmap_sem);
  90. vma = find_vma(mm, address);
  91. if (!vma)
  92. goto bad_area;
  93. if (vma->vm_start <= address)
  94. goto good_area;
  95. if (!(vma->vm_flags & VM_GROWSDOWN))
  96. goto bad_area;
  97. if (expand_stack(vma, address))
  98. goto bad_area;
  99. /*
  100. * Ok, we have a good vm_area for this memory access, so
  101. * we can handle it..
  102. */
  103. good_area:
  104. si_code = SEGV_ACCERR;
  105. /* Handle protection violation, execute on heap or stack */
  106. if ((regs->ecr_vec == ECR_V_PROTV) &&
  107. (regs->ecr_cause == ECR_C_PROTV_INST_FETCH))
  108. goto bad_area;
  109. if (write) {
  110. if (!(vma->vm_flags & VM_WRITE))
  111. goto bad_area;
  112. flags |= FAULT_FLAG_WRITE;
  113. } else {
  114. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  115. goto bad_area;
  116. }
  117. /*
  118. * If for any reason at all we couldn't handle the fault,
  119. * make sure we exit gracefully rather than endlessly redo
  120. * the fault.
  121. */
  122. fault = handle_mm_fault(vma, address, flags);
  123. if (unlikely(fatal_signal_pending(current))) {
  124. /*
  125. * if fault retry, mmap_sem already relinquished by core mm
  126. * so OK to return to user mode (with signal handled first)
  127. */
  128. if (fault & VM_FAULT_RETRY) {
  129. if (!user_mode(regs))
  130. goto no_context;
  131. return;
  132. }
  133. }
  134. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  135. if (likely(!(fault & VM_FAULT_ERROR))) {
  136. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  137. /* To avoid updating stats twice for retry case */
  138. if (fault & VM_FAULT_MAJOR) {
  139. tsk->maj_flt++;
  140. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  141. regs, address);
  142. } else {
  143. tsk->min_flt++;
  144. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  145. regs, address);
  146. }
  147. if (fault & VM_FAULT_RETRY) {
  148. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  149. flags |= FAULT_FLAG_TRIED;
  150. goto retry;
  151. }
  152. }
  153. /* Fault Handled Gracefully */
  154. up_read(&mm->mmap_sem);
  155. return;
  156. }
  157. if (fault & VM_FAULT_OOM)
  158. goto out_of_memory;
  159. else if (fault & VM_FAULT_SIGSEGV)
  160. goto bad_area;
  161. else if (fault & VM_FAULT_SIGBUS)
  162. goto do_sigbus;
  163. /* no man's land */
  164. BUG();
  165. /*
  166. * Something tried to access memory that isn't in our memory map..
  167. * Fix it, but check if it's kernel or user first..
  168. */
  169. bad_area:
  170. up_read(&mm->mmap_sem);
  171. /* User mode accesses just cause a SIGSEGV */
  172. if (user_mode(regs)) {
  173. tsk->thread.fault_address = address;
  174. force_sig_fault(SIGSEGV, si_code, (void __user *)address, tsk);
  175. return;
  176. }
  177. no_context:
  178. /* Are we prepared to handle this kernel fault?
  179. *
  180. * (The kernel has valid exception-points in the source
  181. * when it accesses user-memory. When it fails in one
  182. * of those points, we find it in a table and do a jump
  183. * to some fixup code that loads an appropriate error
  184. * code)
  185. */
  186. if (fixup_exception(regs))
  187. return;
  188. die("Oops", regs, address);
  189. out_of_memory:
  190. up_read(&mm->mmap_sem);
  191. if (user_mode(regs)) {
  192. pagefault_out_of_memory();
  193. return;
  194. }
  195. goto no_context;
  196. do_sigbus:
  197. up_read(&mm->mmap_sem);
  198. if (!user_mode(regs))
  199. goto no_context;
  200. tsk->thread.fault_address = address;
  201. force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *)address, tsk);
  202. }