fault.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. mask = ( (((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
  124. | (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT)
  125. | (((isr >> IA64_ISR_R_BIT) & 1UL) << VM_READ_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 (current->pid == 1) {
  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. }