fault.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4. * Lennox Wu <lennox.wu@sunplusct.com>
  5. * Chen Liqin <liqin.chen@sunplusct.com>
  6. * Copyright (C) 2012 Regents of the University of California
  7. */
  8. #include <linux/mm.h>
  9. #include <linux/kernel.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/perf_event.h>
  12. #include <linux/signal.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/kprobes.h>
  15. #include <asm/ptrace.h>
  16. #include <asm/tlbflush.h>
  17. #include "../kernel/head.h"
  18. static inline void no_context(struct pt_regs *regs, unsigned long addr)
  19. {
  20. /* Are we prepared to handle this kernel fault? */
  21. if (fixup_exception(regs))
  22. return;
  23. /*
  24. * Oops. The kernel tried to access some bad page. We'll have to
  25. * terminate things with extreme prejudice.
  26. */
  27. bust_spinlocks(1);
  28. pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n",
  29. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  30. "paging request", addr);
  31. die(regs, "Oops");
  32. do_exit(SIGKILL);
  33. }
  34. static inline void mm_fault_error(struct pt_regs *regs, unsigned long addr, vm_fault_t fault)
  35. {
  36. if (fault & VM_FAULT_OOM) {
  37. /*
  38. * We ran out of memory, call the OOM killer, and return the userspace
  39. * (which will retry the fault, or kill us if we got oom-killed).
  40. */
  41. if (!user_mode(regs)) {
  42. no_context(regs, addr);
  43. return;
  44. }
  45. pagefault_out_of_memory();
  46. return;
  47. } else if (fault & VM_FAULT_SIGBUS) {
  48. /* Kernel mode? Handle exceptions or die */
  49. if (!user_mode(regs)) {
  50. no_context(regs, addr);
  51. return;
  52. }
  53. do_trap(regs, SIGBUS, BUS_ADRERR, addr);
  54. return;
  55. }
  56. BUG();
  57. }
  58. static inline void bad_area(struct pt_regs *regs, struct mm_struct *mm, int code, unsigned long addr)
  59. {
  60. /*
  61. * Something tried to access memory that isn't in our memory map.
  62. * Fix it, but check if it's kernel or user first.
  63. */
  64. mmap_read_unlock(mm);
  65. /* User mode accesses just cause a SIGSEGV */
  66. if (user_mode(regs)) {
  67. do_trap(regs, SIGSEGV, code, addr);
  68. return;
  69. }
  70. no_context(regs, addr);
  71. }
  72. static inline void vmalloc_fault(struct pt_regs *regs, int code, unsigned long addr)
  73. {
  74. pgd_t *pgd, *pgd_k;
  75. pud_t *pud, *pud_k;
  76. p4d_t *p4d, *p4d_k;
  77. pmd_t *pmd, *pmd_k;
  78. pte_t *pte_k;
  79. int index;
  80. unsigned long pfn;
  81. /* User mode accesses just cause a SIGSEGV */
  82. if (user_mode(regs))
  83. return do_trap(regs, SIGSEGV, code, addr);
  84. /*
  85. * Synchronize this task's top level page-table
  86. * with the 'reference' page table.
  87. *
  88. * Do _not_ use "tsk->active_mm->pgd" here.
  89. * We might be inside an interrupt in the middle
  90. * of a task switch.
  91. */
  92. index = pgd_index(addr);
  93. pfn = csr_read(CSR_SATP) & SATP_PPN;
  94. pgd = (pgd_t *)pfn_to_virt(pfn) + index;
  95. pgd_k = init_mm.pgd + index;
  96. if (!pgd_present(*pgd_k)) {
  97. no_context(regs, addr);
  98. return;
  99. }
  100. set_pgd(pgd, *pgd_k);
  101. p4d = p4d_offset(pgd, addr);
  102. p4d_k = p4d_offset(pgd_k, addr);
  103. if (!p4d_present(*p4d_k)) {
  104. no_context(regs, addr);
  105. return;
  106. }
  107. pud = pud_offset(p4d, addr);
  108. pud_k = pud_offset(p4d_k, addr);
  109. if (!pud_present(*pud_k)) {
  110. no_context(regs, addr);
  111. return;
  112. }
  113. /*
  114. * Since the vmalloc area is global, it is unnecessary
  115. * to copy individual PTEs
  116. */
  117. pmd = pmd_offset(pud, addr);
  118. pmd_k = pmd_offset(pud_k, addr);
  119. if (!pmd_present(*pmd_k)) {
  120. no_context(regs, addr);
  121. return;
  122. }
  123. set_pmd(pmd, *pmd_k);
  124. /*
  125. * Make sure the actual PTE exists as well to
  126. * catch kernel vmalloc-area accesses to non-mapped
  127. * addresses. If we don't do this, this will just
  128. * silently loop forever.
  129. */
  130. pte_k = pte_offset_kernel(pmd_k, addr);
  131. if (!pte_present(*pte_k)) {
  132. no_context(regs, addr);
  133. return;
  134. }
  135. /*
  136. * The kernel assumes that TLBs don't cache invalid
  137. * entries, but in RISC-V, SFENCE.VMA specifies an
  138. * ordering constraint, not a cache flush; it is
  139. * necessary even after writing invalid entries.
  140. */
  141. local_flush_tlb_page(addr);
  142. }
  143. static inline bool access_error(unsigned long cause, struct vm_area_struct *vma)
  144. {
  145. switch (cause) {
  146. case EXC_INST_PAGE_FAULT:
  147. if (!(vma->vm_flags & VM_EXEC)) {
  148. return true;
  149. }
  150. break;
  151. case EXC_LOAD_PAGE_FAULT:
  152. if (!(vma->vm_flags & VM_READ)) {
  153. return true;
  154. }
  155. break;
  156. case EXC_STORE_PAGE_FAULT:
  157. if (!(vma->vm_flags & VM_WRITE)) {
  158. return true;
  159. }
  160. break;
  161. default:
  162. panic("%s: unhandled cause %lu", __func__, cause);
  163. }
  164. return false;
  165. }
  166. /*
  167. * This routine handles page faults. It determines the address and the
  168. * problem, and then passes it off to one of the appropriate routines.
  169. */
  170. asmlinkage void do_page_fault(struct pt_regs *regs)
  171. {
  172. struct task_struct *tsk;
  173. struct vm_area_struct *vma;
  174. struct mm_struct *mm;
  175. unsigned long addr, cause;
  176. unsigned int flags = FAULT_FLAG_DEFAULT;
  177. int code = SEGV_MAPERR;
  178. vm_fault_t fault;
  179. cause = regs->cause;
  180. addr = regs->badaddr;
  181. tsk = current;
  182. mm = tsk->mm;
  183. if (kprobe_page_fault(regs, cause))
  184. return;
  185. /*
  186. * Fault-in kernel-space virtual memory on-demand.
  187. * The 'reference' page table is init_mm.pgd.
  188. *
  189. * NOTE! We MUST NOT take any locks for this case. We may
  190. * be in an interrupt or a critical region, and should
  191. * only copy the information from the master page table,
  192. * nothing more.
  193. */
  194. if (unlikely((addr >= VMALLOC_START) && (addr <= VMALLOC_END))) {
  195. vmalloc_fault(regs, code, addr);
  196. return;
  197. }
  198. /* Enable interrupts if they were enabled in the parent context. */
  199. if (likely(regs->status & SR_PIE) || user_mode(regs))
  200. local_irq_enable();
  201. /*
  202. * If we're in an interrupt, have no user context, or are running
  203. * in an atomic region, then we must not take the fault.
  204. */
  205. if (unlikely(faulthandler_disabled() || !mm)) {
  206. tsk->thread.bad_cause = cause;
  207. no_context(regs, addr);
  208. return;
  209. }
  210. if (user_mode(regs))
  211. flags |= FAULT_FLAG_USER;
  212. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr);
  213. if (cause == EXC_STORE_PAGE_FAULT)
  214. flags |= FAULT_FLAG_WRITE;
  215. else if (cause == EXC_INST_PAGE_FAULT)
  216. flags |= FAULT_FLAG_INSTRUCTION;
  217. retry:
  218. mmap_read_lock(mm);
  219. vma = find_vma(mm, addr);
  220. if (unlikely(!vma)) {
  221. tsk->thread.bad_cause = cause;
  222. bad_area(regs, mm, code, addr);
  223. return;
  224. }
  225. if (likely(vma->vm_start <= addr))
  226. goto good_area;
  227. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  228. tsk->thread.bad_cause = cause;
  229. bad_area(regs, mm, code, addr);
  230. return;
  231. }
  232. if (unlikely(expand_stack(vma, addr))) {
  233. tsk->thread.bad_cause = cause;
  234. bad_area(regs, mm, code, addr);
  235. return;
  236. }
  237. /*
  238. * Ok, we have a good vm_area for this memory access, so
  239. * we can handle it.
  240. */
  241. good_area:
  242. code = SEGV_ACCERR;
  243. if (unlikely(access_error(cause, vma))) {
  244. tsk->thread.bad_cause = cause;
  245. bad_area(regs, mm, code, addr);
  246. return;
  247. }
  248. /*
  249. * If for any reason at all we could not handle the fault,
  250. * make sure we exit gracefully rather than endlessly redo
  251. * the fault.
  252. */
  253. fault = handle_mm_fault(vma, addr, flags, regs);
  254. /*
  255. * If we need to retry but a fatal signal is pending, handle the
  256. * signal first. We do not need to release the mmap_lock because it
  257. * would already be released in __lock_page_or_retry in mm/filemap.c.
  258. */
  259. if (fault_signal_pending(fault, regs))
  260. return;
  261. if (unlikely((fault & VM_FAULT_RETRY) && (flags & FAULT_FLAG_ALLOW_RETRY))) {
  262. flags |= FAULT_FLAG_TRIED;
  263. /*
  264. * No need to mmap_read_unlock(mm) as we would
  265. * have already released it in __lock_page_or_retry
  266. * in mm/filemap.c.
  267. */
  268. goto retry;
  269. }
  270. mmap_read_unlock(mm);
  271. if (unlikely(fault & VM_FAULT_ERROR)) {
  272. tsk->thread.bad_cause = cause;
  273. mm_fault_error(regs, addr, fault);
  274. return;
  275. }
  276. return;
  277. }