fault.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  3. * Lennox Wu <lennox.wu@sunplusct.com>
  4. * Chen Liqin <liqin.chen@sunplusct.com>
  5. * Copyright (C) 2012 Regents of the University of California
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see the file COPYING, or write
  19. * to the Free Software Foundation, Inc.,
  20. */
  21. #include <linux/mm.h>
  22. #include <linux/kernel.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/signal.h>
  26. #include <linux/uaccess.h>
  27. #include <asm/pgalloc.h>
  28. #include <asm/ptrace.h>
  29. /*
  30. * This routine handles page faults. It determines the address and the
  31. * problem, and then passes it off to one of the appropriate routines.
  32. */
  33. asmlinkage void do_page_fault(struct pt_regs *regs)
  34. {
  35. struct task_struct *tsk;
  36. struct vm_area_struct *vma;
  37. struct mm_struct *mm;
  38. unsigned long addr, cause;
  39. unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  40. int code = SEGV_MAPERR;
  41. vm_fault_t fault;
  42. cause = regs->scause;
  43. addr = regs->sbadaddr;
  44. tsk = current;
  45. mm = tsk->mm;
  46. /*
  47. * Fault-in kernel-space virtual memory on-demand.
  48. * The 'reference' page table is init_mm.pgd.
  49. *
  50. * NOTE! We MUST NOT take any locks for this case. We may
  51. * be in an interrupt or a critical region, and should
  52. * only copy the information from the master page table,
  53. * nothing more.
  54. */
  55. if (unlikely((addr >= VMALLOC_START) && (addr <= VMALLOC_END)))
  56. goto vmalloc_fault;
  57. /* Enable interrupts if they were enabled in the parent context. */
  58. if (likely(regs->sstatus & SR_SPIE))
  59. local_irq_enable();
  60. /*
  61. * If we're in an interrupt, have no user context, or are running
  62. * in an atomic region, then we must not take the fault.
  63. */
  64. if (unlikely(faulthandler_disabled() || !mm))
  65. goto no_context;
  66. if (user_mode(regs))
  67. flags |= FAULT_FLAG_USER;
  68. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  69. retry:
  70. down_read(&mm->mmap_sem);
  71. vma = find_vma(mm, addr);
  72. if (unlikely(!vma))
  73. goto bad_area;
  74. if (likely(vma->vm_start <= addr))
  75. goto good_area;
  76. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
  77. goto bad_area;
  78. if (unlikely(expand_stack(vma, addr)))
  79. goto bad_area;
  80. /*
  81. * Ok, we have a good vm_area for this memory access, so
  82. * we can handle it.
  83. */
  84. good_area:
  85. code = SEGV_ACCERR;
  86. switch (cause) {
  87. case EXC_INST_PAGE_FAULT:
  88. if (!(vma->vm_flags & VM_EXEC))
  89. goto bad_area;
  90. break;
  91. case EXC_LOAD_PAGE_FAULT:
  92. if (!(vma->vm_flags & VM_READ))
  93. goto bad_area;
  94. break;
  95. case EXC_STORE_PAGE_FAULT:
  96. if (!(vma->vm_flags & VM_WRITE))
  97. goto bad_area;
  98. flags |= FAULT_FLAG_WRITE;
  99. break;
  100. default:
  101. panic("%s: unhandled cause %lu", __func__, cause);
  102. }
  103. /*
  104. * If for any reason at all we could not handle the fault,
  105. * make sure we exit gracefully rather than endlessly redo
  106. * the fault.
  107. */
  108. fault = handle_mm_fault(vma, addr, flags);
  109. /*
  110. * If we need to retry but a fatal signal is pending, handle the
  111. * signal first. We do not need to release the mmap_sem because it
  112. * would already be released in __lock_page_or_retry in mm/filemap.c.
  113. */
  114. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(tsk))
  115. return;
  116. if (unlikely(fault & VM_FAULT_ERROR)) {
  117. if (fault & VM_FAULT_OOM)
  118. goto out_of_memory;
  119. else if (fault & VM_FAULT_SIGBUS)
  120. goto do_sigbus;
  121. BUG();
  122. }
  123. /*
  124. * Major/minor page fault accounting is only done on the
  125. * initial attempt. If we go through a retry, it is extremely
  126. * likely that the page will be found in page cache at that point.
  127. */
  128. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  129. if (fault & VM_FAULT_MAJOR) {
  130. tsk->maj_flt++;
  131. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
  132. 1, regs, addr);
  133. } else {
  134. tsk->min_flt++;
  135. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
  136. 1, regs, addr);
  137. }
  138. if (fault & VM_FAULT_RETRY) {
  139. /*
  140. * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  141. * of starvation.
  142. */
  143. flags &= ~(FAULT_FLAG_ALLOW_RETRY);
  144. flags |= FAULT_FLAG_TRIED;
  145. /*
  146. * No need to up_read(&mm->mmap_sem) as we would
  147. * have already released it in __lock_page_or_retry
  148. * in mm/filemap.c.
  149. */
  150. goto retry;
  151. }
  152. }
  153. up_read(&mm->mmap_sem);
  154. return;
  155. /*
  156. * Something tried to access memory that isn't in our memory map.
  157. * Fix it, but check if it's kernel or user first.
  158. */
  159. bad_area:
  160. up_read(&mm->mmap_sem);
  161. /* User mode accesses just cause a SIGSEGV */
  162. if (user_mode(regs)) {
  163. do_trap(regs, SIGSEGV, code, addr, tsk);
  164. return;
  165. }
  166. no_context:
  167. /* Are we prepared to handle this kernel fault? */
  168. if (fixup_exception(regs))
  169. return;
  170. /*
  171. * Oops. The kernel tried to access some bad page. We'll have to
  172. * terminate things with extreme prejudice.
  173. */
  174. bust_spinlocks(1);
  175. pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n",
  176. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  177. "paging request", addr);
  178. die(regs, "Oops");
  179. do_exit(SIGKILL);
  180. /*
  181. * We ran out of memory, call the OOM killer, and return the userspace
  182. * (which will retry the fault, or kill us if we got oom-killed).
  183. */
  184. out_of_memory:
  185. up_read(&mm->mmap_sem);
  186. if (!user_mode(regs))
  187. goto no_context;
  188. pagefault_out_of_memory();
  189. return;
  190. do_sigbus:
  191. up_read(&mm->mmap_sem);
  192. /* Kernel mode? Handle exceptions or die */
  193. if (!user_mode(regs))
  194. goto no_context;
  195. do_trap(regs, SIGBUS, BUS_ADRERR, addr, tsk);
  196. return;
  197. vmalloc_fault:
  198. {
  199. pgd_t *pgd, *pgd_k;
  200. pud_t *pud, *pud_k;
  201. p4d_t *p4d, *p4d_k;
  202. pmd_t *pmd, *pmd_k;
  203. pte_t *pte_k;
  204. int index;
  205. if (user_mode(regs))
  206. goto bad_area;
  207. /*
  208. * Synchronize this task's top level page-table
  209. * with the 'reference' page table.
  210. *
  211. * Do _not_ use "tsk->active_mm->pgd" here.
  212. * We might be inside an interrupt in the middle
  213. * of a task switch.
  214. *
  215. * Note: Use the old spbtr name instead of using the current
  216. * satp name to support binutils 2.29 which doesn't know about
  217. * the privileged ISA 1.10 yet.
  218. */
  219. index = pgd_index(addr);
  220. pgd = (pgd_t *)pfn_to_virt(csr_read(sptbr)) + index;
  221. pgd_k = init_mm.pgd + index;
  222. if (!pgd_present(*pgd_k))
  223. goto no_context;
  224. set_pgd(pgd, *pgd_k);
  225. p4d = p4d_offset(pgd, addr);
  226. p4d_k = p4d_offset(pgd_k, addr);
  227. if (!p4d_present(*p4d_k))
  228. goto no_context;
  229. pud = pud_offset(p4d, addr);
  230. pud_k = pud_offset(p4d_k, addr);
  231. if (!pud_present(*pud_k))
  232. goto no_context;
  233. /*
  234. * Since the vmalloc area is global, it is unnecessary
  235. * to copy individual PTEs
  236. */
  237. pmd = pmd_offset(pud, addr);
  238. pmd_k = pmd_offset(pud_k, addr);
  239. if (!pmd_present(*pmd_k))
  240. goto no_context;
  241. set_pmd(pmd, *pmd_k);
  242. /*
  243. * Make sure the actual PTE exists as well to
  244. * catch kernel vmalloc-area accesses to non-mapped
  245. * addresses. If we don't do this, this will just
  246. * silently loop forever.
  247. */
  248. pte_k = pte_offset_kernel(pmd_k, addr);
  249. if (!pte_present(*pte_k))
  250. goto no_context;
  251. return;
  252. }
  253. }