fault.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <davidm@hpl.hp.com>
  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 <asm/pgtable.h>
  18. #include <asm/processor.h>
  19. #include <asm/exception.h>
  20. extern int die(char *, struct pt_regs *, long);
  21. #ifdef CONFIG_KPROBES
  22. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  23. {
  24. int ret = 0;
  25. if (!user_mode(regs)) {
  26. /* kprobe_running() needs smp_processor_id() */
  27. preempt_disable();
  28. if (kprobe_running() && kprobe_fault_handler(regs, trap))
  29. ret = 1;
  30. preempt_enable();
  31. }
  32. return ret;
  33. }
  34. #else
  35. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  36. {
  37. return 0;
  38. }
  39. #endif
  40. /*
  41. * Return TRUE if ADDRESS points at a page in the kernel's mapped segment
  42. * (inside region 5, on ia64) and that page is present.
  43. */
  44. static int
  45. mapped_kernel_page_is_present (unsigned long address)
  46. {
  47. pgd_t *pgd;
  48. pud_t *pud;
  49. pmd_t *pmd;
  50. pte_t *ptep, pte;
  51. pgd = pgd_offset_k(address);
  52. if (pgd_none(*pgd) || pgd_bad(*pgd))
  53. return 0;
  54. pud = pud_offset(pgd, address);
  55. if (pud_none(*pud) || pud_bad(*pud))
  56. return 0;
  57. pmd = pmd_offset(pud, address);
  58. if (pmd_none(*pmd) || pmd_bad(*pmd))
  59. return 0;
  60. ptep = pte_offset_kernel(pmd, address);
  61. if (!ptep)
  62. return 0;
  63. pte = *ptep;
  64. return pte_present(pte);
  65. }
  66. # define VM_READ_BIT 0
  67. # define VM_WRITE_BIT 1
  68. # define VM_EXEC_BIT 2
  69. void __kprobes
  70. ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *regs)
  71. {
  72. int signal = SIGSEGV, code = SEGV_MAPERR;
  73. struct vm_area_struct *vma, *prev_vma;
  74. struct mm_struct *mm = current->mm;
  75. unsigned long mask;
  76. vm_fault_t fault;
  77. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  78. mask = ((((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
  79. | (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT));
  80. /* mmap_sem is performance critical.... */
  81. prefetchw(&mm->mmap_sem);
  82. /*
  83. * If we're in an interrupt or have no user context, we must not take the fault..
  84. */
  85. if (faulthandler_disabled() || !mm)
  86. goto no_context;
  87. #ifdef CONFIG_VIRTUAL_MEM_MAP
  88. /*
  89. * If fault is in region 5 and we are in the kernel, we may already
  90. * have the mmap_sem (pfn_valid macro is called during mmap). There
  91. * is no vma for region 5 addr's anyway, so skip getting the semaphore
  92. * and go directly to the exception handling code.
  93. */
  94. if ((REGION_NUMBER(address) == 5) && !user_mode(regs))
  95. goto bad_area_no_up;
  96. #endif
  97. /*
  98. * This is to handle the kprobes on user space access instructions
  99. */
  100. if (notify_page_fault(regs, TRAP_BRKPT))
  101. return;
  102. if (user_mode(regs))
  103. flags |= FAULT_FLAG_USER;
  104. if (mask & VM_WRITE)
  105. flags |= FAULT_FLAG_WRITE;
  106. retry:
  107. down_read(&mm->mmap_sem);
  108. vma = find_vma_prev(mm, address, &prev_vma);
  109. if (!vma && !prev_vma )
  110. goto bad_area;
  111. /*
  112. * find_vma_prev() returns vma such that address < vma->vm_end or NULL
  113. *
  114. * May find no vma, but could be that the last vm area is the
  115. * register backing store that needs to expand upwards, in
  116. * this case vma will be null, but prev_vma will ne non-null
  117. */
  118. if (( !vma && prev_vma ) || (address < vma->vm_start) )
  119. goto check_expansion;
  120. good_area:
  121. code = SEGV_ACCERR;
  122. /* OK, we've got a good vm_area for this memory area. Check the access permissions: */
  123. # if (((1 << VM_READ_BIT) != VM_READ || (1 << VM_WRITE_BIT) != VM_WRITE) \
  124. || (1 << VM_EXEC_BIT) != VM_EXEC)
  125. # error File is out of sync with <linux/mm.h>. Please update.
  126. # endif
  127. if (((isr >> IA64_ISR_R_BIT) & 1UL) && (!(vma->vm_flags & (VM_READ | VM_WRITE))))
  128. goto bad_area;
  129. if ((vma->vm_flags & mask) != mask)
  130. goto bad_area;
  131. /*
  132. * If for any reason at all we couldn't handle the fault, make
  133. * sure we exit gracefully rather than endlessly redo the
  134. * fault.
  135. */
  136. fault = handle_mm_fault(vma, address, flags);
  137. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  138. return;
  139. if (unlikely(fault & VM_FAULT_ERROR)) {
  140. /*
  141. * We ran out of memory, or some other thing happened
  142. * to us that made us unable to handle the page fault
  143. * gracefully.
  144. */
  145. if (fault & VM_FAULT_OOM) {
  146. goto out_of_memory;
  147. } else if (fault & VM_FAULT_SIGSEGV) {
  148. goto bad_area;
  149. } else if (fault & VM_FAULT_SIGBUS) {
  150. signal = SIGBUS;
  151. goto bad_area;
  152. }
  153. BUG();
  154. }
  155. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  156. if (fault & VM_FAULT_MAJOR)
  157. current->maj_flt++;
  158. else
  159. current->min_flt++;
  160. if (fault & VM_FAULT_RETRY) {
  161. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  162. flags |= FAULT_FLAG_TRIED;
  163. /* No need to up_read(&mm->mmap_sem) as we would
  164. * have already released it in __lock_page_or_retry
  165. * in mm/filemap.c.
  166. */
  167. goto retry;
  168. }
  169. }
  170. up_read(&mm->mmap_sem);
  171. return;
  172. check_expansion:
  173. if (!(prev_vma && (prev_vma->vm_flags & VM_GROWSUP) && (address == prev_vma->vm_end))) {
  174. if (!vma)
  175. goto bad_area;
  176. if (!(vma->vm_flags & VM_GROWSDOWN))
  177. goto bad_area;
  178. if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
  179. || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
  180. goto bad_area;
  181. if (expand_stack(vma, address))
  182. goto bad_area;
  183. } else {
  184. vma = prev_vma;
  185. if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
  186. || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
  187. goto bad_area;
  188. /*
  189. * Since the register backing store is accessed sequentially,
  190. * we disallow growing it by more than a page at a time.
  191. */
  192. if (address > vma->vm_end + PAGE_SIZE - sizeof(long))
  193. goto bad_area;
  194. if (expand_upwards(vma, address))
  195. goto bad_area;
  196. }
  197. goto good_area;
  198. bad_area:
  199. up_read(&mm->mmap_sem);
  200. #ifdef CONFIG_VIRTUAL_MEM_MAP
  201. bad_area_no_up:
  202. #endif
  203. if ((isr & IA64_ISR_SP)
  204. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  205. {
  206. /*
  207. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  208. * bit in the psr to ensure forward progress. (Target register will get a
  209. * NaT for ld.s, lfetch will be canceled.)
  210. */
  211. ia64_psr(regs)->ed = 1;
  212. return;
  213. }
  214. if (user_mode(regs)) {
  215. struct siginfo si;
  216. clear_siginfo(&si);
  217. si.si_signo = signal;
  218. si.si_errno = 0;
  219. si.si_code = code;
  220. si.si_addr = (void __user *) address;
  221. si.si_isr = isr;
  222. si.si_flags = __ISR_VALID;
  223. force_sig_info(signal, &si, current);
  224. return;
  225. }
  226. no_context:
  227. if ((isr & IA64_ISR_SP)
  228. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  229. {
  230. /*
  231. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  232. * bit in the psr to ensure forward progress. (Target register will get a
  233. * NaT for ld.s, lfetch will be canceled.)
  234. */
  235. ia64_psr(regs)->ed = 1;
  236. return;
  237. }
  238. /*
  239. * Since we have no vma's for region 5, we might get here even if the address is
  240. * valid, due to the VHPT walker inserting a non present translation that becomes
  241. * stale. If that happens, the non present fault handler already purged the stale
  242. * translation, which fixed the problem. So, we check to see if the translation is
  243. * valid, and return if it is.
  244. */
  245. if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
  246. return;
  247. if (ia64_done_with_exception(regs))
  248. return;
  249. /*
  250. * Oops. The kernel tried to access some bad page. We'll have to terminate things
  251. * with extreme prejudice.
  252. */
  253. bust_spinlocks(1);
  254. if (address < PAGE_SIZE)
  255. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
  256. else
  257. printk(KERN_ALERT "Unable to handle kernel paging request at "
  258. "virtual address %016lx\n", address);
  259. if (die("Oops", regs, isr))
  260. regs = NULL;
  261. bust_spinlocks(0);
  262. if (regs)
  263. do_exit(SIGKILL);
  264. return;
  265. out_of_memory:
  266. up_read(&mm->mmap_sem);
  267. if (!user_mode(regs))
  268. goto no_context;
  269. pagefault_out_of_memory();
  270. }