fault.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 fault, code = SEGV_MAPERR;
  41. cause = regs->scause;
  42. addr = regs->sbadaddr;
  43. tsk = current;
  44. mm = tsk->mm;
  45. /*
  46. * Fault-in kernel-space virtual memory on-demand.
  47. * The 'reference' page table is init_mm.pgd.
  48. *
  49. * NOTE! We MUST NOT take any locks for this case. We may
  50. * be in an interrupt or a critical region, and should
  51. * only copy the information from the master page table,
  52. * nothing more.
  53. */
  54. if (unlikely((addr >= VMALLOC_START) && (addr <= VMALLOC_END)))
  55. goto vmalloc_fault;
  56. /* Enable interrupts if they were enabled in the parent context. */
  57. if (likely(regs->sstatus & SR_SPIE))
  58. local_irq_enable();
  59. /*
  60. * If we're in an interrupt, have no user context, or are running
  61. * in an atomic region, then we must not take the fault.
  62. */
  63. if (unlikely(faulthandler_disabled() || !mm))
  64. goto no_context;
  65. if (user_mode(regs))
  66. flags |= FAULT_FLAG_USER;
  67. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  68. retry:
  69. down_read(&mm->mmap_sem);
  70. vma = find_vma(mm, addr);
  71. if (unlikely(!vma))
  72. goto bad_area;
  73. if (likely(vma->vm_start <= addr))
  74. goto good_area;
  75. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
  76. goto bad_area;
  77. if (unlikely(expand_stack(vma, addr)))
  78. goto bad_area;
  79. /*
  80. * Ok, we have a good vm_area for this memory access, so
  81. * we can handle it.
  82. */
  83. good_area:
  84. code = SEGV_ACCERR;
  85. switch (cause) {
  86. case EXC_INST_PAGE_FAULT:
  87. if (!(vma->vm_flags & VM_EXEC))
  88. goto bad_area;
  89. break;
  90. case EXC_LOAD_PAGE_FAULT:
  91. if (!(vma->vm_flags & VM_READ))
  92. goto bad_area;
  93. break;
  94. case EXC_STORE_PAGE_FAULT:
  95. if (!(vma->vm_flags & VM_WRITE))
  96. goto bad_area;
  97. flags |= FAULT_FLAG_WRITE;
  98. break;
  99. default:
  100. panic("%s: unhandled cause %lu", __func__, cause);
  101. }
  102. /*
  103. * If for any reason at all we could not handle the fault,
  104. * make sure we exit gracefully rather than endlessly redo
  105. * the fault.
  106. */
  107. fault = handle_mm_fault(vma, addr, flags);
  108. /*
  109. * If we need to retry but a fatal signal is pending, handle the
  110. * signal first. We do not need to release the mmap_sem because it
  111. * would already be released in __lock_page_or_retry in mm/filemap.c.
  112. */
  113. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(tsk))
  114. return;
  115. if (unlikely(fault & VM_FAULT_ERROR)) {
  116. if (fault & VM_FAULT_OOM)
  117. goto out_of_memory;
  118. else if (fault & VM_FAULT_SIGBUS)
  119. goto do_sigbus;
  120. BUG();
  121. }
  122. /*
  123. * Major/minor page fault accounting is only done on the
  124. * initial attempt. If we go through a retry, it is extremely
  125. * likely that the page will be found in page cache at that point.
  126. */
  127. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  128. if (fault & VM_FAULT_MAJOR) {
  129. tsk->maj_flt++;
  130. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ,
  131. 1, regs, addr);
  132. } else {
  133. tsk->min_flt++;
  134. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN,
  135. 1, regs, addr);
  136. }
  137. if (fault & VM_FAULT_RETRY) {
  138. /*
  139. * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  140. * of starvation.
  141. */
  142. flags &= ~(FAULT_FLAG_ALLOW_RETRY);
  143. flags |= FAULT_FLAG_TRIED;
  144. /*
  145. * No need to up_read(&mm->mmap_sem) as we would
  146. * have already released it in __lock_page_or_retry
  147. * in mm/filemap.c.
  148. */
  149. goto retry;
  150. }
  151. }
  152. up_read(&mm->mmap_sem);
  153. return;
  154. /*
  155. * Something tried to access memory that isn't in our memory map.
  156. * Fix it, but check if it's kernel or user first.
  157. */
  158. bad_area:
  159. up_read(&mm->mmap_sem);
  160. /* User mode accesses just cause a SIGSEGV */
  161. if (user_mode(regs)) {
  162. do_trap(regs, SIGSEGV, code, addr, tsk);
  163. return;
  164. }
  165. no_context:
  166. /* Are we prepared to handle this kernel fault? */
  167. if (fixup_exception(regs))
  168. return;
  169. /*
  170. * Oops. The kernel tried to access some bad page. We'll have to
  171. * terminate things with extreme prejudice.
  172. */
  173. bust_spinlocks(1);
  174. pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n",
  175. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  176. "paging request", addr);
  177. die(regs, "Oops");
  178. do_exit(SIGKILL);
  179. /*
  180. * We ran out of memory, call the OOM killer, and return the userspace
  181. * (which will retry the fault, or kill us if we got oom-killed).
  182. */
  183. out_of_memory:
  184. up_read(&mm->mmap_sem);
  185. if (!user_mode(regs))
  186. goto no_context;
  187. pagefault_out_of_memory();
  188. return;
  189. do_sigbus:
  190. up_read(&mm->mmap_sem);
  191. /* Kernel mode? Handle exceptions or die */
  192. if (!user_mode(regs))
  193. goto no_context;
  194. do_trap(regs, SIGBUS, BUS_ADRERR, addr, tsk);
  195. return;
  196. vmalloc_fault:
  197. {
  198. pgd_t *pgd, *pgd_k;
  199. pud_t *pud, *pud_k;
  200. p4d_t *p4d, *p4d_k;
  201. pmd_t *pmd, *pmd_k;
  202. pte_t *pte_k;
  203. int index;
  204. if (user_mode(regs))
  205. goto bad_area;
  206. /*
  207. * Synchronize this task's top level page-table
  208. * with the 'reference' page table.
  209. *
  210. * Do _not_ use "tsk->active_mm->pgd" here.
  211. * We might be inside an interrupt in the middle
  212. * of a task switch.
  213. *
  214. * Note: Use the old spbtr name instead of using the current
  215. * satp name to support binutils 2.29 which doesn't know about
  216. * the privileged ISA 1.10 yet.
  217. */
  218. index = pgd_index(addr);
  219. pgd = (pgd_t *)pfn_to_virt(csr_read(sptbr)) + index;
  220. pgd_k = init_mm.pgd + index;
  221. if (!pgd_present(*pgd_k))
  222. goto no_context;
  223. set_pgd(pgd, *pgd_k);
  224. p4d = p4d_offset(pgd, addr);
  225. p4d_k = p4d_offset(pgd_k, addr);
  226. if (!p4d_present(*p4d_k))
  227. goto no_context;
  228. pud = pud_offset(p4d, addr);
  229. pud_k = pud_offset(p4d_k, addr);
  230. if (!pud_present(*pud_k))
  231. goto no_context;
  232. /*
  233. * Since the vmalloc area is global, it is unnecessary
  234. * to copy individual PTEs
  235. */
  236. pmd = pmd_offset(pud, addr);
  237. pmd_k = pmd_offset(pud_k, addr);
  238. if (!pmd_present(*pmd_k))
  239. goto no_context;
  240. set_pmd(pmd, *pmd_k);
  241. /*
  242. * Make sure the actual PTE exists as well to
  243. * catch kernel vmalloc-area accesses to non-mapped
  244. * addresses. If we don't do this, this will just
  245. * silently loop forever.
  246. */
  247. pte_k = pte_offset_kernel(pmd_k, addr);
  248. if (!pte_present(*pte_k))
  249. goto no_context;
  250. return;
  251. }
  252. }