fault.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. 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() && kprobes_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)
  97. goto bad_area;
  98. /* find_vma_prev() returns vma such that address < vma->vm_end or NULL */
  99. if (address < vma->vm_start)
  100. goto check_expansion;
  101. good_area:
  102. code = SEGV_ACCERR;
  103. /* OK, we've got a good vm_area for this memory area. Check the access permissions: */
  104. # define VM_READ_BIT 0
  105. # define VM_WRITE_BIT 1
  106. # define VM_EXEC_BIT 2
  107. # if (((1 << VM_READ_BIT) != VM_READ || (1 << VM_WRITE_BIT) != VM_WRITE) \
  108. || (1 << VM_EXEC_BIT) != VM_EXEC)
  109. # error File is out of sync with <linux/mm.h>. Please update.
  110. # endif
  111. if (((isr >> IA64_ISR_R_BIT) & 1UL) && (!(vma->vm_flags & (VM_READ | VM_WRITE))))
  112. goto bad_area;
  113. mask = ( (((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
  114. | (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT));
  115. if ((vma->vm_flags & mask) != mask)
  116. goto bad_area;
  117. survive:
  118. /*
  119. * If for any reason at all we couldn't handle the fault, make
  120. * sure we exit gracefully rather than endlessly redo the
  121. * fault.
  122. */
  123. fault = handle_mm_fault(mm, vma, address, (mask & VM_WRITE) != 0);
  124. if (unlikely(fault & VM_FAULT_ERROR)) {
  125. /*
  126. * We ran out of memory, or some other thing happened
  127. * to us that made us unable to handle the page fault
  128. * gracefully.
  129. */
  130. if (fault & VM_FAULT_OOM) {
  131. goto out_of_memory;
  132. } else if (fault & VM_FAULT_SIGBUS) {
  133. signal = SIGBUS;
  134. goto bad_area;
  135. }
  136. BUG();
  137. }
  138. if (fault & VM_FAULT_MAJOR)
  139. current->maj_flt++;
  140. else
  141. current->min_flt++;
  142. up_read(&mm->mmap_sem);
  143. return;
  144. check_expansion:
  145. if (!(prev_vma && (prev_vma->vm_flags & VM_GROWSUP) && (address == prev_vma->vm_end))) {
  146. if (!(vma->vm_flags & VM_GROWSDOWN))
  147. goto bad_area;
  148. if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
  149. || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
  150. goto bad_area;
  151. if (expand_stack(vma, address))
  152. goto bad_area;
  153. } else {
  154. vma = prev_vma;
  155. if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
  156. || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
  157. goto bad_area;
  158. /*
  159. * Since the register backing store is accessed sequentially,
  160. * we disallow growing it by more than a page at a time.
  161. */
  162. if (address > vma->vm_end + PAGE_SIZE - sizeof(long))
  163. goto bad_area;
  164. if (expand_upwards(vma, address))
  165. goto bad_area;
  166. }
  167. goto good_area;
  168. bad_area:
  169. up_read(&mm->mmap_sem);
  170. #ifdef CONFIG_VIRTUAL_MEM_MAP
  171. bad_area_no_up:
  172. #endif
  173. if ((isr & IA64_ISR_SP)
  174. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  175. {
  176. /*
  177. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  178. * bit in the psr to ensure forward progress. (Target register will get a
  179. * NaT for ld.s, lfetch will be canceled.)
  180. */
  181. ia64_psr(regs)->ed = 1;
  182. return;
  183. }
  184. if (user_mode(regs)) {
  185. si.si_signo = signal;
  186. si.si_errno = 0;
  187. si.si_code = code;
  188. si.si_addr = (void __user *) address;
  189. si.si_isr = isr;
  190. si.si_flags = __ISR_VALID;
  191. force_sig_info(signal, &si, current);
  192. return;
  193. }
  194. no_context:
  195. if ((isr & IA64_ISR_SP)
  196. || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
  197. {
  198. /*
  199. * This fault was due to a speculative load or lfetch.fault, set the "ed"
  200. * bit in the psr to ensure forward progress. (Target register will get a
  201. * NaT for ld.s, lfetch will be canceled.)
  202. */
  203. ia64_psr(regs)->ed = 1;
  204. return;
  205. }
  206. /*
  207. * Since we have no vma's for region 5, we might get here even if the address is
  208. * valid, due to the VHPT walker inserting a non present translation that becomes
  209. * stale. If that happens, the non present fault handler already purged the stale
  210. * translation, which fixed the problem. So, we check to see if the translation is
  211. * valid, and return if it is.
  212. */
  213. if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
  214. return;
  215. if (ia64_done_with_exception(regs))
  216. return;
  217. /*
  218. * Oops. The kernel tried to access some bad page. We'll have to terminate things
  219. * with extreme prejudice.
  220. */
  221. bust_spinlocks(1);
  222. if (address < PAGE_SIZE)
  223. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
  224. else
  225. printk(KERN_ALERT "Unable to handle kernel paging request at "
  226. "virtual address %016lx\n", address);
  227. die("Oops", regs, isr);
  228. bust_spinlocks(0);
  229. do_exit(SIGKILL);
  230. return;
  231. out_of_memory:
  232. up_read(&mm->mmap_sem);
  233. if (is_init(current)) {
  234. yield();
  235. down_read(&mm->mmap_sem);
  236. goto survive;
  237. }
  238. printk(KERN_CRIT "VM: killing process %s\n", current->comm);
  239. if (user_mode(regs))
  240. do_exit(SIGKILL);
  241. goto no_context;
  242. }