fault.c 7.7 KB

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