fault.c 7.0 KB

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