fault.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. void __kprobes
  64. ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *regs)
  65. {
  66. int signal = SIGSEGV, code = SEGV_MAPERR;
  67. struct vm_area_struct *vma, *prev_vma;
  68. struct mm_struct *mm = current->mm;
  69. struct siginfo si;
  70. unsigned long mask;
  71. int fault;
  72. /* mmap_sem is performance critical.... */
  73. prefetchw(&mm->mmap_sem);
  74. /*
  75. * If we're in an interrupt or have no user context, we must not take the fault..
  76. */
  77. if (in_atomic() || !mm)
  78. goto no_context;
  79. #ifdef CONFIG_VIRTUAL_MEM_MAP
  80. /*
  81. * If fault is in region 5 and we are in the kernel, we may already
  82. * have the mmap_sem (pfn_valid macro is called during mmap). There
  83. * is no vma for region 5 addr's anyway, so skip getting the semaphore
  84. * and go directly to the exception handling code.
  85. */
  86. if ((REGION_NUMBER(address) == 5) && !user_mode(regs))
  87. goto bad_area_no_up;
  88. #endif
  89. /*
  90. * This is to handle the kprobes on user space access instructions
  91. */
  92. if (notify_page_fault(regs, TRAP_BRKPT))
  93. return;
  94. down_read(&mm->mmap_sem);
  95. vma = find_vma_prev(mm, address, &prev_vma);
  96. if (!vma && !prev_vma )
  97. goto bad_area;
  98. /*
  99. * find_vma_prev() returns vma such that address < vma->vm_end or NULL
  100. *
  101. * May find no vma, but could be that the last vm area is the
  102. * register backing store that needs to expand upwards, in
  103. * this case vma will be null, but prev_vma will ne non-null
  104. */
  105. if (( !vma && prev_vma ) || (address < vma->vm_start) )
  106. goto check_expansion;
  107. good_area:
  108. code = SEGV_ACCERR;
  109. /* OK, we've got a good vm_area for this memory area. Check the access permissions: */
  110. # define VM_READ_BIT 0
  111. # define VM_WRITE_BIT 1
  112. # define VM_EXEC_BIT 2
  113. # if (((1 << VM_READ_BIT) != VM_READ || (1 << VM_WRITE_BIT) != VM_WRITE) \
  114. || (1 << VM_EXEC_BIT) != VM_EXEC)
  115. # error File is out of sync with <linux/mm.h>. Please update.
  116. # endif
  117. if (((isr >> IA64_ISR_R_BIT) & 1UL) && (!(vma->vm_flags & (VM_READ | VM_WRITE))))
  118. goto bad_area;
  119. mask = ( (((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
  120. | (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT));
  121. if ((vma->vm_flags & mask) != mask)
  122. goto bad_area;
  123. /*
  124. * If for any reason at all we couldn't handle the fault, make
  125. * sure we exit gracefully rather than endlessly redo the
  126. * fault.
  127. */
  128. fault = handle_mm_fault(mm, vma, address, (mask & VM_WRITE) ? FAULT_FLAG_WRITE : 0);
  129. if (unlikely(fault & VM_FAULT_ERROR)) {
  130. /*
  131. * We ran out of memory, or some other thing happened
  132. * to us that made us unable to handle the page fault
  133. * gracefully.
  134. */
  135. if (fault & VM_FAULT_OOM) {
  136. goto out_of_memory;
  137. } else if (fault & VM_FAULT_SIGSEGV) {
  138. goto bad_area;
  139. } else if (fault & VM_FAULT_SIGBUS) {
  140. signal = SIGBUS;
  141. goto bad_area;
  142. }
  143. BUG();
  144. }
  145. if (fault & VM_FAULT_MAJOR)
  146. current->maj_flt++;
  147. else
  148. current->min_flt++;
  149. up_read(&mm->mmap_sem);
  150. return;
  151. check_expansion:
  152. if (!(prev_vma && (prev_vma->vm_flags & VM_GROWSUP) && (address == prev_vma->vm_end))) {
  153. if (!vma)
  154. goto bad_area;
  155. if (!(vma->vm_flags & VM_GROWSDOWN))
  156. goto bad_area;
  157. if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
  158. || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
  159. goto bad_area;
  160. if (expand_stack(vma, address))
  161. goto bad_area;
  162. } else {
  163. vma = prev_vma;
  164. if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
  165. || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
  166. goto bad_area;
  167. /*
  168. * Since the register backing store is accessed sequentially,
  169. * we disallow growing it by more than a page at a time.
  170. */
  171. if (address > vma->vm_end + PAGE_SIZE - sizeof(long))
  172. goto bad_area;
  173. if (expand_upwards(vma, address))
  174. goto bad_area;
  175. }
  176. goto good_area;
  177. bad_area:
  178. up_read(&mm->mmap_sem);
  179. #ifdef CONFIG_VIRTUAL_MEM_MAP
  180. bad_area_no_up:
  181. #endif
  182. if ((isr & IA64_ISR_SP)
  183. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  184. {
  185. /*
  186. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  187. * bit in the psr to ensure forward progress. (Target register will get a
  188. * NaT for ld.s, lfetch will be canceled.)
  189. */
  190. ia64_psr(regs)->ed = 1;
  191. return;
  192. }
  193. if (user_mode(regs)) {
  194. si.si_signo = signal;
  195. si.si_errno = 0;
  196. si.si_code = code;
  197. si.si_addr = (void __user *) address;
  198. si.si_isr = isr;
  199. si.si_flags = __ISR_VALID;
  200. force_sig_info(signal, &si, current);
  201. return;
  202. }
  203. no_context:
  204. if ((isr & IA64_ISR_SP)
  205. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  206. {
  207. /*
  208. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  209. * bit in the psr to ensure forward progress. (Target register will get a
  210. * NaT for ld.s, lfetch will be canceled.)
  211. */
  212. ia64_psr(regs)->ed = 1;
  213. return;
  214. }
  215. /*
  216. * Since we have no vma's for region 5, we might get here even if the address is
  217. * valid, due to the VHPT walker inserting a non present translation that becomes
  218. * stale. If that happens, the non present fault handler already purged the stale
  219. * translation, which fixed the problem. So, we check to see if the translation is
  220. * valid, and return if it is.
  221. */
  222. if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
  223. return;
  224. if (ia64_done_with_exception(regs))
  225. return;
  226. /*
  227. * Oops. The kernel tried to access some bad page. We'll have to terminate things
  228. * with extreme prejudice.
  229. */
  230. bust_spinlocks(1);
  231. if (address < PAGE_SIZE)
  232. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
  233. else
  234. printk(KERN_ALERT "Unable to handle kernel paging request at "
  235. "virtual address %016lx\n", address);
  236. if (die("Oops", regs, isr))
  237. regs = NULL;
  238. bust_spinlocks(0);
  239. if (regs)
  240. do_exit(SIGKILL);
  241. return;
  242. out_of_memory:
  243. up_read(&mm->mmap_sem);
  244. if (!user_mode(regs))
  245. goto no_context;
  246. pagefault_out_of_memory();
  247. }