fault.c 5.2 KB

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