fault.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MMU fault handling support.
  4. *
  5. * Copyright (C) 1998-2002 Hewlett-Packard Co
  6. * David Mosberger-Tang <[email protected]>
  7. */
  8. #include <linux/sched/signal.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/extable.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kprobes.h>
  14. #include <linux/kdebug.h>
  15. #include <linux/prefetch.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/perf_event.h>
  18. #include <asm/processor.h>
  19. #include <asm/exception.h>
  20. extern int die(char *, struct pt_regs *, long);
  21. /*
  22. * Return TRUE if ADDRESS points at a page in the kernel's mapped segment
  23. * (inside region 5, on ia64) and that page is present.
  24. */
  25. static int
  26. mapped_kernel_page_is_present (unsigned long address)
  27. {
  28. pgd_t *pgd;
  29. p4d_t *p4d;
  30. pud_t *pud;
  31. pmd_t *pmd;
  32. pte_t *ptep, pte;
  33. pgd = pgd_offset_k(address);
  34. if (pgd_none(*pgd) || pgd_bad(*pgd))
  35. return 0;
  36. p4d = p4d_offset(pgd, address);
  37. if (p4d_none(*p4d) || p4d_bad(*p4d))
  38. return 0;
  39. pud = pud_offset(p4d, address);
  40. if (pud_none(*pud) || pud_bad(*pud))
  41. return 0;
  42. pmd = pmd_offset(pud, address);
  43. if (pmd_none(*pmd) || pmd_bad(*pmd))
  44. return 0;
  45. ptep = pte_offset_kernel(pmd, address);
  46. if (!ptep)
  47. return 0;
  48. pte = *ptep;
  49. return pte_present(pte);
  50. }
  51. # define VM_READ_BIT 0
  52. # define VM_WRITE_BIT 1
  53. # define VM_EXEC_BIT 2
  54. void __kprobes
  55. ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *regs)
  56. {
  57. int signal = SIGSEGV, code = SEGV_MAPERR;
  58. struct vm_area_struct *vma, *prev_vma;
  59. struct mm_struct *mm = current->mm;
  60. unsigned long mask;
  61. vm_fault_t fault;
  62. unsigned int flags = FAULT_FLAG_DEFAULT;
  63. mask = ((((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
  64. | (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT));
  65. /* mmap_lock is performance critical.... */
  66. prefetchw(&mm->mmap_lock);
  67. /*
  68. * If we're in an interrupt or have no user context, we must not take the fault..
  69. */
  70. if (faulthandler_disabled() || !mm)
  71. goto no_context;
  72. /*
  73. * This is to handle the kprobes on user space access instructions
  74. */
  75. if (kprobe_page_fault(regs, TRAP_BRKPT))
  76. return;
  77. if (user_mode(regs))
  78. flags |= FAULT_FLAG_USER;
  79. if (mask & VM_WRITE)
  80. flags |= FAULT_FLAG_WRITE;
  81. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  82. retry:
  83. mmap_read_lock(mm);
  84. vma = find_vma_prev(mm, address, &prev_vma);
  85. if (!vma && !prev_vma )
  86. goto bad_area;
  87. /*
  88. * find_vma_prev() returns vma such that address < vma->vm_end or NULL
  89. *
  90. * May find no vma, but could be that the last vm area is the
  91. * register backing store that needs to expand upwards, in
  92. * this case vma will be null, but prev_vma will ne non-null
  93. */
  94. if (( !vma && prev_vma ) || (address < vma->vm_start) ) {
  95. vma = expand_stack(mm, address);
  96. if (!vma)
  97. goto bad_area_nosemaphore;
  98. }
  99. code = SEGV_ACCERR;
  100. /* OK, we've got a good vm_area for this memory area. Check the access permissions: */
  101. # if (((1 << VM_READ_BIT) != VM_READ || (1 << VM_WRITE_BIT) != VM_WRITE) \
  102. || (1 << VM_EXEC_BIT) != VM_EXEC)
  103. # error File is out of sync with <linux/mm.h>. Please update.
  104. # endif
  105. if (((isr >> IA64_ISR_R_BIT) & 1UL) && (!(vma->vm_flags & (VM_READ | VM_WRITE))))
  106. goto bad_area;
  107. if ((vma->vm_flags & mask) != mask)
  108. goto bad_area;
  109. /*
  110. * If for any reason at all we couldn't handle the fault, make
  111. * sure we exit gracefully rather than endlessly redo the
  112. * fault.
  113. */
  114. fault = handle_mm_fault(vma, address, flags, regs);
  115. if (fault_signal_pending(fault, regs))
  116. return;
  117. /* The fault is fully completed (including releasing mmap lock) */
  118. if (fault & VM_FAULT_COMPLETED)
  119. return;
  120. if (unlikely(fault & VM_FAULT_ERROR)) {
  121. /*
  122. * We ran out of memory, or some other thing happened
  123. * to us that made us unable to handle the page fault
  124. * gracefully.
  125. */
  126. if (fault & VM_FAULT_OOM) {
  127. goto out_of_memory;
  128. } else if (fault & VM_FAULT_SIGSEGV) {
  129. goto bad_area;
  130. } else if (fault & VM_FAULT_SIGBUS) {
  131. signal = SIGBUS;
  132. goto bad_area;
  133. }
  134. BUG();
  135. }
  136. if (fault & VM_FAULT_RETRY) {
  137. flags |= FAULT_FLAG_TRIED;
  138. /* No need to mmap_read_unlock(mm) as we would
  139. * have already released it in __lock_page_or_retry
  140. * in mm/filemap.c.
  141. */
  142. goto retry;
  143. }
  144. mmap_read_unlock(mm);
  145. return;
  146. bad_area:
  147. mmap_read_unlock(mm);
  148. bad_area_nosemaphore:
  149. if ((isr & IA64_ISR_SP)
  150. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  151. {
  152. /*
  153. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  154. * bit in the psr to ensure forward progress. (Target register will get a
  155. * NaT for ld.s, lfetch will be canceled.)
  156. */
  157. ia64_psr(regs)->ed = 1;
  158. return;
  159. }
  160. if (user_mode(regs)) {
  161. force_sig_fault(signal, code, (void __user *) address,
  162. 0, __ISR_VALID, isr);
  163. return;
  164. }
  165. no_context:
  166. if ((isr & IA64_ISR_SP)
  167. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  168. {
  169. /*
  170. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  171. * bit in the psr to ensure forward progress. (Target register will get a
  172. * NaT for ld.s, lfetch will be canceled.)
  173. */
  174. ia64_psr(regs)->ed = 1;
  175. return;
  176. }
  177. /*
  178. * Since we have no vma's for region 5, we might get here even if the address is
  179. * valid, due to the VHPT walker inserting a non present translation that becomes
  180. * stale. If that happens, the non present fault handler already purged the stale
  181. * translation, which fixed the problem. So, we check to see if the translation is
  182. * valid, and return if it is.
  183. */
  184. if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
  185. return;
  186. if (ia64_done_with_exception(regs))
  187. return;
  188. /*
  189. * Oops. The kernel tried to access some bad page. We'll have to terminate things
  190. * with extreme prejudice.
  191. */
  192. bust_spinlocks(1);
  193. if (address < PAGE_SIZE)
  194. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
  195. else
  196. printk(KERN_ALERT "Unable to handle kernel paging request at "
  197. "virtual address %016lx\n", address);
  198. if (die("Oops", regs, isr))
  199. regs = NULL;
  200. bust_spinlocks(0);
  201. if (regs)
  202. make_task_dead(SIGKILL);
  203. return;
  204. out_of_memory:
  205. mmap_read_unlock(mm);
  206. if (!user_mode(regs))
  207. goto no_context;
  208. pagefault_out_of_memory();
  209. }