fault.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /*
  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. /*
  63. * We fault-in kernel-space virtual memory on-demand. The
  64. * 'reference' page table is init_mm.pgd.
  65. *
  66. * NOTE! We MUST NOT take any locks for this case. We may
  67. * be in an interrupt or a critical region, and should
  68. * only copy the information from the master page table,
  69. * nothing more.
  70. */
  71. if (address >= VMALLOC_START) {
  72. ret = handle_kernel_vaddr_fault(address);
  73. if (unlikely(ret))
  74. goto bad_area_nosemaphore;
  75. else
  76. return;
  77. }
  78. info.si_code = SEGV_MAPERR;
  79. /*
  80. * If we're in an interrupt or have no user
  81. * context, we must not take the fault..
  82. */
  83. if (faulthandler_disabled() || !mm)
  84. goto no_context;
  85. if (user_mode(regs))
  86. flags |= FAULT_FLAG_USER;
  87. retry:
  88. down_read(&mm->mmap_sem);
  89. vma = find_vma(mm, address);
  90. if (!vma)
  91. goto bad_area;
  92. if (vma->vm_start <= address)
  93. goto good_area;
  94. if (!(vma->vm_flags & VM_GROWSDOWN))
  95. goto bad_area;
  96. if (expand_stack(vma, address))
  97. goto bad_area;
  98. /*
  99. * Ok, we have a good vm_area for this memory access, so
  100. * we can handle it..
  101. */
  102. good_area:
  103. info.si_code = SEGV_ACCERR;
  104. /* Handle protection violation, execute on heap or stack */
  105. if ((regs->ecr_vec == ECR_V_PROTV) &&
  106. (regs->ecr_cause == ECR_C_PROTV_INST_FETCH))
  107. goto bad_area;
  108. if (write) {
  109. if (!(vma->vm_flags & VM_WRITE))
  110. goto bad_area;
  111. flags |= FAULT_FLAG_WRITE;
  112. } else {
  113. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  114. goto bad_area;
  115. }
  116. /*
  117. * If for any reason at all we couldn't handle the fault,
  118. * make sure we exit gracefully rather than endlessly redo
  119. * the fault.
  120. */
  121. fault = handle_mm_fault(vma, address, flags);
  122. /* If Pagefault was interrupted by SIGKILL, exit page fault "early" */
  123. if (unlikely(fatal_signal_pending(current))) {
  124. if ((fault & VM_FAULT_ERROR) && !(fault & VM_FAULT_RETRY))
  125. up_read(&mm->mmap_sem);
  126. if (user_mode(regs))
  127. return;
  128. }
  129. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  130. if (likely(!(fault & VM_FAULT_ERROR))) {
  131. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  132. /* To avoid updating stats twice for retry case */
  133. if (fault & VM_FAULT_MAJOR) {
  134. tsk->maj_flt++;
  135. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
  136. regs, address);
  137. } else {
  138. tsk->min_flt++;
  139. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
  140. regs, address);
  141. }
  142. if (fault & VM_FAULT_RETRY) {
  143. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  144. flags |= FAULT_FLAG_TRIED;
  145. goto retry;
  146. }
  147. }
  148. /* Fault Handled Gracefully */
  149. up_read(&mm->mmap_sem);
  150. return;
  151. }
  152. if (fault & VM_FAULT_OOM)
  153. goto out_of_memory;
  154. else if (fault & VM_FAULT_SIGSEGV)
  155. goto bad_area;
  156. else if (fault & VM_FAULT_SIGBUS)
  157. goto do_sigbus;
  158. /* no man's land */
  159. BUG();
  160. /*
  161. * Something tried to access memory that isn't in our memory map..
  162. * Fix it, but check if it's kernel or user first..
  163. */
  164. bad_area:
  165. up_read(&mm->mmap_sem);
  166. bad_area_nosemaphore:
  167. /* User mode accesses just cause a SIGSEGV */
  168. if (user_mode(regs)) {
  169. tsk->thread.fault_address = address;
  170. info.si_signo = SIGSEGV;
  171. info.si_errno = 0;
  172. /* info.si_code has been set above */
  173. info.si_addr = (void __user *)address;
  174. force_sig_info(SIGSEGV, &info, 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 acesses 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. info.si_signo = SIGBUS;
  202. info.si_errno = 0;
  203. info.si_code = BUS_ADRERR;
  204. info.si_addr = (void __user *)address;
  205. force_sig_info(SIGBUS, &info, tsk);
  206. }