fault.c 6.5 KB

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