fault.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. flags |= FAULT_FLAG_TRIED;
  156. /* No need to up_read(&mm->mmap_sem) as we would
  157. * have already released it in __lock_page_or_retry
  158. * in mm/filemap.c.
  159. */
  160. goto retry;
  161. }
  162. }
  163. up_read(&mm->mmap_sem);
  164. return;
  165. check_expansion:
  166. if (!(prev_vma && (prev_vma->vm_flags & VM_GROWSUP) && (address == prev_vma->vm_end))) {
  167. if (!vma)
  168. goto bad_area;
  169. if (!(vma->vm_flags & VM_GROWSDOWN))
  170. goto bad_area;
  171. if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
  172. || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
  173. goto bad_area;
  174. if (expand_stack(vma, address))
  175. goto bad_area;
  176. } else {
  177. vma = prev_vma;
  178. if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
  179. || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
  180. goto bad_area;
  181. /*
  182. * Since the register backing store is accessed sequentially,
  183. * we disallow growing it by more than a page at a time.
  184. */
  185. if (address > vma->vm_end + PAGE_SIZE - sizeof(long))
  186. goto bad_area;
  187. if (expand_upwards(vma, address))
  188. goto bad_area;
  189. }
  190. goto good_area;
  191. bad_area:
  192. up_read(&mm->mmap_sem);
  193. #ifdef CONFIG_VIRTUAL_MEM_MAP
  194. bad_area_no_up:
  195. #endif
  196. if ((isr & IA64_ISR_SP)
  197. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  198. {
  199. /*
  200. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  201. * bit in the psr to ensure forward progress. (Target register will get a
  202. * NaT for ld.s, lfetch will be canceled.)
  203. */
  204. ia64_psr(regs)->ed = 1;
  205. return;
  206. }
  207. if (user_mode(regs)) {
  208. si.si_signo = signal;
  209. si.si_errno = 0;
  210. si.si_code = code;
  211. si.si_addr = (void __user *) address;
  212. si.si_isr = isr;
  213. si.si_flags = __ISR_VALID;
  214. force_sig_info(signal, &si, current);
  215. return;
  216. }
  217. no_context:
  218. if ((isr & IA64_ISR_SP)
  219. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  220. {
  221. /*
  222. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  223. * bit in the psr to ensure forward progress. (Target register will get a
  224. * NaT for ld.s, lfetch will be canceled.)
  225. */
  226. ia64_psr(regs)->ed = 1;
  227. return;
  228. }
  229. /*
  230. * Since we have no vma's for region 5, we might get here even if the address is
  231. * valid, due to the VHPT walker inserting a non present translation that becomes
  232. * stale. If that happens, the non present fault handler already purged the stale
  233. * translation, which fixed the problem. So, we check to see if the translation is
  234. * valid, and return if it is.
  235. */
  236. if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
  237. return;
  238. if (ia64_done_with_exception(regs))
  239. return;
  240. /*
  241. * Oops. The kernel tried to access some bad page. We'll have to terminate things
  242. * with extreme prejudice.
  243. */
  244. bust_spinlocks(1);
  245. if (address < PAGE_SIZE)
  246. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
  247. else
  248. printk(KERN_ALERT "Unable to handle kernel paging request at "
  249. "virtual address %016lx\n", address);
  250. if (die("Oops", regs, isr))
  251. regs = NULL;
  252. bust_spinlocks(0);
  253. if (regs)
  254. do_exit(SIGKILL);
  255. return;
  256. out_of_memory:
  257. up_read(&mm->mmap_sem);
  258. if (!user_mode(regs))
  259. goto no_context;
  260. pagefault_out_of_memory();
  261. }