fault.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <asm/pgalloc.h>
  18. #include <asm/mmu.h>
  19. /*
  20. * kernel virtual address is required to implement vmalloc/pkmap/fixmap
  21. * Refer to asm/processor.h for System Memory Map
  22. *
  23. * It simply copies the PMD entry (pointer to 2nd level page table or hugepage)
  24. * from swapper pgdir to task pgdir. The 2nd level table/page is thus shared
  25. */
  26. noinline static int handle_kernel_vaddr_fault(unsigned long address)
  27. {
  28. /*
  29. * Synchronize this task's top level page-table
  30. * with the 'reference' page table.
  31. */
  32. pgd_t *pgd, *pgd_k;
  33. pud_t *pud, *pud_k;
  34. pmd_t *pmd, *pmd_k;
  35. pgd = pgd_offset_fast(current->active_mm, address);
  36. pgd_k = pgd_offset_k(address);
  37. if (!pgd_present(*pgd_k))
  38. goto bad_area;
  39. pud = pud_offset(pgd, address);
  40. pud_k = pud_offset(pgd_k, address);
  41. if (!pud_present(*pud_k))
  42. goto bad_area;
  43. pmd = pmd_offset(pud, address);
  44. pmd_k = pmd_offset(pud_k, address);
  45. if (!pmd_present(*pmd_k))
  46. goto bad_area;
  47. set_pmd(pmd, *pmd_k);
  48. /* XXX: create the TLB entry here */
  49. return 0;
  50. bad_area:
  51. return 1;
  52. }
  53. void do_page_fault(unsigned long address, struct pt_regs *regs)
  54. {
  55. struct vm_area_struct *vma = NULL;
  56. struct task_struct *tsk = current;
  57. struct mm_struct *mm = tsk->mm;
  58. siginfo_t info;
  59. int fault, ret;
  60. int write = regs->ecr_cause & ECR_C_PROTV_STORE; /* ST/EX */
  61. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  62. clear_siginfo(&info);
  63. /*
  64. * We fault-in kernel-space virtual memory on-demand. The
  65. * 'reference' page table is init_mm.pgd.
  66. *
  67. * NOTE! We MUST NOT take any locks for this case. We may
  68. * be in an interrupt or a critical region, and should
  69. * only copy the information from the master page table,
  70. * nothing more.
  71. */
  72. if (address >= VMALLOC_START) {
  73. ret = handle_kernel_vaddr_fault(address);
  74. if (unlikely(ret))
  75. goto bad_area_nosemaphore;
  76. else
  77. return;
  78. }
  79. info.si_code = SEGV_MAPERR;
  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. info.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 Pagefault was interrupted by SIGKILL, exit page fault "early" */
  124. if (unlikely(fatal_signal_pending(current))) {
  125. if ((fault & VM_FAULT_ERROR) && !(fault & VM_FAULT_RETRY))
  126. up_read(&mm->mmap_sem);
  127. if (user_mode(regs))
  128. return;
  129. }
  130. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  131. if (likely(!(fault & VM_FAULT_ERROR))) {
  132. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  133. /* To avoid updating stats twice for retry case */
  134. if (fault & VM_FAULT_MAJOR) {
  135. tsk->maj_flt++;
  136. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  137. regs, address);
  138. } else {
  139. tsk->min_flt++;
  140. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  141. regs, address);
  142. }
  143. if (fault & VM_FAULT_RETRY) {
  144. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  145. flags |= FAULT_FLAG_TRIED;
  146. goto retry;
  147. }
  148. }
  149. /* Fault Handled Gracefully */
  150. up_read(&mm->mmap_sem);
  151. return;
  152. }
  153. if (fault & VM_FAULT_OOM)
  154. goto out_of_memory;
  155. else if (fault & VM_FAULT_SIGSEGV)
  156. goto bad_area;
  157. else if (fault & VM_FAULT_SIGBUS)
  158. goto do_sigbus;
  159. /* no man's land */
  160. BUG();
  161. /*
  162. * Something tried to access memory that isn't in our memory map..
  163. * Fix it, but check if it's kernel or user first..
  164. */
  165. bad_area:
  166. up_read(&mm->mmap_sem);
  167. bad_area_nosemaphore:
  168. /* User mode accesses just cause a SIGSEGV */
  169. if (user_mode(regs)) {
  170. tsk->thread.fault_address = address;
  171. info.si_signo = SIGSEGV;
  172. info.si_errno = 0;
  173. /* info.si_code has been set above */
  174. info.si_addr = (void __user *)address;
  175. force_sig_info(SIGSEGV, &info, tsk);
  176. return;
  177. }
  178. no_context:
  179. /* Are we prepared to handle this kernel fault?
  180. *
  181. * (The kernel has valid exception-points in the source
  182. * when it accesses user-memory. When it fails in one
  183. * of those points, we find it in a table and do a jump
  184. * to some fixup code that loads an appropriate error
  185. * code)
  186. */
  187. if (fixup_exception(regs))
  188. return;
  189. die("Oops", regs, address);
  190. out_of_memory:
  191. up_read(&mm->mmap_sem);
  192. if (user_mode(regs)) {
  193. pagefault_out_of_memory();
  194. return;
  195. }
  196. goto no_context;
  197. do_sigbus:
  198. up_read(&mm->mmap_sem);
  199. if (!user_mode(regs))
  200. goto no_context;
  201. tsk->thread.fault_address = address;
  202. info.si_signo = SIGBUS;
  203. info.si_errno = 0;
  204. info.si_code = BUS_ADRERR;
  205. info.si_addr = (void __user *)address;
  206. force_sig_info(SIGBUS, &info, tsk);
  207. }