fault.c 7.6 KB

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