fault.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * MMU fault handling support.
  3. *
  4. * Copyright (C) 1998-2002 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. */
  7. #include <linux/sched/signal.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mm.h>
  10. #include <linux/extable.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kprobes.h>
  13. #include <linux/kdebug.h>
  14. #include <linux/prefetch.h>
  15. #include <linux/uaccess.h>
  16. #include <asm/pgtable.h>
  17. #include <asm/processor.h>
  18. #include <asm/exception.h>
  19. extern int die(char *, struct pt_regs *, long);
  20. #ifdef CONFIG_KPROBES
  21. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  22. {
  23. int ret = 0;
  24. if (!user_mode(regs)) {
  25. /* kprobe_running() needs smp_processor_id() */
  26. preempt_disable();
  27. if (kprobe_running() && kprobe_fault_handler(regs, trap))
  28. ret = 1;
  29. preempt_enable();
  30. }
  31. return ret;
  32. }
  33. #else
  34. static inline int notify_page_fault(struct pt_regs *regs, int trap)
  35. {
  36. return 0;
  37. }
  38. #endif
  39. /*
  40. * Return TRUE if ADDRESS points at a page in the kernel's mapped segment
  41. * (inside region 5, on ia64) and that page is present.
  42. */
  43. static int
  44. mapped_kernel_page_is_present (unsigned long address)
  45. {
  46. pgd_t *pgd;
  47. pud_t *pud;
  48. pmd_t *pmd;
  49. pte_t *ptep, pte;
  50. pgd = pgd_offset_k(address);
  51. if (pgd_none(*pgd) || pgd_bad(*pgd))
  52. return 0;
  53. pud = pud_offset(pgd, address);
  54. if (pud_none(*pud) || pud_bad(*pud))
  55. return 0;
  56. pmd = pmd_offset(pud, address);
  57. if (pmd_none(*pmd) || pmd_bad(*pmd))
  58. return 0;
  59. ptep = pte_offset_kernel(pmd, address);
  60. if (!ptep)
  61. return 0;
  62. pte = *ptep;
  63. return pte_present(pte);
  64. }
  65. # define VM_READ_BIT 0
  66. # define VM_WRITE_BIT 1
  67. # define VM_EXEC_BIT 2
  68. void __kprobes
  69. ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *regs)
  70. {
  71. int signal = SIGSEGV, code = SEGV_MAPERR;
  72. struct vm_area_struct *vma, *prev_vma;
  73. struct mm_struct *mm = current->mm;
  74. struct siginfo si;
  75. unsigned long mask;
  76. int 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. si.si_signo = signal;
  216. si.si_errno = 0;
  217. si.si_code = code;
  218. si.si_addr = (void __user *) address;
  219. si.si_isr = isr;
  220. si.si_flags = __ISR_VALID;
  221. force_sig_info(signal, &si, current);
  222. return;
  223. }
  224. no_context:
  225. if ((isr & IA64_ISR_SP)
  226. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  227. {
  228. /*
  229. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  230. * bit in the psr to ensure forward progress. (Target register will get a
  231. * NaT for ld.s, lfetch will be canceled.)
  232. */
  233. ia64_psr(regs)->ed = 1;
  234. return;
  235. }
  236. /*
  237. * Since we have no vma's for region 5, we might get here even if the address is
  238. * valid, due to the VHPT walker inserting a non present translation that becomes
  239. * stale. If that happens, the non present fault handler already purged the stale
  240. * translation, which fixed the problem. So, we check to see if the translation is
  241. * valid, and return if it is.
  242. */
  243. if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
  244. return;
  245. if (ia64_done_with_exception(regs))
  246. return;
  247. /*
  248. * Oops. The kernel tried to access some bad page. We'll have to terminate things
  249. * with extreme prejudice.
  250. */
  251. bust_spinlocks(1);
  252. if (address < PAGE_SIZE)
  253. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
  254. else
  255. printk(KERN_ALERT "Unable to handle kernel paging request at "
  256. "virtual address %016lx\n", address);
  257. if (die("Oops", regs, isr))
  258. regs = NULL;
  259. bust_spinlocks(0);
  260. if (regs)
  261. do_exit(SIGKILL);
  262. return;
  263. out_of_memory:
  264. up_read(&mm->mmap_sem);
  265. if (!user_mode(regs))
  266. goto no_context;
  267. pagefault_out_of_memory();
  268. }