fault.c 5.3 KB

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