fault.c 7.4 KB

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