fault.c 7.4 KB

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