fault.c 7.6 KB

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