fault.c 6.5 KB

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